1 /* $NetBSD: vs_line.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
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.
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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
26 #include "../common/common.h"
29 #ifdef VISIBLE_TAB_CHARS
37 * Update one line on the screen.
39 * PUBLIC: int vs_line __P((SCR *, SMAP *, size_t *, size_t *));
42 vs_line(SCR
*sp
, SMAP
*smp
, size_t *yp
, size_t *xp
)
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
;
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
);
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.
65 if (!F_ISSET(sp
, SC_TINPUT_INFO
) && VIP(sp
)->totalcount
> 1)
67 if (F_ISSET(sp
, SC_SCR_EXWROTE
) && (size_t)(smp
- HMAP
) != LASTLINE(sp
))
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
))
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.
91 (void)gp
->scr_cursor(sp
, &oldy
, &oldx
);
92 (void)gp
->scr_move(sp
, smp
- HMAP
, 0);
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
109 cols_per_screen
= sp
->cols
;
110 if (O_ISSET(sp
, O_LEFTRIGHT
)) {
112 skip_cols
= smp
->coff
;
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
))
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
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
) {
159 *xp
= sp
->cols
- cols_per_screen
;
162 /* If the line is on the screen, quit. */
163 if (is_cached
|| no_draw
)
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) {
188 empty
: (void)gp
->scr_addstr(sp
,
189 (const char *)KEY_NAME(sp
, ch
),
194 (void)gp
->scr_clrtoeol(sp
);
195 (void)gp
->scr_move(sp
, oldy
, oldx
);
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
)
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
];
229 /* Test to see if we've seen this exact line before. */
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
;
241 /* Test to see if we saw an earlier part of this line before. */
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
;
248 offset_in_line
= tsmp
->c_eboff
+ 1;
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
;
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
)
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
;
283 } else if (scno
!= skip_cols
) {
284 smp
->c_sboff
= offset_in_line
;
286 offset_in_char
= chlen
- (scno
- skip_cols
);
289 smp
->c_sboff
= ++offset_in_line
;
294 /* Do it the hard way, for historic line-folding screens. */
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
)
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)
314 /* Put starting info for this line in the cache. */
316 smp
->c_sboff
= offset_in_line
;
317 smp
->c_scoff
= offset_in_char
= chlen
- scno
;
320 smp
->c_sboff
= ++offset_in_line
;
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...
334 smp
->lno
!= sp
->lno
|| sp
->cno
< offset_in_line
||
335 offset_in_line
+ cols_per_screen
< sp
->cno
) {
337 /* If the line is on the screen, quit. */
338 if (is_cached
|| no_draw
)
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
;
351 scno
+= chlen
= KEY_COL(sp
, ch
) - offset_in_char
;
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
) {
365 chlen
-= scno
- cols_per_screen
;
366 smp
->c_ecsize
= smp
->c_eclen
= chlen
;
367 scno
= cols_per_screen
;
369 smp
->c_ecsize
= chlen
;
370 chlen
-= scno
- cols_per_screen
;
371 smp
->c_eclen
= chlen
;
373 if (scno
> cols_per_screen
)
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.
391 --cno_cnt
== 0 && (F_ISSET(sp
, SC_TINPUT
) || !is_partial
)) {
393 if (F_ISSET(sp
, SC_TINPUT
))
395 *xp
= scno
- smp
->c_ecsize
;
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
)
409 /* If the line is on the screen, don't display anything. */
410 if (is_cached
|| no_draw
)
415 (void)gp->scr_waddstr(sp, cbuf, 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
432 if (cbp
+ chlen
>= ecbp
)
435 /* don't display half a wide character */
436 if (is_partial
&& CHAR_WIDTH(sp
, ch
) > 1) {
441 /* XXXX this needs some rethinking */
443 /* Put a space before non-spacing char. */
444 if (CHAR_WIDTH(sp
, ch
) <= 0)
448 for (kp
= KEY_NAME(sp
, ch
) + offset_in_char
;
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,
467 chlen
= KEY_LEN(sp
, L('$'));
468 if (cbp
+ chlen
>= ecbp
)
470 for (kp
= KEY_NAME(sp
, L('$')); chlen
--;)
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. */
483 ret1
: (void)gp
->scr_move(sp
, oldy
, oldx
);
489 * Repaint the numbers on all the lines.
491 * PUBLIC: int vs_number __P((SCR *));
498 size_t len
, oldy
, oldx
;
504 /* No reason to do anything if we're in input mode on the info line. */
505 if (F_ISSET(sp
, SC_TINPUT_INFO
))
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
)) {
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
))
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
);