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