-
Notifications
You must be signed in to change notification settings - Fork 89
/
input.c
605 lines (575 loc) · 12.1 KB
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
* Copyright (C) 1984-2024 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* High level routines dealing with getting lines of input
* from the file being viewed.
*
* When we speak of "lines" here, we mean PRINTABLE lines;
* lines processed with respect to the screen width.
* We use the term "raw line" to refer to lines simply
* delimited by newlines; not processed with respect to screen width.
*/
#include "less.h"
extern int squeeze;
extern int hshift;
extern int quit_if_one_screen;
extern int ignore_eoi;
extern int status_col;
extern int wordwrap;
extern POSITION start_attnpos;
extern POSITION end_attnpos;
#if HILITE_SEARCH
extern int hilite_search;
extern size_t size_linebuf;
extern int show_attn;
#endif
/*
* Set the status column.
* base Position of first char in line.
* disp First visible char.
* Different than base_pos if line is shifted.
* edisp Last visible char.
* eol End of line. Normally the newline.
* Different than edisp if line is chopped.
*/
static void init_status_col(POSITION base_pos, POSITION disp_pos, POSITION edisp_pos, POSITION eol_pos)
{
int hl_before = (chop_line() && disp_pos != NULL_POSITION) ?
is_hilited_attr(base_pos, disp_pos, TRUE, NULL) : 0;
int hl_after = (chop_line() && edisp_pos != NULL_POSITION) ?
is_hilited_attr(edisp_pos, eol_pos, TRUE, NULL) : 0;
int attr;
char ch;
if (hl_before && hl_after)
{
attr = hl_after;
ch = '=';
} else if (hl_before)
{
attr = hl_before;
ch = '<';
} else if (hl_after)
{
attr = hl_after;
ch = '>';
} else if (disp_pos != NULL_POSITION)
{
attr = is_hilited_attr(disp_pos, edisp_pos, TRUE, NULL);
ch = '*';
} else
{
attr = 0;
}
if (attr)
set_status_col(ch, attr);
}
/*
* Get the next line.
* A "current" position is passed and a "new" position is returned.
* The current position is the position of the first character of
* a line. The new position is the position of the first character
* of the NEXT line. The line obtained is the line starting at curr_pos.
*/
public POSITION forw_line_seg(POSITION curr_pos, lbool skipeol, lbool rscroll, lbool nochop)
{
POSITION base_pos;
POSITION new_pos;
POSITION edisp_pos;
int c;
lbool blankline;
lbool endline;
lbool chopped;
int backchars;
POSITION wrap_pos;
lbool skipped_leading;
get_forw_line:
if (curr_pos == NULL_POSITION)
{
null_line();
return (NULL_POSITION);
}
#if HILITE_SEARCH
if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
{
/*
* If we are ignoring EOI (command F), only prepare
* one line ahead, to avoid getting stuck waiting for
* slow data without displaying the data we already have.
* If we're not ignoring EOI, we *could* do the same, but
* for efficiency we prepare several lines ahead at once.
*/
prep_hilite(curr_pos, curr_pos + (POSITION) (3*size_linebuf), ignore_eoi ? 1 : -1);
curr_pos = next_unfiltered(curr_pos);
}
#endif
if (ch_seek(curr_pos))
{
null_line();
return (NULL_POSITION);
}
/*
* Step back to the beginning of the line.
*/
base_pos = curr_pos;
for (;;)
{
c = ch_back_get();
if (c == EOI)
break;
if (c == '\n')
{
(void) ch_forw_get();
break;
}
--base_pos;
}
/*
* Read forward again to the position we should start at.
*/
prewind();
plinestart(base_pos);
(void) ch_seek(base_pos);
new_pos = base_pos;
while (new_pos < curr_pos)
{
c = ch_forw_get();
if (c == EOI)
{
null_line();
return (NULL_POSITION);
}
backchars = pappend((char) c, new_pos);
new_pos++;
if (backchars > 0)
{
pshift_all();
if (wordwrap && (c == ' ' || c == '\t'))
{
do
{
new_pos++;
c = ch_forw_get(); /* {{ what if c == EOI? }} */
} while (c == ' ' || c == '\t');
backchars = 1;
}
new_pos -= backchars;
while (--backchars >= 0)
(void) ch_back_get();
}
}
(void) pflushmbc();
pshift_all();
/*
* Read the first character to display.
*/
c = ch_forw_get();
if (c == EOI)
{
null_line();
return (NULL_POSITION);
}
blankline = (c == '\n' || c == '\r');
wrap_pos = NULL_POSITION;
skipped_leading = FALSE;
/*
* Read each character in the line and append to the line buffer.
*/
chopped = FALSE;
for (;;)
{
if (c == '\n' || c == EOI)
{
/*
* End of the line.
*/
backchars = pflushmbc();
new_pos = ch_tell();
if (backchars > 0 && (nochop || !chop_line()) && hshift == 0)
{
new_pos -= backchars + 1;
endline = FALSE;
} else
endline = TRUE;
edisp_pos = new_pos;
break;
}
if (c != '\r')
blankline = FALSE;
/*
* Append the char to the line and get the next char.
*/
backchars = pappend((char) c, ch_tell()-1);
if (backchars > 0)
{
/*
* The char won't fit in the line; the line
* is too long to print in the screen width.
* End the line here.
*/
if (skipeol)
{
/* Read to end of line. */
edisp_pos = ch_tell() - backchars;
do
{
c = ch_forw_get();
} while (c != '\n' && c != EOI);
new_pos = ch_tell();
endline = TRUE;
quit_if_one_screen = FALSE;
chopped = TRUE;
} else
{
if (!wordwrap)
new_pos = ch_tell() - backchars;
else
{
/*
* We're word-wrapping, so go back to the last space.
* However, if it's the space itself that couldn't fit,
* simply ignore it and any subsequent spaces.
*/
if (c == ' ' || c == '\t')
{
do
{
new_pos = ch_tell();
c = ch_forw_get(); /* {{ what if c == EOI? }} */
} while (c == ' ' || c == '\t');
if (c == '\r')
c = ch_forw_get(); /* {{ what if c == EOI? }} */
if (c == '\n')
new_pos = ch_tell();
} else if (wrap_pos == NULL_POSITION)
new_pos = ch_tell() - backchars;
else
{
new_pos = wrap_pos;
loadc();
}
}
endline = FALSE;
edisp_pos = new_pos;
}
break;
}
if (wordwrap)
{
if (c == ' ' || c == '\t')
{
if (skipped_leading)
{
wrap_pos = ch_tell();
savec();
}
} else
skipped_leading = TRUE;
}
c = ch_forw_get();
}
#if HILITE_SEARCH
if (blankline && show_attn)
{
/* Add spurious space to carry possible attn hilite.
* Use pappend_b so that if line ended with \r\n,
* we insert the space before the \r. */
pappend_b(' ', ch_tell()-1, TRUE);
}
#endif
pdone(endline, rscroll && chopped, TRUE);
#if HILITE_SEARCH
if (is_filtered(base_pos))
{
/*
* We don't want to display this line.
* Get the next line.
*/
curr_pos = new_pos;
goto get_forw_line;
}
if (status_col)
init_status_col(base_pos, line_position(), edisp_pos, new_pos);
#endif
if (squeeze && blankline)
{
/*
* This line is blank.
* Skip down to the last contiguous blank line
* and pretend it is the one which we are returning.
*/
while ((c = ch_forw_get()) == '\n' || c == '\r')
continue;
if (c != EOI)
(void) ch_back_get();
new_pos = ch_tell();
}
return (new_pos);
}
public POSITION forw_line(POSITION curr_pos)
{
return forw_line_seg(curr_pos, (chop_line() || hshift > 0), TRUE, FALSE);
}
/*
* Get the previous line.
* A "current" position is passed and a "new" position is returned.
* The current position is the position of the first character of
* a line. The new position is the position of the first character
* of the PREVIOUS line. The line obtained is the one starting at new_pos.
*/
public POSITION back_line(POSITION curr_pos)
{
POSITION base_pos;
POSITION new_pos;
POSITION edisp_pos;
POSITION begin_new_pos;
int c;
lbool endline;
lbool chopped;
int backchars;
POSITION wrap_pos;
lbool skipped_leading;
get_back_line:
if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
{
null_line();
return (NULL_POSITION);
}
#if HILITE_SEARCH
if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
prep_hilite((curr_pos < (POSITION) (3*size_linebuf)) ? 0 :
curr_pos - (POSITION) (3*size_linebuf), curr_pos, -1);
#endif
if (ch_seek(curr_pos-1))
{
null_line();
return (NULL_POSITION);
}
if (squeeze)
{
/*
* Find out if the "current" line was blank.
*/
(void) ch_forw_get(); /* Skip the newline */
c = ch_forw_get(); /* First char of "current" line */
/* {{ what if c == EOI? }} */
(void) ch_back_get(); /* Restore our position */
(void) ch_back_get();
if (c == '\n' || c == '\r')
{
/*
* The "current" line was blank.
* Skip over any preceding blank lines,
* since we skipped them in forw_line().
*/
while ((c = ch_back_get()) == '\n' || c == '\r')
continue;
if (c == EOI)
{
null_line();
return (NULL_POSITION);
}
(void) ch_forw_get();
}
}
/*
* Scan backwards until we hit the beginning of the line.
*/
for (;;)
{
c = ch_back_get();
if (c == '\n')
{
/*
* This is the newline ending the previous line.
* We have hit the beginning of the line.
*/
base_pos = ch_tell() + 1;
break;
}
if (c == EOI)
{
/*
* We have hit the beginning of the file.
* This must be the first line in the file.
* This must, of course, be the beginning of the line.
*/
base_pos = ch_tell();
break;
}
}
/*
* Now scan forwards from the beginning of this line.
* We keep discarding "printable lines" (based on screen width)
* until we reach the curr_pos.
*
* {{ This algorithm is pretty inefficient if the lines
* are much longer than the screen width,
* but I don't know of any better way. }}
*/
new_pos = base_pos;
if (ch_seek(new_pos))
{
null_line();
return (NULL_POSITION);
}
endline = FALSE;
prewind();
plinestart(new_pos);
loop:
wrap_pos = NULL_POSITION;
skipped_leading = FALSE;
begin_new_pos = new_pos;
(void) ch_seek(new_pos);
chopped = FALSE;
for (;;)
{
c = ch_forw_get();
if (c == EOI)
{
null_line();
return (NULL_POSITION);
}
new_pos++;
if (c == '\n')
{
backchars = pflushmbc();
if (backchars > 0 && !chop_line() && hshift == 0)
{
backchars++;
goto shift;
}
endline = TRUE;
edisp_pos = new_pos;
break;
}
backchars = pappend((char) c, ch_tell()-1);
if (backchars > 0)
{
/*
* Got a full printable line, but we haven't
* reached our curr_pos yet. Discard the line
* and start a new one.
*/
if (chop_line() || hshift > 0)
{
endline = TRUE;
chopped = TRUE;
quit_if_one_screen = FALSE;
edisp_pos = new_pos;
break;
}
shift:
if (!wordwrap)
{
pshift_all();
new_pos -= backchars;
} else
{
if (c == ' ' || c == '\t')
{
for (;;)
{
c = ch_forw_get(); /* {{ what if c == EOI? }} */
if (c == ' ' || c == '\t')
new_pos++;
else
{
if (c == '\r')
{
c = ch_forw_get(); /* {{ what if c == EOI? }} */
if (c == '\n')
new_pos++;
}
if (c == '\n')
new_pos++;
edisp_pos = new_pos;
break;
}
}
if (new_pos >= curr_pos)
{
edisp_pos = new_pos;
break;
}
pshift_all();
} else
{
pshift_all();
if (wrap_pos == NULL_POSITION)
new_pos -= backchars;
else
new_pos = wrap_pos;
}
}
goto loop;
}
if (wordwrap)
{
if (c == ' ' || c == '\t')
{
if (skipped_leading)
wrap_pos = new_pos;
} else
skipped_leading = TRUE;
}
if (new_pos >= curr_pos)
{
edisp_pos = new_pos;
break;
}
}
pdone(endline, chopped, FALSE);
#if HILITE_SEARCH
if (is_filtered(base_pos))
{
/*
* We don't want to display this line.
* Get the previous line.
*/
curr_pos = begin_new_pos;
goto get_back_line;
}
if (status_col)
init_status_col(base_pos, line_position(), edisp_pos, new_pos);
#endif
return (begin_new_pos);
}
/*
* Set attnpos.
*/
public void set_attnpos(POSITION pos)
{
int c;
if (pos != NULL_POSITION)
{
if (ch_seek(pos))
return;
for (;;)
{
c = ch_forw_get();
if (c == EOI)
break;
if (c == '\n' || c == '\r')
{
(void) ch_back_get();
break;
}
pos++;
}
end_attnpos = pos;
for (;;)
{
c = ch_back_get();
if (c == EOI || c == '\n' || c == '\r')
break;
pos--;
}
}
start_attnpos = pos;
}