2 * Copyright (C) 1984-2014 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.
12 * High level routines dealing with getting lines of input
13 * from the file being viewed.
15 * When we speak of "lines" here, we mean PRINTABLE lines;
16 * lines processed with respect to the screen width.
17 * We use the term "raw line" to refer to lines simply
18 * delimited by newlines; not processed with respect to screen width.
26 extern int quit_if_one_screen;
28 extern int ignore_eoi;
29 extern int status_col;
30 extern POSITION start_attnpos;
31 extern POSITION end_attnpos;
33 extern int hilite_search;
34 extern int size_linebuf;
39 * A "current" position is passed and a "new" position is returned.
40 * The current position is the position of the first character of
41 * a line. The new position is the position of the first character
42 * 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.
151 return (NULL_POSITION);
153 if (c == '\n' || c == EOI)
158 backchars = pflushmbc();
160 if (backchars > 0 && !chopline && hshift == 0)
162 new_pos -= backchars + 1;
172 * Append the char to the line and get the next char.
174 backchars = pappend(c, ch_tell()-1);
178 * The char won't fit in the line; the line
179 * is too long to print in the screen width.
182 if (chopline || hshift > 0)
189 return (NULL_POSITION);
192 } while (c != '\n' && c != EOI);
195 quit_if_one_screen = FALSE;
198 new_pos = ch_tell() - backchars;
209 if (is_filtered(base_pos))
212 * We don't want to display this line.
219 if (status_col && is_hilited(base_pos, ch_tell()-1, 1, NULL))
223 if (squeeze && blankline)
226 * This line is blank.
227 * Skip down to the last contiguous blank line
228 * and pretend it is the one which we are returning.
230 while ((c = ch_forw_get()) == '\n' || c == '\r')
234 return (NULL_POSITION);
237 (void) ch_back_get();
245 * Get the previous line.
246 * A "current" position is passed and a "new" position is returned.
247 * The current position is the position of the first character of
248 * a line. The new position is the position of the first character
249 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
255 POSITION new_pos, begin_new_pos, base_pos;
261 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
264 return (NULL_POSITION);
267 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
268 prep_hilite((curr_pos < 3*size_linebuf) ?
269 0 : curr_pos - 3*size_linebuf, curr_pos, -1);
271 if (ch_seek(curr_pos-1))
274 return (NULL_POSITION);
280 * Find out if the "current" line was blank.
282 (void) ch_forw_get(); /* Skip the newline */
283 c = ch_forw_get(); /* First char of "current" line */
284 (void) ch_back_get(); /* Restore our position */
285 (void) ch_back_get();
287 if (c == '\n' || c == '\r')
290 * The "current" line was blank.
291 * Skip over any preceding blank lines,
292 * since we skipped them in forw_line().
294 while ((c = ch_back_get()) == '\n' || c == '\r')
298 return (NULL_POSITION);
303 return (NULL_POSITION);
305 (void) ch_forw_get();
310 * Scan backwards until we hit the beginning of the line.
317 return (NULL_POSITION);
323 * This is the newline ending the previous line.
324 * We have hit the beginning of the line.
326 base_pos = ch_tell() + 1;
332 * We have hit the beginning of the file.
333 * This must be the first line in the file.
334 * This must, of course, be the beginning of the line.
336 base_pos = ch_tell();
342 * Now scan forwards from the beginning of this line.
343 * We keep discarding "printable lines" (based on screen width)
344 * until we reach the curr_pos.
346 * {{ This algorithm is pretty inefficient if the lines
347 * are much longer than the screen width,
348 * but I don't know of any better way. }}
351 if (ch_seek(new_pos))
354 return (NULL_POSITION);
360 begin_new_pos = new_pos;
361 (void) ch_seek(new_pos);
366 if (c == EOI || ABORT_SIGS())
369 return (NULL_POSITION);
374 backchars = pflushmbc();
375 if (backchars > 0 && !chopline && hshift == 0)
383 backchars = pappend(c, ch_tell()-1);
387 * Got a full printable line, but we haven't
388 * reached our curr_pos yet. Discard the line
389 * and start a new one.
391 if (chopline || hshift > 0)
394 quit_if_one_screen = FALSE;
399 while (backchars-- > 0)
401 (void) ch_back_get();
406 } while (new_pos < curr_pos);
411 if (is_filtered(base_pos))
414 * We don't want to display this line.
415 * Get the previous line.
417 curr_pos = begin_new_pos;
421 if (status_col && curr_pos > 0 && is_hilited(base_pos, curr_pos-1, 1, NULL))
425 return (begin_new_pos);
437 if (pos != NULL_POSITION)
446 if (c == '\n' || c == '\r')
448 (void) ch_back_get();
457 if (c == EOI || c == '\n' || c == '\r')