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