2 * Copyright (C) 1984-2023 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;
39 extern int full_screen;
41 extern int size_linebuf;
42 extern int hilite_search;
43 extern int status_col;
46 extern char *tagoption;
50 * Sound the bell to indicate user is trying to move past end of file.
52 public void eof_bell(void)
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.
70 public int eof_displayed(void)
77 if (ch_length() == NULL_POSITION)
79 * If the file length is not known,
80 * we can't possibly be displaying EOF.
85 * If the bottom line is empty, we are at EOF.
86 * If the bottom line ends at the file length,
87 * we must be just at EOF.
89 pos = position(BOTTOM_PLUS_ONE);
90 return (pos == NULL_POSITION || pos == ch_length());
94 * Check to see if the entire file is currently displayed.
96 public int entire_file_displayed(void)
100 /* Make sure last line of file is displayed. */
101 if (!eof_displayed())
104 /* Make sure first line of file is displayed. */
106 return (pos == NULL_POSITION || pos == 0);
110 * If the screen is "squished", repaint it.
111 * "Squished" means the first displayed line is not at the top
112 * of the screen; this can happen when we display a short file
113 * for the first time.
115 public void squish_check(void)
124 * Read the first pfx columns of the next line.
125 * If skipeol==0 stop there, otherwise read and discard chars to end of line.
127 static POSITION forw_line_pfx(POSITION pos, int pfx, int skipeol)
129 int save_sc_width = sc_width;
130 int save_auto_wrap = auto_wrap;
131 int save_hshift = hshift;
132 /* Set fake sc_width to force only pfx chars to be read. */
133 sc_width = pfx + line_pfx_width();
136 pos = forw_line_seg(pos, skipeol, FALSE, FALSE);
137 sc_width = save_sc_width;
138 auto_wrap = save_auto_wrap;
139 hshift = save_hshift;
144 * Set header text color.
145 * Underline last line of headers, but not at beginning of file
146 * (where there is no gap between the last header line and the next line).
148 static void set_attr_header(int ln)
150 set_attr_line(AT_COLOR_HEADER);
151 if (ln+1 == header_lines && position(0) != ch_zero())
152 set_attr_line(AT_UNDERLINE);
156 * Display file headers, overlaying text already drawn
157 * at top and left of screen.
159 public int overlay_header(void)
161 POSITION pos = ch_zero(); /* header lines are at beginning of file */
165 if (header_lines > 0)
167 /* Draw header_lines lines from start of file at top of screen. */
169 for (ln = 0; ln < header_lines; ++ln)
171 pos = forw_line(pos);
180 /* Draw header_cols columns at left of each line. */
183 for (ln = 0; ln < sc_height-1; ++ln)
185 if (ln >= header_lines) /* switch from header lines to normal lines */
187 if (pos == NULL_POSITION)
191 /* Need skipeol for all header lines except the last one. */
192 pos = forw_line_pfx(pos, header_cols, ln+1 < header_lines);
205 * Display n lines, scrolling forward,
206 * starting at position pos in the input file.
207 * "force" means display the n lines even if we hit end of file.
208 * "only_last" means display only the last screenful if n > screen size.
209 * "nblank" is the number of blank lines to draw before the first
210 * real line. If nblank > 0, the pos must be NULL_POSITION.
211 * The first real line after the blanks will start at ch_zero().
213 public void forw(int n, POSITION pos, int force, int only_last, int nblank)
221 * do_repaint tells us not to display anything till the end,
222 * then just repaint the entire screen.
223 * We repaint if we are supposed to display only the last
224 * screenful and the request is for more than a screenful.
225 * Also if the request exceeds the forward scroll limit
226 * (but not if the request is for exactly a screenful, since
227 * repainting itself involves scrolling forward a screenful).
229 do_repaint = (only_last && n > sc_height-1) ||
230 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
233 if (pos != NULL_POSITION && (hilite_search == OPT_ONPLUS || is_filtering() || status_col)) {
234 prep_hilite(pos, pos + 4*size_linebuf, ignore_eoi ? 1 : -1);
235 pos = next_unfiltered(pos);
241 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
244 * Start a new screen.
245 * {{ This is not really desirable if we happen
246 * to hit eof in the middle of this screen,
247 * but we don't yet know if that will happen. }}
256 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
259 * This is not contiguous with what is
260 * currently displayed. Clear the screen image
261 * (position table) and start a new screen.
270 } else if (!first_time && !is_filtering() && full_screen)
272 putstr("...skipping...\n");
280 * Read the next line of input.
285 * Still drawing blanks; don't get a line
287 * If this is the last blank line, get ready to
288 * read a line starting at ch_zero() next time.
295 * Get the next line from the file.
297 pos = forw_line(pos);
299 pos = next_unfiltered(pos);
301 if (pos == NULL_POSITION)
304 * End of file: stop here unless the top line
305 * is still empty, or "force" is true.
306 * Even if force is true, stop when the last
307 * line in the file reaches the top of screen.
309 if (!force && position(TOP) != NULL_POSITION)
311 if (!empty_lines(0, 0) &&
312 !empty_lines(1, 1) &&
313 empty_lines(2, sc_height-1))
318 * Add the position of the next line to the position table.
319 * Display the current line on the screen.
326 * If this is the first screen displayed and
327 * we hit an early EOF (i.e. before the requested
328 * number of lines), we "squish" the display down
329 * at the bottom of the screen.
330 * But don't do this if a + option or a -t option
331 * was given. These options can cause us to
332 * start the display after the beginning of the file,
333 * and it is not appropriate to squish in that case.
335 if (first_time && pos == NULL_POSITION && !top_scroll &&
336 header_lines == 0 && header_cols == 0 &&
348 * Can't call clear_eol here. The cursor might be at end of line
349 * on an ignaw terminal, so clear_eol would clear the last char
350 * of the current line instead of all of the next line.
351 * If we really need to do this on clear_bg terminals, we need
352 * to find a better way.
355 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
358 * Writing the last character on the last line
359 * of the display may have scrolled the screen.
360 * If we were in standout mode, clear_bg terminals
361 * will fill the new line with the standout color.
362 * Now we're in normal mode again, so clear the line.
370 if (header_lines > 0)
373 * Don't allow ch_zero to appear on screen except at top of screen.
374 * Otherwise duplicate header lines may be displayed.
376 if (onscreen(ch_zero()) > 0)
378 jump_loc(ch_zero(), 0); /* {{ yuck }} */
382 if (nlines == 0 && !ignore_eoi)
389 /* lower_left(); {{ considered harmful? }} */
392 (void) currline(BOTTOM);
396 * Display n lines, scrolling backward.
398 public void back(int n, POSITION pos, int force, int only_last)
404 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1) || header_lines > 0);
406 if (pos != NULL_POSITION && (hilite_search == OPT_ONPLUS || is_filtering() || status_col)) {
407 prep_hilite((pos < 3*size_linebuf) ? 0 : pos - 3*size_linebuf, pos, -1);
413 * Get the previous line of input.
416 pos = prev_unfiltered(pos);
419 pos = back_line(pos);
420 if (pos == NULL_POSITION)
423 * Beginning of file: stop here unless "force" is true.
429 * Add the position of the previous line to the position table.
430 * Display the line on the screen.
450 (void) currline(BOTTOM);
454 * Display n more lines, forward.
455 * Start just after the line currently displayed at the bottom of the screen.
457 public void forward(int n, int force, int only_last)
461 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
464 * If the -e flag is set and we're trying to go
465 * forward from end-of-file, go on to the next file.
472 pos = position(BOTTOM_PLUS_ONE);
473 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
478 * ignore_eoi is to support A_F_FOREVER.
479 * Back up until there is a line at the bottom
488 back(1, position(TOP), 1, 0);
489 pos = position(BOTTOM_PLUS_ONE);
490 } while (pos == NULL_POSITION && !ABORT_SIGS());
498 forw(n, pos, force, only_last, 0);
502 * Display n more lines, backward.
503 * Start just before the line currently displayed at the top of the screen.
505 public void backward(int n, int force, int only_last)
510 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
515 back(n, pos, force, only_last);
519 * Get the backwards scroll limit.
520 * Must call this function instead of just using the value of
521 * back_scroll, because the default case depends on sc_height and
522 * top_scroll, as well as back_scroll.
524 public int get_back_scroll(void)
528 if (back_scroll >= 0)
529 return (back_scroll);
531 return (sc_height - 2);
532 return (10000); /* infinity */
536 * Will the entire file fit on one screen?
538 public int get_one_screen(void)
541 POSITION pos = ch_zero();
543 for (nlines = 0; nlines < sc_height; nlines++)
545 pos = forw_line(pos);
546 if (pos == NULL_POSITION) break;
548 return (nlines < sc_height);