nrelease: Clean up a bit the 'clean' target
[dragonfly.git] / contrib / nvi2 / vi / v_scroll.c
blob1ee5fcb5eb17a165d69840762c40f0ab5c84f093
1 /*-
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.
8 */
10 #include "config.h"
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
16 #include <bitstring.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <stdio.h>
21 #include "../common/common.h"
22 #include "vi.h"
24 static void goto_adjust(VICMD *);
27 * The historic vi had a problem in that all movements were by physical
28 * lines, not by logical, or screen lines. Arguments can be made that this
29 * is the right thing to do. For example, single line movements, such as
30 * 'j' or 'k', should probably work on physical lines. Commands like "dj",
31 * or "j.", where '.' is a change command, make more sense for physical lines
32 * than they do for logical lines.
34 * These arguments, however, don't apply to scrolling commands like ^D and
35 * ^F -- if the window is fairly small, using physical lines can result in
36 * a half-page scroll repainting the entire screen, which is not what the
37 * user wanted. Second, if the line is larger than the screen, using physical
38 * lines can make it impossible to display parts of the line -- there aren't
39 * any commands that don't display the beginning of the line in historic vi,
40 * and if both the beginning and end of the line can't be on the screen at
41 * the same time, you lose. This is even worse in the case of the H, L, and
42 * M commands -- for large lines, they may all refer to the same line and
43 * will result in no movement at all.
45 * Another issue is that page and half-page scrolling commands historically
46 * moved to the first non-blank character in the new line. If the line is
47 * approximately the same size as the screen, this loses because the cursor
48 * before and after a ^D, may refer to the same location on the screen. In
49 * this implementation, scrolling commands set the cursor to the first non-
50 * blank character if the line changes because of the scroll. Otherwise,
51 * the cursor is left alone.
53 * This implementation does the scrolling (^B, ^D, ^F, ^U, ^Y, ^E), and the
54 * cursor positioning commands (H, L, M) commands using logical lines, not
55 * physical.
59 * v_lgoto -- [count]G
60 * Go to first non-blank character of the line count, the last line
61 * of the file by default.
63 * PUBLIC: int v_lgoto(SCR *, VICMD *);
65 int
66 v_lgoto(SCR *sp, VICMD *vp)
68 recno_t nlines;
70 if (F_ISSET(vp, VC_C1SET)) {
71 if (!db_exist(sp, vp->count)) {
73 * !!!
74 * Historically, 1G was legal in an empty file.
76 if (vp->count == 1) {
77 if (db_last(sp, &nlines))
78 return (1);
79 if (nlines == 0)
80 return (0);
82 v_eof(sp, &vp->m_start);
83 return (1);
85 vp->m_stop.lno = vp->count;
86 } else {
87 if (db_last(sp, &nlines))
88 return (1);
89 vp->m_stop.lno = nlines ? nlines : 1;
91 goto_adjust(vp);
92 return (0);
96 * v_home -- [count]H
97 * Move to the first non-blank character of the logical line
98 * count - 1 from the top of the screen, 0 by default.
100 * PUBLIC: int v_home(SCR *, VICMD *);
103 v_home(SCR *sp, VICMD *vp)
105 if (vs_sm_position(sp, &vp->m_stop,
106 F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0, P_TOP))
107 return (1);
108 goto_adjust(vp);
109 return (0);
113 * v_middle -- M
114 * Move to the first non-blank character of the logical line
115 * in the middle of the screen.
117 * PUBLIC: int v_middle(SCR *, VICMD *);
120 v_middle(SCR *sp, VICMD *vp)
123 * Yielding to none in our quest for compatibility with every
124 * historical blemish of vi, no matter how strange it might be,
125 * we permit the user to enter a count and then ignore it.
127 if (vs_sm_position(sp, &vp->m_stop, 0, P_MIDDLE))
128 return (1);
129 goto_adjust(vp);
130 return (0);
134 * v_bottom -- [count]L
135 * Move to the first non-blank character of the logical line
136 * count - 1 from the bottom of the screen, 0 by default.
138 * PUBLIC: int v_bottom(SCR *, VICMD *);
141 v_bottom(SCR *sp, VICMD *vp)
143 if (vs_sm_position(sp, &vp->m_stop,
144 F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0, P_BOTTOM))
145 return (1);
146 goto_adjust(vp);
147 return (0);
150 static void
151 goto_adjust(VICMD *vp)
153 /* Guess that it's the end of the range. */
154 vp->m_final = vp->m_stop;
157 * Non-motion commands move the cursor to the end of the range, and
158 * then to the NEXT nonblank of the line. Historic vi always moved
159 * to the first nonblank in the line; since the H, M, and L commands
160 * are logical motions in this implementation, we do the next nonblank
161 * so that it looks approximately the same to the user. To make this
162 * happen, the VM_RCM_SETNNB flag is set in the vcmd.c command table.
164 * If it's a motion, it's more complicated. The best possible solution
165 * is probably to display the first nonblank of the line the cursor
166 * will eventually rest on. This is tricky, particularly given that if
167 * the associated command is a delete, we don't yet know what line that
168 * will be. So, we clear the VM_RCM_SETNNB flag, and set the first
169 * nonblank flag (VM_RCM_SETFNB). Note, if the lines are sufficiently
170 * long, this can cause the cursor to warp out of the screen. It's too
171 * hard to fix.
173 * XXX
174 * The G command is always first nonblank, so it's okay to reset it.
176 if (ISMOTION(vp)) {
177 F_CLR(vp, VM_RCM_MASK);
178 F_SET(vp, VM_RCM_SETFNB);
179 } else
180 return;
183 * If moving backward in the file, delete and yank move to the end
184 * of the range, unless the line didn't change, in which case yank
185 * doesn't move. If moving forward in the file, delete and yank
186 * stay at the start of the range. Ignore others.
188 if (vp->m_stop.lno < vp->m_start.lno ||
189 (vp->m_stop.lno == vp->m_start.lno &&
190 vp->m_stop.cno < vp->m_start.cno)) {
191 if (ISCMD(vp->rkp, 'y') && vp->m_stop.lno == vp->m_start.lno)
192 vp->m_final = vp->m_start;
193 } else
194 vp->m_final = vp->m_start;
198 * v_up -- [count]^P, [count]k, [count]-
199 * Move up by lines.
201 * PUBLIC: int v_up(SCR *, VICMD *);
204 v_up(SCR *sp, VICMD *vp)
206 recno_t lno;
208 lno = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
209 if (vp->m_start.lno <= lno) {
210 v_sof(sp, &vp->m_start);
211 return (1);
213 vp->m_stop.lno = vp->m_start.lno - lno;
214 vp->m_final = vp->m_stop;
215 return (0);
219 * v_cr -- [count]^M
220 * In a script window, send the line to the shell.
221 * In a regular window, move down by lines.
223 * PUBLIC: int v_cr(SCR *, VICMD *);
226 v_cr(SCR *sp, VICMD *vp)
228 /* If it's a colon command-line edit window, it's an ex command. */
229 if (F_ISSET(sp, SC_COMEDIT))
230 return (v_ecl_exec(sp));
232 /* If it's a script window, exec the line. */
233 if (F_ISSET(sp, SC_SCRIPT))
234 return (sscr_exec(sp, vp->m_start.lno));
236 /* Otherwise, it's the same as v_down(). */
237 return (v_down(sp, vp));
241 * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
242 * Move down by lines.
244 * PUBLIC: int v_down(SCR *, VICMD *);
247 v_down(SCR *sp, VICMD *vp)
249 recno_t lno;
251 lno = vp->m_start.lno + (F_ISSET(vp, VC_C1SET) ? vp->count : 1);
252 if (!db_exist(sp, lno)) {
253 v_eof(sp, &vp->m_start);
254 return (1);
256 vp->m_stop.lno = lno;
257 vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
258 return (0);
262 * v_hpageup -- [count]^U
263 * Page up half screens.
265 * PUBLIC: int v_hpageup(SCR *, VICMD *);
268 v_hpageup(SCR *sp, VICMD *vp)
271 * Half screens always succeed unless already at SOF.
273 * !!!
274 * Half screens set the scroll value, even if the command
275 * ultimately failed, in historic vi. Probably a don't care.
277 if (F_ISSET(vp, VC_C1SET))
278 sp->defscroll = vp->count;
279 if (vs_sm_scroll(sp, &vp->m_stop, sp->defscroll, CNTRL_U))
280 return (1);
281 vp->m_final = vp->m_stop;
282 return (0);
286 * v_hpagedown -- [count]^D
287 * Page down half screens.
289 * PUBLIC: int v_hpagedown(SCR *, VICMD *);
292 v_hpagedown(SCR *sp, VICMD *vp)
295 * Half screens always succeed unless already at EOF.
297 * !!!
298 * Half screens set the scroll value, even if the command
299 * ultimately failed, in historic vi. Probably a don't care.
301 if (F_ISSET(vp, VC_C1SET))
302 sp->defscroll = vp->count;
303 if (vs_sm_scroll(sp, &vp->m_stop, sp->defscroll, CNTRL_D))
304 return (1);
305 vp->m_final = vp->m_stop;
306 return (0);
310 * v_pagedown -- [count]^F
311 * Page down full screens.
312 * !!!
313 * Historic vi did not move to the EOF if the screen couldn't move, i.e.
314 * if EOF was already displayed on the screen. This implementation does
315 * move to EOF in that case, making ^F more like the historic ^D.
317 * PUBLIC: int v_pagedown(SCR *, VICMD *);
320 v_pagedown(SCR *sp, VICMD *vp)
322 recno_t offset;
325 * !!!
326 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
328 * top_line = top_line + count * (window - 2);
330 * which was historically wrong. The correct one is:
332 * top_line = top_line + count * window - 2;
334 * i.e. the two line "overlap" was only subtracted once. Which
335 * makes no sense, but then again, an overlap makes no sense for
336 * any screen but the "next" one anyway. We do it the historical
337 * way as there's no good reason to change it.
339 * If the screen has been split horizontally, use the smaller of
340 * the current window size and the window option value.
342 * It possible for this calculation to be less than 1; move at
343 * least one line.
345 offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_SPLIT(sp) ?
346 MIN(sp->t_maxrows, O_VAL(sp, O_WINDOW)) : O_VAL(sp, O_WINDOW));
347 offset = offset <= 2 ? 1 : offset - 2;
348 if (vs_sm_scroll(sp, &vp->m_stop, offset, CNTRL_F))
349 return (1);
350 vp->m_final = vp->m_stop;
351 return (0);
355 * v_pageup -- [count]^B
356 * Page up full screens.
358 * !!!
359 * Historic vi did not move to the SOF if the screen couldn't move, i.e.
360 * if SOF was already displayed on the screen. This implementation does
361 * move to SOF in that case, making ^B more like the historic ^U.
363 * PUBLIC: int v_pageup(SCR *, VICMD *);
366 v_pageup(SCR *sp, VICMD *vp)
368 recno_t offset;
371 * !!!
372 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
374 * top_line = top_line - count * (window - 2);
376 * which was historically wrong. The correct one is:
378 * top_line = (top_line - count * window) + 2;
380 * A simpler expression is that, as with ^F, we scroll exactly:
382 * count * window - 2
384 * lines.
386 * Bizarre. As with ^F, an overlap makes no sense for anything
387 * but the first screen. We do it the historical way as there's
388 * no good reason to change it.
390 * If the screen has been split horizontally, use the smaller of
391 * the current window size and the window option value.
393 * It possible for this calculation to be less than 1; move at
394 * least one line.
396 offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_SPLIT(sp) ?
397 MIN(sp->t_maxrows, O_VAL(sp, O_WINDOW)) : O_VAL(sp, O_WINDOW));
398 offset = offset <= 2 ? 1 : offset - 2;
399 if (vs_sm_scroll(sp, &vp->m_stop, offset, CNTRL_B))
400 return (1);
401 vp->m_final = vp->m_stop;
402 return (0);
406 * v_lineup -- [count]^Y
407 * Page up by lines.
409 * PUBLIC: int v_lineup(SCR *, VICMD *);
412 v_lineup(SCR *sp, VICMD *vp)
415 * The cursor moves down, staying with its original line, unless it
416 * reaches the bottom of the screen.
418 if (vs_sm_scroll(sp,
419 &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_Y))
420 return (1);
421 vp->m_final = vp->m_stop;
422 return (0);
426 * v_linedown -- [count]^E
427 * Page down by lines.
429 * PUBLIC: int v_linedown(SCR *, VICMD *);
432 v_linedown(SCR *sp, VICMD *vp)
435 * The cursor moves up, staying with its original line, unless it
436 * reaches the top of the screen.
438 if (vs_sm_scroll(sp,
439 &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_E))
440 return (1);
441 vp->m_final = vp->m_stop;
442 return (0);