blob: dca4f2666b717dc6b44b8d5e3c9b74fb7c7eeaab [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 */
169 if (mouse_dragging)
170 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
845 if (wp->w_p_cuc)
846 redraw_win_later(wp, SOME_VALID);
847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849}
850
851/*
852 * Validate curwin->w_cline_height only.
853 */
854 static void
855validate_cheight()
856{
857 check_cursor_moved(curwin);
858 if (!(curwin->w_valid & VALID_CHEIGHT))
859 {
860#ifdef FEAT_DIFF
861 if (curwin->w_cursor.lnum == curwin->w_topline)
862 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
863 + curwin->w_topfill;
864 else
865#endif
866 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
867#ifdef FEAT_FOLDING
868 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
869#endif
870 curwin->w_valid |= VALID_CHEIGHT;
871 }
872}
873
874/*
875 * validate w_wcol and w_virtcol only. Only correct when 'wrap' on!
876 */
877 void
878validate_cursor_col()
879{
880 colnr_T off;
881 colnr_T col;
882
883 validate_virtcol();
884 if (!(curwin->w_valid & VALID_WCOL))
885 {
886 col = curwin->w_virtcol;
887 off = curwin_col_off();
888 col += off;
889
890 /* long line wrapping, adjust curwin->w_wrow */
891 if (curwin->w_p_wrap && col >= (colnr_T)W_WIDTH(curwin)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000892 && W_WIDTH(curwin) - off + curwin_col_off2() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
894 col -= W_WIDTH(curwin);
895 col = col % (W_WIDTH(curwin) - off + curwin_col_off2());
896 }
897 curwin->w_wcol = col;
898 curwin->w_valid |= VALID_WCOL;
899 }
900}
901
902/*
903 * Compute offset of a window, occupied by line number, fold column and sign
904 * column (these don't move when scrolling horizontally).
905 */
906 int
907win_col_off(wp)
908 win_T *wp;
909{
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000910 return ((wp->w_p_nu ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#ifdef FEAT_CMDWIN
912 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
913#endif
914#ifdef FEAT_FOLDING
915 + wp->w_p_fdc
916#endif
917#ifdef FEAT_SIGNS
918 + (
919# ifdef FEAT_NETBEANS_INTG
920 /* always show glyph gutter in netbeans */
921 usingNetbeans ||
922# endif
923 wp->w_buffer->b_signlist != NULL ? 2 : 0)
924#endif
925 );
926}
927
928 int
929curwin_col_off()
930{
931 return win_col_off(curwin);
932}
933
934/*
935 * Return the difference in column offset for the second screen line of a
936 * wrapped line. It's 8 if 'number' is on and 'n' is in 'cpoptions'.
937 */
938 int
939win_col_off2(wp)
940 win_T *wp;
941{
942 if (wp->w_p_nu && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000943 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 return 0;
945}
946
947 int
948curwin_col_off2()
949{
950 return win_col_off2(curwin);
951}
952
953/*
954 * compute curwin->w_wcol and curwin->w_virtcol.
955 * Also updates curwin->w_wrow and curwin->w_cline_row.
956 * Also updates curwin->w_leftcol.
957 */
958 void
959curs_columns(scroll)
960 int scroll; /* when TRUE, may scroll horizontally */
961{
962 int diff;
963 int extra; /* offset for first screen line */
964 int off_left, off_right;
965 int n;
966 int p_lines;
967 int width = 0;
968 int textwidth;
969 int new_leftcol;
970 colnr_T startcol;
971 colnr_T endcol;
972 colnr_T prev_skipcol;
973
974 /*
975 * First make sure that w_topline is valid (after moving the cursor).
976 */
977 update_topline();
978
979 /*
980 * Next make sure that w_cline_row is valid.
981 */
982 if (!(curwin->w_valid & VALID_CROW))
983 curs_rows(curwin, FALSE);
984
985 /*
986 * Compute the number of virtual columns.
987 */
988#ifdef FEAT_FOLDING
989 if (curwin->w_cline_folded)
990 /* In a folded line the cursor is always in the first column */
991 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
992 else
993#endif
994 getvvcol(curwin, &curwin->w_cursor,
995 &startcol, &(curwin->w_virtcol), &endcol);
996
997 /* remove '$' from change command when cursor moves onto it */
998 if (startcol > dollar_vcol)
999 dollar_vcol = 0;
1000
1001 extra = curwin_col_off();
1002 curwin->w_wcol = curwin->w_virtcol + extra;
1003 endcol += extra;
1004
1005 /*
1006 * Now compute w_wrow, counting screen lines from w_cline_row.
1007 */
1008 curwin->w_wrow = curwin->w_cline_row;
1009
1010 textwidth = W_WIDTH(curwin) - extra;
1011 if (textwidth <= 0)
1012 {
1013 /* No room for text, put cursor in last char of window. */
1014 curwin->w_wcol = W_WIDTH(curwin) - 1;
1015 curwin->w_wrow = curwin->w_height - 1;
1016 }
1017 else if (curwin->w_p_wrap
1018#ifdef FEAT_VERTSPLIT
1019 && curwin->w_width != 0
1020#endif
1021 )
1022 {
1023 width = textwidth + curwin_col_off2();
1024
1025 /* long line wrapping, adjust curwin->w_wrow */
1026 if (curwin->w_wcol >= W_WIDTH(curwin))
1027 {
1028 n = (curwin->w_wcol - W_WIDTH(curwin)) / width + 1;
1029 curwin->w_wcol -= n * width;
1030 curwin->w_wrow += n;
1031
1032#ifdef FEAT_LINEBREAK
1033 /* When cursor wraps to first char of next line in Insert
1034 * mode, the 'showbreak' string isn't shown, backup to first
1035 * column */
1036 if (*p_sbr && *ml_get_cursor() == NUL
1037 && curwin->w_wcol == (int)vim_strsize(p_sbr))
1038 curwin->w_wcol = 0;
1039#endif
1040 }
1041 }
1042
1043 /* No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1044 * is not folded.
1045 * If scrolling is off, curwin->w_leftcol is assumed to be 0 */
1046 else if (scroll
1047#ifdef FEAT_FOLDING
1048 && !curwin->w_cline_folded
1049#endif
1050 )
1051 {
1052 /*
1053 * If Cursor is left of the screen, scroll rightwards.
1054 * If Cursor is right of the screen, scroll leftwards
1055 * If we get closer to the edge than 'sidescrolloff', scroll a little
1056 * extra
1057 */
1058 off_left = (int)startcol - (int)curwin->w_leftcol - p_siso;
1059 off_right = (int)endcol - (int)(curwin->w_leftcol + W_WIDTH(curwin)
1060 - p_siso) + 1;
1061 if (off_left < 0 || off_right > 0)
1062 {
1063 if (off_left < 0)
1064 diff = -off_left;
1065 else
1066 diff = off_right;
1067
1068 /* When far off or not enough room on either side, put cursor in
1069 * middle of window. */
1070 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1071 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1072 else
1073 {
1074 if (diff < p_ss)
1075 diff = p_ss;
1076 if (off_left < 0)
1077 new_leftcol = curwin->w_leftcol - diff;
1078 else
1079 new_leftcol = curwin->w_leftcol + diff;
1080 }
1081 if (new_leftcol < 0)
1082 new_leftcol = 0;
1083 if (new_leftcol != (int)curwin->w_leftcol)
1084 {
1085 curwin->w_leftcol = new_leftcol;
1086 /* screen has to be redrawn with new curwin->w_leftcol */
1087 redraw_later(NOT_VALID);
1088 }
1089 }
1090 curwin->w_wcol -= curwin->w_leftcol;
1091 }
1092 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1093 curwin->w_wcol -= curwin->w_leftcol;
1094 else
1095 curwin->w_wcol = 0;
1096
1097#ifdef FEAT_DIFF
1098 /* Skip over filler lines. At the top use w_topfill, there
1099 * may be some filler lines above the window. */
1100 if (curwin->w_cursor.lnum == curwin->w_topline)
1101 curwin->w_wrow += curwin->w_topfill;
1102 else
1103 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1104#endif
1105
1106 prev_skipcol = curwin->w_skipcol;
1107
1108 p_lines = 0;
1109 if ((curwin->w_wrow >= curwin->w_height
1110 || ((prev_skipcol > 0
1111 || curwin->w_wrow + p_so >= curwin->w_height)
1112 && (p_lines =
1113#ifdef FEAT_DIFF
1114 plines_win_nofill
1115#else
1116 plines_win
1117#endif
1118 (curwin, curwin->w_cursor.lnum, FALSE))
1119 - 1 >= curwin->w_height))
1120 && curwin->w_height != 0
1121 && curwin->w_cursor.lnum == curwin->w_topline
1122 && width > 0
1123#ifdef FEAT_VERTSPLIT
1124 && curwin->w_width != 0
1125#endif
1126 )
1127 {
1128 /* Cursor past end of screen. Happens with a single line that does
1129 * not fit on screen. Find a skipcol to show the text around the
1130 * cursor. Avoid scrolling all the time. compute value of "extra":
1131 * 1: Less than "p_so" lines above
1132 * 2: Less than "p_so" lines below
1133 * 3: both of them */
1134 extra = 0;
1135 if (curwin->w_skipcol + p_so * width > curwin->w_virtcol)
1136 extra = 1;
1137 /* Compute last display line of the buffer line that we want at the
1138 * bottom of the window. */
1139 if (p_lines == 0)
1140 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1141 --p_lines;
1142 if (p_lines > curwin->w_wrow + p_so)
1143 n = curwin->w_wrow + p_so;
1144 else
1145 n = p_lines;
1146 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1147 extra += 2;
1148
1149 if (extra == 3 || p_lines < p_so * 2)
1150 {
1151 /* not enough room for 'scrolloff', put cursor in the middle */
1152 n = curwin->w_virtcol / width;
1153 if (n > curwin->w_height / 2)
1154 n -= curwin->w_height / 2;
1155 else
1156 n = 0;
1157 /* don't skip more than necessary */
1158 if (n > p_lines - curwin->w_height + 1)
1159 n = p_lines - curwin->w_height + 1;
1160 curwin->w_skipcol = n * width;
1161 }
1162 else if (extra == 1)
1163 {
1164 /* less then 'scrolloff' lines above, decrease skipcol */
1165 extra = (curwin->w_skipcol + p_so * width - curwin->w_virtcol
1166 + width - 1) / width;
1167 if (extra > 0)
1168 {
1169 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1170 extra = curwin->w_skipcol / width;
1171 curwin->w_skipcol -= extra * width;
1172 }
1173 }
1174 else if (extra == 2)
1175 {
1176 /* less then 'scrolloff' lines below, increase skipcol */
1177 endcol = (n - curwin->w_height + 1) * width;
1178 while (endcol > curwin->w_virtcol)
1179 endcol -= width;
1180 if (endcol > curwin->w_skipcol)
1181 curwin->w_skipcol = endcol;
1182 }
1183
1184 curwin->w_wrow -= curwin->w_skipcol / width;
1185 if (curwin->w_wrow >= curwin->w_height)
1186 {
1187 /* small window, make sure cursor is in it */
1188 extra = curwin->w_wrow - curwin->w_height + 1;
1189 curwin->w_skipcol += extra * width;
1190 curwin->w_wrow -= extra;
1191 }
1192
1193 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1194 if (extra > 0)
1195 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1196 else if (extra < 0)
1197 win_del_lines(curwin, 0, -extra, FALSE, FALSE);
1198 }
1199 else
1200 curwin->w_skipcol = 0;
1201 if (prev_skipcol != curwin->w_skipcol)
1202 redraw_later(NOT_VALID);
1203
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001204#ifdef FEAT_SYN_HL
1205 /* Redraw when w_virtcol changes and 'cursorcolumn' is set, or when w_row
1206 * changes and 'cursorline' is set. */
1207 if ((curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0)
1208 || (curwin->w_p_cul && (curwin->w_valid & VALID_WROW) == 0))
1209 redraw_later(SOME_VALID);
1210#endif
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1213}
1214
1215/*
1216 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1217 */
1218/*ARGSUSED*/
1219 void
1220scrolldown(line_count, byfold)
1221 long line_count;
1222 int byfold; /* TRUE: count a closed fold as one line */
1223{
1224 long done = 0; /* total # of physical lines done */
1225 int wrow;
1226 int moved = FALSE;
1227
1228#ifdef FEAT_FOLDING
1229 linenr_T first;
1230
1231 /* Make sure w_topline is at the first of a sequence of folded lines. */
1232 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1233#endif
1234 validate_cursor(); /* w_wrow needs to be valid */
1235 while (line_count-- > 0)
1236 {
1237#ifdef FEAT_DIFF
1238 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
1239 {
1240 ++curwin->w_topfill;
1241 ++done;
1242 }
1243 else
1244#endif
1245 {
1246 if (curwin->w_topline == 1)
1247 break;
1248 --curwin->w_topline;
1249#ifdef FEAT_DIFF
1250 curwin->w_topfill = 0;
1251#endif
1252#ifdef FEAT_FOLDING
1253 /* A sequence of folded lines only counts for one logical line */
1254 if (hasFolding(curwin->w_topline, &first, NULL))
1255 {
1256 ++done;
1257 if (!byfold)
1258 line_count -= curwin->w_topline - first - 1;
1259 curwin->w_botline -= curwin->w_topline - first;
1260 curwin->w_topline = first;
1261 }
1262 else
1263#endif
1264#ifdef FEAT_DIFF
1265 done += plines_nofill(curwin->w_topline);
1266#else
1267 done += plines(curwin->w_topline);
1268#endif
1269 }
1270 --curwin->w_botline; /* approximate w_botline */
1271 invalidate_botline();
1272 }
1273 curwin->w_wrow += done; /* keep w_wrow updated */
1274 curwin->w_cline_row += done; /* keep w_cline_row updated */
1275
1276#ifdef FEAT_DIFF
1277 if (curwin->w_cursor.lnum == curwin->w_topline)
1278 curwin->w_cline_row = 0;
1279 check_topfill(curwin, TRUE);
1280#endif
1281
1282 /*
1283 * Compute the row number of the last row of the cursor line
1284 * and move the cursor onto the displayed part of the window.
1285 */
1286 wrow = curwin->w_wrow;
1287 if (curwin->w_p_wrap
1288#ifdef FEAT_VERTSPLIT
1289 && curwin->w_width != 0
1290#endif
1291 )
1292 {
1293 validate_virtcol();
1294 validate_cheight();
1295 wrow += curwin->w_cline_height - 1 -
1296 curwin->w_virtcol / W_WIDTH(curwin);
1297 }
1298 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1299 {
1300#ifdef FEAT_FOLDING
1301 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1302 {
1303 --wrow;
1304 if (first == 1)
1305 curwin->w_cursor.lnum = 1;
1306 else
1307 curwin->w_cursor.lnum = first - 1;
1308 }
1309 else
1310#endif
1311 wrow -= plines(curwin->w_cursor.lnum--);
1312 curwin->w_valid &=
1313 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1314 moved = TRUE;
1315 }
1316 if (moved)
1317 {
1318#ifdef FEAT_FOLDING
1319 /* Move cursor to first line of closed fold. */
1320 foldAdjustCursor();
1321#endif
1322 coladvance(curwin->w_curswant);
1323 }
1324}
1325
1326/*
1327 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1328 */
1329/*ARGSUSED*/
1330 void
1331scrollup(line_count, byfold)
1332 long line_count;
1333 int byfold; /* TRUE: count a closed fold as one line */
1334{
1335#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1336 linenr_T lnum;
1337
1338 if (
1339# ifdef FEAT_FOLDING
1340 (byfold && hasAnyFolding(curwin))
1341# ifdef FEAT_DIFF
1342 ||
1343# endif
1344# endif
1345# ifdef FEAT_DIFF
1346 curwin->w_p_diff
1347# endif
1348 )
1349 {
1350 /* count each sequence of folded lines as one logical line */
1351 lnum = curwin->w_topline;
1352 while (line_count--)
1353 {
1354# ifdef FEAT_DIFF
1355 if (curwin->w_topfill > 0)
1356 --curwin->w_topfill;
1357 else
1358# endif
1359 {
1360# ifdef FEAT_FOLDING
1361 if (byfold)
1362 (void)hasFolding(lnum, NULL, &lnum);
1363# endif
1364 if (lnum >= curbuf->b_ml.ml_line_count)
1365 break;
1366 ++lnum;
1367# ifdef FEAT_DIFF
1368 curwin->w_topfill = diff_check_fill(curwin, lnum);
1369# endif
1370 }
1371 }
1372 /* approximate w_botline */
1373 curwin->w_botline += lnum - curwin->w_topline;
1374 curwin->w_topline = lnum;
1375 }
1376 else
1377#endif
1378 {
1379 curwin->w_topline += line_count;
1380 curwin->w_botline += line_count; /* approximate w_botline */
1381 }
1382
1383 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1384 curwin->w_topline = curbuf->b_ml.ml_line_count;
1385 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1386 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1387
1388#ifdef FEAT_DIFF
1389 check_topfill(curwin, FALSE);
1390#endif
1391
1392#ifdef FEAT_FOLDING
1393 if (hasAnyFolding(curwin))
1394 /* Make sure w_topline is at the first of a sequence of folded lines. */
1395 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1396#endif
1397
1398 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1399 if (curwin->w_cursor.lnum < curwin->w_topline)
1400 {
1401 curwin->w_cursor.lnum = curwin->w_topline;
1402 curwin->w_valid &=
1403 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1404 coladvance(curwin->w_curswant);
1405 }
1406}
1407
1408#ifdef FEAT_DIFF
1409/*
1410 * Don't end up with too many filler lines in the window.
1411 */
1412 void
1413check_topfill(wp, down)
1414 win_T *wp;
1415 int down; /* when TRUE scroll down when not enough space */
1416{
1417 int n;
1418
1419 if (wp->w_topfill > 0)
1420 {
1421 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1422 if (wp->w_topfill + n > wp->w_height)
1423 {
1424 if (down && wp->w_topline > 1)
1425 {
1426 --wp->w_topline;
1427 wp->w_topfill = 0;
1428 }
1429 else
1430 {
1431 wp->w_topfill = wp->w_height - n;
1432 if (wp->w_topfill < 0)
1433 wp->w_topfill = 0;
1434 }
1435 }
1436 }
1437}
1438
1439/*
1440 * Use as many filler lines as possible for w_topline. Make sure w_topline
1441 * is still visible.
1442 */
1443 static void
1444max_topfill()
1445{
1446 int n;
1447
1448 n = plines_nofill(curwin->w_topline);
1449 if (n >= curwin->w_height)
1450 curwin->w_topfill = 0;
1451 else
1452 {
1453 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1454 if (curwin->w_topfill + n > curwin->w_height)
1455 curwin->w_topfill = curwin->w_height - n;
1456 }
1457}
1458#endif
1459
1460#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1461/*
1462 * Scroll the screen one line down, but don't do it if it would move the
1463 * cursor off the screen.
1464 */
1465 void
1466scrolldown_clamp()
1467{
1468 int end_row;
1469#ifdef FEAT_DIFF
1470 int can_fill = (curwin->w_topfill
1471 < diff_check_fill(curwin, curwin->w_topline));
1472#endif
1473
1474 if (curwin->w_topline <= 1
1475#ifdef FEAT_DIFF
1476 && !can_fill
1477#endif
1478 )
1479 return;
1480
1481 validate_cursor(); /* w_wrow needs to be valid */
1482
1483 /*
1484 * Compute the row number of the last row of the cursor line
1485 * and make sure it doesn't go off the screen. Make sure the cursor
1486 * doesn't go past 'scrolloff' lines from the screen end.
1487 */
1488 end_row = curwin->w_wrow;
1489#ifdef FEAT_DIFF
1490 if (can_fill)
1491 ++end_row;
1492 else
1493 end_row += plines_nofill(curwin->w_topline - 1);
1494#else
1495 end_row += plines(curwin->w_topline - 1);
1496#endif
1497 if (curwin->w_p_wrap
1498#ifdef FEAT_VERTSPLIT
1499 && curwin->w_width != 0
1500#endif
1501 )
1502 {
1503 validate_cheight();
1504 validate_virtcol();
1505 end_row += curwin->w_cline_height - 1 -
1506 curwin->w_virtcol / W_WIDTH(curwin);
1507 }
1508 if (end_row < curwin->w_height - p_so)
1509 {
1510#ifdef FEAT_DIFF
1511 if (can_fill)
1512 {
1513 ++curwin->w_topfill;
1514 check_topfill(curwin, TRUE);
1515 }
1516 else
1517 {
1518 --curwin->w_topline;
1519 curwin->w_topfill = 0;
1520 }
1521#else
1522 --curwin->w_topline;
1523#endif
1524#ifdef FEAT_FOLDING
1525 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1526#endif
1527 --curwin->w_botline; /* approximate w_botline */
1528 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1529 }
1530}
1531
1532/*
1533 * Scroll the screen one line up, but don't do it if it would move the cursor
1534 * off the screen.
1535 */
1536 void
1537scrollup_clamp()
1538{
1539 int start_row;
1540
1541 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1542#ifdef FEAT_DIFF
1543 && curwin->w_topfill == 0
1544#endif
1545 )
1546 return;
1547
1548 validate_cursor(); /* w_wrow needs to be valid */
1549
1550 /*
1551 * Compute the row number of the first row of the cursor line
1552 * and make sure it doesn't go off the screen. Make sure the cursor
1553 * doesn't go before 'scrolloff' lines from the screen start.
1554 */
1555#ifdef FEAT_DIFF
1556 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1557 - curwin->w_topfill;
1558#else
1559 start_row = curwin->w_wrow - plines(curwin->w_topline);
1560#endif
1561 if (curwin->w_p_wrap
1562#ifdef FEAT_VERTSPLIT
1563 && curwin->w_width != 0
1564#endif
1565 )
1566 {
1567 validate_virtcol();
1568 start_row -= curwin->w_virtcol / W_WIDTH(curwin);
1569 }
1570 if (start_row >= p_so)
1571 {
1572#ifdef FEAT_DIFF
1573 if (curwin->w_topfill > 0)
1574 --curwin->w_topfill;
1575 else
1576#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001577 {
1578#ifdef FEAT_FOLDING
1579 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 ++curwin->w_botline; /* approximate w_botline */
1584 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1585 }
1586}
1587#endif /* FEAT_INS_EXPAND */
1588
1589/*
1590 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1591 * a (wrapped) text line. Uses and sets "lp->fill".
1592 * Returns the height of the added line in "lp->height".
1593 * Lines above the first one are incredibly high.
1594 */
1595 static void
1596topline_back(lp)
1597 lineoff_T *lp;
1598{
1599#ifdef FEAT_DIFF
1600 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1601 {
1602 /* Add a filler line. */
1603 ++lp->fill;
1604 lp->height = 1;
1605 }
1606 else
1607#endif
1608 {
1609 --lp->lnum;
1610#ifdef FEAT_DIFF
1611 lp->fill = 0;
1612#endif
1613 if (lp->lnum < 1)
1614 lp->height = MAXCOL;
1615 else
1616#ifdef FEAT_FOLDING
1617 if (hasFolding(lp->lnum, &lp->lnum, NULL))
1618 /* Add a closed fold */
1619 lp->height = 1;
1620 else
1621#endif
1622 {
1623#ifdef FEAT_DIFF
1624 lp->height = plines_nofill(lp->lnum);
1625#else
1626 lp->height = plines(lp->lnum);
1627#endif
1628 }
1629 }
1630}
1631
1632/*
1633 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1634 * a (wrapped) text line. Uses and sets "lp->fill".
1635 * Returns the height of the added line in "lp->height".
1636 * Lines below the last one are incredibly high.
1637 */
1638 static void
1639botline_forw(lp)
1640 lineoff_T *lp;
1641{
1642#ifdef FEAT_DIFF
1643 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1644 {
1645 /* Add a filler line. */
1646 ++lp->fill;
1647 lp->height = 1;
1648 }
1649 else
1650#endif
1651 {
1652 ++lp->lnum;
1653#ifdef FEAT_DIFF
1654 lp->fill = 0;
1655#endif
1656 if (lp->lnum > curbuf->b_ml.ml_line_count)
1657 lp->height = MAXCOL;
1658 else
1659#ifdef FEAT_FOLDING
1660 if (hasFolding(lp->lnum, NULL, &lp->lnum))
1661 /* Add a closed fold */
1662 lp->height = 1;
1663 else
1664#endif
1665 {
1666#ifdef FEAT_DIFF
1667 lp->height = plines_nofill(lp->lnum);
1668#else
1669 lp->height = plines(lp->lnum);
1670#endif
1671 }
1672 }
1673}
1674
1675#ifdef FEAT_DIFF
1676/*
1677 * Switch from including filler lines below lp->lnum to including filler
1678 * lines above loff.lnum + 1. This keeps pointing to the same line.
1679 * When there are no filler lines nothing changes.
1680 */
1681 static void
1682botline_topline(lp)
1683 lineoff_T *lp;
1684{
1685 if (lp->fill > 0)
1686 {
1687 ++lp->lnum;
1688 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1689 }
1690}
1691
1692/*
1693 * Switch from including filler lines above lp->lnum to including filler
1694 * lines below loff.lnum - 1. This keeps pointing to the same line.
1695 * When there are no filler lines nothing changes.
1696 */
1697 static void
1698topline_botline(lp)
1699 lineoff_T *lp;
1700{
1701 if (lp->fill > 0)
1702 {
1703 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1704 --lp->lnum;
1705 }
1706}
1707#endif
1708
1709/*
1710 * Recompute topline to put the cursor at the top of the window.
1711 * Scroll at least "min_scroll" lines.
1712 * If "always" is TRUE, always set topline (for "zt").
1713 */
1714 void
1715scroll_cursor_top(min_scroll, always)
1716 int min_scroll;
1717 int always;
1718{
1719 int scrolled = 0;
1720 int extra = 0;
1721 int used;
1722 int i;
1723 linenr_T top; /* just above displayed lines */
1724 linenr_T bot; /* just below displayed lines */
1725 linenr_T old_topline = curwin->w_topline;
1726#ifdef FEAT_DIFF
1727 linenr_T old_topfill = curwin->w_topfill;
1728#endif
1729 linenr_T new_topline;
1730 int off = p_so;
1731
1732#ifdef FEAT_MOUSE
1733 if (mouse_dragging > 0)
1734 off = mouse_dragging - 1;
1735#endif
1736
1737 /*
1738 * Decrease topline until:
1739 * - it has become 1
1740 * - (part of) the cursor line is moved off the screen or
1741 * - moved at least 'scrolljump' lines and
1742 * - at least 'scrolloff' lines above and below the cursor
1743 */
1744 validate_cheight();
1745 used = curwin->w_cline_height;
1746 if (curwin->w_cursor.lnum < curwin->w_topline)
1747 scrolled = used;
1748
1749#ifdef FEAT_FOLDING
1750 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1751 {
1752 --top;
1753 ++bot;
1754 }
1755 else
1756#endif
1757 {
1758 top = curwin->w_cursor.lnum - 1;
1759 bot = curwin->w_cursor.lnum + 1;
1760 }
1761 new_topline = top + 1;
1762
1763#ifdef FEAT_DIFF
1764 /* count filler lines of the cursor window as context */
1765 i = diff_check_fill(curwin, curwin->w_cursor.lnum);
1766 used += i;
1767 extra += i;
1768#endif
1769
1770 /*
1771 * Check if the lines from "top" to "bot" fit in the window. If they do,
1772 * set new_topline and advance "top" and "bot" to include more lines.
1773 */
1774 while (top > 0)
1775 {
1776#ifdef FEAT_FOLDING
1777 if (hasFolding(top, &top, NULL))
1778 /* count one logical line for a sequence of folded lines */
1779 i = 1;
1780 else
1781#endif
1782 i = plines(top);
1783 used += i;
1784 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1785 {
1786#ifdef FEAT_FOLDING
1787 if (hasFolding(bot, NULL, &bot))
1788 /* count one logical line for a sequence of folded lines */
1789 ++used;
1790 else
1791#endif
1792 used += plines(bot);
1793 }
1794 if (used > curwin->w_height)
1795 break;
1796 if (top < curwin->w_topline)
1797 scrolled += i;
1798
1799 /*
1800 * If scrolling is needed, scroll at least 'sj' lines.
1801 */
1802 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1803 && extra >= off)
1804 break;
1805
1806 extra += i;
1807 new_topline = top;
1808 --top;
1809 ++bot;
1810 }
1811
1812 /*
1813 * If we don't have enough space, put cursor in the middle.
1814 * This makes sure we get the same position when using "k" and "j"
1815 * in a small window.
1816 */
1817 if (used > curwin->w_height)
1818 scroll_cursor_halfway(FALSE);
1819 else
1820 {
1821 /*
1822 * If "always" is FALSE, only adjust topline to a lower value, higher
1823 * value may happen with wrapping lines
1824 */
1825 if (new_topline < curwin->w_topline || always)
1826 curwin->w_topline = new_topline;
1827 if (curwin->w_topline > curwin->w_cursor.lnum)
1828 curwin->w_topline = curwin->w_cursor.lnum;
1829#ifdef FEAT_DIFF
1830 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1831 if (curwin->w_topfill > 0 && extra > off)
1832 {
1833 curwin->w_topfill -= extra - off;
1834 if (curwin->w_topfill < 0)
1835 curwin->w_topfill = 0;
1836 }
1837 check_topfill(curwin, FALSE);
1838#endif
1839 if (curwin->w_topline != old_topline
1840#ifdef FEAT_DIFF
1841 || curwin->w_topfill != old_topfill
1842#endif
1843 )
1844 curwin->w_valid &=
1845 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1846 curwin->w_valid |= VALID_TOPLINE;
1847 }
1848}
1849
1850/*
1851 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1852 * screen lines for text lines.
1853 */
1854 void
1855set_empty_rows(wp, used)
1856 win_T *wp;
1857 int used;
1858{
1859#ifdef FEAT_DIFF
1860 wp->w_filler_rows = 0;
1861#endif
1862 if (used == 0)
1863 wp->w_empty_rows = 0; /* single line that doesn't fit */
1864 else
1865 {
1866 wp->w_empty_rows = wp->w_height - used;
1867#ifdef FEAT_DIFF
1868 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1869 {
1870 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1871 if (wp->w_empty_rows > wp->w_filler_rows)
1872 wp->w_empty_rows -= wp->w_filler_rows;
1873 else
1874 {
1875 wp->w_filler_rows = wp->w_empty_rows;
1876 wp->w_empty_rows = 0;
1877 }
1878 }
1879#endif
1880 }
1881}
1882
1883/*
1884 * Recompute topline to put the cursor at the bottom of the window.
1885 * Scroll at least "min_scroll" lines.
1886 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1887 * This is messy stuff!!!
1888 */
1889 void
1890scroll_cursor_bot(min_scroll, set_topbot)
1891 int min_scroll;
1892 int set_topbot;
1893{
1894 int used;
1895 int scrolled = 0;
1896 int extra = 0;
1897 int i;
1898 linenr_T line_count;
1899 linenr_T old_topline = curwin->w_topline;
1900 lineoff_T loff;
1901 lineoff_T boff;
1902#ifdef FEAT_DIFF
1903 int old_topfill = curwin->w_topfill;
1904 int fill_below_window;
1905#endif
1906 linenr_T old_botline = curwin->w_botline;
1907 linenr_T old_valid = curwin->w_valid;
1908 int old_empty_rows = curwin->w_empty_rows;
1909 linenr_T cln; /* Cursor Line Number */
1910
1911 cln = curwin->w_cursor.lnum;
1912 if (set_topbot)
1913 {
1914 used = 0;
1915 curwin->w_botline = cln + 1;
1916#ifdef FEAT_DIFF
1917 loff.fill = 0;
1918#endif
1919 for (curwin->w_topline = curwin->w_botline;
1920 curwin->w_topline > 1;
1921 curwin->w_topline = loff.lnum)
1922 {
1923 loff.lnum = curwin->w_topline;
1924 topline_back(&loff);
1925 if (used + loff.height > curwin->w_height)
1926 break;
1927 used += loff.height;
1928#ifdef FEAT_DIFF
1929 curwin->w_topfill = loff.fill;
1930#endif
1931 }
1932 set_empty_rows(curwin, used);
1933 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1934 if (curwin->w_topline != old_topline
1935#ifdef FEAT_DIFF
1936 || curwin->w_topfill != old_topfill
1937#endif
1938 )
1939 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1940 }
1941 else
1942 validate_botline();
1943
1944 /* The lines of the cursor line itself are always used. */
1945#ifdef FEAT_DIFF
1946 used = plines_nofill(cln);
1947#else
1948 validate_cheight();
1949 used = curwin->w_cline_height;
1950#endif
1951
1952 /* If the cursor is below botline, we will at least scroll by the height
1953 * of the cursor line. Correct for empty lines, which are really part of
1954 * botline. */
1955 if (cln >= curwin->w_botline)
1956 {
1957 scrolled = used;
1958 if (cln == curwin->w_botline)
1959 scrolled -= curwin->w_empty_rows;
1960 }
1961
1962 /*
1963 * Stop counting lines to scroll when
1964 * - hitting start of the file
1965 * - scrolled nothing or at least 'sj' lines
1966 * - at least 'so' lines below the cursor
1967 * - lines between botline and cursor have been counted
1968 */
1969#ifdef FEAT_FOLDING
1970 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1971#endif
1972 {
1973 loff.lnum = cln;
1974 boff.lnum = cln;
1975 }
1976#ifdef FEAT_DIFF
1977 loff.fill = 0;
1978 boff.fill = 0;
1979 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
1980 - curwin->w_filler_rows;
1981#endif
1982
1983 while (loff.lnum > 1)
1984 {
1985 /* Stop when scrolled nothing or at least "min_scroll", found "extra"
1986 * context for 'scrolloff' and counted all lines below the window. */
1987 if ((((scrolled <= 0 || scrolled >= min_scroll)
1988 && extra >= (
1989#ifdef FEAT_MOUSE
1990 mouse_dragging ? mouse_dragging - 1 :
1991#endif
1992 p_so))
1993 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
1994 && loff.lnum <= curwin->w_botline
1995#ifdef FEAT_DIFF
1996 && (loff.lnum < curwin->w_botline
1997 || loff.fill >= fill_below_window)
1998#endif
1999 )
2000 break;
2001
2002 /* Add one line above */
2003 topline_back(&loff);
2004 used += loff.height;
2005 if (used > curwin->w_height)
2006 break;
2007 if (loff.lnum >= curwin->w_botline
2008#ifdef FEAT_DIFF
2009 && (loff.lnum > curwin->w_botline
2010 || loff.fill <= fill_below_window)
2011#endif
2012 )
2013 {
2014 /* Count screen lines that are below the window. */
2015 scrolled += loff.height;
2016 if (loff.lnum == curwin->w_botline
2017#ifdef FEAT_DIFF
2018 && boff.fill == 0
2019#endif
2020 )
2021 scrolled -= curwin->w_empty_rows;
2022 }
2023
2024 if (boff.lnum < curbuf->b_ml.ml_line_count)
2025 {
2026 /* Add one line below */
2027 botline_forw(&boff);
2028 used += boff.height;
2029 if (used > curwin->w_height)
2030 break;
2031 if (extra < (
2032#ifdef FEAT_MOUSE
2033 mouse_dragging > 0 ? mouse_dragging - 1 :
2034#endif
2035 p_so) || scrolled < min_scroll)
2036 {
2037 extra += boff.height;
2038 if (boff.lnum >= curwin->w_botline
2039#ifdef FEAT_DIFF
2040 || (boff.lnum + 1 == curwin->w_botline
2041 && boff.fill > curwin->w_filler_rows)
2042#endif
2043 )
2044 {
2045 /* Count screen lines that are below the window. */
2046 scrolled += boff.height;
2047 if (boff.lnum == curwin->w_botline
2048#ifdef FEAT_DIFF
2049 && boff.fill == 0
2050#endif
2051 )
2052 scrolled -= curwin->w_empty_rows;
2053 }
2054 }
2055 }
2056 }
2057
2058 /* curwin->w_empty_rows is larger, no need to scroll */
2059 if (scrolled <= 0)
2060 line_count = 0;
2061 /* more than a screenfull, don't scroll but redraw */
2062 else if (used > curwin->w_height)
2063 line_count = used;
2064 /* scroll minimal number of lines */
2065 else
2066 {
2067 line_count = 0;
2068#ifdef FEAT_DIFF
2069 boff.fill = curwin->w_topfill;
2070#endif
2071 boff.lnum = curwin->w_topline - 1;
2072 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2073 {
2074 botline_forw(&boff);
2075 i += boff.height;
2076 ++line_count;
2077 }
2078 if (i < scrolled) /* below curwin->w_botline, don't scroll */
2079 line_count = 9999;
2080 }
2081
2082 /*
2083 * Scroll up if the cursor is off the bottom of the screen a bit.
2084 * Otherwise put it at 1/2 of the screen.
2085 */
2086 if (line_count >= curwin->w_height && line_count > min_scroll)
2087 scroll_cursor_halfway(FALSE);
2088 else
2089 scrollup(line_count, TRUE);
2090
2091 /*
2092 * If topline didn't change we need to restore w_botline and w_empty_rows
2093 * (we changed them).
2094 * If topline did change, update_screen() will set botline.
2095 */
2096 if (curwin->w_topline == old_topline && set_topbot)
2097 {
2098 curwin->w_botline = old_botline;
2099 curwin->w_empty_rows = old_empty_rows;
2100 curwin->w_valid = old_valid;
2101 }
2102 curwin->w_valid |= VALID_TOPLINE;
2103}
2104
2105/*
2106 * Recompute topline to put the cursor halfway the window
2107 * If "atend" is TRUE, also put it halfway at the end of the file.
2108 */
2109 void
2110scroll_cursor_halfway(atend)
2111 int atend;
2112{
2113 int above = 0;
2114 linenr_T topline;
2115#ifdef FEAT_DIFF
2116 int topfill = 0;
2117#endif
2118 int below = 0;
2119 int used;
2120 lineoff_T loff;
2121 lineoff_T boff;
2122
2123 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2124#ifdef FEAT_FOLDING
2125 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2126#endif
2127#ifdef FEAT_DIFF
2128 used = plines_nofill(loff.lnum);
2129 loff.fill = 0;
2130 boff.fill = 0;
2131#else
2132 used = plines(loff.lnum);
2133#endif
2134 topline = loff.lnum;
2135 while (topline > 1)
2136 {
2137 if (below <= above) /* add a line below the cursor first */
2138 {
2139 if (boff.lnum < curbuf->b_ml.ml_line_count)
2140 {
2141 botline_forw(&boff);
2142 used += boff.height;
2143 if (used > curwin->w_height)
2144 break;
2145 below += boff.height;
2146 }
2147 else
2148 {
2149 ++below; /* count a "~" line */
2150 if (atend)
2151 ++used;
2152 }
2153 }
2154
2155 if (below > above) /* add a line above the cursor */
2156 {
2157 topline_back(&loff);
2158 used += loff.height;
2159 if (used > curwin->w_height)
2160 break;
2161 above += loff.height;
2162 topline = loff.lnum;
2163#ifdef FEAT_DIFF
2164 topfill = loff.fill;
2165#endif
2166 }
2167 }
2168#ifdef FEAT_FOLDING
2169 if (!hasFolding(topline, &curwin->w_topline, NULL))
2170#endif
2171 curwin->w_topline = topline;
2172#ifdef FEAT_DIFF
2173 curwin->w_topfill = topfill;
2174 check_topfill(curwin, FALSE);
2175#endif
2176 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2177 curwin->w_valid |= VALID_TOPLINE;
2178}
2179
2180/*
2181 * Correct the cursor position so that it is in a part of the screen at least
2182 * 'so' lines from the top and bottom, if possible.
2183 * If not possible, put it at the same position as scroll_cursor_halfway().
2184 * When called topline must be valid!
2185 */
2186 void
2187cursor_correct()
2188{
2189 int above = 0; /* screen lines above topline */
2190 linenr_T topline;
2191 int below = 0; /* screen lines below botline */
2192 linenr_T botline;
2193 int above_wanted, below_wanted;
2194 linenr_T cln; /* Cursor Line Number */
2195 int max_off;
2196
2197 /*
2198 * How many lines we would like to have above/below the cursor depends on
2199 * whether the first/last line of the file is on screen.
2200 */
2201 above_wanted = p_so;
2202 below_wanted = p_so;
2203#ifdef FEAT_MOUSE
2204 if (mouse_dragging)
2205 {
2206 above_wanted = mouse_dragging - 1;
2207 below_wanted = mouse_dragging - 1;
2208 }
2209#endif
2210 if (curwin->w_topline == 1)
2211 {
2212 above_wanted = 0;
2213 max_off = curwin->w_height / 2;
2214 if (below_wanted > max_off)
2215 below_wanted = max_off;
2216 }
2217 validate_botline();
2218 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
2219#ifdef FEAT_MOUSE
2220 && !mouse_dragging
2221#endif
2222 )
2223 {
2224 below_wanted = 0;
2225 max_off = (curwin->w_height - 1) / 2;
2226 if (above_wanted > max_off)
2227 above_wanted = max_off;
2228 }
2229
2230 /*
2231 * If there are sufficient file-lines above and below the cursor, we can
2232 * return now.
2233 */
2234 cln = curwin->w_cursor.lnum;
2235 if (cln >= curwin->w_topline + above_wanted
2236 && cln < curwin->w_botline - below_wanted
2237#ifdef FEAT_FOLDING
2238 && !hasAnyFolding(curwin)
2239#endif
2240 )
2241 return;
2242
2243 /*
2244 * Narrow down the area where the cursor can be put by taking lines from
2245 * the top and the bottom until:
2246 * - the desired context lines are found
2247 * - the lines from the top is past the lines from the bottom
2248 */
2249 topline = curwin->w_topline;
2250 botline = curwin->w_botline - 1;
2251#ifdef FEAT_DIFF
2252 /* count filler lines as context */
2253 above = curwin->w_topfill;
2254 below = curwin->w_filler_rows;
2255#endif
2256 while ((above < above_wanted || below < below_wanted) && topline < botline)
2257 {
2258 if (below < below_wanted && (below <= above || above >= above_wanted))
2259 {
2260#ifdef FEAT_FOLDING
2261 if (hasFolding(botline, &botline, NULL))
2262 ++below;
2263 else
2264#endif
2265 below += plines(botline);
2266 --botline;
2267 }
2268 if (above < above_wanted && (above < below || below >= below_wanted))
2269 {
2270#ifdef FEAT_FOLDING
2271 if (hasFolding(topline, NULL, &topline))
2272 ++above;
2273 else
2274#endif
2275#ifndef FEAT_DIFF
2276 above += plines(topline);
2277#else
2278 above += plines_nofill(topline);
2279
2280 /* Count filler lines below this line as context. */
2281 if (topline < botline)
2282 above += diff_check_fill(curwin, topline + 1);
2283#endif
2284 ++topline;
2285 }
2286 }
2287 if (topline == botline || botline == 0)
2288 curwin->w_cursor.lnum = topline;
2289 else if (topline > botline)
2290 curwin->w_cursor.lnum = botline;
2291 else
2292 {
2293 if (cln < topline && curwin->w_topline > 1)
2294 {
2295 curwin->w_cursor.lnum = topline;
2296 curwin->w_valid &=
2297 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2298 }
2299 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2300 {
2301 curwin->w_cursor.lnum = botline;
2302 curwin->w_valid &=
2303 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2304 }
2305 }
2306 curwin->w_valid |= VALID_TOPLINE;
2307}
2308
2309static void get_scroll_overlap __ARGS((lineoff_T *lp, int dir));
2310
2311/*
2312 * move screen 'count' pages up or down and update screen
2313 *
2314 * return FAIL for failure, OK otherwise
2315 */
2316 int
2317onepage(dir, count)
2318 int dir;
2319 long count;
2320{
2321 long n;
2322 int retval = OK;
2323 lineoff_T loff;
2324 linenr_T old_topline = curwin->w_topline;
2325
2326 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */
2327 {
2328 beep_flush();
2329 return FAIL;
2330 }
2331
2332 for ( ; count > 0; --count)
2333 {
2334 validate_botline();
2335 /*
2336 * It's an error to move a page up when the first line is already on
2337 * the screen. It's an error to move a page down when the last line
2338 * is on the screen and the topline is 'scrolloff' lines from the
2339 * last line.
2340 */
2341 if (dir == FORWARD
2342 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so)
2343 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2344 : (curwin->w_topline == 1
2345#ifdef FEAT_DIFF
2346 && curwin->w_topfill ==
2347 diff_check_fill(curwin, curwin->w_topline)
2348#endif
2349 ))
2350 {
2351 beep_flush();
2352 retval = FAIL;
2353 break;
2354 }
2355
2356#ifdef FEAT_DIFF
2357 loff.fill = 0;
2358#endif
2359 if (dir == FORWARD)
2360 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002361 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002363 /* Vi compatible scrolling */
2364 if (p_window <= 2)
2365 ++curwin->w_topline;
2366 else
2367 curwin->w_topline += p_window - 2;
2368 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2369 curwin->w_topline = curbuf->b_ml.ml_line_count;
2370 curwin->w_cursor.lnum = curwin->w_topline;
2371 }
2372 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2373 {
2374 /* at end of file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 curwin->w_topline = curbuf->b_ml.ml_line_count;
2376#ifdef FEAT_DIFF
2377 curwin->w_topfill = 0;
2378#endif
2379 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2380 }
2381 else
2382 {
2383 /* For the overlap, start with the line just below the window
2384 * and go upwards. */
2385 loff.lnum = curwin->w_botline;
2386#ifdef FEAT_DIFF
2387 loff.fill = diff_check_fill(curwin, loff.lnum)
2388 - curwin->w_filler_rows;
2389#endif
2390 get_scroll_overlap(&loff, -1);
2391 curwin->w_topline = loff.lnum;
2392#ifdef FEAT_DIFF
2393 curwin->w_topfill = loff.fill;
2394 check_topfill(curwin, FALSE);
2395#endif
2396 curwin->w_cursor.lnum = curwin->w_topline;
2397 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2398 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2399 }
2400 }
2401 else /* dir == BACKWARDS */
2402 {
2403#ifdef FEAT_DIFF
2404 if (curwin->w_topline == 1)
2405 {
2406 /* Include max number of filler lines */
2407 max_topfill();
2408 continue;
2409 }
2410#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002411 if (firstwin == lastwin && p_window > 0 && p_window < Rows - 1)
2412 {
2413 /* Vi compatible scrolling (sort of) */
2414 if (p_window <= 2)
2415 --curwin->w_topline;
2416 else
2417 curwin->w_topline -= p_window - 2;
2418 if (curwin->w_topline < 1)
2419 curwin->w_topline = 1;
2420 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2421 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2422 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2423 continue;
2424 }
2425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /* Find the line at the top of the window that is going to be the
2427 * line at the bottom of the window. Make sure this results in
2428 * the same line as before doing CTRL-F. */
2429 loff.lnum = curwin->w_topline - 1;
2430#ifdef FEAT_DIFF
2431 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2432 - curwin->w_topfill;
2433#endif
2434 get_scroll_overlap(&loff, 1);
2435
2436 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2437 {
2438 loff.lnum = curbuf->b_ml.ml_line_count;
2439#ifdef FEAT_DIFF
2440 loff.fill = 0;
2441 }
2442 else
2443 {
2444 botline_topline(&loff);
2445#endif
2446 }
2447 curwin->w_cursor.lnum = loff.lnum;
2448
2449 /* Find the line just above the new topline to get the right line
2450 * at the bottom of the window. */
2451 n = 0;
2452 while (n <= curwin->w_height && loff.lnum >= 1)
2453 {
2454 topline_back(&loff);
2455 n += loff.height;
2456 }
2457 if (n <= curwin->w_height) /* at begin of file */
2458 {
2459 curwin->w_topline = 1;
2460#ifdef FEAT_DIFF
2461 max_topfill();
2462#endif
2463 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2464 }
2465 else
2466 {
2467 /* Go two lines forward again. */
2468#ifdef FEAT_DIFF
2469 topline_botline(&loff);
2470#endif
2471 botline_forw(&loff);
2472 botline_forw(&loff);
2473#ifdef FEAT_DIFF
2474 botline_topline(&loff);
2475#endif
2476#ifdef FEAT_FOLDING
2477 /* We're at the wrong end of a fold now. */
2478 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2479#endif
2480
2481 /* Always scroll at least one line. Avoid getting stuck on
2482 * very long lines. */
2483 if (loff.lnum >= curwin->w_topline
2484#ifdef FEAT_DIFF
2485 && (loff.lnum > curwin->w_topline
2486 || loff.fill >= curwin->w_topfill)
2487#endif
2488 )
2489 {
2490#ifdef FEAT_DIFF
2491 /* First try using the maximum number of filler lines. If
2492 * that's not enough, backup one line. */
2493 loff.fill = curwin->w_topfill;
2494 if (curwin->w_topfill < diff_check_fill(curwin,
2495 curwin->w_topline))
2496 max_topfill();
2497 if (curwin->w_topfill == loff.fill)
2498#endif
2499 {
2500 --curwin->w_topline;
2501#ifdef FEAT_DIFF
2502 curwin->w_topfill = 0;
2503#endif
2504 }
2505 comp_botline(curwin);
2506 curwin->w_cursor.lnum = curwin->w_botline - 1;
2507 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|
2508 VALID_WROW|VALID_CROW);
2509 }
2510 else
2511 {
2512 curwin->w_topline = loff.lnum;
2513#ifdef FEAT_DIFF
2514 curwin->w_topfill = loff.fill;
2515 check_topfill(curwin, FALSE);
2516#endif
2517 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2518 }
2519 }
2520 }
2521 }
2522#ifdef FEAT_FOLDING
2523 foldAdjustCursor();
2524#endif
2525 cursor_correct();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002526 if (retval == OK)
2527 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2529
2530 /*
2531 * Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2532 * But make sure we scroll at least one line (happens with mix of long
2533 * wrapping lines and non-wrapping line).
2534 */
2535 if (retval == OK && dir == FORWARD && check_top_offset())
2536 {
2537 scroll_cursor_top(1, FALSE);
2538 if (curwin->w_topline <= old_topline
2539 && old_topline < curbuf->b_ml.ml_line_count)
2540 {
2541 curwin->w_topline = old_topline + 1;
2542#ifdef FEAT_FOLDING
2543 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2544#endif
2545 }
2546 }
2547
2548 redraw_later(VALID);
2549 return retval;
2550}
2551
2552/*
2553 * Decide how much overlap to use for page-up or page-down scrolling.
2554 * This is symmetric, so that doing both keeps the same lines displayed.
2555 * Three lines are examined:
2556 *
2557 * before CTRL-F after CTRL-F / before CTRL-B
2558 * etc. l1
2559 * l1 last but one line ------------
2560 * l2 last text line l2 top text line
2561 * ------------- l3 second text line
2562 * l3 etc.
2563 */
2564 static void
2565get_scroll_overlap(lp, dir)
2566 lineoff_T *lp;
2567 int dir;
2568{
2569 int h1, h2, h3, h4;
2570 int min_height = curwin->w_height - 2;
2571 lineoff_T loff0, loff1, loff2;
2572
2573#ifdef FEAT_DIFF
2574 if (lp->fill > 0)
2575 lp->height = 1;
2576 else
2577 lp->height = plines_nofill(lp->lnum);
2578#else
2579 lp->height = plines(lp->lnum);
2580#endif
2581 h1 = lp->height;
2582 if (h1 > min_height)
2583 return; /* no overlap */
2584
2585 loff0 = *lp;
2586 if (dir > 0)
2587 botline_forw(lp);
2588 else
2589 topline_back(lp);
2590 h2 = lp->height;
2591 if (h2 + h1 > min_height)
2592 {
2593 *lp = loff0; /* no overlap */
2594 return;
2595 }
2596
2597 loff1 = *lp;
2598 if (dir > 0)
2599 botline_forw(lp);
2600 else
2601 topline_back(lp);
2602 h3 = lp->height;
2603 if (h3 + h2 > min_height)
2604 {
2605 *lp = loff0; /* no overlap */
2606 return;
2607 }
2608
2609 loff2 = *lp;
2610 if (dir > 0)
2611 botline_forw(lp);
2612 else
2613 topline_back(lp);
2614 h4 = lp->height;
2615 if (h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
2616 *lp = loff1; /* 1 line overlap */
2617 else
2618 *lp = loff2; /* 2 lines overlap */
2619 return;
2620}
2621
2622/* #define KEEP_SCREEN_LINE */
2623/*
2624 * Scroll 'scroll' lines up or down.
2625 */
2626 void
2627halfpage(flag, Prenum)
2628 int flag;
2629 linenr_T Prenum;
2630{
2631 long scrolled = 0;
2632 int i;
2633 int n;
2634 int room;
2635
2636 if (Prenum)
2637 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2638 curwin->w_height : Prenum;
2639 n = (curwin->w_p_scr <= curwin->w_height) ?
2640 curwin->w_p_scr : curwin->w_height;
2641
2642 validate_botline();
2643 room = curwin->w_empty_rows;
2644#ifdef FEAT_DIFF
2645 room += curwin->w_filler_rows;
2646#endif
2647 if (flag)
2648 {
2649 /*
2650 * scroll the text up
2651 */
2652 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2653 {
2654#ifdef FEAT_DIFF
2655 if (curwin->w_topfill > 0)
2656 {
2657 i = 1;
2658 if (--n < 0 && scrolled > 0)
2659 break;
2660 --curwin->w_topfill;
2661 }
2662 else
2663#endif
2664 {
2665#ifdef FEAT_DIFF
2666 i = plines_nofill(curwin->w_topline);
2667#else
2668 i = plines(curwin->w_topline);
2669#endif
2670 n -= i;
2671 if (n < 0 && scrolled > 0)
2672 break;
2673#ifdef FEAT_FOLDING
2674 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2675#endif
2676 ++curwin->w_topline;
2677#ifdef FEAT_DIFF
2678 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2679#endif
2680
2681#ifndef KEEP_SCREEN_LINE
2682 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2683 {
2684 ++curwin->w_cursor.lnum;
2685 curwin->w_valid &=
2686 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2687 }
2688#endif
2689 }
2690 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2691 scrolled += i;
2692
2693 /*
2694 * Correct w_botline for changed w_topline.
2695 * Won't work when there are filler lines.
2696 */
2697#ifdef FEAT_DIFF
2698 if (curwin->w_p_diff)
2699 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2700 else
2701#endif
2702 {
2703 room += i;
2704 do
2705 {
2706 i = plines(curwin->w_botline);
2707 if (i > room)
2708 break;
2709#ifdef FEAT_FOLDING
2710 (void)hasFolding(curwin->w_botline, NULL,
2711 &curwin->w_botline);
2712#endif
2713 ++curwin->w_botline;
2714 room -= i;
2715 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2716 }
2717 }
2718
2719#ifndef KEEP_SCREEN_LINE
2720 /*
2721 * When hit bottom of the file: move cursor down.
2722 */
2723 if (n > 0)
2724 {
2725# ifdef FEAT_FOLDING
2726 if (hasAnyFolding(curwin))
2727 {
2728 while (--n >= 0
2729 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2730 {
2731 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2732 &curwin->w_cursor.lnum);
2733 ++curwin->w_cursor.lnum;
2734 }
2735 }
2736 else
2737# endif
2738 curwin->w_cursor.lnum += n;
2739 check_cursor_lnum();
2740 }
2741#else
2742 /* try to put the cursor in the same screen line */
2743 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0)
2744 && curwin->w_cursor.lnum < curwin->w_botline - 1)
2745 {
2746 scrolled -= plines(curwin->w_cursor.lnum);
2747 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline)
2748 break;
2749# ifdef FEAT_FOLDING
2750 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2751 &curwin->w_cursor.lnum);
2752# endif
2753 ++curwin->w_cursor.lnum;
2754 }
2755#endif
2756 }
2757 else
2758 {
2759 /*
2760 * scroll the text down
2761 */
2762 while (n > 0 && curwin->w_topline > 1)
2763 {
2764#ifdef FEAT_DIFF
2765 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2766 {
2767 i = 1;
2768 if (--n < 0 && scrolled > 0)
2769 break;
2770 ++curwin->w_topfill;
2771 }
2772 else
2773#endif
2774 {
2775#ifdef FEAT_DIFF
2776 i = plines_nofill(curwin->w_topline - 1);
2777#else
2778 i = plines(curwin->w_topline - 1);
2779#endif
2780 n -= i;
2781 if (n < 0 && scrolled > 0)
2782 break;
2783 --curwin->w_topline;
2784#ifdef FEAT_FOLDING
2785 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2786#endif
2787#ifdef FEAT_DIFF
2788 curwin->w_topfill = 0;
2789#endif
2790 }
2791 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2792 VALID_BOTLINE|VALID_BOTLINE_AP);
2793 scrolled += i;
2794#ifndef KEEP_SCREEN_LINE
2795 if (curwin->w_cursor.lnum > 1)
2796 {
2797 --curwin->w_cursor.lnum;
2798 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2799 }
2800#endif
2801 }
2802#ifndef KEEP_SCREEN_LINE
2803 /*
2804 * When hit top of the file: move cursor up.
2805 */
2806 if (n > 0)
2807 {
2808 if (curwin->w_cursor.lnum <= (linenr_T)n)
2809 curwin->w_cursor.lnum = 1;
2810 else
2811# ifdef FEAT_FOLDING
2812 if (hasAnyFolding(curwin))
2813 {
2814 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2815 {
2816 --curwin->w_cursor.lnum;
2817 (void)hasFolding(curwin->w_cursor.lnum,
2818 &curwin->w_cursor.lnum, NULL);
2819 }
2820 }
2821 else
2822# endif
2823 curwin->w_cursor.lnum -= n;
2824 }
2825#else
2826 /* try to put the cursor in the same screen line */
2827 scrolled += n; /* move cursor when topline is 1 */
2828 while (curwin->w_cursor.lnum > curwin->w_topline
2829 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline))
2830 {
2831 scrolled -= plines(curwin->w_cursor.lnum - 1);
2832 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline)
2833 break;
2834 --curwin->w_cursor.lnum;
2835# ifdef FEAT_FOLDING
2836 foldAdjustCursor();
2837# endif
2838 }
2839#endif
2840 }
2841# ifdef FEAT_FOLDING
2842 /* Move cursor to first line of closed fold. */
2843 foldAdjustCursor();
2844# endif
2845#ifdef FEAT_DIFF
2846 check_topfill(curwin, !flag);
2847#endif
2848 cursor_correct();
2849 beginline(BL_SOL | BL_FIX);
2850 redraw_later(VALID);
2851}