1 /* $NetBSD: linenum.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
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.
14 * Code to handle displaying line numbers.
16 * Finding the line number of a given file position is rather tricky.
17 * We don't want to just start at the beginning of the file and
18 * count newlines, because that is slow for large files (and also
19 * wouldn't work if we couldn't get to the start of the file; e.g.
20 * if input is a long pipe).
22 * So we use the function add_lnum to cache line numbers.
23 * We try to be very clever and keep only the more interesting
24 * line numbers when we run out of space in our table. A line
25 * number is more interesting than another when it is far from
26 * other line numbers. For example, we'd rather keep lines
27 * 100,200,300 than 100,101,300. 200 is more interesting than
28 * 101 because 101 can be derived very cheaply from 100, while
29 * 200 is more expensive to derive from 100.
31 * The function currline() returns the line number of a given
32 * position in the file. As a side effect, it calls add_lnum
33 * to cache the line number. Therefore currline is occasionally
34 * called to make sure we cache line numbers often enough.
40 * Structure to keep track of a line number and the associated file position.
41 * A doubly-linked circular list of line numbers is kept ordered by line number.
45 struct linenum_info
*next
; /* Link to next in the list */
46 struct linenum_info
*prev
; /* Line to previous in the list */
47 POSITION pos
; /* File position */
48 POSITION gap
; /* Gap between prev and next */
49 LINENUM line
; /* Line number */
52 * "gap" needs some explanation: the gap of any particular line number
53 * is the distance between the previous one and the next one in the list.
54 * ("Distance" means difference in file position.) In other words, the
55 * gap of a line number is the gap which would be introduced if this
56 * line number were deleted. It is used to decide which one to replace
57 * when we have a new one to insert and the table is full.
60 #define NPOOL 200 /* Size of line number pool */
62 #define LONGTIME (2) /* In seconds */
64 static struct linenum_info anchor
; /* Anchor of the list */
65 static struct linenum_info
*freelist
; /* Anchor of the unused entries */
66 static struct linenum_info pool
[NPOOL
]; /* The pool itself */
67 static struct linenum_info
*spare
; /* We always keep one spare entry */
72 extern int screen_trashed
;
74 static void calcgap
__P((struct linenum_info
*));
75 static void longloopmessage
__P((void));
76 static void longish
__P((void));
79 * Initialize the line number structures.
84 register struct linenum_info
*p
;
87 * Put all the entries on the free list.
88 * Leave one for the "spare".
90 for (p
= pool
; p
< &pool
[NPOOL
-2]; p
++)
92 pool
[NPOOL
-2].next
= NULL
;
95 spare
= &pool
[NPOOL
-1];
98 * Initialize the anchor.
100 anchor
.next
= anchor
.prev
= &anchor
;
102 anchor
.pos
= (POSITION
)0;
107 * Calculate the gap for an entry.
111 register struct linenum_info
*p
;
114 * Don't bother to compute a gap for the anchor.
115 * Also don't compute a gap for the last one in the list.
116 * The gap for that last one should be considered infinite,
117 * but we never look at it anyway.
119 if (p
== &anchor
|| p
->next
== &anchor
)
121 p
->gap
= p
->next
->pos
- p
->prev
->pos
;
125 * Add a new line number to the cache.
126 * The specified position (pos) should be the file position of the
127 * FIRST character in the specified line.
130 add_lnum(linenum
, pos
)
134 register struct linenum_info
*p
;
135 register struct linenum_info
*new;
136 register struct linenum_info
*nextp
;
137 register struct linenum_info
*prevp
;
138 register POSITION mingap
;
141 * Find the proper place in the list for the new one.
142 * The entries are sorted by position.
144 for (p
= anchor
.next
; p
!= &anchor
&& p
->pos
< pos
; p
= p
->next
)
145 if (p
->line
== linenum
)
146 /* We already have this one. */
151 if (freelist
!= NULL
)
154 * We still have free (unused) entries.
158 freelist
= freelist
->next
;
163 * Use the "spare" entry.
170 * Fill in the fields of the new entry,
171 * and insert it into the proper place in the list.
182 * Recalculate gaps for the new entry and the neighboring entries.
191 * We have used the spare entry.
192 * Scan the list to find the one with the smallest
193 * gap, take it out and make it the spare.
194 * We should never remove the last one, so stop when
195 * we get to p->next == &anchor. This also avoids
196 * looking at the gap of the last one, which is
197 * not computed by calcgap.
199 mingap
= anchor
.next
->gap
;
200 for (p
= anchor
.next
; p
->next
!= &anchor
; p
= p
->next
)
202 if (p
->gap
<= mingap
)
208 spare
->next
->prev
= spare
->prev
;
209 spare
->prev
->next
= spare
->next
;
214 * If we get stuck in a long loop trying to figure out the
215 * line number, print a message to tell the user what we're doing.
220 ierror("Calculating line numbers", NULL_PARG
);
223 static int loopcount
;
225 static long startime
;
232 if (loopcount
>= 0 && ++loopcount
> 100)
235 if (get_time() >= startime
+ LONGTIME
)
242 if (loopcount
>= 0 && ++loopcount
> LONGLOOP
)
251 * Turn off line numbers because the user has interrupted
252 * a lengthy line number calculation.
257 if (linenums
== OPT_ONPLUS
)
259 * We were displaying line numbers, so need to repaint.
263 error("Line numbers turned off", NULL_PARG
);
267 * Find the line number associated with a given position.
268 * Return 0 if we can't figure it out.
274 register struct linenum_info
*p
;
275 register LINENUM linenum
;
280 * We're not using line numbers.
283 if (pos
== NULL_POSITION
)
285 * Caller doesn't know what he's talking about.
288 if (pos
<= ch_zero())
290 * Beginning of file is always line number 1.
295 * Find the entry nearest to the position we want.
297 for (p
= anchor
.next
; p
!= &anchor
&& p
->pos
< pos
; p
= p
->next
)
300 /* Found it exactly. */
304 * This is the (possibly) time-consuming part.
305 * We start at the line we just found and start
306 * reading the file forward or backward till we
307 * get to the place we want.
309 * First decide whether we should go forward from the
310 * previous one or backwards from the next one.
311 * The decision is based on which way involves
312 * traversing fewer bytes in the file.
315 startime
= get_time();
317 if (p
== &anchor
|| pos
- p
->prev
->pos
< p
->pos
- pos
)
326 for (linenum
= p
->line
, cpos
= p
->pos
; cpos
< pos
; linenum
++)
329 * Allow a signal to abort this loop.
331 cpos
= forw_raw_line(cpos
, (char **)NULL
, (int *)NULL
);
336 if (cpos
== NULL_POSITION
)
341 * We might as well cache it.
343 add_lnum(linenum
, cpos
);
345 * If the given position is not at the start of a line,
346 * make sure we return the correct line number.
358 for (linenum
= p
->line
, cpos
= p
->pos
; cpos
> pos
; linenum
--)
361 * Allow a signal to abort this loop.
363 cpos
= back_raw_line(cpos
, (char **)NULL
, (int *)NULL
);
368 if (cpos
== NULL_POSITION
)
373 * We might as well cache it.
375 add_lnum(linenum
, cpos
);
382 * Find the position of a given line number.
383 * Return NULL_POSITION if we can't figure it out.
389 register struct linenum_info
*p
;
395 * Line number 1 is beginning of file.
400 * Find the entry nearest to the line number we want.
402 for (p
= anchor
.next
; p
!= &anchor
&& p
->line
< linenum
; p
= p
->next
)
404 if (p
->line
== linenum
)
405 /* Found it exactly. */
408 if (p
== &anchor
|| linenum
- p
->prev
->line
< p
->line
- linenum
)
415 return (NULL_POSITION
);
416 for (clinenum
= p
->line
, cpos
= p
->pos
; clinenum
< linenum
; clinenum
++)
419 * Allow a signal to abort this loop.
421 cpos
= forw_raw_line(cpos
, (char **)NULL
, (int *)NULL
);
423 return (NULL_POSITION
);
424 if (cpos
== NULL_POSITION
)
425 return (NULL_POSITION
);
433 return (NULL_POSITION
);
434 for (clinenum
= p
->line
, cpos
= p
->pos
; clinenum
> linenum
; clinenum
--)
437 * Allow a signal to abort this loop.
439 cpos
= back_raw_line(cpos
, (char **)NULL
, (int *)NULL
);
441 return (NULL_POSITION
);
442 if (cpos
== NULL_POSITION
)
443 return (NULL_POSITION
);
447 * We might as well cache it.
449 add_lnum(clinenum
, cpos
);
454 * Return the line number of the "current" line.
455 * The argument "where" tells which line is to be considered
456 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
466 pos
= position(where
);
468 while (pos
== NULL_POSITION
&& where
>= 0 && where
< sc_height
)
469 pos
= position(++where
);
470 if (pos
== NULL_POSITION
)
472 linenum
= find_linenum(pos
);