version 1.7, 2014/04/25 13:38:21 |
version 1.8, 2015/11/05 22:08:44 |
|
|
* |
* |
* For more information, see the README file. |
* For more information, see the README file. |
*/ |
*/ |
|
/* |
|
* Modified for use with illumos. |
|
* Copyright 2014 Garrett D'Amore <garrett@damore.org> |
|
*/ |
|
|
|
|
/* |
/* |
* Routines dealing with the "position" table. |
* Routines dealing with the "position" table. |
* This is a table which tells the position (in the input file) of the |
* This is a table which tells the position (in the input file) of the |
* first char on each currently displayed line. |
* first char on each currently displayed line. |
* |
* |
* {{ The position table is scrolled by moving all the entries. |
* {{ The position table is scrolled by moving all the entries. |
* Would be better to have a circular table |
* Would be better to have a circular table |
* and just change a couple of pointers. }} |
* and just change a couple of pointers. }} |
*/ |
*/ |
|
|
#include "less.h" |
#include "less.h" |
#include "position.h" |
#include "position.h" |
|
|
static POSITION *table = NULL; /* The position table */ |
static off_t *table = NULL; /* The position table */ |
static int table_size; |
static int table_size; |
|
|
extern int sc_width, sc_height; |
extern int sc_width, sc_height; |
|
|
* 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 |
off_t |
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; |
|
|
/* |
/* |
* 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 |
void |
add_forw_pos(pos) |
add_forw_pos(off_t pos) |
POSITION pos; |
|
{ |
{ |
register int i; |
int i; |
|
|
/* |
/* |
* Scroll the position table up. |
* Scroll the position table up. |
|
|
/* |
/* |
* 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 |
void |
add_back_pos(pos) |
add_back_pos(off_t pos) |
POSITION pos; |
|
{ |
{ |
register int i; |
int i; |
|
|
/* |
/* |
* Scroll the position table down. |
* Scroll the position table down. |
|
|
/* |
/* |
* Initialize the position table, done whenever we clear the screen. |
* Initialize the position table, done whenever we clear the screen. |
*/ |
*/ |
public void |
void |
pos_clear() |
pos_clear(void) |
{ |
{ |
register int i; |
int i; |
|
|
for (i = 0; i < sc_height; i++) |
for (i = 0; i < sc_height; i++) |
table[i] = NULL_POSITION; |
table[i] = -1; |
} |
} |
|
|
/* |
/* |
* Allocate or reallocate the position table. |
* Allocate or reallocate the position table. |
*/ |
*/ |
public void |
void |
pos_init() |
pos_init(void) |
{ |
{ |
struct scrpos scrpos; |
struct scrpos scrpos; |
|
|
|
|
* 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. |
*/ |
*/ |
if (table != NULL) |
if (table != NULL) { |
{ |
|
get_scrpos(&scrpos); |
get_scrpos(&scrpos); |
free((char*)table); |
free(table); |
} else |
} else { |
scrpos.pos = NULL_POSITION; |
scrpos.pos = -1; |
table = (POSITION *) ecalloc(sc_height, sizeof(POSITION)); |
} |
|
table = ecalloc(sc_height, sizeof (off_t)); |
table_size = sc_height; |
table_size = sc_height; |
pos_clear(); |
pos_clear(); |
if (scrpos.pos != NULL_POSITION) |
if (scrpos.pos != -1) |
table[scrpos.ln-1] = scrpos.pos; |
table[scrpos.ln-1] = scrpos.pos; |
} |
} |
|
|
|
|
* 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 |
int |
onscreen(pos) |
onscreen(off_t pos) |
POSITION pos; |
|
{ |
{ |
register int i; |
register int i; |
|
|
|
|
/* |
/* |
* See if the entire screen is empty. |
* See if the entire screen is empty. |
*/ |
*/ |
public int |
int |
empty_screen() |
empty_screen(void) |
{ |
{ |
return (empty_lines(0, sc_height-1)); |
return (empty_lines(0, sc_height-1)); |
} |
} |
|
|
public int |
int |
empty_lines(s, e) |
empty_lines(int s, int e) |
int s; |
|
int e; |
|
{ |
{ |
register int i; |
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] != -1 && table[i] != 0) |
return (0); |
return (0); |
return (1); |
return (1); |
} |
} |
|
|
* 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 |
void |
get_scrpos(scrpos) |
get_scrpos(struct scrpos *scrpos) |
struct scrpos *scrpos; |
|
{ |
{ |
register int i; |
int i; |
|
|
/* |
/* |
* 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] != -1) { |
{ |
|
scrpos->ln = i+1; |
scrpos->ln = i+1; |
scrpos->pos = table[i]; |
scrpos->pos = table[i]; |
return; |
return; |
|
|
/* |
/* |
* The screen is empty. |
* The screen is empty. |
*/ |
*/ |
scrpos->pos = NULL_POSITION; |
scrpos->pos = -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 |
int |
adjsline(sline) |
adjsline(int sline) |
int sline; |
|
{ |
{ |
/* |
/* |
* Negative screen line number means |
* Negative screen line number means |