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