api(nvim_open_win): add "noautocmd" option

This option, when set, stops nvim_open_win() from potentially firing
buffer-related autocmd events
(BufEnter, BufLeave and BufWinEnter in the case of nvim_open_win()).
This commit is contained in:
Sean Dewar 2021-05-27 22:10:42 +01:00
parent 27c616d688
commit 802f8429d5
No known key found for this signature in database
GPG Key ID: 08CC2C83AD41B581
6 changed files with 71 additions and 35 deletions

View File

@ -1645,7 +1645,7 @@ bool api_object_to_bool(Object obj, const char *what,
} else if (obj.type == kObjectTypeNil) {
return nil_value; // caller decides what NIL (missing retval in lua) means
} else {
api_set_error(err, kErrorTypeValidation, "%s is not an boolean", what);
api_set_error(err, kErrorTypeValidation, "%s is not a boolean", what);
return false;
}
}
@ -1868,7 +1868,7 @@ static void parse_border_style(Object style, FloatConfig *fconfig, Error *err)
}
bool parse_float_config(Dictionary config, FloatConfig *fconfig, bool reconf,
Error *err)
bool new_win, Error *err)
{
// TODO(bfredl): use a get/has_key interface instead and get rid of extra
// flags
@ -2015,6 +2015,12 @@ bool parse_float_config(Dictionary config, FloatConfig *fconfig, bool reconf,
api_set_error(err, kErrorTypeValidation,
"Invalid value of 'style' key");
}
} else if (strequal(key, "noautocmd") && new_win) {
fconfig->noautocmd
= api_object_to_bool(val, "'noautocmd' key", false, err);
if (ERROR_SET(err)) {
return false;
}
} else {
api_set_error(err, kErrorTypeValidation,
"Invalid key '%s'", key);

View File

@ -1459,6 +1459,9 @@ void nvim_chan_send(Integer chan, String data, Error *err)
/// By default `FloatBorder` highlight is used which links to `VertSplit`
/// when not defined. It could also be specified by character:
/// [ {"+", "MyCorner"}, {"x", "MyBorder"} ]
/// - `noautocmd`: If true then no buffer-related autocommand events such as
/// |BufEnter|, |BufLeave| or |BufWinEnter| may fire from
/// calling this function.
///
/// @param[out] err Error details, if any
///
@ -1469,7 +1472,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dictionary config,
FUNC_API_CHECK_TEXTLOCK
{
FloatConfig fconfig = FLOAT_CONFIG_INIT;
if (!parse_float_config(config, &fconfig, false, err)) {
if (!parse_float_config(config, &fconfig, false, true, err)) {
return 0;
}
win_T *wp = win_new_float(NULL, fconfig, err);
@ -1484,7 +1487,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dictionary config,
return 0;
}
if (buffer > 0) {
nvim_win_set_buf(wp->handle, buffer, err);
win_set_buf(wp->handle, buffer, fconfig.noautocmd, err);
}
if (fconfig.style == kWinStyleMinimal) {

View File

@ -46,35 +46,7 @@ void nvim_win_set_buf(Window window, Buffer buffer, Error *err)
FUNC_API_SINCE(5)
FUNC_API_CHECK_TEXTLOCK
{
win_T *win = find_window_by_handle(window, err), *save_curwin = curwin;
buf_T *buf = find_buffer_by_handle(buffer, err);
tabpage_T *tab = win_find_tabpage(win), *save_curtab = curtab;
if (!win || !buf) {
return;
}
if (switch_win(&save_curwin, &save_curtab, win, tab, false) == FAIL) {
api_set_error(err,
kErrorTypeException,
"Failed to switch to window %d",
window);
}
try_start();
int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
if (!try_end(err) && result == FAIL) {
api_set_error(err,
kErrorTypeException,
"Failed to set buffer %d",
buffer);
}
// If window is not current, state logic will not validate its cursor.
// So do it now.
validate_cursor();
restore_win(save_curwin, save_curtab, false);
win_set_buf(window, buffer, false, err);
}
/// Gets the (1,0)-indexed cursor position in the window. |api-indexing|
@ -423,7 +395,7 @@ void nvim_win_set_config(Window window, Dictionary config, Error *err)
// reuse old values, if not overriden
FloatConfig fconfig = new_float ? FLOAT_CONFIG_INIT : win->w_float_config;
if (!parse_float_config(config, &fconfig, !new_float, err)) {
if (!parse_float_config(config, &fconfig, !new_float, false, err)) {
return;
}
if (new_float) {

View File

@ -1090,6 +1090,7 @@ typedef struct {
schar_T border_chars[8];
int border_hl_ids[8];
int border_attr[8];
bool noautocmd;
} FloatConfig;
#define FLOAT_CONFIG_INIT ((FloatConfig){ .height = 0, .width = 0, \
@ -1098,7 +1099,8 @@ typedef struct {
.relative = 0, .external = false, \
.focusable = true, \
.zindex = kZIndexFloatDefault, \
.style = kWinStyleUnused })
.style = kWinStyleUnused, \
.noautocmd = false })
// Structure to store last cursor position and topline. Used by check_lnums()
// and reset_lnums().

View File

@ -570,6 +570,45 @@ static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize,
}
}
void win_set_buf(Window window, Buffer buffer, bool noautocmd, Error *err)
{
win_T *win = find_window_by_handle(window, err), *save_curwin = curwin;
buf_T *buf = find_buffer_by_handle(buffer, err);
tabpage_T *tab = win_find_tabpage(win), *save_curtab = curtab;
if (!win || !buf) {
return;
}
if (noautocmd) {
block_autocmds();
}
if (switch_win_noblock(&save_curwin, &save_curtab, win, tab, false) == FAIL) {
api_set_error(err,
kErrorTypeException,
"Failed to switch to window %d",
window);
}
try_start();
int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
if (!try_end(err) && result == FAIL) {
api_set_error(err,
kErrorTypeException,
"Failed to set buffer %d",
buffer);
}
// If window is not current, state logic will not validate its cursor.
// So do it now.
validate_cursor();
restore_win_noblock(save_curwin, save_curtab, false);
if (noautocmd) {
unblock_autocmds();
}
}
/// Create a new float.
///
/// if wp == NULL allocate a new window, otherwise turn existing window into a

View File

@ -387,4 +387,18 @@ describe('API/win', function()
eq({oldbuf}, meths.list_bufs())
end)
end)
describe('open_win', function()
it('noautocmd option works', function()
command('autocmd BufEnter,BufLeave,BufWinEnter * let g:fired = 1')
meths.open_win(meths.create_buf(true, true), true, {
relative='win', row=3, col=3, width=12, height=3, noautocmd=true
})
eq(0, funcs.exists('g:fired'))
meths.open_win(meths.create_buf(true, true), true, {
relative='win', row=3, col=3, width=12, height=3
})
eq(1, funcs.exists('g:fired'))
end)
end)
end)