blob: 9d3487fde2f4f9b8bb4f497272b84bfb1fb2439d [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
290 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
291 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 }
339 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
340
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100341 if (dict_has_key(dict, "id"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200342 id = dict_get_number(dict, (char_u *)"id");
343
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 Moolenaar8f667172018-12-14 15:38:31 +0100402 type_name = dict_get_string(dict, (char_u *)"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 Moolenaare3d31b02018-12-24 23:07:04 +0100406 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
407 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 {
418 long length = dict_get_number(dict, (char_u *)"length");
419
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 Moolenaare3d31b02018-12-24 23:07:04 +0100429 end_col = dict_get_number(dict, (char_u *)"end_col");
430 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 Moolenaar8f667172018-12-14 15:38:31 +0100442 id = dict_get_number(dict, (char_u *)"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);
567 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
568 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;
701 buf->b_ml.ml_line_ptr = newtext;
702 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
703 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100704 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100705 }
706 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100707 if (did_clear)
708 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100709}
710
711/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100712 * prop_find({props} [, {direction}])
713 */
714 void
715f_prop_find(typval_T *argvars, typval_T *rettv)
716{
717 pos_T *cursor = &curwin->w_cursor;
718 dict_T *dict;
719 buf_T *buf = curbuf;
720 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200721 int lnum_start;
722 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100723 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200724 int id = 0;
725 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200726 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100727 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200728 int lnum = -1;
729 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100730 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100731 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100732
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200733 if (in_vim9script()
734 && (check_for_dict_arg(argvars, 0) == FAIL
735 || check_for_opt_string_arg(argvars, 1) == FAIL))
736 return;
737
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100738 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
739 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000740 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100741 return;
742 }
743 dict = argvars[0].vval.v_dict;
744
745 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
746 return;
747 if (buf->b_ml.ml_mfp == NULL)
748 return;
749
750 if (argvars[1].v_type != VAR_UNKNOWN)
751 {
752 char_u *dir_s = tv_get_string(&argvars[1]);
753
754 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100755 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100756 else if (*dir_s != 'f')
757 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000758 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100759 return;
760 }
761 }
762
763 di = dict_find(dict, (char_u *)"lnum", -1);
764 if (di != NULL)
765 lnum = tv_get_number(&di->di_tv);
766
767 di = dict_find(dict, (char_u *)"col", -1);
768 if (di != NULL)
769 col = tv_get_number(&di->di_tv);
770
771 if (lnum == -1)
772 {
773 lnum = cursor->lnum;
774 col = cursor->col + 1;
775 }
776 else if (col == -1)
777 col = 1;
778
779 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
780 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200781 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100782 return;
783 }
784
Bram Moolenaareb245562020-09-03 22:33:44 +0200785 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100786
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100787 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200788 {
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100789 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200790 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200791 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100792 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100793 {
794 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
795 proptype_T *type = lookup_prop_type(name, buf);
796
797 if (type == NULL)
798 return;
799 type_id = type->pt_id;
800 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100801 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200802 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100803 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000804 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100805 return;
806 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200807 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100808 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000809 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100810 return;
811 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100812
813 lnum_start = lnum;
814
815 if (rettv_dict_alloc(rettv) == FAIL)
816 return;
817
818 while (1)
819 {
820 char_u *text = ml_get_buf(buf, lnum, FALSE);
821 size_t textlen = STRLEN(text) + 1;
822 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200823 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100824 int i;
825 textprop_T prop;
826 int prop_start;
827 int prop_end;
828
LemonBoy9bd3ce22022-04-18 21:54:02 +0100829 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100830 {
831 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100832 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100833
LemonBoy9bd3ce22022-04-18 21:54:02 +0100834 // For the very first line try to find the first property before or
835 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100836 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100837 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100838 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100839 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100840 if (prop.tp_col > col)
841 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100842 }
843 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
844 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100845 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100846 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200847 : (id_found && prop.tp_id == id)
848 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100849 {
850 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100851 if (lnum_start == lnum
852 && col >= prop.tp_col
853 && (col <= prop.tp_col + prop.tp_len
854 - (prop.tp_len != 0)))
855 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100856
LemonBoy9bd3ce22022-04-18 21:54:02 +0100857 // The property was not continued from last line, it starts on
858 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100859 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100860 // The property does not continue on the next line, it ends on
861 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100862 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100863 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100864 seen_end = 1;
865
866 // Skip lines without the start flag.
867 if (!prop_start)
868 {
869 // Always search backwards for start when search started
870 // on a prop and we're not skipping.
871 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +0100872 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200873 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100874 }
875
876 // If skipstart is true, skip the prop at start pos (even if
877 // continued from another line).
878 if (start_pos_has_prop && skipstart && !seen_end)
879 {
880 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200881 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100882 }
883
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100884 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
885 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
886
887 return;
888 }
889 }
890
891 if (dir > 0)
892 {
893 if (lnum >= buf->b_ml.ml_line_count)
894 break;
895 lnum++;
896 }
897 else
898 {
899 if (lnum <= 1)
900 break;
901 lnum--;
902 }
903 }
904}
905
906/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000907 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
908 */
909 static int
910prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
911{
912 int i;
913
914 for (i = 0; i < len; i++)
915 if (types_or_ids[i] == type_or_id)
916 return TRUE;
917
918 return FALSE;
919}
920
921/*
922 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
923 * If 'prop_types' is not NULL, then return only the text properties with
924 * matching property type in the 'prop_types' array.
925 * If 'prop_ids' is not NULL, then return only the text properties with
926 * an identifier in the 'props_ids' array.
927 * If 'add_lnum' is TRUE, then add the line number also to the text property
928 * dictionary.
929 */
930 static void
931get_props_in_line(
932 buf_T *buf,
933 linenr_T lnum,
934 int *prop_types,
935 int prop_types_len,
936 int *prop_ids,
937 int prop_ids_len,
938 list_T *retlist,
939 int add_lnum)
940{
941 char_u *text = ml_get_buf(buf, lnum, FALSE);
942 size_t textlen = STRLEN(text) + 1;
943 int count;
944 int i;
945 textprop_T prop;
946
947 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
948 for (i = 0; i < count; ++i)
949 {
950 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
951 sizeof(textprop_T));
952 if ((prop_types == NULL
953 || prop_type_or_id_in_list(prop_types, prop_types_len,
954 prop.tp_type))
955 && (prop_ids == NULL ||
956 prop_type_or_id_in_list(prop_ids, prop_ids_len,
957 prop.tp_id)))
958 {
959 dict_T *d = dict_alloc();
960
961 if (d == NULL)
962 break;
963 prop_fill_dict(d, &prop, buf);
964 if (add_lnum)
965 dict_add_number(d, "lnum", lnum);
966 list_append_dict(retlist, d);
967 }
968 }
969}
970
971/*
972 * Convert a List of property type names into an array of property type
973 * identifiers. Returns a pointer to the allocated array. Returns NULL on
974 * error. 'num_types' is set to the number of returned property types.
975 */
976 static int *
977get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
978{
979 int *prop_types;
980 listitem_T *li;
981 int i;
982 char_u *name;
983 proptype_T *type;
984
985 *num_types = 0;
986
987 prop_types = ALLOC_MULT(int, list_len(l));
988 if (prop_types == NULL)
989 return NULL;
990
991 i = 0;
992 FOR_ALL_LIST_ITEMS(l, li)
993 {
994 if (li->li_tv.v_type != VAR_STRING)
995 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000996 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000997 goto errret;
998 }
999 name = li->li_tv.vval.v_string;
1000 if (name == NULL)
1001 goto errret;
1002
1003 type = lookup_prop_type(name, buf);
1004 if (type == NULL)
1005 goto errret;
1006 prop_types[i++] = type->pt_id;
1007 }
1008
1009 *num_types = i;
1010 return prop_types;
1011
1012errret:
1013 VIM_CLEAR(prop_types);
1014 return NULL;
1015}
1016
1017/*
1018 * Convert a List of property identifiers into an array of property
1019 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1020 * error. 'num_ids' is set to the number of returned property identifiers.
1021 */
1022 static int *
1023get_prop_ids_from_list(list_T *l, int *num_ids)
1024{
1025 int *prop_ids;
1026 listitem_T *li;
1027 int i;
1028 int id;
1029 int error;
1030
1031 *num_ids = 0;
1032
1033 prop_ids = ALLOC_MULT(int, list_len(l));
1034 if (prop_ids == NULL)
1035 return NULL;
1036
1037 i = 0;
1038 FOR_ALL_LIST_ITEMS(l, li)
1039 {
1040 error = FALSE;
1041 id = tv_get_number_chk(&li->li_tv, &error);
1042 if (error)
1043 goto errret;
1044
1045 prop_ids[i++] = id;
1046 }
1047
1048 *num_ids = i;
1049 return prop_ids;
1050
1051errret:
1052 VIM_CLEAR(prop_ids);
1053 return NULL;
1054}
1055
1056/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001057 * prop_list({lnum} [, {bufnr}])
1058 */
1059 void
1060f_prop_list(typval_T *argvars, typval_T *rettv)
1061{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001062 linenr_T lnum;
1063 linenr_T start_lnum;
1064 linenr_T end_lnum;
1065 buf_T *buf = curbuf;
1066 int add_lnum = FALSE;
1067 int *prop_types = NULL;
1068 int prop_types_len = 0;
1069 int *prop_ids = NULL;
1070 int prop_ids_len = 0;
1071 list_T *l;
1072 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001073
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001074 if (in_vim9script()
1075 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001076 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001077 return;
1078
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001079 if (rettv_list_alloc(rettv) != OK)
1080 return;
1081
1082 // default: get text properties on current line
1083 start_lnum = tv_get_number(&argvars[0]);
1084 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001085 if (argvars[1].v_type != VAR_UNKNOWN)
1086 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001087 dict_T *d;
1088
1089 if (argvars[1].v_type != VAR_DICT)
1090 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001091 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001092 return;
1093 }
1094 d = argvars[1].vval.v_dict;
1095
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001096 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1097 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001098
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001099 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001100 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001101 if (di->di_tv.v_type != VAR_NUMBER)
1102 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001103 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001104 return;
1105 }
1106 end_lnum = tv_get_number(&di->di_tv);
1107 if (end_lnum < 0)
1108 // negative end_lnum is used as an offset from the last buffer
1109 // line
1110 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1111 else if (end_lnum > buf->b_ml.ml_line_count)
1112 end_lnum = buf->b_ml.ml_line_count;
1113 add_lnum = TRUE;
1114 }
1115 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1116 {
1117 if (di->di_tv.v_type != VAR_LIST)
1118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001119 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001120 return;
1121 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001122
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001123 l = di->di_tv.vval.v_list;
1124 if (l != NULL && list_len(l) > 0)
1125 {
1126 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1127 if (prop_types == NULL)
1128 return;
1129 }
1130 }
1131 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1132 {
1133 if (di->di_tv.v_type != VAR_LIST)
1134 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001135 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001136 goto errret;
1137 }
1138
1139 l = di->di_tv.vval.v_list;
1140 if (l != NULL && list_len(l) > 0)
1141 {
1142 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1143 if (prop_ids == NULL)
1144 goto errret;
1145 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001146 }
1147 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001148 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1149 || end_lnum < 1 || end_lnum < start_lnum)
1150 emsg(_(e_invalid_range));
1151 else
1152 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1153 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1154 prop_ids, prop_ids_len,
1155 rettv->vval.v_list, add_lnum);
1156
1157errret:
1158 VIM_CLEAR(prop_types);
1159 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001160}
1161
1162/*
1163 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1164 */
1165 void
1166f_prop_remove(typval_T *argvars, typval_T *rettv)
1167{
1168 linenr_T start = 1;
1169 linenr_T end = 0;
1170 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001171 linenr_T first_changed = 0;
1172 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001173 dict_T *dict;
1174 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001175 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001176 int id = -1;
1177 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001178 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001179
1180 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001181
1182 if (in_vim9script()
1183 && (check_for_dict_arg(argvars, 0) == FAIL
1184 || check_for_opt_number_arg(argvars, 1) == FAIL
1185 || (argvars[1].v_type != VAR_UNKNOWN
1186 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1187 return;
1188
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001189 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1190 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001191 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001192 return;
1193 }
1194
1195 if (argvars[1].v_type != VAR_UNKNOWN)
1196 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001197 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001198 end = start;
1199 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001200 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001201 if (start < 1 || end < 1)
1202 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001203 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001204 return;
1205 }
1206 }
1207
1208 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001209 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1210 return;
1211 if (buf->b_ml.ml_mfp == NULL)
1212 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001213
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001214 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001215
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001216 if (dict_has_key(dict, "id"))
Bram Moolenaar8f667172018-12-14 15:38:31 +01001217 id = dict_get_number(dict, (char_u *)"id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001218 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001219 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01001220 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001221 proptype_T *type = lookup_prop_type(name, buf);
1222
1223 if (type == NULL)
1224 return;
1225 type_id = type->pt_id;
1226 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001227 both = dict_get_bool(dict, (char_u *)"both", FALSE);
1228
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001229 if (id == -1 && type_id == -1)
1230 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001231 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001232 return;
1233 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001234 if (both && (id == -1 || type_id == -1))
1235 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001236 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001237 return;
1238 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001239
1240 if (end == 0)
1241 end = buf->b_ml.ml_line_count;
1242 for (lnum = start; lnum <= end; ++lnum)
1243 {
1244 char_u *text;
1245 size_t len;
1246
1247 if (lnum > buf->b_ml.ml_line_count)
1248 break;
1249 text = ml_get_buf(buf, lnum, FALSE);
1250 len = STRLEN(text) + 1;
1251 if ((size_t)buf->b_ml.ml_line_len > len)
1252 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001253 static textprop_T textprop; // static because of alignment
1254 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001255
1256 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1257 / sizeof(textprop_T); ++idx)
1258 {
1259 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1260 + idx * sizeof(textprop_T);
1261 size_t taillen;
1262
1263 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001264 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1265 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001266 {
1267 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1268 {
1269 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1270
1271 // need to allocate the line to be able to change it
1272 if (newptr == NULL)
1273 return;
1274 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1275 buf->b_ml.ml_line_len);
1276 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001277 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1278
1279 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001280 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001281 }
1282
1283 taillen = buf->b_ml.ml_line_len - len
1284 - (idx + 1) * sizeof(textprop_T);
1285 if (taillen > 0)
1286 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1287 taillen);
1288 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1289 --idx;
1290
Bram Moolenaar965c0442021-05-17 00:22:06 +02001291 if (first_changed == 0)
1292 first_changed = lnum;
1293 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001294 ++rettv->vval.v_number;
1295 if (!do_all)
1296 break;
1297 }
1298 }
1299 }
1300 }
Bram Moolenaar965c0442021-05-17 00:22:06 +02001301 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001302 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001303 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1304 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001305 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001306}
1307
1308/*
1309 * Common for f_prop_type_add() and f_prop_type_change().
1310 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001311 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001312prop_type_set(typval_T *argvars, int add)
1313{
1314 char_u *name;
1315 buf_T *buf = NULL;
1316 dict_T *dict;
1317 dictitem_T *di;
1318 proptype_T *prop;
1319
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001320 if (in_vim9script()
1321 && (check_for_string_arg(argvars, 0) == FAIL
1322 || check_for_dict_arg(argvars, 1) == FAIL))
1323 return;
1324
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001325 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001326 if (*name == NUL)
1327 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001328 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001329 return;
1330 }
1331
1332 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1333 return;
1334 dict = argvars[1].vval.v_dict;
1335
1336 prop = find_prop(name, buf);
1337 if (add)
1338 {
1339 hashtab_T **htp;
1340
1341 if (prop != NULL)
1342 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001343 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001344 return;
1345 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001346 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001347 if (prop == NULL)
1348 return;
1349 STRCPY(prop->pt_name, name);
1350 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001351 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001352 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1353 if (*htp == NULL)
1354 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001355 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001356 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001357 {
1358 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001359 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001360 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001361 hash_init(*htp);
1362 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001363 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001364 }
1365 else
1366 {
1367 if (prop == NULL)
1368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001369 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001370 return;
1371 }
1372 }
1373
1374 if (dict != NULL)
1375 {
1376 di = dict_find(dict, (char_u *)"highlight", -1);
1377 if (di != NULL)
1378 {
1379 char_u *highlight;
1380 int hl_id = 0;
1381
Bram Moolenaar8f667172018-12-14 15:38:31 +01001382 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001383 if (highlight != NULL && *highlight != NUL)
1384 hl_id = syn_name2id(highlight);
1385 if (hl_id <= 0)
1386 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001387 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001388 highlight == NULL ? (char_u *)"" : highlight);
1389 return;
1390 }
1391 prop->pt_hl_id = hl_id;
1392 }
1393
Bram Moolenaar58187f12019-05-05 16:33:47 +02001394 di = dict_find(dict, (char_u *)"combine", -1);
1395 if (di != NULL)
1396 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001397 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001398 prop->pt_flags |= PT_FLAG_COMBINE;
1399 else
1400 prop->pt_flags &= ~PT_FLAG_COMBINE;
1401 }
1402
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001403 di = dict_find(dict, (char_u *)"priority", -1);
1404 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001405 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001406
1407 di = dict_find(dict, (char_u *)"start_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_START_INCL;
1412 else
1413 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1414 }
1415
1416 di = dict_find(dict, (char_u *)"end_incl", -1);
1417 if (di != NULL)
1418 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001419 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001420 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1421 else
1422 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1423 }
1424 }
1425}
1426
1427/*
1428 * prop_type_add({name}, {props})
1429 */
1430 void
1431f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1432{
1433 prop_type_set(argvars, TRUE);
1434}
1435
1436/*
1437 * prop_type_change({name}, {props})
1438 */
1439 void
1440f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1441{
1442 prop_type_set(argvars, FALSE);
1443}
1444
1445/*
1446 * prop_type_delete({name} [, {bufnr}])
1447 */
1448 void
1449f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1450{
1451 char_u *name;
1452 buf_T *buf = NULL;
1453 hashitem_T *hi;
1454
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001455 if (in_vim9script()
1456 && (check_for_string_arg(argvars, 0) == FAIL
1457 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1458 return;
1459
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001460 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001461 if (*name == NUL)
1462 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001463 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001464 return;
1465 }
1466
1467 if (argvars[1].v_type != VAR_UNKNOWN)
1468 {
1469 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1470 return;
1471 }
1472
1473 hi = find_prop_hi(name, buf);
1474 if (hi != NULL)
1475 {
1476 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001477 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001478
1479 if (buf == NULL)
1480 ht = global_proptypes;
1481 else
1482 ht = buf->b_proptypes;
1483 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001484 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001485 }
1486}
1487
1488/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001489 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001490 */
1491 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001492f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001493{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001494 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001496 if (in_vim9script()
1497 && (check_for_string_arg(argvars, 0) == FAIL
1498 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1499 return;
1500
1501 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001502 if (*name == NUL)
1503 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001504 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001505 return;
1506 }
1507 if (rettv_dict_alloc(rettv) == OK)
1508 {
1509 proptype_T *prop = NULL;
1510 buf_T *buf = NULL;
1511
1512 if (argvars[1].v_type != VAR_UNKNOWN)
1513 {
1514 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1515 return;
1516 }
1517
1518 prop = find_prop(name, buf);
1519 if (prop != NULL)
1520 {
1521 dict_T *d = rettv->vval.v_dict;
1522
1523 if (prop->pt_hl_id > 0)
1524 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1525 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001526 dict_add_number(d, "combine",
1527 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001528 dict_add_number(d, "start_incl",
1529 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1530 dict_add_number(d, "end_incl",
1531 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1532 if (buf != NULL)
1533 dict_add_number(d, "bufnr", buf->b_fnum);
1534 }
1535 }
1536}
1537
1538 static void
1539list_types(hashtab_T *ht, list_T *l)
1540{
1541 long todo;
1542 hashitem_T *hi;
1543
1544 todo = (long)ht->ht_used;
1545 for (hi = ht->ht_array; todo > 0; ++hi)
1546 {
1547 if (!HASHITEM_EMPTY(hi))
1548 {
1549 proptype_T *prop = HI2PT(hi);
1550
1551 list_append_string(l, prop->pt_name, -1);
1552 --todo;
1553 }
1554 }
1555}
1556
1557/*
1558 * prop_type_list([{bufnr}])
1559 */
1560 void
1561f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1562{
1563 buf_T *buf = NULL;
1564
1565 if (rettv_list_alloc(rettv) == OK)
1566 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001567 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1568 return;
1569
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001570 if (argvars[0].v_type != VAR_UNKNOWN)
1571 {
1572 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1573 return;
1574 }
1575 if (buf == NULL)
1576 {
1577 if (global_proptypes != NULL)
1578 list_types(global_proptypes, rettv->vval.v_list);
1579 }
1580 else if (buf->b_proptypes != NULL)
1581 list_types(buf->b_proptypes, rettv->vval.v_list);
1582 }
1583}
1584
1585/*
1586 * Free all property types in "ht".
1587 */
1588 static void
1589clear_ht_prop_types(hashtab_T *ht)
1590{
1591 long todo;
1592 hashitem_T *hi;
1593
1594 if (ht == NULL)
1595 return;
1596
1597 todo = (long)ht->ht_used;
1598 for (hi = ht->ht_array; todo > 0; ++hi)
1599 {
1600 if (!HASHITEM_EMPTY(hi))
1601 {
1602 proptype_T *prop = HI2PT(hi);
1603
1604 vim_free(prop);
1605 --todo;
1606 }
1607 }
1608
1609 hash_clear(ht);
1610 vim_free(ht);
1611}
1612
1613#if defined(EXITFREE) || defined(PROTO)
1614/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001615 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001616 */
1617 void
1618clear_global_prop_types(void)
1619{
1620 clear_ht_prop_types(global_proptypes);
1621 global_proptypes = NULL;
1622}
1623#endif
1624
1625/*
1626 * Free all property types for "buf".
1627 */
1628 void
1629clear_buf_prop_types(buf_T *buf)
1630{
1631 clear_ht_prop_types(buf->b_proptypes);
1632 buf->b_proptypes = NULL;
1633}
1634
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001635// Struct used to return two values from adjust_prop().
1636typedef struct
1637{
1638 int dirty; // if the property was changed
1639 int can_drop; // whether after this change, the prop may be removed
1640} adjustres_T;
1641
1642/*
1643 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1644 *
1645 * Note that "col" is zero-based, while tp_col is one-based.
1646 * Only for the current buffer.
1647 * "flags" can have:
1648 * APC_SUBSTITUTE: Text is replaced, not inserted.
1649 */
1650 static adjustres_T
1651adjust_prop(
1652 textprop_T *prop,
1653 colnr_T col,
1654 int added,
1655 int flags)
1656{
1657 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1658 int start_incl = (pt != NULL
1659 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001660 || (flags & APC_SUBSTITUTE)
1661 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001662 int end_incl = (pt != NULL
LemonBoy698cb4c2022-05-14 18:10:15 +01001663 && (pt->pt_flags & PT_FLAG_INS_END_INCL))
1664 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
1665 // Do not drop zero-width props if they later can increase in size.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001666 int droppable = !(start_incl || end_incl);
1667 adjustres_T res = {TRUE, FALSE};
1668
1669 if (added > 0)
1670 {
1671 if (col + 1 <= prop->tp_col
1672 - (start_incl || (prop->tp_len == 0 && end_incl)))
1673 // Change is entirely before the text property: Only shift
1674 prop->tp_col += added;
1675 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1676 // Insertion was inside text property
1677 prop->tp_len += added;
1678 }
1679 else if (prop->tp_col > col + 1)
1680 {
1681 if (prop->tp_col + added < col + 1)
1682 {
1683 prop->tp_len += (prop->tp_col - 1 - col) + added;
1684 prop->tp_col = col + 1;
1685 if (prop->tp_len <= 0)
1686 {
1687 prop->tp_len = 0;
1688 res.can_drop = droppable;
1689 }
1690 }
1691 else
1692 prop->tp_col += added;
1693 }
1694 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1695 {
1696 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1697
1698 prop->tp_len += after > 0 ? added + after : added;
1699 res.can_drop = prop->tp_len <= 0 && droppable;
1700 }
1701 else
1702 res.dirty = FALSE;
1703
1704 return res;
1705}
1706
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001707/*
1708 * Adjust the columns of text properties in line "lnum" after position "col" to
1709 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001710 * Note that "col" is zero-based, while tp_col is one-based.
1711 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001712 * "flags" can have:
1713 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1714 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001715 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001716 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001717 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001718 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001719adjust_prop_columns(
1720 linenr_T lnum,
1721 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001722 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001723 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001724{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001725 int proplen;
1726 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001727 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001728 int ri, wi;
1729 size_t textlen;
1730
1731 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001732 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001733
1734 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1735 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001736 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001737 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001738
Bram Moolenaar196d1572019-01-02 23:47:18 +01001739 wi = 0; // write index
1740 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001741 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001742 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001743 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001744
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001745 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1746 res = adjust_prop(&prop, col, bytes_added, flags);
1747 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001748 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001749 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001750 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1751 && u_savesub(lnum) == FAIL)
1752 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001753 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001754
1755 // u_savesub() may have updated curbuf->b_ml, fetch it again
1756 if (curbuf->b_ml.ml_line_lnum != lnum)
1757 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001758 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001759 if (res.can_drop)
1760 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001761 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001762 ++wi;
1763 }
1764 if (dirty)
1765 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001766 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1767
1768 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1769 curbuf->b_ml.ml_line_ptr =
1770 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001771 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001772 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001773 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001774 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001775}
1776
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001777/*
1778 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001779 * "lnum_props" is the line that has the properties from before the split.
1780 * "lnum_top" is the top line.
1781 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001782 * "deleted" is the number of bytes deleted.
1783 */
1784 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001785adjust_props_for_split(
1786 linenr_T lnum_props,
1787 linenr_T lnum_top,
1788 int kept,
1789 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001790{
1791 char_u *props;
1792 int count;
1793 garray_T prevprop;
1794 garray_T nextprop;
1795 int i;
1796 int skipped = kept + deleted;
1797
1798 if (!curbuf->b_has_textprop)
1799 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001800
1801 // Get the text properties from "lnum_props".
1802 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001803 ga_init2(&prevprop, sizeof(textprop_T), 10);
1804 ga_init2(&nextprop, sizeof(textprop_T), 10);
1805
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001806 // Keep the relevant ones in the first line, reducing the length if needed.
1807 // Copy the ones that include the split to the second line.
1808 // Move the ones after the split to the second line.
1809 for (i = 0; i < count; ++i)
1810 {
1811 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001812 proptype_T *pt;
1813 int start_incl, end_incl;
1814 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001815
1816 // copy the prop to an aligned structure
1817 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1818
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001819 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1820 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1821 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1822 cont_prev = prop.tp_col + !start_incl <= kept;
1823 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1824
1825 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001826 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001827 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1828
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001829 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001830 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001831 if (p->tp_col + p->tp_len >= kept)
1832 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001833 if (cont_next)
1834 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001835 }
1836
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001837 // Only add the property to the next line if the length is bigger than
1838 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001839 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001840 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001841 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001842 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001843 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001844 if (p->tp_col > skipped)
1845 p->tp_col -= skipped - 1;
1846 else
1847 {
1848 p->tp_len -= skipped - p->tp_col;
1849 p->tp_col = 1;
1850 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001851 if (cont_prev)
1852 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001853 }
1854 }
1855
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001856 set_text_props(lnum_top, prevprop.ga_data,
1857 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001858 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001859 set_text_props(lnum_top + 1, nextprop.ga_data,
1860 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001861 ga_clear(&nextprop);
1862}
1863
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001864/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001865 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001866 */
1867 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001868prepend_joined_props(
1869 char_u *new_props,
1870 int propcount,
1871 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001872 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001873 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001874 long col,
1875 int removed)
1876{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001877 char_u *props;
1878 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1879 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001880
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001881 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001882 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001883 textprop_T prop;
1884 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001885
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001886 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1887 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1888
1889 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001890 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001891
1892 if (add_all || end)
1893 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1894 &prop, sizeof(prop));
1895 else
1896 {
1897 int j;
1898 int found = FALSE;
1899
1900 // Search for continuing prop.
1901 for (j = *props_remaining; j < propcount; ++j)
1902 {
1903 textprop_T op;
1904
1905 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1906 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1907 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001908 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001909 found = TRUE;
1910 op.tp_len += op.tp_col - prop.tp_col;
1911 op.tp_col = prop.tp_col;
1912 // Start/end is taken care of when deleting joined lines
1913 op.tp_flags = prop.tp_flags;
1914 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1915 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001916 }
1917 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001918 if (!found)
1919 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001920 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001921 }
1922}
1923
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001924#endif // FEAT_PROP_POPUP