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.33 1993/11/19 10:55:44 bostic Exp $ (Berkeley) $Date: 1993/11/19 10:55:44 $";
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 for (tsp
= sp
->gp
->dq
.cqh_first
;
68 tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
70 (F_ISSET(tsp
, S_REDRAW
| S_REFORMAT
| S_REFRESH
) ||
71 tsp
->ep
== ep
&& F_ISSET(SVP(tsp
), SVI_SCREENDIRTY
))) {
72 (void)svi_paint(tsp
, tsp
->ep
);
73 F_CLR(SVP(tsp
), SVI_SCREENDIRTY
);
77 * 3: Refresh the current screen.
79 * Always refresh the current screen, it may be a cursor movement.
80 * Also, always do it last -- that way, S_REFRESH can be set in
81 * the current screen only, and the screen won't flash.
83 F_CLR(sp
, SVI_SCREENDIRTY
);
84 return (svi_paint(sp
, ep
));
89 * This is the guts of the vi curses screen code. The idea is that
90 * the SCR structure passed in contains the new coordinates of the
91 * screen. What makes this hard is that we don't know how big
92 * characters are, doing input can put the cursor in illegal places,
93 * and we're frantically trying to avoid repainting unless it's
94 * absolutely necessary. If you change this code, you'd better know
95 * what you're doing. It's subtle and quick to anger.
104 recno_t lastline
, lcnt
;
105 size_t cwtotal
, cnt
, len
, x
, y
;
110 #define OLNO sp->olno
112 #define OCNO sp->ocno
113 #define SCNO sp->sc_col
116 * 1: Reformat the lines.
118 * If the lines themselves have changed (:set list, for example),
119 * fill in the map from scratch. Adjust the screen that's being
120 * displayed if the leftright flag is set.
122 if (F_ISSET(sp
, S_REFORMAT
)) {
123 /* Toss svi_screens() cached information. */
124 SVP(sp
)->ss_lno
= OOBLNO
;
126 /* Toss svi_line() cached information. */
127 if (svi_sm_fill(sp
, ep
, HMAP
->lno
, P_TOP
))
129 if (O_ISSET(sp
, O_LEFTRIGHT
) &&
130 (cnt
= svi_screens(sp
, ep
, LNO
, &CNO
)) != 1)
131 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
133 F_CLR(sp
, S_REFORMAT
);
140 * Line changes can cause the top line to change as well. As
141 * before, if the movement is large, the screen is repainted.
145 * Tiny screens cannot be permitted into the "scrolling" parts of
146 * the smap code for two reasons. If the screen size is 1 line,
147 * HMAP == TMAP and the code will quickly drop core. If the screen
148 * size is 2, none of the divisions by 2 will work, and scrolling
149 * won't work. In fact, because no line change will be less than
150 * HALFTEXT(sp), we always ending up "filling" the map, with a
151 * P_MIDDLE flag, which isn't what the user wanted. Tiny screens
152 * can go into the "fill" portions of the smap code, however.
154 if (sp
->t_rows
<= 2) {
155 if (LNO
< HMAP
->lno
) {
156 if (svi_sm_fill(sp
, ep
, LNO
, P_TOP
))
158 } else if (LNO
> TMAP
->lno
)
159 if (svi_sm_fill(sp
, ep
, LNO
, P_BOTTOM
))
161 if (sp
->t_rows
== 1) {
162 HMAP
->off
= svi_screens(sp
, ep
, LNO
, &CNO
);
172 * Users can use the window, w300, w1200 and w9600 options to make
173 * the screen artificially small. The behavior of these options
174 * in the historic vi wasn't all that consistent, and, in fact, it
175 * was never documented how various screen movements affected the
176 * screen size. Generally, one of three things would happen:
177 * 1: The screen would expand in size, showing the line
178 * 2: The screen would scroll, showing the line
179 * 3: The screen would compress to its smallest size and
181 * In general, scrolling didn't cause compression (200^D was handled
182 * the same as ^D), movement to a specific line would (:N where N
183 * was 1 line below the screen caused a screen compress), and cursor
184 * movement would scroll if it was 11 lines or less, and compress if
185 * it was more than 11 lines. (And, no, I have no idea where the 11
188 * What we do is try and figure out if the line is less than half of
189 * a full screen away. If it is, we expand the screen if there's
190 * room, and then scroll as necessary. The alternative is to compress
194 * This code is a special case from beginning to end. Unfortunately,
195 * home modems are still slow enough that it's worth having.
198 * If the line a really long one, i.e. part of the line is on the
199 * screen but the column offset is not, we'll end up in the adjust
200 * code, when we should probably have compressed the screen.
202 if (ISSMALLSCREEN(sp
))
203 if (LNO
< HMAP
->lno
) {
204 lcnt
= svi_sm_nlines(sp
, ep
, HMAP
, LNO
, sp
->t_maxrows
);
205 if (lcnt
<= HALFSCREEN(sp
))
206 for (; lcnt
&& sp
->t_rows
!= sp
->t_maxrows
;
207 --lcnt
, ++sp
->t_rows
) {
209 if (svi_sm_1down(sp
, ep
))
214 } else if (LNO
> TMAP
->lno
) {
215 lcnt
= svi_sm_nlines(sp
, ep
, TMAP
, LNO
, sp
->t_maxrows
);
216 if (lcnt
<= HALFSCREEN(sp
))
217 for (; lcnt
&& sp
->t_rows
!= sp
->t_maxrows
;
218 --lcnt
, ++sp
->t_rows
) {
219 if (svi_sm_next(sp
, ep
, TMAP
, TMAP
+ 1))
222 if (svi_line(sp
, ep
, TMAP
, NULL
, NULL
))
226 small_fill
: MOVE(sp
, INFOLINE(sp
), 0);
228 for (; sp
->t_rows
> sp
->t_minrows
;
229 --sp
->t_rows
, --TMAP
) {
230 MOVE(sp
, TMAP
- HMAP
, 0);
233 if (svi_sm_fill(sp
, ep
, LNO
, P_FILL
))
243 if (LNO
>= HMAP
->lno
) {
244 if (LNO
<= TMAP
->lno
)
248 * If less than half a screen away, scroll down until the
249 * line is on the screen.
251 lcnt
= svi_sm_nlines(sp
, ep
, TMAP
, LNO
, HALFTEXT(sp
));
252 if (lcnt
< HALFTEXT(sp
)) {
254 if (svi_sm_1up(sp
, ep
))
260 * If less than a full screen from the bottom of the file, put
261 * the last line of the file on the bottom of the screen. The
262 * calculation is safe because we know there's at least one
263 * full screen of lines, otherwise couldn't have gotten here.
265 if (file_lline(sp
, ep
, &lastline
))
269 lcnt
= svi_sm_nlines(sp
, ep
, &tmp
, lastline
, sp
->t_rows
);
270 if (lcnt
< sp
->t_rows
) {
271 if (svi_sm_fill(sp
, ep
, lastline
, P_BOTTOM
))
278 * If more than a full screen from the last line of the file,
279 * put the new line in the middle of the screen.
287 * If less than half a screen away, scroll up until the line is
288 * the first line on the screen.
290 lcnt
= svi_sm_nlines(sp
, ep
, HMAP
, LNO
, HALFTEXT(sp
));
291 if (lcnt
< HALFTEXT(sp
)) {
293 if (svi_sm_1down(sp
, ep
))
299 * If less than half a screen from the top of the file, put the first
300 * line of the file at the top of the screen. Otherwise, put the line
301 * in the middle of the screen.
305 lcnt
= svi_sm_nlines(sp
, ep
, &tmp
, LNO
, HALFTEXT(sp
));
306 if (lcnt
< HALFTEXT(sp
)) {
307 if (svi_sm_fill(sp
, ep
, 1, P_TOP
))
310 middle
: if (svi_sm_fill(sp
, ep
, LNO
, P_MIDDLE
))
315 * At this point we know part of the line is on the screen. Since
316 * scrolling is done using logical lines, not physical, all of the
317 * line may not be on the screen. While that's not necessarily bad,
318 * if the part the cursor is on isn't there, we're going to lose.
319 * This can be tricky; if the line covers the entire screen, lno
320 * may be the same as both ends of the map, that's why we test BOTH
321 * the top and the bottom of the map. This isn't a problem for
322 * left-right scrolling, the cursor movement code handles the problem.
324 * There's a performance issue here if editing *really* long lines.
325 * This gets to the right spot by scrolling, and, in a binary, by
326 * scrolling hundreds of lines. If the adjustment looks like it's
327 * going to be a serious problem, refill the screen and repaint.
329 adjust
: if (!O_ISSET(sp
, O_LEFTRIGHT
) &&
330 (LNO
== HMAP
->lno
|| LNO
== TMAP
->lno
)) {
331 cnt
= svi_screens(sp
, ep
, LNO
, &CNO
);
332 if (LNO
== HMAP
->lno
&& cnt
< HMAP
->off
)
333 if ((HMAP
->off
- cnt
) > HALFTEXT(sp
)) {
335 svi_sm_fill(sp
, ep
, OOBLNO
, P_TOP
);
338 while (cnt
< HMAP
->off
)
339 if (svi_sm_1down(sp
, ep
))
341 if (LNO
== TMAP
->lno
&& cnt
> TMAP
->off
)
342 if ((cnt
- TMAP
->off
) > HALFTEXT(sp
)) {
344 svi_sm_fill(sp
, ep
, OOBLNO
, P_BOTTOM
);
347 while (cnt
> TMAP
->off
)
348 if (svi_sm_1up(sp
, ep
))
352 /* If the screen needs to be repainted, skip cursor optimization. */
353 if (F_ISSET(sp
, S_REDRAW
))
357 * 4: Cursor movements.
359 * Decide cursor position. If the line has changed, the cursor has
360 * moved over a tab, or don't know where the cursor was, reparse the
361 * line. Note, if we think that the cursor "hasn't moved", reparse
362 * the line. This is 'cause if it hasn't moved, we've almost always
365 * Otherwise, we've just moved over fixed-width characters, and can
366 * calculate the left/right scrolling and cursor movement without
367 * reparsing the line. Note that we don't know which (if any) of
368 * the characters between the old and new cursor positions changed.
371 * With some work, it should be possible to handle tabs quickly, at
372 * least in obvious situations, like moving right and encountering
373 * a tab, without reparsing the whole line.
376 /* If the line we're working with has changed, reparse. */
377 if (F_ISSET(SVP(sp
), SVI_CUR_INVALID
) || LNO
!= OLNO
) {
378 F_CLR(SVP(sp
), SVI_CUR_INVALID
);
382 /* Otherwise, if nothing's changed, go fast. */
387 * Get the current line. If this fails, we either have an empty
388 * file and can just repaint, or there's a real problem. This
389 * isn't a performance issue because there aren't any ways to get
392 if ((p
= file_gline(sp
, ep
, LNO
, &len
)) == NULL
) {
393 if (file_lline(sp
, ep
, &lastline
))
397 GETLINE_ERR(sp
, LNO
);
402 /* This is just a test. */
403 if (CNO
>= len
&& len
!= 0) {
404 msgq(sp
, M_ERR
, "Error: %s/%d: cno (%u) >= len (%u)",
405 tail(__FILE__
), __LINE__
, CNO
, len
);
410 * The basic scheme here is to look at the characters in between
411 * the old and new positions and decide how big they are on the
412 * screen, and therefore, how many screen positions to move.
417 * 4a: Cursor moved left.
419 * Point to the old character. The old cursor position can
420 * be past EOL if, for example, we just deleted the rest of
421 * the line. In this case, since we don't know the width of
422 * the characters we traversed, we have to do it slowly.
425 cnt
= (OCNO
- CNO
) + 1;
430 * Quit sanity check -- it's hard to figure out exactly when
431 * we cross a screen boundary as we do in the cursor right
432 * movement. If cnt is so large that we're going to cross the
433 * boundary no matter what, stop now.
435 if (SCNO
+ 1 + MAX_CHARACTER_COLUMNS
< cnt
)
439 * Count up the widths of the characters. If it's a tab
440 * character, go do it the the slow way.
442 for (cwtotal
= 0; cnt
--; cwtotal
+= cname
[ch
].len
)
443 if ((ch
= *(u_char
*)p
--) == '\t')
447 * Decrement the screen cursor by the total width of the
448 * characters minus 1.
453 * If we're moving left, and there's a wide character in the
454 * current position, go to the end of the character.
456 if (cname
[ch
].len
> 1)
457 cwtotal
-= cname
[ch
].len
- 1;
460 * If the new column moved us out of the current screen,
461 * calculate a new screen.
463 if (SCNO
< cwtotal
) {
464 lscreen
: if (O_ISSET(sp
, O_LEFTRIGHT
)) {
465 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
474 * 4b: Cursor moved right.
476 * Point to the first character to the right.
482 * Count up the widths of the characters. If it's a tab
483 * character, go do it the the slow way. If we cross a
484 * screen boundary, we can quit.
486 for (cwtotal
= SCNO
; cnt
--;) {
487 if ((ch
= *(u_char
*)p
++) == '\t')
489 if ((cwtotal
+= cname
[ch
].len
) >= SCREEN_COLS(sp
))
494 * Increment the screen cursor by the total width of the
500 * If the new column moved us out of the current screen,
501 * calculate a new screen.
503 if (SCNO
>= SCREEN_COLS(sp
)) {
504 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
505 SCNO
-= SCREEN_COLS(sp
);
506 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
514 fast
: getyx(stdscr
, y
, x
); /* Just move the cursor. */
515 y
-= sp
->woff
; /* Correct for split screens. */
518 slow
: /* Find the current line in the map. */
519 for (smp
= HMAP
; smp
->lno
!= LNO
; ++smp
);
522 * If doing left-right scrolling and the cursor movement has changed
523 * the screen being displayed, scroll it. If we're painting the info
524 * line, however, just scroll that single line.
526 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
527 cnt
= svi_screens(sp
, ep
, LNO
, &CNO
) % SCREEN_COLS(sp
);
528 if (cnt
!= HMAP
->off
) {
529 if (ISINFOLINE(sp
, smp
))
532 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
539 * Update screen lines for this file line until we have a new
540 * screen cursor position.
542 for (y
= -1; smp
<= TMAP
&& smp
->lno
== LNO
; ++smp
) {
543 if (svi_line(sp
, ep
, smp
, &y
, &SCNO
))
549 /* Not too bad, move the cursor. */
553 * Lost big, do what you have to do. We flush the cache since
554 * S_REDRAW gets set when the screen isn't worth fixing, and
555 * it's simpler to just repaint. So, don't trust anything that
556 * we think we know about it.
558 paint
: for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
560 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
561 if (svi_line(sp
, ep
, smp
, &y
, &SCNO
))
565 * If it's a small screen and we're redrawing, clear the unused
566 * lines, ex may have overwritten them.
568 if (F_ISSET(sp
, S_REDRAW
)) {
569 if (ISSMALLSCREEN(sp
))
570 for (cnt
= sp
->t_rows
; cnt
<= sp
->t_maxrows
; ++cnt
) {
577 /* Update saved information. */
581 /* Refresh the screen. */
582 if (F_ISSET(sp
, S_REFRESH
)) {
584 F_CLR(sp
, S_REFRESH
);
588 * If there are any keys waiting, we don't ring the bell or update
589 * the messages. The reason is that there may be multiple messages,
590 * and we're going to get the wrong key as a message delimiter.
593 * This is wrong. If you type quickly enough, the status message
594 * for a screen may not be displayed during a split command because
595 * there were keys waiting so we switched screens without ever
596 * displaying it. Maybe the split code should flush messages?
598 if (!sex_key_wait(sp
)) {
599 if (F_ISSET(sp
, S_BELLSCHED
))
602 * If the bottom line isn't in use by the colon command:
604 * Display any messages. Don't test S_UPDATE_MODE. The
605 * message printing routine set it to avoid anyone else
606 * destroying the message we're about to display.
608 * If the bottom line isn't in use by anyone, put out the
609 * standard status line.
611 if (!F_ISSET(SVP(sp
), SVI_INFOLINE
))
612 if (sp
->msgq
.lh_first
!= NULL
&&
613 !F_ISSET(sp
->msgq
.lh_first
, M_EMPTY
))
615 else if (!F_ISSET(sp
, S_UPDATE_MODE
))
616 svi_modeline(sp
, ep
);
619 /* Place the cursor. */
622 /* Flush it all out. */
630 * Flush any accumulated messages.
642 #define MCONTMSG " [More ...]"
644 /* Display the messages. */
646 for (mp
= sp
->msgq
.lh_first
, p
= NULL
;
647 mp
!= NULL
&& !F_ISSET(mp
, M_EMPTY
); mp
= mp
->q
.le_next
) {
651 lcont
: /* Move to the message line and clear it. */
652 MOVE(sp
, INFOLINE(sp
), 0);
656 * Turn on standout mode if requested, or, if we've split
657 * the screen and need a divider.
659 if (F_ISSET(mp
, M_INV_VIDEO
) ||
660 sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
664 * Print up to the "more" message. Avoid the last character
665 * in the last line, some hardware doesn't like it.
667 if (svi_ncols(sp
, p
, mp
->len
, NULL
) < sp
->cols
- 1)
670 len
= (sp
->cols
- sizeof(MCONTMSG
)) - 1;
675 chlen
= cname
[ch
].len
;
680 ADDNSTR(cname
[ch
].name
, chlen
);
684 * If more, print continue message. If user key fails,
685 * keep showing the messages anyway.
687 if (mp
->len
|| (mp
->q
.le_next
!= NULL
&&
688 !F_ISSET(mp
->q
.le_next
, M_EMPTY
))) {
689 ADDNSTR(MCONTMSG
, sizeof(MCONTMSG
) - 1);
692 if (term_key(sp
, &ch
, 0) != INP_OK
)
694 if (sp
->special
[ch
] == K_CR
||
695 sp
->special
[ch
] == K_NL
|| isblank(ch
))
701 /* Turn off standout mode. */
702 if (F_ISSET(mp
, M_INV_VIDEO
) ||
703 sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
716 #define MODESIZE (RULERSIZE + 15)
720 * Update the mode line.
727 char *s
, buf
[RULERSIZE
];
729 MOVE(sp
, INFOLINE(sp
), 0);
732 /* Display the dividing line. */
733 if (sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
736 /* Display the ruler. */
737 if (O_ISSET(sp
, O_RULER
) && sp
->cols
> RULERSIZE
+ 2) {
738 MOVE(sp
, INFOLINE(sp
), sp
->cols
/ 2 - RULERSIZE
/ 2);
741 sizeof(buf
), "%lu,%lu", sp
->lno
, sp
->cno
+ 1);
745 /* Show the modified bit. */
746 if (O_ISSET(sp
, O_SHOWDIRTY
) &&
747 F_ISSET(ep
, F_MODIFIED
) && sp
->cols
> MODESIZE
) {
748 MOVE(sp
, INFOLINE(sp
), sp
->cols
- 9);
753 * Show the mode. Leave the last character blank, in case it's a
754 * really dumb terminal with hardware scroll. Second, don't try
755 * to *paint* the last character, SunOS 4.1.1 and Ultrix 4.2 curses
756 * won't let you paint the last character in the screen.
758 if (O_ISSET(sp
, O_SHOWMODE
) && sp
->cols
> MODESIZE
) {
759 MOVE(sp
, INFOLINE(sp
), sp
->cols
- 8);
760 s
= F_ISSET(sp
, S_INPUT
) ? " Input" : "Command";
771 #define DIVIDESIZE 10
773 char buf
[DIVIDESIZE
+ 1];
775 dividesize
= DIVIDESIZE
> sp
->cols
? sp
->cols
: DIVIDESIZE
;
776 memset(buf
, ' ', dividesize
);
777 if (standout() == ERR
)
779 ADDNSTR(buf
, dividesize
);
780 if (standend() == ERR
)