dhcpcd: update README.DRAGONFLY
[dragonfly.git] / contrib / less / jump.c
blob2bb4abbc1a092c2f3bf8f8ec660c8b8454743733
1 /*
2 * Copyright (C) 1984-2023 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 which jump to a new location in the file.
15 #include "less.h"
16 #include "position.h"
18 extern int jump_sline;
19 extern int squished;
20 extern int screen_trashed;
21 extern int sc_width, sc_height;
22 extern int show_attn;
23 extern int top_scroll;
26 * Jump to the end of the file.
28 public void jump_forw(void)
30 POSITION pos;
31 POSITION end_pos;
33 if (ch_end_seek())
35 error("Cannot seek to end of file", NULL_PARG);
36 return;
38 end_pos = ch_tell();
39 if (position(sc_height-1) == end_pos)
41 eof_bell();
42 return;
44 /*
45 * Note; lastmark will be called later by jump_loc, but it fails
46 * because the position table has been cleared by pos_clear below.
47 * So call it here before calling pos_clear.
49 lastmark();
51 * Position the last line in the file at the last screen line.
52 * Go back one line from the end of the file
53 * to get to the beginning of the last line.
55 pos_clear();
56 pos = back_line(end_pos);
57 if (pos == NULL_POSITION)
58 jump_loc(ch_zero(), sc_height-1);
59 else
61 jump_loc(pos, sc_height-1);
62 if (position(sc_height-1) != end_pos)
63 repaint();
68 * Jump to the last buffered line in the file.
70 public void jump_forw_buffered(void)
72 POSITION end;
74 if (ch_end_buffer_seek())
76 error("Cannot seek to end of buffers", NULL_PARG);
77 return;
79 end = ch_tell();
80 if (end != NULL_POSITION && end > 0)
81 jump_line_loc(end-1, sc_height-1);
85 * Jump to line n in the file.
87 public void jump_back(LINENUM linenum)
89 POSITION pos;
90 PARG parg;
93 * Find the position of the specified line.
94 * If we can seek there, just jump to it.
95 * If we can't seek, but we're trying to go to line number 1,
96 * use ch_beg_seek() to get as close as we can.
98 pos = find_pos(linenum);
99 if (pos != NULL_POSITION && ch_seek(pos) == 0)
101 if (show_attn)
102 set_attnpos(pos);
103 jump_loc(pos, jump_sline);
104 } else if (linenum <= 1 && ch_beg_seek() == 0)
106 jump_loc(ch_tell(), jump_sline);
107 error("Cannot seek to beginning of file", NULL_PARG);
108 } else
110 parg.p_linenum = linenum;
111 error("Cannot seek to line number %n", &parg);
116 * Repaint the screen.
118 public void repaint(void)
120 struct scrpos scrpos;
122 * Start at the line currently at the top of the screen
123 * and redisplay the screen.
125 get_scrpos(&scrpos, TOP);
126 pos_clear();
127 if (scrpos.pos == NULL_POSITION)
128 /* Screen hasn't been drawn yet. */
129 jump_loc(ch_zero(), 1);
130 else
131 jump_loc(scrpos.pos, scrpos.ln);
135 * Jump to a specified percentage into the file.
137 public void jump_percent(int percent, long fraction)
139 POSITION pos, len;
142 * Determine the position in the file
143 * (the specified percentage of the file's length).
145 if ((len = ch_length()) == NULL_POSITION)
147 ierror("Determining length of file", NULL_PARG);
148 ch_end_seek();
150 if ((len = ch_length()) == NULL_POSITION)
152 error("Don't know length of file", NULL_PARG);
153 return;
155 pos = percent_pos(len, percent, fraction);
156 if (pos >= len)
157 pos = len-1;
159 jump_line_loc(pos, jump_sline);
163 * Jump to a specified position in the file.
164 * Like jump_loc, but the position need not be
165 * the first character in a line.
167 public void jump_line_loc(POSITION pos, int sline)
169 int c;
171 if (ch_seek(pos) == 0)
174 * Back up to the beginning of the line.
176 while ((c = ch_back_get()) != '\n' && c != EOI)
178 if (c == '\n')
179 (void) ch_forw_get();
180 pos = ch_tell();
182 if (show_attn)
183 set_attnpos(pos);
184 jump_loc(pos, sline);
188 * Jump to a specified position in the file.
189 * The position must be the first character in a line.
190 * Place the target line on a specified line on the screen.
192 public void jump_loc(POSITION pos, int sline)
194 int nline;
195 int sindex;
196 POSITION tpos;
197 POSITION bpos;
200 * Normalize sline.
202 sindex = sindex_from_sline(sline);
204 if ((nline = onscreen(pos)) >= 0)
207 * The line is currently displayed.
208 * Just scroll there.
210 nline -= sindex;
211 if (nline > 0)
212 forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0);
213 else
214 back(-nline, position(TOP), 1, 0);
215 #if HILITE_SEARCH
216 if (show_attn)
217 repaint_hilite(1);
218 #endif
219 return;
223 * Line is not on screen.
224 * Seek to the desired location.
226 if (ch_seek(pos))
228 error("Cannot seek to that file position", NULL_PARG);
229 return;
233 * See if the desired line is before or after
234 * the currently displayed screen.
236 tpos = position(TOP);
237 bpos = position(BOTTOM_PLUS_ONE);
238 if (tpos == NULL_POSITION || pos >= tpos)
241 * The desired line is after the current screen.
242 * Move back in the file far enough so that we can
243 * call forw() and put the desired line at the
244 * sline-th line on the screen.
246 for (nline = 0; nline < sindex; nline++)
248 if (bpos != NULL_POSITION && pos <= bpos)
251 * Surprise! The desired line is
252 * close enough to the current screen
253 * that we can just scroll there after all.
255 forw(sc_height-sindex+nline-1, bpos, 1, 0, 0);
256 #if HILITE_SEARCH
257 if (show_attn)
258 repaint_hilite(1);
259 #endif
260 return;
262 pos = back_line(pos);
263 if (pos == NULL_POSITION)
266 * Oops. Ran into the beginning of the file.
267 * Exit the loop here and rely on forw()
268 * below to draw the required number of
269 * blank lines at the top of the screen.
271 break;
274 lastmark();
275 squished = 0;
276 screen_trashed = 0;
277 forw(sc_height-1, pos, 1, 0, sindex-nline);
278 } else
281 * The desired line is before the current screen.
282 * Move forward in the file far enough so that we
283 * can call back() and put the desired line at the
284 * sindex-th line on the screen.
286 for (nline = sindex; nline < sc_height - 1; nline++)
288 pos = forw_line(pos);
289 if (pos == NULL_POSITION)
292 * Ran into end of file.
293 * This shouldn't normally happen,
294 * but may if there is some kind of read error.
296 break;
298 #if HILITE_SEARCH
299 pos = next_unfiltered(pos);
300 #endif
301 if (pos >= tpos)
304 * Surprise! The desired line is
305 * close enough to the current screen
306 * that we can just scroll there after all.
308 back(nline+1, tpos, 1, 0);
309 #if HILITE_SEARCH
310 if (show_attn)
311 repaint_hilite(1);
312 #endif
313 return;
316 lastmark();
317 if (!top_scroll)
318 clear();
319 else
320 home();
321 screen_trashed = 0;
322 add_back_pos(pos);
323 back(sc_height-1, pos, 1, 0);