diff options
-rw-r--r-- | runtime/doc/api.txt | 6 | ||||
-rw-r--r-- | src/nvim/api/keysets.lua | 2 | ||||
-rw-r--r-- | src/nvim/api/private/helpers.c | 6 | ||||
-rw-r--r-- | src/nvim/api/vim.c | 39 | ||||
-rw-r--r-- | test/functional/api/vim_spec.lua | 18 |
5 files changed, 64 insertions, 7 deletions
diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index e78f140e99..3ab7c77056 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -1674,12 +1674,18 @@ nvim_set_option_value({name}, {value}, {*opts}) global and local value are set unless otherwise specified with {scope}. + Note the options {win} and {buf} cannot be used together. + Parameters: ~ {name} Option name {value} New option value {opts} Optional parameters • scope: One of 'global' or 'local'. Analogous to |:setglobal| and |:setlocal|, respectively. + • win: |window-ID|. Used for setting window local + option. + • buf: Buffer number. Used for setting buffer + local option. nvim_set_var({name}, {value}) *nvim_set_var()* Sets a global (g:) variable. diff --git a/src/nvim/api/keysets.lua b/src/nvim/api/keysets.lua index 6924e2ef8f..d4882abffe 100644 --- a/src/nvim/api/keysets.lua +++ b/src/nvim/api/keysets.lua @@ -85,6 +85,8 @@ return { }; option = { "scope"; + "win"; + "buf"; }; highlight = { "bold"; diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index adabb1471e..c251a4a25c 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -1040,8 +1040,8 @@ Object copy_object(Object obj) } } -static void set_option_value_for(char *key, int numval, char *stringval, int opt_flags, - int opt_type, void *from, Error *err) +void set_option_value_for(char *key, long numval, char *stringval, int opt_flags, + int opt_type, void *from, Error *err) { switchwin_T switchwin; aco_save_T aco; @@ -1081,7 +1081,7 @@ static void set_option_value_for(char *key, int numval, char *stringval, int opt } -static void set_option_value_err(char *key, int numval, char *stringval, int opt_flags, Error *err) +static void set_option_value_err(char *key, long numval, char *stringval, int opt_flags, Error *err) { char *errmsg; diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 15992a98be..8e216c2031 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -775,11 +775,15 @@ end: /// |:set|: for global-local options, both the global and local value are set /// unless otherwise specified with {scope}. /// +/// Note the options {win} and {buf} cannot be used together. +/// /// @param name Option name /// @param value New option value /// @param opts Optional parameters /// - scope: One of 'global' or 'local'. Analogous to /// |:setglobal| and |:setlocal|, respectively. +/// - win: |window-ID|. Used for setting window local option. +/// - buf: Buffer number. Used for setting buffer local option. /// @param[out] err Error details, if any void nvim_set_option_value(String name, Object value, Dict(option) *opts, Error *err) FUNC_API_SINCE(9) @@ -799,6 +803,36 @@ void nvim_set_option_value(String name, Object value, Dict(option) *opts, Error return; } + int opt_type = SREQ_GLOBAL; + void *to = NULL; + + if (opts->win.type == kObjectTypeInteger) { + opt_type = SREQ_WIN; + to = find_window_by_handle((int)opts->win.data.integer, err); + } else if (HAS_KEY(opts->win)) { + api_set_error(err, kErrorTypeValidation, "invalid value for key: win"); + return; + } + + if (opts->buf.type == kObjectTypeInteger) { + scope = OPT_LOCAL; + opt_type = SREQ_BUF; + to = find_buffer_by_handle((int)opts->buf.data.integer, err); + } else if (HAS_KEY(opts->buf)) { + api_set_error(err, kErrorTypeValidation, "invalid value for key: buf"); + return; + } + + if (HAS_KEY(opts->scope) && HAS_KEY(opts->buf)) { + api_set_error(err, kErrorTypeValidation, "scope and buf cannot be used together"); + return; + } + + if (HAS_KEY(opts->win) && HAS_KEY(opts->buf)) { + api_set_error(err, kErrorTypeValidation, "buf and win cannot be used together"); + return; + } + long numval = 0; char *stringval = NULL; @@ -820,10 +854,7 @@ void nvim_set_option_value(String name, Object value, Dict(option) *opts, Error return; } - char *e = set_option_value(name.data, numval, stringval, scope); - if (e) { - api_set_error(err, kErrorTypeException, "%s", e); - } + set_option_value_for(name.data, numval, stringval, scope, opt_type, to, err); } /// Gets the option information for all options. diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index c5e8cfee23..55535a92e5 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -1439,6 +1439,24 @@ describe('API', function() nvim('set_option_value', 'autoread', NIL, {scope = 'local'}) eq(NIL, nvim('get_option_value', 'autoread', {scope = 'local'})) end) + + it('set window options', function() + -- Same as to nvim_win_set_option + nvim('set_option_value', 'colorcolumn', '4,3', {win=0}) + eq('4,3', nvim('get_option_value', 'colorcolumn', {scope = 'local'})) + command("set modified hidden") + command("enew") -- edit new buffer, window option is preserved + eq('4,3', nvim('get_option_value', 'colorcolumn', {scope = 'local'})) + end) + + it('set local window options', function() + -- Different to nvim_win_set_option + nvim('set_option_value', 'colorcolumn', '4,3', {win=0, scope='local'}) + eq('4,3', nvim('get_option_value', 'colorcolumn', {scope = 'local'})) + command("set modified hidden") + command("enew") -- edit new buffer, window option is reset + eq('', nvim('get_option_value', 'colorcolumn', {scope = 'local'})) + end) end) describe('nvim_{get,set}_current_buf, nvim_list_bufs', function() |