Patch 8.1.0691
Problem:    Text properties are not adjusted for :substitute.
Solution:   Adjust text properties as well as possible.
Files:      src/ex_cmds.c, src/textprop.c, src/proto/textprop.pro,
            src/testdir/test_textprop.vim


*** ../vim-8.1.0690/src/ex_cmds.c       2019-01-01 13:20:05.940711222 +0100
--- src/ex_cmds.c       2019-01-04 21:45:26.793312693 +0100
***************
*** 5628,5636 ****
--- 5628,5646 ----
                 * - original text up to match
                 * - length of substituted part
                 * - original text after match
+                * Adjust text properties here, since we have all information
+                * needed.
                 */
                if (nmatch == 1)
+               {
                    p1 = sub_firstline;
+ #ifdef FEAT_TEXT_PROP
+                   if (curbuf->b_has_textprop)
+                       adjust_prop_columns(lnum, regmatch.startpos[0].col,
+                             sublen - 1 - (regmatch.endpos[0].col
+                                                 - regmatch.startpos[0].col));
+ #endif
+               }
                else
                {
                    p1 = ml_get(sub_firstlnum + nmatch - 1);
***************
*** 5732,5742 ****
                        STRMOVE(p1, p1 + 1);
                    else if (*p1 == CAR)
                    {
!                       if (u_inssub(lnum) == OK)   /* prepare for undo */
                        {
!                           *p1 = NUL;              /* truncate up to the CR */
!                           ml_append(lnum - 1, new_start,
!                                       (colnr_T)(p1 - new_start + 1), FALSE);
                            mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
                            if (subflags.do_ask)
                                appended_lines(lnum - 1, 1L);
--- 5742,5753 ----
                        STRMOVE(p1, p1 + 1);
                    else if (*p1 == CAR)
                    {
!                       if (u_inssub(lnum) == OK)   // prepare for undo
                        {
!                           colnr_T     plen = (colnr_T)(p1 - new_start + 1);
! 
!                           *p1 = NUL;              // truncate up to the CR
!                           ml_append(lnum - 1, new_start, plen, FALSE);
                            mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
                            if (subflags.do_ask)
                                appended_lines(lnum - 1, 1L);
***************
*** 5746,5758 ****
                                    first_line = lnum;
                                last_line = lnum + 1;
                            }
!                           /* All line numbers increase. */
                            ++sub_firstlnum;
                            ++lnum;
                            ++line2;
!                           /* move the cursor to the new line, like Vi */
                            ++curwin->w_cursor.lnum;
!                           /* copy the rest */
                            STRMOVE(new_start, p1 + 1);
                            p1 = new_start - 1;
                        }
--- 5757,5772 ----
                                    first_line = lnum;
                                last_line = lnum + 1;
                            }
! #ifdef FEAT_TEXT_PROP
!                           adjust_props_for_split(lnum, plen, 1);
! #endif
!                           // all line numbers increase
                            ++sub_firstlnum;
                            ++lnum;
                            ++line2;
!                           // move the cursor to the new line, like Vi
                            ++curwin->w_cursor.lnum;
!                           // copy the rest
                            STRMOVE(new_start, p1 + 1);
                            p1 = new_start - 1;
                        }
*** ../vim-8.1.0690/src/textprop.c      2019-01-03 22:19:22.231686171 +0100
--- src/textprop.c      2019-01-04 23:07:43.454356479 +0100
***************
*** 18,23 ****
--- 18,25 ----
   *
   * TODO:
   * - Adjust text property column and length when text is inserted/deleted.
+  *   -> a :substitute with a multi-line match
+  *   -> search for changed_bytes() from ex_cmds.c
   * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
   * - Add an arrray for global_proptypes, to quickly lookup a prop type by ID
   * - Add an arrray for b_proptypes, to quickly lookup a prop type by ID
***************
*** 346,351 ****
--- 348,381 ----
      return (int)(proplen / sizeof(textprop_T));
  }
  
+ /*
+  * Set the text properties for line "lnum" to "props" with length "len".
+  * If "len" is zero text properties are removed, "props" is not used.
+  * Any existing text properties are dropped.
+  * Only works for the current buffer.
+  */
+     static void
+ set_text_props(linenr_T lnum, char_u *props, int len)
+ {
+     char_u *text;
+     char_u *newtext;
+     size_t textlen;
+ 
+     text = ml_get(lnum);
+     textlen = STRLEN(text) + 1;
+     newtext = alloc(textlen + len);
+     if (newtext == NULL)
+       return;
+     mch_memmove(newtext, text, textlen);
+     if (len > 0)
+       mch_memmove(newtext + textlen, props, len);
+     if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
+       vim_free(curbuf->b_ml.ml_line_ptr);
+     curbuf->b_ml.ml_line_ptr = newtext;
+     curbuf->b_ml.ml_line_len = textlen + len;
+     curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
+ }
+ 
      static proptype_T *
  find_type_by_id(hashtab_T *ht, int id)
  {
***************
*** 976,979 ****
--- 1006,1074 ----
      }
  }
  
+ /*
+  * Adjust text properties for a line that was split in two.
+  * "lnum" is the newly inserted line.  The text properties are now on the line
+  * below it.  "kept" is the number of bytes kept in the first line, while
+  * "deleted" is the number of bytes deleted.
+  */
+     void
+ adjust_props_for_split(linenr_T lnum, int kept, int deleted)
+ {
+     char_u    *props;
+     int               count;
+     garray_T    prevprop;
+     garray_T    nextprop;
+     int               i;
+     int               skipped = kept + deleted;
+ 
+     if (!curbuf->b_has_textprop)
+       return;
+     count = get_text_props(curbuf, lnum + 1, &props, FALSE);
+     ga_init2(&prevprop, sizeof(textprop_T), 10);
+     ga_init2(&nextprop, sizeof(textprop_T), 10);
+ 
+     // Get the text properties, which are at "lnum + 1".
+     // Keep the relevant ones in the first line, reducing the length if 
needed.
+     // Copy the ones that include the split to the second line.
+     // Move the ones after the split to the second line.
+     for (i = 0; i < count; ++i)
+     {
+       textprop_T  prop;
+       textprop_T *p;
+ 
+       // copy the prop to an aligned structure
+       mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
+ 
+       if (prop.tp_col < kept && ga_grow(&prevprop, 1) == OK)
+       {
+           p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
+           *p = prop;
+           if (p->tp_col + p->tp_len >= kept)
+               p->tp_len = kept - p->tp_col;
+           ++prevprop.ga_len;
+       }
+ 
+       if (prop.tp_col + prop.tp_len >= skipped && ga_grow(&nextprop, 1) == OK)
+       {
+           p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
+           *p = prop;
+           if (p->tp_col > skipped)
+               p->tp_col -= skipped - 1;
+           else
+           {
+               p->tp_len -= skipped - p->tp_col;
+               p->tp_col = 1;
+           }
+           ++nextprop.ga_len;
+       }
+     }
+ 
+     set_text_props(lnum, prevprop.ga_data, prevprop.ga_len * 
sizeof(textprop_T));
+     ga_clear(&prevprop);
+ 
+     set_text_props(lnum + 1, nextprop.ga_data, nextprop.ga_len * 
sizeof(textprop_T));
+     ga_clear(&nextprop);
+ }
+ 
  #endif // FEAT_TEXT_PROP
*** ../vim-8.1.0690/src/proto/textprop.pro      2019-01-01 19:47:17.854123944 
+0100
--- src/proto/textprop.pro      2019-01-04 21:47:36.892257155 +0100
***************
*** 14,17 ****
--- 14,18 ----
  void clear_global_prop_types(void);
  void clear_buf_prop_types(buf_T *buf);
  void adjust_prop_columns(linenr_T lnum, colnr_T col, int bytes_added);
+ void adjust_props_for_split(linenr_T lnum, int kept, int deleted);
  /* vim: set ft=c : */
*** ../vim-8.1.0690/src/testdir/test_textprop.vim       2019-01-04 
18:07:20.981806698 +0100
--- src/testdir/test_textprop.vim       2019-01-04 22:54:06.720964163 +0100
***************
*** 89,118 ****
    call setline(1, 'one two three')
    call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
    call prop_add(1, 5, {'length': 3, 'id': 12, 'type': 'two'})
!   call prop_add(1, 8, {'length': 5, 'id': 13, 'type': 'three'})
    call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
  endfunc
  
! let s:expected_props = [{'col': 1, 'length': 13, 'id': 14, 'type': 'whole', 
'start': 1, 'end': 1},
        \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 
1},
        \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 
1},
!       \ {'col': 8, 'length': 5, 'id': 13, 'type': 'three', 'start': 1, 'end': 
1},
        \ ]
  
  func Test_prop_add()
    new
    call AddPropTypes()
    call SetupPropsInFirstLine()
!   call assert_equal(s:expected_props, prop_list(1))
    call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 
'whole'})", 'E966:')
    call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 
'whole'})", 'E964:')
   
    " Insert a line above, text props must still be there.
    call append(0, 'empty')
!   call assert_equal(s:expected_props, prop_list(2))
    " Delete a line above, text props must still be there.
    1del
!   call assert_equal(s:expected_props, prop_list(1))
  
    " Prop without length or end column is zero length
    call prop_clear(1)
--- 89,122 ----
    call setline(1, 'one two three')
    call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
    call prop_add(1, 5, {'length': 3, 'id': 12, 'type': 'two'})
!   call prop_add(1, 9, {'length': 5, 'id': 13, 'type': 'three'})
    call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
  endfunc
  
! func Get_expected_props()
!   return [
!       \ {'col': 1, 'length': 13, 'id': 14, 'type': 'whole', 'start': 1, 
'end': 1},
        \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 
1},
        \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 
1},
!       \ {'col': 9, 'length': 5, 'id': 13, 'type': 'three', 'start': 1, 'end': 
1},
        \ ]
+ endfunc
  
  func Test_prop_add()
    new
    call AddPropTypes()
    call SetupPropsInFirstLine()
!   let expected_props = Get_expected_props()
!   call assert_equal(expected_props, prop_list(1))
    call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 
'whole'})", 'E966:')
    call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 
'whole'})", 'E964:')
   
    " Insert a line above, text props must still be there.
    call append(0, 'empty')
!   call assert_equal(expected_props, prop_list(2))
    " Delete a line above, text props must still be there.
    1del
!   call assert_equal(expected_props, prop_list(1))
  
    " Prop without length or end column is zero length
    call prop_clear(1)
***************
*** 128,134 ****
    new
    call AddPropTypes()
    call SetupPropsInFirstLine()
!   let props = deepcopy(s:expected_props)
    call assert_equal(props, prop_list(1))
  
    " remove by id
--- 132,138 ----
    new
    call AddPropTypes()
    call SetupPropsInFirstLine()
!   let props = Get_expected_props()
    call assert_equal(props, prop_list(1))
  
    " remove by id
***************
*** 236,242 ****
    new
    call AddPropTypes()
    call SetupPropsInFirstLine()
!   call assert_equal(s:expected_props, prop_list(1))
  
    call prop_clear(1)
    call assert_equal([], prop_list(1))
--- 240,246 ----
    new
    call AddPropTypes()
    call SetupPropsInFirstLine()
!   call assert_equal(Get_expected_props(), prop_list(1))
  
    call prop_clear(1)
    call assert_equal([], prop_list(1))
***************
*** 251,257 ****
    call SetupPropsInFirstLine()
    let bufnr = bufnr('')
    wincmd w
!   call assert_equal(s:expected_props, prop_list(1, {'bufnr': bufnr}))
  
    call prop_clear(1, 1, {'bufnr': bufnr})
    call assert_equal([], prop_list(1, {'bufnr': bufnr}))
--- 255,261 ----
    call SetupPropsInFirstLine()
    let bufnr = bufnr('')
    wincmd w
!   call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
  
    call prop_clear(1, 1, {'bufnr': bufnr})
    call assert_equal([], prop_list(1, {'bufnr': bufnr}))
***************
*** 265,271 ****
    new
    call AddPropTypes()
    call SetupPropsInFirstLine()
!   call assert_equal(s:expected_props, prop_list(1))
  
    call setline(1, 'foobar')
    call assert_equal([], prop_list(1))
--- 269,275 ----
    new
    call AddPropTypes()
    call SetupPropsInFirstLine()
!   call assert_equal(Get_expected_props(), prop_list(1))
  
    call setline(1, 'foobar')
    call assert_equal([], prop_list(1))
***************
*** 280,286 ****
    call SetupPropsInFirstLine()
    let bufnr = bufnr('')
    wincmd w
!   call assert_equal(s:expected_props, prop_list(1, {'bufnr': bufnr}))
  
    call setbufline(bufnr, 1, 'foobar')
    call assert_equal([], prop_list(1, {'bufnr': bufnr}))
--- 284,290 ----
    call SetupPropsInFirstLine()
    let bufnr = bufnr('')
    wincmd w
!   call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
  
    call setbufline(bufnr, 1, 'foobar')
    call assert_equal([], prop_list(1, {'bufnr': bufnr}))
***************
*** 289,294 ****
--- 293,346 ----
    call DeletePropTypes()
    bwipe!
  endfunc
+ 
+ func Test_prop_substitute()
+   new
+   " Set first line to 'one two three'
+   call AddPropTypes()
+   call SetupPropsInFirstLine()
+   let expected_props = Get_expected_props()
+   call assert_equal(expected_props, prop_list(1))
+ 
+   " Change "n" in "one" to XX: 'oXXe two three'
+   s/n/XX/
+   let expected_props[0].length += 1
+   let expected_props[1].length += 1
+   let expected_props[2].col += 1
+   let expected_props[3].col += 1
+   call assert_equal(expected_props, prop_list(1))
+ 
+   " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
+   s/t//g
+   let expected_props[0].length -= 2
+   let expected_props[2].length -= 1
+   let expected_props[3].length -= 1
+   let expected_props[3].col -= 1
+   call assert_equal(expected_props, prop_list(1))
+ 
+   " Split the line by changing w to line break: 'oXXe ', 'o hree'
+   " The long prop is split and spans both lines.
+   " The props on "two" and "three" move to the next line.
+   s/w/\r/
+   let new_props = [
+       \ copy(expected_props[0]),
+       \ copy(expected_props[2]),
+       \ copy(expected_props[3]),
+       \ ]
+   let expected_props[0].length = 5
+   unlet expected_props[3]
+   unlet expected_props[2]
+   call assert_equal(expected_props, prop_list(1))
+ 
+   let new_props[0].length = 6
+   let new_props[1].col = 1
+   let new_props[1].length = 1
+   let new_props[2].col = 3
+   call assert_equal(new_props, prop_list(2))
+ 
+   call DeletePropTypes()
+   bwipe!
+ endfunc
  
  " Setup a three line prop in lines 2 - 4.
  " Add short props in line 1 and 5.
*** ../vim-8.1.0690/src/version.c       2019-01-04 18:07:20.981806698 +0100
--- src/version.c       2019-01-04 18:39:18.369177640 +0100
***************
*** 801,802 ****
--- 801,804 ----
  {   /* Add new patch number below this line */
+ /**/
+     691,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
106. When told to "go to your room" you inform your parents that you
     can't...because you were kicked out and banned.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui