Merge pull request #18743 from gpanders/bowooptvalue

Add "buf" and "win" to nvim_get_option_value and use them in vim.bo and vim.wo
This commit is contained in:
Gregory Anders 2022-06-20 17:28:37 -06:00 committed by GitHub
commit 6d52a29c3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 17 deletions

View File

@ -2037,15 +2037,17 @@ nvim_get_option_value({name}, {*opts}) *nvim_get_option_value()*
matches that of |:set|: the local value of an option is matches that of |:set|: the local value of an option is
returned if it exists; otherwise, the global value is returned if it exists; otherwise, the global value is
returned. Local values always correspond to the current buffer returned. Local values always correspond to the current buffer
or window. To get a buffer-local or window-local option for a or window, unless "buf" or "win" is set in {opts}.
specific buffer or window, use |nvim_buf_get_option()| or
|nvim_win_get_option()|.
Parameters: ~ Parameters: ~
{name} Option name {name} Option name
{opts} Optional parameters {opts} Optional parameters
• scope: One of 'global' or 'local'. Analogous to • scope: One of "global" or "local". Analogous to
|:setglobal| and |:setlocal|, respectively. |:setglobal| and |:setlocal|, respectively.
• win: |window-ID|. Used for getting window local
options.
• buf: Buffer number. Used for getting buffer
local options. Implies {scope} is "local".
Return: ~ Return: ~
Option value Option value

View File

@ -91,11 +91,11 @@ do -- buffer option accessor
return new_buf_opt_accessor(k) return new_buf_opt_accessor(k)
end end
return a.nvim_buf_get_option(bufnr or 0, k) return a.nvim_get_option_value(k, { buf = bufnr or 0 })
end end
local function set(k, v) local function set(k, v)
return a.nvim_buf_set_option(bufnr or 0, k, v) return a.nvim_set_option_value(k, v, { buf = bufnr or 0 })
end end
return make_meta_accessor(get, set, nil, function(k) return make_meta_accessor(get, set, nil, function(k)
@ -121,11 +121,11 @@ do -- window option accessor
if winnr == nil and type(k) == 'number' then if winnr == nil and type(k) == 'number' then
return new_win_opt_accessor(k) return new_win_opt_accessor(k)
end end
return a.nvim_win_get_option(winnr or 0, k) return a.nvim_get_option_value(k, { win = winnr or 0 })
end end
local function set(k, v) local function set(k, v)
return a.nvim_win_set_option(winnr or 0, k, v) return a.nvim_set_option_value(k, v, { win = winnr or 0 })
end end
return make_meta_accessor(get, set, nil, function(k) return make_meta_accessor(get, set, nil, function(k)

View File

@ -24,14 +24,15 @@
/// Gets the value of an option. The behavior of this function matches that of /// Gets the value of an option. The behavior of this function matches that of
/// |:set|: the local value of an option is returned if it exists; otherwise, /// |:set|: the local value of an option is returned if it exists; otherwise,
/// the global value is returned. Local values always correspond to the current /// the global value is returned. Local values always correspond to the current
/// buffer or window. To get a buffer-local or window-local option for a /// buffer or window, unless "buf" or "win" is set in {opts}.
/// specific buffer or window, use |nvim_buf_get_option()| or
/// |nvim_win_get_option()|.
/// ///
/// @param name Option name /// @param name Option name
/// @param opts Optional parameters /// @param opts Optional parameters
/// - scope: One of 'global' or 'local'. Analogous to /// - scope: One of "global" or "local". Analogous to
/// |:setglobal| and |:setlocal|, respectively. /// |:setglobal| and |:setlocal|, respectively.
/// - win: |window-ID|. Used for getting window local options.
/// - buf: Buffer number. Used for getting buffer local options.
/// Implies {scope} is "local".
/// @param[out] err Error details, if any /// @param[out] err Error details, if any
/// @return Option value /// @return Option value
Object nvim_get_option_value(String name, Dict(option) *opts, Error *err) Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
@ -54,9 +55,44 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
goto end; goto end;
} }
int opt_type = SREQ_GLOBAL;
void *from = NULL;
if (opts->win.type == kObjectTypeInteger) {
opt_type = SREQ_WIN;
from = 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");
goto end;
}
if (opts->buf.type == kObjectTypeInteger) {
scope = OPT_LOCAL;
opt_type = SREQ_BUF;
from = 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");
goto end;
}
if (HAS_KEY(opts->scope) && HAS_KEY(opts->buf)) {
api_set_error(err, kErrorTypeValidation, "scope and buf cannot be used together");
goto end;
}
if (HAS_KEY(opts->win) && HAS_KEY(opts->buf)) {
api_set_error(err, kErrorTypeValidation, "buf and win cannot be used together");
goto end;
}
long numval = 0; long numval = 0;
char *stringval = NULL; char *stringval = NULL;
switch (get_option_value(name.data, &numval, &stringval, scope)) { int result = get_option_value_for(name.data, &numval, &stringval, scope, opt_type, from, err);
if (ERROR_SET(err)) {
goto end;
}
switch (result) {
case 0: case 0:
rv = STRING_OBJ(cstr_as_string(stringval)); rv = STRING_OBJ(cstr_as_string(stringval));
break; break;
@ -507,3 +543,42 @@ static void set_option_value_err(char *key, long numval, char *stringval, int op
api_set_error(err, kErrorTypeException, "%s", errmsg); api_set_error(err, kErrorTypeException, "%s", errmsg);
} }
} }
int get_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;
int result = 0;
try_start();
switch (opt_type) {
case SREQ_WIN:
if (switch_win_noblock(&switchwin, (win_T *)from, win_find_tabpage((win_T *)from), true)
== FAIL) {
restore_win_noblock(&switchwin, true);
if (try_end(err)) {
return result;
}
api_set_error(err,
kErrorTypeException,
"Problem while switching windows");
return result;
}
result = get_option_value(key, numval, stringval, opt_flags);
restore_win_noblock(&switchwin, true);
break;
case SREQ_BUF:
aucmd_prepbuf(&aco, (buf_T *)from);
result = get_option_value(key, numval, stringval, opt_flags);
aucmd_restbuf(&aco);
break;
case SREQ_GLOBAL:
result = get_option_value(key, numval, stringval, opt_flags);
break;
}
try_end(err);
return result;
}

View File

@ -1453,10 +1453,21 @@ describe('API', function()
it('set local window options', function() it('set local window options', function()
-- Different to nvim_win_set_option -- Different to nvim_win_set_option
nvim('set_option_value', 'colorcolumn', '4,3', {win=0, scope='local'}) nvim('set_option_value', 'colorcolumn', '4,3', {win=0, scope='local'})
eq('4,3', nvim('get_option_value', 'colorcolumn', {scope = 'local'})) eq('4,3', nvim('get_option_value', 'colorcolumn', {win = 0, scope = 'local'}))
command("set modified hidden") command("set modified hidden")
command("enew") -- edit new buffer, window option is reset command("enew") -- edit new buffer, window option is reset
eq('', nvim('get_option_value', 'colorcolumn', {scope = 'local'})) eq('', nvim('get_option_value', 'colorcolumn', {win = 0, scope = 'local'}))
end)
it('get buffer or window-local options', function()
nvim('command', 'new')
local buf = nvim('get_current_buf').id
nvim('buf_set_option', buf, 'tagfunc', 'foobar')
eq('foobar', nvim('get_option_value', 'tagfunc', {buf = buf}))
local win = nvim('get_current_win').id
nvim('win_set_option', win, 'number', true)
eq(true, nvim('get_option_value', 'number', {win = win}))
end) end)
end) end)

View File

@ -1396,7 +1396,7 @@ describe('lua stdlib', function()
]] ]]
eq('', funcs.luaeval "vim.bo.filetype") eq('', funcs.luaeval "vim.bo.filetype")
eq(true, funcs.luaeval "vim.bo[BUF].modifiable") eq(true, funcs.luaeval "vim.bo[BUF].modifiable")
matches("Invalid option name: 'nosuchopt'$", matches("unknown option 'nosuchopt'$",
pcall_err(exec_lua, 'return vim.bo.nosuchopt')) pcall_err(exec_lua, 'return vim.bo.nosuchopt'))
matches("Expected lua string$", matches("Expected lua string$",
pcall_err(exec_lua, 'return vim.bo[0][0].autoread')) pcall_err(exec_lua, 'return vim.bo[0][0].autoread'))
@ -1415,7 +1415,7 @@ describe('lua stdlib', function()
eq(0, funcs.luaeval "vim.wo.cole") eq(0, funcs.luaeval "vim.wo.cole")
eq(0, funcs.luaeval "vim.wo[0].cole") eq(0, funcs.luaeval "vim.wo[0].cole")
eq(0, funcs.luaeval "vim.wo[1001].cole") eq(0, funcs.luaeval "vim.wo[1001].cole")
matches("Invalid option name: 'notanopt'$", matches("unknown option 'notanopt'$",
pcall_err(exec_lua, 'return vim.wo.notanopt')) pcall_err(exec_lua, 'return vim.wo.notanopt'))
matches("Expected lua string$", matches("Expected lua string$",
pcall_err(exec_lua, 'return vim.wo[0][0].list')) pcall_err(exec_lua, 'return vim.wo[0][0].list'))