2 * Copyright (C) 1984-2017 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, see the README file.
11 * High level routines dealing with getting lines of input
12 * from the file being viewed.
14 * When we speak of "lines" here, we mean PRINTABLE lines;
15 * lines processed with respect to the screen width.
16 * We use the term "raw line" to refer to lines simply
17 * delimited by newlines; not processed with respect to screen width.
25 extern int quit_if_one_screen;
27 extern int ignore_eoi;
28 extern int status_col;
29 extern POSITION start_attnpos;
30 extern POSITION end_attnpos;
32 extern int hilite_search;
33 extern int size_linebuf;
38 * A "current" position is passed and a "new" position is returned.
39 * The current position is the position of the first character of
40 * a line. The new position is the position of the first character
41 * of the NEXT line. The line obtained is the line starting at curr_pos.
56 if (curr_pos == NULL_POSITION)
59 return (NULL_POSITION);
62 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,
73 curr_pos = next_unfiltered(curr_pos);
76 if (ch_seek(curr_pos))
79 return (NULL_POSITION);
83 * Step back to the beginning of the line.
91 return (NULL_POSITION);
105 * Read forward again to the position we should start at.
109 (void) ch_seek(base_pos);
111 while (new_pos < curr_pos)
116 return (NULL_POSITION);
119 backchars = pappend(c, new_pos);
124 new_pos -= backchars;
125 while (--backchars >= 0)
126 (void) ch_back_get();
133 * Read the first character to display.
139 return (NULL_POSITION);
141 blankline = (c == '\n' || c == '\r');
144 * Read each character in the line and append to the line buffer.
152 return (NULL_POSITION);
154 if (c == '\n' || c == EOI)
159 backchars = pflushmbc();
161 if (backchars > 0 && !chopline && hshift == 0)
163 new_pos -= backchars + 1;
173 * Append the char to the line and get the next char.
175 backchars = pappend(c, ch_tell()-1);
179 * The char won't fit in the line; the line
180 * is too long to print in the screen width.
183 if (chopline || hshift > 0)
190 return (NULL_POSITION);
193 } while (c != '\n' && c != EOI);
196 quit_if_one_screen = FALSE;
200 new_pos = ch_tell() - backchars;
208 pdone(endline, chopped, 1);
211 if (is_filtered(base_pos))
214 * We don't want to display this line.
221 if (status_col && is_hilited(base_pos, ch_tell()-1, 1, NULL))
225 if (squeeze && blankline)
228 * This line is blank.
229 * Skip down to the last contiguous blank line
230 * and pretend it is the one which we are returning.
232 while ((c = ch_forw_get()) == '\n' || c == '\r')
236 return (NULL_POSITION);
239 (void) ch_back_get();
247 * Get the previous line.
248 * A "current" position is passed and a "new" position is returned.
249 * The current position is the position of the first character of
250 * a line. The new position is the position of the first character
251 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
257 POSITION new_pos, begin_new_pos, base_pos;
264 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
267 return (NULL_POSITION);
270 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
271 prep_hilite((curr_pos < 3*size_linebuf) ?
272 0 : curr_pos - 3*size_linebuf, curr_pos, -1);
274 if (ch_seek(curr_pos-1))
277 return (NULL_POSITION);
283 * Find out if the "current" line was blank.
285 (void) ch_forw_get(); /* Skip the newline */
286 c = ch_forw_get(); /* First char of "current" line */
287 (void) ch_back_get(); /* Restore our position */
288 (void) ch_back_get();
290 if (c == '\n' || c == '\r')
293 * The "current" line was blank.
294 * Skip over any preceding blank lines,
295 * since we skipped them in forw_line().
297 while ((c = ch_back_get()) == '\n' || c == '\r')
301 return (NULL_POSITION);
306 return (NULL_POSITION);
308 (void) ch_forw_get();
313 * Scan backwards until we hit the beginning of the line.
320 return (NULL_POSITION);
326 * This is the newline ending the previous line.
327 * We have hit the beginning of the line.
329 base_pos = ch_tell() + 1;
335 * We have hit the beginning of the file.
336 * This must be the first line in the file.
337 * This must, of course, be the beginning of the line.
339 base_pos = ch_tell();
345 * Now scan forwards from the beginning of this line.
346 * We keep discarding "printable lines" (based on screen width)
347 * until we reach the curr_pos.
349 * {{ This algorithm is pretty inefficient if the lines
350 * are much longer than the screen width,
351 * but I don't know of any better way. }}
354 if (ch_seek(new_pos))
357 return (NULL_POSITION);
363 begin_new_pos = new_pos;
364 (void) ch_seek(new_pos);
370 if (c == EOI || ABORT_SIGS())
373 return (NULL_POSITION);
378 backchars = pflushmbc();
379 if (backchars > 0 && !chopline && hshift == 0)
387 backchars = pappend(c, ch_tell()-1);
391 * Got a full printable line, but we haven't
392 * reached our curr_pos yet. Discard the line
393 * and start a new one.
395 if (chopline || hshift > 0)
399 quit_if_one_screen = FALSE;
404 while (backchars-- > 0)
406 (void) ch_back_get();
411 } while (new_pos < curr_pos);
413 pdone(endline, chopped, 0);
416 if (is_filtered(base_pos))
419 * We don't want to display this line.
420 * Get the previous line.
422 curr_pos = begin_new_pos;
426 if (status_col && curr_pos > 0 && is_hilited(base_pos, curr_pos-1, 1, NULL))
430 return (begin_new_pos);
442 if (pos != NULL_POSITION)
451 if (c == '\n' || c == '\r')
453 (void) ch_back_get();
462 if (c == EOI || c == '\n' || c == '\r')