aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/getchar.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c
index 1c0464b575..a77c467fe5 100644
--- a/src/nvim/getchar.c
+++ b/src/nvim/getchar.c
@@ -835,7 +835,7 @@ static void init_typebuf(void)
typebuf.tb_noremap = noremapbuf_init;
typebuf.tb_buflen = TYPELEN_INIT;
typebuf.tb_len = 0;
- typebuf.tb_off = 0;
+ typebuf.tb_off = MAXMAPLEN + 4;
typebuf.tb_change_cnt = 1;
}
}
@@ -879,9 +879,15 @@ int ins_typebuf(char_u *str, int noremap, int offset, int nottyped, bool silent)
// Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
typebuf.tb_off -= addlen;
memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
+ } else if (typebuf.tb_len == 0
+ && typebuf.tb_buflen >= addlen + 3 * (MAXMAPLEN + 4)) {
+ // Buffer is empty and string fits in the existing buffer.
+ // Leave some space before and after, if possible.
+ typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
+ memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
} else {
// Need to allocate a new buffer.
- // In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
+ // In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
// characters. We add some extra room to avoid having to allocate too
// often.
newoff = MAXMAPLEN + 4;
@@ -1145,7 +1151,7 @@ void alloc_typebuf(void)
typebuf.tb_buf = xmalloc(TYPELEN_INIT);
typebuf.tb_noremap = xmalloc(TYPELEN_INIT);
typebuf.tb_buflen = TYPELEN_INIT;
- typebuf.tb_off = 0;
+ typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
typebuf.tb_len = 0;
typebuf.tb_maplen = 0;
typebuf.tb_silent = 0;