diff options
author | Sean Dewar <seandewar@users.noreply.github.com> | 2021-04-14 20:13:22 +0100 |
---|---|---|
committer | Sean Dewar <seandewar@users.noreply.github.com> | 2021-04-21 14:55:04 +0100 |
commit | 65f35e0c7db700c25d9de9a3c8637f720c07583a (patch) | |
tree | 57ac945230ad1271c337d38256147e5f2e67bd33 | |
parent | 805eb81ccdc6a7000926e458b6e0dd59df16a112 (diff) | |
download | rneovim-65f35e0c7db700c25d9de9a3c8637f720c07583a.tar.gz rneovim-65f35e0c7db700c25d9de9a3c8637f720c07583a.tar.bz2 rneovim-65f35e0c7db700c25d9de9a3c8637f720c07583a.zip |
vim-patch:8.2.1588: cannot read back the prompt of a prompt buffer
Problem: Cannot read back the prompt of a prompt buffer.
Solution: Add prompt_getprompt(). (Ben Jackson, closes vim/vim#6851)
https://github.com/vim/vim/commit/077cc7aa0e0c431e97795612374fe17fe7c88803
Updated prompt_getprompt() doc to https://github.com/vim/vim/commit/cb80aa2d53e56d3aba3b3c439fb467f29a750c5e
and removed mention of method syntax usage (not supported by Nvim).
-rw-r--r-- | runtime/doc/eval.txt | 8 | ||||
-rw-r--r-- | runtime/doc/usr_41.txt | 1 | ||||
-rw-r--r-- | src/nvim/edit.c | 17 | ||||
-rw-r--r-- | src/nvim/eval.lua | 1 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 20 | ||||
-rw-r--r-- | src/nvim/testdir/test_prompt_buffer.vim | 36 |
6 files changed, 78 insertions, 5 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index cff87b2fed..b8dcfd0ff4 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -2309,6 +2309,7 @@ perleval({expr}) any evaluate |perl| expression pow({x}, {y}) Float {x} to the power of {y} prevnonblank({lnum}) Number line nr of non-blank line <= {lnum} printf({fmt}, {expr1}...) String format text +prompt_getprompt({buf}) String get prompt text prompt_setcallback({buf}, {expr}) none set prompt callback function prompt_setinterrupt({buf}, {text}) none set prompt interrupt function prompt_setprompt({buf}, {text}) none set prompt text @@ -6746,6 +6747,13 @@ printf({fmt}, {expr1} ...) *printf()* of "%" items. If there are not sufficient or too many arguments an error is given. Up to 18 arguments can be used. +prompt_getprompt({buf}) *prompt_getprompt()* + Returns the effective prompt text for buffer {buf}. {buf} can + be a buffer name or number. See |prompt-buffer|. + + If the buffer doesn't exist or isn't a prompt buffer, an empty + string is returned. + prompt_setcallback({buf}, {expr}) *prompt_setcallback()* Set prompt callback for buffer {buf} to {expr}. When {expr} is an empty string the callback is removed. This has only diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index 21f5dcc815..41948f577e 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -978,6 +978,7 @@ Tags: *tag-functions* settagstack() modify the tag stack of a window Prompt Buffer: *promptbuffer-functions* + prompt_getprompt() get the effective prompt text for a buffer prompt_setcallback() set prompt callback for a buffer prompt_setinterrupt() set interrupt callback for a buffer prompt_setprompt() set the prompt text for a buffer diff --git a/src/nvim/edit.c b/src/nvim/edit.c index f2fddc89fe..999cc74185 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -1604,13 +1604,20 @@ void edit_putchar(int c, bool highlight) } } +/// Return the effective prompt for the specified buffer. +char_u *buf_prompt_text(const buf_T *const buf) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +{ + if (buf->b_prompt_text == NULL) { + return (char_u *)"% "; + } + return buf->b_prompt_text; +} + // Return the effective prompt for the current buffer. -char_u *prompt_text(void) +char_u *prompt_text(void) FUNC_ATTR_WARN_UNUSED_RESULT { - if (curbuf->b_prompt_text == NULL) { - return (char_u *)"% "; - } - return curbuf->b_prompt_text; + return buf_prompt_text(curbuf); } // Prepare for prompt mode: Make sure the last line has the prompt text. diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index b10e99fc08..77e7c7b3a9 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -251,6 +251,7 @@ return { pow={args=2}, prevnonblank={args=1}, printf={args=varargs(1)}, + prompt_getprompt={args=1}, prompt_setcallback={args={2, 2}}, prompt_setinterrupt={args={2, 2}}, prompt_setprompt={args={2, 2}}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 206f76ba8e..6d328953f6 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -6491,6 +6491,26 @@ static void f_prompt_setinterrupt(typval_T *argvars, buf->b_prompt_interrupt= interrupt_callback; } +/// "prompt_getprompt({buffer})" function +void f_prompt_getprompt(typval_T *argvars, typval_T *rettv, FunPtr fptr) + FUNC_ATTR_NONNULL_ALL +{ + // return an empty string by default, e.g. it's not a prompt buffer + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + + buf_T *const buf = tv_get_buf_from_arg(&argvars[0]); + if (buf == NULL) { + return; + } + + if (!bt_prompt(buf)) { + return; + } + + rettv->vval.v_string = vim_strsave(buf_prompt_text(buf)); +} + // "prompt_setprompt({buffer}, {text})" function static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv, FunPtr fptr) diff --git a/src/nvim/testdir/test_prompt_buffer.vim b/src/nvim/testdir/test_prompt_buffer.vim index 66647018b5..6fc5850be3 100644 --- a/src/nvim/testdir/test_prompt_buffer.vim +++ b/src/nvim/testdir/test_prompt_buffer.vim @@ -156,4 +156,40 @@ func Test_prompt_buffer_edit() call assert_equal(0, prompt_setprompt([], '')) endfunc +func Test_prompt_buffer_getbufinfo() + new + call assert_equal('', prompt_getprompt('%')) + call assert_equal('', prompt_getprompt(bufnr('%'))) + let another_buffer = bufnr('%') + + set buftype=prompt + call assert_equal('% ', prompt_getprompt('%')) + call prompt_setprompt( bufnr( '%' ), 'This is a test: ' ) + call assert_equal('This is a test: ', prompt_getprompt('%')) + + call prompt_setprompt( bufnr( '%' ), '' ) + " Nvim doesn't support method call syntax yet. + " call assert_equal('', '%'->prompt_getprompt()) + call assert_equal('', prompt_getprompt('%')) + + call prompt_setprompt( bufnr( '%' ), 'Another: ' ) + call assert_equal('Another: ', prompt_getprompt('%')) + let another = bufnr('%') + + new + + call assert_equal('', prompt_getprompt('%')) + call assert_equal('Another: ', prompt_getprompt(another)) + + " Doesn't exist + let buffers_before = len( getbufinfo() ) + call assert_equal('', prompt_getprompt( bufnr('$') + 1)) + call assert_equal(buffers_before, len( getbufinfo())) + + " invalid type + call assert_fails('call prompt_getprompt({})', 'E728:') + + %bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab |