patch 8.2.0844: text properties crossing lines not handled correctly

Problem:    Text properties crossing lines not handled correctly.
Solution:   When saving for undo include an extra line when needed and do not
            adjust properties when undoing. (Axel Forsman, closes #5875)
diff --git a/src/memline.c b/src/memline.c
index b409eb5..6d02119 100644
--- a/src/memline.c
+++ b/src/memline.c
@@ -243,7 +243,6 @@
 static void add_b0_fenc(ZERO_BL *b0p, buf_T *buf);
 static time_t swapfile_info(char_u *);
 static int recov_file_names(char_u **, char_u *, int prepend_dot);
-static int ml_delete_int(buf_T *, linenr_T, int);
 static char_u *findswapname(buf_T *, char_u **, char_u *);
 static void ml_flush_line(buf_T *);
 static bhdr_T *ml_new_data(memfile_T *, int, int);
@@ -2618,6 +2617,9 @@
 }
 
 #ifdef FEAT_PROP_POPUP
+/*
+ * Add text properties that continue from the previous line.
+ */
     static void
 add_text_props_for_append(
 	    buf_T	*buf,
@@ -2657,15 +2659,17 @@
 	count = get_text_props(buf, lnum, &props, FALSE);
 	for (n = 0; n < count; ++n)
 	{
-	    mch_memmove(&prop, props + n * sizeof(textprop_T), sizeof(textprop_T));
+	    mch_memmove(&prop, props + n * sizeof(textprop_T),
+							   sizeof(textprop_T));
 	    if (prop.tp_flags & TP_FLAG_CONT_NEXT)
 	    {
 		if (round == 2)
 		{
 		    prop.tp_flags |= TP_FLAG_CONT_PREV;
 		    prop.tp_col = 1;
-		    prop.tp_len = *len;
-		    mch_memmove(new_line + *len + new_prop_count * sizeof(textprop_T), &prop, sizeof(textprop_T));
+		    prop.tp_len = *len;  // not exactly the right length
+		    mch_memmove(new_line + *len + new_prop_count
+			      * sizeof(textprop_T), &prop, sizeof(textprop_T));
 		}
 		++new_prop_count;
 	    }
@@ -2683,8 +2687,7 @@
     linenr_T	lnum,		// append after this line (can be 0)
     char_u	*line_arg,	// text of the new line
     colnr_T	len_arg,	// length of line, including NUL, or 0
-    int		newfile,	// flag, see above
-    int		mark)		// mark the new line
+    int		flags)		// ML_APPEND_ flags
 {
     char_u	*line = line_arg;
     colnr_T	len = len_arg;
@@ -2716,7 +2719,7 @@
 	len = (colnr_T)STRLEN(line) + 1;	// space needed for the text
 
 #ifdef FEAT_PROP_POPUP
-    if (curbuf->b_has_textprop && lnum > 0)
+    if (curbuf->b_has_textprop && lnum > 0 && !(flags & ML_APPEND_UNDO))
 	// Add text properties that continue from the previous line.
 	add_text_props_for_append(buf, lnum, &line, &len, &tofree);
 #endif
@@ -2815,14 +2818,14 @@
 	 * copy the text into the block
 	 */
 	mch_memmove((char *)dp + dp->db_index[db_idx + 1], line, (size_t)len);
-	if (mark)
+	if (flags & ML_APPEND_MARK)
 	    dp->db_index[db_idx + 1] |= DB_MARKED;
 
 	/*
 	 * Mark the block dirty.
 	 */
 	buf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
-	if (!newfile)
+	if (!(flags & ML_APPEND_NEW))
 	    buf->b_ml.ml_flags |= ML_LOCKED_POS;
     }
     else	    // not enough space in data block
@@ -2891,7 +2894,8 @@
 	}
 
 	page_count = ((space_needed + HEADER_SIZE) + page_size - 1) / page_size;
-	if ((hp_new = ml_new_data(mfp, newfile, page_count)) == NULL)
+	if ((hp_new = ml_new_data(mfp, flags & ML_APPEND_NEW, page_count))
+								       == NULL)
 	{
 			// correct line counts in pointer blocks
 	    --(buf->b_ml.ml_locked_lineadd);
@@ -2927,7 +2931,7 @@
 	    dp_right->db_txt_start -= len;
 	    dp_right->db_free -= len + INDEX_SIZE;
 	    dp_right->db_index[0] = dp_right->db_txt_start;
-	    if (mark)
+	    if (flags & ML_APPEND_MARK)
 		dp_right->db_index[0] |= DB_MARKED;
 
 	    mch_memmove((char *)dp_right + dp_right->db_txt_start,
@@ -2968,7 +2972,7 @@
 	    dp_left->db_txt_start -= len;
 	    dp_left->db_free -= len + INDEX_SIZE;
 	    dp_left->db_index[line_count_left] = dp_left->db_txt_start;
-	    if (mark)
+	    if (flags & ML_APPEND_MARK)
 		dp_left->db_index[line_count_left] |= DB_MARKED;
 	    mch_memmove((char *)dp_left + dp_left->db_txt_start,
 							   line, (size_t)len);
@@ -2999,7 +3003,7 @@
 	 */
 	if (lines_moved || in_left)
 	    buf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
-	if (!newfile && db_idx >= 0 && in_left)
+	if (!(flags & ML_APPEND_NEW) && db_idx >= 0 && in_left)
 	    buf->b_ml.ml_flags |= ML_LOCKED_POS;
 	mf_put(mfp, hp_new, TRUE, FALSE);
 
@@ -3207,7 +3211,7 @@
     linenr_T	lnum,		// append after this line (can be 0)
     char_u	*line,		// text of the new line
     colnr_T	len,		// length of line, including NUL, or 0
-    int		newfile)	// flag, see above
+    int		flags)		// ML_APPEND_ flags
 {
     if (lnum > buf->b_ml.ml_line_count)
 	return FAIL;  // lnum out of range
@@ -3224,7 +3228,7 @@
 	ml_flush_line(buf);
 #endif
 
-    return ml_append_int(buf, lnum, line, len, newfile, FALSE);
+    return ml_append_int(buf, lnum, line, len, flags);
 }
 
 /*
@@ -3232,7 +3236,7 @@
  * "line" does not need to be allocated, but can't be another line in a
  * buffer, unlocking may make it invalid.
  *
- *   newfile: TRUE when starting to edit a new file, meaning that pe_old_lnum
+ * "newfile": TRUE when starting to edit a new file, meaning that pe_old_lnum
  *		will be set for recovery
  * Check: The caller of this function should probably also call
  * appended_lines().
@@ -3246,12 +3250,23 @@
     colnr_T	len,		// length of new line, including NUL, or 0
     int		newfile)	// flag, see above
 {
+    return ml_append_flags(lnum, line, len, newfile ? ML_APPEND_NEW : 0);
+}
+
+    int
+ml_append_flags(
+    linenr_T	lnum,		// append after this line (can be 0)
+    char_u	*line,		// text of the new line
+    colnr_T	len,		// length of new line, including NUL, or 0
+    int		flags)		// ML_APPEND_ values
+{
     // When starting up, we might still need to create the memfile
     if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL)
 	return FAIL;
-    return ml_append_flush(curbuf, lnum, line, len, newfile);
+    return ml_append_flush(curbuf, lnum, line, len, flags);
 }
 
+
 #if defined(FEAT_SPELL) || defined(FEAT_QUICKFIX) || defined(PROTO)
 /*
  * Like ml_append() but for an arbitrary buffer.  The buffer must already have
@@ -3267,7 +3282,7 @@
 {
     if (buf->b_ml.ml_mfp == NULL)
 	return FAIL;
-    return ml_append_flush(buf, lnum, line, len, newfile);
+    return ml_append_flush(buf, lnum, line, len, newfile ? ML_APPEND_NEW : 0);
 }
 #endif
 
@@ -3487,31 +3502,13 @@
 
 /*
  * Delete line "lnum" in the current buffer.
- * When "message" is TRUE may give a "No lines in buffer" message.
- *
- * Check: The caller of this function should probably also call
- * deleted_lines() after this.
+ * When "flags" has ML_DEL_MESSAGE may give a "No lines in buffer" message.
+ * When "flags" has ML_DEL_UNDO this is called from undo.
  *
  * return FAIL for failure, OK otherwise
  */
-    int
-ml_delete(linenr_T lnum, int message)
-{
-    ml_flush_line(curbuf);
-    if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
-	return FAIL;
-
-#ifdef FEAT_EVAL
-    // When inserting above recorded changes: flush the changes before changing
-    // the text.
-    may_invoke_listeners(curbuf, lnum, lnum + 1, -1);
-#endif
-
-    return ml_delete_int(curbuf, lnum, message);
-}
-
     static int
-ml_delete_int(buf_T *buf, linenr_T lnum, int message)
+ml_delete_int(buf_T *buf, linenr_T lnum, int flags)
 {
     bhdr_T	*hp;
     memfile_T	*mfp;
@@ -3539,7 +3536,7 @@
  */
     if (buf->b_ml.ml_line_count == 1)	    // file becomes empty
     {
-	if (message
+	if ((flags & ML_DEL_MESSAGE)
 #ifdef FEAT_NETBEANS_INTG
 		&& !netbeansSuppressNoLines
 #endif
@@ -3586,7 +3583,7 @@
 #ifdef FEAT_PROP_POPUP
     // If there are text properties, make a copy, so that we can update
     // properties in preceding and following lines.
-    if (buf->b_has_textprop)
+    if (buf->b_has_textprop && !(flags & ML_DEL_UNDO))
     {
 	size_t	textlen = STRLEN((char_u *)dp + line_start) + 1;
 
@@ -3699,6 +3696,40 @@
 }
 
 /*
+ * Delete line "lnum" in the current buffer.
+ * When "message" is TRUE may give a "No lines in buffer" message.
+ *
+ * Check: The caller of this function should probably also call
+ * deleted_lines() after this.
+ *
+ * return FAIL for failure, OK otherwise
+ */
+    int
+ml_delete(linenr_T lnum, int message)
+{
+    return ml_delete_flags(lnum, message ? ML_DEL_MESSAGE : 0);
+}
+
+/*
+ * Like ml_delete() but using flags (see ml_delete_int()).
+ */
+    int
+ml_delete_flags(linenr_T lnum, int flags)
+{
+    ml_flush_line(curbuf);
+    if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
+	return FAIL;
+
+#ifdef FEAT_EVAL
+    // When inserting above recorded changes: flush the changes before changing
+    // the text.
+    may_invoke_listeners(curbuf, lnum, lnum + 1, -1);
+#endif
+
+    return ml_delete_int(curbuf, lnum, flags);
+}
+
+/*
  * set the DB_MARKED flag for line 'lnum'
  */
     void
@@ -3905,9 +3936,9 @@
 		 * Don't forget to copy the mark!
 		 */
 		// How about handling errors???
-		(void)ml_append_int(buf, lnum, new_line, new_len, FALSE,
-					     (dp->db_index[idx] & DB_MARKED));
-		(void)ml_delete_int(buf, lnum, FALSE);
+		(void)ml_append_int(buf, lnum, new_line, new_len,
+			 (dp->db_index[idx] & DB_MARKED) ? ML_APPEND_MARK : 0);
+		(void)ml_delete_int(buf, lnum, 0);
 	    }
 	}
 	vim_free(new_line);