position.c revision 1.3 1 /* $NetBSD: position.c,v 1.3 2011/07/03 20:14:13 tron Exp $ */
2
3 /*
4 * Copyright (C) 1984-2011 Mark Nudelman
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
8 *
9 * For more information about less, or for information on how to
10 * contact the author, see the README file.
11 */
12
13
14 /*
15 * Routines dealing with the "position" table.
16 * This is a table which tells the position (in the input file) of the
17 * first char on each currently displayed line.
18 *
19 * {{ The position table is scrolled by moving all the entries.
20 * Would be better to have a circular table
21 * and just change a couple of pointers. }}
22 */
23
24 #include "less.h"
25 #include "position.h"
26
27 static POSITION *table = NULL; /* The position table */
28 static int table_size;
29
30 extern int sc_width, sc_height;
31
32 /*
33 * Return the starting file position of a line displayed on the screen.
34 * The line may be specified as a line number relative to the top
35 * of the screen, but is usually one of these special cases:
36 * the top (first) line on the screen
37 * the second line on the screen
38 * the bottom line on the screen
39 * the line after the bottom line on the screen
40 */
41 public POSITION
42 position(where)
43 int where;
44 {
45 switch (where)
46 {
47 case BOTTOM:
48 where = sc_height - 2;
49 break;
50 case BOTTOM_PLUS_ONE:
51 where = sc_height - 1;
52 break;
53 case MIDDLE:
54 where = (sc_height - 1) / 2;
55 }
56 return (table[where]);
57 }
58
59 /*
60 * Add a new file position to the bottom of the position table.
61 */
62 public void
63 add_forw_pos(pos)
64 POSITION pos;
65 {
66 register int i;
67
68 /*
69 * Scroll the position table up.
70 */
71 for (i = 1; i < sc_height; i++)
72 table[i-1] = table[i];
73 table[sc_height - 1] = pos;
74 }
75
76 /*
77 * Add a new file position to the top of the position table.
78 */
79 public void
80 add_back_pos(pos)
81 POSITION pos;
82 {
83 register 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 */
96 public void
97 pos_clear()
98 {
99 register int i;
100
101 for (i = 0; i < sc_height; i++)
102 table[i] = NULL_POSITION;
103 }
104
105 /*
106 * Allocate or reallocate the position table.
107 */
108 public void
109 pos_init()
110 {
111 struct scrpos scrpos;
112 scrpos.pos = scrpos.ln = 0; /* XXX: GCC */
113
114 if (sc_height <= table_size)
115 return;
116 /*
117 * If we already have a table, remember the first line in it
118 * before we free it, so we can copy that line to the new table.
119 */
120 if (table != NULL)
121 {
122 get_scrpos(&scrpos);
123 free((char*)table);
124 } else
125 scrpos.pos = NULL_POSITION;
126 table = (POSITION *) ecalloc(sc_height, sizeof(POSITION));
127 table_size = sc_height;
128 pos_clear();
129 if (scrpos.pos != NULL_POSITION)
130 table[scrpos.ln-1] = scrpos.pos;
131 }
132
133 /*
134 * See if the byte at a specified position is currently on the screen.
135 * Check the position table to see if the position falls within its range.
136 * Return the position table entry if found, -1 if not.
137 */
138 public int
139 onscreen(pos)
140 POSITION pos;
141 {
142 register int i;
143
144 if (pos < table[0])
145 return (-1);
146 for (i = 1; i < sc_height; i++)
147 if (pos < table[i])
148 return (i-1);
149 return (-1);
150 }
151
152 /*
153 * See if the entire screen is empty.
154 */
155 public int
156 empty_screen()
157 {
158 return (empty_lines(0, sc_height-1));
159 }
160
161 public int
162 empty_lines(s, e)
163 int s;
164 int e;
165 {
166 register int i;
167
168 for (i = s; i <= e; i++)
169 if (table[i] != NULL_POSITION)
170 return (0);
171 return (1);
172 }
173
174 /*
175 * Get the current screen position.
176 * The screen position consists of both a file position and
177 * a screen line number where the file position is placed on the screen.
178 * Normally the screen line number is 0, but if we are positioned
179 * such that the top few lines are empty, we may have to set
180 * the screen line to a number > 0.
181 */
182 public void
183 get_scrpos(scrpos)
184 struct scrpos *scrpos;
185 {
186 register int i;
187
188 /*
189 * Find the first line on the screen which has something on it,
190 * and return the screen line number and the file position.
191 */
192 for (i = 0; i < sc_height; i++)
193 if (table[i] != NULL_POSITION)
194 {
195 scrpos->ln = i+1;
196 scrpos->pos = table[i];
197 return;
198 }
199 /*
200 * The screen is empty.
201 */
202 scrpos->pos = NULL_POSITION;
203 }
204
205 /*
206 * Adjust a screen line number to be a simple positive integer
207 * in the range { 0 .. sc_height-2 }.
208 * (The bottom line, sc_height-1, is reserved for prompts, etc.)
209 * The given "sline" may be in the range { 1 .. sc_height-1 }
210 * to refer to lines relative to the top of the screen (starting from 1),
211 * or it may be in { -1 .. -(sc_height-1) } to refer to lines
212 * relative to the bottom of the screen.
213 */
214 public int
215 adjsline(sline)
216 int sline;
217 {
218 /*
219 * Negative screen line number means
220 * relative to the bottom of the screen.
221 */
222 if (sline < 0)
223 sline += sc_height;
224 /*
225 * Can't be less than 1 or greater than sc_height-1.
226 */
227 if (sline <= 0)
228 sline = 1;
229 if (sline >= sc_height)
230 sline = sc_height - 1;
231 /*
232 * Return zero-based line number, not one-based.
233 */
234 return (sline-1);
235 }
236