vs_refresh.c revision 1.3 1 /* $NetBSD: vs_refresh.c,v 1.3 2013/11/25 22:43:46 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.
7 *
8 * See the LICENSE file for redistribution information.
9 */
10
11 #include "config.h"
12
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 */
16
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
20
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "../common/common.h"
29 #include "vi.h"
30
31 #define UPDATE_CURSOR 0x01 /* Update the cursor. */
32 #define UPDATE_SCREEN 0x02 /* Flush to screen. */
33
34 static void vs_modeline __P((SCR *));
35 static int vs_paint __P((SCR *, u_int));
36
37 /*
38 * vs_refresh --
39 * Refresh all screens.
40 *
41 * PUBLIC: int vs_refresh __P((SCR *, int));
42 */
43 int
44 vs_refresh(SCR *sp, int forcepaint)
45 {
46 GS *gp;
47 SCR *tsp;
48 int need_refresh;
49 u_int priv_paint, pub_paint;
50
51 gp = sp->gp;
52
53 /*
54 * 1: Refresh the screen.
55 *
56 * If SC_SCR_REDRAW is set in the current screen, repaint everything
57 * that we can find, including status lines.
58 */
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);
63
64 /*
65 * 2: Related or dirtied screens, or screens with messages.
66 *
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.
72 */
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);
85 }
86
87 /*
88 * 3: Refresh the current screen.
89 *
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.
93 */
94 if (vs_paint(sp, UPDATE_CURSOR | (!forcepaint &&
95 F_ISSET(sp, SC_SCR_VI) && KEYS_WAITING(sp) ? 0 : UPDATE_SCREEN)))
96 return (1);
97
98 /*
99 * 4: Paint any missing status lines.
100 *
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.
108 *
109 * And, finally, if we updated any status lines, make sure the cursor
110 * gets back to where it belongs.
111 */
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);
117 }
118 if (need_refresh)
119 (void)gp->scr_refresh(sp, 0);
120
121 /*
122 * A side-effect of refreshing the screen is that it's now ready
123 * for everything else, i.e. messages.
124 */
125 F_SET(sp, SC_SCR_VI);
126 return (0);
127 }
128
129 /*
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.
138 */
139 static int
140 vs_paint(SCR *sp, u_int flags)
141 {
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;
149
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. */
155
156 gp = sp->gp;
157 vip = VIP(sp);
158 didpaint = leftright_warp = 0;
159
160 /*
161 * 5: Reformat the lines.
162 *
163 * If the lines themselves have changed (:set list, for example),
164 * fill in the map from scratch. Adjust the screen that's being
165 * displayed if the leftright flag is set.
166 */
167 if (F_ISSET(sp, SC_SCR_REFORMAT)) {
168 /* Invalidate the line size cache. */
169 VI_SCR_CFLUSH(vip);
170
171 /* Toss vs_line() cached information. */
172 if (F_ISSET(sp, SC_SCR_TOP)) {
173 if (vs_sm_fill(sp, LNO, P_TOP))
174 return (1);
175 }
176 else if (F_ISSET(sp, SC_SCR_CENTER)) {
177 if (vs_sm_fill(sp, LNO, P_MIDDLE))
178 return (1);
179 } else
180 if (vs_sm_fill(sp, OOBLNO, P_TOP))
181 return (1);
182 F_SET(sp, SC_SCR_REDRAW);
183 }
184
185 /*
186 * 6: Line movement.
187 *
188 * Line changes can cause the top line to change as well. As
189 * before, if the movement is large, the screen is repainted.
190 *
191 * 6a: Small screens.
192 *
193 * Users can use the window, w300, w1200 and w9600 options to make
194 * the screen artificially small. The behavior of these options
195 * in the historic vi wasn't all that consistent, and, in fact, it
196 * was never documented how various screen movements affected the
197 * screen size. Generally, one of three things would happen:
198 * 1: The screen would expand in size, showing the line
199 * 2: The screen would scroll, showing the line
200 * 3: The screen would compress to its smallest size and
201 * repaint.
202 * In general, scrolling didn't cause compression (200^D was handled
203 * the same as ^D), movement to a specific line would (:N where N
204 * was 1 line below the screen caused a screen compress), and cursor
205 * movement would scroll if it was 11 lines or less, and compress if
206 * it was more than 11 lines. (And, no, I have no idea where the 11
207 * comes from.)
208 *
209 * What we do is try and figure out if the line is less than half of
210 * a full screen away. If it is, we expand the screen if there's
211 * room, and then scroll as necessary. The alternative is to compress
212 * and repaint.
213 *
214 * !!!
215 * This code is a special case from beginning to end. Unfortunately,
216 * home modems are still slow enough that it's worth having.
217 *
218 * XXX
219 * If the line a really long one, i.e. part of the line is on the
220 * screen but the column offset is not, we'll end up in the adjust
221 * code, when we should probably have compressed the screen.
222 */
223 if (IS_SMALL(sp)) {
224 if (LNO < HMAP->lno) {
225 lcnt = vs_sm_nlines(sp, HMAP, LNO, sp->t_maxrows);
226 if (lcnt <= HALFSCREEN(sp))
227 for (; lcnt && sp->t_rows != sp->t_maxrows;
228 --lcnt, ++sp->t_rows) {
229 ++TMAP;
230 if (vs_sm_1down(sp))
231 return (1);
232 }
233 else
234 goto small_fill;
235 } else if (LNO > TMAP->lno) {
236 lcnt = vs_sm_nlines(sp, TMAP, LNO, sp->t_maxrows);
237 if (lcnt <= HALFSCREEN(sp))
238 for (; lcnt && sp->t_rows != sp->t_maxrows;
239 --lcnt, ++sp->t_rows) {
240 if (vs_sm_next(sp, TMAP, TMAP + 1))
241 return (1);
242 ++TMAP;
243 if (vs_line(sp, TMAP, NULL, NULL))
244 return (1);
245 }
246 else {
247 small_fill: (void)gp->scr_move(sp, LASTLINE(sp), 0);
248 (void)gp->scr_clrtoeol(sp);
249 for (; sp->t_rows > sp->t_minrows;
250 --sp->t_rows, --TMAP) {
251 (void)gp->scr_move(sp, TMAP - HMAP, 0);
252 (void)gp->scr_clrtoeol(sp);
253 }
254 if (vs_sm_fill(sp, LNO, P_FILL))
255 return (1);
256 F_SET(sp, SC_SCR_REDRAW);
257 goto adjust;
258 }
259 }
260 }
261
262 /*
263 * 6b: Line down, or current screen.
264 */
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;
273
274 /*
275 * If less than half a screen above the line, scroll down
276 * until the line is on the screen.
277 */
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;
284 }
285 goto bottom;
286 }
287
288 /*
289 * 6c: If not on the current screen, may request center or top.
290 */
291 if (F_ISSET(sp, SC_SCR_TOP))
292 goto top;
293 if (F_ISSET(sp, SC_SCR_CENTER))
294 goto middle;
295
296 /*
297 * 6d: Line up.
298 */
299 lcnt = vs_sm_nlines(sp, HMAP, LNO, HALFTEXT(sp));
300 if (lcnt < HALFTEXT(sp)) {
301 /*
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.
305 */
306 if (db_exist(sp, HMAP->lno)) {
307 while (lcnt--)
308 if (vs_sm_1down(sp))
309 return (1);
310 goto adjust;
311 }
312
313 /*
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.
316 */
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;
328 }
329 /* It's not close, just put the line in the middle. */
330 goto middle;
331 }
332
333 /*
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.
337 */
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);
351 }
352 F_SET(sp, SC_SCR_REDRAW);
353
354 /*
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.
363 *
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.
368 */
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 }
382 if (LNO == TMAP->lno && cnt > TMAP->soff) {
383 if ((cnt - TMAP->soff) > HALFTEXT(sp)) {
384 TMAP->soff = cnt;
385 vs_sm_fill(sp, OOBLNO, P_BOTTOM);
386 F_SET(sp, SC_SCR_REDRAW);
387 } else
388 while (cnt > TMAP->soff)
389 if (vs_sm_1up(sp))
390 return (1);
391 }
392 }
393
394 /*
395 * If the screen needs to be repainted, skip cursor optimization.
396 * However, in the code above we skipped leftright scrolling on
397 * the grounds that the cursor code would handle it. Make sure
398 * the right screen is up.
399 */
400 if (F_ISSET(sp, SC_SCR_REDRAW)) {
401 if (O_ISSET(sp, O_LEFTRIGHT))
402 goto slow;
403 goto paint;
404 }
405
406 /*
407 * 7: Cursor movements (current screen only).
408 */
409 if (!LF_ISSET(UPDATE_CURSOR))
410 goto number;
411
412 /*
413 * Decide cursor position. If the line has changed, the cursor has
414 * moved over a tab, or don't know where the cursor was, reparse the
415 * line. Otherwise, we've just moved over fixed-width characters,
416 * and can calculate the left/right scrolling and cursor movement
417 * without reparsing the line. Note that we don't know which (if any)
418 * of the characters between the old and new cursor positions changed.
419 *
420 * XXX
421 * With some work, it should be possible to handle tabs quickly, at
422 * least in obvious situations, like moving right and encountering
423 * a tab, without reparsing the whole line.
424 *
425 * If the line we're working with has changed, reread it..
426 */
427 if (F_ISSET(vip, VIP_CUR_INVALID) || LNO != OLNO)
428 goto slow;
429
430 /* Otherwise, if nothing's changed, ignore the cursor. */
431 if (CNO == OCNO)
432 goto fast;
433
434 /*
435 * Get the current line. If this fails, we either have an empty
436 * file and can just repaint, or there's a real problem. This
437 * isn't a performance issue because there aren't any ways to get
438 * here repeatedly.
439 */
440 if (db_eget(sp, LNO, &p, &len, &isempty)) {
441 if (isempty)
442 goto slow;
443 return (1);
444 }
445
446 #ifdef DEBUG
447 /* Sanity checking. */
448 if (CNO >= len && len != 0) {
449 msgq(sp, M_ERR, "Error: %s/%d: cno (%u) >= len (%u)",
450 tail(__FILE__), __LINE__, CNO, len);
451 return (1);
452 }
453 #endif
454 /*
455 * The basic scheme here is to look at the characters in between
456 * the old and new positions and decide how big they are on the
457 * screen, and therefore, how many screen positions to move.
458 */
459 if (CNO < OCNO) {
460 /*
461 * 7a: Cursor moved left.
462 *
463 * Point to the old character. The old cursor position can
464 * be past EOL if, for example, we just deleted the rest of
465 * the line. In this case, since we don't know the width of
466 * the characters we traversed, we have to do it slowly.
467 */
468 p += OCNO;
469 cnt = (OCNO - CNO) + 1;
470 if (OCNO >= len)
471 goto slow;
472
473 /*
474 * Quick sanity check -- it's hard to figure out exactly when
475 * we cross a screen boundary as we do in the cursor right
476 * movement. If cnt is so large that we're going to cross the
477 * boundary no matter what, stop now.
478 */
479 if (SCNO + 1 + MAX_CHARACTER_COLUMNS < cnt)
480 goto slow;
481
482 /*
483 * Count up the widths of the characters. If it's a tab
484 * character, go do it the the slow way.
485 */
486 for (cwtotal = 0; cnt--; cwtotal += KEY_COL(sp, ch))
487 if ((ch = *(UCHAR_T *)p--) == '\t')
488 goto slow;
489
490 /*
491 * Decrement the screen cursor by the total width of the
492 * characters minus 1.
493 */
494 cwtotal -= 1;
495
496 /*
497 * If we're moving left, and there's a wide character in the
498 * current position, go to the end of the character.
499 */
500 if (KEY_COL(sp, ch) > 1)
501 cwtotal -= KEY_COL(sp, ch) - 1;
502
503 /*
504 * If the new column moved us off of the current logical line,
505 * calculate a new one. If doing leftright scrolling, we've
506 * moved off of the current screen, as well.
507 */
508 if (SCNO < cwtotal)
509 goto slow;
510 SCNO -= cwtotal;
511 } else {
512 /*
513 * 7b: Cursor moved right.
514 *
515 * Point to the first character to the right.
516 */
517 p += OCNO + 1;
518 cnt = CNO - OCNO;
519
520 /*
521 * Count up the widths of the characters. If it's a tab
522 * character, go do it the the slow way. If we cross a
523 * screen boundary, we can quit.
524 */
525 for (cwtotal = SCNO; cnt--;) {
526 if ((ch = *(UCHAR_T *)p++) == '\t')
527 goto slow;
528 if ((cwtotal += KEY_COL(sp, ch)) >= SCREEN_COLS(sp))
529 break;
530 }
531
532 /*
533 * Increment the screen cursor by the total width of the
534 * characters.
535 */
536 SCNO = cwtotal;
537
538 /* See screen change comment in section 6a. */
539 if (SCNO >= SCREEN_COLS(sp))
540 goto slow;
541 }
542
543 /*
544 * 7c: Fast cursor update.
545 *
546 * We have the current column, retrieve the current row.
547 */
548 fast: (void)gp->scr_cursor(sp, &y, ¬used);
549 goto done_cursor;
550
551 /*
552 * 7d: Slow cursor update.
553 *
554 * Walk through the map and find the current line.
555 */
556 slow: for (smp = HMAP; smp->lno != LNO; ++smp);
557
558 /*
559 * 7e: Leftright scrolling adjustment.
560 *
561 * If doing left-right scrolling and the cursor movement has changed
562 * the displayed screen, scroll the screen left or right, unless we're
563 * updating the info line in which case we just scroll that one line.
564 * We adjust the offset up or down until we have a window that covers
565 * the current column, making sure that we adjust differently for the
566 * first screen as compared to subsequent ones.
567 */
568 if (O_ISSET(sp, O_LEFTRIGHT)) {
569 /*
570 * Get the screen column for this character, and correct
571 * for the number option offset.
572 */
573 cnt = vs_columns(sp, NULL, LNO, &CNO, NULL);
574 if (O_ISSET(sp, O_NUMBER))
575 cnt -= O_NUMBER_LENGTH;
576
577 /* Adjust the window towards the beginning of the line. */
578 off = smp->coff;
579 if (off >= cnt) {
580 do {
581 if (off >= O_VAL(sp, O_SIDESCROLL))
582 off -= O_VAL(sp, O_SIDESCROLL);
583 else {
584 off = 0;
585 break;
586 }
587 } while (off >= cnt);
588 goto shifted;
589 }
590
591 /* Adjust the window towards the end of the line. */
592 if ((off == 0 && off + SCREEN_COLS(sp) < cnt) ||
593 (off != 0 && off + sp->cols < cnt)) {
594 do {
595 off += O_VAL(sp, O_SIDESCROLL);
596 } while (off + sp->cols < cnt);
597
598 shifted: /* Fill in screen map with the new offset. */
599 if (F_ISSET(sp, SC_TINPUT_INFO))
600 smp->coff = off;
601 else {
602 for (smp = HMAP; smp <= TMAP; ++smp)
603 smp->coff = off;
604 leftright_warp = 1;
605 }
606 goto paint;
607 }
608
609 /*
610 * We may have jumped here to adjust a leftright screen because
611 * redraw was set. If so, we have to paint the entire screen.
612 */
613 if (F_ISSET(sp, SC_SCR_REDRAW))
614 goto paint;
615 }
616
617 /*
618 * Update the screen lines for this particular file line until we
619 * have a new screen cursor position.
620 */
621 for (y = -1,
622 vip->sc_smap = NULL; smp <= TMAP && smp->lno == LNO; ++smp) {
623 if (vs_line(sp, smp, &y, &SCNO))
624 return (1);
625 if (y != (size_t)-1) {
626 vip->sc_smap = smp;
627 break;
628 }
629 }
630 goto done_cursor;
631
632 /*
633 * 8: Repaint the entire screen.
634 *
635 * Lost big, do what you have to do. We flush the cache, since
636 * SC_SCR_REDRAW gets set when the screen isn't worth fixing, and
637 * it's simpler to repaint. So, don't trust anything that we
638 * think we know about it.
639 */
640 paint: for (smp = HMAP; smp <= TMAP; ++smp)
641 SMAP_FLUSH(smp);
642 for (y = -1, vip->sc_smap = NULL, smp = HMAP; smp <= TMAP; ++smp) {
643 if (vs_line(sp, smp, &y, &SCNO))
644 return (1);
645 if (y != (size_t)-1 && vip->sc_smap == NULL)
646 vip->sc_smap = smp;
647 }
648 /*
649 * If it's a small screen and we're redrawing, clear the unused lines,
650 * ex may have overwritten them.
651 */
652 if (F_ISSET(sp, SC_SCR_REDRAW) && IS_SMALL(sp))
653 for (cnt = sp->t_rows; cnt <= sp->t_maxrows; ++cnt) {
654 (void)gp->scr_move(sp, cnt, 0);
655 (void)gp->scr_clrtoeol(sp);
656 }
657
658 didpaint = 1;
659
660 done_cursor:
661 /*
662 * Sanity checking. When the repainting code messes up, the usual
663 * result is we don't repaint the cursor and so sc_smap will be
664 * NULL. If we're debugging, die, otherwise restart from scratch.
665 */
666 #ifdef DEBUG
667 if (vip->sc_smap == NULL) {
668 fprintf(stderr, "smap error\n");
669 sleep(100);
670 abort();
671 }
672 #else
673 if (vip->sc_smap == NULL) {
674 F_SET(sp, SC_SCR_REFORMAT);
675 return (vs_paint(sp, flags));
676 }
677 #endif
678
679 /*
680 * 9: Set the remembered cursor values.
681 */
682 OCNO = CNO;
683 OLNO = LNO;
684
685 /*
686 * 10: Repaint the line numbers.
687 *
688 * If O_NUMBER is set and the VIP_N_RENUMBER bit is set, and we
689 * didn't repaint the screen, repaint all of the line numbers,
690 * they've changed.
691 */
692 number: if (O_ISSET(sp, O_NUMBER) &&
693 F_ISSET(vip, VIP_N_RENUMBER) && !didpaint && vs_number(sp))
694 return (1);
695
696 /*
697 * 11: Update the mode line, position the cursor, and flush changes.
698 *
699 * If we warped the screen, we have to refresh everything.
700 */
701 if (leftright_warp)
702 LF_SET(UPDATE_CURSOR | UPDATE_SCREEN);
703
704 if (LF_ISSET(UPDATE_SCREEN) && !IS_ONELINE(sp) &&
705 !F_ISSET(vip, VIP_S_MODELINE) && !F_ISSET(sp, SC_TINPUT_INFO))
706 vs_modeline(sp);
707
708 if (LF_ISSET(UPDATE_CURSOR)) {
709 (void)gp->scr_move(sp, y, SCNO);
710
711 /*
712 * XXX
713 * If the screen shifted, we recalculate the "most favorite"
714 * cursor position. Vi won't know that we've warped the
715 * screen, so it's going to have a wrong idea about where the
716 * cursor should be. This is vi's problem, and fixing it here
717 * is a gross layering violation.
718 */
719 if (leftright_warp)
720 (void)vs_column(sp, &sp->rcm);
721 }
722
723 if (LF_ISSET(UPDATE_SCREEN))
724 (void)gp->scr_refresh(sp, F_ISSET(vip, VIP_N_EX_PAINT));
725
726 /* 12: Clear the flags that are handled by this routine. */
727 F_CLR(sp, SC_SCR_CENTER | SC_SCR_REDRAW | SC_SCR_REFORMAT | SC_SCR_TOP);
728 F_CLR(vip, VIP_CUR_INVALID |
729 VIP_N_EX_PAINT | VIP_N_REFRESH | VIP_N_RENUMBER | VIP_S_MODELINE);
730
731 return (0);
732
733 #undef LNO
734 #undef OLNO
735 #undef CNO
736 #undef OCNO
737 #undef SCNO
738 }
739
740 /*
741 * vs_modeline --
742 * Update the mode line.
743 */
744 static void
745 vs_modeline(SCR *sp)
746 {
747 static const char * const modes[] = {
748 "215|Append", /* SM_APPEND */
749 "216|Change", /* SM_CHANGE */
750 "217|Command", /* SM_COMMAND */
751 "218|Insert", /* SM_INSERT */
752 "219|Replace", /* SM_REPLACE */
753 };
754 GS *gp;
755 size_t cols, curcol, curlen, endpoint, len, midpoint;
756 const char *t = NULL;
757 int ellipsis;
758 char *p, buf[20];
759
760 gp = sp->gp;
761
762 /*
763 * We put down the file name, the ruler, the mode and the dirty flag.
764 * If there's not enough room, there's not enough room, we don't play
765 * any special games. We try to put the ruler in the middle and the
766 * mode and dirty flag at the end.
767 *
768 * !!!
769 * Leave the last character blank, in case it's a really dumb terminal
770 * with hardware scroll. Second, don't paint the last character in the
771 * screen, SunOS 4.1.1 and Ultrix 4.2 curses won't let you.
772 *
773 * Move to the last line on the screen.
774 */
775 (void)gp->scr_move(sp, LASTLINE(sp), 0);
776
777 /* If more than one screen in the display, show the file name. */
778 curlen = 0;
779 if (IS_SPLIT(sp)) {
780 for (p = sp->frp->name; *p != '\0'; ++p);
781 for (ellipsis = 0, cols = sp->cols / 2; --p > sp->frp->name;) {
782 if (*p == '/') {
783 ++p;
784 break;
785 }
786 if ((curlen += KEY_LEN(sp, *p)) > cols) {
787 ellipsis = 3;
788 curlen +=
789 KEY_LEN(sp, '.') * 3 + KEY_LEN(sp, ' ');
790 while (curlen > cols) {
791 ++p;
792 curlen -= KEY_LEN(sp, *p);
793 }
794 break;
795 }
796 }
797 if (ellipsis) {
798 while (ellipsis--)
799 (void)gp->scr_addstr(sp,
800 (const char *)KEY_NAME(sp, '.'),
801 KEY_LEN(sp, '.'));
802 (void)gp->scr_addstr(sp,
803 (const char *)KEY_NAME(sp, ' '), KEY_LEN(sp, ' '));
804 }
805 for (; *p != '\0'; ++p)
806 (void)gp->scr_addstr(sp,
807 (const char *)KEY_NAME(sp, *p), KEY_LEN(sp, *p));
808 }
809
810 /* Clear the rest of the line. */
811 (void)gp->scr_clrtoeol(sp);
812
813 /*
814 * Display the ruler. If we're not at the midpoint yet, move there.
815 * Otherwise, add in two extra spaces.
816 *
817 * Adjust the current column for the fact that the editor uses it as
818 * a zero-based number.
819 *
820 * XXX
821 * Assume that numbers, commas, and spaces only take up a single
822 * column on the screen.
823 */
824 cols = sp->cols - 1;
825 if (O_ISSET(sp, O_RULER)) {
826 vs_column(sp, &curcol);
827 len =
828 snprintf(buf, sizeof(buf), "%lu,%lu",
829 (unsigned long)sp->lno, (unsigned long)curcol + 1);
830
831 midpoint = (cols - ((len + 1) / 2)) / 2;
832 if (curlen < midpoint) {
833 (void)gp->scr_move(sp, LASTLINE(sp), midpoint);
834 curlen += len;
835 } else if (curlen + 2 + len < cols) {
836 (void)gp->scr_addstr(sp, " ", 2);
837 curlen += 2 + len;
838 }
839 (void)gp->scr_addstr(sp, buf, len);
840 }
841
842 /*
843 * Display the mode and the modified flag, as close to the end of the
844 * line as possible, but guaranteeing at least two spaces between the
845 * ruler and the modified flag.
846 */
847 #define MODESIZE 9
848 endpoint = cols;
849 if (O_ISSET(sp, O_SHOWMODE)) {
850 if (F_ISSET(sp->ep, F_MODIFIED))
851 --endpoint;
852 t = msg_cat(sp, modes[sp->showmode], &len);
853 endpoint -= len;
854 }
855
856 if (endpoint > curlen + 2) {
857 (void)gp->scr_move(sp, LASTLINE(sp), endpoint);
858 if (O_ISSET(sp, O_SHOWMODE)) {
859 if (F_ISSET(sp->ep, F_MODIFIED))
860 (void)gp->scr_addstr(sp,
861 (const char *)KEY_NAME(sp, '*'),
862 KEY_LEN(sp, '*'));
863 (void)gp->scr_addstr(sp, t, len);
864 }
865 }
866 }
867