blob: 5c07195f4f94fea8c74b20a86fc82e64fc7722e2 [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
153f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
154{
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
177 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
178 curbuf, &argvars[2]);
179}
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
184 * the buffer 'buf' and assign identifier 'id'.
185 */
186 static int
187prop_add_one(
188 buf_T *buf,
189 char_u *type_name,
190 int id,
191 linenr_T start_lnum,
192 linenr_T end_lnum,
193 colnr_T start_col,
194 colnr_T end_col)
195{
196 proptype_T *type;
197 linenr_T lnum;
198 int proplen;
199 char_u *props = NULL;
200 char_u *newprops;
201 size_t textlen;
202 char_u *newtext;
203 int i;
204 textprop_T tmp_prop;
205
206 type = lookup_prop_type(type_name, buf);
207 if (type == NULL)
208 return FAIL;
209
210 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
211 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000212 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200213 return FAIL;
214 }
215 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
216 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000217 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200218 return FAIL;
219 }
220
221 if (buf->b_ml.ml_mfp == NULL)
222 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000223 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200224 return FAIL;
225 }
226
227 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
228 {
229 colnr_T col; // start column
230 long length; // in bytes
231
232 // Fetch the line to get the ml_line_len field updated.
233 proplen = get_text_props(buf, lnum, &props, TRUE);
234 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
235
236 if (lnum == start_lnum)
237 col = start_col;
238 else
239 col = 1;
240 if (col - 1 > (colnr_T)textlen)
241 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000242 semsg(_(e_invalid_column_number_nr), (long)start_col);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200243 return FAIL;
244 }
245
246 if (lnum == end_lnum)
247 length = end_col - col;
248 else
249 length = (int)textlen - col + 1;
250 if (length > (long)textlen)
251 length = (int)textlen; // can include the end-of-line
252 if (length < 0)
253 length = 0; // zero-width property
254
255 // Allocate the new line with space for the new property.
256 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
257 if (newtext == NULL)
258 return FAIL;
259 // Copy the text, including terminating NUL.
260 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
261
262 // Find the index where to insert the new property.
263 // Since the text properties are not aligned properly when stored with
264 // the text, we need to copy them as bytes before using it as a struct.
265 for (i = 0; i < proplen; ++i)
266 {
267 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
268 sizeof(textprop_T));
269 if (tmp_prop.tp_col >= col)
270 break;
271 }
272 newprops = newtext + textlen;
273 if (i > 0)
274 mch_memmove(newprops, props, sizeof(textprop_T) * i);
275
276 tmp_prop.tp_col = col;
277 tmp_prop.tp_len = length;
278 tmp_prop.tp_id = id;
279 tmp_prop.tp_type = type->pt_id;
280 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
281 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
282 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
283 sizeof(textprop_T));
284
285 if (i < proplen)
286 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
287 props + i * sizeof(textprop_T),
288 sizeof(textprop_T) * (proplen - i));
289
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100290 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200291 vim_free(buf->b_ml.ml_line_ptr);
292 buf->b_ml.ml_line_ptr = newtext;
293 buf->b_ml.ml_line_len += sizeof(textprop_T);
294 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
295 }
296
297 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
298 return OK;
299}
300
301/*
302 * prop_add_list()
303 * First argument specifies the text property:
304 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
305 * Second argument is a List where each item is a List with the following
306 * entries: [lnum, start_col, end_col]
307 */
308 void
309f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
310{
311 dict_T *dict;
312 char_u *type_name;
313 buf_T *buf = curbuf;
314 int id = 0;
315 listitem_T *li;
316 list_T *pos_list;
317 linenr_T start_lnum;
318 colnr_T start_col;
319 linenr_T end_lnum;
320 colnr_T end_col;
321 int error = FALSE;
322
323 if (check_for_dict_arg(argvars, 0) == FAIL
324 || check_for_list_arg(argvars, 1) == FAIL)
325 return;
326
327 if (argvars[1].vval.v_list == NULL)
328 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000329 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200330 return;
331 }
332
333 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100334 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200335 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000336 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200337 return;
338 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100339 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200340
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100341 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100342 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200343
344 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
345 return;
346
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100347 // This must be done _before_ we start adding properties because property
348 // changes trigger buffer (memline) reorganisation, which needs this flag
349 // to be correctly set.
350 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200351 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
352 {
353 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
354 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000355 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200356 return;
357 }
358
359 pos_list = li->li_tv.vval.v_list;
360 start_lnum = list_find_nr(pos_list, 0L, &error);
361 start_col = list_find_nr(pos_list, 1L, &error);
362 end_lnum = list_find_nr(pos_list, 2L, &error);
363 end_col = list_find_nr(pos_list, 3L, &error);
364 if (error || start_lnum <= 0 || start_col <= 0
365 || end_lnum <= 0 || end_col <= 0)
366 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000367 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200368 return;
369 }
370 if (prop_add_one(buf, type_name, id, start_lnum, end_lnum,
371 start_col, end_col) == FAIL)
372 return;
373 }
374
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200375 redraw_buf_later(buf, VALID);
376}
377
378/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200379 * Shared between prop_add() and popup_create().
380 * "dict_arg" is the function argument of a dict containing "bufnr".
381 * it is NULL for popup_create().
382 */
383 void
384prop_add_common(
385 linenr_T start_lnum,
386 colnr_T start_col,
387 dict_T *dict,
388 buf_T *default_buf,
389 typval_T *dict_arg)
390{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200391 linenr_T end_lnum;
392 colnr_T end_col;
393 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200394 buf_T *buf = default_buf;
395 int id = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100396
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100397 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100398 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000399 emsg(_(e_missing_property_type_name));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100400 return;
401 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100402 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100403
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100404 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100405 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100406 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100407 if (end_lnum < start_lnum)
408 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000409 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100410 return;
411 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100412 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100413 else
414 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100415
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100416 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100417 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100418 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100419
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100420 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100421 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000422 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100423 return;
424 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100425 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100426 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100427 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100428 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100429 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100430 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100431 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000432 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100433 return;
434 }
435 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100436 else if (start_lnum == end_lnum)
437 end_col = start_col;
438 else
439 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100440
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100441 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100442 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100443
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200444 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100445 return;
446
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100447 // This must be done _before_ we add the property because property changes
448 // trigger buffer (memline) reorganisation, which needs this flag to be
449 // correctly set.
450 buf->b_has_textprop = TRUE; // this is never reset
451
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200452 prop_add_one(buf, type_name, id, start_lnum, end_lnum, start_col, end_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100453
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200454 redraw_buf_later(buf, VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100455}
456
457/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100458 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100459 * Returns the number of text properties and, when non-zero, a pointer to the
460 * first one in "props" (note that it is not aligned, therefore the char_u
461 * pointer).
462 */
463 int
464get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
465{
466 char_u *text;
467 size_t textlen;
468 size_t proplen;
469
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100470 // Be quick when no text property types have been defined or the buffer,
471 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200472 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473 return 0;
474
475 // Fetch the line to get the ml_line_len field updated.
476 text = ml_get_buf(buf, lnum, will_change);
477 textlen = STRLEN(text) + 1;
478 proplen = buf->b_ml.ml_line_len - textlen;
479 if (proplen % sizeof(textprop_T) != 0)
480 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000481 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100482 return 0;
483 }
484 if (proplen > 0)
485 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100486 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100487}
488
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200489/**
490 * Return the number of text properties on line "lnum" in the current buffer.
491 * When "only_starting" is true only text properties starting in this line will
492 * be considered.
493 */
494 int
495count_props(linenr_T lnum, int only_starting)
496{
497 char_u *props;
498 int proplen = get_text_props(curbuf, lnum, &props, 0);
499 int result = proplen;
500 int i;
501 textprop_T prop;
502
503 if (only_starting)
504 for (i = 0; i < proplen; ++i)
505 {
506 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
507 if (prop.tp_flags & TP_FLAG_CONT_PREV)
508 --result;
509 }
510 return result;
511}
512
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100513/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200514 * Find text property "type_id" in the visible lines of window "wp".
515 * Match "id" when it is > 0.
516 * Returns FAIL when not found.
517 */
518 int
519find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
520 linenr_T *found_lnum)
521{
522 linenr_T lnum;
523 char_u *props;
524 int count;
525 int i;
526
527 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100528 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200529 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
530 {
531 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
532 for (i = 0; i < count; ++i)
533 {
534 mch_memmove(prop, props + i * sizeof(textprop_T),
535 sizeof(textprop_T));
536 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
537 {
538 *found_lnum = lnum;
539 return OK;
540 }
541 }
542 }
543 return FAIL;
544}
545
546/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100547 * Set the text properties for line "lnum" to "props" with length "len".
548 * If "len" is zero text properties are removed, "props" is not used.
549 * Any existing text properties are dropped.
550 * Only works for the current buffer.
551 */
552 static void
553set_text_props(linenr_T lnum, char_u *props, int len)
554{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100555 char_u *text;
556 char_u *newtext;
557 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100558
559 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100560 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100561 newtext = alloc(textlen + len);
562 if (newtext == NULL)
563 return;
564 mch_memmove(newtext, text, textlen);
565 if (len > 0)
566 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100567 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100568 vim_free(curbuf->b_ml.ml_line_ptr);
569 curbuf->b_ml.ml_line_ptr = newtext;
570 curbuf->b_ml.ml_line_len = textlen + len;
571 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
572}
573
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100574 static proptype_T *
575find_type_by_id(hashtab_T *ht, int id)
576{
577 long todo;
578 hashitem_T *hi;
579
580 if (ht == NULL)
581 return NULL;
582
583 // TODO: Make this faster by keeping a list of types sorted on ID and use
584 // a binary search.
585
586 todo = (long)ht->ht_used;
587 for (hi = ht->ht_array; todo > 0; ++hi)
588 {
589 if (!HASHITEM_EMPTY(hi))
590 {
591 proptype_T *prop = HI2PT(hi);
592
593 if (prop->pt_id == id)
594 return prop;
595 --todo;
596 }
597 }
598 return NULL;
599}
600
601/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100602 * Fill 'dict' with text properties in 'prop'.
603 */
604 static void
605prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
606{
607 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200608 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100609
610 dict_add_number(dict, "col", prop->tp_col);
611 dict_add_number(dict, "length", prop->tp_len);
612 dict_add_number(dict, "id", prop->tp_id);
613 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
614 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200615
616 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
617 if (pt == NULL)
618 {
619 pt = find_type_by_id(global_proptypes, prop->tp_type);
620 buflocal = FALSE;
621 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100622 if (pt != NULL)
623 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200624
625 if (buflocal)
626 dict_add_number(dict, "type_bufnr", buf->b_fnum);
627 else
628 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100629}
630
631/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100632 * Find a property type by ID in "buf" or globally.
633 * Returns NULL if not found.
634 */
635 proptype_T *
636text_prop_type_by_id(buf_T *buf, int id)
637{
638 proptype_T *type;
639
640 type = find_type_by_id(buf->b_proptypes, id);
641 if (type == NULL)
642 type = find_type_by_id(global_proptypes, id);
643 return type;
644}
645
646/*
647 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
648 */
649 void
650f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
651{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200652 linenr_T start;
653 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100654 linenr_T lnum;
655 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100656 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100657
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200658 if (in_vim9script()
659 && (check_for_number_arg(argvars, 0) == FAIL
660 || check_for_opt_number_arg(argvars, 1) == FAIL
661 || (argvars[1].v_type != VAR_UNKNOWN
662 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
663 return;
664
665 start = tv_get_number(&argvars[0]);
666 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100667 if (argvars[1].v_type != VAR_UNKNOWN)
668 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100669 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100670 if (argvars[2].v_type != VAR_UNKNOWN)
671 {
672 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
673 return;
674 }
675 }
676 if (start < 1 || end < 1)
677 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200678 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100679 return;
680 }
681
682 for (lnum = start; lnum <= end; ++lnum)
683 {
684 char_u *text;
685 size_t len;
686
687 if (lnum > buf->b_ml.ml_line_count)
688 break;
689 text = ml_get_buf(buf, lnum, FALSE);
690 len = STRLEN(text) + 1;
691 if ((size_t)buf->b_ml.ml_line_len > len)
692 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100693 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100694 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
695 {
696 char_u *newtext = vim_strsave(text);
697
698 // need to allocate the line now
699 if (newtext == NULL)
700 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100701 if (buf->b_ml.ml_flags & ML_ALLOCATED)
702 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100703 buf->b_ml.ml_line_ptr = newtext;
704 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
705 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100706 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100707 }
708 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100709 if (did_clear)
710 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100711}
712
713/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100714 * prop_find({props} [, {direction}])
715 */
716 void
717f_prop_find(typval_T *argvars, typval_T *rettv)
718{
719 pos_T *cursor = &curwin->w_cursor;
720 dict_T *dict;
721 buf_T *buf = curbuf;
722 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200723 int lnum_start;
724 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100725 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200726 int id = 0;
727 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200728 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100729 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200730 int lnum = -1;
731 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100732 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100733 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100734
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200735 if (in_vim9script()
736 && (check_for_dict_arg(argvars, 0) == FAIL
737 || check_for_opt_string_arg(argvars, 1) == FAIL))
738 return;
739
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100740 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
741 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000742 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100743 return;
744 }
745 dict = argvars[0].vval.v_dict;
746
747 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
748 return;
749 if (buf->b_ml.ml_mfp == NULL)
750 return;
751
752 if (argvars[1].v_type != VAR_UNKNOWN)
753 {
754 char_u *dir_s = tv_get_string(&argvars[1]);
755
756 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100757 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100758 else if (*dir_s != 'f')
759 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000760 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100761 return;
762 }
763 }
764
765 di = dict_find(dict, (char_u *)"lnum", -1);
766 if (di != NULL)
767 lnum = tv_get_number(&di->di_tv);
768
769 di = dict_find(dict, (char_u *)"col", -1);
770 if (di != NULL)
771 col = tv_get_number(&di->di_tv);
772
773 if (lnum == -1)
774 {
775 lnum = cursor->lnum;
776 col = cursor->col + 1;
777 }
778 else if (col == -1)
779 col = 1;
780
781 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
782 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200783 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100784 return;
785 }
786
Bram Moolenaard61efa52022-07-23 09:52:04 +0100787 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100788
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100789 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200790 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100791 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200792 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200793 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100794 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100795 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100796 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100797 proptype_T *type = lookup_prop_type(name, buf);
798
799 if (type == NULL)
800 return;
801 type_id = type->pt_id;
802 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100803 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200804 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100805 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000806 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100807 return;
808 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200809 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100810 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000811 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100812 return;
813 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100814
815 lnum_start = lnum;
816
817 if (rettv_dict_alloc(rettv) == FAIL)
818 return;
819
820 while (1)
821 {
822 char_u *text = ml_get_buf(buf, lnum, FALSE);
823 size_t textlen = STRLEN(text) + 1;
824 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200825 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100826 int i;
827 textprop_T prop;
828 int prop_start;
829 int prop_end;
830
LemonBoy9bd3ce22022-04-18 21:54:02 +0100831 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100832 {
833 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100834 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100835
LemonBoy9bd3ce22022-04-18 21:54:02 +0100836 // For the very first line try to find the first property before or
837 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100838 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100839 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100840 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100841 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100842 if (prop.tp_col > col)
843 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100844 }
845 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
846 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100847 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100848 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200849 : (id_found && prop.tp_id == id)
850 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100851 {
852 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100853 if (lnum_start == lnum
854 && col >= prop.tp_col
855 && (col <= prop.tp_col + prop.tp_len
856 - (prop.tp_len != 0)))
857 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100858
LemonBoy9bd3ce22022-04-18 21:54:02 +0100859 // The property was not continued from last line, it starts on
860 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100861 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100862 // The property does not continue on the next line, it ends on
863 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100864 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100865 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100866 seen_end = 1;
867
868 // Skip lines without the start flag.
869 if (!prop_start)
870 {
871 // Always search backwards for start when search started
872 // on a prop and we're not skipping.
873 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +0100874 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200875 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100876 }
877
878 // If skipstart is true, skip the prop at start pos (even if
879 // continued from another line).
880 if (start_pos_has_prop && skipstart && !seen_end)
881 {
882 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200883 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100884 }
885
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100886 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
887 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
888
889 return;
890 }
891 }
892
893 if (dir > 0)
894 {
895 if (lnum >= buf->b_ml.ml_line_count)
896 break;
897 lnum++;
898 }
899 else
900 {
901 if (lnum <= 1)
902 break;
903 lnum--;
904 }
905 }
906}
907
908/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000909 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
910 */
911 static int
912prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
913{
914 int i;
915
916 for (i = 0; i < len; i++)
917 if (types_or_ids[i] == type_or_id)
918 return TRUE;
919
920 return FALSE;
921}
922
923/*
924 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
925 * If 'prop_types' is not NULL, then return only the text properties with
926 * matching property type in the 'prop_types' array.
927 * If 'prop_ids' is not NULL, then return only the text properties with
928 * an identifier in the 'props_ids' array.
929 * If 'add_lnum' is TRUE, then add the line number also to the text property
930 * dictionary.
931 */
932 static void
933get_props_in_line(
934 buf_T *buf,
935 linenr_T lnum,
936 int *prop_types,
937 int prop_types_len,
938 int *prop_ids,
939 int prop_ids_len,
940 list_T *retlist,
941 int add_lnum)
942{
943 char_u *text = ml_get_buf(buf, lnum, FALSE);
944 size_t textlen = STRLEN(text) + 1;
945 int count;
946 int i;
947 textprop_T prop;
948
949 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
950 for (i = 0; i < count; ++i)
951 {
952 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
953 sizeof(textprop_T));
954 if ((prop_types == NULL
955 || prop_type_or_id_in_list(prop_types, prop_types_len,
956 prop.tp_type))
957 && (prop_ids == NULL ||
958 prop_type_or_id_in_list(prop_ids, prop_ids_len,
959 prop.tp_id)))
960 {
961 dict_T *d = dict_alloc();
962
963 if (d == NULL)
964 break;
965 prop_fill_dict(d, &prop, buf);
966 if (add_lnum)
967 dict_add_number(d, "lnum", lnum);
968 list_append_dict(retlist, d);
969 }
970 }
971}
972
973/*
974 * Convert a List of property type names into an array of property type
975 * identifiers. Returns a pointer to the allocated array. Returns NULL on
976 * error. 'num_types' is set to the number of returned property types.
977 */
978 static int *
979get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
980{
981 int *prop_types;
982 listitem_T *li;
983 int i;
984 char_u *name;
985 proptype_T *type;
986
987 *num_types = 0;
988
989 prop_types = ALLOC_MULT(int, list_len(l));
990 if (prop_types == NULL)
991 return NULL;
992
993 i = 0;
994 FOR_ALL_LIST_ITEMS(l, li)
995 {
996 if (li->li_tv.v_type != VAR_STRING)
997 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000998 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000999 goto errret;
1000 }
1001 name = li->li_tv.vval.v_string;
1002 if (name == NULL)
1003 goto errret;
1004
1005 type = lookup_prop_type(name, buf);
1006 if (type == NULL)
1007 goto errret;
1008 prop_types[i++] = type->pt_id;
1009 }
1010
1011 *num_types = i;
1012 return prop_types;
1013
1014errret:
1015 VIM_CLEAR(prop_types);
1016 return NULL;
1017}
1018
1019/*
1020 * Convert a List of property identifiers into an array of property
1021 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1022 * error. 'num_ids' is set to the number of returned property identifiers.
1023 */
1024 static int *
1025get_prop_ids_from_list(list_T *l, int *num_ids)
1026{
1027 int *prop_ids;
1028 listitem_T *li;
1029 int i;
1030 int id;
1031 int error;
1032
1033 *num_ids = 0;
1034
1035 prop_ids = ALLOC_MULT(int, list_len(l));
1036 if (prop_ids == NULL)
1037 return NULL;
1038
1039 i = 0;
1040 FOR_ALL_LIST_ITEMS(l, li)
1041 {
1042 error = FALSE;
1043 id = tv_get_number_chk(&li->li_tv, &error);
1044 if (error)
1045 goto errret;
1046
1047 prop_ids[i++] = id;
1048 }
1049
1050 *num_ids = i;
1051 return prop_ids;
1052
1053errret:
1054 VIM_CLEAR(prop_ids);
1055 return NULL;
1056}
1057
1058/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001059 * prop_list({lnum} [, {bufnr}])
1060 */
1061 void
1062f_prop_list(typval_T *argvars, typval_T *rettv)
1063{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001064 linenr_T lnum;
1065 linenr_T start_lnum;
1066 linenr_T end_lnum;
1067 buf_T *buf = curbuf;
1068 int add_lnum = FALSE;
1069 int *prop_types = NULL;
1070 int prop_types_len = 0;
1071 int *prop_ids = NULL;
1072 int prop_ids_len = 0;
1073 list_T *l;
1074 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001075
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001076 if (in_vim9script()
1077 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001078 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001079 return;
1080
Bram Moolenaar93a10962022-06-16 11:42:09 +01001081 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001082 return;
1083
1084 // default: get text properties on current line
1085 start_lnum = tv_get_number(&argvars[0]);
1086 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001087 if (argvars[1].v_type != VAR_UNKNOWN)
1088 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001089 dict_T *d;
1090
1091 if (argvars[1].v_type != VAR_DICT)
1092 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001093 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001094 return;
1095 }
1096 d = argvars[1].vval.v_dict;
1097
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001098 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1099 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001100
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001101 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001102 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001103 if (di->di_tv.v_type != VAR_NUMBER)
1104 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001105 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001106 return;
1107 }
1108 end_lnum = tv_get_number(&di->di_tv);
1109 if (end_lnum < 0)
1110 // negative end_lnum is used as an offset from the last buffer
1111 // line
1112 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1113 else if (end_lnum > buf->b_ml.ml_line_count)
1114 end_lnum = buf->b_ml.ml_line_count;
1115 add_lnum = TRUE;
1116 }
1117 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1118 {
1119 if (di->di_tv.v_type != VAR_LIST)
1120 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001121 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001122 return;
1123 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001124
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001125 l = di->di_tv.vval.v_list;
1126 if (l != NULL && list_len(l) > 0)
1127 {
1128 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1129 if (prop_types == NULL)
1130 return;
1131 }
1132 }
1133 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1134 {
1135 if (di->di_tv.v_type != VAR_LIST)
1136 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001137 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001138 goto errret;
1139 }
1140
1141 l = di->di_tv.vval.v_list;
1142 if (l != NULL && list_len(l) > 0)
1143 {
1144 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1145 if (prop_ids == NULL)
1146 goto errret;
1147 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001148 }
1149 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001150 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1151 || end_lnum < 1 || end_lnum < start_lnum)
1152 emsg(_(e_invalid_range));
1153 else
1154 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1155 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1156 prop_ids, prop_ids_len,
1157 rettv->vval.v_list, add_lnum);
1158
1159errret:
1160 VIM_CLEAR(prop_types);
1161 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001162}
1163
1164/*
1165 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1166 */
1167 void
1168f_prop_remove(typval_T *argvars, typval_T *rettv)
1169{
1170 linenr_T start = 1;
1171 linenr_T end = 0;
1172 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001173 linenr_T first_changed = 0;
1174 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001175 dict_T *dict;
1176 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001177 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001178 int id = -1;
1179 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001180 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001181
1182 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001183
1184 if (in_vim9script()
1185 && (check_for_dict_arg(argvars, 0) == FAIL
1186 || check_for_opt_number_arg(argvars, 1) == FAIL
1187 || (argvars[1].v_type != VAR_UNKNOWN
1188 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1189 return;
1190
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001191 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1192 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001193 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001194 return;
1195 }
1196
1197 if (argvars[1].v_type != VAR_UNKNOWN)
1198 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001199 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001200 end = start;
1201 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001202 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001203 if (start < 1 || end < 1)
1204 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001205 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001206 return;
1207 }
1208 }
1209
1210 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001211 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1212 return;
1213 if (buf->b_ml.ml_mfp == NULL)
1214 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001215
Bram Moolenaard61efa52022-07-23 09:52:04 +01001216 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001217
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001218 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001219 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001220 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001221 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001222 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001223 proptype_T *type = lookup_prop_type(name, buf);
1224
1225 if (type == NULL)
1226 return;
1227 type_id = type->pt_id;
1228 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001229 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001230
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001231 if (id == -1 && type_id == -1)
1232 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001233 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001234 return;
1235 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001236 if (both && (id == -1 || type_id == -1))
1237 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001238 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001239 return;
1240 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001241
1242 if (end == 0)
1243 end = buf->b_ml.ml_line_count;
1244 for (lnum = start; lnum <= end; ++lnum)
1245 {
1246 char_u *text;
1247 size_t len;
1248
1249 if (lnum > buf->b_ml.ml_line_count)
1250 break;
1251 text = ml_get_buf(buf, lnum, FALSE);
1252 len = STRLEN(text) + 1;
1253 if ((size_t)buf->b_ml.ml_line_len > len)
1254 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001255 static textprop_T textprop; // static because of alignment
1256 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001257
1258 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1259 / sizeof(textprop_T); ++idx)
1260 {
1261 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1262 + idx * sizeof(textprop_T);
1263 size_t taillen;
1264
1265 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001266 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1267 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001268 {
1269 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1270 {
1271 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1272
1273 // need to allocate the line to be able to change it
1274 if (newptr == NULL)
1275 return;
1276 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1277 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001278 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1279 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001280 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001281 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1282
1283 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001284 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001285 }
1286
1287 taillen = buf->b_ml.ml_line_len - len
1288 - (idx + 1) * sizeof(textprop_T);
1289 if (taillen > 0)
1290 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1291 taillen);
1292 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1293 --idx;
1294
Bram Moolenaar965c0442021-05-17 00:22:06 +02001295 if (first_changed == 0)
1296 first_changed = lnum;
1297 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001298 ++rettv->vval.v_number;
1299 if (!do_all)
1300 break;
1301 }
1302 }
1303 }
1304 }
Bram Moolenaar965c0442021-05-17 00:22:06 +02001305 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001306 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001307 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1308 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001309 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001310}
1311
1312/*
1313 * Common for f_prop_type_add() and f_prop_type_change().
1314 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001315 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001316prop_type_set(typval_T *argvars, int add)
1317{
1318 char_u *name;
1319 buf_T *buf = NULL;
1320 dict_T *dict;
1321 dictitem_T *di;
1322 proptype_T *prop;
1323
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001324 if (in_vim9script()
1325 && (check_for_string_arg(argvars, 0) == FAIL
1326 || check_for_dict_arg(argvars, 1) == FAIL))
1327 return;
1328
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001329 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001330 if (*name == NUL)
1331 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001332 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001333 return;
1334 }
1335
1336 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1337 return;
1338 dict = argvars[1].vval.v_dict;
1339
1340 prop = find_prop(name, buf);
1341 if (add)
1342 {
1343 hashtab_T **htp;
1344
1345 if (prop != NULL)
1346 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001347 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001348 return;
1349 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001350 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001351 if (prop == NULL)
1352 return;
1353 STRCPY(prop->pt_name, name);
1354 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001355 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001356 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1357 if (*htp == NULL)
1358 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001359 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001360 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001361 {
1362 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001363 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001364 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001365 hash_init(*htp);
1366 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001367 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001368 }
1369 else
1370 {
1371 if (prop == NULL)
1372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001373 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001374 return;
1375 }
1376 }
1377
1378 if (dict != NULL)
1379 {
1380 di = dict_find(dict, (char_u *)"highlight", -1);
1381 if (di != NULL)
1382 {
1383 char_u *highlight;
1384 int hl_id = 0;
1385
Bram Moolenaard61efa52022-07-23 09:52:04 +01001386 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001387 if (highlight != NULL && *highlight != NUL)
1388 hl_id = syn_name2id(highlight);
1389 if (hl_id <= 0)
1390 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001391 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392 highlight == NULL ? (char_u *)"" : highlight);
1393 return;
1394 }
1395 prop->pt_hl_id = hl_id;
1396 }
1397
Bram Moolenaar58187f12019-05-05 16:33:47 +02001398 di = dict_find(dict, (char_u *)"combine", -1);
1399 if (di != NULL)
1400 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001401 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001402 prop->pt_flags |= PT_FLAG_COMBINE;
1403 else
1404 prop->pt_flags &= ~PT_FLAG_COMBINE;
1405 }
1406
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001407 di = dict_find(dict, (char_u *)"priority", -1);
1408 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001409 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001410
1411 di = dict_find(dict, (char_u *)"start_incl", -1);
1412 if (di != NULL)
1413 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001414 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001415 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1416 else
1417 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1418 }
1419
1420 di = dict_find(dict, (char_u *)"end_incl", -1);
1421 if (di != NULL)
1422 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001423 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001424 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1425 else
1426 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1427 }
1428 }
1429}
1430
1431/*
1432 * prop_type_add({name}, {props})
1433 */
1434 void
1435f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1436{
1437 prop_type_set(argvars, TRUE);
1438}
1439
1440/*
1441 * prop_type_change({name}, {props})
1442 */
1443 void
1444f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1445{
1446 prop_type_set(argvars, FALSE);
1447}
1448
1449/*
1450 * prop_type_delete({name} [, {bufnr}])
1451 */
1452 void
1453f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1454{
1455 char_u *name;
1456 buf_T *buf = NULL;
1457 hashitem_T *hi;
1458
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001459 if (in_vim9script()
1460 && (check_for_string_arg(argvars, 0) == FAIL
1461 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1462 return;
1463
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001464 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001465 if (*name == NUL)
1466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001467 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001468 return;
1469 }
1470
1471 if (argvars[1].v_type != VAR_UNKNOWN)
1472 {
1473 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1474 return;
1475 }
1476
1477 hi = find_prop_hi(name, buf);
1478 if (hi != NULL)
1479 {
1480 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001481 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001482
1483 if (buf == NULL)
1484 ht = global_proptypes;
1485 else
1486 ht = buf->b_proptypes;
1487 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001488 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001489 }
1490}
1491
1492/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001493 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494 */
1495 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001496f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001497{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001498 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001499
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001500 if (in_vim9script()
1501 && (check_for_string_arg(argvars, 0) == FAIL
1502 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1503 return;
1504
1505 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001506 if (*name == NUL)
1507 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001508 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001509 return;
1510 }
1511 if (rettv_dict_alloc(rettv) == OK)
1512 {
1513 proptype_T *prop = NULL;
1514 buf_T *buf = NULL;
1515
1516 if (argvars[1].v_type != VAR_UNKNOWN)
1517 {
1518 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1519 return;
1520 }
1521
1522 prop = find_prop(name, buf);
1523 if (prop != NULL)
1524 {
1525 dict_T *d = rettv->vval.v_dict;
1526
1527 if (prop->pt_hl_id > 0)
1528 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1529 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001530 dict_add_number(d, "combine",
1531 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001532 dict_add_number(d, "start_incl",
1533 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1534 dict_add_number(d, "end_incl",
1535 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1536 if (buf != NULL)
1537 dict_add_number(d, "bufnr", buf->b_fnum);
1538 }
1539 }
1540}
1541
1542 static void
1543list_types(hashtab_T *ht, list_T *l)
1544{
1545 long todo;
1546 hashitem_T *hi;
1547
1548 todo = (long)ht->ht_used;
1549 for (hi = ht->ht_array; todo > 0; ++hi)
1550 {
1551 if (!HASHITEM_EMPTY(hi))
1552 {
1553 proptype_T *prop = HI2PT(hi);
1554
1555 list_append_string(l, prop->pt_name, -1);
1556 --todo;
1557 }
1558 }
1559}
1560
1561/*
1562 * prop_type_list([{bufnr}])
1563 */
1564 void
1565f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1566{
1567 buf_T *buf = NULL;
1568
1569 if (rettv_list_alloc(rettv) == OK)
1570 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001571 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1572 return;
1573
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001574 if (argvars[0].v_type != VAR_UNKNOWN)
1575 {
1576 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1577 return;
1578 }
1579 if (buf == NULL)
1580 {
1581 if (global_proptypes != NULL)
1582 list_types(global_proptypes, rettv->vval.v_list);
1583 }
1584 else if (buf->b_proptypes != NULL)
1585 list_types(buf->b_proptypes, rettv->vval.v_list);
1586 }
1587}
1588
1589/*
1590 * Free all property types in "ht".
1591 */
1592 static void
1593clear_ht_prop_types(hashtab_T *ht)
1594{
1595 long todo;
1596 hashitem_T *hi;
1597
1598 if (ht == NULL)
1599 return;
1600
1601 todo = (long)ht->ht_used;
1602 for (hi = ht->ht_array; todo > 0; ++hi)
1603 {
1604 if (!HASHITEM_EMPTY(hi))
1605 {
1606 proptype_T *prop = HI2PT(hi);
1607
1608 vim_free(prop);
1609 --todo;
1610 }
1611 }
1612
1613 hash_clear(ht);
1614 vim_free(ht);
1615}
1616
1617#if defined(EXITFREE) || defined(PROTO)
1618/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001619 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001620 */
1621 void
1622clear_global_prop_types(void)
1623{
1624 clear_ht_prop_types(global_proptypes);
1625 global_proptypes = NULL;
1626}
1627#endif
1628
1629/*
1630 * Free all property types for "buf".
1631 */
1632 void
1633clear_buf_prop_types(buf_T *buf)
1634{
1635 clear_ht_prop_types(buf->b_proptypes);
1636 buf->b_proptypes = NULL;
1637}
1638
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001639// Struct used to return two values from adjust_prop().
1640typedef struct
1641{
1642 int dirty; // if the property was changed
1643 int can_drop; // whether after this change, the prop may be removed
1644} adjustres_T;
1645
1646/*
1647 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1648 *
1649 * Note that "col" is zero-based, while tp_col is one-based.
1650 * Only for the current buffer.
1651 * "flags" can have:
1652 * APC_SUBSTITUTE: Text is replaced, not inserted.
1653 */
1654 static adjustres_T
1655adjust_prop(
1656 textprop_T *prop,
1657 colnr_T col,
1658 int added,
1659 int flags)
1660{
1661 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1662 int start_incl = (pt != NULL
1663 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001664 || (flags & APC_SUBSTITUTE)
1665 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001666 int end_incl = (pt != NULL
LemonBoy698cb4c2022-05-14 18:10:15 +01001667 && (pt->pt_flags & PT_FLAG_INS_END_INCL))
1668 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
1669 // Do not drop zero-width props if they later can increase in size.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001670 int droppable = !(start_incl || end_incl);
1671 adjustres_T res = {TRUE, FALSE};
1672
1673 if (added > 0)
1674 {
1675 if (col + 1 <= prop->tp_col
1676 - (start_incl || (prop->tp_len == 0 && end_incl)))
1677 // Change is entirely before the text property: Only shift
1678 prop->tp_col += added;
1679 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1680 // Insertion was inside text property
1681 prop->tp_len += added;
1682 }
1683 else if (prop->tp_col > col + 1)
1684 {
1685 if (prop->tp_col + added < col + 1)
1686 {
1687 prop->tp_len += (prop->tp_col - 1 - col) + added;
1688 prop->tp_col = col + 1;
1689 if (prop->tp_len <= 0)
1690 {
1691 prop->tp_len = 0;
1692 res.can_drop = droppable;
1693 }
1694 }
1695 else
1696 prop->tp_col += added;
1697 }
1698 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1699 {
1700 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1701
1702 prop->tp_len += after > 0 ? added + after : added;
1703 res.can_drop = prop->tp_len <= 0 && droppable;
1704 }
1705 else
1706 res.dirty = FALSE;
1707
1708 return res;
1709}
1710
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001711/*
1712 * Adjust the columns of text properties in line "lnum" after position "col" to
1713 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001714 * Note that "col" is zero-based, while tp_col is one-based.
1715 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001716 * "flags" can have:
1717 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1718 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001719 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001720 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001721 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001722 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001723adjust_prop_columns(
1724 linenr_T lnum,
1725 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001726 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001727 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001728{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001729 int proplen;
1730 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001731 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001732 int ri, wi;
1733 size_t textlen;
1734
1735 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001736 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001737
1738 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1739 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001740 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001741 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001742
Bram Moolenaar196d1572019-01-02 23:47:18 +01001743 wi = 0; // write index
1744 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001745 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001746 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001747 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001748
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001749 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1750 res = adjust_prop(&prop, col, bytes_added, flags);
1751 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001752 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001753 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001754 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1755 && u_savesub(lnum) == FAIL)
1756 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001757 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001758
1759 // u_savesub() may have updated curbuf->b_ml, fetch it again
1760 if (curbuf->b_ml.ml_line_lnum != lnum)
1761 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001762 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001763 if (res.can_drop)
1764 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001765 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001766 ++wi;
1767 }
1768 if (dirty)
1769 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001770 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1771
1772 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001773 {
1774 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1775
1776 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1777 vim_free(curbuf->b_ml.ml_line_ptr);
1778 curbuf->b_ml.ml_line_ptr = p;
1779 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001780 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001781 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001782 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001783 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001784}
1785
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001786/*
1787 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001788 * "lnum_props" is the line that has the properties from before the split.
1789 * "lnum_top" is the top line.
1790 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001791 * "deleted" is the number of bytes deleted.
1792 */
1793 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001794adjust_props_for_split(
1795 linenr_T lnum_props,
1796 linenr_T lnum_top,
1797 int kept,
1798 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001799{
1800 char_u *props;
1801 int count;
1802 garray_T prevprop;
1803 garray_T nextprop;
1804 int i;
1805 int skipped = kept + deleted;
1806
1807 if (!curbuf->b_has_textprop)
1808 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001809
1810 // Get the text properties from "lnum_props".
1811 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001812 ga_init2(&prevprop, sizeof(textprop_T), 10);
1813 ga_init2(&nextprop, sizeof(textprop_T), 10);
1814
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001815 // Keep the relevant ones in the first line, reducing the length if needed.
1816 // Copy the ones that include the split to the second line.
1817 // Move the ones after the split to the second line.
1818 for (i = 0; i < count; ++i)
1819 {
1820 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001821 proptype_T *pt;
1822 int start_incl, end_incl;
1823 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001824
1825 // copy the prop to an aligned structure
1826 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1827
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001828 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1829 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1830 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1831 cont_prev = prop.tp_col + !start_incl <= kept;
1832 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1833
1834 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001835 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001836 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1837
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001838 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001839 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001840 if (p->tp_col + p->tp_len >= kept)
1841 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001842 if (cont_next)
1843 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001844 }
1845
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001846 // Only add the property to the next line if the length is bigger than
1847 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001848 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001849 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001850 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001851 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001852 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001853 if (p->tp_col > skipped)
1854 p->tp_col -= skipped - 1;
1855 else
1856 {
1857 p->tp_len -= skipped - p->tp_col;
1858 p->tp_col = 1;
1859 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001860 if (cont_prev)
1861 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001862 }
1863 }
1864
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001865 set_text_props(lnum_top, prevprop.ga_data,
1866 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001867 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001868 set_text_props(lnum_top + 1, nextprop.ga_data,
1869 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001870 ga_clear(&nextprop);
1871}
1872
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001873/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001874 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001875 */
1876 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001877prepend_joined_props(
1878 char_u *new_props,
1879 int propcount,
1880 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001881 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001882 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001883 long col,
1884 int removed)
1885{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001886 char_u *props;
1887 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1888 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001889
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001890 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001891 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001892 textprop_T prop;
1893 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001894
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001895 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1896 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1897
1898 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001899 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001900
1901 if (add_all || end)
1902 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1903 &prop, sizeof(prop));
1904 else
1905 {
1906 int j;
1907 int found = FALSE;
1908
1909 // Search for continuing prop.
1910 for (j = *props_remaining; j < propcount; ++j)
1911 {
1912 textprop_T op;
1913
1914 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1915 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1916 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001917 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001918 found = TRUE;
1919 op.tp_len += op.tp_col - prop.tp_col;
1920 op.tp_col = prop.tp_col;
1921 // Start/end is taken care of when deleting joined lines
1922 op.tp_flags = prop.tp_flags;
1923 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1924 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001925 }
1926 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001927 if (!found)
1928 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001929 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001930 }
1931}
1932
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001933#endif // FEAT_PROP_POPUP