Changeset View
Changeset View
Standalone View
Standalone View
contrib/less/position.c
Show All 30 Lines | |||||
* The line may be specified as a line number relative to the top | * The line may be specified as a line number relative to the top | ||||
* of the screen, but is usually one of these special cases: | * of the screen, but is usually one of these special cases: | ||||
* the top (first) line on the screen | * the top (first) line on the screen | ||||
* the second line on the screen | * the second line on the screen | ||||
* the bottom line on the screen | * the bottom line on the screen | ||||
* the line after the bottom line on the screen | * the line after the bottom line on the screen | ||||
*/ | */ | ||||
public POSITION | public POSITION | ||||
position(where) | position(int where) | ||||
int where; | |||||
{ | { | ||||
switch (where) | switch (where) | ||||
{ | { | ||||
case BOTTOM: | case BOTTOM: | ||||
where = sc_height - 2; | where = sc_height - 2; | ||||
break; | break; | ||||
case BOTTOM_PLUS_ONE: | case BOTTOM_PLUS_ONE: | ||||
where = sc_height - 1; | where = sc_height - 1; | ||||
break; | break; | ||||
case MIDDLE: | case MIDDLE: | ||||
where = (sc_height - 1) / 2; | where = (sc_height - 1) / 2; | ||||
} | } | ||||
return (table[where]); | return (table[where]); | ||||
} | } | ||||
/* | /* | ||||
* Add a new file position to the bottom of the position table. | * Add a new file position to the bottom of the position table. | ||||
*/ | */ | ||||
public void | public void | ||||
add_forw_pos(pos) | add_forw_pos(POSITION pos) | ||||
POSITION pos; | |||||
{ | { | ||||
register int i; | register int i; | ||||
/* | /* | ||||
* Scroll the position table up. | * Scroll the position table up. | ||||
*/ | */ | ||||
for (i = 1; i < sc_height; i++) | for (i = 1; i < sc_height; i++) | ||||
table[i-1] = table[i]; | table[i-1] = table[i]; | ||||
table[sc_height - 1] = pos; | table[sc_height - 1] = pos; | ||||
} | } | ||||
/* | /* | ||||
* Add a new file position to the top of the position table. | * Add a new file position to the top of the position table. | ||||
*/ | */ | ||||
public void | public void | ||||
add_back_pos(pos) | add_back_pos(POSITION pos) | ||||
POSITION pos; | |||||
{ | { | ||||
register int i; | register int i; | ||||
/* | /* | ||||
* Scroll the position table down. | * Scroll the position table down. | ||||
*/ | */ | ||||
for (i = sc_height - 1; i > 0; i--) | for (i = sc_height - 1; i > 0; i--) | ||||
table[i] = table[i-1]; | table[i] = table[i-1]; | ||||
table[0] = pos; | table[0] = pos; | ||||
} | } | ||||
/* | /* | ||||
* Initialize the position table, done whenever we clear the screen. | * Initialize the position table, done whenever we clear the screen. | ||||
*/ | */ | ||||
public void | public void | ||||
pos_clear() | pos_clear(void) | ||||
{ | { | ||||
register int i; | register int i; | ||||
for (i = 0; i < sc_height; i++) | for (i = 0; i < sc_height; i++) | ||||
table[i] = NULL_POSITION; | table[i] = NULL_POSITION; | ||||
} | } | ||||
/* | /* | ||||
* Allocate or reallocate the position table. | * Allocate or reallocate the position table. | ||||
*/ | */ | ||||
public void | public void | ||||
pos_init() | pos_init(void) | ||||
{ | { | ||||
struct scrpos scrpos; | struct scrpos scrpos; | ||||
if (sc_height <= table_size) | if (sc_height <= table_size) | ||||
return; | return; | ||||
/* | /* | ||||
* If we already have a table, remember the first line in it | * If we already have a table, remember the first line in it | ||||
* before we free it, so we can copy that line to the new table. | * before we free it, so we can copy that line to the new table. | ||||
Show All 12 Lines | |||||
} | } | ||||
/* | /* | ||||
* See if the byte at a specified position is currently on the screen. | * See if the byte at a specified position is currently on the screen. | ||||
* Check the position table to see if the position falls within its range. | * Check the position table to see if the position falls within its range. | ||||
* Return the position table entry if found, -1 if not. | * Return the position table entry if found, -1 if not. | ||||
*/ | */ | ||||
public int | public int | ||||
onscreen(pos) | onscreen(POSITION pos) | ||||
POSITION pos; | |||||
{ | { | ||||
register int i; | register int i; | ||||
if (pos < table[0]) | if (pos < table[0]) | ||||
return (-1); | return (-1); | ||||
for (i = 1; i < sc_height; i++) | for (i = 1; i < sc_height; i++) | ||||
if (pos < table[i]) | if (pos < table[i]) | ||||
return (i-1); | return (i-1); | ||||
return (-1); | return (-1); | ||||
} | } | ||||
/* | /* | ||||
* See if the entire screen is empty. | * See if the entire screen is empty. | ||||
*/ | */ | ||||
public int | public int | ||||
empty_screen() | empty_screen(void) | ||||
{ | { | ||||
return (empty_lines(0, sc_height-1)); | return (empty_lines(0, sc_height-1)); | ||||
} | } | ||||
public int | public int | ||||
empty_lines(s, e) | empty_lines(int s, int e) | ||||
int s; | |||||
int e; | |||||
{ | { | ||||
register int i; | register int i; | ||||
for (i = s; i <= e; i++) | for (i = s; i <= e; i++) | ||||
if (table[i] != NULL_POSITION && table[i] != 0) | if (table[i] != NULL_POSITION && table[i] != 0) | ||||
return (0); | return (0); | ||||
return (1); | return (1); | ||||
} | } | ||||
/* | /* | ||||
* Get the current screen position. | * Get the current screen position. | ||||
* The screen position consists of both a file position and | * The screen position consists of both a file position and | ||||
* a screen line number where the file position is placed on the screen. | * a screen line number where the file position is placed on the screen. | ||||
* Normally the screen line number is 0, but if we are positioned | * Normally the screen line number is 0, but if we are positioned | ||||
* such that the top few lines are empty, we may have to set | * such that the top few lines are empty, we may have to set | ||||
* the screen line to a number > 0. | * the screen line to a number > 0. | ||||
*/ | */ | ||||
public void | public void | ||||
get_scrpos(scrpos) | get_scrpos(struct scrpos *scrpos) | ||||
struct scrpos *scrpos; | |||||
{ | { | ||||
register int i; | register int i; | ||||
emaste: extra space? | |||||
/* | /* | ||||
* Find the first line on the screen which has something on it, | * Find the first line on the screen which has something on it, | ||||
* and return the screen line number and the file position. | * and return the screen line number and the file position. | ||||
*/ | */ | ||||
for (i = 0; i < sc_height; i++) | for (i = 0; i < sc_height; i++) | ||||
if (table[i] != NULL_POSITION) | if (table[i] != NULL_POSITION) | ||||
{ | { | ||||
Show All 12 Lines | |||||
* in the range { 0 .. sc_height-2 }. | * in the range { 0 .. sc_height-2 }. | ||||
* (The bottom line, sc_height-1, is reserved for prompts, etc.) | * (The bottom line, sc_height-1, is reserved for prompts, etc.) | ||||
* The given "sline" may be in the range { 1 .. sc_height-1 } | * The given "sline" may be in the range { 1 .. sc_height-1 } | ||||
* to refer to lines relative to the top of the screen (starting from 1), | * to refer to lines relative to the top of the screen (starting from 1), | ||||
* or it may be in { -1 .. -(sc_height-1) } to refer to lines | * or it may be in { -1 .. -(sc_height-1) } to refer to lines | ||||
* relative to the bottom of the screen. | * relative to the bottom of the screen. | ||||
*/ | */ | ||||
public int | public int | ||||
adjsline(sline) | adjsline(int sline) | ||||
int sline; | |||||
{ | { | ||||
/* | /* | ||||
* Negative screen line number means | * Negative screen line number means | ||||
* relative to the bottom of the screen. | * relative to the bottom of the screen. | ||||
*/ | */ | ||||
if (sline < 0) | if (sline < 0) | ||||
sline += sc_height; | sline += sc_height; | ||||
/* | /* | ||||
Show All 11 Lines |
extra space?