cursor.c revision 913cc679
1/* $XTermId: cursor.c,v 1.71 2017/05/06 00:58:27 tom Exp $ */
2
3/*
4 * Copyright 2002-2016,2017 by Thomas E. Dickey
5 *
6 *                         All Rights Reserved
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Except as contained in this notice, the name(s) of the above copyright
28 * holders shall not be used in advertising or otherwise to promote the
29 * sale, use or other dealings in this Software without prior written
30 * authorization.
31 *
32 * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
33 *
34 *                         All Rights Reserved
35 *
36 * Permission to use, copy, modify, and distribute this software and its
37 * documentation for any purpose and without fee is hereby granted,
38 * provided that the above copyright notice appear in all copies and that
39 * both that copyright notice and this permission notice appear in
40 * supporting documentation, and that the name of Digital Equipment
41 * Corporation not be used in advertising or publicity pertaining to
42 * distribution of the software without specific, written prior permission.
43 *
44 *
45 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
46 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
47 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
48 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
49 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
50 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
52 */
53
54/* cursor.c */
55
56#include <xterm.h>
57#include <data.h>
58#include <menu.h>
59
60#include <assert.h>
61
62/*
63 * Moves the cursor to the specified position, checking for bounds.
64 * (this includes scrolling regions)
65 * The origin is considered to be 0, 0 for this procedure.
66 */
67void
68CursorSet(TScreen *screen, int row, int col, unsigned flags)
69{
70    int use_row = row;
71    int use_col = col;
72    int max_col = screen->max_col;
73    int max_row = screen->max_row;
74
75    if (flags & ORIGIN) {
76	use_col += screen->lft_marg;
77	max_col = screen->rgt_marg;
78    }
79    use_col = (use_col < 0 ? 0 : use_col);
80    set_cur_col(screen, (use_col <= max_col ? use_col : max_col));
81
82    if (flags & ORIGIN) {
83	use_row += screen->top_marg;
84	max_row = screen->bot_marg;
85    }
86    use_row = (use_row < 0 ? 0 : use_row);
87    set_cur_row(screen, (use_row <= max_row ? use_row : max_row));
88
89    ResetWrap(screen);
90
91    TRACE(("CursorSet(%d,%d) margins V[%d..%d] H[%d..%d] -> %d,%d %s\n",
92	   row, col,
93	   screen->top_marg,
94	   screen->bot_marg,
95	   screen->lft_marg,
96	   screen->rgt_marg,
97	   screen->cur_row,
98	   screen->cur_col,
99	   ((flags & ORIGIN) ? "origin" : "normal")));
100}
101
102/*
103 * moves the cursor left n, no wrap around
104 */
105void
106CursorBack(XtermWidget xw, int n)
107{
108#define WRAP_MASK (REVERSEWRAP | WRAPAROUND)
109    TScreen *screen = TScreenOf(xw);
110    int rev;
111    int left = ScrnLeftMargin(xw);
112    int before = screen->cur_col;
113
114    if ((rev = ((xw->flags & WRAP_MASK) == WRAP_MASK)) != 0
115	&& screen->do_wrap) {
116	n--;
117    }
118
119    /* if the cursor is already before the left-margin, we have to let it go */
120    if (before < left)
121	left = 0;
122
123    if ((screen->cur_col -= n) < left) {
124	if (rev) {
125	    int in_row = ScrnRightMargin(xw) - left + 1;
126	    int offset = (in_row * screen->cur_row) + screen->cur_col - left;
127	    if (offset < 0) {
128		int length = in_row * MaxRows(screen);
129		offset += ((-offset) / length + 1) * length;
130	    }
131	    set_cur_row(screen, (offset / in_row));
132	    set_cur_col(screen, (offset % in_row) + left);
133	    do_xevents();
134	} else {
135	    set_cur_col(screen, left);
136	}
137    }
138    ResetWrap(screen);
139}
140
141/*
142 * moves the cursor forward n, no wraparound
143 */
144void
145CursorForward(XtermWidget xw, int n)
146{
147    TScreen *screen = TScreenOf(xw);
148#if OPT_DEC_CHRSET
149    LineData *ld = getLineData(screen, screen->cur_row);
150#endif
151    int next = screen->cur_col + n;
152    int max;
153
154    if (IsLeftRightMode(xw)) {
155	max = screen->rgt_marg;
156	if (screen->cur_col > max)
157	    max = screen->max_col;
158    } else {
159	max = LineMaxCol(screen, ld);
160    }
161
162    if (next > max)
163	next = max;
164
165    set_cur_col(screen, next);
166    ResetWrap(screen);
167}
168
169/*
170 * moves the cursor down n, no scrolling.
171 * Won't pass bottom margin or bottom of screen.
172 */
173void
174CursorDown(TScreen *screen, int n)
175{
176    int max;
177    int next = screen->cur_row + n;
178
179    max = (screen->cur_row > screen->bot_marg ?
180	   screen->max_row : screen->bot_marg);
181    if (next > max)
182	next = max;
183    if (next > screen->max_row)
184	next = screen->max_row;
185
186    set_cur_row(screen, next);
187    ResetWrap(screen);
188}
189
190/*
191 * moves the cursor up n, no linestarving.
192 * Won't pass top margin or top of screen.
193 */
194void
195CursorUp(TScreen *screen, int n)
196{
197    int min;
198    int next = screen->cur_row - n;
199
200    min = ((screen->cur_row < screen->top_marg)
201	   ? 0
202	   : screen->top_marg);
203    if (next < min)
204	next = min;
205    if (next < 0)
206	next = 0;
207
208    set_cur_row(screen, next);
209    ResetWrap(screen);
210}
211
212/*
213 * Moves cursor down amount lines, scrolls if necessary.
214 * Won't leave scrolling region. No carriage return.
215 */
216void
217xtermIndex(XtermWidget xw, int amount)
218{
219    TScreen *screen = TScreenOf(xw);
220
221    /*
222     * indexing when below scrolling region is cursor down.
223     * if cursor high enough, no scrolling necessary.
224     */
225    if (screen->cur_row > screen->bot_marg
226	|| screen->cur_row + amount <= screen->bot_marg
227	|| (IsLeftRightMode(xw)
228	    && !ScrnIsColInMargins(screen, screen->cur_col))) {
229	CursorDown(screen, amount);
230    } else {
231	int j;
232	CursorDown(screen, j = screen->bot_marg - screen->cur_row);
233	xtermScroll(xw, amount - j);
234    }
235}
236
237/*
238 * Moves cursor up amount lines, reverse scrolls if necessary.
239 * Won't leave scrolling region. No carriage return.
240 */
241void
242RevIndex(XtermWidget xw, int amount)
243{
244    TScreen *screen = TScreenOf(xw);
245
246    /*
247     * reverse indexing when above scrolling region is cursor up.
248     * if cursor low enough, no reverse indexing needed
249     */
250    if (screen->cur_row < screen->top_marg
251	|| screen->cur_row - amount >= screen->top_marg
252	|| (IsLeftRightMode(xw)
253	    && !ScrnIsColInMargins(screen, screen->cur_col))) {
254	CursorUp(screen, amount);
255    } else {
256	RevScroll(xw, amount - (screen->cur_row - screen->top_marg));
257	CursorUp(screen, screen->cur_row - screen->top_marg);
258    }
259}
260
261/*
262 * Moves Cursor To First Column In Line
263 * (Note: xterm doesn't implement SLH, SLL which would affect use of this)
264 */
265void
266CarriageReturn(XtermWidget xw)
267{
268    TScreen *screen = TScreenOf(xw);
269    int left = ScrnLeftMargin(xw);
270    int col;
271
272    if (xw->flags & ORIGIN) {
273	col = left;
274    } else if (screen->cur_col >= left) {
275	col = left;
276    } else {
277	/*
278	 * If origin-mode is not active, it is possible to use cursor
279	 * addressing outside the margins.  In that case we will go to the
280	 * first column rather than following the margin.
281	 */
282	col = 0;
283    }
284
285    set_cur_col(screen, col);
286    ResetWrap(screen);
287    do_xevents();
288}
289
290/*
291 * When resizing the window, if we're showing the alternate screen, we still
292 * have to adjust the saved cursor from the normal screen to account for
293 * shifting of the saved-line region in/out of the viewable window.
294 */
295void
296AdjustSavedCursor(XtermWidget xw, int adjust)
297{
298    TScreen *screen = TScreenOf(xw);
299
300    if (screen->whichBuf) {
301	SavedCursor *sc = &screen->sc[0];
302
303	if (adjust > 0) {
304	    TRACE(("AdjustSavedCursor %d -> %d\n", sc->row, sc->row - adjust));
305	    sc->row += adjust;
306	}
307    }
308}
309
310/*
311 * Save Cursor and Attributes
312 */
313void
314CursorSave(XtermWidget xw)
315{
316    TScreen *screen = TScreenOf(xw);
317    SavedCursor *sc = &screen->sc[screen->whichBuf];
318
319    sc->saved = True;
320    sc->row = screen->cur_row;
321    sc->col = screen->cur_col;
322    sc->flags = xw->flags;
323    sc->curgl = screen->curgl;
324    sc->curgr = screen->curgr;
325    sc->wrap_flag = screen->do_wrap;
326#if OPT_ISO_COLORS
327    sc->cur_foreground = xw->cur_foreground;
328    sc->cur_background = xw->cur_background;
329    sc->sgr_foreground = xw->sgr_foreground;
330#endif
331    memmove(sc->gsets, screen->gsets, sizeof(screen->gsets));
332}
333
334/*
335 * We save/restore all visible attributes, plus wrapping, origin mode, and the
336 * selective erase attribute.
337 */
338#define DECSC_FLAGS (ATTRIBUTES|ORIGIN|PROTECTED)
339
340/*
341 * Restore Cursor and Attributes
342 */
343void
344CursorRestore(XtermWidget xw)
345{
346    TScreen *screen = TScreenOf(xw);
347    SavedCursor *sc = &screen->sc[screen->whichBuf];
348
349    /* Restore the character sets, unless we never did a save-cursor op.
350     * In that case, we'll reset the character sets.
351     */
352    if (sc->saved) {
353	memmove(screen->gsets, sc->gsets, sizeof(screen->gsets));
354	screen->curgl = sc->curgl;
355	screen->curgr = sc->curgr;
356    } else {
357	resetCharsets(screen);
358    }
359
360    UIntClr(xw->flags, DECSC_FLAGS);
361    UIntSet(xw->flags, sc->flags & DECSC_FLAGS);
362    CursorSet(screen,
363	      ((xw->flags & ORIGIN)
364	       ? sc->row - screen->top_marg
365	       : sc->row),
366	      sc->col, xw->flags);
367    screen->do_wrap = sc->wrap_flag;	/* after CursorSet/ResetWrap */
368
369#if OPT_ISO_COLORS
370    xw->sgr_foreground = sc->sgr_foreground;
371    SGR_Foreground(xw, (xw->flags & FG_COLOR) ? sc->cur_foreground : -1);
372    SGR_Background(xw, (xw->flags & BG_COLOR) ? sc->cur_background : -1);
373#endif
374}
375
376/*
377 * Move the cursor to the first column of the n-th next line.
378 */
379void
380CursorNextLine(XtermWidget xw, int count)
381{
382    TScreen *screen = TScreenOf(xw);
383
384    CursorDown(screen, count < 1 ? 1 : count);
385    CarriageReturn(xw);
386    do_xevents();
387}
388
389/*
390 * Move the cursor to the first column of the n-th previous line.
391 */
392void
393CursorPrevLine(XtermWidget xw, int count)
394{
395    TScreen *screen = TScreenOf(xw);
396
397    CursorUp(screen, count < 1 ? 1 : count);
398    CarriageReturn(xw);
399    do_xevents();
400}
401
402/*
403 * Return col/row values which can be passed to CursorSet() preserving the
404 * current col/row, e.g., accounting for DECOM.
405 */
406int
407CursorCol(XtermWidget xw)
408{
409    TScreen *screen = TScreenOf(xw);
410    int result = screen->cur_col;
411    if (xw->flags & ORIGIN) {
412	result -= ScrnLeftMargin(xw);
413	if (result < 0)
414	    result = 0;
415    }
416    return result;
417}
418
419int
420CursorRow(XtermWidget xw)
421{
422    TScreen *screen = TScreenOf(xw);
423    int result = screen->cur_row;
424    if (xw->flags & ORIGIN) {
425	result -= screen->top_marg;
426	if (result < 0)
427	    result = 0;
428    }
429    return result;
430}
431
432#if OPT_TRACE
433int
434set_cur_row(TScreen *screen, int value)
435{
436    TRACE(("set_cur_row %d vs %d\n", value, screen ? screen->max_row : -1));
437
438    assert(screen != 0);
439    assert(value >= 0);
440    assert(value <= screen->max_row);
441    screen->cur_row = value;
442    return value;
443}
444
445int
446set_cur_col(TScreen *screen, int value)
447{
448    TRACE(("set_cur_col %d vs %d\n", value, screen ? screen->max_col : -1));
449
450    assert(screen != 0);
451    assert(value >= 0);
452    assert(value <= screen->max_col);
453    screen->cur_col = value;
454    return value;
455}
456#endif /* OPT_TRACE */
457