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