diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index 1eebd0bb18..e3daf1defa 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -979,6 +979,19 @@ nvim_get_hl_id_by_name({name}) *nvim_get_hl_id_by_name()* similar to |hlID()|, but allocates a new ID if not present. +nvim_get_hl_ns({*opts}) *nvim_get_hl_ns()* + Gets the active highlight namespace. + + Parameters: ~ + • {opts} Optional parameters + • winid: (number) |window-ID| for retrieving a window's + highlight namespace. A value of -1 is returned when + |nvim_win_set_hl_ns()| has not been called for the window + (or was called with a namespace of -1). + + Return: ~ + Namespace id, or -1 + nvim_get_keymap({mode}) *nvim_get_keymap()* Gets a list of global (non-buffer-local) |mapping| definitions. diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index 68ef54eb2f..f0101eb6a4 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -1258,6 +1258,16 @@ function vim.api.nvim_get_hl_by_name(name, rgb) end --- @return integer function vim.api.nvim_get_hl_id_by_name(name) end +--- Gets the active highlight namespace. +--- +--- @param opts vim.api.keyset.get_ns Optional parameters +--- • winid: (number) `window-ID` for retrieving a window's +--- highlight namespace. A value of -1 is returned when +--- `nvim_win_set_hl_ns()` has not been called for the window +--- (or was called with a namespace of -1). +--- @return integer +function vim.api.nvim_get_hl_ns(opts) end + --- Gets a list of global (non-buffer-local) `mapping` definitions. --- --- @param mode string Mode short-name ("n", "i", "v", ...) diff --git a/runtime/lua/vim/_meta/api_keysets.lua b/runtime/lua/vim/_meta/api_keysets.lua index eaaa32d7b3..88de2f58d0 100644 --- a/runtime/lua/vim/_meta/api_keysets.lua +++ b/runtime/lua/vim/_meta/api_keysets.lua @@ -136,6 +136,9 @@ error('Cannot require a meta file') --- @field link? boolean --- @field create? boolean +--- @class vim.api.keyset.get_ns +--- @field winid? integer + --- @class vim.api.keyset.highlight --- @field bold? boolean --- @field standout? boolean diff --git a/src/nvim/api/keysets.h b/src/nvim/api/keysets.h index 236e75983e..5962dec3ec 100644 --- a/src/nvim/api/keysets.h +++ b/src/nvim/api/keysets.h @@ -195,6 +195,11 @@ typedef struct { Boolean create; } Dict(get_highlight); +typedef struct { + OptionalKeys is_set__get_ns_; + Window winid; +} Dict(get_ns); + typedef struct { OptionalKeys is_set__win_text_height_; Integer start_row; diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index c55f9592bf..6a38374ade 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -170,6 +170,29 @@ void nvim_set_hl(Integer ns_id, String name, Dict(highlight) *val, Error *err) } } +/// Gets the active highlight namespace. +/// +/// @param opts Optional parameters +/// - winid: (number) |window-ID| for retrieving a window's highlight +/// namespace. A value of -1 is returned when |nvim_win_set_hl_ns()| +/// has not been called for the window (or was called with a namespace +/// of -1). +/// @param[out] err Error details, if any +/// @return Namespace id, or -1 +Integer nvim_get_hl_ns(Dict(get_ns) *opts, Error *err) + FUNC_API_SINCE(12) +{ + if (HAS_KEY(opts, get_ns, winid)) { + win_T *win = find_window_by_handle(opts->winid, err); + if (!win) { + return 0; + } + return win->w_ns_hl; + } else { + return ns_hl_global; + } +} + /// Set active namespace for highlights defined with |nvim_set_hl()|. This can be set for /// a single window, see |nvim_win_set_hl_ns()|. /// diff --git a/test/functional/api/highlight_spec.lua b/test/functional/api/highlight_spec.lua index 1a054741ef..7d7d07e30e 100644 --- a/test/functional/api/highlight_spec.lua +++ b/test/functional/api/highlight_spec.lua @@ -637,3 +637,19 @@ describe('API: get highlight', function() eq({link ='Foo', default = true}, meths.get_hl(0, {name = 'Bar'})) end) end) + +describe('API: set/get highlight namespace', function() + it('set/get highlight namespace', function() + eq(0, meths.get_hl_ns({})) + local ns = meths.create_namespace('') + meths.set_hl_ns(ns) + eq(ns, meths.get_hl_ns({})) + end) + + it('set/get window highlight namespace', function() + eq(-1, meths.get_hl_ns({winid = 0})) + local ns = meths.create_namespace('') + meths.win_set_hl_ns(0, ns) + eq(ns, meths.get_hl_ns({winid = 0})) + end) +end)