1 /* 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 22 static void comp_botline __ARGS((win_T *wp)); 23 static int check_top_offset __ARGS((void)); 24 static void curs_rows __ARGS((win_T *wp, int do_botline)); 25 static void validate_botline_win __ARGS((win_T *wp)); 26 static void validate_cheight __ARGS((void)); 27 28 typedef 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 37 static void topline_back __ARGS((lineoff_T *lp)); 38 static void botline_forw __ARGS((lineoff_T *lp)); 39 #ifdef FEAT_DIFF 40 static void botline_topline __ARGS((lineoff_T *lp)); 41 static void topline_botline __ARGS((lineoff_T *lp)); 42 static 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 50 comp_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 130 update_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 141 update_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 384 check_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 422 update_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 436 check_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 468 changed_window_setting() 469 { 470 changed_window_setting_win(curwin); 471 } 472 473 void 474 changed_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 487 set_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 512 changed_cline_bef_curs() 513 { 514 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL 515 |VALID_CHEIGHT|VALID_TOPLINE); 516 } 517 518 void 519 changed_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 533 changed_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 545 changed_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 552 changed_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 563 validate_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 573 validate_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 584 invalidate_botline() 585 { 586 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP); 587 } 588 589 void 590 invalidate_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 602 approximate_botline() 603 { 604 curwin->w_valid &= ~VALID_BOTLINE; 605 } 606 #endif 607 608 void 609 approximate_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 620 botline_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 631 botline_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 641 cursor_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 653 validate_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 665 validate_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 684 curs_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 813 validate_virtcol() 814 { 815 validate_virtcol_win(curwin); 816 } 817 818 /* 819 * Validate wp->w_virtcol only. 820 */ 821 void 822 validate_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 837 validate_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 860 validate_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 892 win_col_off(wp) 893 win_T *wp; 894 { 895 return ((wp->w_p_nu ? number_width(wp) + 1 : 0) 896 #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 914 curwin_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 924 win_col_off2(wp) 925 win_T *wp; 926 { 927 if (wp->w_p_nu && vim_strchr(p_cpo, CPO_NUMCOL) != NULL) 928 return number_width(wp) + 1; 929 return 0; 930 } 931 932 int 933 curwin_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 944 curs_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 1197 scrolldown(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 1308 scrollup(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 1390 check_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 1421 max_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 1443 scrolldown_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 1514 scrollup_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 1554 ++curwin->w_topline; 1555 ++curwin->w_botline; /* approximate w_botline */ 1556 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE); 1557 } 1558 } 1559 #endif /* FEAT_INS_EXPAND */ 1560 1561 /* 1562 * Add one line above "lp->lnum". This can be a filler line, a closed fold or 1563 * a (wrapped) text line. Uses and sets "lp->fill". 1564 * Returns the height of the added line in "lp->height". 1565 * Lines above the first one are incredibly high. 1566 */ 1567 static void 1568 topline_back(lp) 1569 lineoff_T *lp; 1570 { 1571 #ifdef FEAT_DIFF 1572 if (lp->fill < diff_check_fill(curwin, lp->lnum)) 1573 { 1574 /* Add a filler line. */ 1575 ++lp->fill; 1576 lp->height = 1; 1577 } 1578 else 1579 #endif 1580 { 1581 --lp->lnum; 1582 #ifdef FEAT_DIFF 1583 lp->fill = 0; 1584 #endif 1585 if (lp->lnum < 1) 1586 lp->height = MAXCOL; 1587 else 1588 #ifdef FEAT_FOLDING 1589 if (hasFolding(lp->lnum, &lp->lnum, NULL)) 1590 /* Add a closed fold */ 1591 lp->height = 1; 1592 else 1593 #endif 1594 { 1595 #ifdef FEAT_DIFF 1596 lp->height = plines_nofill(lp->lnum); 1597 #else 1598 lp->height = plines(lp->lnum); 1599 #endif 1600 } 1601 } 1602 } 1603 1604 /* 1605 * Add one line below "lp->lnum". This can be a filler line, a closed fold or 1606 * a (wrapped) text line. Uses and sets "lp->fill". 1607 * Returns the height of the added line in "lp->height". 1608 * Lines below the last one are incredibly high. 1609 */ 1610 static void 1611 botline_forw(lp) 1612 lineoff_T *lp; 1613 { 1614 #ifdef FEAT_DIFF 1615 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1)) 1616 { 1617 /* Add a filler line. */ 1618 ++lp->fill; 1619 lp->height = 1; 1620 } 1621 else 1622 #endif 1623 { 1624 ++lp->lnum; 1625 #ifdef FEAT_DIFF 1626 lp->fill = 0; 1627 #endif 1628 if (lp->lnum > curbuf->b_ml.ml_line_count) 1629 lp->height = MAXCOL; 1630 else 1631 #ifdef FEAT_FOLDING 1632 if (hasFolding(lp->lnum, NULL, &lp->lnum)) 1633 /* Add a closed fold */ 1634 lp->height = 1; 1635 else 1636 #endif 1637 { 1638 #ifdef FEAT_DIFF 1639 lp->height = plines_nofill(lp->lnum); 1640 #else 1641 lp->height = plines(lp->lnum); 1642 #endif 1643 } 1644 } 1645 } 1646 1647 #ifdef FEAT_DIFF 1648 /* 1649 * Switch from including filler lines below lp->lnum to including filler 1650 * lines above loff.lnum + 1. This keeps pointing to the same line. 1651 * When there are no filler lines nothing changes. 1652 */ 1653 static void 1654 botline_topline(lp) 1655 lineoff_T *lp; 1656 { 1657 if (lp->fill > 0) 1658 { 1659 ++lp->lnum; 1660 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1; 1661 } 1662 } 1663 1664 /* 1665 * Switch from including filler lines above lp->lnum to including filler 1666 * lines below loff.lnum - 1. This keeps pointing to the same line. 1667 * When there are no filler lines nothing changes. 1668 */ 1669 static void 1670 topline_botline(lp) 1671 lineoff_T *lp; 1672 { 1673 if (lp->fill > 0) 1674 { 1675 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1; 1676 --lp->lnum; 1677 } 1678 } 1679 #endif 1680 1681 /* 1682 * Recompute topline to put the cursor at the top of the window. 1683 * Scroll at least "min_scroll" lines. 1684 * If "always" is TRUE, always set topline (for "zt"). 1685 */ 1686 void 1687 scroll_cursor_top(min_scroll, always) 1688 int min_scroll; 1689 int always; 1690 { 1691 int scrolled = 0; 1692 int extra = 0; 1693 int used; 1694 int i; 1695 linenr_T top; /* just above displayed lines */ 1696 linenr_T bot; /* just below displayed lines */ 1697 linenr_T old_topline = curwin->w_topline; 1698 #ifdef FEAT_DIFF 1699 linenr_T old_topfill = curwin->w_topfill; 1700 #endif 1701 linenr_T new_topline; 1702 int off = p_so; 1703 1704 #ifdef FEAT_MOUSE 1705 if (mouse_dragging > 0) 1706 off = mouse_dragging - 1; 1707 #endif 1708 1709 /* 1710 * Decrease topline until: 1711 * - it has become 1 1712 * - (part of) the cursor line is moved off the screen or 1713 * - moved at least 'scrolljump' lines and 1714 * - at least 'scrolloff' lines above and below the cursor 1715 */ 1716 validate_cheight(); 1717 used = curwin->w_cline_height; 1718 if (curwin->w_cursor.lnum < curwin->w_topline) 1719 scrolled = used; 1720 1721 #ifdef FEAT_FOLDING 1722 if (hasFolding(curwin->w_cursor.lnum, &top, &bot)) 1723 { 1724 --top; 1725 ++bot; 1726 } 1727 else 1728 #endif 1729 { 1730 top = curwin->w_cursor.lnum - 1; 1731 bot = curwin->w_cursor.lnum + 1; 1732 } 1733 new_topline = top + 1; 1734 1735 #ifdef FEAT_DIFF 1736 /* count filler lines of the cursor window as context */ 1737 i = diff_check_fill(curwin, curwin->w_cursor.lnum); 1738 used += i; 1739 extra += i; 1740 #endif 1741 1742 /* 1743 * Check if the lines from "top" to "bot" fit in the window. If they do, 1744 * set new_topline and advance "top" and "bot" to include more lines. 1745 */ 1746 while (top > 0) 1747 { 1748 #ifdef FEAT_FOLDING 1749 if (hasFolding(top, &top, NULL)) 1750 /* count one logical line for a sequence of folded lines */ 1751 i = 1; 1752 else 1753 #endif 1754 i = plines(top); 1755 used += i; 1756 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count) 1757 { 1758 #ifdef FEAT_FOLDING 1759 if (hasFolding(bot, NULL, &bot)) 1760 /* count one logical line for a sequence of folded lines */ 1761 ++used; 1762 else 1763 #endif 1764 used += plines(bot); 1765 } 1766 if (used > curwin->w_height) 1767 break; 1768 if (top < curwin->w_topline) 1769 scrolled += i; 1770 1771 /* 1772 * If scrolling is needed, scroll at least 'sj' lines. 1773 */ 1774 if ((new_topline >= curwin->w_topline || scrolled > min_scroll) 1775 && extra >= off) 1776 break; 1777 1778 extra += i; 1779 new_topline = top; 1780 --top; 1781 ++bot; 1782 } 1783 1784 /* 1785 * If we don't have enough space, put cursor in the middle. 1786 * This makes sure we get the same position when using "k" and "j" 1787 * in a small window. 1788 */ 1789 if (used > curwin->w_height) 1790 scroll_cursor_halfway(FALSE); 1791 else 1792 { 1793 /* 1794 * If "always" is FALSE, only adjust topline to a lower value, higher 1795 * value may happen with wrapping lines 1796 */ 1797 if (new_topline < curwin->w_topline || always) 1798 curwin->w_topline = new_topline; 1799 if (curwin->w_topline > curwin->w_cursor.lnum) 1800 curwin->w_topline = curwin->w_cursor.lnum; 1801 #ifdef FEAT_DIFF 1802 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline); 1803 if (curwin->w_topfill > 0 && extra > off) 1804 { 1805 curwin->w_topfill -= extra - off; 1806 if (curwin->w_topfill < 0) 1807 curwin->w_topfill = 0; 1808 } 1809 check_topfill(curwin, FALSE); 1810 #endif 1811 if (curwin->w_topline != old_topline 1812 #ifdef FEAT_DIFF 1813 || curwin->w_topfill != old_topfill 1814 #endif 1815 ) 1816 curwin->w_valid &= 1817 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); 1818 curwin->w_valid |= VALID_TOPLINE; 1819 } 1820 } 1821 1822 /* 1823 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used" 1824 * screen lines for text lines. 1825 */ 1826 void 1827 set_empty_rows(wp, used) 1828 win_T *wp; 1829 int used; 1830 { 1831 #ifdef FEAT_DIFF 1832 wp->w_filler_rows = 0; 1833 #endif 1834 if (used == 0) 1835 wp->w_empty_rows = 0; /* single line that doesn't fit */ 1836 else 1837 { 1838 wp->w_empty_rows = wp->w_height - used; 1839 #ifdef FEAT_DIFF 1840 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count) 1841 { 1842 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline); 1843 if (wp->w_empty_rows > wp->w_filler_rows) 1844 wp->w_empty_rows -= wp->w_filler_rows; 1845 else 1846 { 1847 wp->w_filler_rows = wp->w_empty_rows; 1848 wp->w_empty_rows = 0; 1849 } 1850 } 1851 #endif 1852 } 1853 } 1854 1855 /* 1856 * Recompute topline to put the cursor at the bottom of the window. 1857 * Scroll at least "min_scroll" lines. 1858 * If "set_topbot" is TRUE, set topline and botline first (for "zb"). 1859 * This is messy stuff!!! 1860 */ 1861 void 1862 scroll_cursor_bot(min_scroll, set_topbot) 1863 int min_scroll; 1864 int set_topbot; 1865 { 1866 int used; 1867 int scrolled = 0; 1868 int extra = 0; 1869 int i; 1870 linenr_T line_count; 1871 linenr_T old_topline = curwin->w_topline; 1872 lineoff_T loff; 1873 lineoff_T boff; 1874 #ifdef FEAT_DIFF 1875 int old_topfill = curwin->w_topfill; 1876 int fill_below_window; 1877 #endif 1878 linenr_T old_botline = curwin->w_botline; 1879 linenr_T old_valid = curwin->w_valid; 1880 int old_empty_rows = curwin->w_empty_rows; 1881 linenr_T cln; /* Cursor Line Number */ 1882 1883 cln = curwin->w_cursor.lnum; 1884 if (set_topbot) 1885 { 1886 used = 0; 1887 curwin->w_botline = cln + 1; 1888 #ifdef FEAT_DIFF 1889 loff.fill = 0; 1890 #endif 1891 for (curwin->w_topline = curwin->w_botline; 1892 curwin->w_topline > 1; 1893 curwin->w_topline = loff.lnum) 1894 { 1895 loff.lnum = curwin->w_topline; 1896 topline_back(&loff); 1897 if (used + loff.height > curwin->w_height) 1898 break; 1899 used += loff.height; 1900 #ifdef FEAT_DIFF 1901 curwin->w_topfill = loff.fill; 1902 #endif 1903 } 1904 set_empty_rows(curwin, used); 1905 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP; 1906 if (curwin->w_topline != old_topline 1907 #ifdef FEAT_DIFF 1908 || curwin->w_topfill != old_topfill 1909 #endif 1910 ) 1911 curwin->w_valid &= ~(VALID_WROW|VALID_CROW); 1912 } 1913 else 1914 validate_botline(); 1915 1916 /* The lines of the cursor line itself are always used. */ 1917 #ifdef FEAT_DIFF 1918 used = plines_nofill(cln); 1919 #else 1920 validate_cheight(); 1921 used = curwin->w_cline_height; 1922 #endif 1923 1924 /* If the cursor is below botline, we will at least scroll by the height 1925 * of the cursor line. Correct for empty lines, which are really part of 1926 * botline. */ 1927 if (cln >= curwin->w_botline) 1928 { 1929 scrolled = used; 1930 if (cln == curwin->w_botline) 1931 scrolled -= curwin->w_empty_rows; 1932 } 1933 1934 /* 1935 * Stop counting lines to scroll when 1936 * - hitting start of the file 1937 * - scrolled nothing or at least 'sj' lines 1938 * - at least 'so' lines below the cursor 1939 * - lines between botline and cursor have been counted 1940 */ 1941 #ifdef FEAT_FOLDING 1942 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum)) 1943 #endif 1944 { 1945 loff.lnum = cln; 1946 boff.lnum = cln; 1947 } 1948 #ifdef FEAT_DIFF 1949 loff.fill = 0; 1950 boff.fill = 0; 1951 fill_below_window = diff_check_fill(curwin, curwin->w_botline) 1952 - curwin->w_filler_rows; 1953 #endif 1954 1955 while (loff.lnum > 1) 1956 { 1957 /* Stop when scrolled nothing or at least "min_scroll", found "extra" 1958 * context for 'scrolloff' and counted all lines below the window. */ 1959 if ((((scrolled <= 0 || scrolled >= min_scroll) 1960 && extra >= ( 1961 #ifdef FEAT_MOUSE 1962 mouse_dragging ? mouse_dragging - 1 : 1963 #endif 1964 p_so)) 1965 || boff.lnum + 1 > curbuf->b_ml.ml_line_count) 1966 && loff.lnum <= curwin->w_botline 1967 #ifdef FEAT_DIFF 1968 && (loff.lnum < curwin->w_botline 1969 || loff.fill >= fill_below_window) 1970 #endif 1971 ) 1972 break; 1973 1974 /* Add one line above */ 1975 topline_back(&loff); 1976 used += loff.height; 1977 if (used > curwin->w_height) 1978 break; 1979 if (loff.lnum >= curwin->w_botline 1980 #ifdef FEAT_DIFF 1981 && (loff.lnum > curwin->w_botline 1982 || loff.fill <= fill_below_window) 1983 #endif 1984 ) 1985 { 1986 /* Count screen lines that are below the window. */ 1987 scrolled += loff.height; 1988 if (loff.lnum == curwin->w_botline 1989 #ifdef FEAT_DIFF 1990 && boff.fill == 0 1991 #endif 1992 ) 1993 scrolled -= curwin->w_empty_rows; 1994 } 1995 1996 if (boff.lnum < curbuf->b_ml.ml_line_count) 1997 { 1998 /* Add one line below */ 1999 botline_forw(&boff); 2000 used += boff.height; 2001 if (used > curwin->w_height) 2002 break; 2003 if (extra < ( 2004 #ifdef FEAT_MOUSE 2005 mouse_dragging > 0 ? mouse_dragging - 1 : 2006 #endif 2007 p_so) || scrolled < min_scroll) 2008 { 2009 extra += boff.height; 2010 if (boff.lnum >= curwin->w_botline 2011 #ifdef FEAT_DIFF 2012 || (boff.lnum + 1 == curwin->w_botline 2013 && boff.fill > curwin->w_filler_rows) 2014 #endif 2015 ) 2016 { 2017 /* Count screen lines that are below the window. */ 2018 scrolled += boff.height; 2019 if (boff.lnum == curwin->w_botline 2020 #ifdef FEAT_DIFF 2021 && boff.fill == 0 2022 #endif 2023 ) 2024 scrolled -= curwin->w_empty_rows; 2025 } 2026 } 2027 } 2028 } 2029 2030 /* curwin->w_empty_rows is larger, no need to scroll */ 2031 if (scrolled <= 0) 2032 line_count = 0; 2033 /* more than a screenfull, don't scroll but redraw */ 2034 else if (used > curwin->w_height) 2035 line_count = used; 2036 /* scroll minimal number of lines */ 2037 else 2038 { 2039 line_count = 0; 2040 #ifdef FEAT_DIFF 2041 boff.fill = curwin->w_topfill; 2042 #endif 2043 boff.lnum = curwin->w_topline - 1; 2044 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; ) 2045 { 2046 botline_forw(&boff); 2047 i += boff.height; 2048 ++line_count; 2049 } 2050 if (i < scrolled) /* below curwin->w_botline, don't scroll */ 2051 line_count = 9999; 2052 } 2053 2054 /* 2055 * Scroll up if the cursor is off the bottom of the screen a bit. 2056 * Otherwise put it at 1/2 of the screen. 2057 */ 2058 if (line_count >= curwin->w_height && line_count > min_scroll) 2059 scroll_cursor_halfway(FALSE); 2060 else 2061 scrollup(line_count, TRUE); 2062 2063 /* 2064 * If topline didn't change we need to restore w_botline and w_empty_rows 2065 * (we changed them). 2066 * If topline did change, update_screen() will set botline. 2067 */ 2068 if (curwin->w_topline == old_topline && set_topbot) 2069 { 2070 curwin->w_botline = old_botline; 2071 curwin->w_empty_rows = old_empty_rows; 2072 curwin->w_valid = old_valid; 2073 } 2074 curwin->w_valid |= VALID_TOPLINE; 2075 } 2076 2077 /* 2078 * Recompute topline to put the cursor halfway the window 2079 * If "atend" is TRUE, also put it halfway at the end of the file. 2080 */ 2081 void 2082 scroll_cursor_halfway(atend) 2083 int atend; 2084 { 2085 int above = 0; 2086 linenr_T topline; 2087 #ifdef FEAT_DIFF 2088 int topfill = 0; 2089 #endif 2090 int below = 0; 2091 int used; 2092 lineoff_T loff; 2093 lineoff_T boff; 2094 2095 loff.lnum = boff.lnum = curwin->w_cursor.lnum; 2096 #ifdef FEAT_FOLDING 2097 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum); 2098 #endif 2099 #ifdef FEAT_DIFF 2100 used = plines_nofill(loff.lnum); 2101 loff.fill = 0; 2102 boff.fill = 0; 2103 #else 2104 used = plines(loff.lnum); 2105 #endif 2106 topline = loff.lnum; 2107 while (topline > 1) 2108 { 2109 if (below <= above) /* add a line below the cursor first */ 2110 { 2111 if (boff.lnum < curbuf->b_ml.ml_line_count) 2112 { 2113 botline_forw(&boff); 2114 used += boff.height; 2115 if (used > curwin->w_height) 2116 break; 2117 below += boff.height; 2118 } 2119 else 2120 { 2121 ++below; /* count a "~" line */ 2122 if (atend) 2123 ++used; 2124 } 2125 } 2126 2127 if (below > above) /* add a line above the cursor */ 2128 { 2129 topline_back(&loff); 2130 used += loff.height; 2131 if (used > curwin->w_height) 2132 break; 2133 above += loff.height; 2134 topline = loff.lnum; 2135 #ifdef FEAT_DIFF 2136 topfill = loff.fill; 2137 #endif 2138 } 2139 } 2140 #ifdef FEAT_FOLDING 2141 if (!hasFolding(topline, &curwin->w_topline, NULL)) 2142 #endif 2143 curwin->w_topline = topline; 2144 #ifdef FEAT_DIFF 2145 curwin->w_topfill = topfill; 2146 check_topfill(curwin, FALSE); 2147 #endif 2148 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); 2149 curwin->w_valid |= VALID_TOPLINE; 2150 } 2151 2152 /* 2153 * Correct the cursor position so that it is in a part of the screen at least 2154 * 'so' lines from the top and bottom, if possible. 2155 * If not possible, put it at the same position as scroll_cursor_halfway(). 2156 * When called topline must be valid! 2157 */ 2158 void 2159 cursor_correct() 2160 { 2161 int above = 0; /* screen lines above topline */ 2162 linenr_T topline; 2163 int below = 0; /* screen lines below botline */ 2164 linenr_T botline; 2165 int above_wanted, below_wanted; 2166 linenr_T cln; /* Cursor Line Number */ 2167 int max_off; 2168 2169 /* 2170 * How many lines we would like to have above/below the cursor depends on 2171 * whether the first/last line of the file is on screen. 2172 */ 2173 above_wanted = p_so; 2174 below_wanted = p_so; 2175 #ifdef FEAT_MOUSE 2176 if (mouse_dragging) 2177 { 2178 above_wanted = mouse_dragging - 1; 2179 below_wanted = mouse_dragging - 1; 2180 } 2181 #endif 2182 if (curwin->w_topline == 1) 2183 { 2184 above_wanted = 0; 2185 max_off = curwin->w_height / 2; 2186 if (below_wanted > max_off) 2187 below_wanted = max_off; 2188 } 2189 validate_botline(); 2190 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1 2191 #ifdef FEAT_MOUSE 2192 && !mouse_dragging 2193 #endif 2194 ) 2195 { 2196 below_wanted = 0; 2197 max_off = (curwin->w_height - 1) / 2; 2198 if (above_wanted > max_off) 2199 above_wanted = max_off; 2200 } 2201 2202 /* 2203 * If there are sufficient file-lines above and below the cursor, we can 2204 * return now. 2205 */ 2206 cln = curwin->w_cursor.lnum; 2207 if (cln >= curwin->w_topline + above_wanted 2208 && cln < curwin->w_botline - below_wanted 2209 #ifdef FEAT_FOLDING 2210 && !hasAnyFolding(curwin) 2211 #endif 2212 ) 2213 return; 2214 2215 /* 2216 * Narrow down the area where the cursor can be put by taking lines from 2217 * the top and the bottom until: 2218 * - the desired context lines are found 2219 * - the lines from the top is past the lines from the bottom 2220 */ 2221 topline = curwin->w_topline; 2222 botline = curwin->w_botline - 1; 2223 #ifdef FEAT_DIFF 2224 /* count filler lines as context */ 2225 above = curwin->w_topfill; 2226 below = curwin->w_filler_rows; 2227 #endif 2228 while ((above < above_wanted || below < below_wanted) && topline < botline) 2229 { 2230 if (below < below_wanted && (below <= above || above >= above_wanted)) 2231 { 2232 #ifdef FEAT_FOLDING 2233 if (hasFolding(botline, &botline, NULL)) 2234 ++below; 2235 else 2236 #endif 2237 below += plines(botline); 2238 --botline; 2239 } 2240 if (above < above_wanted && (above < below || below >= below_wanted)) 2241 { 2242 #ifdef FEAT_FOLDING 2243 if (hasFolding(topline, NULL, &topline)) 2244 ++above; 2245 else 2246 #endif 2247 #ifndef FEAT_DIFF 2248 above += plines(topline); 2249 #else 2250 above += plines_nofill(topline); 2251 2252 /* Count filler lines below this line as context. */ 2253 if (topline < botline) 2254 above += diff_check_fill(curwin, topline + 1); 2255 #endif 2256 ++topline; 2257 } 2258 } 2259 if (topline == botline || botline == 0) 2260 curwin->w_cursor.lnum = topline; 2261 else if (topline > botline) 2262 curwin->w_cursor.lnum = botline; 2263 else 2264 { 2265 if (cln < topline && curwin->w_topline > 1) 2266 { 2267 curwin->w_cursor.lnum = topline; 2268 curwin->w_valid &= 2269 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW); 2270 } 2271 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count) 2272 { 2273 curwin->w_cursor.lnum = botline; 2274 curwin->w_valid &= 2275 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW); 2276 } 2277 } 2278 curwin->w_valid |= VALID_TOPLINE; 2279 } 2280 2281 static void get_scroll_overlap __ARGS((lineoff_T *lp, int dir)); 2282 2283 /* 2284 * move screen 'count' pages up or down and update screen 2285 * 2286 * return FAIL for failure, OK otherwise 2287 */ 2288 int 2289 onepage(dir, count) 2290 int dir; 2291 long count; 2292 { 2293 long n; 2294 int retval = OK; 2295 lineoff_T loff; 2296 linenr_T old_topline = curwin->w_topline; 2297 2298 if (curbuf->b_ml.ml_line_count == 1) /* nothing to do */ 2299 { 2300 beep_flush(); 2301 return FAIL; 2302 } 2303 2304 for ( ; count > 0; --count) 2305 { 2306 validate_botline(); 2307 /* 2308 * It's an error to move a page up when the first line is already on 2309 * the screen. It's an error to move a page down when the last line 2310 * is on the screen and the topline is 'scrolloff' lines from the 2311 * last line. 2312 */ 2313 if (dir == FORWARD 2314 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - p_so) 2315 && curwin->w_botline > curbuf->b_ml.ml_line_count) 2316 : (curwin->w_topline == 1 2317 #ifdef FEAT_DIFF 2318 && curwin->w_topfill == 2319 diff_check_fill(curwin, curwin->w_topline) 2320 #endif 2321 )) 2322 { 2323 beep_flush(); 2324 retval = FAIL; 2325 break; 2326 } 2327 2328 #ifdef FEAT_DIFF 2329 loff.fill = 0; 2330 #endif 2331 if (dir == FORWARD) 2332 { 2333 /* at end of file */ 2334 if (curwin->w_botline > curbuf->b_ml.ml_line_count) 2335 { 2336 curwin->w_topline = curbuf->b_ml.ml_line_count; 2337 #ifdef FEAT_DIFF 2338 curwin->w_topfill = 0; 2339 #endif 2340 curwin->w_valid &= ~(VALID_WROW|VALID_CROW); 2341 } 2342 else 2343 { 2344 /* For the overlap, start with the line just below the window 2345 * and go upwards. */ 2346 loff.lnum = curwin->w_botline; 2347 #ifdef FEAT_DIFF 2348 loff.fill = diff_check_fill(curwin, loff.lnum) 2349 - curwin->w_filler_rows; 2350 #endif 2351 get_scroll_overlap(&loff, -1); 2352 curwin->w_topline = loff.lnum; 2353 #ifdef FEAT_DIFF 2354 curwin->w_topfill = loff.fill; 2355 check_topfill(curwin, FALSE); 2356 #endif 2357 curwin->w_cursor.lnum = curwin->w_topline; 2358 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW| 2359 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); 2360 } 2361 } 2362 else /* dir == BACKWARDS */ 2363 { 2364 #ifdef FEAT_DIFF 2365 if (curwin->w_topline == 1) 2366 { 2367 /* Include max number of filler lines */ 2368 max_topfill(); 2369 continue; 2370 } 2371 #endif 2372 /* Find the line at the top of the window that is going to be the 2373 * line at the bottom of the window. Make sure this results in 2374 * the same line as before doing CTRL-F. */ 2375 loff.lnum = curwin->w_topline - 1; 2376 #ifdef FEAT_DIFF 2377 loff.fill = diff_check_fill(curwin, loff.lnum + 1) 2378 - curwin->w_topfill; 2379 #endif 2380 get_scroll_overlap(&loff, 1); 2381 2382 if (loff.lnum >= curbuf->b_ml.ml_line_count) 2383 { 2384 loff.lnum = curbuf->b_ml.ml_line_count; 2385 #ifdef FEAT_DIFF 2386 loff.fill = 0; 2387 } 2388 else 2389 { 2390 botline_topline(&loff); 2391 #endif 2392 } 2393 curwin->w_cursor.lnum = loff.lnum; 2394 2395 /* Find the line just above the new topline to get the right line 2396 * at the bottom of the window. */ 2397 n = 0; 2398 while (n <= curwin->w_height && loff.lnum >= 1) 2399 { 2400 topline_back(&loff); 2401 n += loff.height; 2402 } 2403 if (n <= curwin->w_height) /* at begin of file */ 2404 { 2405 curwin->w_topline = 1; 2406 #ifdef FEAT_DIFF 2407 max_topfill(); 2408 #endif 2409 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE); 2410 } 2411 else 2412 { 2413 /* Go two lines forward again. */ 2414 #ifdef FEAT_DIFF 2415 topline_botline(&loff); 2416 #endif 2417 botline_forw(&loff); 2418 botline_forw(&loff); 2419 #ifdef FEAT_DIFF 2420 botline_topline(&loff); 2421 #endif 2422 #ifdef FEAT_FOLDING 2423 /* We're at the wrong end of a fold now. */ 2424 (void)hasFolding(loff.lnum, &loff.lnum, NULL); 2425 #endif 2426 2427 /* Always scroll at least one line. Avoid getting stuck on 2428 * very long lines. */ 2429 if (loff.lnum >= curwin->w_topline 2430 #ifdef FEAT_DIFF 2431 && (loff.lnum > curwin->w_topline 2432 || loff.fill >= curwin->w_topfill) 2433 #endif 2434 ) 2435 { 2436 #ifdef FEAT_DIFF 2437 /* First try using the maximum number of filler lines. If 2438 * that's not enough, backup one line. */ 2439 loff.fill = curwin->w_topfill; 2440 if (curwin->w_topfill < diff_check_fill(curwin, 2441 curwin->w_topline)) 2442 max_topfill(); 2443 if (curwin->w_topfill == loff.fill) 2444 #endif 2445 { 2446 --curwin->w_topline; 2447 #ifdef FEAT_DIFF 2448 curwin->w_topfill = 0; 2449 #endif 2450 } 2451 comp_botline(curwin); 2452 curwin->w_cursor.lnum = curwin->w_botline - 1; 2453 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT| 2454 VALID_WROW|VALID_CROW); 2455 } 2456 else 2457 { 2458 curwin->w_topline = loff.lnum; 2459 #ifdef FEAT_DIFF 2460 curwin->w_topfill = loff.fill; 2461 check_topfill(curwin, FALSE); 2462 #endif 2463 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE); 2464 } 2465 } 2466 } 2467 } 2468 #ifdef FEAT_FOLDING 2469 foldAdjustCursor(); 2470 #endif 2471 cursor_correct(); 2472 beginline(BL_SOL | BL_FIX); 2473 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL); 2474 2475 /* 2476 * Avoid the screen jumping up and down when 'scrolloff' is non-zero. 2477 * But make sure we scroll at least one line (happens with mix of long 2478 * wrapping lines and non-wrapping line). 2479 */ 2480 if (retval == OK && dir == FORWARD && check_top_offset()) 2481 { 2482 scroll_cursor_top(1, FALSE); 2483 if (curwin->w_topline <= old_topline 2484 && old_topline < curbuf->b_ml.ml_line_count) 2485 { 2486 curwin->w_topline = old_topline + 1; 2487 #ifdef FEAT_FOLDING 2488 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL); 2489 #endif 2490 } 2491 } 2492 2493 redraw_later(VALID); 2494 return retval; 2495 } 2496 2497 /* 2498 * Decide how much overlap to use for page-up or page-down scrolling. 2499 * This is symmetric, so that doing both keeps the same lines displayed. 2500 * Three lines are examined: 2501 * 2502 * before CTRL-F after CTRL-F / before CTRL-B 2503 * etc. l1 2504 * l1 last but one line ------------ 2505 * l2 last text line l2 top text line 2506 * ------------- l3 second text line 2507 * l3 etc. 2508 */ 2509 static void 2510 get_scroll_overlap(lp, dir) 2511 lineoff_T *lp; 2512 int dir; 2513 { 2514 int h1, h2, h3, h4; 2515 int min_height = curwin->w_height - 2; 2516 lineoff_T loff0, loff1, loff2; 2517 2518 #ifdef FEAT_DIFF 2519 if (lp->fill > 0) 2520 lp->height = 1; 2521 else 2522 lp->height = plines_nofill(lp->lnum); 2523 #else 2524 lp->height = plines(lp->lnum); 2525 #endif 2526 h1 = lp->height; 2527 if (h1 > min_height) 2528 return; /* no overlap */ 2529 2530 loff0 = *lp; 2531 if (dir > 0) 2532 botline_forw(lp); 2533 else 2534 topline_back(lp); 2535 h2 = lp->height; 2536 if (h2 + h1 > min_height) 2537 { 2538 *lp = loff0; /* no overlap */ 2539 return; 2540 } 2541 2542 loff1 = *lp; 2543 if (dir > 0) 2544 botline_forw(lp); 2545 else 2546 topline_back(lp); 2547 h3 = lp->height; 2548 if (h3 + h2 > min_height) 2549 { 2550 *lp = loff0; /* no overlap */ 2551 return; 2552 } 2553 2554 loff2 = *lp; 2555 if (dir > 0) 2556 botline_forw(lp); 2557 else 2558 topline_back(lp); 2559 h4 = lp->height; 2560 if (h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height) 2561 *lp = loff1; /* 1 line overlap */ 2562 else 2563 *lp = loff2; /* 2 lines overlap */ 2564 return; 2565 } 2566 2567 /* #define KEEP_SCREEN_LINE */ 2568 /* 2569 * Scroll 'scroll' lines up or down. 2570 */ 2571 void 2572 halfpage(flag, Prenum) 2573 int flag; 2574 linenr_T Prenum; 2575 { 2576 long scrolled = 0; 2577 int i; 2578 int n; 2579 int room; 2580 2581 if (Prenum) 2582 curwin->w_p_scr = (Prenum > curwin->w_height) ? 2583 curwin->w_height : Prenum; 2584 n = (curwin->w_p_scr <= curwin->w_height) ? 2585 curwin->w_p_scr : curwin->w_height; 2586 2587 validate_botline(); 2588 room = curwin->w_empty_rows; 2589 #ifdef FEAT_DIFF 2590 room += curwin->w_filler_rows; 2591 #endif 2592 if (flag) 2593 { 2594 /* 2595 * scroll the text up 2596 */ 2597 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count) 2598 { 2599 #ifdef FEAT_DIFF 2600 if (curwin->w_topfill > 0) 2601 { 2602 i = 1; 2603 if (--n < 0 && scrolled > 0) 2604 break; 2605 --curwin->w_topfill; 2606 } 2607 else 2608 #endif 2609 { 2610 #ifdef FEAT_DIFF 2611 i = plines_nofill(curwin->w_topline); 2612 #else 2613 i = plines(curwin->w_topline); 2614 #endif 2615 n -= i; 2616 if (n < 0 && scrolled > 0) 2617 break; 2618 #ifdef FEAT_FOLDING 2619 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline); 2620 #endif 2621 ++curwin->w_topline; 2622 #ifdef FEAT_DIFF 2623 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline); 2624 #endif 2625 2626 #ifndef KEEP_SCREEN_LINE 2627 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) 2628 { 2629 ++curwin->w_cursor.lnum; 2630 curwin->w_valid &= 2631 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL); 2632 } 2633 #endif 2634 } 2635 curwin->w_valid &= ~(VALID_CROW|VALID_WROW); 2636 scrolled += i; 2637 2638 /* 2639 * Correct w_botline for changed w_topline. 2640 * Won't work when there are filler lines. 2641 */ 2642 #ifdef FEAT_DIFF 2643 if (curwin->w_p_diff) 2644 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP); 2645 else 2646 #endif 2647 { 2648 room += i; 2649 do 2650 { 2651 i = plines(curwin->w_botline); 2652 if (i > room) 2653 break; 2654 #ifdef FEAT_FOLDING 2655 (void)hasFolding(curwin->w_botline, NULL, 2656 &curwin->w_botline); 2657 #endif 2658 ++curwin->w_botline; 2659 room -= i; 2660 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count); 2661 } 2662 } 2663 2664 #ifndef KEEP_SCREEN_LINE 2665 /* 2666 * When hit bottom of the file: move cursor down. 2667 */ 2668 if (n > 0) 2669 { 2670 # ifdef FEAT_FOLDING 2671 if (hasAnyFolding(curwin)) 2672 { 2673 while (--n >= 0 2674 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) 2675 { 2676 (void)hasFolding(curwin->w_cursor.lnum, NULL, 2677 &curwin->w_cursor.lnum); 2678 ++curwin->w_cursor.lnum; 2679 } 2680 } 2681 else 2682 # endif 2683 curwin->w_cursor.lnum += n; 2684 check_cursor_lnum(); 2685 } 2686 #else 2687 /* try to put the cursor in the same screen line */ 2688 while ((curwin->w_cursor.lnum < curwin->w_topline || scrolled > 0) 2689 && curwin->w_cursor.lnum < curwin->w_botline - 1) 2690 { 2691 scrolled -= plines(curwin->w_cursor.lnum); 2692 if (scrolled < 0 && curwin->w_cursor.lnum >= curwin->w_topline) 2693 break; 2694 # ifdef FEAT_FOLDING 2695 (void)hasFolding(curwin->w_cursor.lnum, NULL, 2696 &curwin->w_cursor.lnum); 2697 # endif 2698 ++curwin->w_cursor.lnum; 2699 } 2700 #endif 2701 } 2702 else 2703 { 2704 /* 2705 * scroll the text down 2706 */ 2707 while (n > 0 && curwin->w_topline > 1) 2708 { 2709 #ifdef FEAT_DIFF 2710 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline)) 2711 { 2712 i = 1; 2713 if (--n < 0 && scrolled > 0) 2714 break; 2715 ++curwin->w_topfill; 2716 } 2717 else 2718 #endif 2719 { 2720 #ifdef FEAT_DIFF 2721 i = plines_nofill(curwin->w_topline - 1); 2722 #else 2723 i = plines(curwin->w_topline - 1); 2724 #endif 2725 n -= i; 2726 if (n < 0 && scrolled > 0) 2727 break; 2728 --curwin->w_topline; 2729 #ifdef FEAT_FOLDING 2730 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL); 2731 #endif 2732 #ifdef FEAT_DIFF 2733 curwin->w_topfill = 0; 2734 #endif 2735 } 2736 curwin->w_valid &= ~(VALID_CROW|VALID_WROW| 2737 VALID_BOTLINE|VALID_BOTLINE_AP); 2738 scrolled += i; 2739 #ifndef KEEP_SCREEN_LINE 2740 if (curwin->w_cursor.lnum > 1) 2741 { 2742 --curwin->w_cursor.lnum; 2743 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL); 2744 } 2745 #endif 2746 } 2747 #ifndef KEEP_SCREEN_LINE 2748 /* 2749 * When hit top of the file: move cursor up. 2750 */ 2751 if (n > 0) 2752 { 2753 if (curwin->w_cursor.lnum <= (linenr_T)n) 2754 curwin->w_cursor.lnum = 1; 2755 else 2756 # ifdef FEAT_FOLDING 2757 if (hasAnyFolding(curwin)) 2758 { 2759 while (--n >= 0 && curwin->w_cursor.lnum > 1) 2760 { 2761 --curwin->w_cursor.lnum; 2762 (void)hasFolding(curwin->w_cursor.lnum, 2763 &curwin->w_cursor.lnum, NULL); 2764 } 2765 } 2766 else 2767 # endif 2768 curwin->w_cursor.lnum -= n; 2769 } 2770 #else 2771 /* try to put the cursor in the same screen line */ 2772 scrolled += n; /* move cursor when topline is 1 */ 2773 while (curwin->w_cursor.lnum > curwin->w_topline 2774 && (scrolled > 0 || curwin->w_cursor.lnum >= curwin->w_botline)) 2775 { 2776 scrolled -= plines(curwin->w_cursor.lnum - 1); 2777 if (scrolled < 0 && curwin->w_cursor.lnum < curwin->w_botline) 2778 break; 2779 --curwin->w_cursor.lnum; 2780 # ifdef FEAT_FOLDING 2781 foldAdjustCursor(); 2782 # endif 2783 } 2784 #endif 2785 } 2786 # ifdef FEAT_FOLDING 2787 /* Move cursor to first line of closed fold. */ 2788 foldAdjustCursor(); 2789 # endif 2790 #ifdef FEAT_DIFF 2791 check_topfill(curwin, !flag); 2792 #endif 2793 cursor_correct(); 2794 beginline(BL_SOL | BL_FIX); 2795 redraw_later(VALID); 2796 } 2797