blob: 0abae13278b31b632ea0698e602122f8ddea6a3a [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 *
13 * TODO:
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010014 * - Adjust text property column and length when text is inserted/deleted.
Bram Moolenaard79eef22019-05-24 20:41:55 +020015 * -> :substitute with multiple matches, issue #4427
Bram Moolenaar4164bb22019-01-04 23:09:49 +010016 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010017 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020018 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010019 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaarb56ac042018-12-28 23:22:40 +010020 * - Add an arrray for global_proptypes, to quickly lookup a prop type by ID
21 * - Add an arrray for b_proptypes, to quickly lookup a prop type by ID
22 * - Checking the text length to detect text properties is slow. Use a flag in
23 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010024 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
25 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010026 * - Perhaps have a window-local option to disable highlighting from text
27 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010028 */
29
30#include "vim.h"
31
32#if defined(FEAT_TEXT_PROP) || defined(PROTO)
33
34/*
35 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
36 * This avoids adding a pointer to the hashtable item.
37 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
38 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
39 * HI2PT() converts a hashitem pointer to a proptype pointer.
40 */
41#define PT2HIKEY(p) ((p)->pt_name)
42#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
43#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
44
45// The global text property types.
46static hashtab_T *global_proptypes = NULL;
47
48// The last used text property type ID.
49static int proptype_id = 0;
50
51static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
52static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld");
Bram Moolenaare3d31b02018-12-24 23:07:04 +010053static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010054
55/*
56 * Find a property type by name, return the hashitem.
57 * Returns NULL if the item can't be found.
58 */
59 static hashitem_T *
60find_prop_hi(char_u *name, buf_T *buf)
61{
62 hashtab_T *ht;
63 hashitem_T *hi;
64
65 if (*name == NUL)
66 return NULL;
67 if (buf == NULL)
68 ht = global_proptypes;
69 else
70 ht = buf->b_proptypes;
71
72 if (ht == NULL)
73 return NULL;
74 hi = hash_find(ht, name);
75 if (HASHITEM_EMPTY(hi))
76 return NULL;
77 return hi;
78}
79
80/*
81 * Like find_prop_hi() but return the property type.
82 */
83 static proptype_T *
84find_prop(char_u *name, buf_T *buf)
85{
86 hashitem_T *hi = find_prop_hi(name, buf);
87
88 if (hi == NULL)
89 return NULL;
90 return HI2PT(hi);
91}
92
93/*
94 * Lookup a property type by name. First in "buf" and when not found in the
95 * global types.
96 * When not found gives an error message and returns NULL.
97 */
98 static proptype_T *
99lookup_prop_type(char_u *name, buf_T *buf)
100{
101 proptype_T *type = find_prop(name, buf);
102
103 if (type == NULL)
104 type = find_prop(name, NULL);
105 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100106 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100107 return type;
108}
109
110/*
111 * Get an optional "bufnr" item from the dict in "arg".
112 * When the argument is not used or "bufnr" is not present then "buf" is
113 * unchanged.
114 * If "bufnr" is valid or not present return OK.
115 * When "arg" is not a dict or "bufnr" is invalide return FAIL.
116 */
117 static int
118get_bufnr_from_arg(typval_T *arg, buf_T **buf)
119{
120 dictitem_T *di;
121
122 if (arg->v_type != VAR_DICT)
123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100124 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100125 return FAIL;
126 }
127 if (arg->vval.v_dict == NULL)
128 return OK; // NULL dict is like an empty dict
129 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
130 if (di != NULL)
131 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200132 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100133 if (*buf == NULL)
134 return FAIL;
135 }
136 return OK;
137}
138
139/*
140 * prop_add({lnum}, {col}, {props})
141 */
142 void
143f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
144{
145 linenr_T lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100146 linenr_T start_lnum;
147 linenr_T end_lnum;
148 colnr_T start_col;
149 colnr_T end_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100150 dict_T *dict;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100151 char_u *type_name;
152 proptype_T *type;
153 buf_T *buf = curbuf;
154 int id = 0;
155 char_u *newtext;
156 int proplen;
157 size_t textlen;
Bram Moolenaarc6663882019-02-22 19:14:54 +0100158 char_u *props = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100159 char_u *newprops;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100160 textprop_T tmp_prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100161 int i;
162
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100163 start_lnum = tv_get_number(&argvars[0]);
164 start_col = tv_get_number(&argvars[1]);
165 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100167 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100168 return;
169 }
170 if (argvars[2].v_type != VAR_DICT)
171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100172 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100173 return;
174 }
175 dict = argvars[2].vval.v_dict;
176
177 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100179 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100180 return;
181 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100182 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100183
184 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
185 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100186 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
187 if (end_lnum < start_lnum)
188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100189 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100190 return;
191 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100192 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100193 else
194 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100195
196 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100197 {
198 long length = dict_get_number(dict, (char_u *)"length");
199
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100200 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100202 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100203 return;
204 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100205 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100206 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100207 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
208 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100209 end_col = dict_get_number(dict, (char_u *)"end_col");
210 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100212 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100213 return;
214 }
215 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100216 else if (start_lnum == end_lnum)
217 end_col = start_col;
218 else
219 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100220
221 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100222 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100223
224 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
225 return;
226
227 type = lookup_prop_type(type_name, buf);
228 if (type == NULL)
229 return;
230
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100231 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100233 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100234 return;
235 }
236 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100238 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100239 return;
240 }
241
Bram Moolenaard79eef22019-05-24 20:41:55 +0200242 if (buf->b_ml.ml_mfp == NULL)
243 ml_open(buf);
244
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100245 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100246 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100247 colnr_T col; // start column
248 long length; // in bytes
249
250 // Fetch the line to get the ml_line_len field updated.
251 proplen = get_text_props(buf, lnum, &props, TRUE);
252 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
253
254 if (lnum == start_lnum)
255 col = start_col;
256 else
257 col = 1;
258 if (col - 1 > (colnr_T)textlen)
259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100260 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100261 return;
262 }
263
264 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100265 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100266 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100267 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100268 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100269 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100270 if (length < 0)
271 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100272
273 // Allocate the new line with space for the new proprety.
274 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
275 if (newtext == NULL)
276 return;
277 // Copy the text, including terminating NUL.
278 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
279
280 // Find the index where to insert the new property.
281 // Since the text properties are not aligned properly when stored with the
282 // text, we need to copy them as bytes before using it as a struct.
283 for (i = 0; i < proplen; ++i)
284 {
285 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
286 sizeof(textprop_T));
287 if (tmp_prop.tp_col >= col)
288 break;
289 }
290 newprops = newtext + textlen;
291 if (i > 0)
292 mch_memmove(newprops, props, sizeof(textprop_T) * i);
293
294 tmp_prop.tp_col = col;
295 tmp_prop.tp_len = length;
296 tmp_prop.tp_id = id;
297 tmp_prop.tp_type = type->pt_id;
298 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
299 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
300 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
301 sizeof(textprop_T));
302
303 if (i < proplen)
304 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
305 props + i * sizeof(textprop_T),
306 sizeof(textprop_T) * (proplen - i));
307
308 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
309 vim_free(buf->b_ml.ml_line_ptr);
310 buf->b_ml.ml_line_ptr = newtext;
311 buf->b_ml.ml_line_len += sizeof(textprop_T);
312 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100313 }
314
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100315 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100316 redraw_buf_later(buf, NOT_VALID);
317}
318
319/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100320 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100321 * Returns the number of text properties and, when non-zero, a pointer to the
322 * first one in "props" (note that it is not aligned, therefore the char_u
323 * pointer).
324 */
325 int
326get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
327{
328 char_u *text;
329 size_t textlen;
330 size_t proplen;
331
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100332 // Be quick when no text property types have been defined or the buffer,
333 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200334 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100335 return 0;
336
337 // Fetch the line to get the ml_line_len field updated.
338 text = ml_get_buf(buf, lnum, will_change);
339 textlen = STRLEN(text) + 1;
340 proplen = buf->b_ml.ml_line_len - textlen;
341 if (proplen % sizeof(textprop_T) != 0)
342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100343 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100344 return 0;
345 }
346 if (proplen > 0)
347 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100348 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100349}
350
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100351/*
352 * Set the text properties for line "lnum" to "props" with length "len".
353 * If "len" is zero text properties are removed, "props" is not used.
354 * Any existing text properties are dropped.
355 * Only works for the current buffer.
356 */
357 static void
358set_text_props(linenr_T lnum, char_u *props, int len)
359{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100360 char_u *text;
361 char_u *newtext;
362 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100363
364 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100365 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100366 newtext = alloc(textlen + len);
367 if (newtext == NULL)
368 return;
369 mch_memmove(newtext, text, textlen);
370 if (len > 0)
371 mch_memmove(newtext + textlen, props, len);
372 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
373 vim_free(curbuf->b_ml.ml_line_ptr);
374 curbuf->b_ml.ml_line_ptr = newtext;
375 curbuf->b_ml.ml_line_len = textlen + len;
376 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
377}
378
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100379 static proptype_T *
380find_type_by_id(hashtab_T *ht, int id)
381{
382 long todo;
383 hashitem_T *hi;
384
385 if (ht == NULL)
386 return NULL;
387
388 // TODO: Make this faster by keeping a list of types sorted on ID and use
389 // a binary search.
390
391 todo = (long)ht->ht_used;
392 for (hi = ht->ht_array; todo > 0; ++hi)
393 {
394 if (!HASHITEM_EMPTY(hi))
395 {
396 proptype_T *prop = HI2PT(hi);
397
398 if (prop->pt_id == id)
399 return prop;
400 --todo;
401 }
402 }
403 return NULL;
404}
405
406/*
407 * Find a property type by ID in "buf" or globally.
408 * Returns NULL if not found.
409 */
410 proptype_T *
411text_prop_type_by_id(buf_T *buf, int id)
412{
413 proptype_T *type;
414
415 type = find_type_by_id(buf->b_proptypes, id);
416 if (type == NULL)
417 type = find_type_by_id(global_proptypes, id);
418 return type;
419}
420
421/*
422 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
423 */
424 void
425f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
426{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100427 linenr_T start = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100428 linenr_T end = start;
429 linenr_T lnum;
430 buf_T *buf = curbuf;
431
432 if (argvars[1].v_type != VAR_UNKNOWN)
433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100434 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100435 if (argvars[2].v_type != VAR_UNKNOWN)
436 {
437 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
438 return;
439 }
440 }
441 if (start < 1 || end < 1)
442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100443 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100444 return;
445 }
446
447 for (lnum = start; lnum <= end; ++lnum)
448 {
449 char_u *text;
450 size_t len;
451
452 if (lnum > buf->b_ml.ml_line_count)
453 break;
454 text = ml_get_buf(buf, lnum, FALSE);
455 len = STRLEN(text) + 1;
456 if ((size_t)buf->b_ml.ml_line_len > len)
457 {
458 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
459 {
460 char_u *newtext = vim_strsave(text);
461
462 // need to allocate the line now
463 if (newtext == NULL)
464 return;
465 buf->b_ml.ml_line_ptr = newtext;
466 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
467 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100468 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469 }
470 }
471 redraw_buf_later(buf, NOT_VALID);
472}
473
474/*
475 * prop_list({lnum} [, {bufnr}])
476 */
477 void
478f_prop_list(typval_T *argvars, typval_T *rettv)
479{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100480 linenr_T lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100481 buf_T *buf = curbuf;
482
483 if (argvars[1].v_type != VAR_UNKNOWN)
484 {
485 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
486 return;
487 }
488 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100490 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100491 return;
492 }
493
494 if (rettv_list_alloc(rettv) == OK)
495 {
496 char_u *text = ml_get_buf(buf, lnum, FALSE);
497 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100498 int count = (int)((buf->b_ml.ml_line_len - textlen)
499 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100500 int i;
501 textprop_T prop;
502 proptype_T *pt;
503
504 for (i = 0; i < count; ++i)
505 {
506 dict_T *d = dict_alloc();
507
508 if (d == NULL)
509 break;
510 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
511 sizeof(textprop_T));
512 dict_add_number(d, "col", prop.tp_col);
513 dict_add_number(d, "length", prop.tp_len);
514 dict_add_number(d, "id", prop.tp_id);
515 dict_add_number(d, "start", !(prop.tp_flags & TP_FLAG_CONT_PREV));
516 dict_add_number(d, "end", !(prop.tp_flags & TP_FLAG_CONT_NEXT));
517 pt = text_prop_type_by_id(buf, prop.tp_type);
518 if (pt != NULL)
519 dict_add_string(d, "type", pt->pt_name);
520
521 list_append_dict(rettv->vval.v_list, d);
522 }
523 }
524}
525
526/*
527 * prop_remove({props} [, {lnum} [, {lnum_end}]])
528 */
529 void
530f_prop_remove(typval_T *argvars, typval_T *rettv)
531{
532 linenr_T start = 1;
533 linenr_T end = 0;
534 linenr_T lnum;
535 dict_T *dict;
536 buf_T *buf = curbuf;
537 dictitem_T *di;
538 int do_all = FALSE;
539 int id = -1;
540 int type_id = -1;
541
542 rettv->vval.v_number = 0;
543 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100545 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100546 return;
547 }
548
549 if (argvars[1].v_type != VAR_UNKNOWN)
550 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100551 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100552 end = start;
553 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100554 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100555 if (start < 1 || end < 1)
556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100557 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100558 return;
559 }
560 }
561
562 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200563 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
564 return;
565 if (buf->b_ml.ml_mfp == NULL)
566 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100567
568 di = dict_find(dict, (char_u*)"all", -1);
569 if (di != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100570 do_all = dict_get_number(dict, (char_u *)"all");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100571
572 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100573 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100574 if (dict_find(dict, (char_u *)"type", -1))
575 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100576 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100577 proptype_T *type = lookup_prop_type(name, buf);
578
579 if (type == NULL)
580 return;
581 type_id = type->pt_id;
582 }
583 if (id == -1 && type_id == -1)
584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100585 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100586 return;
587 }
588
589 if (end == 0)
590 end = buf->b_ml.ml_line_count;
591 for (lnum = start; lnum <= end; ++lnum)
592 {
593 char_u *text;
594 size_t len;
595
596 if (lnum > buf->b_ml.ml_line_count)
597 break;
598 text = ml_get_buf(buf, lnum, FALSE);
599 len = STRLEN(text) + 1;
600 if ((size_t)buf->b_ml.ml_line_len > len)
601 {
602 static textprop_T textprop; // static because of alignment
603 unsigned idx;
604
605 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
606 / sizeof(textprop_T); ++idx)
607 {
608 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
609 + idx * sizeof(textprop_T);
610 size_t taillen;
611
612 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
613 if (textprop.tp_id == id || textprop.tp_type == type_id)
614 {
615 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
616 {
617 char_u *newptr = alloc(buf->b_ml.ml_line_len);
618
619 // need to allocate the line to be able to change it
620 if (newptr == NULL)
621 return;
622 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
623 buf->b_ml.ml_line_len);
624 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100625 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
626
627 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200628 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100629 }
630
631 taillen = buf->b_ml.ml_line_len - len
632 - (idx + 1) * sizeof(textprop_T);
633 if (taillen > 0)
634 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
635 taillen);
636 buf->b_ml.ml_line_len -= sizeof(textprop_T);
637 --idx;
638
639 ++rettv->vval.v_number;
640 if (!do_all)
641 break;
642 }
643 }
644 }
645 }
646 redraw_buf_later(buf, NOT_VALID);
647}
648
649/*
650 * Common for f_prop_type_add() and f_prop_type_change().
651 */
652 void
653prop_type_set(typval_T *argvars, int add)
654{
655 char_u *name;
656 buf_T *buf = NULL;
657 dict_T *dict;
658 dictitem_T *di;
659 proptype_T *prop;
660
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100661 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100662 if (*name == NUL)
663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100664 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100665 return;
666 }
667
668 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
669 return;
670 dict = argvars[1].vval.v_dict;
671
672 prop = find_prop(name, buf);
673 if (add)
674 {
675 hashtab_T **htp;
676
677 if (prop != NULL)
678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100679 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100680 return;
681 }
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200682 prop = (proptype_T *)alloc_clear(sizeof(proptype_T) + STRLEN(name));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100683 if (prop == NULL)
684 return;
685 STRCPY(prop->pt_name, name);
686 prop->pt_id = ++proptype_id;
687 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
688 if (*htp == NULL)
689 {
690 *htp = (hashtab_T *)alloc(sizeof(hashtab_T));
691 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100692 {
693 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100694 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100695 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100696 hash_init(*htp);
697 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100698 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100699 }
700 else
701 {
702 if (prop == NULL)
703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100704 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100705 return;
706 }
707 }
708
709 if (dict != NULL)
710 {
711 di = dict_find(dict, (char_u *)"highlight", -1);
712 if (di != NULL)
713 {
714 char_u *highlight;
715 int hl_id = 0;
716
Bram Moolenaar8f667172018-12-14 15:38:31 +0100717 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100718 if (highlight != NULL && *highlight != NUL)
719 hl_id = syn_name2id(highlight);
720 if (hl_id <= 0)
721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100722 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100723 highlight == NULL ? (char_u *)"" : highlight);
724 return;
725 }
726 prop->pt_hl_id = hl_id;
727 }
728
Bram Moolenaar58187f12019-05-05 16:33:47 +0200729 di = dict_find(dict, (char_u *)"combine", -1);
730 if (di != NULL)
731 {
732 if (tv_get_number(&di->di_tv))
733 prop->pt_flags |= PT_FLAG_COMBINE;
734 else
735 prop->pt_flags &= ~PT_FLAG_COMBINE;
736 }
737
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100738 di = dict_find(dict, (char_u *)"priority", -1);
739 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100740 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100741
742 di = dict_find(dict, (char_u *)"start_incl", -1);
743 if (di != NULL)
744 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100745 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100746 prop->pt_flags |= PT_FLAG_INS_START_INCL;
747 else
748 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
749 }
750
751 di = dict_find(dict, (char_u *)"end_incl", -1);
752 if (di != NULL)
753 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100754 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100755 prop->pt_flags |= PT_FLAG_INS_END_INCL;
756 else
757 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
758 }
759 }
760}
761
762/*
763 * prop_type_add({name}, {props})
764 */
765 void
766f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
767{
768 prop_type_set(argvars, TRUE);
769}
770
771/*
772 * prop_type_change({name}, {props})
773 */
774 void
775f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
776{
777 prop_type_set(argvars, FALSE);
778}
779
780/*
781 * prop_type_delete({name} [, {bufnr}])
782 */
783 void
784f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
785{
786 char_u *name;
787 buf_T *buf = NULL;
788 hashitem_T *hi;
789
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100790 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100791 if (*name == NUL)
792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100793 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100794 return;
795 }
796
797 if (argvars[1].v_type != VAR_UNKNOWN)
798 {
799 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
800 return;
801 }
802
803 hi = find_prop_hi(name, buf);
804 if (hi != NULL)
805 {
806 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100807 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100808
809 if (buf == NULL)
810 ht = global_proptypes;
811 else
812 ht = buf->b_proptypes;
813 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100814 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100815 }
816}
817
818/*
819 * prop_type_get({name} [, {bufnr}])
820 */
821 void
822f_prop_type_get(typval_T *argvars, typval_T *rettv UNUSED)
823{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100824 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100825
826 if (*name == NUL)
827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100829 return;
830 }
831 if (rettv_dict_alloc(rettv) == OK)
832 {
833 proptype_T *prop = NULL;
834 buf_T *buf = NULL;
835
836 if (argvars[1].v_type != VAR_UNKNOWN)
837 {
838 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
839 return;
840 }
841
842 prop = find_prop(name, buf);
843 if (prop != NULL)
844 {
845 dict_T *d = rettv->vval.v_dict;
846
847 if (prop->pt_hl_id > 0)
848 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
849 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +0200850 dict_add_number(d, "combine",
851 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100852 dict_add_number(d, "start_incl",
853 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
854 dict_add_number(d, "end_incl",
855 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
856 if (buf != NULL)
857 dict_add_number(d, "bufnr", buf->b_fnum);
858 }
859 }
860}
861
862 static void
863list_types(hashtab_T *ht, list_T *l)
864{
865 long todo;
866 hashitem_T *hi;
867
868 todo = (long)ht->ht_used;
869 for (hi = ht->ht_array; todo > 0; ++hi)
870 {
871 if (!HASHITEM_EMPTY(hi))
872 {
873 proptype_T *prop = HI2PT(hi);
874
875 list_append_string(l, prop->pt_name, -1);
876 --todo;
877 }
878 }
879}
880
881/*
882 * prop_type_list([{bufnr}])
883 */
884 void
885f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
886{
887 buf_T *buf = NULL;
888
889 if (rettv_list_alloc(rettv) == OK)
890 {
891 if (argvars[0].v_type != VAR_UNKNOWN)
892 {
893 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
894 return;
895 }
896 if (buf == NULL)
897 {
898 if (global_proptypes != NULL)
899 list_types(global_proptypes, rettv->vval.v_list);
900 }
901 else if (buf->b_proptypes != NULL)
902 list_types(buf->b_proptypes, rettv->vval.v_list);
903 }
904}
905
906/*
907 * Free all property types in "ht".
908 */
909 static void
910clear_ht_prop_types(hashtab_T *ht)
911{
912 long todo;
913 hashitem_T *hi;
914
915 if (ht == NULL)
916 return;
917
918 todo = (long)ht->ht_used;
919 for (hi = ht->ht_array; todo > 0; ++hi)
920 {
921 if (!HASHITEM_EMPTY(hi))
922 {
923 proptype_T *prop = HI2PT(hi);
924
925 vim_free(prop);
926 --todo;
927 }
928 }
929
930 hash_clear(ht);
931 vim_free(ht);
932}
933
934#if defined(EXITFREE) || defined(PROTO)
935/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100936 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100937 */
938 void
939clear_global_prop_types(void)
940{
941 clear_ht_prop_types(global_proptypes);
942 global_proptypes = NULL;
943}
944#endif
945
946/*
947 * Free all property types for "buf".
948 */
949 void
950clear_buf_prop_types(buf_T *buf)
951{
952 clear_ht_prop_types(buf->b_proptypes);
953 buf->b_proptypes = NULL;
954}
955
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100956/*
957 * Adjust the columns of text properties in line "lnum" after position "col" to
958 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100959 * Note that "col" is zero-based, while tp_col is one-based.
960 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200961 * "flags" can have:
962 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
963 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +0200964 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200965 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100966 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200967 int
Bram Moolenaar196d1572019-01-02 23:47:18 +0100968adjust_prop_columns(
969 linenr_T lnum,
970 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200971 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200972 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100973{
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100974 int proplen;
975 char_u *props;
976 textprop_T tmp_prop;
977 proptype_T *pt;
978 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +0100979 int ri, wi;
980 size_t textlen;
981
982 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200983 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100984
985 proplen = get_text_props(curbuf, lnum, &props, TRUE);
986 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200987 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +0100988 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100989
Bram Moolenaar196d1572019-01-02 23:47:18 +0100990 wi = 0; // write index
991 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100992 {
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200993 int start_incl;
994
Bram Moolenaar196d1572019-01-02 23:47:18 +0100995 mch_memmove(&tmp_prop, props + ri * sizeof(textprop_T),
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100996 sizeof(textprop_T));
997 pt = text_prop_type_by_id(curbuf, tmp_prop.tp_type);
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200998 start_incl = (flags & APC_SUBSTITUTE) ||
999 (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001000
Bram Moolenaar196d1572019-01-02 23:47:18 +01001001 if (bytes_added > 0
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001002 && (tmp_prop.tp_col >= col + (start_incl ? 2 : 1)))
1003 {
1004 if (tmp_prop.tp_col < col + (start_incl ? 2 : 1))
1005 {
1006 tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
1007 tmp_prop.tp_col = col + 1;
1008 }
1009 else
1010 tmp_prop.tp_col += bytes_added;
1011 // Save for undo if requested and not done yet.
1012 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
1013 u_savesub(lnum);
1014 dirty = TRUE;
1015 }
1016 else if (bytes_added <= 0 && (tmp_prop.tp_col > col + 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001017 {
Bram Moolenaar8055d172019-05-17 22:57:26 +02001018 if (tmp_prop.tp_col + bytes_added < col + 1)
1019 {
1020 tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
1021 tmp_prop.tp_col = col + 1;
1022 }
1023 else
1024 tmp_prop.tp_col += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001025 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001026 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001027 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001028 dirty = TRUE;
Bram Moolenaar8055d172019-05-17 22:57:26 +02001029 if (tmp_prop.tp_len <= 0)
1030 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001031 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001032 else if (tmp_prop.tp_len > 0
1033 && tmp_prop.tp_col + tmp_prop.tp_len > col
Bram Moolenaar4614f532019-01-06 12:54:55 +01001034 + ((pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
Bram Moolenaar196d1572019-01-02 23:47:18 +01001035 ? 0 : 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001036 {
Bram Moolenaar8055d172019-05-17 22:57:26 +02001037 int after = col - bytes_added
1038 - (tmp_prop.tp_col - 1 + tmp_prop.tp_len);
1039 if (after > 0)
1040 tmp_prop.tp_len += bytes_added + after;
1041 else
1042 tmp_prop.tp_len += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001043 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001044 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001045 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001046 dirty = TRUE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001047 if (tmp_prop.tp_len <= 0)
1048 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001049 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001050 mch_memmove(props + wi * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001051 sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001052 ++wi;
1053 }
1054 if (dirty)
1055 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001056 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1057
1058 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1059 curbuf->b_ml.ml_line_ptr =
1060 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001061 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001062 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001063 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001064 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001065}
1066
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001067/*
1068 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001069 * "lnum_props" is the line that has the properties from before the split.
1070 * "lnum_top" is the top line.
1071 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001072 * "deleted" is the number of bytes deleted.
1073 */
1074 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001075adjust_props_for_split(
1076 linenr_T lnum_props,
1077 linenr_T lnum_top,
1078 int kept,
1079 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001080{
1081 char_u *props;
1082 int count;
1083 garray_T prevprop;
1084 garray_T nextprop;
1085 int i;
1086 int skipped = kept + deleted;
1087
1088 if (!curbuf->b_has_textprop)
1089 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001090
1091 // Get the text properties from "lnum_props".
1092 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001093 ga_init2(&prevprop, sizeof(textprop_T), 10);
1094 ga_init2(&nextprop, sizeof(textprop_T), 10);
1095
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001096 // Keep the relevant ones in the first line, reducing the length if needed.
1097 // Copy the ones that include the split to the second line.
1098 // Move the ones after the split to the second line.
1099 for (i = 0; i < count; ++i)
1100 {
1101 textprop_T prop;
1102 textprop_T *p;
1103
1104 // copy the prop to an aligned structure
1105 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1106
1107 if (prop.tp_col < kept && ga_grow(&prevprop, 1) == OK)
1108 {
1109 p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1110 *p = prop;
1111 if (p->tp_col + p->tp_len >= kept)
1112 p->tp_len = kept - p->tp_col;
1113 ++prevprop.ga_len;
1114 }
1115
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001116 // Only add the property to the next line if the length is bigger than
1117 // zero.
1118 if (prop.tp_col + prop.tp_len > skipped && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001119 {
1120 p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
1121 *p = prop;
1122 if (p->tp_col > skipped)
1123 p->tp_col -= skipped - 1;
1124 else
1125 {
1126 p->tp_len -= skipped - p->tp_col;
1127 p->tp_col = 1;
1128 }
1129 ++nextprop.ga_len;
1130 }
1131 }
1132
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001133 set_text_props(lnum_top, prevprop.ga_data,
1134 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001135 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001136 set_text_props(lnum_top + 1, nextprop.ga_data,
1137 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001138 ga_clear(&nextprop);
1139}
1140
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001141/*
1142 * Line "lnum" has been joined and will end up at column "col" in the new line.
1143 * "removed" bytes have been removed from the start of the line, properties
1144 * there are to be discarded.
1145 * Move the adjusted text properties to an allocated string, store it in
1146 * "prop_line" and adjust the columns.
1147 */
1148 void
1149adjust_props_for_join(
1150 linenr_T lnum,
1151 textprop_T **prop_line,
1152 int *prop_length,
1153 long col,
1154 int removed)
1155{
1156 int proplen;
1157 char_u *props;
1158 int ri;
1159 int wi = 0;
1160
1161 proplen = get_text_props(curbuf, lnum, &props, FALSE);
1162 if (proplen > 0)
1163 {
1164 *prop_line = (textprop_T *)alloc(proplen * (int)sizeof(textprop_T));
1165 if (*prop_line != NULL)
1166 {
1167 for (ri = 0; ri < proplen; ++ri)
1168 {
1169 textprop_T *cp = *prop_line + wi;
1170
1171 mch_memmove(cp, props + ri * sizeof(textprop_T),
1172 sizeof(textprop_T));
1173 if (cp->tp_col + cp->tp_len > removed)
1174 {
1175 if (cp->tp_col > removed)
1176 cp->tp_col += col;
1177 else
1178 {
1179 // property was partly deleted, make it shorter
1180 cp->tp_len -= removed - cp->tp_col;
1181 cp->tp_col = col;
1182 }
1183 ++wi;
1184 }
1185 }
1186 }
1187 *prop_length = wi;
1188 }
1189}
1190
1191/*
1192 * After joining lines: concatenate the text and the properties of all joined
1193 * lines into one line and replace the line.
1194 */
1195 void
1196join_prop_lines(
1197 linenr_T lnum,
1198 char_u *newp,
1199 textprop_T **prop_lines,
1200 int *prop_lengths,
1201 int count)
1202{
1203 size_t proplen = 0;
1204 size_t oldproplen;
1205 char_u *props;
1206 int i;
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001207 size_t len;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001208 char_u *line;
1209 size_t l;
1210
1211 for (i = 0; i < count - 1; ++i)
1212 proplen += prop_lengths[i];
1213 if (proplen == 0)
1214 {
1215 ml_replace(lnum, newp, FALSE);
1216 return;
1217 }
1218
1219 // get existing properties of the joined line
1220 oldproplen = get_text_props(curbuf, lnum, &props, FALSE);
1221
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001222 len = STRLEN(newp) + 1;
Bram Moolenaar51e14382019-05-25 20:21:28 +02001223 line = alloc(len + (oldproplen + proplen) * sizeof(textprop_T));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001224 if (line == NULL)
1225 return;
1226 mch_memmove(line, newp, len);
1227 l = oldproplen * sizeof(textprop_T);
1228 mch_memmove(line + len, props, l);
1229 len += l;
1230
1231 for (i = 0; i < count - 1; ++i)
1232 if (prop_lines[i] != NULL)
1233 {
1234 l = prop_lengths[i] * sizeof(textprop_T);
1235 mch_memmove(line + len, prop_lines[i], l);
1236 len += l;
1237 vim_free(prop_lines[i]);
1238 }
1239
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001240 ml_replace_len(lnum, line, (colnr_T)len, TRUE, FALSE);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001241 vim_free(newp);
1242 vim_free(prop_lines);
1243 vim_free(prop_lengths);
1244}
1245
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001246#endif // FEAT_TEXT_PROP