api: add nvim_win_close() to close window by id

This commit is contained in:
Björn Linse 2019-03-03 10:26:11 +01:00
parent 7a6da502b9
commit c8f310825c
4 changed files with 96 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include "nvim/api/window.h" #include "nvim/api/window.h"
#include "nvim/api/private/defs.h" #include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/ex_docmd.h"
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/buffer.h" #include "nvim/buffer.h"
#include "nvim/cursor.h" #include "nvim/cursor.h"
@ -470,3 +471,29 @@ void nvim_win_config(Window window, Integer width, Integer height,
win->w_pos_changed = true; win->w_pos_changed = true;
} }
} }
/// Close a window.
///
/// This is equivalent to |:close| with count except that it takes a window id.
///
/// @param window Window handle
/// @param force Behave like `:close!` The last window of a buffer with
/// unwritten changes can be closed. The buffer will become
/// hidden, even if 'hidden' is not set.
///
/// @param[out] err Error details, if any
/// @return Window number
void nvim_win_close(Window window, Boolean force, Error *err)
FUNC_API_SINCE(6)
{
win_T *win = find_window_by_handle(window, err);
if (!win) {
return;
}
tabpage_T *tabpage = win_find_tabpage(win);
TryState tstate;
try_enter(&tstate);
ex_win_close(force, win, tabpage == curtab ? NULL : tabpage);
vim_ignored = try_leave(&tstate, err);
}

View File

@ -6170,7 +6170,7 @@ static void ex_pclose(exarg_T *eap)
* Close window "win" and take care of handling closing the last window for a * Close window "win" and take care of handling closing the last window for a
* modified buffer. * modified buffer.
*/ */
static void void
ex_win_close( ex_win_close(
int forceit, int forceit,
win_T *win, win_T *win,

View File

@ -286,4 +286,41 @@ describe('API/win', function()
ok(not window('is_valid', win)) ok(not window('is_valid', win))
end) end)
end) end)
describe('close', function()
it('can close current window', function()
local oldwin = meths.get_current_win()
command('split')
local newwin = meths.get_current_win()
meths.win_close(newwin,false)
eq({oldwin}, meths.list_wins())
end)
it('can close noncurrent window', function()
local oldwin = meths.get_current_win()
command('split')
local newwin = meths.get_current_win()
meths.win_close(oldwin,false)
eq({newwin}, meths.list_wins())
end)
it('handles changed buffer', function()
local oldwin = meths.get_current_win()
insert('text')
command('new')
local newwin = meths.get_current_win()
eq({false,"Vim:E37: No write since last change (add ! to override)"},
meth_pcall(meths.win_close, oldwin,false))
eq({newwin,oldwin}, meths.list_wins())
end)
it('handles changed buffer with force', function()
local oldwin = meths.get_current_win()
insert('text')
command('new')
local newwin = meths.get_current_win()
meths.win_close(oldwin,true)
eq({newwin}, meths.list_wins())
end)
end)
end) end)

View File

@ -118,6 +118,37 @@ describe('floating windows', function()
| |
]]) ]])
end end
meths.win_close(win, false)
if multigrid then
screen:expect([[
## grid 1
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
|
## grid 2
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
]])
else
screen:expect([[
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
end
end) end)
it('API has proper error messages', function() it('API has proper error messages', function()