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