blob: ff96833f6c000a7fd673b4d9724f076c30cef76a [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010017
18/*
19 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
20 * This avoids adding a pointer to the hashtable item.
21 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
22 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
23 * HI2PT() converts a hashitem pointer to a proptype pointer.
24 */
25#define PT2HIKEY(p) ((p)->pt_name)
26#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
27#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
28
29// The global text property types.
30static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010031static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33// The last used text property type ID.
34static int proptype_id = 0;
35
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036/*
37 * Find a property type by name, return the hashitem.
38 * Returns NULL if the item can't be found.
39 */
40 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010041find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010042{
43 hashtab_T *ht;
44 hashitem_T *hi;
45
46 if (*name == NUL)
47 return NULL;
48 if (buf == NULL)
49 ht = global_proptypes;
50 else
51 ht = buf->b_proptypes;
52
53 if (ht == NULL)
54 return NULL;
55 hi = hash_find(ht, name);
56 if (HASHITEM_EMPTY(hi))
57 return NULL;
58 return hi;
59}
60
61/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010062 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010063 */
64 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010065find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010066{
Bram Moolenaare44336b2022-08-07 18:20:08 +010067 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010068
69 if (hi == NULL)
70 return NULL;
71 return HI2PT(hi);
72}
73
74/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020075 * Get the prop type ID of "name".
76 * When not found return zero.
77 */
78 int
79find_prop_type_id(char_u *name, buf_T *buf)
80{
Bram Moolenaare44336b2022-08-07 18:20:08 +010081 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020082
83 if (pt == NULL)
84 return 0;
85 return pt->pt_id;
86}
87
88/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089 * Lookup a property type by name. First in "buf" and when not found in the
90 * global types.
91 * When not found gives an error message and returns NULL.
92 */
93 static proptype_T *
94lookup_prop_type(char_u *name, buf_T *buf)
95{
Bram Moolenaare44336b2022-08-07 18:20:08 +010096 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097
98 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +010099 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100101 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100152 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153 return;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200154
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100155 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
156 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157}
158
159/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200160 * Attach a text property 'type_name' to the text starting
161 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100162 * the buffer "buf" and assign identifier "id".
163 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200164 */
165 static int
166prop_add_one(
167 buf_T *buf,
168 char_u *type_name,
169 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100170 char_u *text_arg,
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100171 int text_padding_left,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100172 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200173 linenr_T start_lnum,
174 linenr_T end_lnum,
175 colnr_T start_col,
176 colnr_T end_col)
177{
178 proptype_T *type;
179 linenr_T lnum;
180 int proplen;
181 char_u *props = NULL;
182 char_u *newprops;
183 size_t textlen;
184 char_u *newtext;
185 int i;
186 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text = text_arg;
188 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189
190 type = lookup_prop_type(type_name, buf);
191 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200193
194 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
195 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000196 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200198 }
199 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
200 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000201 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200203 }
204
205 if (buf->b_ml.ml_mfp == NULL)
206 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000207 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
209 }
210
211 if (text != NULL)
212 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100213 garray_T *gap = &buf->b_textprop_text;
214 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100215
216 // double check we got the right ID
217 if (-id - 1 != gap->ga_len)
218 iemsg("text prop ID mismatch");
219 if (gap->ga_growsize == 0)
220 ga_init2(gap, sizeof(char *), 50);
221 if (ga_grow(gap, 1) == FAIL)
222 goto theend;
223 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100224
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100225 // change any control character (Tab, Newline, etc.) to a Space to make
226 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100227 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100228 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100230 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200231 }
232
233 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
234 {
235 colnr_T col; // start column
236 long length; // in bytes
237
238 // Fetch the line to get the ml_line_len field updated.
239 proplen = get_text_props(buf, lnum, &props, TRUE);
240 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
241
242 if (lnum == start_lnum)
243 col = start_col;
244 else
245 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100246 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200247 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000248 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100249 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200250 }
251
252 if (lnum == end_lnum)
253 length = end_col - col;
254 else
255 length = (int)textlen - col + 1;
256 if (length > (long)textlen)
257 length = (int)textlen; // can include the end-of-line
258 if (length < 0)
259 length = 0; // zero-width property
260
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100261 if (text_arg != NULL)
262 {
263 length = 1; // text is placed on one character
264 if (col == 0)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100265 {
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100266 col = MAXCOL; // after the line
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100267 length += text_padding_left;
268 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100269 }
270
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200271 // Allocate the new line with space for the new property.
272 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
273 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100274 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200275 // Copy the text, including terminating NUL.
276 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
277
278 // Find the index where to insert the new property.
279 // Since the text properties are not aligned properly when stored with
280 // the text, we need to copy them as bytes before using it as a struct.
281 for (i = 0; i < proplen; ++i)
282 {
283 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
284 sizeof(textprop_T));
285 if (tmp_prop.tp_col >= col)
286 break;
287 }
288 newprops = newtext + textlen;
289 if (i > 0)
290 mch_memmove(newprops, props, sizeof(textprop_T) * i);
291
292 tmp_prop.tp_col = col;
293 tmp_prop.tp_len = length;
294 tmp_prop.tp_id = id;
295 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100296 tmp_prop.tp_flags = text_flags
297 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100298 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
299 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
300 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200301 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
302 sizeof(textprop_T));
303
304 if (i < proplen)
305 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
306 props + i * sizeof(textprop_T),
307 sizeof(textprop_T) * (proplen - i));
308
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100309 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200310 vim_free(buf->b_ml.ml_line_ptr);
311 buf->b_ml.ml_line_ptr = newtext;
312 buf->b_ml.ml_line_len += sizeof(textprop_T);
313 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
314 }
315
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100316 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200317 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100318 res = OK;
319
320theend:
321 vim_free(text);
322 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200323}
324
325/*
326 * prop_add_list()
327 * First argument specifies the text property:
328 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
329 * Second argument is a List where each item is a List with the following
330 * entries: [lnum, start_col, end_col]
331 */
332 void
333f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
334{
335 dict_T *dict;
336 char_u *type_name;
337 buf_T *buf = curbuf;
338 int id = 0;
339 listitem_T *li;
340 list_T *pos_list;
341 linenr_T start_lnum;
342 colnr_T start_col;
343 linenr_T end_lnum;
344 colnr_T end_col;
345 int error = FALSE;
346
347 if (check_for_dict_arg(argvars, 0) == FAIL
348 || check_for_list_arg(argvars, 1) == FAIL)
349 return;
350
Bram Moolenaard83392a2022-09-01 12:22:46 +0100351 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200352 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200353
354 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100355 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200356 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000357 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200358 return;
359 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100360 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200361
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100362 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100363 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364
365 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
366 return;
367
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100368 // This must be done _before_ we start adding properties because property
369 // changes trigger buffer (memline) reorganisation, which needs this flag
370 // to be correctly set.
371 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200372 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
373 {
374 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
375 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000376 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200377 return;
378 }
379
380 pos_list = li->li_tv.vval.v_list;
381 start_lnum = list_find_nr(pos_list, 0L, &error);
382 start_col = list_find_nr(pos_list, 1L, &error);
383 end_lnum = list_find_nr(pos_list, 2L, &error);
384 end_col = list_find_nr(pos_list, 3L, &error);
385 if (error || start_lnum <= 0 || start_col <= 0
386 || end_lnum <= 0 || end_col <= 0)
387 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000388 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200389 return;
390 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100391 if (prop_add_one(buf, type_name, id, NULL, 0, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200392 start_col, end_col) == FAIL)
393 return;
394 }
395
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100396 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200397}
398
399/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100400 * Get the next ID to use for a textprop with text in buffer "buf".
401 */
402 static int
403get_textprop_id(buf_T *buf)
404{
405 // TODO: recycle deleted entries
406 return -(buf->b_textprop_text.ga_len + 1);
407}
408
409/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200410 * Shared between prop_add() and popup_create().
411 * "dict_arg" is the function argument of a dict containing "bufnr".
412 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100413 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200414 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100415 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200416prop_add_common(
417 linenr_T start_lnum,
418 colnr_T start_col,
419 dict_T *dict,
420 buf_T *default_buf,
421 typval_T *dict_arg)
422{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200423 linenr_T end_lnum;
424 colnr_T end_col;
425 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426 buf_T *buf = default_buf;
427 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100428 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100429 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100430 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100431
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100432 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100433 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000434 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100435 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100436 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100437 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100438
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100439 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100440 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100441 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100442 if (end_lnum < start_lnum)
443 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000444 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100445 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100446 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100447 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100448 else
449 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100450
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100451 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100452 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100453 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100454
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100455 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100456 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000457 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100458 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100459 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100460 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100461 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100462 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100463 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100464 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100465 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000467 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100468 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469 }
470 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 else if (start_lnum == end_lnum)
472 end_col = start_col;
473 else
474 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100475
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100476 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100477 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100478
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100479 if (dict_has_key(dict, "text"))
480 {
481 text = dict_get_string(dict, "text", TRUE);
482 if (text == NULL)
483 goto theend;
484 // use a default length of 1 to make multiple props show up
485 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100486
487 if (dict_has_key(dict, "text_align"))
488 {
489 char_u *p = dict_get_string(dict, "text_align", FALSE);
490
491 if (p == NULL)
492 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100493 if (start_col != 0)
494 {
495 emsg(_(e_can_only_use_text_align_when_column_is_zero));
496 goto theend;
497 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100498 if (STRCMP(p, "right") == 0)
499 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100500 else if (STRCMP(p, "above") == 0)
501 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100502 else if (STRCMP(p, "below") == 0)
503 flags |= TP_FLAG_ALIGN_BELOW;
504 else if (STRCMP(p, "after") != 0)
505 {
506 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
507 goto theend;
508 }
509 }
510
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100511 if (dict_has_key(dict, "text_padding_left"))
512 {
513 text_padding_left = dict_get_number(dict, "text_padding_left");
514 if (text_padding_left < 0)
515 {
516 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
517 goto theend;
518 }
519 }
520
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100521 if (dict_has_key(dict, "text_wrap"))
522 {
523 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100524
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100525 if (p == NULL)
526 goto theend;
527 if (STRCMP(p, "wrap") == 0)
528 flags |= TP_FLAG_WRAP;
529 else if (STRCMP(p, "truncate") != 0)
530 {
531 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
532 goto theend;
533 }
534 }
535 }
536
537 // Column must be 1 or more for a normal text property; when "text" is
538 // present zero means it goes after the line.
539 if (start_col < (text == NULL ? 1 : 0))
540 {
541 semsg(_(e_invalid_column_number_nr), (long)start_col);
542 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100543 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100544 if (start_col > 0 && text_padding_left > 0)
545 {
546 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
547 goto theend;
548 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100549
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200550 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100551 goto theend;
552
553 if (id < 0 && buf->b_textprop_text.ga_len > 0)
554 {
555 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
556 goto theend;
557 }
558 if (text != NULL)
559 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100560
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100561 // This must be done _before_ we add the property because property changes
562 // trigger buffer (memline) reorganisation, which needs this flag to be
563 // correctly set.
564 buf->b_has_textprop = TRUE; // this is never reset
565
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100566 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100567 start_lnum, end_lnum, start_col, end_col);
568 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100569
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100570 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100571
572theend:
573 vim_free(text);
574 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100575}
576
577/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100578 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100579 * Returns the number of text properties and, when non-zero, a pointer to the
580 * first one in "props" (note that it is not aligned, therefore the char_u
581 * pointer).
582 */
583 int
584get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
585{
586 char_u *text;
587 size_t textlen;
588 size_t proplen;
589
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100590 // Be quick when no text property types have been defined or the buffer,
591 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200592 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100593 return 0;
594
595 // Fetch the line to get the ml_line_len field updated.
596 text = ml_get_buf(buf, lnum, will_change);
597 textlen = STRLEN(text) + 1;
598 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100599 if (proplen == 0)
600 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100601 if (proplen % sizeof(textprop_T) != 0)
602 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000603 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100604 return 0;
605 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100606 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100607 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100608}
609
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100610/*
611 * Return the number of text properties with "below" alignment in line "lnum".
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100612 * A "right" aligned property also goes below after a "below" or other "right"
613 * aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100614 */
615 int
616prop_count_below(buf_T *buf, linenr_T lnum)
617{
618 char_u *props;
619 int count = get_text_props(buf, lnum, &props, FALSE);
620 int result = 0;
621 textprop_T prop;
622 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100623 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100624
625 if (count == 0)
626 return 0;
627 for (i = 0; i < count; ++i)
628 {
629 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100630 if (prop.tp_col == MAXCOL)
631 {
632 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
633 || (next_right_goes_below
634 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
635 {
636 next_right_goes_below = TRUE;
637 ++result;
638 }
639 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
640 next_right_goes_below = TRUE;
641 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100642 }
643 return result;
644}
645
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200646/**
647 * Return the number of text properties on line "lnum" in the current buffer.
648 * When "only_starting" is true only text properties starting in this line will
649 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100650 * When "last_line" is FALSE then text properties after the line are not
651 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200652 */
653 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100654count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200655{
656 char_u *props;
657 int proplen = get_text_props(curbuf, lnum, &props, 0);
658 int result = proplen;
659 int i;
660 textprop_T prop;
661
Bram Moolenaare175dc62022-08-01 22:18:50 +0100662 for (i = 0; i < proplen; ++i)
663 {
664 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100665 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100666 // previous line, or when not in the last line and it is virtual text
667 // after the line.
668 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
669 || (!last_line && prop.tp_col == MAXCOL))
670 --result;
671 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200672 return result;
673}
674
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100675static textprop_T *text_prop_compare_props;
676static buf_T *text_prop_compare_buf;
677
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100678/* Score for sorting on position of the text property: 0: above,
679 * 1: after (default), 2: right, 3: below (comes last)
680 */
681 static int
682text_prop_order(int flags)
683{
684 if (flags & TP_FLAG_ALIGN_ABOVE)
685 return 0;
686 if (flags & TP_FLAG_ALIGN_RIGHT)
687 return 2;
688 if (flags & TP_FLAG_ALIGN_BELOW)
689 return 3;
690 return 1;
691}
692
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100693/*
694 * Function passed to qsort() to sort text properties.
695 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
696 * both have the same priority.
697 */
698 static int
699text_prop_compare(const void *s1, const void *s2)
700{
701 int idx1, idx2;
702 textprop_T *tp1, *tp2;
703 proptype_T *pt1, *pt2;
704 colnr_T col1, col2;
705
706 idx1 = *(int *)s1;
707 idx2 = *(int *)s2;
708 tp1 = &text_prop_compare_props[idx1];
709 tp2 = &text_prop_compare_props[idx2];
710 col1 = tp1->tp_col;
711 col2 = tp2->tp_col;
712 if (col1 == MAXCOL && col2 == MAXCOL)
713 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100714 int order1 = text_prop_order(tp1->tp_flags);
715 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100716
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100717 // both props add text before or after the line, sort on order where it
718 // is added
719 if (order1 != order2)
720 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100721 }
722
723 // property that inserts text has priority over one that doesn't
724 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
725 return tp1->tp_id < 0 ? 1 : -1;
726
727 // check highest priority, defined by the type
728 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
729 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
730 if (pt1 != pt2)
731 {
732 if (pt1 == NULL)
733 return -1;
734 if (pt2 == NULL)
735 return 1;
736 if (pt1->pt_priority != pt2->pt_priority)
737 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
738 }
739
740 // same priority, one that starts first wins
741 if (col1 != col2)
742 return col1 < col2 ? 1 : -1;
743
744 // for a property with text the id can be used as tie breaker
745 if (tp1->tp_id < 0)
746 return tp1->tp_id > tp2->tp_id ? 1 : -1;
747
748 return 0;
749}
750
751/*
752 * Sort "count" text properties using an array if indexes "idxs" into the list
753 * of text props "props" for buffer "buf".
754 */
755 void
756sort_text_props(
757 buf_T *buf,
758 textprop_T *props,
759 int *idxs,
760 int count)
761{
762 text_prop_compare_buf = buf;
763 text_prop_compare_props = props;
764 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
765}
766
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100767/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200768 * Find text property "type_id" in the visible lines of window "wp".
769 * Match "id" when it is > 0.
770 * Returns FAIL when not found.
771 */
772 int
773find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
774 linenr_T *found_lnum)
775{
776 linenr_T lnum;
777 char_u *props;
778 int count;
779 int i;
780
781 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100782 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200783 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
784 {
785 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
786 for (i = 0; i < count; ++i)
787 {
788 mch_memmove(prop, props + i * sizeof(textprop_T),
789 sizeof(textprop_T));
790 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
791 {
792 *found_lnum = lnum;
793 return OK;
794 }
795 }
796 }
797 return FAIL;
798}
799
800/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100801 * Set the text properties for line "lnum" to "props" with length "len".
802 * If "len" is zero text properties are removed, "props" is not used.
803 * Any existing text properties are dropped.
804 * Only works for the current buffer.
805 */
806 static void
807set_text_props(linenr_T lnum, char_u *props, int len)
808{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100809 char_u *text;
810 char_u *newtext;
811 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100812
813 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100814 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100815 newtext = alloc(textlen + len);
816 if (newtext == NULL)
817 return;
818 mch_memmove(newtext, text, textlen);
819 if (len > 0)
820 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100821 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100822 vim_free(curbuf->b_ml.ml_line_ptr);
823 curbuf->b_ml.ml_line_ptr = newtext;
824 curbuf->b_ml.ml_line_len = textlen + len;
825 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
826}
827
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100828/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100829 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100830 */
831 void
832add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
833{
834 char_u *text;
835 char_u *newtext;
836 int proplen = text_prop_count * (int)sizeof(textprop_T);
837
838 text = ml_get(lnum);
839 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
840 if (newtext == NULL)
841 return;
842 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
843 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
844 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
845 vim_free(curbuf->b_ml.ml_line_ptr);
846 curbuf->b_ml.ml_line_ptr = newtext;
847 curbuf->b_ml.ml_line_len += proplen;
848 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
849}
850
Bram Moolenaare44336b2022-08-07 18:20:08 +0100851/*
852 * Function passed to qsort() for sorting proptype_T on pt_id.
853 */
854 static int
855compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100856{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100857 proptype_T *tp1 = *(proptype_T **)s1;
858 proptype_T *tp2 = *(proptype_T **)s2;
859
860 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
861}
862
863 static proptype_T *
864find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
865{
866 int low = 0;
867 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100868
Bram Moolenaar10246902022-08-08 17:08:05 +0100869 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100870 return NULL;
871
Bram Moolenaare44336b2022-08-07 18:20:08 +0100872 // Make the loopup faster by creating an array with pointers to
873 // hashtable entries, sorted on pt_id.
874 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100875 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100876 long todo;
877 hashitem_T *hi;
878 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100879
Bram Moolenaare44336b2022-08-07 18:20:08 +0100880 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
881 if (*array == NULL)
882 return NULL;
883 todo = (long)ht->ht_used;
884 for (hi = ht->ht_array; todo > 0; ++hi)
885 {
886 if (!HASHITEM_EMPTY(hi))
887 {
888 (*array)[i++] = HI2PT(hi);
889 --todo;
890 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100891 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100892 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
893 }
894
895 // binary search in the sorted array
896 high = ht->ht_used;
897 while (high > low)
898 {
899 int m = (high + low) / 2;
900
901 if ((*array)[m]->pt_id == id)
902 return (*array)[m];
903 if ((*array)[m]->pt_id > id)
904 high = m;
905 else
906 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100907 }
908 return NULL;
909}
910
911/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100912 * Fill 'dict' with text properties in 'prop'.
913 */
914 static void
915prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
916{
917 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200918 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100919
920 dict_add_number(dict, "col", prop->tp_col);
921 dict_add_number(dict, "length", prop->tp_len);
922 dict_add_number(dict, "id", prop->tp_id);
923 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
924 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200925
Bram Moolenaare44336b2022-08-07 18:20:08 +0100926 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200927 if (pt == NULL)
928 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100929 pt = find_type_by_id(global_proptypes, &global_proparray,
930 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200931 buflocal = FALSE;
932 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100933 if (pt != NULL)
934 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200935
936 if (buflocal)
937 dict_add_number(dict, "type_bufnr", buf->b_fnum);
938 else
939 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100940}
941
942/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100943 * Find a property type by ID in "buf" or globally.
944 * Returns NULL if not found.
945 */
946 proptype_T *
947text_prop_type_by_id(buf_T *buf, int id)
948{
949 proptype_T *type;
950
Bram Moolenaare44336b2022-08-07 18:20:08 +0100951 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100952 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100953 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100954 return type;
955}
956
957/*
958 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
959 */
960 void
961f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
962{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200963 linenr_T start;
964 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100965 linenr_T lnum;
966 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100967 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100968
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200969 if (in_vim9script()
970 && (check_for_number_arg(argvars, 0) == FAIL
971 || check_for_opt_number_arg(argvars, 1) == FAIL
972 || (argvars[1].v_type != VAR_UNKNOWN
973 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
974 return;
975
976 start = tv_get_number(&argvars[0]);
977 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100978 if (argvars[1].v_type != VAR_UNKNOWN)
979 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100980 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100981 if (argvars[2].v_type != VAR_UNKNOWN)
982 {
983 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
984 return;
985 }
986 }
987 if (start < 1 || end < 1)
988 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200989 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100990 return;
991 }
992
993 for (lnum = start; lnum <= end; ++lnum)
994 {
995 char_u *text;
996 size_t len;
997
998 if (lnum > buf->b_ml.ml_line_count)
999 break;
1000 text = ml_get_buf(buf, lnum, FALSE);
1001 len = STRLEN(text) + 1;
1002 if ((size_t)buf->b_ml.ml_line_len > len)
1003 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001004 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001005 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1006 {
1007 char_u *newtext = vim_strsave(text);
1008
1009 // need to allocate the line now
1010 if (newtext == NULL)
1011 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001012 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1013 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001014 buf->b_ml.ml_line_ptr = newtext;
1015 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1016 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001017 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001018 }
1019 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001020 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001021 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001022}
1023
1024/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001025 * prop_find({props} [, {direction}])
1026 */
1027 void
1028f_prop_find(typval_T *argvars, typval_T *rettv)
1029{
1030 pos_T *cursor = &curwin->w_cursor;
1031 dict_T *dict;
1032 buf_T *buf = curbuf;
1033 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001034 int lnum_start;
1035 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001036 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001037 int id = 0;
1038 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001039 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001040 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001041 int lnum = -1;
1042 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001043 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001044 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001045
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001046 if (in_vim9script()
1047 && (check_for_dict_arg(argvars, 0) == FAIL
1048 || check_for_opt_string_arg(argvars, 1) == FAIL))
1049 return;
1050
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001051 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001052 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001053 dict = argvars[0].vval.v_dict;
1054
1055 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1056 return;
1057 if (buf->b_ml.ml_mfp == NULL)
1058 return;
1059
1060 if (argvars[1].v_type != VAR_UNKNOWN)
1061 {
1062 char_u *dir_s = tv_get_string(&argvars[1]);
1063
1064 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001065 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001066 else if (*dir_s != 'f')
1067 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001068 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001069 return;
1070 }
1071 }
1072
1073 di = dict_find(dict, (char_u *)"lnum", -1);
1074 if (di != NULL)
1075 lnum = tv_get_number(&di->di_tv);
1076
1077 di = dict_find(dict, (char_u *)"col", -1);
1078 if (di != NULL)
1079 col = tv_get_number(&di->di_tv);
1080
1081 if (lnum == -1)
1082 {
1083 lnum = cursor->lnum;
1084 col = cursor->col + 1;
1085 }
1086 else if (col == -1)
1087 col = 1;
1088
1089 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1090 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001091 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001092 return;
1093 }
1094
Bram Moolenaard61efa52022-07-23 09:52:04 +01001095 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001096
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001097 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001098 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001099 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001100 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001101 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001102 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001103 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001104 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001105 proptype_T *type = lookup_prop_type(name, buf);
1106
1107 if (type == NULL)
1108 return;
1109 type_id = type->pt_id;
1110 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001111 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001112 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001113 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001114 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001115 return;
1116 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001117 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001118 {
Ben Jacksona7704222022-08-20 20:54:51 +01001119 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001120 return;
1121 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001122
1123 lnum_start = lnum;
1124
1125 if (rettv_dict_alloc(rettv) == FAIL)
1126 return;
1127
1128 while (1)
1129 {
1130 char_u *text = ml_get_buf(buf, lnum, FALSE);
1131 size_t textlen = STRLEN(text) + 1;
1132 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001133 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001134 int i;
1135 textprop_T prop;
1136 int prop_start;
1137 int prop_end;
1138
LemonBoy9bd3ce22022-04-18 21:54:02 +01001139 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001140 {
1141 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001142 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001143
LemonBoy9bd3ce22022-04-18 21:54:02 +01001144 // For the very first line try to find the first property before or
1145 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001146 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001147 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001148 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001149 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001150 if (prop.tp_col > col)
1151 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001152 }
1153 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1154 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001155 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001156 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001157 : (id_found && prop.tp_id == id)
1158 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001159 {
1160 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001161 if (lnum_start == lnum
1162 && col >= prop.tp_col
1163 && (col <= prop.tp_col + prop.tp_len
1164 - (prop.tp_len != 0)))
1165 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001166
LemonBoy9bd3ce22022-04-18 21:54:02 +01001167 // The property was not continued from last line, it starts on
1168 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001169 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001170 // The property does not continue on the next line, it ends on
1171 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001172 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001173 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001174 seen_end = 1;
1175
1176 // Skip lines without the start flag.
1177 if (!prop_start)
1178 {
1179 // Always search backwards for start when search started
1180 // on a prop and we're not skipping.
1181 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001182 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001183 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001184 }
1185
1186 // If skipstart is true, skip the prop at start pos (even if
1187 // continued from another line).
1188 if (start_pos_has_prop && skipstart && !seen_end)
1189 {
1190 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001191 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001192 }
1193
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001194 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1195 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1196
1197 return;
1198 }
1199 }
1200
1201 if (dir > 0)
1202 {
1203 if (lnum >= buf->b_ml.ml_line_count)
1204 break;
1205 lnum++;
1206 }
1207 else
1208 {
1209 if (lnum <= 1)
1210 break;
1211 lnum--;
1212 }
1213 }
1214}
1215
1216/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001217 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1218 */
1219 static int
1220prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1221{
1222 int i;
1223
1224 for (i = 0; i < len; i++)
1225 if (types_or_ids[i] == type_or_id)
1226 return TRUE;
1227
1228 return FALSE;
1229}
1230
1231/*
1232 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1233 * If 'prop_types' is not NULL, then return only the text properties with
1234 * matching property type in the 'prop_types' array.
1235 * If 'prop_ids' is not NULL, then return only the text properties with
1236 * an identifier in the 'props_ids' array.
1237 * If 'add_lnum' is TRUE, then add the line number also to the text property
1238 * dictionary.
1239 */
1240 static void
1241get_props_in_line(
1242 buf_T *buf,
1243 linenr_T lnum,
1244 int *prop_types,
1245 int prop_types_len,
1246 int *prop_ids,
1247 int prop_ids_len,
1248 list_T *retlist,
1249 int add_lnum)
1250{
1251 char_u *text = ml_get_buf(buf, lnum, FALSE);
1252 size_t textlen = STRLEN(text) + 1;
1253 int count;
1254 int i;
1255 textprop_T prop;
1256
1257 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1258 for (i = 0; i < count; ++i)
1259 {
1260 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1261 sizeof(textprop_T));
1262 if ((prop_types == NULL
1263 || prop_type_or_id_in_list(prop_types, prop_types_len,
1264 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001265 && (prop_ids == NULL
1266 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1267 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001268 {
1269 dict_T *d = dict_alloc();
1270
1271 if (d == NULL)
1272 break;
1273 prop_fill_dict(d, &prop, buf);
1274 if (add_lnum)
1275 dict_add_number(d, "lnum", lnum);
1276 list_append_dict(retlist, d);
1277 }
1278 }
1279}
1280
1281/*
1282 * Convert a List of property type names into an array of property type
1283 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1284 * error. 'num_types' is set to the number of returned property types.
1285 */
1286 static int *
1287get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1288{
1289 int *prop_types;
1290 listitem_T *li;
1291 int i;
1292 char_u *name;
1293 proptype_T *type;
1294
1295 *num_types = 0;
1296
1297 prop_types = ALLOC_MULT(int, list_len(l));
1298 if (prop_types == NULL)
1299 return NULL;
1300
1301 i = 0;
1302 FOR_ALL_LIST_ITEMS(l, li)
1303 {
1304 if (li->li_tv.v_type != VAR_STRING)
1305 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001306 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001307 goto errret;
1308 }
1309 name = li->li_tv.vval.v_string;
1310 if (name == NULL)
1311 goto errret;
1312
1313 type = lookup_prop_type(name, buf);
1314 if (type == NULL)
1315 goto errret;
1316 prop_types[i++] = type->pt_id;
1317 }
1318
1319 *num_types = i;
1320 return prop_types;
1321
1322errret:
1323 VIM_CLEAR(prop_types);
1324 return NULL;
1325}
1326
1327/*
1328 * Convert a List of property identifiers into an array of property
1329 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1330 * error. 'num_ids' is set to the number of returned property identifiers.
1331 */
1332 static int *
1333get_prop_ids_from_list(list_T *l, int *num_ids)
1334{
1335 int *prop_ids;
1336 listitem_T *li;
1337 int i;
1338 int id;
1339 int error;
1340
1341 *num_ids = 0;
1342
1343 prop_ids = ALLOC_MULT(int, list_len(l));
1344 if (prop_ids == NULL)
1345 return NULL;
1346
1347 i = 0;
1348 FOR_ALL_LIST_ITEMS(l, li)
1349 {
1350 error = FALSE;
1351 id = tv_get_number_chk(&li->li_tv, &error);
1352 if (error)
1353 goto errret;
1354
1355 prop_ids[i++] = id;
1356 }
1357
1358 *num_ids = i;
1359 return prop_ids;
1360
1361errret:
1362 VIM_CLEAR(prop_ids);
1363 return NULL;
1364}
1365
1366/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001367 * prop_list({lnum} [, {bufnr}])
1368 */
1369 void
1370f_prop_list(typval_T *argvars, typval_T *rettv)
1371{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001372 linenr_T lnum;
1373 linenr_T start_lnum;
1374 linenr_T end_lnum;
1375 buf_T *buf = curbuf;
1376 int add_lnum = FALSE;
1377 int *prop_types = NULL;
1378 int prop_types_len = 0;
1379 int *prop_ids = NULL;
1380 int prop_ids_len = 0;
1381 list_T *l;
1382 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001383
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001384 if (in_vim9script()
1385 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001386 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001387 return;
1388
Bram Moolenaar93a10962022-06-16 11:42:09 +01001389 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001390 return;
1391
1392 // default: get text properties on current line
1393 start_lnum = tv_get_number(&argvars[0]);
1394 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001395 if (argvars[1].v_type != VAR_UNKNOWN)
1396 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001397 dict_T *d;
1398
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001399 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001400 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001401 d = argvars[1].vval.v_dict;
1402
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001403 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1404 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001405
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001406 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001407 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001408 if (di->di_tv.v_type != VAR_NUMBER)
1409 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001410 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001411 return;
1412 }
1413 end_lnum = tv_get_number(&di->di_tv);
1414 if (end_lnum < 0)
1415 // negative end_lnum is used as an offset from the last buffer
1416 // line
1417 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1418 else if (end_lnum > buf->b_ml.ml_line_count)
1419 end_lnum = buf->b_ml.ml_line_count;
1420 add_lnum = TRUE;
1421 }
1422 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1423 {
1424 if (di->di_tv.v_type != VAR_LIST)
1425 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001426 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001427 return;
1428 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001429
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001430 l = di->di_tv.vval.v_list;
1431 if (l != NULL && list_len(l) > 0)
1432 {
1433 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1434 if (prop_types == NULL)
1435 return;
1436 }
1437 }
1438 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1439 {
1440 if (di->di_tv.v_type != VAR_LIST)
1441 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001442 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001443 goto errret;
1444 }
1445
1446 l = di->di_tv.vval.v_list;
1447 if (l != NULL && list_len(l) > 0)
1448 {
1449 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1450 if (prop_ids == NULL)
1451 goto errret;
1452 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001453 }
1454 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001455 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1456 || end_lnum < 1 || end_lnum < start_lnum)
1457 emsg(_(e_invalid_range));
1458 else
1459 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1460 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1461 prop_ids, prop_ids_len,
1462 rettv->vval.v_list, add_lnum);
1463
1464errret:
1465 VIM_CLEAR(prop_types);
1466 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001467}
1468
1469/*
1470 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1471 */
1472 void
1473f_prop_remove(typval_T *argvars, typval_T *rettv)
1474{
1475 linenr_T start = 1;
1476 linenr_T end = 0;
1477 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001478 linenr_T first_changed = 0;
1479 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001480 dict_T *dict;
1481 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001482 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001483 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001484 int type_id = -1; // for a single "type"
1485 int *type_ids = NULL; // array, for a list of "types", allocated
1486 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001487 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001488 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001489
1490 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001491
1492 if (in_vim9script()
1493 && (check_for_dict_arg(argvars, 0) == FAIL
1494 || check_for_opt_number_arg(argvars, 1) == FAIL
1495 || (argvars[1].v_type != VAR_UNKNOWN
1496 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1497 return;
1498
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001499 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001500 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001501
1502 if (argvars[1].v_type != VAR_UNKNOWN)
1503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001504 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001505 end = start;
1506 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001507 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001508 if (start < 1 || end < 1)
1509 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001510 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001511 return;
1512 }
1513 }
1514
1515 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001516 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1517 return;
1518 if (buf->b_ml.ml_mfp == NULL)
1519 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001520
Bram Moolenaard61efa52022-07-23 09:52:04 +01001521 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001522
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001523 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001524 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001525
1526 // if a specific type was supplied "type": check that (and ignore "types".
1527 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001528 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001529 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001530 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001531 proptype_T *type = lookup_prop_type(name, buf);
1532
1533 if (type == NULL)
1534 return;
1535 type_id = type->pt_id;
1536 }
Ben Jacksona7704222022-08-20 20:54:51 +01001537 if (dict_has_key(dict, "types"))
1538 {
1539 typval_T types;
1540 listitem_T *li = NULL;
1541
1542 dict_get_tv(dict, "types", &types);
1543 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1544 {
1545 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1546
1547 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1548 {
1549 proptype_T *prop_type;
1550
1551 if (li->li_tv.v_type != VAR_STRING)
1552 continue;
1553
1554 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1555
1556 if (!prop_type)
1557 goto cleanup_prop_remove;
1558
1559 type_ids[num_type_ids++] = prop_type->pt_id;
1560 }
1561 }
1562 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001563 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001564
Ben Jacksona7704222022-08-20 20:54:51 +01001565 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001566 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001567 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001568 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001569 }
Ben Jacksona7704222022-08-20 20:54:51 +01001570 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001571 {
Ben Jacksona7704222022-08-20 20:54:51 +01001572 emsg(_(e_need_id_and_type_or_types_with_both));
1573 goto cleanup_prop_remove;
1574 }
1575 if (type_id != -1 && num_type_ids > 0)
1576 {
1577 emsg(_(e_cannot_specify_both_type_and_types));
1578 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001579 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001580
1581 if (end == 0)
1582 end = buf->b_ml.ml_line_count;
1583 for (lnum = start; lnum <= end; ++lnum)
1584 {
1585 char_u *text;
1586 size_t len;
1587
1588 if (lnum > buf->b_ml.ml_line_count)
1589 break;
1590 text = ml_get_buf(buf, lnum, FALSE);
1591 len = STRLEN(text) + 1;
1592 if ((size_t)buf->b_ml.ml_line_len > len)
1593 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001594 static textprop_T textprop; // static because of alignment
1595 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001596
1597 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1598 / sizeof(textprop_T); ++idx)
1599 {
1600 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1601 + idx * sizeof(textprop_T);
1602 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001603 int matches_id = 0;
1604 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001605
1606 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001607
1608 matches_id = textprop.tp_id == id;
1609 if (num_type_ids > 0)
1610 {
1611 int idx2;
1612
1613 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1614 matches_type = textprop.tp_type == type_ids[idx2];
1615 }
1616 else
1617 {
1618 matches_type = textprop.tp_type == type_id;
1619 }
1620
1621 if (both ? matches_id && matches_type
1622 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001623 {
1624 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1625 {
1626 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1627
1628 // need to allocate the line to be able to change it
1629 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001630 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001631 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1632 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001633 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1634 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001635 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001636 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1637
1638 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001639 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001640 }
1641
1642 taillen = buf->b_ml.ml_line_len - len
1643 - (idx + 1) * sizeof(textprop_T);
1644 if (taillen > 0)
1645 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1646 taillen);
1647 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1648 --idx;
1649
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001650 if (textprop.tp_id < 0)
1651 {
1652 garray_T *gap = &buf->b_textprop_text;
1653 int ii = -textprop.tp_id - 1;
1654
1655 // negative ID: property with text - free the text
1656 if (ii < gap->ga_len)
1657 {
1658 char_u **p = ((char_u **)gap->ga_data) + ii;
1659 vim_free(*p);
1660 *p = NULL;
1661 did_remove_text = TRUE;
1662 }
1663 }
1664
Bram Moolenaar965c0442021-05-17 00:22:06 +02001665 if (first_changed == 0)
1666 first_changed = lnum;
1667 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001668 ++rettv->vval.v_number;
1669 if (!do_all)
1670 break;
1671 }
1672 }
1673 }
1674 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001675
Bram Moolenaar965c0442021-05-17 00:22:06 +02001676 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001677 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001678 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001679 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001680 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001681 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001682
1683 if (did_remove_text)
1684 {
1685 garray_T *gap = &buf->b_textprop_text;
1686
1687 // Reduce the growarray size for NULL pointers at the end.
1688 while (gap->ga_len > 0
1689 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1690 --gap->ga_len;
1691 }
Ben Jacksona7704222022-08-20 20:54:51 +01001692
1693cleanup_prop_remove:
1694 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001695}
1696
1697/*
1698 * Common for f_prop_type_add() and f_prop_type_change().
1699 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001700 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001701prop_type_set(typval_T *argvars, int add)
1702{
1703 char_u *name;
1704 buf_T *buf = NULL;
1705 dict_T *dict;
1706 dictitem_T *di;
1707 proptype_T *prop;
1708
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001709 if (in_vim9script()
1710 && (check_for_string_arg(argvars, 0) == FAIL
1711 || check_for_dict_arg(argvars, 1) == FAIL))
1712 return;
1713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001714 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001715 if (*name == NUL)
1716 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001717 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001718 return;
1719 }
1720
1721 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1722 return;
1723 dict = argvars[1].vval.v_dict;
1724
Bram Moolenaare44336b2022-08-07 18:20:08 +01001725 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001726 if (add)
1727 {
1728 hashtab_T **htp;
1729
1730 if (prop != NULL)
1731 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001732 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001733 return;
1734 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001735 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001736 if (prop == NULL)
1737 return;
1738 STRCPY(prop->pt_name, name);
1739 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001740 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001741 if (buf == NULL)
1742 {
1743 htp = &global_proptypes;
1744 VIM_CLEAR(global_proparray);
1745 }
1746 else
1747 {
1748 htp = &buf->b_proptypes;
1749 VIM_CLEAR(buf->b_proparray);
1750 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001751 if (*htp == NULL)
1752 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001753 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001754 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001755 {
1756 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001757 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001758 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001759 hash_init(*htp);
1760 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001761 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001762 }
1763 else
1764 {
1765 if (prop == NULL)
1766 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001767 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001768 return;
1769 }
1770 }
1771
1772 if (dict != NULL)
1773 {
1774 di = dict_find(dict, (char_u *)"highlight", -1);
1775 if (di != NULL)
1776 {
1777 char_u *highlight;
1778 int hl_id = 0;
1779
Bram Moolenaard61efa52022-07-23 09:52:04 +01001780 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001781 if (highlight != NULL && *highlight != NUL)
1782 hl_id = syn_name2id(highlight);
1783 if (hl_id <= 0)
1784 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001785 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001786 highlight == NULL ? (char_u *)"" : highlight);
1787 return;
1788 }
1789 prop->pt_hl_id = hl_id;
1790 }
1791
Bram Moolenaar58187f12019-05-05 16:33:47 +02001792 di = dict_find(dict, (char_u *)"combine", -1);
1793 if (di != NULL)
1794 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001795 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001796 prop->pt_flags |= PT_FLAG_COMBINE;
1797 else
1798 prop->pt_flags &= ~PT_FLAG_COMBINE;
1799 }
1800
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001801 di = dict_find(dict, (char_u *)"override", -1);
1802 if (di != NULL)
1803 {
1804 if (tv_get_bool(&di->di_tv))
1805 prop->pt_flags |= PT_FLAG_OVERRIDE;
1806 else
1807 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1808 }
1809
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001810 di = dict_find(dict, (char_u *)"priority", -1);
1811 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001812 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001813
1814 di = dict_find(dict, (char_u *)"start_incl", -1);
1815 if (di != NULL)
1816 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001817 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001818 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1819 else
1820 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1821 }
1822
1823 di = dict_find(dict, (char_u *)"end_incl", -1);
1824 if (di != NULL)
1825 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001826 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001827 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1828 else
1829 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1830 }
1831 }
1832}
1833
1834/*
1835 * prop_type_add({name}, {props})
1836 */
1837 void
1838f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1839{
1840 prop_type_set(argvars, TRUE);
1841}
1842
1843/*
1844 * prop_type_change({name}, {props})
1845 */
1846 void
1847f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1848{
1849 prop_type_set(argvars, FALSE);
1850}
1851
1852/*
1853 * prop_type_delete({name} [, {bufnr}])
1854 */
1855 void
1856f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1857{
1858 char_u *name;
1859 buf_T *buf = NULL;
1860 hashitem_T *hi;
1861
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001862 if (in_vim9script()
1863 && (check_for_string_arg(argvars, 0) == FAIL
1864 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1865 return;
1866
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001867 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001868 if (*name == NUL)
1869 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001870 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001871 return;
1872 }
1873
1874 if (argvars[1].v_type != VAR_UNKNOWN)
1875 {
1876 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1877 return;
1878 }
1879
Bram Moolenaare44336b2022-08-07 18:20:08 +01001880 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001881 if (hi != NULL)
1882 {
1883 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001884 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001885
1886 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001887 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001888 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001889 VIM_CLEAR(global_proparray);
1890 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001891 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001892 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001893 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001894 VIM_CLEAR(buf->b_proparray);
1895 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001896 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001897 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001898 }
1899}
1900
1901/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001902 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001903 */
1904 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001905f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001906{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001907 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001908
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001909 if (in_vim9script()
1910 && (check_for_string_arg(argvars, 0) == FAIL
1911 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1912 return;
1913
1914 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001915 if (*name == NUL)
1916 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001917 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001918 return;
1919 }
1920 if (rettv_dict_alloc(rettv) == OK)
1921 {
1922 proptype_T *prop = NULL;
1923 buf_T *buf = NULL;
1924
1925 if (argvars[1].v_type != VAR_UNKNOWN)
1926 {
1927 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1928 return;
1929 }
1930
Bram Moolenaare44336b2022-08-07 18:20:08 +01001931 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001932 if (prop != NULL)
1933 {
1934 dict_T *d = rettv->vval.v_dict;
1935
1936 if (prop->pt_hl_id > 0)
1937 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1938 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001939 dict_add_number(d, "combine",
1940 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001941 dict_add_number(d, "start_incl",
1942 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1943 dict_add_number(d, "end_incl",
1944 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1945 if (buf != NULL)
1946 dict_add_number(d, "bufnr", buf->b_fnum);
1947 }
1948 }
1949}
1950
1951 static void
1952list_types(hashtab_T *ht, list_T *l)
1953{
1954 long todo;
1955 hashitem_T *hi;
1956
1957 todo = (long)ht->ht_used;
1958 for (hi = ht->ht_array; todo > 0; ++hi)
1959 {
1960 if (!HASHITEM_EMPTY(hi))
1961 {
1962 proptype_T *prop = HI2PT(hi);
1963
1964 list_append_string(l, prop->pt_name, -1);
1965 --todo;
1966 }
1967 }
1968}
1969
1970/*
1971 * prop_type_list([{bufnr}])
1972 */
1973 void
1974f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1975{
1976 buf_T *buf = NULL;
1977
1978 if (rettv_list_alloc(rettv) == OK)
1979 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001980 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1981 return;
1982
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001983 if (argvars[0].v_type != VAR_UNKNOWN)
1984 {
1985 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1986 return;
1987 }
1988 if (buf == NULL)
1989 {
1990 if (global_proptypes != NULL)
1991 list_types(global_proptypes, rettv->vval.v_list);
1992 }
1993 else if (buf->b_proptypes != NULL)
1994 list_types(buf->b_proptypes, rettv->vval.v_list);
1995 }
1996}
1997
1998/*
1999 * Free all property types in "ht".
2000 */
2001 static void
2002clear_ht_prop_types(hashtab_T *ht)
2003{
2004 long todo;
2005 hashitem_T *hi;
2006
2007 if (ht == NULL)
2008 return;
2009
2010 todo = (long)ht->ht_used;
2011 for (hi = ht->ht_array; todo > 0; ++hi)
2012 {
2013 if (!HASHITEM_EMPTY(hi))
2014 {
2015 proptype_T *prop = HI2PT(hi);
2016
2017 vim_free(prop);
2018 --todo;
2019 }
2020 }
2021
2022 hash_clear(ht);
2023 vim_free(ht);
2024}
2025
2026#if defined(EXITFREE) || defined(PROTO)
2027/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002028 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002029 */
2030 void
2031clear_global_prop_types(void)
2032{
2033 clear_ht_prop_types(global_proptypes);
2034 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002035 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002036}
2037#endif
2038
2039/*
2040 * Free all property types for "buf".
2041 */
2042 void
2043clear_buf_prop_types(buf_T *buf)
2044{
2045 clear_ht_prop_types(buf->b_proptypes);
2046 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002047 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002048}
2049
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002050// Struct used to return two values from adjust_prop().
2051typedef struct
2052{
2053 int dirty; // if the property was changed
2054 int can_drop; // whether after this change, the prop may be removed
2055} adjustres_T;
2056
2057/*
2058 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2059 *
2060 * Note that "col" is zero-based, while tp_col is one-based.
2061 * Only for the current buffer.
2062 * "flags" can have:
2063 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002064 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002065 */
2066 static adjustres_T
2067adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002068 textprop_T *prop,
2069 colnr_T col,
2070 int added,
2071 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002072{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002073 proptype_T *pt;
2074 int start_incl;
2075 int end_incl;
2076 int droppable;
2077 adjustres_T res = {TRUE, FALSE};
2078
2079 // prop after end of the line doesn't move
2080 if (prop->tp_col == MAXCOL)
2081 {
2082 res.dirty = FALSE;
2083 return res;
2084 }
2085
2086 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2087 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002088 || (flags & APC_SUBSTITUTE)
2089 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002090 if (prop->tp_id < 0 && (flags & APC_INDENT))
2091 // when inserting indent just before a character with virtual text
2092 // shift the text property
2093 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002094 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002095 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002096 // do not drop zero-width props if they later can increase in size
2097 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002098
2099 if (added > 0)
2100 {
2101 if (col + 1 <= prop->tp_col
2102 - (start_incl || (prop->tp_len == 0 && end_incl)))
2103 // Change is entirely before the text property: Only shift
2104 prop->tp_col += added;
2105 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2106 // Insertion was inside text property
2107 prop->tp_len += added;
2108 }
2109 else if (prop->tp_col > col + 1)
2110 {
2111 if (prop->tp_col + added < col + 1)
2112 {
2113 prop->tp_len += (prop->tp_col - 1 - col) + added;
2114 prop->tp_col = col + 1;
2115 if (prop->tp_len <= 0)
2116 {
2117 prop->tp_len = 0;
2118 res.can_drop = droppable;
2119 }
2120 }
2121 else
2122 prop->tp_col += added;
2123 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002124 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2125 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002126 {
2127 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2128
2129 prop->tp_len += after > 0 ? added + after : added;
2130 res.can_drop = prop->tp_len <= 0 && droppable;
2131 }
2132 else
2133 res.dirty = FALSE;
2134
2135 return res;
2136}
2137
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002138/*
2139 * Adjust the columns of text properties in line "lnum" after position "col" to
2140 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002141 * Note that "col" is zero-based, while tp_col is one-based.
2142 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002143 * "flags" can have:
2144 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2145 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002146 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002147 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002148 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002149 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002150 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002151adjust_prop_columns(
2152 linenr_T lnum,
2153 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002154 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002155 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002156{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002157 int proplen;
2158 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002159 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002160 int ri, wi;
2161 size_t textlen;
2162
2163 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002164 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002165
2166 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2167 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002168 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002169 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002170
Bram Moolenaar196d1572019-01-02 23:47:18 +01002171 wi = 0; // write index
2172 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002173 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002174 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002175 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002176
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002177 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2178 res = adjust_prop(&prop, col, bytes_added, flags);
2179 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002180 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002181 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002182 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2183 && u_savesub(lnum) == FAIL)
2184 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002185 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002186
2187 // u_savesub() may have updated curbuf->b_ml, fetch it again
2188 if (curbuf->b_ml.ml_line_lnum != lnum)
2189 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002190 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002191 if (res.can_drop)
2192 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002193 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002194 ++wi;
2195 }
2196 if (dirty)
2197 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002198 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2199
2200 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002201 {
2202 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2203
2204 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2205 vim_free(curbuf->b_ml.ml_line_ptr);
2206 curbuf->b_ml.ml_line_ptr = p;
2207 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002208 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002209 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002210 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002211 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002212}
2213
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002214/*
2215 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002216 * "lnum_props" is the line that has the properties from before the split.
2217 * "lnum_top" is the top line.
2218 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002219 * "deleted" is the number of bytes deleted.
2220 */
2221 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002222adjust_props_for_split(
2223 linenr_T lnum_props,
2224 linenr_T lnum_top,
2225 int kept,
2226 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002227{
2228 char_u *props;
2229 int count;
2230 garray_T prevprop;
2231 garray_T nextprop;
2232 int i;
2233 int skipped = kept + deleted;
2234
2235 if (!curbuf->b_has_textprop)
2236 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002237
2238 // Get the text properties from "lnum_props".
2239 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002240 ga_init2(&prevprop, sizeof(textprop_T), 10);
2241 ga_init2(&nextprop, sizeof(textprop_T), 10);
2242
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002243 // Keep the relevant ones in the first line, reducing the length if needed.
2244 // Copy the ones that include the split to the second line.
2245 // Move the ones after the split to the second line.
2246 for (i = 0; i < count; ++i)
2247 {
2248 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002249 proptype_T *pt;
2250 int start_incl, end_incl;
2251 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002252
2253 // copy the prop to an aligned structure
2254 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2255
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002256 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2257 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2258 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002259 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2260 cont_next = prop.tp_col != MAXCOL
2261 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002262 // when a prop has text it is never copied
2263 if (prop.tp_id < 0 && cont_next)
2264 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002265
2266 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002267 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002268 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2269
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002270 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002271 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002272 if (p->tp_col + p->tp_len >= kept)
2273 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002274 if (cont_next)
2275 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002276 }
2277
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002278 // Only add the property to the next line if the length is bigger than
2279 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002280 if ((cont_next || prop.tp_col == MAXCOL)
2281 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002282 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002283 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002284
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002285 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002286 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002287 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002288 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002289 if (p->tp_col > skipped)
2290 p->tp_col -= skipped - 1;
2291 else
2292 {
2293 p->tp_len -= skipped - p->tp_col;
2294 p->tp_col = 1;
2295 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002296 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002297 if (cont_prev)
2298 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002299 }
2300 }
2301
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002302 set_text_props(lnum_top, prevprop.ga_data,
2303 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002304 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002305 set_text_props(lnum_top + 1, nextprop.ga_data,
2306 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002307 ga_clear(&nextprop);
2308}
2309
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002310/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002311 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002312 */
2313 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002314prepend_joined_props(
2315 char_u *new_props,
2316 int propcount,
2317 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002318 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002319 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002320 long col,
2321 int removed)
2322{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002323 char_u *props;
2324 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2325 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002326
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002327 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002328 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002329 textprop_T prop;
2330 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002331
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002332 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002333 if (prop.tp_col == MAXCOL && !last_line)
2334 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002335 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2336
2337 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002338 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002339
Bram Moolenaare175dc62022-08-01 22:18:50 +01002340 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002341 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2342 &prop, sizeof(prop));
2343 else
2344 {
2345 int j;
2346 int found = FALSE;
2347
2348 // Search for continuing prop.
2349 for (j = *props_remaining; j < propcount; ++j)
2350 {
2351 textprop_T op;
2352
2353 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2354 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2355 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002356 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002357 found = TRUE;
2358 op.tp_len += op.tp_col - prop.tp_col;
2359 op.tp_col = prop.tp_col;
2360 // Start/end is taken care of when deleting joined lines
2361 op.tp_flags = prop.tp_flags;
2362 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2363 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002364 }
2365 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002366 if (!found)
2367 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002368 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002369 }
2370}
2371
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002372#endif // FEAT_PROP_POPUP