Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / v_scroll.c
blobb8a1d1612d0c334c34cdc0da63b53a507c03a8d2
1 /* $NetBSD: v_scroll.c,v 1.1.1.2 2008/05/18 14:31:44 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
27 #include "../common/common.h"
28 #include "vi.h"
30 static void goto_adjust __P((VICMD *));
33 * The historic vi had a problem in that all movements were by physical
34 * lines, not by logical, or screen lines. Arguments can be made that this
35 * is the right thing to do. For example, single line movements, such as
36 * 'j' or 'k', should probably work on physical lines. Commands like "dj",
37 * or "j.", where '.' is a change command, make more sense for physical lines
38 * than they do for logical lines.
40 * These arguments, however, don't apply to scrolling commands like ^D and
41 * ^F -- if the window is fairly small, using physical lines can result in
42 * a half-page scroll repainting the entire screen, which is not what the
43 * user wanted. Second, if the line is larger than the screen, using physical
44 * lines can make it impossible to display parts of the line -- there aren't
45 * any commands that don't display the beginning of the line in historic vi,
46 * and if both the beginning and end of the line can't be on the screen at
47 * the same time, you lose. This is even worse in the case of the H, L, and
48 * M commands -- for large lines, they may all refer to the same line and
49 * will result in no movement at all.
51 * Another issue is that page and half-page scrolling commands historically
52 * moved to the first non-blank character in the new line. If the line is
53 * approximately the same size as the screen, this loses because the cursor
54 * before and after a ^D, may refer to the same location on the screen. In
55 * this implementation, scrolling commands set the cursor to the first non-
56 * blank character if the line changes because of the scroll. Otherwise,
57 * the cursor is left alone.
59 * This implementation does the scrolling (^B, ^D, ^F, ^U, ^Y, ^E), and the
60 * cursor positioning commands (H, L, M) commands using logical lines, not
61 * physical.
65 * v_lgoto -- [count]G
66 * Go to first non-blank character of the line count, the last line
67 * of the file by default.
69 * PUBLIC: int v_lgoto __P((SCR *, VICMD *));
71 int
72 v_lgoto(SCR *sp, VICMD *vp)
74 db_recno_t nlines;
76 if (F_ISSET(vp, VC_C1SET)) {
77 if (!db_exist(sp, vp->count)) {
79 * !!!
80 * Historically, 1G was legal in an empty file.
82 if (vp->count == 1) {
83 if (db_last(sp, &nlines))
84 return (1);
85 if (nlines == 0)
86 return (0);
88 v_eof(sp, &vp->m_start);
89 return (1);
91 vp->m_stop.lno = vp->count;
92 } else {
93 if (db_last(sp, &nlines))
94 return (1);
95 vp->m_stop.lno = nlines ? nlines : 1;
97 goto_adjust(vp);
98 return (0);
102 * v_home -- [count]H
103 * Move to the first non-blank character of the logical line
104 * count - 1 from the top of the screen, 0 by default.
106 * PUBLIC: int v_home __P((SCR *, VICMD *));
109 v_home(SCR *sp, VICMD *vp)
111 if (vs_sm_position(sp, &vp->m_stop,
112 F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0, P_TOP))
113 return (1);
114 goto_adjust(vp);
115 return (0);
119 * v_middle -- M
120 * Move to the first non-blank character of the logical line
121 * in the middle of the screen.
123 * PUBLIC: int v_middle __P((SCR *, VICMD *));
126 v_middle(SCR *sp, VICMD *vp)
129 * Yielding to none in our quest for compatibility with every
130 * historical blemish of vi, no matter how strange it might be,
131 * we permit the user to enter a count and then ignore it.
133 if (vs_sm_position(sp, &vp->m_stop, 0, P_MIDDLE))
134 return (1);
135 goto_adjust(vp);
136 return (0);
140 * v_bottom -- [count]L
141 * Move to the first non-blank character of the logical line
142 * count - 1 from the bottom of the screen, 0 by default.
144 * PUBLIC: int v_bottom __P((SCR *, VICMD *));
147 v_bottom(SCR *sp, VICMD *vp)
149 if (vs_sm_position(sp, &vp->m_stop,
150 F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0, P_BOTTOM))
151 return (1);
152 goto_adjust(vp);
153 return (0);
156 static void
157 goto_adjust(VICMD *vp)
159 /* Guess that it's the end of the range. */
160 vp->m_final = vp->m_stop;
163 * Non-motion commands move the cursor to the end of the range, and
164 * then to the NEXT nonblank of the line. Historic vi always moved
165 * to the first nonblank in the line; since the H, M, and L commands
166 * are logical motions in this implementation, we do the next nonblank
167 * so that it looks approximately the same to the user. To make this
168 * happen, the VM_RCM_SETNNB flag is set in the vcmd.c command table.
170 * If it's a motion, it's more complicated. The best possible solution
171 * is probably to display the first nonblank of the line the cursor
172 * will eventually rest on. This is tricky, particularly given that if
173 * the associated command is a delete, we don't yet know what line that
174 * will be. So, we clear the VM_RCM_SETNNB flag, and set the first
175 * nonblank flag (VM_RCM_SETFNB). Note, if the lines are sufficiently
176 * long, this can cause the cursor to warp out of the screen. It's too
177 * hard to fix.
179 * XXX
180 * The G command is always first nonblank, so it's okay to reset it.
182 if (ISMOTION(vp)) {
183 F_CLR(vp, VM_RCM_MASK);
184 F_SET(vp, VM_RCM_SETFNB);
185 } else
186 return;
189 * If moving backward in the file, delete and yank move to the end
190 * of the range, unless the line didn't change, in which case yank
191 * doesn't move. If moving forward in the file, delete and yank
192 * stay at the start of the range. Ignore others.
194 if (vp->m_stop.lno < vp->m_start.lno ||
195 (vp->m_stop.lno == vp->m_start.lno &&
196 vp->m_stop.cno < vp->m_start.cno)) {
197 if (ISCMD(vp->rkp, 'y') && vp->m_stop.lno == vp->m_start.lno)
198 vp->m_final = vp->m_start;
199 } else
200 vp->m_final = vp->m_start;
204 * v_up -- [count]^P, [count]k, [count]-
205 * Move up by lines.
207 * PUBLIC: int v_up __P((SCR *, VICMD *));
210 v_up(SCR *sp, VICMD *vp)
212 db_recno_t lno;
214 lno = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
215 if (vp->m_start.lno <= lno) {
216 v_sof(sp, &vp->m_start);
217 return (1);
219 vp->m_stop.lno = vp->m_start.lno - lno;
220 vp->m_final = vp->m_stop;
221 return (0);
225 * v_cr -- [count]^M
226 * In a script window, send the line to the shell.
227 * In a regular window, move down by lines.
229 * PUBLIC: int v_cr __P((SCR *, VICMD *));
232 v_cr(SCR *sp, VICMD *vp)
234 /* If it's a colon command-line edit window, it's an ex command. */
235 if (F_ISSET(sp, SC_COMEDIT))
236 return (v_ecl_exec(sp));
238 /* If it's a script window, exec the line. */
239 if (F_ISSET(sp, SC_SCRIPT))
240 return (sscr_exec(sp, vp->m_start.lno));
242 /* Otherwise, it's the same as v_down(). */
243 return (v_down(sp, vp));
247 * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
248 * Move down by lines.
250 * PUBLIC: int v_down __P((SCR *, VICMD *));
253 v_down(SCR *sp, VICMD *vp)
255 db_recno_t lno;
257 lno = vp->m_start.lno + (F_ISSET(vp, VC_C1SET) ? vp->count : 1);
258 if (!db_exist(sp, lno)) {
259 v_eof(sp, &vp->m_start);
260 return (1);
262 vp->m_stop.lno = lno;
263 vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
264 return (0);
268 * v_hpageup -- [count]^U
269 * Page up half screens.
271 * PUBLIC: int v_hpageup __P((SCR *, VICMD *));
274 v_hpageup(SCR *sp, VICMD *vp)
277 * Half screens always succeed unless already at SOF.
279 * !!!
280 * Half screens set the scroll value, even if the command
281 * ultimately failed, in historic vi. Probably a don't care.
283 if (F_ISSET(vp, VC_C1SET))
284 sp->defscroll = vp->count;
285 if (vs_sm_scroll(sp, &vp->m_stop, sp->defscroll, CNTRL_U))
286 return (1);
287 vp->m_final = vp->m_stop;
288 return (0);
292 * v_hpagedown -- [count]^D
293 * Page down half screens.
295 * PUBLIC: int v_hpagedown __P((SCR *, VICMD *));
298 v_hpagedown(SCR *sp, VICMD *vp)
301 * Half screens always succeed unless already at EOF.
303 * !!!
304 * Half screens set the scroll value, even if the command
305 * ultimately failed, in historic vi. Probably a don't care.
307 if (F_ISSET(vp, VC_C1SET))
308 sp->defscroll = vp->count;
309 if (vs_sm_scroll(sp, &vp->m_stop, sp->defscroll, CNTRL_D))
310 return (1);
311 vp->m_final = vp->m_stop;
312 return (0);
316 * v_pagedown -- [count]^F
317 * Page down full screens.
318 * !!!
319 * Historic vi did not move to the EOF if the screen couldn't move, i.e.
320 * if EOF was already displayed on the screen. This implementation does
321 * move to EOF in that case, making ^F more like the the historic ^D.
323 * PUBLIC: int v_pagedown __P((SCR *, VICMD *));
326 v_pagedown(SCR *sp, VICMD *vp)
328 db_recno_t offset;
331 * !!!
332 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
334 * top_line = top_line + count * (window - 2);
336 * which was historically wrong. The correct one is:
338 * top_line = top_line + count * window - 2;
340 * i.e. the two line "overlap" was only subtracted once. Which
341 * makes no sense, but then again, an overlap makes no sense for
342 * any screen but the "next" one anyway. We do it the historical
343 * way as there's no good reason to change it.
345 * If the screen has been split horizontally, use the smaller of
346 * the current window size and the window option value.
348 * It possible for this calculation to be less than 1; move at
349 * least one line.
351 offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_HSPLIT(sp) ?
352 MIN(sp->t_maxrows, O_VAL(sp, O_WINDOW)) : O_VAL(sp, O_WINDOW));
353 offset = offset <= 2 ? 1 : offset - 2;
354 if (vs_sm_scroll(sp, &vp->m_stop, offset, CNTRL_F))
355 return (1);
356 vp->m_final = vp->m_stop;
357 return (0);
361 * v_pageup -- [count]^B
362 * Page up full screens.
364 * !!!
365 * Historic vi did not move to the SOF if the screen couldn't move, i.e.
366 * if SOF was already displayed on the screen. This implementation does
367 * move to SOF in that case, making ^B more like the the historic ^U.
369 * PUBLIC: int v_pageup __P((SCR *, VICMD *));
372 v_pageup(SCR *sp, VICMD *vp)
374 db_recno_t offset;
377 * !!!
378 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
380 * top_line = top_line - count * (window - 2);
382 * which was historically wrong. The correct one is:
384 * top_line = (top_line - count * window) + 2;
386 * A simpler expression is that, as with ^F, we scroll exactly:
388 * count * window - 2
390 * lines.
392 * Bizarre. As with ^F, an overlap makes no sense for anything
393 * but the first screen. We do it the historical way as there's
394 * no good reason to change it.
396 * If the screen has been split horizontally, use the smaller of
397 * the current window size and the window option value.
399 * It possible for this calculation to be less than 1; move at
400 * least one line.
402 offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_HSPLIT(sp) ?
403 MIN(sp->t_maxrows, O_VAL(sp, O_WINDOW)) : O_VAL(sp, O_WINDOW));
404 offset = offset <= 2 ? 1 : offset - 2;
405 if (vs_sm_scroll(sp, &vp->m_stop, offset, CNTRL_B))
406 return (1);
407 vp->m_final = vp->m_stop;
408 return (0);
412 * v_lineup -- [count]^Y
413 * Page up by lines.
415 * PUBLIC: int v_lineup __P((SCR *, VICMD *));
418 v_lineup(SCR *sp, VICMD *vp)
421 * The cursor moves down, staying with its original line, unless it
422 * reaches the bottom of the screen.
424 if (vs_sm_scroll(sp,
425 &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_Y))
426 return (1);
427 vp->m_final = vp->m_stop;
428 return (0);
432 * v_linedown -- [count]^E
433 * Page down by lines.
435 * PUBLIC: int v_linedown __P((SCR *, VICMD *));
438 v_linedown(SCR *sp, VICMD *vp)
441 * The cursor moves up, staying with its original line, unless it
442 * reaches the top of the screen.
444 if (vs_sm_scroll(sp,
445 &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_E))
446 return (1);
447 vp->m_final = vp->m_stop;
448 return (0);