usr.sbin/makefs/hammer2: Remove redundant hammer2_inode_modify()
[dragonfly.git] / contrib / less / position.c
blobeabaf7e66dc2541593a8433e22c103b515ea7799
1 /*
2 * Copyright (C) 1984-2022 Mark Nudelman
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.
7 * For more information, see the README file.
8 */
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.
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. }}
21 #include "less.h"
22 #include "position.h"
24 static POSITION *table = NULL; /* The position table */
25 static int table_size = 0;
27 extern int sc_width, sc_height;
28 extern int header_lines;
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
39 public POSITION
40 position(sindex)
41 int sindex;
43 switch (sindex)
45 case BOTTOM:
46 sindex = sc_height - 2;
47 break;
48 case BOTTOM_PLUS_ONE:
49 sindex = sc_height - 1;
50 break;
51 case MIDDLE:
52 sindex = (sc_height - 1) / 2;
53 break;
55 return (table[sindex]);
59 * Add a new file position to the bottom of the position table.
61 public void
62 add_forw_pos(pos)
63 POSITION pos;
65 int i;
68 * Scroll the position table up.
70 for (i = 1; i < sc_height; i++)
71 table[i-1] = table[i];
72 table[sc_height - 1] = pos;
76 * Add a new file position to the top of the position table.
78 public void
79 add_back_pos(pos)
80 POSITION pos;
82 int i;
85 * Scroll the position table down.
87 for (i = sc_height - 1; i > 0; i--)
88 table[i] = table[i-1];
89 table[0] = pos;
93 * Initialize the position table, done whenever we clear the screen.
95 public void
96 pos_clear(VOID_PARAM)
98 int i;
100 for (i = 0; i < sc_height; i++)
101 table[i] = NULL_POSITION;
105 * Allocate or reallocate the position table.
107 public void
108 pos_init(VOID_PARAM)
110 struct scrpos scrpos;
112 if (sc_height <= table_size)
113 return;
115 * If we already have a table, remember the first line in it
116 * before we free it, so we can copy that line to the new table.
118 if (table != NULL)
120 get_scrpos(&scrpos, TOP);
121 free((char*)table);
122 } else
123 scrpos.pos = NULL_POSITION;
124 table = (POSITION *) ecalloc(sc_height, sizeof(POSITION));
125 table_size = sc_height;
126 pos_clear();
127 if (scrpos.pos != NULL_POSITION)
128 table[scrpos.ln-1] = scrpos.pos;
132 * See if the byte at a specified position is currently on the screen.
133 * Check the position table to see if the position falls within its range.
134 * Return the position table entry if found, -1 if not.
136 public int
137 onscreen(pos)
138 POSITION pos;
140 int i;
142 if (pos < table[0])
143 return (-1);
144 for (i = 1; i < sc_height; i++)
145 if (pos < table[i])
146 return (i-1);
147 return (-1);
151 * See if the entire screen is empty.
153 public int
154 empty_screen(VOID_PARAM)
156 return (empty_lines(0, sc_height-1));
159 public int
160 empty_lines(s, e)
161 int s;
162 int e;
164 int i;
166 for (i = s; i <= e; i++)
167 if (table[i] != NULL_POSITION && table[i] != 0)
168 return (0);
169 return (1);
173 * Get the current screen position.
174 * The screen position consists of both a file position and
175 * a screen line number where the file position is placed on the screen.
176 * Normally the screen line number is 0, but if we are positioned
177 * such that the top few lines are empty, we may have to set
178 * the screen line to a number > 0.
180 public void
181 get_scrpos(scrpos, where)
182 struct scrpos *scrpos;
183 int where;
185 int i;
186 int dir;
187 int last;
189 switch (where)
191 case TOP:
192 i = 0; dir = +1; last = sc_height-2;
193 break;
194 case BOTTOM: case BOTTOM_PLUS_ONE:
195 i = sc_height-2; dir = -1; last = 0;
196 break;
197 default:
198 i = where;
199 if (table[i] == NULL_POSITION) {
200 scrpos->pos = NULL_POSITION;
201 return;
203 /* Values of dir and last don't matter after this. */
204 break;
208 * Find the first line on the screen which has something on it,
209 * and return the screen line number and the file position.
211 for (;; i += dir)
213 if (table[i] != NULL_POSITION)
215 scrpos->ln = i+1;
216 scrpos->pos = table[i];
217 return;
219 if (i == last) break;
222 * The screen is empty.
224 scrpos->pos = NULL_POSITION;
228 * Adjust a screen line number to be a simple positive integer
229 * in the range { 0 .. sc_height-2 }.
230 * (The bottom line, sc_height-1, is reserved for prompts, etc.)
231 * The given "sline" may be in the range { 1 .. sc_height-1 }
232 * to refer to lines relative to the top of the screen (starting from 1),
233 * or it may be in { -1 .. -(sc_height-1) } to refer to lines
234 * relative to the bottom of the screen.
236 public int
237 sindex_from_sline(sline)
238 int sline;
241 * Negative screen line number means
242 * relative to the bottom of the screen.
244 if (sline < 0)
245 sline += sc_height;
247 * Can't be less than 1 or greater than sc_height.
249 if (sline <= 0)
250 sline = 1;
251 if (sline > sc_height)
252 sline = sc_height;
254 * Return zero-based line number, not one-based.
256 return (sline-1);