api: add nvim_win_call

This commit is contained in:
notomo
2021-06-04 09:47:43 +09:00
committed by Björn Linse
parent fd7e77b227
commit 1a9eb7a987
2 changed files with 65 additions and 0 deletions

View File

@@ -11,6 +11,7 @@
#include "nvim/api/window.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/lua/executor.h"
#include "nvim/ex_docmd.h"
#include "nvim/vim.h"
#include "nvim/buffer.h"
@@ -524,3 +525,39 @@ void nvim_win_close(Window window, Boolean force, Error *err)
ex_win_close(force, win, tabpage == curtab ? NULL : tabpage);
vim_ignored = try_leave(&tstate, err);
}
/// Calls a function with window as temporary current window.
///
/// @see |win_execute()|
/// @see |nvim_buf_call()|
///
/// @param window Window handle, or 0 for current window
/// @param fun Function to call inside the window (currently lua callable
/// only)
/// @param[out] err Error details, if any
/// @return Return value of function. NB: will deepcopy lua values
/// currently, use upvalues to send lua references in and out.
Object nvim_win_call(Window window, LuaRef fun, Error *err)
FUNC_API_SINCE(7)
FUNC_API_LUA_ONLY
{
win_T *win = find_window_by_handle(window, err);
if (!win) {
return NIL;
}
tabpage_T *tabpage = win_find_tabpage(win);
win_T *save_curwin;
tabpage_T *save_curtab;
try_start();
Object res = OBJECT_INIT;
if (switch_win_noblock(&save_curwin, &save_curtab, win, tabpage, true) ==
OK) {
Array args = ARRAY_DICT_INIT;
res = nlua_call_ref(fun, NULL, args, true, err);
}
restore_win_noblock(save_curwin, save_curtab, true);
try_end(err);
return res;
}