cursor.c revision 913cc679
1913cc679Smrg/* $XTermId: cursor.c,v 1.71 2017/05/06 00:58:27 tom Exp $ */
2d522f475Smrg
3d522f475Smrg/*
4913cc679Smrg * Copyright 2002-2016,2017 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/* cursor.c */
55d522f475Smrg
56d522f475Smrg#include <xterm.h>
57d522f475Smrg#include <data.h>
58d522f475Smrg#include <menu.h>
59d522f475Smrg
60d522f475Smrg#include <assert.h>
61d522f475Smrg
62d522f475Smrg/*
63d522f475Smrg * Moves the cursor to the specified position, checking for bounds.
64d522f475Smrg * (this includes scrolling regions)
65d522f475Smrg * The origin is considered to be 0, 0 for this procedure.
66d522f475Smrg */
67d522f475Smrgvoid
682e4f8982SmrgCursorSet(TScreen *screen, int row, int col, unsigned flags)
69d522f475Smrg{
70d522f475Smrg    int use_row = row;
710bd37d32Smrg    int use_col = col;
720bd37d32Smrg    int max_col = screen->max_col;
730bd37d32Smrg    int max_row = screen->max_row;
740bd37d32Smrg
750bd37d32Smrg    if (flags & ORIGIN) {
760bd37d32Smrg	use_col += screen->lft_marg;
770bd37d32Smrg	max_col = screen->rgt_marg;
780bd37d32Smrg    }
790bd37d32Smrg    use_col = (use_col < 0 ? 0 : use_col);
800bd37d32Smrg    set_cur_col(screen, (use_col <= max_col ? use_col : max_col));
81d522f475Smrg
82d522f475Smrg    if (flags & ORIGIN) {
83d522f475Smrg	use_row += screen->top_marg;
84d522f475Smrg	max_row = screen->bot_marg;
85d522f475Smrg    }
86d522f475Smrg    use_row = (use_row < 0 ? 0 : use_row);
87d522f475Smrg    set_cur_row(screen, (use_row <= max_row ? use_row : max_row));
88d522f475Smrg
890bd37d32Smrg    ResetWrap(screen);
900bd37d32Smrg
910bd37d32Smrg    TRACE(("CursorSet(%d,%d) margins V[%d..%d] H[%d..%d] -> %d,%d %s\n",
92d522f475Smrg	   row, col,
93d522f475Smrg	   screen->top_marg,
94d522f475Smrg	   screen->bot_marg,
950bd37d32Smrg	   screen->lft_marg,
960bd37d32Smrg	   screen->rgt_marg,
97d522f475Smrg	   screen->cur_row,
98d522f475Smrg	   screen->cur_col,
992e4f8982Smrg	   ((flags & ORIGIN) ? "origin" : "normal")));
100d522f475Smrg}
101d522f475Smrg
102d522f475Smrg/*
103d522f475Smrg * moves the cursor left n, no wrap around
104d522f475Smrg */
105d522f475Smrgvoid
106d522f475SmrgCursorBack(XtermWidget xw, int n)
107d522f475Smrg{
1080bd37d32Smrg#define WRAP_MASK (REVERSEWRAP | WRAPAROUND)
10920d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
1102e4f8982Smrg    int rev;
1110bd37d32Smrg    int left = ScrnLeftMargin(xw);
1120bd37d32Smrg    int before = screen->cur_col;
113d522f475Smrg
1142e4f8982Smrg    if ((rev = ((xw->flags & WRAP_MASK) == WRAP_MASK)) != 0
1150bd37d32Smrg	&& screen->do_wrap) {
116d522f475Smrg	n--;
1170bd37d32Smrg    }
1180bd37d32Smrg
1190bd37d32Smrg    /* if the cursor is already before the left-margin, we have to let it go */
1200bd37d32Smrg    if (before < left)
1210bd37d32Smrg	left = 0;
1220bd37d32Smrg
1230bd37d32Smrg    if ((screen->cur_col -= n) < left) {
124d522f475Smrg	if (rev) {
1252e4f8982Smrg	    int in_row = ScrnRightMargin(xw) - left + 1;
1262e4f8982Smrg	    int offset = (in_row * screen->cur_row) + screen->cur_col - left;
1270bd37d32Smrg	    if (offset < 0) {
1282e4f8982Smrg		int length = in_row * MaxRows(screen);
1290bd37d32Smrg		offset += ((-offset) / length + 1) * length;
130d522f475Smrg	    }
1310bd37d32Smrg	    set_cur_row(screen, (offset / in_row));
1320bd37d32Smrg	    set_cur_col(screen, (offset % in_row) + left);
133d522f475Smrg	    do_xevents();
134d522f475Smrg	} else {
1350bd37d32Smrg	    set_cur_col(screen, left);
136d522f475Smrg	}
137d522f475Smrg    }
1380bd37d32Smrg    ResetWrap(screen);
139d522f475Smrg}
140d522f475Smrg
141d522f475Smrg/*
142d522f475Smrg * moves the cursor forward n, no wraparound
143d522f475Smrg */
144d522f475Smrgvoid
1450bd37d32SmrgCursorForward(XtermWidget xw, int n)
146d522f475Smrg{
1470bd37d32Smrg    TScreen *screen = TScreenOf(xw);
148956cc18dSsnj#if OPT_DEC_CHRSET
149956cc18dSsnj    LineData *ld = getLineData(screen, screen->cur_row);
150956cc18dSsnj#endif
151d522f475Smrg    int next = screen->cur_col + n;
1520bd37d32Smrg    int max;
1530bd37d32Smrg
1540bd37d32Smrg    if (IsLeftRightMode(xw)) {
1550bd37d32Smrg	max = screen->rgt_marg;
1560bd37d32Smrg	if (screen->cur_col > max)
1570bd37d32Smrg	    max = screen->max_col;
1580bd37d32Smrg    } else {
1590bd37d32Smrg	max = LineMaxCol(screen, ld);
1600bd37d32Smrg    }
161d522f475Smrg
162d522f475Smrg    if (next > max)
163d522f475Smrg	next = max;
164d522f475Smrg
165d522f475Smrg    set_cur_col(screen, next);
1660bd37d32Smrg    ResetWrap(screen);
167d522f475Smrg}
168d522f475Smrg
169d522f475Smrg/*
170d522f475Smrg * moves the cursor down n, no scrolling.
171d522f475Smrg * Won't pass bottom margin or bottom of screen.
172d522f475Smrg */
173d522f475Smrgvoid
1742e4f8982SmrgCursorDown(TScreen *screen, int n)
175d522f475Smrg{
176d522f475Smrg    int max;
177d522f475Smrg    int next = screen->cur_row + n;
178d522f475Smrg
179d522f475Smrg    max = (screen->cur_row > screen->bot_marg ?
180d522f475Smrg	   screen->max_row : screen->bot_marg);
181d522f475Smrg    if (next > max)
182d522f475Smrg	next = max;
183d522f475Smrg    if (next > screen->max_row)
184d522f475Smrg	next = screen->max_row;
185d522f475Smrg
186d522f475Smrg    set_cur_row(screen, next);
1870bd37d32Smrg    ResetWrap(screen);
188d522f475Smrg}
189d522f475Smrg
190d522f475Smrg/*
191d522f475Smrg * moves the cursor up n, no linestarving.
192d522f475Smrg * Won't pass top margin or top of screen.
193d522f475Smrg */
194d522f475Smrgvoid
1952e4f8982SmrgCursorUp(TScreen *screen, int n)
196d522f475Smrg{
197d522f475Smrg    int min;
198d522f475Smrg    int next = screen->cur_row - n;
199d522f475Smrg
200d522f475Smrg    min = ((screen->cur_row < screen->top_marg)
201d522f475Smrg	   ? 0
202d522f475Smrg	   : screen->top_marg);
203d522f475Smrg    if (next < min)
204d522f475Smrg	next = min;
205d522f475Smrg    if (next < 0)
206d522f475Smrg	next = 0;
207d522f475Smrg
208d522f475Smrg    set_cur_row(screen, next);
2090bd37d32Smrg    ResetWrap(screen);
210d522f475Smrg}
211d522f475Smrg
212d522f475Smrg/*
213d522f475Smrg * Moves cursor down amount lines, scrolls if necessary.
214d522f475Smrg * Won't leave scrolling region. No carriage return.
215d522f475Smrg */
216d522f475Smrgvoid
217d522f475SmrgxtermIndex(XtermWidget xw, int amount)
218d522f475Smrg{
21920d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
220d522f475Smrg
221d522f475Smrg    /*
222d522f475Smrg     * indexing when below scrolling region is cursor down.
223d522f475Smrg     * if cursor high enough, no scrolling necessary.
224d522f475Smrg     */
225d522f475Smrg    if (screen->cur_row > screen->bot_marg
2260bd37d32Smrg	|| screen->cur_row + amount <= screen->bot_marg
2270bd37d32Smrg	|| (IsLeftRightMode(xw)
2280bd37d32Smrg	    && !ScrnIsColInMargins(screen, screen->cur_col))) {
229d522f475Smrg	CursorDown(screen, amount);
2300bd37d32Smrg    } else {
2312e4f8982Smrg	int j;
2320bd37d32Smrg	CursorDown(screen, j = screen->bot_marg - screen->cur_row);
2330bd37d32Smrg	xtermScroll(xw, amount - j);
234d522f475Smrg    }
235d522f475Smrg}
236d522f475Smrg
237d522f475Smrg/*
238d522f475Smrg * Moves cursor up amount lines, reverse scrolls if necessary.
239d522f475Smrg * Won't leave scrolling region. No carriage return.
240d522f475Smrg */
241d522f475Smrgvoid
242d522f475SmrgRevIndex(XtermWidget xw, int amount)
243d522f475Smrg{
24420d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
245d522f475Smrg
246d522f475Smrg    /*
247d522f475Smrg     * reverse indexing when above scrolling region is cursor up.
248d522f475Smrg     * if cursor low enough, no reverse indexing needed
249d522f475Smrg     */
250d522f475Smrg    if (screen->cur_row < screen->top_marg
2510bd37d32Smrg	|| screen->cur_row - amount >= screen->top_marg
2520bd37d32Smrg	|| (IsLeftRightMode(xw)
2530bd37d32Smrg	    && !ScrnIsColInMargins(screen, screen->cur_col))) {
254d522f475Smrg	CursorUp(screen, amount);
2550bd37d32Smrg    } else {
2560bd37d32Smrg	RevScroll(xw, amount - (screen->cur_row - screen->top_marg));
2570bd37d32Smrg	CursorUp(screen, screen->cur_row - screen->top_marg);
258d522f475Smrg    }
259d522f475Smrg}
260d522f475Smrg
261d522f475Smrg/*
262d522f475Smrg * Moves Cursor To First Column In Line
263d522f475Smrg * (Note: xterm doesn't implement SLH, SLL which would affect use of this)
264d522f475Smrg */
265d522f475Smrgvoid
2660bd37d32SmrgCarriageReturn(XtermWidget xw)
267d522f475Smrg{
2680bd37d32Smrg    TScreen *screen = TScreenOf(xw);
2690bd37d32Smrg    int left = ScrnLeftMargin(xw);
2700bd37d32Smrg    int col;
2710bd37d32Smrg
2720bd37d32Smrg    if (xw->flags & ORIGIN) {
2730bd37d32Smrg	col = left;
274e0a2b6dfSmrg    } else if (screen->cur_col >= left) {
2750bd37d32Smrg	col = left;
2760bd37d32Smrg    } else {
2770bd37d32Smrg	/*
2780bd37d32Smrg	 * If origin-mode is not active, it is possible to use cursor
2790bd37d32Smrg	 * addressing outside the margins.  In that case we will go to the
2800bd37d32Smrg	 * first column rather than following the margin.
2810bd37d32Smrg	 */
2820bd37d32Smrg	col = 0;
2830bd37d32Smrg    }
2840bd37d32Smrg
2850bd37d32Smrg    set_cur_col(screen, col);
2860bd37d32Smrg    ResetWrap(screen);
287d522f475Smrg    do_xevents();
288d522f475Smrg}
289d522f475Smrg
290d522f475Smrg/*
291d522f475Smrg * When resizing the window, if we're showing the alternate screen, we still
292d522f475Smrg * have to adjust the saved cursor from the normal screen to account for
293d522f475Smrg * shifting of the saved-line region in/out of the viewable window.
294d522f475Smrg */
295d522f475Smrgvoid
296d522f475SmrgAdjustSavedCursor(XtermWidget xw, int adjust)
297d522f475Smrg{
29820d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
299d522f475Smrg
300956cc18dSsnj    if (screen->whichBuf) {
301956cc18dSsnj	SavedCursor *sc = &screen->sc[0];
302d522f475Smrg
303d522f475Smrg	if (adjust > 0) {
304d522f475Smrg	    TRACE(("AdjustSavedCursor %d -> %d\n", sc->row, sc->row - adjust));
305d522f475Smrg	    sc->row += adjust;
306d522f475Smrg	}
307d522f475Smrg    }
308d522f475Smrg}
309d522f475Smrg
310d522f475Smrg/*
311d522f475Smrg * Save Cursor and Attributes
312d522f475Smrg */
313d522f475Smrgvoid
314d522f475SmrgCursorSave(XtermWidget xw)
315d522f475Smrg{
31620d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
317956cc18dSsnj    SavedCursor *sc = &screen->sc[screen->whichBuf];
318d522f475Smrg
319d522f475Smrg    sc->saved = True;
320d522f475Smrg    sc->row = screen->cur_row;
321d522f475Smrg    sc->col = screen->cur_col;
322d522f475Smrg    sc->flags = xw->flags;
323d522f475Smrg    sc->curgl = screen->curgl;
324d522f475Smrg    sc->curgr = screen->curgr;
325913cc679Smrg    sc->wrap_flag = screen->do_wrap;
326d522f475Smrg#if OPT_ISO_COLORS
327d522f475Smrg    sc->cur_foreground = xw->cur_foreground;
328d522f475Smrg    sc->cur_background = xw->cur_background;
329d522f475Smrg    sc->sgr_foreground = xw->sgr_foreground;
330d522f475Smrg#endif
331d522f475Smrg    memmove(sc->gsets, screen->gsets, sizeof(screen->gsets));
332d522f475Smrg}
333d522f475Smrg
334d522f475Smrg/*
335d522f475Smrg * We save/restore all visible attributes, plus wrapping, origin mode, and the
336d522f475Smrg * selective erase attribute.
337d522f475Smrg */
338913cc679Smrg#define DECSC_FLAGS (ATTRIBUTES|ORIGIN|PROTECTED)
339d522f475Smrg
340d522f475Smrg/*
341d522f475Smrg * Restore Cursor and Attributes
342d522f475Smrg */
343d522f475Smrgvoid
344d522f475SmrgCursorRestore(XtermWidget xw)
345d522f475Smrg{
34620d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
347956cc18dSsnj    SavedCursor *sc = &screen->sc[screen->whichBuf];
348d522f475Smrg
349d522f475Smrg    /* Restore the character sets, unless we never did a save-cursor op.
350d522f475Smrg     * In that case, we'll reset the character sets.
351d522f475Smrg     */
352d522f475Smrg    if (sc->saved) {
353d522f475Smrg	memmove(screen->gsets, sc->gsets, sizeof(screen->gsets));
354d522f475Smrg	screen->curgl = sc->curgl;
355d522f475Smrg	screen->curgr = sc->curgr;
356d522f475Smrg    } else {
357d522f475Smrg	resetCharsets(screen);
358d522f475Smrg    }
359d522f475Smrg
36020d2c4d2Smrg    UIntClr(xw->flags, DECSC_FLAGS);
36120d2c4d2Smrg    UIntSet(xw->flags, sc->flags & DECSC_FLAGS);
362d522f475Smrg    CursorSet(screen,
363d522f475Smrg	      ((xw->flags & ORIGIN)
364d522f475Smrg	       ? sc->row - screen->top_marg
365d522f475Smrg	       : sc->row),
366d522f475Smrg	      sc->col, xw->flags);
367913cc679Smrg    screen->do_wrap = sc->wrap_flag;	/* after CursorSet/ResetWrap */
368d522f475Smrg
369d522f475Smrg#if OPT_ISO_COLORS
370d522f475Smrg    xw->sgr_foreground = sc->sgr_foreground;
3712e4f8982Smrg    SGR_Foreground(xw, (xw->flags & FG_COLOR) ? sc->cur_foreground : -1);
3722e4f8982Smrg    SGR_Background(xw, (xw->flags & BG_COLOR) ? sc->cur_background : -1);
373d522f475Smrg#endif
374d522f475Smrg}
375d522f475Smrg
376d522f475Smrg/*
377d522f475Smrg * Move the cursor to the first column of the n-th next line.
378d522f475Smrg */
379d522f475Smrgvoid
3800bd37d32SmrgCursorNextLine(XtermWidget xw, int count)
381d522f475Smrg{
3820bd37d32Smrg    TScreen *screen = TScreenOf(xw);
3830bd37d32Smrg
384d522f475Smrg    CursorDown(screen, count < 1 ? 1 : count);
3850bd37d32Smrg    CarriageReturn(xw);
386d522f475Smrg    do_xevents();
387d522f475Smrg}
388d522f475Smrg
389d522f475Smrg/*
390d522f475Smrg * Move the cursor to the first column of the n-th previous line.
391d522f475Smrg */
392d522f475Smrgvoid
3930bd37d32SmrgCursorPrevLine(XtermWidget xw, int count)
394d522f475Smrg{
3950bd37d32Smrg    TScreen *screen = TScreenOf(xw);
3960bd37d32Smrg
397d522f475Smrg    CursorUp(screen, count < 1 ? 1 : count);
3980bd37d32Smrg    CarriageReturn(xw);
399d522f475Smrg    do_xevents();
400d522f475Smrg}
401d522f475Smrg
4020bd37d32Smrg/*
4030bd37d32Smrg * Return col/row values which can be passed to CursorSet() preserving the
4040bd37d32Smrg * current col/row, e.g., accounting for DECOM.
4050bd37d32Smrg */
4060bd37d32Smrgint
4070bd37d32SmrgCursorCol(XtermWidget xw)
4080bd37d32Smrg{
4090bd37d32Smrg    TScreen *screen = TScreenOf(xw);
4100bd37d32Smrg    int result = screen->cur_col;
4110bd37d32Smrg    if (xw->flags & ORIGIN) {
4120bd37d32Smrg	result -= ScrnLeftMargin(xw);
4130bd37d32Smrg	if (result < 0)
4140bd37d32Smrg	    result = 0;
4150bd37d32Smrg    }
4160bd37d32Smrg    return result;
4170bd37d32Smrg}
4180bd37d32Smrg
4190bd37d32Smrgint
4200bd37d32SmrgCursorRow(XtermWidget xw)
4210bd37d32Smrg{
4220bd37d32Smrg    TScreen *screen = TScreenOf(xw);
4230bd37d32Smrg    int result = screen->cur_row;
4240bd37d32Smrg    if (xw->flags & ORIGIN) {
4250bd37d32Smrg	result -= screen->top_marg;
4260bd37d32Smrg	if (result < 0)
4270bd37d32Smrg	    result = 0;
4280bd37d32Smrg    }
4290bd37d32Smrg    return result;
4300bd37d32Smrg}
4310bd37d32Smrg
432d522f475Smrg#if OPT_TRACE
433d522f475Smrgint
4342e4f8982Smrgset_cur_row(TScreen *screen, int value)
435d522f475Smrg{
436956cc18dSsnj    TRACE(("set_cur_row %d vs %d\n", value, screen ? screen->max_row : -1));
437956cc18dSsnj
438d522f475Smrg    assert(screen != 0);
439d522f475Smrg    assert(value >= 0);
440d522f475Smrg    assert(value <= screen->max_row);
441d522f475Smrg    screen->cur_row = value;
442d522f475Smrg    return value;
443d522f475Smrg}
444d522f475Smrg
445d522f475Smrgint
4462e4f8982Smrgset_cur_col(TScreen *screen, int value)
447d522f475Smrg{
448956cc18dSsnj    TRACE(("set_cur_col %d vs %d\n", value, screen ? screen->max_col : -1));
449956cc18dSsnj
450d522f475Smrg    assert(screen != 0);
451d522f475Smrg    assert(value >= 0);
452d522f475Smrg    assert(value <= screen->max_col);
453d522f475Smrg    screen->cur_col = value;
454d522f475Smrg    return value;
455d522f475Smrg}
456d522f475Smrg#endif /* OPT_TRACE */
457