robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// eval/window.c: Window related builtin functions 2 3#include <stdbool.h> 4#include <stdint.h> 5#include <stdio.h> 6#include <stdlib.h> 7#include <string.h> 8 9#include "nvim/ascii_defs.h" 10#include "nvim/autocmd.h" 11#include "nvim/buffer.h" 12#include "nvim/charset.h" 13#include "nvim/context.h" 14#include "nvim/cursor.h" 15#include "nvim/errors.h" 16#include "nvim/eval/funcs.h" 17#include "nvim/eval/typval.h" 18#include "nvim/eval/typval_defs.h" 19#include "nvim/eval/window.h" 20#include "nvim/ex_getln.h" 21#include "nvim/garray.h" 22#include "nvim/garray_defs.h" 23#include "nvim/gettext_defs.h" 24#include "nvim/globals.h" 25#include "nvim/macros_defs.h" 26#include "nvim/mark_defs.h" 27#include "nvim/memory.h" 28#include "nvim/message.h" 29#include "nvim/move.h" 30#include "nvim/normal.h" 31#include "nvim/option_vars.h" 32#include "nvim/os/fs.h" 33#include "nvim/pos_defs.h" 34#include "nvim/strings.h" 35#include "nvim/types_defs.h" 36#include "nvim/vim_defs.h" 37#include "nvim/window.h" 38 39#include "eval/window.c.generated.h" 40 41static const char e_cannot_resize_window_in_another_tab_page [] 42= N_ ("E1308: Cannot resize a window in another tab page" ); 43 44bool win_has_winnr (win_T * wp ,tabpage_T * tp ) 45FUNC_ATTR_NONNULL_ALL 46{ 47return (wp == (tp == curtab ?curwin :tp -> tp_curwin )) 48|| (!wp -> w_config .hide && wp -> w_config .focusable ); 49} 50 51win_T * win_find_nr (int nr ,tabpage_T * tp ) 52{ 53if (tp == NULL ) { 54assert (curtab ); 55tp = curtab ; 56 } 57FOR_ALL_WINDOWS_IN_TAB (wp ,tp ) { 58if ((nr -= win_has_winnr (wp ,tp )) <=0 ) { 59return wp ; 60 } 61 } 62return NULL ; 63} 64 65static int win_getid (typval_T * argvars ) 66{ 67if (argvars [0 ].v_type == VAR_UNKNOWN ) { 68return curwin -> handle ; 69 } 70int winnr = (int )tv_get_number (& argvars [0 ]); 71win_T * wp ; 72if (winnr <=0 ) { 73return 0 ; 74 } 75 76tabpage_T * tp = NULL ; 77if (argvars [1 ].v_type == VAR_UNKNOWN ) { 78tp = curtab ; 79wp = firstwin ; 80 }else { 81int tabnr = (int )tv_get_number (& argvars [1 ]); 82FOR_ALL_TABS (tp2 ) { 83if (-- tabnr == 0 ) { 84tp = tp2 ; 85break ; 86 } 87 } 88if (tp == NULL ) { 89return -1 ; 90 } 91if (tp == curtab ) { 92wp = firstwin ; 93 }else { 94wp = tp -> tp_firstwin ; 95 } 96 } 97for (;wp != NULL ;wp = wp -> w_next ) { 98if ((winnr -= win_has_winnr (wp ,tp ))== 0 ) { 99return wp -> handle ; 100 } 101 } 102return 0 ; 103} 104 105static void win_id2tabwin (typval_T * const argvars ,typval_T * const rettv ) 106{ 107handle_T id = (handle_T )tv_get_number (& argvars [0 ]); 108 109int winnr = 1 ; 110int tabnr = 1 ; 111win_get_tabwin (id ,& tabnr ,& winnr ); 112 113list_T * const list = tv_list_alloc_ret (rettv ,2 ); 114tv_list_append_number (list ,tabnr ); 115tv_list_append_number (list ,winnr ); 116} 117 118win_T * win_id2wp (int id ) 119{ 120return win_id2wp_tp (id ,NULL ); 121} 122 123/// Return the window and tab pointer of window "id". 124/// Returns NULL when not found. 125win_T * win_id2wp_tp (int id ,tabpage_T ** tpp ) 126{ 127FOR_ALL_TAB_WINDOWS (tp ,wp ) { 128if (wp -> handle == id ) { 129if (tpp != NULL ) { 130* tpp = tp ; 131 } 132return wp ; 133 } 134 } 135 136return NULL ; 137} 138 139static int win_id2win (typval_T * argvars ) 140{ 141int nr = 1 ; 142int id = (int )tv_get_number (& argvars [0 ]); 143 144FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 145if (wp -> handle == id ) { 146return (win_has_winnr (wp ,curtab ) ?nr :0 ); 147 } 148nr += win_has_winnr (wp ,curtab ); 149 } 150return 0 ; 151} 152 153void win_findbuf (typval_T * argvars ,list_T * list ) 154{ 155int bufnr = (int )tv_get_number (& argvars [0 ]); 156 157FOR_ALL_TAB_WINDOWS (tp ,wp ) { 158if (wp -> w_buffer -> b_fnum == bufnr ) { 159tv_list_append_number (list ,wp -> handle ); 160 } 161 } 162} 163 164/// Find window specified by "vp" in tabpage "tp". 165/// 166/// @param tp NULL for current tab page 167/// @return current window if "vp" is number zero. 168/// NULL if not found. 169win_T * find_win_by_nr (typval_T * vp ,tabpage_T * tp ) 170{ 171int nr = (int )tv_get_number_chk (vp ,NULL ); 172 173if (nr < 0 ) { 174return NULL ; 175 } 176 177if (nr == 0 ) { 178return curwin ; 179 } 180 181// This method accepts NULL as an alias for curtab. 182if (tp == NULL ) { 183tp = curtab ; 184 } 185 186if (nr >=LOWEST_WIN_ID ) { 187// window id 188FOR_ALL_WINDOWS_IN_TAB (wp ,tp ) { 189if (wp -> handle == nr ) { 190return wp ; 191 } 192 } 193return NULL ; 194 } 195 196return win_find_nr (nr ,tp ); 197} 198 199/// Find a window: When using a Window ID in any tab page, when using a number 200/// in the current tab page. 201/// Returns NULL when not found. 202win_T * find_win_by_nr_or_id (typval_T * vp ) 203{ 204int nr = (int )tv_get_number_chk (vp ,NULL ); 205 206if (nr >=LOWEST_WIN_ID ) { 207return win_id2wp ((int )tv_get_number (vp )); 208 } 209 210return find_win_by_nr (vp ,NULL ); 211} 212 213/// Find window specified by "wvp" in tabpage "tvp". 214win_T * find_tabwin (typval_T * wvp ,typval_T * tvp ) 215{ 216win_T * wp = NULL ; 217tabpage_T * tp = NULL ; 218 219if (wvp -> v_type != VAR_UNKNOWN ) { 220if (tvp -> v_type != VAR_UNKNOWN ) { 221int n = (int )tv_get_number (tvp ); 222if (n >=0 ) { 223tp = find_tabpage (n ); 224 } 225 }else { 226tp = curtab ; 227 } 228 229if (tp != NULL ) { 230wp = find_win_by_nr (wvp ,tp ); 231 } 232 }else { 233wp = curwin ; 234 } 235 236return wp ; 237} 238 239/// Get the layout of the given tab page for winlayout(). 240static void get_framelayout (const frame_T * fr ,list_T * l ,bool outer ) 241{ 242if (fr == NULL ) { 243return ; 244 } 245 246list_T * fr_list ; 247if (outer ) { 248// outermost call from f_winlayout() 249fr_list = l ; 250 }else { 251fr_list = tv_list_alloc (2 ); 252tv_list_append_list (l ,fr_list ); 253 } 254 255if (fr -> fr_layout == FR_LEAF ) { 256if (fr -> fr_win != NULL ) { 257tv_list_append_string (fr_list ,S_LEN ("leaf" )); 258tv_list_append_number (fr_list ,fr -> fr_win -> handle ); 259 } 260 }else { 261if (fr -> fr_layout == FR_ROW ) { 262tv_list_append_string (fr_list ,S_LEN ("row" )); 263 }else { 264tv_list_append_string (fr_list ,S_LEN ("col" )); 265 } 266 267list_T * const win_list = tv_list_alloc (kListLenUnknown ); 268tv_list_append_list (fr_list ,win_list ); 269const frame_T * child = fr -> fr_child ; 270while (child != NULL ) { 271get_framelayout (child ,win_list , false); 272child = child -> fr_next ; 273 } 274 } 275} 276 277/// Common code for tabpagewinnr() and winnr(). 278static int get_winnr (tabpage_T * tp ,typval_T * argvar ) 279{ 280int nr = 1 ; 281 282win_T * twin = (tp == curtab ) ?curwin :tp -> tp_curwin ; 283if (argvar -> v_type != VAR_UNKNOWN ) { 284bool invalid_arg = false; 285const char * const arg = tv_get_string_chk (argvar ); 286if (arg == NULL ) { 287nr = 0 ;// Type error; errmsg already given. 288 }else if (strcmp (arg ,"$" )== 0 ) { 289twin = (tp == curtab ) ?lastwin :tp -> tp_lastwin ; 290 }else if (strcmp (arg ,"#" )== 0 ) { 291twin = (tp == curtab ) ?prevwin :tp -> tp_prevwin ; 292if (twin == NULL ) { 293nr = 0 ; 294 } 295 }else { 296// Extract the window count (if specified). e.g. winnr('3j') 297char * endp = (char * )arg ; 298int count = getdigits_int (& endp , false,0 ); 299if (count <=0 ) { 300// if count is not specified, default to 1 301count = 1 ; 302 } 303if (endp != NULL && * endp != NUL ) { 304if (strequal (endp ,"j" )) { 305twin = win_vert_neighbor (tp ,twin , false,count ); 306 }else if (strequal (endp ,"k" )) { 307twin = win_vert_neighbor (tp ,twin , true,count ); 308 }else if (strequal (endp ,"h" )) { 309twin = win_horz_neighbor (tp ,twin , true,count ); 310 }else if (strequal (endp ,"l" )) { 311twin = win_horz_neighbor (tp ,twin , false,count ); 312 }else { 313invalid_arg = true; 314 } 315 }else { 316invalid_arg = true; 317 } 318 } 319 320if (invalid_arg ) { 321semsg (_ (e_invexpr2 ),arg ); 322nr = 0 ; 323 } 324 }else if (!win_has_winnr (twin ,tp )) { 325nr = 0 ; 326 } 327 328if (nr <=0 ) { 329return 0 ; 330 } 331 332nr = 0 ; 333win_T * wp = (tp == curtab ) ?firstwin :tp -> tp_firstwin ; 334for (;wp != NULL ;wp = wp -> w_next ) { 335nr += win_has_winnr (wp ,tp ); 336if (wp == twin ) { 337break ; 338 } 339 } 340if (wp == NULL ) { 341nr = 0 ;// didn't find it in this tabpage 342 } 343return nr ; 344} 345 346/// @return information about a window as a dictionary. 347static dict_T * get_win_info (win_T * wp ,int16_t tpnr ,int16_t winnr ) 348{ 349dict_T * const dict = tv_dict_alloc (); 350 351// make sure w_botline is valid 352validate_botline_win (wp ); 353 354tv_dict_add_nr (dict ,S_LEN ("tabnr" ),tpnr ); 355tv_dict_add_nr (dict ,S_LEN ("winnr" ),winnr ); 356tv_dict_add_nr (dict ,S_LEN ("winid" ),wp -> handle ); 357tv_dict_add_nr (dict ,S_LEN ("height" ),wp -> w_view_height ); 358tv_dict_add_nr (dict ,S_LEN ("status_height" ),wp -> w_status_height ); 359tv_dict_add_nr (dict ,S_LEN ("winrow" ),wp -> w_winrow + 1 ); 360tv_dict_add_nr (dict ,S_LEN ("topline" ),wp -> w_topline ); 361tv_dict_add_nr (dict ,S_LEN ("botline" ),wp -> w_botline - 1 ); 362tv_dict_add_nr (dict ,S_LEN ("leftcol" ),wp -> w_leftcol ); 363tv_dict_add_nr (dict ,S_LEN ("winbar" ),wp -> w_winbar_height ); 364tv_dict_add_nr (dict ,S_LEN ("width" ),wp -> w_view_width ); 365tv_dict_add_nr (dict ,S_LEN ("bufnr" ),wp -> w_buffer -> b_fnum ); 366tv_dict_add_nr (dict ,S_LEN ("wincol" ),wp -> w_wincol + 1 ); 367tv_dict_add_nr (dict ,S_LEN ("textoff" ),win_col_off (wp )); 368tv_dict_add_nr (dict ,S_LEN ("terminal" ),bt_terminal (wp -> w_buffer )); 369tv_dict_add_nr (dict ,S_LEN ("quickfix" ),bt_quickfix (wp -> w_buffer )); 370tv_dict_add_nr (dict ,S_LEN ("loclist" ), 371 (bt_quickfix (wp -> w_buffer )&& wp -> w_llist_ref != NULL )); 372 373// Add a reference to window variables 374tv_dict_add_dict (dict ,S_LEN ("variables" ),wp -> w_vars ); 375 376return dict ; 377} 378 379/// @return information (variables, options, etc.) about a tab page 380/// as a dictionary. 381static dict_T * get_tabpage_info (tabpage_T * tp ,int tp_idx ) 382{ 383dict_T * const dict = tv_dict_alloc (); 384 385tv_dict_add_nr (dict ,S_LEN ("tabnr" ),tp_idx ); 386 387list_T * const l = tv_list_alloc (kListLenMayKnow ); 388FOR_ALL_WINDOWS_IN_TAB (wp ,tp ) { 389tv_list_append_number (l , (varnumber_T )wp -> handle ); 390 } 391tv_dict_add_list (dict ,S_LEN ("windows" ),l ); 392 393// Make a reference to tabpage variables 394tv_dict_add_dict (dict ,S_LEN ("variables" ),tp -> tp_vars ); 395 396return dict ; 397} 398 399/// "gettabinfo()" function 400void f_gettabinfo (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 401{ 402tabpage_T * tparg = NULL ; 403 404tv_list_alloc_ret (rettv , (argvars [0 ].v_type == VAR_UNKNOWN 405 ?1 406 :kListLenMayKnow )); 407 408if (argvars [0 ].v_type != VAR_UNKNOWN ) { 409// Information about one tab page 410tparg = find_tabpage ((int )tv_get_number_chk (& argvars [0 ],NULL )); 411if (tparg == NULL ) { 412return ; 413 } 414 } 415 416// Get information about a specific tab page or all tab pages 417int tpnr = 0 ; 418FOR_ALL_TABS (tp ) { 419tpnr ++ ; 420if (tparg != NULL && tp != tparg ) { 421continue ; 422 } 423dict_T * const d = get_tabpage_info (tp ,tpnr ); 424tv_list_append_dict (rettv -> vval .v_list ,d ); 425if (tparg != NULL ) { 426return ; 427 } 428 } 429} 430 431/// "getwininfo()" function 432void f_getwininfo (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 433{ 434win_T * wparg = NULL ; 435 436tv_list_alloc_ret (rettv ,kListLenMayKnow ); 437 438if (argvars [0 ].v_type != VAR_UNKNOWN ) { 439wparg = win_id2wp ((int )tv_get_number (& argvars [0 ])); 440if (wparg == NULL ) { 441return ; 442 } 443 } 444 445// Collect information about either all the windows across all the tab 446// pages or one particular window. 447int16_t tabnr = 0 ; 448FOR_ALL_TABS (tp ) { 449tabnr ++ ; 450int16_t winnr = 0 ; 451FOR_ALL_WINDOWS_IN_TAB (wp ,tp ) { 452winnr += win_has_winnr (wp ,tp ); 453if (wparg != NULL && wp != wparg ) { 454continue ; 455 } 456dict_T * const d = get_win_info (wp ,tabnr ,win_has_winnr (wp ,tp ) ?winnr :0 ); 457tv_list_append_dict (rettv -> vval .v_list ,d ); 458if (wparg != NULL ) { 459// found information about a specific window 460return ; 461 } 462 } 463 } 464} 465 466/// "getwinpos({timeout})" function 467void f_getwinpos (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 468{ 469tv_list_alloc_ret (rettv ,2 ); 470tv_list_append_number (rettv -> vval .v_list ,-1 ); 471tv_list_append_number (rettv -> vval .v_list ,-1 ); 472} 473 474/// "getwinposx()" function 475void f_getwinposx (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 476{ 477rettv -> vval .v_number = -1 ; 478} 479 480/// "getwinposy()" function 481void f_getwinposy (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 482{ 483rettv -> vval .v_number = -1 ; 484} 485 486/// "tabpagenr()" function 487void f_tabpagenr (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 488{ 489int nr = 1 ; 490 491if (argvars [0 ].v_type != VAR_UNKNOWN ) { 492const char * const arg = tv_get_string_chk (& argvars [0 ]); 493nr = 0 ; 494if (arg != NULL ) { 495if (strcmp (arg ,"$" )== 0 ) { 496nr = tabpage_index (NULL )- 1 ; 497 }else if (strcmp (arg ,"#" )== 0 ) { 498nr = valid_tabpage (lastused_tabpage ) ?tabpage_index (lastused_tabpage ) :0 ; 499 }else { 500semsg (_ (e_invexpr2 ),arg ); 501 } 502 } 503 }else { 504nr = tabpage_index (curtab ); 505 } 506rettv -> vval .v_number = nr ; 507} 508 509/// "tabpagewinnr()" function 510void f_tabpagewinnr (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 511{ 512int nr = 1 ; 513tabpage_T * const tp = find_tabpage ((int )tv_get_number (& argvars [0 ])); 514if (tp == NULL ) { 515nr = 0 ; 516 }else { 517nr = get_winnr (tp ,& argvars [1 ]); 518 } 519rettv -> vval .v_number = nr ; 520} 521 522/// "win_execute(win_id, command)" function 523void f_win_execute (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 524{ 525// Return an empty string if something fails. 526rettv -> v_type = VAR_STRING ; 527rettv -> vval .v_string = NULL ; 528 529int id = (int )tv_get_number (argvars ); 530tabpage_T * tp ; 531win_T * wp = win_id2wp_tp (id ,& tp ); 532if (wp == NULL || tp == NULL ) { 533return ; 534 } 535 536CtxSwitch cs = {0 }; 537if (ctx_switch (& cs ,wp ,tp ,NULL ,kCtxNoDisplay |kCtxKeepCwd |kCtxValidate )) { 538execute_common (argvars ,rettv ,1 ); 539 } 540ctx_restore (& cs ); 541} 542 543/// "win_findbuf()" function 544void f_win_findbuf (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 545{ 546tv_list_alloc_ret (rettv ,kListLenMayKnow ); 547win_findbuf (argvars ,rettv -> vval .v_list ); 548} 549 550/// "win_getid()" function 551void f_win_getid (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 552{ 553rettv -> vval .v_number = win_getid (argvars ); 554} 555 556/// "win_gotoid()" function 557void f_win_gotoid (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 558{ 559int id = (int )tv_get_number (& argvars [0 ]); 560if (curwin -> handle == id ) { 561// Nothing to do. 562rettv -> vval .v_number = 1 ; 563return ; 564 } 565 566if (text_or_buf_locked ()) { 567return ; 568 } 569FOR_ALL_TAB_WINDOWS (tp ,wp ) { 570if (wp -> handle == id ) { 571// When jumping to another buffer stop Visual mode. 572if (Visual .active && wp -> w_buffer != curbuf ) { 573end_visual_mode (); 574 } 575goto_tabpage_win (tp ,wp ); 576rettv -> vval .v_number = 1 ; 577return ; 578 } 579 } 580} 581 582/// "win_id2tabwin()" function 583void f_win_id2tabwin (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 584{ 585win_id2tabwin (argvars ,rettv ); 586} 587 588/// "win_id2win()" function 589void f_win_id2win (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 590{ 591rettv -> vval .v_number = win_id2win (argvars ); 592} 593 594/// "win_move_separator()" function 595void f_win_move_separator (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 596{ 597rettv -> vval .v_number = false; 598 599win_T * wp = find_win_by_nr_or_id (& argvars [0 ]); 600if (wp == NULL || wp -> w_floating ) { 601return ; 602 } 603if (!win_valid (wp )) { 604emsg (_ (e_cannot_resize_window_in_another_tab_page )); 605return ; 606 } 607 608int offset = (int )tv_get_number (& argvars [1 ]); 609win_drag_vsep_line (wp ,offset ); 610rettv -> vval .v_number = true; 611} 612 613/// "win_move_statusline()" function 614void f_win_move_statusline (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 615{ 616win_T * wp ; 617int offset ; 618 619rettv -> vval .v_number = false; 620 621wp = find_win_by_nr_or_id (& argvars [0 ]); 622if (wp == NULL || wp -> w_floating ) { 623return ; 624 } 625if (!win_valid (wp )) { 626emsg (_ (e_cannot_resize_window_in_another_tab_page )); 627return ; 628 } 629 630offset = (int )tv_get_number (& argvars [1 ]); 631win_drag_status_line (wp ,offset ); 632rettv -> vval .v_number = true; 633} 634 635/// "win_screenpos()" function 636void f_win_screenpos (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 637{ 638tv_list_alloc_ret (rettv ,2 ); 639const win_T * const wp = find_win_by_nr_or_id (& argvars [0 ]); 640tv_list_append_number (rettv -> vval .v_list ,wp == NULL ?0 :wp -> w_winrow + 1 ); 641tv_list_append_number (rettv -> vval .v_list ,wp == NULL ?0 :wp -> w_wincol + 1 ); 642} 643 644/// "win_splitmove()" function 645void f_win_splitmove (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 646{ 647win_T * wp = find_win_by_nr_or_id (& argvars [0 ]); 648win_T * targetwin = find_win_by_nr_or_id (& argvars [1 ]); 649win_T * oldwin = curwin ; 650 651rettv -> vval .v_number = -1 ; 652 653if (wp == NULL || targetwin == NULL || wp == targetwin 654|| !win_valid (wp )|| !win_valid (targetwin ) 655|| targetwin -> w_floating ) { 656emsg (_ (e_invalwindow )); 657return ; 658 } 659 660int flags = 0 ; 661int size = 0 ; 662 663if (argvars [2 ].v_type != VAR_UNKNOWN ) { 664dict_T * d ; 665dictitem_T * di ; 666 667if (tv_check_for_nonnull_dict_arg (argvars ,2 )== FAIL ) { 668return ; 669 } 670 671d = argvars [2 ].vval .v_dict ; 672if (tv_dict_get_bool (d ,"vertical" , false)) { 673flags |=WSP_VERT ; 674 } 675if ((di = tv_dict_find (d ,"rightbelow" ,-1 ))!= NULL ) { 676flags |=tv_get_bool (& di -> di_tv ) ?WSP_BELOW :WSP_ABOVE ; 677 } 678size = (int )tv_dict_get_number (d ,"size" ); 679 } 680 681// Check if we're allowed to continue before we bother switching windows. 682if (is_ctx_win (wp )|| text_or_buf_locked ()|| check_split_disallowed (wp )== FAIL ) { 683return ; 684 } 685 686if (curwin != targetwin ) { 687win_goto (targetwin ); 688 } 689 690// Autocommands may have sent us elsewhere or closed "wp" or "oldwin". 691if (curwin == targetwin && win_valid (wp )) { 692if (win_splitmove (wp ,size ,flags )== OK ) { 693rettv -> vval .v_number = 0 ; 694 } 695 }else { 696emsg (_ (e_auabort )); 697 } 698 699if (oldwin != curwin && win_valid (oldwin )) { 700win_goto (oldwin ); 701 } 702} 703 704/// "win_gettype(nr)" function 705void f_win_gettype (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 706{ 707win_T * wp = curwin ; 708 709rettv -> v_type = VAR_STRING ; 710rettv -> vval .v_string = NULL ; 711if (argvars [0 ].v_type != VAR_UNKNOWN ) { 712wp = find_win_by_nr_or_id (& argvars [0 ]); 713if (wp == NULL ) { 714rettv -> vval .v_string = xstrdup ("unknown" ); 715return ; 716 } 717 } 718if (is_ctx_win (wp )) { 719rettv -> vval .v_string = xstrdup ("autocmd" ); 720 }else if (wp -> w_p_pvw ) { 721rettv -> vval .v_string = xstrdup ("preview" ); 722 }else if (wp -> w_floating ) { 723rettv -> vval .v_string = xstrdup ("popup" ); 724 }else if (bt_cmdwin (wp -> w_buffer )) { 725rettv -> vval .v_string = xstrdup ("command" ); 726 }else if (bt_quickfix (wp -> w_buffer )) { 727rettv -> vval .v_string = xstrdup ((wp -> w_llist_ref != NULL ?"loclist" :"quickfix" )); 728 } 729} 730 731/// "getcmdwintype()" function 732void f_getcmdwintype (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 733{ 734rettv -> v_type = VAR_STRING ; 735rettv -> vval .v_string = NULL ; 736rettv -> vval .v_string = xmallocz (1 ); 737rettv -> vval .v_string [0 ]= (char )cmdwin_type ; 738} 739 740/// "winbufnr(nr)" function 741void f_winbufnr (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 742{ 743win_T * wp = find_win_by_nr_or_id (& argvars [0 ]); 744if (wp == NULL ) { 745rettv -> vval .v_number = -1 ; 746 }else { 747rettv -> vval .v_number = wp -> w_buffer -> b_fnum ; 748 } 749} 750 751/// "wincol()" function 752void f_wincol (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 753{ 754validate_cursor (curwin ); 755rettv -> vval .v_number = curwin -> w_wcol + 1 ; 756} 757 758/// "winheight(nr)" function 759void f_winheight (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 760{ 761win_T * wp = find_win_by_nr_or_id (& argvars [0 ]); 762if (wp == NULL ) { 763rettv -> vval .v_number = -1 ; 764 }else { 765rettv -> vval .v_number = wp -> w_view_height ; 766 } 767} 768 769/// "winlayout()" function 770void f_winlayout (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 771{ 772tabpage_T * tp ; 773 774tv_list_alloc_ret (rettv ,2 ); 775 776if (argvars [0 ].v_type == VAR_UNKNOWN ) { 777tp = curtab ; 778 }else { 779tp = find_tabpage ((int )tv_get_number (& argvars [0 ])); 780if (tp == NULL ) { 781return ; 782 } 783 } 784 785get_framelayout (tp -> tp_topframe ,rettv -> vval .v_list , true); 786} 787 788/// "winline()" function 789void f_winline (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 790{ 791validate_cursor (curwin ); 792rettv -> vval .v_number = curwin -> w_wrow + 1 ; 793} 794 795/// "winnr()" function 796void f_winnr (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 797{ 798rettv -> vval .v_number = get_winnr (curtab ,& argvars [0 ]); 799} 800 801/// "winrestcmd()" function 802void f_winrestcmd (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 803{ 804char buf [50 ]; 805 806garray_T ga ; 807ga_init (& ga , (int )sizeof (char ),70 ); 808 809// Do this twice to handle some window layouts properly. 810for (int i = 0 ;i < 2 ;i ++ ) { 811int winnr = 1 ; 812FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 813if (!win_has_winnr (wp ,curtab )) { 814continue ; 815 } 816size_t buflen = vim_snprintf_safelen (buf ,sizeof (buf ), 817":%dresize %d|" ,winnr ,wp -> w_height ); 818ga_concat_len (& ga ,buf ,buflen ); 819buflen = vim_snprintf_safelen (buf ,sizeof (buf ), 820"vert :%dresize %d|" ,winnr ,wp -> w_width ); 821ga_concat_len (& ga ,buf ,buflen ); 822winnr ++ ; 823 } 824 } 825ga_append (& ga ,NUL ); 826 827rettv -> vval .v_string = ga .ga_data ; 828rettv -> v_type = VAR_STRING ; 829} 830 831/// "winrestview()" function 832void f_winrestview (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 833{ 834if (tv_check_for_nonnull_dict_arg (argvars ,0 )== FAIL ) { 835return ; 836 } 837 838dict_T * dict = argvars [0 ].vval .v_dict ; 839dictitem_T * di ; 840if ((di = tv_dict_find (dict ,S_LEN ("lnum" )))!= NULL ) { 841curwin -> w_cursor .lnum = (linenr_T )tv_get_number (& di -> di_tv ); 842 } 843if ((di = tv_dict_find (dict ,S_LEN ("col" )))!= NULL ) { 844curwin -> w_cursor .col = (colnr_T )tv_get_number (& di -> di_tv ); 845 } 846if ((di = tv_dict_find (dict ,S_LEN ("coladd" )))!= NULL ) { 847curwin -> w_cursor .coladd = (colnr_T )tv_get_number (& di -> di_tv ); 848 } 849if ((di = tv_dict_find (dict ,S_LEN ("topline" )))!= NULL ) { 850set_topline (curwin , (linenr_T )tv_get_number (& di -> di_tv ));// A folded line: its fold start. 851 } 852viewstate_T vs ; 853save_viewstate (curwin ,& vs );// Keys absent from the dict keep their value. 854if ((di = tv_dict_find (dict ,S_LEN ("curswant" )))!= NULL ) { 855vs .vs_curswant = (colnr_T )tv_get_number (& di -> di_tv ); 856vs .vs_set_curswant = false; 857 } 858if ((di = tv_dict_find (dict ,S_LEN ("topfill" )))!= NULL ) { 859vs .vs_topfill = (int )tv_get_number (& di -> di_tv ); 860 } 861if ((di = tv_dict_find (dict ,S_LEN ("leftcol" )))!= NULL ) { 862vs .vs_leftcol = (colnr_T )tv_get_number (& di -> di_tv ); 863 } 864if ((di = tv_dict_find (dict ,S_LEN ("skipcol" )))!= NULL ) { 865vs .vs_skipcol = (colnr_T )tv_get_number (& di -> di_tv ); 866 } 867restore_viewstate (curwin ,& vs ); 868 869// The dict may name positions beyond the buffer. 870check_cursor (curwin ); 871changed_window_setting (curwin ); 872if (curwin -> w_topline <=0 ) { 873curwin -> w_topline = 1 ; 874 } 875if (curwin -> w_topline > curbuf -> b_ml .ml_line_count ) { 876curwin -> w_topline = curbuf -> b_ml .ml_line_count ; 877 } 878check_topfill (curwin , true); 879} 880 881/// "winsaveview()" function 882void f_winsaveview (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 883{ 884tv_dict_alloc_ret (rettv ); 885dict_T * dict = rettv -> vval .v_dict ; 886 887tv_dict_add_nr (dict ,S_LEN ("lnum" ), (varnumber_T )curwin -> w_cursor .lnum ); 888tv_dict_add_nr (dict ,S_LEN ("col" ), (varnumber_T )curwin -> w_cursor .col ); 889tv_dict_add_nr (dict ,S_LEN ("coladd" ), (varnumber_T )curwin -> w_cursor .coladd ); 890update_curswant (); 891viewstate_T vs ; 892save_viewstate (curwin ,& vs ); 893tv_dict_add_nr (dict ,S_LEN ("curswant" ), (varnumber_T )vs .vs_curswant ); 894 895tv_dict_add_nr (dict ,S_LEN ("topline" ), (varnumber_T )vs .vs_topline ); 896tv_dict_add_nr (dict ,S_LEN ("topfill" ), (varnumber_T )vs .vs_topfill ); 897tv_dict_add_nr (dict ,S_LEN ("leftcol" ), (varnumber_T )vs .vs_leftcol ); 898tv_dict_add_nr (dict ,S_LEN ("skipcol" ), (varnumber_T )vs .vs_skipcol ); 899} 900 901/// "winwidth(nr)" function 902void f_winwidth (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 903{ 904win_T * wp = find_win_by_nr_or_id (& argvars [0 ]); 905if (wp == NULL ) { 906rettv -> vval .v_number = -1 ; 907 }else { 908rettv -> vval .v_number = wp -> w_view_width ; 909 } 910}