1
0
forked from aniani/vim

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>
This commit is contained in:
John Marriott 2025-03-18 20:49:01 +01:00 committed by Christian Brabandt
parent a3a7d10bfb
commit 8ac0f73eb1
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
4 changed files with 25 additions and 31 deletions

View File

@ -2916,11 +2916,11 @@ stuff_inserted(
long count, // Repeat this many times long count, // Repeat this many times
int no_esc) // Don't add an ESC at the end 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 = ' '; char_u last = ' ';
insert = get_last_insert(); insert = get_last_insert();
if (insert->string == NULL) if (insert.string == NULL)
{ {
emsg(_(e_no_inserted_text_yet)); emsg(_(e_no_inserted_text_yet));
return FAIL; return FAIL;
@ -2930,39 +2930,39 @@ stuff_inserted(
if (c != NUL) if (c != NUL)
stuffcharReadbuff(c); stuffcharReadbuff(c);
if (insert->length > 0) if (insert.length > 0)
{ {
char_u *p; char_u *p;
// look for the last ESC in 'insert' // 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) if (*p == ESC)
{ {
insert->length = (size_t)(p - insert->string); insert.length = (size_t)(p - insert.string);
break; 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 // 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" // comes after it OR if it will insert more than once and "ptr"
// starts with ^D. -- Acevedo // starts with ^D. -- Acevedo
if ((*p == '0' || *p == '^') if ((*p == '0' || *p == '^')
&& (no_esc || (*insert->string == Ctrl_D && count > 1))) && (no_esc || (*insert.string == Ctrl_D && count > 1)))
{ {
last = *p; last = *p;
--insert->length; --insert.length;
} }
} }
do do
{ {
stuffReadbuffLen(insert->string, insert->length); stuffReadbuffLen(insert.string, insert.length);
// a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" // a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^"
switch (last) switch (last)
{ {
@ -2991,23 +2991,18 @@ stuff_inserted(
return OK; return OK;
} }
string_T * string_T
get_last_insert(void) get_last_insert(void)
{ {
static string_T insert = {NULL, 0}; string_T insert = {NULL, 0};
if (last_insert.string == NULL) if (last_insert.string != NULL)
{
insert.string = NULL;
insert.length = 0;
}
else
{ {
insert.string = last_insert.string + last_insert_skip; insert.string = last_insert.string + last_insert_skip;
insert.length = (size_t)(last_insert.length - last_insert_skip); insert.length = (size_t)(last_insert.length - last_insert_skip);
} }
return &insert; return insert;
} }
/* /*
@ -3017,22 +3012,17 @@ get_last_insert(void)
char_u * char_u *
get_last_insert_save(void) get_last_insert_save(void)
{ {
string_T *insert = get_last_insert(); string_T insert = get_last_insert();
char_u *s; char_u *s;
if (insert->string == NULL) if (insert.string == NULL)
return NULL; return NULL;
s = vim_strnsave(insert->string, insert->length); s = vim_strnsave(insert.string, insert.length);
if (s == NULL) if (s == NULL)
return NULL; return NULL;
if (insert->length > 0) if (insert.length > 0 && s[insert.length - 1] == ESC) // remove trailing ESC
{ s[insert.length - 1] = NUL;
// remove trailing ESC
--insert->length;
if (s[insert->length] == ESC)
s[insert->length] = NUL;
}
return s; return s;
} }

View File

@ -24,7 +24,7 @@ int cursor_up(long n, int upd_topline);
void cursor_down_inner(win_T *wp, long n); void cursor_down_inner(win_T *wp, long n);
int cursor_down(long n, int upd_topline); int cursor_down(long n, int upd_topline);
int stuff_inserted(int c, long count, int no_esc); int stuff_inserted(int c, long count, int no_esc);
string_T *get_last_insert(void); string_T get_last_insert(void);
char_u *get_last_insert_save(void); char_u *get_last_insert_save(void);
void replace_push(int c); void replace_push(int c);
int replace_push_mb(char_u *p); int replace_push_mb(char_u *p);

View File

@ -2379,6 +2379,7 @@ ex_display(exarg_T *eap)
char_u *arg = eap->arg; char_u *arg = eap->arg;
int clen; int clen;
int type; int type;
string_T insert;
if (arg != NULL && *arg == NUL) if (arg != NULL && *arg == NUL)
arg = NULL; arg = NULL;
@ -2471,7 +2472,8 @@ ex_display(exarg_T *eap)
} }
// display last inserted text // display last inserted text
if ((p = get_last_insert()->string) != NULL insert = get_last_insert();
if ((p = insert.string) != NULL
&& (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int
&& !message_filtered(p)) && !message_filtered(p))
{ {

View File

@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1222,
/**/ /**/
1221, 1221,
/**/ /**/