2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
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.
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.
36 error("Cannot seek to end of file", NULL
);
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
);
54 jump_loc(0, sc_height
-1);
56 jump_loc(pos
, sc_height
-1);
57 if (position(sc_height
-1) != end_pos
)
63 * Jump to line n in the file.
66 jump_back(off_t linenum
)
72 * Find the position of the specified line.
73 * If we can seek there, just jump to it.
74 * If we can't seek, but we're trying to go to line number 1,
75 * use ch_beg_seek() to get as close as we can.
77 pos
= find_pos(linenum
);
78 if (pos
!= -1 && ch_seek(pos
) == 0) {
81 jump_loc(pos
, jump_sline
);
82 } else if (linenum
<= 1 && ch_beg_seek() == 0) {
83 jump_loc(ch_tell(), jump_sline
);
84 error("Cannot seek to beginning of file", NULL
);
86 parg
.p_linenum
= linenum
;
87 error("Cannot seek to line number %n", &parg
);
99 * Start at the line currently at the top of the screen
100 * and redisplay the screen.
104 jump_loc(scrpos
.pos
, scrpos
.ln
);
108 * Jump to a specified percentage into the file.
111 jump_percent(int percent
, long fraction
)
116 * Determine the position in the file
117 * (the specified percentage of the file's length).
119 if ((len
= ch_length()) == -1) {
120 ierror("Determining length of file", NULL
);
123 if ((len
= ch_length()) == -1) {
124 error("Don't know length of file", NULL
);
127 pos
= percent_pos(len
, percent
, fraction
);
131 jump_line_loc(pos
, jump_sline
);
135 * Jump to a specified position in the file.
136 * Like jump_loc, but the position need not be
137 * the first character in a line.
140 jump_line_loc(off_t pos
, int sline
)
144 if (ch_seek(pos
) == 0) {
146 * Back up to the beginning of the line.
148 while ((c
= ch_back_get()) != '\n' && c
!= EOI
)
151 (void) ch_forw_get();
156 jump_loc(pos
, sline
);
160 * Jump to a specified position in the file.
161 * The position must be the first character in a line.
162 * Place the target line on a specified line on the screen.
165 jump_loc(off_t pos
, int sline
)
174 sline
= adjsline(sline
);
176 if ((nline
= onscreen(pos
)) >= 0) {
178 * The line is currently displayed.
183 forw(nline
, position(BOTTOM_PLUS_ONE
), 1, 0, 0);
185 back(-nline
, position(TOP
), 1, 0);
192 * Line is not on screen.
193 * Seek to the desired location.
196 error("Cannot seek to that file position", NULL
);
201 * See if the desired line is before or after
202 * the currently displayed screen.
204 tpos
= position(TOP
);
205 bpos
= position(BOTTOM_PLUS_ONE
);
206 if (tpos
== -1 || pos
>= tpos
) {
208 * The desired line is after the current screen.
209 * Move back in the file far enough so that we can
210 * call forw() and put the desired line at the
211 * sline-th line on the screen.
213 for (nline
= 0; nline
< sline
; nline
++) {
214 if (bpos
!= -1 && pos
<= bpos
) {
216 * Surprise! The desired line is
217 * close enough to the current screen
218 * that we can just scroll there after all.
220 forw(sc_height
-sline
+nline
-1, bpos
, 1, 0, 0);
225 pos
= back_line(pos
);
228 * Oops. Ran into the beginning of the file.
229 * Exit the loop here and rely on forw()
230 * below to draw the required number of
231 * blank lines at the top of the screen.
239 forw(sc_height
-1, pos
, 1, 0, sline
-nline
);
242 * The desired line is before the current screen.
243 * Move forward in the file far enough so that we
244 * can call back() and put the desired line at the
245 * sline-th line on the screen.
247 for (nline
= sline
; nline
< sc_height
- 1; nline
++) {
248 pos
= forw_line(pos
);
251 * Ran into end of file.
252 * This shouldn't normally happen,
253 * but may if there is some kind of read error.
259 * Surprise! The desired line is
260 * close enough to the current screen
261 * that we can just scroll there after all.
263 back(nline
+ 1, tpos
, 1, 0);
276 back(sc_height
-1, pos
, 1, 0);