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 * Code to handle displaying line numbers.
15 * Finding the line number of a given file position is rather tricky.
16 * We don't want to just start at the beginning of the file and
17 * count newlines, because that is slow for large files (and also
18 * wouldn't work if we couldn't get to the start of the file; e.g.
19 * if input is a long pipe).
21 * So we use the function add_lnum to cache line numbers.
22 * We try to be very clever and keep only the more interesting
23 * line numbers when we run out of space in our table. A line
24 * number is more interesting than another when it is far from
25 * other line numbers. For example, we'd rather keep lines
26 * 100,200,300 than 100,101,300. 200 is more interesting than
27 * 101 because 101 can be derived very cheaply from 100, while
28 * 200 is more expensive to derive from 100.
30 * The function currline() returns the line number of a given
31 * position in the file. As a side effect, it calls add_lnum
32 * to cache the line number. Therefore currline is occasionally
33 * called to make sure we cache line numbers often enough.
43 * Structure to keep track of a line number and the associated file position.
44 * A doubly-linked circular list of line numbers is kept ordered by line number.
47 struct linenum_info
*next
; /* Link to next in the list */
48 struct linenum_info
*prev
; /* Line to previous in the list */
49 off_t pos
; /* File position */
50 off_t gap
; /* Gap between prev and next */
51 off_t line
; /* Line number */
54 * "gap" needs some explanation: the gap of any particular line number
55 * is the distance between the previous one and the next one in the list.
56 * ("Distance" means difference in file position.) In other words, the
57 * gap of a line number is the gap which would be introduced if this
58 * line number were deleted. It is used to decide which one to replace
59 * when we have a new one to insert and the table is full.
62 #define NPOOL 200 /* Size of line number pool */
64 #define LONGTIME (2) /* In seconds */
66 static struct linenum_info anchor
; /* Anchor of the list */
67 static struct linenum_info
*freelist
; /* Anchor of the unused entries */
68 static struct linenum_info pool
[NPOOL
]; /* The pool itself */
69 static struct linenum_info
*spare
; /* We always keep one spare entry */
72 extern volatile sig_atomic_t sigs
;
74 extern int screen_trashed
;
77 * Initialize the line number structures.
82 struct linenum_info
*p
;
85 * Put all the entries on the free list.
86 * Leave one for the "spare".
88 for (p
= pool
; p
< &pool
[NPOOL
-2]; p
++)
90 pool
[NPOOL
-2].next
= NULL
;
93 spare
= &pool
[NPOOL
-1];
96 * Initialize the anchor.
98 anchor
.next
= anchor
.prev
= &anchor
;
105 * Calculate the gap for an entry.
108 calcgap(struct linenum_info
*p
)
111 * Don't bother to compute a gap for the anchor.
112 * Also don't compute a gap for the last one in the list.
113 * The gap for that last one should be considered infinite,
114 * but we never look at it anyway.
116 if (p
== &anchor
|| p
->next
== &anchor
)
118 p
->gap
= p
->next
->pos
- p
->prev
->pos
;
122 * Add a new line number to the cache.
123 * The specified position (pos) should be the file position of the
124 * FIRST character in the specified line.
127 add_lnum(off_t linenum
, off_t pos
)
129 struct linenum_info
*p
;
130 struct linenum_info
*new;
131 struct linenum_info
*nextp
;
132 struct linenum_info
*prevp
;
136 * Find the proper place in the list for the new one.
137 * The entries are sorted by position.
139 for (p
= anchor
.next
; p
!= &anchor
&& p
->pos
< pos
; p
= p
->next
)
140 if (p
->line
== linenum
)
141 /* We already have this one. */
146 if (freelist
!= NULL
) {
148 * We still have free (unused) entries.
152 freelist
= freelist
->next
;
156 * Use the "spare" entry.
163 * Fill in the fields of the new entry,
164 * and insert it into the proper place in the list.
175 * Recalculate gaps for the new entry and the neighboring entries.
183 * We have used the spare entry.
184 * Scan the list to find the one with the smallest
185 * gap, take it out and make it the spare.
186 * We should never remove the last one, so stop when
187 * we get to p->next == &anchor. This also avoids
188 * looking at the gap of the last one, which is
189 * not computed by calcgap.
191 mingap
= anchor
.next
->gap
;
192 for (p
= anchor
.next
; p
->next
!= &anchor
; p
= p
->next
) {
193 if (p
->gap
<= mingap
) {
198 spare
->next
->prev
= spare
->prev
;
199 spare
->prev
->next
= spare
->next
;
203 static int loopcount
;
204 static struct timespec timeout
;
207 timeout_set(int seconds
)
209 clock_gettime(CLOCK_MONOTONIC
, &timeout
);
210 timeout
.tv_sec
+= seconds
;
214 timeout_elapsed(void)
218 clock_gettime(CLOCK_MONOTONIC
, &now
);
219 return timespeccmp(&now
, &timeout
, >=);
225 if (loopcount
>= 0 && ++loopcount
> 100) {
227 if (timeout_elapsed()) {
228 ierror("Calculating line numbers", NULL
);
235 * Turn off line numbers because the user has interrupted
236 * a lengthy line number calculation.
241 if (linenums
== OPT_ONPLUS
)
243 * We were displaying line numbers, so need to repaint.
247 error("Line numbers turned off", NULL
);
251 * Find the line number associated with a given position.
252 * Return 0 if we can't figure it out.
255 find_linenum(off_t pos
)
257 struct linenum_info
*p
;
263 * We're not using line numbers.
268 * Caller doesn't know what he's talking about.
271 if (pos
<= ch_zero())
273 * Beginning of file is always line number 1.
278 * Find the entry nearest to the position we want.
280 for (p
= anchor
.next
; p
!= &anchor
&& p
->pos
< pos
; p
= p
->next
)
283 /* Found it exactly. */
287 * This is the (possibly) time-consuming part.
288 * We start at the line we just found and start
289 * reading the file forward or backward till we
290 * get to the place we want.
292 * First decide whether we should go forward from the
293 * previous one or backwards from the next one.
294 * The decision is based on which way involves
295 * traversing fewer bytes in the file.
297 timeout_set(LONGTIME
);
298 if (p
== &anchor
|| pos
- p
->prev
->pos
< p
->pos
- pos
) {
306 for (linenum
= p
->line
, cpos
= p
->pos
; cpos
< pos
; linenum
++) {
308 * Allow a signal to abort this loop.
310 cpos
= forw_raw_line(cpos
, NULL
, NULL
);
320 * We might as well cache it.
322 add_lnum(linenum
, cpos
);
324 * If the given position is not at the start of a line,
325 * make sure we return the correct line number.
336 for (linenum
= p
->line
, cpos
= p
->pos
; cpos
> pos
; linenum
--) {
338 * Allow a signal to abort this loop.
340 cpos
= back_raw_line(cpos
, NULL
, NULL
);
350 * We might as well cache it.
352 add_lnum(linenum
, cpos
);
359 * Find the position of a given line number.
360 * Return -1 if we can't figure it out.
363 find_pos(off_t linenum
)
365 struct linenum_info
*p
;
371 * Line number 1 is beginning of file.
376 * Find the entry nearest to the line number we want.
378 for (p
= anchor
.next
; p
!= &anchor
&& p
->line
< linenum
; p
= p
->next
)
380 if (p
->line
== linenum
)
381 /* Found it exactly. */
384 if (p
== &anchor
|| linenum
- p
->prev
->line
< p
->line
- linenum
) {
391 for (clinenum
= p
->line
, cpos
= p
->pos
;
395 * Allow a signal to abort this loop.
397 cpos
= forw_raw_line(cpos
, NULL
, NULL
);
409 for (clinenum
= p
->line
, cpos
= p
->pos
;
413 * Allow a signal to abort this loop.
415 cpos
= back_raw_line(cpos
, (char **)NULL
, (int *)NULL
);
423 * We might as well cache it.
425 add_lnum(clinenum
, cpos
);
430 * Return the line number of the "current" line.
431 * The argument "where" tells which line is to be considered
432 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
441 pos
= position(where
);
443 while (pos
== -1 && where
>= 0 && where
< sc_height
)
444 pos
= position(++where
);
447 linenum
= find_linenum(pos
);