Skip to content

Commit f273245

Browse files
committed
patch 8.1.0027: difficult to make a plugin that feeds a line to a job
Problem: Difficult to make a plugin that feeds a line to a job. Solution: Add the nitial code for the "prompt" buftype.
1 parent 33c5e9f commit f273245

22 files changed

+532
-58
lines changed

runtime/doc/channel.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The Netbeans interface also uses a channel. |netbeans|
2222
9. Starting a job without a channel |job-start-nochannel|
2323
10. Job options |job-options|
2424
11. Controlling a job |job-control|
25+
12. Using a prompt buffer |prompt-buffer|
2526

2627
{Vi does not have any of these features}
2728
{only when compiled with the |+channel| feature for channel stuff}
@@ -770,5 +771,43 @@ signals. E.g. to force a job to stop, "kill it": >
770771
771772
For more options see |job_stop()|.
772773

774+
==============================================================================
775+
12. Using a prompt buffer *prompt-buffer*
776+
777+
If you want to type input for the job in a Vim window you have a few options:
778+
- Use a normal buffer and handle all possible commands yourself.
779+
This will be complicated, since there are so many possible commands.
780+
- Use a terminal window. This works well if what you type goes directly to
781+
the job and the job output is directly displayed in the window.
782+
See |terminal-window|.
783+
- Use a prompt window. This works well when entering a line for the job in Vim
784+
while displaying (possibly filtered) output from the job.
785+
786+
A prompt buffer is created by setting 'buftype' to "prompt". You would
787+
normally only do that in a newly created buffer.
788+
789+
The user can edit and enter one line of text at the very last line of the
790+
buffer. When pressing Enter in the prompt line the callback set with
791+
|prompt_setcallback()| is invoked. It would normally send the line to a job.
792+
Another callback would receive the output from the job and display it in the
793+
buffer, below the prompt (and above the next prompt).
794+
795+
Only the text in the last line, after the prompt, is editable. The rest of the
796+
buffer is not modifiable with Normal mode commands. It can be modified by
797+
calling functions, such as |append()|. Using other commands may mess up the
798+
buffer.
799+
800+
After setting 'buftype' to "prompt" Vim does not automatically start Insert
801+
mode, use `:startinsert` if you want to enter Insert mode, so that the user
802+
can start typing a line.
803+
804+
The text of the prompt can be set with the |prompt_setprompt()| function.
805+
806+
The user can go to Normal mode and navigate through the buffer. This can be
807+
useful see older output or copy text.
808+
809+
Any command that starts Insert mode, such as "a", "i", "A" and "I", will move
810+
the cursor to the last line, after the prompt.
811+
773812

774813
vim:tw=78:ts=8:ft=help:norl:

runtime/doc/eval.txt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,9 @@ perleval({expr}) any evaluate |Perl| expression
22942294
pow({x}, {y}) Float {x} to the power of {y}
22952295
prevnonblank({lnum}) Number line nr of non-blank line <= {lnum}
22962296
printf({fmt}, {expr1}...) String format text
2297+
prompt_addtext({buf}, {expr}) none add text to a prompt buffer
2298+
prompt_setprompt({buf}, {text}) none set prompt text
2299+
prompt_setcallback({buf}, {expr}) none set prompt callback function
22972300
pumvisible() Number whether popup menu is visible
22982301
pyeval({expr}) any evaluate |Python| expression
22992302
py3eval({expr}) any evaluate |python3| expression
@@ -2302,7 +2305,7 @@ range({expr} [, {max} [, {stride}]])
23022305
List items from {expr} to {max}
23032306
readfile({fname} [, {binary} [, {max}]])
23042307
List get list of lines from file {fname}
2305-
reg_executing() Number get the executing register name
2308+
reg_executing() String get the executing register name
23062309
reg_recording() String get the recording register name
23072310
reltime([{start} [, {end}]]) List get time value
23082311
reltimefloat({time}) Float turn the time value into a Float
@@ -4650,7 +4653,7 @@ getline({lnum} [, {end}])
46504653
from the current buffer. Example: >
46514654
getline(1)
46524655
< When {lnum} is a String that doesn't start with a
4653-
digit, line() is called to translate the String into a Number.
4656+
digit, |line()| is called to translate the String into a Number.
46544657
To get the line under the cursor: >
46554658
getline(".")
46564659
< When {lnum} is smaller than 1 or bigger than the number of
@@ -6475,6 +6478,42 @@ printf({fmt}, {expr1} ...) *printf()*
64756478
arguments an error is given. Up to 18 arguments can be used.
64766479

64776480

6481+
prompt_setprompt({buf}, {text}) *prompt_setprompt()*
6482+
Set prompt for buffer {buf} to {text}. You most likely want
6483+
{text} to end in a space.
6484+
The result is only visible if {buf} has 'buftype' set to
6485+
"prompt". Example: >
6486+
call prompt_setprompt(bufnr(''), 'command: ')
6487+
6488+
6489+
prompt_setcallback({buf}, {expr}) *prompt_setcallback()*
6490+
Set prompt callback for buffer {buf} to {expr}. This has only
6491+
effect if {buf} has 'buftype' set to "prompt".
6492+
The callback is invoked when pressing Enter. The current
6493+
buffer will always be the prompt buffer. A new line for a
6494+
prompt is added before invoking the callback, thus the prompt
6495+
for which the callback was invoked will be in the last but one
6496+
line.
6497+
If the callback wants to add text to the buffer, it must
6498+
insert it above the last line, since that is where the current
6499+
prompt is. This can also be done asynchronously.
6500+
The callback is invoked with one argument, which is the text
6501+
that was entered at the prompt. This can be an empty string
6502+
if the user only typed Enter.
6503+
Example: >
6504+
call prompt_setcallback(bufnr(''), function('s:TextEntered'))
6505+
func s:TextEntered(text)
6506+
if a:text == 'exit' || a:text == 'quit'
6507+
stopinsert
6508+
close
6509+
else
6510+
call append(line('$') - 1, 'Entered: "' . a:text . '"')
6511+
" Reset 'modified' to allow the buffer to be closed.
6512+
set nomodified
6513+
endif
6514+
endfunc
6515+
6516+
64786517
pumvisible() *pumvisible()*
64796518
Returns non-zero when the popup menu is visible, zero
64806519
otherwise. See |ins-completion-menu|.

runtime/doc/options.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,9 @@ A jump table for the options with a short description can be found at |Q_op|.
13941394
manually)
13951395
terminal buffer for a |terminal| (you are not supposed to set
13961396
this manually)
1397+
prompt buffer where only the last line can be edited, meant
1398+
to be used by a plugin, see |prompt-buffer|
1399+
{only when compiled with the |+channel| feature}
13971400

13981401
This option is used together with 'bufhidden' and 'swapfile' to
13991402
specify special kinds of buffers. See |special-buffers|.
@@ -4264,7 +4267,8 @@ A jump table for the options with a short description can be found at |Q_op|.
42644267
'imactivatefunc' 'imaf' string (default "")
42654268
global
42664269
{not in Vi}
4267-
{only available when compiled with |+mbyte|}
4270+
{only available when compiled with the |+multi_byte|
4271+
feature}
42684272
This option specifies a function that will be called to
42694273
activate or deactivate the Input Method.
42704274
It is not used in the GUI.
@@ -4316,7 +4320,8 @@ A jump table for the options with a short description can be found at |Q_op|.
43164320
'imcmdline' 'imc' boolean (default off)
43174321
global
43184322
{not in Vi}
4319-
{only available when compiled with |+mbyte|}
4323+
{only available when compiled with the |+multi_byte|
4324+
feature}
43204325
When set the Input Method is always on when starting to edit a command
43214326
line, unless entering a search pattern (see 'imsearch' for that).
43224327
Setting this option is useful when your input method allows entering
@@ -4327,7 +4332,8 @@ A jump table for the options with a short description can be found at |Q_op|.
43274332
'imdisable' 'imd' boolean (default off, on for some systems (SGI))
43284333
global
43294334
{not in Vi}
4330-
{only available when compiled with |+mbyte|}
4335+
{only available when compiled with the |+multi_byte|
4336+
feature}
43314337
When set the Input Method is never used. This is useful to disable
43324338
the IM when it doesn't work properly.
43334339
Currently this option is on by default for SGI/IRIX machines. This
@@ -4380,7 +4386,8 @@ A jump table for the options with a short description can be found at |Q_op|.
43804386
'imstatusfunc' 'imsf' string (default "")
43814387
global
43824388
{not in Vi}
4383-
{only available when compiled with |+mbyte|}
4389+
{only available when compiled with the |+multi_byte|
4390+
feature}
43844391
This option specifies a function that is called to obtain the status
43854392
of Input Method. It must return a positive number when IME is active.
43864393
It is not used in the GUI.

runtime/doc/todo.txt

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ browser use: https://github.com/vim/vim/issues/1234
3838
*known-bugs*
3939
-------------------- Known bugs and current work -----------------------
4040

41+
Prompt buffer:
42+
- Add a command line history.
43+
- delay next prompt until plugin gives OK?
44+
4145
Terminal emulator window:
4246
- Win32: Termdebug doesn't work, because gdb does not support mi2 on a tty.
4347
This plugin: https://github.com/cpiger/NeoDebug runs gdb as a job,
@@ -71,33 +75,66 @@ Crash when mixing matchadd and substitute()? (Max Christian Pohle, 2018 May
7175
On Win32 when not in the console and t_Co >= 256, allow using 'tgc'.
7276
(Nobuhiro Takasaki, #2833) Also check t_Co.
7377

74-
balloon_show() does not work properly in the terminal. (Ben Jackson, 2017 Dec
75-
20, #2481)
76-
Also see #2352, want better control over balloon, perhaps set the position.
78+
Patch to fix arguments of :edit. (Dominique Pelle, 2018 May 28 #2966)
79+
80+
Ptch to update html syntax. (Jorge Maldonado Ventura, #2974)
81+
82+
Patch to fix that restoring window doesn't work when 'winheight' is large.
83+
(Darrell Nash, 2018 May 30, #2971) Doesn't work? Issue #2970
84+
85+
Patch to add completion to :unlet for environment vars. (Jason Franklin, 2018
86+
May 30) Last update.
7787

7888
Errors found with random data:
7989
heap-buffer-overflow in alist_add (#2472)
8090

8191
More warnings from static analysis:
8292
https://lgtm.com/projects/g/vim/vim/alerts/?mode=list
8393

94+
Patch to make "is" and "as" work bettter. (Jason Franklin, 2018 May 19)
95+
96+
Patch to add tests for user and language completion. (Dominique Pelle, 2018
97+
Jun 2, #2978)
98+
99+
Using ":file" in quickfix window during an autocommand doesn't work.
100+
(Jason Franklin, 2018 May 23) Allow for using it when there is no argument.
101+
102+
Pull request #2967: Allow white space in sign text. (Ben Jackson)
103+
104+
Patch for xterm and vt320 builtin termcap. (Kouichi Iwamoto, 2018 May 31,
105+
#2973)
106+
107+
Patch to add more testing for :cd command. (Dominique Pelle, 2018 May 30,
108+
#2972)
109+
84110
Script generated by :mksession does not work well if there are windows with
85111
modified buffers
86112
change "silent only" into "silent only!"
87113
change "edit fname" of first buffer to "hide edit fname"
88114
skip "badd fname" if "fname" is already in the buffer list
89115
remove remark about unloading buffers from documentation
90116

117+
Patch to make :help work for tags with a ?. (Hirohito Higashi, 2018 May 28)
118+
91119
Compiler warnings (geeknik, 2017 Oct 26):
92120
- signed integer overflow in do_sub() (#2249)
93121
- signed integer overflow in get_address() (#2248)
94122
- signed integer overflow in getdecchrs() (#2254)
95123
- undefined left shift in get_string_tv() (#2250)
96124

125+
Patch for more quickfix refactoring. (Yegappan Lakshmanan, #2950)
126+
97127
Tests failing for "make testgui" with GTK:
98128
- Test_setbufvar_options()
99129
- Test_exit_callback_interval()
100130

131+
Make balloon_show() work outside of 'balloonexpr'? Users expect it to work:
132+
#2948. (related to #1512?)
133+
On Win32 it stops showing, because showState is already ShS_SHOWING.
134+
balloon_show() does not work properly in the terminal. (Ben Jackson, 2017 Dec
135+
20, #2481)
136+
Also see #2352, want better control over balloon, perhaps set the position.
137+
101138
Try out background make plugin:
102139
https://github.com/AndrewVos/vim-make-background
103140
or asyncmake:
@@ -112,6 +149,8 @@ used for git temp files.
112149

113150
Cursor in wrong position when line wraps. (#2540)
114151

152+
Patch for Lua support. (Kazunobu Kuriyama, 2018 May 26)
153+
115154
Add an option similar to 'lazyredraw' to skip redrawing while executing a
116155
script or function.
117156

@@ -141,6 +180,9 @@ How to test that it works well for all Vim users?
141180

142181
Alternative manpager.vim. (Enno, 2018 Jan 5, #2529)
143182

183+
Patch to use NGETTEXT() in many more places. (Sergey Alyoshin, 2018 May 25)
184+
Updated ptach May 27.
185+
144186
Does setting 'cursorline' cause syntax highlighting to slow down? Perhaps is
145187
mess up the cache? (Mike Lee Williams, 2018 Jan 27, #2539)
146188
Also: 'foldtext' is evaluated too often. (Daniel Hahler, #2773)

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,7 @@ test_arglist \
22522252
test_popup \
22532253
test_preview \
22542254
test_profile \
2255+
test_prompt_buffer \
22552256
test_put \
22562257
test_python2 \
22572258
test_python3 \

src/buffer.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,10 @@ free_buffer(buf_T *buf)
851851
#ifdef FEAT_TERMINAL
852852
free_terminal(buf);
853853
#endif
854+
#ifdef FEAT_JOB_CHANNEL
855+
vim_free(buf->b_prompt_text);
856+
free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
857+
#endif
854858

855859
buf_hashtab_remove(buf);
856860

@@ -5633,6 +5637,15 @@ bt_help(buf_T *buf)
56335637
return buf != NULL && buf->b_help;
56345638
}
56355639

5640+
/*
5641+
* Return TRUE if "buf" is a prompt buffer.
5642+
*/
5643+
int
5644+
bt_prompt(buf_T *buf)
5645+
{
5646+
return buf != NULL && buf->b_p_bt[0] == 'p';
5647+
}
5648+
56365649
/*
56375650
* Return TRUE if "buf" is a "nofile", "acwrite" or "terminal" buffer.
56385651
* This means the buffer name is not a file name.
@@ -5642,7 +5655,8 @@ bt_nofile(buf_T *buf)
56425655
{
56435656
return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
56445657
|| buf->b_p_bt[0] == 'a'
5645-
|| buf->b_p_bt[0] == 't');
5658+
|| buf->b_p_bt[0] == 't'
5659+
|| buf->b_p_bt[0] == 'p');
56465660
}
56475661

56485662
/*
@@ -5651,7 +5665,9 @@ bt_nofile(buf_T *buf)
56515665
int
56525666
bt_dontwrite(buf_T *buf)
56535667
{
5654-
return buf != NULL && (buf->b_p_bt[0] == 'n' || buf->b_p_bt[0] == 't');
5668+
return buf != NULL && (buf->b_p_bt[0] == 'n'
5669+
|| buf->b_p_bt[0] == 't'
5670+
|| buf->b_p_bt[0] == 'p');
56555671
}
56565672

56575673
int

src/channel.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5836,4 +5836,38 @@ job_stop(job_T *job, typval_T *argvars, char *type)
58365836
return 1;
58375837
}
58385838

5839+
void
5840+
invoke_prompt_callback(void)
5841+
{
5842+
typval_T rettv;
5843+
int dummy;
5844+
typval_T argv[2];
5845+
char_u *text;
5846+
char_u *prompt;
5847+
linenr_T lnum = curbuf->b_ml.ml_line_count;
5848+
5849+
// Add a new line for the prompt before invoking the callback, so that
5850+
// text can always be inserted above the last line.
5851+
ml_append(lnum, (char_u *)"", 0, FALSE);
5852+
curwin->w_cursor.lnum = lnum + 1;
5853+
curwin->w_cursor.col = 0;
5854+
5855+
if (curbuf->b_prompt_callback == NULL)
5856+
return;
5857+
text = ml_get(lnum);
5858+
prompt = prompt_text();
5859+
if (STRLEN(text) >= STRLEN(prompt))
5860+
text += STRLEN(prompt);
5861+
argv[0].v_type = VAR_STRING;
5862+
argv[0].vval.v_string = vim_strsave(text);
5863+
argv[1].v_type = VAR_UNKNOWN;
5864+
5865+
call_func(curbuf->b_prompt_callback,
5866+
(int)STRLEN(curbuf->b_prompt_callback),
5867+
&rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5868+
curbuf->b_prompt_partial, NULL);
5869+
clear_tv(&argv[0]);
5870+
clear_tv(&rettv);
5871+
}
5872+
58395873
#endif /* FEAT_JOB_CHANNEL */

src/diff.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,13 @@ nv_diffgetput(int put, long count)
21412141
exarg_T ea;
21422142
char_u buf[30];
21432143

2144+
#ifdef FEAT_JOB_CHANNEL
2145+
if (bt_prompt(curbuf))
2146+
{
2147+
vim_beep(BO_OPER);
2148+
return;
2149+
}
2150+
#endif
21442151
if (count == 0)
21452152
ea.arg = (char_u *)"";
21462153
else

0 commit comments

Comments
 (0)