blob: f5cf6b851ca639629bbd932fbe045cb114fe1c68 [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
347 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
348 {
349 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
350 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000351 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200352 return;
353 }
354
355 pos_list = li->li_tv.vval.v_list;
356 start_lnum = list_find_nr(pos_list, 0L, &error);
357 start_col = list_find_nr(pos_list, 1L, &error);
358 end_lnum = list_find_nr(pos_list, 2L, &error);
359 end_col = list_find_nr(pos_list, 3L, &error);
360 if (error || start_lnum <= 0 || start_col <= 0
361 || end_lnum <= 0 || end_col <= 0)
362 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000363 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364 return;
365 }
366 if (prop_add_one(buf, type_name, id, start_lnum, end_lnum,
367 start_col, end_col) == FAIL)
368 return;
369 }
370
371 buf->b_has_textprop = TRUE; // this is never reset
372 redraw_buf_later(buf, VALID);
373}
374
375/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200376 * Shared between prop_add() and popup_create().
377 * "dict_arg" is the function argument of a dict containing "bufnr".
378 * it is NULL for popup_create().
379 */
380 void
381prop_add_common(
382 linenr_T start_lnum,
383 colnr_T start_col,
384 dict_T *dict,
385 buf_T *default_buf,
386 typval_T *dict_arg)
387{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200388 linenr_T end_lnum;
389 colnr_T end_col;
390 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200391 buf_T *buf = default_buf;
392 int id = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100393
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100394 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100395 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000396 emsg(_(e_missing_property_type_name));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100397 return;
398 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100399 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100400
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100401 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100402 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100403 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
404 if (end_lnum < start_lnum)
405 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000406 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100407 return;
408 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100409 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100410 else
411 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100412
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100413 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100414 {
415 long length = dict_get_number(dict, (char_u *)"length");
416
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100417 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100418 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000419 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100420 return;
421 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100422 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100423 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100424 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100425 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100426 end_col = dict_get_number(dict, (char_u *)"end_col");
427 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100428 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000429 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100430 return;
431 }
432 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100433 else if (start_lnum == end_lnum)
434 end_col = start_col;
435 else
436 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100437
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100438 if (dict_has_key(dict, "id"))
Bram Moolenaar8f667172018-12-14 15:38:31 +0100439 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100440
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200441 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100442 return;
443
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200444 prop_add_one(buf, type_name, id, start_lnum, end_lnum, start_col, end_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100445
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100446 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200447 redraw_buf_later(buf, VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100448}
449
450/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100451 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100452 * Returns the number of text properties and, when non-zero, a pointer to the
453 * first one in "props" (note that it is not aligned, therefore the char_u
454 * pointer).
455 */
456 int
457get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
458{
459 char_u *text;
460 size_t textlen;
461 size_t proplen;
462
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100463 // Be quick when no text property types have been defined or the buffer,
464 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200465 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100466 return 0;
467
468 // Fetch the line to get the ml_line_len field updated.
469 text = ml_get_buf(buf, lnum, will_change);
470 textlen = STRLEN(text) + 1;
471 proplen = buf->b_ml.ml_line_len - textlen;
472 if (proplen % sizeof(textprop_T) != 0)
473 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000474 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100475 return 0;
476 }
477 if (proplen > 0)
478 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100479 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100480}
481
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200482/**
483 * Return the number of text properties on line "lnum" in the current buffer.
484 * When "only_starting" is true only text properties starting in this line will
485 * be considered.
486 */
487 int
488count_props(linenr_T lnum, int only_starting)
489{
490 char_u *props;
491 int proplen = get_text_props(curbuf, lnum, &props, 0);
492 int result = proplen;
493 int i;
494 textprop_T prop;
495
496 if (only_starting)
497 for (i = 0; i < proplen; ++i)
498 {
499 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
500 if (prop.tp_flags & TP_FLAG_CONT_PREV)
501 --result;
502 }
503 return result;
504}
505
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100506/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200507 * Find text property "type_id" in the visible lines of window "wp".
508 * Match "id" when it is > 0.
509 * Returns FAIL when not found.
510 */
511 int
512find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
513 linenr_T *found_lnum)
514{
515 linenr_T lnum;
516 char_u *props;
517 int count;
518 int i;
519
520 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100521 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200522 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
523 {
524 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
525 for (i = 0; i < count; ++i)
526 {
527 mch_memmove(prop, props + i * sizeof(textprop_T),
528 sizeof(textprop_T));
529 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
530 {
531 *found_lnum = lnum;
532 return OK;
533 }
534 }
535 }
536 return FAIL;
537}
538
539/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100540 * Set the text properties for line "lnum" to "props" with length "len".
541 * If "len" is zero text properties are removed, "props" is not used.
542 * Any existing text properties are dropped.
543 * Only works for the current buffer.
544 */
545 static void
546set_text_props(linenr_T lnum, char_u *props, int len)
547{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100548 char_u *text;
549 char_u *newtext;
550 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100551
552 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100553 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100554 newtext = alloc(textlen + len);
555 if (newtext == NULL)
556 return;
557 mch_memmove(newtext, text, textlen);
558 if (len > 0)
559 mch_memmove(newtext + textlen, props, len);
560 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
561 vim_free(curbuf->b_ml.ml_line_ptr);
562 curbuf->b_ml.ml_line_ptr = newtext;
563 curbuf->b_ml.ml_line_len = textlen + len;
564 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
565}
566
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100567 static proptype_T *
568find_type_by_id(hashtab_T *ht, int id)
569{
570 long todo;
571 hashitem_T *hi;
572
573 if (ht == NULL)
574 return NULL;
575
576 // TODO: Make this faster by keeping a list of types sorted on ID and use
577 // a binary search.
578
579 todo = (long)ht->ht_used;
580 for (hi = ht->ht_array; todo > 0; ++hi)
581 {
582 if (!HASHITEM_EMPTY(hi))
583 {
584 proptype_T *prop = HI2PT(hi);
585
586 if (prop->pt_id == id)
587 return prop;
588 --todo;
589 }
590 }
591 return NULL;
592}
593
594/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100595 * Fill 'dict' with text properties in 'prop'.
596 */
597 static void
598prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
599{
600 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200601 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100602
603 dict_add_number(dict, "col", prop->tp_col);
604 dict_add_number(dict, "length", prop->tp_len);
605 dict_add_number(dict, "id", prop->tp_id);
606 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
607 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200608
609 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
610 if (pt == NULL)
611 {
612 pt = find_type_by_id(global_proptypes, prop->tp_type);
613 buflocal = FALSE;
614 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100615 if (pt != NULL)
616 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200617
618 if (buflocal)
619 dict_add_number(dict, "type_bufnr", buf->b_fnum);
620 else
621 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100622}
623
624/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100625 * Find a property type by ID in "buf" or globally.
626 * Returns NULL if not found.
627 */
628 proptype_T *
629text_prop_type_by_id(buf_T *buf, int id)
630{
631 proptype_T *type;
632
633 type = find_type_by_id(buf->b_proptypes, id);
634 if (type == NULL)
635 type = find_type_by_id(global_proptypes, id);
636 return type;
637}
638
639/*
640 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
641 */
642 void
643f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
644{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200645 linenr_T start;
646 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100647 linenr_T lnum;
648 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100649 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100650
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200651 if (in_vim9script()
652 && (check_for_number_arg(argvars, 0) == FAIL
653 || check_for_opt_number_arg(argvars, 1) == FAIL
654 || (argvars[1].v_type != VAR_UNKNOWN
655 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
656 return;
657
658 start = tv_get_number(&argvars[0]);
659 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100660 if (argvars[1].v_type != VAR_UNKNOWN)
661 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100662 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100663 if (argvars[2].v_type != VAR_UNKNOWN)
664 {
665 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
666 return;
667 }
668 }
669 if (start < 1 || end < 1)
670 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200671 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100672 return;
673 }
674
675 for (lnum = start; lnum <= end; ++lnum)
676 {
677 char_u *text;
678 size_t len;
679
680 if (lnum > buf->b_ml.ml_line_count)
681 break;
682 text = ml_get_buf(buf, lnum, FALSE);
683 len = STRLEN(text) + 1;
684 if ((size_t)buf->b_ml.ml_line_len > len)
685 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100686 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100687 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
688 {
689 char_u *newtext = vim_strsave(text);
690
691 // need to allocate the line now
692 if (newtext == NULL)
693 return;
694 buf->b_ml.ml_line_ptr = newtext;
695 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
696 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100697 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100698 }
699 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100700 if (did_clear)
701 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100702}
703
704/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100705 * prop_find({props} [, {direction}])
706 */
707 void
708f_prop_find(typval_T *argvars, typval_T *rettv)
709{
710 pos_T *cursor = &curwin->w_cursor;
711 dict_T *dict;
712 buf_T *buf = curbuf;
713 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200714 int lnum_start;
715 int start_pos_has_prop = 0;
716 int seen_end = 0;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200717 int id = 0;
718 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200719 int type_id = -1;
720 int skipstart = 0;
721 int lnum = -1;
722 int col = -1;
723 int dir = 1; // 1 = forward, -1 = backward
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100724 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100725
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200726 if (in_vim9script()
727 && (check_for_dict_arg(argvars, 0) == FAIL
728 || check_for_opt_string_arg(argvars, 1) == FAIL))
729 return;
730
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100731 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
732 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000733 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100734 return;
735 }
736 dict = argvars[0].vval.v_dict;
737
738 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
739 return;
740 if (buf->b_ml.ml_mfp == NULL)
741 return;
742
743 if (argvars[1].v_type != VAR_UNKNOWN)
744 {
745 char_u *dir_s = tv_get_string(&argvars[1]);
746
747 if (*dir_s == 'b')
748 dir = -1;
749 else if (*dir_s != 'f')
750 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000751 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100752 return;
753 }
754 }
755
756 di = dict_find(dict, (char_u *)"lnum", -1);
757 if (di != NULL)
758 lnum = tv_get_number(&di->di_tv);
759
760 di = dict_find(dict, (char_u *)"col", -1);
761 if (di != NULL)
762 col = tv_get_number(&di->di_tv);
763
764 if (lnum == -1)
765 {
766 lnum = cursor->lnum;
767 col = cursor->col + 1;
768 }
769 else if (col == -1)
770 col = 1;
771
772 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
773 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200774 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100775 return;
776 }
777
Bram Moolenaareb245562020-09-03 22:33:44 +0200778 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100779
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100780 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200781 {
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100782 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200783 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200784 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100785 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100786 {
787 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
788 proptype_T *type = lookup_prop_type(name, buf);
789
790 if (type == NULL)
791 return;
792 type_id = type->pt_id;
793 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100794 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200795 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100796 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000797 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100798 return;
799 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200800 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100801 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000802 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100803 return;
804 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100805
806 lnum_start = lnum;
807
808 if (rettv_dict_alloc(rettv) == FAIL)
809 return;
810
811 while (1)
812 {
813 char_u *text = ml_get_buf(buf, lnum, FALSE);
814 size_t textlen = STRLEN(text) + 1;
815 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200816 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100817 int i;
818 textprop_T prop;
819 int prop_start;
820 int prop_end;
821
822 for (i = 0; i < count; ++i)
823 {
824 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
825 sizeof(textprop_T));
826
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100827 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100828 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100829 if (dir < 0)
830 {
831 if (col < prop.tp_col)
832 break;
833 }
834 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
835 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100836 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100837 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200838 : (id_found && prop.tp_id == id)
839 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100840 {
841 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100842 if (lnum_start == lnum
843 && col >= prop.tp_col
844 && (col <= prop.tp_col + prop.tp_len
845 - (prop.tp_len != 0)))
846 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100847
848 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
849 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
850 if (!prop_start && prop_end && dir > 0)
851 seen_end = 1;
852
853 // Skip lines without the start flag.
854 if (!prop_start)
855 {
856 // Always search backwards for start when search started
857 // on a prop and we're not skipping.
858 if (start_pos_has_prop && !skipstart)
859 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200860 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100861 }
862
863 // If skipstart is true, skip the prop at start pos (even if
864 // continued from another line).
865 if (start_pos_has_prop && skipstart && !seen_end)
866 {
867 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200868 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100869 }
870
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100871 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
872 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
873
874 return;
875 }
876 }
877
878 if (dir > 0)
879 {
880 if (lnum >= buf->b_ml.ml_line_count)
881 break;
882 lnum++;
883 }
884 else
885 {
886 if (lnum <= 1)
887 break;
888 lnum--;
889 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100890 // Adjust col to indicate that we're continuing from prev/next line.
891 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100892 }
893}
894
895/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000896 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
897 */
898 static int
899prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
900{
901 int i;
902
903 for (i = 0; i < len; i++)
904 if (types_or_ids[i] == type_or_id)
905 return TRUE;
906
907 return FALSE;
908}
909
910/*
911 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
912 * If 'prop_types' is not NULL, then return only the text properties with
913 * matching property type in the 'prop_types' array.
914 * If 'prop_ids' is not NULL, then return only the text properties with
915 * an identifier in the 'props_ids' array.
916 * If 'add_lnum' is TRUE, then add the line number also to the text property
917 * dictionary.
918 */
919 static void
920get_props_in_line(
921 buf_T *buf,
922 linenr_T lnum,
923 int *prop_types,
924 int prop_types_len,
925 int *prop_ids,
926 int prop_ids_len,
927 list_T *retlist,
928 int add_lnum)
929{
930 char_u *text = ml_get_buf(buf, lnum, FALSE);
931 size_t textlen = STRLEN(text) + 1;
932 int count;
933 int i;
934 textprop_T prop;
935
936 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
937 for (i = 0; i < count; ++i)
938 {
939 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
940 sizeof(textprop_T));
941 if ((prop_types == NULL
942 || prop_type_or_id_in_list(prop_types, prop_types_len,
943 prop.tp_type))
944 && (prop_ids == NULL ||
945 prop_type_or_id_in_list(prop_ids, prop_ids_len,
946 prop.tp_id)))
947 {
948 dict_T *d = dict_alloc();
949
950 if (d == NULL)
951 break;
952 prop_fill_dict(d, &prop, buf);
953 if (add_lnum)
954 dict_add_number(d, "lnum", lnum);
955 list_append_dict(retlist, d);
956 }
957 }
958}
959
960/*
961 * Convert a List of property type names into an array of property type
962 * identifiers. Returns a pointer to the allocated array. Returns NULL on
963 * error. 'num_types' is set to the number of returned property types.
964 */
965 static int *
966get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
967{
968 int *prop_types;
969 listitem_T *li;
970 int i;
971 char_u *name;
972 proptype_T *type;
973
974 *num_types = 0;
975
976 prop_types = ALLOC_MULT(int, list_len(l));
977 if (prop_types == NULL)
978 return NULL;
979
980 i = 0;
981 FOR_ALL_LIST_ITEMS(l, li)
982 {
983 if (li->li_tv.v_type != VAR_STRING)
984 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000985 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000986 goto errret;
987 }
988 name = li->li_tv.vval.v_string;
989 if (name == NULL)
990 goto errret;
991
992 type = lookup_prop_type(name, buf);
993 if (type == NULL)
994 goto errret;
995 prop_types[i++] = type->pt_id;
996 }
997
998 *num_types = i;
999 return prop_types;
1000
1001errret:
1002 VIM_CLEAR(prop_types);
1003 return NULL;
1004}
1005
1006/*
1007 * Convert a List of property identifiers into an array of property
1008 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1009 * error. 'num_ids' is set to the number of returned property identifiers.
1010 */
1011 static int *
1012get_prop_ids_from_list(list_T *l, int *num_ids)
1013{
1014 int *prop_ids;
1015 listitem_T *li;
1016 int i;
1017 int id;
1018 int error;
1019
1020 *num_ids = 0;
1021
1022 prop_ids = ALLOC_MULT(int, list_len(l));
1023 if (prop_ids == NULL)
1024 return NULL;
1025
1026 i = 0;
1027 FOR_ALL_LIST_ITEMS(l, li)
1028 {
1029 error = FALSE;
1030 id = tv_get_number_chk(&li->li_tv, &error);
1031 if (error)
1032 goto errret;
1033
1034 prop_ids[i++] = id;
1035 }
1036
1037 *num_ids = i;
1038 return prop_ids;
1039
1040errret:
1041 VIM_CLEAR(prop_ids);
1042 return NULL;
1043}
1044
1045/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001046 * prop_list({lnum} [, {bufnr}])
1047 */
1048 void
1049f_prop_list(typval_T *argvars, typval_T *rettv)
1050{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001051 linenr_T lnum;
1052 linenr_T start_lnum;
1053 linenr_T end_lnum;
1054 buf_T *buf = curbuf;
1055 int add_lnum = FALSE;
1056 int *prop_types = NULL;
1057 int prop_types_len = 0;
1058 int *prop_ids = NULL;
1059 int prop_ids_len = 0;
1060 list_T *l;
1061 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001062
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001063 if (in_vim9script()
1064 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001065 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001066 return;
1067
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001068 if (rettv_list_alloc(rettv) != OK)
1069 return;
1070
1071 // default: get text properties on current line
1072 start_lnum = tv_get_number(&argvars[0]);
1073 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001074 if (argvars[1].v_type != VAR_UNKNOWN)
1075 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001076 dict_T *d;
1077
1078 if (argvars[1].v_type != VAR_DICT)
1079 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001080 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001081 return;
1082 }
1083 d = argvars[1].vval.v_dict;
1084
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001085 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1086 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001087
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001088 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001089 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001090 if (di->di_tv.v_type != VAR_NUMBER)
1091 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001092 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001093 return;
1094 }
1095 end_lnum = tv_get_number(&di->di_tv);
1096 if (end_lnum < 0)
1097 // negative end_lnum is used as an offset from the last buffer
1098 // line
1099 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1100 else if (end_lnum > buf->b_ml.ml_line_count)
1101 end_lnum = buf->b_ml.ml_line_count;
1102 add_lnum = TRUE;
1103 }
1104 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1105 {
1106 if (di->di_tv.v_type != VAR_LIST)
1107 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001108 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001109 return;
1110 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001111
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001112 l = di->di_tv.vval.v_list;
1113 if (l != NULL && list_len(l) > 0)
1114 {
1115 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1116 if (prop_types == NULL)
1117 return;
1118 }
1119 }
1120 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1121 {
1122 if (di->di_tv.v_type != VAR_LIST)
1123 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001124 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001125 goto errret;
1126 }
1127
1128 l = di->di_tv.vval.v_list;
1129 if (l != NULL && list_len(l) > 0)
1130 {
1131 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1132 if (prop_ids == NULL)
1133 goto errret;
1134 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001135 }
1136 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001137 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1138 || end_lnum < 1 || end_lnum < start_lnum)
1139 emsg(_(e_invalid_range));
1140 else
1141 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1142 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1143 prop_ids, prop_ids_len,
1144 rettv->vval.v_list, add_lnum);
1145
1146errret:
1147 VIM_CLEAR(prop_types);
1148 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001149}
1150
1151/*
1152 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1153 */
1154 void
1155f_prop_remove(typval_T *argvars, typval_T *rettv)
1156{
1157 linenr_T start = 1;
1158 linenr_T end = 0;
1159 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001160 linenr_T first_changed = 0;
1161 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001162 dict_T *dict;
1163 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001164 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001165 int id = -1;
1166 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001167 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001168
1169 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001170
1171 if (in_vim9script()
1172 && (check_for_dict_arg(argvars, 0) == FAIL
1173 || check_for_opt_number_arg(argvars, 1) == FAIL
1174 || (argvars[1].v_type != VAR_UNKNOWN
1175 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1176 return;
1177
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001178 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1179 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001180 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001181 return;
1182 }
1183
1184 if (argvars[1].v_type != VAR_UNKNOWN)
1185 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001186 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001187 end = start;
1188 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001189 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001190 if (start < 1 || end < 1)
1191 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001192 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001193 return;
1194 }
1195 }
1196
1197 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001198 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1199 return;
1200 if (buf->b_ml.ml_mfp == NULL)
1201 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001202
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001203 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001204
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001205 if (dict_has_key(dict, "id"))
Bram Moolenaar8f667172018-12-14 15:38:31 +01001206 id = dict_get_number(dict, (char_u *)"id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001207 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001208 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01001209 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001210 proptype_T *type = lookup_prop_type(name, buf);
1211
1212 if (type == NULL)
1213 return;
1214 type_id = type->pt_id;
1215 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001216 both = dict_get_bool(dict, (char_u *)"both", FALSE);
1217
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001218 if (id == -1 && type_id == -1)
1219 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001220 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001221 return;
1222 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001223 if (both && (id == -1 || type_id == -1))
1224 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001225 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001226 return;
1227 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001228
1229 if (end == 0)
1230 end = buf->b_ml.ml_line_count;
1231 for (lnum = start; lnum <= end; ++lnum)
1232 {
1233 char_u *text;
1234 size_t len;
1235
1236 if (lnum > buf->b_ml.ml_line_count)
1237 break;
1238 text = ml_get_buf(buf, lnum, FALSE);
1239 len = STRLEN(text) + 1;
1240 if ((size_t)buf->b_ml.ml_line_len > len)
1241 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001242 static textprop_T textprop; // static because of alignment
1243 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001244
1245 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1246 / sizeof(textprop_T); ++idx)
1247 {
1248 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1249 + idx * sizeof(textprop_T);
1250 size_t taillen;
1251
1252 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001253 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1254 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001255 {
1256 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1257 {
1258 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1259
1260 // need to allocate the line to be able to change it
1261 if (newptr == NULL)
1262 return;
1263 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1264 buf->b_ml.ml_line_len);
1265 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001266 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1267
1268 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001269 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001270 }
1271
1272 taillen = buf->b_ml.ml_line_len - len
1273 - (idx + 1) * sizeof(textprop_T);
1274 if (taillen > 0)
1275 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1276 taillen);
1277 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1278 --idx;
1279
Bram Moolenaar965c0442021-05-17 00:22:06 +02001280 if (first_changed == 0)
1281 first_changed = lnum;
1282 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001283 ++rettv->vval.v_number;
1284 if (!do_all)
1285 break;
1286 }
1287 }
1288 }
1289 }
Bram Moolenaar965c0442021-05-17 00:22:06 +02001290 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001291 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001292 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1293 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001294 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001295}
1296
1297/*
1298 * Common for f_prop_type_add() and f_prop_type_change().
1299 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001300 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001301prop_type_set(typval_T *argvars, int add)
1302{
1303 char_u *name;
1304 buf_T *buf = NULL;
1305 dict_T *dict;
1306 dictitem_T *di;
1307 proptype_T *prop;
1308
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001309 if (in_vim9script()
1310 && (check_for_string_arg(argvars, 0) == FAIL
1311 || check_for_dict_arg(argvars, 1) == FAIL))
1312 return;
1313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001314 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001315 if (*name == NUL)
1316 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001317 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001318 return;
1319 }
1320
1321 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1322 return;
1323 dict = argvars[1].vval.v_dict;
1324
1325 prop = find_prop(name, buf);
1326 if (add)
1327 {
1328 hashtab_T **htp;
1329
1330 if (prop != NULL)
1331 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001332 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001333 return;
1334 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001335 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001336 if (prop == NULL)
1337 return;
1338 STRCPY(prop->pt_name, name);
1339 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001340 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001341 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1342 if (*htp == NULL)
1343 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001344 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001345 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001346 {
1347 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001348 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001349 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001350 hash_init(*htp);
1351 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001352 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001353 }
1354 else
1355 {
1356 if (prop == NULL)
1357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001358 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001359 return;
1360 }
1361 }
1362
1363 if (dict != NULL)
1364 {
1365 di = dict_find(dict, (char_u *)"highlight", -1);
1366 if (di != NULL)
1367 {
1368 char_u *highlight;
1369 int hl_id = 0;
1370
Bram Moolenaar8f667172018-12-14 15:38:31 +01001371 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001372 if (highlight != NULL && *highlight != NUL)
1373 hl_id = syn_name2id(highlight);
1374 if (hl_id <= 0)
1375 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001376 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001377 highlight == NULL ? (char_u *)"" : highlight);
1378 return;
1379 }
1380 prop->pt_hl_id = hl_id;
1381 }
1382
Bram Moolenaar58187f12019-05-05 16:33:47 +02001383 di = dict_find(dict, (char_u *)"combine", -1);
1384 if (di != NULL)
1385 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001386 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001387 prop->pt_flags |= PT_FLAG_COMBINE;
1388 else
1389 prop->pt_flags &= ~PT_FLAG_COMBINE;
1390 }
1391
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392 di = dict_find(dict, (char_u *)"priority", -1);
1393 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001394 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001395
1396 di = dict_find(dict, (char_u *)"start_incl", -1);
1397 if (di != NULL)
1398 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001399 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001400 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1401 else
1402 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1403 }
1404
1405 di = dict_find(dict, (char_u *)"end_incl", -1);
1406 if (di != NULL)
1407 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001408 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001409 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1410 else
1411 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1412 }
1413 }
1414}
1415
1416/*
1417 * prop_type_add({name}, {props})
1418 */
1419 void
1420f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1421{
1422 prop_type_set(argvars, TRUE);
1423}
1424
1425/*
1426 * prop_type_change({name}, {props})
1427 */
1428 void
1429f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1430{
1431 prop_type_set(argvars, FALSE);
1432}
1433
1434/*
1435 * prop_type_delete({name} [, {bufnr}])
1436 */
1437 void
1438f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1439{
1440 char_u *name;
1441 buf_T *buf = NULL;
1442 hashitem_T *hi;
1443
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001444 if (in_vim9script()
1445 && (check_for_string_arg(argvars, 0) == FAIL
1446 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1447 return;
1448
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001449 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001450 if (*name == NUL)
1451 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001452 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001453 return;
1454 }
1455
1456 if (argvars[1].v_type != VAR_UNKNOWN)
1457 {
1458 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1459 return;
1460 }
1461
1462 hi = find_prop_hi(name, buf);
1463 if (hi != NULL)
1464 {
1465 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001466 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001467
1468 if (buf == NULL)
1469 ht = global_proptypes;
1470 else
1471 ht = buf->b_proptypes;
1472 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001473 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001474 }
1475}
1476
1477/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001478 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001479 */
1480 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001481f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001482{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001483 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001484
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001485 if (in_vim9script()
1486 && (check_for_string_arg(argvars, 0) == FAIL
1487 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1488 return;
1489
1490 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001491 if (*name == NUL)
1492 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001493 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494 return;
1495 }
1496 if (rettv_dict_alloc(rettv) == OK)
1497 {
1498 proptype_T *prop = NULL;
1499 buf_T *buf = NULL;
1500
1501 if (argvars[1].v_type != VAR_UNKNOWN)
1502 {
1503 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1504 return;
1505 }
1506
1507 prop = find_prop(name, buf);
1508 if (prop != NULL)
1509 {
1510 dict_T *d = rettv->vval.v_dict;
1511
1512 if (prop->pt_hl_id > 0)
1513 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1514 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001515 dict_add_number(d, "combine",
1516 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001517 dict_add_number(d, "start_incl",
1518 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1519 dict_add_number(d, "end_incl",
1520 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1521 if (buf != NULL)
1522 dict_add_number(d, "bufnr", buf->b_fnum);
1523 }
1524 }
1525}
1526
1527 static void
1528list_types(hashtab_T *ht, list_T *l)
1529{
1530 long todo;
1531 hashitem_T *hi;
1532
1533 todo = (long)ht->ht_used;
1534 for (hi = ht->ht_array; todo > 0; ++hi)
1535 {
1536 if (!HASHITEM_EMPTY(hi))
1537 {
1538 proptype_T *prop = HI2PT(hi);
1539
1540 list_append_string(l, prop->pt_name, -1);
1541 --todo;
1542 }
1543 }
1544}
1545
1546/*
1547 * prop_type_list([{bufnr}])
1548 */
1549 void
1550f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1551{
1552 buf_T *buf = NULL;
1553
1554 if (rettv_list_alloc(rettv) == OK)
1555 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001556 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1557 return;
1558
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001559 if (argvars[0].v_type != VAR_UNKNOWN)
1560 {
1561 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1562 return;
1563 }
1564 if (buf == NULL)
1565 {
1566 if (global_proptypes != NULL)
1567 list_types(global_proptypes, rettv->vval.v_list);
1568 }
1569 else if (buf->b_proptypes != NULL)
1570 list_types(buf->b_proptypes, rettv->vval.v_list);
1571 }
1572}
1573
1574/*
1575 * Free all property types in "ht".
1576 */
1577 static void
1578clear_ht_prop_types(hashtab_T *ht)
1579{
1580 long todo;
1581 hashitem_T *hi;
1582
1583 if (ht == NULL)
1584 return;
1585
1586 todo = (long)ht->ht_used;
1587 for (hi = ht->ht_array; todo > 0; ++hi)
1588 {
1589 if (!HASHITEM_EMPTY(hi))
1590 {
1591 proptype_T *prop = HI2PT(hi);
1592
1593 vim_free(prop);
1594 --todo;
1595 }
1596 }
1597
1598 hash_clear(ht);
1599 vim_free(ht);
1600}
1601
1602#if defined(EXITFREE) || defined(PROTO)
1603/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001604 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001605 */
1606 void
1607clear_global_prop_types(void)
1608{
1609 clear_ht_prop_types(global_proptypes);
1610 global_proptypes = NULL;
1611}
1612#endif
1613
1614/*
1615 * Free all property types for "buf".
1616 */
1617 void
1618clear_buf_prop_types(buf_T *buf)
1619{
1620 clear_ht_prop_types(buf->b_proptypes);
1621 buf->b_proptypes = NULL;
1622}
1623
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001624// Struct used to return two values from adjust_prop().
1625typedef struct
1626{
1627 int dirty; // if the property was changed
1628 int can_drop; // whether after this change, the prop may be removed
1629} adjustres_T;
1630
1631/*
1632 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1633 *
1634 * Note that "col" is zero-based, while tp_col is one-based.
1635 * Only for the current buffer.
1636 * "flags" can have:
1637 * APC_SUBSTITUTE: Text is replaced, not inserted.
1638 */
1639 static adjustres_T
1640adjust_prop(
1641 textprop_T *prop,
1642 colnr_T col,
1643 int added,
1644 int flags)
1645{
1646 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1647 int start_incl = (pt != NULL
1648 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1649 || (flags & APC_SUBSTITUTE);
1650 int end_incl = (pt != NULL
1651 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1652 // Do not drop zero-width props if they later can increase in
1653 // size.
1654 int droppable = !(start_incl || end_incl);
1655 adjustres_T res = {TRUE, FALSE};
1656
1657 if (added > 0)
1658 {
1659 if (col + 1 <= prop->tp_col
1660 - (start_incl || (prop->tp_len == 0 && end_incl)))
1661 // Change is entirely before the text property: Only shift
1662 prop->tp_col += added;
1663 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1664 // Insertion was inside text property
1665 prop->tp_len += added;
1666 }
1667 else if (prop->tp_col > col + 1)
1668 {
1669 if (prop->tp_col + added < col + 1)
1670 {
1671 prop->tp_len += (prop->tp_col - 1 - col) + added;
1672 prop->tp_col = col + 1;
1673 if (prop->tp_len <= 0)
1674 {
1675 prop->tp_len = 0;
1676 res.can_drop = droppable;
1677 }
1678 }
1679 else
1680 prop->tp_col += added;
1681 }
1682 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1683 {
1684 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1685
1686 prop->tp_len += after > 0 ? added + after : added;
1687 res.can_drop = prop->tp_len <= 0 && droppable;
1688 }
1689 else
1690 res.dirty = FALSE;
1691
1692 return res;
1693}
1694
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001695/*
1696 * Adjust the columns of text properties in line "lnum" after position "col" to
1697 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001698 * Note that "col" is zero-based, while tp_col is one-based.
1699 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001700 * "flags" can have:
1701 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1702 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001703 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001704 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001705 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001706 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001707adjust_prop_columns(
1708 linenr_T lnum,
1709 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001710 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001711 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001712{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001713 int proplen;
1714 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001715 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001716 int ri, wi;
1717 size_t textlen;
1718
1719 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001720 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001721
1722 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1723 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001724 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001725 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001726
Bram Moolenaar196d1572019-01-02 23:47:18 +01001727 wi = 0; // write index
1728 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001729 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001730 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001731 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001732
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001733 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1734 res = adjust_prop(&prop, col, bytes_added, flags);
1735 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001736 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001737 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001738 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1739 && u_savesub(lnum) == FAIL)
1740 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001741 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001742
1743 // u_savesub() may have updated curbuf->b_ml, fetch it again
1744 if (curbuf->b_ml.ml_line_lnum != lnum)
1745 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001746 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001747 if (res.can_drop)
1748 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001749 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001750 ++wi;
1751 }
1752 if (dirty)
1753 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001754 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1755
1756 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1757 curbuf->b_ml.ml_line_ptr =
1758 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001759 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001760 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001761 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001762 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001763}
1764
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001765/*
1766 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001767 * "lnum_props" is the line that has the properties from before the split.
1768 * "lnum_top" is the top line.
1769 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001770 * "deleted" is the number of bytes deleted.
1771 */
1772 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001773adjust_props_for_split(
1774 linenr_T lnum_props,
1775 linenr_T lnum_top,
1776 int kept,
1777 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001778{
1779 char_u *props;
1780 int count;
1781 garray_T prevprop;
1782 garray_T nextprop;
1783 int i;
1784 int skipped = kept + deleted;
1785
1786 if (!curbuf->b_has_textprop)
1787 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001788
1789 // Get the text properties from "lnum_props".
1790 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001791 ga_init2(&prevprop, sizeof(textprop_T), 10);
1792 ga_init2(&nextprop, sizeof(textprop_T), 10);
1793
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001794 // Keep the relevant ones in the first line, reducing the length if needed.
1795 // Copy the ones that include the split to the second line.
1796 // Move the ones after the split to the second line.
1797 for (i = 0; i < count; ++i)
1798 {
1799 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001800 proptype_T *pt;
1801 int start_incl, end_incl;
1802 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001803
1804 // copy the prop to an aligned structure
1805 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1806
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001807 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1808 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1809 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1810 cont_prev = prop.tp_col + !start_incl <= kept;
1811 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1812
1813 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001814 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001815 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1816
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001817 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001818 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001819 if (p->tp_col + p->tp_len >= kept)
1820 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001821 if (cont_next)
1822 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001823 }
1824
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001825 // Only add the property to the next line if the length is bigger than
1826 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001827 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001828 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001829 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001830 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001831 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001832 if (p->tp_col > skipped)
1833 p->tp_col -= skipped - 1;
1834 else
1835 {
1836 p->tp_len -= skipped - p->tp_col;
1837 p->tp_col = 1;
1838 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001839 if (cont_prev)
1840 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001841 }
1842 }
1843
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001844 set_text_props(lnum_top, prevprop.ga_data,
1845 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001846 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001847 set_text_props(lnum_top + 1, nextprop.ga_data,
1848 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001849 ga_clear(&nextprop);
1850}
1851
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001852/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001853 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001854 */
1855 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001856prepend_joined_props(
1857 char_u *new_props,
1858 int propcount,
1859 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001860 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001861 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001862 long col,
1863 int removed)
1864{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001865 char_u *props;
1866 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1867 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001868
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001869 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001870 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001871 textprop_T prop;
1872 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001873
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001874 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1875 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1876
1877 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001878 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001879
1880 if (add_all || end)
1881 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1882 &prop, sizeof(prop));
1883 else
1884 {
1885 int j;
1886 int found = FALSE;
1887
1888 // Search for continuing prop.
1889 for (j = *props_remaining; j < propcount; ++j)
1890 {
1891 textprop_T op;
1892
1893 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1894 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1895 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001896 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001897 found = TRUE;
1898 op.tp_len += op.tp_col - prop.tp_col;
1899 op.tp_col = prop.tp_col;
1900 // Start/end is taken care of when deleting joined lines
1901 op.tp_flags = prop.tp_flags;
1902 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1903 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001904 }
1905 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001906 if (!found)
1907 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001908 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001909 }
1910}
1911
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001912#endif // FEAT_PROP_POPUP