robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#include <assert.h> 2#include <stdbool.h> 3#include <string.h> 4 5#include "nvim/api/keysets_defs.h" 6#include "nvim/api/options.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/autocmd.h" 12#include "nvim/autocmd_defs.h" 13#include "nvim/buffer.h" 14#include "nvim/buffer_defs.h" 15#include "nvim/context.h" 16#include "nvim/globals.h" 17#include "nvim/lua/executor.h" 18#include "nvim/memline.h" 19#include "nvim/memory.h" 20#include "nvim/memory_defs.h" 21#include "nvim/option.h" 22#include "nvim/option_defs.h" 23#include "nvim/option_vars.h" 24#include "nvim/strings.h" 25#include "nvim/types_defs.h" 26#include "nvim/vim_defs.h" 27#include "nvim/window.h" 28 29#include "api/options.c.generated.h" 30 31static int validate_option_value_args (Dict (option )* opts ,char * name ,bool allow_tab , 32OptIndex * opt_idxp ,int * opt_flags ,OptScope * scope , 33void ** from ,char ** filetype ,set_op_T * operation , 34bool * dry_run ,Error * err ) 35{ 36#define HAS_KEY_X (d ,v ) HAS_KEY(d, option, v) 37// Validate incompatible argument combinations first, then resolve handles and scope. 38VALIDATE_CON (!HAS_KEY_X (opts ,filetype ) 39|| (!HAS_KEY_X (opts ,scope )&& !HAS_KEY_X (opts ,buf ) 40&& !HAS_KEY_X (opts ,win )&& !HAS_KEY_X (opts ,tab )), 41"filetype" ,"'scope', 'buf', 'win' or 'tab'" , { 42return FAIL ; 43 }); 44 45VALIDATE_CON (!HAS_KEY_X (opts ,tab )|| allow_tab ,"tab" ,"this function" , { 46return FAIL ; 47 }); 48 49VALIDATE_CON (!HAS_KEY_X (opts ,tab ) 50|| (!HAS_KEY_X (opts ,win )&& !HAS_KEY_X (opts ,buf ) 51&& !HAS_KEY_X (opts ,filetype )&& !HAS_KEY_X (opts ,scope )), 52"tab" ,"'win', 'buf', 'filetype' or 'scope'" , { 53return FAIL ; 54 }); 55 56VALIDATE_CON (!(HAS_KEY_X (opts ,win )&& HAS_KEY_X (opts ,buf )),"buf" ,"win" , { 57return FAIL ; 58 }); 59 60if (HAS_KEY_X (opts ,scope )) { 61if (!strcmp (opts -> scope .data ,"local" )) { 62* opt_flags = OPT_LOCAL ; 63 }else if (!strcmp (opts -> scope .data ,"global" )) { 64* opt_flags = OPT_GLOBAL ; 65 }else { 66VALIDATE_EXP (false,"scope" ,"'local' or 'global'" ,NULL , { 67return FAIL ; 68 }); 69 } 70 } 71 72* scope = kOptScopeGlobal ; 73 74if (filetype != NULL && HAS_KEY_X (opts ,filetype )) { 75* filetype = opts -> filetype .data ; 76 } 77 78if (HAS_KEY_X (opts ,win )) { 79* scope = kOptScopeWin ; 80* from = find_window_by_handle (opts -> win ,err ); 81if (ERROR_SET (err )) { 82return FAIL ; 83 } 84 } 85 86if (HAS_KEY_X (opts ,buf )) { 87VALIDATE_CON (!(HAS_KEY_X (opts ,scope )&& * opt_flags == OPT_GLOBAL ),"buf" ,"global scope" , { 88return FAIL ; 89 }); 90* opt_flags = OPT_LOCAL ; 91* scope = kOptScopeBuf ; 92* from = find_buffer_by_handle (opts -> buf ,err ); 93if (ERROR_SET (err )) { 94return FAIL ; 95 } 96 } 97 98if (HAS_KEY_X (opts ,tab )) { 99* scope = kOptScopeTab ; 100* from = find_tab_by_handle (opts -> tab ,err ); 101if (ERROR_SET (err )) { 102return FAIL ; 103 } 104 } 105 106* opt_idxp = find_option (name ); 107if (* opt_idxp == kOptInvalid ) { 108// unknown option 109api_set_error (err ,kErrorTypeValidation ,"Unknown option '%s'" ,name ); 110return FAIL ; 111 } 112 113if (operation != NULL && HAS_KEY_X (opts ,operation )) { 114if (strequal (opts -> operation .data ,"set" )) { 115* operation = OP_NONE ; 116 }else if (strequal (opts -> operation .data ,"append" )) { 117* operation = OP_ADDING ; 118 }else if (strequal (opts -> operation .data ,"prepend" )) { 119* operation = OP_PREPENDING ; 120 }else if (strequal (opts -> operation .data ,"remove" )) { 121* operation = OP_REMOVING ; 122 }else { 123VALIDATE_EXP (false,"operation" ,"'set', 'append', 'prepend', or 'remove'" ,NULL , { 124return FAIL ; 125 }); 126 } 127 128VALIDATE_CON (* operation == OP_NONE || option_has_type (* opt_idxp ,kObjectTypeString ) 129|| option_has_type (* opt_idxp ,kObjectTypeInteger ), 130opts -> operation .data , 131"boolean options" , { 132return FAIL ; 133 }); 134 } 135 136if (dry_run != NULL && HAS_KEY_X (opts ,dry_run )) { 137* dry_run = opts -> dry_run ; 138 } 139 140// Reject keys whose scope the option doesn't support. 141VALIDATE_CON (!HAS_KEY_X (opts ,tab )|| option_has_scope (* opt_idxp ,kOptScopeTab ), 142"tab" ,name , {return FAIL ; }); 143 144// If 'buf' or 'win' is passed, make sure the option supports it. 145if (* scope == kOptScopeBuf || * scope == kOptScopeWin ) { 146if (!option_has_scope (* opt_idxp ,* scope )) { 147char * tgt = * scope == kOptScopeBuf ?"buf" :"win" ; 148char * global = option_has_scope (* opt_idxp ,kOptScopeGlobal ) ?"global " :"" ; 149char * req = option_has_scope (* opt_idxp ,kOptScopeBuf ) 150 ?"buffer-local " 151 : (option_has_scope (* opt_idxp ,kOptScopeWin ) ?"window-local " :"" ); 152 153api_set_error (err ,kErrorTypeValidation ,"'%s' cannot be passed for %s%soption '%s'" , 154tgt ,global ,req ,name ); 155return FAIL ; 156 } 157 } 158 159return ERROR_SET (err ) ?FAIL :OK ; 160#undef HAS_KEY_X 161} 162 163/// Create a dummy buffer and run the FileType autocmd on it. 164static buf_T * do_ft_buf (const char * filetype ,CtxSwitch * aco ,Error * err ) 165FUNC_ATTR_NONNULL_ARG (2 ,3 ) 166{ 167if (filetype == NULL ) { 168return NULL ; 169 } 170 171// Allocate a buffer without putting it in the buffer list. 172buf_T * ftbuf = buflist_new (NULL ,NULL ,1 ,BLN_DUMMY ); 173if (ftbuf == NULL ) { 174api_set_error (err ,kErrorTypeException ,"Could not create internal buffer" ); 175return NULL ; 176 } 177 178// Open a memline for use by autocommands. 179if (ml_open (ftbuf )== FAIL ) { 180api_set_error (err ,kErrorTypeException ,"Could not load internal buffer" ); 181return ftbuf ; 182 } 183 184bufref_T bufref ; 185set_bufref (& bufref ,ftbuf ); 186 187// Set curwin/curbuf to buf and save a few things. 188ctx_switch (aco ,NULL ,NULL ,ftbuf ,0 ); 189 190set_option_direct (kOptBufhidden ,STATIC_CSTR_AS_OBJ ("hide" ),OPT_LOCAL ,SID_NONE ); 191set_option_direct (kOptBuftype ,STATIC_CSTR_AS_OBJ ("nofile" ),OPT_LOCAL ,SID_NONE ); 192assert (ftbuf -> b_ml .ml_mfp -> mf_fd < 0 );// ml_open() should not have opened swapfile already 193ftbuf -> b_p_swf = false; 194ftbuf -> b_p_ml = false; 195ftbuf -> b_p_ft = xstrdup (filetype ); 196 197if (!has_event (EVENT_FILETYPE )) { 198return ftbuf ;// Nothing more to do. 199 } 200 201bool did_au_ft = false; 202TRY_WRAP (err , { 203did_au_ft = do_filetype_autocmd (ftbuf , true); 204 }); 205 206if (!bufref_valid (& bufref )) { 207if (!ERROR_SET (err )) { 208api_set_error (err ,kErrorTypeException ,"Internal buffer was deleted" ); 209 } 210return NULL ; 211 } 212 213if (!did_au_ft && !ERROR_SET (err )) { 214api_set_error (err ,kErrorTypeException ,"Could not execute FileType autocommands" ); 215 } 216return ftbuf ; 217} 218 219static void wipe_ft_buf (buf_T * buf ) 220FUNC_ATTR_NONNULL_ALL 221{ 222block_autocmds (); 223 224bufref_T bufref ; 225set_bufref (& bufref ,buf ); 226 227close_windows (buf , false); 228// Autocommands are blocked, but 'bufhidden' may have wiped it already. 229// Also can't wipe if the buffer is somehow still in a window or current. 230if (bufref_valid (& bufref )&& buf != curbuf && buf -> b_nwindows == 0 ) { 231wipe_buffer (buf , false); 232 } 233if (bufref_valid (& bufref )) { 234buf -> b_flags &= ~BF_DUMMY ;// Couldn't wipe; keep it instead. 235 } 236 237unblock_autocmds (); 238} 239 240/// Gets the value of an option. The behavior of this function matches that of 241/// |:set|: the local value of an option is returned if it exists; otherwise, 242/// the global value is returned. Local values always correspond to the current 243/// buffer or window, unless "buf" or "win" is set in {opts}. 244/// 245/// @param name Option name 246/// @param opts Optional parameters 247/// - buf: Buffer number. Used for getting buffer local options. 248/// Implies {scope} is "local". 249/// - filetype: |filetype|. Used to get the default option for a 250/// specific filetype. Cannot be used with any other option. 251/// Note: this will trigger |ftplugin| and all |FileType| 252/// autocommands for the corresponding filetype. 253/// - scope: One of "global" or "local". Analogous to 254/// |:setglobal| and |:setlocal|, respectively. 255/// - tab: |tab-ID| for tab-local options. Currently only 256/// supports "cmdheight". Tabpage `0` means the current tabpage. 257/// - win: |window-ID|. Used for getting window local options. 258/// @param[out] err Error details, if any 259/// @return Option value 260Object nvim_get_option_value (String name ,Dict (option )* opts ,Error * err ) 261FUNC_API_SINCE (9 )FUNC_API_RET_ALLOC 262{ 263OptIndex opt_idx = 0 ; 264int opt_flags = 0 ; 265OptScope scope = kOptScopeGlobal ; 266void * from = NULL ; 267char * filetype = NULL ; 268 269if (!validate_option_value_args (opts ,name .data , true,& opt_idx ,& opt_flags ,& scope ,& from , 270& filetype ,NULL ,NULL ,err )) { 271return (Object )OBJECT_INIT ; 272 } 273 274CtxSwitch aco = {0 }; 275 276buf_T * ftbuf = do_ft_buf (filetype ,& aco ,err ); 277if (ERROR_SET (err )) { 278// Restore curwin/curbuf and a few other things. 279ctx_restore (& aco ); 280if (ftbuf != NULL ) { 281wipe_ft_buf (ftbuf ); 282 } 283return (Object )OBJECT_INIT ; 284 } 285 286if (ftbuf != NULL ) { 287assert (!from ); 288from = ftbuf ; 289 } 290 291Object value = get_option_value_for (opt_idx ,opt_flags ,scope ,from ,err ); 292 293// Restore curwin/curbuf and a few other things. 294ctx_restore (& aco ); 295if (ftbuf != NULL ) { 296wipe_ft_buf (ftbuf ); 297 } 298 299if (ERROR_SET (err )) { 300api_free_object (value ); 301return (Object )OBJECT_INIT ; 302 } 303 304return value ; 305} 306 307/// Sets the value of an option. The behavior of this function matches that of 308/// |:set|: for global-local options, both the global and local value are set 309/// unless otherwise specified with {scope}. 310/// 311/// Note the options {win} and {buf} cannot be used together. 312/// 313/// @param name Option name 314/// @param value New option value 315/// @param opts Optional parameters 316/// - buf: Buffer number. Used for setting buffer local option. 317/// - dry_run: (`boolean?`, default: false) If true, validates the 318/// option value without setting it. 319/// - operation: One of "set", "append", "prepend", or "remove". 320/// Corresponds to |:set=|, |:set+=|, |:set^=|, and |:set-=|. 321/// Default is "set". 322/// - scope: One of "global" or "local". Analogous to 323/// |:setglobal| and |:setlocal|, respectively. 324/// - tab: |tab-ID| for tab-local options (currently only 'cmdheight'). Tabpage 0 325/// means the current tabpage. If a non-current tab is given, the value will take 326/// effect when it is switched-to. 327/// - win: |window-ID|. Used for setting window local option. 328/// @param[out] err Error details, if any 329/// @return Option value 330Object nvim_set_option_value (uint64_t channel_id ,String name ,Object value ,Dict (option )* opts , 331Arena * arena ,Error * err ) 332FUNC_API_SINCE (9 ) 333{ 334OptIndex opt_idx = 0 ; 335int opt_flags = 0 ; 336OptScope scope = kOptScopeGlobal ; 337set_op_T operation = OP_NONE ; 338void * to = NULL ; 339bool dry_run = false; 340if (!validate_option_value_args (opts ,name .data , true,& opt_idx ,& opt_flags ,& scope ,& to ,NULL , 341& operation ,& dry_run ,err )) { 342return NIL ; 343 } 344 345// If: 346// - window id is provided 347// - scope is not provided 348// - option is global or local to window (global-local) 349// 350// Then force scope to local since we don't want to change the global option 351if (scope == kOptScopeWin && opt_flags == 0 ) { 352if (option_has_scope (opt_idx ,kOptScopeGlobal )) { 353opt_flags = OPT_LOCAL ; 354 } 355 } 356 357// Convert the incoming value into an Object. 358bool error = false; 359Object optval_right = optval_from_obj (opt_idx ,value ,operation ,& error ); 360 361VALIDATE_EXP (!error ,name .data ,"a valid type" ,api_typename (value .type ), { 362return NIL ; 363 }); 364 365Object merged_val = NIL ; 366const char * errmsg = NULL ; 367vimoption_T * option = get_option (opt_idx ); 368 369// Need to use varp specific to buf/win to ensure that merges are handled 370// correctly when the supplied buf/win are different than curbuf/curwin. 371buf_T * buf = scope == kOptScopeBuf ?to :curbuf ; 372win_T * win = scope == kOptScopeWin ?to :curwin ; 373void * varp = get_varp_from (option ,buf ,win ); 374char * argp = NULL ; 375 376switch (optval_right .type ) { 377case kObjectTypeUnset : 378case kObjectTypeNil : 379break ; 380case kObjectTypeString : { 381char * optval_escaped = escape_option_str_cmdline (optval_right .data .string .data ); 382// We need a leading equal sign because get_option_newval is used for 383// cmdline stuff and expects an = 384argp = arena_printf (arena ,"=%s" ,optval_escaped ).data ; 385XFREE_CLEAR (optval_escaped ); 386break ; 387 } 388case kObjectTypeInteger : 389argp = arena_printf (arena ,"=%" PRId64 ,optval_right .data .integer ).data ; 390break ; 391case kObjectTypeBoolean : 392merged_val = optval_right ; 393break ; 394case kObjectTypeLuaRef : 395// Callback option: no ":set"-style merge; take an independent ref for the set below. 396merged_val = copy_object (optval_right ,NULL ); 397break ; 398default : 399abort (); 400 } 401 402optval_free (optval_right ); 403 404if (optval_right .type == kObjectTypeInteger || optval_right .type == kObjectTypeString ) { 405Object oldval = optval_own (opt_idx ,opt_from_varp (opt_idx ,varp )); 406merged_val = get_option_newval (opt_idx ,opt_flags ,PREFIX_NONE ,& argp ,0 ,operation , 407varp ,& oldval ,& errmsg ); 408optval_free (oldval ); 409VALIDATE (errmsg == NULL ,"%s" ,errmsg , { 410return NIL ; 411 }); 412 } 413 414if (dry_run ) { 415const CharBuf errbuf = { (char [IOSIZE ]){0 },IOSIZE }; 416errmsg = validate_option_value (opt_idx ,& merged_val ,opt_flags ,buf ,win ,& errbuf ); 417if (errmsg != NULL ) { 418api_set_error (err ,kErrorTypeException ,"%s" ,errmsg ); 419optval_free (merged_val ); 420return NIL ; 421 } 422 }else { 423WITH_SCRIPT_CONTEXT (channel_id , { 424set_option_value_for (opt_idx ,merged_val ,opt_flags ,scope ,to ,operation == OP_NONE ,err ); 425 }); 426 } 427 428// Return the value in its structured (list/map/set) form. 429Object rv = optval_to_obj (opt_idx ,merged_val ,arena ); 430optval_free (merged_val ); 431return rv ; 432} 433 434/// Gets the option information for all options. 435/// 436/// The dict has the full option names as keys and option metadata dicts as detailed at 437/// |nvim_get_option_info2()|. 438/// 439/// @see |nvim_get_commands()| 440/// 441/// @return dict of all options 442Dict nvim_get_all_options_info (Arena * arena ,Error * err ) 443FUNC_API_SINCE (7 ) 444{ 445return get_all_options_info (arena ); 446} 447 448/// Gets the option information for one option from arbitrary buffer or window 449/// 450/// Resulting dict has keys: 451/// - name: Name of the option (like 'filetype') 452/// - shortname: Shortened name of the option (like 'ft') 453/// - type: type of option ("string", "number" or "boolean") 454/// - default: The default value for the option 455/// - was_set: Whether the option was set. 456/// 457/// - last_set_sid: Last set script id (if any) 458/// - last_set_linenr: line number where option was set 459/// - last_set_chan: Channel where option was set (0 for local) 460/// 461/// - scope: one of "global", "win", "buf", or "tab" 462/// - global_local: whether win or buf option has a global value 463/// 464/// - commalist: List of comma separated values 465/// - flaglist: List of single char flags 466/// 467/// When {scope} is not provided, the last set information applies to the local 468/// value in the current buffer or window if it is available, otherwise the 469/// global value information is returned. This behavior can be disabled by 470/// explicitly specifying {scope} in the {opts} table. 471/// 472/// @param name Option name 473/// @param opts Optional parameters 474/// - buf: Buffer number. Used for getting buffer local options. 475/// Implies {scope} is "local". 476/// - scope: One of "global" or "local". Analogous to 477/// |:setglobal| and |:setlocal|, respectively. 478/// - win: |window-ID|. Used for getting window local options. 479/// @param[out] err Error details, if any 480/// @return Option Information 481DictAs (get_option_info )nvim_get_option_info2 (String name ,Dict (option )* opts ,Arena * arena , 482Error * err ) 483FUNC_API_SINCE (11 ) 484{ 485OptIndex opt_idx = 0 ; 486int opt_flags = 0 ; 487OptScope scope = kOptScopeGlobal ; 488void * from = NULL ; 489// TODO(justinmk): support tab-local option. 490if (!validate_option_value_args (opts ,name .data , false,& opt_idx ,& opt_flags ,& scope ,& from ,NULL , 491NULL ,NULL ,err )) { 492return (Dict )ARRAY_DICT_INIT ; 493 } 494 495buf_T * buf = (scope == kOptScopeBuf ) ? (buf_T * )from :curbuf ; 496win_T * win = (scope == kOptScopeWin ) ? (win_T * )from :curwin ; 497 498return get_option_info (name ,opt_flags ,buf ,win ,arena ,err ); 499}