2 * Copyright (C) 1984-2024 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 * Primitives for displaying the file on the screen,
13 * scrolling either forward or backward.
19 public lbool squished
;
20 public int no_back_scroll
= 0;
21 public int forw_prompt
;
22 public int first_time
= 1;
23 public lbool no_eof_bell
= FALSE
;
26 extern int top_scroll
;
28 extern int sc_width
, sc_height
;
31 extern lbool plusoption
;
32 extern int forw_scroll
;
33 extern int back_scroll
;
34 extern int ignore_eoi
;
35 extern int header_lines
;
36 extern int header_cols
;
37 extern int full_screen
;
38 extern POSITION header_start_pos
;
40 extern size_t size_linebuf
;
41 extern int hilite_search
;
42 extern int status_col
;
45 extern char *tagoption
;
49 * Sound the bell to indicate user is trying to move past end of file.
51 public void eof_bell(void)
56 static time_type last_eof_bell
= 0;
57 time_type now
= get_time();
58 if (now
== last_eof_bell
) /* max once per second */
62 if (quiet
== NOT_QUIET
)
69 * Check to see if the end of file is currently displayed.
71 public lbool
eof_displayed(void)
78 if (ch_length() == NULL_POSITION
)
80 * If the file length is not known,
81 * we can't possibly be displaying EOF.
86 * If the bottom line is empty, we are at EOF.
87 * If the bottom line ends at the file length,
88 * we must be just at EOF.
90 pos
= position(BOTTOM_PLUS_ONE
);
91 return (pos
== NULL_POSITION
|| pos
== ch_length());
95 * Check to see if the entire file is currently displayed.
97 public lbool
entire_file_displayed(void)
101 /* Make sure last line of file is displayed. */
102 if (!eof_displayed())
105 /* Make sure first line of file is displayed. */
107 return (pos
== NULL_POSITION
|| pos
== 0);
111 * If the screen is "squished", repaint it.
112 * "Squished" means the first displayed line is not at the top
113 * of the screen; this can happen when we display a short file
114 * for the first time.
116 public void squish_check(void)
125 * Read the first pfx columns of the next line.
126 * If skipeol==0 stop there, otherwise read and discard chars to end of line.
128 static POSITION
forw_line_pfx(POSITION pos
, int pfx
, int skipeol
)
130 int save_sc_width
= sc_width
;
131 int save_auto_wrap
= auto_wrap
;
132 int save_hshift
= hshift
;
133 /* Set fake sc_width to force only pfx chars to be read. */
134 sc_width
= pfx
+ line_pfx_width();
137 pos
= forw_line_seg(pos
, skipeol
, FALSE
, FALSE
);
138 sc_width
= save_sc_width
;
139 auto_wrap
= save_auto_wrap
;
140 hshift
= save_hshift
;
145 * Set header text color.
146 * Underline last line of headers, but not at header_start_pos
147 * (where there is no gap between the last header line and the next line).
149 static void set_attr_header(int ln
)
151 set_attr_line(AT_COLOR_HEADER
);
152 if (ln
+1 == header_lines
&& position(0) != header_start_pos
)
153 set_attr_line(AT_UNDERLINE
);
157 * Display file headers, overlaying text already drawn
158 * at top and left of screen.
160 public int overlay_header(void)
165 if (header_lines
> 0)
167 /* Draw header_lines lines from start of file at top of screen. */
168 POSITION pos
= header_start_pos
;
170 for (ln
= 0; ln
< header_lines
; ++ln
)
172 pos
= forw_line(pos
);
181 /* Draw header_cols columns at left of each line. */
182 POSITION pos
= header_start_pos
;
184 for (ln
= 0; ln
< sc_height
-1; ++ln
)
186 if (ln
>= header_lines
) /* switch from header lines to normal lines */
188 if (pos
== NULL_POSITION
)
192 /* Need skipeol for all header lines except the last one. */
193 pos
= forw_line_pfx(pos
, header_cols
, ln
+1 < header_lines
);
206 * Display n lines, scrolling forward,
207 * starting at position pos in the input file.
208 * "force" means display the n lines even if we hit end of file.
209 * "only_last" means display only the last screenful if n > screen size.
210 * "nblank" is the number of blank lines to draw before the first
211 * real line. If nblank > 0, the pos must be NULL_POSITION.
212 * The first real line after the blanks will start at ch_zero().
214 public void forw(int n
, POSITION pos
, lbool force
, lbool only_last
, int nblank
)
219 pos
= after_header_pos(pos
);
223 * do_repaint tells us not to display anything till the end,
224 * then just repaint the entire screen.
225 * We repaint if we are supposed to display only the last
226 * screenful and the request is for more than a screenful.
227 * Also if the request exceeds the forward scroll limit
228 * (but not if the request is for exactly a screenful, since
229 * repainting itself involves scrolling forward a screenful).
231 do_repaint
= (only_last
&& n
> sc_height
-1) ||
232 (forw_scroll
>= 0 && n
> forw_scroll
&& n
!= sc_height
-1);
235 if (pos
!= NULL_POSITION
&& (hilite_search
== OPT_ONPLUS
|| is_filtering() || status_col
)) {
236 prep_hilite(pos
, pos
+ (POSITION
) (4*size_linebuf
), ignore_eoi
? 1 : -1);
237 pos
= next_unfiltered(pos
);
243 if (top_scroll
&& n
>= sc_height
- 1 && pos
!= ch_length())
246 * Start a new screen.
247 * {{ This is not really desirable if we happen
248 * to hit eof in the middle of this screen,
249 * but we don't yet know if that will happen. }}
258 if (pos
!= position(BOTTOM_PLUS_ONE
) || empty_screen())
261 * This is not contiguous with what is
262 * currently displayed. Clear the screen image
263 * (position table) and start a new screen.
272 } else if (!first_time
&& !is_filtering() && full_screen
)
274 putstr("...skipping...\n");
282 * Read the next line of input.
287 * Still drawing blanks; don't get a line
289 * If this is the last blank line, get ready to
290 * read a line starting at ch_zero() next time.
297 * Get the next line from the file.
299 pos
= forw_line(pos
);
301 pos
= next_unfiltered(pos
);
303 if (pos
== NULL_POSITION
)
306 * End of file: stop here unless the top line
307 * is still empty, or "force" is true.
308 * Even if force is true, stop when the last
309 * line in the file reaches the top of screen.
311 if (!force
&& position(TOP
) != NULL_POSITION
)
313 if (!empty_lines(0, 0) &&
314 !empty_lines(1, 1) &&
315 empty_lines(2, sc_height
-1))
320 * Add the position of the next line to the position table.
321 * Display the current line on the screen.
328 * If this is the first screen displayed and
329 * we hit an early EOF (i.e. before the requested
330 * number of lines), we "squish" the display down
331 * at the bottom of the screen.
332 * But don't do this if a + option or a -t option
333 * was given. These options can cause us to
334 * start the display after the beginning of the file,
335 * and it is not appropriate to squish in that case.
337 if (first_time
&& pos
== NULL_POSITION
&& !top_scroll
&&
338 header_lines
== 0 && header_cols
== 0 &&
350 * Can't call clear_eol here. The cursor might be at end of line
351 * on an ignaw terminal, so clear_eol would clear the last char
352 * of the current line instead of all of the next line.
353 * If we really need to do this on clear_bg terminals, we need
354 * to find a better way.
357 if (clear_bg
&& apply_at_specials(final_attr
) != AT_NORMAL
)
360 * Writing the last character on the last line
361 * of the display may have scrolled the screen.
362 * If we were in standout mode, clear_bg terminals
363 * will fill the new line with the standout color.
364 * Now we're in normal mode again, so clear the line.
371 if (nlines
== 0 && !ignore_eoi
)
378 /* lower_left(); {{ considered harmful? }} */
381 (void) currline(BOTTOM
);
385 * Display n lines, scrolling backward.
387 public void back(int n
, POSITION pos
, lbool force
, lbool only_last
)
393 do_repaint
= (n
> get_back_scroll() || (only_last
&& n
> sc_height
-1) || header_lines
> 0);
395 if (pos
!= NULL_POSITION
&& (hilite_search
== OPT_ONPLUS
|| is_filtering() || status_col
)) {
396 prep_hilite((pos
< (POSITION
) (3*size_linebuf
)) ? 0 : pos
- (POSITION
) (3*size_linebuf
), pos
, -1);
402 * Get the previous line of input.
405 pos
= prev_unfiltered(pos
);
407 pos
= back_line(pos
);
408 if (pos
== NULL_POSITION
)
411 * Beginning of file: stop here unless "force" is true.
416 if (pos
!= after_header_pos(pos
))
419 * Don't allow scrolling back to before the current header line.
424 * Add the position of the previous line to the position table.
425 * Display the line on the screen.
445 (void) currline(BOTTOM
);
449 * Display n more lines, forward.
450 * Start just after the line currently displayed at the bottom of the screen.
452 public void forward(int n
, lbool force
, lbool only_last
)
456 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE
))
459 * If the -e flag is set and we're trying to go
460 * forward from end-of-file, go on to the next file.
467 pos
= position(BOTTOM_PLUS_ONE
);
468 if (pos
== NULL_POSITION
&& (!force
|| empty_lines(2, sc_height
-1)))
473 * ignore_eoi is to support A_F_FOREVER.
474 * Back up until there is a line at the bottom
483 back(1, position(TOP
), 1, 0);
484 pos
= position(BOTTOM_PLUS_ONE
);
485 } while (pos
== NULL_POSITION
&& !ABORT_SIGS());
493 forw(n
, pos
, force
, only_last
, 0);
497 * Display n more lines, backward.
498 * Start just before the line currently displayed at the top of the screen.
500 public void backward(int n
, lbool force
, lbool only_last
)
505 if (pos
== NULL_POSITION
&& (!force
|| position(BOTTOM
) == 0))
510 back(n
, pos
, force
, only_last
);
514 * Get the backwards scroll limit.
515 * Must call this function instead of just using the value of
516 * back_scroll, because the default case depends on sc_height and
517 * top_scroll, as well as back_scroll.
519 public int get_back_scroll(void)
523 if (back_scroll
>= 0)
524 return (back_scroll
);
526 return (sc_height
- 2);
527 return (10000); /* infinity */
531 * Will the entire file fit on one screen?
533 public int get_one_screen(void)
536 POSITION pos
= ch_zero();
538 for (nlines
= 0; nlines
< sc_height
; nlines
++)
540 pos
= forw_line(pos
);
541 if (pos
== NULL_POSITION
) break;
543 return (nlines
< sc_height
);