2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, 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
;
28 extern volatile sig_atomic_t sigs
;
29 extern int ignore_eoi
;
30 extern int status_col
;
31 extern off_t start_attnpos
;
32 extern off_t end_attnpos
;
33 extern int hilite_search
;
34 extern int size_linebuf
;
38 * A "current" position is passed and a "new" position is returned.
39 * The current position is the position of the first character of
40 * a line. The new position is the position of the first character
41 * of the NEXT line. The line obtained is the line starting at curr_pos.
44 forw_line(off_t curr_pos
)
58 if (hilite_search
== OPT_ONPLUS
|| is_filtering() || status_col
)
60 * If we are ignoring EOI (command F), only prepare
61 * one line ahead, to avoid getting stuck waiting for
62 * slow data without displaying the data we already have.
63 * If we're not ignoring EOI, we *could* do the same, but
64 * for efficiency we prepare several lines ahead at once.
66 prep_hilite(curr_pos
, curr_pos
+ 3*size_linebuf
,
68 if (ch_seek(curr_pos
)) {
74 * Step back to the beginning of the line.
93 * Read forward again to the position we should start at.
97 (void) ch_seek(base_pos
);
99 while (new_pos
< curr_pos
) {
105 backchars
= pappend(c
, new_pos
);
109 new_pos
-= backchars
;
110 while (--backchars
>= 0)
111 (void) ch_back_get();
118 * Read the first character to display.
125 blankline
= (c
== '\n' || c
== '\r');
128 * Read each character in the line and append to the line buffer.
135 if (c
== '\n' || c
== EOI
) {
139 backchars
= pflushmbc();
141 if (backchars
> 0 && !chopline
&& hshift
== 0) {
142 new_pos
-= backchars
+ 1;
152 * Append the char to the line and get the next char.
154 backchars
= pappend(c
, ch_tell()-1);
157 * The char won't fit in the line; the line
158 * is too long to print in the screen width.
161 if (chopline
|| hshift
> 0) {
168 } while (c
!= '\n' && c
!= EOI
);
171 quit_if_one_screen
= FALSE
;
173 new_pos
= ch_tell() - backchars
;
183 if (is_filtered(base_pos
)) {
185 * We don't want to display this line.
192 if (status_col
&& is_hilited(base_pos
, ch_tell()-1, 1, NULL
))
195 if (squeeze
&& blankline
) {
197 * This line is blank.
198 * Skip down to the last contiguous blank line
199 * and pretend it is the one which we are returning.
201 while ((c
= ch_forw_get()) == '\n' || c
== '\r')
207 (void) ch_back_get();
215 * Get the previous line.
216 * A "current" position is passed and a "new" position is returned.
217 * The current position is the position of the first character of
218 * a line. The new position is the position of the first character
219 * of the PREVIOUS line. The line obtained is the one starting at new_pos.
222 back_line(off_t curr_pos
)
224 off_t new_pos
, begin_new_pos
, base_pos
;
230 if (curr_pos
== -1 || curr_pos
<= ch_zero()) {
234 if (hilite_search
== OPT_ONPLUS
|| is_filtering() || status_col
)
235 prep_hilite((curr_pos
< 3*size_linebuf
) ?
236 0 : curr_pos
- 3*size_linebuf
, curr_pos
, -1);
237 if (ch_seek(curr_pos
-1)) {
244 * Find out if the "current" line was blank.
246 (void) ch_forw_get(); /* Skip the newline */
247 c
= ch_forw_get(); /* First char of "current" line */
248 (void) ch_back_get(); /* Restore our position */
249 (void) ch_back_get();
251 if (c
== '\n' || c
== '\r') {
253 * The "current" line was blank.
254 * Skip over any preceding blank lines,
255 * since we skipped them in forw_line().
257 while ((c
= ch_back_get()) == '\n' || c
== '\r')
266 (void) ch_forw_get();
271 * Scan backwards until we hit the beginning of the line.
281 * This is the newline ending the previous line.
282 * We have hit the beginning of the line.
284 base_pos
= ch_tell() + 1;
289 * We have hit the beginning of the file.
290 * This must be the first line in the file.
291 * This must, of course, be the beginning of the line.
293 base_pos
= ch_tell();
299 * Now scan forwards from the beginning of this line.
300 * We keep discarding "printable lines" (based on screen width)
301 * until we reach the curr_pos.
303 * {{ This algorithm is pretty inefficient if the lines
304 * are much longer than the screen width,
305 * but I don't know of any better way. }}
308 if (ch_seek(new_pos
)) {
316 begin_new_pos
= new_pos
;
317 (void) ch_seek(new_pos
);
321 if (c
== EOI
|| ABORT_SIGS()) {
327 backchars
= pflushmbc();
328 if (backchars
> 0 && !chopline
&& hshift
== 0) {
335 backchars
= pappend(c
, ch_tell()-1);
338 * Got a full printable line, but we haven't
339 * reached our curr_pos yet. Discard the line
340 * and start a new one.
342 if (chopline
|| hshift
> 0) {
344 quit_if_one_screen
= FALSE
;
349 while (backchars
-- > 0) {
350 (void) ch_back_get();
355 } while (new_pos
< curr_pos
);
359 if (is_filtered(base_pos
)) {
361 * We don't want to display this line.
362 * Get the previous line.
364 curr_pos
= begin_new_pos
;
368 if (status_col
&& curr_pos
> 0 &&
369 is_hilited(base_pos
, curr_pos
-1, 1, NULL
))
372 return (begin_new_pos
);
379 set_attnpos(off_t pos
)
390 if (c
!= '\n' && c
!= '\r')
399 if (c
== EOI
|| c
== '\n' || c
== '\r')