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.43 1993/12/23 10:22:03 bostic Exp $ (Berkeley) $Date: 1993/12/23 10:22:03 $";
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
*));
35 * 1: Resize the screen.
37 * Notice that a resize is requested, and set up everything so that
38 * the file gets reinitialized. Done here, instead of in the vi loop
39 * because there may be other initialization that other screens need
40 * to do. The actual changing of the row/column values was done by
41 * calling the ex options code which put them into the environment,
42 * which is used by curses. Stupid, but ugly.
44 if (F_ISSET(sp
, S_RESIZE
)) {
45 /* Reinitialize curses. */
46 if (svi_curses_end(sp
) || svi_curses_init(sp
))
49 /* Lose any svi_screens() cached information. */
50 SVP(sp
)->ss_lno
= OOBLNO
;
53 * Fill the map, incidentally losing any svi_line()
56 if (sp
->s_fill(sp
, ep
, sp
->lno
, P_FILL
))
58 F_CLR(sp
, S_RESIZE
| S_REFORMAT
);
65 * If S_REFRESH is set in the current screen, repaint everything
68 if (F_ISSET(sp
, S_REFRESH
))
69 for (tsp
= sp
->gp
->dq
.cqh_first
;
70 tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
74 * 3: Related or dirtied screens, or screens with messages.
76 * If related screens share a view into a file, they may have been
77 * modified as well. Refresh any screens with paint or dirty bits
78 * set, or where messages are waiting. Finally, if we refresh any
79 * screens other than the current one, the cursor will be trashed.
81 paintbits
= S_REDRAW
| S_REFORMAT
| S_REFRESH
;
82 if (O_ISSET(sp
, O_NUMBER
))
83 paintbits
|= S_RENUMBER
;
84 for (tsp
= sp
->gp
->dq
.cqh_first
;
85 tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
87 (F_ISSET(tsp
, paintbits
) ||
88 F_ISSET(SVP(tsp
), SVI_SCREENDIRTY
) ||
89 tsp
->msgq
.lh_first
!= NULL
&&
90 !F_ISSET(tsp
->msgq
.lh_first
, M_EMPTY
))) {
91 (void)svi_paint(tsp
, tsp
->ep
);
92 F_CLR(SVP(tsp
), SVI_SCREENDIRTY
);
93 F_SET(SVP(sp
), SVI_CUR_INVALID
);
97 * 4: Refresh the current screen.
99 * Always refresh the current screen, it may be a cursor movement.
100 * Also, always do it last -- that way, S_REFRESH can be set in
101 * the current screen only, and the screen won't flash.
103 F_CLR(sp
, SVI_SCREENDIRTY
);
104 return (svi_paint(sp
, ep
));
109 * This is the guts of the vi curses screen code. The idea is that
110 * the SCR structure passed in contains the new coordinates of the
111 * screen. What makes this hard is that we don't know how big
112 * characters are, doing input can put the cursor in illegal places,
113 * and we're frantically trying to avoid repainting unless it's
114 * absolutely necessary. If you change this code, you'd better know
115 * what you're doing. It's subtle and quick to anger.
125 recno_t lastline
, lcnt
;
126 size_t cwtotal
, cnt
, len
, x
, y
;
131 #define OLNO svp->olno
133 #define OCNO svp->ocno
134 #define SCNO svp->sc_col
140 * 1: Reformat the lines.
142 * If the lines themselves have changed (:set list, for example),
143 * fill in the map from scratch. Adjust the screen that's being
144 * displayed if the leftright flag is set.
146 if (F_ISSET(sp
, S_REFORMAT
)) {
147 /* Toss svi_screens() cached information. */
148 SVP(sp
)->ss_lno
= OOBLNO
;
150 /* Toss svi_line() cached information. */
151 if (svi_sm_fill(sp
, ep
, HMAP
->lno
, P_TOP
))
153 if (O_ISSET(sp
, O_LEFTRIGHT
) &&
154 (cnt
= svi_screens(sp
, ep
, LNO
, &CNO
)) != 1)
155 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
157 F_CLR(sp
, S_REFORMAT
);
164 * Line changes can cause the top line to change as well. As
165 * before, if the movement is large, the screen is repainted.
169 * Tiny screens cannot be permitted into the "scrolling" parts of
170 * the smap code for two reasons. If the screen size is 1 line,
171 * HMAP == TMAP and the code will quickly drop core. If the screen
172 * size is 2, none of the divisions by 2 will work, and scrolling
173 * won't work. In fact, because no line change will be less than
174 * HALFTEXT(sp), we always ending up "filling" the map, with a
175 * P_MIDDLE flag, which isn't what the user wanted. Tiny screens
176 * can go into the "fill" portions of the smap code, however.
178 if (sp
->t_rows
<= 2) {
179 if (LNO
< HMAP
->lno
) {
180 if (svi_sm_fill(sp
, ep
, LNO
, P_TOP
))
182 } else if (LNO
> TMAP
->lno
)
183 if (svi_sm_fill(sp
, ep
, LNO
, P_BOTTOM
))
185 if (sp
->t_rows
== 1) {
186 HMAP
->off
= svi_screens(sp
, ep
, LNO
, &CNO
);
196 * Users can use the window, w300, w1200 and w9600 options to make
197 * the screen artificially small. The behavior of these options
198 * in the historic vi wasn't all that consistent, and, in fact, it
199 * was never documented how various screen movements affected the
200 * screen size. Generally, one of three things would happen:
201 * 1: The screen would expand in size, showing the line
202 * 2: The screen would scroll, showing the line
203 * 3: The screen would compress to its smallest size and
205 * In general, scrolling didn't cause compression (200^D was handled
206 * the same as ^D), movement to a specific line would (:N where N
207 * was 1 line below the screen caused a screen compress), and cursor
208 * movement would scroll if it was 11 lines or less, and compress if
209 * it was more than 11 lines. (And, no, I have no idea where the 11
212 * What we do is try and figure out if the line is less than half of
213 * a full screen away. If it is, we expand the screen if there's
214 * room, and then scroll as necessary. The alternative is to compress
218 * This code is a special case from beginning to end. Unfortunately,
219 * home modems are still slow enough that it's worth having.
222 * If the line a really long one, i.e. part of the line is on the
223 * screen but the column offset is not, we'll end up in the adjust
224 * code, when we should probably have compressed the screen.
226 if (ISSMALLSCREEN(sp
))
227 if (LNO
< HMAP
->lno
) {
228 lcnt
= svi_sm_nlines(sp
, ep
, HMAP
, LNO
, sp
->t_maxrows
);
229 if (lcnt
<= HALFSCREEN(sp
))
230 for (; lcnt
&& sp
->t_rows
!= sp
->t_maxrows
;
231 --lcnt
, ++sp
->t_rows
) {
233 if (svi_sm_1down(sp
, ep
))
238 } else if (LNO
> TMAP
->lno
) {
239 lcnt
= svi_sm_nlines(sp
, ep
, TMAP
, LNO
, sp
->t_maxrows
);
240 if (lcnt
<= HALFSCREEN(sp
))
241 for (; lcnt
&& sp
->t_rows
!= sp
->t_maxrows
;
242 --lcnt
, ++sp
->t_rows
) {
243 if (svi_sm_next(sp
, ep
, TMAP
, TMAP
+ 1))
246 if (svi_line(sp
, ep
, TMAP
, NULL
, NULL
))
250 small_fill
: MOVE(sp
, INFOLINE(sp
), 0);
252 for (; sp
->t_rows
> sp
->t_minrows
;
253 --sp
->t_rows
, --TMAP
) {
254 MOVE(sp
, TMAP
- HMAP
, 0);
257 if (svi_sm_fill(sp
, ep
, LNO
, P_FILL
))
267 if (LNO
>= HMAP
->lno
) {
268 if (LNO
<= TMAP
->lno
)
272 * If less than half a screen away, scroll down until the
273 * line is on the screen.
275 lcnt
= svi_sm_nlines(sp
, ep
, TMAP
, LNO
, HALFTEXT(sp
));
276 if (lcnt
< HALFTEXT(sp
)) {
278 if (svi_sm_1up(sp
, ep
))
284 * If less than a full screen from the bottom of the file, put
285 * the last line of the file on the bottom of the screen. The
286 * calculation is safe because we know there's at least one
287 * full screen of lines, otherwise couldn't have gotten here.
289 if (file_lline(sp
, ep
, &lastline
))
293 lcnt
= svi_sm_nlines(sp
, ep
, &tmp
, lastline
, sp
->t_rows
);
294 if (lcnt
< sp
->t_rows
) {
295 if (svi_sm_fill(sp
, ep
, lastline
, P_BOTTOM
))
302 * If more than a full screen from the last line of the file,
303 * put the new line in the middle of the screen.
311 * If less than half a screen away, scroll up until the line is
312 * the first line on the screen.
314 lcnt
= svi_sm_nlines(sp
, ep
, HMAP
, LNO
, HALFTEXT(sp
));
315 if (lcnt
< HALFTEXT(sp
)) {
317 if (svi_sm_1down(sp
, ep
))
323 * If less than half a screen from the top of the file, put the first
324 * line of the file at the top of the screen. Otherwise, put the line
325 * in the middle of the screen.
329 lcnt
= svi_sm_nlines(sp
, ep
, &tmp
, LNO
, HALFTEXT(sp
));
330 if (lcnt
< HALFTEXT(sp
)) {
331 if (svi_sm_fill(sp
, ep
, 1, P_TOP
))
334 middle
: if (svi_sm_fill(sp
, ep
, LNO
, P_MIDDLE
))
339 * At this point we know part of the line is on the screen. Since
340 * scrolling is done using logical lines, not physical, all of the
341 * line may not be on the screen. While that's not necessarily bad,
342 * if the part the cursor is on isn't there, we're going to lose.
343 * This can be tricky; if the line covers the entire screen, lno
344 * may be the same as both ends of the map, that's why we test BOTH
345 * the top and the bottom of the map. This isn't a problem for
346 * left-right scrolling, the cursor movement code handles the problem.
348 * There's a performance issue here if editing *really* long lines.
349 * This gets to the right spot by scrolling, and, in a binary, by
350 * scrolling hundreds of lines. If the adjustment looks like it's
351 * going to be a serious problem, refill the screen and repaint.
353 adjust
: if (!O_ISSET(sp
, O_LEFTRIGHT
) &&
354 (LNO
== HMAP
->lno
|| LNO
== TMAP
->lno
)) {
355 cnt
= svi_screens(sp
, ep
, LNO
, &CNO
);
356 if (LNO
== HMAP
->lno
&& cnt
< HMAP
->off
)
357 if ((HMAP
->off
- cnt
) > HALFTEXT(sp
)) {
359 svi_sm_fill(sp
, ep
, OOBLNO
, P_TOP
);
362 while (cnt
< HMAP
->off
)
363 if (svi_sm_1down(sp
, ep
))
365 if (LNO
== TMAP
->lno
&& cnt
> TMAP
->off
)
366 if ((cnt
- TMAP
->off
) > HALFTEXT(sp
)) {
368 svi_sm_fill(sp
, ep
, OOBLNO
, P_BOTTOM
);
371 while (cnt
> TMAP
->off
)
372 if (svi_sm_1up(sp
, ep
))
376 /* If the screen needs to be repainted, skip cursor optimization. */
377 if (F_ISSET(sp
, S_REDRAW
))
381 * 4: Cursor movements.
383 * Decide cursor position. If the line has changed, the cursor has
384 * moved over a tab, or don't know where the cursor was, reparse the
385 * line. Note, if we think that the cursor "hasn't moved", reparse
386 * the line. This is 'cause if it hasn't moved, we've almost always
389 * Otherwise, we've just moved over fixed-width characters, and can
390 * calculate the left/right scrolling and cursor movement without
391 * reparsing the line. Note that we don't know which (if any) of
392 * the characters between the old and new cursor positions changed.
395 * With some work, it should be possible to handle tabs quickly, at
396 * least in obvious situations, like moving right and encountering
397 * a tab, without reparsing the whole line.
400 /* If the line we're working with has changed, reparse. */
401 if (F_ISSET(SVP(sp
), SVI_CUR_INVALID
) || LNO
!= OLNO
) {
402 F_CLR(SVP(sp
), SVI_CUR_INVALID
);
406 /* Otherwise, if nothing's changed, go fast. */
411 * Get the current line. If this fails, we either have an empty
412 * file and can just repaint, or there's a real problem. This
413 * isn't a performance issue because there aren't any ways to get
416 if ((p
= file_gline(sp
, ep
, LNO
, &len
)) == NULL
) {
417 if (file_lline(sp
, ep
, &lastline
))
421 GETLINE_ERR(sp
, LNO
);
426 /* This is just a test. */
427 if (CNO
>= len
&& len
!= 0) {
428 msgq(sp
, M_ERR
, "Error: %s/%d: cno (%u) >= len (%u)",
429 tail(__FILE__
), __LINE__
, CNO
, len
);
434 * The basic scheme here is to look at the characters in between
435 * the old and new positions and decide how big they are on the
436 * screen, and therefore, how many screen positions to move.
438 cname
= sp
->gp
->cname
;
441 * 4a: Cursor moved left.
443 * Point to the old character. The old cursor position can
444 * be past EOL if, for example, we just deleted the rest of
445 * the line. In this case, since we don't know the width of
446 * the characters we traversed, we have to do it slowly.
449 cnt
= (OCNO
- CNO
) + 1;
454 * Quit sanity check -- it's hard to figure out exactly when
455 * we cross a screen boundary as we do in the cursor right
456 * movement. If cnt is so large that we're going to cross the
457 * boundary no matter what, stop now.
459 if (SCNO
+ 1 + MAX_CHARACTER_COLUMNS
< cnt
)
463 * Count up the widths of the characters. If it's a tab
464 * character, go do it the the slow way.
466 for (cwtotal
= 0; cnt
--; cwtotal
+= cname
[ch
].len
)
467 if ((ch
= *(u_char
*)p
--) == '\t')
471 * Decrement the screen cursor by the total width of the
472 * characters minus 1.
477 * If we're moving left, and there's a wide character in the
478 * current position, go to the end of the character.
480 if (cname
[ch
].len
> 1)
481 cwtotal
-= cname
[ch
].len
- 1;
484 * If the new column moved us out of the current screen,
485 * calculate a new screen.
487 if (SCNO
< cwtotal
) {
488 lscreen
: if (O_ISSET(sp
, O_LEFTRIGHT
)) {
489 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
498 * 4b: Cursor moved right.
500 * Point to the first character to the right.
506 * Count up the widths of the characters. If it's a tab
507 * character, go do it the the slow way. If we cross a
508 * screen boundary, we can quit.
510 for (cwtotal
= SCNO
; cnt
--;) {
511 if ((ch
= *(u_char
*)p
++) == '\t')
513 if ((cwtotal
+= cname
[ch
].len
) >= SCREEN_COLS(sp
))
518 * Increment the screen cursor by the total width of the
524 * If the new column moved us out of the current screen,
525 * calculate a new screen.
527 if (SCNO
>= SCREEN_COLS(sp
)) {
528 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
529 SCNO
-= SCREEN_COLS(sp
);
530 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
539 * 4c: Fast cursor update.
541 * Retrieve the current cursor position, and correct it
544 fast
: getyx(stdscr
, y
, x
);
549 * 4d: Slow cursor update.
551 * Walk through the map and find the current line. If doing left-right
552 * scrolling and the cursor movement has changed the screen displayed,
553 * scroll the screen left or right, unless we're updating the info line
554 * in which case we just scroll that one line. Then update the screen
555 * lines for this file line until we have a new screen cursor position.
557 slow
: for (smp
= HMAP
; smp
->lno
!= LNO
; ++smp
);
558 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
559 cnt
= svi_screens(sp
, ep
, LNO
, &CNO
) % SCREEN_COLS(sp
);
560 if (cnt
!= HMAP
->off
) {
561 if (ISINFOLINE(sp
, smp
))
564 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
569 for (y
= -1; smp
<= TMAP
&& smp
->lno
== LNO
; ++smp
) {
570 if (svi_line(sp
, ep
, smp
, &y
, &SCNO
))
578 * 5: Repaint the entire screen.
580 * Lost big, do what you have to do. We flush the cache as S_REDRAW
581 * gets set when the screen isn't worth fixing, and it's simpler to
582 * repaint. So, don't trust anything that we think we know about it.
584 paint
: for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
586 for (smp
= HMAP
; smp
<= TMAP
; ++smp
)
587 if (svi_line(sp
, ep
, smp
, &y
, &SCNO
))
590 * If it's a small screen and we're redrawing, clear the unused lines,
591 * ex may have overwritten them.
593 if (F_ISSET(sp
, S_REDRAW
)) {
594 if (ISSMALLSCREEN(sp
))
595 for (cnt
= sp
->t_rows
; cnt
<= sp
->t_maxrows
; ++cnt
) {
605 * 6: Repaint the line numbers.
607 * If O_NUMBER is set and the S_RENUMBER bit is set, and we didn't
608 * repaint the screen, repaint all of the line numbers, they've
611 number
: if (O_ISSET(sp
, O_NUMBER
) && F_ISSET(sp
, S_RENUMBER
) && !didpaint
) {
612 if (svi_number(sp
, ep
))
614 F_CLR(sp
, S_RENUMBER
);
618 * 7: Refresh the screen.
620 * If the screen was corrupted, refresh it.
622 if (F_ISSET(sp
, S_REFRESH
)) {
624 F_CLR(sp
, S_REFRESH
);
627 if (F_ISSET(sp
, S_BELLSCHED
))
630 * If the bottom line isn't in use by the colon command:
632 * Display any messages. Don't test S_UPDATE_MODE. The
633 * message printing routine set it to avoid anyone else
634 * destroying the message we're about to display.
636 * If the bottom line isn't in use by anyone, put out the
637 * standard status line.
639 if (!F_ISSET(SVP(sp
), SVI_INFOLINE
))
640 if (sp
->msgq
.lh_first
!= NULL
&&
641 !F_ISSET(sp
->msgq
.lh_first
, M_EMPTY
))
643 else if (!F_ISSET(sp
, S_UPDATE_MODE
))
644 svi_modeline(sp
, ep
);
646 /* Update saved information. */
650 /* Place the cursor. */
653 /* Flush it all out. */
661 * Flush any accumulated messages.
674 #define MCONTMSG " [More ...]"
676 /* Display the messages. */
677 cname
= sp
->gp
->cname
;
678 for (mp
= sp
->msgq
.lh_first
, p
= NULL
;
679 mp
!= NULL
&& !F_ISSET(mp
, M_EMPTY
); mp
= mp
->q
.le_next
) {
682 lcont
: /* Move to the message line and clear it. */
683 MOVE(sp
, INFOLINE(sp
), 0);
687 * Turn on standout mode if requested, or, if we've split
688 * the screen and need a divider.
690 if (F_ISSET(mp
, M_INV_VIDEO
) ||
691 sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
695 * Print up to the "more" message. Avoid the last character
696 * in the last line, some hardware doesn't like it.
698 if (svi_ncols(sp
, p
, mp
->len
, NULL
) < sp
->cols
- 1)
701 len
= (sp
->cols
- sizeof(MCONTMSG
)) - 1;
706 chlen
= cname
[ch
].len
;
711 ADDNSTR(cname
[ch
].name
, chlen
);
715 * If more, print continue message. If user key fails,
716 * keep showing the messages anyway.
718 if (mp
->len
|| (mp
->q
.le_next
!= NULL
&&
719 !F_ISSET(mp
->q
.le_next
, M_EMPTY
))) {
720 ADDNSTR(MCONTMSG
, sizeof(MCONTMSG
) - 1);
723 if (term_user_key(sp
, &ikey
) != INP_OK
)
725 if (ikey
.value
== K_CR
||
726 ikey
.value
== K_NL
|| ikey
.ch
== ' ')
732 /* Turn off standout mode. */
733 if (F_ISSET(mp
, M_INV_VIDEO
) ||
734 sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
747 #define MODESIZE (RULERSIZE + 15)
751 * Update the mode line.
758 char *s
, buf
[RULERSIZE
];
760 MOVE(sp
, INFOLINE(sp
), 0);
763 /* Display a dividing line if not the bottom screen. */
764 if (sp
->q
.cqe_next
!= (void *)&sp
->gp
->dq
)
767 /* Display the ruler. */
768 if (O_ISSET(sp
, O_RULER
) && sp
->cols
> RULERSIZE
+ 2) {
769 MOVE(sp
, INFOLINE(sp
), sp
->cols
/ 2 - RULERSIZE
/ 2);
772 sizeof(buf
), "%lu,%lu", sp
->lno
, sp
->cno
+ 1);
776 /* Show the modified bit. */
777 if (O_ISSET(sp
, O_SHOWDIRTY
) &&
778 F_ISSET(ep
, F_MODIFIED
) && sp
->cols
> MODESIZE
) {
779 MOVE(sp
, INFOLINE(sp
), sp
->cols
- 9);
784 * Show the mode. Leave the last character blank, in case it's a
785 * really dumb terminal with hardware scroll. Second, don't try
786 * to *paint* the last character, SunOS 4.1.1 and Ultrix 4.2 curses
787 * won't let you paint the last character in the screen.
789 if (O_ISSET(sp
, O_SHOWMODE
) && sp
->cols
> MODESIZE
) {
790 MOVE(sp
, INFOLINE(sp
), sp
->cols
- 8);
791 s
= F_ISSET(sp
, S_INPUT
) ? " Input" : "Command";
800 * Draw a dividing line between the screens.
808 #define DIVIDESTR "+=+=+=+=+=+=+=+"
809 len
= sizeof(DIVIDESTR
) - 1 > sp
->cols
?
810 sp
->cols
: sizeof(DIVIDESTR
) - 1;
811 ADDNSTR(DIVIDESTR
, len
);