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