1 /*	$OpenBSD: vs_line.c,v 1.13 2009/10/27 23:59:48 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 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 <limits.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 #include "../common/common.h"
24 #include "vi.h"
25 
26 #ifdef VISIBLE_TAB_CHARS
27 #define	TABCH	'-'
28 #else
29 #define	TABCH	' '
30 #endif
31 
32 /*
33  * vs_line --
34  *	Update one line on the screen.
35  *
36  * PUBLIC: int vs_line(SCR *, SMAP *, size_t *, size_t *);
37  */
38 int
vs_line(sp,smp,yp,xp)39 vs_line(sp, smp, yp, xp)
40 	SCR *sp;
41 	SMAP *smp;
42 	size_t *xp, *yp;
43 {
44 	CHAR_T *kp;
45 	GS *gp;
46 	SMAP *tsmp;
47 	size_t chlen = 0, cno_cnt, cols_per_screen, len, nlen;
48 	size_t offset_in_char, offset_in_line, oldx, oldy;
49 	size_t scno, skip_cols, skip_screens;
50 	int ch = 0, dne, is_cached, is_partial, is_tab, no_draw;
51 	int list_tab, list_dollar;
52 	char *p, *cbp, *ecbp, cbuf[128];
53 
54 #if defined(DEBUG) && 0
55 	TRACE(sp, "vs_line: row %u: line: %u off: %u\n",
56 	    smp - HMAP, smp->lno, smp->off);
57 #endif
58 	/*
59 	 * If ex modifies the screen after ex output is already on the screen,
60 	 * don't touch it -- we'll get scrolling wrong, at best.
61 	 */
62 	no_draw = 0;
63 	if (!F_ISSET(sp, SC_TINPUT_INFO) && VIP(sp)->totalcount > 1)
64 		no_draw = 1;
65 	if (F_ISSET(sp, SC_SCR_EXWROTE) && smp - HMAP != LASTLINE(sp))
66 		no_draw = 1;
67 
68 	/*
69 	 * Assume that, if the cache entry for the line is filled in, the
70 	 * line is already on the screen, and all we need to do is return
71 	 * the cursor position.  If the calling routine doesn't need the
72 	 * cursor position, we can just return.
73 	 */
74 	is_cached = SMAP_CACHE(smp);
75 	if (yp == NULL && (is_cached || no_draw))
76 		return (0);
77 
78 	/*
79 	 * A nasty side effect of this routine is that it returns the screen
80 	 * position for the "current" character.  Not pretty, but this is the
81 	 * only routine that really knows what's out there.
82 	 *
83 	 * Move to the line.  This routine can be called by vs_sm_position(),
84 	 * which uses it to fill in the cache entry so it can figure out what
85 	 * the real contents of the screen are.  Because of this, we have to
86 	 * return to whereever we started from.
87 	 */
88 	gp = sp->gp;
89 	(void)gp->scr_cursor(sp, &oldy, &oldx);
90 	(void)gp->scr_move(sp, smp - HMAP, 0);
91 
92 	/* Get the line. */
93 	dne = db_get(sp, smp->lno, 0, &p, &len);
94 
95 	/*
96 	 * Special case if we're printing the info/mode line.  Skip printing
97 	 * the leading number, as well as other minor setup.  The only time
98 	 * this code paints the mode line is when the user is entering text
99 	 * for a ":" command, so we can put the code here instead of dealing
100 	 * with the empty line logic below.  This is a kludge, but it's pretty
101 	 * much confined to this module.
102 	 *
103 	 * Set the number of columns for this screen.
104 	 * Set the number of chars or screens to skip until a character is to
105 	 * be displayed.
106 	 */
107 	cols_per_screen = sp->cols;
108 	if (O_ISSET(sp, O_LEFTRIGHT)) {
109 		skip_screens = 0;
110 		skip_cols = smp->coff;
111 	} else {
112 		skip_screens = smp->soff - 1;
113 		skip_cols = skip_screens * cols_per_screen;
114 	}
115 
116 	list_tab = O_ISSET(sp, O_LIST);
117 	if (F_ISSET(sp, SC_TINPUT_INFO))
118 		list_dollar = 0;
119 	else {
120 		list_dollar = list_tab;
121 
122 		/*
123 		 * If O_NUMBER is set, the line doesn't exist and it's line
124 		 * number 1, i.e., an empty file, display the line number.
125 		 *
126 		 * If O_NUMBER is set, the line exists and the first character
127 		 * on the screen is the first character in the line, display
128 		 * the line number.
129 		 *
130 		 * !!!
131 		 * If O_NUMBER set, decrement the number of columns in the
132 		 * first screen.  DO NOT CHANGE THIS -- IT'S RIGHT!  The
133 		 * rest of the code expects this to reflect the number of
134 		 * columns in the first screen, regardless of the number of
135 		 * columns we're going to skip.
136 		 */
137 		if (O_ISSET(sp, O_NUMBER)) {
138 			cols_per_screen -= O_NUMBER_LENGTH;
139 			if ((!dne || smp->lno == 1) && skip_cols == 0) {
140 				nlen = snprintf(cbuf, sizeof(cbuf),
141 				    O_NUMBER_FMT, (ulong)smp->lno);
142 				if (nlen >= sizeof(cbuf))
143 					nlen = sizeof(cbuf) - 1;
144 				(void)gp->scr_addstr(sp, cbuf, nlen);
145 			}
146 		}
147 	}
148 
149 	/*
150 	 * Special case non-existent lines and the first line of an empty
151 	 * file.  In both cases, the cursor position is 0, but corrected
152 	 * as necessary for the O_NUMBER field, if it was displayed.
153 	 */
154 	if (dne || len == 0) {
155 		/* Fill in the cursor. */
156 		if (yp != NULL && smp->lno == sp->lno) {
157 			*yp = smp - HMAP;
158 			*xp = sp->cols - cols_per_screen;
159 		}
160 
161 		/* If the line is on the screen, quit. */
162 		if (is_cached || no_draw)
163 			goto ret1;
164 
165 		/* Set line cache information. */
166 		smp->c_sboff = smp->c_eboff = 0;
167 		smp->c_scoff = smp->c_eclen = 0;
168 
169 		/*
170 		 * Lots of special cases for empty lines, but they only apply
171 		 * if we're displaying the first screen of the line.
172 		 */
173 		if (skip_cols == 0) {
174 			if (dne) {
175 				if (smp->lno == 1) {
176 					if (list_dollar) {
177 						ch = '$';
178 						goto empty;
179 					}
180 				} else {
181 					ch = '~';
182 					goto empty;
183 				}
184 			} else
185 				if (list_dollar) {
186 					ch = '$';
187 empty:					(void)gp->scr_addstr(sp,
188 					    KEY_NAME(sp, ch), KEY_LEN(sp, ch));
189 				}
190 		}
191 
192 		(void)gp->scr_clrtoeol(sp);
193 		(void)gp->scr_move(sp, oldy, oldx);
194 		return (0);
195 	}
196 
197 	/*
198 	 * If we just wrote this or a previous line, we cached the starting
199 	 * and ending positions of that line.  The way it works is we keep
200 	 * information about the lines displayed in the SMAP.  If we're
201 	 * painting the screen in the forward direction, this saves us from
202 	 * reformatting the physical line for every line on the screen.  This
203 	 * wins big on binary files with 10K lines.
204 	 *
205 	 * Test for the first screen of the line, then the current screen line,
206 	 * then the line behind us, then do the hard work.  Note, it doesn't
207 	 * do us any good to have a line in front of us -- it would be really
208 	 * hard to try and figure out tabs in the reverse direction, i.e. how
209 	 * many spaces a tab takes up in the reverse direction depends on
210 	 * what characters preceded it.
211 	 *
212 	 * Test for the first screen of the line.
213 	 */
214 	if (skip_cols == 0) {
215 		smp->c_sboff = offset_in_line = 0;
216 		smp->c_scoff = offset_in_char = 0;
217 		p = &p[offset_in_line];
218 		goto display;
219 	}
220 
221 	/* Test to see if we've seen this exact line before. */
222 	if (is_cached) {
223 		offset_in_line = smp->c_sboff;
224 		offset_in_char = smp->c_scoff;
225 		p = &p[offset_in_line];
226 
227 		/* Set cols_per_screen to 2nd and later line length. */
228 		if (O_ISSET(sp, O_LEFTRIGHT) || skip_cols > cols_per_screen)
229 			cols_per_screen = sp->cols;
230 		goto display;
231 	}
232 
233 	/* Test to see if we saw an earlier part of this line before. */
234 	if (smp != HMAP &&
235 	    SMAP_CACHE(tsmp = smp - 1) && tsmp->lno == smp->lno) {
236 		if (tsmp->c_eclen != tsmp->c_ecsize) {
237 			offset_in_line = tsmp->c_eboff;
238 			offset_in_char = tsmp->c_eclen;
239 		} else {
240 			offset_in_line = tsmp->c_eboff + 1;
241 			offset_in_char = 0;
242 		}
243 
244 		/* Put starting info for this line in the cache. */
245 		smp->c_sboff = offset_in_line;
246 		smp->c_scoff = offset_in_char;
247 		p = &p[offset_in_line];
248 
249 		/* Set cols_per_screen to 2nd and later line length. */
250 		if (O_ISSET(sp, O_LEFTRIGHT) || skip_cols > cols_per_screen)
251 			cols_per_screen = sp->cols;
252 		goto display;
253 	}
254 
255 	scno = 0;
256 	offset_in_line = 0;
257 	offset_in_char = 0;
258 
259 	/* Do it the hard way, for leftright scrolling screens. */
260 	if (O_ISSET(sp, O_LEFTRIGHT)) {
261 		for (; offset_in_line < len; ++offset_in_line) {
262 			chlen = (ch = *(u_char *)p++) == '\t' && !list_tab ?
263 			    TAB_OFF(scno) : KEY_LEN(sp, ch);
264 			if ((scno += chlen) >= skip_cols)
265 				break;
266 		}
267 
268 		/* Set cols_per_screen to 2nd and later line length. */
269 		cols_per_screen = sp->cols;
270 
271 		/* Put starting info for this line in the cache. */
272 		if (scno != skip_cols) {
273 			smp->c_sboff = offset_in_line;
274 			smp->c_scoff =
275 			    offset_in_char = chlen - (scno - skip_cols);
276 			--p;
277 		} else {
278 			smp->c_sboff = ++offset_in_line;
279 			smp->c_scoff = 0;
280 		}
281 	}
282 
283 	/* Do it the hard way, for historic line-folding screens. */
284 	else {
285 		for (; offset_in_line < len; ++offset_in_line) {
286 			chlen = (ch = *(u_char *)p++) == '\t' && !list_tab ?
287 			    TAB_OFF(scno) : KEY_LEN(sp, ch);
288 			if ((scno += chlen) < cols_per_screen)
289 				continue;
290 			scno -= cols_per_screen;
291 
292 			/* Set cols_per_screen to 2nd and later line length. */
293 			cols_per_screen = sp->cols;
294 
295 			/*
296 			 * If crossed the last skipped screen boundary, start
297 			 * displaying the characters.
298 			 */
299 			if (--skip_screens == 0)
300 				break;
301 		}
302 
303 		/* Put starting info for this line in the cache. */
304 		if (scno != 0) {
305 			smp->c_sboff = offset_in_line;
306 			smp->c_scoff = offset_in_char = chlen - scno;
307 			--p;
308 		} else {
309 			smp->c_sboff = ++offset_in_line;
310 			smp->c_scoff = 0;
311 		}
312 	}
313 
314 display:
315 	/*
316 	 * Set the number of characters to skip before reaching the cursor
317 	 * character.  Offset by 1 and use 0 as a flag value.  Vs_line is
318 	 * called repeatedly with a valid pointer to a cursor position.
319 	 * Don't fill anything in unless it's the right line and the right
320 	 * character, and the right part of the character...
321 	 */
322 	if (yp == NULL ||
323 	    smp->lno != sp->lno || sp->cno < offset_in_line ||
324 	    offset_in_line + cols_per_screen < sp->cno) {
325 		cno_cnt = 0;
326 		/* If the line is on the screen, quit. */
327 		if (is_cached || no_draw)
328 			goto ret1;
329 	} else
330 		cno_cnt = (sp->cno - offset_in_line) + 1;
331 
332 	ecbp = (cbp = cbuf) + sizeof(cbuf) - 1;
333 
334 	/* This is the loop that actually displays characters. */
335 	for (is_partial = 0, scno = 0;
336 	    offset_in_line < len; ++offset_in_line, offset_in_char = 0) {
337 		if ((ch = *(u_char *)p++) == '\t' && !list_tab) {
338 			scno += chlen = TAB_OFF(scno) - offset_in_char;
339 			is_tab = 1;
340 		} else {
341 			scno += chlen = KEY_LEN(sp, ch) - offset_in_char;
342 			is_tab = 0;
343 		}
344 
345 		/*
346 		 * Only display up to the right-hand column.  Set a flag if
347 		 * the entire character wasn't displayed for use in setting
348 		 * the cursor.  If reached the end of the line, set the cache
349 		 * info for the screen.  Don't worry about there not being
350 		 * characters to display on the next screen, its lno/off won't
351 		 * match up in that case.
352 		 */
353 		if (scno >= cols_per_screen) {
354 			if (is_tab == 1) {
355 				chlen -= scno - cols_per_screen;
356 				smp->c_ecsize = smp->c_eclen = chlen;
357 				scno = cols_per_screen;
358 			} else {
359 				smp->c_ecsize = chlen;
360 				chlen -= scno - cols_per_screen;
361 				smp->c_eclen = chlen;
362 
363 				if (scno > cols_per_screen)
364 					is_partial = 1;
365 			}
366 			smp->c_eboff = offset_in_line;
367 
368 			/* Terminate the loop. */
369 			offset_in_line = len;
370 		}
371 
372 		/*
373 		 * If the caller wants the cursor value, and this was the
374 		 * cursor character, set the value.  There are two ways to
375 		 * put the cursor on a character -- if it's normal display
376 		 * mode, it goes on the last column of the character.  If
377 		 * it's input mode, it goes on the first.  In normal mode,
378 		 * set the cursor only if the entire character was displayed.
379 		 */
380 		if (cno_cnt &&
381 		    --cno_cnt == 0 && (F_ISSET(sp, SC_TINPUT) || !is_partial)) {
382 			*yp = smp - HMAP;
383 			if (F_ISSET(sp, SC_TINPUT)) {
384 				if (is_partial)
385 					*xp = scno - smp->c_ecsize;
386 				else
387 					*xp = scno - chlen;
388 			} else
389 				*xp = scno - 1;
390 			if (O_ISSET(sp, O_NUMBER) &&
391 			    !F_ISSET(sp, SC_TINPUT_INFO) && skip_cols == 0)
392 				*xp += O_NUMBER_LENGTH;
393 
394 			/* If the line is on the screen, quit. */
395 			if (is_cached)
396 				goto ret1;
397 		}
398 
399 		/* If the line is on the screen, don't display anything. */
400 		if (is_cached)
401 			continue;
402 
403 #define	FLUSH(gp, sp, cbp, cbuf) do {					\
404 	*(cbp) = '\0';							\
405 	(void)(gp)->scr_addstr((sp), (cbuf), (cbp) - (cbuf));		\
406 	(cbp) = (cbuf);							\
407 } while (0)
408 		/*
409 		 * Display the character.  We do tab expansion here because
410 		 * the screen interface doesn't have any way to set the tab
411 		 * length.  Note, it's theoretically possible for chlen to
412 		 * be larger than cbuf, if the user set a impossibly large
413 		 * tabstop.
414 		 */
415 		if (is_tab)
416 			while (chlen--) {
417 				if (cbp >= ecbp)
418 					FLUSH(gp, sp, cbp, cbuf);
419 				*cbp++ = TABCH;
420 			}
421 		else {
422 			if (cbp + chlen >= ecbp)
423 				FLUSH(gp, sp, cbp, cbuf);
424 			for (kp = KEY_NAME(sp, ch) + offset_in_char; chlen--;)
425 				*cbp++ = *kp++;
426 		}
427 	}
428 
429 	if (scno < cols_per_screen) {
430 		/* If we didn't paint the whole line, update the cache. */
431 		smp->c_ecsize = smp->c_eclen = KEY_LEN(sp, ch);
432 		smp->c_eboff = len - 1;
433 
434 		/*
435 		 * If not the info/mode line, and O_LIST set, and at the
436 		 * end of the line, and the line ended on this screen,
437 		 * add a trailing $.
438 		 */
439 		if (list_dollar) {
440 			++scno;
441 
442 			chlen = KEY_LEN(sp, '$');
443 			if (cbp + chlen >= ecbp)
444 				FLUSH(gp, sp, cbp, cbuf);
445 			for (kp = KEY_NAME(sp, '$'); chlen--;)
446 				*cbp++ = *kp++;
447 		}
448 
449 		/* If still didn't paint the whole line, clear the rest. */
450 		if (scno < cols_per_screen)
451 			(void)gp->scr_clrtoeol(sp);
452 	}
453 
454 	/* Flush any buffered characters. */
455 	if (cbp > cbuf)
456 		FLUSH(gp, sp, cbp, cbuf);
457 
458 ret1:	(void)gp->scr_move(sp, oldy, oldx);
459 	return (0);
460 }
461 
462 /*
463  * vs_number --
464  *	Repaint the numbers on all the lines.
465  *
466  * PUBLIC: int vs_number(SCR *);
467  */
468 int
vs_number(sp)469 vs_number(sp)
470 	SCR *sp;
471 {
472 	GS *gp;
473 	SMAP *smp;
474 	size_t len, oldy, oldx;
475 	int exist;
476 	char nbuf[10];
477 
478 	gp = sp->gp;
479 
480 	/* No reason to do anything if we're in input mode on the info line. */
481 	if (F_ISSET(sp, SC_TINPUT_INFO))
482 		return (0);
483 
484 	/*
485 	 * Try and avoid getting the last line in the file, by getting the
486 	 * line after the last line in the screen -- if it exists, we know
487 	 * we have to to number all the lines in the screen.  Get the one
488 	 * after the last instead of the last, so that the info line doesn't
489 	 * fool us.  (The problem is that file_lline will lie, and tell us
490 	 * that the info line is the last line in the file.) If that test
491 	 * fails, we have to check each line for existence.
492 	 */
493 	exist = db_exist(sp, TMAP->lno + 1);
494 
495 	(void)gp->scr_cursor(sp, &oldy, &oldx);
496 	for (smp = HMAP; smp <= TMAP; ++smp) {
497 		/* Numbers are only displayed for the first screen line. */
498 		if (O_ISSET(sp, O_LEFTRIGHT)) {
499 			if (smp->coff != 0)
500 				continue;
501 		} else
502 			if (smp->soff != 1)
503 				continue;
504 
505 		/*
506 		 * The first line of an empty file gets numbered, otherwise
507 		 * number any existing line.
508 		 */
509 		if (smp->lno != 1 && !exist && !db_exist(sp, smp->lno))
510 			break;
511 
512 		(void)gp->scr_move(sp, smp - HMAP, 0);
513 		len = snprintf(nbuf, sizeof(nbuf), O_NUMBER_FMT, (ulong)smp->lno);
514 		if (len >= sizeof(nbuf))
515 			len = sizeof(nbuf) - 1;
516 		(void)gp->scr_addstr(sp, nbuf, len);
517 	}
518 	(void)gp->scr_move(sp, oldy, oldx);
519 	return (0);
520 }
521