Skip to content

Commit fcc1b57

Browse files
Shougozeertzjq
authored andcommitted
patch 9.1.0597: KeyInputPre cannot get the (unmapped typed) key
Problem: KeyInputPre cannot get the (unmapped typed) key (after v9.1.0563) Solution: Add the "typedchar" property to the v:event dict (Shougo Matsushita) closes: #15231 Co-authored-by: zeertzjq <zeertzjq@outlook.com> Signed-off-by: Shougo Matsushita <Shougo.Matsu@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 76c1902 commit fcc1b57

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

runtime/doc/autocmd.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*autocmd.txt* For Vim version 9.1. Last change: 2024 Jul 11
1+
*autocmd.txt* For Vim version 9.1. Last change: 2024 Jul 17
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -980,17 +980,19 @@ InsertLeavePre Just before leaving Insert mode. Also when
980980
InsertLeave Just after leaving Insert mode. Also when
981981
using CTRL-O |i_CTRL-O|. But not for |i_CTRL-C|.
982982
*KeyInputPre*
983-
KeyInputPre Just before a key is processed. The pattern is
984-
matched against a string that indicates the
985-
current mode, which is the same as what is
986-
returned by `mode(1)`.
983+
KeyInputPre Just before a key is processed after mappings
984+
have been applied. The pattern is matched
985+
against a string that indicates the current
986+
mode, which is the same as what is returned by
987+
`mode(1)`.
987988
The |v:char| variable indicates the key typed
988989
and can be changed during the event to process
989990
a different key. When |v:char| is not a
990991
single character or a special key, the first
991992
character is used.
992993
The following values of |v:event| are set:
993994
typed The key is typed or not.
995+
typedchar The (actual) typed key.
994996
It is not allowed to change the text
995997
|textlock| or the current mode.
996998
{only with the +eval feature}

src/getchar.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
4242

4343
static int typeahead_char = 0; // typeahead char that's not flushed
4444

45+
#ifdef FEAT_EVAL
46+
static char_u typedchars[MAXMAPLEN + 1] = { NUL }; // typed chars before map
47+
static int typedchars_pos = 0;
48+
#endif
49+
4550
/*
4651
* When block_redo is TRUE the redo buffer will not be changed.
4752
* Used by edit() to repeat insertions.
@@ -1709,6 +1714,13 @@ updatescript(int c)
17091714
ml_sync_all(c == 0, TRUE);
17101715
count = 0;
17111716
}
1717+
#ifdef FEAT_EVAL
1718+
if (typedchars_pos < MAXMAPLEN)
1719+
{
1720+
typedchars[typedchars_pos] = c;
1721+
typedchars_pos++;
1722+
}
1723+
#endif
17121724
}
17131725

17141726
/*
@@ -2135,6 +2147,9 @@ vgetc(void)
21352147

21362148
#ifdef FEAT_EVAL
21372149
c = do_key_input_pre(c);
2150+
2151+
// Clear the next typedchars_pos
2152+
typedchars_pos = 0;
21382153
#endif
21392154

21402155
// Need to process the character before we know it's safe to do something
@@ -2175,6 +2190,9 @@ do_key_input_pre(int c)
21752190
else
21762191
buf[(*mb_char2bytes)(c, buf)] = NUL;
21772192

2193+
typedchars[typedchars_pos] = NUL;
2194+
vim_unescape_csi(typedchars);
2195+
21782196
get_mode(curr_mode);
21792197

21802198
// Lock the text to avoid weird things from happening.
@@ -2183,6 +2201,7 @@ do_key_input_pre(int c)
21832201

21842202
v_event = get_v_event(&save_v_event);
21852203
(void)dict_add_bool(v_event, "typed", KeyTyped);
2204+
(void)dict_add_string(v_event, "typedchar", typedchars);
21862205

21872206
if (apply_autocmds(EVENT_KEYINPUTPRE, curr_mode, curr_mode, FALSE, curbuf)
21882207
&& STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)

src/testdir/test_autocmd.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4820,6 +4820,15 @@ func Test_KeyInputPre()
48204820
call feedkeys('j', 'nx')
48214821

48224822
au! KeyInputPre
4823+
4824+
" Test for v:event.typedchar
4825+
nnoremap j k
4826+
au KeyInputPre n
4827+
\ call assert_equal(v:event.typedchar, 'j')
4828+
\ | call assert_equal(v:char, 'k')
4829+
call feedkeys('j', 'tx')
4830+
4831+
au! KeyInputPre
48234832
endfunc
48244833

48254834
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
597,
707709
/**/
708710
596,
709711
/**/

0 commit comments

Comments
 (0)