blob: 4c17a4aca120ed8aeb3e01cb4f72084859ab5c48 [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
50static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010051
52/*
53 * Find a property type by name, return the hashitem.
54 * Returns NULL if the item can't be found.
55 */
56 static hashitem_T *
57find_prop_hi(char_u *name, buf_T *buf)
58{
59 hashtab_T *ht;
60 hashitem_T *hi;
61
62 if (*name == NUL)
63 return NULL;
64 if (buf == NULL)
65 ht = global_proptypes;
66 else
67 ht = buf->b_proptypes;
68
69 if (ht == NULL)
70 return NULL;
71 hi = hash_find(ht, name);
72 if (HASHITEM_EMPTY(hi))
73 return NULL;
74 return hi;
75}
76
77/*
78 * Like find_prop_hi() but return the property type.
79 */
80 static proptype_T *
81find_prop(char_u *name, buf_T *buf)
82{
83 hashitem_T *hi = find_prop_hi(name, buf);
84
85 if (hi == NULL)
86 return NULL;
87 return HI2PT(hi);
88}
89
90/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020091 * Get the prop type ID of "name".
92 * When not found return zero.
93 */
94 int
95find_prop_type_id(char_u *name, buf_T *buf)
96{
97 proptype_T *pt = find_prop(name, buf);
98
99 if (pt == NULL)
100 return 0;
101 return pt->pt_id;
102}
103
104/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100105 * Lookup a property type by name. First in "buf" and when not found in the
106 * global types.
107 * When not found gives an error message and returns NULL.
108 */
109 static proptype_T *
110lookup_prop_type(char_u *name, buf_T *buf)
111{
112 proptype_T *type = find_prop(name, buf);
113
114 if (type == NULL)
115 type = find_prop(name, NULL);
116 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100117 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100118 return type;
119}
120
121/*
122 * Get an optional "bufnr" item from the dict in "arg".
123 * When the argument is not used or "bufnr" is not present then "buf" is
124 * unchanged.
125 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100126 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 */
128 static int
129get_bufnr_from_arg(typval_T *arg, buf_T **buf)
130{
131 dictitem_T *di;
132
133 if (arg->v_type != VAR_DICT)
134 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000135 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100136 return FAIL;
137 }
138 if (arg->vval.v_dict == NULL)
139 return OK; // NULL dict is like an empty dict
140 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200141 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
142 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200144 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100145 if (*buf == NULL)
146 return FAIL;
147 }
148 return OK;
149}
150
151/*
152 * prop_add({lnum}, {col}, {props})
153 */
154 void
155f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
156{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100157 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100158 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100159
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200160 if (in_vim9script()
161 && (check_for_number_arg(argvars, 0) == FAIL
162 || check_for_number_arg(argvars, 1) == FAIL
163 || check_for_dict_arg(argvars, 2) == FAIL))
164 return;
165
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100166 start_lnum = tv_get_number(&argvars[0]);
167 start_col = tv_get_number(&argvars[1]);
168 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100169 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000170 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100171 return;
172 }
173 if (argvars[2].v_type != VAR_DICT)
174 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000175 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100176 return;
177 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200178
179 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
180 curbuf, &argvars[2]);
181}
182
183/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200184 * Attach a text property 'type_name' to the text starting
185 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
186 * the buffer 'buf' and assign identifier 'id'.
187 */
188 static int
189prop_add_one(
190 buf_T *buf,
191 char_u *type_name,
192 int id,
193 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;
207
208 type = lookup_prop_type(type_name, buf);
209 if (type == NULL)
210 return FAIL;
211
212 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
213 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000214 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200215 return FAIL;
216 }
217 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
218 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000219 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200220 return FAIL;
221 }
222
223 if (buf->b_ml.ml_mfp == NULL)
224 {
225 emsg(_("E275: Cannot add text property to unloaded buffer"));
226 return FAIL;
227 }
228
229 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
230 {
231 colnr_T col; // start column
232 long length; // in bytes
233
234 // Fetch the line to get the ml_line_len field updated.
235 proplen = get_text_props(buf, lnum, &props, TRUE);
236 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
237
238 if (lnum == start_lnum)
239 col = start_col;
240 else
241 col = 1;
242 if (col - 1 > (colnr_T)textlen)
243 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000244 semsg(_(e_invalid_column_number_nr), (long)start_col);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200245 return FAIL;
246 }
247
248 if (lnum == end_lnum)
249 length = end_col - col;
250 else
251 length = (int)textlen - col + 1;
252 if (length > (long)textlen)
253 length = (int)textlen; // can include the end-of-line
254 if (length < 0)
255 length = 0; // zero-width property
256
257 // Allocate the new line with space for the new property.
258 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
259 if (newtext == NULL)
260 return FAIL;
261 // Copy the text, including terminating NUL.
262 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
263
264 // Find the index where to insert the new property.
265 // Since the text properties are not aligned properly when stored with
266 // the text, we need to copy them as bytes before using it as a struct.
267 for (i = 0; i < proplen; ++i)
268 {
269 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
270 sizeof(textprop_T));
271 if (tmp_prop.tp_col >= col)
272 break;
273 }
274 newprops = newtext + textlen;
275 if (i > 0)
276 mch_memmove(newprops, props, sizeof(textprop_T) * i);
277
278 tmp_prop.tp_col = col;
279 tmp_prop.tp_len = length;
280 tmp_prop.tp_id = id;
281 tmp_prop.tp_type = type->pt_id;
282 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
283 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
284 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
285 sizeof(textprop_T));
286
287 if (i < proplen)
288 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
289 props + i * sizeof(textprop_T),
290 sizeof(textprop_T) * (proplen - i));
291
292 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
293 vim_free(buf->b_ml.ml_line_ptr);
294 buf->b_ml.ml_line_ptr = newtext;
295 buf->b_ml.ml_line_len += sizeof(textprop_T);
296 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
297 }
298
299 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
300 return OK;
301}
302
303/*
304 * prop_add_list()
305 * First argument specifies the text property:
306 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
307 * Second argument is a List where each item is a List with the following
308 * entries: [lnum, start_col, end_col]
309 */
310 void
311f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
312{
313 dict_T *dict;
314 char_u *type_name;
315 buf_T *buf = curbuf;
316 int id = 0;
317 listitem_T *li;
318 list_T *pos_list;
319 linenr_T start_lnum;
320 colnr_T start_col;
321 linenr_T end_lnum;
322 colnr_T end_col;
323 int error = FALSE;
324
325 if (check_for_dict_arg(argvars, 0) == FAIL
326 || check_for_list_arg(argvars, 1) == FAIL)
327 return;
328
329 if (argvars[1].vval.v_list == NULL)
330 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000331 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200332 return;
333 }
334
335 dict = argvars[0].vval.v_dict;
336 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
337 {
338 emsg(_("E965: missing property type name"));
339 return;
340 }
341 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
342
343 if (dict_find(dict, (char_u *)"id", -1) != NULL)
344 id = dict_get_number(dict, (char_u *)"id");
345
346 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
347 return;
348
349 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
350 {
351 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
352 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000353 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200354 return;
355 }
356
357 pos_list = li->li_tv.vval.v_list;
358 start_lnum = list_find_nr(pos_list, 0L, &error);
359 start_col = list_find_nr(pos_list, 1L, &error);
360 end_lnum = list_find_nr(pos_list, 2L, &error);
361 end_col = list_find_nr(pos_list, 3L, &error);
362 if (error || start_lnum <= 0 || start_col <= 0
363 || end_lnum <= 0 || end_col <= 0)
364 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000365 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200366 return;
367 }
368 if (prop_add_one(buf, type_name, id, start_lnum, end_lnum,
369 start_col, end_col) == FAIL)
370 return;
371 }
372
373 buf->b_has_textprop = TRUE; // this is never reset
374 redraw_buf_later(buf, VALID);
375}
376
377/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200378 * Shared between prop_add() and popup_create().
379 * "dict_arg" is the function argument of a dict containing "bufnr".
380 * it is NULL for popup_create().
381 */
382 void
383prop_add_common(
384 linenr_T start_lnum,
385 colnr_T start_col,
386 dict_T *dict,
387 buf_T *default_buf,
388 typval_T *dict_arg)
389{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200390 linenr_T end_lnum;
391 colnr_T end_col;
392 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200393 buf_T *buf = default_buf;
394 int id = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100395
396 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100398 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100399 return;
400 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100401 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100402
403 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
404 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100405 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
406 if (end_lnum < start_lnum)
407 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000408 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100409 return;
410 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100411 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100412 else
413 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100414
415 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100416 {
417 long length = dict_get_number(dict, (char_u *)"length");
418
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100419 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100420 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000421 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100422 return;
423 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100424 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100425 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100426 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
427 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100428 end_col = dict_get_number(dict, (char_u *)"end_col");
429 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100430 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000431 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100432 return;
433 }
434 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100435 else if (start_lnum == end_lnum)
436 end_col = start_col;
437 else
438 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100439
440 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100441 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100442
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200443 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100444 return;
445
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200446 prop_add_one(buf, type_name, id, start_lnum, end_lnum, start_col, end_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100447
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100448 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200449 redraw_buf_later(buf, VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100450}
451
452/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100453 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100454 * Returns the number of text properties and, when non-zero, a pointer to the
455 * first one in "props" (note that it is not aligned, therefore the char_u
456 * pointer).
457 */
458 int
459get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
460{
461 char_u *text;
462 size_t textlen;
463 size_t proplen;
464
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100465 // Be quick when no text property types have been defined or the buffer,
466 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200467 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100468 return 0;
469
470 // Fetch the line to get the ml_line_len field updated.
471 text = ml_get_buf(buf, lnum, will_change);
472 textlen = STRLEN(text) + 1;
473 proplen = buf->b_ml.ml_line_len - textlen;
474 if (proplen % sizeof(textprop_T) != 0)
475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100476 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100477 return 0;
478 }
479 if (proplen > 0)
480 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100481 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100482}
483
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200484/**
485 * Return the number of text properties on line "lnum" in the current buffer.
486 * When "only_starting" is true only text properties starting in this line will
487 * be considered.
488 */
489 int
490count_props(linenr_T lnum, int only_starting)
491{
492 char_u *props;
493 int proplen = get_text_props(curbuf, lnum, &props, 0);
494 int result = proplen;
495 int i;
496 textprop_T prop;
497
498 if (only_starting)
499 for (i = 0; i < proplen; ++i)
500 {
501 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
502 if (prop.tp_flags & TP_FLAG_CONT_PREV)
503 --result;
504 }
505 return result;
506}
507
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100508/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200509 * Find text property "type_id" in the visible lines of window "wp".
510 * Match "id" when it is > 0.
511 * Returns FAIL when not found.
512 */
513 int
514find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
515 linenr_T *found_lnum)
516{
517 linenr_T lnum;
518 char_u *props;
519 int count;
520 int i;
521
522 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100523 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200524 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
525 {
526 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
527 for (i = 0; i < count; ++i)
528 {
529 mch_memmove(prop, props + i * sizeof(textprop_T),
530 sizeof(textprop_T));
531 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
532 {
533 *found_lnum = lnum;
534 return OK;
535 }
536 }
537 }
538 return FAIL;
539}
540
541/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100542 * Set the text properties for line "lnum" to "props" with length "len".
543 * If "len" is zero text properties are removed, "props" is not used.
544 * Any existing text properties are dropped.
545 * Only works for the current buffer.
546 */
547 static void
548set_text_props(linenr_T lnum, char_u *props, int len)
549{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100550 char_u *text;
551 char_u *newtext;
552 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100553
554 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100555 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100556 newtext = alloc(textlen + len);
557 if (newtext == NULL)
558 return;
559 mch_memmove(newtext, text, textlen);
560 if (len > 0)
561 mch_memmove(newtext + textlen, props, len);
562 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
563 vim_free(curbuf->b_ml.ml_line_ptr);
564 curbuf->b_ml.ml_line_ptr = newtext;
565 curbuf->b_ml.ml_line_len = textlen + len;
566 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
567}
568
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100569 static proptype_T *
570find_type_by_id(hashtab_T *ht, int id)
571{
572 long todo;
573 hashitem_T *hi;
574
575 if (ht == NULL)
576 return NULL;
577
578 // TODO: Make this faster by keeping a list of types sorted on ID and use
579 // a binary search.
580
581 todo = (long)ht->ht_used;
582 for (hi = ht->ht_array; todo > 0; ++hi)
583 {
584 if (!HASHITEM_EMPTY(hi))
585 {
586 proptype_T *prop = HI2PT(hi);
587
588 if (prop->pt_id == id)
589 return prop;
590 --todo;
591 }
592 }
593 return NULL;
594}
595
596/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100597 * Fill 'dict' with text properties in 'prop'.
598 */
599 static void
600prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
601{
602 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200603 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100604
605 dict_add_number(dict, "col", prop->tp_col);
606 dict_add_number(dict, "length", prop->tp_len);
607 dict_add_number(dict, "id", prop->tp_id);
608 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
609 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200610
611 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
612 if (pt == NULL)
613 {
614 pt = find_type_by_id(global_proptypes, prop->tp_type);
615 buflocal = FALSE;
616 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100617 if (pt != NULL)
618 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200619
620 if (buflocal)
621 dict_add_number(dict, "type_bufnr", buf->b_fnum);
622 else
623 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100624}
625
626/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100627 * Find a property type by ID in "buf" or globally.
628 * Returns NULL if not found.
629 */
630 proptype_T *
631text_prop_type_by_id(buf_T *buf, int id)
632{
633 proptype_T *type;
634
635 type = find_type_by_id(buf->b_proptypes, id);
636 if (type == NULL)
637 type = find_type_by_id(global_proptypes, id);
638 return type;
639}
640
641/*
642 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
643 */
644 void
645f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
646{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200647 linenr_T start;
648 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100649 linenr_T lnum;
650 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100651 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100652
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200653 if (in_vim9script()
654 && (check_for_number_arg(argvars, 0) == FAIL
655 || check_for_opt_number_arg(argvars, 1) == FAIL
656 || (argvars[1].v_type != VAR_UNKNOWN
657 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
658 return;
659
660 start = tv_get_number(&argvars[0]);
661 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100662 if (argvars[1].v_type != VAR_UNKNOWN)
663 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100664 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100665 if (argvars[2].v_type != VAR_UNKNOWN)
666 {
667 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
668 return;
669 }
670 }
671 if (start < 1 || end < 1)
672 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200673 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100674 return;
675 }
676
677 for (lnum = start; lnum <= end; ++lnum)
678 {
679 char_u *text;
680 size_t len;
681
682 if (lnum > buf->b_ml.ml_line_count)
683 break;
684 text = ml_get_buf(buf, lnum, FALSE);
685 len = STRLEN(text) + 1;
686 if ((size_t)buf->b_ml.ml_line_len > len)
687 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100688 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100689 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
690 {
691 char_u *newtext = vim_strsave(text);
692
693 // need to allocate the line now
694 if (newtext == NULL)
695 return;
696 buf->b_ml.ml_line_ptr = newtext;
697 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
698 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100699 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100700 }
701 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100702 if (did_clear)
703 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100704}
705
706/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100707 * prop_find({props} [, {direction}])
708 */
709 void
710f_prop_find(typval_T *argvars, typval_T *rettv)
711{
712 pos_T *cursor = &curwin->w_cursor;
713 dict_T *dict;
714 buf_T *buf = curbuf;
715 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200716 int lnum_start;
717 int start_pos_has_prop = 0;
718 int seen_end = 0;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200719 int id = 0;
720 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200721 int type_id = -1;
722 int skipstart = 0;
723 int lnum = -1;
724 int col = -1;
725 int dir = 1; // 1 = forward, -1 = backward
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100726 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100727
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200728 if (in_vim9script()
729 && (check_for_dict_arg(argvars, 0) == FAIL
730 || check_for_opt_string_arg(argvars, 1) == FAIL))
731 return;
732
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100733 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
734 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000735 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100736 return;
737 }
738 dict = argvars[0].vval.v_dict;
739
740 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
741 return;
742 if (buf->b_ml.ml_mfp == NULL)
743 return;
744
745 if (argvars[1].v_type != VAR_UNKNOWN)
746 {
747 char_u *dir_s = tv_get_string(&argvars[1]);
748
749 if (*dir_s == 'b')
750 dir = -1;
751 else if (*dir_s != 'f')
752 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000753 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100754 return;
755 }
756 }
757
758 di = dict_find(dict, (char_u *)"lnum", -1);
759 if (di != NULL)
760 lnum = tv_get_number(&di->di_tv);
761
762 di = dict_find(dict, (char_u *)"col", -1);
763 if (di != NULL)
764 col = tv_get_number(&di->di_tv);
765
766 if (lnum == -1)
767 {
768 lnum = cursor->lnum;
769 col = cursor->col + 1;
770 }
771 else if (col == -1)
772 col = 1;
773
774 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
775 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200776 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100777 return;
778 }
779
Bram Moolenaareb245562020-09-03 22:33:44 +0200780 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100781
782 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200783 {
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100784 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200785 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200786 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100787 if (dict_find(dict, (char_u *)"type", -1))
788 {
789 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
790 proptype_T *type = lookup_prop_type(name, buf);
791
792 if (type == NULL)
793 return;
794 type_id = type->pt_id;
795 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100796 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200797 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100798 {
799 emsg(_("E968: Need at least one of 'id' or 'type'"));
800 return;
801 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200802 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100803 {
804 emsg(_("E860: Need 'id' and 'type' with 'both'"));
805 return;
806 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100807
808 lnum_start = lnum;
809
810 if (rettv_dict_alloc(rettv) == FAIL)
811 return;
812
813 while (1)
814 {
815 char_u *text = ml_get_buf(buf, lnum, FALSE);
816 size_t textlen = STRLEN(text) + 1;
817 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200818 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100819 int i;
820 textprop_T prop;
821 int prop_start;
822 int prop_end;
823
824 for (i = 0; i < count; ++i)
825 {
826 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
827 sizeof(textprop_T));
828
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100829 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100830 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100831 if (dir < 0)
832 {
833 if (col < prop.tp_col)
834 break;
835 }
836 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
837 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100838 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100839 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200840 : (id_found && prop.tp_id == id)
841 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100842 {
843 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100844 if (lnum_start == lnum
845 && col >= prop.tp_col
846 && (col <= prop.tp_col + prop.tp_len
847 - (prop.tp_len != 0)))
848 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100849
850 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
851 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
852 if (!prop_start && prop_end && dir > 0)
853 seen_end = 1;
854
855 // Skip lines without the start flag.
856 if (!prop_start)
857 {
858 // Always search backwards for start when search started
859 // on a prop and we're not skipping.
860 if (start_pos_has_prop && !skipstart)
861 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200862 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100863 }
864
865 // If skipstart is true, skip the prop at start pos (even if
866 // continued from another line).
867 if (start_pos_has_prop && skipstart && !seen_end)
868 {
869 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200870 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100871 }
872
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100873 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
874 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
875
876 return;
877 }
878 }
879
880 if (dir > 0)
881 {
882 if (lnum >= buf->b_ml.ml_line_count)
883 break;
884 lnum++;
885 }
886 else
887 {
888 if (lnum <= 1)
889 break;
890 lnum--;
891 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100892 // Adjust col to indicate that we're continuing from prev/next line.
893 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100894 }
895}
896
897/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000898 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
899 */
900 static int
901prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
902{
903 int i;
904
905 for (i = 0; i < len; i++)
906 if (types_or_ids[i] == type_or_id)
907 return TRUE;
908
909 return FALSE;
910}
911
912/*
913 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
914 * If 'prop_types' is not NULL, then return only the text properties with
915 * matching property type in the 'prop_types' array.
916 * If 'prop_ids' is not NULL, then return only the text properties with
917 * an identifier in the 'props_ids' array.
918 * If 'add_lnum' is TRUE, then add the line number also to the text property
919 * dictionary.
920 */
921 static void
922get_props_in_line(
923 buf_T *buf,
924 linenr_T lnum,
925 int *prop_types,
926 int prop_types_len,
927 int *prop_ids,
928 int prop_ids_len,
929 list_T *retlist,
930 int add_lnum)
931{
932 char_u *text = ml_get_buf(buf, lnum, FALSE);
933 size_t textlen = STRLEN(text) + 1;
934 int count;
935 int i;
936 textprop_T prop;
937
938 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
939 for (i = 0; i < count; ++i)
940 {
941 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
942 sizeof(textprop_T));
943 if ((prop_types == NULL
944 || prop_type_or_id_in_list(prop_types, prop_types_len,
945 prop.tp_type))
946 && (prop_ids == NULL ||
947 prop_type_or_id_in_list(prop_ids, prop_ids_len,
948 prop.tp_id)))
949 {
950 dict_T *d = dict_alloc();
951
952 if (d == NULL)
953 break;
954 prop_fill_dict(d, &prop, buf);
955 if (add_lnum)
956 dict_add_number(d, "lnum", lnum);
957 list_append_dict(retlist, d);
958 }
959 }
960}
961
962/*
963 * Convert a List of property type names into an array of property type
964 * identifiers. Returns a pointer to the allocated array. Returns NULL on
965 * error. 'num_types' is set to the number of returned property types.
966 */
967 static int *
968get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
969{
970 int *prop_types;
971 listitem_T *li;
972 int i;
973 char_u *name;
974 proptype_T *type;
975
976 *num_types = 0;
977
978 prop_types = ALLOC_MULT(int, list_len(l));
979 if (prop_types == NULL)
980 return NULL;
981
982 i = 0;
983 FOR_ALL_LIST_ITEMS(l, li)
984 {
985 if (li->li_tv.v_type != VAR_STRING)
986 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000987 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000988 goto errret;
989 }
990 name = li->li_tv.vval.v_string;
991 if (name == NULL)
992 goto errret;
993
994 type = lookup_prop_type(name, buf);
995 if (type == NULL)
996 goto errret;
997 prop_types[i++] = type->pt_id;
998 }
999
1000 *num_types = i;
1001 return prop_types;
1002
1003errret:
1004 VIM_CLEAR(prop_types);
1005 return NULL;
1006}
1007
1008/*
1009 * Convert a List of property identifiers into an array of property
1010 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1011 * error. 'num_ids' is set to the number of returned property identifiers.
1012 */
1013 static int *
1014get_prop_ids_from_list(list_T *l, int *num_ids)
1015{
1016 int *prop_ids;
1017 listitem_T *li;
1018 int i;
1019 int id;
1020 int error;
1021
1022 *num_ids = 0;
1023
1024 prop_ids = ALLOC_MULT(int, list_len(l));
1025 if (prop_ids == NULL)
1026 return NULL;
1027
1028 i = 0;
1029 FOR_ALL_LIST_ITEMS(l, li)
1030 {
1031 error = FALSE;
1032 id = tv_get_number_chk(&li->li_tv, &error);
1033 if (error)
1034 goto errret;
1035
1036 prop_ids[i++] = id;
1037 }
1038
1039 *num_ids = i;
1040 return prop_ids;
1041
1042errret:
1043 VIM_CLEAR(prop_ids);
1044 return NULL;
1045}
1046
1047/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001048 * prop_list({lnum} [, {bufnr}])
1049 */
1050 void
1051f_prop_list(typval_T *argvars, typval_T *rettv)
1052{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001053 linenr_T lnum;
1054 linenr_T start_lnum;
1055 linenr_T end_lnum;
1056 buf_T *buf = curbuf;
1057 int add_lnum = FALSE;
1058 int *prop_types = NULL;
1059 int prop_types_len = 0;
1060 int *prop_ids = NULL;
1061 int prop_ids_len = 0;
1062 list_T *l;
1063 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001064
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001065 if (in_vim9script()
1066 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001067 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001068 return;
1069
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001070 if (rettv_list_alloc(rettv) != OK)
1071 return;
1072
1073 // default: get text properties on current line
1074 start_lnum = tv_get_number(&argvars[0]);
1075 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001076 if (argvars[1].v_type != VAR_UNKNOWN)
1077 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001078 dict_T *d;
1079
1080 if (argvars[1].v_type != VAR_DICT)
1081 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001082 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001083 return;
1084 }
1085 d = argvars[1].vval.v_dict;
1086
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001087 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1088 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001089
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001090 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001091 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001092 if (di->di_tv.v_type != VAR_NUMBER)
1093 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001094 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001095 return;
1096 }
1097 end_lnum = tv_get_number(&di->di_tv);
1098 if (end_lnum < 0)
1099 // negative end_lnum is used as an offset from the last buffer
1100 // line
1101 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1102 else if (end_lnum > buf->b_ml.ml_line_count)
1103 end_lnum = buf->b_ml.ml_line_count;
1104 add_lnum = TRUE;
1105 }
1106 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1107 {
1108 if (di->di_tv.v_type != VAR_LIST)
1109 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001110 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001111 return;
1112 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001113
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001114 l = di->di_tv.vval.v_list;
1115 if (l != NULL && list_len(l) > 0)
1116 {
1117 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1118 if (prop_types == NULL)
1119 return;
1120 }
1121 }
1122 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1123 {
1124 if (di->di_tv.v_type != VAR_LIST)
1125 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001126 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001127 goto errret;
1128 }
1129
1130 l = di->di_tv.vval.v_list;
1131 if (l != NULL && list_len(l) > 0)
1132 {
1133 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1134 if (prop_ids == NULL)
1135 goto errret;
1136 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001137 }
1138 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001139 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1140 || end_lnum < 1 || end_lnum < start_lnum)
1141 emsg(_(e_invalid_range));
1142 else
1143 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1144 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1145 prop_ids, prop_ids_len,
1146 rettv->vval.v_list, add_lnum);
1147
1148errret:
1149 VIM_CLEAR(prop_types);
1150 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001151}
1152
1153/*
1154 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1155 */
1156 void
1157f_prop_remove(typval_T *argvars, typval_T *rettv)
1158{
1159 linenr_T start = 1;
1160 linenr_T end = 0;
1161 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001162 linenr_T first_changed = 0;
1163 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001164 dict_T *dict;
1165 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001166 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001167 int id = -1;
1168 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001169 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001170
1171 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001172
1173 if (in_vim9script()
1174 && (check_for_dict_arg(argvars, 0) == FAIL
1175 || check_for_opt_number_arg(argvars, 1) == FAIL
1176 || (argvars[1].v_type != VAR_UNKNOWN
1177 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1178 return;
1179
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001180 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1181 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001182 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001183 return;
1184 }
1185
1186 if (argvars[1].v_type != VAR_UNKNOWN)
1187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001188 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001189 end = start;
1190 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001191 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001192 if (start < 1 || end < 1)
1193 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001194 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001195 return;
1196 }
1197 }
1198
1199 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001200 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1201 return;
1202 if (buf->b_ml.ml_mfp == NULL)
1203 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001204
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001205 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001206
1207 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01001208 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001209 if (dict_find(dict, (char_u *)"type", -1))
1210 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01001211 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001212 proptype_T *type = lookup_prop_type(name, buf);
1213
1214 if (type == NULL)
1215 return;
1216 type_id = type->pt_id;
1217 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001218 both = dict_get_bool(dict, (char_u *)"both", FALSE);
1219
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001220 if (id == -1 && type_id == -1)
1221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001223 return;
1224 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001225 if (both && (id == -1 || type_id == -1))
1226 {
1227 emsg(_("E860: Need 'id' and 'type' with 'both'"));
1228 return;
1229 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001230
1231 if (end == 0)
1232 end = buf->b_ml.ml_line_count;
1233 for (lnum = start; lnum <= end; ++lnum)
1234 {
1235 char_u *text;
1236 size_t len;
1237
1238 if (lnum > buf->b_ml.ml_line_count)
1239 break;
1240 text = ml_get_buf(buf, lnum, FALSE);
1241 len = STRLEN(text) + 1;
1242 if ((size_t)buf->b_ml.ml_line_len > len)
1243 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001244 static textprop_T textprop; // static because of alignment
1245 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001246
1247 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1248 / sizeof(textprop_T); ++idx)
1249 {
1250 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1251 + idx * sizeof(textprop_T);
1252 size_t taillen;
1253
1254 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001255 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1256 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001257 {
1258 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1259 {
1260 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1261
1262 // need to allocate the line to be able to change it
1263 if (newptr == NULL)
1264 return;
1265 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1266 buf->b_ml.ml_line_len);
1267 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001268 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1269
1270 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001271 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001272 }
1273
1274 taillen = buf->b_ml.ml_line_len - len
1275 - (idx + 1) * sizeof(textprop_T);
1276 if (taillen > 0)
1277 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1278 taillen);
1279 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1280 --idx;
1281
Bram Moolenaar965c0442021-05-17 00:22:06 +02001282 if (first_changed == 0)
1283 first_changed = lnum;
1284 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001285 ++rettv->vval.v_number;
1286 if (!do_all)
1287 break;
1288 }
1289 }
1290 }
1291 }
Bram Moolenaar965c0442021-05-17 00:22:06 +02001292 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001293 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001294 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1295 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001296 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001297}
1298
1299/*
1300 * Common for f_prop_type_add() and f_prop_type_change().
1301 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001302 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001303prop_type_set(typval_T *argvars, int add)
1304{
1305 char_u *name;
1306 buf_T *buf = NULL;
1307 dict_T *dict;
1308 dictitem_T *di;
1309 proptype_T *prop;
1310
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001311 if (in_vim9script()
1312 && (check_for_string_arg(argvars, 0) == FAIL
1313 || check_for_dict_arg(argvars, 1) == FAIL))
1314 return;
1315
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001316 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001317 if (*name == NUL)
1318 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001319 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001320 return;
1321 }
1322
1323 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1324 return;
1325 dict = argvars[1].vval.v_dict;
1326
1327 prop = find_prop(name, buf);
1328 if (add)
1329 {
1330 hashtab_T **htp;
1331
1332 if (prop != NULL)
1333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001334 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001335 return;
1336 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001337 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001338 if (prop == NULL)
1339 return;
1340 STRCPY(prop->pt_name, name);
1341 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001342 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001343 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1344 if (*htp == NULL)
1345 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001346 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001347 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001348 {
1349 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001350 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001351 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001352 hash_init(*htp);
1353 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001354 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001355 }
1356 else
1357 {
1358 if (prop == NULL)
1359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001360 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001361 return;
1362 }
1363 }
1364
1365 if (dict != NULL)
1366 {
1367 di = dict_find(dict, (char_u *)"highlight", -1);
1368 if (di != NULL)
1369 {
1370 char_u *highlight;
1371 int hl_id = 0;
1372
Bram Moolenaar8f667172018-12-14 15:38:31 +01001373 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001374 if (highlight != NULL && *highlight != NUL)
1375 hl_id = syn_name2id(highlight);
1376 if (hl_id <= 0)
1377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001378 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001379 highlight == NULL ? (char_u *)"" : highlight);
1380 return;
1381 }
1382 prop->pt_hl_id = hl_id;
1383 }
1384
Bram Moolenaar58187f12019-05-05 16:33:47 +02001385 di = dict_find(dict, (char_u *)"combine", -1);
1386 if (di != NULL)
1387 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001388 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001389 prop->pt_flags |= PT_FLAG_COMBINE;
1390 else
1391 prop->pt_flags &= ~PT_FLAG_COMBINE;
1392 }
1393
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001394 di = dict_find(dict, (char_u *)"priority", -1);
1395 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001396 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001397
1398 di = dict_find(dict, (char_u *)"start_incl", -1);
1399 if (di != NULL)
1400 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001401 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001402 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1403 else
1404 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1405 }
1406
1407 di = dict_find(dict, (char_u *)"end_incl", -1);
1408 if (di != NULL)
1409 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001410 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001411 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1412 else
1413 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1414 }
1415 }
1416}
1417
1418/*
1419 * prop_type_add({name}, {props})
1420 */
1421 void
1422f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1423{
1424 prop_type_set(argvars, TRUE);
1425}
1426
1427/*
1428 * prop_type_change({name}, {props})
1429 */
1430 void
1431f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1432{
1433 prop_type_set(argvars, FALSE);
1434}
1435
1436/*
1437 * prop_type_delete({name} [, {bufnr}])
1438 */
1439 void
1440f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1441{
1442 char_u *name;
1443 buf_T *buf = NULL;
1444 hashitem_T *hi;
1445
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001446 if (in_vim9script()
1447 && (check_for_string_arg(argvars, 0) == FAIL
1448 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1449 return;
1450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001451 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001452 if (*name == NUL)
1453 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001454 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001455 return;
1456 }
1457
1458 if (argvars[1].v_type != VAR_UNKNOWN)
1459 {
1460 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1461 return;
1462 }
1463
1464 hi = find_prop_hi(name, buf);
1465 if (hi != NULL)
1466 {
1467 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001468 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001469
1470 if (buf == NULL)
1471 ht = global_proptypes;
1472 else
1473 ht = buf->b_proptypes;
1474 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001475 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001476 }
1477}
1478
1479/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001480 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001481 */
1482 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001483f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001484{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001485 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001486
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001487 if (in_vim9script()
1488 && (check_for_string_arg(argvars, 0) == FAIL
1489 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1490 return;
1491
1492 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001493 if (*name == NUL)
1494 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001495 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001496 return;
1497 }
1498 if (rettv_dict_alloc(rettv) == OK)
1499 {
1500 proptype_T *prop = NULL;
1501 buf_T *buf = NULL;
1502
1503 if (argvars[1].v_type != VAR_UNKNOWN)
1504 {
1505 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1506 return;
1507 }
1508
1509 prop = find_prop(name, buf);
1510 if (prop != NULL)
1511 {
1512 dict_T *d = rettv->vval.v_dict;
1513
1514 if (prop->pt_hl_id > 0)
1515 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1516 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001517 dict_add_number(d, "combine",
1518 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001519 dict_add_number(d, "start_incl",
1520 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1521 dict_add_number(d, "end_incl",
1522 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1523 if (buf != NULL)
1524 dict_add_number(d, "bufnr", buf->b_fnum);
1525 }
1526 }
1527}
1528
1529 static void
1530list_types(hashtab_T *ht, list_T *l)
1531{
1532 long todo;
1533 hashitem_T *hi;
1534
1535 todo = (long)ht->ht_used;
1536 for (hi = ht->ht_array; todo > 0; ++hi)
1537 {
1538 if (!HASHITEM_EMPTY(hi))
1539 {
1540 proptype_T *prop = HI2PT(hi);
1541
1542 list_append_string(l, prop->pt_name, -1);
1543 --todo;
1544 }
1545 }
1546}
1547
1548/*
1549 * prop_type_list([{bufnr}])
1550 */
1551 void
1552f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1553{
1554 buf_T *buf = NULL;
1555
1556 if (rettv_list_alloc(rettv) == OK)
1557 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001558 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1559 return;
1560
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001561 if (argvars[0].v_type != VAR_UNKNOWN)
1562 {
1563 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1564 return;
1565 }
1566 if (buf == NULL)
1567 {
1568 if (global_proptypes != NULL)
1569 list_types(global_proptypes, rettv->vval.v_list);
1570 }
1571 else if (buf->b_proptypes != NULL)
1572 list_types(buf->b_proptypes, rettv->vval.v_list);
1573 }
1574}
1575
1576/*
1577 * Free all property types in "ht".
1578 */
1579 static void
1580clear_ht_prop_types(hashtab_T *ht)
1581{
1582 long todo;
1583 hashitem_T *hi;
1584
1585 if (ht == NULL)
1586 return;
1587
1588 todo = (long)ht->ht_used;
1589 for (hi = ht->ht_array; todo > 0; ++hi)
1590 {
1591 if (!HASHITEM_EMPTY(hi))
1592 {
1593 proptype_T *prop = HI2PT(hi);
1594
1595 vim_free(prop);
1596 --todo;
1597 }
1598 }
1599
1600 hash_clear(ht);
1601 vim_free(ht);
1602}
1603
1604#if defined(EXITFREE) || defined(PROTO)
1605/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001606 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001607 */
1608 void
1609clear_global_prop_types(void)
1610{
1611 clear_ht_prop_types(global_proptypes);
1612 global_proptypes = NULL;
1613}
1614#endif
1615
1616/*
1617 * Free all property types for "buf".
1618 */
1619 void
1620clear_buf_prop_types(buf_T *buf)
1621{
1622 clear_ht_prop_types(buf->b_proptypes);
1623 buf->b_proptypes = NULL;
1624}
1625
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001626// Struct used to return two values from adjust_prop().
1627typedef struct
1628{
1629 int dirty; // if the property was changed
1630 int can_drop; // whether after this change, the prop may be removed
1631} adjustres_T;
1632
1633/*
1634 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1635 *
1636 * Note that "col" is zero-based, while tp_col is one-based.
1637 * Only for the current buffer.
1638 * "flags" can have:
1639 * APC_SUBSTITUTE: Text is replaced, not inserted.
1640 */
1641 static adjustres_T
1642adjust_prop(
1643 textprop_T *prop,
1644 colnr_T col,
1645 int added,
1646 int flags)
1647{
1648 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1649 int start_incl = (pt != NULL
1650 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1651 || (flags & APC_SUBSTITUTE);
1652 int end_incl = (pt != NULL
1653 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1654 // Do not drop zero-width props if they later can increase in
1655 // size.
1656 int droppable = !(start_incl || end_incl);
1657 adjustres_T res = {TRUE, FALSE};
1658
1659 if (added > 0)
1660 {
1661 if (col + 1 <= prop->tp_col
1662 - (start_incl || (prop->tp_len == 0 && end_incl)))
1663 // Change is entirely before the text property: Only shift
1664 prop->tp_col += added;
1665 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1666 // Insertion was inside text property
1667 prop->tp_len += added;
1668 }
1669 else if (prop->tp_col > col + 1)
1670 {
1671 if (prop->tp_col + added < col + 1)
1672 {
1673 prop->tp_len += (prop->tp_col - 1 - col) + added;
1674 prop->tp_col = col + 1;
1675 if (prop->tp_len <= 0)
1676 {
1677 prop->tp_len = 0;
1678 res.can_drop = droppable;
1679 }
1680 }
1681 else
1682 prop->tp_col += added;
1683 }
1684 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1685 {
1686 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1687
1688 prop->tp_len += after > 0 ? added + after : added;
1689 res.can_drop = prop->tp_len <= 0 && droppable;
1690 }
1691 else
1692 res.dirty = FALSE;
1693
1694 return res;
1695}
1696
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001697/*
1698 * Adjust the columns of text properties in line "lnum" after position "col" to
1699 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001700 * Note that "col" is zero-based, while tp_col is one-based.
1701 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001702 * "flags" can have:
1703 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1704 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001705 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001706 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001707 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001708 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001709adjust_prop_columns(
1710 linenr_T lnum,
1711 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001712 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001713 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001714{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001715 int proplen;
1716 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001717 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001718 int ri, wi;
1719 size_t textlen;
1720
1721 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001722 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001723
1724 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1725 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001726 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001727 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001728
Bram Moolenaar196d1572019-01-02 23:47:18 +01001729 wi = 0; // write index
1730 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001731 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001732 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001733 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001734
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001735 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1736 res = adjust_prop(&prop, col, bytes_added, flags);
1737 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001738 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001739 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001740 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1741 && u_savesub(lnum) == FAIL)
1742 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001743 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001744
1745 // u_savesub() may have updated curbuf->b_ml, fetch it again
1746 if (curbuf->b_ml.ml_line_lnum != lnum)
1747 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001748 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001749 if (res.can_drop)
1750 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001751 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001752 ++wi;
1753 }
1754 if (dirty)
1755 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001756 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1757
1758 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1759 curbuf->b_ml.ml_line_ptr =
1760 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001761 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001762 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001763 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001764 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001765}
1766
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001767/*
1768 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001769 * "lnum_props" is the line that has the properties from before the split.
1770 * "lnum_top" is the top line.
1771 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001772 * "deleted" is the number of bytes deleted.
1773 */
1774 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001775adjust_props_for_split(
1776 linenr_T lnum_props,
1777 linenr_T lnum_top,
1778 int kept,
1779 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001780{
1781 char_u *props;
1782 int count;
1783 garray_T prevprop;
1784 garray_T nextprop;
1785 int i;
1786 int skipped = kept + deleted;
1787
1788 if (!curbuf->b_has_textprop)
1789 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001790
1791 // Get the text properties from "lnum_props".
1792 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001793 ga_init2(&prevprop, sizeof(textprop_T), 10);
1794 ga_init2(&nextprop, sizeof(textprop_T), 10);
1795
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001796 // Keep the relevant ones in the first line, reducing the length if needed.
1797 // Copy the ones that include the split to the second line.
1798 // Move the ones after the split to the second line.
1799 for (i = 0; i < count; ++i)
1800 {
1801 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001802 proptype_T *pt;
1803 int start_incl, end_incl;
1804 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001805
1806 // copy the prop to an aligned structure
1807 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1808
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001809 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1810 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1811 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1812 cont_prev = prop.tp_col + !start_incl <= kept;
1813 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1814
1815 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001816 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001817 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1818
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001819 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001820 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001821 if (p->tp_col + p->tp_len >= kept)
1822 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001823 if (cont_next)
1824 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001825 }
1826
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001827 // Only add the property to the next line if the length is bigger than
1828 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001829 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001830 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001831 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001832 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001833 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001834 if (p->tp_col > skipped)
1835 p->tp_col -= skipped - 1;
1836 else
1837 {
1838 p->tp_len -= skipped - p->tp_col;
1839 p->tp_col = 1;
1840 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001841 if (cont_prev)
1842 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001843 }
1844 }
1845
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001846 set_text_props(lnum_top, prevprop.ga_data,
1847 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001848 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001849 set_text_props(lnum_top + 1, nextprop.ga_data,
1850 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001851 ga_clear(&nextprop);
1852}
1853
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001854/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001855 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001856 */
1857 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001858prepend_joined_props(
1859 char_u *new_props,
1860 int propcount,
1861 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001862 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001863 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001864 long col,
1865 int removed)
1866{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001867 char_u *props;
1868 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1869 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001870
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001871 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001872 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001873 textprop_T prop;
1874 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001875
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001876 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1877 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1878
1879 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001880 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001881
1882 if (add_all || end)
1883 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1884 &prop, sizeof(prop));
1885 else
1886 {
1887 int j;
1888 int found = FALSE;
1889
1890 // Search for continuing prop.
1891 for (j = *props_remaining; j < propcount; ++j)
1892 {
1893 textprop_T op;
1894
1895 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1896 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1897 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001898 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001899 found = TRUE;
1900 op.tp_len += op.tp_col - prop.tp_col;
1901 op.tp_col = prop.tp_col;
1902 // Start/end is taken care of when deleting joined lines
1903 op.tp_flags = prop.tp_flags;
1904 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1905 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001906 }
1907 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001908 if (!found)
1909 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001910 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001911 }
1912}
1913
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001914#endif // FEAT_PROP_POPUP