blob: 9256368125a354d8d7d56db1a1ffa4dc6260e3be [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);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200399 if (error || start_lnum <= 0 || start_col <= 0
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100400 || end_lnum <= 0 || end_col <= 0)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200401 {
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100402 if (prev_did_emsg == did_emsg)
403 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200404 return;
405 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100406 if (prop_add_one(buf, type_name, id, NULL, 0, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200407 start_col, end_col) == FAIL)
408 return;
409 }
410
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100411 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200412}
413
414/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100415 * Get the next ID to use for a textprop with text in buffer "buf".
416 */
417 static int
418get_textprop_id(buf_T *buf)
419{
420 // TODO: recycle deleted entries
421 return -(buf->b_textprop_text.ga_len + 1);
422}
423
424/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200425 * Shared between prop_add() and popup_create().
426 * "dict_arg" is the function argument of a dict containing "bufnr".
427 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100428 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200429 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100430 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200431prop_add_common(
432 linenr_T start_lnum,
433 colnr_T start_col,
434 dict_T *dict,
435 buf_T *default_buf,
436 typval_T *dict_arg)
437{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200438 linenr_T end_lnum;
439 colnr_T end_col;
440 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200441 buf_T *buf = default_buf;
442 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100443 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100444 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100445 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100446
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100447 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100448 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000449 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100450 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100451 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100452 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100453
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100454 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100455 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100456 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100457 if (end_lnum < start_lnum)
458 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000459 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100460 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100461 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100462 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100463 else
464 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100465
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100466 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100467 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100468 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100469
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100470 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000472 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100473 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100474 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100475 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100476 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100477 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100478 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100479 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100480 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100481 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000482 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100483 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100484 }
485 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100486 else if (start_lnum == end_lnum)
487 end_col = start_col;
488 else
489 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100490
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100491 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100492 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100493
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100494 if (dict_has_key(dict, "text"))
495 {
Bram Moolenaarfb593c52022-09-17 18:57:36 +0100496 if (dict_has_key(dict, "length")
497 || dict_has_key(dict, "end_col")
498 || dict_has_key(dict, "end_lnum"))
499 {
500 emsg(_(e_cannot_use_length_endcol_and_endlnum_with_text));
501 goto theend;
502 }
503
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100504 text = dict_get_string(dict, "text", TRUE);
505 if (text == NULL)
506 goto theend;
507 // use a default length of 1 to make multiple props show up
508 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100509
510 if (dict_has_key(dict, "text_align"))
511 {
512 char_u *p = dict_get_string(dict, "text_align", FALSE);
513
514 if (p == NULL)
515 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100516 if (start_col != 0)
517 {
518 emsg(_(e_can_only_use_text_align_when_column_is_zero));
519 goto theend;
520 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100521 if (STRCMP(p, "right") == 0)
522 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100523 else if (STRCMP(p, "above") == 0)
524 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100525 else if (STRCMP(p, "below") == 0)
526 flags |= TP_FLAG_ALIGN_BELOW;
527 else if (STRCMP(p, "after") != 0)
528 {
529 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
530 goto theend;
531 }
532 }
533
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100534 if (dict_has_key(dict, "text_padding_left"))
535 {
536 text_padding_left = dict_get_number(dict, "text_padding_left");
537 if (text_padding_left < 0)
538 {
539 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
540 goto theend;
541 }
542 }
543
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100544 if (dict_has_key(dict, "text_wrap"))
545 {
546 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100547
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100548 if (p == NULL)
549 goto theend;
550 if (STRCMP(p, "wrap") == 0)
551 flags |= TP_FLAG_WRAP;
552 else if (STRCMP(p, "truncate") != 0)
553 {
554 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
555 goto theend;
556 }
557 }
558 }
559
560 // Column must be 1 or more for a normal text property; when "text" is
561 // present zero means it goes after the line.
562 if (start_col < (text == NULL ? 1 : 0))
563 {
564 semsg(_(e_invalid_column_number_nr), (long)start_col);
565 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100566 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100567 if (start_col > 0 && text_padding_left > 0)
568 {
569 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
570 goto theend;
571 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100572
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200573 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100574 goto theend;
575
576 if (id < 0 && buf->b_textprop_text.ga_len > 0)
577 {
578 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
579 goto theend;
580 }
581 if (text != NULL)
582 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100583
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100584 // This must be done _before_ we add the property because property changes
585 // trigger buffer (memline) reorganisation, which needs this flag to be
586 // correctly set.
587 buf->b_has_textprop = TRUE; // this is never reset
588
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100589 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100590 start_lnum, end_lnum, start_col, end_col);
591 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100592
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100593 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100594
595theend:
596 vim_free(text);
597 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100598}
599
600/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100601 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100602 * Returns the number of text properties and, when non-zero, a pointer to the
603 * first one in "props" (note that it is not aligned, therefore the char_u
604 * pointer).
605 */
606 int
607get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
608{
609 char_u *text;
610 size_t textlen;
611 size_t proplen;
612
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100613 // Be quick when no text property types have been defined or the buffer,
614 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200615 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100616 return 0;
617
618 // Fetch the line to get the ml_line_len field updated.
619 text = ml_get_buf(buf, lnum, will_change);
620 textlen = STRLEN(text) + 1;
621 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100622 if (proplen == 0)
623 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100624 if (proplen % sizeof(textprop_T) != 0)
625 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000626 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100627 return 0;
628 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100629 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100630 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100631}
632
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100633/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100634 * Return the number of text properties with "above" or "below" alignment in
635 * line "lnum". A "right" aligned property also goes below after a "below" or
636 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100637 */
638 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100639prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100640{
641 char_u *props;
642 int count = get_text_props(buf, lnum, &props, FALSE);
643 int result = 0;
644 textprop_T prop;
645 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100646 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100647
648 if (count == 0)
649 return 0;
650 for (i = 0; i < count; ++i)
651 {
652 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100653 if (prop.tp_col == MAXCOL)
654 {
655 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
656 || (next_right_goes_below
657 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
658 {
659 next_right_goes_below = TRUE;
660 ++result;
661 }
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100662 else if (prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
663 {
664 next_right_goes_below = FALSE;
665 ++result;
666 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100667 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
668 next_right_goes_below = TRUE;
669 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100670 }
671 return result;
672}
673
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200674/**
675 * Return the number of text properties on line "lnum" in the current buffer.
676 * When "only_starting" is true only text properties starting in this line will
677 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100678 * When "last_line" is FALSE then text properties after the line are not
679 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200680 */
681 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100682count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200683{
684 char_u *props;
685 int proplen = get_text_props(curbuf, lnum, &props, 0);
686 int result = proplen;
687 int i;
688 textprop_T prop;
689
Bram Moolenaare175dc62022-08-01 22:18:50 +0100690 for (i = 0; i < proplen; ++i)
691 {
692 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100693 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100694 // previous line, or when not in the last line and it is virtual text
695 // after the line.
696 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
697 || (!last_line && prop.tp_col == MAXCOL))
698 --result;
699 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200700 return result;
701}
702
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100703static textprop_T *text_prop_compare_props;
704static buf_T *text_prop_compare_buf;
705
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100706/* Score for sorting on position of the text property: 0: above,
707 * 1: after (default), 2: right, 3: below (comes last)
708 */
709 static int
710text_prop_order(int flags)
711{
712 if (flags & TP_FLAG_ALIGN_ABOVE)
713 return 0;
714 if (flags & TP_FLAG_ALIGN_RIGHT)
715 return 2;
716 if (flags & TP_FLAG_ALIGN_BELOW)
717 return 3;
718 return 1;
719}
720
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100721/*
722 * Function passed to qsort() to sort text properties.
723 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
724 * both have the same priority.
725 */
726 static int
727text_prop_compare(const void *s1, const void *s2)
728{
729 int idx1, idx2;
730 textprop_T *tp1, *tp2;
731 proptype_T *pt1, *pt2;
732 colnr_T col1, col2;
733
734 idx1 = *(int *)s1;
735 idx2 = *(int *)s2;
736 tp1 = &text_prop_compare_props[idx1];
737 tp2 = &text_prop_compare_props[idx2];
738 col1 = tp1->tp_col;
739 col2 = tp2->tp_col;
740 if (col1 == MAXCOL && col2 == MAXCOL)
741 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100742 int order1 = text_prop_order(tp1->tp_flags);
743 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100744
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100745 // both props add text before or after the line, sort on order where it
746 // is added
747 if (order1 != order2)
748 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100749 }
750
751 // property that inserts text has priority over one that doesn't
752 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
753 return tp1->tp_id < 0 ? 1 : -1;
754
755 // check highest priority, defined by the type
756 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
757 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
758 if (pt1 != pt2)
759 {
760 if (pt1 == NULL)
761 return -1;
762 if (pt2 == NULL)
763 return 1;
764 if (pt1->pt_priority != pt2->pt_priority)
765 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
766 }
767
768 // same priority, one that starts first wins
769 if (col1 != col2)
770 return col1 < col2 ? 1 : -1;
771
772 // for a property with text the id can be used as tie breaker
773 if (tp1->tp_id < 0)
774 return tp1->tp_id > tp2->tp_id ? 1 : -1;
775
776 return 0;
777}
778
779/*
780 * Sort "count" text properties using an array if indexes "idxs" into the list
781 * of text props "props" for buffer "buf".
782 */
783 void
784sort_text_props(
785 buf_T *buf,
786 textprop_T *props,
787 int *idxs,
788 int count)
789{
790 text_prop_compare_buf = buf;
791 text_prop_compare_props = props;
792 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
793}
794
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100795/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200796 * Find text property "type_id" in the visible lines of window "wp".
797 * Match "id" when it is > 0.
798 * Returns FAIL when not found.
799 */
800 int
801find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
802 linenr_T *found_lnum)
803{
804 linenr_T lnum;
805 char_u *props;
806 int count;
807 int i;
808
809 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100810 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200811 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
812 {
813 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
814 for (i = 0; i < count; ++i)
815 {
816 mch_memmove(prop, props + i * sizeof(textprop_T),
817 sizeof(textprop_T));
818 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
819 {
820 *found_lnum = lnum;
821 return OK;
822 }
823 }
824 }
825 return FAIL;
826}
827
828/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100829 * Set the text properties for line "lnum" to "props" with length "len".
830 * If "len" is zero text properties are removed, "props" is not used.
831 * Any existing text properties are dropped.
832 * Only works for the current buffer.
833 */
834 static void
835set_text_props(linenr_T lnum, char_u *props, int len)
836{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100837 char_u *text;
838 char_u *newtext;
839 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100840
841 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100842 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100843 newtext = alloc(textlen + len);
844 if (newtext == NULL)
845 return;
846 mch_memmove(newtext, text, textlen);
847 if (len > 0)
848 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100849 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100850 vim_free(curbuf->b_ml.ml_line_ptr);
851 curbuf->b_ml.ml_line_ptr = newtext;
852 curbuf->b_ml.ml_line_len = textlen + len;
853 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
854}
855
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100856/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100857 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100858 */
859 void
860add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
861{
862 char_u *text;
863 char_u *newtext;
864 int proplen = text_prop_count * (int)sizeof(textprop_T);
865
866 text = ml_get(lnum);
867 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
868 if (newtext == NULL)
869 return;
870 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
871 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
872 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
873 vim_free(curbuf->b_ml.ml_line_ptr);
874 curbuf->b_ml.ml_line_ptr = newtext;
875 curbuf->b_ml.ml_line_len += proplen;
876 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
877}
878
Bram Moolenaare44336b2022-08-07 18:20:08 +0100879/*
880 * Function passed to qsort() for sorting proptype_T on pt_id.
881 */
882 static int
883compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100884{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100885 proptype_T *tp1 = *(proptype_T **)s1;
886 proptype_T *tp2 = *(proptype_T **)s2;
887
888 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
889}
890
891 static proptype_T *
892find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
893{
894 int low = 0;
895 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100896
Bram Moolenaar10246902022-08-08 17:08:05 +0100897 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100898 return NULL;
899
Bram Moolenaare44336b2022-08-07 18:20:08 +0100900 // Make the loopup faster by creating an array with pointers to
901 // hashtable entries, sorted on pt_id.
902 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100903 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100904 long todo;
905 hashitem_T *hi;
906 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100907
Bram Moolenaare44336b2022-08-07 18:20:08 +0100908 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
909 if (*array == NULL)
910 return NULL;
911 todo = (long)ht->ht_used;
912 for (hi = ht->ht_array; todo > 0; ++hi)
913 {
914 if (!HASHITEM_EMPTY(hi))
915 {
916 (*array)[i++] = HI2PT(hi);
917 --todo;
918 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100919 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100920 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
921 }
922
923 // binary search in the sorted array
924 high = ht->ht_used;
925 while (high > low)
926 {
927 int m = (high + low) / 2;
928
929 if ((*array)[m]->pt_id == id)
930 return (*array)[m];
931 if ((*array)[m]->pt_id > id)
932 high = m;
933 else
934 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100935 }
936 return NULL;
937}
938
939/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100940 * Fill 'dict' with text properties in 'prop'.
941 */
942 static void
943prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
944{
945 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200946 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100947
948 dict_add_number(dict, "col", prop->tp_col);
949 dict_add_number(dict, "length", prop->tp_len);
950 dict_add_number(dict, "id", prop->tp_id);
951 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
952 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200953
Bram Moolenaare44336b2022-08-07 18:20:08 +0100954 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200955 if (pt == NULL)
956 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100957 pt = find_type_by_id(global_proptypes, &global_proparray,
958 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200959 buflocal = FALSE;
960 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100961 if (pt != NULL)
962 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200963
964 if (buflocal)
965 dict_add_number(dict, "type_bufnr", buf->b_fnum);
966 else
967 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100968}
969
970/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100971 * Find a property type by ID in "buf" or globally.
972 * Returns NULL if not found.
973 */
974 proptype_T *
975text_prop_type_by_id(buf_T *buf, int id)
976{
977 proptype_T *type;
978
Bram Moolenaare44336b2022-08-07 18:20:08 +0100979 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100980 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100981 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100982 return type;
983}
984
985/*
986 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
987 */
988 void
989f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
990{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200991 linenr_T start;
992 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100993 linenr_T lnum;
994 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100995 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100996
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200997 if (in_vim9script()
998 && (check_for_number_arg(argvars, 0) == FAIL
999 || check_for_opt_number_arg(argvars, 1) == FAIL
1000 || (argvars[1].v_type != VAR_UNKNOWN
1001 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1002 return;
1003
1004 start = tv_get_number(&argvars[0]);
1005 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001006 if (argvars[1].v_type != VAR_UNKNOWN)
1007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001008 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001009 if (argvars[2].v_type != VAR_UNKNOWN)
1010 {
1011 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
1012 return;
1013 }
1014 }
1015 if (start < 1 || end < 1)
1016 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001017 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001018 return;
1019 }
1020
1021 for (lnum = start; lnum <= end; ++lnum)
1022 {
1023 char_u *text;
1024 size_t len;
1025
1026 if (lnum > buf->b_ml.ml_line_count)
1027 break;
1028 text = ml_get_buf(buf, lnum, FALSE);
1029 len = STRLEN(text) + 1;
1030 if ((size_t)buf->b_ml.ml_line_len > len)
1031 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001032 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001033 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1034 {
1035 char_u *newtext = vim_strsave(text);
1036
1037 // need to allocate the line now
1038 if (newtext == NULL)
1039 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001040 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1041 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001042 buf->b_ml.ml_line_ptr = newtext;
1043 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1044 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001045 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001046 }
1047 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001048 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001049 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001050}
1051
1052/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001053 * prop_find({props} [, {direction}])
1054 */
1055 void
1056f_prop_find(typval_T *argvars, typval_T *rettv)
1057{
1058 pos_T *cursor = &curwin->w_cursor;
1059 dict_T *dict;
1060 buf_T *buf = curbuf;
1061 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001062 int lnum_start;
1063 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001064 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001065 int id = 0;
1066 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001067 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001068 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001069 int lnum = -1;
1070 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001071 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001072 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001073
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001074 if (in_vim9script()
1075 && (check_for_dict_arg(argvars, 0) == FAIL
1076 || check_for_opt_string_arg(argvars, 1) == FAIL))
1077 return;
1078
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001079 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001080 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001081 dict = argvars[0].vval.v_dict;
1082
1083 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1084 return;
1085 if (buf->b_ml.ml_mfp == NULL)
1086 return;
1087
1088 if (argvars[1].v_type != VAR_UNKNOWN)
1089 {
1090 char_u *dir_s = tv_get_string(&argvars[1]);
1091
1092 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001093 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001094 else if (*dir_s != 'f')
1095 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001096 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001097 return;
1098 }
1099 }
1100
1101 di = dict_find(dict, (char_u *)"lnum", -1);
1102 if (di != NULL)
1103 lnum = tv_get_number(&di->di_tv);
1104
1105 di = dict_find(dict, (char_u *)"col", -1);
1106 if (di != NULL)
1107 col = tv_get_number(&di->di_tv);
1108
1109 if (lnum == -1)
1110 {
1111 lnum = cursor->lnum;
1112 col = cursor->col + 1;
1113 }
1114 else if (col == -1)
1115 col = 1;
1116
1117 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1118 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001119 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001120 return;
1121 }
1122
Bram Moolenaard61efa52022-07-23 09:52:04 +01001123 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001124
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001125 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001126 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001127 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001128 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001129 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001130 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001131 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001132 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001133 proptype_T *type = lookup_prop_type(name, buf);
1134
1135 if (type == NULL)
1136 return;
1137 type_id = type->pt_id;
1138 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001139 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001140 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001141 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001142 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001143 return;
1144 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001145 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001146 {
Ben Jacksona7704222022-08-20 20:54:51 +01001147 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001148 return;
1149 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001150
1151 lnum_start = lnum;
1152
1153 if (rettv_dict_alloc(rettv) == FAIL)
1154 return;
1155
1156 while (1)
1157 {
1158 char_u *text = ml_get_buf(buf, lnum, FALSE);
1159 size_t textlen = STRLEN(text) + 1;
1160 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001161 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001162 int i;
1163 textprop_T prop;
1164 int prop_start;
1165 int prop_end;
1166
LemonBoy9bd3ce22022-04-18 21:54:02 +01001167 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001168 {
1169 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001170 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001171
LemonBoy9bd3ce22022-04-18 21:54:02 +01001172 // For the very first line try to find the first property before or
1173 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001174 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001175 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001176 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001177 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001178 if (prop.tp_col > col)
1179 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001180 }
1181 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1182 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001183 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001184 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001185 : (id_found && prop.tp_id == id)
1186 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001187 {
1188 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001189 if (lnum_start == lnum
1190 && col >= prop.tp_col
1191 && (col <= prop.tp_col + prop.tp_len
1192 - (prop.tp_len != 0)))
1193 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001194
LemonBoy9bd3ce22022-04-18 21:54:02 +01001195 // The property was not continued from last line, it starts on
1196 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001197 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001198 // The property does not continue on the next line, it ends on
1199 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001200 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001201 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001202 seen_end = 1;
1203
1204 // Skip lines without the start flag.
1205 if (!prop_start)
1206 {
1207 // Always search backwards for start when search started
1208 // on a prop and we're not skipping.
1209 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001210 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001211 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001212 }
1213
1214 // If skipstart is true, skip the prop at start pos (even if
1215 // continued from another line).
1216 if (start_pos_has_prop && skipstart && !seen_end)
1217 {
1218 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001219 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001220 }
1221
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001222 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1223 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1224
1225 return;
1226 }
1227 }
1228
1229 if (dir > 0)
1230 {
1231 if (lnum >= buf->b_ml.ml_line_count)
1232 break;
1233 lnum++;
1234 }
1235 else
1236 {
1237 if (lnum <= 1)
1238 break;
1239 lnum--;
1240 }
1241 }
1242}
1243
1244/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001245 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1246 */
1247 static int
1248prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1249{
1250 int i;
1251
1252 for (i = 0; i < len; i++)
1253 if (types_or_ids[i] == type_or_id)
1254 return TRUE;
1255
1256 return FALSE;
1257}
1258
1259/*
1260 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1261 * If 'prop_types' is not NULL, then return only the text properties with
1262 * matching property type in the 'prop_types' array.
1263 * If 'prop_ids' is not NULL, then return only the text properties with
1264 * an identifier in the 'props_ids' array.
1265 * If 'add_lnum' is TRUE, then add the line number also to the text property
1266 * dictionary.
1267 */
1268 static void
1269get_props_in_line(
1270 buf_T *buf,
1271 linenr_T lnum,
1272 int *prop_types,
1273 int prop_types_len,
1274 int *prop_ids,
1275 int prop_ids_len,
1276 list_T *retlist,
1277 int add_lnum)
1278{
1279 char_u *text = ml_get_buf(buf, lnum, FALSE);
1280 size_t textlen = STRLEN(text) + 1;
1281 int count;
1282 int i;
1283 textprop_T prop;
1284
1285 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1286 for (i = 0; i < count; ++i)
1287 {
1288 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1289 sizeof(textprop_T));
1290 if ((prop_types == NULL
1291 || prop_type_or_id_in_list(prop_types, prop_types_len,
1292 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001293 && (prop_ids == NULL
1294 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1295 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001296 {
1297 dict_T *d = dict_alloc();
1298
1299 if (d == NULL)
1300 break;
1301 prop_fill_dict(d, &prop, buf);
1302 if (add_lnum)
1303 dict_add_number(d, "lnum", lnum);
1304 list_append_dict(retlist, d);
1305 }
1306 }
1307}
1308
1309/*
1310 * Convert a List of property type names into an array of property type
1311 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1312 * error. 'num_types' is set to the number of returned property types.
1313 */
1314 static int *
1315get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1316{
1317 int *prop_types;
1318 listitem_T *li;
1319 int i;
1320 char_u *name;
1321 proptype_T *type;
1322
1323 *num_types = 0;
1324
1325 prop_types = ALLOC_MULT(int, list_len(l));
1326 if (prop_types == NULL)
1327 return NULL;
1328
1329 i = 0;
1330 FOR_ALL_LIST_ITEMS(l, li)
1331 {
1332 if (li->li_tv.v_type != VAR_STRING)
1333 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001334 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001335 goto errret;
1336 }
1337 name = li->li_tv.vval.v_string;
1338 if (name == NULL)
1339 goto errret;
1340
1341 type = lookup_prop_type(name, buf);
1342 if (type == NULL)
1343 goto errret;
1344 prop_types[i++] = type->pt_id;
1345 }
1346
1347 *num_types = i;
1348 return prop_types;
1349
1350errret:
1351 VIM_CLEAR(prop_types);
1352 return NULL;
1353}
1354
1355/*
1356 * Convert a List of property identifiers into an array of property
1357 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1358 * error. 'num_ids' is set to the number of returned property identifiers.
1359 */
1360 static int *
1361get_prop_ids_from_list(list_T *l, int *num_ids)
1362{
1363 int *prop_ids;
1364 listitem_T *li;
1365 int i;
1366 int id;
1367 int error;
1368
1369 *num_ids = 0;
1370
1371 prop_ids = ALLOC_MULT(int, list_len(l));
1372 if (prop_ids == NULL)
1373 return NULL;
1374
1375 i = 0;
1376 FOR_ALL_LIST_ITEMS(l, li)
1377 {
1378 error = FALSE;
1379 id = tv_get_number_chk(&li->li_tv, &error);
1380 if (error)
1381 goto errret;
1382
1383 prop_ids[i++] = id;
1384 }
1385
1386 *num_ids = i;
1387 return prop_ids;
1388
1389errret:
1390 VIM_CLEAR(prop_ids);
1391 return NULL;
1392}
1393
1394/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001395 * prop_list({lnum} [, {bufnr}])
1396 */
1397 void
1398f_prop_list(typval_T *argvars, typval_T *rettv)
1399{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001400 linenr_T lnum;
1401 linenr_T start_lnum;
1402 linenr_T end_lnum;
1403 buf_T *buf = curbuf;
1404 int add_lnum = FALSE;
1405 int *prop_types = NULL;
1406 int prop_types_len = 0;
1407 int *prop_ids = NULL;
1408 int prop_ids_len = 0;
1409 list_T *l;
1410 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001411
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001412 if (in_vim9script()
1413 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001414 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001415 return;
1416
Bram Moolenaar93a10962022-06-16 11:42:09 +01001417 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001418 return;
1419
1420 // default: get text properties on current line
1421 start_lnum = tv_get_number(&argvars[0]);
1422 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001423 if (argvars[1].v_type != VAR_UNKNOWN)
1424 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001425 dict_T *d;
1426
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001427 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001428 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001429 d = argvars[1].vval.v_dict;
1430
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001431 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1432 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001433
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001434 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001435 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001436 if (di->di_tv.v_type != VAR_NUMBER)
1437 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001438 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001439 return;
1440 }
1441 end_lnum = tv_get_number(&di->di_tv);
1442 if (end_lnum < 0)
1443 // negative end_lnum is used as an offset from the last buffer
1444 // line
1445 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1446 else if (end_lnum > buf->b_ml.ml_line_count)
1447 end_lnum = buf->b_ml.ml_line_count;
1448 add_lnum = TRUE;
1449 }
1450 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1451 {
1452 if (di->di_tv.v_type != VAR_LIST)
1453 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001454 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001455 return;
1456 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001457
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001458 l = di->di_tv.vval.v_list;
1459 if (l != NULL && list_len(l) > 0)
1460 {
1461 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1462 if (prop_types == NULL)
1463 return;
1464 }
1465 }
1466 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1467 {
1468 if (di->di_tv.v_type != VAR_LIST)
1469 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001470 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001471 goto errret;
1472 }
1473
1474 l = di->di_tv.vval.v_list;
1475 if (l != NULL && list_len(l) > 0)
1476 {
1477 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1478 if (prop_ids == NULL)
1479 goto errret;
1480 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001481 }
1482 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001483 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1484 || end_lnum < 1 || end_lnum < start_lnum)
1485 emsg(_(e_invalid_range));
1486 else
1487 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1488 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1489 prop_ids, prop_ids_len,
1490 rettv->vval.v_list, add_lnum);
1491
1492errret:
1493 VIM_CLEAR(prop_types);
1494 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495}
1496
1497/*
1498 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1499 */
1500 void
1501f_prop_remove(typval_T *argvars, typval_T *rettv)
1502{
1503 linenr_T start = 1;
1504 linenr_T end = 0;
1505 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001506 linenr_T first_changed = 0;
1507 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001508 dict_T *dict;
1509 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001510 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001511 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001512 int type_id = -1; // for a single "type"
1513 int *type_ids = NULL; // array, for a list of "types", allocated
1514 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001515 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001516 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001517
1518 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001519
1520 if (in_vim9script()
1521 && (check_for_dict_arg(argvars, 0) == FAIL
1522 || check_for_opt_number_arg(argvars, 1) == FAIL
1523 || (argvars[1].v_type != VAR_UNKNOWN
1524 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1525 return;
1526
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001527 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001528 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001529
1530 if (argvars[1].v_type != VAR_UNKNOWN)
1531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001532 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001533 end = start;
1534 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001535 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001536 if (start < 1 || end < 1)
1537 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001538 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001539 return;
1540 }
1541 }
1542
1543 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001544 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1545 return;
1546 if (buf->b_ml.ml_mfp == NULL)
1547 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001548
Bram Moolenaard61efa52022-07-23 09:52:04 +01001549 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001550
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001551 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001552 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001553
1554 // if a specific type was supplied "type": check that (and ignore "types".
1555 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001556 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001557 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001558 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001559 proptype_T *type = lookup_prop_type(name, buf);
1560
1561 if (type == NULL)
1562 return;
1563 type_id = type->pt_id;
1564 }
Ben Jacksona7704222022-08-20 20:54:51 +01001565 if (dict_has_key(dict, "types"))
1566 {
1567 typval_T types;
1568 listitem_T *li = NULL;
1569
1570 dict_get_tv(dict, "types", &types);
1571 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1572 {
1573 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1574
1575 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1576 {
1577 proptype_T *prop_type;
1578
1579 if (li->li_tv.v_type != VAR_STRING)
1580 continue;
1581
1582 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1583
1584 if (!prop_type)
1585 goto cleanup_prop_remove;
1586
1587 type_ids[num_type_ids++] = prop_type->pt_id;
1588 }
1589 }
1590 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001591 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001592
Ben Jacksona7704222022-08-20 20:54:51 +01001593 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001594 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001595 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001596 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001597 }
Ben Jacksona7704222022-08-20 20:54:51 +01001598 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001599 {
Ben Jacksona7704222022-08-20 20:54:51 +01001600 emsg(_(e_need_id_and_type_or_types_with_both));
1601 goto cleanup_prop_remove;
1602 }
1603 if (type_id != -1 && num_type_ids > 0)
1604 {
1605 emsg(_(e_cannot_specify_both_type_and_types));
1606 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001607 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001608
1609 if (end == 0)
1610 end = buf->b_ml.ml_line_count;
1611 for (lnum = start; lnum <= end; ++lnum)
1612 {
1613 char_u *text;
1614 size_t len;
1615
1616 if (lnum > buf->b_ml.ml_line_count)
1617 break;
1618 text = ml_get_buf(buf, lnum, FALSE);
1619 len = STRLEN(text) + 1;
1620 if ((size_t)buf->b_ml.ml_line_len > len)
1621 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001622 static textprop_T textprop; // static because of alignment
1623 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001624
1625 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1626 / sizeof(textprop_T); ++idx)
1627 {
1628 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1629 + idx * sizeof(textprop_T);
1630 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001631 int matches_id = 0;
1632 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001633
1634 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001635
1636 matches_id = textprop.tp_id == id;
1637 if (num_type_ids > 0)
1638 {
1639 int idx2;
1640
1641 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1642 matches_type = textprop.tp_type == type_ids[idx2];
1643 }
1644 else
1645 {
1646 matches_type = textprop.tp_type == type_id;
1647 }
1648
1649 if (both ? matches_id && matches_type
1650 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001651 {
1652 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1653 {
1654 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1655
1656 // need to allocate the line to be able to change it
1657 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001658 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001659 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1660 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001661 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1662 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001663 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001664 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1665
1666 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001667 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001668 }
1669
1670 taillen = buf->b_ml.ml_line_len - len
1671 - (idx + 1) * sizeof(textprop_T);
1672 if (taillen > 0)
1673 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1674 taillen);
1675 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1676 --idx;
1677
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001678 if (textprop.tp_id < 0)
1679 {
1680 garray_T *gap = &buf->b_textprop_text;
1681 int ii = -textprop.tp_id - 1;
1682
1683 // negative ID: property with text - free the text
1684 if (ii < gap->ga_len)
1685 {
1686 char_u **p = ((char_u **)gap->ga_data) + ii;
1687 vim_free(*p);
1688 *p = NULL;
1689 did_remove_text = TRUE;
1690 }
1691 }
1692
Bram Moolenaar965c0442021-05-17 00:22:06 +02001693 if (first_changed == 0)
1694 first_changed = lnum;
1695 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001696 ++rettv->vval.v_number;
1697 if (!do_all)
1698 break;
1699 }
1700 }
1701 }
1702 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001703
Bram Moolenaar965c0442021-05-17 00:22:06 +02001704 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001705 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001706 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001707 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001708 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001709 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001710
1711 if (did_remove_text)
1712 {
1713 garray_T *gap = &buf->b_textprop_text;
1714
1715 // Reduce the growarray size for NULL pointers at the end.
1716 while (gap->ga_len > 0
1717 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1718 --gap->ga_len;
1719 }
Ben Jacksona7704222022-08-20 20:54:51 +01001720
1721cleanup_prop_remove:
1722 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001723}
1724
1725/*
1726 * Common for f_prop_type_add() and f_prop_type_change().
1727 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001728 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001729prop_type_set(typval_T *argvars, int add)
1730{
1731 char_u *name;
1732 buf_T *buf = NULL;
1733 dict_T *dict;
1734 dictitem_T *di;
1735 proptype_T *prop;
1736
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001737 if (in_vim9script()
1738 && (check_for_string_arg(argvars, 0) == FAIL
1739 || check_for_dict_arg(argvars, 1) == FAIL))
1740 return;
1741
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001742 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001743 if (*name == NUL)
1744 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001745 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001746 return;
1747 }
1748
1749 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1750 return;
1751 dict = argvars[1].vval.v_dict;
1752
Bram Moolenaare44336b2022-08-07 18:20:08 +01001753 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001754 if (add)
1755 {
1756 hashtab_T **htp;
1757
1758 if (prop != NULL)
1759 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001760 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001761 return;
1762 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001763 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001764 if (prop == NULL)
1765 return;
1766 STRCPY(prop->pt_name, name);
1767 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001768 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001769 if (buf == NULL)
1770 {
1771 htp = &global_proptypes;
1772 VIM_CLEAR(global_proparray);
1773 }
1774 else
1775 {
1776 htp = &buf->b_proptypes;
1777 VIM_CLEAR(buf->b_proparray);
1778 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001779 if (*htp == NULL)
1780 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001781 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001782 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001783 {
1784 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001785 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001786 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001787 hash_init(*htp);
1788 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001789 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001790 }
1791 else
1792 {
1793 if (prop == NULL)
1794 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001795 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001796 return;
1797 }
1798 }
1799
1800 if (dict != NULL)
1801 {
1802 di = dict_find(dict, (char_u *)"highlight", -1);
1803 if (di != NULL)
1804 {
1805 char_u *highlight;
1806 int hl_id = 0;
1807
Bram Moolenaard61efa52022-07-23 09:52:04 +01001808 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001809 if (highlight != NULL && *highlight != NUL)
1810 hl_id = syn_name2id(highlight);
1811 if (hl_id <= 0)
1812 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001813 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001814 highlight == NULL ? (char_u *)"" : highlight);
1815 return;
1816 }
1817 prop->pt_hl_id = hl_id;
1818 }
1819
Bram Moolenaar58187f12019-05-05 16:33:47 +02001820 di = dict_find(dict, (char_u *)"combine", -1);
1821 if (di != NULL)
1822 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001823 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001824 prop->pt_flags |= PT_FLAG_COMBINE;
1825 else
1826 prop->pt_flags &= ~PT_FLAG_COMBINE;
1827 }
1828
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001829 di = dict_find(dict, (char_u *)"override", -1);
1830 if (di != NULL)
1831 {
1832 if (tv_get_bool(&di->di_tv))
1833 prop->pt_flags |= PT_FLAG_OVERRIDE;
1834 else
1835 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1836 }
1837
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001838 di = dict_find(dict, (char_u *)"priority", -1);
1839 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001840 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001841
1842 di = dict_find(dict, (char_u *)"start_incl", -1);
1843 if (di != NULL)
1844 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001845 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001846 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1847 else
1848 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1849 }
1850
1851 di = dict_find(dict, (char_u *)"end_incl", -1);
1852 if (di != NULL)
1853 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001854 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001855 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1856 else
1857 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1858 }
1859 }
1860}
1861
1862/*
1863 * prop_type_add({name}, {props})
1864 */
1865 void
1866f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1867{
1868 prop_type_set(argvars, TRUE);
1869}
1870
1871/*
1872 * prop_type_change({name}, {props})
1873 */
1874 void
1875f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1876{
1877 prop_type_set(argvars, FALSE);
1878}
1879
1880/*
1881 * prop_type_delete({name} [, {bufnr}])
1882 */
1883 void
1884f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1885{
1886 char_u *name;
1887 buf_T *buf = NULL;
1888 hashitem_T *hi;
1889
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001890 if (in_vim9script()
1891 && (check_for_string_arg(argvars, 0) == FAIL
1892 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1893 return;
1894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001895 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001896 if (*name == NUL)
1897 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001898 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001899 return;
1900 }
1901
1902 if (argvars[1].v_type != VAR_UNKNOWN)
1903 {
1904 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1905 return;
1906 }
1907
Bram Moolenaare44336b2022-08-07 18:20:08 +01001908 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001909 if (hi != NULL)
1910 {
1911 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001912 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001913
1914 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001915 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001916 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001917 VIM_CLEAR(global_proparray);
1918 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001919 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001920 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001921 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001922 VIM_CLEAR(buf->b_proparray);
1923 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001924 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001925 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001926 }
1927}
1928
1929/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001930 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001931 */
1932 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001933f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001934{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001935 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001936
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001937 if (in_vim9script()
1938 && (check_for_string_arg(argvars, 0) == FAIL
1939 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1940 return;
1941
1942 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001943 if (*name == NUL)
1944 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001945 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001946 return;
1947 }
1948 if (rettv_dict_alloc(rettv) == OK)
1949 {
1950 proptype_T *prop = NULL;
1951 buf_T *buf = NULL;
1952
1953 if (argvars[1].v_type != VAR_UNKNOWN)
1954 {
1955 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1956 return;
1957 }
1958
Bram Moolenaare44336b2022-08-07 18:20:08 +01001959 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001960 if (prop != NULL)
1961 {
1962 dict_T *d = rettv->vval.v_dict;
1963
1964 if (prop->pt_hl_id > 0)
1965 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1966 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001967 dict_add_number(d, "combine",
1968 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001969 dict_add_number(d, "start_incl",
1970 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1971 dict_add_number(d, "end_incl",
1972 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1973 if (buf != NULL)
1974 dict_add_number(d, "bufnr", buf->b_fnum);
1975 }
1976 }
1977}
1978
1979 static void
1980list_types(hashtab_T *ht, list_T *l)
1981{
1982 long todo;
1983 hashitem_T *hi;
1984
1985 todo = (long)ht->ht_used;
1986 for (hi = ht->ht_array; todo > 0; ++hi)
1987 {
1988 if (!HASHITEM_EMPTY(hi))
1989 {
1990 proptype_T *prop = HI2PT(hi);
1991
1992 list_append_string(l, prop->pt_name, -1);
1993 --todo;
1994 }
1995 }
1996}
1997
1998/*
1999 * prop_type_list([{bufnr}])
2000 */
2001 void
2002f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
2003{
2004 buf_T *buf = NULL;
2005
2006 if (rettv_list_alloc(rettv) == OK)
2007 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002008 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
2009 return;
2010
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002011 if (argvars[0].v_type != VAR_UNKNOWN)
2012 {
2013 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2014 return;
2015 }
2016 if (buf == NULL)
2017 {
2018 if (global_proptypes != NULL)
2019 list_types(global_proptypes, rettv->vval.v_list);
2020 }
2021 else if (buf->b_proptypes != NULL)
2022 list_types(buf->b_proptypes, rettv->vval.v_list);
2023 }
2024}
2025
2026/*
2027 * Free all property types in "ht".
2028 */
2029 static void
2030clear_ht_prop_types(hashtab_T *ht)
2031{
2032 long todo;
2033 hashitem_T *hi;
2034
2035 if (ht == NULL)
2036 return;
2037
2038 todo = (long)ht->ht_used;
2039 for (hi = ht->ht_array; todo > 0; ++hi)
2040 {
2041 if (!HASHITEM_EMPTY(hi))
2042 {
2043 proptype_T *prop = HI2PT(hi);
2044
2045 vim_free(prop);
2046 --todo;
2047 }
2048 }
2049
2050 hash_clear(ht);
2051 vim_free(ht);
2052}
2053
2054#if defined(EXITFREE) || defined(PROTO)
2055/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002056 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002057 */
2058 void
2059clear_global_prop_types(void)
2060{
2061 clear_ht_prop_types(global_proptypes);
2062 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002063 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002064}
2065#endif
2066
2067/*
2068 * Free all property types for "buf".
2069 */
2070 void
2071clear_buf_prop_types(buf_T *buf)
2072{
2073 clear_ht_prop_types(buf->b_proptypes);
2074 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002075 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002076}
2077
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002078// Struct used to return two values from adjust_prop().
2079typedef struct
2080{
2081 int dirty; // if the property was changed
2082 int can_drop; // whether after this change, the prop may be removed
2083} adjustres_T;
2084
2085/*
2086 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2087 *
2088 * Note that "col" is zero-based, while tp_col is one-based.
2089 * Only for the current buffer.
2090 * "flags" can have:
2091 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002092 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002093 */
2094 static adjustres_T
2095adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002096 textprop_T *prop,
2097 colnr_T col,
2098 int added,
2099 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002100{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002101 proptype_T *pt;
2102 int start_incl;
2103 int end_incl;
2104 int droppable;
2105 adjustres_T res = {TRUE, FALSE};
2106
2107 // prop after end of the line doesn't move
2108 if (prop->tp_col == MAXCOL)
2109 {
2110 res.dirty = FALSE;
2111 return res;
2112 }
2113
2114 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2115 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002116 || (flags & APC_SUBSTITUTE)
2117 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002118 if (prop->tp_id < 0 && (flags & APC_INDENT))
2119 // when inserting indent just before a character with virtual text
2120 // shift the text property
2121 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002122 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002123 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002124 // do not drop zero-width props if they later can increase in size
2125 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002126
2127 if (added > 0)
2128 {
2129 if (col + 1 <= prop->tp_col
2130 - (start_incl || (prop->tp_len == 0 && end_incl)))
2131 // Change is entirely before the text property: Only shift
2132 prop->tp_col += added;
2133 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2134 // Insertion was inside text property
2135 prop->tp_len += added;
2136 }
2137 else if (prop->tp_col > col + 1)
2138 {
2139 if (prop->tp_col + added < col + 1)
2140 {
2141 prop->tp_len += (prop->tp_col - 1 - col) + added;
2142 prop->tp_col = col + 1;
2143 if (prop->tp_len <= 0)
2144 {
2145 prop->tp_len = 0;
2146 res.can_drop = droppable;
2147 }
2148 }
2149 else
2150 prop->tp_col += added;
2151 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002152 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2153 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002154 {
2155 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2156
2157 prop->tp_len += after > 0 ? added + after : added;
2158 res.can_drop = prop->tp_len <= 0 && droppable;
2159 }
2160 else
2161 res.dirty = FALSE;
2162
2163 return res;
2164}
2165
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002166/*
2167 * Adjust the columns of text properties in line "lnum" after position "col" to
2168 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002169 * Note that "col" is zero-based, while tp_col is one-based.
2170 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002171 * "flags" can have:
2172 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2173 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002174 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002175 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002176 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002177 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002178 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002179adjust_prop_columns(
2180 linenr_T lnum,
2181 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002182 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002183 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002184{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002185 int proplen;
2186 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002187 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002188 int ri, wi;
2189 size_t textlen;
2190
2191 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002192 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002193
2194 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2195 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002196 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002197 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002198
Bram Moolenaar196d1572019-01-02 23:47:18 +01002199 wi = 0; // write index
2200 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002201 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002202 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002203 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002204
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002205 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2206 res = adjust_prop(&prop, col, bytes_added, flags);
2207 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002208 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002209 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002210 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2211 && u_savesub(lnum) == FAIL)
2212 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002213 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002214
2215 // u_savesub() may have updated curbuf->b_ml, fetch it again
2216 if (curbuf->b_ml.ml_line_lnum != lnum)
2217 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002218 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002219 if (res.can_drop)
2220 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002221 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002222 ++wi;
2223 }
2224 if (dirty)
2225 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002226 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2227
2228 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002229 {
2230 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2231
2232 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2233 vim_free(curbuf->b_ml.ml_line_ptr);
2234 curbuf->b_ml.ml_line_ptr = p;
2235 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002236 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002237 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002238 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002239 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002240}
2241
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002242/*
2243 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002244 * "lnum_props" is the line that has the properties from before the split.
2245 * "lnum_top" is the top line.
2246 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002247 * "deleted" is the number of bytes deleted.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002248 * "at_eol" is true if the split is after the end of the line.
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002249 */
2250 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002251adjust_props_for_split(
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002252 linenr_T lnum_props,
2253 linenr_T lnum_top,
2254 int kept,
2255 int deleted,
2256 int at_eol)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002257{
2258 char_u *props;
2259 int count;
2260 garray_T prevprop;
2261 garray_T nextprop;
2262 int i;
2263 int skipped = kept + deleted;
2264
2265 if (!curbuf->b_has_textprop)
2266 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002267
2268 // Get the text properties from "lnum_props".
2269 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002270 ga_init2(&prevprop, sizeof(textprop_T), 10);
2271 ga_init2(&nextprop, sizeof(textprop_T), 10);
2272
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002273 // Keep the relevant ones in the first line, reducing the length if needed.
2274 // Copy the ones that include the split to the second line.
2275 // Move the ones after the split to the second line.
2276 for (i = 0; i < count; ++i)
2277 {
2278 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002279 proptype_T *pt;
2280 int start_incl, end_incl;
2281 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002282 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002283
2284 // copy the prop to an aligned structure
2285 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2286
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002287 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2288 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2289 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002290
2291 // a text prop "above" behaves like it is on the first text column
2292 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2293
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002294 if (prop_col == MAXCOL)
2295 {
2296 cont_prev = at_eol;
2297 cont_next = !at_eol;
2298 }
2299 else
2300 {
2301 cont_prev = prop_col + !start_incl <= kept;
2302 cont_next = skipped <= prop_col + prop.tp_len - !end_incl;
2303 }
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002304 // when a prop has text it is never copied
2305 if (prop.tp_id < 0 && cont_next)
2306 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002307
2308 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002309 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002310 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2311
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002312 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002313 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002314 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002315 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002316 if (cont_next)
2317 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002318 }
2319
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002320 // Only add the property to the next line if the length is bigger than
2321 // zero.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002322 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002323 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002324 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002325
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002326 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002327 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002328 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002329 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002330 if (p->tp_col > skipped)
2331 p->tp_col -= skipped - 1;
2332 else
2333 {
2334 p->tp_len -= skipped - p->tp_col;
2335 p->tp_col = 1;
2336 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002337 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002338 if (cont_prev)
2339 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002340 }
2341 }
2342
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002343 set_text_props(lnum_top, prevprop.ga_data,
2344 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002345 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002346 set_text_props(lnum_top + 1, nextprop.ga_data,
2347 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002348 ga_clear(&nextprop);
2349}
2350
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002351/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002352 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002353 */
2354 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002355prepend_joined_props(
2356 char_u *new_props,
2357 int propcount,
2358 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002359 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002360 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002361 long col,
2362 int removed)
2363{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002364 char_u *props;
2365 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2366 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002367
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002368 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002369 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002370 textprop_T prop;
2371 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002372
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002373 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002374 if (prop.tp_col == MAXCOL && !last_line)
2375 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002376 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2377
2378 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002379 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002380
Bram Moolenaare175dc62022-08-01 22:18:50 +01002381 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002382 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2383 &prop, sizeof(prop));
2384 else
2385 {
2386 int j;
2387 int found = FALSE;
2388
2389 // Search for continuing prop.
2390 for (j = *props_remaining; j < propcount; ++j)
2391 {
2392 textprop_T op;
2393
2394 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2395 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2396 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002397 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002398 found = TRUE;
2399 op.tp_len += op.tp_col - prop.tp_col;
2400 op.tp_col = prop.tp_col;
2401 // Start/end is taken care of when deleting joined lines
2402 op.tp_flags = prop.tp_flags;
2403 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2404 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002405 }
2406 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002407 if (!found)
2408 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002409 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002410 }
2411}
2412
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002413#endif // FEAT_PROP_POPUP