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.cmod_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.cmod_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 3572 || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write) 3573 { 3574 dialog_changed(wp->w_buffer, FALSE); 3575 if (!win_valid(wp)) // autocommands messed wp up 3576 { 3577 nextwp = firstwin; 3578 continue; 3579 } 3580 } 3581 if (bufIsChanged(wp->w_buffer)) 3582 #endif 3583 continue; 3584 } 3585 win_close(wp, !buf_hide(wp->w_buffer) 3586 && !bufIsChanged(wp->w_buffer)); 3587 } 3588 } 3589 3590 if (message && !ONE_WINDOW) 3591 emsg(_("E445: Other window contains changes")); 3592 } 3593 3594 static void 3595 win_init_empty(win_T *wp) 3596 { 3597 redraw_win_later(wp, NOT_VALID); 3598 wp->w_lines_valid = 0; 3599 wp->w_cursor.lnum = 1; 3600 wp->w_curswant = wp->w_cursor.col = 0; 3601 wp->w_cursor.coladd = 0; 3602 wp->w_pcmark.lnum = 1; // pcmark not cleared but set to line 1 3603 wp->w_pcmark.col = 0; 3604 wp->w_prev_pcmark.lnum = 0; 3605 wp->w_prev_pcmark.col = 0; 3606 wp->w_topline = 1; 3607 #ifdef FEAT_DIFF 3608 wp->w_topfill = 0; 3609 #endif 3610 wp->w_botline = 2; 3611 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) 3612 wp->w_s = &wp->w_buffer->b_s; 3613 #endif 3614 } 3615 3616 /* 3617 * Init the current window "curwin". 3618 * Called when a new file is being edited. 3619 */ 3620 void 3621 curwin_init(void) 3622 { 3623 win_init_empty(curwin); 3624 } 3625 3626 /* 3627 * Allocate the first window and put an empty buffer in it. 3628 * Called from main(). 3629 * Return FAIL when something goes wrong (out of memory). 3630 */ 3631 int 3632 win_alloc_first(void) 3633 { 3634 if (win_alloc_firstwin(NULL) == FAIL) 3635 return FAIL; 3636 3637 first_tabpage = alloc_tabpage(); 3638 if (first_tabpage == NULL) 3639 return FAIL; 3640 first_tabpage->tp_topframe = topframe; 3641 curtab = first_tabpage; 3642 curtab->tp_firstwin = firstwin; 3643 curtab->tp_lastwin = lastwin; 3644 curtab->tp_curwin = curwin; 3645 3646 return OK; 3647 } 3648 3649 /* 3650 * Allocate and init a window that is not a regular window. 3651 * This can only be done after the first window is fully initialized, thus it 3652 * can't be in win_alloc_first(). 3653 */ 3654 win_T * 3655 win_alloc_popup_win(void) 3656 { 3657 win_T *wp; 3658 3659 wp = win_alloc(NULL, TRUE); 3660 if (wp != NULL) 3661 { 3662 // We need to initialize options with something, using the current 3663 // window makes most sense. 3664 win_init_some(wp, curwin); 3665 3666 RESET_BINDING(wp); 3667 new_frame(wp); 3668 } 3669 return wp; 3670 } 3671 3672 /* 3673 * Initialize window "wp" to display buffer "buf". 3674 */ 3675 void 3676 win_init_popup_win(win_T *wp, buf_T *buf) 3677 { 3678 wp->w_buffer = buf; 3679 ++buf->b_nwindows; 3680 win_init_empty(wp); // set cursor and topline to safe values 3681 3682 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in 3683 // win_enter_ext(). 3684 VIM_CLEAR(wp->w_localdir); 3685 } 3686 3687 /* 3688 * Allocate the first window or the first window in a new tab page. 3689 * When "oldwin" is NULL create an empty buffer for it. 3690 * When "oldwin" is not NULL copy info from it to the new window. 3691 * Return FAIL when something goes wrong (out of memory). 3692 */ 3693 static int 3694 win_alloc_firstwin(win_T *oldwin) 3695 { 3696 curwin = win_alloc(NULL, FALSE); 3697 if (oldwin == NULL) 3698 { 3699 // Very first window, need to create an empty buffer for it and 3700 // initialize from scratch. 3701 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED); 3702 if (curwin == NULL || curbuf == NULL) 3703 return FAIL; 3704 curwin->w_buffer = curbuf; 3705 #ifdef FEAT_SYN_HL 3706 curwin->w_s = &(curbuf->b_s); 3707 #endif 3708 curbuf->b_nwindows = 1; // there is one window 3709 curwin->w_alist = &global_alist; 3710 curwin_init(); // init current window 3711 } 3712 else 3713 { 3714 // First window in new tab page, initialize it from "oldwin". 3715 win_init(curwin, oldwin, 0); 3716 3717 // We don't want cursor- and scroll-binding in the first window. 3718 RESET_BINDING(curwin); 3719 } 3720 3721 new_frame(curwin); 3722 if (curwin->w_frame == NULL) 3723 return FAIL; 3724 topframe = curwin->w_frame; 3725 topframe->fr_width = Columns; 3726 topframe->fr_height = Rows - p_ch; 3727 3728 return OK; 3729 } 3730 3731 /* 3732 * Create a frame for window "wp". 3733 */ 3734 static void 3735 new_frame(win_T *wp) 3736 { 3737 frame_T *frp = ALLOC_CLEAR_ONE(frame_T); 3738 3739 wp->w_frame = frp; 3740 if (frp != NULL) 3741 { 3742 frp->fr_layout = FR_LEAF; 3743 frp->fr_win = wp; 3744 } 3745 } 3746 3747 /* 3748 * Initialize the window and frame size to the maximum. 3749 */ 3750 void 3751 win_init_size(void) 3752 { 3753 firstwin->w_height = ROWS_AVAIL; 3754 topframe->fr_height = ROWS_AVAIL; 3755 firstwin->w_width = Columns; 3756 topframe->fr_width = Columns; 3757 } 3758 3759 /* 3760 * Allocate a new tabpage_T and init the values. 3761 * Returns NULL when out of memory. 3762 */ 3763 static tabpage_T * 3764 alloc_tabpage(void) 3765 { 3766 tabpage_T *tp; 3767 # ifdef FEAT_GUI 3768 int i; 3769 # endif 3770 3771 3772 tp = ALLOC_CLEAR_ONE(tabpage_T); 3773 if (tp == NULL) 3774 return NULL; 3775 3776 # ifdef FEAT_EVAL 3777 // init t: variables 3778 tp->tp_vars = dict_alloc(); 3779 if (tp->tp_vars == NULL) 3780 { 3781 vim_free(tp); 3782 return NULL; 3783 } 3784 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE); 3785 # endif 3786 3787 # ifdef FEAT_GUI 3788 for (i = 0; i < 3; i++) 3789 tp->tp_prev_which_scrollbars[i] = -1; 3790 # endif 3791 # ifdef FEAT_DIFF 3792 tp->tp_diff_invalid = TRUE; 3793 # endif 3794 tp->tp_ch_used = p_ch; 3795 3796 return tp; 3797 } 3798 3799 void 3800 free_tabpage(tabpage_T *tp) 3801 { 3802 int idx; 3803 3804 # ifdef FEAT_DIFF 3805 diff_clear(tp); 3806 # endif 3807 # ifdef FEAT_PROP_POPUP 3808 while (tp->tp_first_popupwin != NULL) 3809 popup_close_tabpage(tp, tp->tp_first_popupwin->w_id, TRUE); 3810 #endif 3811 for (idx = 0; idx < SNAP_COUNT; ++idx) 3812 clear_snapshot(tp, idx); 3813 #ifdef FEAT_EVAL 3814 vars_clear(&tp->tp_vars->dv_hashtab); // free all t: variables 3815 hash_init(&tp->tp_vars->dv_hashtab); 3816 unref_var_dict(tp->tp_vars); 3817 #endif 3818 3819 if (tp == lastused_tabpage) 3820 lastused_tabpage = NULL; 3821 3822 vim_free(tp->tp_localdir); 3823 vim_free(tp->tp_prevdir); 3824 3825 #ifdef FEAT_PYTHON 3826 python_tabpage_free(tp); 3827 #endif 3828 3829 #ifdef FEAT_PYTHON3 3830 python3_tabpage_free(tp); 3831 #endif 3832 3833 vim_free(tp); 3834 } 3835 3836 /* 3837 * Create a new Tab page with one window. 3838 * It will edit the current buffer, like after ":split". 3839 * When "after" is 0 put it just after the current Tab page. 3840 * Otherwise put it just before tab page "after". 3841 * Return FAIL or OK. 3842 */ 3843 int 3844 win_new_tabpage(int after) 3845 { 3846 tabpage_T *tp = curtab; 3847 tabpage_T *prev_tp = curtab; 3848 tabpage_T *newtp; 3849 int n; 3850 3851 newtp = alloc_tabpage(); 3852 if (newtp == NULL) 3853 return FAIL; 3854 3855 // Remember the current windows in this Tab page. 3856 if (leave_tabpage(curbuf, TRUE) == FAIL) 3857 { 3858 vim_free(newtp); 3859 return FAIL; 3860 } 3861 curtab = newtp; 3862 3863 newtp->tp_localdir = (tp->tp_localdir == NULL) 3864 ? NULL : vim_strsave(tp->tp_localdir); 3865 // Create a new empty window. 3866 if (win_alloc_firstwin(tp->tp_curwin) == OK) 3867 { 3868 // Make the new Tab page the new topframe. 3869 if (after == 1) 3870 { 3871 // New tab page becomes the first one. 3872 newtp->tp_next = first_tabpage; 3873 first_tabpage = newtp; 3874 } 3875 else 3876 { 3877 if (after > 0) 3878 { 3879 // Put new tab page before tab page "after". 3880 n = 2; 3881 for (tp = first_tabpage; tp->tp_next != NULL 3882 && n < after; tp = tp->tp_next) 3883 ++n; 3884 } 3885 newtp->tp_next = tp->tp_next; 3886 tp->tp_next = newtp; 3887 } 3888 newtp->tp_firstwin = newtp->tp_lastwin = newtp->tp_curwin = curwin; 3889 3890 win_init_size(); 3891 firstwin->w_winrow = tabline_height(); 3892 win_comp_scroll(curwin); 3893 3894 newtp->tp_topframe = topframe; 3895 last_status(FALSE); 3896 3897 lastused_tabpage = prev_tp; 3898 3899 #if defined(FEAT_GUI) 3900 // When 'guioptions' includes 'L' or 'R' may have to remove or add 3901 // scrollbars. Have to update them anyway. 3902 gui_may_update_scrollbars(); 3903 #endif 3904 #ifdef FEAT_JOB_CHANNEL 3905 entering_window(curwin); 3906 #endif 3907 3908 redraw_all_later(NOT_VALID); 3909 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf); 3910 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf); 3911 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf); 3912 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf); 3913 return OK; 3914 } 3915 3916 // Failed, get back the previous Tab page 3917 enter_tabpage(curtab, curbuf, TRUE, TRUE); 3918 return FAIL; 3919 } 3920 3921 /* 3922 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer, 3923 * like with ":split". 3924 * Returns OK if a new tab page was created, FAIL otherwise. 3925 */ 3926 static int 3927 may_open_tabpage(void) 3928 { 3929 int n = (cmdmod.cmod_tab == 0) 3930 ? postponed_split_tab : cmdmod.cmod_tab; 3931 3932 if (n != 0) 3933 { 3934 cmdmod.cmod_tab = 0; // reset it to avoid doing it twice 3935 postponed_split_tab = 0; 3936 return win_new_tabpage(n); 3937 } 3938 return FAIL; 3939 } 3940 3941 /* 3942 * Create up to "maxcount" tabpages with empty windows. 3943 * Returns the number of resulting tab pages. 3944 */ 3945 int 3946 make_tabpages(int maxcount) 3947 { 3948 int count = maxcount; 3949 int todo; 3950 3951 // Limit to 'tabpagemax' tabs. 3952 if (count > p_tpm) 3953 count = p_tpm; 3954 3955 /* 3956 * Don't execute autocommands while creating the tab pages. Must do that 3957 * when putting the buffers in the windows. 3958 */ 3959 block_autocmds(); 3960 3961 for (todo = count - 1; todo > 0; --todo) 3962 if (win_new_tabpage(0) == FAIL) 3963 break; 3964 3965 unblock_autocmds(); 3966 3967 // return actual number of tab pages 3968 return (count - todo); 3969 } 3970 3971 /* 3972 * Return TRUE when "tpc" points to a valid tab page. 3973 */ 3974 int 3975 valid_tabpage(tabpage_T *tpc) 3976 { 3977 tabpage_T *tp; 3978 3979 FOR_ALL_TABPAGES(tp) 3980 if (tp == tpc) 3981 return TRUE; 3982 return FALSE; 3983 } 3984 3985 /* 3986 * Return TRUE when "tpc" points to a valid tab page and at least one window is 3987 * valid. 3988 */ 3989 int 3990 valid_tabpage_win(tabpage_T *tpc) 3991 { 3992 tabpage_T *tp; 3993 win_T *wp; 3994 3995 FOR_ALL_TABPAGES(tp) 3996 { 3997 if (tp == tpc) 3998 { 3999 FOR_ALL_WINDOWS_IN_TAB(tp, wp) 4000 { 4001 if (win_valid_any_tab(wp)) 4002 return TRUE; 4003 } 4004 return FALSE; 4005 } 4006 } 4007 // shouldn't happen 4008 return FALSE; 4009 } 4010 4011 /* 4012 * Close tabpage "tab", assuming it has no windows in it. 4013 * There must be another tabpage or this will crash. 4014 */ 4015 void 4016 close_tabpage(tabpage_T *tab) 4017 { 4018 tabpage_T *ptp; 4019 4020 if (tab == first_tabpage) 4021 { 4022 first_tabpage = tab->tp_next; 4023 ptp = first_tabpage; 4024 } 4025 else 4026 { 4027 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab; 4028 ptp = ptp->tp_next) 4029 ; 4030 assert(ptp != NULL); 4031 ptp->tp_next = tab->tp_next; 4032 } 4033 4034 goto_tabpage_tp(ptp, FALSE, FALSE); 4035 free_tabpage(tab); 4036 } 4037 4038 /* 4039 * Find tab page "n" (first one is 1). Returns NULL when not found. 4040 */ 4041 tabpage_T * 4042 find_tabpage(int n) 4043 { 4044 tabpage_T *tp; 4045 int i = 1; 4046 4047 if (n == 0) 4048 return curtab; 4049 4050 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next) 4051 ++i; 4052 return tp; 4053 } 4054 4055 /* 4056 * Get index of tab page "tp". First one has index 1. 4057 * When not found returns number of tab pages plus one. 4058 */ 4059 int 4060 tabpage_index(tabpage_T *ftp) 4061 { 4062 int i = 1; 4063 tabpage_T *tp; 4064 4065 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next) 4066 ++i; 4067 return i; 4068 } 4069 4070 /* 4071 * Prepare for leaving the current tab page. 4072 * When autocommands change "curtab" we don't leave the tab page and return 4073 * FAIL. 4074 * Careful: When OK is returned need to get a new tab page very very soon! 4075 */ 4076 static int 4077 leave_tabpage( 4078 buf_T *new_curbuf UNUSED, // what is going to be the new curbuf, 4079 // NULL if unknown 4080 int trigger_leave_autocmds UNUSED) 4081 { 4082 tabpage_T *tp = curtab; 4083 4084 #ifdef FEAT_JOB_CHANNEL 4085 leaving_window(curwin); 4086 #endif 4087 reset_VIsual_and_resel(); // stop Visual mode 4088 if (trigger_leave_autocmds) 4089 { 4090 if (new_curbuf != curbuf) 4091 { 4092 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); 4093 if (curtab != tp) 4094 return FAIL; 4095 } 4096 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf); 4097 if (curtab != tp) 4098 return FAIL; 4099 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf); 4100 if (curtab != tp) 4101 return FAIL; 4102 } 4103 #if defined(FEAT_GUI) 4104 // Remove the scrollbars. They may be added back later. 4105 if (gui.in_use) 4106 gui_remove_scrollbars(); 4107 #endif 4108 tp->tp_curwin = curwin; 4109 tp->tp_prevwin = prevwin; 4110 tp->tp_firstwin = firstwin; 4111 tp->tp_lastwin = lastwin; 4112 tp->tp_old_Rows = Rows; 4113 tp->tp_old_Columns = Columns; 4114 firstwin = NULL; 4115 lastwin = NULL; 4116 return OK; 4117 } 4118 4119 /* 4120 * Start using tab page "tp". 4121 * Only to be used after leave_tabpage() or freeing the current tab page. 4122 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE. 4123 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE. 4124 */ 4125 static void 4126 enter_tabpage( 4127 tabpage_T *tp, 4128 buf_T *old_curbuf UNUSED, 4129 int trigger_enter_autocmds, 4130 int trigger_leave_autocmds) 4131 { 4132 int row; 4133 int old_off = tp->tp_firstwin->w_winrow; 4134 win_T *next_prevwin = tp->tp_prevwin; 4135 tabpage_T *last_tab = curtab; 4136 4137 curtab = tp; 4138 firstwin = tp->tp_firstwin; 4139 lastwin = tp->tp_lastwin; 4140 topframe = tp->tp_topframe; 4141 4142 // We would like doing the TabEnter event first, but we don't have a 4143 // valid current window yet, which may break some commands. 4144 // This triggers autocommands, thus may make "tp" invalid. 4145 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE, 4146 trigger_enter_autocmds, trigger_leave_autocmds); 4147 prevwin = next_prevwin; 4148 4149 last_status(FALSE); // status line may appear or disappear 4150 row = win_comp_pos(); // recompute w_winrow for all windows 4151 #ifdef FEAT_DIFF 4152 diff_need_scrollbind = TRUE; 4153 #endif 4154 4155 // The tabpage line may have appeared or disappeared, may need to resize 4156 // the frames for that. When the Vim window was resized need to update 4157 // frame sizes too. Use the stored value of p_ch, so that it can be 4158 // different for each tab page. 4159 if (p_ch != curtab->tp_ch_used) 4160 clear_cmdline = TRUE; 4161 p_ch = curtab->tp_ch_used; 4162 4163 // When cmdheight is changed in a tab page with '<C-w>-', cmdline_row is 4164 // changed but p_ch and tp_ch_used are not changed. Thus we also need to 4165 // check cmdline_row. 4166 if ((row < cmdline_row) && (cmdline_row <= Rows - p_ch)) 4167 clear_cmdline = TRUE; 4168 4169 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow 4170 #ifdef FEAT_GUI_TABLINE 4171 && !gui_use_tabline() 4172 #endif 4173 )) 4174 shell_new_rows(); 4175 if (curtab->tp_old_Columns != Columns && starting == 0) 4176 shell_new_columns(); // update window widths 4177 4178 lastused_tabpage = last_tab; 4179 4180 #if defined(FEAT_GUI) 4181 // When 'guioptions' includes 'L' or 'R' may have to remove or add 4182 // scrollbars. Have to update them anyway. 4183 gui_may_update_scrollbars(); 4184 #endif 4185 4186 // Apply autocommands after updating the display, when 'rows' and 4187 // 'columns' have been set correctly. 4188 if (trigger_enter_autocmds) 4189 { 4190 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf); 4191 if (old_curbuf != curbuf) 4192 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); 4193 } 4194 4195 redraw_all_later(NOT_VALID); 4196 } 4197 4198 /* 4199 * Go to tab page "n". For ":tab N" and "Ngt". 4200 * When "n" is 9999 go to the last tab page. 4201 */ 4202 void 4203 goto_tabpage(int n) 4204 { 4205 tabpage_T *tp = NULL; // shut up compiler 4206 tabpage_T *ttp; 4207 int i; 4208 4209 if (text_locked()) 4210 { 4211 // Not allowed when editing the command line. 4212 text_locked_msg(); 4213 return; 4214 } 4215 4216 // If there is only one it can't work. 4217 if (first_tabpage->tp_next == NULL) 4218 { 4219 if (n > 1) 4220 beep_flush(); 4221 return; 4222 } 4223 4224 if (n == 0) 4225 { 4226 // No count, go to next tab page, wrap around end. 4227 if (curtab->tp_next == NULL) 4228 tp = first_tabpage; 4229 else 4230 tp = curtab->tp_next; 4231 } 4232 else if (n < 0) 4233 { 4234 // "gT": go to previous tab page, wrap around end. "N gT" repeats 4235 // this N times. 4236 ttp = curtab; 4237 for (i = n; i < 0; ++i) 4238 { 4239 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL; 4240 tp = tp->tp_next) 4241 ; 4242 ttp = tp; 4243 } 4244 } 4245 else if (n == 9999) 4246 { 4247 // Go to last tab page. 4248 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next) 4249 ; 4250 } 4251 else 4252 { 4253 // Go to tab page "n". 4254 tp = find_tabpage(n); 4255 if (tp == NULL) 4256 { 4257 beep_flush(); 4258 return; 4259 } 4260 } 4261 4262 goto_tabpage_tp(tp, TRUE, TRUE); 4263 4264 #ifdef FEAT_GUI_TABLINE 4265 if (gui_use_tabline()) 4266 gui_mch_set_curtab(tabpage_index(curtab)); 4267 #endif 4268 } 4269 4270 /* 4271 * Go to tabpage "tp". 4272 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE. 4273 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE. 4274 * Note: doesn't update the GUI tab. 4275 */ 4276 void 4277 goto_tabpage_tp( 4278 tabpage_T *tp, 4279 int trigger_enter_autocmds, 4280 int trigger_leave_autocmds) 4281 { 4282 // Don't repeat a message in another tab page. 4283 set_keep_msg(NULL, 0); 4284 4285 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer, 4286 trigger_leave_autocmds) == OK) 4287 { 4288 if (valid_tabpage(tp)) 4289 enter_tabpage(tp, curbuf, trigger_enter_autocmds, 4290 trigger_leave_autocmds); 4291 else 4292 enter_tabpage(curtab, curbuf, trigger_enter_autocmds, 4293 trigger_leave_autocmds); 4294 } 4295 } 4296 4297 /* 4298 * Go to the last accessed tab page, if there is one. 4299 * Return OK or FAIL 4300 */ 4301 int 4302 goto_tabpage_lastused(void) 4303 { 4304 if (valid_tabpage(lastused_tabpage)) 4305 { 4306 goto_tabpage_tp(lastused_tabpage, TRUE, TRUE); 4307 return OK; 4308 } 4309 return FAIL; 4310 } 4311 4312 /* 4313 * Enter window "wp" in tab page "tp". 4314 * Also updates the GUI tab. 4315 */ 4316 void 4317 goto_tabpage_win(tabpage_T *tp, win_T *wp) 4318 { 4319 goto_tabpage_tp(tp, TRUE, TRUE); 4320 if (curtab == tp && win_valid(wp)) 4321 { 4322 win_enter(wp, TRUE); 4323 # ifdef FEAT_GUI_TABLINE 4324 if (gui_use_tabline()) 4325 gui_mch_set_curtab(tabpage_index(curtab)); 4326 # endif 4327 } 4328 } 4329 4330 /* 4331 * Move the current tab page to after tab page "nr". 4332 */ 4333 void 4334 tabpage_move(int nr) 4335 { 4336 int n = 1; 4337 tabpage_T *tp, *tp_dst; 4338 4339 if (first_tabpage->tp_next == NULL) 4340 return; 4341 4342 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next) 4343 ++n; 4344 4345 if (tp == curtab || (nr > 0 && tp->tp_next != NULL 4346 && tp->tp_next == curtab)) 4347 return; 4348 4349 tp_dst = tp; 4350 4351 // Remove the current tab page from the list of tab pages. 4352 if (curtab == first_tabpage) 4353 first_tabpage = curtab->tp_next; 4354 else 4355 { 4356 FOR_ALL_TABPAGES(tp) 4357 if (tp->tp_next == curtab) 4358 break; 4359 if (tp == NULL) // "cannot happen" 4360 return; 4361 tp->tp_next = curtab->tp_next; 4362 } 4363 4364 // Re-insert it at the specified position. 4365 if (nr <= 0) 4366 { 4367 curtab->tp_next = first_tabpage; 4368 first_tabpage = curtab; 4369 } 4370 else 4371 { 4372 curtab->tp_next = tp_dst->tp_next; 4373 tp_dst->tp_next = curtab; 4374 } 4375 4376 // Need to redraw the tabline. Tab page contents doesn't change. 4377 redraw_tabline = TRUE; 4378 } 4379 4380 4381 /* 4382 * Go to another window. 4383 * When jumping to another buffer, stop Visual mode. Do this before 4384 * changing windows so we can yank the selection into the '*' register. 4385 * When jumping to another window on the same buffer, adjust its cursor 4386 * position to keep the same Visual area. 4387 */ 4388 void 4389 win_goto(win_T *wp) 4390 { 4391 #ifdef FEAT_CONCEAL 4392 win_T *owp = curwin; 4393 #endif 4394 4395 #ifdef FEAT_PROP_POPUP 4396 if (ERROR_IF_ANY_POPUP_WINDOW) 4397 return; 4398 if (popup_is_popup(wp)) 4399 { 4400 emsg(_("E366: Not allowed to enter a popup window")); 4401 return; 4402 } 4403 #endif 4404 if (text_and_win_locked()) 4405 { 4406 beep_flush(); 4407 text_locked_msg(); 4408 return; 4409 } 4410 if (curbuf_locked()) 4411 return; 4412 4413 if (wp->w_buffer != curbuf) 4414 reset_VIsual_and_resel(); 4415 else if (VIsual_active) 4416 wp->w_cursor = curwin->w_cursor; 4417 4418 #ifdef FEAT_GUI 4419 need_mouse_correct = TRUE; 4420 #endif 4421 win_enter(wp, TRUE); 4422 4423 #ifdef FEAT_CONCEAL 4424 // Conceal cursor line in previous window, unconceal in current window. 4425 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled) 4426 redrawWinline(owp, owp->w_cursor.lnum); 4427 if (curwin->w_p_cole > 0 && !msg_scrolled) 4428 need_cursor_line_redraw = TRUE; 4429 #endif 4430 } 4431 4432 #if defined(FEAT_PERL) || defined(PROTO) 4433 /* 4434 * Find window number "winnr" (counting top to bottom). 4435 */ 4436 win_T * 4437 win_find_nr(int winnr) 4438 { 4439 win_T *wp; 4440 4441 FOR_ALL_WINDOWS(wp) 4442 if (--winnr == 0) 4443 break; 4444 return wp; 4445 } 4446 #endif 4447 4448 #if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO) 4449 /* 4450 * Find the tabpage for window "win". 4451 */ 4452 tabpage_T * 4453 win_find_tabpage(win_T *win) 4454 { 4455 win_T *wp; 4456 tabpage_T *tp; 4457 4458 FOR_ALL_TAB_WINDOWS(tp, wp) 4459 if (wp == win) 4460 return tp; 4461 return NULL; 4462 } 4463 #endif 4464 4465 /* 4466 * Get the above or below neighbor window of the specified window. 4467 * up - TRUE for the above neighbor 4468 * count - nth neighbor window 4469 * Returns the specified window if the neighbor is not found. 4470 */ 4471 win_T * 4472 win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count) 4473 { 4474 frame_T *fr; 4475 frame_T *nfr; 4476 frame_T *foundfr; 4477 4478 #ifdef FEAT_PROP_POPUP 4479 if (popup_is_popup(wp)) 4480 // popups don't have neighbors. 4481 return NULL; 4482 #endif 4483 foundfr = wp->w_frame; 4484 while (count--) 4485 { 4486 /* 4487 * First go upwards in the tree of frames until we find a upwards or 4488 * downwards neighbor. 4489 */ 4490 fr = foundfr; 4491 for (;;) 4492 { 4493 if (fr == tp->tp_topframe) 4494 goto end; 4495 if (up) 4496 nfr = fr->fr_prev; 4497 else 4498 nfr = fr->fr_next; 4499 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL) 4500 break; 4501 fr = fr->fr_parent; 4502 } 4503 4504 /* 4505 * Now go downwards to find the bottom or top frame in it. 4506 */ 4507 for (;;) 4508 { 4509 if (nfr->fr_layout == FR_LEAF) 4510 { 4511 foundfr = nfr; 4512 break; 4513 } 4514 fr = nfr->fr_child; 4515 if (nfr->fr_layout == FR_ROW) 4516 { 4517 // Find the frame at the cursor row. 4518 while (fr->fr_next != NULL 4519 && frame2win(fr)->w_wincol + fr->fr_width 4520 <= wp->w_wincol + wp->w_wcol) 4521 fr = fr->fr_next; 4522 } 4523 if (nfr->fr_layout == FR_COL && up) 4524 while (fr->fr_next != NULL) 4525 fr = fr->fr_next; 4526 nfr = fr; 4527 } 4528 } 4529 end: 4530 return foundfr != NULL ? foundfr->fr_win : NULL; 4531 } 4532 4533 /* 4534 * Move to window above or below "count" times. 4535 */ 4536 static void 4537 win_goto_ver( 4538 int up, // TRUE to go to win above 4539 long count) 4540 { 4541 win_T *win; 4542 4543 #ifdef FEAT_PROP_POPUP 4544 if (ERROR_IF_TERM_POPUP_WINDOW) 4545 return; 4546 #endif 4547 win = win_vert_neighbor(curtab, curwin, up, count); 4548 if (win != NULL) 4549 win_goto(win); 4550 } 4551 4552 /* 4553 * Get the left or right neighbor window of the specified window. 4554 * left - TRUE for the left neighbor 4555 * count - nth neighbor window 4556 * Returns the specified window if the neighbor is not found. 4557 */ 4558 win_T * 4559 win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count) 4560 { 4561 frame_T *fr; 4562 frame_T *nfr; 4563 frame_T *foundfr; 4564 4565 #ifdef FEAT_PROP_POPUP 4566 if (popup_is_popup(wp)) 4567 // popups don't have neighbors. 4568 return NULL; 4569 #endif 4570 foundfr = wp->w_frame; 4571 while (count--) 4572 { 4573 /* 4574 * First go upwards in the tree of frames until we find a left or 4575 * right neighbor. 4576 */ 4577 fr = foundfr; 4578 for (;;) 4579 { 4580 if (fr == tp->tp_topframe) 4581 goto end; 4582 if (left) 4583 nfr = fr->fr_prev; 4584 else 4585 nfr = fr->fr_next; 4586 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL) 4587 break; 4588 fr = fr->fr_parent; 4589 } 4590 4591 /* 4592 * Now go downwards to find the leftmost or rightmost frame in it. 4593 */ 4594 for (;;) 4595 { 4596 if (nfr->fr_layout == FR_LEAF) 4597 { 4598 foundfr = nfr; 4599 break; 4600 } 4601 fr = nfr->fr_child; 4602 if (nfr->fr_layout == FR_COL) 4603 { 4604 // Find the frame at the cursor row. 4605 while (fr->fr_next != NULL 4606 && frame2win(fr)->w_winrow + fr->fr_height 4607 <= wp->w_winrow + wp->w_wrow) 4608 fr = fr->fr_next; 4609 } 4610 if (nfr->fr_layout == FR_ROW && left) 4611 while (fr->fr_next != NULL) 4612 fr = fr->fr_next; 4613 nfr = fr; 4614 } 4615 } 4616 end: 4617 return foundfr != NULL ? foundfr->fr_win : NULL; 4618 } 4619 4620 /* 4621 * Move to left or right window. 4622 */ 4623 static void 4624 win_goto_hor( 4625 int left, // TRUE to go to left win 4626 long count) 4627 { 4628 win_T *win; 4629 4630 #ifdef FEAT_PROP_POPUP 4631 if (ERROR_IF_TERM_POPUP_WINDOW) 4632 return; 4633 #endif 4634 win = win_horz_neighbor(curtab, curwin, left, count); 4635 if (win != NULL) 4636 win_goto(win); 4637 } 4638 4639 /* 4640 * Make window "wp" the current window. 4641 */ 4642 void 4643 win_enter(win_T *wp, int undo_sync) 4644 { 4645 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE); 4646 } 4647 4648 /* 4649 * Make window wp the current window. 4650 * Can be called with "curwin_invalid" TRUE, which means that curwin has just 4651 * been closed and isn't valid. 4652 */ 4653 static void 4654 win_enter_ext( 4655 win_T *wp, 4656 int undo_sync, 4657 int curwin_invalid, 4658 int trigger_new_autocmds, 4659 int trigger_enter_autocmds, 4660 int trigger_leave_autocmds) 4661 { 4662 int other_buffer = FALSE; 4663 4664 if (wp == curwin && !curwin_invalid) // nothing to do 4665 return; 4666 4667 #ifdef FEAT_JOB_CHANNEL 4668 if (!curwin_invalid) 4669 leaving_window(curwin); 4670 #endif 4671 4672 if (!curwin_invalid && trigger_leave_autocmds) 4673 { 4674 /* 4675 * Be careful: If autocommands delete the window, return now. 4676 */ 4677 if (wp->w_buffer != curbuf) 4678 { 4679 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); 4680 other_buffer = TRUE; 4681 if (!win_valid(wp)) 4682 return; 4683 } 4684 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf); 4685 if (!win_valid(wp)) 4686 return; 4687 #ifdef FEAT_EVAL 4688 // autocmds may abort script processing 4689 if (aborting()) 4690 return; 4691 #endif 4692 } 4693 4694 // sync undo before leaving the current buffer 4695 if (undo_sync && curbuf != wp->w_buffer) 4696 u_sync(FALSE); 4697 4698 // Might need to scroll the old window before switching, e.g., when the 4699 // cursor was moved. 4700 update_topline(); 4701 4702 // may have to copy the buffer options when 'cpo' contains 'S' 4703 if (wp->w_buffer != curbuf) 4704 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP); 4705 if (!curwin_invalid) 4706 { 4707 prevwin = curwin; // remember for CTRL-W p 4708 curwin->w_redr_status = TRUE; 4709 } 4710 curwin = wp; 4711 curbuf = wp->w_buffer; 4712 check_cursor(); 4713 if (!virtual_active()) 4714 curwin->w_cursor.coladd = 0; 4715 changed_line_abv_curs(); // assume cursor position needs updating 4716 4717 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL) 4718 { 4719 char_u *dirname; 4720 4721 // Window or tab has a local directory: Save current directory as 4722 // global directory (unless that was done already) and change to the 4723 // local directory. 4724 if (globaldir == NULL) 4725 { 4726 char_u cwd[MAXPATHL]; 4727 4728 if (mch_dirname(cwd, MAXPATHL) == OK) 4729 globaldir = vim_strsave(cwd); 4730 } 4731 if (curwin->w_localdir != NULL) 4732 dirname = curwin->w_localdir; 4733 else 4734 dirname = curtab->tp_localdir; 4735 4736 if (mch_chdir((char *)dirname) == 0) 4737 shorten_fnames(TRUE); 4738 } 4739 else if (globaldir != NULL) 4740 { 4741 // Window doesn't have a local directory and we are not in the global 4742 // directory: Change to the global directory. 4743 vim_ignored = mch_chdir((char *)globaldir); 4744 VIM_CLEAR(globaldir); 4745 shorten_fnames(TRUE); 4746 } 4747 4748 #ifdef FEAT_JOB_CHANNEL 4749 entering_window(curwin); 4750 #endif 4751 // Careful: autocommands may close the window and make "wp" invalid 4752 if (trigger_new_autocmds) 4753 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf); 4754 if (trigger_enter_autocmds) 4755 { 4756 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf); 4757 if (other_buffer) 4758 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); 4759 } 4760 4761 #ifdef FEAT_TITLE 4762 maketitle(); 4763 #endif 4764 curwin->w_redr_status = TRUE; 4765 #ifdef FEAT_TERMINAL 4766 if (bt_terminal(curwin->w_buffer)) 4767 // terminal is likely in another mode 4768 redraw_mode = TRUE; 4769 #endif 4770 redraw_tabline = TRUE; 4771 if (restart_edit) 4772 redraw_later(VALID); // causes status line redraw 4773 4774 // set window height to desired minimal value 4775 if (curwin->w_height < p_wh && !curwin->w_p_wfh 4776 #ifdef FEAT_PROP_POPUP 4777 && !popup_is_popup(curwin) 4778 #endif 4779 ) 4780 win_setheight((int)p_wh); 4781 else if (curwin->w_height == 0) 4782 win_setheight(1); 4783 4784 // set window width to desired minimal value 4785 if (curwin->w_width < p_wiw && !curwin->w_p_wfw) 4786 win_setwidth((int)p_wiw); 4787 4788 setmouse(); // in case jumped to/from help buffer 4789 4790 // Change directories when the 'acd' option is set. 4791 DO_AUTOCHDIR; 4792 } 4793 4794 4795 /* 4796 * Jump to the first open window that contains buffer "buf", if one exists. 4797 * Returns a pointer to the window found, otherwise NULL. 4798 */ 4799 win_T * 4800 buf_jump_open_win(buf_T *buf) 4801 { 4802 win_T *wp = NULL; 4803 4804 if (curwin->w_buffer == buf) 4805 wp = curwin; 4806 else 4807 FOR_ALL_WINDOWS(wp) 4808 if (wp->w_buffer == buf) 4809 break; 4810 if (wp != NULL) 4811 win_enter(wp, FALSE); 4812 return wp; 4813 } 4814 4815 /* 4816 * Jump to the first open window in any tab page that contains buffer "buf", 4817 * if one exists. 4818 * Returns a pointer to the window found, otherwise NULL. 4819 */ 4820 win_T * 4821 buf_jump_open_tab(buf_T *buf) 4822 { 4823 win_T *wp = buf_jump_open_win(buf); 4824 tabpage_T *tp; 4825 4826 if (wp != NULL) 4827 return wp; 4828 4829 FOR_ALL_TABPAGES(tp) 4830 if (tp != curtab) 4831 { 4832 FOR_ALL_WINDOWS_IN_TAB(tp, wp) 4833 if (wp->w_buffer == buf) 4834 break; 4835 if (wp != NULL) 4836 { 4837 goto_tabpage_win(tp, wp); 4838 if (curwin != wp) 4839 wp = NULL; // something went wrong 4840 break; 4841 } 4842 } 4843 return wp; 4844 } 4845 4846 static int last_win_id = LOWEST_WIN_ID - 1; 4847 4848 /* 4849 * Allocate a window structure and link it in the window list when "hidden" is 4850 * FALSE. 4851 */ 4852 static win_T * 4853 win_alloc(win_T *after UNUSED, int hidden UNUSED) 4854 { 4855 win_T *new_wp; 4856 4857 /* 4858 * allocate window structure and linesizes arrays 4859 */ 4860 new_wp = ALLOC_CLEAR_ONE(win_T); 4861 if (new_wp == NULL) 4862 return NULL; 4863 4864 if (win_alloc_lines(new_wp) == FAIL) 4865 { 4866 vim_free(new_wp); 4867 return NULL; 4868 } 4869 4870 new_wp->w_id = ++last_win_id; 4871 4872 #ifdef FEAT_EVAL 4873 // init w: variables 4874 new_wp->w_vars = dict_alloc(); 4875 if (new_wp->w_vars == NULL) 4876 { 4877 win_free_lsize(new_wp); 4878 vim_free(new_wp); 4879 return NULL; 4880 } 4881 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE); 4882 #endif 4883 4884 // Don't execute autocommands while the window is not properly 4885 // initialized yet. gui_create_scrollbar() may trigger a FocusGained 4886 // event. 4887 block_autocmds(); 4888 4889 /* 4890 * link the window in the window list 4891 */ 4892 if (!hidden) 4893 win_append(after, new_wp); 4894 new_wp->w_wincol = 0; 4895 new_wp->w_width = Columns; 4896 4897 // position the display and the cursor at the top of the file. 4898 new_wp->w_topline = 1; 4899 #ifdef FEAT_DIFF 4900 new_wp->w_topfill = 0; 4901 #endif 4902 new_wp->w_botline = 2; 4903 new_wp->w_cursor.lnum = 1; 4904 new_wp->w_scbind_pos = 1; 4905 4906 // use global option value for global-local options 4907 new_wp->w_p_so = -1; 4908 new_wp->w_p_siso = -1; 4909 4910 // We won't calculate w_fraction until resizing the window 4911 new_wp->w_fraction = 0; 4912 new_wp->w_prev_fraction_row = -1; 4913 4914 #ifdef FEAT_GUI 4915 if (gui.in_use) 4916 { 4917 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT], 4918 SBAR_LEFT, new_wp); 4919 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT], 4920 SBAR_RIGHT, new_wp); 4921 } 4922 #endif 4923 #ifdef FEAT_FOLDING 4924 foldInitWin(new_wp); 4925 #endif 4926 unblock_autocmds(); 4927 #ifdef FEAT_SEARCH_EXTRA 4928 new_wp->w_match_head = NULL; 4929 new_wp->w_next_match_id = 4; 4930 #endif 4931 return new_wp; 4932 } 4933 4934 /* 4935 * Remove window 'wp' from the window list and free the structure. 4936 */ 4937 static void 4938 win_free( 4939 win_T *wp, 4940 tabpage_T *tp) // tab page "win" is in, NULL for current 4941 { 4942 int i; 4943 buf_T *buf; 4944 wininfo_T *wip; 4945 4946 #ifdef FEAT_FOLDING 4947 clearFolding(wp); 4948 #endif 4949 4950 // reduce the reference count to the argument list. 4951 alist_unlink(wp->w_alist); 4952 4953 // Don't execute autocommands while the window is halfway being deleted. 4954 // gui_mch_destroy_scrollbar() may trigger a FocusGained event. 4955 block_autocmds(); 4956 4957 #ifdef FEAT_LUA 4958 lua_window_free(wp); 4959 #endif 4960 4961 #ifdef FEAT_MZSCHEME 4962 mzscheme_window_free(wp); 4963 #endif 4964 4965 #ifdef FEAT_PERL 4966 perl_win_free(wp); 4967 #endif 4968 4969 #ifdef FEAT_PYTHON 4970 python_window_free(wp); 4971 #endif 4972 4973 #ifdef FEAT_PYTHON3 4974 python3_window_free(wp); 4975 #endif 4976 4977 #ifdef FEAT_TCL 4978 tcl_window_free(wp); 4979 #endif 4980 4981 #ifdef FEAT_RUBY 4982 ruby_window_free(wp); 4983 #endif 4984 4985 clear_winopt(&wp->w_onebuf_opt); 4986 clear_winopt(&wp->w_allbuf_opt); 4987 4988 #ifdef FEAT_EVAL 4989 vars_clear(&wp->w_vars->dv_hashtab); // free all w: variables 4990 hash_init(&wp->w_vars->dv_hashtab); 4991 unref_var_dict(wp->w_vars); 4992 #endif 4993 4994 { 4995 tabpage_T *ttp; 4996 4997 if (prevwin == wp) 4998 prevwin = NULL; 4999 FOR_ALL_TABPAGES(ttp) 5000 if (ttp->tp_prevwin == wp) 5001 ttp->tp_prevwin = NULL; 5002 } 5003 win_free_lsize(wp); 5004 5005 for (i = 0; i < wp->w_tagstacklen; ++i) 5006 { 5007 vim_free(wp->w_tagstack[i].tagname); 5008 vim_free(wp->w_tagstack[i].user_data); 5009 } 5010 vim_free(wp->w_localdir); 5011 vim_free(wp->w_prevdir); 5012 5013 // Remove the window from the b_wininfo lists, it may happen that the 5014 // freed memory is re-used for another window. 5015 FOR_ALL_BUFFERS(buf) 5016 FOR_ALL_BUF_WININFO(buf, wip) 5017 if (wip->wi_win == wp) 5018 { 5019 wininfo_T *wip2; 5020 5021 // If there already is an entry with "wi_win" set to NULL it 5022 // must be removed, it would never be used. 5023 for (wip2 = buf->b_wininfo; wip2 != NULL; wip2 = wip2->wi_next) 5024 if (wip2->wi_win == NULL) 5025 { 5026 if (wip2->wi_next != NULL) 5027 wip2->wi_next->wi_prev = wip2->wi_prev; 5028 if (wip2->wi_prev == NULL) 5029 buf->b_wininfo = wip2->wi_next; 5030 else 5031 wip2->wi_prev->wi_next = wip2->wi_next; 5032 free_wininfo(wip2); 5033 break; 5034 } 5035 5036 wip->wi_win = NULL; 5037 } 5038 5039 #ifdef FEAT_SEARCH_EXTRA 5040 clear_matches(wp); 5041 #endif 5042 5043 #ifdef FEAT_JUMPLIST 5044 free_jumplist(wp); 5045 #endif 5046 5047 #ifdef FEAT_QUICKFIX 5048 qf_free_all(wp); 5049 #endif 5050 5051 #ifdef FEAT_GUI 5052 if (gui.in_use) 5053 { 5054 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]); 5055 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]); 5056 } 5057 #endif // FEAT_GUI 5058 5059 #ifdef FEAT_MENU 5060 remove_winbar(wp); 5061 #endif 5062 #ifdef FEAT_PROP_POPUP 5063 free_callback(&wp->w_close_cb); 5064 free_callback(&wp->w_filter_cb); 5065 for (i = 0; i < 4; ++i) 5066 VIM_CLEAR(wp->w_border_highlight[i]); 5067 vim_free(wp->w_scrollbar_highlight); 5068 vim_free(wp->w_thumb_highlight); 5069 vim_free(wp->w_popup_title); 5070 list_unref(wp->w_popup_mask); 5071 vim_free(wp->w_popup_mask_cells); 5072 #endif 5073 5074 #ifdef FEAT_SYN_HL 5075 vim_free(wp->w_p_cc_cols); 5076 #endif 5077 5078 if (win_valid_any_tab(wp)) 5079 win_remove(wp, tp); 5080 if (autocmd_busy) 5081 { 5082 wp->w_next = au_pending_free_win; 5083 au_pending_free_win = wp; 5084 } 5085 else 5086 vim_free(wp); 5087 5088 unblock_autocmds(); 5089 } 5090 5091 /* 5092 * Return TRUE if "wp" is not in the list of windows: the autocmd window or a 5093 * popup window. 5094 */ 5095 static int 5096 win_unlisted(win_T *wp) 5097 { 5098 return wp == aucmd_win || WIN_IS_POPUP(wp); 5099 } 5100 5101 #if defined(FEAT_PROP_POPUP) || defined(PROTO) 5102 /* 5103 * Free a popup window. This does not take the window out of the window list 5104 * and assumes there is only one toplevel frame, no split. 5105 */ 5106 void 5107 win_free_popup(win_T *win) 5108 { 5109 if (bt_popup(win->w_buffer)) 5110 win_close_buffer(win, DOBUF_WIPE_REUSE, FALSE); 5111 else 5112 close_buffer(win, win->w_buffer, 0, FALSE, FALSE); 5113 # if defined(FEAT_TIMERS) 5114 if (win->w_popup_timer != NULL) 5115 stop_timer(win->w_popup_timer); 5116 # endif 5117 vim_free(win->w_frame); 5118 win_free(win, NULL); 5119 } 5120 #endif 5121 5122 /* 5123 * Append window "wp" in the window list after window "after". 5124 */ 5125 static void 5126 win_append(win_T *after, win_T *wp) 5127 { 5128 win_T *before; 5129 5130 if (after == NULL) // after NULL is in front of the first 5131 before = firstwin; 5132 else 5133 before = after->w_next; 5134 5135 wp->w_next = before; 5136 wp->w_prev = after; 5137 if (after == NULL) 5138 firstwin = wp; 5139 else 5140 after->w_next = wp; 5141 if (before == NULL) 5142 lastwin = wp; 5143 else 5144 before->w_prev = wp; 5145 } 5146 5147 /* 5148 * Remove a window from the window list. 5149 */ 5150 void 5151 win_remove( 5152 win_T *wp, 5153 tabpage_T *tp) // tab page "win" is in, NULL for current 5154 { 5155 if (wp->w_prev != NULL) 5156 wp->w_prev->w_next = wp->w_next; 5157 else if (tp == NULL) 5158 firstwin = curtab->tp_firstwin = wp->w_next; 5159 else 5160 tp->tp_firstwin = wp->w_next; 5161 5162 if (wp->w_next != NULL) 5163 wp->w_next->w_prev = wp->w_prev; 5164 else if (tp == NULL) 5165 lastwin = curtab->tp_lastwin = wp->w_prev; 5166 else 5167 tp->tp_lastwin = wp->w_prev; 5168 } 5169 5170 /* 5171 * Append frame "frp" in a frame list after frame "after". 5172 */ 5173 static void 5174 frame_append(frame_T *after, frame_T *frp) 5175 { 5176 frp->fr_next = after->fr_next; 5177 after->fr_next = frp; 5178 if (frp->fr_next != NULL) 5179 frp->fr_next->fr_prev = frp; 5180 frp->fr_prev = after; 5181 } 5182 5183 /* 5184 * Insert frame "frp" in a frame list before frame "before". 5185 */ 5186 static void 5187 frame_insert(frame_T *before, frame_T *frp) 5188 { 5189 frp->fr_next = before; 5190 frp->fr_prev = before->fr_prev; 5191 before->fr_prev = frp; 5192 if (frp->fr_prev != NULL) 5193 frp->fr_prev->fr_next = frp; 5194 else 5195 frp->fr_parent->fr_child = frp; 5196 } 5197 5198 /* 5199 * Remove a frame from a frame list. 5200 */ 5201 static void 5202 frame_remove(frame_T *frp) 5203 { 5204 if (frp->fr_prev != NULL) 5205 frp->fr_prev->fr_next = frp->fr_next; 5206 else 5207 { 5208 frp->fr_parent->fr_child = frp->fr_next; 5209 // special case: topframe->fr_child == frp 5210 if (topframe->fr_child == frp) 5211 topframe->fr_child = frp->fr_next; 5212 } 5213 if (frp->fr_next != NULL) 5214 frp->fr_next->fr_prev = frp->fr_prev; 5215 } 5216 5217 /* 5218 * Allocate w_lines[] for window "wp". 5219 * Return FAIL for failure, OK for success. 5220 */ 5221 int 5222 win_alloc_lines(win_T *wp) 5223 { 5224 wp->w_lines_valid = 0; 5225 wp->w_lines = ALLOC_CLEAR_MULT(wline_T, Rows ); 5226 if (wp->w_lines == NULL) 5227 return FAIL; 5228 return OK; 5229 } 5230 5231 /* 5232 * free lsize arrays for a window 5233 */ 5234 void 5235 win_free_lsize(win_T *wp) 5236 { 5237 // TODO: why would wp be NULL here? 5238 if (wp != NULL) 5239 VIM_CLEAR(wp->w_lines); 5240 } 5241 5242 /* 5243 * Called from win_new_shellsize() after Rows changed. 5244 * This only does the current tab page, others must be done when made active. 5245 */ 5246 void 5247 shell_new_rows(void) 5248 { 5249 int h = (int)ROWS_AVAIL; 5250 5251 if (firstwin == NULL) // not initialized yet 5252 return; 5253 if (h < frame_minheight(topframe, NULL)) 5254 h = frame_minheight(topframe, NULL); 5255 5256 // First try setting the heights of windows with 'winfixheight'. If 5257 // that doesn't result in the right height, forget about that option. 5258 frame_new_height(topframe, h, FALSE, TRUE); 5259 if (!frame_check_height(topframe, h)) 5260 frame_new_height(topframe, h, FALSE, FALSE); 5261 5262 (void)win_comp_pos(); // recompute w_winrow and w_wincol 5263 compute_cmdrow(); 5264 curtab->tp_ch_used = p_ch; 5265 5266 #if 0 5267 // Disabled: don't want making the screen smaller make a window larger. 5268 if (p_ea) 5269 win_equal(curwin, FALSE, 'v'); 5270 #endif 5271 } 5272 5273 /* 5274 * Called from win_new_shellsize() after Columns changed. 5275 */ 5276 void 5277 shell_new_columns(void) 5278 { 5279 if (firstwin == NULL) // not initialized yet 5280 return; 5281 5282 // First try setting the widths of windows with 'winfixwidth'. If that 5283 // doesn't result in the right width, forget about that option. 5284 frame_new_width(topframe, (int)Columns, FALSE, TRUE); 5285 if (!frame_check_width(topframe, Columns)) 5286 frame_new_width(topframe, (int)Columns, FALSE, FALSE); 5287 5288 (void)win_comp_pos(); // recompute w_winrow and w_wincol 5289 #if 0 5290 // Disabled: don't want making the screen smaller make a window larger. 5291 if (p_ea) 5292 win_equal(curwin, FALSE, 'h'); 5293 #endif 5294 } 5295 5296 #if defined(FEAT_CMDWIN) || defined(PROTO) 5297 /* 5298 * Save the size of all windows in "gap". 5299 */ 5300 void 5301 win_size_save(garray_T *gap) 5302 5303 { 5304 win_T *wp; 5305 5306 ga_init2(gap, (int)sizeof(int), 1); 5307 if (ga_grow(gap, win_count() * 2 + 1) == OK) 5308 { 5309 // first entry is value of 'lines' 5310 ((int *)gap->ga_data)[gap->ga_len++] = Rows; 5311 5312 FOR_ALL_WINDOWS(wp) 5313 { 5314 ((int *)gap->ga_data)[gap->ga_len++] = 5315 wp->w_width + wp->w_vsep_width; 5316 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height; 5317 } 5318 } 5319 } 5320 5321 /* 5322 * Restore window sizes, but only if the number of windows is still the same 5323 * and 'lines' didn't change. 5324 * Does not free the growarray. 5325 */ 5326 void 5327 win_size_restore(garray_T *gap) 5328 { 5329 win_T *wp; 5330 int i, j; 5331 5332 if (win_count() * 2 + 1 == gap->ga_len 5333 && ((int *)gap->ga_data)[0] == Rows) 5334 { 5335 // The order matters, because frames contain other frames, but it's 5336 // difficult to get right. The easy way out is to do it twice. 5337 for (j = 0; j < 2; ++j) 5338 { 5339 i = 1; 5340 FOR_ALL_WINDOWS(wp) 5341 { 5342 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]); 5343 win_setheight_win(((int *)gap->ga_data)[i++], wp); 5344 } 5345 } 5346 // recompute the window positions 5347 (void)win_comp_pos(); 5348 } 5349 } 5350 #endif // FEAT_CMDWIN 5351 5352 /* 5353 * Update the position for all windows, using the width and height of the 5354 * frames. 5355 * Returns the row just after the last window. 5356 */ 5357 int 5358 win_comp_pos(void) 5359 { 5360 int row = tabline_height(); 5361 int col = 0; 5362 5363 frame_comp_pos(topframe, &row, &col); 5364 return row; 5365 } 5366 5367 /* 5368 * Update the position of the windows in frame "topfrp", using the width and 5369 * height of the frames. 5370 * "*row" and "*col" are the top-left position of the frame. They are updated 5371 * to the bottom-right position plus one. 5372 */ 5373 static void 5374 frame_comp_pos(frame_T *topfrp, int *row, int *col) 5375 { 5376 win_T *wp; 5377 frame_T *frp; 5378 int startcol; 5379 int startrow; 5380 int h; 5381 5382 wp = topfrp->fr_win; 5383 if (wp != NULL) 5384 { 5385 if (wp->w_winrow != *row || wp->w_wincol != *col) 5386 { 5387 // position changed, redraw 5388 wp->w_winrow = *row; 5389 wp->w_wincol = *col; 5390 redraw_win_later(wp, NOT_VALID); 5391 wp->w_redr_status = TRUE; 5392 } 5393 // WinBar will not show if the window height is zero 5394 h = VISIBLE_HEIGHT(wp) + wp->w_status_height; 5395 *row += h > topfrp->fr_height ? topfrp->fr_height : h; 5396 *col += wp->w_width + wp->w_vsep_width; 5397 } 5398 else 5399 { 5400 startrow = *row; 5401 startcol = *col; 5402 FOR_ALL_FRAMES(frp, topfrp->fr_child) 5403 { 5404 if (topfrp->fr_layout == FR_ROW) 5405 *row = startrow; // all frames are at the same row 5406 else 5407 *col = startcol; // all frames are at the same col 5408 frame_comp_pos(frp, row, col); 5409 } 5410 } 5411 } 5412 5413 /* 5414 * Set current window height and take care of repositioning other windows to 5415 * fit around it. 5416 */ 5417 void 5418 win_setheight(int height) 5419 { 5420 win_setheight_win(height, curwin); 5421 } 5422 5423 /* 5424 * Set the window height of window "win" and take care of repositioning other 5425 * windows to fit around it. 5426 */ 5427 void 5428 win_setheight_win(int height, win_T *win) 5429 { 5430 int row; 5431 5432 if (win == curwin) 5433 { 5434 // Always keep current window at least one line high, even when 5435 // 'winminheight' is zero. 5436 if (height < p_wmh) 5437 height = p_wmh; 5438 if (height == 0) 5439 height = 1; 5440 height += WINBAR_HEIGHT(curwin); 5441 } 5442 5443 frame_setheight(win->w_frame, height + win->w_status_height); 5444 5445 // recompute the window positions 5446 row = win_comp_pos(); 5447 5448 /* 5449 * If there is extra space created between the last window and the command 5450 * line, clear it. 5451 */ 5452 if (full_screen && msg_scrolled == 0 && row < cmdline_row) 5453 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0); 5454 cmdline_row = row; 5455 msg_row = row; 5456 msg_col = 0; 5457 5458 redraw_all_later(NOT_VALID); 5459 } 5460 5461 /* 5462 * Set the height of a frame to "height" and take care that all frames and 5463 * windows inside it are resized. Also resize frames on the left and right if 5464 * the are in the same FR_ROW frame. 5465 * 5466 * Strategy: 5467 * If the frame is part of a FR_COL frame, try fitting the frame in that 5468 * frame. If that doesn't work (the FR_COL frame is too small), recursively 5469 * go to containing frames to resize them and make room. 5470 * If the frame is part of a FR_ROW frame, all frames must be resized as well. 5471 * Check for the minimal height of the FR_ROW frame. 5472 * At the top level we can also use change the command line height. 5473 */ 5474 static void 5475 frame_setheight(frame_T *curfrp, int height) 5476 { 5477 int room; // total number of lines available 5478 int take; // number of lines taken from other windows 5479 int room_cmdline; // lines available from cmdline 5480 int run; 5481 frame_T *frp; 5482 int h; 5483 int room_reserved; 5484 5485 // If the height already is the desired value, nothing to do. 5486 if (curfrp->fr_height == height) 5487 return; 5488 5489 if (curfrp->fr_parent == NULL) 5490 { 5491 // topframe: can only change the command line 5492 if (height > ROWS_AVAIL) 5493 height = ROWS_AVAIL; 5494 if (height > 0) 5495 frame_new_height(curfrp, height, FALSE, FALSE); 5496 } 5497 else if (curfrp->fr_parent->fr_layout == FR_ROW) 5498 { 5499 // Row of frames: Also need to resize frames left and right of this 5500 // one. First check for the minimal height of these. 5501 h = frame_minheight(curfrp->fr_parent, NULL); 5502 if (height < h) 5503 height = h; 5504 frame_setheight(curfrp->fr_parent, height); 5505 } 5506 else 5507 { 5508 /* 5509 * Column of frames: try to change only frames in this column. 5510 */ 5511 /* 5512 * Do this twice: 5513 * 1: compute room available, if it's not enough try resizing the 5514 * containing frame. 5515 * 2: compute the room available and adjust the height to it. 5516 * Try not to reduce the height of a window with 'winfixheight' set. 5517 */ 5518 for (run = 1; run <= 2; ++run) 5519 { 5520 room = 0; 5521 room_reserved = 0; 5522 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child) 5523 { 5524 if (frp != curfrp 5525 && frp->fr_win != NULL 5526 && frp->fr_win->w_p_wfh) 5527 room_reserved += frp->fr_height; 5528 room += frp->fr_height; 5529 if (frp != curfrp) 5530 room -= frame_minheight(frp, NULL); 5531 } 5532 if (curfrp->fr_width != Columns) 5533 room_cmdline = 0; 5534 else 5535 { 5536 room_cmdline = Rows - p_ch - (lastwin->w_winrow 5537 + VISIBLE_HEIGHT(lastwin) 5538 + lastwin->w_status_height); 5539 if (room_cmdline < 0) 5540 room_cmdline = 0; 5541 } 5542 5543 if (height <= room + room_cmdline) 5544 break; 5545 if (run == 2 || curfrp->fr_width == Columns) 5546 { 5547 if (height > room + room_cmdline) 5548 height = room + room_cmdline; 5549 break; 5550 } 5551 frame_setheight(curfrp->fr_parent, height 5552 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1); 5553 } 5554 5555 /* 5556 * Compute the number of lines we will take from others frames (can be 5557 * negative!). 5558 */ 5559 take = height - curfrp->fr_height; 5560 5561 // If there is not enough room, also reduce the height of a window 5562 // with 'winfixheight' set. 5563 if (height > room + room_cmdline - room_reserved) 5564 room_reserved = room + room_cmdline - height; 5565 // If there is only a 'winfixheight' window and making the 5566 // window smaller, need to make the other window taller. 5567 if (take < 0 && room - curfrp->fr_height < room_reserved) 5568 room_reserved = 0; 5569 5570 if (take > 0 && room_cmdline > 0) 5571 { 5572 // use lines from cmdline first 5573 if (take < room_cmdline) 5574 room_cmdline = take; 5575 take -= room_cmdline; 5576 topframe->fr_height += room_cmdline; 5577 } 5578 5579 /* 5580 * set the current frame to the new height 5581 */ 5582 frame_new_height(curfrp, height, FALSE, FALSE); 5583 5584 /* 5585 * First take lines from the frames after the current frame. If 5586 * that is not enough, takes lines from frames above the current 5587 * frame. 5588 */ 5589 for (run = 0; run < 2; ++run) 5590 { 5591 if (run == 0) 5592 frp = curfrp->fr_next; // 1st run: start with next window 5593 else 5594 frp = curfrp->fr_prev; // 2nd run: start with prev window 5595 while (frp != NULL && take != 0) 5596 { 5597 h = frame_minheight(frp, NULL); 5598 if (room_reserved > 0 5599 && frp->fr_win != NULL 5600 && frp->fr_win->w_p_wfh) 5601 { 5602 if (room_reserved >= frp->fr_height) 5603 room_reserved -= frp->fr_height; 5604 else 5605 { 5606 if (frp->fr_height - room_reserved > take) 5607 room_reserved = frp->fr_height - take; 5608 take -= frp->fr_height - room_reserved; 5609 frame_new_height(frp, room_reserved, FALSE, FALSE); 5610 room_reserved = 0; 5611 } 5612 } 5613 else 5614 { 5615 if (frp->fr_height - take < h) 5616 { 5617 take -= frp->fr_height - h; 5618 frame_new_height(frp, h, FALSE, FALSE); 5619 } 5620 else 5621 { 5622 frame_new_height(frp, frp->fr_height - take, 5623 FALSE, FALSE); 5624 take = 0; 5625 } 5626 } 5627 if (run == 0) 5628 frp = frp->fr_next; 5629 else 5630 frp = frp->fr_prev; 5631 } 5632 } 5633 } 5634 } 5635 5636 /* 5637 * Set current window width and take care of repositioning other windows to 5638 * fit around it. 5639 */ 5640 void 5641 win_setwidth(int width) 5642 { 5643 win_setwidth_win(width, curwin); 5644 } 5645 5646 void 5647 win_setwidth_win(int width, win_T *wp) 5648 { 5649 // Always keep current window at least one column wide, even when 5650 // 'winminwidth' is zero. 5651 if (wp == curwin) 5652 { 5653 if (width < p_wmw) 5654 width = p_wmw; 5655 if (width == 0) 5656 width = 1; 5657 } 5658 5659 frame_setwidth(wp->w_frame, width + wp->w_vsep_width); 5660 5661 // recompute the window positions 5662 (void)win_comp_pos(); 5663 5664 redraw_all_later(NOT_VALID); 5665 } 5666 5667 /* 5668 * Set the width of a frame to "width" and take care that all frames and 5669 * windows inside it are resized. Also resize frames above and below if the 5670 * are in the same FR_ROW frame. 5671 * 5672 * Strategy is similar to frame_setheight(). 5673 */ 5674 static void 5675 frame_setwidth(frame_T *curfrp, int width) 5676 { 5677 int room; // total number of lines available 5678 int take; // number of lines taken from other windows 5679 int run; 5680 frame_T *frp; 5681 int w; 5682 int room_reserved; 5683 5684 // If the width already is the desired value, nothing to do. 5685 if (curfrp->fr_width == width) 5686 return; 5687 5688 if (curfrp->fr_parent == NULL) 5689 // topframe: can't change width 5690 return; 5691 5692 if (curfrp->fr_parent->fr_layout == FR_COL) 5693 { 5694 // Column of frames: Also need to resize frames above and below of 5695 // this one. First check for the minimal width of these. 5696 w = frame_minwidth(curfrp->fr_parent, NULL); 5697 if (width < w) 5698 width = w; 5699 frame_setwidth(curfrp->fr_parent, width); 5700 } 5701 else 5702 { 5703 /* 5704 * Row of frames: try to change only frames in this row. 5705 * 5706 * Do this twice: 5707 * 1: compute room available, if it's not enough try resizing the 5708 * containing frame. 5709 * 2: compute the room available and adjust the width to it. 5710 */ 5711 for (run = 1; run <= 2; ++run) 5712 { 5713 room = 0; 5714 room_reserved = 0; 5715 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child) 5716 { 5717 if (frp != curfrp 5718 && frp->fr_win != NULL 5719 && frp->fr_win->w_p_wfw) 5720 room_reserved += frp->fr_width; 5721 room += frp->fr_width; 5722 if (frp != curfrp) 5723 room -= frame_minwidth(frp, NULL); 5724 } 5725 5726 if (width <= room) 5727 break; 5728 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL) 5729 { 5730 if (width > room) 5731 width = room; 5732 break; 5733 } 5734 frame_setwidth(curfrp->fr_parent, width 5735 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1); 5736 } 5737 5738 /* 5739 * Compute the number of lines we will take from others frames (can be 5740 * negative!). 5741 */ 5742 take = width - curfrp->fr_width; 5743 5744 // If there is not enough room, also reduce the width of a window 5745 // with 'winfixwidth' set. 5746 if (width > room - room_reserved) 5747 room_reserved = room - width; 5748 // If there is only a 'winfixwidth' window and making the 5749 // window smaller, need to make the other window narrower. 5750 if (take < 0 && room - curfrp->fr_width < room_reserved) 5751 room_reserved = 0; 5752 5753 /* 5754 * set the current frame to the new width 5755 */ 5756 frame_new_width(curfrp, width, FALSE, FALSE); 5757 5758 /* 5759 * First take lines from the frames right of the current frame. If 5760 * that is not enough, takes lines from frames left of the current 5761 * frame. 5762 */ 5763 for (run = 0; run < 2; ++run) 5764 { 5765 if (run == 0) 5766 frp = curfrp->fr_next; // 1st run: start with next window 5767 else 5768 frp = curfrp->fr_prev; // 2nd run: start with prev window 5769 while (frp != NULL && take != 0) 5770 { 5771 w = frame_minwidth(frp, NULL); 5772 if (room_reserved > 0 5773 && frp->fr_win != NULL 5774 && frp->fr_win->w_p_wfw) 5775 { 5776 if (room_reserved >= frp->fr_width) 5777 room_reserved -= frp->fr_width; 5778 else 5779 { 5780 if (frp->fr_width - room_reserved > take) 5781 room_reserved = frp->fr_width - take; 5782 take -= frp->fr_width - room_reserved; 5783 frame_new_width(frp, room_reserved, FALSE, FALSE); 5784 room_reserved = 0; 5785 } 5786 } 5787 else 5788 { 5789 if (frp->fr_width - take < w) 5790 { 5791 take -= frp->fr_width - w; 5792 frame_new_width(frp, w, FALSE, FALSE); 5793 } 5794 else 5795 { 5796 frame_new_width(frp, frp->fr_width - take, 5797 FALSE, FALSE); 5798 take = 0; 5799 } 5800 } 5801 if (run == 0) 5802 frp = frp->fr_next; 5803 else 5804 frp = frp->fr_prev; 5805 } 5806 } 5807 } 5808 } 5809 5810 /* 5811 * Check 'winminheight' for a valid value and reduce it if needed. 5812 */ 5813 void 5814 win_setminheight(void) 5815 { 5816 int room; 5817 int needed; 5818 int first = TRUE; 5819 5820 // loop until there is a 'winminheight' that is possible 5821 while (p_wmh > 0) 5822 { 5823 room = Rows - p_ch; 5824 needed = frame_minheight(topframe, NULL); 5825 if (room >= needed) 5826 break; 5827 --p_wmh; 5828 if (first) 5829 { 5830 emsg(_(e_noroom)); 5831 first = FALSE; 5832 } 5833 } 5834 } 5835 5836 /* 5837 * Check 'winminwidth' for a valid value and reduce it if needed. 5838 */ 5839 void 5840 win_setminwidth(void) 5841 { 5842 int room; 5843 int needed; 5844 int first = TRUE; 5845 5846 // loop until there is a 'winminheight' that is possible 5847 while (p_wmw > 0) 5848 { 5849 room = Columns; 5850 needed = frame_minwidth(topframe, NULL); 5851 if (room >= needed) 5852 break; 5853 --p_wmw; 5854 if (first) 5855 { 5856 emsg(_(e_noroom)); 5857 first = FALSE; 5858 } 5859 } 5860 } 5861 5862 /* 5863 * Status line of dragwin is dragged "offset" lines down (negative is up). 5864 */ 5865 void 5866 win_drag_status_line(win_T *dragwin, int offset) 5867 { 5868 frame_T *curfr; 5869 frame_T *fr; 5870 int room; 5871 int row; 5872 int up; // if TRUE, drag status line up, otherwise down 5873 int n; 5874 5875 fr = dragwin->w_frame; 5876 curfr = fr; 5877 if (fr != topframe) // more than one window 5878 { 5879 fr = fr->fr_parent; 5880 // When the parent frame is not a column of frames, its parent should 5881 // be. 5882 if (fr->fr_layout != FR_COL) 5883 { 5884 curfr = fr; 5885 if (fr != topframe) // only a row of windows, may drag statusline 5886 fr = fr->fr_parent; 5887 } 5888 } 5889 5890 // If this is the last frame in a column, may want to resize the parent 5891 // frame instead (go two up to skip a row of frames). 5892 while (curfr != topframe && curfr->fr_next == NULL) 5893 { 5894 if (fr != topframe) 5895 fr = fr->fr_parent; 5896 curfr = fr; 5897 if (fr != topframe) 5898 fr = fr->fr_parent; 5899 } 5900 5901 if (offset < 0) // drag up 5902 { 5903 up = TRUE; 5904 offset = -offset; 5905 // sum up the room of the current frame and above it 5906 if (fr == curfr) 5907 { 5908 // only one window 5909 room = fr->fr_height - frame_minheight(fr, NULL); 5910 } 5911 else 5912 { 5913 room = 0; 5914 for (fr = fr->fr_child; ; fr = fr->fr_next) 5915 { 5916 room += fr->fr_height - frame_minheight(fr, NULL); 5917 if (fr == curfr) 5918 break; 5919 } 5920 } 5921 fr = curfr->fr_next; // put fr at frame that grows 5922 } 5923 else // drag down 5924 { 5925 up = FALSE; 5926 /* 5927 * Only dragging the last status line can reduce p_ch. 5928 */ 5929 room = Rows - cmdline_row; 5930 if (curfr->fr_next == NULL) 5931 room -= 1; 5932 else 5933 room -= p_ch; 5934 if (room < 0) 5935 room = 0; 5936 // sum up the room of frames below of the current one 5937 FOR_ALL_FRAMES(fr, curfr->fr_next) 5938 room += fr->fr_height - frame_minheight(fr, NULL); 5939 fr = curfr; // put fr at window that grows 5940 } 5941 5942 if (room < offset) // Not enough room 5943 offset = room; // Move as far as we can 5944 if (offset <= 0) 5945 return; 5946 5947 /* 5948 * Grow frame fr by "offset" lines. 5949 * Doesn't happen when dragging the last status line up. 5950 */ 5951 if (fr != NULL) 5952 frame_new_height(fr, fr->fr_height + offset, up, FALSE); 5953 5954 if (up) 5955 fr = curfr; // current frame gets smaller 5956 else 5957 fr = curfr->fr_next; // next frame gets smaller 5958 5959 /* 5960 * Now make the other frames smaller. 5961 */ 5962 while (fr != NULL && offset > 0) 5963 { 5964 n = frame_minheight(fr, NULL); 5965 if (fr->fr_height - offset <= n) 5966 { 5967 offset -= fr->fr_height - n; 5968 frame_new_height(fr, n, !up, FALSE); 5969 } 5970 else 5971 { 5972 frame_new_height(fr, fr->fr_height - offset, !up, FALSE); 5973 break; 5974 } 5975 if (up) 5976 fr = fr->fr_prev; 5977 else 5978 fr = fr->fr_next; 5979 } 5980 row = win_comp_pos(); 5981 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0); 5982 cmdline_row = row; 5983 p_ch = Rows - cmdline_row; 5984 if (p_ch < 1) 5985 p_ch = 1; 5986 curtab->tp_ch_used = p_ch; 5987 redraw_all_later(SOME_VALID); 5988 showmode(); 5989 } 5990 5991 /* 5992 * Separator line of dragwin is dragged "offset" lines right (negative is left). 5993 */ 5994 void 5995 win_drag_vsep_line(win_T *dragwin, int offset) 5996 { 5997 frame_T *curfr; 5998 frame_T *fr; 5999 int room; 6000 int left; // if TRUE, drag separator line left, otherwise right 6001 int n; 6002 6003 fr = dragwin->w_frame; 6004 if (fr == topframe) // only one window (cannot happen?) 6005 return; 6006 curfr = fr; 6007 fr = fr->fr_parent; 6008 // When the parent frame is not a row of frames, its parent should be. 6009 if (fr->fr_layout != FR_ROW) 6010 { 6011 if (fr == topframe) // only a column of windows (cannot happen?) 6012 return; 6013 curfr = fr; 6014 fr = fr->fr_parent; 6015 } 6016 6017 // If this is the last frame in a row, may want to resize a parent 6018 // frame instead. 6019 while (curfr->fr_next == NULL) 6020 { 6021 if (fr == topframe) 6022 break; 6023 curfr = fr; 6024 fr = fr->fr_parent; 6025 if (fr != topframe) 6026 { 6027 curfr = fr; 6028 fr = fr->fr_parent; 6029 } 6030 } 6031 6032 if (offset < 0) // drag left 6033 { 6034 left = TRUE; 6035 offset = -offset; 6036 // sum up the room of the current frame and left of it 6037 room = 0; 6038 for (fr = fr->fr_child; ; fr = fr->fr_next) 6039 { 6040 room += fr->fr_width - frame_minwidth(fr, NULL); 6041 if (fr == curfr) 6042 break; 6043 } 6044 fr = curfr->fr_next; // put fr at frame that grows 6045 } 6046 else // drag right 6047 { 6048 left = FALSE; 6049 // sum up the room of frames right of the current one 6050 room = 0; 6051 FOR_ALL_FRAMES(fr, curfr->fr_next) 6052 room += fr->fr_width - frame_minwidth(fr, NULL); 6053 fr = curfr; // put fr at window that grows 6054 } 6055 6056 if (room < offset) // Not enough room 6057 offset = room; // Move as far as we can 6058 if (offset <= 0) // No room at all, quit. 6059 return; 6060 if (fr == NULL) 6061 return; // Safety check, should not happen. 6062 6063 // grow frame fr by offset lines 6064 frame_new_width(fr, fr->fr_width + offset, left, FALSE); 6065 6066 // shrink other frames: current and at the left or at the right 6067 if (left) 6068 fr = curfr; // current frame gets smaller 6069 else 6070 fr = curfr->fr_next; // next frame gets smaller 6071 6072 while (fr != NULL && offset > 0) 6073 { 6074 n = frame_minwidth(fr, NULL); 6075 if (fr->fr_width - offset <= n) 6076 { 6077 offset -= fr->fr_width - n; 6078 frame_new_width(fr, n, !left, FALSE); 6079 } 6080 else 6081 { 6082 frame_new_width(fr, fr->fr_width - offset, !left, FALSE); 6083 break; 6084 } 6085 if (left) 6086 fr = fr->fr_prev; 6087 else 6088 fr = fr->fr_next; 6089 } 6090 (void)win_comp_pos(); 6091 redraw_all_later(NOT_VALID); 6092 } 6093 6094 #define FRACTION_MULT 16384L 6095 6096 /* 6097 * Set wp->w_fraction for the current w_wrow and w_height. 6098 * Has no effect when the window is less than two lines. 6099 */ 6100 void 6101 set_fraction(win_T *wp) 6102 { 6103 if (wp->w_height > 1) 6104 // When cursor is in the first line the percentage is computed as if 6105 // it's halfway that line. Thus with two lines it is 25%, with three 6106 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc. 6107 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT 6108 + FRACTION_MULT / 2) / (long)wp->w_height; 6109 } 6110 6111 /* 6112 * Set the height of a window. 6113 * "height" excludes any window toolbar. 6114 * This takes care of the things inside the window, not what happens to the 6115 * window position, the frame or to other windows. 6116 */ 6117 void 6118 win_new_height(win_T *wp, int height) 6119 { 6120 int prev_height = wp->w_height; 6121 6122 // Don't want a negative height. Happens when splitting a tiny window. 6123 // Will equalize heights soon to fix it. 6124 if (height < 0) 6125 height = 0; 6126 if (wp->w_height == height) 6127 return; // nothing to do 6128 6129 if (wp->w_height > 0) 6130 { 6131 if (wp == curwin) 6132 // w_wrow needs to be valid. When setting 'laststatus' this may 6133 // call win_new_height() recursively. 6134 validate_cursor(); 6135 if (wp->w_height != prev_height) 6136 return; // Recursive call already changed the size, bail out here 6137 // to avoid the following to mess things up. 6138 if (wp->w_wrow != wp->w_prev_fraction_row) 6139 set_fraction(wp); 6140 } 6141 6142 wp->w_height = height; 6143 wp->w_skipcol = 0; 6144 6145 // There is no point in adjusting the scroll position when exiting. Some 6146 // values might be invalid. 6147 if (!exiting) 6148 scroll_to_fraction(wp, prev_height); 6149 } 6150 6151 void 6152 scroll_to_fraction(win_T *wp, int prev_height) 6153 { 6154 linenr_T lnum; 6155 int sline, line_size; 6156 int height = wp->w_height; 6157 6158 // Don't change w_topline in any of these cases: 6159 // - window height is 0 6160 // - 'scrollbind' is set and this isn't the current window 6161 // - window height is sufficient to display the whole buffer and first line 6162 // is visible. 6163 if (height > 0 6164 && (!wp->w_p_scb || wp == curwin) 6165 && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1)) 6166 { 6167 /* 6168 * Find a value for w_topline that shows the cursor at the same 6169 * relative position in the window as before (more or less). 6170 */ 6171 lnum = wp->w_cursor.lnum; 6172 if (lnum < 1) // can happen when starting up 6173 lnum = 1; 6174 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) 6175 / FRACTION_MULT; 6176 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1; 6177 sline = wp->w_wrow - line_size; 6178 6179 if (sline >= 0) 6180 { 6181 // Make sure the whole cursor line is visible, if possible. 6182 int rows = plines_win(wp, lnum, FALSE); 6183 6184 if (sline > wp->w_height - rows) 6185 { 6186 sline = wp->w_height - rows; 6187 wp->w_wrow -= rows - line_size; 6188 } 6189 } 6190 6191 if (sline < 0) 6192 { 6193 /* 6194 * Cursor line would go off top of screen if w_wrow was this high. 6195 * Make cursor line the first line in the window. If not enough 6196 * room use w_skipcol; 6197 */ 6198 wp->w_wrow = line_size; 6199 if (wp->w_wrow >= wp->w_height 6200 && (wp->w_width - win_col_off(wp)) > 0) 6201 { 6202 wp->w_skipcol += wp->w_width - win_col_off(wp); 6203 --wp->w_wrow; 6204 while (wp->w_wrow >= wp->w_height) 6205 { 6206 wp->w_skipcol += wp->w_width - win_col_off(wp) 6207 + win_col_off2(wp); 6208 --wp->w_wrow; 6209 } 6210 } 6211 } 6212 else if (sline > 0) 6213 { 6214 while (sline > 0 && lnum > 1) 6215 { 6216 #ifdef FEAT_FOLDING 6217 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL); 6218 if (lnum == 1) 6219 { 6220 // first line in buffer is folded 6221 line_size = 1; 6222 --sline; 6223 break; 6224 } 6225 #endif 6226 --lnum; 6227 #ifdef FEAT_DIFF 6228 if (lnum == wp->w_topline) 6229 line_size = plines_win_nofill(wp, lnum, TRUE) 6230 + wp->w_topfill; 6231 else 6232 #endif 6233 line_size = plines_win(wp, lnum, TRUE); 6234 sline -= line_size; 6235 } 6236 6237 if (sline < 0) 6238 { 6239 /* 6240 * Line we want at top would go off top of screen. Use next 6241 * line instead. 6242 */ 6243 #ifdef FEAT_FOLDING 6244 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL); 6245 #endif 6246 lnum++; 6247 wp->w_wrow -= line_size + sline; 6248 } 6249 else if (sline > 0) 6250 { 6251 // First line of file reached, use that as topline. 6252 lnum = 1; 6253 wp->w_wrow -= sline; 6254 } 6255 } 6256 set_topline(wp, lnum); 6257 } 6258 6259 if (wp == curwin) 6260 { 6261 if (get_scrolloff_value()) 6262 update_topline(); 6263 curs_columns(FALSE); // validate w_wrow 6264 } 6265 if (prev_height > 0) 6266 wp->w_prev_fraction_row = wp->w_wrow; 6267 6268 win_comp_scroll(wp); 6269 redraw_win_later(wp, SOME_VALID); 6270 wp->w_redr_status = TRUE; 6271 invalidate_botline_win(wp); 6272 } 6273 6274 /* 6275 * Set the width of a window. 6276 */ 6277 void 6278 win_new_width(win_T *wp, int width) 6279 { 6280 wp->w_width = width; 6281 wp->w_lines_valid = 0; 6282 changed_line_abv_curs_win(wp); 6283 invalidate_botline_win(wp); 6284 if (wp == curwin) 6285 { 6286 update_topline(); 6287 curs_columns(TRUE); // validate w_wrow 6288 } 6289 redraw_win_later(wp, NOT_VALID); 6290 wp->w_redr_status = TRUE; 6291 } 6292 6293 void 6294 win_comp_scroll(win_T *wp) 6295 { 6296 wp->w_p_scr = ((unsigned)wp->w_height >> 1); 6297 if (wp->w_p_scr == 0) 6298 wp->w_p_scr = 1; 6299 } 6300 6301 /* 6302 * command_height: called whenever p_ch has been changed 6303 */ 6304 void 6305 command_height(void) 6306 { 6307 int h; 6308 frame_T *frp; 6309 int old_p_ch = curtab->tp_ch_used; 6310 6311 // Use the value of p_ch that we remembered. This is needed for when the 6312 // GUI starts up, we can't be sure in what order things happen. And when 6313 // p_ch was changed in another tab page. 6314 curtab->tp_ch_used = p_ch; 6315 6316 // Find bottom frame with width of screen. 6317 frp = lastwin->w_frame; 6318 while (frp->fr_width != Columns && frp->fr_parent != NULL) 6319 frp = frp->fr_parent; 6320 6321 // Avoid changing the height of a window with 'winfixheight' set. 6322 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF 6323 && frp->fr_win->w_p_wfh) 6324 frp = frp->fr_prev; 6325 6326 if (starting != NO_SCREEN) 6327 { 6328 cmdline_row = Rows - p_ch; 6329 6330 if (p_ch > old_p_ch) // p_ch got bigger 6331 { 6332 while (p_ch > old_p_ch) 6333 { 6334 if (frp == NULL) 6335 { 6336 emsg(_(e_noroom)); 6337 p_ch = old_p_ch; 6338 curtab->tp_ch_used = p_ch; 6339 cmdline_row = Rows - p_ch; 6340 break; 6341 } 6342 h = frp->fr_height - frame_minheight(frp, NULL); 6343 if (h > p_ch - old_p_ch) 6344 h = p_ch - old_p_ch; 6345 old_p_ch += h; 6346 frame_add_height(frp, -h); 6347 frp = frp->fr_prev; 6348 } 6349 6350 // Recompute window positions. 6351 (void)win_comp_pos(); 6352 6353 // clear the lines added to cmdline 6354 if (full_screen) 6355 screen_fill((int)(cmdline_row), (int)Rows, 0, 6356 (int)Columns, ' ', ' ', 0); 6357 msg_row = cmdline_row; 6358 redraw_cmdline = TRUE; 6359 return; 6360 } 6361 6362 if (msg_row < cmdline_row) 6363 msg_row = cmdline_row; 6364 redraw_cmdline = TRUE; 6365 } 6366 frame_add_height(frp, (int)(old_p_ch - p_ch)); 6367 6368 // Recompute window positions. 6369 if (frp != lastwin->w_frame) 6370 (void)win_comp_pos(); 6371 } 6372 6373 /* 6374 * Resize frame "frp" to be "n" lines higher (negative for less high). 6375 * Also resize the frames it is contained in. 6376 */ 6377 static void 6378 frame_add_height(frame_T *frp, int n) 6379 { 6380 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE); 6381 for (;;) 6382 { 6383 frp = frp->fr_parent; 6384 if (frp == NULL) 6385 break; 6386 frp->fr_height += n; 6387 } 6388 } 6389 6390 /* 6391 * Add or remove a status line for the bottom window(s), according to the 6392 * value of 'laststatus'. 6393 */ 6394 void 6395 last_status( 6396 int morewin) // pretend there are two or more windows 6397 { 6398 // Don't make a difference between horizontal or vertical split. 6399 last_status_rec(topframe, (p_ls == 2 6400 || (p_ls == 1 && (morewin || !ONE_WINDOW)))); 6401 } 6402 6403 static void 6404 last_status_rec(frame_T *fr, int statusline) 6405 { 6406 frame_T *fp; 6407 win_T *wp; 6408 6409 if (fr->fr_layout == FR_LEAF) 6410 { 6411 wp = fr->fr_win; 6412 if (wp->w_status_height != 0 && !statusline) 6413 { 6414 // remove status line 6415 win_new_height(wp, wp->w_height + 1); 6416 wp->w_status_height = 0; 6417 comp_col(); 6418 } 6419 else if (wp->w_status_height == 0 && statusline) 6420 { 6421 // Find a frame to take a line from. 6422 fp = fr; 6423 while (fp->fr_height <= frame_minheight(fp, NULL)) 6424 { 6425 if (fp == topframe) 6426 { 6427 emsg(_(e_noroom)); 6428 return; 6429 } 6430 // In a column of frames: go to frame above. If already at 6431 // the top or in a row of frames: go to parent. 6432 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL) 6433 fp = fp->fr_prev; 6434 else 6435 fp = fp->fr_parent; 6436 } 6437 wp->w_status_height = 1; 6438 if (fp != fr) 6439 { 6440 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE); 6441 frame_fix_height(wp); 6442 (void)win_comp_pos(); 6443 } 6444 else 6445 win_new_height(wp, wp->w_height - 1); 6446 comp_col(); 6447 redraw_all_later(SOME_VALID); 6448 } 6449 } 6450 else if (fr->fr_layout == FR_ROW) 6451 { 6452 // vertically split windows, set status line for each one 6453 FOR_ALL_FRAMES(fp, fr->fr_child) 6454 last_status_rec(fp, statusline); 6455 } 6456 else 6457 { 6458 // horizontally split window, set status line for last one 6459 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next) 6460 ; 6461 last_status_rec(fp, statusline); 6462 } 6463 } 6464 6465 /* 6466 * Return the number of lines used by the tab page line. 6467 */ 6468 int 6469 tabline_height(void) 6470 { 6471 #ifdef FEAT_GUI_TABLINE 6472 // When the GUI has the tabline then this always returns zero. 6473 if (gui_use_tabline()) 6474 return 0; 6475 #endif 6476 switch (p_stal) 6477 { 6478 case 0: return 0; 6479 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1; 6480 } 6481 return 1; 6482 } 6483 6484 /* 6485 * Return the minimal number of rows that is needed on the screen to display 6486 * the current number of windows. 6487 */ 6488 int 6489 min_rows(void) 6490 { 6491 int total; 6492 tabpage_T *tp; 6493 int n; 6494 6495 if (firstwin == NULL) // not initialized yet 6496 return MIN_LINES; 6497 6498 total = 0; 6499 FOR_ALL_TABPAGES(tp) 6500 { 6501 n = frame_minheight(tp->tp_topframe, NULL); 6502 if (total < n) 6503 total = n; 6504 } 6505 total += tabline_height(); 6506 total += 1; // count the room for the command line 6507 return total; 6508 } 6509 6510 /* 6511 * Return TRUE if there is only one window and only one tab page, not 6512 * counting a help or preview window, unless it is the current window. 6513 * Does not count unlisted windows. 6514 */ 6515 int 6516 only_one_window(void) 6517 { 6518 int count = 0; 6519 win_T *wp; 6520 6521 #if defined(FEAT_PROP_POPUP) 6522 // If the current window is a popup then there always is another window. 6523 if (popup_is_popup(curwin)) 6524 return FALSE; 6525 #endif 6526 6527 // If there is another tab page there always is another window. 6528 if (first_tabpage->tp_next != NULL) 6529 return FALSE; 6530 6531 FOR_ALL_WINDOWS(wp) 6532 if (wp->w_buffer != NULL 6533 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf)) 6534 # ifdef FEAT_QUICKFIX 6535 || wp->w_p_pvw 6536 # endif 6537 ) || wp == curwin) && wp != aucmd_win) 6538 ++count; 6539 return (count <= 1); 6540 } 6541 6542 /* 6543 * Correct the cursor line number in other windows. Used after changing the 6544 * current buffer, and before applying autocommands. 6545 * When "do_curwin" is TRUE, also check current window. 6546 */ 6547 void 6548 check_lnums(int do_curwin) 6549 { 6550 win_T *wp; 6551 tabpage_T *tp; 6552 6553 FOR_ALL_TAB_WINDOWS(tp, wp) 6554 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf) 6555 { 6556 // save the original cursor position and topline 6557 wp->w_save_cursor.w_cursor_save = wp->w_cursor; 6558 wp->w_save_cursor.w_topline_save = wp->w_topline; 6559 6560 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count) 6561 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count; 6562 if (wp->w_topline > curbuf->b_ml.ml_line_count) 6563 wp->w_topline = curbuf->b_ml.ml_line_count; 6564 6565 // save the corrected cursor position and topline 6566 wp->w_save_cursor.w_cursor_corr = wp->w_cursor; 6567 wp->w_save_cursor.w_topline_corr = wp->w_topline; 6568 } 6569 } 6570 6571 /* 6572 * Reset cursor and topline to its stored values from check_lnums(). 6573 * check_lnums() must have been called first! 6574 */ 6575 void 6576 reset_lnums() 6577 { 6578 win_T *wp; 6579 tabpage_T *tp; 6580 6581 FOR_ALL_TAB_WINDOWS(tp, wp) 6582 if (wp->w_buffer == curbuf) 6583 { 6584 // Restore the value if the autocommand didn't change it. 6585 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor)) 6586 wp->w_cursor = wp->w_save_cursor.w_cursor_save; 6587 if (wp->w_save_cursor.w_topline_corr == wp->w_topline) 6588 wp->w_topline = wp->w_save_cursor.w_topline_save; 6589 } 6590 } 6591 6592 /* 6593 * A snapshot of the window sizes, to restore them after closing the help 6594 * window. 6595 * Only these fields are used: 6596 * fr_layout 6597 * fr_width 6598 * fr_height 6599 * fr_next 6600 * fr_child 6601 * fr_win (only valid for the old curwin, NULL otherwise) 6602 */ 6603 6604 /* 6605 * Create a snapshot of the current frame sizes. 6606 */ 6607 void 6608 make_snapshot(int idx) 6609 { 6610 clear_snapshot(curtab, idx); 6611 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]); 6612 } 6613 6614 static void 6615 make_snapshot_rec(frame_T *fr, frame_T **frp) 6616 { 6617 *frp = ALLOC_CLEAR_ONE(frame_T); 6618 if (*frp == NULL) 6619 return; 6620 (*frp)->fr_layout = fr->fr_layout; 6621 (*frp)->fr_width = fr->fr_width; 6622 (*frp)->fr_height = fr->fr_height; 6623 if (fr->fr_next != NULL) 6624 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next)); 6625 if (fr->fr_child != NULL) 6626 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child)); 6627 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin) 6628 (*frp)->fr_win = curwin; 6629 } 6630 6631 /* 6632 * Remove any existing snapshot. 6633 */ 6634 static void 6635 clear_snapshot(tabpage_T *tp, int idx) 6636 { 6637 clear_snapshot_rec(tp->tp_snapshot[idx]); 6638 tp->tp_snapshot[idx] = NULL; 6639 } 6640 6641 static void 6642 clear_snapshot_rec(frame_T *fr) 6643 { 6644 if (fr != NULL) 6645 { 6646 clear_snapshot_rec(fr->fr_next); 6647 clear_snapshot_rec(fr->fr_child); 6648 vim_free(fr); 6649 } 6650 } 6651 6652 /* 6653 * Restore a previously created snapshot, if there is any. 6654 * This is only done if the screen size didn't change and the window layout is 6655 * still the same. 6656 */ 6657 void 6658 restore_snapshot( 6659 int idx, 6660 int close_curwin) // closing current window 6661 { 6662 win_T *wp; 6663 6664 if (curtab->tp_snapshot[idx] != NULL 6665 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width 6666 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height 6667 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK) 6668 { 6669 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe); 6670 win_comp_pos(); 6671 if (wp != NULL && close_curwin) 6672 win_goto(wp); 6673 redraw_all_later(NOT_VALID); 6674 } 6675 clear_snapshot(curtab, idx); 6676 } 6677 6678 /* 6679 * Check if frames "sn" and "fr" have the same layout, same following frames 6680 * and same children. And the window pointer is valid. 6681 */ 6682 static int 6683 check_snapshot_rec(frame_T *sn, frame_T *fr) 6684 { 6685 if (sn->fr_layout != fr->fr_layout 6686 || (sn->fr_next == NULL) != (fr->fr_next == NULL) 6687 || (sn->fr_child == NULL) != (fr->fr_child == NULL) 6688 || (sn->fr_next != NULL 6689 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL) 6690 || (sn->fr_child != NULL 6691 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL) 6692 || (sn->fr_win != NULL && !win_valid(sn->fr_win))) 6693 return FAIL; 6694 return OK; 6695 } 6696 6697 /* 6698 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all 6699 * following frames and children. 6700 * Returns a pointer to the old current window, or NULL. 6701 */ 6702 static win_T * 6703 restore_snapshot_rec(frame_T *sn, frame_T *fr) 6704 { 6705 win_T *wp = NULL; 6706 win_T *wp2; 6707 6708 fr->fr_height = sn->fr_height; 6709 fr->fr_width = sn->fr_width; 6710 if (fr->fr_layout == FR_LEAF) 6711 { 6712 frame_new_height(fr, fr->fr_height, FALSE, FALSE); 6713 frame_new_width(fr, fr->fr_width, FALSE, FALSE); 6714 wp = sn->fr_win; 6715 } 6716 if (sn->fr_next != NULL) 6717 { 6718 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next); 6719 if (wp2 != NULL) 6720 wp = wp2; 6721 } 6722 if (sn->fr_child != NULL) 6723 { 6724 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child); 6725 if (wp2 != NULL) 6726 wp = wp2; 6727 } 6728 return wp; 6729 } 6730 6731 #if defined(FEAT_GUI) || defined(PROTO) 6732 /* 6733 * Return TRUE if there is any vertically split window. 6734 */ 6735 int 6736 win_hasvertsplit(void) 6737 { 6738 frame_T *fr; 6739 6740 if (topframe->fr_layout == FR_ROW) 6741 return TRUE; 6742 6743 if (topframe->fr_layout == FR_COL) 6744 FOR_ALL_FRAMES(fr, topframe->fr_child) 6745 if (fr->fr_layout == FR_ROW) 6746 return TRUE; 6747 6748 return FALSE; 6749 } 6750 #endif 6751 6752 #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO) 6753 int 6754 get_win_number(win_T *wp, win_T *first_win) 6755 { 6756 int i = 1; 6757 win_T *w; 6758 6759 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w)) 6760 ++i; 6761 6762 if (w == NULL) 6763 return 0; 6764 else 6765 return i; 6766 } 6767 6768 int 6769 get_tab_number(tabpage_T *tp UNUSED) 6770 { 6771 int i = 1; 6772 tabpage_T *t; 6773 6774 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next) 6775 ++i; 6776 6777 if (t == NULL) 6778 return 0; 6779 else 6780 return i; 6781 } 6782 #endif 6783 6784 /* 6785 * Return TRUE if "topfrp" and its children are at the right height. 6786 */ 6787 static int 6788 frame_check_height(frame_T *topfrp, int height) 6789 { 6790 frame_T *frp; 6791 6792 if (topfrp->fr_height != height) 6793 return FALSE; 6794 6795 if (topfrp->fr_layout == FR_ROW) 6796 FOR_ALL_FRAMES(frp, topfrp->fr_child) 6797 if (frp->fr_height != height) 6798 return FALSE; 6799 6800 return TRUE; 6801 } 6802 6803 /* 6804 * Return TRUE if "topfrp" and its children are at the right width. 6805 */ 6806 static int 6807 frame_check_width(frame_T *topfrp, int width) 6808 { 6809 frame_T *frp; 6810 6811 if (topfrp->fr_width != width) 6812 return FALSE; 6813 6814 if (topfrp->fr_layout == FR_COL) 6815 FOR_ALL_FRAMES(frp, topfrp->fr_child) 6816 if (frp->fr_width != width) 6817 return FALSE; 6818 6819 return TRUE; 6820 } 6821 6822 #if defined(FEAT_SYN_HL) || defined(PROTO) 6823 /* 6824 * Simple int comparison function for use with qsort() 6825 */ 6826 static int 6827 int_cmp(const void *a, const void *b) 6828 { 6829 return *(const int *)a - *(const int *)b; 6830 } 6831 6832 /* 6833 * Handle setting 'colorcolumn' or 'textwidth' in window "wp". 6834 * Returns error message, NULL if it's OK. 6835 */ 6836 char * 6837 check_colorcolumn(win_T *wp) 6838 { 6839 char_u *s; 6840 int col; 6841 int count = 0; 6842 int color_cols[256]; 6843 int i; 6844 int j = 0; 6845 6846 if (wp->w_buffer == NULL) 6847 return NULL; // buffer was closed 6848 6849 for (s = wp->w_p_cc; *s != NUL && count < 255;) 6850 { 6851 if (*s == '-' || *s == '+') 6852 { 6853 // -N and +N: add to 'textwidth' 6854 col = (*s == '-') ? -1 : 1; 6855 ++s; 6856 if (!VIM_ISDIGIT(*s)) 6857 return e_invarg; 6858 col = col * getdigits(&s); 6859 if (wp->w_buffer->b_p_tw == 0) 6860 goto skip; // 'textwidth' not set, skip this item 6861 col += wp->w_buffer->b_p_tw; 6862 if (col < 0) 6863 goto skip; 6864 } 6865 else if (VIM_ISDIGIT(*s)) 6866 col = getdigits(&s); 6867 else 6868 return e_invarg; 6869 color_cols[count++] = col - 1; // 1-based to 0-based 6870 skip: 6871 if (*s == NUL) 6872 break; 6873 if (*s != ',') 6874 return e_invarg; 6875 if (*++s == NUL) 6876 return e_invarg; // illegal trailing comma as in "set cc=80," 6877 } 6878 6879 vim_free(wp->w_p_cc_cols); 6880 if (count == 0) 6881 wp->w_p_cc_cols = NULL; 6882 else 6883 { 6884 wp->w_p_cc_cols = ALLOC_MULT(int, count + 1); 6885 if (wp->w_p_cc_cols != NULL) 6886 { 6887 // sort the columns for faster usage on screen redraw inside 6888 // win_line() 6889 qsort(color_cols, count, sizeof(int), int_cmp); 6890 6891 for (i = 0; i < count; ++i) 6892 // skip duplicates 6893 if (j == 0 || wp->w_p_cc_cols[j - 1] != color_cols[i]) 6894 wp->w_p_cc_cols[j++] = color_cols[i]; 6895 wp->w_p_cc_cols[j] = -1; // end marker 6896 } 6897 } 6898 6899 return NULL; // no error 6900 } 6901 #endif 6902