2 * Copyright (C) 1984-2022 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * Primitives for displaying the file on the screen,
13 * scrolling either forward or backward.
19 public int screen_trashed;
21 public int no_back_scroll = 0;
22 public int forw_prompt;
23 public int first_time = 1;
26 extern int top_scroll;
28 extern int sc_width, sc_height;
31 extern int plusoption;
32 extern int forw_scroll;
33 extern int back_scroll;
34 extern int ignore_eoi;
36 extern int final_attr;
37 extern int header_lines;
38 extern int header_cols;
40 extern int size_linebuf;
41 extern int hilite_search;
42 extern int status_col;
45 extern char *tagoption;
49 * Sound the bell to indicate user is trying to move past end of file.
55 static time_type last_eof_bell = 0;
56 time_type now = get_time();
57 if (now == last_eof_bell) /* max once per second */
61 if (quiet == NOT_QUIET)
68 * Check to see if the end of file is currently displayed.
71 eof_displayed(VOID_PARAM)
78 if (ch_length() == NULL_POSITION)
80 * If the file length is not known,
81 * we can't possibly be displaying EOF.
86 * If the bottom line is empty, we are at EOF.
87 * If the bottom line ends at the file length,
88 * we must be just at EOF.
90 pos = position(BOTTOM_PLUS_ONE);
91 return (pos == NULL_POSITION || pos == ch_length());
95 * Check to see if the entire file is currently displayed.
98 entire_file_displayed(VOID_PARAM)
102 /* Make sure last line of file is displayed. */
103 if (!eof_displayed())
106 /* Make sure first line of file is displayed. */
108 return (pos == NULL_POSITION || pos == 0);
112 * If the screen is "squished", repaint it.
113 * "Squished" means the first displayed line is not at the top
114 * of the screen; this can happen when we display a short file
115 * for the first time.
118 squish_check(VOID_PARAM)
127 * Read the first pfx columns of the next line.
128 * If skipeol==0 stop there, otherwise read and discard chars to end of line.
131 forw_line_pfx(pos, pfx, skipeol)
136 int save_sc_width = sc_width;
137 int save_auto_wrap = auto_wrap;
138 int save_hshift = hshift;
139 /* Set fake sc_width to force only pfx chars to be read. */
140 sc_width = pfx + line_pfx_width();
143 pos = forw_line_seg(pos, skipeol, FALSE, FALSE);
144 sc_width = save_sc_width;
145 auto_wrap = save_auto_wrap;
146 hshift = save_hshift;
151 * Set header text color.
152 * Underline last line of headers, but not at beginning of file
153 * (where there is no gap between the last header line and the next line).
159 set_attr_line(AT_COLOR_HEADER);
160 if (ln+1 == header_lines && position(0) != ch_zero())
161 set_attr_line(AT_UNDERLINE);
165 * Display file headers, overlaying text already drawn
166 * at top and left of screen.
169 overlay_header(VOID_PARAM)
171 POSITION pos = ch_zero(); /* header lines are at beginning of file */
175 if (header_lines > 0)
177 /* Draw header_lines lines from start of file at top of screen. */
179 for (ln = 0; ln < header_lines; ++ln)
181 pos = forw_line(pos);
190 /* Draw header_cols columns at left of each line. */
193 for (ln = 0; ln < sc_height-1; ++ln)
195 if (ln >= header_lines) /* switch from header lines to normal lines */
197 if (pos == NULL_POSITION)
201 /* Need skipeol for all header lines except the last one. */
202 pos = forw_line_pfx(pos, header_cols, ln+1 < header_lines);
215 * Display n lines, scrolling forward,
216 * starting at position pos in the input file.
217 * "force" means display the n lines even if we hit end of file.
218 * "only_last" means display only the last screenful if n > screen size.
219 * "nblank" is the number of blank lines to draw before the first
220 * real line. If nblank > 0, the pos must be NULL_POSITION.
221 * The first real line after the blanks will start at ch_zero().
224 forw(n, pos, force, only_last, nblank)
237 * do_repaint tells us not to display anything till the end,
238 * then just repaint the entire screen.
239 * We repaint if we are supposed to display only the last
240 * screenful and the request is for more than a screenful.
241 * Also if the request exceeds the forward scroll limit
242 * (but not if the request is for exactly a screenful, since
243 * repainting itself involves scrolling forward a screenful).
245 do_repaint = (only_last && n > sc_height-1) ||
246 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
249 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
250 prep_hilite(pos, pos + 4*size_linebuf, ignore_eoi ? 1 : -1);
251 pos = next_unfiltered(pos);
257 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
260 * Start a new screen.
261 * {{ This is not really desirable if we happen
262 * to hit eof in the middle of this screen,
263 * but we don't yet know if that will happen. }}
272 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
275 * This is not contiguous with what is
276 * currently displayed. Clear the screen image
277 * (position table) and start a new screen.
286 } else if (!first_time && !is_filtering())
288 putstr("...skipping...\n");
296 * Read the next line of input.
301 * Still drawing blanks; don't get a line
303 * If this is the last blank line, get ready to
304 * read a line starting at ch_zero() next time.
311 * Get the next line from the file.
313 pos = forw_line(pos);
315 pos = next_unfiltered(pos);
317 if (pos == NULL_POSITION)
320 * End of file: stop here unless the top line
321 * is still empty, or "force" is true.
322 * Even if force is true, stop when the last
323 * line in the file reaches the top of screen.
325 if (!force && position(TOP) != NULL_POSITION)
327 if (!empty_lines(0, 0) &&
328 !empty_lines(1, 1) &&
329 empty_lines(2, sc_height-1))
334 * Add the position of the next line to the position table.
335 * Display the current line on the screen.
342 * If this is the first screen displayed and
343 * we hit an early EOF (i.e. before the requested
344 * number of lines), we "squish" the display down
345 * at the bottom of the screen.
346 * But don't do this if a + option or a -t option
347 * was given. These options can cause us to
348 * start the display after the beginning of the file,
349 * and it is not appropriate to squish in that case.
351 if (first_time && pos == NULL_POSITION && !top_scroll &&
363 * Can't call clear_eol here. The cursor might be at end of line
364 * on an ignaw terminal, so clear_eol would clear the last char
365 * of the current line instead of all of the next line.
366 * If we really need to do this on clear_bg terminals, we need
367 * to find a better way.
370 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
373 * Writing the last character on the last line
374 * of the display may have scrolled the screen.
375 * If we were in standout mode, clear_bg terminals
376 * will fill the new line with the standout color.
377 * Now we're in normal mode again, so clear the line.
385 if (header_lines > 0)
388 * Don't allow ch_zero to appear on screen except at top of screen.
389 * Otherwise duplicate header lines may be displayed.
391 if (onscreen(ch_zero()) > 0)
393 jump_loc(ch_zero(), 0); /* {{ yuck }} */
397 if (nlines == 0 && !ignore_eoi)
404 /* lower_left(); {{ considered harmful? }} */
407 (void) currline(BOTTOM);
411 * Display n lines, scrolling backward.
414 back(n, pos, force, only_last)
424 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1) || header_lines > 0);
426 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
427 prep_hilite((pos < 3*size_linebuf) ? 0 : pos - 3*size_linebuf, pos, -1);
433 * Get the previous line of input.
436 pos = prev_unfiltered(pos);
439 pos = back_line(pos);
440 if (pos == NULL_POSITION)
443 * Beginning of file: stop here unless "force" is true.
449 * Add the position of the previous line to the position table.
450 * Display the line on the screen.
470 (void) currline(BOTTOM);
474 * Display n more lines, forward.
475 * Start just after the line currently displayed at the bottom of the screen.
478 forward(n, force, only_last)
485 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
488 * If the -e flag is set and we're trying to go
489 * forward from end-of-file, go on to the next file.
496 pos = position(BOTTOM_PLUS_ONE);
497 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
502 * ignore_eoi is to support A_F_FOREVER.
503 * Back up until there is a line at the bottom
512 back(1, position(TOP), 1, 0);
513 pos = position(BOTTOM_PLUS_ONE);
514 } while (pos == NULL_POSITION);
522 forw(n, pos, force, only_last, 0);
526 * Display n more lines, backward.
527 * Start just before the line currently displayed at the top of the screen.
530 backward(n, force, only_last)
538 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
543 back(n, pos, force, only_last);
547 * Get the backwards scroll limit.
548 * Must call this function instead of just using the value of
549 * back_scroll, because the default case depends on sc_height and
550 * top_scroll, as well as back_scroll.
553 get_back_scroll(VOID_PARAM)
557 if (back_scroll >= 0)
558 return (back_scroll);
560 return (sc_height - 2);
561 return (10000); /* infinity */
565 * Will the entire file fit on one screen?
568 get_one_screen(VOID_PARAM)
571 POSITION pos = ch_zero();
573 for (nlines = 0; nlines < sc_height; nlines++)
575 pos = forw_line(pos);
576 if (pos == NULL_POSITION) break;
578 return (nlines < sc_height);