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