Deleted Added
sdiff udiff text old ( 161475 ) new ( 170256 )
full compact
1/*
2 * Copyright (C) 1984-2005 Mark Nudelman
3 *
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.
6 *
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
10
11
12/*
13 * Routines which jump to a new location in the file.
14 */
15
16#include "less.h"
17#include "position.h"
18
19extern int hit_eof;
20extern int jump_sline;
21extern int squished;
22extern int screen_trashed;
23extern int sc_width, sc_height;
24extern int show_attn;
25extern int top_scroll;
26
27/*
28 * Jump to the end of the file.
29 */
30 public void
31jump_forw()
32{
33 POSITION pos;
34
35 if (ch_end_seek())
36 {
37 error("Cannot seek to end of file", NULL_PARG);
38 return;
39 }
40 /*
41 * Position the last line in the file at the last screen line.
42 * Go back one line from the end of the file
43 * to get to the beginning of the last line.
44 */
45 pos = back_line(ch_tell());
46 if (pos == NULL_POSITION)
47 jump_loc((POSITION)0, sc_height-1);
48 else
49 jump_loc(pos, sc_height-1);
50}
51
52/*
53 * Jump to line n in the file.
54 */
55 public void
56jump_back(linenum)
57 LINENUM linenum;
58{
59 POSITION pos;
60 PARG parg;
61
62 /*
63 * Find the position of the specified line.
64 * If we can seek there, just jump to it.
65 * If we can't seek, but we're trying to go to line number 1,
66 * use ch_beg_seek() to get as close as we can.
67 */
68 pos = find_pos(linenum);
69 if (pos != NULL_POSITION && ch_seek(pos) == 0)
70 {
71 if (show_attn)
72 set_attnpos(pos);
73 jump_loc(pos, jump_sline);
74 } else if (linenum <= 1 && ch_beg_seek() == 0)
75 {
76 jump_loc(ch_tell(), jump_sline);
77 error("Cannot seek to beginning of file", NULL_PARG);
78 } else
79 {
80 parg.p_linenum = linenum;
81 error("Cannot seek to line number %n", &parg);
82 }
83}
84
85/*
86 * Repaint the screen.
87 */
88 public void
89repaint()
90{
91 struct scrpos scrpos;
92 /*
93 * Start at the line currently at the top of the screen
94 * and redisplay the screen.
95 */
96 get_scrpos(&scrpos);
97 pos_clear();
98 jump_loc(scrpos.pos, scrpos.ln);
99}
100
101/*
102 * Jump to a specified percentage into the file.
103 */
104 public void
105jump_percent(percent)
106 int percent;
107{
108 POSITION pos, len;
109
110 /*
111 * Determine the position in the file
112 * (the specified percentage of the file's length).
113 */
114 if ((len = ch_length()) == NULL_POSITION)
115 {
116 ierror("Determining length of file", NULL_PARG);
117 ch_end_seek();
118 }
119 if ((len = ch_length()) == NULL_POSITION)
120 {
121 error("Don't know length of file", NULL_PARG);
122 return;
123 }
124 pos = percent_pos(len, percent);
125 if (pos >= len)
126 pos = len-1;
127
128 jump_line_loc(pos, jump_sline);
129}
130
131/*
132 * Jump to a specified position in the file.
133 * Like jump_loc, but the position need not be
134 * the first character in a line.
135 */
136 public void
137jump_line_loc(pos, sline)
138 POSITION pos;
139 int sline;
140{
141 int c;
142
143 if (ch_seek(pos) == 0)
144 {
145 /*
146 * Back up to the beginning of the line.
147 */
148 while ((c = ch_back_get()) != '\n' && c != EOI)
149 ;
150 if (c == '\n')
151 (void) ch_forw_get();
152 pos = ch_tell();
153 }
154 if (show_attn)
155 set_attnpos(pos);
156 jump_loc(pos, sline);
157}
158
159/*
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.
163 */
164 public void
165jump_loc(pos, sline)
166 POSITION pos;
167 int sline;
168{
169 register int nline;
170 POSITION tpos;
171 POSITION bpos;
172
173 /*
174 * Normalize sline.
175 */
176 sline = adjsline(sline);
177
178 if ((nline = onscreen(pos)) >= 0)
179 {
180 /*
181 * The line is currently displayed.
182 * Just scroll there.
183 */
184 nline -= sline;
185 if (nline > 0)
186 forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0);
187 else
188 back(-nline, position(TOP), 1, 0);
189 if (show_attn)
190 repaint_hilite(1);
191 return;
192 }
193
194 /*
195 * Line is not on screen.
196 * Seek to the desired location.
197 */
198 if (ch_seek(pos))
199 {
200 error("Cannot seek to that file position", NULL_PARG);
201 return;
202 }
203
204 /*
205 * See if the desired line is before or after
206 * the currently displayed screen.
207 */
208 tpos = position(TOP);
209 bpos = position(BOTTOM_PLUS_ONE);
210 if (tpos == NULL_POSITION || pos >= tpos)
211 {
212 /*
213 * The desired line is after the current screen.
214 * Move back in the file far enough so that we can
215 * call forw() and put the desired line at the
216 * sline-th line on the screen.
217 */
218 for (nline = 0; nline < sline; nline++)
219 {
220 if (bpos != NULL_POSITION && pos <= bpos)
221 {
222 /*
223 * Surprise! The desired line is
224 * close enough to the current screen
225 * that we can just scroll there after all.
226 */
227 forw(sc_height-sline+nline-1, bpos, 1, 0, 0);
228 if (show_attn)
229 repaint_hilite(1);
230 return;
231 }
232 pos = back_line(pos);
233 if (pos == NULL_POSITION)
234 {
235 /*
236 * Oops. Ran into the beginning of the file.
237 * Exit the loop here and rely on forw()
238 * below to draw the required number of
239 * blank lines at the top of the screen.
240 */
241 break;
242 }
243 }
244 lastmark();
245 hit_eof = 0;
246 squished = 0;
247 screen_trashed = 0;
248 forw(sc_height-1, pos, 1, 0, sline-nline);
249 } else
250 {
251 /*
252 * The desired line is before the current screen.
253 * Move forward in the file far enough so that we
254 * can call back() and put the desired line at the
255 * sline-th line on the screen.
256 */
257 for (nline = sline; nline < sc_height - 1; nline++)
258 {
259 pos = forw_line(pos);
260 if (pos == NULL_POSITION)
261 {
262 /*
263 * Ran into end of file.
264 * This shouldn't normally happen,
265 * but may if there is some kind of read error.
266 */
267 break;
268 }
269 if (pos >= tpos)
270 {
271 /*
272 * Surprise! The desired line is
273 * close enough to the current screen
274 * that we can just scroll there after all.
275 */
276 back(nline+1, tpos, 1, 0);
277 if (show_attn)
278 repaint_hilite(1);
279 return;
280 }
281 }
282 lastmark();
283 if (top_scroll != OPT_ON)
284 clear();
285 else
286 home();
287 screen_trashed = 0;
288 add_back_pos(pos);
289 back(sc_height-1, pos, 1, 0);
290 }
291}