Sync mly(4) with FreeBSD.
[dragonfly.git] / contrib / less-4 / input.c
blob1129a7b05539ca2bcfb5f525b528f44ffc867176
1 /*
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.
9 */
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.
22 #include "less.h"
24 extern int squeeze;
25 extern int chopline;
26 extern int hshift;
27 extern int quit_if_one_screen;
28 extern int sigs;
29 extern int ignore_eoi;
30 extern int status_col;
31 extern POSITION start_attnpos;
32 extern POSITION end_attnpos;
33 #if HILITE_SEARCH
34 extern int hilite_search;
35 extern int size_linebuf;
36 #endif
39 * Get the next line.
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.
45 public POSITION
46 forw_line(curr_pos)
47 POSITION curr_pos;
49 POSITION base_pos;
50 POSITION new_pos;
51 register int c;
52 int blankline;
53 int endline;
54 int backchars;
56 if (curr_pos == NULL_POSITION)
58 null_line();
59 return (NULL_POSITION);
61 #if HILITE_SEARCH
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,
71 ignore_eoi ? 1 : -1);
72 #endif
73 if (ch_seek(curr_pos))
75 null_line();
76 return (NULL_POSITION);
79 base_pos = curr_pos;
80 for (;;)
82 if (ABORT_SIGS())
84 null_line();
85 return (NULL_POSITION);
87 c = ch_back_get();
88 if (c == EOI)
89 break;
90 if (c == '\n')
92 (void) ch_forw_get();
93 break;
95 --base_pos;
98 prewind();
99 plinenum(base_pos);
100 (void) ch_seek(base_pos);
101 while (base_pos < curr_pos)
103 if (ABORT_SIGS())
105 null_line();
106 return (NULL_POSITION);
108 c = ch_forw_get();
109 backchars = pappend(c, base_pos);
110 base_pos++;
111 if (backchars > 0)
113 pshift_all();
114 base_pos -= backchars;
115 while (--backchars >= 0)
116 (void) ch_back_get();
119 (void) pflushmbc();
120 pshift_all();
122 c = ch_forw_get();
123 if (c == EOI)
125 null_line();
126 return (NULL_POSITION);
128 blankline = (c == '\n' || c == '\r');
130 for (;;)
132 if (ABORT_SIGS())
134 null_line();
135 return (NULL_POSITION);
137 if (c == '\n' || c == EOI)
140 * End of the line.
142 backchars = pflushmbc();
143 new_pos = ch_tell();
144 if (backchars > 0 && !chopline && hshift == 0)
146 new_pos -= backchars + 1;
147 endline = FALSE;
148 } else
149 endline = TRUE;
150 break;
152 if (c != '\r')
153 blankline = 0;
156 * Append the char to the line and get the next char.
158 backchars = pappend(c, ch_tell()-1);
159 if (backchars > 0)
162 * The char won't fit in the line; the line
163 * is too long to print in the screen width.
164 * End the line here.
166 if (chopline || hshift > 0)
170 c = ch_forw_get();
171 } while (c != '\n' && c != EOI);
172 new_pos = ch_tell();
173 endline = TRUE;
174 quit_if_one_screen = FALSE;
175 } else
177 new_pos = ch_tell() - backchars;
178 endline = FALSE;
180 break;
182 c = ch_forw_get();
184 pdone(endline);
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')
194 if (ABORT_SIGS())
196 null_line();
197 return (NULL_POSITION);
199 if (c != EOI)
200 (void) ch_back_get();
201 new_pos = ch_tell();
204 return (new_pos);
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.
214 public POSITION
215 back_line(curr_pos)
216 POSITION curr_pos;
218 POSITION new_pos, begin_new_pos;
219 int c;
220 int endline;
221 int backchars;
223 if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
225 null_line();
226 return (NULL_POSITION);
228 #if HILITE_SEARCH
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);
232 #endif
233 if (ch_seek(curr_pos-1))
235 null_line();
236 return (NULL_POSITION);
239 if (squeeze)
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')
257 if (ABORT_SIGS())
259 null_line();
260 return (NULL_POSITION);
262 if (c == EOI)
264 null_line();
265 return (NULL_POSITION);
267 (void) ch_forw_get();
272 * Scan backwards until we hit the beginning of the line.
274 for (;;)
276 if (ABORT_SIGS())
278 null_line();
279 return (NULL_POSITION);
281 c = ch_back_get();
282 if (c == '\n')
285 * This is the newline ending the previous line.
286 * We have hit the beginning of the line.
288 new_pos = ch_tell() + 1;
289 break;
291 if (c == EOI)
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.
298 new_pos = ch_tell();
299 break;
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))
314 null_line();
315 return (NULL_POSITION);
317 endline = FALSE;
318 prewind();
319 plinenum(new_pos);
320 loop:
321 begin_new_pos = new_pos;
322 (void) ch_seek(new_pos);
326 c = ch_forw_get();
327 if (c == EOI || ABORT_SIGS())
329 null_line();
330 return (NULL_POSITION);
332 new_pos++;
333 if (c == '\n')
335 backchars = pflushmbc();
336 if (backchars > 0 && !chopline && hshift == 0)
338 backchars++;
339 goto shift;
341 endline = TRUE;
342 break;
344 backchars = pappend(c, ch_tell()-1);
345 if (backchars > 0)
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)
354 endline = TRUE;
355 quit_if_one_screen = FALSE;
356 break;
358 shift:
359 pshift_all();
360 while (backchars-- > 0)
362 (void) ch_back_get();
363 new_pos--;
365 goto loop;
367 } while (new_pos < curr_pos);
369 pdone(endline);
371 return (begin_new_pos);
375 * Set attnpos.
377 public void
378 set_attnpos(pos)
379 POSITION pos;
381 int c;
383 if (pos != NULL_POSITION)
385 if (ch_seek(pos))
386 return;
387 for (;;)
389 c = ch_forw_get();
390 if (c == EOI)
391 return;
392 if (c != '\n' && c != '\r')
393 break;
394 pos++;
397 start_attnpos = pos;
398 for (;;)
400 c = ch_forw_get();
401 pos++;
402 if (c == EOI || c == '\n' || c == '\r')
403 break;
405 end_attnpos = pos;