1 /* $NetBSD: jump.c,v 1.4 2013/09/04 20:02:10 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * 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
;
25 extern int top_scroll
;
28 * Jump to the end of the file.
38 error("Cannot seek to end of file", NULL_PARG
);
42 * Note; lastmark will be called later by jump_loc, but it fails
43 * because the position table has been cleared by pos_clear below.
44 * So call it here before calling pos_clear.
48 * Position the last line in the file at the last screen line.
49 * Go back one line from the end of the file
50 * to get to the beginning of the last line.
54 pos
= back_line(end_pos
);
55 if (pos
== NULL_POSITION
)
56 jump_loc((POSITION
)0, sc_height
-1);
59 jump_loc(pos
, sc_height
-1);
60 if (position(sc_height
-1) != end_pos
)
66 * Jump to line n in the file.
76 * Find the position of the specified line.
77 * If we can seek there, just jump to it.
78 * If we can't seek, but we're trying to go to line number 1,
79 * use ch_beg_seek() to get as close as we can.
81 pos
= find_pos(linenum
);
82 if (pos
!= NULL_POSITION
&& ch_seek(pos
) == 0)
86 jump_loc(pos
, jump_sline
);
87 } else if (linenum
<= 1 && ch_beg_seek() == 0)
89 jump_loc(ch_tell(), jump_sline
);
90 error("Cannot seek to beginning of file", NULL_PARG
);
93 parg
.p_linenum
= linenum
;
94 error("Cannot seek to line number %n", &parg
);
104 struct scrpos scrpos
;
106 * Start at the line currently at the top of the screen
107 * and redisplay the screen.
111 jump_loc(scrpos
.pos
, scrpos
.ln
);
115 * Jump to a specified percentage into the file.
118 jump_percent(percent
, fraction
)
125 * Determine the position in the file
126 * (the specified percentage of the file's length).
128 if ((len
= ch_length()) == NULL_POSITION
)
130 ierror("Determining length of file", NULL_PARG
);
133 if ((len
= ch_length()) == NULL_POSITION
)
135 error("Don't know length of file", NULL_PARG
);
138 pos
= percent_pos(len
, percent
, fraction
);
142 jump_line_loc(pos
, jump_sline
);
146 * Jump to a specified position in the file.
147 * Like jump_loc, but the position need not be
148 * the first character in a line.
151 jump_line_loc(pos
, sline
)
157 if (ch_seek(pos
) == 0)
160 * Back up to the beginning of the line.
162 while ((c
= ch_back_get()) != '\n' && c
!= EOI
)
165 (void) ch_forw_get();
170 jump_loc(pos
, sline
);
174 * Jump to a specified position in the file.
175 * The position must be the first character in a line.
176 * Place the target line on a specified line on the screen.
190 sline
= adjsline(sline
);
192 if ((nline
= onscreen(pos
)) >= 0)
195 * The line is currently displayed.
200 forw(nline
, position(BOTTOM_PLUS_ONE
), 1, 0, 0);
202 back(-nline
, position(TOP
), 1, 0);
211 * Line is not on screen.
212 * Seek to the desired location.
216 error("Cannot seek to that file position", NULL_PARG
);
221 * See if the desired line is before or after
222 * the currently displayed screen.
224 tpos
= position(TOP
);
225 bpos
= position(BOTTOM_PLUS_ONE
);
226 if (tpos
== NULL_POSITION
|| pos
>= tpos
)
229 * The desired line is after the current screen.
230 * Move back in the file far enough so that we can
231 * call forw() and put the desired line at the
232 * sline-th line on the screen.
234 for (nline
= 0; nline
< sline
; nline
++)
236 if (bpos
!= NULL_POSITION
&& pos
<= bpos
)
239 * Surprise! The desired line is
240 * close enough to the current screen
241 * that we can just scroll there after all.
243 forw(sc_height
-sline
+nline
-1, bpos
, 1, 0, 0);
250 pos
= back_line(pos
);
251 if (pos
== NULL_POSITION
)
254 * Oops. Ran into the beginning of the file.
255 * Exit the loop here and rely on forw()
256 * below to draw the required number of
257 * blank lines at the top of the screen.
265 forw(sc_height
-1, pos
, 1, 0, sline
-nline
);
269 * The desired line is before the current screen.
270 * Move forward in the file far enough so that we
271 * can call back() and put the desired line at the
272 * sline-th line on the screen.
274 for (nline
= sline
; nline
< sc_height
- 1; nline
++)
276 pos
= forw_line(pos
);
277 if (pos
== NULL_POSITION
)
280 * Ran into end of file.
281 * This shouldn't normally happen,
282 * but may if there is some kind of read error.
289 * Surprise! The desired line is
290 * close enough to the current screen
291 * that we can just scroll there after all.
293 back(nline
+1, tpos
, 1, 0);
308 back(sc_height
-1, pos
, 1, 0);