diff options
Diffstat (limited to 'src/nvim/keycodes.c')
-rw-r--r-- | src/nvim/keycodes.c | 210 |
1 files changed, 87 insertions, 123 deletions
diff --git a/src/nvim/keycodes.c b/src/nvim/keycodes.c index e19806e464..745500fe39 100644 --- a/src/nvim/keycodes.c +++ b/src/nvim/keycodes.c @@ -1,29 +1,25 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include <assert.h> #include <inttypes.h> #include <limits.h> #include <stdbool.h> #include <stdio.h> #include <string.h> +#include <sys/types.h> -#include "nvim/ascii.h" +#include "nvim/ascii_defs.h" #include "nvim/charset.h" #include "nvim/eval/typval_defs.h" #include "nvim/eval/vars.h" +#include "nvim/func_attr.h" #include "nvim/gettext.h" #include "nvim/globals.h" #include "nvim/keycodes.h" -#include "nvim/log.h" -#include "nvim/macros.h" +#include "nvim/macros_defs.h" #include "nvim/mbyte.h" #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/mouse.h" #include "nvim/strings.h" -#include "nvim/types.h" -#include "nvim/vim.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "keycodes.c.generated.h" @@ -34,18 +30,18 @@ static const struct modmasktable { uint16_t mod_mask; ///< Bit-mask for particular key modifier. uint16_t mod_flag; ///< Bit(s) for particular key modifier. - char_u name; ///< Single letter name of modifier. + char name; ///< Single letter name of modifier. } mod_mask_table[] = { - { MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M' }, - { MOD_MASK_META, MOD_MASK_META, (char_u)'T' }, - { MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C' }, - { MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S' }, - { MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2' }, - { MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3' }, - { MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4' }, - { MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D' }, + { MOD_MASK_ALT, MOD_MASK_ALT, 'M' }, + { MOD_MASK_META, MOD_MASK_META, 'T' }, + { MOD_MASK_CTRL, MOD_MASK_CTRL, 'C' }, + { MOD_MASK_SHIFT, MOD_MASK_SHIFT, 'S' }, + { MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, '2' }, + { MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, '3' }, + { MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, '4' }, + { MOD_MASK_CMD, MOD_MASK_CMD, 'D' }, // 'A' must be the last one - { MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A' }, + { MOD_MASK_ALT, MOD_MASK_ALT, 'A' }, { 0, 0, NUL } // NOTE: when adding an entry, update MAX_KEY_NAME_LEN! }; @@ -386,7 +382,7 @@ int name_to_mod_mask(int c) { c = TOUPPER_ASC(c); for (size_t i = 0; mod_mask_table[i].mod_mask != 0; i++) { - if (c == mod_mask_table[i].name) { + if (c == (uint8_t)mod_mask_table[i].name) { return mod_mask_table[i].mod_flag; } } @@ -468,16 +464,12 @@ int handle_x_keys(const int key) } /// @return a string which contains the name of the given key when the given modifiers are down. -char_u *get_special_key_name(int c, int modifiers) +char *get_special_key_name(int c, int modifiers) { - static char_u string[MAX_KEY_NAME_LEN + 1]; - - int i, idx; - int table_idx; - char *s; + static char string[MAX_KEY_NAME_LEN + 1]; string[0] = '<'; - idx = 1; + int idx = 1; // Key that stands for a normal character. if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY) { @@ -487,7 +479,7 @@ char_u *get_special_key_name(int c, int modifiers) // Translate shifted special keys into unshifted keys and set modifier. // Same for CTRL and ALT modifiers. if (IS_SPECIAL(c)) { - for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE) { + for (int i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE) { if (KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1] && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2]) { modifiers |= modifier_keys_table[i]; @@ -499,7 +491,7 @@ char_u *get_special_key_name(int c, int modifiers) } // try to find the key in the special key table - table_idx = find_special_key_in_table(c); + int table_idx = find_special_key_in_table(c); // When not a known special key, and not a printable character, try to // extract modifiers. @@ -520,11 +512,11 @@ char_u *get_special_key_name(int c, int modifiers) } // translate the modifier into a string - for (i = 0; mod_mask_table[i].name != 'A'; i++) { + for (int i = 0; mod_mask_table[i].name != 'A'; i++) { if ((modifiers & mod_mask_table[i].mod_mask) == mod_mask_table[i].mod_flag) { string[idx++] = mod_mask_table[i].name; - string[idx++] = (char_u)'-'; + string[idx++] = '-'; } } @@ -532,18 +524,18 @@ char_u *get_special_key_name(int c, int modifiers) if (IS_SPECIAL(c)) { string[idx++] = 't'; string[idx++] = '_'; - string[idx++] = (char_u)KEY2TERMCAP0(c); - string[idx++] = KEY2TERMCAP1(c); + string[idx++] = (char)(uint8_t)KEY2TERMCAP0(c); + string[idx++] = (char)(uint8_t)KEY2TERMCAP1(c); } else { // Not a special key, only modifiers, output directly. if (utf_char2len(c) > 1) { - idx += utf_char2bytes(c, (char *)string + idx); + idx += utf_char2bytes(c, string + idx); } else if (vim_isprintc(c)) { - string[idx++] = (char_u)c; + string[idx++] = (char)(uint8_t)c; } else { - s = transchar(c); + char *s = transchar(c); while (*s) { - string[idx++] = (uint8_t)(*s++); + string[idx++] = *s++; } } } @@ -572,8 +564,8 @@ char_u *get_special_key_name(int c, int modifiers) /// @param[out] did_simplify found <C-H>, etc. /// /// @return Number of characters added to dst, zero for no match. -unsigned int trans_special(const char **const srcp, const size_t src_len, char *const dst, - const int flags, const bool escape_ks, bool *const did_simplify) +unsigned trans_special(const char **const srcp, const size_t src_len, char *const dst, + const int flags, const bool escape_ks, bool *const did_simplify) FUNC_ATTR_NONNULL_ARG(1, 3) FUNC_ATTR_WARN_UNUSED_RESULT { int modifiers = 0; @@ -582,7 +574,7 @@ unsigned int trans_special(const char **const srcp, const size_t src_len, char * return 0; } - return special_to_buf(key, modifiers, escape_ks, (char_u *)dst); + return special_to_buf(key, modifiers, escape_ks, dst); } /// Put the character sequence for "key" with "modifiers" into "dst" and return @@ -590,27 +582,27 @@ unsigned int trans_special(const char **const srcp, const size_t src_len, char * /// When "escape_ks" is true escape K_SPECIAL bytes in the character. /// The sequence is not NUL terminated. /// This is how characters in a string are encoded. -unsigned int special_to_buf(int key, int modifiers, bool escape_ks, char_u *dst) +unsigned special_to_buf(int key, int modifiers, bool escape_ks, char *dst) { - unsigned int dlen = 0; + unsigned dlen = 0; // Put the appropriate modifier in a string. if (modifiers != 0) { - dst[dlen++] = K_SPECIAL; - dst[dlen++] = KS_MODIFIER; - dst[dlen++] = (char_u)modifiers; + dst[dlen++] = (char)(uint8_t)K_SPECIAL; + dst[dlen++] = (char)(uint8_t)KS_MODIFIER; + dst[dlen++] = (char)(uint8_t)modifiers; } if (IS_SPECIAL(key)) { - dst[dlen++] = K_SPECIAL; - dst[dlen++] = (char_u)KEY2TERMCAP0(key); - dst[dlen++] = KEY2TERMCAP1(key); + dst[dlen++] = (char)(uint8_t)K_SPECIAL; + dst[dlen++] = (char)(uint8_t)KEY2TERMCAP0(key); + dst[dlen++] = (char)(uint8_t)KEY2TERMCAP1(key); } else if (escape_ks) { - char_u *after = add_char2buf(key, dst + dlen); + char *after = add_char2buf(key, dst + dlen); assert(after >= dst && (uintmax_t)(after - dst) <= UINT_MAX); - dlen = (unsigned int)(after - dst); + dlen = (unsigned)(after - dst); } else { - dlen += (unsigned int)utf_char2bytes(key, (char *)dst + dlen); + dlen += (unsigned)utf_char2bytes(key, dst + dlen); } return dlen; @@ -629,15 +621,9 @@ int find_special_key(const char **const srcp, const size_t src_len, int *const m const int flags, bool *const did_simplify) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1, 3) { - const char *last_dash; - const char *end_of_name; - const char *src; const char *bp; const char *const end = *srcp + src_len - 1; const bool in_string = flags & FSK_IN_STRING; - int modifiers; - int bit; - int key; uvarnumber_T n; int l; @@ -645,7 +631,7 @@ int find_special_key(const char **const srcp, const size_t src_len, int *const m return 0; } - src = *srcp; + const char *src = *srcp; if (src[0] != '<') { return 0; } @@ -654,7 +640,7 @@ int find_special_key(const char **const srcp, const size_t src_len, int *const m } // Find end of modifier list - last_dash = src; + const char *last_dash = src; for (bp = src + 1; bp <= end && (*bp == '-' || ascii_isident(*bp)); bp++) { if (*bp == '-') { last_dash = bp; @@ -674,7 +660,7 @@ int find_special_key(const char **const srcp, const size_t src_len, int *const m if (end - bp > 3 && bp[0] == 't' && bp[1] == '_') { bp += 3; // skip t_xx, xx may be '-' or '>' } else if (end - bp > 4 && STRNICMP(bp, "char-", 5) == 0) { - vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, true); + vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, true, NULL); if (l == 0) { emsg(_(e_invarg)); return 0; @@ -685,13 +671,14 @@ int find_special_key(const char **const srcp, const size_t src_len, int *const m } if (bp <= end && *bp == '>') { // found matching '>' - end_of_name = bp + 1; + int key; + const char *end_of_name = bp + 1; // Which modifiers are given? - modifiers = 0x0; + int modifiers = 0x0; for (bp = src + 1; bp < last_dash; bp++) { if (*bp != '-') { - bit = name_to_mod_mask((uint8_t)(*bp)); + int bit = name_to_mod_mask((uint8_t)(*bp)); if (bit == 0x0) { break; // Illegal modifier name } @@ -704,7 +691,7 @@ int find_special_key(const char **const srcp, const size_t src_len, int *const m if (STRNICMP(last_dash + 1, "char-", 5) == 0 && ascii_isdigit(last_dash[6])) { // <Char-123> or <Char-033> or <Char-0x33> - vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL, &n, 0, true); + vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL, &n, 0, true, NULL); if (l == 0) { emsg(_(e_invarg)); return 0; @@ -723,7 +710,7 @@ int find_special_key(const char **const srcp, const size_t src_len, int *const m if (modifiers != 0 && last_dash[l + 1] == '>') { key = utf_ptr2char(last_dash + off); } else { - key = get_special_key_code((char_u *)last_dash + off); + key = get_special_key_code(last_dash + off); if (!(flags & FSK_KEEP_X_KEY)) { key = handle_x_keys(key); } @@ -822,18 +809,22 @@ int find_special_key_in_table(int c) /// a termcap name. /// /// @return Key code or 0 if not found. -int get_special_key_code(const char_u *name) +int get_special_key_code(const char *name) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { + if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL) { + return TERMCAP2KEY((uint8_t)name[2], (uint8_t)name[3]); + } + for (int i = 0; key_names_table[i].name != NULL; i++) { const char *const table_name = key_names_table[i].name; int j; - for (j = 0; ascii_isident(name[j]) && table_name[j] != NUL; j++) { - if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) { + for (j = 0; ascii_isident((uint8_t)name[j]) && table_name[j] != NUL; j++) { + if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC((uint8_t)name[j])) { break; } } - if (!ascii_isident(name[j]) && table_name[j] == NUL) { + if (!ascii_isident((uint8_t)name[j]) && table_name[j] == NUL) { return key_names_table[i].key; } } @@ -845,9 +836,7 @@ int get_special_key_code(const char_u *name) /// @return which button is down or was released. int get_mouse_button(int code, bool *is_click, bool *is_drag) { - int i; - - for (i = 0; mouse_table[i].pseudo_code; i++) { + for (int i = 0; mouse_table[i].pseudo_code; i++) { if (code == mouse_table[i].pseudo_code) { *is_click = mouse_table[i].is_click; *is_drag = mouse_table[i].is_drag; @@ -873,25 +862,24 @@ int get_mouse_button(int code, bool *is_click, bool *is_drag) /// If `*bufp` is non-NULL, it will be used directly, /// and is assumed to be 128 bytes long (enough for transcoding LHS of mapping), /// and will be set to NULL in case of failure. +/// @param[in] sid_arg Script ID to use for <SID>, or 0 to use current_sctx /// @param[in] flags REPTERM_FROM_PART see above /// REPTERM_DO_LT also translate <lt> /// REPTERM_NO_SPECIAL do not accept <key> notation /// REPTERM_NO_SIMPLIFY do not simplify <C-H> into 0x08, etc. -/// @param[out] did_simplify set when some <C-H> code was simplied, unless it is NULL. +/// @param[out] did_simplify set when some <C-H> code was simplified, unless it is NULL. /// @param[in] cpo_flags Relevant flags derived from p_cpo, see CPO_TO_CPO_FLAGS. /// /// @return The same as what `*bufp` is set to. char *replace_termcodes(const char *const from, const size_t from_len, char **const bufp, - const int flags, bool *const did_simplify, const int cpo_flags) + const scid_T sid_arg, const int flags, bool *const did_simplify, + const int cpo_flags) FUNC_ATTR_NONNULL_ARG(1, 3) { - ssize_t i; - size_t slen; char key; size_t dlen = 0; const char *src; const char *const end = from + from_len - 1; - char *result; // buffer for resulting string const bool do_backslash = !(cpo_flags & FLAG_CPO_BSLASH); // backslash is a special character const bool do_special = !(flags & REPTERM_NO_SPECIAL); @@ -901,23 +889,10 @@ char *replace_termcodes(const char *const from, const size_t from_len, char **co // Allocate space for the translation. Worst case a single character is // replaced by 6 bytes (shifted special key), plus a NUL at the end. const size_t buf_len = allocated ? from_len * 6 + 1 : 128; - result = allocated ? xmalloc(buf_len) : *bufp; + char *result = allocated ? xmalloc(buf_len) : *bufp; // buffer for resulting string src = from; - // Check for #n at start only: function key n - if ((flags & REPTERM_FROM_PART) && from_len > 1 && src[0] == '#' - && ascii_isdigit(src[1])) { // function key - result[dlen++] = (char)K_SPECIAL; - result[dlen++] = 'k'; - if (src[1] == '0') { - result[dlen++] = ';'; // #0 is F10 is "k;" - } else { - result[dlen++] = src[1]; // #3 is F3 is "k3" - } - src += 2; - } - // Copy each byte from *from to result[dlen] while (src <= end) { if (!allocated && dlen + 64 > buf_len) { @@ -926,27 +901,28 @@ char *replace_termcodes(const char *const from, const size_t from_len, char **co // Check for special <> keycodes, like "<C-S-LeftMouse>" if (do_special && ((flags & REPTERM_DO_LT) || ((end - src) >= 3 && strncmp(src, "<lt>", 4) != 0))) { - // Replace <SID> by K_SNR <script-nr> _. + // Change <SID>Func to K_SNR <script-nr> _Func. This name is used + // for script-local user functions. // (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14) if (end - src >= 4 && STRNICMP(src, "<SID>", 5) == 0) { - if (current_sctx.sc_sid <= 0) { + if (sid_arg < 0 || (sid_arg == 0 && current_sctx.sc_sid <= 0)) { emsg(_(e_usingsid)); } else { + const scid_T sid = sid_arg != 0 ? sid_arg : current_sctx.sc_sid; src += 5; result[dlen++] = (char)K_SPECIAL; result[dlen++] = (char)KS_EXTRA; result[dlen++] = KE_SNR; - snprintf(result + dlen, buf_len - dlen, "%" PRId64, - (int64_t)current_sctx.sc_sid); + snprintf(result + dlen, buf_len - dlen, "%" PRId64, (int64_t)sid); dlen += strlen(result + dlen); result[dlen++] = '_'; continue; } } - slen = trans_special(&src, (size_t)(end - src) + 1, result + dlen, - FSK_KEYCODE | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY), - true, did_simplify); + size_t slen = trans_special(&src, (size_t)(end - src) + 1, result + dlen, + FSK_KEYCODE | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY), + true, did_simplify); if (slen) { dlen += slen; continue; @@ -1002,7 +978,7 @@ char *replace_termcodes(const char *const from, const size_t from_len, char **co } // skip multibyte char correctly - for (i = utfc_ptr2len_len((char *)src, (int)(end - src) + 1); i > 0; i--) { + for (ssize_t i = utfc_ptr2len_len(src, (int)(end - src) + 1); i > 0; i--) { // If the character is K_SPECIAL, replace it with K_SPECIAL // KS_SPECIAL KE_FILLER. if (*src == (char)K_SPECIAL) { @@ -1033,20 +1009,20 @@ char *replace_termcodes(const char *const from, const size_t from_len, char **co /// @param[out] s Buffer to add to. Must have at least MB_MAXBYTES + 1 bytes. /// /// @return Pointer to after the added bytes. -char_u *add_char2buf(int c, char_u *s) +char *add_char2buf(int c, char *s) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { - char_u temp[MB_MAXBYTES + 1]; - const int len = utf_char2bytes(c, (char *)temp); + char temp[MB_MAXBYTES + 1]; + const int len = utf_char2bytes(c, temp); for (int i = 0; i < len; i++) { c = (uint8_t)temp[i]; // Need to escape K_SPECIAL like in the typeahead buffer. if (c == K_SPECIAL) { - *s++ = K_SPECIAL; - *s++ = KS_SPECIAL; + *s++ = (char)(uint8_t)K_SPECIAL; + *s++ = (char)(uint8_t)KS_SPECIAL; *s++ = KE_FILLER; } else { - *s++ = (char_u)c; + *s++ = (char)(uint8_t)c; } } return s; @@ -1060,9 +1036,9 @@ char *vim_strsave_escape_ks(char *p) // illegal utf-8 byte: // 0xc0 -> 0xc3 - 0x80 -> 0xc3 K_SPECIAL KS_SPECIAL KE_FILLER char *res = xmalloc(strlen(p) * 4 + 1); - char_u *d = (char_u *)res; - for (char_u *s = (char_u *)p; *s != NUL;) { - if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL) { + char *d = res; + for (char *s = p; *s != NUL;) { + if ((uint8_t)s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL) { // Copy special key unmodified. *d++ = *s++; *d++ = *s++; @@ -1070,8 +1046,8 @@ char *vim_strsave_escape_ks(char *p) } else { // Add character, possibly multi-byte to destination, escaping // K_SPECIAL. Be careful, it can be an illegal byte! - d = add_char2buf(utf_ptr2char((char *)s), d); - s += utf_ptr2len((char *)s); + d = add_char2buf(utf_ptr2char(s), d); + s += utf_ptr2len(s); } } *d = NUL; @@ -1081,9 +1057,9 @@ char *vim_strsave_escape_ks(char *p) /// Remove escaping from K_SPECIAL characters. Reverse of /// vim_strsave_escape_ks(). Works in-place. -void vim_unescape_ks(char_u *p) +void vim_unescape_ks(char *p) { - char_u *s = p, *d = p; + uint8_t *s = (uint8_t *)p, *d = (uint8_t *)p; while (*s != NUL) { if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER) { @@ -1095,15 +1071,3 @@ void vim_unescape_ks(char_u *p) } *d = NUL; } - -/// Logs a single key as a human-readable keycode. -void log_key(int log_level, int key) -{ - if (log_level < MIN_LOG_LEVEL) { - return; - } - char *keyname = key == K_EVENT - ? "K_EVENT" - : (char *)get_special_key_name(key, mod_mask); - LOG(log_level, "input: %s", keyname); -} |