2 * Copyright (C) 1984-2015 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.
45 forw_line(POSITION curr_pos)
55 if (curr_pos == NULL_POSITION)
58 return (NULL_POSITION);
61 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
64 * If we are ignoring EOI (command F), only prepare
65 * one line ahead, to avoid getting stuck waiting for
66 * slow data without displaying the data we already have.
67 * If we're not ignoring EOI, we *could* do the same, but
68 * for efficiency we prepare several lines ahead at once.
70 prep_hilite(curr_pos, curr_pos + 3*size_linebuf,
72 curr_pos = next_unfiltered(curr_pos);
75 if (ch_seek(curr_pos))
78 return (NULL_POSITION);
82 * Step back to the beginning of the line.
90 return (NULL_POSITION);
104 * Read forward again to the position we should start at.
108 (void) ch_seek(base_pos);
110 while (new_pos < curr_pos)
115 return (NULL_POSITION);
118 backchars = pappend(c, new_pos);
123 new_pos -= backchars;
124 while (--backchars >= 0)
125 (void) ch_back_get();
132 * Read the first character to display.
138 return (NULL_POSITION);
140 blankline = (c == '\n' || c == '\r');
143 * Read each character in the line and append to the line buffer.
150 return (NULL_POSITION);
152 if (c == '\n' || c == EOI)
157 backchars = pflushmbc();
159 if (backchars > 0 && !chopline && hshift == 0)
161 new_pos -= backchars + 1;
171 * Append the char to the line and get the next char.
173 backchars = pappend(c, ch_tell()-1);
177 * The char won't fit in the line; the line
178 * is too long to print in the screen width.
181 if (chopline || hshift > 0)
188 return (NULL_POSITION);
191 } while (c != '\n' && c != EOI);
194 quit_if_one_screen = FALSE;
197 new_pos = ch_tell() - backchars;
208 if (is_filtered(base_pos))
211 * We don't want to display this line.
218 if (status_col && is_hilited(base_pos, ch_tell()-1, 1, NULL))
222 if (squeeze && blankline)
225 * This line is blank.
226 * Skip down to the last contiguous blank line
227 * and pretend it is the one which we are returning.
229 while ((c = ch_forw_get()) == '\n' || c == '\r')
233 return (NULL_POSITION);
236 (void) ch_back_get();
244 * Get the previous line.
245 * A "current" position is passed and a "new" position is returned.
246 * The current position is the position of the first character of
247 * a line. The new position is the position of the first character
248 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
251 back_line(POSITION curr_pos)
253 POSITION new_pos, begin_new_pos, base_pos;
259 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
262 return (NULL_POSITION);
265 if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
266 prep_hilite((curr_pos < 3*size_linebuf) ?
267 0 : curr_pos - 3*size_linebuf, curr_pos, -1);
269 if (ch_seek(curr_pos-1))
272 return (NULL_POSITION);
278 * Find out if the "current" line was blank.
280 (void) ch_forw_get(); /* Skip the newline */
281 c = ch_forw_get(); /* First char of "current" line */
282 (void) ch_back_get(); /* Restore our position */
283 (void) ch_back_get();
285 if (c == '\n' || c == '\r')
288 * The "current" line was blank.
289 * Skip over any preceding blank lines,
290 * since we skipped them in forw_line().
292 while ((c = ch_back_get()) == '\n' || c == '\r')
296 return (NULL_POSITION);
301 return (NULL_POSITION);
303 (void) ch_forw_get();
308 * Scan backwards until we hit the beginning of the line.
315 return (NULL_POSITION);
321 * This is the newline ending the previous line.
322 * We have hit the beginning of the line.
324 base_pos = ch_tell() + 1;
330 * We have hit the beginning of the file.
331 * This must be the first line in the file.
332 * This must, of course, be the beginning of the line.
334 base_pos = ch_tell();
340 * Now scan forwards from the beginning of this line.
341 * We keep discarding "printable lines" (based on screen width)
342 * until we reach the curr_pos.
344 * {{ This algorithm is pretty inefficient if the lines
345 * are much longer than the screen width,
346 * but I don't know of any better way. }}
349 if (ch_seek(new_pos))
352 return (NULL_POSITION);
358 begin_new_pos = new_pos;
359 (void) ch_seek(new_pos);
364 if (c == EOI || ABORT_SIGS())
367 return (NULL_POSITION);
372 backchars = pflushmbc();
373 if (backchars > 0 && !chopline && hshift == 0)
381 backchars = pappend(c, ch_tell()-1);
385 * Got a full printable line, but we haven't
386 * reached our curr_pos yet. Discard the line
387 * and start a new one.
389 if (chopline || hshift > 0)
392 quit_if_one_screen = FALSE;
397 while (backchars-- > 0)
399 (void) ch_back_get();
404 } while (new_pos < curr_pos);
409 if (is_filtered(base_pos))
412 * We don't want to display this line.
413 * Get the previous line.
415 curr_pos = begin_new_pos;
419 if (status_col && curr_pos > 0 && is_hilited(base_pos, curr_pos-1, 1, NULL))
423 return (begin_new_pos);
430 set_attnpos(POSITION pos)
434 if (pos != NULL_POSITION)
443 if (c == '\n' || c == '\r')
445 (void) ch_back_get();
454 if (c == EOI || c == '\n' || c == '\r')