vs_smap.c revision 1.1 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 1993, 1994
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos * Copyright (c) 1993, 1994, 1995, 1996
5 1.1 christos * Keith Bostic. All rights reserved.
6 1.1 christos *
7 1.1 christos * See the LICENSE file for redistribution information.
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include "config.h"
11 1.1 christos
12 1.1 christos #ifndef lint
13 1.1 christos static const char sccsid[] = "Id: vs_smap.c,v 10.30 2002/01/19 21:59:07 skimo Exp (Berkeley) Date: 2002/01/19 21:59:07 ";
14 1.1 christos #endif /* not lint */
15 1.1 christos
16 1.1 christos #include <sys/types.h>
17 1.1 christos #include <sys/queue.h>
18 1.1 christos #include <sys/time.h>
19 1.1 christos
20 1.1 christos #include <bitstring.h>
21 1.1 christos #include <limits.h>
22 1.1 christos #include <stdio.h>
23 1.1 christos #include <stdlib.h>
24 1.1 christos #include <string.h>
25 1.1 christos
26 1.1 christos #include "../common/common.h"
27 1.1 christos #include "vi.h"
28 1.1 christos
29 1.1 christos static int vs_deleteln __P((SCR *, int));
30 1.1 christos static int vs_insertln __P((SCR *, int));
31 1.1 christos static int vs_sm_delete __P((SCR *, db_recno_t));
32 1.1 christos static int vs_sm_down __P((SCR *, MARK *, db_recno_t, scroll_t, SMAP *));
33 1.1 christos static int vs_sm_erase __P((SCR *));
34 1.1 christos static int vs_sm_insert __P((SCR *, db_recno_t));
35 1.1 christos static int vs_sm_reset __P((SCR *, db_recno_t));
36 1.1 christos static int vs_sm_up __P((SCR *, MARK *, db_recno_t, scroll_t, SMAP *));
37 1.1 christos
38 1.1 christos /*
39 1.1 christos * vs_change --
40 1.1 christos * Make a change to the screen.
41 1.1 christos *
42 1.1 christos * PUBLIC: int vs_change __P((SCR *, db_recno_t, lnop_t));
43 1.1 christos */
44 1.1 christos int
45 1.1 christos vs_change(SCR *sp, db_recno_t lno, lnop_t op)
46 1.1 christos {
47 1.1 christos VI_PRIVATE *vip;
48 1.1 christos SMAP *p;
49 1.1 christos size_t cnt, oldy, oldx;
50 1.1 christos
51 1.1 christos vip = VIP(sp);
52 1.1 christos
53 1.1 christos /*
54 1.1 christos * XXX
55 1.1 christos * Very nasty special case. The historic vi code displays a single
56 1.1 christos * space (or a '$' if the list option is set) for the first line in
57 1.1 christos * an "empty" file. If we "insert" a line, that line gets scrolled
58 1.1 christos * down, not repainted, so it's incorrect when we refresh the screen.
59 1.1 christos * The vi text input functions detect it explicitly and don't insert
60 1.1 christos * a new line.
61 1.1 christos *
62 1.1 christos * Check for line #2 before going to the end of the file.
63 1.1 christos */
64 1.1 christos if (((op == LINE_APPEND && lno == 0) ||
65 1.1 christos (op == LINE_INSERT && lno == 1)) &&
66 1.1 christos !db_exist(sp, 2)) {
67 1.1 christos lno = 1;
68 1.1 christos op = LINE_RESET;
69 1.1 christos }
70 1.1 christos
71 1.1 christos /* Appending is the same as inserting, if the line is incremented. */
72 1.1 christos if (op == LINE_APPEND) {
73 1.1 christos ++lno;
74 1.1 christos op = LINE_INSERT;
75 1.1 christos }
76 1.1 christos
77 1.1 christos /* Ignore the change if the line is after the map. */
78 1.1 christos if (lno > TMAP->lno)
79 1.1 christos return (0);
80 1.1 christos
81 1.1 christos /*
82 1.1 christos * If the line is before the map, and it's a decrement, decrement
83 1.1 christos * the map. If it's an increment, increment the map. Otherwise,
84 1.1 christos * ignore it.
85 1.1 christos */
86 1.1 christos if (lno < HMAP->lno) {
87 1.1 christos switch (op) {
88 1.1 christos case LINE_APPEND:
89 1.1 christos abort();
90 1.1 christos /* NOTREACHED */
91 1.1 christos case LINE_DELETE:
92 1.1 christos for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
93 1.1 christos --p->lno;
94 1.1 christos if (sp->lno >= lno)
95 1.1 christos --sp->lno;
96 1.1 christos F_SET(vip, VIP_N_RENUMBER);
97 1.1 christos break;
98 1.1 christos case LINE_INSERT:
99 1.1 christos for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
100 1.1 christos ++p->lno;
101 1.1 christos if (sp->lno >= lno)
102 1.1 christos ++sp->lno;
103 1.1 christos F_SET(vip, VIP_N_RENUMBER);
104 1.1 christos break;
105 1.1 christos case LINE_RESET:
106 1.1 christos break;
107 1.1 christos }
108 1.1 christos return (0);
109 1.1 christos }
110 1.1 christos
111 1.1 christos F_SET(vip, VIP_N_REFRESH);
112 1.1 christos
113 1.1 christos /*
114 1.1 christos * Invalidate the line size cache, and invalidate the cursor if it's
115 1.1 christos * on this line,
116 1.1 christos */
117 1.1 christos VI_SCR_CFLUSH(vip);
118 1.1 christos if (sp->lno == lno)
119 1.1 christos F_SET(vip, VIP_CUR_INVALID);
120 1.1 christos
121 1.1 christos /*
122 1.1 christos * If ex modifies the screen after ex output is already on the screen
123 1.1 christos * or if we've switched into ex canonical mode, don't touch it -- we'll
124 1.1 christos * get scrolling wrong, at best.
125 1.1 christos */
126 1.1 christos if (!F_ISSET(sp, SC_TINPUT_INFO) &&
127 1.1 christos (F_ISSET(sp, SC_SCR_EXWROTE) || VIP(sp)->totalcount > 1)) {
128 1.1 christos F_SET(vip, VIP_N_EX_REDRAW);
129 1.1 christos return (0);
130 1.1 christos }
131 1.1 christos
132 1.1 christos /* Save and restore the cursor for these routines. */
133 1.1 christos (void)sp->gp->scr_cursor(sp, &oldy, &oldx);
134 1.1 christos
135 1.1 christos switch (op) {
136 1.1 christos case LINE_DELETE:
137 1.1 christos if (vs_sm_delete(sp, lno))
138 1.1 christos return (1);
139 1.1 christos if (sp->lno > lno)
140 1.1 christos --sp->lno;
141 1.1 christos F_SET(vip, VIP_N_RENUMBER);
142 1.1 christos break;
143 1.1 christos case LINE_INSERT:
144 1.1 christos if (vs_sm_insert(sp, lno))
145 1.1 christos return (1);
146 1.1 christos if (sp->lno > lno)
147 1.1 christos ++sp->lno;
148 1.1 christos F_SET(vip, VIP_N_RENUMBER);
149 1.1 christos break;
150 1.1 christos case LINE_RESET:
151 1.1 christos if (vs_sm_reset(sp, lno))
152 1.1 christos return (1);
153 1.1 christos break;
154 1.1 christos default:
155 1.1 christos abort();
156 1.1 christos }
157 1.1 christos
158 1.1 christos (void)sp->gp->scr_move(sp, oldy, oldx);
159 1.1 christos return (0);
160 1.1 christos }
161 1.1 christos
162 1.1 christos /*
163 1.1 christos * vs_sm_fill --
164 1.1 christos * Fill in the screen map, placing the specified line at the
165 1.1 christos * right position. There isn't any way to tell if an SMAP
166 1.1 christos * entry has been filled in, so this routine had better be
167 1.1 christos * called with P_FILL set before anything else is done.
168 1.1 christos *
169 1.1 christos * !!!
170 1.1 christos * Unexported interface: if lno is OOBLNO, P_TOP means that the HMAP
171 1.1 christos * slot is already filled in, P_BOTTOM means that the TMAP slot is
172 1.1 christos * already filled in, and we just finish up the job.
173 1.1 christos *
174 1.1 christos * PUBLIC: int vs_sm_fill __P((SCR *, db_recno_t, pos_t));
175 1.1 christos */
176 1.1 christos int
177 1.1 christos vs_sm_fill(SCR *sp, db_recno_t lno, pos_t pos)
178 1.1 christos {
179 1.1 christos SMAP *p, tmp;
180 1.1 christos size_t cnt;
181 1.1 christos
182 1.1 christos /* Flush all cached information from the SMAP. */
183 1.1 christos for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
184 1.1 christos SMAP_FLUSH(p);
185 1.1 christos
186 1.1 christos /*
187 1.1 christos * If the map is filled, the screen must be redrawn.
188 1.1 christos *
189 1.1 christos * XXX
190 1.1 christos * This is a bug. We should try and figure out if the desired line
191 1.1 christos * is already in the map or close by -- scrolling the screen would
192 1.1 christos * be a lot better than redrawing.
193 1.1 christos */
194 1.1 christos F_SET(sp, SC_SCR_REDRAW);
195 1.1 christos
196 1.1 christos switch (pos) {
197 1.1 christos case P_FILL:
198 1.1 christos tmp.lno = 1;
199 1.1 christos tmp.coff = 0;
200 1.1 christos tmp.soff = 1;
201 1.1 christos
202 1.1 christos /* See if less than half a screen from the top. */
203 1.1 christos if (vs_sm_nlines(sp,
204 1.1 christos &tmp, lno, HALFTEXT(sp)) <= HALFTEXT(sp)) {
205 1.1 christos lno = 1;
206 1.1 christos goto top;
207 1.1 christos }
208 1.1 christos
209 1.1 christos /* See if less than half a screen from the bottom. */
210 1.1 christos if (db_last(sp, &tmp.lno))
211 1.1 christos return (1);
212 1.1 christos tmp.coff = 0;
213 1.1 christos tmp.soff = vs_screens(sp, tmp.lno, NULL);
214 1.1 christos if (vs_sm_nlines(sp,
215 1.1 christos &tmp, lno, HALFTEXT(sp)) <= HALFTEXT(sp)) {
216 1.1 christos TMAP->lno = tmp.lno;
217 1.1 christos TMAP->coff = tmp.coff;
218 1.1 christos TMAP->soff = tmp.soff;
219 1.1 christos goto bottom;
220 1.1 christos }
221 1.1 christos goto middle;
222 1.1 christos case P_TOP:
223 1.1 christos if (lno != OOBLNO) {
224 1.1 christos top: HMAP->lno = lno;
225 1.1 christos HMAP->coff = 0;
226 1.1 christos HMAP->soff = 1;
227 1.1 christos } else {
228 1.1 christos /*
229 1.1 christos * If number of lines HMAP->lno (top line) spans
230 1.1 christos * changed due to, say reformatting, and now is
231 1.1 christos * fewer than HMAP->soff, reset so the line is
232 1.1 christos * redrawn at the top of the screen.
233 1.1 christos */
234 1.1 christos cnt = vs_screens(sp, HMAP->lno, NULL);
235 1.1 christos if (cnt < HMAP->soff)
236 1.1 christos HMAP->soff = 1;
237 1.1 christos }
238 1.1 christos /* If we fail, just punt. */
239 1.1 christos for (p = HMAP, cnt = sp->t_rows; --cnt; ++p)
240 1.1 christos if (vs_sm_next(sp, p, p + 1))
241 1.1 christos goto err;
242 1.1 christos break;
243 1.1 christos case P_MIDDLE:
244 1.1 christos /* If we fail, guess that the file is too small. */
245 1.1 christos middle: p = HMAP + sp->t_rows / 2;
246 1.1 christos p->lno = lno;
247 1.1 christos p->coff = 0;
248 1.1 christos p->soff = 1;
249 1.1 christos for (; p > HMAP; --p)
250 1.1 christos if (vs_sm_prev(sp, p, p - 1)) {
251 1.1 christos lno = 1;
252 1.1 christos goto top;
253 1.1 christos }
254 1.1 christos
255 1.1 christos /* If we fail, just punt. */
256 1.1 christos p = HMAP + sp->t_rows / 2;
257 1.1 christos for (; p < TMAP; ++p)
258 1.1 christos if (vs_sm_next(sp, p, p + 1))
259 1.1 christos goto err;
260 1.1 christos break;
261 1.1 christos case P_BOTTOM:
262 1.1 christos if (lno != OOBLNO) {
263 1.1 christos TMAP->lno = lno;
264 1.1 christos TMAP->coff = 0;
265 1.1 christos TMAP->soff = vs_screens(sp, lno, NULL);
266 1.1 christos }
267 1.1 christos /* If we fail, guess that the file is too small. */
268 1.1 christos bottom: for (p = TMAP; p > HMAP; --p)
269 1.1 christos if (vs_sm_prev(sp, p, p - 1)) {
270 1.1 christos lno = 1;
271 1.1 christos goto top;
272 1.1 christos }
273 1.1 christos break;
274 1.1 christos default:
275 1.1 christos abort();
276 1.1 christos }
277 1.1 christos return (0);
278 1.1 christos
279 1.1 christos /*
280 1.1 christos * Try and put *something* on the screen. If this fails, we have a
281 1.1 christos * serious hard error.
282 1.1 christos */
283 1.1 christos err: HMAP->lno = 1;
284 1.1 christos HMAP->coff = 0;
285 1.1 christos HMAP->soff = 1;
286 1.1 christos for (p = HMAP; p < TMAP; ++p)
287 1.1 christos if (vs_sm_next(sp, p, p + 1))
288 1.1 christos return (1);
289 1.1 christos return (0);
290 1.1 christos }
291 1.1 christos
292 1.1 christos /*
293 1.1 christos * For the routines vs_sm_reset, vs_sm_delete and vs_sm_insert: if the
294 1.1 christos * screen contains only a single line (whether because the screen is small
295 1.1 christos * or the line large), it gets fairly exciting. Skip the fun, set a flag
296 1.1 christos * so the screen map is refilled and the screen redrawn, and return. This
297 1.1 christos * is amazingly slow, but it's not clear that anyone will care.
298 1.1 christos */
299 1.1 christos #define HANDLE_WEIRDNESS(cnt) { \
300 1.1 christos if (cnt >= sp->t_rows) { \
301 1.1 christos F_SET(sp, SC_SCR_REFORMAT); \
302 1.1 christos return (0); \
303 1.1 christos } \
304 1.1 christos }
305 1.1 christos
306 1.1 christos /*
307 1.1 christos * vs_sm_delete --
308 1.1 christos * Delete a line out of the SMAP.
309 1.1 christos */
310 1.1 christos static int
311 1.1 christos vs_sm_delete(SCR *sp, db_recno_t lno)
312 1.1 christos {
313 1.1 christos SMAP *p, *t;
314 1.1 christos size_t cnt_orig;
315 1.1 christos
316 1.1 christos /*
317 1.1 christos * Find the line in the map, and count the number of screen lines
318 1.1 christos * which display any part of the deleted line.
319 1.1 christos */
320 1.1 christos for (p = HMAP; p->lno != lno; ++p);
321 1.1 christos if (O_ISSET(sp, O_LEFTRIGHT))
322 1.1 christos cnt_orig = 1;
323 1.1 christos else
324 1.1 christos for (cnt_orig = 1, t = p + 1;
325 1.1 christos t <= TMAP && t->lno == lno; ++cnt_orig, ++t);
326 1.1 christos
327 1.1 christos HANDLE_WEIRDNESS(cnt_orig);
328 1.1 christos
329 1.1 christos /* Delete that many lines from the screen. */
330 1.1 christos (void)sp->gp->scr_move(sp, p - HMAP, 0);
331 1.1 christos if (vs_deleteln(sp, cnt_orig))
332 1.1 christos return (1);
333 1.1 christos
334 1.1 christos /* Shift the screen map up. */
335 1.1 christos memmove(p, p + cnt_orig, (((TMAP - p) - cnt_orig) + 1) * sizeof(SMAP));
336 1.1 christos
337 1.1 christos /* Decrement the line numbers for the rest of the map. */
338 1.1 christos for (t = TMAP - cnt_orig; p <= t; ++p)
339 1.1 christos --p->lno;
340 1.1 christos
341 1.1 christos /* Display the new lines. */
342 1.1 christos for (p = TMAP - cnt_orig;;) {
343 1.1 christos if (p < TMAP && vs_sm_next(sp, p, p + 1))
344 1.1 christos return (1);
345 1.1 christos /* vs_sm_next() flushed the cache. */
346 1.1 christos if (vs_line(sp, ++p, NULL, NULL))
347 1.1 christos return (1);
348 1.1 christos if (p == TMAP)
349 1.1 christos break;
350 1.1 christos }
351 1.1 christos return (0);
352 1.1 christos }
353 1.1 christos
354 1.1 christos /*
355 1.1 christos * vs_sm_insert --
356 1.1 christos * Insert a line into the SMAP.
357 1.1 christos */
358 1.1 christos static int
359 1.1 christos vs_sm_insert(SCR *sp, db_recno_t lno)
360 1.1 christos {
361 1.1 christos SMAP *p, *t;
362 1.1 christos size_t cnt_orig, cnt, coff;
363 1.1 christos
364 1.1 christos /* Save the offset. */
365 1.1 christos coff = HMAP->coff;
366 1.1 christos
367 1.1 christos /*
368 1.1 christos * Find the line in the map, find out how many screen lines
369 1.1 christos * needed to display the line.
370 1.1 christos */
371 1.1 christos for (p = HMAP; p->lno != lno; ++p);
372 1.1 christos
373 1.1 christos cnt_orig = vs_screens(sp, lno, NULL);
374 1.1 christos HANDLE_WEIRDNESS(cnt_orig);
375 1.1 christos
376 1.1 christos /*
377 1.1 christos * The lines left in the screen override the number of screen
378 1.1 christos * lines in the inserted line.
379 1.1 christos */
380 1.1 christos cnt = (TMAP - p) + 1;
381 1.1 christos if (cnt_orig > cnt)
382 1.1 christos cnt_orig = cnt;
383 1.1 christos
384 1.1 christos /* Push down that many lines. */
385 1.1 christos (void)sp->gp->scr_move(sp, p - HMAP, 0);
386 1.1 christos if (vs_insertln(sp, cnt_orig))
387 1.1 christos return (1);
388 1.1 christos
389 1.1 christos /* Shift the screen map down. */
390 1.1 christos memmove(p + cnt_orig, p, (((TMAP - p) - cnt_orig) + 1) * sizeof(SMAP));
391 1.1 christos
392 1.1 christos /* Increment the line numbers for the rest of the map. */
393 1.1 christos for (t = p + cnt_orig; t <= TMAP; ++t)
394 1.1 christos ++t->lno;
395 1.1 christos
396 1.1 christos /* Fill in the SMAP for the new lines, and display. */
397 1.1 christos for (cnt = 1, t = p; cnt <= cnt_orig; ++t, ++cnt) {
398 1.1 christos t->lno = lno;
399 1.1 christos t->coff = coff;
400 1.1 christos t->soff = cnt;
401 1.1 christos SMAP_FLUSH(t);
402 1.1 christos if (vs_line(sp, t, NULL, NULL))
403 1.1 christos return (1);
404 1.1 christos }
405 1.1 christos return (0);
406 1.1 christos }
407 1.1 christos
408 1.1 christos /*
409 1.1 christos * vs_sm_reset --
410 1.1 christos * Reset a line in the SMAP.
411 1.1 christos */
412 1.1 christos static int
413 1.1 christos vs_sm_reset(SCR *sp, db_recno_t lno)
414 1.1 christos {
415 1.1 christos SMAP *p, *t;
416 1.1 christos size_t cnt_orig, cnt_new, cnt, diff;
417 1.1 christos
418 1.1 christos /*
419 1.1 christos * See if the number of on-screen rows taken up by the old display
420 1.1 christos * for the line is the same as the number needed for the new one.
421 1.1 christos * If so, repaint, otherwise do it the hard way.
422 1.1 christos */
423 1.1 christos for (p = HMAP; p->lno != lno; ++p);
424 1.1 christos if (O_ISSET(sp, O_LEFTRIGHT)) {
425 1.1 christos t = p;
426 1.1 christos cnt_orig = cnt_new = 1;
427 1.1 christos } else {
428 1.1 christos for (cnt_orig = 0,
429 1.1 christos t = p; t <= TMAP && t->lno == lno; ++cnt_orig, ++t);
430 1.1 christos cnt_new = vs_screens(sp, lno, NULL);
431 1.1 christos }
432 1.1 christos
433 1.1 christos HANDLE_WEIRDNESS(cnt_orig);
434 1.1 christos
435 1.1 christos if (cnt_orig == cnt_new) {
436 1.1 christos do {
437 1.1 christos SMAP_FLUSH(p);
438 1.1 christos if (vs_line(sp, p, NULL, NULL))
439 1.1 christos return (1);
440 1.1 christos } while (++p < t);
441 1.1 christos return (0);
442 1.1 christos }
443 1.1 christos
444 1.1 christos if (cnt_orig < cnt_new) {
445 1.1 christos /* Get the difference. */
446 1.1 christos diff = cnt_new - cnt_orig;
447 1.1 christos
448 1.1 christos /*
449 1.1 christos * The lines left in the screen override the number of screen
450 1.1 christos * lines in the inserted line.
451 1.1 christos */
452 1.1 christos cnt = (TMAP - p) + 1;
453 1.1 christos if (diff > cnt)
454 1.1 christos diff = cnt;
455 1.1 christos
456 1.1 christos /* If there are any following lines, push them down. */
457 1.1 christos if (cnt > 1) {
458 1.1 christos (void)sp->gp->scr_move(sp, p - HMAP, 0);
459 1.1 christos if (vs_insertln(sp, diff))
460 1.1 christos return (1);
461 1.1 christos
462 1.1 christos /* Shift the screen map down. */
463 1.1 christos memmove(p + diff, p,
464 1.1 christos (((TMAP - p) - diff) + 1) * sizeof(SMAP));
465 1.1 christos }
466 1.1 christos
467 1.1 christos /* Fill in the SMAP for the replaced line, and display. */
468 1.1 christos for (cnt = 1, t = p; cnt_new-- && t <= TMAP; ++t, ++cnt) {
469 1.1 christos t->lno = lno;
470 1.1 christos t->soff = cnt;
471 1.1 christos SMAP_FLUSH(t);
472 1.1 christos if (vs_line(sp, t, NULL, NULL))
473 1.1 christos return (1);
474 1.1 christos }
475 1.1 christos } else {
476 1.1 christos /* Get the difference. */
477 1.1 christos diff = cnt_orig - cnt_new;
478 1.1 christos
479 1.1 christos /* Delete that many lines from the screen. */
480 1.1 christos (void)sp->gp->scr_move(sp, p - HMAP, 0);
481 1.1 christos if (vs_deleteln(sp, diff))
482 1.1 christos return (1);
483 1.1 christos
484 1.1 christos /* Shift the screen map up. */
485 1.1 christos memmove(p, p + diff, (((TMAP - p) - diff) + 1) * sizeof(SMAP));
486 1.1 christos
487 1.1 christos /* Fill in the SMAP for the replaced line, and display. */
488 1.1 christos for (cnt = 1, t = p; cnt_new--; ++t, ++cnt) {
489 1.1 christos t->lno = lno;
490 1.1 christos t->soff = cnt;
491 1.1 christos SMAP_FLUSH(t);
492 1.1 christos if (vs_line(sp, t, NULL, NULL))
493 1.1 christos return (1);
494 1.1 christos }
495 1.1 christos
496 1.1 christos /* Display the new lines at the bottom of the screen. */
497 1.1 christos for (t = TMAP - diff;;) {
498 1.1 christos if (t < TMAP && vs_sm_next(sp, t, t + 1))
499 1.1 christos return (1);
500 1.1 christos /* vs_sm_next() flushed the cache. */
501 1.1 christos if (vs_line(sp, ++t, NULL, NULL))
502 1.1 christos return (1);
503 1.1 christos if (t == TMAP)
504 1.1 christos break;
505 1.1 christos }
506 1.1 christos }
507 1.1 christos return (0);
508 1.1 christos }
509 1.1 christos
510 1.1 christos /*
511 1.1 christos * vs_sm_scroll
512 1.1 christos * Scroll the SMAP up/down count logical lines. Different
513 1.1 christos * semantics based on the vi command, *sigh*.
514 1.1 christos *
515 1.1 christos * PUBLIC: int vs_sm_scroll __P((SCR *, MARK *, db_recno_t, scroll_t));
516 1.1 christos */
517 1.1 christos int
518 1.1 christos vs_sm_scroll(SCR *sp, MARK *rp, db_recno_t count, scroll_t scmd)
519 1.1 christos {
520 1.1 christos SMAP *smp;
521 1.1 christos
522 1.1 christos /*
523 1.1 christos * Invalidate the cursor. The line is probably going to change,
524 1.1 christos * (although for ^E and ^Y it may not). In any case, the scroll
525 1.1 christos * routines move the cursor to draw things.
526 1.1 christos */
527 1.1 christos F_SET(VIP(sp), VIP_CUR_INVALID);
528 1.1 christos
529 1.1 christos /* Find the cursor in the screen. */
530 1.1 christos if (vs_sm_cursor(sp, &smp))
531 1.1 christos return (1);
532 1.1 christos
533 1.1 christos switch (scmd) {
534 1.1 christos case CNTRL_B:
535 1.1 christos case CNTRL_U:
536 1.1 christos case CNTRL_Y:
537 1.1 christos case Z_CARAT:
538 1.1 christos if (vs_sm_down(sp, rp, count, scmd, smp))
539 1.1 christos return (1);
540 1.1 christos break;
541 1.1 christos case CNTRL_D:
542 1.1 christos case CNTRL_E:
543 1.1 christos case CNTRL_F:
544 1.1 christos case Z_PLUS:
545 1.1 christos if (vs_sm_up(sp, rp, count, scmd, smp))
546 1.1 christos return (1);
547 1.1 christos break;
548 1.1 christos default:
549 1.1 christos abort();
550 1.1 christos }
551 1.1 christos
552 1.1 christos /*
553 1.1 christos * !!!
554 1.1 christos * If we're at the start of a line, go for the first non-blank.
555 1.1 christos * This makes it look like the old vi, even though we're moving
556 1.1 christos * around by logical lines, not physical ones.
557 1.1 christos *
558 1.1 christos * XXX
559 1.1 christos * In the presence of a long line, which has more than a screen
560 1.1 christos * width of leading spaces, this code can cause a cursor warp.
561 1.1 christos * Live with it.
562 1.1 christos */
563 1.1 christos if (scmd != CNTRL_E && scmd != CNTRL_Y &&
564 1.1 christos rp->cno == 0 && nonblank(sp, rp->lno, &rp->cno))
565 1.1 christos return (1);
566 1.1 christos
567 1.1 christos return (0);
568 1.1 christos }
569 1.1 christos
570 1.1 christos /*
571 1.1 christos * vs_sm_up --
572 1.1 christos * Scroll the SMAP up count logical lines.
573 1.1 christos */
574 1.1 christos static int
575 1.1 christos vs_sm_up(SCR *sp, MARK *rp, db_recno_t count, scroll_t scmd, SMAP *smp)
576 1.1 christos {
577 1.1 christos int cursor_set, echanged, zset;
578 1.1 christos SMAP *ssmp, s1, s2;
579 1.1 christos
580 1.1 christos /*
581 1.1 christos * Check to see if movement is possible.
582 1.1 christos *
583 1.1 christos * Get the line after the map. If that line is a new one (and if
584 1.1 christos * O_LEFTRIGHT option is set, this has to be true), and the next
585 1.1 christos * line doesn't exist, and the cursor doesn't move, or the cursor
586 1.1 christos * isn't even on the screen, or the cursor is already at the last
587 1.1 christos * line in the map, it's an error. If that test succeeded because
588 1.1 christos * the cursor wasn't at the end of the map, test to see if the map
589 1.1 christos * is mostly empty.
590 1.1 christos */
591 1.1 christos if (vs_sm_next(sp, TMAP, &s1))
592 1.1 christos return (1);
593 1.1 christos if (s1.lno > TMAP->lno && !db_exist(sp, s1.lno)) {
594 1.1 christos if (scmd == CNTRL_E || scmd == Z_PLUS || smp == TMAP) {
595 1.1 christos v_eof(sp, NULL);
596 1.1 christos return (1);
597 1.1 christos }
598 1.1 christos if (vs_sm_next(sp, smp, &s1))
599 1.1 christos return (1);
600 1.1 christos if (s1.lno > smp->lno && !db_exist(sp, s1.lno)) {
601 1.1 christos v_eof(sp, NULL);
602 1.1 christos return (1);
603 1.1 christos }
604 1.1 christos }
605 1.1 christos
606 1.1 christos /*
607 1.1 christos * Small screens: see vs_refresh.c section 6a.
608 1.1 christos *
609 1.1 christos * If it's a small screen, and the movement isn't larger than a
610 1.1 christos * screen, i.e some context will remain, open up the screen and
611 1.1 christos * display by scrolling. In this case, the cursor moves down one
612 1.1 christos * line for each line displayed. Otherwise, erase/compress and
613 1.1 christos * repaint, and move the cursor to the first line in the screen.
614 1.1 christos * Note, the ^F command is always in the latter case, for historical
615 1.1 christos * reasons.
616 1.1 christos */
617 1.1 christos cursor_set = 0;
618 1.1 christos if (IS_SMALL(sp)) {
619 1.1 christos if (count >= sp->t_maxrows || scmd == CNTRL_F) {
620 1.1 christos s1 = TMAP[0];
621 1.1 christos if (vs_sm_erase(sp))
622 1.1 christos return (1);
623 1.1 christos for (; count--; s1 = s2) {
624 1.1 christos if (vs_sm_next(sp, &s1, &s2))
625 1.1 christos return (1);
626 1.1 christos if (s2.lno != s1.lno && !db_exist(sp, s2.lno))
627 1.1 christos break;
628 1.1 christos }
629 1.1 christos TMAP[0] = s2;
630 1.1 christos if (vs_sm_fill(sp, OOBLNO, P_BOTTOM))
631 1.1 christos return (1);
632 1.1 christos return (vs_sm_position(sp, rp, 0, P_TOP));
633 1.1 christos }
634 1.1 christos cursor_set = scmd == CNTRL_E || vs_sm_cursor(sp, &ssmp);
635 1.1 christos for (; count &&
636 1.1 christos sp->t_rows != sp->t_maxrows; --count, ++sp->t_rows) {
637 1.1 christos if (vs_sm_next(sp, TMAP, &s1))
638 1.1 christos return (1);
639 1.1 christos if (TMAP->lno != s1.lno && !db_exist(sp, s1.lno))
640 1.1 christos break;
641 1.1 christos *++TMAP = s1;
642 1.1 christos /* vs_sm_next() flushed the cache. */
643 1.1 christos if (vs_line(sp, TMAP, NULL, NULL))
644 1.1 christos return (1);
645 1.1 christos
646 1.1 christos if (!cursor_set)
647 1.1 christos ++ssmp;
648 1.1 christos }
649 1.1 christos if (!cursor_set) {
650 1.1 christos rp->lno = ssmp->lno;
651 1.1 christos rp->cno = ssmp->c_sboff;
652 1.1 christos }
653 1.1 christos if (count == 0)
654 1.1 christos return (0);
655 1.1 christos }
656 1.1 christos
657 1.1 christos for (echanged = zset = 0; count; --count) {
658 1.1 christos /* Decide what would show up on the screen. */
659 1.1 christos if (vs_sm_next(sp, TMAP, &s1))
660 1.1 christos return (1);
661 1.1 christos
662 1.1 christos /* If the line doesn't exist, we're done. */
663 1.1 christos if (TMAP->lno != s1.lno && !db_exist(sp, s1.lno))
664 1.1 christos break;
665 1.1 christos
666 1.1 christos /* Scroll the screen cursor up one logical line. */
667 1.1 christos if (vs_sm_1up(sp))
668 1.1 christos return (1);
669 1.1 christos switch (scmd) {
670 1.1 christos case CNTRL_E:
671 1.1 christos if (smp > HMAP)
672 1.1 christos --smp;
673 1.1 christos else
674 1.1 christos echanged = 1;
675 1.1 christos break;
676 1.1 christos case Z_PLUS:
677 1.1 christos if (zset) {
678 1.1 christos if (smp > HMAP)
679 1.1 christos --smp;
680 1.1 christos } else {
681 1.1 christos smp = TMAP;
682 1.1 christos zset = 1;
683 1.1 christos }
684 1.1 christos /* FALLTHROUGH */
685 1.1 christos default:
686 1.1 christos break;
687 1.1 christos }
688 1.1 christos }
689 1.1 christos
690 1.1 christos if (cursor_set)
691 1.1 christos return(0);
692 1.1 christos
693 1.1 christos switch (scmd) {
694 1.1 christos case CNTRL_E:
695 1.1 christos /*
696 1.1 christos * On a ^E that was forced to change lines, try and keep the
697 1.1 christos * cursor as close as possible to the last position, but also
698 1.1 christos * set it up so that the next "real" movement will return the
699 1.1 christos * cursor to the closest position to the last real movement.
700 1.1 christos */
701 1.1 christos if (echanged) {
702 1.1 christos rp->lno = smp->lno;
703 1.1 christos rp->cno = vs_colpos(sp, smp->lno,
704 1.1 christos (O_ISSET(sp, O_LEFTRIGHT) ?
705 1.1 christos smp->coff : (smp->soff - 1) * sp->cols) +
706 1.1 christos sp->rcm % sp->cols);
707 1.1 christos }
708 1.1 christos return (0);
709 1.1 christos case CNTRL_F:
710 1.1 christos /*
711 1.1 christos * If there are more lines, the ^F command is positioned at
712 1.1 christos * the first line of the screen.
713 1.1 christos */
714 1.1 christos if (!count) {
715 1.1 christos smp = HMAP;
716 1.1 christos break;
717 1.1 christos }
718 1.1 christos /* FALLTHROUGH */
719 1.1 christos case CNTRL_D:
720 1.1 christos /*
721 1.1 christos * The ^D and ^F commands move the cursor towards EOF
722 1.1 christos * if there are more lines to move. Check to be sure
723 1.1 christos * the lines actually exist. (They may not if the
724 1.1 christos * file is smaller than the screen.)
725 1.1 christos */
726 1.1 christos for (; count; --count, ++smp)
727 1.1 christos if (smp == TMAP || !db_exist(sp, smp[1].lno))
728 1.1 christos break;
729 1.1 christos break;
730 1.1 christos case Z_PLUS:
731 1.1 christos /* The z+ command moves the cursor to the first new line. */
732 1.1 christos break;
733 1.1 christos default:
734 1.1 christos abort();
735 1.1 christos }
736 1.1 christos
737 1.1 christos if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
738 1.1 christos return (1);
739 1.1 christos rp->lno = smp->lno;
740 1.1 christos rp->cno = smp->c_scoff == 255 ? 0 : smp->c_sboff;
741 1.1 christos return (0);
742 1.1 christos }
743 1.1 christos
744 1.1 christos /*
745 1.1 christos * vs_sm_1up --
746 1.1 christos * Scroll the SMAP up one.
747 1.1 christos *
748 1.1 christos * PUBLIC: int vs_sm_1up __P((SCR *));
749 1.1 christos */
750 1.1 christos int
751 1.1 christos vs_sm_1up(SCR *sp)
752 1.1 christos {
753 1.1 christos /*
754 1.1 christos * Delete the top line of the screen. Shift the screen map
755 1.1 christos * up and display a new line at the bottom of the screen.
756 1.1 christos */
757 1.1 christos (void)sp->gp->scr_move(sp, 0, 0);
758 1.1 christos if (vs_deleteln(sp, 1))
759 1.1 christos return (1);
760 1.1 christos
761 1.1 christos /* One-line screens can fail. */
762 1.1 christos if (IS_ONELINE(sp)) {
763 1.1 christos if (vs_sm_next(sp, TMAP, TMAP))
764 1.1 christos return (1);
765 1.1 christos } else {
766 1.1 christos memmove(HMAP, HMAP + 1, (sp->rows - 1) * sizeof(SMAP));
767 1.1 christos if (vs_sm_next(sp, TMAP - 1, TMAP))
768 1.1 christos return (1);
769 1.1 christos }
770 1.1 christos /* vs_sm_next() flushed the cache. */
771 1.1 christos return (vs_line(sp, TMAP, NULL, NULL));
772 1.1 christos }
773 1.1 christos
774 1.1 christos /*
775 1.1 christos * vs_deleteln --
776 1.1 christos * Delete a line a la curses, make sure to put the information
777 1.1 christos * line and other screens back.
778 1.1 christos */
779 1.1 christos static int
780 1.1 christos vs_deleteln(SCR *sp, int cnt)
781 1.1 christos {
782 1.1 christos GS *gp;
783 1.1 christos size_t oldy, oldx;
784 1.1 christos
785 1.1 christos gp = sp->gp;
786 1.1 christos
787 1.1 christos /* If the screen is vertically split, we can't scroll it. */
788 1.1 christos if (IS_VSPLIT(sp)) {
789 1.1 christos F_SET(sp, SC_SCR_REDRAW);
790 1.1 christos return (0);
791 1.1 christos }
792 1.1 christos
793 1.1 christos if (IS_ONELINE(sp))
794 1.1 christos (void)gp->scr_clrtoeol(sp);
795 1.1 christos else {
796 1.1 christos (void)gp->scr_cursor(sp, &oldy, &oldx);
797 1.1 christos while (cnt--) {
798 1.1 christos (void)gp->scr_deleteln(sp);
799 1.1 christos (void)gp->scr_move(sp, LASTLINE(sp), 0);
800 1.1 christos (void)gp->scr_insertln(sp);
801 1.1 christos (void)gp->scr_move(sp, oldy, oldx);
802 1.1 christos }
803 1.1 christos }
804 1.1 christos return (0);
805 1.1 christos }
806 1.1 christos
807 1.1 christos /*
808 1.1 christos * vs_sm_down --
809 1.1 christos * Scroll the SMAP down count logical lines.
810 1.1 christos */
811 1.1 christos static int
812 1.1 christos vs_sm_down(SCR *sp, MARK *rp, db_recno_t count, scroll_t scmd, SMAP *smp)
813 1.1 christos {
814 1.1 christos SMAP *ssmp, s1, s2;
815 1.1 christos int cursor_set, ychanged, zset;
816 1.1 christos
817 1.1 christos /* Check to see if movement is possible. */
818 1.1 christos if (HMAP->lno == 1 &&
819 1.1 christos (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1) &&
820 1.1 christos (scmd == CNTRL_Y || scmd == Z_CARAT || smp == HMAP)) {
821 1.1 christos v_sof(sp, NULL);
822 1.1 christos return (1);
823 1.1 christos }
824 1.1 christos
825 1.1 christos /*
826 1.1 christos * Small screens: see vs_refresh.c section 6a.
827 1.1 christos *
828 1.1 christos * If it's a small screen, and the movement isn't larger than a
829 1.1 christos * screen, i.e some context will remain, open up the screen and
830 1.1 christos * display by scrolling. In this case, the cursor moves up one
831 1.1 christos * line for each line displayed. Otherwise, erase/compress and
832 1.1 christos * repaint, and move the cursor to the first line in the screen.
833 1.1 christos * Note, the ^B command is always in the latter case, for historical
834 1.1 christos * reasons.
835 1.1 christos */
836 1.1 christos cursor_set = scmd == CNTRL_Y;
837 1.1 christos if (IS_SMALL(sp)) {
838 1.1 christos if (count >= sp->t_maxrows || scmd == CNTRL_B) {
839 1.1 christos s1 = HMAP[0];
840 1.1 christos if (vs_sm_erase(sp))
841 1.1 christos return (1);
842 1.1 christos for (; count--; s1 = s2) {
843 1.1 christos if (vs_sm_prev(sp, &s1, &s2))
844 1.1 christos return (1);
845 1.1 christos if (s2.lno == 1 &&
846 1.1 christos (O_ISSET(sp, O_LEFTRIGHT) || s2.soff == 1))
847 1.1 christos break;
848 1.1 christos }
849 1.1 christos HMAP[0] = s2;
850 1.1 christos if (vs_sm_fill(sp, OOBLNO, P_TOP))
851 1.1 christos return (1);
852 1.1 christos return (vs_sm_position(sp, rp, 0, P_BOTTOM));
853 1.1 christos }
854 1.1 christos cursor_set = scmd == CNTRL_Y || vs_sm_cursor(sp, &ssmp);
855 1.1 christos for (; count &&
856 1.1 christos sp->t_rows != sp->t_maxrows; --count, ++sp->t_rows) {
857 1.1 christos if (HMAP->lno == 1 &&
858 1.1 christos (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1))
859 1.1 christos break;
860 1.1 christos ++TMAP;
861 1.1 christos if (vs_sm_1down(sp))
862 1.1 christos return (1);
863 1.1 christos }
864 1.1 christos if (!cursor_set) {
865 1.1 christos rp->lno = ssmp->lno;
866 1.1 christos rp->cno = ssmp->c_sboff;
867 1.1 christos }
868 1.1 christos if (count == 0)
869 1.1 christos return (0);
870 1.1 christos }
871 1.1 christos
872 1.1 christos for (ychanged = zset = 0; count; --count) {
873 1.1 christos /* If the line doesn't exist, we're done. */
874 1.1 christos if (HMAP->lno == 1 &&
875 1.1 christos (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1))
876 1.1 christos break;
877 1.1 christos
878 1.1 christos /* Scroll the screen and cursor down one logical line. */
879 1.1 christos if (vs_sm_1down(sp))
880 1.1 christos return (1);
881 1.1 christos switch (scmd) {
882 1.1 christos case CNTRL_Y:
883 1.1 christos if (smp < TMAP)
884 1.1 christos ++smp;
885 1.1 christos else
886 1.1 christos ychanged = 1;
887 1.1 christos break;
888 1.1 christos case Z_CARAT:
889 1.1 christos if (zset) {
890 1.1 christos if (smp < TMAP)
891 1.1 christos ++smp;
892 1.1 christos } else {
893 1.1 christos smp = HMAP;
894 1.1 christos zset = 1;
895 1.1 christos }
896 1.1 christos /* FALLTHROUGH */
897 1.1 christos default:
898 1.1 christos break;
899 1.1 christos }
900 1.1 christos }
901 1.1 christos
902 1.1 christos if (scmd != CNTRL_Y && cursor_set)
903 1.1 christos return(0);
904 1.1 christos
905 1.1 christos switch (scmd) {
906 1.1 christos case CNTRL_B:
907 1.1 christos /*
908 1.1 christos * If there are more lines, the ^B command is positioned at
909 1.1 christos * the last line of the screen. However, the line may not
910 1.1 christos * exist.
911 1.1 christos */
912 1.1 christos if (!count) {
913 1.1 christos for (smp = TMAP; smp > HMAP; --smp)
914 1.1 christos if (db_exist(sp, smp->lno))
915 1.1 christos break;
916 1.1 christos break;
917 1.1 christos }
918 1.1 christos /* FALLTHROUGH */
919 1.1 christos case CNTRL_U:
920 1.1 christos /*
921 1.1 christos * The ^B and ^U commands move the cursor towards SOF
922 1.1 christos * if there are more lines to move.
923 1.1 christos */
924 1.1 christos if (count < smp - HMAP)
925 1.1 christos smp -= count;
926 1.1 christos else
927 1.1 christos smp = HMAP;
928 1.1 christos break;
929 1.1 christos case CNTRL_Y:
930 1.1 christos /*
931 1.1 christos * On a ^Y that was forced to change lines, try and keep the
932 1.1 christos * cursor as close as possible to the last position, but also
933 1.1 christos * set it up so that the next "real" movement will return the
934 1.1 christos * cursor to the closest position to the last real movement.
935 1.1 christos */
936 1.1 christos if (ychanged) {
937 1.1 christos rp->lno = smp->lno;
938 1.1 christos rp->cno = vs_colpos(sp, smp->lno,
939 1.1 christos (O_ISSET(sp, O_LEFTRIGHT) ?
940 1.1 christos smp->coff : (smp->soff - 1) * sp->cols) +
941 1.1 christos sp->rcm % sp->cols);
942 1.1 christos }
943 1.1 christos return (0);
944 1.1 christos case Z_CARAT:
945 1.1 christos /* The z^ command moves the cursor to the first new line. */
946 1.1 christos break;
947 1.1 christos default:
948 1.1 christos abort();
949 1.1 christos }
950 1.1 christos
951 1.1 christos if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
952 1.1 christos return (1);
953 1.1 christos rp->lno = smp->lno;
954 1.1 christos rp->cno = smp->c_scoff == 255 ? 0 : smp->c_sboff;
955 1.1 christos return (0);
956 1.1 christos }
957 1.1 christos
958 1.1 christos /*
959 1.1 christos * vs_sm_erase --
960 1.1 christos * Erase the small screen area for the scrolling functions.
961 1.1 christos */
962 1.1 christos static int
963 1.1 christos vs_sm_erase(SCR *sp)
964 1.1 christos {
965 1.1 christos GS *gp;
966 1.1 christos
967 1.1 christos gp = sp->gp;
968 1.1 christos (void)gp->scr_move(sp, LASTLINE(sp), 0);
969 1.1 christos (void)gp->scr_clrtoeol(sp);
970 1.1 christos for (; sp->t_rows > sp->t_minrows; --sp->t_rows, --TMAP) {
971 1.1 christos (void)gp->scr_move(sp, TMAP - HMAP, 0);
972 1.1 christos (void)gp->scr_clrtoeol(sp);
973 1.1 christos }
974 1.1 christos return (0);
975 1.1 christos }
976 1.1 christos
977 1.1 christos /*
978 1.1 christos * vs_sm_1down --
979 1.1 christos * Scroll the SMAP down one.
980 1.1 christos *
981 1.1 christos * PUBLIC: int vs_sm_1down __P((SCR *));
982 1.1 christos */
983 1.1 christos int
984 1.1 christos vs_sm_1down(SCR *sp)
985 1.1 christos {
986 1.1 christos /*
987 1.1 christos * Insert a line at the top of the screen. Shift the screen map
988 1.1 christos * down and display a new line at the top of the screen.
989 1.1 christos */
990 1.1 christos (void)sp->gp->scr_move(sp, 0, 0);
991 1.1 christos if (vs_insertln(sp, 1))
992 1.1 christos return (1);
993 1.1 christos
994 1.1 christos /* One-line screens can fail. */
995 1.1 christos if (IS_ONELINE(sp)) {
996 1.1 christos if (vs_sm_prev(sp, HMAP, HMAP))
997 1.1 christos return (1);
998 1.1 christos } else {
999 1.1 christos memmove(HMAP + 1, HMAP, (sp->rows - 1) * sizeof(SMAP));
1000 1.1 christos if (vs_sm_prev(sp, HMAP + 1, HMAP))
1001 1.1 christos return (1);
1002 1.1 christos }
1003 1.1 christos /* vs_sm_prev() flushed the cache. */
1004 1.1 christos return (vs_line(sp, HMAP, NULL, NULL));
1005 1.1 christos }
1006 1.1 christos
1007 1.1 christos /*
1008 1.1 christos * vs_insertln --
1009 1.1 christos * Insert a line a la curses, make sure to put the information
1010 1.1 christos * line and other screens back.
1011 1.1 christos */
1012 1.1 christos static int
1013 1.1 christos vs_insertln(SCR *sp, int cnt)
1014 1.1 christos {
1015 1.1 christos GS *gp;
1016 1.1 christos size_t oldy, oldx;
1017 1.1 christos
1018 1.1 christos gp = sp->gp;
1019 1.1 christos
1020 1.1 christos /* If the screen is vertically split, we can't scroll it. */
1021 1.1 christos if (IS_VSPLIT(sp)) {
1022 1.1 christos F_SET(sp, SC_SCR_REDRAW);
1023 1.1 christos return (0);
1024 1.1 christos }
1025 1.1 christos
1026 1.1 christos if (IS_ONELINE(sp)) {
1027 1.1 christos (void)gp->scr_move(sp, LASTLINE(sp), 0);
1028 1.1 christos (void)gp->scr_clrtoeol(sp);
1029 1.1 christos } else {
1030 1.1 christos (void)gp->scr_cursor(sp, &oldy, &oldx);
1031 1.1 christos while (cnt--) {
1032 1.1 christos (void)gp->scr_move(sp, LASTLINE(sp) - 1, 0);
1033 1.1 christos (void)gp->scr_deleteln(sp);
1034 1.1 christos (void)gp->scr_move(sp, oldy, oldx);
1035 1.1 christos (void)gp->scr_insertln(sp);
1036 1.1 christos }
1037 1.1 christos }
1038 1.1 christos return (0);
1039 1.1 christos }
1040 1.1 christos
1041 1.1 christos /*
1042 1.1 christos * vs_sm_next --
1043 1.1 christos * Fill in the next entry in the SMAP.
1044 1.1 christos *
1045 1.1 christos * PUBLIC: int vs_sm_next __P((SCR *, SMAP *, SMAP *));
1046 1.1 christos */
1047 1.1 christos int
1048 1.1 christos vs_sm_next(SCR *sp, SMAP *p, SMAP *t)
1049 1.1 christos {
1050 1.1 christos size_t lcnt;
1051 1.1 christos
1052 1.1 christos SMAP_FLUSH(t);
1053 1.1 christos if (O_ISSET(sp, O_LEFTRIGHT)) {
1054 1.1 christos t->lno = p->lno + 1;
1055 1.1 christos t->coff = p->coff;
1056 1.1 christos } else {
1057 1.1 christos lcnt = vs_screens(sp, p->lno, NULL);
1058 1.1 christos if (lcnt == p->soff) {
1059 1.1 christos t->lno = p->lno + 1;
1060 1.1 christos t->soff = 1;
1061 1.1 christos } else {
1062 1.1 christos t->lno = p->lno;
1063 1.1 christos t->soff = p->soff + 1;
1064 1.1 christos }
1065 1.1 christos }
1066 1.1 christos return (0);
1067 1.1 christos }
1068 1.1 christos
1069 1.1 christos /*
1070 1.1 christos * vs_sm_prev --
1071 1.1 christos * Fill in the previous entry in the SMAP.
1072 1.1 christos *
1073 1.1 christos * PUBLIC: int vs_sm_prev __P((SCR *, SMAP *, SMAP *));
1074 1.1 christos */
1075 1.1 christos int
1076 1.1 christos vs_sm_prev(SCR *sp, SMAP *p, SMAP *t)
1077 1.1 christos {
1078 1.1 christos SMAP_FLUSH(t);
1079 1.1 christos if (O_ISSET(sp, O_LEFTRIGHT)) {
1080 1.1 christos t->lno = p->lno - 1;
1081 1.1 christos t->coff = p->coff;
1082 1.1 christos } else {
1083 1.1 christos if (p->soff != 1) {
1084 1.1 christos t->lno = p->lno;
1085 1.1 christos t->soff = p->soff - 1;
1086 1.1 christos } else {
1087 1.1 christos t->lno = p->lno - 1;
1088 1.1 christos t->soff = vs_screens(sp, t->lno, NULL);
1089 1.1 christos }
1090 1.1 christos }
1091 1.1 christos return (t->lno == 0);
1092 1.1 christos }
1093 1.1 christos
1094 1.1 christos /*
1095 1.1 christos * vs_sm_cursor --
1096 1.1 christos * Return the SMAP entry referenced by the cursor.
1097 1.1 christos *
1098 1.1 christos * PUBLIC: int vs_sm_cursor __P((SCR *, SMAP **));
1099 1.1 christos */
1100 1.1 christos int
1101 1.1 christos vs_sm_cursor(SCR *sp, SMAP **smpp)
1102 1.1 christos {
1103 1.1 christos SMAP *p;
1104 1.1 christos
1105 1.1 christos /* See if the cursor is not in the map. */
1106 1.1 christos if (sp->lno < HMAP->lno || sp->lno > TMAP->lno)
1107 1.1 christos return (1);
1108 1.1 christos
1109 1.1 christos /* Find the first occurence of the line. */
1110 1.1 christos for (p = HMAP; p->lno != sp->lno; ++p);
1111 1.1 christos
1112 1.1 christos /* Fill in the map information until we find the right line. */
1113 1.1 christos for (; p <= TMAP; ++p) {
1114 1.1 christos /* Short lines are common and easy to detect. */
1115 1.1 christos if (p != TMAP && (p + 1)->lno != p->lno) {
1116 1.1 christos *smpp = p;
1117 1.1 christos return (0);
1118 1.1 christos }
1119 1.1 christos if (!SMAP_CACHE(p) && vs_line(sp, p, NULL, NULL))
1120 1.1 christos return (1);
1121 1.1 christos if (p->c_eboff >= sp->cno) {
1122 1.1 christos *smpp = p;
1123 1.1 christos return (0);
1124 1.1 christos }
1125 1.1 christos }
1126 1.1 christos
1127 1.1 christos /* It was past the end of the map after all. */
1128 1.1 christos return (1);
1129 1.1 christos }
1130 1.1 christos
1131 1.1 christos /*
1132 1.1 christos * vs_sm_position --
1133 1.1 christos * Return the line/column of the top, middle or last line on the screen.
1134 1.1 christos * (The vi H, M and L commands.) Here because only the screen routines
1135 1.1 christos * know what's really out there.
1136 1.1 christos *
1137 1.1 christos * PUBLIC: int vs_sm_position __P((SCR *, MARK *, u_long, pos_t));
1138 1.1 christos */
1139 1.1 christos int
1140 1.1 christos vs_sm_position(SCR *sp, MARK *rp, u_long cnt, pos_t pos)
1141 1.1 christos {
1142 1.1 christos SMAP *smp;
1143 1.1 christos db_recno_t last;
1144 1.1 christos
1145 1.1 christos switch (pos) {
1146 1.1 christos case P_TOP:
1147 1.1 christos /*
1148 1.1 christos * !!!
1149 1.1 christos * Historically, an invalid count to the H command failed.
1150 1.1 christos * We do nothing special here, just making sure that H in
1151 1.1 christos * an empty screen works.
1152 1.1 christos */
1153 1.1 christos if (cnt > TMAP - HMAP)
1154 1.1 christos goto sof;
1155 1.1 christos smp = HMAP + cnt;
1156 1.1 christos if (cnt && !db_exist(sp, smp->lno)) {
1157 1.1 christos sof: msgq(sp, M_BERR, "220|Movement past the end-of-screen");
1158 1.1 christos return (1);
1159 1.1 christos }
1160 1.1 christos break;
1161 1.1 christos case P_MIDDLE:
1162 1.1 christos /*
1163 1.1 christos * !!!
1164 1.1 christos * Historically, a count to the M command was ignored.
1165 1.1 christos * If the screen isn't filled, find the middle of what's
1166 1.1 christos * real and move there.
1167 1.1 christos */
1168 1.1 christos if (!db_exist(sp, TMAP->lno)) {
1169 1.1 christos if (db_last(sp, &last))
1170 1.1 christos return (1);
1171 1.1 christos for (smp = TMAP; smp->lno > last && smp > HMAP; --smp);
1172 1.1 christos if (smp > HMAP)
1173 1.1 christos smp -= (smp - HMAP) / 2;
1174 1.1 christos } else
1175 1.1 christos smp = (HMAP + (TMAP - HMAP) / 2) + cnt;
1176 1.1 christos break;
1177 1.1 christos case P_BOTTOM:
1178 1.1 christos /*
1179 1.1 christos * !!!
1180 1.1 christos * Historically, an invalid count to the L command failed.
1181 1.1 christos * If the screen isn't filled, find the bottom of what's
1182 1.1 christos * real and try to offset from there.
1183 1.1 christos */
1184 1.1 christos if (cnt > TMAP - HMAP)
1185 1.1 christos goto eof;
1186 1.1 christos smp = TMAP - cnt;
1187 1.1 christos if (!db_exist(sp, smp->lno)) {
1188 1.1 christos if (db_last(sp, &last))
1189 1.1 christos return (1);
1190 1.1 christos for (; smp->lno > last && smp > HMAP; --smp);
1191 1.1 christos if (cnt > smp - HMAP) {
1192 1.1 christos eof: msgq(sp, M_BERR,
1193 1.1 christos "221|Movement past the beginning-of-screen");
1194 1.1 christos return (1);
1195 1.1 christos }
1196 1.1 christos smp -= cnt;
1197 1.1 christos }
1198 1.1 christos break;
1199 1.1 christos default:
1200 1.1 christos abort();
1201 1.1 christos }
1202 1.1 christos
1203 1.1 christos /* Make sure that the cached information is valid. */
1204 1.1 christos if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
1205 1.1 christos return (1);
1206 1.1 christos rp->lno = smp->lno;
1207 1.1 christos rp->cno = smp->c_sboff;
1208 1.1 christos
1209 1.1 christos return (0);
1210 1.1 christos }
1211 1.1 christos
1212 1.1 christos /*
1213 1.1 christos * vs_sm_nlines --
1214 1.1 christos * Return the number of screen lines from an SMAP entry to the
1215 1.1 christos * start of some file line, less than a maximum value.
1216 1.1 christos *
1217 1.1 christos * PUBLIC: db_recno_t vs_sm_nlines __P((SCR *, SMAP *, db_recno_t, size_t));
1218 1.1 christos */
1219 1.1 christos db_recno_t
1220 1.1 christos vs_sm_nlines(SCR *sp, SMAP *from_sp, db_recno_t to_lno, size_t max)
1221 1.1 christos {
1222 1.1 christos db_recno_t lno, lcnt;
1223 1.1 christos
1224 1.1 christos if (O_ISSET(sp, O_LEFTRIGHT))
1225 1.1 christos return (from_sp->lno > to_lno ?
1226 1.1 christos from_sp->lno - to_lno : to_lno - from_sp->lno);
1227 1.1 christos
1228 1.1 christos if (from_sp->lno == to_lno)
1229 1.1 christos return (from_sp->soff - 1);
1230 1.1 christos
1231 1.1 christos if (from_sp->lno > to_lno) {
1232 1.1 christos lcnt = from_sp->soff - 1; /* Correct for off-by-one. */
1233 1.1 christos for (lno = from_sp->lno; --lno >= to_lno && lcnt <= max;)
1234 1.1 christos lcnt += vs_screens(sp, lno, NULL);
1235 1.1 christos } else {
1236 1.1 christos lno = from_sp->lno;
1237 1.1 christos lcnt = (vs_screens(sp, lno, NULL) - from_sp->soff) + 1;
1238 1.1 christos for (; ++lno < to_lno && lcnt <= max;)
1239 1.1 christos lcnt += vs_screens(sp, lno, NULL);
1240 1.1 christos }
1241 1.1 christos return (lcnt);
1242 1.1 christos }
1243