Skip to content

Commit 0e5979a

Browse files
committed
patch 8.1.0069: cannot handle pressing CTRL-C in a prompt buffer
Problem: Cannot handle pressing CTRL-C in a prompt buffer. Solution: Add prompt_setinterrupt().
1 parent 2f82ca7 commit 0e5979a

File tree

6 files changed

+98
-13
lines changed

6 files changed

+98
-13
lines changed

runtime/doc/eval.txt

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,8 +2297,9 @@ pow({x}, {y}) Float {x} to the power of {y}
22972297
prevnonblank({lnum}) Number line nr of non-blank line <= {lnum}
22982298
printf({fmt}, {expr1}...) String format text
22992299
prompt_addtext({buf}, {expr}) none add text to a prompt buffer
2300-
prompt_setprompt({buf}, {text}) none set prompt text
23012300
prompt_setcallback({buf}, {expr}) none set prompt callback function
2301+
prompt_setinterrupt({buf}, {text}) none set prompt interrupt function
2302+
prompt_setprompt({buf}, {text}) none set prompt text
23022303
pumvisible() Number whether popup menu is visible
23032304
pyeval({expr}) any evaluate |Python| expression
23042305
py3eval({expr}) any evaluate |python3| expression
@@ -6506,17 +6507,11 @@ printf({fmt}, {expr1} ...) *printf()*
65066507
arguments an error is given. Up to 18 arguments can be used.
65076508

65086509

6509-
prompt_setprompt({buf}, {text}) *prompt_setprompt()*
6510-
Set prompt for buffer {buf} to {text}. You most likely want
6511-
{text} to end in a space.
6512-
The result is only visible if {buf} has 'buftype' set to
6513-
"prompt". Example: >
6514-
call prompt_setprompt(bufnr(''), 'command: ')
6515-
6516-
65176510
prompt_setcallback({buf}, {expr}) *prompt_setcallback()*
6518-
Set prompt callback for buffer {buf} to {expr}. This has only
6511+
Set prompt callback for buffer {buf} to {expr}. When {expr}
6512+
is an empty string the callback is removed. This has only
65196513
effect if {buf} has 'buftype' set to "prompt".
6514+
65206515
The callback is invoked when pressing Enter. The current
65216516
buffer will always be the prompt buffer. A new line for a
65226517
prompt is added before invoking the callback, thus the prompt
@@ -6541,6 +6536,22 @@ prompt_setcallback({buf}, {expr}) *prompt_setcallback()*
65416536
endif
65426537
endfunc
65436538
6539+
prompt_setinterrupt({buf}, {expr}) *prompt_setinterrupt()*
6540+
Set a callback for buffer {buf} to {expr}. When {expr} is an
6541+
empty string the callback is removed. This has only effect if
6542+
{buf} has 'buftype' set to "prompt".
6543+
6544+
This callback will be invoked when pressing CTRL-C in Insert
6545+
mode. Without setting a callback Vim will exit Insert mode,
6546+
as in any buffer.
6547+
6548+
prompt_setprompt({buf}, {text}) *prompt_setprompt()*
6549+
Set prompt for buffer {buf} to {text}. You most likely want
6550+
{text} to end in a space.
6551+
The result is only visible if {buf} has 'buftype' set to
6552+
"prompt". Example: >
6553+
call prompt_setprompt(bufnr(''), 'command: ')
6554+
65446555
65456556
pumvisible() *pumvisible()*
65466557
Returns non-zero when the popup menu is visible, zero
@@ -8563,7 +8574,9 @@ term_start({cmd}, {options}) *term_start()*
85638574
instead of using 'termwinsize'
85648575
"term_cols" horizontal size to use for the terminal,
85658576
instead of using 'termwinsize'
8566-
"vertical" split the window vertically
8577+
"vertical" split the window vertically; note that
8578+
other window position can be defined with
8579+
command modifiers, such as |:belowright|.
85678580
"curwin" use the current window, do not split the
85688581
window; fails if the current buffer
85698582
cannot be |abandon|ed
@@ -9392,11 +9405,12 @@ vtp Compiled for vcon support |+vtp| (check vcon to find
93929405
out if it works in the current console).
93939406
wildignore Compiled with 'wildignore' option.
93949407
wildmenu Compiled with 'wildmenu' option.
9408+
win16 old version for MS-Windows 3.1 (always False)
93959409
win32 Win32 version of Vim (MS-Windows 95 and later, 32 or
93969410
64 bits)
93979411
win32unix Win32 version of Vim, using Unix files (Cygwin)
93989412
win64 Win64 version of Vim (MS-Windows 64 bit).
9399-
win95 Win32 version for MS-Windows 95/98/ME.
9413+
win95 Win32 version for MS-Windows 95/98/ME (always False)
94009414
winaltkeys Compiled with 'winaltkeys' option.
94019415
windows Compiled with support for more than one window.
94029416
writebackup Compiled with 'writebackup' default on.

src/channel.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5856,7 +5856,7 @@ invoke_prompt_callback(void)
58565856
curwin->w_cursor.lnum = lnum + 1;
58575857
curwin->w_cursor.col = 0;
58585858

5859-
if (curbuf->b_prompt_callback == NULL)
5859+
if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
58605860
return;
58615861
text = ml_get(lnum);
58625862
prompt = prompt_text();
@@ -5874,4 +5874,28 @@ invoke_prompt_callback(void)
58745874
clear_tv(&rettv);
58755875
}
58765876

5877+
/*
5878+
* Return TRUE when the interrupt callback was invoked.
5879+
*/
5880+
int
5881+
invoke_prompt_interrupt(void)
5882+
{
5883+
typval_T rettv;
5884+
int dummy;
5885+
typval_T argv[1];
5886+
5887+
if (curbuf->b_prompt_interrupt == NULL
5888+
|| *curbuf->b_prompt_interrupt == NUL)
5889+
return FALSE;
5890+
argv[0].v_type = VAR_UNKNOWN;
5891+
5892+
got_int = FALSE; // don't skip executing commands
5893+
call_func(curbuf->b_prompt_interrupt,
5894+
(int)STRLEN(curbuf->b_prompt_interrupt),
5895+
&rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5896+
curbuf->b_prompt_int_partial, NULL);
5897+
clear_tv(&rettv);
5898+
return TRUE;
5899+
}
5900+
58775901
#endif /* FEAT_JOB_CHANNEL */

src/edit.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,19 @@ edit(
10161016
goto doESCkey;
10171017
}
10181018
#endif
1019+
#ifdef FEAT_JOB_CHANNEL
1020+
if (c == Ctrl_C && bt_prompt(curbuf))
1021+
{
1022+
if (invoke_prompt_interrupt())
1023+
{
1024+
if (!bt_prompt(curbuf))
1025+
// buffer changed to a non-prompt buffer, get out of
1026+
// Insert mode
1027+
goto doESCkey;
1028+
break;
1029+
}
1030+
}
1031+
#endif
10191032

10201033
#ifdef UNIX
10211034
do_intr:

src/evalfunc.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
298298
static void f_printf(typval_T *argvars, typval_T *rettv);
299299
#ifdef FEAT_JOB_CHANNEL
300300
static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
301+
static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
301302
static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
302303
#endif
303304
static void f_pumvisible(typval_T *argvars, typval_T *rettv);
@@ -754,6 +755,7 @@ static struct fst
754755
{"printf", 1, 19, f_printf},
755756
#ifdef FEAT_JOB_CHANNEL
756757
{"prompt_setcallback", 2, 2, f_prompt_setcallback},
758+
{"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
757759
{"prompt_setprompt", 2, 2, f_prompt_setprompt},
758760
#endif
759761
{"pumvisible", 0, 0, f_pumvisible},
@@ -8621,6 +8623,35 @@ f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
86218623
buf->b_prompt_partial = partial;
86228624
}
86238625

8626+
/*
8627+
* "prompt_setinterrupt({buffer}, {callback})" function
8628+
*/
8629+
static void
8630+
f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8631+
{
8632+
buf_T *buf;
8633+
char_u *callback;
8634+
partial_T *partial;
8635+
8636+
if (check_secure())
8637+
return;
8638+
buf = get_buf_tv(&argvars[0], FALSE);
8639+
if (buf == NULL)
8640+
return;
8641+
8642+
callback = get_callback(&argvars[1], &partial);
8643+
if (callback == NULL)
8644+
return;
8645+
8646+
free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8647+
if (partial == NULL)
8648+
buf->b_prompt_interrupt = vim_strsave(callback);
8649+
else
8650+
/* pointer into the partial */
8651+
buf->b_prompt_interrupt = callback;
8652+
buf->b_prompt_int_partial = partial;
8653+
}
8654+
86248655
/*
86258656
* "prompt_setprompt({buffer}, {text})" function
86268657
*/

src/proto/channel.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ void job_info(job_T *job, dict_T *dict);
7272
void job_info_all(list_T *l);
7373
int job_stop(job_T *job, typval_T *argvars, char *type);
7474
void invoke_prompt_callback(void);
75+
int invoke_prompt_interrupt(void);
7576
/* vim: set ft=c : */

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ static char *(features[]) =
761761

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
69,
764766
/**/
765767
68,
766768
/**/

0 commit comments

Comments
 (0)