Update to less-418:
[dragonfly.git] / contrib / less-403 / forwback.c
1 /*
2  * Copyright (C) 1984-2007  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 public int forw_prompt;
25
26 extern int sigs;
27 extern int top_scroll;
28 extern int quiet;
29 extern int sc_width, sc_height;
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 extern int oldbot;
37 #if TAGS
38 extern char *tagoption;
39 #endif
40
41 /*
42  * Sound the bell to indicate user is trying to move past end of file.
43  */
44         static void
45 eof_bell()
46 {
47         if (quiet == NOT_QUIET)
48                 bell();
49         else
50                 vbell();
51 }
52
53 /*
54  * Check to see if the end of file is currently "displayed".
55  */
56         static void
57 eof_check()
58 {
59         POSITION pos;
60
61         if (ignore_eoi)
62                 return;
63         if (ABORT_SIGS())
64                 return;
65         /*
66          * If the bottom line is empty, we are at EOF.
67          * If the bottom line ends at the file length,
68          * we must be just at EOF.
69          */
70         pos = position(BOTTOM_PLUS_ONE);
71         if (pos == NULL_POSITION || pos == ch_length())
72                 hit_eof++;
73 }
74
75 /*
76  * If the screen is "squished", repaint it.
77  * "Squished" means the first displayed line is not at the top
78  * of the screen; this can happen when we display a short file
79  * for the first time.
80  */
81         public void
82 squish_check()
83 {
84         if (!squished)
85                 return;
86         squished = 0;
87         repaint();
88 }
89
90 /*
91  * Display n lines, scrolling forward, 
92  * starting at position pos in the input file.
93  * "force" means display the n lines even if we hit end of file.
94  * "only_last" means display only the last screenful if n > screen size.
95  * "nblank" is the number of blank lines to draw before the first
96  *   real line.  If nblank > 0, the pos must be NULL_POSITION.
97  *   The first real line after the blanks will start at ch_zero().
98  */
99         public void
100 forw(n, pos, force, only_last, nblank)
101         register int n;
102         POSITION pos;
103         int force;
104         int only_last;
105         int nblank;
106 {
107         int eof = 0;
108         int nlines = 0;
109         int do_repaint;
110         static int first_time = 1;
111
112         squish_check();
113
114         /*
115          * do_repaint tells us not to display anything till the end, 
116          * then just repaint the entire screen.
117          * We repaint if we are supposed to display only the last 
118          * screenful and the request is for more than a screenful.
119          * Also if the request exceeds the forward scroll limit
120          * (but not if the request is for exactly a screenful, since
121          * repainting itself involves scrolling forward a screenful).
122          */
123         do_repaint = (only_last && n > sc_height-1) || 
124                 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
125
126         if (!do_repaint)
127         {
128                 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
129                 {
130                         /*
131                          * Start a new screen.
132                          * {{ This is not really desirable if we happen
133                          *    to hit eof in the middle of this screen,
134                          *    but we don't yet know if that will happen. }}
135                          */
136                         pos_clear();
137                         add_forw_pos(pos);
138                         force = 1;
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                                 clear();
159                                 home();
160                         } else if (!first_time)
161                         {
162                                 putstr("...skipping...\n");
163                         }
164                 }
165         }
166
167         while (--n >= 0)
168         {
169                 /*
170                  * Read the next line of input.
171                  */
172                 if (nblank > 0)
173                 {
174                         /*
175                          * Still drawing blanks; don't get a line 
176                          * from the file yet.
177                          * If this is the last blank line, get ready to
178                          * read a line starting at ch_zero() next time.
179                          */
180                         if (--nblank == 0)
181                                 pos = ch_zero();
182                 } else
183                 {
184                         /* 
185                          * Get the next line from the file.
186                          */
187                         pos = forw_line(pos);
188                         if (pos == NULL_POSITION)
189                         {
190                                 /*
191                                  * End of file: stop here unless the top line 
192                                  * is still empty, or "force" is true.
193                                  * Even if force is true, stop when the last
194                                  * line in the file reaches the top of screen.
195                                  */
196                                 eof = 1;
197                                 if (!force && position(TOP) != NULL_POSITION)
198                                         break;
199                                 if (!empty_lines(0, 0) && 
200                                     !empty_lines(1, 1) &&
201                                      empty_lines(2, sc_height-1))
202                                         break;
203                         }
204                 }
205                 /*
206                  * Add the position of the next line to the position table.
207                  * Display the current line on the screen.
208                  */
209                 add_forw_pos(pos);
210                 nlines++;
211                 if (do_repaint)
212                         continue;
213                 /*
214                  * If this is the first screen displayed and
215                  * we hit an early EOF (i.e. before the requested
216                  * number of lines), we "squish" the display down
217                  * at the bottom of the screen.
218                  * But don't do this if a + option or a -t option
219                  * was given.  These options can cause us to
220                  * start the display after the beginning of the file,
221                  * and it is not appropriate to squish in that case.
222                  */
223                 if (first_time && pos == NULL_POSITION && !top_scroll && 
224 #if TAGS
225                     tagoption == NULL &&
226 #endif
227                     !plusoption)
228                 {
229                         squished = 1;
230                         continue;
231                 }
232                 put_line();
233 #if 0
234                 /* {{ 
235                  * Can't call clear_eol here.  The cursor might be at end of line
236                  * on an ignaw terminal, so clear_eol would clear the last char
237                  * of the current line instead of all of the next line.
238                  * If we really need to do this on clear_bg terminals, we need
239                  * to find a better way.
240                  * }}
241                  */
242                 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
243                 {
244                         /*
245                          * Writing the last character on the last line
246                          * of the display may have scrolled the screen.
247                          * If we were in standout mode, clear_bg terminals 
248                          * will fill the new line with the standout color.
249                          * Now we're in normal mode again, so clear the line.
250                          */
251                         clear_eol();
252                 }
253 #endif
254                 forw_prompt = 1;
255         }
256
257         if (ignore_eoi)
258                 hit_eof = 0;
259         else if (eof && !ABORT_SIGS())
260                 hit_eof++;
261         else
262                 eof_check();
263         if (nlines == 0)
264                 eof_bell();
265         else if (do_repaint)
266                 repaint();
267         first_time = 0;
268         (void) currline(BOTTOM);
269 }
270
271 /*
272  * Display n lines, scrolling backward.
273  */
274         public void
275 back(n, pos, force, only_last)
276         register int n;
277         POSITION pos;
278         int force;
279         int only_last;
280 {
281         int nlines = 0;
282         int do_repaint;
283
284         squish_check();
285         do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
286         hit_eof = 0;
287         while (--n >= 0)
288         {
289                 /*
290                  * Get the previous line of input.
291                  */
292                 pos = back_line(pos);
293                 if (pos == NULL_POSITION)
294                 {
295                         /*
296                          * Beginning of file: stop here unless "force" is true.
297                          */
298                         if (!force)
299                                 break;
300                 }
301                 /*
302                  * Add the position of the previous line to the position table.
303                  * Display the line on the screen.
304                  */
305                 add_back_pos(pos);
306                 nlines++;
307                 if (!do_repaint)
308                 {
309                         home();
310                         add_line();
311                         put_line();
312                 }
313         }
314
315         eof_check();
316         if (nlines == 0)
317                 eof_bell();
318         else if (do_repaint)
319                 repaint();
320         else if (!oldbot)
321                 lower_left();
322         (void) currline(BOTTOM);
323 }
324
325 /*
326  * Display n more lines, forward.
327  * Start just after the line currently displayed at the bottom of the screen.
328  */
329         public void
330 forward(n, force, only_last)
331         int n;
332         int force;
333         int only_last;
334 {
335         POSITION pos;
336
337         if (get_quit_at_eof() && hit_eof && !(ch_getflags() & CH_HELPFILE))
338         {
339                 /*
340                  * If the -e flag is set and we're trying to go
341                  * forward from end-of-file, go on to the next file.
342                  */
343                 if (edit_next(1))
344                         quit(QUIT_OK);
345                 return;
346         }
347
348         pos = position(BOTTOM_PLUS_ONE);
349         if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
350         {
351                 if (ignore_eoi)
352                 {
353                         /*
354                          * ignore_eoi is to support A_F_FOREVER.
355                          * Back up until there is a line at the bottom
356                          * of the screen.
357                          */
358                         if (empty_screen())
359                                 pos = ch_zero();
360                         else
361                         {
362                                 do
363                                 {
364                                         back(1, position(TOP), 1, 0);
365                                         pos = position(BOTTOM_PLUS_ONE);
366                                 } while (pos == NULL_POSITION);
367                         }
368                 } else
369                 {
370                         eof_bell();
371                         hit_eof++;
372                         return;
373                 }
374         }
375         forw(n, pos, force, only_last, 0);
376 }
377
378 /*
379  * Display n more lines, backward.
380  * Start just before the line currently displayed at the top of the screen.
381  */
382         public void
383 backward(n, force, only_last)
384         int n;
385         int force;
386         int only_last;
387 {
388         POSITION pos;
389
390         pos = position(TOP);
391         if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
392         {
393                 eof_bell();
394                 return;   
395         }
396         back(n, pos, force, only_last);
397 }
398
399 /*
400  * Get the backwards scroll limit.
401  * Must call this function instead of just using the value of
402  * back_scroll, because the default case depends on sc_height and
403  * top_scroll, as well as back_scroll.
404  */
405         public int
406 get_back_scroll()
407 {
408         if (no_back_scroll)
409                 return (0);
410         if (back_scroll >= 0)
411                 return (back_scroll);
412         if (top_scroll)
413                 return (sc_height - 2);
414         return (10000); /* infinity */
415 }