1
mirror of https://github.com/neovim/neovim.git synced 2024-12-28 14:31:13 -07:00

Merge pull request from lewis6991/setwinopt

feat(api): add `win` and `buf` to `nvim_set_option_value`
This commit is contained in:
bfredl 2022-05-25 17:05:28 +02:00 committed by GitHub
commit 24352cba01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 7 deletions
runtime/doc
src/nvim/api
test/functional/api

View File

@ -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.

View File

@ -85,6 +85,8 @@ return {
};
option = {
"scope";
"win";
"buf";
};
highlight = {
"bold";

View File

@ -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;

View File

@ -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.

View File

@ -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()