2 * Copyright (C) 1984-2015 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.
12 * Routines which jump to a new location in the file.
18 extern int jump_sline;
20 extern int screen_trashed;
21 extern int sc_width, sc_height;
23 extern int top_scroll;
26 * Jump to the end of the file.
36 error("Cannot seek to end of file", NULL_PARG);
40 * Note; lastmark will be called later by jump_loc, but it fails
41 * because the position table has been cleared by pos_clear below.
42 * So call it here before calling pos_clear.
46 * Position the last line in the file at the last screen line.
47 * Go back one line from the end of the file
48 * to get to the beginning of the last line.
52 pos = back_line(end_pos);
53 if (pos == NULL_POSITION)
54 jump_loc((POSITION)0, sc_height-1);
57 jump_loc(pos, sc_height-1);
58 if (position(sc_height-1) != end_pos)
64 * Jump to the last buffered line in the file.
67 jump_forw_buffered(void)
71 if (ch_end_buffer_seek())
73 error("Cannot seek to end of buffers", NULL_PARG);
77 if (end != NULL_POSITION && end > 0)
78 jump_line_loc(end-1, sc_height-1);
82 * Jump to line n in the file.
85 jump_back(LINENUM linenum)
91 * Find the position of the specified line.
92 * If we can seek there, just jump to it.
93 * If we can't seek, but we're trying to go to line number 1,
94 * use ch_beg_seek() to get as close as we can.
96 pos = find_pos(linenum);
97 if (pos != NULL_POSITION && ch_seek(pos) == 0)
101 jump_loc(pos, jump_sline);
102 } else if (linenum <= 1 && ch_beg_seek() == 0)
104 jump_loc(ch_tell(), jump_sline);
105 error("Cannot seek to beginning of file", NULL_PARG);
108 parg.p_linenum = linenum;
109 error("Cannot seek to line number %n", &parg);
114 * Repaint the screen.
119 struct scrpos scrpos;
121 * Start at the line currently at the top of the screen
122 * and redisplay the screen.
126 jump_loc(scrpos.pos, scrpos.ln);
130 * Jump to a specified percentage into the file.
133 jump_percent(int percent, long fraction)
138 * Determine the position in the file
139 * (the specified percentage of the file's length).
141 if ((len = ch_length()) == NULL_POSITION)
143 ierror("Determining length of file", NULL_PARG);
146 if ((len = ch_length()) == NULL_POSITION)
148 error("Don't know length of file", NULL_PARG);
151 pos = percent_pos(len, percent, fraction);
155 jump_line_loc(pos, jump_sline);
159 * Jump to a specified position in the file.
160 * Like jump_loc, but the position need not be
161 * the first character in a line.
164 jump_line_loc(POSITION pos, int sline)
168 if (ch_seek(pos) == 0)
171 * Back up to the beginning of the line.
173 while ((c = ch_back_get()) != '\n' && c != EOI)
176 (void) ch_forw_get();
181 jump_loc(pos, sline);
185 * Jump to a specified position in the file.
186 * The position must be the first character in a line.
187 * Place the target line on a specified line on the screen.
190 jump_loc(POSITION pos, int sline)
199 sline = adjsline(sline);
201 if ((nline = onscreen(pos)) >= 0)
204 * The line is currently displayed.
209 forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0);
211 back(-nline, position(TOP), 1, 0);
220 * Line is not on screen.
221 * Seek to the desired location.
225 error("Cannot seek to that file position", NULL_PARG);
230 * See if the desired line is before or after
231 * the currently displayed screen.
233 tpos = position(TOP);
234 bpos = position(BOTTOM_PLUS_ONE);
235 if (tpos == NULL_POSITION || pos >= tpos)
238 * The desired line is after the current screen.
239 * Move back in the file far enough so that we can
240 * call forw() and put the desired line at the
241 * sline-th line on the screen.
243 for (nline = 0; nline < sline; nline++)
245 if (bpos != NULL_POSITION && pos <= bpos)
248 * Surprise! The desired line is
249 * close enough to the current screen
250 * that we can just scroll there after all.
252 forw(sc_height-sline+nline-1, bpos, 1, 0, 0);
259 pos = back_line(pos);
260 if (pos == NULL_POSITION)
263 * Oops. Ran into the beginning of the file.
264 * Exit the loop here and rely on forw()
265 * below to draw the required number of
266 * blank lines at the top of the screen.
274 forw(sc_height-1, pos, 1, 0, sline-nline);
278 * The desired line is before the current screen.
279 * Move forward in the file far enough so that we
280 * can call back() and put the desired line at the
281 * sline-th line on the screen.
283 for (nline = sline; nline < sc_height - 1; nline++)
285 pos = forw_line(pos);
286 if (pos == NULL_POSITION)
289 * Ran into end of file.
290 * This shouldn't normally happen,
291 * but may if there is some kind of read error.
296 pos = next_unfiltered(pos);
301 * Surprise! The desired line is
302 * close enough to the current screen
303 * that we can just scroll there after all.
305 back(nline+1, tpos, 1, 0);
320 back(sc_height-1, pos, 1, 0);