3 * Copyright (C) 1984-2011 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 screen_trashed;
23 public int no_back_scroll = 0;
24 public int forw_prompt;
27 extern int top_scroll;
29 extern int sc_width, sc_height;
30 extern int less_is_more;
31 extern int plusoption;
32 extern int forw_scroll;
33 extern int back_scroll;
34 extern int ignore_eoi;
36 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.
65 if (ch_length() == NULL_POSITION)
67 * If the file length is not known,
68 * we can't possibly be displaying EOF.
73 * If the bottom line is empty, we are at EOF.
74 * If the bottom line ends at the file length,
75 * we must be just at EOF.
77 pos = position(BOTTOM_PLUS_ONE);
78 return (pos == NULL_POSITION || pos == ch_length());
82 * Check to see if the entire file is currently displayed.
85 entire_file_displayed()
89 /* Make sure last line of file is displayed. */
93 /* Make sure first line of file is displayed. */
95 return (pos == NULL_POSITION || pos == 0);
99 * If the screen is "squished", repaint it.
100 * "Squished" means the first displayed line is not at the top
101 * of the screen; this can happen when we display a short file
102 * for the first time.
114 * Display n lines, scrolling forward,
115 * starting at position pos in the input file.
116 * "force" means display the n lines even if we hit end of file.
117 * "only_last" means display only the last screenful if n > screen size.
118 * "nblank" is the number of blank lines to draw before the first
119 * real line. If nblank > 0, the pos must be NULL_POSITION.
120 * The first real line after the blanks will start at ch_zero().
123 forw(n, pos, force, only_last, nblank)
133 static int first_time = 1;
138 * do_repaint tells us not to display anything till the end,
139 * then just repaint the entire screen.
140 * We repaint if we are supposed to display only the last
141 * screenful and the request is for more than a screenful.
142 * Also if the request exceeds the forward scroll limit
143 * (but not if the request is for exactly a screenful, since
144 * repainting itself involves scrolling forward a screenful).
146 do_repaint = (only_last && n > sc_height-1) ||
147 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
151 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
154 * Start a new screen.
155 * {{ This is not really desirable if we happen
156 * to hit eof in the middle of this screen,
157 * but we don't yet know if that will happen. }}
162 if (less_is_more == 0) {
168 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
171 * This is not contiguous with what is
172 * currently displayed. Clear the screen image
173 * (position table) and start a new screen.
182 } else if (!first_time)
184 putstr("...skipping...\n");
192 * Read the next line of input.
197 * Still drawing blanks; don't get a line
199 * If this is the last blank line, get ready to
200 * read a line starting at ch_zero() next time.
207 * Get the next line from the file.
209 pos = forw_line(pos);
210 if (pos == NULL_POSITION)
213 * End of file: stop here unless the top line
214 * is still empty, or "force" is true.
215 * Even if force is true, stop when the last
216 * line in the file reaches the top of screen.
219 if (!force && position(TOP) != NULL_POSITION)
221 if (!empty_lines(0, 0) &&
222 !empty_lines(1, 1) &&
223 empty_lines(2, sc_height-1))
228 * Add the position of the next line to the position table.
229 * Display the current line on the screen.
236 * If this is the first screen displayed and
237 * we hit an early EOF (i.e. before the requested
238 * number of lines), we "squish" the display down
239 * at the bottom of the screen.
240 * But don't do this if a + option or a -t option
241 * was given. These options can cause us to
242 * start the display after the beginning of the file,
243 * and it is not appropriate to squish in that case.
245 if ((first_time || less_is_more) &&
246 pos == NULL_POSITION && !top_scroll &&
258 * Can't call clear_eol here. The cursor might be at end of line
259 * on an ignaw terminal, so clear_eol would clear the last char
260 * of the current line instead of all of the next line.
261 * If we really need to do this on clear_bg terminals, we need
262 * to find a better way.
265 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
268 * Writing the last character on the last line
269 * of the display may have scrolled the screen.
270 * If we were in standout mode, clear_bg terminals
271 * will fill the new line with the standout color.
272 * Now we're in normal mode again, so clear the line.
285 (void) currline(BOTTOM);
289 * Display n lines, scrolling backward.
292 back(n, pos, force, only_last)
302 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
306 * Get the previous line of input.
308 pos = back_line(pos);
309 if (pos == NULL_POSITION)
312 * Beginning of file: stop here unless "force" is true.
318 * Add the position of the previous line to the position table.
319 * Display the line on the screen.
337 (void) currline(BOTTOM);
341 * Display n more lines, forward.
342 * Start just after the line currently displayed at the bottom of the screen.
345 forward(n, force, only_last)
352 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
355 * If the -e flag is set and we're trying to go
356 * forward from end-of-file, go on to the next file.
363 pos = position(BOTTOM_PLUS_ONE);
364 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
369 * ignore_eoi is to support A_F_FOREVER.
370 * Back up until there is a line at the bottom
379 back(1, position(TOP), 1, 0);
380 pos = position(BOTTOM_PLUS_ONE);
381 } while (pos == NULL_POSITION);
389 forw(n, pos, force, only_last, 0);
393 * Display n more lines, backward.
394 * Start just before the line currently displayed at the top of the screen.
397 backward(n, force, only_last)
405 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
410 back(n, pos, force, only_last);
414 * Get the backwards scroll limit.
415 * Must call this function instead of just using the value of
416 * back_scroll, because the default case depends on sc_height and
417 * top_scroll, as well as back_scroll.
424 if (back_scroll >= 0)
425 return (back_scroll);
427 return (sc_height - 2);
428 return (10000); /* infinity */