diff options
author | Bram Moolenaar <Bram@vim.org> | 2019-06-30 20:21:37 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2019-06-30 20:21:37 +0200 |
commit | 15e248e37f3925d430f96e945d52d3dc423cdc83 (patch) | |
tree | bd29a54010b6ff38816f4ca523f30e4ba20d61a0 /src | |
parent | 2e62b568e91c36adb16dbcc609665170f09f3845 (diff) | |
download | vim-git-15e248e37f3925d430f96e945d52d3dc423cdc83.tar.gz |
patch 8.1.1610: there is no way to add or load a buffer without side effectsv8.1.1610
Problem: There is no way to add or load a buffer without side effects.
Solution: Add the bufadd() and bufload() functions.
Diffstat (limited to 'src')
-rw-r--r-- | src/evalfunc.c | 32 | ||||
-rw-r--r-- | src/testdir/test_functions.vim | 28 | ||||
-rw-r--r-- | src/version.c | 2 |
3 files changed, 62 insertions, 0 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c index b24f55b31..954ea9fe9 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -71,8 +71,10 @@ static void f_balloon_split(typval_T *argvars, typval_T *rettv); #endif static void f_browse(typval_T *argvars, typval_T *rettv); static void f_browsedir(typval_T *argvars, typval_T *rettv); +static void f_bufadd(typval_T *argvars, typval_T *rettv); static void f_bufexists(typval_T *argvars, typval_T *rettv); static void f_buflisted(typval_T *argvars, typval_T *rettv); +static void f_bufload(typval_T *argvars, typval_T *rettv); static void f_bufloaded(typval_T *argvars, typval_T *rettv); static void f_bufname(typval_T *argvars, typval_T *rettv); static void f_bufnr(typval_T *argvars, typval_T *rettv); @@ -526,11 +528,13 @@ static struct fst #endif {"browse", 4, 4, f_browse}, {"browsedir", 2, 2, f_browsedir}, + {"bufadd", 1, 1, f_bufadd}, {"bufexists", 1, 1, f_bufexists}, {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */ {"buffer_name", 1, 1, f_bufname}, /* obsolete */ {"buffer_number", 1, 1, f_bufnr}, /* obsolete */ {"buflisted", 1, 1, f_buflisted}, + {"bufload", 1, 1, f_bufload}, {"bufloaded", 1, 1, f_bufloaded}, {"bufname", 1, 1, f_bufname}, {"bufnr", 1, 2, f_bufnr}, @@ -1920,6 +1924,15 @@ find_buffer(typval_T *avar) } /* + * "bufadd(expr)" function + */ + static void +f_bufadd(typval_T *argvars, typval_T *rettv) +{ + rettv->vval.v_number = buflist_add(tv_get_string(&argvars[0]), 0); +} + +/* * "bufexists(expr)" function */ static void @@ -1941,6 +1954,25 @@ f_buflisted(typval_T *argvars, typval_T *rettv) } /* + * "bufload(expr)" function + */ + static void +f_bufload(typval_T *argvars, typval_T *rettv UNUSED) +{ + buf_T *buf = get_buf_arg(&argvars[0]); + + if (buf != NULL && buf->b_ml.ml_mfp == NULL) + { + aco_save_T aco; + + aucmd_prepbuf(&aco, buf); + swap_exists_action = SEA_NONE; + open_buffer(FALSE, NULL, 0); + aucmd_restbuf(&aco); + } +} + +/* * "bufloaded(expr)" function */ static void diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim index cd3f253bb..34a4bbf62 100644 --- a/src/testdir/test_functions.vim +++ b/src/testdir/test_functions.vim @@ -1515,3 +1515,31 @@ endfunc func Test_eventhandler() call assert_equal(0, eventhandler()) endfunc + +func Test_bufadd_bufload() + call assert_equal(0, bufexists('someName')) + let buf = bufadd('someName') + call assert_notequal(0, buf) + call assert_equal(1, bufexists('someName')) + call assert_equal(0, getbufvar(buf, '&buflisted')) + call assert_equal(0, bufloaded(buf)) + call bufload(buf) + call assert_equal(1, bufloaded(buf)) + call assert_equal([''], getbufline(buf, 1, '$')) + + let curbuf = bufnr('') + call writefile(['some', 'text'], 'otherName') + let buf = bufadd('otherName') + call assert_notequal(0, buf) + call assert_equal(1, bufexists('otherName')) + call assert_equal(0, getbufvar(buf, '&buflisted')) + call assert_equal(0, bufloaded(buf)) + call bufload(buf) + call assert_equal(1, bufloaded(buf)) + call assert_equal(['some', 'text'], getbufline(buf, 1, '$')) + call assert_equal(curbuf, bufnr('')) + + bwipe someName + bwipe otherName + call assert_equal(0, bufexists('someName')) +endfunc diff --git a/src/version.c b/src/version.c index 89b7f4f42..6fce98b1f 100644 --- a/src/version.c +++ b/src/version.c @@ -778,6 +778,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1610, +/**/ 1609, /**/ 1608, |