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.
13 static const char sccsid
[] = "$Id: vs_line.c,v 10.33 2001/05/10 19:25:49 skimo Exp $ (Berkeley) $Date: 2001/05/10 19:25:49 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
25 #include "../common/common.h"
28 #ifdef VISIBLE_TAB_CHARS
36 * Update one line on the screen.
38 * PUBLIC: int vs_line __P((SCR *, SMAP *, size_t *, size_t *));
41 vs_line(sp
, smp
, yp
, xp
)
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 dne
, is_cached
, is_partial
, is_tab
, no_draw
;
53 int list_tab
, list_dollar
;
55 CHAR_T
*cbp
, *ecbp
, cbuf
[128];
58 #if defined(DEBUG) && 0
59 vtrace(sp
, "vs_line: row %u: line: %u off: %u\n",
60 smp
- HMAP
, smp
->lno
, smp
->off
);
63 * If ex modifies the screen after ex output is already on the screen,
64 * don't touch it -- we'll get scrolling wrong, at best.
67 if (!F_ISSET(sp
, SC_TINPUT_INFO
) && VIP(sp
)->totalcount
> 1)
69 if (F_ISSET(sp
, SC_SCR_EXWROTE
) && smp
- HMAP
!= LASTLINE(sp
))
73 * Assume that, if the cache entry for the line is filled in, the
74 * line is already on the screen, and all we need to do is return
75 * the cursor position. If the calling routine doesn't need the
76 * cursor position, we can just return.
78 is_cached
= SMAP_CACHE(smp
);
79 if (yp
== NULL
&& (is_cached
|| no_draw
))
83 * A nasty side effect of this routine is that it returns the screen
84 * position for the "current" character. Not pretty, but this is the
85 * only routine that really knows what's out there.
87 * Move to the line. This routine can be called by vs_sm_position(),
88 * which uses it to fill in the cache entry so it can figure out what
89 * the real contents of the screen are. Because of this, we have to
90 * return to whereever we started from.
93 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
94 (void)gp
->scr_move(sp
, smp
- HMAP
, 0);
97 dne
= db_get(sp
, smp
->lno
, 0, &p
, &len
);
100 * Special case if we're printing the info/mode line. Skip printing
101 * the leading number, as well as other minor setup. The only time
102 * this code paints the mode line is when the user is entering text
103 * for a ":" command, so we can put the code here instead of dealing
104 * with the empty line logic below. This is a kludge, but it's pretty
105 * much confined to this module.
107 * Set the number of columns for this screen.
108 * Set the number of chars or screens to skip until a character is to
111 cols_per_screen
= sp
->cols
;
112 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
114 skip_cols
= smp
->coff
;
116 skip_screens
= smp
->soff
- 1;
117 skip_cols
= skip_screens
* cols_per_screen
;
120 list_tab
= O_ISSET(sp
, O_LIST
);
121 if (F_ISSET(sp
, SC_TINPUT_INFO
))
124 list_dollar
= list_tab
;
127 * If O_NUMBER is set, the line doesn't exist and it's line
128 * number 1, i.e., an empty file, display the line number.
130 * If O_NUMBER is set, the line exists and the first character
131 * on the screen is the first character in the line, display
135 * If O_NUMBER set, decrement the number of columns in the
136 * first screen. DO NOT CHANGE THIS -- IT'S RIGHT! The
137 * rest of the code expects this to reflect the number of
138 * columns in the first screen, regardless of the number of
139 * columns we're going to skip.
141 if (O_ISSET(sp
, O_NUMBER
)) {
142 cols_per_screen
-= O_NUMBER_LENGTH
;
143 if ((!dne
|| smp
->lno
== 1) && skip_cols
== 0) {
144 nlen
= snprintf((char*)cbuf
,
145 sizeof(cbuf
), O_NUMBER_FMT
, 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
) {
160 *xp
= sp
->cols
- cols_per_screen
;
163 /* If the line is on the screen, quit. */
164 if (is_cached
|| no_draw
)
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.
189 empty
: (void)gp
->scr_addstr(sp
,
190 KEY_NAME(sp
, ch
), KEY_LEN(sp
, ch
));
193 (void)gp
->scr_clrtoeol(sp
);
194 (void)gp
->scr_move(sp
, oldy
, oldx
);
199 * If we just wrote this or a previous line, we cached the starting
200 * and ending positions of that line. The way it works is we keep
201 * information about the lines displayed in the SMAP. If we're
202 * painting the screen in the forward direction, this saves us from
203 * reformatting the physical line for every line on the screen. This
204 * wins big on binary files with 10K lines.
206 * Test for the first screen of the line, then the current screen line,
207 * then the line behind us, then do the hard work. Note, it doesn't
208 * do us any good to have a line in front of us -- it would be really
209 * hard to try and figure out tabs in the reverse direction, i.e. how
210 * many spaces a tab takes up in the reverse direction depends on
211 * what characters preceded it.
213 * Test for the first screen of the line.
215 if (skip_cols
== 0) {
216 smp
->c_sboff
= offset_in_line
= 0;
217 smp
->c_scoff
= offset_in_char
= 0;
218 p
= &p
[offset_in_line
];
222 /* Test to see if we've seen this exact line before. */
224 offset_in_line
= smp
->c_sboff
;
225 offset_in_char
= smp
->c_scoff
;
226 p
= &p
[offset_in_line
];
228 /* Set cols_per_screen to 2nd and later line length. */
229 if (O_ISSET(sp
, O_LEFTRIGHT
) || skip_cols
> cols_per_screen
)
230 cols_per_screen
= sp
->cols
;
234 /* Test to see if we saw an earlier part of this line before. */
236 SMAP_CACHE(tsmp
= smp
- 1) && tsmp
->lno
== smp
->lno
) {
237 if (tsmp
->c_eclen
!= tsmp
->c_ecsize
) {
238 offset_in_line
= tsmp
->c_eboff
;
239 offset_in_char
= tsmp
->c_eclen
;
241 offset_in_line
= tsmp
->c_eboff
+ 1;
245 /* Put starting info for this line in the cache. */
246 smp
->c_sboff
= offset_in_line
;
247 smp
->c_scoff
= offset_in_char
;
248 p
= &p
[offset_in_line
];
250 /* Set cols_per_screen to 2nd and later line length. */
251 if (O_ISSET(sp
, O_LEFTRIGHT
) || skip_cols
> cols_per_screen
)
252 cols_per_screen
= sp
->cols
;
260 /* Do it the hard way, for leftright scrolling screens. */
261 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
262 for (; offset_in_line
< len
; ++offset_in_line
) {
263 chlen
= (ch
= *p
++) == L('\t') && !list_tab
?
264 TAB_OFF(scno
) : KEY_COL(sp
, ch
);
265 if ((scno
+= chlen
) >= skip_cols
)
269 /* Set cols_per_screen to 2nd and later line length. */
270 cols_per_screen
= sp
->cols
;
272 /* Put starting info for this line in the cache. */
273 if (scno
!= skip_cols
) {
274 smp
->c_sboff
= offset_in_line
;
276 offset_in_char
= chlen
- (scno
- skip_cols
);
279 smp
->c_sboff
= ++offset_in_line
;
284 /* Do it the hard way, for historic line-folding screens. */
286 for (; offset_in_line
< len
; ++offset_in_line
) {
287 chlen
= (ch
= *p
++) == L('\t') && !list_tab
?
288 TAB_OFF(scno
) : KEY_COL(sp
, ch
);
289 if ((scno
+= chlen
) < cols_per_screen
)
291 scno
-= cols_per_screen
;
293 /* Set cols_per_screen to 2nd and later line length. */
294 cols_per_screen
= sp
->cols
;
297 * If crossed the last skipped screen boundary, start
298 * displaying the characters.
300 if (--skip_screens
== 0)
304 /* Put starting info for this line in the cache. */
306 smp
->c_sboff
= offset_in_line
;
307 smp
->c_scoff
= offset_in_char
= chlen
- scno
;
310 smp
->c_sboff
= ++offset_in_line
;
317 * Set the number of characters to skip before reaching the cursor
318 * character. Offset by 1 and use 0 as a flag value. Vs_line is
319 * called repeatedly with a valid pointer to a cursor position.
320 * Don't fill anything in unless it's the right line and the right
321 * character, and the right part of the character...
324 smp
->lno
!= sp
->lno
|| sp
->cno
< offset_in_line
||
325 offset_in_line
+ cols_per_screen
< sp
->cno
) {
327 /* If the line is on the screen, quit. */
328 if (is_cached
|| no_draw
)
331 cno_cnt
= (sp
->cno
- offset_in_line
) + 1;
333 /* This is the loop that actually displays characters. */
334 ecbp
= (cbp
= cbuf
) + sizeof(cbuf
)/sizeof(CHAR_T
) - 1;
335 for (is_partial
= 0, scno
= 0;
336 offset_in_line
< len
; ++offset_in_line
, offset_in_char
= 0) {
337 if ((ch
= *p
++) == L('\t') && !list_tab
) {
338 scno
+= chlen
= TAB_OFF(scno
) - offset_in_char
;
341 scno
+= chlen
= KEY_COL(sp
, ch
) - offset_in_char
;
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.
353 if (scno
>= cols_per_screen
) {
355 chlen
-= scno
- cols_per_screen
;
356 smp
->c_ecsize
= smp
->c_eclen
= chlen
;
357 scno
= cols_per_screen
;
359 smp
->c_ecsize
= chlen
;
360 chlen
-= scno
- cols_per_screen
;
361 smp
->c_eclen
= chlen
;
363 if (scno
> cols_per_screen
)
366 smp
->c_eboff
= offset_in_line
;
368 /* Terminate the loop. */
369 offset_in_line
= len
;
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.
381 --cno_cnt
== 0 && (F_ISSET(sp
, SC_TINPUT
) || !is_partial
)) {
383 if (F_ISSET(sp
, SC_TINPUT
))
385 *xp
= scno
- smp
->c_ecsize
;
390 if (O_ISSET(sp
, O_NUMBER
) &&
391 !F_ISSET(sp
, SC_TINPUT_INFO
) && skip_cols
== 0)
392 *xp
+= O_NUMBER_LENGTH
;
394 /* If the line is on the screen, quit. */
395 if (is_cached
|| no_draw
)
399 /* If the line is on the screen, don't display anything. */
400 if (is_cached
|| no_draw
)
405 (void)gp->scr_waddstr(sp, cbuf, cbp - cbuf); \
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
422 if (cbp
+ chlen
>= ecbp
)
425 /* don't display half a wide character */
426 if (is_partial
&& CHAR_WIDTH(sp
, ch
) > 1) {
434 for (kp
= KEY_NAME(sp
, ch
) + offset_in_char
;
436 *cbp
++ = (u_char
)*kp
++;
440 if (scno
< cols_per_screen
) {
441 /* If didn't paint the whole line, update the cache. */
442 smp
->c_ecsize
= smp
->c_eclen
= KEY_LEN(sp
, ch
);
443 smp
->c_eboff
= len
- 1;
446 * If not the info/mode line, and O_LIST set, and at the
447 * end of the line, and the line ended on this screen,
453 chlen
= KEY_LEN(sp
, '$');
454 if (cbp
+ chlen
>= ecbp
)
456 for (kp
= KEY_NAME(sp
, '$'); chlen
--;)
460 /* If still didn't paint the whole line, clear the rest. */
461 if (scno
< cols_per_screen
)
462 (void)gp
->scr_clrtoeol(sp
);
465 /* Flush any buffered characters. */
469 ret1
: (void)gp
->scr_move(sp
, oldy
, oldx
);
475 * Repaint the numbers on all the lines.
477 * PUBLIC: int vs_number __P((SCR *));
486 size_t len
, oldy
, oldx
;
493 /* No reason to do anything if we're in input mode on the info line. */
494 if (F_ISSET(sp
, SC_TINPUT_INFO
))
498 * Try and avoid getting the last line in the file, by getting the
499 * line after the last line in the screen -- if it exists, we know
500 * we have to to number all the lines in the screen. Get the one
501 * after the last instead of the last, so that the info line doesn't
502 * fool us. (The problem is that file_lline will lie, and tell us
503 * that the info line is the last line in the file.) If that test
504 * fails, we have to check each line for existence.
506 exist
= db_exist(sp
, TMAP
->lno
+ 1);
508 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
509 for (smp
= HMAP
; smp
<= TMAP
; ++smp
) {
510 /* Numbers are only displayed for the first screen line. */
511 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
519 * The first line of an empty file gets numbered, otherwise
520 * number any existing line.
522 if (smp
->lno
!= 1 && !exist
&& !db_exist(sp
, smp
->lno
))
525 (void)gp
->scr_move(sp
, smp
- HMAP
, 0);
526 len
= snprintf(nbuf
, sizeof(nbuf
), O_NUMBER_FMT
, smp
->lno
);
527 (void)gp
->scr_addstr(sp
, nbuf
, len
);
529 (void)gp
->scr_move(sp
, oldy
, oldx
);