3 * Copyright (C) 1984-2002 Mark Nudelman
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
8 * For more information about less, or for information on how to
9 * contact the author, see the README file.
14 * Routines to manipulate the "line buffer".
15 * The line buffer holds a line of output as it is being built
16 * in preparation for output to the screen.
21 #define IS_CONT(c) (((c) & 0xC0) == 0x80)
23 public char *linebuf = NULL; /* Buffer which holds the current output line */
24 static char *attr = NULL; /* Extension of linebuf to hold attributes */
25 public int size_linebuf = 0; /* Size of line buffer (and attr buffer) */
27 public int cshift; /* Current left-shift of output line buffer */
28 public int hshift; /* Desired left-shift of output line buffer */
29 public int tabstops[TABSTOP_MAX] = { 0 }; /* Custom tabstops */
30 public int ntabstops = 1; /* Number of tabstops */
31 public int tabdefault = 8; /* Default repeated tabstops */
33 static int curr; /* Index into linebuf */
34 static int column; /* Printable length, accounting for
36 static int overstrike; /* Next char should overstrike previous char */
37 static int last_overstrike = AT_NORMAL;
38 static int is_null_line; /* There is no current line */
39 static int lmargin; /* Left margin */
40 static int hilites; /* Number of hilites in this line */
42 static POSITION pendpos;
43 static char *end_ansi_chars;
45 static int do_append();
52 extern int status_col;
53 extern int auto_wrap, ignaw;
54 extern int bo_s_width, bo_e_width;
55 extern int ul_s_width, ul_e_width;
56 extern int bl_s_width, bl_e_width;
57 extern int so_s_width, so_e_width;
58 extern int sc_width, sc_height;
60 extern POSITION start_attnpos;
61 extern POSITION end_attnpos;
64 * Initialize from environment variables.
69 end_ansi_chars = lgetenv("LESSANSIENDCHARS");
70 if (end_ansi_chars == NULL || *end_ansi_chars == '\0')
72 linebuf = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
73 attr = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
74 size_linebuf = LINEBUF_SIZE;
78 * Expand the line buffer.
83 int new_size = size_linebuf + LINEBUF_SIZE;
84 char *new_buf = (char *) calloc(new_size, sizeof(char));
85 char *new_attr = (char *) calloc(new_size, sizeof(char));
86 if (new_buf == NULL || new_attr == NULL)
94 memcpy(new_buf, linebuf, size_linebuf * sizeof(char));
95 memcpy(new_attr, attr, size_linebuf * sizeof(char));
100 size_linebuf = new_size;
105 * Rewind the line buffer.
124 * Insert the line number (of the given position) into the line buffer.
130 register LINENUM linenum = 0;
133 if (linenums == OPT_ONPLUS)
136 * Get the line number and put it in the current line.
137 * {{ Note: since find_linenum calls forw_raw_line,
138 * it may seek in the input file, requiring the caller
139 * of plinenum to re-seek if necessary. }}
140 * {{ Since forw_raw_line modifies linebuf, we must
141 * do this first, before storing anything in linebuf. }}
143 linenum = find_linenum(pos);
147 * Display a status column if the -J option is set.
152 if (start_attnpos != NULL_POSITION &&
153 pos >= start_attnpos && pos < end_attnpos)
154 attr[curr] = AT_STANDOUT;
161 * Display the line number at the start of each line
162 * if the -N option is set.
164 if (linenums == OPT_ONPLUS)
166 char buf[INT_STRLEN_BOUND(pos) + 2];
169 linenumtoa(linenum, buf);
171 if (n < MIN_LINENUM_WIDTH)
172 n = MIN_LINENUM_WIDTH;
173 sprintf(linebuf+curr, "%*s ", n, buf);
174 n++; /* One space after the line number. */
175 for (i = 0; i < n; i++)
176 attr[curr+i] = AT_NORMAL;
183 * Append enough spaces to bring us to the lmargin.
185 while (column < lmargin)
188 attr[curr++] = AT_NORMAL;
194 * Determine how many characters are required to shift N columns.
204 * Each char counts for one column, except ANSI color escape
205 * sequences use no columns since they don't move the cursor.
207 while (*p != '\0' && len > 0)
216 if (is_ansi_end(*p++))
225 * Determine how many characters are required to shift N columns (UTF version).
226 * {{ FIXME: what about color escape sequences in UTF mode? }}
229 utf_shift_chars(s, len)
235 while (*s != '\0' && len > 0)
251 * Shift the input line left.
252 * This means discarding N printable chars at the start of the buffer.
261 if (shift > column - lmargin)
262 shift = column - lmargin;
263 if (shift > curr - lmargin)
264 shift = curr - lmargin;
267 nchars = utf_shift_chars(linebuf + lmargin, shift);
269 nchars = shift_chars(linebuf + lmargin, shift);
272 for (i = 0; i < curr - nchars; i++)
274 linebuf[lmargin + i] = linebuf[lmargin + i + nchars];
275 attr[lmargin + i] = attr[lmargin + i + nchars];
283 * Return the printing width of the start (enter) sequence
284 * for a given character attribute.
292 case AT_BOLD: return (bo_s_width);
293 case AT_UNDERLINE: return (ul_s_width);
294 case AT_BLINK: return (bl_s_width);
295 case AT_STANDOUT: return (so_s_width);
301 * Return the printing width of the end (exit) sequence
302 * for a given character attribute.
310 case AT_BOLD: return (bo_e_width);
311 case AT_UNDERLINE: return (ul_e_width);
312 case AT_BLINK: return (bl_e_width);
313 case AT_STANDOUT: return (so_e_width);
319 * Return the printing width of a given character and attribute,
320 * if the character were added to the current position in the line buffer.
321 * Adding a character with a given attribute may cause an enter or exit
322 * attribute sequence to be inserted, so this must be taken into account.
331 if (utf_mode && IS_CONT(c))
336 * Backspace moves backwards one position.
342 * Control characters do unpredicatable things,
343 * so we don't even try to guess; say it doesn't move.
344 * This can only happen if the -r flag is in effect.
349 * Other characters take one space,
350 * plus the width of any attribute enter/exit sequence.
353 if (curr > 0 && attr[curr-1] != a)
354 w += attr_ewidth(attr[curr-1]);
355 if (a && (curr == 0 || attr[curr-1] != a))
361 * Delete the previous character in the line buffer.
367 column -= pwidth(linebuf[curr], attr[curr]);
371 * Are we currently within a recognized ANSI escape sequence?
379 * Search backwards for either an ESC (which means we ARE in a seq);
380 * or an end char (which means we're NOT in a seq).
382 for (i = curr-1; i >= 0; i--)
384 if (linebuf[i] == ESC)
386 if (is_ansi_end(linebuf[i]))
393 * Is a character the end of an ANSI escape sequence?
399 return (strchr(end_ansi_chars, c) != NULL);
403 * Append a character and attribute to the line buffer.
405 #define STORE_CHAR(c,a,pos) \
406 do { if (store_char((c),(a),(pos))) return (1); else curr++; } while (0)
409 store_char(c, a, pos)
419 if (is_hilited(pos, pos+1, 0))
422 * This character should be highlighted.
423 * Override the attribute passed in.
429 if (ctldisp == OPT_ONPLUS && in_ansi_esc_seq())
433 if (ctldisp != OPT_ON && column + w + attr_ewidth(a) > sc_width)
435 * Won't fit on screen.
439 if (curr >= size_linebuf-2)
442 * Won't fit in line buffer.
445 if (expand_linebuf())
450 * Special handling for "magic cookie" terminals.
451 * If an attribute enter/exit sequence has a printing width > 0,
452 * and the sequence is adjacent to a space, delete the space.
453 * We just mark the space as invisible, to avoid having too
454 * many spaces deleted.
455 * {{ Note that even if the attribute width is > 1, we
456 * delete only one space. It's not worth trying to do more.
457 * It's hardly worth doing this much. }}
459 if (curr > 0 && a != AT_NORMAL &&
460 linebuf[curr-1] == ' ' && attr[curr-1] == AT_NORMAL &&
464 * We are about to append an enter-attribute sequence
465 * just after a space. Delete the space.
467 attr[curr-1] = AT_INVIS;
469 } else if (curr > 0 && attr[curr-1] != AT_NORMAL &&
470 attr[curr-1] != AT_INVIS && c == ' ' && a == AT_NORMAL &&
471 attr_ewidth(attr[curr-1]) > 0)
474 * We are about to append a space just after an
475 * exit-attribute sequence. Delete the space.
480 /* End of magic cookie handling. */
489 * Append a tab to the line buffer.
490 * Store spaces to represent the tab.
492 #define STORE_TAB(a,pos) \
493 do { if (store_tab((a),(pos))) return (1); } while (0)
500 int to_tab = column + cshift - lmargin;
503 if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1])
504 to_tab = tabdefault -
505 ((to_tab - tabstops[ntabstops-1]) % tabdefault);
508 for (i = ntabstops - 2; i >= 0; i--)
509 if (to_tab >= tabstops[i])
511 to_tab = tabstops[i+1] - to_tab;
515 STORE_CHAR(' ', attr, pos);
516 } while (--to_tab > 0);
521 * Append a character to the line buffer.
522 * Expand tabs into spaces, handle underlining, boldfacing, etc.
523 * Returns 0 if ok, 1 if couldn't fit in buffer.
534 if (do_append(pendc, pendpos))
536 * Oops. We've probably lost the char which
537 * was in pendc, since caller won't back up.
543 if (c == '\r' && bs_mode == BS_SPECIAL)
546 * Don't put the CR into the buffer until we see
547 * the next char. If the next char is a newline,
555 r = do_append(c, pos);
557 * If we need to shift the line, do it.
558 * But wait until we get to at least the middle of the screen,
559 * so shifting it doesn't affect the chars we're currently
560 * pappending. (Bold & underline can get messed up otherwise.)
562 if (cshift < hshift && column > sc_width / 2)
564 linebuf[curr] = '\0';
565 pshift(hshift - cshift);
570 #define IS_UTF8_4BYTE(c) ( ((c) & 0xf8) == 0xf0 )
571 #define IS_UTF8_3BYTE(c) ( ((c) & 0xf0) == 0xe0 )
572 #define IS_UTF8_2BYTE(c) ( ((c) & 0xe0) == 0xc0 )
573 #define IS_UTF8_TRAIL(c) ( ((c) & 0xc0) == 0x80 )
583 #define STOREC(c,a) \
584 if ((c) == '\t') STORE_TAB((a),pos); else STORE_CHAR((c),(a),pos)
591 STORE_CHAR(c, AT_NORMAL, pos);
594 goto do_control_char;
602 } else if (overstrike)
605 * Overstrike the character at the current position
606 * in the line buffer. This will cause either
607 * underline (if a "_" is overstruck),
608 * bold (if an identical character is overstruck),
609 * or just deletion of the character in the buffer.
612 if (utf_mode && IS_UTF8_4BYTE(c) && curr > 2 && (char)c == linebuf[curr-3])
617 STORE_CHAR(linebuf[curr], AT_BOLD, pos);
619 } else if (utf_mode && (IS_UTF8_3BYTE(c) || (overstrike==2 && IS_UTF8_TRAIL(c))) && curr > 1 && (char)c == linebuf[curr-2])
623 STORE_CHAR(linebuf[curr], AT_BOLD, pos);
625 } else if (utf_mode && curr > 0 && (IS_UTF8_2BYTE(c) || (overstrike==1 && IS_UTF8_TRAIL(c))) && (char)c == linebuf[curr-1])
628 STORE_CHAR(linebuf[curr], AT_BOLD, pos);
630 } else if (utf_mode && curr > 0 && IS_UTF8_TRAIL(c) && attr[curr-1] == AT_UNDERLINE)
632 STOREC(c, AT_UNDERLINE);
633 } else if ((char)c == linebuf[curr])
636 * Overstriking a char with itself means make it bold.
637 * But overstriking an underscore with itself is
638 * ambiguous. It could mean make it bold, or
639 * it could mean make it underlined.
640 * Use the previous overstrike to resolve it.
642 if (c == '_' && last_overstrike != AT_NORMAL)
643 STOREC(c, last_overstrike);
651 for (i = 0; i < 5; i++)
653 if (curr <= i || !IS_CONT(linebuf[curr-i]))
655 attr[curr-i-1] = AT_UNDERLINE;
658 STOREC(linebuf[curr], AT_UNDERLINE);
659 } else if (linebuf[curr] == '_')
663 if (IS_UTF8_2BYTE(c))
665 else if (IS_UTF8_3BYTE(c))
667 else if (IS_UTF8_4BYTE(c))
670 STOREC(c, AT_UNDERLINE);
671 } else if (control_char(c))
672 goto do_control_char;
674 STOREC(c, AT_NORMAL);
675 } else if (c == '\t')
678 * Expand a tab into spaces.
683 goto do_control_char;
686 STORE_TAB(AT_NORMAL, pos);
689 } else if (control_char(c))
692 if (ctldisp == OPT_ON || (ctldisp == OPT_ONPLUS && c == ESC))
695 * Output as a normal character.
697 STORE_CHAR(c, AT_NORMAL, pos);
701 * Convert to printable representation.
707 * Make sure we can get the entire representation
708 * of the character on this line.
710 if (column + (int) strlen(s) +
711 attr_swidth(a) + attr_ewidth(a) > sc_width)
714 for ( ; *s != 0; s++)
715 STORE_CHAR(*s, a, pos);
719 STOREC(c, AT_NORMAL);
726 * Terminate the line in the line buffer.
732 if (pendc && (pendc != '\r' || !endline))
734 * If we had a pending character, put it in the buffer.
735 * But discard a pending CR if we are at end of line
736 * (that is, discard the CR in a CR/LF sequence).
738 (void) do_append(pendc, pendpos);
741 * Make sure we've shifted the line, if we need to.
744 pshift(hshift - cshift);
747 * Add a newline if necessary,
748 * and append a '\0' to the end of the line.
750 if (column < sc_width || !auto_wrap || ignaw || ctldisp == OPT_ON)
752 linebuf[curr] = '\n';
753 attr[curr] = AT_NORMAL;
756 linebuf[curr] = '\0';
757 attr[curr] = AT_NORMAL;
760 if (status_col && hilites > 0)
763 attr[0] = AT_STANDOUT;
767 * If we are done with this line, reset the current shift.
774 * Get a character from the current line.
775 * Return the character as the function return value,
776 * and the character attribute in *ap.
788 * If there is no current line, we pretend the line is
789 * either "~" or "", depending on the "twiddle" flag.
792 s = (twiddle) ? "~\n" : "\n";
797 return (linebuf[i] & 0377);
801 * Indicate that there is no current line.
811 * Analogous to forw_line(), but deals with "raw lines":
812 * lines which are not split for screen width.
813 * {{ This is supposed to be more efficient than forw_line(). }}
816 forw_raw_line(curr_pos, linep)
824 if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
825 (c = ch_forw_get()) == EOI)
826 return (NULL_POSITION);
831 if (c == '\n' || c == EOI)
836 if (n >= size_linebuf-1)
838 if (expand_linebuf())
841 * Overflowed the input buffer.
842 * Pretend the line ended here.
844 new_pos = ch_tell() - 1;
858 * Analogous to back_line(), but deals with "raw lines".
859 * {{ This is supposed to be more efficient than back_line(). }}
862 back_raw_line(curr_pos, linep)
870 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
872 return (NULL_POSITION);
882 * This is the newline ending the previous line.
883 * We have hit the beginning of the line.
885 new_pos = ch_tell() + 1;
891 * We have hit the beginning of the file.
892 * This must be the first line in the file.
893 * This must, of course, be the beginning of the line.
900 int old_size_linebuf = size_linebuf;
903 if (expand_linebuf())
906 * Overflowed the input buffer.
907 * Pretend the line ended here.
909 new_pos = ch_tell() + 1;
913 * Shift the data to the end of the new linebuf.
915 for (fm = linebuf + old_size_linebuf - 1,
916 to = linebuf + size_linebuf - 1;
917 fm >= linebuf; fm--, to--)
919 n = size_linebuf - old_size_linebuf;
924 *linep = &linebuf[n];