blob: 4153e3f45335e85559ba444b1a28fb9fc078f846 [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010017
18/*
19 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
20 * This avoids adding a pointer to the hashtable item.
21 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
22 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
23 * HI2PT() converts a hashitem pointer to a proptype pointer.
24 */
25#define PT2HIKEY(p) ((p)->pt_name)
26#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
27#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
28
29// The global text property types.
30static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010031static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33// The last used text property type ID.
34static int proptype_id = 0;
35
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036/*
37 * Find a property type by name, return the hashitem.
38 * Returns NULL if the item can't be found.
39 */
40 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010041find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010042{
43 hashtab_T *ht;
44 hashitem_T *hi;
45
46 if (*name == NUL)
47 return NULL;
48 if (buf == NULL)
49 ht = global_proptypes;
50 else
51 ht = buf->b_proptypes;
52
53 if (ht == NULL)
54 return NULL;
55 hi = hash_find(ht, name);
56 if (HASHITEM_EMPTY(hi))
57 return NULL;
58 return hi;
59}
60
61/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010062 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010063 */
64 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010065find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010066{
Bram Moolenaare44336b2022-08-07 18:20:08 +010067 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010068
69 if (hi == NULL)
70 return NULL;
71 return HI2PT(hi);
72}
73
74/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020075 * Get the prop type ID of "name".
76 * When not found return zero.
77 */
78 int
79find_prop_type_id(char_u *name, buf_T *buf)
80{
Bram Moolenaare44336b2022-08-07 18:20:08 +010081 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020082
83 if (pt == NULL)
84 return 0;
85 return pt->pt_id;
86}
87
88/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089 * Lookup a property type by name. First in "buf" and when not found in the
90 * global types.
91 * When not found gives an error message and returns NULL.
92 */
93 static proptype_T *
94lookup_prop_type(char_u *name, buf_T *buf)
95{
Bram Moolenaare44336b2022-08-07 18:20:08 +010096 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097
98 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +010099 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100101 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100152 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153 return;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200154
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100155 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
156 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157}
158
159/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200160 * Attach a text property 'type_name' to the text starting
161 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100162 * the buffer "buf" and assign identifier "id".
163 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200164 */
165 static int
166prop_add_one(
167 buf_T *buf,
168 char_u *type_name,
169 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100170 char_u *text_arg,
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100171 int text_padding_left,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100172 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200173 linenr_T start_lnum,
174 linenr_T end_lnum,
175 colnr_T start_col,
176 colnr_T end_col)
177{
178 proptype_T *type;
179 linenr_T lnum;
180 int proplen;
181 char_u *props = NULL;
182 char_u *newprops;
183 size_t textlen;
184 char_u *newtext;
185 int i;
186 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text = text_arg;
188 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189
190 type = lookup_prop_type(type_name, buf);
191 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200193
194 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
195 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000196 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200198 }
199 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
200 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000201 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200203 }
204
205 if (buf->b_ml.ml_mfp == NULL)
206 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000207 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
209 }
210
211 if (text != NULL)
212 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100213 garray_T *gap = &buf->b_textprop_text;
214 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100215
216 // double check we got the right ID
217 if (-id - 1 != gap->ga_len)
218 iemsg("text prop ID mismatch");
219 if (gap->ga_growsize == 0)
220 ga_init2(gap, sizeof(char *), 50);
221 if (ga_grow(gap, 1) == FAIL)
222 goto theend;
223 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100224
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100225 // change any control character (Tab, Newline, etc.) to a Space to make
226 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100227 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100228 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100230 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200231 }
232
233 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
234 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100235 colnr_T col; // start column use in tp_col
236 colnr_T sort_col; // column where it appears
237 long length; // in bytes
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200238
239 // Fetch the line to get the ml_line_len field updated.
240 proplen = get_text_props(buf, lnum, &props, TRUE);
241 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
242
243 if (lnum == start_lnum)
244 col = start_col;
245 else
246 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100247 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200248 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000249 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100250 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200251 }
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100252 sort_col = col;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200253
254 if (lnum == end_lnum)
255 length = end_col - col;
256 else
257 length = (int)textlen - col + 1;
258 if (length > (long)textlen)
259 length = (int)textlen; // can include the end-of-line
260 if (length < 0)
261 length = 0; // zero-width property
262
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100263 if (text_arg != NULL)
264 {
265 length = 1; // text is placed on one character
266 if (col == 0)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100267 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100268 col = MAXCOL; // before or after the line
269 if ((text_flags & TP_FLAG_ALIGN_ABOVE) == 0)
270 sort_col = MAXCOL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100271 length += text_padding_left;
272 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100273 }
274
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200275 // Allocate the new line with space for the new property.
276 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
277 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100278 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200279 // Copy the text, including terminating NUL.
280 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
281
282 // Find the index where to insert the new property.
283 // Since the text properties are not aligned properly when stored with
284 // the text, we need to copy them as bytes before using it as a struct.
285 for (i = 0; i < proplen; ++i)
286 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100287 colnr_T prop_col;
288
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200289 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
290 sizeof(textprop_T));
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100291 // col is MAXCOL when the text goes above or after the line, when
292 // above we should use column zero for sorting
293 prop_col = (tmp_prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
294 ? 0 : tmp_prop.tp_col;
295 if (prop_col >= sort_col)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200296 break;
297 }
298 newprops = newtext + textlen;
299 if (i > 0)
300 mch_memmove(newprops, props, sizeof(textprop_T) * i);
301
302 tmp_prop.tp_col = col;
303 tmp_prop.tp_len = length;
304 tmp_prop.tp_id = id;
305 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100306 tmp_prop.tp_flags = text_flags
307 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100308 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
309 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
310 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200311 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
312 sizeof(textprop_T));
313
314 if (i < proplen)
315 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
316 props + i * sizeof(textprop_T),
317 sizeof(textprop_T) * (proplen - i));
318
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100319 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200320 vim_free(buf->b_ml.ml_line_ptr);
321 buf->b_ml.ml_line_ptr = newtext;
322 buf->b_ml.ml_line_len += sizeof(textprop_T);
323 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
324 }
325
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100326 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200327 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100328 res = OK;
329
330theend:
331 vim_free(text);
332 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200333}
334
335/*
336 * prop_add_list()
337 * First argument specifies the text property:
338 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
339 * Second argument is a List where each item is a List with the following
340 * entries: [lnum, start_col, end_col]
341 */
342 void
343f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
344{
345 dict_T *dict;
346 char_u *type_name;
347 buf_T *buf = curbuf;
348 int id = 0;
349 listitem_T *li;
350 list_T *pos_list;
351 linenr_T start_lnum;
352 colnr_T start_col;
353 linenr_T end_lnum;
354 colnr_T end_col;
355 int error = FALSE;
356
357 if (check_for_dict_arg(argvars, 0) == FAIL
358 || check_for_list_arg(argvars, 1) == FAIL)
359 return;
360
Bram Moolenaard83392a2022-09-01 12:22:46 +0100361 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200362 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363
364 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100365 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200366 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000367 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200368 return;
369 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100370 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200371
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100372 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100373 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200374
375 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
376 return;
377
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100378 // This must be done _before_ we start adding properties because property
379 // changes trigger buffer (memline) reorganisation, which needs this flag
380 // to be correctly set.
381 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200382 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
383 {
384 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
385 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000386 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200387 return;
388 }
389
390 pos_list = li->li_tv.vval.v_list;
391 start_lnum = list_find_nr(pos_list, 0L, &error);
392 start_col = list_find_nr(pos_list, 1L, &error);
393 end_lnum = list_find_nr(pos_list, 2L, &error);
394 end_col = list_find_nr(pos_list, 3L, &error);
395 if (error || start_lnum <= 0 || start_col <= 0
396 || end_lnum <= 0 || end_col <= 0)
397 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000398 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200399 return;
400 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100401 if (prop_add_one(buf, type_name, id, NULL, 0, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200402 start_col, end_col) == FAIL)
403 return;
404 }
405
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100406 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200407}
408
409/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100410 * Get the next ID to use for a textprop with text in buffer "buf".
411 */
412 static int
413get_textprop_id(buf_T *buf)
414{
415 // TODO: recycle deleted entries
416 return -(buf->b_textprop_text.ga_len + 1);
417}
418
419/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200420 * Shared between prop_add() and popup_create().
421 * "dict_arg" is the function argument of a dict containing "bufnr".
422 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100423 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200424 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100425 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426prop_add_common(
427 linenr_T start_lnum,
428 colnr_T start_col,
429 dict_T *dict,
430 buf_T *default_buf,
431 typval_T *dict_arg)
432{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200433 linenr_T end_lnum;
434 colnr_T end_col;
435 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200436 buf_T *buf = default_buf;
437 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100438 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100439 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100440 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100441
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100442 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100443 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000444 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100445 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100446 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100447 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100448
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100449 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100450 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100451 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100452 if (end_lnum < start_lnum)
453 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000454 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100455 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100456 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100457 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100458 else
459 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100460
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100461 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100462 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100463 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100465 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000467 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100468 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100469 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100470 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100472 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100474 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100475 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100476 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000477 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100478 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100479 }
480 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100481 else if (start_lnum == end_lnum)
482 end_col = start_col;
483 else
484 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100485
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100486 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100487 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100488
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100489 if (dict_has_key(dict, "text"))
490 {
Bram Moolenaarfb593c52022-09-17 18:57:36 +0100491 if (dict_has_key(dict, "length")
492 || dict_has_key(dict, "end_col")
493 || dict_has_key(dict, "end_lnum"))
494 {
495 emsg(_(e_cannot_use_length_endcol_and_endlnum_with_text));
496 goto theend;
497 }
498
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100499 text = dict_get_string(dict, "text", TRUE);
500 if (text == NULL)
501 goto theend;
502 // use a default length of 1 to make multiple props show up
503 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100504
505 if (dict_has_key(dict, "text_align"))
506 {
507 char_u *p = dict_get_string(dict, "text_align", FALSE);
508
509 if (p == NULL)
510 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100511 if (start_col != 0)
512 {
513 emsg(_(e_can_only_use_text_align_when_column_is_zero));
514 goto theend;
515 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100516 if (STRCMP(p, "right") == 0)
517 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100518 else if (STRCMP(p, "above") == 0)
519 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100520 else if (STRCMP(p, "below") == 0)
521 flags |= TP_FLAG_ALIGN_BELOW;
522 else if (STRCMP(p, "after") != 0)
523 {
524 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
525 goto theend;
526 }
527 }
528
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100529 if (dict_has_key(dict, "text_padding_left"))
530 {
531 text_padding_left = dict_get_number(dict, "text_padding_left");
532 if (text_padding_left < 0)
533 {
534 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
535 goto theend;
536 }
537 }
538
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100539 if (dict_has_key(dict, "text_wrap"))
540 {
541 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100542
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100543 if (p == NULL)
544 goto theend;
545 if (STRCMP(p, "wrap") == 0)
546 flags |= TP_FLAG_WRAP;
547 else if (STRCMP(p, "truncate") != 0)
548 {
549 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
550 goto theend;
551 }
552 }
553 }
554
555 // Column must be 1 or more for a normal text property; when "text" is
556 // present zero means it goes after the line.
557 if (start_col < (text == NULL ? 1 : 0))
558 {
559 semsg(_(e_invalid_column_number_nr), (long)start_col);
560 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100561 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100562 if (start_col > 0 && text_padding_left > 0)
563 {
564 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
565 goto theend;
566 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100567
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200568 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100569 goto theend;
570
571 if (id < 0 && buf->b_textprop_text.ga_len > 0)
572 {
573 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
574 goto theend;
575 }
576 if (text != NULL)
577 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100578
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100579 // This must be done _before_ we add the property because property changes
580 // trigger buffer (memline) reorganisation, which needs this flag to be
581 // correctly set.
582 buf->b_has_textprop = TRUE; // this is never reset
583
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100584 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100585 start_lnum, end_lnum, start_col, end_col);
586 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100587
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100588 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100589
590theend:
591 vim_free(text);
592 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100593}
594
595/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100596 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100597 * Returns the number of text properties and, when non-zero, a pointer to the
598 * first one in "props" (note that it is not aligned, therefore the char_u
599 * pointer).
600 */
601 int
602get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
603{
604 char_u *text;
605 size_t textlen;
606 size_t proplen;
607
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100608 // Be quick when no text property types have been defined or the buffer,
609 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200610 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100611 return 0;
612
613 // Fetch the line to get the ml_line_len field updated.
614 text = ml_get_buf(buf, lnum, will_change);
615 textlen = STRLEN(text) + 1;
616 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100617 if (proplen == 0)
618 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100619 if (proplen % sizeof(textprop_T) != 0)
620 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000621 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100622 return 0;
623 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100624 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100625 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100626}
627
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100628/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100629 * Return the number of text properties with "above" or "below" alignment in
630 * line "lnum". A "right" aligned property also goes below after a "below" or
631 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100632 */
633 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100634prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100635{
636 char_u *props;
637 int count = get_text_props(buf, lnum, &props, FALSE);
638 int result = 0;
639 textprop_T prop;
640 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100641 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100642
643 if (count == 0)
644 return 0;
645 for (i = 0; i < count; ++i)
646 {
647 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100648 if (prop.tp_col == MAXCOL)
649 {
650 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
651 || (next_right_goes_below
652 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
653 {
654 next_right_goes_below = TRUE;
655 ++result;
656 }
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100657 else if (prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
658 {
659 next_right_goes_below = FALSE;
660 ++result;
661 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100662 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
663 next_right_goes_below = TRUE;
664 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100665 }
666 return result;
667}
668
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200669/**
670 * Return the number of text properties on line "lnum" in the current buffer.
671 * When "only_starting" is true only text properties starting in this line will
672 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100673 * When "last_line" is FALSE then text properties after the line are not
674 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200675 */
676 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100677count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200678{
679 char_u *props;
680 int proplen = get_text_props(curbuf, lnum, &props, 0);
681 int result = proplen;
682 int i;
683 textprop_T prop;
684
Bram Moolenaare175dc62022-08-01 22:18:50 +0100685 for (i = 0; i < proplen; ++i)
686 {
687 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100688 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100689 // previous line, or when not in the last line and it is virtual text
690 // after the line.
691 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
692 || (!last_line && prop.tp_col == MAXCOL))
693 --result;
694 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200695 return result;
696}
697
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100698static textprop_T *text_prop_compare_props;
699static buf_T *text_prop_compare_buf;
700
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100701/* Score for sorting on position of the text property: 0: above,
702 * 1: after (default), 2: right, 3: below (comes last)
703 */
704 static int
705text_prop_order(int flags)
706{
707 if (flags & TP_FLAG_ALIGN_ABOVE)
708 return 0;
709 if (flags & TP_FLAG_ALIGN_RIGHT)
710 return 2;
711 if (flags & TP_FLAG_ALIGN_BELOW)
712 return 3;
713 return 1;
714}
715
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100716/*
717 * Function passed to qsort() to sort text properties.
718 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
719 * both have the same priority.
720 */
721 static int
722text_prop_compare(const void *s1, const void *s2)
723{
724 int idx1, idx2;
725 textprop_T *tp1, *tp2;
726 proptype_T *pt1, *pt2;
727 colnr_T col1, col2;
728
729 idx1 = *(int *)s1;
730 idx2 = *(int *)s2;
731 tp1 = &text_prop_compare_props[idx1];
732 tp2 = &text_prop_compare_props[idx2];
733 col1 = tp1->tp_col;
734 col2 = tp2->tp_col;
735 if (col1 == MAXCOL && col2 == MAXCOL)
736 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100737 int order1 = text_prop_order(tp1->tp_flags);
738 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100739
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100740 // both props add text before or after the line, sort on order where it
741 // is added
742 if (order1 != order2)
743 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100744 }
745
746 // property that inserts text has priority over one that doesn't
747 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
748 return tp1->tp_id < 0 ? 1 : -1;
749
750 // check highest priority, defined by the type
751 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
752 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
753 if (pt1 != pt2)
754 {
755 if (pt1 == NULL)
756 return -1;
757 if (pt2 == NULL)
758 return 1;
759 if (pt1->pt_priority != pt2->pt_priority)
760 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
761 }
762
763 // same priority, one that starts first wins
764 if (col1 != col2)
765 return col1 < col2 ? 1 : -1;
766
767 // for a property with text the id can be used as tie breaker
768 if (tp1->tp_id < 0)
769 return tp1->tp_id > tp2->tp_id ? 1 : -1;
770
771 return 0;
772}
773
774/*
775 * Sort "count" text properties using an array if indexes "idxs" into the list
776 * of text props "props" for buffer "buf".
777 */
778 void
779sort_text_props(
780 buf_T *buf,
781 textprop_T *props,
782 int *idxs,
783 int count)
784{
785 text_prop_compare_buf = buf;
786 text_prop_compare_props = props;
787 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
788}
789
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100790/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200791 * Find text property "type_id" in the visible lines of window "wp".
792 * Match "id" when it is > 0.
793 * Returns FAIL when not found.
794 */
795 int
796find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
797 linenr_T *found_lnum)
798{
799 linenr_T lnum;
800 char_u *props;
801 int count;
802 int i;
803
804 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100805 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200806 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
807 {
808 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
809 for (i = 0; i < count; ++i)
810 {
811 mch_memmove(prop, props + i * sizeof(textprop_T),
812 sizeof(textprop_T));
813 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
814 {
815 *found_lnum = lnum;
816 return OK;
817 }
818 }
819 }
820 return FAIL;
821}
822
823/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100824 * Set the text properties for line "lnum" to "props" with length "len".
825 * If "len" is zero text properties are removed, "props" is not used.
826 * Any existing text properties are dropped.
827 * Only works for the current buffer.
828 */
829 static void
830set_text_props(linenr_T lnum, char_u *props, int len)
831{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100832 char_u *text;
833 char_u *newtext;
834 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100835
836 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100837 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100838 newtext = alloc(textlen + len);
839 if (newtext == NULL)
840 return;
841 mch_memmove(newtext, text, textlen);
842 if (len > 0)
843 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100844 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100845 vim_free(curbuf->b_ml.ml_line_ptr);
846 curbuf->b_ml.ml_line_ptr = newtext;
847 curbuf->b_ml.ml_line_len = textlen + len;
848 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
849}
850
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100851/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100852 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100853 */
854 void
855add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
856{
857 char_u *text;
858 char_u *newtext;
859 int proplen = text_prop_count * (int)sizeof(textprop_T);
860
861 text = ml_get(lnum);
862 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
863 if (newtext == NULL)
864 return;
865 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
866 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
867 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
868 vim_free(curbuf->b_ml.ml_line_ptr);
869 curbuf->b_ml.ml_line_ptr = newtext;
870 curbuf->b_ml.ml_line_len += proplen;
871 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
872}
873
Bram Moolenaare44336b2022-08-07 18:20:08 +0100874/*
875 * Function passed to qsort() for sorting proptype_T on pt_id.
876 */
877 static int
878compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100879{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100880 proptype_T *tp1 = *(proptype_T **)s1;
881 proptype_T *tp2 = *(proptype_T **)s2;
882
883 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
884}
885
886 static proptype_T *
887find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
888{
889 int low = 0;
890 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100891
Bram Moolenaar10246902022-08-08 17:08:05 +0100892 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100893 return NULL;
894
Bram Moolenaare44336b2022-08-07 18:20:08 +0100895 // Make the loopup faster by creating an array with pointers to
896 // hashtable entries, sorted on pt_id.
897 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100898 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100899 long todo;
900 hashitem_T *hi;
901 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100902
Bram Moolenaare44336b2022-08-07 18:20:08 +0100903 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
904 if (*array == NULL)
905 return NULL;
906 todo = (long)ht->ht_used;
907 for (hi = ht->ht_array; todo > 0; ++hi)
908 {
909 if (!HASHITEM_EMPTY(hi))
910 {
911 (*array)[i++] = HI2PT(hi);
912 --todo;
913 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100914 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100915 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
916 }
917
918 // binary search in the sorted array
919 high = ht->ht_used;
920 while (high > low)
921 {
922 int m = (high + low) / 2;
923
924 if ((*array)[m]->pt_id == id)
925 return (*array)[m];
926 if ((*array)[m]->pt_id > id)
927 high = m;
928 else
929 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100930 }
931 return NULL;
932}
933
934/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100935 * Fill 'dict' with text properties in 'prop'.
936 */
937 static void
938prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
939{
940 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200941 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100942
943 dict_add_number(dict, "col", prop->tp_col);
944 dict_add_number(dict, "length", prop->tp_len);
945 dict_add_number(dict, "id", prop->tp_id);
946 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
947 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200948
Bram Moolenaare44336b2022-08-07 18:20:08 +0100949 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200950 if (pt == NULL)
951 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100952 pt = find_type_by_id(global_proptypes, &global_proparray,
953 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200954 buflocal = FALSE;
955 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100956 if (pt != NULL)
957 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200958
959 if (buflocal)
960 dict_add_number(dict, "type_bufnr", buf->b_fnum);
961 else
962 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100963}
964
965/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100966 * Find a property type by ID in "buf" or globally.
967 * Returns NULL if not found.
968 */
969 proptype_T *
970text_prop_type_by_id(buf_T *buf, int id)
971{
972 proptype_T *type;
973
Bram Moolenaare44336b2022-08-07 18:20:08 +0100974 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100975 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100976 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100977 return type;
978}
979
980/*
981 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
982 */
983 void
984f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
985{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200986 linenr_T start;
987 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100988 linenr_T lnum;
989 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100990 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100991
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200992 if (in_vim9script()
993 && (check_for_number_arg(argvars, 0) == FAIL
994 || check_for_opt_number_arg(argvars, 1) == FAIL
995 || (argvars[1].v_type != VAR_UNKNOWN
996 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
997 return;
998
999 start = tv_get_number(&argvars[0]);
1000 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001001 if (argvars[1].v_type != VAR_UNKNOWN)
1002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001003 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001004 if (argvars[2].v_type != VAR_UNKNOWN)
1005 {
1006 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
1007 return;
1008 }
1009 }
1010 if (start < 1 || end < 1)
1011 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001012 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001013 return;
1014 }
1015
1016 for (lnum = start; lnum <= end; ++lnum)
1017 {
1018 char_u *text;
1019 size_t len;
1020
1021 if (lnum > buf->b_ml.ml_line_count)
1022 break;
1023 text = ml_get_buf(buf, lnum, FALSE);
1024 len = STRLEN(text) + 1;
1025 if ((size_t)buf->b_ml.ml_line_len > len)
1026 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001027 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001028 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1029 {
1030 char_u *newtext = vim_strsave(text);
1031
1032 // need to allocate the line now
1033 if (newtext == NULL)
1034 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001035 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1036 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001037 buf->b_ml.ml_line_ptr = newtext;
1038 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1039 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001040 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001041 }
1042 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001043 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001044 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001045}
1046
1047/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001048 * prop_find({props} [, {direction}])
1049 */
1050 void
1051f_prop_find(typval_T *argvars, typval_T *rettv)
1052{
1053 pos_T *cursor = &curwin->w_cursor;
1054 dict_T *dict;
1055 buf_T *buf = curbuf;
1056 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001057 int lnum_start;
1058 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001059 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001060 int id = 0;
1061 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001062 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001063 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001064 int lnum = -1;
1065 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001066 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001067 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001068
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001069 if (in_vim9script()
1070 && (check_for_dict_arg(argvars, 0) == FAIL
1071 || check_for_opt_string_arg(argvars, 1) == FAIL))
1072 return;
1073
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001074 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001075 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001076 dict = argvars[0].vval.v_dict;
1077
1078 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1079 return;
1080 if (buf->b_ml.ml_mfp == NULL)
1081 return;
1082
1083 if (argvars[1].v_type != VAR_UNKNOWN)
1084 {
1085 char_u *dir_s = tv_get_string(&argvars[1]);
1086
1087 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001088 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001089 else if (*dir_s != 'f')
1090 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001091 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001092 return;
1093 }
1094 }
1095
1096 di = dict_find(dict, (char_u *)"lnum", -1);
1097 if (di != NULL)
1098 lnum = tv_get_number(&di->di_tv);
1099
1100 di = dict_find(dict, (char_u *)"col", -1);
1101 if (di != NULL)
1102 col = tv_get_number(&di->di_tv);
1103
1104 if (lnum == -1)
1105 {
1106 lnum = cursor->lnum;
1107 col = cursor->col + 1;
1108 }
1109 else if (col == -1)
1110 col = 1;
1111
1112 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1113 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001114 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001115 return;
1116 }
1117
Bram Moolenaard61efa52022-07-23 09:52:04 +01001118 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001119
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001120 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001121 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001122 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001123 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001124 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001125 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001126 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001127 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001128 proptype_T *type = lookup_prop_type(name, buf);
1129
1130 if (type == NULL)
1131 return;
1132 type_id = type->pt_id;
1133 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001134 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001135 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001136 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001137 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001138 return;
1139 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001140 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001141 {
Ben Jacksona7704222022-08-20 20:54:51 +01001142 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001143 return;
1144 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001145
1146 lnum_start = lnum;
1147
1148 if (rettv_dict_alloc(rettv) == FAIL)
1149 return;
1150
1151 while (1)
1152 {
1153 char_u *text = ml_get_buf(buf, lnum, FALSE);
1154 size_t textlen = STRLEN(text) + 1;
1155 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001156 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001157 int i;
1158 textprop_T prop;
1159 int prop_start;
1160 int prop_end;
1161
LemonBoy9bd3ce22022-04-18 21:54:02 +01001162 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001163 {
1164 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001165 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001166
LemonBoy9bd3ce22022-04-18 21:54:02 +01001167 // For the very first line try to find the first property before or
1168 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001169 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001170 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001171 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001172 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001173 if (prop.tp_col > col)
1174 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001175 }
1176 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1177 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001178 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001179 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001180 : (id_found && prop.tp_id == id)
1181 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001182 {
1183 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001184 if (lnum_start == lnum
1185 && col >= prop.tp_col
1186 && (col <= prop.tp_col + prop.tp_len
1187 - (prop.tp_len != 0)))
1188 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001189
LemonBoy9bd3ce22022-04-18 21:54:02 +01001190 // The property was not continued from last line, it starts on
1191 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001192 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001193 // The property does not continue on the next line, it ends on
1194 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001195 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001196 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001197 seen_end = 1;
1198
1199 // Skip lines without the start flag.
1200 if (!prop_start)
1201 {
1202 // Always search backwards for start when search started
1203 // on a prop and we're not skipping.
1204 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001205 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001206 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001207 }
1208
1209 // If skipstart is true, skip the prop at start pos (even if
1210 // continued from another line).
1211 if (start_pos_has_prop && skipstart && !seen_end)
1212 {
1213 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001214 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001215 }
1216
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001217 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1218 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1219
1220 return;
1221 }
1222 }
1223
1224 if (dir > 0)
1225 {
1226 if (lnum >= buf->b_ml.ml_line_count)
1227 break;
1228 lnum++;
1229 }
1230 else
1231 {
1232 if (lnum <= 1)
1233 break;
1234 lnum--;
1235 }
1236 }
1237}
1238
1239/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001240 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1241 */
1242 static int
1243prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1244{
1245 int i;
1246
1247 for (i = 0; i < len; i++)
1248 if (types_or_ids[i] == type_or_id)
1249 return TRUE;
1250
1251 return FALSE;
1252}
1253
1254/*
1255 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1256 * If 'prop_types' is not NULL, then return only the text properties with
1257 * matching property type in the 'prop_types' array.
1258 * If 'prop_ids' is not NULL, then return only the text properties with
1259 * an identifier in the 'props_ids' array.
1260 * If 'add_lnum' is TRUE, then add the line number also to the text property
1261 * dictionary.
1262 */
1263 static void
1264get_props_in_line(
1265 buf_T *buf,
1266 linenr_T lnum,
1267 int *prop_types,
1268 int prop_types_len,
1269 int *prop_ids,
1270 int prop_ids_len,
1271 list_T *retlist,
1272 int add_lnum)
1273{
1274 char_u *text = ml_get_buf(buf, lnum, FALSE);
1275 size_t textlen = STRLEN(text) + 1;
1276 int count;
1277 int i;
1278 textprop_T prop;
1279
1280 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1281 for (i = 0; i < count; ++i)
1282 {
1283 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1284 sizeof(textprop_T));
1285 if ((prop_types == NULL
1286 || prop_type_or_id_in_list(prop_types, prop_types_len,
1287 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001288 && (prop_ids == NULL
1289 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1290 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001291 {
1292 dict_T *d = dict_alloc();
1293
1294 if (d == NULL)
1295 break;
1296 prop_fill_dict(d, &prop, buf);
1297 if (add_lnum)
1298 dict_add_number(d, "lnum", lnum);
1299 list_append_dict(retlist, d);
1300 }
1301 }
1302}
1303
1304/*
1305 * Convert a List of property type names into an array of property type
1306 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1307 * error. 'num_types' is set to the number of returned property types.
1308 */
1309 static int *
1310get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1311{
1312 int *prop_types;
1313 listitem_T *li;
1314 int i;
1315 char_u *name;
1316 proptype_T *type;
1317
1318 *num_types = 0;
1319
1320 prop_types = ALLOC_MULT(int, list_len(l));
1321 if (prop_types == NULL)
1322 return NULL;
1323
1324 i = 0;
1325 FOR_ALL_LIST_ITEMS(l, li)
1326 {
1327 if (li->li_tv.v_type != VAR_STRING)
1328 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001329 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001330 goto errret;
1331 }
1332 name = li->li_tv.vval.v_string;
1333 if (name == NULL)
1334 goto errret;
1335
1336 type = lookup_prop_type(name, buf);
1337 if (type == NULL)
1338 goto errret;
1339 prop_types[i++] = type->pt_id;
1340 }
1341
1342 *num_types = i;
1343 return prop_types;
1344
1345errret:
1346 VIM_CLEAR(prop_types);
1347 return NULL;
1348}
1349
1350/*
1351 * Convert a List of property identifiers into an array of property
1352 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1353 * error. 'num_ids' is set to the number of returned property identifiers.
1354 */
1355 static int *
1356get_prop_ids_from_list(list_T *l, int *num_ids)
1357{
1358 int *prop_ids;
1359 listitem_T *li;
1360 int i;
1361 int id;
1362 int error;
1363
1364 *num_ids = 0;
1365
1366 prop_ids = ALLOC_MULT(int, list_len(l));
1367 if (prop_ids == NULL)
1368 return NULL;
1369
1370 i = 0;
1371 FOR_ALL_LIST_ITEMS(l, li)
1372 {
1373 error = FALSE;
1374 id = tv_get_number_chk(&li->li_tv, &error);
1375 if (error)
1376 goto errret;
1377
1378 prop_ids[i++] = id;
1379 }
1380
1381 *num_ids = i;
1382 return prop_ids;
1383
1384errret:
1385 VIM_CLEAR(prop_ids);
1386 return NULL;
1387}
1388
1389/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001390 * prop_list({lnum} [, {bufnr}])
1391 */
1392 void
1393f_prop_list(typval_T *argvars, typval_T *rettv)
1394{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001395 linenr_T lnum;
1396 linenr_T start_lnum;
1397 linenr_T end_lnum;
1398 buf_T *buf = curbuf;
1399 int add_lnum = FALSE;
1400 int *prop_types = NULL;
1401 int prop_types_len = 0;
1402 int *prop_ids = NULL;
1403 int prop_ids_len = 0;
1404 list_T *l;
1405 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001406
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001407 if (in_vim9script()
1408 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001409 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001410 return;
1411
Bram Moolenaar93a10962022-06-16 11:42:09 +01001412 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001413 return;
1414
1415 // default: get text properties on current line
1416 start_lnum = tv_get_number(&argvars[0]);
1417 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001418 if (argvars[1].v_type != VAR_UNKNOWN)
1419 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001420 dict_T *d;
1421
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001422 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001423 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001424 d = argvars[1].vval.v_dict;
1425
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001426 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1427 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001428
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001429 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001430 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001431 if (di->di_tv.v_type != VAR_NUMBER)
1432 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001433 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001434 return;
1435 }
1436 end_lnum = tv_get_number(&di->di_tv);
1437 if (end_lnum < 0)
1438 // negative end_lnum is used as an offset from the last buffer
1439 // line
1440 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1441 else if (end_lnum > buf->b_ml.ml_line_count)
1442 end_lnum = buf->b_ml.ml_line_count;
1443 add_lnum = TRUE;
1444 }
1445 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1446 {
1447 if (di->di_tv.v_type != VAR_LIST)
1448 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001449 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001450 return;
1451 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001452
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001453 l = di->di_tv.vval.v_list;
1454 if (l != NULL && list_len(l) > 0)
1455 {
1456 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1457 if (prop_types == NULL)
1458 return;
1459 }
1460 }
1461 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1462 {
1463 if (di->di_tv.v_type != VAR_LIST)
1464 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001465 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001466 goto errret;
1467 }
1468
1469 l = di->di_tv.vval.v_list;
1470 if (l != NULL && list_len(l) > 0)
1471 {
1472 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1473 if (prop_ids == NULL)
1474 goto errret;
1475 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001476 }
1477 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001478 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1479 || end_lnum < 1 || end_lnum < start_lnum)
1480 emsg(_(e_invalid_range));
1481 else
1482 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1483 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1484 prop_ids, prop_ids_len,
1485 rettv->vval.v_list, add_lnum);
1486
1487errret:
1488 VIM_CLEAR(prop_types);
1489 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001490}
1491
1492/*
1493 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1494 */
1495 void
1496f_prop_remove(typval_T *argvars, typval_T *rettv)
1497{
1498 linenr_T start = 1;
1499 linenr_T end = 0;
1500 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001501 linenr_T first_changed = 0;
1502 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001503 dict_T *dict;
1504 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001505 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001506 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001507 int type_id = -1; // for a single "type"
1508 int *type_ids = NULL; // array, for a list of "types", allocated
1509 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001510 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001511 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001512
1513 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001514
1515 if (in_vim9script()
1516 && (check_for_dict_arg(argvars, 0) == FAIL
1517 || check_for_opt_number_arg(argvars, 1) == FAIL
1518 || (argvars[1].v_type != VAR_UNKNOWN
1519 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1520 return;
1521
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001522 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001523 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001524
1525 if (argvars[1].v_type != VAR_UNKNOWN)
1526 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001527 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001528 end = start;
1529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001530 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001531 if (start < 1 || end < 1)
1532 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001533 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001534 return;
1535 }
1536 }
1537
1538 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001539 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1540 return;
1541 if (buf->b_ml.ml_mfp == NULL)
1542 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001543
Bram Moolenaard61efa52022-07-23 09:52:04 +01001544 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001545
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001546 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001547 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001548
1549 // if a specific type was supplied "type": check that (and ignore "types".
1550 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001551 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001552 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001553 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001554 proptype_T *type = lookup_prop_type(name, buf);
1555
1556 if (type == NULL)
1557 return;
1558 type_id = type->pt_id;
1559 }
Ben Jacksona7704222022-08-20 20:54:51 +01001560 if (dict_has_key(dict, "types"))
1561 {
1562 typval_T types;
1563 listitem_T *li = NULL;
1564
1565 dict_get_tv(dict, "types", &types);
1566 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1567 {
1568 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1569
1570 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1571 {
1572 proptype_T *prop_type;
1573
1574 if (li->li_tv.v_type != VAR_STRING)
1575 continue;
1576
1577 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1578
1579 if (!prop_type)
1580 goto cleanup_prop_remove;
1581
1582 type_ids[num_type_ids++] = prop_type->pt_id;
1583 }
1584 }
1585 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001586 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001587
Ben Jacksona7704222022-08-20 20:54:51 +01001588 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001589 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001590 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001591 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001592 }
Ben Jacksona7704222022-08-20 20:54:51 +01001593 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001594 {
Ben Jacksona7704222022-08-20 20:54:51 +01001595 emsg(_(e_need_id_and_type_or_types_with_both));
1596 goto cleanup_prop_remove;
1597 }
1598 if (type_id != -1 && num_type_ids > 0)
1599 {
1600 emsg(_(e_cannot_specify_both_type_and_types));
1601 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001602 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001603
1604 if (end == 0)
1605 end = buf->b_ml.ml_line_count;
1606 for (lnum = start; lnum <= end; ++lnum)
1607 {
1608 char_u *text;
1609 size_t len;
1610
1611 if (lnum > buf->b_ml.ml_line_count)
1612 break;
1613 text = ml_get_buf(buf, lnum, FALSE);
1614 len = STRLEN(text) + 1;
1615 if ((size_t)buf->b_ml.ml_line_len > len)
1616 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001617 static textprop_T textprop; // static because of alignment
1618 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001619
1620 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1621 / sizeof(textprop_T); ++idx)
1622 {
1623 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1624 + idx * sizeof(textprop_T);
1625 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001626 int matches_id = 0;
1627 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001628
1629 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001630
1631 matches_id = textprop.tp_id == id;
1632 if (num_type_ids > 0)
1633 {
1634 int idx2;
1635
1636 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1637 matches_type = textprop.tp_type == type_ids[idx2];
1638 }
1639 else
1640 {
1641 matches_type = textprop.tp_type == type_id;
1642 }
1643
1644 if (both ? matches_id && matches_type
1645 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001646 {
1647 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1648 {
1649 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1650
1651 // need to allocate the line to be able to change it
1652 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001653 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001654 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1655 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001656 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1657 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001658 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001659 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1660
1661 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001662 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001663 }
1664
1665 taillen = buf->b_ml.ml_line_len - len
1666 - (idx + 1) * sizeof(textprop_T);
1667 if (taillen > 0)
1668 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1669 taillen);
1670 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1671 --idx;
1672
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001673 if (textprop.tp_id < 0)
1674 {
1675 garray_T *gap = &buf->b_textprop_text;
1676 int ii = -textprop.tp_id - 1;
1677
1678 // negative ID: property with text - free the text
1679 if (ii < gap->ga_len)
1680 {
1681 char_u **p = ((char_u **)gap->ga_data) + ii;
1682 vim_free(*p);
1683 *p = NULL;
1684 did_remove_text = TRUE;
1685 }
1686 }
1687
Bram Moolenaar965c0442021-05-17 00:22:06 +02001688 if (first_changed == 0)
1689 first_changed = lnum;
1690 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001691 ++rettv->vval.v_number;
1692 if (!do_all)
1693 break;
1694 }
1695 }
1696 }
1697 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001698
Bram Moolenaar965c0442021-05-17 00:22:06 +02001699 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001700 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001701 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001702 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001703 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001704 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001705
1706 if (did_remove_text)
1707 {
1708 garray_T *gap = &buf->b_textprop_text;
1709
1710 // Reduce the growarray size for NULL pointers at the end.
1711 while (gap->ga_len > 0
1712 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1713 --gap->ga_len;
1714 }
Ben Jacksona7704222022-08-20 20:54:51 +01001715
1716cleanup_prop_remove:
1717 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001718}
1719
1720/*
1721 * Common for f_prop_type_add() and f_prop_type_change().
1722 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001723 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001724prop_type_set(typval_T *argvars, int add)
1725{
1726 char_u *name;
1727 buf_T *buf = NULL;
1728 dict_T *dict;
1729 dictitem_T *di;
1730 proptype_T *prop;
1731
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001732 if (in_vim9script()
1733 && (check_for_string_arg(argvars, 0) == FAIL
1734 || check_for_dict_arg(argvars, 1) == FAIL))
1735 return;
1736
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001737 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001738 if (*name == NUL)
1739 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001740 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001741 return;
1742 }
1743
1744 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1745 return;
1746 dict = argvars[1].vval.v_dict;
1747
Bram Moolenaare44336b2022-08-07 18:20:08 +01001748 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001749 if (add)
1750 {
1751 hashtab_T **htp;
1752
1753 if (prop != NULL)
1754 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001755 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001756 return;
1757 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001758 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001759 if (prop == NULL)
1760 return;
1761 STRCPY(prop->pt_name, name);
1762 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001763 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001764 if (buf == NULL)
1765 {
1766 htp = &global_proptypes;
1767 VIM_CLEAR(global_proparray);
1768 }
1769 else
1770 {
1771 htp = &buf->b_proptypes;
1772 VIM_CLEAR(buf->b_proparray);
1773 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001774 if (*htp == NULL)
1775 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001776 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001777 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001778 {
1779 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001780 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001781 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001782 hash_init(*htp);
1783 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001784 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001785 }
1786 else
1787 {
1788 if (prop == NULL)
1789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001790 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001791 return;
1792 }
1793 }
1794
1795 if (dict != NULL)
1796 {
1797 di = dict_find(dict, (char_u *)"highlight", -1);
1798 if (di != NULL)
1799 {
1800 char_u *highlight;
1801 int hl_id = 0;
1802
Bram Moolenaard61efa52022-07-23 09:52:04 +01001803 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001804 if (highlight != NULL && *highlight != NUL)
1805 hl_id = syn_name2id(highlight);
1806 if (hl_id <= 0)
1807 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001808 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001809 highlight == NULL ? (char_u *)"" : highlight);
1810 return;
1811 }
1812 prop->pt_hl_id = hl_id;
1813 }
1814
Bram Moolenaar58187f12019-05-05 16:33:47 +02001815 di = dict_find(dict, (char_u *)"combine", -1);
1816 if (di != NULL)
1817 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001818 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001819 prop->pt_flags |= PT_FLAG_COMBINE;
1820 else
1821 prop->pt_flags &= ~PT_FLAG_COMBINE;
1822 }
1823
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001824 di = dict_find(dict, (char_u *)"override", -1);
1825 if (di != NULL)
1826 {
1827 if (tv_get_bool(&di->di_tv))
1828 prop->pt_flags |= PT_FLAG_OVERRIDE;
1829 else
1830 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1831 }
1832
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001833 di = dict_find(dict, (char_u *)"priority", -1);
1834 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001835 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001836
1837 di = dict_find(dict, (char_u *)"start_incl", -1);
1838 if (di != NULL)
1839 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001840 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001841 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1842 else
1843 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1844 }
1845
1846 di = dict_find(dict, (char_u *)"end_incl", -1);
1847 if (di != NULL)
1848 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001849 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001850 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1851 else
1852 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1853 }
1854 }
1855}
1856
1857/*
1858 * prop_type_add({name}, {props})
1859 */
1860 void
1861f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1862{
1863 prop_type_set(argvars, TRUE);
1864}
1865
1866/*
1867 * prop_type_change({name}, {props})
1868 */
1869 void
1870f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1871{
1872 prop_type_set(argvars, FALSE);
1873}
1874
1875/*
1876 * prop_type_delete({name} [, {bufnr}])
1877 */
1878 void
1879f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1880{
1881 char_u *name;
1882 buf_T *buf = NULL;
1883 hashitem_T *hi;
1884
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001885 if (in_vim9script()
1886 && (check_for_string_arg(argvars, 0) == FAIL
1887 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1888 return;
1889
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001890 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001891 if (*name == NUL)
1892 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001893 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001894 return;
1895 }
1896
1897 if (argvars[1].v_type != VAR_UNKNOWN)
1898 {
1899 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1900 return;
1901 }
1902
Bram Moolenaare44336b2022-08-07 18:20:08 +01001903 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001904 if (hi != NULL)
1905 {
1906 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001907 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001908
1909 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001910 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001911 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001912 VIM_CLEAR(global_proparray);
1913 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001914 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001915 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001916 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001917 VIM_CLEAR(buf->b_proparray);
1918 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001919 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001920 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001921 }
1922}
1923
1924/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001925 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001926 */
1927 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001928f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001929{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001930 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001931
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001932 if (in_vim9script()
1933 && (check_for_string_arg(argvars, 0) == FAIL
1934 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1935 return;
1936
1937 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001938 if (*name == NUL)
1939 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001940 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001941 return;
1942 }
1943 if (rettv_dict_alloc(rettv) == OK)
1944 {
1945 proptype_T *prop = NULL;
1946 buf_T *buf = NULL;
1947
1948 if (argvars[1].v_type != VAR_UNKNOWN)
1949 {
1950 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1951 return;
1952 }
1953
Bram Moolenaare44336b2022-08-07 18:20:08 +01001954 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001955 if (prop != NULL)
1956 {
1957 dict_T *d = rettv->vval.v_dict;
1958
1959 if (prop->pt_hl_id > 0)
1960 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1961 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001962 dict_add_number(d, "combine",
1963 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001964 dict_add_number(d, "start_incl",
1965 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1966 dict_add_number(d, "end_incl",
1967 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1968 if (buf != NULL)
1969 dict_add_number(d, "bufnr", buf->b_fnum);
1970 }
1971 }
1972}
1973
1974 static void
1975list_types(hashtab_T *ht, list_T *l)
1976{
1977 long todo;
1978 hashitem_T *hi;
1979
1980 todo = (long)ht->ht_used;
1981 for (hi = ht->ht_array; todo > 0; ++hi)
1982 {
1983 if (!HASHITEM_EMPTY(hi))
1984 {
1985 proptype_T *prop = HI2PT(hi);
1986
1987 list_append_string(l, prop->pt_name, -1);
1988 --todo;
1989 }
1990 }
1991}
1992
1993/*
1994 * prop_type_list([{bufnr}])
1995 */
1996 void
1997f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1998{
1999 buf_T *buf = NULL;
2000
2001 if (rettv_list_alloc(rettv) == OK)
2002 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002003 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
2004 return;
2005
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002006 if (argvars[0].v_type != VAR_UNKNOWN)
2007 {
2008 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2009 return;
2010 }
2011 if (buf == NULL)
2012 {
2013 if (global_proptypes != NULL)
2014 list_types(global_proptypes, rettv->vval.v_list);
2015 }
2016 else if (buf->b_proptypes != NULL)
2017 list_types(buf->b_proptypes, rettv->vval.v_list);
2018 }
2019}
2020
2021/*
2022 * Free all property types in "ht".
2023 */
2024 static void
2025clear_ht_prop_types(hashtab_T *ht)
2026{
2027 long todo;
2028 hashitem_T *hi;
2029
2030 if (ht == NULL)
2031 return;
2032
2033 todo = (long)ht->ht_used;
2034 for (hi = ht->ht_array; todo > 0; ++hi)
2035 {
2036 if (!HASHITEM_EMPTY(hi))
2037 {
2038 proptype_T *prop = HI2PT(hi);
2039
2040 vim_free(prop);
2041 --todo;
2042 }
2043 }
2044
2045 hash_clear(ht);
2046 vim_free(ht);
2047}
2048
2049#if defined(EXITFREE) || defined(PROTO)
2050/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002051 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002052 */
2053 void
2054clear_global_prop_types(void)
2055{
2056 clear_ht_prop_types(global_proptypes);
2057 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002058 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002059}
2060#endif
2061
2062/*
2063 * Free all property types for "buf".
2064 */
2065 void
2066clear_buf_prop_types(buf_T *buf)
2067{
2068 clear_ht_prop_types(buf->b_proptypes);
2069 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002070 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002071}
2072
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002073// Struct used to return two values from adjust_prop().
2074typedef struct
2075{
2076 int dirty; // if the property was changed
2077 int can_drop; // whether after this change, the prop may be removed
2078} adjustres_T;
2079
2080/*
2081 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2082 *
2083 * Note that "col" is zero-based, while tp_col is one-based.
2084 * Only for the current buffer.
2085 * "flags" can have:
2086 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002087 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002088 */
2089 static adjustres_T
2090adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002091 textprop_T *prop,
2092 colnr_T col,
2093 int added,
2094 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002095{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002096 proptype_T *pt;
2097 int start_incl;
2098 int end_incl;
2099 int droppable;
2100 adjustres_T res = {TRUE, FALSE};
2101
2102 // prop after end of the line doesn't move
2103 if (prop->tp_col == MAXCOL)
2104 {
2105 res.dirty = FALSE;
2106 return res;
2107 }
2108
2109 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2110 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002111 || (flags & APC_SUBSTITUTE)
2112 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002113 if (prop->tp_id < 0 && (flags & APC_INDENT))
2114 // when inserting indent just before a character with virtual text
2115 // shift the text property
2116 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002117 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002118 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002119 // do not drop zero-width props if they later can increase in size
2120 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002121
2122 if (added > 0)
2123 {
2124 if (col + 1 <= prop->tp_col
2125 - (start_incl || (prop->tp_len == 0 && end_incl)))
2126 // Change is entirely before the text property: Only shift
2127 prop->tp_col += added;
2128 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2129 // Insertion was inside text property
2130 prop->tp_len += added;
2131 }
2132 else if (prop->tp_col > col + 1)
2133 {
2134 if (prop->tp_col + added < col + 1)
2135 {
2136 prop->tp_len += (prop->tp_col - 1 - col) + added;
2137 prop->tp_col = col + 1;
2138 if (prop->tp_len <= 0)
2139 {
2140 prop->tp_len = 0;
2141 res.can_drop = droppable;
2142 }
2143 }
2144 else
2145 prop->tp_col += added;
2146 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002147 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2148 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002149 {
2150 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2151
2152 prop->tp_len += after > 0 ? added + after : added;
2153 res.can_drop = prop->tp_len <= 0 && droppable;
2154 }
2155 else
2156 res.dirty = FALSE;
2157
2158 return res;
2159}
2160
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002161/*
2162 * Adjust the columns of text properties in line "lnum" after position "col" to
2163 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002164 * Note that "col" is zero-based, while tp_col is one-based.
2165 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002166 * "flags" can have:
2167 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2168 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002169 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002170 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002171 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002172 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002173 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002174adjust_prop_columns(
2175 linenr_T lnum,
2176 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002177 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002178 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002179{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002180 int proplen;
2181 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002182 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002183 int ri, wi;
2184 size_t textlen;
2185
2186 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002187 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002188
2189 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2190 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002191 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002192 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002193
Bram Moolenaar196d1572019-01-02 23:47:18 +01002194 wi = 0; // write index
2195 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002196 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002197 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002198 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002199
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002200 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2201 res = adjust_prop(&prop, col, bytes_added, flags);
2202 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002203 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002204 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002205 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2206 && u_savesub(lnum) == FAIL)
2207 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002208 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002209
2210 // u_savesub() may have updated curbuf->b_ml, fetch it again
2211 if (curbuf->b_ml.ml_line_lnum != lnum)
2212 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002213 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002214 if (res.can_drop)
2215 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002216 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002217 ++wi;
2218 }
2219 if (dirty)
2220 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002221 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2222
2223 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002224 {
2225 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2226
2227 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2228 vim_free(curbuf->b_ml.ml_line_ptr);
2229 curbuf->b_ml.ml_line_ptr = p;
2230 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002231 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002232 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002233 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002234 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002235}
2236
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002237/*
2238 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002239 * "lnum_props" is the line that has the properties from before the split.
2240 * "lnum_top" is the top line.
2241 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002242 * "deleted" is the number of bytes deleted.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002243 * "at_eol" is true if the split is after the end of the line.
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002244 */
2245 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002246adjust_props_for_split(
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002247 linenr_T lnum_props,
2248 linenr_T lnum_top,
2249 int kept,
2250 int deleted,
2251 int at_eol)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002252{
2253 char_u *props;
2254 int count;
2255 garray_T prevprop;
2256 garray_T nextprop;
2257 int i;
2258 int skipped = kept + deleted;
2259
2260 if (!curbuf->b_has_textprop)
2261 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002262
2263 // Get the text properties from "lnum_props".
2264 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002265 ga_init2(&prevprop, sizeof(textprop_T), 10);
2266 ga_init2(&nextprop, sizeof(textprop_T), 10);
2267
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002268 // Keep the relevant ones in the first line, reducing the length if needed.
2269 // Copy the ones that include the split to the second line.
2270 // Move the ones after the split to the second line.
2271 for (i = 0; i < count; ++i)
2272 {
2273 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002274 proptype_T *pt;
2275 int start_incl, end_incl;
2276 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002277 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002278
2279 // copy the prop to an aligned structure
2280 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2281
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002282 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2283 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2284 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002285
2286 // a text prop "above" behaves like it is on the first text column
2287 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2288
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002289 if (prop_col == MAXCOL)
2290 {
2291 cont_prev = at_eol;
2292 cont_next = !at_eol;
2293 }
2294 else
2295 {
2296 cont_prev = prop_col + !start_incl <= kept;
2297 cont_next = skipped <= prop_col + prop.tp_len - !end_incl;
2298 }
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002299 // when a prop has text it is never copied
2300 if (prop.tp_id < 0 && cont_next)
2301 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002302
2303 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002304 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002305 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2306
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002307 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002308 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002309 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002310 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002311 if (cont_next)
2312 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002313 }
2314
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002315 // Only add the property to the next line if the length is bigger than
2316 // zero.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002317 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002318 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002319 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002320
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002321 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002322 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002323 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002324 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002325 if (p->tp_col > skipped)
2326 p->tp_col -= skipped - 1;
2327 else
2328 {
2329 p->tp_len -= skipped - p->tp_col;
2330 p->tp_col = 1;
2331 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002332 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002333 if (cont_prev)
2334 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002335 }
2336 }
2337
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002338 set_text_props(lnum_top, prevprop.ga_data,
2339 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002340 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002341 set_text_props(lnum_top + 1, nextprop.ga_data,
2342 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002343 ga_clear(&nextprop);
2344}
2345
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002346/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002347 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002348 */
2349 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002350prepend_joined_props(
2351 char_u *new_props,
2352 int propcount,
2353 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002354 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002355 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002356 long col,
2357 int removed)
2358{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002359 char_u *props;
2360 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2361 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002362
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002363 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002364 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002365 textprop_T prop;
2366 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002367
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002368 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002369 if (prop.tp_col == MAXCOL && !last_line)
2370 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002371 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2372
2373 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002374 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002375
Bram Moolenaare175dc62022-08-01 22:18:50 +01002376 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002377 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2378 &prop, sizeof(prop));
2379 else
2380 {
2381 int j;
2382 int found = FALSE;
2383
2384 // Search for continuing prop.
2385 for (j = *props_remaining; j < propcount; ++j)
2386 {
2387 textprop_T op;
2388
2389 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2390 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2391 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002392 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002393 found = TRUE;
2394 op.tp_len += op.tp_col - prop.tp_col;
2395 op.tp_col = prop.tp_col;
2396 // Start/end is taken care of when deleting joined lines
2397 op.tp_flags = prop.tp_flags;
2398 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2399 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002400 }
2401 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002402 if (!found)
2403 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002404 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002405 }
2406}
2407
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002408#endif // FEAT_PROP_POPUP