2 * High level routines dealing with getting lines of input
3 * from the file being viewed.
5 * When we speak of "lines" here, we mean PRINTABLE lines;
6 * lines processed with respect to the screen width.
7 * We use the term "raw line" to refer to lines simply
8 * delimited by newlines; not processed with respect to screen width.
19 * A "current" position is passed and a "new" position is returned.
20 * The current position is the position of the first character of
21 * a line. The new position is the position of the first character
22 * of the NEXT line. The line obtained is the line starting at curr_pos.
33 if (curr_pos == NULL_POSITION || ch_seek(curr_pos))
36 return (NULL_POSITION);
41 (void) ch_seek(curr_pos);
47 return (NULL_POSITION);
49 blankline = (c == '\n' || c == '\r');
56 return (NULL_POSITION);
58 if (c == '\n' || c == EOI)
69 * Append the char to the line and get the next char.
74 * The char won't fit in the line; the line
75 * is too long to print in the screen width.
83 } while (c != '\n' && c != EOI);
88 new_pos = ch_tell() - 1;
97 if (squeeze && blankline)
100 * This line is blank.
101 * Skip down to the last contiguous blank line
102 * and pretend it is the one which we are returning.
104 while ((c = ch_forw_get()) == '\n' || c == '\r')
108 return (NULL_POSITION);
111 (void) ch_back_get();
119 * Get the previous line.
120 * A "current" position is passed and a "new" position is returned.
121 * The current position is the position of the first character of
122 * a line. The new position is the position of the first character
123 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
129 POSITION new_pos, begin_new_pos;
133 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero() ||
137 return (NULL_POSITION);
143 * Find out if the "current" line was blank.
145 (void) ch_forw_get(); /* Skip the newline */
146 c = ch_forw_get(); /* First char of "current" line */
147 (void) ch_back_get(); /* Restore our position */
148 (void) ch_back_get();
153 * The "current" line was blank.
154 * Skip over any preceding blank lines,
155 * since we skipped them in forw_line().
157 while ((c = ch_back_get()) == '\n' || c == '\r')
161 return (NULL_POSITION);
166 return (NULL_POSITION);
168 (void) ch_forw_get();
173 * Scan backwards until we hit the beginning of the line.
180 return (NULL_POSITION);
186 * This is the newline ending the previous line.
187 * We have hit the beginning of the line.
189 new_pos = ch_tell() + 1;
195 * We have hit the beginning of the file.
196 * This must be the first line in the file.
197 * This must, of course, be the beginning of the line.
205 * Now scan forwards from the beginning of this line.
206 * We keep discarding "printable lines" (based on screen width)
207 * until we reach the curr_pos.
209 * {{ This algorithm is pretty inefficient if the lines
210 * are much longer than the screen width,
211 * but I don't know of any better way. }}
213 if (ch_seek(new_pos))
216 return (NULL_POSITION);
220 begin_new_pos = new_pos;
223 (void) ch_seek(new_pos);
228 if (c == EOI || sigs)
231 return (NULL_POSITION);
242 * Got a full printable line, but we haven't
243 * reached our curr_pos yet. Discard the line
244 * and start a new one.
252 (void) ch_back_get();
256 } while (new_pos < curr_pos);
260 return (begin_new_pos);