xref: /dragonfly/contrib/less/position.c (revision c6ecc293)
1 /*
2  * Copyright (C) 1984-2019  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 
29 /*
30  * Return the starting file position of a line displayed on the screen.
31  * The line may be specified as a line number relative to the top
32  * of the screen, but is usually one of these special cases:
33  *	the top (first) line on the screen
34  *	the second line on the screen
35  *	the bottom line on the screen
36  *	the line after the bottom line on the screen
37  */
38 	public POSITION
39 position(sindex)
40 	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 MIDDLE:
51 		sindex = (sc_height - 1) / 2;
52 		break;
53 	}
54 	return (table[sindex]);
55 }
56 
57 /*
58  * Add a new file position to the bottom of the position table.
59  */
60 	public void
61 add_forw_pos(pos)
62 	POSITION pos;
63 {
64 	int i;
65 
66 	/*
67 	 * Scroll the position table up.
68 	 */
69 	for (i = 1;  i < sc_height;  i++)
70 		table[i-1] = table[i];
71 	table[sc_height - 1] = pos;
72 }
73 
74 /*
75  * Add a new file position to the top of the position table.
76  */
77 	public void
78 add_back_pos(pos)
79 	POSITION pos;
80 {
81 	int i;
82 
83 	/*
84 	 * Scroll the position table down.
85 	 */
86 	for (i = sc_height - 1;  i > 0;  i--)
87 		table[i] = table[i-1];
88 	table[0] = pos;
89 }
90 
91 /*
92  * Initialize the position table, done whenever we clear the screen.
93  */
94 	public void
95 pos_clear(VOID_PARAM)
96 {
97 	int i;
98 
99 	for (i = 0;  i < sc_height;  i++)
100 		table[i] = NULL_POSITION;
101 }
102 
103 /*
104  * Allocate or reallocate the position table.
105  */
106 	public void
107 pos_init(VOID_PARAM)
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(sc_height, sizeof(POSITION));
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  */
135 	public int
136 onscreen(pos)
137 	POSITION pos;
138 {
139 	int i;
140 
141 	if (pos < table[0])
142 		return (-1);
143 	for (i = 1;  i < sc_height;  i++)
144 		if (pos < table[i])
145 			return (i-1);
146 	return (-1);
147 }
148 
149 /*
150  * See if the entire screen is empty.
151  */
152 	public int
153 empty_screen(VOID_PARAM)
154 {
155 	return (empty_lines(0, sc_height-1));
156 }
157 
158 	public int
159 empty_lines(s, e)
160 	int s;
161 	int e;
162 {
163 	int i;
164 
165 	for (i = s;  i <= e;  i++)
166 		if (table[i] != NULL_POSITION && table[i] != 0)
167 			return (0);
168 	return (1);
169 }
170 
171 /*
172  * Get the current screen position.
173  * The screen position consists of both a file position and
174  * a screen line number where the file position is placed on the screen.
175  * Normally the screen line number is 0, but if we are positioned
176  * such that the top few lines are empty, we may have to set
177  * the screen line to a number > 0.
178  */
179 	public void
180 get_scrpos(scrpos, where)
181 	struct scrpos *scrpos;
182 	int where;
183 {
184 	int i;
185 	int dir;
186 	int last;
187 
188 	switch (where)
189 	{
190 	case TOP:
191 		i = 0; dir = +1; last = sc_height-2;
192 		break;
193 	case BOTTOM: case BOTTOM_PLUS_ONE:
194 		i = sc_height-2; dir = -1; last = 0;
195 		break;
196 	default:
197 		i = where;
198 		if (table[i] == NULL_POSITION) {
199 			scrpos->pos = NULL_POSITION;
200 			return;
201 		}
202 		/* Values of dir and last don't matter after this. */
203 		break;
204 	}
205 
206 	/*
207 	 * Find the first line on the screen which has something on it,
208 	 * and return the screen line number and the file position.
209 	 */
210 	for (;; i += dir)
211 	{
212 		if (table[i] != NULL_POSITION)
213 		{
214 			scrpos->ln = i+1;
215 			scrpos->pos = table[i];
216 			return;
217 		}
218 		if (i == last) break;
219 	}
220 	/*
221 	 * The screen is empty.
222 	 */
223 	scrpos->pos = NULL_POSITION;
224 }
225 
226 /*
227  * Adjust a screen line number to be a simple positive integer
228  * in the range { 0 .. sc_height-2 }.
229  * (The bottom line, sc_height-1, is reserved for prompts, etc.)
230  * The given "sline" may be in the range { 1 .. sc_height-1 }
231  * to refer to lines relative to the top of the screen (starting from 1),
232  * or it may be in { -1 .. -(sc_height-1) } to refer to lines
233  * relative to the bottom of the screen.
234  */
235 	public int
236 sindex_from_sline(sline)
237 	int sline;
238 {
239 	/*
240 	 * Negative screen line number means
241 	 * relative to the bottom of the screen.
242 	 */
243 	if (sline < 0)
244 		sline += sc_height;
245 	/*
246 	 * Can't be less than 1 or greater than sc_height.
247 	 */
248 	if (sline <= 0)
249 		sline = 1;
250 	if (sline > sc_height)
251 		sline = sc_height;
252 	/*
253 	 * Return zero-based line number, not one-based.
254 	 */
255 	return (sline-1);
256 }
257