Skip to content

Commit 39c82ea

Browse files
committed
patch 9.0.1130: unexpected output when autoloading a script
Problem: Unexpected output when autoloading a script for an interactive operation. Solution: Reset "KeyTyped" while loading a script and when handling a nested function. (closes #11773)
1 parent 7bdcba0 commit 39c82ea

5 files changed

Lines changed: 65 additions & 3 deletions

File tree

src/scriptfile.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,10 +1334,10 @@ do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
13341334
* When "ret_sid" is not NULL and we loaded the script before, don't load it
13351335
* again.
13361336
*
1337-
* The 'eap' argument is used when sourcing lines from a buffer instead of a
1337+
* The "eap" argument is used when sourcing lines from a buffer instead of a
13381338
* file.
13391339
*
1340-
* If 'clearvars' is TRUE, then for scripts which are loaded more than
1340+
* If "clearvars" is TRUE, then for scripts which are loaded more than
13411341
* once, clear all the functions and variables previously defined in that
13421342
* script.
13431343
*
@@ -1538,6 +1538,7 @@ do_source_ext(
15381538
current_sctx.sc_version = SCRIPT_VERSION_VIM9;
15391539
else
15401540
current_sctx.sc_version = 1; // default script version
1541+
current_sctx.sc_lnum = 0;
15411542

15421543
#ifdef FEAT_EVAL
15431544
# ifdef FEAT_PROFILE
@@ -1549,7 +1550,10 @@ do_source_ext(
15491550
// Also starts profiling timer for nested script.
15501551
save_funccal(&funccalp_entry);
15511552

1552-
current_sctx.sc_lnum = 0;
1553+
// Reset "KeyTyped" to avoid some commands thinking they are invoked
1554+
// interactively. E.g. defining a function would output indent.
1555+
int save_KeyTyped = KeyTyped;
1556+
KeyTyped = FALSE;
15531557

15541558
// Check if this script was sourced before to find its SID.
15551559
// Always use a new sequence number.
@@ -1765,6 +1769,7 @@ do_source_ext(
17651769
# endif
17661770
#endif
17671771
current_sctx = save_current_sctx;
1772+
KeyTyped = save_KeyTyped;
17681773

17691774
if (cookie.fp != NULL)
17701775
fclose(cookie.fp);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @73
4+
|~| @73
5+
|~| @73
6+
|:+0#0000000&|"> @72

src/testdir/test_vim9_func.vim

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4413,6 +4413,48 @@ def Test_invalid_redir()
44134413
delfunc g:Ttwo
44144414
enddef
44154415

4416+
func Test_keytyped_in_nested_function()
4417+
CheckRunVimInTerminal
4418+
4419+
call Run_Test_keytyped_in_nested_function()
4420+
endfunc
4421+
4422+
def Run_Test_keytyped_in_nested_function()
4423+
var lines =<< trim END
4424+
vim9script
4425+
autocmd CmdlineEnter * sample#Init()
4426+
4427+
exe 'set rtp=' .. getcwd() .. '/Xrtpdir'
4428+
END
4429+
writefile(lines, 'Xkeytyped', 'D')
4430+
4431+
var dir = 'Xrtpdir/autoload'
4432+
mkdir(dir, 'pR')
4433+
4434+
lines =<< trim END
4435+
vim9script
4436+
export def Init(): void
4437+
cnoremap <expr>" <SID>Quote('"')
4438+
enddef
4439+
def Quote(str: string): string
4440+
def InPair(): number
4441+
return 0
4442+
enddef
4443+
return str
4444+
enddef
4445+
END
4446+
writefile(lines, dir .. '/sample.vim')
4447+
4448+
var buf = g:RunVimInTerminal('-S Xkeytyped', {rows: 6})
4449+
4450+
term_sendkeys(buf, ':"')
4451+
g:VerifyScreenDump(buf, 'Test_keytyped_in_nested_func', {})
4452+
4453+
# clean up
4454+
term_sendkeys(buf, "\<Esc>")
4455+
g:StopVimInTerminal(buf)
4456+
enddef
4457+
44164458
" The following messes up syntax highlight, keep near the end.
44174459
if has('python3')
44184460
def Test_python3_command()

src/version.c

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

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1130,
698700
/**/
699701
1129,
700702
/**/

src/vim9compile.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,14 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
987987
goto theend;
988988
}
989989

990+
// Make sure "KeyTyped" is not set, it may cause indent to be written.
991+
int save_KeyTyped = KeyTyped;
992+
KeyTyped = FALSE;
993+
990994
ufunc = define_function(eap, lambda_name, lines_to_free, FALSE);
995+
996+
KeyTyped = save_KeyTyped;
997+
991998
if (ufunc == NULL)
992999
{
9931000
r = eap->skip ? OK : FAIL;

0 commit comments

Comments
 (0)