@@ -920,7 +920,7 @@ init_typebuf(void)
920920 typebuf .tb_noremap = noremapbuf_init ;
921921 typebuf .tb_buflen = TYPELEN_INIT ;
922922 typebuf .tb_len = 0 ;
923- typebuf .tb_off = 0 ;
923+ typebuf .tb_off = MAXMAPLEN + 4 ;
924924 typebuf .tb_change_cnt = 1 ;
925925 }
926926}
@@ -974,11 +974,21 @@ ins_typebuf(
974974 typebuf .tb_off -= addlen ;
975975 mch_memmove (typebuf .tb_buf + typebuf .tb_off , str , (size_t )addlen );
976976 }
977+ else if (typebuf .tb_len == 0 && typebuf .tb_buflen
978+ >= addlen + 3 * (MAXMAPLEN + 4 ))
979+ {
980+ /*
981+ * Buffer is empty and string fits in the existing buffer.
982+ * Leave some space before and after, if possible.
983+ */
984+ typebuf .tb_off = (typebuf .tb_buflen - addlen - 3 * (MAXMAPLEN + 4 )) / 2 ;
985+ mch_memmove (typebuf .tb_buf + typebuf .tb_off , str , (size_t )addlen );
986+ }
977987 else
978988 {
979989 /*
980990 * Need to allocate a new buffer.
981- * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
991+ * In typebuf.tb_buf there must always be room for 3 * ( MAXMAPLEN + 4)
982992 * characters. We add some extra room to avoid having to allocate too
983993 * often.
984994 */
@@ -1291,7 +1301,7 @@ alloc_typebuf(void)
12911301 return FAIL ;
12921302 }
12931303 typebuf .tb_buflen = TYPELEN_INIT ;
1294- typebuf .tb_off = 0 ;
1304+ typebuf .tb_off = MAXMAPLEN + 4 ; /* can insert without realloc */
12951305 typebuf .tb_len = 0 ;
12961306 typebuf .tb_maplen = 0 ;
12971307 typebuf .tb_silent = 0 ;
0 commit comments