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