]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/less/forwback.c
Import Amazon Elastic Network Adapter (ENA) HAL to sys/contrib/
[FreeBSD/FreeBSD.git] / contrib / less / forwback.c
1 /* $FreeBSD$ */
2 /*
3  * Copyright (C) 1984-2015  Mark Nudelman
4  *
5  * You may distribute under the terms of either the GNU General Public
6  * License or the Less License, as specified in the README file.
7  *
8  * For more information, 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 screen_trashed;
21 public int squished;
22 public int no_back_scroll = 0;
23 public int forw_prompt;
24 public int same_pos_bell = 1;
25
26 extern int sigs;
27 extern int top_scroll;
28 extern int quiet;
29 extern int sc_width, sc_height;
30 extern int less_is_more;
31 extern int plusoption;
32 extern int forw_scroll;
33 extern int back_scroll;
34 extern int ignore_eoi;
35 extern int clear_bg;
36 extern int final_attr;
37 extern int oldbot;
38 #if HILITE_SEARCH
39 extern int size_linebuf;
40 extern int hilite_search;
41 extern int status_col;
42 #endif
43 #if TAGS
44 extern char *tagoption;
45 #endif
46
47 /*
48  * Sound the bell to indicate user is trying to move past end of file.
49  */
50         static void
51 eof_bell(void)
52 {
53         if (quiet == NOT_QUIET)
54                 bell();
55         else
56                 vbell();
57 }
58
59 /*
60  * Check to see if the end of file is currently displayed.
61  */
62         public int
63 eof_displayed(void)
64 {
65         POSITION pos;
66
67         if (ignore_eoi)
68                 return (0);
69
70         if (ch_length() == NULL_POSITION)
71                 /*
72                  * If the file length is not known,
73                  * we can't possibly be displaying EOF.
74                  */
75                 return (0);
76
77         /*
78          * If the bottom line is empty, we are at EOF.
79          * If the bottom line ends at the file length,
80          * we must be just at EOF.
81          */
82         pos = position(BOTTOM_PLUS_ONE);
83         return (pos == NULL_POSITION || pos == ch_length());
84 }
85
86 /*
87  * Check to see if the entire file is currently displayed.
88  */
89         public int
90 entire_file_displayed(void)
91 {
92         POSITION pos;
93
94         /* Make sure last line of file is displayed. */
95         if (!eof_displayed())
96                 return (0);
97
98         /* Make sure first line of file is displayed. */
99         pos = position(0);
100         return (pos == NULL_POSITION || pos == 0);
101 }
102
103 /*
104  * If the screen is "squished", repaint it.
105  * "Squished" means the first displayed line is not at the top
106  * of the screen; this can happen when we display a short file
107  * for the first time.
108  */
109         public void
110 squish_check(void)
111 {
112         if (!squished)
113                 return;
114         squished = 0;
115         repaint();
116 }
117
118 /*
119  * Display n lines, scrolling forward, 
120  * starting at position pos in the input file.
121  * "force" means display the n lines even if we hit end of file.
122  * "only_last" means display only the last screenful if n > screen size.
123  * "nblank" is the number of blank lines to draw before the first
124  *   real line.  If nblank > 0, the pos must be NULL_POSITION.
125  *   The first real line after the blanks will start at ch_zero().
126  */
127         public void
128 forw(int n, POSITION pos, int force, int only_last, int nblank)
129 {
130         int nlines = 0;
131         int do_repaint;
132         static int first_time = 1;
133
134         squish_check();
135
136         /*
137          * do_repaint tells us not to display anything till the end, 
138          * then just repaint the entire screen.
139          * We repaint if we are supposed to display only the last 
140          * screenful and the request is for more than a screenful.
141          * Also if the request exceeds the forward scroll limit
142          * (but not if the request is for exactly a screenful, since
143          * repainting itself involves scrolling forward a screenful).
144          */
145         do_repaint = (only_last && n > sc_height-1) || 
146                 (forw_scroll >= 0 && n > forw_scroll && n != sc_height-1);
147
148 #if HILITE_SEARCH
149         if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
150                 prep_hilite(pos, pos + 4*size_linebuf, ignore_eoi ? 1 : -1);
151                 pos = next_unfiltered(pos);
152         }
153 #endif
154
155         if (!do_repaint)
156         {
157                 if (top_scroll && n >= sc_height - 1 && pos != ch_length())
158                 {
159                         /*
160                          * Start a new screen.
161                          * {{ This is not really desirable if we happen
162                          *    to hit eof in the middle of this screen,
163                          *    but we don't yet know if that will happen. }}
164                          */
165                         pos_clear();
166                         add_forw_pos(pos);
167                         force = 1;
168                         if (less_is_more == 0) {
169                                 clear();
170                                 home();
171                         }
172                 }
173
174                 if (pos != position(BOTTOM_PLUS_ONE) || empty_screen())
175                 {
176                         /*
177                          * This is not contiguous with what is
178                          * currently displayed.  Clear the screen image 
179                          * (position table) and start a new screen.
180                          */
181                         pos_clear();
182                         add_forw_pos(pos);
183                         force = 1;
184                         if (top_scroll)
185                         {
186                                 clear();
187                                 home();
188                         } else if (!first_time)
189                         {
190                                 putstr("...skipping...\n");
191                         }
192                 }
193         }
194
195         while (--n >= 0)
196         {
197                 /*
198                  * Read the next line of input.
199                  */
200                 if (nblank > 0)
201                 {
202                         /*
203                          * Still drawing blanks; don't get a line 
204                          * from the file yet.
205                          * If this is the last blank line, get ready to
206                          * read a line starting at ch_zero() next time.
207                          */
208                         if (--nblank == 0)
209                                 pos = ch_zero();
210                 } else
211                 {
212                         /* 
213                          * Get the next line from the file.
214                          */
215                         pos = forw_line(pos);
216 #if HILITE_SEARCH
217                         pos = next_unfiltered(pos);
218 #endif
219                         if (pos == NULL_POSITION)
220                         {
221                                 /*
222                                  * End of file: stop here unless the top line 
223                                  * is still empty, or "force" is true.
224                                  * Even if force is true, stop when the last
225                                  * line in the file reaches the top of screen.
226                                  */
227                                 if (!force && position(TOP) != NULL_POSITION)
228                                         break;
229                                 if (!empty_lines(0, 0) && 
230                                     !empty_lines(1, 1) &&
231                                      empty_lines(2, sc_height-1))
232                                         break;
233                         }
234                 }
235                 /*
236                  * Add the position of the next line to the position table.
237                  * Display the current line on the screen.
238                  */
239                 add_forw_pos(pos);
240                 nlines++;
241                 if (do_repaint)
242                         continue;
243                 /*
244                  * If this is the first screen displayed and
245                  * we hit an early EOF (i.e. before the requested
246                  * number of lines), we "squish" the display down
247                  * at the bottom of the screen.
248                  * But don't do this if a + option or a -t option
249                  * was given.  These options can cause us to
250                  * start the display after the beginning of the file,
251                  * and it is not appropriate to squish in that case.
252                  */
253                 if ((first_time || less_is_more) &&
254                     pos == NULL_POSITION && !top_scroll && 
255 #if TAGS
256                     tagoption == NULL &&
257 #endif
258                     !plusoption)
259                 {
260                         squished = 1;
261                         continue;
262                 }
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                  * }}
272                  */
273                 if (clear_bg && apply_at_specials(final_attr) != AT_NORMAL)
274                 {
275                         /*
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.
281                          */
282                         clear_eol();
283                 }
284 #endif
285                 forw_prompt = 1;
286         }
287
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);
294 }
295
296 /*
297  * Display n lines, scrolling backward.
298  */
299         public void
300 back(int n, POSITION pos, int force, int only_last)
301 {
302         int nlines = 0;
303         int do_repaint;
304
305         squish_check();
306         do_repaint = (n > get_back_scroll() || (only_last && n > sc_height-1));
307 #if HILITE_SEARCH
308         if (hilite_search == OPT_ONPLUS || is_filtering() || status_col) {
309                 prep_hilite((pos < 3*size_linebuf) ?  0 : pos - 3*size_linebuf, pos, -1);
310         }
311 #endif
312         while (--n >= 0)
313         {
314                 /*
315                  * Get the previous line of input.
316                  */
317 #if HILITE_SEARCH
318                 pos = prev_unfiltered(pos);
319 #endif
320
321                 pos = back_line(pos);
322                 if (pos == NULL_POSITION)
323                 {
324                         /*
325                          * Beginning of file: stop here unless "force" is true.
326                          */
327                         if (!force)
328                                 break;
329                 }
330                 /*
331                  * Add the position of the previous line to the position table.
332                  * Display the line on the screen.
333                  */
334                 add_back_pos(pos);
335                 nlines++;
336                 if (!do_repaint)
337                 {
338                         home();
339                         add_line();
340                         put_line();
341                 }
342         }
343
344         if (nlines == 0 && same_pos_bell)
345                 eof_bell();
346         else if (do_repaint)
347                 repaint();
348         else if (!oldbot)
349                 lower_left();
350         (void) currline(BOTTOM);
351 }
352
353 /*
354  * Display n more lines, forward.
355  * Start just after the line currently displayed at the bottom of the screen.
356  */
357         public void
358 forward(int n, int force, int only_last)
359 {
360         POSITION pos;
361
362         if (get_quit_at_eof() && eof_displayed() && !(ch_getflags() & CH_HELPFILE))
363         {
364                 /*
365                  * If the -e flag is set and we're trying to go
366                  * forward from end-of-file, go on to the next file.
367                  */
368                 if (edit_next(1))
369                         quit(QUIT_OK);
370                 return;
371         }
372
373         pos = position(BOTTOM_PLUS_ONE);
374         if (pos == NULL_POSITION && (!force || empty_lines(2, sc_height-1)))
375         {
376                 if (ignore_eoi)
377                 {
378                         /*
379                          * ignore_eoi is to support A_F_FOREVER.
380                          * Back up until there is a line at the bottom
381                          * of the screen.
382                          */
383                         if (empty_screen())
384                                 pos = ch_zero();
385                         else
386                         {
387                                 do
388                                 {
389                                         back(1, position(TOP), 1, 0);
390                                         pos = position(BOTTOM_PLUS_ONE);
391                                 } while (pos == NULL_POSITION);
392                         }
393                 } else
394                 {
395                         eof_bell();
396                         return;
397                 }
398         }
399         forw(n, pos, force, only_last, 0);
400 }
401
402 /*
403  * Display n more lines, backward.
404  * Start just before the line currently displayed at the top of the screen.
405  */
406         public void
407 backward(int n, int force, int only_last)
408 {
409         POSITION pos;
410
411         pos = position(TOP);
412         if (pos == NULL_POSITION && (!force || position(BOTTOM) == 0))
413         {
414                 eof_bell();
415                 return;   
416         }
417         back(n, pos, force, only_last);
418 }
419
420 /*
421  * Get the backwards scroll limit.
422  * Must call this function instead of just using the value of
423  * back_scroll, because the default case depends on sc_height and
424  * top_scroll, as well as back_scroll.
425  */
426         public int
427 get_back_scroll(void)
428 {
429         if (no_back_scroll)
430                 return (0);
431         if (back_scroll >= 0)
432                 return (back_scroll);
433         if (top_scroll)
434                 return (sc_height - 2);
435         return (10000); /* infinity */
436 }