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> |
5 |
|
|
* |
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. |
8 |
|
|
* |
9 |
|
|
* For more information, see the README file. |
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 |
|
|
|
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; |
25 |
|
|
|
26 |
|
|
/* |
27 |
|
|
* Jump to the end of the file. |
28 |
|
|
*/ |
29 |
|
|
void |
30 |
|
|
jump_forw(void) |
31 |
|
|
{ |
32 |
|
|
off_t pos; |
33 |
|
|
off_t end_pos; |
34 |
|
|
|
35 |
✗✓ |
22 |
if (ch_end_seek()) { |
36 |
|
|
error("Cannot seek to end of file", NULL); |
37 |
|
|
return; |
38 |
|
|
} |
39 |
|
|
/* |
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. |
43 |
|
|
*/ |
44 |
|
11 |
lastmark(); |
45 |
|
|
/* |
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. |
49 |
|
|
*/ |
50 |
|
11 |
pos_clear(); |
51 |
|
11 |
end_pos = ch_tell(); |
52 |
|
11 |
pos = back_line(end_pos); |
53 |
✗✓ |
11 |
if (pos == -1) { |
54 |
|
|
jump_loc(0, sc_height-1); |
55 |
|
|
} else { |
56 |
|
11 |
jump_loc(pos, sc_height-1); |
57 |
✗✓ |
11 |
if (position(sc_height-1) != end_pos) |
58 |
|
|
repaint(); |
59 |
|
|
} |
60 |
|
22 |
} |
61 |
|
|
|
62 |
|
|
/* |
63 |
|
|
* Jump to line n in the file. |
64 |
|
|
*/ |
65 |
|
|
void |
66 |
|
|
jump_back(off_t linenum) |
67 |
|
|
{ |
68 |
|
|
off_t pos; |
69 |
|
10 |
PARG parg; |
70 |
|
|
|
71 |
|
|
/* |
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. |
76 |
|
|
*/ |
77 |
|
5 |
pos = find_pos(linenum); |
78 |
✓✗✓✗
|
10 |
if (pos != -1 && ch_seek(pos) == 0) { |
79 |
✗✓ |
5 |
if (show_attn) |
80 |
|
|
set_attnpos(pos); |
81 |
|
5 |
jump_loc(pos, jump_sline); |
82 |
✗✗✗✗
|
5 |
} 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); |
88 |
|
|
} |
89 |
|
5 |
} |
90 |
|
|
|
91 |
|
|
/* |
92 |
|
|
* Repaint the screen. |
93 |
|
|
*/ |
94 |
|
|
void |
95 |
|
|
repaint(void) |
96 |
|
|
{ |
97 |
|
144 |
struct scrpos scrpos; |
98 |
|
|
/* |
99 |
|
|
* Start at the line currently at the top of the screen |
100 |
|
|
* and redisplay the screen. |
101 |
|
|
*/ |
102 |
|
72 |
get_scrpos(&scrpos); |
103 |
|
72 |
pos_clear(); |
104 |
|
72 |
jump_loc(scrpos.pos, scrpos.ln); |
105 |
|
72 |
} |
106 |
|
|
|
107 |
|
|
/* |
108 |
|
|
* Jump to a specified percentage into the file. |
109 |
|
|
*/ |
110 |
|
|
void |
111 |
|
|
jump_percent(int percent, long fraction) |
112 |
|
|
{ |
113 |
|
|
off_t pos, len; |
114 |
|
|
|
115 |
|
|
/* |
116 |
|
|
* Determine the position in the file |
117 |
|
|
* (the specified percentage of the file's length). |
118 |
|
|
*/ |
119 |
✓✗ |
2 |
if ((len = ch_length()) == -1) { |
120 |
|
1 |
ierror("Determining length of file", NULL); |
121 |
|
1 |
ch_end_seek(); |
122 |
|
1 |
} |
123 |
✗✓ |
1 |
if ((len = ch_length()) == -1) { |
124 |
|
|
error("Don't know length of file", NULL); |
125 |
|
|
return; |
126 |
|
|
} |
127 |
|
1 |
pos = percent_pos(len, percent, fraction); |
128 |
✗✓ |
1 |
if (pos >= len) |
129 |
|
|
pos = len-1; |
130 |
|
|
|
131 |
|
1 |
jump_line_loc(pos, jump_sline); |
132 |
|
2 |
} |
133 |
|
|
|
134 |
|
|
/* |
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. |
138 |
|
|
*/ |
139 |
|
|
void |
140 |
|
|
jump_line_loc(off_t pos, int sline) |
141 |
|
|
{ |
142 |
|
|
int c; |
143 |
|
|
|
144 |
✓✗ |
2 |
if (ch_seek(pos) == 0) { |
145 |
|
|
/* |
146 |
|
|
* Back up to the beginning of the line. |
147 |
|
|
*/ |
148 |
✗✓ |
1 |
while ((c = ch_back_get()) != '\n' && c != EOI) |
149 |
|
|
; |
150 |
✗✓ |
1 |
if (c == '\n') |
151 |
|
|
(void) ch_forw_get(); |
152 |
|
1 |
pos = ch_tell(); |
153 |
|
1 |
} |
154 |
✗✓ |
1 |
if (show_attn) |
155 |
|
|
set_attnpos(pos); |
156 |
|
1 |
jump_loc(pos, sline); |
157 |
|
1 |
} |
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 |
|
|
void |
165 |
|
|
jump_loc(off_t pos, int sline) |
166 |
|
|
{ |
167 |
|
|
int nline; |
168 |
|
|
off_t tpos; |
169 |
|
|
off_t bpos; |
170 |
|
|
|
171 |
|
|
/* |
172 |
|
|
* Normalize sline. |
173 |
|
|
*/ |
174 |
|
274 |
sline = adjsline(sline); |
175 |
|
|
|
176 |
✓✓ |
137 |
if ((nline = onscreen(pos)) >= 0) { |
177 |
|
|
/* |
178 |
|
|
* The line is currently displayed. |
179 |
|
|
* Just scroll there. |
180 |
|
|
*/ |
181 |
|
9 |
nline -= sline; |
182 |
✓✓ |
9 |
if (nline > 0) |
183 |
|
5 |
forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0); |
184 |
|
|
else |
185 |
|
4 |
back(-nline, position(TOP), 1, 0); |
186 |
✗✓ |
9 |
if (show_attn) |
187 |
|
|
repaint_hilite(1); |
188 |
|
9 |
return; |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
/* |
192 |
|
|
* Line is not on screen. |
193 |
|
|
* Seek to the desired location. |
194 |
|
|
*/ |
195 |
✗✓ |
128 |
if (ch_seek(pos)) { |
196 |
|
|
error("Cannot seek to that file position", NULL); |
197 |
|
|
return; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
/* |
201 |
|
|
* See if the desired line is before or after |
202 |
|
|
* the currently displayed screen. |
203 |
|
|
*/ |
204 |
|
128 |
tpos = position(TOP); |
205 |
|
128 |
bpos = position(BOTTOM_PLUS_ONE); |
206 |
✓✓✓✓
|
136 |
if (tpos == -1 || pos >= tpos) { |
207 |
|
|
/* |
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. |
212 |
|
|
*/ |
213 |
✓✓ |
934 |
for (nline = 0; nline < sline; nline++) { |
214 |
✗✓✗✗
|
341 |
if (bpos != -1 && pos <= bpos) { |
215 |
|
|
/* |
216 |
|
|
* Surprise! The desired line is |
217 |
|
|
* close enough to the current screen |
218 |
|
|
* that we can just scroll there after all. |
219 |
|
|
*/ |
220 |
|
|
forw(sc_height-sline+nline-1, bpos, 1, 0, 0); |
221 |
|
|
if (show_attn) |
222 |
|
|
repaint_hilite(1); |
223 |
|
|
return; |
224 |
|
|
} |
225 |
|
341 |
pos = back_line(pos); |
226 |
✓✗ |
341 |
if (pos == -1) { |
227 |
|
|
/* |
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. |
232 |
|
|
*/ |
233 |
|
|
break; |
234 |
|
|
} |
235 |
|
|
} |
236 |
|
126 |
lastmark(); |
237 |
|
126 |
squished = 0; |
238 |
|
126 |
screen_trashed = 0; |
239 |
|
126 |
forw(sc_height-1, pos, 1, 0, sline-nline); |
240 |
|
126 |
} else { |
241 |
|
|
/* |
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. |
246 |
|
|
*/ |
247 |
✓✓ |
130 |
for (nline = sline; nline < sc_height - 1; nline++) { |
248 |
|
64 |
pos = forw_line(pos); |
249 |
✓✗ |
64 |
if (pos == -1) { |
250 |
|
|
/* |
251 |
|
|
* Ran into end of file. |
252 |
|
|
* This shouldn't normally happen, |
253 |
|
|
* but may if there is some kind of read error. |
254 |
|
|
*/ |
255 |
|
|
break; |
256 |
|
|
} |
257 |
✓✓ |
64 |
if (pos >= tpos) { |
258 |
|
|
/* |
259 |
|
|
* Surprise! The desired line is |
260 |
|
|
* close enough to the current screen |
261 |
|
|
* that we can just scroll there after all. |
262 |
|
|
*/ |
263 |
|
1 |
back(nline + 1, tpos, 1, 0); |
264 |
✗✓ |
1 |
if (show_attn) |
265 |
|
|
repaint_hilite(1); |
266 |
|
1 |
return; |
267 |
|
|
} |
268 |
|
|
} |
269 |
|
1 |
lastmark(); |
270 |
✗✓ |
1 |
if (!top_scroll) |
271 |
|
|
do_clear(); |
272 |
|
|
else |
273 |
|
1 |
home(); |
274 |
|
1 |
screen_trashed = 0; |
275 |
|
1 |
add_back_pos(pos); |
276 |
|
1 |
back(sc_height-1, pos, 1, 0); |
277 |
|
|
} |
278 |
|
264 |
} |