2 * Copyright (C) 1984-2011 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines which jump to a new location in the file.
19 extern int jump_sline;
21 extern int screen_trashed;
22 extern int sc_width, sc_height;
24 extern int top_scroll;
27 * Jump to the end of the file.
37 error("Cannot seek to end of file", NULL_PARG);
41 * Note; lastmark will be called later by jump_loc, but it fails
42 * because the position table has been cleared by pos_clear below.
43 * So call it here before calling pos_clear.
47 * Position the last line in the file at the last screen line.
48 * Go back one line from the end of the file
49 * to get to the beginning of the last line.
53 pos = back_line(end_pos);
54 if (pos == NULL_POSITION)
55 jump_loc((POSITION)0, sc_height-1);
58 jump_loc(pos, sc_height-1);
59 if (position(sc_height-1) != end_pos)
65 * Jump to line n in the file.
75 * Find the position of the specified line.
76 * If we can seek there, just jump to it.
77 * If we can't seek, but we're trying to go to line number 1,
78 * use ch_beg_seek() to get as close as we can.
80 pos = find_pos(linenum);
81 if (pos != NULL_POSITION && ch_seek(pos) == 0)
85 jump_loc(pos, jump_sline);
86 } else if (linenum <= 1 && ch_beg_seek() == 0)
88 jump_loc(ch_tell(), jump_sline);
89 error("Cannot seek to beginning of file", NULL_PARG);
92 parg.p_linenum = linenum;
93 error("Cannot seek to line number %n", &parg);
103 struct scrpos scrpos;
105 * Start at the line currently at the top of the screen
106 * and redisplay the screen.
110 jump_loc(scrpos.pos, scrpos.ln);
114 * Jump to a specified percentage into the file.
117 jump_percent(percent, fraction)
124 * Determine the position in the file
125 * (the specified percentage of the file's length).
127 if ((len = ch_length()) == NULL_POSITION)
129 ierror("Determining length of file", NULL_PARG);
132 if ((len = ch_length()) == NULL_POSITION)
134 error("Don't know length of file", NULL_PARG);
137 pos = percent_pos(len, percent, fraction);
141 jump_line_loc(pos, jump_sline);
145 * Jump to a specified position in the file.
146 * Like jump_loc, but the position need not be
147 * the first character in a line.
150 jump_line_loc(pos, sline)
156 if (ch_seek(pos) == 0)
159 * Back up to the beginning of the line.
161 while ((c = ch_back_get()) != '\n' && c != EOI)
164 (void) ch_forw_get();
169 jump_loc(pos, sline);
173 * Jump to a specified position in the file.
174 * The position must be the first character in a line.
175 * Place the target line on a specified line on the screen.
189 sline = adjsline(sline);
191 if ((nline = onscreen(pos)) >= 0)
194 * The line is currently displayed.
199 forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0);
201 back(-nline, position(TOP), 1, 0);
210 * Line is not on screen.
211 * Seek to the desired location.
215 error("Cannot seek to that file position", NULL_PARG);
220 * See if the desired line is before or after
221 * the currently displayed screen.
223 tpos = position(TOP);
224 bpos = position(BOTTOM_PLUS_ONE);
225 if (tpos == NULL_POSITION || pos >= tpos)
228 * The desired line is after the current screen.
229 * Move back in the file far enough so that we can
230 * call forw() and put the desired line at the
231 * sline-th line on the screen.
233 for (nline = 0; nline < sline; nline++)
235 if (bpos != NULL_POSITION && pos <= bpos)
238 * Surprise! The desired line is
239 * close enough to the current screen
240 * that we can just scroll there after all.
242 forw(sc_height-sline+nline-1, bpos, 1, 0, 0);
249 pos = back_line(pos);
250 if (pos == NULL_POSITION)
253 * Oops. Ran into the beginning of the file.
254 * Exit the loop here and rely on forw()
255 * below to draw the required number of
256 * blank lines at the top of the screen.
264 forw(sc_height-1, pos, 1, 0, sline-nline);
268 * The desired line is before the current screen.
269 * Move forward in the file far enough so that we
270 * can call back() and put the desired line at the
271 * sline-th line on the screen.
273 for (nline = sline; nline < sc_height - 1; nline++)
275 pos = forw_line(pos);
276 if (pos == NULL_POSITION)
279 * Ran into end of file.
280 * This shouldn't normally happen,
281 * but may if there is some kind of read error.
288 * Surprise! The desired line is
289 * close enough to the current screen
290 * that we can just scroll there after all.
292 back(nline+1, tpos, 1, 0);
307 back(sc_height-1, pos, 1, 0);