blob: e993afce8c3cfe61ec5f647edebd2a2b58c4c580 [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 Moolenaar4164bb22019-01-04 23:09:49 +010015 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010016 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020017 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010018 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaarb56ac042018-12-28 23:22:40 +010019 * - Add an arrray for global_proptypes, to quickly lookup a prop type by ID
20 * - Add an arrray for b_proptypes, to quickly lookup a prop type by ID
21 * - Checking the text length to detect text properties is slow. Use a flag in
22 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010023 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
24 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010025 * - Perhaps have a window-local option to disable highlighting from text
26 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010027 */
28
29#include "vim.h"
30
31#if defined(FEAT_TEXT_PROP) || defined(PROTO)
32
33/*
34 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
35 * This avoids adding a pointer to the hashtable item.
36 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
37 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
38 * HI2PT() converts a hashitem pointer to a proptype pointer.
39 */
40#define PT2HIKEY(p) ((p)->pt_name)
41#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
42#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
43
44// The global text property types.
45static hashtab_T *global_proptypes = NULL;
46
47// The last used text property type ID.
48static int proptype_id = 0;
49
50static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
51static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld");
Bram Moolenaare3d31b02018-12-24 23:07:04 +010052static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010053
54/*
55 * Find a property type by name, return the hashitem.
56 * Returns NULL if the item can't be found.
57 */
58 static hashitem_T *
59find_prop_hi(char_u *name, buf_T *buf)
60{
61 hashtab_T *ht;
62 hashitem_T *hi;
63
64 if (*name == NUL)
65 return NULL;
66 if (buf == NULL)
67 ht = global_proptypes;
68 else
69 ht = buf->b_proptypes;
70
71 if (ht == NULL)
72 return NULL;
73 hi = hash_find(ht, name);
74 if (HASHITEM_EMPTY(hi))
75 return NULL;
76 return hi;
77}
78
79/*
80 * Like find_prop_hi() but return the property type.
81 */
82 static proptype_T *
83find_prop(char_u *name, buf_T *buf)
84{
85 hashitem_T *hi = find_prop_hi(name, buf);
86
87 if (hi == NULL)
88 return NULL;
89 return HI2PT(hi);
90}
91
92/*
93 * Lookup a property type by name. First in "buf" and when not found in the
94 * global types.
95 * When not found gives an error message and returns NULL.
96 */
97 static proptype_T *
98lookup_prop_type(char_u *name, buf_T *buf)
99{
100 proptype_T *type = find_prop(name, buf);
101
102 if (type == NULL)
103 type = find_prop(name, NULL);
104 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100105 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100106 return type;
107}
108
109/*
110 * Get an optional "bufnr" item from the dict in "arg".
111 * When the argument is not used or "bufnr" is not present then "buf" is
112 * unchanged.
113 * If "bufnr" is valid or not present return OK.
114 * When "arg" is not a dict or "bufnr" is invalide return FAIL.
115 */
116 static int
117get_bufnr_from_arg(typval_T *arg, buf_T **buf)
118{
119 dictitem_T *di;
120
121 if (arg->v_type != VAR_DICT)
122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100123 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100124 return FAIL;
125 }
126 if (arg->vval.v_dict == NULL)
127 return OK; // NULL dict is like an empty dict
128 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
129 if (di != NULL)
130 {
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +0100131 *buf = tv_get_buf(&di->di_tv, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100132 if (*buf == NULL)
133 return FAIL;
134 }
135 return OK;
136}
137
138/*
139 * prop_add({lnum}, {col}, {props})
140 */
141 void
142f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
143{
144 linenr_T lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100145 linenr_T start_lnum;
146 linenr_T end_lnum;
147 colnr_T start_col;
148 colnr_T end_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100149 dict_T *dict;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100150 char_u *type_name;
151 proptype_T *type;
152 buf_T *buf = curbuf;
153 int id = 0;
154 char_u *newtext;
155 int proplen;
156 size_t textlen;
Bram Moolenaarc6663882019-02-22 19:14:54 +0100157 char_u *props = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100158 char_u *newprops;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100159 textprop_T tmp_prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100160 int i;
161
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100162 start_lnum = tv_get_number(&argvars[0]);
163 start_col = tv_get_number(&argvars[1]);
164 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100166 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100167 return;
168 }
169 if (argvars[2].v_type != VAR_DICT)
170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100171 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100172 return;
173 }
174 dict = argvars[2].vval.v_dict;
175
176 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100178 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100179 return;
180 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100181 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100182
183 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
184 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100185 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
186 if (end_lnum < start_lnum)
187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100188 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100189 return;
190 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100191 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100192 else
193 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100194
195 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100196 {
197 long length = dict_get_number(dict, (char_u *)"length");
198
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100199 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100201 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100202 return;
203 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100204 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100205 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100206 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
207 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100208 end_col = dict_get_number(dict, (char_u *)"end_col");
209 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100211 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100212 return;
213 }
214 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100215 else if (start_lnum == end_lnum)
216 end_col = start_col;
217 else
218 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100219
220 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100221 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100222
223 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
224 return;
225
226 type = lookup_prop_type(type_name, buf);
227 if (type == NULL)
228 return;
229
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100230 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100232 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100233 return;
234 }
235 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100237 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100238 return;
239 }
240
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100241 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100242 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100243 colnr_T col; // start column
244 long length; // in bytes
245
246 // Fetch the line to get the ml_line_len field updated.
247 proplen = get_text_props(buf, lnum, &props, TRUE);
248 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
249
250 if (lnum == start_lnum)
251 col = start_col;
252 else
253 col = 1;
254 if (col - 1 > (colnr_T)textlen)
255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100256 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100257 return;
258 }
259
260 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100261 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100262 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100263 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100264 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100265 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100266 if (length < 0)
267 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100268
269 // Allocate the new line with space for the new proprety.
270 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
271 if (newtext == NULL)
272 return;
273 // Copy the text, including terminating NUL.
274 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
275
276 // Find the index where to insert the new property.
277 // Since the text properties are not aligned properly when stored with the
278 // text, we need to copy them as bytes before using it as a struct.
279 for (i = 0; i < proplen; ++i)
280 {
281 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
282 sizeof(textprop_T));
283 if (tmp_prop.tp_col >= col)
284 break;
285 }
286 newprops = newtext + textlen;
287 if (i > 0)
288 mch_memmove(newprops, props, sizeof(textprop_T) * i);
289
290 tmp_prop.tp_col = col;
291 tmp_prop.tp_len = length;
292 tmp_prop.tp_id = id;
293 tmp_prop.tp_type = type->pt_id;
294 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
295 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
296 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
297 sizeof(textprop_T));
298
299 if (i < proplen)
300 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
301 props + i * sizeof(textprop_T),
302 sizeof(textprop_T) * (proplen - i));
303
304 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
305 vim_free(buf->b_ml.ml_line_ptr);
306 buf->b_ml.ml_line_ptr = newtext;
307 buf->b_ml.ml_line_len += sizeof(textprop_T);
308 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100309 }
310
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100311 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100312 redraw_buf_later(buf, NOT_VALID);
313}
314
315/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100316 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100317 * Returns the number of text properties and, when non-zero, a pointer to the
318 * first one in "props" (note that it is not aligned, therefore the char_u
319 * pointer).
320 */
321 int
322get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
323{
324 char_u *text;
325 size_t textlen;
326 size_t proplen;
327
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100328 // Be quick when no text property types have been defined or the buffer,
329 // unless we are adding one.
330 if (!buf->b_has_textprop && !will_change)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100331 return 0;
332
333 // Fetch the line to get the ml_line_len field updated.
334 text = ml_get_buf(buf, lnum, will_change);
335 textlen = STRLEN(text) + 1;
336 proplen = buf->b_ml.ml_line_len - textlen;
337 if (proplen % sizeof(textprop_T) != 0)
338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100339 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100340 return 0;
341 }
342 if (proplen > 0)
343 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100344 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100345}
346
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100347/*
348 * Set the text properties for line "lnum" to "props" with length "len".
349 * If "len" is zero text properties are removed, "props" is not used.
350 * Any existing text properties are dropped.
351 * Only works for the current buffer.
352 */
353 static void
354set_text_props(linenr_T lnum, char_u *props, int len)
355{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100356 char_u *text;
357 char_u *newtext;
358 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100359
360 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100361 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100362 newtext = alloc(textlen + len);
363 if (newtext == NULL)
364 return;
365 mch_memmove(newtext, text, textlen);
366 if (len > 0)
367 mch_memmove(newtext + textlen, props, len);
368 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
369 vim_free(curbuf->b_ml.ml_line_ptr);
370 curbuf->b_ml.ml_line_ptr = newtext;
371 curbuf->b_ml.ml_line_len = textlen + len;
372 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
373}
374
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100375 static proptype_T *
376find_type_by_id(hashtab_T *ht, int id)
377{
378 long todo;
379 hashitem_T *hi;
380
381 if (ht == NULL)
382 return NULL;
383
384 // TODO: Make this faster by keeping a list of types sorted on ID and use
385 // a binary search.
386
387 todo = (long)ht->ht_used;
388 for (hi = ht->ht_array; todo > 0; ++hi)
389 {
390 if (!HASHITEM_EMPTY(hi))
391 {
392 proptype_T *prop = HI2PT(hi);
393
394 if (prop->pt_id == id)
395 return prop;
396 --todo;
397 }
398 }
399 return NULL;
400}
401
402/*
403 * Find a property type by ID in "buf" or globally.
404 * Returns NULL if not found.
405 */
406 proptype_T *
407text_prop_type_by_id(buf_T *buf, int id)
408{
409 proptype_T *type;
410
411 type = find_type_by_id(buf->b_proptypes, id);
412 if (type == NULL)
413 type = find_type_by_id(global_proptypes, id);
414 return type;
415}
416
417/*
418 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
419 */
420 void
421f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
422{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100423 linenr_T start = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100424 linenr_T end = start;
425 linenr_T lnum;
426 buf_T *buf = curbuf;
427
428 if (argvars[1].v_type != VAR_UNKNOWN)
429 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100430 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100431 if (argvars[2].v_type != VAR_UNKNOWN)
432 {
433 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
434 return;
435 }
436 }
437 if (start < 1 || end < 1)
438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100439 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100440 return;
441 }
442
443 for (lnum = start; lnum <= end; ++lnum)
444 {
445 char_u *text;
446 size_t len;
447
448 if (lnum > buf->b_ml.ml_line_count)
449 break;
450 text = ml_get_buf(buf, lnum, FALSE);
451 len = STRLEN(text) + 1;
452 if ((size_t)buf->b_ml.ml_line_len > len)
453 {
454 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
455 {
456 char_u *newtext = vim_strsave(text);
457
458 // need to allocate the line now
459 if (newtext == NULL)
460 return;
461 buf->b_ml.ml_line_ptr = newtext;
462 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
463 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100464 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100465 }
466 }
467 redraw_buf_later(buf, NOT_VALID);
468}
469
470/*
471 * prop_list({lnum} [, {bufnr}])
472 */
473 void
474f_prop_list(typval_T *argvars, typval_T *rettv)
475{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100476 linenr_T lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100477 buf_T *buf = curbuf;
478
479 if (argvars[1].v_type != VAR_UNKNOWN)
480 {
481 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
482 return;
483 }
484 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100486 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100487 return;
488 }
489
490 if (rettv_list_alloc(rettv) == OK)
491 {
492 char_u *text = ml_get_buf(buf, lnum, FALSE);
493 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100494 int count = (int)((buf->b_ml.ml_line_len - textlen)
495 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100496 int i;
497 textprop_T prop;
498 proptype_T *pt;
499
500 for (i = 0; i < count; ++i)
501 {
502 dict_T *d = dict_alloc();
503
504 if (d == NULL)
505 break;
506 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
507 sizeof(textprop_T));
508 dict_add_number(d, "col", prop.tp_col);
509 dict_add_number(d, "length", prop.tp_len);
510 dict_add_number(d, "id", prop.tp_id);
511 dict_add_number(d, "start", !(prop.tp_flags & TP_FLAG_CONT_PREV));
512 dict_add_number(d, "end", !(prop.tp_flags & TP_FLAG_CONT_NEXT));
513 pt = text_prop_type_by_id(buf, prop.tp_type);
514 if (pt != NULL)
515 dict_add_string(d, "type", pt->pt_name);
516
517 list_append_dict(rettv->vval.v_list, d);
518 }
519 }
520}
521
522/*
523 * prop_remove({props} [, {lnum} [, {lnum_end}]])
524 */
525 void
526f_prop_remove(typval_T *argvars, typval_T *rettv)
527{
528 linenr_T start = 1;
529 linenr_T end = 0;
530 linenr_T lnum;
531 dict_T *dict;
532 buf_T *buf = curbuf;
533 dictitem_T *di;
534 int do_all = FALSE;
535 int id = -1;
536 int type_id = -1;
537
538 rettv->vval.v_number = 0;
539 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100541 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100542 return;
543 }
544
545 if (argvars[1].v_type != VAR_UNKNOWN)
546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100547 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100548 end = start;
549 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100550 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100551 if (start < 1 || end < 1)
552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100553 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100554 return;
555 }
556 }
557
558 dict = argvars[0].vval.v_dict;
559 di = dict_find(dict, (char_u *)"bufnr", -1);
560 if (di != NULL)
561 {
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +0100562 buf = tv_get_buf(&di->di_tv, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100563 if (buf == NULL)
564 return;
565 }
566
567 di = dict_find(dict, (char_u*)"all", -1);
568 if (di != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100569 do_all = dict_get_number(dict, (char_u *)"all");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100570
571 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100572 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100573 if (dict_find(dict, (char_u *)"type", -1))
574 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100575 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100576 proptype_T *type = lookup_prop_type(name, buf);
577
578 if (type == NULL)
579 return;
580 type_id = type->pt_id;
581 }
582 if (id == -1 && type_id == -1)
583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100584 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100585 return;
586 }
587
588 if (end == 0)
589 end = buf->b_ml.ml_line_count;
590 for (lnum = start; lnum <= end; ++lnum)
591 {
592 char_u *text;
593 size_t len;
594
595 if (lnum > buf->b_ml.ml_line_count)
596 break;
597 text = ml_get_buf(buf, lnum, FALSE);
598 len = STRLEN(text) + 1;
599 if ((size_t)buf->b_ml.ml_line_len > len)
600 {
601 static textprop_T textprop; // static because of alignment
602 unsigned idx;
603
604 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
605 / sizeof(textprop_T); ++idx)
606 {
607 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
608 + idx * sizeof(textprop_T);
609 size_t taillen;
610
611 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
612 if (textprop.tp_id == id || textprop.tp_type == type_id)
613 {
614 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
615 {
616 char_u *newptr = alloc(buf->b_ml.ml_line_len);
617
618 // need to allocate the line to be able to change it
619 if (newptr == NULL)
620 return;
621 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
622 buf->b_ml.ml_line_len);
623 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100624 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
625
626 cur_prop = buf->b_ml.ml_line_ptr + len
627 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100628 }
629
630 taillen = buf->b_ml.ml_line_len - len
631 - (idx + 1) * sizeof(textprop_T);
632 if (taillen > 0)
633 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
634 taillen);
635 buf->b_ml.ml_line_len -= sizeof(textprop_T);
636 --idx;
637
638 ++rettv->vval.v_number;
639 if (!do_all)
640 break;
641 }
642 }
643 }
644 }
645 redraw_buf_later(buf, NOT_VALID);
646}
647
648/*
649 * Common for f_prop_type_add() and f_prop_type_change().
650 */
651 void
652prop_type_set(typval_T *argvars, int add)
653{
654 char_u *name;
655 buf_T *buf = NULL;
656 dict_T *dict;
657 dictitem_T *di;
658 proptype_T *prop;
659
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100660 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100661 if (*name == NUL)
662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100663 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100664 return;
665 }
666
667 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
668 return;
669 dict = argvars[1].vval.v_dict;
670
671 prop = find_prop(name, buf);
672 if (add)
673 {
674 hashtab_T **htp;
675
676 if (prop != NULL)
677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100678 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100679 return;
680 }
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200681 prop = (proptype_T *)alloc_clear(sizeof(proptype_T) + STRLEN(name));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100682 if (prop == NULL)
683 return;
684 STRCPY(prop->pt_name, name);
685 prop->pt_id = ++proptype_id;
686 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
687 if (*htp == NULL)
688 {
689 *htp = (hashtab_T *)alloc(sizeof(hashtab_T));
690 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100691 {
692 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100693 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100694 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100695 hash_init(*htp);
696 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100697 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100698 }
699 else
700 {
701 if (prop == NULL)
702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100703 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100704 return;
705 }
706 }
707
708 if (dict != NULL)
709 {
710 di = dict_find(dict, (char_u *)"highlight", -1);
711 if (di != NULL)
712 {
713 char_u *highlight;
714 int hl_id = 0;
715
Bram Moolenaar8f667172018-12-14 15:38:31 +0100716 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100717 if (highlight != NULL && *highlight != NUL)
718 hl_id = syn_name2id(highlight);
719 if (hl_id <= 0)
720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100721 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100722 highlight == NULL ? (char_u *)"" : highlight);
723 return;
724 }
725 prop->pt_hl_id = hl_id;
726 }
727
Bram Moolenaar58187f12019-05-05 16:33:47 +0200728 di = dict_find(dict, (char_u *)"combine", -1);
729 if (di != NULL)
730 {
731 if (tv_get_number(&di->di_tv))
732 prop->pt_flags |= PT_FLAG_COMBINE;
733 else
734 prop->pt_flags &= ~PT_FLAG_COMBINE;
735 }
736
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100737 di = dict_find(dict, (char_u *)"priority", -1);
738 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100739 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100740
741 di = dict_find(dict, (char_u *)"start_incl", -1);
742 if (di != NULL)
743 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100744 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100745 prop->pt_flags |= PT_FLAG_INS_START_INCL;
746 else
747 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
748 }
749
750 di = dict_find(dict, (char_u *)"end_incl", -1);
751 if (di != NULL)
752 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100753 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100754 prop->pt_flags |= PT_FLAG_INS_END_INCL;
755 else
756 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
757 }
758 }
759}
760
761/*
762 * prop_type_add({name}, {props})
763 */
764 void
765f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
766{
767 prop_type_set(argvars, TRUE);
768}
769
770/*
771 * prop_type_change({name}, {props})
772 */
773 void
774f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
775{
776 prop_type_set(argvars, FALSE);
777}
778
779/*
780 * prop_type_delete({name} [, {bufnr}])
781 */
782 void
783f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
784{
785 char_u *name;
786 buf_T *buf = NULL;
787 hashitem_T *hi;
788
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100789 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100790 if (*name == NUL)
791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100792 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100793 return;
794 }
795
796 if (argvars[1].v_type != VAR_UNKNOWN)
797 {
798 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
799 return;
800 }
801
802 hi = find_prop_hi(name, buf);
803 if (hi != NULL)
804 {
805 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100806 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100807
808 if (buf == NULL)
809 ht = global_proptypes;
810 else
811 ht = buf->b_proptypes;
812 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100813 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100814 }
815}
816
817/*
818 * prop_type_get({name} [, {bufnr}])
819 */
820 void
821f_prop_type_get(typval_T *argvars, typval_T *rettv UNUSED)
822{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100823 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100824
825 if (*name == NUL)
826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100827 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100828 return;
829 }
830 if (rettv_dict_alloc(rettv) == OK)
831 {
832 proptype_T *prop = NULL;
833 buf_T *buf = NULL;
834
835 if (argvars[1].v_type != VAR_UNKNOWN)
836 {
837 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
838 return;
839 }
840
841 prop = find_prop(name, buf);
842 if (prop != NULL)
843 {
844 dict_T *d = rettv->vval.v_dict;
845
846 if (prop->pt_hl_id > 0)
847 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
848 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +0200849 dict_add_number(d, "combine",
850 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100851 dict_add_number(d, "start_incl",
852 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
853 dict_add_number(d, "end_incl",
854 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
855 if (buf != NULL)
856 dict_add_number(d, "bufnr", buf->b_fnum);
857 }
858 }
859}
860
861 static void
862list_types(hashtab_T *ht, list_T *l)
863{
864 long todo;
865 hashitem_T *hi;
866
867 todo = (long)ht->ht_used;
868 for (hi = ht->ht_array; todo > 0; ++hi)
869 {
870 if (!HASHITEM_EMPTY(hi))
871 {
872 proptype_T *prop = HI2PT(hi);
873
874 list_append_string(l, prop->pt_name, -1);
875 --todo;
876 }
877 }
878}
879
880/*
881 * prop_type_list([{bufnr}])
882 */
883 void
884f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
885{
886 buf_T *buf = NULL;
887
888 if (rettv_list_alloc(rettv) == OK)
889 {
890 if (argvars[0].v_type != VAR_UNKNOWN)
891 {
892 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
893 return;
894 }
895 if (buf == NULL)
896 {
897 if (global_proptypes != NULL)
898 list_types(global_proptypes, rettv->vval.v_list);
899 }
900 else if (buf->b_proptypes != NULL)
901 list_types(buf->b_proptypes, rettv->vval.v_list);
902 }
903}
904
905/*
906 * Free all property types in "ht".
907 */
908 static void
909clear_ht_prop_types(hashtab_T *ht)
910{
911 long todo;
912 hashitem_T *hi;
913
914 if (ht == NULL)
915 return;
916
917 todo = (long)ht->ht_used;
918 for (hi = ht->ht_array; todo > 0; ++hi)
919 {
920 if (!HASHITEM_EMPTY(hi))
921 {
922 proptype_T *prop = HI2PT(hi);
923
924 vim_free(prop);
925 --todo;
926 }
927 }
928
929 hash_clear(ht);
930 vim_free(ht);
931}
932
933#if defined(EXITFREE) || defined(PROTO)
934/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100935 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100936 */
937 void
938clear_global_prop_types(void)
939{
940 clear_ht_prop_types(global_proptypes);
941 global_proptypes = NULL;
942}
943#endif
944
945/*
946 * Free all property types for "buf".
947 */
948 void
949clear_buf_prop_types(buf_T *buf)
950{
951 clear_ht_prop_types(buf->b_proptypes);
952 buf->b_proptypes = NULL;
953}
954
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100955/*
956 * Adjust the columns of text properties in line "lnum" after position "col" to
957 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100958 * Note that "col" is zero-based, while tp_col is one-based.
959 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200960 * "flags" can have:
961 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
962 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +0200963 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200964 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100965 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200966 int
Bram Moolenaar196d1572019-01-02 23:47:18 +0100967adjust_prop_columns(
968 linenr_T lnum,
969 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200970 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200971 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100972{
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100973 int proplen;
974 char_u *props;
975 textprop_T tmp_prop;
976 proptype_T *pt;
977 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +0100978 int ri, wi;
979 size_t textlen;
980
981 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200982 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100983
984 proplen = get_text_props(curbuf, lnum, &props, TRUE);
985 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200986 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +0100987 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100988
Bram Moolenaar196d1572019-01-02 23:47:18 +0100989 wi = 0; // write index
990 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100991 {
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200992 int start_incl;
993
Bram Moolenaar196d1572019-01-02 23:47:18 +0100994 mch_memmove(&tmp_prop, props + ri * sizeof(textprop_T),
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100995 sizeof(textprop_T));
996 pt = text_prop_type_by_id(curbuf, tmp_prop.tp_type);
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200997 start_incl = (flags & APC_SUBSTITUTE) ||
998 (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100999
Bram Moolenaar196d1572019-01-02 23:47:18 +01001000 if (bytes_added > 0
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001001 && (tmp_prop.tp_col >= col + (start_incl ? 2 : 1)))
1002 {
1003 if (tmp_prop.tp_col < col + (start_incl ? 2 : 1))
1004 {
1005 tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
1006 tmp_prop.tp_col = col + 1;
1007 }
1008 else
1009 tmp_prop.tp_col += bytes_added;
1010 // Save for undo if requested and not done yet.
1011 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
1012 u_savesub(lnum);
1013 dirty = TRUE;
1014 }
1015 else if (bytes_added <= 0 && (tmp_prop.tp_col > col + 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001016 {
Bram Moolenaar8055d172019-05-17 22:57:26 +02001017 if (tmp_prop.tp_col + bytes_added < col + 1)
1018 {
1019 tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
1020 tmp_prop.tp_col = col + 1;
1021 }
1022 else
1023 tmp_prop.tp_col += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001024 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001025 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001026 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001027 dirty = TRUE;
Bram Moolenaar8055d172019-05-17 22:57:26 +02001028 if (tmp_prop.tp_len <= 0)
1029 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001030 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001031 else if (tmp_prop.tp_len > 0
1032 && tmp_prop.tp_col + tmp_prop.tp_len > col
Bram Moolenaar4614f532019-01-06 12:54:55 +01001033 + ((pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
Bram Moolenaar196d1572019-01-02 23:47:18 +01001034 ? 0 : 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001035 {
Bram Moolenaar8055d172019-05-17 22:57:26 +02001036 int after = col - bytes_added
1037 - (tmp_prop.tp_col - 1 + tmp_prop.tp_len);
1038 if (after > 0)
1039 tmp_prop.tp_len += bytes_added + after;
1040 else
1041 tmp_prop.tp_len += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001042 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001043 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001044 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001045 dirty = TRUE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001046 if (tmp_prop.tp_len <= 0)
1047 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001048 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001049 mch_memmove(props + wi * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001050 sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001051 ++wi;
1052 }
1053 if (dirty)
1054 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001055 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1056
1057 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1058 curbuf->b_ml.ml_line_ptr =
1059 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001060 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001061 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001062 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001063 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001064}
1065
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001066/*
1067 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001068 * "lnum_props" is the line that has the properties from before the split.
1069 * "lnum_top" is the top line.
1070 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001071 * "deleted" is the number of bytes deleted.
1072 */
1073 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001074adjust_props_for_split(
1075 linenr_T lnum_props,
1076 linenr_T lnum_top,
1077 int kept,
1078 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001079{
1080 char_u *props;
1081 int count;
1082 garray_T prevprop;
1083 garray_T nextprop;
1084 int i;
1085 int skipped = kept + deleted;
1086
1087 if (!curbuf->b_has_textprop)
1088 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001089
1090 // Get the text properties from "lnum_props".
1091 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001092 ga_init2(&prevprop, sizeof(textprop_T), 10);
1093 ga_init2(&nextprop, sizeof(textprop_T), 10);
1094
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001095 // Keep the relevant ones in the first line, reducing the length if needed.
1096 // Copy the ones that include the split to the second line.
1097 // Move the ones after the split to the second line.
1098 for (i = 0; i < count; ++i)
1099 {
1100 textprop_T prop;
1101 textprop_T *p;
1102
1103 // copy the prop to an aligned structure
1104 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1105
1106 if (prop.tp_col < kept && ga_grow(&prevprop, 1) == OK)
1107 {
1108 p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1109 *p = prop;
1110 if (p->tp_col + p->tp_len >= kept)
1111 p->tp_len = kept - p->tp_col;
1112 ++prevprop.ga_len;
1113 }
1114
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001115 // Only add the property to the next line if the length is bigger than
1116 // zero.
1117 if (prop.tp_col + prop.tp_len > skipped && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001118 {
1119 p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
1120 *p = prop;
1121 if (p->tp_col > skipped)
1122 p->tp_col -= skipped - 1;
1123 else
1124 {
1125 p->tp_len -= skipped - p->tp_col;
1126 p->tp_col = 1;
1127 }
1128 ++nextprop.ga_len;
1129 }
1130 }
1131
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001132 set_text_props(lnum_top, prevprop.ga_data,
1133 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001134 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001135 set_text_props(lnum_top + 1, nextprop.ga_data,
1136 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001137 ga_clear(&nextprop);
1138}
1139
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001140/*
1141 * Line "lnum" has been joined and will end up at column "col" in the new line.
1142 * "removed" bytes have been removed from the start of the line, properties
1143 * there are to be discarded.
1144 * Move the adjusted text properties to an allocated string, store it in
1145 * "prop_line" and adjust the columns.
1146 */
1147 void
1148adjust_props_for_join(
1149 linenr_T lnum,
1150 textprop_T **prop_line,
1151 int *prop_length,
1152 long col,
1153 int removed)
1154{
1155 int proplen;
1156 char_u *props;
1157 int ri;
1158 int wi = 0;
1159
1160 proplen = get_text_props(curbuf, lnum, &props, FALSE);
1161 if (proplen > 0)
1162 {
1163 *prop_line = (textprop_T *)alloc(proplen * (int)sizeof(textprop_T));
1164 if (*prop_line != NULL)
1165 {
1166 for (ri = 0; ri < proplen; ++ri)
1167 {
1168 textprop_T *cp = *prop_line + wi;
1169
1170 mch_memmove(cp, props + ri * sizeof(textprop_T),
1171 sizeof(textprop_T));
1172 if (cp->tp_col + cp->tp_len > removed)
1173 {
1174 if (cp->tp_col > removed)
1175 cp->tp_col += col;
1176 else
1177 {
1178 // property was partly deleted, make it shorter
1179 cp->tp_len -= removed - cp->tp_col;
1180 cp->tp_col = col;
1181 }
1182 ++wi;
1183 }
1184 }
1185 }
1186 *prop_length = wi;
1187 }
1188}
1189
1190/*
1191 * After joining lines: concatenate the text and the properties of all joined
1192 * lines into one line and replace the line.
1193 */
1194 void
1195join_prop_lines(
1196 linenr_T lnum,
1197 char_u *newp,
1198 textprop_T **prop_lines,
1199 int *prop_lengths,
1200 int count)
1201{
1202 size_t proplen = 0;
1203 size_t oldproplen;
1204 char_u *props;
1205 int i;
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001206 size_t len;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001207 char_u *line;
1208 size_t l;
1209
1210 for (i = 0; i < count - 1; ++i)
1211 proplen += prop_lengths[i];
1212 if (proplen == 0)
1213 {
1214 ml_replace(lnum, newp, FALSE);
1215 return;
1216 }
1217
1218 // get existing properties of the joined line
1219 oldproplen = get_text_props(curbuf, lnum, &props, FALSE);
1220
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001221 len = STRLEN(newp) + 1;
1222 line = alloc((int)(len + (oldproplen + proplen) * sizeof(textprop_T)));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001223 if (line == NULL)
1224 return;
1225 mch_memmove(line, newp, len);
1226 l = oldproplen * sizeof(textprop_T);
1227 mch_memmove(line + len, props, l);
1228 len += l;
1229
1230 for (i = 0; i < count - 1; ++i)
1231 if (prop_lines[i] != NULL)
1232 {
1233 l = prop_lengths[i] * sizeof(textprop_T);
1234 mch_memmove(line + len, prop_lines[i], l);
1235 len += l;
1236 vim_free(prop_lines[i]);
1237 }
1238
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001239 ml_replace_len(lnum, line, (colnr_T)len, TRUE, FALSE);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001240 vim_free(newp);
1241 vim_free(prop_lines);
1242 vim_free(prop_lengths);
1243}
1244
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001245#endif // FEAT_TEXT_PROP