patch 9.1.1222: using wrong length for last inserted string
Problem: using wrong length for last inserted string
(Christ van Willegen, after v9.1.1212)
Solution: use the correct length in get_last_insert_save(), make
get_last_insert() return a string_T (John Marriott)
closes: #16921
Signed-off-by: John Marriott <basilisk@internode.on.net>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/edit.c b/src/edit.c
index 143a4ce..a2a004c 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -2916,11 +2916,11 @@
long count, // Repeat this many times
int no_esc) // Don't add an ESC at the end
{
- string_T *insert; // text to be inserted
+ string_T insert; // text to be inserted
char_u last = ' ';
insert = get_last_insert();
- if (insert->string == NULL)
+ if (insert.string == NULL)
{
emsg(_(e_no_inserted_text_yet));
return FAIL;
@@ -2930,39 +2930,39 @@
if (c != NUL)
stuffcharReadbuff(c);
- if (insert->length > 0)
+ if (insert.length > 0)
{
char_u *p;
// look for the last ESC in 'insert'
- for (p = insert->string + insert->length - 1; p >= insert->string; --p)
+ for (p = insert.string + insert.length - 1; p >= insert.string; --p)
{
if (*p == ESC)
{
- insert->length = (size_t)(p - insert->string);
+ insert.length = (size_t)(p - insert.string);
break;
}
}
}
- if (insert->length > 0)
+ if (insert.length > 0)
{
- char_u *p = insert->string + insert->length - 1;
+ char_u *p = insert.string + insert.length - 1;
// when the last char is either "0" or "^" it will be quoted if no ESC
// comes after it OR if it will insert more than once and "ptr"
// starts with ^D. -- Acevedo
if ((*p == '0' || *p == '^')
- && (no_esc || (*insert->string == Ctrl_D && count > 1)))
+ && (no_esc || (*insert.string == Ctrl_D && count > 1)))
{
last = *p;
- --insert->length;
+ --insert.length;
}
}
do
{
- stuffReadbuffLen(insert->string, insert->length);
+ stuffReadbuffLen(insert.string, insert.length);
// a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
switch (last)
{
@@ -2991,23 +2991,18 @@
return OK;
}
- string_T *
+ string_T
get_last_insert(void)
{
- static string_T insert = {NULL, 0};
+ string_T insert = {NULL, 0};
- if (last_insert.string == NULL)
- {
- insert.string = NULL;
- insert.length = 0;
- }
- else
+ if (last_insert.string != NULL)
{
insert.string = last_insert.string + last_insert_skip;
insert.length = (size_t)(last_insert.length - last_insert_skip);
}
- return &insert;
+ return insert;
}
/*
@@ -3017,22 +3012,17 @@
char_u *
get_last_insert_save(void)
{
- string_T *insert = get_last_insert();
+ string_T insert = get_last_insert();
char_u *s;
- if (insert->string == NULL)
+ if (insert.string == NULL)
return NULL;
- s = vim_strnsave(insert->string, insert->length);
+ s = vim_strnsave(insert.string, insert.length);
if (s == NULL)
return NULL;
- if (insert->length > 0)
- {
- // remove trailing ESC
- --insert->length;
- if (s[insert->length] == ESC)
- s[insert->length] = NUL;
- }
+ if (insert.length > 0 && s[insert.length - 1] == ESC) // remove trailing ESC
+ s[insert.length - 1] = NUL;
return s;
}