Changeset View
Changeset View
Standalone View
Standalone View
contrib/less/linenum.c
Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | |||||
extern int sigs; | extern int sigs; | ||||
extern int sc_height; | extern int sc_height; | ||||
extern int screen_trashed; | extern int screen_trashed; | ||||
/* | /* | ||||
* Initialize the line number structures. | * Initialize the line number structures. | ||||
*/ | */ | ||||
public void | public void | ||||
clr_linenum() | clr_linenum(void) | ||||
{ | { | ||||
register struct linenum_info *p; | struct linenum_info *p; | ||||
/* | /* | ||||
* Put all the entries on the free list. | * Put all the entries on the free list. | ||||
* Leave one for the "spare". | * Leave one for the "spare". | ||||
*/ | */ | ||||
for (p = pool; p < &pool[NPOOL-2]; p++) | for (p = pool; p < &pool[NPOOL-2]; p++) | ||||
p->next = p+1; | p->next = p+1; | ||||
pool[NPOOL-2].next = NULL; | pool[NPOOL-2].next = NULL; | ||||
Show All 9 Lines | clr_linenum(void) | ||||
anchor.pos = (POSITION)0; | anchor.pos = (POSITION)0; | ||||
anchor.line = 1; | anchor.line = 1; | ||||
} | } | ||||
/* | /* | ||||
* Calculate the gap for an entry. | * Calculate the gap for an entry. | ||||
*/ | */ | ||||
static void | static void | ||||
calcgap(p) | calcgap(struct linenum_info *p) | ||||
register struct linenum_info *p; | |||||
{ | { | ||||
/* | /* | ||||
* Don't bother to compute a gap for the anchor. | * Don't bother to compute a gap for the anchor. | ||||
* Also don't compute a gap for the last one in the list. | * Also don't compute a gap for the last one in the list. | ||||
* The gap for that last one should be considered infinite, | * The gap for that last one should be considered infinite, | ||||
* but we never look at it anyway. | * but we never look at it anyway. | ||||
*/ | */ | ||||
if (p == &anchor || p->next == &anchor) | if (p == &anchor || p->next == &anchor) | ||||
return; | return; | ||||
p->gap = p->next->pos - p->prev->pos; | p->gap = p->next->pos - p->prev->pos; | ||||
} | } | ||||
/* | /* | ||||
* Add a new line number to the cache. | * Add a new line number to the cache. | ||||
* The specified position (pos) should be the file position of the | * The specified position (pos) should be the file position of the | ||||
* FIRST character in the specified line. | * FIRST character in the specified line. | ||||
*/ | */ | ||||
public void | public void | ||||
add_lnum(linenum, pos) | add_lnum(LINENUM linenum, POSITION pos) | ||||
LINENUM linenum; | |||||
POSITION pos; | |||||
{ | { | ||||
register struct linenum_info *p; | struct linenum_info *p; | ||||
register struct linenum_info *new; | struct linenum_info *new; | ||||
register struct linenum_info *nextp; | struct linenum_info *nextp; | ||||
register struct linenum_info *prevp; | struct linenum_info *prevp; | ||||
register POSITION mingap; | POSITION mingap; | ||||
/* | /* | ||||
* Find the proper place in the list for the new one. | * Find the proper place in the list for the new one. | ||||
* The entries are sorted by position. | * The entries are sorted by position. | ||||
*/ | */ | ||||
for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next) | for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next) | ||||
if (p->line == linenum) | if (p->line == linenum) | ||||
/* We already have this one. */ | /* We already have this one. */ | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | add_lnum(LINENUM linenum, POSITION pos) | ||||
} | } | ||||
} | } | ||||
/* | /* | ||||
* If we get stuck in a long loop trying to figure out the | * 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. | * line number, print a message to tell the user what we're doing. | ||||
*/ | */ | ||||
static void | static void | ||||
longloopmessage() | longloopmessage(void) | ||||
{ | { | ||||
ierror("Calculating line numbers", NULL_PARG); | ierror("Calculating line numbers", NULL_PARG); | ||||
} | } | ||||
static int loopcount; | static int loopcount; | ||||
#if HAVE_TIME | #if HAVE_TIME | ||||
static time_type startime; | static time_type startime; | ||||
#endif | #endif | ||||
static void | static void | ||||
longish() | longish(void) | ||||
{ | { | ||||
#if HAVE_TIME | #if HAVE_TIME | ||||
if (loopcount >= 0 && ++loopcount > 100) | if (loopcount >= 0 && ++loopcount > 100) | ||||
{ | { | ||||
loopcount = 0; | loopcount = 0; | ||||
if (get_time() >= startime + LONGTIME) | if (get_time() >= startime + LONGTIME) | ||||
{ | { | ||||
longloopmessage(); | longloopmessage(); | ||||
Show All 9 Lines | |||||
#endif | #endif | ||||
} | } | ||||
/* | /* | ||||
* Turn off line numbers because the user has interrupted | * Turn off line numbers because the user has interrupted | ||||
* a lengthy line number calculation. | * a lengthy line number calculation. | ||||
*/ | */ | ||||
static void | static void | ||||
abort_long() | abort_long(void) | ||||
{ | { | ||||
if (linenums == OPT_ONPLUS) | if (linenums == OPT_ONPLUS) | ||||
/* | /* | ||||
* We were displaying line numbers, so need to repaint. | * We were displaying line numbers, so need to repaint. | ||||
*/ | */ | ||||
screen_trashed = 1; | screen_trashed = 1; | ||||
linenums = 0; | linenums = 0; | ||||
error("Line numbers turned off", NULL_PARG); | error("Line numbers turned off", NULL_PARG); | ||||
} | } | ||||
/* | /* | ||||
* Find the line number associated with a given position. | * Find the line number associated with a given position. | ||||
* Return 0 if we can't figure it out. | * Return 0 if we can't figure it out. | ||||
*/ | */ | ||||
public LINENUM | public LINENUM | ||||
find_linenum(pos) | find_linenum(POSITION pos) | ||||
POSITION pos; | |||||
{ | { | ||||
register struct linenum_info *p; | struct linenum_info *p; | ||||
register LINENUM linenum; | LINENUM linenum; | ||||
POSITION cpos; | POSITION cpos; | ||||
if (!linenums) | if (!linenums) | ||||
/* | /* | ||||
* We're not using line numbers. | * We're not using line numbers. | ||||
*/ | */ | ||||
return (0); | return (0); | ||||
if (pos == NULL_POSITION) | if (pos == NULL_POSITION) | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | #endif | ||||
return (linenum); | return (linenum); | ||||
} | } | ||||
/* | /* | ||||
* Find the position of a given line number. | * Find the position of a given line number. | ||||
* Return NULL_POSITION if we can't figure it out. | * Return NULL_POSITION if we can't figure it out. | ||||
*/ | */ | ||||
public POSITION | public POSITION | ||||
find_pos(linenum) | find_pos(LINENUM linenum) | ||||
LINENUM linenum; | |||||
{ | { | ||||
register struct linenum_info *p; | struct linenum_info *p; | ||||
POSITION cpos; | POSITION cpos; | ||||
LINENUM clinenum; | LINENUM clinenum; | ||||
if (linenum <= 1) | if (linenum <= 1) | ||||
/* | /* | ||||
* Line number 1 is beginning of file. | * Line number 1 is beginning of file. | ||||
*/ | */ | ||||
return (ch_zero()); | return (ch_zero()); | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
/* | /* | ||||
* Return the line number of the "current" line. | * Return the line number of the "current" line. | ||||
* The argument "where" tells which line is to be considered | * The argument "where" tells which line is to be considered | ||||
* the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc). | * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc). | ||||
*/ | */ | ||||
public LINENUM | public LINENUM | ||||
currline(where) | currline(int where) | ||||
int where; | |||||
{ | { | ||||
POSITION pos; | POSITION pos; | ||||
POSITION len; | POSITION len; | ||||
LINENUM linenum; | LINENUM linenum; | ||||
pos = position(where); | pos = position(where); | ||||
len = ch_length(); | len = ch_length(); | ||||
while (pos == NULL_POSITION && where >= 0 && where < sc_height) | while (pos == NULL_POSITION && where >= 0 && where < sc_height) | ||||
pos = position(++where); | pos = position(++where); | ||||
if (pos == NULL_POSITION) | if (pos == NULL_POSITION) | ||||
pos = len; | pos = len; | ||||
linenum = find_linenum(pos); | linenum = find_linenum(pos); | ||||
if (pos == len) | if (pos == len) | ||||
linenum--; | linenum--; | ||||
return (linenum); | return (linenum); | ||||
} | } |