Upgrade to less version 403.
[dragonfly.git] / contrib / less-394 / forwback.c
1 /*
2  * Copyright (C) 1984-2005  Mark Nudelman
3  *
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.
6  *
7  * For more information about less, or for information on how to 
8  * contact the author, see the README file.
9  */
10
11
12 /*
13  * Primitives for displaying the file on the screen,
14  * scrolling either forward or backward.
15  */
16
17 #include "less.h"
18 #include "position.h"
19
20 public int hit_eof;     /* Keeps track of how many times we hit end of file */
21 public int screen_trashed;
22 public int squished;
23 public int no_back_scroll = 0;
24
25 extern int sigs;
26 extern int top_scroll;
27 extern int quiet;
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;
34 extern int clear_bg;
35 extern int final_attr;
36 #if TAGS
37 extern char *tagoption;
38 #endif
39
40 /*
41  * Sound the bell to indicate user is trying to move past end of file.
42  */
43         static void
44 eof_bell()
45 {
46         if (quiet == NOT_QUIET)
47                 bell();
48         else
49                 vbell();
50 }
51
52 /*
53  * Check to see if the end of file is currently "displayed".
54  */
55         static void
56 eof_check()
57 {
58         POSITION pos;
59
60         if (ignore_eoi)
61                 return;
62         if (ABORT_SIGS())
63                 return;
64         /*
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.
68          */
69         pos = position(BOTTOM_PLUS_ONE);
70         if (pos == NULL_POSITION || pos == ch_length())
71                 hit_eof++;
72 }
73
74 /*
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
78  * for the first time.
79  */
80         static void
81 squish_check()
82 {
83         if (!squished)
84                 return;
85         squished = 0;
86         repaint();
87 }
88
89 /*
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().
97  */
98         public void
99 forw(n, pos, force, only_last, nblank)
100         register int n;
101         POSITION pos;
102         int force;
103         int only_last;
104         int nblank;
105 {
106         int eof = 0;
107         int nlines = 0;
108         int do_repaint;
109         static int first_time = 1;
110
111         squish_check();
112
113         /*
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).
121          */
122         do_repaint = (only_last && n > sc_height-1) || 
123                 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
124
125         if (!do_repaint)
126         {
127                 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
128                 {
129                         /*
130                          * Start a new screen.
131                          * {{ This is not really desirable if we happen
132                          *    to hit eof in the middle of this screen,
133                          *    but we don't yet know if that will happen. }}
134                          */
135                         pos_clear();
136                         add_forw_pos(pos);
137                         force = 1;
138                         if (top_scroll == OPT_ONPLUS || (first_time && top_scroll != OPT_ON))
139                                 clear();
140                         home();
141                 } else
142                 {
143                         clear_bot();
144                 }
145
146                 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
147                 {
148                         /*
149                          * This is not contiguous with what is
150                          * currently displayed.  Clear the screen image 
151                          * (position table) and start a new screen.
152                          */
153                         pos_clear();
154                         add_forw_pos(pos);
155                         force = 1;
156                         if (top_scroll)
157                         {
158                                 if (top_scroll == OPT_ONPLUS)
159                                         clear();
160                                 home();
161                         } else if (!first_time)
162                         {
163                                 putstr("...skipping...\n");
164                         }
165                 }
166         }
167
168         while (--n >= 0)
169         {
170                 /*
171                  * Read the next line of input.
172                  */
173                 if (nblank > 0)
174                 {
175                         /*
176                          * Still drawing blanks; don't get a line 
177                          * from the file yet.
178                          * If this is the last blank line, get ready to
179                          * read a line starting at ch_zero() next time.
180                          */
181                         if (--nblank == 0)
182                                 pos = ch_zero();
183                 } else
184                 {
185                         /* 
186                          * Get the next line from the file.
187                          */
188                         pos = forw_line(pos);
189                         if (pos == NULL_POSITION)
190                         {
191                                 /*
192                                  * End of file: stop here unless the top line 
193                                  * is still empty, or "force" is true.
194                                  * Even if force is true, stop when the last
195                                  * line in the file reaches the top of screen.
196                                  */
197                                 eof = 1;
198                                 if (!force && position(TOP) != NULL_POSITION)
199                                         break;
200                                 if (!empty_lines(0, 0) && 
201                                     !empty_lines(1, 1) &&
202                                      empty_lines(2, sc_height-1))
203                                         break;
204                         }
205                 }
206                 /*
207                  * Add the position of the next line to the position table.
208                  * Display the current line on the screen.
209                  */
210                 add_forw_pos(pos);
211                 nlines++;
212                 if (do_repaint)
213                         continue;
214                 /*
215                  * If this is the first screen displayed and
216                  * we hit an early EOF (i.e. before the requested
217                  * number of lines), we "squish" the display down
218                  * at the bottom of the screen.
219                  * But don't do this if a + option or a -t option
220                  * was given.  These options can cause us to
221                  * start the display after the beginning of the file,
222                  * and it is not appropriate to squish in that case.
223                  */
224                 if (first_time && pos == NULL_POSITION && !top_scroll && 
225 #if TAGS
226                     tagoption == NULL &&
227 #endif
228                     !plusoption)
229                 {
230                         squished = 1;
231                         continue;
232                 }
233                 if (top_scroll == OPT_ON)
234                         clear_eol();
235                 put_line();
236                 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
237                 {
238                         /*
239                          * Writing the last character on the last line
240                          * of the display may have scrolled the screen.
241                          * If we were in standout mode, clear_bg terminals 
242                          * will fill the new line with the standout color.
243                          * Now we're in normal mode again, so clear the line.
244                          */
245                         clear_eol();
246                 }
247         }
248
249         if (ignore_eoi)
250                 hit_eof = 0;
251         else if (eof && !ABORT_SIGS())
252                 hit_eof++;
253         else
254                 eof_check();
255         if (nlines == 0)
256                 eof_bell();
257         else if (do_repaint)
258                 repaint();
259         first_time = 0;
260         (void) currline(BOTTOM);
261 }
262
263 /*
264  * Display n lines, scrolling backward.
265  */
266         public void
267 back(n, pos, force, only_last)
268         register int n;
269         POSITION pos;
270         int force;
271         int only_last;
272 {
273         int nlines = 0;
274         int do_repaint;
275
276         squish_check();
277         do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
278         hit_eof = 0;
279         while (--n >= 0)
280         {
281                 /*
282                  * Get the previous line of input.
283                  */
284                 pos = back_line(pos);
285                 if (pos == NULL_POSITION)
286                 {
287                         /*
288                          * Beginning of file: stop here unless "force" is true.
289                          */
290                         if (!force)
291                                 break;
292                 }
293                 /*
294                  * Add the position of the previous line to the position table.
295                  * Display the line on the screen.
296                  */
297                 add_back_pos(pos);
298                 nlines++;
299                 if (!do_repaint)
300                 {
301                         home();
302                         add_line();
303                         put_line();
304                 }
305         }
306
307         eof_check();
308         if (nlines == 0)
309                 eof_bell();
310         else if (do_repaint)
311                 repaint();
312         (void) currline(BOTTOM);
313 }
314
315 /*
316  * Display n more lines, forward.
317  * Start just after the line currently displayed at the bottom of the screen.
318  */
319         public void
320 forward(n, force, only_last)
321         int n;
322         int force;
323         int only_last;
324 {
325         POSITION pos;
326
327         if (quit_at_eof && hit_eof && !(ch_getflags() & CH_HELPFILE))
328         {
329                 /*
330                  * If the -e flag is set and we're trying to go
331                  * forward from end-of-file, go on to the next file.
332                  */
333                 if (edit_next(1))
334                         quit(QUIT_OK);
335                 return;
336         }
337
338         pos = position(BOTTOM_PLUS_ONE);
339         if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
340         {
341                 if (ignore_eoi)
342                 {
343                         /*
344                          * ignore_eoi is to support A_F_FOREVER.
345                          * Back up until there is a line at the bottom
346                          * of the screen.
347                          */
348                         if (empty_screen())
349                                 pos = ch_zero();
350                         else
351                         {
352                                 do
353                                 {
354                                         back(1, position(TOP), 1, 0);
355                                         pos = position(BOTTOM_PLUS_ONE);
356                                 } while (pos == NULL_POSITION);
357                         }
358                 } else
359                 {
360                         eof_bell();
361                         hit_eof++;
362                         return;
363                 }
364         }
365         forw(n, pos, force, only_last, 0);
366 }
367
368 /*
369  * Display n more lines, backward.
370  * Start just before the line currently displayed at the top of the screen.
371  */
372         public void
373 backward(n, force, only_last)
374         int n;
375         int force;
376         int only_last;
377 {
378         POSITION pos;
379
380         pos = position(TOP);
381         if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
382         {
383                 eof_bell();
384                 return;   
385         }
386         back(n, pos, force, only_last);
387 }
388
389 /*
390  * Get the backwards scroll limit.
391  * Must call this function instead of just using the value of
392  * back_scroll, because the default case depends on sc_height and
393  * top_scroll, as well as back_scroll.
394  */
395         public int
396 get_back_scroll()
397 {
398         if (no_back_scroll)
399                 return (0);
400         if (back_scroll >= 0)
401                 return (back_scroll);
402         if (top_scroll)
403                 return (sc_height - 2);
404         return (10000); /* infinity */
405 }