cursor.c revision e0a2b6df
1e0a2b6dfSmrg/* $XTermId: cursor.c,v 1.69 2013/08/08 08:16:40 tom Exp $ */
2d522f475Smrg
3d522f475Smrg/*
4e0a2b6dfSmrg * Copyright 2002-2012,2013 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
68d522f475SmrgCursorSet(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,
99d522f475Smrg	   (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);
1100bd37d32Smrg    int offset, in_row, length, rev;
1110bd37d32Smrg    int left = ScrnLeftMargin(xw);
1120bd37d32Smrg    int before = screen->cur_col;
113d522f475Smrg
1140bd37d32Smrg    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) {
1250bd37d32Smrg	    in_row = ScrnRightMargin(xw) - left + 1;
1260bd37d32Smrg	    offset = (in_row * screen->cur_row) + screen->cur_col - left;
1270bd37d32Smrg	    if (offset < 0) {
1280bd37d32Smrg		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
174d522f475SmrgCursorDown(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
195d522f475SmrgCursorUp(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    int j;
221d522f475Smrg
222d522f475Smrg    /*
223d522f475Smrg     * indexing when below scrolling region is cursor down.
224d522f475Smrg     * if cursor high enough, no scrolling necessary.
225d522f475Smrg     */
226d522f475Smrg    if (screen->cur_row > screen->bot_marg
2270bd37d32Smrg	|| screen->cur_row + amount <= screen->bot_marg
2280bd37d32Smrg	|| (IsLeftRightMode(xw)
2290bd37d32Smrg	    && !ScrnIsColInMargins(screen, screen->cur_col))) {
230d522f475Smrg	CursorDown(screen, amount);
2310bd37d32Smrg    } else {
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;
325d522f475Smrg#if OPT_ISO_COLORS
326d522f475Smrg    sc->cur_foreground = xw->cur_foreground;
327d522f475Smrg    sc->cur_background = xw->cur_background;
328d522f475Smrg    sc->sgr_foreground = xw->sgr_foreground;
329d522f475Smrg#endif
330d522f475Smrg    memmove(sc->gsets, screen->gsets, sizeof(screen->gsets));
331d522f475Smrg}
332d522f475Smrg
333d522f475Smrg/*
334d522f475Smrg * We save/restore all visible attributes, plus wrapping, origin mode, and the
335d522f475Smrg * selective erase attribute.
336d522f475Smrg */
337d522f475Smrg#define DECSC_FLAGS (ATTRIBUTES|ORIGIN|WRAPAROUND|PROTECTED)
338d522f475Smrg
339d522f475Smrg/*
340d522f475Smrg * Restore Cursor and Attributes
341d522f475Smrg */
342d522f475Smrgvoid
343d522f475SmrgCursorRestore(XtermWidget xw)
344d522f475Smrg{
34520d2c4d2Smrg    TScreen *screen = TScreenOf(xw);
346956cc18dSsnj    SavedCursor *sc = &screen->sc[screen->whichBuf];
347d522f475Smrg
348d522f475Smrg    /* Restore the character sets, unless we never did a save-cursor op.
349d522f475Smrg     * In that case, we'll reset the character sets.
350d522f475Smrg     */
351d522f475Smrg    if (sc->saved) {
352d522f475Smrg	memmove(screen->gsets, sc->gsets, sizeof(screen->gsets));
353d522f475Smrg	screen->curgl = sc->curgl;
354d522f475Smrg	screen->curgr = sc->curgr;
355d522f475Smrg    } else {
356d522f475Smrg	resetCharsets(screen);
357d522f475Smrg    }
358d522f475Smrg
35920d2c4d2Smrg    UIntClr(xw->flags, DECSC_FLAGS);
36020d2c4d2Smrg    UIntSet(xw->flags, sc->flags & DECSC_FLAGS);
361d522f475Smrg    CursorSet(screen,
362d522f475Smrg	      ((xw->flags & ORIGIN)
363d522f475Smrg	       ? sc->row - screen->top_marg
364d522f475Smrg	       : sc->row),
365d522f475Smrg	      sc->col, xw->flags);
366d522f475Smrg
367d522f475Smrg#if OPT_ISO_COLORS
368d522f475Smrg    xw->sgr_foreground = sc->sgr_foreground;
369d522f475Smrg    SGR_Foreground(xw, xw->flags & FG_COLOR ? sc->cur_foreground : -1);
370d522f475Smrg    SGR_Background(xw, xw->flags & BG_COLOR ? sc->cur_background : -1);
371d522f475Smrg#endif
372d522f475Smrg    update_autowrap();
373d522f475Smrg}
374d522f475Smrg
375d522f475Smrg/*
376d522f475Smrg * Move the cursor to the first column of the n-th next line.
377d522f475Smrg */
378d522f475Smrgvoid
3790bd37d32SmrgCursorNextLine(XtermWidget xw, int count)
380d522f475Smrg{
3810bd37d32Smrg    TScreen *screen = TScreenOf(xw);
3820bd37d32Smrg
383d522f475Smrg    CursorDown(screen, count < 1 ? 1 : count);
3840bd37d32Smrg    CarriageReturn(xw);
385d522f475Smrg    do_xevents();
386d522f475Smrg}
387d522f475Smrg
388d522f475Smrg/*
389d522f475Smrg * Move the cursor to the first column of the n-th previous line.
390d522f475Smrg */
391d522f475Smrgvoid
3920bd37d32SmrgCursorPrevLine(XtermWidget xw, int count)
393d522f475Smrg{
3940bd37d32Smrg    TScreen *screen = TScreenOf(xw);
3950bd37d32Smrg
396d522f475Smrg    CursorUp(screen, count < 1 ? 1 : count);
3970bd37d32Smrg    CarriageReturn(xw);
398d522f475Smrg    do_xevents();
399d522f475Smrg}
400d522f475Smrg
4010bd37d32Smrg/*
4020bd37d32Smrg * Return col/row values which can be passed to CursorSet() preserving the
4030bd37d32Smrg * current col/row, e.g., accounting for DECOM.
4040bd37d32Smrg */
4050bd37d32Smrgint
4060bd37d32SmrgCursorCol(XtermWidget xw)
4070bd37d32Smrg{
4080bd37d32Smrg    TScreen *screen = TScreenOf(xw);
4090bd37d32Smrg    int result = screen->cur_col;
4100bd37d32Smrg    if (xw->flags & ORIGIN) {
4110bd37d32Smrg	result -= ScrnLeftMargin(xw);
4120bd37d32Smrg	if (result < 0)
4130bd37d32Smrg	    result = 0;
4140bd37d32Smrg    }
4150bd37d32Smrg    return result;
4160bd37d32Smrg}
4170bd37d32Smrg
4180bd37d32Smrgint
4190bd37d32SmrgCursorRow(XtermWidget xw)
4200bd37d32Smrg{
4210bd37d32Smrg    TScreen *screen = TScreenOf(xw);
4220bd37d32Smrg    int result = screen->cur_row;
4230bd37d32Smrg    if (xw->flags & ORIGIN) {
4240bd37d32Smrg	result -= screen->top_marg;
4250bd37d32Smrg	if (result < 0)
4260bd37d32Smrg	    result = 0;
4270bd37d32Smrg    }
4280bd37d32Smrg    return result;
4290bd37d32Smrg}
4300bd37d32Smrg
431d522f475Smrg#if OPT_TRACE
432d522f475Smrgint
433d522f475Smrgset_cur_row(TScreen * screen, int value)
434d522f475Smrg{
435956cc18dSsnj    TRACE(("set_cur_row %d vs %d\n", value, screen ? screen->max_row : -1));
436956cc18dSsnj
437d522f475Smrg    assert(screen != 0);
438d522f475Smrg    assert(value >= 0);
439d522f475Smrg    assert(value <= screen->max_row);
440d522f475Smrg    screen->cur_row = value;
441d522f475Smrg    return value;
442d522f475Smrg}
443d522f475Smrg
444d522f475Smrgint
445d522f475Smrgset_cur_col(TScreen * screen, int value)
446d522f475Smrg{
447956cc18dSsnj    TRACE(("set_cur_col %d vs %d\n", value, screen ? screen->max_col : -1));
448956cc18dSsnj
449d522f475Smrg    assert(screen != 0);
450d522f475Smrg    assert(value >= 0);
451d522f475Smrg    assert(value <= screen->max_col);
452d522f475Smrg    screen->cur_col = value;
453d522f475Smrg    return value;
454d522f475Smrg}
455d522f475Smrg#endif /* OPT_TRACE */
456