2 * Copyright (C) 1984-2002 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines to manipulate the "line buffer".
14 * The line buffer holds a line of output as it is being built
15 * in preparation for output to the screen.
20 #define IS_CONT(c) (((c) & 0xC0) == 0x80)
22 public char *linebuf
= NULL
; /* Buffer which holds the current output line */
23 static char *attr
= NULL
; /* Extension of linebuf to hold attributes */
24 public int size_linebuf
= 0; /* Size of line buffer (and attr buffer) */
26 public int cshift
; /* Current left-shift of output line buffer */
27 public int hshift
; /* Desired left-shift of output line buffer */
28 public int tabstops
[TABSTOP_MAX
] = { 0 }; /* Custom tabstops */
29 public int ntabstops
= 1; /* Number of tabstops */
30 public int tabdefault
= 8; /* Default repeated tabstops */
32 static int curr
; /* Index into linebuf */
33 static int column
; /* Printable length, accounting for
35 static int overstrike
; /* Next char should overstrike previous char */
36 static int last_overstrike
= AT_NORMAL
;
37 static int is_null_line
; /* There is no current line */
38 static int lmargin
; /* Left margin */
39 static int hilites
; /* Number of hilites in this line */
41 static POSITION pendpos
;
42 static char *end_ansi_chars
;
44 static int do_append();
51 extern int status_col
;
52 extern int auto_wrap
, ignaw
;
53 extern int bo_s_width
, bo_e_width
;
54 extern int ul_s_width
, ul_e_width
;
55 extern int bl_s_width
, bl_e_width
;
56 extern int so_s_width
, so_e_width
;
57 extern int sc_width
, sc_height
;
59 extern POSITION start_attnpos
;
60 extern POSITION end_attnpos
;
63 * Initialize from environment variables.
68 end_ansi_chars
= lgetenv("LESSANSIENDCHARS");
69 if (end_ansi_chars
== NULL
|| *end_ansi_chars
== '\0')
71 linebuf
= (char *) ecalloc(LINEBUF_SIZE
, sizeof(char));
72 attr
= (char *) ecalloc(LINEBUF_SIZE
, sizeof(char));
73 size_linebuf
= LINEBUF_SIZE
;
77 * Expand the line buffer.
82 int new_size
= size_linebuf
+ LINEBUF_SIZE
;
83 char *new_buf
= (char *) calloc(new_size
, sizeof(char));
84 char *new_attr
= (char *) calloc(new_size
, sizeof(char));
85 if (new_buf
== NULL
|| new_attr
== NULL
)
93 memcpy(new_buf
, linebuf
, size_linebuf
* sizeof(char));
94 memcpy(new_attr
, attr
, size_linebuf
* sizeof(char));
99 size_linebuf
= new_size
;
104 * Rewind the line buffer.
123 * Insert the line number (of the given position) into the line buffer.
129 register LINENUM linenum
= 0;
132 if (linenums
== OPT_ONPLUS
)
135 * Get the line number and put it in the current line.
136 * {{ Note: since find_linenum calls forw_raw_line,
137 * it may seek in the input file, requiring the caller
138 * of plinenum to re-seek if necessary. }}
139 * {{ Since forw_raw_line modifies linebuf, we must
140 * do this first, before storing anything in linebuf. }}
142 linenum
= find_linenum(pos
);
146 * Display a status column if the -J option is set.
151 if (start_attnpos
!= NULL_POSITION
&&
152 pos
>= start_attnpos
&& pos
< end_attnpos
)
153 attr
[curr
] = AT_STANDOUT
;
160 * Display the line number at the start of each line
161 * if the -N option is set.
163 if (linenums
== OPT_ONPLUS
)
165 char buf
[INT_STRLEN_BOUND(pos
) + 2];
168 linenumtoa(linenum
, buf
);
170 if (n
< MIN_LINENUM_WIDTH
)
171 n
= MIN_LINENUM_WIDTH
;
172 sprintf(linebuf
+curr
, "%*s ", n
, buf
);
173 n
++; /* One space after the line number. */
174 for (i
= 0; i
< n
; i
++)
175 attr
[curr
+i
] = AT_NORMAL
;
182 * Append enough spaces to bring us to the lmargin.
184 while (column
< lmargin
)
187 attr
[curr
++] = AT_NORMAL
;
193 * Determine how many characters are required to shift N columns.
203 * Each char counts for one column, except ANSI color escape
204 * sequences use no columns since they don't move the cursor.
206 while (*p
!= '\0' && len
> 0)
215 if (is_ansi_end(*p
++))
224 * Determine how many characters are required to shift N columns (UTF version).
225 * {{ FIXME: what about color escape sequences in UTF mode? }}
228 utf_shift_chars(s
, len
)
234 while (*s
!= '\0' && len
> 0)
250 * Shift the input line left.
251 * This means discarding N printable chars at the start of the buffer.
260 if (shift
> column
- lmargin
)
261 shift
= column
- lmargin
;
262 if (shift
> curr
- lmargin
)
263 shift
= curr
- lmargin
;
266 nchars
= utf_shift_chars(linebuf
+ lmargin
, shift
);
268 nchars
= shift_chars(linebuf
+ lmargin
, shift
);
271 for (i
= 0; i
< curr
- nchars
; i
++)
273 linebuf
[lmargin
+ i
] = linebuf
[lmargin
+ i
+ nchars
];
274 attr
[lmargin
+ i
] = attr
[lmargin
+ i
+ nchars
];
282 * Return the printing width of the start (enter) sequence
283 * for a given character attribute.
291 case AT_BOLD
: return (bo_s_width
);
292 case AT_UNDERLINE
: return (ul_s_width
);
293 case AT_BLINK
: return (bl_s_width
);
294 case AT_STANDOUT
: return (so_s_width
);
300 * Return the printing width of the end (exit) sequence
301 * for a given character attribute.
309 case AT_BOLD
: return (bo_e_width
);
310 case AT_UNDERLINE
: return (ul_e_width
);
311 case AT_BLINK
: return (bl_e_width
);
312 case AT_STANDOUT
: return (so_e_width
);
318 * Return the printing width of a given character and attribute,
319 * if the character were added to the current position in the line buffer.
320 * Adding a character with a given attribute may cause an enter or exit
321 * attribute sequence to be inserted, so this must be taken into account.
330 if (utf_mode
&& IS_CONT(c
))
335 * Backspace moves backwards one position.
341 * Control characters do unpredicatable things,
342 * so we don't even try to guess; say it doesn't move.
343 * This can only happen if the -r flag is in effect.
348 * Other characters take one space,
349 * plus the width of any attribute enter/exit sequence.
352 if (curr
> 0 && attr
[curr
-1] != a
)
353 w
+= attr_ewidth(attr
[curr
-1]);
354 if (a
&& (curr
== 0 || attr
[curr
-1] != a
))
360 * Delete the previous character in the line buffer.
366 column
-= pwidth(linebuf
[curr
], attr
[curr
]);
370 * Are we currently within a recognized ANSI escape sequence?
378 * Search backwards for either an ESC (which means we ARE in a seq);
379 * or an end char (which means we're NOT in a seq).
381 for (i
= curr
-1; i
>= 0; i
--)
383 if (linebuf
[i
] == ESC
)
385 if (is_ansi_end(linebuf
[i
]))
392 * Is a character the end of an ANSI escape sequence?
398 return (strchr(end_ansi_chars
, c
) != NULL
);
402 * Append a character and attribute to the line buffer.
404 #define STORE_CHAR(c,a,pos) \
405 do { if (store_char((c),(a),(pos))) return (1); else curr++; } while (0)
408 store_char(c
, a
, pos
)
418 if (is_hilited(pos
, pos
+1, 0))
421 * This character should be highlighted.
422 * Override the attribute passed in.
428 if (ctldisp
== OPT_ONPLUS
&& in_ansi_esc_seq())
432 if (ctldisp
!= OPT_ON
&& column
+ w
+ attr_ewidth(a
) > sc_width
)
434 * Won't fit on screen.
438 if (curr
>= size_linebuf
-2)
441 * Won't fit in line buffer.
444 if (expand_linebuf())
449 * Special handling for "magic cookie" terminals.
450 * If an attribute enter/exit sequence has a printing width > 0,
451 * and the sequence is adjacent to a space, delete the space.
452 * We just mark the space as invisible, to avoid having too
453 * many spaces deleted.
454 * {{ Note that even if the attribute width is > 1, we
455 * delete only one space. It's not worth trying to do more.
456 * It's hardly worth doing this much. }}
458 if (curr
> 0 && a
!= AT_NORMAL
&&
459 linebuf
[curr
-1] == ' ' && attr
[curr
-1] == AT_NORMAL
&&
463 * We are about to append an enter-attribute sequence
464 * just after a space. Delete the space.
466 attr
[curr
-1] = AT_INVIS
;
468 } else if (curr
> 0 && attr
[curr
-1] != AT_NORMAL
&&
469 attr
[curr
-1] != AT_INVIS
&& c
== ' ' && a
== AT_NORMAL
&&
470 attr_ewidth(attr
[curr
-1]) > 0)
473 * We are about to append a space just after an
474 * exit-attribute sequence. Delete the space.
479 /* End of magic cookie handling. */
488 * Append a tab to the line buffer.
489 * Store spaces to represent the tab.
491 #define STORE_TAB(a,pos) \
492 do { if (store_tab((a),(pos))) return (1); } while (0)
499 int to_tab
= column
+ cshift
- lmargin
;
502 if (ntabstops
< 2 || to_tab
>= tabstops
[ntabstops
-1])
503 to_tab
= tabdefault
-
504 ((to_tab
- tabstops
[ntabstops
-1]) % tabdefault
);
507 for (i
= ntabstops
- 2; i
>= 0; i
--)
508 if (to_tab
>= tabstops
[i
])
510 to_tab
= tabstops
[i
+1] - to_tab
;
514 STORE_CHAR(' ', attr
, pos
);
515 } while (--to_tab
> 0);
520 * Append a character to the line buffer.
521 * Expand tabs into spaces, handle underlining, boldfacing, etc.
522 * Returns 0 if ok, 1 if couldn't fit in buffer.
533 if (do_append(pendc
, pendpos
))
535 * Oops. We've probably lost the char which
536 * was in pendc, since caller won't back up.
542 if (c
== '\r' && bs_mode
== BS_SPECIAL
)
545 * Don't put the CR into the buffer until we see
546 * the next char. If the next char is a newline,
554 r
= do_append(c
, pos
);
556 * If we need to shift the line, do it.
557 * But wait until we get to at least the middle of the screen,
558 * so shifting it doesn't affect the chars we're currently
559 * pappending. (Bold & underline can get messed up otherwise.)
561 if (cshift
< hshift
&& column
> sc_width
/ 2)
563 linebuf
[curr
] = '\0';
564 pshift(hshift
- cshift
);
569 #define IS_UTF8_4BYTE(c) ( ((c) & 0xf8) == 0xf0 )
570 #define IS_UTF8_3BYTE(c) ( ((c) & 0xf0) == 0xe0 )
571 #define IS_UTF8_2BYTE(c) ( ((c) & 0xe0) == 0xc0 )
572 #define IS_UTF8_TRAIL(c) ( ((c) & 0xc0) == 0x80 )
582 #define STOREC(c,a) \
583 if ((c) == '\t') STORE_TAB((a),pos); else STORE_CHAR((c),(a),pos)
590 STORE_CHAR(c
, AT_NORMAL
, pos
);
593 goto do_control_char
;
601 } else if (overstrike
)
604 * Overstrike the character at the current position
605 * in the line buffer. This will cause either
606 * underline (if a "_" is overstruck),
607 * bold (if an identical character is overstruck),
608 * or just deletion of the character in the buffer.
611 if (utf_mode
&& IS_UTF8_4BYTE(c
) && curr
> 2 && (char)c
== linebuf
[curr
-3])
616 STORE_CHAR(linebuf
[curr
], AT_BOLD
, pos
);
618 } else if (utf_mode
&& (IS_UTF8_3BYTE(c
) || (overstrike
==2 && IS_UTF8_TRAIL(c
))) && curr
> 1 && (char)c
== linebuf
[curr
-2])
622 STORE_CHAR(linebuf
[curr
], AT_BOLD
, pos
);
624 } else if (utf_mode
&& curr
> 0 && (IS_UTF8_2BYTE(c
) || (overstrike
==1 && IS_UTF8_TRAIL(c
))) && (char)c
== linebuf
[curr
-1])
627 STORE_CHAR(linebuf
[curr
], AT_BOLD
, pos
);
629 } else if (utf_mode
&& curr
> 0 && IS_UTF8_TRAIL(c
) && attr
[curr
-1] == AT_UNDERLINE
)
631 STOREC(c
, AT_UNDERLINE
);
632 } else if ((char)c
== linebuf
[curr
])
635 * Overstriking a char with itself means make it bold.
636 * But overstriking an underscore with itself is
637 * ambiguous. It could mean make it bold, or
638 * it could mean make it underlined.
639 * Use the previous overstrike to resolve it.
641 if (c
== '_' && last_overstrike
!= AT_NORMAL
)
642 STOREC(c
, last_overstrike
);
650 for (i
= 0; i
< 5; i
++)
652 if (curr
<= i
|| !IS_CONT(linebuf
[curr
-i
]))
654 attr
[curr
-i
-1] = AT_UNDERLINE
;
657 STOREC(linebuf
[curr
], AT_UNDERLINE
);
658 } else if (linebuf
[curr
] == '_')
662 if (IS_UTF8_2BYTE(c
))
664 else if (IS_UTF8_3BYTE(c
))
666 else if (IS_UTF8_4BYTE(c
))
669 STOREC(c
, AT_UNDERLINE
);
670 } else if (control_char(c
))
671 goto do_control_char
;
673 STOREC(c
, AT_NORMAL
);
674 } else if (c
== '\t')
677 * Expand a tab into spaces.
682 goto do_control_char
;
685 STORE_TAB(AT_NORMAL
, pos
);
688 } else if (control_char(c
))
691 if (ctldisp
== OPT_ON
|| (ctldisp
== OPT_ONPLUS
&& c
== ESC
))
694 * Output as a normal character.
696 STORE_CHAR(c
, AT_NORMAL
, pos
);
700 * Convert to printable representation.
706 * Make sure we can get the entire representation
707 * of the character on this line.
709 if (column
+ (int) strlen(s
) +
710 attr_swidth(a
) + attr_ewidth(a
) > sc_width
)
713 for ( ; *s
!= 0; s
++)
714 STORE_CHAR(*s
, a
, pos
);
718 STOREC(c
, AT_NORMAL
);
725 * Terminate the line in the line buffer.
731 if (pendc
&& (pendc
!= '\r' || !endline
))
733 * If we had a pending character, put it in the buffer.
734 * But discard a pending CR if we are at end of line
735 * (that is, discard the CR in a CR/LF sequence).
737 (void) do_append(pendc
, pendpos
);
740 * Make sure we've shifted the line, if we need to.
743 pshift(hshift
- cshift
);
746 * Add a newline if necessary,
747 * and append a '\0' to the end of the line.
749 if (column
< sc_width
|| !auto_wrap
|| ignaw
|| ctldisp
== OPT_ON
)
751 linebuf
[curr
] = '\n';
752 attr
[curr
] = AT_NORMAL
;
755 linebuf
[curr
] = '\0';
756 attr
[curr
] = AT_NORMAL
;
759 if (status_col
&& hilites
> 0)
762 attr
[0] = AT_STANDOUT
;
766 * If we are done with this line, reset the current shift.
773 * Get a character from the current line.
774 * Return the character as the function return value,
775 * and the character attribute in *ap.
787 * If there is no current line, we pretend the line is
788 * either "~" or "", depending on the "twiddle" flag.
791 s
= (twiddle
) ? "~\n" : "\n";
796 return (linebuf
[i
] & 0377);
800 * Indicate that there is no current line.
810 * Analogous to forw_line(), but deals with "raw lines":
811 * lines which are not split for screen width.
812 * {{ This is supposed to be more efficient than forw_line(). }}
815 forw_raw_line(curr_pos
, linep
)
823 if (curr_pos
== NULL_POSITION
|| ch_seek(curr_pos
) ||
824 (c
= ch_forw_get()) == EOI
)
825 return (NULL_POSITION
);
830 if (c
== '\n' || c
== EOI
)
835 if (n
>= size_linebuf
-1)
837 if (expand_linebuf())
840 * Overflowed the input buffer.
841 * Pretend the line ended here.
843 new_pos
= ch_tell() - 1;
857 * Analogous to back_line(), but deals with "raw lines".
858 * {{ This is supposed to be more efficient than back_line(). }}
861 back_raw_line(curr_pos
, linep
)
869 if (curr_pos
== NULL_POSITION
|| curr_pos
<= ch_zero() ||
871 return (NULL_POSITION
);
881 * This is the newline ending the previous line.
882 * We have hit the beginning of the line.
884 new_pos
= ch_tell() + 1;
890 * We have hit the beginning of the file.
891 * This must be the first line in the file.
892 * This must, of course, be the beginning of the line.
899 int old_size_linebuf
= size_linebuf
;
902 if (expand_linebuf())
905 * Overflowed the input buffer.
906 * Pretend the line ended here.
908 new_pos
= ch_tell() + 1;
912 * Shift the data to the end of the new linebuf.
914 for (fm
= linebuf
+ old_size_linebuf
,
915 to
= linebuf
+ size_linebuf
;
916 fm
>= linebuf
; fm
--, to
--)
918 n
= size_linebuf
- old_size_linebuf
;
923 *linep
= &linebuf
[n
];