refactor: remove CSI unescaping and clean up related names and comments

This commit is contained in:
zeertzjq 2022-01-21 18:08:56 +08:00
parent 8300d337c8
commit 6e69a3c3e7
10 changed files with 107 additions and 155 deletions

View File

@ -187,21 +187,23 @@ static void on_redraw_event(void **argv)
/// On execution error: does not fail, but updates v:errmsg. /// On execution error: does not fail, but updates v:errmsg.
/// ///
/// To input sequences like <C-o> use |nvim_replace_termcodes()| (typically /// To input sequences like <C-o> use |nvim_replace_termcodes()| (typically
/// with escape_csi=true) to replace |keycodes|, then pass the result to /// with escape_ks=false) to replace |keycodes|, then pass the result to
/// nvim_feedkeys(). /// nvim_feedkeys().
/// ///
/// Example: /// Example:
/// <pre> /// <pre>
/// :let key = nvim_replace_termcodes("<C-o>", v:true, v:false, v:true) /// :let key = nvim_replace_termcodes("<C-o>", v:true, v:false, v:true)
/// :call nvim_feedkeys(key, 'n', v:true) /// :call nvim_feedkeys(key, 'n', v:false)
/// </pre> /// </pre>
/// ///
/// @param keys to be typed /// @param keys to be typed
/// @param mode behavior flags, see |feedkeys()| /// @param mode behavior flags, see |feedkeys()|
/// @param escape_csi If true, escape K_SPECIAL/CSI bytes in `keys` /// @param escape_ks If true, escape K_SPECIAL bytes in `keys`
/// This should be false if you already used
/// |nvim_replace_termcodes()|, and true otherwise.
/// @see feedkeys() /// @see feedkeys()
/// @see vim_strsave_escape_csi /// @see vim_strsave_escape_ks
void nvim_feedkeys(String keys, String mode, Boolean escape_csi) void nvim_feedkeys(String keys, String mode, Boolean escape_ks)
FUNC_API_SINCE(1) FUNC_API_SINCE(1)
{ {
bool remap = true; bool remap = true;
@ -232,10 +234,10 @@ void nvim_feedkeys(String keys, String mode, Boolean escape_csi)
} }
char *keys_esc; char *keys_esc;
if (escape_csi) { if (escape_ks) {
// Need to escape K_SPECIAL and CSI before putting the string in the // Need to escape K_SPECIAL before putting the string in the
// typeahead buffer. // typeahead buffer.
keys_esc = (char *)vim_strsave_escape_csi((char_u *)keys.data); keys_esc = (char *)vim_strsave_escape_ks((char_u *)keys.data);
} else { } else {
keys_esc = keys.data; keys_esc = keys.data;
} }
@ -245,7 +247,7 @@ void nvim_feedkeys(String keys, String mode, Boolean escape_csi)
typebuf_was_filled = true; typebuf_was_filled = true;
} }
if (escape_csi) { if (escape_ks) {
xfree(keys_esc); xfree(keys_esc);
} }

View File

@ -260,7 +260,7 @@ static colnr_T Insstart_blank_vcol; // vcol for first inserted blank
static bool update_Insstart_orig = true; // set Insstart_orig to Insstart static bool update_Insstart_orig = true; // set Insstart_orig to Insstart
static char_u *last_insert = NULL; // the text of the previous insert, static char_u *last_insert = NULL; // the text of the previous insert,
// K_SPECIAL and CSI are escaped // K_SPECIAL is escaped
static int last_insert_skip; // nr of chars in front of previous insert static int last_insert_skip; // nr of chars in front of previous insert
static int new_insert_skip; // nr of chars in front of current insert static int new_insert_skip; // nr of chars in front of current insert
static int did_restart_edit; // "restart_edit" when calling edit() static int did_restart_edit; // "restart_edit" when calling edit()
@ -6817,7 +6817,7 @@ void free_last_insert(void)
/// Add character "c" to buffer "s" /// Add character "c" to buffer "s"
/// ///
/// Escapes the special meaning of K_SPECIAL and CSI, handles multi-byte /// Escapes the special meaning of K_SPECIAL, handles multi-byte
/// characters. /// characters.
/// ///
/// @param[in] c Character to add. /// @param[in] c Character to add.
@ -6831,7 +6831,7 @@ char_u *add_char2buf(int c, char_u *s)
const int len = utf_char2bytes(c, temp); const int len = utf_char2bytes(c, temp);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
c = temp[i]; c = temp[i];
// Need to escape K_SPECIAL and CSI like in the typeahead buffer. // Need to escape K_SPECIAL like in the typeahead buffer.
if (c == K_SPECIAL) { if (c == K_SPECIAL) {
*s++ = K_SPECIAL; *s++ = K_SPECIAL;
*s++ = KS_SPECIAL; *s++ = KS_SPECIAL;

View File

@ -6204,7 +6204,6 @@ static void do_ucmd(exarg_T *eap)
// K_SPECIAL has been put in the buffer as K_SPECIAL // K_SPECIAL has been put in the buffer as K_SPECIAL
// KS_SPECIAL KE_FILLER, like for mappings, but // KS_SPECIAL KE_FILLER, like for mappings, but
// do_cmdline() doesn't handle that, so convert it back. // do_cmdline() doesn't handle that, so convert it back.
// Also change K_SPECIAL KS_EXTRA KE_CSI into CSI.
len = ksp - p; len = ksp - p;
if (len > 0) { if (len > 0) {
memmove(q, p, len); memmove(q, p, len);
@ -8627,7 +8626,7 @@ static void ex_normal(exarg_T *eap)
return; return;
} }
// vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do // vgetc() expects K_SPECIAL to have been escaped. Don't do
// this for the K_SPECIAL leading byte, otherwise special keys will not // this for the K_SPECIAL leading byte, otherwise special keys will not
// work. // work.
{ {
@ -8636,8 +8635,7 @@ static void ex_normal(exarg_T *eap)
// Count the number of characters to be escaped. // Count the number of characters to be escaped.
for (p = eap->arg; *p != NUL; p++) { for (p = eap->arg; *p != NUL; p++) {
for (l = utfc_ptr2len(p) - 1; l > 0; l--) { for (l = utfc_ptr2len(p) - 1; l > 0; l--) {
if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI if (*++p == K_SPECIAL) { // trailbyte K_SPECIAL
) {
len += 2; len += 2;
} }
} }

View File

@ -59,25 +59,18 @@
static int curscript = 0; static int curscript = 0;
FileDescriptor *scriptin[NSCRIPT] = { NULL }; FileDescriptor *scriptin[NSCRIPT] = { NULL };
/* // These buffers are used for storing:
* These buffers are used for storing: // - stuffed characters: A command that is translated into another command.
* - stuffed characters: A command that is translated into another command. // - redo characters: will redo the last change.
* - redo characters: will redo the last change. // - recorded characters: for the "q" command.
* - recorded characters: for the "q" command. //
* // The bytes are stored like in the typeahead buffer:
* The bytes are stored like in the typeahead buffer: // - K_SPECIAL introduces a special key (two more bytes follow). A literal
* - K_SPECIAL introduces a special key (two more bytes follow). A literal // K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
* K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER. // These translations are also done on multi-byte characters!
* - CSI introduces a GUI termcap code (also when gui.in_use is FALSE, //
* otherwise switching the GUI on would make mappings invalid). // Escaping K_SPECIAL is done by inchar().
* A literal CSI is stored as CSI KS_EXTRA KE_CSI. // Un-escaping is done by vgetc().
* These translations are also done on multi-byte characters!
*
* Escaping CSI bytes is done by the system-specific input functions, called
* by ui_inchar().
* Escaping K_SPECIAL is done by inchar().
* Un-escaping is done by vgetc().
*/
#define MINIMAL_SIZE 20 // minimal size for b_str #define MINIMAL_SIZE 20 // minimal size for b_str
@ -173,7 +166,7 @@ void free_buff(buffheader_T *buf)
} }
/// Return the contents of a buffer as a single string. /// Return the contents of a buffer as a single string.
/// K_SPECIAL and CSI in the returned string are escaped. /// K_SPECIAL in the returned string is escaped.
/// ///
/// @param dozero count == zero is not an error /// @param dozero count == zero is not an error
static char_u *get_buffcont(buffheader_T *buffer, int dozero) static char_u *get_buffcont(buffheader_T *buffer, int dozero)
@ -202,11 +195,9 @@ static char_u *get_buffcont(buffheader_T *buffer, int dozero)
return p; return p;
} }
/* /// Return the contents of the record buffer as a single string
* Return the contents of the record buffer as a single string /// and clear the record buffer.
* and clear the record buffer. /// K_SPECIAL in the returned string is escaped.
* K_SPECIAL and CSI in the returned string are escaped.
*/
char_u *get_recorded(void) char_u *get_recorded(void)
{ {
char_u *p; char_u *p;
@ -236,10 +227,8 @@ char_u *get_recorded(void)
return p; return p;
} }
/* /// Return the contents of the redo buffer as a single string.
* Return the contents of the redo buffer as a single string. /// K_SPECIAL in the returned string is escaped.
* K_SPECIAL and CSI in the returned string are escaped.
*/
char_u *get_inserted(void) char_u *get_inserted(void)
{ {
return get_buffcont(&redobuff, FALSE); return get_buffcont(&redobuff, FALSE);
@ -247,7 +236,7 @@ char_u *get_inserted(void)
/// Add string after the current block of the given buffer /// Add string after the current block of the given buffer
/// ///
/// K_SPECIAL and CSI should have been escaped already. /// K_SPECIAL should have been escaped already.
/// ///
/// @param[out] buf Buffer to add to. /// @param[out] buf Buffer to add to.
/// @param[in] s String to add. /// @param[in] s String to add.
@ -305,10 +294,8 @@ static void add_num_buff(buffheader_T *buf, long n)
add_buff(buf, number, -1L); add_buff(buf, number, -1L);
} }
/* /// Add character 'c' to buffer "buf".
* Add character 'c' to buffer "buf". /// Translates special keys, NUL, K_SPECIAL and multibyte characters.
* Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
*/
static void add_char_buff(buffheader_T *buf, int c) static void add_char_buff(buffheader_T *buf, int c)
{ {
uint8_t bytes[MB_MAXBYTES + 1]; uint8_t bytes[MB_MAXBYTES + 1];
@ -340,12 +327,10 @@ static void add_char_buff(buffheader_T *buf, int c)
} }
} }
/* /// Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
* Get one byte from the read buffers. Use readbuf1 one first, use readbuf2 /// if that one is empty.
* if that one is empty. /// If advance == TRUE go to the next char.
* If advance == TRUE go to the next char. /// No translation is done K_SPECIAL is escaped.
* No translation is done K_SPECIAL and CSI are escaped.
*/
static int read_readbuffers(int advance) static int read_readbuffers(int advance)
{ {
int c; int c;
@ -524,10 +509,8 @@ void restoreRedobuff(save_redo_T *save_redo)
old_redobuff = save_redo->sr_old_redobuff; old_redobuff = save_redo->sr_old_redobuff;
} }
/* /// Append "s" to the redo buffer.
* Append "s" to the redo buffer. /// K_SPECIAL should already have been escaped.
* K_SPECIAL and CSI should already have been escaped.
*/
void AppendToRedobuff(const char *s) void AppendToRedobuff(const char *s)
{ {
if (!block_redo) { if (!block_redo) {
@ -536,7 +519,7 @@ void AppendToRedobuff(const char *s)
} }
/// Append to Redo buffer literally, escaping special characters with CTRL-V. /// Append to Redo buffer literally, escaping special characters with CTRL-V.
/// K_SPECIAL and CSI are escaped as well. /// K_SPECIAL is escaped as well.
/// ///
/// @param str String to append /// @param str String to append
/// @param len Length of `str` or -1 for up to the NUL. /// @param len Length of `str` or -1 for up to the NUL.
@ -584,10 +567,8 @@ void AppendToRedobuffLit(const char_u *str, int len)
} }
} }
/* /// Append a character to the redo buffer.
* Append a character to the redo buffer. /// Translates special keys, NUL, K_SPECIAL and multibyte characters.
* Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
*/
void AppendCharToRedobuff(int c) void AppendCharToRedobuff(int c)
{ {
if (!block_redo) { if (!block_redo) {
@ -605,17 +586,15 @@ void AppendNumberToRedobuff(long n)
} }
} }
/* /// Append string "s" to the stuff buffer.
* Append string "s" to the stuff buffer. /// K_SPECIAL must already have been escaped.
* CSI and K_SPECIAL must already have been escaped.
*/
void stuffReadbuff(const char *s) void stuffReadbuff(const char *s)
{ {
add_buff(&readbuf1, s, -1L); add_buff(&readbuf1, s, -1L);
} }
/// Append string "s" to the redo stuff buffer. /// Append string "s" to the redo stuff buffer.
/// @remark CSI and K_SPECIAL must already have been escaped. /// @remark K_SPECIAL must already have been escaped.
void stuffRedoReadbuff(const char *s) void stuffRedoReadbuff(const char *s)
{ {
add_buff(&readbuf2, s, -1L); add_buff(&readbuf2, s, -1L);
@ -626,11 +605,9 @@ void stuffReadbuffLen(const char *s, long len)
add_buff(&readbuf1, s, len); add_buff(&readbuf1, s, len);
} }
/* /// Stuff "s" into the stuff buffer, leaving special key codes unmodified and
* Stuff "s" into the stuff buffer, leaving special key codes unmodified and /// escaping other K_SPECIAL bytes.
* escaping other K_SPECIAL and CSI bytes. /// Change CR, LF and ESC into a space.
* Change CR, LF and ESC into a space.
*/
void stuffReadbuffSpec(const char *s) void stuffReadbuffSpec(const char *s)
{ {
while (*s != NUL) { while (*s != NUL) {
@ -648,10 +625,8 @@ void stuffReadbuffSpec(const char *s)
} }
} }
/* /// Append a character to the stuff buffer.
* Append a character to the stuff buffer. /// Translates special keys, NUL, K_SPECIAL and multibyte characters.
* Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
*/
void stuffcharReadbuff(int c) void stuffcharReadbuff(int c)
{ {
add_char_buff(&readbuf1, c); add_char_buff(&readbuf1, c);
@ -665,12 +640,12 @@ void stuffnumReadbuff(long n)
add_num_buff(&readbuf1, n); add_num_buff(&readbuf1, n);
} }
// Read a character from the redo buffer. Translates K_SPECIAL, CSI and /// Read a character from the redo buffer. Translates K_SPECIAL and
// multibyte characters. /// multibyte characters.
// The redo buffer is left as it is. /// The redo buffer is left as it is.
// If init is true, prepare for redo, return FAIL if nothing to redo, OK /// If init is true, prepare for redo, return FAIL if nothing to redo, OK
// otherwise. /// otherwise.
// If old_redo is true, use old_redobuff instead of redobuff. /// If old_redo is true, use old_redobuff instead of redobuff.
static int read_redo(bool init, bool old_redo) static int read_redo(bool init, bool old_redo)
{ {
static buffblock_T *bp; static buffblock_T *bp;
@ -724,9 +699,9 @@ static int read_redo(bool init, bool old_redo)
return c; return c;
} }
// Copy the rest of the redo buffer into the stuff buffer (in a slow way). /// Copy the rest of the redo buffer into the stuff buffer (in a slow way).
// If old_redo is true, use old_redobuff instead of redobuff. /// If old_redo is true, use old_redobuff instead of redobuff.
// The escaped K_SPECIAL and CSI are copied without translation. /// The escaped K_SPECIAL is copied without translation.
static void copy_redo(bool old_redo) static void copy_redo(bool old_redo)
{ {
int c; int c;
@ -1426,15 +1401,13 @@ static void updatescript(int c)
} }
} }
/* /// Get the next input character.
* Get the next input character. /// Can return a special key or a multi-byte character.
* Can return a special key or a multi-byte character. /// Can return NUL when called recursively, use safe_vgetc() if that's not
* Can return NUL when called recursively, use safe_vgetc() if that's not /// wanted.
* wanted. /// This translates escaped K_SPECIAL bytes to a K_SPECIAL byte.
* This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte. /// Collects the bytes of a multibyte character into the whole character.
* Collects the bytes of a multibyte character into the whole character. /// Returns the modifiers in the global "mod_mask".
* Returns the modifiers in the global "mod_mask".
*/
int vgetc(void) int vgetc(void)
{ {
int c, c2; int c, c2;
@ -1571,14 +1544,9 @@ int vgetc(void)
buf[i] = (char_u)vgetorpeek(true); buf[i] = (char_u)vgetorpeek(true);
if (buf[i] == K_SPECIAL) { if (buf[i] == K_SPECIAL) {
// Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence, // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence,
// which represents a K_SPECIAL (0x80), // which represents a K_SPECIAL (0x80).
// or a CSI - KS_EXTRA - KE_CSI sequence, which represents (void)vgetorpeek(true); // skip KS_SPECIAL
// a CSI (0x9B), (void)vgetorpeek(true); // skip KE_FILLER
// of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too.
c = vgetorpeek(true);
if (vgetorpeek(true) == KE_CSI && c == KS_EXTRA) {
buf[i] = CSI;
}
} }
} }
no_mapping--; no_mapping--;
@ -2042,7 +2010,7 @@ void vungetc(int c)
/// ///
/// When `no_mapping` (global) is zero, checks for mappings in the current mode. /// When `no_mapping` (global) is zero, checks for mappings in the current mode.
/// Only returns one byte (of a multi-byte character). /// Only returns one byte (of a multi-byte character).
/// K_SPECIAL and CSI may be escaped, need to get two more bytes then. /// K_SPECIAL may be escaped, need to get two more bytes then.
static int vgetorpeek(bool advance) static int vgetorpeek(bool advance)
{ {
int c, c1; int c, c1;
@ -2572,7 +2540,7 @@ int fix_input_buffer(char_u *buf, int len)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
if (!using_script()) { if (!using_script()) {
// Should not escape K_SPECIAL/CSI reading input from the user because vim // Should not escape K_SPECIAL reading input from the user because vim
// key codes keys are processed in input.c/input_enqueue. // key codes keys are processed in input.c/input_enqueue.
buf[len] = NUL; buf[len] = NUL;
return len; return len;
@ -2585,7 +2553,6 @@ int fix_input_buffer(char_u *buf, int len)
// Two characters are special: NUL and K_SPECIAL. // Two characters are special: NUL and K_SPECIAL.
// Replace NUL by K_SPECIAL KS_ZERO KE_FILLER // Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
// Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER // Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
// Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
for (i = len; --i >= 0; ++p) { for (i = len; --i >= 0; ++p) {
if (p[0] == NUL if (p[0] == NUL
|| (p[0] == K_SPECIAL || (p[0] == K_SPECIAL
@ -3476,10 +3443,10 @@ static void showmap(mapblock_T *mp, bool local)
} else if (mp->m_str == NULL) { } else if (mp->m_str == NULL) {
msg_puts_attr("<Nop>", HL_ATTR(HLF_8)); msg_puts_attr("<Nop>", HL_ATTR(HLF_8));
} else { } else {
// Remove escaping of CSI, because "m_str" is in a format to be used // Remove escaping of K_SPECIAL, because "m_str" is in a format to be used
// as typeahead. // as typeahead.
char_u *s = vim_strsave(mp->m_str); char_u *s = vim_strsave(mp->m_str);
vim_unescape_csi(s); vim_unescape_ks(s);
msg_outtrans_special(s, false, 0); msg_outtrans_special(s, false, 0);
xfree(s); xfree(s);
} }
@ -3859,9 +3826,9 @@ bool check_abbr(int c, char_u *ptr, int col, int mincol)
int match; int match;
if (strchr((const char *)mp->m_keys, K_SPECIAL) != NULL) { if (strchr((const char *)mp->m_keys, K_SPECIAL) != NULL) {
// Might have CSI escaped mp->m_keys. // Might have K_SPECIAL escaped mp->m_keys.
q = vim_strsave(mp->m_keys); q = vim_strsave(mp->m_keys);
vim_unescape_csi(q); vim_unescape_ks(q);
qlen = (int)STRLEN(q); qlen = (int)STRLEN(q);
} }
// find entries with right mode and keys // find entries with right mode and keys
@ -3907,7 +3874,7 @@ bool check_abbr(int c, char_u *ptr, int col, int mincol)
int newlen = utf_char2bytes(c, tb + j); int newlen = utf_char2bytes(c, tb + j);
tb[j + newlen] = NUL; tb[j + newlen] = NUL;
// Need to escape K_SPECIAL. // Need to escape K_SPECIAL.
char_u *escaped = vim_strsave_escape_csi(tb + j); char_u *escaped = vim_strsave_escape_ks(tb + j);
if (escaped != NULL) { if (escaped != NULL) {
newlen = (int)STRLEN(escaped); newlen = (int)STRLEN(escaped);
memmove(tb + j, escaped, (size_t)newlen); memmove(tb + j, escaped, (size_t)newlen);
@ -3960,11 +3927,11 @@ static char_u *eval_map_expr(mapblock_T *mp, int c)
int save_msg_col; int save_msg_col;
int save_msg_row; int save_msg_row;
/* Remove escaping of CSI, because "str" is in a format to be used as // Remove escaping of K_SPECIAL, because "str" is in a format to be used as
* typeahead. */ // typeahead.
if (mp->m_luaref == LUA_NOREF) { if (mp->m_luaref == LUA_NOREF) {
expr = vim_strsave(mp->m_str); expr = vim_strsave(mp->m_str);
vim_unescape_csi(expr); vim_unescape_ks(expr);
} }
save_cmd = save_cmdline_alloc(); save_cmd = save_cmdline_alloc();
@ -4004,18 +3971,16 @@ static char_u *eval_map_expr(mapblock_T *mp, int c)
if (p == NULL) { if (p == NULL) {
return NULL; return NULL;
} }
// Escape CSI in the result to be able to use the string as typeahead. // Escape K_SPECIAL in the result to be able to use the string as typeahead.
res = vim_strsave_escape_csi(p); res = vim_strsave_escape_ks(p);
xfree(p); xfree(p);
return res; return res;
} }
/* /// Copy "p" to allocated memory, escaping K_SPECIAL so that the result
* Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result /// can be put in the typeahead buffer.
* can be put in the typeahead buffer. char_u *vim_strsave_escape_ks(char_u *p)
*/
char_u *vim_strsave_escape_csi(char_u *p)
{ {
// Need a buffer to hold up to three times as much. Four in case of an // Need a buffer to hold up to three times as much. Four in case of an
// illegal utf-8 byte: // illegal utf-8 byte:
@ -4030,7 +3995,7 @@ char_u *vim_strsave_escape_csi(char_u *p)
*d++ = *s++; *d++ = *s++;
} else { } else {
// Add character, possibly multi-byte to destination, escaping // Add character, possibly multi-byte to destination, escaping
// CSI and K_SPECIAL. Be careful, it can be an illegal byte! // K_SPECIAL. Be careful, it can be an illegal byte!
d = add_char2buf(utf_ptr2char(s), d); d = add_char2buf(utf_ptr2char(s), d);
s += utf_ptr2len(s); s += utf_ptr2len(s);
} }
@ -4040,11 +4005,9 @@ char_u *vim_strsave_escape_csi(char_u *p)
return res; return res;
} }
/* /// Remove escaping from K_SPECIAL characters. Reverse of
* Remove escaping from CSI and K_SPECIAL characters. Reverse of /// vim_strsave_escape_ks(). Works in-place.
* vim_strsave_escape_csi(). Works in-place. void vim_unescape_ks(char_u *p)
*/
void vim_unescape_csi(char_u *p)
{ {
char_u *s = p, *d = p; char_u *s = p, *d = p;
@ -4052,10 +4015,6 @@ void vim_unescape_csi(char_u *p)
if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER) { if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER) {
*d++ = K_SPECIAL; *d++ = K_SPECIAL;
s += 3; s += 3;
} else if ((s[0] == K_SPECIAL || s[0] == CSI)
&& s[1] == KS_EXTRA && s[2] == (int)KE_CSI) {
*d++ = CSI;
s += 3;
} else { } else {
*d++ = *s++; *d++ = *s++;
} }
@ -4298,7 +4257,7 @@ int put_escstr(FILE *fd, char_u *strstart, int what)
for (; *str != NUL; str++) { for (; *str != NUL; str++) {
// Check for a multi-byte character, which may contain escaped // Check for a multi-byte character, which may contain escaped
// K_SPECIAL and CSI bytes. // K_SPECIAL bytes.
const char *p = mb_unescape((const char **)&str); const char *p = mb_unescape((const char **)&str);
if (p != NULL) { if (p != NULL) {
while (*p != NUL) { while (*p != NUL) {

View File

@ -105,7 +105,7 @@ int get_keystroke(MultiQueue *events)
// terminal code to complete. // terminal code to complete.
n = os_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0, events); n = os_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0, events);
if (n > 0) { if (n > 0) {
// Replace zero and CSI by a special key code. // Replace zero and K_SPECIAL by a special key code.
n = fix_input_buffer(buf + len, n); n = fix_input_buffer(buf + len, n);
len += n; len += n;
waited = 0; waited = 0;

View File

@ -963,7 +963,6 @@ char_u *replace_termcodes(const char_u *from, const size_t from_len, char_u **bu
for (i = utfc_ptr2len_len(src, (int)(end - src) + 1); i > 0; i--) { for (i = utfc_ptr2len_len(src, (int)(end - src) + 1); i > 0; i--) {
// If the character is K_SPECIAL, replace it with K_SPECIAL // If the character is K_SPECIAL, replace it with K_SPECIAL
// KS_SPECIAL KE_FILLER. // KS_SPECIAL KE_FILLER.
// If compiled with the GUI replace CSI with K_CSI.
if (*src == K_SPECIAL) { if (*src == K_SPECIAL) {
result[dlen++] = K_SPECIAL; result[dlen++] = K_SPECIAL;
result[dlen++] = KS_SPECIAL; result[dlen++] = KS_SPECIAL;

View File

@ -220,7 +220,7 @@ enum key_extra {
KE_KINS = 79, // keypad Insert key KE_KINS = 79, // keypad Insert key
KE_KDEL = 80, // keypad Delete key KE_KDEL = 80, // keypad Delete key
KE_CSI = 81, // CSI typed directly (no longer produced by Nvim) // KE_CSI = 81, // Nvim doesn't need escaping CSI
KE_SNR = 82, // <SNR> KE_SNR = 82, // <SNR>
KE_PLUG = 83, // <Plug> KE_PLUG = 83, // <Plug>
KE_CMDWIN = 84, // open command-line window from Command-line Mode KE_CMDWIN = 84, // open command-line window from Command-line Mode

View File

@ -2089,8 +2089,7 @@ const char *mb_unescape(const char **const pp)
size_t buf_idx = 0; size_t buf_idx = 0;
uint8_t *str = (uint8_t *)(*pp); uint8_t *str = (uint8_t *)(*pp);
// Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI // Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL.
// KS_EXTRA KE_CSI to CSI.
// Maximum length of a utf-8 character is 4 bytes. // Maximum length of a utf-8 character is 4 bytes.
for (size_t str_idx = 0; str[str_idx] != NUL && buf_idx < 4; str_idx++) { for (size_t str_idx = 0; str[str_idx] != NUL && buf_idx < 4; str_idx++) {
if (str[str_idx] == K_SPECIAL if (str[str_idx] == K_SPECIAL
@ -2098,11 +2097,6 @@ const char *mb_unescape(const char **const pp)
&& str[str_idx + 2] == KE_FILLER) { && str[str_idx + 2] == KE_FILLER) {
buf[buf_idx++] = (char)K_SPECIAL; buf[buf_idx++] = (char)K_SPECIAL;
str_idx += 2; str_idx += 2;
} else if ((str[str_idx] == K_SPECIAL)
&& str[str_idx + 1] == KS_EXTRA
&& str[str_idx + 2] == KE_CSI) {
buf[buf_idx++] = (char)CSI;
str_idx += 2;
} else if (str[str_idx] == K_SPECIAL) { } else if (str[str_idx] == K_SPECIAL) {
break; // A special key can't be a multibyte char. break; // A special key can't be a multibyte char.
} else { } else {

View File

@ -922,8 +922,8 @@ int do_record(int c)
// The recorded text contents. // The recorded text contents.
p = get_recorded(); p = get_recorded();
if (p != NULL) { if (p != NULL) {
// Remove escaping for CSI and K_SPECIAL in multi-byte chars. // Remove escaping for K_SPECIAL in multi-byte chars.
vim_unescape_csi(p); vim_unescape_ks(p);
(void)tv_dict_add_str(dict, S_LEN("regcontents"), (const char *)p); (void)tv_dict_add_str(dict, S_LEN("regcontents"), (const char *)p);
} }
@ -933,7 +933,7 @@ int do_record(int c)
buf[1] = NUL; buf[1] = NUL;
(void)tv_dict_add_str(dict, S_LEN("regname"), buf); (void)tv_dict_add_str(dict, S_LEN("regname"), buf);
// Get the recorded key hits. K_SPECIAL and CSI will be escaped, this // Get the recorded key hits. K_SPECIAL will be escaped, this
// needs to be removed again to put it in a register. exec_reg then // needs to be removed again to put it in a register. exec_reg then
// adds the escaping back later. // adds the escaping back later.
apply_autocmds(EVENT_RECORDINGLEAVE, NULL, NULL, false, curbuf); apply_autocmds(EVENT_RECORDINGLEAVE, NULL, NULL, false, curbuf);
@ -1099,7 +1099,7 @@ int do_execreg(int regname, int colon, int addcr, int silent)
return FAIL; return FAIL;
} }
} }
escaped = vim_strsave_escape_csi(reg->y_array[i]); escaped = vim_strsave_escape_ks(reg->y_array[i]);
retval = ins_typebuf(escaped, remap, 0, true, silent); retval = ins_typebuf(escaped, remap, 0, true, silent);
xfree(escaped); xfree(escaped);
if (retval == FAIL) { if (retval == FAIL) {
@ -1141,7 +1141,7 @@ static void put_reedit_in_typebuf(int silent)
/// Insert register contents "s" into the typeahead buffer, so that it will be /// Insert register contents "s" into the typeahead buffer, so that it will be
/// executed again. /// executed again.
/// ///
/// @param esc when true then it is to be taken literally: Escape CSI /// @param esc when true then it is to be taken literally: Escape K_SPECIAL
/// characters and no remapping. /// characters and no remapping.
/// @param colon add ':' before the line /// @param colon add ':' before the line
static int put_in_typebuf(char_u *s, bool esc, bool colon, int silent) static int put_in_typebuf(char_u *s, bool esc, bool colon, int silent)
@ -1156,7 +1156,7 @@ static int put_in_typebuf(char_u *s, bool esc, bool colon, int silent)
char_u *p; char_u *p;
if (esc) { if (esc) {
p = vim_strsave_escape_csi(s); p = vim_strsave_escape_ks(s);
} else { } else {
p = s; p = s;
} }

View File

@ -1367,18 +1367,18 @@ describe('API', function()
end) end)
describe('nvim_feedkeys', function() describe('nvim_feedkeys', function()
it('CSI escaping', function() it('K_SPECIAL escaping', function()
local function on_setup() local function on_setup()
-- notice the special char(…) \xe2\80\xa6 -- notice the special char(…) \xe2\80\xa6
nvim('feedkeys', ':let x1="…"\n', '', true) nvim('feedkeys', ':let x1="…"\n', '', true)
-- Both nvim_replace_termcodes and nvim_feedkeys escape \x80 -- Both nvim_replace_termcodes and nvim_feedkeys escape \x80
local inp = helpers.nvim('replace_termcodes', ':let x2="…"<CR>', true, true, true) local inp = helpers.nvim('replace_termcodes', ':let x2="…"<CR>', true, true, true)
nvim('feedkeys', inp, '', true) -- escape_csi=true nvim('feedkeys', inp, '', true) -- escape_ks=true
-- nvim_feedkeys with CSI escaping disabled -- nvim_feedkeys with K_SPECIAL escaping disabled
inp = helpers.nvim('replace_termcodes', ':let x3="…"<CR>', true, true, true) inp = helpers.nvim('replace_termcodes', ':let x3="…"<CR>', true, true, true)
nvim('feedkeys', inp, '', false) -- escape_csi=false nvim('feedkeys', inp, '', false) -- escape_ks=false
helpers.stop() helpers.stop()
end end