blob: de3344f89c7a3befd2e8ed52d08e1b3748e3b592 [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
351 if (argvars[1].vval.v_list == NULL)
352 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000353 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200354 return;
355 }
356
357 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100358 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200359 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000360 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200361 return;
362 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100363 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100365 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100366 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200367
368 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
369 return;
370
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100371 // This must be done _before_ we start adding properties because property
372 // changes trigger buffer (memline) reorganisation, which needs this flag
373 // to be correctly set.
374 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200375 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
376 {
377 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
378 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000379 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200380 return;
381 }
382
383 pos_list = li->li_tv.vval.v_list;
384 start_lnum = list_find_nr(pos_list, 0L, &error);
385 start_col = list_find_nr(pos_list, 1L, &error);
386 end_lnum = list_find_nr(pos_list, 2L, &error);
387 end_col = list_find_nr(pos_list, 3L, &error);
388 if (error || start_lnum <= 0 || start_col <= 0
389 || end_lnum <= 0 || end_col <= 0)
390 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000391 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200392 return;
393 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100394 if (prop_add_one(buf, type_name, id, NULL, 0, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200395 start_col, end_col) == FAIL)
396 return;
397 }
398
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100399 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200400}
401
402/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100403 * Get the next ID to use for a textprop with text in buffer "buf".
404 */
405 static int
406get_textprop_id(buf_T *buf)
407{
408 // TODO: recycle deleted entries
409 return -(buf->b_textprop_text.ga_len + 1);
410}
411
412/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200413 * Shared between prop_add() and popup_create().
414 * "dict_arg" is the function argument of a dict containing "bufnr".
415 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100416 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200417 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100418 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200419prop_add_common(
420 linenr_T start_lnum,
421 colnr_T start_col,
422 dict_T *dict,
423 buf_T *default_buf,
424 typval_T *dict_arg)
425{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426 linenr_T end_lnum;
427 colnr_T end_col;
428 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200429 buf_T *buf = default_buf;
430 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100431 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100432 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100433 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100434
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100435 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100436 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000437 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100438 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100439 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100440 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100441
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100442 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100443 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100444 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100445 if (end_lnum < start_lnum)
446 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000447 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100448 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100449 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100450 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100451 else
452 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100453
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100454 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100455 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100456 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100457
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100458 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100459 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000460 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100461 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100462 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100463 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100465 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100466 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100467 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100468 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000470 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100471 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100472 }
473 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100474 else if (start_lnum == end_lnum)
475 end_col = start_col;
476 else
477 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100478
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100479 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100480 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100481
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100482 if (dict_has_key(dict, "text"))
483 {
484 text = dict_get_string(dict, "text", TRUE);
485 if (text == NULL)
486 goto theend;
487 // use a default length of 1 to make multiple props show up
488 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100489
490 if (dict_has_key(dict, "text_align"))
491 {
492 char_u *p = dict_get_string(dict, "text_align", FALSE);
493
494 if (p == NULL)
495 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100496 if (start_col != 0)
497 {
498 emsg(_(e_can_only_use_text_align_when_column_is_zero));
499 goto theend;
500 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100501 if (STRCMP(p, "right") == 0)
502 flags |= TP_FLAG_ALIGN_RIGHT;
503 else if (STRCMP(p, "below") == 0)
504 flags |= TP_FLAG_ALIGN_BELOW;
505 else if (STRCMP(p, "after") != 0)
506 {
507 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
508 goto theend;
509 }
510 }
511
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100512 if (dict_has_key(dict, "text_padding_left"))
513 {
514 text_padding_left = dict_get_number(dict, "text_padding_left");
515 if (text_padding_left < 0)
516 {
517 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
518 goto theend;
519 }
520 }
521
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100522 if (dict_has_key(dict, "text_wrap"))
523 {
524 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100525
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100526 if (p == NULL)
527 goto theend;
528 if (STRCMP(p, "wrap") == 0)
529 flags |= TP_FLAG_WRAP;
530 else if (STRCMP(p, "truncate") != 0)
531 {
532 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
533 goto theend;
534 }
535 }
536 }
537
538 // Column must be 1 or more for a normal text property; when "text" is
539 // present zero means it goes after the line.
540 if (start_col < (text == NULL ? 1 : 0))
541 {
542 semsg(_(e_invalid_column_number_nr), (long)start_col);
543 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100544 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100545 if (start_col > 0 && text_padding_left > 0)
546 {
547 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
548 goto theend;
549 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100550
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200551 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100552 goto theend;
553
554 if (id < 0 && buf->b_textprop_text.ga_len > 0)
555 {
556 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
557 goto theend;
558 }
559 if (text != NULL)
560 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100561
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100562 // This must be done _before_ we add the property because property changes
563 // trigger buffer (memline) reorganisation, which needs this flag to be
564 // correctly set.
565 buf->b_has_textprop = TRUE; // this is never reset
566
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100567 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100568 start_lnum, end_lnum, start_col, end_col);
569 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100570
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100571 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100572
573theend:
574 vim_free(text);
575 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100576}
577
578/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100579 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100580 * Returns the number of text properties and, when non-zero, a pointer to the
581 * first one in "props" (note that it is not aligned, therefore the char_u
582 * pointer).
583 */
584 int
585get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
586{
587 char_u *text;
588 size_t textlen;
589 size_t proplen;
590
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100591 // Be quick when no text property types have been defined or the buffer,
592 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200593 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100594 return 0;
595
596 // Fetch the line to get the ml_line_len field updated.
597 text = ml_get_buf(buf, lnum, will_change);
598 textlen = STRLEN(text) + 1;
599 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100600 if (proplen == 0)
601 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100602 if (proplen % sizeof(textprop_T) != 0)
603 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000604 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100605 return 0;
606 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100607 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100608 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100609}
610
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100611/*
612 * Return the number of text properties with "below" alignment in line "lnum".
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100613 * A "right" aligned property also goes below after a "below" or other "right"
614 * aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100615 */
616 int
617prop_count_below(buf_T *buf, linenr_T lnum)
618{
619 char_u *props;
620 int count = get_text_props(buf, lnum, &props, FALSE);
621 int result = 0;
622 textprop_T prop;
623 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100624 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100625
626 if (count == 0)
627 return 0;
628 for (i = 0; i < count; ++i)
629 {
630 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100631 if (prop.tp_col == MAXCOL)
632 {
633 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
634 || (next_right_goes_below
635 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
636 {
637 next_right_goes_below = TRUE;
638 ++result;
639 }
640 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
641 next_right_goes_below = TRUE;
642 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100643 }
644 return result;
645}
646
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200647/**
648 * Return the number of text properties on line "lnum" in the current buffer.
649 * When "only_starting" is true only text properties starting in this line will
650 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100651 * When "last_line" is FALSE then text properties after the line are not
652 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200653 */
654 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100655count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200656{
657 char_u *props;
658 int proplen = get_text_props(curbuf, lnum, &props, 0);
659 int result = proplen;
660 int i;
661 textprop_T prop;
662
Bram Moolenaare175dc62022-08-01 22:18:50 +0100663 for (i = 0; i < proplen; ++i)
664 {
665 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100666 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100667 // previous line, or when not in the last line and it is virtual text
668 // after the line.
669 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
670 || (!last_line && prop.tp_col == MAXCOL))
671 --result;
672 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200673 return result;
674}
675
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100676static textprop_T *text_prop_compare_props;
677static buf_T *text_prop_compare_buf;
678
679/*
680 * Function passed to qsort() to sort text properties.
681 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
682 * both have the same priority.
683 */
684 static int
685text_prop_compare(const void *s1, const void *s2)
686{
687 int idx1, idx2;
688 textprop_T *tp1, *tp2;
689 proptype_T *pt1, *pt2;
690 colnr_T col1, col2;
691
692 idx1 = *(int *)s1;
693 idx2 = *(int *)s2;
694 tp1 = &text_prop_compare_props[idx1];
695 tp2 = &text_prop_compare_props[idx2];
696 col1 = tp1->tp_col;
697 col2 = tp2->tp_col;
698 if (col1 == MAXCOL && col2 == MAXCOL)
699 {
700 int flags1 = 0;
701 int flags2 = 0;
702
703 // both props add text are after the line, order on 0: after (default),
704 // 1: right, 2: below (comes last)
705 if (tp1->tp_flags & TP_FLAG_ALIGN_RIGHT)
706 flags1 = 1;
707 if (tp1->tp_flags & TP_FLAG_ALIGN_BELOW)
708 flags1 = 2;
709 if (tp2->tp_flags & TP_FLAG_ALIGN_RIGHT)
710 flags2 = 1;
711 if (tp2->tp_flags & TP_FLAG_ALIGN_BELOW)
712 flags2 = 2;
713 if (flags1 != flags2)
714 return flags1 < flags2 ? 1 : -1;
715 }
716
717 // property that inserts text has priority over one that doesn't
718 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
719 return tp1->tp_id < 0 ? 1 : -1;
720
721 // check highest priority, defined by the type
722 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
723 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
724 if (pt1 != pt2)
725 {
726 if (pt1 == NULL)
727 return -1;
728 if (pt2 == NULL)
729 return 1;
730 if (pt1->pt_priority != pt2->pt_priority)
731 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
732 }
733
734 // same priority, one that starts first wins
735 if (col1 != col2)
736 return col1 < col2 ? 1 : -1;
737
738 // for a property with text the id can be used as tie breaker
739 if (tp1->tp_id < 0)
740 return tp1->tp_id > tp2->tp_id ? 1 : -1;
741
742 return 0;
743}
744
745/*
746 * Sort "count" text properties using an array if indexes "idxs" into the list
747 * of text props "props" for buffer "buf".
748 */
749 void
750sort_text_props(
751 buf_T *buf,
752 textprop_T *props,
753 int *idxs,
754 int count)
755{
756 text_prop_compare_buf = buf;
757 text_prop_compare_props = props;
758 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
759}
760
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100761/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200762 * Find text property "type_id" in the visible lines of window "wp".
763 * Match "id" when it is > 0.
764 * Returns FAIL when not found.
765 */
766 int
767find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
768 linenr_T *found_lnum)
769{
770 linenr_T lnum;
771 char_u *props;
772 int count;
773 int i;
774
775 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100776 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200777 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
778 {
779 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
780 for (i = 0; i < count; ++i)
781 {
782 mch_memmove(prop, props + i * sizeof(textprop_T),
783 sizeof(textprop_T));
784 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
785 {
786 *found_lnum = lnum;
787 return OK;
788 }
789 }
790 }
791 return FAIL;
792}
793
794/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100795 * Set the text properties for line "lnum" to "props" with length "len".
796 * If "len" is zero text properties are removed, "props" is not used.
797 * Any existing text properties are dropped.
798 * Only works for the current buffer.
799 */
800 static void
801set_text_props(linenr_T lnum, char_u *props, int len)
802{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100803 char_u *text;
804 char_u *newtext;
805 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100806
807 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100808 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100809 newtext = alloc(textlen + len);
810 if (newtext == NULL)
811 return;
812 mch_memmove(newtext, text, textlen);
813 if (len > 0)
814 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100815 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100816 vim_free(curbuf->b_ml.ml_line_ptr);
817 curbuf->b_ml.ml_line_ptr = newtext;
818 curbuf->b_ml.ml_line_len = textlen + len;
819 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
820}
821
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100822/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100823 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100824 */
825 void
826add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
827{
828 char_u *text;
829 char_u *newtext;
830 int proplen = text_prop_count * (int)sizeof(textprop_T);
831
832 text = ml_get(lnum);
833 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
834 if (newtext == NULL)
835 return;
836 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
837 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
838 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
839 vim_free(curbuf->b_ml.ml_line_ptr);
840 curbuf->b_ml.ml_line_ptr = newtext;
841 curbuf->b_ml.ml_line_len += proplen;
842 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
843}
844
Bram Moolenaare44336b2022-08-07 18:20:08 +0100845/*
846 * Function passed to qsort() for sorting proptype_T on pt_id.
847 */
848 static int
849compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100850{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100851 proptype_T *tp1 = *(proptype_T **)s1;
852 proptype_T *tp2 = *(proptype_T **)s2;
853
854 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
855}
856
857 static proptype_T *
858find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
859{
860 int low = 0;
861 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100862
Bram Moolenaar10246902022-08-08 17:08:05 +0100863 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100864 return NULL;
865
Bram Moolenaare44336b2022-08-07 18:20:08 +0100866 // Make the loopup faster by creating an array with pointers to
867 // hashtable entries, sorted on pt_id.
868 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100869 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100870 long todo;
871 hashitem_T *hi;
872 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100873
Bram Moolenaare44336b2022-08-07 18:20:08 +0100874 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
875 if (*array == NULL)
876 return NULL;
877 todo = (long)ht->ht_used;
878 for (hi = ht->ht_array; todo > 0; ++hi)
879 {
880 if (!HASHITEM_EMPTY(hi))
881 {
882 (*array)[i++] = HI2PT(hi);
883 --todo;
884 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100885 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100886 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
887 }
888
889 // binary search in the sorted array
890 high = ht->ht_used;
891 while (high > low)
892 {
893 int m = (high + low) / 2;
894
895 if ((*array)[m]->pt_id == id)
896 return (*array)[m];
897 if ((*array)[m]->pt_id > id)
898 high = m;
899 else
900 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100901 }
902 return NULL;
903}
904
905/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100906 * Fill 'dict' with text properties in 'prop'.
907 */
908 static void
909prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
910{
911 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200912 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100913
914 dict_add_number(dict, "col", prop->tp_col);
915 dict_add_number(dict, "length", prop->tp_len);
916 dict_add_number(dict, "id", prop->tp_id);
917 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
918 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200919
Bram Moolenaare44336b2022-08-07 18:20:08 +0100920 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200921 if (pt == NULL)
922 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100923 pt = find_type_by_id(global_proptypes, &global_proparray,
924 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200925 buflocal = FALSE;
926 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100927 if (pt != NULL)
928 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200929
930 if (buflocal)
931 dict_add_number(dict, "type_bufnr", buf->b_fnum);
932 else
933 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100934}
935
936/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100937 * Find a property type by ID in "buf" or globally.
938 * Returns NULL if not found.
939 */
940 proptype_T *
941text_prop_type_by_id(buf_T *buf, int id)
942{
943 proptype_T *type;
944
Bram Moolenaare44336b2022-08-07 18:20:08 +0100945 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100946 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100947 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100948 return type;
949}
950
951/*
952 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
953 */
954 void
955f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
956{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200957 linenr_T start;
958 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100959 linenr_T lnum;
960 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100961 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100962
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200963 if (in_vim9script()
964 && (check_for_number_arg(argvars, 0) == FAIL
965 || check_for_opt_number_arg(argvars, 1) == FAIL
966 || (argvars[1].v_type != VAR_UNKNOWN
967 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
968 return;
969
970 start = tv_get_number(&argvars[0]);
971 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100972 if (argvars[1].v_type != VAR_UNKNOWN)
973 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100974 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100975 if (argvars[2].v_type != VAR_UNKNOWN)
976 {
977 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
978 return;
979 }
980 }
981 if (start < 1 || end < 1)
982 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200983 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100984 return;
985 }
986
987 for (lnum = start; lnum <= end; ++lnum)
988 {
989 char_u *text;
990 size_t len;
991
992 if (lnum > buf->b_ml.ml_line_count)
993 break;
994 text = ml_get_buf(buf, lnum, FALSE);
995 len = STRLEN(text) + 1;
996 if ((size_t)buf->b_ml.ml_line_len > len)
997 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100998 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100999 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1000 {
1001 char_u *newtext = vim_strsave(text);
1002
1003 // need to allocate the line now
1004 if (newtext == NULL)
1005 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001006 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1007 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001008 buf->b_ml.ml_line_ptr = newtext;
1009 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1010 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001011 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001012 }
1013 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001014 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001015 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001016}
1017
1018/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001019 * prop_find({props} [, {direction}])
1020 */
1021 void
1022f_prop_find(typval_T *argvars, typval_T *rettv)
1023{
1024 pos_T *cursor = &curwin->w_cursor;
1025 dict_T *dict;
1026 buf_T *buf = curbuf;
1027 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001028 int lnum_start;
1029 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001030 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001031 int id = 0;
1032 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001033 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001034 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001035 int lnum = -1;
1036 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001037 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001038 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001039
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001040 if (in_vim9script()
1041 && (check_for_dict_arg(argvars, 0) == FAIL
1042 || check_for_opt_string_arg(argvars, 1) == FAIL))
1043 return;
1044
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001045 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001046 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001047 dict = argvars[0].vval.v_dict;
1048
1049 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1050 return;
1051 if (buf->b_ml.ml_mfp == NULL)
1052 return;
1053
1054 if (argvars[1].v_type != VAR_UNKNOWN)
1055 {
1056 char_u *dir_s = tv_get_string(&argvars[1]);
1057
1058 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001059 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001060 else if (*dir_s != 'f')
1061 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001062 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001063 return;
1064 }
1065 }
1066
1067 di = dict_find(dict, (char_u *)"lnum", -1);
1068 if (di != NULL)
1069 lnum = tv_get_number(&di->di_tv);
1070
1071 di = dict_find(dict, (char_u *)"col", -1);
1072 if (di != NULL)
1073 col = tv_get_number(&di->di_tv);
1074
1075 if (lnum == -1)
1076 {
1077 lnum = cursor->lnum;
1078 col = cursor->col + 1;
1079 }
1080 else if (col == -1)
1081 col = 1;
1082
1083 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1084 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001085 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001086 return;
1087 }
1088
Bram Moolenaard61efa52022-07-23 09:52:04 +01001089 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001090
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001091 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001092 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001093 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001094 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001095 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001096 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001097 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001098 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001099 proptype_T *type = lookup_prop_type(name, buf);
1100
1101 if (type == NULL)
1102 return;
1103 type_id = type->pt_id;
1104 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001105 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001106 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001107 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001108 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001109 return;
1110 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001111 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001112 {
Ben Jacksona7704222022-08-20 20:54:51 +01001113 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001114 return;
1115 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001116
1117 lnum_start = lnum;
1118
1119 if (rettv_dict_alloc(rettv) == FAIL)
1120 return;
1121
1122 while (1)
1123 {
1124 char_u *text = ml_get_buf(buf, lnum, FALSE);
1125 size_t textlen = STRLEN(text) + 1;
1126 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001127 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001128 int i;
1129 textprop_T prop;
1130 int prop_start;
1131 int prop_end;
1132
LemonBoy9bd3ce22022-04-18 21:54:02 +01001133 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001134 {
1135 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001136 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001137
LemonBoy9bd3ce22022-04-18 21:54:02 +01001138 // For the very first line try to find the first property before or
1139 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001140 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001141 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001142 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001143 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001144 if (prop.tp_col > col)
1145 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001146 }
1147 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1148 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001149 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001150 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001151 : (id_found && prop.tp_id == id)
1152 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001153 {
1154 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001155 if (lnum_start == lnum
1156 && col >= prop.tp_col
1157 && (col <= prop.tp_col + prop.tp_len
1158 - (prop.tp_len != 0)))
1159 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001160
LemonBoy9bd3ce22022-04-18 21:54:02 +01001161 // The property was not continued from last line, it starts on
1162 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001163 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001164 // The property does not continue on the next line, it ends on
1165 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001166 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001167 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001168 seen_end = 1;
1169
1170 // Skip lines without the start flag.
1171 if (!prop_start)
1172 {
1173 // Always search backwards for start when search started
1174 // on a prop and we're not skipping.
1175 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001176 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001177 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001178 }
1179
1180 // If skipstart is true, skip the prop at start pos (even if
1181 // continued from another line).
1182 if (start_pos_has_prop && skipstart && !seen_end)
1183 {
1184 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001185 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001186 }
1187
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001188 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1189 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1190
1191 return;
1192 }
1193 }
1194
1195 if (dir > 0)
1196 {
1197 if (lnum >= buf->b_ml.ml_line_count)
1198 break;
1199 lnum++;
1200 }
1201 else
1202 {
1203 if (lnum <= 1)
1204 break;
1205 lnum--;
1206 }
1207 }
1208}
1209
1210/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001211 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1212 */
1213 static int
1214prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1215{
1216 int i;
1217
1218 for (i = 0; i < len; i++)
1219 if (types_or_ids[i] == type_or_id)
1220 return TRUE;
1221
1222 return FALSE;
1223}
1224
1225/*
1226 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1227 * If 'prop_types' is not NULL, then return only the text properties with
1228 * matching property type in the 'prop_types' array.
1229 * If 'prop_ids' is not NULL, then return only the text properties with
1230 * an identifier in the 'props_ids' array.
1231 * If 'add_lnum' is TRUE, then add the line number also to the text property
1232 * dictionary.
1233 */
1234 static void
1235get_props_in_line(
1236 buf_T *buf,
1237 linenr_T lnum,
1238 int *prop_types,
1239 int prop_types_len,
1240 int *prop_ids,
1241 int prop_ids_len,
1242 list_T *retlist,
1243 int add_lnum)
1244{
1245 char_u *text = ml_get_buf(buf, lnum, FALSE);
1246 size_t textlen = STRLEN(text) + 1;
1247 int count;
1248 int i;
1249 textprop_T prop;
1250
1251 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1252 for (i = 0; i < count; ++i)
1253 {
1254 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1255 sizeof(textprop_T));
1256 if ((prop_types == NULL
1257 || prop_type_or_id_in_list(prop_types, prop_types_len,
1258 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001259 && (prop_ids == NULL
1260 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1261 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001262 {
1263 dict_T *d = dict_alloc();
1264
1265 if (d == NULL)
1266 break;
1267 prop_fill_dict(d, &prop, buf);
1268 if (add_lnum)
1269 dict_add_number(d, "lnum", lnum);
1270 list_append_dict(retlist, d);
1271 }
1272 }
1273}
1274
1275/*
1276 * Convert a List of property type names into an array of property type
1277 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1278 * error. 'num_types' is set to the number of returned property types.
1279 */
1280 static int *
1281get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1282{
1283 int *prop_types;
1284 listitem_T *li;
1285 int i;
1286 char_u *name;
1287 proptype_T *type;
1288
1289 *num_types = 0;
1290
1291 prop_types = ALLOC_MULT(int, list_len(l));
1292 if (prop_types == NULL)
1293 return NULL;
1294
1295 i = 0;
1296 FOR_ALL_LIST_ITEMS(l, li)
1297 {
1298 if (li->li_tv.v_type != VAR_STRING)
1299 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001300 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001301 goto errret;
1302 }
1303 name = li->li_tv.vval.v_string;
1304 if (name == NULL)
1305 goto errret;
1306
1307 type = lookup_prop_type(name, buf);
1308 if (type == NULL)
1309 goto errret;
1310 prop_types[i++] = type->pt_id;
1311 }
1312
1313 *num_types = i;
1314 return prop_types;
1315
1316errret:
1317 VIM_CLEAR(prop_types);
1318 return NULL;
1319}
1320
1321/*
1322 * Convert a List of property identifiers into an array of property
1323 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1324 * error. 'num_ids' is set to the number of returned property identifiers.
1325 */
1326 static int *
1327get_prop_ids_from_list(list_T *l, int *num_ids)
1328{
1329 int *prop_ids;
1330 listitem_T *li;
1331 int i;
1332 int id;
1333 int error;
1334
1335 *num_ids = 0;
1336
1337 prop_ids = ALLOC_MULT(int, list_len(l));
1338 if (prop_ids == NULL)
1339 return NULL;
1340
1341 i = 0;
1342 FOR_ALL_LIST_ITEMS(l, li)
1343 {
1344 error = FALSE;
1345 id = tv_get_number_chk(&li->li_tv, &error);
1346 if (error)
1347 goto errret;
1348
1349 prop_ids[i++] = id;
1350 }
1351
1352 *num_ids = i;
1353 return prop_ids;
1354
1355errret:
1356 VIM_CLEAR(prop_ids);
1357 return NULL;
1358}
1359
1360/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001361 * prop_list({lnum} [, {bufnr}])
1362 */
1363 void
1364f_prop_list(typval_T *argvars, typval_T *rettv)
1365{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001366 linenr_T lnum;
1367 linenr_T start_lnum;
1368 linenr_T end_lnum;
1369 buf_T *buf = curbuf;
1370 int add_lnum = FALSE;
1371 int *prop_types = NULL;
1372 int prop_types_len = 0;
1373 int *prop_ids = NULL;
1374 int prop_ids_len = 0;
1375 list_T *l;
1376 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001377
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001378 if (in_vim9script()
1379 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001380 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001381 return;
1382
Bram Moolenaar93a10962022-06-16 11:42:09 +01001383 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001384 return;
1385
1386 // default: get text properties on current line
1387 start_lnum = tv_get_number(&argvars[0]);
1388 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001389 if (argvars[1].v_type != VAR_UNKNOWN)
1390 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001391 dict_T *d;
1392
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001393 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001394 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001395 d = argvars[1].vval.v_dict;
1396
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001397 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1398 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001399
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001400 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001401 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001402 if (di->di_tv.v_type != VAR_NUMBER)
1403 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001404 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001405 return;
1406 }
1407 end_lnum = tv_get_number(&di->di_tv);
1408 if (end_lnum < 0)
1409 // negative end_lnum is used as an offset from the last buffer
1410 // line
1411 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1412 else if (end_lnum > buf->b_ml.ml_line_count)
1413 end_lnum = buf->b_ml.ml_line_count;
1414 add_lnum = TRUE;
1415 }
1416 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1417 {
1418 if (di->di_tv.v_type != VAR_LIST)
1419 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001420 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001421 return;
1422 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001423
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001424 l = di->di_tv.vval.v_list;
1425 if (l != NULL && list_len(l) > 0)
1426 {
1427 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1428 if (prop_types == NULL)
1429 return;
1430 }
1431 }
1432 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1433 {
1434 if (di->di_tv.v_type != VAR_LIST)
1435 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001436 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001437 goto errret;
1438 }
1439
1440 l = di->di_tv.vval.v_list;
1441 if (l != NULL && list_len(l) > 0)
1442 {
1443 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1444 if (prop_ids == NULL)
1445 goto errret;
1446 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001447 }
1448 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001449 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1450 || end_lnum < 1 || end_lnum < start_lnum)
1451 emsg(_(e_invalid_range));
1452 else
1453 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1454 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1455 prop_ids, prop_ids_len,
1456 rettv->vval.v_list, add_lnum);
1457
1458errret:
1459 VIM_CLEAR(prop_types);
1460 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001461}
1462
1463/*
1464 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1465 */
1466 void
1467f_prop_remove(typval_T *argvars, typval_T *rettv)
1468{
1469 linenr_T start = 1;
1470 linenr_T end = 0;
1471 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001472 linenr_T first_changed = 0;
1473 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001474 dict_T *dict;
1475 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001476 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001477 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001478 int type_id = -1; // for a single "type"
1479 int *type_ids = NULL; // array, for a list of "types", allocated
1480 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001481 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001482 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001483
1484 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001485
1486 if (in_vim9script()
1487 && (check_for_dict_arg(argvars, 0) == FAIL
1488 || check_for_opt_number_arg(argvars, 1) == FAIL
1489 || (argvars[1].v_type != VAR_UNKNOWN
1490 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1491 return;
1492
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001493 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495
1496 if (argvars[1].v_type != VAR_UNKNOWN)
1497 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001498 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001499 end = start;
1500 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001501 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001502 if (start < 1 || end < 1)
1503 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001504 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001505 return;
1506 }
1507 }
1508
1509 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001510 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1511 return;
1512 if (buf->b_ml.ml_mfp == NULL)
1513 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001514
Bram Moolenaard61efa52022-07-23 09:52:04 +01001515 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001516
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001517 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001518 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001519
1520 // if a specific type was supplied "type": check that (and ignore "types".
1521 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001522 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001523 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001524 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001525 proptype_T *type = lookup_prop_type(name, buf);
1526
1527 if (type == NULL)
1528 return;
1529 type_id = type->pt_id;
1530 }
Ben Jacksona7704222022-08-20 20:54:51 +01001531 if (dict_has_key(dict, "types"))
1532 {
1533 typval_T types;
1534 listitem_T *li = NULL;
1535
1536 dict_get_tv(dict, "types", &types);
1537 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1538 {
1539 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1540
1541 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1542 {
1543 proptype_T *prop_type;
1544
1545 if (li->li_tv.v_type != VAR_STRING)
1546 continue;
1547
1548 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1549
1550 if (!prop_type)
1551 goto cleanup_prop_remove;
1552
1553 type_ids[num_type_ids++] = prop_type->pt_id;
1554 }
1555 }
1556 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001557 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001558
Ben Jacksona7704222022-08-20 20:54:51 +01001559 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001560 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001561 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001562 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001563 }
Ben Jacksona7704222022-08-20 20:54:51 +01001564 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001565 {
Ben Jacksona7704222022-08-20 20:54:51 +01001566 emsg(_(e_need_id_and_type_or_types_with_both));
1567 goto cleanup_prop_remove;
1568 }
1569 if (type_id != -1 && num_type_ids > 0)
1570 {
1571 emsg(_(e_cannot_specify_both_type_and_types));
1572 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001573 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001574
1575 if (end == 0)
1576 end = buf->b_ml.ml_line_count;
1577 for (lnum = start; lnum <= end; ++lnum)
1578 {
1579 char_u *text;
1580 size_t len;
1581
1582 if (lnum > buf->b_ml.ml_line_count)
1583 break;
1584 text = ml_get_buf(buf, lnum, FALSE);
1585 len = STRLEN(text) + 1;
1586 if ((size_t)buf->b_ml.ml_line_len > len)
1587 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001588 static textprop_T textprop; // static because of alignment
1589 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001590
1591 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1592 / sizeof(textprop_T); ++idx)
1593 {
1594 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1595 + idx * sizeof(textprop_T);
1596 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001597 int matches_id = 0;
1598 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001599
1600 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001601
1602 matches_id = textprop.tp_id == id;
1603 if (num_type_ids > 0)
1604 {
1605 int idx2;
1606
1607 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1608 matches_type = textprop.tp_type == type_ids[idx2];
1609 }
1610 else
1611 {
1612 matches_type = textprop.tp_type == type_id;
1613 }
1614
1615 if (both ? matches_id && matches_type
1616 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001617 {
1618 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1619 {
1620 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1621
1622 // need to allocate the line to be able to change it
1623 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001624 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001625 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1626 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001627 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1628 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001629 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001630 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1631
1632 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001633 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001634 }
1635
1636 taillen = buf->b_ml.ml_line_len - len
1637 - (idx + 1) * sizeof(textprop_T);
1638 if (taillen > 0)
1639 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1640 taillen);
1641 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1642 --idx;
1643
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001644 if (textprop.tp_id < 0)
1645 {
1646 garray_T *gap = &buf->b_textprop_text;
1647 int ii = -textprop.tp_id - 1;
1648
1649 // negative ID: property with text - free the text
1650 if (ii < gap->ga_len)
1651 {
1652 char_u **p = ((char_u **)gap->ga_data) + ii;
1653 vim_free(*p);
1654 *p = NULL;
1655 did_remove_text = TRUE;
1656 }
1657 }
1658
Bram Moolenaar965c0442021-05-17 00:22:06 +02001659 if (first_changed == 0)
1660 first_changed = lnum;
1661 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001662 ++rettv->vval.v_number;
1663 if (!do_all)
1664 break;
1665 }
1666 }
1667 }
1668 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001669
Bram Moolenaar965c0442021-05-17 00:22:06 +02001670 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001671 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001672 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001673 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001674 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001675 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001676
1677 if (did_remove_text)
1678 {
1679 garray_T *gap = &buf->b_textprop_text;
1680
1681 // Reduce the growarray size for NULL pointers at the end.
1682 while (gap->ga_len > 0
1683 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1684 --gap->ga_len;
1685 }
Ben Jacksona7704222022-08-20 20:54:51 +01001686
1687cleanup_prop_remove:
1688 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001689}
1690
1691/*
1692 * Common for f_prop_type_add() and f_prop_type_change().
1693 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001694 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001695prop_type_set(typval_T *argvars, int add)
1696{
1697 char_u *name;
1698 buf_T *buf = NULL;
1699 dict_T *dict;
1700 dictitem_T *di;
1701 proptype_T *prop;
1702
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001703 if (in_vim9script()
1704 && (check_for_string_arg(argvars, 0) == FAIL
1705 || check_for_dict_arg(argvars, 1) == FAIL))
1706 return;
1707
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001708 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001709 if (*name == NUL)
1710 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001711 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001712 return;
1713 }
1714
1715 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1716 return;
1717 dict = argvars[1].vval.v_dict;
1718
Bram Moolenaare44336b2022-08-07 18:20:08 +01001719 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001720 if (add)
1721 {
1722 hashtab_T **htp;
1723
1724 if (prop != NULL)
1725 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001726 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001727 return;
1728 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001729 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001730 if (prop == NULL)
1731 return;
1732 STRCPY(prop->pt_name, name);
1733 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001734 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001735 if (buf == NULL)
1736 {
1737 htp = &global_proptypes;
1738 VIM_CLEAR(global_proparray);
1739 }
1740 else
1741 {
1742 htp = &buf->b_proptypes;
1743 VIM_CLEAR(buf->b_proparray);
1744 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001745 if (*htp == NULL)
1746 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001747 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001748 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001749 {
1750 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001751 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001752 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001753 hash_init(*htp);
1754 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001755 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001756 }
1757 else
1758 {
1759 if (prop == NULL)
1760 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001761 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001762 return;
1763 }
1764 }
1765
1766 if (dict != NULL)
1767 {
1768 di = dict_find(dict, (char_u *)"highlight", -1);
1769 if (di != NULL)
1770 {
1771 char_u *highlight;
1772 int hl_id = 0;
1773
Bram Moolenaard61efa52022-07-23 09:52:04 +01001774 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001775 if (highlight != NULL && *highlight != NUL)
1776 hl_id = syn_name2id(highlight);
1777 if (hl_id <= 0)
1778 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001779 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001780 highlight == NULL ? (char_u *)"" : highlight);
1781 return;
1782 }
1783 prop->pt_hl_id = hl_id;
1784 }
1785
Bram Moolenaar58187f12019-05-05 16:33:47 +02001786 di = dict_find(dict, (char_u *)"combine", -1);
1787 if (di != NULL)
1788 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001789 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001790 prop->pt_flags |= PT_FLAG_COMBINE;
1791 else
1792 prop->pt_flags &= ~PT_FLAG_COMBINE;
1793 }
1794
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001795 di = dict_find(dict, (char_u *)"override", -1);
1796 if (di != NULL)
1797 {
1798 if (tv_get_bool(&di->di_tv))
1799 prop->pt_flags |= PT_FLAG_OVERRIDE;
1800 else
1801 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1802 }
1803
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001804 di = dict_find(dict, (char_u *)"priority", -1);
1805 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001806 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001807
1808 di = dict_find(dict, (char_u *)"start_incl", -1);
1809 if (di != NULL)
1810 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001811 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001812 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1813 else
1814 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1815 }
1816
1817 di = dict_find(dict, (char_u *)"end_incl", -1);
1818 if (di != NULL)
1819 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001820 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001821 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1822 else
1823 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1824 }
1825 }
1826}
1827
1828/*
1829 * prop_type_add({name}, {props})
1830 */
1831 void
1832f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1833{
1834 prop_type_set(argvars, TRUE);
1835}
1836
1837/*
1838 * prop_type_change({name}, {props})
1839 */
1840 void
1841f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1842{
1843 prop_type_set(argvars, FALSE);
1844}
1845
1846/*
1847 * prop_type_delete({name} [, {bufnr}])
1848 */
1849 void
1850f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1851{
1852 char_u *name;
1853 buf_T *buf = NULL;
1854 hashitem_T *hi;
1855
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001856 if (in_vim9script()
1857 && (check_for_string_arg(argvars, 0) == FAIL
1858 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1859 return;
1860
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001861 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001862 if (*name == NUL)
1863 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001864 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001865 return;
1866 }
1867
1868 if (argvars[1].v_type != VAR_UNKNOWN)
1869 {
1870 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1871 return;
1872 }
1873
Bram Moolenaare44336b2022-08-07 18:20:08 +01001874 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001875 if (hi != NULL)
1876 {
1877 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001878 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001879
1880 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001881 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001882 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001883 VIM_CLEAR(global_proparray);
1884 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001885 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001886 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001887 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001888 VIM_CLEAR(buf->b_proparray);
1889 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001890 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001891 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001892 }
1893}
1894
1895/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001896 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001897 */
1898 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001899f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001900{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001901 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001902
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001903 if (in_vim9script()
1904 && (check_for_string_arg(argvars, 0) == FAIL
1905 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1906 return;
1907
1908 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001909 if (*name == NUL)
1910 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001911 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001912 return;
1913 }
1914 if (rettv_dict_alloc(rettv) == OK)
1915 {
1916 proptype_T *prop = NULL;
1917 buf_T *buf = NULL;
1918
1919 if (argvars[1].v_type != VAR_UNKNOWN)
1920 {
1921 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1922 return;
1923 }
1924
Bram Moolenaare44336b2022-08-07 18:20:08 +01001925 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001926 if (prop != NULL)
1927 {
1928 dict_T *d = rettv->vval.v_dict;
1929
1930 if (prop->pt_hl_id > 0)
1931 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1932 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001933 dict_add_number(d, "combine",
1934 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001935 dict_add_number(d, "start_incl",
1936 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1937 dict_add_number(d, "end_incl",
1938 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1939 if (buf != NULL)
1940 dict_add_number(d, "bufnr", buf->b_fnum);
1941 }
1942 }
1943}
1944
1945 static void
1946list_types(hashtab_T *ht, list_T *l)
1947{
1948 long todo;
1949 hashitem_T *hi;
1950
1951 todo = (long)ht->ht_used;
1952 for (hi = ht->ht_array; todo > 0; ++hi)
1953 {
1954 if (!HASHITEM_EMPTY(hi))
1955 {
1956 proptype_T *prop = HI2PT(hi);
1957
1958 list_append_string(l, prop->pt_name, -1);
1959 --todo;
1960 }
1961 }
1962}
1963
1964/*
1965 * prop_type_list([{bufnr}])
1966 */
1967 void
1968f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1969{
1970 buf_T *buf = NULL;
1971
1972 if (rettv_list_alloc(rettv) == OK)
1973 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001974 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1975 return;
1976
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001977 if (argvars[0].v_type != VAR_UNKNOWN)
1978 {
1979 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1980 return;
1981 }
1982 if (buf == NULL)
1983 {
1984 if (global_proptypes != NULL)
1985 list_types(global_proptypes, rettv->vval.v_list);
1986 }
1987 else if (buf->b_proptypes != NULL)
1988 list_types(buf->b_proptypes, rettv->vval.v_list);
1989 }
1990}
1991
1992/*
1993 * Free all property types in "ht".
1994 */
1995 static void
1996clear_ht_prop_types(hashtab_T *ht)
1997{
1998 long todo;
1999 hashitem_T *hi;
2000
2001 if (ht == NULL)
2002 return;
2003
2004 todo = (long)ht->ht_used;
2005 for (hi = ht->ht_array; todo > 0; ++hi)
2006 {
2007 if (!HASHITEM_EMPTY(hi))
2008 {
2009 proptype_T *prop = HI2PT(hi);
2010
2011 vim_free(prop);
2012 --todo;
2013 }
2014 }
2015
2016 hash_clear(ht);
2017 vim_free(ht);
2018}
2019
2020#if defined(EXITFREE) || defined(PROTO)
2021/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002022 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002023 */
2024 void
2025clear_global_prop_types(void)
2026{
2027 clear_ht_prop_types(global_proptypes);
2028 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002029 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002030}
2031#endif
2032
2033/*
2034 * Free all property types for "buf".
2035 */
2036 void
2037clear_buf_prop_types(buf_T *buf)
2038{
2039 clear_ht_prop_types(buf->b_proptypes);
2040 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002041 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002042}
2043
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002044// Struct used to return two values from adjust_prop().
2045typedef struct
2046{
2047 int dirty; // if the property was changed
2048 int can_drop; // whether after this change, the prop may be removed
2049} adjustres_T;
2050
2051/*
2052 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2053 *
2054 * Note that "col" is zero-based, while tp_col is one-based.
2055 * Only for the current buffer.
2056 * "flags" can have:
2057 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002058 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002059 */
2060 static adjustres_T
2061adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002062 textprop_T *prop,
2063 colnr_T col,
2064 int added,
2065 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002066{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002067 proptype_T *pt;
2068 int start_incl;
2069 int end_incl;
2070 int droppable;
2071 adjustres_T res = {TRUE, FALSE};
2072
2073 // prop after end of the line doesn't move
2074 if (prop->tp_col == MAXCOL)
2075 {
2076 res.dirty = FALSE;
2077 return res;
2078 }
2079
2080 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2081 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002082 || (flags & APC_SUBSTITUTE)
2083 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002084 if (prop->tp_id < 0 && (flags & APC_INDENT))
2085 // when inserting indent just before a character with virtual text
2086 // shift the text property
2087 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002088 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002089 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002090 // do not drop zero-width props if they later can increase in size
2091 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002092
2093 if (added > 0)
2094 {
2095 if (col + 1 <= prop->tp_col
2096 - (start_incl || (prop->tp_len == 0 && end_incl)))
2097 // Change is entirely before the text property: Only shift
2098 prop->tp_col += added;
2099 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2100 // Insertion was inside text property
2101 prop->tp_len += added;
2102 }
2103 else if (prop->tp_col > col + 1)
2104 {
2105 if (prop->tp_col + added < col + 1)
2106 {
2107 prop->tp_len += (prop->tp_col - 1 - col) + added;
2108 prop->tp_col = col + 1;
2109 if (prop->tp_len <= 0)
2110 {
2111 prop->tp_len = 0;
2112 res.can_drop = droppable;
2113 }
2114 }
2115 else
2116 prop->tp_col += added;
2117 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002118 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2119 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002120 {
2121 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2122
2123 prop->tp_len += after > 0 ? added + after : added;
2124 res.can_drop = prop->tp_len <= 0 && droppable;
2125 }
2126 else
2127 res.dirty = FALSE;
2128
2129 return res;
2130}
2131
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002132/*
2133 * Adjust the columns of text properties in line "lnum" after position "col" to
2134 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002135 * Note that "col" is zero-based, while tp_col is one-based.
2136 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002137 * "flags" can have:
2138 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2139 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002140 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002141 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002142 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002143 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002144 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002145adjust_prop_columns(
2146 linenr_T lnum,
2147 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002148 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002149 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002150{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002151 int proplen;
2152 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002153 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002154 int ri, wi;
2155 size_t textlen;
2156
2157 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002158 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002159
2160 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2161 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002162 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002163 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002164
Bram Moolenaar196d1572019-01-02 23:47:18 +01002165 wi = 0; // write index
2166 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002167 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002168 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002169 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002170
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002171 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2172 res = adjust_prop(&prop, col, bytes_added, flags);
2173 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002174 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002175 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002176 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2177 && u_savesub(lnum) == FAIL)
2178 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002179 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002180
2181 // u_savesub() may have updated curbuf->b_ml, fetch it again
2182 if (curbuf->b_ml.ml_line_lnum != lnum)
2183 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002184 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002185 if (res.can_drop)
2186 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002187 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002188 ++wi;
2189 }
2190 if (dirty)
2191 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002192 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2193
2194 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002195 {
2196 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2197
2198 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2199 vim_free(curbuf->b_ml.ml_line_ptr);
2200 curbuf->b_ml.ml_line_ptr = p;
2201 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002202 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002203 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002204 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002205 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002206}
2207
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002208/*
2209 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002210 * "lnum_props" is the line that has the properties from before the split.
2211 * "lnum_top" is the top line.
2212 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002213 * "deleted" is the number of bytes deleted.
2214 */
2215 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002216adjust_props_for_split(
2217 linenr_T lnum_props,
2218 linenr_T lnum_top,
2219 int kept,
2220 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002221{
2222 char_u *props;
2223 int count;
2224 garray_T prevprop;
2225 garray_T nextprop;
2226 int i;
2227 int skipped = kept + deleted;
2228
2229 if (!curbuf->b_has_textprop)
2230 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002231
2232 // Get the text properties from "lnum_props".
2233 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002234 ga_init2(&prevprop, sizeof(textprop_T), 10);
2235 ga_init2(&nextprop, sizeof(textprop_T), 10);
2236
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002237 // Keep the relevant ones in the first line, reducing the length if needed.
2238 // Copy the ones that include the split to the second line.
2239 // Move the ones after the split to the second line.
2240 for (i = 0; i < count; ++i)
2241 {
2242 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002243 proptype_T *pt;
2244 int start_incl, end_incl;
2245 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002246
2247 // copy the prop to an aligned structure
2248 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2249
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002250 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2251 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2252 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002253 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2254 cont_next = prop.tp_col != MAXCOL
2255 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002256 // when a prop has text it is never copied
2257 if (prop.tp_id < 0 && cont_next)
2258 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002259
2260 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002261 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002262 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2263
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002264 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002265 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002266 if (p->tp_col + p->tp_len >= kept)
2267 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002268 if (cont_next)
2269 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002270 }
2271
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002272 // Only add the property to the next line if the length is bigger than
2273 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002274 if ((cont_next || prop.tp_col == MAXCOL)
2275 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002276 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002277 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002278
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002279 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002280 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002281 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002282 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002283 if (p->tp_col > skipped)
2284 p->tp_col -= skipped - 1;
2285 else
2286 {
2287 p->tp_len -= skipped - p->tp_col;
2288 p->tp_col = 1;
2289 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002290 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002291 if (cont_prev)
2292 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002293 }
2294 }
2295
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002296 set_text_props(lnum_top, prevprop.ga_data,
2297 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002298 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002299 set_text_props(lnum_top + 1, nextprop.ga_data,
2300 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002301 ga_clear(&nextprop);
2302}
2303
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002304/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002305 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002306 */
2307 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002308prepend_joined_props(
2309 char_u *new_props,
2310 int propcount,
2311 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002312 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002313 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002314 long col,
2315 int removed)
2316{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002317 char_u *props;
2318 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2319 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002320
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002321 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002322 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002323 textprop_T prop;
2324 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002325
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002326 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002327 if (prop.tp_col == MAXCOL && !last_line)
2328 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002329 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2330
2331 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002332 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002333
Bram Moolenaare175dc62022-08-01 22:18:50 +01002334 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002335 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2336 &prop, sizeof(prop));
2337 else
2338 {
2339 int j;
2340 int found = FALSE;
2341
2342 // Search for continuing prop.
2343 for (j = *props_remaining; j < propcount; ++j)
2344 {
2345 textprop_T op;
2346
2347 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2348 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2349 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002350 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002351 found = TRUE;
2352 op.tp_len += op.tp_col - prop.tp_col;
2353 op.tp_col = prop.tp_col;
2354 // Start/end is taken care of when deleting joined lines
2355 op.tp_flags = prop.tp_flags;
2356 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2357 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002358 }
2359 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002360 if (!found)
2361 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002362 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002363 }
2364}
2365
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002366#endif // FEAT_PROP_POPUP