mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 20:08:17 +00:00
message: Refactor str2special_save and str2special
Does not alter their usages as well.
This commit is contained in:
@@ -1281,90 +1281,94 @@ msg_outtrans_special (
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Convert string, replacing key codes with printables
|
||||||
* Return the lhs or rhs of a mapping, with the key codes turned into printable
|
///
|
||||||
* strings, in an allocated string.
|
/// Used for lhs or rhs of mappings.
|
||||||
*/
|
///
|
||||||
char_u *
|
/// @param[in] str String to convert.
|
||||||
str2special_save (
|
/// @param[in] replace_spaces Convert spaces into <Space>, normally used for
|
||||||
char_u *str,
|
/// lhs, but not rhs.
|
||||||
int is_lhs /* TRUE for lhs, FALSE for rhs */
|
///
|
||||||
)
|
/// @return [allocated] Converted string.
|
||||||
|
char *str2special_save(const char *const str, const bool replace_spaces)
|
||||||
|
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_MALLOC
|
||||||
|
FUNC_ATTR_NONNULL_RET
|
||||||
{
|
{
|
||||||
garray_T ga;
|
garray_T ga;
|
||||||
char_u *p = str;
|
|
||||||
|
|
||||||
ga_init(&ga, 1, 40);
|
ga_init(&ga, 1, 40);
|
||||||
while (*p != NUL)
|
|
||||||
ga_concat(&ga, str2special(&p, is_lhs));
|
const char *p = str;
|
||||||
|
while (*p != NUL) {
|
||||||
|
ga_concat(&ga, (const char_u *)str2special(&p, replace_spaces));
|
||||||
|
}
|
||||||
ga_append(&ga, NUL);
|
ga_append(&ga, NUL);
|
||||||
return (char_u *)ga.ga_data;
|
return (char *)ga.ga_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Convert character, replacing key one key code with printable representation
|
||||||
* Return the printable string for the key codes at "*sp".
|
///
|
||||||
* Used for translating the lhs or rhs of a mapping to printable chars.
|
/// @param[in,out] sp String to convert. Is advanced to the next key code.
|
||||||
* Advances "sp" to the next code.
|
/// @param[in] replace_spaces Convert spaces into <Space>, normally used for
|
||||||
*/
|
/// lhs, but not rhs.
|
||||||
char_u *
|
///
|
||||||
str2special (
|
/// @return Converted key code, in a static buffer. Buffer is always one and the
|
||||||
char_u **sp,
|
/// same, so save converted string somewhere before running str2special
|
||||||
int from /* TRUE for lhs of mapping */
|
/// for the second time.
|
||||||
)
|
const char *str2special(const char **const sp, const bool replace_spaces)
|
||||||
|
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET
|
||||||
{
|
{
|
||||||
int c;
|
static char buf[7];
|
||||||
static char_u buf[7];
|
|
||||||
char_u *str = *sp;
|
|
||||||
int modifiers = 0;
|
|
||||||
int special = FALSE;
|
|
||||||
|
|
||||||
if (has_mbyte) {
|
// Try to un-escape a multi-byte character. Return the un-escaped
|
||||||
char_u *p;
|
// string if it is a multi-byte character.
|
||||||
|
const char *const p = mb_unescape(sp);
|
||||||
/* Try to un-escape a multi-byte character. Return the un-escaped
|
if (p != NULL) {
|
||||||
* string if it is a multi-byte character. */
|
|
||||||
p = mb_unescape(sp);
|
|
||||||
if (p != NULL)
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
c = *str;
|
const char *str = *sp;
|
||||||
|
int c = (uint8_t)(*str);
|
||||||
|
int modifiers = 0;
|
||||||
|
bool special = false;
|
||||||
if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL) {
|
if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL) {
|
||||||
if (str[1] == KS_MODIFIER) {
|
if ((uint8_t)str[1] == KS_MODIFIER) {
|
||||||
modifiers = str[2];
|
modifiers = (uint8_t)str[2];
|
||||||
str += 3;
|
str += 3;
|
||||||
c = *str;
|
c = (uint8_t)(*str);
|
||||||
}
|
}
|
||||||
if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL) {
|
if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL) {
|
||||||
c = TO_SPECIAL(str[1], str[2]);
|
c = TO_SPECIAL((uint8_t)str[1], (uint8_t)str[2]);
|
||||||
str += 2;
|
str += 2;
|
||||||
if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
|
if (c == KS_ZERO) { // display <Nul> as ^@ or <Nul>
|
||||||
c = NUL;
|
c = NUL;
|
||||||
}
|
}
|
||||||
if (IS_SPECIAL(c) || modifiers) /* special key */
|
}
|
||||||
special = TRUE;
|
if (IS_SPECIAL(c) || modifiers) { // Special key.
|
||||||
|
special = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (has_mbyte && !IS_SPECIAL(c)) {
|
if (!IS_SPECIAL(c)) {
|
||||||
int len = (*mb_ptr2len)(str);
|
const int len = utf_ptr2len((const char_u *)str);
|
||||||
|
|
||||||
/* For multi-byte characters check for an illegal byte. */
|
// Check for an illegal byte.
|
||||||
if (has_mbyte && MB_BYTE2LEN(*str) > len) {
|
if (MB_BYTE2LEN((uint8_t)(*str)) > len) {
|
||||||
transchar_nonprint(buf, c);
|
transchar_nonprint((char_u *)buf, c);
|
||||||
*sp = str + 1;
|
*sp = str + 1;
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
/* Since 'special' is TRUE the multi-byte character 'c' will be
|
// Since 'special' is TRUE the multi-byte character 'c' will be
|
||||||
* processed by get_special_key_name() */
|
// processed by get_special_key_name().
|
||||||
c = (*mb_ptr2char)(str);
|
c = utf_ptr2char((const char_u *)str);
|
||||||
*sp = str + len;
|
*sp = str + len;
|
||||||
} else
|
} else {
|
||||||
*sp = str + 1;
|
*sp = str + 1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Make unprintable characters in <> form, also <M-Space> and <Tab>.
|
// Make unprintable characters in <> form, also <M-Space> and <Tab>.
|
||||||
* Use <Space> only for lhs of a mapping. */
|
if (special || char2cells(c) > 1 || (replace_spaces && c == ' ')) {
|
||||||
if (special || char2cells(c) > 1 || (from && c == ' '))
|
return (const char *)get_special_key_name(c, modifiers);
|
||||||
return get_special_key_name(c, modifiers);
|
}
|
||||||
buf[0] = c;
|
buf[0] = c;
|
||||||
buf[1] = NUL;
|
buf[1] = NUL;
|
||||||
return buf;
|
return buf;
|
||||||
|
Reference in New Issue
Block a user