2 * Copyright (C) 1984-2002 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 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.
53 if (curr_pos == NULL_POSITION)
56 return (NULL_POSITION);
59 if (hilite_search == OPT_ONPLUS)
61 * If we are ignoring EOI (command F), only prepare
62 * one line ahead, to avoid getting stuck waiting for
63 * slow data without displaying the data we already have.
64 * If we're not ignoring EOI, we *could* do the same, but
65 * for efficiency we prepare several lines ahead at once.
67 prep_hilite(curr_pos, curr_pos + 3*size_linebuf,
70 if (ch_seek(curr_pos))
73 return (NULL_POSITION);
78 (void) ch_seek(curr_pos);
84 return (NULL_POSITION);
86 blankline = (c == '\n' || c == '\r');
93 return (NULL_POSITION);
95 if (c == '\n' || c == EOI)
106 * Append the char to the line and get the next char.
108 if (pappend(c, ch_tell()-1))
111 * The char won't fit in the line; the line
112 * is too long to print in the screen width.
115 if (chopline || hshift > 0)
120 } while (c != '\n' && c != EOI);
123 quit_if_one_screen = FALSE;
126 new_pos = ch_tell() - 1;
135 if (squeeze && blankline)
138 * This line is blank.
139 * Skip down to the last contiguous blank line
140 * and pretend it is the one which we are returning.
142 while ((c = ch_forw_get()) == '\n' || c == '\r')
146 return (NULL_POSITION);
149 (void) ch_back_get();
157 * Get the previous line.
158 * A "current" position is passed and a "new" position is returned.
159 * The current position is the position of the first character of
160 * a line. The new position is the position of the first character
161 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
167 POSITION new_pos, begin_new_pos;
171 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
174 return (NULL_POSITION);
177 if (hilite_search == OPT_ONPLUS)
178 prep_hilite((curr_pos < 3*size_linebuf) ?
179 0 : curr_pos - 3*size_linebuf, curr_pos, -1);
181 if (ch_seek(curr_pos-1))
184 return (NULL_POSITION);
190 * Find out if the "current" line was blank.
192 (void) ch_forw_get(); /* Skip the newline */
193 c = ch_forw_get(); /* First char of "current" line */
194 (void) ch_back_get(); /* Restore our position */
195 (void) ch_back_get();
197 if (c == '\n' || c == '\r')
200 * The "current" line was blank.
201 * Skip over any preceding blank lines,
202 * since we skipped them in forw_line().
204 while ((c = ch_back_get()) == '\n' || c == '\r')
208 return (NULL_POSITION);
213 return (NULL_POSITION);
215 (void) ch_forw_get();
220 * Scan backwards until we hit the beginning of the line.
227 return (NULL_POSITION);
233 * This is the newline ending the previous line.
234 * We have hit the beginning of the line.
236 new_pos = ch_tell() + 1;
242 * We have hit the beginning of the file.
243 * This must be the first line in the file.
244 * This must, of course, be the beginning of the line.
252 * Now scan forwards from the beginning of this line.
253 * We keep discarding "printable lines" (based on screen width)
254 * until we reach the curr_pos.
256 * {{ This algorithm is pretty inefficient if the lines
257 * are much longer than the screen width,
258 * but I don't know of any better way. }}
260 if (ch_seek(new_pos))
263 return (NULL_POSITION);
267 begin_new_pos = new_pos;
270 (void) ch_seek(new_pos);
275 if (c == EOI || ABORT_SIGS())
278 return (NULL_POSITION);
286 if (pappend(c, ch_tell()-1))
289 * Got a full printable line, but we haven't
290 * reached our curr_pos yet. Discard the line
291 * and start a new one.
293 if (chopline || hshift > 0)
296 quit_if_one_screen = FALSE;
300 (void) ch_back_get();
304 } while (new_pos < curr_pos);
308 return (begin_new_pos);
320 if (pos != NULL_POSITION)
329 if (c != '\n' && c != '\r')
339 if (c == EOI || c == '\n' || c == '\r')