robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// Island of misfit toys. 2 3#include <stdbool.h> 4#include <stdint.h> 5#include <string.h> 6 7#include "nvim/api/buffer.h" 8#include "nvim/api/deprecated.h" 9#include "nvim/api/extmark.h" 10#include "nvim/api/keysets_defs.h" 11#include "nvim/api/private/defs.h" 12#include "nvim/api/private/dispatch.h" 13#include "nvim/api/private/helpers.h" 14#include "nvim/api/private/validate.h" 15#include "nvim/api/vimscript.h" 16#include "nvim/buffer_defs.h" 17#include "nvim/decoration.h" 18#include "nvim/decoration_defs.h" 19#include "nvim/eval/vars.h" 20#include "nvim/extmark.h" 21#include "nvim/globals.h" 22#include "nvim/highlight.h" 23#include "nvim/highlight_group.h" 24#include "nvim/lua/executor.h" 25#include "nvim/marktree.h" 26#include "nvim/memory.h" 27#include "nvim/memory_defs.h" 28#include "nvim/message.h" 29#include "nvim/option.h" 30#include "nvim/option_defs.h" 31#include "nvim/pos_defs.h" 32#include "nvim/strings.h" 33#include "nvim/types_defs.h" 34#include "nvim/window.h" 35 36#include "api/deprecated.c.generated.h" 37 38/// @deprecated Use nvim_exec2() instead. 39/// @see nvim_exec2 40String nvim_exec (uint64_t channel_id ,String src ,Boolean output ,Error * err ) 41FUNC_API_SINCE (7 )FUNC_API_DEPRECATED_SINCE (11 ) 42FUNC_API_RET_ALLOC 43{ 44Dict (exec_opts )opts = { .output = output }; 45return exec_impl (channel_id ,src ,& opts ,err ); 46} 47 48/// @deprecated 49/// @see nvim_exec2 50String nvim_command_output (uint64_t channel_id ,String command ,Error * err ) 51FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (7 ) 52FUNC_API_RET_ALLOC 53{ 54Dict (exec_opts )opts = { .output = true }; 55return exec_impl (channel_id ,command ,& opts ,err ); 56} 57 58/// @deprecated Use nvim_exec_lua() instead. 59/// @see nvim_exec_lua 60Object nvim_execute_lua (String code ,Array args ,Arena * arena ,Error * err ) 61FUNC_API_SINCE (3 ) 62FUNC_API_DEPRECATED_SINCE (7 ) 63FUNC_API_REMOTE_ONLY 64{ 65return nlua_exec (code ,NULL ,args ,kRetObject ,arena ,err ); 66} 67 68/// Gets the buffer number 69/// 70/// @deprecated The buffer number now is equal to the object id 71/// 72/// @param buffer Buffer id, or 0 for current buffer 73/// @param[out] err Error details, if any 74/// @return Buffer number 75Integer nvim_buf_get_number (Buffer buffer ,Error * err ) 76FUNC_API_SINCE (1 ) 77FUNC_API_DEPRECATED_SINCE (2 ) 78{ 79buf_T * buf = find_buffer_by_handle (buffer ,err ); 80 81if (!buf ) { 82return 0 ; 83 } 84 85return buf -> b_fnum ; 86} 87 88static uint32_t src2ns (Integer * src_id ) 89{ 90if (* src_id == 0 ) { 91* src_id = nvim_create_namespace ((String )STRING_INIT ); 92 } 93if (* src_id < 0 ) { 94return (((uint32_t )1 ) <<31 )- 1 ; 95 } 96return (uint32_t )(* src_id ); 97} 98 99/// Clears highlights and virtual text from namespace and range of lines 100/// 101/// @deprecated use |nvim_buf_clear_namespace()|. 102/// 103/// @param buffer Buffer id, or 0 for current buffer 104/// @param ns_id Namespace to clear, or -1 to clear all. 105/// @param line_start Start of range of lines to clear 106/// @param line_end End of range of lines to clear (exclusive) or -1 to clear 107/// to end of file. 108/// @param[out] err Error details, if any 109void nvim_buf_clear_highlight (Buffer buffer ,Integer ns_id ,Integer line_start ,Integer line_end , 110Error * err ) 111FUNC_API_SINCE (1 ) 112FUNC_API_DEPRECATED_SINCE (7 ) 113{ 114nvim_buf_clear_namespace (buffer ,ns_id ,line_start ,line_end ,err ); 115} 116 117/// Adds a highlight to buffer. 118/// 119/// @deprecated use |nvim_buf_set_extmark()| or |vim.hl.range()| 120/// 121/// Namespaces are used for batch deletion/updating of a set of highlights. To 122/// create a namespace, use |nvim_create_namespace()| which returns a namespace 123/// id. Pass it in to this function as `ns_id` to add highlights to the 124/// namespace. All highlights in the same namespace can then be cleared with 125/// single call to |nvim_buf_clear_namespace()|. If the highlight never will be 126/// deleted by an API call, pass `ns_id = -1`. 127/// 128/// As a shorthand, `ns_id = 0` can be used to create a new namespace for the 129/// highlight, the allocated id is then returned. If `hl_group` is the empty 130/// string no highlight is added, but a new `ns_id` is still returned. This is 131/// supported for backwards compatibility, new code should use 132/// |nvim_create_namespace()| to create a new empty namespace. 133/// 134/// @param buffer Buffer id, or 0 for current buffer 135/// @param ns_id namespace to use or -1 for ungrouped highlight 136/// @param hl_group Name of the highlight group to use 137/// @param line Line to highlight (zero-indexed) 138/// @param col_start Start of (byte-indexed) column range to highlight 139/// @param col_end End of (byte-indexed) column range to highlight, 140/// or -1 to highlight to end of line 141/// @param[out] err Error details, if any 142/// @return The ns_id that was used 143Integer nvim_buf_add_highlight (Buffer buffer ,Integer ns_id ,String hl_group ,Integer line , 144Integer col_start ,Integer col_end ,Error * err ) 145FUNC_API_SINCE (1 ) 146FUNC_API_DEPRECATED_SINCE (13 ) 147{ 148buf_T * buf = find_buffer_by_handle (buffer ,err ); 149if (!buf ) { 150return 0 ; 151 } 152 153VALIDATE_RANGE ((line >=0 && line < MAXLNUM ),"line number" , { 154return 0 ; 155 }); 156VALIDATE_RANGE ((col_start >=0 && col_start <=MAXCOL ),"column" , { 157return 0 ; 158 }); 159 160if (col_end < 0 || col_end > MAXCOL ) { 161col_end = MAXCOL ; 162 } 163 164uint32_t ns = src2ns (& ns_id ); 165 166if (!(line < buf -> b_ml .ml_line_count )) { 167// safety check, we can't add marks outside the range 168return ns_id ; 169 } 170 171int hl_id = 0 ; 172if (hl_group .size > 0 ) { 173hl_id = syn_check_group (hl_group .data ,hl_group .size ); 174 }else { 175return ns_id ; 176 } 177 178int end_line = (int )line ; 179if (col_end == MAXCOL ) { 180col_end = 0 ; 181end_line ++ ; 182 } 183 184DecorInline decor = DECOR_INLINE_INIT ; 185decor .data .hl .hl_id = hl_id ; 186 187extmark_set (buf ,ns ,NULL , (int )line , (colnr_T )col_start ,end_line , (colnr_T )col_end , 188decor ,MT_FLAG_DECOR_HL , true, false, false, false,NULL ); 189return ns_id ; 190} 191/// Set the virtual text (annotation) for a buffer line. 192/// 193/// @deprecated use nvim_buf_set_extmark to use full virtual text functionality. 194/// 195/// The text will be placed after the buffer text. Virtual text will never 196/// cause reflow, rather virtual text will be truncated at the end of the screen 197/// line. The virtual text will begin one cell (|lcs-eol| or space) after the 198/// ordinary text. 199/// 200/// Namespaces are used to support batch deletion/updating of virtual text. 201/// To create a namespace, use |nvim_create_namespace()|. Virtual text is 202/// cleared using |nvim_buf_clear_namespace()|. The same `ns_id` can be used for 203/// both virtual text and highlights added by |nvim_buf_add_highlight()|, both 204/// can then be cleared with a single call to |nvim_buf_clear_namespace()|. If 205/// the virtual text never will be cleared by an API call, pass `ns_id = -1`. 206/// 207/// As a shorthand, `ns_id = 0` can be used to create a new namespace for the 208/// virtual text, the allocated id is then returned. 209/// 210/// @param buffer Buffer id, or 0 for current buffer 211/// @param src_id Namespace to use or 0 to create a namespace, 212/// or -1 for a ungrouped annotation 213/// @param line Line to annotate with virtual text (zero-indexed) 214/// @param chunks A list of [text, hl_group] arrays, each representing a 215/// text chunk with specified highlight. `hl_group` element 216/// can be omitted for no highlight. 217/// @param opts Optional parameters. Currently not used. 218/// @param[out] err Error details, if any 219/// @return The ns_id that was used 220Integer nvim_buf_set_virtual_text (Buffer buffer ,Integer src_id ,Integer line ,Array chunks , 221Dict (empty )* opts ,Error * err ) 222FUNC_API_SINCE (5 ) 223FUNC_API_DEPRECATED_SINCE (8 ) 224{ 225buf_T * buf = find_buffer_by_handle (buffer ,err ); 226if (!buf ) { 227return 0 ; 228 } 229 230if (line < 0 || line >=MAXLNUM ) { 231api_set_error (err ,kErrorTypeValidation ,"Line number outside range" ); 232return 0 ; 233 } 234 235uint32_t ns_id = src2ns (& src_id ); 236int width ; 237 238VirtText virt_text = parse_virt_text (chunks ,err ,& width , false); 239if (ERROR_SET (err )) { 240return 0 ; 241 } 242 243DecorVirtText * existing = decor_find_virttext (buf , (int )line ,ns_id ); 244 245if (existing ) { 246clear_virttext (& existing -> data .virt_text ); 247existing -> data .virt_text = virt_text ; 248existing -> width = width ; 249return src_id ; 250 } 251 252DecorVirtText * vt = xmalloc (sizeof * vt ); 253* vt = (DecorVirtText )DECOR_VIRT_TEXT_INIT ; 254vt -> data .virt_text = virt_text ; 255vt -> width = width ; 256vt -> priority = 0 ; 257 258DecorInline decor = { .ext = true, .data .ext .vt = vt , .data .ext .sh_idx = DECOR_ID_INVALID }; 259 260extmark_set (buf ,ns_id ,NULL , (int )line ,0 ,-1 ,-1 ,decor ,0 , true, 261 false, false, false,NULL ); 262return src_id ; 263} 264 265/// Gets a highlight definition by id. |hlID()| 266/// 267/// @deprecated use |nvim_get_hl()| instead 268/// 269/// @param hl_id Highlight id as returned by |hlID()| 270/// @param rgb Export RGB colors 271/// @param[out] err Error details, if any 272/// @return Highlight definition map 273/// @see nvim_get_hl_by_name 274Dict nvim_get_hl_by_id (Integer hl_id ,Boolean rgb ,Arena * arena ,Error * err ) 275FUNC_API_SINCE (3 ) 276FUNC_API_DEPRECATED_SINCE (9 ) 277{ 278Dict dic = ARRAY_DICT_INIT ; 279VALIDATE_INT ((syn_get_final_id ((int )hl_id )!= 0 ),"highlight id" ,hl_id , { 280return dic ; 281 }); 282int attrcode = syn_id2attr ((int )hl_id ); 283return hl_get_attr_by_id (attrcode ,rgb ,arena ,err ); 284} 285 286/// Gets a highlight definition by name. 287/// 288/// @deprecated use |nvim_get_hl()| instead 289/// 290/// @param name Highlight group name 291/// @param rgb Export RGB colors 292/// @param[out] err Error details, if any 293/// @return Highlight definition map 294/// @see nvim_get_hl_by_id 295Dict nvim_get_hl_by_name (String name ,Boolean rgb ,Arena * arena ,Error * err ) 296FUNC_API_SINCE (3 ) 297FUNC_API_DEPRECATED_SINCE (9 ) 298{ 299Dict result = ARRAY_DICT_INIT ; 300int id = syn_name2id (name .data ); 301 302VALIDATE_S ((id != 0 ),"highlight name" ,name .data , { 303return result ; 304 }); 305return nvim_get_hl_by_id (id ,rgb ,arena ,err ); 306} 307 308/// Inserts a sequence of lines to a buffer at a certain index 309/// 310/// @deprecated use nvim_buf_set_lines(buffer, lnum, lnum, true, lines) 311/// 312/// @param buffer Buffer id 313/// @param lnum Insert the lines after `lnum`. If negative, appends to 314/// the end of the buffer. 315/// @param lines Array of lines 316/// @param[out] err Error details, if any 317void buffer_insert (Buffer buffer ,Integer lnum ,ArrayOf (String )lines ,Arena * arena ,Error * err ) 318FUNC_API_DEPRECATED_SINCE (1 ) 319{ 320// "lnum" will be the index of the line after inserting, 321// no matter if it is negative or not 322nvim_buf_set_lines (0 ,buffer ,lnum ,lnum , true,lines ,arena ,err ); 323} 324 325/// Gets a buffer line 326/// 327/// @deprecated use nvim_buf_get_lines instead. 328/// for positive indices (including 0) use 329/// "nvim_buf_get_lines(buffer, index, index+1, true)" 330/// for negative indices use 331/// "nvim_buf_get_lines(buffer, index-1, index, true)" 332/// 333/// @param buffer Buffer id 334/// @param index Line index 335/// @param[out] err Error details, if any 336/// @return Line string 337String buffer_get_line (Buffer buffer ,Integer index ,Arena * arena ,Error * err ) 338FUNC_API_DEPRECATED_SINCE (1 ) 339{ 340String rv = { .size = 0 }; 341 342index = convert_index (index ); 343Array slice = nvim_buf_get_lines (0 ,buffer ,index ,index + 1 , true,arena ,NULL ,err ); 344 345if (!ERROR_SET (err )&& slice .size ) { 346rv = slice .items [0 ].data .string ; 347 } 348 349return rv ; 350} 351 352/// Sets a buffer line 353/// 354/// @deprecated use nvim_buf_set_lines instead. 355/// for positive indices use 356/// "nvim_buf_set_lines(buffer, index, index+1, true, [line])" 357/// for negative indices use 358/// "nvim_buf_set_lines(buffer, index-1, index, true, [line])" 359/// 360/// @param buffer Buffer id 361/// @param index Line index 362/// @param line Contents of the new line 363/// @param[out] err Error details, if any 364void buffer_set_line (Buffer buffer ,Integer index ,String line ,Arena * arena ,Error * err ) 365FUNC_API_DEPRECATED_SINCE (1 ) 366{ 367Object l = STRING_OBJ (line ); 368Array array = { .items = & l , .size = 1 }; 369index = convert_index (index ); 370nvim_buf_set_lines (0 ,buffer ,index ,index + 1 , true,array ,arena ,err ); 371} 372 373/// Deletes a buffer line 374/// 375/// @deprecated use nvim_buf_set_lines instead. 376/// for positive indices use 377/// "nvim_buf_set_lines(buffer, index, index+1, true, [])" 378/// for negative indices use 379/// "nvim_buf_set_lines(buffer, index-1, index, true, [])" 380/// @param buffer buffer id 381/// @param index line index 382/// @param[out] err Error details, if any 383void buffer_del_line (Buffer buffer ,Integer index ,Arena * arena ,Error * err ) 384FUNC_API_DEPRECATED_SINCE (1 ) 385{ 386Array array = ARRAY_DICT_INIT ; 387index = convert_index (index ); 388nvim_buf_set_lines (0 ,buffer ,index ,index + 1 , true,array ,arena ,err ); 389} 390 391/// Retrieves a line range from the buffer 392/// 393/// @deprecated use nvim_buf_get_lines(buffer, newstart, newend, false) 394/// where newstart = start + int(not include_start) - int(start < 0) 395/// newend = end + int(include_end) - int(end < 0) 396/// int(bool) = 1 if bool is true else 0 397/// @param buffer Buffer id 398/// @param start First line index 399/// @param end Last line index 400/// @param include_start True if the slice includes the `start` parameter 401/// @param include_end True if the slice includes the `end` parameter 402/// @param[out] err Error details, if any 403/// @return Array of lines 404ArrayOf (String )buffer_get_line_slice (Buffer buffer , 405Integer start , 406Integer end , 407Boolean include_start , 408Boolean include_end , 409Arena * arena , 410Error * err ) 411FUNC_API_DEPRECATED_SINCE (1 ) 412{ 413start = convert_index (start )+ !include_start ; 414end = convert_index (end )+ include_end ; 415return nvim_buf_get_lines (0 ,buffer ,start ,end , false,arena ,NULL ,err ); 416} 417 418/// Replaces a line range on the buffer 419/// 420/// @deprecated use nvim_buf_set_lines(buffer, newstart, newend, false, lines) 421/// where newstart = start + int(not include_start) + int(start < 0) 422/// newend = end + int(include_end) + int(end < 0) 423/// int(bool) = 1 if bool is true else 0 424/// 425/// @param buffer Buffer id, or 0 for current buffer 426/// @param start First line index 427/// @param end Last line index 428/// @param include_start True if the slice includes the `start` parameter 429/// @param include_end True if the slice includes the `end` parameter 430/// @param replacement Array of lines to use as replacement (0-length 431// array will delete the line range) 432/// @param[out] err Error details, if any 433void buffer_set_line_slice (Buffer buffer ,Integer start ,Integer end ,Boolean include_start , 434Boolean include_end ,ArrayOf (String )replacement ,Arena * arena , 435Error * err ) 436FUNC_API_DEPRECATED_SINCE (1 ) 437{ 438start = convert_index (start )+ !include_start ; 439end = convert_index (end )+ include_end ; 440nvim_buf_set_lines (0 ,buffer ,start ,end , false,replacement ,arena ,err ); 441} 442 443/// Sets a buffer-scoped (b:) variable 444/// 445/// @deprecated 446/// 447/// @param buffer Buffer id, or 0 for current buffer 448/// @param name Variable name 449/// @param value Variable value 450/// @param[out] err Error details, if any 451/// @return Old value or nil if there was no previous value. 452/// 453/// @warning It may return nil if there was no previous value 454/// or if previous value was `v:null`. 455Object buffer_set_var (Buffer buffer ,String name ,Object value ,Arena * arena ,Error * err ) 456FUNC_API_DEPRECATED_SINCE (1 ) 457{ 458buf_T * buf = find_buffer_by_handle (buffer ,err ); 459 460if (!buf ) { 461return NIL ; 462 } 463 464return dict_set_var (buf -> b_vars ,name ,value , false, true,arena ,err ); 465} 466 467/// Removes a buffer-scoped (b:) variable 468/// 469/// @deprecated 470/// 471/// @param buffer Buffer id, or 0 for current buffer 472/// @param name Variable name 473/// @param[out] err Error details, if any 474/// @return Old value 475Object buffer_del_var (Buffer buffer ,String name ,Arena * arena ,Error * err ) 476FUNC_API_DEPRECATED_SINCE (1 ) 477{ 478buf_T * buf = find_buffer_by_handle (buffer ,err ); 479 480if (!buf ) { 481return NIL ; 482 } 483 484return dict_set_var (buf -> b_vars ,name ,NIL , true, true,arena ,err ); 485} 486 487/// Sets a window-scoped (w:) variable 488/// 489/// @deprecated 490/// 491/// @param window |window-ID|, or 0 for current window 492/// @param name Variable name 493/// @param value Variable value 494/// @param[out] err Error details, if any 495/// @return Old value or nil if there was no previous value. 496/// 497/// @warning It may return nil if there was no previous value 498/// or if previous value was `v:null`. 499Object window_set_var (Window window ,String name ,Object value ,Arena * arena ,Error * err ) 500FUNC_API_DEPRECATED_SINCE (1 ) 501{ 502win_T * win = find_window_by_handle (window ,err ); 503 504if (!win ) { 505return NIL ; 506 } 507 508return dict_set_var (win -> w_vars ,name ,value , false, true,arena ,err ); 509} 510 511/// Removes a window-scoped (w:) variable 512/// 513/// @deprecated 514/// 515/// @param window |window-ID|, or 0 for current window 516/// @param name variable name 517/// @param[out] err Error details, if any 518/// @return Old value 519Object window_del_var (Window window ,String name ,Arena * arena ,Error * err ) 520FUNC_API_DEPRECATED_SINCE (1 ) 521{ 522win_T * win = find_window_by_handle (window ,err ); 523 524if (!win ) { 525return NIL ; 526 } 527 528return dict_set_var (win -> w_vars ,name ,NIL , true, true,arena ,err ); 529} 530 531/// Sets a tab-scoped (t:) variable 532/// 533/// @deprecated 534/// 535/// @param tabpage |tab-ID|, or 0 for current tabpage 536/// @param name Variable name 537/// @param value Variable value 538/// @param[out] err Error details, if any 539/// @return Old value or nil if there was no previous value. 540/// 541/// @warning It may return nil if there was no previous value 542/// or if previous value was `v:null`. 543Object tabpage_set_var (Tabpage tabpage ,String name ,Object value ,Arena * arena ,Error * err ) 544FUNC_API_DEPRECATED_SINCE (1 ) 545{ 546tabpage_T * tab = find_tab_by_handle (tabpage ,err ); 547 548if (!tab ) { 549return NIL ; 550 } 551 552return dict_set_var (tab -> tp_vars ,name ,value , false, true,arena ,err ); 553} 554 555/// Removes a tab-scoped (t:) variable 556/// 557/// @deprecated 558/// 559/// @param tabpage |tab-ID|, or 0 for current tabpage 560/// @param name Variable name 561/// @param[out] err Error details, if any 562/// @return Old value 563Object tabpage_del_var (Tabpage tabpage ,String name ,Arena * arena ,Error * err ) 564FUNC_API_DEPRECATED_SINCE (1 ) 565{ 566tabpage_T * tab = find_tab_by_handle (tabpage ,err ); 567 568if (!tab ) { 569return NIL ; 570 } 571 572return dict_set_var (tab -> tp_vars ,name ,NIL , true, true,arena ,err ); 573} 574 575/// @deprecated 576/// @see nvim_set_var 577/// @warning May return nil if there was no previous value 578/// OR if previous value was `v:null`. 579/// @return Old value or nil if there was no previous value. 580Object vim_set_var (String name ,Object value ,Arena * arena ,Error * err ) 581FUNC_API_DEPRECATED_SINCE (1 ) 582{ 583return dict_set_var (get_globvar_dict (),name ,value , false, true,arena ,err ); 584} 585 586/// @deprecated 587/// @see nvim_del_var 588Object vim_del_var (String name ,Arena * arena ,Error * err ) 589FUNC_API_DEPRECATED_SINCE (1 ) 590{ 591return dict_set_var (get_globvar_dict (),name ,NIL , true, true,arena ,err ); 592} 593 594static int64_t convert_index (int64_t index ) 595{ 596return index < 0 ?index - 1 :index ; 597} 598 599/// Gets the option information for one option 600/// 601/// @deprecated Use @ref nvim_get_option_info2 instead. 602/// 603/// @param name Option name 604/// @param[out] err Error details, if any 605/// @return Option Information 606DictAs (get_option_info )nvim_get_option_info (String name ,Arena * arena ,Error * err ) 607FUNC_API_SINCE (7 ) 608FUNC_API_DEPRECATED_SINCE (11 ) 609{ 610return get_option_info (name ,OPT_GLOBAL ,curbuf ,curwin ,arena ,err ); 611} 612 613/// Sets the global value of an option. 614/// 615/// @deprecated 616/// @param channel_id 617/// @param name Option name 618/// @param value New option value 619/// @param[out] err Error details, if any 620void nvim_set_option (uint64_t channel_id ,String name ,Object value ,Error * err ) 621FUNC_API_SINCE (1 ) 622FUNC_API_DEPRECATED_SINCE (11 ) 623{ 624set_option_to (channel_id ,NULL ,kOptScopeGlobal ,name ,value ,err ); 625} 626 627/// Gets the global value of an option. 628/// 629/// @deprecated 630/// @param name Option name 631/// @param[out] err Error details, if any 632/// @return Option value (global) 633Object nvim_get_option (String name ,Error * err ) 634FUNC_API_SINCE (1 ) 635FUNC_API_DEPRECATED_SINCE (11 ) 636FUNC_API_RET_ALLOC 637{ 638return get_option_from (NULL ,kOptScopeGlobal ,name ,err ); 639} 640 641/// Gets a buffer option value 642/// 643/// @deprecated 644/// @param buffer Buffer id, or 0 for current buffer 645/// @param name Option name 646/// @param[out] err Error details, if any 647/// @return Option value 648Object nvim_buf_get_option (Buffer buffer ,String name ,Error * err ) 649FUNC_API_SINCE (1 ) 650FUNC_API_DEPRECATED_SINCE (11 ) 651FUNC_API_RET_ALLOC 652{ 653buf_T * buf = find_buffer_by_handle (buffer ,err ); 654 655if (!buf ) { 656return (Object )OBJECT_INIT ; 657 } 658 659return get_option_from (buf ,kOptScopeBuf ,name ,err ); 660} 661 662/// Sets a buffer option value. Passing `nil` as value deletes the option (only 663/// works if there's a global fallback) 664/// 665/// @deprecated 666/// @param channel_id 667/// @param buffer Buffer id, or 0 for current buffer 668/// @param name Option name 669/// @param value Option value 670/// @param[out] err Error details, if any 671void nvim_buf_set_option (uint64_t channel_id ,Buffer buffer ,String name ,Object value ,Error * err ) 672FUNC_API_SINCE (1 ) 673FUNC_API_DEPRECATED_SINCE (11 ) 674{ 675buf_T * buf = find_buffer_by_handle (buffer ,err ); 676 677if (!buf ) { 678return ; 679 } 680 681set_option_to (channel_id ,buf ,kOptScopeBuf ,name ,value ,err ); 682} 683 684/// Gets a window option value 685/// 686/// @deprecated 687/// @param window |window-ID|, or 0 for current window 688/// @param name Option name 689/// @param[out] err Error details, if any 690/// @return Option value 691Object nvim_win_get_option (Window window ,String name ,Error * err ) 692FUNC_API_SINCE (1 ) 693FUNC_API_DEPRECATED_SINCE (11 ) 694FUNC_API_RET_ALLOC 695{ 696win_T * win = find_window_by_handle (window ,err ); 697 698if (!win ) { 699return (Object )OBJECT_INIT ; 700 } 701 702return get_option_from (win ,kOptScopeWin ,name ,err ); 703} 704 705/// Sets a window option value. Passing `nil` as value deletes the option (only 706/// works if there's a global fallback) 707/// 708/// @deprecated 709/// @param channel_id 710/// @param window |window-ID|, or 0 for current window 711/// @param name Option name 712/// @param value Option value 713/// @param[out] err Error details, if any 714void nvim_win_set_option (uint64_t channel_id ,Window window ,String name ,Object value ,Error * err ) 715FUNC_API_SINCE (1 ) 716FUNC_API_DEPRECATED_SINCE (11 ) 717{ 718win_T * win = find_window_by_handle (window ,err ); 719 720if (!win ) { 721return ; 722 } 723 724set_option_to (channel_id ,win ,kOptScopeWin ,name ,value ,err ); 725} 726 727/// Gets the value of a global or local (buffer, window) option. 728/// 729/// @param[in] from Pointer to buffer or window for local option value. 730/// @param scope Option scope. See OptScope in option.h. 731/// @param name The option name. 732/// @param[out] err Details of an error that may have occurred. 733/// 734/// @return the option value. Must be freed by caller. 735static Object get_option_from (void * from ,OptScope scope ,String name ,Error * err ) 736{ 737VALIDATE_S (name .size > 0 ,"option name" ,"<empty>" , { 738return (Object )OBJECT_INIT ; 739 }); 740 741OptIndex opt_idx = find_option (name .data ); 742VALIDATE_S (opt_idx != kOptInvalid ,"option name" ,name .data , { 743return (Object )OBJECT_INIT ; 744 }); 745 746// Reject a scope the option doesn't support. This must be an explicit check: an unset value is 747// itself Nil, so a Nil value can't distinguish "unsupported scope" from "unset value". 748VALIDATE_S (option_has_scope (opt_idx ,scope ),"option name" ,name .data , { 749return (Object )OBJECT_INIT ; 750 }); 751 752Object value = get_option_value_for (opt_idx ,scope == kOptScopeGlobal ?OPT_GLOBAL :OPT_LOCAL , 753scope ,from ,err ); 754if (ERROR_SET (err )) { 755return (Object )OBJECT_INIT ; 756 } 757 758return value ; 759} 760 761/// Sets the value of a global or local (buffer, window) option. 762/// 763/// @param[in] to Pointer to buffer or window for local option value. 764/// @param scope Option scope. See OptScope in option.h. 765/// @param name The option name. 766/// @param value New option value. 767/// @param[out] err Details of an error that may have occurred. 768static void set_option_to (uint64_t channel_id ,void * to ,OptScope scope ,String name ,Object value , 769Error * err ) 770{ 771VALIDATE_S (name .size > 0 ,"option name" ,"<empty>" , { 772return ; 773 }); 774 775OptIndex opt_idx = find_option (name .data ); 776VALIDATE_S (opt_idx != kOptInvalid ,"option name" ,name .data , { 777return ; 778 }); 779 780// Only scalar Objects (nil/boolean/number/string) are valid option values. 781// Don't use `name` in the error message here, because `name` can be any String. 782// No need to check the option's value type here; the option setter handles that. 783const bool valid = value .type == kObjectTypeNil || value .type == kObjectTypeBoolean 784|| value .type == kObjectTypeInteger || value .type == kObjectTypeString ; 785VALIDATE_EXP (valid ,"value" ,"valid option type" ,api_typename (value .type ), { 786return ; 787 }); 788Object optval = value ; 789 790// For global-win-local options -> setlocal 791// For win-local options -> setglobal and setlocal (opt_flags == 0) 792const int opt_flags 793= (scope == kOptScopeWin && !option_has_scope (opt_idx ,kOptScopeGlobal )) 794 ?0 795 : ((scope == kOptScopeGlobal ) ?OPT_GLOBAL :OPT_LOCAL ); 796 797WITH_SCRIPT_CONTEXT (channel_id , { 798set_option_value_for (opt_idx ,optval ,opt_flags ,scope ,to , true,err ); 799 }); 800} 801 802/// @deprecated Use nvim_exec_lua() instead. 803/// 804/// Calls many API methods atomically. 805/// 806/// This has two main usages: 807/// 1. To perform several requests from an async context atomically, i.e. 808/// without interleaving redraws, RPC requests from other clients, or user 809/// interactions (however API methods may trigger autocommands or event 810/// processing which have such side effects, e.g. |:sleep| may wake timers). 811/// 2. To minimize RPC overhead (roundtrips) of a sequence of many requests. 812/// 813/// @param channel_id 814/// @param calls an array of calls, where each call is described by an array 815/// with two elements: the request name, and an array of arguments. 816/// @param[out] err Validation error details (malformed `calls` parameter), 817/// if any. Errors from batched calls are given in the return value. 818/// 819/// @return Array of two elements. The first is an array of return 820/// values. The second is NIL if all calls succeeded. If a call resulted in 821/// an error, it is a three-element array with the zero-based index of the call 822/// which resulted in an error, the error type and the error message. If an 823/// error occurred, the values from all preceding calls will still be returned. 824Array nvim_call_atomic (uint64_t channel_id ,Array calls ,Arena * arena ,Error * err ) 825FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (12 )FUNC_API_REMOTE_ONLY 826{ 827Array rv = arena_array (arena ,2 ); 828Array results = arena_array (arena ,calls .size ); 829Error nested_error = ERROR_INIT ; 830 831size_t i ;// also used for freeing the variables 832for (i = 0 ;i < calls .size ;i ++ ) { 833VALIDATE_T ("'calls' item" ,kObjectTypeArray ,calls .items [i ].type , { 834 gototheend ; 835 }); 836Array call = calls .items [i ].data .array ; 837VALIDATE_EXP ((call .size == 2 ),"'calls' item" ,"2-item Array" ,NULL , { 838 gototheend ; 839 }); 840VALIDATE_T ("name" ,kObjectTypeString ,call .items [0 ].type , { 841 gototheend ; 842 }); 843String name = call .items [0 ].data .string ; 844VALIDATE_T ("call args" ,kObjectTypeArray ,call .items [1 ].type , { 845 gototheend ; 846 }); 847Array args = call .items [1 ].data .array ; 848 849MsgpackRpcRequestHandler handler = 850msgpack_rpc_get_handler_for (name .data , 851name .size , 852& nested_error ); 853 854if (ERROR_SET (& nested_error )) { 855break ; 856 } 857 858Object result = handler .fn (channel_id ,args ,arena ,& nested_error ); 859if (ERROR_SET (& nested_error )) { 860// error handled after loop 861break ; 862 } 863// TODO(bfredl): wasteful copy. It could be avoided to encoding to msgpack 864// directly here. But `result` might become invalid when next api function 865// is called in the loop. 866ADD_C (results ,copy_object (result ,arena )); 867if (handler .ret_alloc ) { 868api_free_object (result ); 869 } 870 } 871 872ADD_C (rv ,ARRAY_OBJ (results )); 873if (ERROR_SET (& nested_error )) { 874Array errval = arena_array (arena ,3 ); 875ADD_C (errval ,INTEGER_OBJ ((Integer )i )); 876ADD_C (errval ,INTEGER_OBJ (nested_error .type )); 877ADD_C (errval ,STRING_OBJ (copy_string (cstr_as_string (nested_error .msg ),arena ))); 878ADD_C (rv ,ARRAY_OBJ (errval )); 879 }else { 880ADD_C (rv ,NIL ); 881 } 882 883theend : 884api_clear_error (& nested_error ); 885return rv ; 886} 887 888/// @deprecated 889/// 890/// @param channel_id Channel id (passed automatically by the dispatcher) 891/// @param event Event type string 892void nvim_subscribe (uint64_t channel_id ,String event ) 893// XXX: c_grammar.lua is order-sensitive. 894FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (13 )FUNC_API_REMOTE_ONLY 895{ 896// Does nothing. `rpcnotify(0,…)` broadcasts to all channels, there are no "subscriptions". 897} 898 899/// @deprecated 900/// 901/// @param channel_id Channel id (passed automatically by the dispatcher) 902/// @param event Event type string 903void nvim_unsubscribe (uint64_t channel_id ,String event ) 904// XXX: c_grammar.lua is order-sensitive. 905FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (13 )FUNC_API_REMOTE_ONLY 906{ 907// Does nothing. `rpcnotify(0,…)` broadcasts to all channels, there are no "subscriptions". 908} 909 910enum {LINE_BUFFER_MIN_SIZE = 4096 , }; 911 912/// Writes a message to vim output or error buffer. The string is split 913/// and flushed after each newline. Incomplete lines are kept for writing 914/// later. 915/// 916/// @param message Message to write 917/// @param to_err true: message is an error (uses `emsg` instead of `msg`) 918/// @param writeln Append a trailing newline 919static void write_msg (String message ,bool to_err ,bool writeln ) 920{ 921static StringBuilder out_line_buf = KV_INITIAL_VALUE ; 922static StringBuilder err_line_buf = KV_INITIAL_VALUE ; 923StringBuilder * line_buf = to_err ?& err_line_buf :& out_line_buf ; 924 925msg_ext_no_fast (); 926#define PUSH_CHAR (c ) \ 927 if (kv_max(*line_buf) == 0) { \ 928 kv_resize(*line_buf, LINE_BUFFER_MIN_SIZE); \ 929 } \ 930 if (c == NL) { \ 931 kv_push(*line_buf, NUL); \ 932 if (to_err) { \ 933 emsg(line_buf->items); \ 934 } else { \ 935 msg(line_buf->items, 0); \ 936 } \ 937 if (msg_silent == 0) { \ 938 msg_didout = true; \ 939 } \ 940 kv_drop(*line_buf, kv_size(*line_buf)); \ 941 kv_resize(*line_buf, LINE_BUFFER_MIN_SIZE); \ 942 } else if (c == NUL) { \ 943 kv_push(*line_buf, NL); \ 944 } else { \ 945 kv_push(*line_buf, c); \ 946 } 947 948no_wait_return ++ ; 949for (uint32_t i = 0 ;i < message .size ;i ++ ) { 950if (got_int ) { 951break ; 952 } 953PUSH_CHAR (message .data [i ]); 954 } 955if (writeln ) { 956PUSH_CHAR (NL ); 957 } 958no_wait_return -- ; 959msg_end (); 960} 961 962/// @deprecated 963/// 964/// @param str Message 965void nvim_out_write (String str ) 966FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (13 ) 967{ 968write_msg (str , false, false); 969} 970 971/// @deprecated 972/// 973/// @param str Message 974void nvim_err_write (String str ) 975FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (13 ) 976{ 977write_msg (str , true, false); 978} 979 980/// @deprecated 981/// 982/// @param str Message 983void nvim_err_writeln (String str ) 984FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (13 ) 985{ 986write_msg (str , true, true); 987} 988 989/// @deprecated 990/// 991/// Use `nvim_echo` or `nvim_exec_lua("vim.notify(...)", ...)` instead. 992/// 993/// @param msg Message to display to the user 994/// @param log_level The log level 995/// @param opts Reserved for future use. 996/// @param[out] err Error details, if any 997Object nvim_notify (String msg ,Integer log_level ,Dict opts ,Arena * arena ,Error * err ) 998FUNC_API_SINCE (7 )FUNC_API_DEPRECATED_SINCE (13 ) 999{ 1000MAXSIZE_TEMP_ARRAY (args ,3 ); 1001ADD_C (args ,STRING_OBJ (msg )); 1002ADD_C (args ,INTEGER_OBJ (log_level )); 1003ADD_C (args ,DICT_OBJ (opts )); 1004 1005return NLUA_EXEC_STATIC ("return vim.notify(...)" ,args ,kRetObject ,arena ,err ); 1006} 1007 1008/// @deprecated 1009/// 1010/// Use |nvim_win_resize()| instead, e.g., nvim_win_resize(win, -1, height) 1011/// 1012/// Sets the window height. 1013/// 1014/// @param win |window-ID|, or 0 for current window 1015/// @param height Height as a count of rows 1016/// @param[out] err Error details, if any 1017void nvim_win_set_height (Window win ,Integer height ,Error * err ) 1018FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (15 ) 1019{ 1020win_T * w = find_window_by_handle (win ,err ); 1021 1022if (!w ) { 1023return ; 1024 } 1025 1026TRY_WRAP (err , { 1027win_setheight_win ((int )height ,w , true); 1028 }); 1029} 1030 1031/// @deprecated 1032/// 1033/// Use |nvim_win_resize()| instead, e.g., nvim_win_resize(win, width, -1) 1034/// 1035/// Sets the window width. This will only succeed if the screen is split 1036/// vertically. 1037/// 1038/// @param win |window-ID|, or 0 for current window 1039/// @param width Width as a count of columns 1040/// @param[out] err Error details, if any 1041void nvim_win_set_width (Window win ,Integer width ,Error * err ) 1042FUNC_API_SINCE (1 )FUNC_API_DEPRECATED_SINCE (15 ) 1043{ 1044win_T * w = find_window_by_handle (win ,err ); 1045 1046if (!w ) { 1047return ; 1048 } 1049 1050TRY_WRAP (err , { 1051win_setwidth_win ((int )width ,w , true); 1052 }); 1053}