xref: /dflybsd-src/contrib/nvi2/vi/v_scroll.c (revision 07bc39c2f4bbca56f12568e06d89da17f2eeb965)
1e0b8e63eSJohn Marino /*-
2e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994
3e0b8e63eSJohn Marino  *	The Regents of the University of California.  All rights reserved.
4e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5e0b8e63eSJohn Marino  *	Keith Bostic.  All rights reserved.
6e0b8e63eSJohn Marino  *
7e0b8e63eSJohn Marino  * See the LICENSE file for redistribution information.
8e0b8e63eSJohn Marino  */
9e0b8e63eSJohn Marino 
10e0b8e63eSJohn Marino #include "config.h"
11e0b8e63eSJohn Marino 
12e0b8e63eSJohn Marino #include <sys/types.h>
13e0b8e63eSJohn Marino #include <sys/queue.h>
14e0b8e63eSJohn Marino #include <sys/time.h>
15e0b8e63eSJohn Marino 
16e0b8e63eSJohn Marino #include <bitstring.h>
17e0b8e63eSJohn Marino #include <errno.h>
18e0b8e63eSJohn Marino #include <limits.h>
19e0b8e63eSJohn Marino #include <stdio.h>
20e0b8e63eSJohn Marino 
21e0b8e63eSJohn Marino #include "../common/common.h"
22e0b8e63eSJohn Marino #include "vi.h"
23e0b8e63eSJohn Marino 
24e0b8e63eSJohn Marino static void goto_adjust(VICMD *);
25e0b8e63eSJohn Marino 
26e0b8e63eSJohn Marino /*
27e0b8e63eSJohn Marino  * The historic vi had a problem in that all movements were by physical
28e0b8e63eSJohn Marino  * lines, not by logical, or screen lines.  Arguments can be made that this
29e0b8e63eSJohn Marino  * is the right thing to do.  For example, single line movements, such as
30e0b8e63eSJohn Marino  * 'j' or 'k', should probably work on physical lines.  Commands like "dj",
31e0b8e63eSJohn Marino  * or "j.", where '.' is a change command, make more sense for physical lines
32e0b8e63eSJohn Marino  * than they do for logical lines.
33e0b8e63eSJohn Marino  *
34e0b8e63eSJohn Marino  * These arguments, however, don't apply to scrolling commands like ^D and
35e0b8e63eSJohn Marino  * ^F -- if the window is fairly small, using physical lines can result in
36e0b8e63eSJohn Marino  * a half-page scroll repainting the entire screen, which is not what the
37e0b8e63eSJohn Marino  * user wanted.  Second, if the line is larger than the screen, using physical
38e0b8e63eSJohn Marino  * lines can make it impossible to display parts of the line -- there aren't
39e0b8e63eSJohn Marino  * any commands that don't display the beginning of the line in historic vi,
40e0b8e63eSJohn Marino  * and if both the beginning and end of the line can't be on the screen at
41e0b8e63eSJohn Marino  * the same time, you lose.  This is even worse in the case of the H, L, and
42e0b8e63eSJohn Marino  * M commands -- for large lines, they may all refer to the same line and
43e0b8e63eSJohn Marino  * will result in no movement at all.
44e0b8e63eSJohn Marino  *
45e0b8e63eSJohn Marino  * Another issue is that page and half-page scrolling commands historically
46e0b8e63eSJohn Marino  * moved to the first non-blank character in the new line.  If the line is
47e0b8e63eSJohn Marino  * approximately the same size as the screen, this loses because the cursor
48e0b8e63eSJohn Marino  * before and after a ^D, may refer to the same location on the screen.  In
49e0b8e63eSJohn Marino  * this implementation, scrolling commands set the cursor to the first non-
50e0b8e63eSJohn Marino  * blank character if the line changes because of the scroll.  Otherwise,
51e0b8e63eSJohn Marino  * the cursor is left alone.
52e0b8e63eSJohn Marino  *
53e0b8e63eSJohn Marino  * This implementation does the scrolling (^B, ^D, ^F, ^U, ^Y, ^E), and the
54e0b8e63eSJohn Marino  * cursor positioning commands (H, L, M) commands using logical lines, not
55e0b8e63eSJohn Marino  * physical.
56e0b8e63eSJohn Marino  */
57e0b8e63eSJohn Marino 
58e0b8e63eSJohn Marino /*
59e0b8e63eSJohn Marino  * v_lgoto -- [count]G
60e0b8e63eSJohn Marino  *	Go to first non-blank character of the line count, the last line
61e0b8e63eSJohn Marino  *	of the file by default.
62e0b8e63eSJohn Marino  *
63e0b8e63eSJohn Marino  * PUBLIC: int v_lgoto(SCR *, VICMD *);
64e0b8e63eSJohn Marino  */
65e0b8e63eSJohn Marino int
v_lgoto(SCR * sp,VICMD * vp)66e0b8e63eSJohn Marino v_lgoto(SCR *sp, VICMD *vp)
67e0b8e63eSJohn Marino {
68e0b8e63eSJohn Marino 	recno_t nlines;
69e0b8e63eSJohn Marino 
70e0b8e63eSJohn Marino 	if (F_ISSET(vp, VC_C1SET)) {
71e0b8e63eSJohn Marino 		if (!db_exist(sp, vp->count)) {
72e0b8e63eSJohn Marino 			/*
73e0b8e63eSJohn Marino 			 * !!!
74e0b8e63eSJohn Marino 			 * Historically, 1G was legal in an empty file.
75e0b8e63eSJohn Marino 			 */
76e0b8e63eSJohn Marino 			if (vp->count == 1) {
77e0b8e63eSJohn Marino 				if (db_last(sp, &nlines))
78e0b8e63eSJohn Marino 					return (1);
79e0b8e63eSJohn Marino 				if (nlines == 0)
80e0b8e63eSJohn Marino 					return (0);
81e0b8e63eSJohn Marino 			}
82e0b8e63eSJohn Marino 			v_eof(sp, &vp->m_start);
83e0b8e63eSJohn Marino 			return (1);
84e0b8e63eSJohn Marino 		}
85e0b8e63eSJohn Marino 		vp->m_stop.lno = vp->count;
86e0b8e63eSJohn Marino 	} else {
87e0b8e63eSJohn Marino 		if (db_last(sp, &nlines))
88e0b8e63eSJohn Marino 			return (1);
89e0b8e63eSJohn Marino 		vp->m_stop.lno = nlines ? nlines : 1;
90e0b8e63eSJohn Marino 	}
91e0b8e63eSJohn Marino 	goto_adjust(vp);
92e0b8e63eSJohn Marino 	return (0);
93e0b8e63eSJohn Marino }
94e0b8e63eSJohn Marino 
95e0b8e63eSJohn Marino /*
96e0b8e63eSJohn Marino  * v_home -- [count]H
97e0b8e63eSJohn Marino  *	Move to the first non-blank character of the logical line
98e0b8e63eSJohn Marino  *	count - 1 from the top of the screen, 0 by default.
99e0b8e63eSJohn Marino  *
100e0b8e63eSJohn Marino  * PUBLIC: int v_home(SCR *, VICMD *);
101e0b8e63eSJohn Marino  */
102e0b8e63eSJohn Marino int
v_home(SCR * sp,VICMD * vp)103e0b8e63eSJohn Marino v_home(SCR *sp, VICMD *vp)
104e0b8e63eSJohn Marino {
105e0b8e63eSJohn Marino 	if (vs_sm_position(sp, &vp->m_stop,
106e0b8e63eSJohn Marino 	    F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0, P_TOP))
107e0b8e63eSJohn Marino 		return (1);
108e0b8e63eSJohn Marino 	goto_adjust(vp);
109e0b8e63eSJohn Marino 	return (0);
110e0b8e63eSJohn Marino }
111e0b8e63eSJohn Marino 
112e0b8e63eSJohn Marino /*
113e0b8e63eSJohn Marino  * v_middle -- M
114e0b8e63eSJohn Marino  *	Move to the first non-blank character of the logical line
115e0b8e63eSJohn Marino  *	in the middle of the screen.
116e0b8e63eSJohn Marino  *
117e0b8e63eSJohn Marino  * PUBLIC: int v_middle(SCR *, VICMD *);
118e0b8e63eSJohn Marino  */
119e0b8e63eSJohn Marino int
v_middle(SCR * sp,VICMD * vp)120e0b8e63eSJohn Marino v_middle(SCR *sp, VICMD *vp)
121e0b8e63eSJohn Marino {
122e0b8e63eSJohn Marino 	/*
123e0b8e63eSJohn Marino 	 * Yielding to none in our quest for compatibility with every
124e0b8e63eSJohn Marino 	 * historical blemish of vi, no matter how strange it might be,
125e0b8e63eSJohn Marino 	 * we permit the user to enter a count and then ignore it.
126e0b8e63eSJohn Marino 	 */
127e0b8e63eSJohn Marino 	if (vs_sm_position(sp, &vp->m_stop, 0, P_MIDDLE))
128e0b8e63eSJohn Marino 		return (1);
129e0b8e63eSJohn Marino 	goto_adjust(vp);
130e0b8e63eSJohn Marino 	return (0);
131e0b8e63eSJohn Marino }
132e0b8e63eSJohn Marino 
133e0b8e63eSJohn Marino /*
134e0b8e63eSJohn Marino  * v_bottom -- [count]L
135e0b8e63eSJohn Marino  *	Move to the first non-blank character of the logical line
136e0b8e63eSJohn Marino  *	count - 1 from the bottom of the screen, 0 by default.
137e0b8e63eSJohn Marino  *
138e0b8e63eSJohn Marino  * PUBLIC: int v_bottom(SCR *, VICMD *);
139e0b8e63eSJohn Marino  */
140e0b8e63eSJohn Marino int
v_bottom(SCR * sp,VICMD * vp)141e0b8e63eSJohn Marino v_bottom(SCR *sp, VICMD *vp)
142e0b8e63eSJohn Marino {
143e0b8e63eSJohn Marino 	if (vs_sm_position(sp, &vp->m_stop,
144e0b8e63eSJohn Marino 	    F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0, P_BOTTOM))
145e0b8e63eSJohn Marino 		return (1);
146e0b8e63eSJohn Marino 	goto_adjust(vp);
147e0b8e63eSJohn Marino 	return (0);
148e0b8e63eSJohn Marino }
149e0b8e63eSJohn Marino 
150e0b8e63eSJohn Marino static void
goto_adjust(VICMD * vp)151e0b8e63eSJohn Marino goto_adjust(VICMD *vp)
152e0b8e63eSJohn Marino {
153e0b8e63eSJohn Marino 	/* Guess that it's the end of the range. */
154e0b8e63eSJohn Marino 	vp->m_final = vp->m_stop;
155e0b8e63eSJohn Marino 
156e0b8e63eSJohn Marino 	/*
157e0b8e63eSJohn Marino 	 * Non-motion commands move the cursor to the end of the range, and
158e0b8e63eSJohn Marino 	 * then to the NEXT nonblank of the line.  Historic vi always moved
159e0b8e63eSJohn Marino 	 * to the first nonblank in the line; since the H, M, and L commands
160e0b8e63eSJohn Marino 	 * are logical motions in this implementation, we do the next nonblank
161e0b8e63eSJohn Marino 	 * so that it looks approximately the same to the user.  To make this
162e0b8e63eSJohn Marino 	 * happen, the VM_RCM_SETNNB flag is set in the vcmd.c command table.
163e0b8e63eSJohn Marino 	 *
164e0b8e63eSJohn Marino 	 * If it's a motion, it's more complicated.  The best possible solution
165e0b8e63eSJohn Marino 	 * is probably to display the first nonblank of the line the cursor
166e0b8e63eSJohn Marino 	 * will eventually rest on.  This is tricky, particularly given that if
167e0b8e63eSJohn Marino 	 * the associated command is a delete, we don't yet know what line that
168e0b8e63eSJohn Marino 	 * will be.  So, we clear the VM_RCM_SETNNB flag, and set the first
169e0b8e63eSJohn Marino 	 * nonblank flag (VM_RCM_SETFNB).  Note, if the lines are sufficiently
170e0b8e63eSJohn Marino 	 * long, this can cause the cursor to warp out of the screen.  It's too
171e0b8e63eSJohn Marino 	 * hard to fix.
172e0b8e63eSJohn Marino 	 *
173e0b8e63eSJohn Marino 	 * XXX
174e0b8e63eSJohn Marino 	 * The G command is always first nonblank, so it's okay to reset it.
175e0b8e63eSJohn Marino 	 */
176e0b8e63eSJohn Marino 	if (ISMOTION(vp)) {
177e0b8e63eSJohn Marino 		F_CLR(vp, VM_RCM_MASK);
178e0b8e63eSJohn Marino 		F_SET(vp, VM_RCM_SETFNB);
179e0b8e63eSJohn Marino 	} else
180e0b8e63eSJohn Marino 		return;
181e0b8e63eSJohn Marino 
182e0b8e63eSJohn Marino 	/*
183e0b8e63eSJohn Marino 	 * If moving backward in the file, delete and yank move to the end
184e0b8e63eSJohn Marino 	 * of the range, unless the line didn't change, in which case yank
185e0b8e63eSJohn Marino 	 * doesn't move.  If moving forward in the file, delete and yank
186e0b8e63eSJohn Marino 	 * stay at the start of the range.  Ignore others.
187e0b8e63eSJohn Marino 	 */
188e0b8e63eSJohn Marino 	if (vp->m_stop.lno < vp->m_start.lno ||
189e0b8e63eSJohn Marino 	    (vp->m_stop.lno == vp->m_start.lno &&
190e0b8e63eSJohn Marino 	    vp->m_stop.cno < vp->m_start.cno)) {
191e0b8e63eSJohn Marino 		if (ISCMD(vp->rkp, 'y') && vp->m_stop.lno == vp->m_start.lno)
192e0b8e63eSJohn Marino 			vp->m_final = vp->m_start;
193e0b8e63eSJohn Marino 	} else
194e0b8e63eSJohn Marino 		vp->m_final = vp->m_start;
195e0b8e63eSJohn Marino }
196e0b8e63eSJohn Marino 
197e0b8e63eSJohn Marino /*
198e0b8e63eSJohn Marino  * v_up -- [count]^P, [count]k, [count]-
199e0b8e63eSJohn Marino  *	Move up by lines.
200e0b8e63eSJohn Marino  *
201e0b8e63eSJohn Marino  * PUBLIC: int v_up(SCR *, VICMD *);
202e0b8e63eSJohn Marino  */
203e0b8e63eSJohn Marino int
v_up(SCR * sp,VICMD * vp)204e0b8e63eSJohn Marino v_up(SCR *sp, VICMD *vp)
205e0b8e63eSJohn Marino {
206e0b8e63eSJohn Marino 	recno_t lno;
207e0b8e63eSJohn Marino 
208e0b8e63eSJohn Marino 	lno = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
209e0b8e63eSJohn Marino 	if (vp->m_start.lno <= lno) {
210e0b8e63eSJohn Marino 		v_sof(sp, &vp->m_start);
211e0b8e63eSJohn Marino 		return (1);
212e0b8e63eSJohn Marino 	}
213e0b8e63eSJohn Marino 	vp->m_stop.lno = vp->m_start.lno - lno;
214e0b8e63eSJohn Marino 	vp->m_final = vp->m_stop;
215e0b8e63eSJohn Marino 	return (0);
216e0b8e63eSJohn Marino }
217e0b8e63eSJohn Marino 
218e0b8e63eSJohn Marino /*
219e0b8e63eSJohn Marino  * v_cr -- [count]^M
220e0b8e63eSJohn Marino  *	In a script window, send the line to the shell.
221e0b8e63eSJohn Marino  *	In a regular window, move down by lines.
222e0b8e63eSJohn Marino  *
223e0b8e63eSJohn Marino  * PUBLIC: int v_cr(SCR *, VICMD *);
224e0b8e63eSJohn Marino  */
225e0b8e63eSJohn Marino int
v_cr(SCR * sp,VICMD * vp)226e0b8e63eSJohn Marino v_cr(SCR *sp, VICMD *vp)
227e0b8e63eSJohn Marino {
228e0b8e63eSJohn Marino 	/* If it's a colon command-line edit window, it's an ex command. */
229e0b8e63eSJohn Marino 	if (F_ISSET(sp, SC_COMEDIT))
230e0b8e63eSJohn Marino 		return (v_ecl_exec(sp));
231e0b8e63eSJohn Marino 
232e0b8e63eSJohn Marino 	/* If it's a script window, exec the line. */
233e0b8e63eSJohn Marino 	if (F_ISSET(sp, SC_SCRIPT))
234e0b8e63eSJohn Marino 		return (sscr_exec(sp, vp->m_start.lno));
235e0b8e63eSJohn Marino 
236e0b8e63eSJohn Marino 	/* Otherwise, it's the same as v_down(). */
237e0b8e63eSJohn Marino 	return (v_down(sp, vp));
238e0b8e63eSJohn Marino }
239e0b8e63eSJohn Marino 
240e0b8e63eSJohn Marino /*
241e0b8e63eSJohn Marino  * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
242e0b8e63eSJohn Marino  *	Move down by lines.
243e0b8e63eSJohn Marino  *
244e0b8e63eSJohn Marino  * PUBLIC: int v_down(SCR *, VICMD *);
245e0b8e63eSJohn Marino  */
246e0b8e63eSJohn Marino int
v_down(SCR * sp,VICMD * vp)247e0b8e63eSJohn Marino v_down(SCR *sp, VICMD *vp)
248e0b8e63eSJohn Marino {
249e0b8e63eSJohn Marino 	recno_t lno;
250e0b8e63eSJohn Marino 
251e0b8e63eSJohn Marino 	lno = vp->m_start.lno + (F_ISSET(vp, VC_C1SET) ? vp->count : 1);
252e0b8e63eSJohn Marino 	if (!db_exist(sp, lno)) {
253e0b8e63eSJohn Marino 		v_eof(sp, &vp->m_start);
254e0b8e63eSJohn Marino 		return (1);
255e0b8e63eSJohn Marino 	}
256e0b8e63eSJohn Marino 	vp->m_stop.lno = lno;
257e0b8e63eSJohn Marino 	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
258e0b8e63eSJohn Marino 	return (0);
259e0b8e63eSJohn Marino }
260e0b8e63eSJohn Marino 
261e0b8e63eSJohn Marino /*
262e0b8e63eSJohn Marino  * v_hpageup -- [count]^U
263e0b8e63eSJohn Marino  *	Page up half screens.
264e0b8e63eSJohn Marino  *
265e0b8e63eSJohn Marino  * PUBLIC: int v_hpageup(SCR *, VICMD *);
266e0b8e63eSJohn Marino  */
267e0b8e63eSJohn Marino int
v_hpageup(SCR * sp,VICMD * vp)268e0b8e63eSJohn Marino v_hpageup(SCR *sp, VICMD *vp)
269e0b8e63eSJohn Marino {
270e0b8e63eSJohn Marino 	/*
271e0b8e63eSJohn Marino 	 * Half screens always succeed unless already at SOF.
272e0b8e63eSJohn Marino 	 *
273e0b8e63eSJohn Marino 	 * !!!
274e0b8e63eSJohn Marino 	 * Half screens set the scroll value, even if the command
275e0b8e63eSJohn Marino 	 * ultimately failed, in historic vi.  Probably a don't care.
276e0b8e63eSJohn Marino 	 */
277e0b8e63eSJohn Marino 	if (F_ISSET(vp, VC_C1SET))
278e0b8e63eSJohn Marino 		sp->defscroll = vp->count;
279e0b8e63eSJohn Marino 	if (vs_sm_scroll(sp, &vp->m_stop, sp->defscroll, CNTRL_U))
280e0b8e63eSJohn Marino 		return (1);
281e0b8e63eSJohn Marino 	vp->m_final = vp->m_stop;
282e0b8e63eSJohn Marino 	return (0);
283e0b8e63eSJohn Marino }
284e0b8e63eSJohn Marino 
285e0b8e63eSJohn Marino /*
286e0b8e63eSJohn Marino  * v_hpagedown -- [count]^D
287e0b8e63eSJohn Marino  *	Page down half screens.
288e0b8e63eSJohn Marino  *
289e0b8e63eSJohn Marino  * PUBLIC: int v_hpagedown(SCR *, VICMD *);
290e0b8e63eSJohn Marino  */
291e0b8e63eSJohn Marino int
v_hpagedown(SCR * sp,VICMD * vp)292e0b8e63eSJohn Marino v_hpagedown(SCR *sp, VICMD *vp)
293e0b8e63eSJohn Marino {
294e0b8e63eSJohn Marino 	/*
295e0b8e63eSJohn Marino 	 * Half screens always succeed unless already at EOF.
296e0b8e63eSJohn Marino 	 *
297e0b8e63eSJohn Marino 	 * !!!
298e0b8e63eSJohn Marino 	 * Half screens set the scroll value, even if the command
299e0b8e63eSJohn Marino 	 * ultimately failed, in historic vi.  Probably a don't care.
300e0b8e63eSJohn Marino 	 */
301e0b8e63eSJohn Marino 	if (F_ISSET(vp, VC_C1SET))
302e0b8e63eSJohn Marino 		sp->defscroll = vp->count;
303e0b8e63eSJohn Marino 	if (vs_sm_scroll(sp, &vp->m_stop, sp->defscroll, CNTRL_D))
304e0b8e63eSJohn Marino 		return (1);
305e0b8e63eSJohn Marino 	vp->m_final = vp->m_stop;
306e0b8e63eSJohn Marino 	return (0);
307e0b8e63eSJohn Marino }
308e0b8e63eSJohn Marino 
309e0b8e63eSJohn Marino /*
310e0b8e63eSJohn Marino  * v_pagedown -- [count]^F
311e0b8e63eSJohn Marino  *	Page down full screens.
312e0b8e63eSJohn Marino  * !!!
313e0b8e63eSJohn Marino  * Historic vi did not move to the EOF if the screen couldn't move, i.e.
314e0b8e63eSJohn Marino  * if EOF was already displayed on the screen.  This implementation does
315*b1ac2ebbSDaniel Fojt  * move to EOF in that case, making ^F more like the historic ^D.
316e0b8e63eSJohn Marino  *
317e0b8e63eSJohn Marino  * PUBLIC: int v_pagedown(SCR *, VICMD *);
318e0b8e63eSJohn Marino  */
319e0b8e63eSJohn Marino int
v_pagedown(SCR * sp,VICMD * vp)320e0b8e63eSJohn Marino v_pagedown(SCR *sp, VICMD *vp)
321e0b8e63eSJohn Marino {
322e0b8e63eSJohn Marino 	recno_t offset;
323e0b8e63eSJohn Marino 
324e0b8e63eSJohn Marino 	/*
325e0b8e63eSJohn Marino 	 * !!!
326e0b8e63eSJohn Marino 	 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
327e0b8e63eSJohn Marino 	 *
328e0b8e63eSJohn Marino 	 *	top_line = top_line + count * (window - 2);
329e0b8e63eSJohn Marino 	 *
330e0b8e63eSJohn Marino 	 * which was historically wrong.  The correct one is:
331e0b8e63eSJohn Marino 	 *
332e0b8e63eSJohn Marino 	 *	top_line = top_line + count * window - 2;
333e0b8e63eSJohn Marino 	 *
334e0b8e63eSJohn Marino 	 * i.e. the two line "overlap" was only subtracted once.  Which
335e0b8e63eSJohn Marino 	 * makes no sense, but then again, an overlap makes no sense for
336e0b8e63eSJohn Marino 	 * any screen but the "next" one anyway.  We do it the historical
337e0b8e63eSJohn Marino 	 * way as there's no good reason to change it.
338e0b8e63eSJohn Marino 	 *
339e0b8e63eSJohn Marino 	 * If the screen has been split horizontally, use the smaller of
340e0b8e63eSJohn Marino 	 * the current window size and the window option value.
341e0b8e63eSJohn Marino 	 *
342e0b8e63eSJohn Marino 	 * It possible for this calculation to be less than 1; move at
343e0b8e63eSJohn Marino 	 * least one line.
344e0b8e63eSJohn Marino 	 */
345e0b8e63eSJohn Marino 	offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_SPLIT(sp) ?
346e0b8e63eSJohn Marino 	    MIN(sp->t_maxrows, O_VAL(sp, O_WINDOW)) : O_VAL(sp, O_WINDOW));
347e0b8e63eSJohn Marino 	offset = offset <= 2 ? 1 : offset - 2;
348e0b8e63eSJohn Marino 	if (vs_sm_scroll(sp, &vp->m_stop, offset, CNTRL_F))
349e0b8e63eSJohn Marino 		return (1);
350e0b8e63eSJohn Marino 	vp->m_final = vp->m_stop;
351e0b8e63eSJohn Marino 	return (0);
352e0b8e63eSJohn Marino }
353e0b8e63eSJohn Marino 
354e0b8e63eSJohn Marino /*
355e0b8e63eSJohn Marino  * v_pageup -- [count]^B
356e0b8e63eSJohn Marino  *	Page up full screens.
357e0b8e63eSJohn Marino  *
358e0b8e63eSJohn Marino  * !!!
359e0b8e63eSJohn Marino  * Historic vi did not move to the SOF if the screen couldn't move, i.e.
360e0b8e63eSJohn Marino  * if SOF was already displayed on the screen.  This implementation does
361*b1ac2ebbSDaniel Fojt  * move to SOF in that case, making ^B more like the historic ^U.
362e0b8e63eSJohn Marino  *
363e0b8e63eSJohn Marino  * PUBLIC: int v_pageup(SCR *, VICMD *);
364e0b8e63eSJohn Marino  */
365e0b8e63eSJohn Marino int
v_pageup(SCR * sp,VICMD * vp)366e0b8e63eSJohn Marino v_pageup(SCR *sp, VICMD *vp)
367e0b8e63eSJohn Marino {
368e0b8e63eSJohn Marino 	recno_t offset;
369e0b8e63eSJohn Marino 
370e0b8e63eSJohn Marino 	/*
371e0b8e63eSJohn Marino 	 * !!!
372e0b8e63eSJohn Marino 	 * The calculation in IEEE Std 1003.2-1992 (POSIX) is:
373e0b8e63eSJohn Marino 	 *
374e0b8e63eSJohn Marino 	 *	top_line = top_line - count * (window - 2);
375e0b8e63eSJohn Marino 	 *
376e0b8e63eSJohn Marino 	 * which was historically wrong.  The correct one is:
377e0b8e63eSJohn Marino 	 *
378e0b8e63eSJohn Marino 	 *	top_line = (top_line - count * window) + 2;
379e0b8e63eSJohn Marino 	 *
380e0b8e63eSJohn Marino 	 * A simpler expression is that, as with ^F, we scroll exactly:
381e0b8e63eSJohn Marino 	 *
382e0b8e63eSJohn Marino 	 *	count * window - 2
383e0b8e63eSJohn Marino 	 *
384e0b8e63eSJohn Marino 	 * lines.
385e0b8e63eSJohn Marino 	 *
386e0b8e63eSJohn Marino 	 * Bizarre.  As with ^F, an overlap makes no sense for anything
387e0b8e63eSJohn Marino 	 * but the first screen.  We do it the historical way as there's
388e0b8e63eSJohn Marino 	 * no good reason to change it.
389e0b8e63eSJohn Marino 	 *
390e0b8e63eSJohn Marino 	 * If the screen has been split horizontally, use the smaller of
391e0b8e63eSJohn Marino 	 * the current window size and the window option value.
392e0b8e63eSJohn Marino 	 *
393e0b8e63eSJohn Marino 	 * It possible for this calculation to be less than 1; move at
394e0b8e63eSJohn Marino 	 * least one line.
395e0b8e63eSJohn Marino 	 */
396e0b8e63eSJohn Marino 	offset = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (IS_SPLIT(sp) ?
397e0b8e63eSJohn Marino 	    MIN(sp->t_maxrows, O_VAL(sp, O_WINDOW)) : O_VAL(sp, O_WINDOW));
398e0b8e63eSJohn Marino 	offset = offset <= 2 ? 1 : offset - 2;
399e0b8e63eSJohn Marino 	if (vs_sm_scroll(sp, &vp->m_stop, offset, CNTRL_B))
400e0b8e63eSJohn Marino 		return (1);
401e0b8e63eSJohn Marino 	vp->m_final = vp->m_stop;
402e0b8e63eSJohn Marino 	return (0);
403e0b8e63eSJohn Marino }
404e0b8e63eSJohn Marino 
405e0b8e63eSJohn Marino /*
406e0b8e63eSJohn Marino  * v_lineup -- [count]^Y
407e0b8e63eSJohn Marino  *	Page up by lines.
408e0b8e63eSJohn Marino  *
409e0b8e63eSJohn Marino  * PUBLIC: int v_lineup(SCR *, VICMD *);
410e0b8e63eSJohn Marino  */
411e0b8e63eSJohn Marino int
v_lineup(SCR * sp,VICMD * vp)412e0b8e63eSJohn Marino v_lineup(SCR *sp, VICMD *vp)
413e0b8e63eSJohn Marino {
414e0b8e63eSJohn Marino 	/*
415e0b8e63eSJohn Marino 	 * The cursor moves down, staying with its original line, unless it
416e0b8e63eSJohn Marino 	 * reaches the bottom of the screen.
417e0b8e63eSJohn Marino 	 */
418e0b8e63eSJohn Marino 	if (vs_sm_scroll(sp,
419e0b8e63eSJohn Marino 	    &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_Y))
420e0b8e63eSJohn Marino 		return (1);
421e0b8e63eSJohn Marino 	vp->m_final = vp->m_stop;
422e0b8e63eSJohn Marino 	return (0);
423e0b8e63eSJohn Marino }
424e0b8e63eSJohn Marino 
425e0b8e63eSJohn Marino /*
426e0b8e63eSJohn Marino  * v_linedown -- [count]^E
427e0b8e63eSJohn Marino  *	Page down by lines.
428e0b8e63eSJohn Marino  *
429e0b8e63eSJohn Marino  * PUBLIC: int v_linedown(SCR *, VICMD *);
430e0b8e63eSJohn Marino  */
431e0b8e63eSJohn Marino int
v_linedown(SCR * sp,VICMD * vp)432e0b8e63eSJohn Marino v_linedown(SCR *sp, VICMD *vp)
433e0b8e63eSJohn Marino {
434e0b8e63eSJohn Marino 	/*
435e0b8e63eSJohn Marino 	 * The cursor moves up, staying with its original line, unless it
436e0b8e63eSJohn Marino 	 * reaches the top of the screen.
437e0b8e63eSJohn Marino 	 */
438e0b8e63eSJohn Marino 	if (vs_sm_scroll(sp,
439e0b8e63eSJohn Marino 	    &vp->m_stop, F_ISSET(vp, VC_C1SET) ? vp->count : 1, CNTRL_E))
440e0b8e63eSJohn Marino 		return (1);
441e0b8e63eSJohn Marino 	vp->m_final = vp->m_stop;
442e0b8e63eSJohn Marino 	return (0);
443e0b8e63eSJohn Marino }
444