blob: ba21e751bff454ffa8edce66e9035d4420cdd8be [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 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100235 colnr_T col; // start column use in tp_col
236 colnr_T sort_col; // column where it appears
237 long length; // in bytes
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200238
239 // Fetch the line to get the ml_line_len field updated.
240 proplen = get_text_props(buf, lnum, &props, TRUE);
241 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
242
243 if (lnum == start_lnum)
244 col = start_col;
245 else
246 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100247 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200248 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000249 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100250 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200251 }
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100252 sort_col = col;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200253
254 if (lnum == end_lnum)
255 length = end_col - col;
256 else
257 length = (int)textlen - col + 1;
258 if (length > (long)textlen)
259 length = (int)textlen; // can include the end-of-line
260 if (length < 0)
261 length = 0; // zero-width property
262
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100263 if (text_arg != NULL)
264 {
265 length = 1; // text is placed on one character
266 if (col == 0)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100267 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100268 col = MAXCOL; // before or after the line
269 if ((text_flags & TP_FLAG_ALIGN_ABOVE) == 0)
270 sort_col = MAXCOL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100271 length += text_padding_left;
272 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100273 }
274
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200275 // Allocate the new line with space for the new property.
276 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
277 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100278 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200279 // Copy the text, including terminating NUL.
280 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
281
282 // Find the index where to insert the new property.
283 // Since the text properties are not aligned properly when stored with
284 // the text, we need to copy them as bytes before using it as a struct.
285 for (i = 0; i < proplen; ++i)
286 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100287 colnr_T prop_col;
288
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200289 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
290 sizeof(textprop_T));
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100291 // col is MAXCOL when the text goes above or after the line, when
292 // above we should use column zero for sorting
293 prop_col = (tmp_prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
294 ? 0 : tmp_prop.tp_col;
295 if (prop_col >= sort_col)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200296 break;
297 }
298 newprops = newtext + textlen;
299 if (i > 0)
300 mch_memmove(newprops, props, sizeof(textprop_T) * i);
301
302 tmp_prop.tp_col = col;
303 tmp_prop.tp_len = length;
304 tmp_prop.tp_id = id;
305 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100306 tmp_prop.tp_flags = text_flags
307 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100308 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
309 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
310 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200311 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
312 sizeof(textprop_T));
313
314 if (i < proplen)
315 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
316 props + i * sizeof(textprop_T),
317 sizeof(textprop_T) * (proplen - i));
318
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100319 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200320 vim_free(buf->b_ml.ml_line_ptr);
321 buf->b_ml.ml_line_ptr = newtext;
322 buf->b_ml.ml_line_len += sizeof(textprop_T);
323 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
324 }
325
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100326 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200327 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100328 res = OK;
329
330theend:
331 vim_free(text);
332 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200333}
334
335/*
336 * prop_add_list()
337 * First argument specifies the text property:
338 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
339 * Second argument is a List where each item is a List with the following
340 * entries: [lnum, start_col, end_col]
341 */
342 void
343f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
344{
345 dict_T *dict;
346 char_u *type_name;
347 buf_T *buf = curbuf;
348 int id = 0;
349 listitem_T *li;
350 list_T *pos_list;
351 linenr_T start_lnum;
352 colnr_T start_col;
353 linenr_T end_lnum;
354 colnr_T end_col;
355 int error = FALSE;
356
357 if (check_for_dict_arg(argvars, 0) == FAIL
358 || check_for_list_arg(argvars, 1) == FAIL)
359 return;
360
Bram Moolenaard83392a2022-09-01 12:22:46 +0100361 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200362 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363
364 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100365 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200366 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000367 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200368 return;
369 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100370 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200371
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100372 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100373 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200374
375 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
376 return;
377
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100378 // This must be done _before_ we start adding properties because property
379 // changes trigger buffer (memline) reorganisation, which needs this flag
380 // to be correctly set.
381 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200382 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
383 {
384 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
385 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000386 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200387 return;
388 }
389
390 pos_list = li->li_tv.vval.v_list;
391 start_lnum = list_find_nr(pos_list, 0L, &error);
392 start_col = list_find_nr(pos_list, 1L, &error);
393 end_lnum = list_find_nr(pos_list, 2L, &error);
394 end_col = list_find_nr(pos_list, 3L, &error);
395 if (error || start_lnum <= 0 || start_col <= 0
396 || end_lnum <= 0 || end_col <= 0)
397 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000398 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200399 return;
400 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100401 if (prop_add_one(buf, type_name, id, NULL, 0, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200402 start_col, end_col) == FAIL)
403 return;
404 }
405
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100406 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200407}
408
409/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100410 * Get the next ID to use for a textprop with text in buffer "buf".
411 */
412 static int
413get_textprop_id(buf_T *buf)
414{
415 // TODO: recycle deleted entries
416 return -(buf->b_textprop_text.ga_len + 1);
417}
418
419/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200420 * Shared between prop_add() and popup_create().
421 * "dict_arg" is the function argument of a dict containing "bufnr".
422 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100423 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200424 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100425 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426prop_add_common(
427 linenr_T start_lnum,
428 colnr_T start_col,
429 dict_T *dict,
430 buf_T *default_buf,
431 typval_T *dict_arg)
432{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200433 linenr_T end_lnum;
434 colnr_T end_col;
435 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200436 buf_T *buf = default_buf;
437 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100438 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100439 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100440 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100441
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100442 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100443 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000444 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100445 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100446 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100447 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100448
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100449 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100450 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100451 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100452 if (end_lnum < start_lnum)
453 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000454 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100455 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100456 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100457 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100458 else
459 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100460
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100461 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100462 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100463 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100465 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000467 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100468 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100469 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100470 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100472 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100474 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100475 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100476 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000477 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100478 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100479 }
480 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100481 else if (start_lnum == end_lnum)
482 end_col = start_col;
483 else
484 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100485
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100486 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100487 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100488
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100489 if (dict_has_key(dict, "text"))
490 {
491 text = dict_get_string(dict, "text", TRUE);
492 if (text == NULL)
493 goto theend;
494 // use a default length of 1 to make multiple props show up
495 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100496
497 if (dict_has_key(dict, "text_align"))
498 {
499 char_u *p = dict_get_string(dict, "text_align", FALSE);
500
501 if (p == NULL)
502 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100503 if (start_col != 0)
504 {
505 emsg(_(e_can_only_use_text_align_when_column_is_zero));
506 goto theend;
507 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100508 if (STRCMP(p, "right") == 0)
509 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100510 else if (STRCMP(p, "above") == 0)
511 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100512 else if (STRCMP(p, "below") == 0)
513 flags |= TP_FLAG_ALIGN_BELOW;
514 else if (STRCMP(p, "after") != 0)
515 {
516 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
517 goto theend;
518 }
519 }
520
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100521 if (dict_has_key(dict, "text_padding_left"))
522 {
523 text_padding_left = dict_get_number(dict, "text_padding_left");
524 if (text_padding_left < 0)
525 {
526 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
527 goto theend;
528 }
529 }
530
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100531 if (dict_has_key(dict, "text_wrap"))
532 {
533 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100534
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100535 if (p == NULL)
536 goto theend;
537 if (STRCMP(p, "wrap") == 0)
538 flags |= TP_FLAG_WRAP;
539 else if (STRCMP(p, "truncate") != 0)
540 {
541 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
542 goto theend;
543 }
544 }
545 }
546
547 // Column must be 1 or more for a normal text property; when "text" is
548 // present zero means it goes after the line.
549 if (start_col < (text == NULL ? 1 : 0))
550 {
551 semsg(_(e_invalid_column_number_nr), (long)start_col);
552 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100553 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100554 if (start_col > 0 && text_padding_left > 0)
555 {
556 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
557 goto theend;
558 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100559
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200560 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100561 goto theend;
562
563 if (id < 0 && buf->b_textprop_text.ga_len > 0)
564 {
565 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
566 goto theend;
567 }
568 if (text != NULL)
569 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100570
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100571 // This must be done _before_ we add the property because property changes
572 // trigger buffer (memline) reorganisation, which needs this flag to be
573 // correctly set.
574 buf->b_has_textprop = TRUE; // this is never reset
575
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100576 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100577 start_lnum, end_lnum, start_col, end_col);
578 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100579
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100580 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100581
582theend:
583 vim_free(text);
584 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100585}
586
587/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100588 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100589 * Returns the number of text properties and, when non-zero, a pointer to the
590 * first one in "props" (note that it is not aligned, therefore the char_u
591 * pointer).
592 */
593 int
594get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
595{
596 char_u *text;
597 size_t textlen;
598 size_t proplen;
599
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100600 // Be quick when no text property types have been defined or the buffer,
601 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200602 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100603 return 0;
604
605 // Fetch the line to get the ml_line_len field updated.
606 text = ml_get_buf(buf, lnum, will_change);
607 textlen = STRLEN(text) + 1;
608 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100609 if (proplen == 0)
610 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100611 if (proplen % sizeof(textprop_T) != 0)
612 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000613 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100614 return 0;
615 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100616 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100617 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100618}
619
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100620/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100621 * Return the number of text properties with "above" or "below" alignment in
622 * line "lnum". A "right" aligned property also goes below after a "below" or
623 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100624 */
625 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100626prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100627{
628 char_u *props;
629 int count = get_text_props(buf, lnum, &props, FALSE);
630 int result = 0;
631 textprop_T prop;
632 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100633 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100634
635 if (count == 0)
636 return 0;
637 for (i = 0; i < count; ++i)
638 {
639 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100640 if (prop.tp_col == MAXCOL)
641 {
642 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
643 || (next_right_goes_below
644 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
645 {
646 next_right_goes_below = TRUE;
647 ++result;
648 }
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100649 else if (prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
650 {
651 next_right_goes_below = FALSE;
652 ++result;
653 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100654 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
655 next_right_goes_below = TRUE;
656 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100657 }
658 return result;
659}
660
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200661/**
662 * Return the number of text properties on line "lnum" in the current buffer.
663 * When "only_starting" is true only text properties starting in this line will
664 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100665 * When "last_line" is FALSE then text properties after the line are not
666 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200667 */
668 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100669count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200670{
671 char_u *props;
672 int proplen = get_text_props(curbuf, lnum, &props, 0);
673 int result = proplen;
674 int i;
675 textprop_T prop;
676
Bram Moolenaare175dc62022-08-01 22:18:50 +0100677 for (i = 0; i < proplen; ++i)
678 {
679 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100680 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100681 // previous line, or when not in the last line and it is virtual text
682 // after the line.
683 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
684 || (!last_line && prop.tp_col == MAXCOL))
685 --result;
686 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200687 return result;
688}
689
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100690static textprop_T *text_prop_compare_props;
691static buf_T *text_prop_compare_buf;
692
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100693/* Score for sorting on position of the text property: 0: above,
694 * 1: after (default), 2: right, 3: below (comes last)
695 */
696 static int
697text_prop_order(int flags)
698{
699 if (flags & TP_FLAG_ALIGN_ABOVE)
700 return 0;
701 if (flags & TP_FLAG_ALIGN_RIGHT)
702 return 2;
703 if (flags & TP_FLAG_ALIGN_BELOW)
704 return 3;
705 return 1;
706}
707
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100708/*
709 * Function passed to qsort() to sort text properties.
710 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
711 * both have the same priority.
712 */
713 static int
714text_prop_compare(const void *s1, const void *s2)
715{
716 int idx1, idx2;
717 textprop_T *tp1, *tp2;
718 proptype_T *pt1, *pt2;
719 colnr_T col1, col2;
720
721 idx1 = *(int *)s1;
722 idx2 = *(int *)s2;
723 tp1 = &text_prop_compare_props[idx1];
724 tp2 = &text_prop_compare_props[idx2];
725 col1 = tp1->tp_col;
726 col2 = tp2->tp_col;
727 if (col1 == MAXCOL && col2 == MAXCOL)
728 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100729 int order1 = text_prop_order(tp1->tp_flags);
730 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100731
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100732 // both props add text before or after the line, sort on order where it
733 // is added
734 if (order1 != order2)
735 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100736 }
737
738 // property that inserts text has priority over one that doesn't
739 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
740 return tp1->tp_id < 0 ? 1 : -1;
741
742 // check highest priority, defined by the type
743 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
744 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
745 if (pt1 != pt2)
746 {
747 if (pt1 == NULL)
748 return -1;
749 if (pt2 == NULL)
750 return 1;
751 if (pt1->pt_priority != pt2->pt_priority)
752 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
753 }
754
755 // same priority, one that starts first wins
756 if (col1 != col2)
757 return col1 < col2 ? 1 : -1;
758
759 // for a property with text the id can be used as tie breaker
760 if (tp1->tp_id < 0)
761 return tp1->tp_id > tp2->tp_id ? 1 : -1;
762
763 return 0;
764}
765
766/*
767 * Sort "count" text properties using an array if indexes "idxs" into the list
768 * of text props "props" for buffer "buf".
769 */
770 void
771sort_text_props(
772 buf_T *buf,
773 textprop_T *props,
774 int *idxs,
775 int count)
776{
777 text_prop_compare_buf = buf;
778 text_prop_compare_props = props;
779 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
780}
781
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100782/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200783 * Find text property "type_id" in the visible lines of window "wp".
784 * Match "id" when it is > 0.
785 * Returns FAIL when not found.
786 */
787 int
788find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
789 linenr_T *found_lnum)
790{
791 linenr_T lnum;
792 char_u *props;
793 int count;
794 int i;
795
796 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100797 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200798 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
799 {
800 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
801 for (i = 0; i < count; ++i)
802 {
803 mch_memmove(prop, props + i * sizeof(textprop_T),
804 sizeof(textprop_T));
805 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
806 {
807 *found_lnum = lnum;
808 return OK;
809 }
810 }
811 }
812 return FAIL;
813}
814
815/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100816 * Set the text properties for line "lnum" to "props" with length "len".
817 * If "len" is zero text properties are removed, "props" is not used.
818 * Any existing text properties are dropped.
819 * Only works for the current buffer.
820 */
821 static void
822set_text_props(linenr_T lnum, char_u *props, int len)
823{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100824 char_u *text;
825 char_u *newtext;
826 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100827
828 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100829 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100830 newtext = alloc(textlen + len);
831 if (newtext == NULL)
832 return;
833 mch_memmove(newtext, text, textlen);
834 if (len > 0)
835 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100836 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100837 vim_free(curbuf->b_ml.ml_line_ptr);
838 curbuf->b_ml.ml_line_ptr = newtext;
839 curbuf->b_ml.ml_line_len = textlen + len;
840 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
841}
842
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100843/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100844 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100845 */
846 void
847add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
848{
849 char_u *text;
850 char_u *newtext;
851 int proplen = text_prop_count * (int)sizeof(textprop_T);
852
853 text = ml_get(lnum);
854 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
855 if (newtext == NULL)
856 return;
857 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
858 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
859 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
860 vim_free(curbuf->b_ml.ml_line_ptr);
861 curbuf->b_ml.ml_line_ptr = newtext;
862 curbuf->b_ml.ml_line_len += proplen;
863 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
864}
865
Bram Moolenaare44336b2022-08-07 18:20:08 +0100866/*
867 * Function passed to qsort() for sorting proptype_T on pt_id.
868 */
869 static int
870compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100871{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100872 proptype_T *tp1 = *(proptype_T **)s1;
873 proptype_T *tp2 = *(proptype_T **)s2;
874
875 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
876}
877
878 static proptype_T *
879find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
880{
881 int low = 0;
882 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100883
Bram Moolenaar10246902022-08-08 17:08:05 +0100884 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100885 return NULL;
886
Bram Moolenaare44336b2022-08-07 18:20:08 +0100887 // Make the loopup faster by creating an array with pointers to
888 // hashtable entries, sorted on pt_id.
889 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100890 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100891 long todo;
892 hashitem_T *hi;
893 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100894
Bram Moolenaare44336b2022-08-07 18:20:08 +0100895 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
896 if (*array == NULL)
897 return NULL;
898 todo = (long)ht->ht_used;
899 for (hi = ht->ht_array; todo > 0; ++hi)
900 {
901 if (!HASHITEM_EMPTY(hi))
902 {
903 (*array)[i++] = HI2PT(hi);
904 --todo;
905 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100906 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100907 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
908 }
909
910 // binary search in the sorted array
911 high = ht->ht_used;
912 while (high > low)
913 {
914 int m = (high + low) / 2;
915
916 if ((*array)[m]->pt_id == id)
917 return (*array)[m];
918 if ((*array)[m]->pt_id > id)
919 high = m;
920 else
921 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100922 }
923 return NULL;
924}
925
926/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100927 * Fill 'dict' with text properties in 'prop'.
928 */
929 static void
930prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
931{
932 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200933 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100934
935 dict_add_number(dict, "col", prop->tp_col);
936 dict_add_number(dict, "length", prop->tp_len);
937 dict_add_number(dict, "id", prop->tp_id);
938 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
939 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200940
Bram Moolenaare44336b2022-08-07 18:20:08 +0100941 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200942 if (pt == NULL)
943 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100944 pt = find_type_by_id(global_proptypes, &global_proparray,
945 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200946 buflocal = FALSE;
947 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100948 if (pt != NULL)
949 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200950
951 if (buflocal)
952 dict_add_number(dict, "type_bufnr", buf->b_fnum);
953 else
954 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100955}
956
957/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100958 * Find a property type by ID in "buf" or globally.
959 * Returns NULL if not found.
960 */
961 proptype_T *
962text_prop_type_by_id(buf_T *buf, int id)
963{
964 proptype_T *type;
965
Bram Moolenaare44336b2022-08-07 18:20:08 +0100966 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100967 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100968 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100969 return type;
970}
971
972/*
973 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
974 */
975 void
976f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
977{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200978 linenr_T start;
979 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100980 linenr_T lnum;
981 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100982 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100983
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200984 if (in_vim9script()
985 && (check_for_number_arg(argvars, 0) == FAIL
986 || check_for_opt_number_arg(argvars, 1) == FAIL
987 || (argvars[1].v_type != VAR_UNKNOWN
988 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
989 return;
990
991 start = tv_get_number(&argvars[0]);
992 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100993 if (argvars[1].v_type != VAR_UNKNOWN)
994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100996 if (argvars[2].v_type != VAR_UNKNOWN)
997 {
998 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
999 return;
1000 }
1001 }
1002 if (start < 1 || end < 1)
1003 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001004 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001005 return;
1006 }
1007
1008 for (lnum = start; lnum <= end; ++lnum)
1009 {
1010 char_u *text;
1011 size_t len;
1012
1013 if (lnum > buf->b_ml.ml_line_count)
1014 break;
1015 text = ml_get_buf(buf, lnum, FALSE);
1016 len = STRLEN(text) + 1;
1017 if ((size_t)buf->b_ml.ml_line_len > len)
1018 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001019 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001020 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1021 {
1022 char_u *newtext = vim_strsave(text);
1023
1024 // need to allocate the line now
1025 if (newtext == NULL)
1026 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001027 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1028 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001029 buf->b_ml.ml_line_ptr = newtext;
1030 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1031 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001032 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001033 }
1034 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001035 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001036 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001037}
1038
1039/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001040 * prop_find({props} [, {direction}])
1041 */
1042 void
1043f_prop_find(typval_T *argvars, typval_T *rettv)
1044{
1045 pos_T *cursor = &curwin->w_cursor;
1046 dict_T *dict;
1047 buf_T *buf = curbuf;
1048 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001049 int lnum_start;
1050 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001051 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001052 int id = 0;
1053 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001054 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001055 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001056 int lnum = -1;
1057 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001058 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001059 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001060
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001061 if (in_vim9script()
1062 && (check_for_dict_arg(argvars, 0) == FAIL
1063 || check_for_opt_string_arg(argvars, 1) == FAIL))
1064 return;
1065
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001066 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001067 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001068 dict = argvars[0].vval.v_dict;
1069
1070 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1071 return;
1072 if (buf->b_ml.ml_mfp == NULL)
1073 return;
1074
1075 if (argvars[1].v_type != VAR_UNKNOWN)
1076 {
1077 char_u *dir_s = tv_get_string(&argvars[1]);
1078
1079 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001080 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001081 else if (*dir_s != 'f')
1082 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001083 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001084 return;
1085 }
1086 }
1087
1088 di = dict_find(dict, (char_u *)"lnum", -1);
1089 if (di != NULL)
1090 lnum = tv_get_number(&di->di_tv);
1091
1092 di = dict_find(dict, (char_u *)"col", -1);
1093 if (di != NULL)
1094 col = tv_get_number(&di->di_tv);
1095
1096 if (lnum == -1)
1097 {
1098 lnum = cursor->lnum;
1099 col = cursor->col + 1;
1100 }
1101 else if (col == -1)
1102 col = 1;
1103
1104 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1105 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001106 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001107 return;
1108 }
1109
Bram Moolenaard61efa52022-07-23 09:52:04 +01001110 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001111
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001112 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001113 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001114 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001115 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001116 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001117 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001118 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001119 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001120 proptype_T *type = lookup_prop_type(name, buf);
1121
1122 if (type == NULL)
1123 return;
1124 type_id = type->pt_id;
1125 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001126 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001127 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001128 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001129 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001130 return;
1131 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001132 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001133 {
Ben Jacksona7704222022-08-20 20:54:51 +01001134 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001135 return;
1136 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001137
1138 lnum_start = lnum;
1139
1140 if (rettv_dict_alloc(rettv) == FAIL)
1141 return;
1142
1143 while (1)
1144 {
1145 char_u *text = ml_get_buf(buf, lnum, FALSE);
1146 size_t textlen = STRLEN(text) + 1;
1147 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001148 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001149 int i;
1150 textprop_T prop;
1151 int prop_start;
1152 int prop_end;
1153
LemonBoy9bd3ce22022-04-18 21:54:02 +01001154 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001155 {
1156 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001157 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001158
LemonBoy9bd3ce22022-04-18 21:54:02 +01001159 // For the very first line try to find the first property before or
1160 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001161 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001162 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001163 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001164 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001165 if (prop.tp_col > col)
1166 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001167 }
1168 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1169 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001170 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001171 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001172 : (id_found && prop.tp_id == id)
1173 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001174 {
1175 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001176 if (lnum_start == lnum
1177 && col >= prop.tp_col
1178 && (col <= prop.tp_col + prop.tp_len
1179 - (prop.tp_len != 0)))
1180 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001181
LemonBoy9bd3ce22022-04-18 21:54:02 +01001182 // The property was not continued from last line, it starts on
1183 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001184 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001185 // The property does not continue on the next line, it ends on
1186 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001187 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001188 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001189 seen_end = 1;
1190
1191 // Skip lines without the start flag.
1192 if (!prop_start)
1193 {
1194 // Always search backwards for start when search started
1195 // on a prop and we're not skipping.
1196 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001197 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001198 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001199 }
1200
1201 // If skipstart is true, skip the prop at start pos (even if
1202 // continued from another line).
1203 if (start_pos_has_prop && skipstart && !seen_end)
1204 {
1205 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001206 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001207 }
1208
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001209 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1210 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1211
1212 return;
1213 }
1214 }
1215
1216 if (dir > 0)
1217 {
1218 if (lnum >= buf->b_ml.ml_line_count)
1219 break;
1220 lnum++;
1221 }
1222 else
1223 {
1224 if (lnum <= 1)
1225 break;
1226 lnum--;
1227 }
1228 }
1229}
1230
1231/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001232 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1233 */
1234 static int
1235prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1236{
1237 int i;
1238
1239 for (i = 0; i < len; i++)
1240 if (types_or_ids[i] == type_or_id)
1241 return TRUE;
1242
1243 return FALSE;
1244}
1245
1246/*
1247 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1248 * If 'prop_types' is not NULL, then return only the text properties with
1249 * matching property type in the 'prop_types' array.
1250 * If 'prop_ids' is not NULL, then return only the text properties with
1251 * an identifier in the 'props_ids' array.
1252 * If 'add_lnum' is TRUE, then add the line number also to the text property
1253 * dictionary.
1254 */
1255 static void
1256get_props_in_line(
1257 buf_T *buf,
1258 linenr_T lnum,
1259 int *prop_types,
1260 int prop_types_len,
1261 int *prop_ids,
1262 int prop_ids_len,
1263 list_T *retlist,
1264 int add_lnum)
1265{
1266 char_u *text = ml_get_buf(buf, lnum, FALSE);
1267 size_t textlen = STRLEN(text) + 1;
1268 int count;
1269 int i;
1270 textprop_T prop;
1271
1272 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1273 for (i = 0; i < count; ++i)
1274 {
1275 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1276 sizeof(textprop_T));
1277 if ((prop_types == NULL
1278 || prop_type_or_id_in_list(prop_types, prop_types_len,
1279 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001280 && (prop_ids == NULL
1281 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1282 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001283 {
1284 dict_T *d = dict_alloc();
1285
1286 if (d == NULL)
1287 break;
1288 prop_fill_dict(d, &prop, buf);
1289 if (add_lnum)
1290 dict_add_number(d, "lnum", lnum);
1291 list_append_dict(retlist, d);
1292 }
1293 }
1294}
1295
1296/*
1297 * Convert a List of property type names into an array of property type
1298 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1299 * error. 'num_types' is set to the number of returned property types.
1300 */
1301 static int *
1302get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1303{
1304 int *prop_types;
1305 listitem_T *li;
1306 int i;
1307 char_u *name;
1308 proptype_T *type;
1309
1310 *num_types = 0;
1311
1312 prop_types = ALLOC_MULT(int, list_len(l));
1313 if (prop_types == NULL)
1314 return NULL;
1315
1316 i = 0;
1317 FOR_ALL_LIST_ITEMS(l, li)
1318 {
1319 if (li->li_tv.v_type != VAR_STRING)
1320 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001321 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001322 goto errret;
1323 }
1324 name = li->li_tv.vval.v_string;
1325 if (name == NULL)
1326 goto errret;
1327
1328 type = lookup_prop_type(name, buf);
1329 if (type == NULL)
1330 goto errret;
1331 prop_types[i++] = type->pt_id;
1332 }
1333
1334 *num_types = i;
1335 return prop_types;
1336
1337errret:
1338 VIM_CLEAR(prop_types);
1339 return NULL;
1340}
1341
1342/*
1343 * Convert a List of property identifiers into an array of property
1344 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1345 * error. 'num_ids' is set to the number of returned property identifiers.
1346 */
1347 static int *
1348get_prop_ids_from_list(list_T *l, int *num_ids)
1349{
1350 int *prop_ids;
1351 listitem_T *li;
1352 int i;
1353 int id;
1354 int error;
1355
1356 *num_ids = 0;
1357
1358 prop_ids = ALLOC_MULT(int, list_len(l));
1359 if (prop_ids == NULL)
1360 return NULL;
1361
1362 i = 0;
1363 FOR_ALL_LIST_ITEMS(l, li)
1364 {
1365 error = FALSE;
1366 id = tv_get_number_chk(&li->li_tv, &error);
1367 if (error)
1368 goto errret;
1369
1370 prop_ids[i++] = id;
1371 }
1372
1373 *num_ids = i;
1374 return prop_ids;
1375
1376errret:
1377 VIM_CLEAR(prop_ids);
1378 return NULL;
1379}
1380
1381/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001382 * prop_list({lnum} [, {bufnr}])
1383 */
1384 void
1385f_prop_list(typval_T *argvars, typval_T *rettv)
1386{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001387 linenr_T lnum;
1388 linenr_T start_lnum;
1389 linenr_T end_lnum;
1390 buf_T *buf = curbuf;
1391 int add_lnum = FALSE;
1392 int *prop_types = NULL;
1393 int prop_types_len = 0;
1394 int *prop_ids = NULL;
1395 int prop_ids_len = 0;
1396 list_T *l;
1397 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001398
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001399 if (in_vim9script()
1400 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001401 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001402 return;
1403
Bram Moolenaar93a10962022-06-16 11:42:09 +01001404 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001405 return;
1406
1407 // default: get text properties on current line
1408 start_lnum = tv_get_number(&argvars[0]);
1409 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001410 if (argvars[1].v_type != VAR_UNKNOWN)
1411 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001412 dict_T *d;
1413
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001414 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001415 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001416 d = argvars[1].vval.v_dict;
1417
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001418 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1419 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001420
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001421 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001422 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001423 if (di->di_tv.v_type != VAR_NUMBER)
1424 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001425 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001426 return;
1427 }
1428 end_lnum = tv_get_number(&di->di_tv);
1429 if (end_lnum < 0)
1430 // negative end_lnum is used as an offset from the last buffer
1431 // line
1432 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1433 else if (end_lnum > buf->b_ml.ml_line_count)
1434 end_lnum = buf->b_ml.ml_line_count;
1435 add_lnum = TRUE;
1436 }
1437 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1438 {
1439 if (di->di_tv.v_type != VAR_LIST)
1440 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001441 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001442 return;
1443 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001444
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001445 l = di->di_tv.vval.v_list;
1446 if (l != NULL && list_len(l) > 0)
1447 {
1448 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1449 if (prop_types == NULL)
1450 return;
1451 }
1452 }
1453 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1454 {
1455 if (di->di_tv.v_type != VAR_LIST)
1456 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001457 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001458 goto errret;
1459 }
1460
1461 l = di->di_tv.vval.v_list;
1462 if (l != NULL && list_len(l) > 0)
1463 {
1464 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1465 if (prop_ids == NULL)
1466 goto errret;
1467 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001468 }
1469 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001470 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1471 || end_lnum < 1 || end_lnum < start_lnum)
1472 emsg(_(e_invalid_range));
1473 else
1474 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1475 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1476 prop_ids, prop_ids_len,
1477 rettv->vval.v_list, add_lnum);
1478
1479errret:
1480 VIM_CLEAR(prop_types);
1481 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001482}
1483
1484/*
1485 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1486 */
1487 void
1488f_prop_remove(typval_T *argvars, typval_T *rettv)
1489{
1490 linenr_T start = 1;
1491 linenr_T end = 0;
1492 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001493 linenr_T first_changed = 0;
1494 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495 dict_T *dict;
1496 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001497 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001498 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001499 int type_id = -1; // for a single "type"
1500 int *type_ids = NULL; // array, for a list of "types", allocated
1501 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001502 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001503 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001504
1505 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001506
1507 if (in_vim9script()
1508 && (check_for_dict_arg(argvars, 0) == FAIL
1509 || check_for_opt_number_arg(argvars, 1) == FAIL
1510 || (argvars[1].v_type != VAR_UNKNOWN
1511 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1512 return;
1513
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001514 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001515 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001516
1517 if (argvars[1].v_type != VAR_UNKNOWN)
1518 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001519 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001520 end = start;
1521 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001522 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001523 if (start < 1 || end < 1)
1524 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001525 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001526 return;
1527 }
1528 }
1529
1530 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001531 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1532 return;
1533 if (buf->b_ml.ml_mfp == NULL)
1534 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001535
Bram Moolenaard61efa52022-07-23 09:52:04 +01001536 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001537
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001538 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001539 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001540
1541 // if a specific type was supplied "type": check that (and ignore "types".
1542 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001543 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001544 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001545 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001546 proptype_T *type = lookup_prop_type(name, buf);
1547
1548 if (type == NULL)
1549 return;
1550 type_id = type->pt_id;
1551 }
Ben Jacksona7704222022-08-20 20:54:51 +01001552 if (dict_has_key(dict, "types"))
1553 {
1554 typval_T types;
1555 listitem_T *li = NULL;
1556
1557 dict_get_tv(dict, "types", &types);
1558 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1559 {
1560 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1561
1562 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1563 {
1564 proptype_T *prop_type;
1565
1566 if (li->li_tv.v_type != VAR_STRING)
1567 continue;
1568
1569 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1570
1571 if (!prop_type)
1572 goto cleanup_prop_remove;
1573
1574 type_ids[num_type_ids++] = prop_type->pt_id;
1575 }
1576 }
1577 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001578 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001579
Ben Jacksona7704222022-08-20 20:54:51 +01001580 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001581 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001582 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001583 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001584 }
Ben Jacksona7704222022-08-20 20:54:51 +01001585 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001586 {
Ben Jacksona7704222022-08-20 20:54:51 +01001587 emsg(_(e_need_id_and_type_or_types_with_both));
1588 goto cleanup_prop_remove;
1589 }
1590 if (type_id != -1 && num_type_ids > 0)
1591 {
1592 emsg(_(e_cannot_specify_both_type_and_types));
1593 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001594 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001595
1596 if (end == 0)
1597 end = buf->b_ml.ml_line_count;
1598 for (lnum = start; lnum <= end; ++lnum)
1599 {
1600 char_u *text;
1601 size_t len;
1602
1603 if (lnum > buf->b_ml.ml_line_count)
1604 break;
1605 text = ml_get_buf(buf, lnum, FALSE);
1606 len = STRLEN(text) + 1;
1607 if ((size_t)buf->b_ml.ml_line_len > len)
1608 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001609 static textprop_T textprop; // static because of alignment
1610 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001611
1612 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1613 / sizeof(textprop_T); ++idx)
1614 {
1615 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1616 + idx * sizeof(textprop_T);
1617 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001618 int matches_id = 0;
1619 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001620
1621 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001622
1623 matches_id = textprop.tp_id == id;
1624 if (num_type_ids > 0)
1625 {
1626 int idx2;
1627
1628 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1629 matches_type = textprop.tp_type == type_ids[idx2];
1630 }
1631 else
1632 {
1633 matches_type = textprop.tp_type == type_id;
1634 }
1635
1636 if (both ? matches_id && matches_type
1637 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001638 {
1639 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1640 {
1641 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1642
1643 // need to allocate the line to be able to change it
1644 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001645 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001646 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1647 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001648 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1649 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001650 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001651 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1652
1653 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001654 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001655 }
1656
1657 taillen = buf->b_ml.ml_line_len - len
1658 - (idx + 1) * sizeof(textprop_T);
1659 if (taillen > 0)
1660 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1661 taillen);
1662 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1663 --idx;
1664
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001665 if (textprop.tp_id < 0)
1666 {
1667 garray_T *gap = &buf->b_textprop_text;
1668 int ii = -textprop.tp_id - 1;
1669
1670 // negative ID: property with text - free the text
1671 if (ii < gap->ga_len)
1672 {
1673 char_u **p = ((char_u **)gap->ga_data) + ii;
1674 vim_free(*p);
1675 *p = NULL;
1676 did_remove_text = TRUE;
1677 }
1678 }
1679
Bram Moolenaar965c0442021-05-17 00:22:06 +02001680 if (first_changed == 0)
1681 first_changed = lnum;
1682 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001683 ++rettv->vval.v_number;
1684 if (!do_all)
1685 break;
1686 }
1687 }
1688 }
1689 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001690
Bram Moolenaar965c0442021-05-17 00:22:06 +02001691 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001692 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001693 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001694 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001695 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001696 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001697
1698 if (did_remove_text)
1699 {
1700 garray_T *gap = &buf->b_textprop_text;
1701
1702 // Reduce the growarray size for NULL pointers at the end.
1703 while (gap->ga_len > 0
1704 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1705 --gap->ga_len;
1706 }
Ben Jacksona7704222022-08-20 20:54:51 +01001707
1708cleanup_prop_remove:
1709 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001710}
1711
1712/*
1713 * Common for f_prop_type_add() and f_prop_type_change().
1714 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001715 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001716prop_type_set(typval_T *argvars, int add)
1717{
1718 char_u *name;
1719 buf_T *buf = NULL;
1720 dict_T *dict;
1721 dictitem_T *di;
1722 proptype_T *prop;
1723
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001724 if (in_vim9script()
1725 && (check_for_string_arg(argvars, 0) == FAIL
1726 || check_for_dict_arg(argvars, 1) == FAIL))
1727 return;
1728
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001729 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001730 if (*name == NUL)
1731 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001732 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001733 return;
1734 }
1735
1736 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1737 return;
1738 dict = argvars[1].vval.v_dict;
1739
Bram Moolenaare44336b2022-08-07 18:20:08 +01001740 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001741 if (add)
1742 {
1743 hashtab_T **htp;
1744
1745 if (prop != NULL)
1746 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001747 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001748 return;
1749 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001750 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001751 if (prop == NULL)
1752 return;
1753 STRCPY(prop->pt_name, name);
1754 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001755 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001756 if (buf == NULL)
1757 {
1758 htp = &global_proptypes;
1759 VIM_CLEAR(global_proparray);
1760 }
1761 else
1762 {
1763 htp = &buf->b_proptypes;
1764 VIM_CLEAR(buf->b_proparray);
1765 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001766 if (*htp == NULL)
1767 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001768 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001769 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001770 {
1771 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001772 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001773 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001774 hash_init(*htp);
1775 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001776 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001777 }
1778 else
1779 {
1780 if (prop == NULL)
1781 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001782 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001783 return;
1784 }
1785 }
1786
1787 if (dict != NULL)
1788 {
1789 di = dict_find(dict, (char_u *)"highlight", -1);
1790 if (di != NULL)
1791 {
1792 char_u *highlight;
1793 int hl_id = 0;
1794
Bram Moolenaard61efa52022-07-23 09:52:04 +01001795 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001796 if (highlight != NULL && *highlight != NUL)
1797 hl_id = syn_name2id(highlight);
1798 if (hl_id <= 0)
1799 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001800 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001801 highlight == NULL ? (char_u *)"" : highlight);
1802 return;
1803 }
1804 prop->pt_hl_id = hl_id;
1805 }
1806
Bram Moolenaar58187f12019-05-05 16:33:47 +02001807 di = dict_find(dict, (char_u *)"combine", -1);
1808 if (di != NULL)
1809 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001810 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001811 prop->pt_flags |= PT_FLAG_COMBINE;
1812 else
1813 prop->pt_flags &= ~PT_FLAG_COMBINE;
1814 }
1815
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001816 di = dict_find(dict, (char_u *)"override", -1);
1817 if (di != NULL)
1818 {
1819 if (tv_get_bool(&di->di_tv))
1820 prop->pt_flags |= PT_FLAG_OVERRIDE;
1821 else
1822 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1823 }
1824
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001825 di = dict_find(dict, (char_u *)"priority", -1);
1826 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001827 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001828
1829 di = dict_find(dict, (char_u *)"start_incl", -1);
1830 if (di != NULL)
1831 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001832 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001833 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1834 else
1835 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1836 }
1837
1838 di = dict_find(dict, (char_u *)"end_incl", -1);
1839 if (di != NULL)
1840 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001841 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001842 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1843 else
1844 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1845 }
1846 }
1847}
1848
1849/*
1850 * prop_type_add({name}, {props})
1851 */
1852 void
1853f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1854{
1855 prop_type_set(argvars, TRUE);
1856}
1857
1858/*
1859 * prop_type_change({name}, {props})
1860 */
1861 void
1862f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1863{
1864 prop_type_set(argvars, FALSE);
1865}
1866
1867/*
1868 * prop_type_delete({name} [, {bufnr}])
1869 */
1870 void
1871f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1872{
1873 char_u *name;
1874 buf_T *buf = NULL;
1875 hashitem_T *hi;
1876
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001877 if (in_vim9script()
1878 && (check_for_string_arg(argvars, 0) == FAIL
1879 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1880 return;
1881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001882 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001883 if (*name == NUL)
1884 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001885 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001886 return;
1887 }
1888
1889 if (argvars[1].v_type != VAR_UNKNOWN)
1890 {
1891 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1892 return;
1893 }
1894
Bram Moolenaare44336b2022-08-07 18:20:08 +01001895 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001896 if (hi != NULL)
1897 {
1898 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001899 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001900
1901 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001902 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001903 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001904 VIM_CLEAR(global_proparray);
1905 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001906 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001907 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001908 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001909 VIM_CLEAR(buf->b_proparray);
1910 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001911 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001912 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001913 }
1914}
1915
1916/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001917 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001918 */
1919 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001920f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001921{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001922 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001923
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001924 if (in_vim9script()
1925 && (check_for_string_arg(argvars, 0) == FAIL
1926 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1927 return;
1928
1929 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001930 if (*name == NUL)
1931 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001932 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001933 return;
1934 }
1935 if (rettv_dict_alloc(rettv) == OK)
1936 {
1937 proptype_T *prop = NULL;
1938 buf_T *buf = NULL;
1939
1940 if (argvars[1].v_type != VAR_UNKNOWN)
1941 {
1942 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1943 return;
1944 }
1945
Bram Moolenaare44336b2022-08-07 18:20:08 +01001946 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001947 if (prop != NULL)
1948 {
1949 dict_T *d = rettv->vval.v_dict;
1950
1951 if (prop->pt_hl_id > 0)
1952 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1953 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001954 dict_add_number(d, "combine",
1955 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001956 dict_add_number(d, "start_incl",
1957 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1958 dict_add_number(d, "end_incl",
1959 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1960 if (buf != NULL)
1961 dict_add_number(d, "bufnr", buf->b_fnum);
1962 }
1963 }
1964}
1965
1966 static void
1967list_types(hashtab_T *ht, list_T *l)
1968{
1969 long todo;
1970 hashitem_T *hi;
1971
1972 todo = (long)ht->ht_used;
1973 for (hi = ht->ht_array; todo > 0; ++hi)
1974 {
1975 if (!HASHITEM_EMPTY(hi))
1976 {
1977 proptype_T *prop = HI2PT(hi);
1978
1979 list_append_string(l, prop->pt_name, -1);
1980 --todo;
1981 }
1982 }
1983}
1984
1985/*
1986 * prop_type_list([{bufnr}])
1987 */
1988 void
1989f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1990{
1991 buf_T *buf = NULL;
1992
1993 if (rettv_list_alloc(rettv) == OK)
1994 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001995 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1996 return;
1997
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001998 if (argvars[0].v_type != VAR_UNKNOWN)
1999 {
2000 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2001 return;
2002 }
2003 if (buf == NULL)
2004 {
2005 if (global_proptypes != NULL)
2006 list_types(global_proptypes, rettv->vval.v_list);
2007 }
2008 else if (buf->b_proptypes != NULL)
2009 list_types(buf->b_proptypes, rettv->vval.v_list);
2010 }
2011}
2012
2013/*
2014 * Free all property types in "ht".
2015 */
2016 static void
2017clear_ht_prop_types(hashtab_T *ht)
2018{
2019 long todo;
2020 hashitem_T *hi;
2021
2022 if (ht == NULL)
2023 return;
2024
2025 todo = (long)ht->ht_used;
2026 for (hi = ht->ht_array; todo > 0; ++hi)
2027 {
2028 if (!HASHITEM_EMPTY(hi))
2029 {
2030 proptype_T *prop = HI2PT(hi);
2031
2032 vim_free(prop);
2033 --todo;
2034 }
2035 }
2036
2037 hash_clear(ht);
2038 vim_free(ht);
2039}
2040
2041#if defined(EXITFREE) || defined(PROTO)
2042/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002043 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002044 */
2045 void
2046clear_global_prop_types(void)
2047{
2048 clear_ht_prop_types(global_proptypes);
2049 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002050 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002051}
2052#endif
2053
2054/*
2055 * Free all property types for "buf".
2056 */
2057 void
2058clear_buf_prop_types(buf_T *buf)
2059{
2060 clear_ht_prop_types(buf->b_proptypes);
2061 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002062 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002063}
2064
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002065// Struct used to return two values from adjust_prop().
2066typedef struct
2067{
2068 int dirty; // if the property was changed
2069 int can_drop; // whether after this change, the prop may be removed
2070} adjustres_T;
2071
2072/*
2073 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2074 *
2075 * Note that "col" is zero-based, while tp_col is one-based.
2076 * Only for the current buffer.
2077 * "flags" can have:
2078 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002079 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002080 */
2081 static adjustres_T
2082adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002083 textprop_T *prop,
2084 colnr_T col,
2085 int added,
2086 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002087{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002088 proptype_T *pt;
2089 int start_incl;
2090 int end_incl;
2091 int droppable;
2092 adjustres_T res = {TRUE, FALSE};
2093
2094 // prop after end of the line doesn't move
2095 if (prop->tp_col == MAXCOL)
2096 {
2097 res.dirty = FALSE;
2098 return res;
2099 }
2100
2101 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2102 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002103 || (flags & APC_SUBSTITUTE)
2104 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002105 if (prop->tp_id < 0 && (flags & APC_INDENT))
2106 // when inserting indent just before a character with virtual text
2107 // shift the text property
2108 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002109 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002110 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002111 // do not drop zero-width props if they later can increase in size
2112 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002113
2114 if (added > 0)
2115 {
2116 if (col + 1 <= prop->tp_col
2117 - (start_incl || (prop->tp_len == 0 && end_incl)))
2118 // Change is entirely before the text property: Only shift
2119 prop->tp_col += added;
2120 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2121 // Insertion was inside text property
2122 prop->tp_len += added;
2123 }
2124 else if (prop->tp_col > col + 1)
2125 {
2126 if (prop->tp_col + added < col + 1)
2127 {
2128 prop->tp_len += (prop->tp_col - 1 - col) + added;
2129 prop->tp_col = col + 1;
2130 if (prop->tp_len <= 0)
2131 {
2132 prop->tp_len = 0;
2133 res.can_drop = droppable;
2134 }
2135 }
2136 else
2137 prop->tp_col += added;
2138 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002139 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2140 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002141 {
2142 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2143
2144 prop->tp_len += after > 0 ? added + after : added;
2145 res.can_drop = prop->tp_len <= 0 && droppable;
2146 }
2147 else
2148 res.dirty = FALSE;
2149
2150 return res;
2151}
2152
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002153/*
2154 * Adjust the columns of text properties in line "lnum" after position "col" to
2155 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002156 * Note that "col" is zero-based, while tp_col is one-based.
2157 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002158 * "flags" can have:
2159 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2160 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002161 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002162 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002163 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002164 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002165 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002166adjust_prop_columns(
2167 linenr_T lnum,
2168 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002169 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002170 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002171{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002172 int proplen;
2173 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002174 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002175 int ri, wi;
2176 size_t textlen;
2177
2178 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002179 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002180
2181 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2182 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002183 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002184 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002185
Bram Moolenaar196d1572019-01-02 23:47:18 +01002186 wi = 0; // write index
2187 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002188 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002189 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002190 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002191
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002192 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2193 res = adjust_prop(&prop, col, bytes_added, flags);
2194 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002195 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002196 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002197 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2198 && u_savesub(lnum) == FAIL)
2199 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002200 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002201
2202 // u_savesub() may have updated curbuf->b_ml, fetch it again
2203 if (curbuf->b_ml.ml_line_lnum != lnum)
2204 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002205 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002206 if (res.can_drop)
2207 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002208 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002209 ++wi;
2210 }
2211 if (dirty)
2212 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002213 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2214
2215 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002216 {
2217 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2218
2219 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2220 vim_free(curbuf->b_ml.ml_line_ptr);
2221 curbuf->b_ml.ml_line_ptr = p;
2222 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002223 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002224 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002225 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002226 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002227}
2228
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002229/*
2230 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002231 * "lnum_props" is the line that has the properties from before the split.
2232 * "lnum_top" is the top line.
2233 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002234 * "deleted" is the number of bytes deleted.
2235 */
2236 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002237adjust_props_for_split(
2238 linenr_T lnum_props,
2239 linenr_T lnum_top,
2240 int kept,
2241 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002242{
2243 char_u *props;
2244 int count;
2245 garray_T prevprop;
2246 garray_T nextprop;
2247 int i;
2248 int skipped = kept + deleted;
2249
2250 if (!curbuf->b_has_textprop)
2251 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002252
2253 // Get the text properties from "lnum_props".
2254 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002255 ga_init2(&prevprop, sizeof(textprop_T), 10);
2256 ga_init2(&nextprop, sizeof(textprop_T), 10);
2257
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002258 // Keep the relevant ones in the first line, reducing the length if needed.
2259 // Copy the ones that include the split to the second line.
2260 // Move the ones after the split to the second line.
2261 for (i = 0; i < count; ++i)
2262 {
2263 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002264 proptype_T *pt;
2265 int start_incl, end_incl;
2266 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002267 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002268
2269 // copy the prop to an aligned structure
2270 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2271
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002272 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2273 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2274 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002275
2276 // a text prop "above" behaves like it is on the first text column
2277 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2278
2279 cont_prev = prop_col != MAXCOL && prop_col + !start_incl <= kept;
2280 cont_next = prop_col != MAXCOL
2281 && skipped <= prop_col + prop.tp_len - !end_incl;
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002282 // when a prop has text it is never copied
2283 if (prop.tp_id < 0 && cont_next)
2284 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002285
2286 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002287 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002288 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2289
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002290 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002291 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002292 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002293 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002294 if (cont_next)
2295 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002296 }
2297
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002298 // Only add the property to the next line if the length is bigger than
2299 // zero.
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002300 if ((cont_next || prop_col == MAXCOL) && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002301 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002302 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002303
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002304 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002305 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002306 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002307 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002308 if (p->tp_col > skipped)
2309 p->tp_col -= skipped - 1;
2310 else
2311 {
2312 p->tp_len -= skipped - p->tp_col;
2313 p->tp_col = 1;
2314 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002315 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002316 if (cont_prev)
2317 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002318 }
2319 }
2320
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002321 set_text_props(lnum_top, prevprop.ga_data,
2322 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002323 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002324 set_text_props(lnum_top + 1, nextprop.ga_data,
2325 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002326 ga_clear(&nextprop);
2327}
2328
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002329/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002330 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002331 */
2332 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002333prepend_joined_props(
2334 char_u *new_props,
2335 int propcount,
2336 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002337 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002338 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002339 long col,
2340 int removed)
2341{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002342 char_u *props;
2343 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2344 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002345
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002346 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002347 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002348 textprop_T prop;
2349 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002350
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002351 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002352 if (prop.tp_col == MAXCOL && !last_line)
2353 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002354 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2355
2356 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002357 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002358
Bram Moolenaare175dc62022-08-01 22:18:50 +01002359 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002360 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2361 &prop, sizeof(prop));
2362 else
2363 {
2364 int j;
2365 int found = FALSE;
2366
2367 // Search for continuing prop.
2368 for (j = *props_remaining; j < propcount; ++j)
2369 {
2370 textprop_T op;
2371
2372 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2373 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2374 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002375 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002376 found = TRUE;
2377 op.tp_len += op.tp_col - prop.tp_col;
2378 op.tp_col = prop.tp_col;
2379 // Start/end is taken care of when deleting joined lines
2380 op.tp_flags = prop.tp_flags;
2381 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2382 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002383 }
2384 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002385 if (!found)
2386 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002387 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002388 }
2389}
2390
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002391#endif // FEAT_PROP_POPUP