Commit | Line | Data |
---|---|---|
bfe13c81 KB |
1 | /* |
2 | * Copyright (c) 1988 Mark Nudleman | |
3 | * Copyright (c) 1988 Regents of the University of California. | |
4 | * All rights reserved. | |
5 | * | |
6 | * This code is derived from software contributed to Berkeley by | |
7 | * Mark Nudleman. | |
8 | * | |
9 | * Redistribution and use in source and binary forms are permitted | |
10 | * provided that the above copyright notice and this paragraph are | |
11 | * duplicated in all such forms and that any documentation, | |
12 | * advertising materials, and other materials related to such | |
13 | * distribution and use acknowledge that the software was developed | |
14 | * by the University of California, Berkeley. The name of the | |
15 | * University may not be used to endorse or promote products derived | |
16 | * from this software without specific prior written permission. | |
17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
18 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
19 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
20 | */ | |
21 | ||
22 | #ifndef lint | |
54795006 | 23 | static char sccsid[] = "@(#)linenum.c 5.2 (Berkeley) %G%"; |
bfe13c81 KB |
24 | #endif /* not lint */ |
25 | ||
26 | /* | |
27 | * Code to handle displaying line numbers. | |
28 | * | |
29 | * Finding the line number of a given file position is rather tricky. | |
30 | * We don't want to just start at the beginning of the file and | |
31 | * count newlines, because that is slow for large files (and also | |
32 | * wouldn't work if we couldn't get to the start of the file; e.g. | |
33 | * if input is a long pipe). | |
34 | * | |
35 | * So we use the function add_lnum to cache line numbers. | |
36 | * We try to be very clever and keep only the more interesting | |
37 | * line numbers when we run out of space in our table. A line | |
38 | * number is more interesting than another when it is far from | |
39 | * other line numbers. For example, we'd rather keep lines | |
40 | * 100,200,300 than 100,101,300. 200 is more interesting than | |
41 | * 101 because 101 can be derived very cheaply from 100, while | |
42 | * 200 is more expensive to derive from 100. | |
43 | * | |
44 | * The function currline() returns the line number of a given | |
45 | * position in the file. As a side effect, it calls add_lnum | |
46 | * to cache the line number. Therefore currline is occasionally | |
47 | * called to make sure we cache line numbers often enough. | |
48 | */ | |
49 | ||
50 | #include "less.h" | |
51 | #include "position.h" | |
52 | ||
53 | /* | |
54 | * Structure to keep track of a line number and the associated file position. | |
55 | * A doubly-linked circular list of line numbers is kept ordered by line number. | |
56 | */ | |
57 | struct linenum | |
58 | { | |
59 | struct linenum *next; /* Link to next in the list */ | |
60 | struct linenum *prev; /* Line to previous in the list */ | |
61 | POSITION pos; /* File position */ | |
62 | POSITION gap; /* Gap between prev and next */ | |
63 | int line; /* Line number */ | |
64 | }; | |
65 | /* | |
66 | * "gap" needs some explanation: the gap of any particular line number | |
67 | * is the distance between the previous one and the next one in the list. | |
68 | * ("Distance" means difference in file position.) In other words, the | |
69 | * gap of a line number is the gap which would be introduced if this | |
70 | * line number were deleted. It is used to decide which one to replace | |
71 | * when we have a new one to insert and the table is full. | |
72 | */ | |
73 | ||
74 | #define NPOOL 50 /* Size of line number pool */ | |
75 | ||
76 | #define LONGTIME (2) /* In seconds */ | |
77 | ||
78 | public int lnloop = 0; /* Are we in the line num loop? */ | |
79 | ||
80 | static struct linenum anchor; /* Anchor of the list */ | |
81 | static struct linenum *freelist; /* Anchor of the unused entries */ | |
82 | static struct linenum pool[NPOOL]; /* The pool itself */ | |
83 | static struct linenum *spare; /* We always keep one spare entry */ | |
84 | ||
85 | extern int linenums; | |
86 | extern int sigs; | |
87 | ||
88 | /* | |
89 | * Initialize the line number structures. | |
90 | */ | |
91 | public void | |
92 | clr_linenum() | |
93 | { | |
94 | register struct linenum *p; | |
95 | ||
96 | /* | |
97 | * Put all the entries on the free list. | |
98 | * Leave one for the "spare". | |
99 | */ | |
100 | for (p = pool; p < &pool[NPOOL-2]; p++) | |
101 | p->next = p+1; | |
102 | pool[NPOOL-2].next = NULL; | |
103 | freelist = pool; | |
104 | ||
105 | spare = &pool[NPOOL-1]; | |
106 | ||
107 | /* | |
108 | * Initialize the anchor. | |
109 | */ | |
110 | anchor.next = anchor.prev = &anchor; | |
111 | anchor.gap = 0; | |
112 | anchor.pos = (POSITION)0; | |
113 | anchor.line = 1; | |
114 | } | |
115 | ||
116 | /* | |
117 | * Calculate the gap for an entry. | |
118 | */ | |
119 | static void | |
120 | calcgap(p) | |
121 | register struct linenum *p; | |
122 | { | |
123 | /* | |
124 | * Don't bother to compute a gap for the anchor. | |
125 | * Also don't compute a gap for the last one in the list. | |
126 | * The gap for that last one should be considered infinite, | |
127 | * but we never look at it anyway. | |
128 | */ | |
129 | if (p == &anchor || p->next == &anchor) | |
130 | return; | |
131 | p->gap = p->next->pos - p->prev->pos; | |
132 | } | |
133 | ||
134 | /* | |
135 | * Add a new line number to the cache. | |
136 | * The specified position (pos) should be the file position of the | |
137 | * FIRST character in the specified line. | |
138 | */ | |
139 | public void | |
140 | add_lnum(line, pos) | |
141 | int line; | |
142 | POSITION pos; | |
143 | { | |
144 | register struct linenum *p; | |
145 | register struct linenum *new; | |
146 | register struct linenum *nextp; | |
147 | register struct linenum *prevp; | |
148 | register POSITION mingap; | |
149 | ||
150 | /* | |
151 | * Find the proper place in the list for the new one. | |
152 | * The entries are sorted by position. | |
153 | */ | |
154 | for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next) | |
155 | if (p->line == line) | |
156 | /* We already have this one. */ | |
157 | return; | |
158 | nextp = p; | |
159 | prevp = p->prev; | |
160 | ||
161 | if (freelist != NULL) | |
162 | { | |
163 | /* | |
164 | * We still have free (unused) entries. | |
165 | * Use one of them. | |
166 | */ | |
167 | new = freelist; | |
168 | freelist = freelist->next; | |
169 | } else | |
170 | { | |
171 | /* | |
172 | * No free entries. | |
173 | * Use the "spare" entry. | |
174 | */ | |
175 | new = spare; | |
176 | spare = NULL; | |
177 | } | |
178 | ||
179 | /* | |
180 | * Fill in the fields of the new entry, | |
181 | * and insert it into the proper place in the list. | |
182 | */ | |
183 | new->next = nextp; | |
184 | new->prev = prevp; | |
185 | new->pos = pos; | |
186 | new->line = line; | |
187 | ||
188 | nextp->prev = new; | |
189 | prevp->next = new; | |
190 | ||
191 | /* | |
192 | * Recalculate gaps for the new entry and the neighboring entries. | |
193 | */ | |
194 | calcgap(new); | |
195 | calcgap(nextp); | |
196 | calcgap(prevp); | |
197 | ||
198 | if (spare == NULL) | |
199 | { | |
200 | /* | |
201 | * We have used the spare entry. | |
202 | * Scan the list to find the one with the smallest | |
203 | * gap, take it out and make it the spare. | |
204 | * We should never remove the last one, so stop when | |
205 | * we get to p->next == &anchor. This also avoids | |
206 | * looking at the gap of the last one, which is | |
207 | * not computed by calcgap. | |
208 | */ | |
209 | mingap = anchor.next->gap; | |
210 | for (p = anchor.next; p->next != &anchor; p = p->next) | |
211 | { | |
212 | if (p->gap <= mingap) | |
213 | { | |
214 | spare = p; | |
215 | mingap = p->gap; | |
216 | } | |
217 | } | |
218 | spare->next->prev = spare->prev; | |
219 | spare->prev->next = spare->next; | |
220 | } | |
221 | } | |
222 | ||
223 | /* | |
224 | * If we get stuck in a long loop trying to figure out the | |
225 | * line number, print a message to tell the user what we're doing. | |
226 | */ | |
227 | static void | |
228 | longloopmessage() | |
229 | { | |
230 | ierror("Calculating line numbers"); | |
231 | /* | |
232 | * Set the lnloop flag here, so if the user interrupts while | |
233 | * we are calculating line numbers, the signal handler will | |
234 | * turn off line numbers (linenums=0). | |
235 | */ | |
236 | lnloop = 1; | |
237 | } | |
238 | ||
239 | /* | |
240 | * Find the line number associated with a given position. | |
241 | * Return 0 if we can't figure it out. | |
242 | */ | |
243 | public int | |
244 | find_linenum(pos) | |
245 | POSITION pos; | |
246 | { | |
247 | register struct linenum *p; | |
248 | register int lno; | |
249 | register int loopcount; | |
250 | POSITION cpos; | |
bfe13c81 | 251 | long startime; |
bfe13c81 KB |
252 | |
253 | if (!linenums) | |
254 | /* | |
255 | * We're not using line numbers. | |
256 | */ | |
257 | return (0); | |
258 | if (pos == NULL_POSITION) | |
259 | /* | |
260 | * Caller doesn't know what he's talking about. | |
261 | */ | |
262 | return (0); | |
263 | if (pos == (POSITION)0) | |
264 | /* | |
265 | * Beginning of file is always line number 1. | |
266 | */ | |
267 | return (1); | |
268 | ||
269 | /* | |
270 | * Find the entry nearest to the position we want. | |
271 | */ | |
272 | for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next) | |
273 | continue; | |
274 | if (p->pos == pos) | |
275 | /* Found it exactly. */ | |
276 | return (p->line); | |
277 | ||
278 | /* | |
279 | * This is the (possibly) time-consuming part. | |
280 | * We start at the line we just found and start | |
281 | * reading the file forward or backward till we | |
282 | * get to the place we want. | |
283 | * | |
284 | * First decide whether we should go forward from the | |
285 | * previous one or backwards from the next one. | |
286 | * The decision is based on which way involves | |
287 | * traversing fewer bytes in the file. | |
288 | */ | |
289 | flush(); | |
bfe13c81 | 290 | startime = get_time(); |
bfe13c81 KB |
291 | if (p == &anchor || pos - p->prev->pos < p->pos - pos) |
292 | { | |
293 | /* | |
294 | * Go forward. | |
295 | */ | |
296 | p = p->prev; | |
297 | if (ch_seek(p->pos)) | |
298 | return (0); | |
299 | loopcount = 0; | |
300 | for (lno = p->line, cpos = p->pos; cpos < pos; lno++) | |
301 | { | |
302 | /* | |
303 | * Allow a signal to abort this loop. | |
304 | */ | |
305 | cpos = forw_raw_line(cpos); | |
306 | if (sigs || cpos == NULL_POSITION) | |
307 | return (0); | |
bfe13c81 KB |
308 | if (loopcount >= 0 && ++loopcount > 100) |
309 | { | |
310 | loopcount = 0; | |
311 | if (get_time() >= startime + LONGTIME) | |
312 | { | |
313 | longloopmessage(); | |
314 | loopcount = -1; | |
315 | } | |
316 | } | |
bfe13c81 KB |
317 | } |
318 | lnloop = 0; | |
319 | /* | |
320 | * If the given position is not at the start of a line, | |
321 | * make sure we return the correct line number. | |
322 | */ | |
323 | if (cpos > pos) | |
324 | lno--; | |
325 | } else | |
326 | { | |
327 | /* | |
328 | * Go backward. | |
329 | */ | |
330 | if (ch_seek(p->pos)) | |
331 | return (0); | |
332 | loopcount = 0; | |
333 | for (lno = p->line, cpos = p->pos; cpos > pos; lno--) | |
334 | { | |
335 | /* | |
336 | * Allow a signal to abort this loop. | |
337 | */ | |
338 | cpos = back_raw_line(cpos); | |
339 | if (sigs || cpos == NULL_POSITION) | |
340 | return (0); | |
bfe13c81 KB |
341 | if (loopcount >= 0 && ++loopcount > 100) |
342 | { | |
343 | loopcount = 0; | |
344 | if (get_time() >= startime + LONGTIME) | |
345 | { | |
346 | longloopmessage(); | |
347 | loopcount = -1; | |
348 | } | |
349 | } | |
bfe13c81 KB |
350 | } |
351 | lnloop = 0; | |
352 | } | |
353 | ||
354 | /* | |
355 | * We might as well cache it. | |
356 | */ | |
357 | add_lnum(lno, cpos); | |
358 | return (lno); | |
359 | } | |
360 | ||
361 | /* | |
362 | * Return the line number of the "current" line. | |
363 | * The argument "where" tells which line is to be considered | |
364 | * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc). | |
365 | */ | |
366 | public int | |
367 | currline(where) | |
368 | int where; | |
369 | { | |
370 | POSITION pos; | |
371 | ||
372 | pos = position(where); | |
373 | if (pos == NULL_POSITION) | |
374 | pos = ch_length(); | |
375 | return (find_linenum(pos)); | |
376 | } | |
377 | ||
378 | #if DEBUG_STUFF | |
379 | debug() | |
380 | { | |
381 | register struct linenum *p; | |
382 | char buf[20]; | |
383 | ||
384 | lower_left(); | |
385 | clear_eol(); | |
386 | for (p = anchor.next; p != &anchor; p = p->next) | |
387 | { | |
388 | sprintf(buf, "%d-%d ", p->line, p->pos); | |
389 | putstr(buf); | |
390 | } | |
391 | putstr("\n"); | |
392 | error("DEBUG"); | |
393 | } | |
394 | #endif /*DEBUG_STUFF*/ |