/*************************************************************************
 ***                        position.c                   (JJB TEMPLAR) ***
 *** Date modifications begun: 7/8/89.                                 ***
 *** Last modified: 7/8/89.                                            ***
 *************************************************************************/
/*** Routines dealing with the "position" table. This is a table that  ***
 *** tells the pos in the input file of the first char on each         ***
 *** currently displayed line.                                         ***
 *************************************************************************/

#include "less.h"
#include "position.h"

#define NPOS    100             /* Must be greater than sc_height. */
static LONG     table[NPOS];    /* The position table. */

extern int sc_width, sc_height;

LONG    position(where) /*===============================================*/
int where;
{
    switch (where) {
        case BOTTOM:          where = sc_height - 2;     break;
        case BOTTOM_PLUS_ONE: where = sc_height - 1;     break;
    }
    return (table[where]);
}

void    add_forw_pos(pos) /*=============================================*/
LONG    pos;              /* Add a new pos to end of table.              */
{
register int    i;
    for (i = 1;  i < sc_height;  i++) table[i-1] = table[i];
    table[sc_height - 1] = pos;   /* Table moved up one. */
}

void    add_back_pos(pos) /*=============================================*/
LONG    pos;              /* Add a new pos to top of table.              */
{
register int    i;
    for (i = sc_height - 1;  i > 0;  i--) table[i] = table[i-1];
    table[0] = pos;               /* Table moved down one */
}

void    pos_clear() /*===================================================*/
{                   /* Clear table.                                      */
register int    i;
    for (i = 0;  i < sc_height;  i++) table[i] = NULL_POSITION;
}

int     onscreen(pos) /*=================================================*/
LONG    pos;    /* Is pos on screen? If so, return table line #, else -1 */
{
register int    i;
    if (pos < table[0]) return (-1);        /* Before top line */
    for (i = 1;  i < sc_height;  i++)
        if (pos < table[i]) return (i-1);   /* Found line! */
    return (-1);                            /* Not found :-( */
}

void    set_top_pos(pos) /*==============================================*/
LONG    pos;
{
    table[0] = pos;
}
