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