]> granicus.if.org Git - vim/commitdiff
patch 8.2.1588: cannot read back the prompt of a prompt buffer v8.2.1588
authorBram Moolenaar <Bram@vim.org>
Fri, 4 Sep 2020 14:35:35 +0000 (16:35 +0200)
committerBram Moolenaar <Bram@vim.org>
Fri, 4 Sep 2020 14:35:35 +0000 (16:35 +0200)
Problem:    Cannot read back the prompt of a prompt buffer.
Solution:   Add prompt_getprompt(). (Ben Jackson, closes #6851)

runtime/doc/channel.txt
runtime/doc/eval.txt
runtime/doc/usr_41.txt
src/channel.c
src/edit.c
src/evalfunc.c
src/proto/channel.pro
src/proto/edit.pro
src/testdir/test_prompt_buffer.vim
src/version.c

index 84a344fa957c06eaae96d9c948d38d82a05d39fb..18b0232dfac9826a62acdd223011ecf6059073eb 100644 (file)
@@ -1,4 +1,4 @@
-*channel.txt*      For Vim version 8.2.  Last change: 2020 Jul 10
+*channel.txt*      For Vim version 8.2.  Last change: 2020 Sep 03
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -608,6 +608,10 @@ ch_logfile({fname} [, {mode}])                                     *ch_logfile()*
                after every message, on Unix you can use "tail -f" to see what
                is going on in real time.
 
+               To enable the log very early, to see what is received from a
+               terminal during startup, use |--cmd|: >
+                       vim --cmd "call ch_logfile('logfile', 'w')"
+<
                This function is not available in the |sandbox|.
                NOTE: the channel communication is stored in the file, be
                aware that this may contain confidential and privacy sensitive
@@ -1256,7 +1260,9 @@ After setting 'buftype' to "prompt" Vim does not automatically start Insert
 mode, use `:startinsert` if you want to enter Insert mode, so that the user
 can start typing a line.
 
-The text of the prompt can be set with the |prompt_setprompt()| function.
+The text of the prompt can be set with the |prompt_setprompt()| function. If
+no prompt is set with |prompt_setprompt()|, "% " is used. You can get the
+effective prompt text for a buffer, with |prompt_getprompt()|.
 
 The user can go to Normal mode and navigate through the buffer.  This can be
 useful to see older output or copy text.
index a2e1af919f988b898e01f8af17f3d6e6763928b1..130e6423b9e22147068fc20b7fc14ad8b360539f 100644 (file)
@@ -2684,6 +2684,7 @@ popup_show({id})          none    unhide popup window {id}
 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
@@ -7835,6 +7836,17 @@ printf({fmt}, {expr1} ...)                               *printf()*
                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. |prompt-buffer|.
+
+               If the buffer doesn't exist or isn't a prompt buffer, an empty
+               string is returned.
+
+               Can also be used as a |method|: >
+                       GetBuffer()->prompt_getprompt()
+
+
 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
@@ -7890,7 +7902,7 @@ prompt_setprompt({buf}, {text})                           *prompt_setprompt()*
                Can also be used as a |method|: >
                        GetBuffer()->prompt_setprompt('command: ')
 
-prop_ functions are documented here: |text-prop-functions|.
+prop_ functions are documented here: |text-prop-functions|
 
 pum_getpos()                                           *pum_getpos()*
                If the popup menu (see |ins-completion-menu|) is not visible,
index ff223e6aefab5457100b00ad6d501c7f222323be..b11f0134f6aa9ae144e2280e93eae5edd7b7938b 100644 (file)
@@ -1118,6 +1118,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
index 1a899e8aa8fc539499c67fbf11622aecff32e06c..5bf561e220ebf5f652313b9fbd5f61de40bbf513 100644 (file)
@@ -6368,6 +6368,29 @@ f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
     set_callback(&buf->b_prompt_interrupt, &callback);
 }
 
+
+/*
+ * "prompt_getprompt({buffer})" function
+ */
+    void
+f_prompt_getprompt(typval_T *argvars, typval_T *rettv)
+{
+    buf_T      *buf;
+
+    // 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 = 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
  */
index dc0b45082d355e7bd0762885c8d20e0ba28fca80..bc74f44bb4774d6f058f7bd89de7a5e963179f86 100644 (file)
@@ -1681,17 +1681,27 @@ edit_putchar(int c, int highlight)
 }
 
 #if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
+/*
+ * Return the effective prompt for the specified buffer.
+ */
+    char_u *
+buf_prompt_text(buf_T* buf)
+{
+    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)
 {
-    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.
  * Move the cursor to this line.
index 91fbc675ce31b6eb0af503bc59a2549efd3730c5..ac75a7d5673b0cf6fa45e4a7f9a4c9a496890abd 100644 (file)
@@ -806,6 +806,7 @@ static funcentry_T global_functions[] =
     {"pow",            2, 2, FEARG_1,    ret_float,    FLOAT_FUNC(f_pow)},
     {"prevnonblank",   1, 1, FEARG_1,    ret_number,   f_prevnonblank},
     {"printf",         1, 19, FEARG_2,   ret_string,   f_printf},
+    {"prompt_getprompt", 1, 1, FEARG_1,          ret_string,   JOB_FUNC(f_prompt_getprompt)},
     {"prompt_setcallback", 2, 2, FEARG_1, ret_void,    JOB_FUNC(f_prompt_setcallback)},
     {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void,    JOB_FUNC(f_prompt_setinterrupt)},
     {"prompt_setprompt", 2, 2, FEARG_1,          ret_void,     JOB_FUNC(f_prompt_setprompt)},
index 56a0167b823ce9db54d40c446545325d0cb41c28..a402177cfb5fc0b839f1aa64feaedfb10e3b545b 100644 (file)
@@ -56,6 +56,7 @@ char *job_status(job_T *job);
 int job_stop(job_T *job, typval_T *argvars, char *type);
 void invoke_prompt_callback(void);
 int invoke_prompt_interrupt(void);
+void f_prompt_getprompt(typval_T *argvars, typval_T *rettv);
 void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
 void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
 void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
index 6efe53715133bf63fae4a52f2ec688d0596b9417..e2ec8dc869e915d5bf0155b6d5fb6a8c66c2d4f8 100644 (file)
@@ -4,6 +4,7 @@ int ins_need_undo_get(void);
 void ins_redraw(int ready);
 int decodeModifyOtherKeys(int c);
 void edit_putchar(int c, int highlight);
+char_u *buf_prompt_text(buf_T* buf);
 char_u *prompt_text(void);
 int prompt_curpos_editable(void);
 void edit_unputchar(void);
index 45eb1c907a3b714cb6d85f6b49f9d23b4ea42f7a..365687511dff5088f5ac04d8b8ed33cd5d22d40a 100644 (file)
@@ -148,4 +148,38 @@ 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( '%' ), '' )
+  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
index 92ff1e9bb5f7dccd8ed172fc8a419367e1c5f7a3..2c6e47efc0fb289e16e2f240aa2bdc6545f67fd5 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1588,
 /**/
     1587,
 /**/