blob: c1fc34e0ea8d3eee8e98b38989813a034ef89861 [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;
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100356 int prev_did_emsg = did_emsg;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200357
358 if (check_for_dict_arg(argvars, 0) == FAIL
359 || check_for_list_arg(argvars, 1) == FAIL)
360 return;
361
Bram Moolenaard83392a2022-09-01 12:22:46 +0100362 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364
365 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100366 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200367 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000368 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200369 return;
370 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100371 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200372
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100373 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100374 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200375
376 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
377 return;
378
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100379 // This must be done _before_ we start adding properties because property
380 // changes trigger buffer (memline) reorganisation, which needs this flag
381 // to be correctly set.
382 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200383 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
384 {
385 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
386 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000387 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200388 return;
389 }
390
391 pos_list = li->li_tv.vval.v_list;
392 start_lnum = list_find_nr(pos_list, 0L, &error);
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100393 if (!error)
394 start_col = list_find_nr(pos_list, 1L, &error);
395 if (!error)
396 end_lnum = list_find_nr(pos_list, 2L, &error);
397 if (!error)
398 end_col = list_find_nr(pos_list, 3L, &error);
Bram Moolenaard93009e2022-10-13 14:35:24 +0100399 int this_id = id;
400 if (!error && pos_list->lv_len > 4)
401 this_id = list_find_nr(pos_list, 4L, &error);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200402 if (error || start_lnum <= 0 || start_col <= 0
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100403 || end_lnum <= 0 || end_col <= 0)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200404 {
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100405 if (prev_did_emsg == did_emsg)
406 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200407 return;
408 }
Bram Moolenaard93009e2022-10-13 14:35:24 +0100409 if (prop_add_one(buf, type_name, this_id, NULL, 0, 0,
410 start_lnum, end_lnum, start_col, end_col) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200411 return;
412 }
413
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100414 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200415}
416
417/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100418 * Get the next ID to use for a textprop with text in buffer "buf".
419 */
420 static int
421get_textprop_id(buf_T *buf)
422{
423 // TODO: recycle deleted entries
424 return -(buf->b_textprop_text.ga_len + 1);
425}
426
427/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200428 * Shared between prop_add() and popup_create().
429 * "dict_arg" is the function argument of a dict containing "bufnr".
430 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100431 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200432 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100433 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200434prop_add_common(
435 linenr_T start_lnum,
436 colnr_T start_col,
437 dict_T *dict,
438 buf_T *default_buf,
439 typval_T *dict_arg)
440{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200441 linenr_T end_lnum;
442 colnr_T end_col;
443 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200444 buf_T *buf = default_buf;
445 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100446 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100447 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100448 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100449
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100450 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100451 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000452 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100453 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100454 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100455 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100456
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100457 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100458 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100459 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100460 if (end_lnum < start_lnum)
461 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000462 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100463 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100465 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100466 else
467 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100468
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100469 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100470 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100471 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100472
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100473 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100474 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000475 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100476 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100477 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100478 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100479 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100480 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100481 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100482 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100483 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100484 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000485 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100486 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100487 }
488 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100489 else if (start_lnum == end_lnum)
490 end_col = start_col;
491 else
492 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100493
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100494 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100495 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100496
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100497 if (dict_has_key(dict, "text"))
498 {
Bram Moolenaarfb593c52022-09-17 18:57:36 +0100499 if (dict_has_key(dict, "length")
500 || dict_has_key(dict, "end_col")
501 || dict_has_key(dict, "end_lnum"))
502 {
503 emsg(_(e_cannot_use_length_endcol_and_endlnum_with_text));
504 goto theend;
505 }
506
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100507 text = dict_get_string(dict, "text", TRUE);
508 if (text == NULL)
509 goto theend;
510 // use a default length of 1 to make multiple props show up
511 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100512
513 if (dict_has_key(dict, "text_align"))
514 {
515 char_u *p = dict_get_string(dict, "text_align", FALSE);
516
517 if (p == NULL)
518 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100519 if (start_col != 0)
520 {
521 emsg(_(e_can_only_use_text_align_when_column_is_zero));
522 goto theend;
523 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100524 if (STRCMP(p, "right") == 0)
525 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100526 else if (STRCMP(p, "above") == 0)
527 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100528 else if (STRCMP(p, "below") == 0)
529 flags |= TP_FLAG_ALIGN_BELOW;
530 else if (STRCMP(p, "after") != 0)
531 {
532 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
533 goto theend;
534 }
535 }
536
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100537 if (dict_has_key(dict, "text_padding_left"))
538 {
539 text_padding_left = dict_get_number(dict, "text_padding_left");
540 if (text_padding_left < 0)
541 {
542 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
543 goto theend;
544 }
545 }
546
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100547 if (dict_has_key(dict, "text_wrap"))
548 {
549 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100550
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100551 if (p == NULL)
552 goto theend;
553 if (STRCMP(p, "wrap") == 0)
554 flags |= TP_FLAG_WRAP;
555 else if (STRCMP(p, "truncate") != 0)
556 {
557 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
558 goto theend;
559 }
560 }
561 }
562
563 // Column must be 1 or more for a normal text property; when "text" is
564 // present zero means it goes after the line.
565 if (start_col < (text == NULL ? 1 : 0))
566 {
567 semsg(_(e_invalid_column_number_nr), (long)start_col);
568 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100569 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100570 if (start_col > 0 && text_padding_left > 0)
571 {
572 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
573 goto theend;
574 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100575
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200576 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100577 goto theend;
578
579 if (id < 0 && buf->b_textprop_text.ga_len > 0)
580 {
581 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
582 goto theend;
583 }
584 if (text != NULL)
585 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100586
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100587 // This must be done _before_ we add the property because property changes
588 // trigger buffer (memline) reorganisation, which needs this flag to be
589 // correctly set.
590 buf->b_has_textprop = TRUE; // this is never reset
591
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100592 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100593 start_lnum, end_lnum, start_col, end_col);
594 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100595
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100596 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100597
598theend:
599 vim_free(text);
600 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100601}
602
603/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100604 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100605 * Returns the number of text properties and, when non-zero, a pointer to the
606 * first one in "props" (note that it is not aligned, therefore the char_u
607 * pointer).
608 */
609 int
610get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
611{
612 char_u *text;
613 size_t textlen;
614 size_t proplen;
615
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100616 // Be quick when no text property types have been defined or the buffer,
617 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200618 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100619 return 0;
620
621 // Fetch the line to get the ml_line_len field updated.
622 text = ml_get_buf(buf, lnum, will_change);
623 textlen = STRLEN(text) + 1;
624 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100625 if (proplen == 0)
626 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100627 if (proplen % sizeof(textprop_T) != 0)
628 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000629 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100630 return 0;
631 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100632 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100633 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100634}
635
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100636/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100637 * Return the number of text properties with "above" or "below" alignment in
638 * line "lnum". A "right" aligned property also goes below after a "below" or
639 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100640 */
641 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100642prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100643{
644 char_u *props;
645 int count = get_text_props(buf, lnum, &props, FALSE);
646 int result = 0;
647 textprop_T prop;
648 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100649 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100650
651 if (count == 0)
652 return 0;
653 for (i = 0; i < count; ++i)
654 {
655 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar89469d12022-12-02 20:46:26 +0000656 if (prop.tp_col == MAXCOL && text_prop_type_valid(buf, &prop))
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100657 {
658 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
659 || (next_right_goes_below
660 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
661 {
662 next_right_goes_below = TRUE;
663 ++result;
664 }
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100665 else if (prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
666 {
667 next_right_goes_below = FALSE;
668 ++result;
669 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100670 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
671 next_right_goes_below = TRUE;
672 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100673 }
674 return result;
675}
676
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200677/**
678 * Return the number of text properties on line "lnum" in the current buffer.
679 * When "only_starting" is true only text properties starting in this line will
680 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100681 * When "last_line" is FALSE then text properties after the line are not
682 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200683 */
684 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100685count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200686{
687 char_u *props;
688 int proplen = get_text_props(curbuf, lnum, &props, 0);
689 int result = proplen;
690 int i;
691 textprop_T prop;
692
Bram Moolenaare175dc62022-08-01 22:18:50 +0100693 for (i = 0; i < proplen; ++i)
694 {
695 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100696 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100697 // previous line, or when not in the last line and it is virtual text
698 // after the line.
699 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
Bram Moolenaar89469d12022-12-02 20:46:26 +0000700 || (!last_line && prop.tp_col == MAXCOL)
701 || !text_prop_type_valid(curbuf, &prop))
Bram Moolenaare175dc62022-08-01 22:18:50 +0100702 --result;
703 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200704 return result;
705}
706
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100707static textprop_T *text_prop_compare_props;
708static buf_T *text_prop_compare_buf;
709
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100710/* Score for sorting on position of the text property: 0: above,
711 * 1: after (default), 2: right, 3: below (comes last)
712 */
713 static int
714text_prop_order(int flags)
715{
716 if (flags & TP_FLAG_ALIGN_ABOVE)
717 return 0;
718 if (flags & TP_FLAG_ALIGN_RIGHT)
719 return 2;
720 if (flags & TP_FLAG_ALIGN_BELOW)
721 return 3;
722 return 1;
723}
724
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100725/*
726 * Function passed to qsort() to sort text properties.
727 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
728 * both have the same priority.
729 */
730 static int
731text_prop_compare(const void *s1, const void *s2)
732{
733 int idx1, idx2;
734 textprop_T *tp1, *tp2;
735 proptype_T *pt1, *pt2;
736 colnr_T col1, col2;
737
738 idx1 = *(int *)s1;
739 idx2 = *(int *)s2;
740 tp1 = &text_prop_compare_props[idx1];
741 tp2 = &text_prop_compare_props[idx2];
742 col1 = tp1->tp_col;
743 col2 = tp2->tp_col;
744 if (col1 == MAXCOL && col2 == MAXCOL)
745 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100746 int order1 = text_prop_order(tp1->tp_flags);
747 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100748
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100749 // both props add text before or after the line, sort on order where it
750 // is added
751 if (order1 != order2)
752 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100753 }
754
755 // property that inserts text has priority over one that doesn't
756 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
757 return tp1->tp_id < 0 ? 1 : -1;
758
759 // check highest priority, defined by the type
760 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
761 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
762 if (pt1 != pt2)
763 {
764 if (pt1 == NULL)
765 return -1;
766 if (pt2 == NULL)
767 return 1;
768 if (pt1->pt_priority != pt2->pt_priority)
769 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
770 }
771
772 // same priority, one that starts first wins
773 if (col1 != col2)
774 return col1 < col2 ? 1 : -1;
775
776 // for a property with text the id can be used as tie breaker
777 if (tp1->tp_id < 0)
778 return tp1->tp_id > tp2->tp_id ? 1 : -1;
779
780 return 0;
781}
782
783/*
784 * Sort "count" text properties using an array if indexes "idxs" into the list
785 * of text props "props" for buffer "buf".
786 */
787 void
788sort_text_props(
789 buf_T *buf,
790 textprop_T *props,
791 int *idxs,
792 int count)
793{
794 text_prop_compare_buf = buf;
795 text_prop_compare_props = props;
796 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
797}
798
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100799/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200800 * Find text property "type_id" in the visible lines of window "wp".
801 * Match "id" when it is > 0.
802 * Returns FAIL when not found.
803 */
804 int
Bram Moolenaar89469d12022-12-02 20:46:26 +0000805find_visible_prop(
806 win_T *wp,
807 int type_id,
808 int id,
809 textprop_T *prop,
810 linenr_T *found_lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200811{
Bram Moolenaar89469d12022-12-02 20:46:26 +0000812 // return when "type_id" no longer exists
813 if (text_prop_type_by_id(wp->w_buffer, type_id) == NULL)
814 return FAIL;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200815
816 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100817 validate_botline_win(wp);
Bram Moolenaar89469d12022-12-02 20:46:26 +0000818 for (linenr_T lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200819 {
Bram Moolenaar89469d12022-12-02 20:46:26 +0000820 char_u *props;
821 int count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
822 for (int i = 0; i < count; ++i)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200823 {
824 mch_memmove(prop, props + i * sizeof(textprop_T),
825 sizeof(textprop_T));
826 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
827 {
828 *found_lnum = lnum;
829 return OK;
830 }
831 }
832 }
833 return FAIL;
834}
835
836/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100837 * Set the text properties for line "lnum" to "props" with length "len".
838 * If "len" is zero text properties are removed, "props" is not used.
839 * Any existing text properties are dropped.
840 * Only works for the current buffer.
841 */
842 static void
843set_text_props(linenr_T lnum, char_u *props, int len)
844{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100845 char_u *text;
846 char_u *newtext;
847 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100848
849 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100850 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100851 newtext = alloc(textlen + len);
852 if (newtext == NULL)
853 return;
854 mch_memmove(newtext, text, textlen);
855 if (len > 0)
856 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100857 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100858 vim_free(curbuf->b_ml.ml_line_ptr);
859 curbuf->b_ml.ml_line_ptr = newtext;
860 curbuf->b_ml.ml_line_len = textlen + len;
861 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
862}
863
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100864/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100865 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100866 */
867 void
868add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
869{
870 char_u *text;
871 char_u *newtext;
872 int proplen = text_prop_count * (int)sizeof(textprop_T);
873
874 text = ml_get(lnum);
875 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
876 if (newtext == NULL)
877 return;
878 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
879 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
880 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
881 vim_free(curbuf->b_ml.ml_line_ptr);
882 curbuf->b_ml.ml_line_ptr = newtext;
883 curbuf->b_ml.ml_line_len += proplen;
884 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
885}
886
Bram Moolenaare44336b2022-08-07 18:20:08 +0100887/*
888 * Function passed to qsort() for sorting proptype_T on pt_id.
889 */
890 static int
891compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100892{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100893 proptype_T *tp1 = *(proptype_T **)s1;
894 proptype_T *tp2 = *(proptype_T **)s2;
895
896 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
897}
898
899 static proptype_T *
900find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
901{
902 int low = 0;
903 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100904
Bram Moolenaar10246902022-08-08 17:08:05 +0100905 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100906 return NULL;
907
dundargocc57b5bc2022-11-02 13:30:51 +0000908 // Make the lookup faster by creating an array with pointers to
Bram Moolenaare44336b2022-08-07 18:20:08 +0100909 // hashtable entries, sorted on pt_id.
910 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100911 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100912 long todo;
913 hashitem_T *hi;
914 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100915
Bram Moolenaare44336b2022-08-07 18:20:08 +0100916 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
917 if (*array == NULL)
918 return NULL;
919 todo = (long)ht->ht_used;
920 for (hi = ht->ht_array; todo > 0; ++hi)
921 {
922 if (!HASHITEM_EMPTY(hi))
923 {
924 (*array)[i++] = HI2PT(hi);
925 --todo;
926 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100927 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100928 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
929 }
930
931 // binary search in the sorted array
932 high = ht->ht_used;
933 while (high > low)
934 {
935 int m = (high + low) / 2;
936
937 if ((*array)[m]->pt_id == id)
938 return (*array)[m];
939 if ((*array)[m]->pt_id > id)
940 high = m;
941 else
942 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100943 }
944 return NULL;
945}
946
947/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100948 * Fill 'dict' with text properties in 'prop'.
949 */
950 static void
951prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
952{
953 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200954 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100955
956 dict_add_number(dict, "col", prop->tp_col);
957 dict_add_number(dict, "length", prop->tp_len);
958 dict_add_number(dict, "id", prop->tp_id);
959 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
960 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200961
Bram Moolenaare44336b2022-08-07 18:20:08 +0100962 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200963 if (pt == NULL)
964 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100965 pt = find_type_by_id(global_proptypes, &global_proparray,
966 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200967 buflocal = FALSE;
968 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100969 if (pt != NULL)
970 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200971
972 if (buflocal)
973 dict_add_number(dict, "type_bufnr", buf->b_fnum);
974 else
975 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100976}
977
978/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100979 * Find a property type by ID in "buf" or globally.
980 * Returns NULL if not found.
981 */
982 proptype_T *
983text_prop_type_by_id(buf_T *buf, int id)
984{
985 proptype_T *type;
986
Bram Moolenaare44336b2022-08-07 18:20:08 +0100987 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100988 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100989 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100990 return type;
991}
992
993/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000994 * Return TRUE if "prop" is a valid text property type.
995 */
996 int
997text_prop_type_valid(buf_T *buf, textprop_T *prop)
998{
999 return text_prop_type_by_id(buf, prop->tp_type) != NULL;
1000}
1001
1002/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001003 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
1004 */
1005 void
1006f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
1007{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001008 linenr_T start;
1009 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001010 linenr_T lnum;
1011 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001012 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001013
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001014 if (in_vim9script()
1015 && (check_for_number_arg(argvars, 0) == FAIL
1016 || check_for_opt_number_arg(argvars, 1) == FAIL
1017 || (argvars[1].v_type != VAR_UNKNOWN
1018 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1019 return;
1020
1021 start = tv_get_number(&argvars[0]);
1022 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001023 if (argvars[1].v_type != VAR_UNKNOWN)
1024 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001025 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001026 if (argvars[2].v_type != VAR_UNKNOWN)
1027 {
1028 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
1029 return;
1030 }
1031 }
1032 if (start < 1 || end < 1)
1033 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001034 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001035 return;
1036 }
1037
1038 for (lnum = start; lnum <= end; ++lnum)
1039 {
1040 char_u *text;
1041 size_t len;
1042
1043 if (lnum > buf->b_ml.ml_line_count)
1044 break;
1045 text = ml_get_buf(buf, lnum, FALSE);
1046 len = STRLEN(text) + 1;
1047 if ((size_t)buf->b_ml.ml_line_len > len)
1048 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001049 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001050 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1051 {
1052 char_u *newtext = vim_strsave(text);
1053
1054 // need to allocate the line now
1055 if (newtext == NULL)
1056 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001057 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1058 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001059 buf->b_ml.ml_line_ptr = newtext;
1060 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1061 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001062 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001063 }
1064 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001065 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001066 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001067}
1068
1069/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001070 * prop_find({props} [, {direction}])
1071 */
1072 void
1073f_prop_find(typval_T *argvars, typval_T *rettv)
1074{
1075 pos_T *cursor = &curwin->w_cursor;
1076 dict_T *dict;
1077 buf_T *buf = curbuf;
1078 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001079 int lnum_start;
1080 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001081 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001082 int id = 0;
1083 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001084 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001085 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001086 int lnum = -1;
1087 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001088 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001089 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001090
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001091 if (in_vim9script()
1092 && (check_for_dict_arg(argvars, 0) == FAIL
1093 || check_for_opt_string_arg(argvars, 1) == FAIL))
1094 return;
1095
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001096 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001097 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001098 dict = argvars[0].vval.v_dict;
1099
1100 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1101 return;
1102 if (buf->b_ml.ml_mfp == NULL)
1103 return;
1104
1105 if (argvars[1].v_type != VAR_UNKNOWN)
1106 {
1107 char_u *dir_s = tv_get_string(&argvars[1]);
1108
1109 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001110 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001111 else if (*dir_s != 'f')
1112 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001113 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001114 return;
1115 }
1116 }
1117
1118 di = dict_find(dict, (char_u *)"lnum", -1);
1119 if (di != NULL)
1120 lnum = tv_get_number(&di->di_tv);
1121
1122 di = dict_find(dict, (char_u *)"col", -1);
1123 if (di != NULL)
1124 col = tv_get_number(&di->di_tv);
1125
1126 if (lnum == -1)
1127 {
1128 lnum = cursor->lnum;
1129 col = cursor->col + 1;
1130 }
1131 else if (col == -1)
1132 col = 1;
1133
1134 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1135 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001136 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001137 return;
1138 }
1139
Bram Moolenaard61efa52022-07-23 09:52:04 +01001140 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001141
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001142 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001143 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001144 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001145 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001146 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001147 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001148 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001149 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001150 proptype_T *type = lookup_prop_type(name, buf);
1151
1152 if (type == NULL)
1153 return;
1154 type_id = type->pt_id;
1155 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001156 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001157 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001158 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001159 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001160 return;
1161 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001162 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001163 {
Ben Jacksona7704222022-08-20 20:54:51 +01001164 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001165 return;
1166 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001167
1168 lnum_start = lnum;
1169
1170 if (rettv_dict_alloc(rettv) == FAIL)
1171 return;
1172
1173 while (1)
1174 {
1175 char_u *text = ml_get_buf(buf, lnum, FALSE);
1176 size_t textlen = STRLEN(text) + 1;
1177 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001178 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001179 int i;
1180 textprop_T prop;
1181 int prop_start;
1182 int prop_end;
1183
LemonBoy9bd3ce22022-04-18 21:54:02 +01001184 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001185 {
1186 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001187 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001188
LemonBoy9bd3ce22022-04-18 21:54:02 +01001189 // For the very first line try to find the first property before or
1190 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001191 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001192 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001193 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001194 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001195 if (prop.tp_col > col)
1196 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001197 }
1198 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1199 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001200 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001201 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001202 : (id_found && prop.tp_id == id)
1203 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001204 {
1205 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001206 if (lnum_start == lnum
1207 && col >= prop.tp_col
1208 && (col <= prop.tp_col + prop.tp_len
1209 - (prop.tp_len != 0)))
1210 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001211
LemonBoy9bd3ce22022-04-18 21:54:02 +01001212 // The property was not continued from last line, it starts on
1213 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001214 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001215 // The property does not continue on the next line, it ends on
1216 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001217 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001218 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001219 seen_end = 1;
1220
1221 // Skip lines without the start flag.
1222 if (!prop_start)
1223 {
1224 // Always search backwards for start when search started
1225 // on a prop and we're not skipping.
1226 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001227 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001228 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001229 }
1230
1231 // If skipstart is true, skip the prop at start pos (even if
1232 // continued from another line).
1233 if (start_pos_has_prop && skipstart && !seen_end)
1234 {
1235 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001236 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001237 }
1238
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001239 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1240 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1241
1242 return;
1243 }
1244 }
1245
1246 if (dir > 0)
1247 {
1248 if (lnum >= buf->b_ml.ml_line_count)
1249 break;
1250 lnum++;
1251 }
1252 else
1253 {
1254 if (lnum <= 1)
1255 break;
1256 lnum--;
1257 }
1258 }
1259}
1260
1261/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001262 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1263 */
1264 static int
1265prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1266{
1267 int i;
1268
1269 for (i = 0; i < len; i++)
1270 if (types_or_ids[i] == type_or_id)
1271 return TRUE;
1272
1273 return FALSE;
1274}
1275
1276/*
1277 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1278 * If 'prop_types' is not NULL, then return only the text properties with
1279 * matching property type in the 'prop_types' array.
1280 * If 'prop_ids' is not NULL, then return only the text properties with
1281 * an identifier in the 'props_ids' array.
1282 * If 'add_lnum' is TRUE, then add the line number also to the text property
1283 * dictionary.
1284 */
1285 static void
1286get_props_in_line(
1287 buf_T *buf,
1288 linenr_T lnum,
1289 int *prop_types,
1290 int prop_types_len,
1291 int *prop_ids,
1292 int prop_ids_len,
1293 list_T *retlist,
1294 int add_lnum)
1295{
1296 char_u *text = ml_get_buf(buf, lnum, FALSE);
1297 size_t textlen = STRLEN(text) + 1;
1298 int count;
1299 int i;
1300 textprop_T prop;
1301
1302 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1303 for (i = 0; i < count; ++i)
1304 {
1305 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1306 sizeof(textprop_T));
1307 if ((prop_types == NULL
1308 || prop_type_or_id_in_list(prop_types, prop_types_len,
1309 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001310 && (prop_ids == NULL
1311 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1312 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001313 {
1314 dict_T *d = dict_alloc();
1315
1316 if (d == NULL)
1317 break;
1318 prop_fill_dict(d, &prop, buf);
1319 if (add_lnum)
1320 dict_add_number(d, "lnum", lnum);
1321 list_append_dict(retlist, d);
1322 }
1323 }
1324}
1325
1326/*
1327 * Convert a List of property type names into an array of property type
1328 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1329 * error. 'num_types' is set to the number of returned property types.
1330 */
1331 static int *
1332get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1333{
1334 int *prop_types;
1335 listitem_T *li;
1336 int i;
1337 char_u *name;
1338 proptype_T *type;
1339
1340 *num_types = 0;
1341
1342 prop_types = ALLOC_MULT(int, list_len(l));
1343 if (prop_types == NULL)
1344 return NULL;
1345
1346 i = 0;
1347 FOR_ALL_LIST_ITEMS(l, li)
1348 {
1349 if (li->li_tv.v_type != VAR_STRING)
1350 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001351 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001352 goto errret;
1353 }
1354 name = li->li_tv.vval.v_string;
1355 if (name == NULL)
1356 goto errret;
1357
1358 type = lookup_prop_type(name, buf);
1359 if (type == NULL)
1360 goto errret;
1361 prop_types[i++] = type->pt_id;
1362 }
1363
1364 *num_types = i;
1365 return prop_types;
1366
1367errret:
1368 VIM_CLEAR(prop_types);
1369 return NULL;
1370}
1371
1372/*
1373 * Convert a List of property identifiers into an array of property
1374 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1375 * error. 'num_ids' is set to the number of returned property identifiers.
1376 */
1377 static int *
1378get_prop_ids_from_list(list_T *l, int *num_ids)
1379{
1380 int *prop_ids;
1381 listitem_T *li;
1382 int i;
1383 int id;
1384 int error;
1385
1386 *num_ids = 0;
1387
1388 prop_ids = ALLOC_MULT(int, list_len(l));
1389 if (prop_ids == NULL)
1390 return NULL;
1391
1392 i = 0;
1393 FOR_ALL_LIST_ITEMS(l, li)
1394 {
1395 error = FALSE;
1396 id = tv_get_number_chk(&li->li_tv, &error);
1397 if (error)
1398 goto errret;
1399
1400 prop_ids[i++] = id;
1401 }
1402
1403 *num_ids = i;
1404 return prop_ids;
1405
1406errret:
1407 VIM_CLEAR(prop_ids);
1408 return NULL;
1409}
1410
1411/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001412 * prop_list({lnum} [, {bufnr}])
1413 */
1414 void
1415f_prop_list(typval_T *argvars, typval_T *rettv)
1416{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001417 linenr_T lnum;
1418 linenr_T start_lnum;
1419 linenr_T end_lnum;
1420 buf_T *buf = curbuf;
1421 int add_lnum = FALSE;
1422 int *prop_types = NULL;
1423 int prop_types_len = 0;
1424 int *prop_ids = NULL;
1425 int prop_ids_len = 0;
1426 list_T *l;
1427 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001428
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001429 if (in_vim9script()
1430 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001431 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001432 return;
1433
Bram Moolenaar93a10962022-06-16 11:42:09 +01001434 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001435 return;
1436
1437 // default: get text properties on current line
1438 start_lnum = tv_get_number(&argvars[0]);
1439 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001440 if (argvars[1].v_type != VAR_UNKNOWN)
1441 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001442 dict_T *d;
1443
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001444 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001445 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001446 d = argvars[1].vval.v_dict;
1447
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001448 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1449 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001450
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001451 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001452 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001453 if (di->di_tv.v_type != VAR_NUMBER)
1454 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001455 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001456 return;
1457 }
1458 end_lnum = tv_get_number(&di->di_tv);
1459 if (end_lnum < 0)
1460 // negative end_lnum is used as an offset from the last buffer
1461 // line
1462 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1463 else if (end_lnum > buf->b_ml.ml_line_count)
1464 end_lnum = buf->b_ml.ml_line_count;
1465 add_lnum = TRUE;
1466 }
1467 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1468 {
1469 if (di->di_tv.v_type != VAR_LIST)
1470 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001471 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001472 return;
1473 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001474
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001475 l = di->di_tv.vval.v_list;
1476 if (l != NULL && list_len(l) > 0)
1477 {
1478 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1479 if (prop_types == NULL)
1480 return;
1481 }
1482 }
1483 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1484 {
1485 if (di->di_tv.v_type != VAR_LIST)
1486 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001487 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001488 goto errret;
1489 }
1490
1491 l = di->di_tv.vval.v_list;
1492 if (l != NULL && list_len(l) > 0)
1493 {
1494 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1495 if (prop_ids == NULL)
1496 goto errret;
1497 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001498 }
1499 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001500 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1501 || end_lnum < 1 || end_lnum < start_lnum)
1502 emsg(_(e_invalid_range));
1503 else
1504 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1505 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1506 prop_ids, prop_ids_len,
1507 rettv->vval.v_list, add_lnum);
1508
1509errret:
1510 VIM_CLEAR(prop_types);
1511 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001512}
1513
1514/*
1515 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1516 */
1517 void
1518f_prop_remove(typval_T *argvars, typval_T *rettv)
1519{
1520 linenr_T start = 1;
1521 linenr_T end = 0;
1522 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001523 linenr_T first_changed = 0;
1524 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001525 dict_T *dict;
1526 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001527 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001528 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001529 int type_id = -1; // for a single "type"
1530 int *type_ids = NULL; // array, for a list of "types", allocated
1531 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001532 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001533 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001534
1535 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001536
1537 if (in_vim9script()
1538 && (check_for_dict_arg(argvars, 0) == FAIL
1539 || check_for_opt_number_arg(argvars, 1) == FAIL
1540 || (argvars[1].v_type != VAR_UNKNOWN
1541 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1542 return;
1543
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001544 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001545 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001546
1547 if (argvars[1].v_type != VAR_UNKNOWN)
1548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001549 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001550 end = start;
1551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001552 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001553 if (start < 1 || end < 1)
1554 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001555 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001556 return;
1557 }
1558 }
1559
1560 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001561 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1562 return;
1563 if (buf->b_ml.ml_mfp == NULL)
1564 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001565
Bram Moolenaard61efa52022-07-23 09:52:04 +01001566 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001567
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001568 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001569 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001570
1571 // if a specific type was supplied "type": check that (and ignore "types".
1572 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001573 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001574 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001575 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001576 proptype_T *type = lookup_prop_type(name, buf);
1577
1578 if (type == NULL)
1579 return;
1580 type_id = type->pt_id;
1581 }
Ben Jacksona7704222022-08-20 20:54:51 +01001582 if (dict_has_key(dict, "types"))
1583 {
1584 typval_T types;
1585 listitem_T *li = NULL;
1586
1587 dict_get_tv(dict, "types", &types);
1588 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1589 {
1590 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1591
1592 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1593 {
1594 proptype_T *prop_type;
1595
1596 if (li->li_tv.v_type != VAR_STRING)
1597 continue;
1598
1599 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1600
1601 if (!prop_type)
1602 goto cleanup_prop_remove;
1603
1604 type_ids[num_type_ids++] = prop_type->pt_id;
1605 }
1606 }
1607 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001608 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001609
Ben Jacksona7704222022-08-20 20:54:51 +01001610 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001611 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001612 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001613 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001614 }
Ben Jacksona7704222022-08-20 20:54:51 +01001615 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001616 {
Ben Jacksona7704222022-08-20 20:54:51 +01001617 emsg(_(e_need_id_and_type_or_types_with_both));
1618 goto cleanup_prop_remove;
1619 }
1620 if (type_id != -1 && num_type_ids > 0)
1621 {
1622 emsg(_(e_cannot_specify_both_type_and_types));
1623 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001624 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001625
1626 if (end == 0)
1627 end = buf->b_ml.ml_line_count;
1628 for (lnum = start; lnum <= end; ++lnum)
1629 {
1630 char_u *text;
1631 size_t len;
1632
1633 if (lnum > buf->b_ml.ml_line_count)
1634 break;
1635 text = ml_get_buf(buf, lnum, FALSE);
1636 len = STRLEN(text) + 1;
1637 if ((size_t)buf->b_ml.ml_line_len > len)
1638 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001639 static textprop_T textprop; // static because of alignment
1640 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001641
1642 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1643 / sizeof(textprop_T); ++idx)
1644 {
1645 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1646 + idx * sizeof(textprop_T);
1647 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001648 int matches_id = 0;
1649 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001650
1651 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001652
1653 matches_id = textprop.tp_id == id;
1654 if (num_type_ids > 0)
1655 {
1656 int idx2;
1657
1658 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1659 matches_type = textprop.tp_type == type_ids[idx2];
1660 }
1661 else
1662 {
1663 matches_type = textprop.tp_type == type_id;
1664 }
1665
1666 if (both ? matches_id && matches_type
1667 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001668 {
1669 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1670 {
1671 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1672
1673 // need to allocate the line to be able to change it
1674 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001675 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001676 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1677 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001678 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1679 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001680 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001681 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1682
1683 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001684 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001685 }
1686
1687 taillen = buf->b_ml.ml_line_len - len
1688 - (idx + 1) * sizeof(textprop_T);
1689 if (taillen > 0)
1690 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1691 taillen);
1692 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1693 --idx;
1694
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001695 if (textprop.tp_id < 0)
1696 {
1697 garray_T *gap = &buf->b_textprop_text;
1698 int ii = -textprop.tp_id - 1;
1699
1700 // negative ID: property with text - free the text
1701 if (ii < gap->ga_len)
1702 {
1703 char_u **p = ((char_u **)gap->ga_data) + ii;
1704 vim_free(*p);
1705 *p = NULL;
1706 did_remove_text = TRUE;
1707 }
1708 }
1709
Bram Moolenaar965c0442021-05-17 00:22:06 +02001710 if (first_changed == 0)
1711 first_changed = lnum;
1712 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001713 ++rettv->vval.v_number;
1714 if (!do_all)
1715 break;
1716 }
1717 }
1718 }
1719 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001720
Bram Moolenaar965c0442021-05-17 00:22:06 +02001721 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001722 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001723 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001724 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001725 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001726 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001727
1728 if (did_remove_text)
1729 {
1730 garray_T *gap = &buf->b_textprop_text;
1731
1732 // Reduce the growarray size for NULL pointers at the end.
1733 while (gap->ga_len > 0
1734 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1735 --gap->ga_len;
1736 }
Ben Jacksona7704222022-08-20 20:54:51 +01001737
1738cleanup_prop_remove:
1739 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001740}
1741
1742/*
1743 * Common for f_prop_type_add() and f_prop_type_change().
1744 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001745 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001746prop_type_set(typval_T *argvars, int add)
1747{
1748 char_u *name;
1749 buf_T *buf = NULL;
1750 dict_T *dict;
1751 dictitem_T *di;
1752 proptype_T *prop;
1753
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001754 if (in_vim9script()
1755 && (check_for_string_arg(argvars, 0) == FAIL
1756 || check_for_dict_arg(argvars, 1) == FAIL))
1757 return;
1758
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001759 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001760 if (*name == NUL)
1761 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001762 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001763 return;
1764 }
1765
1766 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1767 return;
1768 dict = argvars[1].vval.v_dict;
1769
Bram Moolenaare44336b2022-08-07 18:20:08 +01001770 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001771 if (add)
1772 {
1773 hashtab_T **htp;
1774
1775 if (prop != NULL)
1776 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001777 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001778 return;
1779 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001780 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001781 if (prop == NULL)
1782 return;
1783 STRCPY(prop->pt_name, name);
1784 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001785 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001786 if (buf == NULL)
1787 {
1788 htp = &global_proptypes;
1789 VIM_CLEAR(global_proparray);
1790 }
1791 else
1792 {
1793 htp = &buf->b_proptypes;
1794 VIM_CLEAR(buf->b_proparray);
1795 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001796 if (*htp == NULL)
1797 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001798 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001799 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001800 {
1801 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001802 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001803 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001804 hash_init(*htp);
1805 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001806 hash_add(*htp, PT2HIKEY(prop), "prop type");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001807 }
1808 else
1809 {
1810 if (prop == NULL)
1811 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001812 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001813 return;
1814 }
1815 }
1816
1817 if (dict != NULL)
1818 {
1819 di = dict_find(dict, (char_u *)"highlight", -1);
1820 if (di != NULL)
1821 {
1822 char_u *highlight;
1823 int hl_id = 0;
1824
Bram Moolenaard61efa52022-07-23 09:52:04 +01001825 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001826 if (highlight != NULL && *highlight != NUL)
1827 hl_id = syn_name2id(highlight);
1828 if (hl_id <= 0)
1829 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001830 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001831 highlight == NULL ? (char_u *)"" : highlight);
1832 return;
1833 }
1834 prop->pt_hl_id = hl_id;
1835 }
1836
Bram Moolenaar58187f12019-05-05 16:33:47 +02001837 di = dict_find(dict, (char_u *)"combine", -1);
1838 if (di != NULL)
1839 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001840 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001841 prop->pt_flags |= PT_FLAG_COMBINE;
1842 else
1843 prop->pt_flags &= ~PT_FLAG_COMBINE;
1844 }
1845
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001846 di = dict_find(dict, (char_u *)"override", -1);
1847 if (di != NULL)
1848 {
1849 if (tv_get_bool(&di->di_tv))
1850 prop->pt_flags |= PT_FLAG_OVERRIDE;
1851 else
1852 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1853 }
1854
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001855 di = dict_find(dict, (char_u *)"priority", -1);
1856 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001857 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001858
1859 di = dict_find(dict, (char_u *)"start_incl", -1);
1860 if (di != NULL)
1861 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001862 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001863 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1864 else
1865 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1866 }
1867
1868 di = dict_find(dict, (char_u *)"end_incl", -1);
1869 if (di != NULL)
1870 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001871 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001872 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1873 else
1874 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1875 }
1876 }
1877}
1878
1879/*
1880 * prop_type_add({name}, {props})
1881 */
1882 void
1883f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1884{
1885 prop_type_set(argvars, TRUE);
1886}
1887
1888/*
1889 * prop_type_change({name}, {props})
1890 */
1891 void
1892f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1893{
1894 prop_type_set(argvars, FALSE);
1895}
1896
1897/*
1898 * prop_type_delete({name} [, {bufnr}])
1899 */
1900 void
1901f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1902{
1903 char_u *name;
1904 buf_T *buf = NULL;
1905 hashitem_T *hi;
1906
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001907 if (in_vim9script()
1908 && (check_for_string_arg(argvars, 0) == FAIL
1909 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1910 return;
1911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001912 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001913 if (*name == NUL)
1914 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001915 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001916 return;
1917 }
1918
1919 if (argvars[1].v_type != VAR_UNKNOWN)
1920 {
1921 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1922 return;
1923 }
1924
Bram Moolenaare44336b2022-08-07 18:20:08 +01001925 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001926 if (hi != NULL)
1927 {
1928 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001929 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001930
1931 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001932 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001933 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001934 VIM_CLEAR(global_proparray);
1935 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001936 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001937 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001938 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001939 VIM_CLEAR(buf->b_proparray);
1940 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001941 hash_remove(ht, hi, "prop type delete");
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001942 vim_free(prop);
Bram Moolenaar89469d12022-12-02 20:46:26 +00001943
1944 // currently visibile text properties will disappear
1945 redraw_all_later(UPD_CLEAR);
1946 changed_window_setting_buf(buf == NULL ? curbuf : buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001947 }
1948}
1949
1950/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001951 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001952 */
1953 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001954f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001955{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001956 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001957
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001958 if (in_vim9script()
1959 && (check_for_string_arg(argvars, 0) == FAIL
1960 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1961 return;
1962
1963 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001964 if (*name == NUL)
1965 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001966 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001967 return;
1968 }
1969 if (rettv_dict_alloc(rettv) == OK)
1970 {
1971 proptype_T *prop = NULL;
1972 buf_T *buf = NULL;
1973
1974 if (argvars[1].v_type != VAR_UNKNOWN)
1975 {
1976 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1977 return;
1978 }
1979
Bram Moolenaare44336b2022-08-07 18:20:08 +01001980 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001981 if (prop != NULL)
1982 {
1983 dict_T *d = rettv->vval.v_dict;
1984
1985 if (prop->pt_hl_id > 0)
1986 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1987 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001988 dict_add_number(d, "combine",
1989 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001990 dict_add_number(d, "start_incl",
1991 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1992 dict_add_number(d, "end_incl",
1993 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1994 if (buf != NULL)
1995 dict_add_number(d, "bufnr", buf->b_fnum);
1996 }
1997 }
1998}
1999
2000 static void
2001list_types(hashtab_T *ht, list_T *l)
2002{
2003 long todo;
2004 hashitem_T *hi;
2005
2006 todo = (long)ht->ht_used;
2007 for (hi = ht->ht_array; todo > 0; ++hi)
2008 {
2009 if (!HASHITEM_EMPTY(hi))
2010 {
2011 proptype_T *prop = HI2PT(hi);
2012
2013 list_append_string(l, prop->pt_name, -1);
2014 --todo;
2015 }
2016 }
2017}
2018
2019/*
2020 * prop_type_list([{bufnr}])
2021 */
2022 void
2023f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
2024{
2025 buf_T *buf = NULL;
2026
2027 if (rettv_list_alloc(rettv) == OK)
2028 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002029 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
2030 return;
2031
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002032 if (argvars[0].v_type != VAR_UNKNOWN)
2033 {
2034 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2035 return;
2036 }
2037 if (buf == NULL)
2038 {
2039 if (global_proptypes != NULL)
2040 list_types(global_proptypes, rettv->vval.v_list);
2041 }
2042 else if (buf->b_proptypes != NULL)
2043 list_types(buf->b_proptypes, rettv->vval.v_list);
2044 }
2045}
2046
2047/*
2048 * Free all property types in "ht".
2049 */
2050 static void
2051clear_ht_prop_types(hashtab_T *ht)
2052{
2053 long todo;
2054 hashitem_T *hi;
2055
2056 if (ht == NULL)
2057 return;
2058
2059 todo = (long)ht->ht_used;
2060 for (hi = ht->ht_array; todo > 0; ++hi)
2061 {
2062 if (!HASHITEM_EMPTY(hi))
2063 {
2064 proptype_T *prop = HI2PT(hi);
2065
2066 vim_free(prop);
2067 --todo;
2068 }
2069 }
2070
2071 hash_clear(ht);
2072 vim_free(ht);
2073}
2074
2075#if defined(EXITFREE) || defined(PROTO)
2076/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002077 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002078 */
2079 void
2080clear_global_prop_types(void)
2081{
2082 clear_ht_prop_types(global_proptypes);
2083 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002084 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002085}
2086#endif
2087
2088/*
2089 * Free all property types for "buf".
2090 */
2091 void
2092clear_buf_prop_types(buf_T *buf)
2093{
2094 clear_ht_prop_types(buf->b_proptypes);
2095 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002096 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002097}
2098
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002099// Struct used to return two values from adjust_prop().
2100typedef struct
2101{
2102 int dirty; // if the property was changed
2103 int can_drop; // whether after this change, the prop may be removed
2104} adjustres_T;
2105
2106/*
2107 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2108 *
2109 * Note that "col" is zero-based, while tp_col is one-based.
2110 * Only for the current buffer.
2111 * "flags" can have:
2112 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002113 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002114 */
2115 static adjustres_T
2116adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002117 textprop_T *prop,
2118 colnr_T col,
2119 int added,
2120 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002121{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002122 proptype_T *pt;
2123 int start_incl;
2124 int end_incl;
2125 int droppable;
2126 adjustres_T res = {TRUE, FALSE};
2127
2128 // prop after end of the line doesn't move
2129 if (prop->tp_col == MAXCOL)
2130 {
2131 res.dirty = FALSE;
2132 return res;
2133 }
2134
2135 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2136 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002137 || (flags & APC_SUBSTITUTE)
2138 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002139 if (prop->tp_id < 0 && (flags & APC_INDENT))
2140 // when inserting indent just before a character with virtual text
2141 // shift the text property
2142 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002143 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002144 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002145 // do not drop zero-width props if they later can increase in size
2146 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002147
2148 if (added > 0)
2149 {
2150 if (col + 1 <= prop->tp_col
2151 - (start_incl || (prop->tp_len == 0 && end_incl)))
2152 // Change is entirely before the text property: Only shift
2153 prop->tp_col += added;
2154 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2155 // Insertion was inside text property
2156 prop->tp_len += added;
2157 }
2158 else if (prop->tp_col > col + 1)
2159 {
2160 if (prop->tp_col + added < col + 1)
2161 {
2162 prop->tp_len += (prop->tp_col - 1 - col) + added;
2163 prop->tp_col = col + 1;
2164 if (prop->tp_len <= 0)
2165 {
2166 prop->tp_len = 0;
2167 res.can_drop = droppable;
2168 }
2169 }
2170 else
2171 prop->tp_col += added;
2172 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002173 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2174 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002175 {
2176 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2177
2178 prop->tp_len += after > 0 ? added + after : added;
2179 res.can_drop = prop->tp_len <= 0 && droppable;
2180 }
2181 else
2182 res.dirty = FALSE;
2183
2184 return res;
2185}
2186
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002187/*
2188 * Adjust the columns of text properties in line "lnum" after position "col" to
2189 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002190 * Note that "col" is zero-based, while tp_col is one-based.
2191 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002192 * "flags" can have:
2193 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2194 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002195 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002196 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002197 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002198 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002199 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002200adjust_prop_columns(
2201 linenr_T lnum,
2202 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002203 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002204 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002205{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002206 int proplen;
2207 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002208 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002209 int ri, wi;
2210 size_t textlen;
2211
2212 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002213 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002214
2215 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2216 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002217 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002218 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002219
Bram Moolenaar196d1572019-01-02 23:47:18 +01002220 wi = 0; // write index
2221 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002222 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002223 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002224 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002225
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002226 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2227 res = adjust_prop(&prop, col, bytes_added, flags);
2228 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002229 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002230 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002231 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2232 && u_savesub(lnum) == FAIL)
2233 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002234 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002235
2236 // u_savesub() may have updated curbuf->b_ml, fetch it again
2237 if (curbuf->b_ml.ml_line_lnum != lnum)
2238 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002239 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002240 if (res.can_drop)
2241 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002242 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002243 ++wi;
2244 }
2245 if (dirty)
2246 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002247 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2248
2249 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002250 {
2251 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2252
2253 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2254 vim_free(curbuf->b_ml.ml_line_ptr);
2255 curbuf->b_ml.ml_line_ptr = p;
2256 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002257 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002258 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002259 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002260 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002261}
2262
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002263/*
2264 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002265 * "lnum_props" is the line that has the properties from before the split.
2266 * "lnum_top" is the top line.
2267 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002268 * "deleted" is the number of bytes deleted.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002269 * "at_eol" is true if the split is after the end of the line.
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002270 */
2271 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002272adjust_props_for_split(
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002273 linenr_T lnum_props,
2274 linenr_T lnum_top,
2275 int kept,
2276 int deleted,
2277 int at_eol)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002278{
2279 char_u *props;
2280 int count;
2281 garray_T prevprop;
2282 garray_T nextprop;
2283 int i;
2284 int skipped = kept + deleted;
2285
2286 if (!curbuf->b_has_textprop)
2287 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002288
2289 // Get the text properties from "lnum_props".
2290 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002291 ga_init2(&prevprop, sizeof(textprop_T), 10);
2292 ga_init2(&nextprop, sizeof(textprop_T), 10);
2293
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002294 // Keep the relevant ones in the first line, reducing the length if needed.
2295 // Copy the ones that include the split to the second line.
2296 // Move the ones after the split to the second line.
2297 for (i = 0; i < count; ++i)
2298 {
2299 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002300 proptype_T *pt;
2301 int start_incl, end_incl;
2302 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002303 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002304
2305 // copy the prop to an aligned structure
2306 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2307
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002308 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2309 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2310 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002311
2312 // a text prop "above" behaves like it is on the first text column
2313 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2314
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002315 if (prop_col == MAXCOL)
2316 {
2317 cont_prev = at_eol;
2318 cont_next = !at_eol;
2319 }
2320 else
2321 {
2322 cont_prev = prop_col + !start_incl <= kept;
2323 cont_next = skipped <= prop_col + prop.tp_len - !end_incl;
2324 }
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002325 // when a prop has text it is never copied
2326 if (prop.tp_id < 0 && cont_next)
2327 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002328
2329 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002330 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002331 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2332
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002333 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002334 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002335 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002336 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002337 if (cont_next)
2338 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002339 }
2340
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002341 // Only add the property to the next line if the length is bigger than
2342 // zero.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002343 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002344 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002345 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002346
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002347 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002348 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002349 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002350 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002351 if (p->tp_col > skipped)
2352 p->tp_col -= skipped - 1;
2353 else
2354 {
2355 p->tp_len -= skipped - p->tp_col;
2356 p->tp_col = 1;
2357 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002358 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002359 if (cont_prev)
2360 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002361 }
2362 }
2363
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002364 set_text_props(lnum_top, prevprop.ga_data,
2365 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002366 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002367 set_text_props(lnum_top + 1, nextprop.ga_data,
2368 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002369 ga_clear(&nextprop);
2370}
2371
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002372/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002373 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002374 */
2375 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002376prepend_joined_props(
2377 char_u *new_props,
2378 int propcount,
2379 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002380 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002381 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002382 long col,
2383 int removed)
2384{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002385 char_u *props;
2386 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2387 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002388
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002389 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002390 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002391 textprop_T prop;
2392 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002393
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002394 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002395 if (prop.tp_col == MAXCOL && !last_line)
2396 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002397 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2398
2399 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002400 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002401
Bram Moolenaare175dc62022-08-01 22:18:50 +01002402 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002403 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2404 &prop, sizeof(prop));
2405 else
2406 {
2407 int j;
2408 int found = FALSE;
2409
2410 // Search for continuing prop.
2411 for (j = *props_remaining; j < propcount; ++j)
2412 {
2413 textprop_T op;
2414
2415 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2416 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2417 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002418 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002419 found = TRUE;
2420 op.tp_len += op.tp_col - prop.tp_col;
2421 op.tp_col = prop.tp_col;
2422 // Start/end is taken care of when deleting joined lines
2423 op.tp_flags = prop.tp_flags;
2424 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2425 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002426 }
2427 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002428 if (!found)
2429 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002430 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002431 }
2432}
2433
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002434#endif // FEAT_PROP_POPUP