ex/ex_display.c (ex_display): fix argument checking in case of wide characters
[nvi.git] / vi / vs_refresh.c
blob459fe62d0e247419540da6cece0527df43f74d18
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "../common/common.h"
28 #include "vi.h"
30 #define UPDATE_CURSOR 0x01 /* Update the cursor. */
31 #define UPDATE_SCREEN 0x02 /* Flush to screen. */
33 static void vs_modeline __P((SCR *));
34 static int vs_paint __P((SCR *, u_int));
37 * vs_refresh --
38 * Refresh all screens.
40 * PUBLIC: int vs_refresh __P((SCR *, int));
42 int
43 vs_refresh(SCR *sp, int forcepaint)
45 GS *gp;
46 SCR *tsp;
47 int need_refresh;
48 u_int priv_paint, pub_paint;
50 gp = sp->gp;
53 * 1: Refresh the screen.
55 * If SC_SCR_REDRAW is set in the current screen, repaint everything
56 * that we can find, including status lines.
58 if (F_ISSET(sp, SC_SCR_REDRAW))
59 for (tsp = sp->wp->scrq.cqh_first;
60 tsp != (void *)&sp->wp->scrq; tsp = tsp->q.cqe_next)
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 for (tsp = sp->wp->scrq.cqh_first;
78 tsp != (void *)&sp->wp->scrq; tsp = tsp->q.cqe_next)
79 if (tsp != sp && !F_ISSET(tsp, SC_EXIT | SC_EXIT_FORCE) &&
80 (F_ISSET(tsp, pub_paint) ||
81 F_ISSET(VIP(tsp), priv_paint))) {
82 (void)vs_paint(tsp,
83 (F_ISSET(VIP(tsp), VIP_CUR_INVALID) ?
84 UPDATE_CURSOR : 0) | UPDATE_SCREEN);
85 F_SET(VIP(sp), VIP_CUR_INVALID);
89 * 3: Refresh the current screen.
91 * Always refresh the current screen, it may be a cursor movement.
92 * Also, always do it last -- that way, SC_SCR_REDRAW can be set
93 * in the current screen only, and the screen won't flash.
95 if (vs_paint(sp, UPDATE_CURSOR | (!forcepaint &&
96 F_ISSET(sp, SC_SCR_VI) && KEYS_WAITING(sp) ? 0 : UPDATE_SCREEN)))
97 return (1);
100 * 4: Paint any missing status lines.
102 * XXX
103 * This is fairly evil. Status lines are written using the vi message
104 * mechanism, since we have no idea how long they are. Since we may be
105 * painting screens other than the current one, we don't want to make
106 * the user wait. We depend heavily on there not being any other lines
107 * currently waiting to be displayed and the message truncation code in
108 * the msgq_status routine working.
110 * And, finally, if we updated any status lines, make sure the cursor
111 * gets back to where it belongs.
113 for (need_refresh = 0, tsp = sp->wp->scrq.cqh_first;
114 tsp != (void *)&sp->wp->scrq; tsp = tsp->q.cqe_next)
115 if (F_ISSET(tsp, SC_STATUS)) {
116 need_refresh = 1;
117 vs_resolve(tsp, sp, 0);
119 if (need_refresh)
120 (void)gp->scr_refresh(sp, 0);
123 * A side-effect of refreshing the screen is that it's now ready
124 * for everything else, i.e. messages.
126 F_SET(sp, SC_SCR_VI);
127 return (0);
131 * vs_paint --
132 * This is the guts of the vi curses screen code. The idea is that
133 * the SCR structure passed in contains the new coordinates of the
134 * screen. What makes this hard is that we don't know how big
135 * characters are, doing input can put the cursor in illegal places,
136 * and we're frantically trying to avoid repainting unless it's
137 * absolutely necessary. If you change this code, you'd better know
138 * what you're doing. It's subtle and quick to anger.
140 static int
141 vs_paint(SCR *sp, u_int flags)
143 GS *gp;
144 SMAP *smp, tmp;
145 VI_PRIVATE *vip;
146 db_recno_t lastline, lcnt;
147 size_t cwtotal, cnt, len, notused, off, y;
148 int ch, didpaint, isempty, leftright_warp;
149 CHAR_T *p;
151 #define LNO sp->lno /* Current file line. */
152 #define OLNO vip->olno /* Remembered file line. */
153 #define CNO sp->cno /* Current file column. */
154 #define OCNO vip->ocno /* Remembered file column. */
155 #define SCNO vip->sc_col /* Current screen column. */
157 gp = sp->gp;
158 vip = VIP(sp);
159 didpaint = leftright_warp = 0;
162 * 5: Reformat the lines.
164 * If the lines themselves have changed (:set list, for example),
165 * fill in the map from scratch. Adjust the screen that's being
166 * displayed if the leftright flag is set.
168 if (F_ISSET(sp, SC_SCR_REFORMAT)) {
169 /* Invalidate the line size cache. */
170 VI_SCR_CFLUSH(vip);
172 /* Toss vs_line() cached information. */
173 if (F_ISSET(sp, SC_SCR_TOP)) {
174 if (vs_sm_fill(sp, LNO, P_TOP))
175 return (1);
177 else if (F_ISSET(sp, SC_SCR_CENTER)) {
178 if (vs_sm_fill(sp, LNO, P_MIDDLE))
179 return (1);
180 } else
181 if (vs_sm_fill(sp, OOBLNO, P_TOP))
182 return (1);
183 F_SET(sp, SC_SCR_REDRAW);
187 * 6: Line movement.
189 * Line changes can cause the top line to change as well. As
190 * before, if the movement is large, the screen is repainted.
192 * 6a: Small screens.
194 * Users can use the window, w300, w1200 and w9600 options to make
195 * the screen artificially small. The behavior of these options
196 * in the historic vi wasn't all that consistent, and, in fact, it
197 * was never documented how various screen movements affected the
198 * screen size. Generally, one of three things would happen:
199 * 1: The screen would expand in size, showing the line
200 * 2: The screen would scroll, showing the line
201 * 3: The screen would compress to its smallest size and
202 * repaint.
203 * In general, scrolling didn't cause compression (200^D was handled
204 * the same as ^D), movement to a specific line would (:N where N
205 * was 1 line below the screen caused a screen compress), and cursor
206 * movement would scroll if it was 11 lines or less, and compress if
207 * it was more than 11 lines. (And, no, I have no idea where the 11
208 * comes from.)
210 * What we do is try and figure out if the line is less than half of
211 * a full screen away. If it is, we expand the screen if there's
212 * room, and then scroll as necessary. The alternative is to compress
213 * and repaint.
215 * !!!
216 * This code is a special case from beginning to end. Unfortunately,
217 * home modems are still slow enough that it's worth having.
219 * XXX
220 * If the line a really long one, i.e. part of the line is on the
221 * screen but the column offset is not, we'll end up in the adjust
222 * code, when we should probably have compressed the screen.
224 if (IS_SMALL(sp))
225 if (LNO < HMAP->lno) {
226 lcnt = vs_sm_nlines(sp, HMAP, LNO, sp->t_maxrows);
227 if (lcnt <= HALFSCREEN(sp))
228 for (; lcnt && sp->t_rows != sp->t_maxrows;
229 --lcnt, ++sp->t_rows) {
230 ++TMAP;
231 if (vs_sm_1down(sp))
232 return (1);
234 else
235 goto small_fill;
236 } else if (LNO > TMAP->lno) {
237 lcnt = vs_sm_nlines(sp, TMAP, LNO, sp->t_maxrows);
238 if (lcnt <= HALFSCREEN(sp))
239 for (; lcnt && sp->t_rows != sp->t_maxrows;
240 --lcnt, ++sp->t_rows) {
241 if (vs_sm_next(sp, TMAP, TMAP + 1))
242 return (1);
243 ++TMAP;
244 if (vs_line(sp, TMAP, NULL, NULL))
245 return (1);
247 else {
248 small_fill: (void)gp->scr_move(sp, LASTLINE(sp), 0);
249 (void)gp->scr_clrtoeol(sp);
250 for (; sp->t_rows > sp->t_minrows;
251 --sp->t_rows, --TMAP) {
252 (void)gp->scr_move(sp, TMAP - HMAP, 0);
253 (void)gp->scr_clrtoeol(sp);
255 if (vs_sm_fill(sp, LNO, P_FILL))
256 return (1);
257 F_SET(sp, SC_SCR_REDRAW);
258 goto adjust;
263 * 6b: Line down, or current screen.
265 if (LNO >= HMAP->lno) {
266 /* Current screen. */
267 if (LNO <= TMAP->lno)
268 goto adjust;
269 if (F_ISSET(sp, SC_SCR_TOP))
270 goto top;
271 if (F_ISSET(sp, SC_SCR_CENTER))
272 goto middle;
275 * If less than half a screen above the line, scroll down
276 * until the line is on the screen.
278 lcnt = vs_sm_nlines(sp, TMAP, LNO, HALFTEXT(sp));
279 if (lcnt < HALFTEXT(sp)) {
280 while (lcnt--)
281 if (vs_sm_1up(sp))
282 return (1);
283 goto adjust;
285 goto bottom;
289 * 6c: If not on the current screen, may request center or top.
291 if (F_ISSET(sp, SC_SCR_TOP))
292 goto top;
293 if (F_ISSET(sp, SC_SCR_CENTER))
294 goto middle;
297 * 6d: Line up.
299 lcnt = vs_sm_nlines(sp, HMAP, LNO, HALFTEXT(sp));
300 if (lcnt < HALFTEXT(sp)) {
302 * If less than half a screen below the line, scroll up until
303 * the line is the first line on the screen. Special check so
304 * that if the screen has been emptied, we refill it.
306 if (db_exist(sp, HMAP->lno)) {
307 while (lcnt--)
308 if (vs_sm_1down(sp))
309 return (1);
310 goto adjust;
314 * If less than a half screen from the bottom of the file,
315 * put the last line of the file on the bottom of the screen.
317 bottom: if (db_last(sp, &lastline))
318 return (1);
319 tmp.lno = LNO;
320 tmp.coff = HMAP->coff;
321 tmp.soff = 1;
322 lcnt = vs_sm_nlines(sp, &tmp, lastline+1, sp->t_rows);
323 if (lcnt < HALFTEXT(sp)) {
324 if (vs_sm_fill(sp, lastline, P_BOTTOM))
325 return (1);
326 F_SET(sp, SC_SCR_REDRAW);
327 goto adjust;
329 /* It's not close, just put the line in the middle. */
330 goto middle;
334 * If less than half a screen from the top of the file, put the first
335 * line of the file at the top of the screen. Otherwise, put the line
336 * in the middle of the screen.
338 tmp.lno = 1;
339 tmp.coff = HMAP->coff;
340 tmp.soff = 1;
341 lcnt = vs_sm_nlines(sp, &tmp, LNO, HALFTEXT(sp));
342 if (lcnt < HALFTEXT(sp)) {
343 if (vs_sm_fill(sp, 1, P_TOP))
344 return (1);
345 } else
346 middle: if (vs_sm_fill(sp, LNO, P_MIDDLE))
347 return (1);
348 if (0) {
349 top: if (vs_sm_fill(sp, LNO, P_TOP))
350 return (1);
352 F_SET(sp, SC_SCR_REDRAW);
355 * At this point we know part of the line is on the screen. Since
356 * scrolling is done using logical lines, not physical, all of the
357 * line may not be on the screen. While that's not necessarily bad,
358 * if the part the cursor is on isn't there, we're going to lose.
359 * This can be tricky; if the line covers the entire screen, lno
360 * may be the same as both ends of the map, that's why we test BOTH
361 * the top and the bottom of the map. This isn't a problem for
362 * left-right scrolling, the cursor movement code handles the problem.
364 * There's a performance issue here if editing *really* long lines.
365 * This gets to the right spot by scrolling, and, in a binary, by
366 * scrolling hundreds of lines. If the adjustment looks like it's
367 * going to be a serious problem, refill the screen and repaint.
369 adjust: if (!O_ISSET(sp, O_LEFTRIGHT) &&
370 (LNO == HMAP->lno || LNO == TMAP->lno)) {
371 cnt = vs_screens(sp, LNO, &CNO);
372 if (LNO == HMAP->lno && cnt < HMAP->soff)
373 if ((HMAP->soff - cnt) > HALFTEXT(sp)) {
374 HMAP->soff = cnt;
375 vs_sm_fill(sp, OOBLNO, P_TOP);
376 F_SET(sp, SC_SCR_REDRAW);
377 } else
378 while (cnt < HMAP->soff)
379 if (vs_sm_1down(sp))
380 return (1);
381 if (LNO == TMAP->lno && cnt > TMAP->soff)
382 if ((cnt - TMAP->soff) > HALFTEXT(sp)) {
383 TMAP->soff = cnt;
384 vs_sm_fill(sp, OOBLNO, P_BOTTOM);
385 F_SET(sp, SC_SCR_REDRAW);
386 } else
387 while (cnt > TMAP->soff)
388 if (vs_sm_1up(sp))
389 return (1);
393 * If the screen needs to be repainted, skip cursor optimization.
394 * However, in the code above we skipped leftright scrolling on
395 * the grounds that the cursor code would handle it. Make sure
396 * the right screen is up.
398 if (F_ISSET(sp, SC_SCR_REDRAW)) {
399 if (O_ISSET(sp, O_LEFTRIGHT))
400 goto slow;
401 goto paint;
405 * 7: Cursor movements (current screen only).
407 if (!LF_ISSET(UPDATE_CURSOR))
408 goto number;
411 * Decide cursor position. If the line has changed, the cursor has
412 * moved over a tab, or don't know where the cursor was, reparse the
413 * line. Otherwise, we've just moved over fixed-width characters,
414 * and can calculate the left/right scrolling and cursor movement
415 * without reparsing the line. Note that we don't know which (if any)
416 * of the characters between the old and new cursor positions changed.
418 * XXX
419 * With some work, it should be possible to handle tabs quickly, at
420 * least in obvious situations, like moving right and encountering
421 * a tab, without reparsing the whole line.
423 * If the line we're working with has changed, reread it..
425 if (F_ISSET(vip, VIP_CUR_INVALID) || LNO != OLNO)
426 goto slow;
428 /* Otherwise, if nothing's changed, ignore the cursor. */
429 if (CNO == OCNO)
430 goto fast;
433 * Get the current line. If this fails, we either have an empty
434 * file and can just repaint, or there's a real problem. This
435 * isn't a performance issue because there aren't any ways to get
436 * here repeatedly.
438 if (db_eget(sp, LNO, &p, &len, &isempty)) {
439 if (isempty)
440 goto slow;
441 return (1);
444 #ifdef DEBUG
445 /* Sanity checking. */
446 if (CNO >= len && len != 0) {
447 msgq(sp, M_ERR, "Error: %s/%d: cno (%u) >= len (%u)",
448 tail(__FILE__), __LINE__, CNO, len);
449 return (1);
451 #endif
453 * The basic scheme here is to look at the characters in between
454 * the old and new positions and decide how big they are on the
455 * screen, and therefore, how many screen positions to move.
457 if (CNO < OCNO) {
459 * 7a: Cursor moved left.
461 * Point to the old character. The old cursor position can
462 * be past EOL if, for example, we just deleted the rest of
463 * the line. In this case, since we don't know the width of
464 * the characters we traversed, we have to do it slowly.
466 p += OCNO;
467 cnt = (OCNO - CNO) + 1;
468 if (OCNO >= len)
469 goto slow;
472 * Quick sanity check -- it's hard to figure out exactly when
473 * we cross a screen boundary as we do in the cursor right
474 * movement. If cnt is so large that we're going to cross the
475 * boundary no matter what, stop now.
477 if (SCNO + 1 + MAX_CHARACTER_COLUMNS < cnt)
478 goto slow;
481 * Count up the widths of the characters. If it's a tab
482 * character, go do it the the slow way.
484 for (cwtotal = 0; cnt--; cwtotal += KEY_COL(sp, ch))
485 if ((ch = *(UCHAR_T *)p--) == '\t')
486 goto slow;
489 * Decrement the screen cursor by the total width of the
490 * characters minus 1.
492 cwtotal -= 1;
495 * If we're moving left, and there's a wide character in the
496 * current position, go to the end of the character.
498 if (KEY_COL(sp, ch) > 1)
499 cwtotal -= KEY_COL(sp, ch) - 1;
502 * If the new column moved us off of the current logical line,
503 * calculate a new one. If doing leftright scrolling, we've
504 * moved off of the current screen, as well.
506 if (SCNO < cwtotal)
507 goto slow;
508 SCNO -= cwtotal;
509 } else {
511 * 7b: Cursor moved right.
513 * Point to the first character to the right.
515 p += OCNO + 1;
516 cnt = CNO - OCNO;
519 * Count up the widths of the characters. If it's a tab
520 * character, go do it the the slow way. If we cross a
521 * screen boundary, we can quit.
523 for (cwtotal = SCNO; cnt--;) {
524 if ((ch = *(UCHAR_T *)p++) == '\t')
525 goto slow;
526 if ((cwtotal += KEY_COL(sp, ch)) >= SCREEN_COLS(sp))
527 break;
531 * Increment the screen cursor by the total width of the
532 * characters.
534 SCNO = cwtotal;
536 /* See screen change comment in section 6a. */
537 if (SCNO >= SCREEN_COLS(sp))
538 goto slow;
542 * 7c: Fast cursor update.
544 * We have the current column, retrieve the current row.
546 fast: (void)gp->scr_cursor(sp, &y, &notused);
547 goto done_cursor;
550 * 7d: Slow cursor update.
552 * Walk through the map and find the current line.
554 slow: for (smp = HMAP; smp->lno != LNO; ++smp);
557 * 7e: Leftright scrolling adjustment.
559 * If doing left-right scrolling and the cursor movement has changed
560 * the displayed screen, scroll the screen left or right, unless we're
561 * updating the info line in which case we just scroll that one line.
562 * We adjust the offset up or down until we have a window that covers
563 * the current column, making sure that we adjust differently for the
564 * first screen as compared to subsequent ones.
566 if (O_ISSET(sp, O_LEFTRIGHT)) {
568 * Get the screen column for this character, and correct
569 * for the number option offset.
571 cnt = vs_columns(sp, NULL, LNO, &CNO, NULL);
572 if (O_ISSET(sp, O_NUMBER))
573 cnt -= O_NUMBER_LENGTH;
575 /* Adjust the window towards the beginning of the line. */
576 off = smp->coff;
577 if (off >= cnt) {
578 do {
579 if (off >= O_VAL(sp, O_SIDESCROLL))
580 off -= O_VAL(sp, O_SIDESCROLL);
581 else {
582 off = 0;
583 break;
585 } while (off >= cnt);
586 goto shifted;
589 /* Adjust the window towards the end of the line. */
590 if (off == 0 && off + SCREEN_COLS(sp) < cnt ||
591 off != 0 && off + sp->cols < cnt) {
592 do {
593 off += O_VAL(sp, O_SIDESCROLL);
594 } while (off + sp->cols < cnt);
596 shifted: /* Fill in screen map with the new offset. */
597 if (F_ISSET(sp, SC_TINPUT_INFO))
598 smp->coff = off;
599 else {
600 for (smp = HMAP; smp <= TMAP; ++smp)
601 smp->coff = off;
602 leftright_warp = 1;
604 goto paint;
608 * We may have jumped here to adjust a leftright screen because
609 * redraw was set. If so, we have to paint the entire screen.
611 if (F_ISSET(sp, SC_SCR_REDRAW))
612 goto paint;
616 * Update the screen lines for this particular file line until we
617 * have a new screen cursor position.
619 for (y = -1,
620 vip->sc_smap = NULL; smp <= TMAP && smp->lno == LNO; ++smp) {
621 if (vs_line(sp, smp, &y, &SCNO))
622 return (1);
623 if (y != -1) {
624 vip->sc_smap = smp;
625 break;
628 goto done_cursor;
631 * 8: Repaint the entire screen.
633 * Lost big, do what you have to do. We flush the cache, since
634 * SC_SCR_REDRAW gets set when the screen isn't worth fixing, and
635 * it's simpler to repaint. So, don't trust anything that we
636 * think we know about it.
638 paint: for (smp = HMAP; smp <= TMAP; ++smp)
639 SMAP_FLUSH(smp);
640 for (y = -1, vip->sc_smap = NULL, smp = HMAP; smp <= TMAP; ++smp) {
641 if (vs_line(sp, smp, &y, &SCNO))
642 return (1);
643 if (y != -1 && vip->sc_smap == NULL)
644 vip->sc_smap = smp;
647 * If it's a small screen and we're redrawing, clear the unused lines,
648 * ex may have overwritten them.
650 if (F_ISSET(sp, SC_SCR_REDRAW) && IS_SMALL(sp))
651 for (cnt = sp->t_rows; cnt <= sp->t_maxrows; ++cnt) {
652 (void)gp->scr_move(sp, cnt, 0);
653 (void)gp->scr_clrtoeol(sp);
656 didpaint = 1;
658 done_cursor:
660 * Sanity checking. When the repainting code messes up, the usual
661 * result is we don't repaint the cursor and so sc_smap will be
662 * NULL. If we're debugging, die, otherwise restart from scratch.
664 #ifdef DEBUG
665 if (vip->sc_smap == NULL) {
666 fprintf(stderr, "smap error\n");
667 sleep(100);
668 abort();
670 #else
671 if (vip->sc_smap == NULL) {
672 F_SET(sp, SC_SCR_REFORMAT);
673 return (vs_paint(sp, flags));
675 #endif
678 * 9: Set the remembered cursor values.
680 OCNO = CNO;
681 OLNO = LNO;
684 * 10: Repaint the line numbers.
686 * If O_NUMBER is set and the VIP_N_RENUMBER bit is set, and we
687 * didn't repaint the screen, repaint all of the line numbers,
688 * they've changed.
690 number: if (O_ISSET(sp, O_NUMBER) &&
691 F_ISSET(vip, VIP_N_RENUMBER) && !didpaint && vs_number(sp))
692 return (1);
695 * 11: Update the mode line, position the cursor, and flush changes.
697 * If we warped the screen, we have to refresh everything.
699 if (leftright_warp)
700 LF_SET(UPDATE_CURSOR | UPDATE_SCREEN);
702 if (LF_ISSET(UPDATE_SCREEN) && !IS_ONELINE(sp) &&
703 !F_ISSET(vip, VIP_S_MODELINE) && !F_ISSET(sp, SC_TINPUT_INFO))
704 vs_modeline(sp);
706 if (LF_ISSET(UPDATE_CURSOR)) {
707 (void)gp->scr_move(sp, y, SCNO);
710 * XXX
711 * If the screen shifted, we recalculate the "most favorite"
712 * cursor position. Vi won't know that we've warped the
713 * screen, so it's going to have a wrong idea about where the
714 * cursor should be. This is vi's problem, and fixing it here
715 * is a gross layering violation.
717 if (leftright_warp)
718 (void)vs_column(sp, &sp->rcm);
721 if (LF_ISSET(UPDATE_SCREEN))
722 (void)gp->scr_refresh(sp, F_ISSET(vip, VIP_N_EX_PAINT));
724 /* 12: Clear the flags that are handled by this routine. */
725 F_CLR(sp, SC_SCR_CENTER | SC_SCR_REDRAW | SC_SCR_REFORMAT | SC_SCR_TOP);
726 F_CLR(vip, VIP_CUR_INVALID |
727 VIP_N_EX_PAINT | VIP_N_REFRESH | VIP_N_RENUMBER | VIP_S_MODELINE);
729 return (0);
731 #undef LNO
732 #undef OLNO
733 #undef CNO
734 #undef OCNO
735 #undef SCNO
739 * vs_modeline --
740 * Update the mode line.
742 static void
743 vs_modeline(SCR *sp)
745 static char * const modes[] = {
746 "215|Append", /* SM_APPEND */
747 "216|Change", /* SM_CHANGE */
748 "217|Command", /* SM_COMMAND */
749 "218|Insert", /* SM_INSERT */
750 "219|Replace", /* SM_REPLACE */
752 GS *gp;
753 size_t cols, curcol, curlen, endpoint, len, midpoint;
754 const char *t;
755 int ellipsis;
756 char *p, buf[20];
758 gp = sp->gp;
761 * We put down the file name, the ruler, the mode and the dirty flag.
762 * If there's not enough room, there's not enough room, we don't play
763 * any special games. We try to put the ruler in the middle and the
764 * mode and dirty flag at the end.
766 * !!!
767 * Leave the last character blank, in case it's a really dumb terminal
768 * with hardware scroll. Second, don't paint the last character in the
769 * screen, SunOS 4.1.1 and Ultrix 4.2 curses won't let you.
771 * Move to the last line on the screen.
773 (void)gp->scr_move(sp, LASTLINE(sp), 0);
775 /* If more than one screen in the display, show the file name. */
776 curlen = 0;
777 if (IS_SPLIT(sp)) {
778 for (p = sp->frp->name; *p != '\0'; ++p);
779 for (ellipsis = 0, cols = sp->cols / 2; --p > sp->frp->name;) {
780 if (*p == '/') {
781 ++p;
782 break;
784 if ((curlen += KEY_LEN(sp, *p)) > cols) {
785 ellipsis = 3;
786 curlen +=
787 KEY_LEN(sp, '.') * 3 + KEY_LEN(sp, ' ');
788 while (curlen > cols) {
789 ++p;
790 curlen -= KEY_LEN(sp, *p);
792 break;
795 if (ellipsis) {
796 while (ellipsis--)
797 (void)gp->scr_addstr(sp,
798 KEY_NAME(sp, '.'), KEY_LEN(sp, '.'));
799 (void)gp->scr_addstr(sp,
800 KEY_NAME(sp, ' '), KEY_LEN(sp, ' '));
802 for (; *p != '\0'; ++p)
803 (void)gp->scr_addstr(sp,
804 KEY_NAME(sp, *p), KEY_LEN(sp, *p));
807 /* Clear the rest of the line. */
808 (void)gp->scr_clrtoeol(sp);
811 * Display the ruler. If we're not at the midpoint yet, move there.
812 * Otherwise, add in two extra spaces.
814 * Adjust the current column for the fact that the editor uses it as
815 * a zero-based number.
817 * XXX
818 * Assume that numbers, commas, and spaces only take up a single
819 * column on the screen.
821 cols = sp->cols - 1;
822 if (O_ISSET(sp, O_RULER)) {
823 vs_column(sp, &curcol);
824 len =
825 snprintf(buf, sizeof(buf), "%lu,%lu", sp->lno, curcol + 1);
827 midpoint = (cols - ((len + 1) / 2)) / 2;
828 if (curlen < midpoint) {
829 (void)gp->scr_move(sp, LASTLINE(sp), midpoint);
830 curlen += len;
831 } else if (curlen + 2 + len < cols) {
832 (void)gp->scr_addstr(sp, " ", 2);
833 curlen += 2 + len;
835 (void)gp->scr_addstr(sp, buf, len);
839 * Display the mode and the modified flag, as close to the end of the
840 * line as possible, but guaranteeing at least two spaces between the
841 * ruler and the modified flag.
843 #define MODESIZE 9
844 endpoint = cols;
845 if (O_ISSET(sp, O_SHOWMODE)) {
846 if (F_ISSET(sp->ep, F_MODIFIED))
847 --endpoint;
848 t = msg_cat(sp, modes[sp->showmode], &len);
849 endpoint -= len;
852 if (endpoint > curlen + 2) {
853 (void)gp->scr_move(sp, LASTLINE(sp), endpoint);
854 if (O_ISSET(sp, O_SHOWMODE)) {
855 if (F_ISSET(sp->ep, F_MODIFIED))
856 (void)gp->scr_addstr(sp,
857 KEY_NAME(sp, '*'), KEY_LEN(sp, '*'));
858 (void)gp->scr_addstr(sp, t, len);