]> granicus.if.org Git - vim/commitdiff
patch 8.1.0678: text properties as not adjusted for inserted text v8.1.0678
authorBram Moolenaar <Bram@vim.org>
Tue, 1 Jan 2019 23:02:11 +0000 (00:02 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 1 Jan 2019 23:02:11 +0000 (00:02 +0100)
Problem:    Text properties as not adjusted for inserted text.
Solution:   Adjust text properties when inserting text.

src/misc1.c
src/proto/misc1.pro
src/testdir/dumps/Test_textprop_01.dump
src/testdir/test_textprop.vim
src/textprop.c
src/version.c

index 00a549d66ca412c65f29458390bfac0434ec0064..692d5c769e9f177d1d69ffd90a80a548098168ff 100644 (file)
@@ -2322,7 +2322,7 @@ ins_bytes_len(char_u *p, int len)
        for (i = 0; i < len; i += n)
        {
            if (enc_utf8)
-               /* avoid reading past p[len] */
+               // avoid reading past p[len]
                n = utfc_ptr2len_len(p + i, len - i);
            else
                n = (*mb_ptr2len)(p + i);
@@ -2365,12 +2365,12 @@ ins_char(int c)
 ins_char_bytes(char_u *buf, int charlen)
 {
     int                c = buf[0];
-    int                newlen;         /* nr of bytes inserted */
-    int                oldlen;         /* nr of bytes deleted (0 when not replacing) */
+    int                newlen;         // nr of bytes inserted
+    int                oldlen;         // nr of bytes deleted (0 when not replacing)
     char_u     *p;
     char_u     *newp;
     char_u     *oldp;
-    int                linelen;        /* length of old line including NUL */
+    int                linelen;        // length of old line including NUL
     colnr_T    col;
     linenr_T   lnum = curwin->w_cursor.lnum;
     int                i;
@@ -2439,8 +2439,7 @@ ins_char_bytes(char_u *buf, int charlen)
            }
            curwin->w_p_list = old_list;
        }
-       else
-           if (oldp[col] != NUL)
+       else if (oldp[col] != NUL)
        {
            /* normal replace */
 #ifdef FEAT_MBYTE
@@ -2494,11 +2493,11 @@ ins_char_bytes(char_u *buf, int charlen)
     while (i < newlen)
        p[i++] = ' ';
 
-    /* Replace the line in the buffer. */
+    // Replace the line in the buffer.
     ml_replace(lnum, newp, FALSE);
 
-    /* mark the buffer as changed and prepare for displaying */
-    changed_bytes(lnum, col);
+    // mark the buffer as changed and prepare for displaying
+    inserted_bytes(lnum, col, newlen - oldlen);
 
     /*
      * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
@@ -2566,7 +2565,7 @@ ins_str(char_u *s)
     mch_memmove(newp + col, s, (size_t)newlen);
     mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
     ml_replace(lnum, newp, FALSE);
-    changed_bytes(lnum, col);
+    inserted_bytes(lnum, col, newlen);
     curwin->w_cursor.col += newlen;
 }
 
@@ -3014,6 +3013,21 @@ changed_bytes(linenr_T lnum, colnr_T col)
            }
     }
 #endif
+}
+
+/*
+ * Like changed_bytes() but also adjust text properties for "added" bytes.
+ * When "added" is negative text was deleted.
+ */
+    void
+inserted_bytes(linenr_T lnum, colnr_T col, int added)
+{
+    changed_bytes(lnum, col);
+
+#ifdef FEAT_TEXT_PROP
+    if (curbuf->b_has_textprop && added != 0)
+       adjust_prop_columns(lnum, col, added);
+#endif
 }
 
     static void
index 58c3d3f49c211cd9e809a5e5949b761992a2ff0c..4a903d289a55c90271bfbb227a5b21a20c7feb02 100644 (file)
@@ -35,6 +35,7 @@ char_u *skip_to_option_part(char_u *p);
 void changed(void);
 void changed_int(void);
 void changed_bytes(linenr_T lnum, colnr_T col);
+void inserted_bytes(linenr_T lnum, colnr_T col, int added);
 void appended_lines(linenr_T lnum, long count);
 void appended_lines_mark(linenr_T lnum, long count);
 void deleted_lines(linenr_T lnum, long count);
index f78c447a053e663d55b9c9f73157fbf72906d768..98130a795eb3220217a1024ad93f9caeb472bdf1 100644 (file)
@@ -1,6 +1,6 @@
-| +0#af5f00255#ffffff0@1|1| >O+0#0000000&|n|e| +0&#ffff4012|t|w|o| +0&#ffffff0@63
+| +0#af5f00255#ffffff0@1|1| |O+0#0000000&|n|e| +0&#ffff4012|t|w|o| +0&#ffffff0@63
 | +0#af5f00255&@1|2| |N+0#0000000#ffff4012|u|m|b|é|r| |1+0#4040ff13&|2|3| +0#0000000&|ä|n|d| |t|h|œ|n| |4+0#4040ff13&|¾|7|.+0#0000000&| +0&#ffffff0@46
-| +0#af5f00255&@1|3| |T+0#0000000#ffff4012|h|r+0&#ffffff0|e@1| @65
+| +0#af5f00255&@1|3| |-+0#0000000#ffff4012@1|x+0&#ffffff0|a+0#4040ff13&@1|x+0#0000000&|-@1|x+0#4040ff13&|b@1|x+0#0000000&|-@1|x|c+0#4040ff13&@1|x|-+0#0000000&@1|x+0#4040ff13&|d@1>x|-+0#0000000&@1| @44
 |~+0#4040ff13&| @73
 |~| @73
-| +0#0000000&@56|1|,|1| @10|A|l@1| 
+| +0#0000000&@56|3|,|2|4| @9|A|l@1| 
index fdf604591b212f80c922b46afe9489d30b21fe85..d810dfc884d40fff001d0442295331026f4e73ad 100644 (file)
@@ -300,17 +300,25 @@ funct Test_textprop_screenshots()
     return
   endif
   call writefile([
-       \ "call setline(1, ['One two', 'Numbér 123 änd thœn 4¾7.', 'Three'])",
+       \ "call setline(1, ['One two', 'Numbér 123 änd thœn 4¾7.', '--aa--bb--cc--dd--'])",
        \ "hi NumberProp ctermfg=blue",
        \ "hi LongProp ctermbg=yellow",
        \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
        \ "call prop_type_add('long', {'highlight': 'LongProp'})",
+       \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
+       \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
+       \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
        \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
        \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
        \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
+       \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
+       \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
+       \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
+       \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
        \ "set number",
        \ "hi clear SpellBad",
        \ "set spell",
+       \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
        \], 'XtestProp')
   let buf = RunVimInTerminal('-S XtestProp', {'rows': 6})
   call VerifyScreenDump(buf, 'Test_textprop_01', {})
index ce6b66c28ca3a1fc49c08b58837df4fb1bfd3e32..526a6f0a1431a2b898b17d3dbcb2cd1d90c3bde5 100644 (file)
@@ -915,11 +915,47 @@ clear_buf_prop_types(buf_T *buf)
 /*
  * Adjust the columns of text properties in line "lnum" after position "col" to
  * shift by "bytes_added" (can be negative).
+ * Note that "col" is zero-based, while tp_col is one-based.
+ * Only for the current buffer.
+ * Called is expected to check b_has_textprop and "bytes_added" being non-zero.
  */
     void
-adjust_prop_columns(linenr_T lnum UNUSED, colnr_T col UNUSED, int bytes_added UNUSED)
+adjust_prop_columns(linenr_T lnum, colnr_T col, int bytes_added)
 {
-    // TODO
+    int                proplen;
+    char_u     *props;
+    textprop_T tmp_prop;
+    proptype_T  *pt;
+    int                dirty = FALSE;
+    int                i;
+
+    proplen = get_text_props(curbuf, lnum, &props, TRUE);
+    if (proplen == 0)
+       return;
+
+    for (i = 0; i < proplen; ++i)
+    {
+       mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
+                                                          sizeof(textprop_T));
+       pt = text_prop_type_by_id(curbuf, tmp_prop.tp_type);
+
+       if (tmp_prop.tp_col >= col + (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL) ? 2 : 1))
+       {
+           tmp_prop.tp_col += bytes_added;
+           dirty = TRUE;
+       }
+       else if (tmp_prop.tp_col + tmp_prop.tp_len > col + (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL) ? 0 : 1))
+       {
+           tmp_prop.tp_len += bytes_added;
+           dirty = TRUE;
+       }
+       if (dirty)
+       {
+           curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
+           mch_memmove(props + i * sizeof(textprop_T), &tmp_prop,
+                                                          sizeof(textprop_T));
+       }
+    }
 }
 
 #endif // FEAT_TEXT_PROP
index 439e1bc3622f12a92ff9d37bcbf0a43667a6f9ba..fbd6110b6917c3dd10b7e00779402d8a73404972 100644 (file)
@@ -799,6 +799,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    678,
 /**/
     677,
 /**/