2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: vs_refresh.c,v 8.32 1993/11/18 13:51:10 bostic Exp $ (Berkeley) $Date: 1993/11/18 13:51:10 $";
12 #include <sys/types.h>
20 #include "svi_screen.h"
21 #include "sex/sex_screen.h"
23 static int svi_modeline
__P((SCR
*, EXF
*));
24 static int svi_msgflush
__P((SCR
*));
34 * 1: Resize the screen.
36 * Notice that a resize is requested, and set up everything so that
37 * the file gets reinitialized. Done here, instead of in the vi loop
38 * because there may be other initialization that other screens need
39 * to do. The actual changing of the row/column values was done by
40 * calling the ex options code which put them into the environment,
41 * which is used by curses. Stupid, but ugly.
43 if (F_ISSET(sp
, S_RESIZE
)) {
44 /* Reinitialize curses. */
45 if (svi_curses_end(sp
) || svi_curses_init(sp
))
48 /* Lose any svi_screens() cached information. */
49 SVP(sp
)->ss_lno
= OOBLNO
;
52 * Fill the map, incidentally losing any svi_line()
55 if (sp
->s_fill(sp
, ep
, sp
->lno
, P_FILL
))
57 F_CLR(sp
, S_RESIZE
| S_REFORMAT
);
62 * 2: Refresh related screens.
64 * If related screens share a view into a file, they may have been
65 * modified as well. Refresh them if the dirty bit is set.
67 #define PAINTBITS S_REDRAW | S_REFORMAT | S_REFRESH
69 if ((tsp
= tsp
->q
.cqe_prev
) == (void *)&sp
->gp
->dq
)
71 if (F_ISSET(tsp
, PAINTBITS
) ||
72 tsp
->ep
== ep
&& F_ISSET(SVP(tsp
), SVI_SCREENDIRTY
)) {
73 (void)svi_paint(tsp
, tsp
->ep
);
74 F_CLR(SVP(tsp
), SVI_SCREENDIRTY
);
78 if ((tsp
= tsp
->q
.cqe_next
) == (void *)&sp
->gp
->dq
)
80 if (F_ISSET(tsp
, PAINTBITS
) ||
81 tsp
->ep
== ep
&& F_ISSET(SVP(tsp
), SVI_SCREENDIRTY
)) {
82 (void)svi_paint(tsp
, tsp
->ep
);
83 F_CLR(SVP(tsp
), SVI_SCREENDIRTY
);
88 * 3: Refresh the current screen.
90 * Always refresh the current screen, it may be a cursor movement.
91 * Also, always do it last -- that way, S_REFRESH can be set in
92 * the current screen only, and the screen won't flash.
94 F_CLR(sp
, SVI_SCREENDIRTY
);
95 return (svi_paint(sp
, ep
));
100 * This is the guts of the vi curses screen code. The idea is that
101 * the SCR structure passed in contains the new coordinates of the
102 * screen. What makes this hard is that we don't know how big
103 * characters are, doing input can put the cursor in illegal places,
104 * and we're frantically trying to avoid repainting unless it's
105 * absolutely necessary. If you change this code, you'd better know
106 * what you're doing. It's subtle and quick to anger.
115 recno_t lastline
, lcnt
;
116 size_t cwtotal
, cnt
, len
, x
, y
;
121 #define OLNO sp->olno
123 #define OCNO sp->ocno
124 #define SCNO sp->sc_col
127 * 1: Reformat the lines.
129 * If the lines themselves have changed (:set list, for example),
130 * fill in the map from scratch. Adjust the screen that's being
131 * displayed if the leftright flag is set.
133 if (F_ISSET(sp
, S_REFORMAT
)) {
134 /* Toss svi_screens() cached information. */
135 SVP(sp
)->ss_lno
= OOBLNO
;
137 /* Toss svi_line() cached information. */
138 if (svi_sm_fill(sp
, ep
, HMAP
->lno
, P_TOP
))
140 if (O_ISSET(sp
, O_LEFTRIGHT
) &&
141 (cnt
= svi_screens(sp
, ep
, LNO
, &CNO
)) != 1)
142 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
144 F_CLR(sp
, S_REFORMAT
);
151 * Line changes can cause the top line to change as well. As
152 * before, if the movement is large, the screen is repainted.
156 * Tiny screens cannot be permitted into the "scrolling" parts of
157 * the smap code for two reasons. If the screen size is 1 line,
158 * HMAP == TMAP and the code will quickly drop core. If the screen
159 * size is 2, none of the divisions by 2 will work, and scrolling
160 * won't work. In fact, because no line change will be less than
161 * HALFTEXT(sp), we always ending up "filling" the map, with a
162 * P_MIDDLE flag, which isn't what the user wanted. Tiny screens
163 * can go into the "fill" portions of the smap code, however.
165 if (sp
->t_rows
<= 2) {
166 if (LNO
< HMAP
->lno
) {
167 if (svi_sm_fill(sp
, ep
, LNO
, P_TOP
))
169 } else if (LNO
> TMAP
->lno
)
170 if (svi_sm_fill(sp
, ep
, LNO
, P_BOTTOM
))
172 if (sp
->t_rows
== 1) {
173 HMAP
->off
= svi_screens(sp
, ep
, LNO
, &CNO
);
183 * Users can use the window, w300, w1200 and w9600 options to make
184 * the screen artificially small. The behavior of these options
185 * in the historic vi wasn't all that consistent, and, in fact, it
186 * was never documented how various screen movements affected the
187 * screen size. Generally, one of three things would happen:
188 * 1: The screen would expand in size, showing the line
189 * 2: The screen would scroll, showing the line
190 * 3: The screen would compress to its smallest size and
192 * In general, scrolling didn't cause compression (200^D was handled
193 * the same as ^D), movement to a specific line would (:N where N
194 * was 1 line below the screen caused a screen compress), and cursor
195 * movement would scroll if it was 11 lines or less, and compress if
196 * it was more than 11 lines. (And, no, I have no idea where the 11
199 * What we do is try and figure out if the line is less than half of
200 * a full screen away. If it is, we expand the screen if there's
201 * room, and then scroll as necessary. The alternative is to compress
205 * This code is a special case from beginning to end. Unfortunately,
206 * home modems are still slow enough that it's worth having.
209 * If the line a really long one, i.e. part of the line is on the
210 * screen but the column offset is not, we'll end up in the adjust
211 * code, when we should probably have compressed the screen.
213 if (ISSMALLSCREEN(sp
))
214 if (LNO
< HMAP
->lno
) {
215 lcnt
= svi_sm_nlines(sp
, ep
, HMAP
, LNO
, sp
->t_maxrows
);
216 if (lcnt
<= HALFSCREEN(sp
))
217 for (; lcnt
&& sp
->t_rows
!= sp
->t_maxrows
;
218 --lcnt
, ++sp
->t_rows
) {
220 if (svi_sm_1down(sp
, ep
))
225 } else if (LNO
> TMAP
->lno
) {
226 lcnt
= svi_sm_nlines(sp
, ep
, TMAP
, LNO
, sp
->t_maxrows
);
227 if (lcnt
<= HALFSCREEN(sp
))
228 for (; lcnt
&& sp
->t_rows
!= sp
->t_maxrows
;
229 --lcnt
, ++sp
->t_rows
) {
230 if (svi_sm_next(sp
, ep
, TMAP
, TMAP
+ 1))
233 if (svi_line(sp
, ep
, TMAP
, NULL
, NULL
))
237 small_fill
: MOVE(sp
, INFOLINE(sp
), 0);
239 for (; sp
->t_rows
> sp
->t_minrows
;
240 --sp
->t_rows
, --TMAP
) {
241 MOVE(sp
, TMAP
- HMAP
, 0);
244 if (svi_sm_fill(sp
, ep
, LNO
, P_FILL
))
254 if (LNO
>= HMAP
->lno
) {
255 if (LNO
<= TMAP
->lno
)
259 * If less than half a screen away, scroll down until the
260 * line is on the screen.
262 lcnt
= svi_sm_nlines(sp
, ep
, TMAP
, LNO
, HALFTEXT(sp
));
263 if (lcnt
< HALFTEXT(sp
)) {
265 if (svi_sm_1up(sp
, ep
))
271 * If less than a full screen from the bottom of the file, put
272 * the last line of the file on the bottom of the screen. The
273 * calculation is safe because we know there's at least one
274 * full screen of lines, otherwise couldn't have gotten here.
276 if (file_lline(sp
, ep
, &lastline
))
280 lcnt
= svi_sm_nlines(sp
, ep
, &tmp
, lastline
, sp
->t_rows
);
281 if (lcnt
< sp
->t_rows
) {
282 if (svi_sm_fill(sp
, ep
, lastline
, P_BOTTOM
))
289 * If more than a full screen from the last line of the file,
290 * put the new line in the middle of the screen.
298 * If less than half a screen away, scroll up until the line is
299 * the first line on the screen.
301 lcnt
= svi_sm_nlines(sp
, ep
, HMAP
, LNO
, HALFTEXT(sp
));
302 if (lcnt
< HALFTEXT(sp
)) {
304 if (svi_sm_1down(sp
, ep
))
310 * If less than half a screen from the top of the file, put the first
311 * line of the file at the top of the screen. Otherwise, put the line
312 * in the middle of the screen.
316 lcnt
= svi_sm_nlines(sp
, ep
, &tmp
, LNO
, HALFTEXT(sp
));
317 if (lcnt
< HALFTEXT(sp
)) {
318 if (svi_sm_fill(sp
, ep
, 1, P_TOP
))
321 middle
: if (svi_sm_fill(sp
, ep
, LNO
, P_MIDDLE
))
326 * At this point we know part of the line is on the screen. Since
327 * scrolling is done using logical lines, not physical, all of the
328 * line may not be on the screen. While that's not necessarily bad,
329 * if the part the cursor is on isn't there, we're going to lose.
330 * This can be tricky; if the line covers the entire screen, lno
331 * may be the same as both ends of the map, that's why we test BOTH
332 * the top and the bottom of the map. This isn't a problem for
333 * left-right scrolling, the cursor movement code handles the problem.
335 * There's a performance issue here if editing *really* long lines.
336 * This gets to the right spot by scrolling, and, in a binary, by
337 * scrolling hundreds of lines. If the adjustment looks like it's
338 * going to be a serious problem, refill the screen and repaint.
340 adjust
: if (!O_ISSET(sp
, O_LEFTRIGHT
) &&
341 (LNO
== HMAP
->lno
|| LNO
== TMAP
->lno
)) {
342 cnt
= svi_screens(sp
, ep
, LNO
, &CNO
);
343 if (LNO
== HMAP
->lno
&& cnt
< HMAP
->off
)
344 if ((HMAP
->off
- cnt
) > HALFTEXT(sp
)) {
346 svi_sm_fill(sp
, ep
, OOBLNO
, P_TOP
);
349 while (cnt
< HMAP
->off
)
350 if (svi_sm_1down(sp
, ep
))
352 if (LNO
== TMAP
->lno
&& cnt
> TMAP
->off
)
353 if ((cnt
- TMAP
->off
) > HALFTEXT(sp
)) {
355 svi_sm_fill(sp
, ep
, OOBLNO
, P_BOTTOM
);
358 while (cnt
> TMAP
->off
)
359 if (svi_sm_1up(sp
, ep
))
363 /* If the screen needs to be repainted, skip cursor optimization. */
364 if (F_ISSET(sp
, S_REDRAW
))
368 * 4: Cursor movements.
370 * Decide cursor position. If the line has changed, the cursor has
371 * moved over a tab, or don't know where the cursor was, reparse the
372 * line. Note, if we think that the cursor "hasn't moved", reparse
373 * the line. This is 'cause if it hasn't moved, we've almost always
376 * Otherwise, we've just moved over fixed-width characters, and can
377 * calculate the left/right scrolling and cursor movement without
378 * reparsing the line. Note that we don't know which (if any) of
379 * the characters between the old and new cursor positions changed.
382 * With some work, it should be possible to handle tabs quickly, at
383 * least in obvious situations, like moving right and encountering
384 * a tab, without reparsing the whole line.
387 /* If the line we're working with has changed, reparse. */
388 if (F_ISSET(SVP(sp
), SVI_CUR_INVALID
) || LNO
!= OLNO
) {
389 F_CLR(SVP(sp
), SVI_CUR_INVALID
);
393 /* Otherwise, if nothing's changed, go fast. */
398 * Get the current line. If this fails, we either have an empty
399 * file and can just repaint, or there's a real problem. This
400 * isn't a performance issue because there aren't any ways to get
403 if ((p
= file_gline(sp
, ep
, LNO
, &len
)) == NULL
) {
404 if (file_lline(sp
, ep
, &lastline
))
408 GETLINE_ERR(sp
, LNO
);
413 /* This is just a test. */
414 if (CNO
>= len
&& len
!= 0) {
415 msgq(sp
, M_ERR
, "Error: %s/%d: cno (%u) >= len (%u)",
416 tail(__FILE__
), __LINE__
, CNO
, len
);
421 * The basic scheme here is to look at the characters in between
422 * the old and new positions and decide how big they are on the
423 * screen, and therefore, how many screen positions to move.
428 * 4a: Cursor moved left.
430 * Point to the old character. The old cursor position can
431 * be past EOL if, for example, we just deleted the rest of
432 * the line. In this case, since we don't know the width of
433 * the characters we traversed, we have to do it slowly.
436 cnt
= (OCNO
- CNO
) + 1;
441 * Quit sanity check -- it's hard to figure out exactly when
442 * we cross a screen boundary as we do in the cursor right
443 * movement. If cnt is so large that we're going to cross the
444 * boundary no matter what, stop now.
446 if (SCNO
+ 1 + MAX_CHARACTER_COLUMNS
< cnt
)
450 * Count up the widths of the characters. If it's a tab
451 * character, go do it the the slow way.
453 for (cwtotal
= 0; cnt
--; cwtotal
+= cname
[ch
].len
)
454 if ((ch
= *(u_char
*)p
--) == '\t')
458 * Decrement the screen cursor by the total width of the
459 * characters minus 1.
464 * If we're moving left, and there's a wide character in the
465 * current position, go to the end of the character.
467 if (cname
[ch
].len
> 1)
468 cwtotal
-= cname
[ch
].len
- 1;
471 * If the new column moved us out of the current screen,
472 * calculate a new screen.
474 if (SCNO
< cwtotal
) {
475 lscreen
: if (O_ISSET(sp
, O_LEFTRIGHT
)) {
476 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
485 * 4b: Cursor moved right.
487 * Point to the first character to the right.
493 * Count up the widths of the characters. If it's a tab
494 * character, go do it the the slow way. If we cross a
495 * screen boundary, we can quit.
497 for (cwtotal
= SCNO
; cnt
--;) {
498 if ((ch
= *(u_char
*)p
++) == '\t')
500 if ((cwtotal
+= cname
[ch
].len
) >= SCREEN_COLS(sp
))
505 * Increment the screen cursor by the total width of the
511 * If the new column moved us out of the current screen,
512 * calculate a new screen.
514 if (SCNO
>= SCREEN_COLS(sp
)) {
515 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
516 SCNO
-= SCREEN_COLS(sp
);
517 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
525 fast
: getyx(stdscr
, y
, x
); /* Just move the cursor. */
526 y
-= sp
->woff
; /* Correct for split screens. */
529 slow
: /* Find the current line in the map. */
530 for (smp
= HMAP
; smp
->lno
!= LNO
; ++smp
);
533 * If doing left-right scrolling and the cursor movement has changed
534 * the screen being displayed, scroll it. If we're painting the info
535 * line, however, just scroll that single line.
537 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
538 cnt
= svi_screens(sp
, ep
, LNO
, &CNO
) % SCREEN_COLS(sp
);
539 if (cnt
!= HMAP
->off
) {
540 if (ISINFOLINE(sp
, smp
))
543 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
550 * Update screen lines for this file line until we have a new
551 * screen cursor position.
553 for (y
= -1; smp
<= TMAP
&& smp
->lno
== LNO
; ++smp
) {
554 if (svi_line(sp
, ep
, smp
, &y
, &SCNO
))
560 /* Not too bad, move the cursor. */
564 * Lost big, do what you have to do. We flush the cache since
565 * S_REDRAW gets set when the screen isn't worth fixing, and
566 * it's simpler to just repaint. So, don't trust anything that
567 * we think we know about it.
569 paint
: for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
571 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
572 if (svi_line(sp
, ep
, smp
, &y
, &SCNO
))
576 * If it's a small screen and we're redrawing, clear the unused
577 * lines, ex may have overwritten them.
579 if (F_ISSET(sp
, S_REDRAW
)) {
580 if (ISSMALLSCREEN(sp
))
581 for (cnt
= sp
->t_rows
; cnt
<= sp
->t_maxrows
; ++cnt
) {
588 /* Update saved information. */
592 /* Refresh the screen. */
593 if (F_ISSET(sp
, S_REFRESH
)) {
595 F_CLR(sp
, S_REFRESH
);
599 * If there are any keys waiting, we don't ring the bell or update
600 * the messages. The reason is that there may be multiple messages,
601 * and we're going to get the wrong key as a message delimiter.
604 * This is wrong. If you type quickly enough, the status message
605 * for a screen may not be displayed during a split command because
606 * there were keys waiting so we switched screens without ever
607 * displaying it. Maybe the split code should flush messages?
609 if (!sex_key_wait(sp
)) {
610 if (F_ISSET(sp
, S_BELLSCHED
))
613 * If the bottom line isn't in use by the colon command:
615 * Display any messages. Don't test S_UPDATE_MODE. The
616 * message printing routine set it to avoid anyone else
617 * destroying the message we're about to display.
619 * If the bottom line isn't in use by anyone, put out the
620 * standard status line.
622 if (!F_ISSET(SVP(sp
), SVI_INFOLINE
))
623 if (sp
->msgq
.lh_first
!= NULL
&&
624 !F_ISSET(sp
->msgq
.lh_first
, M_EMPTY
))
626 else if (!F_ISSET(sp
, S_UPDATE_MODE
))
627 svi_modeline(sp
, ep
);
630 /* Place the cursor. */
633 /* Flush it all out. */
641 * Flush any accumulated messages.
653 #define MCONTMSG " [More ...]"
655 /* Display the messages. */
657 for (mp
= sp
->msgq
.lh_first
, p
= NULL
;
658 mp
!= NULL
&& !F_ISSET(mp
, M_EMPTY
); mp
= mp
->q
.le_next
) {
662 lcont
: /* Move to the message line and clear it. */
663 MOVE(sp
, INFOLINE(sp
), 0);
667 * Turn on standout mode if requested, or, if we've split
668 * the screen and need a divider.
670 if (F_ISSET(mp
, M_INV_VIDEO
) ||
671 sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
675 * Print up to the "more" message. Avoid the last character
676 * in the last line, some hardware doesn't like it.
678 if (svi_ncols(sp
, p
, mp
->len
, NULL
) < sp
->cols
- 1)
681 len
= (sp
->cols
- sizeof(MCONTMSG
)) - 1;
686 chlen
= cname
[ch
].len
;
691 ADDNSTR(cname
[ch
].name
, chlen
);
695 * If more, print continue message. If user key fails,
696 * keep showing the messages anyway.
698 if (mp
->len
|| (mp
->q
.le_next
!= NULL
&&
699 !F_ISSET(mp
->q
.le_next
, M_EMPTY
))) {
700 ADDNSTR(MCONTMSG
, sizeof(MCONTMSG
) - 1);
703 if (term_key(sp
, &ch
, 0) != INP_OK
)
705 if (sp
->special
[ch
] == K_CR
||
706 sp
->special
[ch
] == K_NL
|| isblank(ch
))
712 /* Turn off standout mode. */
713 if (F_ISSET(mp
, M_INV_VIDEO
) ||
714 sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
727 #define MODESIZE (RULERSIZE + 15)
731 * Update the mode line.
738 char *s
, buf
[RULERSIZE
];
740 MOVE(sp
, INFOLINE(sp
), 0);
743 /* Display the dividing line. */
744 if (sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
747 /* Display the ruler. */
748 if (O_ISSET(sp
, O_RULER
) && sp
->cols
> RULERSIZE
+ 2) {
749 MOVE(sp
, INFOLINE(sp
), sp
->cols
/ 2 - RULERSIZE
/ 2);
752 sizeof(buf
), "%lu,%lu", sp
->lno
, sp
->cno
+ 1);
756 /* Show the modified bit. */
757 if (O_ISSET(sp
, O_SHOWDIRTY
) &&
758 F_ISSET(ep
, F_MODIFIED
) && sp
->cols
> MODESIZE
) {
759 MOVE(sp
, INFOLINE(sp
), sp
->cols
- 9);
764 * Show the mode. Leave the last character blank, in case it's a
765 * really dumb terminal with hardware scroll. Second, don't try
766 * to *paint* the last character, SunOS 4.1.1 and Ultrix 4.2 curses
767 * won't let you paint the last character in the screen.
769 if (O_ISSET(sp
, O_SHOWMODE
) && sp
->cols
> MODESIZE
) {
770 MOVE(sp
, INFOLINE(sp
), sp
->cols
- 8);
771 s
= F_ISSET(sp
, S_INPUT
) ? " Input" : "Command";
782 #define DIVIDESIZE 10
784 char buf
[DIVIDESIZE
+ 1];
786 dividesize
= DIVIDESIZE
> sp
->cols
? sp
->cols
: DIVIDESIZE
;
787 memset(buf
, ' ', dividesize
);
788 if (standout() == ERR
)
790 ADDNSTR(buf
, dividesize
);
791 if (standend() == ERR
)