tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / vi / v_scroll.c
blob0f25cb389f891582f9483f6cfbe75ab8c9ed406e
1 /* $NetBSD: v_scroll.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
2 /*-
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.
9 */
11 #include "config.h"
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
26 #include "../common/common.h"
27 #include "vi.h"
29 static void goto_adjust __P((VICMD *));
32 * The historic vi had a problem in that all movements were by physical
33 * lines, not by logical, or screen lines. Arguments can be made that this
34 * is the right thing to do. For example, single line movements, such as
35 * 'j' or 'k', should probably work on physical lines. Commands like "dj",
36 * or "j.", where '.' is a change command, make more sense for physical lines
37 * than they do for logical lines.
39 * These arguments, however, don't apply to scrolling commands like ^D and
40 * ^F -- if the window is fairly small, using physical lines can result in
41 * a half-page scroll repainting the entire screen, which is not what the
42 * user wanted. Second, if the line is larger than the screen, using physical
43 * lines can make it impossible to display parts of the line -- there aren't
44 * any commands that don't display the beginning of the line in historic vi,
45 * and if both the beginning and end of the line can't be on the screen at
46 * the same time, you lose. This is even worse in the case of the H, L, and
47 * M commands -- for large lines, they may all refer to the same line and
48 * will result in no movement at all.
50 * Another issue is that page and half-page scrolling commands historically
51 * moved to the first non-blank character in the new line. If the line is
52 * approximately the same size as the screen, this loses because the cursor
53 * before and after a ^D, may refer to the same location on the screen. In
54 * this implementation, scrolling commands set the cursor to the first non-
55 * blank character if the line changes because of the scroll. Otherwise,
56 * the cursor is left alone.
58 * This implementation does the scrolling (^B, ^D, ^F, ^U, ^Y, ^E), and the
59 * cursor positioning commands (H, L, M) commands using logical lines, not
60 * physical.
64 * v_lgoto -- [count]G
65 * Go to first non-blank character of the line count, the last line
66 * of the file by default.
68 * PUBLIC: int v_lgoto __P((SCR *, VICMD *));
70 int
71 v_lgoto(SCR *sp, VICMD *vp)
73 db_recno_t nlines;
75 if (F_ISSET(vp, VC_C1SET)) {
76 if (!db_exist(sp, vp->count)) {
78 * !!!
79 * Historically, 1G was legal in an empty file.
81 if (vp->count == 1) {
82 if (db_last(sp, &nlines))
83 return (1);
84 if (nlines == 0)
85 return (0);
87 v_eof(sp, &vp->m_start);
88 return (1);
90 vp->m_stop.lno = vp->count;
91 } else {
92 if (db_last(sp, &nlines))
93 return (1);
94 vp->m_stop.lno = nlines ? nlines : 1;
96 goto_adjust(vp);
97 return (0);
101 * v_home -- [count]H
102 * Move to the first non-blank character of the logical line
103 * count - 1 from the top of the screen, 0 by default.
105 * PUBLIC: int v_home __P((SCR *, VICMD *));
108 v_home(SCR *sp, VICMD *vp)
110 if (vs_sm_position(sp, &vp->m_stop,
111 F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0, P_TOP))
112 return (1);
113 goto_adjust(vp);
114 return (0);
118 * v_middle -- M
119 * Move to the first non-blank character of the logical line
120 * in the middle of the screen.
122 * PUBLIC: int v_middle __P((SCR *, VICMD *));
125 v_middle(SCR *sp, VICMD *vp)
128 * Yielding to none in our quest for compatibility with every
129 * historical blemish of vi, no matter how strange it might be,
130 * we permit the user to enter a count and then ignore it.
132 if (vs_sm_position(sp, &vp->m_stop, 0, P_MIDDLE))
133 return (1);
134 goto_adjust(vp);
135 return (0);
139 * v_bottom -- [count]L
140 * Move to the first non-blank character of the logical line
141 * count - 1 from the bottom of the screen, 0 by default.
143 * PUBLIC: int v_bottom __P((SCR *, VICMD *));
146 v_bottom(SCR *sp, VICMD *vp)
148 if (vs_sm_position(sp, &vp->m_stop,
149 F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0, P_BOTTOM))
150 return (1);
151 goto_adjust(vp);
152 return (0);
155 static void
156 goto_adjust(VICMD *vp)
158 /* Guess that it's the end of the range. */
159 vp->m_final = vp->m_stop;
162 * Non-motion commands move the cursor to the end of the range, and
163 * then to the NEXT nonblank of the line. Historic vi always moved
164 * to the first nonblank in the line; since the H, M, and L commands
165 * are logical motions in this implementation, we do the next nonblank
166 * so that it looks approximately the same to the user. To make this
167 * happen, the VM_RCM_SETNNB flag is set in the vcmd.c command table.
169 * If it's a motion, it's more complicated. The best possible solution
170 * is probably to display the first nonblank of the line the cursor
171 * will eventually rest on. This is tricky, particularly given that if
172 * the associated command is a delete, we don't yet know what line that
173 * will be. So, we clear the VM_RCM_SETNNB flag, and set the first
174 * nonblank flag (VM_RCM_SETFNB). Note, if the lines are sufficiently
175 * long, this can cause the cursor to warp out of the screen. It's too
176 * hard to fix.
178 * XXX
179 * The G command is always first nonblank, so it's okay to reset it.
181 if (ISMOTION(vp)) {
182 F_CLR(vp, VM_RCM_MASK);
183 F_SET(vp, VM_RCM_SETFNB);
184 } else
185 return;
188 * If moving backward in the file, delete and yank move to the end
189 * of the range, unless the line didn't change, in which case yank
190 * doesn't move. If moving forward in the file, delete and yank
191 * stay at the start of the range. Ignore others.
193 if (vp->m_stop.lno < vp->m_start.lno ||
194 (vp->m_stop.lno == vp->m_start.lno &&
195 vp->m_stop.cno < vp->m_start.cno)) {
196 if (ISCMD(vp->rkp, 'y') && vp->m_stop.lno == vp->m_start.lno)
197 vp->m_final = vp->m_start;
198 } else
199 vp->m_final = vp->m_start;
203 * v_up -- [count]^P, [count]k, [count]-
204 * Move up by lines.
206 * PUBLIC: int v_up __P((SCR *, VICMD *));
209 v_up(SCR *sp, VICMD *vp)
211 db_recno_t lno;
213 lno = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
214 if (vp->m_start.lno <= lno) {
215 v_sof(sp, &vp->m_start);
216 return (1);
218 vp->m_stop.lno = vp->m_start.lno - lno;
219 vp->m_final = vp->m_stop;
220 return (0);
224 * v_cr -- [count]^M
225 * In a script window, send the line to the shell.
226 * In a regular window, move down by lines.
228 * PUBLIC: int v_cr __P((SCR *, VICMD *));
231 v_cr(SCR *sp, VICMD *vp)
233 /* If it's a colon command-line edit window, it's an ex command. */
234 if (F_ISSET(sp, SC_COMEDIT))
235 return (v_ecl_exec(sp));
237 /* If it's a script window, exec the line. */
238 if (F_ISSET(sp, SC_SCRIPT))
239 return (sscr_exec(sp, vp->m_start.lno));
241 /* Otherwise, it's the same as v_down(). */
242 return (v_down(sp, vp));
246 * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
247 * Move down by lines.
249 * PUBLIC: int v_down __P((SCR *, VICMD *));
252 v_down(SCR *sp, VICMD *vp)
254 db_recno_t lno;
256 lno = vp->m_start.lno + (F_ISSET(vp, VC_C1SET) ? vp->count : 1);
257 if (!db_exist(sp, lno)) {
258 v_eof(sp, &vp->m_start);
259 return (1);
261 vp->m_stop.lno = lno;
262 vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
263 return (0);
267 * v_hpageup -- [count]^U
268 * Page up half screens.
270 * PUBLIC: int v_hpageup __P((SCR *, VICMD *));
273 v_hpageup(SCR *sp, VICMD *vp)
276 * Half screens always succeed unless already at SOF.
278 * !!!
279 * Half screens set the scroll value, even if the command
280 * ultimately failed, in historic vi. Probably a don't care.
282 if (F_ISSET(vp, VC_C1SET))
283 sp->defscroll = vp->count;
284 if (vs_sm_scroll(sp, &vp->m_stop, sp->defscroll, CNTRL_U))
285 return (1);
286 vp->m_final = vp->m_stop;
287 return (0);
291 * v_hpagedown -- [count]^D
292 * Page down half screens.
294 * PUBLIC: int v_hpagedown __P((SCR *, VICMD *));
297 v_hpagedown(SCR *sp, VICMD *vp)
300 * Half screens always succeed unless already at EOF.
302 * !!!
303 * Half screens set the scroll value, even if the command
304 * ultimately failed, in historic vi. Probably a don't care.
306 if (F_ISSET(vp, VC_C1SET))
307 sp->defscroll = vp->count;
308 if (vs_sm_scroll(sp, &vp->m_stop, sp->defscroll, CNTRL_D))
309 return (1);
310 vp->m_final = vp->m_stop;
311 return (0);
315 * v_pagedown -- [count]^F
316 * Page down full screens.
317 * !!!
318 * Historic vi did not move to the EOF if the screen couldn't move, i.e.
319 * if EOF was already displayed on the screen. This implementation does
320 * move to EOF in that case, making ^F more like the the historic ^D.
322 * PUBLIC: int v_pagedown __P((SCR *, VICMD *));
325 v_pagedown(SCR *sp, VICMD *vp)
327 db_recno_t offset;
330 * !!!
331 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
333 * top_line = top_line + count * (window - 2);
335 * which was historically wrong. The correct one is:
337 * top_line = top_line + count * window - 2;
339 * i.e. the two line "overlap" was only subtracted once. Which
340 * makes no sense, but then again, an overlap makes no sense for
341 * any screen but the "next" one anyway. We do it the historical
342 * way as there's no good reason to change it.
344 * If the screen has been split horizontally, use the smaller of
345 * the current window size and the window option value.
347 * It possible for this calculation to be less than 1; move at
348 * least one line.
350 offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_HSPLIT(sp) ?
351 MIN(sp->t_maxrows, O_VAL(sp, O_WINDOW)) : O_VAL(sp, O_WINDOW));
352 offset = offset <= 2 ? 1 : offset - 2;
353 if (vs_sm_scroll(sp, &vp->m_stop, offset, CNTRL_F))
354 return (1);
355 vp->m_final = vp->m_stop;
356 return (0);
360 * v_pageup -- [count]^B
361 * Page up full screens.
363 * !!!
364 * Historic vi did not move to the SOF if the screen couldn't move, i.e.
365 * if SOF was already displayed on the screen. This implementation does
366 * move to SOF in that case, making ^B more like the the historic ^U.
368 * PUBLIC: int v_pageup __P((SCR *, VICMD *));
371 v_pageup(SCR *sp, VICMD *vp)
373 db_recno_t offset;
376 * !!!
377 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
379 * top_line = top_line - count * (window - 2);
381 * which was historically wrong. The correct one is:
383 * top_line = (top_line - count * window) + 2;
385 * A simpler expression is that, as with ^F, we scroll exactly:
387 * count * window - 2
389 * lines.
391 * Bizarre. As with ^F, an overlap makes no sense for anything
392 * but the first screen. We do it the historical way as there's
393 * no good reason to change it.
395 * If the screen has been split horizontally, use the smaller of
396 * the current window size and the window option value.
398 * It possible for this calculation to be less than 1; move at
399 * least one line.
401 offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_HSPLIT(sp) ?
402 MIN(sp->t_maxrows, O_VAL(sp, O_WINDOW)) : O_VAL(sp, O_WINDOW));
403 offset = offset <= 2 ? 1 : offset - 2;
404 if (vs_sm_scroll(sp, &vp->m_stop, offset, CNTRL_B))
405 return (1);
406 vp->m_final = vp->m_stop;
407 return (0);
411 * v_lineup -- [count]^Y
412 * Page up by lines.
414 * PUBLIC: int v_lineup __P((SCR *, VICMD *));
417 v_lineup(SCR *sp, VICMD *vp)
420 * The cursor moves down, staying with its original line, unless it
421 * reaches the bottom of the screen.
423 if (vs_sm_scroll(sp,
424 &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_Y))
425 return (1);
426 vp->m_final = vp->m_stop;
427 return (0);
431 * v_linedown -- [count]^E
432 * Page down by lines.
434 * PUBLIC: int v_linedown __P((SCR *, VICMD *));
437 v_linedown(SCR *sp, VICMD *vp)
440 * The cursor moves up, staying with its original line, unless it
441 * reaches the top of the screen.
443 if (vs_sm_scroll(sp,
444 &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_E))
445 return (1);
446 vp->m_final = vp->m_stop;
447 return (0);