blob: 80b0151679cd4b57940b54690593e83d9bcf1e08 [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/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100611 * Return the number of text properties with "above" or "below" alignment in
612 * line "lnum". A "right" aligned property also goes below after a "below" or
613 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100614 */
615 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100616prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100617{
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 }
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100639 else if (prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
640 {
641 next_right_goes_below = FALSE;
642 ++result;
643 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100644 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
645 next_right_goes_below = TRUE;
646 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100647 }
648 return result;
649}
650
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200651/**
652 * Return the number of text properties on line "lnum" in the current buffer.
653 * When "only_starting" is true only text properties starting in this line will
654 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100655 * When "last_line" is FALSE then text properties after the line are not
656 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200657 */
658 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100659count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200660{
661 char_u *props;
662 int proplen = get_text_props(curbuf, lnum, &props, 0);
663 int result = proplen;
664 int i;
665 textprop_T prop;
666
Bram Moolenaare175dc62022-08-01 22:18:50 +0100667 for (i = 0; i < proplen; ++i)
668 {
669 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100670 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100671 // previous line, or when not in the last line and it is virtual text
672 // after the line.
673 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
674 || (!last_line && prop.tp_col == MAXCOL))
675 --result;
676 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200677 return result;
678}
679
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100680static textprop_T *text_prop_compare_props;
681static buf_T *text_prop_compare_buf;
682
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100683/* Score for sorting on position of the text property: 0: above,
684 * 1: after (default), 2: right, 3: below (comes last)
685 */
686 static int
687text_prop_order(int flags)
688{
689 if (flags & TP_FLAG_ALIGN_ABOVE)
690 return 0;
691 if (flags & TP_FLAG_ALIGN_RIGHT)
692 return 2;
693 if (flags & TP_FLAG_ALIGN_BELOW)
694 return 3;
695 return 1;
696}
697
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100698/*
699 * Function passed to qsort() to sort text properties.
700 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
701 * both have the same priority.
702 */
703 static int
704text_prop_compare(const void *s1, const void *s2)
705{
706 int idx1, idx2;
707 textprop_T *tp1, *tp2;
708 proptype_T *pt1, *pt2;
709 colnr_T col1, col2;
710
711 idx1 = *(int *)s1;
712 idx2 = *(int *)s2;
713 tp1 = &text_prop_compare_props[idx1];
714 tp2 = &text_prop_compare_props[idx2];
715 col1 = tp1->tp_col;
716 col2 = tp2->tp_col;
717 if (col1 == MAXCOL && col2 == MAXCOL)
718 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100719 int order1 = text_prop_order(tp1->tp_flags);
720 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100721
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100722 // both props add text before or after the line, sort on order where it
723 // is added
724 if (order1 != order2)
725 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100726 }
727
728 // property that inserts text has priority over one that doesn't
729 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
730 return tp1->tp_id < 0 ? 1 : -1;
731
732 // check highest priority, defined by the type
733 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
734 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
735 if (pt1 != pt2)
736 {
737 if (pt1 == NULL)
738 return -1;
739 if (pt2 == NULL)
740 return 1;
741 if (pt1->pt_priority != pt2->pt_priority)
742 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
743 }
744
745 // same priority, one that starts first wins
746 if (col1 != col2)
747 return col1 < col2 ? 1 : -1;
748
749 // for a property with text the id can be used as tie breaker
750 if (tp1->tp_id < 0)
751 return tp1->tp_id > tp2->tp_id ? 1 : -1;
752
753 return 0;
754}
755
756/*
757 * Sort "count" text properties using an array if indexes "idxs" into the list
758 * of text props "props" for buffer "buf".
759 */
760 void
761sort_text_props(
762 buf_T *buf,
763 textprop_T *props,
764 int *idxs,
765 int count)
766{
767 text_prop_compare_buf = buf;
768 text_prop_compare_props = props;
769 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
770}
771
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100772/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200773 * Find text property "type_id" in the visible lines of window "wp".
774 * Match "id" when it is > 0.
775 * Returns FAIL when not found.
776 */
777 int
778find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
779 linenr_T *found_lnum)
780{
781 linenr_T lnum;
782 char_u *props;
783 int count;
784 int i;
785
786 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100787 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200788 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
789 {
790 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
791 for (i = 0; i < count; ++i)
792 {
793 mch_memmove(prop, props + i * sizeof(textprop_T),
794 sizeof(textprop_T));
795 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
796 {
797 *found_lnum = lnum;
798 return OK;
799 }
800 }
801 }
802 return FAIL;
803}
804
805/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100806 * Set the text properties for line "lnum" to "props" with length "len".
807 * If "len" is zero text properties are removed, "props" is not used.
808 * Any existing text properties are dropped.
809 * Only works for the current buffer.
810 */
811 static void
812set_text_props(linenr_T lnum, char_u *props, int len)
813{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100814 char_u *text;
815 char_u *newtext;
816 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100817
818 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100819 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100820 newtext = alloc(textlen + len);
821 if (newtext == NULL)
822 return;
823 mch_memmove(newtext, text, textlen);
824 if (len > 0)
825 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100826 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100827 vim_free(curbuf->b_ml.ml_line_ptr);
828 curbuf->b_ml.ml_line_ptr = newtext;
829 curbuf->b_ml.ml_line_len = textlen + len;
830 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
831}
832
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100833/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100834 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100835 */
836 void
837add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
838{
839 char_u *text;
840 char_u *newtext;
841 int proplen = text_prop_count * (int)sizeof(textprop_T);
842
843 text = ml_get(lnum);
844 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
845 if (newtext == NULL)
846 return;
847 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
848 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
849 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
850 vim_free(curbuf->b_ml.ml_line_ptr);
851 curbuf->b_ml.ml_line_ptr = newtext;
852 curbuf->b_ml.ml_line_len += proplen;
853 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
854}
855
Bram Moolenaare44336b2022-08-07 18:20:08 +0100856/*
857 * Function passed to qsort() for sorting proptype_T on pt_id.
858 */
859 static int
860compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100861{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100862 proptype_T *tp1 = *(proptype_T **)s1;
863 proptype_T *tp2 = *(proptype_T **)s2;
864
865 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
866}
867
868 static proptype_T *
869find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
870{
871 int low = 0;
872 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100873
Bram Moolenaar10246902022-08-08 17:08:05 +0100874 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100875 return NULL;
876
Bram Moolenaare44336b2022-08-07 18:20:08 +0100877 // Make the loopup faster by creating an array with pointers to
878 // hashtable entries, sorted on pt_id.
879 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100880 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100881 long todo;
882 hashitem_T *hi;
883 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100884
Bram Moolenaare44336b2022-08-07 18:20:08 +0100885 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
886 if (*array == NULL)
887 return NULL;
888 todo = (long)ht->ht_used;
889 for (hi = ht->ht_array; todo > 0; ++hi)
890 {
891 if (!HASHITEM_EMPTY(hi))
892 {
893 (*array)[i++] = HI2PT(hi);
894 --todo;
895 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100896 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100897 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
898 }
899
900 // binary search in the sorted array
901 high = ht->ht_used;
902 while (high > low)
903 {
904 int m = (high + low) / 2;
905
906 if ((*array)[m]->pt_id == id)
907 return (*array)[m];
908 if ((*array)[m]->pt_id > id)
909 high = m;
910 else
911 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100912 }
913 return NULL;
914}
915
916/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100917 * Fill 'dict' with text properties in 'prop'.
918 */
919 static void
920prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
921{
922 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200923 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100924
925 dict_add_number(dict, "col", prop->tp_col);
926 dict_add_number(dict, "length", prop->tp_len);
927 dict_add_number(dict, "id", prop->tp_id);
928 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
929 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200930
Bram Moolenaare44336b2022-08-07 18:20:08 +0100931 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200932 if (pt == NULL)
933 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100934 pt = find_type_by_id(global_proptypes, &global_proparray,
935 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200936 buflocal = FALSE;
937 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100938 if (pt != NULL)
939 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200940
941 if (buflocal)
942 dict_add_number(dict, "type_bufnr", buf->b_fnum);
943 else
944 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100945}
946
947/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100948 * Find a property type by ID in "buf" or globally.
949 * Returns NULL if not found.
950 */
951 proptype_T *
952text_prop_type_by_id(buf_T *buf, int id)
953{
954 proptype_T *type;
955
Bram Moolenaare44336b2022-08-07 18:20:08 +0100956 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100957 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100958 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100959 return type;
960}
961
962/*
963 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
964 */
965 void
966f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
967{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200968 linenr_T start;
969 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100970 linenr_T lnum;
971 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100972 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100973
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200974 if (in_vim9script()
975 && (check_for_number_arg(argvars, 0) == FAIL
976 || check_for_opt_number_arg(argvars, 1) == FAIL
977 || (argvars[1].v_type != VAR_UNKNOWN
978 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
979 return;
980
981 start = tv_get_number(&argvars[0]);
982 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100983 if (argvars[1].v_type != VAR_UNKNOWN)
984 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100985 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100986 if (argvars[2].v_type != VAR_UNKNOWN)
987 {
988 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
989 return;
990 }
991 }
992 if (start < 1 || end < 1)
993 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200994 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100995 return;
996 }
997
998 for (lnum = start; lnum <= end; ++lnum)
999 {
1000 char_u *text;
1001 size_t len;
1002
1003 if (lnum > buf->b_ml.ml_line_count)
1004 break;
1005 text = ml_get_buf(buf, lnum, FALSE);
1006 len = STRLEN(text) + 1;
1007 if ((size_t)buf->b_ml.ml_line_len > len)
1008 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001009 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001010 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1011 {
1012 char_u *newtext = vim_strsave(text);
1013
1014 // need to allocate the line now
1015 if (newtext == NULL)
1016 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001017 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1018 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001019 buf->b_ml.ml_line_ptr = newtext;
1020 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1021 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001022 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001023 }
1024 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001025 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001026 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001027}
1028
1029/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001030 * prop_find({props} [, {direction}])
1031 */
1032 void
1033f_prop_find(typval_T *argvars, typval_T *rettv)
1034{
1035 pos_T *cursor = &curwin->w_cursor;
1036 dict_T *dict;
1037 buf_T *buf = curbuf;
1038 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001039 int lnum_start;
1040 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001041 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001042 int id = 0;
1043 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001044 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001045 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001046 int lnum = -1;
1047 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001048 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001049 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001050
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001051 if (in_vim9script()
1052 && (check_for_dict_arg(argvars, 0) == FAIL
1053 || check_for_opt_string_arg(argvars, 1) == FAIL))
1054 return;
1055
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001056 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001057 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001058 dict = argvars[0].vval.v_dict;
1059
1060 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1061 return;
1062 if (buf->b_ml.ml_mfp == NULL)
1063 return;
1064
1065 if (argvars[1].v_type != VAR_UNKNOWN)
1066 {
1067 char_u *dir_s = tv_get_string(&argvars[1]);
1068
1069 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001070 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001071 else if (*dir_s != 'f')
1072 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001073 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001074 return;
1075 }
1076 }
1077
1078 di = dict_find(dict, (char_u *)"lnum", -1);
1079 if (di != NULL)
1080 lnum = tv_get_number(&di->di_tv);
1081
1082 di = dict_find(dict, (char_u *)"col", -1);
1083 if (di != NULL)
1084 col = tv_get_number(&di->di_tv);
1085
1086 if (lnum == -1)
1087 {
1088 lnum = cursor->lnum;
1089 col = cursor->col + 1;
1090 }
1091 else if (col == -1)
1092 col = 1;
1093
1094 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1095 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001096 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001097 return;
1098 }
1099
Bram Moolenaard61efa52022-07-23 09:52:04 +01001100 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001101
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001102 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001103 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001104 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001105 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001106 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001107 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001108 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001109 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001110 proptype_T *type = lookup_prop_type(name, buf);
1111
1112 if (type == NULL)
1113 return;
1114 type_id = type->pt_id;
1115 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001116 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001117 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001118 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001119 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001120 return;
1121 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001122 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001123 {
Ben Jacksona7704222022-08-20 20:54:51 +01001124 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001125 return;
1126 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001127
1128 lnum_start = lnum;
1129
1130 if (rettv_dict_alloc(rettv) == FAIL)
1131 return;
1132
1133 while (1)
1134 {
1135 char_u *text = ml_get_buf(buf, lnum, FALSE);
1136 size_t textlen = STRLEN(text) + 1;
1137 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001138 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001139 int i;
1140 textprop_T prop;
1141 int prop_start;
1142 int prop_end;
1143
LemonBoy9bd3ce22022-04-18 21:54:02 +01001144 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001145 {
1146 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001147 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001148
LemonBoy9bd3ce22022-04-18 21:54:02 +01001149 // For the very first line try to find the first property before or
1150 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001151 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001152 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001153 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001154 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001155 if (prop.tp_col > col)
1156 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001157 }
1158 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1159 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001160 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001161 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001162 : (id_found && prop.tp_id == id)
1163 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001164 {
1165 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001166 if (lnum_start == lnum
1167 && col >= prop.tp_col
1168 && (col <= prop.tp_col + prop.tp_len
1169 - (prop.tp_len != 0)))
1170 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001171
LemonBoy9bd3ce22022-04-18 21:54:02 +01001172 // The property was not continued from last line, it starts on
1173 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001174 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001175 // The property does not continue on the next line, it ends on
1176 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001177 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001178 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001179 seen_end = 1;
1180
1181 // Skip lines without the start flag.
1182 if (!prop_start)
1183 {
1184 // Always search backwards for start when search started
1185 // on a prop and we're not skipping.
1186 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001187 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001188 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001189 }
1190
1191 // If skipstart is true, skip the prop at start pos (even if
1192 // continued from another line).
1193 if (start_pos_has_prop && skipstart && !seen_end)
1194 {
1195 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001196 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001197 }
1198
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001199 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1200 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1201
1202 return;
1203 }
1204 }
1205
1206 if (dir > 0)
1207 {
1208 if (lnum >= buf->b_ml.ml_line_count)
1209 break;
1210 lnum++;
1211 }
1212 else
1213 {
1214 if (lnum <= 1)
1215 break;
1216 lnum--;
1217 }
1218 }
1219}
1220
1221/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001222 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1223 */
1224 static int
1225prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1226{
1227 int i;
1228
1229 for (i = 0; i < len; i++)
1230 if (types_or_ids[i] == type_or_id)
1231 return TRUE;
1232
1233 return FALSE;
1234}
1235
1236/*
1237 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1238 * If 'prop_types' is not NULL, then return only the text properties with
1239 * matching property type in the 'prop_types' array.
1240 * If 'prop_ids' is not NULL, then return only the text properties with
1241 * an identifier in the 'props_ids' array.
1242 * If 'add_lnum' is TRUE, then add the line number also to the text property
1243 * dictionary.
1244 */
1245 static void
1246get_props_in_line(
1247 buf_T *buf,
1248 linenr_T lnum,
1249 int *prop_types,
1250 int prop_types_len,
1251 int *prop_ids,
1252 int prop_ids_len,
1253 list_T *retlist,
1254 int add_lnum)
1255{
1256 char_u *text = ml_get_buf(buf, lnum, FALSE);
1257 size_t textlen = STRLEN(text) + 1;
1258 int count;
1259 int i;
1260 textprop_T prop;
1261
1262 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1263 for (i = 0; i < count; ++i)
1264 {
1265 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1266 sizeof(textprop_T));
1267 if ((prop_types == NULL
1268 || prop_type_or_id_in_list(prop_types, prop_types_len,
1269 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001270 && (prop_ids == NULL
1271 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1272 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001273 {
1274 dict_T *d = dict_alloc();
1275
1276 if (d == NULL)
1277 break;
1278 prop_fill_dict(d, &prop, buf);
1279 if (add_lnum)
1280 dict_add_number(d, "lnum", lnum);
1281 list_append_dict(retlist, d);
1282 }
1283 }
1284}
1285
1286/*
1287 * Convert a List of property type names into an array of property type
1288 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1289 * error. 'num_types' is set to the number of returned property types.
1290 */
1291 static int *
1292get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1293{
1294 int *prop_types;
1295 listitem_T *li;
1296 int i;
1297 char_u *name;
1298 proptype_T *type;
1299
1300 *num_types = 0;
1301
1302 prop_types = ALLOC_MULT(int, list_len(l));
1303 if (prop_types == NULL)
1304 return NULL;
1305
1306 i = 0;
1307 FOR_ALL_LIST_ITEMS(l, li)
1308 {
1309 if (li->li_tv.v_type != VAR_STRING)
1310 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001311 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001312 goto errret;
1313 }
1314 name = li->li_tv.vval.v_string;
1315 if (name == NULL)
1316 goto errret;
1317
1318 type = lookup_prop_type(name, buf);
1319 if (type == NULL)
1320 goto errret;
1321 prop_types[i++] = type->pt_id;
1322 }
1323
1324 *num_types = i;
1325 return prop_types;
1326
1327errret:
1328 VIM_CLEAR(prop_types);
1329 return NULL;
1330}
1331
1332/*
1333 * Convert a List of property identifiers into an array of property
1334 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1335 * error. 'num_ids' is set to the number of returned property identifiers.
1336 */
1337 static int *
1338get_prop_ids_from_list(list_T *l, int *num_ids)
1339{
1340 int *prop_ids;
1341 listitem_T *li;
1342 int i;
1343 int id;
1344 int error;
1345
1346 *num_ids = 0;
1347
1348 prop_ids = ALLOC_MULT(int, list_len(l));
1349 if (prop_ids == NULL)
1350 return NULL;
1351
1352 i = 0;
1353 FOR_ALL_LIST_ITEMS(l, li)
1354 {
1355 error = FALSE;
1356 id = tv_get_number_chk(&li->li_tv, &error);
1357 if (error)
1358 goto errret;
1359
1360 prop_ids[i++] = id;
1361 }
1362
1363 *num_ids = i;
1364 return prop_ids;
1365
1366errret:
1367 VIM_CLEAR(prop_ids);
1368 return NULL;
1369}
1370
1371/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001372 * prop_list({lnum} [, {bufnr}])
1373 */
1374 void
1375f_prop_list(typval_T *argvars, typval_T *rettv)
1376{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001377 linenr_T lnum;
1378 linenr_T start_lnum;
1379 linenr_T end_lnum;
1380 buf_T *buf = curbuf;
1381 int add_lnum = FALSE;
1382 int *prop_types = NULL;
1383 int prop_types_len = 0;
1384 int *prop_ids = NULL;
1385 int prop_ids_len = 0;
1386 list_T *l;
1387 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001388
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001389 if (in_vim9script()
1390 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001391 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001392 return;
1393
Bram Moolenaar93a10962022-06-16 11:42:09 +01001394 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001395 return;
1396
1397 // default: get text properties on current line
1398 start_lnum = tv_get_number(&argvars[0]);
1399 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001400 if (argvars[1].v_type != VAR_UNKNOWN)
1401 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001402 dict_T *d;
1403
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001404 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001405 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001406 d = argvars[1].vval.v_dict;
1407
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001408 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1409 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001410
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001411 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001412 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001413 if (di->di_tv.v_type != VAR_NUMBER)
1414 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001415 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001416 return;
1417 }
1418 end_lnum = tv_get_number(&di->di_tv);
1419 if (end_lnum < 0)
1420 // negative end_lnum is used as an offset from the last buffer
1421 // line
1422 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1423 else if (end_lnum > buf->b_ml.ml_line_count)
1424 end_lnum = buf->b_ml.ml_line_count;
1425 add_lnum = TRUE;
1426 }
1427 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1428 {
1429 if (di->di_tv.v_type != VAR_LIST)
1430 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001431 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001432 return;
1433 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001434
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001435 l = di->di_tv.vval.v_list;
1436 if (l != NULL && list_len(l) > 0)
1437 {
1438 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1439 if (prop_types == NULL)
1440 return;
1441 }
1442 }
1443 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1444 {
1445 if (di->di_tv.v_type != VAR_LIST)
1446 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001447 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001448 goto errret;
1449 }
1450
1451 l = di->di_tv.vval.v_list;
1452 if (l != NULL && list_len(l) > 0)
1453 {
1454 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1455 if (prop_ids == NULL)
1456 goto errret;
1457 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001458 }
1459 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001460 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1461 || end_lnum < 1 || end_lnum < start_lnum)
1462 emsg(_(e_invalid_range));
1463 else
1464 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1465 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1466 prop_ids, prop_ids_len,
1467 rettv->vval.v_list, add_lnum);
1468
1469errret:
1470 VIM_CLEAR(prop_types);
1471 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001472}
1473
1474/*
1475 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1476 */
1477 void
1478f_prop_remove(typval_T *argvars, typval_T *rettv)
1479{
1480 linenr_T start = 1;
1481 linenr_T end = 0;
1482 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001483 linenr_T first_changed = 0;
1484 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001485 dict_T *dict;
1486 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001487 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001488 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001489 int type_id = -1; // for a single "type"
1490 int *type_ids = NULL; // array, for a list of "types", allocated
1491 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001492 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001493 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494
1495 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001496
1497 if (in_vim9script()
1498 && (check_for_dict_arg(argvars, 0) == FAIL
1499 || check_for_opt_number_arg(argvars, 1) == FAIL
1500 || (argvars[1].v_type != VAR_UNKNOWN
1501 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1502 return;
1503
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001504 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001505 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001506
1507 if (argvars[1].v_type != VAR_UNKNOWN)
1508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001509 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001510 end = start;
1511 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001512 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001513 if (start < 1 || end < 1)
1514 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001515 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001516 return;
1517 }
1518 }
1519
1520 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001521 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1522 return;
1523 if (buf->b_ml.ml_mfp == NULL)
1524 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001525
Bram Moolenaard61efa52022-07-23 09:52:04 +01001526 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001527
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001528 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001529 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001530
1531 // if a specific type was supplied "type": check that (and ignore "types".
1532 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001533 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001534 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001535 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001536 proptype_T *type = lookup_prop_type(name, buf);
1537
1538 if (type == NULL)
1539 return;
1540 type_id = type->pt_id;
1541 }
Ben Jacksona7704222022-08-20 20:54:51 +01001542 if (dict_has_key(dict, "types"))
1543 {
1544 typval_T types;
1545 listitem_T *li = NULL;
1546
1547 dict_get_tv(dict, "types", &types);
1548 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1549 {
1550 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1551
1552 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1553 {
1554 proptype_T *prop_type;
1555
1556 if (li->li_tv.v_type != VAR_STRING)
1557 continue;
1558
1559 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1560
1561 if (!prop_type)
1562 goto cleanup_prop_remove;
1563
1564 type_ids[num_type_ids++] = prop_type->pt_id;
1565 }
1566 }
1567 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001568 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001569
Ben Jacksona7704222022-08-20 20:54:51 +01001570 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001571 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001572 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001573 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001574 }
Ben Jacksona7704222022-08-20 20:54:51 +01001575 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001576 {
Ben Jacksona7704222022-08-20 20:54:51 +01001577 emsg(_(e_need_id_and_type_or_types_with_both));
1578 goto cleanup_prop_remove;
1579 }
1580 if (type_id != -1 && num_type_ids > 0)
1581 {
1582 emsg(_(e_cannot_specify_both_type_and_types));
1583 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001584 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001585
1586 if (end == 0)
1587 end = buf->b_ml.ml_line_count;
1588 for (lnum = start; lnum <= end; ++lnum)
1589 {
1590 char_u *text;
1591 size_t len;
1592
1593 if (lnum > buf->b_ml.ml_line_count)
1594 break;
1595 text = ml_get_buf(buf, lnum, FALSE);
1596 len = STRLEN(text) + 1;
1597 if ((size_t)buf->b_ml.ml_line_len > len)
1598 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001599 static textprop_T textprop; // static because of alignment
1600 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001601
1602 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1603 / sizeof(textprop_T); ++idx)
1604 {
1605 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1606 + idx * sizeof(textprop_T);
1607 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001608 int matches_id = 0;
1609 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001610
1611 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001612
1613 matches_id = textprop.tp_id == id;
1614 if (num_type_ids > 0)
1615 {
1616 int idx2;
1617
1618 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1619 matches_type = textprop.tp_type == type_ids[idx2];
1620 }
1621 else
1622 {
1623 matches_type = textprop.tp_type == type_id;
1624 }
1625
1626 if (both ? matches_id && matches_type
1627 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001628 {
1629 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1630 {
1631 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1632
1633 // need to allocate the line to be able to change it
1634 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001635 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001636 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1637 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001638 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1639 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001640 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001641 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1642
1643 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001644 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001645 }
1646
1647 taillen = buf->b_ml.ml_line_len - len
1648 - (idx + 1) * sizeof(textprop_T);
1649 if (taillen > 0)
1650 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1651 taillen);
1652 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1653 --idx;
1654
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001655 if (textprop.tp_id < 0)
1656 {
1657 garray_T *gap = &buf->b_textprop_text;
1658 int ii = -textprop.tp_id - 1;
1659
1660 // negative ID: property with text - free the text
1661 if (ii < gap->ga_len)
1662 {
1663 char_u **p = ((char_u **)gap->ga_data) + ii;
1664 vim_free(*p);
1665 *p = NULL;
1666 did_remove_text = TRUE;
1667 }
1668 }
1669
Bram Moolenaar965c0442021-05-17 00:22:06 +02001670 if (first_changed == 0)
1671 first_changed = lnum;
1672 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001673 ++rettv->vval.v_number;
1674 if (!do_all)
1675 break;
1676 }
1677 }
1678 }
1679 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001680
Bram Moolenaar965c0442021-05-17 00:22:06 +02001681 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001682 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001683 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001684 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001685 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001686 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001687
1688 if (did_remove_text)
1689 {
1690 garray_T *gap = &buf->b_textprop_text;
1691
1692 // Reduce the growarray size for NULL pointers at the end.
1693 while (gap->ga_len > 0
1694 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1695 --gap->ga_len;
1696 }
Ben Jacksona7704222022-08-20 20:54:51 +01001697
1698cleanup_prop_remove:
1699 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001700}
1701
1702/*
1703 * Common for f_prop_type_add() and f_prop_type_change().
1704 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001705 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001706prop_type_set(typval_T *argvars, int add)
1707{
1708 char_u *name;
1709 buf_T *buf = NULL;
1710 dict_T *dict;
1711 dictitem_T *di;
1712 proptype_T *prop;
1713
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001714 if (in_vim9script()
1715 && (check_for_string_arg(argvars, 0) == FAIL
1716 || check_for_dict_arg(argvars, 1) == FAIL))
1717 return;
1718
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001719 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001720 if (*name == NUL)
1721 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001722 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001723 return;
1724 }
1725
1726 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1727 return;
1728 dict = argvars[1].vval.v_dict;
1729
Bram Moolenaare44336b2022-08-07 18:20:08 +01001730 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001731 if (add)
1732 {
1733 hashtab_T **htp;
1734
1735 if (prop != NULL)
1736 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001737 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001738 return;
1739 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001740 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001741 if (prop == NULL)
1742 return;
1743 STRCPY(prop->pt_name, name);
1744 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001745 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001746 if (buf == NULL)
1747 {
1748 htp = &global_proptypes;
1749 VIM_CLEAR(global_proparray);
1750 }
1751 else
1752 {
1753 htp = &buf->b_proptypes;
1754 VIM_CLEAR(buf->b_proparray);
1755 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001756 if (*htp == NULL)
1757 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001758 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001759 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001760 {
1761 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001762 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001763 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001764 hash_init(*htp);
1765 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001766 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001767 }
1768 else
1769 {
1770 if (prop == NULL)
1771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001772 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001773 return;
1774 }
1775 }
1776
1777 if (dict != NULL)
1778 {
1779 di = dict_find(dict, (char_u *)"highlight", -1);
1780 if (di != NULL)
1781 {
1782 char_u *highlight;
1783 int hl_id = 0;
1784
Bram Moolenaard61efa52022-07-23 09:52:04 +01001785 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001786 if (highlight != NULL && *highlight != NUL)
1787 hl_id = syn_name2id(highlight);
1788 if (hl_id <= 0)
1789 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001790 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001791 highlight == NULL ? (char_u *)"" : highlight);
1792 return;
1793 }
1794 prop->pt_hl_id = hl_id;
1795 }
1796
Bram Moolenaar58187f12019-05-05 16:33:47 +02001797 di = dict_find(dict, (char_u *)"combine", -1);
1798 if (di != NULL)
1799 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001800 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001801 prop->pt_flags |= PT_FLAG_COMBINE;
1802 else
1803 prop->pt_flags &= ~PT_FLAG_COMBINE;
1804 }
1805
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001806 di = dict_find(dict, (char_u *)"override", -1);
1807 if (di != NULL)
1808 {
1809 if (tv_get_bool(&di->di_tv))
1810 prop->pt_flags |= PT_FLAG_OVERRIDE;
1811 else
1812 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1813 }
1814
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001815 di = dict_find(dict, (char_u *)"priority", -1);
1816 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001817 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001818
1819 di = dict_find(dict, (char_u *)"start_incl", -1);
1820 if (di != NULL)
1821 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001822 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001823 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1824 else
1825 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1826 }
1827
1828 di = dict_find(dict, (char_u *)"end_incl", -1);
1829 if (di != NULL)
1830 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001831 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001832 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1833 else
1834 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1835 }
1836 }
1837}
1838
1839/*
1840 * prop_type_add({name}, {props})
1841 */
1842 void
1843f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1844{
1845 prop_type_set(argvars, TRUE);
1846}
1847
1848/*
1849 * prop_type_change({name}, {props})
1850 */
1851 void
1852f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1853{
1854 prop_type_set(argvars, FALSE);
1855}
1856
1857/*
1858 * prop_type_delete({name} [, {bufnr}])
1859 */
1860 void
1861f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1862{
1863 char_u *name;
1864 buf_T *buf = NULL;
1865 hashitem_T *hi;
1866
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001867 if (in_vim9script()
1868 && (check_for_string_arg(argvars, 0) == FAIL
1869 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1870 return;
1871
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001872 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001873 if (*name == NUL)
1874 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001875 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001876 return;
1877 }
1878
1879 if (argvars[1].v_type != VAR_UNKNOWN)
1880 {
1881 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1882 return;
1883 }
1884
Bram Moolenaare44336b2022-08-07 18:20:08 +01001885 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001886 if (hi != NULL)
1887 {
1888 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001889 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001890
1891 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001892 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001893 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001894 VIM_CLEAR(global_proparray);
1895 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001896 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001897 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001898 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001899 VIM_CLEAR(buf->b_proparray);
1900 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001901 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001902 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001903 }
1904}
1905
1906/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001907 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001908 */
1909 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001910f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001911{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001912 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001913
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001914 if (in_vim9script()
1915 && (check_for_string_arg(argvars, 0) == FAIL
1916 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1917 return;
1918
1919 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001920 if (*name == NUL)
1921 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001922 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001923 return;
1924 }
1925 if (rettv_dict_alloc(rettv) == OK)
1926 {
1927 proptype_T *prop = NULL;
1928 buf_T *buf = NULL;
1929
1930 if (argvars[1].v_type != VAR_UNKNOWN)
1931 {
1932 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1933 return;
1934 }
1935
Bram Moolenaare44336b2022-08-07 18:20:08 +01001936 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001937 if (prop != NULL)
1938 {
1939 dict_T *d = rettv->vval.v_dict;
1940
1941 if (prop->pt_hl_id > 0)
1942 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1943 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001944 dict_add_number(d, "combine",
1945 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001946 dict_add_number(d, "start_incl",
1947 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1948 dict_add_number(d, "end_incl",
1949 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1950 if (buf != NULL)
1951 dict_add_number(d, "bufnr", buf->b_fnum);
1952 }
1953 }
1954}
1955
1956 static void
1957list_types(hashtab_T *ht, list_T *l)
1958{
1959 long todo;
1960 hashitem_T *hi;
1961
1962 todo = (long)ht->ht_used;
1963 for (hi = ht->ht_array; todo > 0; ++hi)
1964 {
1965 if (!HASHITEM_EMPTY(hi))
1966 {
1967 proptype_T *prop = HI2PT(hi);
1968
1969 list_append_string(l, prop->pt_name, -1);
1970 --todo;
1971 }
1972 }
1973}
1974
1975/*
1976 * prop_type_list([{bufnr}])
1977 */
1978 void
1979f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1980{
1981 buf_T *buf = NULL;
1982
1983 if (rettv_list_alloc(rettv) == OK)
1984 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001985 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1986 return;
1987
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001988 if (argvars[0].v_type != VAR_UNKNOWN)
1989 {
1990 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1991 return;
1992 }
1993 if (buf == NULL)
1994 {
1995 if (global_proptypes != NULL)
1996 list_types(global_proptypes, rettv->vval.v_list);
1997 }
1998 else if (buf->b_proptypes != NULL)
1999 list_types(buf->b_proptypes, rettv->vval.v_list);
2000 }
2001}
2002
2003/*
2004 * Free all property types in "ht".
2005 */
2006 static void
2007clear_ht_prop_types(hashtab_T *ht)
2008{
2009 long todo;
2010 hashitem_T *hi;
2011
2012 if (ht == NULL)
2013 return;
2014
2015 todo = (long)ht->ht_used;
2016 for (hi = ht->ht_array; todo > 0; ++hi)
2017 {
2018 if (!HASHITEM_EMPTY(hi))
2019 {
2020 proptype_T *prop = HI2PT(hi);
2021
2022 vim_free(prop);
2023 --todo;
2024 }
2025 }
2026
2027 hash_clear(ht);
2028 vim_free(ht);
2029}
2030
2031#if defined(EXITFREE) || defined(PROTO)
2032/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002033 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002034 */
2035 void
2036clear_global_prop_types(void)
2037{
2038 clear_ht_prop_types(global_proptypes);
2039 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002040 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002041}
2042#endif
2043
2044/*
2045 * Free all property types for "buf".
2046 */
2047 void
2048clear_buf_prop_types(buf_T *buf)
2049{
2050 clear_ht_prop_types(buf->b_proptypes);
2051 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002052 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002053}
2054
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002055// Struct used to return two values from adjust_prop().
2056typedef struct
2057{
2058 int dirty; // if the property was changed
2059 int can_drop; // whether after this change, the prop may be removed
2060} adjustres_T;
2061
2062/*
2063 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2064 *
2065 * Note that "col" is zero-based, while tp_col is one-based.
2066 * Only for the current buffer.
2067 * "flags" can have:
2068 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002069 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002070 */
2071 static adjustres_T
2072adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002073 textprop_T *prop,
2074 colnr_T col,
2075 int added,
2076 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002077{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002078 proptype_T *pt;
2079 int start_incl;
2080 int end_incl;
2081 int droppable;
2082 adjustres_T res = {TRUE, FALSE};
2083
2084 // prop after end of the line doesn't move
2085 if (prop->tp_col == MAXCOL)
2086 {
2087 res.dirty = FALSE;
2088 return res;
2089 }
2090
2091 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2092 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002093 || (flags & APC_SUBSTITUTE)
2094 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002095 if (prop->tp_id < 0 && (flags & APC_INDENT))
2096 // when inserting indent just before a character with virtual text
2097 // shift the text property
2098 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002099 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002100 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002101 // do not drop zero-width props if they later can increase in size
2102 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002103
2104 if (added > 0)
2105 {
2106 if (col + 1 <= prop->tp_col
2107 - (start_incl || (prop->tp_len == 0 && end_incl)))
2108 // Change is entirely before the text property: Only shift
2109 prop->tp_col += added;
2110 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2111 // Insertion was inside text property
2112 prop->tp_len += added;
2113 }
2114 else if (prop->tp_col > col + 1)
2115 {
2116 if (prop->tp_col + added < col + 1)
2117 {
2118 prop->tp_len += (prop->tp_col - 1 - col) + added;
2119 prop->tp_col = col + 1;
2120 if (prop->tp_len <= 0)
2121 {
2122 prop->tp_len = 0;
2123 res.can_drop = droppable;
2124 }
2125 }
2126 else
2127 prop->tp_col += added;
2128 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002129 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2130 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002131 {
2132 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2133
2134 prop->tp_len += after > 0 ? added + after : added;
2135 res.can_drop = prop->tp_len <= 0 && droppable;
2136 }
2137 else
2138 res.dirty = FALSE;
2139
2140 return res;
2141}
2142
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002143/*
2144 * Adjust the columns of text properties in line "lnum" after position "col" to
2145 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002146 * Note that "col" is zero-based, while tp_col is one-based.
2147 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002148 * "flags" can have:
2149 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2150 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002151 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002152 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002153 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002154 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002155 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002156adjust_prop_columns(
2157 linenr_T lnum,
2158 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002159 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002160 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002161{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002162 int proplen;
2163 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002164 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002165 int ri, wi;
2166 size_t textlen;
2167
2168 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002169 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002170
2171 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2172 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002173 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002174 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002175
Bram Moolenaar196d1572019-01-02 23:47:18 +01002176 wi = 0; // write index
2177 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002178 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002179 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002180 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002181
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002182 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2183 res = adjust_prop(&prop, col, bytes_added, flags);
2184 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002185 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002186 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002187 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2188 && u_savesub(lnum) == FAIL)
2189 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002190 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002191
2192 // u_savesub() may have updated curbuf->b_ml, fetch it again
2193 if (curbuf->b_ml.ml_line_lnum != lnum)
2194 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002195 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002196 if (res.can_drop)
2197 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002198 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002199 ++wi;
2200 }
2201 if (dirty)
2202 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002203 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2204
2205 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002206 {
2207 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2208
2209 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2210 vim_free(curbuf->b_ml.ml_line_ptr);
2211 curbuf->b_ml.ml_line_ptr = p;
2212 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002213 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002214 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002215 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002216 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002217}
2218
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002219/*
2220 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002221 * "lnum_props" is the line that has the properties from before the split.
2222 * "lnum_top" is the top line.
2223 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002224 * "deleted" is the number of bytes deleted.
2225 */
2226 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002227adjust_props_for_split(
2228 linenr_T lnum_props,
2229 linenr_T lnum_top,
2230 int kept,
2231 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002232{
2233 char_u *props;
2234 int count;
2235 garray_T prevprop;
2236 garray_T nextprop;
2237 int i;
2238 int skipped = kept + deleted;
2239
2240 if (!curbuf->b_has_textprop)
2241 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002242
2243 // Get the text properties from "lnum_props".
2244 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002245 ga_init2(&prevprop, sizeof(textprop_T), 10);
2246 ga_init2(&nextprop, sizeof(textprop_T), 10);
2247
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002248 // Keep the relevant ones in the first line, reducing the length if needed.
2249 // Copy the ones that include the split to the second line.
2250 // Move the ones after the split to the second line.
2251 for (i = 0; i < count; ++i)
2252 {
2253 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002254 proptype_T *pt;
2255 int start_incl, end_incl;
2256 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002257
2258 // copy the prop to an aligned structure
2259 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2260
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002261 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2262 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2263 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002264 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2265 cont_next = prop.tp_col != MAXCOL
2266 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002267 // when a prop has text it is never copied
2268 if (prop.tp_id < 0 && cont_next)
2269 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002270
2271 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002272 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002273 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2274
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002275 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002276 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002277 if (p->tp_col + p->tp_len >= kept)
2278 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002279 if (cont_next)
2280 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002281 }
2282
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002283 // Only add the property to the next line if the length is bigger than
2284 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002285 if ((cont_next || prop.tp_col == MAXCOL)
2286 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002287 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002288 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002289
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002290 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002291 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002292 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002293 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002294 if (p->tp_col > skipped)
2295 p->tp_col -= skipped - 1;
2296 else
2297 {
2298 p->tp_len -= skipped - p->tp_col;
2299 p->tp_col = 1;
2300 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002301 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002302 if (cont_prev)
2303 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002304 }
2305 }
2306
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002307 set_text_props(lnum_top, prevprop.ga_data,
2308 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002309 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002310 set_text_props(lnum_top + 1, nextprop.ga_data,
2311 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002312 ga_clear(&nextprop);
2313}
2314
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002315/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002316 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002317 */
2318 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002319prepend_joined_props(
2320 char_u *new_props,
2321 int propcount,
2322 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002323 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002324 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002325 long col,
2326 int removed)
2327{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002328 char_u *props;
2329 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2330 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002331
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002332 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002333 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002334 textprop_T prop;
2335 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002336
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002337 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002338 if (prop.tp_col == MAXCOL && !last_line)
2339 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002340 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2341
2342 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002343 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002344
Bram Moolenaare175dc62022-08-01 22:18:50 +01002345 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002346 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2347 &prop, sizeof(prop));
2348 else
2349 {
2350 int j;
2351 int found = FALSE;
2352
2353 // Search for continuing prop.
2354 for (j = *props_remaining; j < propcount; ++j)
2355 {
2356 textprop_T op;
2357
2358 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2359 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2360 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002361 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002362 found = TRUE;
2363 op.tp_len += op.tp_col - prop.tp_col;
2364 op.tp_col = prop.tp_col;
2365 // Start/end is taken care of when deleting joined lines
2366 op.tp_flags = prop.tp_flags;
2367 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2368 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002369 }
2370 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002371 if (!found)
2372 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002373 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002374 }
2375}
2376
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002377#endif // FEAT_PROP_POPUP