diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2021-03-28 19:07:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-28 19:07:56 +0200 |
commit | 6d4a922e07e857b46d882ea96decce7c7c6e2a30 (patch) | |
tree | 04440e96a40cf8e0271d6ee8a20bfb1bc008394f | |
parent | 2ed5a776028f50177526a3fe0d7650282316195d (diff) | |
parent | 45236981d887e3a535adc0fc1e91cadce54c9463 (diff) | |
download | rneovim-6d4a922e07e857b46d882ea96decce7c7c6e2a30.tar.gz rneovim-6d4a922e07e857b46d882ea96decce7c7c6e2a30.tar.bz2 rneovim-6d4a922e07e857b46d882ea96decce7c7c6e2a30.zip |
Merge pull request #14091 from euclidianAce/euclidianAce/nvim_win_hide
api: add vim.api.nvim_win_hide
-rw-r--r-- | runtime/doc/api.txt | 15 | ||||
-rw-r--r-- | src/nvim/api/window.c | 29 | ||||
-rw-r--r-- | test/functional/api/window_spec.lua | 40 |
3 files changed, 84 insertions, 0 deletions
diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index c72381fd06..1e287281cf 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -2518,6 +2518,21 @@ nvim_win_get_width({window}) *nvim_win_get_width()* Return: ~ Width as a count of columns +nvim_win_hide({window}) *nvim_win_hide()* + Closes the window and hide the buffer it contains (like + |:hide| with a |window-ID|). + + Like |:hide| the buffer becomes hidden unless another window + is editing it, or 'bufhidden' is `unload` , `delete` or `wipe` + as opposed to |:close| or |nvim_win_close|, which will close + the buffer. + + Attributes: ~ + not allowed when |textlock| is active + + Parameters: ~ + {window} Window handle, or 0 for current window + nvim_win_is_valid({window}) *nvim_win_is_valid()* Checks if a window is valid diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index f4af1632ec..89fa2f86fb 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -492,6 +492,35 @@ Dictionary nvim_win_get_config(Window window, Error *err) return rv; } +/// Closes the window and hide the buffer it contains (like |:hide| with a +/// |window-ID|). +/// +/// Like |:hide| the buffer becomes hidden unless another window is editing it, +/// or 'bufhidden' is `unload`, `delete` or `wipe` as opposed to |:close| or +/// |nvim_win_close|, which will close the buffer. +/// +/// @param window Window handle, or 0 for current window +/// @param[out] err Error details, if any +void nvim_win_hide(Window window, Error *err) + FUNC_API_SINCE(7) + FUNC_API_CHECK_TEXTLOCK +{ + win_T *win = find_window_by_handle(window, err); + if (!win) { + return; + } + + tabpage_T *tabpage = win_find_tabpage(win); + TryState tstate; + try_enter(&tstate); + if (tabpage == curtab) { + win_close(win, false); + } else { + win_close_othertab(win, false, tabpage); + } + vim_ignored = try_leave(&tstate, err); +} + /// Closes the window (like |:close| with a |window-ID|). /// /// @param window Window handle, or 0 for current window diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index 7471f50dbd..ceeb84cec9 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -347,4 +347,44 @@ describe('API/win', function() eq('', funcs.getcmdwintype()) end) end) + + describe('hide', function() + it('can hide current window', function() + local oldwin = meths.get_current_win() + command('split') + local newwin = meths.get_current_win() + meths.win_hide(newwin) + eq({oldwin}, meths.list_wins()) + end) + it('can hide noncurrent window', function() + local oldwin = meths.get_current_win() + command('split') + local newwin = meths.get_current_win() + meths.win_hide(oldwin) + eq({newwin}, meths.list_wins()) + end) + it('does not close the buffer', function() + local oldwin = meths.get_current_win() + local oldbuf = meths.get_current_buf() + local buf = meths.create_buf(true, false) + local newwin = meths.open_win(buf, true, { + relative='win', row=3, col=3, width=12, height=3 + }) + meths.win_hide(newwin) + eq({oldwin}, meths.list_wins()) + eq({oldbuf, buf}, meths.list_bufs()) + end) + it('deletes the buffer when bufhidden=wipe', function() + local oldwin = meths.get_current_win() + local oldbuf = meths.get_current_buf() + local buf = meths.create_buf(true, false) + local newwin = meths.open_win(buf, true, { + relative='win', row=3, col=3, width=12, height=3 + }) + meths.buf_set_option(buf, 'bufhidden', 'wipe') + meths.win_hide(newwin) + eq({oldwin}, meths.list_wins()) + eq({oldbuf}, meths.list_bufs()) + end) + end) end) |