blob: f544a3ce5a1701a9ae96e3cc32217115aba770a9 [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 {
229 garray_T *gap = &buf->b_textprop_text;
230
231 // double check we got the right ID
232 if (-id - 1 != gap->ga_len)
233 iemsg("text prop ID mismatch");
234 if (gap->ga_growsize == 0)
235 ga_init2(gap, sizeof(char *), 50);
236 if (ga_grow(gap, 1) == FAIL)
237 goto theend;
238 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
239 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200240 }
241
242 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
243 {
244 colnr_T col; // start column
245 long length; // in bytes
246
247 // Fetch the line to get the ml_line_len field updated.
248 proplen = get_text_props(buf, lnum, &props, TRUE);
249 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
250
251 if (lnum == start_lnum)
252 col = start_col;
253 else
254 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100255 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200256 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000257 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100258 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200259 }
260
261 if (lnum == end_lnum)
262 length = end_col - col;
263 else
264 length = (int)textlen - col + 1;
265 if (length > (long)textlen)
266 length = (int)textlen; // can include the end-of-line
267 if (length < 0)
268 length = 0; // zero-width property
269
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100270 if (text_arg != NULL)
271 {
272 length = 1; // text is placed on one character
273 if (col == 0)
274 col = MAXCOL; // after the line
275 }
276
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200277 // Allocate the new line with space for the new property.
278 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
279 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100280 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200281 // Copy the text, including terminating NUL.
282 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
283
284 // Find the index where to insert the new property.
285 // Since the text properties are not aligned properly when stored with
286 // the text, we need to copy them as bytes before using it as a struct.
287 for (i = 0; i < proplen; ++i)
288 {
289 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
290 sizeof(textprop_T));
291 if (tmp_prop.tp_col >= col)
292 break;
293 }
294 newprops = newtext + textlen;
295 if (i > 0)
296 mch_memmove(newprops, props, sizeof(textprop_T) * i);
297
298 tmp_prop.tp_col = col;
299 tmp_prop.tp_len = length;
300 tmp_prop.tp_id = id;
301 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100302 tmp_prop.tp_flags = text_flags
303 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
304 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200305 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
306 sizeof(textprop_T));
307
308 if (i < proplen)
309 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
310 props + i * sizeof(textprop_T),
311 sizeof(textprop_T) * (proplen - i));
312
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100313 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200314 vim_free(buf->b_ml.ml_line_ptr);
315 buf->b_ml.ml_line_ptr = newtext;
316 buf->b_ml.ml_line_len += sizeof(textprop_T);
317 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
318 }
319
320 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100321 res = OK;
322
323theend:
324 vim_free(text);
325 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200326}
327
328/*
329 * prop_add_list()
330 * First argument specifies the text property:
331 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
332 * Second argument is a List where each item is a List with the following
333 * entries: [lnum, start_col, end_col]
334 */
335 void
336f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
337{
338 dict_T *dict;
339 char_u *type_name;
340 buf_T *buf = curbuf;
341 int id = 0;
342 listitem_T *li;
343 list_T *pos_list;
344 linenr_T start_lnum;
345 colnr_T start_col;
346 linenr_T end_lnum;
347 colnr_T end_col;
348 int error = FALSE;
349
350 if (check_for_dict_arg(argvars, 0) == FAIL
351 || check_for_list_arg(argvars, 1) == FAIL)
352 return;
353
354 if (argvars[1].vval.v_list == NULL)
355 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000356 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200357 return;
358 }
359
360 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100361 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200362 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000363 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364 return;
365 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100366 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200367
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100368 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100369 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200370
371 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
372 return;
373
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100374 // This must be done _before_ we start adding properties because property
375 // changes trigger buffer (memline) reorganisation, which needs this flag
376 // to be correctly set.
377 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200378 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
379 {
380 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
381 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000382 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200383 return;
384 }
385
386 pos_list = li->li_tv.vval.v_list;
387 start_lnum = list_find_nr(pos_list, 0L, &error);
388 start_col = list_find_nr(pos_list, 1L, &error);
389 end_lnum = list_find_nr(pos_list, 2L, &error);
390 end_col = list_find_nr(pos_list, 3L, &error);
391 if (error || start_lnum <= 0 || start_col <= 0
392 || end_lnum <= 0 || end_col <= 0)
393 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000394 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200395 return;
396 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100397 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200398 start_col, end_col) == FAIL)
399 return;
400 }
401
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200402 redraw_buf_later(buf, VALID);
403}
404
405/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100406 * Get the next ID to use for a textprop with text in buffer "buf".
407 */
408 static int
409get_textprop_id(buf_T *buf)
410{
411 // TODO: recycle deleted entries
412 return -(buf->b_textprop_text.ga_len + 1);
413}
414
415/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200416 * Shared between prop_add() and popup_create().
417 * "dict_arg" is the function argument of a dict containing "bufnr".
418 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100419 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200420 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100421 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200422prop_add_common(
423 linenr_T start_lnum,
424 colnr_T start_col,
425 dict_T *dict,
426 buf_T *default_buf,
427 typval_T *dict_arg)
428{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200429 linenr_T end_lnum;
430 colnr_T end_col;
431 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200432 buf_T *buf = default_buf;
433 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100434 char_u *text = NULL;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100435 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100436
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100437 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100438 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000439 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100440 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100441 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100442 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100443
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100444 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100445 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100446 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100447 if (end_lnum < start_lnum)
448 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000449 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100450 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100451 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100452 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100453 else
454 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100455
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100456 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100457 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100458 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100459
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100460 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100461 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000462 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100463 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100465 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100466 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100467 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100468 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100469 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100470 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100471 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000472 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100473 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100474 }
475 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100476 else if (start_lnum == end_lnum)
477 end_col = start_col;
478 else
479 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100480
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100481 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100482 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100483
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100484 if (dict_has_key(dict, "text"))
485 {
486 text = dict_get_string(dict, "text", TRUE);
487 if (text == NULL)
488 goto theend;
489 // use a default length of 1 to make multiple props show up
490 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100491
492 if (dict_has_key(dict, "text_align"))
493 {
494 char_u *p = dict_get_string(dict, "text_align", FALSE);
495
496 if (p == NULL)
497 goto theend;
498 if (STRCMP(p, "right") == 0)
499 flags |= TP_FLAG_ALIGN_RIGHT;
500 else if (STRCMP(p, "below") == 0)
501 flags |= TP_FLAG_ALIGN_BELOW;
502 else if (STRCMP(p, "after") != 0)
503 {
504 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
505 goto theend;
506 }
507 }
508
509 if (dict_has_key(dict, "text_wrap"))
510 {
511 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
512 if (p == NULL)
513 goto theend;
514 if (STRCMP(p, "wrap") == 0)
515 flags |= TP_FLAG_WRAP;
516 else if (STRCMP(p, "truncate") != 0)
517 {
518 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
519 goto theend;
520 }
521 }
522 }
523
524 // Column must be 1 or more for a normal text property; when "text" is
525 // present zero means it goes after the line.
526 if (start_col < (text == NULL ? 1 : 0))
527 {
528 semsg(_(e_invalid_column_number_nr), (long)start_col);
529 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100530 }
531
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200532 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100533 goto theend;
534
535 if (id < 0 && buf->b_textprop_text.ga_len > 0)
536 {
537 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
538 goto theend;
539 }
540 if (text != NULL)
541 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100542
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100543 // This must be done _before_ we add the property because property changes
544 // trigger buffer (memline) reorganisation, which needs this flag to be
545 // correctly set.
546 buf->b_has_textprop = TRUE; // this is never reset
547
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100548 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100549 start_lnum, end_lnum, start_col, end_col);
550 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100551
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200552 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100553
554theend:
555 vim_free(text);
556 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100557}
558
559/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100560 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100561 * Returns the number of text properties and, when non-zero, a pointer to the
562 * first one in "props" (note that it is not aligned, therefore the char_u
563 * pointer).
564 */
565 int
566get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
567{
568 char_u *text;
569 size_t textlen;
570 size_t proplen;
571
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100572 // Be quick when no text property types have been defined or the buffer,
573 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200574 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100575 return 0;
576
577 // Fetch the line to get the ml_line_len field updated.
578 text = ml_get_buf(buf, lnum, will_change);
579 textlen = STRLEN(text) + 1;
580 proplen = buf->b_ml.ml_line_len - textlen;
581 if (proplen % sizeof(textprop_T) != 0)
582 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000583 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100584 return 0;
585 }
586 if (proplen > 0)
587 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100588 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100589}
590
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200591/**
592 * Return the number of text properties on line "lnum" in the current buffer.
593 * When "only_starting" is true only text properties starting in this line will
594 * be considered.
595 */
596 int
597count_props(linenr_T lnum, int only_starting)
598{
599 char_u *props;
600 int proplen = get_text_props(curbuf, lnum, &props, 0);
601 int result = proplen;
602 int i;
603 textprop_T prop;
604
605 if (only_starting)
606 for (i = 0; i < proplen; ++i)
607 {
608 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
609 if (prop.tp_flags & TP_FLAG_CONT_PREV)
610 --result;
611 }
612 return result;
613}
614
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100615/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200616 * Find text property "type_id" in the visible lines of window "wp".
617 * Match "id" when it is > 0.
618 * Returns FAIL when not found.
619 */
620 int
621find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
622 linenr_T *found_lnum)
623{
624 linenr_T lnum;
625 char_u *props;
626 int count;
627 int i;
628
629 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100630 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200631 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
632 {
633 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
634 for (i = 0; i < count; ++i)
635 {
636 mch_memmove(prop, props + i * sizeof(textprop_T),
637 sizeof(textprop_T));
638 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
639 {
640 *found_lnum = lnum;
641 return OK;
642 }
643 }
644 }
645 return FAIL;
646}
647
648/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100649 * Set the text properties for line "lnum" to "props" with length "len".
650 * If "len" is zero text properties are removed, "props" is not used.
651 * Any existing text properties are dropped.
652 * Only works for the current buffer.
653 */
654 static void
655set_text_props(linenr_T lnum, char_u *props, int len)
656{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100657 char_u *text;
658 char_u *newtext;
659 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100660
661 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100662 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100663 newtext = alloc(textlen + len);
664 if (newtext == NULL)
665 return;
666 mch_memmove(newtext, text, textlen);
667 if (len > 0)
668 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100669 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100670 vim_free(curbuf->b_ml.ml_line_ptr);
671 curbuf->b_ml.ml_line_ptr = newtext;
672 curbuf->b_ml.ml_line_len = textlen + len;
673 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
674}
675
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100676 static proptype_T *
677find_type_by_id(hashtab_T *ht, int id)
678{
679 long todo;
680 hashitem_T *hi;
681
682 if (ht == NULL)
683 return NULL;
684
685 // TODO: Make this faster by keeping a list of types sorted on ID and use
686 // a binary search.
687
688 todo = (long)ht->ht_used;
689 for (hi = ht->ht_array; todo > 0; ++hi)
690 {
691 if (!HASHITEM_EMPTY(hi))
692 {
693 proptype_T *prop = HI2PT(hi);
694
695 if (prop->pt_id == id)
696 return prop;
697 --todo;
698 }
699 }
700 return NULL;
701}
702
703/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100704 * Fill 'dict' with text properties in 'prop'.
705 */
706 static void
707prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
708{
709 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200710 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100711
712 dict_add_number(dict, "col", prop->tp_col);
713 dict_add_number(dict, "length", prop->tp_len);
714 dict_add_number(dict, "id", prop->tp_id);
715 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
716 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200717
718 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
719 if (pt == NULL)
720 {
721 pt = find_type_by_id(global_proptypes, prop->tp_type);
722 buflocal = FALSE;
723 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100724 if (pt != NULL)
725 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200726
727 if (buflocal)
728 dict_add_number(dict, "type_bufnr", buf->b_fnum);
729 else
730 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100731}
732
733/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100734 * Find a property type by ID in "buf" or globally.
735 * Returns NULL if not found.
736 */
737 proptype_T *
738text_prop_type_by_id(buf_T *buf, int id)
739{
740 proptype_T *type;
741
742 type = find_type_by_id(buf->b_proptypes, id);
743 if (type == NULL)
744 type = find_type_by_id(global_proptypes, id);
745 return type;
746}
747
748/*
749 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
750 */
751 void
752f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
753{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200754 linenr_T start;
755 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100756 linenr_T lnum;
757 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100758 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100759
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200760 if (in_vim9script()
761 && (check_for_number_arg(argvars, 0) == FAIL
762 || check_for_opt_number_arg(argvars, 1) == FAIL
763 || (argvars[1].v_type != VAR_UNKNOWN
764 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
765 return;
766
767 start = tv_get_number(&argvars[0]);
768 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100769 if (argvars[1].v_type != VAR_UNKNOWN)
770 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100771 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100772 if (argvars[2].v_type != VAR_UNKNOWN)
773 {
774 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
775 return;
776 }
777 }
778 if (start < 1 || end < 1)
779 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200780 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100781 return;
782 }
783
784 for (lnum = start; lnum <= end; ++lnum)
785 {
786 char_u *text;
787 size_t len;
788
789 if (lnum > buf->b_ml.ml_line_count)
790 break;
791 text = ml_get_buf(buf, lnum, FALSE);
792 len = STRLEN(text) + 1;
793 if ((size_t)buf->b_ml.ml_line_len > len)
794 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100795 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100796 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
797 {
798 char_u *newtext = vim_strsave(text);
799
800 // need to allocate the line now
801 if (newtext == NULL)
802 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100803 if (buf->b_ml.ml_flags & ML_ALLOCATED)
804 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100805 buf->b_ml.ml_line_ptr = newtext;
806 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
807 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100808 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100809 }
810 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100811 if (did_clear)
812 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100813}
814
815/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100816 * prop_find({props} [, {direction}])
817 */
818 void
819f_prop_find(typval_T *argvars, typval_T *rettv)
820{
821 pos_T *cursor = &curwin->w_cursor;
822 dict_T *dict;
823 buf_T *buf = curbuf;
824 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200825 int lnum_start;
826 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100827 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200828 int id = 0;
829 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200830 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100831 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200832 int lnum = -1;
833 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100834 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100835 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100836
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200837 if (in_vim9script()
838 && (check_for_dict_arg(argvars, 0) == FAIL
839 || check_for_opt_string_arg(argvars, 1) == FAIL))
840 return;
841
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100842 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
843 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000844 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100845 return;
846 }
847 dict = argvars[0].vval.v_dict;
848
849 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
850 return;
851 if (buf->b_ml.ml_mfp == NULL)
852 return;
853
854 if (argvars[1].v_type != VAR_UNKNOWN)
855 {
856 char_u *dir_s = tv_get_string(&argvars[1]);
857
858 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100859 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100860 else if (*dir_s != 'f')
861 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000862 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100863 return;
864 }
865 }
866
867 di = dict_find(dict, (char_u *)"lnum", -1);
868 if (di != NULL)
869 lnum = tv_get_number(&di->di_tv);
870
871 di = dict_find(dict, (char_u *)"col", -1);
872 if (di != NULL)
873 col = tv_get_number(&di->di_tv);
874
875 if (lnum == -1)
876 {
877 lnum = cursor->lnum;
878 col = cursor->col + 1;
879 }
880 else if (col == -1)
881 col = 1;
882
883 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
884 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200885 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100886 return;
887 }
888
Bram Moolenaard61efa52022-07-23 09:52:04 +0100889 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100890
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100891 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200892 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100893 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200894 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200895 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100896 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100897 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100898 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100899 proptype_T *type = lookup_prop_type(name, buf);
900
901 if (type == NULL)
902 return;
903 type_id = type->pt_id;
904 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100905 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200906 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100907 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000908 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100909 return;
910 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200911 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100912 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000913 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100914 return;
915 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100916
917 lnum_start = lnum;
918
919 if (rettv_dict_alloc(rettv) == FAIL)
920 return;
921
922 while (1)
923 {
924 char_u *text = ml_get_buf(buf, lnum, FALSE);
925 size_t textlen = STRLEN(text) + 1;
926 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200927 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100928 int i;
929 textprop_T prop;
930 int prop_start;
931 int prop_end;
932
LemonBoy9bd3ce22022-04-18 21:54:02 +0100933 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100934 {
935 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100936 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100937
LemonBoy9bd3ce22022-04-18 21:54:02 +0100938 // For the very first line try to find the first property before or
939 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100940 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100941 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100942 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100943 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100944 if (prop.tp_col > col)
945 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100946 }
947 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
948 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100949 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100950 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200951 : (id_found && prop.tp_id == id)
952 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100953 {
954 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100955 if (lnum_start == lnum
956 && col >= prop.tp_col
957 && (col <= prop.tp_col + prop.tp_len
958 - (prop.tp_len != 0)))
959 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100960
LemonBoy9bd3ce22022-04-18 21:54:02 +0100961 // The property was not continued from last line, it starts on
962 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100963 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100964 // The property does not continue on the next line, it ends on
965 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100966 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100967 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100968 seen_end = 1;
969
970 // Skip lines without the start flag.
971 if (!prop_start)
972 {
973 // Always search backwards for start when search started
974 // on a prop and we're not skipping.
975 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +0100976 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200977 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100978 }
979
980 // If skipstart is true, skip the prop at start pos (even if
981 // continued from another line).
982 if (start_pos_has_prop && skipstart && !seen_end)
983 {
984 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200985 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100986 }
987
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100988 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
989 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
990
991 return;
992 }
993 }
994
995 if (dir > 0)
996 {
997 if (lnum >= buf->b_ml.ml_line_count)
998 break;
999 lnum++;
1000 }
1001 else
1002 {
1003 if (lnum <= 1)
1004 break;
1005 lnum--;
1006 }
1007 }
1008}
1009
1010/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001011 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1012 */
1013 static int
1014prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1015{
1016 int i;
1017
1018 for (i = 0; i < len; i++)
1019 if (types_or_ids[i] == type_or_id)
1020 return TRUE;
1021
1022 return FALSE;
1023}
1024
1025/*
1026 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1027 * If 'prop_types' is not NULL, then return only the text properties with
1028 * matching property type in the 'prop_types' array.
1029 * If 'prop_ids' is not NULL, then return only the text properties with
1030 * an identifier in the 'props_ids' array.
1031 * If 'add_lnum' is TRUE, then add the line number also to the text property
1032 * dictionary.
1033 */
1034 static void
1035get_props_in_line(
1036 buf_T *buf,
1037 linenr_T lnum,
1038 int *prop_types,
1039 int prop_types_len,
1040 int *prop_ids,
1041 int prop_ids_len,
1042 list_T *retlist,
1043 int add_lnum)
1044{
1045 char_u *text = ml_get_buf(buf, lnum, FALSE);
1046 size_t textlen = STRLEN(text) + 1;
1047 int count;
1048 int i;
1049 textprop_T prop;
1050
1051 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1052 for (i = 0; i < count; ++i)
1053 {
1054 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1055 sizeof(textprop_T));
1056 if ((prop_types == NULL
1057 || prop_type_or_id_in_list(prop_types, prop_types_len,
1058 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001059 && (prop_ids == NULL
1060 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1061 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001062 {
1063 dict_T *d = dict_alloc();
1064
1065 if (d == NULL)
1066 break;
1067 prop_fill_dict(d, &prop, buf);
1068 if (add_lnum)
1069 dict_add_number(d, "lnum", lnum);
1070 list_append_dict(retlist, d);
1071 }
1072 }
1073}
1074
1075/*
1076 * Convert a List of property type names into an array of property type
1077 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1078 * error. 'num_types' is set to the number of returned property types.
1079 */
1080 static int *
1081get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1082{
1083 int *prop_types;
1084 listitem_T *li;
1085 int i;
1086 char_u *name;
1087 proptype_T *type;
1088
1089 *num_types = 0;
1090
1091 prop_types = ALLOC_MULT(int, list_len(l));
1092 if (prop_types == NULL)
1093 return NULL;
1094
1095 i = 0;
1096 FOR_ALL_LIST_ITEMS(l, li)
1097 {
1098 if (li->li_tv.v_type != VAR_STRING)
1099 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001100 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001101 goto errret;
1102 }
1103 name = li->li_tv.vval.v_string;
1104 if (name == NULL)
1105 goto errret;
1106
1107 type = lookup_prop_type(name, buf);
1108 if (type == NULL)
1109 goto errret;
1110 prop_types[i++] = type->pt_id;
1111 }
1112
1113 *num_types = i;
1114 return prop_types;
1115
1116errret:
1117 VIM_CLEAR(prop_types);
1118 return NULL;
1119}
1120
1121/*
1122 * Convert a List of property identifiers into an array of property
1123 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1124 * error. 'num_ids' is set to the number of returned property identifiers.
1125 */
1126 static int *
1127get_prop_ids_from_list(list_T *l, int *num_ids)
1128{
1129 int *prop_ids;
1130 listitem_T *li;
1131 int i;
1132 int id;
1133 int error;
1134
1135 *num_ids = 0;
1136
1137 prop_ids = ALLOC_MULT(int, list_len(l));
1138 if (prop_ids == NULL)
1139 return NULL;
1140
1141 i = 0;
1142 FOR_ALL_LIST_ITEMS(l, li)
1143 {
1144 error = FALSE;
1145 id = tv_get_number_chk(&li->li_tv, &error);
1146 if (error)
1147 goto errret;
1148
1149 prop_ids[i++] = id;
1150 }
1151
1152 *num_ids = i;
1153 return prop_ids;
1154
1155errret:
1156 VIM_CLEAR(prop_ids);
1157 return NULL;
1158}
1159
1160/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001161 * prop_list({lnum} [, {bufnr}])
1162 */
1163 void
1164f_prop_list(typval_T *argvars, typval_T *rettv)
1165{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001166 linenr_T lnum;
1167 linenr_T start_lnum;
1168 linenr_T end_lnum;
1169 buf_T *buf = curbuf;
1170 int add_lnum = FALSE;
1171 int *prop_types = NULL;
1172 int prop_types_len = 0;
1173 int *prop_ids = NULL;
1174 int prop_ids_len = 0;
1175 list_T *l;
1176 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001177
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001178 if (in_vim9script()
1179 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001180 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001181 return;
1182
Bram Moolenaar93a10962022-06-16 11:42:09 +01001183 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001184 return;
1185
1186 // default: get text properties on current line
1187 start_lnum = tv_get_number(&argvars[0]);
1188 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001189 if (argvars[1].v_type != VAR_UNKNOWN)
1190 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001191 dict_T *d;
1192
1193 if (argvars[1].v_type != VAR_DICT)
1194 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001195 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001196 return;
1197 }
1198 d = argvars[1].vval.v_dict;
1199
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001200 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1201 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001202
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001203 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001204 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001205 if (di->di_tv.v_type != VAR_NUMBER)
1206 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001207 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001208 return;
1209 }
1210 end_lnum = tv_get_number(&di->di_tv);
1211 if (end_lnum < 0)
1212 // negative end_lnum is used as an offset from the last buffer
1213 // line
1214 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1215 else if (end_lnum > buf->b_ml.ml_line_count)
1216 end_lnum = buf->b_ml.ml_line_count;
1217 add_lnum = TRUE;
1218 }
1219 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1220 {
1221 if (di->di_tv.v_type != VAR_LIST)
1222 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001223 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001224 return;
1225 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001226
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001227 l = di->di_tv.vval.v_list;
1228 if (l != NULL && list_len(l) > 0)
1229 {
1230 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1231 if (prop_types == NULL)
1232 return;
1233 }
1234 }
1235 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1236 {
1237 if (di->di_tv.v_type != VAR_LIST)
1238 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001239 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001240 goto errret;
1241 }
1242
1243 l = di->di_tv.vval.v_list;
1244 if (l != NULL && list_len(l) > 0)
1245 {
1246 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1247 if (prop_ids == NULL)
1248 goto errret;
1249 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001250 }
1251 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001252 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1253 || end_lnum < 1 || end_lnum < start_lnum)
1254 emsg(_(e_invalid_range));
1255 else
1256 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1257 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1258 prop_ids, prop_ids_len,
1259 rettv->vval.v_list, add_lnum);
1260
1261errret:
1262 VIM_CLEAR(prop_types);
1263 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001264}
1265
1266/*
1267 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1268 */
1269 void
1270f_prop_remove(typval_T *argvars, typval_T *rettv)
1271{
1272 linenr_T start = 1;
1273 linenr_T end = 0;
1274 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001275 linenr_T first_changed = 0;
1276 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001277 dict_T *dict;
1278 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001279 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001280 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001281 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001282 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001283 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001284
1285 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001286
1287 if (in_vim9script()
1288 && (check_for_dict_arg(argvars, 0) == FAIL
1289 || check_for_opt_number_arg(argvars, 1) == FAIL
1290 || (argvars[1].v_type != VAR_UNKNOWN
1291 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1292 return;
1293
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001294 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1295 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001296 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001297 return;
1298 }
1299
1300 if (argvars[1].v_type != VAR_UNKNOWN)
1301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001302 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001303 end = start;
1304 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001305 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001306 if (start < 1 || end < 1)
1307 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001308 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001309 return;
1310 }
1311 }
1312
1313 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001314 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1315 return;
1316 if (buf->b_ml.ml_mfp == NULL)
1317 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001318
Bram Moolenaard61efa52022-07-23 09:52:04 +01001319 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001320
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001321 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001322 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001323 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001324 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001325 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001326 proptype_T *type = lookup_prop_type(name, buf);
1327
1328 if (type == NULL)
1329 return;
1330 type_id = type->pt_id;
1331 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001332 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001333
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001334 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001335 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001336 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001337 return;
1338 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001339 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001340 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001341 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001342 return;
1343 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001344
1345 if (end == 0)
1346 end = buf->b_ml.ml_line_count;
1347 for (lnum = start; lnum <= end; ++lnum)
1348 {
1349 char_u *text;
1350 size_t len;
1351
1352 if (lnum > buf->b_ml.ml_line_count)
1353 break;
1354 text = ml_get_buf(buf, lnum, FALSE);
1355 len = STRLEN(text) + 1;
1356 if ((size_t)buf->b_ml.ml_line_len > len)
1357 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001358 static textprop_T textprop; // static because of alignment
1359 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001360
1361 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1362 / sizeof(textprop_T); ++idx)
1363 {
1364 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1365 + idx * sizeof(textprop_T);
1366 size_t taillen;
1367
1368 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001369 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1370 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001371 {
1372 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1373 {
1374 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1375
1376 // need to allocate the line to be able to change it
1377 if (newptr == NULL)
1378 return;
1379 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1380 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001381 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1382 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001383 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001384 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1385
1386 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001387 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001388 }
1389
1390 taillen = buf->b_ml.ml_line_len - len
1391 - (idx + 1) * sizeof(textprop_T);
1392 if (taillen > 0)
1393 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1394 taillen);
1395 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1396 --idx;
1397
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001398 if (textprop.tp_id < 0)
1399 {
1400 garray_T *gap = &buf->b_textprop_text;
1401 int ii = -textprop.tp_id - 1;
1402
1403 // negative ID: property with text - free the text
1404 if (ii < gap->ga_len)
1405 {
1406 char_u **p = ((char_u **)gap->ga_data) + ii;
1407 vim_free(*p);
1408 *p = NULL;
1409 did_remove_text = TRUE;
1410 }
1411 }
1412
Bram Moolenaar965c0442021-05-17 00:22:06 +02001413 if (first_changed == 0)
1414 first_changed = lnum;
1415 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001416 ++rettv->vval.v_number;
1417 if (!do_all)
1418 break;
1419 }
1420 }
1421 }
1422 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001423
Bram Moolenaar965c0442021-05-17 00:22:06 +02001424 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001425 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001426 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1427 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001428 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001429
1430 if (did_remove_text)
1431 {
1432 garray_T *gap = &buf->b_textprop_text;
1433
1434 // Reduce the growarray size for NULL pointers at the end.
1435 while (gap->ga_len > 0
1436 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1437 --gap->ga_len;
1438 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001439}
1440
1441/*
1442 * Common for f_prop_type_add() and f_prop_type_change().
1443 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001444 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001445prop_type_set(typval_T *argvars, int add)
1446{
1447 char_u *name;
1448 buf_T *buf = NULL;
1449 dict_T *dict;
1450 dictitem_T *di;
1451 proptype_T *prop;
1452
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001453 if (in_vim9script()
1454 && (check_for_string_arg(argvars, 0) == FAIL
1455 || check_for_dict_arg(argvars, 1) == FAIL))
1456 return;
1457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001458 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001459 if (*name == NUL)
1460 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001461 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001462 return;
1463 }
1464
1465 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1466 return;
1467 dict = argvars[1].vval.v_dict;
1468
1469 prop = find_prop(name, buf);
1470 if (add)
1471 {
1472 hashtab_T **htp;
1473
1474 if (prop != NULL)
1475 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001476 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001477 return;
1478 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001479 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001480 if (prop == NULL)
1481 return;
1482 STRCPY(prop->pt_name, name);
1483 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001484 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001485 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1486 if (*htp == NULL)
1487 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001488 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001489 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001490 {
1491 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001492 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001493 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494 hash_init(*htp);
1495 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001496 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001497 }
1498 else
1499 {
1500 if (prop == NULL)
1501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001502 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001503 return;
1504 }
1505 }
1506
1507 if (dict != NULL)
1508 {
1509 di = dict_find(dict, (char_u *)"highlight", -1);
1510 if (di != NULL)
1511 {
1512 char_u *highlight;
1513 int hl_id = 0;
1514
Bram Moolenaard61efa52022-07-23 09:52:04 +01001515 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001516 if (highlight != NULL && *highlight != NUL)
1517 hl_id = syn_name2id(highlight);
1518 if (hl_id <= 0)
1519 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001520 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001521 highlight == NULL ? (char_u *)"" : highlight);
1522 return;
1523 }
1524 prop->pt_hl_id = hl_id;
1525 }
1526
Bram Moolenaar58187f12019-05-05 16:33:47 +02001527 di = dict_find(dict, (char_u *)"combine", -1);
1528 if (di != NULL)
1529 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001530 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001531 prop->pt_flags |= PT_FLAG_COMBINE;
1532 else
1533 prop->pt_flags &= ~PT_FLAG_COMBINE;
1534 }
1535
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001536 di = dict_find(dict, (char_u *)"priority", -1);
1537 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001538 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001539
1540 di = dict_find(dict, (char_u *)"start_incl", -1);
1541 if (di != NULL)
1542 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001543 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001544 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1545 else
1546 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1547 }
1548
1549 di = dict_find(dict, (char_u *)"end_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_END_INCL;
1554 else
1555 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1556 }
1557 }
1558}
1559
1560/*
1561 * prop_type_add({name}, {props})
1562 */
1563 void
1564f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1565{
1566 prop_type_set(argvars, TRUE);
1567}
1568
1569/*
1570 * prop_type_change({name}, {props})
1571 */
1572 void
1573f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1574{
1575 prop_type_set(argvars, FALSE);
1576}
1577
1578/*
1579 * prop_type_delete({name} [, {bufnr}])
1580 */
1581 void
1582f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1583{
1584 char_u *name;
1585 buf_T *buf = NULL;
1586 hashitem_T *hi;
1587
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001588 if (in_vim9script()
1589 && (check_for_string_arg(argvars, 0) == FAIL
1590 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1591 return;
1592
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001593 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001594 if (*name == NUL)
1595 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001596 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001597 return;
1598 }
1599
1600 if (argvars[1].v_type != VAR_UNKNOWN)
1601 {
1602 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1603 return;
1604 }
1605
1606 hi = find_prop_hi(name, buf);
1607 if (hi != NULL)
1608 {
1609 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001610 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001611
1612 if (buf == NULL)
1613 ht = global_proptypes;
1614 else
1615 ht = buf->b_proptypes;
1616 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001617 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001618 }
1619}
1620
1621/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001622 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001623 */
1624 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001625f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001626{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001627 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001628
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001629 if (in_vim9script()
1630 && (check_for_string_arg(argvars, 0) == FAIL
1631 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1632 return;
1633
1634 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001635 if (*name == NUL)
1636 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001637 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001638 return;
1639 }
1640 if (rettv_dict_alloc(rettv) == OK)
1641 {
1642 proptype_T *prop = NULL;
1643 buf_T *buf = NULL;
1644
1645 if (argvars[1].v_type != VAR_UNKNOWN)
1646 {
1647 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1648 return;
1649 }
1650
1651 prop = find_prop(name, buf);
1652 if (prop != NULL)
1653 {
1654 dict_T *d = rettv->vval.v_dict;
1655
1656 if (prop->pt_hl_id > 0)
1657 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1658 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001659 dict_add_number(d, "combine",
1660 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001661 dict_add_number(d, "start_incl",
1662 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1663 dict_add_number(d, "end_incl",
1664 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1665 if (buf != NULL)
1666 dict_add_number(d, "bufnr", buf->b_fnum);
1667 }
1668 }
1669}
1670
1671 static void
1672list_types(hashtab_T *ht, list_T *l)
1673{
1674 long todo;
1675 hashitem_T *hi;
1676
1677 todo = (long)ht->ht_used;
1678 for (hi = ht->ht_array; todo > 0; ++hi)
1679 {
1680 if (!HASHITEM_EMPTY(hi))
1681 {
1682 proptype_T *prop = HI2PT(hi);
1683
1684 list_append_string(l, prop->pt_name, -1);
1685 --todo;
1686 }
1687 }
1688}
1689
1690/*
1691 * prop_type_list([{bufnr}])
1692 */
1693 void
1694f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1695{
1696 buf_T *buf = NULL;
1697
1698 if (rettv_list_alloc(rettv) == OK)
1699 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001700 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1701 return;
1702
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001703 if (argvars[0].v_type != VAR_UNKNOWN)
1704 {
1705 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1706 return;
1707 }
1708 if (buf == NULL)
1709 {
1710 if (global_proptypes != NULL)
1711 list_types(global_proptypes, rettv->vval.v_list);
1712 }
1713 else if (buf->b_proptypes != NULL)
1714 list_types(buf->b_proptypes, rettv->vval.v_list);
1715 }
1716}
1717
1718/*
1719 * Free all property types in "ht".
1720 */
1721 static void
1722clear_ht_prop_types(hashtab_T *ht)
1723{
1724 long todo;
1725 hashitem_T *hi;
1726
1727 if (ht == NULL)
1728 return;
1729
1730 todo = (long)ht->ht_used;
1731 for (hi = ht->ht_array; todo > 0; ++hi)
1732 {
1733 if (!HASHITEM_EMPTY(hi))
1734 {
1735 proptype_T *prop = HI2PT(hi);
1736
1737 vim_free(prop);
1738 --todo;
1739 }
1740 }
1741
1742 hash_clear(ht);
1743 vim_free(ht);
1744}
1745
1746#if defined(EXITFREE) || defined(PROTO)
1747/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001748 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001749 */
1750 void
1751clear_global_prop_types(void)
1752{
1753 clear_ht_prop_types(global_proptypes);
1754 global_proptypes = NULL;
1755}
1756#endif
1757
1758/*
1759 * Free all property types for "buf".
1760 */
1761 void
1762clear_buf_prop_types(buf_T *buf)
1763{
1764 clear_ht_prop_types(buf->b_proptypes);
1765 buf->b_proptypes = NULL;
1766}
1767
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001768// Struct used to return two values from adjust_prop().
1769typedef struct
1770{
1771 int dirty; // if the property was changed
1772 int can_drop; // whether after this change, the prop may be removed
1773} adjustres_T;
1774
1775/*
1776 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1777 *
1778 * Note that "col" is zero-based, while tp_col is one-based.
1779 * Only for the current buffer.
1780 * "flags" can have:
1781 * APC_SUBSTITUTE: Text is replaced, not inserted.
1782 */
1783 static adjustres_T
1784adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001785 textprop_T *prop,
1786 colnr_T col,
1787 int added,
1788 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001789{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001790 proptype_T *pt;
1791 int start_incl;
1792 int end_incl;
1793 int droppable;
1794 adjustres_T res = {TRUE, FALSE};
1795
1796 // prop after end of the line doesn't move
1797 if (prop->tp_col == MAXCOL)
1798 {
1799 res.dirty = FALSE;
1800 return res;
1801 }
1802
1803 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1804 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001805 || (flags & APC_SUBSTITUTE)
1806 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001807 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001808 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001809 // do not drop zero-width props if they later can increase in size
1810 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001811
1812 if (added > 0)
1813 {
1814 if (col + 1 <= prop->tp_col
1815 - (start_incl || (prop->tp_len == 0 && end_incl)))
1816 // Change is entirely before the text property: Only shift
1817 prop->tp_col += added;
1818 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1819 // Insertion was inside text property
1820 prop->tp_len += added;
1821 }
1822 else if (prop->tp_col > col + 1)
1823 {
1824 if (prop->tp_col + added < col + 1)
1825 {
1826 prop->tp_len += (prop->tp_col - 1 - col) + added;
1827 prop->tp_col = col + 1;
1828 if (prop->tp_len <= 0)
1829 {
1830 prop->tp_len = 0;
1831 res.can_drop = droppable;
1832 }
1833 }
1834 else
1835 prop->tp_col += added;
1836 }
1837 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1838 {
1839 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1840
1841 prop->tp_len += after > 0 ? added + after : added;
1842 res.can_drop = prop->tp_len <= 0 && droppable;
1843 }
1844 else
1845 res.dirty = FALSE;
1846
1847 return res;
1848}
1849
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001850/*
1851 * Adjust the columns of text properties in line "lnum" after position "col" to
1852 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001853 * Note that "col" is zero-based, while tp_col is one-based.
1854 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001855 * "flags" can have:
1856 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1857 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001858 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001859 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001860 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001861 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001862adjust_prop_columns(
1863 linenr_T lnum,
1864 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001865 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001866 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001867{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001868 int proplen;
1869 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001870 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001871 int ri, wi;
1872 size_t textlen;
1873
1874 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001875 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001876
1877 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1878 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001879 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001880 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001881
Bram Moolenaar196d1572019-01-02 23:47:18 +01001882 wi = 0; // write index
1883 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001884 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001885 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001886 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001887
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001888 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1889 res = adjust_prop(&prop, col, bytes_added, flags);
1890 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001891 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001892 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001893 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1894 && u_savesub(lnum) == FAIL)
1895 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001896 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001897
1898 // u_savesub() may have updated curbuf->b_ml, fetch it again
1899 if (curbuf->b_ml.ml_line_lnum != lnum)
1900 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001901 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001902 if (res.can_drop)
1903 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001904 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001905 ++wi;
1906 }
1907 if (dirty)
1908 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001909 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1910
1911 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001912 {
1913 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1914
1915 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1916 vim_free(curbuf->b_ml.ml_line_ptr);
1917 curbuf->b_ml.ml_line_ptr = p;
1918 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001919 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001920 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001921 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001922 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001923}
1924
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001925/*
1926 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001927 * "lnum_props" is the line that has the properties from before the split.
1928 * "lnum_top" is the top line.
1929 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001930 * "deleted" is the number of bytes deleted.
1931 */
1932 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001933adjust_props_for_split(
1934 linenr_T lnum_props,
1935 linenr_T lnum_top,
1936 int kept,
1937 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001938{
1939 char_u *props;
1940 int count;
1941 garray_T prevprop;
1942 garray_T nextprop;
1943 int i;
1944 int skipped = kept + deleted;
1945
1946 if (!curbuf->b_has_textprop)
1947 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001948
1949 // Get the text properties from "lnum_props".
1950 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001951 ga_init2(&prevprop, sizeof(textprop_T), 10);
1952 ga_init2(&nextprop, sizeof(textprop_T), 10);
1953
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001954 // Keep the relevant ones in the first line, reducing the length if needed.
1955 // Copy the ones that include the split to the second line.
1956 // Move the ones after the split to the second line.
1957 for (i = 0; i < count; ++i)
1958 {
1959 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001960 proptype_T *pt;
1961 int start_incl, end_incl;
1962 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001963
1964 // copy the prop to an aligned structure
1965 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1966
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001967 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1968 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1969 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1970 cont_prev = prop.tp_col + !start_incl <= kept;
1971 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1972
1973 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001974 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001975 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1976
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001977 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001978 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001979 if (p->tp_col + p->tp_len >= kept)
1980 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001981 if (cont_next)
1982 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001983 }
1984
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001985 // Only add the property to the next line if the length is bigger than
1986 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001987 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001988 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001989 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001990 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001991 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001992 if (p->tp_col > skipped)
1993 p->tp_col -= skipped - 1;
1994 else
1995 {
1996 p->tp_len -= skipped - p->tp_col;
1997 p->tp_col = 1;
1998 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001999 if (cont_prev)
2000 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002001 }
2002 }
2003
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002004 set_text_props(lnum_top, prevprop.ga_data,
2005 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002006 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002007 set_text_props(lnum_top + 1, nextprop.ga_data,
2008 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002009 ga_clear(&nextprop);
2010}
2011
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002012/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002013 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002014 */
2015 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002016prepend_joined_props(
2017 char_u *new_props,
2018 int propcount,
2019 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002020 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002021 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002022 long col,
2023 int removed)
2024{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002025 char_u *props;
2026 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2027 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002028
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002029 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002030 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002031 textprop_T prop;
2032 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002033
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002034 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
2035 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2036
2037 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002038 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002039
2040 if (add_all || end)
2041 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2042 &prop, sizeof(prop));
2043 else
2044 {
2045 int j;
2046 int found = FALSE;
2047
2048 // Search for continuing prop.
2049 for (j = *props_remaining; j < propcount; ++j)
2050 {
2051 textprop_T op;
2052
2053 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2054 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2055 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002056 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002057 found = TRUE;
2058 op.tp_len += op.tp_col - prop.tp_col;
2059 op.tp_col = prop.tp_col;
2060 // Start/end is taken care of when deleting joined lines
2061 op.tp_flags = prop.tp_flags;
2062 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2063 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002064 }
2065 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002066 if (!found)
2067 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002068 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002069 }
2070}
2071
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002072#endif // FEAT_PROP_POPUP