kernel - Fix auto port assignment collision in network code
[dragonfly.git] / contrib / less / forwback.c
blob5c817e83f2e2ef9937fbd375523f23014ac4d41c
1 /*
2 * Copyright (C) 1984-2024 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 lbool squished;
20 public int no_back_scroll = 0;
21 public int forw_prompt;
22 public int first_time = 1;
23 public lbool no_eof_bell = FALSE;
25 extern int sigs;
26 extern int top_scroll;
27 extern int quiet;
28 extern int sc_width, sc_height;
29 extern int hshift;
30 extern int auto_wrap;
31 extern lbool plusoption;
32 extern int forw_scroll;
33 extern int back_scroll;
34 extern int ignore_eoi;
35 extern int header_lines;
36 extern int header_cols;
37 extern int full_screen;
38 extern POSITION header_start_pos;
39 #if HILITE_SEARCH
40 extern size_t size_linebuf;
41 extern int hilite_search;
42 extern int status_col;
43 #endif
44 #if TAGS
45 extern char *tagoption;
46 #endif
49 * Sound the bell to indicate user is trying to move past end of file.
51 public void eof_bell(void)
53 if (no_eof_bell)
54 return;
55 #if HAVE_TIME
56 static time_type last_eof_bell = 0;
57 time_type now = get_time();
58 if (now == last_eof_bell) /* max once per second */
59 return;
60 last_eof_bell = now;
61 #endif
62 if (quiet == NOT_QUIET)
63 bell();
64 else
65 vbell();
69 * Check to see if the end of file is currently displayed.
71 public lbool eof_displayed(void)
73 POSITION pos;
75 if (ignore_eoi)
76 return (FALSE);
78 if (ch_length() == NULL_POSITION)
80 * If the file length is not known,
81 * we can't possibly be displaying EOF.
83 return (FALSE);
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.
97 public lbool entire_file_displayed(void)
99 POSITION pos;
101 /* Make sure last line of file is displayed. */
102 if (!eof_displayed())
103 return (FALSE);
105 /* Make sure first line of file is displayed. */
106 pos = position(0);
107 return (pos == NULL_POSITION || pos == 0);
111 * If the screen is "squished", repaint it.
112 * "Squished" means the first displayed line is not at the top
113 * of the screen; this can happen when we display a short file
114 * for the first time.
116 public void squish_check(void)
118 if (!squished)
119 return;
120 squished = FALSE;
121 repaint();
125 * Read the first pfx columns of the next line.
126 * If skipeol==0 stop there, otherwise read and discard chars to end of line.
128 static POSITION forw_line_pfx(POSITION pos, int pfx, int skipeol)
130 int save_sc_width = sc_width;
131 int save_auto_wrap = auto_wrap;
132 int save_hshift = hshift;
133 /* Set fake sc_width to force only pfx chars to be read. */
134 sc_width = pfx + line_pfx_width();
135 auto_wrap = 0;
136 hshift = 0;
137 pos = forw_line_seg(pos, skipeol, FALSE, FALSE);
138 sc_width = save_sc_width;
139 auto_wrap = save_auto_wrap;
140 hshift = save_hshift;
141 return pos;
145 * Set header text color.
146 * Underline last line of headers, but not at header_start_pos
147 * (where there is no gap between the last header line and the next line).
149 static void set_attr_header(int ln)
151 set_attr_line(AT_COLOR_HEADER);
152 if (ln+1 == header_lines && position(0) != header_start_pos)
153 set_attr_line(AT_UNDERLINE);
157 * Display file headers, overlaying text already drawn
158 * at top and left of screen.
160 public int overlay_header(void)
162 int ln;
163 lbool moved = FALSE;
165 if (header_lines > 0)
167 /* Draw header_lines lines from start of file at top of screen. */
168 POSITION pos = header_start_pos;
169 home();
170 for (ln = 0; ln < header_lines; ++ln)
172 pos = forw_line(pos);
173 set_attr_header(ln);
174 clear_eol();
175 put_line();
177 moved = TRUE;
179 if (header_cols > 0)
181 /* Draw header_cols columns at left of each line. */
182 POSITION pos = header_start_pos;
183 home();
184 for (ln = 0; ln < sc_height-1; ++ln)
186 if (ln >= header_lines) /* switch from header lines to normal lines */
187 pos = position(ln);
188 if (pos == NULL_POSITION)
189 putchr('\n');
190 else
192 /* Need skipeol for all header lines except the last one. */
193 pos = forw_line_pfx(pos, header_cols, ln+1 < header_lines);
194 set_attr_header(ln);
195 put_line();
198 moved = TRUE;
200 if (moved)
201 lower_left();
202 return moved;
206 * Display n lines, scrolling forward,
207 * starting at position pos in the input file.
208 * "force" means display the n lines even if we hit end of file.
209 * "only_last" means display only the last screenful if n > screen size.
210 * "nblank" is the number of blank lines to draw before the first
211 * real line. If nblank > 0, the pos must be NULL_POSITION.
212 * The first real line after the blanks will start at ch_zero().
214 public void forw(int n, POSITION pos, lbool force, lbool only_last, int nblank)
216 int nlines = 0;
217 lbool do_repaint;
219 pos = after_header_pos(pos);
220 squish_check();
223 * do_repaint tells us not to display anything till the end,
224 * then just repaint the entire screen.
225 * We repaint if we are supposed to display only the last
226 * screenful and the request is for more than a screenful.
227 * Also if the request exceeds the forward scroll limit
228 * (but not if the request is for exactly a screenful, since
229 * repainting itself involves scrolling forward a screenful).
231 do_repaint = (only_last && n > sc_height-1) ||
232 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
234 #if HILITE_SEARCH
235 if (pos != NULL_POSITION && (hilite_search == OPT_ONPLUS || is_filtering() || status_col)) {
236 prep_hilite(pos, pos + (POSITION) (4*size_linebuf), ignore_eoi ? 1 : -1);
237 pos = next_unfiltered(pos);
239 #endif
241 if (!do_repaint)
243 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
246 * Start a new screen.
247 * {{ This is not really desirable if we happen
248 * to hit eof in the middle of this screen,
249 * but we don't yet know if that will happen. }}
251 pos_clear();
252 add_forw_pos(pos);
253 force = TRUE;
254 clear();
255 home();
258 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
261 * This is not contiguous with what is
262 * currently displayed. Clear the screen image
263 * (position table) and start a new screen.
265 pos_clear();
266 add_forw_pos(pos);
267 force = TRUE;
268 if (top_scroll)
270 clear();
271 home();
272 } else if (!first_time && !is_filtering() && full_screen)
274 putstr("...skipping...\n");
279 while (--n >= 0)
282 * Read the next line of input.
284 if (nblank > 0)
287 * Still drawing blanks; don't get a line
288 * from the file yet.
289 * If this is the last blank line, get ready to
290 * read a line starting at ch_zero() next time.
292 if (--nblank == 0)
293 pos = ch_zero();
294 } else
297 * Get the next line from the file.
299 pos = forw_line(pos);
300 #if HILITE_SEARCH
301 pos = next_unfiltered(pos);
302 #endif
303 if (pos == NULL_POSITION)
306 * End of file: stop here unless the top line
307 * is still empty, or "force" is true.
308 * Even if force is true, stop when the last
309 * line in the file reaches the top of screen.
311 if (!force && position(TOP) != NULL_POSITION)
312 break;
313 if (!empty_lines(0, 0) &&
314 !empty_lines(1, 1) &&
315 empty_lines(2, sc_height-1))
316 break;
320 * Add the position of the next line to the position table.
321 * Display the current line on the screen.
323 add_forw_pos(pos);
324 nlines++;
325 if (do_repaint)
326 continue;
328 * If this is the first screen displayed and
329 * we hit an early EOF (i.e. before the requested
330 * number of lines), we "squish" the display down
331 * at the bottom of the screen.
332 * But don't do this if a + option or a -t option
333 * was given. These options can cause us to
334 * start the display after the beginning of the file,
335 * and it is not appropriate to squish in that case.
337 if (first_time && pos == NULL_POSITION && !top_scroll &&
338 header_lines == 0 && header_cols == 0 &&
339 #if TAGS
340 tagoption == NULL &&
341 #endif
342 !plusoption)
344 squished = TRUE;
345 continue;
347 put_line();
348 #if 0
349 /* {{
350 * Can't call clear_eol here. The cursor might be at end of line
351 * on an ignaw terminal, so clear_eol would clear the last char
352 * of the current line instead of all of the next line.
353 * If we really need to do this on clear_bg terminals, we need
354 * to find a better way.
355 * }}
357 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
360 * Writing the last character on the last line
361 * of the display may have scrolled the screen.
362 * If we were in standout mode, clear_bg terminals
363 * will fill the new line with the standout color.
364 * Now we're in normal mode again, so clear the line.
366 clear_eol();
368 #endif
369 forw_prompt = 1;
371 if (nlines == 0 && !ignore_eoi)
372 eof_bell();
373 else if (do_repaint)
374 repaint();
375 else
377 overlay_header();
378 /* lower_left(); {{ considered harmful? }} */
380 first_time = 0;
381 (void) currline(BOTTOM);
385 * Display n lines, scrolling backward.
387 public void back(int n, POSITION pos, lbool force, lbool only_last)
389 int nlines = 0;
390 lbool do_repaint;
392 squish_check();
393 do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1) || header_lines > 0);
394 #if HILITE_SEARCH
395 if (pos != NULL_POSITION && (hilite_search == OPT_ONPLUS || is_filtering() || status_col)) {
396 prep_hilite((pos < (POSITION) (3*size_linebuf)) ? 0 : pos - (POSITION) (3*size_linebuf), pos, -1);
398 #endif
399 while (--n >= 0)
402 * Get the previous line of input.
404 #if HILITE_SEARCH
405 pos = prev_unfiltered(pos);
406 #endif
407 pos = back_line(pos);
408 if (pos == NULL_POSITION)
411 * Beginning of file: stop here unless "force" is true.
413 if (!force)
414 break;
416 if (pos != after_header_pos(pos))
419 * Don't allow scrolling back to before the current header line.
421 break;
424 * Add the position of the previous line to the position table.
425 * Display the line on the screen.
427 add_back_pos(pos);
428 nlines++;
429 if (!do_repaint)
431 home();
432 add_line();
433 put_line();
436 if (nlines == 0)
437 eof_bell();
438 else if (do_repaint)
439 repaint();
440 else
442 overlay_header();
443 lower_left();
445 (void) currline(BOTTOM);
449 * Display n more lines, forward.
450 * Start just after the line currently displayed at the bottom of the screen.
452 public void forward(int n, lbool force, lbool only_last)
454 POSITION pos;
456 if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
459 * If the -e flag is set and we're trying to go
460 * forward from end-of-file, go on to the next file.
462 if (edit_next(1))
463 quit(QUIT_OK);
464 return;
467 pos = position(BOTTOM_PLUS_ONE);
468 if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
470 if (ignore_eoi)
473 * ignore_eoi is to support A_F_FOREVER.
474 * Back up until there is a line at the bottom
475 * of the screen.
477 if (empty_screen())
478 pos = ch_zero();
479 else
483 back(1, position(TOP), 1, 0);
484 pos = position(BOTTOM_PLUS_ONE);
485 } while (pos == NULL_POSITION && !ABORT_SIGS());
487 } else
489 eof_bell();
490 return;
493 forw(n, pos, force, only_last, 0);
497 * Display n more lines, backward.
498 * Start just before the line currently displayed at the top of the screen.
500 public void backward(int n, lbool force, lbool only_last)
502 POSITION pos;
504 pos = position(TOP);
505 if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
507 eof_bell();
508 return;
510 back(n, pos, force, only_last);
514 * Get the backwards scroll limit.
515 * Must call this function instead of just using the value of
516 * back_scroll, because the default case depends on sc_height and
517 * top_scroll, as well as back_scroll.
519 public int get_back_scroll(void)
521 if (no_back_scroll)
522 return (0);
523 if (back_scroll >= 0)
524 return (back_scroll);
525 if (top_scroll)
526 return (sc_height - 2);
527 return (10000); /* infinity */
531 * Will the entire file fit on one screen?
533 public int get_one_screen(void)
535 int nlines;
536 POSITION pos = ch_zero();
538 for (nlines = 0; nlines < sc_height; nlines++)
540 pos = forw_line(pos);
541 if (pos == NULL_POSITION) break;
543 return (nlines < sc_height);