message: Add support for replacing < to str2special

This commit is contained in:
ZyX
2017-07-02 19:37:21 +03:00
parent a1fee487ba
commit 24f0056ca5
4 changed files with 26 additions and 18 deletions

View File

@@ -12120,7 +12120,7 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact)
// Return a string.
if (rhs != NULL) {
rettv->vval.v_string = (char_u *)str2special_save(
(const char *)rhs, false);
(const char *)rhs, false, false);
}
} else {
@@ -12157,7 +12157,7 @@ void mapblock_fill_dict(dict_T *const dict,
FUNC_ATTR_NONNULL_ALL
{
char *const lhs = str2special_save((const char *)mp->m_keys,
compatible ? true : false);
compatible, !compatible);
char *const mapmode = map_mode_to_chars(mp->m_mode);
varnumber_T noremap_value;
@@ -12175,7 +12175,8 @@ void mapblock_fill_dict(dict_T *const dict,
tv_dict_add_str(dict, S_LEN("rhs"), (const char *)mp->m_orig_str);
} else {
tv_dict_add_allocated_str(dict, S_LEN("rhs"),
str2special_save((const char *)mp->m_str, false));
str2special_save((const char *)mp->m_str, false,
true));
}
tv_dict_add_allocated_str(dict, S_LEN("lhs"), lhs);
tv_dict_add_nr(dict, S_LEN("noremap"), noremap_value);

View File

@@ -1269,7 +1269,7 @@ msg_outtrans_special (
string = "<Space>";
str++;
} else {
string = str2special((const char **)&str, from);
string = str2special((const char **)&str, from, false);
}
const int len = vim_strsize((char_u *)string);
// Highlight special keys
@@ -1286,11 +1286,13 @@ msg_outtrans_special (
/// Used for lhs or rhs of mappings.
///
/// @param[in] str String to convert.
/// @param[in] replace_spaces Convert spaces into <Space>, normally used for
/// @param[in] replace_spaces Convert spaces into `<Space>`, normally used fo
/// lhs, but not rhs.
/// @param[in] replace_lt Convert `<` into `<lt>`.
///
/// @return [allocated] Converted string.
char *str2special_save(const char *const str, const bool replace_spaces)
char *str2special_save(const char *const str, const bool replace_spaces,
const bool replace_lt)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_MALLOC
FUNC_ATTR_NONNULL_RET
{
@@ -1299,7 +1301,7 @@ char *str2special_save(const char *const str, const bool replace_spaces)
const char *p = str;
while (*p != NUL) {
ga_concat(&ga, (const char_u *)str2special(&p, replace_spaces));
ga_concat(&ga, (const char_u *)str2special(&p, replace_spaces, replace_lt));
}
ga_append(&ga, NUL);
return (char *)ga.ga_data;
@@ -1310,11 +1312,13 @@ char *str2special_save(const char *const str, const bool replace_spaces)
/// @param[in,out] sp String to convert. Is advanced to the next key code.
/// @param[in] replace_spaces Convert spaces into <Space>, normally used for
/// lhs, but not rhs.
/// @param[in] replace_lt Convert `<` into `<lt>`.
///
/// @return Converted key code, in a static buffer. Buffer is always one and the
/// same, so save converted string somewhere before running str2special
/// for the second time.
const char *str2special(const char **const sp, const bool replace_spaces)
const char *str2special(const char **const sp, const bool replace_spaces,
const bool replace_lt)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET
{
static char buf[7];
@@ -1366,7 +1370,10 @@ const char *str2special(const char **const sp, const bool replace_spaces)
}
// Make unprintable characters in <> form, also <M-Space> and <Tab>.
if (special || char2cells(c) > 1 || (replace_spaces && c == ' ')) {
if (special
|| char2cells(c) > 1
|| (replace_spaces && c == ' ')
|| (replace_lt && c == '<')) {
return (const char *)get_special_key_name(c, modifiers);
}
buf[0] = c;
@@ -1383,7 +1390,7 @@ void str2specialbuf(const char *sp, char *buf, size_t len)
FUNC_ATTR_NONNULL_ALL
{
while (*sp) {
const char *s = str2special(&sp, false);
const char *s = str2special(&sp, false, false);
const size_t s_len = strlen(s);
if (s_len <= len) {
break;

View File

@@ -5176,7 +5176,8 @@ static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, int e
if (valuep == &p_pt) {
s = *valuep;
while (*s != NUL) {
if (put_escstr(fd, (char_u *)str2special((const char **)&s, false), 2)
if (put_escstr(fd, (char_u *)str2special((const char **)&s, false,
false), 2)
== FAIL) {
return FAIL;
}

View File

@@ -281,13 +281,12 @@ describe('get_keymap', function()
command(cmd)
eq({cpomap('\\<C-C>', '\\<C-D>', 'n'), cpomap('\\<C-A>', '\\<C-B>', 'n')},
meths.get_keymap('n'))
-- FIXME
-- eq({cpomap('\\<C-C>', '\\<C-D>', 'x'), cpomap('\\<LT>C-A>', '\\<LT>C-B>', 'x')},
-- meths.get_keymap('x'))
-- eq({cpomap('<LT>C-C>', '<LT>C-D>', 's'), cpomap('<LT>C-A>', '<LT>C-B>', 's')},
-- meths.get_keymap('x'))
-- eq({cpomap('<LT>C-C>', '<LT>C-D>', 'o'), cpomap('<LT>C-A>', '<LT>C-B>', 'o')},
-- meths.get_keymap('x'))
eq({cpomap('\\<C-C>', '\\<C-D>', 'x'), cpomap('\\<lt>C-a>', '\\<lt>C-b>', 'x')},
meths.get_keymap('x'))
eq({cpomap('<lt>C-c>', '<lt>C-d>', 's'), cpomap('<lt>C-a>', '<lt>C-b>', 's')},
meths.get_keymap('s'))
eq({cpomap('<lt>C-c>', '<lt>C-d>', 'o'), cpomap('<lt>C-a>', '<lt>C-b>', 'o')},
meths.get_keymap('o'))
end
end)
end)