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