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