2 * Copyright (C) 1984-2000 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)
21 #define LINENUM_WIDTH 8 /* Chars to use for line number */
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 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));
97 size_linebuf = new_size;
102 * Rewind the line buffer.
118 if (linenums == OPT_ONPLUS)
119 lmargin += LINENUM_WIDTH+1;
123 * Insert the line number (of the given position) into the line buffer.
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 lno = 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 sprintf(&linebuf[curr], "%*d", LINENUM_WIDTH, lno);
166 column += LINENUM_WIDTH;
167 for (i = 0; i < LINENUM_WIDTH; i++)
171 * Append enough spaces to bring us to the lmargin.
173 while (column < lmargin)
176 attr[curr++] = AT_NORMAL;
182 * Determine how many characters are required to shift N columns.
192 * Each char counts for one column, except ANSI color escape
193 * sequences use no columns since they don't move the cursor.
195 while (*p != '\0' && len > 0)
204 if (is_ansi_end(*p++))
213 * Determine how many characters are required to shift N columns (UTF version).
214 * {{ FIXME: what about color escape sequences in UTF mode? }}
217 utf_shift_chars(s, len)
223 while (*s != '\0' && len > 0)
239 * Shift the input line left.
240 * This means discarding N printable chars at the start of the buffer.
249 if (shift > column - lmargin)
250 shift = column - lmargin;
251 if (shift > curr - lmargin)
252 shift = curr - lmargin;
255 nchars = utf_shift_chars(linebuf + lmargin, shift);
257 nchars = shift_chars(linebuf + lmargin, shift);
260 for (i = 0; i < curr - nchars; i++)
262 linebuf[lmargin + i] = linebuf[lmargin + i + nchars];
263 attr[lmargin + i] = attr[lmargin + i + nchars];
271 * Return the printing width of the start (enter) sequence
272 * for a given character attribute.
280 case AT_BOLD: return (bo_s_width);
281 case AT_UNDERLINE: return (ul_s_width);
282 case AT_BLINK: return (bl_s_width);
283 case AT_STANDOUT: return (so_s_width);
289 * Return the printing width of the end (exit) sequence
290 * for a given character attribute.
298 case AT_BOLD: return (bo_e_width);
299 case AT_UNDERLINE: return (ul_e_width);
300 case AT_BLINK: return (bl_e_width);
301 case AT_STANDOUT: return (so_e_width);
307 * Return the printing width of a given character and attribute,
308 * if the character were added to the current position in the line buffer.
309 * Adding a character with a given attribute may cause an enter or exit
310 * attribute sequence to be inserted, so this must be taken into account.
319 if (utf_mode && IS_CONT(c))
324 * Backspace moves backwards one position.
330 * Control characters do unpredicatable things,
331 * so we don't even try to guess; say it doesn't move.
332 * This can only happen if the -r flag is in effect.
337 * Other characters take one space,
338 * plus the width of any attribute enter/exit sequence.
341 if (curr > 0 && attr[curr-1] != a)
342 w += attr_ewidth(attr[curr-1]);
343 if (a && (curr == 0 || attr[curr-1] != a))
349 * Delete the previous character in the line buffer.
355 column -= pwidth(linebuf[curr], attr[curr]);
359 * Are we currently within a recognized ANSI escape sequence?
367 * Search backwards for either an ESC (which means we ARE in a seq);
368 * or an end char (which means we're NOT in a seq).
370 for (i = curr-1; i >= 0; i--)
372 if (linebuf[i] == ESC)
374 if (is_ansi_end(linebuf[i]))
381 * Is a character the end of an ANSI escape sequence?
387 return (strchr(end_ansi_chars, c) != NULL);
391 * Append a character and attribute to the line buffer.
393 #define STORE_CHAR(c,a,pos) \
394 do { if (store_char((c),(a),(pos))) return (1); else curr++; } while (0)
397 store_char(c, a, pos)
405 if (is_hilited(pos, pos+1, 0))
408 * This character should be highlighted.
409 * Override the attribute passed in.
415 if (ctldisp == OPT_ONPLUS && in_ansi_esc_seq())
419 if (ctldisp != OPT_ON && column + w + attr_ewidth(a) > sc_width)
421 * Won't fit on screen.
425 if (curr >= size_linebuf-2)
428 * Won't fit in line buffer.
431 if (expand_linebuf())
436 * Special handling for "magic cookie" terminals.
437 * If an attribute enter/exit sequence has a printing width > 0,
438 * and the sequence is adjacent to a space, delete the space.
439 * We just mark the space as invisible, to avoid having too
440 * many spaces deleted.
441 * {{ Note that even if the attribute width is > 1, we
442 * delete only one space. It's not worth trying to do more.
443 * It's hardly worth doing this much. }}
445 if (curr > 0 && a != AT_NORMAL &&
446 linebuf[curr-1] == ' ' && attr[curr-1] == AT_NORMAL &&
450 * We are about to append an enter-attribute sequence
451 * just after a space. Delete the space.
453 attr[curr-1] = AT_INVIS;
455 } else if (curr > 0 && attr[curr-1] != AT_NORMAL &&
456 attr[curr-1] != AT_INVIS && c == ' ' && a == AT_NORMAL &&
457 attr_ewidth(attr[curr-1]) > 0)
460 * We are about to append a space just after an
461 * exit-attribute sequence. Delete the space.
466 /* End of magic cookie handling. */
475 * Append a tab to the line buffer.
476 * Store spaces to represent the tab.
478 #define STORE_TAB(a,pos) \
479 do { if (store_tab((a),(pos))) return (1); } while (0)
486 int to_tab = column + cshift - lmargin;
489 if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1])
490 to_tab = tabdefault -
491 ((to_tab - tabstops[ntabstops-1]) % tabdefault);
494 for (i = ntabstops - 2; i >= 0; i--)
495 if (to_tab >= tabstops[i])
497 to_tab = tabstops[i+1] - to_tab;
501 STORE_CHAR(' ', attr, pos);
502 } while (--to_tab > 0);
507 * Append a character to the line buffer.
508 * Expand tabs into spaces, handle underlining, boldfacing, etc.
509 * Returns 0 if ok, 1 if couldn't fit in buffer.
520 if (do_append(pendc, pendpos))
522 * Oops. We've probably lost the char which
523 * was in pendc, since caller won't back up.
529 if (c == '\r' && bs_mode == BS_SPECIAL)
532 * Don't put the CR into the buffer until we see
533 * the next char. If the next char is a newline,
541 r = do_append(c, pos);
543 * If we need to shift the line, do it.
544 * But wait until we get to at least the middle of the screen,
545 * so shifting it doesn't affect the chars we're currently
546 * pappending. (Bold & underline can get messed up otherwise.)
548 if (cshift < hshift && column > sc_width / 2)
550 linebuf[curr] = '\0';
551 pshift(hshift - cshift);
564 #define STOREC(c,a) \
565 if ((c) == '\t') STORE_TAB((a),pos); else STORE_CHAR((c),(a),pos)
572 STORE_CHAR(c, AT_NORMAL, pos);
575 goto do_control_char;
583 } else if (overstrike)
586 * Overstrike the character at the current position
587 * in the line buffer. This will cause either
588 * underline (if a "_" is overstruck),
589 * bold (if an identical character is overstruck),
590 * or just deletion of the character in the buffer.
593 if (utf_mode && curr > 1 && (char)c == linebuf[curr-2])
598 } else if (utf_mode && curr > 0 && (char)c == linebuf[curr-1])
601 STORE_CHAR(linebuf[curr], AT_BOLD, pos);
603 } else if ((char)c == linebuf[curr])
610 if (curr > 0 && IS_CONT(linebuf[curr]))
611 attr[curr-1] = AT_UNDERLINE;
612 if (curr > 1 && IS_CONT(linebuf[curr-1]))
613 attr[curr-2] = AT_UNDERLINE;
614 if (curr > 2 && IS_CONT(linebuf[curr-2]))
615 attr[curr-3] = AT_UNDERLINE;
616 if (curr > 3 && IS_CONT(linebuf[curr-3]))
617 attr[curr-4] = AT_UNDERLINE;
618 if (curr > 4 && IS_CONT(linebuf[curr-4]))
619 attr[curr-5] = AT_UNDERLINE;
621 STOREC(linebuf[curr], AT_UNDERLINE);
622 } else if (linebuf[curr] == '_')
624 STOREC(c, AT_UNDERLINE);
625 } else if (control_char(c))
626 goto do_control_char;
628 STOREC(c, AT_NORMAL);
629 } else if (c == '\t')
632 * Expand a tab into spaces.
637 goto do_control_char;
640 STORE_TAB(AT_NORMAL, pos);
643 } else if (control_char(c))
646 if (ctldisp == OPT_ON || (ctldisp == OPT_ONPLUS && c == ESC))
649 * Output as a normal character.
651 STORE_CHAR(c, AT_NORMAL, pos);
655 * Convert to printable representation.
661 * Make sure we can get the entire representation
662 * of the character on this line.
664 if (column + (int) strlen(s) +
665 attr_swidth(a) + attr_ewidth(a) > sc_width)
668 for ( ; *s != 0; s++)
669 STORE_CHAR(*s, a, pos);
673 STOREC(c, AT_NORMAL);
680 * Terminate the line in the line buffer.
686 if (pendc && (pendc != '\r' || !endline))
688 * If we had a pending character, put it in the buffer.
689 * But discard a pending CR if we are at end of line
690 * (that is, discard the CR in a CR/LF sequence).
692 (void) do_append(pendc, pendpos);
695 * Make sure we've shifted the line, if we need to.
698 pshift(hshift - cshift);
701 * Add a newline if necessary,
702 * and append a '\0' to the end of the line.
704 if (column < sc_width || !auto_wrap || ignaw || ctldisp == OPT_ON)
706 linebuf[curr] = '\n';
707 attr[curr] = AT_NORMAL;
710 linebuf[curr] = '\0';
711 attr[curr] = AT_NORMAL;
714 if (status_col && hilites > 0)
717 attr[0] = AT_STANDOUT;
721 * If we are done with this line, reset the current shift.
728 * Get a character from the current line.
729 * Return the character as the function return value,
730 * and the character attribute in *ap.
742 * If there is no current line, we pretend the line is
743 * either "~" or "", depending on the "twiddle" flag.
746 s = (twiddle) ? "~\n" : "\n";
751 return (linebuf[i] & 0377);
755 * Indicate that there is no current line.
765 * Analogous to forw_line(), but deals with "raw lines":
766 * lines which are not split for screen width.
767 * {{ This is supposed to be more efficient than forw_line(). }}
770 forw_raw_line(curr_pos, linep)
778 if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
779 (c = ch_forw_get()) == EOI)
780 return (NULL_POSITION);
785 if (c == '\n' || c == EOI)
790 if (n >= size_linebuf-1)
792 if (expand_linebuf())
795 * Overflowed the input buffer.
796 * Pretend the line ended here.
798 new_pos = ch_tell() - 1;
812 * Analogous to back_line(), but deals with "raw lines".
813 * {{ This is supposed to be more efficient than back_line(). }}
816 back_raw_line(curr_pos, linep)
824 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
826 return (NULL_POSITION);
836 * This is the newline ending the previous line.
837 * We have hit the beginning of the line.
839 new_pos = ch_tell() + 1;
845 * We have hit the beginning of the file.
846 * This must be the first line in the file.
847 * This must, of course, be the beginning of the line.
854 int old_size_linebuf = size_linebuf;
857 if (expand_linebuf())
860 * Overflowed the input buffer.
861 * Pretend the line ended here.
863 new_pos = ch_tell() + 1;
867 * Shift the data to the end of the new linebuf.
869 for (fm = linebuf + old_size_linebuf,
870 to = linebuf + size_linebuf;
871 fm >= linebuf; fm--, to--)
873 n = size_linebuf - old_size_linebuf;
878 *linep = &linebuf[n];