xref: /freebsd/contrib/less/position.c (revision 252d6dde57d5dd0184929d1f8fb65e7713f51c6d)
1 /*
2  * Copyright (C) 1984-2025  Mark Nudelman
3  *
4  * You may distribute under the terms of either the GNU General Public
5  * License or the Less License, as specified in the README file.
6  *
7  * For more information, see the README file.
8  */
9 
10 
11 /*
12  * Routines dealing with the "position" table.
13  * This is a table which tells the position (in the input file) of the
14  * first char on each currently displayed line.
15  *
16  * {{ The position table is scrolled by moving all the entries.
17  *    Would be better to have a circular table
18  *    and just change a couple of pointers. }}
19  */
20 
21 #include "less.h"
22 #include "position.h"
23 
24 static POSITION *table = NULL;  /* The position table */
25 static int table_size = 0;
26 
27 extern int sc_width, sc_height;
28 extern int hshift;
29 extern int shell_lines;
30 
31 /*
32  * Return the starting file position of a line displayed on the screen.
33  * The line may be specified as a line number relative to the top
34  * of the screen, but is usually one of these special cases:
35  *      the top (first) line on the screen
36  *      the second line on the screen
37  *      the bottom line on the screen
38  *      the line after the bottom line on the screen
39  */
position(int sindex)40 public POSITION position(int sindex)
41 {
42 	switch (sindex)
43 	{
44 	case BOTTOM:
45 		sindex = sc_height - 2;
46 		break;
47 	case BOTTOM_PLUS_ONE:
48 		sindex = sc_height - 1;
49 		break;
50 	case BOTTOM_OFFSET:
51 		sindex = sc_height - shell_lines;
52 		break;
53 	case MIDDLE:
54 		sindex = (sc_height - 1) / 2;
55 		break;
56 	}
57 	return (table[sindex]);
58 }
59 
60 /*
61  * Add a new file position to the bottom of the position table.
62  */
add_forw_pos(POSITION pos,lbool no_scroll)63 public void add_forw_pos(POSITION pos, lbool no_scroll)
64 {
65 	int i;
66 
67 	/*
68 	 * Scroll the position table up.
69 	 */
70 	if (!no_scroll)
71 	{
72 		for (i = 1;  i < sc_height;  i++)
73 			table[i-1] = table[i];
74 	}
75 	table[sc_height - 1] = pos;
76 }
77 
78 /*
79  * Add a new file position to the top of the position table.
80  */
add_back_pos(POSITION pos)81 public void add_back_pos(POSITION pos)
82 {
83 	int i;
84 
85 	/*
86 	 * Scroll the position table down.
87 	 */
88 	for (i = sc_height - 1;  i > 0;  i--)
89 		table[i] = table[i-1];
90 	table[0] = pos;
91 }
92 
93 /*
94  * Initialize the position table, done whenever we clear the screen.
95  */
pos_clear(void)96 public void pos_clear(void)
97 {
98 	int i;
99 
100 	for (i = 0;  i < sc_height;  i++)
101 		table[i] = NULL_POSITION;
102 }
103 
104 /*
105  * Allocate or reallocate the position table.
106  */
pos_init(void)107 public void pos_init(void)
108 {
109 	struct scrpos scrpos;
110 
111 	if (sc_height <= table_size)
112 		return;
113 	/*
114 	 * If we already have a table, remember the first line in it
115 	 * before we free it, so we can copy that line to the new table.
116 	 */
117 	if (table != NULL)
118 	{
119 		get_scrpos(&scrpos, TOP);
120 		free((char*)table);
121 	} else
122 		scrpos.pos = NULL_POSITION;
123 	table = (POSITION *) ecalloc((size_t) sc_height, sizeof(POSITION)); /*{{type-issue}}*/
124 	table_size = sc_height;
125 	pos_clear();
126 	if (scrpos.pos != NULL_POSITION)
127 		table[scrpos.ln-1] = scrpos.pos;
128 }
129 
130 /*
131  * See if the byte at a specified position is currently on the screen.
132  * Check the position table to see if the position falls within its range.
133  * Return the position table entry if found, -1 if not.
134  */
onscreen(POSITION pos)135 public int onscreen(POSITION pos)
136 {
137 	int i;
138 
139 	if (pos < table[0])
140 		return (-1);
141 	for (i = 1;  i < sc_height;  i++)
142 		if (pos < table[i])
143 			return (i-1);
144 	return (-1);
145 }
146 
147 /*
148  * See if the entire screen is empty.
149  */
empty_screen(void)150 public int empty_screen(void)
151 {
152 	return (empty_lines(0, sc_height-1));
153 }
154 
empty_lines(int s,int e)155 public int empty_lines(int s, int e)
156 {
157 	int i;
158 
159 	for (i = s;  i <= e;  i++)
160 		if (table[i] != NULL_POSITION && table[i] != 0)
161 			return (0);
162 	return (1);
163 }
164 
165 /*
166  * Get the current screen position.
167  * The screen position consists of both a file position and
168  * a screen line number where the file position is placed on the screen.
169  * Normally the screen line number is 0, but if we are positioned
170  * such that the top few lines are empty, we may have to set
171  * the screen line to a number > 0.
172  */
get_scrpos(struct scrpos * scrpos,int where)173 public void get_scrpos(struct scrpos *scrpos, int where)
174 {
175 	int i;
176 	int dir;
177 	int last;
178 
179 	switch (where)
180 	{
181 	case TOP:
182 		i = 0; dir = +1; last = sc_height-2;
183 		break;
184 	case BOTTOM: case BOTTOM_PLUS_ONE:
185 		i = sc_height-2; dir = -1; last = 0;
186 		break;
187 	default:
188 		i = where;
189 		if (table[i] == NULL_POSITION) {
190 			scrpos->pos = NULL_POSITION;
191 			return;
192 		}
193 		/* Values of dir and last don't matter after this. */
194 		break;
195 	}
196 
197 	/*
198 	 * Find the first line on the screen which has something on it,
199 	 * and return the screen line number and the file position.
200 	 */
201 	for (;; i += dir)
202 	{
203 		if (table[i] != NULL_POSITION)
204 		{
205 			scrpos->ln = i+1;
206 			scrpos->pos = table[i];
207 			return;
208 		}
209 		if (i == last) break;
210 	}
211 	/*
212 	 * The screen is empty.
213 	 */
214 	scrpos->pos = NULL_POSITION;
215 }
216 
217 /*
218  * Adjust a screen line number to be a simple positive integer
219  * in the range { 0 .. sc_height-2 }.
220  * (The bottom line, sc_height-1, is reserved for prompts, etc.)
221  * The given "sline" may be in the range { 1 .. sc_height-1 }
222  * to refer to lines relative to the top of the screen (starting from 1),
223  * or it may be in { -1 .. -(sc_height-1) } to refer to lines
224  * relative to the bottom of the screen.
225  */
sindex_from_sline(int sline)226 public int sindex_from_sline(int sline)
227 {
228 	/*
229 	 * Negative screen line number means
230 	 * relative to the bottom of the screen.
231 	 */
232 	if (sline < 0)
233 		sline += sc_height;
234 	/*
235 	 * Can't be less than 1 or greater than sc_height.
236 	 */
237 	if (sline <= 0)
238 		sline = 1;
239 	if (sline > sc_height)
240 		sline = sc_height;
241 	/*
242 	 * Return zero-based line number, not one-based.
243 	 */
244 	return (sline-1);
245 }
246 
247 /*
248  * Given a line that starts at linepos,
249  * and the character at byte offset choff into that line,
250  * return the number of characters (not bytes) between the
251  * beginning of the line and the first byte of the choff character.
252  */
pos_shift(POSITION linepos,size_t choff)253 static int pos_shift(POSITION linepos, size_t choff)
254 {
255 	constant char *line;
256 	size_t line_len;
257 	POSITION pos;
258 	int cvt_ops;
259 	char *cline;
260 
261 	pos = forw_raw_line_len(linepos, choff, &line, &line_len);
262 	if (pos == NULL_POSITION || line_len != choff)
263 		return -1;
264 	cvt_ops = get_cvt_ops(0); /* {{ Passing 0 ignores SRCH_NO_REGEX; does it matter? }} */
265 	/* {{ It would be nice to be able to call cvt_text with dst=NULL, to avoid need to alloc a useless cline. }} */
266 	cline = (char *) ecalloc(1, cvt_length(line_len, cvt_ops));
267 	cvt_text(cline, line, NULL, &line_len, cvt_ops);
268 	free(cline);
269 	return (int) line_len;  /*{{type-issue}}*/
270 }
271 
272 /*
273  * Return the position of the first char of the line containing tpos.
274  * Thus if tpos is the first char of its line, just return tpos.
275  */
beginning_of_line(POSITION tpos)276 static POSITION beginning_of_line(POSITION tpos)
277 {
278 	ch_seek(tpos);
279 	while (ch_tell() != ch_zero())
280 	{
281 		int ch = ch_back_get();
282 		if (ch == '\n')
283 		{
284 			(void) ch_forw_get();
285 			break;
286 		}
287 	}
288 	return ch_tell();
289 }
290 
291 /*
292  * When viewing long lines, it may be that the first char in the top screen
293  * line is not the first char in its (file) line (the table is "beheaded").
294  * This function sets that entry to the position of the first char in the line,
295  * and sets hshift so that the first char in the first line is unchanged.
296  */
pos_rehead(void)297 public void pos_rehead(void)
298 {
299 	POSITION linepos;
300 	POSITION tpos = table[TOP];
301 	if (tpos == NULL_POSITION)
302 		return;
303 	linepos = beginning_of_line(tpos);
304 	if (linepos == tpos)
305 		return;
306 	table[TOP] = linepos;
307 	hshift = pos_shift(linepos, (size_t) (tpos - linepos));
308 	screen_trashed();
309 }
310