2 * Copyright (C) 1984-2022 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, see the README file.
11 * Routines to manipulate the "line buffer".
12 * The line buffer holds a line of output as it is being built
13 * in preparation for output to the screen.
20 #if MSDOS_COMPILER==WIN32C
21 #define WIN32_LEAN_AND_MEAN
25 #define MAX_PFX_WIDTH (MAX_LINENUM_WIDTH + MAX_STATUSCOL_WIDTH + 1)
27 char *buf; /* Buffer which holds the current output line */
28 int *attr; /* Parallel to buf, to hold attributes */
29 int print; /* Index in buf of first printable char */
30 int end; /* Number of chars in buf */
31 char pfx[MAX_PFX_WIDTH]; /* Holds status column and line number */
32 int pfx_attr[MAX_PFX_WIDTH];
33 int pfx_end; /* Number of chars in pfx */
37 * Buffer of ansi sequences which have been shifted off the left edge
40 struct xbuffer shifted_ansi;
43 * Ring buffer of last ansi sequences sent.
44 * While sending a line, these will be resent at the end
45 * of any highlighted string, to restore text modes.
46 * {{ Not ideal, since we don't really know how many to resend. }}
48 #define NUM_LAST_ANSIS 3
49 static struct xbuffer last_ansi;
50 static struct xbuffer last_ansis[NUM_LAST_ANSIS];
51 static int curr_last_ansi;
53 public int size_linebuf = 0; /* Size of line buffer (and attr buffer) */
54 static struct ansi_state *line_ansi = NULL;
55 static int ansi_in_line;
56 static int hlink_in_line;
57 static int line_mark_attr;
58 static int cshift; /* Current left-shift of output line buffer */
59 public int hshift; /* Desired left-shift of output line buffer */
60 public int tabstops[TABSTOP_MAX] = { 0 }; /* Custom tabstops */
61 public int ntabstops = 1; /* Number of tabstops */
62 public int tabdefault = 8; /* Default repeated tabstops */
63 public POSITION highest_hilite; /* Pos of last hilite in file found so far */
65 static int end_column; /* Printable length, accounting for backspaces, etc. */
66 static int right_curr;
67 static int right_column;
68 static int overstrike; /* Next char should overstrike previous char */
69 static int last_overstrike = AT_NORMAL;
70 static int is_null_line; /* There is no current line */
72 static POSITION pendpos;
73 static char *end_ansi_chars;
74 static char *mid_ansi_chars;
77 static int attr_swidth LESSPARAMS ((int a));
78 static int attr_ewidth LESSPARAMS ((int a));
79 static int do_append LESSPARAMS ((LWCHAR ch, char *rep, POSITION pos));
87 extern int status_col;
88 extern int status_col_width;
89 extern int linenum_width;
90 extern int auto_wrap, ignaw;
91 extern int bo_s_width, bo_e_width;
92 extern int ul_s_width, ul_e_width;
93 extern int bl_s_width, bl_e_width;
94 extern int so_s_width, so_e_width;
95 extern int sc_width, sc_height;
97 extern POSITION start_attnpos;
98 extern POSITION end_attnpos;
99 extern char rscroll_char;
100 extern int rscroll_attr;
101 extern int use_color;
102 extern int status_line;
104 static char mbc_buf[MAX_UTF_CHAR_LEN];
105 static int mbc_buf_len = 0;
106 static int mbc_buf_index = 0;
107 static POSITION mbc_pos;
109 /* Configurable color map */
110 static char color_map[AT_NUM_COLORS][12] = {
111 "Wm", /* AT_COLOR_ATTN */
112 "kR", /* AT_COLOR_BIN */
113 "kR", /* AT_COLOR_CTRL */
114 "kY", /* AT_COLOR_ERROR */
115 "c", /* AT_COLOR_LINENUM */
116 "Wb", /* AT_COLOR_MARK */
117 "kC", /* AT_COLOR_PROMPT */
118 "kc", /* AT_COLOR_RSCROLL */
119 "kG", /* AT_COLOR_SEARCH */
120 "", /* AT_COLOR_HEADER */
121 "", /* AT_UNDERLINE */
124 "", /* AT_STANDOUT */
127 /* State while processing an ANSI escape sequence */
129 int hindex; /* Index into hyperlink prefix */
130 int hlink; /* Processing hyperlink address? */
131 int prev_esc; /* Prev char was ESC (to detect ESC-\ seq) */
135 * Initialize from environment variables.
138 init_line(VOID_PARAM)
142 end_ansi_chars = lgetenv("LESSANSIENDCHARS");
143 if (isnullenv(end_ansi_chars))
144 end_ansi_chars = "m";
146 mid_ansi_chars = lgetenv("LESSANSIMIDCHARS");
147 if (isnullenv(mid_ansi_chars))
148 mid_ansi_chars = "0123456789:;[?!\"'#%()*+ ";
150 linebuf.buf = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
151 linebuf.attr = (int *) ecalloc(LINEBUF_SIZE, sizeof(int));
152 size_linebuf = LINEBUF_SIZE;
153 xbuf_init(&shifted_ansi);
154 xbuf_init(&last_ansi);
155 for (ax = 0; ax < NUM_LAST_ANSIS; ax++)
156 xbuf_init(&last_ansis[ax]);
161 * Expand the line buffer.
164 expand_linebuf(VOID_PARAM)
166 /* Double the size of the line buffer. */
167 int new_size = size_linebuf * 2;
168 char *new_buf = (char *) calloc(new_size, sizeof(char));
169 int *new_attr = (int *) calloc(new_size, sizeof(int));
170 if (new_buf == NULL || new_attr == NULL)
172 if (new_attr != NULL)
179 * We just calloc'd the buffers; copy the old contents.
181 memcpy(new_buf, linebuf.buf, size_linebuf * sizeof(char));
182 memcpy(new_attr, linebuf.attr, size_linebuf * sizeof(int));
185 linebuf.buf = new_buf;
186 linebuf.attr = new_attr;
187 size_linebuf = new_size;
192 * Is a character ASCII?
207 if (end_column > right_column && w > 0)
209 right_column = end_column;
210 right_curr = linebuf.end;
216 * Rewind the line buffer.
223 linebuf.print = 6; /* big enough for longest UTF-8 sequence */
225 for (linebuf.end = 0; linebuf.end < linebuf.print; linebuf.end++)
227 linebuf.buf[linebuf.end] = '\0';
228 linebuf.attr[linebuf.end] = 0;
236 last_overstrike = AT_NORMAL;
244 xbuf_reset(&shifted_ansi);
245 xbuf_reset(&last_ansi);
246 for (ax = 0; ax < NUM_LAST_ANSIS; ax++)
247 xbuf_reset(&last_ansis[ax]);
252 * Set a character in the line buffer.
255 set_linebuf(n, ch, attr)
261 linebuf.attr[n] = attr;
265 * Append a character to the line buffer.
268 add_linebuf(ch, attr, w)
273 set_linebuf(linebuf.end++, ch, attr);
278 * Append a string to the line buffer.
281 addstr_linebuf(s, attr, cw)
286 for ( ; *s != '\0'; s++)
287 add_linebuf(*s, attr, cw);
291 * Set a character in the line prefix buffer.
300 linebuf.pfx_attr[n] = attr;
304 * Append a character to the line prefix buffer.
311 set_pfx(linebuf.pfx_end++, ch, attr);
315 * Insert the status column and line number into the line buffer.
324 if (linenums == OPT_ONPLUS)
327 * Get the line number and put it in the current line.
328 * {{ Note: since find_linenum calls forw_raw_line,
329 * it may seek in the input file, requiring the caller
330 * of plinestart to re-seek if necessary. }}
331 * {{ Since forw_raw_line modifies linebuf, we must
332 * do this first, before storing anything in linebuf. }}
334 linenum = find_linenum(pos);
338 * Display a status column if the -J option is set.
340 if (status_col || status_line)
342 char c = posmark(pos);
344 line_mark_attr = AT_HILITE|AT_COLOR_MARK;
345 else if (start_attnpos != NULL_POSITION &&
346 pos >= start_attnpos && pos <= end_attnpos)
347 line_mark_attr = AT_HILITE|AT_COLOR_ATTN;
350 add_pfx(c ? c : ' ', line_mark_attr); /* column 0: status */
351 while (linebuf.pfx_end < status_col_width)
352 add_pfx(' ', AT_NORMAL);
357 * Display the line number at the start of each line
358 * if the -N option is set.
360 if (linenums == OPT_ONPLUS)
362 char buf[INT_STRLEN_BOUND(linenum) + 2];
365 linenum = vlinenum(linenum);
370 linenumtoa(linenum, buf);
371 len = (int) strlen(buf);
373 for (i = 0; i < linenum_width - len; i++)
374 add_pfx(' ', AT_NORMAL);
375 for (i = 0; i < len; i++)
376 add_pfx(buf[i], AT_BOLD|AT_COLOR_LINENUM);
377 add_pfx(' ', AT_NORMAL);
379 end_column = linebuf.pfx_end;
383 * Return the width of the line prefix (status column and line number).
384 * {{ Actual line number can be wider than linenum_width. }}
387 line_pfx_width(VOID_PARAM)
391 width += status_col_width;
392 if (linenums == OPT_ONPLUS)
393 width += linenum_width + 1;
398 * Shift line left so that the last char is just to the left
399 * of the first visible column.
402 pshift_all(VOID_PARAM)
405 for (i = linebuf.print; i < linebuf.end; i++)
406 if (linebuf.attr[i] == AT_ANSI)
407 xbuf_add(&shifted_ansi, linebuf.buf[i]);
408 linebuf.end = linebuf.print;
409 end_column = linebuf.pfx_end;
413 * Return the printing width of the start (enter) sequence
414 * for a given character attribute.
422 a = apply_at_specials(a);
424 if (a & AT_UNDERLINE)
437 * Return the printing width of the end (exit) sequence
438 * for a given character attribute.
446 a = apply_at_specials(a);
448 if (a & AT_UNDERLINE)
461 * Return the printing width of a given character and attribute,
462 * if the character were added after prev_ch.
463 * Adding a character with a given attribute may cause an enter or exit
464 * attribute sequence to be inserted, so this must be taken into account.
467 pwidth(ch, a, prev_ch, prev_a)
478 * Backspace moves backwards one or two positions.
480 if (prev_a & (AT_ANSI|AT_BINARY))
481 return strlen(prchar('\b'));
482 return (utf_mode && is_wide_char(prev_ch)) ? -2 : -1;
485 if (!utf_mode || is_ascii_char(ch))
487 if (control_char((char)ch))
490 * Control characters do unpredictable things,
491 * so we don't even try to guess; say it doesn't move.
492 * This can only happen if the -r flag is in effect.
498 if (is_composing_char(ch) || is_combining_char(prev_ch, ch))
501 * Composing and combining chars take up no space.
503 * Some terminals, upon failure to compose a
504 * composing character with the character(s) that
505 * precede(s) it will actually take up one end_column
506 * for the composing character; there isn't much
507 * we could do short of testing the (complex)
508 * composition process ourselves and printing
509 * a binary representation when it fails.
516 * Other characters take one or two columns,
517 * plus the width of any attribute enter/exit sequence.
520 if (is_wide_char(ch))
522 if (linebuf.end > 0 && !is_at_equiv(linebuf.attr[linebuf.end-1], a))
523 w += attr_ewidth(linebuf.attr[linebuf.end-1]);
524 if (apply_at_specials(a) != AT_NORMAL &&
525 (linebuf.end == 0 || !is_at_equiv(linebuf.attr[linebuf.end-1], a)))
531 * Delete to the previous base character in the line buffer.
539 if (linebuf.end == 0)
541 p = &linebuf.buf[linebuf.end];
542 ch = step_char(&p, -1, linebuf.buf);
543 /* Skip back to the next nonzero-width char. */
544 while (p > linebuf.buf)
548 linebuf.end = (int) (p - linebuf.buf);
549 prev_ch = step_char(&p, -1, linebuf.buf);
550 width = pwidth(ch, linebuf.attr[linebuf.end], prev_ch, linebuf.attr[linebuf.end-1]);
552 /* {{ right_column? }} */
561 * Is a character the end of an ANSI escape sequence?
567 if (!is_ascii_char(ch))
569 return (strchr(end_ansi_chars, (char) ch) != NULL);
573 * Can a char appear in an ANSI escape sequence, before the end char?
579 if (!is_ascii_char(ch))
583 return (strchr(mid_ansi_chars, (char) ch) != NULL);
587 * Skip past an ANSI escape sequence.
588 * pp is initially positioned just after the CSI_START char.
591 skip_ansi(pansi, pp, limit)
592 struct ansi_state *pansi;
594 constant char *limit;
598 c = step_char(pp, +1, limit);
599 } while (*pp < limit && ansi_step(pansi, c) == ANSI_MID);
600 /* Note that we discard final char, for which is_ansi_end is true. */
604 * Determine if a character starts an ANSI escape sequence.
605 * If so, return an ansi_state struct; otherwise return NULL.
607 public struct ansi_state *
611 struct ansi_state *pansi;
613 if (!IS_CSI_START(ch))
615 pansi = ecalloc(1, sizeof(struct ansi_state));
623 * Determine whether the next char in an ANSI escape sequence
628 struct ansi_state *pansi;
633 /* Hyperlink ends with \7 or ESC-backslash. */
636 if (pansi->prev_esc && ch == '\\')
638 pansi->prev_esc = (ch == ESC);
641 if (pansi->hindex >= 0)
643 static char hlink_prefix[] = ESCS "]8;";
644 if (ch == hlink_prefix[pansi->hindex] ||
645 (pansi->hindex == 0 && IS_CSI_START(ch)))
648 if (hlink_prefix[pansi->hindex] == '\0')
649 pansi->hlink = 1; /* now processing hyperlink addr */
652 pansi->hindex = -1; /* not a hyperlink */
654 /* Check for SGR sequences */
655 if (is_ansi_middle(ch))
663 * Free an ansi_state structure.
667 struct ansi_state *pansi;
673 * Will w characters in attribute a fit on the screen?
680 if (ctldisp == OPT_ON)
681 /* We're not counting, so say that everything fits. */
683 return (end_column - cshift + w + attr_ewidth(a) <= sc_width);
687 * Append a character and attribute to the line buffer.
689 #define STORE_CHAR(ch,a,rep,pos) \
691 if (store_char((ch),(a),(rep),(pos))) return (1); \
695 store_char(ch, a, rep, pos)
706 i = (a & (AT_UNDERLINE|AT_BOLD));
716 if (pos == NULL_POSITION)
718 /* Color the prompt unless it has ansi sequences in it. */
719 hl_attr = ansi_in_line ? 0 : AT_STANDOUT|AT_COLOR_PROMPT;
722 hl_attr = is_hilited_attr(pos, pos+1, 0, &matches);
723 if (hl_attr == 0 && status_line)
724 hl_attr = line_mark_attr;
729 * This character should be highlighted.
730 * Override the attribute passed in.
734 if (highest_hilite != NULL_POSITION && pos != NULL_POSITION && pos > highest_hilite)
735 highest_hilite = pos;
744 * This is the first non-hilited char after a hilite.
745 * Resend the last ANSI seq to restore color.
754 for (ai = 0; ai < NUM_LAST_ANSIS; ai++)
756 int ax = (curr_last_ansi + ai) % NUM_LAST_ANSIS;
757 for (i = 0; i < last_ansis[ax].end; i++)
758 STORE_CHAR(last_ansis[ax].data[i], AT_ANSI, NULL, pos);
767 char *p = &linebuf.buf[linebuf.end];
768 LWCHAR prev_ch = (linebuf.end > 0) ? step_char(&p, -1, linebuf.buf) : 0;
769 int prev_a = (linebuf.end > 0) ? linebuf.attr[linebuf.end-1] : 0;
770 w = pwidth(ch, a, prev_ch, prev_a);
773 if (!fits_on_screen(w, a))
783 replen = utf_len(rep[0]);
785 if (linebuf.end + replen >= size_linebuf-6)
788 * Won't fit in line buffer.
791 if (expand_linebuf())
795 if (cshift == hshift && shifted_ansi.end > 0)
797 /* Copy shifted ANSI sequences to beginning of line. */
798 for (i = 0; i < shifted_ansi.end; i++)
799 add_linebuf(shifted_ansi.data[i], AT_ANSI, 0);
800 xbuf_reset(&shifted_ansi);
802 /* Add the char to the buf, even if we will left-shift it next. */
804 for (i = 0; i < replen; i++)
805 add_linebuf(*rep++, a, 0);
809 /* We haven't left-shifted enough yet. */
811 xbuf_add(&shifted_ansi, ch); /* Save ANSI attributes */
812 if (linebuf.end > linebuf.print)
814 /* Shift left enough to put last byte of this char at print-1. */
816 for (i = 0; i < linebuf.print; i++)
818 linebuf.buf[i] = linebuf.buf[i+replen];
819 linebuf.attr[i] = linebuf.attr[i+replen];
821 linebuf.end -= replen;
824 * If the char we just left-shifted was double width,
825 * the 2 spaces we shifted may be too much.
826 * Represent the "half char" at start of line with a highlighted space.
828 while (cshift > hshift)
830 add_linebuf(' ', rscroll_attr, 0);
838 #define STORE_STRING(s,a,pos) \
839 do { if (store_string((s),(a),(pos))) return (1); } while (0)
842 store_string(s, a, pos)
847 if (!fits_on_screen(strlen(s), a))
849 for ( ; *s != 0; s++)
850 STORE_CHAR(*s, a, NULL, pos);
855 * Append a tab to the line buffer.
856 * Store spaces to represent the tab.
858 #define STORE_TAB(a,pos) \
859 do { if (store_tab((a),(pos))) return (1); } while (0)
866 int to_tab = end_column - linebuf.pfx_end;
868 if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1])
869 to_tab = tabdefault -
870 ((to_tab - tabstops[ntabstops-1]) % tabdefault);
874 for (i = ntabstops - 2; i >= 0; i--)
875 if (to_tab >= tabstops[i])
877 to_tab = tabstops[i+1] - to_tab;
881 STORE_CHAR(' ', attr, " ", pos);
882 } while (--to_tab > 0);
886 #define STORE_PRCHAR(c, pos) \
887 do { if (store_prchar((c), (pos))) return 1; } while (0)
895 * Convert to printable representation.
897 STORE_STRING(prchar(c), AT_BINARY|AT_COLOR_CTRL, pos);
907 for (i = 0; i < mbc_buf_index; i++)
908 if (store_prchar(mbc_buf[i], pos))
909 return mbc_buf_index - i;
914 * Append a character to the line buffer.
915 * Expand tabs into spaces, handle underlining, boldfacing, etc.
916 * Returns 0 if ok, 1 if couldn't fit in buffer.
927 if (c == '\r' && pendc == '\r')
929 if (do_append(pendc, NULL, pendpos))
931 * Oops. We've probably lost the char which
932 * was in pendc, since caller won't back up.
938 if (c == '\r' && bs_mode == BS_SPECIAL)
940 if (mbc_buf_len > 0) /* utf_mode must be on. */
942 /* Flush incomplete (truncated) sequence. */
943 r = flush_mbc_buf(mbc_pos);
944 mbc_buf_index = r + 1;
947 return (mbc_buf_index);
951 * Don't put the CR into the buffer until we see
952 * the next char. If the next char is a newline,
962 r = do_append(c, NULL, pos);
965 /* Perform strict validation in all possible cases. */
966 if (mbc_buf_len == 0)
971 if (IS_ASCII_OCTET(c))
972 r = do_append(c, NULL, pos);
973 else if (IS_UTF8_LEAD(c))
975 mbc_buf_len = utf_len(c);
979 /* UTF8_INVALID or stray UTF8_TRAIL */
980 r = flush_mbc_buf(pos);
981 } else if (IS_UTF8_TRAIL(c))
983 mbc_buf[mbc_buf_index++] = c;
984 if (mbc_buf_index < mbc_buf_len)
986 if (is_utf8_well_formed(mbc_buf, mbc_buf_index))
987 r = do_append(get_wchar(mbc_buf), mbc_buf, mbc_pos);
989 /* Complete, but not shortest form, sequence. */
990 mbc_buf_index = r = flush_mbc_buf(mbc_pos);
994 /* Flush incomplete (truncated) sequence. */
995 r = flush_mbc_buf(mbc_pos);
996 mbc_buf_index = r + 1;
998 /* Handle new char. */
1005 /* How many chars should caller back up? */
1006 r = (!utf_mode) ? 1 : mbc_buf_index;
1012 store_control_char(ch, rep, pos)
1017 if (ctldisp == OPT_ON)
1019 /* Output the character itself. */
1020 STORE_CHAR(ch, AT_NORMAL, rep, pos);
1023 /* Output a printable representation of the character. */
1024 STORE_PRCHAR((char) ch, pos);
1030 store_ansi(ch, rep, pos)
1035 switch (ansi_step(line_ansi, ch))
1038 STORE_CHAR(ch, AT_ANSI, rep, pos);
1039 if (line_ansi->hlink)
1041 xbuf_add(&last_ansi, ch);
1044 STORE_CHAR(ch, AT_ANSI, rep, pos);
1045 ansi_done(line_ansi);
1047 xbuf_add(&last_ansi, ch);
1048 xbuf_set(&last_ansis[curr_last_ansi], &last_ansi);
1049 xbuf_reset(&last_ansi);
1050 curr_last_ansi = (curr_last_ansi + 1) % NUM_LAST_ANSIS;
1054 /* Remove whole unrecognized sequence. */
1055 char *start = (cshift < hshift) ? shifted_ansi.data : linebuf.buf;
1056 int *end = (cshift < hshift) ? &shifted_ansi.end : &linebuf.end;
1057 char *p = start + *end;
1060 bch = step_char(&p, -1, start);
1061 } while (p > start && !IS_CSI_START(bch));
1062 *end = (int) (p - start);
1064 xbuf_reset(&last_ansi);
1065 ansi_done(line_ansi);
1073 store_bs(ch, rep, pos)
1078 if (bs_mode == BS_CONTROL)
1079 return store_control_char(ch, rep, pos);
1080 if (linebuf.end > 0 &&
1081 ((linebuf.end <= linebuf.print && linebuf.buf[linebuf.end-1] == '\0') ||
1082 (linebuf.end > 0 && linebuf.attr[linebuf.end - 1] & (AT_ANSI|AT_BINARY))))
1083 STORE_PRCHAR('\b', pos);
1084 else if (bs_mode == BS_NORMAL)
1085 STORE_CHAR(ch, AT_NORMAL, NULL, pos);
1086 else if (bs_mode == BS_SPECIAL)
1087 overstrike = backc();
1092 do_append(ch, rep, pos)
1099 if (ctldisp == OPT_ONPLUS && line_ansi == NULL)
1101 line_ansi = ansi_start(ch);
1102 if (line_ansi != NULL)
1106 if (line_ansi != NULL)
1107 return store_ansi(ch, rep, pos);
1110 return store_bs(ch, rep, pos);
1115 * Overstrike the character at the current position
1116 * in the line buffer. This will cause either
1117 * underline (if a "_" is overstruck),
1118 * bold (if an identical character is overstruck),
1119 * or just replacing the character in the buffer.
1122 overstrike = utf_mode ? -1 : 0;
1125 /* To be correct, this must be a base character. */
1126 prev_ch = get_wchar(&linebuf.buf[linebuf.end]);
1129 prev_ch = (unsigned char) linebuf.buf[linebuf.end];
1131 a = linebuf.attr[linebuf.end];
1135 * Overstriking a char with itself means make it bold.
1136 * But overstriking an underscore with itself is
1137 * ambiguous. It could mean make it bold, or
1138 * it could mean make it underlined.
1139 * Use the previous overstrike to resolve it.
1143 if ((a & (AT_BOLD|AT_UNDERLINE)) != AT_NORMAL)
1144 a |= (AT_BOLD|AT_UNDERLINE);
1145 else if (last_overstrike != AT_NORMAL)
1146 a |= last_overstrike;
1151 } else if (ch == '_')
1155 rep = &linebuf.buf[linebuf.end];
1156 } else if (prev_ch == '_')
1160 /* Else we replace prev_ch, but we keep its attributes. */
1161 } else if (overstrike < 0)
1163 if ( is_composing_char(ch)
1164 || is_combining_char(get_wchar(&linebuf.buf[linebuf.end]), ch))
1165 /* Continuation of the same overstrike. */
1166 a = last_overstrike;
1174 * Expand a tab into spaces.
1179 return store_control_char(ch, rep, pos);
1187 if ((!utf_mode || is_ascii_char(ch)) && control_char((char)ch))
1189 return store_control_char(ch, rep, pos);
1190 } else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch))
1192 STORE_STRING(prutfchar(ch), AT_BINARY, pos);
1195 STORE_CHAR(ch, a, rep, pos);
1204 pflushmbc(VOID_PARAM)
1208 if (mbc_buf_len > 0)
1210 /* Flush incomplete (truncated) sequence. */
1211 r = flush_mbc_buf(mbc_pos);
1218 * Switch to normal attribute at end of line.
1221 add_attr_normal(VOID_PARAM)
1223 if (ctldisp != OPT_ONPLUS || !is_ansi_end('m'))
1225 addstr_linebuf("\033[m", AT_ANSI, 0);
1226 if (hlink_in_line) /* Don't send hyperlink clear if we know we don't need to. */
1227 addstr_linebuf("\033]8;;\033\\", AT_ANSI, 0);
1231 * Terminate the line in the line buffer.
1234 pdone(endline, chopped, forw)
1241 if (pendc && (pendc != '\r' || !endline))
1243 * If we had a pending character, put it in the buffer.
1244 * But discard a pending CR if we are at end of line
1245 * (that is, discard the CR in a CR/LF sequence).
1247 (void) do_append(pendc, NULL, pendpos);
1249 if (chopped && rscroll_char)
1252 * Display the right scrolling char.
1253 * If we've already filled the rightmost screen char
1254 * (in the buffer), overwrite it.
1256 if (end_column >= sc_width + cshift)
1258 /* We've already written in the rightmost char. */
1259 end_column = right_column;
1260 linebuf.end = right_curr;
1263 while (end_column < sc_width-1 + cshift)
1266 * Space to last (rightmost) char on screen.
1267 * This may be necessary if the char we overwrote
1270 add_linebuf(' ', rscroll_attr, 1);
1272 /* Print rscroll char. It must be single-width. */
1273 add_linebuf(rscroll_char, rscroll_attr, 1);
1280 * If we're coloring a status line, fill out the line with spaces.
1282 if (status_line && line_mark_attr != 0) {
1283 while (end_column +1 < sc_width + cshift)
1284 add_linebuf(' ', line_mark_attr, 1);
1288 * Add a newline if necessary,
1289 * and append a '\0' to the end of the line.
1290 * We output a newline if we're not at the right edge of the screen,
1291 * or if the terminal doesn't auto wrap,
1292 * or if this is really the end of the line AND the terminal ignores
1293 * a newline at the right edge.
1294 * (In the last case we don't want to output a newline if the terminal
1295 * doesn't ignore it since that would produce an extra blank line.
1296 * But we do want to output a newline if the terminal ignores it in case
1297 * the next line is blank. In that case the single newline output for
1298 * that blank line would be ignored!)
1300 if (end_column < sc_width + cshift || !auto_wrap || (endline && ignaw) || ctldisp == OPT_ON)
1302 add_linebuf('\n', AT_NORMAL, 0);
1304 else if (ignaw && end_column >= sc_width + cshift && forw)
1307 * Terminals with "ignaw" don't wrap until they *really* need
1308 * to, i.e. when the character *after* the last one to fit on a
1309 * line is output. But they are too hard to deal with when they
1310 * get in the state where a full screen width of characters
1311 * have been output but the cursor is sitting on the right edge
1312 * instead of at the start of the next line.
1313 * So we nudge them into wrapping by outputting a space
1314 * character plus a backspace. But do this only if moving
1315 * forward; if we're moving backward and drawing this line at
1316 * the top of the screen, the space would overwrite the first
1317 * char on the next line. We don't need to do this "nudge"
1318 * at the top of the screen anyway.
1320 add_linebuf(' ', AT_NORMAL, 1);
1321 add_linebuf('\b', AT_NORMAL, -1);
1323 set_linebuf(linebuf.end, '\0', AT_NORMAL);
1327 * Set an attribute on each char of the line in the line buffer.
1335 for (i = linebuf.print; i < linebuf.end; i++)
1336 linebuf.attr[i] |= a;
1340 * Set the char to be displayed in the status column.
1343 set_status_col(c, attr)
1347 set_pfx(0, c, attr);
1351 * Get a character from the current line.
1352 * Return the character as the function return value,
1353 * and the character attribute in *ap.
1363 * If there is no current line, we pretend the line is
1364 * either "~" or "", depending on the "twiddle" flag.
1375 /* Make sure we're back to AT_NORMAL before the '\n'. */
1377 return i ? '\0' : '\n';
1380 if (i < linebuf.pfx_end)
1382 *ap = linebuf.pfx_attr[i];
1383 return linebuf.pfx[i];
1385 i += linebuf.print - linebuf.pfx_end;
1386 *ap = linebuf.attr[i];
1387 return (linebuf.buf[i] & 0xFF);
1391 * Indicate that there is no current line.
1394 null_line(VOID_PARAM)
1401 * Analogous to forw_line(), but deals with "raw lines":
1402 * lines which are not split for screen width.
1403 * {{ This is supposed to be more efficient than forw_line(). }}
1406 forw_raw_line(curr_pos, linep, line_lenp)
1415 if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
1416 (c = ch_forw_get()) == EOI)
1417 return (NULL_POSITION);
1422 if (c == '\n' || c == EOI || ABORT_SIGS())
1424 new_pos = ch_tell();
1427 if (n >= size_linebuf-1)
1429 if (expand_linebuf())
1432 * Overflowed the input buffer.
1433 * Pretend the line ended here.
1435 new_pos = ch_tell() - 1;
1439 linebuf.buf[n++] = c;
1442 linebuf.buf[n] = '\0';
1444 *linep = linebuf.buf;
1445 if (line_lenp != NULL)
1451 * Analogous to back_line(), but deals with "raw lines".
1452 * {{ This is supposed to be more efficient than back_line(). }}
1455 back_raw_line(curr_pos, linep, line_lenp)
1464 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
1465 ch_seek(curr_pos-1))
1466 return (NULL_POSITION);
1469 linebuf.buf[--n] = '\0';
1473 if (c == '\n' || ABORT_SIGS())
1476 * This is the newline ending the previous line.
1477 * We have hit the beginning of the line.
1479 new_pos = ch_tell() + 1;
1485 * We have hit the beginning of the file.
1486 * This must be the first line in the file.
1487 * This must, of course, be the beginning of the line.
1489 new_pos = ch_zero();
1494 int old_size_linebuf = size_linebuf;
1497 if (expand_linebuf())
1500 * Overflowed the input buffer.
1501 * Pretend the line ended here.
1503 new_pos = ch_tell() + 1;
1507 * Shift the data to the end of the new linebuf.
1509 for (fm = linebuf.buf + old_size_linebuf - 1,
1510 to = linebuf.buf + size_linebuf - 1;
1511 fm >= linebuf.buf; fm--, to--)
1513 n = size_linebuf - old_size_linebuf;
1515 linebuf.buf[--n] = c;
1518 *linep = &linebuf.buf[n];
1519 if (line_lenp != NULL)
1520 *line_lenp = size_linebuf - 1 - n;
1525 * Append a string to the line buffer.
1531 while (*str != '\0')
1533 if (pappend(*str++, NULL_POSITION))
1534 /* Doesn't fit on screen. */
1541 * Load a string into the line buffer.
1542 * If the string is too long to fit on the screen,
1543 * truncate the beginning of the string to fit.
1549 int save_hshift = hshift;
1555 if (pappstr(str) == 0)
1558 * Didn't fit on screen; increase left shift by one.
1559 * {{ This gets very inefficient if the string
1560 * is much longer than the screen width. }}
1564 set_linebuf(linebuf.end, '\0', AT_NORMAL);
1565 hshift = save_hshift;
1569 * Find the shift necessary to show the end of the longest displayed line.
1579 save_width = sc_width;
1581 pos = position(TOP);
1582 for (line = 0; line < sc_height && pos != NULL_POSITION; line++)
1584 pos = forw_line(pos);
1585 if (end_column > longest)
1586 longest = end_column;
1588 sc_width = save_width;
1589 if (longest < sc_width)
1591 return longest - sc_width;
1595 * Get the color_map index associated with a given attribute.
1603 switch (attr & AT_COLOR)
1605 case AT_COLOR_ATTN: return 0;
1606 case AT_COLOR_BIN: return 1;
1607 case AT_COLOR_CTRL: return 2;
1608 case AT_COLOR_ERROR: return 3;
1609 case AT_COLOR_LINENUM: return 4;
1610 case AT_COLOR_MARK: return 5;
1611 case AT_COLOR_PROMPT: return 6;
1612 case AT_COLOR_RSCROLL: return 7;
1613 case AT_COLOR_SEARCH: return 8;
1614 case AT_COLOR_HEADER: return 9;
1617 if (attr & AT_UNDERLINE)
1621 if (attr & AT_BLINK)
1623 if (attr & AT_STANDOUT)
1629 * Set the color string to use for a given attribute.
1632 set_color_map(attr, colorstr)
1636 int cx = color_index(attr);
1639 if (strlen(colorstr)+1 > sizeof(color_map[cx]))
1641 if (*colorstr != '\0' && parse_color(colorstr, NULL, NULL) == CT_NULL)
1643 strcpy(color_map[cx], colorstr);
1648 * Get the color string to use for a given attribute.
1654 int cx = color_index(attr);
1657 return color_map[cx];