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