blob: 9a9544c503e17ebe89b8666f4bc1eb576af5d1f5 [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 *
13 * TODO:
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010014 * - Adjust text property column and length when text is inserted/deleted.
Bram Moolenaar4164bb22019-01-04 23:09:49 +010015 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010016 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020017 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010018 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaar32aa1022019-11-02 22:54:41 +010019 * - Add an array for global_proptypes, to quickly lookup a prop type by ID
20 * - Add an array for b_proptypes, to quickly lookup a prop type by ID
Bram Moolenaarb56ac042018-12-28 23:22:40 +010021 * - Checking the text length to detect text properties is slow. Use a flag in
22 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010023 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
24 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010025 * - Perhaps have a window-local option to disable highlighting from text
26 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010027 */
28
29#include "vim.h"
30
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010031#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33/*
34 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
35 * This avoids adding a pointer to the hashtable item.
36 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
37 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
38 * HI2PT() converts a hashitem pointer to a proptype pointer.
39 */
40#define PT2HIKEY(p) ((p)->pt_name)
41#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
42#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
43
44// The global text property types.
45static hashtab_T *global_proptypes = NULL;
46
47// The last used text property type ID.
48static int proptype_id = 0;
49
Bram Moolenaar98aefe72018-12-13 22:20:09 +010050/*
51 * Find a property type by name, return the hashitem.
52 * Returns NULL if the item can't be found.
53 */
54 static hashitem_T *
55find_prop_hi(char_u *name, buf_T *buf)
56{
57 hashtab_T *ht;
58 hashitem_T *hi;
59
60 if (*name == NUL)
61 return NULL;
62 if (buf == NULL)
63 ht = global_proptypes;
64 else
65 ht = buf->b_proptypes;
66
67 if (ht == NULL)
68 return NULL;
69 hi = hash_find(ht, name);
70 if (HASHITEM_EMPTY(hi))
71 return NULL;
72 return hi;
73}
74
75/*
76 * Like find_prop_hi() but return the property type.
77 */
78 static proptype_T *
79find_prop(char_u *name, buf_T *buf)
80{
81 hashitem_T *hi = find_prop_hi(name, buf);
82
83 if (hi == NULL)
84 return NULL;
85 return HI2PT(hi);
86}
87
88/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020089 * Get the prop type ID of "name".
90 * When not found return zero.
91 */
92 int
93find_prop_type_id(char_u *name, buf_T *buf)
94{
95 proptype_T *pt = find_prop(name, buf);
96
97 if (pt == NULL)
98 return 0;
99 return pt->pt_id;
100}
101
102/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100103 * Lookup a property type by name. First in "buf" and when not found in the
104 * global types.
105 * When not found gives an error message and returns NULL.
106 */
107 static proptype_T *
108lookup_prop_type(char_u *name, buf_T *buf)
109{
110 proptype_T *type = find_prop(name, buf);
111
112 if (type == NULL)
113 type = find_prop(name, NULL);
114 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100115 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100116 return type;
117}
118
119/*
120 * Get an optional "bufnr" item from the dict in "arg".
121 * When the argument is not used or "bufnr" is not present then "buf" is
122 * unchanged.
123 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100124 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100125 */
126 static int
127get_bufnr_from_arg(typval_T *arg, buf_T **buf)
128{
129 dictitem_T *di;
130
131 if (arg->v_type != VAR_DICT)
132 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000133 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100134 return FAIL;
135 }
136 if (arg->vval.v_dict == NULL)
137 return OK; // NULL dict is like an empty dict
138 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200139 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
140 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100141 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200142 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143 if (*buf == NULL)
144 return FAIL;
145 }
146 return OK;
147}
148
149/*
150 * prop_add({lnum}, {col}, {props})
151 */
152 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100153f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100154{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100155 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100156 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100157
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200158 if (in_vim9script()
159 && (check_for_number_arg(argvars, 0) == FAIL
160 || check_for_number_arg(argvars, 1) == FAIL
161 || check_for_dict_arg(argvars, 2) == FAIL))
162 return;
163
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100164 start_lnum = tv_get_number(&argvars[0]);
165 start_col = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100166 if (argvars[2].v_type != VAR_DICT)
167 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000168 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100169 return;
170 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200171
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100172 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
173 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200174}
175
176/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200177 * Attach a text property 'type_name' to the text starting
178 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100179 * the buffer "buf" and assign identifier "id".
180 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200181 */
182 static int
183prop_add_one(
184 buf_T *buf,
185 char_u *type_name,
186 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text_arg,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100188 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189 linenr_T start_lnum,
190 linenr_T end_lnum,
191 colnr_T start_col,
192 colnr_T end_col)
193{
194 proptype_T *type;
195 linenr_T lnum;
196 int proplen;
197 char_u *props = NULL;
198 char_u *newprops;
199 size_t textlen;
200 char_u *newtext;
201 int i;
202 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100203 char_u *text = text_arg;
204 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200205
206 type = lookup_prop_type(type_name, buf);
207 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200209
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);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100213 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200214 }
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);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100218 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200219 }
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));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100224 goto theend;
225 }
226
227 if (text != NULL)
228 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 garray_T *gap = &buf->b_textprop_text;
230 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100231
232 // double check we got the right ID
233 if (-id - 1 != gap->ga_len)
234 iemsg("text prop ID mismatch");
235 if (gap->ga_growsize == 0)
236 ga_init2(gap, sizeof(char *), 50);
237 if (ga_grow(gap, 1) == FAIL)
238 goto theend;
239 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100240
241 // change any Tab to a Space to make it simpler to compute the size
242 for (p = text; *p != NUL; MB_PTR_ADV(p))
243 if (*p == TAB)
244 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100245 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200246 }
247
248 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
249 {
250 colnr_T col; // start column
251 long length; // in bytes
252
253 // Fetch the line to get the ml_line_len field updated.
254 proplen = get_text_props(buf, lnum, &props, TRUE);
255 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
256
257 if (lnum == start_lnum)
258 col = start_col;
259 else
260 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100261 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200262 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000263 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100264 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200265 }
266
267 if (lnum == end_lnum)
268 length = end_col - col;
269 else
270 length = (int)textlen - col + 1;
271 if (length > (long)textlen)
272 length = (int)textlen; // can include the end-of-line
273 if (length < 0)
274 length = 0; // zero-width property
275
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100276 if (text_arg != NULL)
277 {
278 length = 1; // text is placed on one character
279 if (col == 0)
280 col = MAXCOL; // after the line
281 }
282
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200283 // Allocate the new line with space for the new property.
284 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
285 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100286 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200287 // Copy the text, including terminating NUL.
288 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
289
290 // Find the index where to insert the new property.
291 // Since the text properties are not aligned properly when stored with
292 // the text, we need to copy them as bytes before using it as a struct.
293 for (i = 0; i < proplen; ++i)
294 {
295 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
296 sizeof(textprop_T));
297 if (tmp_prop.tp_col >= col)
298 break;
299 }
300 newprops = newtext + textlen;
301 if (i > 0)
302 mch_memmove(newprops, props, sizeof(textprop_T) * i);
303
304 tmp_prop.tp_col = col;
305 tmp_prop.tp_len = length;
306 tmp_prop.tp_id = id;
307 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100308 tmp_prop.tp_flags = text_flags
309 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
310 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200311 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
312 sizeof(textprop_T));
313
314 if (i < proplen)
315 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
316 props + i * sizeof(textprop_T),
317 sizeof(textprop_T) * (proplen - i));
318
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100319 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200320 vim_free(buf->b_ml.ml_line_ptr);
321 buf->b_ml.ml_line_ptr = newtext;
322 buf->b_ml.ml_line_len += sizeof(textprop_T);
323 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
324 }
325
326 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100327 res = OK;
328
329theend:
330 vim_free(text);
331 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200332}
333
334/*
335 * prop_add_list()
336 * First argument specifies the text property:
337 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
338 * Second argument is a List where each item is a List with the following
339 * entries: [lnum, start_col, end_col]
340 */
341 void
342f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
343{
344 dict_T *dict;
345 char_u *type_name;
346 buf_T *buf = curbuf;
347 int id = 0;
348 listitem_T *li;
349 list_T *pos_list;
350 linenr_T start_lnum;
351 colnr_T start_col;
352 linenr_T end_lnum;
353 colnr_T end_col;
354 int error = FALSE;
355
356 if (check_for_dict_arg(argvars, 0) == FAIL
357 || check_for_list_arg(argvars, 1) == FAIL)
358 return;
359
360 if (argvars[1].vval.v_list == NULL)
361 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000362 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363 return;
364 }
365
366 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100367 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200368 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000369 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200370 return;
371 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100372 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200373
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100374 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100375 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200376
377 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
378 return;
379
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100380 // This must be done _before_ we start adding properties because property
381 // changes trigger buffer (memline) reorganisation, which needs this flag
382 // to be correctly set.
383 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200384 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
385 {
386 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
387 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000388 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200389 return;
390 }
391
392 pos_list = li->li_tv.vval.v_list;
393 start_lnum = list_find_nr(pos_list, 0L, &error);
394 start_col = list_find_nr(pos_list, 1L, &error);
395 end_lnum = list_find_nr(pos_list, 2L, &error);
396 end_col = list_find_nr(pos_list, 3L, &error);
397 if (error || start_lnum <= 0 || start_col <= 0
398 || end_lnum <= 0 || end_col <= 0)
399 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000400 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200401 return;
402 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100403 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200404 start_col, end_col) == FAIL)
405 return;
406 }
407
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200408 redraw_buf_later(buf, VALID);
409}
410
411/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100412 * Get the next ID to use for a textprop with text in buffer "buf".
413 */
414 static int
415get_textprop_id(buf_T *buf)
416{
417 // TODO: recycle deleted entries
418 return -(buf->b_textprop_text.ga_len + 1);
419}
420
421/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200422 * Shared between prop_add() and popup_create().
423 * "dict_arg" is the function argument of a dict containing "bufnr".
424 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100425 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100427 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200428prop_add_common(
429 linenr_T start_lnum,
430 colnr_T start_col,
431 dict_T *dict,
432 buf_T *default_buf,
433 typval_T *dict_arg)
434{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200435 linenr_T end_lnum;
436 colnr_T end_col;
437 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200438 buf_T *buf = default_buf;
439 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100440 char_u *text = NULL;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100441 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100442
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100443 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100444 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000445 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100446 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100447 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100448 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100449
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100450 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100451 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100452 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100453 if (end_lnum < start_lnum)
454 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000455 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100456 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100457 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100458 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100459 else
460 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100461
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100462 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100463 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100464 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100465
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100466 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100467 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000468 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100469 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100470 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100471 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100472 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100473 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100474 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100475 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100476 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100477 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000478 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100479 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100480 }
481 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100482 else if (start_lnum == end_lnum)
483 end_col = start_col;
484 else
485 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100486
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100487 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100488 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100489
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100490 if (dict_has_key(dict, "text"))
491 {
492 text = dict_get_string(dict, "text", TRUE);
493 if (text == NULL)
494 goto theend;
495 // use a default length of 1 to make multiple props show up
496 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100497
498 if (dict_has_key(dict, "text_align"))
499 {
500 char_u *p = dict_get_string(dict, "text_align", FALSE);
501
502 if (p == NULL)
503 goto theend;
504 if (STRCMP(p, "right") == 0)
505 flags |= TP_FLAG_ALIGN_RIGHT;
506 else if (STRCMP(p, "below") == 0)
507 flags |= TP_FLAG_ALIGN_BELOW;
508 else if (STRCMP(p, "after") != 0)
509 {
510 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
511 goto theend;
512 }
513 }
514
515 if (dict_has_key(dict, "text_wrap"))
516 {
517 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
518 if (p == NULL)
519 goto theend;
520 if (STRCMP(p, "wrap") == 0)
521 flags |= TP_FLAG_WRAP;
522 else if (STRCMP(p, "truncate") != 0)
523 {
524 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
525 goto theend;
526 }
527 }
528 }
529
530 // Column must be 1 or more for a normal text property; when "text" is
531 // present zero means it goes after the line.
532 if (start_col < (text == NULL ? 1 : 0))
533 {
534 semsg(_(e_invalid_column_number_nr), (long)start_col);
535 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100536 }
537
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200538 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100539 goto theend;
540
541 if (id < 0 && buf->b_textprop_text.ga_len > 0)
542 {
543 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
544 goto theend;
545 }
546 if (text != NULL)
547 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100548
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100549 // This must be done _before_ we add the property because property changes
550 // trigger buffer (memline) reorganisation, which needs this flag to be
551 // correctly set.
552 buf->b_has_textprop = TRUE; // this is never reset
553
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100554 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100555 start_lnum, end_lnum, start_col, end_col);
556 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100557
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200558 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100559
560theend:
561 vim_free(text);
562 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100563}
564
565/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100566 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100567 * Returns the number of text properties and, when non-zero, a pointer to the
568 * first one in "props" (note that it is not aligned, therefore the char_u
569 * pointer).
570 */
571 int
572get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
573{
574 char_u *text;
575 size_t textlen;
576 size_t proplen;
577
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100578 // Be quick when no text property types have been defined or the buffer,
579 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200580 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100581 return 0;
582
583 // Fetch the line to get the ml_line_len field updated.
584 text = ml_get_buf(buf, lnum, will_change);
585 textlen = STRLEN(text) + 1;
586 proplen = buf->b_ml.ml_line_len - textlen;
587 if (proplen % sizeof(textprop_T) != 0)
588 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000589 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100590 return 0;
591 }
592 if (proplen > 0)
593 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100594 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100595}
596
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200597/**
598 * Return the number of text properties on line "lnum" in the current buffer.
599 * When "only_starting" is true only text properties starting in this line will
600 * be considered.
601 */
602 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100603count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200604{
605 char_u *props;
606 int proplen = get_text_props(curbuf, lnum, &props, 0);
607 int result = proplen;
608 int i;
609 textprop_T prop;
610
Bram Moolenaare175dc62022-08-01 22:18:50 +0100611 for (i = 0; i < proplen; ++i)
612 {
613 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
614 // A prop is droppend when in the first line and it continues from the
615 // previous line, or when not in the last line and it is virtual text
616 // after the line.
617 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
618 || (!last_line && prop.tp_col == MAXCOL))
619 --result;
620 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200621 return result;
622}
623
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100624/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200625 * Find text property "type_id" in the visible lines of window "wp".
626 * Match "id" when it is > 0.
627 * Returns FAIL when not found.
628 */
629 int
630find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
631 linenr_T *found_lnum)
632{
633 linenr_T lnum;
634 char_u *props;
635 int count;
636 int i;
637
638 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100639 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200640 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
641 {
642 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
643 for (i = 0; i < count; ++i)
644 {
645 mch_memmove(prop, props + i * sizeof(textprop_T),
646 sizeof(textprop_T));
647 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
648 {
649 *found_lnum = lnum;
650 return OK;
651 }
652 }
653 }
654 return FAIL;
655}
656
657/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100658 * Set the text properties for line "lnum" to "props" with length "len".
659 * If "len" is zero text properties are removed, "props" is not used.
660 * Any existing text properties are dropped.
661 * Only works for the current buffer.
662 */
663 static void
664set_text_props(linenr_T lnum, char_u *props, int len)
665{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100666 char_u *text;
667 char_u *newtext;
668 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100669
670 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100671 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100672 newtext = alloc(textlen + len);
673 if (newtext == NULL)
674 return;
675 mch_memmove(newtext, text, textlen);
676 if (len > 0)
677 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100678 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100679 vim_free(curbuf->b_ml.ml_line_ptr);
680 curbuf->b_ml.ml_line_ptr = newtext;
681 curbuf->b_ml.ml_line_len = textlen + len;
682 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
683}
684
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100685 static proptype_T *
686find_type_by_id(hashtab_T *ht, int id)
687{
688 long todo;
689 hashitem_T *hi;
690
691 if (ht == NULL)
692 return NULL;
693
694 // TODO: Make this faster by keeping a list of types sorted on ID and use
695 // a binary search.
696
697 todo = (long)ht->ht_used;
698 for (hi = ht->ht_array; todo > 0; ++hi)
699 {
700 if (!HASHITEM_EMPTY(hi))
701 {
702 proptype_T *prop = HI2PT(hi);
703
704 if (prop->pt_id == id)
705 return prop;
706 --todo;
707 }
708 }
709 return NULL;
710}
711
712/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100713 * Fill 'dict' with text properties in 'prop'.
714 */
715 static void
716prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
717{
718 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200719 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100720
721 dict_add_number(dict, "col", prop->tp_col);
722 dict_add_number(dict, "length", prop->tp_len);
723 dict_add_number(dict, "id", prop->tp_id);
724 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
725 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200726
727 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
728 if (pt == NULL)
729 {
730 pt = find_type_by_id(global_proptypes, prop->tp_type);
731 buflocal = FALSE;
732 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100733 if (pt != NULL)
734 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200735
736 if (buflocal)
737 dict_add_number(dict, "type_bufnr", buf->b_fnum);
738 else
739 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100740}
741
742/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100743 * Find a property type by ID in "buf" or globally.
744 * Returns NULL if not found.
745 */
746 proptype_T *
747text_prop_type_by_id(buf_T *buf, int id)
748{
749 proptype_T *type;
750
751 type = find_type_by_id(buf->b_proptypes, id);
752 if (type == NULL)
753 type = find_type_by_id(global_proptypes, id);
754 return type;
755}
756
757/*
758 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
759 */
760 void
761f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
762{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200763 linenr_T start;
764 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100765 linenr_T lnum;
766 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100767 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100768
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200769 if (in_vim9script()
770 && (check_for_number_arg(argvars, 0) == FAIL
771 || check_for_opt_number_arg(argvars, 1) == FAIL
772 || (argvars[1].v_type != VAR_UNKNOWN
773 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
774 return;
775
776 start = tv_get_number(&argvars[0]);
777 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100778 if (argvars[1].v_type != VAR_UNKNOWN)
779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100780 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100781 if (argvars[2].v_type != VAR_UNKNOWN)
782 {
783 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
784 return;
785 }
786 }
787 if (start < 1 || end < 1)
788 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200789 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100790 return;
791 }
792
793 for (lnum = start; lnum <= end; ++lnum)
794 {
795 char_u *text;
796 size_t len;
797
798 if (lnum > buf->b_ml.ml_line_count)
799 break;
800 text = ml_get_buf(buf, lnum, FALSE);
801 len = STRLEN(text) + 1;
802 if ((size_t)buf->b_ml.ml_line_len > len)
803 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100804 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100805 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
806 {
807 char_u *newtext = vim_strsave(text);
808
809 // need to allocate the line now
810 if (newtext == NULL)
811 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100812 if (buf->b_ml.ml_flags & ML_ALLOCATED)
813 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100814 buf->b_ml.ml_line_ptr = newtext;
815 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
816 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100817 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100818 }
819 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100820 if (did_clear)
821 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100822}
823
824/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100825 * prop_find({props} [, {direction}])
826 */
827 void
828f_prop_find(typval_T *argvars, typval_T *rettv)
829{
830 pos_T *cursor = &curwin->w_cursor;
831 dict_T *dict;
832 buf_T *buf = curbuf;
833 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200834 int lnum_start;
835 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100836 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200837 int id = 0;
838 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200839 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100840 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200841 int lnum = -1;
842 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100843 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100844 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100845
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200846 if (in_vim9script()
847 && (check_for_dict_arg(argvars, 0) == FAIL
848 || check_for_opt_string_arg(argvars, 1) == FAIL))
849 return;
850
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100851 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
852 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000853 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100854 return;
855 }
856 dict = argvars[0].vval.v_dict;
857
858 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
859 return;
860 if (buf->b_ml.ml_mfp == NULL)
861 return;
862
863 if (argvars[1].v_type != VAR_UNKNOWN)
864 {
865 char_u *dir_s = tv_get_string(&argvars[1]);
866
867 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100868 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100869 else if (*dir_s != 'f')
870 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000871 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100872 return;
873 }
874 }
875
876 di = dict_find(dict, (char_u *)"lnum", -1);
877 if (di != NULL)
878 lnum = tv_get_number(&di->di_tv);
879
880 di = dict_find(dict, (char_u *)"col", -1);
881 if (di != NULL)
882 col = tv_get_number(&di->di_tv);
883
884 if (lnum == -1)
885 {
886 lnum = cursor->lnum;
887 col = cursor->col + 1;
888 }
889 else if (col == -1)
890 col = 1;
891
892 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
893 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200894 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100895 return;
896 }
897
Bram Moolenaard61efa52022-07-23 09:52:04 +0100898 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100899
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100900 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200901 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100902 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200903 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200904 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100905 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100906 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100907 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100908 proptype_T *type = lookup_prop_type(name, buf);
909
910 if (type == NULL)
911 return;
912 type_id = type->pt_id;
913 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100914 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200915 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100916 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000917 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100918 return;
919 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200920 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100921 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000922 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100923 return;
924 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100925
926 lnum_start = lnum;
927
928 if (rettv_dict_alloc(rettv) == FAIL)
929 return;
930
931 while (1)
932 {
933 char_u *text = ml_get_buf(buf, lnum, FALSE);
934 size_t textlen = STRLEN(text) + 1;
935 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200936 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100937 int i;
938 textprop_T prop;
939 int prop_start;
940 int prop_end;
941
LemonBoy9bd3ce22022-04-18 21:54:02 +0100942 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100943 {
944 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100945 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100946
LemonBoy9bd3ce22022-04-18 21:54:02 +0100947 // For the very first line try to find the first property before or
948 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100949 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100950 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100951 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100952 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100953 if (prop.tp_col > col)
954 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100955 }
956 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
957 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100958 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100959 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200960 : (id_found && prop.tp_id == id)
961 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100962 {
963 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100964 if (lnum_start == lnum
965 && col >= prop.tp_col
966 && (col <= prop.tp_col + prop.tp_len
967 - (prop.tp_len != 0)))
968 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100969
LemonBoy9bd3ce22022-04-18 21:54:02 +0100970 // The property was not continued from last line, it starts on
971 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100972 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100973 // The property does not continue on the next line, it ends on
974 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100975 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100976 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100977 seen_end = 1;
978
979 // Skip lines without the start flag.
980 if (!prop_start)
981 {
982 // Always search backwards for start when search started
983 // on a prop and we're not skipping.
984 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +0100985 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200986 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100987 }
988
989 // If skipstart is true, skip the prop at start pos (even if
990 // continued from another line).
991 if (start_pos_has_prop && skipstart && !seen_end)
992 {
993 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200994 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100995 }
996
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100997 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
998 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
999
1000 return;
1001 }
1002 }
1003
1004 if (dir > 0)
1005 {
1006 if (lnum >= buf->b_ml.ml_line_count)
1007 break;
1008 lnum++;
1009 }
1010 else
1011 {
1012 if (lnum <= 1)
1013 break;
1014 lnum--;
1015 }
1016 }
1017}
1018
1019/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001020 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1021 */
1022 static int
1023prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1024{
1025 int i;
1026
1027 for (i = 0; i < len; i++)
1028 if (types_or_ids[i] == type_or_id)
1029 return TRUE;
1030
1031 return FALSE;
1032}
1033
1034/*
1035 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1036 * If 'prop_types' is not NULL, then return only the text properties with
1037 * matching property type in the 'prop_types' array.
1038 * If 'prop_ids' is not NULL, then return only the text properties with
1039 * an identifier in the 'props_ids' array.
1040 * If 'add_lnum' is TRUE, then add the line number also to the text property
1041 * dictionary.
1042 */
1043 static void
1044get_props_in_line(
1045 buf_T *buf,
1046 linenr_T lnum,
1047 int *prop_types,
1048 int prop_types_len,
1049 int *prop_ids,
1050 int prop_ids_len,
1051 list_T *retlist,
1052 int add_lnum)
1053{
1054 char_u *text = ml_get_buf(buf, lnum, FALSE);
1055 size_t textlen = STRLEN(text) + 1;
1056 int count;
1057 int i;
1058 textprop_T prop;
1059
1060 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1061 for (i = 0; i < count; ++i)
1062 {
1063 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1064 sizeof(textprop_T));
1065 if ((prop_types == NULL
1066 || prop_type_or_id_in_list(prop_types, prop_types_len,
1067 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001068 && (prop_ids == NULL
1069 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1070 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001071 {
1072 dict_T *d = dict_alloc();
1073
1074 if (d == NULL)
1075 break;
1076 prop_fill_dict(d, &prop, buf);
1077 if (add_lnum)
1078 dict_add_number(d, "lnum", lnum);
1079 list_append_dict(retlist, d);
1080 }
1081 }
1082}
1083
1084/*
1085 * Convert a List of property type names into an array of property type
1086 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1087 * error. 'num_types' is set to the number of returned property types.
1088 */
1089 static int *
1090get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1091{
1092 int *prop_types;
1093 listitem_T *li;
1094 int i;
1095 char_u *name;
1096 proptype_T *type;
1097
1098 *num_types = 0;
1099
1100 prop_types = ALLOC_MULT(int, list_len(l));
1101 if (prop_types == NULL)
1102 return NULL;
1103
1104 i = 0;
1105 FOR_ALL_LIST_ITEMS(l, li)
1106 {
1107 if (li->li_tv.v_type != VAR_STRING)
1108 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001109 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001110 goto errret;
1111 }
1112 name = li->li_tv.vval.v_string;
1113 if (name == NULL)
1114 goto errret;
1115
1116 type = lookup_prop_type(name, buf);
1117 if (type == NULL)
1118 goto errret;
1119 prop_types[i++] = type->pt_id;
1120 }
1121
1122 *num_types = i;
1123 return prop_types;
1124
1125errret:
1126 VIM_CLEAR(prop_types);
1127 return NULL;
1128}
1129
1130/*
1131 * Convert a List of property identifiers into an array of property
1132 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1133 * error. 'num_ids' is set to the number of returned property identifiers.
1134 */
1135 static int *
1136get_prop_ids_from_list(list_T *l, int *num_ids)
1137{
1138 int *prop_ids;
1139 listitem_T *li;
1140 int i;
1141 int id;
1142 int error;
1143
1144 *num_ids = 0;
1145
1146 prop_ids = ALLOC_MULT(int, list_len(l));
1147 if (prop_ids == NULL)
1148 return NULL;
1149
1150 i = 0;
1151 FOR_ALL_LIST_ITEMS(l, li)
1152 {
1153 error = FALSE;
1154 id = tv_get_number_chk(&li->li_tv, &error);
1155 if (error)
1156 goto errret;
1157
1158 prop_ids[i++] = id;
1159 }
1160
1161 *num_ids = i;
1162 return prop_ids;
1163
1164errret:
1165 VIM_CLEAR(prop_ids);
1166 return NULL;
1167}
1168
1169/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001170 * prop_list({lnum} [, {bufnr}])
1171 */
1172 void
1173f_prop_list(typval_T *argvars, typval_T *rettv)
1174{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001175 linenr_T lnum;
1176 linenr_T start_lnum;
1177 linenr_T end_lnum;
1178 buf_T *buf = curbuf;
1179 int add_lnum = FALSE;
1180 int *prop_types = NULL;
1181 int prop_types_len = 0;
1182 int *prop_ids = NULL;
1183 int prop_ids_len = 0;
1184 list_T *l;
1185 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001186
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001187 if (in_vim9script()
1188 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001189 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001190 return;
1191
Bram Moolenaar93a10962022-06-16 11:42:09 +01001192 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001193 return;
1194
1195 // default: get text properties on current line
1196 start_lnum = tv_get_number(&argvars[0]);
1197 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001198 if (argvars[1].v_type != VAR_UNKNOWN)
1199 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001200 dict_T *d;
1201
1202 if (argvars[1].v_type != VAR_DICT)
1203 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001204 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001205 return;
1206 }
1207 d = argvars[1].vval.v_dict;
1208
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001209 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1210 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001211
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001212 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001213 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001214 if (di->di_tv.v_type != VAR_NUMBER)
1215 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001216 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001217 return;
1218 }
1219 end_lnum = tv_get_number(&di->di_tv);
1220 if (end_lnum < 0)
1221 // negative end_lnum is used as an offset from the last buffer
1222 // line
1223 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1224 else if (end_lnum > buf->b_ml.ml_line_count)
1225 end_lnum = buf->b_ml.ml_line_count;
1226 add_lnum = TRUE;
1227 }
1228 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1229 {
1230 if (di->di_tv.v_type != VAR_LIST)
1231 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001232 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001233 return;
1234 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001235
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001236 l = di->di_tv.vval.v_list;
1237 if (l != NULL && list_len(l) > 0)
1238 {
1239 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1240 if (prop_types == NULL)
1241 return;
1242 }
1243 }
1244 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1245 {
1246 if (di->di_tv.v_type != VAR_LIST)
1247 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001248 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001249 goto errret;
1250 }
1251
1252 l = di->di_tv.vval.v_list;
1253 if (l != NULL && list_len(l) > 0)
1254 {
1255 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1256 if (prop_ids == NULL)
1257 goto errret;
1258 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001259 }
1260 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001261 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1262 || end_lnum < 1 || end_lnum < start_lnum)
1263 emsg(_(e_invalid_range));
1264 else
1265 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1266 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1267 prop_ids, prop_ids_len,
1268 rettv->vval.v_list, add_lnum);
1269
1270errret:
1271 VIM_CLEAR(prop_types);
1272 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001273}
1274
1275/*
1276 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1277 */
1278 void
1279f_prop_remove(typval_T *argvars, typval_T *rettv)
1280{
1281 linenr_T start = 1;
1282 linenr_T end = 0;
1283 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001284 linenr_T first_changed = 0;
1285 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001286 dict_T *dict;
1287 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001288 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001289 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001290 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001291 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001292 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001293
1294 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001295
1296 if (in_vim9script()
1297 && (check_for_dict_arg(argvars, 0) == FAIL
1298 || check_for_opt_number_arg(argvars, 1) == FAIL
1299 || (argvars[1].v_type != VAR_UNKNOWN
1300 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1301 return;
1302
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001303 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1304 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001305 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001306 return;
1307 }
1308
1309 if (argvars[1].v_type != VAR_UNKNOWN)
1310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001311 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001312 end = start;
1313 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001314 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001315 if (start < 1 || end < 1)
1316 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001317 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001318 return;
1319 }
1320 }
1321
1322 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001323 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1324 return;
1325 if (buf->b_ml.ml_mfp == NULL)
1326 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001327
Bram Moolenaard61efa52022-07-23 09:52:04 +01001328 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001329
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001330 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001331 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001332 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001333 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001334 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001335 proptype_T *type = lookup_prop_type(name, buf);
1336
1337 if (type == NULL)
1338 return;
1339 type_id = type->pt_id;
1340 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001341 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001342
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001343 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001344 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001345 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001346 return;
1347 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001348 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001349 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001350 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001351 return;
1352 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001353
1354 if (end == 0)
1355 end = buf->b_ml.ml_line_count;
1356 for (lnum = start; lnum <= end; ++lnum)
1357 {
1358 char_u *text;
1359 size_t len;
1360
1361 if (lnum > buf->b_ml.ml_line_count)
1362 break;
1363 text = ml_get_buf(buf, lnum, FALSE);
1364 len = STRLEN(text) + 1;
1365 if ((size_t)buf->b_ml.ml_line_len > len)
1366 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001367 static textprop_T textprop; // static because of alignment
1368 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001369
1370 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1371 / sizeof(textprop_T); ++idx)
1372 {
1373 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1374 + idx * sizeof(textprop_T);
1375 size_t taillen;
1376
1377 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001378 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1379 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001380 {
1381 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1382 {
1383 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1384
1385 // need to allocate the line to be able to change it
1386 if (newptr == NULL)
1387 return;
1388 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1389 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001390 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1391 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001393 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1394
1395 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001396 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001397 }
1398
1399 taillen = buf->b_ml.ml_line_len - len
1400 - (idx + 1) * sizeof(textprop_T);
1401 if (taillen > 0)
1402 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1403 taillen);
1404 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1405 --idx;
1406
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001407 if (textprop.tp_id < 0)
1408 {
1409 garray_T *gap = &buf->b_textprop_text;
1410 int ii = -textprop.tp_id - 1;
1411
1412 // negative ID: property with text - free the text
1413 if (ii < gap->ga_len)
1414 {
1415 char_u **p = ((char_u **)gap->ga_data) + ii;
1416 vim_free(*p);
1417 *p = NULL;
1418 did_remove_text = TRUE;
1419 }
1420 }
1421
Bram Moolenaar965c0442021-05-17 00:22:06 +02001422 if (first_changed == 0)
1423 first_changed = lnum;
1424 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001425 ++rettv->vval.v_number;
1426 if (!do_all)
1427 break;
1428 }
1429 }
1430 }
1431 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001432
Bram Moolenaar965c0442021-05-17 00:22:06 +02001433 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001434 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001435 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1436 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001437 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001438
1439 if (did_remove_text)
1440 {
1441 garray_T *gap = &buf->b_textprop_text;
1442
1443 // Reduce the growarray size for NULL pointers at the end.
1444 while (gap->ga_len > 0
1445 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1446 --gap->ga_len;
1447 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001448}
1449
1450/*
1451 * Common for f_prop_type_add() and f_prop_type_change().
1452 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001453 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001454prop_type_set(typval_T *argvars, int add)
1455{
1456 char_u *name;
1457 buf_T *buf = NULL;
1458 dict_T *dict;
1459 dictitem_T *di;
1460 proptype_T *prop;
1461
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001462 if (in_vim9script()
1463 && (check_for_string_arg(argvars, 0) == FAIL
1464 || check_for_dict_arg(argvars, 1) == FAIL))
1465 return;
1466
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001467 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001468 if (*name == NUL)
1469 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001470 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001471 return;
1472 }
1473
1474 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1475 return;
1476 dict = argvars[1].vval.v_dict;
1477
1478 prop = find_prop(name, buf);
1479 if (add)
1480 {
1481 hashtab_T **htp;
1482
1483 if (prop != NULL)
1484 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001485 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001486 return;
1487 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001488 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001489 if (prop == NULL)
1490 return;
1491 STRCPY(prop->pt_name, name);
1492 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001493 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1495 if (*htp == NULL)
1496 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001497 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001498 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001499 {
1500 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001501 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001502 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001503 hash_init(*htp);
1504 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001505 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001506 }
1507 else
1508 {
1509 if (prop == NULL)
1510 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001511 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001512 return;
1513 }
1514 }
1515
1516 if (dict != NULL)
1517 {
1518 di = dict_find(dict, (char_u *)"highlight", -1);
1519 if (di != NULL)
1520 {
1521 char_u *highlight;
1522 int hl_id = 0;
1523
Bram Moolenaard61efa52022-07-23 09:52:04 +01001524 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001525 if (highlight != NULL && *highlight != NUL)
1526 hl_id = syn_name2id(highlight);
1527 if (hl_id <= 0)
1528 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001529 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001530 highlight == NULL ? (char_u *)"" : highlight);
1531 return;
1532 }
1533 prop->pt_hl_id = hl_id;
1534 }
1535
Bram Moolenaar58187f12019-05-05 16:33:47 +02001536 di = dict_find(dict, (char_u *)"combine", -1);
1537 if (di != NULL)
1538 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001539 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001540 prop->pt_flags |= PT_FLAG_COMBINE;
1541 else
1542 prop->pt_flags &= ~PT_FLAG_COMBINE;
1543 }
1544
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001545 di = dict_find(dict, (char_u *)"priority", -1);
1546 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001547 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001548
1549 di = dict_find(dict, (char_u *)"start_incl", -1);
1550 if (di != NULL)
1551 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001552 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001553 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1554 else
1555 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1556 }
1557
1558 di = dict_find(dict, (char_u *)"end_incl", -1);
1559 if (di != NULL)
1560 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001561 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001562 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1563 else
1564 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1565 }
1566 }
1567}
1568
1569/*
1570 * prop_type_add({name}, {props})
1571 */
1572 void
1573f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1574{
1575 prop_type_set(argvars, TRUE);
1576}
1577
1578/*
1579 * prop_type_change({name}, {props})
1580 */
1581 void
1582f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1583{
1584 prop_type_set(argvars, FALSE);
1585}
1586
1587/*
1588 * prop_type_delete({name} [, {bufnr}])
1589 */
1590 void
1591f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1592{
1593 char_u *name;
1594 buf_T *buf = NULL;
1595 hashitem_T *hi;
1596
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001597 if (in_vim9script()
1598 && (check_for_string_arg(argvars, 0) == FAIL
1599 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1600 return;
1601
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001602 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001603 if (*name == NUL)
1604 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001605 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001606 return;
1607 }
1608
1609 if (argvars[1].v_type != VAR_UNKNOWN)
1610 {
1611 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1612 return;
1613 }
1614
1615 hi = find_prop_hi(name, buf);
1616 if (hi != NULL)
1617 {
1618 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001619 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001620
1621 if (buf == NULL)
1622 ht = global_proptypes;
1623 else
1624 ht = buf->b_proptypes;
1625 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001626 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001627 }
1628}
1629
1630/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001631 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001632 */
1633 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001634f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001635{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001636 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001637
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001638 if (in_vim9script()
1639 && (check_for_string_arg(argvars, 0) == FAIL
1640 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1641 return;
1642
1643 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001644 if (*name == NUL)
1645 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001646 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001647 return;
1648 }
1649 if (rettv_dict_alloc(rettv) == OK)
1650 {
1651 proptype_T *prop = NULL;
1652 buf_T *buf = NULL;
1653
1654 if (argvars[1].v_type != VAR_UNKNOWN)
1655 {
1656 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1657 return;
1658 }
1659
1660 prop = find_prop(name, buf);
1661 if (prop != NULL)
1662 {
1663 dict_T *d = rettv->vval.v_dict;
1664
1665 if (prop->pt_hl_id > 0)
1666 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1667 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001668 dict_add_number(d, "combine",
1669 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001670 dict_add_number(d, "start_incl",
1671 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1672 dict_add_number(d, "end_incl",
1673 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1674 if (buf != NULL)
1675 dict_add_number(d, "bufnr", buf->b_fnum);
1676 }
1677 }
1678}
1679
1680 static void
1681list_types(hashtab_T *ht, list_T *l)
1682{
1683 long todo;
1684 hashitem_T *hi;
1685
1686 todo = (long)ht->ht_used;
1687 for (hi = ht->ht_array; todo > 0; ++hi)
1688 {
1689 if (!HASHITEM_EMPTY(hi))
1690 {
1691 proptype_T *prop = HI2PT(hi);
1692
1693 list_append_string(l, prop->pt_name, -1);
1694 --todo;
1695 }
1696 }
1697}
1698
1699/*
1700 * prop_type_list([{bufnr}])
1701 */
1702 void
1703f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1704{
1705 buf_T *buf = NULL;
1706
1707 if (rettv_list_alloc(rettv) == OK)
1708 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001709 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1710 return;
1711
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001712 if (argvars[0].v_type != VAR_UNKNOWN)
1713 {
1714 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1715 return;
1716 }
1717 if (buf == NULL)
1718 {
1719 if (global_proptypes != NULL)
1720 list_types(global_proptypes, rettv->vval.v_list);
1721 }
1722 else if (buf->b_proptypes != NULL)
1723 list_types(buf->b_proptypes, rettv->vval.v_list);
1724 }
1725}
1726
1727/*
1728 * Free all property types in "ht".
1729 */
1730 static void
1731clear_ht_prop_types(hashtab_T *ht)
1732{
1733 long todo;
1734 hashitem_T *hi;
1735
1736 if (ht == NULL)
1737 return;
1738
1739 todo = (long)ht->ht_used;
1740 for (hi = ht->ht_array; todo > 0; ++hi)
1741 {
1742 if (!HASHITEM_EMPTY(hi))
1743 {
1744 proptype_T *prop = HI2PT(hi);
1745
1746 vim_free(prop);
1747 --todo;
1748 }
1749 }
1750
1751 hash_clear(ht);
1752 vim_free(ht);
1753}
1754
1755#if defined(EXITFREE) || defined(PROTO)
1756/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001757 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001758 */
1759 void
1760clear_global_prop_types(void)
1761{
1762 clear_ht_prop_types(global_proptypes);
1763 global_proptypes = NULL;
1764}
1765#endif
1766
1767/*
1768 * Free all property types for "buf".
1769 */
1770 void
1771clear_buf_prop_types(buf_T *buf)
1772{
1773 clear_ht_prop_types(buf->b_proptypes);
1774 buf->b_proptypes = NULL;
1775}
1776
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001777// Struct used to return two values from adjust_prop().
1778typedef struct
1779{
1780 int dirty; // if the property was changed
1781 int can_drop; // whether after this change, the prop may be removed
1782} adjustres_T;
1783
1784/*
1785 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1786 *
1787 * Note that "col" is zero-based, while tp_col is one-based.
1788 * Only for the current buffer.
1789 * "flags" can have:
1790 * APC_SUBSTITUTE: Text is replaced, not inserted.
1791 */
1792 static adjustres_T
1793adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001794 textprop_T *prop,
1795 colnr_T col,
1796 int added,
1797 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001798{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001799 proptype_T *pt;
1800 int start_incl;
1801 int end_incl;
1802 int droppable;
1803 adjustres_T res = {TRUE, FALSE};
1804
1805 // prop after end of the line doesn't move
1806 if (prop->tp_col == MAXCOL)
1807 {
1808 res.dirty = FALSE;
1809 return res;
1810 }
1811
1812 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1813 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001814 || (flags & APC_SUBSTITUTE)
1815 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001816 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001817 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001818 // do not drop zero-width props if they later can increase in size
1819 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001820
1821 if (added > 0)
1822 {
1823 if (col + 1 <= prop->tp_col
1824 - (start_incl || (prop->tp_len == 0 && end_incl)))
1825 // Change is entirely before the text property: Only shift
1826 prop->tp_col += added;
1827 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1828 // Insertion was inside text property
1829 prop->tp_len += added;
1830 }
1831 else if (prop->tp_col > col + 1)
1832 {
1833 if (prop->tp_col + added < col + 1)
1834 {
1835 prop->tp_len += (prop->tp_col - 1 - col) + added;
1836 prop->tp_col = col + 1;
1837 if (prop->tp_len <= 0)
1838 {
1839 prop->tp_len = 0;
1840 res.can_drop = droppable;
1841 }
1842 }
1843 else
1844 prop->tp_col += added;
1845 }
1846 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1847 {
1848 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1849
1850 prop->tp_len += after > 0 ? added + after : added;
1851 res.can_drop = prop->tp_len <= 0 && droppable;
1852 }
1853 else
1854 res.dirty = FALSE;
1855
1856 return res;
1857}
1858
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001859/*
1860 * Adjust the columns of text properties in line "lnum" after position "col" to
1861 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001862 * Note that "col" is zero-based, while tp_col is one-based.
1863 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001864 * "flags" can have:
1865 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1866 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001867 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001868 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001869 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001870 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001871adjust_prop_columns(
1872 linenr_T lnum,
1873 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001874 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001875 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001876{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001877 int proplen;
1878 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001879 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001880 int ri, wi;
1881 size_t textlen;
1882
1883 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001884 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001885
1886 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1887 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001888 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001889 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001890
Bram Moolenaar196d1572019-01-02 23:47:18 +01001891 wi = 0; // write index
1892 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001893 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001894 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001895 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001896
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001897 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1898 res = adjust_prop(&prop, col, bytes_added, flags);
1899 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001900 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001901 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001902 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1903 && u_savesub(lnum) == FAIL)
1904 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001905 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001906
1907 // u_savesub() may have updated curbuf->b_ml, fetch it again
1908 if (curbuf->b_ml.ml_line_lnum != lnum)
1909 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001910 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001911 if (res.can_drop)
1912 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001913 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001914 ++wi;
1915 }
1916 if (dirty)
1917 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001918 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1919
1920 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001921 {
1922 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1923
1924 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1925 vim_free(curbuf->b_ml.ml_line_ptr);
1926 curbuf->b_ml.ml_line_ptr = p;
1927 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001928 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001929 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001930 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001931 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001932}
1933
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001934/*
1935 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001936 * "lnum_props" is the line that has the properties from before the split.
1937 * "lnum_top" is the top line.
1938 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001939 * "deleted" is the number of bytes deleted.
1940 */
1941 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001942adjust_props_for_split(
1943 linenr_T lnum_props,
1944 linenr_T lnum_top,
1945 int kept,
1946 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001947{
1948 char_u *props;
1949 int count;
1950 garray_T prevprop;
1951 garray_T nextprop;
1952 int i;
1953 int skipped = kept + deleted;
1954
1955 if (!curbuf->b_has_textprop)
1956 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001957
1958 // Get the text properties from "lnum_props".
1959 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001960 ga_init2(&prevprop, sizeof(textprop_T), 10);
1961 ga_init2(&nextprop, sizeof(textprop_T), 10);
1962
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001963 // Keep the relevant ones in the first line, reducing the length if needed.
1964 // Copy the ones that include the split to the second line.
1965 // Move the ones after the split to the second line.
1966 for (i = 0; i < count; ++i)
1967 {
1968 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001969 proptype_T *pt;
1970 int start_incl, end_incl;
1971 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001972
1973 // copy the prop to an aligned structure
1974 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1975
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001976 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1977 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1978 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1979 cont_prev = prop.tp_col + !start_incl <= kept;
1980 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1981
1982 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001983 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001984 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1985
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001986 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001987 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001988 if (p->tp_col + p->tp_len >= kept)
1989 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001990 if (cont_next)
1991 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001992 }
1993
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001994 // Only add the property to the next line if the length is bigger than
1995 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001996 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001997 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001998 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001999 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002000 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002001 if (p->tp_col > skipped)
2002 p->tp_col -= skipped - 1;
2003 else
2004 {
2005 p->tp_len -= skipped - p->tp_col;
2006 p->tp_col = 1;
2007 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002008 if (cont_prev)
2009 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002010 }
2011 }
2012
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002013 set_text_props(lnum_top, prevprop.ga_data,
2014 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002015 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002016 set_text_props(lnum_top + 1, nextprop.ga_data,
2017 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002018 ga_clear(&nextprop);
2019}
2020
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002021/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002022 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002023 */
2024 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002025prepend_joined_props(
2026 char_u *new_props,
2027 int propcount,
2028 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002029 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002030 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002031 long col,
2032 int removed)
2033{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002034 char_u *props;
2035 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2036 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002037
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002038 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002039 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002040 textprop_T prop;
2041 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002042
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002043 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002044 if (prop.tp_col == MAXCOL && !last_line)
2045 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002046 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2047
2048 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002049 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002050
Bram Moolenaare175dc62022-08-01 22:18:50 +01002051 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002052 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2053 &prop, sizeof(prop));
2054 else
2055 {
2056 int j;
2057 int found = FALSE;
2058
2059 // Search for continuing prop.
2060 for (j = *props_remaining; j < propcount; ++j)
2061 {
2062 textprop_T op;
2063
2064 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2065 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2066 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002067 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002068 found = TRUE;
2069 op.tp_len += op.tp_col - prop.tp_col;
2070 op.tp_col = prop.tp_col;
2071 // Start/end is taken care of when deleting joined lines
2072 op.tp_flags = prop.tp_flags;
2073 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2074 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002075 }
2076 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002077 if (!found)
2078 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002079 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002080 }
2081}
2082
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002083#endif // FEAT_PROP_POPUP