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