1 /* $NetBSD: v_scroll.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 static const char sccsid
[] = "Id: v_scroll.c,v 10.12 2001/06/25 15:19:34 skimo Exp (Berkeley) Date: 2001/06/25 15:19:34 ";
19 __RCSID("$NetBSD: v_scroll.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
31 #include "../common/common.h"
34 static void goto_adjust
__P((VICMD
*));
37 * The historic vi had a problem in that all movements were by physical
38 * lines, not by logical, or screen lines. Arguments can be made that this
39 * is the right thing to do. For example, single line movements, such as
40 * 'j' or 'k', should probably work on physical lines. Commands like "dj",
41 * or "j.", where '.' is a change command, make more sense for physical lines
42 * than they do for logical lines.
44 * These arguments, however, don't apply to scrolling commands like ^D and
45 * ^F -- if the window is fairly small, using physical lines can result in
46 * a half-page scroll repainting the entire screen, which is not what the
47 * user wanted. Second, if the line is larger than the screen, using physical
48 * lines can make it impossible to display parts of the line -- there aren't
49 * any commands that don't display the beginning of the line in historic vi,
50 * and if both the beginning and end of the line can't be on the screen at
51 * the same time, you lose. This is even worse in the case of the H, L, and
52 * M commands -- for large lines, they may all refer to the same line and
53 * will result in no movement at all.
55 * Another issue is that page and half-page scrolling commands historically
56 * moved to the first non-blank character in the new line. If the line is
57 * approximately the same size as the screen, this loses because the cursor
58 * before and after a ^D, may refer to the same location on the screen. In
59 * this implementation, scrolling commands set the cursor to the first non-
60 * blank character if the line changes because of the scroll. Otherwise,
61 * the cursor is left alone.
63 * This implementation does the scrolling (^B, ^D, ^F, ^U, ^Y, ^E), and the
64 * cursor positioning commands (H, L, M) commands using logical lines, not
70 * Go to first non-blank character of the line count, the last line
71 * of the file by default.
73 * PUBLIC: int v_lgoto __P((SCR *, VICMD *));
76 v_lgoto(SCR
*sp
, VICMD
*vp
)
80 if (F_ISSET(vp
, VC_C1SET
)) {
81 if (!db_exist(sp
, vp
->count
)) {
84 * Historically, 1G was legal in an empty file.
87 if (db_last(sp
, &nlines
))
92 v_eof(sp
, &vp
->m_start
);
95 vp
->m_stop
.lno
= vp
->count
;
97 if (db_last(sp
, &nlines
))
99 vp
->m_stop
.lno
= nlines
? nlines
: 1;
107 * Move to the first non-blank character of the logical line
108 * count - 1 from the top of the screen, 0 by default.
110 * PUBLIC: int v_home __P((SCR *, VICMD *));
113 v_home(SCR
*sp
, VICMD
*vp
)
115 if (vs_sm_position(sp
, &vp
->m_stop
,
116 F_ISSET(vp
, VC_C1SET
) ? vp
->count
- 1 : 0, P_TOP
))
124 * Move to the first non-blank character of the logical line
125 * in the middle of the screen.
127 * PUBLIC: int v_middle __P((SCR *, VICMD *));
130 v_middle(SCR
*sp
, VICMD
*vp
)
133 * Yielding to none in our quest for compatibility with every
134 * historical blemish of vi, no matter how strange it might be,
135 * we permit the user to enter a count and then ignore it.
137 if (vs_sm_position(sp
, &vp
->m_stop
, 0, P_MIDDLE
))
144 * v_bottom -- [count]L
145 * Move to the first non-blank character of the logical line
146 * count - 1 from the bottom of the screen, 0 by default.
148 * PUBLIC: int v_bottom __P((SCR *, VICMD *));
151 v_bottom(SCR
*sp
, VICMD
*vp
)
153 if (vs_sm_position(sp
, &vp
->m_stop
,
154 F_ISSET(vp
, VC_C1SET
) ? vp
->count
- 1 : 0, P_BOTTOM
))
161 goto_adjust(VICMD
*vp
)
163 /* Guess that it's the end of the range. */
164 vp
->m_final
= vp
->m_stop
;
167 * Non-motion commands move the cursor to the end of the range, and
168 * then to the NEXT nonblank of the line. Historic vi always moved
169 * to the first nonblank in the line; since the H, M, and L commands
170 * are logical motions in this implementation, we do the next nonblank
171 * so that it looks approximately the same to the user. To make this
172 * happen, the VM_RCM_SETNNB flag is set in the vcmd.c command table.
174 * If it's a motion, it's more complicated. The best possible solution
175 * is probably to display the first nonblank of the line the cursor
176 * will eventually rest on. This is tricky, particularly given that if
177 * the associated command is a delete, we don't yet know what line that
178 * will be. So, we clear the VM_RCM_SETNNB flag, and set the first
179 * nonblank flag (VM_RCM_SETFNB). Note, if the lines are sufficiently
180 * long, this can cause the cursor to warp out of the screen. It's too
184 * The G command is always first nonblank, so it's okay to reset it.
187 F_CLR(vp
, VM_RCM_MASK
);
188 F_SET(vp
, VM_RCM_SETFNB
);
193 * If moving backward in the file, delete and yank move to the end
194 * of the range, unless the line didn't change, in which case yank
195 * doesn't move. If moving forward in the file, delete and yank
196 * stay at the start of the range. Ignore others.
198 if (vp
->m_stop
.lno
< vp
->m_start
.lno
||
199 (vp
->m_stop
.lno
== vp
->m_start
.lno
&&
200 vp
->m_stop
.cno
< vp
->m_start
.cno
)) {
201 if (ISCMD(vp
->rkp
, 'y') && vp
->m_stop
.lno
== vp
->m_start
.lno
)
202 vp
->m_final
= vp
->m_start
;
204 vp
->m_final
= vp
->m_start
;
208 * v_up -- [count]^P, [count]k, [count]-
211 * PUBLIC: int v_up __P((SCR *, VICMD *));
214 v_up(SCR
*sp
, VICMD
*vp
)
218 lno
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
219 if (vp
->m_start
.lno
<= lno
) {
220 v_sof(sp
, &vp
->m_start
);
223 vp
->m_stop
.lno
= vp
->m_start
.lno
- lno
;
224 vp
->m_final
= vp
->m_stop
;
230 * In a script window, send the line to the shell.
231 * In a regular window, move down by lines.
233 * PUBLIC: int v_cr __P((SCR *, VICMD *));
236 v_cr(SCR
*sp
, VICMD
*vp
)
238 /* If it's a colon command-line edit window, it's an ex command. */
239 if (F_ISSET(sp
, SC_COMEDIT
))
240 return (v_ecl_exec(sp
));
242 /* If it's a script window, exec the line. */
243 if (F_ISSET(sp
, SC_SCRIPT
))
244 return (sscr_exec(sp
, vp
->m_start
.lno
));
246 /* Otherwise, it's the same as v_down(). */
247 return (v_down(sp
, vp
));
251 * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
252 * Move down by lines.
254 * PUBLIC: int v_down __P((SCR *, VICMD *));
257 v_down(SCR
*sp
, VICMD
*vp
)
261 lno
= vp
->m_start
.lno
+ (F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1);
262 if (!db_exist(sp
, lno
)) {
263 v_eof(sp
, &vp
->m_start
);
266 vp
->m_stop
.lno
= lno
;
267 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
272 * v_hpageup -- [count]^U
273 * Page up half screens.
275 * PUBLIC: int v_hpageup __P((SCR *, VICMD *));
278 v_hpageup(SCR
*sp
, VICMD
*vp
)
281 * Half screens always succeed unless already at SOF.
284 * Half screens set the scroll value, even if the command
285 * ultimately failed, in historic vi. Probably a don't care.
287 if (F_ISSET(vp
, VC_C1SET
))
288 sp
->defscroll
= vp
->count
;
289 if (vs_sm_scroll(sp
, &vp
->m_stop
, sp
->defscroll
, CNTRL_U
))
291 vp
->m_final
= vp
->m_stop
;
296 * v_hpagedown -- [count]^D
297 * Page down half screens.
299 * PUBLIC: int v_hpagedown __P((SCR *, VICMD *));
302 v_hpagedown(SCR
*sp
, VICMD
*vp
)
305 * Half screens always succeed unless already at EOF.
308 * Half screens set the scroll value, even if the command
309 * ultimately failed, in historic vi. Probably a don't care.
311 if (F_ISSET(vp
, VC_C1SET
))
312 sp
->defscroll
= vp
->count
;
313 if (vs_sm_scroll(sp
, &vp
->m_stop
, sp
->defscroll
, CNTRL_D
))
315 vp
->m_final
= vp
->m_stop
;
320 * v_pagedown -- [count]^F
321 * Page down full screens.
323 * Historic vi did not move to the EOF if the screen couldn't move, i.e.
324 * if EOF was already displayed on the screen. This implementation does
325 * move to EOF in that case, making ^F more like the the historic ^D.
327 * PUBLIC: int v_pagedown __P((SCR *, VICMD *));
330 v_pagedown(SCR
*sp
, VICMD
*vp
)
336 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
338 * top_line = top_line + count * (window - 2);
340 * which was historically wrong. The correct one is:
342 * top_line = top_line + count * window - 2;
344 * i.e. the two line "overlap" was only subtracted once. Which
345 * makes no sense, but then again, an overlap makes no sense for
346 * any screen but the "next" one anyway. We do it the historical
347 * way as there's no good reason to change it.
349 * If the screen has been split horizontally, use the smaller of
350 * the current window size and the window option value.
352 * It possible for this calculation to be less than 1; move at
355 offset
= (F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1) * (IS_HSPLIT(sp
) ?
356 MIN(sp
->t_maxrows
, O_VAL(sp
, O_WINDOW
)) : O_VAL(sp
, O_WINDOW
));
357 offset
= offset
<= 2 ? 1 : offset
- 2;
358 if (vs_sm_scroll(sp
, &vp
->m_stop
, offset
, CNTRL_F
))
360 vp
->m_final
= vp
->m_stop
;
365 * v_pageup -- [count]^B
366 * Page up full screens.
369 * Historic vi did not move to the SOF if the screen couldn't move, i.e.
370 * if SOF was already displayed on the screen. This implementation does
371 * move to SOF in that case, making ^B more like the the historic ^U.
373 * PUBLIC: int v_pageup __P((SCR *, VICMD *));
376 v_pageup(SCR
*sp
, VICMD
*vp
)
382 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
384 * top_line = top_line - count * (window - 2);
386 * which was historically wrong. The correct one is:
388 * top_line = (top_line - count * window) + 2;
390 * A simpler expression is that, as with ^F, we scroll exactly:
396 * Bizarre. As with ^F, an overlap makes no sense for anything
397 * but the first screen. We do it the historical way as there's
398 * no good reason to change it.
400 * If the screen has been split horizontally, use the smaller of
401 * the current window size and the window option value.
403 * It possible for this calculation to be less than 1; move at
406 offset
= (F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1) * (IS_HSPLIT(sp
) ?
407 MIN(sp
->t_maxrows
, O_VAL(sp
, O_WINDOW
)) : O_VAL(sp
, O_WINDOW
));
408 offset
= offset
<= 2 ? 1 : offset
- 2;
409 if (vs_sm_scroll(sp
, &vp
->m_stop
, offset
, CNTRL_B
))
411 vp
->m_final
= vp
->m_stop
;
416 * v_lineup -- [count]^Y
419 * PUBLIC: int v_lineup __P((SCR *, VICMD *));
422 v_lineup(SCR
*sp
, VICMD
*vp
)
425 * The cursor moves down, staying with its original line, unless it
426 * reaches the bottom of the screen.
429 &vp
->m_stop
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, CNTRL_Y
))
431 vp
->m_final
= vp
->m_stop
;
436 * v_linedown -- [count]^E
437 * Page down by lines.
439 * PUBLIC: int v_linedown __P((SCR *, VICMD *));
442 v_linedown(SCR
*sp
, VICMD
*vp
)
445 * The cursor moves up, staying with its original line, unless it
446 * reaches the top of the screen.
449 &vp
->m_stop
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, CNTRL_E
))
451 vp
->m_final
= vp
->m_stop
;