xref: /dragonfly/contrib/nvi2/vi/v_scroll.c (revision 6e5c5008)
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_scroll.c,v 10.12 2001/06/25 15:19:34 skimo Exp $";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 
25 #include "../common/common.h"
26 #include "vi.h"
27 
28 static void goto_adjust(VICMD *);
29 
30 /*
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.
37  *
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.
48  *
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.
56  *
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.
60  */
61 
62 /*
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.
66  *
67  * PUBLIC: int v_lgoto(SCR *, VICMD *);
68  */
69 int
70 v_lgoto(SCR *sp, VICMD *vp)
71 {
72 	recno_t nlines;
73 
74 	if (F_ISSET(vp, VC_C1SET)) {
75 		if (!db_exist(sp, vp->count)) {
76 			/*
77 			 * !!!
78 			 * Historically, 1G was legal in an empty file.
79 			 */
80 			if (vp->count == 1) {
81 				if (db_last(sp, &nlines))
82 					return (1);
83 				if (nlines == 0)
84 					return (0);
85 			}
86 			v_eof(sp, &vp->m_start);
87 			return (1);
88 		}
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;
94 	}
95 	goto_adjust(vp);
96 	return (0);
97 }
98 
99 /*
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.
103  *
104  * PUBLIC: int v_home(SCR *, VICMD *);
105  */
106 int
107 v_home(SCR *sp, VICMD *vp)
108 {
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);
114 }
115 
116 /*
117  * v_middle -- M
118  *	Move to the first non-blank character of the logical line
119  *	in the middle of the screen.
120  *
121  * PUBLIC: int v_middle(SCR *, VICMD *);
122  */
123 int
124 v_middle(SCR *sp, VICMD *vp)
125 {
126 	/*
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.
130 	 */
131 	if (vs_sm_position(sp, &vp->m_stop, 0, P_MIDDLE))
132 		return (1);
133 	goto_adjust(vp);
134 	return (0);
135 }
136 
137 /*
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.
141  *
142  * PUBLIC: int v_bottom(SCR *, VICMD *);
143  */
144 int
145 v_bottom(SCR *sp, VICMD *vp)
146 {
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);
152 }
153 
154 static void
155 goto_adjust(VICMD *vp)
156 {
157 	/* Guess that it's the end of the range. */
158 	vp->m_final = vp->m_stop;
159 
160 	/*
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.
167 	 *
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.
176 	 *
177 	 * XXX
178 	 * The G command is always first nonblank, so it's okay to reset it.
179 	 */
180 	if (ISMOTION(vp)) {
181 		F_CLR(vp, VM_RCM_MASK);
182 		F_SET(vp, VM_RCM_SETFNB);
183 	} else
184 		return;
185 
186 	/*
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.
191 	 */
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;
199 }
200 
201 /*
202  * v_up -- [count]^P, [count]k, [count]-
203  *	Move up by lines.
204  *
205  * PUBLIC: int v_up(SCR *, VICMD *);
206  */
207 int
208 v_up(SCR *sp, VICMD *vp)
209 {
210 	recno_t lno;
211 
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);
216 	}
217 	vp->m_stop.lno = vp->m_start.lno - lno;
218 	vp->m_final = vp->m_stop;
219 	return (0);
220 }
221 
222 /*
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.
226  *
227  * PUBLIC: int v_cr(SCR *, VICMD *);
228  */
229 int
230 v_cr(SCR *sp, VICMD *vp)
231 {
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));
235 
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));
239 
240 	/* Otherwise, it's the same as v_down(). */
241 	return (v_down(sp, vp));
242 }
243 
244 /*
245  * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
246  *	Move down by lines.
247  *
248  * PUBLIC: int v_down(SCR *, VICMD *);
249  */
250 int
251 v_down(SCR *sp, VICMD *vp)
252 {
253 	recno_t lno;
254 
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);
259 	}
260 	vp->m_stop.lno = lno;
261 	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
262 	return (0);
263 }
264 
265 /*
266  * v_hpageup -- [count]^U
267  *	Page up half screens.
268  *
269  * PUBLIC: int v_hpageup(SCR *, VICMD *);
270  */
271 int
272 v_hpageup(SCR *sp, VICMD *vp)
273 {
274 	/*
275 	 * Half screens always succeed unless already at SOF.
276 	 *
277 	 * !!!
278 	 * Half screens set the scroll value, even if the command
279 	 * ultimately failed, in historic vi.  Probably a don't care.
280 	 */
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);
287 }
288 
289 /*
290  * v_hpagedown -- [count]^D
291  *	Page down half screens.
292  *
293  * PUBLIC: int v_hpagedown(SCR *, VICMD *);
294  */
295 int
296 v_hpagedown(SCR *sp, VICMD *vp)
297 {
298 	/*
299 	 * Half screens always succeed unless already at EOF.
300 	 *
301 	 * !!!
302 	 * Half screens set the scroll value, even if the command
303 	 * ultimately failed, in historic vi.  Probably a don't care.
304 	 */
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);
311 }
312 
313 /*
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.
320  *
321  * PUBLIC: int v_pagedown(SCR *, VICMD *);
322  */
323 int
324 v_pagedown(SCR *sp, VICMD *vp)
325 {
326 	recno_t offset;
327 
328 	/*
329 	 * !!!
330 	 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
331 	 *
332 	 *	top_line = top_line + count * (window - 2);
333 	 *
334 	 * which was historically wrong.  The correct one is:
335 	 *
336 	 *	top_line = top_line + count * window - 2;
337 	 *
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.
342 	 *
343 	 * If the screen has been split horizontally, use the smaller of
344 	 * the current window size and the window option value.
345 	 *
346 	 * It possible for this calculation to be less than 1; move at
347 	 * least one line.
348 	 */
349 	offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_SPLIT(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);
356 }
357 
358 /*
359  * v_pageup -- [count]^B
360  *	Page up full screens.
361  *
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.
366  *
367  * PUBLIC: int v_pageup(SCR *, VICMD *);
368  */
369 int
370 v_pageup(SCR *sp, VICMD *vp)
371 {
372 	recno_t offset;
373 
374 	/*
375 	 * !!!
376 	 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
377 	 *
378 	 *	top_line = top_line - count * (window - 2);
379 	 *
380 	 * which was historically wrong.  The correct one is:
381 	 *
382 	 *	top_line = (top_line - count * window) + 2;
383 	 *
384 	 * A simpler expression is that, as with ^F, we scroll exactly:
385 	 *
386 	 *	count * window - 2
387 	 *
388 	 * lines.
389 	 *
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.
393 	 *
394 	 * If the screen has been split horizontally, use the smaller of
395 	 * the current window size and the window option value.
396 	 *
397 	 * It possible for this calculation to be less than 1; move at
398 	 * least one line.
399 	 */
400 	offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_SPLIT(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);
407 }
408 
409 /*
410  * v_lineup -- [count]^Y
411  *	Page up by lines.
412  *
413  * PUBLIC: int v_lineup(SCR *, VICMD *);
414  */
415 int
416 v_lineup(SCR *sp, VICMD *vp)
417 {
418 	/*
419 	 * The cursor moves down, staying with its original line, unless it
420 	 * reaches the bottom of the screen.
421 	 */
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);
427 }
428 
429 /*
430  * v_linedown -- [count]^E
431  *	Page down by lines.
432  *
433  * PUBLIC: int v_linedown(SCR *, VICMD *);
434  */
435 int
436 v_linedown(SCR *sp, VICMD *vp)
437 {
438 	/*
439 	 * The cursor moves up, staying with its original line, unless it
440 	 * reaches the top of the screen.
441 	 */
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);
447 }
448