blob: 45023e88d5e789a6eeaaa24ce77779a0590b6455 [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;
491 if (STRCMP(p, "right") == 0)
492 flags |= TP_FLAG_ALIGN_RIGHT;
493 else if (STRCMP(p, "below") == 0)
494 flags |= TP_FLAG_ALIGN_BELOW;
495 else if (STRCMP(p, "after") != 0)
496 {
497 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
498 goto theend;
499 }
500 }
501
502 if (dict_has_key(dict, "text_wrap"))
503 {
504 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
505 if (p == NULL)
506 goto theend;
507 if (STRCMP(p, "wrap") == 0)
508 flags |= TP_FLAG_WRAP;
509 else if (STRCMP(p, "truncate") != 0)
510 {
511 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
512 goto theend;
513 }
514 }
515 }
516
517 // Column must be 1 or more for a normal text property; when "text" is
518 // present zero means it goes after the line.
519 if (start_col < (text == NULL ? 1 : 0))
520 {
521 semsg(_(e_invalid_column_number_nr), (long)start_col);
522 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100523 }
524
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200525 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100526 goto theend;
527
528 if (id < 0 && buf->b_textprop_text.ga_len > 0)
529 {
530 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
531 goto theend;
532 }
533 if (text != NULL)
534 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100535
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100536 // This must be done _before_ we add the property because property changes
537 // trigger buffer (memline) reorganisation, which needs this flag to be
538 // correctly set.
539 buf->b_has_textprop = TRUE; // this is never reset
540
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100541 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100542 start_lnum, end_lnum, start_col, end_col);
543 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100544
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200545 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100546
547theend:
548 vim_free(text);
549 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100550}
551
552/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100553 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100554 * Returns the number of text properties and, when non-zero, a pointer to the
555 * first one in "props" (note that it is not aligned, therefore the char_u
556 * pointer).
557 */
558 int
559get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
560{
561 char_u *text;
562 size_t textlen;
563 size_t proplen;
564
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100565 // Be quick when no text property types have been defined or the buffer,
566 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200567 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100568 return 0;
569
570 // Fetch the line to get the ml_line_len field updated.
571 text = ml_get_buf(buf, lnum, will_change);
572 textlen = STRLEN(text) + 1;
573 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100574 if (proplen == 0)
575 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100576 if (proplen % sizeof(textprop_T) != 0)
577 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000578 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100579 return 0;
580 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100581 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100582 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100583}
584
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100585/*
586 * Return the number of text properties with "below" alignment in line "lnum".
587 */
588 int
589prop_count_below(buf_T *buf, linenr_T lnum)
590{
591 char_u *props;
592 int count = get_text_props(buf, lnum, &props, FALSE);
593 int result = 0;
594 textprop_T prop;
595 int i;
596
597 if (count == 0)
598 return 0;
599 for (i = 0; i < count; ++i)
600 {
601 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
602 if (prop.tp_col == MAXCOL && (prop.tp_flags & TP_FLAG_ALIGN_BELOW))
603 ++result;
604 }
605 return result;
606}
607
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200608/**
609 * Return the number of text properties on line "lnum" in the current buffer.
610 * When "only_starting" is true only text properties starting in this line will
611 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100612 * When "last_line" is FALSE then text properties after the line are not
613 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200614 */
615 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100616count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200617{
618 char_u *props;
619 int proplen = get_text_props(curbuf, lnum, &props, 0);
620 int result = proplen;
621 int i;
622 textprop_T prop;
623
Bram Moolenaare175dc62022-08-01 22:18:50 +0100624 for (i = 0; i < proplen; ++i)
625 {
626 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100627 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100628 // previous line, or when not in the last line and it is virtual text
629 // after the line.
630 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
631 || (!last_line && prop.tp_col == MAXCOL))
632 --result;
633 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200634 return result;
635}
636
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100637/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200638 * Find text property "type_id" in the visible lines of window "wp".
639 * Match "id" when it is > 0.
640 * Returns FAIL when not found.
641 */
642 int
643find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
644 linenr_T *found_lnum)
645{
646 linenr_T lnum;
647 char_u *props;
648 int count;
649 int i;
650
651 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100652 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200653 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
654 {
655 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
656 for (i = 0; i < count; ++i)
657 {
658 mch_memmove(prop, props + i * sizeof(textprop_T),
659 sizeof(textprop_T));
660 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
661 {
662 *found_lnum = lnum;
663 return OK;
664 }
665 }
666 }
667 return FAIL;
668}
669
670/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100671 * Set the text properties for line "lnum" to "props" with length "len".
672 * If "len" is zero text properties are removed, "props" is not used.
673 * Any existing text properties are dropped.
674 * Only works for the current buffer.
675 */
676 static void
677set_text_props(linenr_T lnum, char_u *props, int len)
678{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100679 char_u *text;
680 char_u *newtext;
681 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100682
683 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100684 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100685 newtext = alloc(textlen + len);
686 if (newtext == NULL)
687 return;
688 mch_memmove(newtext, text, textlen);
689 if (len > 0)
690 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100691 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100692 vim_free(curbuf->b_ml.ml_line_ptr);
693 curbuf->b_ml.ml_line_ptr = newtext;
694 curbuf->b_ml.ml_line_len = textlen + len;
695 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
696}
697
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100698/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100699 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100700 */
701 void
702add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
703{
704 char_u *text;
705 char_u *newtext;
706 int proplen = text_prop_count * (int)sizeof(textprop_T);
707
708 text = ml_get(lnum);
709 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
710 if (newtext == NULL)
711 return;
712 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
713 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
714 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
715 vim_free(curbuf->b_ml.ml_line_ptr);
716 curbuf->b_ml.ml_line_ptr = newtext;
717 curbuf->b_ml.ml_line_len += proplen;
718 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
719}
720
Bram Moolenaare44336b2022-08-07 18:20:08 +0100721/*
722 * Function passed to qsort() for sorting proptype_T on pt_id.
723 */
724 static int
725compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100726{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100727 proptype_T *tp1 = *(proptype_T **)s1;
728 proptype_T *tp2 = *(proptype_T **)s2;
729
730 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
731}
732
733 static proptype_T *
734find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
735{
736 int low = 0;
737 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100738
Bram Moolenaar10246902022-08-08 17:08:05 +0100739 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100740 return NULL;
741
Bram Moolenaare44336b2022-08-07 18:20:08 +0100742 // Make the loopup faster by creating an array with pointers to
743 // hashtable entries, sorted on pt_id.
744 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100745 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100746 long todo;
747 hashitem_T *hi;
748 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100749
Bram Moolenaare44336b2022-08-07 18:20:08 +0100750 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
751 if (*array == NULL)
752 return NULL;
753 todo = (long)ht->ht_used;
754 for (hi = ht->ht_array; todo > 0; ++hi)
755 {
756 if (!HASHITEM_EMPTY(hi))
757 {
758 (*array)[i++] = HI2PT(hi);
759 --todo;
760 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100761 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100762 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
763 }
764
765 // binary search in the sorted array
766 high = ht->ht_used;
767 while (high > low)
768 {
769 int m = (high + low) / 2;
770
771 if ((*array)[m]->pt_id == id)
772 return (*array)[m];
773 if ((*array)[m]->pt_id > id)
774 high = m;
775 else
776 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100777 }
778 return NULL;
779}
780
781/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100782 * Fill 'dict' with text properties in 'prop'.
783 */
784 static void
785prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
786{
787 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200788 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100789
790 dict_add_number(dict, "col", prop->tp_col);
791 dict_add_number(dict, "length", prop->tp_len);
792 dict_add_number(dict, "id", prop->tp_id);
793 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
794 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200795
Bram Moolenaare44336b2022-08-07 18:20:08 +0100796 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200797 if (pt == NULL)
798 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100799 pt = find_type_by_id(global_proptypes, &global_proparray,
800 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200801 buflocal = FALSE;
802 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100803 if (pt != NULL)
804 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200805
806 if (buflocal)
807 dict_add_number(dict, "type_bufnr", buf->b_fnum);
808 else
809 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100810}
811
812/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100813 * Find a property type by ID in "buf" or globally.
814 * Returns NULL if not found.
815 */
816 proptype_T *
817text_prop_type_by_id(buf_T *buf, int id)
818{
819 proptype_T *type;
820
Bram Moolenaare44336b2022-08-07 18:20:08 +0100821 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100822 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100823 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100824 return type;
825}
826
827/*
828 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
829 */
830 void
831f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
832{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200833 linenr_T start;
834 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100835 linenr_T lnum;
836 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100837 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100838
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200839 if (in_vim9script()
840 && (check_for_number_arg(argvars, 0) == FAIL
841 || check_for_opt_number_arg(argvars, 1) == FAIL
842 || (argvars[1].v_type != VAR_UNKNOWN
843 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
844 return;
845
846 start = tv_get_number(&argvars[0]);
847 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100848 if (argvars[1].v_type != VAR_UNKNOWN)
849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100850 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100851 if (argvars[2].v_type != VAR_UNKNOWN)
852 {
853 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
854 return;
855 }
856 }
857 if (start < 1 || end < 1)
858 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200859 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100860 return;
861 }
862
863 for (lnum = start; lnum <= end; ++lnum)
864 {
865 char_u *text;
866 size_t len;
867
868 if (lnum > buf->b_ml.ml_line_count)
869 break;
870 text = ml_get_buf(buf, lnum, FALSE);
871 len = STRLEN(text) + 1;
872 if ((size_t)buf->b_ml.ml_line_len > len)
873 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100874 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100875 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
876 {
877 char_u *newtext = vim_strsave(text);
878
879 // need to allocate the line now
880 if (newtext == NULL)
881 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100882 if (buf->b_ml.ml_flags & ML_ALLOCATED)
883 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100884 buf->b_ml.ml_line_ptr = newtext;
885 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
886 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100887 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100888 }
889 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100890 if (did_clear)
891 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100892}
893
894/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100895 * prop_find({props} [, {direction}])
896 */
897 void
898f_prop_find(typval_T *argvars, typval_T *rettv)
899{
900 pos_T *cursor = &curwin->w_cursor;
901 dict_T *dict;
902 buf_T *buf = curbuf;
903 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200904 int lnum_start;
905 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100906 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200907 int id = 0;
908 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200909 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100910 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200911 int lnum = -1;
912 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100913 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100914 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100915
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200916 if (in_vim9script()
917 && (check_for_dict_arg(argvars, 0) == FAIL
918 || check_for_opt_string_arg(argvars, 1) == FAIL))
919 return;
920
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100921 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
922 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000923 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100924 return;
925 }
926 dict = argvars[0].vval.v_dict;
927
928 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
929 return;
930 if (buf->b_ml.ml_mfp == NULL)
931 return;
932
933 if (argvars[1].v_type != VAR_UNKNOWN)
934 {
935 char_u *dir_s = tv_get_string(&argvars[1]);
936
937 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100938 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100939 else if (*dir_s != 'f')
940 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000941 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100942 return;
943 }
944 }
945
946 di = dict_find(dict, (char_u *)"lnum", -1);
947 if (di != NULL)
948 lnum = tv_get_number(&di->di_tv);
949
950 di = dict_find(dict, (char_u *)"col", -1);
951 if (di != NULL)
952 col = tv_get_number(&di->di_tv);
953
954 if (lnum == -1)
955 {
956 lnum = cursor->lnum;
957 col = cursor->col + 1;
958 }
959 else if (col == -1)
960 col = 1;
961
962 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
963 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200964 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100965 return;
966 }
967
Bram Moolenaard61efa52022-07-23 09:52:04 +0100968 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100969
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100970 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200971 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100972 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200973 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200974 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100975 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100976 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100977 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100978 proptype_T *type = lookup_prop_type(name, buf);
979
980 if (type == NULL)
981 return;
982 type_id = type->pt_id;
983 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100984 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200985 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100986 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000987 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100988 return;
989 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200990 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100991 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000992 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100993 return;
994 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100995
996 lnum_start = lnum;
997
998 if (rettv_dict_alloc(rettv) == FAIL)
999 return;
1000
1001 while (1)
1002 {
1003 char_u *text = ml_get_buf(buf, lnum, FALSE);
1004 size_t textlen = STRLEN(text) + 1;
1005 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001006 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001007 int i;
1008 textprop_T prop;
1009 int prop_start;
1010 int prop_end;
1011
LemonBoy9bd3ce22022-04-18 21:54:02 +01001012 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001013 {
1014 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001015 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001016
LemonBoy9bd3ce22022-04-18 21:54:02 +01001017 // For the very first line try to find the first property before or
1018 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001019 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001020 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001021 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001022 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001023 if (prop.tp_col > col)
1024 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001025 }
1026 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1027 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001028 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001029 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001030 : (id_found && prop.tp_id == id)
1031 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001032 {
1033 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001034 if (lnum_start == lnum
1035 && col >= prop.tp_col
1036 && (col <= prop.tp_col + prop.tp_len
1037 - (prop.tp_len != 0)))
1038 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001039
LemonBoy9bd3ce22022-04-18 21:54:02 +01001040 // The property was not continued from last line, it starts on
1041 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001042 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001043 // The property does not continue on the next line, it ends on
1044 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001045 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001046 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001047 seen_end = 1;
1048
1049 // Skip lines without the start flag.
1050 if (!prop_start)
1051 {
1052 // Always search backwards for start when search started
1053 // on a prop and we're not skipping.
1054 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001055 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001056 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001057 }
1058
1059 // If skipstart is true, skip the prop at start pos (even if
1060 // continued from another line).
1061 if (start_pos_has_prop && skipstart && !seen_end)
1062 {
1063 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001064 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001065 }
1066
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001067 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1068 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1069
1070 return;
1071 }
1072 }
1073
1074 if (dir > 0)
1075 {
1076 if (lnum >= buf->b_ml.ml_line_count)
1077 break;
1078 lnum++;
1079 }
1080 else
1081 {
1082 if (lnum <= 1)
1083 break;
1084 lnum--;
1085 }
1086 }
1087}
1088
1089/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001090 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1091 */
1092 static int
1093prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1094{
1095 int i;
1096
1097 for (i = 0; i < len; i++)
1098 if (types_or_ids[i] == type_or_id)
1099 return TRUE;
1100
1101 return FALSE;
1102}
1103
1104/*
1105 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1106 * If 'prop_types' is not NULL, then return only the text properties with
1107 * matching property type in the 'prop_types' array.
1108 * If 'prop_ids' is not NULL, then return only the text properties with
1109 * an identifier in the 'props_ids' array.
1110 * If 'add_lnum' is TRUE, then add the line number also to the text property
1111 * dictionary.
1112 */
1113 static void
1114get_props_in_line(
1115 buf_T *buf,
1116 linenr_T lnum,
1117 int *prop_types,
1118 int prop_types_len,
1119 int *prop_ids,
1120 int prop_ids_len,
1121 list_T *retlist,
1122 int add_lnum)
1123{
1124 char_u *text = ml_get_buf(buf, lnum, FALSE);
1125 size_t textlen = STRLEN(text) + 1;
1126 int count;
1127 int i;
1128 textprop_T prop;
1129
1130 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1131 for (i = 0; i < count; ++i)
1132 {
1133 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1134 sizeof(textprop_T));
1135 if ((prop_types == NULL
1136 || prop_type_or_id_in_list(prop_types, prop_types_len,
1137 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001138 && (prop_ids == NULL
1139 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1140 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001141 {
1142 dict_T *d = dict_alloc();
1143
1144 if (d == NULL)
1145 break;
1146 prop_fill_dict(d, &prop, buf);
1147 if (add_lnum)
1148 dict_add_number(d, "lnum", lnum);
1149 list_append_dict(retlist, d);
1150 }
1151 }
1152}
1153
1154/*
1155 * Convert a List of property type names into an array of property type
1156 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1157 * error. 'num_types' is set to the number of returned property types.
1158 */
1159 static int *
1160get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1161{
1162 int *prop_types;
1163 listitem_T *li;
1164 int i;
1165 char_u *name;
1166 proptype_T *type;
1167
1168 *num_types = 0;
1169
1170 prop_types = ALLOC_MULT(int, list_len(l));
1171 if (prop_types == NULL)
1172 return NULL;
1173
1174 i = 0;
1175 FOR_ALL_LIST_ITEMS(l, li)
1176 {
1177 if (li->li_tv.v_type != VAR_STRING)
1178 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001179 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001180 goto errret;
1181 }
1182 name = li->li_tv.vval.v_string;
1183 if (name == NULL)
1184 goto errret;
1185
1186 type = lookup_prop_type(name, buf);
1187 if (type == NULL)
1188 goto errret;
1189 prop_types[i++] = type->pt_id;
1190 }
1191
1192 *num_types = i;
1193 return prop_types;
1194
1195errret:
1196 VIM_CLEAR(prop_types);
1197 return NULL;
1198}
1199
1200/*
1201 * Convert a List of property identifiers into an array of property
1202 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1203 * error. 'num_ids' is set to the number of returned property identifiers.
1204 */
1205 static int *
1206get_prop_ids_from_list(list_T *l, int *num_ids)
1207{
1208 int *prop_ids;
1209 listitem_T *li;
1210 int i;
1211 int id;
1212 int error;
1213
1214 *num_ids = 0;
1215
1216 prop_ids = ALLOC_MULT(int, list_len(l));
1217 if (prop_ids == NULL)
1218 return NULL;
1219
1220 i = 0;
1221 FOR_ALL_LIST_ITEMS(l, li)
1222 {
1223 error = FALSE;
1224 id = tv_get_number_chk(&li->li_tv, &error);
1225 if (error)
1226 goto errret;
1227
1228 prop_ids[i++] = id;
1229 }
1230
1231 *num_ids = i;
1232 return prop_ids;
1233
1234errret:
1235 VIM_CLEAR(prop_ids);
1236 return NULL;
1237}
1238
1239/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001240 * prop_list({lnum} [, {bufnr}])
1241 */
1242 void
1243f_prop_list(typval_T *argvars, typval_T *rettv)
1244{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001245 linenr_T lnum;
1246 linenr_T start_lnum;
1247 linenr_T end_lnum;
1248 buf_T *buf = curbuf;
1249 int add_lnum = FALSE;
1250 int *prop_types = NULL;
1251 int prop_types_len = 0;
1252 int *prop_ids = NULL;
1253 int prop_ids_len = 0;
1254 list_T *l;
1255 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001256
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001257 if (in_vim9script()
1258 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001259 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001260 return;
1261
Bram Moolenaar93a10962022-06-16 11:42:09 +01001262 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001263 return;
1264
1265 // default: get text properties on current line
1266 start_lnum = tv_get_number(&argvars[0]);
1267 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001268 if (argvars[1].v_type != VAR_UNKNOWN)
1269 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001270 dict_T *d;
1271
1272 if (argvars[1].v_type != VAR_DICT)
1273 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001274 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001275 return;
1276 }
1277 d = argvars[1].vval.v_dict;
1278
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001279 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1280 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001281
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001282 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001283 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001284 if (di->di_tv.v_type != VAR_NUMBER)
1285 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001286 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001287 return;
1288 }
1289 end_lnum = tv_get_number(&di->di_tv);
1290 if (end_lnum < 0)
1291 // negative end_lnum is used as an offset from the last buffer
1292 // line
1293 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1294 else if (end_lnum > buf->b_ml.ml_line_count)
1295 end_lnum = buf->b_ml.ml_line_count;
1296 add_lnum = TRUE;
1297 }
1298 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1299 {
1300 if (di->di_tv.v_type != VAR_LIST)
1301 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001302 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001303 return;
1304 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001305
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001306 l = di->di_tv.vval.v_list;
1307 if (l != NULL && list_len(l) > 0)
1308 {
1309 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1310 if (prop_types == NULL)
1311 return;
1312 }
1313 }
1314 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1315 {
1316 if (di->di_tv.v_type != VAR_LIST)
1317 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001318 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001319 goto errret;
1320 }
1321
1322 l = di->di_tv.vval.v_list;
1323 if (l != NULL && list_len(l) > 0)
1324 {
1325 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1326 if (prop_ids == NULL)
1327 goto errret;
1328 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001329 }
1330 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001331 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1332 || end_lnum < 1 || end_lnum < start_lnum)
1333 emsg(_(e_invalid_range));
1334 else
1335 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1336 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1337 prop_ids, prop_ids_len,
1338 rettv->vval.v_list, add_lnum);
1339
1340errret:
1341 VIM_CLEAR(prop_types);
1342 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001343}
1344
1345/*
1346 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1347 */
1348 void
1349f_prop_remove(typval_T *argvars, typval_T *rettv)
1350{
1351 linenr_T start = 1;
1352 linenr_T end = 0;
1353 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001354 linenr_T first_changed = 0;
1355 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001356 dict_T *dict;
1357 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001358 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001359 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001360 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001361 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001362 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001363
1364 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001365
1366 if (in_vim9script()
1367 && (check_for_dict_arg(argvars, 0) == FAIL
1368 || check_for_opt_number_arg(argvars, 1) == FAIL
1369 || (argvars[1].v_type != VAR_UNKNOWN
1370 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1371 return;
1372
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001373 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1374 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001375 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001376 return;
1377 }
1378
1379 if (argvars[1].v_type != VAR_UNKNOWN)
1380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001381 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001382 end = start;
1383 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001384 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001385 if (start < 1 || end < 1)
1386 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001387 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001388 return;
1389 }
1390 }
1391
1392 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001393 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1394 return;
1395 if (buf->b_ml.ml_mfp == NULL)
1396 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001397
Bram Moolenaard61efa52022-07-23 09:52:04 +01001398 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001399
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001400 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001401 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001402 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001403 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001404 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001405 proptype_T *type = lookup_prop_type(name, buf);
1406
1407 if (type == NULL)
1408 return;
1409 type_id = type->pt_id;
1410 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001411 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001412
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001413 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001414 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001415 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001416 return;
1417 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001418 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001419 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001420 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001421 return;
1422 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001423
1424 if (end == 0)
1425 end = buf->b_ml.ml_line_count;
1426 for (lnum = start; lnum <= end; ++lnum)
1427 {
1428 char_u *text;
1429 size_t len;
1430
1431 if (lnum > buf->b_ml.ml_line_count)
1432 break;
1433 text = ml_get_buf(buf, lnum, FALSE);
1434 len = STRLEN(text) + 1;
1435 if ((size_t)buf->b_ml.ml_line_len > len)
1436 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001437 static textprop_T textprop; // static because of alignment
1438 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001439
1440 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1441 / sizeof(textprop_T); ++idx)
1442 {
1443 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1444 + idx * sizeof(textprop_T);
1445 size_t taillen;
1446
1447 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001448 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1449 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001450 {
1451 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1452 {
1453 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1454
1455 // need to allocate the line to be able to change it
1456 if (newptr == NULL)
1457 return;
1458 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1459 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001460 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1461 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001462 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001463 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1464
1465 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001466 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001467 }
1468
1469 taillen = buf->b_ml.ml_line_len - len
1470 - (idx + 1) * sizeof(textprop_T);
1471 if (taillen > 0)
1472 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1473 taillen);
1474 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1475 --idx;
1476
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001477 if (textprop.tp_id < 0)
1478 {
1479 garray_T *gap = &buf->b_textprop_text;
1480 int ii = -textprop.tp_id - 1;
1481
1482 // negative ID: property with text - free the text
1483 if (ii < gap->ga_len)
1484 {
1485 char_u **p = ((char_u **)gap->ga_data) + ii;
1486 vim_free(*p);
1487 *p = NULL;
1488 did_remove_text = TRUE;
1489 }
1490 }
1491
Bram Moolenaar965c0442021-05-17 00:22:06 +02001492 if (first_changed == 0)
1493 first_changed = lnum;
1494 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495 ++rettv->vval.v_number;
1496 if (!do_all)
1497 break;
1498 }
1499 }
1500 }
1501 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001502
Bram Moolenaar965c0442021-05-17 00:22:06 +02001503 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001504 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001505 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1506 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001507 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001508
1509 if (did_remove_text)
1510 {
1511 garray_T *gap = &buf->b_textprop_text;
1512
1513 // Reduce the growarray size for NULL pointers at the end.
1514 while (gap->ga_len > 0
1515 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1516 --gap->ga_len;
1517 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001518}
1519
1520/*
1521 * Common for f_prop_type_add() and f_prop_type_change().
1522 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001523 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001524prop_type_set(typval_T *argvars, int add)
1525{
1526 char_u *name;
1527 buf_T *buf = NULL;
1528 dict_T *dict;
1529 dictitem_T *di;
1530 proptype_T *prop;
1531
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001532 if (in_vim9script()
1533 && (check_for_string_arg(argvars, 0) == FAIL
1534 || check_for_dict_arg(argvars, 1) == FAIL))
1535 return;
1536
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001537 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001538 if (*name == NUL)
1539 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001540 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001541 return;
1542 }
1543
1544 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1545 return;
1546 dict = argvars[1].vval.v_dict;
1547
Bram Moolenaare44336b2022-08-07 18:20:08 +01001548 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001549 if (add)
1550 {
1551 hashtab_T **htp;
1552
1553 if (prop != NULL)
1554 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001555 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001556 return;
1557 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001558 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001559 if (prop == NULL)
1560 return;
1561 STRCPY(prop->pt_name, name);
1562 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001563 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001564 if (buf == NULL)
1565 {
1566 htp = &global_proptypes;
1567 VIM_CLEAR(global_proparray);
1568 }
1569 else
1570 {
1571 htp = &buf->b_proptypes;
1572 VIM_CLEAR(buf->b_proparray);
1573 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001574 if (*htp == NULL)
1575 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001576 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001577 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001578 {
1579 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001580 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001581 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001582 hash_init(*htp);
1583 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001584 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001585 }
1586 else
1587 {
1588 if (prop == NULL)
1589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001590 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001591 return;
1592 }
1593 }
1594
1595 if (dict != NULL)
1596 {
1597 di = dict_find(dict, (char_u *)"highlight", -1);
1598 if (di != NULL)
1599 {
1600 char_u *highlight;
1601 int hl_id = 0;
1602
Bram Moolenaard61efa52022-07-23 09:52:04 +01001603 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001604 if (highlight != NULL && *highlight != NUL)
1605 hl_id = syn_name2id(highlight);
1606 if (hl_id <= 0)
1607 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001608 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001609 highlight == NULL ? (char_u *)"" : highlight);
1610 return;
1611 }
1612 prop->pt_hl_id = hl_id;
1613 }
1614
Bram Moolenaar58187f12019-05-05 16:33:47 +02001615 di = dict_find(dict, (char_u *)"combine", -1);
1616 if (di != NULL)
1617 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001618 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001619 prop->pt_flags |= PT_FLAG_COMBINE;
1620 else
1621 prop->pt_flags &= ~PT_FLAG_COMBINE;
1622 }
1623
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001624 di = dict_find(dict, (char_u *)"override", -1);
1625 if (di != NULL)
1626 {
1627 if (tv_get_bool(&di->di_tv))
1628 prop->pt_flags |= PT_FLAG_OVERRIDE;
1629 else
1630 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1631 }
1632
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001633 di = dict_find(dict, (char_u *)"priority", -1);
1634 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001635 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001636
1637 di = dict_find(dict, (char_u *)"start_incl", -1);
1638 if (di != NULL)
1639 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001640 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001641 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1642 else
1643 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1644 }
1645
1646 di = dict_find(dict, (char_u *)"end_incl", -1);
1647 if (di != NULL)
1648 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001649 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001650 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1651 else
1652 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1653 }
1654 }
1655}
1656
1657/*
1658 * prop_type_add({name}, {props})
1659 */
1660 void
1661f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1662{
1663 prop_type_set(argvars, TRUE);
1664}
1665
1666/*
1667 * prop_type_change({name}, {props})
1668 */
1669 void
1670f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1671{
1672 prop_type_set(argvars, FALSE);
1673}
1674
1675/*
1676 * prop_type_delete({name} [, {bufnr}])
1677 */
1678 void
1679f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1680{
1681 char_u *name;
1682 buf_T *buf = NULL;
1683 hashitem_T *hi;
1684
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001685 if (in_vim9script()
1686 && (check_for_string_arg(argvars, 0) == FAIL
1687 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1688 return;
1689
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001690 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001691 if (*name == NUL)
1692 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001693 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001694 return;
1695 }
1696
1697 if (argvars[1].v_type != VAR_UNKNOWN)
1698 {
1699 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1700 return;
1701 }
1702
Bram Moolenaare44336b2022-08-07 18:20:08 +01001703 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001704 if (hi != NULL)
1705 {
1706 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001707 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001708
1709 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001710 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001711 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001712 VIM_CLEAR(global_proparray);
1713 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001714 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001715 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001716 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001717 VIM_CLEAR(buf->b_proparray);
1718 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001719 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001720 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001721 }
1722}
1723
1724/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001725 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001726 */
1727 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001728f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001729{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001730 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001731
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001732 if (in_vim9script()
1733 && (check_for_string_arg(argvars, 0) == FAIL
1734 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1735 return;
1736
1737 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001738 if (*name == NUL)
1739 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001740 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001741 return;
1742 }
1743 if (rettv_dict_alloc(rettv) == OK)
1744 {
1745 proptype_T *prop = NULL;
1746 buf_T *buf = NULL;
1747
1748 if (argvars[1].v_type != VAR_UNKNOWN)
1749 {
1750 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1751 return;
1752 }
1753
Bram Moolenaare44336b2022-08-07 18:20:08 +01001754 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001755 if (prop != NULL)
1756 {
1757 dict_T *d = rettv->vval.v_dict;
1758
1759 if (prop->pt_hl_id > 0)
1760 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1761 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001762 dict_add_number(d, "combine",
1763 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001764 dict_add_number(d, "start_incl",
1765 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1766 dict_add_number(d, "end_incl",
1767 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1768 if (buf != NULL)
1769 dict_add_number(d, "bufnr", buf->b_fnum);
1770 }
1771 }
1772}
1773
1774 static void
1775list_types(hashtab_T *ht, list_T *l)
1776{
1777 long todo;
1778 hashitem_T *hi;
1779
1780 todo = (long)ht->ht_used;
1781 for (hi = ht->ht_array; todo > 0; ++hi)
1782 {
1783 if (!HASHITEM_EMPTY(hi))
1784 {
1785 proptype_T *prop = HI2PT(hi);
1786
1787 list_append_string(l, prop->pt_name, -1);
1788 --todo;
1789 }
1790 }
1791}
1792
1793/*
1794 * prop_type_list([{bufnr}])
1795 */
1796 void
1797f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1798{
1799 buf_T *buf = NULL;
1800
1801 if (rettv_list_alloc(rettv) == OK)
1802 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001803 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1804 return;
1805
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001806 if (argvars[0].v_type != VAR_UNKNOWN)
1807 {
1808 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1809 return;
1810 }
1811 if (buf == NULL)
1812 {
1813 if (global_proptypes != NULL)
1814 list_types(global_proptypes, rettv->vval.v_list);
1815 }
1816 else if (buf->b_proptypes != NULL)
1817 list_types(buf->b_proptypes, rettv->vval.v_list);
1818 }
1819}
1820
1821/*
1822 * Free all property types in "ht".
1823 */
1824 static void
1825clear_ht_prop_types(hashtab_T *ht)
1826{
1827 long todo;
1828 hashitem_T *hi;
1829
1830 if (ht == NULL)
1831 return;
1832
1833 todo = (long)ht->ht_used;
1834 for (hi = ht->ht_array; todo > 0; ++hi)
1835 {
1836 if (!HASHITEM_EMPTY(hi))
1837 {
1838 proptype_T *prop = HI2PT(hi);
1839
1840 vim_free(prop);
1841 --todo;
1842 }
1843 }
1844
1845 hash_clear(ht);
1846 vim_free(ht);
1847}
1848
1849#if defined(EXITFREE) || defined(PROTO)
1850/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001851 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001852 */
1853 void
1854clear_global_prop_types(void)
1855{
1856 clear_ht_prop_types(global_proptypes);
1857 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001858 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001859}
1860#endif
1861
1862/*
1863 * Free all property types for "buf".
1864 */
1865 void
1866clear_buf_prop_types(buf_T *buf)
1867{
1868 clear_ht_prop_types(buf->b_proptypes);
1869 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001870 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001871}
1872
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001873// Struct used to return two values from adjust_prop().
1874typedef struct
1875{
1876 int dirty; // if the property was changed
1877 int can_drop; // whether after this change, the prop may be removed
1878} adjustres_T;
1879
1880/*
1881 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1882 *
1883 * Note that "col" is zero-based, while tp_col is one-based.
1884 * Only for the current buffer.
1885 * "flags" can have:
1886 * APC_SUBSTITUTE: Text is replaced, not inserted.
1887 */
1888 static adjustres_T
1889adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001890 textprop_T *prop,
1891 colnr_T col,
1892 int added,
1893 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001894{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001895 proptype_T *pt;
1896 int start_incl;
1897 int end_incl;
1898 int droppable;
1899 adjustres_T res = {TRUE, FALSE};
1900
1901 // prop after end of the line doesn't move
1902 if (prop->tp_col == MAXCOL)
1903 {
1904 res.dirty = FALSE;
1905 return res;
1906 }
1907
1908 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1909 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001910 || (flags & APC_SUBSTITUTE)
1911 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001912 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001913 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001914 // do not drop zero-width props if they later can increase in size
1915 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001916
1917 if (added > 0)
1918 {
1919 if (col + 1 <= prop->tp_col
1920 - (start_incl || (prop->tp_len == 0 && end_incl)))
1921 // Change is entirely before the text property: Only shift
1922 prop->tp_col += added;
1923 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1924 // Insertion was inside text property
1925 prop->tp_len += added;
1926 }
1927 else if (prop->tp_col > col + 1)
1928 {
1929 if (prop->tp_col + added < col + 1)
1930 {
1931 prop->tp_len += (prop->tp_col - 1 - col) + added;
1932 prop->tp_col = col + 1;
1933 if (prop->tp_len <= 0)
1934 {
1935 prop->tp_len = 0;
1936 res.can_drop = droppable;
1937 }
1938 }
1939 else
1940 prop->tp_col += added;
1941 }
1942 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1943 {
1944 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1945
1946 prop->tp_len += after > 0 ? added + after : added;
1947 res.can_drop = prop->tp_len <= 0 && droppable;
1948 }
1949 else
1950 res.dirty = FALSE;
1951
1952 return res;
1953}
1954
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001955/*
1956 * Adjust the columns of text properties in line "lnum" after position "col" to
1957 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001958 * Note that "col" is zero-based, while tp_col is one-based.
1959 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001960 * "flags" can have:
1961 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1962 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001963 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001964 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001965 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001966 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001967adjust_prop_columns(
1968 linenr_T lnum,
1969 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001970 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001971 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001972{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001973 int proplen;
1974 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001975 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001976 int ri, wi;
1977 size_t textlen;
1978
1979 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001980 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001981
1982 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1983 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001984 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001985 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001986
Bram Moolenaar196d1572019-01-02 23:47:18 +01001987 wi = 0; // write index
1988 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001989 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001990 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001991 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001992
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001993 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1994 res = adjust_prop(&prop, col, bytes_added, flags);
1995 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001996 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001997 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001998 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1999 && u_savesub(lnum) == FAIL)
2000 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002001 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002002
2003 // u_savesub() may have updated curbuf->b_ml, fetch it again
2004 if (curbuf->b_ml.ml_line_lnum != lnum)
2005 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002006 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002007 if (res.can_drop)
2008 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002009 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002010 ++wi;
2011 }
2012 if (dirty)
2013 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002014 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2015
2016 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002017 {
2018 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2019
2020 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2021 vim_free(curbuf->b_ml.ml_line_ptr);
2022 curbuf->b_ml.ml_line_ptr = p;
2023 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002024 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002025 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002026 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002027 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002028}
2029
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002030/*
2031 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002032 * "lnum_props" is the line that has the properties from before the split.
2033 * "lnum_top" is the top line.
2034 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002035 * "deleted" is the number of bytes deleted.
2036 */
2037 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002038adjust_props_for_split(
2039 linenr_T lnum_props,
2040 linenr_T lnum_top,
2041 int kept,
2042 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002043{
2044 char_u *props;
2045 int count;
2046 garray_T prevprop;
2047 garray_T nextprop;
2048 int i;
2049 int skipped = kept + deleted;
2050
2051 if (!curbuf->b_has_textprop)
2052 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002053
2054 // Get the text properties from "lnum_props".
2055 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002056 ga_init2(&prevprop, sizeof(textprop_T), 10);
2057 ga_init2(&nextprop, sizeof(textprop_T), 10);
2058
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002059 // Keep the relevant ones in the first line, reducing the length if needed.
2060 // Copy the ones that include the split to the second line.
2061 // Move the ones after the split to the second line.
2062 for (i = 0; i < count; ++i)
2063 {
2064 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002065 proptype_T *pt;
2066 int start_incl, end_incl;
2067 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002068
2069 // copy the prop to an aligned structure
2070 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2071
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002072 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2073 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2074 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002075 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2076 cont_next = prop.tp_col != MAXCOL
2077 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002078
2079 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002080 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002081 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2082
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002083 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002084 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002085 if (p->tp_col + p->tp_len >= kept)
2086 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002087 if (cont_next)
2088 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002089 }
2090
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002091 // Only add the property to the next line if the length is bigger than
2092 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002093 if ((cont_next || prop.tp_col == MAXCOL)
2094 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002095 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002096 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002097
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002098 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002099 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002100 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002101 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002102 if (p->tp_col > skipped)
2103 p->tp_col -= skipped - 1;
2104 else
2105 {
2106 p->tp_len -= skipped - p->tp_col;
2107 p->tp_col = 1;
2108 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002109 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002110 if (cont_prev)
2111 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002112 }
2113 }
2114
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002115 set_text_props(lnum_top, prevprop.ga_data,
2116 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002117 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002118 set_text_props(lnum_top + 1, nextprop.ga_data,
2119 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002120 ga_clear(&nextprop);
2121}
2122
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002123/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002124 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002125 */
2126 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002127prepend_joined_props(
2128 char_u *new_props,
2129 int propcount,
2130 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002131 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002132 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002133 long col,
2134 int removed)
2135{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002136 char_u *props;
2137 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2138 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002139
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002140 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002141 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002142 textprop_T prop;
2143 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002144
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002145 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002146 if (prop.tp_col == MAXCOL && !last_line)
2147 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002148 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2149
2150 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002151 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002152
Bram Moolenaare175dc62022-08-01 22:18:50 +01002153 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002154 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2155 &prop, sizeof(prop));
2156 else
2157 {
2158 int j;
2159 int found = FALSE;
2160
2161 // Search for continuing prop.
2162 for (j = *props_remaining; j < propcount; ++j)
2163 {
2164 textprop_T op;
2165
2166 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2167 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2168 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002169 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002170 found = TRUE;
2171 op.tp_len += op.tp_col - prop.tp_col;
2172 op.tp_col = prop.tp_col;
2173 // Start/end is taken care of when deleting joined lines
2174 op.tp_flags = prop.tp_flags;
2175 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2176 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002177 }
2178 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002179 if (!found)
2180 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002181 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002182 }
2183}
2184
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002185#endif // FEAT_PROP_POPUP