blob: 939b1640747c1429308957dcbacdb7419073aa37 [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 Moolenaar32aa1022019-11-02 22:54:41 +010019 * - Add an array for global_proptypes, to quickly lookup a prop type by ID
20 * - Add an array for b_proptypes, to quickly lookup a prop type by ID
Bram Moolenaarb56ac042018-12-28 23:22:40 +010021 * - 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
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010031#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
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
Bram Moolenaar98aefe72018-12-13 22:20:09 +010050/*
51 * Find a property type by name, return the hashitem.
52 * Returns NULL if the item can't be found.
53 */
54 static hashitem_T *
55find_prop_hi(char_u *name, buf_T *buf)
56{
57 hashtab_T *ht;
58 hashitem_T *hi;
59
60 if (*name == NUL)
61 return NULL;
62 if (buf == NULL)
63 ht = global_proptypes;
64 else
65 ht = buf->b_proptypes;
66
67 if (ht == NULL)
68 return NULL;
69 hi = hash_find(ht, name);
70 if (HASHITEM_EMPTY(hi))
71 return NULL;
72 return hi;
73}
74
75/*
76 * Like find_prop_hi() but return the property type.
77 */
78 static proptype_T *
79find_prop(char_u *name, buf_T *buf)
80{
81 hashitem_T *hi = find_prop_hi(name, buf);
82
83 if (hi == NULL)
84 return NULL;
85 return HI2PT(hi);
86}
87
88/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020089 * Get the prop type ID of "name".
90 * When not found return zero.
91 */
92 int
93find_prop_type_id(char_u *name, buf_T *buf)
94{
95 proptype_T *pt = find_prop(name, buf);
96
97 if (pt == NULL)
98 return 0;
99 return pt->pt_id;
100}
101
102/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100103 * Lookup a property type by name. First in "buf" and when not found in the
104 * global types.
105 * When not found gives an error message and returns NULL.
106 */
107 static proptype_T *
108lookup_prop_type(char_u *name, buf_T *buf)
109{
110 proptype_T *type = find_prop(name, buf);
111
112 if (type == NULL)
113 type = find_prop(name, NULL);
114 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100115 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100116 return type;
117}
118
119/*
120 * Get an optional "bufnr" item from the dict in "arg".
121 * When the argument is not used or "bufnr" is not present then "buf" is
122 * unchanged.
123 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100124 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100125 */
126 static int
127get_bufnr_from_arg(typval_T *arg, buf_T **buf)
128{
129 dictitem_T *di;
130
131 if (arg->v_type != VAR_DICT)
132 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000133 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100134 return FAIL;
135 }
136 if (arg->vval.v_dict == NULL)
137 return OK; // NULL dict is like an empty dict
138 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200139 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
140 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100141 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200142 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143 if (*buf == NULL)
144 return FAIL;
145 }
146 return OK;
147}
148
149/*
150 * prop_add({lnum}, {col}, {props})
151 */
152 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100153f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100154{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100155 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100156 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100157
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200158 if (in_vim9script()
159 && (check_for_number_arg(argvars, 0) == FAIL
160 || check_for_number_arg(argvars, 1) == FAIL
161 || check_for_dict_arg(argvars, 2) == FAIL))
162 return;
163
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100164 start_lnum = tv_get_number(&argvars[0]);
165 start_col = tv_get_number(&argvars[1]);
166 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100167 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000168 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100169 return;
170 }
171 if (argvars[2].v_type != VAR_DICT)
172 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000173 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100174 return;
175 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200176
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100177 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
178 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200179}
180
181/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200182 * Attach a text property 'type_name' to the text starting
183 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100184 * the buffer "buf" and assign identifier "id".
185 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200186 */
187 static int
188prop_add_one(
189 buf_T *buf,
190 char_u *type_name,
191 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 char_u *text_arg,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200193 linenr_T start_lnum,
194 linenr_T end_lnum,
195 colnr_T start_col,
196 colnr_T end_col)
197{
198 proptype_T *type;
199 linenr_T lnum;
200 int proplen;
201 char_u *props = NULL;
202 char_u *newprops;
203 size_t textlen;
204 char_u *newtext;
205 int i;
206 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100207 char_u *text = text_arg;
208 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200209
210 type = lookup_prop_type(type_name, buf);
211 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100212 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200213
214 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
215 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000216 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100217 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200218 }
219 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
220 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000221 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100222 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200223 }
224
225 if (buf->b_ml.ml_mfp == NULL)
226 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000227 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100228 goto theend;
229 }
230
231 if (text != NULL)
232 {
233 garray_T *gap = &buf->b_textprop_text;
234
235 // double check we got the right ID
236 if (-id - 1 != gap->ga_len)
237 iemsg("text prop ID mismatch");
238 if (gap->ga_growsize == 0)
239 ga_init2(gap, sizeof(char *), 50);
240 if (ga_grow(gap, 1) == FAIL)
241 goto theend;
242 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
243 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200244 }
245
246 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
247 {
248 colnr_T col; // start column
249 long length; // in bytes
250
251 // Fetch the line to get the ml_line_len field updated.
252 proplen = get_text_props(buf, lnum, &props, TRUE);
253 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
254
255 if (lnum == start_lnum)
256 col = start_col;
257 else
258 col = 1;
259 if (col - 1 > (colnr_T)textlen)
260 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000261 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100262 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200263 }
264
265 if (lnum == end_lnum)
266 length = end_col - col;
267 else
268 length = (int)textlen - col + 1;
269 if (length > (long)textlen)
270 length = (int)textlen; // can include the end-of-line
271 if (length < 0)
272 length = 0; // zero-width property
273
274 // Allocate the new line with space for the new property.
275 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
276 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100277 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200278 // Copy the text, including terminating NUL.
279 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
280
281 // Find the index where to insert the new property.
282 // Since the text properties are not aligned properly when stored with
283 // the text, we need to copy them as bytes before using it as a struct.
284 for (i = 0; i < proplen; ++i)
285 {
286 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
287 sizeof(textprop_T));
288 if (tmp_prop.tp_col >= col)
289 break;
290 }
291 newprops = newtext + textlen;
292 if (i > 0)
293 mch_memmove(newprops, props, sizeof(textprop_T) * i);
294
295 tmp_prop.tp_col = col;
296 tmp_prop.tp_len = length;
297 tmp_prop.tp_id = id;
298 tmp_prop.tp_type = type->pt_id;
299 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
300 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
301 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
302 sizeof(textprop_T));
303
304 if (i < proplen)
305 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
306 props + i * sizeof(textprop_T),
307 sizeof(textprop_T) * (proplen - i));
308
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100309 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200310 vim_free(buf->b_ml.ml_line_ptr);
311 buf->b_ml.ml_line_ptr = newtext;
312 buf->b_ml.ml_line_len += sizeof(textprop_T);
313 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
314 }
315
316 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100317 res = OK;
318
319theend:
320 vim_free(text);
321 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200322}
323
324/*
325 * prop_add_list()
326 * First argument specifies the text property:
327 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
328 * Second argument is a List where each item is a List with the following
329 * entries: [lnum, start_col, end_col]
330 */
331 void
332f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
333{
334 dict_T *dict;
335 char_u *type_name;
336 buf_T *buf = curbuf;
337 int id = 0;
338 listitem_T *li;
339 list_T *pos_list;
340 linenr_T start_lnum;
341 colnr_T start_col;
342 linenr_T end_lnum;
343 colnr_T end_col;
344 int error = FALSE;
345
346 if (check_for_dict_arg(argvars, 0) == FAIL
347 || check_for_list_arg(argvars, 1) == FAIL)
348 return;
349
350 if (argvars[1].vval.v_list == NULL)
351 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000352 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200353 return;
354 }
355
356 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100357 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200358 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000359 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200360 return;
361 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100362 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100364 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100365 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200366
367 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
368 return;
369
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100370 // This must be done _before_ we start adding properties because property
371 // changes trigger buffer (memline) reorganisation, which needs this flag
372 // to be correctly set.
373 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200374 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
375 {
376 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
377 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000378 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200379 return;
380 }
381
382 pos_list = li->li_tv.vval.v_list;
383 start_lnum = list_find_nr(pos_list, 0L, &error);
384 start_col = list_find_nr(pos_list, 1L, &error);
385 end_lnum = list_find_nr(pos_list, 2L, &error);
386 end_col = list_find_nr(pos_list, 3L, &error);
387 if (error || start_lnum <= 0 || start_col <= 0
388 || end_lnum <= 0 || end_col <= 0)
389 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000390 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200391 return;
392 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100393 if (prop_add_one(buf, type_name, id, NULL, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200394 start_col, end_col) == FAIL)
395 return;
396 }
397
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200398 redraw_buf_later(buf, VALID);
399}
400
401/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100402 * Get the next ID to use for a textprop with text in buffer "buf".
403 */
404 static int
405get_textprop_id(buf_T *buf)
406{
407 // TODO: recycle deleted entries
408 return -(buf->b_textprop_text.ga_len + 1);
409}
410
411/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200412 * Shared between prop_add() and popup_create().
413 * "dict_arg" is the function argument of a dict containing "bufnr".
414 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100415 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200416 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100417 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200418prop_add_common(
419 linenr_T start_lnum,
420 colnr_T start_col,
421 dict_T *dict,
422 buf_T *default_buf,
423 typval_T *dict_arg)
424{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200425 linenr_T end_lnum;
426 colnr_T end_col;
427 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200428 buf_T *buf = default_buf;
429 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100430 char_u *text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100431
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100432 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100433 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000434 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100435 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100436 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100437 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100438
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100439 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100440 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100441 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100442 if (end_lnum < start_lnum)
443 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000444 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100445 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100446 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100447 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100448 else
449 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100450
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100451 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100452 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100453 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100454
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100455 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100456 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000457 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100458 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100459 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100460 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100461 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100462 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100463 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100464 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100465 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000467 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100468 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469 }
470 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 else if (start_lnum == end_lnum)
472 end_col = start_col;
473 else
474 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100475
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100476 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100477 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100478
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100479 if (dict_has_key(dict, "text"))
480 {
481 text = dict_get_string(dict, "text", TRUE);
482 if (text == NULL)
483 goto theend;
484 // use a default length of 1 to make multiple props show up
485 end_col = start_col + 1;
486 }
487
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200488 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100489 goto theend;
490
491 if (id < 0 && buf->b_textprop_text.ga_len > 0)
492 {
493 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
494 goto theend;
495 }
496 if (text != NULL)
497 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100498
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100499 // This must be done _before_ we add the property because property changes
500 // trigger buffer (memline) reorganisation, which needs this flag to be
501 // correctly set.
502 buf->b_has_textprop = TRUE; // this is never reset
503
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100504 prop_add_one(buf, type_name, id, text,
505 start_lnum, end_lnum, start_col, end_col);
506 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100507
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200508 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100509
510theend:
511 vim_free(text);
512 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100513}
514
515/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100516 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100517 * Returns the number of text properties and, when non-zero, a pointer to the
518 * first one in "props" (note that it is not aligned, therefore the char_u
519 * pointer).
520 */
521 int
522get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
523{
524 char_u *text;
525 size_t textlen;
526 size_t proplen;
527
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100528 // Be quick when no text property types have been defined or the buffer,
529 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200530 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100531 return 0;
532
533 // Fetch the line to get the ml_line_len field updated.
534 text = ml_get_buf(buf, lnum, will_change);
535 textlen = STRLEN(text) + 1;
536 proplen = buf->b_ml.ml_line_len - textlen;
537 if (proplen % sizeof(textprop_T) != 0)
538 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000539 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100540 return 0;
541 }
542 if (proplen > 0)
543 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100544 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100545}
546
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200547/**
548 * Return the number of text properties on line "lnum" in the current buffer.
549 * When "only_starting" is true only text properties starting in this line will
550 * be considered.
551 */
552 int
553count_props(linenr_T lnum, int only_starting)
554{
555 char_u *props;
556 int proplen = get_text_props(curbuf, lnum, &props, 0);
557 int result = proplen;
558 int i;
559 textprop_T prop;
560
561 if (only_starting)
562 for (i = 0; i < proplen; ++i)
563 {
564 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
565 if (prop.tp_flags & TP_FLAG_CONT_PREV)
566 --result;
567 }
568 return result;
569}
570
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100571/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200572 * Find text property "type_id" in the visible lines of window "wp".
573 * Match "id" when it is > 0.
574 * Returns FAIL when not found.
575 */
576 int
577find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
578 linenr_T *found_lnum)
579{
580 linenr_T lnum;
581 char_u *props;
582 int count;
583 int i;
584
585 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100586 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200587 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
588 {
589 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
590 for (i = 0; i < count; ++i)
591 {
592 mch_memmove(prop, props + i * sizeof(textprop_T),
593 sizeof(textprop_T));
594 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
595 {
596 *found_lnum = lnum;
597 return OK;
598 }
599 }
600 }
601 return FAIL;
602}
603
604/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100605 * Set the text properties for line "lnum" to "props" with length "len".
606 * If "len" is zero text properties are removed, "props" is not used.
607 * Any existing text properties are dropped.
608 * Only works for the current buffer.
609 */
610 static void
611set_text_props(linenr_T lnum, char_u *props, int len)
612{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100613 char_u *text;
614 char_u *newtext;
615 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100616
617 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100618 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100619 newtext = alloc(textlen + len);
620 if (newtext == NULL)
621 return;
622 mch_memmove(newtext, text, textlen);
623 if (len > 0)
624 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100625 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100626 vim_free(curbuf->b_ml.ml_line_ptr);
627 curbuf->b_ml.ml_line_ptr = newtext;
628 curbuf->b_ml.ml_line_len = textlen + len;
629 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
630}
631
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100632 static proptype_T *
633find_type_by_id(hashtab_T *ht, int id)
634{
635 long todo;
636 hashitem_T *hi;
637
638 if (ht == NULL)
639 return NULL;
640
641 // TODO: Make this faster by keeping a list of types sorted on ID and use
642 // a binary search.
643
644 todo = (long)ht->ht_used;
645 for (hi = ht->ht_array; todo > 0; ++hi)
646 {
647 if (!HASHITEM_EMPTY(hi))
648 {
649 proptype_T *prop = HI2PT(hi);
650
651 if (prop->pt_id == id)
652 return prop;
653 --todo;
654 }
655 }
656 return NULL;
657}
658
659/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100660 * Fill 'dict' with text properties in 'prop'.
661 */
662 static void
663prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
664{
665 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200666 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100667
668 dict_add_number(dict, "col", prop->tp_col);
669 dict_add_number(dict, "length", prop->tp_len);
670 dict_add_number(dict, "id", prop->tp_id);
671 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
672 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200673
674 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
675 if (pt == NULL)
676 {
677 pt = find_type_by_id(global_proptypes, prop->tp_type);
678 buflocal = FALSE;
679 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100680 if (pt != NULL)
681 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200682
683 if (buflocal)
684 dict_add_number(dict, "type_bufnr", buf->b_fnum);
685 else
686 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100687}
688
689/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100690 * Find a property type by ID in "buf" or globally.
691 * Returns NULL if not found.
692 */
693 proptype_T *
694text_prop_type_by_id(buf_T *buf, int id)
695{
696 proptype_T *type;
697
698 type = find_type_by_id(buf->b_proptypes, id);
699 if (type == NULL)
700 type = find_type_by_id(global_proptypes, id);
701 return type;
702}
703
704/*
705 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
706 */
707 void
708f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
709{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200710 linenr_T start;
711 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100712 linenr_T lnum;
713 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100714 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100715
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200716 if (in_vim9script()
717 && (check_for_number_arg(argvars, 0) == FAIL
718 || check_for_opt_number_arg(argvars, 1) == FAIL
719 || (argvars[1].v_type != VAR_UNKNOWN
720 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
721 return;
722
723 start = tv_get_number(&argvars[0]);
724 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100725 if (argvars[1].v_type != VAR_UNKNOWN)
726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100727 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100728 if (argvars[2].v_type != VAR_UNKNOWN)
729 {
730 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
731 return;
732 }
733 }
734 if (start < 1 || end < 1)
735 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200736 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100737 return;
738 }
739
740 for (lnum = start; lnum <= end; ++lnum)
741 {
742 char_u *text;
743 size_t len;
744
745 if (lnum > buf->b_ml.ml_line_count)
746 break;
747 text = ml_get_buf(buf, lnum, FALSE);
748 len = STRLEN(text) + 1;
749 if ((size_t)buf->b_ml.ml_line_len > len)
750 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100751 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100752 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
753 {
754 char_u *newtext = vim_strsave(text);
755
756 // need to allocate the line now
757 if (newtext == NULL)
758 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100759 if (buf->b_ml.ml_flags & ML_ALLOCATED)
760 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100761 buf->b_ml.ml_line_ptr = newtext;
762 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
763 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100764 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100765 }
766 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100767 if (did_clear)
768 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100769}
770
771/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100772 * prop_find({props} [, {direction}])
773 */
774 void
775f_prop_find(typval_T *argvars, typval_T *rettv)
776{
777 pos_T *cursor = &curwin->w_cursor;
778 dict_T *dict;
779 buf_T *buf = curbuf;
780 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200781 int lnum_start;
782 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100783 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200784 int id = 0;
785 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200786 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100787 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200788 int lnum = -1;
789 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100790 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100791 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100792
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200793 if (in_vim9script()
794 && (check_for_dict_arg(argvars, 0) == FAIL
795 || check_for_opt_string_arg(argvars, 1) == FAIL))
796 return;
797
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100798 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
799 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000800 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100801 return;
802 }
803 dict = argvars[0].vval.v_dict;
804
805 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
806 return;
807 if (buf->b_ml.ml_mfp == NULL)
808 return;
809
810 if (argvars[1].v_type != VAR_UNKNOWN)
811 {
812 char_u *dir_s = tv_get_string(&argvars[1]);
813
814 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100815 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100816 else if (*dir_s != 'f')
817 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000818 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100819 return;
820 }
821 }
822
823 di = dict_find(dict, (char_u *)"lnum", -1);
824 if (di != NULL)
825 lnum = tv_get_number(&di->di_tv);
826
827 di = dict_find(dict, (char_u *)"col", -1);
828 if (di != NULL)
829 col = tv_get_number(&di->di_tv);
830
831 if (lnum == -1)
832 {
833 lnum = cursor->lnum;
834 col = cursor->col + 1;
835 }
836 else if (col == -1)
837 col = 1;
838
839 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
840 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200841 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100842 return;
843 }
844
Bram Moolenaard61efa52022-07-23 09:52:04 +0100845 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100846
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100847 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200848 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100849 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200850 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200851 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100852 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100853 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100854 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100855 proptype_T *type = lookup_prop_type(name, buf);
856
857 if (type == NULL)
858 return;
859 type_id = type->pt_id;
860 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100861 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200862 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100863 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000864 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100865 return;
866 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200867 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100868 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000869 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100870 return;
871 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100872
873 lnum_start = lnum;
874
875 if (rettv_dict_alloc(rettv) == FAIL)
876 return;
877
878 while (1)
879 {
880 char_u *text = ml_get_buf(buf, lnum, FALSE);
881 size_t textlen = STRLEN(text) + 1;
882 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200883 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100884 int i;
885 textprop_T prop;
886 int prop_start;
887 int prop_end;
888
LemonBoy9bd3ce22022-04-18 21:54:02 +0100889 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100890 {
891 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100892 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100893
LemonBoy9bd3ce22022-04-18 21:54:02 +0100894 // For the very first line try to find the first property before or
895 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100896 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100897 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100898 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100899 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100900 if (prop.tp_col > col)
901 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100902 }
903 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
904 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100905 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100906 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200907 : (id_found && prop.tp_id == id)
908 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100909 {
910 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100911 if (lnum_start == lnum
912 && col >= prop.tp_col
913 && (col <= prop.tp_col + prop.tp_len
914 - (prop.tp_len != 0)))
915 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100916
LemonBoy9bd3ce22022-04-18 21:54:02 +0100917 // The property was not continued from last line, it starts on
918 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100919 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100920 // The property does not continue on the next line, it ends on
921 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100922 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100923 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100924 seen_end = 1;
925
926 // Skip lines without the start flag.
927 if (!prop_start)
928 {
929 // Always search backwards for start when search started
930 // on a prop and we're not skipping.
931 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +0100932 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200933 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100934 }
935
936 // If skipstart is true, skip the prop at start pos (even if
937 // continued from another line).
938 if (start_pos_has_prop && skipstart && !seen_end)
939 {
940 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200941 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100942 }
943
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100944 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
945 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
946
947 return;
948 }
949 }
950
951 if (dir > 0)
952 {
953 if (lnum >= buf->b_ml.ml_line_count)
954 break;
955 lnum++;
956 }
957 else
958 {
959 if (lnum <= 1)
960 break;
961 lnum--;
962 }
963 }
964}
965
966/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000967 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
968 */
969 static int
970prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
971{
972 int i;
973
974 for (i = 0; i < len; i++)
975 if (types_or_ids[i] == type_or_id)
976 return TRUE;
977
978 return FALSE;
979}
980
981/*
982 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
983 * If 'prop_types' is not NULL, then return only the text properties with
984 * matching property type in the 'prop_types' array.
985 * If 'prop_ids' is not NULL, then return only the text properties with
986 * an identifier in the 'props_ids' array.
987 * If 'add_lnum' is TRUE, then add the line number also to the text property
988 * dictionary.
989 */
990 static void
991get_props_in_line(
992 buf_T *buf,
993 linenr_T lnum,
994 int *prop_types,
995 int prop_types_len,
996 int *prop_ids,
997 int prop_ids_len,
998 list_T *retlist,
999 int add_lnum)
1000{
1001 char_u *text = ml_get_buf(buf, lnum, FALSE);
1002 size_t textlen = STRLEN(text) + 1;
1003 int count;
1004 int i;
1005 textprop_T prop;
1006
1007 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1008 for (i = 0; i < count; ++i)
1009 {
1010 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1011 sizeof(textprop_T));
1012 if ((prop_types == NULL
1013 || prop_type_or_id_in_list(prop_types, prop_types_len,
1014 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001015 && (prop_ids == NULL
1016 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1017 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001018 {
1019 dict_T *d = dict_alloc();
1020
1021 if (d == NULL)
1022 break;
1023 prop_fill_dict(d, &prop, buf);
1024 if (add_lnum)
1025 dict_add_number(d, "lnum", lnum);
1026 list_append_dict(retlist, d);
1027 }
1028 }
1029}
1030
1031/*
1032 * Convert a List of property type names into an array of property type
1033 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1034 * error. 'num_types' is set to the number of returned property types.
1035 */
1036 static int *
1037get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1038{
1039 int *prop_types;
1040 listitem_T *li;
1041 int i;
1042 char_u *name;
1043 proptype_T *type;
1044
1045 *num_types = 0;
1046
1047 prop_types = ALLOC_MULT(int, list_len(l));
1048 if (prop_types == NULL)
1049 return NULL;
1050
1051 i = 0;
1052 FOR_ALL_LIST_ITEMS(l, li)
1053 {
1054 if (li->li_tv.v_type != VAR_STRING)
1055 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001056 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001057 goto errret;
1058 }
1059 name = li->li_tv.vval.v_string;
1060 if (name == NULL)
1061 goto errret;
1062
1063 type = lookup_prop_type(name, buf);
1064 if (type == NULL)
1065 goto errret;
1066 prop_types[i++] = type->pt_id;
1067 }
1068
1069 *num_types = i;
1070 return prop_types;
1071
1072errret:
1073 VIM_CLEAR(prop_types);
1074 return NULL;
1075}
1076
1077/*
1078 * Convert a List of property identifiers into an array of property
1079 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1080 * error. 'num_ids' is set to the number of returned property identifiers.
1081 */
1082 static int *
1083get_prop_ids_from_list(list_T *l, int *num_ids)
1084{
1085 int *prop_ids;
1086 listitem_T *li;
1087 int i;
1088 int id;
1089 int error;
1090
1091 *num_ids = 0;
1092
1093 prop_ids = ALLOC_MULT(int, list_len(l));
1094 if (prop_ids == NULL)
1095 return NULL;
1096
1097 i = 0;
1098 FOR_ALL_LIST_ITEMS(l, li)
1099 {
1100 error = FALSE;
1101 id = tv_get_number_chk(&li->li_tv, &error);
1102 if (error)
1103 goto errret;
1104
1105 prop_ids[i++] = id;
1106 }
1107
1108 *num_ids = i;
1109 return prop_ids;
1110
1111errret:
1112 VIM_CLEAR(prop_ids);
1113 return NULL;
1114}
1115
1116/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001117 * prop_list({lnum} [, {bufnr}])
1118 */
1119 void
1120f_prop_list(typval_T *argvars, typval_T *rettv)
1121{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001122 linenr_T lnum;
1123 linenr_T start_lnum;
1124 linenr_T end_lnum;
1125 buf_T *buf = curbuf;
1126 int add_lnum = FALSE;
1127 int *prop_types = NULL;
1128 int prop_types_len = 0;
1129 int *prop_ids = NULL;
1130 int prop_ids_len = 0;
1131 list_T *l;
1132 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001133
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001134 if (in_vim9script()
1135 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001136 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001137 return;
1138
Bram Moolenaar93a10962022-06-16 11:42:09 +01001139 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001140 return;
1141
1142 // default: get text properties on current line
1143 start_lnum = tv_get_number(&argvars[0]);
1144 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001145 if (argvars[1].v_type != VAR_UNKNOWN)
1146 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001147 dict_T *d;
1148
1149 if (argvars[1].v_type != VAR_DICT)
1150 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001151 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001152 return;
1153 }
1154 d = argvars[1].vval.v_dict;
1155
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001156 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1157 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001158
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001159 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001160 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001161 if (di->di_tv.v_type != VAR_NUMBER)
1162 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001163 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001164 return;
1165 }
1166 end_lnum = tv_get_number(&di->di_tv);
1167 if (end_lnum < 0)
1168 // negative end_lnum is used as an offset from the last buffer
1169 // line
1170 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1171 else if (end_lnum > buf->b_ml.ml_line_count)
1172 end_lnum = buf->b_ml.ml_line_count;
1173 add_lnum = TRUE;
1174 }
1175 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1176 {
1177 if (di->di_tv.v_type != VAR_LIST)
1178 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001179 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001180 return;
1181 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001182
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001183 l = di->di_tv.vval.v_list;
1184 if (l != NULL && list_len(l) > 0)
1185 {
1186 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1187 if (prop_types == NULL)
1188 return;
1189 }
1190 }
1191 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1192 {
1193 if (di->di_tv.v_type != VAR_LIST)
1194 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001195 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001196 goto errret;
1197 }
1198
1199 l = di->di_tv.vval.v_list;
1200 if (l != NULL && list_len(l) > 0)
1201 {
1202 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1203 if (prop_ids == NULL)
1204 goto errret;
1205 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001206 }
1207 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001208 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1209 || end_lnum < 1 || end_lnum < start_lnum)
1210 emsg(_(e_invalid_range));
1211 else
1212 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1213 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1214 prop_ids, prop_ids_len,
1215 rettv->vval.v_list, add_lnum);
1216
1217errret:
1218 VIM_CLEAR(prop_types);
1219 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001220}
1221
1222/*
1223 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1224 */
1225 void
1226f_prop_remove(typval_T *argvars, typval_T *rettv)
1227{
1228 linenr_T start = 1;
1229 linenr_T end = 0;
1230 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001231 linenr_T first_changed = 0;
1232 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001233 dict_T *dict;
1234 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001235 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001236 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001237 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001238 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001239 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001240
1241 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001242
1243 if (in_vim9script()
1244 && (check_for_dict_arg(argvars, 0) == FAIL
1245 || check_for_opt_number_arg(argvars, 1) == FAIL
1246 || (argvars[1].v_type != VAR_UNKNOWN
1247 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1248 return;
1249
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001250 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1251 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001252 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001253 return;
1254 }
1255
1256 if (argvars[1].v_type != VAR_UNKNOWN)
1257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001258 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001259 end = start;
1260 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001261 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001262 if (start < 1 || end < 1)
1263 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001264 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001265 return;
1266 }
1267 }
1268
1269 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001270 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1271 return;
1272 if (buf->b_ml.ml_mfp == NULL)
1273 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001274
Bram Moolenaard61efa52022-07-23 09:52:04 +01001275 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001276
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001277 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001278 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001279 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001280 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001281 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001282 proptype_T *type = lookup_prop_type(name, buf);
1283
1284 if (type == NULL)
1285 return;
1286 type_id = type->pt_id;
1287 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001288 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001289
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001290 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001291 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001292 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001293 return;
1294 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001295 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001296 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001297 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001298 return;
1299 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001300
1301 if (end == 0)
1302 end = buf->b_ml.ml_line_count;
1303 for (lnum = start; lnum <= end; ++lnum)
1304 {
1305 char_u *text;
1306 size_t len;
1307
1308 if (lnum > buf->b_ml.ml_line_count)
1309 break;
1310 text = ml_get_buf(buf, lnum, FALSE);
1311 len = STRLEN(text) + 1;
1312 if ((size_t)buf->b_ml.ml_line_len > len)
1313 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001314 static textprop_T textprop; // static because of alignment
1315 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001316
1317 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1318 / sizeof(textprop_T); ++idx)
1319 {
1320 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1321 + idx * sizeof(textprop_T);
1322 size_t taillen;
1323
1324 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001325 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1326 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001327 {
1328 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1329 {
1330 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1331
1332 // need to allocate the line to be able to change it
1333 if (newptr == NULL)
1334 return;
1335 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1336 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001337 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1338 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001339 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001340 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1341
1342 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001343 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001344 }
1345
1346 taillen = buf->b_ml.ml_line_len - len
1347 - (idx + 1) * sizeof(textprop_T);
1348 if (taillen > 0)
1349 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1350 taillen);
1351 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1352 --idx;
1353
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001354 if (textprop.tp_id < 0)
1355 {
1356 garray_T *gap = &buf->b_textprop_text;
1357 int ii = -textprop.tp_id - 1;
1358
1359 // negative ID: property with text - free the text
1360 if (ii < gap->ga_len)
1361 {
1362 char_u **p = ((char_u **)gap->ga_data) + ii;
1363 vim_free(*p);
1364 *p = NULL;
1365 did_remove_text = TRUE;
1366 }
1367 }
1368
Bram Moolenaar965c0442021-05-17 00:22:06 +02001369 if (first_changed == 0)
1370 first_changed = lnum;
1371 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001372 ++rettv->vval.v_number;
1373 if (!do_all)
1374 break;
1375 }
1376 }
1377 }
1378 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001379
Bram Moolenaar965c0442021-05-17 00:22:06 +02001380 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001381 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001382 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1383 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001384 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001385
1386 if (did_remove_text)
1387 {
1388 garray_T *gap = &buf->b_textprop_text;
1389
1390 // Reduce the growarray size for NULL pointers at the end.
1391 while (gap->ga_len > 0
1392 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1393 --gap->ga_len;
1394 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001395}
1396
1397/*
1398 * Common for f_prop_type_add() and f_prop_type_change().
1399 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001400 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001401prop_type_set(typval_T *argvars, int add)
1402{
1403 char_u *name;
1404 buf_T *buf = NULL;
1405 dict_T *dict;
1406 dictitem_T *di;
1407 proptype_T *prop;
1408
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001409 if (in_vim9script()
1410 && (check_for_string_arg(argvars, 0) == FAIL
1411 || check_for_dict_arg(argvars, 1) == FAIL))
1412 return;
1413
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001414 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001415 if (*name == NUL)
1416 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001417 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001418 return;
1419 }
1420
1421 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1422 return;
1423 dict = argvars[1].vval.v_dict;
1424
1425 prop = find_prop(name, buf);
1426 if (add)
1427 {
1428 hashtab_T **htp;
1429
1430 if (prop != NULL)
1431 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001432 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001433 return;
1434 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001435 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001436 if (prop == NULL)
1437 return;
1438 STRCPY(prop->pt_name, name);
1439 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001440 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001441 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1442 if (*htp == NULL)
1443 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001444 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001445 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001446 {
1447 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001448 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001449 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001450 hash_init(*htp);
1451 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001452 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001453 }
1454 else
1455 {
1456 if (prop == NULL)
1457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001458 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001459 return;
1460 }
1461 }
1462
1463 if (dict != NULL)
1464 {
1465 di = dict_find(dict, (char_u *)"highlight", -1);
1466 if (di != NULL)
1467 {
1468 char_u *highlight;
1469 int hl_id = 0;
1470
Bram Moolenaard61efa52022-07-23 09:52:04 +01001471 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001472 if (highlight != NULL && *highlight != NUL)
1473 hl_id = syn_name2id(highlight);
1474 if (hl_id <= 0)
1475 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001476 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001477 highlight == NULL ? (char_u *)"" : highlight);
1478 return;
1479 }
1480 prop->pt_hl_id = hl_id;
1481 }
1482
Bram Moolenaar58187f12019-05-05 16:33:47 +02001483 di = dict_find(dict, (char_u *)"combine", -1);
1484 if (di != NULL)
1485 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001486 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001487 prop->pt_flags |= PT_FLAG_COMBINE;
1488 else
1489 prop->pt_flags &= ~PT_FLAG_COMBINE;
1490 }
1491
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001492 di = dict_find(dict, (char_u *)"priority", -1);
1493 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001494 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495
1496 di = dict_find(dict, (char_u *)"start_incl", -1);
1497 if (di != NULL)
1498 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001499 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001500 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1501 else
1502 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1503 }
1504
1505 di = dict_find(dict, (char_u *)"end_incl", -1);
1506 if (di != NULL)
1507 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001508 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001509 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1510 else
1511 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1512 }
1513 }
1514}
1515
1516/*
1517 * prop_type_add({name}, {props})
1518 */
1519 void
1520f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1521{
1522 prop_type_set(argvars, TRUE);
1523}
1524
1525/*
1526 * prop_type_change({name}, {props})
1527 */
1528 void
1529f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1530{
1531 prop_type_set(argvars, FALSE);
1532}
1533
1534/*
1535 * prop_type_delete({name} [, {bufnr}])
1536 */
1537 void
1538f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1539{
1540 char_u *name;
1541 buf_T *buf = NULL;
1542 hashitem_T *hi;
1543
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001544 if (in_vim9script()
1545 && (check_for_string_arg(argvars, 0) == FAIL
1546 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1547 return;
1548
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001549 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001550 if (*name == NUL)
1551 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001552 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001553 return;
1554 }
1555
1556 if (argvars[1].v_type != VAR_UNKNOWN)
1557 {
1558 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1559 return;
1560 }
1561
1562 hi = find_prop_hi(name, buf);
1563 if (hi != NULL)
1564 {
1565 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001566 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001567
1568 if (buf == NULL)
1569 ht = global_proptypes;
1570 else
1571 ht = buf->b_proptypes;
1572 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001573 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001574 }
1575}
1576
1577/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001578 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001579 */
1580 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001581f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001582{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001583 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001584
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001585 if (in_vim9script()
1586 && (check_for_string_arg(argvars, 0) == FAIL
1587 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1588 return;
1589
1590 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001591 if (*name == NUL)
1592 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001593 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001594 return;
1595 }
1596 if (rettv_dict_alloc(rettv) == OK)
1597 {
1598 proptype_T *prop = NULL;
1599 buf_T *buf = NULL;
1600
1601 if (argvars[1].v_type != VAR_UNKNOWN)
1602 {
1603 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1604 return;
1605 }
1606
1607 prop = find_prop(name, buf);
1608 if (prop != NULL)
1609 {
1610 dict_T *d = rettv->vval.v_dict;
1611
1612 if (prop->pt_hl_id > 0)
1613 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1614 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001615 dict_add_number(d, "combine",
1616 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001617 dict_add_number(d, "start_incl",
1618 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1619 dict_add_number(d, "end_incl",
1620 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1621 if (buf != NULL)
1622 dict_add_number(d, "bufnr", buf->b_fnum);
1623 }
1624 }
1625}
1626
1627 static void
1628list_types(hashtab_T *ht, list_T *l)
1629{
1630 long todo;
1631 hashitem_T *hi;
1632
1633 todo = (long)ht->ht_used;
1634 for (hi = ht->ht_array; todo > 0; ++hi)
1635 {
1636 if (!HASHITEM_EMPTY(hi))
1637 {
1638 proptype_T *prop = HI2PT(hi);
1639
1640 list_append_string(l, prop->pt_name, -1);
1641 --todo;
1642 }
1643 }
1644}
1645
1646/*
1647 * prop_type_list([{bufnr}])
1648 */
1649 void
1650f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1651{
1652 buf_T *buf = NULL;
1653
1654 if (rettv_list_alloc(rettv) == OK)
1655 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001656 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1657 return;
1658
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001659 if (argvars[0].v_type != VAR_UNKNOWN)
1660 {
1661 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1662 return;
1663 }
1664 if (buf == NULL)
1665 {
1666 if (global_proptypes != NULL)
1667 list_types(global_proptypes, rettv->vval.v_list);
1668 }
1669 else if (buf->b_proptypes != NULL)
1670 list_types(buf->b_proptypes, rettv->vval.v_list);
1671 }
1672}
1673
1674/*
1675 * Free all property types in "ht".
1676 */
1677 static void
1678clear_ht_prop_types(hashtab_T *ht)
1679{
1680 long todo;
1681 hashitem_T *hi;
1682
1683 if (ht == NULL)
1684 return;
1685
1686 todo = (long)ht->ht_used;
1687 for (hi = ht->ht_array; todo > 0; ++hi)
1688 {
1689 if (!HASHITEM_EMPTY(hi))
1690 {
1691 proptype_T *prop = HI2PT(hi);
1692
1693 vim_free(prop);
1694 --todo;
1695 }
1696 }
1697
1698 hash_clear(ht);
1699 vim_free(ht);
1700}
1701
1702#if defined(EXITFREE) || defined(PROTO)
1703/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001704 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001705 */
1706 void
1707clear_global_prop_types(void)
1708{
1709 clear_ht_prop_types(global_proptypes);
1710 global_proptypes = NULL;
1711}
1712#endif
1713
1714/*
1715 * Free all property types for "buf".
1716 */
1717 void
1718clear_buf_prop_types(buf_T *buf)
1719{
1720 clear_ht_prop_types(buf->b_proptypes);
1721 buf->b_proptypes = NULL;
1722}
1723
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001724// Struct used to return two values from adjust_prop().
1725typedef struct
1726{
1727 int dirty; // if the property was changed
1728 int can_drop; // whether after this change, the prop may be removed
1729} adjustres_T;
1730
1731/*
1732 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1733 *
1734 * Note that "col" is zero-based, while tp_col is one-based.
1735 * Only for the current buffer.
1736 * "flags" can have:
1737 * APC_SUBSTITUTE: Text is replaced, not inserted.
1738 */
1739 static adjustres_T
1740adjust_prop(
1741 textprop_T *prop,
1742 colnr_T col,
1743 int added,
1744 int flags)
1745{
1746 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1747 int start_incl = (pt != NULL
1748 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001749 || (flags & APC_SUBSTITUTE)
1750 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001751 int end_incl = (pt != NULL
LemonBoy698cb4c2022-05-14 18:10:15 +01001752 && (pt->pt_flags & PT_FLAG_INS_END_INCL))
1753 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
1754 // Do not drop zero-width props if they later can increase in size.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001755 int droppable = !(start_incl || end_incl);
1756 adjustres_T res = {TRUE, FALSE};
1757
1758 if (added > 0)
1759 {
1760 if (col + 1 <= prop->tp_col
1761 - (start_incl || (prop->tp_len == 0 && end_incl)))
1762 // Change is entirely before the text property: Only shift
1763 prop->tp_col += added;
1764 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1765 // Insertion was inside text property
1766 prop->tp_len += added;
1767 }
1768 else if (prop->tp_col > col + 1)
1769 {
1770 if (prop->tp_col + added < col + 1)
1771 {
1772 prop->tp_len += (prop->tp_col - 1 - col) + added;
1773 prop->tp_col = col + 1;
1774 if (prop->tp_len <= 0)
1775 {
1776 prop->tp_len = 0;
1777 res.can_drop = droppable;
1778 }
1779 }
1780 else
1781 prop->tp_col += added;
1782 }
1783 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1784 {
1785 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1786
1787 prop->tp_len += after > 0 ? added + after : added;
1788 res.can_drop = prop->tp_len <= 0 && droppable;
1789 }
1790 else
1791 res.dirty = FALSE;
1792
1793 return res;
1794}
1795
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001796/*
1797 * Adjust the columns of text properties in line "lnum" after position "col" to
1798 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001799 * Note that "col" is zero-based, while tp_col is one-based.
1800 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001801 * "flags" can have:
1802 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1803 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001804 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001805 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001806 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001807 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001808adjust_prop_columns(
1809 linenr_T lnum,
1810 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001811 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001812 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001813{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001814 int proplen;
1815 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001816 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001817 int ri, wi;
1818 size_t textlen;
1819
1820 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001821 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001822
1823 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1824 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001825 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001826 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001827
Bram Moolenaar196d1572019-01-02 23:47:18 +01001828 wi = 0; // write index
1829 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001830 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001831 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001832 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001833
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001834 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1835 res = adjust_prop(&prop, col, bytes_added, flags);
1836 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001837 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001838 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001839 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1840 && u_savesub(lnum) == FAIL)
1841 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001842 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001843
1844 // u_savesub() may have updated curbuf->b_ml, fetch it again
1845 if (curbuf->b_ml.ml_line_lnum != lnum)
1846 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001847 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001848 if (res.can_drop)
1849 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001850 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001851 ++wi;
1852 }
1853 if (dirty)
1854 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001855 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1856
1857 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001858 {
1859 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1860
1861 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1862 vim_free(curbuf->b_ml.ml_line_ptr);
1863 curbuf->b_ml.ml_line_ptr = p;
1864 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001865 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001866 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001867 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001868 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001869}
1870
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001871/*
1872 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001873 * "lnum_props" is the line that has the properties from before the split.
1874 * "lnum_top" is the top line.
1875 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001876 * "deleted" is the number of bytes deleted.
1877 */
1878 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001879adjust_props_for_split(
1880 linenr_T lnum_props,
1881 linenr_T lnum_top,
1882 int kept,
1883 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001884{
1885 char_u *props;
1886 int count;
1887 garray_T prevprop;
1888 garray_T nextprop;
1889 int i;
1890 int skipped = kept + deleted;
1891
1892 if (!curbuf->b_has_textprop)
1893 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001894
1895 // Get the text properties from "lnum_props".
1896 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001897 ga_init2(&prevprop, sizeof(textprop_T), 10);
1898 ga_init2(&nextprop, sizeof(textprop_T), 10);
1899
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001900 // Keep the relevant ones in the first line, reducing the length if needed.
1901 // Copy the ones that include the split to the second line.
1902 // Move the ones after the split to the second line.
1903 for (i = 0; i < count; ++i)
1904 {
1905 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001906 proptype_T *pt;
1907 int start_incl, end_incl;
1908 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001909
1910 // copy the prop to an aligned structure
1911 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1912
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001913 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1914 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1915 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1916 cont_prev = prop.tp_col + !start_incl <= kept;
1917 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1918
1919 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001920 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001921 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1922
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001923 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001924 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001925 if (p->tp_col + p->tp_len >= kept)
1926 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001927 if (cont_next)
1928 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001929 }
1930
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001931 // Only add the property to the next line if the length is bigger than
1932 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001933 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001934 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001935 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001936 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001937 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001938 if (p->tp_col > skipped)
1939 p->tp_col -= skipped - 1;
1940 else
1941 {
1942 p->tp_len -= skipped - p->tp_col;
1943 p->tp_col = 1;
1944 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001945 if (cont_prev)
1946 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001947 }
1948 }
1949
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001950 set_text_props(lnum_top, prevprop.ga_data,
1951 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001952 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001953 set_text_props(lnum_top + 1, nextprop.ga_data,
1954 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001955 ga_clear(&nextprop);
1956}
1957
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001958/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001959 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001960 */
1961 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001962prepend_joined_props(
1963 char_u *new_props,
1964 int propcount,
1965 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001966 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001967 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001968 long col,
1969 int removed)
1970{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001971 char_u *props;
1972 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1973 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001974
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001975 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001976 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001977 textprop_T prop;
1978 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001979
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001980 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1981 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1982
1983 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001984 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001985
1986 if (add_all || end)
1987 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1988 &prop, sizeof(prop));
1989 else
1990 {
1991 int j;
1992 int found = FALSE;
1993
1994 // Search for continuing prop.
1995 for (j = *props_remaining; j < propcount; ++j)
1996 {
1997 textprop_T op;
1998
1999 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2000 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2001 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002002 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002003 found = TRUE;
2004 op.tp_len += op.tp_col - prop.tp_col;
2005 op.tp_col = prop.tp_col;
2006 // Start/end is taken care of when deleting joined lines
2007 op.tp_flags = prop.tp_flags;
2008 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2009 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002010 }
2011 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002012 if (!found)
2013 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002014 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002015 }
2016}
2017
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002018#endif // FEAT_PROP_POPUP