3 * Copyright (C) 1984-2005 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;
27 extern int top_scroll;
29 extern int sc_width, sc_height;
30 extern int quit_at_eof;
32 extern int plusoption;
33 extern int forw_scroll;
34 extern int back_scroll;
35 extern int ignore_eoi;
37 extern int final_attr;
39 extern char *tagoption;
43 * Sound the bell to indicate user is trying to move past end of file.
48 if (quiet == NOT_QUIET)
55 * Check to see if the end of file is currently "displayed".
67 * If the bottom line is empty, we are at EOF.
68 * If the bottom line ends at the file length,
69 * we must be just at EOF.
71 pos = position(BOTTOM_PLUS_ONE);
72 if (pos == NULL_POSITION || pos == ch_length())
77 * If the screen is "squished", repaint it.
78 * "Squished" means the first displayed line is not at the top
79 * of the screen; this can happen when we display a short file
92 * Display n lines, scrolling forward,
93 * starting at position pos in the input file.
94 * "force" means display the n lines even if we hit end of file.
95 * "only_last" means display only the last screenful if n > screen size.
96 * "nblank" is the number of blank lines to draw before the first
97 * real line. If nblank > 0, the pos must be NULL_POSITION.
98 * The first real line after the blanks will start at ch_zero().
101 forw(n, pos, force, only_last, nblank)
111 static int first_time = 1;
116 * do_repaint tells us not to display anything till the end,
117 * then just repaint the entire screen.
118 * We repaint if we are supposed to display only the last
119 * screenful and the request is for more than a screenful.
120 * Also if the request exceeds the forward scroll limit
121 * (but not if the request is for exactly a screenful, since
122 * repainting itself involves scrolling forward a screenful).
124 do_repaint = (only_last && n > sc_height-1) ||
125 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
129 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
132 * Start a new screen.
133 * {{ This is not really desirable if we happen
134 * to hit eof in the middle of this screen,
135 * but we don't yet know if that will happen. }}
142 if (top_scroll == OPT_ONPLUS || (first_time && top_scroll != OPT_ON))
151 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
154 * This is not contiguous with what is
155 * currently displayed. Clear the screen image
156 * (position table) and start a new screen.
163 if (top_scroll == OPT_ONPLUS)
166 } else if (!first_time)
168 putstr("...skipping...\n");
176 * Read the next line of input.
181 * Still drawing blanks; don't get a line
183 * If this is the last blank line, get ready to
184 * read a line starting at ch_zero() next time.
191 * Get the next line from the file.
193 pos = forw_line(pos);
194 if (pos == NULL_POSITION)
197 * End of file: stop here unless the top line
198 * is still empty, or "force" is true.
199 * Even if force is true, stop when the last
200 * line in the file reaches the top of screen.
203 if (!force && position(TOP) != NULL_POSITION)
205 if (!empty_lines(0, 0) &&
206 !empty_lines(1, 1) &&
207 empty_lines(2, sc_height-1))
212 * Add the position of the next line to the position table.
213 * Display the current line on the screen.
220 * If this is the first screen displayed and
221 * we hit an early EOF (i.e. before the requested
222 * number of lines), we "squish" the display down
223 * at the bottom of the screen.
224 * But don't do this if a + option or a -t option
225 * was given. These options can cause us to
226 * start the display after the beginning of the file,
227 * and it is not appropriate to squish in that case.
229 if ((first_time || more_mode) &&
230 pos == NULL_POSITION && !top_scroll &&
239 if (top_scroll == OPT_ON)
242 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
245 * Writing the last character on the last line
246 * of the display may have scrolled the screen.
247 * If we were in standout mode, clear_bg terminals
248 * will fill the new line with the standout color.
249 * Now we're in normal mode again, so clear the line.
257 else if (eof && !ABORT_SIGS())
266 (void) currline(BOTTOM);
270 * Display n lines, scrolling backward.
273 back(n, pos, force, only_last)
283 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
288 * Get the previous line of input.
290 pos = back_line(pos);
291 if (pos == NULL_POSITION)
294 * Beginning of file: stop here unless "force" is true.
300 * Add the position of the previous line to the position table.
301 * Display the line on the screen.
318 (void) currline(BOTTOM);
322 * Display n more lines, forward.
323 * Start just after the line currently displayed at the bottom of the screen.
326 forward(n, force, only_last)
333 if (quit_at_eof && hit_eof && !(ch_getflags() & CH_HELPFILE))
336 * If the -e flag is set and we're trying to go
337 * forward from end-of-file, go on to the next file.
344 pos = position(BOTTOM_PLUS_ONE);
345 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
350 * ignore_eoi is to support A_F_FOREVER.
351 * Back up until there is a line at the bottom
360 back(1, position(TOP), 1, 0);
361 pos = position(BOTTOM_PLUS_ONE);
362 } while (pos == NULL_POSITION);
371 forw(n, pos, force, only_last, 0);
375 * Display n more lines, backward.
376 * Start just before the line currently displayed at the top of the screen.
379 backward(n, force, only_last)
387 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
392 back(n, pos, force, only_last);
396 * Get the backwards scroll limit.
397 * Must call this function instead of just using the value of
398 * back_scroll, because the default case depends on sc_height and
399 * top_scroll, as well as back_scroll.
406 if (back_scroll >= 0)
407 return (back_scroll);
409 return (sc_height - 2);
410 return (10000); /* infinity */