From 907fc8ac373226556b84c2fdc4fe26525bbdb2c4 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 13 Sep 2022 06:23:33 +0800 Subject: vim-patch:9.0.0449: there is no easy way to translate a key code into a string (#20168) Problem: There is no easy way to translate a string with a key code into a readable string. Solution: Add the keytrans() function. (closes vim/vim#11114) https://github.com/vim/vim/commit/cdc839353f68ca43db6446e1b727fc7ba657b738 vim-patch:7b2d87220c6c Add missing part of patch https://github.com/vim/vim/commit/7b2d87220c6c974d5cdae672b6f9620a6bcbd1dc --- src/nvim/eval.lua | 1 + src/nvim/eval/funcs.c | 14 ++++++++++++++ src/nvim/menu.c | 3 +-- src/nvim/message.c | 8 ++++---- src/nvim/option.c | 4 +--- src/nvim/testdir/test_functions.vim | 26 ++++++++++++++++++++++++++ 6 files changed, 47 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 5c500af899..837fef23f4 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -236,6 +236,7 @@ return { json_decode={args=1, base=1}, json_encode={args=1, base=1}, keys={args=1, base=1}, + keytrans={args=1, base=1}, last_buffer_nr={}, -- obsolete len={args=1, base=1}, libcall={args=3, base=3}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 3c08acc10b..b5ae24953f 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -4673,6 +4673,20 @@ static void f_json_encode(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) rettv->vval.v_string = encode_tv2json(&argvars[0], NULL); } +/// "keytrans()" function +static void f_keytrans(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + rettv->v_type = VAR_STRING; + if (tv_check_for_string_arg(argvars, 0) == FAIL + || argvars[0].vval.v_string == NULL) { + return; + } + // Need to escape K_SPECIAL for mb_unescape(). + char *escaped = vim_strsave_escape_ks(argvars[0].vval.v_string); + rettv->vval.v_string = str2special_save(escaped, true, true); + xfree(escaped); +} + /// "last_buffer_nr()" function. static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { diff --git a/src/nvim/menu.c b/src/nvim/menu.c index b38e832c77..57116170aa 100644 --- a/src/nvim/menu.c +++ b/src/nvim/menu.c @@ -685,8 +685,7 @@ static dict_T *menu_get_recursive(const vimmenu_T *menu, int modes) if ((menu->modes & modes & (1 << bit)) != 0) { dict_T *impl = tv_dict_alloc(); tv_dict_add_allocated_str(impl, S_LEN("rhs"), - str2special_save(menu->strings[bit], - false, false)); + str2special_save(menu->strings[bit], false, false)); tv_dict_add_nr(impl, S_LEN("silent"), menu->silent[bit]); tv_dict_add_nr(impl, S_LEN("enabled"), (menu->enabled & (1 << bit)) ? 1 : 0); diff --git a/src/nvim/message.c b/src/nvim/message.c index 46f6adec47..fd8aa00d7b 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -1681,8 +1681,8 @@ int msg_outtrans_special(const char *strstart, bool from, int maxlen) /// Used for lhs or rhs of mappings. /// /// @param[in] str String to convert. -/// @param[in] replace_spaces Convert spaces into ``, normally used fo -/// lhs, but not rhs. +/// @param[in] replace_spaces Convert spaces into ``, normally used for +/// lhs of mapping and keytrans(), but not rhs. /// @param[in] replace_lt Convert `<` into ``. /// /// @return [allocated] Converted string. @@ -1704,8 +1704,8 @@ char *str2special_save(const char *const str, const bool replace_spaces, const b /// Convert character, replacing key with printable representation. /// /// @param[in,out] sp String to convert. Is advanced to the next key code. -/// @param[in] replace_spaces Convert spaces into , normally used for -/// lhs, but not rhs. +/// @param[in] replace_spaces Convert spaces into ``, normally used for +/// lhs of mapping and keytrans(), but not rhs. /// @param[in] replace_lt Convert `<` into ``. /// /// @return Converted key code, in a static buffer. Buffer is always one and the diff --git a/src/nvim/option.c b/src/nvim/option.c index de9510cf8a..5adec084e6 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -3558,9 +3558,7 @@ static int put_setstring(FILE *fd, char *cmd, char *name, char **valuep, uint64_ if (valuep == &p_pt) { s = (char_u *)(*valuep); while (*s != NUL) { - if (put_escstr(fd, (char_u *)str2special((const char **)&s, false, - false), 2) - == FAIL) { + if (put_escstr(fd, (char_u *)str2special((const char **)&s, false, false), 2) == FAIL) { return FAIL; } } diff --git a/src/nvim/testdir/test_functions.vim b/src/nvim/testdir/test_functions.vim index c41884936e..1750a600f3 100644 --- a/src/nvim/testdir/test_functions.vim +++ b/src/nvim/testdir/test_functions.vim @@ -1907,6 +1907,32 @@ func Test_eval() call assert_fails("call eval('5 a')", 'E488:') endfunc +" Test for the keytrans() function +func Test_keytrans() + call assert_equal('', keytrans(' ')) + call assert_equal('', keytrans('<')) + call assert_equal('Tab>', keytrans('')) + call assert_equal('', keytrans("\")) + call assert_equal('', keytrans("\")) + call assert_equal('', keytrans("\")) + call assert_equal('', keytrans("\")) + call assert_equal('', keytrans("\")) + call assert_equal('', keytrans("\")) + call assert_equal('', keytrans("\")) + call assert_equal('', keytrans("\<*M-Space>")) + call assert_equal('', "\<*M-x>"->keytrans()) + call assert_equal('', "\<*C-I>"->keytrans()) + call assert_equal('', "\<*S-3>"->keytrans()) + call assert_equal('π', 'π'->keytrans()) + call assert_equal('', "\"->keytrans()) + call assert_equal('ě', 'ě'->keytrans()) + call assert_equal('', "\"->keytrans()) + call assert_equal('', ''->keytrans()) + call assert_equal('', v:_null_string->keytrans()) + call assert_fails('call keytrans(1)', 'E1174:') + call assert_fails('call keytrans()', 'E119:') +endfunc + " Test for the nr2char() function func Test_nr2char() " set encoding=latin1 -- cgit