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