tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / vi / vs_line.c
blobf83e9589bd3d7959b87600cd0a50d9c3683900f8
1 /* $NetBSD: vs_line.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
2 /*-
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 static const char sccsid[] = "Id: vs_line.c,v 10.38 2002/01/19 21:59:07 skimo Exp (Berkeley) Date: 2002/01/19 21:59:07 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "../common/common.h"
27 #include "vi.h"
29 #ifdef VISIBLE_TAB_CHARS
30 #define TABCH '-'
31 #else
32 #define TABCH ' '
33 #endif
36 * vs_line --
37 * Update one line on the screen.
39 * PUBLIC: int vs_line __P((SCR *, SMAP *, size_t *, size_t *));
41 int
42 vs_line(SCR *sp, SMAP *smp, size_t *yp, size_t *xp)
44 unsigned char *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 dne, is_cached, is_partial, is_tab, no_draw;
51 int list_tab, list_dollar;
52 CHAR_T *p;
53 CHAR_T *cbp, *ecbp, cbuf[128];
54 ARG_CHAR_T ch = L('\0');
56 #if defined(DEBUG) && 0
57 vtrace(sp, "vs_line: row %u: line: %u off: %u\n",
58 smp - HMAP, smp->lno, smp->off);
59 #endif
61 * If ex modifies the screen after ex output is already on the screen,
62 * don't touch it -- we'll get scrolling wrong, at best.
64 no_draw = 0;
65 if (!F_ISSET(sp, SC_TINPUT_INFO) && VIP(sp)->totalcount > 1)
66 no_draw = 1;
67 if (F_ISSET(sp, SC_SCR_EXWROTE) && (size_t)(smp - HMAP) != LASTLINE(sp))
68 no_draw = 1;
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 || no_draw))
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,
144 (unsigned long)smp->lno);
145 (void)gp->scr_addstr(sp, (char*)cbuf, nlen);
151 * Special case non-existent lines and the first line of an empty
152 * file. In both cases, the cursor position is 0, but corrected
153 * as necessary for the O_NUMBER field, if it was displayed.
155 if (dne || len == 0) {
156 /* Fill in the cursor. */
157 if (yp != NULL && smp->lno == sp->lno) {
158 *yp = smp - HMAP;
159 *xp = sp->cols - cols_per_screen;
162 /* If the line is on the screen, quit. */
163 if (is_cached || no_draw)
164 goto ret1;
166 /* Set line cache information. */
167 smp->c_sboff = smp->c_eboff = 0;
168 smp->c_scoff = smp->c_eclen = 0;
171 * Lots of special cases for empty lines, but they only apply
172 * if we're displaying the first screen of the line.
174 if (skip_cols == 0) {
175 if (dne) {
176 if (smp->lno == 1) {
177 if (list_dollar) {
178 ch = L('$');
179 goto empty;
181 } else {
182 ch = L('~');
183 goto empty;
185 } else
186 if (list_dollar) {
187 ch = L('$');
188 empty: (void)gp->scr_addstr(sp,
189 (const char *)KEY_NAME(sp, ch),
190 KEY_LEN(sp, ch));
194 (void)gp->scr_clrtoeol(sp);
195 (void)gp->scr_move(sp, oldy, oldx);
196 return (0);
199 /* If we shortened this line in another screen, the cursor
200 * position may have fallen off.
202 if (sp->lno == smp->lno && sp->cno >= len)
203 sp->cno = len - 1;
206 * If we just wrote this or a previous line, we cached the starting
207 * and ending positions of that line. The way it works is we keep
208 * information about the lines displayed in the SMAP. If we're
209 * painting the screen in the forward direction, this saves us from
210 * reformatting the physical line for every line on the screen. This
211 * wins big on binary files with 10K lines.
213 * Test for the first screen of the line, then the current screen line,
214 * then the line behind us, then do the hard work. Note, it doesn't
215 * do us any good to have a line in front of us -- it would be really
216 * hard to try and figure out tabs in the reverse direction, i.e. how
217 * many spaces a tab takes up in the reverse direction depends on
218 * what characters preceded it.
220 * Test for the first screen of the line.
222 if (skip_cols == 0) {
223 smp->c_sboff = offset_in_line = 0;
224 smp->c_scoff = offset_in_char = 0;
225 p = &p[offset_in_line];
226 goto display;
229 /* Test to see if we've seen this exact line before. */
230 if (is_cached) {
231 offset_in_line = smp->c_sboff;
232 offset_in_char = smp->c_scoff;
233 p = &p[offset_in_line];
235 /* Set cols_per_screen to 2nd and later line length. */
236 if (O_ISSET(sp, O_LEFTRIGHT) || skip_cols > cols_per_screen)
237 cols_per_screen = sp->cols;
238 goto display;
241 /* Test to see if we saw an earlier part of this line before. */
242 if (smp != HMAP &&
243 SMAP_CACHE(tsmp = smp - 1) && tsmp->lno == smp->lno) {
244 if (tsmp->c_eclen != tsmp->c_ecsize) {
245 offset_in_line = tsmp->c_eboff;
246 offset_in_char = tsmp->c_eclen;
247 } else {
248 offset_in_line = tsmp->c_eboff + 1;
249 offset_in_char = 0;
252 /* Put starting info for this line in the cache. */
253 smp->c_sboff = offset_in_line;
254 smp->c_scoff = offset_in_char;
255 p = &p[offset_in_line];
257 /* Set cols_per_screen to 2nd and later line length. */
258 if (O_ISSET(sp, O_LEFTRIGHT) || skip_cols > cols_per_screen)
259 cols_per_screen = sp->cols;
260 goto display;
263 scno = 0;
264 offset_in_line = 0;
265 offset_in_char = 0;
267 /* Do it the hard way, for leftright scrolling screens. */
268 if (O_ISSET(sp, O_LEFTRIGHT)) {
269 for (; offset_in_line < len; ++offset_in_line) {
270 chlen = (ch = (UCHAR_T)*p++) == L('\t') && !list_tab ?
271 TAB_OFF(scno) : KEY_COL(sp, ch);
272 if ((scno += chlen) >= skip_cols)
273 break;
276 /* Set cols_per_screen to 2nd and later line length. */
277 cols_per_screen = sp->cols;
279 /* Put starting info for this line in the cache. */
280 if (offset_in_line >= len) {
281 smp->c_sboff = offset_in_line;
282 smp->c_scoff = 255;
283 } else if (scno != skip_cols) {
284 smp->c_sboff = offset_in_line;
285 smp->c_scoff =
286 offset_in_char = chlen - (scno - skip_cols);
287 --p;
288 } else {
289 smp->c_sboff = ++offset_in_line;
290 smp->c_scoff = 0;
294 /* Do it the hard way, for historic line-folding screens. */
295 else {
296 for (; offset_in_line < len; ++offset_in_line) {
297 chlen = (ch = (UCHAR_T)*p++) == L('\t') && !list_tab ?
298 TAB_OFF(scno) : KEY_COL(sp, ch);
299 if ((scno += chlen) < cols_per_screen)
300 continue;
301 scno -= cols_per_screen;
303 /* Set cols_per_screen to 2nd and later line length. */
304 cols_per_screen = sp->cols;
307 * If crossed the last skipped screen boundary, start
308 * displaying the characters.
310 if (--skip_screens == 0)
311 break;
314 /* Put starting info for this line in the cache. */
315 if (scno != 0) {
316 smp->c_sboff = offset_in_line;
317 smp->c_scoff = offset_in_char = chlen - scno;
318 --p;
319 } else {
320 smp->c_sboff = ++offset_in_line;
321 smp->c_scoff = 0;
325 display:
327 * Set the number of characters to skip before reaching the cursor
328 * character. Offset by 1 and use 0 as a flag value. Vs_line is
329 * called repeatedly with a valid pointer to a cursor position.
330 * Don't fill anything in unless it's the right line and the right
331 * character, and the right part of the character...
333 if (yp == NULL ||
334 smp->lno != sp->lno || sp->cno < offset_in_line ||
335 offset_in_line + cols_per_screen < sp->cno) {
336 cno_cnt = 0;
337 /* If the line is on the screen, quit. */
338 if (is_cached || no_draw)
339 goto ret1;
340 } else
341 cno_cnt = (sp->cno - offset_in_line) + 1;
343 /* This is the loop that actually displays characters. */
344 ecbp = (cbp = cbuf) + sizeof(cbuf)/sizeof(CHAR_T) - 1;
345 for (is_partial = 0, scno = 0;
346 offset_in_line < len; ++offset_in_line, offset_in_char = 0) {
347 if ((ch = (UCHAR_T)*p++) == L('\t') && !list_tab) {
348 scno += chlen = TAB_OFF(scno) - offset_in_char;
349 is_tab = 1;
350 } else {
351 scno += chlen = KEY_COL(sp, ch) - offset_in_char;
352 is_tab = 0;
356 * Only display up to the right-hand column. Set a flag if
357 * the entire character wasn't displayed for use in setting
358 * the cursor. If reached the end of the line, set the cache
359 * info for the screen. Don't worry about there not being
360 * characters to display on the next screen, its lno/off won't
361 * match up in that case.
363 if (scno >= cols_per_screen) {
364 if (is_tab == 1) {
365 chlen -= scno - cols_per_screen;
366 smp->c_ecsize = smp->c_eclen = chlen;
367 scno = cols_per_screen;
368 } else {
369 smp->c_ecsize = chlen;
370 chlen -= scno - cols_per_screen;
371 smp->c_eclen = chlen;
373 if (scno > cols_per_screen)
374 is_partial = 1;
376 smp->c_eboff = offset_in_line;
378 /* Terminate the loop. */
379 offset_in_line = len;
383 * If the caller wants the cursor value, and this was the
384 * cursor character, set the value. There are two ways to
385 * put the cursor on a character -- if it's normal display
386 * mode, it goes on the last column of the character. If
387 * it's input mode, it goes on the first. In normal mode,
388 * set the cursor only if the entire character was displayed.
390 if (cno_cnt &&
391 --cno_cnt == 0 && (F_ISSET(sp, SC_TINPUT) || !is_partial)) {
392 *yp = smp - HMAP;
393 if (F_ISSET(sp, SC_TINPUT))
394 if (is_partial)
395 *xp = scno - smp->c_ecsize;
396 else
397 *xp = scno - chlen;
398 else
399 *xp = scno - 1;
400 if (O_ISSET(sp, O_NUMBER) &&
401 !F_ISSET(sp, SC_TINPUT_INFO) && skip_cols == 0)
402 *xp += O_NUMBER_LENGTH;
404 /* If the line is on the screen, quit. */
405 if (is_cached || no_draw)
406 goto ret1;
409 /* If the line is on the screen, don't display anything. */
410 if (is_cached || no_draw)
411 continue;
413 #define FLUSH { \
414 *cbp = '\0'; \
415 (void)gp->scr_waddstr(sp, cbuf, cbp - cbuf); \
416 cbp = cbuf; \
419 * Display the character. We do tab expansion here because
420 * the screen interface doesn't have any way to set the tab
421 * length. Note, it's theoretically possible for chlen to
422 * be larger than cbuf, if the user set a impossibly large
423 * tabstop.
425 if (is_tab)
426 while (chlen--) {
427 if (cbp >= ecbp)
428 FLUSH;
429 *cbp++ = TABCH;
431 else {
432 if (cbp + chlen >= ecbp)
433 FLUSH;
435 /* don't display half a wide character */
436 if (is_partial && CHAR_WIDTH(sp, ch) > 1) {
437 *cbp++ = ' ';
438 break;
441 /* XXXX this needs some rethinking */
442 if (INTISWIDE(ch)) {
443 /* Put a space before non-spacing char. */
444 if (CHAR_WIDTH(sp, ch) <= 0)
445 *cbp++ = L(' ');
446 *cbp++ = ch;
447 } else
448 for (kp = KEY_NAME(sp, ch) + offset_in_char;
449 chlen--;)
450 *cbp++ = (u_char)*kp++;
454 if (scno < cols_per_screen) {
455 /* If didn't paint the whole line, update the cache. */
456 smp->c_ecsize = smp->c_eclen = KEY_LEN(sp, ch);
457 smp->c_eboff = len - 1;
460 * If not the info/mode line, and O_LIST set, and at the
461 * end of the line, and the line ended on this screen,
462 * add a trailing $.
464 if (list_dollar) {
465 ++scno;
467 chlen = KEY_LEN(sp, L('$'));
468 if (cbp + chlen >= ecbp)
469 FLUSH;
470 for (kp = KEY_NAME(sp, L('$')); chlen--;)
471 *cbp++ = *kp++;
474 /* If still didn't paint the whole line, clear the rest. */
475 if (scno < cols_per_screen)
476 (void)gp->scr_clrtoeol(sp);
479 /* Flush any buffered characters. */
480 if (cbp > cbuf)
481 FLUSH;
483 ret1: (void)gp->scr_move(sp, oldy, oldx);
484 return (0);
488 * vs_number --
489 * Repaint the numbers on all the lines.
491 * PUBLIC: int vs_number __P((SCR *));
494 vs_number(SCR *sp)
496 GS *gp;
497 SMAP *smp;
498 size_t len, oldy, oldx;
499 int exist;
500 char nbuf[10];
502 gp = sp->gp;
504 /* No reason to do anything if we're in input mode on the info line. */
505 if (F_ISSET(sp, SC_TINPUT_INFO))
506 return (0);
509 * Try and avoid getting the last line in the file, by getting the
510 * line after the last line in the screen -- if it exists, we know
511 * we have to to number all the lines in the screen. Get the one
512 * after the last instead of the last, so that the info line doesn't
513 * fool us. (The problem is that file_lline will lie, and tell us
514 * that the info line is the last line in the file.) If that test
515 * fails, we have to check each line for existence.
517 exist = db_exist(sp, TMAP->lno + 1);
519 (void)gp->scr_cursor(sp, &oldy, &oldx);
520 for (smp = HMAP; smp <= TMAP; ++smp) {
521 /* Numbers are only displayed for the first screen line. */
522 if (O_ISSET(sp, O_LEFTRIGHT)) {
523 if (smp->coff != 0)
524 continue;
525 } else
526 if (smp->soff != 1)
527 continue;
530 * The first line of an empty file gets numbered, otherwise
531 * number any existing line.
533 if (smp->lno != 1 && !exist && !db_exist(sp, smp->lno))
534 break;
536 (void)gp->scr_move(sp, smp - HMAP, 0);
537 len = snprintf(nbuf, sizeof(nbuf), O_NUMBER_FMT,
538 (unsigned long)smp->lno);
539 (void)gp->scr_addstr(sp, nbuf, len);
541 (void)gp->scr_move(sp, oldy, oldx);
542 return (0);