3 * Copyright (C) 1984-2015 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;
24 public int same_pos_bell = 1;
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 int size_linebuf;
40 extern int hilite_search;
41 extern int status_col;
44 extern char *tagoption;
48 * Sound the bell to indicate user is trying to move past end of file.
53 if (quiet == NOT_QUIET)
60 * Check to see if the end of file is currently displayed.
70 if (ch_length() == NULL_POSITION)
72 * If the file length is not known,
73 * we can't possibly be displaying EOF.
78 * If the bottom line is empty, we are at EOF.
79 * If the bottom line ends at the file length,
80 * we must be just at EOF.
82 pos = position(BOTTOM_PLUS_ONE);
83 return (pos == NULL_POSITION || pos == ch_length());
87 * Check to see if the entire file is currently displayed.
90 entire_file_displayed(void)
94 /* Make sure last line of file is displayed. */
98 /* Make sure first line of file is displayed. */
100 return (pos == NULL_POSITION || pos == 0);
104 * If the screen is "squished", repaint it.
105 * "Squished" means the first displayed line is not at the top
106 * of the screen; this can happen when we display a short file
107 * for the first time.
119 * Display n lines, scrolling forward,
120 * starting at position pos in the input file.
121 * "force" means display the n lines even if we hit end of file.
122 * "only_last" means display only the last screenful if n > screen size.
123 * "nblank" is the number of blank lines to draw before the first
124 * real line. If nblank > 0, the pos must be NULL_POSITION.
125 * The first real line after the blanks will start at ch_zero().
128 forw(int n, POSITION pos, int force, int only_last, int nblank)
132 static int first_time = 1;
137 * do_repaint tells us not to display anything till the end,
138 * then just repaint the entire screen.
139 * We repaint if we are supposed to display only the last
140 * screenful and the request is for more than a screenful.
141 * Also if the request exceeds the forward scroll limit
142 * (but not if the request is for exactly a screenful, since
143 * repainting itself involves scrolling forward a screenful).
145 do_repaint = (only_last && n > sc_height-1) ||
146 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
149 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
150 prep_hilite(pos, pos + 4*size_linebuf, ignore_eoi ? 1 : -1);
151 pos = next_unfiltered(pos);
157 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
160 * Start a new screen.
161 * {{ This is not really desirable if we happen
162 * to hit eof in the middle of this screen,
163 * but we don't yet know if that will happen. }}
168 if (less_is_more == 0) {
174 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
177 * This is not contiguous with what is
178 * currently displayed. Clear the screen image
179 * (position table) and start a new screen.
188 } else if (!first_time)
190 putstr("...skipping...\n");
198 * Read the next line of input.
203 * Still drawing blanks; don't get a line
205 * If this is the last blank line, get ready to
206 * read a line starting at ch_zero() next time.
213 * Get the next line from the file.
215 pos = forw_line(pos);
217 pos = next_unfiltered(pos);
219 if (pos == NULL_POSITION)
222 * End of file: stop here unless the top line
223 * is still empty, or "force" is true.
224 * Even if force is true, stop when the last
225 * line in the file reaches the top of screen.
227 if (!force && position(TOP) != NULL_POSITION)
229 if (!empty_lines(0, 0) &&
230 !empty_lines(1, 1) &&
231 empty_lines(2, sc_height-1))
236 * Add the position of the next line to the position table.
237 * Display the current line on the screen.
244 * If this is the first screen displayed and
245 * we hit an early EOF (i.e. before the requested
246 * number of lines), we "squish" the display down
247 * at the bottom of the screen.
248 * But don't do this if a + option or a -t option
249 * was given. These options can cause us to
250 * start the display after the beginning of the file,
251 * and it is not appropriate to squish in that case.
253 if ((first_time || less_is_more) &&
254 pos == NULL_POSITION && !top_scroll &&
266 * Can't call clear_eol here. The cursor might be at end of line
267 * on an ignaw terminal, so clear_eol would clear the last char
268 * of the current line instead of all of the next line.
269 * If we really need to do this on clear_bg terminals, we need
270 * to find a better way.
273 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
276 * Writing the last character on the last line
277 * of the display may have scrolled the screen.
278 * If we were in standout mode, clear_bg terminals
279 * will fill the new line with the standout color.
280 * Now we're in normal mode again, so clear the line.
288 if (nlines == 0 && same_pos_bell)
293 (void) currline(BOTTOM);
297 * Display n lines, scrolling backward.
300 back(int n, POSITION pos, int force, int only_last)
306 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
308 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
309 prep_hilite((pos < 3*size_linebuf) ? 0 : pos - 3*size_linebuf, pos, -1);
315 * Get the previous line of input.
318 pos = prev_unfiltered(pos);
321 pos = back_line(pos);
322 if (pos == NULL_POSITION)
325 * Beginning of file: stop here unless "force" is true.
331 * Add the position of the previous line to the position table.
332 * Display the line on the screen.
344 if (nlines == 0 && same_pos_bell)
350 (void) currline(BOTTOM);
354 * Display n more lines, forward.
355 * Start just after the line currently displayed at the bottom of the screen.
358 forward(int n, int force, int only_last)
362 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
365 * If the -e flag is set and we're trying to go
366 * forward from end-of-file, go on to the next file.
373 pos = position(BOTTOM_PLUS_ONE);
374 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
379 * ignore_eoi is to support A_F_FOREVER.
380 * Back up until there is a line at the bottom
389 back(1, position(TOP), 1, 0);
390 pos = position(BOTTOM_PLUS_ONE);
391 } while (pos == NULL_POSITION);
399 forw(n, pos, force, only_last, 0);
403 * Display n more lines, backward.
404 * Start just before the line currently displayed at the top of the screen.
407 backward(int n, int force, int only_last)
412 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
417 back(n, pos, force, only_last);
421 * Get the backwards scroll limit.
422 * Must call this function instead of just using the value of
423 * back_scroll, because the default case depends on sc_height and
424 * top_scroll, as well as back_scroll.
427 get_back_scroll(void)
431 if (back_scroll >= 0)
432 return (back_scroll);
434 return (sc_height - 2);
435 return (10000); /* infinity */