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