MFC r1.6 r1.15 (HEAD)
[dragonfly.git] / contrib / less-4 / linenum.c
blob184306b8e8bc721f8f097b8e1f4e959abda1719b
1 /*
2 * Copyright (C) 1984-2007 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 about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * Code to handle displaying line numbers.
15 * Finding the line number of a given file position is rather tricky.
16 * We don't want to just start at the beginning of the file and
17 * count newlines, because that is slow for large files (and also
18 * wouldn't work if we couldn't get to the start of the file; e.g.
19 * if input is a long pipe).
21 * So we use the function add_lnum to cache line numbers.
22 * We try to be very clever and keep only the more interesting
23 * line numbers when we run out of space in our table. A line
24 * number is more interesting than another when it is far from
25 * other line numbers. For example, we'd rather keep lines
26 * 100,200,300 than 100,101,300. 200 is more interesting than
27 * 101 because 101 can be derived very cheaply from 100, while
28 * 200 is more expensive to derive from 100.
30 * The function currline() returns the line number of a given
31 * position in the file. As a side effect, it calls add_lnum
32 * to cache the line number. Therefore currline is occasionally
33 * called to make sure we cache line numbers often enough.
36 #include "less.h"
39 * Structure to keep track of a line number and the associated file position.
40 * A doubly-linked circular list of line numbers is kept ordered by line number.
42 struct linenum_info
44 struct linenum_info *next; /* Link to next in the list */
45 struct linenum_info *prev; /* Line to previous in the list */
46 POSITION pos; /* File position */
47 POSITION gap; /* Gap between prev and next */
48 LINENUM line; /* Line number */
51 * "gap" needs some explanation: the gap of any particular line number
52 * is the distance between the previous one and the next one in the list.
53 * ("Distance" means difference in file position.) In other words, the
54 * gap of a line number is the gap which would be introduced if this
55 * line number were deleted. It is used to decide which one to replace
56 * when we have a new one to insert and the table is full.
59 #define NPOOL 50 /* Size of line number pool */
61 #define LONGTIME (2) /* In seconds */
63 public int lnloop = 0; /* Are we in the line num loop? */
65 static struct linenum_info anchor; /* Anchor of the list */
66 static struct linenum_info *freelist; /* Anchor of the unused entries */
67 static struct linenum_info pool[NPOOL]; /* The pool itself */
68 static struct linenum_info *spare; /* We always keep one spare entry */
70 extern int linenums;
71 extern int sigs;
72 extern int sc_height;
75 * Initialize the line number structures.
77 public void
78 clr_linenum()
80 register 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 register 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 register struct linenum_info *p;
131 register struct linenum_info *new;
132 register struct linenum_info *nextp;
133 register struct linenum_info *prevp;
134 register 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()
216 ierror("Calculating line numbers", NULL_PARG);
218 * Set the lnloop flag here, so if the user interrupts while
219 * we are calculating line numbers, the signal handler will
220 * turn off line numbers (linenums=0).
222 lnloop = 1;
225 static int loopcount;
226 #if HAVE_TIME
227 static long startime;
228 #endif
230 static void
231 longish()
233 #if HAVE_TIME
234 if (loopcount >= 0 && ++loopcount > 100)
236 loopcount = 0;
237 if (get_time() >= startime + LONGTIME)
239 longloopmessage();
240 loopcount = -1;
243 #else
244 if (loopcount >= 0 && ++loopcount > LONGLOOP)
246 longloopmessage();
247 loopcount = -1;
249 #endif
253 * Find the line number associated with a given position.
254 * Return 0 if we can't figure it out.
256 public LINENUM
257 find_linenum(pos)
258 POSITION pos;
260 register struct linenum_info *p;
261 register LINENUM linenum;
262 POSITION cpos;
264 if (!linenums)
266 * We're not using line numbers.
268 return (0);
269 if (pos == NULL_POSITION)
271 * Caller doesn't know what he's talking about.
273 return (0);
274 if (pos <= ch_zero())
276 * Beginning of file is always line number 1.
278 return (1);
281 * Find the entry nearest to the position we want.
283 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
284 continue;
285 if (p->pos == pos)
286 /* Found it exactly. */
287 return (p->line);
290 * This is the (possibly) time-consuming part.
291 * We start at the line we just found and start
292 * reading the file forward or backward till we
293 * get to the place we want.
295 * First decide whether we should go forward from the
296 * previous one or backwards from the next one.
297 * The decision is based on which way involves
298 * traversing fewer bytes in the file.
300 #if HAVE_TIME
301 startime = get_time();
302 #endif
303 if (p == &anchor || pos - p->prev->pos < p->pos - pos)
306 * Go forward.
308 p = p->prev;
309 if (ch_seek(p->pos))
310 return (0);
311 loopcount = 0;
312 for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++)
315 * Allow a signal to abort this loop.
317 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
318 if (ABORT_SIGS() || cpos == NULL_POSITION)
319 return (0);
320 longish();
322 lnloop = 0;
324 * We might as well cache it.
326 add_lnum(linenum, cpos);
328 * If the given position is not at the start of a line,
329 * make sure we return the correct line number.
331 if (cpos > pos)
332 linenum--;
333 } else
336 * Go backward.
338 if (ch_seek(p->pos))
339 return (0);
340 loopcount = 0;
341 for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--)
344 * Allow a signal to abort this loop.
346 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
347 if (ABORT_SIGS() || cpos == NULL_POSITION)
348 return (0);
349 longish();
351 lnloop = 0;
353 * We might as well cache it.
355 add_lnum(linenum, cpos);
358 return (linenum);
362 * Find the position of a given line number.
363 * Return NULL_POSITION if we can't figure it out.
365 public POSITION
366 find_pos(linenum)
367 LINENUM linenum;
369 register struct linenum_info *p;
370 POSITION cpos;
371 LINENUM clinenum;
373 if (linenum <= 1)
375 * Line number 1 is beginning of file.
377 return (ch_zero());
380 * Find the entry nearest to the line number we want.
382 for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next)
383 continue;
384 if (p->line == linenum)
385 /* Found it exactly. */
386 return (p->pos);
388 if (p == &anchor || linenum - p->prev->line < p->line - linenum)
391 * Go forward.
393 p = p->prev;
394 if (ch_seek(p->pos))
395 return (NULL_POSITION);
396 for (clinenum = p->line, cpos = p->pos; clinenum < linenum; clinenum++)
399 * Allow a signal to abort this loop.
401 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
402 if (ABORT_SIGS() || cpos == NULL_POSITION)
403 return (NULL_POSITION);
405 } else
408 * Go backward.
410 if (ch_seek(p->pos))
411 return (NULL_POSITION);
412 for (clinenum = p->line, cpos = p->pos; clinenum > linenum; clinenum--)
415 * Allow a signal to abort this loop.
417 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
418 if (ABORT_SIGS() || cpos == NULL_POSITION)
419 return (NULL_POSITION);
423 * We might as well cache it.
425 add_lnum(clinenum, cpos);
426 return (cpos);
430 * Return the line number of the "current" line.
431 * The argument "where" tells which line is to be considered
432 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
434 public LINENUM
435 currline(where)
436 int where;
438 POSITION pos;
439 POSITION len;
440 LINENUM linenum;
442 pos = position(where);
443 len = ch_length();
444 while (pos == NULL_POSITION && where >= 0 && where < sc_height)
445 pos = position(++where);
446 if (pos == NULL_POSITION)
447 pos = len;
448 linenum = find_linenum(pos);
449 if (pos == len)
450 linenum--;
451 return (linenum);