2 * Copyright (C) 1984-2008 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * High level routines dealing with getting lines of input
14 * from the file being viewed.
16 * When we speak of "lines" here, we mean PRINTABLE lines;
17 * lines processed with respect to the screen width.
18 * We use the term "raw line" to refer to lines simply
19 * delimited by newlines; not processed with respect to screen width.
27 extern int quit_if_one_screen
;
29 extern int ignore_eoi
;
30 extern int status_col
;
31 extern POSITION start_attnpos
;
32 extern POSITION end_attnpos
;
34 extern int hilite_search
;
35 extern int size_linebuf
;
40 * A "current" position is passed and a "new" position is returned.
41 * The current position is the position of the first character of
42 * a line. The new position is the position of the first character
43 * of the NEXT line. The line obtained is the line starting at curr_pos.
57 if (curr_pos
== NULL_POSITION
)
60 return (NULL_POSITION
);
63 if (hilite_search
== OPT_ONPLUS
|| is_filtering() || status_col
)
65 * If we are ignoring EOI (command F), only prepare
66 * one line ahead, to avoid getting stuck waiting for
67 * slow data without displaying the data we already have.
68 * If we're not ignoring EOI, we *could* do the same, but
69 * for efficiency we prepare several lines ahead at once.
71 prep_hilite(curr_pos
, curr_pos
+ 3*size_linebuf
,
74 if (ch_seek(curr_pos
))
77 return (NULL_POSITION
);
81 * Step back to the beginning of the line.
89 return (NULL_POSITION
);
103 * Read forward again to the position we should start at.
107 (void) ch_seek(base_pos
);
109 while (new_pos
< curr_pos
)
114 return (NULL_POSITION
);
117 backchars
= pappend(c
, new_pos
);
122 new_pos
-= backchars
;
123 while (--backchars
>= 0)
124 (void) ch_back_get();
131 * Read the first character to display.
137 return (NULL_POSITION
);
139 blankline
= (c
== '\n' || c
== '\r');
142 * Read each character in the line and append to the line buffer.
149 return (NULL_POSITION
);
151 if (c
== '\n' || c
== EOI
)
156 backchars
= pflushmbc();
158 if (backchars
> 0 && !chopline
&& hshift
== 0)
160 new_pos
-= backchars
+ 1;
170 * Append the char to the line and get the next char.
172 backchars
= pappend(c
, ch_tell()-1);
176 * The char won't fit in the line; the line
177 * is too long to print in the screen width.
180 if (chopline
|| hshift
> 0)
185 } while (c
!= '\n' && c
!= EOI
);
188 quit_if_one_screen
= FALSE
;
191 new_pos
= ch_tell() - backchars
;
202 if (is_filtered(base_pos
))
205 * We don't want to display this line.
212 if (status_col
&& is_hilited(base_pos
, ch_tell()-1, 1, NULL
))
216 if (squeeze
&& blankline
)
219 * This line is blank.
220 * Skip down to the last contiguous blank line
221 * and pretend it is the one which we are returning.
223 while ((c
= ch_forw_get()) == '\n' || c
== '\r')
227 return (NULL_POSITION
);
230 (void) ch_back_get();
238 * Get the previous line.
239 * A "current" position is passed and a "new" position is returned.
240 * The current position is the position of the first character of
241 * a line. The new position is the position of the first character
242 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
248 POSITION new_pos
, begin_new_pos
, base_pos
;
254 if (curr_pos
== NULL_POSITION
|| curr_pos
<= ch_zero())
257 return (NULL_POSITION
);
260 if (hilite_search
== OPT_ONPLUS
|| is_filtering() || status_col
)
261 prep_hilite((curr_pos
< 3*size_linebuf
) ?
262 0 : curr_pos
- 3*size_linebuf
, curr_pos
, -1);
264 if (ch_seek(curr_pos
-1))
267 return (NULL_POSITION
);
273 * Find out if the "current" line was blank.
275 (void) ch_forw_get(); /* Skip the newline */
276 c
= ch_forw_get(); /* First char of "current" line */
277 (void) ch_back_get(); /* Restore our position */
278 (void) ch_back_get();
280 if (c
== '\n' || c
== '\r')
283 * The "current" line was blank.
284 * Skip over any preceding blank lines,
285 * since we skipped them in forw_line().
287 while ((c
= ch_back_get()) == '\n' || c
== '\r')
291 return (NULL_POSITION
);
296 return (NULL_POSITION
);
298 (void) ch_forw_get();
303 * Scan backwards until we hit the beginning of the line.
310 return (NULL_POSITION
);
316 * This is the newline ending the previous line.
317 * We have hit the beginning of the line.
319 base_pos
= ch_tell() + 1;
325 * We have hit the beginning of the file.
326 * This must be the first line in the file.
327 * This must, of course, be the beginning of the line.
329 base_pos
= ch_tell();
335 * Now scan forwards from the beginning of this line.
336 * We keep discarding "printable lines" (based on screen width)
337 * until we reach the curr_pos.
339 * {{ This algorithm is pretty inefficient if the lines
340 * are much longer than the screen width,
341 * but I don't know of any better way. }}
344 if (ch_seek(new_pos
))
347 return (NULL_POSITION
);
353 begin_new_pos
= new_pos
;
354 (void) ch_seek(new_pos
);
359 if (c
== EOI
|| ABORT_SIGS())
362 return (NULL_POSITION
);
367 backchars
= pflushmbc();
368 if (backchars
> 0 && !chopline
&& hshift
== 0)
376 backchars
= pappend(c
, ch_tell()-1);
380 * Got a full printable line, but we haven't
381 * reached our curr_pos yet. Discard the line
382 * and start a new one.
384 if (chopline
|| hshift
> 0)
387 quit_if_one_screen
= FALSE
;
392 while (backchars
-- > 0)
394 (void) ch_back_get();
399 } while (new_pos
< curr_pos
);
401 pdone(endline
, ch_forw_get());
404 if (is_filtered(base_pos
))
407 * We don't want to display this line.
408 * Get the previous line.
410 curr_pos
= begin_new_pos
;
414 if (status_col
&& is_hilited(base_pos
, ch_tell()-1, 1, NULL
))
418 return (begin_new_pos
);
430 if (pos
!= NULL_POSITION
)
439 if (c
!= '\n' && c
!= '\r')
449 if (c
== EOI
|| c
== '\n' || c
== '\r')