3 * Copyright (C) 1984-2021 Mark Nudelman
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
8 * For more information, see the README file.
13 * Primitives for displaying the file on the screen,
14 * scrolling either forward or backward.
20 public int screen_trashed;
22 public int no_back_scroll = 0;
23 public int forw_prompt;
26 extern int top_scroll;
28 extern int sc_width, sc_height;
29 extern int less_is_more;
30 extern int plusoption;
31 extern int forw_scroll;
32 extern int back_scroll;
33 extern int ignore_eoi;
35 extern int final_attr;
38 extern int size_linebuf;
39 extern int hilite_search;
40 extern int status_col;
43 extern char *tagoption;
47 * Sound the bell to indicate user is trying to move past end of file.
53 static time_type last_eof_bell = 0;
54 time_type now = get_time();
55 if (now == last_eof_bell) /* max once per second */
59 if (quiet == NOT_QUIET)
66 * Check to see if the end of file is currently displayed.
69 eof_displayed(VOID_PARAM)
76 if (ch_length() == NULL_POSITION)
78 * If the file length is not known,
79 * we can't possibly be displaying EOF.
84 * If the bottom line is empty, we are at EOF.
85 * If the bottom line ends at the file length,
86 * we must be just at EOF.
88 pos = position(BOTTOM_PLUS_ONE);
89 return (pos == NULL_POSITION || pos == ch_length());
93 * Check to see if the entire file is currently displayed.
96 entire_file_displayed(VOID_PARAM)
100 /* Make sure last line of file is displayed. */
101 if (!eof_displayed())
104 /* Make sure first line of file is displayed. */
106 return (pos == NULL_POSITION || pos == 0);
110 * If the screen is "squished", repaint it.
111 * "Squished" means the first displayed line is not at the top
112 * of the screen; this can happen when we display a short file
113 * for the first time.
116 squish_check(VOID_PARAM)
125 * Display n lines, scrolling forward,
126 * starting at position pos in the input file.
127 * "force" means display the n lines even if we hit end of file.
128 * "only_last" means display only the last screenful if n > screen size.
129 * "nblank" is the number of blank lines to draw before the first
130 * real line. If nblank > 0, the pos must be NULL_POSITION.
131 * The first real line after the blanks will start at ch_zero().
134 forw(n, pos, force, only_last, nblank)
143 static int first_time = 1;
148 * do_repaint tells us not to display anything till the end,
149 * then just repaint the entire screen.
150 * We repaint if we are supposed to display only the last
151 * screenful and the request is for more than a screenful.
152 * Also if the request exceeds the forward scroll limit
153 * (but not if the request is for exactly a screenful, since
154 * repainting itself involves scrolling forward a screenful).
156 do_repaint = (only_last && n > sc_height-1) ||
157 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
160 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
161 prep_hilite(pos, pos + 4*size_linebuf, ignore_eoi ? 1 : -1);
162 pos = next_unfiltered(pos);
168 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
171 * Start a new screen.
172 * {{ This is not really desirable if we happen
173 * to hit eof in the middle of this screen,
174 * but we don't yet know if that will happen. }}
179 if (less_is_more == 0) {
185 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
188 * This is not contiguous with what is
189 * currently displayed. Clear the screen image
190 * (position table) and start a new screen.
199 } else if (!first_time && !is_filtering())
201 putstr("...skipping...\n");
209 * Read the next line of input.
214 * Still drawing blanks; don't get a line
216 * If this is the last blank line, get ready to
217 * read a line starting at ch_zero() next time.
224 * Get the next line from the file.
226 pos = forw_line(pos);
228 pos = next_unfiltered(pos);
230 if (pos == NULL_POSITION)
233 * End of file: stop here unless the top line
234 * is still empty, or "force" is true.
235 * Even if force is true, stop when the last
236 * line in the file reaches the top of screen.
238 if (!force && position(TOP) != NULL_POSITION)
240 if (!empty_lines(0, 0) &&
241 !empty_lines(1, 1) &&
242 empty_lines(2, sc_height-1))
247 * Add the position of the next line to the position table.
248 * Display the current line on the screen.
255 * If this is the first screen displayed and
256 * we hit an early EOF (i.e. before the requested
257 * number of lines), we "squish" the display down
258 * at the bottom of the screen.
259 * But don't do this if a + option or a -t option
260 * was given. These options can cause us to
261 * start the display after the beginning of the file,
262 * and it is not appropriate to squish in that case.
264 if ((first_time || less_is_more) &&
265 pos == NULL_POSITION && !top_scroll &&
277 * Can't call clear_eol here. The cursor might be at end of line
278 * on an ignaw terminal, so clear_eol would clear the last char
279 * of the current line instead of all of the next line.
280 * If we really need to do this on clear_bg terminals, we need
281 * to find a better way.
284 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
287 * Writing the last character on the last line
288 * of the display may have scrolled the screen.
289 * If we were in standout mode, clear_bg terminals
290 * will fill the new line with the standout color.
291 * Now we're in normal mode again, so clear the line.
299 if (nlines == 0 && !ignore_eoi)
304 (void) currline(BOTTOM);
308 * Display n lines, scrolling backward.
311 back(n, pos, force, only_last)
321 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
323 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
324 prep_hilite((pos < 3*size_linebuf) ? 0 : pos - 3*size_linebuf, pos, -1);
330 * Get the previous line of input.
333 pos = prev_unfiltered(pos);
336 pos = back_line(pos);
337 if (pos == NULL_POSITION)
340 * Beginning of file: stop here unless "force" is true.
346 * Add the position of the previous line to the position table.
347 * Display the line on the screen.
365 (void) currline(BOTTOM);
369 * Display n more lines, forward.
370 * Start just after the line currently displayed at the bottom of the screen.
373 forward(n, force, only_last)
380 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
383 * If the -e flag is set and we're trying to go
384 * forward from end-of-file, go on to the next file.
391 pos = position(BOTTOM_PLUS_ONE);
392 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
397 * ignore_eoi is to support A_F_FOREVER.
398 * Back up until there is a line at the bottom
407 back(1, position(TOP), 1, 0);
408 pos = position(BOTTOM_PLUS_ONE);
409 } while (pos == NULL_POSITION);
417 forw(n, pos, force, only_last, 0);
421 * Display n more lines, backward.
422 * Start just before the line currently displayed at the top of the screen.
425 backward(n, force, only_last)
433 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
438 back(n, pos, force, only_last);
442 * Get the backwards scroll limit.
443 * Must call this function instead of just using the value of
444 * back_scroll, because the default case depends on sc_height and
445 * top_scroll, as well as back_scroll.
448 get_back_scroll(VOID_PARAM)
452 if (back_scroll >= 0)
453 return (back_scroll);
455 return (sc_height - 2);
456 return (10000); /* infinity */
460 * Will the entire file fit on one screen?
463 get_one_screen(VOID_PARAM)
466 POSITION pos = ch_zero();
468 for (nlines = 0; nlines < sc_height; nlines++)
470 pos = forw_line(pos);
471 if (pos == NULL_POSITION) break;
473 return (nlines < sc_height);