2 * Copyright (C) 1984-2002 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * Primitives for displaying the file on the screen,
14 * scrolling either forward or backward.
20 public int hit_eof
; /* Keeps track of how many times we hit end of file */
21 public int screen_trashed
;
23 public int no_back_scroll
= 0;
26 extern int top_scroll
;
28 extern int sc_width
, sc_height
;
29 extern int quit_at_eof
;
30 extern int plusoption
;
31 extern int forw_scroll
;
32 extern int back_scroll
;
33 extern int ignore_eoi
;
35 extern int final_attr
;
37 extern char *tagoption
;
41 * Sound the bell to indicate user is trying to move past end of file.
46 if (quiet
== NOT_QUIET
)
53 * Check to see if the end of file is currently "displayed".
65 * If the bottom line is empty, we are at EOF.
66 * If the bottom line ends at the file length,
67 * we must be just at EOF.
69 pos
= position(BOTTOM_PLUS_ONE
);
70 if (pos
== NULL_POSITION
|| pos
== ch_length())
75 * If the screen is "squished", repaint it.
76 * "Squished" means the first displayed line is not at the top
77 * of the screen; this can happen when we display a short file
90 * Display n lines, scrolling forward,
91 * starting at position pos in the input file.
92 * "force" means display the n lines even if we hit end of file.
93 * "only_last" means display only the last screenful if n > screen size.
94 * "nblank" is the number of blank lines to draw before the first
95 * real line. If nblank > 0, the pos must be NULL_POSITION.
96 * The first real line after the blanks will start at ch_zero().
99 forw(n
, pos
, force
, only_last
, nblank
)
109 static int first_time
= 1;
114 * do_repaint tells us not to display anything till the end,
115 * then just repaint the entire screen.
116 * We repaint if we are supposed to display only the last
117 * screenful and the request is for more than a screenful.
118 * Also if the request exceeds the forward scroll limit
119 * (but not if the request is for exactly a screenful, since
120 * repainting itself involves scrolling forward a screenful).
122 do_repaint
= (only_last
&& n
> sc_height
-1) ||
123 (forw_scroll
>= 0 && n
> forw_scroll
&& n
!= sc_height
-1);
128 * Forget any current line shift we might have
129 * (from the last line of the previous screenful).
134 if (top_scroll
&& n
>= sc_height
- 1 && pos
!= ch_length())
137 * Start a new screen.
138 * {{ This is not really desirable if we happen
139 * to hit eof in the middle of this screen,
140 * but we don't yet know if that will happen. }}
145 if (top_scroll
== OPT_ONPLUS
|| first_time
)
153 if (pos
!= position(BOTTOM_PLUS_ONE
) || empty_screen())
156 * This is not contiguous with what is
157 * currently displayed. Clear the screen image
158 * (position table) and start a new screen.
165 if (top_scroll
== OPT_ONPLUS
)
168 } else if (!first_time
)
170 putstr("...skipping...\n");
178 * Read the next line of input.
183 * Still drawing blanks; don't get a line
185 * If this is the last blank line, get ready to
186 * read a line starting at ch_zero() next time.
193 * Get the next line from the file.
195 pos
= forw_line(pos
);
196 if (pos
== NULL_POSITION
)
199 * End of file: stop here unless the top line
200 * is still empty, or "force" is true.
201 * Even if force is true, stop when the last
202 * line in the file reaches the top of screen.
205 if (!force
&& position(TOP
) != NULL_POSITION
)
207 if (!empty_lines(0, 0) &&
208 !empty_lines(1, 1) &&
209 empty_lines(2, sc_height
-1))
214 * Add the position of the next line to the position table.
215 * Display the current line on the screen.
222 * If this is the first screen displayed and
223 * we hit an early EOF (i.e. before the requested
224 * number of lines), we "squish" the display down
225 * at the bottom of the screen.
226 * But don't do this if a + option or a -t option
227 * was given. These options can cause us to
228 * start the display after the beginning of the file,
229 * and it is not appropriate to squish in that case.
231 if (first_time
&& pos
== NULL_POSITION
&& !top_scroll
&&
240 if (top_scroll
== OPT_ON
)
243 if (clear_bg
&& final_attr
!= AT_NORMAL
)
246 * Writing the last character on the last line
247 * of the display may have scrolled the screen.
248 * If we were in standout mode, clear_bg terminals
249 * will fill the new line with the standout color.
250 * Now we're in normal mode again, so clear the line.
258 else if (eof
&& !ABORT_SIGS())
267 (void) currline(BOTTOM
);
271 * Display n lines, scrolling backward.
274 back(n
, pos
, force
, only_last
)
284 do_repaint
= (n
> get_back_scroll() || (only_last
&& n
> sc_height
-1));
289 * Get the previous line of input.
291 pos
= back_line(pos
);
292 if (pos
== NULL_POSITION
)
295 * Beginning of file: stop here unless "force" is true.
301 * Add the position of the previous line to the position table.
302 * Display the line on the screen.
319 (void) currline(BOTTOM
);
323 * Display n more lines, forward.
324 * Start just after the line currently displayed at the bottom of the screen.
327 forward(n
, force
, only_last
)
334 if (quit_at_eof
&& hit_eof
&& !(ch_getflags() & CH_HELPFILE
))
337 * If the -e flag is set and we're trying to go
338 * forward from end-of-file, go on to the next file.
345 pos
= position(BOTTOM_PLUS_ONE
);
346 if (pos
== NULL_POSITION
&& (!force
|| empty_lines(2, sc_height
-1)))
351 * ignore_eoi is to support A_F_FOREVER.
352 * Back up until there is a line at the bottom
361 back(1, position(TOP
), 1, 0);
362 pos
= position(BOTTOM_PLUS_ONE
);
363 } while (pos
== NULL_POSITION
);
372 forw(n
, pos
, force
, only_last
, 0);
376 * Display n more lines, backward.
377 * Start just before the line currently displayed at the top of the screen.
380 backward(n
, force
, only_last
)
388 if (pos
== NULL_POSITION
&& (!force
|| position(BOTTOM
) == 0))
393 back(n
, pos
, force
, only_last
);
397 * Get the backwards scroll limit.
398 * Must call this function instead of just using the value of
399 * back_scroll, because the default case depends on sc_height and
400 * top_scroll, as well as back_scroll.
407 if (back_scroll
>= 0)
408 return (back_scroll
);
410 return (sc_height
- 2);
411 return (10000); /* infinity */