1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read a list of people who contributed. 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 #include "vim.h" 11 12 static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum); 13 static void win_init(win_T *newp, win_T *oldp, int flags); 14 static void win_init_some(win_T *newp, win_T *oldp); 15 static void frame_comp_pos(frame_T *topfrp, int *row, int *col); 16 static void frame_setheight(frame_T *curfrp, int height); 17 static void frame_setwidth(frame_T *curfrp, int width); 18 static void win_exchange(long); 19 static void win_rotate(int, int); 20 static void win_totop(int size, int flags); 21 static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height); 22 static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp); 23 static frame_T *win_altframe(win_T *win, tabpage_T *tp); 24 static tabpage_T *alt_tabpage(void); 25 static win_T *frame2win(frame_T *frp); 26 static int frame_has_win(frame_T *frp, win_T *wp); 27 static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh); 28 static int frame_fixed_height(frame_T *frp); 29 static int frame_fixed_width(frame_T *frp); 30 static void frame_add_statusline(frame_T *frp); 31 static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw); 32 static void frame_add_vsep(frame_T *frp); 33 static int frame_minwidth(frame_T *topfrp, win_T *next_curwin); 34 static void frame_fix_width(win_T *wp); 35 static int win_alloc_firstwin(win_T *oldwin); 36 static void new_frame(win_T *wp); 37 static tabpage_T *alloc_tabpage(void); 38 static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds); 39 static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds); 40 static void frame_fix_height(win_T *wp); 41 static int frame_minheight(frame_T *topfrp, win_T *next_curwin); 42 static int may_open_tabpage(void); 43 static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_new_autocmds, int trigger_enter_autocmds, int trigger_leave_autocmds); 44 static void win_free(win_T *wp, tabpage_T *tp); 45 static int win_unlisted(win_T *wp); 46 static void win_append(win_T *after, win_T *wp); 47 static void frame_append(frame_T *after, frame_T *frp); 48 static void frame_insert(frame_T *before, frame_T *frp); 49 static void frame_remove(frame_T *frp); 50 static void win_goto_ver(int up, long count); 51 static void win_goto_hor(int left, long count); 52 static void frame_add_height(frame_T *frp, int n); 53 static void last_status_rec(frame_T *fr, int statusline); 54 55 static void make_snapshot_rec(frame_T *fr, frame_T **frp); 56 static void clear_snapshot(tabpage_T *tp, int idx); 57 static void clear_snapshot_rec(frame_T *fr); 58 static int check_snapshot_rec(frame_T *sn, frame_T *fr); 59 static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr); 60 61 static int frame_check_height(frame_T *topfrp, int height); 62 static int frame_check_width(frame_T *topfrp, int width); 63 64 static win_T *win_alloc(win_T *after, int hidden); 65 66 #define NOWIN (win_T *)-1 // non-existing window 67 68 #define ROWS_AVAIL (Rows - p_ch - tabline_height()) 69 70 static char *m_onlyone = N_("Already only one window"); 71 72 // When non-zero splitting a window is forbidden. Used to avoid that nasty 73 // autocommands mess up the window structure. 74 static int split_disallowed = 0; 75 76 // #define WIN_DEBUG 77 #ifdef WIN_DEBUG 78 /* 79 * Call this method to log the current window layout. 80 */ 81 static void 82 log_frame_layout(frame_T *frame) 83 { 84 ch_log(NULL, "layout %s, wi: %d, he: %d, wwi: %d, whe: %d, id: %d", 85 frame->fr_layout == FR_LEAF ? "LEAF" 86 : frame->fr_layout == FR_ROW ? "ROW" : "COL", 87 frame->fr_width, 88 frame->fr_height, 89 frame->fr_win == NULL ? -1 : frame->fr_win->w_width, 90 frame->fr_win == NULL ? -1 : frame->fr_win->w_height, 91 frame->fr_win == NULL ? -1 : frame->fr_win->w_id); 92 if (frame->fr_child != NULL) 93 { 94 ch_log(NULL, "children"); 95 log_frame_layout(frame->fr_child); 96 if (frame->fr_next != NULL) 97 ch_log(NULL, "END of children"); 98 } 99 if (frame->fr_next != NULL) 100 log_frame_layout(frame->fr_next); 101 } 102 #endif 103 104 /* 105 * All CTRL-W window commands are handled here, called from normal_cmd(). 106 */ 107 void 108 do_window( 109 int nchar, 110 long Prenum, 111 int xchar) // extra char from ":wincmd gx" or NUL 112 { 113 long Prenum1; 114 win_T *wp; 115 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID) 116 char_u *ptr; 117 linenr_T lnum = -1; 118 #endif 119 #ifdef FEAT_FIND_ID 120 int type = FIND_DEFINE; 121 int len; 122 #endif 123 char_u cbuf[40]; 124 125 if (ERROR_IF_ANY_POPUP_WINDOW) 126 return; 127 128 #ifdef FEAT_CMDWIN 129 # define CHECK_CMDWIN \ 130 do { \ 131 if (cmdwin_type != 0) \ 132 { \ 133 emsg(_(e_cmdwin)); \ 134 return; \ 135 } \ 136 } while (0) 137 #else 138 # define CHECK_CMDWIN do { /**/ } while (0) 139 #endif 140 141 Prenum1 = Prenum == 0 ? 1 : Prenum; 142 143 switch (nchar) 144 { 145 // split current window in two parts, horizontally 146 case 'S': 147 case Ctrl_S: 148 case 's': 149 CHECK_CMDWIN; 150 reset_VIsual_and_resel(); // stop Visual mode 151 #ifdef FEAT_QUICKFIX 152 // When splitting the quickfix window open a new buffer in it, 153 // don't replicate the quickfix buffer. 154 if (bt_quickfix(curbuf)) 155 goto newwindow; 156 #endif 157 #ifdef FEAT_GUI 158 need_mouse_correct = TRUE; 159 #endif 160 (void)win_split((int)Prenum, 0); 161 break; 162 163 // split current window in two parts, vertically 164 case Ctrl_V: 165 case 'v': 166 CHECK_CMDWIN; 167 reset_VIsual_and_resel(); // stop Visual mode 168 #ifdef FEAT_QUICKFIX 169 // When splitting the quickfix window open a new buffer in it, 170 // don't replicate the quickfix buffer. 171 if (bt_quickfix(curbuf)) 172 goto newwindow; 173 #endif 174 #ifdef FEAT_GUI 175 need_mouse_correct = TRUE; 176 #endif 177 (void)win_split((int)Prenum, WSP_VERT); 178 break; 179 180 // split current window and edit alternate file 181 case Ctrl_HAT: 182 case '^': 183 CHECK_CMDWIN; 184 reset_VIsual_and_resel(); // stop Visual mode 185 186 if (buflist_findnr(Prenum == 0 187 ? curwin->w_alt_fnum : Prenum) == NULL) 188 { 189 if (Prenum == 0) 190 emsg(_(e_noalt)); 191 else 192 semsg(_("E92: Buffer %ld not found"), Prenum); 193 break; 194 } 195 196 if (!curbuf_locked() && win_split(0, 0) == OK) 197 (void)buflist_getfile( 198 Prenum == 0 ? curwin->w_alt_fnum : Prenum, 199 (linenr_T)0, GETF_ALT, FALSE); 200 break; 201 202 // open new window 203 case Ctrl_N: 204 case 'n': 205 CHECK_CMDWIN; 206 reset_VIsual_and_resel(); // stop Visual mode 207 #ifdef FEAT_QUICKFIX 208 newwindow: 209 #endif 210 if (Prenum) 211 // window height 212 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum); 213 else 214 cbuf[0] = NUL; 215 #if defined(FEAT_QUICKFIX) 216 if (nchar == 'v' || nchar == Ctrl_V) 217 STRCAT(cbuf, "v"); 218 #endif 219 STRCAT(cbuf, "new"); 220 do_cmdline_cmd(cbuf); 221 break; 222 223 // quit current window 224 case Ctrl_Q: 225 case 'q': 226 reset_VIsual_and_resel(); // stop Visual mode 227 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum); 228 do_cmdline_cmd(cbuf); 229 break; 230 231 // close current window 232 case Ctrl_C: 233 case 'c': 234 reset_VIsual_and_resel(); // stop Visual mode 235 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum); 236 do_cmdline_cmd(cbuf); 237 break; 238 239 #if defined(FEAT_QUICKFIX) 240 // close preview window 241 case Ctrl_Z: 242 case 'z': 243 CHECK_CMDWIN; 244 reset_VIsual_and_resel(); // stop Visual mode 245 do_cmdline_cmd((char_u *)"pclose"); 246 break; 247 248 // cursor to preview window 249 case 'P': 250 FOR_ALL_WINDOWS(wp) 251 if (wp->w_p_pvw) 252 break; 253 if (wp == NULL) 254 emsg(_("E441: There is no preview window")); 255 else 256 win_goto(wp); 257 break; 258 #endif 259 260 // close all but current window 261 case Ctrl_O: 262 case 'o': 263 CHECK_CMDWIN; 264 reset_VIsual_and_resel(); // stop Visual mode 265 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum); 266 do_cmdline_cmd(cbuf); 267 break; 268 269 // cursor to next window with wrap around 270 case Ctrl_W: 271 case 'w': 272 // cursor to previous window with wrap around 273 case 'W': 274 CHECK_CMDWIN; 275 if (ONE_WINDOW && Prenum != 1) // just one window 276 beep_flush(); 277 else 278 { 279 if (Prenum) // go to specified window 280 { 281 for (wp = firstwin; --Prenum > 0; ) 282 { 283 if (wp->w_next == NULL) 284 break; 285 else 286 wp = wp->w_next; 287 } 288 } 289 else 290 { 291 if (nchar == 'W') // go to previous window 292 { 293 wp = curwin->w_prev; 294 if (wp == NULL) 295 wp = lastwin; // wrap around 296 } 297 else // go to next window 298 { 299 wp = curwin->w_next; 300 if (wp == NULL) 301 wp = firstwin; // wrap around 302 } 303 } 304 win_goto(wp); 305 } 306 break; 307 308 // cursor to window below 309 case 'j': 310 case K_DOWN: 311 case Ctrl_J: 312 CHECK_CMDWIN; 313 win_goto_ver(FALSE, Prenum1); 314 break; 315 316 // cursor to window above 317 case 'k': 318 case K_UP: 319 case Ctrl_K: 320 CHECK_CMDWIN; 321 win_goto_ver(TRUE, Prenum1); 322 break; 323 324 // cursor to left window 325 case 'h': 326 case K_LEFT: 327 case Ctrl_H: 328 case K_BS: 329 CHECK_CMDWIN; 330 win_goto_hor(TRUE, Prenum1); 331 break; 332 333 // cursor to right window 334 case 'l': 335 case K_RIGHT: 336 case Ctrl_L: 337 CHECK_CMDWIN; 338 win_goto_hor(FALSE, Prenum1); 339 break; 340 341 // move window to new tab page 342 case 'T': 343 CHECK_CMDWIN; 344 if (one_window()) 345 msg(_(m_onlyone)); 346 else 347 { 348 tabpage_T *oldtab = curtab; 349 tabpage_T *newtab; 350 351 // First create a new tab with the window, then go back to 352 // the old tab and close the window there. 353 wp = curwin; 354 if (win_new_tabpage((int)Prenum) == OK 355 && valid_tabpage(oldtab)) 356 { 357 newtab = curtab; 358 goto_tabpage_tp(oldtab, TRUE, TRUE); 359 if (curwin == wp) 360 win_close(curwin, FALSE); 361 if (valid_tabpage(newtab)) 362 goto_tabpage_tp(newtab, TRUE, TRUE); 363 } 364 } 365 break; 366 367 // cursor to top-left window 368 case 't': 369 case Ctrl_T: 370 win_goto(firstwin); 371 break; 372 373 // cursor to bottom-right window 374 case 'b': 375 case Ctrl_B: 376 win_goto(lastwin); 377 break; 378 379 // cursor to last accessed (previous) window 380 case 'p': 381 case Ctrl_P: 382 if (!win_valid(prevwin)) 383 beep_flush(); 384 else 385 win_goto(prevwin); 386 break; 387 388 // exchange current and next window 389 case 'x': 390 case Ctrl_X: 391 CHECK_CMDWIN; 392 win_exchange(Prenum); 393 break; 394 395 // rotate windows downwards 396 case Ctrl_R: 397 case 'r': 398 CHECK_CMDWIN; 399 reset_VIsual_and_resel(); // stop Visual mode 400 win_rotate(FALSE, (int)Prenum1); // downwards 401 break; 402 403 // rotate windows upwards 404 case 'R': 405 CHECK_CMDWIN; 406 reset_VIsual_and_resel(); // stop Visual mode 407 win_rotate(TRUE, (int)Prenum1); // upwards 408 break; 409 410 // move window to the very top/bottom/left/right 411 case 'K': 412 case 'J': 413 case 'H': 414 case 'L': 415 CHECK_CMDWIN; 416 win_totop((int)Prenum, 417 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0) 418 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT)); 419 break; 420 421 // make all windows the same height 422 case '=': 423 #ifdef FEAT_GUI 424 need_mouse_correct = TRUE; 425 #endif 426 win_equal(NULL, FALSE, 'b'); 427 break; 428 429 // increase current window height 430 case '+': 431 #ifdef FEAT_GUI 432 need_mouse_correct = TRUE; 433 #endif 434 win_setheight(curwin->w_height + (int)Prenum1); 435 break; 436 437 // decrease current window height 438 case '-': 439 #ifdef FEAT_GUI 440 need_mouse_correct = TRUE; 441 #endif 442 win_setheight(curwin->w_height - (int)Prenum1); 443 break; 444 445 // set current window height 446 case Ctrl__: 447 case '_': 448 #ifdef FEAT_GUI 449 need_mouse_correct = TRUE; 450 #endif 451 win_setheight(Prenum ? (int)Prenum : 9999); 452 break; 453 454 // increase current window width 455 case '>': 456 #ifdef FEAT_GUI 457 need_mouse_correct = TRUE; 458 #endif 459 win_setwidth(curwin->w_width + (int)Prenum1); 460 break; 461 462 // decrease current window width 463 case '<': 464 #ifdef FEAT_GUI 465 need_mouse_correct = TRUE; 466 #endif 467 win_setwidth(curwin->w_width - (int)Prenum1); 468 break; 469 470 // set current window width 471 case '|': 472 #ifdef FEAT_GUI 473 need_mouse_correct = TRUE; 474 #endif 475 win_setwidth(Prenum != 0 ? (int)Prenum : 9999); 476 break; 477 478 // jump to tag and split window if tag exists (in preview window) 479 #if defined(FEAT_QUICKFIX) 480 case '}': 481 CHECK_CMDWIN; 482 if (Prenum) 483 g_do_tagpreview = Prenum; 484 else 485 g_do_tagpreview = p_pvh; 486 #endif 487 // FALLTHROUGH 488 case ']': 489 case Ctrl_RSB: 490 CHECK_CMDWIN; 491 // keep Visual mode, can select words to use as a tag 492 if (Prenum) 493 postponed_split = Prenum; 494 else 495 postponed_split = -1; 496 #ifdef FEAT_QUICKFIX 497 if (nchar != '}') 498 g_do_tagpreview = 0; 499 #endif 500 501 // Execute the command right here, required when "wincmd ]" 502 // was used in a function. 503 do_nv_ident(Ctrl_RSB, NUL); 504 break; 505 506 #ifdef FEAT_SEARCHPATH 507 // edit file name under cursor in a new window 508 case 'f': 509 case 'F': 510 case Ctrl_F: 511 wingotofile: 512 CHECK_CMDWIN; 513 514 ptr = grab_file_name(Prenum1, &lnum); 515 if (ptr != NULL) 516 { 517 tabpage_T *oldtab = curtab; 518 win_T *oldwin = curwin; 519 # ifdef FEAT_GUI 520 need_mouse_correct = TRUE; 521 # endif 522 setpcmark(); 523 if (win_split(0, 0) == OK) 524 { 525 RESET_BINDING(curwin); 526 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, 527 ECMD_HIDE, NULL) == FAIL) 528 { 529 // Failed to open the file, close the window 530 // opened for it. 531 win_close(curwin, FALSE); 532 goto_tabpage_win(oldtab, oldwin); 533 } 534 else if (nchar == 'F' && lnum >= 0) 535 { 536 curwin->w_cursor.lnum = lnum; 537 check_cursor_lnum(); 538 beginline(BL_SOL | BL_FIX); 539 } 540 } 541 vim_free(ptr); 542 } 543 break; 544 #endif 545 546 #ifdef FEAT_FIND_ID 547 // Go to the first occurrence of the identifier under cursor along path in a 548 // new window -- webb 549 case 'i': // Go to any match 550 case Ctrl_I: 551 type = FIND_ANY; 552 // FALLTHROUGH 553 case 'd': // Go to definition, using 'define' 554 case Ctrl_D: 555 CHECK_CMDWIN; 556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) 557 break; 558 find_pattern_in_path(ptr, 0, len, TRUE, 559 Prenum == 0 ? TRUE : FALSE, type, 560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM); 561 curwin->w_set_curswant = TRUE; 562 break; 563 #endif 564 565 // Quickfix window only: view the result under the cursor in a new split. 566 #if defined(FEAT_QUICKFIX) 567 case K_KENTER: 568 case CAR: 569 if (bt_quickfix(curbuf)) 570 qf_view_result(TRUE); 571 break; 572 #endif 573 574 // CTRL-W g extended commands 575 case 'g': 576 case Ctrl_G: 577 CHECK_CMDWIN; 578 #ifdef USE_ON_FLY_SCROLL 579 dont_scroll = TRUE; // disallow scrolling here 580 #endif 581 ++no_mapping; 582 ++allow_keys; // no mapping for xchar, but allow key codes 583 if (xchar == NUL) 584 xchar = plain_vgetc(); 585 LANGMAP_ADJUST(xchar, TRUE); 586 --no_mapping; 587 --allow_keys; 588 #ifdef FEAT_CMDL_INFO 589 (void)add_to_showcmd(xchar); 590 #endif 591 switch (xchar) 592 { 593 #if defined(FEAT_QUICKFIX) 594 case '}': 595 xchar = Ctrl_RSB; 596 if (Prenum) 597 g_do_tagpreview = Prenum; 598 else 599 g_do_tagpreview = p_pvh; 600 #endif 601 // FALLTHROUGH 602 case ']': 603 case Ctrl_RSB: 604 // keep Visual mode, can select words to use as a tag 605 if (Prenum) 606 postponed_split = Prenum; 607 else 608 postponed_split = -1; 609 610 // Execute the command right here, required when 611 // "wincmd g}" was used in a function. 612 do_nv_ident('g', xchar); 613 break; 614 615 #ifdef FEAT_SEARCHPATH 616 case 'f': // CTRL-W gf: "gf" in a new tab page 617 case 'F': // CTRL-W gF: "gF" in a new tab page 618 cmdmod.tab = tabpage_index(curtab) + 1; 619 nchar = xchar; 620 goto wingotofile; 621 #endif 622 case 't': // CTRL-W gt: go to next tab page 623 goto_tabpage((int)Prenum); 624 break; 625 626 case 'T': // CTRL-W gT: go to previous tab page 627 goto_tabpage(-(int)Prenum1); 628 break; 629 630 case TAB: // CTRL-W g<Tab>: go to last used tab page 631 if (goto_tabpage_lastused() == FAIL) 632 beep_flush(); 633 break; 634 635 default: 636 beep_flush(); 637 break; 638 } 639 break; 640 641 default: beep_flush(); 642 break; 643 } 644 } 645 646 /* 647 * Figure out the address type for ":wincmd". 648 */ 649 void 650 get_wincmd_addr_type(char_u *arg, exarg_T *eap) 651 { 652 switch (*arg) 653 { 654 case 'S': 655 case Ctrl_S: 656 case 's': 657 case Ctrl_N: 658 case 'n': 659 case 'j': 660 case Ctrl_J: 661 case 'k': 662 case Ctrl_K: 663 case 'T': 664 case Ctrl_R: 665 case 'r': 666 case 'R': 667 case 'K': 668 case 'J': 669 case '+': 670 case '-': 671 case Ctrl__: 672 case '_': 673 case '|': 674 case ']': 675 case Ctrl_RSB: 676 case 'g': 677 case Ctrl_G: 678 case Ctrl_V: 679 case 'v': 680 case 'h': 681 case Ctrl_H: 682 case 'l': 683 case Ctrl_L: 684 case 'H': 685 case 'L': 686 case '>': 687 case '<': 688 #if defined(FEAT_QUICKFIX) 689 case '}': 690 #endif 691 #ifdef FEAT_SEARCHPATH 692 case 'f': 693 case 'F': 694 case Ctrl_F: 695 #endif 696 #ifdef FEAT_FIND_ID 697 case 'i': 698 case Ctrl_I: 699 case 'd': 700 case Ctrl_D: 701 #endif 702 // window size or any count 703 eap->addr_type = ADDR_OTHER; 704 break; 705 706 case Ctrl_HAT: 707 case '^': 708 // buffer number 709 eap->addr_type = ADDR_BUFFERS; 710 break; 711 712 case Ctrl_Q: 713 case 'q': 714 case Ctrl_C: 715 case 'c': 716 case Ctrl_O: 717 case 'o': 718 case Ctrl_W: 719 case 'w': 720 case 'W': 721 case 'x': 722 case Ctrl_X: 723 // window number 724 eap->addr_type = ADDR_WINDOWS; 725 break; 726 727 #if defined(FEAT_QUICKFIX) 728 case Ctrl_Z: 729 case 'z': 730 case 'P': 731 #endif 732 case 't': 733 case Ctrl_T: 734 case 'b': 735 case Ctrl_B: 736 case 'p': 737 case Ctrl_P: 738 case '=': 739 case CAR: 740 // no count 741 eap->addr_type = ADDR_NONE; 742 break; 743 } 744 } 745 746 static void 747 cmd_with_count( 748 char *cmd, 749 char_u *bufp, 750 size_t bufsize, 751 long Prenum) 752 { 753 size_t len = STRLEN(cmd); 754 755 STRCPY(bufp, cmd); 756 if (Prenum > 0) 757 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum); 758 } 759 760 /* 761 * If "split_disallowed" is set given an error and return FAIL. 762 * Otherwise return OK. 763 */ 764 static int 765 check_split_disallowed() 766 { 767 if (split_disallowed > 0) 768 { 769 emsg(_("E242: Can't split a window while closing another")); 770 return FAIL; 771 } 772 return OK; 773 } 774 775 /* 776 * split the current window, implements CTRL-W s and :split 777 * 778 * "size" is the height or width for the new window, 0 to use half of current 779 * height or width. 780 * 781 * "flags": 782 * WSP_ROOM: require enough room for new window 783 * WSP_VERT: vertical split. 784 * WSP_TOP: open window at the top-left of the shell (help window). 785 * WSP_BOT: open window at the bottom-right of the shell (quickfix window). 786 * WSP_HELP: creating the help window, keep layout snapshot 787 * 788 * return FAIL for failure, OK otherwise 789 */ 790 int 791 win_split(int size, int flags) 792 { 793 if (ERROR_IF_ANY_POPUP_WINDOW) 794 return FAIL; 795 796 // When the ":tab" modifier was used open a new tab page instead. 797 if (may_open_tabpage() == OK) 798 return OK; 799 800 // Add flags from ":vertical", ":topleft" and ":botright". 801 flags |= cmdmod.split; 802 if ((flags & WSP_TOP) && (flags & WSP_BOT)) 803 { 804 emsg(_("E442: Can't split topleft and botright at the same time")); 805 return FAIL; 806 } 807 if (check_split_disallowed() == FAIL) 808 return FAIL; 809 810 // When creating the help window make a snapshot of the window layout. 811 // Otherwise clear the snapshot, it's now invalid. 812 if (flags & WSP_HELP) 813 make_snapshot(SNAP_HELP_IDX); 814 else 815 clear_snapshot(curtab, SNAP_HELP_IDX); 816 817 return win_split_ins(size, flags, NULL, 0); 818 } 819 820 /* 821 * When "new_wp" is NULL: split the current window in two. 822 * When "new_wp" is not NULL: insert this window at the far 823 * top/left/right/bottom. 824 * return FAIL for failure, OK otherwise 825 */ 826 int 827 win_split_ins( 828 int size, 829 int flags, 830 win_T *new_wp, 831 int dir) 832 { 833 win_T *wp = new_wp; 834 win_T *oldwin; 835 int new_size = size; 836 int i; 837 int need_status = 0; 838 int do_equal = FALSE; 839 int needed; 840 int available; 841 int oldwin_height = 0; 842 int layout; 843 frame_T *frp, *curfrp, *frp2, *prevfrp; 844 int before; 845 int minheight; 846 int wmh1; 847 int did_set_fraction = FALSE; 848 849 if (flags & WSP_TOP) 850 oldwin = firstwin; 851 else if (flags & WSP_BOT) 852 oldwin = lastwin; 853 else 854 oldwin = curwin; 855 856 // add a status line when p_ls == 1 and splitting the first window 857 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0) 858 { 859 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL) 860 { 861 emsg(_(e_noroom)); 862 return FAIL; 863 } 864 need_status = STATUS_HEIGHT; 865 } 866 867 #ifdef FEAT_GUI 868 // May be needed for the scrollbars that are going to change. 869 if (gui.in_use) 870 out_flush(); 871 #endif 872 873 if (flags & WSP_VERT) 874 { 875 int wmw1; 876 int minwidth; 877 878 layout = FR_ROW; 879 880 /* 881 * Check if we are able to split the current window and compute its 882 * width. 883 */ 884 // Current window requires at least 1 space. 885 wmw1 = (p_wmw == 0 ? 1 : p_wmw); 886 needed = wmw1 + 1; 887 if (flags & WSP_ROOM) 888 needed += p_wiw - wmw1; 889 if (flags & (WSP_BOT | WSP_TOP)) 890 { 891 minwidth = frame_minwidth(topframe, NOWIN); 892 available = topframe->fr_width; 893 needed += minwidth; 894 } 895 else if (p_ea) 896 { 897 minwidth = frame_minwidth(oldwin->w_frame, NOWIN); 898 prevfrp = oldwin->w_frame; 899 for (frp = oldwin->w_frame->fr_parent; frp != NULL; 900 frp = frp->fr_parent) 901 { 902 if (frp->fr_layout == FR_ROW) 903 FOR_ALL_FRAMES(frp2, frp->fr_child) 904 if (frp2 != prevfrp) 905 minwidth += frame_minwidth(frp2, NOWIN); 906 prevfrp = frp; 907 } 908 available = topframe->fr_width; 909 needed += minwidth; 910 } 911 else 912 { 913 minwidth = frame_minwidth(oldwin->w_frame, NOWIN); 914 available = oldwin->w_frame->fr_width; 915 needed += minwidth; 916 } 917 if (available < needed && new_wp == NULL) 918 { 919 emsg(_(e_noroom)); 920 return FAIL; 921 } 922 if (new_size == 0) 923 new_size = oldwin->w_width / 2; 924 if (new_size > available - minwidth - 1) 925 new_size = available - minwidth - 1; 926 if (new_size < wmw1) 927 new_size = wmw1; 928 929 // if it doesn't fit in the current window, need win_equal() 930 if (oldwin->w_width - new_size - 1 < p_wmw) 931 do_equal = TRUE; 932 933 // We don't like to take lines for the new window from a 934 // 'winfixwidth' window. Take them from a window to the left or right 935 // instead, if possible. Add one for the separator. 936 if (oldwin->w_p_wfw) 937 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin); 938 939 // Only make all windows the same width if one of them (except oldwin) 940 // is wider than one of the split windows. 941 if (!do_equal && p_ea && size == 0 && *p_ead != 'v' 942 && oldwin->w_frame->fr_parent != NULL) 943 { 944 frp = oldwin->w_frame->fr_parent->fr_child; 945 while (frp != NULL) 946 { 947 if (frp->fr_win != oldwin && frp->fr_win != NULL 948 && (frp->fr_win->w_width > new_size 949 || frp->fr_win->w_width > oldwin->w_width 950 - new_size - 1)) 951 { 952 do_equal = TRUE; 953 break; 954 } 955 frp = frp->fr_next; 956 } 957 } 958 } 959 else 960 { 961 layout = FR_COL; 962 963 /* 964 * Check if we are able to split the current window and compute its 965 * height. 966 */ 967 // Current window requires at least 1 space. 968 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin); 969 needed = wmh1 + STATUS_HEIGHT; 970 if (flags & WSP_ROOM) 971 needed += p_wh - wmh1; 972 if (flags & (WSP_BOT | WSP_TOP)) 973 { 974 minheight = frame_minheight(topframe, NOWIN) + need_status; 975 available = topframe->fr_height; 976 needed += minheight; 977 } 978 else if (p_ea) 979 { 980 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status; 981 prevfrp = oldwin->w_frame; 982 for (frp = oldwin->w_frame->fr_parent; frp != NULL; 983 frp = frp->fr_parent) 984 { 985 if (frp->fr_layout == FR_COL) 986 FOR_ALL_FRAMES(frp2, frp->fr_child) 987 if (frp2 != prevfrp) 988 minheight += frame_minheight(frp2, NOWIN); 989 prevfrp = frp; 990 } 991 available = topframe->fr_height; 992 needed += minheight; 993 } 994 else 995 { 996 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status; 997 available = oldwin->w_frame->fr_height; 998 needed += minheight; 999 } 1000 if (available < needed && new_wp == NULL) 1001 { 1002 emsg(_(e_noroom)); 1003 return FAIL; 1004 } 1005 oldwin_height = oldwin->w_height; 1006 if (need_status) 1007 { 1008 oldwin->w_status_height = STATUS_HEIGHT; 1009 oldwin_height -= STATUS_HEIGHT; 1010 } 1011 if (new_size == 0) 1012 new_size = oldwin_height / 2; 1013 if (new_size > available - minheight - STATUS_HEIGHT) 1014 new_size = available - minheight - STATUS_HEIGHT; 1015 if (new_size < wmh1) 1016 new_size = wmh1; 1017 1018 // if it doesn't fit in the current window, need win_equal() 1019 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh) 1020 do_equal = TRUE; 1021 1022 // We don't like to take lines for the new window from a 1023 // 'winfixheight' window. Take them from a window above or below 1024 // instead, if possible. 1025 if (oldwin->w_p_wfh) 1026 { 1027 // Set w_fraction now so that the cursor keeps the same relative 1028 // vertical position using the old height. 1029 set_fraction(oldwin); 1030 did_set_fraction = TRUE; 1031 1032 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT, 1033 oldwin); 1034 oldwin_height = oldwin->w_height; 1035 if (need_status) 1036 oldwin_height -= STATUS_HEIGHT; 1037 } 1038 1039 // Only make all windows the same height if one of them (except oldwin) 1040 // is higher than one of the split windows. 1041 if (!do_equal && p_ea && size == 0 && *p_ead != 'h' 1042 && oldwin->w_frame->fr_parent != NULL) 1043 { 1044 frp = oldwin->w_frame->fr_parent->fr_child; 1045 while (frp != NULL) 1046 { 1047 if (frp->fr_win != oldwin && frp->fr_win != NULL 1048 && (frp->fr_win->w_height > new_size 1049 || frp->fr_win->w_height > oldwin_height - new_size 1050 - STATUS_HEIGHT)) 1051 { 1052 do_equal = TRUE; 1053 break; 1054 } 1055 frp = frp->fr_next; 1056 } 1057 } 1058 } 1059 1060 /* 1061 * allocate new window structure and link it in the window list 1062 */ 1063 if ((flags & WSP_TOP) == 0 1064 && ((flags & WSP_BOT) 1065 || (flags & WSP_BELOW) 1066 || (!(flags & WSP_ABOVE) 1067 && ( (flags & WSP_VERT) ? p_spr : p_sb)))) 1068 { 1069 // new window below/right of current one 1070 if (new_wp == NULL) 1071 wp = win_alloc(oldwin, FALSE); 1072 else 1073 win_append(oldwin, wp); 1074 } 1075 else 1076 { 1077 if (new_wp == NULL) 1078 wp = win_alloc(oldwin->w_prev, FALSE); 1079 else 1080 win_append(oldwin->w_prev, wp); 1081 } 1082 1083 if (new_wp == NULL) 1084 { 1085 if (wp == NULL) 1086 return FAIL; 1087 1088 new_frame(wp); 1089 if (wp->w_frame == NULL) 1090 { 1091 win_free(wp, NULL); 1092 return FAIL; 1093 } 1094 1095 // make the contents of the new window the same as the current one 1096 win_init(wp, curwin, flags); 1097 } 1098 1099 /* 1100 * Reorganise the tree of frames to insert the new window. 1101 */ 1102 if (flags & (WSP_TOP | WSP_BOT)) 1103 { 1104 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0) 1105 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0)) 1106 { 1107 curfrp = topframe->fr_child; 1108 if (flags & WSP_BOT) 1109 while (curfrp->fr_next != NULL) 1110 curfrp = curfrp->fr_next; 1111 } 1112 else 1113 curfrp = topframe; 1114 before = (flags & WSP_TOP); 1115 } 1116 else 1117 { 1118 curfrp = oldwin->w_frame; 1119 if (flags & WSP_BELOW) 1120 before = FALSE; 1121 else if (flags & WSP_ABOVE) 1122 before = TRUE; 1123 else if (flags & WSP_VERT) 1124 before = !p_spr; 1125 else 1126 before = !p_sb; 1127 } 1128 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout) 1129 { 1130 // Need to create a new frame in the tree to make a branch. 1131 frp = ALLOC_CLEAR_ONE(frame_T); 1132 *frp = *curfrp; 1133 curfrp->fr_layout = layout; 1134 frp->fr_parent = curfrp; 1135 frp->fr_next = NULL; 1136 frp->fr_prev = NULL; 1137 curfrp->fr_child = frp; 1138 curfrp->fr_win = NULL; 1139 curfrp = frp; 1140 if (frp->fr_win != NULL) 1141 oldwin->w_frame = frp; 1142 else 1143 FOR_ALL_FRAMES(frp, frp->fr_child) 1144 frp->fr_parent = curfrp; 1145 } 1146 1147 if (new_wp == NULL) 1148 frp = wp->w_frame; 1149 else 1150 frp = new_wp->w_frame; 1151 frp->fr_parent = curfrp->fr_parent; 1152 1153 // Insert the new frame at the right place in the frame list. 1154 if (before) 1155 frame_insert(curfrp, frp); 1156 else 1157 frame_append(curfrp, frp); 1158 1159 // Set w_fraction now so that the cursor keeps the same relative 1160 // vertical position. 1161 if (!did_set_fraction) 1162 set_fraction(oldwin); 1163 wp->w_fraction = oldwin->w_fraction; 1164 1165 if (flags & WSP_VERT) 1166 { 1167 wp->w_p_scr = curwin->w_p_scr; 1168 1169 if (need_status) 1170 { 1171 win_new_height(oldwin, oldwin->w_height - 1); 1172 oldwin->w_status_height = need_status; 1173 } 1174 if (flags & (WSP_TOP | WSP_BOT)) 1175 { 1176 // set height and row of new window to full height 1177 wp->w_winrow = tabline_height(); 1178 win_new_height(wp, curfrp->fr_height - (p_ls > 0) 1179 - WINBAR_HEIGHT(wp)); 1180 wp->w_status_height = (p_ls > 0); 1181 } 1182 else 1183 { 1184 // height and row of new window is same as current window 1185 wp->w_winrow = oldwin->w_winrow; 1186 win_new_height(wp, VISIBLE_HEIGHT(oldwin)); 1187 wp->w_status_height = oldwin->w_status_height; 1188 } 1189 frp->fr_height = curfrp->fr_height; 1190 1191 // "new_size" of the current window goes to the new window, use 1192 // one column for the vertical separator 1193 win_new_width(wp, new_size); 1194 if (before) 1195 wp->w_vsep_width = 1; 1196 else 1197 { 1198 wp->w_vsep_width = oldwin->w_vsep_width; 1199 oldwin->w_vsep_width = 1; 1200 } 1201 if (flags & (WSP_TOP | WSP_BOT)) 1202 { 1203 if (flags & WSP_BOT) 1204 frame_add_vsep(curfrp); 1205 // Set width of neighbor frame 1206 frame_new_width(curfrp, curfrp->fr_width 1207 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP, 1208 FALSE); 1209 } 1210 else 1211 win_new_width(oldwin, oldwin->w_width - (new_size + 1)); 1212 if (before) // new window left of current one 1213 { 1214 wp->w_wincol = oldwin->w_wincol; 1215 oldwin->w_wincol += new_size + 1; 1216 } 1217 else // new window right of current one 1218 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1; 1219 frame_fix_width(oldwin); 1220 frame_fix_width(wp); 1221 } 1222 else 1223 { 1224 // width and column of new window is same as current window 1225 if (flags & (WSP_TOP | WSP_BOT)) 1226 { 1227 wp->w_wincol = 0; 1228 win_new_width(wp, Columns); 1229 wp->w_vsep_width = 0; 1230 } 1231 else 1232 { 1233 wp->w_wincol = oldwin->w_wincol; 1234 win_new_width(wp, oldwin->w_width); 1235 wp->w_vsep_width = oldwin->w_vsep_width; 1236 } 1237 frp->fr_width = curfrp->fr_width; 1238 1239 // "new_size" of the current window goes to the new window, use 1240 // one row for the status line 1241 win_new_height(wp, new_size); 1242 if (flags & (WSP_TOP | WSP_BOT)) 1243 { 1244 int new_fr_height = curfrp->fr_height - new_size 1245 + WINBAR_HEIGHT(wp) ; 1246 1247 if (!((flags & WSP_BOT) && p_ls == 0)) 1248 new_fr_height -= STATUS_HEIGHT; 1249 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE); 1250 } 1251 else 1252 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT)); 1253 if (before) // new window above current one 1254 { 1255 wp->w_winrow = oldwin->w_winrow; 1256 wp->w_status_height = STATUS_HEIGHT; 1257 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT; 1258 } 1259 else // new window below current one 1260 { 1261 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin) 1262 + STATUS_HEIGHT; 1263 wp->w_status_height = oldwin->w_status_height; 1264 if (!(flags & WSP_BOT)) 1265 oldwin->w_status_height = STATUS_HEIGHT; 1266 } 1267 if (flags & WSP_BOT) 1268 frame_add_statusline(curfrp); 1269 frame_fix_height(wp); 1270 frame_fix_height(oldwin); 1271 } 1272 1273 if (flags & (WSP_TOP | WSP_BOT)) 1274 (void)win_comp_pos(); 1275 1276 // Both windows need redrawing. Update all status lines, in case they 1277 // show something related to the window count or position. 1278 redraw_win_later(wp, NOT_VALID); 1279 redraw_win_later(oldwin, NOT_VALID); 1280 status_redraw_all(); 1281 1282 if (need_status) 1283 { 1284 msg_row = Rows - 1; 1285 msg_col = sc_col; 1286 msg_clr_eos_force(); // Old command/ruler may still be there 1287 comp_col(); 1288 msg_row = Rows - 1; 1289 msg_col = 0; // put position back at start of line 1290 } 1291 1292 /* 1293 * equalize the window sizes. 1294 */ 1295 if (do_equal || dir != 0) 1296 win_equal(wp, TRUE, 1297 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h') 1298 : dir == 'h' ? 'b' : 'v'); 1299 1300 // Don't change the window height/width to 'winheight' / 'winwidth' if a 1301 // size was given. 1302 if (flags & WSP_VERT) 1303 { 1304 i = p_wiw; 1305 if (size != 0) 1306 p_wiw = size; 1307 1308 # ifdef FEAT_GUI 1309 // When 'guioptions' includes 'L' or 'R' may have to add scrollbars. 1310 if (gui.in_use) 1311 gui_init_which_components(NULL); 1312 # endif 1313 } 1314 else 1315 { 1316 i = p_wh; 1317 if (size != 0) 1318 p_wh = size; 1319 } 1320 1321 #ifdef FEAT_JUMPLIST 1322 // Keep same changelist position in new window. 1323 wp->w_changelistidx = oldwin->w_changelistidx; 1324 #endif 1325 1326 /* 1327 * make the new window the current window 1328 */ 1329 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE); 1330 if (flags & WSP_VERT) 1331 p_wiw = i; 1332 else 1333 p_wh = i; 1334 1335 return OK; 1336 } 1337 1338 1339 /* 1340 * Initialize window "newp" from window "oldp". 1341 * Used when splitting a window and when creating a new tab page. 1342 * The windows will both edit the same buffer. 1343 * WSP_NEWLOC may be specified in flags to prevent the location list from 1344 * being copied. 1345 */ 1346 static void 1347 win_init(win_T *newp, win_T *oldp, int flags UNUSED) 1348 { 1349 int i; 1350 1351 newp->w_buffer = oldp->w_buffer; 1352 #ifdef FEAT_SYN_HL 1353 newp->w_s = &(oldp->w_buffer->b_s); 1354 #endif 1355 oldp->w_buffer->b_nwindows++; 1356 newp->w_cursor = oldp->w_cursor; 1357 newp->w_valid = 0; 1358 newp->w_curswant = oldp->w_curswant; 1359 newp->w_set_curswant = oldp->w_set_curswant; 1360 newp->w_topline = oldp->w_topline; 1361 #ifdef FEAT_DIFF 1362 newp->w_topfill = oldp->w_topfill; 1363 #endif 1364 newp->w_leftcol = oldp->w_leftcol; 1365 newp->w_pcmark = oldp->w_pcmark; 1366 newp->w_prev_pcmark = oldp->w_prev_pcmark; 1367 newp->w_alt_fnum = oldp->w_alt_fnum; 1368 newp->w_wrow = oldp->w_wrow; 1369 newp->w_fraction = oldp->w_fraction; 1370 newp->w_prev_fraction_row = oldp->w_prev_fraction_row; 1371 #ifdef FEAT_JUMPLIST 1372 copy_jumplist(oldp, newp); 1373 #endif 1374 #ifdef FEAT_QUICKFIX 1375 if (flags & WSP_NEWLOC) 1376 { 1377 // Don't copy the location list. 1378 newp->w_llist = NULL; 1379 newp->w_llist_ref = NULL; 1380 } 1381 else 1382 copy_loclist_stack(oldp, newp); 1383 #endif 1384 newp->w_localdir = (oldp->w_localdir == NULL) 1385 ? NULL : vim_strsave(oldp->w_localdir); 1386 newp->w_prevdir = (oldp->w_prevdir == NULL) 1387 ? NULL : vim_strsave(oldp->w_prevdir); 1388 1389 // copy tagstack and folds 1390 for (i = 0; i < oldp->w_tagstacklen; i++) 1391 { 1392 taggy_T *tag = &newp->w_tagstack[i]; 1393 *tag = oldp->w_tagstack[i]; 1394 if (tag->tagname != NULL) 1395 tag->tagname = vim_strsave(tag->tagname); 1396 if (tag->user_data != NULL) 1397 tag->user_data = vim_strsave(tag->user_data); 1398 } 1399 newp->w_tagstackidx = oldp->w_tagstackidx; 1400 newp->w_tagstacklen = oldp->w_tagstacklen; 1401 #ifdef FEAT_FOLDING 1402 copyFoldingState(oldp, newp); 1403 #endif 1404 1405 win_init_some(newp, oldp); 1406 1407 #ifdef FEAT_SYN_HL 1408 check_colorcolumn(newp); 1409 #endif 1410 } 1411 1412 /* 1413 * Initialize window "newp" from window "old". 1414 * Only the essential things are copied. 1415 */ 1416 static void 1417 win_init_some(win_T *newp, win_T *oldp) 1418 { 1419 // Use the same argument list. 1420 newp->w_alist = oldp->w_alist; 1421 ++newp->w_alist->al_refcount; 1422 newp->w_arg_idx = oldp->w_arg_idx; 1423 1424 // copy options from existing window 1425 win_copy_options(oldp, newp); 1426 } 1427 1428 /* 1429 * Return TRUE if "win" is a global popup or a popup in the current tab page. 1430 */ 1431 int 1432 win_valid_popup(win_T *win UNUSED) 1433 { 1434 #ifdef FEAT_PROP_POPUP 1435 win_T *wp; 1436 1437 FOR_ALL_POPUPWINS(wp) 1438 if (wp == win) 1439 return TRUE; 1440 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp) 1441 if (wp == win) 1442 return TRUE; 1443 #endif 1444 return FALSE; 1445 } 1446 1447 /* 1448 * Check if "win" is a pointer to an existing window in the current tab page. 1449 */ 1450 int 1451 win_valid(win_T *win) 1452 { 1453 win_T *wp; 1454 1455 if (win == NULL) 1456 return FALSE; 1457 FOR_ALL_WINDOWS(wp) 1458 if (wp == win) 1459 return TRUE; 1460 return win_valid_popup(win); 1461 } 1462 1463 /* 1464 * Check if "win" is a pointer to an existing window in any tab page. 1465 */ 1466 int 1467 win_valid_any_tab(win_T *win) 1468 { 1469 win_T *wp; 1470 tabpage_T *tp; 1471 1472 if (win == NULL) 1473 return FALSE; 1474 FOR_ALL_TABPAGES(tp) 1475 { 1476 FOR_ALL_WINDOWS_IN_TAB(tp, wp) 1477 { 1478 if (wp == win) 1479 return TRUE; 1480 } 1481 #ifdef FEAT_PROP_POPUP 1482 FOR_ALL_POPUPWINS_IN_TAB(tp, wp) 1483 if (wp == win) 1484 return TRUE; 1485 #endif 1486 } 1487 return win_valid_popup(win); 1488 } 1489 1490 /* 1491 * Return the number of windows. 1492 */ 1493 int 1494 win_count(void) 1495 { 1496 win_T *wp; 1497 int count = 0; 1498 1499 FOR_ALL_WINDOWS(wp) 1500 ++count; 1501 return count; 1502 } 1503 1504 /* 1505 * Make "count" windows on the screen. 1506 * Return actual number of windows on the screen. 1507 * Must be called when there is just one window, filling the whole screen 1508 * (excluding the command line). 1509 */ 1510 int 1511 make_windows( 1512 int count, 1513 int vertical UNUSED) // split windows vertically if TRUE 1514 { 1515 int maxcount; 1516 int todo; 1517 1518 if (vertical) 1519 { 1520 // Each windows needs at least 'winminwidth' lines and a separator 1521 // column. 1522 maxcount = (curwin->w_width + curwin->w_vsep_width 1523 - (p_wiw - p_wmw)) / (p_wmw + 1); 1524 } 1525 else 1526 { 1527 // Each window needs at least 'winminheight' lines and a status line. 1528 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height 1529 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT); 1530 } 1531 1532 if (maxcount < 2) 1533 maxcount = 2; 1534 if (count > maxcount) 1535 count = maxcount; 1536 1537 /* 1538 * add status line now, otherwise first window will be too big 1539 */ 1540 if (count > 1) 1541 last_status(TRUE); 1542 1543 /* 1544 * Don't execute autocommands while creating the windows. Must do that 1545 * when putting the buffers in the windows. 1546 */ 1547 block_autocmds(); 1548 1549 // todo is number of windows left to create 1550 for (todo = count - 1; todo > 0; --todo) 1551 if (vertical) 1552 { 1553 if (win_split(curwin->w_width - (curwin->w_width - todo) 1554 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL) 1555 break; 1556 } 1557 else 1558 { 1559 if (win_split(curwin->w_height - (curwin->w_height - todo 1560 * STATUS_HEIGHT) / (todo + 1) 1561 - STATUS_HEIGHT, WSP_ABOVE) == FAIL) 1562 break; 1563 } 1564 1565 unblock_autocmds(); 1566 1567 // return actual number of windows 1568 return (count - todo); 1569 } 1570 1571 /* 1572 * Exchange current and next window 1573 */ 1574 static void 1575 win_exchange(long Prenum) 1576 { 1577 frame_T *frp; 1578 frame_T *frp2; 1579 win_T *wp; 1580 win_T *wp2; 1581 int temp; 1582 1583 if (ERROR_IF_ANY_POPUP_WINDOW) 1584 return; 1585 if (ONE_WINDOW) // just one window 1586 { 1587 beep_flush(); 1588 return; 1589 } 1590 1591 #ifdef FEAT_GUI 1592 need_mouse_correct = TRUE; 1593 #endif 1594 1595 /* 1596 * find window to exchange with 1597 */ 1598 if (Prenum) 1599 { 1600 frp = curwin->w_frame->fr_parent->fr_child; 1601 while (frp != NULL && --Prenum > 0) 1602 frp = frp->fr_next; 1603 } 1604 else if (curwin->w_frame->fr_next != NULL) // Swap with next 1605 frp = curwin->w_frame->fr_next; 1606 else // Swap last window in row/col with previous 1607 frp = curwin->w_frame->fr_prev; 1608 1609 // We can only exchange a window with another window, not with a frame 1610 // containing windows. 1611 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin) 1612 return; 1613 wp = frp->fr_win; 1614 1615 /* 1616 * 1. remove curwin from the list. Remember after which window it was in wp2 1617 * 2. insert curwin before wp in the list 1618 * if wp != wp2 1619 * 3. remove wp from the list 1620 * 4. insert wp after wp2 1621 * 5. exchange the status line height and vsep width. 1622 */ 1623 wp2 = curwin->w_prev; 1624 frp2 = curwin->w_frame->fr_prev; 1625 if (wp->w_prev != curwin) 1626 { 1627 win_remove(curwin, NULL); 1628 frame_remove(curwin->w_frame); 1629 win_append(wp->w_prev, curwin); 1630 frame_insert(frp, curwin->w_frame); 1631 } 1632 if (wp != wp2) 1633 { 1634 win_remove(wp, NULL); 1635 frame_remove(wp->w_frame); 1636 win_append(wp2, wp); 1637 if (frp2 == NULL) 1638 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame); 1639 else 1640 frame_append(frp2, wp->w_frame); 1641 } 1642 temp = curwin->w_status_height; 1643 curwin->w_status_height = wp->w_status_height; 1644 wp->w_status_height = temp; 1645 temp = curwin->w_vsep_width; 1646 curwin->w_vsep_width = wp->w_vsep_width; 1647 wp->w_vsep_width = temp; 1648 1649 // If the windows are not in the same frame, exchange the sizes to avoid 1650 // messing up the window layout. Otherwise fix the frame sizes. 1651 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent) 1652 { 1653 temp = curwin->w_height; 1654 curwin->w_height = wp->w_height; 1655 wp->w_height = temp; 1656 temp = curwin->w_width; 1657 curwin->w_width = wp->w_width; 1658 wp->w_width = temp; 1659 } 1660 else 1661 { 1662 frame_fix_height(curwin); 1663 frame_fix_height(wp); 1664 frame_fix_width(curwin); 1665 frame_fix_width(wp); 1666 } 1667 1668 (void)win_comp_pos(); // recompute window positions 1669 1670 win_enter(wp, TRUE); 1671 redraw_all_later(NOT_VALID); 1672 } 1673 1674 /* 1675 * rotate windows: if upwards TRUE the second window becomes the first one 1676 * if upwards FALSE the first window becomes the second one 1677 */ 1678 static void 1679 win_rotate(int upwards, int count) 1680 { 1681 win_T *wp1; 1682 win_T *wp2; 1683 frame_T *frp; 1684 int n; 1685 1686 if (ONE_WINDOW) // nothing to do 1687 { 1688 beep_flush(); 1689 return; 1690 } 1691 1692 #ifdef FEAT_GUI 1693 need_mouse_correct = TRUE; 1694 #endif 1695 1696 // Check if all frames in this row/col have one window. 1697 FOR_ALL_FRAMES(frp, curwin->w_frame->fr_parent->fr_child) 1698 if (frp->fr_win == NULL) 1699 { 1700 emsg(_("E443: Cannot rotate when another window is split")); 1701 return; 1702 } 1703 1704 while (count--) 1705 { 1706 if (upwards) // first window becomes last window 1707 { 1708 // remove first window/frame from the list 1709 frp = curwin->w_frame->fr_parent->fr_child; 1710 wp1 = frp->fr_win; 1711 win_remove(wp1, NULL); 1712 frame_remove(frp); 1713 1714 // find last frame and append removed window/frame after it 1715 for ( ; frp->fr_next != NULL; frp = frp->fr_next) 1716 ; 1717 win_append(frp->fr_win, wp1); 1718 frame_append(frp, wp1->w_frame); 1719 1720 wp2 = frp->fr_win; // previously last window 1721 } 1722 else // last window becomes first window 1723 { 1724 // find last window/frame in the list and remove it 1725 for (frp = curwin->w_frame; frp->fr_next != NULL; 1726 frp = frp->fr_next) 1727 ; 1728 wp1 = frp->fr_win; 1729 wp2 = wp1->w_prev; // will become last window 1730 win_remove(wp1, NULL); 1731 frame_remove(frp); 1732 1733 // append the removed window/frame before the first in the list 1734 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1); 1735 frame_insert(frp->fr_parent->fr_child, frp); 1736 } 1737 1738 // exchange status height and vsep width of old and new last window 1739 n = wp2->w_status_height; 1740 wp2->w_status_height = wp1->w_status_height; 1741 wp1->w_status_height = n; 1742 frame_fix_height(wp1); 1743 frame_fix_height(wp2); 1744 n = wp2->w_vsep_width; 1745 wp2->w_vsep_width = wp1->w_vsep_width; 1746 wp1->w_vsep_width = n; 1747 frame_fix_width(wp1); 1748 frame_fix_width(wp2); 1749 1750 // recompute w_winrow and w_wincol for all windows 1751 (void)win_comp_pos(); 1752 } 1753 1754 redraw_all_later(NOT_VALID); 1755 } 1756 1757 /* 1758 * Move the current window to the very top/bottom/left/right of the screen. 1759 */ 1760 static void 1761 win_totop(int size, int flags) 1762 { 1763 int dir; 1764 int height = curwin->w_height; 1765 1766 if (ONE_WINDOW) 1767 { 1768 beep_flush(); 1769 return; 1770 } 1771 if (check_split_disallowed() == FAIL) 1772 return; 1773 1774 // Remove the window and frame from the tree of frames. 1775 (void)winframe_remove(curwin, &dir, NULL); 1776 win_remove(curwin, NULL); 1777 last_status(FALSE); // may need to remove last status line 1778 (void)win_comp_pos(); // recompute window positions 1779 1780 // Split a window on the desired side and put the window there. 1781 (void)win_split_ins(size, flags, curwin, dir); 1782 if (!(flags & WSP_VERT)) 1783 { 1784 win_setheight(height); 1785 if (p_ea) 1786 win_equal(curwin, TRUE, 'v'); 1787 } 1788 1789 #if defined(FEAT_GUI) 1790 // When 'guioptions' includes 'L' or 'R' may have to remove or add 1791 // scrollbars. Have to update them anyway. 1792 gui_may_update_scrollbars(); 1793 #endif 1794 } 1795 1796 /* 1797 * Move window "win1" to below/right of "win2" and make "win1" the current 1798 * window. Only works within the same frame! 1799 */ 1800 void 1801 win_move_after(win_T *win1, win_T *win2) 1802 { 1803 int height; 1804 1805 // check if the arguments are reasonable 1806 if (win1 == win2) 1807 return; 1808 1809 // check if there is something to do 1810 if (win2->w_next != win1) 1811 { 1812 if (win1->w_frame->fr_parent != win2->w_frame->fr_parent) 1813 { 1814 iemsg("INTERNAL: trying to move a window into another frame"); 1815 return; 1816 } 1817 1818 // may need to move the status line/vertical separator of the last 1819 // window 1820 if (win1 == lastwin) 1821 { 1822 height = win1->w_prev->w_status_height; 1823 win1->w_prev->w_status_height = win1->w_status_height; 1824 win1->w_status_height = height; 1825 if (win1->w_prev->w_vsep_width == 1) 1826 { 1827 // Remove the vertical separator from the last-but-one window, 1828 // add it to the last window. Adjust the frame widths. 1829 win1->w_prev->w_vsep_width = 0; 1830 win1->w_prev->w_frame->fr_width -= 1; 1831 win1->w_vsep_width = 1; 1832 win1->w_frame->fr_width += 1; 1833 } 1834 } 1835 else if (win2 == lastwin) 1836 { 1837 height = win1->w_status_height; 1838 win1->w_status_height = win2->w_status_height; 1839 win2->w_status_height = height; 1840 if (win1->w_vsep_width == 1) 1841 { 1842 // Remove the vertical separator from win1, add it to the last 1843 // window, win2. Adjust the frame widths. 1844 win2->w_vsep_width = 1; 1845 win2->w_frame->fr_width += 1; 1846 win1->w_vsep_width = 0; 1847 win1->w_frame->fr_width -= 1; 1848 } 1849 } 1850 win_remove(win1, NULL); 1851 frame_remove(win1->w_frame); 1852 win_append(win2, win1); 1853 frame_append(win2->w_frame, win1->w_frame); 1854 1855 (void)win_comp_pos(); // recompute w_winrow for all windows 1856 redraw_later(NOT_VALID); 1857 } 1858 win_enter(win1, FALSE); 1859 } 1860 1861 /* 1862 * Make all windows the same height. 1863 * 'next_curwin' will soon be the current window, make sure it has enough 1864 * rows. 1865 */ 1866 void 1867 win_equal( 1868 win_T *next_curwin, // pointer to current window to be or NULL 1869 int current, // do only frame with current window 1870 int dir) // 'v' for vertically, 'h' for horizontally, 1871 // 'b' for both, 0 for using p_ead 1872 { 1873 if (dir == 0) 1874 dir = *p_ead; 1875 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current, 1876 topframe, dir, 0, tabline_height(), 1877 (int)Columns, topframe->fr_height); 1878 } 1879 1880 /* 1881 * Set a frame to a new position and height, spreading the available room 1882 * equally over contained frames. 1883 * The window "next_curwin" (if not NULL) should at least get the size from 1884 * 'winheight' and 'winwidth' if possible. 1885 */ 1886 static void 1887 win_equal_rec( 1888 win_T *next_curwin, // pointer to current window to be or NULL 1889 int current, // do only frame with current window 1890 frame_T *topfr, // frame to set size off 1891 int dir, // 'v', 'h' or 'b', see win_equal() 1892 int col, // horizontal position for frame 1893 int row, // vertical position for frame 1894 int width, // new width of frame 1895 int height) // new height of frame 1896 { 1897 int n, m; 1898 int extra_sep = 0; 1899 int wincount, totwincount = 0; 1900 frame_T *fr; 1901 int next_curwin_size = 0; 1902 int room = 0; 1903 int new_size; 1904 int has_next_curwin = 0; 1905 int hnc; 1906 1907 if (topfr->fr_layout == FR_LEAF) 1908 { 1909 // Set the width/height of this frame. 1910 // Redraw when size or position changes 1911 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row 1912 || topfr->fr_width != width || topfr->fr_win->w_wincol != col 1913 ) 1914 { 1915 topfr->fr_win->w_winrow = row; 1916 frame_new_height(topfr, height, FALSE, FALSE); 1917 topfr->fr_win->w_wincol = col; 1918 frame_new_width(topfr, width, FALSE, FALSE); 1919 redraw_all_later(NOT_VALID); 1920 } 1921 } 1922 else if (topfr->fr_layout == FR_ROW) 1923 { 1924 topfr->fr_width = width; 1925 topfr->fr_height = height; 1926 1927 if (dir != 'v') // equalize frame widths 1928 { 1929 // Compute the maximum number of windows horizontally in this 1930 // frame. 1931 n = frame_minwidth(topfr, NOWIN); 1932 // add one for the rightmost window, it doesn't have a separator 1933 if (col + width == Columns) 1934 extra_sep = 1; 1935 else 1936 extra_sep = 0; 1937 totwincount = (n + extra_sep) / (p_wmw + 1); 1938 has_next_curwin = frame_has_win(topfr, next_curwin); 1939 1940 /* 1941 * Compute width for "next_curwin" window and room available for 1942 * other windows. 1943 * "m" is the minimal width when counting p_wiw for "next_curwin". 1944 */ 1945 m = frame_minwidth(topfr, next_curwin); 1946 room = width - m; 1947 if (room < 0) 1948 { 1949 next_curwin_size = p_wiw + room; 1950 room = 0; 1951 } 1952 else 1953 { 1954 next_curwin_size = -1; 1955 FOR_ALL_FRAMES(fr, topfr->fr_child) 1956 { 1957 // If 'winfixwidth' set keep the window width if 1958 // possible. 1959 // Watch out for this window being the next_curwin. 1960 if (frame_fixed_width(fr)) 1961 { 1962 n = frame_minwidth(fr, NOWIN); 1963 new_size = fr->fr_width; 1964 if (frame_has_win(fr, next_curwin)) 1965 { 1966 room += p_wiw - p_wmw; 1967 next_curwin_size = 0; 1968 if (new_size < p_wiw) 1969 new_size = p_wiw; 1970 } 1971 else 1972 // These windows don't use up room. 1973 totwincount -= (n + (fr->fr_next == NULL 1974 ? extra_sep : 0)) / (p_wmw + 1); 1975 room -= new_size - n; 1976 if (room < 0) 1977 { 1978 new_size += room; 1979 room = 0; 1980 } 1981 fr->fr_newwidth = new_size; 1982 } 1983 } 1984 if (next_curwin_size == -1) 1985 { 1986 if (!has_next_curwin) 1987 next_curwin_size = 0; 1988 else if (totwincount > 1 1989 && (room + (totwincount - 2)) 1990 / (totwincount - 1) > p_wiw) 1991 { 1992 // Can make all windows wider than 'winwidth', spread 1993 // the room equally. 1994 next_curwin_size = (room + p_wiw 1995 + (totwincount - 1) * p_wmw 1996 + (totwincount - 1)) / totwincount; 1997 room -= next_curwin_size - p_wiw; 1998 } 1999 else 2000 next_curwin_size = p_wiw; 2001 } 2002 } 2003 2004 if (has_next_curwin) 2005 --totwincount; // don't count curwin 2006 } 2007 2008 FOR_ALL_FRAMES(fr, topfr->fr_child) 2009 { 2010 wincount = 1; 2011 if (fr->fr_next == NULL) 2012 // last frame gets all that remains (avoid roundoff error) 2013 new_size = width; 2014 else if (dir == 'v') 2015 new_size = fr->fr_width; 2016 else if (frame_fixed_width(fr)) 2017 { 2018 new_size = fr->fr_newwidth; 2019 wincount = 0; // doesn't count as a sizeable window 2020 } 2021 else 2022 { 2023 // Compute the maximum number of windows horiz. in "fr". 2024 n = frame_minwidth(fr, NOWIN); 2025 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0)) 2026 / (p_wmw + 1); 2027 m = frame_minwidth(fr, next_curwin); 2028 if (has_next_curwin) 2029 hnc = frame_has_win(fr, next_curwin); 2030 else 2031 hnc = FALSE; 2032 if (hnc) // don't count next_curwin 2033 --wincount; 2034 if (totwincount == 0) 2035 new_size = room; 2036 else 2037 new_size = (wincount * room + ((unsigned)totwincount >> 1)) 2038 / totwincount; 2039 if (hnc) // add next_curwin size 2040 { 2041 next_curwin_size -= p_wiw - (m - n); 2042 new_size += next_curwin_size; 2043 room -= new_size - next_curwin_size; 2044 } 2045 else 2046 room -= new_size; 2047 new_size += n; 2048 } 2049 2050 // Skip frame that is full width when splitting or closing a 2051 // window, unless equalizing all frames. 2052 if (!current || dir != 'v' || topfr->fr_parent != NULL 2053 || (new_size != fr->fr_width) 2054 || frame_has_win(fr, next_curwin)) 2055 win_equal_rec(next_curwin, current, fr, dir, col, row, 2056 new_size, height); 2057 col += new_size; 2058 width -= new_size; 2059 totwincount -= wincount; 2060 } 2061 } 2062 else // topfr->fr_layout == FR_COL 2063 { 2064 topfr->fr_width = width; 2065 topfr->fr_height = height; 2066 2067 if (dir != 'h') // equalize frame heights 2068 { 2069 // Compute maximum number of windows vertically in this frame. 2070 n = frame_minheight(topfr, NOWIN); 2071 // add one for the bottom window if it doesn't have a statusline 2072 if (row + height == cmdline_row && p_ls == 0) 2073 extra_sep = 1; 2074 else 2075 extra_sep = 0; 2076 totwincount = (n + extra_sep) / (p_wmh + 1); 2077 has_next_curwin = frame_has_win(topfr, next_curwin); 2078 2079 /* 2080 * Compute height for "next_curwin" window and room available for 2081 * other windows. 2082 * "m" is the minimal height when counting p_wh for "next_curwin". 2083 */ 2084 m = frame_minheight(topfr, next_curwin); 2085 room = height - m; 2086 if (room < 0) 2087 { 2088 // The room is less then 'winheight', use all space for the 2089 // current window. 2090 next_curwin_size = p_wh + room; 2091 room = 0; 2092 } 2093 else 2094 { 2095 next_curwin_size = -1; 2096 FOR_ALL_FRAMES(fr, topfr->fr_child) 2097 { 2098 // If 'winfixheight' set keep the window height if 2099 // possible. 2100 // Watch out for this window being the next_curwin. 2101 if (frame_fixed_height(fr)) 2102 { 2103 n = frame_minheight(fr, NOWIN); 2104 new_size = fr->fr_height; 2105 if (frame_has_win(fr, next_curwin)) 2106 { 2107 room += p_wh - p_wmh; 2108 next_curwin_size = 0; 2109 if (new_size < p_wh) 2110 new_size = p_wh; 2111 } 2112 else 2113 // These windows don't use up room. 2114 totwincount -= (n + (fr->fr_next == NULL 2115 ? extra_sep : 0)) / (p_wmh + 1); 2116 room -= new_size - n; 2117 if (room < 0) 2118 { 2119 new_size += room; 2120 room = 0; 2121 } 2122 fr->fr_newheight = new_size; 2123 } 2124 } 2125 if (next_curwin_size == -1) 2126 { 2127 if (!has_next_curwin) 2128 next_curwin_size = 0; 2129 else if (totwincount > 1 2130 && (room + (totwincount - 2)) 2131 / (totwincount - 1) > p_wh) 2132 { 2133 // can make all windows higher than 'winheight', 2134 // spread the room equally. 2135 next_curwin_size = (room + p_wh 2136 + (totwincount - 1) * p_wmh 2137 + (totwincount - 1)) / totwincount; 2138 room -= next_curwin_size - p_wh; 2139 } 2140 else 2141 next_curwin_size = p_wh; 2142 } 2143 } 2144 2145 if (has_next_curwin) 2146 --totwincount; // don't count curwin 2147 } 2148 2149 FOR_ALL_FRAMES(fr, topfr->fr_child) 2150 { 2151 wincount = 1; 2152 if (fr->fr_next == NULL) 2153 // last frame gets all that remains (avoid roundoff error) 2154 new_size = height; 2155 else if (dir == 'h') 2156 new_size = fr->fr_height; 2157 else if (frame_fixed_height(fr)) 2158 { 2159 new_size = fr->fr_newheight; 2160 wincount = 0; // doesn't count as a sizeable window 2161 } 2162 else 2163 { 2164 // Compute the maximum number of windows vert. in "fr". 2165 n = frame_minheight(fr, NOWIN); 2166 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0)) 2167 / (p_wmh + 1); 2168 m = frame_minheight(fr, next_curwin); 2169 if (has_next_curwin) 2170 hnc = frame_has_win(fr, next_curwin); 2171 else 2172 hnc = FALSE; 2173 if (hnc) // don't count next_curwin 2174 --wincount; 2175 if (totwincount == 0) 2176 new_size = room; 2177 else 2178 new_size = (wincount * room + ((unsigned)totwincount >> 1)) 2179 / totwincount; 2180 if (hnc) // add next_curwin size 2181 { 2182 next_curwin_size -= p_wh - (m - n); 2183 new_size += next_curwin_size; 2184 room -= new_size - next_curwin_size; 2185 } 2186 else 2187 room -= new_size; 2188 new_size += n; 2189 } 2190 // Skip frame that is full width when splitting or closing a 2191 // window, unless equalizing all frames. 2192 if (!current || dir != 'h' || topfr->fr_parent != NULL 2193 || (new_size != fr->fr_height) 2194 || frame_has_win(fr, next_curwin)) 2195 win_equal_rec(next_curwin, current, fr, dir, col, row, 2196 width, new_size); 2197 row += new_size; 2198 height -= new_size; 2199 totwincount -= wincount; 2200 } 2201 } 2202 } 2203 2204 #ifdef FEAT_JOB_CHANNEL 2205 static void 2206 leaving_window(win_T *win) 2207 { 2208 // Only matters for a prompt window. 2209 if (!bt_prompt(win->w_buffer)) 2210 return; 2211 2212 // When leaving a prompt window stop Insert mode and perhaps restart 2213 // it when entering that window again. 2214 win->w_buffer->b_prompt_insert = restart_edit; 2215 if (restart_edit != 0 && mode_displayed) 2216 clear_cmdline = TRUE; // unshow mode later 2217 restart_edit = NUL; 2218 2219 // When leaving the window (or closing the window) was done from a 2220 // callback we need to break out of the Insert mode loop and restart Insert 2221 // mode when entering the window again. 2222 if (State & INSERT) 2223 { 2224 stop_insert_mode = TRUE; 2225 if (win->w_buffer->b_prompt_insert == NUL) 2226 win->w_buffer->b_prompt_insert = 'A'; 2227 } 2228 } 2229 2230 void 2231 entering_window(win_T *win) 2232 { 2233 // Only matters for a prompt window. 2234 if (!bt_prompt(win->w_buffer)) 2235 return; 2236 2237 // When switching to a prompt buffer that was in Insert mode, don't stop 2238 // Insert mode, it may have been set in leaving_window(). 2239 if (win->w_buffer->b_prompt_insert != NUL) 2240 stop_insert_mode = FALSE; 2241 2242 // When entering the prompt window restart Insert mode if we were in Insert 2243 // mode when we left it. 2244 restart_edit = win->w_buffer->b_prompt_insert; 2245 } 2246 #endif 2247 2248 /* 2249 * Close all windows for buffer "buf". 2250 */ 2251 void 2252 close_windows( 2253 buf_T *buf, 2254 int keep_curwin) // don't close "curwin" 2255 { 2256 win_T *wp; 2257 tabpage_T *tp, *nexttp; 2258 int h = tabline_height(); 2259 int count = tabpage_index(NULL); 2260 2261 ++RedrawingDisabled; 2262 2263 for (wp = firstwin; wp != NULL && !ONE_WINDOW; ) 2264 { 2265 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin) 2266 && !(wp->w_closing || wp->w_buffer->b_locked > 0)) 2267 { 2268 if (win_close(wp, FALSE) == FAIL) 2269 // If closing the window fails give up, to avoid looping 2270 // forever. 2271 break; 2272 2273 // Start all over, autocommands may change the window layout. 2274 wp = firstwin; 2275 } 2276 else 2277 wp = wp->w_next; 2278 } 2279 2280 // Also check windows in other tab pages. 2281 for (tp = first_tabpage; tp != NULL; tp = nexttp) 2282 { 2283 nexttp = tp->tp_next; 2284 if (tp != curtab) 2285 FOR_ALL_WINDOWS_IN_TAB(tp, wp) 2286 if (wp->w_buffer == buf 2287 && !(wp->w_closing || wp->w_buffer->b_locked > 0)) 2288 { 2289 win_close_othertab(wp, FALSE, tp); 2290 2291 // Start all over, the tab page may be closed and 2292 // autocommands may change the window layout. 2293 nexttp = first_tabpage; 2294 break; 2295 } 2296 } 2297 2298 --RedrawingDisabled; 2299 2300 if (count != tabpage_index(NULL)) 2301 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf); 2302 2303 redraw_tabline = TRUE; 2304 if (h != tabline_height()) 2305 shell_new_rows(); 2306 } 2307 2308 /* 2309 * Return TRUE if the current window is the only window that exists (ignoring 2310 * "aucmd_win"). 2311 * Returns FALSE if there is a window, possibly in another tab page. 2312 */ 2313 static int 2314 last_window(void) 2315 { 2316 return (one_window() && first_tabpage->tp_next == NULL); 2317 } 2318 2319 /* 2320 * Return TRUE if there is only one window other than "aucmd_win" in the 2321 * current tab page. 2322 */ 2323 int 2324 one_window(void) 2325 { 2326 win_T *wp; 2327 int seen_one = FALSE; 2328 2329 FOR_ALL_WINDOWS(wp) 2330 { 2331 if (wp != aucmd_win) 2332 { 2333 if (seen_one) 2334 return FALSE; 2335 seen_one = TRUE; 2336 } 2337 } 2338 return TRUE; 2339 } 2340 2341 /* 2342 * Close the possibly last window in a tab page. 2343 * Returns TRUE when the window was closed already. 2344 */ 2345 static int 2346 close_last_window_tabpage( 2347 win_T *win, 2348 int free_buf, 2349 tabpage_T *prev_curtab) 2350 { 2351 if (ONE_WINDOW) 2352 { 2353 buf_T *old_curbuf = curbuf; 2354 2355 /* 2356 * Closing the last window in a tab page. First go to another tab 2357 * page and then close the window and the tab page. This avoids that 2358 * curwin and curtab are invalid while we are freeing memory, they may 2359 * be used in GUI events. 2360 * Don't trigger autocommands yet, they may use wrong values, so do 2361 * that below. 2362 */ 2363 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE); 2364 redraw_tabline = TRUE; 2365 2366 // Safety check: Autocommands may have closed the window when jumping 2367 // to the other tab page. 2368 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win) 2369 { 2370 int h = tabline_height(); 2371 2372 win_close_othertab(win, free_buf, prev_curtab); 2373 if (h != tabline_height()) 2374 shell_new_rows(); 2375 } 2376 #ifdef FEAT_JOB_CHANNEL 2377 entering_window(curwin); 2378 #endif 2379 // Since goto_tabpage_tp above did not trigger *Enter autocommands, do 2380 // that now. 2381 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf); 2382 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf); 2383 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf); 2384 if (old_curbuf != curbuf) 2385 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); 2386 return TRUE; 2387 } 2388 return FALSE; 2389 } 2390 2391 /* 2392 * Close the buffer of "win" and unload it if "action" is DOBUF_UNLOAD. 2393 * "action" can also be zero (do nothing) or DOBUF_WIPE. 2394 * "abort_if_last" is passed to close_buffer(): abort closing if all other 2395 * windows are closed. 2396 */ 2397 static void 2398 win_close_buffer(win_T *win, int action, int abort_if_last) 2399 { 2400 #ifdef FEAT_SYN_HL 2401 // Free independent synblock before the buffer is freed. 2402 if (win->w_buffer != NULL) 2403 reset_synblock(win); 2404 #endif 2405 2406 #ifdef FEAT_QUICKFIX 2407 // When the quickfix/location list window is closed, unlist the buffer. 2408 if (win->w_buffer != NULL && bt_quickfix(win->w_buffer)) 2409 win->w_buffer->b_p_bl = FALSE; 2410 #endif 2411 2412 // Close the link to the buffer. 2413 if (win->w_buffer != NULL) 2414 { 2415 bufref_T bufref; 2416 2417 set_bufref(&bufref, curbuf); 2418 win->w_closing = TRUE; 2419 close_buffer(win, win->w_buffer, action, abort_if_last, FALSE); 2420 if (win_valid_any_tab(win)) 2421 win->w_closing = FALSE; 2422 // Make sure curbuf is valid. It can become invalid if 'bufhidden' is 2423 // "wipe". 2424 if (!bufref_valid(&bufref)) 2425 curbuf = firstbuf; 2426 } 2427 } 2428 2429 /* 2430 * Close window "win". Only works for the current tab page. 2431 * If "free_buf" is TRUE related buffer may be unloaded. 2432 * 2433 * Called by :quit, :close, :xit, :wq and findtag(). 2434 * Returns FAIL when the window was not closed. 2435 */ 2436 int 2437 win_close(win_T *win, int free_buf) 2438 { 2439 win_T *wp; 2440 int other_buffer = FALSE; 2441 int close_curwin = FALSE; 2442 int dir; 2443 int help_window = FALSE; 2444 tabpage_T *prev_curtab = curtab; 2445 frame_T *win_frame = win->w_frame->fr_parent; 2446 #ifdef FEAT_DIFF 2447 int had_diffmode = win->w_p_diff; 2448 #endif 2449 2450 #if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP) 2451 // Can close a popup window with a terminal if the job has finished. 2452 if (may_close_term_popup() == OK) 2453 return OK; 2454 #endif 2455 if (ERROR_IF_ANY_POPUP_WINDOW) 2456 return FAIL; 2457 2458 if (last_window()) 2459 { 2460 emsg(_("E444: Cannot close last window")); 2461 return FAIL; 2462 } 2463 2464 if (win->w_closing || (win->w_buffer != NULL 2465 && win->w_buffer->b_locked > 0)) 2466 return FAIL; // window is already being closed 2467 if (win_unlisted(win)) 2468 { 2469 emsg(_(e_autocmd_close)); 2470 return FAIL; 2471 } 2472 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window()) 2473 { 2474 emsg(_("E814: Cannot close window, only autocmd window would remain")); 2475 return FAIL; 2476 } 2477 2478 // When closing the last window in a tab page first go to another tab page 2479 // and then close the window and the tab page to avoid that curwin and 2480 // curtab are invalid while we are freeing memory. 2481 if (close_last_window_tabpage(win, free_buf, prev_curtab)) 2482 return FAIL; 2483 2484 // When closing the help window, try restoring a snapshot after closing 2485 // the window. Otherwise clear the snapshot, it's now invalid. 2486 if (bt_help(win->w_buffer)) 2487 help_window = TRUE; 2488 else 2489 clear_snapshot(curtab, SNAP_HELP_IDX); 2490 2491 if (win == curwin) 2492 { 2493 #ifdef FEAT_JOB_CHANNEL 2494 leaving_window(curwin); 2495 #endif 2496 /* 2497 * Guess which window is going to be the new current window. 2498 * This may change because of the autocommands (sigh). 2499 */ 2500 wp = frame2win(win_altframe(win, NULL)); 2501 2502 /* 2503 * Be careful: If autocommands delete the window or cause this window 2504 * to be the last one left, return now. 2505 */ 2506 if (wp->w_buffer != curbuf) 2507 { 2508 other_buffer = TRUE; 2509 win->w_closing = TRUE; 2510 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); 2511 if (!win_valid(win)) 2512 return FAIL; 2513 win->w_closing = FALSE; 2514 if (last_window()) 2515 return FAIL; 2516 } 2517 win->w_closing = TRUE; 2518 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf); 2519 if (!win_valid(win)) 2520 return FAIL; 2521 win->w_closing = FALSE; 2522 if (last_window()) 2523 return FAIL; 2524 #ifdef FEAT_EVAL 2525 // autocmds may abort script processing 2526 if (aborting()) 2527 return FAIL; 2528 #endif 2529 } 2530 2531 #ifdef FEAT_GUI 2532 // Avoid trouble with scrollbars that are going to be deleted in 2533 // win_free(). 2534 if (gui.in_use) 2535 out_flush(); 2536 #endif 2537 2538 #ifdef FEAT_PROP_POPUP 2539 if (popup_win_closed(win) && !win_valid(win)) 2540 return FAIL; 2541 #endif 2542 win_close_buffer(win, free_buf ? DOBUF_UNLOAD : 0, TRUE); 2543 2544 if (only_one_window() && win_valid(win) && win->w_buffer == NULL 2545 && (last_window() || curtab != prev_curtab 2546 || close_last_window_tabpage(win, free_buf, prev_curtab))) 2547 { 2548 // Autocommands have closed all windows, quit now. Restore 2549 // curwin->w_buffer, otherwise writing viminfo may fail. 2550 if (curwin->w_buffer == NULL) 2551 curwin->w_buffer = curbuf; 2552 getout(0); 2553 } 2554 2555 // Autocommands may have moved to another tab page. 2556 if (curtab != prev_curtab && win_valid_any_tab(win) 2557 && win->w_buffer == NULL) 2558 { 2559 // Need to close the window anyway, since the buffer is NULL. 2560 win_close_othertab(win, FALSE, prev_curtab); 2561 return FAIL; 2562 } 2563 2564 // Autocommands may have closed the window already or closed the only 2565 // other window. 2566 if (!win_valid(win) || last_window() 2567 || close_last_window_tabpage(win, free_buf, prev_curtab)) 2568 return FAIL; 2569 2570 // Now we are really going to close the window. Disallow any autocommand 2571 // to split a window to avoid trouble. 2572 ++split_disallowed; 2573 2574 // Free the memory used for the window and get the window that received 2575 // the screen space. 2576 wp = win_free_mem(win, &dir, NULL); 2577 2578 // Make sure curwin isn't invalid. It can cause severe trouble when 2579 // printing an error message. For win_equal() curbuf needs to be valid 2580 // too. 2581 if (win == curwin) 2582 { 2583 curwin = wp; 2584 #ifdef FEAT_QUICKFIX 2585 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer)) 2586 { 2587 /* 2588 * If the cursor goes to the preview or the quickfix window, try 2589 * finding another window to go to. 2590 */ 2591 for (;;) 2592 { 2593 if (wp->w_next == NULL) 2594 wp = firstwin; 2595 else 2596 wp = wp->w_next; 2597 if (wp == curwin) 2598 break; 2599 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer)) 2600 { 2601 curwin = wp; 2602 break; 2603 } 2604 } 2605 } 2606 #endif 2607 curbuf = curwin->w_buffer; 2608 close_curwin = TRUE; 2609 2610 // The cursor position may be invalid if the buffer changed after last 2611 // using the window. 2612 check_cursor(); 2613 } 2614 if (p_ea && (*p_ead == 'b' || *p_ead == dir)) 2615 // If the frame of the closed window contains the new current window, 2616 // only resize that frame. Otherwise resize all windows. 2617 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir); 2618 else 2619 win_comp_pos(); 2620 if (close_curwin) 2621 { 2622 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE); 2623 if (other_buffer) 2624 // careful: after this wp and win may be invalid! 2625 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); 2626 } 2627 2628 --split_disallowed; 2629 2630 /* 2631 * If last window has a status line now and we don't want one, 2632 * remove the status line. 2633 */ 2634 last_status(FALSE); 2635 2636 // After closing the help window, try restoring the window layout from 2637 // before it was opened. 2638 if (help_window) 2639 restore_snapshot(SNAP_HELP_IDX, close_curwin); 2640 2641 #ifdef FEAT_DIFF 2642 // If the window had 'diff' set and now there is only one window left in 2643 // the tab page with 'diff' set, and "closeoff" is in 'diffopt', then 2644 // execute ":diffoff!". 2645 if (diffopt_closeoff() && had_diffmode && curtab == prev_curtab) 2646 { 2647 int diffcount = 0; 2648 win_T *dwin; 2649 2650 FOR_ALL_WINDOWS(dwin) 2651 if (dwin->w_p_diff) 2652 ++diffcount; 2653 if (diffcount == 1) 2654 do_cmdline_cmd((char_u *)"diffoff!"); 2655 } 2656 #endif 2657 2658 #if defined(FEAT_GUI) 2659 // When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. 2660 if (gui.in_use && !win_hasvertsplit()) 2661 gui_init_which_components(NULL); 2662 #endif 2663 2664 redraw_all_later(NOT_VALID); 2665 return OK; 2666 } 2667 2668 /* 2669 * Close window "win" in tab page "tp", which is not the current tab page. 2670 * This may be the last window in that tab page and result in closing the tab, 2671 * thus "tp" may become invalid! 2672 * Caller must check if buffer is hidden and whether the tabline needs to be 2673 * updated. 2674 */ 2675 void 2676 win_close_othertab(win_T *win, int free_buf, tabpage_T *tp) 2677 { 2678 win_T *wp; 2679 int dir; 2680 tabpage_T *ptp = NULL; 2681 int free_tp = FALSE; 2682 2683 // Get here with win->w_buffer == NULL when win_close() detects the tab 2684 // page changed. 2685 if (win->w_closing || (win->w_buffer != NULL 2686 && win->w_buffer->b_locked > 0)) 2687 return; // window is already being closed 2688 2689 if (win->w_buffer != NULL) 2690 // Close the link to the buffer. 2691 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, 2692 FALSE, FALSE); 2693 2694 // Careful: Autocommands may have closed the tab page or made it the 2695 // current tab page. 2696 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next) 2697 ; 2698 if (ptp == NULL || tp == curtab) 2699 return; 2700 2701 // Autocommands may have closed the window already. 2702 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next) 2703 ; 2704 if (wp == NULL) 2705 return; 2706 2707 // When closing the last window in a tab page remove the tab page. 2708 if (tp->tp_firstwin == tp->tp_lastwin) 2709 { 2710 if (tp == first_tabpage) 2711 first_tabpage = tp->tp_next; 2712 else 2713 { 2714 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp; 2715 ptp = ptp->tp_next) 2716 ; 2717 if (ptp == NULL) 2718 { 2719 internal_error("win_close_othertab()"); 2720 return; 2721 } 2722 ptp->tp_next = tp->tp_next; 2723 } 2724 free_tp = TRUE; 2725 } 2726 2727 // Free the memory used for the window. 2728 win_free_mem(win, &dir, tp); 2729 2730 if (free_tp) 2731 free_tabpage(tp); 2732 } 2733 2734 /* 2735 * Free the memory used for a window. 2736 * Returns a pointer to the window that got the freed up space. 2737 */ 2738 static win_T * 2739 win_free_mem( 2740 win_T *win, 2741 int *dirp, // set to 'v' or 'h' for direction if 'ea' 2742 tabpage_T *tp) // tab page "win" is in, NULL for current 2743 { 2744 frame_T *frp; 2745 win_T *wp; 2746 tabpage_T *win_tp = tp == NULL ? curtab : tp; 2747 2748 // Remove the window and its frame from the tree of frames. 2749 frp = win->w_frame; 2750 wp = winframe_remove(win, dirp, tp); 2751 vim_free(frp); 2752 win_free(win, tp); 2753 2754 // When deleting the current window in the tab, select a new current 2755 // window. 2756 if (win == win_tp->tp_curwin) 2757 win_tp->tp_curwin = wp; 2758 2759 return wp; 2760 } 2761 2762 #if defined(EXITFREE) || defined(PROTO) 2763 void 2764 win_free_all(void) 2765 { 2766 int dummy; 2767 2768 while (first_tabpage->tp_next != NULL) 2769 tabpage_close(TRUE); 2770 2771 if (aucmd_win != NULL) 2772 { 2773 (void)win_free_mem(aucmd_win, &dummy, NULL); 2774 aucmd_win = NULL; 2775 } 2776 2777 while (firstwin != NULL) 2778 (void)win_free_mem(firstwin, &dummy, NULL); 2779 2780 // No window should be used after this. Set curwin to NULL to crash 2781 // instead of using freed memory. 2782 curwin = NULL; 2783 } 2784 #endif 2785 2786 /* 2787 * Remove a window and its frame from the tree of frames. 2788 * Returns a pointer to the window that got the freed up space. 2789 */ 2790 win_T * 2791 winframe_remove( 2792 win_T *win, 2793 int *dirp UNUSED, // set to 'v' or 'h' for direction if 'ea' 2794 tabpage_T *tp) // tab page "win" is in, NULL for current 2795 { 2796 frame_T *frp, *frp2, *frp3; 2797 frame_T *frp_close = win->w_frame; 2798 win_T *wp; 2799 2800 /* 2801 * If there is only one window there is nothing to remove. 2802 */ 2803 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin) 2804 return NULL; 2805 2806 /* 2807 * Remove the window from its frame. 2808 */ 2809 frp2 = win_altframe(win, tp); 2810 wp = frame2win(frp2); 2811 2812 // Remove this frame from the list of frames. 2813 frame_remove(frp_close); 2814 2815 if (frp_close->fr_parent->fr_layout == FR_COL) 2816 { 2817 // When 'winfixheight' is set, try to find another frame in the column 2818 // (as close to the closed frame as possible) to distribute the height 2819 // to. 2820 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh) 2821 { 2822 frp = frp_close->fr_prev; 2823 frp3 = frp_close->fr_next; 2824 while (frp != NULL || frp3 != NULL) 2825 { 2826 if (frp != NULL) 2827 { 2828 if (!frame_fixed_height(frp)) 2829 { 2830 frp2 = frp; 2831 wp = frame2win(frp2); 2832 break; 2833 } 2834 frp = frp->fr_prev; 2835 } 2836 if (frp3 != NULL) 2837 { 2838 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh) 2839 { 2840 frp2 = frp3; 2841 wp = frp3->fr_win; 2842 break; 2843 } 2844 frp3 = frp3->fr_next; 2845 } 2846 } 2847 } 2848 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height, 2849 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE); 2850 *dirp = 'v'; 2851 } 2852 else 2853 { 2854 // When 'winfixwidth' is set, try to find another frame in the column 2855 // (as close to the closed frame as possible) to distribute the width 2856 // to. 2857 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw) 2858 { 2859 frp = frp_close->fr_prev; 2860 frp3 = frp_close->fr_next; 2861 while (frp != NULL || frp3 != NULL) 2862 { 2863 if (frp != NULL) 2864 { 2865 if (!frame_fixed_width(frp)) 2866 { 2867 frp2 = frp; 2868 wp = frame2win(frp2); 2869 break; 2870 } 2871 frp = frp->fr_prev; 2872 } 2873 if (frp3 != NULL) 2874 { 2875 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw) 2876 { 2877 frp2 = frp3; 2878 wp = frp3->fr_win; 2879 break; 2880 } 2881 frp3 = frp3->fr_next; 2882 } 2883 } 2884 } 2885 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width, 2886 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE); 2887 *dirp = 'h'; 2888 } 2889 2890 // If rows/columns go to a window below/right its positions need to be 2891 // updated. Can only be done after the sizes have been updated. 2892 if (frp2 == frp_close->fr_next) 2893 { 2894 int row = win->w_winrow; 2895 int col = win->w_wincol; 2896 2897 frame_comp_pos(frp2, &row, &col); 2898 } 2899 2900 if (frp2->fr_next == NULL && frp2->fr_prev == NULL) 2901 { 2902 // There is no other frame in this list, move its info to the parent 2903 // and remove it. 2904 frp2->fr_parent->fr_layout = frp2->fr_layout; 2905 frp2->fr_parent->fr_child = frp2->fr_child; 2906 FOR_ALL_FRAMES(frp, frp2->fr_child) 2907 frp->fr_parent = frp2->fr_parent; 2908 frp2->fr_parent->fr_win = frp2->fr_win; 2909 if (frp2->fr_win != NULL) 2910 frp2->fr_win->w_frame = frp2->fr_parent; 2911 frp = frp2->fr_parent; 2912 if (topframe->fr_child == frp2) 2913 topframe->fr_child = frp; 2914 vim_free(frp2); 2915 2916 frp2 = frp->fr_parent; 2917 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout) 2918 { 2919 // The frame above the parent has the same layout, have to merge 2920 // the frames into this list. 2921 if (frp2->fr_child == frp) 2922 frp2->fr_child = frp->fr_child; 2923 frp->fr_child->fr_prev = frp->fr_prev; 2924 if (frp->fr_prev != NULL) 2925 frp->fr_prev->fr_next = frp->fr_child; 2926 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next) 2927 { 2928 frp3->fr_parent = frp2; 2929 if (frp3->fr_next == NULL) 2930 { 2931 frp3->fr_next = frp->fr_next; 2932 if (frp->fr_next != NULL) 2933 frp->fr_next->fr_prev = frp3; 2934 break; 2935 } 2936 } 2937 if (topframe->fr_child == frp) 2938 topframe->fr_child = frp2; 2939 vim_free(frp); 2940 } 2941 } 2942 2943 return wp; 2944 } 2945 2946 /* 2947 * Return a pointer to the frame that will receive the empty screen space that 2948 * is left over after "win" is closed. 2949 * 2950 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left 2951 * by default. Otherwise, the free space goes below or to the right. The 2952 * result is that opening a window and then immediately closing it will 2953 * preserve the initial window layout. The 'wfh' and 'wfw' settings are 2954 * respected when possible. 2955 */ 2956 static frame_T * 2957 win_altframe( 2958 win_T *win, 2959 tabpage_T *tp) // tab page "win" is in, NULL for current 2960 { 2961 frame_T *frp; 2962 frame_T *other_fr, *target_fr; 2963 2964 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin) 2965 return alt_tabpage()->tp_curwin->w_frame; 2966 2967 frp = win->w_frame; 2968 2969 if (frp->fr_prev == NULL) 2970 return frp->fr_next; 2971 if (frp->fr_next == NULL) 2972 return frp->fr_prev; 2973 2974 // By default the next window will get the space that was abandoned by this 2975 // window 2976 target_fr = frp->fr_next; 2977 other_fr = frp->fr_prev; 2978 2979 // If this is part of a column of windows and 'splitbelow' is true then the 2980 // previous window will get the space. 2981 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_COL && p_sb) 2982 { 2983 target_fr = frp->fr_prev; 2984 other_fr = frp->fr_next; 2985 } 2986 2987 // If this is part of a row of windows, and 'splitright' is true then the 2988 // previous window will get the space. 2989 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW && p_spr) 2990 { 2991 target_fr = frp->fr_prev; 2992 other_fr = frp->fr_next; 2993 } 2994 2995 // If 'wfh' or 'wfw' is set for the target and not for the alternate 2996 // window, reverse the selection. 2997 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW) 2998 { 2999 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr)) 3000 target_fr = other_fr; 3001 } 3002 else 3003 { 3004 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr)) 3005 target_fr = other_fr; 3006 } 3007 3008 return target_fr; 3009 } 3010 3011 /* 3012 * Return the tabpage that will be used if the current one is closed. 3013 */ 3014 static tabpage_T * 3015 alt_tabpage(void) 3016 { 3017 tabpage_T *tp; 3018 3019 // Use the next tab page if possible. 3020 if (curtab->tp_next != NULL) 3021 return curtab->tp_next; 3022 3023 // Find the last but one tab page. 3024 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next) 3025 ; 3026 return tp; 3027 } 3028 3029 /* 3030 * Find the left-upper window in frame "frp". 3031 */ 3032 static win_T * 3033 frame2win(frame_T *frp) 3034 { 3035 while (frp->fr_win == NULL) 3036 frp = frp->fr_child; 3037 return frp->fr_win; 3038 } 3039 3040 /* 3041 * Return TRUE if frame "frp" contains window "wp". 3042 */ 3043 static int 3044 frame_has_win(frame_T *frp, win_T *wp) 3045 { 3046 frame_T *p; 3047 3048 if (frp->fr_layout == FR_LEAF) 3049 return frp->fr_win == wp; 3050 3051 FOR_ALL_FRAMES(p, frp->fr_child) 3052 if (frame_has_win(p, wp)) 3053 return TRUE; 3054 return FALSE; 3055 } 3056 3057 /* 3058 * Set a new height for a frame. Recursively sets the height for contained 3059 * frames and windows. Caller must take care of positions. 3060 */ 3061 static void 3062 frame_new_height( 3063 frame_T *topfrp, 3064 int height, 3065 int topfirst, // resize topmost contained frame first 3066 int wfh) // obey 'winfixheight' when there is a choice; 3067 // may cause the height not to be set 3068 { 3069 frame_T *frp; 3070 int extra_lines; 3071 int h; 3072 3073 if (topfrp->fr_win != NULL) 3074 { 3075 // Simple case: just one window. 3076 win_new_height(topfrp->fr_win, 3077 height - topfrp->fr_win->w_status_height 3078 - WINBAR_HEIGHT(topfrp->fr_win)); 3079 } 3080 else if (topfrp->fr_layout == FR_ROW) 3081 { 3082 do 3083 { 3084 // All frames in this row get the same new height. 3085 FOR_ALL_FRAMES(frp, topfrp->fr_child) 3086 { 3087 frame_new_height(frp, height, topfirst, wfh); 3088 if (frp->fr_height > height) 3089 { 3090 // Could not fit the windows, make the whole row higher. 3091 height = frp->fr_height; 3092 break; 3093 } 3094 } 3095 } 3096 while (frp != NULL); 3097 } 3098 else // fr_layout == FR_COL 3099 { 3100 // Complicated case: Resize a column of frames. Resize the bottom 3101 // frame first, frames above that when needed. 3102 3103 frp = topfrp->fr_child; 3104 if (wfh) 3105 // Advance past frames with one window with 'wfh' set. 3106 while (frame_fixed_height(frp)) 3107 { 3108 frp = frp->fr_next; 3109 if (frp == NULL) 3110 return; // no frame without 'wfh', give up 3111 } 3112 if (!topfirst) 3113 { 3114 // Find the bottom frame of this column 3115 while (frp->fr_next != NULL) 3116 frp = frp->fr_next; 3117 if (wfh) 3118 // Advance back for frames with one window with 'wfh' set. 3119 while (frame_fixed_height(frp)) 3120 frp = frp->fr_prev; 3121 } 3122 3123 extra_lines = height - topfrp->fr_height; 3124 if (extra_lines < 0) 3125 { 3126 // reduce height of contained frames, bottom or top frame first 3127 while (frp != NULL) 3128 { 3129 h = frame_minheight(frp, NULL); 3130 if (frp->fr_height + extra_lines < h) 3131 { 3132 extra_lines += frp->fr_height - h; 3133 frame_new_height(frp, h, topfirst, wfh); 3134 } 3135 else 3136 { 3137 frame_new_height(frp, frp->fr_height + extra_lines, 3138 topfirst, wfh); 3139 break; 3140 } 3141 if (topfirst) 3142 { 3143 do 3144 frp = frp->fr_next; 3145 while (wfh && frp != NULL && frame_fixed_height(frp)); 3146 } 3147 else 3148 { 3149 do 3150 frp = frp->fr_prev; 3151 while (wfh && frp != NULL && frame_fixed_height(frp)); 3152 } 3153 // Increase "height" if we could not reduce enough frames. 3154 if (frp == NULL) 3155 height -= extra_lines; 3156 } 3157 } 3158 else if (extra_lines > 0) 3159 { 3160 // increase height of bottom or top frame 3161 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh); 3162 } 3163 } 3164 topfrp->fr_height = height; 3165 } 3166 3167 /* 3168 * Return TRUE if height of frame "frp" should not be changed because of 3169 * the 'winfixheight' option. 3170 */ 3171 static int 3172 frame_fixed_height(frame_T *frp) 3173 { 3174 // frame with one window: fixed height if 'winfixheight' set. 3175 if (frp->fr_win != NULL) 3176 return frp->fr_win->w_p_wfh; 3177 3178 if (frp->fr_layout == FR_ROW) 3179 { 3180 // The frame is fixed height if one of the frames in the row is fixed 3181 // height. 3182 FOR_ALL_FRAMES(frp, frp->fr_child) 3183 if (frame_fixed_height(frp)) 3184 return TRUE; 3185 return FALSE; 3186 } 3187 3188 // frp->fr_layout == FR_COL: The frame is fixed height if all of the 3189 // frames in the row are fixed height. 3190 FOR_ALL_FRAMES(frp, frp->fr_child) 3191 if (!frame_fixed_height(frp)) 3192 return FALSE; 3193 return TRUE; 3194 } 3195 3196 /* 3197 * Return TRUE if width of frame "frp" should not be changed because of 3198 * the 'winfixwidth' option. 3199 */ 3200 static int 3201 frame_fixed_width(frame_T *frp) 3202 { 3203 // frame with one window: fixed width if 'winfixwidth' set. 3204 if (frp->fr_win != NULL) 3205 return frp->fr_win->w_p_wfw; 3206 3207 if (frp->fr_layout == FR_COL) 3208 { 3209 // The frame is fixed width if one of the frames in the row is fixed 3210 // width. 3211 FOR_ALL_FRAMES(frp, frp->fr_child) 3212 if (frame_fixed_width(frp)) 3213 return TRUE; 3214 return FALSE; 3215 } 3216 3217 // frp->fr_layout == FR_ROW: The frame is fixed width if all of the 3218 // frames in the row are fixed width. 3219 FOR_ALL_FRAMES(frp, frp->fr_child) 3220 if (!frame_fixed_width(frp)) 3221 return FALSE; 3222 return TRUE; 3223 } 3224 3225 /* 3226 * Add a status line to windows at the bottom of "frp". 3227 * Note: Does not check if there is room! 3228 */ 3229 static void 3230 frame_add_statusline(frame_T *frp) 3231 { 3232 win_T *wp; 3233 3234 if (frp->fr_layout == FR_LEAF) 3235 { 3236 wp = frp->fr_win; 3237 if (wp->w_status_height == 0) 3238 { 3239 if (wp->w_height > 0) // don't make it negative 3240 --wp->w_height; 3241 wp->w_status_height = STATUS_HEIGHT; 3242 } 3243 } 3244 else if (frp->fr_layout == FR_ROW) 3245 { 3246 // Handle all the frames in the row. 3247 FOR_ALL_FRAMES(frp, frp->fr_child) 3248 frame_add_statusline(frp); 3249 } 3250 else // frp->fr_layout == FR_COL 3251 { 3252 // Only need to handle the last frame in the column. 3253 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next) 3254 ; 3255 frame_add_statusline(frp); 3256 } 3257 } 3258 3259 /* 3260 * Set width of a frame. Handles recursively going through contained frames. 3261 * May remove separator line for windows at the right side (for win_close()). 3262 */ 3263 static void 3264 frame_new_width( 3265 frame_T *topfrp, 3266 int width, 3267 int leftfirst, // resize leftmost contained frame first 3268 int wfw) // obey 'winfixwidth' when there is a choice; 3269 // may cause the width not to be set 3270 { 3271 frame_T *frp; 3272 int extra_cols; 3273 int w; 3274 win_T *wp; 3275 3276 if (topfrp->fr_layout == FR_LEAF) 3277 { 3278 // Simple case: just one window. 3279 wp = topfrp->fr_win; 3280 // Find out if there are any windows right of this one. 3281 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent) 3282 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL) 3283 break; 3284 if (frp->fr_parent == NULL) 3285 wp->w_vsep_width = 0; 3286 win_new_width(wp, width - wp->w_vsep_width); 3287 } 3288 else if (topfrp->fr_layout == FR_COL) 3289 { 3290 do 3291 { 3292 // All frames in this column get the same new width. 3293 FOR_ALL_FRAMES(frp, topfrp->fr_child) 3294 { 3295 frame_new_width(frp, width, leftfirst, wfw); 3296 if (frp->fr_width > width) 3297 { 3298 // Could not fit the windows, make whole column wider. 3299 width = frp->fr_width; 3300 break; 3301 } 3302 } 3303 } while (frp != NULL); 3304 } 3305 else // fr_layout == FR_ROW 3306 { 3307 // Complicated case: Resize a row of frames. Resize the rightmost 3308 // frame first, frames left of it when needed. 3309 3310 frp = topfrp->fr_child; 3311 if (wfw) 3312 // Advance past frames with one window with 'wfw' set. 3313 while (frame_fixed_width(frp)) 3314 { 3315 frp = frp->fr_next; 3316 if (frp == NULL) 3317 return; // no frame without 'wfw', give up 3318 } 3319 if (!leftfirst) 3320 { 3321 // Find the rightmost frame of this row 3322 while (frp->fr_next != NULL) 3323 frp = frp->fr_next; 3324 if (wfw) 3325 // Advance back for frames with one window with 'wfw' set. 3326 while (frame_fixed_width(frp)) 3327 frp = frp->fr_prev; 3328 } 3329 3330 extra_cols = width - topfrp->fr_width; 3331 if (extra_cols < 0) 3332 { 3333 // reduce frame width, rightmost frame first 3334 while (frp != NULL) 3335 { 3336 w = frame_minwidth(frp, NULL); 3337 if (frp->fr_width + extra_cols < w) 3338 { 3339 extra_cols += frp->fr_width - w; 3340 frame_new_width(frp, w, leftfirst, wfw); 3341 } 3342 else 3343 { 3344 frame_new_width(frp, frp->fr_width + extra_cols, 3345 leftfirst, wfw); 3346 break; 3347 } 3348 if (leftfirst) 3349 { 3350 do 3351 frp = frp->fr_next; 3352 while (wfw && frp != NULL && frame_fixed_width(frp)); 3353 } 3354 else 3355 { 3356 do 3357 frp = frp->fr_prev; 3358 while (wfw && frp != NULL && frame_fixed_width(frp)); 3359 } 3360 // Increase "width" if we could not reduce enough frames. 3361 if (frp == NULL) 3362 width -= extra_cols; 3363 } 3364 } 3365 else if (extra_cols > 0) 3366 { 3367 // increase width of rightmost frame 3368 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw); 3369 } 3370 } 3371 topfrp->fr_width = width; 3372 } 3373 3374 /* 3375 * Add the vertical separator to windows at the right side of "frp". 3376 * Note: Does not check if there is room! 3377 */ 3378 static void 3379 frame_add_vsep(frame_T *frp) 3380 { 3381 win_T *wp; 3382 3383 if (frp->fr_layout == FR_LEAF) 3384 { 3385 wp = frp->fr_win; 3386 if (wp->w_vsep_width == 0) 3387 { 3388 if (wp->w_width > 0) // don't make it negative 3389 --wp->w_width; 3390 wp->w_vsep_width = 1; 3391 } 3392 } 3393 else if (frp->fr_layout == FR_COL) 3394 { 3395 // Handle all the frames in the column. 3396 FOR_ALL_FRAMES(frp, frp->fr_child) 3397 frame_add_vsep(frp); 3398 } 3399 else // frp->fr_layout == FR_ROW 3400 { 3401 // Only need to handle the last frame in the row. 3402 frp = frp->fr_child; 3403 while (frp->fr_next != NULL) 3404 frp = frp->fr_next; 3405 frame_add_vsep(frp); 3406 } 3407 } 3408 3409 /* 3410 * Set frame width from the window it contains. 3411 */ 3412 static void 3413 frame_fix_width(win_T *wp) 3414 { 3415 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width; 3416 } 3417 3418 /* 3419 * Set frame height from the window it contains. 3420 */ 3421 static void 3422 frame_fix_height(win_T *wp) 3423 { 3424 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height; 3425 } 3426 3427 /* 3428 * Compute the minimal height for frame "topfrp". 3429 * Uses the 'winminheight' option. 3430 * When "next_curwin" isn't NULL, use p_wh for this window. 3431 * When "next_curwin" is NOWIN, don't use at least one line for the current 3432 * window. 3433 */ 3434 static int 3435 frame_minheight(frame_T *topfrp, win_T *next_curwin) 3436 { 3437 frame_T *frp; 3438 int m; 3439 int n; 3440 3441 if (topfrp->fr_win != NULL) 3442 { 3443 if (topfrp->fr_win == next_curwin) 3444 m = p_wh + topfrp->fr_win->w_status_height; 3445 else 3446 { 3447 // window: minimal height of the window plus status line 3448 m = p_wmh + topfrp->fr_win->w_status_height; 3449 if (topfrp->fr_win == curwin && next_curwin == NULL) 3450 { 3451 // Current window is minimal one line high and WinBar is 3452 // visible. 3453 if (p_wmh == 0) 3454 ++m; 3455 m += WINBAR_HEIGHT(curwin); 3456 } 3457 } 3458 } 3459 else if (topfrp->fr_layout == FR_ROW) 3460 { 3461 // get the minimal height from each frame in this row 3462 m = 0; 3463 FOR_ALL_FRAMES(frp, topfrp->fr_child) 3464 { 3465 n = frame_minheight(frp, next_curwin); 3466 if (n > m) 3467 m = n; 3468 } 3469 } 3470 else 3471 { 3472 // Add up the minimal heights for all frames in this column. 3473 m = 0; 3474 FOR_ALL_FRAMES(frp, topfrp->fr_child) 3475 m += frame_minheight(frp, next_curwin); 3476 } 3477 3478 return m; 3479 } 3480 3481 /* 3482 * Compute the minimal width for frame "topfrp". 3483 * When "next_curwin" isn't NULL, use p_wiw for this window. 3484 * When "next_curwin" is NOWIN, don't use at least one column for the current 3485 * window. 3486 */ 3487 static int 3488 frame_minwidth( 3489 frame_T *topfrp, 3490 win_T *next_curwin) // use p_wh and p_wiw for next_curwin 3491 { 3492 frame_T *frp; 3493 int m, n; 3494 3495 if (topfrp->fr_win != NULL) 3496 { 3497 if (topfrp->fr_win == next_curwin) 3498 m = p_wiw + topfrp->fr_win->w_vsep_width; 3499 else 3500 { 3501 // window: minimal width of the window plus separator column 3502 m = p_wmw + topfrp->fr_win->w_vsep_width; 3503 // Current window is minimal one column wide 3504 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL) 3505 ++m; 3506 } 3507 } 3508 else if (topfrp->fr_layout == FR_COL) 3509 { 3510 // get the minimal width from each frame in this column 3511 m = 0; 3512 FOR_ALL_FRAMES(frp, topfrp->fr_child) 3513 { 3514 n = frame_minwidth(frp, next_curwin); 3515 if (n > m) 3516 m = n; 3517 } 3518 } 3519 else 3520 { 3521 // Add up the minimal widths for all frames in this row. 3522 m = 0; 3523 FOR_ALL_FRAMES(frp, topfrp->fr_child) 3524 m += frame_minwidth(frp, next_curwin); 3525 } 3526 3527 return m; 3528 } 3529 3530 3531 /* 3532 * Try to close all windows except current one. 3533 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is 3534 * used and the buffer was modified. 3535 * 3536 * Used by ":bdel" and ":only". 3537 */ 3538 void 3539 close_others( 3540 int message, 3541 int forceit) // always hide all other windows 3542 { 3543 win_T *wp; 3544 win_T *nextwp; 3545 int r; 3546 3547 if (one_window()) 3548 { 3549 if (message && !autocmd_busy) 3550 msg(_(m_onlyone)); 3551 return; 3552 } 3553 3554 // Be very careful here: autocommands may change the window layout. 3555 for (wp = firstwin; win_valid(wp); wp = nextwp) 3556 { 3557 nextwp = wp->w_next; 3558 if (wp != curwin) // don't close current window 3559 { 3560 3561 // Check if it's allowed to abandon this window 3562 r = can_abandon(wp->w_buffer, forceit); 3563 if (!win_valid(wp)) // autocommands messed wp up 3564 { 3565 nextwp = firstwin; 3566 continue; 3567 } 3568 if (!r) 3569 { 3570 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 3571 if (message && (p_confirm || cmdmod.confirm) && p_write) 3572 { 3573 dialog_changed(wp->w_buffer, FALSE); 3574 if (!win_valid(wp)) // autocommands messed wp up 3575 { 3576 nextwp = firstwin; 3577 continue; 3578 } 3579 } 3580 if (bufIsChanged(wp->w_buffer)) 3581 #endif 3582 continue; 3583 } 3584 win_close(wp, !buf_hide(wp->w_buffer) 3585 && !bufIsChanged(wp->w_buffer)); 3586 } 3587 } 3588 3589 if (message && !ONE_WINDOW) 3590 emsg(_("E445: Other window contains changes")); 3591 } 3592 3593 static void 3594 win_init_empty(win_T *wp) 3595 { 3596 redraw_win_later(wp, NOT_VALID); 3597 wp->w_lines_valid = 0; 3598 wp->w_cursor.lnum = 1; 3599 wp->w_curswant = wp->w_cursor.col = 0; 3600 wp->w_cursor.coladd = 0; 3601 wp->w_pcmark.lnum = 1; // pcmark not cleared but set to line 1 3602 wp->w_pcmark.col = 0; 3603 wp->w_prev_pcmark.lnum = 0; 3604 wp->w_prev_pcmark.col = 0; 3605 wp->w_topline = 1; 3606 #ifdef FEAT_DIFF 3607 wp->w_topfill = 0; 3608 #endif 3609 wp->w_botline = 2; 3610 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) 3611 wp->w_s = &wp->w_buffer->b_s; 3612 #endif 3613 } 3614 3615 /* 3616 * Init the current window "curwin". 3617 * Called when a new file is being edited. 3618 */ 3619 void 3620 curwin_init(void) 3621 { 3622 win_init_empty(curwin); 3623 } 3624 3625 /* 3626 * Allocate the first window and put an empty buffer in it. 3627 * Called from main(). 3628 * Return FAIL when something goes wrong (out of memory). 3629 */ 3630 int 3631 win_alloc_first(void) 3632 { 3633 if (win_alloc_firstwin(NULL) == FAIL) 3634 return FAIL; 3635 3636 first_tabpage = alloc_tabpage(); 3637 if (first_tabpage == NULL) 3638 return FAIL; 3639 first_tabpage->tp_topframe = topframe; 3640 curtab = first_tabpage; 3641 curtab->tp_firstwin = firstwin; 3642 curtab->tp_lastwin = lastwin; 3643 curtab->tp_curwin = curwin; 3644 3645 return OK; 3646 } 3647 3648 /* 3649 * Allocate and init a window that is not a regular window. 3650 * This can only be done after the first window is fully initialized, thus it 3651 * can't be in win_alloc_first(). 3652 */ 3653 win_T * 3654 win_alloc_popup_win(void) 3655 { 3656 win_T *wp; 3657 3658 wp = win_alloc(NULL, TRUE); 3659 if (wp != NULL) 3660 { 3661 // We need to initialize options with something, using the current 3662 // window makes most sense. 3663 win_init_some(wp, curwin); 3664 3665 RESET_BINDING(wp); 3666 new_frame(wp); 3667 } 3668 return wp; 3669 } 3670 3671 /* 3672 * Initialize window "wp" to display buffer "buf". 3673 */ 3674 void 3675 win_init_popup_win(win_T *wp, buf_T *buf) 3676 { 3677 wp->w_buffer = buf; 3678 ++buf->b_nwindows; 3679 win_init_empty(wp); // set cursor and topline to safe values 3680 3681 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in 3682 // win_enter_ext(). 3683 VIM_CLEAR(wp->w_localdir); 3684 } 3685 3686 /* 3687 * Allocate the first window or the first window in a new tab page. 3688 * When "oldwin" is NULL create an empty buffer for it. 3689 * When "oldwin" is not NULL copy info from it to the new window. 3690 * Return FAIL when something goes wrong (out of memory). 3691 */ 3692 static int 3693 win_alloc_firstwin(win_T *oldwin) 3694 { 3695 curwin = win_alloc(NULL, FALSE); 3696 if (oldwin == NULL) 3697 { 3698 // Very first window, need to create an empty buffer for it and 3699 // initialize from scratch. 3700 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED); 3701 if (curwin == NULL || curbuf == NULL) 3702 return FAIL; 3703 curwin->w_buffer = curbuf; 3704 #ifdef FEAT_SYN_HL 3705 curwin->w_s = &(curbuf->b_s); 3706 #endif 3707 curbuf->b_nwindows = 1; // there is one window 3708 curwin->w_alist = &global_alist; 3709 curwin_init(); // init current window 3710 } 3711 else 3712 { 3713 // First window in new tab page, initialize it from "oldwin". 3714 win_init(curwin, oldwin, 0); 3715 3716 // We don't want cursor- and scroll-binding in the first window. 3717 RESET_BINDING(curwin); 3718 } 3719 3720 new_frame(curwin); 3721 if (curwin->w_frame == NULL) 3722 return FAIL; 3723 topframe = curwin->w_frame; 3724 topframe->fr_width = Columns; 3725 topframe->fr_height = Rows - p_ch; 3726 3727 return OK; 3728 } 3729 3730 /* 3731 * Create a frame for window "wp". 3732 */ 3733 static void 3734 new_frame(win_T *wp) 3735 { 3736 frame_T *frp = ALLOC_CLEAR_ONE(frame_T); 3737 3738 wp->w_frame = frp; 3739 if (frp != NULL) 3740 { 3741 frp->fr_layout = FR_LEAF; 3742 frp->fr_win = wp; 3743 } 3744 } 3745 3746 /* 3747 * Initialize the window and frame size to the maximum. 3748 */ 3749 void 3750 win_init_size(void) 3751 { 3752 firstwin->w_height = ROWS_AVAIL; 3753 topframe->fr_height = ROWS_AVAIL; 3754 firstwin->w_width = Columns; 3755 topframe->fr_width = Columns; 3756 } 3757 3758 /* 3759 * Allocate a new tabpage_T and init the values. 3760 * Returns NULL when out of memory. 3761 */ 3762 static tabpage_T * 3763 alloc_tabpage(void) 3764 { 3765 tabpage_T *tp; 3766 # ifdef FEAT_GUI 3767 int i; 3768 # endif 3769 3770 3771 tp = ALLOC_CLEAR_ONE(tabpage_T); 3772 if (tp == NULL) 3773 return NULL; 3774 3775 # ifdef FEAT_EVAL 3776 // init t: variables 3777 tp->tp_vars = dict_alloc(); 3778 if (tp->tp_vars == NULL) 3779 { 3780 vim_free(tp); 3781 return NULL; 3782 } 3783 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE); 3784 # endif 3785 3786 # ifdef FEAT_GUI 3787 for (i = 0; i < 3; i++) 3788 tp->tp_prev_which_scrollbars[i] = -1; 3789 # endif 3790 # ifdef FEAT_DIFF 3791 tp->tp_diff_invalid = TRUE; 3792 # endif 3793 tp->tp_ch_used = p_ch; 3794 3795 return tp; 3796 } 3797 3798 void 3799 free_tabpage(tabpage_T *tp) 3800 { 3801 int idx; 3802 3803 # ifdef FEAT_DIFF 3804 diff_clear(tp); 3805 # endif 3806 # ifdef FEAT_PROP_POPUP 3807 while (tp->tp_first_popupwin != NULL) 3808 popup_close_tabpage(tp, tp->tp_first_popupwin->w_id, TRUE); 3809 #endif 3810 for (idx = 0; idx < SNAP_COUNT; ++idx) 3811 clear_snapshot(tp, idx); 3812 #ifdef FEAT_EVAL 3813 vars_clear(&tp->tp_vars->dv_hashtab); // free all t: variables 3814 hash_init(&tp->tp_vars->dv_hashtab); 3815 unref_var_dict(tp->tp_vars); 3816 #endif 3817 3818 if (tp == lastused_tabpage) 3819 lastused_tabpage = NULL; 3820 3821 vim_free(tp->tp_localdir); 3822 vim_free(tp->tp_prevdir); 3823 3824 #ifdef FEAT_PYTHON 3825 python_tabpage_free(tp); 3826 #endif 3827 3828 #ifdef FEAT_PYTHON3 3829 python3_tabpage_free(tp); 3830 #endif 3831 3832 vim_free(tp); 3833 } 3834 3835 /* 3836 * Create a new Tab page with one window. 3837 * It will edit the current buffer, like after ":split". 3838 * When "after" is 0 put it just after the current Tab page. 3839 * Otherwise put it just before tab page "after". 3840 * Return FAIL or OK. 3841 */ 3842 int 3843 win_new_tabpage(int after) 3844 { 3845 tabpage_T *tp = curtab; 3846 tabpage_T *prev_tp = curtab; 3847 tabpage_T *newtp; 3848 int n; 3849 3850 newtp = alloc_tabpage(); 3851 if (newtp == NULL) 3852 return FAIL; 3853 3854 // Remember the current windows in this Tab page. 3855 if (leave_tabpage(curbuf, TRUE) == FAIL) 3856 { 3857 vim_free(newtp); 3858 return FAIL; 3859 } 3860 curtab = newtp; 3861 3862 newtp->tp_localdir = (tp->tp_localdir == NULL) 3863 ? NULL : vim_strsave(tp->tp_localdir); 3864 // Create a new empty window. 3865 if (win_alloc_firstwin(tp->tp_curwin) == OK) 3866 { 3867 // Make the new Tab page the new topframe. 3868 if (after == 1) 3869 { 3870 // New tab page becomes the first one. 3871 newtp->tp_next = first_tabpage; 3872 first_tabpage = newtp; 3873 } 3874 else 3875 { 3876 if (after > 0) 3877 { 3878 // Put new tab page before tab page "after". 3879 n = 2; 3880 for (tp = first_tabpage; tp->tp_next != NULL 3881 && n < after; tp = tp->tp_next) 3882 ++n; 3883 } 3884 newtp->tp_next = tp->tp_next; 3885 tp->tp_next = newtp; 3886 } 3887 newtp->tp_firstwin = newtp->tp_lastwin = newtp->tp_curwin = curwin; 3888 3889 win_init_size(); 3890 firstwin->w_winrow = tabline_height(); 3891 win_comp_scroll(curwin); 3892 3893 newtp->tp_topframe = topframe; 3894 last_status(FALSE); 3895 3896 lastused_tabpage = prev_tp; 3897 3898 #if defined(FEAT_GUI) 3899 // When 'guioptions' includes 'L' or 'R' may have to remove or add 3900 // scrollbars. Have to update them anyway. 3901 gui_may_update_scrollbars(); 3902 #endif 3903 #ifdef FEAT_JOB_CHANNEL 3904 entering_window(curwin); 3905 #endif 3906 3907 redraw_all_later(NOT_VALID); 3908 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf); 3909 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf); 3910 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf); 3911 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf); 3912 return OK; 3913 } 3914 3915 // Failed, get back the previous Tab page 3916 enter_tabpage(curtab, curbuf, TRUE, TRUE); 3917 return FAIL; 3918 } 3919 3920 /* 3921 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer, 3922 * like with ":split". 3923 * Returns OK if a new tab page was created, FAIL otherwise. 3924 */ 3925 static int 3926 may_open_tabpage(void) 3927 { 3928 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab; 3929 3930 if (n != 0) 3931 { 3932 cmdmod.tab = 0; // reset it to avoid doing it twice 3933 postponed_split_tab = 0; 3934 return win_new_tabpage(n); 3935 } 3936 return FAIL; 3937 } 3938 3939 /* 3940 * Create up to "maxcount" tabpages with empty windows. 3941 * Returns the number of resulting tab pages. 3942 */ 3943 int 3944 make_tabpages(int maxcount) 3945 { 3946 int count = maxcount; 3947 int todo; 3948 3949 // Limit to 'tabpagemax' tabs. 3950 if (count > p_tpm) 3951 count = p_tpm; 3952 3953 /* 3954 * Don't execute autocommands while creating the tab pages. Must do that 3955 * when putting the buffers in the windows. 3956 */ 3957 block_autocmds(); 3958 3959 for (todo = count - 1; todo > 0; --todo) 3960 if (win_new_tabpage(0) == FAIL) 3961 break; 3962 3963 unblock_autocmds(); 3964 3965 // return actual number of tab pages 3966 return (count - todo); 3967 } 3968 3969 /* 3970 * Return TRUE when "tpc" points to a valid tab page. 3971 */ 3972 int 3973 valid_tabpage(tabpage_T *tpc) 3974 { 3975 tabpage_T *tp; 3976 3977 FOR_ALL_TABPAGES(tp) 3978 if (tp == tpc) 3979 return TRUE; 3980 return FALSE; 3981 } 3982 3983 /* 3984 * Return TRUE when "tpc" points to a valid tab page and at least one window is 3985 * valid. 3986 */ 3987 int 3988 valid_tabpage_win(tabpage_T *tpc) 3989 { 3990 tabpage_T *tp; 3991 win_T *wp; 3992 3993 FOR_ALL_TABPAGES(tp) 3994 { 3995 if (tp == tpc) 3996 { 3997 FOR_ALL_WINDOWS_IN_TAB(tp, wp) 3998 { 3999 if (win_valid_any_tab(wp)) 4000 return TRUE; 4001 } 4002 return FALSE; 4003 } 4004 } 4005 // shouldn't happen 4006 return FALSE; 4007 } 4008 4009 /* 4010 * Close tabpage "tab", assuming it has no windows in it. 4011 * There must be another tabpage or this will crash. 4012 */ 4013 void 4014 close_tabpage(tabpage_T *tab) 4015 { 4016 tabpage_T *ptp; 4017 4018 if (tab == first_tabpage) 4019 { 4020 first_tabpage = tab->tp_next; 4021 ptp = first_tabpage; 4022 } 4023 else 4024 { 4025 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab; 4026 ptp = ptp->tp_next) 4027 ; 4028 assert(ptp != NULL); 4029 ptp->tp_next = tab->tp_next; 4030 } 4031 4032 goto_tabpage_tp(ptp, FALSE, FALSE); 4033 free_tabpage(tab); 4034 } 4035 4036 /* 4037 * Find tab page "n" (first one is 1). Returns NULL when not found. 4038 */ 4039 tabpage_T * 4040 find_tabpage(int n) 4041 { 4042 tabpage_T *tp; 4043 int i = 1; 4044 4045 if (n == 0) 4046 return curtab; 4047 4048 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next) 4049 ++i; 4050 return tp; 4051 } 4052 4053 /* 4054 * Get index of tab page "tp". First one has index 1. 4055 * When not found returns number of tab pages plus one. 4056 */ 4057 int 4058 tabpage_index(tabpage_T *ftp) 4059 { 4060 int i = 1; 4061 tabpage_T *tp; 4062 4063 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next) 4064 ++i; 4065 return i; 4066 } 4067 4068 /* 4069 * Prepare for leaving the current tab page. 4070 * When autocommands change "curtab" we don't leave the tab page and return 4071 * FAIL. 4072 * Careful: When OK is returned need to get a new tab page very very soon! 4073 */ 4074 static int 4075 leave_tabpage( 4076 buf_T *new_curbuf UNUSED, // what is going to be the new curbuf, 4077 // NULL if unknown 4078 int trigger_leave_autocmds UNUSED) 4079 { 4080 tabpage_T *tp = curtab; 4081 4082 #ifdef FEAT_JOB_CHANNEL 4083 leaving_window(curwin); 4084 #endif 4085 reset_VIsual_and_resel(); // stop Visual mode 4086 if (trigger_leave_autocmds) 4087 { 4088 if (new_curbuf != curbuf) 4089 { 4090 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); 4091 if (curtab != tp) 4092 return FAIL; 4093 } 4094 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf); 4095 if (curtab != tp) 4096 return FAIL; 4097 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf); 4098 if (curtab != tp) 4099 return FAIL; 4100 } 4101 #if defined(FEAT_GUI) 4102 // Remove the scrollbars. They may be added back later. 4103 if (gui.in_use) 4104 gui_remove_scrollbars(); 4105 #endif 4106 tp->tp_curwin = curwin; 4107 tp->tp_prevwin = prevwin; 4108 tp->tp_firstwin = firstwin; 4109 tp->tp_lastwin = lastwin; 4110 tp->tp_old_Rows = Rows; 4111 tp->tp_old_Columns = Columns; 4112 firstwin = NULL; 4113 lastwin = NULL; 4114 return OK; 4115 } 4116 4117 /* 4118 * Start using tab page "tp". 4119 * Only to be used after leave_tabpage() or freeing the current tab page. 4120 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE. 4121 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE. 4122 */ 4123 static void 4124 enter_tabpage( 4125 tabpage_T *tp, 4126 buf_T *old_curbuf UNUSED, 4127 int trigger_enter_autocmds, 4128 int trigger_leave_autocmds) 4129 { 4130 int row; 4131 int old_off = tp->tp_firstwin->w_winrow; 4132 win_T *next_prevwin = tp->tp_prevwin; 4133 tabpage_T *last_tab = curtab; 4134 4135 curtab = tp; 4136 firstwin = tp->tp_firstwin; 4137 lastwin = tp->tp_lastwin; 4138 topframe = tp->tp_topframe; 4139 4140 // We would like doing the TabEnter event first, but we don't have a 4141 // valid current window yet, which may break some commands. 4142 // This triggers autocommands, thus may make "tp" invalid. 4143 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE, 4144 trigger_enter_autocmds, trigger_leave_autocmds); 4145 prevwin = next_prevwin; 4146 4147 last_status(FALSE); // status line may appear or disappear 4148 row = win_comp_pos(); // recompute w_winrow for all windows 4149 #ifdef FEAT_DIFF 4150 diff_need_scrollbind = TRUE; 4151 #endif 4152 4153 // The tabpage line may have appeared or disappeared, may need to resize 4154 // the frames for that. When the Vim window was resized need to update 4155 // frame sizes too. Use the stored value of p_ch, so that it can be 4156 // different for each tab page. 4157 if (p_ch != curtab->tp_ch_used) 4158 clear_cmdline = TRUE; 4159 p_ch = curtab->tp_ch_used; 4160 4161 // When cmdheight is changed in a tab page with '<C-w>-', cmdline_row is 4162 // changed but p_ch and tp_ch_used are not changed. Thus we also need to 4163 // check cmdline_row. 4164 if ((row < cmdline_row) && (cmdline_row <= Rows - p_ch)) 4165 clear_cmdline = TRUE; 4166 4167 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow 4168 #ifdef FEAT_GUI_TABLINE 4169 && !gui_use_tabline() 4170 #endif 4171 )) 4172 shell_new_rows(); 4173 if (curtab->tp_old_Columns != Columns && starting == 0) 4174 shell_new_columns(); // update window widths 4175 4176 lastused_tabpage = last_tab; 4177 4178 #if defined(FEAT_GUI) 4179 // When 'guioptions' includes 'L' or 'R' may have to remove or add 4180 // scrollbars. Have to update them anyway. 4181 gui_may_update_scrollbars(); 4182 #endif 4183 4184 // Apply autocommands after updating the display, when 'rows' and 4185 // 'columns' have been set correctly. 4186 if (trigger_enter_autocmds) 4187 { 4188 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf); 4189 if (old_curbuf != curbuf) 4190 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); 4191 } 4192 4193 redraw_all_later(NOT_VALID); 4194 } 4195 4196 /* 4197 * Go to tab page "n". For ":tab N" and "Ngt". 4198 * When "n" is 9999 go to the last tab page. 4199 */ 4200 void 4201 goto_tabpage(int n) 4202 { 4203 tabpage_T *tp = NULL; // shut up compiler 4204 tabpage_T *ttp; 4205 int i; 4206 4207 if (text_locked()) 4208 { 4209 // Not allowed when editing the command line. 4210 text_locked_msg(); 4211 return; 4212 } 4213 4214 // If there is only one it can't work. 4215 if (first_tabpage->tp_next == NULL) 4216 { 4217 if (n > 1) 4218 beep_flush(); 4219 return; 4220 } 4221 4222 if (n == 0) 4223 { 4224 // No count, go to next tab page, wrap around end. 4225 if (curtab->tp_next == NULL) 4226 tp = first_tabpage; 4227 else 4228 tp = curtab->tp_next; 4229 } 4230 else if (n < 0) 4231 { 4232 // "gT": go to previous tab page, wrap around end. "N gT" repeats 4233 // this N times. 4234 ttp = curtab; 4235 for (i = n; i < 0; ++i) 4236 { 4237 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL; 4238 tp = tp->tp_next) 4239 ; 4240 ttp = tp; 4241 } 4242 } 4243 else if (n == 9999) 4244 { 4245 // Go to last tab page. 4246 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next) 4247 ; 4248 } 4249 else 4250 { 4251 // Go to tab page "n". 4252 tp = find_tabpage(n); 4253 if (tp == NULL) 4254 { 4255 beep_flush(); 4256 return; 4257 } 4258 } 4259 4260 goto_tabpage_tp(tp, TRUE, TRUE); 4261 4262 #ifdef FEAT_GUI_TABLINE 4263 if (gui_use_tabline()) 4264 gui_mch_set_curtab(tabpage_index(curtab)); 4265 #endif 4266 } 4267 4268 /* 4269 * Go to tabpage "tp". 4270 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE. 4271 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE. 4272 * Note: doesn't update the GUI tab. 4273 */ 4274 void 4275 goto_tabpage_tp( 4276 tabpage_T *tp, 4277 int trigger_enter_autocmds, 4278 int trigger_leave_autocmds) 4279 { 4280 // Don't repeat a message in another tab page. 4281 set_keep_msg(NULL, 0); 4282 4283 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer, 4284 trigger_leave_autocmds) == OK) 4285 { 4286 if (valid_tabpage(tp)) 4287 enter_tabpage(tp, curbuf, trigger_enter_autocmds, 4288 trigger_leave_autocmds); 4289 else 4290 enter_tabpage(curtab, curbuf, trigger_enter_autocmds, 4291 trigger_leave_autocmds); 4292 } 4293 } 4294 4295 /* 4296 * Go to the last accessed tab page, if there is one. 4297 * Return OK or FAIL 4298 */ 4299 int 4300 goto_tabpage_lastused(void) 4301 { 4302 if (valid_tabpage(lastused_tabpage)) 4303 { 4304 goto_tabpage_tp(lastused_tabpage, TRUE, TRUE); 4305 return OK; 4306 } 4307 return FAIL; 4308 } 4309 4310 /* 4311 * Enter window "wp" in tab page "tp". 4312 * Also updates the GUI tab. 4313 */ 4314 void 4315 goto_tabpage_win(tabpage_T *tp, win_T *wp) 4316 { 4317 goto_tabpage_tp(tp, TRUE, TRUE); 4318 if (curtab == tp && win_valid(wp)) 4319 { 4320 win_enter(wp, TRUE); 4321 # ifdef FEAT_GUI_TABLINE 4322 if (gui_use_tabline()) 4323 gui_mch_set_curtab(tabpage_index(curtab)); 4324 # endif 4325 } 4326 } 4327 4328 /* 4329 * Move the current tab page to after tab page "nr". 4330 */ 4331 void 4332 tabpage_move(int nr) 4333 { 4334 int n = 1; 4335 tabpage_T *tp, *tp_dst; 4336 4337 if (first_tabpage->tp_next == NULL) 4338 return; 4339 4340 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next) 4341 ++n; 4342 4343 if (tp == curtab || (nr > 0 && tp->tp_next != NULL 4344 && tp->tp_next == curtab)) 4345 return; 4346 4347 tp_dst = tp; 4348 4349 // Remove the current tab page from the list of tab pages. 4350 if (curtab == first_tabpage) 4351 first_tabpage = curtab->tp_next; 4352 else 4353 { 4354 FOR_ALL_TABPAGES(tp) 4355 if (tp->tp_next == curtab) 4356 break; 4357 if (tp == NULL) // "cannot happen" 4358 return; 4359 tp->tp_next = curtab->tp_next; 4360 } 4361 4362 // Re-insert it at the specified position. 4363 if (nr <= 0) 4364 { 4365 curtab->tp_next = first_tabpage; 4366 first_tabpage = curtab; 4367 } 4368 else 4369 { 4370 curtab->tp_next = tp_dst->tp_next; 4371 tp_dst->tp_next = curtab; 4372 } 4373 4374 // Need to redraw the tabline. Tab page contents doesn't change. 4375 redraw_tabline = TRUE; 4376 } 4377 4378 4379 /* 4380 * Go to another window. 4381 * When jumping to another buffer, stop Visual mode. Do this before 4382 * changing windows so we can yank the selection into the '*' register. 4383 * When jumping to another window on the same buffer, adjust its cursor 4384 * position to keep the same Visual area. 4385 */ 4386 void 4387 win_goto(win_T *wp) 4388 { 4389 #ifdef FEAT_CONCEAL 4390 win_T *owp = curwin; 4391 #endif 4392 4393 #ifdef FEAT_PROP_POPUP 4394 if (ERROR_IF_ANY_POPUP_WINDOW) 4395 return; 4396 if (popup_is_popup(wp)) 4397 { 4398 emsg(_("E366: Not allowed to enter a popup window")); 4399 return; 4400 } 4401 #endif 4402 if (text_and_win_locked()) 4403 { 4404 beep_flush(); 4405 text_locked_msg(); 4406 return; 4407 } 4408 if (curbuf_locked()) 4409 return; 4410 4411 if (wp->w_buffer != curbuf) 4412 reset_VIsual_and_resel(); 4413 else if (VIsual_active) 4414 wp->w_cursor = curwin->w_cursor; 4415 4416 #ifdef FEAT_GUI 4417 need_mouse_correct = TRUE; 4418 #endif 4419 win_enter(wp, TRUE); 4420 4421 #ifdef FEAT_CONCEAL 4422 // Conceal cursor line in previous window, unconceal in current window. 4423 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled) 4424 redrawWinline(owp, owp->w_cursor.lnum); 4425 if (curwin->w_p_cole > 0 && !msg_scrolled) 4426 need_cursor_line_redraw = TRUE; 4427 #endif 4428 } 4429 4430 #if defined(FEAT_PERL) || defined(PROTO) 4431 /* 4432 * Find window number "winnr" (counting top to bottom). 4433 */ 4434 win_T * 4435 win_find_nr(int winnr) 4436 { 4437 win_T *wp; 4438 4439 FOR_ALL_WINDOWS(wp) 4440 if (--winnr == 0) 4441 break; 4442 return wp; 4443 } 4444 #endif 4445 4446 #if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO) 4447 /* 4448 * Find the tabpage for window "win". 4449 */ 4450 tabpage_T * 4451 win_find_tabpage(win_T *win) 4452 { 4453 win_T *wp; 4454 tabpage_T *tp; 4455 4456 FOR_ALL_TAB_WINDOWS(tp, wp) 4457 if (wp == win) 4458 return tp; 4459 return NULL; 4460 } 4461 #endif 4462 4463 /* 4464 * Get the above or below neighbor window of the specified window. 4465 * up - TRUE for the above neighbor 4466 * count - nth neighbor window 4467 * Returns the specified window if the neighbor is not found. 4468 */ 4469 win_T * 4470 win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count) 4471 { 4472 frame_T *fr; 4473 frame_T *nfr; 4474 frame_T *foundfr; 4475 4476 #ifdef FEAT_PROP_POPUP 4477 if (popup_is_popup(wp)) 4478 // popups don't have neighbors. 4479 return NULL; 4480 #endif 4481 foundfr = wp->w_frame; 4482 while (count--) 4483 { 4484 /* 4485 * First go upwards in the tree of frames until we find a upwards or 4486 * downwards neighbor. 4487 */ 4488 fr = foundfr; 4489 for (;;) 4490 { 4491 if (fr == tp->tp_topframe) 4492 goto end; 4493 if (up) 4494 nfr = fr->fr_prev; 4495 else 4496 nfr = fr->fr_next; 4497 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL) 4498 break; 4499 fr = fr->fr_parent; 4500 } 4501 4502 /* 4503 * Now go downwards to find the bottom or top frame in it. 4504 */ 4505 for (;;) 4506 { 4507 if (nfr->fr_layout == FR_LEAF) 4508 { 4509 foundfr = nfr; 4510 break; 4511 } 4512 fr = nfr->fr_child; 4513 if (nfr->fr_layout == FR_ROW) 4514 { 4515 // Find the frame at the cursor row. 4516 while (fr->fr_next != NULL 4517 && frame2win(fr)->w_wincol + fr->fr_width 4518 <= wp->w_wincol + wp->w_wcol) 4519 fr = fr->fr_next; 4520 } 4521 if (nfr->fr_layout == FR_COL && up) 4522 while (fr->fr_next != NULL) 4523 fr = fr->fr_next; 4524 nfr = fr; 4525 } 4526 } 4527 end: 4528 return foundfr != NULL ? foundfr->fr_win : NULL; 4529 } 4530 4531 /* 4532 * Move to window above or below "count" times. 4533 */ 4534 static void 4535 win_goto_ver( 4536 int up, // TRUE to go to win above 4537 long count) 4538 { 4539 win_T *win; 4540 4541 #ifdef FEAT_PROP_POPUP 4542 if (ERROR_IF_TERM_POPUP_WINDOW) 4543 return; 4544 #endif 4545 win = win_vert_neighbor(curtab, curwin, up, count); 4546 if (win != NULL) 4547 win_goto(win); 4548 } 4549 4550 /* 4551 * Get the left or right neighbor window of the specified window. 4552 * left - TRUE for the left neighbor 4553 * count - nth neighbor window 4554 * Returns the specified window if the neighbor is not found. 4555 */ 4556 win_T * 4557 win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count) 4558 { 4559 frame_T *fr; 4560 frame_T *nfr; 4561 frame_T *foundfr; 4562 4563 #ifdef FEAT_PROP_POPUP 4564 if (popup_is_popup(wp)) 4565 // popups don't have neighbors. 4566 return NULL; 4567 #endif 4568 foundfr = wp->w_frame; 4569 while (count--) 4570 { 4571 /* 4572 * First go upwards in the tree of frames until we find a left or 4573 * right neighbor. 4574 */ 4575 fr = foundfr; 4576 for (;;) 4577 { 4578 if (fr == tp->tp_topframe) 4579 goto end; 4580 if (left) 4581 nfr = fr->fr_prev; 4582 else 4583 nfr = fr->fr_next; 4584 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL) 4585 break; 4586 fr = fr->fr_parent; 4587 } 4588 4589 /* 4590 * Now go downwards to find the leftmost or rightmost frame in it. 4591 */ 4592 for (;;) 4593 { 4594 if (nfr->fr_layout == FR_LEAF) 4595 { 4596 foundfr = nfr; 4597 break; 4598 } 4599 fr = nfr->fr_child; 4600 if (nfr->fr_layout == FR_COL) 4601 { 4602 // Find the frame at the cursor row. 4603 while (fr->fr_next != NULL 4604 && frame2win(fr)->w_winrow + fr->fr_height 4605 <= wp->w_winrow + wp->w_wrow) 4606 fr = fr->fr_next; 4607 } 4608 if (nfr->fr_layout == FR_ROW && left) 4609 while (fr->fr_next != NULL) 4610 fr = fr->fr_next; 4611 nfr = fr; 4612 } 4613 } 4614 end: 4615 return foundfr != NULL ? foundfr->fr_win : NULL; 4616 } 4617 4618 /* 4619 * Move to left or right window. 4620 */ 4621 static void 4622 win_goto_hor( 4623 int left, // TRUE to go to left win 4624 long count) 4625 { 4626 win_T *win; 4627 4628 #ifdef FEAT_PROP_POPUP 4629 if (ERROR_IF_TERM_POPUP_WINDOW) 4630 return; 4631 #endif 4632 win = win_horz_neighbor(curtab, curwin, left, count); 4633 if (win != NULL) 4634 win_goto(win); 4635 } 4636 4637 /* 4638 * Make window "wp" the current window. 4639 */ 4640 void 4641 win_enter(win_T *wp, int undo_sync) 4642 { 4643 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE); 4644 } 4645 4646 /* 4647 * Make window wp the current window. 4648 * Can be called with "curwin_invalid" TRUE, which means that curwin has just 4649 * been closed and isn't valid. 4650 */ 4651 static void 4652 win_enter_ext( 4653 win_T *wp, 4654 int undo_sync, 4655 int curwin_invalid, 4656 int trigger_new_autocmds, 4657 int trigger_enter_autocmds, 4658 int trigger_leave_autocmds) 4659 { 4660 int other_buffer = FALSE; 4661 4662 if (wp == curwin && !curwin_invalid) // nothing to do 4663 return; 4664 4665 #ifdef FEAT_JOB_CHANNEL 4666 if (!curwin_invalid) 4667 leaving_window(curwin); 4668 #endif 4669 4670 if (!curwin_invalid && trigger_leave_autocmds) 4671 { 4672 /* 4673 * Be careful: If autocommands delete the window, return now. 4674 */ 4675 if (wp->w_buffer != curbuf) 4676 { 4677 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); 4678 other_buffer = TRUE; 4679 if (!win_valid(wp)) 4680 return; 4681 } 4682 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf); 4683 if (!win_valid(wp)) 4684 return; 4685 #ifdef FEAT_EVAL 4686 // autocmds may abort script processing 4687 if (aborting()) 4688 return; 4689 #endif 4690 } 4691 4692 // sync undo before leaving the current buffer 4693 if (undo_sync && curbuf != wp->w_buffer) 4694 u_sync(FALSE); 4695 4696 // Might need to scroll the old window before switching, e.g., when the 4697 // cursor was moved. 4698 update_topline(); 4699 4700 // may have to copy the buffer options when 'cpo' contains 'S' 4701 if (wp->w_buffer != curbuf) 4702 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP); 4703 if (!curwin_invalid) 4704 { 4705 prevwin = curwin; // remember for CTRL-W p 4706 curwin->w_redr_status = TRUE; 4707 } 4708 curwin = wp; 4709 curbuf = wp->w_buffer; 4710 check_cursor(); 4711 if (!virtual_active()) 4712 curwin->w_cursor.coladd = 0; 4713 changed_line_abv_curs(); // assume cursor position needs updating 4714 4715 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL) 4716 { 4717 char_u *dirname; 4718 4719 // Window or tab has a local directory: Save current directory as 4720 // global directory (unless that was done already) and change to the 4721 // local directory. 4722 if (globaldir == NULL) 4723 { 4724 char_u cwd[MAXPATHL]; 4725 4726 if (mch_dirname(cwd, MAXPATHL) == OK) 4727 globaldir = vim_strsave(cwd); 4728 } 4729 if (curwin->w_localdir != NULL) 4730 dirname = curwin->w_localdir; 4731 else 4732 dirname = curtab->tp_localdir; 4733 4734 if (mch_chdir((char *)dirname) == 0) 4735 shorten_fnames(TRUE); 4736 } 4737 else if (globaldir != NULL) 4738 { 4739 // Window doesn't have a local directory and we are not in the global 4740 // directory: Change to the global directory. 4741 vim_ignored = mch_chdir((char *)globaldir); 4742 VIM_CLEAR(globaldir); 4743 shorten_fnames(TRUE); 4744 } 4745 4746 #ifdef FEAT_JOB_CHANNEL 4747 entering_window(curwin); 4748 #endif 4749 // Careful: autocommands may close the window and make "wp" invalid 4750 if (trigger_new_autocmds) 4751 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf); 4752 if (trigger_enter_autocmds) 4753 { 4754 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf); 4755 if (other_buffer) 4756 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); 4757 } 4758 4759 #ifdef FEAT_TITLE 4760 maketitle(); 4761 #endif 4762 curwin->w_redr_status = TRUE; 4763 #ifdef FEAT_TERMINAL 4764 if (bt_terminal(curwin->w_buffer)) 4765 // terminal is likely in another mode 4766 redraw_mode = TRUE; 4767 #endif 4768 redraw_tabline = TRUE; 4769 if (restart_edit) 4770 redraw_later(VALID); // causes status line redraw 4771 4772 // set window height to desired minimal value 4773 if (curwin->w_height < p_wh && !curwin->w_p_wfh 4774 #ifdef FEAT_PROP_POPUP 4775 && !popup_is_popup(curwin) 4776 #endif 4777 ) 4778 win_setheight((int)p_wh); 4779 else if (curwin->w_height == 0) 4780 win_setheight(1); 4781 4782 // set window width to desired minimal value 4783 if (curwin->w_width < p_wiw && !curwin->w_p_wfw) 4784 win_setwidth((int)p_wiw); 4785 4786 setmouse(); // in case jumped to/from help buffer 4787 4788 // Change directories when the 'acd' option is set. 4789 DO_AUTOCHDIR; 4790 } 4791 4792 4793 /* 4794 * Jump to the first open window that contains buffer "buf", if one exists. 4795 * Returns a pointer to the window found, otherwise NULL. 4796 */ 4797 win_T * 4798 buf_jump_open_win(buf_T *buf) 4799 { 4800 win_T *wp = NULL; 4801 4802 if (curwin->w_buffer == buf) 4803 wp = curwin; 4804 else 4805 FOR_ALL_WINDOWS(wp) 4806 if (wp->w_buffer == buf) 4807 break; 4808 if (wp != NULL) 4809 win_enter(wp, FALSE); 4810 return wp; 4811 } 4812 4813 /* 4814 * Jump to the first open window in any tab page that contains buffer "buf", 4815 * if one exists. 4816 * Returns a pointer to the window found, otherwise NULL. 4817 */ 4818 win_T * 4819 buf_jump_open_tab(buf_T *buf) 4820 { 4821 win_T *wp = buf_jump_open_win(buf); 4822 tabpage_T *tp; 4823 4824 if (wp != NULL) 4825 return wp; 4826 4827 FOR_ALL_TABPAGES(tp) 4828 if (tp != curtab) 4829 { 4830 FOR_ALL_WINDOWS_IN_TAB(tp, wp) 4831 if (wp->w_buffer == buf) 4832 break; 4833 if (wp != NULL) 4834 { 4835 goto_tabpage_win(tp, wp); 4836 if (curwin != wp) 4837 wp = NULL; // something went wrong 4838 break; 4839 } 4840 } 4841 return wp; 4842 } 4843 4844 static int last_win_id = LOWEST_WIN_ID - 1; 4845 4846 /* 4847 * Allocate a window structure and link it in the window list when "hidden" is 4848 * FALSE. 4849 */ 4850 static win_T * 4851 win_alloc(win_T *after UNUSED, int hidden UNUSED) 4852 { 4853 win_T *new_wp; 4854 4855 /* 4856 * allocate window structure and linesizes arrays 4857 */ 4858 new_wp = ALLOC_CLEAR_ONE(win_T); 4859 if (new_wp == NULL) 4860 return NULL; 4861 4862 if (win_alloc_lines(new_wp) == FAIL) 4863 { 4864 vim_free(new_wp); 4865 return NULL; 4866 } 4867 4868 new_wp->w_id = ++last_win_id; 4869 4870 #ifdef FEAT_EVAL 4871 // init w: variables 4872 new_wp->w_vars = dict_alloc(); 4873 if (new_wp->w_vars == NULL) 4874 { 4875 win_free_lsize(new_wp); 4876 vim_free(new_wp); 4877 return NULL; 4878 } 4879 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE); 4880 #endif 4881 4882 // Don't execute autocommands while the window is not properly 4883 // initialized yet. gui_create_scrollbar() may trigger a FocusGained 4884 // event. 4885 block_autocmds(); 4886 4887 /* 4888 * link the window in the window list 4889 */ 4890 if (!hidden) 4891 win_append(after, new_wp); 4892 new_wp->w_wincol = 0; 4893 new_wp->w_width = Columns; 4894 4895 // position the display and the cursor at the top of the file. 4896 new_wp->w_topline = 1; 4897 #ifdef FEAT_DIFF 4898 new_wp->w_topfill = 0; 4899 #endif 4900 new_wp->w_botline = 2; 4901 new_wp->w_cursor.lnum = 1; 4902 new_wp->w_scbind_pos = 1; 4903 4904 // use global option value for global-local options 4905 new_wp->w_p_so = -1; 4906 new_wp->w_p_siso = -1; 4907 4908 // We won't calculate w_fraction until resizing the window 4909 new_wp->w_fraction = 0; 4910 new_wp->w_prev_fraction_row = -1; 4911 4912 #ifdef FEAT_GUI 4913 if (gui.in_use) 4914 { 4915 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT], 4916 SBAR_LEFT, new_wp); 4917 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT], 4918 SBAR_RIGHT, new_wp); 4919 } 4920 #endif 4921 #ifdef FEAT_FOLDING 4922 foldInitWin(new_wp); 4923 #endif 4924 unblock_autocmds(); 4925 #ifdef FEAT_SEARCH_EXTRA 4926 new_wp->w_match_head = NULL; 4927 new_wp->w_next_match_id = 4; 4928 #endif 4929 return new_wp; 4930 } 4931 4932 /* 4933 * Remove window 'wp' from the window list and free the structure. 4934 */ 4935 static void 4936 win_free( 4937 win_T *wp, 4938 tabpage_T *tp) // tab page "win" is in, NULL for current 4939 { 4940 int i; 4941 buf_T *buf; 4942 wininfo_T *wip; 4943 4944 #ifdef FEAT_FOLDING 4945 clearFolding(wp); 4946 #endif 4947 4948 // reduce the reference count to the argument list. 4949 alist_unlink(wp->w_alist); 4950 4951 // Don't execute autocommands while the window is halfway being deleted. 4952 // gui_mch_destroy_scrollbar() may trigger a FocusGained event. 4953 block_autocmds(); 4954 4955 #ifdef FEAT_LUA 4956 lua_window_free(wp); 4957 #endif 4958 4959 #ifdef FEAT_MZSCHEME 4960 mzscheme_window_free(wp); 4961 #endif 4962 4963 #ifdef FEAT_PERL 4964 perl_win_free(wp); 4965 #endif 4966 4967 #ifdef FEAT_PYTHON 4968 python_window_free(wp); 4969 #endif 4970 4971 #ifdef FEAT_PYTHON3 4972 python3_window_free(wp); 4973 #endif 4974 4975 #ifdef FEAT_TCL 4976 tcl_window_free(wp); 4977 #endif 4978 4979 #ifdef FEAT_RUBY 4980 ruby_window_free(wp); 4981 #endif 4982 4983 clear_winopt(&wp->w_onebuf_opt); 4984 clear_winopt(&wp->w_allbuf_opt); 4985 4986 #ifdef FEAT_EVAL 4987 vars_clear(&wp->w_vars->dv_hashtab); // free all w: variables 4988 hash_init(&wp->w_vars->dv_hashtab); 4989 unref_var_dict(wp->w_vars); 4990 #endif 4991 4992 { 4993 tabpage_T *ttp; 4994 4995 if (prevwin == wp) 4996 prevwin = NULL; 4997 FOR_ALL_TABPAGES(ttp) 4998 if (ttp->tp_prevwin == wp) 4999 ttp->tp_prevwin = NULL; 5000 } 5001 win_free_lsize(wp); 5002 5003 for (i = 0; i < wp->w_tagstacklen; ++i) 5004 { 5005 vim_free(wp->w_tagstack[i].tagname); 5006 vim_free(wp->w_tagstack[i].user_data); 5007 } 5008 vim_free(wp->w_localdir); 5009 vim_free(wp->w_prevdir); 5010 5011 // Remove the window from the b_wininfo lists, it may happen that the 5012 // freed memory is re-used for another window. 5013 FOR_ALL_BUFFERS(buf) 5014 FOR_ALL_BUF_WININFO(buf, wip) 5015 if (wip->wi_win == wp) 5016 wip->wi_win = NULL; 5017 5018 #ifdef FEAT_SEARCH_EXTRA 5019 clear_matches(wp); 5020 #endif 5021 5022 #ifdef FEAT_JUMPLIST 5023 free_jumplist(wp); 5024 #endif 5025 5026 #ifdef FEAT_QUICKFIX 5027 qf_free_all(wp); 5028 #endif 5029 5030 #ifdef FEAT_GUI 5031 if (gui.in_use) 5032 { 5033 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]); 5034 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]); 5035 } 5036 #endif // FEAT_GUI 5037 5038 #ifdef FEAT_MENU 5039 remove_winbar(wp); 5040 #endif 5041 #ifdef FEAT_PROP_POPUP 5042 free_callback(&wp->w_close_cb); 5043 free_callback(&wp->w_filter_cb); 5044 for (i = 0; i < 4; ++i) 5045 VIM_CLEAR(wp->w_border_highlight[i]); 5046 vim_free(wp->w_scrollbar_highlight); 5047 vim_free(wp->w_thumb_highlight); 5048 vim_free(wp->w_popup_title); 5049 list_unref(wp->w_popup_mask); 5050 vim_free(wp->w_popup_mask_cells); 5051 #endif 5052 5053 #ifdef FEAT_SYN_HL 5054 vim_free(wp->w_p_cc_cols); 5055 #endif 5056 5057 if (win_valid_any_tab(wp)) 5058 win_remove(wp, tp); 5059 if (autocmd_busy) 5060 { 5061 wp->w_next = au_pending_free_win; 5062 au_pending_free_win = wp; 5063 } 5064 else 5065 vim_free(wp); 5066 5067 unblock_autocmds(); 5068 } 5069 5070 /* 5071 * Return TRUE if "wp" is not in the list of windows: the autocmd window or a 5072 * popup window. 5073 */ 5074 static int 5075 win_unlisted(win_T *wp) 5076 { 5077 return wp == aucmd_win || WIN_IS_POPUP(wp); 5078 } 5079 5080 #if defined(FEAT_PROP_POPUP) || defined(PROTO) 5081 /* 5082 * Free a popup window. This does not take the window out of the window list 5083 * and assumes there is only one toplevel frame, no split. 5084 */ 5085 void 5086 win_free_popup(win_T *win) 5087 { 5088 if (bt_popup(win->w_buffer)) 5089 win_close_buffer(win, DOBUF_WIPE_REUSE, FALSE); 5090 else 5091 close_buffer(win, win->w_buffer, 0, FALSE, FALSE); 5092 # if defined(FEAT_TIMERS) 5093 if (win->w_popup_timer != NULL) 5094 stop_timer(win->w_popup_timer); 5095 # endif 5096 vim_free(win->w_frame); 5097 win_free(win, NULL); 5098 } 5099 #endif 5100 5101 /* 5102 * Append window "wp" in the window list after window "after". 5103 */ 5104 static void 5105 win_append(win_T *after, win_T *wp) 5106 { 5107 win_T *before; 5108 5109 if (after == NULL) // after NULL is in front of the first 5110 before = firstwin; 5111 else 5112 before = after->w_next; 5113 5114 wp->w_next = before; 5115 wp->w_prev = after; 5116 if (after == NULL) 5117 firstwin = wp; 5118 else 5119 after->w_next = wp; 5120 if (before == NULL) 5121 lastwin = wp; 5122 else 5123 before->w_prev = wp; 5124 } 5125 5126 /* 5127 * Remove a window from the window list. 5128 */ 5129 void 5130 win_remove( 5131 win_T *wp, 5132 tabpage_T *tp) // tab page "win" is in, NULL for current 5133 { 5134 if (wp->w_prev != NULL) 5135 wp->w_prev->w_next = wp->w_next; 5136 else if (tp == NULL) 5137 firstwin = curtab->tp_firstwin = wp->w_next; 5138 else 5139 tp->tp_firstwin = wp->w_next; 5140 5141 if (wp->w_next != NULL) 5142 wp->w_next->w_prev = wp->w_prev; 5143 else if (tp == NULL) 5144 lastwin = curtab->tp_lastwin = wp->w_prev; 5145 else 5146 tp->tp_lastwin = wp->w_prev; 5147 } 5148 5149 /* 5150 * Append frame "frp" in a frame list after frame "after". 5151 */ 5152 static void 5153 frame_append(frame_T *after, frame_T *frp) 5154 { 5155 frp->fr_next = after->fr_next; 5156 after->fr_next = frp; 5157 if (frp->fr_next != NULL) 5158 frp->fr_next->fr_prev = frp; 5159 frp->fr_prev = after; 5160 } 5161 5162 /* 5163 * Insert frame "frp" in a frame list before frame "before". 5164 */ 5165 static void 5166 frame_insert(frame_T *before, frame_T *frp) 5167 { 5168 frp->fr_next = before; 5169 frp->fr_prev = before->fr_prev; 5170 before->fr_prev = frp; 5171 if (frp->fr_prev != NULL) 5172 frp->fr_prev->fr_next = frp; 5173 else 5174 frp->fr_parent->fr_child = frp; 5175 } 5176 5177 /* 5178 * Remove a frame from a frame list. 5179 */ 5180 static void 5181 frame_remove(frame_T *frp) 5182 { 5183 if (frp->fr_prev != NULL) 5184 frp->fr_prev->fr_next = frp->fr_next; 5185 else 5186 { 5187 frp->fr_parent->fr_child = frp->fr_next; 5188 // special case: topframe->fr_child == frp 5189 if (topframe->fr_child == frp) 5190 topframe->fr_child = frp->fr_next; 5191 } 5192 if (frp->fr_next != NULL) 5193 frp->fr_next->fr_prev = frp->fr_prev; 5194 } 5195 5196 /* 5197 * Allocate w_lines[] for window "wp". 5198 * Return FAIL for failure, OK for success. 5199 */ 5200 int 5201 win_alloc_lines(win_T *wp) 5202 { 5203 wp->w_lines_valid = 0; 5204 wp->w_lines = ALLOC_CLEAR_MULT(wline_T, Rows ); 5205 if (wp->w_lines == NULL) 5206 return FAIL; 5207 return OK; 5208 } 5209 5210 /* 5211 * free lsize arrays for a window 5212 */ 5213 void 5214 win_free_lsize(win_T *wp) 5215 { 5216 // TODO: why would wp be NULL here? 5217 if (wp != NULL) 5218 VIM_CLEAR(wp->w_lines); 5219 } 5220 5221 /* 5222 * Called from win_new_shellsize() after Rows changed. 5223 * This only does the current tab page, others must be done when made active. 5224 */ 5225 void 5226 shell_new_rows(void) 5227 { 5228 int h = (int)ROWS_AVAIL; 5229 5230 if (firstwin == NULL) // not initialized yet 5231 return; 5232 if (h < frame_minheight(topframe, NULL)) 5233 h = frame_minheight(topframe, NULL); 5234 5235 // First try setting the heights of windows with 'winfixheight'. If 5236 // that doesn't result in the right height, forget about that option. 5237 frame_new_height(topframe, h, FALSE, TRUE); 5238 if (!frame_check_height(topframe, h)) 5239 frame_new_height(topframe, h, FALSE, FALSE); 5240 5241 (void)win_comp_pos(); // recompute w_winrow and w_wincol 5242 compute_cmdrow(); 5243 curtab->tp_ch_used = p_ch; 5244 5245 #if 0 5246 // Disabled: don't want making the screen smaller make a window larger. 5247 if (p_ea) 5248 win_equal(curwin, FALSE, 'v'); 5249 #endif 5250 } 5251 5252 /* 5253 * Called from win_new_shellsize() after Columns changed. 5254 */ 5255 void 5256 shell_new_columns(void) 5257 { 5258 if (firstwin == NULL) // not initialized yet 5259 return; 5260 5261 // First try setting the widths of windows with 'winfixwidth'. If that 5262 // doesn't result in the right width, forget about that option. 5263 frame_new_width(topframe, (int)Columns, FALSE, TRUE); 5264 if (!frame_check_width(topframe, Columns)) 5265 frame_new_width(topframe, (int)Columns, FALSE, FALSE); 5266 5267 (void)win_comp_pos(); // recompute w_winrow and w_wincol 5268 #if 0 5269 // Disabled: don't want making the screen smaller make a window larger. 5270 if (p_ea) 5271 win_equal(curwin, FALSE, 'h'); 5272 #endif 5273 } 5274 5275 #if defined(FEAT_CMDWIN) || defined(PROTO) 5276 /* 5277 * Save the size of all windows in "gap". 5278 */ 5279 void 5280 win_size_save(garray_T *gap) 5281 5282 { 5283 win_T *wp; 5284 5285 ga_init2(gap, (int)sizeof(int), 1); 5286 if (ga_grow(gap, win_count() * 2 + 1) == OK) 5287 { 5288 // first entry is value of 'lines' 5289 ((int *)gap->ga_data)[gap->ga_len++] = Rows; 5290 5291 FOR_ALL_WINDOWS(wp) 5292 { 5293 ((int *)gap->ga_data)[gap->ga_len++] = 5294 wp->w_width + wp->w_vsep_width; 5295 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height; 5296 } 5297 } 5298 } 5299 5300 /* 5301 * Restore window sizes, but only if the number of windows is still the same 5302 * and 'lines' didn't change. 5303 * Does not free the growarray. 5304 */ 5305 void 5306 win_size_restore(garray_T *gap) 5307 { 5308 win_T *wp; 5309 int i, j; 5310 5311 if (win_count() * 2 + 1 == gap->ga_len 5312 && ((int *)gap->ga_data)[0] == Rows) 5313 { 5314 // The order matters, because frames contain other frames, but it's 5315 // difficult to get right. The easy way out is to do it twice. 5316 for (j = 0; j < 2; ++j) 5317 { 5318 i = 1; 5319 FOR_ALL_WINDOWS(wp) 5320 { 5321 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]); 5322 win_setheight_win(((int *)gap->ga_data)[i++], wp); 5323 } 5324 } 5325 // recompute the window positions 5326 (void)win_comp_pos(); 5327 } 5328 } 5329 #endif // FEAT_CMDWIN 5330 5331 /* 5332 * Update the position for all windows, using the width and height of the 5333 * frames. 5334 * Returns the row just after the last window. 5335 */ 5336 int 5337 win_comp_pos(void) 5338 { 5339 int row = tabline_height(); 5340 int col = 0; 5341 5342 frame_comp_pos(topframe, &row, &col); 5343 return row; 5344 } 5345 5346 /* 5347 * Update the position of the windows in frame "topfrp", using the width and 5348 * height of the frames. 5349 * "*row" and "*col" are the top-left position of the frame. They are updated 5350 * to the bottom-right position plus one. 5351 */ 5352 static void 5353 frame_comp_pos(frame_T *topfrp, int *row, int *col) 5354 { 5355 win_T *wp; 5356 frame_T *frp; 5357 int startcol; 5358 int startrow; 5359 int h; 5360 5361 wp = topfrp->fr_win; 5362 if (wp != NULL) 5363 { 5364 if (wp->w_winrow != *row || wp->w_wincol != *col) 5365 { 5366 // position changed, redraw 5367 wp->w_winrow = *row; 5368 wp->w_wincol = *col; 5369 redraw_win_later(wp, NOT_VALID); 5370 wp->w_redr_status = TRUE; 5371 } 5372 // WinBar will not show if the window height is zero 5373 h = VISIBLE_HEIGHT(wp) + wp->w_status_height; 5374 *row += h > topfrp->fr_height ? topfrp->fr_height : h; 5375 *col += wp->w_width + wp->w_vsep_width; 5376 } 5377 else 5378 { 5379 startrow = *row; 5380 startcol = *col; 5381 FOR_ALL_FRAMES(frp, topfrp->fr_child) 5382 { 5383 if (topfrp->fr_layout == FR_ROW) 5384 *row = startrow; // all frames are at the same row 5385 else 5386 *col = startcol; // all frames are at the same col 5387 frame_comp_pos(frp, row, col); 5388 } 5389 } 5390 } 5391 5392 /* 5393 * Set current window height and take care of repositioning other windows to 5394 * fit around it. 5395 */ 5396 void 5397 win_setheight(int height) 5398 { 5399 win_setheight_win(height, curwin); 5400 } 5401 5402 /* 5403 * Set the window height of window "win" and take care of repositioning other 5404 * windows to fit around it. 5405 */ 5406 void 5407 win_setheight_win(int height, win_T *win) 5408 { 5409 int row; 5410 5411 if (win == curwin) 5412 { 5413 // Always keep current window at least one line high, even when 5414 // 'winminheight' is zero. 5415 if (height < p_wmh) 5416 height = p_wmh; 5417 if (height == 0) 5418 height = 1; 5419 height += WINBAR_HEIGHT(curwin); 5420 } 5421 5422 frame_setheight(win->w_frame, height + win->w_status_height); 5423 5424 // recompute the window positions 5425 row = win_comp_pos(); 5426 5427 /* 5428 * If there is extra space created between the last window and the command 5429 * line, clear it. 5430 */ 5431 if (full_screen && msg_scrolled == 0 && row < cmdline_row) 5432 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0); 5433 cmdline_row = row; 5434 msg_row = row; 5435 msg_col = 0; 5436 5437 redraw_all_later(NOT_VALID); 5438 } 5439 5440 /* 5441 * Set the height of a frame to "height" and take care that all frames and 5442 * windows inside it are resized. Also resize frames on the left and right if 5443 * the are in the same FR_ROW frame. 5444 * 5445 * Strategy: 5446 * If the frame is part of a FR_COL frame, try fitting the frame in that 5447 * frame. If that doesn't work (the FR_COL frame is too small), recursively 5448 * go to containing frames to resize them and make room. 5449 * If the frame is part of a FR_ROW frame, all frames must be resized as well. 5450 * Check for the minimal height of the FR_ROW frame. 5451 * At the top level we can also use change the command line height. 5452 */ 5453 static void 5454 frame_setheight(frame_T *curfrp, int height) 5455 { 5456 int room; // total number of lines available 5457 int take; // number of lines taken from other windows 5458 int room_cmdline; // lines available from cmdline 5459 int run; 5460 frame_T *frp; 5461 int h; 5462 int room_reserved; 5463 5464 // If the height already is the desired value, nothing to do. 5465 if (curfrp->fr_height == height) 5466 return; 5467 5468 if (curfrp->fr_parent == NULL) 5469 { 5470 // topframe: can only change the command line 5471 if (height > ROWS_AVAIL) 5472 height = ROWS_AVAIL; 5473 if (height > 0) 5474 frame_new_height(curfrp, height, FALSE, FALSE); 5475 } 5476 else if (curfrp->fr_parent->fr_layout == FR_ROW) 5477 { 5478 // Row of frames: Also need to resize frames left and right of this 5479 // one. First check for the minimal height of these. 5480 h = frame_minheight(curfrp->fr_parent, NULL); 5481 if (height < h) 5482 height = h; 5483 frame_setheight(curfrp->fr_parent, height); 5484 } 5485 else 5486 { 5487 /* 5488 * Column of frames: try to change only frames in this column. 5489 */ 5490 /* 5491 * Do this twice: 5492 * 1: compute room available, if it's not enough try resizing the 5493 * containing frame. 5494 * 2: compute the room available and adjust the height to it. 5495 * Try not to reduce the height of a window with 'winfixheight' set. 5496 */ 5497 for (run = 1; run <= 2; ++run) 5498 { 5499 room = 0; 5500 room_reserved = 0; 5501 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child) 5502 { 5503 if (frp != curfrp 5504 && frp->fr_win != NULL 5505 && frp->fr_win->w_p_wfh) 5506 room_reserved += frp->fr_height; 5507 room += frp->fr_height; 5508 if (frp != curfrp) 5509 room -= frame_minheight(frp, NULL); 5510 } 5511 if (curfrp->fr_width != Columns) 5512 room_cmdline = 0; 5513 else 5514 { 5515 room_cmdline = Rows - p_ch - (lastwin->w_winrow 5516 + VISIBLE_HEIGHT(lastwin) 5517 + lastwin->w_status_height); 5518 if (room_cmdline < 0) 5519 room_cmdline = 0; 5520 } 5521 5522 if (height <= room + room_cmdline) 5523 break; 5524 if (run == 2 || curfrp->fr_width == Columns) 5525 { 5526 if (height > room + room_cmdline) 5527 height = room + room_cmdline; 5528 break; 5529 } 5530 frame_setheight(curfrp->fr_parent, height 5531 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1); 5532 } 5533 5534 /* 5535 * Compute the number of lines we will take from others frames (can be 5536 * negative!). 5537 */ 5538 take = height - curfrp->fr_height; 5539 5540 // If there is not enough room, also reduce the height of a window 5541 // with 'winfixheight' set. 5542 if (height > room + room_cmdline - room_reserved) 5543 room_reserved = room + room_cmdline - height; 5544 // If there is only a 'winfixheight' window and making the 5545 // window smaller, need to make the other window taller. 5546 if (take < 0 && room - curfrp->fr_height < room_reserved) 5547 room_reserved = 0; 5548 5549 if (take > 0 && room_cmdline > 0) 5550 { 5551 // use lines from cmdline first 5552 if (take < room_cmdline) 5553 room_cmdline = take; 5554 take -= room_cmdline; 5555 topframe->fr_height += room_cmdline; 5556 } 5557 5558 /* 5559 * set the current frame to the new height 5560 */ 5561 frame_new_height(curfrp, height, FALSE, FALSE); 5562 5563 /* 5564 * First take lines from the frames after the current frame. If 5565 * that is not enough, takes lines from frames above the current 5566 * frame. 5567 */ 5568 for (run = 0; run < 2; ++run) 5569 { 5570 if (run == 0) 5571 frp = curfrp->fr_next; // 1st run: start with next window 5572 else 5573 frp = curfrp->fr_prev; // 2nd run: start with prev window 5574 while (frp != NULL && take != 0) 5575 { 5576 h = frame_minheight(frp, NULL); 5577 if (room_reserved > 0 5578 && frp->fr_win != NULL 5579 && frp->fr_win->w_p_wfh) 5580 { 5581 if (room_reserved >= frp->fr_height) 5582 room_reserved -= frp->fr_height; 5583 else 5584 { 5585 if (frp->fr_height - room_reserved > take) 5586 room_reserved = frp->fr_height - take; 5587 take -= frp->fr_height - room_reserved; 5588 frame_new_height(frp, room_reserved, FALSE, FALSE); 5589 room_reserved = 0; 5590 } 5591 } 5592 else 5593 { 5594 if (frp->fr_height - take < h) 5595 { 5596 take -= frp->fr_height - h; 5597 frame_new_height(frp, h, FALSE, FALSE); 5598 } 5599 else 5600 { 5601 frame_new_height(frp, frp->fr_height - take, 5602 FALSE, FALSE); 5603 take = 0; 5604 } 5605 } 5606 if (run == 0) 5607 frp = frp->fr_next; 5608 else 5609 frp = frp->fr_prev; 5610 } 5611 } 5612 } 5613 } 5614 5615 /* 5616 * Set current window width and take care of repositioning other windows to 5617 * fit around it. 5618 */ 5619 void 5620 win_setwidth(int width) 5621 { 5622 win_setwidth_win(width, curwin); 5623 } 5624 5625 void 5626 win_setwidth_win(int width, win_T *wp) 5627 { 5628 // Always keep current window at least one column wide, even when 5629 // 'winminwidth' is zero. 5630 if (wp == curwin) 5631 { 5632 if (width < p_wmw) 5633 width = p_wmw; 5634 if (width == 0) 5635 width = 1; 5636 } 5637 5638 frame_setwidth(wp->w_frame, width + wp->w_vsep_width); 5639 5640 // recompute the window positions 5641 (void)win_comp_pos(); 5642 5643 redraw_all_later(NOT_VALID); 5644 } 5645 5646 /* 5647 * Set the width of a frame to "width" and take care that all frames and 5648 * windows inside it are resized. Also resize frames above and below if the 5649 * are in the same FR_ROW frame. 5650 * 5651 * Strategy is similar to frame_setheight(). 5652 */ 5653 static void 5654 frame_setwidth(frame_T *curfrp, int width) 5655 { 5656 int room; // total number of lines available 5657 int take; // number of lines taken from other windows 5658 int run; 5659 frame_T *frp; 5660 int w; 5661 int room_reserved; 5662 5663 // If the width already is the desired value, nothing to do. 5664 if (curfrp->fr_width == width) 5665 return; 5666 5667 if (curfrp->fr_parent == NULL) 5668 // topframe: can't change width 5669 return; 5670 5671 if (curfrp->fr_parent->fr_layout == FR_COL) 5672 { 5673 // Column of frames: Also need to resize frames above and below of 5674 // this one. First check for the minimal width of these. 5675 w = frame_minwidth(curfrp->fr_parent, NULL); 5676 if (width < w) 5677 width = w; 5678 frame_setwidth(curfrp->fr_parent, width); 5679 } 5680 else 5681 { 5682 /* 5683 * Row of frames: try to change only frames in this row. 5684 * 5685 * Do this twice: 5686 * 1: compute room available, if it's not enough try resizing the 5687 * containing frame. 5688 * 2: compute the room available and adjust the width to it. 5689 */ 5690 for (run = 1; run <= 2; ++run) 5691 { 5692 room = 0; 5693 room_reserved = 0; 5694 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child) 5695 { 5696 if (frp != curfrp 5697 && frp->fr_win != NULL 5698 && frp->fr_win->w_p_wfw) 5699 room_reserved += frp->fr_width; 5700 room += frp->fr_width; 5701 if (frp != curfrp) 5702 room -= frame_minwidth(frp, NULL); 5703 } 5704 5705 if (width <= room) 5706 break; 5707 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL) 5708 { 5709 if (width > room) 5710 width = room; 5711 break; 5712 } 5713 frame_setwidth(curfrp->fr_parent, width 5714 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1); 5715 } 5716 5717 /* 5718 * Compute the number of lines we will take from others frames (can be 5719 * negative!). 5720 */ 5721 take = width - curfrp->fr_width; 5722 5723 // If there is not enough room, also reduce the width of a window 5724 // with 'winfixwidth' set. 5725 if (width > room - room_reserved) 5726 room_reserved = room - width; 5727 // If there is only a 'winfixwidth' window and making the 5728 // window smaller, need to make the other window narrower. 5729 if (take < 0 && room - curfrp->fr_width < room_reserved) 5730 room_reserved = 0; 5731 5732 /* 5733 * set the current frame to the new width 5734 */ 5735 frame_new_width(curfrp, width, FALSE, FALSE); 5736 5737 /* 5738 * First take lines from the frames right of the current frame. If 5739 * that is not enough, takes lines from frames left of the current 5740 * frame. 5741 */ 5742 for (run = 0; run < 2; ++run) 5743 { 5744 if (run == 0) 5745 frp = curfrp->fr_next; // 1st run: start with next window 5746 else 5747 frp = curfrp->fr_prev; // 2nd run: start with prev window 5748 while (frp != NULL && take != 0) 5749 { 5750 w = frame_minwidth(frp, NULL); 5751 if (room_reserved > 0 5752 && frp->fr_win != NULL 5753 && frp->fr_win->w_p_wfw) 5754 { 5755 if (room_reserved >= frp->fr_width) 5756 room_reserved -= frp->fr_width; 5757 else 5758 { 5759 if (frp->fr_width - room_reserved > take) 5760 room_reserved = frp->fr_width - take; 5761 take -= frp->fr_width - room_reserved; 5762 frame_new_width(frp, room_reserved, FALSE, FALSE); 5763 room_reserved = 0; 5764 } 5765 } 5766 else 5767 { 5768 if (frp->fr_width - take < w) 5769 { 5770 take -= frp->fr_width - w; 5771 frame_new_width(frp, w, FALSE, FALSE); 5772 } 5773 else 5774 { 5775 frame_new_width(frp, frp->fr_width - take, 5776 FALSE, FALSE); 5777 take = 0; 5778 } 5779 } 5780 if (run == 0) 5781 frp = frp->fr_next; 5782 else 5783 frp = frp->fr_prev; 5784 } 5785 } 5786 } 5787 } 5788 5789 /* 5790 * Check 'winminheight' for a valid value and reduce it if needed. 5791 */ 5792 void 5793 win_setminheight(void) 5794 { 5795 int room; 5796 int needed; 5797 int first = TRUE; 5798 5799 // loop until there is a 'winminheight' that is possible 5800 while (p_wmh > 0) 5801 { 5802 room = Rows - p_ch; 5803 needed = frame_minheight(topframe, NULL); 5804 if (room >= needed) 5805 break; 5806 --p_wmh; 5807 if (first) 5808 { 5809 emsg(_(e_noroom)); 5810 first = FALSE; 5811 } 5812 } 5813 } 5814 5815 /* 5816 * Check 'winminwidth' for a valid value and reduce it if needed. 5817 */ 5818 void 5819 win_setminwidth(void) 5820 { 5821 int room; 5822 int needed; 5823 int first = TRUE; 5824 5825 // loop until there is a 'winminheight' that is possible 5826 while (p_wmw > 0) 5827 { 5828 room = Columns; 5829 needed = frame_minwidth(topframe, NULL); 5830 if (room >= needed) 5831 break; 5832 --p_wmw; 5833 if (first) 5834 { 5835 emsg(_(e_noroom)); 5836 first = FALSE; 5837 } 5838 } 5839 } 5840 5841 /* 5842 * Status line of dragwin is dragged "offset" lines down (negative is up). 5843 */ 5844 void 5845 win_drag_status_line(win_T *dragwin, int offset) 5846 { 5847 frame_T *curfr; 5848 frame_T *fr; 5849 int room; 5850 int row; 5851 int up; // if TRUE, drag status line up, otherwise down 5852 int n; 5853 5854 fr = dragwin->w_frame; 5855 curfr = fr; 5856 if (fr != topframe) // more than one window 5857 { 5858 fr = fr->fr_parent; 5859 // When the parent frame is not a column of frames, its parent should 5860 // be. 5861 if (fr->fr_layout != FR_COL) 5862 { 5863 curfr = fr; 5864 if (fr != topframe) // only a row of windows, may drag statusline 5865 fr = fr->fr_parent; 5866 } 5867 } 5868 5869 // If this is the last frame in a column, may want to resize the parent 5870 // frame instead (go two up to skip a row of frames). 5871 while (curfr != topframe && curfr->fr_next == NULL) 5872 { 5873 if (fr != topframe) 5874 fr = fr->fr_parent; 5875 curfr = fr; 5876 if (fr != topframe) 5877 fr = fr->fr_parent; 5878 } 5879 5880 if (offset < 0) // drag up 5881 { 5882 up = TRUE; 5883 offset = -offset; 5884 // sum up the room of the current frame and above it 5885 if (fr == curfr) 5886 { 5887 // only one window 5888 room = fr->fr_height - frame_minheight(fr, NULL); 5889 } 5890 else 5891 { 5892 room = 0; 5893 for (fr = fr->fr_child; ; fr = fr->fr_next) 5894 { 5895 room += fr->fr_height - frame_minheight(fr, NULL); 5896 if (fr == curfr) 5897 break; 5898 } 5899 } 5900 fr = curfr->fr_next; // put fr at frame that grows 5901 } 5902 else // drag down 5903 { 5904 up = FALSE; 5905 /* 5906 * Only dragging the last status line can reduce p_ch. 5907 */ 5908 room = Rows - cmdline_row; 5909 if (curfr->fr_next == NULL) 5910 room -= 1; 5911 else 5912 room -= p_ch; 5913 if (room < 0) 5914 room = 0; 5915 // sum up the room of frames below of the current one 5916 FOR_ALL_FRAMES(fr, curfr->fr_next) 5917 room += fr->fr_height - frame_minheight(fr, NULL); 5918 fr = curfr; // put fr at window that grows 5919 } 5920 5921 if (room < offset) // Not enough room 5922 offset = room; // Move as far as we can 5923 if (offset <= 0) 5924 return; 5925 5926 /* 5927 * Grow frame fr by "offset" lines. 5928 * Doesn't happen when dragging the last status line up. 5929 */ 5930 if (fr != NULL) 5931 frame_new_height(fr, fr->fr_height + offset, up, FALSE); 5932 5933 if (up) 5934 fr = curfr; // current frame gets smaller 5935 else 5936 fr = curfr->fr_next; // next frame gets smaller 5937 5938 /* 5939 * Now make the other frames smaller. 5940 */ 5941 while (fr != NULL && offset > 0) 5942 { 5943 n = frame_minheight(fr, NULL); 5944 if (fr->fr_height - offset <= n) 5945 { 5946 offset -= fr->fr_height - n; 5947 frame_new_height(fr, n, !up, FALSE); 5948 } 5949 else 5950 { 5951 frame_new_height(fr, fr->fr_height - offset, !up, FALSE); 5952 break; 5953 } 5954 if (up) 5955 fr = fr->fr_prev; 5956 else 5957 fr = fr->fr_next; 5958 } 5959 row = win_comp_pos(); 5960 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0); 5961 cmdline_row = row; 5962 p_ch = Rows - cmdline_row; 5963 if (p_ch < 1) 5964 p_ch = 1; 5965 curtab->tp_ch_used = p_ch; 5966 redraw_all_later(SOME_VALID); 5967 showmode(); 5968 } 5969 5970 /* 5971 * Separator line of dragwin is dragged "offset" lines right (negative is left). 5972 */ 5973 void 5974 win_drag_vsep_line(win_T *dragwin, int offset) 5975 { 5976 frame_T *curfr; 5977 frame_T *fr; 5978 int room; 5979 int left; // if TRUE, drag separator line left, otherwise right 5980 int n; 5981 5982 fr = dragwin->w_frame; 5983 if (fr == topframe) // only one window (cannot happen?) 5984 return; 5985 curfr = fr; 5986 fr = fr->fr_parent; 5987 // When the parent frame is not a row of frames, its parent should be. 5988 if (fr->fr_layout != FR_ROW) 5989 { 5990 if (fr == topframe) // only a column of windows (cannot happen?) 5991 return; 5992 curfr = fr; 5993 fr = fr->fr_parent; 5994 } 5995 5996 // If this is the last frame in a row, may want to resize a parent 5997 // frame instead. 5998 while (curfr->fr_next == NULL) 5999 { 6000 if (fr == topframe) 6001 break; 6002 curfr = fr; 6003 fr = fr->fr_parent; 6004 if (fr != topframe) 6005 { 6006 curfr = fr; 6007 fr = fr->fr_parent; 6008 } 6009 } 6010 6011 if (offset < 0) // drag left 6012 { 6013 left = TRUE; 6014 offset = -offset; 6015 // sum up the room of the current frame and left of it 6016 room = 0; 6017 for (fr = fr->fr_child; ; fr = fr->fr_next) 6018 { 6019 room += fr->fr_width - frame_minwidth(fr, NULL); 6020 if (fr == curfr) 6021 break; 6022 } 6023 fr = curfr->fr_next; // put fr at frame that grows 6024 } 6025 else // drag right 6026 { 6027 left = FALSE; 6028 // sum up the room of frames right of the current one 6029 room = 0; 6030 FOR_ALL_FRAMES(fr, curfr->fr_next) 6031 room += fr->fr_width - frame_minwidth(fr, NULL); 6032 fr = curfr; // put fr at window that grows 6033 } 6034 6035 if (room < offset) // Not enough room 6036 offset = room; // Move as far as we can 6037 if (offset <= 0) // No room at all, quit. 6038 return; 6039 if (fr == NULL) 6040 return; // Safety check, should not happen. 6041 6042 // grow frame fr by offset lines 6043 frame_new_width(fr, fr->fr_width + offset, left, FALSE); 6044 6045 // shrink other frames: current and at the left or at the right 6046 if (left) 6047 fr = curfr; // current frame gets smaller 6048 else 6049 fr = curfr->fr_next; // next frame gets smaller 6050 6051 while (fr != NULL && offset > 0) 6052 { 6053 n = frame_minwidth(fr, NULL); 6054 if (fr->fr_width - offset <= n) 6055 { 6056 offset -= fr->fr_width - n; 6057 frame_new_width(fr, n, !left, FALSE); 6058 } 6059 else 6060 { 6061 frame_new_width(fr, fr->fr_width - offset, !left, FALSE); 6062 break; 6063 } 6064 if (left) 6065 fr = fr->fr_prev; 6066 else 6067 fr = fr->fr_next; 6068 } 6069 (void)win_comp_pos(); 6070 redraw_all_later(NOT_VALID); 6071 } 6072 6073 #define FRACTION_MULT 16384L 6074 6075 /* 6076 * Set wp->w_fraction for the current w_wrow and w_height. 6077 * Has no effect when the window is less than two lines. 6078 */ 6079 void 6080 set_fraction(win_T *wp) 6081 { 6082 if (wp->w_height > 1) 6083 // When cursor is in the first line the percentage is computed as if 6084 // it's halfway that line. Thus with two lines it is 25%, with three 6085 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc. 6086 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT 6087 + FRACTION_MULT / 2) / (long)wp->w_height; 6088 } 6089 6090 /* 6091 * Set the height of a window. 6092 * "height" excludes any window toolbar. 6093 * This takes care of the things inside the window, not what happens to the 6094 * window position, the frame or to other windows. 6095 */ 6096 void 6097 win_new_height(win_T *wp, int height) 6098 { 6099 int prev_height = wp->w_height; 6100 6101 // Don't want a negative height. Happens when splitting a tiny window. 6102 // Will equalize heights soon to fix it. 6103 if (height < 0) 6104 height = 0; 6105 if (wp->w_height == height) 6106 return; // nothing to do 6107 6108 if (wp->w_height > 0) 6109 { 6110 if (wp == curwin) 6111 // w_wrow needs to be valid. When setting 'laststatus' this may 6112 // call win_new_height() recursively. 6113 validate_cursor(); 6114 if (wp->w_height != prev_height) 6115 return; // Recursive call already changed the size, bail out here 6116 // to avoid the following to mess things up. 6117 if (wp->w_wrow != wp->w_prev_fraction_row) 6118 set_fraction(wp); 6119 } 6120 6121 wp->w_height = height; 6122 wp->w_skipcol = 0; 6123 6124 // There is no point in adjusting the scroll position when exiting. Some 6125 // values might be invalid. 6126 if (!exiting) 6127 scroll_to_fraction(wp, prev_height); 6128 } 6129 6130 void 6131 scroll_to_fraction(win_T *wp, int prev_height) 6132 { 6133 linenr_T lnum; 6134 int sline, line_size; 6135 int height = wp->w_height; 6136 6137 // Don't change w_topline in any of these cases: 6138 // - window height is 0 6139 // - 'scrollbind' is set and this isn't the current window 6140 // - window height is sufficient to display the whole buffer and first line 6141 // is visible. 6142 if (height > 0 6143 && (!wp->w_p_scb || wp == curwin) 6144 && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1)) 6145 { 6146 /* 6147 * Find a value for w_topline that shows the cursor at the same 6148 * relative position in the window as before (more or less). 6149 */ 6150 lnum = wp->w_cursor.lnum; 6151 if (lnum < 1) // can happen when starting up 6152 lnum = 1; 6153 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) 6154 / FRACTION_MULT; 6155 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1; 6156 sline = wp->w_wrow - line_size; 6157 6158 if (sline >= 0) 6159 { 6160 // Make sure the whole cursor line is visible, if possible. 6161 int rows = plines_win(wp, lnum, FALSE); 6162 6163 if (sline > wp->w_height - rows) 6164 { 6165 sline = wp->w_height - rows; 6166 wp->w_wrow -= rows - line_size; 6167 } 6168 } 6169 6170 if (sline < 0) 6171 { 6172 /* 6173 * Cursor line would go off top of screen if w_wrow was this high. 6174 * Make cursor line the first line in the window. If not enough 6175 * room use w_skipcol; 6176 */ 6177 wp->w_wrow = line_size; 6178 if (wp->w_wrow >= wp->w_height 6179 && (wp->w_width - win_col_off(wp)) > 0) 6180 { 6181 wp->w_skipcol += wp->w_width - win_col_off(wp); 6182 --wp->w_wrow; 6183 while (wp->w_wrow >= wp->w_height) 6184 { 6185 wp->w_skipcol += wp->w_width - win_col_off(wp) 6186 + win_col_off2(wp); 6187 --wp->w_wrow; 6188 } 6189 } 6190 } 6191 else if (sline > 0) 6192 { 6193 while (sline > 0 && lnum > 1) 6194 { 6195 #ifdef FEAT_FOLDING 6196 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL); 6197 if (lnum == 1) 6198 { 6199 // first line in buffer is folded 6200 line_size = 1; 6201 --sline; 6202 break; 6203 } 6204 #endif 6205 --lnum; 6206 #ifdef FEAT_DIFF 6207 if (lnum == wp->w_topline) 6208 line_size = plines_win_nofill(wp, lnum, TRUE) 6209 + wp->w_topfill; 6210 else 6211 #endif 6212 line_size = plines_win(wp, lnum, TRUE); 6213 sline -= line_size; 6214 } 6215 6216 if (sline < 0) 6217 { 6218 /* 6219 * Line we want at top would go off top of screen. Use next 6220 * line instead. 6221 */ 6222 #ifdef FEAT_FOLDING 6223 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL); 6224 #endif 6225 lnum++; 6226 wp->w_wrow -= line_size + sline; 6227 } 6228 else if (sline > 0) 6229 { 6230 // First line of file reached, use that as topline. 6231 lnum = 1; 6232 wp->w_wrow -= sline; 6233 } 6234 } 6235 set_topline(wp, lnum); 6236 } 6237 6238 if (wp == curwin) 6239 { 6240 if (get_scrolloff_value()) 6241 update_topline(); 6242 curs_columns(FALSE); // validate w_wrow 6243 } 6244 if (prev_height > 0) 6245 wp->w_prev_fraction_row = wp->w_wrow; 6246 6247 win_comp_scroll(wp); 6248 redraw_win_later(wp, SOME_VALID); 6249 wp->w_redr_status = TRUE; 6250 invalidate_botline_win(wp); 6251 } 6252 6253 /* 6254 * Set the width of a window. 6255 */ 6256 void 6257 win_new_width(win_T *wp, int width) 6258 { 6259 wp->w_width = width; 6260 wp->w_lines_valid = 0; 6261 changed_line_abv_curs_win(wp); 6262 invalidate_botline_win(wp); 6263 if (wp == curwin) 6264 { 6265 update_topline(); 6266 curs_columns(TRUE); // validate w_wrow 6267 } 6268 redraw_win_later(wp, NOT_VALID); 6269 wp->w_redr_status = TRUE; 6270 } 6271 6272 void 6273 win_comp_scroll(win_T *wp) 6274 { 6275 wp->w_p_scr = ((unsigned)wp->w_height >> 1); 6276 if (wp->w_p_scr == 0) 6277 wp->w_p_scr = 1; 6278 } 6279 6280 /* 6281 * command_height: called whenever p_ch has been changed 6282 */ 6283 void 6284 command_height(void) 6285 { 6286 int h; 6287 frame_T *frp; 6288 int old_p_ch = curtab->tp_ch_used; 6289 6290 // Use the value of p_ch that we remembered. This is needed for when the 6291 // GUI starts up, we can't be sure in what order things happen. And when 6292 // p_ch was changed in another tab page. 6293 curtab->tp_ch_used = p_ch; 6294 6295 // Find bottom frame with width of screen. 6296 frp = lastwin->w_frame; 6297 while (frp->fr_width != Columns && frp->fr_parent != NULL) 6298 frp = frp->fr_parent; 6299 6300 // Avoid changing the height of a window with 'winfixheight' set. 6301 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF 6302 && frp->fr_win->w_p_wfh) 6303 frp = frp->fr_prev; 6304 6305 if (starting != NO_SCREEN) 6306 { 6307 cmdline_row = Rows - p_ch; 6308 6309 if (p_ch > old_p_ch) // p_ch got bigger 6310 { 6311 while (p_ch > old_p_ch) 6312 { 6313 if (frp == NULL) 6314 { 6315 emsg(_(e_noroom)); 6316 p_ch = old_p_ch; 6317 curtab->tp_ch_used = p_ch; 6318 cmdline_row = Rows - p_ch; 6319 break; 6320 } 6321 h = frp->fr_height - frame_minheight(frp, NULL); 6322 if (h > p_ch - old_p_ch) 6323 h = p_ch - old_p_ch; 6324 old_p_ch += h; 6325 frame_add_height(frp, -h); 6326 frp = frp->fr_prev; 6327 } 6328 6329 // Recompute window positions. 6330 (void)win_comp_pos(); 6331 6332 // clear the lines added to cmdline 6333 if (full_screen) 6334 screen_fill((int)(cmdline_row), (int)Rows, 0, 6335 (int)Columns, ' ', ' ', 0); 6336 msg_row = cmdline_row; 6337 redraw_cmdline = TRUE; 6338 return; 6339 } 6340 6341 if (msg_row < cmdline_row) 6342 msg_row = cmdline_row; 6343 redraw_cmdline = TRUE; 6344 } 6345 frame_add_height(frp, (int)(old_p_ch - p_ch)); 6346 6347 // Recompute window positions. 6348 if (frp != lastwin->w_frame) 6349 (void)win_comp_pos(); 6350 } 6351 6352 /* 6353 * Resize frame "frp" to be "n" lines higher (negative for less high). 6354 * Also resize the frames it is contained in. 6355 */ 6356 static void 6357 frame_add_height(frame_T *frp, int n) 6358 { 6359 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE); 6360 for (;;) 6361 { 6362 frp = frp->fr_parent; 6363 if (frp == NULL) 6364 break; 6365 frp->fr_height += n; 6366 } 6367 } 6368 6369 /* 6370 * Add or remove a status line for the bottom window(s), according to the 6371 * value of 'laststatus'. 6372 */ 6373 void 6374 last_status( 6375 int morewin) // pretend there are two or more windows 6376 { 6377 // Don't make a difference between horizontal or vertical split. 6378 last_status_rec(topframe, (p_ls == 2 6379 || (p_ls == 1 && (morewin || !ONE_WINDOW)))); 6380 } 6381 6382 static void 6383 last_status_rec(frame_T *fr, int statusline) 6384 { 6385 frame_T *fp; 6386 win_T *wp; 6387 6388 if (fr->fr_layout == FR_LEAF) 6389 { 6390 wp = fr->fr_win; 6391 if (wp->w_status_height != 0 && !statusline) 6392 { 6393 // remove status line 6394 win_new_height(wp, wp->w_height + 1); 6395 wp->w_status_height = 0; 6396 comp_col(); 6397 } 6398 else if (wp->w_status_height == 0 && statusline) 6399 { 6400 // Find a frame to take a line from. 6401 fp = fr; 6402 while (fp->fr_height <= frame_minheight(fp, NULL)) 6403 { 6404 if (fp == topframe) 6405 { 6406 emsg(_(e_noroom)); 6407 return; 6408 } 6409 // In a column of frames: go to frame above. If already at 6410 // the top or in a row of frames: go to parent. 6411 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL) 6412 fp = fp->fr_prev; 6413 else 6414 fp = fp->fr_parent; 6415 } 6416 wp->w_status_height = 1; 6417 if (fp != fr) 6418 { 6419 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE); 6420 frame_fix_height(wp); 6421 (void)win_comp_pos(); 6422 } 6423 else 6424 win_new_height(wp, wp->w_height - 1); 6425 comp_col(); 6426 redraw_all_later(SOME_VALID); 6427 } 6428 } 6429 else if (fr->fr_layout == FR_ROW) 6430 { 6431 // vertically split windows, set status line for each one 6432 FOR_ALL_FRAMES(fp, fr->fr_child) 6433 last_status_rec(fp, statusline); 6434 } 6435 else 6436 { 6437 // horizontally split window, set status line for last one 6438 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next) 6439 ; 6440 last_status_rec(fp, statusline); 6441 } 6442 } 6443 6444 /* 6445 * Return the number of lines used by the tab page line. 6446 */ 6447 int 6448 tabline_height(void) 6449 { 6450 #ifdef FEAT_GUI_TABLINE 6451 // When the GUI has the tabline then this always returns zero. 6452 if (gui_use_tabline()) 6453 return 0; 6454 #endif 6455 switch (p_stal) 6456 { 6457 case 0: return 0; 6458 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1; 6459 } 6460 return 1; 6461 } 6462 6463 /* 6464 * Return the minimal number of rows that is needed on the screen to display 6465 * the current number of windows. 6466 */ 6467 int 6468 min_rows(void) 6469 { 6470 int total; 6471 tabpage_T *tp; 6472 int n; 6473 6474 if (firstwin == NULL) // not initialized yet 6475 return MIN_LINES; 6476 6477 total = 0; 6478 FOR_ALL_TABPAGES(tp) 6479 { 6480 n = frame_minheight(tp->tp_topframe, NULL); 6481 if (total < n) 6482 total = n; 6483 } 6484 total += tabline_height(); 6485 total += 1; // count the room for the command line 6486 return total; 6487 } 6488 6489 /* 6490 * Return TRUE if there is only one window and only one tab page, not 6491 * counting a help or preview window, unless it is the current window. 6492 * Does not count unlisted windows. 6493 */ 6494 int 6495 only_one_window(void) 6496 { 6497 int count = 0; 6498 win_T *wp; 6499 6500 #if defined(FEAT_PROP_POPUP) 6501 // If the current window is a popup then there always is another window. 6502 if (popup_is_popup(curwin)) 6503 return FALSE; 6504 #endif 6505 6506 // If there is another tab page there always is another window. 6507 if (first_tabpage->tp_next != NULL) 6508 return FALSE; 6509 6510 FOR_ALL_WINDOWS(wp) 6511 if (wp->w_buffer != NULL 6512 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf)) 6513 # ifdef FEAT_QUICKFIX 6514 || wp->w_p_pvw 6515 # endif 6516 ) || wp == curwin) && wp != aucmd_win) 6517 ++count; 6518 return (count <= 1); 6519 } 6520 6521 /* 6522 * Correct the cursor line number in other windows. Used after changing the 6523 * current buffer, and before applying autocommands. 6524 * When "do_curwin" is TRUE, also check current window. 6525 */ 6526 void 6527 check_lnums(int do_curwin) 6528 { 6529 win_T *wp; 6530 tabpage_T *tp; 6531 6532 FOR_ALL_TAB_WINDOWS(tp, wp) 6533 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf) 6534 { 6535 // save the original cursor position and topline 6536 wp->w_save_cursor.w_cursor_save = wp->w_cursor; 6537 wp->w_save_cursor.w_topline_save = wp->w_topline; 6538 6539 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count) 6540 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count; 6541 if (wp->w_topline > curbuf->b_ml.ml_line_count) 6542 wp->w_topline = curbuf->b_ml.ml_line_count; 6543 6544 // save the corrected cursor position and topline 6545 wp->w_save_cursor.w_cursor_corr = wp->w_cursor; 6546 wp->w_save_cursor.w_topline_corr = wp->w_topline; 6547 } 6548 } 6549 6550 /* 6551 * Reset cursor and topline to its stored values from check_lnums(). 6552 * check_lnums() must have been called first! 6553 */ 6554 void 6555 reset_lnums() 6556 { 6557 win_T *wp; 6558 tabpage_T *tp; 6559 6560 FOR_ALL_TAB_WINDOWS(tp, wp) 6561 if (wp->w_buffer == curbuf) 6562 { 6563 // Restore the value if the autocommand didn't change it. 6564 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor)) 6565 wp->w_cursor = wp->w_save_cursor.w_cursor_save; 6566 if (wp->w_save_cursor.w_topline_corr == wp->w_topline) 6567 wp->w_topline = wp->w_save_cursor.w_topline_save; 6568 } 6569 } 6570 6571 /* 6572 * A snapshot of the window sizes, to restore them after closing the help 6573 * window. 6574 * Only these fields are used: 6575 * fr_layout 6576 * fr_width 6577 * fr_height 6578 * fr_next 6579 * fr_child 6580 * fr_win (only valid for the old curwin, NULL otherwise) 6581 */ 6582 6583 /* 6584 * Create a snapshot of the current frame sizes. 6585 */ 6586 void 6587 make_snapshot(int idx) 6588 { 6589 clear_snapshot(curtab, idx); 6590 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]); 6591 } 6592 6593 static void 6594 make_snapshot_rec(frame_T *fr, frame_T **frp) 6595 { 6596 *frp = ALLOC_CLEAR_ONE(frame_T); 6597 if (*frp == NULL) 6598 return; 6599 (*frp)->fr_layout = fr->fr_layout; 6600 (*frp)->fr_width = fr->fr_width; 6601 (*frp)->fr_height = fr->fr_height; 6602 if (fr->fr_next != NULL) 6603 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next)); 6604 if (fr->fr_child != NULL) 6605 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child)); 6606 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin) 6607 (*frp)->fr_win = curwin; 6608 } 6609 6610 /* 6611 * Remove any existing snapshot. 6612 */ 6613 static void 6614 clear_snapshot(tabpage_T *tp, int idx) 6615 { 6616 clear_snapshot_rec(tp->tp_snapshot[idx]); 6617 tp->tp_snapshot[idx] = NULL; 6618 } 6619 6620 static void 6621 clear_snapshot_rec(frame_T *fr) 6622 { 6623 if (fr != NULL) 6624 { 6625 clear_snapshot_rec(fr->fr_next); 6626 clear_snapshot_rec(fr->fr_child); 6627 vim_free(fr); 6628 } 6629 } 6630 6631 /* 6632 * Restore a previously created snapshot, if there is any. 6633 * This is only done if the screen size didn't change and the window layout is 6634 * still the same. 6635 */ 6636 void 6637 restore_snapshot( 6638 int idx, 6639 int close_curwin) // closing current window 6640 { 6641 win_T *wp; 6642 6643 if (curtab->tp_snapshot[idx] != NULL 6644 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width 6645 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height 6646 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK) 6647 { 6648 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe); 6649 win_comp_pos(); 6650 if (wp != NULL && close_curwin) 6651 win_goto(wp); 6652 redraw_all_later(NOT_VALID); 6653 } 6654 clear_snapshot(curtab, idx); 6655 } 6656 6657 /* 6658 * Check if frames "sn" and "fr" have the same layout, same following frames 6659 * and same children. And the window pointer is valid. 6660 */ 6661 static int 6662 check_snapshot_rec(frame_T *sn, frame_T *fr) 6663 { 6664 if (sn->fr_layout != fr->fr_layout 6665 || (sn->fr_next == NULL) != (fr->fr_next == NULL) 6666 || (sn->fr_child == NULL) != (fr->fr_child == NULL) 6667 || (sn->fr_next != NULL 6668 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL) 6669 || (sn->fr_child != NULL 6670 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL) 6671 || (sn->fr_win != NULL && !win_valid(sn->fr_win))) 6672 return FAIL; 6673 return OK; 6674 } 6675 6676 /* 6677 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all 6678 * following frames and children. 6679 * Returns a pointer to the old current window, or NULL. 6680 */ 6681 static win_T * 6682 restore_snapshot_rec(frame_T *sn, frame_T *fr) 6683 { 6684 win_T *wp = NULL; 6685 win_T *wp2; 6686 6687 fr->fr_height = sn->fr_height; 6688 fr->fr_width = sn->fr_width; 6689 if (fr->fr_layout == FR_LEAF) 6690 { 6691 frame_new_height(fr, fr->fr_height, FALSE, FALSE); 6692 frame_new_width(fr, fr->fr_width, FALSE, FALSE); 6693 wp = sn->fr_win; 6694 } 6695 if (sn->fr_next != NULL) 6696 { 6697 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next); 6698 if (wp2 != NULL) 6699 wp = wp2; 6700 } 6701 if (sn->fr_child != NULL) 6702 { 6703 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child); 6704 if (wp2 != NULL) 6705 wp = wp2; 6706 } 6707 return wp; 6708 } 6709 6710 #if defined(FEAT_GUI) || defined(PROTO) 6711 /* 6712 * Return TRUE if there is any vertically split window. 6713 */ 6714 int 6715 win_hasvertsplit(void) 6716 { 6717 frame_T *fr; 6718 6719 if (topframe->fr_layout == FR_ROW) 6720 return TRUE; 6721 6722 if (topframe->fr_layout == FR_COL) 6723 FOR_ALL_FRAMES(fr, topframe->fr_child) 6724 if (fr->fr_layout == FR_ROW) 6725 return TRUE; 6726 6727 return FALSE; 6728 } 6729 #endif 6730 6731 #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO) 6732 int 6733 get_win_number(win_T *wp, win_T *first_win) 6734 { 6735 int i = 1; 6736 win_T *w; 6737 6738 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w)) 6739 ++i; 6740 6741 if (w == NULL) 6742 return 0; 6743 else 6744 return i; 6745 } 6746 6747 int 6748 get_tab_number(tabpage_T *tp UNUSED) 6749 { 6750 int i = 1; 6751 tabpage_T *t; 6752 6753 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next) 6754 ++i; 6755 6756 if (t == NULL) 6757 return 0; 6758 else 6759 return i; 6760 } 6761 #endif 6762 6763 /* 6764 * Return TRUE if "topfrp" and its children are at the right height. 6765 */ 6766 static int 6767 frame_check_height(frame_T *topfrp, int height) 6768 { 6769 frame_T *frp; 6770 6771 if (topfrp->fr_height != height) 6772 return FALSE; 6773 6774 if (topfrp->fr_layout == FR_ROW) 6775 FOR_ALL_FRAMES(frp, topfrp->fr_child) 6776 if (frp->fr_height != height) 6777 return FALSE; 6778 6779 return TRUE; 6780 } 6781 6782 /* 6783 * Return TRUE if "topfrp" and its children are at the right width. 6784 */ 6785 static int 6786 frame_check_width(frame_T *topfrp, int width) 6787 { 6788 frame_T *frp; 6789 6790 if (topfrp->fr_width != width) 6791 return FALSE; 6792 6793 if (topfrp->fr_layout == FR_COL) 6794 FOR_ALL_FRAMES(frp, topfrp->fr_child) 6795 if (frp->fr_width != width) 6796 return FALSE; 6797 6798 return TRUE; 6799 } 6800 6801 #if defined(FEAT_SYN_HL) || defined(PROTO) 6802 /* 6803 * Simple int comparison function for use with qsort() 6804 */ 6805 static int 6806 int_cmp(const void *a, const void *b) 6807 { 6808 return *(const int *)a - *(const int *)b; 6809 } 6810 6811 /* 6812 * Handle setting 'colorcolumn' or 'textwidth' in window "wp". 6813 * Returns error message, NULL if it's OK. 6814 */ 6815 char * 6816 check_colorcolumn(win_T *wp) 6817 { 6818 char_u *s; 6819 int col; 6820 int count = 0; 6821 int color_cols[256]; 6822 int i; 6823 int j = 0; 6824 6825 if (wp->w_buffer == NULL) 6826 return NULL; // buffer was closed 6827 6828 for (s = wp->w_p_cc; *s != NUL && count < 255;) 6829 { 6830 if (*s == '-' || *s == '+') 6831 { 6832 // -N and +N: add to 'textwidth' 6833 col = (*s == '-') ? -1 : 1; 6834 ++s; 6835 if (!VIM_ISDIGIT(*s)) 6836 return e_invarg; 6837 col = col * getdigits(&s); 6838 if (wp->w_buffer->b_p_tw == 0) 6839 goto skip; // 'textwidth' not set, skip this item 6840 col += wp->w_buffer->b_p_tw; 6841 if (col < 0) 6842 goto skip; 6843 } 6844 else if (VIM_ISDIGIT(*s)) 6845 col = getdigits(&s); 6846 else 6847 return e_invarg; 6848 color_cols[count++] = col - 1; // 1-based to 0-based 6849 skip: 6850 if (*s == NUL) 6851 break; 6852 if (*s != ',') 6853 return e_invarg; 6854 if (*++s == NUL) 6855 return e_invarg; // illegal trailing comma as in "set cc=80," 6856 } 6857 6858 vim_free(wp->w_p_cc_cols); 6859 if (count == 0) 6860 wp->w_p_cc_cols = NULL; 6861 else 6862 { 6863 wp->w_p_cc_cols = ALLOC_MULT(int, count + 1); 6864 if (wp->w_p_cc_cols != NULL) 6865 { 6866 // sort the columns for faster usage on screen redraw inside 6867 // win_line() 6868 qsort(color_cols, count, sizeof(int), int_cmp); 6869 6870 for (i = 0; i < count; ++i) 6871 // skip duplicates 6872 if (j == 0 || wp->w_p_cc_cols[j - 1] != color_cols[i]) 6873 wp->w_p_cc_cols[j++] = color_cols[i]; 6874 wp->w_p_cc_cols[j] = -1; // end marker 6875 } 6876 } 6877 6878 return NULL; // no error 6879 } 6880 #endif 6881