@@ -3217,11 +3217,22 @@ ml_replace(linenr_T lnum, char_u *line, int copy)
32173217
32183218 if (line != NULL )
32193219 len = (colnr_T )STRLEN (line );
3220- return ml_replace_len (lnum , line , len , copy );
3220+ return ml_replace_len (lnum , line , len , FALSE, copy );
32213221}
32223222
3223+ /*
3224+ * Replace a line for the current buffer. Like ml_replace() with:
3225+ * "len_arg" is the length of the text, excluding NUL.
3226+ * If "has_props" is TRUE then "line_arg" includes the text properties and
3227+ * "len_arg" includes the NUL of the text.
3228+ */
32233229 int
3224- ml_replace_len (linenr_T lnum , char_u * line_arg , colnr_T len_arg , int copy )
3230+ ml_replace_len (
3231+ linenr_T lnum ,
3232+ char_u * line_arg ,
3233+ colnr_T len_arg ,
3234+ int has_props ,
3235+ int copy )
32253236{
32263237 char_u * line = line_arg ;
32273238 colnr_T len = len_arg ;
@@ -3233,8 +3244,21 @@ ml_replace_len(linenr_T lnum, char_u *line_arg, colnr_T len_arg, int copy)
32333244 if (curbuf -> b_ml .ml_mfp == NULL && open_buffer (FALSE, NULL , 0 ) == FAIL )
32343245 return FAIL ;
32353246
3236- if (copy && (line = vim_strnsave (line , len )) == NULL ) /* allocate memory */
3237- return FAIL ;
3247+ if (!has_props )
3248+ ++ len ; // include the NUL after the text
3249+ if (copy )
3250+ {
3251+ // copy the line to allocated memory
3252+ #ifdef FEAT_TEXT_PROP
3253+ if (has_props )
3254+ line = vim_memsave (line , len );
3255+ else
3256+ #endif
3257+ line = vim_strnsave (line , len - 1 );
3258+ if (line == NULL )
3259+ return FAIL ;
3260+ }
3261+
32383262#ifdef FEAT_NETBEANS_INTG
32393263 if (netbeans_active ())
32403264 {
@@ -3249,14 +3273,14 @@ ml_replace_len(linenr_T lnum, char_u *line_arg, colnr_T len_arg, int copy)
32493273 curbuf -> b_ml .ml_flags &= ~ML_LINE_DIRTY ;
32503274
32513275#ifdef FEAT_TEXT_PROP
3252- if (curbuf -> b_has_textprop )
3276+ if (curbuf -> b_has_textprop && ! has_props )
32533277 // Need to fetch the old line to copy over any text properties.
32543278 ml_get_buf (curbuf , lnum , TRUE);
32553279#endif
32563280 }
32573281
32583282#ifdef FEAT_TEXT_PROP
3259- if (curbuf -> b_has_textprop )
3283+ if (curbuf -> b_has_textprop && ! has_props )
32603284 {
32613285 size_t oldtextlen = STRLEN (curbuf -> b_ml .ml_line_ptr ) + 1 ;
32623286
@@ -3266,11 +3290,11 @@ ml_replace_len(linenr_T lnum, char_u *line_arg, colnr_T len_arg, int copy)
32663290 size_t textproplen = curbuf -> b_ml .ml_line_len - oldtextlen ;
32673291
32683292 // Need to copy over text properties, stored after the text.
3269- newline = alloc (len + 1 + (int )textproplen );
3293+ newline = alloc (len + (int )textproplen );
32703294 if (newline != NULL )
32713295 {
3272- mch_memmove (newline , line , len + 1 );
3273- mch_memmove (newline + len + 1 , curbuf -> b_ml .ml_line_ptr + oldtextlen , textproplen );
3296+ mch_memmove (newline , line , len );
3297+ mch_memmove (newline + len , curbuf -> b_ml .ml_line_ptr + oldtextlen , textproplen );
32743298 vim_free (line );
32753299 line = newline ;
32763300 len += (colnr_T )textproplen ;
@@ -3279,11 +3303,11 @@ ml_replace_len(linenr_T lnum, char_u *line_arg, colnr_T len_arg, int copy)
32793303 }
32803304#endif
32813305
3282- if (curbuf -> b_ml .ml_flags & ML_LINE_DIRTY ) /* same line allocated */
3283- vim_free (curbuf -> b_ml .ml_line_ptr ); /* free it */
3306+ if (curbuf -> b_ml .ml_flags & ML_LINE_DIRTY ) // same line allocated
3307+ vim_free (curbuf -> b_ml .ml_line_ptr ); // free it
32843308
32853309 curbuf -> b_ml .ml_line_ptr = line ;
3286- curbuf -> b_ml .ml_line_len = len + 1 ;
3310+ curbuf -> b_ml .ml_line_len = len ;
32873311 curbuf -> b_ml .ml_line_lnum = lnum ;
32883312 curbuf -> b_ml .ml_flags = (curbuf -> b_ml .ml_flags | ML_LINE_DIRTY ) & ~ML_EMPTY ;
32893313
0 commit comments