2 * Copyright (C) 1984-2002 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.
20 extern int jump_sline
;
22 extern int screen_trashed
;
23 extern int sc_width
, sc_height
;
27 * Jump to the end of the file.
36 error("Cannot seek to end of file", NULL_PARG
);
40 * Position the last line in the file at the last screen line.
41 * Go back one line from the end of the file
42 * to get to the beginning of the last line.
44 pos
= back_line(ch_tell());
45 if (pos
== NULL_POSITION
)
46 jump_loc((POSITION
)0, sc_height
-1);
48 jump_loc(pos
, sc_height
-1);
52 * Jump to line n in the file.
62 * Find the position of the specified line.
63 * If we can seek there, just jump to it.
64 * If we can't seek, but we're trying to go to line number 1,
65 * use ch_beg_seek() to get as close as we can.
67 pos
= find_pos(linenum
);
68 if (pos
!= NULL_POSITION
&& ch_seek(pos
) == 0)
72 jump_loc(pos
, jump_sline
);
73 } else if (linenum
<= 1 && ch_beg_seek() == 0)
75 jump_loc(ch_tell(), jump_sline
);
76 error("Cannot seek to beginning of file", NULL_PARG
);
79 parg
.p_linenum
= linenum
;
80 error("Cannot seek to line number %n", &parg
);
92 * Start at the line currently at the top of the screen
93 * and redisplay the screen.
97 jump_loc(scrpos
.pos
, scrpos
.ln
);
101 * Jump to a specified percentage into the file.
104 jump_percent(percent
)
110 * Determine the position in the file
111 * (the specified percentage of the file's length).
113 if ((len
= ch_length()) == NULL_POSITION
)
115 ierror("Determining length of file", NULL_PARG
);
118 if ((len
= ch_length()) == NULL_POSITION
)
120 error("Don't know length of file", NULL_PARG
);
123 pos
= percent_pos(len
, percent
);
127 jump_line_loc(pos
, jump_sline
);
131 * Jump to a specified position in the file.
132 * Like jump_loc, but the position need not be
133 * the first character in a line.
136 jump_line_loc(pos
, sline
)
142 if (ch_seek(pos
) == 0)
145 * Back up to the beginning of the line.
147 while ((c
= ch_back_get()) != '\n' && c
!= EOI
)
150 (void) ch_forw_get();
155 jump_loc(pos
, sline
);
159 * Jump to a specified position in the file.
160 * The position must be the first character in a line.
161 * Place the target line on a specified line on the screen.
175 sline
= adjsline(sline
);
177 if ((nline
= onscreen(pos
)) >= 0)
180 * The line is currently displayed.
185 forw(nline
, position(BOTTOM_PLUS_ONE
), 1, 0, 0);
187 back(-nline
, position(TOP
), 1, 0);
194 * Line is not on screen.
195 * Seek to the desired location.
199 error("Cannot seek to that file position", NULL_PARG
);
204 * See if the desired line is before or after
205 * the currently displayed screen.
207 tpos
= position(TOP
);
208 bpos
= position(BOTTOM_PLUS_ONE
);
209 if (tpos
== NULL_POSITION
|| pos
>= tpos
)
212 * The desired line is after the current screen.
213 * Move back in the file far enough so that we can
214 * call forw() and put the desired line at the
215 * sline-th line on the screen.
217 for (nline
= 0; nline
< sline
; nline
++)
219 if (bpos
!= NULL_POSITION
&& pos
<= bpos
)
222 * Surprise! The desired line is
223 * close enough to the current screen
224 * that we can just scroll there after all.
226 forw(sc_height
-sline
+nline
-1, bpos
, 1, 0, 0);
231 pos
= back_line(pos
);
232 if (pos
== NULL_POSITION
)
235 * Oops. Ran into the beginning of the file.
236 * Exit the loop here and rely on forw()
237 * below to draw the required number of
238 * blank lines at the top of the screen.
247 forw(sc_height
-1, pos
, 1, 0, sline
-nline
);
251 * The desired line is before the current screen.
252 * Move forward in the file far enough so that we
253 * can call back() and put the desired line at the
254 * sline-th line on the screen.
256 for (nline
= sline
; nline
< sc_height
- 1; nline
++)
258 pos
= forw_line(pos
);
259 if (pos
== NULL_POSITION
)
262 * Ran into end of file.
263 * This shouldn't normally happen,
264 * but may if there is some kind of read error.
271 * Surprise! The desired line is
272 * close enough to the current screen
273 * that we can just scroll there after all.
275 back(nline
+1, tpos
, 1, 0);
285 back(sc_height
-1, pos
, 1, 0);