linenum.c (6f26c71d76bb795b30684affb3b57870a7926b26) | linenum.c (2235c7feac959bcc9ddfd6a2bc6be32102b1f84c) |
---|---|
1/* | 1/* |
2 * Copyright (C) 1984-2020 Mark Nudelman | 2 * Copyright (C) 1984-2021 Mark Nudelman |
3 * 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. 6 * 7 * For more information, see the README file. 8 */ 9 10 --- 24 unchanged lines hidden (view full) --- 35#include "less.h" 36 37/* 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. 40 */ 41struct linenum_info 42{ | 3 * 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. 6 * 7 * For more information, see the README file. 8 */ 9 10 --- 24 unchanged lines hidden (view full) --- 35#include "less.h" 36 37/* 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. 40 */ 41struct linenum_info 42{ |
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 */ | 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 */ |
48}; 49/* 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. 56 */ 57 | 48}; 49/* 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. 56 */ 57 |
58#define NPOOL 200 /* Size of line number pool */ | 58#define NPOOL 200 /* Size of line number pool */ |
59 | 59 |
60#define LONGTIME (2) /* In seconds */ | 60#define LONGTIME (2) /* In seconds */ |
61 | 61 |
62static struct linenum_info anchor; /* Anchor of the list */ 63static struct linenum_info *freelist; /* Anchor of the unused entries */ 64static struct linenum_info pool[NPOOL]; /* The pool itself */ 65static struct linenum_info *spare; /* We always keep one spare entry */ | 62static struct linenum_info anchor; /* Anchor of the list */ 63static struct linenum_info *freelist; /* Anchor of the unused entries */ 64static struct linenum_info pool[NPOOL]; /* The pool itself */ 65static struct linenum_info *spare; /* We always keep one spare entry */ |
66 67extern int linenums; 68extern int sigs; 69extern int sc_height; 70extern int screen_trashed; 71 72/* 73 * Initialize the line number structures. --- 169 unchanged lines hidden (view full) --- 243 244/* 245 * Turn off line numbers because the user has interrupted 246 * a lengthy line number calculation. 247 */ 248 static void 249abort_long(VOID_PARAM) 250{ | 66 67extern int linenums; 68extern int sigs; 69extern int sc_height; 70extern int screen_trashed; 71 72/* 73 * Initialize the line number structures. --- 169 unchanged lines hidden (view full) --- 243 244/* 245 * Turn off line numbers because the user has interrupted 246 * a lengthy line number calculation. 247 */ 248 static void 249abort_long(VOID_PARAM) 250{ |
251 if (loopcount >= 0) 252 return; |
|
251 if (linenums == OPT_ONPLUS) 252 /* 253 * We were displaying line numbers, so need to repaint. 254 */ 255 screen_trashed = 1; 256 linenums = 0; 257 error("Line numbers turned off", NULL_PARG); 258} --- 44 unchanged lines hidden (view full) --- 303 * First decide whether we should go forward from the 304 * previous one or backwards from the next one. 305 * The decision is based on which way involves 306 * traversing fewer bytes in the file. 307 */ 308#if HAVE_TIME 309 startime = get_time(); 310#endif | 253 if (linenums == OPT_ONPLUS) 254 /* 255 * We were displaying line numbers, so need to repaint. 256 */ 257 screen_trashed = 1; 258 linenums = 0; 259 error("Line numbers turned off", NULL_PARG); 260} --- 44 unchanged lines hidden (view full) --- 305 * First decide whether we should go forward from the 306 * previous one or backwards from the next one. 307 * The decision is based on which way involves 308 * traversing fewer bytes in the file. 309 */ 310#if HAVE_TIME 311 startime = get_time(); 312#endif |
313 loopcount = 0; |
|
311 if (p == &anchor || pos - p->prev->pos < p->pos - pos) 312 { 313 /* 314 * Go forward. 315 */ 316 p = p->prev; 317 if (ch_seek(p->pos)) 318 return (0); | 314 if (p == &anchor || pos - p->prev->pos < p->pos - pos) 315 { 316 /* 317 * Go forward. 318 */ 319 p = p->prev; 320 if (ch_seek(p->pos)) 321 return (0); |
319 loopcount = 0; | |
320 for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++) 321 { 322 /* 323 * Allow a signal to abort this loop. 324 */ 325 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL); 326 if (ABORT_SIGS()) { 327 abort_long(); --- 15 unchanged lines hidden (view full) --- 343 linenum--; 344 } else 345 { 346 /* 347 * Go backward. 348 */ 349 if (ch_seek(p->pos)) 350 return (0); | 322 for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++) 323 { 324 /* 325 * Allow a signal to abort this loop. 326 */ 327 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL); 328 if (ABORT_SIGS()) { 329 abort_long(); --- 15 unchanged lines hidden (view full) --- 345 linenum--; 346 } else 347 { 348 /* 349 * Go backward. 350 */ 351 if (ch_seek(p->pos)) 352 return (0); |
351 loopcount = 0; | |
352 for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--) 353 { 354 /* 355 * Allow a signal to abort this loop. 356 */ 357 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL); 358 if (ABORT_SIGS()) { 359 abort_long(); 360 return (0); 361 } 362 if (cpos == NULL_POSITION) 363 return (0); 364 longish(); 365 } 366 /* 367 * We might as well cache it. 368 */ 369 add_lnum(linenum, cpos); 370 } | 353 for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--) 354 { 355 /* 356 * Allow a signal to abort this loop. 357 */ 358 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL); 359 if (ABORT_SIGS()) { 360 abort_long(); 361 return (0); 362 } 363 if (cpos == NULL_POSITION) 364 return (0); 365 longish(); 366 } 367 /* 368 * We might as well cache it. 369 */ 370 add_lnum(linenum, cpos); 371 } |
371 | 372 loopcount = 0; |
372 return (linenum); 373} 374 375/* 376 * Find the position of a given line number. 377 * Return NULL_POSITION if we can't figure it out. 378 */ 379 public POSITION --- 91 unchanged lines hidden --- | 373 return (linenum); 374} 375 376/* 377 * Find the position of a given line number. 378 * Return NULL_POSITION if we can't figure it out. 379 */ 380 public POSITION --- 91 unchanged lines hidden --- |