2 * Copyright (C) 1984-2015 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, see the README file.
12 * Code to handle displaying line numbers.
14 * Finding the line number of a given file position is rather tricky.
15 * We don't want to just start at the beginning of the file and
16 * count newlines, because that is slow for large files (and also
17 * wouldn't work if we couldn't get to the start of the file; e.g.
18 * if input is a long pipe).
20 * So we use the function add_lnum to cache line numbers.
21 * We try to be very clever and keep only the more interesting
22 * line numbers when we run out of space in our table. A line
23 * number is more interesting than another when it is far from
24 * other line numbers. For example, we'd rather keep lines
25 * 100,200,300 than 100,101,300. 200 is more interesting than
26 * 101 because 101 can be derived very cheaply from 100, while
27 * 200 is more expensive to derive from 100.
29 * The function currline() returns the line number of a given
30 * position in the file. As a side effect, it calls add_lnum
31 * to cache the line number. Therefore currline is occasionally
32 * called to make sure we cache line numbers often enough.
38 * Structure to keep track of a line number and the associated file position.
39 * A doubly-linked circular list of line numbers is kept ordered by line number.
43 struct linenum_info *next; /* Link to next in the list */
44 struct linenum_info *prev; /* Line to previous in the list */
45 POSITION pos; /* File position */
46 POSITION gap; /* Gap between prev and next */
47 LINENUM line; /* Line number */
50 * "gap" needs some explanation: the gap of any particular line number
51 * is the distance between the previous one and the next one in the list.
52 * ("Distance" means difference in file position.) In other words, the
53 * gap of a line number is the gap which would be introduced if this
54 * line number were deleted. It is used to decide which one to replace
55 * when we have a new one to insert and the table is full.
58 #define NPOOL 200 /* Size of line number pool */
60 #define LONGTIME (2) /* In seconds */
62 static struct linenum_info anchor; /* Anchor of the list */
63 static struct linenum_info *freelist; /* Anchor of the unused entries */
64 static struct linenum_info pool[NPOOL]; /* The pool itself */
65 static struct linenum_info *spare; /* We always keep one spare entry */
70 extern int screen_trashed;
73 * Initialize the line number structures.
78 struct linenum_info *p;
81 * Put all the entries on the free list.
82 * Leave one for the "spare".
84 for (p = pool; p < &pool[NPOOL-2]; p++)
86 pool[NPOOL-2].next = NULL;
89 spare = &pool[NPOOL-1];
92 * Initialize the anchor.
94 anchor.next = anchor.prev = &anchor;
96 anchor.pos = (POSITION)0;
101 * Calculate the gap for an entry.
104 calcgap(struct linenum_info *p)
107 * Don't bother to compute a gap for the anchor.
108 * Also don't compute a gap for the last one in the list.
109 * The gap for that last one should be considered infinite,
110 * but we never look at it anyway.
112 if (p == &anchor || p->next == &anchor)
114 p->gap = p->next->pos - p->prev->pos;
118 * Add a new line number to the cache.
119 * The specified position (pos) should be the file position of the
120 * FIRST character in the specified line.
123 add_lnum(LINENUM linenum, POSITION pos)
125 struct linenum_info *p;
126 struct linenum_info *new;
127 struct linenum_info *nextp;
128 struct linenum_info *prevp;
132 * Find the proper place in the list for the new one.
133 * The entries are sorted by position.
135 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
136 if (p->line == linenum)
137 /* We already have this one. */
142 if (freelist != NULL)
145 * We still have free (unused) entries.
149 freelist = freelist->next;
154 * Use the "spare" entry.
161 * Fill in the fields of the new entry,
162 * and insert it into the proper place in the list.
173 * Recalculate gaps for the new entry and the neighboring entries.
182 * We have used the spare entry.
183 * Scan the list to find the one with the smallest
184 * gap, take it out and make it the spare.
185 * We should never remove the last one, so stop when
186 * we get to p->next == &anchor. This also avoids
187 * looking at the gap of the last one, which is
188 * not computed by calcgap.
190 mingap = anchor.next->gap;
191 for (p = anchor.next; p->next != &anchor; p = p->next)
193 if (p->gap <= mingap)
199 spare->next->prev = spare->prev;
200 spare->prev->next = spare->next;
205 * If we get stuck in a long loop trying to figure out the
206 * line number, print a message to tell the user what we're doing.
209 longloopmessage(void)
211 ierror("Calculating line numbers", NULL_PARG);
214 static int loopcount;
216 static time_type startime;
223 if (loopcount >= 0 && ++loopcount > 100)
226 if (get_time() >= startime + LONGTIME)
233 if (loopcount >= 0 && ++loopcount > LONGLOOP)
242 * Turn off line numbers because the user has interrupted
243 * a lengthy line number calculation.
248 if (linenums == OPT_ONPLUS)
250 * We were displaying line numbers, so need to repaint.
254 error("Line numbers turned off", NULL_PARG);
258 * Find the line number associated with a given position.
259 * Return 0 if we can't figure it out.
262 find_linenum(POSITION pos)
264 struct linenum_info *p;
270 * We're not using line numbers.
273 if (pos == NULL_POSITION)
275 * Caller doesn't know what he's talking about.
278 if (pos <= ch_zero())
280 * Beginning of file is always line number 1.
285 * Find the entry nearest to the position we want.
287 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
290 /* Found it exactly. */
294 * This is the (possibly) time-consuming part.
295 * We start at the line we just found and start
296 * reading the file forward or backward till we
297 * get to the place we want.
299 * First decide whether we should go forward from the
300 * previous one or backwards from the next one.
301 * The decision is based on which way involves
302 * traversing fewer bytes in the file.
305 startime = get_time();
307 if (p == &anchor || pos - p->prev->pos < p->pos - pos)
316 for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++)
319 * Allow a signal to abort this loop.
321 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
326 if (cpos == NULL_POSITION)
331 * We might as well cache it.
333 add_lnum(linenum, cpos);
335 * If the given position is not at the start of a line,
336 * make sure we return the correct line number.
348 for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--)
351 * Allow a signal to abort this loop.
353 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
358 if (cpos == NULL_POSITION)
363 * We might as well cache it.
365 add_lnum(linenum, cpos);
372 * Find the position of a given line number.
373 * Return NULL_POSITION if we can't figure it out.
376 find_pos(LINENUM linenum)
378 struct linenum_info *p;
384 * Line number 1 is beginning of file.
389 * Find the entry nearest to the line number we want.
391 for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next)
393 if (p->line == linenum)
394 /* Found it exactly. */
397 if (p == &anchor || linenum - p->prev->line < p->line - linenum)
404 return (NULL_POSITION);
405 for (clinenum = p->line, cpos = p->pos; clinenum < linenum; clinenum++)
408 * Allow a signal to abort this loop.
410 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
412 return (NULL_POSITION);
413 if (cpos == NULL_POSITION)
414 return (NULL_POSITION);
422 return (NULL_POSITION);
423 for (clinenum = p->line, cpos = p->pos; clinenum > linenum; clinenum--)
426 * Allow a signal to abort this loop.
428 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
430 return (NULL_POSITION);
431 if (cpos == NULL_POSITION)
432 return (NULL_POSITION);
436 * We might as well cache it.
438 add_lnum(clinenum, cpos);
443 * Return the line number of the "current" line.
444 * The argument "where" tells which line is to be considered
445 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
454 pos = position(where);
456 while (pos == NULL_POSITION && where >= 0 && where < sc_height)
457 pos = position(++where);
458 if (pos == NULL_POSITION)
460 linenum = find_linenum(pos);