optionally use bundled db
[nvi.git] / vi / v_scroll.c
blob6944062ab21820c6fb2fa8f5e1089751eb6e9443
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 #ifndef lint
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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
25 #include "../common/common.h"
26 #include "vi.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
59 * physical.
63 * v_lgoto -- [count]G
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 *));
69 int
70 v_lgoto(SCR *sp, VICMD *vp)
72 db_recno_t nlines;
74 if (F_ISSET(vp, VC_C1SET)) {
75 if (!db_exist(sp, vp->count)) {
77 * !!!
78 * Historically, 1G was legal in an empty file.
80 if (vp->count == 1) {
81 if (db_last(sp, &nlines))
82 return (1);
83 if (nlines == 0)
84 return (0);
86 v_eof(sp, &vp->m_start);
87 return (1);
89 vp->m_stop.lno = vp->count;
90 } else {
91 if (db_last(sp, &nlines))
92 return (1);
93 vp->m_stop.lno = nlines ? nlines : 1;
95 goto_adjust(vp);
96 return (0);
100 * v_home -- [count]H
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))
111 return (1);
112 goto_adjust(vp);
113 return (0);
117 * v_middle -- M
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))
132 return (1);
133 goto_adjust(vp);
134 return (0);
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))
149 return (1);
150 goto_adjust(vp);
151 return (0);
154 static void
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
175 * hard to fix.
177 * XXX
178 * The G command is always first nonblank, so it's okay to reset it.
180 if (ISMOTION(vp)) {
181 F_CLR(vp, VM_RCM_MASK);
182 F_SET(vp, VM_RCM_SETFNB);
183 } else
184 return;
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;
197 } else
198 vp->m_final = vp->m_start;
202 * v_up -- [count]^P, [count]k, [count]-
203 * Move up by lines.
205 * PUBLIC: int v_up __P((SCR *, VICMD *));
208 v_up(SCR *sp, VICMD *vp)
210 db_recno_t lno;
212 lno = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
213 if (vp->m_start.lno <= lno) {
214 v_sof(sp, &vp->m_start);
215 return (1);
217 vp->m_stop.lno = vp->m_start.lno - lno;
218 vp->m_final = vp->m_stop;
219 return (0);
223 * v_cr -- [count]^M
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)
253 db_recno_t lno;
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);
258 return (1);
260 vp->m_stop.lno = lno;
261 vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
262 return (0);
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.
277 * !!!
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))
284 return (1);
285 vp->m_final = vp->m_stop;
286 return (0);
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.
301 * !!!
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))
308 return (1);
309 vp->m_final = vp->m_stop;
310 return (0);
314 * v_pagedown -- [count]^F
315 * Page down full screens.
316 * !!!
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)
326 db_recno_t offset;
329 * !!!
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
347 * least one line.
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))
353 return (1);
354 vp->m_final = vp->m_stop;
355 return (0);
359 * v_pageup -- [count]^B
360 * Page up full screens.
362 * !!!
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)
372 db_recno_t offset;
375 * !!!
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:
386 * count * window - 2
388 * lines.
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
398 * least one line.
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))
404 return (1);
405 vp->m_final = vp->m_stop;
406 return (0);
410 * v_lineup -- [count]^Y
411 * Page up by lines.
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.
422 if (vs_sm_scroll(sp,
423 &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_Y))
424 return (1);
425 vp->m_final = vp->m_stop;
426 return (0);
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.
442 if (vs_sm_scroll(sp,
443 &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_E))
444 return (1);
445 vp->m_final = vp->m_stop;
446 return (0);