blob: f33edc2117ad8f2df201230ed3c97c77061d6b75 [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010017
18/*
19 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
20 * This avoids adding a pointer to the hashtable item.
21 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
22 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
23 * HI2PT() converts a hashitem pointer to a proptype pointer.
24 */
25#define PT2HIKEY(p) ((p)->pt_name)
26#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
27#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
28
29// The global text property types.
30static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010031static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33// The last used text property type ID.
34static int proptype_id = 0;
35
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036/*
37 * Find a property type by name, return the hashitem.
38 * Returns NULL if the item can't be found.
39 */
40 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010041find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010042{
43 hashtab_T *ht;
44 hashitem_T *hi;
45
46 if (*name == NUL)
47 return NULL;
48 if (buf == NULL)
49 ht = global_proptypes;
50 else
51 ht = buf->b_proptypes;
52
53 if (ht == NULL)
54 return NULL;
55 hi = hash_find(ht, name);
56 if (HASHITEM_EMPTY(hi))
57 return NULL;
58 return hi;
59}
60
61/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010062 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010063 */
64 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010065find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010066{
Bram Moolenaare44336b2022-08-07 18:20:08 +010067 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010068
69 if (hi == NULL)
70 return NULL;
71 return HI2PT(hi);
72}
73
74/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020075 * Get the prop type ID of "name".
76 * When not found return zero.
77 */
78 int
79find_prop_type_id(char_u *name, buf_T *buf)
80{
Bram Moolenaare44336b2022-08-07 18:20:08 +010081 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020082
83 if (pt == NULL)
84 return 0;
85 return pt->pt_id;
86}
87
88/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089 * Lookup a property type by name. First in "buf" and when not found in the
90 * global types.
91 * When not found gives an error message and returns NULL.
92 */
93 static proptype_T *
94lookup_prop_type(char_u *name, buf_T *buf)
95{
Bram Moolenaare44336b2022-08-07 18:20:08 +010096 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097
98 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +010099 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100101 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100152 if (argvars[2].v_type != VAR_DICT)
153 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000154 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100155 return;
156 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100158 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
159 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200160}
161
162/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200163 * Attach a text property 'type_name' to the text starting
164 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100165 * the buffer "buf" and assign identifier "id".
166 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200167 */
168 static int
169prop_add_one(
170 buf_T *buf,
171 char_u *type_name,
172 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100173 char_u *text_arg,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100174 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200175 linenr_T start_lnum,
176 linenr_T end_lnum,
177 colnr_T start_col,
178 colnr_T end_col)
179{
180 proptype_T *type;
181 linenr_T lnum;
182 int proplen;
183 char_u *props = NULL;
184 char_u *newprops;
185 size_t textlen;
186 char_u *newtext;
187 int i;
188 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100189 char_u *text = text_arg;
190 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200191
192 type = lookup_prop_type(type_name, buf);
193 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100194 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200195
196 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
197 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000198 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100199 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200200 }
201 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
202 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000203 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100204 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200205 }
206
207 if (buf->b_ml.ml_mfp == NULL)
208 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000209 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100210 goto theend;
211 }
212
213 if (text != NULL)
214 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100215 garray_T *gap = &buf->b_textprop_text;
216 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100217
218 // double check we got the right ID
219 if (-id - 1 != gap->ga_len)
220 iemsg("text prop ID mismatch");
221 if (gap->ga_growsize == 0)
222 ga_init2(gap, sizeof(char *), 50);
223 if (ga_grow(gap, 1) == FAIL)
224 goto theend;
225 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100226
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100227 // change any control character (Tab, Newline, etc.) to a Space to make
228 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100230 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100231 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100232 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200233 }
234
235 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
236 {
237 colnr_T col; // start column
238 long length; // in bytes
239
240 // Fetch the line to get the ml_line_len field updated.
241 proplen = get_text_props(buf, lnum, &props, TRUE);
242 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
243
244 if (lnum == start_lnum)
245 col = start_col;
246 else
247 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100248 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200249 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000250 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100251 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200252 }
253
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)
267 col = MAXCOL; // after the line
268 }
269
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200270 // Allocate the new line with space for the new property.
271 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
272 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100273 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200274 // Copy the text, including terminating NUL.
275 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
276
277 // Find the index where to insert the new property.
278 // Since the text properties are not aligned properly when stored with
279 // the text, we need to copy them as bytes before using it as a struct.
280 for (i = 0; i < proplen; ++i)
281 {
282 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
283 sizeof(textprop_T));
284 if (tmp_prop.tp_col >= col)
285 break;
286 }
287 newprops = newtext + textlen;
288 if (i > 0)
289 mch_memmove(newprops, props, sizeof(textprop_T) * i);
290
291 tmp_prop.tp_col = col;
292 tmp_prop.tp_len = length;
293 tmp_prop.tp_id = id;
294 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100295 tmp_prop.tp_flags = text_flags
296 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
297 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200298 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
299 sizeof(textprop_T));
300
301 if (i < proplen)
302 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
303 props + i * sizeof(textprop_T),
304 sizeof(textprop_T) * (proplen - i));
305
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100306 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200307 vim_free(buf->b_ml.ml_line_ptr);
308 buf->b_ml.ml_line_ptr = newtext;
309 buf->b_ml.ml_line_len += sizeof(textprop_T);
310 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
311 }
312
313 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100314 res = OK;
315
316theend:
317 vim_free(text);
318 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200319}
320
321/*
322 * prop_add_list()
323 * First argument specifies the text property:
324 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
325 * Second argument is a List where each item is a List with the following
326 * entries: [lnum, start_col, end_col]
327 */
328 void
329f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
330{
331 dict_T *dict;
332 char_u *type_name;
333 buf_T *buf = curbuf;
334 int id = 0;
335 listitem_T *li;
336 list_T *pos_list;
337 linenr_T start_lnum;
338 colnr_T start_col;
339 linenr_T end_lnum;
340 colnr_T end_col;
341 int error = FALSE;
342
343 if (check_for_dict_arg(argvars, 0) == FAIL
344 || check_for_list_arg(argvars, 1) == FAIL)
345 return;
346
347 if (argvars[1].vval.v_list == NULL)
348 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000349 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200350 return;
351 }
352
353 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100354 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200355 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000356 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200357 return;
358 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100359 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200360
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100361 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100362 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363
364 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
365 return;
366
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100367 // This must be done _before_ we start adding properties because property
368 // changes trigger buffer (memline) reorganisation, which needs this flag
369 // to be correctly set.
370 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200371 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
372 {
373 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
374 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000375 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200376 return;
377 }
378
379 pos_list = li->li_tv.vval.v_list;
380 start_lnum = list_find_nr(pos_list, 0L, &error);
381 start_col = list_find_nr(pos_list, 1L, &error);
382 end_lnum = list_find_nr(pos_list, 2L, &error);
383 end_col = list_find_nr(pos_list, 3L, &error);
384 if (error || start_lnum <= 0 || start_col <= 0
385 || end_lnum <= 0 || end_col <= 0)
386 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000387 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200388 return;
389 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100390 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200391 start_col, end_col) == FAIL)
392 return;
393 }
394
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200395 redraw_buf_later(buf, VALID);
396}
397
398/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100399 * Get the next ID to use for a textprop with text in buffer "buf".
400 */
401 static int
402get_textprop_id(buf_T *buf)
403{
404 // TODO: recycle deleted entries
405 return -(buf->b_textprop_text.ga_len + 1);
406}
407
408/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200409 * Shared between prop_add() and popup_create().
410 * "dict_arg" is the function argument of a dict containing "bufnr".
411 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100412 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200413 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100414 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200415prop_add_common(
416 linenr_T start_lnum,
417 colnr_T start_col,
418 dict_T *dict,
419 buf_T *default_buf,
420 typval_T *dict_arg)
421{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200422 linenr_T end_lnum;
423 colnr_T end_col;
424 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200425 buf_T *buf = default_buf;
426 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100427 char_u *text = NULL;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100428 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100429
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100430 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100431 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000432 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100433 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100434 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100435 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100436
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100437 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100438 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100439 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100440 if (end_lnum < start_lnum)
441 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000442 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100443 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100444 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100445 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100446 else
447 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100448
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100449 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100450 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100451 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100452
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100453 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100454 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000455 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100456 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100457 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100458 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100459 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100460 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100461 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100462 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100463 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100464 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000465 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100466 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100467 }
468 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100469 else if (start_lnum == end_lnum)
470 end_col = start_col;
471 else
472 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100474 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100475 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100476
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100477 if (dict_has_key(dict, "text"))
478 {
479 text = dict_get_string(dict, "text", TRUE);
480 if (text == NULL)
481 goto theend;
482 // use a default length of 1 to make multiple props show up
483 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100484
485 if (dict_has_key(dict, "text_align"))
486 {
487 char_u *p = dict_get_string(dict, "text_align", FALSE);
488
489 if (p == NULL)
490 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100491 if (start_col != 0)
492 {
493 emsg(_(e_can_only_use_text_align_when_column_is_zero));
494 goto theend;
495 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100496 if (STRCMP(p, "right") == 0)
497 flags |= TP_FLAG_ALIGN_RIGHT;
498 else if (STRCMP(p, "below") == 0)
499 flags |= TP_FLAG_ALIGN_BELOW;
500 else if (STRCMP(p, "after") != 0)
501 {
502 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
503 goto theend;
504 }
505 }
506
507 if (dict_has_key(dict, "text_wrap"))
508 {
509 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
510 if (p == NULL)
511 goto theend;
512 if (STRCMP(p, "wrap") == 0)
513 flags |= TP_FLAG_WRAP;
514 else if (STRCMP(p, "truncate") != 0)
515 {
516 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
517 goto theend;
518 }
519 }
520 }
521
522 // Column must be 1 or more for a normal text property; when "text" is
523 // present zero means it goes after the line.
524 if (start_col < (text == NULL ? 1 : 0))
525 {
526 semsg(_(e_invalid_column_number_nr), (long)start_col);
527 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100528 }
529
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200530 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100531 goto theend;
532
533 if (id < 0 && buf->b_textprop_text.ga_len > 0)
534 {
535 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
536 goto theend;
537 }
538 if (text != NULL)
539 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100540
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100541 // This must be done _before_ we add the property because property changes
542 // trigger buffer (memline) reorganisation, which needs this flag to be
543 // correctly set.
544 buf->b_has_textprop = TRUE; // this is never reset
545
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100546 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100547 start_lnum, end_lnum, start_col, end_col);
548 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100549
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200550 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100551
552theend:
553 vim_free(text);
554 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100555}
556
557/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100558 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100559 * Returns the number of text properties and, when non-zero, a pointer to the
560 * first one in "props" (note that it is not aligned, therefore the char_u
561 * pointer).
562 */
563 int
564get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
565{
566 char_u *text;
567 size_t textlen;
568 size_t proplen;
569
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100570 // Be quick when no text property types have been defined or the buffer,
571 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200572 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100573 return 0;
574
575 // Fetch the line to get the ml_line_len field updated.
576 text = ml_get_buf(buf, lnum, will_change);
577 textlen = STRLEN(text) + 1;
578 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100579 if (proplen == 0)
580 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100581 if (proplen % sizeof(textprop_T) != 0)
582 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000583 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100584 return 0;
585 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100586 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100587 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100588}
589
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100590/*
591 * Return the number of text properties with "below" alignment in line "lnum".
592 */
593 int
594prop_count_below(buf_T *buf, linenr_T lnum)
595{
596 char_u *props;
597 int count = get_text_props(buf, lnum, &props, FALSE);
598 int result = 0;
599 textprop_T prop;
600 int i;
601
602 if (count == 0)
603 return 0;
604 for (i = 0; i < count; ++i)
605 {
606 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
607 if (prop.tp_col == MAXCOL && (prop.tp_flags & TP_FLAG_ALIGN_BELOW))
608 ++result;
609 }
610 return result;
611}
612
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200613/**
614 * Return the number of text properties on line "lnum" in the current buffer.
615 * When "only_starting" is true only text properties starting in this line will
616 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100617 * When "last_line" is FALSE then text properties after the line are not
618 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200619 */
620 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100621count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200622{
623 char_u *props;
624 int proplen = get_text_props(curbuf, lnum, &props, 0);
625 int result = proplen;
626 int i;
627 textprop_T prop;
628
Bram Moolenaare175dc62022-08-01 22:18:50 +0100629 for (i = 0; i < proplen; ++i)
630 {
631 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100632 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100633 // previous line, or when not in the last line and it is virtual text
634 // after the line.
635 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
636 || (!last_line && prop.tp_col == MAXCOL))
637 --result;
638 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200639 return result;
640}
641
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100642/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200643 * Find text property "type_id" in the visible lines of window "wp".
644 * Match "id" when it is > 0.
645 * Returns FAIL when not found.
646 */
647 int
648find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
649 linenr_T *found_lnum)
650{
651 linenr_T lnum;
652 char_u *props;
653 int count;
654 int i;
655
656 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100657 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200658 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
659 {
660 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
661 for (i = 0; i < count; ++i)
662 {
663 mch_memmove(prop, props + i * sizeof(textprop_T),
664 sizeof(textprop_T));
665 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
666 {
667 *found_lnum = lnum;
668 return OK;
669 }
670 }
671 }
672 return FAIL;
673}
674
675/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100676 * Set the text properties for line "lnum" to "props" with length "len".
677 * If "len" is zero text properties are removed, "props" is not used.
678 * Any existing text properties are dropped.
679 * Only works for the current buffer.
680 */
681 static void
682set_text_props(linenr_T lnum, char_u *props, int len)
683{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100684 char_u *text;
685 char_u *newtext;
686 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100687
688 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100689 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100690 newtext = alloc(textlen + len);
691 if (newtext == NULL)
692 return;
693 mch_memmove(newtext, text, textlen);
694 if (len > 0)
695 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100696 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100697 vim_free(curbuf->b_ml.ml_line_ptr);
698 curbuf->b_ml.ml_line_ptr = newtext;
699 curbuf->b_ml.ml_line_len = textlen + len;
700 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
701}
702
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100703/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100704 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100705 */
706 void
707add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
708{
709 char_u *text;
710 char_u *newtext;
711 int proplen = text_prop_count * (int)sizeof(textprop_T);
712
713 text = ml_get(lnum);
714 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
715 if (newtext == NULL)
716 return;
717 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
718 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
719 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
720 vim_free(curbuf->b_ml.ml_line_ptr);
721 curbuf->b_ml.ml_line_ptr = newtext;
722 curbuf->b_ml.ml_line_len += proplen;
723 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
724}
725
Bram Moolenaare44336b2022-08-07 18:20:08 +0100726/*
727 * Function passed to qsort() for sorting proptype_T on pt_id.
728 */
729 static int
730compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100731{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100732 proptype_T *tp1 = *(proptype_T **)s1;
733 proptype_T *tp2 = *(proptype_T **)s2;
734
735 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
736}
737
738 static proptype_T *
739find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
740{
741 int low = 0;
742 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100743
Bram Moolenaar10246902022-08-08 17:08:05 +0100744 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100745 return NULL;
746
Bram Moolenaare44336b2022-08-07 18:20:08 +0100747 // Make the loopup faster by creating an array with pointers to
748 // hashtable entries, sorted on pt_id.
749 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100750 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100751 long todo;
752 hashitem_T *hi;
753 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100754
Bram Moolenaare44336b2022-08-07 18:20:08 +0100755 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
756 if (*array == NULL)
757 return NULL;
758 todo = (long)ht->ht_used;
759 for (hi = ht->ht_array; todo > 0; ++hi)
760 {
761 if (!HASHITEM_EMPTY(hi))
762 {
763 (*array)[i++] = HI2PT(hi);
764 --todo;
765 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100766 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100767 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
768 }
769
770 // binary search in the sorted array
771 high = ht->ht_used;
772 while (high > low)
773 {
774 int m = (high + low) / 2;
775
776 if ((*array)[m]->pt_id == id)
777 return (*array)[m];
778 if ((*array)[m]->pt_id > id)
779 high = m;
780 else
781 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100782 }
783 return NULL;
784}
785
786/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100787 * Fill 'dict' with text properties in 'prop'.
788 */
789 static void
790prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
791{
792 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200793 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100794
795 dict_add_number(dict, "col", prop->tp_col);
796 dict_add_number(dict, "length", prop->tp_len);
797 dict_add_number(dict, "id", prop->tp_id);
798 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
799 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200800
Bram Moolenaare44336b2022-08-07 18:20:08 +0100801 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200802 if (pt == NULL)
803 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100804 pt = find_type_by_id(global_proptypes, &global_proparray,
805 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200806 buflocal = FALSE;
807 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100808 if (pt != NULL)
809 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200810
811 if (buflocal)
812 dict_add_number(dict, "type_bufnr", buf->b_fnum);
813 else
814 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100815}
816
817/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100818 * Find a property type by ID in "buf" or globally.
819 * Returns NULL if not found.
820 */
821 proptype_T *
822text_prop_type_by_id(buf_T *buf, int id)
823{
824 proptype_T *type;
825
Bram Moolenaare44336b2022-08-07 18:20:08 +0100826 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100827 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100828 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100829 return type;
830}
831
832/*
833 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
834 */
835 void
836f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
837{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200838 linenr_T start;
839 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100840 linenr_T lnum;
841 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100842 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100843
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200844 if (in_vim9script()
845 && (check_for_number_arg(argvars, 0) == FAIL
846 || check_for_opt_number_arg(argvars, 1) == FAIL
847 || (argvars[1].v_type != VAR_UNKNOWN
848 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
849 return;
850
851 start = tv_get_number(&argvars[0]);
852 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100853 if (argvars[1].v_type != VAR_UNKNOWN)
854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100855 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100856 if (argvars[2].v_type != VAR_UNKNOWN)
857 {
858 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
859 return;
860 }
861 }
862 if (start < 1 || end < 1)
863 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200864 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100865 return;
866 }
867
868 for (lnum = start; lnum <= end; ++lnum)
869 {
870 char_u *text;
871 size_t len;
872
873 if (lnum > buf->b_ml.ml_line_count)
874 break;
875 text = ml_get_buf(buf, lnum, FALSE);
876 len = STRLEN(text) + 1;
877 if ((size_t)buf->b_ml.ml_line_len > len)
878 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100879 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100880 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
881 {
882 char_u *newtext = vim_strsave(text);
883
884 // need to allocate the line now
885 if (newtext == NULL)
886 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100887 if (buf->b_ml.ml_flags & ML_ALLOCATED)
888 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100889 buf->b_ml.ml_line_ptr = newtext;
890 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
891 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100892 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100893 }
894 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100895 if (did_clear)
896 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100897}
898
899/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100900 * prop_find({props} [, {direction}])
901 */
902 void
903f_prop_find(typval_T *argvars, typval_T *rettv)
904{
905 pos_T *cursor = &curwin->w_cursor;
906 dict_T *dict;
907 buf_T *buf = curbuf;
908 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200909 int lnum_start;
910 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100911 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200912 int id = 0;
913 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200914 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100915 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200916 int lnum = -1;
917 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100918 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100919 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100920
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200921 if (in_vim9script()
922 && (check_for_dict_arg(argvars, 0) == FAIL
923 || check_for_opt_string_arg(argvars, 1) == FAIL))
924 return;
925
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100926 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
927 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000928 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100929 return;
930 }
931 dict = argvars[0].vval.v_dict;
932
933 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
934 return;
935 if (buf->b_ml.ml_mfp == NULL)
936 return;
937
938 if (argvars[1].v_type != VAR_UNKNOWN)
939 {
940 char_u *dir_s = tv_get_string(&argvars[1]);
941
942 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100943 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100944 else if (*dir_s != 'f')
945 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000946 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100947 return;
948 }
949 }
950
951 di = dict_find(dict, (char_u *)"lnum", -1);
952 if (di != NULL)
953 lnum = tv_get_number(&di->di_tv);
954
955 di = dict_find(dict, (char_u *)"col", -1);
956 if (di != NULL)
957 col = tv_get_number(&di->di_tv);
958
959 if (lnum == -1)
960 {
961 lnum = cursor->lnum;
962 col = cursor->col + 1;
963 }
964 else if (col == -1)
965 col = 1;
966
967 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
968 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200969 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100970 return;
971 }
972
Bram Moolenaard61efa52022-07-23 09:52:04 +0100973 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100974
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100975 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200976 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100977 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200978 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200979 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100980 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100981 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100982 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100983 proptype_T *type = lookup_prop_type(name, buf);
984
985 if (type == NULL)
986 return;
987 type_id = type->pt_id;
988 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100989 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200990 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100991 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000992 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100993 return;
994 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200995 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100996 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000997 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100998 return;
999 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001000
1001 lnum_start = lnum;
1002
1003 if (rettv_dict_alloc(rettv) == FAIL)
1004 return;
1005
1006 while (1)
1007 {
1008 char_u *text = ml_get_buf(buf, lnum, FALSE);
1009 size_t textlen = STRLEN(text) + 1;
1010 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001011 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001012 int i;
1013 textprop_T prop;
1014 int prop_start;
1015 int prop_end;
1016
LemonBoy9bd3ce22022-04-18 21:54:02 +01001017 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001018 {
1019 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001020 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001021
LemonBoy9bd3ce22022-04-18 21:54:02 +01001022 // For the very first line try to find the first property before or
1023 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001024 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001025 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001026 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001027 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001028 if (prop.tp_col > col)
1029 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001030 }
1031 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1032 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001033 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001034 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001035 : (id_found && prop.tp_id == id)
1036 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001037 {
1038 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001039 if (lnum_start == lnum
1040 && col >= prop.tp_col
1041 && (col <= prop.tp_col + prop.tp_len
1042 - (prop.tp_len != 0)))
1043 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001044
LemonBoy9bd3ce22022-04-18 21:54:02 +01001045 // The property was not continued from last line, it starts on
1046 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001047 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001048 // The property does not continue on the next line, it ends on
1049 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001050 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001051 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001052 seen_end = 1;
1053
1054 // Skip lines without the start flag.
1055 if (!prop_start)
1056 {
1057 // Always search backwards for start when search started
1058 // on a prop and we're not skipping.
1059 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001060 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001061 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001062 }
1063
1064 // If skipstart is true, skip the prop at start pos (even if
1065 // continued from another line).
1066 if (start_pos_has_prop && skipstart && !seen_end)
1067 {
1068 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001069 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001070 }
1071
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001072 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1073 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1074
1075 return;
1076 }
1077 }
1078
1079 if (dir > 0)
1080 {
1081 if (lnum >= buf->b_ml.ml_line_count)
1082 break;
1083 lnum++;
1084 }
1085 else
1086 {
1087 if (lnum <= 1)
1088 break;
1089 lnum--;
1090 }
1091 }
1092}
1093
1094/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001095 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1096 */
1097 static int
1098prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1099{
1100 int i;
1101
1102 for (i = 0; i < len; i++)
1103 if (types_or_ids[i] == type_or_id)
1104 return TRUE;
1105
1106 return FALSE;
1107}
1108
1109/*
1110 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1111 * If 'prop_types' is not NULL, then return only the text properties with
1112 * matching property type in the 'prop_types' array.
1113 * If 'prop_ids' is not NULL, then return only the text properties with
1114 * an identifier in the 'props_ids' array.
1115 * If 'add_lnum' is TRUE, then add the line number also to the text property
1116 * dictionary.
1117 */
1118 static void
1119get_props_in_line(
1120 buf_T *buf,
1121 linenr_T lnum,
1122 int *prop_types,
1123 int prop_types_len,
1124 int *prop_ids,
1125 int prop_ids_len,
1126 list_T *retlist,
1127 int add_lnum)
1128{
1129 char_u *text = ml_get_buf(buf, lnum, FALSE);
1130 size_t textlen = STRLEN(text) + 1;
1131 int count;
1132 int i;
1133 textprop_T prop;
1134
1135 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1136 for (i = 0; i < count; ++i)
1137 {
1138 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1139 sizeof(textprop_T));
1140 if ((prop_types == NULL
1141 || prop_type_or_id_in_list(prop_types, prop_types_len,
1142 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001143 && (prop_ids == NULL
1144 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1145 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001146 {
1147 dict_T *d = dict_alloc();
1148
1149 if (d == NULL)
1150 break;
1151 prop_fill_dict(d, &prop, buf);
1152 if (add_lnum)
1153 dict_add_number(d, "lnum", lnum);
1154 list_append_dict(retlist, d);
1155 }
1156 }
1157}
1158
1159/*
1160 * Convert a List of property type names into an array of property type
1161 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1162 * error. 'num_types' is set to the number of returned property types.
1163 */
1164 static int *
1165get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1166{
1167 int *prop_types;
1168 listitem_T *li;
1169 int i;
1170 char_u *name;
1171 proptype_T *type;
1172
1173 *num_types = 0;
1174
1175 prop_types = ALLOC_MULT(int, list_len(l));
1176 if (prop_types == NULL)
1177 return NULL;
1178
1179 i = 0;
1180 FOR_ALL_LIST_ITEMS(l, li)
1181 {
1182 if (li->li_tv.v_type != VAR_STRING)
1183 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001184 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001185 goto errret;
1186 }
1187 name = li->li_tv.vval.v_string;
1188 if (name == NULL)
1189 goto errret;
1190
1191 type = lookup_prop_type(name, buf);
1192 if (type == NULL)
1193 goto errret;
1194 prop_types[i++] = type->pt_id;
1195 }
1196
1197 *num_types = i;
1198 return prop_types;
1199
1200errret:
1201 VIM_CLEAR(prop_types);
1202 return NULL;
1203}
1204
1205/*
1206 * Convert a List of property identifiers into an array of property
1207 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1208 * error. 'num_ids' is set to the number of returned property identifiers.
1209 */
1210 static int *
1211get_prop_ids_from_list(list_T *l, int *num_ids)
1212{
1213 int *prop_ids;
1214 listitem_T *li;
1215 int i;
1216 int id;
1217 int error;
1218
1219 *num_ids = 0;
1220
1221 prop_ids = ALLOC_MULT(int, list_len(l));
1222 if (prop_ids == NULL)
1223 return NULL;
1224
1225 i = 0;
1226 FOR_ALL_LIST_ITEMS(l, li)
1227 {
1228 error = FALSE;
1229 id = tv_get_number_chk(&li->li_tv, &error);
1230 if (error)
1231 goto errret;
1232
1233 prop_ids[i++] = id;
1234 }
1235
1236 *num_ids = i;
1237 return prop_ids;
1238
1239errret:
1240 VIM_CLEAR(prop_ids);
1241 return NULL;
1242}
1243
1244/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001245 * prop_list({lnum} [, {bufnr}])
1246 */
1247 void
1248f_prop_list(typval_T *argvars, typval_T *rettv)
1249{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001250 linenr_T lnum;
1251 linenr_T start_lnum;
1252 linenr_T end_lnum;
1253 buf_T *buf = curbuf;
1254 int add_lnum = FALSE;
1255 int *prop_types = NULL;
1256 int prop_types_len = 0;
1257 int *prop_ids = NULL;
1258 int prop_ids_len = 0;
1259 list_T *l;
1260 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001261
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001262 if (in_vim9script()
1263 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001264 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001265 return;
1266
Bram Moolenaar93a10962022-06-16 11:42:09 +01001267 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001268 return;
1269
1270 // default: get text properties on current line
1271 start_lnum = tv_get_number(&argvars[0]);
1272 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001273 if (argvars[1].v_type != VAR_UNKNOWN)
1274 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001275 dict_T *d;
1276
1277 if (argvars[1].v_type != VAR_DICT)
1278 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001279 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001280 return;
1281 }
1282 d = argvars[1].vval.v_dict;
1283
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001284 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1285 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001286
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001287 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001288 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001289 if (di->di_tv.v_type != VAR_NUMBER)
1290 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001291 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001292 return;
1293 }
1294 end_lnum = tv_get_number(&di->di_tv);
1295 if (end_lnum < 0)
1296 // negative end_lnum is used as an offset from the last buffer
1297 // line
1298 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1299 else if (end_lnum > buf->b_ml.ml_line_count)
1300 end_lnum = buf->b_ml.ml_line_count;
1301 add_lnum = TRUE;
1302 }
1303 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1304 {
1305 if (di->di_tv.v_type != VAR_LIST)
1306 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001307 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001308 return;
1309 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001310
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001311 l = di->di_tv.vval.v_list;
1312 if (l != NULL && list_len(l) > 0)
1313 {
1314 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1315 if (prop_types == NULL)
1316 return;
1317 }
1318 }
1319 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1320 {
1321 if (di->di_tv.v_type != VAR_LIST)
1322 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001323 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001324 goto errret;
1325 }
1326
1327 l = di->di_tv.vval.v_list;
1328 if (l != NULL && list_len(l) > 0)
1329 {
1330 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1331 if (prop_ids == NULL)
1332 goto errret;
1333 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001334 }
1335 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001336 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1337 || end_lnum < 1 || end_lnum < start_lnum)
1338 emsg(_(e_invalid_range));
1339 else
1340 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1341 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1342 prop_ids, prop_ids_len,
1343 rettv->vval.v_list, add_lnum);
1344
1345errret:
1346 VIM_CLEAR(prop_types);
1347 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001348}
1349
1350/*
1351 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1352 */
1353 void
1354f_prop_remove(typval_T *argvars, typval_T *rettv)
1355{
1356 linenr_T start = 1;
1357 linenr_T end = 0;
1358 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001359 linenr_T first_changed = 0;
1360 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001361 dict_T *dict;
1362 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001363 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001364 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001365 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001366 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001367 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001368
1369 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001370
1371 if (in_vim9script()
1372 && (check_for_dict_arg(argvars, 0) == FAIL
1373 || check_for_opt_number_arg(argvars, 1) == FAIL
1374 || (argvars[1].v_type != VAR_UNKNOWN
1375 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1376 return;
1377
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001378 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1379 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001380 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001381 return;
1382 }
1383
1384 if (argvars[1].v_type != VAR_UNKNOWN)
1385 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001386 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001387 end = start;
1388 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001389 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001390 if (start < 1 || end < 1)
1391 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001392 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001393 return;
1394 }
1395 }
1396
1397 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001398 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1399 return;
1400 if (buf->b_ml.ml_mfp == NULL)
1401 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001402
Bram Moolenaard61efa52022-07-23 09:52:04 +01001403 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001404
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001405 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001406 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001407 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001408 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001409 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001410 proptype_T *type = lookup_prop_type(name, buf);
1411
1412 if (type == NULL)
1413 return;
1414 type_id = type->pt_id;
1415 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001416 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001417
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001418 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001419 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001420 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001421 return;
1422 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001423 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001424 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001425 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001426 return;
1427 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001428
1429 if (end == 0)
1430 end = buf->b_ml.ml_line_count;
1431 for (lnum = start; lnum <= end; ++lnum)
1432 {
1433 char_u *text;
1434 size_t len;
1435
1436 if (lnum > buf->b_ml.ml_line_count)
1437 break;
1438 text = ml_get_buf(buf, lnum, FALSE);
1439 len = STRLEN(text) + 1;
1440 if ((size_t)buf->b_ml.ml_line_len > len)
1441 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001442 static textprop_T textprop; // static because of alignment
1443 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001444
1445 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1446 / sizeof(textprop_T); ++idx)
1447 {
1448 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1449 + idx * sizeof(textprop_T);
1450 size_t taillen;
1451
1452 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001453 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1454 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001455 {
1456 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1457 {
1458 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1459
1460 // need to allocate the line to be able to change it
1461 if (newptr == NULL)
1462 return;
1463 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1464 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001465 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1466 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001467 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001468 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1469
1470 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001471 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001472 }
1473
1474 taillen = buf->b_ml.ml_line_len - len
1475 - (idx + 1) * sizeof(textprop_T);
1476 if (taillen > 0)
1477 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1478 taillen);
1479 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1480 --idx;
1481
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001482 if (textprop.tp_id < 0)
1483 {
1484 garray_T *gap = &buf->b_textprop_text;
1485 int ii = -textprop.tp_id - 1;
1486
1487 // negative ID: property with text - free the text
1488 if (ii < gap->ga_len)
1489 {
1490 char_u **p = ((char_u **)gap->ga_data) + ii;
1491 vim_free(*p);
1492 *p = NULL;
1493 did_remove_text = TRUE;
1494 }
1495 }
1496
Bram Moolenaar965c0442021-05-17 00:22:06 +02001497 if (first_changed == 0)
1498 first_changed = lnum;
1499 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001500 ++rettv->vval.v_number;
1501 if (!do_all)
1502 break;
1503 }
1504 }
1505 }
1506 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001507
Bram Moolenaar965c0442021-05-17 00:22:06 +02001508 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001509 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001510 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1511 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001512 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001513
1514 if (did_remove_text)
1515 {
1516 garray_T *gap = &buf->b_textprop_text;
1517
1518 // Reduce the growarray size for NULL pointers at the end.
1519 while (gap->ga_len > 0
1520 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1521 --gap->ga_len;
1522 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001523}
1524
1525/*
1526 * Common for f_prop_type_add() and f_prop_type_change().
1527 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001528 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001529prop_type_set(typval_T *argvars, int add)
1530{
1531 char_u *name;
1532 buf_T *buf = NULL;
1533 dict_T *dict;
1534 dictitem_T *di;
1535 proptype_T *prop;
1536
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001537 if (in_vim9script()
1538 && (check_for_string_arg(argvars, 0) == FAIL
1539 || check_for_dict_arg(argvars, 1) == FAIL))
1540 return;
1541
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001542 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001543 if (*name == NUL)
1544 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001545 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001546 return;
1547 }
1548
1549 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1550 return;
1551 dict = argvars[1].vval.v_dict;
1552
Bram Moolenaare44336b2022-08-07 18:20:08 +01001553 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001554 if (add)
1555 {
1556 hashtab_T **htp;
1557
1558 if (prop != NULL)
1559 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001560 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001561 return;
1562 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001563 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001564 if (prop == NULL)
1565 return;
1566 STRCPY(prop->pt_name, name);
1567 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001568 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001569 if (buf == NULL)
1570 {
1571 htp = &global_proptypes;
1572 VIM_CLEAR(global_proparray);
1573 }
1574 else
1575 {
1576 htp = &buf->b_proptypes;
1577 VIM_CLEAR(buf->b_proparray);
1578 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001579 if (*htp == NULL)
1580 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001581 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001582 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001583 {
1584 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001585 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001586 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001587 hash_init(*htp);
1588 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001589 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001590 }
1591 else
1592 {
1593 if (prop == NULL)
1594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001595 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001596 return;
1597 }
1598 }
1599
1600 if (dict != NULL)
1601 {
1602 di = dict_find(dict, (char_u *)"highlight", -1);
1603 if (di != NULL)
1604 {
1605 char_u *highlight;
1606 int hl_id = 0;
1607
Bram Moolenaard61efa52022-07-23 09:52:04 +01001608 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001609 if (highlight != NULL && *highlight != NUL)
1610 hl_id = syn_name2id(highlight);
1611 if (hl_id <= 0)
1612 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001613 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001614 highlight == NULL ? (char_u *)"" : highlight);
1615 return;
1616 }
1617 prop->pt_hl_id = hl_id;
1618 }
1619
Bram Moolenaar58187f12019-05-05 16:33:47 +02001620 di = dict_find(dict, (char_u *)"combine", -1);
1621 if (di != NULL)
1622 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001623 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001624 prop->pt_flags |= PT_FLAG_COMBINE;
1625 else
1626 prop->pt_flags &= ~PT_FLAG_COMBINE;
1627 }
1628
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001629 di = dict_find(dict, (char_u *)"override", -1);
1630 if (di != NULL)
1631 {
1632 if (tv_get_bool(&di->di_tv))
1633 prop->pt_flags |= PT_FLAG_OVERRIDE;
1634 else
1635 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1636 }
1637
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001638 di = dict_find(dict, (char_u *)"priority", -1);
1639 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001640 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001641
1642 di = dict_find(dict, (char_u *)"start_incl", -1);
1643 if (di != NULL)
1644 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001645 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001646 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1647 else
1648 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1649 }
1650
1651 di = dict_find(dict, (char_u *)"end_incl", -1);
1652 if (di != NULL)
1653 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001654 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001655 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1656 else
1657 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1658 }
1659 }
1660}
1661
1662/*
1663 * prop_type_add({name}, {props})
1664 */
1665 void
1666f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1667{
1668 prop_type_set(argvars, TRUE);
1669}
1670
1671/*
1672 * prop_type_change({name}, {props})
1673 */
1674 void
1675f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1676{
1677 prop_type_set(argvars, FALSE);
1678}
1679
1680/*
1681 * prop_type_delete({name} [, {bufnr}])
1682 */
1683 void
1684f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1685{
1686 char_u *name;
1687 buf_T *buf = NULL;
1688 hashitem_T *hi;
1689
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001690 if (in_vim9script()
1691 && (check_for_string_arg(argvars, 0) == FAIL
1692 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1693 return;
1694
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001695 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001696 if (*name == NUL)
1697 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001698 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001699 return;
1700 }
1701
1702 if (argvars[1].v_type != VAR_UNKNOWN)
1703 {
1704 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1705 return;
1706 }
1707
Bram Moolenaare44336b2022-08-07 18:20:08 +01001708 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001709 if (hi != NULL)
1710 {
1711 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001712 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001713
1714 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001715 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001716 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001717 VIM_CLEAR(global_proparray);
1718 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001719 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001720 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001721 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001722 VIM_CLEAR(buf->b_proparray);
1723 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001724 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001725 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001726 }
1727}
1728
1729/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001730 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001731 */
1732 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001733f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001734{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001735 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001736
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001737 if (in_vim9script()
1738 && (check_for_string_arg(argvars, 0) == FAIL
1739 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1740 return;
1741
1742 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001743 if (*name == NUL)
1744 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001745 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001746 return;
1747 }
1748 if (rettv_dict_alloc(rettv) == OK)
1749 {
1750 proptype_T *prop = NULL;
1751 buf_T *buf = NULL;
1752
1753 if (argvars[1].v_type != VAR_UNKNOWN)
1754 {
1755 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1756 return;
1757 }
1758
Bram Moolenaare44336b2022-08-07 18:20:08 +01001759 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001760 if (prop != NULL)
1761 {
1762 dict_T *d = rettv->vval.v_dict;
1763
1764 if (prop->pt_hl_id > 0)
1765 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1766 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001767 dict_add_number(d, "combine",
1768 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001769 dict_add_number(d, "start_incl",
1770 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1771 dict_add_number(d, "end_incl",
1772 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1773 if (buf != NULL)
1774 dict_add_number(d, "bufnr", buf->b_fnum);
1775 }
1776 }
1777}
1778
1779 static void
1780list_types(hashtab_T *ht, list_T *l)
1781{
1782 long todo;
1783 hashitem_T *hi;
1784
1785 todo = (long)ht->ht_used;
1786 for (hi = ht->ht_array; todo > 0; ++hi)
1787 {
1788 if (!HASHITEM_EMPTY(hi))
1789 {
1790 proptype_T *prop = HI2PT(hi);
1791
1792 list_append_string(l, prop->pt_name, -1);
1793 --todo;
1794 }
1795 }
1796}
1797
1798/*
1799 * prop_type_list([{bufnr}])
1800 */
1801 void
1802f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1803{
1804 buf_T *buf = NULL;
1805
1806 if (rettv_list_alloc(rettv) == OK)
1807 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001808 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1809 return;
1810
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001811 if (argvars[0].v_type != VAR_UNKNOWN)
1812 {
1813 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1814 return;
1815 }
1816 if (buf == NULL)
1817 {
1818 if (global_proptypes != NULL)
1819 list_types(global_proptypes, rettv->vval.v_list);
1820 }
1821 else if (buf->b_proptypes != NULL)
1822 list_types(buf->b_proptypes, rettv->vval.v_list);
1823 }
1824}
1825
1826/*
1827 * Free all property types in "ht".
1828 */
1829 static void
1830clear_ht_prop_types(hashtab_T *ht)
1831{
1832 long todo;
1833 hashitem_T *hi;
1834
1835 if (ht == NULL)
1836 return;
1837
1838 todo = (long)ht->ht_used;
1839 for (hi = ht->ht_array; todo > 0; ++hi)
1840 {
1841 if (!HASHITEM_EMPTY(hi))
1842 {
1843 proptype_T *prop = HI2PT(hi);
1844
1845 vim_free(prop);
1846 --todo;
1847 }
1848 }
1849
1850 hash_clear(ht);
1851 vim_free(ht);
1852}
1853
1854#if defined(EXITFREE) || defined(PROTO)
1855/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001856 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001857 */
1858 void
1859clear_global_prop_types(void)
1860{
1861 clear_ht_prop_types(global_proptypes);
1862 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001863 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001864}
1865#endif
1866
1867/*
1868 * Free all property types for "buf".
1869 */
1870 void
1871clear_buf_prop_types(buf_T *buf)
1872{
1873 clear_ht_prop_types(buf->b_proptypes);
1874 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001875 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001876}
1877
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001878// Struct used to return two values from adjust_prop().
1879typedef struct
1880{
1881 int dirty; // if the property was changed
1882 int can_drop; // whether after this change, the prop may be removed
1883} adjustres_T;
1884
1885/*
1886 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1887 *
1888 * Note that "col" is zero-based, while tp_col is one-based.
1889 * Only for the current buffer.
1890 * "flags" can have:
1891 * APC_SUBSTITUTE: Text is replaced, not inserted.
1892 */
1893 static adjustres_T
1894adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001895 textprop_T *prop,
1896 colnr_T col,
1897 int added,
1898 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001899{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001900 proptype_T *pt;
1901 int start_incl;
1902 int end_incl;
1903 int droppable;
1904 adjustres_T res = {TRUE, FALSE};
1905
1906 // prop after end of the line doesn't move
1907 if (prop->tp_col == MAXCOL)
1908 {
1909 res.dirty = FALSE;
1910 return res;
1911 }
1912
1913 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1914 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001915 || (flags & APC_SUBSTITUTE)
1916 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001917 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001918 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001919 // do not drop zero-width props if they later can increase in size
1920 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001921
1922 if (added > 0)
1923 {
1924 if (col + 1 <= prop->tp_col
1925 - (start_incl || (prop->tp_len == 0 && end_incl)))
1926 // Change is entirely before the text property: Only shift
1927 prop->tp_col += added;
1928 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1929 // Insertion was inside text property
1930 prop->tp_len += added;
1931 }
1932 else if (prop->tp_col > col + 1)
1933 {
1934 if (prop->tp_col + added < col + 1)
1935 {
1936 prop->tp_len += (prop->tp_col - 1 - col) + added;
1937 prop->tp_col = col + 1;
1938 if (prop->tp_len <= 0)
1939 {
1940 prop->tp_len = 0;
1941 res.can_drop = droppable;
1942 }
1943 }
1944 else
1945 prop->tp_col += added;
1946 }
1947 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1948 {
1949 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1950
1951 prop->tp_len += after > 0 ? added + after : added;
1952 res.can_drop = prop->tp_len <= 0 && droppable;
1953 }
1954 else
1955 res.dirty = FALSE;
1956
1957 return res;
1958}
1959
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001960/*
1961 * Adjust the columns of text properties in line "lnum" after position "col" to
1962 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001963 * Note that "col" is zero-based, while tp_col is one-based.
1964 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001965 * "flags" can have:
1966 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1967 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001968 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001969 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001970 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001971 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001972adjust_prop_columns(
1973 linenr_T lnum,
1974 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001975 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001976 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001977{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001978 int proplen;
1979 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001980 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001981 int ri, wi;
1982 size_t textlen;
1983
1984 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001985 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001986
1987 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1988 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001989 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001990 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001991
Bram Moolenaar196d1572019-01-02 23:47:18 +01001992 wi = 0; // write index
1993 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001994 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001995 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001996 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001997
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001998 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1999 res = adjust_prop(&prop, col, bytes_added, flags);
2000 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002001 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002002 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002003 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2004 && u_savesub(lnum) == FAIL)
2005 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002006 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002007
2008 // u_savesub() may have updated curbuf->b_ml, fetch it again
2009 if (curbuf->b_ml.ml_line_lnum != lnum)
2010 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002011 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002012 if (res.can_drop)
2013 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002014 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002015 ++wi;
2016 }
2017 if (dirty)
2018 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002019 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2020
2021 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002022 {
2023 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2024
2025 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2026 vim_free(curbuf->b_ml.ml_line_ptr);
2027 curbuf->b_ml.ml_line_ptr = p;
2028 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002029 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002030 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002031 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002032 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002033}
2034
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002035/*
2036 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002037 * "lnum_props" is the line that has the properties from before the split.
2038 * "lnum_top" is the top line.
2039 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002040 * "deleted" is the number of bytes deleted.
2041 */
2042 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002043adjust_props_for_split(
2044 linenr_T lnum_props,
2045 linenr_T lnum_top,
2046 int kept,
2047 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002048{
2049 char_u *props;
2050 int count;
2051 garray_T prevprop;
2052 garray_T nextprop;
2053 int i;
2054 int skipped = kept + deleted;
2055
2056 if (!curbuf->b_has_textprop)
2057 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002058
2059 // Get the text properties from "lnum_props".
2060 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002061 ga_init2(&prevprop, sizeof(textprop_T), 10);
2062 ga_init2(&nextprop, sizeof(textprop_T), 10);
2063
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002064 // Keep the relevant ones in the first line, reducing the length if needed.
2065 // Copy the ones that include the split to the second line.
2066 // Move the ones after the split to the second line.
2067 for (i = 0; i < count; ++i)
2068 {
2069 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002070 proptype_T *pt;
2071 int start_incl, end_incl;
2072 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002073
2074 // copy the prop to an aligned structure
2075 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2076
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002077 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2078 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2079 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002080 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2081 cont_next = prop.tp_col != MAXCOL
2082 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002083
2084 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002085 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002086 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2087
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002088 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002089 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002090 if (p->tp_col + p->tp_len >= kept)
2091 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002092 if (cont_next)
2093 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002094 }
2095
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002096 // Only add the property to the next line if the length is bigger than
2097 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002098 if ((cont_next || prop.tp_col == MAXCOL)
2099 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002100 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002101 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002102
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002103 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002104 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002105 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002106 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002107 if (p->tp_col > skipped)
2108 p->tp_col -= skipped - 1;
2109 else
2110 {
2111 p->tp_len -= skipped - p->tp_col;
2112 p->tp_col = 1;
2113 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002114 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002115 if (cont_prev)
2116 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002117 }
2118 }
2119
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002120 set_text_props(lnum_top, prevprop.ga_data,
2121 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002122 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002123 set_text_props(lnum_top + 1, nextprop.ga_data,
2124 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002125 ga_clear(&nextprop);
2126}
2127
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002128/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002129 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002130 */
2131 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002132prepend_joined_props(
2133 char_u *new_props,
2134 int propcount,
2135 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002136 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002137 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002138 long col,
2139 int removed)
2140{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002141 char_u *props;
2142 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2143 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002144
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002145 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002146 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002147 textprop_T prop;
2148 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002149
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002150 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002151 if (prop.tp_col == MAXCOL && !last_line)
2152 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002153 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2154
2155 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002156 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002157
Bram Moolenaare175dc62022-08-01 22:18:50 +01002158 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002159 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2160 &prop, sizeof(prop));
2161 else
2162 {
2163 int j;
2164 int found = FALSE;
2165
2166 // Search for continuing prop.
2167 for (j = *props_remaining; j < propcount; ++j)
2168 {
2169 textprop_T op;
2170
2171 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2172 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2173 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002174 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002175 found = TRUE;
2176 op.tp_len += op.tp_col - prop.tp_col;
2177 op.tp_col = prop.tp_col;
2178 // Start/end is taken care of when deleting joined lines
2179 op.tp_flags = prop.tp_flags;
2180 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2181 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002182 }
2183 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002184 if (!found)
2185 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002186 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002187 }
2188}
2189
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002190#endif // FEAT_PROP_POPUP