robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#include <stdbool.h> 2#include <stdint.h> 3#include <stdlib.h> 4 5#include "lua.h" 6#include "nvim/api/keysets_defs.h" 7#include "nvim/api/private/defs.h" 8#include "nvim/api/private/dispatch.h" 9#include "nvim/api/private/helpers.h" 10#include "nvim/api/private/validate.h" 11#include "nvim/api/window.h" 12#include "nvim/autocmd.h" 13#include "nvim/buffer_defs.h" 14#include "nvim/context.h" 15#include "nvim/cursor.h" 16#include "nvim/drawscreen.h" 17#include "nvim/errors.h" 18#include "nvim/eval/window.h" 19#include "nvim/ex_docmd.h" 20#include "nvim/gettext_defs.h" 21#include "nvim/globals.h" 22#include "nvim/lua/executor.h" 23#include "nvim/memory_defs.h" 24#include "nvim/message.h" 25#include "nvim/move.h" 26#include "nvim/plines.h" 27#include "nvim/pos_defs.h" 28#include "nvim/types_defs.h" 29#include "nvim/window.h" 30 31#include "api/window.c.generated.h" // IWYU pragma: keep 32 33/// Gets the current buffer in a window 34/// 35/// @param win |window-ID|, or 0 for current window 36/// @param[out] err Error details, if any 37/// @return Buffer id 38Buffer nvim_win_get_buf (Window win ,Error * err ) 39FUNC_API_SINCE (1 ) 40{ 41win_T * w = find_window_by_handle (win ,err ); 42 43if (!w ) { 44return 0 ; 45 } 46 47return w -> w_buffer -> handle ; 48} 49 50/// Sets the current buffer in a window. 51/// 52/// Note: As a side-effect, this executes |BufEnter| and |BufLeave| autocommands. 53/// @param win |window-ID|, or 0 for current window 54/// @param buf Buffer id 55/// @param[out] err Error details, if any 56void nvim_win_set_buf (Window win ,Buffer buf ,Error * err ) 57FUNC_API_SINCE (5 ) 58FUNC_API_TEXTLOCK 59{ 60win_T * w = find_window_by_handle (win ,err ); 61if (!w ) { 62return ; 63 } 64buf_T * b = find_buffer_by_handle (buf ,err ); 65if (!b ) { 66return ; 67 } 68 69// 'winfixbuf' on the cmdwin window prevents replacing its buffer; no 70// separate cmdwin guard needed. 71win_set_buf (w ,b ,err ); 72} 73 74/// Gets the (1,0)-indexed, buffer-relative cursor position for a given window 75/// (different windows showing the same buffer have independent cursor 76/// positions). |api-indexing| 77/// 78/// @see |getcurpos()| 79/// 80/// @param win |window-ID|, or 0 for current window 81/// @param[out] err Error details, if any 82/// @return (row, col) tuple 83ArrayOf (Integer ,2 )nvim_win_get_cursor (Window win ,Arena * arena ,Error * err ) 84FUNC_API_SINCE (1 ) 85{ 86Array rv = ARRAY_DICT_INIT ; 87win_T * w = find_window_by_handle (win ,err ); 88 89if (w ) { 90rv = arena_array (arena ,2 ); 91ADD_C (rv ,INTEGER_OBJ (w -> w_cursor .lnum )); 92ADD_C (rv ,INTEGER_OBJ (w -> w_cursor .col )); 93 } 94 95return rv ; 96} 97 98/// Sets the (1,0)-indexed cursor position (byte offset) in the window. |api-indexing| 99/// This scrolls the window even if it is not the current one. 100/// 101/// @param win |window-ID|, or 0 for current window 102/// @param pos (row, col) tuple representing the new position 103/// @param[out] err Error details, if any 104void nvim_win_set_cursor (Window win ,ArrayOf (Integer ,2 )pos ,Error * err ) 105FUNC_API_SINCE (1 ) 106{ 107win_T * w = find_window_by_handle (win ,err ); 108 109if (!w ) { 110return ; 111 } 112 113VALIDATE_EXP (!(pos .size != 2 || pos .items [0 ].type != kObjectTypeInteger 114|| pos .items [1 ].type != kObjectTypeInteger ),"pos" ,"[row, col] array" ,NULL , { 115return ; 116 }); 117 118int64_t row = pos .items [0 ].data .integer ; 119int64_t col = pos .items [1 ].data .integer ; 120 121VALIDATE_RANGE (!(row <=0 || row > w -> w_buffer -> b_ml .ml_line_count ),"cursor line" , { 122return ; 123 }); 124 125VALIDATE_RANGE (!(col > MAXCOL || col < 0 ),"cursor column" , { 126return ; 127 }); 128 129w -> w_cursor .lnum = (linenr_T )row ; 130w -> w_cursor .col = (colnr_T )col ; 131w -> w_cursor .coladd = 0 ; 132// When column is out of range silently correct it. 133check_cursor_col (w ); 134 135// Make sure we stick in this column. 136w -> w_set_curswant = true; 137 138// make sure cursor is in visible range and 139// cursorcolumn and cursorline are updated even if w != curwin 140CtxSwitch switchwin ; 141if (ctx_switch (& switchwin ,w ,NULL ,NULL ,kCtxNoEvents |kCtxNoDisplay )) { 142update_topline (curwin ); 143validate_cursor (curwin ); 144 } 145ctx_restore (& switchwin ); 146 147redraw_later (w ,UPD_VALID ); 148w -> w_redr_status = true; 149} 150 151/// Gets the window height 152/// 153/// @param win |window-ID|, or 0 for current window 154/// @param[out] err Error details, if any 155/// @return Height as a count of rows 156Integer nvim_win_get_height (Window win ,Error * err ) 157FUNC_API_SINCE (1 ) 158{ 159win_T * w = find_window_by_handle (win ,err ); 160 161if (!w ) { 162return 0 ; 163 } 164 165return w -> w_height ; 166} 167 168/// Gets the window width 169/// 170/// @param win |window-ID|, or 0 for current window 171/// @param[out] err Error details, if any 172/// @return Width as a count of columns 173Integer nvim_win_get_width (Window win ,Error * err ) 174FUNC_API_SINCE (1 ) 175{ 176win_T * w = find_window_by_handle (win ,err ); 177 178if (!w ) { 179return 0 ; 180 } 181 182return w -> w_width ; 183} 184 185/// Gets a window-scoped (w:) variable 186/// 187/// @param win |window-ID|, or 0 for current window 188/// @param name Variable name 189/// @param[out] err Error details, if any 190/// @return Variable value 191Object nvim_win_get_var (Window win ,String name ,Arena * arena ,Error * err ) 192FUNC_API_SINCE (1 ) 193{ 194win_T * w = find_window_by_handle (win ,err ); 195 196if (!w ) { 197return (Object )OBJECT_INIT ; 198 } 199 200return dict_get_value (w -> w_vars ,name ,arena ,err ); 201} 202 203/// Sets a window-scoped (w:) variable 204/// 205/// @param win |window-ID|, or 0 for current window 206/// @param name Variable name 207/// @param value Variable value 208/// @param[out] err Error details, if any 209void nvim_win_set_var (Window win ,String name ,Object value ,Error * err ) 210FUNC_API_SINCE (1 ) 211{ 212win_T * w = find_window_by_handle (win ,err ); 213 214if (!w ) { 215return ; 216 } 217 218dict_set_var (w -> w_vars ,name ,value , false, false,NULL ,err ); 219} 220 221/// Removes a window-scoped (w:) variable 222/// 223/// @param win |window-ID|, or 0 for current window 224/// @param name Variable name 225/// @param[out] err Error details, if any 226void nvim_win_del_var (Window win ,String name ,Error * err ) 227FUNC_API_SINCE (1 ) 228{ 229win_T * w = find_window_by_handle (win ,err ); 230 231if (!w ) { 232return ; 233 } 234 235dict_set_var (w -> w_vars ,name ,NIL , true, false,NULL ,err ); 236} 237 238/// Gets the window position in display cells. First position is zero. 239/// 240/// @param win |window-ID|, or 0 for current window 241/// @param[out] err Error details, if any 242/// @return (row, col) tuple with the window position 243ArrayOf (Integer ,2 )nvim_win_get_position (Window win ,Arena * arena ,Error * err ) 244FUNC_API_SINCE (1 ) 245{ 246Array rv = ARRAY_DICT_INIT ; 247win_T * w = find_window_by_handle (win ,err ); 248 249if (w ) { 250rv = arena_array (arena ,2 ); 251ADD_C (rv ,INTEGER_OBJ (w -> w_winrow )); 252ADD_C (rv ,INTEGER_OBJ (w -> w_wincol )); 253 } 254 255return rv ; 256} 257 258/// Gets the window tabpage 259/// 260/// @param win |window-ID|, or 0 for current window 261/// @param[out] err Error details, if any 262/// @return Tabpage that contains the window 263Tabpage nvim_win_get_tabpage (Window win ,Error * err ) 264FUNC_API_SINCE (1 ) 265{ 266Tabpage rv = 0 ; 267win_T * w = find_window_by_handle (win ,err ); 268 269if (w ) { 270rv = win_find_tabpage (w )-> handle ; 271 } 272 273return rv ; 274} 275 276/// Gets the window number 277/// 278/// @param win |window-ID|, or 0 for current window 279/// @param[out] err Error details, if any 280/// @return Window number 281Integer nvim_win_get_number (Window win ,Error * err ) 282FUNC_API_SINCE (1 ) 283{ 284int rv = 0 ; 285win_T * w = find_window_by_handle (win ,err ); 286 287if (!w ) { 288return rv ; 289 } 290 291int tabnr ; 292win_get_tabwin (w -> handle ,& tabnr ,& rv ); 293 294return rv ; 295} 296 297/// Checks if a window is valid 298/// 299/// @param win |window-ID|, or 0 for current window 300/// @return true if the window is valid, false otherwise 301Boolean nvim_win_is_valid (Window win ) 302FUNC_API_SINCE (1 ) 303{ 304Error stub = ERROR_INIT ; 305Boolean ret = find_window_by_handle (win ,& stub )!= NULL ; 306api_clear_error (& stub ); 307return ret ; 308} 309 310/// Closes the window and hides the buffer it contains (like |:hide| with a |window-ID|; unrelated 311/// to the `hide` flag of |nvim_open_win()|, |nvim_win_get_config()|). 312/// 313/// Compare |:close| and |nvim_win_close()|, which close the buffer instead of hiding it. 314/// 315/// @param win |window-ID|, or 0 for current window 316/// @param[out] err Error details, if any 317void nvim_win_hide (Window win ,Error * err ) 318FUNC_API_SINCE (7 ) 319FUNC_API_TEXTLOCK 320{ 321win_T * w = find_window_by_handle (win ,err ); 322if (!w ) { 323return ; 324 } 325 326tabpage_T * tabpage = win_find_tabpage (w ); 327TRY_WRAP (err , { 328// Never close the autocommand window. 329if (is_ctx_win (w )) { 330emsg (_ (e_autocmd_close )); 331 }else if (tabpage == curtab ) { 332win_close (w , false, false); 333 }else { 334win_close_othertab (w , false,tabpage , false); 335 } 336 }); 337} 338 339/// Closes the window (like |:close| with a |window-ID|). 340/// 341/// @param win |window-ID|, or 0 for current window 342/// @param force Behave like `:close!` The last window of a buffer with 343/// unwritten changes can be closed. The buffer will become 344/// hidden, even if 'hidden' is not set. 345/// @param[out] err Error details, if any 346void nvim_win_close (Window win ,Boolean force ,Error * err ) 347FUNC_API_SINCE (6 ) 348FUNC_API_TEXTLOCK 349{ 350win_T * w = find_window_by_handle (win ,err ); 351if (!w ) { 352return ; 353 } 354 355tabpage_T * tabpage = win_find_tabpage (w ); 356TRY_WRAP (err , { 357ex_win_close (force ,w ,tabpage == curtab ?NULL :tabpage ); 358 }); 359} 360 361/// Calls function `fn` in the context of window `win` and returns its result (may be multiple 362/// values). 363/// 364/// @see |win_execute()| 365/// @see |nvim_buf_call()| 366/// 367/// @param win |window-ID|, or 0 for current window. 368/// @param fn Lua function to call inside the window. 369/// @param err Error details, if any. 370/// @return Value(s) returned by `fn()`. 371Object nvim_win_call (Window win ,LuaRef fn ,lua_State * lstate ,Error * err ) 372FUNC_API_SINCE (7 ) 373FUNC_API_LUA_ONLY 374{ 375win_T * w = find_window_by_handle (win ,err ); 376if (!w ) { 377return NIL ; 378 } 379tabpage_T * tabpage = win_find_tabpage (w ); 380 381TRY_WRAP (err , { 382CtxSwitch cs = {0 }; 383if (ctx_switch (& cs ,w ,tabpage ,NULL ,kCtxNoDisplay |kCtxKeepCwd |kCtxValidate )) { 384Array args = ARRAY_DICT_INIT ; 385nlua_call_ref (fn ,NULL ,args ,kRetMultiStack ,NULL ,err ); 386 } 387ctx_restore (& cs ); 388 }); 389return NIL ;// kRetMultiStack: values are already on the lua stack 390} 391 392/// Set highlight namespace for a window. This will use highlights defined with 393/// |nvim_set_hl()| for this namespace, but fall back to global highlights (ns=0) when 394/// missing. 395/// 396/// This takes precedence over the 'winhighlight' option. 397/// 398/// @param win 399/// @param ns_id the namespace to use 400/// @param[out] err Error details, if any 401void nvim_win_set_hl_ns (Window win ,Integer ns_id ,Error * err ) 402FUNC_API_SINCE (10 ) 403{ 404win_T * w = find_window_by_handle (win ,err ); 405if (!w ) { 406return ; 407 } 408 409// -1 is allowed as inherit global namespace 410VALIDATE_S ((ns_id >=-1 ),"namespace" ,"" , { 411return ; 412 }); 413 414w -> w_ns_hl = (NS )ns_id ; 415w -> w_hl_needs_update = true; 416redraw_later (w ,UPD_NOT_VALID ); 417} 418 419/// Computes the number of screen lines occupied by a range of text in a given window. 420/// Works for off-screen text and takes folds into account. 421/// 422/// Diff filler or virtual lines above a line are counted as a part of that line, 423/// unless the line is on "start_row" and "start_vcol" is specified. 424/// 425/// Diff filler or virtual lines below the last buffer line are counted in the result 426/// when "end_row" is omitted. 427/// 428/// Line indexing is similar to |nvim_buf_get_text()|. 429/// 430/// @param win |window-ID|, or 0 for current window. 431/// @param opts Optional parameters: 432/// - end_row: Ending line index, 0-based inclusive. 433/// When omitted end at the very bottom. 434/// - end_vcol: Ending virtual column index on "end_row", 435/// 0-based exclusive, rounded up to full screen lines. 436/// When 0 only include diff filler and virtual lines above 437/// "end_row". When omitted include the whole line. 438/// - max_height: Don't add the height of lines below the row 439/// for which this height is reached. Useful to e.g. limit the 440/// height to the window height, avoiding unnecessary work. Or 441/// to find out how many buffer lines beyond "start_row" take 442/// up a certain number of logical lines (returned in 443/// "end_row" and "end_vcol"). 444/// - start_row: Starting line index, 0-based inclusive. 445/// When omitted start at the very top. 446/// - start_vcol: Starting virtual column index on "start_row", 447/// 0-based inclusive, rounded down to full screen lines. 448/// When omitted include the whole line. 449/// @return Dict containing text height information, with these keys: 450/// - all: The total number of screen lines occupied by the range. 451/// - fill: The number of diff filler or virtual lines among them. 452/// - end_row: The row on which the returned height is reached (first row of 453/// a closed fold). 454/// - end_vcol: Ending virtual column in "end_row" where "max_height" or the returned 455/// height is reached. 0 if "end_row" is a closed fold. 456/// 457/// @see |virtcol()| for text width. 458DictAs (win_text_height_ret )nvim_win_text_height (Window win ,Dict (win_text_height )* opts , 459Arena * arena ,Error * err ) 460FUNC_API_SINCE (12 ) 461{ 462Dict rv = arena_dict (arena ,2 ); 463 464win_T * const w = find_window_by_handle (win ,err ); 465if (!w ) { 466return rv ; 467 } 468buf_T * const buf = w -> w_buffer ; 469const linenr_T line_count = buf -> b_ml .ml_line_count ; 470 471linenr_T start_lnum = 1 ; 472linenr_T end_lnum = line_count ; 473int64_t start_vcol = -1 ; 474int64_t end_vcol = -1 ; 475 476bool oob = false; 477 478if (HAS_KEY (opts ,win_text_height ,start_row )) { 479start_lnum = (linenr_T )normalize_index (buf ,opts -> start_row , false,& oob ); 480 } 481 482if (HAS_KEY (opts ,win_text_height ,end_row )) { 483end_lnum = (linenr_T )normalize_index (buf ,opts -> end_row , false,& oob ); 484 } 485 486VALIDATE (!oob ,"%s" ,"Line index out of bounds" , { 487return rv ; 488 }); 489VALIDATE ((start_lnum <=end_lnum ),"%s" ,"'start_row' is higher than 'end_row'" , { 490return rv ; 491 }); 492 493if (HAS_KEY (opts ,win_text_height ,start_vcol )) { 494VALIDATE (HAS_KEY (opts ,win_text_height ,start_row ), 495"%s" ,"'start_vcol' specified without 'start_row'" , { 496return rv ; 497 }); 498start_vcol = opts -> start_vcol ; 499VALIDATE_RANGE ((start_vcol >=0 && start_vcol <=MAXCOL ),"start_vcol" , { 500return rv ; 501 }); 502 } 503 504if (HAS_KEY (opts ,win_text_height ,end_vcol )) { 505VALIDATE (HAS_KEY (opts ,win_text_height ,end_row ), 506"%s" ,"'end_vcol' specified without 'end_row'" , { 507return rv ; 508 }); 509end_vcol = opts -> end_vcol ; 510VALIDATE_RANGE ((end_vcol >=0 && end_vcol <=MAXCOL ),"end_vcol" , { 511return rv ; 512 }); 513 } 514 515int64_t max = INT64_MAX ; 516if (HAS_KEY (opts ,win_text_height ,max_height )) { 517VALIDATE_RANGE (opts -> max_height > 0 ,"max_height" , { 518return rv ; 519 }); 520max = opts -> max_height ; 521 } 522 523if (start_lnum == end_lnum && start_vcol >=0 && end_vcol >=0 ) { 524VALIDATE ((start_vcol <=end_vcol ),"%s" ,"'start_vcol' is higher than 'end_vcol'" , { 525return rv ; 526 }); 527 } 528 529int64_t fill = 0 ; 530int64_t all = win_text_height (w ,start_lnum ,start_vcol ,& end_lnum ,& end_vcol ,& fill ,max ); 531if (!HAS_KEY (opts ,win_text_height ,end_row )) { 532const int64_t end_fill = win_get_fill (w ,line_count + 1 ); 533fill += end_fill ; 534all += end_fill ; 535 } 536PUT_C (rv ,"all" ,INTEGER_OBJ (all )); 537PUT_C (rv ,"fill" ,INTEGER_OBJ (fill )); 538PUT_C (rv ,"end_row" ,INTEGER_OBJ (end_lnum - 1 )); 539PUT_C (rv ,"end_vcol" ,INTEGER_OBJ (end_vcol )); 540return rv ; 541} 542 543/// Resizes a window, anchored to a given edge. 544/// 545/// The window first takes space from the non-anchored side, and only then from the anchored side. 546/// The "anchor" decides the fixed edge, so a window can grow or shrink in any direction. 547/// 548/// Can set height, width (only for a vertical split), or both. The "anchor" applies to the matching 549/// axis (top/bottom for height, left/right for width); the other axis uses its default anchor. 550/// 551/// @param win |window-ID|, or 0 for current window 552/// @param width New width (columns), or -1 for "no change". 553/// @param height New height (rows), or -1 for "no change". 554/// @param opts Optional parameters. 555/// - anchor: Edge that stays fixed while the opposite edge moves; the 556/// neighbor on the moving side is resized first. One of: 557/// - "top" (default) or "bottom" 558/// - "left" (default) or "right" 559/// @param[out] err Error details, if any 560void nvim_win_resize (Window win ,Integer width ,Integer height ,Dict (win_resize )* opts ,Error * err ) 561FUNC_API_SINCE (15 ) 562{ 563win_T * w = find_window_by_handle (win ,err ); 564 565if (!w ) { 566return ; 567 } 568 569VALIDATE_EXP ((height >=0 || height == -1 ),"height" ,"non-negative number or -1" ,NULL , { 570return ; 571 }); 572VALIDATE_EXP ((width >=0 || width == -1 ),"width" ,"non-negative number or -1" ,NULL , { 573return ; 574 }); 575 576VALIDATE_R ((height >=0 || width >=0 ),"'height' or 'width'" , { 577return ; 578 }); 579 580// default anchors 581bool from_top = true; 582bool from_left = true; 583 584if (HAS_KEY (opts ,win_resize ,anchor )) { 585char * anchor = opts -> anchor .data ; 586bool is_height = strequal ("top" ,anchor )|| strequal ("bottom" ,anchor ); 587bool is_width = strequal ("left" ,anchor )|| strequal ("right" ,anchor ); 588 589VALIDATE_EXP (is_height || is_width ,"anchor" ,"\"top\", \"bottom\", \"left\" or \"right\"" , 590anchor , {return ; }); 591// anchor must match a dimension that is actually being resized. 592VALIDATE_CON (!(is_height && height == -1 )&& !(is_width && width == -1 ),anchor , 593is_width ?"width" :"height" , { 594return ; 595 }); 596 597from_top = !strequal ("bottom" ,anchor ); 598from_left = !strequal ("right" ,anchor ); 599 } 600if (height >=0 ) { 601TRY_WRAP (err ,win_setheight_win ((int )height ,w ,from_top )); 602 } 603if (!ERROR_SET (err )&& width >=0 ) { 604TRY_WRAP (err ,win_setwidth_win ((int )width ,w ,from_left )); 605 } 606}