Add our READMEs.
[dragonfly/port-amd64.git] / contrib / less-403 / jump.c
blob6e0c90dbcc10baff6a0c5efcce2a234640502d64
1 /*
2 * Copyright (C) 1984-2007 Mark Nudelman
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.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * Routines which jump to a new location in the file.
16 #include "less.h"
17 #include "position.h"
19 extern int hit_eof;
20 extern int jump_sline;
21 extern int squished;
22 extern int screen_trashed;
23 extern int sc_width, sc_height;
24 extern int show_attn;
25 extern int top_scroll;
28 * Jump to the end of the file.
30 public void
31 jump_forw()
33 POSITION pos;
35 if (ch_end_seek())
37 error("Cannot seek to end of file", NULL_PARG);
38 return;
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.
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);
53 * Jump to line n in the file.
55 public void
56 jump_back(linenum)
57 LINENUM linenum;
59 POSITION pos;
60 PARG parg;
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.
68 pos = find_pos(linenum);
69 if (pos != NULL_POSITION && ch_seek(pos) == 0)
71 if (show_attn)
72 set_attnpos(pos);
73 jump_loc(pos, jump_sline);
74 } else if (linenum <= 1 && ch_beg_seek() == 0)
76 jump_loc(ch_tell(), jump_sline);
77 error("Cannot seek to beginning of file", NULL_PARG);
78 } else
80 parg.p_linenum = linenum;
81 error("Cannot seek to line number %n", &parg);
86 * Repaint the screen.
88 public void
89 repaint()
91 struct scrpos scrpos;
93 * Start at the line currently at the top of the screen
94 * and redisplay the screen.
96 get_scrpos(&scrpos);
97 pos_clear();
98 jump_loc(scrpos.pos, scrpos.ln);
102 * Jump to a specified percentage into the file.
104 public void
105 jump_percent(percent, fraction)
106 int percent;
107 long fraction;
109 POSITION pos, len;
112 * Determine the position in the file
113 * (the specified percentage of the file's length).
115 if ((len = ch_length()) == NULL_POSITION)
117 ierror("Determining length of file", NULL_PARG);
118 ch_end_seek();
120 if ((len = ch_length()) == NULL_POSITION)
122 error("Don't know length of file", NULL_PARG);
123 return;
125 pos = percent_pos(len, percent, fraction);
126 if (pos >= len)
127 pos = len-1;
129 jump_line_loc(pos, jump_sline);
133 * Jump to a specified position in the file.
134 * Like jump_loc, but the position need not be
135 * the first character in a line.
137 public void
138 jump_line_loc(pos, sline)
139 POSITION pos;
140 int sline;
142 int c;
144 if (ch_seek(pos) == 0)
147 * Back up to the beginning of the line.
149 while ((c = ch_back_get()) != '\n' && c != EOI)
151 if (c == '\n')
152 (void) ch_forw_get();
153 pos = ch_tell();
155 if (show_attn)
156 set_attnpos(pos);
157 jump_loc(pos, sline);
161 * Jump to a specified position in the file.
162 * The position must be the first character in a line.
163 * Place the target line on a specified line on the screen.
165 public void
166 jump_loc(pos, sline)
167 POSITION pos;
168 int sline;
170 register int nline;
171 POSITION tpos;
172 POSITION bpos;
175 * Normalize sline.
177 sline = adjsline(sline);
179 if ((nline = onscreen(pos)) >= 0)
182 * The line is currently displayed.
183 * Just scroll there.
185 nline -= sline;
186 if (nline > 0)
187 forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0);
188 else
189 back(-nline, position(TOP), 1, 0);
190 if (show_attn)
191 repaint_hilite(1);
192 return;
196 * Line is not on screen.
197 * Seek to the desired location.
199 if (ch_seek(pos))
201 error("Cannot seek to that file position", NULL_PARG);
202 return;
206 * See if the desired line is before or after
207 * the currently displayed screen.
209 tpos = position(TOP);
210 bpos = position(BOTTOM_PLUS_ONE);
211 if (tpos == NULL_POSITION || pos >= tpos)
214 * The desired line is after the current screen.
215 * Move back in the file far enough so that we can
216 * call forw() and put the desired line at the
217 * sline-th line on the screen.
219 for (nline = 0; nline < sline; nline++)
221 if (bpos != NULL_POSITION && pos <= bpos)
224 * Surprise! The desired line is
225 * close enough to the current screen
226 * that we can just scroll there after all.
228 forw(sc_height-sline+nline-1, bpos, 1, 0, 0);
229 if (show_attn)
230 repaint_hilite(1);
231 return;
233 pos = back_line(pos);
234 if (pos == NULL_POSITION)
237 * Oops. Ran into the beginning of the file.
238 * Exit the loop here and rely on forw()
239 * below to draw the required number of
240 * blank lines at the top of the screen.
242 break;
245 lastmark();
246 hit_eof = 0;
247 squished = 0;
248 screen_trashed = 0;
249 forw(sc_height-1, pos, 1, 0, sline-nline);
250 } else
253 * The desired line is before the current screen.
254 * Move forward in the file far enough so that we
255 * can call back() and put the desired line at the
256 * sline-th line on the screen.
258 for (nline = sline; nline < sc_height - 1; nline++)
260 pos = forw_line(pos);
261 if (pos == NULL_POSITION)
264 * Ran into end of file.
265 * This shouldn't normally happen,
266 * but may if there is some kind of read error.
268 break;
270 if (pos >= tpos)
273 * Surprise! The desired line is
274 * close enough to the current screen
275 * that we can just scroll there after all.
277 back(nline+1, tpos, 1, 0);
278 if (show_attn)
279 repaint_hilite(1);
280 return;
283 lastmark();
284 if (!top_scroll)
285 clear();
286 else
287 home();
288 screen_trashed = 0;
289 add_back_pos(pos);
290 back(sc_height-1, pos, 1, 0);