Update to less-418:
[dragonfly.git] / contrib / less-4 / line.c
blob3bd2b3808ee494d545421e8c048d5e3aa04bf91d
1 /*
2 * Copyright (C) 1984-2007 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.
9 */
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.
18 #include "less.h"
19 #include "charset.h"
21 static char *linebuf = NULL; /* Buffer which holds the current output line */
22 static char *attr = NULL; /* Extension of linebuf to hold attributes */
23 public int size_linebuf = 0; /* Size of line buffer (and attr buffer) */
25 static int cshift; /* Current left-shift of output line buffer */
26 public int hshift; /* Desired left-shift of output line buffer */
27 public int tabstops[TABSTOP_MAX] = { 0 }; /* Custom tabstops */
28 public int ntabstops = 1; /* Number of tabstops */
29 public int tabdefault = 8; /* Default repeated tabstops */
31 static int curr; /* Index into linebuf */
32 static int column; /* Printable length, accounting for
33 backspaces, etc. */
34 static int overstrike; /* Next char should overstrike previous char */
35 static int last_overstrike = AT_NORMAL;
36 static int is_null_line; /* There is no current line */
37 static int lmargin; /* Left margin */
38 static int line_matches; /* Number of search matches in this line */
39 static char pendc;
40 static POSITION pendpos;
41 static char *end_ansi_chars;
42 static char *mid_ansi_chars;
44 static int attr_swidth();
45 static int attr_ewidth();
46 static int do_append();
48 extern int sigs;
49 extern int bs_mode;
50 extern int linenums;
51 extern int ctldisp;
52 extern int twiddle;
53 extern int binattr;
54 extern int status_col;
55 extern int auto_wrap, ignaw;
56 extern int bo_s_width, bo_e_width;
57 extern int ul_s_width, ul_e_width;
58 extern int bl_s_width, bl_e_width;
59 extern int so_s_width, so_e_width;
60 extern int sc_width, sc_height;
61 extern int utf_mode;
62 extern int oldbot;
63 extern POSITION start_attnpos;
64 extern POSITION end_attnpos;
66 static char mbc_buf[MAX_UTF_CHAR_LEN];
67 static int mbc_buf_len = 0;
68 static int mbc_buf_index = 0;
69 static POSITION mbc_pos;
72 * Initialize from environment variables.
74 public void
75 init_line()
77 end_ansi_chars = lgetenv("LESSANSIENDCHARS");
78 if (end_ansi_chars == NULL || *end_ansi_chars == '\0')
79 end_ansi_chars = "m";
81 mid_ansi_chars = lgetenv("LESSANSIMIDCHARS");
82 if (mid_ansi_chars == NULL || *mid_ansi_chars == '\0')
83 mid_ansi_chars = "0123456789;[?!\"'#%()*+ ";
85 linebuf = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
86 attr = (char *) ecalloc(LINEBUF_SIZE, sizeof(char));
87 size_linebuf = LINEBUF_SIZE;
91 * Expand the line buffer.
93 static int
94 expand_linebuf()
96 /* Double the size of the line buffer. */
97 int new_size = size_linebuf * 2;
99 /* Just realloc to expand the buffer, if we can. */
100 #if HAVE_REALLOC
101 char *new_buf = (char *) realloc(linebuf, new_size);
102 char *new_attr = (char *) realloc(attr, new_size);
103 #else
104 char *new_buf = (char *) calloc(new_size, sizeof(char));
105 char *new_attr = (char *) calloc(new_size, sizeof(char));
106 #endif
107 if (new_buf == NULL || new_attr == NULL)
109 if (new_attr != NULL)
110 free(new_attr);
111 if (new_buf != NULL)
112 free(new_buf);
113 return 1;
115 #if HAVE_REALLOC
117 * We realloc'd the buffers; they already have the old contents.
119 #if 0
120 memset(new_buf + size_linebuf, 0, new_size - size_linebuf);
121 memset(new_attr + size_linebuf, 0, new_size - size_linebuf);
122 #endif
123 #else
125 * We just calloc'd the buffers; copy the old contents.
127 memcpy(new_buf, linebuf, size_linebuf * sizeof(char));
128 memcpy(new_attr, attr, size_linebuf * sizeof(char));
129 free(attr);
130 free(linebuf);
131 #endif
132 linebuf = new_buf;
133 attr = new_attr;
134 size_linebuf = new_size;
135 return 0;
139 * Is a character ASCII?
141 public int
142 is_ascii_char(ch)
143 LWCHAR ch;
145 return (ch <= 0x7F);
149 * Rewind the line buffer.
151 public void
152 prewind()
154 curr = 0;
155 column = 0;
156 cshift = 0;
157 overstrike = 0;
158 last_overstrike = AT_NORMAL;
159 mbc_buf_len = 0;
160 is_null_line = 0;
161 pendc = '\0';
162 lmargin = 0;
163 if (status_col)
164 lmargin += 1;
165 #if HILITE_SEARCH
166 line_matches = 0;
167 #endif
171 * Insert the line number (of the given position) into the line buffer.
173 public void
174 plinenum(pos)
175 POSITION pos;
177 register LINENUM linenum = 0;
178 register int i;
180 if (linenums == OPT_ONPLUS)
183 * Get the line number and put it in the current line.
184 * {{ Note: since find_linenum calls forw_raw_line,
185 * it may seek in the input file, requiring the caller
186 * of plinenum to re-seek if necessary. }}
187 * {{ Since forw_raw_line modifies linebuf, we must
188 * do this first, before storing anything in linebuf. }}
190 linenum = find_linenum(pos);
194 * Display a status column if the -J option is set.
196 if (status_col)
198 linebuf[curr] = ' ';
199 if (start_attnpos != NULL_POSITION &&
200 pos >= start_attnpos && pos < end_attnpos)
201 attr[curr] = AT_NORMAL|AT_HILITE;
202 else
203 attr[curr] = AT_NORMAL;
204 curr++;
205 column++;
208 * Display the line number at the start of each line
209 * if the -N option is set.
211 if (linenums == OPT_ONPLUS)
213 char buf[INT_STRLEN_BOUND(pos) + 2];
214 int n;
216 linenumtoa(linenum, buf);
217 n = strlen(buf);
218 if (n < MIN_LINENUM_WIDTH)
219 n = MIN_LINENUM_WIDTH;
220 sprintf(linebuf+curr, "%*s ", n, buf);
221 n++; /* One space after the line number. */
222 for (i = 0; i < n; i++)
223 attr[curr+i] = AT_NORMAL;
224 curr += n;
225 column += n;
226 lmargin += n;
230 * Append enough spaces to bring us to the lmargin.
232 while (column < lmargin)
234 linebuf[curr] = ' ';
235 attr[curr++] = AT_NORMAL;
236 column++;
241 * Shift the input line left.
242 * This means discarding N printable chars at the start of the buffer.
244 static void
245 pshift(shift)
246 int shift;
248 LWCHAR prev_ch = 0;
249 unsigned char c;
250 int shifted = 0;
251 int to;
252 int from;
253 int len;
254 int width;
255 int prev_attr;
256 int next_attr;
258 if (shift > column - lmargin)
259 shift = column - lmargin;
260 if (shift > curr - lmargin)
261 shift = curr - lmargin;
263 to = from = lmargin;
265 * We keep on going when shifted == shift
266 * to get all combining chars.
268 while (shifted <= shift && from < curr)
270 c = linebuf[from];
271 if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
273 /* Keep cumulative effect. */
274 linebuf[to] = c;
275 attr[to++] = attr[from++];
276 while (from < curr && linebuf[from])
278 linebuf[to] = linebuf[from];
279 attr[to++] = attr[from];
280 if (!is_ansi_middle(linebuf[from++]))
281 break;
283 continue;
286 width = 0;
288 if (!IS_ASCII_OCTET(c) && utf_mode)
290 /* Assumes well-formedness validation already done. */
291 LWCHAR ch;
293 len = utf_len(c);
294 if (from + len > curr)
295 break;
296 ch = get_wchar(linebuf + from);
297 if (!is_composing_char(ch) && !is_combining_char(prev_ch, ch))
298 width = is_wide_char(ch) ? 2 : 1;
299 prev_ch = ch;
300 } else
302 len = 1;
303 if (c == '\b')
304 /* XXX - Incorrect if several '\b' in a row. */
305 width = (utf_mode && is_wide_char(prev_ch)) ? -2 : -1;
306 else if (!control_char(c))
307 width = 1;
308 prev_ch = 0;
311 if (width == 2 && shift - shifted == 1) {
312 /* Should never happen when called by pshift_all(). */
313 attr[to] = attr[from];
315 * Assume a wide_char will never be the first half of a
316 * combining_char pair, so reset prev_ch in case we're
317 * followed by a '\b'.
319 prev_ch = linebuf[to++] = ' ';
320 from += len;
321 shifted++;
322 continue;
325 /* Adjust width for magic cookies. */
326 prev_attr = (to > 0) ? attr[to-1] : AT_NORMAL;
327 next_attr = (from + len < curr) ? attr[from + len] : prev_attr;
328 if (!is_at_equiv(attr[from], prev_attr) &&
329 !is_at_equiv(attr[from], next_attr))
331 width += attr_swidth(attr[from]);
332 if (from + len < curr)
333 width += attr_ewidth(attr[from]);
334 if (is_at_equiv(prev_attr, next_attr))
336 width += attr_ewidth(prev_attr);
337 if (from + len < curr)
338 width += attr_swidth(next_attr);
342 if (shift - shifted < width)
343 break;
344 from += len;
345 shifted += width;
346 if (shifted < 0)
347 shifted = 0;
349 while (from < curr)
351 linebuf[to] = linebuf[from];
352 attr[to++] = attr[from++];
354 curr = to;
355 column -= shifted;
356 cshift += shifted;
362 public void
363 pshift_all()
365 pshift(column);
369 * Return the printing width of the start (enter) sequence
370 * for a given character attribute.
372 static int
373 attr_swidth(a)
374 int a;
376 int w = 0;
378 a = apply_at_specials(a);
380 if (a & AT_UNDERLINE)
381 w += ul_s_width;
382 if (a & AT_BOLD)
383 w += bo_s_width;
384 if (a & AT_BLINK)
385 w += bl_s_width;
386 if (a & AT_STANDOUT)
387 w += so_s_width;
389 return w;
393 * Return the printing width of the end (exit) sequence
394 * for a given character attribute.
396 static int
397 attr_ewidth(a)
398 int a;
400 int w = 0;
402 a = apply_at_specials(a);
404 if (a & AT_UNDERLINE)
405 w += ul_e_width;
406 if (a & AT_BOLD)
407 w += bo_e_width;
408 if (a & AT_BLINK)
409 w += bl_e_width;
410 if (a & AT_STANDOUT)
411 w += so_e_width;
413 return w;
417 * Return the printing width of a given character and attribute,
418 * if the character were added to the current position in the line buffer.
419 * Adding a character with a given attribute may cause an enter or exit
420 * attribute sequence to be inserted, so this must be taken into account.
422 static int
423 pwidth(ch, a, prev_ch)
424 LWCHAR ch;
425 int a;
426 LWCHAR prev_ch;
428 int w;
430 if (ch == '\b')
432 * Backspace moves backwards one or two positions.
433 * XXX - Incorrect if several '\b' in a row.
435 return (utf_mode && is_wide_char(prev_ch)) ? -2 : -1;
437 if (!utf_mode || is_ascii_char(ch))
439 if (control_char((char)ch))
442 * Control characters do unpredictable things,
443 * so we don't even try to guess; say it doesn't move.
444 * This can only happen if the -r flag is in effect.
446 return (0);
448 } else
450 if (is_composing_char(ch) || is_combining_char(prev_ch, ch))
453 * Composing and combining chars take up no space.
455 * Some terminals, upon failure to compose a
456 * composing character with the character(s) that
457 * precede(s) it will actually take up one column
458 * for the composing character; there isn't much
459 * we could do short of testing the (complex)
460 * composition process ourselves and printing
461 * a binary representation when it fails.
463 return (0);
468 * Other characters take one or two columns,
469 * plus the width of any attribute enter/exit sequence.
471 w = 1;
472 if (is_wide_char(ch))
473 w++;
474 if (curr > 0 && !is_at_equiv(attr[curr-1], a))
475 w += attr_ewidth(attr[curr-1]);
476 if ((apply_at_specials(a) != AT_NORMAL) &&
477 (curr == 0 || !is_at_equiv(attr[curr-1], a)))
478 w += attr_swidth(a);
479 return (w);
483 * Delete to the previous base character in the line buffer.
484 * Return 1 if one is found.
486 static int
487 backc()
489 LWCHAR prev_ch;
490 char *p = linebuf + curr;
491 LWCHAR ch = step_char(&p, -1, linebuf + lmargin);
492 int width;
494 /* This assumes that there is no '\b' in linebuf. */
495 while ( curr > lmargin
496 && column > lmargin
497 && (!(attr[curr - 1] & (AT_ANSI|AT_BINARY))))
499 curr = p - linebuf;
500 prev_ch = step_char(&p, -1, linebuf + lmargin);
501 width = pwidth(ch, attr[curr], prev_ch);
502 column -= width;
503 if (width > 0)
504 return 1;
505 ch = prev_ch;
508 return 0;
512 * Are we currently within a recognized ANSI escape sequence?
514 static int
515 in_ansi_esc_seq()
517 char *p;
520 * Search backwards for either an ESC (which means we ARE in a seq);
521 * or an end char (which means we're NOT in a seq).
523 for (p = &linebuf[curr]; p > linebuf; )
525 LWCHAR ch = step_char(&p, -1, linebuf);
526 if (IS_CSI_START(ch))
527 return (1);
528 if (!is_ansi_middle(ch))
529 return (0);
531 return (0);
535 * Is a character the end of an ANSI escape sequence?
537 public int
538 is_ansi_end(ch)
539 LWCHAR ch;
541 if (!is_ascii_char(ch))
542 return (0);
543 return (strchr(end_ansi_chars, (char) ch) != NULL);
549 public int
550 is_ansi_middle(ch)
551 LWCHAR ch;
553 if (!is_ascii_char(ch))
554 return (0);
555 if (is_ansi_end(ch))
556 return (0);
557 return (strchr(mid_ansi_chars, (char) ch) != NULL);
561 * Append a character and attribute to the line buffer.
563 #define STORE_CHAR(ch,a,rep,pos) \
564 do { \
565 if (store_char((ch),(a),(rep),(pos))) return (1); \
566 } while (0)
568 static int
569 store_char(ch, a, rep, pos)
570 LWCHAR ch;
571 int a;
572 char *rep;
573 POSITION pos;
575 int w;
576 int replen;
577 char cs;
579 w = (a & (AT_UNDERLINE|AT_BOLD)); /* Pre-use w. */
580 if (w != AT_NORMAL)
581 last_overstrike = w;
583 #if HILITE_SEARCH
585 int matches;
586 if (is_hilited(pos, pos+1, 0, &matches))
589 * This character should be highlighted.
590 * Override the attribute passed in.
592 if (a != AT_ANSI)
593 a |= AT_HILITE;
595 line_matches += matches;
597 #endif
599 if (ctldisp == OPT_ONPLUS && in_ansi_esc_seq())
601 if (!is_ansi_end(ch) && !is_ansi_middle(ch)) {
602 /* Remove whole unrecognized sequence. */
603 do {
604 if (curr == 0)
605 break;
606 --curr;
607 } while (!IS_CSI_START(linebuf[curr]));
608 return 0;
610 a = AT_ANSI; /* Will force re-AT_'ing around it. */
611 w = 0;
613 else if (ctldisp == OPT_ONPLUS && IS_CSI_START(ch))
615 a = AT_ANSI; /* Will force re-AT_'ing around it. */
616 w = 0;
618 else
620 char *p = &linebuf[curr];
621 LWCHAR prev_ch = step_char(&p, -1, linebuf);
622 w = pwidth(ch, a, prev_ch);
625 if (ctldisp != OPT_ON && column + w + attr_ewidth(a) > sc_width)
627 * Won't fit on screen.
629 return (1);
631 if (rep == NULL)
633 cs = (char) ch;
634 rep = &cs;
635 replen = 1;
636 } else
638 replen = utf_len(rep[0]);
640 if (curr + replen >= size_linebuf-6)
643 * Won't fit in line buffer.
644 * Try to expand it.
646 if (expand_linebuf())
647 return (1);
650 while (replen-- > 0)
652 linebuf[curr] = *rep++;
653 attr[curr] = a;
654 curr++;
656 column += w;
657 return (0);
661 * Append a tab to the line buffer.
662 * Store spaces to represent the tab.
664 #define STORE_TAB(a,pos) \
665 do { if (store_tab((a),(pos))) return (1); } while (0)
667 static int
668 store_tab(attr, pos)
669 int attr;
670 POSITION pos;
672 int to_tab = column + cshift - lmargin;
673 int i;
675 if (ntabstops < 2 || to_tab >= tabstops[ntabstops-1])
676 to_tab = tabdefault -
677 ((to_tab - tabstops[ntabstops-1]) % tabdefault);
678 else
680 for (i = ntabstops - 2; i >= 0; i--)
681 if (to_tab >= tabstops[i])
682 break;
683 to_tab = tabstops[i+1] - to_tab;
686 if (column + to_tab - 1 + pwidth(' ', attr, 0) + attr_ewidth(attr) > sc_width)
687 return 1;
689 do {
690 STORE_CHAR(' ', attr, " ", pos);
691 } while (--to_tab > 0);
692 return 0;
695 #define STORE_PRCHAR(c, pos) \
696 do { if (store_prchar((c), (pos))) return 1; } while (0)
698 static int
699 store_prchar(c, pos)
700 char c;
701 POSITION pos;
703 char *s;
706 * Convert to printable representation.
708 s = prchar(c);
711 * Make sure we can get the entire representation
712 * of the character on this line.
714 if (column + (int) strlen(s) - 1 +
715 pwidth(' ', binattr, 0) + attr_ewidth(binattr) > sc_width)
716 return 1;
718 for ( ; *s != 0; s++)
719 STORE_CHAR(*s, AT_BINARY, NULL, pos);
721 return 0;
724 static int
725 flush_mbc_buf(pos)
726 POSITION pos;
728 int i;
730 for (i = 0; i < mbc_buf_index; i++)
731 if (store_prchar(mbc_buf[i], pos))
732 return mbc_buf_index - i;
734 return 0;
738 * Append a character to the line buffer.
739 * Expand tabs into spaces, handle underlining, boldfacing, etc.
740 * Returns 0 if ok, 1 if couldn't fit in buffer.
742 public int
743 pappend(c, pos)
744 char c;
745 POSITION pos;
747 int r;
749 if (pendc)
751 if (do_append(pendc, NULL, pendpos))
753 * Oops. We've probably lost the char which
754 * was in pendc, since caller won't back up.
756 return (1);
757 pendc = '\0';
760 if (c == '\r' && bs_mode == BS_SPECIAL)
762 if (mbc_buf_len > 0) /* utf_mode must be on. */
764 /* Flush incomplete (truncated) sequence. */
765 r = flush_mbc_buf(mbc_pos);
766 mbc_buf_index = r + 1;
767 mbc_buf_len = 0;
768 if (r)
769 return (mbc_buf_index);
773 * Don't put the CR into the buffer until we see
774 * the next char. If the next char is a newline,
775 * discard the CR.
777 pendc = c;
778 pendpos = pos;
779 return (0);
782 if (!utf_mode)
784 r = do_append((LWCHAR) c, NULL, pos);
785 } else
787 /* Perform strict validation in all possible cases. */
788 if (mbc_buf_len == 0)
790 retry:
791 mbc_buf_index = 1;
792 *mbc_buf = c;
793 if (IS_ASCII_OCTET(c))
794 r = do_append((LWCHAR) c, NULL, pos);
795 else if (IS_UTF8_LEAD(c))
797 mbc_buf_len = utf_len(c);
798 mbc_pos = pos;
799 return (0);
800 } else
801 /* UTF8_INVALID or stray UTF8_TRAIL */
802 r = flush_mbc_buf(pos);
803 } else if (IS_UTF8_TRAIL(c))
805 mbc_buf[mbc_buf_index++] = c;
806 if (mbc_buf_index < mbc_buf_len)
807 return (0);
808 if (is_utf8_well_formed(mbc_buf))
809 r = do_append(get_wchar(mbc_buf), mbc_buf, mbc_pos);
810 else
811 /* Complete, but not shortest form, sequence. */
812 mbc_buf_index = r = flush_mbc_buf(mbc_pos);
813 mbc_buf_len = 0;
814 } else
816 /* Flush incomplete (truncated) sequence. */
817 r = flush_mbc_buf(mbc_pos);
818 mbc_buf_index = r + 1;
819 mbc_buf_len = 0;
820 /* Handle new char. */
821 if (!r)
822 goto retry;
827 * If we need to shift the line, do it.
828 * But wait until we get to at least the middle of the screen,
829 * so shifting it doesn't affect the chars we're currently
830 * pappending. (Bold & underline can get messed up otherwise.)
832 if (cshift < hshift && column > sc_width / 2)
834 linebuf[curr] = '\0';
835 pshift(hshift - cshift);
837 if (r)
839 /* How many chars should caller back up? */
840 r = (!utf_mode) ? 1 : mbc_buf_index;
842 return (r);
845 static int
846 do_append(ch, rep, pos)
847 LWCHAR ch;
848 char *rep;
849 POSITION pos;
851 register int a;
852 LWCHAR prev_ch;
854 a = AT_NORMAL;
856 if (ch == '\b')
858 if (bs_mode == BS_CONTROL)
859 goto do_control_char;
862 * A better test is needed here so we don't
863 * backspace over part of the printed
864 * representation of a binary character.
866 if ( curr <= lmargin
867 || column <= lmargin
868 || (attr[curr - 1] & (AT_ANSI|AT_BINARY)))
869 STORE_PRCHAR('\b', pos);
870 else if (bs_mode == BS_NORMAL)
871 STORE_CHAR(ch, AT_NORMAL, NULL, pos);
872 else if (bs_mode == BS_SPECIAL)
873 overstrike = backc();
875 return 0;
878 if (overstrike > 0)
881 * Overstrike the character at the current position
882 * in the line buffer. This will cause either
883 * underline (if a "_" is overstruck),
884 * bold (if an identical character is overstruck),
885 * or just deletion of the character in the buffer.
887 overstrike = utf_mode ? -1 : 0;
888 /* To be correct, this must be a base character. */
889 prev_ch = get_wchar(linebuf + curr);
890 a = attr[curr];
891 if (ch == prev_ch)
894 * Overstriking a char with itself means make it bold.
895 * But overstriking an underscore with itself is
896 * ambiguous. It could mean make it bold, or
897 * it could mean make it underlined.
898 * Use the previous overstrike to resolve it.
900 if (ch == '_')
902 if ((a & (AT_BOLD|AT_UNDERLINE)) != AT_NORMAL)
903 a |= (AT_BOLD|AT_UNDERLINE);
904 else if (last_overstrike != AT_NORMAL)
905 a |= last_overstrike;
906 else
907 a |= AT_BOLD;
908 } else
909 a |= AT_BOLD;
910 } else if (ch == '_')
912 a |= AT_UNDERLINE;
913 ch = prev_ch;
914 rep = linebuf + curr;
915 } else if (prev_ch == '_')
917 a |= AT_UNDERLINE;
919 /* Else we replace prev_ch, but we keep its attributes. */
920 } else if (overstrike < 0)
922 if ( is_composing_char(ch)
923 || is_combining_char(get_wchar(linebuf + curr), ch))
924 /* Continuation of the same overstrike. */
925 a = last_overstrike;
926 else
927 overstrike = 0;
930 if (ch == '\t')
933 * Expand a tab into spaces.
935 switch (bs_mode)
937 case BS_CONTROL:
938 goto do_control_char;
939 case BS_NORMAL:
940 case BS_SPECIAL:
941 STORE_TAB(a, pos);
942 break;
944 } else if ((!utf_mode || is_ascii_char(ch)) && control_char((char)ch))
946 do_control_char:
947 if (ctldisp == OPT_ON || (ctldisp == OPT_ONPLUS && IS_CSI_START(ch)))
950 * Output as a normal character.
952 STORE_CHAR(ch, AT_NORMAL, rep, pos);
953 } else
955 STORE_PRCHAR((char) ch, pos);
957 } else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch))
959 char *s;
961 s = prutfchar(ch);
963 if (column + (int) strlen(s) - 1 +
964 pwidth(' ', binattr, 0) + attr_ewidth(binattr) > sc_width)
965 return (1);
967 for ( ; *s != 0; s++)
968 STORE_CHAR(*s, AT_BINARY, NULL, pos);
969 } else
971 STORE_CHAR(ch, a, rep, pos);
973 return (0);
979 public int
980 pflushmbc()
982 int r = 0;
984 if (mbc_buf_len > 0)
986 /* Flush incomplete (truncated) sequence. */
987 r = flush_mbc_buf(mbc_pos);
988 mbc_buf_len = 0;
990 return r;
994 * Terminate the line in the line buffer.
996 public void
997 pdone(endline)
998 int endline;
1000 int nl;
1002 (void) pflushmbc();
1004 if (pendc && (pendc != '\r' || !endline))
1006 * If we had a pending character, put it in the buffer.
1007 * But discard a pending CR if we are at end of line
1008 * (that is, discard the CR in a CR/LF sequence).
1010 (void) do_append(pendc, NULL, pendpos);
1013 * Make sure we've shifted the line, if we need to.
1015 if (cshift < hshift)
1016 pshift(hshift - cshift);
1018 if (ctldisp == OPT_ONPLUS && is_ansi_end('m'))
1020 /* Switch to normal attribute at end of line. */
1021 char *p = "\033[m";
1022 for ( ; *p != '\0'; p++)
1024 linebuf[curr] = *p;
1025 attr[curr++] = AT_ANSI;
1030 * Add a newline if necessary,
1031 * and append a '\0' to the end of the line.
1032 * We output a newline if we're not at the right edge of the screen,
1033 * or if the terminal doesn't auto wrap,
1034 * or if this is really the end of the line AND the terminal ignores
1035 * a newline at the right edge.
1036 * (In the last case we don't want to output a newline if the terminal
1037 * doesn't ignore it since that would produce an extra blank line.
1038 * But we do want to output a newline if the terminal ignores it in case
1039 * the next line is blank. In that case the single newline output for
1040 * that blank line would be ignored!)
1042 if (!oldbot)
1043 nl = (column < sc_width || !auto_wrap || (endline && ignaw) || ctldisp == OPT_ON);
1044 else
1045 nl = (column < sc_width || !auto_wrap || ignaw || ctldisp == OPT_ON);
1046 if (nl)
1048 linebuf[curr] = '\n';
1049 attr[curr] = AT_NORMAL;
1050 curr++;
1052 else if (ignaw && !auto_wrap && column >= sc_width)
1055 * Big horrible kludge.
1056 * No-wrap terminals are too hard to deal with when they get in
1057 * the state where a full screen width of characters have been
1058 * output but the cursor is sitting on the right edge instead
1059 * of at the start of the next line.
1060 * So after we output a full line, we output an extra
1061 * space and backspace to force the cursor to the
1062 * beginning of the next line, like a sane terminal.
1064 linebuf[curr] = ' ';
1065 attr[curr++] = AT_NORMAL;
1066 linebuf[curr] = '\b';
1067 attr[curr++] = AT_NORMAL;
1069 linebuf[curr] = '\0';
1070 attr[curr] = AT_NORMAL;
1072 #if HILITE_SEARCH
1073 if (status_col && line_matches > 0)
1075 linebuf[0] = '*';
1076 attr[0] = AT_NORMAL|AT_HILITE;
1078 #endif
1082 * Get a character from the current line.
1083 * Return the character as the function return value,
1084 * and the character attribute in *ap.
1086 public int
1087 gline(i, ap)
1088 register int i;
1089 register int *ap;
1091 if (is_null_line)
1094 * If there is no current line, we pretend the line is
1095 * either "~" or "", depending on the "twiddle" flag.
1097 if (twiddle)
1099 if (i == 0)
1101 *ap = AT_BOLD;
1102 return '~';
1104 --i;
1106 /* Make sure we're back to AT_NORMAL before the '\n'. */
1107 *ap = AT_NORMAL;
1108 return i ? '\0' : '\n';
1111 *ap = attr[i];
1112 return (linebuf[i] & 0xFF);
1116 * Indicate that there is no current line.
1118 public void
1119 null_line()
1121 is_null_line = 1;
1122 cshift = 0;
1126 * Analogous to forw_line(), but deals with "raw lines":
1127 * lines which are not split for screen width.
1128 * {{ This is supposed to be more efficient than forw_line(). }}
1130 public POSITION
1131 forw_raw_line(curr_pos, linep, line_lenp)
1132 POSITION curr_pos;
1133 char **linep;
1134 int *line_lenp;
1136 register int n;
1137 register int c;
1138 POSITION new_pos;
1140 if (curr_pos == NULL_POSITION || ch_seek(curr_pos) ||
1141 (c = ch_forw_get()) == EOI)
1142 return (NULL_POSITION);
1144 n = 0;
1145 for (;;)
1147 if (c == '\n' || c == EOI || ABORT_SIGS())
1149 new_pos = ch_tell();
1150 break;
1152 if (n >= size_linebuf-1)
1154 if (expand_linebuf())
1157 * Overflowed the input buffer.
1158 * Pretend the line ended here.
1160 new_pos = ch_tell() - 1;
1161 break;
1164 linebuf[n++] = c;
1165 c = ch_forw_get();
1167 linebuf[n] = '\0';
1168 if (linep != NULL)
1169 *linep = linebuf;
1170 if (line_lenp != NULL)
1171 *line_lenp = n;
1172 return (new_pos);
1176 * Analogous to back_line(), but deals with "raw lines".
1177 * {{ This is supposed to be more efficient than back_line(). }}
1179 public POSITION
1180 back_raw_line(curr_pos, linep, line_lenp)
1181 POSITION curr_pos;
1182 char **linep;
1183 int *line_lenp;
1185 register int n;
1186 register int c;
1187 POSITION new_pos;
1189 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
1190 ch_seek(curr_pos-1))
1191 return (NULL_POSITION);
1193 n = size_linebuf;
1194 linebuf[--n] = '\0';
1195 for (;;)
1197 c = ch_back_get();
1198 if (c == '\n' || ABORT_SIGS())
1201 * This is the newline ending the previous line.
1202 * We have hit the beginning of the line.
1204 new_pos = ch_tell() + 1;
1205 break;
1207 if (c == EOI)
1210 * We have hit the beginning of the file.
1211 * This must be the first line in the file.
1212 * This must, of course, be the beginning of the line.
1214 new_pos = ch_zero();
1215 break;
1217 if (n <= 0)
1219 int old_size_linebuf = size_linebuf;
1220 char *fm;
1221 char *to;
1222 if (expand_linebuf())
1225 * Overflowed the input buffer.
1226 * Pretend the line ended here.
1228 new_pos = ch_tell() + 1;
1229 break;
1232 * Shift the data to the end of the new linebuf.
1234 for (fm = linebuf + old_size_linebuf - 1,
1235 to = linebuf + size_linebuf - 1;
1236 fm >= linebuf; fm--, to--)
1237 *to = *fm;
1238 n = size_linebuf - old_size_linebuf;
1240 linebuf[--n] = c;
1242 if (linep != NULL)
1243 *linep = &linebuf[n];
1244 if (line_lenp != NULL)
1245 *line_lenp = size_linebuf - 1 - n;
1246 return (new_pos);