blob: 44f7ff0a4198669ea8bf47e8feb950597dde0936 [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 Moolenaara9fa8c52023-01-02 18:10:04 +0000101 semsg(_(e_property_type_str_does_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100152 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153 return;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200154
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100155 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
156 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157}
158
159/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200160 * Attach a text property 'type_name' to the text starting
161 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100162 * the buffer "buf" and assign identifier "id".
163 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200164 */
165 static int
166prop_add_one(
167 buf_T *buf,
168 char_u *type_name,
169 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100170 char_u *text_arg,
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100171 int text_padding_left,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100172 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200173 linenr_T start_lnum,
174 linenr_T end_lnum,
175 colnr_T start_col,
176 colnr_T end_col)
177{
178 proptype_T *type;
179 linenr_T lnum;
180 int proplen;
181 char_u *props = NULL;
182 char_u *newprops;
183 size_t textlen;
184 char_u *newtext;
185 int i;
186 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text = text_arg;
188 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189
190 type = lookup_prop_type(type_name, buf);
191 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200193
194 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
195 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000196 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200198 }
199 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
200 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000201 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200203 }
204
205 if (buf->b_ml.ml_mfp == NULL)
206 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000207 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
209 }
210
211 if (text != NULL)
212 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100213 garray_T *gap = &buf->b_textprop_text;
214 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100215
216 // double check we got the right ID
217 if (-id - 1 != gap->ga_len)
218 iemsg("text prop ID mismatch");
219 if (gap->ga_growsize == 0)
220 ga_init2(gap, sizeof(char *), 50);
221 if (ga_grow(gap, 1) == FAIL)
222 goto theend;
223 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100224
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100225 // change any control character (Tab, Newline, etc.) to a Space to make
226 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100227 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100228 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100230 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200231 }
232
233 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
234 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100235 colnr_T col; // start column use in tp_col
236 colnr_T sort_col; // column where it appears
237 long length; // in bytes
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200238
239 // Fetch the line to get the ml_line_len field updated.
240 proplen = get_text_props(buf, lnum, &props, TRUE);
241 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
242
243 if (lnum == start_lnum)
244 col = start_col;
245 else
246 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100247 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200248 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000249 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100250 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200251 }
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100252 sort_col = col;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200253
254 if (lnum == end_lnum)
255 length = end_col - col;
256 else
257 length = (int)textlen - col + 1;
258 if (length > (long)textlen)
259 length = (int)textlen; // can include the end-of-line
260 if (length < 0)
261 length = 0; // zero-width property
262
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100263 if (text_arg != NULL)
264 {
265 length = 1; // text is placed on one character
266 if (col == 0)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100267 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100268 col = MAXCOL; // before or after the line
269 if ((text_flags & TP_FLAG_ALIGN_ABOVE) == 0)
270 sort_col = MAXCOL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100271 length += text_padding_left;
272 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100273 }
274
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200275 // Allocate the new line with space for the new property.
276 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
277 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100278 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200279 // Copy the text, including terminating NUL.
280 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
281
282 // Find the index where to insert the new property.
283 // Since the text properties are not aligned properly when stored with
284 // the text, we need to copy them as bytes before using it as a struct.
285 for (i = 0; i < proplen; ++i)
286 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100287 colnr_T prop_col;
288
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200289 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
290 sizeof(textprop_T));
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100291 // col is MAXCOL when the text goes above or after the line, when
292 // above we should use column zero for sorting
293 prop_col = (tmp_prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
294 ? 0 : tmp_prop.tp_col;
295 if (prop_col >= sort_col)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200296 break;
297 }
298 newprops = newtext + textlen;
299 if (i > 0)
300 mch_memmove(newprops, props, sizeof(textprop_T) * i);
301
302 tmp_prop.tp_col = col;
303 tmp_prop.tp_len = length;
304 tmp_prop.tp_id = id;
305 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100306 tmp_prop.tp_flags = text_flags
307 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100308 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
309 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
310 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200311 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
312 sizeof(textprop_T));
313
314 if (i < proplen)
315 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
316 props + i * sizeof(textprop_T),
317 sizeof(textprop_T) * (proplen - i));
318
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100319 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200320 vim_free(buf->b_ml.ml_line_ptr);
321 buf->b_ml.ml_line_ptr = newtext;
322 buf->b_ml.ml_line_len += sizeof(textprop_T);
323 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
324 }
325
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100326 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200327 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100328 res = OK;
329
330theend:
331 vim_free(text);
332 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200333}
334
335/*
336 * prop_add_list()
337 * First argument specifies the text property:
338 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
339 * Second argument is a List where each item is a List with the following
340 * entries: [lnum, start_col, end_col]
341 */
342 void
343f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
344{
345 dict_T *dict;
346 char_u *type_name;
347 buf_T *buf = curbuf;
348 int id = 0;
349 listitem_T *li;
350 list_T *pos_list;
351 linenr_T start_lnum;
352 colnr_T start_col;
353 linenr_T end_lnum;
354 colnr_T end_col;
355 int error = FALSE;
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100356 int prev_did_emsg = did_emsg;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200357
358 if (check_for_dict_arg(argvars, 0) == FAIL
359 || check_for_list_arg(argvars, 1) == FAIL)
360 return;
361
Bram Moolenaard83392a2022-09-01 12:22:46 +0100362 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364
365 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100366 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200367 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000368 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200369 return;
370 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100371 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200372
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100373 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100374 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200375
376 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
377 return;
378
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100379 // This must be done _before_ we start adding properties because property
380 // changes trigger buffer (memline) reorganisation, which needs this flag
381 // to be correctly set.
382 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200383 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
384 {
385 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
386 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000387 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200388 return;
389 }
390
391 pos_list = li->li_tv.vval.v_list;
392 start_lnum = list_find_nr(pos_list, 0L, &error);
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100393 if (!error)
394 start_col = list_find_nr(pos_list, 1L, &error);
395 if (!error)
396 end_lnum = list_find_nr(pos_list, 2L, &error);
397 if (!error)
398 end_col = list_find_nr(pos_list, 3L, &error);
Bram Moolenaard93009e2022-10-13 14:35:24 +0100399 int this_id = id;
400 if (!error && pos_list->lv_len > 4)
401 this_id = list_find_nr(pos_list, 4L, &error);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200402 if (error || start_lnum <= 0 || start_col <= 0
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100403 || end_lnum <= 0 || end_col <= 0)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200404 {
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100405 if (prev_did_emsg == did_emsg)
406 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200407 return;
408 }
Bram Moolenaard93009e2022-10-13 14:35:24 +0100409 if (prop_add_one(buf, type_name, this_id, NULL, 0, 0,
410 start_lnum, end_lnum, start_col, end_col) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200411 return;
412 }
413
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100414 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200415}
416
417/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100418 * Get the next ID to use for a textprop with text in buffer "buf".
419 */
420 static int
421get_textprop_id(buf_T *buf)
422{
423 // TODO: recycle deleted entries
424 return -(buf->b_textprop_text.ga_len + 1);
425}
426
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000427// Flag that is set when a negative ID isused for a normal text property.
428// It is then impossible to use virtual text properties.
429static int did_use_negative_pop_id = FALSE;
430
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100431/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200432 * Shared between prop_add() and popup_create().
433 * "dict_arg" is the function argument of a dict containing "bufnr".
434 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100435 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200436 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100437 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200438prop_add_common(
439 linenr_T start_lnum,
440 colnr_T start_col,
441 dict_T *dict,
442 buf_T *default_buf,
443 typval_T *dict_arg)
444{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200445 linenr_T end_lnum;
446 colnr_T end_col;
447 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200448 buf_T *buf = default_buf;
449 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100450 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100451 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100452 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100453
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100454 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100455 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000456 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100457 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100458 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100459 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100460
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100461 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100462 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100463 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464 if (end_lnum < start_lnum)
465 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000466 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100467 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100468 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100470 else
471 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100472
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100473 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100474 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100475 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100476
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100477 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100478 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000479 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100480 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100481 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100482 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100483 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100484 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100485 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100486 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100487 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100488 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000489 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100490 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100491 }
492 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100493 else if (start_lnum == end_lnum)
494 end_col = start_col;
495 else
496 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100497
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100498 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100499 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100500
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100501 if (dict_has_key(dict, "text"))
502 {
Bram Moolenaarfb593c52022-09-17 18:57:36 +0100503 if (dict_has_key(dict, "length")
504 || dict_has_key(dict, "end_col")
505 || dict_has_key(dict, "end_lnum"))
506 {
507 emsg(_(e_cannot_use_length_endcol_and_endlnum_with_text));
508 goto theend;
509 }
510
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100511 text = dict_get_string(dict, "text", TRUE);
512 if (text == NULL)
513 goto theend;
514 // use a default length of 1 to make multiple props show up
515 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100516
517 if (dict_has_key(dict, "text_align"))
518 {
519 char_u *p = dict_get_string(dict, "text_align", FALSE);
520
521 if (p == NULL)
522 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100523 if (start_col != 0)
524 {
525 emsg(_(e_can_only_use_text_align_when_column_is_zero));
526 goto theend;
527 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100528 if (STRCMP(p, "right") == 0)
529 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100530 else if (STRCMP(p, "above") == 0)
531 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100532 else if (STRCMP(p, "below") == 0)
533 flags |= TP_FLAG_ALIGN_BELOW;
534 else if (STRCMP(p, "after") != 0)
535 {
536 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
537 goto theend;
538 }
539 }
540
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100541 if (dict_has_key(dict, "text_padding_left"))
542 {
543 text_padding_left = dict_get_number(dict, "text_padding_left");
544 if (text_padding_left < 0)
545 {
546 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
547 goto theend;
548 }
549 }
550
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100551 if (dict_has_key(dict, "text_wrap"))
552 {
553 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100554
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100555 if (p == NULL)
556 goto theend;
557 if (STRCMP(p, "wrap") == 0)
558 flags |= TP_FLAG_WRAP;
559 else if (STRCMP(p, "truncate") != 0)
560 {
561 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
562 goto theend;
563 }
564 }
565 }
566
567 // Column must be 1 or more for a normal text property; when "text" is
568 // present zero means it goes after the line.
569 if (start_col < (text == NULL ? 1 : 0))
570 {
571 semsg(_(e_invalid_column_number_nr), (long)start_col);
572 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100573 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100574 if (start_col > 0 && text_padding_left > 0)
575 {
576 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
577 goto theend;
578 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100579
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200580 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100581 goto theend;
582
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000583 if (id < 0)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100584 {
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000585 if (buf->b_textprop_text.ga_len > 0)
586 {
587 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
588 goto theend;
589 }
590 did_use_negative_pop_id = TRUE;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100591 }
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000592
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100593 if (text != NULL)
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000594 {
595 if (did_use_negative_pop_id)
596 {
597 emsg(_(e_cannot_add_textprop_with_text_after_using_textprop_with_negative_id));
598 goto theend;
599 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100600 id = get_textprop_id(buf);
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000601 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100602
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100603 // This must be done _before_ we add the property because property changes
604 // trigger buffer (memline) reorganisation, which needs this flag to be
605 // correctly set.
606 buf->b_has_textprop = TRUE; // this is never reset
607
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100608 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100609 start_lnum, end_lnum, start_col, end_col);
610 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100611
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100612 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100613
614theend:
615 vim_free(text);
616 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100617}
618
619/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100620 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100621 * Returns the number of text properties and, when non-zero, a pointer to the
622 * first one in "props" (note that it is not aligned, therefore the char_u
623 * pointer).
624 */
625 int
626get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
627{
628 char_u *text;
629 size_t textlen;
630 size_t proplen;
631
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100632 // Be quick when no text property types have been defined or the buffer,
633 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200634 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100635 return 0;
636
637 // Fetch the line to get the ml_line_len field updated.
638 text = ml_get_buf(buf, lnum, will_change);
639 textlen = STRLEN(text) + 1;
640 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100641 if (proplen == 0)
642 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100643 if (proplen % sizeof(textprop_T) != 0)
644 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100645 iemsg(e_text_property_info_corrupted);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100646 return 0;
647 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100648 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100649 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100650}
651
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100652/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100653 * Return the number of text properties with "above" or "below" alignment in
654 * line "lnum". A "right" aligned property also goes below after a "below" or
655 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100656 */
657 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100658prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100659{
660 char_u *props;
661 int count = get_text_props(buf, lnum, &props, FALSE);
662 int result = 0;
663 textprop_T prop;
664 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100665 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100666
667 if (count == 0)
668 return 0;
669 for (i = 0; i < count; ++i)
670 {
671 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar89469d12022-12-02 20:46:26 +0000672 if (prop.tp_col == MAXCOL && text_prop_type_valid(buf, &prop))
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100673 {
674 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
675 || (next_right_goes_below
676 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
677 {
678 next_right_goes_below = TRUE;
679 ++result;
680 }
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100681 else if (prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
682 {
683 next_right_goes_below = FALSE;
684 ++result;
685 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100686 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
687 next_right_goes_below = TRUE;
688 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100689 }
690 return result;
691}
692
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200693/**
694 * Return the number of text properties on line "lnum" in the current buffer.
695 * When "only_starting" is true only text properties starting in this line will
696 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100697 * When "last_line" is FALSE then text properties after the line are not
698 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200699 */
700 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100701count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200702{
703 char_u *props;
704 int proplen = get_text_props(curbuf, lnum, &props, 0);
705 int result = proplen;
706 int i;
707 textprop_T prop;
708
Bram Moolenaare175dc62022-08-01 22:18:50 +0100709 for (i = 0; i < proplen; ++i)
710 {
711 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100712 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100713 // previous line, or when not in the last line and it is virtual text
714 // after the line.
715 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
Bram Moolenaar89469d12022-12-02 20:46:26 +0000716 || (!last_line && prop.tp_col == MAXCOL)
717 || !text_prop_type_valid(curbuf, &prop))
Bram Moolenaare175dc62022-08-01 22:18:50 +0100718 --result;
719 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200720 return result;
721}
722
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100723static textprop_T *text_prop_compare_props;
724static buf_T *text_prop_compare_buf;
725
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000726/*
727 * Score for sorting on position of the text property: 0: above,
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100728 * 1: after (default), 2: right, 3: below (comes last)
729 */
730 static int
731text_prop_order(int flags)
732{
733 if (flags & TP_FLAG_ALIGN_ABOVE)
734 return 0;
735 if (flags & TP_FLAG_ALIGN_RIGHT)
736 return 2;
737 if (flags & TP_FLAG_ALIGN_BELOW)
738 return 3;
739 return 1;
740}
741
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100742/*
743 * Function passed to qsort() to sort text properties.
744 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
745 * both have the same priority.
746 */
747 static int
748text_prop_compare(const void *s1, const void *s2)
749{
750 int idx1, idx2;
751 textprop_T *tp1, *tp2;
752 proptype_T *pt1, *pt2;
753 colnr_T col1, col2;
754
755 idx1 = *(int *)s1;
756 idx2 = *(int *)s2;
757 tp1 = &text_prop_compare_props[idx1];
758 tp2 = &text_prop_compare_props[idx2];
759 col1 = tp1->tp_col;
760 col2 = tp2->tp_col;
761 if (col1 == MAXCOL && col2 == MAXCOL)
762 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100763 int order1 = text_prop_order(tp1->tp_flags);
764 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100765
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100766 // both props add text before or after the line, sort on order where it
767 // is added
768 if (order1 != order2)
769 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100770 }
771
772 // property that inserts text has priority over one that doesn't
773 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
774 return tp1->tp_id < 0 ? 1 : -1;
775
776 // check highest priority, defined by the type
777 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
778 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
779 if (pt1 != pt2)
780 {
781 if (pt1 == NULL)
782 return -1;
783 if (pt2 == NULL)
784 return 1;
785 if (pt1->pt_priority != pt2->pt_priority)
786 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
787 }
788
789 // same priority, one that starts first wins
790 if (col1 != col2)
791 return col1 < col2 ? 1 : -1;
792
793 // for a property with text the id can be used as tie breaker
794 if (tp1->tp_id < 0)
795 return tp1->tp_id > tp2->tp_id ? 1 : -1;
796
797 return 0;
798}
799
800/*
801 * Sort "count" text properties using an array if indexes "idxs" into the list
802 * of text props "props" for buffer "buf".
803 */
804 void
805sort_text_props(
806 buf_T *buf,
807 textprop_T *props,
808 int *idxs,
809 int count)
810{
811 text_prop_compare_buf = buf;
812 text_prop_compare_props = props;
813 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
814}
815
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100816/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200817 * Find text property "type_id" in the visible lines of window "wp".
818 * Match "id" when it is > 0.
819 * Returns FAIL when not found.
820 */
821 int
Bram Moolenaar89469d12022-12-02 20:46:26 +0000822find_visible_prop(
823 win_T *wp,
824 int type_id,
825 int id,
826 textprop_T *prop,
827 linenr_T *found_lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200828{
Bram Moolenaar89469d12022-12-02 20:46:26 +0000829 // return when "type_id" no longer exists
830 if (text_prop_type_by_id(wp->w_buffer, type_id) == NULL)
831 return FAIL;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200832
833 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100834 validate_botline_win(wp);
Bram Moolenaar89469d12022-12-02 20:46:26 +0000835 for (linenr_T lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200836 {
Bram Moolenaar89469d12022-12-02 20:46:26 +0000837 char_u *props;
838 int count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
839 for (int i = 0; i < count; ++i)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200840 {
841 mch_memmove(prop, props + i * sizeof(textprop_T),
842 sizeof(textprop_T));
843 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
844 {
845 *found_lnum = lnum;
846 return OK;
847 }
848 }
849 }
850 return FAIL;
851}
852
853/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100854 * Set the text properties for line "lnum" to "props" with length "len".
855 * If "len" is zero text properties are removed, "props" is not used.
856 * Any existing text properties are dropped.
857 * Only works for the current buffer.
858 */
859 static void
860set_text_props(linenr_T lnum, char_u *props, int len)
861{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100862 char_u *text;
863 char_u *newtext;
864 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100865
866 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100867 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100868 newtext = alloc(textlen + len);
869 if (newtext == NULL)
870 return;
871 mch_memmove(newtext, text, textlen);
872 if (len > 0)
873 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100874 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100875 vim_free(curbuf->b_ml.ml_line_ptr);
876 curbuf->b_ml.ml_line_ptr = newtext;
877 curbuf->b_ml.ml_line_len = textlen + len;
878 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
879}
880
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100881/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100882 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100883 */
884 void
885add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
886{
887 char_u *text;
888 char_u *newtext;
889 int proplen = text_prop_count * (int)sizeof(textprop_T);
890
891 text = ml_get(lnum);
892 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
893 if (newtext == NULL)
894 return;
895 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
896 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
897 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
898 vim_free(curbuf->b_ml.ml_line_ptr);
899 curbuf->b_ml.ml_line_ptr = newtext;
900 curbuf->b_ml.ml_line_len += proplen;
901 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
902}
903
Bram Moolenaare44336b2022-08-07 18:20:08 +0100904/*
905 * Function passed to qsort() for sorting proptype_T on pt_id.
906 */
907 static int
908compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100909{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100910 proptype_T *tp1 = *(proptype_T **)s1;
911 proptype_T *tp2 = *(proptype_T **)s2;
912
913 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
914}
915
916 static proptype_T *
917find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
918{
919 int low = 0;
920 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100921
Bram Moolenaar10246902022-08-08 17:08:05 +0100922 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100923 return NULL;
924
dundargocc57b5bc2022-11-02 13:30:51 +0000925 // Make the lookup faster by creating an array with pointers to
Bram Moolenaare44336b2022-08-07 18:20:08 +0100926 // hashtable entries, sorted on pt_id.
927 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100928 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100929 long todo;
930 hashitem_T *hi;
931 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100932
Bram Moolenaare44336b2022-08-07 18:20:08 +0100933 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
934 if (*array == NULL)
935 return NULL;
936 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000937 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100938 {
939 if (!HASHITEM_EMPTY(hi))
940 {
941 (*array)[i++] = HI2PT(hi);
942 --todo;
943 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100944 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100945 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
946 }
947
948 // binary search in the sorted array
949 high = ht->ht_used;
950 while (high > low)
951 {
952 int m = (high + low) / 2;
953
954 if ((*array)[m]->pt_id == id)
955 return (*array)[m];
956 if ((*array)[m]->pt_id > id)
957 high = m;
958 else
959 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100960 }
961 return NULL;
962}
963
964/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100965 * Fill 'dict' with text properties in 'prop'.
966 */
967 static void
968prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
969{
970 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200971 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100972
973 dict_add_number(dict, "col", prop->tp_col);
974 dict_add_number(dict, "length", prop->tp_len);
975 dict_add_number(dict, "id", prop->tp_id);
976 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
977 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200978
Bram Moolenaare44336b2022-08-07 18:20:08 +0100979 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200980 if (pt == NULL)
981 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100982 pt = find_type_by_id(global_proptypes, &global_proparray,
983 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200984 buflocal = FALSE;
985 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100986 if (pt != NULL)
987 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200988
989 if (buflocal)
990 dict_add_number(dict, "type_bufnr", buf->b_fnum);
991 else
992 dict_add_number(dict, "type_bufnr", 0);
Yegappan Lakshmananf9037f12023-08-20 18:27:45 +0200993 if (prop->tp_id < 0)
994 {
995 // virtual text property
996 garray_T *gap = &buf->b_textprop_text;
997 char_u *text;
998
999 // negate the property id to get the string index
1000 text = ((char_u **)gap->ga_data)[-prop->tp_id - 1];
1001 dict_add_string(dict, "text", text);
1002
1003 // text_align
1004 char_u *text_align = NULL;
1005 if (prop->tp_flags & TP_FLAG_ALIGN_RIGHT)
1006 text_align = (char_u *)"right";
1007 else if (prop->tp_flags & TP_FLAG_ALIGN_ABOVE)
1008 text_align = (char_u *)"above";
1009 else if (prop->tp_flags & TP_FLAG_ALIGN_BELOW)
1010 text_align = (char_u *)"below";
1011 if (text_align != NULL)
1012 dict_add_string(dict, "text_align", text_align);
1013
1014 // text_wrap
1015 if (prop->tp_flags & TP_FLAG_WRAP)
1016 dict_add_string(dict, "text_wrap", (char_u *)"wrap");
1017 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001018}
1019
1020/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001021 * Find a property type by ID in "buf" or globally.
1022 * Returns NULL if not found.
1023 */
1024 proptype_T *
1025text_prop_type_by_id(buf_T *buf, int id)
1026{
1027 proptype_T *type;
1028
Bram Moolenaare44336b2022-08-07 18:20:08 +01001029 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001030 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001031 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001032 return type;
1033}
1034
1035/*
Bram Moolenaar89469d12022-12-02 20:46:26 +00001036 * Return TRUE if "prop" is a valid text property type.
1037 */
1038 int
1039text_prop_type_valid(buf_T *buf, textprop_T *prop)
1040{
1041 return text_prop_type_by_id(buf, prop->tp_type) != NULL;
1042}
1043
1044/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001045 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
1046 */
1047 void
1048f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
1049{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001050 linenr_T start;
1051 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001052 linenr_T lnum;
1053 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001054 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001055
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001056 if (in_vim9script()
1057 && (check_for_number_arg(argvars, 0) == FAIL
1058 || check_for_opt_number_arg(argvars, 1) == FAIL
1059 || (argvars[1].v_type != VAR_UNKNOWN
1060 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1061 return;
1062
1063 start = tv_get_number(&argvars[0]);
1064 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001065 if (argvars[1].v_type != VAR_UNKNOWN)
1066 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001067 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001068 if (argvars[2].v_type != VAR_UNKNOWN)
1069 {
1070 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
1071 return;
1072 }
1073 }
1074 if (start < 1 || end < 1)
1075 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001076 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001077 return;
1078 }
1079
1080 for (lnum = start; lnum <= end; ++lnum)
1081 {
1082 char_u *text;
1083 size_t len;
1084
1085 if (lnum > buf->b_ml.ml_line_count)
1086 break;
1087 text = ml_get_buf(buf, lnum, FALSE);
1088 len = STRLEN(text) + 1;
1089 if ((size_t)buf->b_ml.ml_line_len > len)
1090 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001091 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001092 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1093 {
1094 char_u *newtext = vim_strsave(text);
1095
1096 // need to allocate the line now
1097 if (newtext == NULL)
1098 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001099 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1100 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001101 buf->b_ml.ml_line_ptr = newtext;
1102 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1103 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001104 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001105 }
1106 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001107 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001108 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001109}
1110
1111/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001112 * prop_find({props} [, {direction}])
1113 */
1114 void
1115f_prop_find(typval_T *argvars, typval_T *rettv)
1116{
1117 pos_T *cursor = &curwin->w_cursor;
1118 dict_T *dict;
1119 buf_T *buf = curbuf;
1120 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001121 int lnum_start;
1122 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001123 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001124 int id = 0;
1125 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001126 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001127 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001128 int lnum = -1;
1129 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001130 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001131 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001132
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001133 if (in_vim9script()
1134 && (check_for_dict_arg(argvars, 0) == FAIL
1135 || check_for_opt_string_arg(argvars, 1) == FAIL))
1136 return;
1137
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001138 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001139 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001140 dict = argvars[0].vval.v_dict;
1141
1142 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1143 return;
1144 if (buf->b_ml.ml_mfp == NULL)
1145 return;
1146
1147 if (argvars[1].v_type != VAR_UNKNOWN)
1148 {
1149 char_u *dir_s = tv_get_string(&argvars[1]);
1150
1151 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001152 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001153 else if (*dir_s != 'f')
1154 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001155 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001156 return;
1157 }
1158 }
1159
1160 di = dict_find(dict, (char_u *)"lnum", -1);
1161 if (di != NULL)
1162 lnum = tv_get_number(&di->di_tv);
1163
1164 di = dict_find(dict, (char_u *)"col", -1);
1165 if (di != NULL)
1166 col = tv_get_number(&di->di_tv);
1167
1168 if (lnum == -1)
1169 {
1170 lnum = cursor->lnum;
1171 col = cursor->col + 1;
1172 }
1173 else if (col == -1)
1174 col = 1;
1175
1176 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1177 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001178 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001179 return;
1180 }
1181
Bram Moolenaard61efa52022-07-23 09:52:04 +01001182 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001183
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001184 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001185 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001186 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001187 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001188 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001189 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001190 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001191 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001192 proptype_T *type = lookup_prop_type(name, buf);
1193
1194 if (type == NULL)
1195 return;
1196 type_id = type->pt_id;
1197 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001198 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001199 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001200 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001201 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001202 return;
1203 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001204 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001205 {
Ben Jacksona7704222022-08-20 20:54:51 +01001206 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001207 return;
1208 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001209
1210 lnum_start = lnum;
1211
1212 if (rettv_dict_alloc(rettv) == FAIL)
1213 return;
1214
1215 while (1)
1216 {
1217 char_u *text = ml_get_buf(buf, lnum, FALSE);
1218 size_t textlen = STRLEN(text) + 1;
1219 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001220 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001221 int i;
1222 textprop_T prop;
1223 int prop_start;
1224 int prop_end;
1225
LemonBoy9bd3ce22022-04-18 21:54:02 +01001226 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001227 {
1228 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001229 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001230
LemonBoy9bd3ce22022-04-18 21:54:02 +01001231 // For the very first line try to find the first property before or
1232 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001233 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001234 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001235 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001236 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001237 if (prop.tp_col > col)
1238 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001239 }
1240 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1241 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001242 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001243 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001244 : (id_found && prop.tp_id == id)
1245 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001246 {
1247 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001248 if (lnum_start == lnum
1249 && col >= prop.tp_col
1250 && (col <= prop.tp_col + prop.tp_len
1251 - (prop.tp_len != 0)))
1252 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001253
LemonBoy9bd3ce22022-04-18 21:54:02 +01001254 // The property was not continued from last line, it starts on
1255 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001256 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001257 // The property does not continue on the next line, it ends on
1258 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001259 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001260 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001261 seen_end = 1;
1262
1263 // Skip lines without the start flag.
1264 if (!prop_start)
1265 {
1266 // Always search backwards for start when search started
1267 // on a prop and we're not skipping.
1268 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001269 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001270 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001271 }
1272
1273 // If skipstart is true, skip the prop at start pos (even if
1274 // continued from another line).
1275 if (start_pos_has_prop && skipstart && !seen_end)
1276 {
1277 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001278 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001279 }
1280
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001281 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1282 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1283
1284 return;
1285 }
1286 }
1287
1288 if (dir > 0)
1289 {
1290 if (lnum >= buf->b_ml.ml_line_count)
1291 break;
1292 lnum++;
1293 }
1294 else
1295 {
1296 if (lnum <= 1)
1297 break;
1298 lnum--;
1299 }
1300 }
1301}
1302
1303/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001304 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1305 */
1306 static int
1307prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1308{
1309 int i;
1310
1311 for (i = 0; i < len; i++)
1312 if (types_or_ids[i] == type_or_id)
1313 return TRUE;
1314
1315 return FALSE;
1316}
1317
1318/*
1319 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1320 * If 'prop_types' is not NULL, then return only the text properties with
1321 * matching property type in the 'prop_types' array.
1322 * If 'prop_ids' is not NULL, then return only the text properties with
1323 * an identifier in the 'props_ids' array.
1324 * If 'add_lnum' is TRUE, then add the line number also to the text property
1325 * dictionary.
1326 */
1327 static void
1328get_props_in_line(
1329 buf_T *buf,
1330 linenr_T lnum,
1331 int *prop_types,
1332 int prop_types_len,
1333 int *prop_ids,
1334 int prop_ids_len,
1335 list_T *retlist,
1336 int add_lnum)
1337{
1338 char_u *text = ml_get_buf(buf, lnum, FALSE);
1339 size_t textlen = STRLEN(text) + 1;
1340 int count;
1341 int i;
1342 textprop_T prop;
1343
1344 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1345 for (i = 0; i < count; ++i)
1346 {
1347 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1348 sizeof(textprop_T));
1349 if ((prop_types == NULL
1350 || prop_type_or_id_in_list(prop_types, prop_types_len,
1351 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001352 && (prop_ids == NULL
1353 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1354 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001355 {
1356 dict_T *d = dict_alloc();
1357
1358 if (d == NULL)
1359 break;
1360 prop_fill_dict(d, &prop, buf);
1361 if (add_lnum)
1362 dict_add_number(d, "lnum", lnum);
1363 list_append_dict(retlist, d);
1364 }
1365 }
1366}
1367
1368/*
1369 * Convert a List of property type names into an array of property type
1370 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1371 * error. 'num_types' is set to the number of returned property types.
1372 */
1373 static int *
1374get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1375{
1376 int *prop_types;
1377 listitem_T *li;
1378 int i;
1379 char_u *name;
1380 proptype_T *type;
1381
1382 *num_types = 0;
1383
1384 prop_types = ALLOC_MULT(int, list_len(l));
1385 if (prop_types == NULL)
1386 return NULL;
1387
1388 i = 0;
1389 FOR_ALL_LIST_ITEMS(l, li)
1390 {
1391 if (li->li_tv.v_type != VAR_STRING)
1392 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001393 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001394 goto errret;
1395 }
1396 name = li->li_tv.vval.v_string;
1397 if (name == NULL)
1398 goto errret;
1399
1400 type = lookup_prop_type(name, buf);
1401 if (type == NULL)
1402 goto errret;
1403 prop_types[i++] = type->pt_id;
1404 }
1405
1406 *num_types = i;
1407 return prop_types;
1408
1409errret:
1410 VIM_CLEAR(prop_types);
1411 return NULL;
1412}
1413
1414/*
1415 * Convert a List of property identifiers into an array of property
1416 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1417 * error. 'num_ids' is set to the number of returned property identifiers.
1418 */
1419 static int *
1420get_prop_ids_from_list(list_T *l, int *num_ids)
1421{
1422 int *prop_ids;
1423 listitem_T *li;
1424 int i;
1425 int id;
1426 int error;
1427
1428 *num_ids = 0;
1429
1430 prop_ids = ALLOC_MULT(int, list_len(l));
1431 if (prop_ids == NULL)
1432 return NULL;
1433
1434 i = 0;
1435 FOR_ALL_LIST_ITEMS(l, li)
1436 {
1437 error = FALSE;
1438 id = tv_get_number_chk(&li->li_tv, &error);
1439 if (error)
1440 goto errret;
1441
1442 prop_ids[i++] = id;
1443 }
1444
1445 *num_ids = i;
1446 return prop_ids;
1447
1448errret:
1449 VIM_CLEAR(prop_ids);
1450 return NULL;
1451}
1452
1453/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001454 * prop_list({lnum} [, {bufnr}])
1455 */
1456 void
1457f_prop_list(typval_T *argvars, typval_T *rettv)
1458{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001459 linenr_T lnum;
1460 linenr_T start_lnum;
1461 linenr_T end_lnum;
1462 buf_T *buf = curbuf;
1463 int add_lnum = FALSE;
1464 int *prop_types = NULL;
1465 int prop_types_len = 0;
1466 int *prop_ids = NULL;
1467 int prop_ids_len = 0;
1468 list_T *l;
1469 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001470
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001471 if (in_vim9script()
1472 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001473 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001474 return;
1475
Bram Moolenaar93a10962022-06-16 11:42:09 +01001476 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001477 return;
1478
1479 // default: get text properties on current line
1480 start_lnum = tv_get_number(&argvars[0]);
1481 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001482 if (argvars[1].v_type != VAR_UNKNOWN)
1483 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001484 dict_T *d;
1485
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001486 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001487 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001488 d = argvars[1].vval.v_dict;
1489
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001490 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1491 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001492
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001493 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001495 if (di->di_tv.v_type != VAR_NUMBER)
1496 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001497 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001498 return;
1499 }
1500 end_lnum = tv_get_number(&di->di_tv);
1501 if (end_lnum < 0)
1502 // negative end_lnum is used as an offset from the last buffer
1503 // line
1504 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1505 else if (end_lnum > buf->b_ml.ml_line_count)
1506 end_lnum = buf->b_ml.ml_line_count;
1507 add_lnum = TRUE;
1508 }
1509 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1510 {
1511 if (di->di_tv.v_type != VAR_LIST)
1512 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001513 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001514 return;
1515 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001516
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001517 l = di->di_tv.vval.v_list;
1518 if (l != NULL && list_len(l) > 0)
1519 {
1520 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1521 if (prop_types == NULL)
1522 return;
1523 }
1524 }
1525 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1526 {
1527 if (di->di_tv.v_type != VAR_LIST)
1528 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001529 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001530 goto errret;
1531 }
1532
1533 l = di->di_tv.vval.v_list;
1534 if (l != NULL && list_len(l) > 0)
1535 {
1536 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1537 if (prop_ids == NULL)
1538 goto errret;
1539 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001540 }
1541 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001542 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1543 || end_lnum < 1 || end_lnum < start_lnum)
1544 emsg(_(e_invalid_range));
1545 else
1546 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1547 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1548 prop_ids, prop_ids_len,
1549 rettv->vval.v_list, add_lnum);
1550
1551errret:
1552 VIM_CLEAR(prop_types);
1553 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001554}
1555
1556/*
1557 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1558 */
1559 void
1560f_prop_remove(typval_T *argvars, typval_T *rettv)
1561{
1562 linenr_T start = 1;
1563 linenr_T end = 0;
1564 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001565 linenr_T first_changed = 0;
1566 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001567 dict_T *dict;
1568 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001569 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001570 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001571 int type_id = -1; // for a single "type"
1572 int *type_ids = NULL; // array, for a list of "types", allocated
1573 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001574 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001575 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001576
1577 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001578
1579 if (in_vim9script()
1580 && (check_for_dict_arg(argvars, 0) == FAIL
1581 || check_for_opt_number_arg(argvars, 1) == FAIL
1582 || (argvars[1].v_type != VAR_UNKNOWN
1583 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1584 return;
1585
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001586 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001587 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001588
1589 if (argvars[1].v_type != VAR_UNKNOWN)
1590 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001591 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001592 end = start;
1593 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001594 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001595 if (start < 1 || end < 1)
1596 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001597 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001598 return;
1599 }
1600 }
1601
1602 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001603 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1604 return;
1605 if (buf->b_ml.ml_mfp == NULL)
1606 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001607
Bram Moolenaard61efa52022-07-23 09:52:04 +01001608 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001609
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001610 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001611 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001612
1613 // if a specific type was supplied "type": check that (and ignore "types".
1614 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001615 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001616 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001617 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001618 proptype_T *type = lookup_prop_type(name, buf);
1619
1620 if (type == NULL)
1621 return;
1622 type_id = type->pt_id;
1623 }
Ben Jacksona7704222022-08-20 20:54:51 +01001624 if (dict_has_key(dict, "types"))
1625 {
1626 typval_T types;
1627 listitem_T *li = NULL;
1628
1629 dict_get_tv(dict, "types", &types);
1630 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1631 {
1632 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1633
1634 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1635 {
1636 proptype_T *prop_type;
1637
1638 if (li->li_tv.v_type != VAR_STRING)
1639 continue;
1640
1641 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1642
1643 if (!prop_type)
1644 goto cleanup_prop_remove;
1645
1646 type_ids[num_type_ids++] = prop_type->pt_id;
1647 }
1648 }
1649 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001650 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001651
Ben Jacksona7704222022-08-20 20:54:51 +01001652 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001653 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001654 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001655 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001656 }
Ben Jacksona7704222022-08-20 20:54:51 +01001657 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001658 {
Ben Jacksona7704222022-08-20 20:54:51 +01001659 emsg(_(e_need_id_and_type_or_types_with_both));
1660 goto cleanup_prop_remove;
1661 }
1662 if (type_id != -1 && num_type_ids > 0)
1663 {
1664 emsg(_(e_cannot_specify_both_type_and_types));
1665 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001666 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001667
1668 if (end == 0)
1669 end = buf->b_ml.ml_line_count;
1670 for (lnum = start; lnum <= end; ++lnum)
1671 {
1672 char_u *text;
1673 size_t len;
1674
1675 if (lnum > buf->b_ml.ml_line_count)
1676 break;
1677 text = ml_get_buf(buf, lnum, FALSE);
1678 len = STRLEN(text) + 1;
1679 if ((size_t)buf->b_ml.ml_line_len > len)
1680 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001681 static textprop_T textprop; // static because of alignment
1682 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001683
1684 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1685 / sizeof(textprop_T); ++idx)
1686 {
1687 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1688 + idx * sizeof(textprop_T);
1689 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001690 int matches_id = 0;
1691 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001692
1693 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001694
1695 matches_id = textprop.tp_id == id;
1696 if (num_type_ids > 0)
1697 {
1698 int idx2;
1699
1700 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1701 matches_type = textprop.tp_type == type_ids[idx2];
1702 }
1703 else
1704 {
1705 matches_type = textprop.tp_type == type_id;
1706 }
1707
1708 if (both ? matches_id && matches_type
1709 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001710 {
1711 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1712 {
1713 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1714
1715 // need to allocate the line to be able to change it
1716 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001717 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001718 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1719 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001720 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1721 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001722 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001723 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1724
1725 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001726 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001727 }
1728
1729 taillen = buf->b_ml.ml_line_len - len
1730 - (idx + 1) * sizeof(textprop_T);
1731 if (taillen > 0)
1732 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1733 taillen);
1734 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1735 --idx;
1736
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001737 if (textprop.tp_id < 0)
1738 {
1739 garray_T *gap = &buf->b_textprop_text;
1740 int ii = -textprop.tp_id - 1;
1741
1742 // negative ID: property with text - free the text
1743 if (ii < gap->ga_len)
1744 {
1745 char_u **p = ((char_u **)gap->ga_data) + ii;
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00001746 VIM_CLEAR(*p);
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001747 did_remove_text = TRUE;
1748 }
1749 }
1750
Bram Moolenaar965c0442021-05-17 00:22:06 +02001751 if (first_changed == 0)
1752 first_changed = lnum;
1753 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001754 ++rettv->vval.v_number;
1755 if (!do_all)
1756 break;
1757 }
1758 }
1759 }
1760 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001761
Bram Moolenaar965c0442021-05-17 00:22:06 +02001762 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001763 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001764 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001765 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001766 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001767 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001768
1769 if (did_remove_text)
1770 {
1771 garray_T *gap = &buf->b_textprop_text;
1772
1773 // Reduce the growarray size for NULL pointers at the end.
1774 while (gap->ga_len > 0
1775 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1776 --gap->ga_len;
1777 }
Ben Jacksona7704222022-08-20 20:54:51 +01001778
1779cleanup_prop_remove:
1780 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001781}
1782
1783/*
1784 * Common for f_prop_type_add() and f_prop_type_change().
1785 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001786 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001787prop_type_set(typval_T *argvars, int add)
1788{
1789 char_u *name;
1790 buf_T *buf = NULL;
1791 dict_T *dict;
1792 dictitem_T *di;
1793 proptype_T *prop;
1794
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001795 if (in_vim9script()
1796 && (check_for_string_arg(argvars, 0) == FAIL
1797 || check_for_dict_arg(argvars, 1) == FAIL))
1798 return;
1799
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001800 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001801 if (*name == NUL)
1802 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001803 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001804 return;
1805 }
1806
1807 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1808 return;
1809 dict = argvars[1].vval.v_dict;
1810
Bram Moolenaare44336b2022-08-07 18:20:08 +01001811 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001812 if (add)
1813 {
1814 hashtab_T **htp;
1815
1816 if (prop != NULL)
1817 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001818 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001819 return;
1820 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001821 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001822 if (prop == NULL)
1823 return;
1824 STRCPY(prop->pt_name, name);
1825 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001826 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001827 if (buf == NULL)
1828 {
1829 htp = &global_proptypes;
1830 VIM_CLEAR(global_proparray);
1831 }
1832 else
1833 {
1834 htp = &buf->b_proptypes;
1835 VIM_CLEAR(buf->b_proparray);
1836 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001837 if (*htp == NULL)
1838 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001839 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001840 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001841 {
1842 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001843 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001844 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001845 hash_init(*htp);
1846 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001847 hash_add(*htp, PT2HIKEY(prop), "prop type");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001848 }
1849 else
1850 {
1851 if (prop == NULL)
1852 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001853 semsg(_(e_property_type_str_does_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001854 return;
1855 }
1856 }
1857
1858 if (dict != NULL)
1859 {
1860 di = dict_find(dict, (char_u *)"highlight", -1);
1861 if (di != NULL)
1862 {
1863 char_u *highlight;
1864 int hl_id = 0;
1865
Bram Moolenaard61efa52022-07-23 09:52:04 +01001866 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001867 if (highlight != NULL && *highlight != NUL)
1868 hl_id = syn_name2id(highlight);
1869 if (hl_id <= 0)
1870 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001871 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001872 highlight == NULL ? (char_u *)"" : highlight);
1873 return;
1874 }
1875 prop->pt_hl_id = hl_id;
1876 }
1877
Bram Moolenaar58187f12019-05-05 16:33:47 +02001878 di = dict_find(dict, (char_u *)"combine", -1);
1879 if (di != NULL)
1880 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001881 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001882 prop->pt_flags |= PT_FLAG_COMBINE;
1883 else
1884 prop->pt_flags &= ~PT_FLAG_COMBINE;
1885 }
1886
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001887 di = dict_find(dict, (char_u *)"override", -1);
1888 if (di != NULL)
1889 {
1890 if (tv_get_bool(&di->di_tv))
1891 prop->pt_flags |= PT_FLAG_OVERRIDE;
1892 else
1893 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1894 }
1895
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001896 di = dict_find(dict, (char_u *)"priority", -1);
1897 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001898 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001899
1900 di = dict_find(dict, (char_u *)"start_incl", -1);
1901 if (di != NULL)
1902 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001903 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001904 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1905 else
1906 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1907 }
1908
1909 di = dict_find(dict, (char_u *)"end_incl", -1);
1910 if (di != NULL)
1911 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001912 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001913 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1914 else
1915 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1916 }
1917 }
1918}
1919
1920/*
1921 * prop_type_add({name}, {props})
1922 */
1923 void
1924f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1925{
1926 prop_type_set(argvars, TRUE);
1927}
1928
1929/*
1930 * prop_type_change({name}, {props})
1931 */
1932 void
1933f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1934{
1935 prop_type_set(argvars, FALSE);
1936}
1937
1938/*
1939 * prop_type_delete({name} [, {bufnr}])
1940 */
1941 void
1942f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1943{
1944 char_u *name;
1945 buf_T *buf = NULL;
1946 hashitem_T *hi;
1947
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001948 if (in_vim9script()
1949 && (check_for_string_arg(argvars, 0) == FAIL
1950 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1951 return;
1952
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001953 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001954 if (*name == NUL)
1955 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001956 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001957 return;
1958 }
1959
1960 if (argvars[1].v_type != VAR_UNKNOWN)
1961 {
1962 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1963 return;
1964 }
1965
Bram Moolenaare44336b2022-08-07 18:20:08 +01001966 hi = find_prop_type_hi(name, buf);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001967 if (hi == NULL)
1968 return;
1969
1970 hashtab_T *ht;
1971 proptype_T *prop = HI2PT(hi);
1972
1973 if (buf == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001974 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001975 ht = global_proptypes;
1976 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001977 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001978 else
1979 {
1980 ht = buf->b_proptypes;
1981 VIM_CLEAR(buf->b_proparray);
1982 }
1983 hash_remove(ht, hi, "prop type delete");
1984 vim_free(prop);
1985
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001986 // currently visible text properties will disappear
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001987 redraw_all_later(UPD_CLEAR);
1988 changed_window_setting_buf(buf == NULL ? curbuf : buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001989}
1990
1991/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001992 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001993 */
1994 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001995f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001996{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001997 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001998
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001999 if (in_vim9script()
2000 && (check_for_string_arg(argvars, 0) == FAIL
2001 || check_for_opt_dict_arg(argvars, 1) == FAIL))
2002 return;
2003
2004 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002005 if (*name == NUL)
2006 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00002007 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002008 return;
2009 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002010
2011 if (rettv_dict_alloc(rettv) == FAIL)
2012 return;
2013
2014 proptype_T *prop = NULL;
2015 buf_T *buf = NULL;
2016
2017 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002018 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002019 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
2020 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002021 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002022
2023 prop = find_prop_type(name, buf);
2024 if (prop == NULL)
2025 return;
2026
2027 dict_T *d = rettv->vval.v_dict;
2028
2029 if (prop->pt_hl_id > 0)
2030 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
2031 dict_add_number(d, "priority", prop->pt_priority);
2032 dict_add_number(d, "combine",
2033 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
2034 dict_add_number(d, "start_incl",
2035 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
2036 dict_add_number(d, "end_incl",
2037 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
2038 if (buf != NULL)
2039 dict_add_number(d, "bufnr", buf->b_fnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002040}
2041
2042 static void
2043list_types(hashtab_T *ht, list_T *l)
2044{
2045 long todo;
2046 hashitem_T *hi;
2047
2048 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002049 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002050 {
2051 if (!HASHITEM_EMPTY(hi))
2052 {
2053 proptype_T *prop = HI2PT(hi);
2054
2055 list_append_string(l, prop->pt_name, -1);
2056 --todo;
2057 }
2058 }
2059}
2060
2061/*
2062 * prop_type_list([{bufnr}])
2063 */
2064 void
2065f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
2066{
2067 buf_T *buf = NULL;
2068
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002069 if (rettv_list_alloc(rettv) == FAIL)
2070 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002071
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002072 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
2073 return;
2074
2075 if (argvars[0].v_type != VAR_UNKNOWN)
2076 {
2077 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2078 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002079 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002080 if (buf == NULL)
2081 {
2082 if (global_proptypes != NULL)
2083 list_types(global_proptypes, rettv->vval.v_list);
2084 }
2085 else if (buf->b_proptypes != NULL)
2086 list_types(buf->b_proptypes, rettv->vval.v_list);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002087}
2088
2089/*
2090 * Free all property types in "ht".
2091 */
2092 static void
2093clear_ht_prop_types(hashtab_T *ht)
2094{
2095 long todo;
2096 hashitem_T *hi;
2097
2098 if (ht == NULL)
2099 return;
2100
2101 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002102 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 proptype_T *prop = HI2PT(hi);
2107
2108 vim_free(prop);
2109 --todo;
2110 }
2111 }
2112
2113 hash_clear(ht);
2114 vim_free(ht);
2115}
2116
2117#if defined(EXITFREE) || defined(PROTO)
2118/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002119 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002120 */
2121 void
2122clear_global_prop_types(void)
2123{
2124 clear_ht_prop_types(global_proptypes);
2125 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002126 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002127}
2128#endif
2129
2130/*
2131 * Free all property types for "buf".
2132 */
2133 void
2134clear_buf_prop_types(buf_T *buf)
2135{
2136 clear_ht_prop_types(buf->b_proptypes);
2137 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002138 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002139}
2140
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002141// Struct used to return two values from adjust_prop().
2142typedef struct
2143{
2144 int dirty; // if the property was changed
2145 int can_drop; // whether after this change, the prop may be removed
2146} adjustres_T;
2147
2148/*
2149 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2150 *
2151 * Note that "col" is zero-based, while tp_col is one-based.
2152 * Only for the current buffer.
2153 * "flags" can have:
2154 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002155 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002156 */
2157 static adjustres_T
2158adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002159 textprop_T *prop,
2160 colnr_T col,
2161 int added,
2162 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002163{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002164 proptype_T *pt;
2165 int start_incl;
2166 int end_incl;
2167 int droppable;
2168 adjustres_T res = {TRUE, FALSE};
2169
2170 // prop after end of the line doesn't move
2171 if (prop->tp_col == MAXCOL)
2172 {
2173 res.dirty = FALSE;
2174 return res;
2175 }
2176
2177 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2178 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002179 || (flags & APC_SUBSTITUTE)
2180 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002181 if (prop->tp_id < 0 && (flags & APC_INDENT))
2182 // when inserting indent just before a character with virtual text
2183 // shift the text property
2184 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002185 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002186 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002187 // do not drop zero-width props if they later can increase in size
2188 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002189
2190 if (added > 0)
2191 {
2192 if (col + 1 <= prop->tp_col
2193 - (start_incl || (prop->tp_len == 0 && end_incl)))
2194 // Change is entirely before the text property: Only shift
2195 prop->tp_col += added;
2196 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2197 // Insertion was inside text property
2198 prop->tp_len += added;
2199 }
2200 else if (prop->tp_col > col + 1)
2201 {
2202 if (prop->tp_col + added < col + 1)
2203 {
2204 prop->tp_len += (prop->tp_col - 1 - col) + added;
2205 prop->tp_col = col + 1;
2206 if (prop->tp_len <= 0)
2207 {
2208 prop->tp_len = 0;
2209 res.can_drop = droppable;
2210 }
2211 }
2212 else
2213 prop->tp_col += added;
2214 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002215 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2216 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002217 {
2218 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2219
2220 prop->tp_len += after > 0 ? added + after : added;
2221 res.can_drop = prop->tp_len <= 0 && droppable;
2222 }
2223 else
2224 res.dirty = FALSE;
2225
2226 return res;
2227}
2228
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002229/*
2230 * Adjust the columns of text properties in line "lnum" after position "col" to
2231 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002232 * Note that "col" is zero-based, while tp_col is one-based.
2233 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002234 * "flags" can have:
2235 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2236 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002237 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002238 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002239 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002240 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002241 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002242adjust_prop_columns(
2243 linenr_T lnum,
2244 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002245 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002246 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002247{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002248 int proplen;
2249 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002250 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002251 int ri, wi;
2252 size_t textlen;
2253
2254 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002255 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002256
2257 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2258 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002259 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002260 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002261
Bram Moolenaar196d1572019-01-02 23:47:18 +01002262 wi = 0; // write index
2263 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002264 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002265 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002266 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002267
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002268 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2269 res = adjust_prop(&prop, col, bytes_added, flags);
2270 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002271 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002272 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002273 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2274 && u_savesub(lnum) == FAIL)
2275 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002276 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002277
2278 // u_savesub() may have updated curbuf->b_ml, fetch it again
2279 if (curbuf->b_ml.ml_line_lnum != lnum)
2280 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002281 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002282 if (res.can_drop)
2283 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002284 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002285 ++wi;
2286 }
2287 if (dirty)
2288 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002289 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2290
2291 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002292 {
2293 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2294
2295 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2296 vim_free(curbuf->b_ml.ml_line_ptr);
2297 curbuf->b_ml.ml_line_ptr = p;
2298 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002299 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002300 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002301 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002302 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002303}
2304
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002305/*
2306 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002307 * "lnum_props" is the line that has the properties from before the split.
2308 * "lnum_top" is the top line.
2309 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002310 * "deleted" is the number of bytes deleted.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002311 * "at_eol" is true if the split is after the end of the line.
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002312 */
2313 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002314adjust_props_for_split(
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002315 linenr_T lnum_props,
2316 linenr_T lnum_top,
2317 int kept,
2318 int deleted,
2319 int at_eol)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002320{
2321 char_u *props;
2322 int count;
2323 garray_T prevprop;
2324 garray_T nextprop;
2325 int i;
2326 int skipped = kept + deleted;
2327
2328 if (!curbuf->b_has_textprop)
2329 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002330
2331 // Get the text properties from "lnum_props".
2332 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002333 ga_init2(&prevprop, sizeof(textprop_T), 10);
2334 ga_init2(&nextprop, sizeof(textprop_T), 10);
2335
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002336 // Keep the relevant ones in the first line, reducing the length if needed.
2337 // Copy the ones that include the split to the second line.
2338 // Move the ones after the split to the second line.
2339 for (i = 0; i < count; ++i)
2340 {
2341 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002342 proptype_T *pt;
2343 int start_incl, end_incl;
2344 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002345 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002346
2347 // copy the prop to an aligned structure
2348 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2349
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002350 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2351 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2352 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002353
2354 // a text prop "above" behaves like it is on the first text column
2355 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2356
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002357 if (prop_col == MAXCOL)
2358 {
2359 cont_prev = at_eol;
2360 cont_next = !at_eol;
2361 }
2362 else
2363 {
2364 cont_prev = prop_col + !start_incl <= kept;
2365 cont_next = skipped <= prop_col + prop.tp_len - !end_incl;
2366 }
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002367 // when a prop has text it is never copied
2368 if (prop.tp_id < 0 && cont_next)
2369 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002370
2371 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002372 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002373 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2374
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002375 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002376 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002377 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002378 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002379 if (cont_next)
2380 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002381 }
2382
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002383 // Only add the property to the next line if the length is bigger than
2384 // zero.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002385 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002386 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002387 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002388
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002389 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002390 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002391 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002392 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002393 if (p->tp_col > skipped)
2394 p->tp_col -= skipped - 1;
2395 else
2396 {
2397 p->tp_len -= skipped - p->tp_col;
2398 p->tp_col = 1;
2399 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002400 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002401 if (cont_prev)
2402 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002403 }
2404 }
2405
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002406 set_text_props(lnum_top, prevprop.ga_data,
2407 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002408 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002409 set_text_props(lnum_top + 1, nextprop.ga_data,
2410 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002411 ga_clear(&nextprop);
2412}
2413
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002414/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002415 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002416 */
2417 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002418prepend_joined_props(
2419 char_u *new_props,
2420 int propcount,
2421 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002422 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002423 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002424 long col,
2425 int removed)
2426{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002427 char_u *props;
2428 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2429 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002430
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002431 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002432 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002433 textprop_T prop;
2434 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002435
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002436 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002437 if (prop.tp_col == MAXCOL && !last_line)
2438 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002439 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2440
2441 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002442 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002443
Bram Moolenaare175dc62022-08-01 22:18:50 +01002444 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002445 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2446 &prop, sizeof(prop));
2447 else
2448 {
2449 int j;
2450 int found = FALSE;
2451
2452 // Search for continuing prop.
2453 for (j = *props_remaining; j < propcount; ++j)
2454 {
2455 textprop_T op;
2456
2457 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2458 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2459 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002460 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002461 found = TRUE;
2462 op.tp_len += op.tp_col - prop.tp_col;
2463 op.tp_col = prop.tp_col;
2464 // Start/end is taken care of when deleting joined lines
2465 op.tp_flags = prop.tp_flags;
2466 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2467 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002468 }
2469 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002470 if (!found)
2471 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002472 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002473 }
2474}
2475
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002476#endif // FEAT_PROP_POPUP