periodic(8): Sync with FreeBSD current
[dragonfly.git] / contrib / less / forwback.c
blob721a0286a7b85860699f0dd1f002fcd3cd8af541
1 /*
2 * Copyright (C) 1984-2015 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.
8 */
12 * Primitives for displaying the file on the screen,
13 * scrolling either forward or backward.
16 #include "less.h"
17 #include "position.h"
19 public int screen_trashed;
20 public int squished;
21 public int no_back_scroll = 0;
22 public int forw_prompt;
23 public int same_pos_bell = 1;
25 extern int sigs;
26 extern int top_scroll;
27 extern int quiet;
28 extern int sc_width, sc_height;
29 extern int plusoption;
30 extern int forw_scroll;
31 extern int back_scroll;
32 extern int ignore_eoi;
33 extern int clear_bg;
34 extern int final_attr;
35 extern int oldbot;
36 #if HILITE_SEARCH
37 extern int size_linebuf;
38 extern int hilite_search;
39 extern int status_col;
40 #endif
41 #if TAGS
42 extern char *tagoption;
43 #endif
46 * Sound the bell to indicate user is trying to move past end of file.
48 static void
49 eof_bell()
51 if (quiet == NOT_QUIET)
52 bell();
53 else
54 vbell();
58 * Check to see if the end of file is currently displayed.
60 public int
61 eof_displayed()
63 POSITION pos;
65 if (ignore_eoi)
66 return (0);
68 if (ch_length() == NULL_POSITION)
70 * If the file length is not known,
71 * we can't possibly be displaying EOF.
73 return (0);
76 * If the bottom line is empty, we are at EOF.
77 * If the bottom line ends at the file length,
78 * we must be just at EOF.
80 pos = position(BOTTOM_PLUS_ONE);
81 return (pos == NULL_POSITION || pos == ch_length());
85 * Check to see if the entire file is currently displayed.
87 public int
88 entire_file_displayed()
90 POSITION pos;
92 /* Make sure last line of file is displayed. */
93 if (!eof_displayed())
94 return (0);
96 /* Make sure first line of file is displayed. */
97 pos = position(0);
98 return (pos == NULL_POSITION || pos == 0);
102 * If the screen is "squished", repaint it.
103 * "Squished" means the first displayed line is not at the top
104 * of the screen; this can happen when we display a short file
105 * for the first time.
107 public void
108 squish_check()
110 if (!squished)
111 return;
112 squished = 0;
113 repaint();
117 * Display n lines, scrolling forward,
118 * starting at position pos in the input file.
119 * "force" means display the n lines even if we hit end of file.
120 * "only_last" means display only the last screenful if n > screen size.
121 * "nblank" is the number of blank lines to draw before the first
122 * real line. If nblank > 0, the pos must be NULL_POSITION.
123 * The first real line after the blanks will start at ch_zero().
125 public void
126 forw(n, pos, force, only_last, nblank)
127 register int n;
128 POSITION pos;
129 int force;
130 int only_last;
131 int nblank;
133 int nlines = 0;
134 int do_repaint;
135 static int first_time = 1;
137 squish_check();
140 * do_repaint tells us not to display anything till the end,
141 * then just repaint the entire screen.
142 * We repaint if we are supposed to display only the last
143 * screenful and the request is for more than a screenful.
144 * Also if the request exceeds the forward scroll limit
145 * (but not if the request is for exactly a screenful, since
146 * repainting itself involves scrolling forward a screenful).
148 do_repaint = (only_last && n > sc_height-1) ||
149 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
151 #if HILITE_SEARCH
152 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
153 prep_hilite(pos, pos + 4*size_linebuf, ignore_eoi ? 1 : -1);
154 pos = next_unfiltered(pos);
156 #endif
158 if (!do_repaint)
160 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
163 * Start a new screen.
164 * {{ This is not really desirable if we happen
165 * to hit eof in the middle of this screen,
166 * but we don't yet know if that will happen. }}
168 pos_clear();
169 add_forw_pos(pos);
170 force = 1;
171 clear();
172 home();
175 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
178 * This is not contiguous with what is
179 * currently displayed. Clear the screen image
180 * (position table) and start a new screen.
182 pos_clear();
183 add_forw_pos(pos);
184 force = 1;
185 if (top_scroll)
187 clear();
188 home();
189 } else if (!first_time)
191 putstr("...skipping...\n");
196 while (--n >= 0)
199 * Read the next line of input.
201 if (nblank > 0)
204 * Still drawing blanks; don't get a line
205 * from the file yet.
206 * If this is the last blank line, get ready to
207 * read a line starting at ch_zero() next time.
209 if (--nblank == 0)
210 pos = ch_zero();
211 } else
214 * Get the next line from the file.
216 pos = forw_line(pos);
217 #if HILITE_SEARCH
218 pos = next_unfiltered(pos);
219 #endif
220 if (pos == NULL_POSITION)
223 * End of file: stop here unless the top line
224 * is still empty, or "force" is true.
225 * Even if force is true, stop when the last
226 * line in the file reaches the top of screen.
228 if (!force && position(TOP) != NULL_POSITION)
229 break;
230 if (!empty_lines(0, 0) &&
231 !empty_lines(1, 1) &&
232 empty_lines(2, sc_height-1))
233 break;
237 * Add the position of the next line to the position table.
238 * Display the current line on the screen.
240 add_forw_pos(pos);
241 nlines++;
242 if (do_repaint)
243 continue;
245 * If this is the first screen displayed and
246 * we hit an early EOF (i.e. before the requested
247 * number of lines), we "squish" the display down
248 * at the bottom of the screen.
249 * But don't do this if a + option or a -t option
250 * was given. These options can cause us to
251 * start the display after the beginning of the file,
252 * and it is not appropriate to squish in that case.
254 if (first_time && pos == NULL_POSITION && !top_scroll &&
255 #if TAGS
256 tagoption == NULL &&
257 #endif
258 !plusoption)
260 squished = 1;
261 continue;
263 put_line();
264 #if 0
265 /* {{
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.
271 * }}
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.
282 clear_eol();
284 #endif
285 forw_prompt = 1;
288 if (nlines == 0 && same_pos_bell)
289 eof_bell();
290 else if (do_repaint)
291 repaint();
292 first_time = 0;
293 (void) currline(BOTTOM);
297 * Display n lines, scrolling backward.
299 public void
300 back(n, pos, force, only_last)
301 register int n;
302 POSITION pos;
303 int force;
304 int only_last;
306 int nlines = 0;
307 int do_repaint;
309 squish_check();
310 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
311 #if HILITE_SEARCH
312 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
313 prep_hilite((pos < 3*size_linebuf) ? 0 : pos - 3*size_linebuf, pos, -1);
315 #endif
316 while (--n >= 0)
319 * Get the previous line of input.
321 #if HILITE_SEARCH
322 pos = prev_unfiltered(pos);
323 #endif
325 pos = back_line(pos);
326 if (pos == NULL_POSITION)
329 * Beginning of file: stop here unless "force" is true.
331 if (!force)
332 break;
335 * Add the position of the previous line to the position table.
336 * Display the line on the screen.
338 add_back_pos(pos);
339 nlines++;
340 if (!do_repaint)
342 home();
343 add_line();
344 put_line();
348 if (nlines == 0 && same_pos_bell)
349 eof_bell();
350 else if (do_repaint)
351 repaint();
352 else if (!oldbot)
353 lower_left();
354 (void) currline(BOTTOM);
358 * Display n more lines, forward.
359 * Start just after the line currently displayed at the bottom of the screen.
361 public void
362 forward(n, force, only_last)
363 int n;
364 int force;
365 int only_last;
367 POSITION pos;
369 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
372 * If the -e flag is set and we're trying to go
373 * forward from end-of-file, go on to the next file.
375 if (edit_next(1))
376 quit(QUIT_OK);
377 return;
380 pos = position(BOTTOM_PLUS_ONE);
381 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
383 if (ignore_eoi)
386 * ignore_eoi is to support A_F_FOREVER.
387 * Back up until there is a line at the bottom
388 * of the screen.
390 if (empty_screen())
391 pos = ch_zero();
392 else
396 back(1, position(TOP), 1, 0);
397 pos = position(BOTTOM_PLUS_ONE);
398 } while (pos == NULL_POSITION);
400 } else
402 eof_bell();
403 return;
406 forw(n, pos, force, only_last, 0);
410 * Display n more lines, backward.
411 * Start just before the line currently displayed at the top of the screen.
413 public void
414 backward(n, force, only_last)
415 int n;
416 int force;
417 int only_last;
419 POSITION pos;
421 pos = position(TOP);
422 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
424 eof_bell();
425 return;
427 back(n, pos, force, only_last);
431 * Get the backwards scroll limit.
432 * Must call this function instead of just using the value of
433 * back_scroll, because the default case depends on sc_height and
434 * top_scroll, as well as back_scroll.
436 public int
437 get_back_scroll()
439 if (no_back_scroll)
440 return (0);
441 if (back_scroll >= 0)
442 return (back_scroll);
443 if (top_scroll)
444 return (sc_height - 2);
445 return (10000); /* infinity */