diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2017-09-02 14:21:06 +0200 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2019-02-09 11:48:45 +0100 |
commit | 891ed14f13c3ea99d3fb985371c69f769ee7ff57 (patch) | |
tree | 6e48660879a7f88178fd082d6c555fa64bec6102 /src/nvim/api | |
parent | f6faeea41c70015f6d9b63fecaf80d106cd2636d (diff) | |
download | rneovim-891ed14f13c3ea99d3fb985371c69f769ee7ff57.tar.gz rneovim-891ed14f13c3ea99d3fb985371c69f769ee7ff57.tar.bz2 rneovim-891ed14f13c3ea99d3fb985371c69f769ee7ff57.zip |
api: add nvim_create_buf to create a new empty buffer.
Loading existing files into a buffer is non-trivial and requires a window.
Creating an unnamed emtpy buffer is trivial and safe though, thus worth a
special case.
Change nvim_buf_set_option to use aucmd_prepbuf. This is necessary
to allow some options to be set on a not yet displayed buffer, such
as 'buftype' option.
vim-patch:7.4.1858: Add BLN_NEW to enforce buflist_new creating new buffer
Diffstat (limited to 'src/nvim/api')
-rw-r--r-- | src/nvim/api/private/helpers.c | 7 | ||||
-rw-r--r-- | src/nvim/api/vim.c | 19 |
2 files changed, 23 insertions, 3 deletions
diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 82c9a1da67..f5cac82315 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -27,6 +27,7 @@ #include "nvim/version.h" #include "nvim/lib/kvec.h" #include "nvim/getchar.h" +#include "nvim/fileio.h" #include "nvim/ui.h" /// Helper structure for vim_to_object @@ -1094,7 +1095,7 @@ static void set_option_value_for(char *key, { win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; - bufref_T save_curbuf = { NULL, 0, 0 }; + aco_save_T aco; try_start(); switch (opt_type) @@ -1115,9 +1116,9 @@ static void set_option_value_for(char *key, restore_win(save_curwin, save_curtab, true); break; case SREQ_BUF: - switch_buffer(&save_curbuf, (buf_T *)from); + aucmd_prepbuf(&aco, (buf_T *)from); set_option_value_err(key, numval, stringval, opt_flags, err); - restore_buffer(&save_curbuf); + aucmd_restbuf(&aco); break; case SREQ_GLOBAL: set_option_value_err(key, numval, stringval, opt_flags, err); diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 5b59ff39f4..86e243c4f0 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -955,6 +955,25 @@ void nvim_set_current_win(Window window, Error *err) } } +/// Create new empty buffer +/// +/// @param listed whether the buffer should be listed +/// @param[out] err Error details, if any +/// @return the buffer handle or 0 when error +Buffer nvim_create_buf(Boolean listed, Error *err) + FUNC_API_SINCE(6) +{ + try_start(); + Buffer buffer = buflist_add(NULL, + BLN_NOOPT | BLN_NEW | (listed ? BLN_LISTED : 0)); + if (!try_end(err) && buffer == 0) { + api_set_error(err, + kErrorTypeException, + "Failed to create buffer"); + } + return buffer; +} + /// Gets the current list of tabpage handles. /// /// @return List of tabpage handles |