1 /*
2 * Copyright (C) 1984-2026 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 lbool empty_screen(void)
151 {
152 return (empty_lines(0, sc_height-1));
153 }
154
empty_lines(int s,int e)155 public lbool empty_lines(int s, int e)
156 {
157 int i;
158
159 for (i = s; i <= e; i++)
160 if (table[i] != NULL_POSITION)
161 return FALSE;
162 return TRUE;
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_pos(struct scrpos * scrpos,int where,POSITION pos)173 public void get_scrpos_pos(struct scrpos *scrpos, int where, POSITION pos)
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 if (pos != NULL_POSITION)
198 {
199 scrpos->ln = i+1;
200 scrpos->pos = pos;
201 } else
202 {
203 /*
204 * Find the first line on the screen which has something on it,
205 * and return the screen line number and the file position.
206 */
207 for (;; i += dir)
208 {
209 if (table[i] != NULL_POSITION)
210 {
211 scrpos->ln = i+1;
212 scrpos->pos = table[i];
213 return;
214 }
215 if (i == last) break;
216 }
217 /* The screen is empty. */
218 scrpos->pos = NULL_POSITION;
219 }
220 }
221
get_scrpos(struct scrpos * scrpos,int where)222 public void get_scrpos(struct scrpos *scrpos, int where)
223 {
224 get_scrpos_pos(scrpos, where, NULL_POSITION);
225 }
226
227 /*
228 * Convert sline to sindex and clip to screen size.
229 */
sindex_from_sline_clipped(int sline)230 static int sindex_from_sline_clipped(int sline)
231 {
232 /*
233 * Can't be less than 1 or greater than sc_height-1.
234 */
235 if (sline <= 0)
236 sline = 1;
237 if (sline >= sc_height)
238 sline = sc_height-1;
239 /*
240 * Return zero-based line number, not one-based.
241 */
242 return (sline-1);
243 }
244
245 /*
246 * Adjust a screen line number to be a simple positive integer
247 * in the range { 0 .. sc_height-2 }.
248 * (The bottom line, sc_height-1, is reserved for prompts, etc.)
249 * The given "sline" may be in the range { 1 .. sc_height-1 }
250 * to refer to lines relative to the top of the screen (starting from 1),
251 * or it may be in { -1 .. -(sc_height-1) } to refer to lines
252 * relative to the bottom of the screen.
253 */
sindex_from_sline(int sline)254 public int sindex_from_sline(int sline)
255 {
256 /*
257 * Negative screen line number means
258 * relative to the bottom of the screen.
259 */
260 if (sline < 0)
261 sline += sc_height;
262 return sindex_from_sline_clipped(sline);
263 }
264
265 /*
266 * Given a line that starts at linepos,
267 * and the character at byte offset choff into that line,
268 * return the number of characters (not bytes) between the
269 * beginning of the line and the first byte of the choff character.
270 */
pos_shift(POSITION linepos,size_t choff)271 static int pos_shift(POSITION linepos, size_t choff)
272 {
273 constant char *line;
274 size_t line_len;
275 POSITION pos;
276 int cvt_ops;
277 char *cline;
278
279 pos = forw_raw_line_len(linepos, choff, &line, &line_len);
280 if (pos == NULL_POSITION || line_len != choff)
281 return -1;
282 cvt_ops = get_cvt_ops(0); /* {{ Passing 0 ignores SRCH_NO_REGEX; does it matter? }} */
283 /* {{ It would be nice to be able to call cvt_text with dst=NULL, to avoid need to alloc a useless cline. }} */
284 cline = (char *) ecalloc(1, cvt_length(line_len, cvt_ops));
285 cvt_text(cline, line, NULL, &line_len, cvt_ops);
286 free(cline);
287 return (int) line_len; /*{{type-issue}}*/
288 }
289
290 /*
291 * Return the position of the first char of the line containing tpos.
292 * Thus if tpos is the first char of its line, just return tpos.
293 */
beginning_of_line(POSITION tpos)294 public POSITION beginning_of_line(POSITION tpos)
295 {
296 ch_seek(tpos);
297 while (ch_tell() != ch_zero())
298 {
299 int ch = ch_back_get();
300 if (ch == EOI)
301 break; /* {{ probably unnecessary due to ch_zero() check }} */
302 if (ch == '\n')
303 {
304 (void) ch_forw_get();
305 break;
306 }
307 }
308 return ch_tell();
309 }
310
311 /*
312 * When viewing long lines, it may be that the first char in the top screen
313 * line is not the first char in its (file) line (the table is "beheaded").
314 * This function sets that entry to the position of the first char in the
315 * line, and optionally sets hshift so that the first char in the first line
316 * is unchanged.
317 */
pos_rehead(lbool adj_hshift)318 public void pos_rehead(lbool adj_hshift)
319 {
320 POSITION linepos;
321 POSITION tpos = table[TOP];
322 if (tpos == NULL_POSITION)
323 return;
324 linepos = beginning_of_line(tpos);
325 if (linepos == tpos)
326 return;
327 table[TOP] = linepos;
328 if (adj_hshift)
329 hshift = pos_shift(linepos, (size_t) (tpos - linepos));
330 screen_trashed();
331 }
332