]>
diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/miscellany/less-177/linenum.c
2 * Code to handle displaying line numbers.
4 * Finding the line number of a given file position is rather tricky.
5 * We don't want to just start at the beginning of the file and
6 * count newlines, because that is slow for large files (and also
7 * wouldn't work if we couldn't get to the start of the file; e.g.
8 * if input is a long pipe).
10 * So we use the function add_lnum to cache line numbers.
11 * We try to be very clever and keep only the more interesting
12 * line numbers when we run out of space in our table. A line
13 * number is more interesting than another when it is far from
14 * other line numbers. For example, we'd rather keep lines
15 * 100,200,300 than 100,101,300. 200 is more interesting than
16 * 101 because 101 can be derived very cheaply from 100, while
17 * 200 is more expensive to derive from 100.
19 * The function currline() returns the line number of a given
20 * position in the file. As a side effect, it calls add_lnum
21 * to cache the line number. Therefore currline is occasionally
22 * called to make sure we cache line numbers often enough.
29 * Structure to keep track of a line number and the associated file position.
30 * A doubly-linked circular list of line numbers is kept ordered by line number.
34 struct linenum
*next
; /* Link to next in the list */
35 struct linenum
*prev
; /* Line to previous in the list */
36 POSITION pos
; /* File position */
37 POSITION gap
; /* Gap between prev and next */
38 int line
; /* Line number */
41 * "gap" needs some explanation: the gap of any particular line number
42 * is the distance between the previous one and the next one in the list.
43 * ("Distance" means difference in file position.) In other words, the
44 * gap of a line number is the gap which would be introduced if this
45 * line number were deleted. It is used to decide which one to replace
46 * when we have a new one to insert and the table is full.
49 #define NPOOL 50 /* Size of line number pool */
51 #define LONGTIME (2) /* In seconds */
53 public int lnloop
= 0; /* Are we in the line num loop? */
55 static struct linenum anchor
; /* Anchor of the list */
56 static struct linenum
*freelist
; /* Anchor of the unused entries */
57 static struct linenum pool
[NPOOL
]; /* The pool itself */
58 static struct linenum
*spare
; /* We always keep one spare entry */
65 * Initialize the line number structures.
70 register struct linenum
*p
;
73 * Put all the entries on the free list.
74 * Leave one for the "spare".
76 for (p
= pool
; p
< &pool
[NPOOL
-2]; p
++)
78 pool
[NPOOL
-2].next
= NULL
;
81 spare
= &pool
[NPOOL
-1];
84 * Initialize the anchor.
86 anchor
.next
= anchor
.prev
= &anchor
;
88 anchor
.pos
= (POSITION
)0;
93 * Calculate the gap for an entry.
97 register struct linenum
*p
;
100 * Don't bother to compute a gap for the anchor.
101 * Also don't compute a gap for the last one in the list.
102 * The gap for that last one should be considered infinite,
103 * but we never look at it anyway.
105 if (p
== &anchor
|| p
->next
== &anchor
)
107 p
->gap
= p
->next
->pos
- p
->prev
->pos
;
111 * Add a new line number to the cache.
112 * The specified position (pos) should be the file position of the
113 * FIRST character in the specified line.
120 register struct linenum
*p
;
121 register struct linenum
*new;
122 register struct linenum
*nextp
;
123 register struct linenum
*prevp
;
124 register POSITION mingap
;
127 * Find the proper place in the list for the new one.
128 * The entries are sorted by position.
130 for (p
= anchor
.next
; p
!= &anchor
&& p
->pos
< pos
; p
= p
->next
)
132 /* We already have this one. */
137 if (freelist
!= NULL
)
140 * We still have free (unused) entries.
144 freelist
= freelist
->next
;
149 * Use the "spare" entry.
156 * Fill in the fields of the new entry,
157 * and insert it into the proper place in the list.
168 * Recalculate gaps for the new entry and the neighboring entries.
177 * We have used the spare entry.
178 * Scan the list to find the one with the smallest
179 * gap, take it out and make it the spare.
180 * We should never remove the last one, so stop when
181 * we get to p->next == &anchor. This also avoids
182 * looking at the gap of the last one, which is
183 * not computed by calcgap.
185 mingap
= anchor
.next
->gap
;
186 for (p
= anchor
.next
; p
->next
!= &anchor
; p
= p
->next
)
188 if (p
->gap
<= mingap
)
194 spare
->next
->prev
= spare
->prev
;
195 spare
->prev
->next
= spare
->next
;
200 * If we get stuck in a long loop trying to figure out the
201 * line number, print a message to tell the user what we're doing.
206 ierror("Calculating line numbers", NULL_PARG
);
208 * Set the lnloop flag here, so if the user interrupts while
209 * we are calculating line numbers, the signal handler will
210 * turn off line numbers (linenums=0).
215 static int loopcount
;
217 static long startime
;
224 if (loopcount
>= 0 && ++loopcount
> 100)
227 if (get_time() >= startime
+ LONGTIME
)
234 if (loopcount
>= 0 && ++loopcount
> LONGLOOP
)
243 * Find the line number associated with a given position.
244 * Return 0 if we can't figure it out.
250 register struct linenum
*p
;
256 * We're not using line numbers.
259 if (pos
== NULL_POSITION
)
261 * Caller doesn't know what he's talking about.
264 if (pos
<= ch_zero())
266 * Beginning of file is always line number 1.
271 * Find the entry nearest to the position we want.
273 for (p
= anchor
.next
; p
!= &anchor
&& p
->pos
< pos
; p
= p
->next
)
276 /* Found it exactly. */
280 * This is the (possibly) time-consuming part.
281 * We start at the line we just found and start
282 * reading the file forward or backward till we
283 * get to the place we want.
285 * First decide whether we should go forward from the
286 * previous one or backwards from the next one.
287 * The decision is based on which way involves
288 * traversing fewer bytes in the file.
292 startime
= get_time();
294 if (p
== &anchor
|| pos
- p
->prev
->pos
< p
->pos
- pos
)
303 for (lno
= p
->line
, cpos
= p
->pos
; cpos
< pos
; lno
++)
306 * Allow a signal to abort this loop.
308 cpos
= forw_raw_line(cpos
, (char **)NULL
);
309 if (sigs
|| cpos
== NULL_POSITION
)
315 * We might as well cache it.
319 * If the given position is not at the start of a line,
320 * make sure we return the correct line number.
332 for (lno
= p
->line
, cpos
= p
->pos
; cpos
> pos
; lno
--)
335 * Allow a signal to abort this loop.
337 cpos
= back_raw_line(cpos
, (char **)NULL
);
338 if (sigs
|| cpos
== NULL_POSITION
)
344 * We might as well cache it.
353 * Find the position of a given line number.
354 * Return NULL_POSITION if we can't figure it out.
360 register struct linenum
*p
;
366 * Line number 1 is beginning of file.
371 * Find the entry nearest to the line number we want.
373 for (p
= anchor
.next
; p
!= &anchor
&& p
->line
< lno
; p
= p
->next
)
376 /* Found it exactly. */
380 if (p
== &anchor
|| lno
- p
->prev
->line
< p
->line
- lno
)
387 return (NULL_POSITION
);
388 for (clno
= p
->line
, cpos
= p
->pos
; clno
< lno
; clno
++)
391 * Allow a signal to abort this loop.
393 cpos
= forw_raw_line(cpos
, (char **)NULL
);
394 if (sigs
|| cpos
== NULL_POSITION
)
395 return (NULL_POSITION
);
403 return (NULL_POSITION
);
404 for (clno
= p
->line
, cpos
= p
->pos
; clno
> lno
; clno
--)
407 * Allow a signal to abort this loop.
409 cpos
= back_raw_line(cpos
, (char **)NULL
);
410 if (sigs
|| cpos
== NULL_POSITION
)
411 return (NULL_POSITION
);
415 * We might as well cache it.
417 add_lnum(clno
, cpos
);
422 * Return the line number of the "current" line.
423 * The argument "where" tells which line is to be considered
424 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
434 pos
= position(where
);
436 while (pos
== NULL_POSITION
&& where
>= 0 && where
< sc_height
)
437 pos
= position(++where
);
438 if (pos
== NULL_POSITION
)
440 lnum
= find_linenum(pos
);