aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/doc/api.txt10
-rw-r--r--runtime/lua/vim/_meta.lua8
-rw-r--r--src/nvim/api/options.c85
-rw-r--r--test/functional/api/vim_spec.lua15
-rw-r--r--test/functional/lua/vim_spec.lua4
5 files changed, 105 insertions, 17 deletions
diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt
index 331a4fe700..1d7a783bf1 100644
--- a/runtime/doc/api.txt
+++ b/runtime/doc/api.txt
@@ -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
returned if it exists; otherwise, 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
- specific buffer or window, use |nvim_buf_get_option()| or
- |nvim_win_get_option()|.
+ or window, unless "buf" or "win" is set in {opts}.
Parameters: ~
{name} Option name
{opts} Optional parameters
- • scope: One of 'global' or 'local'. Analogous to
+ • scope: One of "global" or "local". Analogous to
|: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: ~
Option value
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 59cf837a1d..715a7e5561 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -91,11 +91,11 @@ do -- buffer option accessor
return new_buf_opt_accessor(k)
end
- return a.nvim_buf_get_option(bufnr or 0, k)
+ return a.nvim_get_option_value(k, { buf = bufnr or 0 })
end
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
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
return new_win_opt_accessor(k)
end
- return a.nvim_win_get_option(winnr or 0, k)
+ return a.nvim_get_option_value(k, { win = winnr or 0 })
end
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
return make_meta_accessor(get, set, nil, function(k)
diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c
index 61d4becd32..a35c7222ed 100644
--- a/src/nvim/api/options.c
+++ b/src/nvim/api/options.c
@@ -24,14 +24,15 @@
/// 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,
/// 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
-/// specific buffer or window, use |nvim_buf_get_option()| or
-/// |nvim_win_get_option()|.
+/// buffer or window, unless "buf" or "win" is set in {opts}.
///
/// @param name Option name
/// @param opts Optional parameters
-/// - scope: One of 'global' or 'local'. Analogous to
+/// - scope: One of "global" or "local". Analogous to
/// |: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
/// @return Option value
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;
}
+ 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;
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:
rv = STRING_OBJ(cstr_as_string(stringval));
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);
}
}
+
+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;
+}
diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua
index ea5725d836..ef6798dea3 100644
--- a/test/functional/api/vim_spec.lua
+++ b/test/functional/api/vim_spec.lua
@@ -1453,10 +1453,21 @@ describe('API', function()
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'}))
+ eq('4,3', nvim('get_option_value', 'colorcolumn', {win = 0, scope = 'local'}))
command("set modified hidden")
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)
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index 1af46b7c2f..44e65afc78 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -1396,7 +1396,7 @@ describe('lua stdlib', function()
]]
eq('', funcs.luaeval "vim.bo.filetype")
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'))
matches("Expected lua string$",
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[0].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'))
matches("Expected lua string$",
pcall_err(exec_lua, 'return vim.wo[0][0].list'))