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