2 * Copyright (C) 1984-2011 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)
187 return (NULL_POSITION);
190 } while (c != '\n' && c != EOI);
193 quit_if_one_screen = FALSE;
196 new_pos = ch_tell() - backchars;
207 if (is_filtered(base_pos))
210 * We don't want to display this line.
217 if (status_col && is_hilited(base_pos, ch_tell()-1, 1, NULL))
221 if (squeeze && blankline)
224 * This line is blank.
225 * Skip down to the last contiguous blank line
226 * and pretend it is the one which we are returning.
228 while ((c = ch_forw_get()) == '\n' || c == '\r')
232 return (NULL_POSITION);
235 (void) ch_back_get();
243 * Get the previous line.
244 * A "current" position is passed and a "new" position is returned.
245 * The current position is the position of the first character of
246 * a line. The new position is the position of the first character
247 * of the PREVIOUS line. The line obtained is the one starting at new_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 && is_hilited(base_pos, ch_tell()-1, 1, NULL))
423 return (begin_new_pos);
435 if (pos != NULL_POSITION)
444 if (c != '\n' && c != '\r')
454 if (c == EOI || c == '\n' || c == '\r')