blob: d67ec4c7e8b6ecc196616fda7c019cdc56d33107 [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 * TODO:
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010014 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
15 * into account.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010016 */
17
18#include "vim.h"
19
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010020#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010021
22/*
23 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
24 * This avoids adding a pointer to the hashtable item.
25 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
26 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
27 * HI2PT() converts a hashitem pointer to a proptype pointer.
28 */
29#define PT2HIKEY(p) ((p)->pt_name)
30#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
31#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
32
33// The global text property types.
34static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010035static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036
37// The last used text property type ID.
38static int proptype_id = 0;
39
Bram Moolenaar98aefe72018-12-13 22:20:09 +010040/*
41 * Find a property type by name, return the hashitem.
42 * Returns NULL if the item can't be found.
43 */
44 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010045find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010046{
47 hashtab_T *ht;
48 hashitem_T *hi;
49
50 if (*name == NUL)
51 return NULL;
52 if (buf == NULL)
53 ht = global_proptypes;
54 else
55 ht = buf->b_proptypes;
56
57 if (ht == NULL)
58 return NULL;
59 hi = hash_find(ht, name);
60 if (HASHITEM_EMPTY(hi))
61 return NULL;
62 return hi;
63}
64
65/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010066 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010067 */
68 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010069find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010070{
Bram Moolenaare44336b2022-08-07 18:20:08 +010071 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010072
73 if (hi == NULL)
74 return NULL;
75 return HI2PT(hi);
76}
77
78/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020079 * Get the prop type ID of "name".
80 * When not found return zero.
81 */
82 int
83find_prop_type_id(char_u *name, buf_T *buf)
84{
Bram Moolenaare44336b2022-08-07 18:20:08 +010085 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020086
87 if (pt == NULL)
88 return 0;
89 return pt->pt_id;
90}
91
92/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010093 * Lookup a property type by name. First in "buf" and when not found in the
94 * global types.
95 * When not found gives an error message and returns NULL.
96 */
97 static proptype_T *
98lookup_prop_type(char_u *name, buf_T *buf)
99{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100100 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100101
102 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100103 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100104 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100105 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100106 return type;
107}
108
109/*
110 * Get an optional "bufnr" item from the dict in "arg".
111 * When the argument is not used or "bufnr" is not present then "buf" is
112 * unchanged.
113 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100114 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100115 */
116 static int
117get_bufnr_from_arg(typval_T *arg, buf_T **buf)
118{
119 dictitem_T *di;
120
121 if (arg->v_type != VAR_DICT)
122 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000123 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100124 return FAIL;
125 }
126 if (arg->vval.v_dict == NULL)
127 return OK; // NULL dict is like an empty dict
128 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200129 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
130 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100131 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200132 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100133 if (*buf == NULL)
134 return FAIL;
135 }
136 return OK;
137}
138
139/*
140 * prop_add({lnum}, {col}, {props})
141 */
142 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100143f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100144{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100145 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100146 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100147
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200148 if (in_vim9script()
149 && (check_for_number_arg(argvars, 0) == FAIL
150 || check_for_number_arg(argvars, 1) == FAIL
151 || check_for_dict_arg(argvars, 2) == FAIL))
152 return;
153
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100154 start_lnum = tv_get_number(&argvars[0]);
155 start_col = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100156 if (argvars[2].v_type != VAR_DICT)
157 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000158 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100159 return;
160 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200161
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100162 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
163 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200164}
165
166/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200167 * Attach a text property 'type_name' to the text starting
168 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100169 * the buffer "buf" and assign identifier "id".
170 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200171 */
172 static int
173prop_add_one(
174 buf_T *buf,
175 char_u *type_name,
176 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100177 char_u *text_arg,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100178 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200179 linenr_T start_lnum,
180 linenr_T end_lnum,
181 colnr_T start_col,
182 colnr_T end_col)
183{
184 proptype_T *type;
185 linenr_T lnum;
186 int proplen;
187 char_u *props = NULL;
188 char_u *newprops;
189 size_t textlen;
190 char_u *newtext;
191 int i;
192 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100193 char_u *text = text_arg;
194 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200195
196 type = lookup_prop_type(type_name, buf);
197 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100198 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200199
200 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
201 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000202 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100203 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200204 }
205 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
206 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000207 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200209 }
210
211 if (buf->b_ml.ml_mfp == NULL)
212 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000213 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100214 goto theend;
215 }
216
217 if (text != NULL)
218 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100219 garray_T *gap = &buf->b_textprop_text;
220 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100221
222 // double check we got the right ID
223 if (-id - 1 != gap->ga_len)
224 iemsg("text prop ID mismatch");
225 if (gap->ga_growsize == 0)
226 ga_init2(gap, sizeof(char *), 50);
227 if (ga_grow(gap, 1) == FAIL)
228 goto theend;
229 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100230
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100231 // change any control character (Tab, Newline, etc.) to a Space to make
232 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100233 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100234 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100235 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100236 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200237 }
238
239 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
240 {
241 colnr_T col; // start column
242 long length; // in bytes
243
244 // Fetch the line to get the ml_line_len field updated.
245 proplen = get_text_props(buf, lnum, &props, TRUE);
246 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
247
248 if (lnum == start_lnum)
249 col = start_col;
250 else
251 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100252 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200253 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000254 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100255 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200256 }
257
258 if (lnum == end_lnum)
259 length = end_col - col;
260 else
261 length = (int)textlen - col + 1;
262 if (length > (long)textlen)
263 length = (int)textlen; // can include the end-of-line
264 if (length < 0)
265 length = 0; // zero-width property
266
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100267 if (text_arg != NULL)
268 {
269 length = 1; // text is placed on one character
270 if (col == 0)
271 col = MAXCOL; // after the line
272 }
273
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200274 // Allocate the new line with space for the new property.
275 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
276 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100277 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200278 // Copy the text, including terminating NUL.
279 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
280
281 // Find the index where to insert the new property.
282 // Since the text properties are not aligned properly when stored with
283 // the text, we need to copy them as bytes before using it as a struct.
284 for (i = 0; i < proplen; ++i)
285 {
286 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
287 sizeof(textprop_T));
288 if (tmp_prop.tp_col >= col)
289 break;
290 }
291 newprops = newtext + textlen;
292 if (i > 0)
293 mch_memmove(newprops, props, sizeof(textprop_T) * i);
294
295 tmp_prop.tp_col = col;
296 tmp_prop.tp_len = length;
297 tmp_prop.tp_id = id;
298 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100299 tmp_prop.tp_flags = text_flags
300 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
301 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200302 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
303 sizeof(textprop_T));
304
305 if (i < proplen)
306 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
307 props + i * sizeof(textprop_T),
308 sizeof(textprop_T) * (proplen - i));
309
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100310 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200311 vim_free(buf->b_ml.ml_line_ptr);
312 buf->b_ml.ml_line_ptr = newtext;
313 buf->b_ml.ml_line_len += sizeof(textprop_T);
314 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
315 }
316
317 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100318 res = OK;
319
320theend:
321 vim_free(text);
322 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200323}
324
325/*
326 * prop_add_list()
327 * First argument specifies the text property:
328 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
329 * Second argument is a List where each item is a List with the following
330 * entries: [lnum, start_col, end_col]
331 */
332 void
333f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
334{
335 dict_T *dict;
336 char_u *type_name;
337 buf_T *buf = curbuf;
338 int id = 0;
339 listitem_T *li;
340 list_T *pos_list;
341 linenr_T start_lnum;
342 colnr_T start_col;
343 linenr_T end_lnum;
344 colnr_T end_col;
345 int error = FALSE;
346
347 if (check_for_dict_arg(argvars, 0) == FAIL
348 || check_for_list_arg(argvars, 1) == FAIL)
349 return;
350
351 if (argvars[1].vval.v_list == NULL)
352 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000353 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200354 return;
355 }
356
357 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100358 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200359 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000360 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200361 return;
362 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100363 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100365 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100366 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200367
368 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
369 return;
370
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100371 // This must be done _before_ we start adding properties because property
372 // changes trigger buffer (memline) reorganisation, which needs this flag
373 // to be correctly set.
374 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200375 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
376 {
377 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
378 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000379 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200380 return;
381 }
382
383 pos_list = li->li_tv.vval.v_list;
384 start_lnum = list_find_nr(pos_list, 0L, &error);
385 start_col = list_find_nr(pos_list, 1L, &error);
386 end_lnum = list_find_nr(pos_list, 2L, &error);
387 end_col = list_find_nr(pos_list, 3L, &error);
388 if (error || start_lnum <= 0 || start_col <= 0
389 || end_lnum <= 0 || end_col <= 0)
390 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000391 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200392 return;
393 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100394 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200395 start_col, end_col) == FAIL)
396 return;
397 }
398
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200399 redraw_buf_later(buf, VALID);
400}
401
402/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100403 * Get the next ID to use for a textprop with text in buffer "buf".
404 */
405 static int
406get_textprop_id(buf_T *buf)
407{
408 // TODO: recycle deleted entries
409 return -(buf->b_textprop_text.ga_len + 1);
410}
411
412/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200413 * Shared between prop_add() and popup_create().
414 * "dict_arg" is the function argument of a dict containing "bufnr".
415 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100416 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200417 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100418 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200419prop_add_common(
420 linenr_T start_lnum,
421 colnr_T start_col,
422 dict_T *dict,
423 buf_T *default_buf,
424 typval_T *dict_arg)
425{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426 linenr_T end_lnum;
427 colnr_T end_col;
428 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200429 buf_T *buf = default_buf;
430 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100431 char_u *text = NULL;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100432 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100433
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100434 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100435 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000436 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100437 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100438 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100439 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100440
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100441 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100442 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100443 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100444 if (end_lnum < start_lnum)
445 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000446 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100447 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100448 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100449 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100450 else
451 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100452
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100453 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100454 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100455 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100456
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100457 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100458 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000459 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100460 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100461 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100462 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100463 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100464 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100465 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100466 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100467 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100468 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000469 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100470 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100471 }
472 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100473 else if (start_lnum == end_lnum)
474 end_col = start_col;
475 else
476 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100477
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100478 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100479 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100480
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100481 if (dict_has_key(dict, "text"))
482 {
483 text = dict_get_string(dict, "text", TRUE);
484 if (text == NULL)
485 goto theend;
486 // use a default length of 1 to make multiple props show up
487 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100488
489 if (dict_has_key(dict, "text_align"))
490 {
491 char_u *p = dict_get_string(dict, "text_align", FALSE);
492
493 if (p == NULL)
494 goto theend;
495 if (STRCMP(p, "right") == 0)
496 flags |= TP_FLAG_ALIGN_RIGHT;
497 else if (STRCMP(p, "below") == 0)
498 flags |= TP_FLAG_ALIGN_BELOW;
499 else if (STRCMP(p, "after") != 0)
500 {
501 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
502 goto theend;
503 }
504 }
505
506 if (dict_has_key(dict, "text_wrap"))
507 {
508 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
509 if (p == NULL)
510 goto theend;
511 if (STRCMP(p, "wrap") == 0)
512 flags |= TP_FLAG_WRAP;
513 else if (STRCMP(p, "truncate") != 0)
514 {
515 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
516 goto theend;
517 }
518 }
519 }
520
521 // Column must be 1 or more for a normal text property; when "text" is
522 // present zero means it goes after the line.
523 if (start_col < (text == NULL ? 1 : 0))
524 {
525 semsg(_(e_invalid_column_number_nr), (long)start_col);
526 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100527 }
528
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200529 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100530 goto theend;
531
532 if (id < 0 && buf->b_textprop_text.ga_len > 0)
533 {
534 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
535 goto theend;
536 }
537 if (text != NULL)
538 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100539
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100540 // This must be done _before_ we add the property because property changes
541 // trigger buffer (memline) reorganisation, which needs this flag to be
542 // correctly set.
543 buf->b_has_textprop = TRUE; // this is never reset
544
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100545 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100546 start_lnum, end_lnum, start_col, end_col);
547 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100548
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200549 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100550
551theend:
552 vim_free(text);
553 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100554}
555
556/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100557 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100558 * Returns the number of text properties and, when non-zero, a pointer to the
559 * first one in "props" (note that it is not aligned, therefore the char_u
560 * pointer).
561 */
562 int
563get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
564{
565 char_u *text;
566 size_t textlen;
567 size_t proplen;
568
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100569 // Be quick when no text property types have been defined or the buffer,
570 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200571 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100572 return 0;
573
574 // Fetch the line to get the ml_line_len field updated.
575 text = ml_get_buf(buf, lnum, will_change);
576 textlen = STRLEN(text) + 1;
577 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100578 if (proplen == 0)
579 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100580 if (proplen % sizeof(textprop_T) != 0)
581 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000582 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100583 return 0;
584 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100585 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100586 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100587}
588
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100589/*
590 * Return the number of text properties with "below" alignment in line "lnum".
591 */
592 int
593prop_count_below(buf_T *buf, linenr_T lnum)
594{
595 char_u *props;
596 int count = get_text_props(buf, lnum, &props, FALSE);
597 int result = 0;
598 textprop_T prop;
599 int i;
600
601 if (count == 0)
602 return 0;
603 for (i = 0; i < count; ++i)
604 {
605 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
606 if (prop.tp_col == MAXCOL && (prop.tp_flags & TP_FLAG_ALIGN_BELOW))
607 ++result;
608 }
609 return result;
610}
611
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200612/**
613 * Return the number of text properties on line "lnum" in the current buffer.
614 * When "only_starting" is true only text properties starting in this line will
615 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100616 * When "last_line" is FALSE then text properties after the line are not
617 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200618 */
619 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100620count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200621{
622 char_u *props;
623 int proplen = get_text_props(curbuf, lnum, &props, 0);
624 int result = proplen;
625 int i;
626 textprop_T prop;
627
Bram Moolenaare175dc62022-08-01 22:18:50 +0100628 for (i = 0; i < proplen; ++i)
629 {
630 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100631 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100632 // previous line, or when not in the last line and it is virtual text
633 // after the line.
634 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
635 || (!last_line && prop.tp_col == MAXCOL))
636 --result;
637 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200638 return result;
639}
640
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100641/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200642 * Find text property "type_id" in the visible lines of window "wp".
643 * Match "id" when it is > 0.
644 * Returns FAIL when not found.
645 */
646 int
647find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
648 linenr_T *found_lnum)
649{
650 linenr_T lnum;
651 char_u *props;
652 int count;
653 int i;
654
655 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100656 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200657 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
658 {
659 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
660 for (i = 0; i < count; ++i)
661 {
662 mch_memmove(prop, props + i * sizeof(textprop_T),
663 sizeof(textprop_T));
664 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
665 {
666 *found_lnum = lnum;
667 return OK;
668 }
669 }
670 }
671 return FAIL;
672}
673
674/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100675 * Set the text properties for line "lnum" to "props" with length "len".
676 * If "len" is zero text properties are removed, "props" is not used.
677 * Any existing text properties are dropped.
678 * Only works for the current buffer.
679 */
680 static void
681set_text_props(linenr_T lnum, char_u *props, int len)
682{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100683 char_u *text;
684 char_u *newtext;
685 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100686
687 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100688 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100689 newtext = alloc(textlen + len);
690 if (newtext == NULL)
691 return;
692 mch_memmove(newtext, text, textlen);
693 if (len > 0)
694 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100695 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100696 vim_free(curbuf->b_ml.ml_line_ptr);
697 curbuf->b_ml.ml_line_ptr = newtext;
698 curbuf->b_ml.ml_line_len = textlen + len;
699 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
700}
701
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100702/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100703 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100704 */
705 void
706add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
707{
708 char_u *text;
709 char_u *newtext;
710 int proplen = text_prop_count * (int)sizeof(textprop_T);
711
712 text = ml_get(lnum);
713 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
714 if (newtext == NULL)
715 return;
716 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
717 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
718 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
719 vim_free(curbuf->b_ml.ml_line_ptr);
720 curbuf->b_ml.ml_line_ptr = newtext;
721 curbuf->b_ml.ml_line_len += proplen;
722 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
723}
724
Bram Moolenaare44336b2022-08-07 18:20:08 +0100725/*
726 * Function passed to qsort() for sorting proptype_T on pt_id.
727 */
728 static int
729compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100730{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100731 proptype_T *tp1 = *(proptype_T **)s1;
732 proptype_T *tp2 = *(proptype_T **)s2;
733
734 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
735}
736
737 static proptype_T *
738find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
739{
740 int low = 0;
741 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100742
743 if (ht == NULL)
744 return NULL;
745
Bram Moolenaare44336b2022-08-07 18:20:08 +0100746 // Make the loopup faster by creating an array with pointers to
747 // hashtable entries, sorted on pt_id.
748 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100749 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100750 long todo;
751 hashitem_T *hi;
752 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100753
Bram Moolenaare44336b2022-08-07 18:20:08 +0100754 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
755 if (*array == NULL)
756 return NULL;
757 todo = (long)ht->ht_used;
758 for (hi = ht->ht_array; todo > 0; ++hi)
759 {
760 if (!HASHITEM_EMPTY(hi))
761 {
762 (*array)[i++] = HI2PT(hi);
763 --todo;
764 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100765 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100766 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
767 }
768
769 // binary search in the sorted array
770 high = ht->ht_used;
771 while (high > low)
772 {
773 int m = (high + low) / 2;
774
775 if ((*array)[m]->pt_id == id)
776 return (*array)[m];
777 if ((*array)[m]->pt_id > id)
778 high = m;
779 else
780 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100781 }
782 return NULL;
783}
784
785/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100786 * Fill 'dict' with text properties in 'prop'.
787 */
788 static void
789prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
790{
791 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200792 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100793
794 dict_add_number(dict, "col", prop->tp_col);
795 dict_add_number(dict, "length", prop->tp_len);
796 dict_add_number(dict, "id", prop->tp_id);
797 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
798 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200799
Bram Moolenaare44336b2022-08-07 18:20:08 +0100800 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200801 if (pt == NULL)
802 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100803 pt = find_type_by_id(global_proptypes, &global_proparray,
804 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200805 buflocal = FALSE;
806 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100807 if (pt != NULL)
808 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200809
810 if (buflocal)
811 dict_add_number(dict, "type_bufnr", buf->b_fnum);
812 else
813 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100814}
815
816/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100817 * Find a property type by ID in "buf" or globally.
818 * Returns NULL if not found.
819 */
820 proptype_T *
821text_prop_type_by_id(buf_T *buf, int id)
822{
823 proptype_T *type;
824
Bram Moolenaare44336b2022-08-07 18:20:08 +0100825 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100826 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100827 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100828 return type;
829}
830
831/*
832 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
833 */
834 void
835f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
836{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200837 linenr_T start;
838 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100839 linenr_T lnum;
840 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100841 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100842
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200843 if (in_vim9script()
844 && (check_for_number_arg(argvars, 0) == FAIL
845 || check_for_opt_number_arg(argvars, 1) == FAIL
846 || (argvars[1].v_type != VAR_UNKNOWN
847 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
848 return;
849
850 start = tv_get_number(&argvars[0]);
851 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100852 if (argvars[1].v_type != VAR_UNKNOWN)
853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100854 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100855 if (argvars[2].v_type != VAR_UNKNOWN)
856 {
857 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
858 return;
859 }
860 }
861 if (start < 1 || end < 1)
862 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200863 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100864 return;
865 }
866
867 for (lnum = start; lnum <= end; ++lnum)
868 {
869 char_u *text;
870 size_t len;
871
872 if (lnum > buf->b_ml.ml_line_count)
873 break;
874 text = ml_get_buf(buf, lnum, FALSE);
875 len = STRLEN(text) + 1;
876 if ((size_t)buf->b_ml.ml_line_len > len)
877 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100878 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100879 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
880 {
881 char_u *newtext = vim_strsave(text);
882
883 // need to allocate the line now
884 if (newtext == NULL)
885 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100886 if (buf->b_ml.ml_flags & ML_ALLOCATED)
887 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100888 buf->b_ml.ml_line_ptr = newtext;
889 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
890 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100891 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100892 }
893 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100894 if (did_clear)
895 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100896}
897
898/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100899 * prop_find({props} [, {direction}])
900 */
901 void
902f_prop_find(typval_T *argvars, typval_T *rettv)
903{
904 pos_T *cursor = &curwin->w_cursor;
905 dict_T *dict;
906 buf_T *buf = curbuf;
907 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200908 int lnum_start;
909 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100910 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200911 int id = 0;
912 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200913 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100914 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200915 int lnum = -1;
916 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100917 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100918 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100919
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200920 if (in_vim9script()
921 && (check_for_dict_arg(argvars, 0) == FAIL
922 || check_for_opt_string_arg(argvars, 1) == FAIL))
923 return;
924
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100925 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
926 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000927 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100928 return;
929 }
930 dict = argvars[0].vval.v_dict;
931
932 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
933 return;
934 if (buf->b_ml.ml_mfp == NULL)
935 return;
936
937 if (argvars[1].v_type != VAR_UNKNOWN)
938 {
939 char_u *dir_s = tv_get_string(&argvars[1]);
940
941 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100942 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100943 else if (*dir_s != 'f')
944 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000945 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100946 return;
947 }
948 }
949
950 di = dict_find(dict, (char_u *)"lnum", -1);
951 if (di != NULL)
952 lnum = tv_get_number(&di->di_tv);
953
954 di = dict_find(dict, (char_u *)"col", -1);
955 if (di != NULL)
956 col = tv_get_number(&di->di_tv);
957
958 if (lnum == -1)
959 {
960 lnum = cursor->lnum;
961 col = cursor->col + 1;
962 }
963 else if (col == -1)
964 col = 1;
965
966 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
967 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200968 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100969 return;
970 }
971
Bram Moolenaard61efa52022-07-23 09:52:04 +0100972 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100973
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100974 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200975 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100976 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200977 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200978 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100979 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100980 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100981 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100982 proptype_T *type = lookup_prop_type(name, buf);
983
984 if (type == NULL)
985 return;
986 type_id = type->pt_id;
987 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100988 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200989 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100990 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000991 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100992 return;
993 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200994 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100995 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000996 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100997 return;
998 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100999
1000 lnum_start = lnum;
1001
1002 if (rettv_dict_alloc(rettv) == FAIL)
1003 return;
1004
1005 while (1)
1006 {
1007 char_u *text = ml_get_buf(buf, lnum, FALSE);
1008 size_t textlen = STRLEN(text) + 1;
1009 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001010 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001011 int i;
1012 textprop_T prop;
1013 int prop_start;
1014 int prop_end;
1015
LemonBoy9bd3ce22022-04-18 21:54:02 +01001016 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001017 {
1018 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001019 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001020
LemonBoy9bd3ce22022-04-18 21:54:02 +01001021 // For the very first line try to find the first property before or
1022 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001023 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001024 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001025 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001026 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001027 if (prop.tp_col > col)
1028 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001029 }
1030 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1031 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001032 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001033 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001034 : (id_found && prop.tp_id == id)
1035 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001036 {
1037 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001038 if (lnum_start == lnum
1039 && col >= prop.tp_col
1040 && (col <= prop.tp_col + prop.tp_len
1041 - (prop.tp_len != 0)))
1042 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001043
LemonBoy9bd3ce22022-04-18 21:54:02 +01001044 // The property was not continued from last line, it starts on
1045 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001046 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001047 // The property does not continue on the next line, it ends on
1048 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001049 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001050 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001051 seen_end = 1;
1052
1053 // Skip lines without the start flag.
1054 if (!prop_start)
1055 {
1056 // Always search backwards for start when search started
1057 // on a prop and we're not skipping.
1058 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001059 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001060 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001061 }
1062
1063 // If skipstart is true, skip the prop at start pos (even if
1064 // continued from another line).
1065 if (start_pos_has_prop && skipstart && !seen_end)
1066 {
1067 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001068 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001069 }
1070
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001071 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1072 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1073
1074 return;
1075 }
1076 }
1077
1078 if (dir > 0)
1079 {
1080 if (lnum >= buf->b_ml.ml_line_count)
1081 break;
1082 lnum++;
1083 }
1084 else
1085 {
1086 if (lnum <= 1)
1087 break;
1088 lnum--;
1089 }
1090 }
1091}
1092
1093/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001094 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1095 */
1096 static int
1097prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1098{
1099 int i;
1100
1101 for (i = 0; i < len; i++)
1102 if (types_or_ids[i] == type_or_id)
1103 return TRUE;
1104
1105 return FALSE;
1106}
1107
1108/*
1109 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1110 * If 'prop_types' is not NULL, then return only the text properties with
1111 * matching property type in the 'prop_types' array.
1112 * If 'prop_ids' is not NULL, then return only the text properties with
1113 * an identifier in the 'props_ids' array.
1114 * If 'add_lnum' is TRUE, then add the line number also to the text property
1115 * dictionary.
1116 */
1117 static void
1118get_props_in_line(
1119 buf_T *buf,
1120 linenr_T lnum,
1121 int *prop_types,
1122 int prop_types_len,
1123 int *prop_ids,
1124 int prop_ids_len,
1125 list_T *retlist,
1126 int add_lnum)
1127{
1128 char_u *text = ml_get_buf(buf, lnum, FALSE);
1129 size_t textlen = STRLEN(text) + 1;
1130 int count;
1131 int i;
1132 textprop_T prop;
1133
1134 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1135 for (i = 0; i < count; ++i)
1136 {
1137 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1138 sizeof(textprop_T));
1139 if ((prop_types == NULL
1140 || prop_type_or_id_in_list(prop_types, prop_types_len,
1141 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001142 && (prop_ids == NULL
1143 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1144 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001145 {
1146 dict_T *d = dict_alloc();
1147
1148 if (d == NULL)
1149 break;
1150 prop_fill_dict(d, &prop, buf);
1151 if (add_lnum)
1152 dict_add_number(d, "lnum", lnum);
1153 list_append_dict(retlist, d);
1154 }
1155 }
1156}
1157
1158/*
1159 * Convert a List of property type names into an array of property type
1160 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1161 * error. 'num_types' is set to the number of returned property types.
1162 */
1163 static int *
1164get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1165{
1166 int *prop_types;
1167 listitem_T *li;
1168 int i;
1169 char_u *name;
1170 proptype_T *type;
1171
1172 *num_types = 0;
1173
1174 prop_types = ALLOC_MULT(int, list_len(l));
1175 if (prop_types == NULL)
1176 return NULL;
1177
1178 i = 0;
1179 FOR_ALL_LIST_ITEMS(l, li)
1180 {
1181 if (li->li_tv.v_type != VAR_STRING)
1182 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001183 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001184 goto errret;
1185 }
1186 name = li->li_tv.vval.v_string;
1187 if (name == NULL)
1188 goto errret;
1189
1190 type = lookup_prop_type(name, buf);
1191 if (type == NULL)
1192 goto errret;
1193 prop_types[i++] = type->pt_id;
1194 }
1195
1196 *num_types = i;
1197 return prop_types;
1198
1199errret:
1200 VIM_CLEAR(prop_types);
1201 return NULL;
1202}
1203
1204/*
1205 * Convert a List of property identifiers into an array of property
1206 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1207 * error. 'num_ids' is set to the number of returned property identifiers.
1208 */
1209 static int *
1210get_prop_ids_from_list(list_T *l, int *num_ids)
1211{
1212 int *prop_ids;
1213 listitem_T *li;
1214 int i;
1215 int id;
1216 int error;
1217
1218 *num_ids = 0;
1219
1220 prop_ids = ALLOC_MULT(int, list_len(l));
1221 if (prop_ids == NULL)
1222 return NULL;
1223
1224 i = 0;
1225 FOR_ALL_LIST_ITEMS(l, li)
1226 {
1227 error = FALSE;
1228 id = tv_get_number_chk(&li->li_tv, &error);
1229 if (error)
1230 goto errret;
1231
1232 prop_ids[i++] = id;
1233 }
1234
1235 *num_ids = i;
1236 return prop_ids;
1237
1238errret:
1239 VIM_CLEAR(prop_ids);
1240 return NULL;
1241}
1242
1243/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001244 * prop_list({lnum} [, {bufnr}])
1245 */
1246 void
1247f_prop_list(typval_T *argvars, typval_T *rettv)
1248{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001249 linenr_T lnum;
1250 linenr_T start_lnum;
1251 linenr_T end_lnum;
1252 buf_T *buf = curbuf;
1253 int add_lnum = FALSE;
1254 int *prop_types = NULL;
1255 int prop_types_len = 0;
1256 int *prop_ids = NULL;
1257 int prop_ids_len = 0;
1258 list_T *l;
1259 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001260
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001261 if (in_vim9script()
1262 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001263 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001264 return;
1265
Bram Moolenaar93a10962022-06-16 11:42:09 +01001266 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001267 return;
1268
1269 // default: get text properties on current line
1270 start_lnum = tv_get_number(&argvars[0]);
1271 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001272 if (argvars[1].v_type != VAR_UNKNOWN)
1273 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001274 dict_T *d;
1275
1276 if (argvars[1].v_type != VAR_DICT)
1277 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001278 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001279 return;
1280 }
1281 d = argvars[1].vval.v_dict;
1282
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001283 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1284 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001285
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001286 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001287 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001288 if (di->di_tv.v_type != VAR_NUMBER)
1289 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001290 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001291 return;
1292 }
1293 end_lnum = tv_get_number(&di->di_tv);
1294 if (end_lnum < 0)
1295 // negative end_lnum is used as an offset from the last buffer
1296 // line
1297 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1298 else if (end_lnum > buf->b_ml.ml_line_count)
1299 end_lnum = buf->b_ml.ml_line_count;
1300 add_lnum = TRUE;
1301 }
1302 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1303 {
1304 if (di->di_tv.v_type != VAR_LIST)
1305 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001306 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001307 return;
1308 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001309
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001310 l = di->di_tv.vval.v_list;
1311 if (l != NULL && list_len(l) > 0)
1312 {
1313 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1314 if (prop_types == NULL)
1315 return;
1316 }
1317 }
1318 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1319 {
1320 if (di->di_tv.v_type != VAR_LIST)
1321 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001322 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001323 goto errret;
1324 }
1325
1326 l = di->di_tv.vval.v_list;
1327 if (l != NULL && list_len(l) > 0)
1328 {
1329 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1330 if (prop_ids == NULL)
1331 goto errret;
1332 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001333 }
1334 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001335 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1336 || end_lnum < 1 || end_lnum < start_lnum)
1337 emsg(_(e_invalid_range));
1338 else
1339 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1340 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1341 prop_ids, prop_ids_len,
1342 rettv->vval.v_list, add_lnum);
1343
1344errret:
1345 VIM_CLEAR(prop_types);
1346 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001347}
1348
1349/*
1350 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1351 */
1352 void
1353f_prop_remove(typval_T *argvars, typval_T *rettv)
1354{
1355 linenr_T start = 1;
1356 linenr_T end = 0;
1357 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001358 linenr_T first_changed = 0;
1359 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001360 dict_T *dict;
1361 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001362 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001363 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001364 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001365 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001366 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001367
1368 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001369
1370 if (in_vim9script()
1371 && (check_for_dict_arg(argvars, 0) == FAIL
1372 || check_for_opt_number_arg(argvars, 1) == FAIL
1373 || (argvars[1].v_type != VAR_UNKNOWN
1374 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1375 return;
1376
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001377 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1378 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001379 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001380 return;
1381 }
1382
1383 if (argvars[1].v_type != VAR_UNKNOWN)
1384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001385 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001386 end = start;
1387 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001388 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001389 if (start < 1 || end < 1)
1390 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001391 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392 return;
1393 }
1394 }
1395
1396 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001397 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1398 return;
1399 if (buf->b_ml.ml_mfp == NULL)
1400 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001401
Bram Moolenaard61efa52022-07-23 09:52:04 +01001402 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001403
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001404 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001405 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001406 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001407 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001408 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001409 proptype_T *type = lookup_prop_type(name, buf);
1410
1411 if (type == NULL)
1412 return;
1413 type_id = type->pt_id;
1414 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001415 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001416
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001417 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001418 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001419 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001420 return;
1421 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001422 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001423 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001424 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001425 return;
1426 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001427
1428 if (end == 0)
1429 end = buf->b_ml.ml_line_count;
1430 for (lnum = start; lnum <= end; ++lnum)
1431 {
1432 char_u *text;
1433 size_t len;
1434
1435 if (lnum > buf->b_ml.ml_line_count)
1436 break;
1437 text = ml_get_buf(buf, lnum, FALSE);
1438 len = STRLEN(text) + 1;
1439 if ((size_t)buf->b_ml.ml_line_len > len)
1440 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001441 static textprop_T textprop; // static because of alignment
1442 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001443
1444 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1445 / sizeof(textprop_T); ++idx)
1446 {
1447 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1448 + idx * sizeof(textprop_T);
1449 size_t taillen;
1450
1451 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001452 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1453 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001454 {
1455 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1456 {
1457 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1458
1459 // need to allocate the line to be able to change it
1460 if (newptr == NULL)
1461 return;
1462 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1463 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001464 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1465 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001466 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001467 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1468
1469 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001470 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001471 }
1472
1473 taillen = buf->b_ml.ml_line_len - len
1474 - (idx + 1) * sizeof(textprop_T);
1475 if (taillen > 0)
1476 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1477 taillen);
1478 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1479 --idx;
1480
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001481 if (textprop.tp_id < 0)
1482 {
1483 garray_T *gap = &buf->b_textprop_text;
1484 int ii = -textprop.tp_id - 1;
1485
1486 // negative ID: property with text - free the text
1487 if (ii < gap->ga_len)
1488 {
1489 char_u **p = ((char_u **)gap->ga_data) + ii;
1490 vim_free(*p);
1491 *p = NULL;
1492 did_remove_text = TRUE;
1493 }
1494 }
1495
Bram Moolenaar965c0442021-05-17 00:22:06 +02001496 if (first_changed == 0)
1497 first_changed = lnum;
1498 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001499 ++rettv->vval.v_number;
1500 if (!do_all)
1501 break;
1502 }
1503 }
1504 }
1505 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001506
Bram Moolenaar965c0442021-05-17 00:22:06 +02001507 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001508 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001509 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1510 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001511 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001512
1513 if (did_remove_text)
1514 {
1515 garray_T *gap = &buf->b_textprop_text;
1516
1517 // Reduce the growarray size for NULL pointers at the end.
1518 while (gap->ga_len > 0
1519 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1520 --gap->ga_len;
1521 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001522}
1523
1524/*
1525 * Common for f_prop_type_add() and f_prop_type_change().
1526 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001527 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001528prop_type_set(typval_T *argvars, int add)
1529{
1530 char_u *name;
1531 buf_T *buf = NULL;
1532 dict_T *dict;
1533 dictitem_T *di;
1534 proptype_T *prop;
1535
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001536 if (in_vim9script()
1537 && (check_for_string_arg(argvars, 0) == FAIL
1538 || check_for_dict_arg(argvars, 1) == FAIL))
1539 return;
1540
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001541 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001542 if (*name == NUL)
1543 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001544 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001545 return;
1546 }
1547
1548 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1549 return;
1550 dict = argvars[1].vval.v_dict;
1551
Bram Moolenaare44336b2022-08-07 18:20:08 +01001552 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001553 if (add)
1554 {
1555 hashtab_T **htp;
1556
1557 if (prop != NULL)
1558 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001559 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001560 return;
1561 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001562 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001563 if (prop == NULL)
1564 return;
1565 STRCPY(prop->pt_name, name);
1566 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001567 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001568 if (buf == NULL)
1569 {
1570 htp = &global_proptypes;
1571 VIM_CLEAR(global_proparray);
1572 }
1573 else
1574 {
1575 htp = &buf->b_proptypes;
1576 VIM_CLEAR(buf->b_proparray);
1577 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001578 if (*htp == NULL)
1579 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001580 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001581 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001582 {
1583 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001584 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001585 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001586 hash_init(*htp);
1587 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001588 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001589 }
1590 else
1591 {
1592 if (prop == NULL)
1593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001595 return;
1596 }
1597 }
1598
1599 if (dict != NULL)
1600 {
1601 di = dict_find(dict, (char_u *)"highlight", -1);
1602 if (di != NULL)
1603 {
1604 char_u *highlight;
1605 int hl_id = 0;
1606
Bram Moolenaard61efa52022-07-23 09:52:04 +01001607 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001608 if (highlight != NULL && *highlight != NUL)
1609 hl_id = syn_name2id(highlight);
1610 if (hl_id <= 0)
1611 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001612 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001613 highlight == NULL ? (char_u *)"" : highlight);
1614 return;
1615 }
1616 prop->pt_hl_id = hl_id;
1617 }
1618
Bram Moolenaar58187f12019-05-05 16:33:47 +02001619 di = dict_find(dict, (char_u *)"combine", -1);
1620 if (di != NULL)
1621 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001622 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001623 prop->pt_flags |= PT_FLAG_COMBINE;
1624 else
1625 prop->pt_flags &= ~PT_FLAG_COMBINE;
1626 }
1627
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001628 di = dict_find(dict, (char_u *)"override", -1);
1629 if (di != NULL)
1630 {
1631 if (tv_get_bool(&di->di_tv))
1632 prop->pt_flags |= PT_FLAG_OVERRIDE;
1633 else
1634 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1635 }
1636
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001637 di = dict_find(dict, (char_u *)"priority", -1);
1638 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001639 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001640
1641 di = dict_find(dict, (char_u *)"start_incl", -1);
1642 if (di != NULL)
1643 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001644 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001645 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1646 else
1647 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1648 }
1649
1650 di = dict_find(dict, (char_u *)"end_incl", -1);
1651 if (di != NULL)
1652 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001653 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001654 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1655 else
1656 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1657 }
1658 }
1659}
1660
1661/*
1662 * prop_type_add({name}, {props})
1663 */
1664 void
1665f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1666{
1667 prop_type_set(argvars, TRUE);
1668}
1669
1670/*
1671 * prop_type_change({name}, {props})
1672 */
1673 void
1674f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1675{
1676 prop_type_set(argvars, FALSE);
1677}
1678
1679/*
1680 * prop_type_delete({name} [, {bufnr}])
1681 */
1682 void
1683f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1684{
1685 char_u *name;
1686 buf_T *buf = NULL;
1687 hashitem_T *hi;
1688
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001689 if (in_vim9script()
1690 && (check_for_string_arg(argvars, 0) == FAIL
1691 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1692 return;
1693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001694 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001695 if (*name == NUL)
1696 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001697 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001698 return;
1699 }
1700
1701 if (argvars[1].v_type != VAR_UNKNOWN)
1702 {
1703 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1704 return;
1705 }
1706
Bram Moolenaare44336b2022-08-07 18:20:08 +01001707 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001708 if (hi != NULL)
1709 {
1710 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001711 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001712
1713 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001714 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001715 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001716 VIM_CLEAR(global_proparray);
1717 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001718 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001719 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001720 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001721 VIM_CLEAR(buf->b_proparray);
1722 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001723 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001724 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001725 }
1726}
1727
1728/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001729 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001730 */
1731 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001732f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001733{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001734 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001735
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001736 if (in_vim9script()
1737 && (check_for_string_arg(argvars, 0) == FAIL
1738 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1739 return;
1740
1741 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001742 if (*name == NUL)
1743 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001744 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001745 return;
1746 }
1747 if (rettv_dict_alloc(rettv) == OK)
1748 {
1749 proptype_T *prop = NULL;
1750 buf_T *buf = NULL;
1751
1752 if (argvars[1].v_type != VAR_UNKNOWN)
1753 {
1754 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1755 return;
1756 }
1757
Bram Moolenaare44336b2022-08-07 18:20:08 +01001758 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001759 if (prop != NULL)
1760 {
1761 dict_T *d = rettv->vval.v_dict;
1762
1763 if (prop->pt_hl_id > 0)
1764 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1765 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001766 dict_add_number(d, "combine",
1767 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001768 dict_add_number(d, "start_incl",
1769 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1770 dict_add_number(d, "end_incl",
1771 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1772 if (buf != NULL)
1773 dict_add_number(d, "bufnr", buf->b_fnum);
1774 }
1775 }
1776}
1777
1778 static void
1779list_types(hashtab_T *ht, list_T *l)
1780{
1781 long todo;
1782 hashitem_T *hi;
1783
1784 todo = (long)ht->ht_used;
1785 for (hi = ht->ht_array; todo > 0; ++hi)
1786 {
1787 if (!HASHITEM_EMPTY(hi))
1788 {
1789 proptype_T *prop = HI2PT(hi);
1790
1791 list_append_string(l, prop->pt_name, -1);
1792 --todo;
1793 }
1794 }
1795}
1796
1797/*
1798 * prop_type_list([{bufnr}])
1799 */
1800 void
1801f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1802{
1803 buf_T *buf = NULL;
1804
1805 if (rettv_list_alloc(rettv) == OK)
1806 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001807 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1808 return;
1809
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001810 if (argvars[0].v_type != VAR_UNKNOWN)
1811 {
1812 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1813 return;
1814 }
1815 if (buf == NULL)
1816 {
1817 if (global_proptypes != NULL)
1818 list_types(global_proptypes, rettv->vval.v_list);
1819 }
1820 else if (buf->b_proptypes != NULL)
1821 list_types(buf->b_proptypes, rettv->vval.v_list);
1822 }
1823}
1824
1825/*
1826 * Free all property types in "ht".
1827 */
1828 static void
1829clear_ht_prop_types(hashtab_T *ht)
1830{
1831 long todo;
1832 hashitem_T *hi;
1833
1834 if (ht == NULL)
1835 return;
1836
1837 todo = (long)ht->ht_used;
1838 for (hi = ht->ht_array; todo > 0; ++hi)
1839 {
1840 if (!HASHITEM_EMPTY(hi))
1841 {
1842 proptype_T *prop = HI2PT(hi);
1843
1844 vim_free(prop);
1845 --todo;
1846 }
1847 }
1848
1849 hash_clear(ht);
1850 vim_free(ht);
1851}
1852
1853#if defined(EXITFREE) || defined(PROTO)
1854/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001855 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001856 */
1857 void
1858clear_global_prop_types(void)
1859{
1860 clear_ht_prop_types(global_proptypes);
1861 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001862 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001863}
1864#endif
1865
1866/*
1867 * Free all property types for "buf".
1868 */
1869 void
1870clear_buf_prop_types(buf_T *buf)
1871{
1872 clear_ht_prop_types(buf->b_proptypes);
1873 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001874 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001875}
1876
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001877// Struct used to return two values from adjust_prop().
1878typedef struct
1879{
1880 int dirty; // if the property was changed
1881 int can_drop; // whether after this change, the prop may be removed
1882} adjustres_T;
1883
1884/*
1885 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1886 *
1887 * Note that "col" is zero-based, while tp_col is one-based.
1888 * Only for the current buffer.
1889 * "flags" can have:
1890 * APC_SUBSTITUTE: Text is replaced, not inserted.
1891 */
1892 static adjustres_T
1893adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001894 textprop_T *prop,
1895 colnr_T col,
1896 int added,
1897 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001898{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001899 proptype_T *pt;
1900 int start_incl;
1901 int end_incl;
1902 int droppable;
1903 adjustres_T res = {TRUE, FALSE};
1904
1905 // prop after end of the line doesn't move
1906 if (prop->tp_col == MAXCOL)
1907 {
1908 res.dirty = FALSE;
1909 return res;
1910 }
1911
1912 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1913 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001914 || (flags & APC_SUBSTITUTE)
1915 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001916 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001917 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001918 // do not drop zero-width props if they later can increase in size
1919 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001920
1921 if (added > 0)
1922 {
1923 if (col + 1 <= prop->tp_col
1924 - (start_incl || (prop->tp_len == 0 && end_incl)))
1925 // Change is entirely before the text property: Only shift
1926 prop->tp_col += added;
1927 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1928 // Insertion was inside text property
1929 prop->tp_len += added;
1930 }
1931 else if (prop->tp_col > col + 1)
1932 {
1933 if (prop->tp_col + added < col + 1)
1934 {
1935 prop->tp_len += (prop->tp_col - 1 - col) + added;
1936 prop->tp_col = col + 1;
1937 if (prop->tp_len <= 0)
1938 {
1939 prop->tp_len = 0;
1940 res.can_drop = droppable;
1941 }
1942 }
1943 else
1944 prop->tp_col += added;
1945 }
1946 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1947 {
1948 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1949
1950 prop->tp_len += after > 0 ? added + after : added;
1951 res.can_drop = prop->tp_len <= 0 && droppable;
1952 }
1953 else
1954 res.dirty = FALSE;
1955
1956 return res;
1957}
1958
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001959/*
1960 * Adjust the columns of text properties in line "lnum" after position "col" to
1961 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001962 * Note that "col" is zero-based, while tp_col is one-based.
1963 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001964 * "flags" can have:
1965 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1966 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001967 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001968 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001969 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001970 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001971adjust_prop_columns(
1972 linenr_T lnum,
1973 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001974 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001975 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001976{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001977 int proplen;
1978 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001979 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001980 int ri, wi;
1981 size_t textlen;
1982
1983 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001984 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001985
1986 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1987 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001988 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001989 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001990
Bram Moolenaar196d1572019-01-02 23:47:18 +01001991 wi = 0; // write index
1992 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001993 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001994 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001995 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001996
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001997 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1998 res = adjust_prop(&prop, col, bytes_added, flags);
1999 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002000 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002001 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002002 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2003 && u_savesub(lnum) == FAIL)
2004 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002005 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002006
2007 // u_savesub() may have updated curbuf->b_ml, fetch it again
2008 if (curbuf->b_ml.ml_line_lnum != lnum)
2009 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002010 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002011 if (res.can_drop)
2012 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002013 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002014 ++wi;
2015 }
2016 if (dirty)
2017 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002018 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2019
2020 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002021 {
2022 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2023
2024 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2025 vim_free(curbuf->b_ml.ml_line_ptr);
2026 curbuf->b_ml.ml_line_ptr = p;
2027 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002028 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002029 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002030 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002031 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002032}
2033
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002034/*
2035 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002036 * "lnum_props" is the line that has the properties from before the split.
2037 * "lnum_top" is the top line.
2038 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002039 * "deleted" is the number of bytes deleted.
2040 */
2041 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002042adjust_props_for_split(
2043 linenr_T lnum_props,
2044 linenr_T lnum_top,
2045 int kept,
2046 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002047{
2048 char_u *props;
2049 int count;
2050 garray_T prevprop;
2051 garray_T nextprop;
2052 int i;
2053 int skipped = kept + deleted;
2054
2055 if (!curbuf->b_has_textprop)
2056 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002057
2058 // Get the text properties from "lnum_props".
2059 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002060 ga_init2(&prevprop, sizeof(textprop_T), 10);
2061 ga_init2(&nextprop, sizeof(textprop_T), 10);
2062
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002063 // Keep the relevant ones in the first line, reducing the length if needed.
2064 // Copy the ones that include the split to the second line.
2065 // Move the ones after the split to the second line.
2066 for (i = 0; i < count; ++i)
2067 {
2068 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002069 proptype_T *pt;
2070 int start_incl, end_incl;
2071 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002072
2073 // copy the prop to an aligned structure
2074 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2075
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002076 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2077 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2078 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002079 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2080 cont_next = prop.tp_col != MAXCOL
2081 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002082
2083 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002084 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002085 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2086
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002087 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002088 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002089 if (p->tp_col + p->tp_len >= kept)
2090 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002091 if (cont_next)
2092 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002093 }
2094
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002095 // Only add the property to the next line if the length is bigger than
2096 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002097 if ((cont_next || prop.tp_col == MAXCOL)
2098 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002099 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002100 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002101
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002102 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002103 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002104 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002105 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002106 if (p->tp_col > skipped)
2107 p->tp_col -= skipped - 1;
2108 else
2109 {
2110 p->tp_len -= skipped - p->tp_col;
2111 p->tp_col = 1;
2112 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002113 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002114 if (cont_prev)
2115 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002116 }
2117 }
2118
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002119 set_text_props(lnum_top, prevprop.ga_data,
2120 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002121 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002122 set_text_props(lnum_top + 1, nextprop.ga_data,
2123 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002124 ga_clear(&nextprop);
2125}
2126
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002127/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002128 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002129 */
2130 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002131prepend_joined_props(
2132 char_u *new_props,
2133 int propcount,
2134 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002135 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002136 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002137 long col,
2138 int removed)
2139{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002140 char_u *props;
2141 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2142 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002143
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002144 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002145 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002146 textprop_T prop;
2147 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002148
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002149 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002150 if (prop.tp_col == MAXCOL && !last_line)
2151 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002152 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2153
2154 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002155 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002156
Bram Moolenaare175dc62022-08-01 22:18:50 +01002157 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002158 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2159 &prop, sizeof(prop));
2160 else
2161 {
2162 int j;
2163 int found = FALSE;
2164
2165 // Search for continuing prop.
2166 for (j = *props_remaining; j < propcount; ++j)
2167 {
2168 textprop_T op;
2169
2170 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2171 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2172 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002173 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002174 found = TRUE;
2175 op.tp_len += op.tp_col - prop.tp_col;
2176 op.tp_col = prop.tp_col;
2177 // Start/end is taken care of when deleting joined lines
2178 op.tp_flags = prop.tp_flags;
2179 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2180 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002181 }
2182 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002183 if (!found)
2184 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002185 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002186 }
2187}
2188
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002189#endif // FEAT_PROP_POPUP