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.
13 * High level routines dealing with the output to the screen.
17 #if MSDOS_COMPILER==WIN32C
21 public int errmsgs
; /* Count of messages displayed by error() */
23 public int final_attr
;
28 extern int so_s_width
, so_e_width
;
29 extern int screen_trashed
;
30 extern int any_display
;
34 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
36 extern int nm_fg_color
, nm_bg_color
;
37 extern int bo_fg_color
, bo_bg_color
;
38 extern int ul_fg_color
, ul_bg_color
;
39 extern int so_fg_color
, so_bg_color
;
40 extern int bl_fg_color
, bl_bg_color
;
44 * Display the line which is in the line buffer.
56 * Don't output if a signal is pending.
62 final_attr
= AT_NORMAL
;
64 for (i
= 0; (c
= gline(i
, &a
)) != '\0'; i
++)
77 static char obuf
[OUTBUF_SIZE
];
78 static char *ob
= obuf
;
81 * Flush buffered output.
83 * If we haven't displayed any file data yet,
84 * output messages on error output (file descriptor 2),
85 * otherwise output on standard output (file descriptor 1).
87 * This has the desirable effect of producing all
88 * error messages on error output if standard output
89 * is directed to a file. It also does the same if
90 * we never produce any real output; for example, if
91 * the input file(s) cannot be opened. If we do
92 * eventually produce output, code in edit() makes
93 * sure these messages can be seen before they are
94 * overwritten or scrolled away.
105 #if MSDOS_COMPILER==WIN32C
106 if (is_tty
&& any_display
)
110 CONSOLE_SCREEN_BUFFER_INFO scr
;
114 extern HANDLE con_out
;
118 * There is a bug in Win32 WriteConsole() if we're
119 * writing in the last cell with a different color.
120 * To avoid color problems in the bottom line,
121 * we scroll the screen manually, before writing.
123 GetConsoleScreenBufferInfo(con_out
, &scr
);
124 col
= scr
.dwCursorPosition
.X
;
125 row
= scr
.dwCursorPosition
.Y
;
126 for (op
= obuf
; op
< obuf
+ olen
; op
++)
132 } else if (*op
== '\r')
145 if (row
> scr
.srWindow
.Bottom
)
146 win32_scroll_up(row
- scr
.srWindow
.Bottom
);
147 WriteConsole(con_out
, obuf
, olen
, &nwritten
, NULL
);
152 #if MSDOS_COMPILER==MSOFTC
153 if (is_tty
&& any_display
)
161 #if MSDOS_COMPILER==BORLANDC || MSDOS_COMPILER==DJGPPC
162 if (is_tty
&& any_display
)
165 if (ctldisp
!= OPT_ONPLUS
)
170 * Look for SGR escape sequences, and convert them
171 * to color commands. Replace bold, underline,
172 * and italic escapes into colors specified via
173 * the -D command-line option.
175 char *anchor
, *p
, *p_next
;
176 int buflen
= ob
- obuf
;
177 unsigned char fg
, bg
, norm_attr
;
179 * Only dark colors mentioned here, so that
180 * bold has visible effect.
182 static enum COLORS screen_color
[] = {
183 BLACK
, RED
, GREEN
, BROWN
,
184 BLUE
, MAGENTA
, CYAN
, LIGHTGRAY
187 /* Normal text colors are used as baseline. */
188 bg
= nm_bg_color
& 0xf;
189 fg
= nm_fg_color
& 0xf;
190 norm_attr
= (bg
<< 4) | fg
;
191 for (anchor
= p_next
= obuf
;
192 (p_next
= memchr (p_next
, ESC
,
193 buflen
- (p_next
- obuf
)))
199 * Handle the null escape sequence
200 * (ESC-[m), which is used to restore
201 * the original color.
203 if (p
[1] == '[' && is_ansi_end(p
[2]))
211 if (p
[1] == '[') /* "Esc-[" sequence */
214 * If some chars seen since
215 * the last escape sequence,
216 * write it out to the screen
217 * using current text attributes.
228 while (!is_ansi_end(*p
))
231 long code
= strtol(p
, &q
, 10);
236 * Incomplete sequence.
237 * Leave it unprocessed
240 int slop
= q
- anchor
;
241 strcpy(obuf
, anchor
);
247 || code
> 49 || code
< 0
259 case 1: /* bold on */
263 case 3: /* italic on */
267 case 4: /* underline on */
271 case 8: /* concealed on */
274 case 0: /* all attrs off */
275 case 22:/* bold off */
276 case 23:/* italic off */
277 case 24:/* underline off */
281 case 30: case 31: case 32:
282 case 33: case 34: case 35:
284 fg
= (fg
& 8) | (screen_color
[code
- 30]);
286 case 39: /* default fg */
289 case 40: case 41: case 42:
290 case 43: case 44: case 45:
292 bg
= (bg
& 8) | (screen_color
[code
- 40]);
294 case 49: /* default fg */
300 if (is_ansi_end(*p
) && p
> p_next
)
304 textattr ((bg
<< 4)| fg
);
305 p_next
= anchor
= p
+ 1;
312 /* Output what's left in the buffer. */
321 fd
= (any_display
) ? 1 : 2;
322 if (write(fd
, obuf
, n
) != n
)
328 * Output a character.
334 #if 0 /* fake UTF-8 output for testing */
338 static char ubuf
[MAX_UTF_CHAR_LEN
];
339 static int ubuf_len
= 0;
340 static int ubuf_index
= 0;
343 ubuf_len
= utf_len(c
);
346 ubuf
[ubuf_index
++] = c
;
347 if (ubuf_index
< ubuf_len
)
349 c
= get_wchar(ubuf
) & 0xFF;
359 if (c
== '\n' && is_tty
)
366 if (c
== '\n' && is_tty
) /* In OS-9, '\n' == 0x0D */
371 * Some versions of flush() write to *ob, so we must flush
372 * when we are still one char from the end of obuf.
374 if (ob
>= &obuf
[sizeof(obuf
)-1])
394 * Convert an integral type to a string.
396 #define TYPE_TO_A_FUNC(funcname, type) \
397 void funcname(num, buf) \
401 int neg = (num < 0); \
402 char tbuf[INT_STRLEN_BOUND(num)+2]; \
403 register char *s = tbuf + sizeof(tbuf); \
404 if (neg) num = -num; \
407 *--s = (num % 10) + '0'; \
408 } while ((num /= 10) != 0); \
409 if (neg) *--s = '-'; \
413 TYPE_TO_A_FUNC(postoa
, POSITION
)
414 TYPE_TO_A_FUNC(linenumtoa
, LINENUM
)
415 TYPE_TO_A_FUNC(inttoa
, int)
418 * Output an integer in a given radix.
424 char buf
[INT_STRLEN_BOUND(num
)];
428 return (strlen(buf
));
432 * Output a line number in a given radix.
438 char buf
[INT_STRLEN_BOUND(num
)];
440 linenumtoa(num
, buf
);
442 return (strlen(buf
));
446 * This function implements printf-like functionality
447 * using a more portable argument list mechanism than printf's.
450 less_printf(fmt
, parg
)
479 col
+= iprint_int(parg
->p_int
);
483 col
+= iprint_linenum(parg
->p_linenum
);
494 * If some other non-trivial char is pressed, unget it, so it will
495 * become the next command.
503 while ((c
= getchr()) != '\n' && c
!= '\r')
507 if (c
!= '\n' && c
!= '\r' && c
!= ' ' && c
!= READ_INTR
)
513 * Output a message in the lower left corner of the screen
514 * and wait for carriage return.
522 static char return_to_continue
[] = " (press RETURN)";
526 if (any_display
&& is_tty
)
532 at_enter(AT_STANDOUT
);
536 col
+= less_printf(fmt
, parg
);
538 if (!(any_display
&& is_tty
))
544 putstr(return_to_continue
);
546 col
+= sizeof(return_to_continue
) + so_e_width
;
553 * Printing the message has probably scrolled the screen.
554 * {{ Unless the terminal doesn't have auto margins,
555 * in which case we just hammered on the right margin. }}
562 static char intr_to_abort
[] = "... (interrupt to abort)";
565 * Output a message in the lower left corner of the screen
566 * and don't wait for carriage return.
567 * Usually used to warn that we are beginning a potentially
568 * time-consuming operation.
577 at_enter(AT_STANDOUT
);
578 (void) less_printf(fmt
, parg
);
579 putstr(intr_to_abort
);
586 * Output a message in the lower left corner of the screen
587 * and return a single-character response.
597 if (any_display
&& is_tty
)
600 (void) less_printf(fmt
, parg
);
603 if (!(any_display
&& is_tty
))