xref: /freebsd/contrib/nvi/vi/v_scroll.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15 
16 #include <bitstring.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <stdio.h>
20 
21 #include "../common/common.h"
22 #include "vi.h"
23 
24 static void goto_adjust(VICMD *);
25 
26 /*
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.
33  *
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.
44  *
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.
52  *
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.
56  */
57 
58 /*
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.
62  *
63  * PUBLIC: int v_lgoto(SCR *, VICMD *);
64  */
65 int
66 v_lgoto(SCR *sp, VICMD *vp)
67 {
68 	recno_t nlines;
69 
70 	if (F_ISSET(vp, VC_C1SET)) {
71 		if (!db_exist(sp, vp->count)) {
72 			/*
73 			 * !!!
74 			 * Historically, 1G was legal in an empty file.
75 			 */
76 			if (vp->count == 1) {
77 				if (db_last(sp, &nlines))
78 					return (1);
79 				if (nlines == 0)
80 					return (0);
81 			}
82 			v_eof(sp, &vp->m_start);
83 			return (1);
84 		}
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;
90 	}
91 	goto_adjust(vp);
92 	return (0);
93 }
94 
95 /*
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.
99  *
100  * PUBLIC: int v_home(SCR *, VICMD *);
101  */
102 int
103 v_home(SCR *sp, VICMD *vp)
104 {
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);
110 }
111 
112 /*
113  * v_middle -- M
114  *	Move to the first non-blank character of the logical line
115  *	in the middle of the screen.
116  *
117  * PUBLIC: int v_middle(SCR *, VICMD *);
118  */
119 int
120 v_middle(SCR *sp, VICMD *vp)
121 {
122 	/*
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.
126 	 */
127 	if (vs_sm_position(sp, &vp->m_stop, 0, P_MIDDLE))
128 		return (1);
129 	goto_adjust(vp);
130 	return (0);
131 }
132 
133 /*
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.
137  *
138  * PUBLIC: int v_bottom(SCR *, VICMD *);
139  */
140 int
141 v_bottom(SCR *sp, VICMD *vp)
142 {
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);
148 }
149 
150 static void
151 goto_adjust(VICMD *vp)
152 {
153 	/* Guess that it's the end of the range. */
154 	vp->m_final = vp->m_stop;
155 
156 	/*
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.
163 	 *
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.
172 	 *
173 	 * XXX
174 	 * The G command is always first nonblank, so it's okay to reset it.
175 	 */
176 	if (ISMOTION(vp)) {
177 		F_CLR(vp, VM_RCM_MASK);
178 		F_SET(vp, VM_RCM_SETFNB);
179 	} else
180 		return;
181 
182 	/*
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.
187 	 */
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;
195 }
196 
197 /*
198  * v_up -- [count]^P, [count]k, [count]-
199  *	Move up by lines.
200  *
201  * PUBLIC: int v_up(SCR *, VICMD *);
202  */
203 int
204 v_up(SCR *sp, VICMD *vp)
205 {
206 	recno_t lno;
207 
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);
212 	}
213 	vp->m_stop.lno = vp->m_start.lno - lno;
214 	vp->m_final = vp->m_stop;
215 	return (0);
216 }
217 
218 /*
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.
222  *
223  * PUBLIC: int v_cr(SCR *, VICMD *);
224  */
225 int
226 v_cr(SCR *sp, VICMD *vp)
227 {
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));
231 
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));
235 
236 	/* Otherwise, it's the same as v_down(). */
237 	return (v_down(sp, vp));
238 }
239 
240 /*
241  * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
242  *	Move down by lines.
243  *
244  * PUBLIC: int v_down(SCR *, VICMD *);
245  */
246 int
247 v_down(SCR *sp, VICMD *vp)
248 {
249 	recno_t lno;
250 
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);
255 	}
256 	vp->m_stop.lno = lno;
257 	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
258 	return (0);
259 }
260 
261 /*
262  * v_hpageup -- [count]^U
263  *	Page up half screens.
264  *
265  * PUBLIC: int v_hpageup(SCR *, VICMD *);
266  */
267 int
268 v_hpageup(SCR *sp, VICMD *vp)
269 {
270 	/*
271 	 * Half screens always succeed unless already at SOF.
272 	 *
273 	 * !!!
274 	 * Half screens set the scroll value, even if the command
275 	 * ultimately failed, in historic vi.  Probably a don't care.
276 	 */
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);
283 }
284 
285 /*
286  * v_hpagedown -- [count]^D
287  *	Page down half screens.
288  *
289  * PUBLIC: int v_hpagedown(SCR *, VICMD *);
290  */
291 int
292 v_hpagedown(SCR *sp, VICMD *vp)
293 {
294 	/*
295 	 * Half screens always succeed unless already at EOF.
296 	 *
297 	 * !!!
298 	 * Half screens set the scroll value, even if the command
299 	 * ultimately failed, in historic vi.  Probably a don't care.
300 	 */
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);
307 }
308 
309 /*
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.
316  *
317  * PUBLIC: int v_pagedown(SCR *, VICMD *);
318  */
319 int
320 v_pagedown(SCR *sp, VICMD *vp)
321 {
322 	recno_t offset;
323 
324 	/*
325 	 * !!!
326 	 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
327 	 *
328 	 *	top_line = top_line + count * (window - 2);
329 	 *
330 	 * which was historically wrong.  The correct one is:
331 	 *
332 	 *	top_line = top_line + count * window - 2;
333 	 *
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.
338 	 *
339 	 * If the screen has been split horizontally, use the smaller of
340 	 * the current window size and the window option value.
341 	 *
342 	 * It possible for this calculation to be less than 1; move at
343 	 * least one line.
344 	 */
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);
352 }
353 
354 /*
355  * v_pageup -- [count]^B
356  *	Page up full screens.
357  *
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.
362  *
363  * PUBLIC: int v_pageup(SCR *, VICMD *);
364  */
365 int
366 v_pageup(SCR *sp, VICMD *vp)
367 {
368 	recno_t offset;
369 
370 	/*
371 	 * !!!
372 	 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
373 	 *
374 	 *	top_line = top_line - count * (window - 2);
375 	 *
376 	 * which was historically wrong.  The correct one is:
377 	 *
378 	 *	top_line = (top_line - count * window) + 2;
379 	 *
380 	 * A simpler expression is that, as with ^F, we scroll exactly:
381 	 *
382 	 *	count * window - 2
383 	 *
384 	 * lines.
385 	 *
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.
389 	 *
390 	 * If the screen has been split horizontally, use the smaller of
391 	 * the current window size and the window option value.
392 	 *
393 	 * It possible for this calculation to be less than 1; move at
394 	 * least one line.
395 	 */
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);
403 }
404 
405 /*
406  * v_lineup -- [count]^Y
407  *	Page up by lines.
408  *
409  * PUBLIC: int v_lineup(SCR *, VICMD *);
410  */
411 int
412 v_lineup(SCR *sp, VICMD *vp)
413 {
414 	/*
415 	 * The cursor moves down, staying with its original line, unless it
416 	 * reaches the bottom of the screen.
417 	 */
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);
423 }
424 
425 /*
426  * v_linedown -- [count]^E
427  *	Page down by lines.
428  *
429  * PUBLIC: int v_linedown(SCR *, VICMD *);
430  */
431 int
432 v_linedown(SCR *sp, VICMD *vp)
433 {
434 	/*
435 	 * The cursor moves up, staying with its original line, unless it
436 	 * reaches the top of the screen.
437 	 */
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);
443 }
444