@@ -163,6 +163,7 @@ static void ins_compl_restart __ARGS((void));
163163static void ins_compl_set_original_text __ARGS ((char_u * str ));
164164static void ins_compl_addfrommatch __ARGS ((void ));
165165static int ins_compl_prep __ARGS ((int c ));
166+ static void ins_compl_fixRedoBufForLeader __ARGS ((char_u * ptr_arg ));
166167static buf_T * ins_compl_next_buf __ARGS ((buf_T * buf , int flag ));
167168#if defined(FEAT_COMPL_FUNC ) || defined(FEAT_EVAL )
168169static void ins_compl_add_list __ARGS ((list_T * list ));
@@ -3713,9 +3714,6 @@ ins_compl_prep(c)
37133714 * memory that was used, and make sure we can redo the insert. */
37143715 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E )
37153716 {
3716- char_u * p ;
3717- int temp = 0 ;
3718-
37193717 /*
37203718 * If any of the original typed text has been changed, eg when
37213719 * ignorecase is set, we must add back-spaces to the redo
@@ -3726,25 +3724,9 @@ ins_compl_prep(c)
37263724 */
37273725 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E )
37283726 ptr = compl_curr_match -> cp_str ;
3729- else if (compl_leader != NULL )
3730- ptr = compl_leader ;
37313727 else
3732- ptr = compl_orig_text ;
3733- if (compl_orig_text != NULL )
3734- {
3735- p = compl_orig_text ;
3736- for (temp = 0 ; p [temp ] != NUL && p [temp ] == ptr [temp ];
3737- ++ temp )
3738- ;
3739- #ifdef FEAT_MBYTE
3740- if (temp > 0 )
3741- temp -= (* mb_head_off )(compl_orig_text , p + temp );
3742- #endif
3743- for (p += temp ; * p != NUL ; mb_ptr_adv (p ))
3744- AppendCharToRedobuff (K_BS );
3745- }
3746- if (ptr != NULL )
3747- AppendToRedobuffLit (ptr + temp , -1 );
3728+ ptr = NULL ;
3729+ ins_compl_fixRedoBufForLeader (ptr );
37483730 }
37493731
37503732#ifdef FEAT_CINDENT
@@ -3833,6 +3815,44 @@ ins_compl_prep(c)
38333815 return retval ;
38343816}
38353817
3818+ /*
3819+ * Fix the redo buffer for the completion leader replacing some of the typed
3820+ * text. This inserts backspaces and appends the changed text.
3821+ * "ptr" is the known leader text or NUL.
3822+ */
3823+ static void
3824+ ins_compl_fixRedoBufForLeader (ptr_arg )
3825+ char_u * ptr_arg ;
3826+ {
3827+ int len ;
3828+ char_u * p ;
3829+ char_u * ptr = ptr_arg ;
3830+
3831+ if (ptr == NULL )
3832+ {
3833+ if (compl_leader != NULL )
3834+ ptr = compl_leader ;
3835+ else
3836+ return ; /* nothing to do */
3837+ }
3838+ if (compl_orig_text != NULL )
3839+ {
3840+ p = compl_orig_text ;
3841+ for (len = 0 ; p [len ] != NUL && p [len ] == ptr [len ]; ++ len )
3842+ ;
3843+ #ifdef FEAT_MBYTE
3844+ if (len > 0 )
3845+ len -= (* mb_head_off )(p , p + len );
3846+ #endif
3847+ for (p += len ; * p != NUL ; mb_ptr_adv (p ))
3848+ AppendCharToRedobuff (K_BS );
3849+ }
3850+ else
3851+ len = 0 ;
3852+ if (ptr != NULL )
3853+ AppendToRedobuffLit (ptr + len , -1 );
3854+ }
3855+
38363856/*
38373857 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
38383858 * (depending on flag) starting from buf and looking for a non-scanned
@@ -5241,6 +5261,10 @@ ins_complete(c)
52415261 else
52425262 edit_submode = (char_u * )_ (CTRL_X_MSG (ctrl_x_mode ));
52435263
5264+ /* If any of the original typed text has been changed we need to fix
5265+ * the redo buffer. */
5266+ ins_compl_fixRedoBufForLeader (NULL );
5267+
52445268 /* Always add completion for the original text. */
52455269 vim_free (compl_orig_text );
52465270 compl_orig_text = vim_strnsave (line + compl_col , compl_length );
0 commit comments