2 * Copyright (C) 1984-2015 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.
12 * High level routines dealing with the output to the screen.
16 #if MSDOS_COMPILER==WIN32C
20 public int errmsgs
; /* Count of messages displayed by error() */
22 public int final_attr
;
27 extern int so_s_width
, so_e_width
;
28 extern int screen_trashed
;
29 extern int any_display
;
33 #if MSDOS_COMPILER==WIN32C || MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
35 extern int nm_fg_color
, nm_bg_color
;
36 extern int bo_fg_color
, bo_bg_color
;
37 extern int ul_fg_color
, ul_bg_color
;
38 extern int so_fg_color
, so_bg_color
;
39 extern int bl_fg_color
, bl_bg_color
;
43 * Display the line which is in the line buffer.
55 * Don't output if a signal is pending.
61 final_attr
= AT_NORMAL
;
63 for (i
= 0; (c
= gline(i
, &a
)) != '\0'; i
++)
76 static char obuf
[OUTBUF_SIZE
];
77 static char *ob
= obuf
;
80 * Flush buffered output.
82 * If we haven't displayed any file data yet,
83 * output messages on error output (file descriptor 2),
84 * otherwise output on standard output (file descriptor 1).
86 * This has the desirable effect of producing all
87 * error messages on error output if standard output
88 * is directed to a file. It also does the same if
89 * we never produce any real output; for example, if
90 * the input file(s) cannot be opened. If we do
91 * eventually produce output, code in edit() makes
92 * sure these messages can be seen before they are
93 * overwritten or scrolled away.
101 n
= (int) (ob
- obuf
);
105 #if MSDOS_COMPILER==MSOFTC
106 if (is_tty
&& any_display
)
114 #if MSDOS_COMPILER==WIN32C || MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
115 if (is_tty
&& any_display
)
118 if (ctldisp
!= OPT_ONPLUS
)
119 WIN32textout(obuf
, ob
- obuf
);
123 * Look for SGR escape sequences, and convert them
124 * to color commands. Replace bold, underline,
125 * and italic escapes into colors specified via
126 * the -D command-line option.
128 char *anchor
, *p
, *p_next
;
129 unsigned char fg
, bg
;
130 static unsigned char at
;
131 #if MSDOS_COMPILER==WIN32C
132 /* Screen colors used by 3x and 4x SGR commands. */
133 static unsigned char screen_color
[] = {
137 FOREGROUND_RED
|FOREGROUND_GREEN
,
139 FOREGROUND_BLUE
|FOREGROUND_RED
,
140 FOREGROUND_BLUE
|FOREGROUND_GREEN
,
141 FOREGROUND_BLUE
|FOREGROUND_GREEN
|FOREGROUND_RED
144 static enum COLORS screen_color
[] = {
145 BLACK
, RED
, GREEN
, BROWN
,
146 BLUE
, MAGENTA
, CYAN
, LIGHTGRAY
150 for (anchor
= p_next
= obuf
;
151 (p_next
= memchr(p_next
, ESC
, ob
- p_next
)) != NULL
; )
154 if (p
[1] == '[') /* "ESC-[" sequence */
159 * If some chars seen since
160 * the last escape sequence,
161 * write them out to the screen.
163 WIN32textout(anchor
, p
-anchor
);
166 p
+= 2; /* Skip the "ESC-[" */
170 * Handle null escape sequence
171 * "ESC[m", which restores
177 WIN32setcolors(nm_fg_color
, nm_bg_color
);
183 * Select foreground/background colors
184 * based on the escape sequence.
188 while (!is_ansi_end(*p
))
191 long code
= strtol(p
, &q
, 10);
196 * Incomplete sequence.
197 * Leave it unprocessed
200 int slop
= (int) (q
- anchor
);
201 /* {{ strcpy args overlap! }} */
202 strcpy(obuf
, anchor
);
208 code
> 49 || code
< 0 ||
209 (!is_ansi_end(*q
) && *q
!= ';'))
220 /* case 0: all attrs off */
225 case 1: /* bold on */
228 case 3: /* italic on */
229 case 7: /* inverse on */
232 case 4: /* underline on */
235 case 5: /* slow blink on */
236 case 6: /* fast blink on */
239 case 8: /* concealed on */
242 case 22: /* bold off */
245 case 23: /* italic off */
246 case 27: /* inverse off */
249 case 24: /* underline off */
252 case 30: case 31: case 32:
253 case 33: case 34: case 35:
255 fg
= (fg
& 8) | (screen_color
[code
- 30]);
257 case 39: /* default fg */
260 case 40: case 41: case 42:
261 case 43: case 44: case 45:
263 bg
= (bg
& 8) | (screen_color
[code
- 40]);
265 case 49: /* default fg */
271 if (!is_ansi_end(*p
) || p
== p_next
)
276 * If \e[1m use defined bold
277 * color, else set intensity.
281 #if MSDOS_COMPILER==WIN32C
282 fg
|= FOREGROUND_INTENSITY
;
283 bg
|= BACKGROUND_INTENSITY
;
305 WIN32setcolors(fg
, bg
);
306 p_next
= anchor
= p
+ 1;
311 /* Output what's left in the buffer. */
312 WIN32textout(anchor
, ob
- anchor
);
319 fd
= (any_display
) ? 1 : 2;
320 if (write(fd
, obuf
, n
) != n
)
326 * Output a character.
332 #if 0 /* fake UTF-8 output for testing */
336 static char ubuf
[MAX_UTF_CHAR_LEN
];
337 static int ubuf_len
= 0;
338 static int ubuf_index
= 0;
341 ubuf_len
= utf_len(c
);
344 ubuf
[ubuf_index
++] = c
;
345 if (ubuf_index
< ubuf_len
)
347 c
= get_wchar(ubuf
) & 0xFF;
357 if (c
== '\n' && is_tty
)
364 if (c
== '\n' && is_tty
) /* In OS-9, '\n' == 0x0D */
369 * Some versions of flush() write to *ob, so we must flush
370 * when we are still one char from the end of obuf.
372 if (ob
>= &obuf
[sizeof(obuf
)-1])
392 * Convert an integral type to a string.
394 #define TYPE_TO_A_FUNC(funcname, type) \
395 void funcname(num, buf) \
399 int neg = (num < 0); \
400 char tbuf[INT_STRLEN_BOUND(num)+2]; \
401 register char *s = tbuf + sizeof(tbuf); \
402 if (neg) num = -num; \
405 *--s = (num % 10) + '0'; \
406 } while ((num /= 10) != 0); \
407 if (neg) *--s = '-'; \
411 TYPE_TO_A_FUNC(postoa
, POSITION
)
412 TYPE_TO_A_FUNC(linenumtoa
, LINENUM
)
413 TYPE_TO_A_FUNC(inttoa
, int)
416 * Output an integer in a given radix.
422 char buf
[INT_STRLEN_BOUND(num
)];
426 return ((int) strlen(buf
));
430 * Output a line number in a given radix.
436 char buf
[INT_STRLEN_BOUND(num
)];
438 linenumtoa(num
, buf
);
440 return ((int) strlen(buf
));
444 * This function implements printf-like functionality
445 * using a more portable argument list mechanism than printf's.
448 less_printf(fmt
, parg
)
477 col
+= iprint_int(parg
->p_int
);
481 col
+= iprint_linenum(parg
->p_linenum
);
492 * If some other non-trivial char is pressed, unget it, so it will
493 * become the next command.
501 while ((c
= getchr()) != '\n' && c
!= '\r')
505 if (c
!= '\n' && c
!= '\r' && c
!= ' ' && c
!= READ_INTR
)
511 * Output a message in the lower left corner of the screen
512 * and wait for carriage return.
520 static char return_to_continue
[] = " (press RETURN)";
524 if (any_display
&& is_tty
)
530 at_enter(AT_STANDOUT
);
534 col
+= less_printf(fmt
, parg
);
536 if (!(any_display
&& is_tty
))
542 putstr(return_to_continue
);
544 col
+= sizeof(return_to_continue
) + so_e_width
;
552 * Printing the message has probably scrolled the screen.
553 * {{ Unless the terminal doesn't have auto margins,
554 * in which case we just hammered on the right margin. }}
561 static char intr_to_abort
[] = "... (interrupt to abort)";
564 * Output a message in the lower left corner of the screen
565 * and don't wait for carriage return.
566 * Usually used to warn that we are beginning a potentially
567 * time-consuming operation.
576 at_enter(AT_STANDOUT
);
577 (void) less_printf(fmt
, parg
);
578 putstr(intr_to_abort
);
585 * Output a message in the lower left corner of the screen
586 * and return a single-character response.
596 if (any_display
&& is_tty
)
599 (void) less_printf(fmt
, parg
);
602 if (!(any_display
&& is_tty
))