wg.conf.5: Fix a typo (in-inline comments are *not* allowed)
[dragonfly.git] / contrib / less / linenum.c
blob2258cdd4901b0fbcd3b53c4d80b589937470efca
1 /*
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.
8 */
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.
35 #include "less.h"
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.
41 struct linenum_info
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;
68 extern int linenums;
69 extern int sigs;
70 extern int sc_height;
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++)
86 p->next = p+1;
87 pool[NPOOL-2].next = NULL;
88 freelist = pool;
90 spare = &pool[NPOOL-1];
93 * Initialize the anchor.
95 anchor.next = anchor.prev = &anchor;
96 anchor.gap = 0;
97 anchor.pos = (POSITION)0;
98 anchor.line = 1;
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)
113 return;
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;
128 POSITION mingap;
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. */
137 return;
138 nextp = p;
139 prevp = p->prev;
141 if (freelist != NULL)
144 * We still have free (unused) entries.
145 * Use one of them.
147 new = freelist;
148 freelist = freelist->next;
149 } else
152 * No free entries.
153 * Use the "spare" entry.
155 new = spare;
156 spare = NULL;
160 * Fill in the fields of the new entry,
161 * and insert it into the proper place in the list.
163 new->next = nextp;
164 new->prev = prevp;
165 new->pos = pos;
166 new->line = linenum;
168 nextp->prev = new;
169 prevp->next = new;
172 * Recalculate gaps for the new entry and the neighboring entries.
174 calcgap(new);
175 calcgap(nextp);
176 calcgap(prevp);
178 if (spare == NULL)
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)
194 spare = p;
195 mingap = p->gap;
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);
212 struct delayed_msg
214 void (*message)(void);
215 int loopcount;
216 #if HAVE_TIME
217 time_type startime;
218 #endif
221 static void start_delayed_msg(struct delayed_msg *dmsg, void (*message)(void))
223 dmsg->loopcount = 0;
224 dmsg->message = message;
225 #if HAVE_TIME
226 dmsg->startime = get_time();
227 #endif
230 static void delayed_msg(struct delayed_msg *dmsg)
232 #if HAVE_TIME
233 if (dmsg->loopcount >= 0 && ++(dmsg->loopcount) > 100)
235 dmsg->loopcount = 0;
236 if (get_time() >= dmsg->startime + LONGTIME)
238 dmsg->message();
239 dmsg->loopcount = -1;
242 #else
243 if (dmsg->loopcount >= 0 && ++(dmsg->loopcount) > LONGLOOP)
245 dmsg->message();
246 dmsg->loopcount = -1;
248 #endif
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)
258 return;
259 if (linenums == OPT_ONPLUS)
261 * We were displaying line numbers, so need to repaint.
263 screen_trashed();
264 linenums = 0;
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;
275 LINENUM linenum;
276 POSITION cpos;
277 struct delayed_msg dmsg;
279 if (!linenums)
281 * We're not using line numbers.
283 return (0);
284 if (pos == NULL_POSITION)
286 * Caller doesn't know what he's talking about.
288 return (0);
289 if (pos <= ch_zero())
291 * Beginning of file is always line number 1.
293 return (1);
296 * Find the entry nearest to the position we want.
298 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
299 continue;
300 if (p->pos == pos)
301 /* Found it exactly. */
302 return (p->line);
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)
319 * Go forward.
321 p = p->prev;
322 if (ch_seek(p->pos))
323 return (0);
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);
330 if (ABORT_SIGS()) {
331 abort_delayed_msg(&dmsg);
332 return (0);
334 if (cpos == NULL_POSITION)
335 return (0);
336 delayed_msg(&dmsg);
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.
346 if (cpos > pos)
347 linenum--;
348 } else
351 * Go backward.
353 if (ch_seek(p->pos))
354 return (0);
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);
361 if (ABORT_SIGS()) {
362 abort_delayed_msg(&dmsg);
363 return (0);
365 if (cpos == NULL_POSITION)
366 return (0);
367 delayed_msg(&dmsg);
370 * We might as well cache it.
372 add_lnum(linenum, cpos);
374 return (linenum);
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;
384 POSITION cpos;
385 LINENUM clinenum;
387 if (linenum <= 1)
389 * Line number 1 is beginning of file.
391 return (ch_zero());
394 * Find the entry nearest to the line number we want.
396 for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next)
397 continue;
398 if (p->line == linenum)
399 /* Found it exactly. */
400 return (p->pos);
402 if (p == &anchor || linenum - p->prev->line < p->line - linenum)
405 * Go forward.
407 p = p->prev;
408 if (ch_seek(p->pos))
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);
416 if (ABORT_SIGS())
417 return (NULL_POSITION);
418 if (cpos == NULL_POSITION)
419 return (NULL_POSITION);
421 } else
424 * Go backward.
426 if (ch_seek(p->pos))
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);
434 if (ABORT_SIGS())
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);
444 return (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)
454 POSITION pos;
455 POSITION len;
456 LINENUM linenum;
458 pos = position(where);
459 len = ch_length();
460 while (pos == NULL_POSITION && where >= 0 && where < sc_height)
461 pos = position(++where);
462 if (pos == NULL_POSITION)
463 pos = len;
464 linenum = find_linenum(pos);
465 if (pos == len)
466 linenum--;
467 return (linenum);
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();
481 LINENUM linenum = 0;
482 struct delayed_msg dmsg;
484 if (ch_seek(0))
485 return;
487 * scanning_eof prevents the "Waiting for data" message from
488 * overwriting "Determining length of file".
490 start_delayed_msg(&dmsg, detlenmessage);
491 scanning_eof = TRUE;
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);
498 if (ABORT_SIGS())
500 abort_delayed_msg(&dmsg);
501 break;
503 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)
514 if (nonum_headers)
515 linenum = (linenum < header_lines) ? 0 : linenum - header_lines;
516 return linenum;