-> 1.03
[nvi.git] / vi / v_scroll.c
blob7e1a78a8325e7cfa7ee1148bfe3fdf174d89df9d
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: v_scroll.c,v 8.8 1993/12/16 12:11:56 bostic Exp $ (Berkeley) $Date: 1993/12/16 12:11:56 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <errno.h>
16 #include "vi.h"
17 #include "excmd.h"
18 #include "vcmd.h"
21 * The historic vi had a problem in that all movements were by physical
22 * lines, not by logical, or screen lines. Arguments can be made that this
23 * is the right thing to do. For example, single line movements, such as
24 * 'j' or 'k', should probably work on physical lines. Commands like "dj",
25 * or "j.", where '.' is a change command, make more sense for physical lines
26 * than they do for logical lines.
28 * These arguments, however, don't apply to scrolling commands like ^D and
29 * ^F -- if the window is fairly small, using physical lines can result in
30 * a half-page scroll repainting the entire screen, which is not what the
31 * user wanted. Second, if the line is larger than the screen, using physical
32 * lines can make it impossible to display parts of the line -- there aren't
33 * any commands that don't display the beginning of the line in historic vi,
34 * and if both the beginning and end of the line can't be on the screen at
35 * the same time, you lose. This is even worse in the case of the H, L, and
36 * M commands -- for large lines, they may all refer to the same line and
37 * will result in no movement at all.
39 * This implementation does the scrolling (^B, ^D, ^F, ^U, ^Y, ^E), and the
40 * cursor positioning commands (H, L, M) commands using logical lines, not
41 * physical.
43 * Another issue is that page and half-page scrolling commands historically
44 * moved to the first non-blank character in the new line. If the line is
45 * approximately the same size as the screen, this loses because the cursor
46 * before and after a ^D, may refer to the same location on the screen. In
47 * this implementation, scrolling commands set the cursor to the first non-
48 * blank character if the line changes because of the scroll. Otherwise,
49 * the cursor is left alone.
53 * v_lgoto -- [count]G
54 * Go to first non-blank character of the line count, the last line
55 * of the file by default.
57 int
58 v_lgoto(sp, ep, vp, fm, tm, rp)
59 SCR *sp;
60 EXF *ep;
61 VICMDARG *vp;
62 MARK *fm, *tm, *rp;
64 recno_t last;
66 if (file_lline(sp, ep, &last))
67 return (1);
68 if (F_ISSET(vp, VC_C1SET)) {
69 if (last < vp->count) {
70 v_eof(sp, ep, fm);
71 return (1);
73 rp->lno = vp->count;
74 } else
75 rp->lno = last ? last : 1;
76 return (0);
79 /*
80 * v_home -- [count]H
81 * Move to the first non-blank character of the logical line
82 * count from the top of the screen, 1 by default.
84 int
85 v_home(sp, ep, vp, fm, tm, rp)
86 SCR *sp;
87 EXF *ep;
88 VICMDARG *vp;
89 MARK *fm, *tm, *rp;
91 return (sp->s_position(sp, ep, rp,
92 F_ISSET(vp, VC_C1SET) ? vp->count : 0, P_TOP));
96 * v_middle -- M
97 * Move to the first non-blank character of the logical line
98 * in the middle of the screen.
101 v_middle(sp, ep, vp, fm, tm, rp)
102 SCR *sp;
103 EXF *ep;
104 VICMDARG *vp;
105 MARK *fm, *tm, *rp;
108 * Yielding to none in our quest for compatibility with every
109 * historical blemish of vi, no matter how strange it might be,
110 * we permit the user to enter a count and then ignore it.
112 return (sp->s_position(sp, ep, rp, 0, P_MIDDLE));
116 * v_bottom -- [count]L
117 * Move to the first non-blank character of the logical line
118 * count from the bottom of the screen, 1 by default.
121 v_bottom(sp, ep, vp, fm, tm, rp)
122 SCR *sp;
123 EXF *ep;
124 VICMDARG *vp;
125 MARK *fm, *tm, *rp;
127 return (sp->s_position(sp, ep,
128 rp, F_ISSET(vp, VC_C1SET) ? vp->count : 0, P_BOTTOM));
132 * v_up -- [count]^P, [count]k, [count]-
133 * Move up by lines.
136 v_up(sp, ep, vp, fm, tm, rp)
137 SCR *sp;
138 EXF *ep;
139 VICMDARG *vp;
140 MARK *fm, *tm, *rp;
142 recno_t lno;
144 lno = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
146 if (fm->lno <= lno) {
147 v_sof(sp, fm);
148 return (1);
150 rp->lno = fm->lno - lno;
151 return (0);
155 * v_cr -- [count]^M
156 * In a script window, send the line to the shell.
157 * In a regular window, move down by lines.
160 v_cr(sp, ep, vp, fm, tm, rp)
161 SCR *sp;
162 EXF *ep;
163 VICMDARG *vp;
164 MARK *fm, *tm, *rp;
167 * If it's a script window, exec the line,
168 * otherwise it's the same as v_down().
170 return (F_ISSET(sp, S_SCRIPT) ?
171 sscr_exec(sp, ep, fm->lno) : v_down(sp, ep, vp, fm, tm, rp));
175 * v_down -- [count]^J, [count]^N, [count]j, [count]^M, [count]+
176 * Move down by lines.
179 v_down(sp, ep, vp, fm, tm, rp)
180 SCR *sp;
181 EXF *ep;
182 VICMDARG *vp;
183 MARK *fm, *tm, *rp;
185 recno_t lno;
187 lno = fm->lno + (F_ISSET(vp, VC_C1SET) ? vp->count : 1);
189 if (file_gline(sp, ep, lno, NULL) == NULL) {
190 v_eof(sp, ep, fm);
191 return (1);
193 rp->lno = lno;
194 return (0);
198 * v_hpageup -- [count]^U
199 * Page up half screens.
202 v_hpageup(sp, ep, vp, fm, tm, rp)
203 SCR *sp;
204 EXF *ep;
205 VICMDARG *vp;
206 MARK *fm, *tm, *rp;
209 * Half screens always succeed unless already at SOF. Half screens
210 * set the scroll value, even if the command ultimately failed, in
211 * historic vi. It's probably a don't care.
213 if (F_ISSET(vp, VC_C1SET))
214 O_VAL(sp, O_SCROLL) = vp->count;
215 else
216 vp->count = O_VAL(sp, O_SCROLL);
218 return (sp->s_down(sp, ep, rp, (recno_t)O_VAL(sp, O_SCROLL), 1));
222 * v_hpagedown -- [count]^D
223 * Page down half screens.
226 v_hpagedown(sp, ep, vp, fm, tm, rp)
227 SCR *sp;
228 EXF *ep;
229 VICMDARG *vp;
230 MARK *fm, *tm, *rp;
233 * Half screens always succeed unless already at EOF. Half screens
234 * set the scroll value, even if the command ultimately failed, in
235 * historic vi. It's probably a don't care.
237 if (F_ISSET(vp, VC_C1SET))
238 O_VAL(sp, O_SCROLL) = vp->count;
239 else
240 vp->count = O_VAL(sp, O_SCROLL);
242 return (sp->s_up(sp, ep, rp, (recno_t)O_VAL(sp, O_SCROLL), 1));
246 * v_pageup -- [count]^B
247 * Page up full screens.
249 * !!!
250 * Historic vi did not move to the SOF if the screen couldn't move, i.e.
251 * if SOF was already displayed on the screen. This implementation does
252 * move to SOF in that case, making ^B more like the the historic ^U.
255 v_pageup(sp, ep, vp, fm, tm, rp)
256 SCR *sp;
257 EXF *ep;
258 VICMDARG *vp;
259 MARK *fm, *tm, *rp;
261 recno_t count;
263 /* Calculation from POSIX 1003.2/D8. */
264 count = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (sp->t_rows - 1);
266 return (sp->s_down(sp, ep, rp, count, 1));
270 * v_pagedown -- [count]^F
271 * Page down full screens.
272 * !!!
273 * Historic vi did not move to the EOF if the screen couldn't move, i.e.
274 * if EOF was already displayed on the screen. This implementation does
275 * move to EOF in that case, making ^F more like the the historic ^D.
278 v_pagedown(sp, ep, vp, fm, tm, rp)
279 SCR *sp;
280 EXF *ep;
281 VICMDARG *vp;
282 MARK *fm, *tm, *rp;
284 recno_t count;
286 /* Calculation from POSIX 1003.2/D8. */
287 count = (F_ISSET(vp, VC_C1SET) ? vp->count : 1) * (sp->t_rows - 1);
289 return (sp->s_up(sp, ep, rp, count, 1));
293 * v_lineup -- [count]^Y
294 * Page up by lines.
297 v_lineup(sp, ep, vp, fm, tm, rp)
298 SCR *sp;
299 EXF *ep;
300 VICMDARG *vp;
301 MARK *fm, *tm, *rp;
304 * The cursor moves down, staying with its original line, unless it
305 * reaches the bottom of the screen.
307 return (sp->s_down(sp, ep,
308 rp, F_ISSET(vp, VC_C1SET) ? vp->count : 1, 0));
312 * v_linedown -- [count]^E
313 * Page down by lines.
316 v_linedown(sp, ep, vp, fm, tm, rp)
317 SCR *sp;
318 EXF *ep;
319 VICMDARG *vp;
320 MARK *fm, *tm, *rp;
323 * The cursor moves up, staying with its original line, unless it
324 * reaches the top of the screen.
326 return (sp->s_up(sp, ep,
327 rp, F_ISSET(vp, VC_C1SET) ? vp->count : 1, 0));