usr.sbin/makefs/hammer2: Remove redundant hammer2_inode_modify()
[dragonfly.git] / contrib / less / linenum.c
blob1808ea97851ec6d4ed0704bde2d0381ff74dbbbd
1 /*
2 * Copyright (C) 1984-2022 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 */
67 extern int linenums;
68 extern int sigs;
69 extern int sc_height;
70 extern int screen_trashed;
71 extern int header_lines;
72 extern int nonum_headers;
75 * Initialize the line number structures.
77 public void
78 clr_linenum(VOID_PARAM)
80 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++)
87 p->next = p+1;
88 pool[NPOOL-2].next = NULL;
89 freelist = pool;
91 spare = &pool[NPOOL-1];
94 * Initialize the anchor.
96 anchor.next = anchor.prev = &anchor;
97 anchor.gap = 0;
98 anchor.pos = (POSITION)0;
99 anchor.line = 1;
103 * Calculate the gap for an entry.
105 static void
106 calcgap(p)
107 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)
116 return;
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.
125 public void
126 add_lnum(linenum, pos)
127 LINENUM linenum;
128 POSITION pos;
130 struct linenum_info *p;
131 struct linenum_info *new;
132 struct linenum_info *nextp;
133 struct linenum_info *prevp;
134 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. */
143 return;
144 nextp = p;
145 prevp = p->prev;
147 if (freelist != NULL)
150 * We still have free (unused) entries.
151 * Use one of them.
153 new = freelist;
154 freelist = freelist->next;
155 } else
158 * No free entries.
159 * Use the "spare" entry.
161 new = spare;
162 spare = NULL;
166 * Fill in the fields of the new entry,
167 * and insert it into the proper place in the list.
169 new->next = nextp;
170 new->prev = prevp;
171 new->pos = pos;
172 new->line = linenum;
174 nextp->prev = new;
175 prevp->next = new;
178 * Recalculate gaps for the new entry and the neighboring entries.
180 calcgap(new);
181 calcgap(nextp);
182 calcgap(prevp);
184 if (spare == NULL)
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)
200 spare = p;
201 mingap = p->gap;
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.
213 static void
214 longloopmessage(VOID_PARAM)
216 ierror("Calculating line numbers", NULL_PARG);
219 static int loopcount;
220 #if HAVE_TIME
221 static time_type startime;
222 #endif
224 static void
225 longish(VOID_PARAM)
227 #if HAVE_TIME
228 if (loopcount >= 0 && ++loopcount > 100)
230 loopcount = 0;
231 if (get_time() >= startime + LONGTIME)
233 longloopmessage();
234 loopcount = -1;
237 #else
238 if (loopcount >= 0 && ++loopcount > LONGLOOP)
240 longloopmessage();
241 loopcount = -1;
243 #endif
247 * Turn off line numbers because the user has interrupted
248 * a lengthy line number calculation.
250 static void
251 abort_long(VOID_PARAM)
253 if (loopcount >= 0)
254 return;
255 if (linenums == OPT_ONPLUS)
257 * We were displaying line numbers, so need to repaint.
259 screen_trashed = 1;
260 linenums = 0;
261 error("Line numbers turned off", NULL_PARG);
265 * Find the line number associated with a given position.
266 * Return 0 if we can't figure it out.
268 public LINENUM
269 find_linenum(pos)
270 POSITION pos;
272 struct linenum_info *p;
273 LINENUM linenum;
274 POSITION cpos;
276 if (!linenums)
278 * We're not using line numbers.
280 return (0);
281 if (pos == NULL_POSITION)
283 * Caller doesn't know what he's talking about.
285 return (0);
286 if (pos <= ch_zero())
288 * Beginning of file is always line number 1.
290 return (1);
293 * Find the entry nearest to the position we want.
295 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
296 continue;
297 if (p->pos == pos)
298 /* Found it exactly. */
299 return (p->line);
302 * This is the (possibly) time-consuming part.
303 * We start at the line we just found and start
304 * reading the file forward or backward till we
305 * get to the place we want.
307 * First decide whether we should go forward from the
308 * previous one or backwards from the next one.
309 * The decision is based on which way involves
310 * traversing fewer bytes in the file.
312 #if HAVE_TIME
313 startime = get_time();
314 #endif
315 loopcount = 0;
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, (char **)NULL, (int *)NULL);
330 if (ABORT_SIGS()) {
331 abort_long();
332 return (0);
334 if (cpos == NULL_POSITION)
335 return (0);
336 longish();
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, (char **)NULL, (int *)NULL);
361 if (ABORT_SIGS()) {
362 abort_long();
363 return (0);
365 if (cpos == NULL_POSITION)
366 return (0);
367 longish();
370 * We might as well cache it.
372 add_lnum(linenum, cpos);
374 loopcount = 0;
375 return (linenum);
379 * Find the position of a given line number.
380 * Return NULL_POSITION if we can't figure it out.
382 public POSITION
383 find_pos(linenum)
384 LINENUM linenum;
386 struct linenum_info *p;
387 POSITION cpos;
388 LINENUM clinenum;
390 if (linenum <= 1)
392 * Line number 1 is beginning of file.
394 return (ch_zero());
397 * Find the entry nearest to the line number we want.
399 for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next)
400 continue;
401 if (p->line == linenum)
402 /* Found it exactly. */
403 return (p->pos);
405 if (p == &anchor || linenum - p->prev->line < p->line - linenum)
408 * Go forward.
410 p = p->prev;
411 if (ch_seek(p->pos))
412 return (NULL_POSITION);
413 for (clinenum = p->line, cpos = p->pos; clinenum < linenum; clinenum++)
416 * Allow a signal to abort this loop.
418 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
419 if (ABORT_SIGS())
420 return (NULL_POSITION);
421 if (cpos == NULL_POSITION)
422 return (NULL_POSITION);
424 } else
427 * Go backward.
429 if (ch_seek(p->pos))
430 return (NULL_POSITION);
431 for (clinenum = p->line, cpos = p->pos; clinenum > linenum; clinenum--)
434 * Allow a signal to abort this loop.
436 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
437 if (ABORT_SIGS())
438 return (NULL_POSITION);
439 if (cpos == NULL_POSITION)
440 return (NULL_POSITION);
444 * We might as well cache it.
446 add_lnum(clinenum, cpos);
447 return (cpos);
451 * Return the line number of the "current" line.
452 * The argument "where" tells which line is to be considered
453 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
455 public LINENUM
456 currline(where)
457 int where;
459 POSITION pos;
460 POSITION len;
461 LINENUM linenum;
463 pos = position(where);
464 len = ch_length();
465 while (pos == NULL_POSITION && where >= 0 && where < sc_height)
466 pos = position(++where);
467 if (pos == NULL_POSITION)
468 pos = len;
469 linenum = find_linenum(pos);
470 if (pos == len)
471 linenum--;
472 return (linenum);
476 * Scan entire file, counting line numbers.
478 public void
479 scan_eof(VOID_PARAM)
481 POSITION pos = 0;
482 LINENUM linenum = 0;
484 if (ch_seek(0))
485 return;
486 ierror("Determining length of file", NULL_PARG);
487 while (pos != NULL_POSITION)
489 /* For efficiency, only add one every 256 line numbers. */
490 if ((linenum++ % 256) == 0)
491 add_lnum(linenum, pos);
492 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
493 if (ABORT_SIGS())
494 break;
499 * Return a line number adjusted for display
500 * (handles the --no-number-headers option).
502 public LINENUM
503 vlinenum(linenum)
504 LINENUM linenum;
506 if (nonum_headers)
507 linenum = (linenum < header_lines) ? 0 : linenum - header_lines;
508 return linenum;