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