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