2 * Copyright (C) 1984-2007 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.
56 if (curr_pos == NULL_POSITION)
59 return (NULL_POSITION);
62 if (hilite_search == OPT_ONPLUS || 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,
73 if (ch_seek(curr_pos))
76 return (NULL_POSITION);
85 return (NULL_POSITION);
100 (void) ch_seek(base_pos);
101 while (base_pos < curr_pos)
106 return (NULL_POSITION);
109 backchars = pappend(c, base_pos);
114 base_pos -= backchars;
115 while (--backchars >= 0)
116 (void) ch_back_get();
126 return (NULL_POSITION);
128 blankline = (c == '\n' || c == '\r');
135 return (NULL_POSITION);
137 if (c == '\n' || c == EOI)
142 backchars = pflushmbc();
144 if (backchars > 0 && !chopline && hshift == 0)
146 new_pos -= backchars + 1;
156 * Append the char to the line and get the next char.
158 backchars = pappend(c, ch_tell()-1);
162 * The char won't fit in the line; the line
163 * is too long to print in the screen width.
166 if (chopline || hshift > 0)
171 } while (c != '\n' && c != EOI);
174 quit_if_one_screen = FALSE;
177 new_pos = ch_tell() - backchars;
186 if (squeeze && blankline)
189 * This line is blank.
190 * Skip down to the last contiguous blank line
191 * and pretend it is the one which we are returning.
193 while ((c = ch_forw_get()) == '\n' || c == '\r')
197 return (NULL_POSITION);
200 (void) ch_back_get();
208 * Get the previous line.
209 * A "current" position is passed and a "new" position is returned.
210 * The current position is the position of the first character of
211 * a line. The new position is the position of the first character
212 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
218 POSITION new_pos, begin_new_pos;
223 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
226 return (NULL_POSITION);
229 if (hilite_search == OPT_ONPLUS || status_col)
230 prep_hilite((curr_pos < 3*size_linebuf) ?
231 0 : curr_pos - 3*size_linebuf, curr_pos, -1);
233 if (ch_seek(curr_pos-1))
236 return (NULL_POSITION);
242 * Find out if the "current" line was blank.
244 (void) ch_forw_get(); /* Skip the newline */
245 c = ch_forw_get(); /* First char of "current" line */
246 (void) ch_back_get(); /* Restore our position */
247 (void) ch_back_get();
249 if (c == '\n' || c == '\r')
252 * The "current" line was blank.
253 * Skip over any preceding blank lines,
254 * since we skipped them in forw_line().
256 while ((c = ch_back_get()) == '\n' || c == '\r')
260 return (NULL_POSITION);
265 return (NULL_POSITION);
267 (void) ch_forw_get();
272 * Scan backwards until we hit the beginning of the line.
279 return (NULL_POSITION);
285 * This is the newline ending the previous line.
286 * We have hit the beginning of the line.
288 new_pos = ch_tell() + 1;
294 * We have hit the beginning of the file.
295 * This must be the first line in the file.
296 * This must, of course, be the beginning of the line.
304 * Now scan forwards from the beginning of this line.
305 * We keep discarding "printable lines" (based on screen width)
306 * until we reach the curr_pos.
308 * {{ This algorithm is pretty inefficient if the lines
309 * are much longer than the screen width,
310 * but I don't know of any better way. }}
312 if (ch_seek(new_pos))
315 return (NULL_POSITION);
321 begin_new_pos = new_pos;
322 (void) ch_seek(new_pos);
327 if (c == EOI || ABORT_SIGS())
330 return (NULL_POSITION);
335 backchars = pflushmbc();
336 if (backchars > 0 && !chopline && hshift == 0)
344 backchars = pappend(c, ch_tell()-1);
348 * Got a full printable line, but we haven't
349 * reached our curr_pos yet. Discard the line
350 * and start a new one.
352 if (chopline || hshift > 0)
355 quit_if_one_screen = FALSE;
360 while (backchars-- > 0)
362 (void) ch_back_get();
367 } while (new_pos < curr_pos);
371 return (begin_new_pos);
383 if (pos != NULL_POSITION)
392 if (c != '\n' && c != '\r')
402 if (c == EOI || c == '\n' || c == '\r')