Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / vs_line.c
blobceb8f05e03ea9926d29398f4b09dd248a6f9a47b
1 /* $NetBSD: vs_line.c,v 1.4 2009/01/18 03:45:50 lukem Exp $ */
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.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "../common/common.h"
28 #include "vi.h"
30 #ifdef VISIBLE_TAB_CHARS
31 #define TABCH '-'
32 #else
33 #define TABCH ' '
34 #endif
37 * vs_line --
38 * Update one line on the screen.
40 * PUBLIC: int vs_line __P((SCR *, SMAP *, size_t *, size_t *));
42 int
43 vs_line(SCR *sp, SMAP *smp, size_t *yp, size_t *xp)
45 unsigned char *kp;
46 GS *gp;
47 SMAP *tsmp;
48 size_t chlen = 0, cno_cnt, cols_per_screen, len, nlen;
49 size_t offset_in_char, offset_in_line, oldx, oldy;
50 size_t scno, skip_cols, skip_screens;
51 int dne, is_cached, is_partial, is_tab, no_draw;
52 int list_tab, list_dollar;
53 CHAR_T *p;
54 CHAR_T *cbp, *ecbp, cbuf[128];
55 CHAR_T ch = '\0';
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 no_draw = 0;
66 if (!F_ISSET(sp, SC_TINPUT_INFO) && VIP(sp)->totalcount > 1)
67 no_draw = 1;
68 if (F_ISSET(sp, SC_SCR_EXWROTE) && (size_t)(smp - HMAP) != LASTLINE(sp))
69 no_draw = 1;
72 * Assume that, if the cache entry for the line is filled in, the
73 * line is already on the screen, and all we need to do is return
74 * the cursor position. If the calling routine doesn't need the
75 * cursor position, we can just return.
77 is_cached = SMAP_CACHE(smp);
78 if (yp == NULL && (is_cached || no_draw))
79 return (0);
82 * A nasty side effect of this routine is that it returns the screen
83 * position for the "current" character. Not pretty, but this is the
84 * only routine that really knows what's out there.
86 * Move to the line. This routine can be called by vs_sm_position(),
87 * which uses it to fill in the cache entry so it can figure out what
88 * the real contents of the screen are. Because of this, we have to
89 * return to whereever we started from.
91 gp = sp->gp;
92 (void)gp->scr_cursor(sp, &oldy, &oldx);
93 (void)gp->scr_move(sp, smp - HMAP, 0);
95 /* Get the line. */
96 dne = db_get(sp, smp->lno, 0, &p, &len);
99 * Special case if we're printing the info/mode line. Skip printing
100 * the leading number, as well as other minor setup. The only time
101 * this code paints the mode line is when the user is entering text
102 * for a ":" command, so we can put the code here instead of dealing
103 * with the empty line logic below. This is a kludge, but it's pretty
104 * much confined to this module.
106 * Set the number of columns for this screen.
107 * Set the number of chars or screens to skip until a character is to
108 * be displayed.
110 cols_per_screen = sp->cols;
111 if (O_ISSET(sp, O_LEFTRIGHT)) {
112 skip_screens = 0;
113 skip_cols = smp->coff;
114 } else {
115 skip_screens = smp->soff - 1;
116 skip_cols = skip_screens * cols_per_screen;
119 list_tab = O_ISSET(sp, O_LIST);
120 if (F_ISSET(sp, SC_TINPUT_INFO))
121 list_dollar = 0;
122 else {
123 list_dollar = list_tab;
126 * If O_NUMBER is set, the line doesn't exist and it's line
127 * number 1, i.e., an empty file, display the line number.
129 * If O_NUMBER is set, the line exists and the first character
130 * on the screen is the first character in the line, display
131 * the line number.
133 * !!!
134 * If O_NUMBER set, decrement the number of columns in the
135 * first screen. DO NOT CHANGE THIS -- IT'S RIGHT! The
136 * rest of the code expects this to reflect the number of
137 * columns in the first screen, regardless of the number of
138 * columns we're going to skip.
140 if (O_ISSET(sp, O_NUMBER)) {
141 cols_per_screen -= O_NUMBER_LENGTH;
142 if ((!dne || smp->lno == 1) && skip_cols == 0) {
143 nlen = snprintf((char*)cbuf,
144 sizeof(cbuf), O_NUMBER_FMT,
145 (unsigned long)smp->lno);
146 (void)gp->scr_addstr(sp, (char*)cbuf, nlen);
152 * Special case non-existent lines and the first line of an empty
153 * file. In both cases, the cursor position is 0, but corrected
154 * as necessary for the O_NUMBER field, if it was displayed.
156 if (dne || len == 0) {
157 /* Fill in the cursor. */
158 if (yp != NULL && smp->lno == sp->lno) {
159 *yp = smp - HMAP;
160 *xp = sp->cols - cols_per_screen;
163 /* If the line is on the screen, quit. */
164 if (is_cached || no_draw)
165 goto ret1;
167 /* Set line cache information. */
168 smp->c_sboff = smp->c_eboff = 0;
169 smp->c_scoff = smp->c_eclen = 0;
172 * Lots of special cases for empty lines, but they only apply
173 * if we're displaying the first screen of the line.
175 if (skip_cols == 0) {
176 if (dne) {
177 if (smp->lno == 1) {
178 if (list_dollar) {
179 ch = L('$');
180 goto empty;
182 } else {
183 ch = L('~');
184 goto empty;
186 } else
187 if (list_dollar) {
188 ch = L('$');
189 empty: (void)gp->scr_addstr(sp,
190 (const char *)KEY_NAME(sp, ch),
191 KEY_LEN(sp, ch));
195 (void)gp->scr_clrtoeol(sp);
196 (void)gp->scr_move(sp, oldy, oldx);
197 return (0);
200 /* If we shortened this line in another screen, the cursor
201 * position may have fallen off.
203 if (sp->lno == smp->lno && sp->cno >= len)
204 sp->cno = len - 1;
207 * If we just wrote this or a previous line, we cached the starting
208 * and ending positions of that line. The way it works is we keep
209 * information about the lines displayed in the SMAP. If we're
210 * painting the screen in the forward direction, this saves us from
211 * reformatting the physical line for every line on the screen. This
212 * wins big on binary files with 10K lines.
214 * Test for the first screen of the line, then the current screen line,
215 * then the line behind us, then do the hard work. Note, it doesn't
216 * do us any good to have a line in front of us -- it would be really
217 * hard to try and figure out tabs in the reverse direction, i.e. how
218 * many spaces a tab takes up in the reverse direction depends on
219 * what characters preceded it.
221 * Test for the first screen of the line.
223 if (skip_cols == 0) {
224 smp->c_sboff = offset_in_line = 0;
225 smp->c_scoff = offset_in_char = 0;
226 p = &p[offset_in_line];
227 goto display;
230 /* Test to see if we've seen this exact line before. */
231 if (is_cached) {
232 offset_in_line = smp->c_sboff;
233 offset_in_char = smp->c_scoff;
234 p = &p[offset_in_line];
236 /* Set cols_per_screen to 2nd and later line length. */
237 if (O_ISSET(sp, O_LEFTRIGHT) || skip_cols > cols_per_screen)
238 cols_per_screen = sp->cols;
239 goto display;
242 /* Test to see if we saw an earlier part of this line before. */
243 if (smp != HMAP &&
244 SMAP_CACHE(tsmp = smp - 1) && tsmp->lno == smp->lno) {
245 if (tsmp->c_eclen != tsmp->c_ecsize) {
246 offset_in_line = tsmp->c_eboff;
247 offset_in_char = tsmp->c_eclen;
248 } else {
249 offset_in_line = tsmp->c_eboff + 1;
250 offset_in_char = 0;
253 /* Put starting info for this line in the cache. */
254 smp->c_sboff = offset_in_line;
255 smp->c_scoff = offset_in_char;
256 p = &p[offset_in_line];
258 /* Set cols_per_screen to 2nd and later line length. */
259 if (O_ISSET(sp, O_LEFTRIGHT) || skip_cols > cols_per_screen)
260 cols_per_screen = sp->cols;
261 goto display;
264 scno = 0;
265 offset_in_line = 0;
266 offset_in_char = 0;
268 /* Do it the hard way, for leftright scrolling screens. */
269 if (O_ISSET(sp, O_LEFTRIGHT)) {
270 for (; offset_in_line < len; ++offset_in_line) {
271 chlen = (ch = *p++) == L('\t') && !list_tab ?
272 TAB_OFF(scno) : KEY_COL(sp, ch);
273 if ((scno += chlen) >= skip_cols)
274 break;
277 /* Set cols_per_screen to 2nd and later line length. */
278 cols_per_screen = sp->cols;
280 /* Put starting info for this line in the cache. */
281 if (offset_in_line >= len) {
282 smp->c_sboff = offset_in_line;
283 smp->c_scoff = 255;
284 } else if (scno != skip_cols) {
285 smp->c_sboff = offset_in_line;
286 smp->c_scoff =
287 offset_in_char = chlen - (scno - skip_cols);
288 --p;
289 } else {
290 smp->c_sboff = ++offset_in_line;
291 smp->c_scoff = 0;
295 /* Do it the hard way, for historic line-folding screens. */
296 else {
297 for (; offset_in_line < len; ++offset_in_line) {
298 chlen = (ch = *p++) == L('\t') && !list_tab ?
299 TAB_OFF(scno) : KEY_COL(sp, ch);
300 if ((scno += chlen) < cols_per_screen)
301 continue;
302 scno -= cols_per_screen;
304 /* Set cols_per_screen to 2nd and later line length. */
305 cols_per_screen = sp->cols;
308 * If crossed the last skipped screen boundary, start
309 * displaying the characters.
311 if (--skip_screens == 0)
312 break;
315 /* Put starting info for this line in the cache. */
316 if (scno != 0) {
317 smp->c_sboff = offset_in_line;
318 smp->c_scoff = offset_in_char = chlen - scno;
319 --p;
320 } else {
321 smp->c_sboff = ++offset_in_line;
322 smp->c_scoff = 0;
326 display:
328 * Set the number of characters to skip before reaching the cursor
329 * character. Offset by 1 and use 0 as a flag value. Vs_line is
330 * called repeatedly with a valid pointer to a cursor position.
331 * Don't fill anything in unless it's the right line and the right
332 * character, and the right part of the character...
334 if (yp == NULL ||
335 smp->lno != sp->lno || sp->cno < offset_in_line ||
336 offset_in_line + cols_per_screen < sp->cno) {
337 cno_cnt = 0;
338 /* If the line is on the screen, quit. */
339 if (is_cached || no_draw)
340 goto ret1;
341 } else
342 cno_cnt = (sp->cno - offset_in_line) + 1;
344 /* This is the loop that actually displays characters. */
345 ecbp = (cbp = cbuf) + sizeof(cbuf)/sizeof(CHAR_T) - 1;
346 for (is_partial = 0, scno = 0;
347 offset_in_line < len; ++offset_in_line, offset_in_char = 0) {
348 if ((ch = *p++) == L('\t') && !list_tab) {
349 scno += chlen = TAB_OFF(scno) - offset_in_char;
350 is_tab = 1;
351 } else {
352 scno += chlen = KEY_COL(sp, ch) - offset_in_char;
353 is_tab = 0;
357 * Only display up to the right-hand column. Set a flag if
358 * the entire character wasn't displayed for use in setting
359 * the cursor. If reached the end of the line, set the cache
360 * info for the screen. Don't worry about there not being
361 * characters to display on the next screen, its lno/off won't
362 * match up in that case.
364 if (scno >= cols_per_screen) {
365 if (is_tab == 1) {
366 chlen -= scno - cols_per_screen;
367 smp->c_ecsize = smp->c_eclen = chlen;
368 scno = cols_per_screen;
369 } else {
370 smp->c_ecsize = chlen;
371 chlen -= scno - cols_per_screen;
372 smp->c_eclen = chlen;
374 if (scno > cols_per_screen)
375 is_partial = 1;
377 smp->c_eboff = offset_in_line;
379 /* Terminate the loop. */
380 offset_in_line = len;
384 * If the caller wants the cursor value, and this was the
385 * cursor character, set the value. There are two ways to
386 * put the cursor on a character -- if it's normal display
387 * mode, it goes on the last column of the character. If
388 * it's input mode, it goes on the first. In normal mode,
389 * set the cursor only if the entire character was displayed.
391 if (cno_cnt &&
392 --cno_cnt == 0 && (F_ISSET(sp, SC_TINPUT) || !is_partial)) {
393 *yp = smp - HMAP;
394 if (F_ISSET(sp, SC_TINPUT))
395 if (is_partial)
396 *xp = scno - smp->c_ecsize;
397 else
398 *xp = scno - chlen;
399 else
400 *xp = scno - 1;
401 if (O_ISSET(sp, O_NUMBER) &&
402 !F_ISSET(sp, SC_TINPUT_INFO) && skip_cols == 0)
403 *xp += O_NUMBER_LENGTH;
405 /* If the line is on the screen, quit. */
406 if (is_cached || no_draw)
407 goto ret1;
410 /* If the line is on the screen, don't display anything. */
411 if (is_cached || no_draw)
412 continue;
414 #define FLUSH { \
415 *cbp = '\0'; \
416 (void)gp->scr_waddstr(sp, cbuf, cbp - cbuf); \
417 cbp = cbuf; \
420 * Display the character. We do tab expansion here because
421 * the screen interface doesn't have any way to set the tab
422 * length. Note, it's theoretically possible for chlen to
423 * be larger than cbuf, if the user set a impossibly large
424 * tabstop.
426 if (is_tab)
427 while (chlen--) {
428 if (cbp >= ecbp)
429 FLUSH;
430 *cbp++ = TABCH;
432 else {
433 if (cbp + chlen >= ecbp)
434 FLUSH;
436 /* don't display half a wide character */
437 if (is_partial && CHAR_WIDTH(sp, ch) > 1) {
438 *cbp++ = ' ';
439 break;
442 /* XXXX this needs some rethinking */
443 if (INTISWIDE(ch)) {
444 /* Put a space before non-spacing char. */
445 if (CHAR_WIDTH(sp, ch) <= 0)
446 *cbp++ = L(' ');
447 *cbp++ = ch;
448 } else
449 for (kp = KEY_NAME(sp, ch) + offset_in_char;
450 chlen--;)
451 *cbp++ = (u_char)*kp++;
455 if (scno < cols_per_screen) {
456 /* If didn't paint the whole line, update the cache. */
457 smp->c_ecsize = smp->c_eclen = KEY_LEN(sp, ch);
458 smp->c_eboff = len - 1;
461 * If not the info/mode line, and O_LIST set, and at the
462 * end of the line, and the line ended on this screen,
463 * add a trailing $.
465 if (list_dollar) {
466 ++scno;
468 chlen = KEY_LEN(sp, '$');
469 if (cbp + chlen >= ecbp)
470 FLUSH;
471 for (kp = KEY_NAME(sp, '$'); chlen--;)
472 *cbp++ = *kp++;
475 /* If still didn't paint the whole line, clear the rest. */
476 if (scno < cols_per_screen)
477 (void)gp->scr_clrtoeol(sp);
480 /* Flush any buffered characters. */
481 if (cbp > cbuf)
482 FLUSH;
484 ret1: (void)gp->scr_move(sp, oldy, oldx);
485 return (0);
489 * vs_number --
490 * Repaint the numbers on all the lines.
492 * PUBLIC: int vs_number __P((SCR *));
495 vs_number(SCR *sp)
497 GS *gp;
498 SMAP *smp;
499 VI_PRIVATE *vip;
500 size_t len, oldy, oldx;
501 int exist;
502 char nbuf[10];
504 gp = sp->gp;
505 vip = VIP(sp);
507 /* No reason to do anything if we're in input mode on the info line. */
508 if (F_ISSET(sp, SC_TINPUT_INFO))
509 return (0);
512 * Try and avoid getting the last line in the file, by getting the
513 * line after the last line in the screen -- if it exists, we know
514 * we have to to number all the lines in the screen. Get the one
515 * after the last instead of the last, so that the info line doesn't
516 * fool us. (The problem is that file_lline will lie, and tell us
517 * that the info line is the last line in the file.) If that test
518 * fails, we have to check each line for existence.
520 exist = db_exist(sp, TMAP->lno + 1);
522 (void)gp->scr_cursor(sp, &oldy, &oldx);
523 for (smp = HMAP; smp <= TMAP; ++smp) {
524 /* Numbers are only displayed for the first screen line. */
525 if (O_ISSET(sp, O_LEFTRIGHT)) {
526 if (smp->coff != 0)
527 continue;
528 } else
529 if (smp->soff != 1)
530 continue;
533 * The first line of an empty file gets numbered, otherwise
534 * number any existing line.
536 if (smp->lno != 1 && !exist && !db_exist(sp, smp->lno))
537 break;
539 (void)gp->scr_move(sp, smp - HMAP, 0);
540 len = snprintf(nbuf, sizeof(nbuf), O_NUMBER_FMT,
541 (unsigned long)smp->lno);
542 (void)gp->scr_addstr(sp, nbuf, len);
544 (void)gp->scr_move(sp, oldy, oldx);
545 return (0);