blob: 808d1e639ea118d51798798a2d74eef8208476f0 [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010017
18/*
19 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
20 * This avoids adding a pointer to the hashtable item.
21 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
22 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
23 * HI2PT() converts a hashitem pointer to a proptype pointer.
24 */
25#define PT2HIKEY(p) ((p)->pt_name)
26#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
27#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
28
29// The global text property types.
30static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010031static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33// The last used text property type ID.
34static int proptype_id = 0;
35
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036/*
37 * Find a property type by name, return the hashitem.
38 * Returns NULL if the item can't be found.
39 */
40 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010041find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010042{
43 hashtab_T *ht;
44 hashitem_T *hi;
45
46 if (*name == NUL)
47 return NULL;
48 if (buf == NULL)
49 ht = global_proptypes;
50 else
51 ht = buf->b_proptypes;
52
53 if (ht == NULL)
54 return NULL;
55 hi = hash_find(ht, name);
56 if (HASHITEM_EMPTY(hi))
57 return NULL;
58 return hi;
59}
60
61/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010062 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010063 */
64 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010065find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010066{
Bram Moolenaare44336b2022-08-07 18:20:08 +010067 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010068
69 if (hi == NULL)
70 return NULL;
71 return HI2PT(hi);
72}
73
74/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020075 * Get the prop type ID of "name".
76 * When not found return zero.
77 */
78 int
79find_prop_type_id(char_u *name, buf_T *buf)
80{
Bram Moolenaare44336b2022-08-07 18:20:08 +010081 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020082
83 if (pt == NULL)
84 return 0;
85 return pt->pt_id;
86}
87
88/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089 * Lookup a property type by name. First in "buf" and when not found in the
90 * global types.
91 * When not found gives an error message and returns NULL.
92 */
93 static proptype_T *
94lookup_prop_type(char_u *name, buf_T *buf)
95{
Bram Moolenaare44336b2022-08-07 18:20:08 +010096 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097
98 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +010099 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100101 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100152 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153 return;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200154
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100155 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
156 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157}
158
159/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200160 * Attach a text property 'type_name' to the text starting
161 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100162 * the buffer "buf" and assign identifier "id".
163 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200164 */
165 static int
166prop_add_one(
167 buf_T *buf,
168 char_u *type_name,
169 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100170 char_u *text_arg,
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100171 int text_padding_left,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100172 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200173 linenr_T start_lnum,
174 linenr_T end_lnum,
175 colnr_T start_col,
176 colnr_T end_col)
177{
178 proptype_T *type;
179 linenr_T lnum;
180 int proplen;
181 char_u *props = NULL;
182 char_u *newprops;
183 size_t textlen;
184 char_u *newtext;
185 int i;
186 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text = text_arg;
188 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189
190 type = lookup_prop_type(type_name, buf);
191 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200193
194 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
195 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000196 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200198 }
199 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
200 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000201 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200203 }
204
205 if (buf->b_ml.ml_mfp == NULL)
206 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000207 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
209 }
210
211 if (text != NULL)
212 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100213 garray_T *gap = &buf->b_textprop_text;
214 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100215
216 // double check we got the right ID
217 if (-id - 1 != gap->ga_len)
218 iemsg("text prop ID mismatch");
219 if (gap->ga_growsize == 0)
220 ga_init2(gap, sizeof(char *), 50);
221 if (ga_grow(gap, 1) == FAIL)
222 goto theend;
223 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100224
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100225 // change any control character (Tab, Newline, etc.) to a Space to make
226 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100227 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100228 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100230 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200231 }
232
233 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
234 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100235 colnr_T col; // start column use in tp_col
236 colnr_T sort_col; // column where it appears
237 long length; // in bytes
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200238
239 // Fetch the line to get the ml_line_len field updated.
240 proplen = get_text_props(buf, lnum, &props, TRUE);
241 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
242
243 if (lnum == start_lnum)
244 col = start_col;
245 else
246 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100247 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200248 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000249 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100250 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200251 }
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100252 sort_col = col;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200253
254 if (lnum == end_lnum)
255 length = end_col - col;
256 else
257 length = (int)textlen - col + 1;
258 if (length > (long)textlen)
259 length = (int)textlen; // can include the end-of-line
260 if (length < 0)
261 length = 0; // zero-width property
262
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100263 if (text_arg != NULL)
264 {
265 length = 1; // text is placed on one character
266 if (col == 0)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100267 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100268 col = MAXCOL; // before or after the line
269 if ((text_flags & TP_FLAG_ALIGN_ABOVE) == 0)
270 sort_col = MAXCOL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100271 length += text_padding_left;
272 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100273 }
274
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200275 // Allocate the new line with space for the new property.
276 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
277 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100278 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200279 // Copy the text, including terminating NUL.
280 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
281
282 // Find the index where to insert the new property.
283 // Since the text properties are not aligned properly when stored with
284 // the text, we need to copy them as bytes before using it as a struct.
285 for (i = 0; i < proplen; ++i)
286 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100287 colnr_T prop_col;
288
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200289 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
290 sizeof(textprop_T));
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100291 // col is MAXCOL when the text goes above or after the line, when
292 // above we should use column zero for sorting
293 prop_col = (tmp_prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
294 ? 0 : tmp_prop.tp_col;
295 if (prop_col >= sort_col)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200296 break;
297 }
298 newprops = newtext + textlen;
299 if (i > 0)
300 mch_memmove(newprops, props, sizeof(textprop_T) * i);
301
302 tmp_prop.tp_col = col;
303 tmp_prop.tp_len = length;
304 tmp_prop.tp_id = id;
305 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100306 tmp_prop.tp_flags = text_flags
307 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100308 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
309 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
310 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200311 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
312 sizeof(textprop_T));
313
314 if (i < proplen)
315 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
316 props + i * sizeof(textprop_T),
317 sizeof(textprop_T) * (proplen - i));
318
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100319 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200320 vim_free(buf->b_ml.ml_line_ptr);
321 buf->b_ml.ml_line_ptr = newtext;
322 buf->b_ml.ml_line_len += sizeof(textprop_T);
323 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
324 }
325
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100326 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200327 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100328 res = OK;
329
330theend:
331 vim_free(text);
332 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200333}
334
335/*
336 * prop_add_list()
337 * First argument specifies the text property:
338 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
339 * Second argument is a List where each item is a List with the following
340 * entries: [lnum, start_col, end_col]
341 */
342 void
343f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
344{
345 dict_T *dict;
346 char_u *type_name;
347 buf_T *buf = curbuf;
348 int id = 0;
349 listitem_T *li;
350 list_T *pos_list;
351 linenr_T start_lnum;
352 colnr_T start_col;
353 linenr_T end_lnum;
354 colnr_T end_col;
355 int error = FALSE;
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100356 int prev_did_emsg = did_emsg;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200357
358 if (check_for_dict_arg(argvars, 0) == FAIL
359 || check_for_list_arg(argvars, 1) == FAIL)
360 return;
361
Bram Moolenaard83392a2022-09-01 12:22:46 +0100362 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364
365 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100366 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200367 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000368 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200369 return;
370 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100371 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200372
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100373 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100374 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200375
376 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
377 return;
378
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100379 // This must be done _before_ we start adding properties because property
380 // changes trigger buffer (memline) reorganisation, which needs this flag
381 // to be correctly set.
382 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200383 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
384 {
385 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
386 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000387 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200388 return;
389 }
390
391 pos_list = li->li_tv.vval.v_list;
392 start_lnum = list_find_nr(pos_list, 0L, &error);
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100393 if (!error)
394 start_col = list_find_nr(pos_list, 1L, &error);
395 if (!error)
396 end_lnum = list_find_nr(pos_list, 2L, &error);
397 if (!error)
398 end_col = list_find_nr(pos_list, 3L, &error);
Bram Moolenaard93009e2022-10-13 14:35:24 +0100399 int this_id = id;
400 if (!error && pos_list->lv_len > 4)
401 this_id = list_find_nr(pos_list, 4L, &error);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200402 if (error || start_lnum <= 0 || start_col <= 0
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100403 || end_lnum <= 0 || end_col <= 0)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200404 {
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100405 if (prev_did_emsg == did_emsg)
406 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200407 return;
408 }
Bram Moolenaard93009e2022-10-13 14:35:24 +0100409 if (prop_add_one(buf, type_name, this_id, NULL, 0, 0,
410 start_lnum, end_lnum, start_col, end_col) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200411 return;
412 }
413
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100414 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200415}
416
417/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100418 * Get the next ID to use for a textprop with text in buffer "buf".
419 */
420 static int
421get_textprop_id(buf_T *buf)
422{
423 // TODO: recycle deleted entries
424 return -(buf->b_textprop_text.ga_len + 1);
425}
426
427/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200428 * Shared between prop_add() and popup_create().
429 * "dict_arg" is the function argument of a dict containing "bufnr".
430 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100431 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200432 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100433 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200434prop_add_common(
435 linenr_T start_lnum,
436 colnr_T start_col,
437 dict_T *dict,
438 buf_T *default_buf,
439 typval_T *dict_arg)
440{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200441 linenr_T end_lnum;
442 colnr_T end_col;
443 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200444 buf_T *buf = default_buf;
445 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100446 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100447 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100448 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100449
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100450 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100451 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000452 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100453 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100454 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100455 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100456
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100457 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100458 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100459 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100460 if (end_lnum < start_lnum)
461 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000462 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100463 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100465 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100466 else
467 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100468
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100469 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100470 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100471 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100472
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100473 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100474 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000475 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100476 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100477 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100478 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100479 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100480 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100481 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100482 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100483 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100484 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000485 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100486 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100487 }
488 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100489 else if (start_lnum == end_lnum)
490 end_col = start_col;
491 else
492 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100493
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100494 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100495 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100496
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100497 if (dict_has_key(dict, "text"))
498 {
Bram Moolenaarfb593c52022-09-17 18:57:36 +0100499 if (dict_has_key(dict, "length")
500 || dict_has_key(dict, "end_col")
501 || dict_has_key(dict, "end_lnum"))
502 {
503 emsg(_(e_cannot_use_length_endcol_and_endlnum_with_text));
504 goto theend;
505 }
506
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100507 text = dict_get_string(dict, "text", TRUE);
508 if (text == NULL)
509 goto theend;
510 // use a default length of 1 to make multiple props show up
511 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100512
513 if (dict_has_key(dict, "text_align"))
514 {
515 char_u *p = dict_get_string(dict, "text_align", FALSE);
516
517 if (p == NULL)
518 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100519 if (start_col != 0)
520 {
521 emsg(_(e_can_only_use_text_align_when_column_is_zero));
522 goto theend;
523 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100524 if (STRCMP(p, "right") == 0)
525 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100526 else if (STRCMP(p, "above") == 0)
527 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100528 else if (STRCMP(p, "below") == 0)
529 flags |= TP_FLAG_ALIGN_BELOW;
530 else if (STRCMP(p, "after") != 0)
531 {
532 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
533 goto theend;
534 }
535 }
536
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100537 if (dict_has_key(dict, "text_padding_left"))
538 {
539 text_padding_left = dict_get_number(dict, "text_padding_left");
540 if (text_padding_left < 0)
541 {
542 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
543 goto theend;
544 }
545 }
546
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100547 if (dict_has_key(dict, "text_wrap"))
548 {
549 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100550
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100551 if (p == NULL)
552 goto theend;
553 if (STRCMP(p, "wrap") == 0)
554 flags |= TP_FLAG_WRAP;
555 else if (STRCMP(p, "truncate") != 0)
556 {
557 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
558 goto theend;
559 }
560 }
561 }
562
563 // Column must be 1 or more for a normal text property; when "text" is
564 // present zero means it goes after the line.
565 if (start_col < (text == NULL ? 1 : 0))
566 {
567 semsg(_(e_invalid_column_number_nr), (long)start_col);
568 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100569 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100570 if (start_col > 0 && text_padding_left > 0)
571 {
572 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
573 goto theend;
574 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100575
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200576 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100577 goto theend;
578
579 if (id < 0 && buf->b_textprop_text.ga_len > 0)
580 {
581 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
582 goto theend;
583 }
584 if (text != NULL)
585 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100586
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100587 // This must be done _before_ we add the property because property changes
588 // trigger buffer (memline) reorganisation, which needs this flag to be
589 // correctly set.
590 buf->b_has_textprop = TRUE; // this is never reset
591
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100592 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100593 start_lnum, end_lnum, start_col, end_col);
594 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100595
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100596 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100597
598theend:
599 vim_free(text);
600 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100601}
602
603/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100604 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100605 * Returns the number of text properties and, when non-zero, a pointer to the
606 * first one in "props" (note that it is not aligned, therefore the char_u
607 * pointer).
608 */
609 int
610get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
611{
612 char_u *text;
613 size_t textlen;
614 size_t proplen;
615
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100616 // Be quick when no text property types have been defined or the buffer,
617 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200618 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100619 return 0;
620
621 // Fetch the line to get the ml_line_len field updated.
622 text = ml_get_buf(buf, lnum, will_change);
623 textlen = STRLEN(text) + 1;
624 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100625 if (proplen == 0)
626 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100627 if (proplen % sizeof(textprop_T) != 0)
628 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000629 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100630 return 0;
631 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100632 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100633 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100634}
635
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100636/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100637 * Return the number of text properties with "above" or "below" alignment in
638 * line "lnum". A "right" aligned property also goes below after a "below" or
639 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100640 */
641 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100642prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100643{
644 char_u *props;
645 int count = get_text_props(buf, lnum, &props, FALSE);
646 int result = 0;
647 textprop_T prop;
648 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100649 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100650
651 if (count == 0)
652 return 0;
653 for (i = 0; i < count; ++i)
654 {
655 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100656 if (prop.tp_col == MAXCOL)
657 {
658 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
659 || (next_right_goes_below
660 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
661 {
662 next_right_goes_below = TRUE;
663 ++result;
664 }
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100665 else if (prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
666 {
667 next_right_goes_below = FALSE;
668 ++result;
669 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100670 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
671 next_right_goes_below = TRUE;
672 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100673 }
674 return result;
675}
676
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200677/**
678 * Return the number of text properties on line "lnum" in the current buffer.
679 * When "only_starting" is true only text properties starting in this line will
680 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100681 * When "last_line" is FALSE then text properties after the line are not
682 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200683 */
684 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100685count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200686{
687 char_u *props;
688 int proplen = get_text_props(curbuf, lnum, &props, 0);
689 int result = proplen;
690 int i;
691 textprop_T prop;
692
Bram Moolenaare175dc62022-08-01 22:18:50 +0100693 for (i = 0; i < proplen; ++i)
694 {
695 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100696 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100697 // previous line, or when not in the last line and it is virtual text
698 // after the line.
699 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
700 || (!last_line && prop.tp_col == MAXCOL))
701 --result;
702 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200703 return result;
704}
705
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100706static textprop_T *text_prop_compare_props;
707static buf_T *text_prop_compare_buf;
708
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100709/* Score for sorting on position of the text property: 0: above,
710 * 1: after (default), 2: right, 3: below (comes last)
711 */
712 static int
713text_prop_order(int flags)
714{
715 if (flags & TP_FLAG_ALIGN_ABOVE)
716 return 0;
717 if (flags & TP_FLAG_ALIGN_RIGHT)
718 return 2;
719 if (flags & TP_FLAG_ALIGN_BELOW)
720 return 3;
721 return 1;
722}
723
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100724/*
725 * Function passed to qsort() to sort text properties.
726 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
727 * both have the same priority.
728 */
729 static int
730text_prop_compare(const void *s1, const void *s2)
731{
732 int idx1, idx2;
733 textprop_T *tp1, *tp2;
734 proptype_T *pt1, *pt2;
735 colnr_T col1, col2;
736
737 idx1 = *(int *)s1;
738 idx2 = *(int *)s2;
739 tp1 = &text_prop_compare_props[idx1];
740 tp2 = &text_prop_compare_props[idx2];
741 col1 = tp1->tp_col;
742 col2 = tp2->tp_col;
743 if (col1 == MAXCOL && col2 == MAXCOL)
744 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100745 int order1 = text_prop_order(tp1->tp_flags);
746 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100747
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100748 // both props add text before or after the line, sort on order where it
749 // is added
750 if (order1 != order2)
751 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100752 }
753
754 // property that inserts text has priority over one that doesn't
755 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
756 return tp1->tp_id < 0 ? 1 : -1;
757
758 // check highest priority, defined by the type
759 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
760 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
761 if (pt1 != pt2)
762 {
763 if (pt1 == NULL)
764 return -1;
765 if (pt2 == NULL)
766 return 1;
767 if (pt1->pt_priority != pt2->pt_priority)
768 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
769 }
770
771 // same priority, one that starts first wins
772 if (col1 != col2)
773 return col1 < col2 ? 1 : -1;
774
775 // for a property with text the id can be used as tie breaker
776 if (tp1->tp_id < 0)
777 return tp1->tp_id > tp2->tp_id ? 1 : -1;
778
779 return 0;
780}
781
782/*
783 * Sort "count" text properties using an array if indexes "idxs" into the list
784 * of text props "props" for buffer "buf".
785 */
786 void
787sort_text_props(
788 buf_T *buf,
789 textprop_T *props,
790 int *idxs,
791 int count)
792{
793 text_prop_compare_buf = buf;
794 text_prop_compare_props = props;
795 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
796}
797
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100798/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200799 * Find text property "type_id" in the visible lines of window "wp".
800 * Match "id" when it is > 0.
801 * Returns FAIL when not found.
802 */
803 int
804find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
805 linenr_T *found_lnum)
806{
807 linenr_T lnum;
808 char_u *props;
809 int count;
810 int i;
811
812 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100813 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200814 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
815 {
816 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
817 for (i = 0; i < count; ++i)
818 {
819 mch_memmove(prop, props + i * sizeof(textprop_T),
820 sizeof(textprop_T));
821 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
822 {
823 *found_lnum = lnum;
824 return OK;
825 }
826 }
827 }
828 return FAIL;
829}
830
831/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100832 * Set the text properties for line "lnum" to "props" with length "len".
833 * If "len" is zero text properties are removed, "props" is not used.
834 * Any existing text properties are dropped.
835 * Only works for the current buffer.
836 */
837 static void
838set_text_props(linenr_T lnum, char_u *props, int len)
839{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100840 char_u *text;
841 char_u *newtext;
842 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100843
844 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100845 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100846 newtext = alloc(textlen + len);
847 if (newtext == NULL)
848 return;
849 mch_memmove(newtext, text, textlen);
850 if (len > 0)
851 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100852 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100853 vim_free(curbuf->b_ml.ml_line_ptr);
854 curbuf->b_ml.ml_line_ptr = newtext;
855 curbuf->b_ml.ml_line_len = textlen + len;
856 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
857}
858
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100859/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100860 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100861 */
862 void
863add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
864{
865 char_u *text;
866 char_u *newtext;
867 int proplen = text_prop_count * (int)sizeof(textprop_T);
868
869 text = ml_get(lnum);
870 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
871 if (newtext == NULL)
872 return;
873 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
874 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
875 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
876 vim_free(curbuf->b_ml.ml_line_ptr);
877 curbuf->b_ml.ml_line_ptr = newtext;
878 curbuf->b_ml.ml_line_len += proplen;
879 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
880}
881
Bram Moolenaare44336b2022-08-07 18:20:08 +0100882/*
883 * Function passed to qsort() for sorting proptype_T on pt_id.
884 */
885 static int
886compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100887{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100888 proptype_T *tp1 = *(proptype_T **)s1;
889 proptype_T *tp2 = *(proptype_T **)s2;
890
891 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
892}
893
894 static proptype_T *
895find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
896{
897 int low = 0;
898 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100899
Bram Moolenaar10246902022-08-08 17:08:05 +0100900 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100901 return NULL;
902
Bram Moolenaare44336b2022-08-07 18:20:08 +0100903 // Make the loopup faster by creating an array with pointers to
904 // hashtable entries, sorted on pt_id.
905 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100906 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100907 long todo;
908 hashitem_T *hi;
909 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100910
Bram Moolenaare44336b2022-08-07 18:20:08 +0100911 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
912 if (*array == NULL)
913 return NULL;
914 todo = (long)ht->ht_used;
915 for (hi = ht->ht_array; todo > 0; ++hi)
916 {
917 if (!HASHITEM_EMPTY(hi))
918 {
919 (*array)[i++] = HI2PT(hi);
920 --todo;
921 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100922 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100923 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
924 }
925
926 // binary search in the sorted array
927 high = ht->ht_used;
928 while (high > low)
929 {
930 int m = (high + low) / 2;
931
932 if ((*array)[m]->pt_id == id)
933 return (*array)[m];
934 if ((*array)[m]->pt_id > id)
935 high = m;
936 else
937 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100938 }
939 return NULL;
940}
941
942/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100943 * Fill 'dict' with text properties in 'prop'.
944 */
945 static void
946prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
947{
948 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200949 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100950
951 dict_add_number(dict, "col", prop->tp_col);
952 dict_add_number(dict, "length", prop->tp_len);
953 dict_add_number(dict, "id", prop->tp_id);
954 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
955 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200956
Bram Moolenaare44336b2022-08-07 18:20:08 +0100957 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200958 if (pt == NULL)
959 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100960 pt = find_type_by_id(global_proptypes, &global_proparray,
961 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200962 buflocal = FALSE;
963 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100964 if (pt != NULL)
965 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200966
967 if (buflocal)
968 dict_add_number(dict, "type_bufnr", buf->b_fnum);
969 else
970 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100971}
972
973/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100974 * Find a property type by ID in "buf" or globally.
975 * Returns NULL if not found.
976 */
977 proptype_T *
978text_prop_type_by_id(buf_T *buf, int id)
979{
980 proptype_T *type;
981
Bram Moolenaare44336b2022-08-07 18:20:08 +0100982 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100983 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100984 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100985 return type;
986}
987
988/*
989 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
990 */
991 void
992f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
993{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200994 linenr_T start;
995 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100996 linenr_T lnum;
997 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100998 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100999
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001000 if (in_vim9script()
1001 && (check_for_number_arg(argvars, 0) == FAIL
1002 || check_for_opt_number_arg(argvars, 1) == FAIL
1003 || (argvars[1].v_type != VAR_UNKNOWN
1004 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1005 return;
1006
1007 start = tv_get_number(&argvars[0]);
1008 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001009 if (argvars[1].v_type != VAR_UNKNOWN)
1010 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001011 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001012 if (argvars[2].v_type != VAR_UNKNOWN)
1013 {
1014 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
1015 return;
1016 }
1017 }
1018 if (start < 1 || end < 1)
1019 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001020 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001021 return;
1022 }
1023
1024 for (lnum = start; lnum <= end; ++lnum)
1025 {
1026 char_u *text;
1027 size_t len;
1028
1029 if (lnum > buf->b_ml.ml_line_count)
1030 break;
1031 text = ml_get_buf(buf, lnum, FALSE);
1032 len = STRLEN(text) + 1;
1033 if ((size_t)buf->b_ml.ml_line_len > len)
1034 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001035 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001036 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1037 {
1038 char_u *newtext = vim_strsave(text);
1039
1040 // need to allocate the line now
1041 if (newtext == NULL)
1042 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001043 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1044 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001045 buf->b_ml.ml_line_ptr = newtext;
1046 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1047 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001048 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001049 }
1050 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001051 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001052 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001053}
1054
1055/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001056 * prop_find({props} [, {direction}])
1057 */
1058 void
1059f_prop_find(typval_T *argvars, typval_T *rettv)
1060{
1061 pos_T *cursor = &curwin->w_cursor;
1062 dict_T *dict;
1063 buf_T *buf = curbuf;
1064 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001065 int lnum_start;
1066 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001067 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001068 int id = 0;
1069 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001070 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001071 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001072 int lnum = -1;
1073 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001074 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001075 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001076
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001077 if (in_vim9script()
1078 && (check_for_dict_arg(argvars, 0) == FAIL
1079 || check_for_opt_string_arg(argvars, 1) == FAIL))
1080 return;
1081
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001082 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001083 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001084 dict = argvars[0].vval.v_dict;
1085
1086 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1087 return;
1088 if (buf->b_ml.ml_mfp == NULL)
1089 return;
1090
1091 if (argvars[1].v_type != VAR_UNKNOWN)
1092 {
1093 char_u *dir_s = tv_get_string(&argvars[1]);
1094
1095 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001096 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001097 else if (*dir_s != 'f')
1098 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001099 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001100 return;
1101 }
1102 }
1103
1104 di = dict_find(dict, (char_u *)"lnum", -1);
1105 if (di != NULL)
1106 lnum = tv_get_number(&di->di_tv);
1107
1108 di = dict_find(dict, (char_u *)"col", -1);
1109 if (di != NULL)
1110 col = tv_get_number(&di->di_tv);
1111
1112 if (lnum == -1)
1113 {
1114 lnum = cursor->lnum;
1115 col = cursor->col + 1;
1116 }
1117 else if (col == -1)
1118 col = 1;
1119
1120 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1121 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001122 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001123 return;
1124 }
1125
Bram Moolenaard61efa52022-07-23 09:52:04 +01001126 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001127
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001128 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001129 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001130 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001131 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001132 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001133 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001134 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001135 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001136 proptype_T *type = lookup_prop_type(name, buf);
1137
1138 if (type == NULL)
1139 return;
1140 type_id = type->pt_id;
1141 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001142 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001143 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001144 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001145 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001146 return;
1147 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001148 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001149 {
Ben Jacksona7704222022-08-20 20:54:51 +01001150 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001151 return;
1152 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001153
1154 lnum_start = lnum;
1155
1156 if (rettv_dict_alloc(rettv) == FAIL)
1157 return;
1158
1159 while (1)
1160 {
1161 char_u *text = ml_get_buf(buf, lnum, FALSE);
1162 size_t textlen = STRLEN(text) + 1;
1163 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001164 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001165 int i;
1166 textprop_T prop;
1167 int prop_start;
1168 int prop_end;
1169
LemonBoy9bd3ce22022-04-18 21:54:02 +01001170 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001171 {
1172 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001173 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001174
LemonBoy9bd3ce22022-04-18 21:54:02 +01001175 // For the very first line try to find the first property before or
1176 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001177 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001178 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001179 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001180 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001181 if (prop.tp_col > col)
1182 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001183 }
1184 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1185 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001186 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001187 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001188 : (id_found && prop.tp_id == id)
1189 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001190 {
1191 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001192 if (lnum_start == lnum
1193 && col >= prop.tp_col
1194 && (col <= prop.tp_col + prop.tp_len
1195 - (prop.tp_len != 0)))
1196 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001197
LemonBoy9bd3ce22022-04-18 21:54:02 +01001198 // The property was not continued from last line, it starts on
1199 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001200 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001201 // The property does not continue on the next line, it ends on
1202 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001203 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001204 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001205 seen_end = 1;
1206
1207 // Skip lines without the start flag.
1208 if (!prop_start)
1209 {
1210 // Always search backwards for start when search started
1211 // on a prop and we're not skipping.
1212 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001213 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001214 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001215 }
1216
1217 // If skipstart is true, skip the prop at start pos (even if
1218 // continued from another line).
1219 if (start_pos_has_prop && skipstart && !seen_end)
1220 {
1221 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001222 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001223 }
1224
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001225 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1226 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1227
1228 return;
1229 }
1230 }
1231
1232 if (dir > 0)
1233 {
1234 if (lnum >= buf->b_ml.ml_line_count)
1235 break;
1236 lnum++;
1237 }
1238 else
1239 {
1240 if (lnum <= 1)
1241 break;
1242 lnum--;
1243 }
1244 }
1245}
1246
1247/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001248 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1249 */
1250 static int
1251prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1252{
1253 int i;
1254
1255 for (i = 0; i < len; i++)
1256 if (types_or_ids[i] == type_or_id)
1257 return TRUE;
1258
1259 return FALSE;
1260}
1261
1262/*
1263 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1264 * If 'prop_types' is not NULL, then return only the text properties with
1265 * matching property type in the 'prop_types' array.
1266 * If 'prop_ids' is not NULL, then return only the text properties with
1267 * an identifier in the 'props_ids' array.
1268 * If 'add_lnum' is TRUE, then add the line number also to the text property
1269 * dictionary.
1270 */
1271 static void
1272get_props_in_line(
1273 buf_T *buf,
1274 linenr_T lnum,
1275 int *prop_types,
1276 int prop_types_len,
1277 int *prop_ids,
1278 int prop_ids_len,
1279 list_T *retlist,
1280 int add_lnum)
1281{
1282 char_u *text = ml_get_buf(buf, lnum, FALSE);
1283 size_t textlen = STRLEN(text) + 1;
1284 int count;
1285 int i;
1286 textprop_T prop;
1287
1288 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1289 for (i = 0; i < count; ++i)
1290 {
1291 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1292 sizeof(textprop_T));
1293 if ((prop_types == NULL
1294 || prop_type_or_id_in_list(prop_types, prop_types_len,
1295 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001296 && (prop_ids == NULL
1297 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1298 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001299 {
1300 dict_T *d = dict_alloc();
1301
1302 if (d == NULL)
1303 break;
1304 prop_fill_dict(d, &prop, buf);
1305 if (add_lnum)
1306 dict_add_number(d, "lnum", lnum);
1307 list_append_dict(retlist, d);
1308 }
1309 }
1310}
1311
1312/*
1313 * Convert a List of property type names into an array of property type
1314 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1315 * error. 'num_types' is set to the number of returned property types.
1316 */
1317 static int *
1318get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1319{
1320 int *prop_types;
1321 listitem_T *li;
1322 int i;
1323 char_u *name;
1324 proptype_T *type;
1325
1326 *num_types = 0;
1327
1328 prop_types = ALLOC_MULT(int, list_len(l));
1329 if (prop_types == NULL)
1330 return NULL;
1331
1332 i = 0;
1333 FOR_ALL_LIST_ITEMS(l, li)
1334 {
1335 if (li->li_tv.v_type != VAR_STRING)
1336 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001337 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001338 goto errret;
1339 }
1340 name = li->li_tv.vval.v_string;
1341 if (name == NULL)
1342 goto errret;
1343
1344 type = lookup_prop_type(name, buf);
1345 if (type == NULL)
1346 goto errret;
1347 prop_types[i++] = type->pt_id;
1348 }
1349
1350 *num_types = i;
1351 return prop_types;
1352
1353errret:
1354 VIM_CLEAR(prop_types);
1355 return NULL;
1356}
1357
1358/*
1359 * Convert a List of property identifiers into an array of property
1360 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1361 * error. 'num_ids' is set to the number of returned property identifiers.
1362 */
1363 static int *
1364get_prop_ids_from_list(list_T *l, int *num_ids)
1365{
1366 int *prop_ids;
1367 listitem_T *li;
1368 int i;
1369 int id;
1370 int error;
1371
1372 *num_ids = 0;
1373
1374 prop_ids = ALLOC_MULT(int, list_len(l));
1375 if (prop_ids == NULL)
1376 return NULL;
1377
1378 i = 0;
1379 FOR_ALL_LIST_ITEMS(l, li)
1380 {
1381 error = FALSE;
1382 id = tv_get_number_chk(&li->li_tv, &error);
1383 if (error)
1384 goto errret;
1385
1386 prop_ids[i++] = id;
1387 }
1388
1389 *num_ids = i;
1390 return prop_ids;
1391
1392errret:
1393 VIM_CLEAR(prop_ids);
1394 return NULL;
1395}
1396
1397/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001398 * prop_list({lnum} [, {bufnr}])
1399 */
1400 void
1401f_prop_list(typval_T *argvars, typval_T *rettv)
1402{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001403 linenr_T lnum;
1404 linenr_T start_lnum;
1405 linenr_T end_lnum;
1406 buf_T *buf = curbuf;
1407 int add_lnum = FALSE;
1408 int *prop_types = NULL;
1409 int prop_types_len = 0;
1410 int *prop_ids = NULL;
1411 int prop_ids_len = 0;
1412 list_T *l;
1413 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001414
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001415 if (in_vim9script()
1416 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001417 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001418 return;
1419
Bram Moolenaar93a10962022-06-16 11:42:09 +01001420 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001421 return;
1422
1423 // default: get text properties on current line
1424 start_lnum = tv_get_number(&argvars[0]);
1425 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001426 if (argvars[1].v_type != VAR_UNKNOWN)
1427 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001428 dict_T *d;
1429
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001430 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001431 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001432 d = argvars[1].vval.v_dict;
1433
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001434 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1435 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001436
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001437 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001438 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001439 if (di->di_tv.v_type != VAR_NUMBER)
1440 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001441 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001442 return;
1443 }
1444 end_lnum = tv_get_number(&di->di_tv);
1445 if (end_lnum < 0)
1446 // negative end_lnum is used as an offset from the last buffer
1447 // line
1448 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1449 else if (end_lnum > buf->b_ml.ml_line_count)
1450 end_lnum = buf->b_ml.ml_line_count;
1451 add_lnum = TRUE;
1452 }
1453 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1454 {
1455 if (di->di_tv.v_type != VAR_LIST)
1456 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001457 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001458 return;
1459 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001460
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001461 l = di->di_tv.vval.v_list;
1462 if (l != NULL && list_len(l) > 0)
1463 {
1464 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1465 if (prop_types == NULL)
1466 return;
1467 }
1468 }
1469 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1470 {
1471 if (di->di_tv.v_type != VAR_LIST)
1472 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001473 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001474 goto errret;
1475 }
1476
1477 l = di->di_tv.vval.v_list;
1478 if (l != NULL && list_len(l) > 0)
1479 {
1480 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1481 if (prop_ids == NULL)
1482 goto errret;
1483 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001484 }
1485 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001486 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1487 || end_lnum < 1 || end_lnum < start_lnum)
1488 emsg(_(e_invalid_range));
1489 else
1490 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1491 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1492 prop_ids, prop_ids_len,
1493 rettv->vval.v_list, add_lnum);
1494
1495errret:
1496 VIM_CLEAR(prop_types);
1497 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001498}
1499
1500/*
1501 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1502 */
1503 void
1504f_prop_remove(typval_T *argvars, typval_T *rettv)
1505{
1506 linenr_T start = 1;
1507 linenr_T end = 0;
1508 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001509 linenr_T first_changed = 0;
1510 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001511 dict_T *dict;
1512 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001513 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001514 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001515 int type_id = -1; // for a single "type"
1516 int *type_ids = NULL; // array, for a list of "types", allocated
1517 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001518 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001519 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001520
1521 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001522
1523 if (in_vim9script()
1524 && (check_for_dict_arg(argvars, 0) == FAIL
1525 || check_for_opt_number_arg(argvars, 1) == FAIL
1526 || (argvars[1].v_type != VAR_UNKNOWN
1527 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1528 return;
1529
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001530 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001531 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001532
1533 if (argvars[1].v_type != VAR_UNKNOWN)
1534 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001535 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001536 end = start;
1537 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001538 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001539 if (start < 1 || end < 1)
1540 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001541 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001542 return;
1543 }
1544 }
1545
1546 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001547 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1548 return;
1549 if (buf->b_ml.ml_mfp == NULL)
1550 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001551
Bram Moolenaard61efa52022-07-23 09:52:04 +01001552 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001553
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001554 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001555 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001556
1557 // if a specific type was supplied "type": check that (and ignore "types".
1558 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001559 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001560 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001561 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001562 proptype_T *type = lookup_prop_type(name, buf);
1563
1564 if (type == NULL)
1565 return;
1566 type_id = type->pt_id;
1567 }
Ben Jacksona7704222022-08-20 20:54:51 +01001568 if (dict_has_key(dict, "types"))
1569 {
1570 typval_T types;
1571 listitem_T *li = NULL;
1572
1573 dict_get_tv(dict, "types", &types);
1574 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1575 {
1576 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1577
1578 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1579 {
1580 proptype_T *prop_type;
1581
1582 if (li->li_tv.v_type != VAR_STRING)
1583 continue;
1584
1585 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1586
1587 if (!prop_type)
1588 goto cleanup_prop_remove;
1589
1590 type_ids[num_type_ids++] = prop_type->pt_id;
1591 }
1592 }
1593 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001594 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001595
Ben Jacksona7704222022-08-20 20:54:51 +01001596 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001597 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001598 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001599 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001600 }
Ben Jacksona7704222022-08-20 20:54:51 +01001601 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001602 {
Ben Jacksona7704222022-08-20 20:54:51 +01001603 emsg(_(e_need_id_and_type_or_types_with_both));
1604 goto cleanup_prop_remove;
1605 }
1606 if (type_id != -1 && num_type_ids > 0)
1607 {
1608 emsg(_(e_cannot_specify_both_type_and_types));
1609 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001610 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001611
1612 if (end == 0)
1613 end = buf->b_ml.ml_line_count;
1614 for (lnum = start; lnum <= end; ++lnum)
1615 {
1616 char_u *text;
1617 size_t len;
1618
1619 if (lnum > buf->b_ml.ml_line_count)
1620 break;
1621 text = ml_get_buf(buf, lnum, FALSE);
1622 len = STRLEN(text) + 1;
1623 if ((size_t)buf->b_ml.ml_line_len > len)
1624 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001625 static textprop_T textprop; // static because of alignment
1626 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001627
1628 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1629 / sizeof(textprop_T); ++idx)
1630 {
1631 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1632 + idx * sizeof(textprop_T);
1633 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001634 int matches_id = 0;
1635 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001636
1637 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001638
1639 matches_id = textprop.tp_id == id;
1640 if (num_type_ids > 0)
1641 {
1642 int idx2;
1643
1644 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1645 matches_type = textprop.tp_type == type_ids[idx2];
1646 }
1647 else
1648 {
1649 matches_type = textprop.tp_type == type_id;
1650 }
1651
1652 if (both ? matches_id && matches_type
1653 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001654 {
1655 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1656 {
1657 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1658
1659 // need to allocate the line to be able to change it
1660 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001661 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001662 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1663 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001664 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1665 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001666 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001667 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1668
1669 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001670 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001671 }
1672
1673 taillen = buf->b_ml.ml_line_len - len
1674 - (idx + 1) * sizeof(textprop_T);
1675 if (taillen > 0)
1676 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1677 taillen);
1678 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1679 --idx;
1680
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001681 if (textprop.tp_id < 0)
1682 {
1683 garray_T *gap = &buf->b_textprop_text;
1684 int ii = -textprop.tp_id - 1;
1685
1686 // negative ID: property with text - free the text
1687 if (ii < gap->ga_len)
1688 {
1689 char_u **p = ((char_u **)gap->ga_data) + ii;
1690 vim_free(*p);
1691 *p = NULL;
1692 did_remove_text = TRUE;
1693 }
1694 }
1695
Bram Moolenaar965c0442021-05-17 00:22:06 +02001696 if (first_changed == 0)
1697 first_changed = lnum;
1698 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001699 ++rettv->vval.v_number;
1700 if (!do_all)
1701 break;
1702 }
1703 }
1704 }
1705 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001706
Bram Moolenaar965c0442021-05-17 00:22:06 +02001707 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001708 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001709 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001710 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001711 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001712 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001713
1714 if (did_remove_text)
1715 {
1716 garray_T *gap = &buf->b_textprop_text;
1717
1718 // Reduce the growarray size for NULL pointers at the end.
1719 while (gap->ga_len > 0
1720 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1721 --gap->ga_len;
1722 }
Ben Jacksona7704222022-08-20 20:54:51 +01001723
1724cleanup_prop_remove:
1725 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001726}
1727
1728/*
1729 * Common for f_prop_type_add() and f_prop_type_change().
1730 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001731 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001732prop_type_set(typval_T *argvars, int add)
1733{
1734 char_u *name;
1735 buf_T *buf = NULL;
1736 dict_T *dict;
1737 dictitem_T *di;
1738 proptype_T *prop;
1739
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001740 if (in_vim9script()
1741 && (check_for_string_arg(argvars, 0) == FAIL
1742 || check_for_dict_arg(argvars, 1) == FAIL))
1743 return;
1744
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001745 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001746 if (*name == NUL)
1747 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001748 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001749 return;
1750 }
1751
1752 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1753 return;
1754 dict = argvars[1].vval.v_dict;
1755
Bram Moolenaare44336b2022-08-07 18:20:08 +01001756 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001757 if (add)
1758 {
1759 hashtab_T **htp;
1760
1761 if (prop != NULL)
1762 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001763 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001764 return;
1765 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001766 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001767 if (prop == NULL)
1768 return;
1769 STRCPY(prop->pt_name, name);
1770 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001771 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001772 if (buf == NULL)
1773 {
1774 htp = &global_proptypes;
1775 VIM_CLEAR(global_proparray);
1776 }
1777 else
1778 {
1779 htp = &buf->b_proptypes;
1780 VIM_CLEAR(buf->b_proparray);
1781 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001782 if (*htp == NULL)
1783 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001784 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001785 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001786 {
1787 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001788 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001789 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001790 hash_init(*htp);
1791 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001792 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001793 }
1794 else
1795 {
1796 if (prop == NULL)
1797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001798 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001799 return;
1800 }
1801 }
1802
1803 if (dict != NULL)
1804 {
1805 di = dict_find(dict, (char_u *)"highlight", -1);
1806 if (di != NULL)
1807 {
1808 char_u *highlight;
1809 int hl_id = 0;
1810
Bram Moolenaard61efa52022-07-23 09:52:04 +01001811 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001812 if (highlight != NULL && *highlight != NUL)
1813 hl_id = syn_name2id(highlight);
1814 if (hl_id <= 0)
1815 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001816 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001817 highlight == NULL ? (char_u *)"" : highlight);
1818 return;
1819 }
1820 prop->pt_hl_id = hl_id;
1821 }
1822
Bram Moolenaar58187f12019-05-05 16:33:47 +02001823 di = dict_find(dict, (char_u *)"combine", -1);
1824 if (di != NULL)
1825 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001826 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001827 prop->pt_flags |= PT_FLAG_COMBINE;
1828 else
1829 prop->pt_flags &= ~PT_FLAG_COMBINE;
1830 }
1831
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001832 di = dict_find(dict, (char_u *)"override", -1);
1833 if (di != NULL)
1834 {
1835 if (tv_get_bool(&di->di_tv))
1836 prop->pt_flags |= PT_FLAG_OVERRIDE;
1837 else
1838 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1839 }
1840
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001841 di = dict_find(dict, (char_u *)"priority", -1);
1842 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001843 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001844
1845 di = dict_find(dict, (char_u *)"start_incl", -1);
1846 if (di != NULL)
1847 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001848 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001849 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1850 else
1851 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1852 }
1853
1854 di = dict_find(dict, (char_u *)"end_incl", -1);
1855 if (di != NULL)
1856 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001857 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001858 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1859 else
1860 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1861 }
1862 }
1863}
1864
1865/*
1866 * prop_type_add({name}, {props})
1867 */
1868 void
1869f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1870{
1871 prop_type_set(argvars, TRUE);
1872}
1873
1874/*
1875 * prop_type_change({name}, {props})
1876 */
1877 void
1878f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1879{
1880 prop_type_set(argvars, FALSE);
1881}
1882
1883/*
1884 * prop_type_delete({name} [, {bufnr}])
1885 */
1886 void
1887f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1888{
1889 char_u *name;
1890 buf_T *buf = NULL;
1891 hashitem_T *hi;
1892
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001893 if (in_vim9script()
1894 && (check_for_string_arg(argvars, 0) == FAIL
1895 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1896 return;
1897
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001898 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001899 if (*name == NUL)
1900 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001901 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001902 return;
1903 }
1904
1905 if (argvars[1].v_type != VAR_UNKNOWN)
1906 {
1907 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1908 return;
1909 }
1910
Bram Moolenaare44336b2022-08-07 18:20:08 +01001911 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001912 if (hi != NULL)
1913 {
1914 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001915 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001916
1917 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001918 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001919 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001920 VIM_CLEAR(global_proparray);
1921 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001922 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001923 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001924 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001925 VIM_CLEAR(buf->b_proparray);
1926 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001927 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001928 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001929 }
1930}
1931
1932/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001933 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001934 */
1935 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001936f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001937{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001938 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001939
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001940 if (in_vim9script()
1941 && (check_for_string_arg(argvars, 0) == FAIL
1942 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1943 return;
1944
1945 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001946 if (*name == NUL)
1947 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001948 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001949 return;
1950 }
1951 if (rettv_dict_alloc(rettv) == OK)
1952 {
1953 proptype_T *prop = NULL;
1954 buf_T *buf = NULL;
1955
1956 if (argvars[1].v_type != VAR_UNKNOWN)
1957 {
1958 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1959 return;
1960 }
1961
Bram Moolenaare44336b2022-08-07 18:20:08 +01001962 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001963 if (prop != NULL)
1964 {
1965 dict_T *d = rettv->vval.v_dict;
1966
1967 if (prop->pt_hl_id > 0)
1968 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1969 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001970 dict_add_number(d, "combine",
1971 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001972 dict_add_number(d, "start_incl",
1973 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1974 dict_add_number(d, "end_incl",
1975 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1976 if (buf != NULL)
1977 dict_add_number(d, "bufnr", buf->b_fnum);
1978 }
1979 }
1980}
1981
1982 static void
1983list_types(hashtab_T *ht, list_T *l)
1984{
1985 long todo;
1986 hashitem_T *hi;
1987
1988 todo = (long)ht->ht_used;
1989 for (hi = ht->ht_array; todo > 0; ++hi)
1990 {
1991 if (!HASHITEM_EMPTY(hi))
1992 {
1993 proptype_T *prop = HI2PT(hi);
1994
1995 list_append_string(l, prop->pt_name, -1);
1996 --todo;
1997 }
1998 }
1999}
2000
2001/*
2002 * prop_type_list([{bufnr}])
2003 */
2004 void
2005f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
2006{
2007 buf_T *buf = NULL;
2008
2009 if (rettv_list_alloc(rettv) == OK)
2010 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002011 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
2012 return;
2013
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002014 if (argvars[0].v_type != VAR_UNKNOWN)
2015 {
2016 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2017 return;
2018 }
2019 if (buf == NULL)
2020 {
2021 if (global_proptypes != NULL)
2022 list_types(global_proptypes, rettv->vval.v_list);
2023 }
2024 else if (buf->b_proptypes != NULL)
2025 list_types(buf->b_proptypes, rettv->vval.v_list);
2026 }
2027}
2028
2029/*
2030 * Free all property types in "ht".
2031 */
2032 static void
2033clear_ht_prop_types(hashtab_T *ht)
2034{
2035 long todo;
2036 hashitem_T *hi;
2037
2038 if (ht == NULL)
2039 return;
2040
2041 todo = (long)ht->ht_used;
2042 for (hi = ht->ht_array; todo > 0; ++hi)
2043 {
2044 if (!HASHITEM_EMPTY(hi))
2045 {
2046 proptype_T *prop = HI2PT(hi);
2047
2048 vim_free(prop);
2049 --todo;
2050 }
2051 }
2052
2053 hash_clear(ht);
2054 vim_free(ht);
2055}
2056
2057#if defined(EXITFREE) || defined(PROTO)
2058/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002059 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002060 */
2061 void
2062clear_global_prop_types(void)
2063{
2064 clear_ht_prop_types(global_proptypes);
2065 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002066 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002067}
2068#endif
2069
2070/*
2071 * Free all property types for "buf".
2072 */
2073 void
2074clear_buf_prop_types(buf_T *buf)
2075{
2076 clear_ht_prop_types(buf->b_proptypes);
2077 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002078 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002079}
2080
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002081// Struct used to return two values from adjust_prop().
2082typedef struct
2083{
2084 int dirty; // if the property was changed
2085 int can_drop; // whether after this change, the prop may be removed
2086} adjustres_T;
2087
2088/*
2089 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2090 *
2091 * Note that "col" is zero-based, while tp_col is one-based.
2092 * Only for the current buffer.
2093 * "flags" can have:
2094 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002095 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002096 */
2097 static adjustres_T
2098adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002099 textprop_T *prop,
2100 colnr_T col,
2101 int added,
2102 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002103{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002104 proptype_T *pt;
2105 int start_incl;
2106 int end_incl;
2107 int droppable;
2108 adjustres_T res = {TRUE, FALSE};
2109
2110 // prop after end of the line doesn't move
2111 if (prop->tp_col == MAXCOL)
2112 {
2113 res.dirty = FALSE;
2114 return res;
2115 }
2116
2117 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2118 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002119 || (flags & APC_SUBSTITUTE)
2120 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002121 if (prop->tp_id < 0 && (flags & APC_INDENT))
2122 // when inserting indent just before a character with virtual text
2123 // shift the text property
2124 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002125 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002126 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002127 // do not drop zero-width props if they later can increase in size
2128 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002129
2130 if (added > 0)
2131 {
2132 if (col + 1 <= prop->tp_col
2133 - (start_incl || (prop->tp_len == 0 && end_incl)))
2134 // Change is entirely before the text property: Only shift
2135 prop->tp_col += added;
2136 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2137 // Insertion was inside text property
2138 prop->tp_len += added;
2139 }
2140 else if (prop->tp_col > col + 1)
2141 {
2142 if (prop->tp_col + added < col + 1)
2143 {
2144 prop->tp_len += (prop->tp_col - 1 - col) + added;
2145 prop->tp_col = col + 1;
2146 if (prop->tp_len <= 0)
2147 {
2148 prop->tp_len = 0;
2149 res.can_drop = droppable;
2150 }
2151 }
2152 else
2153 prop->tp_col += added;
2154 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002155 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2156 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002157 {
2158 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2159
2160 prop->tp_len += after > 0 ? added + after : added;
2161 res.can_drop = prop->tp_len <= 0 && droppable;
2162 }
2163 else
2164 res.dirty = FALSE;
2165
2166 return res;
2167}
2168
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002169/*
2170 * Adjust the columns of text properties in line "lnum" after position "col" to
2171 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002172 * Note that "col" is zero-based, while tp_col is one-based.
2173 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002174 * "flags" can have:
2175 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2176 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002177 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002178 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002179 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002180 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002181 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002182adjust_prop_columns(
2183 linenr_T lnum,
2184 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002185 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002186 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002187{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002188 int proplen;
2189 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002190 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002191 int ri, wi;
2192 size_t textlen;
2193
2194 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002195 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002196
2197 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2198 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002199 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002200 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002201
Bram Moolenaar196d1572019-01-02 23:47:18 +01002202 wi = 0; // write index
2203 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002204 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002205 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002206 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002207
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002208 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2209 res = adjust_prop(&prop, col, bytes_added, flags);
2210 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002211 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002212 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002213 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2214 && u_savesub(lnum) == FAIL)
2215 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002216 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002217
2218 // u_savesub() may have updated curbuf->b_ml, fetch it again
2219 if (curbuf->b_ml.ml_line_lnum != lnum)
2220 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002221 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002222 if (res.can_drop)
2223 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002224 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002225 ++wi;
2226 }
2227 if (dirty)
2228 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002229 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2230
2231 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002232 {
2233 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2234
2235 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2236 vim_free(curbuf->b_ml.ml_line_ptr);
2237 curbuf->b_ml.ml_line_ptr = p;
2238 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002239 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002240 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002241 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002242 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002243}
2244
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002245/*
2246 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002247 * "lnum_props" is the line that has the properties from before the split.
2248 * "lnum_top" is the top line.
2249 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002250 * "deleted" is the number of bytes deleted.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002251 * "at_eol" is true if the split is after the end of the line.
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002252 */
2253 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002254adjust_props_for_split(
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002255 linenr_T lnum_props,
2256 linenr_T lnum_top,
2257 int kept,
2258 int deleted,
2259 int at_eol)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002260{
2261 char_u *props;
2262 int count;
2263 garray_T prevprop;
2264 garray_T nextprop;
2265 int i;
2266 int skipped = kept + deleted;
2267
2268 if (!curbuf->b_has_textprop)
2269 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002270
2271 // Get the text properties from "lnum_props".
2272 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002273 ga_init2(&prevprop, sizeof(textprop_T), 10);
2274 ga_init2(&nextprop, sizeof(textprop_T), 10);
2275
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002276 // Keep the relevant ones in the first line, reducing the length if needed.
2277 // Copy the ones that include the split to the second line.
2278 // Move the ones after the split to the second line.
2279 for (i = 0; i < count; ++i)
2280 {
2281 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002282 proptype_T *pt;
2283 int start_incl, end_incl;
2284 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002285 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002286
2287 // copy the prop to an aligned structure
2288 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2289
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002290 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2291 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2292 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002293
2294 // a text prop "above" behaves like it is on the first text column
2295 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2296
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002297 if (prop_col == MAXCOL)
2298 {
2299 cont_prev = at_eol;
2300 cont_next = !at_eol;
2301 }
2302 else
2303 {
2304 cont_prev = prop_col + !start_incl <= kept;
2305 cont_next = skipped <= prop_col + prop.tp_len - !end_incl;
2306 }
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002307 // when a prop has text it is never copied
2308 if (prop.tp_id < 0 && cont_next)
2309 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002310
2311 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002312 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002313 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2314
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002315 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002316 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002317 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002318 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002319 if (cont_next)
2320 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002321 }
2322
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002323 // Only add the property to the next line if the length is bigger than
2324 // zero.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002325 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002326 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002327 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002328
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002329 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002330 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002331 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002332 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002333 if (p->tp_col > skipped)
2334 p->tp_col -= skipped - 1;
2335 else
2336 {
2337 p->tp_len -= skipped - p->tp_col;
2338 p->tp_col = 1;
2339 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002340 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002341 if (cont_prev)
2342 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002343 }
2344 }
2345
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002346 set_text_props(lnum_top, prevprop.ga_data,
2347 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002348 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002349 set_text_props(lnum_top + 1, nextprop.ga_data,
2350 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002351 ga_clear(&nextprop);
2352}
2353
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002354/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002355 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002356 */
2357 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002358prepend_joined_props(
2359 char_u *new_props,
2360 int propcount,
2361 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002362 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002363 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002364 long col,
2365 int removed)
2366{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002367 char_u *props;
2368 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2369 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002370
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002371 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002372 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002373 textprop_T prop;
2374 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002375
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002376 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002377 if (prop.tp_col == MAXCOL && !last_line)
2378 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002379 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2380
2381 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002382 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002383
Bram Moolenaare175dc62022-08-01 22:18:50 +01002384 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002385 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2386 &prop, sizeof(prop));
2387 else
2388 {
2389 int j;
2390 int found = FALSE;
2391
2392 // Search for continuing prop.
2393 for (j = *props_remaining; j < propcount; ++j)
2394 {
2395 textprop_T op;
2396
2397 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2398 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2399 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002400 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002401 found = TRUE;
2402 op.tp_len += op.tp_col - prop.tp_col;
2403 op.tp_col = prop.tp_col;
2404 // Start/end is taken care of when deleting joined lines
2405 op.tp_flags = prop.tp_flags;
2406 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2407 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002408 }
2409 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002410 if (!found)
2411 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002412 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002413 }
2414}
2415
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002416#endif // FEAT_PROP_POPUP