2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 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 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
25 #include "../common/common.h"
28 static void goto_adjust
__P((VICMD
*));
31 * The historic vi had a problem in that all movements were by physical
32 * lines, not by logical, or screen lines. Arguments can be made that this
33 * is the right thing to do. For example, single line movements, such as
34 * 'j' or 'k', should probably work on physical lines. Commands like "dj",
35 * or "j.", where '.' is a change command, make more sense for physical lines
36 * than they do for logical lines.
38 * These arguments, however, don't apply to scrolling commands like ^D and
39 * ^F -- if the window is fairly small, using physical lines can result in
40 * a half-page scroll repainting the entire screen, which is not what the
41 * user wanted. Second, if the line is larger than the screen, using physical
42 * lines can make it impossible to display parts of the line -- there aren't
43 * any commands that don't display the beginning of the line in historic vi,
44 * and if both the beginning and end of the line can't be on the screen at
45 * the same time, you lose. This is even worse in the case of the H, L, and
46 * M commands -- for large lines, they may all refer to the same line and
47 * will result in no movement at all.
49 * Another issue is that page and half-page scrolling commands historically
50 * moved to the first non-blank character in the new line. If the line is
51 * approximately the same size as the screen, this loses because the cursor
52 * before and after a ^D, may refer to the same location on the screen. In
53 * this implementation, scrolling commands set the cursor to the first non-
54 * blank character if the line changes because of the scroll. Otherwise,
55 * the cursor is left alone.
57 * This implementation does the scrolling (^B, ^D, ^F, ^U, ^Y, ^E), and the
58 * cursor positioning commands (H, L, M) commands using logical lines, not
64 * Go to first non-blank character of the line count, the last line
65 * of the file by default.
67 * PUBLIC: int v_lgoto __P((SCR *, VICMD *));
70 v_lgoto(SCR
*sp
, VICMD
*vp
)
74 if (F_ISSET(vp
, VC_C1SET
)) {
75 if (!db_exist(sp
, vp
->count
)) {
78 * Historically, 1G was legal in an empty file.
81 if (db_last(sp
, &nlines
))
86 v_eof(sp
, &vp
->m_start
);
89 vp
->m_stop
.lno
= vp
->count
;
91 if (db_last(sp
, &nlines
))
93 vp
->m_stop
.lno
= nlines
? nlines
: 1;
101 * Move to the first non-blank character of the logical line
102 * count - 1 from the top of the screen, 0 by default.
104 * PUBLIC: int v_home __P((SCR *, VICMD *));
107 v_home(SCR
*sp
, VICMD
*vp
)
109 if (vs_sm_position(sp
, &vp
->m_stop
,
110 F_ISSET(vp
, VC_C1SET
) ? vp
->count
- 1 : 0, P_TOP
))
118 * Move to the first non-blank character of the logical line
119 * in the middle of the screen.
121 * PUBLIC: int v_middle __P((SCR *, VICMD *));
124 v_middle(SCR
*sp
, VICMD
*vp
)
127 * Yielding to none in our quest for compatibility with every
128 * historical blemish of vi, no matter how strange it might be,
129 * we permit the user to enter a count and then ignore it.
131 if (vs_sm_position(sp
, &vp
->m_stop
, 0, P_MIDDLE
))
138 * v_bottom -- [count]L
139 * Move to the first non-blank character of the logical line
140 * count - 1 from the bottom of the screen, 0 by default.
142 * PUBLIC: int v_bottom __P((SCR *, VICMD *));
145 v_bottom(SCR
*sp
, VICMD
*vp
)
147 if (vs_sm_position(sp
, &vp
->m_stop
,
148 F_ISSET(vp
, VC_C1SET
) ? vp
->count
- 1 : 0, P_BOTTOM
))
155 goto_adjust(VICMD
*vp
)
157 /* Guess that it's the end of the range. */
158 vp
->m_final
= vp
->m_stop
;
161 * Non-motion commands move the cursor to the end of the range, and
162 * then to the NEXT nonblank of the line. Historic vi always moved
163 * to the first nonblank in the line; since the H, M, and L commands
164 * are logical motions in this implementation, we do the next nonblank
165 * so that it looks approximately the same to the user. To make this
166 * happen, the VM_RCM_SETNNB flag is set in the vcmd.c command table.
168 * If it's a motion, it's more complicated. The best possible solution
169 * is probably to display the first nonblank of the line the cursor
170 * will eventually rest on. This is tricky, particularly given that if
171 * the associated command is a delete, we don't yet know what line that
172 * will be. So, we clear the VM_RCM_SETNNB flag, and set the first
173 * nonblank flag (VM_RCM_SETFNB). Note, if the lines are sufficiently
174 * long, this can cause the cursor to warp out of the screen. It's too
178 * The G command is always first nonblank, so it's okay to reset it.
181 F_CLR(vp
, VM_RCM_MASK
);
182 F_SET(vp
, VM_RCM_SETFNB
);
187 * If moving backward in the file, delete and yank move to the end
188 * of the range, unless the line didn't change, in which case yank
189 * doesn't move. If moving forward in the file, delete and yank
190 * stay at the start of the range. Ignore others.
192 if (vp
->m_stop
.lno
< vp
->m_start
.lno
||
193 vp
->m_stop
.lno
== vp
->m_start
.lno
&&
194 vp
->m_stop
.cno
< vp
->m_start
.cno
) {
195 if (ISCMD(vp
->rkp
, 'y') && vp
->m_stop
.lno
== vp
->m_start
.lno
)
196 vp
->m_final
= vp
->m_start
;
198 vp
->m_final
= vp
->m_start
;
202 * v_up -- [count]^P, [count]k, [count]-
205 * PUBLIC: int v_up __P((SCR *, VICMD *));
208 v_up(SCR
*sp
, VICMD
*vp
)
212 lno
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
213 if (vp
->m_start
.lno
<= lno
) {
214 v_sof(sp
, &vp
->m_start
);
217 vp
->m_stop
.lno
= vp
->m_start
.lno
- lno
;
218 vp
->m_final
= vp
->m_stop
;
224 * In a script window, send the line to the shell.
225 * In a regular window, move down by lines.
227 * PUBLIC: int v_cr __P((SCR *, VICMD *));
230 v_cr(SCR
*sp
, VICMD
*vp
)
232 /* If it's a colon command-line edit window, it's an ex command. */
233 if (F_ISSET(sp
, SC_COMEDIT
))
234 return (v_ecl_exec(sp
));
236 /* If it's a script window, exec the line. */
237 if (F_ISSET(sp
, SC_SCRIPT
))
238 return (sscr_exec(sp
, vp
->m_start
.lno
));
240 /* Otherwise, it's the same as v_down(). */
241 return (v_down(sp
, vp
));
245 * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
246 * Move down by lines.
248 * PUBLIC: int v_down __P((SCR *, VICMD *));
251 v_down(SCR
*sp
, VICMD
*vp
)
255 lno
= vp
->m_start
.lno
+ (F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1);
256 if (!db_exist(sp
, lno
)) {
257 v_eof(sp
, &vp
->m_start
);
260 vp
->m_stop
.lno
= lno
;
261 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
266 * v_hpageup -- [count]^U
267 * Page up half screens.
269 * PUBLIC: int v_hpageup __P((SCR *, VICMD *));
272 v_hpageup(SCR
*sp
, VICMD
*vp
)
275 * Half screens always succeed unless already at SOF.
278 * Half screens set the scroll value, even if the command
279 * ultimately failed, in historic vi. Probably a don't care.
281 if (F_ISSET(vp
, VC_C1SET
))
282 sp
->defscroll
= vp
->count
;
283 if (vs_sm_scroll(sp
, &vp
->m_stop
, sp
->defscroll
, CNTRL_U
))
285 vp
->m_final
= vp
->m_stop
;
290 * v_hpagedown -- [count]^D
291 * Page down half screens.
293 * PUBLIC: int v_hpagedown __P((SCR *, VICMD *));
296 v_hpagedown(SCR
*sp
, VICMD
*vp
)
299 * Half screens always succeed unless already at EOF.
302 * Half screens set the scroll value, even if the command
303 * ultimately failed, in historic vi. Probably a don't care.
305 if (F_ISSET(vp
, VC_C1SET
))
306 sp
->defscroll
= vp
->count
;
307 if (vs_sm_scroll(sp
, &vp
->m_stop
, sp
->defscroll
, CNTRL_D
))
309 vp
->m_final
= vp
->m_stop
;
314 * v_pagedown -- [count]^F
315 * Page down full screens.
317 * Historic vi did not move to the EOF if the screen couldn't move, i.e.
318 * if EOF was already displayed on the screen. This implementation does
319 * move to EOF in that case, making ^F more like the the historic ^D.
321 * PUBLIC: int v_pagedown __P((SCR *, VICMD *));
324 v_pagedown(SCR
*sp
, VICMD
*vp
)
330 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
332 * top_line = top_line + count * (window - 2);
334 * which was historically wrong. The correct one is:
336 * top_line = top_line + count * window - 2;
338 * i.e. the two line "overlap" was only subtracted once. Which
339 * makes no sense, but then again, an overlap makes no sense for
340 * any screen but the "next" one anyway. We do it the historical
341 * way as there's no good reason to change it.
343 * If the screen has been split horizontally, use the smaller of
344 * the current window size and the window option value.
346 * It possible for this calculation to be less than 1; move at
349 offset
= (F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1) * (IS_HSPLIT(sp
) ?
350 MIN(sp
->t_maxrows
, O_VAL(sp
, O_WINDOW
)) : O_VAL(sp
, O_WINDOW
));
351 offset
= offset
<= 2 ? 1 : offset
- 2;
352 if (vs_sm_scroll(sp
, &vp
->m_stop
, offset
, CNTRL_F
))
354 vp
->m_final
= vp
->m_stop
;
359 * v_pageup -- [count]^B
360 * Page up full screens.
363 * Historic vi did not move to the SOF if the screen couldn't move, i.e.
364 * if SOF was already displayed on the screen. This implementation does
365 * move to SOF in that case, making ^B more like the the historic ^U.
367 * PUBLIC: int v_pageup __P((SCR *, VICMD *));
370 v_pageup(SCR
*sp
, VICMD
*vp
)
376 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
378 * top_line = top_line - count * (window - 2);
380 * which was historically wrong. The correct one is:
382 * top_line = (top_line - count * window) + 2;
384 * A simpler expression is that, as with ^F, we scroll exactly:
390 * Bizarre. As with ^F, an overlap makes no sense for anything
391 * but the first screen. We do it the historical way as there's
392 * no good reason to change it.
394 * If the screen has been split horizontally, use the smaller of
395 * the current window size and the window option value.
397 * It possible for this calculation to be less than 1; move at
400 offset
= (F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1) * (IS_HSPLIT(sp
) ?
401 MIN(sp
->t_maxrows
, O_VAL(sp
, O_WINDOW
)) : O_VAL(sp
, O_WINDOW
));
402 offset
= offset
<= 2 ? 1 : offset
- 2;
403 if (vs_sm_scroll(sp
, &vp
->m_stop
, offset
, CNTRL_B
))
405 vp
->m_final
= vp
->m_stop
;
410 * v_lineup -- [count]^Y
413 * PUBLIC: int v_lineup __P((SCR *, VICMD *));
416 v_lineup(SCR
*sp
, VICMD
*vp
)
419 * The cursor moves down, staying with its original line, unless it
420 * reaches the bottom of the screen.
423 &vp
->m_stop
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, CNTRL_Y
))
425 vp
->m_final
= vp
->m_stop
;
430 * v_linedown -- [count]^E
431 * Page down by lines.
433 * PUBLIC: int v_linedown __P((SCR *, VICMD *));
436 v_linedown(SCR
*sp
, VICMD
*vp
)
439 * The cursor moves up, staying with its original line, unless it
440 * reaches the top of the screen.
443 &vp
->m_stop
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, CNTRL_E
))
445 vp
->m_final
= vp
->m_stop
;