blob: d7db7aa3e12e40612f24180354e10ce8d6bec8c5 [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 Moolenaar98aefe72018-12-13 22:20:09 +01001236 int id = -1;
1237 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001238 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001239
1240 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001241
1242 if (in_vim9script()
1243 && (check_for_dict_arg(argvars, 0) == FAIL
1244 || check_for_opt_number_arg(argvars, 1) == FAIL
1245 || (argvars[1].v_type != VAR_UNKNOWN
1246 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1247 return;
1248
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001249 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1250 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001251 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001252 return;
1253 }
1254
1255 if (argvars[1].v_type != VAR_UNKNOWN)
1256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001257 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001258 end = start;
1259 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001260 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001261 if (start < 1 || end < 1)
1262 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001263 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001264 return;
1265 }
1266 }
1267
1268 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001269 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1270 return;
1271 if (buf->b_ml.ml_mfp == NULL)
1272 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001273
Bram Moolenaard61efa52022-07-23 09:52:04 +01001274 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001275
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001276 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001277 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001278 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001279 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001280 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001281 proptype_T *type = lookup_prop_type(name, buf);
1282
1283 if (type == NULL)
1284 return;
1285 type_id = type->pt_id;
1286 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001287 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001288
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001289 if (id == -1 && type_id == -1)
1290 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001291 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001292 return;
1293 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001294 if (both && (id == -1 || type_id == -1))
1295 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001296 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001297 return;
1298 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001299
1300 if (end == 0)
1301 end = buf->b_ml.ml_line_count;
1302 for (lnum = start; lnum <= end; ++lnum)
1303 {
1304 char_u *text;
1305 size_t len;
1306
1307 if (lnum > buf->b_ml.ml_line_count)
1308 break;
1309 text = ml_get_buf(buf, lnum, FALSE);
1310 len = STRLEN(text) + 1;
1311 if ((size_t)buf->b_ml.ml_line_len > len)
1312 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001313 static textprop_T textprop; // static because of alignment
1314 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001315
1316 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1317 / sizeof(textprop_T); ++idx)
1318 {
1319 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1320 + idx * sizeof(textprop_T);
1321 size_t taillen;
1322
1323 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001324 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1325 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001326 {
1327 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1328 {
1329 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1330
1331 // need to allocate the line to be able to change it
1332 if (newptr == NULL)
1333 return;
1334 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1335 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001336 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1337 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001338 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001339 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1340
1341 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001342 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001343 }
1344
1345 taillen = buf->b_ml.ml_line_len - len
1346 - (idx + 1) * sizeof(textprop_T);
1347 if (taillen > 0)
1348 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1349 taillen);
1350 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1351 --idx;
1352
Bram Moolenaar965c0442021-05-17 00:22:06 +02001353 if (first_changed == 0)
1354 first_changed = lnum;
1355 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001356 ++rettv->vval.v_number;
1357 if (!do_all)
1358 break;
1359 }
1360 }
1361 }
1362 }
Bram Moolenaar965c0442021-05-17 00:22:06 +02001363 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001364 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001365 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1366 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001367 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001368}
1369
1370/*
1371 * Common for f_prop_type_add() and f_prop_type_change().
1372 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001373 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001374prop_type_set(typval_T *argvars, int add)
1375{
1376 char_u *name;
1377 buf_T *buf = NULL;
1378 dict_T *dict;
1379 dictitem_T *di;
1380 proptype_T *prop;
1381
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001382 if (in_vim9script()
1383 && (check_for_string_arg(argvars, 0) == FAIL
1384 || check_for_dict_arg(argvars, 1) == FAIL))
1385 return;
1386
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001387 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001388 if (*name == NUL)
1389 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001390 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001391 return;
1392 }
1393
1394 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1395 return;
1396 dict = argvars[1].vval.v_dict;
1397
1398 prop = find_prop(name, buf);
1399 if (add)
1400 {
1401 hashtab_T **htp;
1402
1403 if (prop != NULL)
1404 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001405 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001406 return;
1407 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001408 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001409 if (prop == NULL)
1410 return;
1411 STRCPY(prop->pt_name, name);
1412 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001413 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001414 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1415 if (*htp == NULL)
1416 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001417 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001418 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001419 {
1420 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001421 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001422 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001423 hash_init(*htp);
1424 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001425 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001426 }
1427 else
1428 {
1429 if (prop == NULL)
1430 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001431 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001432 return;
1433 }
1434 }
1435
1436 if (dict != NULL)
1437 {
1438 di = dict_find(dict, (char_u *)"highlight", -1);
1439 if (di != NULL)
1440 {
1441 char_u *highlight;
1442 int hl_id = 0;
1443
Bram Moolenaard61efa52022-07-23 09:52:04 +01001444 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001445 if (highlight != NULL && *highlight != NUL)
1446 hl_id = syn_name2id(highlight);
1447 if (hl_id <= 0)
1448 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001449 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001450 highlight == NULL ? (char_u *)"" : highlight);
1451 return;
1452 }
1453 prop->pt_hl_id = hl_id;
1454 }
1455
Bram Moolenaar58187f12019-05-05 16:33:47 +02001456 di = dict_find(dict, (char_u *)"combine", -1);
1457 if (di != NULL)
1458 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001459 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001460 prop->pt_flags |= PT_FLAG_COMBINE;
1461 else
1462 prop->pt_flags &= ~PT_FLAG_COMBINE;
1463 }
1464
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001465 di = dict_find(dict, (char_u *)"priority", -1);
1466 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001467 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001468
1469 di = dict_find(dict, (char_u *)"start_incl", -1);
1470 if (di != NULL)
1471 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001472 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001473 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1474 else
1475 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1476 }
1477
1478 di = dict_find(dict, (char_u *)"end_incl", -1);
1479 if (di != NULL)
1480 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001481 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001482 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1483 else
1484 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1485 }
1486 }
1487}
1488
1489/*
1490 * prop_type_add({name}, {props})
1491 */
1492 void
1493f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1494{
1495 prop_type_set(argvars, TRUE);
1496}
1497
1498/*
1499 * prop_type_change({name}, {props})
1500 */
1501 void
1502f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1503{
1504 prop_type_set(argvars, FALSE);
1505}
1506
1507/*
1508 * prop_type_delete({name} [, {bufnr}])
1509 */
1510 void
1511f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1512{
1513 char_u *name;
1514 buf_T *buf = NULL;
1515 hashitem_T *hi;
1516
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001517 if (in_vim9script()
1518 && (check_for_string_arg(argvars, 0) == FAIL
1519 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1520 return;
1521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001522 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001523 if (*name == NUL)
1524 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001525 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001526 return;
1527 }
1528
1529 if (argvars[1].v_type != VAR_UNKNOWN)
1530 {
1531 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1532 return;
1533 }
1534
1535 hi = find_prop_hi(name, buf);
1536 if (hi != NULL)
1537 {
1538 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001539 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001540
1541 if (buf == NULL)
1542 ht = global_proptypes;
1543 else
1544 ht = buf->b_proptypes;
1545 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001546 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001547 }
1548}
1549
1550/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001551 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001552 */
1553 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001554f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001555{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001556 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001557
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001558 if (in_vim9script()
1559 && (check_for_string_arg(argvars, 0) == FAIL
1560 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1561 return;
1562
1563 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001564 if (*name == NUL)
1565 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001566 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001567 return;
1568 }
1569 if (rettv_dict_alloc(rettv) == OK)
1570 {
1571 proptype_T *prop = NULL;
1572 buf_T *buf = NULL;
1573
1574 if (argvars[1].v_type != VAR_UNKNOWN)
1575 {
1576 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1577 return;
1578 }
1579
1580 prop = find_prop(name, buf);
1581 if (prop != NULL)
1582 {
1583 dict_T *d = rettv->vval.v_dict;
1584
1585 if (prop->pt_hl_id > 0)
1586 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1587 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001588 dict_add_number(d, "combine",
1589 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001590 dict_add_number(d, "start_incl",
1591 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1592 dict_add_number(d, "end_incl",
1593 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1594 if (buf != NULL)
1595 dict_add_number(d, "bufnr", buf->b_fnum);
1596 }
1597 }
1598}
1599
1600 static void
1601list_types(hashtab_T *ht, list_T *l)
1602{
1603 long todo;
1604 hashitem_T *hi;
1605
1606 todo = (long)ht->ht_used;
1607 for (hi = ht->ht_array; todo > 0; ++hi)
1608 {
1609 if (!HASHITEM_EMPTY(hi))
1610 {
1611 proptype_T *prop = HI2PT(hi);
1612
1613 list_append_string(l, prop->pt_name, -1);
1614 --todo;
1615 }
1616 }
1617}
1618
1619/*
1620 * prop_type_list([{bufnr}])
1621 */
1622 void
1623f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1624{
1625 buf_T *buf = NULL;
1626
1627 if (rettv_list_alloc(rettv) == OK)
1628 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001629 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1630 return;
1631
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001632 if (argvars[0].v_type != VAR_UNKNOWN)
1633 {
1634 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1635 return;
1636 }
1637 if (buf == NULL)
1638 {
1639 if (global_proptypes != NULL)
1640 list_types(global_proptypes, rettv->vval.v_list);
1641 }
1642 else if (buf->b_proptypes != NULL)
1643 list_types(buf->b_proptypes, rettv->vval.v_list);
1644 }
1645}
1646
1647/*
1648 * Free all property types in "ht".
1649 */
1650 static void
1651clear_ht_prop_types(hashtab_T *ht)
1652{
1653 long todo;
1654 hashitem_T *hi;
1655
1656 if (ht == NULL)
1657 return;
1658
1659 todo = (long)ht->ht_used;
1660 for (hi = ht->ht_array; todo > 0; ++hi)
1661 {
1662 if (!HASHITEM_EMPTY(hi))
1663 {
1664 proptype_T *prop = HI2PT(hi);
1665
1666 vim_free(prop);
1667 --todo;
1668 }
1669 }
1670
1671 hash_clear(ht);
1672 vim_free(ht);
1673}
1674
1675#if defined(EXITFREE) || defined(PROTO)
1676/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001677 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001678 */
1679 void
1680clear_global_prop_types(void)
1681{
1682 clear_ht_prop_types(global_proptypes);
1683 global_proptypes = NULL;
1684}
1685#endif
1686
1687/*
1688 * Free all property types for "buf".
1689 */
1690 void
1691clear_buf_prop_types(buf_T *buf)
1692{
1693 clear_ht_prop_types(buf->b_proptypes);
1694 buf->b_proptypes = NULL;
1695}
1696
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001697// Struct used to return two values from adjust_prop().
1698typedef struct
1699{
1700 int dirty; // if the property was changed
1701 int can_drop; // whether after this change, the prop may be removed
1702} adjustres_T;
1703
1704/*
1705 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1706 *
1707 * Note that "col" is zero-based, while tp_col is one-based.
1708 * Only for the current buffer.
1709 * "flags" can have:
1710 * APC_SUBSTITUTE: Text is replaced, not inserted.
1711 */
1712 static adjustres_T
1713adjust_prop(
1714 textprop_T *prop,
1715 colnr_T col,
1716 int added,
1717 int flags)
1718{
1719 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1720 int start_incl = (pt != NULL
1721 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001722 || (flags & APC_SUBSTITUTE)
1723 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001724 int end_incl = (pt != NULL
LemonBoy698cb4c2022-05-14 18:10:15 +01001725 && (pt->pt_flags & PT_FLAG_INS_END_INCL))
1726 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
1727 // Do not drop zero-width props if they later can increase in size.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001728 int droppable = !(start_incl || end_incl);
1729 adjustres_T res = {TRUE, FALSE};
1730
1731 if (added > 0)
1732 {
1733 if (col + 1 <= prop->tp_col
1734 - (start_incl || (prop->tp_len == 0 && end_incl)))
1735 // Change is entirely before the text property: Only shift
1736 prop->tp_col += added;
1737 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1738 // Insertion was inside text property
1739 prop->tp_len += added;
1740 }
1741 else if (prop->tp_col > col + 1)
1742 {
1743 if (prop->tp_col + added < col + 1)
1744 {
1745 prop->tp_len += (prop->tp_col - 1 - col) + added;
1746 prop->tp_col = col + 1;
1747 if (prop->tp_len <= 0)
1748 {
1749 prop->tp_len = 0;
1750 res.can_drop = droppable;
1751 }
1752 }
1753 else
1754 prop->tp_col += added;
1755 }
1756 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1757 {
1758 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1759
1760 prop->tp_len += after > 0 ? added + after : added;
1761 res.can_drop = prop->tp_len <= 0 && droppable;
1762 }
1763 else
1764 res.dirty = FALSE;
1765
1766 return res;
1767}
1768
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001769/*
1770 * Adjust the columns of text properties in line "lnum" after position "col" to
1771 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001772 * Note that "col" is zero-based, while tp_col is one-based.
1773 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001774 * "flags" can have:
1775 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1776 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001777 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001778 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001779 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001780 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001781adjust_prop_columns(
1782 linenr_T lnum,
1783 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001784 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001785 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001786{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001787 int proplen;
1788 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001789 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001790 int ri, wi;
1791 size_t textlen;
1792
1793 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001794 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001795
1796 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1797 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001798 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001799 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001800
Bram Moolenaar196d1572019-01-02 23:47:18 +01001801 wi = 0; // write index
1802 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001803 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001804 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001805 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001806
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001807 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1808 res = adjust_prop(&prop, col, bytes_added, flags);
1809 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001810 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001811 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001812 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1813 && u_savesub(lnum) == FAIL)
1814 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001815 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001816
1817 // u_savesub() may have updated curbuf->b_ml, fetch it again
1818 if (curbuf->b_ml.ml_line_lnum != lnum)
1819 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001820 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001821 if (res.can_drop)
1822 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001823 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001824 ++wi;
1825 }
1826 if (dirty)
1827 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001828 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1829
1830 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001831 {
1832 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1833
1834 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1835 vim_free(curbuf->b_ml.ml_line_ptr);
1836 curbuf->b_ml.ml_line_ptr = p;
1837 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001838 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001839 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001840 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001841 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001842}
1843
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001844/*
1845 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001846 * "lnum_props" is the line that has the properties from before the split.
1847 * "lnum_top" is the top line.
1848 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001849 * "deleted" is the number of bytes deleted.
1850 */
1851 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001852adjust_props_for_split(
1853 linenr_T lnum_props,
1854 linenr_T lnum_top,
1855 int kept,
1856 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001857{
1858 char_u *props;
1859 int count;
1860 garray_T prevprop;
1861 garray_T nextprop;
1862 int i;
1863 int skipped = kept + deleted;
1864
1865 if (!curbuf->b_has_textprop)
1866 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001867
1868 // Get the text properties from "lnum_props".
1869 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001870 ga_init2(&prevprop, sizeof(textprop_T), 10);
1871 ga_init2(&nextprop, sizeof(textprop_T), 10);
1872
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001873 // Keep the relevant ones in the first line, reducing the length if needed.
1874 // Copy the ones that include the split to the second line.
1875 // Move the ones after the split to the second line.
1876 for (i = 0; i < count; ++i)
1877 {
1878 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001879 proptype_T *pt;
1880 int start_incl, end_incl;
1881 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001882
1883 // copy the prop to an aligned structure
1884 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1885
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001886 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1887 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1888 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1889 cont_prev = prop.tp_col + !start_incl <= kept;
1890 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1891
1892 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001893 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001894 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1895
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001896 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001897 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001898 if (p->tp_col + p->tp_len >= kept)
1899 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001900 if (cont_next)
1901 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001902 }
1903
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001904 // Only add the property to the next line if the length is bigger than
1905 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001906 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001907 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001908 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001909 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001910 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001911 if (p->tp_col > skipped)
1912 p->tp_col -= skipped - 1;
1913 else
1914 {
1915 p->tp_len -= skipped - p->tp_col;
1916 p->tp_col = 1;
1917 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001918 if (cont_prev)
1919 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001920 }
1921 }
1922
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001923 set_text_props(lnum_top, prevprop.ga_data,
1924 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001925 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001926 set_text_props(lnum_top + 1, nextprop.ga_data,
1927 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001928 ga_clear(&nextprop);
1929}
1930
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001931/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001932 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001933 */
1934 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001935prepend_joined_props(
1936 char_u *new_props,
1937 int propcount,
1938 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001939 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001940 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001941 long col,
1942 int removed)
1943{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001944 char_u *props;
1945 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1946 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001947
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001948 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001949 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001950 textprop_T prop;
1951 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001952
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001953 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1954 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1955
1956 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001957 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001958
1959 if (add_all || end)
1960 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1961 &prop, sizeof(prop));
1962 else
1963 {
1964 int j;
1965 int found = FALSE;
1966
1967 // Search for continuing prop.
1968 for (j = *props_remaining; j < propcount; ++j)
1969 {
1970 textprop_T op;
1971
1972 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1973 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1974 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001975 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001976 found = TRUE;
1977 op.tp_len += op.tp_col - prop.tp_col;
1978 op.tp_col = prop.tp_col;
1979 // Start/end is taken care of when deleting joined lines
1980 op.tp_flags = prop.tp_flags;
1981 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1982 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001983 }
1984 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001985 if (!found)
1986 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001987 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001988 }
1989}
1990
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001991#endif // FEAT_PROP_POPUP