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