blob: 5f5c6a2aa23e57cfdd05dc2cb0ef46705120fb53 [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
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010017
18/*
19 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
20 * This avoids adding a pointer to the hashtable item.
21 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
22 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
23 * HI2PT() converts a hashitem pointer to a proptype pointer.
24 */
25#define PT2HIKEY(p) ((p)->pt_name)
26#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
27#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
28
29// The global text property types.
30static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010031static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33// The last used text property type ID.
34static int proptype_id = 0;
35
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036/*
37 * Find a property type by name, return the hashitem.
38 * Returns NULL if the item can't be found.
39 */
40 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010041find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010042{
43 hashtab_T *ht;
44 hashitem_T *hi;
45
46 if (*name == NUL)
47 return NULL;
48 if (buf == NULL)
49 ht = global_proptypes;
50 else
51 ht = buf->b_proptypes;
52
53 if (ht == NULL)
54 return NULL;
55 hi = hash_find(ht, name);
56 if (HASHITEM_EMPTY(hi))
57 return NULL;
58 return hi;
59}
60
61/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010062 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010063 */
64 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010065find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010066{
Bram Moolenaare44336b2022-08-07 18:20:08 +010067 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010068
69 if (hi == NULL)
70 return NULL;
71 return HI2PT(hi);
72}
73
74/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020075 * Get the prop type ID of "name".
76 * When not found return zero.
77 */
78 int
79find_prop_type_id(char_u *name, buf_T *buf)
80{
Bram Moolenaare44336b2022-08-07 18:20:08 +010081 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020082
83 if (pt == NULL)
84 return 0;
85 return pt->pt_id;
86}
87
88/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089 * Lookup a property type by name. First in "buf" and when not found in the
90 * global types.
91 * When not found gives an error message and returns NULL.
92 */
93 static proptype_T *
94lookup_prop_type(char_u *name, buf_T *buf)
95{
Bram Moolenaare44336b2022-08-07 18:20:08 +010096 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097
98 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +010099 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100101 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100152 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153 return;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200154
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100155 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
156 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157}
158
159/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200160 * Attach a text property 'type_name' to the text starting
161 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100162 * the buffer "buf" and assign identifier "id".
163 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200164 */
165 static int
166prop_add_one(
167 buf_T *buf,
168 char_u *type_name,
169 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100170 char_u *text_arg,
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100171 int text_padding_left,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100172 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200173 linenr_T start_lnum,
174 linenr_T end_lnum,
175 colnr_T start_col,
176 colnr_T end_col)
177{
178 proptype_T *type;
179 linenr_T lnum;
180 int proplen;
181 char_u *props = NULL;
182 char_u *newprops;
183 size_t textlen;
184 char_u *newtext;
185 int i;
186 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text = text_arg;
188 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189
190 type = lookup_prop_type(type_name, buf);
191 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200193
194 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
195 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000196 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200198 }
199 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
200 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000201 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200203 }
204
205 if (buf->b_ml.ml_mfp == NULL)
206 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000207 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
209 }
210
211 if (text != NULL)
212 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100213 garray_T *gap = &buf->b_textprop_text;
214 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100215
216 // double check we got the right ID
217 if (-id - 1 != gap->ga_len)
218 iemsg("text prop ID mismatch");
219 if (gap->ga_growsize == 0)
220 ga_init2(gap, sizeof(char *), 50);
221 if (ga_grow(gap, 1) == FAIL)
222 goto theend;
223 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100224
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100225 // change any control character (Tab, Newline, etc.) to a Space to make
226 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100227 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100228 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100230 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200231 }
232
233 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
234 {
235 colnr_T col; // start column
236 long length; // in bytes
237
238 // Fetch the line to get the ml_line_len field updated.
239 proplen = get_text_props(buf, lnum, &props, TRUE);
240 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
241
242 if (lnum == start_lnum)
243 col = start_col;
244 else
245 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100246 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200247 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000248 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100249 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200250 }
251
252 if (lnum == end_lnum)
253 length = end_col - col;
254 else
255 length = (int)textlen - col + 1;
256 if (length > (long)textlen)
257 length = (int)textlen; // can include the end-of-line
258 if (length < 0)
259 length = 0; // zero-width property
260
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100261 if (text_arg != NULL)
262 {
263 length = 1; // text is placed on one character
264 if (col == 0)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100265 {
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100266 col = MAXCOL; // after the line
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100267 length += text_padding_left;
268 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100269 }
270
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200271 // Allocate the new line with space for the new property.
272 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
273 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100274 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200275 // Copy the text, including terminating NUL.
276 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
277
278 // Find the index where to insert the new property.
279 // Since the text properties are not aligned properly when stored with
280 // the text, we need to copy them as bytes before using it as a struct.
281 for (i = 0; i < proplen; ++i)
282 {
283 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
284 sizeof(textprop_T));
285 if (tmp_prop.tp_col >= col)
286 break;
287 }
288 newprops = newtext + textlen;
289 if (i > 0)
290 mch_memmove(newprops, props, sizeof(textprop_T) * i);
291
292 tmp_prop.tp_col = col;
293 tmp_prop.tp_len = length;
294 tmp_prop.tp_id = id;
295 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100296 tmp_prop.tp_flags = text_flags
297 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100298 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
299 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
300 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200301 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
302 sizeof(textprop_T));
303
304 if (i < proplen)
305 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
306 props + i * sizeof(textprop_T),
307 sizeof(textprop_T) * (proplen - i));
308
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100309 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200310 vim_free(buf->b_ml.ml_line_ptr);
311 buf->b_ml.ml_line_ptr = newtext;
312 buf->b_ml.ml_line_len += sizeof(textprop_T);
313 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
314 }
315
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100316 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200317 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100318 res = OK;
319
320theend:
321 vim_free(text);
322 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200323}
324
325/*
326 * prop_add_list()
327 * First argument specifies the text property:
328 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
329 * Second argument is a List where each item is a List with the following
330 * entries: [lnum, start_col, end_col]
331 */
332 void
333f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
334{
335 dict_T *dict;
336 char_u *type_name;
337 buf_T *buf = curbuf;
338 int id = 0;
339 listitem_T *li;
340 list_T *pos_list;
341 linenr_T start_lnum;
342 colnr_T start_col;
343 linenr_T end_lnum;
344 colnr_T end_col;
345 int error = FALSE;
346
347 if (check_for_dict_arg(argvars, 0) == FAIL
348 || check_for_list_arg(argvars, 1) == FAIL)
349 return;
350
Bram Moolenaard83392a2022-09-01 12:22:46 +0100351 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200352 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200353
354 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100355 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200356 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000357 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200358 return;
359 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100360 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200361
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100362 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100363 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364
365 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
366 return;
367
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100368 // This must be done _before_ we start adding properties because property
369 // changes trigger buffer (memline) reorganisation, which needs this flag
370 // to be correctly set.
371 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200372 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
373 {
374 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
375 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000376 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200377 return;
378 }
379
380 pos_list = li->li_tv.vval.v_list;
381 start_lnum = list_find_nr(pos_list, 0L, &error);
382 start_col = list_find_nr(pos_list, 1L, &error);
383 end_lnum = list_find_nr(pos_list, 2L, &error);
384 end_col = list_find_nr(pos_list, 3L, &error);
385 if (error || start_lnum <= 0 || start_col <= 0
386 || end_lnum <= 0 || end_col <= 0)
387 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000388 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200389 return;
390 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100391 if (prop_add_one(buf, type_name, id, NULL, 0, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200392 start_col, end_col) == FAIL)
393 return;
394 }
395
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100396 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200397}
398
399/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100400 * Get the next ID to use for a textprop with text in buffer "buf".
401 */
402 static int
403get_textprop_id(buf_T *buf)
404{
405 // TODO: recycle deleted entries
406 return -(buf->b_textprop_text.ga_len + 1);
407}
408
409/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200410 * Shared between prop_add() and popup_create().
411 * "dict_arg" is the function argument of a dict containing "bufnr".
412 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100413 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200414 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100415 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200416prop_add_common(
417 linenr_T start_lnum,
418 colnr_T start_col,
419 dict_T *dict,
420 buf_T *default_buf,
421 typval_T *dict_arg)
422{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200423 linenr_T end_lnum;
424 colnr_T end_col;
425 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426 buf_T *buf = default_buf;
427 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100428 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100429 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100430 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100431
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100432 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100433 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000434 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100435 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100436 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100437 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100438
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100439 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100440 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100441 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100442 if (end_lnum < start_lnum)
443 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000444 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100445 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100446 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100447 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100448 else
449 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100450
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100451 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100452 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100453 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100454
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100455 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100456 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000457 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100458 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100459 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100460 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100461 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100462 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100463 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100464 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100465 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000467 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100468 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469 }
470 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 else if (start_lnum == end_lnum)
472 end_col = start_col;
473 else
474 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100475
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100476 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100477 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100478
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100479 if (dict_has_key(dict, "text"))
480 {
481 text = dict_get_string(dict, "text", TRUE);
482 if (text == NULL)
483 goto theend;
484 // use a default length of 1 to make multiple props show up
485 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100486
487 if (dict_has_key(dict, "text_align"))
488 {
489 char_u *p = dict_get_string(dict, "text_align", FALSE);
490
491 if (p == NULL)
492 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100493 if (start_col != 0)
494 {
495 emsg(_(e_can_only_use_text_align_when_column_is_zero));
496 goto theend;
497 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100498 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
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100509 if (dict_has_key(dict, "text_padding_left"))
510 {
511 text_padding_left = dict_get_number(dict, "text_padding_left");
512 if (text_padding_left < 0)
513 {
514 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
515 goto theend;
516 }
517 }
518
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100519 if (dict_has_key(dict, "text_wrap"))
520 {
521 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100522
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100523 if (p == NULL)
524 goto theend;
525 if (STRCMP(p, "wrap") == 0)
526 flags |= TP_FLAG_WRAP;
527 else if (STRCMP(p, "truncate") != 0)
528 {
529 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
530 goto theend;
531 }
532 }
533 }
534
535 // Column must be 1 or more for a normal text property; when "text" is
536 // present zero means it goes after the line.
537 if (start_col < (text == NULL ? 1 : 0))
538 {
539 semsg(_(e_invalid_column_number_nr), (long)start_col);
540 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100541 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100542 if (start_col > 0 && text_padding_left > 0)
543 {
544 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
545 goto theend;
546 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100547
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200548 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100549 goto theend;
550
551 if (id < 0 && buf->b_textprop_text.ga_len > 0)
552 {
553 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
554 goto theend;
555 }
556 if (text != NULL)
557 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100558
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100559 // This must be done _before_ we add the property because property changes
560 // trigger buffer (memline) reorganisation, which needs this flag to be
561 // correctly set.
562 buf->b_has_textprop = TRUE; // this is never reset
563
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100564 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100565 start_lnum, end_lnum, start_col, end_col);
566 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100567
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100568 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100569
570theend:
571 vim_free(text);
572 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100573}
574
575/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100576 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100577 * Returns the number of text properties and, when non-zero, a pointer to the
578 * first one in "props" (note that it is not aligned, therefore the char_u
579 * pointer).
580 */
581 int
582get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
583{
584 char_u *text;
585 size_t textlen;
586 size_t proplen;
587
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100588 // Be quick when no text property types have been defined or the buffer,
589 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200590 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100591 return 0;
592
593 // Fetch the line to get the ml_line_len field updated.
594 text = ml_get_buf(buf, lnum, will_change);
595 textlen = STRLEN(text) + 1;
596 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100597 if (proplen == 0)
598 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100599 if (proplen % sizeof(textprop_T) != 0)
600 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000601 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100602 return 0;
603 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100604 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100605 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100606}
607
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100608/*
609 * Return the number of text properties with "below" alignment in line "lnum".
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100610 * A "right" aligned property also goes below after a "below" or other "right"
611 * aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100612 */
613 int
614prop_count_below(buf_T *buf, linenr_T lnum)
615{
616 char_u *props;
617 int count = get_text_props(buf, lnum, &props, FALSE);
618 int result = 0;
619 textprop_T prop;
620 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100621 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100622
623 if (count == 0)
624 return 0;
625 for (i = 0; i < count; ++i)
626 {
627 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100628 if (prop.tp_col == MAXCOL)
629 {
630 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
631 || (next_right_goes_below
632 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
633 {
634 next_right_goes_below = TRUE;
635 ++result;
636 }
637 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
638 next_right_goes_below = TRUE;
639 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100640 }
641 return result;
642}
643
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200644/**
645 * Return the number of text properties on line "lnum" in the current buffer.
646 * When "only_starting" is true only text properties starting in this line will
647 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100648 * When "last_line" is FALSE then text properties after the line are not
649 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200650 */
651 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100652count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200653{
654 char_u *props;
655 int proplen = get_text_props(curbuf, lnum, &props, 0);
656 int result = proplen;
657 int i;
658 textprop_T prop;
659
Bram Moolenaare175dc62022-08-01 22:18:50 +0100660 for (i = 0; i < proplen; ++i)
661 {
662 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100663 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100664 // previous line, or when not in the last line and it is virtual text
665 // after the line.
666 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
667 || (!last_line && prop.tp_col == MAXCOL))
668 --result;
669 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200670 return result;
671}
672
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100673static textprop_T *text_prop_compare_props;
674static buf_T *text_prop_compare_buf;
675
676/*
677 * Function passed to qsort() to sort text properties.
678 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
679 * both have the same priority.
680 */
681 static int
682text_prop_compare(const void *s1, const void *s2)
683{
684 int idx1, idx2;
685 textprop_T *tp1, *tp2;
686 proptype_T *pt1, *pt2;
687 colnr_T col1, col2;
688
689 idx1 = *(int *)s1;
690 idx2 = *(int *)s2;
691 tp1 = &text_prop_compare_props[idx1];
692 tp2 = &text_prop_compare_props[idx2];
693 col1 = tp1->tp_col;
694 col2 = tp2->tp_col;
695 if (col1 == MAXCOL && col2 == MAXCOL)
696 {
697 int flags1 = 0;
698 int flags2 = 0;
699
700 // both props add text are after the line, order on 0: after (default),
701 // 1: right, 2: below (comes last)
702 if (tp1->tp_flags & TP_FLAG_ALIGN_RIGHT)
703 flags1 = 1;
704 if (tp1->tp_flags & TP_FLAG_ALIGN_BELOW)
705 flags1 = 2;
706 if (tp2->tp_flags & TP_FLAG_ALIGN_RIGHT)
707 flags2 = 1;
708 if (tp2->tp_flags & TP_FLAG_ALIGN_BELOW)
709 flags2 = 2;
710 if (flags1 != flags2)
711 return flags1 < flags2 ? 1 : -1;
712 }
713
714 // property that inserts text has priority over one that doesn't
715 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
716 return tp1->tp_id < 0 ? 1 : -1;
717
718 // check highest priority, defined by the type
719 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
720 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
721 if (pt1 != pt2)
722 {
723 if (pt1 == NULL)
724 return -1;
725 if (pt2 == NULL)
726 return 1;
727 if (pt1->pt_priority != pt2->pt_priority)
728 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
729 }
730
731 // same priority, one that starts first wins
732 if (col1 != col2)
733 return col1 < col2 ? 1 : -1;
734
735 // for a property with text the id can be used as tie breaker
736 if (tp1->tp_id < 0)
737 return tp1->tp_id > tp2->tp_id ? 1 : -1;
738
739 return 0;
740}
741
742/*
743 * Sort "count" text properties using an array if indexes "idxs" into the list
744 * of text props "props" for buffer "buf".
745 */
746 void
747sort_text_props(
748 buf_T *buf,
749 textprop_T *props,
750 int *idxs,
751 int count)
752{
753 text_prop_compare_buf = buf;
754 text_prop_compare_props = props;
755 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
756}
757
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100758/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200759 * Find text property "type_id" in the visible lines of window "wp".
760 * Match "id" when it is > 0.
761 * Returns FAIL when not found.
762 */
763 int
764find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
765 linenr_T *found_lnum)
766{
767 linenr_T lnum;
768 char_u *props;
769 int count;
770 int i;
771
772 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100773 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200774 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
775 {
776 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
777 for (i = 0; i < count; ++i)
778 {
779 mch_memmove(prop, props + i * sizeof(textprop_T),
780 sizeof(textprop_T));
781 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
782 {
783 *found_lnum = lnum;
784 return OK;
785 }
786 }
787 }
788 return FAIL;
789}
790
791/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100792 * Set the text properties for line "lnum" to "props" with length "len".
793 * If "len" is zero text properties are removed, "props" is not used.
794 * Any existing text properties are dropped.
795 * Only works for the current buffer.
796 */
797 static void
798set_text_props(linenr_T lnum, char_u *props, int len)
799{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100800 char_u *text;
801 char_u *newtext;
802 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100803
804 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100805 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100806 newtext = alloc(textlen + len);
807 if (newtext == NULL)
808 return;
809 mch_memmove(newtext, text, textlen);
810 if (len > 0)
811 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100812 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100813 vim_free(curbuf->b_ml.ml_line_ptr);
814 curbuf->b_ml.ml_line_ptr = newtext;
815 curbuf->b_ml.ml_line_len = textlen + len;
816 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
817}
818
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100819/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100820 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100821 */
822 void
823add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
824{
825 char_u *text;
826 char_u *newtext;
827 int proplen = text_prop_count * (int)sizeof(textprop_T);
828
829 text = ml_get(lnum);
830 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
831 if (newtext == NULL)
832 return;
833 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
834 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
835 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
836 vim_free(curbuf->b_ml.ml_line_ptr);
837 curbuf->b_ml.ml_line_ptr = newtext;
838 curbuf->b_ml.ml_line_len += proplen;
839 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
840}
841
Bram Moolenaare44336b2022-08-07 18:20:08 +0100842/*
843 * Function passed to qsort() for sorting proptype_T on pt_id.
844 */
845 static int
846compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100847{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100848 proptype_T *tp1 = *(proptype_T **)s1;
849 proptype_T *tp2 = *(proptype_T **)s2;
850
851 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
852}
853
854 static proptype_T *
855find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
856{
857 int low = 0;
858 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100859
Bram Moolenaar10246902022-08-08 17:08:05 +0100860 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100861 return NULL;
862
Bram Moolenaare44336b2022-08-07 18:20:08 +0100863 // Make the loopup faster by creating an array with pointers to
864 // hashtable entries, sorted on pt_id.
865 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100866 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100867 long todo;
868 hashitem_T *hi;
869 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100870
Bram Moolenaare44336b2022-08-07 18:20:08 +0100871 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
872 if (*array == NULL)
873 return NULL;
874 todo = (long)ht->ht_used;
875 for (hi = ht->ht_array; todo > 0; ++hi)
876 {
877 if (!HASHITEM_EMPTY(hi))
878 {
879 (*array)[i++] = HI2PT(hi);
880 --todo;
881 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100882 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100883 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
884 }
885
886 // binary search in the sorted array
887 high = ht->ht_used;
888 while (high > low)
889 {
890 int m = (high + low) / 2;
891
892 if ((*array)[m]->pt_id == id)
893 return (*array)[m];
894 if ((*array)[m]->pt_id > id)
895 high = m;
896 else
897 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100898 }
899 return NULL;
900}
901
902/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100903 * Fill 'dict' with text properties in 'prop'.
904 */
905 static void
906prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
907{
908 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200909 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100910
911 dict_add_number(dict, "col", prop->tp_col);
912 dict_add_number(dict, "length", prop->tp_len);
913 dict_add_number(dict, "id", prop->tp_id);
914 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
915 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200916
Bram Moolenaare44336b2022-08-07 18:20:08 +0100917 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200918 if (pt == NULL)
919 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100920 pt = find_type_by_id(global_proptypes, &global_proparray,
921 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200922 buflocal = FALSE;
923 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100924 if (pt != NULL)
925 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200926
927 if (buflocal)
928 dict_add_number(dict, "type_bufnr", buf->b_fnum);
929 else
930 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100931}
932
933/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100934 * Find a property type by ID in "buf" or globally.
935 * Returns NULL if not found.
936 */
937 proptype_T *
938text_prop_type_by_id(buf_T *buf, int id)
939{
940 proptype_T *type;
941
Bram Moolenaare44336b2022-08-07 18:20:08 +0100942 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100943 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100944 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100945 return type;
946}
947
948/*
949 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
950 */
951 void
952f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
953{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200954 linenr_T start;
955 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100956 linenr_T lnum;
957 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100958 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100959
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200960 if (in_vim9script()
961 && (check_for_number_arg(argvars, 0) == FAIL
962 || check_for_opt_number_arg(argvars, 1) == FAIL
963 || (argvars[1].v_type != VAR_UNKNOWN
964 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
965 return;
966
967 start = tv_get_number(&argvars[0]);
968 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100969 if (argvars[1].v_type != VAR_UNKNOWN)
970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100971 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100972 if (argvars[2].v_type != VAR_UNKNOWN)
973 {
974 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
975 return;
976 }
977 }
978 if (start < 1 || end < 1)
979 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200980 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100981 return;
982 }
983
984 for (lnum = start; lnum <= end; ++lnum)
985 {
986 char_u *text;
987 size_t len;
988
989 if (lnum > buf->b_ml.ml_line_count)
990 break;
991 text = ml_get_buf(buf, lnum, FALSE);
992 len = STRLEN(text) + 1;
993 if ((size_t)buf->b_ml.ml_line_len > len)
994 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100995 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100996 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
997 {
998 char_u *newtext = vim_strsave(text);
999
1000 // need to allocate the line now
1001 if (newtext == NULL)
1002 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001003 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1004 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001005 buf->b_ml.ml_line_ptr = newtext;
1006 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1007 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001008 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001009 }
1010 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001011 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001012 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001013}
1014
1015/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001016 * prop_find({props} [, {direction}])
1017 */
1018 void
1019f_prop_find(typval_T *argvars, typval_T *rettv)
1020{
1021 pos_T *cursor = &curwin->w_cursor;
1022 dict_T *dict;
1023 buf_T *buf = curbuf;
1024 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001025 int lnum_start;
1026 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001027 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001028 int id = 0;
1029 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001030 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001031 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001032 int lnum = -1;
1033 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001034 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001035 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001036
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001037 if (in_vim9script()
1038 && (check_for_dict_arg(argvars, 0) == FAIL
1039 || check_for_opt_string_arg(argvars, 1) == FAIL))
1040 return;
1041
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001042 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001043 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001044 dict = argvars[0].vval.v_dict;
1045
1046 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1047 return;
1048 if (buf->b_ml.ml_mfp == NULL)
1049 return;
1050
1051 if (argvars[1].v_type != VAR_UNKNOWN)
1052 {
1053 char_u *dir_s = tv_get_string(&argvars[1]);
1054
1055 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001056 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001057 else if (*dir_s != 'f')
1058 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001059 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001060 return;
1061 }
1062 }
1063
1064 di = dict_find(dict, (char_u *)"lnum", -1);
1065 if (di != NULL)
1066 lnum = tv_get_number(&di->di_tv);
1067
1068 di = dict_find(dict, (char_u *)"col", -1);
1069 if (di != NULL)
1070 col = tv_get_number(&di->di_tv);
1071
1072 if (lnum == -1)
1073 {
1074 lnum = cursor->lnum;
1075 col = cursor->col + 1;
1076 }
1077 else if (col == -1)
1078 col = 1;
1079
1080 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1081 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001082 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001083 return;
1084 }
1085
Bram Moolenaard61efa52022-07-23 09:52:04 +01001086 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001087
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001088 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001089 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001090 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001091 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001092 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001093 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001094 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001095 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001096 proptype_T *type = lookup_prop_type(name, buf);
1097
1098 if (type == NULL)
1099 return;
1100 type_id = type->pt_id;
1101 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001102 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001103 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001104 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001105 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001106 return;
1107 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001108 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001109 {
Ben Jacksona7704222022-08-20 20:54:51 +01001110 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001111 return;
1112 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001113
1114 lnum_start = lnum;
1115
1116 if (rettv_dict_alloc(rettv) == FAIL)
1117 return;
1118
1119 while (1)
1120 {
1121 char_u *text = ml_get_buf(buf, lnum, FALSE);
1122 size_t textlen = STRLEN(text) + 1;
1123 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001124 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001125 int i;
1126 textprop_T prop;
1127 int prop_start;
1128 int prop_end;
1129
LemonBoy9bd3ce22022-04-18 21:54:02 +01001130 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001131 {
1132 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001133 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001134
LemonBoy9bd3ce22022-04-18 21:54:02 +01001135 // For the very first line try to find the first property before or
1136 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001137 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001138 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001139 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001140 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001141 if (prop.tp_col > col)
1142 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001143 }
1144 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1145 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001146 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001147 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001148 : (id_found && prop.tp_id == id)
1149 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001150 {
1151 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001152 if (lnum_start == lnum
1153 && col >= prop.tp_col
1154 && (col <= prop.tp_col + prop.tp_len
1155 - (prop.tp_len != 0)))
1156 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001157
LemonBoy9bd3ce22022-04-18 21:54:02 +01001158 // The property was not continued from last line, it starts on
1159 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001160 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001161 // The property does not continue on the next line, it ends on
1162 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001163 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001164 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001165 seen_end = 1;
1166
1167 // Skip lines without the start flag.
1168 if (!prop_start)
1169 {
1170 // Always search backwards for start when search started
1171 // on a prop and we're not skipping.
1172 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001173 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001174 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001175 }
1176
1177 // If skipstart is true, skip the prop at start pos (even if
1178 // continued from another line).
1179 if (start_pos_has_prop && skipstart && !seen_end)
1180 {
1181 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001182 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001183 }
1184
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001185 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1186 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1187
1188 return;
1189 }
1190 }
1191
1192 if (dir > 0)
1193 {
1194 if (lnum >= buf->b_ml.ml_line_count)
1195 break;
1196 lnum++;
1197 }
1198 else
1199 {
1200 if (lnum <= 1)
1201 break;
1202 lnum--;
1203 }
1204 }
1205}
1206
1207/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001208 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1209 */
1210 static int
1211prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1212{
1213 int i;
1214
1215 for (i = 0; i < len; i++)
1216 if (types_or_ids[i] == type_or_id)
1217 return TRUE;
1218
1219 return FALSE;
1220}
1221
1222/*
1223 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1224 * If 'prop_types' is not NULL, then return only the text properties with
1225 * matching property type in the 'prop_types' array.
1226 * If 'prop_ids' is not NULL, then return only the text properties with
1227 * an identifier in the 'props_ids' array.
1228 * If 'add_lnum' is TRUE, then add the line number also to the text property
1229 * dictionary.
1230 */
1231 static void
1232get_props_in_line(
1233 buf_T *buf,
1234 linenr_T lnum,
1235 int *prop_types,
1236 int prop_types_len,
1237 int *prop_ids,
1238 int prop_ids_len,
1239 list_T *retlist,
1240 int add_lnum)
1241{
1242 char_u *text = ml_get_buf(buf, lnum, FALSE);
1243 size_t textlen = STRLEN(text) + 1;
1244 int count;
1245 int i;
1246 textprop_T prop;
1247
1248 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1249 for (i = 0; i < count; ++i)
1250 {
1251 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1252 sizeof(textprop_T));
1253 if ((prop_types == NULL
1254 || prop_type_or_id_in_list(prop_types, prop_types_len,
1255 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001256 && (prop_ids == NULL
1257 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1258 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001259 {
1260 dict_T *d = dict_alloc();
1261
1262 if (d == NULL)
1263 break;
1264 prop_fill_dict(d, &prop, buf);
1265 if (add_lnum)
1266 dict_add_number(d, "lnum", lnum);
1267 list_append_dict(retlist, d);
1268 }
1269 }
1270}
1271
1272/*
1273 * Convert a List of property type names into an array of property type
1274 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1275 * error. 'num_types' is set to the number of returned property types.
1276 */
1277 static int *
1278get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1279{
1280 int *prop_types;
1281 listitem_T *li;
1282 int i;
1283 char_u *name;
1284 proptype_T *type;
1285
1286 *num_types = 0;
1287
1288 prop_types = ALLOC_MULT(int, list_len(l));
1289 if (prop_types == NULL)
1290 return NULL;
1291
1292 i = 0;
1293 FOR_ALL_LIST_ITEMS(l, li)
1294 {
1295 if (li->li_tv.v_type != VAR_STRING)
1296 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001297 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001298 goto errret;
1299 }
1300 name = li->li_tv.vval.v_string;
1301 if (name == NULL)
1302 goto errret;
1303
1304 type = lookup_prop_type(name, buf);
1305 if (type == NULL)
1306 goto errret;
1307 prop_types[i++] = type->pt_id;
1308 }
1309
1310 *num_types = i;
1311 return prop_types;
1312
1313errret:
1314 VIM_CLEAR(prop_types);
1315 return NULL;
1316}
1317
1318/*
1319 * Convert a List of property identifiers into an array of property
1320 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1321 * error. 'num_ids' is set to the number of returned property identifiers.
1322 */
1323 static int *
1324get_prop_ids_from_list(list_T *l, int *num_ids)
1325{
1326 int *prop_ids;
1327 listitem_T *li;
1328 int i;
1329 int id;
1330 int error;
1331
1332 *num_ids = 0;
1333
1334 prop_ids = ALLOC_MULT(int, list_len(l));
1335 if (prop_ids == NULL)
1336 return NULL;
1337
1338 i = 0;
1339 FOR_ALL_LIST_ITEMS(l, li)
1340 {
1341 error = FALSE;
1342 id = tv_get_number_chk(&li->li_tv, &error);
1343 if (error)
1344 goto errret;
1345
1346 prop_ids[i++] = id;
1347 }
1348
1349 *num_ids = i;
1350 return prop_ids;
1351
1352errret:
1353 VIM_CLEAR(prop_ids);
1354 return NULL;
1355}
1356
1357/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001358 * prop_list({lnum} [, {bufnr}])
1359 */
1360 void
1361f_prop_list(typval_T *argvars, typval_T *rettv)
1362{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001363 linenr_T lnum;
1364 linenr_T start_lnum;
1365 linenr_T end_lnum;
1366 buf_T *buf = curbuf;
1367 int add_lnum = FALSE;
1368 int *prop_types = NULL;
1369 int prop_types_len = 0;
1370 int *prop_ids = NULL;
1371 int prop_ids_len = 0;
1372 list_T *l;
1373 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001374
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001375 if (in_vim9script()
1376 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001377 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001378 return;
1379
Bram Moolenaar93a10962022-06-16 11:42:09 +01001380 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001381 return;
1382
1383 // default: get text properties on current line
1384 start_lnum = tv_get_number(&argvars[0]);
1385 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001386 if (argvars[1].v_type != VAR_UNKNOWN)
1387 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001388 dict_T *d;
1389
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001390 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001391 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001392 d = argvars[1].vval.v_dict;
1393
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001394 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1395 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001396
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001397 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001398 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001399 if (di->di_tv.v_type != VAR_NUMBER)
1400 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001401 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001402 return;
1403 }
1404 end_lnum = tv_get_number(&di->di_tv);
1405 if (end_lnum < 0)
1406 // negative end_lnum is used as an offset from the last buffer
1407 // line
1408 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1409 else if (end_lnum > buf->b_ml.ml_line_count)
1410 end_lnum = buf->b_ml.ml_line_count;
1411 add_lnum = TRUE;
1412 }
1413 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1414 {
1415 if (di->di_tv.v_type != VAR_LIST)
1416 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001417 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001418 return;
1419 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001420
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001421 l = di->di_tv.vval.v_list;
1422 if (l != NULL && list_len(l) > 0)
1423 {
1424 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1425 if (prop_types == NULL)
1426 return;
1427 }
1428 }
1429 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1430 {
1431 if (di->di_tv.v_type != VAR_LIST)
1432 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001433 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001434 goto errret;
1435 }
1436
1437 l = di->di_tv.vval.v_list;
1438 if (l != NULL && list_len(l) > 0)
1439 {
1440 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1441 if (prop_ids == NULL)
1442 goto errret;
1443 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001444 }
1445 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001446 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1447 || end_lnum < 1 || end_lnum < start_lnum)
1448 emsg(_(e_invalid_range));
1449 else
1450 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1451 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1452 prop_ids, prop_ids_len,
1453 rettv->vval.v_list, add_lnum);
1454
1455errret:
1456 VIM_CLEAR(prop_types);
1457 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001458}
1459
1460/*
1461 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1462 */
1463 void
1464f_prop_remove(typval_T *argvars, typval_T *rettv)
1465{
1466 linenr_T start = 1;
1467 linenr_T end = 0;
1468 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001469 linenr_T first_changed = 0;
1470 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001471 dict_T *dict;
1472 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001473 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001474 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001475 int type_id = -1; // for a single "type"
1476 int *type_ids = NULL; // array, for a list of "types", allocated
1477 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001478 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001479 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001480
1481 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001482
1483 if (in_vim9script()
1484 && (check_for_dict_arg(argvars, 0) == FAIL
1485 || check_for_opt_number_arg(argvars, 1) == FAIL
1486 || (argvars[1].v_type != VAR_UNKNOWN
1487 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1488 return;
1489
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001490 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001491 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001492
1493 if (argvars[1].v_type != VAR_UNKNOWN)
1494 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001495 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001496 end = start;
1497 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001498 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001499 if (start < 1 || end < 1)
1500 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001501 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001502 return;
1503 }
1504 }
1505
1506 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001507 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1508 return;
1509 if (buf->b_ml.ml_mfp == NULL)
1510 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001511
Bram Moolenaard61efa52022-07-23 09:52:04 +01001512 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001513
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001514 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001515 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001516
1517 // if a specific type was supplied "type": check that (and ignore "types".
1518 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001519 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001520 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001521 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001522 proptype_T *type = lookup_prop_type(name, buf);
1523
1524 if (type == NULL)
1525 return;
1526 type_id = type->pt_id;
1527 }
Ben Jacksona7704222022-08-20 20:54:51 +01001528 if (dict_has_key(dict, "types"))
1529 {
1530 typval_T types;
1531 listitem_T *li = NULL;
1532
1533 dict_get_tv(dict, "types", &types);
1534 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1535 {
1536 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1537
1538 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1539 {
1540 proptype_T *prop_type;
1541
1542 if (li->li_tv.v_type != VAR_STRING)
1543 continue;
1544
1545 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1546
1547 if (!prop_type)
1548 goto cleanup_prop_remove;
1549
1550 type_ids[num_type_ids++] = prop_type->pt_id;
1551 }
1552 }
1553 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001554 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001555
Ben Jacksona7704222022-08-20 20:54:51 +01001556 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001557 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001558 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001559 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001560 }
Ben Jacksona7704222022-08-20 20:54:51 +01001561 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001562 {
Ben Jacksona7704222022-08-20 20:54:51 +01001563 emsg(_(e_need_id_and_type_or_types_with_both));
1564 goto cleanup_prop_remove;
1565 }
1566 if (type_id != -1 && num_type_ids > 0)
1567 {
1568 emsg(_(e_cannot_specify_both_type_and_types));
1569 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001570 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001571
1572 if (end == 0)
1573 end = buf->b_ml.ml_line_count;
1574 for (lnum = start; lnum <= end; ++lnum)
1575 {
1576 char_u *text;
1577 size_t len;
1578
1579 if (lnum > buf->b_ml.ml_line_count)
1580 break;
1581 text = ml_get_buf(buf, lnum, FALSE);
1582 len = STRLEN(text) + 1;
1583 if ((size_t)buf->b_ml.ml_line_len > len)
1584 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001585 static textprop_T textprop; // static because of alignment
1586 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001587
1588 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1589 / sizeof(textprop_T); ++idx)
1590 {
1591 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1592 + idx * sizeof(textprop_T);
1593 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001594 int matches_id = 0;
1595 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001596
1597 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001598
1599 matches_id = textprop.tp_id == id;
1600 if (num_type_ids > 0)
1601 {
1602 int idx2;
1603
1604 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1605 matches_type = textprop.tp_type == type_ids[idx2];
1606 }
1607 else
1608 {
1609 matches_type = textprop.tp_type == type_id;
1610 }
1611
1612 if (both ? matches_id && matches_type
1613 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001614 {
1615 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1616 {
1617 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1618
1619 // need to allocate the line to be able to change it
1620 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001621 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001622 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1623 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001624 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1625 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001626 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001627 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1628
1629 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001630 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001631 }
1632
1633 taillen = buf->b_ml.ml_line_len - len
1634 - (idx + 1) * sizeof(textprop_T);
1635 if (taillen > 0)
1636 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1637 taillen);
1638 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1639 --idx;
1640
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001641 if (textprop.tp_id < 0)
1642 {
1643 garray_T *gap = &buf->b_textprop_text;
1644 int ii = -textprop.tp_id - 1;
1645
1646 // negative ID: property with text - free the text
1647 if (ii < gap->ga_len)
1648 {
1649 char_u **p = ((char_u **)gap->ga_data) + ii;
1650 vim_free(*p);
1651 *p = NULL;
1652 did_remove_text = TRUE;
1653 }
1654 }
1655
Bram Moolenaar965c0442021-05-17 00:22:06 +02001656 if (first_changed == 0)
1657 first_changed = lnum;
1658 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001659 ++rettv->vval.v_number;
1660 if (!do_all)
1661 break;
1662 }
1663 }
1664 }
1665 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001666
Bram Moolenaar965c0442021-05-17 00:22:06 +02001667 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001668 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001669 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001670 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001671 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001672 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001673
1674 if (did_remove_text)
1675 {
1676 garray_T *gap = &buf->b_textprop_text;
1677
1678 // Reduce the growarray size for NULL pointers at the end.
1679 while (gap->ga_len > 0
1680 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1681 --gap->ga_len;
1682 }
Ben Jacksona7704222022-08-20 20:54:51 +01001683
1684cleanup_prop_remove:
1685 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001686}
1687
1688/*
1689 * Common for f_prop_type_add() and f_prop_type_change().
1690 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001691 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001692prop_type_set(typval_T *argvars, int add)
1693{
1694 char_u *name;
1695 buf_T *buf = NULL;
1696 dict_T *dict;
1697 dictitem_T *di;
1698 proptype_T *prop;
1699
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001700 if (in_vim9script()
1701 && (check_for_string_arg(argvars, 0) == FAIL
1702 || check_for_dict_arg(argvars, 1) == FAIL))
1703 return;
1704
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001705 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001706 if (*name == NUL)
1707 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001708 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001709 return;
1710 }
1711
1712 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1713 return;
1714 dict = argvars[1].vval.v_dict;
1715
Bram Moolenaare44336b2022-08-07 18:20:08 +01001716 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001717 if (add)
1718 {
1719 hashtab_T **htp;
1720
1721 if (prop != NULL)
1722 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001723 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001724 return;
1725 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001726 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001727 if (prop == NULL)
1728 return;
1729 STRCPY(prop->pt_name, name);
1730 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001731 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001732 if (buf == NULL)
1733 {
1734 htp = &global_proptypes;
1735 VIM_CLEAR(global_proparray);
1736 }
1737 else
1738 {
1739 htp = &buf->b_proptypes;
1740 VIM_CLEAR(buf->b_proparray);
1741 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001742 if (*htp == NULL)
1743 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001744 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001745 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001746 {
1747 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001748 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001749 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001750 hash_init(*htp);
1751 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001752 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001753 }
1754 else
1755 {
1756 if (prop == NULL)
1757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001758 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001759 return;
1760 }
1761 }
1762
1763 if (dict != NULL)
1764 {
1765 di = dict_find(dict, (char_u *)"highlight", -1);
1766 if (di != NULL)
1767 {
1768 char_u *highlight;
1769 int hl_id = 0;
1770
Bram Moolenaard61efa52022-07-23 09:52:04 +01001771 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001772 if (highlight != NULL && *highlight != NUL)
1773 hl_id = syn_name2id(highlight);
1774 if (hl_id <= 0)
1775 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001776 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001777 highlight == NULL ? (char_u *)"" : highlight);
1778 return;
1779 }
1780 prop->pt_hl_id = hl_id;
1781 }
1782
Bram Moolenaar58187f12019-05-05 16:33:47 +02001783 di = dict_find(dict, (char_u *)"combine", -1);
1784 if (di != NULL)
1785 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001786 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001787 prop->pt_flags |= PT_FLAG_COMBINE;
1788 else
1789 prop->pt_flags &= ~PT_FLAG_COMBINE;
1790 }
1791
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001792 di = dict_find(dict, (char_u *)"override", -1);
1793 if (di != NULL)
1794 {
1795 if (tv_get_bool(&di->di_tv))
1796 prop->pt_flags |= PT_FLAG_OVERRIDE;
1797 else
1798 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1799 }
1800
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001801 di = dict_find(dict, (char_u *)"priority", -1);
1802 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001803 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001804
1805 di = dict_find(dict, (char_u *)"start_incl", -1);
1806 if (di != NULL)
1807 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001808 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001809 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1810 else
1811 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1812 }
1813
1814 di = dict_find(dict, (char_u *)"end_incl", -1);
1815 if (di != NULL)
1816 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001817 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001818 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1819 else
1820 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1821 }
1822 }
1823}
1824
1825/*
1826 * prop_type_add({name}, {props})
1827 */
1828 void
1829f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1830{
1831 prop_type_set(argvars, TRUE);
1832}
1833
1834/*
1835 * prop_type_change({name}, {props})
1836 */
1837 void
1838f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1839{
1840 prop_type_set(argvars, FALSE);
1841}
1842
1843/*
1844 * prop_type_delete({name} [, {bufnr}])
1845 */
1846 void
1847f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1848{
1849 char_u *name;
1850 buf_T *buf = NULL;
1851 hashitem_T *hi;
1852
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001853 if (in_vim9script()
1854 && (check_for_string_arg(argvars, 0) == FAIL
1855 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1856 return;
1857
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001858 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001859 if (*name == NUL)
1860 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001861 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001862 return;
1863 }
1864
1865 if (argvars[1].v_type != VAR_UNKNOWN)
1866 {
1867 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1868 return;
1869 }
1870
Bram Moolenaare44336b2022-08-07 18:20:08 +01001871 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001872 if (hi != NULL)
1873 {
1874 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001875 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001876
1877 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001878 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001879 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001880 VIM_CLEAR(global_proparray);
1881 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001882 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001883 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001884 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001885 VIM_CLEAR(buf->b_proparray);
1886 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001887 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001888 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001889 }
1890}
1891
1892/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001893 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001894 */
1895 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001896f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001897{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001898 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001899
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001900 if (in_vim9script()
1901 && (check_for_string_arg(argvars, 0) == FAIL
1902 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1903 return;
1904
1905 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001906 if (*name == NUL)
1907 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001908 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001909 return;
1910 }
1911 if (rettv_dict_alloc(rettv) == OK)
1912 {
1913 proptype_T *prop = NULL;
1914 buf_T *buf = NULL;
1915
1916 if (argvars[1].v_type != VAR_UNKNOWN)
1917 {
1918 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1919 return;
1920 }
1921
Bram Moolenaare44336b2022-08-07 18:20:08 +01001922 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001923 if (prop != NULL)
1924 {
1925 dict_T *d = rettv->vval.v_dict;
1926
1927 if (prop->pt_hl_id > 0)
1928 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1929 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001930 dict_add_number(d, "combine",
1931 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001932 dict_add_number(d, "start_incl",
1933 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1934 dict_add_number(d, "end_incl",
1935 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1936 if (buf != NULL)
1937 dict_add_number(d, "bufnr", buf->b_fnum);
1938 }
1939 }
1940}
1941
1942 static void
1943list_types(hashtab_T *ht, list_T *l)
1944{
1945 long todo;
1946 hashitem_T *hi;
1947
1948 todo = (long)ht->ht_used;
1949 for (hi = ht->ht_array; todo > 0; ++hi)
1950 {
1951 if (!HASHITEM_EMPTY(hi))
1952 {
1953 proptype_T *prop = HI2PT(hi);
1954
1955 list_append_string(l, prop->pt_name, -1);
1956 --todo;
1957 }
1958 }
1959}
1960
1961/*
1962 * prop_type_list([{bufnr}])
1963 */
1964 void
1965f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1966{
1967 buf_T *buf = NULL;
1968
1969 if (rettv_list_alloc(rettv) == OK)
1970 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001971 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1972 return;
1973
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001974 if (argvars[0].v_type != VAR_UNKNOWN)
1975 {
1976 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1977 return;
1978 }
1979 if (buf == NULL)
1980 {
1981 if (global_proptypes != NULL)
1982 list_types(global_proptypes, rettv->vval.v_list);
1983 }
1984 else if (buf->b_proptypes != NULL)
1985 list_types(buf->b_proptypes, rettv->vval.v_list);
1986 }
1987}
1988
1989/*
1990 * Free all property types in "ht".
1991 */
1992 static void
1993clear_ht_prop_types(hashtab_T *ht)
1994{
1995 long todo;
1996 hashitem_T *hi;
1997
1998 if (ht == NULL)
1999 return;
2000
2001 todo = (long)ht->ht_used;
2002 for (hi = ht->ht_array; todo > 0; ++hi)
2003 {
2004 if (!HASHITEM_EMPTY(hi))
2005 {
2006 proptype_T *prop = HI2PT(hi);
2007
2008 vim_free(prop);
2009 --todo;
2010 }
2011 }
2012
2013 hash_clear(ht);
2014 vim_free(ht);
2015}
2016
2017#if defined(EXITFREE) || defined(PROTO)
2018/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002019 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002020 */
2021 void
2022clear_global_prop_types(void)
2023{
2024 clear_ht_prop_types(global_proptypes);
2025 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002026 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002027}
2028#endif
2029
2030/*
2031 * Free all property types for "buf".
2032 */
2033 void
2034clear_buf_prop_types(buf_T *buf)
2035{
2036 clear_ht_prop_types(buf->b_proptypes);
2037 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002038 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002039}
2040
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002041// Struct used to return two values from adjust_prop().
2042typedef struct
2043{
2044 int dirty; // if the property was changed
2045 int can_drop; // whether after this change, the prop may be removed
2046} adjustres_T;
2047
2048/*
2049 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2050 *
2051 * Note that "col" is zero-based, while tp_col is one-based.
2052 * Only for the current buffer.
2053 * "flags" can have:
2054 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002055 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002056 */
2057 static adjustres_T
2058adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002059 textprop_T *prop,
2060 colnr_T col,
2061 int added,
2062 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002063{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002064 proptype_T *pt;
2065 int start_incl;
2066 int end_incl;
2067 int droppable;
2068 adjustres_T res = {TRUE, FALSE};
2069
2070 // prop after end of the line doesn't move
2071 if (prop->tp_col == MAXCOL)
2072 {
2073 res.dirty = FALSE;
2074 return res;
2075 }
2076
2077 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2078 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002079 || (flags & APC_SUBSTITUTE)
2080 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002081 if (prop->tp_id < 0 && (flags & APC_INDENT))
2082 // when inserting indent just before a character with virtual text
2083 // shift the text property
2084 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002085 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002086 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002087 // do not drop zero-width props if they later can increase in size
2088 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002089
2090 if (added > 0)
2091 {
2092 if (col + 1 <= prop->tp_col
2093 - (start_incl || (prop->tp_len == 0 && end_incl)))
2094 // Change is entirely before the text property: Only shift
2095 prop->tp_col += added;
2096 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2097 // Insertion was inside text property
2098 prop->tp_len += added;
2099 }
2100 else if (prop->tp_col > col + 1)
2101 {
2102 if (prop->tp_col + added < col + 1)
2103 {
2104 prop->tp_len += (prop->tp_col - 1 - col) + added;
2105 prop->tp_col = col + 1;
2106 if (prop->tp_len <= 0)
2107 {
2108 prop->tp_len = 0;
2109 res.can_drop = droppable;
2110 }
2111 }
2112 else
2113 prop->tp_col += added;
2114 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002115 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2116 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002117 {
2118 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2119
2120 prop->tp_len += after > 0 ? added + after : added;
2121 res.can_drop = prop->tp_len <= 0 && droppable;
2122 }
2123 else
2124 res.dirty = FALSE;
2125
2126 return res;
2127}
2128
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002129/*
2130 * Adjust the columns of text properties in line "lnum" after position "col" to
2131 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002132 * Note that "col" is zero-based, while tp_col is one-based.
2133 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002134 * "flags" can have:
2135 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2136 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002137 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002138 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002139 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002140 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002141 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002142adjust_prop_columns(
2143 linenr_T lnum,
2144 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002145 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002146 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002147{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002148 int proplen;
2149 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002150 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002151 int ri, wi;
2152 size_t textlen;
2153
2154 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002155 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002156
2157 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2158 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002159 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002160 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002161
Bram Moolenaar196d1572019-01-02 23:47:18 +01002162 wi = 0; // write index
2163 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002164 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002165 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002166 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002167
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002168 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2169 res = adjust_prop(&prop, col, bytes_added, flags);
2170 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002171 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002172 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002173 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2174 && u_savesub(lnum) == FAIL)
2175 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002176 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002177
2178 // u_savesub() may have updated curbuf->b_ml, fetch it again
2179 if (curbuf->b_ml.ml_line_lnum != lnum)
2180 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002181 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002182 if (res.can_drop)
2183 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002184 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002185 ++wi;
2186 }
2187 if (dirty)
2188 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002189 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2190
2191 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002192 {
2193 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2194
2195 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2196 vim_free(curbuf->b_ml.ml_line_ptr);
2197 curbuf->b_ml.ml_line_ptr = p;
2198 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002199 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002200 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002201 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002202 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002203}
2204
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002205/*
2206 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002207 * "lnum_props" is the line that has the properties from before the split.
2208 * "lnum_top" is the top line.
2209 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002210 * "deleted" is the number of bytes deleted.
2211 */
2212 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002213adjust_props_for_split(
2214 linenr_T lnum_props,
2215 linenr_T lnum_top,
2216 int kept,
2217 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002218{
2219 char_u *props;
2220 int count;
2221 garray_T prevprop;
2222 garray_T nextprop;
2223 int i;
2224 int skipped = kept + deleted;
2225
2226 if (!curbuf->b_has_textprop)
2227 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002228
2229 // Get the text properties from "lnum_props".
2230 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002231 ga_init2(&prevprop, sizeof(textprop_T), 10);
2232 ga_init2(&nextprop, sizeof(textprop_T), 10);
2233
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002234 // Keep the relevant ones in the first line, reducing the length if needed.
2235 // Copy the ones that include the split to the second line.
2236 // Move the ones after the split to the second line.
2237 for (i = 0; i < count; ++i)
2238 {
2239 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002240 proptype_T *pt;
2241 int start_incl, end_incl;
2242 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002243
2244 // copy the prop to an aligned structure
2245 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2246
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002247 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2248 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2249 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002250 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2251 cont_next = prop.tp_col != MAXCOL
2252 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002253 // when a prop has text it is never copied
2254 if (prop.tp_id < 0 && cont_next)
2255 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002256
2257 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002258 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002259 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2260
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002261 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002262 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002263 if (p->tp_col + p->tp_len >= kept)
2264 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002265 if (cont_next)
2266 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002267 }
2268
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002269 // Only add the property to the next line if the length is bigger than
2270 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002271 if ((cont_next || prop.tp_col == MAXCOL)
2272 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002273 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002274 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002275
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002276 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002277 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002278 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002279 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002280 if (p->tp_col > skipped)
2281 p->tp_col -= skipped - 1;
2282 else
2283 {
2284 p->tp_len -= skipped - p->tp_col;
2285 p->tp_col = 1;
2286 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002287 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002288 if (cont_prev)
2289 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002290 }
2291 }
2292
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002293 set_text_props(lnum_top, prevprop.ga_data,
2294 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002295 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002296 set_text_props(lnum_top + 1, nextprop.ga_data,
2297 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002298 ga_clear(&nextprop);
2299}
2300
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002301/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002302 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002303 */
2304 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002305prepend_joined_props(
2306 char_u *new_props,
2307 int propcount,
2308 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002309 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002310 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002311 long col,
2312 int removed)
2313{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002314 char_u *props;
2315 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2316 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002317
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002318 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002319 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002320 textprop_T prop;
2321 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002322
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002323 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002324 if (prop.tp_col == MAXCOL && !last_line)
2325 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002326 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2327
2328 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002329 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002330
Bram Moolenaare175dc62022-08-01 22:18:50 +01002331 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002332 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2333 &prop, sizeof(prop));
2334 else
2335 {
2336 int j;
2337 int found = FALSE;
2338
2339 // Search for continuing prop.
2340 for (j = *props_remaining; j < propcount; ++j)
2341 {
2342 textprop_T op;
2343
2344 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2345 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2346 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002347 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002348 found = TRUE;
2349 op.tp_len += op.tp_col - prop.tp_col;
2350 op.tp_col = prop.tp_col;
2351 // Start/end is taken care of when deleting joined lines
2352 op.tp_flags = prop.tp_flags;
2353 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2354 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002355 }
2356 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002357 if (!found)
2358 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002359 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002360 }
2361}
2362
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002363#endif // FEAT_PROP_POPUP