diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-09-12 13:38:41 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-09-12 13:38:41 +0100 |
commit | cdc839353f68ca43db6446e1b727fc7ba657b738 (patch) | |
tree | 0729860faa774e594b183ce0823ae23776b92701 /src/evalfunc.c | |
parent | 5a4eb55122e45444d3a6c56ce108ce29bc8e52ab (diff) |
patch 9.0.0449: there is no easy way to translate a key code into a stringv9.0.0449
Problem: There is no easy way to translate a string with a key code into a
readable string.
Solution: Add the keytrans() function. (closes #11114)
Diffstat (limited to 'src/evalfunc.c')
-rw-r--r-- | src/evalfunc.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c index bc0e23e9c8..cd31562949 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -89,6 +89,7 @@ static void f_inputsecret(typval_T *argvars, typval_T *rettv); static void f_interrupt(typval_T *argvars, typval_T *rettv); static void f_invert(typval_T *argvars, typval_T *rettv); static void f_islocked(typval_T *argvars, typval_T *rettv); +static void f_keytrans(typval_T *argvars, typval_T *rettv); static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv); static void f_libcall(typval_T *argvars, typval_T *rettv); static void f_libcallnr(typval_T *argvars, typval_T *rettv); @@ -2058,6 +2059,8 @@ static funcentry_T global_functions[] = ret_string, f_json_encode}, {"keys", 1, 1, FEARG_1, arg1_dict_any, ret_list_string, f_keys}, + {"keytrans", 1, 1, FEARG_1, arg1_string, + ret_string, f_keytrans}, {"last_buffer_nr", 0, 0, 0, NULL, // obsolete ret_number, f_last_buffer_nr}, {"len", 1, 1, FEARG_1, arg1_len, @@ -7136,6 +7139,24 @@ f_islocked(typval_T *argvars, typval_T *rettv) } /* + * "keytrans()" function + */ + static void +f_keytrans(typval_T *argvars, typval_T *rettv) +{ + char_u *escaped; + + rettv->v_type = VAR_STRING; + if (check_for_string_arg(argvars, 0) == FAIL + || argvars[0].vval.v_string == NULL) + return; + // Need to escape K_SPECIAL and CSI for mb_unescape(). + escaped = vim_strsave_escape_csi(argvars[0].vval.v_string); + rettv->vval.v_string = str2special_save(escaped, TRUE, TRUE); + vim_free(escaped); +} + +/* * "last_buffer_nr()" function. */ static void |