xref: /netbsd-src/external/bsd/less/dist/position.c (revision b95bf550a5b88d2d19011c271f1d77187c3724e1)
1*b95bf550Ssimonb /*	$NetBSD: position.c,v 1.6 2023/10/06 07:33:49 simonb Exp $	*/
220006a0bStron 
320006a0bStron /*
4838f5788Ssimonb  * Copyright (C) 1984-2023  Mark Nudelman
520006a0bStron  *
620006a0bStron  * You may distribute under the terms of either the GNU General Public
720006a0bStron  * License or the Less License, as specified in the README file.
820006a0bStron  *
9ec18bca0Stron  * For more information, see the README file.
1020006a0bStron  */
1120006a0bStron 
1220006a0bStron 
1320006a0bStron /*
1420006a0bStron  * Routines dealing with the "position" table.
1520006a0bStron  * This is a table which tells the position (in the input file) of the
1620006a0bStron  * first char on each currently displayed line.
1720006a0bStron  *
1820006a0bStron  * {{ The position table is scrolled by moving all the entries.
1920006a0bStron  *    Would be better to have a circular table
2020006a0bStron  *    and just change a couple of pointers. }}
2120006a0bStron  */
2220006a0bStron 
2320006a0bStron #include "less.h"
2420006a0bStron #include "position.h"
2520006a0bStron 
2620006a0bStron static POSITION *table = NULL;  /* The position table */
27838f5788Ssimonb static int table_size = 0;
2820006a0bStron 
2920006a0bStron extern int sc_width, sc_height;
30838f5788Ssimonb extern int header_lines;
3120006a0bStron 
3220006a0bStron /*
3320006a0bStron  * Return the starting file position of a line displayed on the screen.
3420006a0bStron  * The line may be specified as a line number relative to the top
3520006a0bStron  * of the screen, but is usually one of these special cases:
3620006a0bStron  *      the top (first) line on the screen
3720006a0bStron  *      the second line on the screen
3820006a0bStron  *      the bottom line on the screen
3920006a0bStron  *      the line after the bottom line on the screen
4020006a0bStron  */
position(int sindex)41838f5788Ssimonb public POSITION position(int sindex)
4220006a0bStron {
43838f5788Ssimonb 	switch (sindex)
4420006a0bStron 	{
4520006a0bStron 	case BOTTOM:
46838f5788Ssimonb 		sindex = sc_height - 2;
4720006a0bStron 		break;
4820006a0bStron 	case BOTTOM_PLUS_ONE:
49838f5788Ssimonb 		sindex = sc_height - 1;
5020006a0bStron 		break;
5120006a0bStron 	case MIDDLE:
52838f5788Ssimonb 		sindex = (sc_height - 1) / 2;
53838f5788Ssimonb 		break;
5420006a0bStron 	}
55838f5788Ssimonb 	return (table[sindex]);
5620006a0bStron }
5720006a0bStron 
5820006a0bStron /*
5920006a0bStron  * Add a new file position to the bottom of the position table.
6020006a0bStron  */
add_forw_pos(POSITION pos)61838f5788Ssimonb public void add_forw_pos(POSITION pos)
6220006a0bStron {
63838f5788Ssimonb 	int i;
6420006a0bStron 
6520006a0bStron 	/*
6620006a0bStron 	 * Scroll the position table up.
6720006a0bStron 	 */
6820006a0bStron 	for (i = 1;  i < sc_height;  i++)
6920006a0bStron 		table[i-1] = table[i];
7020006a0bStron 	table[sc_height - 1] = pos;
7120006a0bStron }
7220006a0bStron 
7320006a0bStron /*
7420006a0bStron  * Add a new file position to the top of the position table.
7520006a0bStron  */
add_back_pos(POSITION pos)76838f5788Ssimonb public void add_back_pos(POSITION pos)
7720006a0bStron {
78838f5788Ssimonb 	int i;
7920006a0bStron 
8020006a0bStron 	/*
8120006a0bStron 	 * Scroll the position table down.
8220006a0bStron 	 */
8320006a0bStron 	for (i = sc_height - 1;  i > 0;  i--)
8420006a0bStron 		table[i] = table[i-1];
8520006a0bStron 	table[0] = pos;
8620006a0bStron }
8720006a0bStron 
8820006a0bStron /*
8920006a0bStron  * Initialize the position table, done whenever we clear the screen.
9020006a0bStron  */
pos_clear(void)91838f5788Ssimonb public void pos_clear(void)
9220006a0bStron {
93838f5788Ssimonb 	int i;
9420006a0bStron 
9520006a0bStron 	for (i = 0;  i < sc_height;  i++)
9620006a0bStron 		table[i] = NULL_POSITION;
9720006a0bStron }
9820006a0bStron 
9920006a0bStron /*
10020006a0bStron  * Allocate or reallocate the position table.
10120006a0bStron  */
pos_init(void)102838f5788Ssimonb public void pos_init(void)
10320006a0bStron {
10420006a0bStron 	struct scrpos scrpos;
10520006a0bStron 
10620006a0bStron 	if (sc_height <= table_size)
10720006a0bStron 		return;
10820006a0bStron 	/*
10920006a0bStron 	 * If we already have a table, remember the first line in it
11020006a0bStron 	 * before we free it, so we can copy that line to the new table.
11120006a0bStron 	 */
11220006a0bStron 	if (table != NULL)
11320006a0bStron 	{
114838f5788Ssimonb 		get_scrpos(&scrpos, TOP);
11520006a0bStron 		free((char*)table);
11620006a0bStron 	} else
11720006a0bStron 		scrpos.pos = NULL_POSITION;
11820006a0bStron 	table = (POSITION *) ecalloc(sc_height, sizeof(POSITION));
11920006a0bStron 	table_size = sc_height;
12020006a0bStron 	pos_clear();
12120006a0bStron 	if (scrpos.pos != NULL_POSITION)
12220006a0bStron 		table[scrpos.ln-1] = scrpos.pos;
12320006a0bStron }
12420006a0bStron 
12520006a0bStron /*
12620006a0bStron  * See if the byte at a specified position is currently on the screen.
12720006a0bStron  * Check the position table to see if the position falls within its range.
12820006a0bStron  * Return the position table entry if found, -1 if not.
12920006a0bStron  */
onscreen(POSITION pos)130838f5788Ssimonb public int onscreen(POSITION pos)
13120006a0bStron {
132838f5788Ssimonb 	int i;
13320006a0bStron 
13420006a0bStron 	if (pos < table[0])
13520006a0bStron 		return (-1);
13620006a0bStron 	for (i = 1;  i < sc_height;  i++)
13720006a0bStron 		if (pos < table[i])
13820006a0bStron 			return (i-1);
13920006a0bStron 	return (-1);
14020006a0bStron }
14120006a0bStron 
14220006a0bStron /*
14320006a0bStron  * See if the entire screen is empty.
14420006a0bStron  */
empty_screen(void)145838f5788Ssimonb public int empty_screen(void)
14620006a0bStron {
14720006a0bStron 	return (empty_lines(0, sc_height-1));
14820006a0bStron }
14920006a0bStron 
empty_lines(int s,int e)150838f5788Ssimonb public int empty_lines(int s, int e)
15120006a0bStron {
152838f5788Ssimonb 	int i;
15320006a0bStron 
15420006a0bStron 	for (i = s;  i <= e;  i++)
155ec18bca0Stron 		if (table[i] != NULL_POSITION && table[i] != 0)
15620006a0bStron 			return (0);
15720006a0bStron 	return (1);
15820006a0bStron }
15920006a0bStron 
16020006a0bStron /*
16120006a0bStron  * Get the current screen position.
16220006a0bStron  * The screen position consists of both a file position and
16320006a0bStron  * a screen line number where the file position is placed on the screen.
16420006a0bStron  * Normally the screen line number is 0, but if we are positioned
16520006a0bStron  * such that the top few lines are empty, we may have to set
16620006a0bStron  * the screen line to a number > 0.
16720006a0bStron  */
get_scrpos(struct scrpos * scrpos,int where)168838f5788Ssimonb public void get_scrpos(struct scrpos *scrpos, int where)
16920006a0bStron {
170838f5788Ssimonb 	int i;
171838f5788Ssimonb 	int dir;
172838f5788Ssimonb 	int last;
173838f5788Ssimonb 
174838f5788Ssimonb 	switch (where)
175838f5788Ssimonb 	{
176838f5788Ssimonb 	case TOP:
177838f5788Ssimonb 		i = 0; dir = +1; last = sc_height-2;
178838f5788Ssimonb 		break;
179838f5788Ssimonb 	case BOTTOM: case BOTTOM_PLUS_ONE:
180838f5788Ssimonb 		i = sc_height-2; dir = -1; last = 0;
181838f5788Ssimonb 		break;
182838f5788Ssimonb 	default:
183838f5788Ssimonb 		i = where;
184838f5788Ssimonb 		if (table[i] == NULL_POSITION) {
185838f5788Ssimonb 			scrpos->pos = NULL_POSITION;
186838f5788Ssimonb 			return;
187838f5788Ssimonb 		}
188838f5788Ssimonb 		/* Values of dir and last don't matter after this. */
189838f5788Ssimonb 		break;
190838f5788Ssimonb 	}
19120006a0bStron 
19220006a0bStron 	/*
19320006a0bStron 	 * Find the first line on the screen which has something on it,
19420006a0bStron 	 * and return the screen line number and the file position.
19520006a0bStron 	 */
196838f5788Ssimonb 	for (;; i += dir)
197838f5788Ssimonb 	{
19820006a0bStron 		if (table[i] != NULL_POSITION)
19920006a0bStron 		{
20020006a0bStron 			scrpos->ln = i+1;
20120006a0bStron 			scrpos->pos = table[i];
20220006a0bStron 			return;
20320006a0bStron 		}
204838f5788Ssimonb 		if (i == last) break;
205838f5788Ssimonb 	}
20620006a0bStron 	/*
20720006a0bStron 	 * The screen is empty.
20820006a0bStron 	 */
20920006a0bStron 	scrpos->pos = NULL_POSITION;
21020006a0bStron }
21120006a0bStron 
21220006a0bStron /*
21320006a0bStron  * Adjust a screen line number to be a simple positive integer
21420006a0bStron  * in the range { 0 .. sc_height-2 }.
21520006a0bStron  * (The bottom line, sc_height-1, is reserved for prompts, etc.)
21620006a0bStron  * The given "sline" may be in the range { 1 .. sc_height-1 }
21720006a0bStron  * to refer to lines relative to the top of the screen (starting from 1),
21820006a0bStron  * or it may be in { -1 .. -(sc_height-1) } to refer to lines
21920006a0bStron  * relative to the bottom of the screen.
22020006a0bStron  */
sindex_from_sline(int sline)221838f5788Ssimonb public int sindex_from_sline(int sline)
22220006a0bStron {
22320006a0bStron 	/*
22420006a0bStron 	 * Negative screen line number means
22520006a0bStron 	 * relative to the bottom of the screen.
22620006a0bStron 	 */
22720006a0bStron 	if (sline < 0)
22820006a0bStron 		sline += sc_height;
22920006a0bStron 	/*
230838f5788Ssimonb 	 * Can't be less than 1 or greater than sc_height.
23120006a0bStron 	 */
23220006a0bStron 	if (sline <= 0)
23320006a0bStron 		sline = 1;
234838f5788Ssimonb 	if (sline > sc_height)
235838f5788Ssimonb 		sline = sc_height;
23620006a0bStron 	/*
23720006a0bStron 	 * Return zero-based line number, not one-based.
23820006a0bStron 	 */
23920006a0bStron 	return (sline-1);
24020006a0bStron }
241