tabs.c revision 20d2c4d2
120d2c4d2Smrg/* $XTermId: tabs.c,v 1.38 2010/04/17 17:11:43 tom Exp $ */
2d522f475Smrg
3d522f475Smrg/*
420d2c4d2Smrg * Copyright 2000-2009,2010 by Thomas E. Dickey
5d522f475Smrg *
6d522f475Smrg *                         All Rights Reserved
7d522f475Smrg *
8d522f475Smrg * Permission is hereby granted, free of charge, to any person obtaining a
9d522f475Smrg * copy of this software and associated documentation files (the
10d522f475Smrg * "Software"), to deal in the Software without restriction, including
11d522f475Smrg * without limitation the rights to use, copy, modify, merge, publish,
12d522f475Smrg * distribute, sublicense, and/or sell copies of the Software, and to
13d522f475Smrg * permit persons to whom the Software is furnished to do so, subject to
14d522f475Smrg * the following conditions:
15d522f475Smrg *
16d522f475Smrg * The above copyright notice and this permission notice shall be included
17d522f475Smrg * in all copies or substantial portions of the Software.
18d522f475Smrg *
19d522f475Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20d522f475Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21d522f475Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22d522f475Smrg * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
23d522f475Smrg * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24d522f475Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25d522f475Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26d522f475Smrg *
27d522f475Smrg * Except as contained in this notice, the name(s) of the above copyright
28d522f475Smrg * holders shall not be used in advertising or otherwise to promote the
29d522f475Smrg * sale, use or other dealings in this Software without prior written
30d522f475Smrg * authorization.
31d522f475Smrg *
32d522f475Smrg * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
33d522f475Smrg *
34d522f475Smrg *                         All Rights Reserved
35d522f475Smrg *
36d522f475Smrg * Permission to use, copy, modify, and distribute this software and its
37d522f475Smrg * documentation for any purpose and without fee is hereby granted,
38d522f475Smrg * provided that the above copyright notice appear in all copies and that
39d522f475Smrg * both that copyright notice and this permission notice appear in
40d522f475Smrg * supporting documentation, and that the name of Digital Equipment
41d522f475Smrg * Corporation not be used in advertising or publicity pertaining to
42d522f475Smrg * distribution of the software without specific, written prior permission.
43d522f475Smrg *
44d522f475Smrg *
45d522f475Smrg * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
46d522f475Smrg * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
47d522f475Smrg * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
48d522f475Smrg * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
49d522f475Smrg * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
50d522f475Smrg * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51d522f475Smrg * SOFTWARE.
52d522f475Smrg */
53d522f475Smrg
54d522f475Smrg/* tabs.c */
55d522f475Smrg
56d522f475Smrg#include <xterm.h>
57d522f475Smrg#include <data.h>
58d522f475Smrg
59d522f475Smrg#define TAB_INDEX(n) ((n) >> TAB_BITS_SHIFT)
60d522f475Smrg#define TAB_MASK(n)  (1 << ((n) & (TAB_BITS_WIDTH-1)))
61d522f475Smrg
6220d2c4d2Smrg#define SET_TAB(tabs,n) UIntSet(tabs[TAB_INDEX(n)], TAB_MASK(n))
6320d2c4d2Smrg#define CLR_TAB(tabs,n) UIntClr(tabs[TAB_INDEX(n)], TAB_MASK(n))
6420d2c4d2Smrg#define TST_TAB(tabs,n) (tabs[TAB_INDEX(n)] & (unsigned) TAB_MASK(n))
65d522f475Smrg
66d522f475Smrg/*
67d522f475Smrg * places tabstops at only every 8 columns
68d522f475Smrg */
69d522f475Smrgvoid
70d522f475SmrgTabReset(Tabs tabs)
71d522f475Smrg{
72d522f475Smrg    int i;
73d522f475Smrg
74d522f475Smrg    TabZonk(tabs);
75d522f475Smrg
76d522f475Smrg    for (i = 0; i < MAX_TABS; i += 8)
77d522f475Smrg	TabSet(tabs, i);
78d522f475Smrg}
79d522f475Smrg
80d522f475Smrg/*
81d522f475Smrg * places a tabstop at col
82d522f475Smrg */
83d522f475Smrgvoid
84d522f475SmrgTabSet(Tabs tabs, int col)
85d522f475Smrg{
86d522f475Smrg    if (col >= 0 && col < MAX_TABS) {
87d522f475Smrg	SET_TAB(tabs, col);
88d522f475Smrg    }
89d522f475Smrg}
90d522f475Smrg
91d522f475Smrg/*
92d522f475Smrg * clears a tabstop at col
93d522f475Smrg */
94d522f475Smrgvoid
95d522f475SmrgTabClear(Tabs tabs, int col)
96d522f475Smrg{
97d522f475Smrg    if (col >= 0 && col < MAX_TABS) {
98d522f475Smrg	CLR_TAB(tabs, col);
99d522f475Smrg    }
100d522f475Smrg}
101d522f475Smrg
102d522f475Smrg/*
103d522f475Smrg * returns the column of the next tabstop
104d522f475Smrg * (or MAX_TABS - 1 if there are no more).
105d522f475Smrg * A tabstop at col is ignored.
106d522f475Smrg */
107d522f475Smrgstatic int
108d522f475SmrgTabNext(XtermWidget xw, Tabs tabs, int col)
109d522f475Smrg{
11020d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
111d522f475Smrg
112d522f475Smrg    if (screen->curses && screen->do_wrap && (xw->flags & WRAPAROUND)) {
113d522f475Smrg	xtermIndex(xw, 1);
114d522f475Smrg	set_cur_col(screen, 0);
1152eaa94a1Schristos	col = 0;
1162eaa94a1Schristos	screen->do_wrap = False;
117d522f475Smrg    }
118d522f475Smrg    for (++col; col < MAX_TABS; ++col)
119d522f475Smrg	if (TST_TAB(tabs, col))
120d522f475Smrg	    return (col);
121d522f475Smrg
122d522f475Smrg    return (MAX_TABS - 1);
123d522f475Smrg}
124d522f475Smrg
125d522f475Smrg/*
126d522f475Smrg * returns the column of the previous tabstop
127d522f475Smrg * (or 0 if there are no more).
128d522f475Smrg * A tabstop at col is ignored.
129d522f475Smrg */
130d522f475Smrgstatic int
131d522f475SmrgTabPrev(Tabs tabs, int col)
132d522f475Smrg{
133d522f475Smrg    for (--col; col >= 0; --col)
1342eaa94a1Schristos	if ((col < MAX_TABS) && TST_TAB(tabs, col))
135d522f475Smrg	    return (col);
136d522f475Smrg
137d522f475Smrg    return (0);
138d522f475Smrg}
139d522f475Smrg
140d522f475Smrg/*
141d522f475Smrg * Tab to the next stop, returning true if the cursor moved
142d522f475Smrg */
143d522f475SmrgBool
144d522f475SmrgTabToNextStop(XtermWidget xw)
145d522f475Smrg{
14620d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
147d522f475Smrg    int saved_column = screen->cur_col;
148d522f475Smrg    int next = TabNext(xw, xw->tabs, screen->cur_col);
149956cc18dSsnj    int max = LineMaxCol(screen, getLineData(screen, screen->cur_row));
150d522f475Smrg
151d522f475Smrg    if (next > max)
152d522f475Smrg	next = max;
153d522f475Smrg    set_cur_col(screen, next);
154d522f475Smrg
155d522f475Smrg    return (screen->cur_col > saved_column);
156d522f475Smrg}
157d522f475Smrg
158d522f475Smrg/*
159d522f475Smrg * Tab to the previous stop, returning true if the cursor moved
160d522f475Smrg */
161d522f475SmrgBool
162d522f475SmrgTabToPrevStop(XtermWidget xw)
163d522f475Smrg{
16420d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
165d522f475Smrg    int saved_column = screen->cur_col;
166d522f475Smrg
167d522f475Smrg    set_cur_col(screen, TabPrev(xw->tabs, screen->cur_col));
168d522f475Smrg
169d522f475Smrg    return (screen->cur_col < saved_column);
170d522f475Smrg}
171d522f475Smrg
172d522f475Smrg/*
173d522f475Smrg * clears all tabs
174d522f475Smrg */
175d522f475Smrgvoid
176d522f475SmrgTabZonk(Tabs tabs)
177d522f475Smrg{
178d522f475Smrg    memset(tabs, 0, sizeof(*tabs) * TAB_ARRAY_SIZE);
179d522f475Smrg}
180