blob: 674faebe248bc7ec1bd6ec7e837be55455263cdc [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/*
11 * Text properties implementation.
12 *
13 * Text properties are attached to the text. They move with the text when
14 * text is inserted/deleted.
15 *
16 * Text properties have a user specified ID number, which can be unique.
17 * Text properties have a type, which can be used to specify highlighting.
18 *
19 * TODO:
Bram Moolenaarc6663882019-02-22 19:14:54 +010020 * - When using 'cursorline' attributes should be merged. (#3912)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010021 * - Adjust text property column and length when text is inserted/deleted.
Bram Moolenaar4164bb22019-01-04 23:09:49 +010022 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010023 * -> search for changed_bytes() from misc1.c
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010024 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaarb56ac042018-12-28 23:22:40 +010025 * - Add an arrray for global_proptypes, to quickly lookup a prop type by ID
26 * - Add an arrray for b_proptypes, to quickly lookup a prop type by ID
27 * - Checking the text length to detect text properties is slow. Use a flag in
28 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010029 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
30 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010031 * - Add mechanism to keep track of changed lines, so that plugin can update
32 * text properties in these.
33 * - Perhaps have a window-local option to disable highlighting from text
34 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010035 */
36
37#include "vim.h"
38
39#if defined(FEAT_TEXT_PROP) || defined(PROTO)
40
41/*
42 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
43 * This avoids adding a pointer to the hashtable item.
44 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
45 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
46 * HI2PT() converts a hashitem pointer to a proptype pointer.
47 */
48#define PT2HIKEY(p) ((p)->pt_name)
49#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
50#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
51
52// The global text property types.
53static hashtab_T *global_proptypes = NULL;
54
55// The last used text property type ID.
56static int proptype_id = 0;
57
58static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
59static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld");
Bram Moolenaare3d31b02018-12-24 23:07:04 +010060static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010061
62/*
63 * Find a property type by name, return the hashitem.
64 * Returns NULL if the item can't be found.
65 */
66 static hashitem_T *
67find_prop_hi(char_u *name, buf_T *buf)
68{
69 hashtab_T *ht;
70 hashitem_T *hi;
71
72 if (*name == NUL)
73 return NULL;
74 if (buf == NULL)
75 ht = global_proptypes;
76 else
77 ht = buf->b_proptypes;
78
79 if (ht == NULL)
80 return NULL;
81 hi = hash_find(ht, name);
82 if (HASHITEM_EMPTY(hi))
83 return NULL;
84 return hi;
85}
86
87/*
88 * Like find_prop_hi() but return the property type.
89 */
90 static proptype_T *
91find_prop(char_u *name, buf_T *buf)
92{
93 hashitem_T *hi = find_prop_hi(name, buf);
94
95 if (hi == NULL)
96 return NULL;
97 return HI2PT(hi);
98}
99
100/*
101 * Lookup a property type by name. First in "buf" and when not found in the
102 * global types.
103 * When not found gives an error message and returns NULL.
104 */
105 static proptype_T *
106lookup_prop_type(char_u *name, buf_T *buf)
107{
108 proptype_T *type = find_prop(name, buf);
109
110 if (type == NULL)
111 type = find_prop(name, NULL);
112 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100113 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100114 return type;
115}
116
117/*
118 * Get an optional "bufnr" item from the dict in "arg".
119 * When the argument is not used or "bufnr" is not present then "buf" is
120 * unchanged.
121 * If "bufnr" is valid or not present return OK.
122 * When "arg" is not a dict or "bufnr" is invalide return FAIL.
123 */
124 static int
125get_bufnr_from_arg(typval_T *arg, buf_T **buf)
126{
127 dictitem_T *di;
128
129 if (arg->v_type != VAR_DICT)
130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100131 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100132 return FAIL;
133 }
134 if (arg->vval.v_dict == NULL)
135 return OK; // NULL dict is like an empty dict
136 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
137 if (di != NULL)
138 {
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +0100139 *buf = tv_get_buf(&di->di_tv, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140 if (*buf == NULL)
141 return FAIL;
142 }
143 return OK;
144}
145
146/*
147 * prop_add({lnum}, {col}, {props})
148 */
149 void
150f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
151{
152 linenr_T lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100153 linenr_T start_lnum;
154 linenr_T end_lnum;
155 colnr_T start_col;
156 colnr_T end_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100157 dict_T *dict;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100158 char_u *type_name;
159 proptype_T *type;
160 buf_T *buf = curbuf;
161 int id = 0;
162 char_u *newtext;
163 int proplen;
164 size_t textlen;
Bram Moolenaarc6663882019-02-22 19:14:54 +0100165 char_u *props = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100166 char_u *newprops;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100167 textprop_T tmp_prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100168 int i;
169
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100170 start_lnum = tv_get_number(&argvars[0]);
171 start_col = tv_get_number(&argvars[1]);
172 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100174 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100175 return;
176 }
177 if (argvars[2].v_type != VAR_DICT)
178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100179 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100180 return;
181 }
182 dict = argvars[2].vval.v_dict;
183
184 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100186 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100187 return;
188 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100189 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100190
191 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
192 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100193 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
194 if (end_lnum < start_lnum)
195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100196 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100197 return;
198 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100199 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100200 else
201 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100202
203 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100204 {
205 long length = dict_get_number(dict, (char_u *)"length");
206
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100207 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100209 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100210 return;
211 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100212 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100213 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100214 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
215 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100216 end_col = dict_get_number(dict, (char_u *)"end_col");
217 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100219 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100220 return;
221 }
222 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100223 else if (start_lnum == end_lnum)
224 end_col = start_col;
225 else
226 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100227
228 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100229 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100230
231 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
232 return;
233
234 type = lookup_prop_type(type_name, buf);
235 if (type == NULL)
236 return;
237
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100238 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100240 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100241 return;
242 }
243 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100245 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100246 return;
247 }
248
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100249 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100250 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100251 colnr_T col; // start column
252 long length; // in bytes
253
254 // Fetch the line to get the ml_line_len field updated.
255 proplen = get_text_props(buf, lnum, &props, TRUE);
256 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
257
258 if (lnum == start_lnum)
259 col = start_col;
260 else
261 col = 1;
262 if (col - 1 > (colnr_T)textlen)
263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100264 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100265 return;
266 }
267
268 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100269 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100270 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100271 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100272 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100273 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100274 if (length < 0)
275 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100276
277 // Allocate the new line with space for the new proprety.
278 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
279 if (newtext == NULL)
280 return;
281 // Copy the text, including terminating NUL.
282 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
283
284 // Find the index where to insert the new property.
285 // Since the text properties are not aligned properly when stored with the
286 // text, we need to copy them as bytes before using it as a struct.
287 for (i = 0; i < proplen; ++i)
288 {
289 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
290 sizeof(textprop_T));
291 if (tmp_prop.tp_col >= col)
292 break;
293 }
294 newprops = newtext + textlen;
295 if (i > 0)
296 mch_memmove(newprops, props, sizeof(textprop_T) * i);
297
298 tmp_prop.tp_col = col;
299 tmp_prop.tp_len = length;
300 tmp_prop.tp_id = id;
301 tmp_prop.tp_type = type->pt_id;
302 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
303 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
304 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
305 sizeof(textprop_T));
306
307 if (i < proplen)
308 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
309 props + i * sizeof(textprop_T),
310 sizeof(textprop_T) * (proplen - i));
311
312 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
313 vim_free(buf->b_ml.ml_line_ptr);
314 buf->b_ml.ml_line_ptr = newtext;
315 buf->b_ml.ml_line_len += sizeof(textprop_T);
316 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100317 }
318
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100319 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100320 redraw_buf_later(buf, NOT_VALID);
321}
322
323/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100324 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100325 * Returns the number of text properties and, when non-zero, a pointer to the
326 * first one in "props" (note that it is not aligned, therefore the char_u
327 * pointer).
328 */
329 int
330get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
331{
332 char_u *text;
333 size_t textlen;
334 size_t proplen;
335
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100336 // Be quick when no text property types have been defined or the buffer,
337 // unless we are adding one.
338 if (!buf->b_has_textprop && !will_change)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100339 return 0;
340
341 // Fetch the line to get the ml_line_len field updated.
342 text = ml_get_buf(buf, lnum, will_change);
343 textlen = STRLEN(text) + 1;
344 proplen = buf->b_ml.ml_line_len - textlen;
345 if (proplen % sizeof(textprop_T) != 0)
346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100347 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100348 return 0;
349 }
350 if (proplen > 0)
351 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100352 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100353}
354
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100355/*
356 * Set the text properties for line "lnum" to "props" with length "len".
357 * If "len" is zero text properties are removed, "props" is not used.
358 * Any existing text properties are dropped.
359 * Only works for the current buffer.
360 */
361 static void
362set_text_props(linenr_T lnum, char_u *props, int len)
363{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100364 char_u *text;
365 char_u *newtext;
366 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100367
368 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100369 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100370 newtext = alloc(textlen + len);
371 if (newtext == NULL)
372 return;
373 mch_memmove(newtext, text, textlen);
374 if (len > 0)
375 mch_memmove(newtext + textlen, props, len);
376 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
377 vim_free(curbuf->b_ml.ml_line_ptr);
378 curbuf->b_ml.ml_line_ptr = newtext;
379 curbuf->b_ml.ml_line_len = textlen + len;
380 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
381}
382
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100383 static proptype_T *
384find_type_by_id(hashtab_T *ht, int id)
385{
386 long todo;
387 hashitem_T *hi;
388
389 if (ht == NULL)
390 return NULL;
391
392 // TODO: Make this faster by keeping a list of types sorted on ID and use
393 // a binary search.
394
395 todo = (long)ht->ht_used;
396 for (hi = ht->ht_array; todo > 0; ++hi)
397 {
398 if (!HASHITEM_EMPTY(hi))
399 {
400 proptype_T *prop = HI2PT(hi);
401
402 if (prop->pt_id == id)
403 return prop;
404 --todo;
405 }
406 }
407 return NULL;
408}
409
410/*
411 * Find a property type by ID in "buf" or globally.
412 * Returns NULL if not found.
413 */
414 proptype_T *
415text_prop_type_by_id(buf_T *buf, int id)
416{
417 proptype_T *type;
418
419 type = find_type_by_id(buf->b_proptypes, id);
420 if (type == NULL)
421 type = find_type_by_id(global_proptypes, id);
422 return type;
423}
424
425/*
426 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
427 */
428 void
429f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
430{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100431 linenr_T start = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100432 linenr_T end = start;
433 linenr_T lnum;
434 buf_T *buf = curbuf;
435
436 if (argvars[1].v_type != VAR_UNKNOWN)
437 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100438 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100439 if (argvars[2].v_type != VAR_UNKNOWN)
440 {
441 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
442 return;
443 }
444 }
445 if (start < 1 || end < 1)
446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100447 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100448 return;
449 }
450
451 for (lnum = start; lnum <= end; ++lnum)
452 {
453 char_u *text;
454 size_t len;
455
456 if (lnum > buf->b_ml.ml_line_count)
457 break;
458 text = ml_get_buf(buf, lnum, FALSE);
459 len = STRLEN(text) + 1;
460 if ((size_t)buf->b_ml.ml_line_len > len)
461 {
462 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
463 {
464 char_u *newtext = vim_strsave(text);
465
466 // need to allocate the line now
467 if (newtext == NULL)
468 return;
469 buf->b_ml.ml_line_ptr = newtext;
470 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
471 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100472 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473 }
474 }
475 redraw_buf_later(buf, NOT_VALID);
476}
477
478/*
479 * prop_list({lnum} [, {bufnr}])
480 */
481 void
482f_prop_list(typval_T *argvars, typval_T *rettv)
483{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100484 linenr_T lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100485 buf_T *buf = curbuf;
486
487 if (argvars[1].v_type != VAR_UNKNOWN)
488 {
489 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
490 return;
491 }
492 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100494 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100495 return;
496 }
497
498 if (rettv_list_alloc(rettv) == OK)
499 {
500 char_u *text = ml_get_buf(buf, lnum, FALSE);
501 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100502 int count = (int)((buf->b_ml.ml_line_len - textlen)
503 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100504 int i;
505 textprop_T prop;
506 proptype_T *pt;
507
508 for (i = 0; i < count; ++i)
509 {
510 dict_T *d = dict_alloc();
511
512 if (d == NULL)
513 break;
514 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
515 sizeof(textprop_T));
516 dict_add_number(d, "col", prop.tp_col);
517 dict_add_number(d, "length", prop.tp_len);
518 dict_add_number(d, "id", prop.tp_id);
519 dict_add_number(d, "start", !(prop.tp_flags & TP_FLAG_CONT_PREV));
520 dict_add_number(d, "end", !(prop.tp_flags & TP_FLAG_CONT_NEXT));
521 pt = text_prop_type_by_id(buf, prop.tp_type);
522 if (pt != NULL)
523 dict_add_string(d, "type", pt->pt_name);
524
525 list_append_dict(rettv->vval.v_list, d);
526 }
527 }
528}
529
530/*
531 * prop_remove({props} [, {lnum} [, {lnum_end}]])
532 */
533 void
534f_prop_remove(typval_T *argvars, typval_T *rettv)
535{
536 linenr_T start = 1;
537 linenr_T end = 0;
538 linenr_T lnum;
539 dict_T *dict;
540 buf_T *buf = curbuf;
541 dictitem_T *di;
542 int do_all = FALSE;
543 int id = -1;
544 int type_id = -1;
545
546 rettv->vval.v_number = 0;
547 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100549 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100550 return;
551 }
552
553 if (argvars[1].v_type != VAR_UNKNOWN)
554 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100555 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100556 end = start;
557 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100558 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100559 if (start < 1 || end < 1)
560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100561 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100562 return;
563 }
564 }
565
566 dict = argvars[0].vval.v_dict;
567 di = dict_find(dict, (char_u *)"bufnr", -1);
568 if (di != NULL)
569 {
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +0100570 buf = tv_get_buf(&di->di_tv, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100571 if (buf == NULL)
572 return;
573 }
574
575 di = dict_find(dict, (char_u*)"all", -1);
576 if (di != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100577 do_all = dict_get_number(dict, (char_u *)"all");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100578
579 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100580 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100581 if (dict_find(dict, (char_u *)"type", -1))
582 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100583 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100584 proptype_T *type = lookup_prop_type(name, buf);
585
586 if (type == NULL)
587 return;
588 type_id = type->pt_id;
589 }
590 if (id == -1 && type_id == -1)
591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100592 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100593 return;
594 }
595
596 if (end == 0)
597 end = buf->b_ml.ml_line_count;
598 for (lnum = start; lnum <= end; ++lnum)
599 {
600 char_u *text;
601 size_t len;
602
603 if (lnum > buf->b_ml.ml_line_count)
604 break;
605 text = ml_get_buf(buf, lnum, FALSE);
606 len = STRLEN(text) + 1;
607 if ((size_t)buf->b_ml.ml_line_len > len)
608 {
609 static textprop_T textprop; // static because of alignment
610 unsigned idx;
611
612 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
613 / sizeof(textprop_T); ++idx)
614 {
615 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
616 + idx * sizeof(textprop_T);
617 size_t taillen;
618
619 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
620 if (textprop.tp_id == id || textprop.tp_type == type_id)
621 {
622 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
623 {
624 char_u *newptr = alloc(buf->b_ml.ml_line_len);
625
626 // need to allocate the line to be able to change it
627 if (newptr == NULL)
628 return;
629 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
630 buf->b_ml.ml_line_len);
631 buf->b_ml.ml_line_ptr = newptr;
632 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
633 }
634
635 taillen = buf->b_ml.ml_line_len - len
636 - (idx + 1) * sizeof(textprop_T);
637 if (taillen > 0)
638 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
639 taillen);
640 buf->b_ml.ml_line_len -= sizeof(textprop_T);
641 --idx;
642
643 ++rettv->vval.v_number;
644 if (!do_all)
645 break;
646 }
647 }
648 }
649 }
650 redraw_buf_later(buf, NOT_VALID);
651}
652
653/*
654 * Common for f_prop_type_add() and f_prop_type_change().
655 */
656 void
657prop_type_set(typval_T *argvars, int add)
658{
659 char_u *name;
660 buf_T *buf = NULL;
661 dict_T *dict;
662 dictitem_T *di;
663 proptype_T *prop;
664
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100665 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100666 if (*name == NUL)
667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100668 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100669 return;
670 }
671
672 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
673 return;
674 dict = argvars[1].vval.v_dict;
675
676 prop = find_prop(name, buf);
677 if (add)
678 {
679 hashtab_T **htp;
680
681 if (prop != NULL)
682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100683 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100684 return;
685 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100686 prop = (proptype_T *)alloc_clear((int)(sizeof(proptype_T) + STRLEN(name)));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100687 if (prop == NULL)
688 return;
689 STRCPY(prop->pt_name, name);
690 prop->pt_id = ++proptype_id;
691 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
692 if (*htp == NULL)
693 {
694 *htp = (hashtab_T *)alloc(sizeof(hashtab_T));
695 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100696 {
697 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100698 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100699 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100700 hash_init(*htp);
701 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100702 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100703 }
704 else
705 {
706 if (prop == NULL)
707 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100708 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100709 return;
710 }
711 }
712
713 if (dict != NULL)
714 {
715 di = dict_find(dict, (char_u *)"highlight", -1);
716 if (di != NULL)
717 {
718 char_u *highlight;
719 int hl_id = 0;
720
Bram Moolenaar8f667172018-12-14 15:38:31 +0100721 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100722 if (highlight != NULL && *highlight != NUL)
723 hl_id = syn_name2id(highlight);
724 if (hl_id <= 0)
725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100726 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100727 highlight == NULL ? (char_u *)"" : highlight);
728 return;
729 }
730 prop->pt_hl_id = hl_id;
731 }
732
733 di = dict_find(dict, (char_u *)"priority", -1);
734 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100735 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100736
737 di = dict_find(dict, (char_u *)"start_incl", -1);
738 if (di != NULL)
739 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100740 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100741 prop->pt_flags |= PT_FLAG_INS_START_INCL;
742 else
743 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
744 }
745
746 di = dict_find(dict, (char_u *)"end_incl", -1);
747 if (di != NULL)
748 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100749 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100750 prop->pt_flags |= PT_FLAG_INS_END_INCL;
751 else
752 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
753 }
754 }
755}
756
757/*
758 * prop_type_add({name}, {props})
759 */
760 void
761f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
762{
763 prop_type_set(argvars, TRUE);
764}
765
766/*
767 * prop_type_change({name}, {props})
768 */
769 void
770f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
771{
772 prop_type_set(argvars, FALSE);
773}
774
775/*
776 * prop_type_delete({name} [, {bufnr}])
777 */
778 void
779f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
780{
781 char_u *name;
782 buf_T *buf = NULL;
783 hashitem_T *hi;
784
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100785 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100786 if (*name == NUL)
787 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100788 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100789 return;
790 }
791
792 if (argvars[1].v_type != VAR_UNKNOWN)
793 {
794 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
795 return;
796 }
797
798 hi = find_prop_hi(name, buf);
799 if (hi != NULL)
800 {
801 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100802 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100803
804 if (buf == NULL)
805 ht = global_proptypes;
806 else
807 ht = buf->b_proptypes;
808 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100809 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100810 }
811}
812
813/*
814 * prop_type_get({name} [, {bufnr}])
815 */
816 void
817f_prop_type_get(typval_T *argvars, typval_T *rettv UNUSED)
818{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100819 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100820
821 if (*name == NUL)
822 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100823 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100824 return;
825 }
826 if (rettv_dict_alloc(rettv) == OK)
827 {
828 proptype_T *prop = NULL;
829 buf_T *buf = NULL;
830
831 if (argvars[1].v_type != VAR_UNKNOWN)
832 {
833 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
834 return;
835 }
836
837 prop = find_prop(name, buf);
838 if (prop != NULL)
839 {
840 dict_T *d = rettv->vval.v_dict;
841
842 if (prop->pt_hl_id > 0)
843 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
844 dict_add_number(d, "priority", prop->pt_priority);
845 dict_add_number(d, "start_incl",
846 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
847 dict_add_number(d, "end_incl",
848 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
849 if (buf != NULL)
850 dict_add_number(d, "bufnr", buf->b_fnum);
851 }
852 }
853}
854
855 static void
856list_types(hashtab_T *ht, list_T *l)
857{
858 long todo;
859 hashitem_T *hi;
860
861 todo = (long)ht->ht_used;
862 for (hi = ht->ht_array; todo > 0; ++hi)
863 {
864 if (!HASHITEM_EMPTY(hi))
865 {
866 proptype_T *prop = HI2PT(hi);
867
868 list_append_string(l, prop->pt_name, -1);
869 --todo;
870 }
871 }
872}
873
874/*
875 * prop_type_list([{bufnr}])
876 */
877 void
878f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
879{
880 buf_T *buf = NULL;
881
882 if (rettv_list_alloc(rettv) == OK)
883 {
884 if (argvars[0].v_type != VAR_UNKNOWN)
885 {
886 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
887 return;
888 }
889 if (buf == NULL)
890 {
891 if (global_proptypes != NULL)
892 list_types(global_proptypes, rettv->vval.v_list);
893 }
894 else if (buf->b_proptypes != NULL)
895 list_types(buf->b_proptypes, rettv->vval.v_list);
896 }
897}
898
899/*
900 * Free all property types in "ht".
901 */
902 static void
903clear_ht_prop_types(hashtab_T *ht)
904{
905 long todo;
906 hashitem_T *hi;
907
908 if (ht == NULL)
909 return;
910
911 todo = (long)ht->ht_used;
912 for (hi = ht->ht_array; todo > 0; ++hi)
913 {
914 if (!HASHITEM_EMPTY(hi))
915 {
916 proptype_T *prop = HI2PT(hi);
917
918 vim_free(prop);
919 --todo;
920 }
921 }
922
923 hash_clear(ht);
924 vim_free(ht);
925}
926
927#if defined(EXITFREE) || defined(PROTO)
928/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100929 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100930 */
931 void
932clear_global_prop_types(void)
933{
934 clear_ht_prop_types(global_proptypes);
935 global_proptypes = NULL;
936}
937#endif
938
939/*
940 * Free all property types for "buf".
941 */
942 void
943clear_buf_prop_types(buf_T *buf)
944{
945 clear_ht_prop_types(buf->b_proptypes);
946 buf->b_proptypes = NULL;
947}
948
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100949/*
950 * Adjust the columns of text properties in line "lnum" after position "col" to
951 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100952 * Note that "col" is zero-based, while tp_col is one-based.
953 * Only for the current buffer.
954 * Called is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100955 */
956 void
Bram Moolenaar196d1572019-01-02 23:47:18 +0100957adjust_prop_columns(
958 linenr_T lnum,
959 colnr_T col,
960 int bytes_added)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100961{
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100962 int proplen;
963 char_u *props;
964 textprop_T tmp_prop;
965 proptype_T *pt;
966 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +0100967 int ri, wi;
968 size_t textlen;
969
970 if (text_prop_frozen > 0)
971 return;
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100972
973 proplen = get_text_props(curbuf, lnum, &props, TRUE);
974 if (proplen == 0)
975 return;
Bram Moolenaar196d1572019-01-02 23:47:18 +0100976 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100977
Bram Moolenaar196d1572019-01-02 23:47:18 +0100978 wi = 0; // write index
979 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100980 {
Bram Moolenaar196d1572019-01-02 23:47:18 +0100981 mch_memmove(&tmp_prop, props + ri * sizeof(textprop_T),
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100982 sizeof(textprop_T));
983 pt = text_prop_type_by_id(curbuf, tmp_prop.tp_type);
984
Bram Moolenaar196d1572019-01-02 23:47:18 +0100985 if (bytes_added > 0
Bram Moolenaar4614f532019-01-06 12:54:55 +0100986 ? (tmp_prop.tp_col >= col
987 + (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL)
988 ? 2 : 1))
Bram Moolenaar196d1572019-01-02 23:47:18 +0100989 : (tmp_prop.tp_col > col + 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100990 {
991 tmp_prop.tp_col += bytes_added;
992 dirty = TRUE;
993 }
Bram Moolenaar196d1572019-01-02 23:47:18 +0100994 else if (tmp_prop.tp_len > 0
995 && tmp_prop.tp_col + tmp_prop.tp_len > col
Bram Moolenaar4614f532019-01-06 12:54:55 +0100996 + ((pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
Bram Moolenaar196d1572019-01-02 23:47:18 +0100997 ? 0 : 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100998 {
999 tmp_prop.tp_len += bytes_added;
1000 dirty = TRUE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001001 if (tmp_prop.tp_len <= 0)
1002 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001003 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001004 mch_memmove(props + wi * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001005 sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001006 ++wi;
1007 }
1008 if (dirty)
1009 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001010 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1011
1012 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1013 curbuf->b_ml.ml_line_ptr =
1014 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001015 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001016 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001017 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001018}
1019
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001020/*
1021 * Adjust text properties for a line that was split in two.
1022 * "lnum" is the newly inserted line. The text properties are now on the line
1023 * below it. "kept" is the number of bytes kept in the first line, while
1024 * "deleted" is the number of bytes deleted.
1025 */
1026 void
1027adjust_props_for_split(linenr_T lnum, int kept, int deleted)
1028{
1029 char_u *props;
1030 int count;
1031 garray_T prevprop;
1032 garray_T nextprop;
1033 int i;
1034 int skipped = kept + deleted;
1035
1036 if (!curbuf->b_has_textprop)
1037 return;
1038 count = get_text_props(curbuf, lnum + 1, &props, FALSE);
1039 ga_init2(&prevprop, sizeof(textprop_T), 10);
1040 ga_init2(&nextprop, sizeof(textprop_T), 10);
1041
1042 // Get the text properties, which are at "lnum + 1".
1043 // Keep the relevant ones in the first line, reducing the length if needed.
1044 // Copy the ones that include the split to the second line.
1045 // Move the ones after the split to the second line.
1046 for (i = 0; i < count; ++i)
1047 {
1048 textprop_T prop;
1049 textprop_T *p;
1050
1051 // copy the prop to an aligned structure
1052 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1053
1054 if (prop.tp_col < kept && ga_grow(&prevprop, 1) == OK)
1055 {
1056 p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1057 *p = prop;
1058 if (p->tp_col + p->tp_len >= kept)
1059 p->tp_len = kept - p->tp_col;
1060 ++prevprop.ga_len;
1061 }
1062
1063 if (prop.tp_col + prop.tp_len >= skipped && ga_grow(&nextprop, 1) == OK)
1064 {
1065 p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
1066 *p = prop;
1067 if (p->tp_col > skipped)
1068 p->tp_col -= skipped - 1;
1069 else
1070 {
1071 p->tp_len -= skipped - p->tp_col;
1072 p->tp_col = 1;
1073 }
1074 ++nextprop.ga_len;
1075 }
1076 }
1077
1078 set_text_props(lnum, prevprop.ga_data, prevprop.ga_len * sizeof(textprop_T));
1079 ga_clear(&prevprop);
1080
1081 set_text_props(lnum + 1, nextprop.ga_data, nextprop.ga_len * sizeof(textprop_T));
1082 ga_clear(&nextprop);
1083}
1084
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001085#endif // FEAT_TEXT_PROP