position.c (009e81b16465ea457c0e63fd49fe77f47cc27a5a) position.c (1ea316270f1f75922ac53976d5d8808a41442f46)
1/*
2 * Copyright (C) 1984-2015 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 */

--- 22 unchanged lines hidden (view full) ---

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
1/*
2 * Copyright (C) 1984-2015 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 */

--- 22 unchanged lines hidden (view full) ---

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
39position(where)
40 int where;
39position(int where)
41{
42 switch (where)
43 {
44 case BOTTOM:
45 where = sc_height - 2;
46 break;
47 case BOTTOM_PLUS_ONE:
48 where = sc_height - 1;
49 break;
50 case MIDDLE:
51 where = (sc_height - 1) / 2;
52 }
53 return (table[where]);
54}
55
56/*
57 * Add a new file position to the bottom of the position table.
58 */
59 public void
40{
41 switch (where)
42 {
43 case BOTTOM:
44 where = sc_height - 2;
45 break;
46 case BOTTOM_PLUS_ONE:
47 where = sc_height - 1;
48 break;
49 case MIDDLE:
50 where = (sc_height - 1) / 2;
51 }
52 return (table[where]);
53}
54
55/*
56 * Add a new file position to the bottom of the position table.
57 */
58 public void
60add_forw_pos(pos)
61 POSITION pos;
59add_forw_pos(POSITION pos)
62{
60{
63 register int i;
61 int i;
64
65 /*
66 * Scroll the position table up.
67 */
68 for (i = 1; i < sc_height; i++)
69 table[i-1] = table[i];
70 table[sc_height - 1] = pos;
71}
72
73/*
74 * Add a new file position to the top of the position table.
75 */
76 public void
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 */
74 public void
77add_back_pos(pos)
78 POSITION pos;
75add_back_pos(POSITION pos)
79{
76{
80 register int i;
77 int i;
81
82 /*
83 * Scroll the position table down.
84 */
85 for (i = sc_height - 1; i > 0; i--)
86 table[i] = table[i-1];
87 table[0] = pos;
88}
89
90/*
91 * Initialize the position table, done whenever we clear the screen.
92 */
93 public void
78
79 /*
80 * Scroll the position table down.
81 */
82 for (i = sc_height - 1; i > 0; i--)
83 table[i] = table[i-1];
84 table[0] = pos;
85}
86
87/*
88 * Initialize the position table, done whenever we clear the screen.
89 */
90 public void
94pos_clear()
91pos_clear(void)
95{
92{
96 register int i;
93 int i;
97
98 for (i = 0; i < sc_height; i++)
99 table[i] = NULL_POSITION;
100}
101
102/*
103 * Allocate or reallocate the position table.
104 */
105 public void
94
95 for (i = 0; i < sc_height; i++)
96 table[i] = NULL_POSITION;
97}
98
99/*
100 * Allocate or reallocate the position table.
101 */
102 public void
106pos_init()
103pos_init(void)
107{
108 struct scrpos scrpos;
109
110 if (sc_height <= table_size)
111 return;
112 /*
113 * If we already have a table, remember the first line in it
114 * before we free it, so we can copy that line to the new table.

--- 12 unchanged lines hidden (view full) ---

127}
128
129/*
130 * See if the byte at a specified position is currently on the screen.
131 * Check the position table to see if the position falls within its range.
132 * Return the position table entry if found, -1 if not.
133 */
134 public int
104{
105 struct scrpos scrpos;
106
107 if (sc_height <= table_size)
108 return;
109 /*
110 * If we already have a table, remember the first line in it
111 * before we free it, so we can copy that line to the new table.

--- 12 unchanged lines hidden (view full) ---

124}
125
126/*
127 * See if the byte at a specified position is currently on the screen.
128 * Check the position table to see if the position falls within its range.
129 * Return the position table entry if found, -1 if not.
130 */
131 public int
135onscreen(pos)
136 POSITION pos;
132onscreen(POSITION pos)
137{
133{
138 register int i;
134 int i;
139
140 if (pos < table[0])
141 return (-1);
142 for (i = 1; i < sc_height; i++)
143 if (pos < table[i])
144 return (i-1);
145 return (-1);
146}
147
148/*
149 * See if the entire screen is empty.
150 */
151 public int
135
136 if (pos < table[0])
137 return (-1);
138 for (i = 1; i < sc_height; i++)
139 if (pos < table[i])
140 return (i-1);
141 return (-1);
142}
143
144/*
145 * See if the entire screen is empty.
146 */
147 public int
152empty_screen()
148empty_screen(void)
153{
154 return (empty_lines(0, sc_height-1));
155}
156
157 public int
149{
150 return (empty_lines(0, sc_height-1));
151}
152
153 public int
158empty_lines(s, e)
159 int s;
160 int e;
154empty_lines(int s, int e)
161{
155{
162 register int i;
156 int i;
163
164 for (i = s; i <= e; i++)
165 if (table[i] != NULL_POSITION && table[i] != 0)
166 return (0);
167 return (1);
168}
169
170/*
171 * Get the current screen position.
172 * The screen position consists of both a file position and
173 * a screen line number where the file position is placed on the screen.
174 * Normally the screen line number is 0, but if we are positioned
175 * such that the top few lines are empty, we may have to set
176 * the screen line to a number > 0.
177 */
178 public void
157
158 for (i = s; i <= e; i++)
159 if (table[i] != NULL_POSITION && table[i] != 0)
160 return (0);
161 return (1);
162}
163
164/*
165 * Get the current screen position.
166 * The screen position consists of both a file position and
167 * a screen line number where the file position is placed on the screen.
168 * Normally the screen line number is 0, but if we are positioned
169 * such that the top few lines are empty, we may have to set
170 * the screen line to a number > 0.
171 */
172 public void
179get_scrpos(scrpos)
180 struct scrpos *scrpos;
173get_scrpos(struct scrpos *scrpos)
181{
174{
182 register int i;
175 int i;
183
184 /*
185 * Find the first line on the screen which has something on it,
186 * and return the screen line number and the file position.
187 */
188 for (i = 0; i < sc_height; i++)
189 if (table[i] != NULL_POSITION)
190 {

--- 12 unchanged lines hidden (view full) ---

203 * in the range { 0 .. sc_height-2 }.
204 * (The bottom line, sc_height-1, is reserved for prompts, etc.)
205 * The given "sline" may be in the range { 1 .. sc_height-1 }
206 * to refer to lines relative to the top of the screen (starting from 1),
207 * or it may be in { -1 .. -(sc_height-1) } to refer to lines
208 * relative to the bottom of the screen.
209 */
210 public int
176
177 /*
178 * Find the first line on the screen which has something on it,
179 * and return the screen line number and the file position.
180 */
181 for (i = 0; i < sc_height; i++)
182 if (table[i] != NULL_POSITION)
183 {

--- 12 unchanged lines hidden (view full) ---

196 * in the range { 0 .. sc_height-2 }.
197 * (The bottom line, sc_height-1, is reserved for prompts, etc.)
198 * The given "sline" may be in the range { 1 .. sc_height-1 }
199 * to refer to lines relative to the top of the screen (starting from 1),
200 * or it may be in { -1 .. -(sc_height-1) } to refer to lines
201 * relative to the bottom of the screen.
202 */
203 public int
211adjsline(sline)
212 int sline;
204adjsline(int sline)
213{
214 /*
215 * Negative screen line number means
216 * relative to the bottom of the screen.
217 */
218 if (sline < 0)
219 sline += sc_height;
220 /*
221 * Can't be less than 1 or greater than sc_height-1.
222 */
223 if (sline <= 0)
224 sline = 1;
225 if (sline >= sc_height)
226 sline = sc_height - 1;
227 /*
228 * Return zero-based line number, not one-based.
229 */
230 return (sline-1);
231}
205{
206 /*
207 * Negative screen line number means
208 * relative to the bottom of the screen.
209 */
210 if (sline < 0)
211 sline += sc_height;
212 /*
213 * Can't be less than 1 or greater than sc_height-1.
214 */
215 if (sline <= 0)
216 sline = 1;
217 if (sline >= sc_height)
218 sline = sc_height - 1;
219 /*
220 * Return zero-based line number, not one-based.
221 */
222 return (sline-1);
223}