1 /*        $NetBSD: forwback.c,v 1.5 2023/10/06 05:49:49 simonb Exp $  */
2 
3 /*
4  * Copyright (C) 1984-2023  Mark Nudelman
5  *
6  * You may distribute under the terms of either the GNU General Public
7  * License or the Less License, as specified in the README file.
8  *
9  * For more information, see the README file.
10  */
11 
12 
13 /*
14  * Primitives for displaying the file on the screen,
15  * scrolling either forward or backward.
16  */
17 
18 #include "less.h"
19 #include "position.h"
20 
21 public int screen_trashed;
22 public int squished;
23 public int no_back_scroll = 0;
24 public int forw_prompt;
25 public int first_time = 1;
26 
27 extern int sigs;
28 extern int top_scroll;
29 extern int quiet;
30 extern int sc_width, sc_height;
31 extern int hshift;
32 extern int auto_wrap;
33 extern int more_mode;
34 extern int plusoption;
35 extern int forw_scroll;
36 extern int back_scroll;
37 extern int ignore_eoi;
38 extern int clear_bg;
39 extern int final_attr;
40 extern int header_lines;
41 extern int header_cols;
42 extern int full_screen;
43 #if HILITE_SEARCH
44 extern int size_linebuf;
45 extern int hilite_search;
46 extern int status_col;
47 #endif
48 #if TAGS
49 extern char *tagoption;
50 #endif
51 
52 /*
53  * Sound the bell to indicate user is trying to move past end of file.
54  */
eof_bell(void)55 public void eof_bell(void)
56 {
57 #if HAVE_TIME
58           static time_type last_eof_bell = 0;
59           time_type now = get_time();
60           if (now == last_eof_bell) /* max once per second */
61                     return;
62           last_eof_bell = now;
63 #endif
64           if (quiet == NOT_QUIET)
65                     bell();
66           else
67                     vbell();
68 }
69 
70 /*
71  * Check to see if the end of file is currently displayed.
72  */
eof_displayed(void)73 public int eof_displayed(void)
74 {
75           POSITION pos;
76 
77           if (ignore_eoi)
78                     return (0);
79 
80           if (ch_length() == NULL_POSITION)
81                     /*
82                      * If the file length is not known,
83                      * we can't possibly be displaying EOF.
84                      */
85                     return (0);
86 
87           /*
88            * If the bottom line is empty, we are at EOF.
89            * If the bottom line ends at the file length,
90            * we must be just at EOF.
91            */
92           pos = position(BOTTOM_PLUS_ONE);
93           return (pos == NULL_POSITION || pos == ch_length());
94 }
95 
96 /*
97  * Check to see if the entire file is currently displayed.
98  */
entire_file_displayed(void)99 public int entire_file_displayed(void)
100 {
101           POSITION pos;
102 
103           /* Make sure last line of file is displayed. */
104           if (!eof_displayed())
105                     return (0);
106 
107           /* Make sure first line of file is displayed. */
108           pos = position(0);
109           return (pos == NULL_POSITION || pos == 0);
110 }
111 
112 /*
113  * If the screen is "squished", repaint it.
114  * "Squished" means the first displayed line is not at the top
115  * of the screen; this can happen when we display a short file
116  * for the first time.
117  */
squish_check(void)118 public void squish_check(void)
119 {
120           if (!squished)
121                     return;
122           squished = 0;
123           repaint();
124 }
125 
126 /*
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.
129  */
forw_line_pfx(POSITION pos,int pfx,int skipeol)130 static POSITION forw_line_pfx(POSITION pos, int pfx, int skipeol)
131 {
132           int save_sc_width = sc_width;
133           int save_auto_wrap = auto_wrap;
134           int save_hshift = hshift;
135           /* Set fake sc_width to force only pfx chars to be read. */
136           sc_width = pfx + line_pfx_width();
137           auto_wrap = 0;
138           hshift = 0;
139           pos = forw_line_seg(pos, skipeol, FALSE, FALSE);
140           sc_width = save_sc_width;
141           auto_wrap = save_auto_wrap;
142           hshift = save_hshift;
143           return pos;
144 }
145 
146 /*
147  * Set header text color.
148  * Underline last line of headers, but not at beginning of file
149  * (where there is no gap between the last header line and the next line).
150  */
set_attr_header(int ln)151 static void set_attr_header(int ln)
152 {
153           set_attr_line(AT_COLOR_HEADER);
154           if (ln+1 == header_lines && position(0) != ch_zero())
155                     set_attr_line(AT_UNDERLINE);
156 }
157 
158 /*
159  * Display file headers, overlaying text already drawn
160  * at top and left of screen.
161  */
overlay_header(void)162 public int overlay_header(void)
163 {
164           POSITION pos = ch_zero(); /* header lines are at beginning of file */
165           int ln;
166           int moved = FALSE;
167 
168           if (header_lines > 0)
169           {
170                     /* Draw header_lines lines from start of file at top of screen. */
171                     home();
172                     for (ln = 0; ln < header_lines; ++ln)
173                     {
174                               pos = forw_line(pos);
175                               set_attr_header(ln);
176                               clear_eol();
177                               put_line();
178                     }
179                     moved = TRUE;
180           }
181           if (header_cols > 0)
182           {
183                     /* Draw header_cols columns at left of each line. */
184                     home();
185                     pos = ch_zero();
186                     for (ln = 0; ln < sc_height-1; ++ln)
187                     {
188                               if (ln >= header_lines) /* switch from header lines to normal lines */
189                                         pos = position(ln);
190                               if (pos == NULL_POSITION)
191                                         putchr('\n');
192                               else
193                               {
194                                         /* Need skipeol for all header lines except the last one. */
195                                         pos = forw_line_pfx(pos, header_cols, ln+1 < header_lines);
196                                         set_attr_header(ln);
197                                         put_line();
198                               }
199                     }
200                     moved = TRUE;
201           }
202           if (moved)
203                     lower_left();
204           return moved;
205 }
206 
207 /*
208  * Display n lines, scrolling forward,
209  * starting at position pos in the input file.
210  * "force" means display the n lines even if we hit end of file.
211  * "only_last" means display only the last screenful if n > screen size.
212  * "nblank" is the number of blank lines to draw before the first
213  *   real line.  If nblank > 0, the pos must be NULL_POSITION.
214  *   The first real line after the blanks will start at ch_zero().
215  */
forw(int n,POSITION pos,int force,int only_last,int nblank)216 public void forw(int n, POSITION pos, int force, int only_last, int nblank)
217 {
218           int nlines = 0;
219           int do_repaint;
220 
221           squish_check();
222 
223           /*
224            * do_repaint tells us not to display anything till the end,
225            * then just repaint the entire screen.
226            * We repaint if we are supposed to display only the last
227            * screenful and the request is for more than a screenful.
228            * Also if the request exceeds the forward scroll limit
229            * (but not if the request is for exactly a screenful, since
230            * repainting itself involves scrolling forward a screenful).
231            */
232           do_repaint = (only_last && n > sc_height-1) ||
233                     (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
234 
235 #if HILITE_SEARCH
236           if (pos != NULL_POSITION && (hilite_search == OPT_ONPLUS || is_filtering() || status_col)) {
237                     prep_hilite(pos, pos + 4*size_linebuf, ignore_eoi ? 1 : -1);
238                     pos = next_unfiltered(pos);
239           }
240 #endif
241 
242           if (!do_repaint)
243           {
244                     if (top_scroll && n >= sc_height - 1 && pos != ch_length())
245                     {
246                               /*
247                                * Start a new screen.
248                                * {{ This is not really desirable if we happen
249                                *    to hit eof in the middle of this screen,
250                                *    but we don't yet know if that will happen. }}
251                                */
252                               pos_clear();
253                               add_forw_pos(pos);
254                               force = 1;
255                               clear();
256                               home();
257                     }
258 
259                     if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
260                     {
261                               /*
262                                * This is not contiguous with what is
263                                * currently displayed.  Clear the screen image
264                                * (position table) and start a new screen.
265                                */
266                               pos_clear();
267                               add_forw_pos(pos);
268                               force = 1;
269                               if (top_scroll)
270                               {
271                                         clear();
272                                         home();
273                               } else if (!first_time && !is_filtering() && full_screen)
274                               {
275                                         putstr("...skipping...\n");
276                               }
277                     }
278           }
279 
280           while (--n >= 0)
281           {
282                     /*
283                      * Read the next line of input.
284                      */
285                     if (nblank > 0)
286                     {
287                               /*
288                                * Still drawing blanks; don't get a line
289                                * from the file yet.
290                                * If this is the last blank line, get ready to
291                                * read a line starting at ch_zero() next time.
292                                */
293                               if (--nblank == 0)
294                                         pos = ch_zero();
295                     } else
296                     {
297                               /*
298                                * Get the next line from the file.
299                                */
300                               pos = forw_line(pos);
301 #if HILITE_SEARCH
302                               pos = next_unfiltered(pos);
303 #endif
304                               if (pos == NULL_POSITION)
305                               {
306                                         /*
307                                          * End of file: stop here unless the top line
308                                          * is still empty, or "force" is true.
309                                          * Even if force is true, stop when the last
310                                          * line in the file reaches the top of screen.
311                                          */
312                                         if (!force && position(TOP) != NULL_POSITION)
313                                                   break;
314                                         if (!empty_lines(0, 0) &&
315                                             !empty_lines(1, 1) &&
316                                              empty_lines(2, sc_height-1))
317                                                   break;
318                               }
319                     }
320                     /*
321                      * Add the position of the next line to the position table.
322                      * Display the current line on the screen.
323                      */
324                     add_forw_pos(pos);
325                     nlines++;
326                     if (do_repaint)
327                               continue;
328                     /*
329                      * If this is the first screen displayed and
330                      * we hit an early EOF (i.e. before the requested
331                      * number of lines), we "squish" the display down
332                      * at the bottom of the screen.
333                      * But don't do this if a + option or a -t option
334                      * was given.  These options can cause us to
335                      * start the display after the beginning of the file,
336                      * and it is not appropriate to squish in that case.
337                      */
338                     if ((first_time || more_mode) &&
339                         pos == NULL_POSITION && !top_scroll &&
340 #if TAGS
341                         tagoption == NULL &&
342 #endif
343                         !plusoption)
344                     {
345                               squished = 1;
346                               continue;
347                     }
348                     put_line();
349 #if 0
350                     /* {{
351                      * Can't call clear_eol here.  The cursor might be at end of line
352                      * on an ignaw terminal, so clear_eol would clear the last char
353                      * of the current line instead of all of the next line.
354                      * If we really need to do this on clear_bg terminals, we need
355                      * to find a better way.
356                      * }}
357                      */
358                     if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
359                     {
360                               /*
361                                * Writing the last character on the last line
362                                * of the display may have scrolled the screen.
363                                * If we were in standout mode, clear_bg terminals
364                                * will fill the new line with the standout color.
365                                * Now we're in normal mode again, so clear the line.
366                                */
367                               clear_eol();
368                     }
369 #endif
370                     forw_prompt = 1;
371           }
372 
373           if (header_lines > 0)
374           {
375                     /*
376                      * Don't allow ch_zero to appear on screen except at top of screen.
377                      * Otherwise duplicate header lines may be displayed.
378                      */
379                     if (onscreen(ch_zero()) > 0)
380                     {
381                               jump_loc(ch_zero(), 0); /* {{ yuck }} */
382                               return;
383                     }
384           }
385           if (nlines == 0 && !ignore_eoi)
386                     eof_bell();
387           else if (do_repaint)
388                     repaint();
389           else
390           {
391                     overlay_header();
392                     /* lower_left(); {{ considered harmful? }} */
393           }
394           first_time = 0;
395           (void) currline(BOTTOM);
396 }
397 
398 /*
399  * Display n lines, scrolling backward.
400  */
back(int n,POSITION pos,int force,int only_last)401 public void back(int n, POSITION pos, int force, int only_last)
402 {
403           int nlines = 0;
404           int do_repaint;
405 
406           squish_check();
407           do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1) || header_lines > 0);
408 #if HILITE_SEARCH
409           if (pos != NULL_POSITION && (hilite_search == OPT_ONPLUS || is_filtering() || status_col)) {
410                     prep_hilite((pos < 3*size_linebuf) ? 0 : pos - 3*size_linebuf, pos, -1);
411           }
412 #endif
413           while (--n >= 0)
414           {
415                     /*
416                      * Get the previous line of input.
417                      */
418 #if HILITE_SEARCH
419                     pos = prev_unfiltered(pos);
420 #endif
421 
422                     pos = back_line(pos);
423                     if (pos == NULL_POSITION)
424                     {
425                               /*
426                                * Beginning of file: stop here unless "force" is true.
427                                */
428                               if (!force)
429                                         break;
430                     }
431                     /*
432                      * Add the position of the previous line to the position table.
433                      * Display the line on the screen.
434                      */
435                     add_back_pos(pos);
436                     nlines++;
437                     if (!do_repaint)
438                     {
439                               home();
440                               add_line();
441                               put_line();
442                     }
443           }
444           if (nlines == 0)
445                     eof_bell();
446           else if (do_repaint)
447                     repaint();
448           else
449           {
450                     overlay_header();
451                     lower_left();
452           }
453           (void) currline(BOTTOM);
454 }
455 
456 /*
457  * Display n more lines, forward.
458  * Start just after the line currently displayed at the bottom of the screen.
459  */
forward(int n,int force,int only_last)460 public void forward(int n, int force, int only_last)
461 {
462           POSITION pos;
463 
464           if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
465           {
466                     /*
467                      * If the -e flag is set and we're trying to go
468                      * forward from end-of-file, go on to the next file.
469                      */
470                     if (edit_next(1))
471                               quit(QUIT_OK);
472                     return;
473           }
474 
475           pos = position(BOTTOM_PLUS_ONE);
476           if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
477           {
478                     if (ignore_eoi)
479                     {
480                               /*
481                                * ignore_eoi is to support A_F_FOREVER.
482                                * Back up until there is a line at the bottom
483                                * of the screen.
484                                */
485                               if (empty_screen())
486                                         pos = ch_zero();
487                               else
488                               {
489                                         do
490                                         {
491                                                   back(1, position(TOP), 1, 0);
492                                                   pos = position(BOTTOM_PLUS_ONE);
493                                         } while (pos == NULL_POSITION && !ABORT_SIGS());
494                               }
495                     } else
496                     {
497                               eof_bell();
498                               return;
499                     }
500           }
501           forw(n, pos, force, only_last, 0);
502 }
503 
504 /*
505  * Display n more lines, backward.
506  * Start just before the line currently displayed at the top of the screen.
507  */
backward(int n,int force,int only_last)508 public void backward(int n, int force, int only_last)
509 {
510           POSITION pos;
511 
512           pos = position(TOP);
513           if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
514           {
515                     eof_bell();
516                     return;
517           }
518           back(n, pos, force, only_last);
519 }
520 
521 /*
522  * Get the backwards scroll limit.
523  * Must call this function instead of just using the value of
524  * back_scroll, because the default case depends on sc_height and
525  * top_scroll, as well as back_scroll.
526  */
get_back_scroll(void)527 public int get_back_scroll(void)
528 {
529           if (no_back_scroll)
530                     return (0);
531           if (back_scroll >= 0)
532                     return (back_scroll);
533           if (top_scroll)
534                     return (sc_height - 2);
535           return (10000); /* infinity */
536 }
537 
538 /*
539  * Will the entire file fit on one screen?
540  */
get_one_screen(void)541 public int get_one_screen(void)
542 {
543           int nlines;
544           POSITION pos = ch_zero();
545 
546           for (nlines = 0;  nlines < sc_height;  nlines++)
547           {
548                     pos = forw_line(pos);
549                     if (pos == NULL_POSITION) break;
550           }
551           return (nlines < sc_height);
552 }
553