2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
22 #include "../common/common.h"
25 static int vs_deleteln(SCR *, int);
26 static int vs_insertln(SCR *, int);
27 static int vs_sm_delete(SCR *, recno_t);
28 static int vs_sm_down(SCR *, MARK *, recno_t, scroll_t, SMAP *);
29 static int vs_sm_erase(SCR *);
30 static int vs_sm_insert(SCR *, recno_t);
31 static int vs_sm_reset(SCR *, recno_t);
32 static int vs_sm_up(SCR *, MARK *, recno_t, scroll_t, SMAP *);
36 * Make a change to the screen.
38 * PUBLIC: int vs_change(SCR *, recno_t, lnop_t);
41 vs_change(SCR *sp, recno_t lno, lnop_t op)
45 size_t cnt, oldy, oldx;
51 * Very nasty special case. The historic vi code displays a single
52 * space (or a '$' if the list option is set) for the first line in
53 * an "empty" file. If we "insert" a line, that line gets scrolled
54 * down, not repainted, so it's incorrect when we refresh the screen.
55 * The vi text input functions detect it explicitly and don't insert
58 * Check for line #2 before going to the end of the file.
60 if (((op == LINE_APPEND && lno == 0) ||
61 (op == LINE_INSERT && lno == 1)) &&
67 /* Appending is the same as inserting, if the line is incremented. */
68 if (op == LINE_APPEND) {
73 /* Ignore the change if the line is after the map. */
78 * If the line is before the map, and it's a decrement, decrement
79 * the map. If it's an increment, increment the map. Otherwise,
82 if (lno < HMAP->lno) {
88 for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
92 F_SET(vip, VIP_N_RENUMBER);
95 for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
99 F_SET(vip, VIP_N_RENUMBER);
107 F_SET(vip, VIP_N_REFRESH);
110 * Invalidate the line size cache, and invalidate the cursor if it's
115 F_SET(vip, VIP_CUR_INVALID);
118 * If ex modifies the screen after ex output is already on the screen
119 * or if we've switched into ex canonical mode, don't touch it -- we'll
120 * get scrolling wrong, at best.
122 if (!F_ISSET(sp, SC_TINPUT_INFO) &&
123 (F_ISSET(sp, SC_SCR_EXWROTE) || VIP(sp)->totalcount > 1)) {
124 F_SET(vip, VIP_N_EX_REDRAW);
128 /* Save and restore the cursor for these routines. */
129 (void)sp->gp->scr_cursor(sp, &oldy, &oldx);
133 if (vs_sm_delete(sp, lno))
137 F_SET(vip, VIP_N_RENUMBER);
140 if (vs_sm_insert(sp, lno))
144 F_SET(vip, VIP_N_RENUMBER);
147 if (vs_sm_reset(sp, lno))
154 (void)sp->gp->scr_move(sp, oldy, oldx);
160 * Fill in the screen map, placing the specified line at the
161 * right position. There isn't any way to tell if an SMAP
162 * entry has been filled in, so this routine had better be
163 * called with P_FILL set before anything else is done.
166 * Unexported interface: if lno is OOBLNO, P_TOP means that the HMAP
167 * slot is already filled in, P_BOTTOM means that the TMAP slot is
168 * already filled in, and we just finish up the job.
170 * PUBLIC: int vs_sm_fill(SCR *, recno_t, pos_t);
173 vs_sm_fill(SCR *sp, recno_t lno, pos_t pos)
178 /* Flush all cached information from the SMAP. */
179 for (p = HMAP, cnt = sp->t_rows; cnt--; ++p)
183 * If the map is filled, the screen must be redrawn.
186 * This is a bug. We should try and figure out if the desired line
187 * is already in the map or close by -- scrolling the screen would
188 * be a lot better than redrawing.
190 F_SET(sp, SC_SCR_REDRAW);
198 /* See if less than half a screen from the top. */
200 &tmp, lno, HALFTEXT(sp)) <= HALFTEXT(sp)) {
205 /* See if less than half a screen from the bottom. */
206 if (db_last(sp, &tmp.lno))
209 tmp.soff = vs_screens(sp, tmp.lno, NULL);
211 &tmp, lno, HALFTEXT(sp)) <= HALFTEXT(sp)) {
213 TMAP->coff = tmp.coff;
214 TMAP->soff = tmp.soff;
220 top: HMAP->lno = lno;
225 * If number of lines HMAP->lno (top line) spans
226 * changed due to, say reformatting, and now is
227 * fewer than HMAP->soff, reset so the line is
228 * redrawn at the top of the screen.
230 cnt = vs_screens(sp, HMAP->lno, NULL);
231 if (cnt < HMAP->soff)
234 /* If we fail, just punt. */
235 for (p = HMAP, cnt = sp->t_rows; --cnt; ++p)
236 if (vs_sm_next(sp, p, p + 1))
240 /* If we fail, guess that the file is too small. */
241 middle: p = HMAP + sp->t_rows / 2;
245 for (; p > HMAP; --p)
246 if (vs_sm_prev(sp, p, p - 1)) {
251 /* If we fail, just punt. */
252 p = HMAP + sp->t_rows / 2;
253 for (; p < TMAP; ++p)
254 if (vs_sm_next(sp, p, p + 1))
261 TMAP->soff = vs_screens(sp, lno, NULL);
263 /* If we fail, guess that the file is too small. */
264 bottom: for (p = TMAP; p > HMAP; --p)
265 if (vs_sm_prev(sp, p, p - 1)) {
276 * Try and put *something* on the screen. If this fails, we have a
277 * serious hard error.
282 for (p = HMAP; p < TMAP; ++p)
283 if (vs_sm_next(sp, p, p + 1))
289 * For the routines vs_sm_reset, vs_sm_delete and vs_sm_insert: if the
290 * screen contains only a single line (whether because the screen is small
291 * or the line large), it gets fairly exciting. Skip the fun, set a flag
292 * so the screen map is refilled and the screen redrawn, and return. This
293 * is amazingly slow, but it's not clear that anyone will care.
295 #define HANDLE_WEIRDNESS(cnt) { \
296 if (cnt >= sp->t_rows) { \
297 F_SET(sp, SC_SCR_REFORMAT); \
304 * Delete a line out of the SMAP.
307 vs_sm_delete(SCR *sp, recno_t lno)
313 * Find the line in the map, and count the number of screen lines
314 * which display any part of the deleted line.
316 for (p = HMAP; p->lno != lno; ++p);
317 if (O_ISSET(sp, O_LEFTRIGHT))
320 for (cnt_orig = 1, t = p + 1;
321 t <= TMAP && t->lno == lno; ++cnt_orig, ++t);
323 HANDLE_WEIRDNESS(cnt_orig);
325 /* Delete that many lines from the screen. */
326 (void)sp->gp->scr_move(sp, p - HMAP, 0);
327 if (vs_deleteln(sp, cnt_orig))
330 /* Shift the screen map up. */
331 memmove(p, p + cnt_orig, (((TMAP - p) - cnt_orig) + 1) * sizeof(SMAP));
333 /* Decrement the line numbers for the rest of the map. */
334 for (t = TMAP - cnt_orig; p <= t; ++p)
337 /* Display the new lines. */
338 for (p = TMAP - cnt_orig;;) {
339 if (p < TMAP && vs_sm_next(sp, p, p + 1))
341 /* vs_sm_next() flushed the cache. */
342 if (vs_line(sp, ++p, NULL, NULL))
352 * Insert a line into the SMAP.
355 vs_sm_insert(SCR *sp, recno_t lno)
358 size_t cnt_orig, cnt, coff;
360 /* Save the offset. */
364 * Find the line in the map, find out how many screen lines
365 * needed to display the line.
367 for (p = HMAP; p->lno != lno; ++p);
369 cnt_orig = vs_screens(sp, lno, NULL);
370 HANDLE_WEIRDNESS(cnt_orig);
373 * The lines left in the screen override the number of screen
374 * lines in the inserted line.
376 cnt = (TMAP - p) + 1;
380 /* Push down that many lines. */
381 (void)sp->gp->scr_move(sp, p - HMAP, 0);
382 if (vs_insertln(sp, cnt_orig))
385 /* Shift the screen map down. */
386 memmove(p + cnt_orig, p, (((TMAP - p) - cnt_orig) + 1) * sizeof(SMAP));
388 /* Increment the line numbers for the rest of the map. */
389 for (t = p + cnt_orig; t <= TMAP; ++t)
392 /* Fill in the SMAP for the new lines, and display. */
393 for (cnt = 1, t = p; cnt <= cnt_orig; ++t, ++cnt) {
398 if (vs_line(sp, t, NULL, NULL))
406 * Reset a line in the SMAP.
409 vs_sm_reset(SCR *sp, recno_t lno)
412 size_t cnt_orig, cnt_new, cnt, diff;
415 * See if the number of on-screen rows taken up by the old display
416 * for the line is the same as the number needed for the new one.
417 * If so, repaint, otherwise do it the hard way.
419 for (p = HMAP; p->lno != lno; ++p);
420 if (O_ISSET(sp, O_LEFTRIGHT)) {
422 cnt_orig = cnt_new = 1;
425 t = p; t <= TMAP && t->lno == lno; ++cnt_orig, ++t);
426 cnt_new = vs_screens(sp, lno, NULL);
429 HANDLE_WEIRDNESS(cnt_orig);
431 if (cnt_orig == cnt_new) {
434 if (vs_line(sp, p, NULL, NULL))
440 if (cnt_orig < cnt_new) {
441 /* Get the difference. */
442 diff = cnt_new - cnt_orig;
445 * The lines left in the screen override the number of screen
446 * lines in the inserted line.
448 cnt = (TMAP - p) + 1;
452 /* If there are any following lines, push them down. */
454 (void)sp->gp->scr_move(sp, p - HMAP, 0);
455 if (vs_insertln(sp, diff))
458 /* Shift the screen map down. */
460 (((TMAP - p) - diff) + 1) * sizeof(SMAP));
463 /* Fill in the SMAP for the replaced line, and display. */
464 for (cnt = 1, t = p; cnt_new-- && t <= TMAP; ++t, ++cnt) {
468 if (vs_line(sp, t, NULL, NULL))
472 /* Get the difference. */
473 diff = cnt_orig - cnt_new;
475 /* Delete that many lines from the screen. */
476 (void)sp->gp->scr_move(sp, p - HMAP, 0);
477 if (vs_deleteln(sp, diff))
480 /* Shift the screen map up. */
481 memmove(p, p + diff, (((TMAP - p) - diff) + 1) * sizeof(SMAP));
483 /* Fill in the SMAP for the replaced line, and display. */
484 for (cnt = 1, t = p; cnt_new--; ++t, ++cnt) {
488 if (vs_line(sp, t, NULL, NULL))
492 /* Display the new lines at the bottom of the screen. */
493 for (t = TMAP - diff;;) {
494 if (t < TMAP && vs_sm_next(sp, t, t + 1))
496 /* vs_sm_next() flushed the cache. */
497 if (vs_line(sp, ++t, NULL, NULL))
508 * Scroll the SMAP up/down count logical lines. Different
509 * semantics based on the vi command, *sigh*.
511 * PUBLIC: int vs_sm_scroll(SCR *, MARK *, recno_t, scroll_t);
514 vs_sm_scroll(SCR *sp, MARK *rp, recno_t count, scroll_t scmd)
519 * Invalidate the cursor. The line is probably going to change,
520 * (although for ^E and ^Y it may not). In any case, the scroll
521 * routines move the cursor to draw things.
523 F_SET(VIP(sp), VIP_CUR_INVALID);
525 /* Find the cursor in the screen. */
526 if (vs_sm_cursor(sp, &smp))
534 if (vs_sm_down(sp, rp, count, scmd, smp))
541 if (vs_sm_up(sp, rp, count, scmd, smp))
550 * If we're at the start of a line, go for the first non-blank.
551 * This makes it look like the old vi, even though we're moving
552 * around by logical lines, not physical ones.
555 * In the presence of a long line, which has more than a screen
556 * width of leading spaces, this code can cause a cursor warp.
559 if (scmd != CNTRL_E && scmd != CNTRL_Y &&
560 rp->cno == 0 && nonblank(sp, rp->lno, &rp->cno))
568 * Scroll the SMAP up count logical lines.
571 vs_sm_up(SCR *sp, MARK *rp, recno_t count, scroll_t scmd, SMAP *smp)
573 int cursor_set, echanged, zset;
577 * Check to see if movement is possible.
579 * Get the line after the map. If that line is a new one (and if
580 * O_LEFTRIGHT option is set, this has to be true), and the next
581 * line doesn't exist, and the cursor doesn't move, or the cursor
582 * isn't even on the screen, or the cursor is already at the last
583 * line in the map, it's an error. If that test succeeded because
584 * the cursor wasn't at the end of the map, test to see if the map
587 if (vs_sm_next(sp, TMAP, &s1))
589 if (s1.lno > TMAP->lno && !db_exist(sp, s1.lno)) {
590 if (scmd == CNTRL_E || scmd == Z_PLUS || smp == TMAP) {
594 if (vs_sm_next(sp, smp, &s1))
596 if (s1.lno > smp->lno && !db_exist(sp, s1.lno)) {
603 * Small screens: see vs_refresh.c section 6a.
605 * If it's a small screen, and the movement isn't larger than a
606 * screen, i.e some context will remain, open up the screen and
607 * display by scrolling. In this case, the cursor moves down one
608 * line for each line displayed. Otherwise, erase/compress and
609 * repaint, and move the cursor to the first line in the screen.
610 * Note, the ^F command is always in the latter case, for historical
615 if (count >= sp->t_maxrows || scmd == CNTRL_F) {
619 for (; count--; s1 = s2) {
620 if (vs_sm_next(sp, &s1, &s2))
622 if (s2.lno != s1.lno && !db_exist(sp, s2.lno))
626 if (vs_sm_fill(sp, OOBLNO, P_BOTTOM))
628 return (vs_sm_position(sp, rp, 0, P_TOP));
630 cursor_set = scmd == CNTRL_E || vs_sm_cursor(sp, &ssmp);
632 sp->t_rows != sp->t_maxrows; --count, ++sp->t_rows) {
633 if (vs_sm_next(sp, TMAP, &s1))
635 if (TMAP->lno != s1.lno && !db_exist(sp, s1.lno))
638 /* vs_sm_next() flushed the cache. */
639 if (vs_line(sp, TMAP, NULL, NULL))
647 rp->cno = ssmp->c_sboff;
653 for (echanged = zset = 0; count; --count) {
654 /* Decide what would show up on the screen. */
655 if (vs_sm_next(sp, TMAP, &s1))
658 /* If the line doesn't exist, we're done. */
659 if (TMAP->lno != s1.lno && !db_exist(sp, s1.lno))
662 /* Scroll the screen cursor up one logical line. */
692 * On a ^E that was forced to change lines, try and keep the
693 * cursor as close as possible to the last position, but also
694 * set it up so that the next "real" movement will return the
695 * cursor to the closest position to the last real movement.
699 rp->cno = vs_colpos(sp, smp->lno,
700 (O_ISSET(sp, O_LEFTRIGHT) ?
701 smp->coff : (smp->soff - 1) * sp->cols) +
707 * If there are more lines, the ^F command is positioned at
708 * the first line of the screen.
717 * The ^D and ^F commands move the cursor towards EOF
718 * if there are more lines to move. Check to be sure
719 * the lines actually exist. (They may not if the
720 * file is smaller than the screen.)
722 for (; count; --count, ++smp)
723 if (smp == TMAP || !db_exist(sp, smp[1].lno))
727 /* The z+ command moves the cursor to the first new line. */
733 if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
736 rp->cno = smp->c_scoff == 255 ? 0 : smp->c_sboff;
742 * Scroll the SMAP up one.
744 * PUBLIC: int vs_sm_1up(SCR *);
750 * Delete the top line of the screen. Shift the screen map
751 * up and display a new line at the bottom of the screen.
753 (void)sp->gp->scr_move(sp, 0, 0);
754 if (vs_deleteln(sp, 1))
757 /* One-line screens can fail. */
758 if (IS_ONELINE(sp)) {
759 if (vs_sm_next(sp, TMAP, TMAP))
762 memmove(HMAP, HMAP + 1, (sp->rows - 1) * sizeof(SMAP));
763 if (vs_sm_next(sp, TMAP - 1, TMAP))
766 /* vs_sm_next() flushed the cache. */
767 return (vs_line(sp, TMAP, NULL, NULL));
772 * Delete a line a la curses, make sure to put the information
773 * line and other screens back.
776 vs_deleteln(SCR *sp, int cnt)
783 /* If the screen is vertically split, we can't scroll it. */
785 F_SET(sp, SC_SCR_REDRAW);
790 (void)gp->scr_clrtoeol(sp);
792 (void)gp->scr_cursor(sp, &oldy, &oldx);
794 (void)gp->scr_deleteln(sp);
795 (void)gp->scr_move(sp, LASTLINE(sp), 0);
796 (void)gp->scr_insertln(sp);
797 (void)gp->scr_move(sp, oldy, oldx);
805 * Scroll the SMAP down count logical lines.
808 vs_sm_down(SCR *sp, MARK *rp, recno_t count, scroll_t scmd, SMAP *smp)
811 int cursor_set, ychanged, zset;
813 /* Check to see if movement is possible. */
814 if (HMAP->lno == 1 &&
815 (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1) &&
816 (scmd == CNTRL_Y || scmd == Z_CARAT || smp == HMAP)) {
822 * Small screens: see vs_refresh.c section 6a.
824 * If it's a small screen, and the movement isn't larger than a
825 * screen, i.e some context will remain, open up the screen and
826 * display by scrolling. In this case, the cursor moves up one
827 * line for each line displayed. Otherwise, erase/compress and
828 * repaint, and move the cursor to the first line in the screen.
829 * Note, the ^B command is always in the latter case, for historical
832 cursor_set = scmd == CNTRL_Y;
834 if (count >= sp->t_maxrows || scmd == CNTRL_B) {
838 for (; count--; s1 = s2) {
839 if (vs_sm_prev(sp, &s1, &s2))
842 (O_ISSET(sp, O_LEFTRIGHT) || s2.soff == 1))
846 if (vs_sm_fill(sp, OOBLNO, P_TOP))
848 return (vs_sm_position(sp, rp, 0, P_BOTTOM));
850 cursor_set = scmd == CNTRL_Y || vs_sm_cursor(sp, &ssmp);
852 sp->t_rows != sp->t_maxrows; --count, ++sp->t_rows) {
853 if (HMAP->lno == 1 &&
854 (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1))
862 rp->cno = ssmp->c_sboff;
868 for (ychanged = zset = 0; count; --count) {
869 /* If the line doesn't exist, we're done. */
870 if (HMAP->lno == 1 &&
871 (O_ISSET(sp, O_LEFTRIGHT) || HMAP->soff == 1))
874 /* Scroll the screen and cursor down one logical line. */
898 if (scmd != CNTRL_Y && cursor_set)
904 * If there are more lines, the ^B command is positioned at
905 * the last line of the screen. However, the line may not
909 for (smp = TMAP; smp > HMAP; --smp)
910 if (db_exist(sp, smp->lno))
917 * The ^B and ^U commands move the cursor towards SOF
918 * if there are more lines to move.
920 if (count < smp - HMAP)
927 * On a ^Y that was forced to change lines, try and keep the
928 * cursor as close as possible to the last position, but also
929 * set it up so that the next "real" movement will return the
930 * cursor to the closest position to the last real movement.
934 rp->cno = vs_colpos(sp, smp->lno,
935 (O_ISSET(sp, O_LEFTRIGHT) ?
936 smp->coff : (smp->soff - 1) * sp->cols) +
941 /* The z^ command moves the cursor to the first new line. */
947 if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
950 rp->cno = smp->c_scoff == 255 ? 0 : smp->c_sboff;
956 * Erase the small screen area for the scrolling functions.
964 (void)gp->scr_move(sp, LASTLINE(sp), 0);
965 (void)gp->scr_clrtoeol(sp);
966 for (; sp->t_rows > sp->t_minrows; --sp->t_rows, --TMAP) {
967 (void)gp->scr_move(sp, TMAP - HMAP, 0);
968 (void)gp->scr_clrtoeol(sp);
975 * Scroll the SMAP down one.
977 * PUBLIC: int vs_sm_1down(SCR *);
983 * Insert a line at the top of the screen. Shift the screen map
984 * down and display a new line at the top of the screen.
986 (void)sp->gp->scr_move(sp, 0, 0);
987 if (vs_insertln(sp, 1))
990 /* One-line screens can fail. */
991 if (IS_ONELINE(sp)) {
992 if (vs_sm_prev(sp, HMAP, HMAP))
995 memmove(HMAP + 1, HMAP, (sp->rows - 1) * sizeof(SMAP));
996 if (vs_sm_prev(sp, HMAP + 1, HMAP))
999 /* vs_sm_prev() flushed the cache. */
1000 return (vs_line(sp, HMAP, NULL, NULL));
1005 * Insert a line a la curses, make sure to put the information
1006 * line and other screens back.
1009 vs_insertln(SCR *sp, int cnt)
1016 /* If the screen is vertically split, we can't scroll it. */
1017 if (IS_VSPLIT(sp)) {
1018 F_SET(sp, SC_SCR_REDRAW);
1022 if (IS_ONELINE(sp)) {
1023 (void)gp->scr_move(sp, LASTLINE(sp), 0);
1024 (void)gp->scr_clrtoeol(sp);
1026 (void)gp->scr_cursor(sp, &oldy, &oldx);
1028 (void)gp->scr_move(sp, LASTLINE(sp) - 1, 0);
1029 (void)gp->scr_deleteln(sp);
1030 (void)gp->scr_move(sp, oldy, oldx);
1031 (void)gp->scr_insertln(sp);
1039 * Fill in the next entry in the SMAP.
1041 * PUBLIC: int vs_sm_next(SCR *, SMAP *, SMAP *);
1044 vs_sm_next(SCR *sp, SMAP *p, SMAP *t)
1049 if (O_ISSET(sp, O_LEFTRIGHT)) {
1050 t->lno = p->lno + 1;
1053 lcnt = vs_screens(sp, p->lno, NULL);
1054 if (lcnt == p->soff) {
1055 t->lno = p->lno + 1;
1059 t->soff = p->soff + 1;
1067 * Fill in the previous entry in the SMAP.
1069 * PUBLIC: int vs_sm_prev(SCR *, SMAP *, SMAP *);
1072 vs_sm_prev(SCR *sp, SMAP *p, SMAP *t)
1075 if (O_ISSET(sp, O_LEFTRIGHT)) {
1076 t->lno = p->lno - 1;
1081 t->soff = p->soff - 1;
1083 t->lno = p->lno - 1;
1084 t->soff = vs_screens(sp, t->lno, NULL);
1087 return (t->lno == 0);
1092 * Return the SMAP entry referenced by the cursor.
1094 * PUBLIC: int vs_sm_cursor(SCR *, SMAP **);
1097 vs_sm_cursor(SCR *sp, SMAP **smpp)
1101 /* See if the cursor is not in the map. */
1102 if (sp->lno < HMAP->lno || sp->lno > TMAP->lno)
1105 /* Find the first occurence of the line. */
1106 for (p = HMAP; p->lno != sp->lno; ++p);
1108 /* Fill in the map information until we find the right line. */
1109 for (; p <= TMAP; ++p) {
1110 /* Short lines are common and easy to detect. */
1111 if (p != TMAP && (p + 1)->lno != p->lno) {
1115 if (!SMAP_CACHE(p) && vs_line(sp, p, NULL, NULL))
1117 if (p->c_eboff >= sp->cno) {
1123 /* It was past the end of the map after all. */
1129 * Return the line/column of the top, middle or last line on the screen.
1130 * (The vi H, M and L commands.) Here because only the screen routines
1131 * know what's really out there.
1133 * PUBLIC: int vs_sm_position(SCR *, MARK *, u_long, pos_t);
1136 vs_sm_position(SCR *sp, MARK *rp, u_long cnt, pos_t pos)
1145 * Historically, an invalid count to the H command failed.
1146 * We do nothing special here, just making sure that H in
1147 * an empty screen works.
1149 if (cnt > TMAP - HMAP)
1152 if (cnt && !db_exist(sp, smp->lno)) {
1153 sof: msgq(sp, M_BERR, "220|Movement past the end-of-screen");
1160 * Historically, a count to the M command was ignored.
1161 * If the screen isn't filled, find the middle of what's
1162 * real and move there.
1164 if (!db_exist(sp, TMAP->lno)) {
1165 if (db_last(sp, &last))
1167 for (smp = TMAP; smp->lno > last && smp > HMAP; --smp);
1169 smp -= (smp - HMAP) / 2;
1171 smp = (HMAP + (TMAP - HMAP) / 2) + cnt;
1176 * Historically, an invalid count to the L command failed.
1177 * If the screen isn't filled, find the bottom of what's
1178 * real and try to offset from there.
1180 if (cnt > TMAP - HMAP)
1183 if (!db_exist(sp, smp->lno)) {
1184 if (db_last(sp, &last))
1186 for (; smp->lno > last && smp > HMAP; --smp);
1187 if (cnt > smp - HMAP) {
1188 eof: msgq(sp, M_BERR,
1189 "221|Movement past the beginning-of-screen");
1199 /* Make sure that the cached information is valid. */
1200 if (!SMAP_CACHE(smp) && vs_line(sp, smp, NULL, NULL))
1203 rp->cno = smp->c_sboff;
1210 * Return the number of screen lines from an SMAP entry to the
1211 * start of some file line, less than a maximum value.
1213 * PUBLIC: recno_t vs_sm_nlines(SCR *, SMAP *, recno_t, size_t);
1216 vs_sm_nlines(SCR *sp, SMAP *from_sp, recno_t to_lno, size_t max)
1220 if (O_ISSET(sp, O_LEFTRIGHT))
1221 return (from_sp->lno > to_lno ?
1222 from_sp->lno - to_lno : to_lno - from_sp->lno);
1224 if (from_sp->lno == to_lno)
1225 return (from_sp->soff - 1);
1227 if (from_sp->lno > to_lno) {
1228 lcnt = from_sp->soff - 1; /* Correct for off-by-one. */
1229 for (lno = from_sp->lno; --lno >= to_lno && lcnt <= max;)
1230 lcnt += vs_screens(sp, lno, NULL);
1233 lcnt = (vs_screens(sp, lno, NULL) - from_sp->soff) + 1;
1234 for (; ++lno < to_lno && lcnt <= max;)
1235 lcnt += vs_screens(sp, lno, NULL);