7
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * VIM - Vi IMproved by Bram Moolenaar
|
|
4 *
|
|
5 * Do ":help uganda" in Vim to read copying and usage conditions.
|
|
6 * Do ":help credits" in Vim to see a list of people who contributed.
|
|
7 * See README.txt for an overview of the Vim source code.
|
|
8 */
|
|
9 /*
|
|
10 * move.c: Functions for moving the cursor and scrolling text.
|
|
11 *
|
|
12 * There are two ways to move the cursor:
|
|
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
|
|
14 * window.
|
|
15 * 2. Scroll the text, the cursor is moved into the text visible in the
|
|
16 * window.
|
|
17 * The 'scrolloff' option makes this a bit complicated.
|
|
18 */
|
|
19
|
|
20 #include "vim.h"
|
|
21
|
|
22 static void comp_botline __ARGS((win_T *wp));
|
|
23 static int check_top_offset __ARGS((void));
|
|
24 static void curs_rows __ARGS((win_T *wp, int do_botline));
|
|
25 static void validate_botline_win __ARGS((win_T *wp));
|
|
26 static void validate_cheight __ARGS((void));
|
|
27
|
|
28 typedef struct
|
|
29 {
|
|
30 linenr_T lnum; /* line number */
|
|
31 #ifdef FEAT_DIFF
|
|
32 int fill; /* filler lines */
|
|
33 #endif
|
|
34 int height; /* height of added line */
|
|
35 } lineoff_T;
|
|
36
|
|
37 static void topline_back __ARGS((lineoff_T *lp));
|
|
38 static void botline_forw __ARGS((lineoff_T *lp));
|
|
39 #ifdef FEAT_DIFF
|
|
40 static void botline_topline __ARGS((lineoff_T *lp));
|
|
41 static void topline_botline __ARGS((lineoff_T *lp));
|
|
42 static void max_topfill __ARGS((void));
|
|
43 #endif
|
|
44
|
|
45 /*
|
|
46 * Compute wp->w_botline for the current wp->w_topline. Can be called after
|
|
47 * wp->w_topline changed.
|
|
48 */
|
|
49 static void
|
|
50 comp_botline(wp)
|
|
51 win_T *wp;
|
|
52 {
|
|
53 int n;
|
|
54 linenr_T lnum;
|
|
55 int done;
|
|
56 #ifdef FEAT_FOLDING
|
|
57 linenr_T last;
|
|
58 int folded;
|
|
59 #endif
|
|
60
|
|
61 /*
|
|
62 * If w_cline_row is valid, start there.
|
|
63 * Otherwise have to start at w_topline.
|
|
64 */
|
|
65 check_cursor_moved(wp);
|
|
66 if (wp->w_valid & VALID_CROW)
|
|
67 {
|
|
68 lnum = wp->w_cursor.lnum;
|
|
69 done = wp->w_cline_row;
|
|
70 }
|
|
71 else
|
|
72 {
|
|
73 lnum = wp->w_topline;
|
|
74 done = 0;
|
|
75 }
|
|
76
|
|
77 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
|
|
78 {
|
|
79 #ifdef FEAT_FOLDING
|
|
80 last = lnum;
|
|
81 folded = FALSE;
|
|
82 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
|
|
83 {
|
|
84 n = 1;
|
|
85 folded = TRUE;
|
|
86 }
|
|
87 else
|
|
88 #endif
|
|
89 #ifdef FEAT_DIFF
|
|
90 if (lnum == wp->w_topline)
|
|
91 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
|
|
92 else
|
|
93 #endif
|
|
94 n = plines_win(wp, lnum, TRUE);
|
|
95 if (
|
|
96 #ifdef FEAT_FOLDING
|
|
97 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
|
|
98 #else
|
|
99 lnum == wp->w_cursor.lnum
|
|
100 #endif
|
|
101 )
|
|
102 {
|
|
103 wp->w_cline_row = done;
|
|
104 wp->w_cline_height = n;
|
|
105 #ifdef FEAT_FOLDING
|
|
106 wp->w_cline_folded = folded;
|
|
107 #endif
|
|
108 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
|
|
109 }
|
|
110 if (done + n > wp->w_height)
|
|
111 break;
|
|
112 done += n;
|
|
113 #ifdef FEAT_FOLDING
|
|
114 lnum = last;
|
|
115 #endif
|
|
116 }
|
|
117
|
|
118 /* wp->w_botline is the line that is just below the window */
|
|
119 wp->w_botline = lnum;
|
|
120 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
|
|
121
|
|
122 set_empty_rows(wp, done);
|
|
123 }
|
|
124
|
|
125 /*
|
|
126 * Update curwin->w_topline and redraw if necessary.
|
|
127 * Used to update the screen before printing a message.
|
|
128 */
|
|
129 void
|
|
130 update_topline_redraw()
|
|
131 {
|
|
132 update_topline();
|
|
133 if (must_redraw)
|
|
134 update_screen(0);
|
|
135 }
|
|
136
|
|
137 /*
|
|
138 * Update curwin->w_topline to move the cursor onto the screen.
|
|
139 */
|
|
140 void
|
|
141 update_topline()
|
|
142 {
|
|
143 long line_count;
|
|
144 int halfheight;
|
|
145 int n;
|
|
146 linenr_T old_topline;
|
|
147 #ifdef FEAT_DIFF
|
|
148 int old_topfill;
|
|
149 #endif
|
|
150 #ifdef FEAT_FOLDING
|
|
151 linenr_T lnum;
|
|
152 #endif
|
|
153 int check_topline = FALSE;
|
|
154 int check_botline = FALSE;
|
|
155 #ifdef FEAT_MOUSE
|
|
156 int save_so = p_so;
|
|
157 #endif
|
|
158
|
|
159 if (!screen_valid(TRUE))
|
|
160 return;
|
|
161
|
|
162 check_cursor_moved(curwin);
|
|
163 if (curwin->w_valid & VALID_TOPLINE)
|
|
164 return;
|
|
165
|
|
166 #ifdef FEAT_MOUSE
|
|
167 /* When dragging with the mouse, don't scroll that quickly */
|
|
168 if (mouse_dragging)
|
|
169 p_so = mouse_dragging - 1;
|
|
170 #endif
|
|
171
|
|
172 old_topline = curwin->w_topline;
|
|
173 #ifdef FEAT_DIFF
|
|
174 old_topfill = curwin->w_topfill;
|
|
175 #endif
|
|
176
|
|
177 /*
|
|
178 * If the buffer is empty, always set topline to 1.
|
|
179 */
|
|
180 if (bufempty()) /* special case - file is empty */
|
|
181 {
|
|
182 if (curwin->w_topline != 1)
|
|
183 redraw_later(NOT_VALID);
|
|
184 curwin->w_topline = 1;
|
|
185 #ifdef FEAT_DIFF
|
|
186 curwin->w_topfill = 0;
|
|
187 #endif
|
|
188 curwin->w_botline = 2;
|
|
189 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
|
|
190 #ifdef FEAT_SCROLLBIND
|
|
191 curwin->w_scbind_pos = 1;
|
|
192 #endif
|
|
193 }
|
|
194
|
|
195 /*
|
|
196 * If the cursor is above or near the top of the window, scroll the window
|
|
197 * to show the line the cursor is in, with 'scrolloff' context.
|
|
198 */
|
|
199 else
|
|
200 {
|
|
201 if (curwin->w_topline > 1)
|
|
202 {
|
|
203 /* If the cursor is above topline, scrolling is always needed.
|
|
204 * If the cursor is far below topline and there is no folding,
|
|
205 * scrolling down is never needed. */
|
|
206 if (curwin->w_cursor.lnum < curwin->w_topline)
|
|
207 check_topline = TRUE;
|
|
208 else if (check_top_offset())
|
|
209 check_topline = TRUE;
|
|
210 }
|
|
211 #ifdef FEAT_DIFF
|
|
212 /* Check if there are more filler lines than allowed. */
|
|
213 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
|
|
214 curwin->w_topline))
|
|
215 check_topline = TRUE;
|
|
216 #endif
|
|
217
|
|
218 if (check_topline)
|
|
219 {
|
|
220 halfheight = curwin->w_height / 2 - 1;
|
|
221 if (halfheight < 2)
|
|
222 halfheight = 2;
|
|
223
|
|
224 #ifdef FEAT_FOLDING
|
|
225 if (hasAnyFolding(curwin))
|
|
226 {
|
|
227 /* Count the number of logical lines between the cursor and
|
|
228 * topline + p_so (approximation of how much will be
|
|
229 * scrolled). */
|
|
230 n = 0;
|
|
231 for (lnum = curwin->w_cursor.lnum;
|
|
232 lnum < curwin->w_topline + p_so; ++lnum)
|
|
233 {
|
|
234 ++n;
|
|
235 /* stop at end of file or when we know we are far off */
|
|
236 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
|
|
237 break;
|
|
238 (void)hasFolding(lnum, NULL, &lnum);
|
|
239 }
|
|
240 }
|
|
241 else
|
|
242 #endif
|
|
243 n = curwin->w_topline + p_so - curwin->w_cursor.lnum;
|
|
244
|
|
245 /* If we weren't very close to begin with, we scroll to put the
|
|
246 * cursor in the middle of the window. Otherwise put the cursor
|
|
247 * near the top of the window. */
|
|
248 if (n >= halfheight)
|
|
249 scroll_cursor_halfway(FALSE);
|
|
250 else
|
|
251 {
|
|
252 scroll_cursor_top((int)p_sj, FALSE);
|
|
253 check_botline = TRUE;
|
|
254 }
|
|
255 }
|
|
256
|
|
257 else
|
|
258 {
|
|
259 #ifdef FEAT_FOLDING
|
|
260 /* Make sure topline is the first line of a fold. */
|
|
261 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
|
|
262 #endif
|
|
263 check_botline = TRUE;
|
|
264 }
|
|
265 }
|
|
266
|
|
267 /*
|
|
268 * If the cursor is below the bottom of the window, scroll the window
|
|
269 * to put the cursor on the window.
|
|
270 * When w_botline is invalid, recompute it first, to avoid a redraw later.
|
|
271 * If w_botline was approximated, we might need a redraw later in a few
|
|
272 * cases, but we don't want to spend (a lot of) time recomputing w_botline
|
|
273 * for every small change.
|
|
274 */
|
|
275 if (check_botline)
|
|
276 {
|
|
277 if (!(curwin->w_valid & VALID_BOTLINE_AP))
|
|
278 validate_botline();
|
|
279
|
|
280 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
|
|
281 {
|
|
282 if (curwin->w_cursor.lnum < curwin->w_botline
|
|
283 && ((long)curwin->w_cursor.lnum
|
|
284 >= (long)curwin->w_botline - p_so
|
|
285 #ifdef FEAT_FOLDING
|
|
286 || hasAnyFolding(curwin)
|
|
287 #endif
|
|
288 ))
|
|
289 {
|
|
290 lineoff_T loff;
|
|
291
|
|
292 /* Cursor is above botline, check if there are 'scrolloff'
|
|
293 * window lines below the cursor. If not, need to scroll. */
|
|
294 n = curwin->w_empty_rows;
|
|
295 loff.lnum = curwin->w_cursor.lnum;
|
|
296 #ifdef FEAT_FOLDING
|
|
297 /* In a fold go to its last line. */
|
|
298 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
|
|
299 #endif
|
|
300 #ifdef FEAT_DIFF
|
|
301 loff.fill = 0;
|
|
302 n += curwin->w_filler_rows;
|
|
303 #endif
|
|
304 loff.height = 0;
|
|
305 while (loff.lnum < curwin->w_botline
|
|
306 #ifdef FEAT_DIFF
|
|
307 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
|
|
308 #endif
|
|
309 )
|
|
310 {
|
|
311 n += loff.height;
|
|
312 if (n >= p_so)
|
|
313 break;
|
|
314 botline_forw(&loff);
|
|
315 }
|
|
316 if (n >= p_so)
|
|
317 /* sufficient context, no need to scroll */
|
|
318 check_botline = FALSE;
|
|
319 }
|
|
320 if (check_botline)
|
|
321 {
|
|
322 #ifdef FEAT_FOLDING
|
|
323 if (hasAnyFolding(curwin))
|
|
324 {
|
|
325 /* Count the number of logical lines between the cursor and
|
|
326 * botline - p_so (approximation of how much will be
|
|
327 * scrolled). */
|
|
328 line_count = 0;
|
|
329 for (lnum = curwin->w_cursor.lnum;
|
|
330 lnum >= curwin->w_botline - p_so; --lnum)
|
|
331 {
|
|
332 ++line_count;
|
|
333 /* stop at end of file or when we know we are far off */
|
|
334 if (lnum <= 0 || line_count > curwin->w_height + 1)
|
|
335 break;
|
|
336 (void)hasFolding(lnum, &lnum, NULL);
|
|
337 }
|
|
338 }
|
|
339 else
|
|
340 #endif
|
|
341 line_count = curwin->w_cursor.lnum - curwin->w_botline
|
|
342 + 1 + p_so;
|
|
343 if (line_count <= curwin->w_height + 1)
|
|
344 scroll_cursor_bot((int)p_sj, FALSE);
|
|
345 else
|
|
346 scroll_cursor_halfway(FALSE);
|
|
347 }
|
|
348 }
|
|
349 }
|
|
350 curwin->w_valid |= VALID_TOPLINE;
|
|
351
|
|
352 /*
|
|
353 * Need to redraw when topline changed.
|
|
354 */
|
|
355 if (curwin->w_topline != old_topline
|
|
356 #ifdef FEAT_DIFF
|
|
357 || curwin->w_topfill != old_topfill
|
|
358 #endif
|
|
359 )
|
|
360 {
|
|
361 dollar_vcol = 0;
|
|
362 if (curwin->w_skipcol)
|
|
363 {
|
|
364 curwin->w_skipcol = 0;
|
|
365 redraw_later(NOT_VALID);
|
|
366 }
|
|
367 else
|
|
368 redraw_later(VALID);
|
|
369 /* May need to set w_skipcol when cursor in w_topline. */
|
|
370 if (curwin->w_cursor.lnum == curwin->w_topline)
|
|
371 validate_cursor();
|
|
372 }
|
|
373
|
|
374 #ifdef FEAT_MOUSE
|
|
375 p_so = save_so;
|
|
376 #endif
|
|
377 }
|
|
378
|
|
379 /*
|
|
380 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
|
|
381 * current window.
|
|
382 */
|
|
383 static int
|
|
384 check_top_offset()
|
|
385 {
|
|
386 lineoff_T loff;
|
|
387 int n;
|
|
388
|
|
389 if (curwin->w_cursor.lnum < curwin->w_topline + p_so
|
|
390 #ifdef FEAT_FOLDING
|
|
391 || hasAnyFolding(curwin)
|
|
392 #endif
|
|
393 )
|
|
394 {
|
|
395 loff.lnum = curwin->w_cursor.lnum;
|
|
396 #ifdef FEAT_DIFF
|
|
397 loff.fill = 0;
|
|
398 n = curwin->w_topfill; /* always have this context */
|
|
399 #else
|
|
400 n = 0;
|
|
401 #endif
|
|
402 /* Count the visible screen lines above the cursor line. */
|
|
403 while (n < p_so)
|
|
404 {
|
|
405 topline_back(&loff);
|
|
406 /* Stop when included a line above the window. */
|
|
407 if (loff.lnum < curwin->w_topline
|
|
408 #ifdef FEAT_DIFF
|
|
409 || (loff.lnum == curwin->w_topline && loff.fill > 0)
|
|
410 #endif
|
|
411 )
|
|
412 break;
|
|
413 n += loff.height;
|
|
414 }
|
|
415 if (n < p_so)
|
|
416 return TRUE;
|
|
417 }
|
|
418 return FALSE;
|
|
419 }
|
|
420
|
|
421 void
|
|
422 update_curswant()
|
|
423 {
|
|
424 if (curwin->w_set_curswant)
|
|
425 {
|
|
426 validate_virtcol();
|
|
427 curwin->w_curswant = curwin->w_virtcol;
|
|
428 curwin->w_set_curswant = FALSE;
|
|
429 }
|
|
430 }
|
|
431
|
|
432 /*
|
|
433 * Check if the cursor has moved. Set the w_valid flag accordingly.
|
|
434 */
|
|
435 void
|
|
436 check_cursor_moved(wp)
|
|
437 win_T *wp;
|
|
438 {
|
|
439 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
|
|
440 {
|
|
441 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
|
|
442 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
|
|
443 wp->w_valid_cursor = wp->w_cursor;
|
|
444 wp->w_valid_leftcol = wp->w_leftcol;
|
|
445 }
|
|
446 else if (wp->w_cursor.col != wp->w_valid_cursor.col
|
|
447 || wp->w_leftcol != wp->w_valid_leftcol
|
|
448 #ifdef FEAT_VIRTUALEDIT
|
|
449 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd
|
|
450 #endif
|
|
451 )
|
|
452 {
|
|
453 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
|
|
454 wp->w_valid_cursor.col = wp->w_cursor.col;
|
|
455 wp->w_valid_leftcol = wp->w_leftcol;
|
|
456 #ifdef FEAT_VIRTUALEDIT
|
|
457 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
|
|
458 #endif
|
|
459 }
|
|
460 }
|
|
461
|
|
462 /*
|
|
463 * Call this function when some window settings have changed, which require
|
|
464 * the cursor position, botline and topline to be recomputed and the window to
|
|
465 * be redrawn. E.g, when changing the 'wrap' option or folding.
|
|
466 */
|
|
467 void
|
|
468 changed_window_setting()
|
|
469 {
|
|
470 changed_window_setting_win(curwin);
|
|
471 }
|
|
472
|
|
473 void
|
|
474 changed_window_setting_win(wp)
|
|
475 win_T *wp;
|
|
476 {
|
|
477 wp->w_lines_valid = 0;
|
|
478 changed_line_abv_curs_win(wp);
|
|
479 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
|
|
480 redraw_win_later(wp, NOT_VALID);
|
|
481 }
|
|
482
|
|
483 /*
|
|
484 * Set wp->w_topline to a certain number.
|
|
485 */
|
|
486 void
|
|
487 set_topline(wp, lnum)
|
|
488 win_T *wp;
|
|
489 linenr_T lnum;
|
|
490 {
|
|
491 #ifdef FEAT_FOLDING
|
|
492 /* go to first of folded lines */
|
|
493 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
|
|
494 #endif
|
|
495 /* Approximate the value of w_botline */
|
|
496 wp->w_botline += lnum - wp->w_topline;
|
|
497 wp->w_topline = lnum;
|
|
498 #ifdef FEAT_DIFF
|
|
499 wp->w_topfill = 0;
|
|
500 #endif
|
|
501 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
|
|
502 /* Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked. */
|
|
503 redraw_later(VALID);
|
|
504 }
|
|
505
|
|
506 /*
|
|
507 * Call this function when the length of the cursor line (in screen
|
|
508 * characters) has changed, and the change is before the cursor.
|
|
509 * Need to take care of w_botline separately!
|
|
510 */
|
|
511 void
|
|
512 changed_cline_bef_curs()
|
|
513 {
|
|
514 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
|
|
515 |VALID_CHEIGHT|VALID_TOPLINE);
|
|
516 }
|
|
517
|
|
518 void
|
|
519 changed_cline_bef_curs_win(wp)
|
|
520 win_T *wp;
|
|
521 {
|
|
522 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
|
|
523 |VALID_CHEIGHT|VALID_TOPLINE);
|
|
524 }
|
|
525
|
|
526 #if 0 /* not used */
|
|
527 /*
|
|
528 * Call this function when the length of the cursor line (in screen
|
|
529 * characters) has changed, and the position of the cursor doesn't change.
|
|
530 * Need to take care of w_botline separately!
|
|
531 */
|
|
532 void
|
|
533 changed_cline_aft_curs()
|
|
534 {
|
|
535 curwin->w_valid &= ~VALID_CHEIGHT;
|
|
536 }
|
|
537 #endif
|
|
538
|
|
539 /*
|
|
540 * Call this function when the length of a line (in screen characters) above
|
|
541 * the cursor have changed.
|
|
542 * Need to take care of w_botline separately!
|
|
543 */
|
|
544 void
|
|
545 changed_line_abv_curs()
|
|
546 {
|
|
547 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
|
|
548 |VALID_CHEIGHT|VALID_TOPLINE);
|
|
549 }
|
|
550
|
|
551 void
|
|
552 changed_line_abv_curs_win(wp)
|
|
553 win_T *wp;
|
|
554 {
|
|
555 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
|
|
556 |VALID_CHEIGHT|VALID_TOPLINE);
|
|
557 }
|
|
558
|
|
559 /*
|
|
560 * Make sure the value of curwin->w_botline is valid.
|
|
561 */
|
|
562 void
|
|
563 validate_botline()
|
|
564 {
|
|
565 if (!(curwin->w_valid & VALID_BOTLINE))
|
|
566 comp_botline(curwin);
|
|
567 }
|
|
568
|
|
569 /*
|
|
570 * Make sure the value of wp->w_botline is valid.
|
|
571 */
|
|
572 static void
|
|
573 validate_botline_win(wp)
|
|
574 win_T *wp;
|
|
575 {
|
|
576 if (!(wp->w_valid & VALID_BOTLINE))
|
|
577 comp_botline(wp);
|
|
578 }
|
|
579
|
|
580 /*
|
|
581 * Mark curwin->w_botline as invalid (because of some change in the buffer).
|
|
582 */
|
|
583 void
|
|
584 invalidate_botline()
|
|
585 {
|
|
586 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
|
|
587 }
|
|
588
|
|
589 void
|
|
590 invalidate_botline_win(wp)
|
|
591 win_T *wp;
|
|
592 {
|
|
593 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
|
|
594 }
|
|
595
|
|
596 #if 0 /* never used */
|
|
597 /*
|
|
598 * Mark curwin->w_botline as approximated (because of some small change in the
|
|
599 * buffer).
|
|
600 */
|
|
601 void
|
|
602 approximate_botline()
|
|
603 {
|
|
604 curwin->w_valid &= ~VALID_BOTLINE;
|
|
605 }
|
|
606 #endif
|
|
607
|
|
608 void
|
|
609 approximate_botline_win(wp)
|
|
610 win_T *wp;
|
|
611 {
|
|
612 wp->w_valid &= ~VALID_BOTLINE;
|
|
613 }
|
|
614
|
|
615 #if 0 /* not used */
|
|
616 /*
|
|
617 * Return TRUE if curwin->w_botline is valid.
|
|
618 */
|
|
619 int
|
|
620 botline_valid()
|
|
621 {
|
|
622 return (curwin->w_valid & VALID_BOTLINE);
|
|
623 }
|
|
624 #endif
|
|
625
|
|
626 #if 0 /* not used */
|
|
627 /*
|
|
628 * Return TRUE if curwin->w_botline is valid or approximated.
|
|
629 */
|
|
630 int
|
|
631 botline_approximated()
|
|
632 {
|
|
633 return (curwin->w_valid & VALID_BOTLINE_AP);
|
|
634 }
|
|
635 #endif
|
|
636
|
|
637 /*
|
|
638 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
|
|
639 */
|
|
640 int
|
|
641 cursor_valid()
|
|
642 {
|
|
643 check_cursor_moved(curwin);
|
|
644 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
|
|
645 (VALID_WROW|VALID_WCOL));
|
|
646 }
|
|
647
|
|
648 /*
|
|
649 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
|
|
650 * w_topline must be valid, you may need to call update_topline() first!
|
|
651 */
|
|
652 void
|
|
653 validate_cursor()
|
|
654 {
|
|
655 check_cursor_moved(curwin);
|
|
656 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
|
|
657 curs_columns(TRUE);
|
|
658 }
|
|
659
|
|
660 #if defined(FEAT_GUI) || defined(PROTO)
|
|
661 /*
|
|
662 * validate w_cline_row.
|
|
663 */
|
|
664 void
|
|
665 validate_cline_row()
|
|
666 {
|
|
667 /*
|
|
668 * First make sure that w_topline is valid (after moving the cursor).
|
|
669 */
|
|
670 update_topline();
|
|
671 check_cursor_moved(curwin);
|
|
672 if (!(curwin->w_valid & VALID_CROW))
|
|
673 curs_rows(curwin, FALSE);
|
|
674 }
|
|
675 #endif
|
|
676
|
|
677 /*
|
|
678 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
|
|
679 * of wp->w_topine.
|
|
680 *
|
|
681 * Returns OK when cursor is in the window, FAIL when it isn't.
|
|
682 */
|
|
683 static void
|
|
684 curs_rows(wp, do_botline)
|
|
685 win_T *wp;
|
|
686 int do_botline; /* also compute w_botline */
|
|
687 {
|
|
688 linenr_T lnum;
|
|
689 int i;
|
|
690 int all_invalid;
|
|
691 int valid;
|
|
692 #ifdef FEAT_FOLDING
|
|
693 long fold_count;
|
|
694 #endif
|
|
695
|
|
696 /* Check if wp->w_lines[].wl_size is invalid */
|
|
697 all_invalid = (!redrawing()
|
|
698 || wp->w_lines_valid == 0
|
|
699 || wp->w_lines[0].wl_lnum > wp->w_topline);
|
|
700 i = 0;
|
|
701 wp->w_cline_row = 0;
|
|
702 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
|
|
703 {
|
|
704 valid = FALSE;
|
|
705 if (!all_invalid && i < wp->w_lines_valid)
|
|
706 {
|
|
707 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
|
|
708 continue; /* skip changed or deleted lines */
|
|
709 if (wp->w_lines[i].wl_lnum == lnum)
|
|
710 {
|
|
711 #ifdef FEAT_FOLDING
|
|
712 /* Check for newly inserted lines below this row, in which
|
|
713 * case we need to check for folded lines. */
|
|
714 if (!wp->w_buffer->b_mod_set
|
|
715 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
|
|
716 || wp->w_buffer->b_mod_top
|
|
717 > wp->w_lines[i].wl_lastlnum + 1)
|
|
718 #endif
|
|
719 valid = TRUE;
|
|
720 }
|
|
721 else if (wp->w_lines[i].wl_lnum > lnum)
|
|
722 --i; /* hold at inserted lines */
|
|
723 }
|
|
724 if (valid
|
|
725 #ifdef FEAT_DIFF
|
|
726 && (lnum != wp->w_topline || !wp->w_p_diff)
|
|
727 #endif
|
|
728 )
|
|
729 {
|
|
730 #ifdef FEAT_FOLDING
|
|
731 lnum = wp->w_lines[i].wl_lastlnum + 1;
|
|
732 /* Cursor inside folded lines, don't count this row */
|
|
733 if (lnum > wp->w_cursor.lnum)
|
|
734 break;
|
|
735 #else
|
|
736 ++lnum;
|
|
737 #endif
|
|
738 wp->w_cline_row += wp->w_lines[i].wl_size;
|
|
739 }
|
|
740 else
|
|
741 {
|
|
742 #ifdef FEAT_FOLDING
|
|
743 fold_count = foldedCount(wp, lnum, NULL);
|
|
744 if (fold_count)
|
|
745 {
|
|
746 lnum += fold_count;
|
|
747 if (lnum > wp->w_cursor.lnum)
|
|
748 break;
|
|
749 ++wp->w_cline_row;
|
|
750 }
|
|
751 else
|
|
752 #endif
|
|
753 #ifdef FEAT_DIFF
|
|
754 if (lnum == wp->w_topline)
|
|
755 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
|
|
756 + wp->w_topfill;
|
|
757 else
|
|
758 #endif
|
|
759 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
|
|
760 }
|
|
761 }
|
|
762
|
|
763 check_cursor_moved(wp);
|
|
764 if (!(wp->w_valid & VALID_CHEIGHT))
|
|
765 {
|
|
766 if (all_invalid
|
|
767 || i == wp->w_lines_valid
|
|
768 || (i < wp->w_lines_valid
|
|
769 && (!wp->w_lines[i].wl_valid
|
|
770 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
|
|
771 {
|
|
772 #ifdef FEAT_DIFF
|
|
773 if (wp->w_cursor.lnum == wp->w_topline)
|
|
774 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
|
|
775 TRUE) + wp->w_topfill;
|
|
776 else
|
|
777 #endif
|
|
778 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
|
|
779 #ifdef FEAT_FOLDING
|
|
780 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
|
|
781 NULL, NULL, TRUE, NULL);
|
|
782 #endif
|
|
783 }
|
|
784 else if (i > wp->w_lines_valid)
|
|
785 {
|
|
786 /* a line that is too long to fit on the last screen line */
|
|
787 wp->w_cline_height = 0;
|
|
788 #ifdef FEAT_FOLDING
|
|
789 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
|
|
790 NULL, NULL, TRUE, NULL);
|
|
791 #endif
|
|
792 }
|
|
793 else
|
|
794 {
|
|
795 wp->w_cline_height = wp->w_lines[i].wl_size;
|
|
796 #ifdef FEAT_FOLDING
|
|
797 wp->w_cline_folded = wp->w_lines[i].wl_folded;
|
|
798 #endif
|
|
799 }
|
|
800 }
|
|
801
|
|
802 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
|
|
803
|
|
804 /* validate botline too, if update_screen doesn't do it */
|
|
805 if (do_botline && all_invalid)
|
|
806 validate_botline_win(wp);
|
|
807 }
|
|
808
|
|
809 /*
|
|
810 * Validate curwin->w_virtcol only.
|
|
811 */
|
|
812 void
|
|
813 validate_virtcol()
|
|
814 {
|
|
815 validate_virtcol_win(curwin);
|
|
816 }
|
|
817
|
|
818 /*
|
|
819 * Validate wp->w_virtcol only.
|
|
820 */
|
|
821 void
|
|
822 validate_virtcol_win(wp)
|
|
823 win_T *wp;
|
|
824 {
|
|
825 check_cursor_moved(wp);
|
|
826 if (!(wp->w_valid & VALID_VIRTCOL))
|
|
827 {
|
|
828 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
|
|
829 wp->w_valid |= VALID_VIRTCOL;
|
|
830 }
|
|
831 }
|
|
832
|
|
833 /*
|
|
834 * Validate curwin->w_cline_height only.
|
|
835 */
|
|
836 static void
|
|
837 validate_cheight()
|
|
838 {
|
|
839 check_cursor_moved(curwin);
|
|
840 if (!(curwin->w_valid & VALID_CHEIGHT))
|
|
841 {
|
|
842 #ifdef FEAT_DIFF
|
|
843 if (curwin->w_cursor.lnum == curwin->w_topline)
|
|
844 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
|
|
845 + curwin->w_topfill;
|
|
846 else
|
|
847 #endif
|
|
848 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
|
|
849 #ifdef FEAT_FOLDING
|
|
850 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
|
|
851 #endif
|
|
852 curwin->w_valid |= VALID_CHEIGHT;
|
|
853 }
|
|
854 }
|
|
855
|
|
856 /*
|
|
857 * validate w_wcol and w_virtcol only. Only correct when 'wrap' on!
|
|
858 */
|
|
859 void
|
|
860 validate_cursor_col()
|
|
861 {
|
|
862 colnr_T off;
|
|
863 colnr_T col;
|
|
864
|
|
865 validate_virtcol();
|
|
866 if (!(curwin->w_valid & VALID_WCOL))
|
|
867 {
|
|
868 col = curwin->w_virtcol;
|
|
869 off = curwin_col_off();
|
|
870 col += off;
|
|
871
|
|
872 /* long line wrapping, adjust curwin->w_wrow */
|
|
873 if (curwin->w_p_wrap && col >= (colnr_T)W_WIDTH(curwin)
|
119
|
874 && W_WIDTH(curwin) - off + curwin_col_off2() > 0)
|
7
|
875 {
|
|
876 col -= W_WIDTH(curwin);
|
|
877 col = col % (W_WIDTH(curwin) - off + curwin_col_off2());
|
|
878 }
|
|
879 curwin->w_wcol = col;
|
|
880 curwin->w_valid |= VALID_WCOL;
|
|
881 }
|
|
882 }
|
|
883
|
|
884 /*
|
|
885 * Compute offset of a window, occupied by line number, fold column and sign
|
|
886 * column (these don't move when scrolling horizontally).
|
|
887 */
|
|
888 int
|
|
889 win_col_off(wp)
|
|
890 win_T *wp;
|
|
891 {
|
13
|
892 return ((wp->w_p_nu ? number_width(wp) + 1 : 0)
|
7
|
893 #ifdef FEAT_CMDWIN
|
|
894 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
|
|
895 #endif
|
|
896 #ifdef FEAT_FOLDING
|
|
897 + wp->w_p_fdc
|
|
898 #endif
|
|
899 #ifdef FEAT_SIGNS
|
|
900 + (
|
|
901 # ifdef FEAT_NETBEANS_INTG
|
|
902 /* always show glyph gutter in netbeans */
|
|
903 usingNetbeans ||
|
|
904 # endif
|
|
905 wp->w_buffer->b_signlist != NULL ? 2 : 0)
|
|
906 #endif
|
|
907 );
|
|
908 }
|
|
909
|
|
910 int
|
|
911 curwin_col_off()
|
|
912 {
|
|
913 return win_col_off(curwin);
|
|
914 }
|
|
915
|
|
916 /*
|
|
917 * Return the difference in column offset for the second screen line of a
|
|
918 * wrapped line. It's 8 if 'number' is on and 'n' is in 'cpoptions'.
|
|
919 */
|
|
920 int
|
|
921 win_col_off2(wp)
|
|
922 win_T *wp;
|
|
923 {
|
|
924 if (wp->w_p_nu && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
|
13
|
925 return number_width(wp) + 1;
|
7
|
926 return 0;
|
|
927 }
|
|
928
|
|
929 int
|
|
930 curwin_col_off2()
|
|
931 {
|
|
932 return win_col_off2(curwin);
|
|
933 }
|
|
934
|
|
935 /*
|
|
936 * compute curwin->w_wcol and curwin->w_virtcol.
|
|
937 * Also updates curwin->w_wrow and curwin->w_cline_row.
|
|
938 * Also updates curwin->w_leftcol.
|
|
939 */
|
|
940 void
|
|
941 curs_columns(scroll)
|
|
942 int scroll; /* when TRUE, may scroll horizontally */
|
|
943 {
|
|
944 int diff;
|
|
945 int extra; /* offset for first screen line */
|
|
946 int off_left, off_right;
|
|
947 int n;
|
|
948 int p_lines;
|
|
949 int width = 0;
|
|
950 int textwidth;
|
|
951 int new_leftcol;
|
|
952 colnr_T startcol;
|
|
953 colnr_T endcol;
|
|
954 colnr_T prev_skipcol;
|
|
955
|
|
956 /*
|
|
957 * First make sure that w_topline is valid (after moving the cursor).
|
|
958 */
|
|
959 update_topline();
|
|
960
|
|
961 /*
|
|
962 * Next make sure that w_cline_row is valid.
|
|
963 */
|
|
964 if (!(curwin->w_valid & VALID_CROW))
|
|
965 curs_rows(curwin, FALSE);
|
|
966
|
|
967 /*
|
|
968 * Compute the number of virtual columns.
|
|
969 */
|
|
970 #ifdef FEAT_FOLDING
|
|
971 if (curwin->w_cline_folded)
|
|
972 /* In a folded line the cursor is always in the first column */
|
|
973 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
|
|
974 else
|
|
975 #endif
|
|
976 getvvcol(curwin, &curwin->w_cursor,
|
|
977 &startcol, &(curwin->w_virtcol), &endcol);
|
|
978
|
|
979 /* remove '$' from change command when cursor moves onto it */
|
|
980 if (startcol > dollar_vcol)
|
|
981 dollar_vcol = 0;
|
|
982
|
|
983 extra = curwin_col_off();
|
|
984 curwin->w_wcol = curwin->w_virtcol + extra;
|
|
985 endcol += extra;
|
|
986
|
|
987 /*
|
|
988 * Now compute w_wrow, counting screen lines from w_cline_row.
|
|
989 */
|
|
990 curwin->w_wrow = curwin->w_cline_row;
|
|
991
|
|
992 textwidth = W_WIDTH(curwin) - extra;
|
|
993 if (textwidth <= 0)
|
|
994 {
|
|
995 /* No room for text, put cursor in last char of window. */
|
|
996 curwin->w_wcol = W_WIDTH(curwin) - 1;
|
|
997 curwin->w_wrow = curwin->w_height - 1;
|
|
998 }
|
|
999 else if (curwin->w_p_wrap
|
|
1000 #ifdef FEAT_VERTSPLIT
|
|
1001 && curwin->w_width != 0
|
|
1002 #endif
|
|
1003 )
|
|
1004 {
|
|
1005 width = textwidth + curwin_col_off2();
|
|
1006
|
|
1007 /* long line wrapping, adjust curwin->w_wrow */
|
|
1008 if (curwin->w_wcol >= W_WIDTH(curwin))
|
|
1009 {
|
|
1010 n = (curwin->w_wcol - W_WIDTH(curwin)) / width + 1;
|
|
1011 curwin->w_wcol -= n * width;
|
|
1012 curwin->w_wrow += n;
|
|
1013
|
|
1014 #ifdef FEAT_LINEBREAK
|
|
1015 /* When cursor wraps to first char of next line in Insert
|
|
1016 * mode, the 'showbreak' string isn't shown, backup to first
|
|
1017 * column */
|
|
1018 if (*p_sbr && *ml_get_cursor() == NUL
|
|
1019 && curwin->w_wcol == (int)vim_strsize(p_sbr))
|
|
1020 curwin->w_wcol = 0;
|
|
1021 #endif
|
|
1022 }
|
|
1023 }
|
|
1024
|
|
1025 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
|
|
1026 * is not folded.
|
|
1027 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
|
|
1028 else if (scroll
|
|
1029 #ifdef FEAT_FOLDING
|
|
1030 && !curwin->w_cline_folded
|
|
1031 #endif
|
|
1032 )
|
|
1033 {
|
|
1034 /*
|
|
1035 * If Cursor is left of the screen, scroll rightwards.
|
|
1036 * If Cursor is right of the screen, scroll leftwards
|
|
1037 * If we get closer to the edge than 'sidescrolloff', scroll a little
|
|
1038 * extra
|
|
1039 */
|
|
1040 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
|
|
1041 off_right = (int)endcol - (int)(curwin->w_leftcol + W_WIDTH(curwin)
|
|
1042 - p_siso) + 1;
|
|
1043 if (off_left < 0 || off_right > 0)
|
|
1044 {
|
|
1045 if (off_left < 0)
|
|
1046 diff = -off_left;
|
|
1047 else
|
|
1048 diff = off_right;
|
|
1049
|
|
1050 /* When far off or not enough room on either side, put cursor in
|
|
1051 * middle of window. */
|
|
1052 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
|
|
1053 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
|
|
1054 else
|
|
1055 {
|
|
1056 if (diff < p_ss)
|
|
1057 diff = p_ss;
|
|
1058 if (off_left < 0)
|
|
1059 new_leftcol = curwin->w_leftcol - diff;
|
|
1060 else
|
|
1061 new_leftcol = curwin->w_leftcol + diff;
|
|
1062 }
|
|
1063 if (new_leftcol < 0)
|
|
1064 new_leftcol = 0;
|
|
1065 if (new_leftcol != (int)curwin->w_leftcol)
|
|
1066 {
|
|
1067 curwin->w_leftcol = new_leftcol;
|
|
1068 /* screen has to be redrawn with new curwin->w_leftcol */
|
|
1069 redraw_later(NOT_VALID);
|
|
1070 }
|
|
1071 }
|
|
1072 curwin->w_wcol -= curwin->w_leftcol;
|
|
1073 }
|
|
1074 else if (curwin->w_wcol > (int)curwin->w_leftcol)
|
|
1075 curwin->w_wcol -= curwin->w_leftcol;
|
|
1076 else
|
|
1077 curwin->w_wcol = 0;
|
|
1078
|
|
1079 #ifdef FEAT_DIFF
|
|
1080 /* Skip over filler lines. At the top use w_topfill, there
|
|
1081 * may be some filler lines above the window. */
|
|
1082 if (curwin->w_cursor.lnum == curwin->w_topline)
|
|
1083 curwin->w_wrow += curwin->w_topfill;
|
|
1084 else
|
|
1085 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
|
|
1086 #endif
|
|
1087
|
|
1088 prev_skipcol = curwin->w_skipcol;
|
|
1089
|
|
1090 p_lines = 0;
|
|
1091 if ((curwin->w_wrow >= curwin->w_height
|
|
1092 || ((prev_skipcol > 0
|
|
1093 || curwin->w_wrow + p_so >= curwin->w_height)
|
|
1094 && (p_lines =
|
|
1095 #ifdef FEAT_DIFF
|
|
1096 plines_win_nofill
|
|
1097 #else
|
|
1098 plines_win
|
|
1099 #endif
|
|
1100 (curwin, curwin->w_cursor.lnum, FALSE))
|
|
1101 - 1 >= curwin->w_height))
|
|
1102 && curwin->w_height != 0
|
|
1103 && curwin->w_cursor.lnum == curwin->w_topline
|
|
1104 && width > 0
|
|
1105 #ifdef FEAT_VERTSPLIT
|
|
1106 && curwin->w_width != 0
|
|
1107 #endif
|
|
1108 )
|
|
1109 {
|
|
1110 /* Cursor past end of screen. Happens with a single line that does
|
|
1111 * not fit on screen. Find a skipcol to show the text around the
|
|
1112 * cursor. Avoid scrolling all the time. compute value of "extra":
|
|
1113 * 1: Less than "p_so" lines above
|
|
1114 * 2: Less than "p_so" lines below
|
|
1115 * 3: both of them */
|
|
1116 extra = 0;
|
|
1117 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
|
|
1118 extra = 1;
|
|
1119 /* Compute last display line of the buffer line that we want at the
|
|
1120 * bottom of the window. */
|
|
1121 if (p_lines == 0)
|
|
1122 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
|
|
1123 --p_lines;
|
|
1124 if (p_lines > curwin->w_wrow + p_so)
|
|
1125 n = curwin->w_wrow + p_so;
|
|
1126 else
|
|
1127 n = p_lines;
|
|
1128 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
|
|
1129 extra += 2;
|
|
1130
|
|
1131 if (extra == 3 || p_lines < p_so * 2)
|
|
1132 {
|
|
1133 /* not enough room for 'scrolloff', put cursor in the middle */
|
|
1134 n = curwin->w_virtcol / width;
|
|
1135 if (n > curwin->w_height / 2)
|
|
1136 n -= curwin->w_height / 2;
|
|
1137 else
|
|
1138 n = 0;
|
|
1139 /* don't skip more than necessary */
|
|
1140 if (n > p_lines - curwin->w_height + 1)
|
|
1141 n = p_lines - curwin->w_height + 1;
|
|
1142 curwin->w_skipcol = n * width;
|
|
1143 }
|
|
1144 else if (extra == 1)
|
|
1145 {
|
|
1146 /* less then 'scrolloff' lines above, decrease skipcol */
|
|
1147 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
|
|
1148 + width - 1) / width;
|
|
1149 if (extra > 0)
|
|
1150 {
|
|
1151 if ((colnr_T)(extra * width) > curwin->w_skipcol)
|
|
1152 extra = curwin->w_skipcol / width;
|
|
1153 curwin->w_skipcol -= extra * width;
|
|
1154 }
|
|
1155 }
|
|
1156 else if (extra == 2)
|
|
1157 {
|
|
1158 /* less then 'scrolloff' lines below, increase skipcol */
|
|
1159 endcol = (n - curwin->w_height + 1) * width;
|
|
1160 while (endcol > curwin->w_virtcol)
|
|
1161 endcol -= width;
|
|
1162 if (endcol > curwin->w_skipcol)
|
|
1163 curwin->w_skipcol = endcol;
|
|
1164 }
|
|
1165
|
|
1166 curwin->w_wrow -= curwin->w_skipcol / width;
|
|
1167 if (curwin->w_wrow >= curwin->w_height)
|
|
1168 {
|
|
1169 /* small window, make sure cursor is in it */
|
|
1170 extra = curwin->w_wrow - curwin->w_height + 1;
|
|
1171 curwin->w_skipcol += extra * width;
|
|
1172 curwin->w_wrow -= extra;
|
|
1173 }
|
|
1174
|
|
1175 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
|
|
1176 if (extra > 0)
|
|
1177 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
|
|
1178 else if (extra < 0)
|
|
1179 win_del_lines(curwin, 0, -extra, FALSE, FALSE);
|
|
1180 }
|
|
1181 else
|
|
1182 curwin->w_skipcol = 0;
|
|
1183 if (prev_skipcol != curwin->w_skipcol)
|
|
1184 redraw_later(NOT_VALID);
|
|
1185
|
|
1186 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
|
|
1187 }
|
|
1188
|
|
1189 /*
|
|
1190 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
|
|
1191 */
|
|
1192 /*ARGSUSED*/
|
|
1193 void
|
|
1194 scrolldown(line_count, byfold)
|
|
1195 long line_count;
|
|
1196 int byfold; /* TRUE: count a closed fold as one line */
|
|
1197 {
|
|
1198 long done = 0; /* total # of physical lines done */
|
|
1199 int wrow;
|
|
1200 int moved = FALSE;
|
|
1201
|
|
1202 #ifdef FEAT_FOLDING
|
|
1203 linenr_T first;
|
|
1204
|
|
1205 /* Make sure w_topline is at the first of a sequence of folded lines. */
|
|
1206 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
|
|
1207 #endif
|
|
1208 validate_cursor(); /* w_wrow needs to be valid */
|
|
1209 while (line_count-- > 0)
|
|
1210 {
|
|
1211 #ifdef FEAT_DIFF
|
|
1212 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
|
|
1213 {
|
|
1214 ++curwin->w_topfill;
|
|
1215 ++done;
|
|
1216 }
|
|
1217 else
|
|
1218 #endif
|
|
1219 {
|
|
1220 if (curwin->w_topline == 1)
|
|
1221 break;
|
|
1222 --curwin->w_topline;
|
|
1223 #ifdef FEAT_DIFF
|
|
1224 curwin->w_topfill = 0;
|
|
1225 #endif
|
|
1226 #ifdef FEAT_FOLDING
|
|
1227 /* A sequence of folded lines only counts for one logical line */
|
|
1228 if (hasFolding(curwin->w_topline, &first, NULL))
|
|
1229 {
|
|
1230 ++done;
|
|
1231 if (!byfold)
|
|
1232 line_count -= curwin->w_topline - first - 1;
|
|
1233 curwin->w_botline -= curwin->w_topline - first;
|
|
1234 curwin->w_topline = first;
|
|
1235 }
|
|
1236 else
|
|
1237 #endif
|
|
1238 #ifdef FEAT_DIFF
|
|
1239 done += plines_nofill(curwin->w_topline);
|
|
1240 #else
|
|
1241 done += plines(curwin->w_topline);
|
|
1242 #endif
|
|
1243 }
|
|
1244 --curwin->w_botline; /* approximate w_botline */
|
|
1245 invalidate_botline();
|
|
1246 }
|
|
1247 curwin->w_wrow += done; /* keep w_wrow updated */
|
|
1248 curwin->w_cline_row += done; /* keep w_cline_row updated */
|
|
1249
|
|
1250 #ifdef FEAT_DIFF
|
|
1251 if (curwin->w_cursor.lnum == curwin->w_topline)
|
|
1252 curwin->w_cline_row = 0;
|
|
1253 check_topfill(curwin, TRUE);
|
|
1254 #endif
|
|
1255
|
|
1256 /*
|
|
1257 * Compute the row number of the last row of the cursor line
|
|
1258 * and move the cursor onto the displayed part of the window.
|
|
1259 */
|
|
1260 wrow = curwin->w_wrow;
|
|
1261 if (curwin->w_p_wrap
|
|
1262 #ifdef FEAT_VERTSPLIT
|
|
1263 && curwin->w_width != 0
|
|
1264 #endif
|
|
1265 )
|
|
1266 {
|
|
1267 validate_virtcol();
|
|
1268 validate_cheight();
|
|
1269 wrow += curwin->w_cline_height - 1 -
|
|
1270 curwin->w_virtcol / W_WIDTH(curwin);
|
|
1271 }
|
|
1272 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
|
|
1273 {
|
|
1274 #ifdef FEAT_FOLDING
|
|
1275 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
|
|
1276 {
|
|
1277 --wrow;
|
|
1278 if (first == 1)
|
|
1279 curwin->w_cursor.lnum = 1;
|
|
1280 else
|
|
1281 curwin->w_cursor.lnum = first - 1;
|
|
1282 }
|
|
1283 else
|
|
1284 #endif
|
|
1285 wrow -= plines(curwin->w_cursor.lnum--);
|
|
1286 curwin->w_valid &=
|
|
1287 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
|
|
1288 moved = TRUE;
|
|
1289 }
|
|
1290 if (moved)
|
|
1291 {
|
|
1292 #ifdef FEAT_FOLDING
|
|
1293 /* Move cursor to first line of closed fold. */
|
|
1294 foldAdjustCursor();
|
|
1295 #endif
|
|
1296 coladvance(curwin->w_curswant);
|
|
1297 }
|
|
1298 }
|
|
1299
|
|
1300 /*
|
|
1301 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
|
|
1302 */
|
|
1303 /*ARGSUSED*/
|
|
1304 void
|
|
1305 scrollup(line_count, byfold)
|
|
1306 long line_count;
|
|
1307 int byfold; /* TRUE: count a closed fold as one line */
|
|
1308 {
|
|
1309 #if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
|
|
1310 linenr_T lnum;
|
|
1311
|
|
1312 if (
|
|
1313 # ifdef FEAT_FOLDING
|
|
1314 (byfold && hasAnyFolding(curwin))
|
|
1315 # ifdef FEAT_DIFF
|
|
1316 ||
|
|
1317 # endif
|
|
1318 # endif
|
|
1319 # ifdef FEAT_DIFF
|
|
1320 curwin->w_p_diff
|
|
1321 # endif
|
|
1322 )
|
|
1323 {
|
|
1324 /* count each sequence of folded lines as one logical line */
|
|
1325 lnum = curwin->w_topline;
|
|
1326 while (line_count--)
|
|
1327 {
|
|
1328 # ifdef FEAT_DIFF
|
|
1329 if (curwin->w_topfill > 0)
|
|
1330 --curwin->w_topfill;
|
|
1331 else
|
|
1332 # endif
|
|
1333 {
|
|
1334 # ifdef FEAT_FOLDING
|
|
1335 if (byfold)
|
|
1336 (void)hasFolding(lnum, NULL, &lnum);
|
|
1337 # endif
|
|
1338 if (lnum >= curbuf->b_ml.ml_line_count)
|
|
1339 break;
|
|
1340 ++lnum;
|
|
1341 # ifdef FEAT_DIFF
|
|
1342 curwin->w_topfill = diff_check_fill(curwin, lnum);
|
|
1343 # endif
|
|
1344 }
|
|
1345 }
|
|
1346 /* approximate w_botline */
|
|
1347 curwin->w_botline += lnum - curwin->w_topline;
|
|
1348 curwin->w_topline = lnum;
|
|
1349 }
|
|
1350 else
|
|
1351 #endif
|
|
1352 {
|
|
1353 curwin->w_topline += line_count;
|
|
1354 curwin->w_botline += line_count; /* approximate w_botline */
|
|
1355 }
|
|
1356
|
|
1357 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
|
|
1358 curwin->w_topline = curbuf->b_ml.ml_line_count;
|
|
1359 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
|
|
1360 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
|
|
1361
|
|
1362 #ifdef FEAT_DIFF
|
|
1363 check_topfill(curwin, FALSE);
|
|
1364 #endif
|
|
1365
|
|
1366 #ifdef FEAT_FOLDING
|
|
1367 if (hasAnyFolding(curwin))
|
|
1368 /* Make sure w_topline is at the first of a sequence of folded lines. */
|
|
1369 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
|
|
1370 #endif
|
|
1371
|
|
1372 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
|
|
1373 if (curwin->w_cursor.lnum < curwin->w_topline)
|
|
1374 {
|
|
1375 curwin->w_cursor.lnum = curwin->w_topline;
|
|
1376 curwin->w_valid &=
|
|
1377 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
|
|
1378 coladvance(curwin->w_curswant);
|
|
1379 }
|
|
1380 }
|
|
1381
|
|
1382 #ifdef FEAT_DIFF
|
|
1383 /*
|
|
1384 * Don't end up with too many filler lines in the window.
|
|
1385 */
|
|
1386 void
|
|
1387 check_topfill(wp, down)
|
|
1388 win_T *wp;
|
|
1389 int down; /* when TRUE scroll down when not enough space */
|
|
1390 {
|
|
1391 int n;
|
|
1392
|
|
1393 if (wp->w_topfill > 0)
|
|
1394 {
|
|
1395 n = plines_win_nofill(wp, wp->w_topline, TRUE);
|
|
1396 if (wp->w_topfill + n > wp->w_height)
|
|
1397 {
|
|
1398 if (down && wp->w_topline > 1)
|
|
1399 {
|
|
1400 --wp->w_topline;
|
|
1401 wp->w_topfill = 0;
|
|
1402 }
|
|
1403 else
|
|
1404 {
|
|
1405 wp->w_topfill = wp->w_height - n;
|
|
1406 if (wp->w_topfill < 0)
|
|
1407 wp->w_topfill = 0;
|
|
1408 }
|
|
1409 }
|
|
1410 }
|
|
1411 }
|
|
1412
|
|
1413 /*
|
|
1414 * Use as many filler lines as possible for w_topline. Make sure w_topline
|
|
1415 * is still visible.
|
|
1416 */
|
|
1417 static void
|
|
1418 max_topfill()
|
|
1419 {
|
|
1420 int n;
|
|
1421
|
|
1422 n = plines_nofill(curwin->w_topline);
|
|
1423 if (n >= curwin->w_height)
|
|
1424 curwin->w_topfill = 0;
|
|
1425 else
|
|
1426 {
|
|
1427 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
|
|
1428 if (curwin->w_topfill + n > curwin->w_height)
|
|
1429 curwin->w_topfill = curwin->w_height - n;
|
|
1430 }
|
|
1431 }
|
|
1432 #endif
|
|
1433
|
|
1434 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
|
|
1435 /*
|
|
1436 * Scroll the screen one line down, but don't do it if it would move the
|
|
1437 * cursor off the screen.
|
|
1438 */
|
|
1439 void
|
|
1440 scrolldown_clamp()
|
|
1441 {
|
|
1442 int end_row;
|
|
1443 #ifdef FEAT_DIFF
|
|
1444 int can_fill = (curwin->w_topfill
|
|
1445 < diff_check_fill(curwin, curwin->w_topline));
|
|
1446 #endif
|
|
1447
|
|
1448 if (curwin->w_topline <= 1
|
|
1449 #ifdef FEAT_DIFF
|
|
1450 && !can_fill
|
|
1451 #endif
|
|
1452 )
|
|
1453 return;
|
|
1454
|
|
1455 validate_cursor(); /* w_wrow needs to be valid */
|
|
1456
|
|
1457 /*
|
|
1458 * Compute the row number of the last row of the cursor line
|
|
1459 * and make sure it doesn't go off the screen. Make sure the cursor
|
|
1460 * doesn't go past 'scrolloff' lines from the screen end.
|
|
1461 */
|
|
1462 end_row = curwin->w_wrow;
|
|
1463 #ifdef FEAT_DIFF
|
|
1464 if (can_fill)
|
|
1465 ++end_row;
|
|
1466 else
|
|
1467 end_row += plines_nofill(curwin->w_topline - 1);
|
|
1468 #else
|
|
1469 end_row += plines(curwin->w_topline - 1);
|
|
1470 #endif
|
|
1471 if (curwin->w_p_wrap
|
|
1472 #ifdef FEAT_VERTSPLIT
|
|
1473 && curwin->w_width != 0
|
|
1474 #endif
|
|
1475 )
|
|
1476 {
|
|
1477 validate_cheight();
|
|
1478 validate_virtcol();
|
|
1479 end_row += curwin->w_cline_height - 1 -
|
|
1480 curwin->w_virtcol / W_WIDTH(curwin);
|
|
1481 }
|
|
1482 if (end_row < curwin->w_height - p_so)
|
|
1483 {
|
|
1484 #ifdef FEAT_DIFF
|
|
1485 if (can_fill)
|
|
1486 {
|
|
1487 ++curwin->w_topfill;
|
|
1488 check_topfill(curwin, TRUE);
|
|
1489 }
|
|
1490 else
|
|
1491 {
|
|
1492 --curwin->w_topline;
|
|
1493 curwin->w_topfill = 0;
|
|
1494 }
|
|
1495 #else
|
|
1496 --curwin->w_topline;
|
|
1497 #endif
|
|
1498 #ifdef FEAT_FOLDING
|
|
1499 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
|
|
1500 #endif
|
|
1501 --curwin->w_botline; /* approximate w_botline */
|
|
1502 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
|
|
1503 }
|
|
1504 }
|
|
1505
|
|
1506 /*
|
|
1507 * Scroll the screen one line up, but don't do it if it would move the cursor
|
|
1508 * off the screen.
|
|
1509 */
|
|
1510 void
|
|
1511 scrollup_clamp()
|
|
1512 {
|
|
1513 int start_row;
|
|
1514
|
|
1515 if (curwin->w_topline == curbuf->b_ml.ml_line_count
|
|
1516 #ifdef FEAT_DIFF
|
|
1517 && curwin->w_topfill == 0
|
|
1518 #endif
|
|
1519 )
|
|
1520 return;
|
|
1521
|
|
1522 validate_cursor(); /* w_wrow needs to be valid */
|
|
1523
|
|
1524 /*
|
|
1525 * Compute the row number of the first row of the cursor line
|
|
1526 * and make sure it doesn't go off the screen. Make sure the cursor
|
|
1527 * doesn't go before 'scrolloff' lines from the screen start.
|
|
1528 */
|
|
1529 #ifdef FEAT_DIFF
|
|
1530 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
|
|
1531 - curwin->w_topfill;
|
|
1532 #else
|
|
1533 start_row = curwin->w_wrow - plines(curwin->w_topline);
|
|
1534 #endif
|
|
1535 if (curwin->w_p_wrap
|
|
1536 #ifdef FEAT_VERTSPLIT
|
|
1537 && curwin->w_width != 0
|
|
1538 #endif
|
|
1539 )
|
|
1540 {
|
|
1541 validate_virtcol();
|
|
1542 start_row -= curwin->w_virtcol / W_WIDTH(curwin);
|
|
1543 }
|
|
1544 if (start_row >= p_so)
|
|
1545 {
|
|
1546 #ifdef FEAT_DIFF
|
|
1547 if (curwin->w_topfill > 0)
|
|
1548 --curwin->w_topfill;
|
|
1549 else
|
|
1550 #endif
|
36
|
1551 {
|
|
1552 #ifdef FEAT_FOLDING
|
|
1553 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
|
|
1554 #endif
|
7
|
1555 ++curwin->w_topline;
|
36
|
1556 }
|
7
|
1557 ++curwin->w_botline; /* approximate w_botline */
|
|
1558 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
|
|
1559 }
|
|
1560 }
|
|
1561 #endif /* FEAT_INS_EXPAND */
|
|
1562
|
|
1563 /*
|
|
1564 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
|
|
1565 * a (wrapped) text line. Uses and sets "lp->fill".
|
|
1566 * Returns the height of the added line in "lp->height".
|
|
1567 * Lines above the first one are incredibly high.
|
|
1568 */
|
|
1569 static void
|
|
1570 topline_back(lp)
|
|
1571 lineoff_T *lp;
|
|
1572 {
|
|
1573 #ifdef FEAT_DIFF
|
|
1574 if (lp->fill < diff_check_fill(curwin, lp->lnum))
|
|
1575 {
|
|
1576 /* Add a filler line. */
|
|
1577 ++lp->fill;
|
|
1578 lp->height = 1;
|
|
1579 }
|
|
1580 else
|
|
1581 #endif
|
|
1582 {
|
|
1583 --lp->lnum;
|
|
1584 #ifdef FEAT_DIFF
|
|
1585 lp->fill = 0;
|
|
1586 #endif
|
|
1587 if (lp->lnum < 1)
|
|
1588 lp->height = MAXCOL;
|
|
1589 else
|
|
1590 #ifdef FEAT_FOLDING
|
|
1591 if (hasFolding(lp->lnum, &lp->lnum, NULL))
|
|
1592 /* Add a closed fold */
|
|
1593 lp->height = 1;
|
|
1594 else
|
|
1595 #endif
|
|
1596 {
|
|
1597 #ifdef FEAT_DIFF
|
|
1598 lp->height = plines_nofill(lp->lnum);
|
|
1599 #else
|
|
1600 lp->height = plines(lp->lnum);
|
|
1601 #endif
|
|
1602 }
|
|
1603 }
|
|
1604 }
|
|
1605
|
|
1606 /*
|
|
1607 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
|
|
1608 * a (wrapped) text line. Uses and sets "lp->fill".
|
|
1609 * Returns the height of the added line in "lp->height".
|
|
1610 * Lines below the last one are incredibly high.
|
|
1611 */
|
|
1612 static void
|
|
1613 botline_forw(lp)
|
|
1614 lineoff_T *lp;
|
|
1615 {
|
|
1616 #ifdef FEAT_DIFF
|
|
1617 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
|
|
1618 {
|
|
1619 /* Add a filler line. */
|
|
1620 ++lp->fill;
|
|
1621 lp->height = 1;
|
|
1622 }
|
|
1623 else
|
|
1624 #endif
|
|
1625 {
|
|
1626 ++lp->lnum;
|
|
1627 #ifdef FEAT_DIFF
|
|
1628 lp->fill = 0;
|
|
1629 #endif
|
|
1630 if (lp->lnum > curbuf->b_ml.ml_line_count)
|
|
1631 lp->height = MAXCOL;
|
|
1632 else
|
|
1633 #ifdef FEAT_FOLDING
|
|
1634 if (hasFolding(lp->lnum, NULL, &lp->lnum))
|
|
1635 /* Add a closed fold */
|
|
1636 lp->height = 1;
|
|
1637 else
|
|
1638 #endif
|
|
1639 {
|
|
1640 #ifdef FEAT_DIFF
|
|
1641 lp->height = plines_nofill(lp->lnum);
|
|
1642 #else
|
|
1643 lp->height = plines(lp->lnum);
|
|
1644 #endif
|
|
1645 }
|
|
1646 }
|
|
1647 }
|
|
1648
|
|
1649 #ifdef FEAT_DIFF
|
|
1650 /*
|
|
1651 * Switch from including filler lines below lp->lnum to including filler
|
|
1652 * lines above loff.lnum + 1. This keeps pointing to the same line.
|
|
1653 * When there are no filler lines nothing changes.
|
|
1654 */
|
|
1655 static void
|
|
1656 botline_topline(lp)
|
|
1657 lineoff_T *lp;
|
|
1658 {
|
|
1659 if (lp->fill > 0)
|
|
1660 {
|
|
1661 ++lp->lnum;
|
|
1662 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
|
|
1663 }
|
|
1664 }
|
|
1665
|
|
1666 /*
|
|
1667 * Switch from including filler lines above lp->lnum to including filler
|
|
1668 * lines below loff.lnum - 1. This keeps pointing to the same line.
|
|
1669 * When there are no filler lines nothing changes.
|
|
1670 */
|
|
1671 static void
|
|
1672 topline_botline(lp)
|
|
1673 lineoff_T *lp;
|
|
1674 {
|
|
1675 if (lp->fill > 0)
|
|
1676 {
|
|
1677 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
|
|
1678 --lp->lnum;
|
|
1679 }
|
|
1680 }
|
|
1681 #endif
|
|
1682
|
|
1683 /*
|
|
1684 * Recompute topline to put the cursor at the top of the window.
|
|
1685 * Scroll at least "min_scroll" lines.
|
|
1686 * If "always" is TRUE, always set topline (for "zt").
|
|
1687 */
|
|
1688 void
|
|
1689 scroll_cursor_top(min_scroll, always)
|
|
1690 int min_scroll;
|
|
1691 int always;
|
|
1692 {
|
|
1693 int scrolled = 0;
|
|
1694 int extra = 0;
|
|
1695 int used;
|
|
1696 int i;
|
|
1697 linenr_T top; /* just above displayed lines */
|
|
1698 linenr_T bot; /* just below displayed lines */
|
|
1699 linenr_T old_topline = curwin->w_topline;
|
|
1700 #ifdef FEAT_DIFF
|
|
1701 linenr_T old_topfill = curwin->w_topfill;
|
|
1702 #endif
|
|
1703 linenr_T new_topline;
|
|
1704 int off = p_so;
|
|
1705
|
|
1706 #ifdef FEAT_MOUSE
|
|
1707 if (mouse_dragging > 0)
|
|
1708 off = mouse_dragging - 1;
|
|
1709 #endif
|
|
1710
|
|
1711 /*
|
|
1712 * Decrease topline until:
|
|
1713 * - it has become 1
|
|
1714 * - (part of) the cursor line is moved off the screen or
|
|
1715 * - moved at least 'scrolljump' lines and
|
|
1716 * - at least 'scrolloff' lines above and below the cursor
|
|
1717 */
|
|
1718 validate_cheight();
|
|
1719 used = curwin->w_cline_height;
|
|
1720 if (curwin->w_cursor.lnum < curwin->w_topline)
|
|
1721 scrolled = used;
|
|
1722
|
|
1723 #ifdef FEAT_FOLDING
|
|
1724 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
|
|
1725 {
|
|
1726 --top;
|
|
1727 ++bot;
|
|
1728 }
|
|
1729 else
|
|
1730 #endif
|
|
1731 {
|
|
1732 top = curwin->w_cursor.lnum - 1;
|
|
1733 bot = curwin->w_cursor.lnum + 1;
|
|
1734 }
|
|
1735 new_topline = top + 1;
|
|
1736
|
|
1737 #ifdef FEAT_DIFF
|
|
1738 /* count filler lines of the cursor window as context */
|
|
1739 i = diff_check_fill(curwin, curwin->w_cursor.lnum);
|
|
1740 used += i;
|
|
1741 extra += i;
|
|
1742 #endif
|
|
1743
|
|
1744 /*
|
|
1745 * Check if the lines from "top" to "bot" fit in the window. If they do,
|
|
1746 * set new_topline and advance "top" and "bot" to include more lines.
|
|
1747 */
|
|
1748 while (top > 0)
|
|
1749 {
|
|
1750 #ifdef FEAT_FOLDING
|
|
1751 if (hasFolding(top, &top, NULL))
|
|
1752 /* count one logical line for a sequence of folded lines */
|
|
1753 i = 1;
|
|
1754 else
|
|
1755 #endif
|
|
1756 i = plines(top);
|
|
1757 used += i;
|
|
1758 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
|
|
1759 {
|
|
1760 #ifdef FEAT_FOLDING
|
|
1761 if (hasFolding(bot, NULL, &bot))
|
|
1762 /* count one logical line for a sequence of folded lines */
|
|
1763 ++used;
|
|
1764 else
|
|
1765 #endif
|
|
1766 used += plines(bot);
|
|
1767 }
|
|
1768 if (used > curwin->w_height)
|
|
1769 break;
|
|
1770 if (top < curwin->w_topline)
|
|
1771 scrolled += i;
|
|
1772
|
|
1773 /*
|
|
1774 * If scrolling is needed, scroll at least 'sj' lines.
|
|
1775 */
|
|
1776 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
|
|
1777 && extra >= off)
|
|
1778 break;
|
|
1779
|
|
1780 extra += i;
|
|
1781 new_topline = top;
|
|
1782 --top;
|
|
1783 ++bot;
|
|
1784 }
|
|
1785
|
|
1786 /*
|
|
1787 * If we don't have enough space, put cursor in the middle.
|
|
1788 * This makes sure we get the same position when using "k" and "j"
|
|
1789 * in a small window.
|
|
1790 */
|
|
1791 if (used > curwin->w_height)
|
|
1792 scroll_cursor_halfway(FALSE);
|
|
1793 else
|
|
1794 {
|
|
1795 /*
|
|
1796 * If "always" is FALSE, only adjust topline to a lower value, higher
|
|
1797 * value may happen with wrapping lines
|
|
1798 */
|
|
1799 if (new_topline < curwin->w_topline || always)
|
|
1800 curwin->w_topline = new_topline;
|
|
1801 if (curwin->w_topline > curwin->w_cursor.lnum)
|
|
1802 curwin->w_topline = curwin->w_cursor.lnum;
|
|
1803 #ifdef FEAT_DIFF
|
|
1804 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
|
|
1805 if (curwin->w_topfill > 0 && extra > off)
|
|
1806 {
|
|
1807 curwin->w_topfill -= extra - off;
|
|
1808 if (curwin->w_topfill < 0)
|
|
1809 curwin->w_topfill = 0;
|
|
1810 }
|
|
1811 check_topfill(curwin, FALSE);
|
|
1812 #endif
|
|
1813 if (curwin->w_topline != old_topline
|
|
1814 #ifdef FEAT_DIFF
|
|
1815 || curwin->w_topfill != old_topfill
|
|
1816 #endif
|
|
1817 )
|
|
1818 curwin->w_valid &=
|
|
1819 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
|
|
1820 curwin->w_valid |= VALID_TOPLINE;
|
|
1821 }
|
|
1822 }
|
|
1823
|
|
1824 /*
|
|
1825 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
|
|
1826 * screen lines for text lines.
|
|
1827 */
|
|
1828 void
|
|
1829 set_empty_rows(wp, used)
|
|
1830 win_T *wp;
|
|
1831 int used;
|
|
1832 {
|
|
1833 #ifdef FEAT_DIFF
|
|
1834 wp->w_filler_rows = 0;
|
|
1835 #endif
|
|
1836 if (used == 0)
|
|
1837 wp->w_empty_rows = 0; /* single line that doesn't fit */
|
|
1838 else
|
|
1839 {
|
|
1840 wp->w_empty_rows = wp->w_height - used;
|
|
1841 #ifdef FEAT_DIFF
|
|
1842 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
|
|
1843 {
|
|
1844 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
|
|
1845 if (wp->w_empty_rows > wp->w_filler_rows)
|
|
1846 wp->w_empty_rows -= wp->w_filler_rows;
|
|
1847 else
|
|
1848 {
|
|
1849 wp->w_filler_rows = wp->w_empty_rows;
|
|
1850 wp->w_empty_rows = 0;
|
|
1851 }
|
|
1852 }
|
|
1853 #endif
|
|
1854 }
|
|
1855 }
|
|
1856
|
|
1857 /*
|
|
1858 * Recompute topline to put the cursor at the bottom of the window.
|
|
1859 * Scroll at least "min_scroll" lines.
|
|
1860 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
|
|
1861 * This is messy stuff!!!
|
|
1862 */
|
|
1863 void
|
|
1864 scroll_cursor_bot(min_scroll, set_topbot)
|
|
1865 int min_scroll;
|
|
1866 int set_topbot;
|
|
1867 {
|
|
1868 int used;
|
|
1869 int scrolled = 0;
|
|
1870 int extra = 0;
|
|
1871 int i;
|
|
1872 linenr_T line_count;
|
|
1873 linenr_T old_topline = curwin->w_topline;
|
|
1874 lineoff_T loff;
|
|
1875 lineoff_T boff;
|
|
1876 #ifdef FEAT_DIFF
|
|
1877 int old_topfill = curwin->w_topfill;
|
|
1878 int fill_below_window;
|
|
1879 #endif
|
|
1880 linenr_T old_botline = curwin->w_botline;
|
|
1881 linenr_T old_valid = curwin->w_valid;
|
|
1882 int old_empty_rows = curwin->w_empty_rows;
|
|
1883 linenr_T cln; /* Cursor Line Number */
|
|
1884
|
|
1885 cln = curwin->w_cursor.lnum;
|
|
1886 if (set_topbot)
|
|
1887 {
|
|
1888 used = 0;
|
|
1889 curwin->w_botline = cln + 1;
|
|
1890 #ifdef FEAT_DIFF
|
|
1891 loff.fill = 0;
|
|
1892 #endif
|
|
1893 for (curwin->w_topline = curwin->w_botline;
|
|
1894 curwin->w_topline > 1;
|
|
1895 curwin->w_topline = loff.lnum)
|
|
1896 {
|
|
1897 loff.lnum = curwin->w_topline;
|
|
1898 topline_back(&loff);
|
|
1899 if (used + loff.height > curwin->w_height)
|
|
1900 break;
|
|
1901 used += loff.height;
|
|
1902 #ifdef FEAT_DIFF
|
|
1903 curwin->w_topfill = loff.fill;
|
|
1904 #endif
|
|
1905 }
|
|
1906 set_empty_rows(curwin, used);
|
|
1907 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
|
|
1908 if (curwin->w_topline != old_topline
|
|
1909 #ifdef FEAT_DIFF
|
|
1910 || curwin->w_topfill != old_topfill
|
|
1911 #endif
|
|
1912 )
|
|
1913 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
|
|
1914 }
|
|
1915 else
|
|
1916 validate_botline();
|
|
1917
|
|
1918 /* The lines of the cursor line itself are always used. */
|
|
1919 #ifdef FEAT_DIFF
|
|
1920 used = plines_nofill(cln);
|
|
1921 #else
|
|
1922 validate_cheight();
|
|
1923 used = curwin->w_cline_height;
|
|
1924 #endif
|
|
1925
|
|
1926 /* If the cursor is below botline, we will at least scroll by the height
|
|
1927 * of the cursor line. Correct for empty lines, which are really part of
|
|
1928 * botline. */
|
|
1929 if (cln >= curwin->w_botline)
|
|
1930 {
|
|
1931 scrolled = used;
|
|
1932 if (cln == curwin->w_botline)
|
|
1933 scrolled -= curwin->w_empty_rows;
|
|
1934 }
|
|
1935
|
|
1936 /*
|
|
1937 * Stop counting lines to scroll when
|
|
1938 * - hitting start of the file
|
|
1939 * - scrolled nothing or at least 'sj' lines
|
|
1940 * - at least 'so' lines below the cursor
|
|
1941 * - lines between botline and cursor have been counted
|
|
1942 */
|
|
1943 #ifdef FEAT_FOLDING
|
|
1944 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
|
|
1945 #endif
|
|
1946 {
|
|
1947 loff.lnum = cln;
|
|
1948 boff.lnum = cln;
|
|
1949 }
|
|
1950 #ifdef FEAT_DIFF
|
|
1951 loff.fill = 0;
|
|
1952 boff.fill = 0;
|
|
1953 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
|
|
1954 - curwin->w_filler_rows;
|
|
1955 #endif
|
|
1956
|
|
1957 while (loff.lnum > 1)
|
|
1958 {
|
|
1959 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
|
|
1960 * context for 'scrolloff' and counted all lines below the window. */
|
|
1961 if ((((scrolled <= 0 || scrolled >= min_scroll)
|
|
1962 && extra >= (
|
|
1963 #ifdef FEAT_MOUSE
|
|
1964 mouse_dragging ? mouse_dragging - 1 :
|
|
1965 #endif
|
|
1966 p_so))
|
|
1967 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
|
|
1968 && loff.lnum <= curwin->w_botline
|
|
1969 #ifdef FEAT_DIFF
|
|
1970 && (loff.lnum < curwin->w_botline
|
|
1971 || loff.fill >= fill_below_window)
|
|
1972 #endif
|
|
1973 )
|
|
1974 break;
|
|
1975
|
|
1976 /* Add one line above */
|
|
1977 topline_back(&loff);
|
|
1978 used += loff.height;
|
|
1979 if (used > curwin->w_height)
|
|
1980 break;
|
|
1981 if (loff.lnum >= curwin->w_botline
|
|
1982 #ifdef FEAT_DIFF
|
|
1983 && (loff.lnum > curwin->w_botline
|
|
1984 || loff.fill <= fill_below_window)
|
|
1985 #endif
|
|
1986 )
|
|
1987 {
|
|
1988 /* Count screen lines that are below the window. */
|
|
1989 scrolled += loff.height;
|
|
1990 if (loff.lnum == curwin->w_botline
|
|
1991 #ifdef FEAT_DIFF
|
|
1992 && boff.fill == 0
|
|
1993 #endif
|
|
1994 )
|
|
1995 scrolled -= curwin->w_empty_rows;
|
|
1996 }
|
|
1997
|
|
1998 if (boff.lnum < curbuf->b_ml.ml_line_count)
|
|
1999 {
|
|
2000 /* Add one line below */
|
|
2001 botline_forw(&boff);
|
|
2002 used += boff.height;
|
|
2003 if (used > curwin->w_height)
|
|
2004 break;
|
|
2005 if (extra < (
|
|
2006 #ifdef FEAT_MOUSE
|
|
2007 mouse_dragging > 0 ? mouse_dragging - 1 :
|
|
2008 #endif
|
|
2009 p_so) || scrolled < min_scroll)
|
|
2010 {
|
|
2011 extra += boff.height;
|
|
2012 if (boff.lnum >= curwin->w_botline
|
|
2013 #ifdef FEAT_DIFF
|
|
2014 || (boff.lnum + 1 == curwin->w_botline
|
|
2015 && boff.fill > curwin->w_filler_rows)
|
|
2016 #endif
|
|
2017 )
|
|
2018 {
|
|
2019 /* Count screen lines that are below the window. */
|
|
2020 scrolled += boff.height;
|
|
2021 if (boff.lnum == curwin->w_botline
|
|
2022 #ifdef FEAT_DIFF
|
|
2023 && boff.fill == 0
|
|
2024 #endif
|
|
2025 )
|
|
2026 scrolled -= curwin->w_empty_rows;
|
|
2027 }
|
|
2028 }
|
|
2029 }
|
|
2030 }
|
|
2031
|
|
2032 /* curwin->w_empty_rows is larger, no need to scroll */
|
|
2033 if (scrolled <= 0)
|
|
2034 line_count = 0;
|
|
2035 /* more than a screenfull, don't scroll but redraw */
|
|
2036 else if (used > curwin->w_height)
|
|
2037 line_count = used;
|
|
2038 /* scroll minimal number of lines */
|
|
2039 else
|
|
2040 {
|
|
2041 line_count = 0;
|
|
2042 #ifdef FEAT_DIFF
|
|
2043 boff.fill = curwin->w_topfill;
|
|
2044 #endif
|
|
2045 boff.lnum = curwin->w_topline - 1;
|
|
2046 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
|
|
2047 {
|
|
2048 botline_forw(&boff);
|
|
2049 i += boff.height;
|
|
2050 ++line_count;
|
|
2051 }
|
|
2052 if (i < scrolled) /* below curwin->w_botline, don't scroll */
|
|
2053 line_count = 9999;
|
|
2054 }
|
|
2055
|
|
2056 /*
|
|
2057 * Scroll up if the cursor is off the bottom of the screen a bit.
|
|
2058 * Otherwise put it at 1/2 of the screen.
|
|
2059 */
|
|
2060 if (line_count >= curwin->w_height && line_count > min_scroll)
|
|
2061 scroll_cursor_halfway(FALSE);
|
|
2062 else
|
|
2063 scrollup(line_count, TRUE);
|
|
2064
|
|
2065 /*
|
|
2066 * If topline didn't change we need to restore w_botline and w_empty_rows
|
|
2067 * (we changed them).
|
|
2068 * If topline did change, update_screen() will set botline.
|
|
2069 */
|
|
2070 if (curwin->w_topline == old_topline && set_topbot)
|
|
2071 {
|
|
2072 curwin->w_botline = old_botline;
|
|
2073 curwin->w_empty_rows = old_empty_rows;
|
|
2074 curwin->w_valid = old_valid;
|
|
2075 }
|
|
2076 curwin->w_valid |= VALID_TOPLINE;
|
|
2077 }
|
|
2078
|
|
2079 /*
|
|
2080 * Recompute topline to put the cursor halfway the window
|
|
2081 * If "atend" is TRUE, also put it halfway at the end of the file.
|
|
2082 */
|
|
2083 void
|
|
2084 scroll_cursor_halfway(atend)
|
|
2085 int atend;
|
|
2086 {
|
|
2087 int above = 0;
|
|
2088 linenr_T topline;
|
|
2089 #ifdef FEAT_DIFF
|
|
2090 int topfill = 0;
|
|
2091 #endif
|
|
2092 int below = 0;
|
|
2093 int used;
|
|
2094 lineoff_T loff;
|
|
2095 lineoff_T boff;
|
|
2096
|
|
2097 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
|
|
2098 #ifdef FEAT_FOLDING
|
|
2099 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
|
|
2100 #endif
|
|
2101 #ifdef FEAT_DIFF
|
|
2102 used = plines_nofill(loff.lnum);
|
|
2103 loff.fill = 0;
|
|
2104 boff.fill = 0;
|
|
2105 #else
|
|
2106 used = plines(loff.lnum);
|
|
2107 #endif
|
|
2108 topline = loff.lnum;
|
|
2109 while (topline > 1)
|
|
2110 {
|
|
2111 if (below <= above) /* add a line below the cursor first */
|
|
2112 {
|
|
2113 if (boff.lnum < curbuf->b_ml.ml_line_count)
|
|
2114 {
|
|
2115 botline_forw(&boff);
|
|
2116 used += boff.height;
|
|
2117 if (used > curwin->w_height)
|
|
2118 break;
|
|
2119 below += boff.height;
|
|
2120 }
|
|
2121 else
|
|
2122 {
|
|
2123 ++below; /* count a "~" line */
|
|
2124 if (atend)
|
|
2125 ++used;
|
|
2126 }
|
|
2127 }
|
|
2128
|
|
2129 if (below > above) /* add a line above the cursor */
|
|
2130 {
|
|
2131 topline_back(&loff);
|
|
2132 used += loff.height;
|
|
2133 if (used > curwin->w_height)
|
|
2134 break;
|
|
2135 above += loff.height;
|
|
2136 topline = loff.lnum;
|
|
2137 #ifdef FEAT_DIFF
|
|
2138 topfill = loff.fill;
|
|
2139 #endif
|
|
2140 }
|
|
2141 }
|
|
2142 #ifdef FEAT_FOLDING
|
|
2143 if (!hasFolding(topline, &curwin->w_topline, NULL))
|
|
2144 #endif
|
|
2145 curwin->w_topline = topline;
|
|
2146 #ifdef FEAT_DIFF
|
|
2147 curwin->w_topfill = topfill;
|
|
2148 check_topfill(curwin, FALSE);
|
|
2149 #endif
|
|
2150 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
|
|
2151 curwin->w_valid |= VALID_TOPLINE;
|
|
2152 }
|
|
2153
|
|
2154 /*
|
|
2155 * Correct the cursor position so that it is in a part of the screen at least
|
|
2156 * 'so' lines from the top and bottom, if possible.
|
|
2157 * If not possible, put it at the same position as scroll_cursor_halfway().
|
|
2158 * When called topline must be valid!
|
|
2159 */
|
|
2160 void
|
|
2161 cursor_correct()
|
|
2162 {
|
|
2163 int above = 0; /* screen lines above topline */
|
|
2164 linenr_T topline;
|
|
2165 int below = 0; /* screen lines below botline */
|
|
2166 linenr_T botline;
|
|
2167 int above_wanted, below_wanted;
|
|
2168 linenr_T cln; /* Cursor Line Number */
|
|
2169 int max_off;
|
|
2170
|
|
2171 /*
|
|
2172 * How many lines we would like to have above/below the cursor depends on
|
|
2173 * whether the first/last line of the file is on screen.
|
|
2174 */
|
|
2175 above_wanted = p_so;
|
|
2176 below_wanted = p_so;
|
|
2177 #ifdef FEAT_MOUSE
|
|
2178 if (mouse_dragging)
|
|
2179 {
|
|
2180 above_wanted = mouse_dragging - 1;
|
|
2181 below_wanted = mouse_dragging - 1;
|
|
2182 }
|
|
2183 #endif
|
|
2184 if (curwin->w_topline == 1)
|
|
2185 {
|
|
2186 above_wanted = 0;
|
|
2187 max_off = curwin->w_height / 2;
|
|
2188 if (below_wanted > max_off)
|
|
2189 below_wanted = max_off;
|
|
2190 }
|
|
2191 validate_botline();
|
|
2192 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
|
|
2193 #ifdef FEAT_MOUSE
|
|
2194 && !mouse_dragging
|
|
2195 #endif
|
|
2196 )
|
|
2197 {
|
|
2198 below_wanted = 0;
|
|
2199 max_off = (curwin->w_height - 1) / 2;
|
|
2200 if (above_wanted > max_off)
|
|
2201 above_wanted = max_off;
|
|
2202 }
|
|
2203
|
|
2204 /*
|
|
2205 * If there are sufficient file-lines above and below the cursor, we can
|
|
2206 * return now.
|
|
2207 */
|
|
2208 cln = curwin->w_cursor.lnum;
|
|
2209 if (cln >= curwin->w_topline + above_wanted
|
|
2210 && cln < curwin->w_botline - below_wanted
|
|
2211 #ifdef FEAT_FOLDING
|
|
2212 && !hasAnyFolding(curwin)
|
|
2213 #endif
|
|
2214 )
|
|
2215 return;
|
|
2216
|
|
2217 /*
|
|
2218 * Narrow down the area where the cursor can be put by taking lines from
|
|
2219 * the top and the bottom until:
|
|
2220 * - the desired context lines are found
|
|
2221 * - the lines from the top is past the lines from the bottom
|
|
2222 */
|
|
2223 topline = curwin->w_topline;
|
|
2224 botline = curwin->w_botline - 1;
|
|
2225 #ifdef FEAT_DIFF
|
|
2226 /* count filler lines as context */
|
|
2227 above = curwin->w_topfill;
|
|
2228 below = curwin->w_filler_rows;
|
|
2229 #endif
|
|
2230 while ((above < above_wanted || below < below_wanted) && topline < botline)
|
|
2231 {
|
|
2232 if (below < below_wanted && (below <= above || above >= above_wanted))
|
|
2233 {
|
|
2234 #ifdef FEAT_FOLDING
|
|
2235 if (hasFolding(botline, &botline, NULL))
|
|
2236 ++below;
|
|
2237 else
|
|
2238 #endif
|
|
2239 below += plines(botline);
|
|
2240 --botline;
|
|
2241 }
|
|
2242 if (above < above_wanted && (above < below || below >= below_wanted))
|
|
2243 {
|
|
2244 #ifdef FEAT_FOLDING
|
|
2245 if (hasFolding(topline, NULL, &topline))
|
|
2246 ++above;
|
|
2247 else
|
|
2248 #endif
|
|
2249 #ifndef FEAT_DIFF
|
|
2250 above += plines(topline);
|
|
2251 #else
|
|
2252 above += plines_nofill(topline);
|
|
2253
|
|
2254 /* Count filler lines below this line as context. */
|
|
2255 if (topline < botline)
|
|
2256 above += diff_check_fill(curwin, topline + 1);
|
|
2257 #endif
|
|
2258 ++topline;
|
|
2259 }
|
|
2260 }
|
|
2261 if (topline == botline || botline == 0)
|
|
2262 curwin->w_cursor.lnum = topline;
|
|
2263 else if (topline > botline)
|
|
2264 curwin->w_cursor.lnum = botline;
|
|
2265 else
|
|
2266 {
|
|
2267 if (cln < topline && curwin->w_topline > 1)
|
|
2268 {
|
|
2269 curwin->w_cursor.lnum = topline;
|
|
2270 curwin->w_valid &=
|
|
2271 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
|
|
2272 }
|
|
2273 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
|
|
2274 {
|
|
2275 curwin->w_cursor.lnum = botline;
|
|
2276 curwin->w_valid &=
|
|
2277 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
|
|
2278 }
|
|
2279 }
|
|
2280 curwin->w_valid |= VALID_TOPLINE;
|
|
2281 }
|
|
2282
|
|
2283 static void get_scroll_overlap __ARGS((lineoff_T *lp, int dir));
|
|
2284
|
|
2285 /*
|
|
2286 * move screen 'count' pages up or down and update screen
|
|
2287 *
|
|
2288 * return FAIL for failure, OK otherwise
|
|
2289 */
|
|
2290 int
|
|
2291 onepage(dir, count)
|
|
2292 int dir;
|
|
2293 long count;
|
|
2294 {
|
|
2295 long n;
|
|
2296 int retval = OK;
|
|
2297 lineoff_T loff;
|
|
2298 linenr_T old_topline = curwin->w_topline;
|
|
2299
|
|
2300 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
|
|
2301 {
|
|
2302 beep_flush();
|
|
2303 return FAIL;
|
|
2304 }
|
|
2305
|
|
2306 for ( ; count > 0; --count)
|
|
2307 {
|
|
2308 validate_botline();
|
|
2309 /*
|
|
2310 * It's an error to move a page up when the first line is already on
|
|
2311 * the screen. It's an error to move a page down when the last line
|
|
2312 * is on the screen and the topline is 'scrolloff' lines from the
|
|
2313 * last line.
|
|
2314 */
|
|
2315 if (dir == FORWARD
|
|
2316 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
|
|
2317 && curwin->w_botline > curbuf->b_ml.ml_line_count)
|
|
2318 : (curwin->w_topline == 1
|
|
2319 #ifdef FEAT_DIFF
|
|
2320 && curwin->w_topfill ==
|
|
2321 diff_check_fill(curwin, curwin->w_topline)
|
|
2322 #endif
|
|
2323 ))
|
|
2324 {
|
|
2325 beep_flush();
|
|
2326 retval = FAIL;
|
|
2327 break;
|
|
2328 }
|
|
2329
|
|
2330 #ifdef FEAT_DIFF
|
|
2331 loff.fill = 0;
|
|
2332 #endif
|
|
2333 if (dir == FORWARD)
|
|
2334 {
|
164
|
2335 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
|
7
|
2336 {
|
164
|
2337 /* Vi compatible scrolling */
|
|
2338 if (p_window <= 2)
|
|
2339 ++curwin->w_topline;
|
|
2340 else
|
|
2341 curwin->w_topline += p_window - 2;
|
|
2342 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
|
|
2343 curwin->w_topline = curbuf->b_ml.ml_line_count;
|
|
2344 curwin->w_cursor.lnum = curwin->w_topline;
|
|
2345 }
|
|
2346 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
|
|
2347 {
|
|
2348 /* at end of file */
|
7
|
2349 curwin->w_topline = curbuf->b_ml.ml_line_count;
|
|
2350 #ifdef FEAT_DIFF
|
|
2351 curwin->w_topfill = 0;
|
|
2352 #endif
|
|
2353 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
|
|
2354 }
|
|
2355 else
|
|
2356 {
|
|
2357 /* For the overlap, start with the line just below the window
|
|
2358 * and go upwards. */
|
|
2359 loff.lnum = curwin->w_botline;
|
|
2360 #ifdef FEAT_DIFF
|
|
2361 loff.fill = diff_check_fill(curwin, loff.lnum)
|
|
2362 - curwin->w_filler_rows;
|
|
2363 #endif
|
|
2364 get_scroll_overlap(&loff, -1);
|
|
2365 curwin->w_topline = loff.lnum;
|
|
2366 #ifdef FEAT_DIFF
|
|
2367 curwin->w_topfill = loff.fill;
|
|
2368 check_topfill(curwin, FALSE);
|
|
2369 #endif
|
|
2370 curwin->w_cursor.lnum = curwin->w_topline;
|
|
2371 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
|
|
2372 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
|
|
2373 }
|
|
2374 }
|
|
2375 else /* dir == BACKWARDS */
|
|
2376 {
|
|
2377 #ifdef FEAT_DIFF
|
|
2378 if (curwin->w_topline == 1)
|
|
2379 {
|
|
2380 /* Include max number of filler lines */
|
|
2381 max_topfill();
|
|
2382 continue;
|
|
2383 }
|
|
2384 #endif
|
164
|
2385 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
|
|
2386 {
|
|
2387 /* Vi compatible scrolling (sort of) */
|
|
2388 if (p_window <= 2)
|
|
2389 --curwin->w_topline;
|
|
2390 else
|
|
2391 curwin->w_topline -= p_window - 2;
|
|
2392 if (curwin->w_topline < 1)
|
|
2393 curwin->w_topline = 1;
|
|
2394 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
|
|
2395 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
|
|
2396 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
|
|
2397 continue;
|
|
2398 }
|
|
2399
|
7
|
2400 /* Find the line at the top of the window that is going to be the
|
|
2401 * line at the bottom of the window. Make sure this results in
|
|
2402 * the same line as before doing CTRL-F. */
|
|
2403 loff.lnum = curwin->w_topline - 1;
|
|
2404 #ifdef FEAT_DIFF
|
|
2405 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
|
|
2406 - curwin->w_topfill;
|
|
2407 #endif
|
|
2408 get_scroll_overlap(&loff, 1);
|
|
2409
|
|
2410 if (loff.lnum >= curbuf->b_ml.ml_line_count)
|
|
2411 {
|
|
2412 loff.lnum = curbuf->b_ml.ml_line_count;
|
|
2413 #ifdef FEAT_DIFF
|
|
2414 loff.fill = 0;
|
|
2415 }
|
|
2416 else
|
|
2417 {
|
|
2418 botline_topline(&loff);
|
|
2419 #endif
|
|
2420 }
|
|
2421 curwin->w_cursor.lnum = loff.lnum;
|
|
2422
|
|
2423 /* Find the line just above the new topline to get the right line
|
|
2424 * at the bottom of the window. */
|
|
2425 n = 0;
|
|
2426 while (n <= curwin->w_height && loff.lnum >= 1)
|
|
2427 {
|
|
2428 topline_back(&loff);
|
|
2429 n += loff.height;
|
|
2430 }
|
|
2431 if (n <= curwin->w_height) /* at begin of file */
|
|
2432 {
|
|
2433 curwin->w_topline = 1;
|
|
2434 #ifdef FEAT_DIFF
|
|
2435 max_topfill();
|
|
2436 #endif
|
|
2437 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
|
|
2438 }
|
|
2439 else
|
|
2440 {
|
|
2441 /* Go two lines forward again. */
|
|
2442 #ifdef FEAT_DIFF
|
|
2443 topline_botline(&loff);
|
|
2444 #endif
|
|
2445 botline_forw(&loff);
|
|
2446 botline_forw(&loff);
|
|
2447 #ifdef FEAT_DIFF
|
|
2448 botline_topline(&loff);
|
|
2449 #endif
|
|
2450 #ifdef FEAT_FOLDING
|
|
2451 /* We're at the wrong end of a fold now. */
|
|
2452 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
|
|
2453 #endif
|
|
2454
|
|
2455 /* Always scroll at least one line. Avoid getting stuck on
|
|
2456 * very long lines. */
|
|
2457 if (loff.lnum >= curwin->w_topline
|
|
2458 #ifdef FEAT_DIFF
|
|
2459 && (loff.lnum > curwin->w_topline
|
|
2460 || loff.fill >= curwin->w_topfill)
|
|
2461 #endif
|
|
2462 )
|
|
2463 {
|
|
2464 #ifdef FEAT_DIFF
|
|
2465 /* First try using the maximum number of filler lines. If
|
|
2466 * that's not enough, backup one line. */
|
|
2467 loff.fill = curwin->w_topfill;
|
|
2468 if (curwin->w_topfill < diff_check_fill(curwin,
|
|
2469 curwin->w_topline))
|
|
2470 max_topfill();
|
|
2471 if (curwin->w_topfill == loff.fill)
|
|
2472 #endif
|
|
2473 {
|
|
2474 --curwin->w_topline;
|
|
2475 #ifdef FEAT_DIFF
|
|
2476 curwin->w_topfill = 0;
|
|
2477 #endif
|
|
2478 }
|
|
2479 comp_botline(curwin);
|
|
2480 curwin->w_cursor.lnum = curwin->w_botline - 1;
|
|
2481 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|
|
|
2482 VALID_WROW|VALID_CROW);
|
|
2483 }
|
|
2484 else
|
|
2485 {
|
|
2486 curwin->w_topline = loff.lnum;
|
|
2487 #ifdef FEAT_DIFF
|
|
2488 curwin->w_topfill = loff.fill;
|
|
2489 check_topfill(curwin, FALSE);
|
|
2490 #endif
|
|
2491 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
|
|
2492 }
|
|
2493 }
|
|
2494 }
|
|
2495 }
|
|
2496 #ifdef FEAT_FOLDING
|
|
2497 foldAdjustCursor();
|
|
2498 #endif
|
|
2499 cursor_correct();
|
161
|
2500 if (retval == OK)
|
|
2501 beginline(BL_SOL | BL_FIX);
|
7
|
2502 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
|
|
2503
|
|
2504 /*
|
|
2505 * Avoid the screen jumping up and down when 'scrolloff' is non-zero.
|
|
2506 * But make sure we scroll at least one line (happens with mix of long
|
|
2507 * wrapping lines and non-wrapping line).
|
|
2508 */
|
|
2509 if (retval == OK && dir == FORWARD && check_top_offset())
|
|
2510 {
|
|
2511 scroll_cursor_top(1, FALSE);
|
|
2512 if (curwin->w_topline <= old_topline
|
|
2513 && old_topline < curbuf->b_ml.ml_line_count)
|
|
2514 {
|
|
2515 curwin->w_topline = old_topline + 1;
|
|
2516 #ifdef FEAT_FOLDING
|
|
2517 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
|
|
2518 #endif
|
|
2519 }
|
|
2520 }
|
|
2521
|
|
2522 redraw_later(VALID);
|
|
2523 return retval;
|
|
2524 }
|
|
2525
|
|
2526 /*
|
|
2527 * Decide how much overlap to use for page-up or page-down scrolling.
|
|
2528 * This is symmetric, so that doing both keeps the same lines displayed.
|
|
2529 * Three lines are examined:
|
|
2530 *
|
|
2531 * before CTRL-F after CTRL-F / before CTRL-B
|
|
2532 * etc. l1
|
|
2533 * l1 last but one line ------------
|
|
2534 * l2 last text line l2 top text line
|
|
2535 * ------------- l3 second text line
|
|
2536 * l3 etc.
|
|
2537 */
|
|
2538 static void
|
|
2539 get_scroll_overlap(lp, dir)
|
|
2540 lineoff_T *lp;
|
|
2541 int dir;
|
|
2542 {
|
|
2543 int h1, h2, h3, h4;
|
|
2544 int min_height = curwin->w_height - 2;
|
|
2545 lineoff_T loff0, loff1, loff2;
|
|
2546
|
|
2547 #ifdef FEAT_DIFF
|
|
2548 if (lp->fill > 0)
|
|
2549 lp->height = 1;
|
|
2550 else
|
|
2551 lp->height = plines_nofill(lp->lnum);
|
|
2552 #else
|
|
2553 lp->height = plines(lp->lnum);
|
|
2554 #endif
|
|
2555 h1 = lp->height;
|
|
2556 if (h1 > min_height)
|
|
2557 return; /* no overlap */
|
|
2558
|
|
2559 loff0 = *lp;
|
|
2560 if (dir > 0)
|
|
2561 botline_forw(lp);
|
|
2562 else
|
|
2563 topline_back(lp);
|
|
2564 h2 = lp->height;
|
|
2565 if (h2 + h1 > min_height)
|
|
2566 {
|
|
2567 *lp = loff0; /* no overlap */
|
|
2568 return;
|
|
2569 }
|
|
2570
|
|
2571 loff1 = *lp;
|
|
2572 if (dir > 0)
|
|
2573 botline_forw(lp);
|
|
2574 else
|
|
2575 topline_back(lp);
|
|
2576 h3 = lp->height;
|
|
2577 if (h3 + h2 > min_height)
|
|
2578 {
|
|
2579 *lp = loff0; /* no overlap */
|
|
2580 return;
|
|
2581 }
|
|
2582
|
|
2583 loff2 = *lp;
|
|
2584 if (dir > 0)
|
|
2585 botline_forw(lp);
|
|
2586 else
|
|
2587 topline_back(lp);
|
|
2588 h4 = lp->height;
|
|
2589 if (h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
|
|
2590 *lp = loff1; /* 1 line overlap */
|
|
2591 else
|
|
2592 *lp = loff2; /* 2 lines overlap */
|
|
2593 return;
|
|
2594 }
|
|
2595
|
|
2596 /* #define KEEP_SCREEN_LINE */
|
|
2597 /*
|
|
2598 * Scroll 'scroll' lines up or down.
|
|
2599 */
|
|
2600 void
|
|
2601 halfpage(flag, Prenum)
|
|
2602 int flag;
|
|
2603 linenr_T Prenum;
|
|
2604 {
|
|
2605 long scrolled = 0;
|
|
2606 int i;
|
|
2607 int n;
|
|
2608 int room;
|
|
2609
|
|
2610 if (Prenum)
|
|
2611 curwin->w_p_scr = (Prenum > curwin->w_height) ?
|
|
2612 curwin->w_height : Prenum;
|
|
2613 n = (curwin->w_p_scr <= curwin->w_height) ?
|
|
2614 curwin->w_p_scr : curwin->w_height;
|
|
2615
|
|
2616 validate_botline();
|
|
2617 room = curwin->w_empty_rows;
|
|
2618 #ifdef FEAT_DIFF
|
|
2619 room += curwin->w_filler_rows;
|
|
2620 #endif
|
|
2621 if (flag)
|
|
2622 {
|
|
2623 /*
|
|
2624 * scroll the text up
|
|
2625 */
|
|
2626 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
|
|
2627 {
|
|
2628 #ifdef FEAT_DIFF
|
|
2629 if (curwin->w_topfill > 0)
|
|
2630 {
|
|
2631 i = 1;
|
|
2632 if (--n < 0 && scrolled > 0)
|
|
2633 break;
|
|
2634 --curwin->w_topfill;
|
|
2635 }
|
|
2636 else
|
|
2637 #endif
|
|
2638 {
|
|
2639 #ifdef FEAT_DIFF
|
|
2640 i = plines_nofill(curwin->w_topline);
|
|
2641 #else
|
|
2642 i = plines(curwin->w_topline);
|
|
2643 #endif
|
|
2644 n -= i;
|
|
2645 if (n < 0 && scrolled > 0)
|
|
2646 break;
|
|
2647 #ifdef FEAT_FOLDING
|
|
2648 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
|
|
2649 #endif
|
|
2650 ++curwin->w_topline;
|
|
2651 #ifdef FEAT_DIFF
|
|
2652 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
|
|
2653 #endif
|
|
2654
|
|
2655 #ifndef KEEP_SCREEN_LINE
|
|
2656 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
|
|
2657 {
|
|
2658 ++curwin->w_cursor.lnum;
|
|
2659 curwin->w_valid &=
|
|
2660 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
|
|
2661 }
|
|
2662 #endif
|
|
2663 }
|
|
2664 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
|
|
2665 scrolled += i;
|
|
2666
|
|
2667 /*
|
|
2668 * Correct w_botline for changed w_topline.
|
|
2669 * Won't work when there are filler lines.
|
|
2670 */
|
|
2671 #ifdef FEAT_DIFF
|
|
2672 if (curwin->w_p_diff)
|
|
2673 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
|
|
2674 else
|
|
2675 #endif
|
|
2676 {
|
|
2677 room += i;
|
|
2678 do
|
|
2679 {
|
|
2680 i = plines(curwin->w_botline);
|
|
2681 if (i > room)
|
|
2682 break;
|
|
2683 #ifdef FEAT_FOLDING
|
|
2684 (void)hasFolding(curwin->w_botline, NULL,
|
|
2685 &curwin->w_botline);
|
|
2686 #endif
|
|
2687 ++curwin->w_botline;
|
|
2688 room -= i;
|
|
2689 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
|
|
2690 }
|
|
2691 }
|
|
2692
|
|
2693 #ifndef KEEP_SCREEN_LINE
|
|
2694 /*
|
|
2695 * When hit bottom of the file: move cursor down.
|
|
2696 */
|
|
2697 if (n > 0)
|
|
2698 {
|
|
2699 # ifdef FEAT_FOLDING
|
|
2700 if (hasAnyFolding(curwin))
|
|
2701 {
|
|
2702 while (--n >= 0
|
|
2703 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
|
|
2704 {
|
|
2705 (void)hasFolding(curwin->w_cursor.lnum, NULL,
|
|
2706 &curwin->w_cursor.lnum);
|
|
2707 ++curwin->w_cursor.lnum;
|
|
2708 }
|
|
2709 }
|
|
2710 else
|
|
2711 # endif
|
|
2712 curwin->w_cursor.lnum += n;
|
|
2713 check_cursor_lnum();
|
|
2714 }
|
|
2715 #else
|
|
2716 /* try to put the cursor in the same screen line */
|
|
2717 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
|
|
2718 && curwin->w_cursor.lnum < curwin->w_botline - 1)
|
|
2719 {
|
|
2720 scrolled -= plines(curwin->w_cursor.lnum);
|
|
2721 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
|
|
2722 break;
|
|
2723 # ifdef FEAT_FOLDING
|
|
2724 (void)hasFolding(curwin->w_cursor.lnum, NULL,
|
|
2725 &curwin->w_cursor.lnum);
|
|
2726 # endif
|
|
2727 ++curwin->w_cursor.lnum;
|
|
2728 }
|
|
2729 #endif
|
|
2730 }
|
|
2731 else
|
|
2732 {
|
|
2733 /*
|
|
2734 * scroll the text down
|
|
2735 */
|
|
2736 while (n > 0 && curwin->w_topline > 1)
|
|
2737 {
|
|
2738 #ifdef FEAT_DIFF
|
|
2739 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
|
|
2740 {
|
|
2741 i = 1;
|
|
2742 if (--n < 0 && scrolled > 0)
|
|
2743 break;
|
|
2744 ++curwin->w_topfill;
|
|
2745 }
|
|
2746 else
|
|
2747 #endif
|
|
2748 {
|
|
2749 #ifdef FEAT_DIFF
|
|
2750 i = plines_nofill(curwin->w_topline - 1);
|
|
2751 #else
|
|
2752 i = plines(curwin->w_topline - 1);
|
|
2753 #endif
|
|
2754 n -= i;
|
|
2755 if (n < 0 && scrolled > 0)
|
|
2756 break;
|
|
2757 --curwin->w_topline;
|
|
2758 #ifdef FEAT_FOLDING
|
|
2759 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
|
|
2760 #endif
|
|
2761 #ifdef FEAT_DIFF
|
|
2762 curwin->w_topfill = 0;
|
|
2763 #endif
|
|
2764 }
|
|
2765 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
|
|
2766 VALID_BOTLINE|VALID_BOTLINE_AP);
|
|
2767 scrolled += i;
|
|
2768 #ifndef KEEP_SCREEN_LINE
|
|
2769 if (curwin->w_cursor.lnum > 1)
|
|
2770 {
|
|
2771 --curwin->w_cursor.lnum;
|
|
2772 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
|
|
2773 }
|
|
2774 #endif
|
|
2775 }
|
|
2776 #ifndef KEEP_SCREEN_LINE
|
|
2777 /*
|
|
2778 * When hit top of the file: move cursor up.
|
|
2779 */
|
|
2780 if (n > 0)
|
|
2781 {
|
|
2782 if (curwin->w_cursor.lnum <= (linenr_T)n)
|
|
2783 curwin->w_cursor.lnum = 1;
|
|
2784 else
|
|
2785 # ifdef FEAT_FOLDING
|
|
2786 if (hasAnyFolding(curwin))
|
|
2787 {
|
|
2788 while (--n >= 0 && curwin->w_cursor.lnum > 1)
|
|
2789 {
|
|
2790 --curwin->w_cursor.lnum;
|
|
2791 (void)hasFolding(curwin->w_cursor.lnum,
|
|
2792 &curwin->w_cursor.lnum, NULL);
|
|
2793 }
|
|
2794 }
|
|
2795 else
|
|
2796 # endif
|
|
2797 curwin->w_cursor.lnum -= n;
|
|
2798 }
|
|
2799 #else
|
|
2800 /* try to put the cursor in the same screen line */
|
|
2801 scrolled += n; /* move cursor when topline is 1 */
|
|
2802 while (curwin->w_cursor.lnum > curwin->w_topline
|
|
2803 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
|
|
2804 {
|
|
2805 scrolled -= plines(curwin->w_cursor.lnum - 1);
|
|
2806 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
|
|
2807 break;
|
|
2808 --curwin->w_cursor.lnum;
|
|
2809 # ifdef FEAT_FOLDING
|
|
2810 foldAdjustCursor();
|
|
2811 # endif
|
|
2812 }
|
|
2813 #endif
|
|
2814 }
|
|
2815 # ifdef FEAT_FOLDING
|
|
2816 /* Move cursor to first line of closed fold. */
|
|
2817 foldAdjustCursor();
|
|
2818 # endif
|
|
2819 #ifdef FEAT_DIFF
|
|
2820 check_topfill(curwin, !flag);
|
|
2821 #endif
|
|
2822 cursor_correct();
|
|
2823 beginline(BL_SOL | BL_FIX);
|
|
2824 redraw_later(VALID);
|
|
2825 }
|