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.
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.
39 * Structure to keep track of a line number and the associated file position.
40 * A doubly-linked circular list of line numbers is kept ordered by line number.
44 struct linenum_info *next; /* Link to next in the list */
45 struct linenum_info *prev; /* Line to previous in the list */
46 POSITION pos; /* File position */
47 POSITION gap; /* Gap between prev and next */
48 LINENUM line; /* Line number */
51 * "gap" needs some explanation: the gap of any particular line number
52 * is the distance between the previous one and the next one in the list.
53 * ("Distance" means difference in file position.) In other words, the
54 * gap of a line number is the gap which would be introduced if this
55 * line number were deleted. It is used to decide which one to replace
56 * when we have a new one to insert and the table is full.
59 #define NPOOL 50 /* Size of line number pool */
61 #define LONGTIME (2) /* In seconds */
63 public int lnloop = 0; /* Are we in the line num loop? */
65 static struct linenum_info anchor; /* Anchor of the list */
66 static struct linenum_info *freelist; /* Anchor of the unused entries */
67 static struct linenum_info pool[NPOOL]; /* The pool itself */
68 static struct linenum_info *spare; /* We always keep one spare entry */
75 * Initialize the line number structures.
80 register struct linenum_info *p;
83 * Put all the entries on the free list.
84 * Leave one for the "spare".
86 for (p = pool; p < &pool[NPOOL-2]; p++)
88 pool[NPOOL-2].next = NULL;
91 spare = &pool[NPOOL-1];
94 * Initialize the anchor.
96 anchor.next = anchor.prev = &anchor;
98 anchor.pos = (POSITION)0;
103 * Calculate the gap for an entry.
107 register struct linenum_info *p;
110 * Don't bother to compute a gap for the anchor.
111 * Also don't compute a gap for the last one in the list.
112 * The gap for that last one should be considered infinite,
113 * but we never look at it anyway.
115 if (p == &anchor || p->next == &anchor)
117 p->gap = p->next->pos - p->prev->pos;
121 * Add a new line number to the cache.
122 * The specified position (pos) should be the file position of the
123 * FIRST character in the specified line.
126 add_lnum(linenum, pos)
130 register struct linenum_info *p;
131 register struct linenum_info *new;
132 register struct linenum_info *nextp;
133 register struct linenum_info *prevp;
134 register POSITION mingap;
137 * Find the proper place in the list for the new one.
138 * The entries are sorted by position.
140 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
141 if (p->line == linenum)
142 /* We already have this one. */
147 if (freelist != NULL)
150 * We still have free (unused) entries.
154 freelist = freelist->next;
159 * Use the "spare" entry.
166 * Fill in the fields of the new entry,
167 * and insert it into the proper place in the list.
178 * Recalculate gaps for the new entry and the neighboring entries.
187 * We have used the spare entry.
188 * Scan the list to find the one with the smallest
189 * gap, take it out and make it the spare.
190 * We should never remove the last one, so stop when
191 * we get to p->next == &anchor. This also avoids
192 * looking at the gap of the last one, which is
193 * not computed by calcgap.
195 mingap = anchor.next->gap;
196 for (p = anchor.next; p->next != &anchor; p = p->next)
198 if (p->gap <= mingap)
204 spare->next->prev = spare->prev;
205 spare->prev->next = spare->next;
210 * If we get stuck in a long loop trying to figure out the
211 * line number, print a message to tell the user what we're doing.
216 ierror("Calculating line numbers", NULL_PARG);
218 * Set the lnloop flag here, so if the user interrupts while
219 * we are calculating line numbers, the signal handler will
220 * turn off line numbers (linenums=0).
225 static int loopcount;
227 static long startime;
234 if (loopcount >= 0 && ++loopcount > 100)
237 if (get_time() >= startime + LONGTIME)
244 if (loopcount >= 0 && ++loopcount > LONGLOOP)
253 * Find the line number associated with a given position.
254 * Return 0 if we can't figure it out.
260 register struct linenum_info *p;
261 register LINENUM linenum;
266 * We're not using line numbers.
269 if (pos == NULL_POSITION)
271 * Caller doesn't know what he's talking about.
274 if (pos <= ch_zero())
276 * Beginning of file is always line number 1.
281 * Find the entry nearest to the position we want.
283 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
286 /* Found it exactly. */
290 * This is the (possibly) time-consuming part.
291 * We start at the line we just found and start
292 * reading the file forward or backward till we
293 * get to the place we want.
295 * First decide whether we should go forward from the
296 * previous one or backwards from the next one.
297 * The decision is based on which way involves
298 * traversing fewer bytes in the file.
301 startime = get_time();
303 if (p == &anchor || pos - p->prev->pos < p->pos - pos)
312 for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++)
315 * Allow a signal to abort this loop.
317 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
318 if (ABORT_SIGS() || cpos == NULL_POSITION)
324 * We might as well cache it.
326 add_lnum(linenum, cpos);
328 * If the given position is not at the start of a line,
329 * make sure we return the correct line number.
341 for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--)
344 * Allow a signal to abort this loop.
346 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
347 if (ABORT_SIGS() || cpos == NULL_POSITION)
353 * We might as well cache it.
355 add_lnum(linenum, cpos);
362 * Find the position of a given line number.
363 * Return NULL_POSITION if we can't figure it out.
369 register struct linenum_info *p;
375 * Line number 1 is beginning of file.
380 * Find the entry nearest to the line number we want.
382 for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next)
384 if (p->line == linenum)
385 /* Found it exactly. */
388 if (p == &anchor || linenum - p->prev->line < p->line - linenum)
395 return (NULL_POSITION);
396 for (clinenum = p->line, cpos = p->pos; clinenum < linenum; clinenum++)
399 * Allow a signal to abort this loop.
401 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
402 if (ABORT_SIGS() || cpos == NULL_POSITION)
403 return (NULL_POSITION);
411 return (NULL_POSITION);
412 for (clinenum = p->line, cpos = p->pos; clinenum > linenum; clinenum--)
415 * Allow a signal to abort this loop.
417 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
418 if (ABORT_SIGS() || cpos == NULL_POSITION)
419 return (NULL_POSITION);
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).
442 pos = position(where);
444 while (pos == NULL_POSITION && where >= 0 && where < sc_height)
445 pos = position(++where);
446 if (pos == NULL_POSITION)
448 linenum = find_linenum(pos);