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