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