2 * Copyright (C) 1984-2024 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 */
66 public lbool scanning_eof
= FALSE
;
71 extern int header_lines
;
72 extern int nonum_headers
;
75 * Initialize the line number structures.
77 public void clr_linenum(void)
79 struct linenum_info
*p
;
82 * Put all the entries on the free list.
83 * Leave one for the "spare".
85 for (p
= pool
; p
< &pool
[NPOOL
-2]; p
++)
87 pool
[NPOOL
-2].next
= NULL
;
90 spare
= &pool
[NPOOL
-1];
93 * Initialize the anchor.
95 anchor
.next
= anchor
.prev
= &anchor
;
97 anchor
.pos
= (POSITION
)0;
102 * Calculate the gap for an entry.
104 static void 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.
122 public void add_lnum(LINENUM linenum
, POSITION pos
)
124 struct linenum_info
*p
;
125 struct linenum_info
*new;
126 struct linenum_info
*nextp
;
127 struct linenum_info
*prevp
;
131 * Find the proper place in the list for the new one.
132 * The entries are sorted by position.
134 for (p
= anchor
.next
; p
!= &anchor
&& p
->pos
< pos
; p
= p
->next
)
135 if (p
->line
== linenum
)
136 /* We already have this one. */
141 if (freelist
!= NULL
)
144 * We still have free (unused) entries.
148 freelist
= freelist
->next
;
153 * Use the "spare" entry.
160 * Fill in the fields of the new entry,
161 * and insert it into the proper place in the list.
172 * Recalculate gaps for the new entry and the neighboring entries.
181 * We have used the spare entry.
182 * Scan the list to find the one with the smallest
183 * gap, take it out and make it the spare.
184 * We should never remove the last one, so stop when
185 * we get to p->next == &anchor. This also avoids
186 * looking at the gap of the last one, which is
187 * not computed by calcgap.
189 mingap
= anchor
.next
->gap
;
190 for (p
= anchor
.next
; p
->next
!= &anchor
; p
= p
->next
)
192 if (p
->gap
<= mingap
)
198 spare
->next
->prev
= spare
->prev
;
199 spare
->prev
->next
= spare
->next
;
204 * If we get stuck in a long loop trying to figure out the
205 * line number, print a message to tell the user what we're doing.
207 static void longloopmessage(void)
209 ierror("Calculating line numbers", NULL_PARG
);
214 void (*message
)(void);
221 static void start_delayed_msg(struct delayed_msg
*dmsg
, void (*message
)(void))
224 dmsg
->message
= message
;
226 dmsg
->startime
= get_time();
230 static void delayed_msg(struct delayed_msg
*dmsg
)
233 if (dmsg
->loopcount
>= 0 && ++(dmsg
->loopcount
) > 100)
236 if (get_time() >= dmsg
->startime
+ LONGTIME
)
239 dmsg
->loopcount
= -1;
243 if (dmsg
->loopcount
>= 0 && ++(dmsg
->loopcount
) > LONGLOOP
)
246 dmsg
->loopcount
= -1;
252 * Turn off line numbers because the user has interrupted
253 * a lengthy line number calculation.
255 static void abort_delayed_msg(struct delayed_msg
*dmsg
)
257 if (dmsg
->loopcount
>= 0)
259 if (linenums
== OPT_ONPLUS
)
261 * We were displaying line numbers, so need to repaint.
265 error("Line numbers turned off", NULL_PARG
);
269 * Find the line number associated with a given position.
270 * Return 0 if we can't figure it out.
272 public LINENUM
find_linenum(POSITION pos
)
274 struct linenum_info
*p
;
277 struct delayed_msg dmsg
;
281 * We're not using line numbers.
284 if (pos
== NULL_POSITION
)
286 * Caller doesn't know what he's talking about.
289 if (pos
<= ch_zero())
291 * Beginning of file is always line number 1.
296 * Find the entry nearest to the position we want.
298 for (p
= anchor
.next
; p
!= &anchor
&& p
->pos
< pos
; p
= p
->next
)
301 /* Found it exactly. */
305 * This is the (possibly) time-consuming part.
306 * We start at the line we just found and start
307 * reading the file forward or backward till we
308 * get to the place we want.
310 * First decide whether we should go forward from the
311 * previous one or backwards from the next one.
312 * The decision is based on which way involves
313 * traversing fewer bytes in the file.
315 start_delayed_msg(&dmsg
, longloopmessage
);
316 if (p
== &anchor
|| pos
- p
->prev
->pos
< p
->pos
- pos
)
324 for (linenum
= p
->line
, cpos
= p
->pos
; cpos
< pos
; linenum
++)
327 * Allow a signal to abort this loop.
329 cpos
= forw_raw_line(cpos
, NULL
, NULL
);
331 abort_delayed_msg(&dmsg
);
334 if (cpos
== NULL_POSITION
)
339 * We might as well cache it.
341 add_lnum(linenum
, cpos
);
343 * If the given position is not at the start of a line,
344 * make sure we return the correct line number.
355 for (linenum
= p
->line
, cpos
= p
->pos
; cpos
> pos
; linenum
--)
358 * Allow a signal to abort this loop.
360 cpos
= back_raw_line(cpos
, NULL
, NULL
);
362 abort_delayed_msg(&dmsg
);
365 if (cpos
== NULL_POSITION
)
370 * We might as well cache it.
372 add_lnum(linenum
, cpos
);
378 * Find the position of a given line number.
379 * Return NULL_POSITION if we can't figure it out.
381 public POSITION
find_pos(LINENUM linenum
)
383 struct linenum_info
*p
;
389 * Line number 1 is beginning of file.
394 * Find the entry nearest to the line number we want.
396 for (p
= anchor
.next
; p
!= &anchor
&& p
->line
< linenum
; p
= p
->next
)
398 if (p
->line
== linenum
)
399 /* Found it exactly. */
402 if (p
== &anchor
|| linenum
- p
->prev
->line
< p
->line
- linenum
)
409 return (NULL_POSITION
);
410 for (clinenum
= p
->line
, cpos
= p
->pos
; clinenum
< linenum
; clinenum
++)
413 * Allow a signal to abort this loop.
415 cpos
= forw_raw_line(cpos
, NULL
, NULL
);
417 return (NULL_POSITION
);
418 if (cpos
== NULL_POSITION
)
419 return (NULL_POSITION
);
427 return (NULL_POSITION
);
428 for (clinenum
= p
->line
, cpos
= p
->pos
; clinenum
> linenum
; clinenum
--)
431 * Allow a signal to abort this loop.
433 cpos
= back_raw_line(cpos
, NULL
, NULL
);
435 return (NULL_POSITION
);
436 if (cpos
== NULL_POSITION
)
437 return (NULL_POSITION
);
441 * We might as well cache it.
443 add_lnum(clinenum
, cpos
);
448 * Return the line number of the "current" line.
449 * The argument "where" tells which line is to be considered
450 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
452 public LINENUM
currline(int where
)
458 pos
= position(where
);
460 while (pos
== NULL_POSITION
&& where
>= 0 && where
< sc_height
)
461 pos
= position(++where
);
462 if (pos
== NULL_POSITION
)
464 linenum
= find_linenum(pos
);
470 static void detlenmessage(void)
472 ierror("Determining length of file", NULL_PARG
);
476 * Scan entire file, counting line numbers.
478 public void scan_eof(void)
480 POSITION pos
= ch_zero();
482 struct delayed_msg dmsg
;
487 * scanning_eof prevents the "Waiting for data" message from
488 * overwriting "Determining length of file".
490 start_delayed_msg(&dmsg
, detlenmessage
);
492 while (pos
!= NULL_POSITION
)
494 /* For efficiency, only add one every 256 line numbers. */
495 if ((linenum
++ % 256) == 0)
496 add_lnum(linenum
, pos
);
497 pos
= forw_raw_line(pos
, NULL
, NULL
);
500 abort_delayed_msg(&dmsg
);
505 scanning_eof
= FALSE
;
509 * Return a line number adjusted for display
510 * (handles the --no-number-headers option).
512 public LINENUM
vlinenum(LINENUM linenum
)
515 linenum
= (linenum
< header_lines
) ? 0 : linenum
- header_lines
;