2012-07-23 21:31:53 +00:00
|
|
|
/*
|
2017-04-29 06:30:21 +00:00
|
|
|
* Copyright (C) 1984-2017 Mark Nudelman
|
2012-07-23 21:31:53 +00:00
|
|
|
*
|
|
|
|
* You may distribute under the terms of either the GNU General Public
|
|
|
|
* License or the Less License, as specified in the README file.
|
|
|
|
*
|
|
|
|
* For more information, see the README file.
|
|
|
|
*/
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Code to handle displaying line numbers.
|
|
|
|
*
|
|
|
|
* Finding the line number of a given file position is rather tricky.
|
|
|
|
* We don't want to just start at the beginning of the file and
|
|
|
|
* count newlines, because that is slow for large files (and also
|
|
|
|
* wouldn't work if we couldn't get to the start of the file; e.g.
|
|
|
|
* if input is a long pipe).
|
|
|
|
*
|
|
|
|
* So we use the function add_lnum to cache line numbers.
|
|
|
|
* We try to be very clever and keep only the more interesting
|
|
|
|
* line numbers when we run out of space in our table. A line
|
|
|
|
* number is more interesting than another when it is far from
|
|
|
|
* other line numbers. For example, we'd rather keep lines
|
|
|
|
* 100,200,300 than 100,101,300. 200 is more interesting than
|
|
|
|
* 101 because 101 can be derived very cheaply from 100, while
|
|
|
|
* 200 is more expensive to derive from 100.
|
|
|
|
*
|
|
|
|
* The function currline() returns the line number of a given
|
|
|
|
* position in the file. As a side effect, it calls add_lnum
|
|
|
|
* to cache the line number. Therefore currline is occasionally
|
|
|
|
* called to make sure we cache line numbers often enough.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "less.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Structure to keep track of a line number and the associated file position.
|
|
|
|
* A doubly-linked circular list of line numbers is kept ordered by line number.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
struct linenum_info
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2004-04-17 07:16:34 +00:00
|
|
|
struct linenum_info *next; /* Link to next in the list */
|
|
|
|
struct linenum_info *prev; /* Line to previous in the list */
|
2000-05-22 09:53:22 +00:00
|
|
|
POSITION pos; /* File position */
|
|
|
|
POSITION gap; /* Gap between prev and next */
|
2004-04-17 07:16:34 +00:00
|
|
|
LINENUM line; /* Line number */
|
2000-05-22 09:53:22 +00:00
|
|
|
};
|
|
|
|
/*
|
|
|
|
* "gap" needs some explanation: the gap of any particular line number
|
|
|
|
* is the distance between the previous one and the next one in the list.
|
|
|
|
* ("Distance" means difference in file position.) In other words, the
|
|
|
|
* gap of a line number is the gap which would be introduced if this
|
|
|
|
* line number were deleted. It is used to decide which one to replace
|
|
|
|
* when we have a new one to insert and the table is full.
|
|
|
|
*/
|
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
#define NPOOL 200 /* Size of line number pool */
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
#define LONGTIME (2) /* In seconds */
|
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
static struct linenum_info anchor; /* Anchor of the list */
|
|
|
|
static struct linenum_info *freelist; /* Anchor of the unused entries */
|
|
|
|
static struct linenum_info pool[NPOOL]; /* The pool itself */
|
|
|
|
static struct linenum_info *spare; /* We always keep one spare entry */
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
extern int linenums;
|
|
|
|
extern int sigs;
|
|
|
|
extern int sc_height;
|
2009-05-08 23:45:27 +00:00
|
|
|
extern int screen_trashed;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the line number structures.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
clr_linenum()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
struct linenum_info *p;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Put all the entries on the free list.
|
|
|
|
* Leave one for the "spare".
|
|
|
|
*/
|
|
|
|
for (p = pool; p < &pool[NPOOL-2]; p++)
|
|
|
|
p->next = p+1;
|
|
|
|
pool[NPOOL-2].next = NULL;
|
|
|
|
freelist = pool;
|
|
|
|
|
|
|
|
spare = &pool[NPOOL-1];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the anchor.
|
|
|
|
*/
|
|
|
|
anchor.next = anchor.prev = &anchor;
|
|
|
|
anchor.gap = 0;
|
|
|
|
anchor.pos = (POSITION)0;
|
|
|
|
anchor.line = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the gap for an entry.
|
|
|
|
*/
|
|
|
|
static void
|
2017-04-25 03:42:16 +00:00
|
|
|
calcgap(p)
|
2017-04-29 06:30:21 +00:00
|
|
|
struct linenum_info *p;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Don't bother to compute a gap for the anchor.
|
|
|
|
* Also don't compute a gap for the last one in the list.
|
|
|
|
* The gap for that last one should be considered infinite,
|
|
|
|
* but we never look at it anyway.
|
|
|
|
*/
|
|
|
|
if (p == &anchor || p->next == &anchor)
|
|
|
|
return;
|
|
|
|
p->gap = p->next->pos - p->prev->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new line number to the cache.
|
|
|
|
* The specified position (pos) should be the file position of the
|
|
|
|
* FIRST character in the specified line.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
add_lnum(linenum, pos)
|
|
|
|
LINENUM linenum;
|
|
|
|
POSITION pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
struct linenum_info *p;
|
|
|
|
struct linenum_info *new;
|
|
|
|
struct linenum_info *nextp;
|
|
|
|
struct linenum_info *prevp;
|
|
|
|
POSITION mingap;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the proper place in the list for the new one.
|
|
|
|
* The entries are sorted by position.
|
|
|
|
*/
|
|
|
|
for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
|
2004-04-17 07:16:34 +00:00
|
|
|
if (p->line == linenum)
|
2000-05-22 09:53:22 +00:00
|
|
|
/* We already have this one. */
|
|
|
|
return;
|
|
|
|
nextp = p;
|
|
|
|
prevp = p->prev;
|
|
|
|
|
|
|
|
if (freelist != NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We still have free (unused) entries.
|
|
|
|
* Use one of them.
|
|
|
|
*/
|
|
|
|
new = freelist;
|
|
|
|
freelist = freelist->next;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* No free entries.
|
|
|
|
* Use the "spare" entry.
|
|
|
|
*/
|
|
|
|
new = spare;
|
|
|
|
spare = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in the fields of the new entry,
|
|
|
|
* and insert it into the proper place in the list.
|
|
|
|
*/
|
|
|
|
new->next = nextp;
|
|
|
|
new->prev = prevp;
|
|
|
|
new->pos = pos;
|
2004-04-17 07:16:34 +00:00
|
|
|
new->line = linenum;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
nextp->prev = new;
|
|
|
|
prevp->next = new;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recalculate gaps for the new entry and the neighboring entries.
|
|
|
|
*/
|
|
|
|
calcgap(new);
|
|
|
|
calcgap(nextp);
|
|
|
|
calcgap(prevp);
|
|
|
|
|
|
|
|
if (spare == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We have used the spare entry.
|
|
|
|
* Scan the list to find the one with the smallest
|
|
|
|
* gap, take it out and make it the spare.
|
|
|
|
* We should never remove the last one, so stop when
|
|
|
|
* we get to p->next == &anchor. This also avoids
|
|
|
|
* looking at the gap of the last one, which is
|
|
|
|
* not computed by calcgap.
|
|
|
|
*/
|
|
|
|
mingap = anchor.next->gap;
|
|
|
|
for (p = anchor.next; p->next != &anchor; p = p->next)
|
|
|
|
{
|
|
|
|
if (p->gap <= mingap)
|
|
|
|
{
|
|
|
|
spare = p;
|
|
|
|
mingap = p->gap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spare->next->prev = spare->prev;
|
|
|
|
spare->prev->next = spare->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we get stuck in a long loop trying to figure out the
|
|
|
|
* line number, print a message to tell the user what we're doing.
|
|
|
|
*/
|
|
|
|
static void
|
2017-04-25 03:42:16 +00:00
|
|
|
longloopmessage()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
ierror("Calculating line numbers", NULL_PARG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int loopcount;
|
|
|
|
#if HAVE_TIME
|
2016-01-04 00:22:34 +00:00
|
|
|
static time_type startime;
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
2017-04-25 03:42:16 +00:00
|
|
|
longish()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
#if HAVE_TIME
|
|
|
|
if (loopcount >= 0 && ++loopcount > 100)
|
|
|
|
{
|
|
|
|
loopcount = 0;
|
|
|
|
if (get_time() >= startime + LONGTIME)
|
|
|
|
{
|
|
|
|
longloopmessage();
|
|
|
|
loopcount = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (loopcount >= 0 && ++loopcount > LONGLOOP)
|
|
|
|
{
|
|
|
|
longloopmessage();
|
|
|
|
loopcount = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
/*
|
|
|
|
* Turn off line numbers because the user has interrupted
|
|
|
|
* a lengthy line number calculation.
|
|
|
|
*/
|
|
|
|
static void
|
2017-04-25 03:42:16 +00:00
|
|
|
abort_long()
|
2009-05-08 23:45:27 +00:00
|
|
|
{
|
|
|
|
if (linenums == OPT_ONPLUS)
|
|
|
|
/*
|
|
|
|
* We were displaying line numbers, so need to repaint.
|
|
|
|
*/
|
|
|
|
screen_trashed = 1;
|
|
|
|
linenums = 0;
|
|
|
|
error("Line numbers turned off", NULL_PARG);
|
|
|
|
}
|
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Find the line number associated with a given position.
|
|
|
|
* Return 0 if we can't figure it out.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
public LINENUM
|
2017-04-25 03:42:16 +00:00
|
|
|
find_linenum(pos)
|
|
|
|
POSITION pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
struct linenum_info *p;
|
|
|
|
LINENUM linenum;
|
2000-05-22 09:53:22 +00:00
|
|
|
POSITION cpos;
|
|
|
|
|
|
|
|
if (!linenums)
|
|
|
|
/*
|
|
|
|
* We're not using line numbers.
|
|
|
|
*/
|
|
|
|
return (0);
|
|
|
|
if (pos == NULL_POSITION)
|
|
|
|
/*
|
|
|
|
* Caller doesn't know what he's talking about.
|
|
|
|
*/
|
|
|
|
return (0);
|
|
|
|
if (pos <= ch_zero())
|
|
|
|
/*
|
|
|
|
* Beginning of file is always line number 1.
|
|
|
|
*/
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the entry nearest to the position we want.
|
|
|
|
*/
|
|
|
|
for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
|
|
|
|
continue;
|
|
|
|
if (p->pos == pos)
|
|
|
|
/* Found it exactly. */
|
|
|
|
return (p->line);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the (possibly) time-consuming part.
|
|
|
|
* We start at the line we just found and start
|
|
|
|
* reading the file forward or backward till we
|
|
|
|
* get to the place we want.
|
|
|
|
*
|
|
|
|
* First decide whether we should go forward from the
|
|
|
|
* previous one or backwards from the next one.
|
|
|
|
* The decision is based on which way involves
|
|
|
|
* traversing fewer bytes in the file.
|
|
|
|
*/
|
|
|
|
#if HAVE_TIME
|
|
|
|
startime = get_time();
|
|
|
|
#endif
|
|
|
|
if (p == &anchor || pos - p->prev->pos < p->pos - pos)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Go forward.
|
|
|
|
*/
|
|
|
|
p = p->prev;
|
|
|
|
if (ch_seek(p->pos))
|
|
|
|
return (0);
|
|
|
|
loopcount = 0;
|
2004-04-17 07:16:34 +00:00
|
|
|
for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Allow a signal to abort this loop.
|
|
|
|
*/
|
2007-06-04 01:42:54 +00:00
|
|
|
cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
|
2009-05-08 23:45:27 +00:00
|
|
|
if (ABORT_SIGS()) {
|
|
|
|
abort_long();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (cpos == NULL_POSITION)
|
2000-05-22 09:53:22 +00:00
|
|
|
return (0);
|
|
|
|
longish();
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We might as well cache it.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
add_lnum(linenum, cpos);
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* If the given position is not at the start of a line,
|
|
|
|
* make sure we return the correct line number.
|
|
|
|
*/
|
|
|
|
if (cpos > pos)
|
2004-04-17 07:16:34 +00:00
|
|
|
linenum--;
|
2000-05-22 09:53:22 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Go backward.
|
|
|
|
*/
|
|
|
|
if (ch_seek(p->pos))
|
|
|
|
return (0);
|
|
|
|
loopcount = 0;
|
2004-04-17 07:16:34 +00:00
|
|
|
for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Allow a signal to abort this loop.
|
|
|
|
*/
|
2007-06-04 01:42:54 +00:00
|
|
|
cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
|
2009-05-08 23:45:27 +00:00
|
|
|
if (ABORT_SIGS()) {
|
|
|
|
abort_long();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (cpos == NULL_POSITION)
|
2000-05-22 09:53:22 +00:00
|
|
|
return (0);
|
|
|
|
longish();
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We might as well cache it.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
add_lnum(linenum, cpos);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
return (linenum);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the position of a given line number.
|
|
|
|
* Return NULL_POSITION if we can't figure it out.
|
|
|
|
*/
|
|
|
|
public POSITION
|
2017-04-25 03:42:16 +00:00
|
|
|
find_pos(linenum)
|
|
|
|
LINENUM linenum;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
struct linenum_info *p;
|
2000-05-22 09:53:22 +00:00
|
|
|
POSITION cpos;
|
2004-04-17 07:16:34 +00:00
|
|
|
LINENUM clinenum;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
if (linenum <= 1)
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Line number 1 is beginning of file.
|
|
|
|
*/
|
|
|
|
return (ch_zero());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the entry nearest to the line number we want.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next)
|
2000-05-22 09:53:22 +00:00
|
|
|
continue;
|
2004-04-17 07:16:34 +00:00
|
|
|
if (p->line == linenum)
|
2000-05-22 09:53:22 +00:00
|
|
|
/* Found it exactly. */
|
|
|
|
return (p->pos);
|
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
if (p == &anchor || linenum - p->prev->line < p->line - linenum)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Go forward.
|
|
|
|
*/
|
|
|
|
p = p->prev;
|
|
|
|
if (ch_seek(p->pos))
|
|
|
|
return (NULL_POSITION);
|
2004-04-17 07:16:34 +00:00
|
|
|
for (clinenum = p->line, cpos = p->pos; clinenum < linenum; clinenum++)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Allow a signal to abort this loop.
|
|
|
|
*/
|
2007-06-04 01:42:54 +00:00
|
|
|
cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
|
2009-05-08 23:45:27 +00:00
|
|
|
if (ABORT_SIGS())
|
|
|
|
return (NULL_POSITION);
|
|
|
|
if (cpos == NULL_POSITION)
|
2000-05-22 09:53:22 +00:00
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Go backward.
|
|
|
|
*/
|
|
|
|
if (ch_seek(p->pos))
|
|
|
|
return (NULL_POSITION);
|
2004-04-17 07:16:34 +00:00
|
|
|
for (clinenum = p->line, cpos = p->pos; clinenum > linenum; clinenum--)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Allow a signal to abort this loop.
|
|
|
|
*/
|
2007-06-04 01:42:54 +00:00
|
|
|
cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
|
2009-05-08 23:45:27 +00:00
|
|
|
if (ABORT_SIGS())
|
|
|
|
return (NULL_POSITION);
|
|
|
|
if (cpos == NULL_POSITION)
|
2000-05-22 09:53:22 +00:00
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We might as well cache it.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
add_lnum(clinenum, cpos);
|
2000-05-22 09:53:22 +00:00
|
|
|
return (cpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the line number of the "current" line.
|
|
|
|
* The argument "where" tells which line is to be considered
|
|
|
|
* the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
public LINENUM
|
2017-04-25 03:42:16 +00:00
|
|
|
currline(where)
|
|
|
|
int where;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
POSITION pos;
|
|
|
|
POSITION len;
|
2004-04-17 07:16:34 +00:00
|
|
|
LINENUM linenum;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
pos = position(where);
|
|
|
|
len = ch_length();
|
|
|
|
while (pos == NULL_POSITION && where >= 0 && where < sc_height)
|
|
|
|
pos = position(++where);
|
|
|
|
if (pos == NULL_POSITION)
|
|
|
|
pos = len;
|
2004-04-17 07:16:34 +00:00
|
|
|
linenum = find_linenum(pos);
|
2000-05-22 09:53:22 +00:00
|
|
|
if (pos == len)
|
2004-04-17 07:16:34 +00:00
|
|
|
linenum--;
|
|
|
|
return (linenum);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|