8888 loader: rename STAND to _STANDALONE
[unleashed.git] / bin / less / jump.c
bloba4a60eeb991a0a342da100005b965ab75ab88f36
1 /*
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.
16 #include "less.h"
17 #include "position.h"
19 extern int jump_sline;
20 extern int squished;
21 extern int screen_trashed;
22 extern int sc_width, sc_height;
23 extern int show_attn;
24 extern int top_scroll;
27 * Jump to the end of the file.
29 void
30 jump_forw(void)
32 off_t pos;
33 off_t end_pos;
35 if (ch_end_seek()) {
36 error("Cannot seek to end of file", NULL);
37 return;
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.
44 lastmark();
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.
50 pos_clear();
51 end_pos = ch_tell();
52 pos = back_line(end_pos);
53 if (pos == -1) {
54 jump_loc(0, sc_height-1);
55 } else {
56 jump_loc(pos, sc_height-1);
57 if (position(sc_height-1) != end_pos)
58 repaint();
63 * Jump to line n in the file.
65 void
66 jump_back(off_t linenum)
68 off_t pos;
69 PARG parg;
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) {
79 if (show_attn)
80 set_attnpos(pos);
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);
85 } else {
86 parg.p_linenum = linenum;
87 error("Cannot seek to line number %n", &parg);
92 * Repaint the screen.
94 void
95 repaint(void)
97 struct scrpos scrpos;
99 * Start at the line currently at the top of the screen
100 * and redisplay the screen.
102 get_scrpos(&scrpos);
103 pos_clear();
104 jump_loc(scrpos.pos, scrpos.ln);
108 * Jump to a specified percentage into the file.
110 void
111 jump_percent(int percent, long fraction)
113 off_t pos, len;
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);
121 ch_end_seek();
123 if ((len = ch_length()) == -1) {
124 error("Don't know length of file", NULL);
125 return;
127 pos = percent_pos(len, percent, fraction);
128 if (pos >= len)
129 pos = len-1;
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.
139 void
140 jump_line_loc(off_t pos, int sline)
142 int c;
144 if (ch_seek(pos) == 0) {
146 * Back up to the beginning of the line.
148 while ((c = ch_back_get()) != '\n' && c != EOI)
150 if (c == '\n')
151 (void) ch_forw_get();
152 pos = ch_tell();
154 if (show_attn)
155 set_attnpos(pos);
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.
164 void
165 jump_loc(off_t pos, int sline)
167 int nline;
168 off_t tpos;
169 off_t bpos;
172 * Normalize sline.
174 sline = adjsline(sline);
176 if ((nline = onscreen(pos)) >= 0) {
178 * The line is currently displayed.
179 * Just scroll there.
181 nline -= sline;
182 if (nline > 0)
183 forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0);
184 else
185 back(-nline, position(TOP), 1, 0);
186 if (show_attn)
187 repaint_hilite(1);
188 return;
192 * Line is not on screen.
193 * Seek to the desired location.
195 if (ch_seek(pos)) {
196 error("Cannot seek to that file position", NULL);
197 return;
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);
221 if (show_attn)
222 repaint_hilite(1);
223 return;
225 pos = back_line(pos);
226 if (pos == -1) {
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.
233 break;
236 lastmark();
237 squished = 0;
238 screen_trashed = 0;
239 forw(sc_height-1, pos, 1, 0, sline-nline);
240 } else {
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);
249 if (pos == -1) {
251 * Ran into end of file.
252 * This shouldn't normally happen,
253 * but may if there is some kind of read error.
255 break;
257 if (pos >= tpos) {
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);
264 if (show_attn)
265 repaint_hilite(1);
266 return;
269 lastmark();
270 if (!top_scroll)
271 do_clear();
272 else
273 home();
274 screen_trashed = 0;
275 add_back_pos(pos);
276 back(sc_height-1, pos, 1, 0);