changeset 17403:9b6c0ef29c20 v8.1.1700

patch 8.1.1700: listener callback called for the wrong buffer commit https://github.com/vim/vim/commit/250e3112c6dc4c4ceded308d5b94392ec02bc03f Author: Bram Moolenaar <Bram@vim.org> Date: Mon Jul 15 23:02:14 2019 +0200 patch 8.1.1700: listener callback called for the wrong buffer Problem: Listener callback called for the wrong buffer. Solution: Invoke listeners before calling ml_append_int().
author Bram Moolenaar <Bram@vim.org>
date Mon, 15 Jul 2019 23:15:07 +0200
parents 49414aa1740b
children d7fc487f259f
files src/memline.c src/version.c
diffstat 2 files changed, 75 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/src/memline.c
+++ b/src/memline.c
@@ -243,7 +243,6 @@ static void set_b0_dir_flag(ZERO_BL *b0p
 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_append_int(buf_T *, linenr_T, char_u *, colnr_T, int, int);
 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 *);
@@ -2746,56 +2745,6 @@ add_text_props_for_append(
 }
 #endif
 
-/*
- * Append a line after lnum (may be 0 to insert a line in front of the file).
- * "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
- *		will be set for recovery
- * Check: The caller of this function should probably also call
- * appended_lines().
- *
- * return FAIL for failure, OK otherwise
- */
-    int
-ml_append(
-    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		newfile)	/* flag, see above */
-{
-    /* 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;
-
-    if (curbuf->b_ml.ml_line_lnum != 0)
-	ml_flush_line(curbuf);
-    return ml_append_int(curbuf, lnum, line, len, newfile, FALSE);
-}
-
-#if defined(FEAT_SPELL) || defined(FEAT_QUICKFIX) || defined(PROTO)
-/*
- * Like ml_append() but for an arbitrary buffer.  The buffer must already have
- * a memline.
- */
-    int
-ml_append_buf(
-    buf_T	*buf,
-    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		newfile)	/* flag, see above */
-{
-    if (buf->b_ml.ml_mfp == NULL)
-	return FAIL;
-
-    if (buf->b_ml.ml_line_lnum != 0)
-	ml_flush_line(buf);
-    return ml_append_int(buf, lnum, line, len, newfile, FALSE);
-}
-#endif
-
     static int
 ml_append_int(
     buf_T	*buf,
@@ -2834,14 +2783,6 @@ ml_append_int(
     if (len == 0)
 	len = (colnr_T)STRLEN(line) + 1;	// space needed for the text
 
-#ifdef FEAT_EVAL
-    // When inserting above recorded changes: flush the changes before changing
-    // the text.  Then flush the cached line, it may become invalid.
-    may_invoke_listeners(buf, lnum + 1, lnum + 1, 1);
-    if (curbuf->b_ml.ml_line_lnum != 0)
-	ml_flush_line(curbuf);
-#endif
-
 #ifdef FEAT_TEXT_PROP
     if (curbuf->b_has_textprop && lnum > 0)
 	// Add text properties that continue from the previous line.
@@ -3326,6 +3267,79 @@ theend:
 }
 
 /*
+ * Flush any pending change and call ml_append_int()
+ */
+    static int
+ml_append_flush(
+    buf_T	*buf,
+    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
+{
+    if (lnum > buf->b_ml.ml_line_count)
+	return FAIL;  // lnum out of range
+
+    if (buf->b_ml.ml_line_lnum != 0)
+	// This may also invoke ml_append_int().
+	ml_flush_line(buf);
+
+#ifdef FEAT_EVAL
+    // When inserting above recorded changes: flush the changes before changing
+    // the text.  Then flush the cached line, it may become invalid.
+    may_invoke_listeners(buf, lnum + 1, lnum + 1, 1);
+    if (buf->b_ml.ml_line_lnum != 0)
+	ml_flush_line(buf);
+#endif
+
+    return ml_append_int(buf, lnum, line, len, newfile, FALSE);
+}
+
+/*
+ * Append a line after lnum (may be 0 to insert a line in front of the file).
+ * "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
+ *		will be set for recovery
+ * Check: The caller of this function should probably also call
+ * appended_lines().
+ *
+ * return FAIL for failure, OK otherwise
+ */
+    int
+ml_append(
+    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		newfile)	/* flag, see above */
+{
+    /* 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);
+}
+
+#if defined(FEAT_SPELL) || defined(FEAT_QUICKFIX) || defined(PROTO)
+/*
+ * Like ml_append() but for an arbitrary buffer.  The buffer must already have
+ * a memline.
+ */
+    int
+ml_append_buf(
+    buf_T	*buf,
+    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		newfile)	/* flag, see above */
+{
+    if (buf->b_ml.ml_mfp == NULL)
+	return FAIL;
+    return ml_append_flush(buf, lnum, line, len, newfile);
+}
+#endif
+
+/*
  * Replace line lnum, with buffering, in current buffer.
  *
  * If "copy" is TRUE, make a copy of the line, otherwise the line has been
--- a/src/version.c
+++ b/src/version.c
@@ -778,6 +778,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1700,
+/**/
     1699,
 /**/
     1698,