aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/getchar.c
diff options
context:
space:
mode:
authorbfredl <bjorn.linse@gmail.com>2022-01-27 13:45:34 +0100
committerGitHub <noreply@github.com>2022-01-27 13:45:34 +0100
commitd95bb8502435acf79239264bf545d64907fcee05 (patch)
treed5c916a97066f6bce1ab5e342653a010e23d4426 /src/nvim/getchar.c
parenta47fdf8421142532d27354f58109da220c45a5fb (diff)
parent5c897b6d0ceb7e7d40c0b740ab36d38bf3a4162d (diff)
downloadrneovim-d95bb8502435acf79239264bf545d64907fcee05.tar.gz
rneovim-d95bb8502435acf79239264bf545d64907fcee05.tar.bz2
rneovim-d95bb8502435acf79239264bf545d64907fcee05.zip
Merge pull request #17185 from zeertzjq/vim-8.2.3993
vim-patch:8.2.{3993,4002}: when recording a change in Select mode char appears twice
Diffstat (limited to 'src/nvim/getchar.c')
-rw-r--r--src/nvim/getchar.c60
1 files changed, 42 insertions, 18 deletions
diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c
index ef590adb3a..95720c498a 100644
--- a/src/nvim/getchar.c
+++ b/src/nvim/getchar.c
@@ -284,9 +284,19 @@ static void add_buff(buffheader_T *const buf, const char *const s, ptrdiff_t sle
}
}
-/*
- * Add number "n" to buffer "buf".
- */
+/// Delete "slen" bytes from the end of "buf".
+/// Only works when it was just added.
+static void delete_buff_tail(buffheader_T *buf, int slen)
+{
+ int len = (int)STRLEN(buf->bh_curr->b_str);
+
+ if (len >= slen) {
+ buf->bh_curr->b_str[len - slen] = NUL;
+ buf->bh_space += (size_t)slen;
+ }
+}
+
+/// Add number "n" to buffer "buf".
static void add_num_buff(buffheader_T *buf, long n)
{
char number[32];
@@ -967,31 +977,31 @@ int ins_typebuf(char_u *str, int noremap, int offset, bool nottyped, bool silent
return OK;
}
-/*
- * Put character "c" back into the typeahead buffer.
- * Can be used for a character obtained by vgetc() that needs to be put back.
- * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
- * the char.
- */
-void ins_char_typebuf(int c, int modifier)
+/// Put character "c" back into the typeahead buffer.
+/// Can be used for a character obtained by vgetc() that needs to be put back.
+/// Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
+/// the char.
+/// @return the length of what was inserted
+int ins_char_typebuf(int c, int modifier)
{
- char_u buf[MB_MAXBYTES + 4];
- int idx = 0;
+ char_u buf[MB_MAXBYTES * 3 + 4];
+ int len = 0;
if (modifier != 0) {
buf[0] = K_SPECIAL;
buf[1] = KS_MODIFIER;
buf[2] = (char_u)modifier;
buf[3] = NUL;
- idx = 3;
+ len = 3;
}
if (IS_SPECIAL(c)) {
- buf[idx] = K_SPECIAL;
- buf[idx + 1] = (char_u)K_SECOND(c);
- buf[idx + 2] = (char_u)K_THIRD(c);
- buf[idx + 3] = NUL;
+ buf[len] = K_SPECIAL;
+ buf[len + 1] = (char_u)K_SECOND(c);
+ buf[len + 2] = (char_u)K_THIRD(c);
+ buf[len + 3] = NUL;
} else {
- char_u *p = buf + idx;
+ char_u *p = buf + len;
int char_len = utf_char2bytes(c, p);
+ len += char_len;
// If the character contains K_SPECIAL bytes they need escaping.
for (int i = char_len; --i >= 0; p++) {
if ((uint8_t)(*p) == K_SPECIAL) {
@@ -999,11 +1009,13 @@ void ins_char_typebuf(int c, int modifier)
*p++ = K_SPECIAL;
*p++ = KS_SPECIAL;
*p = KE_FILLER;
+ len += 2;
}
}
*p = NUL;
}
(void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
+ return len;
}
/// Return TRUE if the typeahead buffer was changed (while waiting for a
@@ -1163,6 +1175,18 @@ static void gotchars(const char_u *chars, size_t len)
maptick++;
}
+/// Undo the last gotchars() for "len" bytes. To be used when putting a typed
+/// character back into the typeahead buffer, thus gotchars() will be called
+/// again.
+/// Only affects recorded characters.
+void ungetchars(int len)
+{
+ if (reg_recording != 0) {
+ delete_buff_tail(&recordbuff, len);
+ last_recorded_len -= (size_t)len;
+ }
+}
+
/*
* Sync undo. Called when typed characters are obtained from the typeahead
* buffer, or when a menu is used.