Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / vs_refresh.c
blob3db106a3ada5bf048b11e685002508507939f7db
1 /* $NetBSD: vs_refresh.c,v 1.2 2008/12/05 22:51:43 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "Id: vs_refresh.c,v 10.50 2001/06/25 15:19:37 skimo Exp (Berkeley) Date: 2001/06/25 15:19:37";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "../common/common.h"
30 #include "vi.h"
32 #define UPDATE_CURSOR 0x01 /* Update the cursor. */
33 #define UPDATE_SCREEN 0x02 /* Flush to screen. */
35 static void vs_modeline __P((SCR *));
36 static int vs_paint __P((SCR *, u_int));
39 * vs_refresh --
40 * Refresh all screens.
42 * PUBLIC: int vs_refresh __P((SCR *, int));
44 int
45 vs_refresh(SCR *sp, int forcepaint)
47 GS *gp;
48 SCR *tsp;
49 int need_refresh;
50 u_int priv_paint, pub_paint;
52 gp = sp->gp;
55 * 1: Refresh the screen.
57 * If SC_SCR_REDRAW is set in the current screen, repaint everything
58 * that we can find, including status lines.
60 if (F_ISSET(sp, SC_SCR_REDRAW))
61 for (tsp = sp->wp->scrq.cqh_first;
62 tsp != (void *)&sp->wp->scrq; tsp = tsp->q.cqe_next)
63 if (tsp != sp)
64 F_SET(tsp, SC_SCR_REDRAW | SC_STATUS);
67 * 2: Related or dirtied screens, or screens with messages.
69 * If related screens share a view into a file, they may have been
70 * modified as well. Refresh any screens that aren't exiting that
71 * have paint or dirty bits set. Always update their screens, we
72 * are not likely to get another chance. Finally, if we refresh any
73 * screens other than the current one, the cursor will be trashed.
75 pub_paint = SC_SCR_REFORMAT | SC_SCR_REDRAW;
76 priv_paint = VIP_CUR_INVALID | VIP_N_REFRESH;
77 if (O_ISSET(sp, O_NUMBER))
78 priv_paint |= VIP_N_RENUMBER;
79 for (tsp = sp->wp->scrq.cqh_first;
80 tsp != (void *)&sp->wp->scrq; tsp = tsp->q.cqe_next)
81 if (tsp != sp && !F_ISSET(tsp, SC_EXIT | SC_EXIT_FORCE) &&
82 (F_ISSET(tsp, pub_paint) ||
83 F_ISSET(VIP(tsp), priv_paint))) {
84 (void)vs_paint(tsp,
85 (F_ISSET(VIP(tsp), VIP_CUR_INVALID) ?
86 UPDATE_CURSOR : 0) | UPDATE_SCREEN);
87 F_SET(VIP(sp), VIP_CUR_INVALID);
91 * 3: Refresh the current screen.
93 * Always refresh the current screen, it may be a cursor movement.
94 * Also, always do it last -- that way, SC_SCR_REDRAW can be set
95 * in the current screen only, and the screen won't flash.
97 if (vs_paint(sp, UPDATE_CURSOR | (!forcepaint &&
98 F_ISSET(sp, SC_SCR_VI) && KEYS_WAITING(sp) ? 0 : UPDATE_SCREEN)))
99 return (1);
102 * 4: Paint any missing status lines.
104 * XXX
105 * This is fairly evil. Status lines are written using the vi message
106 * mechanism, since we have no idea how long they are. Since we may be
107 * painting screens other than the current one, we don't want to make
108 * the user wait. We depend heavily on there not being any other lines
109 * currently waiting to be displayed and the message truncation code in
110 * the msgq_status routine working.
112 * And, finally, if we updated any status lines, make sure the cursor
113 * gets back to where it belongs.
115 for (need_refresh = 0, tsp = sp->wp->scrq.cqh_first;
116 tsp != (void *)&sp->wp->scrq; tsp = tsp->q.cqe_next)
117 if (F_ISSET(tsp, SC_STATUS)) {
118 need_refresh = 1;
119 vs_resolve(tsp, sp, 0);
121 if (need_refresh)
122 (void)gp->scr_refresh(sp, 0);
125 * A side-effect of refreshing the screen is that it's now ready
126 * for everything else, i.e. messages.
128 F_SET(sp, SC_SCR_VI);
129 return (0);
133 * vs_paint --
134 * This is the guts of the vi curses screen code. The idea is that
135 * the SCR structure passed in contains the new coordinates of the
136 * screen. What makes this hard is that we don't know how big
137 * characters are, doing input can put the cursor in illegal places,
138 * and we're frantically trying to avoid repainting unless it's
139 * absolutely necessary. If you change this code, you'd better know
140 * what you're doing. It's subtle and quick to anger.
142 static int
143 vs_paint(SCR *sp, u_int flags)
145 GS *gp;
146 SMAP *smp, tmp;
147 VI_PRIVATE *vip;
148 db_recno_t lastline, lcnt;
149 size_t cwtotal, cnt, len, notused, off, y;
150 int ch = 0, didpaint, isempty, leftright_warp;
151 CHAR_T *p;
153 #define LNO sp->lno /* Current file line. */
154 #define OLNO vip->olno /* Remembered file line. */
155 #define CNO sp->cno /* Current file column. */
156 #define OCNO vip->ocno /* Remembered file column. */
157 #define SCNO vip->sc_col /* Current screen column. */
159 gp = sp->gp;
160 vip = VIP(sp);
161 didpaint = leftright_warp = 0;
164 * 5: Reformat the lines.
166 * If the lines themselves have changed (:set list, for example),
167 * fill in the map from scratch. Adjust the screen that's being
168 * displayed if the leftright flag is set.
170 if (F_ISSET(sp, SC_SCR_REFORMAT)) {
171 /* Invalidate the line size cache. */
172 VI_SCR_CFLUSH(vip);
174 /* Toss vs_line() cached information. */
175 if (F_ISSET(sp, SC_SCR_TOP)) {
176 if (vs_sm_fill(sp, LNO, P_TOP))
177 return (1);
179 else if (F_ISSET(sp, SC_SCR_CENTER)) {
180 if (vs_sm_fill(sp, LNO, P_MIDDLE))
181 return (1);
182 } else
183 if (vs_sm_fill(sp, OOBLNO, P_TOP))
184 return (1);
185 F_SET(sp, SC_SCR_REDRAW);
189 * 6: Line movement.
191 * Line changes can cause the top line to change as well. As
192 * before, if the movement is large, the screen is repainted.
194 * 6a: Small screens.
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
204 * repaint.
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
210 * comes from.)
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
215 * and repaint.
217 * !!!
218 * This code is a special case from beginning to end. Unfortunately,
219 * home modems are still slow enough that it's worth having.
221 * XXX
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 (IS_SMALL(sp)) {
227 if (LNO < HMAP->lno) {
228 lcnt = vs_sm_nlines(sp, HMAP, LNO, sp->t_maxrows);
229 if (lcnt <= HALFSCREEN(sp))
230 for (; lcnt && sp->t_rows != sp->t_maxrows;
231 --lcnt, ++sp->t_rows) {
232 ++TMAP;
233 if (vs_sm_1down(sp))
234 return (1);
236 else
237 goto small_fill;
238 } else if (LNO > TMAP->lno) {
239 lcnt = vs_sm_nlines(sp, 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 (vs_sm_next(sp, TMAP, TMAP + 1))
244 return (1);
245 ++TMAP;
246 if (vs_line(sp, TMAP, NULL, NULL))
247 return (1);
249 else {
250 small_fill: (void)gp->scr_move(sp, LASTLINE(sp), 0);
251 (void)gp->scr_clrtoeol(sp);
252 for (; sp->t_rows > sp->t_minrows;
253 --sp->t_rows, --TMAP) {
254 (void)gp->scr_move(sp, TMAP - HMAP, 0);
255 (void)gp->scr_clrtoeol(sp);
257 if (vs_sm_fill(sp, LNO, P_FILL))
258 return (1);
259 F_SET(sp, SC_SCR_REDRAW);
260 goto adjust;
266 * 6b: Line down, or current screen.
268 if (LNO >= HMAP->lno) {
269 /* Current screen. */
270 if (LNO <= TMAP->lno)
271 goto adjust;
272 if (F_ISSET(sp, SC_SCR_TOP))
273 goto top;
274 if (F_ISSET(sp, SC_SCR_CENTER))
275 goto middle;
278 * If less than half a screen above the line, scroll down
279 * until the line is on the screen.
281 lcnt = vs_sm_nlines(sp, TMAP, LNO, HALFTEXT(sp));
282 if (lcnt < HALFTEXT(sp)) {
283 while (lcnt--)
284 if (vs_sm_1up(sp))
285 return (1);
286 goto adjust;
288 goto bottom;
292 * 6c: If not on the current screen, may request center or top.
294 if (F_ISSET(sp, SC_SCR_TOP))
295 goto top;
296 if (F_ISSET(sp, SC_SCR_CENTER))
297 goto middle;
300 * 6d: Line up.
302 lcnt = vs_sm_nlines(sp, HMAP, LNO, HALFTEXT(sp));
303 if (lcnt < HALFTEXT(sp)) {
305 * If less than half a screen below the line, scroll up until
306 * the line is the first line on the screen. Special check so
307 * that if the screen has been emptied, we refill it.
309 if (db_exist(sp, HMAP->lno)) {
310 while (lcnt--)
311 if (vs_sm_1down(sp))
312 return (1);
313 goto adjust;
317 * If less than a half screen from the bottom of the file,
318 * put the last line of the file on the bottom of the screen.
320 bottom: if (db_last(sp, &lastline))
321 return (1);
322 tmp.lno = LNO;
323 tmp.coff = HMAP->coff;
324 tmp.soff = 1;
325 lcnt = vs_sm_nlines(sp, &tmp, lastline+1, sp->t_rows);
326 if (lcnt < HALFTEXT(sp)) {
327 if (vs_sm_fill(sp, lastline, P_BOTTOM))
328 return (1);
329 F_SET(sp, SC_SCR_REDRAW);
330 goto adjust;
332 /* It's not close, just put the line in the middle. */
333 goto middle;
337 * If less than half a screen from the top of the file, put the first
338 * line of the file at the top of the screen. Otherwise, put the line
339 * in the middle of the screen.
341 tmp.lno = 1;
342 tmp.coff = HMAP->coff;
343 tmp.soff = 1;
344 lcnt = vs_sm_nlines(sp, &tmp, LNO, HALFTEXT(sp));
345 if (lcnt < HALFTEXT(sp)) {
346 if (vs_sm_fill(sp, 1, P_TOP))
347 return (1);
348 } else
349 middle: if (vs_sm_fill(sp, LNO, P_MIDDLE))
350 return (1);
351 if (0) {
352 top: if (vs_sm_fill(sp, LNO, P_TOP))
353 return (1);
355 F_SET(sp, SC_SCR_REDRAW);
358 * At this point we know part of the line is on the screen. Since
359 * scrolling is done using logical lines, not physical, all of the
360 * line may not be on the screen. While that's not necessarily bad,
361 * if the part the cursor is on isn't there, we're going to lose.
362 * This can be tricky; if the line covers the entire screen, lno
363 * may be the same as both ends of the map, that's why we test BOTH
364 * the top and the bottom of the map. This isn't a problem for
365 * left-right scrolling, the cursor movement code handles the problem.
367 * There's a performance issue here if editing *really* long lines.
368 * This gets to the right spot by scrolling, and, in a binary, by
369 * scrolling hundreds of lines. If the adjustment looks like it's
370 * going to be a serious problem, refill the screen and repaint.
372 adjust: if (!O_ISSET(sp, O_LEFTRIGHT) &&
373 (LNO == HMAP->lno || LNO == TMAP->lno)) {
374 cnt = vs_screens(sp, LNO, &CNO);
375 if (LNO == HMAP->lno && cnt < HMAP->soff) {
376 if ((HMAP->soff - cnt) > HALFTEXT(sp)) {
377 HMAP->soff = cnt;
378 vs_sm_fill(sp, OOBLNO, P_TOP);
379 F_SET(sp, SC_SCR_REDRAW);
380 } else
381 while (cnt < HMAP->soff)
382 if (vs_sm_1down(sp))
383 return (1);
385 if (LNO == TMAP->lno && cnt > TMAP->soff) {
386 if ((cnt - TMAP->soff) > HALFTEXT(sp)) {
387 TMAP->soff = cnt;
388 vs_sm_fill(sp, OOBLNO, P_BOTTOM);
389 F_SET(sp, SC_SCR_REDRAW);
390 } else
391 while (cnt > TMAP->soff)
392 if (vs_sm_1up(sp))
393 return (1);
398 * If the screen needs to be repainted, skip cursor optimization.
399 * However, in the code above we skipped leftright scrolling on
400 * the grounds that the cursor code would handle it. Make sure
401 * the right screen is up.
403 if (F_ISSET(sp, SC_SCR_REDRAW)) {
404 if (O_ISSET(sp, O_LEFTRIGHT))
405 goto slow;
406 goto paint;
410 * 7: Cursor movements (current screen only).
412 if (!LF_ISSET(UPDATE_CURSOR))
413 goto number;
416 * Decide cursor position. If the line has changed, the cursor has
417 * moved over a tab, or don't know where the cursor was, reparse the
418 * line. Otherwise, we've just moved over fixed-width characters,
419 * and can calculate the left/right scrolling and cursor movement
420 * without reparsing the line. Note that we don't know which (if any)
421 * of the characters between the old and new cursor positions changed.
423 * XXX
424 * With some work, it should be possible to handle tabs quickly, at
425 * least in obvious situations, like moving right and encountering
426 * a tab, without reparsing the whole line.
428 * If the line we're working with has changed, reread it..
430 if (F_ISSET(vip, VIP_CUR_INVALID) || LNO != OLNO)
431 goto slow;
433 /* Otherwise, if nothing's changed, ignore the cursor. */
434 if (CNO == OCNO)
435 goto fast;
438 * Get the current line. If this fails, we either have an empty
439 * file and can just repaint, or there's a real problem. This
440 * isn't a performance issue because there aren't any ways to get
441 * here repeatedly.
443 if (db_eget(sp, LNO, &p, &len, &isempty)) {
444 if (isempty)
445 goto slow;
446 return (1);
449 #ifdef DEBUG
450 /* Sanity checking. */
451 if (CNO >= len && len != 0) {
452 msgq(sp, M_ERR, "Error: %s/%d: cno (%u) >= len (%u)",
453 tail(__FILE__), __LINE__, CNO, len);
454 return (1);
456 #endif
458 * The basic scheme here is to look at the characters in between
459 * the old and new positions and decide how big they are on the
460 * screen, and therefore, how many screen positions to move.
462 if (CNO < OCNO) {
464 * 7a: Cursor moved left.
466 * Point to the old character. The old cursor position can
467 * be past EOL if, for example, we just deleted the rest of
468 * the line. In this case, since we don't know the width of
469 * the characters we traversed, we have to do it slowly.
471 p += OCNO;
472 cnt = (OCNO - CNO) + 1;
473 if (OCNO >= len)
474 goto slow;
477 * Quick sanity check -- it's hard to figure out exactly when
478 * we cross a screen boundary as we do in the cursor right
479 * movement. If cnt is so large that we're going to cross the
480 * boundary no matter what, stop now.
482 if (SCNO + 1 + MAX_CHARACTER_COLUMNS < cnt)
483 goto slow;
486 * Count up the widths of the characters. If it's a tab
487 * character, go do it the the slow way.
489 for (cwtotal = 0; cnt--; cwtotal += KEY_COL(sp, ch))
490 if ((ch = *(UCHAR_T *)p--) == '\t')
491 goto slow;
494 * Decrement the screen cursor by the total width of the
495 * characters minus 1.
497 cwtotal -= 1;
500 * If we're moving left, and there's a wide character in the
501 * current position, go to the end of the character.
503 if (KEY_COL(sp, ch) > 1)
504 cwtotal -= KEY_COL(sp, ch) - 1;
507 * If the new column moved us off of the current logical line,
508 * calculate a new one. If doing leftright scrolling, we've
509 * moved off of the current screen, as well.
511 if (SCNO < cwtotal)
512 goto slow;
513 SCNO -= cwtotal;
514 } else {
516 * 7b: Cursor moved right.
518 * Point to the first character to the right.
520 p += OCNO + 1;
521 cnt = CNO - OCNO;
524 * Count up the widths of the characters. If it's a tab
525 * character, go do it the the slow way. If we cross a
526 * screen boundary, we can quit.
528 for (cwtotal = SCNO; cnt--;) {
529 if ((ch = *(UCHAR_T *)p++) == '\t')
530 goto slow;
531 if ((cwtotal += KEY_COL(sp, ch)) >= SCREEN_COLS(sp))
532 break;
536 * Increment the screen cursor by the total width of the
537 * characters.
539 SCNO = cwtotal;
541 /* See screen change comment in section 6a. */
542 if (SCNO >= SCREEN_COLS(sp))
543 goto slow;
547 * 7c: Fast cursor update.
549 * We have the current column, retrieve the current row.
551 fast: (void)gp->scr_cursor(sp, &y, &notused);
552 goto done_cursor;
555 * 7d: Slow cursor update.
557 * Walk through the map and find the current line.
559 slow: for (smp = HMAP; smp->lno != LNO; ++smp);
562 * 7e: Leftright scrolling adjustment.
564 * If doing left-right scrolling and the cursor movement has changed
565 * the displayed screen, scroll the screen left or right, unless we're
566 * updating the info line in which case we just scroll that one line.
567 * We adjust the offset up or down until we have a window that covers
568 * the current column, making sure that we adjust differently for the
569 * first screen as compared to subsequent ones.
571 if (O_ISSET(sp, O_LEFTRIGHT)) {
573 * Get the screen column for this character, and correct
574 * for the number option offset.
576 cnt = vs_columns(sp, NULL, LNO, &CNO, NULL);
577 if (O_ISSET(sp, O_NUMBER))
578 cnt -= O_NUMBER_LENGTH;
580 /* Adjust the window towards the beginning of the line. */
581 off = smp->coff;
582 if (off >= cnt) {
583 do {
584 if (off >= O_VAL(sp, O_SIDESCROLL))
585 off -= O_VAL(sp, O_SIDESCROLL);
586 else {
587 off = 0;
588 break;
590 } while (off >= cnt);
591 goto shifted;
594 /* Adjust the window towards the end of the line. */
595 if ((off == 0 && off + SCREEN_COLS(sp) < cnt) ||
596 (off != 0 && off + sp->cols < cnt)) {
597 do {
598 off += O_VAL(sp, O_SIDESCROLL);
599 } while (off + sp->cols < cnt);
601 shifted: /* Fill in screen map with the new offset. */
602 if (F_ISSET(sp, SC_TINPUT_INFO))
603 smp->coff = off;
604 else {
605 for (smp = HMAP; smp <= TMAP; ++smp)
606 smp->coff = off;
607 leftright_warp = 1;
609 goto paint;
613 * We may have jumped here to adjust a leftright screen because
614 * redraw was set. If so, we have to paint the entire screen.
616 if (F_ISSET(sp, SC_SCR_REDRAW))
617 goto paint;
621 * Update the screen lines for this particular file line until we
622 * have a new screen cursor position.
624 for (y = -1,
625 vip->sc_smap = NULL; smp <= TMAP && smp->lno == LNO; ++smp) {
626 if (vs_line(sp, smp, &y, &SCNO))
627 return (1);
628 if (y != (size_t)-1) {
629 vip->sc_smap = smp;
630 break;
633 goto done_cursor;
636 * 8: Repaint the entire screen.
638 * Lost big, do what you have to do. We flush the cache, since
639 * SC_SCR_REDRAW gets set when the screen isn't worth fixing, and
640 * it's simpler to repaint. So, don't trust anything that we
641 * think we know about it.
643 paint: for (smp = HMAP; smp <= TMAP; ++smp)
644 SMAP_FLUSH(smp);
645 for (y = -1, vip->sc_smap = NULL, smp = HMAP; smp <= TMAP; ++smp) {
646 if (vs_line(sp, smp, &y, &SCNO))
647 return (1);
648 if (y != (size_t)-1 && vip->sc_smap == NULL)
649 vip->sc_smap = smp;
652 * If it's a small screen and we're redrawing, clear the unused lines,
653 * ex may have overwritten them.
655 if (F_ISSET(sp, SC_SCR_REDRAW) && IS_SMALL(sp))
656 for (cnt = sp->t_rows; cnt <= sp->t_maxrows; ++cnt) {
657 (void)gp->scr_move(sp, cnt, 0);
658 (void)gp->scr_clrtoeol(sp);
661 didpaint = 1;
663 done_cursor:
665 * Sanity checking. When the repainting code messes up, the usual
666 * result is we don't repaint the cursor and so sc_smap will be
667 * NULL. If we're debugging, die, otherwise restart from scratch.
669 #ifdef DEBUG
670 if (vip->sc_smap == NULL) {
671 fprintf(stderr, "smap error\n");
672 sleep(100);
673 abort();
675 #else
676 if (vip->sc_smap == NULL) {
677 F_SET(sp, SC_SCR_REFORMAT);
678 return (vs_paint(sp, flags));
680 #endif
683 * 9: Set the remembered cursor values.
685 OCNO = CNO;
686 OLNO = LNO;
689 * 10: Repaint the line numbers.
691 * If O_NUMBER is set and the VIP_N_RENUMBER bit is set, and we
692 * didn't repaint the screen, repaint all of the line numbers,
693 * they've changed.
695 number: if (O_ISSET(sp, O_NUMBER) &&
696 F_ISSET(vip, VIP_N_RENUMBER) && !didpaint && vs_number(sp))
697 return (1);
700 * 11: Update the mode line, position the cursor, and flush changes.
702 * If we warped the screen, we have to refresh everything.
704 if (leftright_warp)
705 LF_SET(UPDATE_CURSOR | UPDATE_SCREEN);
707 if (LF_ISSET(UPDATE_SCREEN) && !IS_ONELINE(sp) &&
708 !F_ISSET(vip, VIP_S_MODELINE) && !F_ISSET(sp, SC_TINPUT_INFO))
709 vs_modeline(sp);
711 if (LF_ISSET(UPDATE_CURSOR)) {
712 (void)gp->scr_move(sp, y, SCNO);
715 * XXX
716 * If the screen shifted, we recalculate the "most favorite"
717 * cursor position. Vi won't know that we've warped the
718 * screen, so it's going to have a wrong idea about where the
719 * cursor should be. This is vi's problem, and fixing it here
720 * is a gross layering violation.
722 if (leftright_warp)
723 (void)vs_column(sp, &sp->rcm);
726 if (LF_ISSET(UPDATE_SCREEN))
727 (void)gp->scr_refresh(sp, F_ISSET(vip, VIP_N_EX_PAINT));
729 /* 12: Clear the flags that are handled by this routine. */
730 F_CLR(sp, SC_SCR_CENTER | SC_SCR_REDRAW | SC_SCR_REFORMAT | SC_SCR_TOP);
731 F_CLR(vip, VIP_CUR_INVALID |
732 VIP_N_EX_PAINT | VIP_N_REFRESH | VIP_N_RENUMBER | VIP_S_MODELINE);
734 return (0);
736 #undef LNO
737 #undef OLNO
738 #undef CNO
739 #undef OCNO
740 #undef SCNO
744 * vs_modeline --
745 * Update the mode line.
747 static void
748 vs_modeline(SCR *sp)
750 static const char * const modes[] = {
751 "215|Append", /* SM_APPEND */
752 "216|Change", /* SM_CHANGE */
753 "217|Command", /* SM_COMMAND */
754 "218|Insert", /* SM_INSERT */
755 "219|Replace", /* SM_REPLACE */
757 GS *gp;
758 size_t cols, curcol, curlen, endpoint, len, midpoint;
759 const char *t = NULL;
760 int ellipsis;
761 char *p, buf[20];
763 gp = sp->gp;
766 * We put down the file name, the ruler, the mode and the dirty flag.
767 * If there's not enough room, there's not enough room, we don't play
768 * any special games. We try to put the ruler in the middle and the
769 * mode and dirty flag at the end.
771 * !!!
772 * Leave the last character blank, in case it's a really dumb terminal
773 * with hardware scroll. Second, don't paint the last character in the
774 * screen, SunOS 4.1.1 and Ultrix 4.2 curses won't let you.
776 * Move to the last line on the screen.
778 (void)gp->scr_move(sp, LASTLINE(sp), 0);
780 /* If more than one screen in the display, show the file name. */
781 curlen = 0;
782 if (IS_SPLIT(sp)) {
783 for (p = sp->frp->name; *p != '\0'; ++p);
784 for (ellipsis = 0, cols = sp->cols / 2; --p > sp->frp->name;) {
785 if (*p == '/') {
786 ++p;
787 break;
789 if ((curlen += KEY_LEN(sp, *p)) > cols) {
790 ellipsis = 3;
791 curlen +=
792 KEY_LEN(sp, '.') * 3 + KEY_LEN(sp, ' ');
793 while (curlen > cols) {
794 ++p;
795 curlen -= KEY_LEN(sp, *p);
797 break;
800 if (ellipsis) {
801 while (ellipsis--)
802 (void)gp->scr_addstr(sp,
803 (const char *)KEY_NAME(sp, '.'),
804 KEY_LEN(sp, '.'));
805 (void)gp->scr_addstr(sp,
806 (const char *)KEY_NAME(sp, ' '), KEY_LEN(sp, ' '));
808 for (; *p != '\0'; ++p)
809 (void)gp->scr_addstr(sp,
810 (const char *)KEY_NAME(sp, *p), KEY_LEN(sp, *p));
813 /* Clear the rest of the line. */
814 (void)gp->scr_clrtoeol(sp);
817 * Display the ruler. If we're not at the midpoint yet, move there.
818 * Otherwise, add in two extra spaces.
820 * Adjust the current column for the fact that the editor uses it as
821 * a zero-based number.
823 * XXX
824 * Assume that numbers, commas, and spaces only take up a single
825 * column on the screen.
827 cols = sp->cols - 1;
828 if (O_ISSET(sp, O_RULER)) {
829 vs_column(sp, &curcol);
830 len =
831 snprintf(buf, sizeof(buf), "%lu,%lu",
832 (unsigned long)sp->lno, (unsigned long)curcol + 1);
834 midpoint = (cols - ((len + 1) / 2)) / 2;
835 if (curlen < midpoint) {
836 (void)gp->scr_move(sp, LASTLINE(sp), midpoint);
837 curlen += len;
838 } else if (curlen + 2 + len < cols) {
839 (void)gp->scr_addstr(sp, " ", 2);
840 curlen += 2 + len;
842 (void)gp->scr_addstr(sp, buf, len);
846 * Display the mode and the modified flag, as close to the end of the
847 * line as possible, but guaranteeing at least two spaces between the
848 * ruler and the modified flag.
850 #define MODESIZE 9
851 endpoint = cols;
852 if (O_ISSET(sp, O_SHOWMODE)) {
853 if (F_ISSET(sp->ep, F_MODIFIED))
854 --endpoint;
855 t = msg_cat(sp, modes[sp->showmode], &len);
856 endpoint -= len;
859 if (endpoint > curlen + 2) {
860 (void)gp->scr_move(sp, LASTLINE(sp), endpoint);
861 if (O_ISSET(sp, O_SHOWMODE)) {
862 if (F_ISSET(sp->ep, F_MODIFIED))
863 (void)gp->scr_addstr(sp,
864 (const char *)KEY_NAME(sp, '*'),
865 KEY_LEN(sp, '*'));
866 (void)gp->scr_addstr(sp, t, len);