Home | History | Annotate | Line # | Download | only in dist
position.c revision 1.5
      1 /*	$NetBSD: position.c,v 1.5 2023/10/06 05:49:49 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1984-2023  Mark Nudelman
      5  *
      6  * You may distribute under the terms of either the GNU General Public
      7  * License or the Less License, as specified in the README file.
      8  *
      9  * For more information, see the README file.
     10  */
     11 
     12 
     13 /*
     14  * Routines dealing with the "position" table.
     15  * This is a table which tells the position (in the input file) of the
     16  * first char on each currently displayed line.
     17  *
     18  * {{ The position table is scrolled by moving all the entries.
     19  *    Would be better to have a circular table
     20  *    and just change a couple of pointers. }}
     21  */
     22 
     23 #include "less.h"
     24 #include "position.h"
     25 
     26 static POSITION *table = NULL;  /* The position table */
     27 static int table_size = 0;
     28 
     29 extern int sc_width, sc_height;
     30 extern int header_lines;
     31 
     32 /*
     33  * Return the starting file position of a line displayed on the screen.
     34  * The line may be specified as a line number relative to the top
     35  * of the screen, but is usually one of these special cases:
     36  *      the top (first) line on the screen
     37  *      the second line on the screen
     38  *      the bottom line on the screen
     39  *      the line after the bottom line on the screen
     40  */
     41 public POSITION position(int sindex)
     42 {
     43 	switch (sindex)
     44 	{
     45 	case BOTTOM:
     46 		sindex = sc_height - 2;
     47 		break;
     48 	case BOTTOM_PLUS_ONE:
     49 		sindex = sc_height - 1;
     50 		break;
     51 	case MIDDLE:
     52 		sindex = (sc_height - 1) / 2;
     53 		break;
     54 	}
     55 	return (table[sindex]);
     56 }
     57 
     58 /*
     59  * Add a new file position to the bottom of the position table.
     60  */
     61 public void add_forw_pos(POSITION pos)
     62 {
     63 	int i;
     64 
     65 	/*
     66 	 * Scroll the position table up.
     67 	 */
     68 	for (i = 1;  i < sc_height;  i++)
     69 		table[i-1] = table[i];
     70 	table[sc_height - 1] = pos;
     71 }
     72 
     73 /*
     74  * Add a new file position to the top of the position table.
     75  */
     76 public void add_back_pos(POSITION pos)
     77 {
     78 	int i;
     79 
     80 	/*
     81 	 * Scroll the position table down.
     82 	 */
     83 	for (i = sc_height - 1;  i > 0;  i--)
     84 		table[i] = table[i-1];
     85 	table[0] = pos;
     86 }
     87 
     88 /*
     89  * Initialize the position table, done whenever we clear the screen.
     90  */
     91 public void pos_clear(void)
     92 {
     93 	int i;
     94 
     95 	for (i = 0;  i < sc_height;  i++)
     96 		table[i] = NULL_POSITION;
     97 }
     98 
     99 /*
    100  * Allocate or reallocate the position table.
    101  */
    102 public void pos_init(void)
    103 {
    104 	struct scrpos scrpos;
    105 	scrpos.pos = scrpos.ln = 0;	/* XXX: GCC */
    106 
    107 	if (sc_height <= table_size)
    108 		return;
    109 	/*
    110 	 * If we already have a table, remember the first line in it
    111 	 * before we free it, so we can copy that line to the new table.
    112 	 */
    113 	if (table != NULL)
    114 	{
    115 		get_scrpos(&scrpos, TOP);
    116 		free((char*)table);
    117 	} else
    118 		scrpos.pos = NULL_POSITION;
    119 	table = (POSITION *) ecalloc(sc_height, sizeof(POSITION));
    120 	table_size = sc_height;
    121 	pos_clear();
    122 	if (scrpos.pos != NULL_POSITION)
    123 		table[scrpos.ln-1] = scrpos.pos;
    124 }
    125 
    126 /*
    127  * See if the byte at a specified position is currently on the screen.
    128  * Check the position table to see if the position falls within its range.
    129  * Return the position table entry if found, -1 if not.
    130  */
    131 public int onscreen(POSITION pos)
    132 {
    133 	int i;
    134 
    135 	if (pos < table[0])
    136 		return (-1);
    137 	for (i = 1;  i < sc_height;  i++)
    138 		if (pos < table[i])
    139 			return (i-1);
    140 	return (-1);
    141 }
    142 
    143 /*
    144  * See if the entire screen is empty.
    145  */
    146 public int empty_screen(void)
    147 {
    148 	return (empty_lines(0, sc_height-1));
    149 }
    150 
    151 public int empty_lines(int s, int e)
    152 {
    153 	int i;
    154 
    155 	for (i = s;  i <= e;  i++)
    156 		if (table[i] != NULL_POSITION && table[i] != 0)
    157 			return (0);
    158 	return (1);
    159 }
    160 
    161 /*
    162  * Get the current screen position.
    163  * The screen position consists of both a file position and
    164  * a screen line number where the file position is placed on the screen.
    165  * Normally the screen line number is 0, but if we are positioned
    166  * such that the top few lines are empty, we may have to set
    167  * the screen line to a number > 0.
    168  */
    169 public void get_scrpos(struct scrpos *scrpos, int where)
    170 {
    171 	int i;
    172 	int dir;
    173 	int last;
    174 
    175 	switch (where)
    176 	{
    177 	case TOP:
    178 		i = 0; dir = +1; last = sc_height-2;
    179 		break;
    180 	case BOTTOM: case BOTTOM_PLUS_ONE:
    181 		i = sc_height-2; dir = -1; last = 0;
    182 		break;
    183 	default:
    184 		i = where;
    185 		if (table[i] == NULL_POSITION) {
    186 			scrpos->pos = NULL_POSITION;
    187 			return;
    188 		}
    189 		/* Values of dir and last don't matter after this. */
    190 		break;
    191 	}
    192 
    193 	/*
    194 	 * Find the first line on the screen which has something on it,
    195 	 * and return the screen line number and the file position.
    196 	 */
    197 	for (;; i += dir)
    198 	{
    199 		if (table[i] != NULL_POSITION)
    200 		{
    201 			scrpos->ln = i+1;
    202 			scrpos->pos = table[i];
    203 			return;
    204 		}
    205 		if (i == last) break;
    206 	}
    207 	/*
    208 	 * The screen is empty.
    209 	 */
    210 	scrpos->pos = NULL_POSITION;
    211 }
    212 
    213 /*
    214  * Adjust a screen line number to be a simple positive integer
    215  * in the range { 0 .. sc_height-2 }.
    216  * (The bottom line, sc_height-1, is reserved for prompts, etc.)
    217  * The given "sline" may be in the range { 1 .. sc_height-1 }
    218  * to refer to lines relative to the top of the screen (starting from 1),
    219  * or it may be in { -1 .. -(sc_height-1) } to refer to lines
    220  * relative to the bottom of the screen.
    221  */
    222 public int sindex_from_sline(int sline)
    223 {
    224 	/*
    225 	 * Negative screen line number means
    226 	 * relative to the bottom of the screen.
    227 	 */
    228 	if (sline < 0)
    229 		sline += sc_height;
    230 	/*
    231 	 * Can't be less than 1 or greater than sc_height.
    232 	 */
    233 	if (sline <= 0)
    234 		sline = 1;
    235 	if (sline > sc_height)
    236 		sline = sc_height;
    237 	/*
    238 	 * Return zero-based line number, not one-based.
    239 	 */
    240 	return (sline-1);
    241 }
    242