3 * Copyright (C) 1984-2020 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.
52 if (quiet == NOT_QUIET)
59 * Check to see if the end of file is currently displayed.
62 eof_displayed(VOID_PARAM)
69 if (ch_length() == NULL_POSITION)
71 * If the file length is not known,
72 * we can't possibly be displaying EOF.
77 * If the bottom line is empty, we are at EOF.
78 * If the bottom line ends at the file length,
79 * we must be just at EOF.
81 pos = position(BOTTOM_PLUS_ONE);
82 return (pos == NULL_POSITION || pos == ch_length());
86 * Check to see if the entire file is currently displayed.
89 entire_file_displayed(VOID_PARAM)
93 /* Make sure last line of file is displayed. */
97 /* Make sure first line of file is displayed. */
99 return (pos == NULL_POSITION || pos == 0);
103 * If the screen is "squished", repaint it.
104 * "Squished" means the first displayed line is not at the top
105 * of the screen; this can happen when we display a short file
106 * for the first time.
109 squish_check(VOID_PARAM)
118 * Display n lines, scrolling forward,
119 * starting at position pos in the input file.
120 * "force" means display the n lines even if we hit end of file.
121 * "only_last" means display only the last screenful if n > screen size.
122 * "nblank" is the number of blank lines to draw before the first
123 * real line. If nblank > 0, the pos must be NULL_POSITION.
124 * The first real line after the blanks will start at ch_zero().
127 forw(n, pos, force, only_last, nblank)
136 static int first_time = 1;
141 * do_repaint tells us not to display anything till the end,
142 * then just repaint the entire screen.
143 * We repaint if we are supposed to display only the last
144 * screenful and the request is for more than a screenful.
145 * Also if the request exceeds the forward scroll limit
146 * (but not if the request is for exactly a screenful, since
147 * repainting itself involves scrolling forward a screenful).
149 do_repaint = (only_last && n > sc_height-1) ||
150 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
153 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
154 prep_hilite(pos, pos + 4*size_linebuf, ignore_eoi ? 1 : -1);
155 pos = next_unfiltered(pos);
161 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
164 * Start a new screen.
165 * {{ This is not really desirable if we happen
166 * to hit eof in the middle of this screen,
167 * but we don't yet know if that will happen. }}
172 if (less_is_more == 0) {
178 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
181 * This is not contiguous with what is
182 * currently displayed. Clear the screen image
183 * (position table) and start a new screen.
192 } else if (!first_time)
194 putstr("...skipping...\n");
202 * Read the next line of input.
207 * Still drawing blanks; don't get a line
209 * If this is the last blank line, get ready to
210 * read a line starting at ch_zero() next time.
217 * Get the next line from the file.
219 pos = forw_line(pos);
221 pos = next_unfiltered(pos);
223 if (pos == NULL_POSITION)
226 * End of file: stop here unless the top line
227 * is still empty, or "force" is true.
228 * Even if force is true, stop when the last
229 * line in the file reaches the top of screen.
231 if (!force && position(TOP) != NULL_POSITION)
233 if (!empty_lines(0, 0) &&
234 !empty_lines(1, 1) &&
235 empty_lines(2, sc_height-1))
240 * Add the position of the next line to the position table.
241 * Display the current line on the screen.
248 * If this is the first screen displayed and
249 * we hit an early EOF (i.e. before the requested
250 * number of lines), we "squish" the display down
251 * at the bottom of the screen.
252 * But don't do this if a + option or a -t option
253 * was given. These options can cause us to
254 * start the display after the beginning of the file,
255 * and it is not appropriate to squish in that case.
257 if ((first_time || less_is_more) &&
258 pos == NULL_POSITION && !top_scroll &&
270 * Can't call clear_eol here. The cursor might be at end of line
271 * on an ignaw terminal, so clear_eol would clear the last char
272 * of the current line instead of all of the next line.
273 * If we really need to do this on clear_bg terminals, we need
274 * to find a better way.
277 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
280 * Writing the last character on the last line
281 * of the display may have scrolled the screen.
282 * If we were in standout mode, clear_bg terminals
283 * will fill the new line with the standout color.
284 * Now we're in normal mode again, so clear the line.
292 if (nlines == 0 && !ignore_eoi)
297 (void) currline(BOTTOM);
301 * Display n lines, scrolling backward.
304 back(n, pos, force, only_last)
314 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
316 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
317 prep_hilite((pos < 3*size_linebuf) ? 0 : pos - 3*size_linebuf, pos, -1);
323 * Get the previous line of input.
326 pos = prev_unfiltered(pos);
329 pos = back_line(pos);
330 if (pos == NULL_POSITION)
333 * Beginning of file: stop here unless "force" is true.
339 * Add the position of the previous line to the position table.
340 * Display the line on the screen.
358 (void) currline(BOTTOM);
362 * Display n more lines, forward.
363 * Start just after the line currently displayed at the bottom of the screen.
366 forward(n, force, only_last)
373 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
376 * If the -e flag is set and we're trying to go
377 * forward from end-of-file, go on to the next file.
384 pos = position(BOTTOM_PLUS_ONE);
385 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
390 * ignore_eoi is to support A_F_FOREVER.
391 * Back up until there is a line at the bottom
400 back(1, position(TOP), 1, 0);
401 pos = position(BOTTOM_PLUS_ONE);
402 } while (pos == NULL_POSITION);
410 forw(n, pos, force, only_last, 0);
414 * Display n more lines, backward.
415 * Start just before the line currently displayed at the top of the screen.
418 backward(n, force, only_last)
426 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
431 back(n, pos, force, only_last);
435 * Get the backwards scroll limit.
436 * Must call this function instead of just using the value of
437 * back_scroll, because the default case depends on sc_height and
438 * top_scroll, as well as back_scroll.
441 get_back_scroll(VOID_PARAM)
445 if (back_scroll >= 0)
446 return (back_scroll);
448 return (sc_height - 2);
449 return (10000); /* infinity */
453 * Will the entire file fit on one screen?
456 get_one_screen(VOID_PARAM)
459 POSITION pos = ch_zero();
461 for (nlines = 0; nlines < sc_height; nlines++)
463 pos = forw_line(pos);
464 if (pos == NULL_POSITION) break;
466 return (nlines < sc_height);