tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / vi / vs_refresh.c
blob4dedd94b81a8d880b5ea44a7a934c51baa0353af
1 /* $NetBSD: vs_refresh.c,v 1.5 2013/12/01 02:34:54 christos Exp $ */
2 /*-
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "../common/common.h"
29 #include "vi.h"
31 #define UPDATE_CURSOR 0x01 /* Update the cursor. */
32 #define UPDATE_SCREEN 0x02 /* Flush to screen. */
34 static void vs_modeline __P((SCR *));
35 static int vs_paint __P((SCR *, u_int));
38 * vs_refresh --
39 * Refresh all screens.
41 * PUBLIC: int vs_refresh __P((SCR *, int));
43 int
44 vs_refresh(SCR *sp, int forcepaint)
46 GS *gp;
47 SCR *tsp;
48 int need_refresh;
49 u_int priv_paint, pub_paint;
51 gp = sp->gp;
54 * 1: Refresh the screen.
56 * If SC_SCR_REDRAW is set in the current screen, repaint everything
57 * that we can find, including status lines.
59 if (F_ISSET(sp, SC_SCR_REDRAW))
60 TAILQ_FOREACH(tsp, &sp->wp->scrq, q)
61 if (tsp != sp)
62 F_SET(tsp, SC_SCR_REDRAW | SC_STATUS);
65 * 2: Related or dirtied screens, or screens with messages.
67 * If related screens share a view into a file, they may have been
68 * modified as well. Refresh any screens that aren't exiting that
69 * have paint or dirty bits set. Always update their screens, we
70 * are not likely to get another chance. Finally, if we refresh any
71 * screens other than the current one, the cursor will be trashed.
73 pub_paint = SC_SCR_REFORMAT | SC_SCR_REDRAW;
74 priv_paint = VIP_CUR_INVALID | VIP_N_REFRESH;
75 if (O_ISSET(sp, O_NUMBER))
76 priv_paint |= VIP_N_RENUMBER;
77 TAILQ_FOREACH(tsp, &sp->wp->scrq, q)
78 if (tsp != sp && !F_ISSET(tsp, SC_EXIT | SC_EXIT_FORCE) &&
79 (F_ISSET(tsp, pub_paint) ||
80 F_ISSET(VIP(tsp), priv_paint))) {
81 (void)vs_paint(tsp,
82 (F_ISSET(VIP(tsp), VIP_CUR_INVALID) ?
83 UPDATE_CURSOR : 0) | UPDATE_SCREEN);
84 F_SET(VIP(sp), VIP_CUR_INVALID);
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, SC_SCR_REDRAW can be set
92 * in the current screen only, and the screen won't flash.
94 if (vs_paint(sp, UPDATE_CURSOR | (!forcepaint &&
95 F_ISSET(sp, SC_SCR_VI) && KEYS_WAITING(sp) ? 0 : UPDATE_SCREEN)))
96 return (1);
99 * 4: Paint any missing status lines.
101 * XXX
102 * This is fairly evil. Status lines are written using the vi message
103 * mechanism, since we have no idea how long they are. Since we may be
104 * painting screens other than the current one, we don't want to make
105 * the user wait. We depend heavily on there not being any other lines
106 * currently waiting to be displayed and the message truncation code in
107 * the msgq_status routine working.
109 * And, finally, if we updated any status lines, make sure the cursor
110 * gets back to where it belongs.
112 need_refresh = 0;
113 TAILQ_FOREACH(tsp, &sp->wp->scrq, q)
114 if (F_ISSET(tsp, SC_STATUS)) {
115 need_refresh = 1;
116 vs_resolve(tsp, sp, 0);
118 if (need_refresh)
119 (void)gp->scr_refresh(sp, 0);
122 * A side-effect of refreshing the screen is that it's now ready
123 * for everything else, i.e. messages.
125 F_SET(sp, SC_SCR_VI);
126 return (0);
130 * vs_paint --
131 * This is the guts of the vi curses screen code. The idea is that
132 * the SCR structure passed in contains the new coordinates of the
133 * screen. What makes this hard is that we don't know how big
134 * characters are, doing input can put the cursor in illegal places,
135 * and we're frantically trying to avoid repainting unless it's
136 * absolutely necessary. If you change this code, you'd better know
137 * what you're doing. It's subtle and quick to anger.
139 static int
140 vs_paint(SCR *sp, u_int flags)
142 GS *gp;
143 SMAP *smp, tmp;
144 VI_PRIVATE *vip;
145 db_recno_t lastline, lcnt;
146 size_t cwtotal, cnt, len, notused, off, y;
147 int ch = 0, didpaint, isempty, leftright_warp;
148 CHAR_T *p;
150 #define LNO sp->lno /* Current file line. */
151 #define OLNO vip->olno /* Remembered file line. */
152 #define CNO sp->cno /* Current file column. */
153 #define OCNO vip->ocno /* Remembered file column. */
154 #define SCNO vip->sc_col /* Current screen column. */
156 gp = sp->gp;
157 vip = VIP(sp);
158 if (vip == NULL)
159 return 0;
160 didpaint = leftright_warp = 0;
163 * 5: Reformat the lines.
165 * If the lines themselves have changed (:set list, for example),
166 * fill in the map from scratch. Adjust the screen that's being
167 * displayed if the leftright flag is set.
169 if (F_ISSET(sp, SC_SCR_REFORMAT)) {
170 /* Invalidate the line size cache. */
171 VI_SCR_CFLUSH(vip);
173 /* Toss vs_line() cached information. */
174 if (F_ISSET(sp, SC_SCR_TOP)) {
175 if (vs_sm_fill(sp, LNO, P_TOP))
176 return (1);
178 else if (F_ISSET(sp, SC_SCR_CENTER)) {
179 if (vs_sm_fill(sp, LNO, P_MIDDLE))
180 return (1);
181 } else
182 if (vs_sm_fill(sp, OOBLNO, P_TOP))
183 return (1);
184 F_SET(sp, SC_SCR_REDRAW);
188 * 6: Line movement.
190 * Line changes can cause the top line to change as well. As
191 * before, if the movement is large, the screen is repainted.
193 * 6a: Small screens.
195 * Users can use the window, w300, w1200 and w9600 options to make
196 * the screen artificially small. The behavior of these options
197 * in the historic vi wasn't all that consistent, and, in fact, it
198 * was never documented how various screen movements affected the
199 * screen size. Generally, one of three things would happen:
200 * 1: The screen would expand in size, showing the line
201 * 2: The screen would scroll, showing the line
202 * 3: The screen would compress to its smallest size and
203 * repaint.
204 * In general, scrolling didn't cause compression (200^D was handled
205 * the same as ^D), movement to a specific line would (:N where N
206 * was 1 line below the screen caused a screen compress), and cursor
207 * movement would scroll if it was 11 lines or less, and compress if
208 * it was more than 11 lines. (And, no, I have no idea where the 11
209 * comes from.)
211 * What we do is try and figure out if the line is less than half of
212 * a full screen away. If it is, we expand the screen if there's
213 * room, and then scroll as necessary. The alternative is to compress
214 * and repaint.
216 * !!!
217 * This code is a special case from beginning to end. Unfortunately,
218 * home modems are still slow enough that it's worth having.
220 * XXX
221 * If the line a really long one, i.e. part of the line is on the
222 * screen but the column offset is not, we'll end up in the adjust
223 * code, when we should probably have compressed the screen.
225 if (IS_SMALL(sp)) {
226 if (LNO < HMAP->lno) {
227 lcnt = vs_sm_nlines(sp, HMAP, LNO, sp->t_maxrows);
228 if (lcnt <= HALFSCREEN(sp))
229 for (; lcnt && sp->t_rows != sp->t_maxrows;
230 --lcnt, ++sp->t_rows) {
231 ++TMAP;
232 if (vs_sm_1down(sp))
233 return (1);
235 else
236 goto small_fill;
237 } else if (LNO > TMAP->lno) {
238 lcnt = vs_sm_nlines(sp, TMAP, LNO, sp->t_maxrows);
239 if (lcnt <= HALFSCREEN(sp))
240 for (; lcnt && sp->t_rows != sp->t_maxrows;
241 --lcnt, ++sp->t_rows) {
242 if (vs_sm_next(sp, TMAP, TMAP + 1))
243 return (1);
244 ++TMAP;
245 if (vs_line(sp, TMAP, NULL, NULL))
246 return (1);
248 else {
249 small_fill: (void)gp->scr_move(sp, LASTLINE(sp), 0);
250 (void)gp->scr_clrtoeol(sp);
251 for (; sp->t_rows > sp->t_minrows;
252 --sp->t_rows, --TMAP) {
253 (void)gp->scr_move(sp, TMAP - HMAP, 0);
254 (void)gp->scr_clrtoeol(sp);
256 if (vs_sm_fill(sp, LNO, P_FILL))
257 return (1);
258 F_SET(sp, SC_SCR_REDRAW);
259 goto adjust;
265 * 6b: Line down, or current screen.
267 if (LNO >= HMAP->lno) {
268 /* Current screen. */
269 if (LNO <= TMAP->lno)
270 goto adjust;
271 if (F_ISSET(sp, SC_SCR_TOP))
272 goto top;
273 if (F_ISSET(sp, SC_SCR_CENTER))
274 goto middle;
277 * If less than half a screen above the line, scroll down
278 * until the line is on the screen.
280 lcnt = vs_sm_nlines(sp, TMAP, LNO, HALFTEXT(sp));
281 if (lcnt < HALFTEXT(sp)) {
282 while (lcnt--)
283 if (vs_sm_1up(sp))
284 return (1);
285 goto adjust;
287 goto bottom;
291 * 6c: If not on the current screen, may request center or top.
293 if (F_ISSET(sp, SC_SCR_TOP))
294 goto top;
295 if (F_ISSET(sp, SC_SCR_CENTER))
296 goto middle;
299 * 6d: Line up.
301 lcnt = vs_sm_nlines(sp, HMAP, LNO, HALFTEXT(sp));
302 if (lcnt < HALFTEXT(sp)) {
304 * If less than half a screen below the line, scroll up until
305 * the line is the first line on the screen. Special check so
306 * that if the screen has been emptied, we refill it.
308 if (db_exist(sp, HMAP->lno)) {
309 while (lcnt--)
310 if (vs_sm_1down(sp))
311 return (1);
312 goto adjust;
316 * If less than a half screen from the bottom of the file,
317 * put the last line of the file on the bottom of the screen.
319 bottom: if (db_last(sp, &lastline))
320 return (1);
321 tmp.lno = LNO;
322 tmp.coff = HMAP->coff;
323 tmp.soff = 1;
324 lcnt = vs_sm_nlines(sp, &tmp, lastline+1, sp->t_rows);
325 if (lcnt < HALFTEXT(sp)) {
326 if (vs_sm_fill(sp, lastline, P_BOTTOM))
327 return (1);
328 F_SET(sp, SC_SCR_REDRAW);
329 goto adjust;
331 /* It's not close, just put the line in the middle. */
332 goto middle;
336 * If less than half a screen from the top of the file, put the first
337 * line of the file at the top of the screen. Otherwise, put the line
338 * in the middle of the screen.
340 tmp.lno = 1;
341 tmp.coff = HMAP->coff;
342 tmp.soff = 1;
343 lcnt = vs_sm_nlines(sp, &tmp, LNO, HALFTEXT(sp));
344 if (lcnt < HALFTEXT(sp)) {
345 if (vs_sm_fill(sp, 1, P_TOP))
346 return (1);
347 } else
348 middle: if (vs_sm_fill(sp, LNO, P_MIDDLE))
349 return (1);
350 if (0) {
351 top: if (vs_sm_fill(sp, LNO, P_TOP))
352 return (1);
354 F_SET(sp, SC_SCR_REDRAW);
357 * At this point we know part of the line is on the screen. Since
358 * scrolling is done using logical lines, not physical, all of the
359 * line may not be on the screen. While that's not necessarily bad,
360 * if the part the cursor is on isn't there, we're going to lose.
361 * This can be tricky; if the line covers the entire screen, lno
362 * may be the same as both ends of the map, that's why we test BOTH
363 * the top and the bottom of the map. This isn't a problem for
364 * left-right scrolling, the cursor movement code handles the problem.
366 * There's a performance issue here if editing *really* long lines.
367 * This gets to the right spot by scrolling, and, in a binary, by
368 * scrolling hundreds of lines. If the adjustment looks like it's
369 * going to be a serious problem, refill the screen and repaint.
371 adjust: if (!O_ISSET(sp, O_LEFTRIGHT) &&
372 (LNO == HMAP->lno || LNO == TMAP->lno)) {
373 cnt = vs_screens(sp, LNO, &CNO);
374 if (LNO == HMAP->lno && cnt < HMAP->soff) {
375 if ((HMAP->soff - cnt) > HALFTEXT(sp)) {
376 HMAP->soff = cnt;
377 vs_sm_fill(sp, OOBLNO, P_TOP);
378 F_SET(sp, SC_SCR_REDRAW);
379 } else
380 while (cnt < HMAP->soff)
381 if (vs_sm_1down(sp))
382 return (1);
384 if (LNO == TMAP->lno && cnt > TMAP->soff) {
385 if ((cnt - TMAP->soff) > HALFTEXT(sp)) {
386 TMAP->soff = cnt;
387 vs_sm_fill(sp, OOBLNO, P_BOTTOM);
388 F_SET(sp, SC_SCR_REDRAW);
389 } else
390 while (cnt > TMAP->soff)
391 if (vs_sm_1up(sp))
392 return (1);
397 * If the screen needs to be repainted, skip cursor optimization.
398 * However, in the code above we skipped leftright scrolling on
399 * the grounds that the cursor code would handle it. Make sure
400 * the right screen is up.
402 if (F_ISSET(sp, SC_SCR_REDRAW)) {
403 if (O_ISSET(sp, O_LEFTRIGHT))
404 goto slow;
405 goto paint;
409 * 7: Cursor movements (current screen only).
411 if (!LF_ISSET(UPDATE_CURSOR))
412 goto number;
415 * Decide cursor position. If the line has changed, the cursor has
416 * moved over a tab, or don't know where the cursor was, reparse the
417 * line. Otherwise, we've just moved over fixed-width characters,
418 * and can calculate the left/right scrolling and cursor movement
419 * without reparsing the line. Note that we don't know which (if any)
420 * of the characters between the old and new cursor positions changed.
422 * XXX
423 * With some work, it should be possible to handle tabs quickly, at
424 * least in obvious situations, like moving right and encountering
425 * a tab, without reparsing the whole line.
427 * If the line we're working with has changed, reread it..
429 if (F_ISSET(vip, VIP_CUR_INVALID) || LNO != OLNO)
430 goto slow;
432 /* Otherwise, if nothing's changed, ignore the cursor. */
433 if (CNO == OCNO)
434 goto fast;
437 * Get the current line. If this fails, we either have an empty
438 * file and can just repaint, or there's a real problem. This
439 * isn't a performance issue because there aren't any ways to get
440 * here repeatedly.
442 if (db_eget(sp, LNO, &p, &len, &isempty)) {
443 if (isempty)
444 goto slow;
445 return (1);
448 #ifdef DEBUG
449 /* Sanity checking. */
450 if (CNO >= len && len != 0) {
451 msgq(sp, M_ERR, "Error: %s/%d: cno (%u) >= len (%u)",
452 tail(__FILE__), __LINE__, CNO, len);
453 return (1);
455 #endif
457 * The basic scheme here is to look at the characters in between
458 * the old and new positions and decide how big they are on the
459 * screen, and therefore, how many screen positions to move.
461 if (CNO < OCNO) {
463 * 7a: Cursor moved left.
465 * Point to the old character. The old cursor position can
466 * be past EOL if, for example, we just deleted the rest of
467 * the line. In this case, since we don't know the width of
468 * the characters we traversed, we have to do it slowly.
470 p += OCNO;
471 cnt = (OCNO - CNO) + 1;
472 if (OCNO >= len)
473 goto slow;
476 * Quick sanity check -- it's hard to figure out exactly when
477 * we cross a screen boundary as we do in the cursor right
478 * movement. If cnt is so large that we're going to cross the
479 * boundary no matter what, stop now.
481 if (SCNO + 1 + MAX_CHARACTER_COLUMNS < cnt)
482 goto slow;
485 * Count up the widths of the characters. If it's a tab
486 * character, go do it the the slow way.
488 for (cwtotal = 0; cnt--; cwtotal += KEY_COL(sp, ch))
489 if ((ch = *(UCHAR_T *)p--) == '\t')
490 goto slow;
493 * Decrement the screen cursor by the total width of the
494 * characters minus 1.
496 cwtotal -= 1;
499 * If we're moving left, and there's a wide character in the
500 * current position, go to the end of the character.
502 if (KEY_COL(sp, ch) > 1)
503 cwtotal -= KEY_COL(sp, ch) - 1;
506 * If the new column moved us off of the current logical line,
507 * calculate a new one. If doing leftright scrolling, we've
508 * moved off of the current screen, as well.
510 if (SCNO < cwtotal)
511 goto slow;
512 SCNO -= cwtotal;
513 } else {
515 * 7b: Cursor moved right.
517 * Point to the first character to the right.
519 p += OCNO + 1;
520 cnt = CNO - OCNO;
523 * Count up the widths of the characters. If it's a tab
524 * character, go do it the the slow way. If we cross a
525 * screen boundary, we can quit.
527 for (cwtotal = SCNO; cnt--;) {
528 if ((ch = *(UCHAR_T *)p++) == '\t')
529 goto slow;
530 if ((cwtotal += KEY_COL(sp, ch)) >= SCREEN_COLS(sp))
531 break;
535 * Increment the screen cursor by the total width of the
536 * characters.
538 SCNO = cwtotal;
540 /* See screen change comment in section 6a. */
541 if (SCNO >= SCREEN_COLS(sp))
542 goto slow;
546 * 7c: Fast cursor update.
548 * We have the current column, retrieve the current row.
550 fast: (void)gp->scr_cursor(sp, &y, &notused);
551 goto done_cursor;
554 * 7d: Slow cursor update.
556 * Walk through the map and find the current line.
558 slow: for (smp = HMAP; smp->lno != LNO; ++smp);
561 * 7e: Leftright scrolling adjustment.
563 * If doing left-right scrolling and the cursor movement has changed
564 * the displayed screen, scroll the screen left or right, unless we're
565 * updating the info line in which case we just scroll that one line.
566 * We adjust the offset up or down until we have a window that covers
567 * the current column, making sure that we adjust differently for the
568 * first screen as compared to subsequent ones.
570 if (O_ISSET(sp, O_LEFTRIGHT)) {
572 * Get the screen column for this character, and correct
573 * for the number option offset.
575 cnt = vs_columns(sp, NULL, LNO, &CNO, NULL);
576 if (O_ISSET(sp, O_NUMBER))
577 cnt -= O_NUMBER_LENGTH;
579 /* Adjust the window towards the beginning of the line. */
580 off = smp->coff;
581 if (off >= cnt) {
582 do {
583 if (off >= O_VAL(sp, O_SIDESCROLL))
584 off -= O_VAL(sp, O_SIDESCROLL);
585 else {
586 off = 0;
587 break;
589 } while (off >= cnt);
590 goto shifted;
593 /* Adjust the window towards the end of the line. */
594 if ((off == 0 && off + SCREEN_COLS(sp) < cnt) ||
595 (off != 0 && off + sp->cols < cnt)) {
596 do {
597 off += O_VAL(sp, O_SIDESCROLL);
598 } while (off + sp->cols < cnt);
600 shifted: /* Fill in screen map with the new offset. */
601 if (F_ISSET(sp, SC_TINPUT_INFO))
602 smp->coff = off;
603 else {
604 for (smp = HMAP; smp <= TMAP; ++smp)
605 smp->coff = off;
606 leftright_warp = 1;
608 goto paint;
612 * We may have jumped here to adjust a leftright screen because
613 * redraw was set. If so, we have to paint the entire screen.
615 if (F_ISSET(sp, SC_SCR_REDRAW))
616 goto paint;
620 * Update the screen lines for this particular file line until we
621 * have a new screen cursor position.
623 for (y = -1,
624 vip->sc_smap = NULL; smp <= TMAP && smp->lno == LNO; ++smp) {
625 if (vs_line(sp, smp, &y, &SCNO))
626 return (1);
627 if (y != (size_t)-1) {
628 vip->sc_smap = smp;
629 break;
632 goto done_cursor;
635 * 8: Repaint the entire screen.
637 * Lost big, do what you have to do. We flush the cache, since
638 * SC_SCR_REDRAW gets set when the screen isn't worth fixing, and
639 * it's simpler to repaint. So, don't trust anything that we
640 * think we know about it.
642 paint: for (smp = HMAP; smp <= TMAP; ++smp)
643 SMAP_FLUSH(smp);
644 for (y = -1, vip->sc_smap = NULL, smp = HMAP; smp <= TMAP; ++smp) {
645 if (vs_line(sp, smp, &y, &SCNO))
646 return (1);
647 if (y != (size_t)-1 && vip->sc_smap == NULL)
648 vip->sc_smap = smp;
651 * If it's a small screen and we're redrawing, clear the unused lines,
652 * ex may have overwritten them.
654 if (F_ISSET(sp, SC_SCR_REDRAW) && IS_SMALL(sp))
655 for (cnt = sp->t_rows; cnt <= sp->t_maxrows; ++cnt) {
656 (void)gp->scr_move(sp, cnt, 0);
657 (void)gp->scr_clrtoeol(sp);
660 didpaint = 1;
662 done_cursor:
664 * Sanity checking. When the repainting code messes up, the usual
665 * result is we don't repaint the cursor and so sc_smap will be
666 * NULL. If we're debugging, die, otherwise restart from scratch.
668 #ifdef DEBUG
669 if (vip->sc_smap == NULL) {
670 fprintf(stderr, "smap error\n");
671 sleep(100);
672 abort();
674 #else
675 if (vip->sc_smap == NULL) {
676 F_SET(sp, SC_SCR_REFORMAT);
677 return (vs_paint(sp, flags));
679 #endif
682 * 9: Set the remembered cursor values.
684 OCNO = CNO;
685 OLNO = LNO;
688 * 10: Repaint the line numbers.
690 * If O_NUMBER is set and the VIP_N_RENUMBER bit is set, and we
691 * didn't repaint the screen, repaint all of the line numbers,
692 * they've changed.
694 number: if (O_ISSET(sp, O_NUMBER) &&
695 F_ISSET(vip, VIP_N_RENUMBER) && !didpaint && vs_number(sp))
696 return (1);
699 * 11: Update the mode line, position the cursor, and flush changes.
701 * If we warped the screen, we have to refresh everything.
703 if (leftright_warp)
704 LF_SET(UPDATE_CURSOR | UPDATE_SCREEN);
706 if (LF_ISSET(UPDATE_SCREEN) && !IS_ONELINE(sp) &&
707 !F_ISSET(vip, VIP_S_MODELINE) && !F_ISSET(sp, SC_TINPUT_INFO))
708 vs_modeline(sp);
710 if (LF_ISSET(UPDATE_CURSOR)) {
711 (void)gp->scr_move(sp, y, SCNO);
714 * XXX
715 * If the screen shifted, we recalculate the "most favorite"
716 * cursor position. Vi won't know that we've warped the
717 * screen, so it's going to have a wrong idea about where the
718 * cursor should be. This is vi's problem, and fixing it here
719 * is a gross layering violation.
721 if (leftright_warp)
722 (void)vs_column(sp, &sp->rcm);
725 if (LF_ISSET(UPDATE_SCREEN))
726 (void)gp->scr_refresh(sp, F_ISSET(vip, VIP_N_EX_PAINT));
728 /* 12: Clear the flags that are handled by this routine. */
729 F_CLR(sp, SC_SCR_CENTER | SC_SCR_REDRAW | SC_SCR_REFORMAT | SC_SCR_TOP);
730 F_CLR(vip, VIP_CUR_INVALID |
731 VIP_N_EX_PAINT | VIP_N_REFRESH | VIP_N_RENUMBER | VIP_S_MODELINE);
733 return (0);
735 #undef LNO
736 #undef OLNO
737 #undef CNO
738 #undef OCNO
739 #undef SCNO
743 * vs_modeline --
744 * Update the mode line.
746 static void
747 vs_modeline(SCR *sp)
749 static const char * const modes[] = {
750 "215|Append", /* SM_APPEND */
751 "216|Change", /* SM_CHANGE */
752 "217|Command", /* SM_COMMAND */
753 "218|Insert", /* SM_INSERT */
754 "219|Replace", /* SM_REPLACE */
756 GS *gp;
757 size_t cols, curcol, curlen, endpoint, len, midpoint;
758 const char *t = NULL;
759 int ellipsis;
760 char *p, buf[20];
762 gp = sp->gp;
765 * We put down the file name, the ruler, the mode and the dirty flag.
766 * If there's not enough room, there's not enough room, we don't play
767 * any special games. We try to put the ruler in the middle and the
768 * mode and dirty flag at the end.
770 * !!!
771 * Leave the last character blank, in case it's a really dumb terminal
772 * with hardware scroll. Second, don't paint the last character in the
773 * screen, SunOS 4.1.1 and Ultrix 4.2 curses won't let you.
775 * Move to the last line on the screen.
777 (void)gp->scr_move(sp, LASTLINE(sp), 0);
779 /* If more than one screen in the display, show the file name. */
780 curlen = 0;
781 if (IS_SPLIT(sp)) {
782 for (p = sp->frp->name; *p != '\0'; ++p);
783 for (ellipsis = 0, cols = sp->cols / 2; --p > sp->frp->name;) {
784 if (*p == '/') {
785 ++p;
786 break;
788 if ((curlen += KEY_LEN(sp, *p)) > cols) {
789 ellipsis = 3;
790 curlen +=
791 KEY_LEN(sp, '.') * 3 + KEY_LEN(sp, ' ');
792 while (curlen > cols) {
793 ++p;
794 curlen -= KEY_LEN(sp, *p);
796 break;
799 if (ellipsis) {
800 while (ellipsis--)
801 (void)gp->scr_addstr(sp,
802 (const char *)KEY_NAME(sp, '.'),
803 KEY_LEN(sp, '.'));
804 (void)gp->scr_addstr(sp,
805 (const char *)KEY_NAME(sp, ' '), KEY_LEN(sp, ' '));
807 for (; *p != '\0'; ++p)
808 (void)gp->scr_addstr(sp,
809 (const char *)KEY_NAME(sp, *p), KEY_LEN(sp, *p));
812 /* Clear the rest of the line. */
813 (void)gp->scr_clrtoeol(sp);
816 * Display the ruler. If we're not at the midpoint yet, move there.
817 * Otherwise, add in two extra spaces.
819 * Adjust the current column for the fact that the editor uses it as
820 * a zero-based number.
822 * XXX
823 * Assume that numbers, commas, and spaces only take up a single
824 * column on the screen.
826 cols = sp->cols - 1;
827 if (O_ISSET(sp, O_RULER)) {
828 vs_column(sp, &curcol);
829 len =
830 snprintf(buf, sizeof(buf), "%lu,%lu",
831 (unsigned long)sp->lno, (unsigned long)curcol + 1);
833 midpoint = (cols - ((len + 1) / 2)) / 2;
834 if (curlen < midpoint) {
835 (void)gp->scr_move(sp, LASTLINE(sp), midpoint);
836 curlen += len;
837 } else if (curlen + 2 + len < cols) {
838 (void)gp->scr_addstr(sp, " ", 2);
839 curlen += 2 + len;
841 (void)gp->scr_addstr(sp, buf, len);
845 * Display the mode and the modified flag, as close to the end of the
846 * line as possible, but guaranteeing at least two spaces between the
847 * ruler and the modified flag.
849 #define MODESIZE 9
850 endpoint = cols;
851 if (O_ISSET(sp, O_SHOWMODE)) {
852 if (F_ISSET(sp->ep, F_MODIFIED))
853 --endpoint;
854 t = msg_cat(sp, modes[sp->showmode], &len);
855 endpoint -= len;
858 if (endpoint > curlen + 2) {
859 (void)gp->scr_move(sp, LASTLINE(sp), endpoint);
860 if (O_ISSET(sp, O_SHOWMODE)) {
861 if (F_ISSET(sp->ep, F_MODIFIED))
862 (void)gp->scr_addstr(sp,
863 (const char *)KEY_NAME(sp, '*'),
864 KEY_LEN(sp, '*'));
865 (void)gp->scr_addstr(sp, t, len);