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