3 * Copyright (C) 1984-2007 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 about less, or for information on how to
9 * contact the author, see the README file.
14 * Primitives for displaying the file on the screen,
15 * scrolling either forward or backward.
21 public int hit_eof; /* Keeps track of how many times we hit end of file */
22 public int screen_trashed;
24 public int no_back_scroll = 0;
25 public int forw_prompt;
28 extern int top_scroll;
30 extern int sc_width, sc_height;
31 extern int less_is_more;
32 extern int plusoption;
33 extern int forw_scroll;
34 extern int back_scroll;
35 extern int ignore_eoi;
37 extern int final_attr;
40 extern char *tagoption;
44 * Sound the bell to indicate user is trying to move past end of file.
49 if (quiet == NOT_QUIET)
56 * Check to see if the end of file is currently "displayed".
68 * If the bottom line is empty, we are at EOF.
69 * If the bottom line ends at the file length,
70 * we must be just at EOF.
72 pos = position(BOTTOM_PLUS_ONE);
73 if (pos == NULL_POSITION || pos == ch_length())
78 * If the screen is "squished", repaint it.
79 * "Squished" means the first displayed line is not at the top
80 * of the screen; this can happen when we display a short file
93 * Display n lines, scrolling forward,
94 * starting at position pos in the input file.
95 * "force" means display the n lines even if we hit end of file.
96 * "only_last" means display only the last screenful if n > screen size.
97 * "nblank" is the number of blank lines to draw before the first
98 * real line. If nblank > 0, the pos must be NULL_POSITION.
99 * The first real line after the blanks will start at ch_zero().
102 forw(n, pos, force, only_last, nblank)
112 static int first_time = 1;
117 * do_repaint tells us not to display anything till the end,
118 * then just repaint the entire screen.
119 * We repaint if we are supposed to display only the last
120 * screenful and the request is for more than a screenful.
121 * Also if the request exceeds the forward scroll limit
122 * (but not if the request is for exactly a screenful, since
123 * repainting itself involves scrolling forward a screenful).
125 do_repaint = (only_last && n > sc_height-1) ||
126 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
130 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
133 * Start a new screen.
134 * {{ This is not really desirable if we happen
135 * to hit eof in the middle of this screen,
136 * but we don't yet know if that will happen. }}
141 if (less_is_more == 0) {
150 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
153 * This is not contiguous with what is
154 * currently displayed. Clear the screen image
155 * (position table) and start a new screen.
164 } else if (!first_time)
166 putstr("...skipping...\n");
174 * Read the next line of input.
179 * Still drawing blanks; don't get a line
181 * If this is the last blank line, get ready to
182 * read a line starting at ch_zero() next time.
189 * Get the next line from the file.
191 pos = forw_line(pos);
192 if (pos == NULL_POSITION)
195 * End of file: stop here unless the top line
196 * is still empty, or "force" is true.
197 * Even if force is true, stop when the last
198 * line in the file reaches the top of screen.
201 if (!force && position(TOP) != NULL_POSITION)
203 if (!empty_lines(0, 0) &&
204 !empty_lines(1, 1) &&
205 empty_lines(2, sc_height-1))
210 * Add the position of the next line to the position table.
211 * Display the current line on the screen.
218 * If this is the first screen displayed and
219 * we hit an early EOF (i.e. before the requested
220 * number of lines), we "squish" the display down
221 * at the bottom of the screen.
222 * But don't do this if a + option or a -t option
223 * was given. These options can cause us to
224 * start the display after the beginning of the file,
225 * and it is not appropriate to squish in that case.
227 if ((first_time || less_is_more) &&
228 pos == NULL_POSITION && !top_scroll &&
240 * Can't call clear_eol here. The cursor might be at end of line
241 * on an ignaw terminal, so clear_eol would clear the last char
242 * of the current line instead of all of the next line.
243 * If we really need to do this on clear_bg terminals, we need
244 * to find a better way.
247 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
250 * Writing the last character on the last line
251 * of the display may have scrolled the screen.
252 * If we were in standout mode, clear_bg terminals
253 * will fill the new line with the standout color.
254 * Now we're in normal mode again, so clear the line.
264 else if (eof && !ABORT_SIGS())
273 (void) currline(BOTTOM);
277 * Display n lines, scrolling backward.
280 back(n, pos, force, only_last)
290 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
295 * Get the previous line of input.
297 pos = back_line(pos);
298 if (pos == NULL_POSITION)
301 * Beginning of file: stop here unless "force" is true.
307 * Add the position of the previous line to the position table.
308 * Display the line on the screen.
327 (void) currline(BOTTOM);
331 * Display n more lines, forward.
332 * Start just after the line currently displayed at the bottom of the screen.
335 forward(n, force, only_last)
342 if (get_quit_at_eof() && hit_eof && !(ch_getflags() & CH_HELPFILE))
345 * If the -e flag is set and we're trying to go
346 * forward from end-of-file, go on to the next file.
353 pos = position(BOTTOM_PLUS_ONE);
354 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
359 * ignore_eoi is to support A_F_FOREVER.
360 * Back up until there is a line at the bottom
369 back(1, position(TOP), 1, 0);
370 pos = position(BOTTOM_PLUS_ONE);
371 } while (pos == NULL_POSITION);
380 forw(n, pos, force, only_last, 0);
384 * Display n more lines, backward.
385 * Start just before the line currently displayed at the top of the screen.
388 backward(n, force, only_last)
396 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
401 back(n, pos, force, only_last);
405 * Get the backwards scroll limit.
406 * Must call this function instead of just using the value of
407 * back_scroll, because the default case depends on sc_height and
408 * top_scroll, as well as back_scroll.
415 if (back_scroll >= 0)
416 return (back_scroll);
418 return (sc_height - 2);
419 return (10000); /* infinity */