summaryrefslogtreecommitdiff
path: root/data/vim/patches/8.1.0691
blob: 4b41350fef437934132249575545266a22d37b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
To: vim_dev@googlegroups.com
Subject: Patch 8.1.0691
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
------------

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 -- Bram@Moolenaar.net -- 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    ///