1 /* $NetBSD: vs_smap.c,v 1.1.1.2 2008/05/18 14:31:52 aymeric Exp $ */
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
28 #include "../common/common.h"
31 static int vs_deleteln
__P((SCR
*, int));
32 static int vs_insertln
__P((SCR
*, int));
33 static int vs_sm_delete
__P((SCR
*, db_recno_t
));
34 static int vs_sm_down
__P((SCR
*, MARK
*, db_recno_t
, scroll_t
, SMAP
*));
35 static int vs_sm_erase
__P((SCR
*));
36 static int vs_sm_insert
__P((SCR
*, db_recno_t
));
37 static int vs_sm_reset
__P((SCR
*, db_recno_t
));
38 static int vs_sm_up
__P((SCR
*, MARK
*, db_recno_t
, scroll_t
, SMAP
*));
42 * Make a change to the screen.
44 * PUBLIC: int vs_change __P((SCR *, db_recno_t, lnop_t));
47 vs_change(SCR
*sp
, db_recno_t lno
, lnop_t op
)
51 size_t cnt
, oldy
, oldx
;
57 * Very nasty special case. The historic vi code displays a single
58 * space (or a '$' if the list option is set) for the first line in
59 * an "empty" file. If we "insert" a line, that line gets scrolled
60 * down, not repainted, so it's incorrect when we refresh the screen.
61 * The vi text input functions detect it explicitly and don't insert
64 * Check for line #2 before going to the end of the file.
66 if (((op
== LINE_APPEND
&& lno
== 0) ||
67 (op
== LINE_INSERT
&& lno
== 1)) &&
73 /* Appending is the same as inserting, if the line is incremented. */
74 if (op
== LINE_APPEND
) {
79 /* Ignore the change if the line is after the map. */
84 * If the line is before the map, and it's a decrement, decrement
85 * the map. If it's an increment, increment the map. Otherwise,
88 if (lno
< HMAP
->lno
) {
94 for (p
= HMAP
, cnt
= sp
->t_rows
; cnt
--; ++p
)
98 F_SET(vip
, VIP_N_RENUMBER
);
101 for (p
= HMAP
, cnt
= sp
->t_rows
; cnt
--; ++p
)
105 F_SET(vip
, VIP_N_RENUMBER
);
113 F_SET(vip
, VIP_N_REFRESH
);
116 * Invalidate the line size cache, and invalidate the cursor if it's
121 F_SET(vip
, VIP_CUR_INVALID
);
124 * If ex modifies the screen after ex output is already on the screen
125 * or if we've switched into ex canonical mode, don't touch it -- we'll
126 * get scrolling wrong, at best.
128 if (!F_ISSET(sp
, SC_TINPUT_INFO
) &&
129 (F_ISSET(sp
, SC_SCR_EXWROTE
) || VIP(sp
)->totalcount
> 1)) {
130 F_SET(vip
, VIP_N_EX_REDRAW
);
134 /* Save and restore the cursor for these routines. */
135 (void)sp
->gp
->scr_cursor(sp
, &oldy
, &oldx
);
139 if (vs_sm_delete(sp
, lno
))
143 F_SET(vip
, VIP_N_RENUMBER
);
146 if (vs_sm_insert(sp
, lno
))
150 F_SET(vip
, VIP_N_RENUMBER
);
153 if (vs_sm_reset(sp
, lno
))
160 (void)sp
->gp
->scr_move(sp
, oldy
, oldx
);
166 * Fill in the screen map, placing the specified line at the
167 * right position. There isn't any way to tell if an SMAP
168 * entry has been filled in, so this routine had better be
169 * called with P_FILL set before anything else is done.
172 * Unexported interface: if lno is OOBLNO, P_TOP means that the HMAP
173 * slot is already filled in, P_BOTTOM means that the TMAP slot is
174 * already filled in, and we just finish up the job.
176 * PUBLIC: int vs_sm_fill __P((SCR *, db_recno_t, pos_t));
179 vs_sm_fill(SCR
*sp
, db_recno_t lno
, pos_t pos
)
184 /* Flush all cached information from the SMAP. */
185 for (p
= HMAP
, cnt
= sp
->t_rows
; cnt
--; ++p
)
189 * If the map is filled, the screen must be redrawn.
192 * This is a bug. We should try and figure out if the desired line
193 * is already in the map or close by -- scrolling the screen would
194 * be a lot better than redrawing.
196 F_SET(sp
, SC_SCR_REDRAW
);
204 /* See if less than half a screen from the top. */
206 &tmp
, lno
, HALFTEXT(sp
)) <= HALFTEXT(sp
)) {
211 /* See if less than half a screen from the bottom. */
212 if (db_last(sp
, &tmp
.lno
))
215 tmp
.soff
= vs_screens(sp
, tmp
.lno
, NULL
);
217 &tmp
, lno
, HALFTEXT(sp
)) <= HALFTEXT(sp
)) {
219 TMAP
->coff
= tmp
.coff
;
220 TMAP
->soff
= tmp
.soff
;
226 top
: HMAP
->lno
= lno
;
230 /* If we fail, just punt. */
231 for (p
= HMAP
, cnt
= sp
->t_rows
; --cnt
; ++p
)
232 if (vs_sm_next(sp
, p
, p
+ 1))
236 /* If we fail, guess that the file is too small. */
237 middle
: p
= HMAP
+ sp
->t_rows
/ 2;
241 for (; p
> HMAP
; --p
)
242 if (vs_sm_prev(sp
, p
, p
- 1)) {
247 /* If we fail, just punt. */
248 p
= HMAP
+ sp
->t_rows
/ 2;
249 for (; p
< TMAP
; ++p
)
250 if (vs_sm_next(sp
, p
, p
+ 1))
257 TMAP
->soff
= vs_screens(sp
, lno
, NULL
);
259 /* If we fail, guess that the file is too small. */
260 bottom
: for (p
= TMAP
; p
> HMAP
; --p
)
261 if (vs_sm_prev(sp
, p
, p
- 1)) {
272 * Try and put *something* on the screen. If this fails, we have a
273 * serious hard error.
278 for (p
= HMAP
; p
< TMAP
; ++p
)
279 if (vs_sm_next(sp
, p
, p
+ 1))
285 * For the routines vs_sm_reset, vs_sm_delete and vs_sm_insert: if the
286 * screen contains only a single line (whether because the screen is small
287 * or the line large), it gets fairly exciting. Skip the fun, set a flag
288 * so the screen map is refilled and the screen redrawn, and return. This
289 * is amazingly slow, but it's not clear that anyone will care.
291 #define HANDLE_WEIRDNESS(cnt) { \
292 if (cnt >= sp->t_rows) { \
293 F_SET(sp, SC_SCR_REFORMAT); \
300 * Delete a line out of the SMAP.
303 vs_sm_delete(SCR
*sp
, db_recno_t lno
)
309 * Find the line in the map, and count the number of screen lines
310 * which display any part of the deleted line.
312 for (p
= HMAP
; p
->lno
!= lno
; ++p
);
313 if (O_ISSET(sp
, O_LEFTRIGHT
))
316 for (cnt_orig
= 1, t
= p
+ 1;
317 t
<= TMAP
&& t
->lno
== lno
; ++cnt_orig
, ++t
);
319 HANDLE_WEIRDNESS(cnt_orig
);
321 /* Delete that many lines from the screen. */
322 (void)sp
->gp
->scr_move(sp
, p
- HMAP
, 0);
323 if (vs_deleteln(sp
, cnt_orig
))
326 /* Shift the screen map up. */
327 memmove(p
, p
+ cnt_orig
, (((TMAP
- p
) - cnt_orig
) + 1) * sizeof(SMAP
));
329 /* Decrement the line numbers for the rest of the map. */
330 for (t
= TMAP
- cnt_orig
; p
<= t
; ++p
)
333 /* Display the new lines. */
334 for (p
= TMAP
- cnt_orig
;;) {
335 if (p
< TMAP
&& vs_sm_next(sp
, p
, p
+ 1))
337 /* vs_sm_next() flushed the cache. */
338 if (vs_line(sp
, ++p
, NULL
, NULL
))
348 * Insert a line into the SMAP.
351 vs_sm_insert(SCR
*sp
, db_recno_t lno
)
354 size_t cnt_orig
, cnt
, coff
;
356 /* Save the offset. */
360 * Find the line in the map, find out how many screen lines
361 * needed to display the line.
363 for (p
= HMAP
; p
->lno
!= lno
; ++p
);
365 cnt_orig
= vs_screens(sp
, lno
, NULL
);
366 HANDLE_WEIRDNESS(cnt_orig
);
369 * The lines left in the screen override the number of screen
370 * lines in the inserted line.
372 cnt
= (TMAP
- p
) + 1;
376 /* Push down that many lines. */
377 (void)sp
->gp
->scr_move(sp
, p
- HMAP
, 0);
378 if (vs_insertln(sp
, cnt_orig
))
381 /* Shift the screen map down. */
382 memmove(p
+ cnt_orig
, p
, (((TMAP
- p
) - cnt_orig
) + 1) * sizeof(SMAP
));
384 /* Increment the line numbers for the rest of the map. */
385 for (t
= p
+ cnt_orig
; t
<= TMAP
; ++t
)
388 /* Fill in the SMAP for the new lines, and display. */
389 for (cnt
= 1, t
= p
; cnt
<= cnt_orig
; ++t
, ++cnt
) {
394 if (vs_line(sp
, t
, NULL
, NULL
))
402 * Reset a line in the SMAP.
405 vs_sm_reset(SCR
*sp
, db_recno_t lno
)
408 size_t cnt_orig
, cnt_new
, cnt
, diff
;
411 * See if the number of on-screen rows taken up by the old display
412 * for the line is the same as the number needed for the new one.
413 * If so, repaint, otherwise do it the hard way.
415 for (p
= HMAP
; p
->lno
!= lno
; ++p
);
416 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
418 cnt_orig
= cnt_new
= 1;
421 t
= p
; t
<= TMAP
&& t
->lno
== lno
; ++cnt_orig
, ++t
);
422 cnt_new
= vs_screens(sp
, lno
, NULL
);
425 HANDLE_WEIRDNESS(cnt_orig
);
427 if (cnt_orig
== cnt_new
) {
430 if (vs_line(sp
, p
, NULL
, NULL
))
436 if (cnt_orig
< cnt_new
) {
437 /* Get the difference. */
438 diff
= cnt_new
- cnt_orig
;
441 * The lines left in the screen override the number of screen
442 * lines in the inserted line.
444 cnt
= (TMAP
- p
) + 1;
448 /* If there are any following lines, push them down. */
450 (void)sp
->gp
->scr_move(sp
, p
- HMAP
, 0);
451 if (vs_insertln(sp
, diff
))
454 /* Shift the screen map down. */
456 (((TMAP
- p
) - diff
) + 1) * sizeof(SMAP
));
459 /* Fill in the SMAP for the replaced line, and display. */
460 for (cnt
= 1, t
= p
; cnt_new
-- && t
<= TMAP
; ++t
, ++cnt
) {
464 if (vs_line(sp
, t
, NULL
, NULL
))
468 /* Get the difference. */
469 diff
= cnt_orig
- cnt_new
;
471 /* Delete that many lines from the screen. */
472 (void)sp
->gp
->scr_move(sp
, p
- HMAP
, 0);
473 if (vs_deleteln(sp
, diff
))
476 /* Shift the screen map up. */
477 memmove(p
, p
+ diff
, (((TMAP
- p
) - diff
) + 1) * sizeof(SMAP
));
479 /* Fill in the SMAP for the replaced line, and display. */
480 for (cnt
= 1, t
= p
; cnt_new
--; ++t
, ++cnt
) {
484 if (vs_line(sp
, t
, NULL
, NULL
))
488 /* Display the new lines at the bottom of the screen. */
489 for (t
= TMAP
- diff
;;) {
490 if (t
< TMAP
&& vs_sm_next(sp
, t
, t
+ 1))
492 /* vs_sm_next() flushed the cache. */
493 if (vs_line(sp
, ++t
, NULL
, NULL
))
504 * Scroll the SMAP up/down count logical lines. Different
505 * semantics based on the vi command, *sigh*.
507 * PUBLIC: int vs_sm_scroll __P((SCR *, MARK *, db_recno_t, scroll_t));
510 vs_sm_scroll(SCR
*sp
, MARK
*rp
, db_recno_t count
, scroll_t scmd
)
515 * Invalidate the cursor. The line is probably going to change,
516 * (although for ^E and ^Y it may not). In any case, the scroll
517 * routines move the cursor to draw things.
519 F_SET(VIP(sp
), VIP_CUR_INVALID
);
521 /* Find the cursor in the screen. */
522 if (vs_sm_cursor(sp
, &smp
))
530 if (vs_sm_down(sp
, rp
, count
, scmd
, smp
))
537 if (vs_sm_up(sp
, rp
, count
, scmd
, smp
))
546 * If we're at the start of a line, go for the first non-blank.
547 * This makes it look like the old vi, even though we're moving
548 * around by logical lines, not physical ones.
551 * In the presence of a long line, which has more than a screen
552 * width of leading spaces, this code can cause a cursor warp.
555 if (scmd
!= CNTRL_E
&& scmd
!= CNTRL_Y
&&
556 rp
->cno
== 0 && nonblank(sp
, rp
->lno
, &rp
->cno
))
564 * Scroll the SMAP up count logical lines.
567 vs_sm_up(SCR
*sp
, MARK
*rp
, db_recno_t count
, scroll_t scmd
, SMAP
*smp
)
569 int cursor_set
, echanged
, zset
;
573 * Check to see if movement is possible.
575 * Get the line after the map. If that line is a new one (and if
576 * O_LEFTRIGHT option is set, this has to be true), and the next
577 * line doesn't exist, and the cursor doesn't move, or the cursor
578 * isn't even on the screen, or the cursor is already at the last
579 * line in the map, it's an error. If that test succeeded because
580 * the cursor wasn't at the end of the map, test to see if the map
583 if (vs_sm_next(sp
, TMAP
, &s1
))
585 if (s1
.lno
> TMAP
->lno
&& !db_exist(sp
, s1
.lno
)) {
586 if (scmd
== CNTRL_E
|| scmd
== Z_PLUS
|| smp
== TMAP
) {
590 if (vs_sm_next(sp
, smp
, &s1
))
592 if (s1
.lno
> smp
->lno
&& !db_exist(sp
, s1
.lno
)) {
599 * Small screens: see vs_refresh.c section 6a.
601 * If it's a small screen, and the movement isn't larger than a
602 * screen, i.e some context will remain, open up the screen and
603 * display by scrolling. In this case, the cursor moves down one
604 * line for each line displayed. Otherwise, erase/compress and
605 * repaint, and move the cursor to the first line in the screen.
606 * Note, the ^F command is always in the latter case, for historical
611 if (count
>= sp
->t_maxrows
|| scmd
== CNTRL_F
) {
615 for (; count
--; s1
= s2
) {
616 if (vs_sm_next(sp
, &s1
, &s2
))
618 if (s2
.lno
!= s1
.lno
&& !db_exist(sp
, s2
.lno
))
622 if (vs_sm_fill(sp
, OOBLNO
, P_BOTTOM
))
624 return (vs_sm_position(sp
, rp
, 0, P_TOP
));
626 cursor_set
= scmd
== CNTRL_E
|| vs_sm_cursor(sp
, &ssmp
);
628 sp
->t_rows
!= sp
->t_maxrows
; --count
, ++sp
->t_rows
) {
629 if (vs_sm_next(sp
, TMAP
, &s1
))
631 if (TMAP
->lno
!= s1
.lno
&& !db_exist(sp
, s1
.lno
))
634 /* vs_sm_next() flushed the cache. */
635 if (vs_line(sp
, TMAP
, NULL
, NULL
))
643 rp
->cno
= ssmp
->c_sboff
;
649 for (echanged
= zset
= 0; count
; --count
) {
650 /* Decide what would show up on the screen. */
651 if (vs_sm_next(sp
, TMAP
, &s1
))
654 /* If the line doesn't exist, we're done. */
655 if (TMAP
->lno
!= s1
.lno
&& !db_exist(sp
, s1
.lno
))
658 /* Scroll the screen cursor up one logical line. */
688 * On a ^E that was forced to change lines, try and keep the
689 * cursor as close as possible to the last position, but also
690 * set it up so that the next "real" movement will return the
691 * cursor to the closest position to the last real movement.
695 rp
->cno
= vs_colpos(sp
, smp
->lno
,
696 (O_ISSET(sp
, O_LEFTRIGHT
) ?
697 smp
->coff
: (smp
->soff
- 1) * sp
->cols
) +
703 * If there are more lines, the ^F command is positioned at
704 * the first line of the screen.
713 * The ^D and ^F commands move the cursor towards EOF
714 * if there are more lines to move. Check to be sure
715 * the lines actually exist. (They may not if the
716 * file is smaller than the screen.)
718 for (; count
; --count
, ++smp
)
719 if (smp
== TMAP
|| !db_exist(sp
, smp
[1].lno
))
723 /* The z+ command moves the cursor to the first new line. */
729 if (!SMAP_CACHE(smp
) && vs_line(sp
, smp
, NULL
, NULL
))
732 rp
->cno
= smp
->c_scoff
== 255 ? 0 : smp
->c_sboff
;
738 * Scroll the SMAP up one.
740 * PUBLIC: int vs_sm_1up __P((SCR *));
746 * Delete the top line of the screen. Shift the screen map
747 * up and display a new line at the bottom of the screen.
749 (void)sp
->gp
->scr_move(sp
, 0, 0);
750 if (vs_deleteln(sp
, 1))
753 /* One-line screens can fail. */
754 if (IS_ONELINE(sp
)) {
755 if (vs_sm_next(sp
, TMAP
, TMAP
))
758 memmove(HMAP
, HMAP
+ 1, (sp
->rows
- 1) * sizeof(SMAP
));
759 if (vs_sm_next(sp
, TMAP
- 1, TMAP
))
762 /* vs_sm_next() flushed the cache. */
763 return (vs_line(sp
, TMAP
, NULL
, NULL
));
768 * Delete a line a la curses, make sure to put the information
769 * line and other screens back.
772 vs_deleteln(SCR
*sp
, int cnt
)
779 /* If the screen is vertically split, we can't scroll it. */
781 F_SET(sp
, SC_SCR_REDRAW
);
786 (void)gp
->scr_clrtoeol(sp
);
788 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
790 (void)gp
->scr_deleteln(sp
);
791 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
792 (void)gp
->scr_insertln(sp
);
793 (void)gp
->scr_move(sp
, oldy
, oldx
);
801 * Scroll the SMAP down count logical lines.
804 vs_sm_down(SCR
*sp
, MARK
*rp
, db_recno_t count
, scroll_t scmd
, SMAP
*smp
)
807 int cursor_set
, ychanged
, zset
;
809 /* Check to see if movement is possible. */
810 if (HMAP
->lno
== 1 &&
811 (O_ISSET(sp
, O_LEFTRIGHT
) || HMAP
->soff
== 1) &&
812 (scmd
== CNTRL_Y
|| scmd
== Z_CARAT
|| smp
== HMAP
)) {
818 * Small screens: see vs_refresh.c section 6a.
820 * If it's a small screen, and the movement isn't larger than a
821 * screen, i.e some context will remain, open up the screen and
822 * display by scrolling. In this case, the cursor moves up one
823 * line for each line displayed. Otherwise, erase/compress and
824 * repaint, and move the cursor to the first line in the screen.
825 * Note, the ^B command is always in the latter case, for historical
828 cursor_set
= scmd
== CNTRL_Y
;
830 if (count
>= sp
->t_maxrows
|| scmd
== CNTRL_B
) {
834 for (; count
--; s1
= s2
) {
835 if (vs_sm_prev(sp
, &s1
, &s2
))
838 (O_ISSET(sp
, O_LEFTRIGHT
) || s2
.soff
== 1))
842 if (vs_sm_fill(sp
, OOBLNO
, P_TOP
))
844 return (vs_sm_position(sp
, rp
, 0, P_BOTTOM
));
846 cursor_set
= scmd
== CNTRL_Y
|| vs_sm_cursor(sp
, &ssmp
);
848 sp
->t_rows
!= sp
->t_maxrows
; --count
, ++sp
->t_rows
) {
849 if (HMAP
->lno
== 1 &&
850 (O_ISSET(sp
, O_LEFTRIGHT
) || HMAP
->soff
== 1))
858 rp
->cno
= ssmp
->c_sboff
;
864 for (ychanged
= zset
= 0; count
; --count
) {
865 /* If the line doesn't exist, we're done. */
866 if (HMAP
->lno
== 1 &&
867 (O_ISSET(sp
, O_LEFTRIGHT
) || HMAP
->soff
== 1))
870 /* Scroll the screen and cursor down one logical line. */
894 if (scmd
!= CNTRL_Y
&& cursor_set
)
900 * If there are more lines, the ^B command is positioned at
901 * the last line of the screen. However, the line may not
905 for (smp
= TMAP
; smp
> HMAP
; --smp
)
906 if (db_exist(sp
, smp
->lno
))
913 * The ^B and ^U commands move the cursor towards SOF
914 * if there are more lines to move.
916 if (count
< (db_recno_t
)(smp
- HMAP
))
923 * On a ^Y that was forced to change lines, try and keep the
924 * cursor as close as possible to the last position, but also
925 * set it up so that the next "real" movement will return the
926 * cursor to the closest position to the last real movement.
930 rp
->cno
= vs_colpos(sp
, smp
->lno
,
931 (O_ISSET(sp
, O_LEFTRIGHT
) ?
932 smp
->coff
: (smp
->soff
- 1) * sp
->cols
) +
937 /* The z^ command moves the cursor to the first new line. */
943 if (!SMAP_CACHE(smp
) && vs_line(sp
, smp
, NULL
, NULL
))
946 rp
->cno
= smp
->c_scoff
== 255 ? 0 : smp
->c_sboff
;
952 * Erase the small screen area for the scrolling functions.
960 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
961 (void)gp
->scr_clrtoeol(sp
);
962 for (; sp
->t_rows
> sp
->t_minrows
; --sp
->t_rows
, --TMAP
) {
963 (void)gp
->scr_move(sp
, TMAP
- HMAP
, 0);
964 (void)gp
->scr_clrtoeol(sp
);
971 * Scroll the SMAP down one.
973 * PUBLIC: int vs_sm_1down __P((SCR *));
979 * Insert a line at the top of the screen. Shift the screen map
980 * down and display a new line at the top of the screen.
982 (void)sp
->gp
->scr_move(sp
, 0, 0);
983 if (vs_insertln(sp
, 1))
986 /* One-line screens can fail. */
987 if (IS_ONELINE(sp
)) {
988 if (vs_sm_prev(sp
, HMAP
, HMAP
))
991 memmove(HMAP
+ 1, HMAP
, (sp
->rows
- 1) * sizeof(SMAP
));
992 if (vs_sm_prev(sp
, HMAP
+ 1, HMAP
))
995 /* vs_sm_prev() flushed the cache. */
996 return (vs_line(sp
, HMAP
, NULL
, NULL
));
1001 * Insert a line a la curses, make sure to put the information
1002 * line and other screens back.
1005 vs_insertln(SCR
*sp
, int cnt
)
1012 /* If the screen is vertically split, we can't scroll it. */
1013 if (IS_VSPLIT(sp
)) {
1014 F_SET(sp
, SC_SCR_REDRAW
);
1018 if (IS_ONELINE(sp
)) {
1019 (void)gp
->scr_move(sp
, LASTLINE(sp
), 0);
1020 (void)gp
->scr_clrtoeol(sp
);
1022 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
1024 (void)gp
->scr_move(sp
, LASTLINE(sp
) - 1, 0);
1025 (void)gp
->scr_deleteln(sp
);
1026 (void)gp
->scr_move(sp
, oldy
, oldx
);
1027 (void)gp
->scr_insertln(sp
);
1035 * Fill in the next entry in the SMAP.
1037 * PUBLIC: int vs_sm_next __P((SCR *, SMAP *, SMAP *));
1040 vs_sm_next(SCR
*sp
, SMAP
*p
, SMAP
*t
)
1045 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
1046 t
->lno
= p
->lno
+ 1;
1049 lcnt
= vs_screens(sp
, p
->lno
, NULL
);
1050 if (lcnt
== p
->soff
) {
1051 t
->lno
= p
->lno
+ 1;
1055 t
->soff
= p
->soff
+ 1;
1063 * Fill in the previous entry in the SMAP.
1065 * PUBLIC: int vs_sm_prev __P((SCR *, SMAP *, SMAP *));
1068 vs_sm_prev(SCR
*sp
, SMAP
*p
, SMAP
*t
)
1071 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
1072 t
->lno
= p
->lno
- 1;
1077 t
->soff
= p
->soff
- 1;
1079 t
->lno
= p
->lno
- 1;
1080 t
->soff
= vs_screens(sp
, t
->lno
, NULL
);
1083 return (t
->lno
== 0);
1088 * Return the SMAP entry referenced by the cursor.
1090 * PUBLIC: int vs_sm_cursor __P((SCR *, SMAP **));
1093 vs_sm_cursor(SCR
*sp
, SMAP
**smpp
)
1097 /* See if the cursor is not in the map. */
1098 if (sp
->lno
< HMAP
->lno
|| sp
->lno
> TMAP
->lno
)
1101 /* Find the first occurence of the line. */
1102 for (p
= HMAP
; p
->lno
!= sp
->lno
; ++p
);
1104 /* Fill in the map information until we find the right line. */
1105 for (; p
<= TMAP
; ++p
) {
1106 /* Short lines are common and easy to detect. */
1107 if (p
!= TMAP
&& (p
+ 1)->lno
!= p
->lno
) {
1111 if (!SMAP_CACHE(p
) && vs_line(sp
, p
, NULL
, NULL
))
1113 if (p
->c_eboff
>= sp
->cno
) {
1119 /* It was past the end of the map after all. */
1125 * Return the line/column of the top, middle or last line on the screen.
1126 * (The vi H, M and L commands.) Here because only the screen routines
1127 * know what's really out there.
1129 * PUBLIC: int vs_sm_position __P((SCR *, MARK *, u_long, pos_t));
1132 vs_sm_position(SCR
*sp
, MARK
*rp
, u_long cnt
, pos_t pos
)
1141 * Historically, an invalid count to the H command failed.
1142 * We do nothing special here, just making sure that H in
1143 * an empty screen works.
1145 if (cnt
> (u_long
)(TMAP
- HMAP
))
1148 if (cnt
&& !db_exist(sp
, smp
->lno
)) {
1149 sof
: msgq(sp
, M_BERR
, "220|Movement past the end-of-screen");
1156 * Historically, a count to the M command was ignored.
1157 * If the screen isn't filled, find the middle of what's
1158 * real and move there.
1160 if (!db_exist(sp
, TMAP
->lno
)) {
1161 if (db_last(sp
, &last
))
1163 for (smp
= TMAP
; smp
->lno
> last
&& smp
> HMAP
; --smp
);
1165 smp
-= (smp
- HMAP
) / 2;
1167 smp
= (HMAP
+ (TMAP
- HMAP
) / 2) + cnt
;
1172 * Historically, an invalid count to the L command failed.
1173 * If the screen isn't filled, find the bottom of what's
1174 * real and try to offset from there.
1176 if (cnt
> (u_long
)(TMAP
- HMAP
))
1179 if (!db_exist(sp
, smp
->lno
)) {
1180 if (db_last(sp
, &last
))
1182 for (; smp
->lno
> last
&& smp
> HMAP
; --smp
);
1183 if (cnt
> (u_long
)(smp
- HMAP
)) {
1184 eof
: msgq(sp
, M_BERR
,
1185 "221|Movement past the beginning-of-screen");
1195 /* Make sure that the cached information is valid. */
1196 if (!SMAP_CACHE(smp
) && vs_line(sp
, smp
, NULL
, NULL
))
1199 rp
->cno
= smp
->c_sboff
;
1206 * Return the number of screen lines from an SMAP entry to the
1207 * start of some file line, less than a maximum value.
1209 * PUBLIC: db_recno_t vs_sm_nlines __P((SCR *, SMAP *, db_recno_t, size_t));
1212 vs_sm_nlines(SCR
*sp
, SMAP
*from_sp
, db_recno_t to_lno
, size_t max
)
1214 db_recno_t lno
, lcnt
;
1216 if (O_ISSET(sp
, O_LEFTRIGHT
))
1217 return (from_sp
->lno
> to_lno
?
1218 from_sp
->lno
- to_lno
: to_lno
- from_sp
->lno
);
1220 if (from_sp
->lno
== to_lno
)
1221 return (from_sp
->soff
- 1);
1223 if (from_sp
->lno
> to_lno
) {
1224 lcnt
= from_sp
->soff
- 1; /* Correct for off-by-one. */
1225 for (lno
= from_sp
->lno
; --lno
>= to_lno
&& lcnt
<= max
;)
1226 lcnt
+= vs_screens(sp
, lno
, NULL
);
1229 lcnt
= (vs_screens(sp
, lno
, NULL
) - from_sp
->soff
) + 1;
1230 for (; ++lno
< to_lno
&& lcnt
<= max
;)
1231 lcnt
+= vs_screens(sp
, lno
, NULL
);