diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2019-06-03 13:08:05 +0200 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2019-06-03 13:54:03 +0200 |
commit | 7ac3c311eebe56f0af8d8b4385fae7ab99c888ac (patch) | |
tree | d9cf34d337b3aeaf179a502d9f952719ed61514a /src/nvim/api/vim.c | |
parent | 3273e39db62987ae8bb0c617f70c15d3d7bbab68 (diff) | |
download | rneovim-7ac3c311eebe56f0af8d8b4385fae7ab99c888ac.tar.gz rneovim-7ac3c311eebe56f0af8d8b4385fae7ab99c888ac.tar.bz2 rneovim-7ac3c311eebe56f0af8d8b4385fae7ab99c888ac.zip |
api/buffer: create new buffers in the "opened" state
Otherwise vim will think that ml_append() needs to "enter" the buffer,
which emits unexpected autocommands.
ref https://github.com/vim-airline/vim-airline/issues/1930
Diffstat (limited to 'src/nvim/api/vim.c')
-rw-r--r-- | src/nvim/api/vim.c | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 9b5e0fc40b..2e8ca384b4 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -27,6 +27,7 @@ #include "nvim/types.h" #include "nvim/ex_docmd.h" #include "nvim/screen.h" +#include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/edit.h" @@ -977,11 +978,20 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err) BLN_NOOPT | BLN_NEW | (listed ? BLN_LISTED : 0)); try_end(err); if (buf == NULL) { - if (!ERROR_SET(err)) { - api_set_error(err, kErrorTypeException, "Failed to create buffer"); - } - return 0; + goto fail; } + + // Open the memline for the buffer. This will avoid spurious autocmds when + // a later nvim_buf_set_lines call would have needed to "open" the buffer. + try_start(); + block_autocmds(); + int status = ml_open(buf); + unblock_autocmds(); + try_end(err); + if (status == FAIL) { + goto fail; + } + if (scratch) { aco_save_T aco; aucmd_prepbuf(&aco, buf); @@ -991,6 +1001,12 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err) aucmd_restbuf(&aco); } return buf->b_fnum; + +fail: + if (!ERROR_SET(err)) { + api_set_error(err, kErrorTypeException, "Failed to create buffer"); + } + return 0; } /// Open a new window. |