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.
13 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 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
26 #include "../common/common.h"
29 static int vs_deleteln
__P((SCR
*, int));
30 static int vs_insertln
__P((SCR
*, int));
31 static int vs_sm_delete
__P((SCR
*, db_recno_t
));
32 static int vs_sm_down
__P((SCR
*, MARK
*, db_recno_t
, scroll_t
, SMAP
*));
33 static int vs_sm_erase
__P((SCR
*));
34 static int vs_sm_insert
__P((SCR
*, db_recno_t
));
35 static int vs_sm_reset
__P((SCR
*, db_recno_t
));
36 static int vs_sm_up
__P((SCR
*, MARK
*, db_recno_t
, scroll_t
, SMAP
*));
40 * Make a change to the screen.
42 * PUBLIC: int vs_change __P((SCR *, db_recno_t, lnop_t));
45 vs_change(SCR
*sp
, db_recno_t lno
, lnop_t op
)
49 size_t cnt
, oldy
, oldx
;
55 * Very nasty special case. The historic vi code displays a single
56 * space (or a '$' if the list option is set) for the first line in
57 * an "empty" file. If we "insert" a line, that line gets scrolled
58 * down, not repainted, so it's incorrect when we refresh the screen.
59 * The vi text input functions detect it explicitly and don't insert
62 * Check for line #2 before going to the end of the file.
64 if (((op
== LINE_APPEND
&& lno
== 0) ||
65 (op
== LINE_INSERT
&& lno
== 1)) &&
71 /* Appending is the same as inserting, if the line is incremented. */
72 if (op
== LINE_APPEND
) {
77 /* Ignore the change if the line is after the map. */
82 * If the line is before the map, and it's a decrement, decrement
83 * the map. If it's an increment, increment the map. Otherwise,
86 if (lno
< HMAP
->lno
) {
92 for (p
= HMAP
, cnt
= sp
->t_rows
; cnt
--; ++p
)
96 F_SET(vip
, VIP_N_RENUMBER
);
99 for (p
= HMAP
, cnt
= sp
->t_rows
; cnt
--; ++p
)
103 F_SET(vip
, VIP_N_RENUMBER
);
111 F_SET(vip
, VIP_N_REFRESH
);
114 * Invalidate the line size cache, and invalidate the cursor if it's
119 F_SET(vip
, VIP_CUR_INVALID
);
122 * If ex modifies the screen after ex output is already on the screen
123 * or if we've switched into ex canonical mode, don't touch it -- we'll
124 * get scrolling wrong, at best.
126 if (!F_ISSET(sp
, SC_TINPUT_INFO
) &&
127 (F_ISSET(sp
, SC_SCR_EXWROTE
) || VIP(sp
)->totalcount
> 1)) {
128 F_SET(vip
, VIP_N_EX_REDRAW
);
132 /* Save and restore the cursor for these routines. */
133 (void)sp
->gp
->scr_cursor(sp
, &oldy
, &oldx
);
137 if (vs_sm_delete(sp
, lno
))
141 F_SET(vip
, VIP_N_RENUMBER
);
144 if (vs_sm_insert(sp
, lno
))
148 F_SET(vip
, VIP_N_RENUMBER
);
151 if (vs_sm_reset(sp
, lno
))
158 (void)sp
->gp
->scr_move(sp
, oldy
, oldx
);
164 * Fill in the screen map, placing the specified line at the
165 * right position. There isn't any way to tell if an SMAP
166 * entry has been filled in, so this routine had better be
167 * called with P_FILL set before anything else is done.
170 * Unexported interface: if lno is OOBLNO, P_TOP means that the HMAP
171 * slot is already filled in, P_BOTTOM means that the TMAP slot is
172 * already filled in, and we just finish up the job.
174 * PUBLIC: int vs_sm_fill __P((SCR *, db_recno_t, pos_t));
177 vs_sm_fill(SCR
*sp
, db_recno_t lno
, pos_t pos
)
182 /* Flush all cached information from the SMAP. */
183 for (p
= HMAP
, cnt
= sp
->t_rows
; cnt
--; ++p
)
187 * If the map is filled, the screen must be redrawn.
190 * This is a bug. We should try and figure out if the desired line
191 * is already in the map or close by -- scrolling the screen would
192 * be a lot better than redrawing.
194 F_SET(sp
, SC_SCR_REDRAW
);
202 /* See if less than half a screen from the top. */
204 &tmp
, lno
, HALFTEXT(sp
)) <= HALFTEXT(sp
)) {
209 /* See if less than half a screen from the bottom. */
210 if (db_last(sp
, &tmp
.lno
))
213 tmp
.soff
= vs_screens(sp
, tmp
.lno
, NULL
);
215 &tmp
, lno
, HALFTEXT(sp
)) <= HALFTEXT(sp
)) {
217 TMAP
->coff
= tmp
.coff
;
218 TMAP
->soff
= tmp
.soff
;
224 top
: HMAP
->lno
= lno
;
228 /* If we fail, just punt. */
229 for (p
= HMAP
, cnt
= sp
->t_rows
; --cnt
; ++p
)
230 if (vs_sm_next(sp
, p
, p
+ 1))
234 /* If we fail, guess that the file is too small. */
235 middle
: p
= HMAP
+ sp
->t_rows
/ 2;
239 for (; p
> HMAP
; --p
)
240 if (vs_sm_prev(sp
, p
, p
- 1)) {
245 /* If we fail, just punt. */
246 p
= HMAP
+ sp
->t_rows
/ 2;
247 for (; p
< TMAP
; ++p
)
248 if (vs_sm_next(sp
, p
, p
+ 1))
255 TMAP
->soff
= vs_screens(sp
, lno
, NULL
);
257 /* If we fail, guess that the file is too small. */
258 bottom
: for (p
= TMAP
; p
> HMAP
; --p
)
259 if (vs_sm_prev(sp
, p
, p
- 1)) {
270 * Try and put *something* on the screen. If this fails, we have a
271 * serious hard error.
276 for (p
= HMAP
; p
< TMAP
; ++p
)
277 if (vs_sm_next(sp
, p
, p
+ 1))
283 * For the routines vs_sm_reset, vs_sm_delete and vs_sm_insert: if the
284 * screen contains only a single line (whether because the screen is small
285 * or the line large), it gets fairly exciting. Skip the fun, set a flag
286 * so the screen map is refilled and the screen redrawn, and return. This
287 * is amazingly slow, but it's not clear that anyone will care.
289 #define HANDLE_WEIRDNESS(cnt) { \
290 if (cnt >= sp->t_rows) { \
291 F_SET(sp, SC_SCR_REFORMAT); \
298 * Delete a line out of the SMAP.
301 vs_sm_delete(SCR
*sp
, db_recno_t lno
)
307 * Find the line in the map, and count the number of screen lines
308 * which display any part of the deleted line.
310 for (p
= HMAP
; p
->lno
!= lno
; ++p
);
311 if (O_ISSET(sp
, O_LEFTRIGHT
))
314 for (cnt_orig
= 1, t
= p
+ 1;
315 t
<= TMAP
&& t
->lno
== lno
; ++cnt_orig
, ++t
);
317 HANDLE_WEIRDNESS(cnt_orig
);
319 /* Delete that many lines from the screen. */
320 (void)sp
->gp
->scr_move(sp
, p
- HMAP
, 0);
321 if (vs_deleteln(sp
, cnt_orig
))
324 /* Shift the screen map up. */
325 memmove(p
, p
+ cnt_orig
, (((TMAP
- p
) - cnt_orig
) + 1) * sizeof(SMAP
));
327 /* Decrement the line numbers for the rest of the map. */
328 for (t
= TMAP
- cnt_orig
; p
<= t
; ++p
)
331 /* Display the new lines. */
332 for (p
= TMAP
- cnt_orig
;;) {
333 if (p
< TMAP
&& vs_sm_next(sp
, p
, p
+ 1))
335 /* vs_sm_next() flushed the cache. */
336 if (vs_line(sp
, ++p
, NULL
, NULL
))
346 * Insert a line into the SMAP.
349 vs_sm_insert(SCR
*sp
, db_recno_t lno
)
352 size_t cnt_orig
, cnt
, coff
;
354 /* Save the offset. */
358 * Find the line in the map, find out how many screen lines
359 * needed to display the line.
361 for (p
= HMAP
; p
->lno
!= lno
; ++p
);
363 cnt_orig
= vs_screens(sp
, lno
, NULL
);
364 HANDLE_WEIRDNESS(cnt_orig
);
367 * The lines left in the screen override the number of screen
368 * lines in the inserted line.
370 cnt
= (TMAP
- p
) + 1;
374 /* Push down that many lines. */
375 (void)sp
->gp
->scr_move(sp
, p
- HMAP
, 0);
376 if (vs_insertln(sp
, cnt_orig
))
379 /* Shift the screen map down. */
380 memmove(p
+ cnt_orig
, p
, (((TMAP
- p
) - cnt_orig
) + 1) * sizeof(SMAP
));
382 /* Increment the line numbers for the rest of the map. */
383 for (t
= p
+ cnt_orig
; t
<= TMAP
; ++t
)
386 /* Fill in the SMAP for the new lines, and display. */
387 for (cnt
= 1, t
= p
; cnt
<= cnt_orig
; ++t
, ++cnt
) {
392 if (vs_line(sp
, t
, NULL
, NULL
))
400 * Reset a line in the SMAP.
403 vs_sm_reset(SCR
*sp
, db_recno_t lno
)
406 size_t cnt_orig
, cnt_new
, cnt
, diff
;
409 * See if the number of on-screen rows taken up by the old display
410 * for the line is the same as the number needed for the new one.
411 * If so, repaint, otherwise do it the hard way.
413 for (p
= HMAP
; p
->lno
!= lno
; ++p
);
414 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
416 cnt_orig
= cnt_new
= 1;
419 t
= p
; t
<= TMAP
&& t
->lno
== lno
; ++cnt_orig
, ++t
);
420 cnt_new
= vs_screens(sp
, lno
, NULL
);
423 HANDLE_WEIRDNESS(cnt_orig
);
425 if (cnt_orig
== cnt_new
) {
428 if (vs_line(sp
, p
, NULL
, NULL
))
434 if (cnt_orig
< cnt_new
) {
435 /* Get the difference. */
436 diff
= cnt_new
- cnt_orig
;
439 * The lines left in the screen override the number of screen
440 * lines in the inserted line.
442 cnt
= (TMAP
- p
) + 1;
446 /* If there are any following lines, push them down. */
448 (void)sp
->gp
->scr_move(sp
, p
- HMAP
, 0);
449 if (vs_insertln(sp
, diff
))
452 /* Shift the screen map down. */
454 (((TMAP
- p
) - diff
) + 1) * sizeof(SMAP
));
457 /* Fill in the SMAP for the replaced line, and display. */
458 for (cnt
= 1, t
= p
; cnt_new
-- && t
<= TMAP
; ++t
, ++cnt
) {
462 if (vs_line(sp
, t
, NULL
, NULL
))
466 /* Get the difference. */
467 diff
= cnt_orig
- cnt_new
;
469 /* Delete that many lines from the screen. */
470 (void)sp
->gp
->scr_move(sp
, p
- HMAP
, 0);
471 if (vs_deleteln(sp
, diff
))
474 /* Shift the screen map up. */
475 memmove(p
, p
+ diff
, (((TMAP
- p
) - diff
) + 1) * sizeof(SMAP
));
477 /* Fill in the SMAP for the replaced line, and display. */
478 for (cnt
= 1, t
= p
; cnt_new
--; ++t
, ++cnt
) {
482 if (vs_line(sp
, t
, NULL
, NULL
))
486 /* Display the new lines at the bottom of the screen. */
487 for (t
= TMAP
- diff
;;) {
488 if (t
< TMAP
&& vs_sm_next(sp
, t
, t
+ 1))
490 /* vs_sm_next() flushed the cache. */
491 if (vs_line(sp
, ++t
, NULL
, NULL
))
502 * Scroll the SMAP up/down count logical lines. Different
503 * semantics based on the vi command, *sigh*.
505 * PUBLIC: int vs_sm_scroll __P((SCR *, MARK *, db_recno_t, scroll_t));
508 vs_sm_scroll(SCR
*sp
, MARK
*rp
, db_recno_t count
, scroll_t scmd
)
513 * Invalidate the cursor. The line is probably going to change,
514 * (although for ^E and ^Y it may not). In any case, the scroll
515 * routines move the cursor to draw things.
517 F_SET(VIP(sp
), VIP_CUR_INVALID
);
519 /* Find the cursor in the screen. */
520 if (vs_sm_cursor(sp
, &smp
))
528 if (vs_sm_down(sp
, rp
, count
, scmd
, smp
))
535 if (vs_sm_up(sp
, rp
, count
, scmd
, smp
))
544 * If we're at the start of a line, go for the first non-blank.
545 * This makes it look like the old vi, even though we're moving
546 * around by logical lines, not physical ones.
549 * In the presence of a long line, which has more than a screen
550 * width of leading spaces, this code can cause a cursor warp.
553 if (scmd
!= CNTRL_E
&& scmd
!= CNTRL_Y
&&
554 rp
->cno
== 0 && nonblank(sp
, rp
->lno
, &rp
->cno
))
562 * Scroll the SMAP up count logical lines.
565 vs_sm_up(SCR
*sp
, MARK
*rp
, db_recno_t count
, scroll_t scmd
, SMAP
*smp
)
567 int cursor_set
, echanged
, zset
;
571 * Check to see if movement is possible.
573 * Get the line after the map. If that line is a new one (and if
574 * O_LEFTRIGHT option is set, this has to be true), and the next
575 * line doesn't exist, and the cursor doesn't move, or the cursor
576 * isn't even on the screen, or the cursor is already at the last
577 * line in the map, it's an error. If that test succeeded because
578 * the cursor wasn't at the end of the map, test to see if the map
581 if (vs_sm_next(sp
, TMAP
, &s1
))
583 if (s1
.lno
> TMAP
->lno
&& !db_exist(sp
, s1
.lno
)) {
584 if (scmd
== CNTRL_E
|| scmd
== Z_PLUS
|| smp
== TMAP
) {
588 if (vs_sm_next(sp
, smp
, &s1
))
590 if (s1
.lno
> smp
->lno
&& !db_exist(sp
, s1
.lno
)) {
597 * Small screens: see vs_refresh.c section 6a.
599 * If it's a small screen, and the movement isn't larger than a
600 * screen, i.e some context will remain, open up the screen and
601 * display by scrolling. In this case, the cursor moves down one
602 * line for each line displayed. Otherwise, erase/compress and
603 * repaint, and move the cursor to the first line in the screen.
604 * Note, the ^F command is always in the latter case, for historical
609 if (count
>= sp
->t_maxrows
|| scmd
== CNTRL_F
) {
613 for (; count
--; s1
= s2
) {
614 if (vs_sm_next(sp
, &s1
, &s2
))
616 if (s2
.lno
!= s1
.lno
&& !db_exist(sp
, s2
.lno
))
620 if (vs_sm_fill(sp
, OOBLNO
, P_BOTTOM
))
622 return (vs_sm_position(sp
, rp
, 0, P_TOP
));
624 cursor_set
= scmd
== CNTRL_E
|| vs_sm_cursor(sp
, &ssmp
);
626 sp
->t_rows
!= sp
->t_maxrows
; --count
, ++sp
->t_rows
) {
627 if (vs_sm_next(sp
, TMAP
, &s1
))
629 if (TMAP
->lno
!= s1
.lno
&& !db_exist(sp
, s1
.lno
))
632 /* vs_sm_next() flushed the cache. */
633 if (vs_line(sp
, TMAP
, NULL
, NULL
))
641 rp
->cno
= ssmp
->c_sboff
;
647 for (echanged
= zset
= 0; count
; --count
) {
648 /* Decide what would show up on the screen. */
649 if (vs_sm_next(sp
, TMAP
, &s1
))
652 /* If the line doesn't exist, we're done. */
653 if (TMAP
->lno
!= s1
.lno
&& !db_exist(sp
, s1
.lno
))
656 /* Scroll the screen cursor up one logical line. */
686 * On a ^E that was forced to change lines, try and keep the
687 * cursor as close as possible to the last position, but also
688 * set it up so that the next "real" movement will return the
689 * cursor to the closest position to the last real movement.
693 rp
->cno
= vs_colpos(sp
, smp
->lno
,
694 (O_ISSET(sp
, O_LEFTRIGHT
) ?
695 smp
->coff
: (smp
->soff
- 1) * sp
->cols
) +
701 * If there are more lines, the ^F command is positioned at
702 * the first line of the screen.
711 * The ^D and ^F commands move the cursor towards EOF
712 * if there are more lines to move. Check to be sure
713 * the lines actually exist. (They may not if the
714 * file is smaller than the screen.)
716 for (; count
; --count
, ++smp
)
717 if (smp
== TMAP
|| !db_exist(sp
, smp
[1].lno
))
721 /* The z+ command moves the cursor to the first new line. */
727 if (!SMAP_CACHE(smp
) && vs_line(sp
, smp
, NULL
, NULL
))
730 rp
->cno
= smp
->c_scoff
== 255 ? 0 : smp
->c_sboff
;
736 * Scroll the SMAP up one.
738 * PUBLIC: int vs_sm_1up __P((SCR *));
744 * Delete the top line of the screen. Shift the screen map
745 * up and display a new line at the bottom of the screen.
747 (void)sp
->gp
->scr_move(sp
, 0, 0);
748 if (vs_deleteln(sp
, 1))
751 /* One-line screens can fail. */
752 if (IS_ONELINE(sp
)) {
753 if (vs_sm_next(sp
, TMAP
, TMAP
))
756 memmove(HMAP
, HMAP
+ 1, (sp
->rows
- 1) * sizeof(SMAP
));
757 if (vs_sm_next(sp
, TMAP
- 1, TMAP
))
760 /* vs_sm_next() flushed the cache. */
761 return (vs_line(sp
, TMAP
, NULL
, NULL
));
766 * Delete a line a la curses, make sure to put the information
767 * line and other screens back.
770 vs_deleteln(SCR
*sp
, int cnt
)
777 /* If the screen is vertically split, we can't scroll it. */
779 F_SET(sp
, SC_SCR_REDRAW
);
784 (void)gp
->scr_clrtoeol(sp
);
786 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
788 (void)gp
->scr_deleteln(sp
);
789 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
790 (void)gp
->scr_insertln(sp
);
791 (void)gp
->scr_move(sp
, oldy
, oldx
);
799 * Scroll the SMAP down count logical lines.
802 vs_sm_down(SCR
*sp
, MARK
*rp
, db_recno_t count
, scroll_t scmd
, SMAP
*smp
)
805 int cursor_set
, ychanged
, zset
;
807 /* Check to see if movement is possible. */
808 if (HMAP
->lno
== 1 &&
809 (O_ISSET(sp
, O_LEFTRIGHT
) || HMAP
->soff
== 1) &&
810 (scmd
== CNTRL_Y
|| scmd
== Z_CARAT
|| smp
== HMAP
)) {
816 * Small screens: see vs_refresh.c section 6a.
818 * If it's a small screen, and the movement isn't larger than a
819 * screen, i.e some context will remain, open up the screen and
820 * display by scrolling. In this case, the cursor moves up one
821 * line for each line displayed. Otherwise, erase/compress and
822 * repaint, and move the cursor to the first line in the screen.
823 * Note, the ^B command is always in the latter case, for historical
826 cursor_set
= scmd
== CNTRL_Y
;
828 if (count
>= sp
->t_maxrows
|| scmd
== CNTRL_B
) {
832 for (; count
--; s1
= s2
) {
833 if (vs_sm_prev(sp
, &s1
, &s2
))
836 (O_ISSET(sp
, O_LEFTRIGHT
) || s2
.soff
== 1))
840 if (vs_sm_fill(sp
, OOBLNO
, P_TOP
))
842 return (vs_sm_position(sp
, rp
, 0, P_BOTTOM
));
844 cursor_set
= scmd
== CNTRL_Y
|| vs_sm_cursor(sp
, &ssmp
);
846 sp
->t_rows
!= sp
->t_maxrows
; --count
, ++sp
->t_rows
) {
847 if (HMAP
->lno
== 1 &&
848 (O_ISSET(sp
, O_LEFTRIGHT
) || HMAP
->soff
== 1))
856 rp
->cno
= ssmp
->c_sboff
;
862 for (ychanged
= zset
= 0; count
; --count
) {
863 /* If the line doesn't exist, we're done. */
864 if (HMAP
->lno
== 1 &&
865 (O_ISSET(sp
, O_LEFTRIGHT
) || HMAP
->soff
== 1))
868 /* Scroll the screen and cursor down one logical line. */
892 if (scmd
!= CNTRL_Y
&& cursor_set
)
898 * If there are more lines, the ^B command is positioned at
899 * the last line of the screen. However, the line may not
903 for (smp
= TMAP
; smp
> HMAP
; --smp
)
904 if (db_exist(sp
, smp
->lno
))
911 * The ^B and ^U commands move the cursor towards SOF
912 * if there are more lines to move.
914 if (count
< smp
- HMAP
)
921 * On a ^Y that was forced to change lines, try and keep the
922 * cursor as close as possible to the last position, but also
923 * set it up so that the next "real" movement will return the
924 * cursor to the closest position to the last real movement.
928 rp
->cno
= vs_colpos(sp
, smp
->lno
,
929 (O_ISSET(sp
, O_LEFTRIGHT
) ?
930 smp
->coff
: (smp
->soff
- 1) * sp
->cols
) +
935 /* The z^ command moves the cursor to the first new line. */
941 if (!SMAP_CACHE(smp
) && vs_line(sp
, smp
, NULL
, NULL
))
944 rp
->cno
= smp
->c_scoff
== 255 ? 0 : smp
->c_sboff
;
950 * Erase the small screen area for the scrolling functions.
958 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
959 (void)gp
->scr_clrtoeol(sp
);
960 for (; sp
->t_rows
> sp
->t_minrows
; --sp
->t_rows
, --TMAP
) {
961 (void)gp
->scr_move(sp
, TMAP
- HMAP
, 0);
962 (void)gp
->scr_clrtoeol(sp
);
969 * Scroll the SMAP down one.
971 * PUBLIC: int vs_sm_1down __P((SCR *));
977 * Insert a line at the top of the screen. Shift the screen map
978 * down and display a new line at the top of the screen.
980 (void)sp
->gp
->scr_move(sp
, 0, 0);
981 if (vs_insertln(sp
, 1))
984 /* One-line screens can fail. */
985 if (IS_ONELINE(sp
)) {
986 if (vs_sm_prev(sp
, HMAP
, HMAP
))
989 memmove(HMAP
+ 1, HMAP
, (sp
->rows
- 1) * sizeof(SMAP
));
990 if (vs_sm_prev(sp
, HMAP
+ 1, HMAP
))
993 /* vs_sm_prev() flushed the cache. */
994 return (vs_line(sp
, HMAP
, NULL
, NULL
));
999 * Insert a line a la curses, make sure to put the information
1000 * line and other screens back.
1003 vs_insertln(SCR
*sp
, int cnt
)
1010 /* If the screen is vertically split, we can't scroll it. */
1011 if (IS_VSPLIT(sp
)) {
1012 F_SET(sp
, SC_SCR_REDRAW
);
1016 if (IS_ONELINE(sp
)) {
1017 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
1018 (void)gp
->scr_clrtoeol(sp
);
1020 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
1022 (void)gp
->scr_move(sp
, LASTLINE(sp
) - 1, 0);
1023 (void)gp
->scr_deleteln(sp
);
1024 (void)gp
->scr_move(sp
, oldy
, oldx
);
1025 (void)gp
->scr_insertln(sp
);
1033 * Fill in the next entry in the SMAP.
1035 * PUBLIC: int vs_sm_next __P((SCR *, SMAP *, SMAP *));
1038 vs_sm_next(SCR
*sp
, SMAP
*p
, SMAP
*t
)
1043 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
1044 t
->lno
= p
->lno
+ 1;
1047 lcnt
= vs_screens(sp
, p
->lno
, NULL
);
1048 if (lcnt
== p
->soff
) {
1049 t
->lno
= p
->lno
+ 1;
1053 t
->soff
= p
->soff
+ 1;
1061 * Fill in the previous entry in the SMAP.
1063 * PUBLIC: int vs_sm_prev __P((SCR *, SMAP *, SMAP *));
1066 vs_sm_prev(SCR
*sp
, SMAP
*p
, SMAP
*t
)
1069 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
1070 t
->lno
= p
->lno
- 1;
1075 t
->soff
= p
->soff
- 1;
1077 t
->lno
= p
->lno
- 1;
1078 t
->soff
= vs_screens(sp
, t
->lno
, NULL
);
1081 return (t
->lno
== 0);
1086 * Return the SMAP entry referenced by the cursor.
1088 * PUBLIC: int vs_sm_cursor __P((SCR *, SMAP **));
1091 vs_sm_cursor(SCR
*sp
, SMAP
**smpp
)
1095 /* See if the cursor is not in the map. */
1096 if (sp
->lno
< HMAP
->lno
|| sp
->lno
> TMAP
->lno
)
1099 /* Find the first occurence of the line. */
1100 for (p
= HMAP
; p
->lno
!= sp
->lno
; ++p
);
1102 /* Fill in the map information until we find the right line. */
1103 for (; p
<= TMAP
; ++p
) {
1104 /* Short lines are common and easy to detect. */
1105 if (p
!= TMAP
&& (p
+ 1)->lno
!= p
->lno
) {
1109 if (!SMAP_CACHE(p
) && vs_line(sp
, p
, NULL
, NULL
))
1111 if (p
->c_eboff
>= sp
->cno
) {
1117 /* It was past the end of the map after all. */
1123 * Return the line/column of the top, middle or last line on the screen.
1124 * (The vi H, M and L commands.) Here because only the screen routines
1125 * know what's really out there.
1127 * PUBLIC: int vs_sm_position __P((SCR *, MARK *, u_long, pos_t));
1130 vs_sm_position(SCR
*sp
, MARK
*rp
, u_long cnt
, pos_t pos
)
1139 * Historically, an invalid count to the H command failed.
1140 * We do nothing special here, just making sure that H in
1141 * an empty screen works.
1143 if (cnt
> TMAP
- HMAP
)
1146 if (cnt
&& !db_exist(sp
, smp
->lno
)) {
1147 sof
: msgq(sp
, M_BERR
, "220|Movement past the end-of-screen");
1154 * Historically, a count to the M command was ignored.
1155 * If the screen isn't filled, find the middle of what's
1156 * real and move there.
1158 if (!db_exist(sp
, TMAP
->lno
)) {
1159 if (db_last(sp
, &last
))
1161 for (smp
= TMAP
; smp
->lno
> last
&& smp
> HMAP
; --smp
);
1163 smp
-= (smp
- HMAP
) / 2;
1165 smp
= (HMAP
+ (TMAP
- HMAP
) / 2) + cnt
;
1170 * Historically, an invalid count to the L command failed.
1171 * If the screen isn't filled, find the bottom of what's
1172 * real and try to offset from there.
1174 if (cnt
> TMAP
- HMAP
)
1177 if (!db_exist(sp
, smp
->lno
)) {
1178 if (db_last(sp
, &last
))
1180 for (; smp
->lno
> last
&& smp
> HMAP
; --smp
);
1181 if (cnt
> smp
- HMAP
) {
1182 eof
: msgq(sp
, M_BERR
,
1183 "221|Movement past the beginning-of-screen");
1193 /* Make sure that the cached information is valid. */
1194 if (!SMAP_CACHE(smp
) && vs_line(sp
, smp
, NULL
, NULL
))
1197 rp
->cno
= smp
->c_sboff
;
1204 * Return the number of screen lines from an SMAP entry to the
1205 * start of some file line, less than a maximum value.
1207 * PUBLIC: db_recno_t vs_sm_nlines __P((SCR *, SMAP *, db_recno_t, size_t));
1210 vs_sm_nlines(SCR
*sp
, SMAP
*from_sp
, db_recno_t to_lno
, size_t max
)
1212 db_recno_t lno
, lcnt
;
1214 if (O_ISSET(sp
, O_LEFTRIGHT
))
1215 return (from_sp
->lno
> to_lno
?
1216 from_sp
->lno
- to_lno
: to_lno
- from_sp
->lno
);
1218 if (from_sp
->lno
== to_lno
)
1219 return (from_sp
->soff
- 1);
1221 if (from_sp
->lno
> to_lno
) {
1222 lcnt
= from_sp
->soff
- 1; /* Correct for off-by-one. */
1223 for (lno
= from_sp
->lno
; --lno
>= to_lno
&& lcnt
<= max
;)
1224 lcnt
+= vs_screens(sp
, lno
, NULL
);
1227 lcnt
= (vs_screens(sp
, lno
, NULL
) - from_sp
->soff
) + 1;
1228 for (; ++lno
< to_lno
&& lcnt
<= max
;)
1229 lcnt
+= vs_screens(sp
, lno
, NULL
);