blob: 8670c87c33393ac18f0398331636e3beeaf8682a [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 Moolenaarb9c67a52019-01-01 19:49:20 +010014 * - Adjust text property column and length when text is inserted/deleted.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020016 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010017 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaar32aa1022019-11-02 22:54:41 +010018 * - Add an array for global_proptypes, to quickly lookup a prop type by ID
19 * - Add an array for b_proptypes, to quickly lookup a prop type by ID
Bram Moolenaarb56ac042018-12-28 23:22:40 +010020 * - Checking the text length to detect text properties is slow. Use a flag in
21 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010022 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
23 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010024 * - Perhaps have a window-local option to disable highlighting from text
25 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010026 */
27
28#include "vim.h"
29
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010030#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010031
32/*
33 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
34 * This avoids adding a pointer to the hashtable item.
35 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
36 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
37 * HI2PT() converts a hashitem pointer to a proptype pointer.
38 */
39#define PT2HIKEY(p) ((p)->pt_name)
40#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
41#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
42
43// The global text property types.
44static hashtab_T *global_proptypes = NULL;
45
46// The last used text property type ID.
47static int proptype_id = 0;
48
Bram Moolenaar98aefe72018-12-13 22:20:09 +010049/*
50 * Find a property type by name, return the hashitem.
51 * Returns NULL if the item can't be found.
52 */
53 static hashitem_T *
54find_prop_hi(char_u *name, buf_T *buf)
55{
56 hashtab_T *ht;
57 hashitem_T *hi;
58
59 if (*name == NUL)
60 return NULL;
61 if (buf == NULL)
62 ht = global_proptypes;
63 else
64 ht = buf->b_proptypes;
65
66 if (ht == NULL)
67 return NULL;
68 hi = hash_find(ht, name);
69 if (HASHITEM_EMPTY(hi))
70 return NULL;
71 return hi;
72}
73
74/*
75 * Like find_prop_hi() but return the property type.
76 */
77 static proptype_T *
78find_prop(char_u *name, buf_T *buf)
79{
80 hashitem_T *hi = find_prop_hi(name, buf);
81
82 if (hi == NULL)
83 return NULL;
84 return HI2PT(hi);
85}
86
87/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020088 * Get the prop type ID of "name".
89 * When not found return zero.
90 */
91 int
92find_prop_type_id(char_u *name, buf_T *buf)
93{
94 proptype_T *pt = find_prop(name, buf);
95
96 if (pt == NULL)
97 return 0;
98 return pt->pt_id;
99}
100
101/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 * Lookup a property type by name. First in "buf" and when not found in the
103 * global types.
104 * When not found gives an error message and returns NULL.
105 */
106 static proptype_T *
107lookup_prop_type(char_u *name, buf_T *buf)
108{
109 proptype_T *type = find_prop(name, buf);
110
111 if (type == NULL)
112 type = find_prop(name, NULL);
113 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100114 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100115 return type;
116}
117
118/*
119 * Get an optional "bufnr" item from the dict in "arg".
120 * When the argument is not used or "bufnr" is not present then "buf" is
121 * unchanged.
122 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100123 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100124 */
125 static int
126get_bufnr_from_arg(typval_T *arg, buf_T **buf)
127{
128 dictitem_T *di;
129
130 if (arg->v_type != VAR_DICT)
131 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000132 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100133 return FAIL;
134 }
135 if (arg->vval.v_dict == NULL)
136 return OK; // NULL dict is like an empty dict
137 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200138 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
139 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200141 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100142 if (*buf == NULL)
143 return FAIL;
144 }
145 return OK;
146}
147
148/*
149 * prop_add({lnum}, {col}, {props})
150 */
151 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100152f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100154 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100155 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100156
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200157 if (in_vim9script()
158 && (check_for_number_arg(argvars, 0) == FAIL
159 || check_for_number_arg(argvars, 1) == FAIL
160 || check_for_dict_arg(argvars, 2) == FAIL))
161 return;
162
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100163 start_lnum = tv_get_number(&argvars[0]);
164 start_col = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100165 if (argvars[2].v_type != VAR_DICT)
166 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000167 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100168 return;
169 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200170
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100171 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
172 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200173}
174
175/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200176 * Attach a text property 'type_name' to the text starting
177 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100178 * the buffer "buf" and assign identifier "id".
179 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200180 */
181 static int
182prop_add_one(
183 buf_T *buf,
184 char_u *type_name,
185 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100186 char_u *text_arg,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100187 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200188 linenr_T start_lnum,
189 linenr_T end_lnum,
190 colnr_T start_col,
191 colnr_T end_col)
192{
193 proptype_T *type;
194 linenr_T lnum;
195 int proplen;
196 char_u *props = NULL;
197 char_u *newprops;
198 size_t textlen;
199 char_u *newtext;
200 int i;
201 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 char_u *text = text_arg;
203 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200204
205 type = lookup_prop_type(type_name, buf);
206 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100207 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200208
209 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
210 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000211 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100212 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200213 }
214 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
215 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000216 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100217 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200218 }
219
220 if (buf->b_ml.ml_mfp == NULL)
221 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000222 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100223 goto theend;
224 }
225
226 if (text != NULL)
227 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100228 garray_T *gap = &buf->b_textprop_text;
229 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100230
231 // double check we got the right ID
232 if (-id - 1 != gap->ga_len)
233 iemsg("text prop ID mismatch");
234 if (gap->ga_growsize == 0)
235 ga_init2(gap, sizeof(char *), 50);
236 if (ga_grow(gap, 1) == FAIL)
237 goto theend;
238 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100239
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100240 // change any control character (Tab, Newline, etc.) to a Space to make
241 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100242 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100243 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100244 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100245 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200246 }
247
248 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
249 {
250 colnr_T col; // start column
251 long length; // in bytes
252
253 // Fetch the line to get the ml_line_len field updated.
254 proplen = get_text_props(buf, lnum, &props, TRUE);
255 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
256
257 if (lnum == start_lnum)
258 col = start_col;
259 else
260 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100261 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200262 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000263 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100264 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200265 }
266
267 if (lnum == end_lnum)
268 length = end_col - col;
269 else
270 length = (int)textlen - col + 1;
271 if (length > (long)textlen)
272 length = (int)textlen; // can include the end-of-line
273 if (length < 0)
274 length = 0; // zero-width property
275
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100276 if (text_arg != NULL)
277 {
278 length = 1; // text is placed on one character
279 if (col == 0)
280 col = MAXCOL; // after the line
281 }
282
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200283 // Allocate the new line with space for the new property.
284 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
285 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100286 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200287 // Copy the text, including terminating NUL.
288 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
289
290 // Find the index where to insert the new property.
291 // Since the text properties are not aligned properly when stored with
292 // the text, we need to copy them as bytes before using it as a struct.
293 for (i = 0; i < proplen; ++i)
294 {
295 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
296 sizeof(textprop_T));
297 if (tmp_prop.tp_col >= col)
298 break;
299 }
300 newprops = newtext + textlen;
301 if (i > 0)
302 mch_memmove(newprops, props, sizeof(textprop_T) * i);
303
304 tmp_prop.tp_col = col;
305 tmp_prop.tp_len = length;
306 tmp_prop.tp_id = id;
307 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100308 tmp_prop.tp_flags = text_flags
309 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
310 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200311 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
312 sizeof(textprop_T));
313
314 if (i < proplen)
315 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
316 props + i * sizeof(textprop_T),
317 sizeof(textprop_T) * (proplen - i));
318
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100319 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200320 vim_free(buf->b_ml.ml_line_ptr);
321 buf->b_ml.ml_line_ptr = newtext;
322 buf->b_ml.ml_line_len += sizeof(textprop_T);
323 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
324 }
325
326 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100327 res = OK;
328
329theend:
330 vim_free(text);
331 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200332}
333
334/*
335 * prop_add_list()
336 * First argument specifies the text property:
337 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
338 * Second argument is a List where each item is a List with the following
339 * entries: [lnum, start_col, end_col]
340 */
341 void
342f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
343{
344 dict_T *dict;
345 char_u *type_name;
346 buf_T *buf = curbuf;
347 int id = 0;
348 listitem_T *li;
349 list_T *pos_list;
350 linenr_T start_lnum;
351 colnr_T start_col;
352 linenr_T end_lnum;
353 colnr_T end_col;
354 int error = FALSE;
355
356 if (check_for_dict_arg(argvars, 0) == FAIL
357 || check_for_list_arg(argvars, 1) == FAIL)
358 return;
359
360 if (argvars[1].vval.v_list == NULL)
361 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000362 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363 return;
364 }
365
366 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100367 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200368 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000369 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200370 return;
371 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100372 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200373
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100374 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100375 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200376
377 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
378 return;
379
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100380 // This must be done _before_ we start adding properties because property
381 // changes trigger buffer (memline) reorganisation, which needs this flag
382 // to be correctly set.
383 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200384 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
385 {
386 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
387 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000388 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200389 return;
390 }
391
392 pos_list = li->li_tv.vval.v_list;
393 start_lnum = list_find_nr(pos_list, 0L, &error);
394 start_col = list_find_nr(pos_list, 1L, &error);
395 end_lnum = list_find_nr(pos_list, 2L, &error);
396 end_col = list_find_nr(pos_list, 3L, &error);
397 if (error || start_lnum <= 0 || start_col <= 0
398 || end_lnum <= 0 || end_col <= 0)
399 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000400 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200401 return;
402 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100403 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200404 start_col, end_col) == FAIL)
405 return;
406 }
407
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200408 redraw_buf_later(buf, VALID);
409}
410
411/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100412 * Get the next ID to use for a textprop with text in buffer "buf".
413 */
414 static int
415get_textprop_id(buf_T *buf)
416{
417 // TODO: recycle deleted entries
418 return -(buf->b_textprop_text.ga_len + 1);
419}
420
421/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200422 * Shared between prop_add() and popup_create().
423 * "dict_arg" is the function argument of a dict containing "bufnr".
424 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100425 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100427 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200428prop_add_common(
429 linenr_T start_lnum,
430 colnr_T start_col,
431 dict_T *dict,
432 buf_T *default_buf,
433 typval_T *dict_arg)
434{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200435 linenr_T end_lnum;
436 colnr_T end_col;
437 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200438 buf_T *buf = default_buf;
439 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100440 char_u *text = NULL;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100441 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100442
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100443 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100444 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000445 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100446 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100447 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100448 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100449
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100450 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100451 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100452 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100453 if (end_lnum < start_lnum)
454 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000455 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100456 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100457 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100458 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100459 else
460 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100461
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100462 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100463 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100464 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100465
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100466 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100467 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000468 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100469 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100470 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100471 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100472 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100473 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100474 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100475 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100476 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100477 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000478 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100479 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100480 }
481 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100482 else if (start_lnum == end_lnum)
483 end_col = start_col;
484 else
485 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100486
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100487 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100488 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100489
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100490 if (dict_has_key(dict, "text"))
491 {
492 text = dict_get_string(dict, "text", TRUE);
493 if (text == NULL)
494 goto theend;
495 // use a default length of 1 to make multiple props show up
496 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100497
498 if (dict_has_key(dict, "text_align"))
499 {
500 char_u *p = dict_get_string(dict, "text_align", FALSE);
501
502 if (p == NULL)
503 goto theend;
504 if (STRCMP(p, "right") == 0)
505 flags |= TP_FLAG_ALIGN_RIGHT;
506 else if (STRCMP(p, "below") == 0)
507 flags |= TP_FLAG_ALIGN_BELOW;
508 else if (STRCMP(p, "after") != 0)
509 {
510 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
511 goto theend;
512 }
513 }
514
515 if (dict_has_key(dict, "text_wrap"))
516 {
517 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
518 if (p == NULL)
519 goto theend;
520 if (STRCMP(p, "wrap") == 0)
521 flags |= TP_FLAG_WRAP;
522 else if (STRCMP(p, "truncate") != 0)
523 {
524 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
525 goto theend;
526 }
527 }
528 }
529
530 // Column must be 1 or more for a normal text property; when "text" is
531 // present zero means it goes after the line.
532 if (start_col < (text == NULL ? 1 : 0))
533 {
534 semsg(_(e_invalid_column_number_nr), (long)start_col);
535 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100536 }
537
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200538 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100539 goto theend;
540
541 if (id < 0 && buf->b_textprop_text.ga_len > 0)
542 {
543 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
544 goto theend;
545 }
546 if (text != NULL)
547 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100548
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100549 // This must be done _before_ we add the property because property changes
550 // trigger buffer (memline) reorganisation, which needs this flag to be
551 // correctly set.
552 buf->b_has_textprop = TRUE; // this is never reset
553
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100554 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100555 start_lnum, end_lnum, start_col, end_col);
556 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100557
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200558 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100559
560theend:
561 vim_free(text);
562 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100563}
564
565/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100566 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100567 * Returns the number of text properties and, when non-zero, a pointer to the
568 * first one in "props" (note that it is not aligned, therefore the char_u
569 * pointer).
570 */
571 int
572get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
573{
574 char_u *text;
575 size_t textlen;
576 size_t proplen;
577
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100578 // Be quick when no text property types have been defined or the buffer,
579 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200580 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100581 return 0;
582
583 // Fetch the line to get the ml_line_len field updated.
584 text = ml_get_buf(buf, lnum, will_change);
585 textlen = STRLEN(text) + 1;
586 proplen = buf->b_ml.ml_line_len - textlen;
587 if (proplen % sizeof(textprop_T) != 0)
588 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000589 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100590 return 0;
591 }
592 if (proplen > 0)
593 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100594 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100595}
596
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100597/*
598 * Return the number of text properties with "below" alignment in line "lnum".
599 */
600 int
601prop_count_below(buf_T *buf, linenr_T lnum)
602{
603 char_u *props;
604 int count = get_text_props(buf, lnum, &props, FALSE);
605 int result = 0;
606 textprop_T prop;
607 int i;
608
609 if (count == 0)
610 return 0;
611 for (i = 0; i < count; ++i)
612 {
613 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
614 if (prop.tp_col == MAXCOL && (prop.tp_flags & TP_FLAG_ALIGN_BELOW))
615 ++result;
616 }
617 return result;
618}
619
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200620/**
621 * Return the number of text properties on line "lnum" in the current buffer.
622 * When "only_starting" is true only text properties starting in this line will
623 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100624 * When "last_line" is FALSE then text properties after the line are not
625 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200626 */
627 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100628count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200629{
630 char_u *props;
631 int proplen = get_text_props(curbuf, lnum, &props, 0);
632 int result = proplen;
633 int i;
634 textprop_T prop;
635
Bram Moolenaare175dc62022-08-01 22:18:50 +0100636 for (i = 0; i < proplen; ++i)
637 {
638 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100639 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100640 // previous line, or when not in the last line and it is virtual text
641 // after the line.
642 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
643 || (!last_line && prop.tp_col == MAXCOL))
644 --result;
645 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200646 return result;
647}
648
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100649/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200650 * Find text property "type_id" in the visible lines of window "wp".
651 * Match "id" when it is > 0.
652 * Returns FAIL when not found.
653 */
654 int
655find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
656 linenr_T *found_lnum)
657{
658 linenr_T lnum;
659 char_u *props;
660 int count;
661 int i;
662
663 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100664 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200665 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
666 {
667 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
668 for (i = 0; i < count; ++i)
669 {
670 mch_memmove(prop, props + i * sizeof(textprop_T),
671 sizeof(textprop_T));
672 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
673 {
674 *found_lnum = lnum;
675 return OK;
676 }
677 }
678 }
679 return FAIL;
680}
681
682/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100683 * Set the text properties for line "lnum" to "props" with length "len".
684 * If "len" is zero text properties are removed, "props" is not used.
685 * Any existing text properties are dropped.
686 * Only works for the current buffer.
687 */
688 static void
689set_text_props(linenr_T lnum, char_u *props, int len)
690{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100691 char_u *text;
692 char_u *newtext;
693 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100694
695 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100696 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100697 newtext = alloc(textlen + len);
698 if (newtext == NULL)
699 return;
700 mch_memmove(newtext, text, textlen);
701 if (len > 0)
702 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100703 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100704 vim_free(curbuf->b_ml.ml_line_ptr);
705 curbuf->b_ml.ml_line_ptr = newtext;
706 curbuf->b_ml.ml_line_len = textlen + len;
707 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
708}
709
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100710/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100711 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100712 */
713 void
714add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
715{
716 char_u *text;
717 char_u *newtext;
718 int proplen = text_prop_count * (int)sizeof(textprop_T);
719
720 text = ml_get(lnum);
721 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
722 if (newtext == NULL)
723 return;
724 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
725 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
726 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
727 vim_free(curbuf->b_ml.ml_line_ptr);
728 curbuf->b_ml.ml_line_ptr = newtext;
729 curbuf->b_ml.ml_line_len += proplen;
730 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
731}
732
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100733 static proptype_T *
734find_type_by_id(hashtab_T *ht, int id)
735{
736 long todo;
737 hashitem_T *hi;
738
739 if (ht == NULL)
740 return NULL;
741
742 // TODO: Make this faster by keeping a list of types sorted on ID and use
743 // a binary search.
744
745 todo = (long)ht->ht_used;
746 for (hi = ht->ht_array; todo > 0; ++hi)
747 {
748 if (!HASHITEM_EMPTY(hi))
749 {
750 proptype_T *prop = HI2PT(hi);
751
752 if (prop->pt_id == id)
753 return prop;
754 --todo;
755 }
756 }
757 return NULL;
758}
759
760/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100761 * Fill 'dict' with text properties in 'prop'.
762 */
763 static void
764prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
765{
766 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200767 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100768
769 dict_add_number(dict, "col", prop->tp_col);
770 dict_add_number(dict, "length", prop->tp_len);
771 dict_add_number(dict, "id", prop->tp_id);
772 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
773 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200774
775 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
776 if (pt == NULL)
777 {
778 pt = find_type_by_id(global_proptypes, prop->tp_type);
779 buflocal = FALSE;
780 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100781 if (pt != NULL)
782 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200783
784 if (buflocal)
785 dict_add_number(dict, "type_bufnr", buf->b_fnum);
786 else
787 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100788}
789
790/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100791 * Find a property type by ID in "buf" or globally.
792 * Returns NULL if not found.
793 */
794 proptype_T *
795text_prop_type_by_id(buf_T *buf, int id)
796{
797 proptype_T *type;
798
799 type = find_type_by_id(buf->b_proptypes, id);
800 if (type == NULL)
801 type = find_type_by_id(global_proptypes, id);
802 return type;
803}
804
805/*
806 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
807 */
808 void
809f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
810{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200811 linenr_T start;
812 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100813 linenr_T lnum;
814 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100815 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100816
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200817 if (in_vim9script()
818 && (check_for_number_arg(argvars, 0) == FAIL
819 || check_for_opt_number_arg(argvars, 1) == FAIL
820 || (argvars[1].v_type != VAR_UNKNOWN
821 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
822 return;
823
824 start = tv_get_number(&argvars[0]);
825 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100826 if (argvars[1].v_type != VAR_UNKNOWN)
827 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100828 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100829 if (argvars[2].v_type != VAR_UNKNOWN)
830 {
831 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
832 return;
833 }
834 }
835 if (start < 1 || end < 1)
836 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200837 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100838 return;
839 }
840
841 for (lnum = start; lnum <= end; ++lnum)
842 {
843 char_u *text;
844 size_t len;
845
846 if (lnum > buf->b_ml.ml_line_count)
847 break;
848 text = ml_get_buf(buf, lnum, FALSE);
849 len = STRLEN(text) + 1;
850 if ((size_t)buf->b_ml.ml_line_len > len)
851 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100852 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100853 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
854 {
855 char_u *newtext = vim_strsave(text);
856
857 // need to allocate the line now
858 if (newtext == NULL)
859 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100860 if (buf->b_ml.ml_flags & ML_ALLOCATED)
861 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100862 buf->b_ml.ml_line_ptr = newtext;
863 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
864 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100865 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100866 }
867 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100868 if (did_clear)
869 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100870}
871
872/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100873 * prop_find({props} [, {direction}])
874 */
875 void
876f_prop_find(typval_T *argvars, typval_T *rettv)
877{
878 pos_T *cursor = &curwin->w_cursor;
879 dict_T *dict;
880 buf_T *buf = curbuf;
881 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200882 int lnum_start;
883 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100884 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200885 int id = 0;
886 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200887 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100888 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200889 int lnum = -1;
890 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100891 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100892 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100893
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200894 if (in_vim9script()
895 && (check_for_dict_arg(argvars, 0) == FAIL
896 || check_for_opt_string_arg(argvars, 1) == FAIL))
897 return;
898
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100899 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
900 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000901 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100902 return;
903 }
904 dict = argvars[0].vval.v_dict;
905
906 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
907 return;
908 if (buf->b_ml.ml_mfp == NULL)
909 return;
910
911 if (argvars[1].v_type != VAR_UNKNOWN)
912 {
913 char_u *dir_s = tv_get_string(&argvars[1]);
914
915 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100916 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100917 else if (*dir_s != 'f')
918 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000919 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100920 return;
921 }
922 }
923
924 di = dict_find(dict, (char_u *)"lnum", -1);
925 if (di != NULL)
926 lnum = tv_get_number(&di->di_tv);
927
928 di = dict_find(dict, (char_u *)"col", -1);
929 if (di != NULL)
930 col = tv_get_number(&di->di_tv);
931
932 if (lnum == -1)
933 {
934 lnum = cursor->lnum;
935 col = cursor->col + 1;
936 }
937 else if (col == -1)
938 col = 1;
939
940 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
941 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200942 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100943 return;
944 }
945
Bram Moolenaard61efa52022-07-23 09:52:04 +0100946 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100947
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100948 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200949 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100950 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200951 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200952 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100953 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100954 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100955 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100956 proptype_T *type = lookup_prop_type(name, buf);
957
958 if (type == NULL)
959 return;
960 type_id = type->pt_id;
961 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100962 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200963 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100964 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000965 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100966 return;
967 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200968 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100969 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000970 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100971 return;
972 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100973
974 lnum_start = lnum;
975
976 if (rettv_dict_alloc(rettv) == FAIL)
977 return;
978
979 while (1)
980 {
981 char_u *text = ml_get_buf(buf, lnum, FALSE);
982 size_t textlen = STRLEN(text) + 1;
983 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200984 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100985 int i;
986 textprop_T prop;
987 int prop_start;
988 int prop_end;
989
LemonBoy9bd3ce22022-04-18 21:54:02 +0100990 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100991 {
992 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100993 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100994
LemonBoy9bd3ce22022-04-18 21:54:02 +0100995 // For the very first line try to find the first property before or
996 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100997 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100998 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100999 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001000 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001001 if (prop.tp_col > col)
1002 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001003 }
1004 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1005 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001006 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001007 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001008 : (id_found && prop.tp_id == id)
1009 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001010 {
1011 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001012 if (lnum_start == lnum
1013 && col >= prop.tp_col
1014 && (col <= prop.tp_col + prop.tp_len
1015 - (prop.tp_len != 0)))
1016 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001017
LemonBoy9bd3ce22022-04-18 21:54:02 +01001018 // The property was not continued from last line, it starts on
1019 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001020 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001021 // The property does not continue on the next line, it ends on
1022 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001023 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001024 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001025 seen_end = 1;
1026
1027 // Skip lines without the start flag.
1028 if (!prop_start)
1029 {
1030 // Always search backwards for start when search started
1031 // on a prop and we're not skipping.
1032 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001033 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001034 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001035 }
1036
1037 // If skipstart is true, skip the prop at start pos (even if
1038 // continued from another line).
1039 if (start_pos_has_prop && skipstart && !seen_end)
1040 {
1041 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001042 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001043 }
1044
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001045 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1046 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1047
1048 return;
1049 }
1050 }
1051
1052 if (dir > 0)
1053 {
1054 if (lnum >= buf->b_ml.ml_line_count)
1055 break;
1056 lnum++;
1057 }
1058 else
1059 {
1060 if (lnum <= 1)
1061 break;
1062 lnum--;
1063 }
1064 }
1065}
1066
1067/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001068 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1069 */
1070 static int
1071prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1072{
1073 int i;
1074
1075 for (i = 0; i < len; i++)
1076 if (types_or_ids[i] == type_or_id)
1077 return TRUE;
1078
1079 return FALSE;
1080}
1081
1082/*
1083 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1084 * If 'prop_types' is not NULL, then return only the text properties with
1085 * matching property type in the 'prop_types' array.
1086 * If 'prop_ids' is not NULL, then return only the text properties with
1087 * an identifier in the 'props_ids' array.
1088 * If 'add_lnum' is TRUE, then add the line number also to the text property
1089 * dictionary.
1090 */
1091 static void
1092get_props_in_line(
1093 buf_T *buf,
1094 linenr_T lnum,
1095 int *prop_types,
1096 int prop_types_len,
1097 int *prop_ids,
1098 int prop_ids_len,
1099 list_T *retlist,
1100 int add_lnum)
1101{
1102 char_u *text = ml_get_buf(buf, lnum, FALSE);
1103 size_t textlen = STRLEN(text) + 1;
1104 int count;
1105 int i;
1106 textprop_T prop;
1107
1108 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1109 for (i = 0; i < count; ++i)
1110 {
1111 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1112 sizeof(textprop_T));
1113 if ((prop_types == NULL
1114 || prop_type_or_id_in_list(prop_types, prop_types_len,
1115 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001116 && (prop_ids == NULL
1117 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1118 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001119 {
1120 dict_T *d = dict_alloc();
1121
1122 if (d == NULL)
1123 break;
1124 prop_fill_dict(d, &prop, buf);
1125 if (add_lnum)
1126 dict_add_number(d, "lnum", lnum);
1127 list_append_dict(retlist, d);
1128 }
1129 }
1130}
1131
1132/*
1133 * Convert a List of property type names into an array of property type
1134 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1135 * error. 'num_types' is set to the number of returned property types.
1136 */
1137 static int *
1138get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1139{
1140 int *prop_types;
1141 listitem_T *li;
1142 int i;
1143 char_u *name;
1144 proptype_T *type;
1145
1146 *num_types = 0;
1147
1148 prop_types = ALLOC_MULT(int, list_len(l));
1149 if (prop_types == NULL)
1150 return NULL;
1151
1152 i = 0;
1153 FOR_ALL_LIST_ITEMS(l, li)
1154 {
1155 if (li->li_tv.v_type != VAR_STRING)
1156 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001157 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001158 goto errret;
1159 }
1160 name = li->li_tv.vval.v_string;
1161 if (name == NULL)
1162 goto errret;
1163
1164 type = lookup_prop_type(name, buf);
1165 if (type == NULL)
1166 goto errret;
1167 prop_types[i++] = type->pt_id;
1168 }
1169
1170 *num_types = i;
1171 return prop_types;
1172
1173errret:
1174 VIM_CLEAR(prop_types);
1175 return NULL;
1176}
1177
1178/*
1179 * Convert a List of property identifiers into an array of property
1180 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1181 * error. 'num_ids' is set to the number of returned property identifiers.
1182 */
1183 static int *
1184get_prop_ids_from_list(list_T *l, int *num_ids)
1185{
1186 int *prop_ids;
1187 listitem_T *li;
1188 int i;
1189 int id;
1190 int error;
1191
1192 *num_ids = 0;
1193
1194 prop_ids = ALLOC_MULT(int, list_len(l));
1195 if (prop_ids == NULL)
1196 return NULL;
1197
1198 i = 0;
1199 FOR_ALL_LIST_ITEMS(l, li)
1200 {
1201 error = FALSE;
1202 id = tv_get_number_chk(&li->li_tv, &error);
1203 if (error)
1204 goto errret;
1205
1206 prop_ids[i++] = id;
1207 }
1208
1209 *num_ids = i;
1210 return prop_ids;
1211
1212errret:
1213 VIM_CLEAR(prop_ids);
1214 return NULL;
1215}
1216
1217/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001218 * prop_list({lnum} [, {bufnr}])
1219 */
1220 void
1221f_prop_list(typval_T *argvars, typval_T *rettv)
1222{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001223 linenr_T lnum;
1224 linenr_T start_lnum;
1225 linenr_T end_lnum;
1226 buf_T *buf = curbuf;
1227 int add_lnum = FALSE;
1228 int *prop_types = NULL;
1229 int prop_types_len = 0;
1230 int *prop_ids = NULL;
1231 int prop_ids_len = 0;
1232 list_T *l;
1233 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001234
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001235 if (in_vim9script()
1236 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001237 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001238 return;
1239
Bram Moolenaar93a10962022-06-16 11:42:09 +01001240 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001241 return;
1242
1243 // default: get text properties on current line
1244 start_lnum = tv_get_number(&argvars[0]);
1245 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001246 if (argvars[1].v_type != VAR_UNKNOWN)
1247 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001248 dict_T *d;
1249
1250 if (argvars[1].v_type != VAR_DICT)
1251 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001252 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001253 return;
1254 }
1255 d = argvars[1].vval.v_dict;
1256
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001257 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1258 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001259
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001260 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001261 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001262 if (di->di_tv.v_type != VAR_NUMBER)
1263 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001264 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001265 return;
1266 }
1267 end_lnum = tv_get_number(&di->di_tv);
1268 if (end_lnum < 0)
1269 // negative end_lnum is used as an offset from the last buffer
1270 // line
1271 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1272 else if (end_lnum > buf->b_ml.ml_line_count)
1273 end_lnum = buf->b_ml.ml_line_count;
1274 add_lnum = TRUE;
1275 }
1276 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1277 {
1278 if (di->di_tv.v_type != VAR_LIST)
1279 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001280 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001281 return;
1282 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001283
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001284 l = di->di_tv.vval.v_list;
1285 if (l != NULL && list_len(l) > 0)
1286 {
1287 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1288 if (prop_types == NULL)
1289 return;
1290 }
1291 }
1292 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1293 {
1294 if (di->di_tv.v_type != VAR_LIST)
1295 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001296 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001297 goto errret;
1298 }
1299
1300 l = di->di_tv.vval.v_list;
1301 if (l != NULL && list_len(l) > 0)
1302 {
1303 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1304 if (prop_ids == NULL)
1305 goto errret;
1306 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001307 }
1308 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001309 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1310 || end_lnum < 1 || end_lnum < start_lnum)
1311 emsg(_(e_invalid_range));
1312 else
1313 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1314 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1315 prop_ids, prop_ids_len,
1316 rettv->vval.v_list, add_lnum);
1317
1318errret:
1319 VIM_CLEAR(prop_types);
1320 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001321}
1322
1323/*
1324 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1325 */
1326 void
1327f_prop_remove(typval_T *argvars, typval_T *rettv)
1328{
1329 linenr_T start = 1;
1330 linenr_T end = 0;
1331 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001332 linenr_T first_changed = 0;
1333 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001334 dict_T *dict;
1335 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001336 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001337 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001338 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001339 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001340 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001341
1342 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001343
1344 if (in_vim9script()
1345 && (check_for_dict_arg(argvars, 0) == FAIL
1346 || check_for_opt_number_arg(argvars, 1) == FAIL
1347 || (argvars[1].v_type != VAR_UNKNOWN
1348 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1349 return;
1350
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001351 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1352 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001353 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001354 return;
1355 }
1356
1357 if (argvars[1].v_type != VAR_UNKNOWN)
1358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001359 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001360 end = start;
1361 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001362 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001363 if (start < 1 || end < 1)
1364 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001365 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001366 return;
1367 }
1368 }
1369
1370 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001371 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1372 return;
1373 if (buf->b_ml.ml_mfp == NULL)
1374 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001375
Bram Moolenaard61efa52022-07-23 09:52:04 +01001376 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001377
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001378 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001379 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001380 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001381 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001382 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001383 proptype_T *type = lookup_prop_type(name, buf);
1384
1385 if (type == NULL)
1386 return;
1387 type_id = type->pt_id;
1388 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001389 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001390
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001391 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001393 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001394 return;
1395 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001396 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001397 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001398 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001399 return;
1400 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001401
1402 if (end == 0)
1403 end = buf->b_ml.ml_line_count;
1404 for (lnum = start; lnum <= end; ++lnum)
1405 {
1406 char_u *text;
1407 size_t len;
1408
1409 if (lnum > buf->b_ml.ml_line_count)
1410 break;
1411 text = ml_get_buf(buf, lnum, FALSE);
1412 len = STRLEN(text) + 1;
1413 if ((size_t)buf->b_ml.ml_line_len > len)
1414 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001415 static textprop_T textprop; // static because of alignment
1416 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001417
1418 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1419 / sizeof(textprop_T); ++idx)
1420 {
1421 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1422 + idx * sizeof(textprop_T);
1423 size_t taillen;
1424
1425 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001426 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1427 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001428 {
1429 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1430 {
1431 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1432
1433 // need to allocate the line to be able to change it
1434 if (newptr == NULL)
1435 return;
1436 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1437 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001438 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1439 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001440 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001441 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1442
1443 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001444 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001445 }
1446
1447 taillen = buf->b_ml.ml_line_len - len
1448 - (idx + 1) * sizeof(textprop_T);
1449 if (taillen > 0)
1450 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1451 taillen);
1452 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1453 --idx;
1454
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001455 if (textprop.tp_id < 0)
1456 {
1457 garray_T *gap = &buf->b_textprop_text;
1458 int ii = -textprop.tp_id - 1;
1459
1460 // negative ID: property with text - free the text
1461 if (ii < gap->ga_len)
1462 {
1463 char_u **p = ((char_u **)gap->ga_data) + ii;
1464 vim_free(*p);
1465 *p = NULL;
1466 did_remove_text = TRUE;
1467 }
1468 }
1469
Bram Moolenaar965c0442021-05-17 00:22:06 +02001470 if (first_changed == 0)
1471 first_changed = lnum;
1472 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001473 ++rettv->vval.v_number;
1474 if (!do_all)
1475 break;
1476 }
1477 }
1478 }
1479 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001480
Bram Moolenaar965c0442021-05-17 00:22:06 +02001481 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001482 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001483 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1484 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001485 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001486
1487 if (did_remove_text)
1488 {
1489 garray_T *gap = &buf->b_textprop_text;
1490
1491 // Reduce the growarray size for NULL pointers at the end.
1492 while (gap->ga_len > 0
1493 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1494 --gap->ga_len;
1495 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001496}
1497
1498/*
1499 * Common for f_prop_type_add() and f_prop_type_change().
1500 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001501 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001502prop_type_set(typval_T *argvars, int add)
1503{
1504 char_u *name;
1505 buf_T *buf = NULL;
1506 dict_T *dict;
1507 dictitem_T *di;
1508 proptype_T *prop;
1509
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001510 if (in_vim9script()
1511 && (check_for_string_arg(argvars, 0) == FAIL
1512 || check_for_dict_arg(argvars, 1) == FAIL))
1513 return;
1514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001515 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001516 if (*name == NUL)
1517 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001518 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001519 return;
1520 }
1521
1522 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1523 return;
1524 dict = argvars[1].vval.v_dict;
1525
1526 prop = find_prop(name, buf);
1527 if (add)
1528 {
1529 hashtab_T **htp;
1530
1531 if (prop != NULL)
1532 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001533 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001534 return;
1535 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001536 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001537 if (prop == NULL)
1538 return;
1539 STRCPY(prop->pt_name, name);
1540 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001541 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001542 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1543 if (*htp == NULL)
1544 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001545 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001546 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001547 {
1548 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001549 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001550 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001551 hash_init(*htp);
1552 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001553 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001554 }
1555 else
1556 {
1557 if (prop == NULL)
1558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001559 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001560 return;
1561 }
1562 }
1563
1564 if (dict != NULL)
1565 {
1566 di = dict_find(dict, (char_u *)"highlight", -1);
1567 if (di != NULL)
1568 {
1569 char_u *highlight;
1570 int hl_id = 0;
1571
Bram Moolenaard61efa52022-07-23 09:52:04 +01001572 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001573 if (highlight != NULL && *highlight != NUL)
1574 hl_id = syn_name2id(highlight);
1575 if (hl_id <= 0)
1576 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001577 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001578 highlight == NULL ? (char_u *)"" : highlight);
1579 return;
1580 }
1581 prop->pt_hl_id = hl_id;
1582 }
1583
Bram Moolenaar58187f12019-05-05 16:33:47 +02001584 di = dict_find(dict, (char_u *)"combine", -1);
1585 if (di != NULL)
1586 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001587 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001588 prop->pt_flags |= PT_FLAG_COMBINE;
1589 else
1590 prop->pt_flags &= ~PT_FLAG_COMBINE;
1591 }
1592
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001593 di = dict_find(dict, (char_u *)"override", -1);
1594 if (di != NULL)
1595 {
1596 if (tv_get_bool(&di->di_tv))
1597 prop->pt_flags |= PT_FLAG_OVERRIDE;
1598 else
1599 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1600 }
1601
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001602 di = dict_find(dict, (char_u *)"priority", -1);
1603 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001604 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001605
1606 di = dict_find(dict, (char_u *)"start_incl", -1);
1607 if (di != NULL)
1608 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001609 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001610 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1611 else
1612 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1613 }
1614
1615 di = dict_find(dict, (char_u *)"end_incl", -1);
1616 if (di != NULL)
1617 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001618 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001619 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1620 else
1621 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1622 }
1623 }
1624}
1625
1626/*
1627 * prop_type_add({name}, {props})
1628 */
1629 void
1630f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1631{
1632 prop_type_set(argvars, TRUE);
1633}
1634
1635/*
1636 * prop_type_change({name}, {props})
1637 */
1638 void
1639f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1640{
1641 prop_type_set(argvars, FALSE);
1642}
1643
1644/*
1645 * prop_type_delete({name} [, {bufnr}])
1646 */
1647 void
1648f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1649{
1650 char_u *name;
1651 buf_T *buf = NULL;
1652 hashitem_T *hi;
1653
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001654 if (in_vim9script()
1655 && (check_for_string_arg(argvars, 0) == FAIL
1656 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1657 return;
1658
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001659 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001660 if (*name == NUL)
1661 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001662 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001663 return;
1664 }
1665
1666 if (argvars[1].v_type != VAR_UNKNOWN)
1667 {
1668 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1669 return;
1670 }
1671
1672 hi = find_prop_hi(name, buf);
1673 if (hi != NULL)
1674 {
1675 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001676 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001677
1678 if (buf == NULL)
1679 ht = global_proptypes;
1680 else
1681 ht = buf->b_proptypes;
1682 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001683 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001684 }
1685}
1686
1687/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001688 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001689 */
1690 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001691f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001692{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001693 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001694
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001695 if (in_vim9script()
1696 && (check_for_string_arg(argvars, 0) == FAIL
1697 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1698 return;
1699
1700 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001701 if (*name == NUL)
1702 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001703 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001704 return;
1705 }
1706 if (rettv_dict_alloc(rettv) == OK)
1707 {
1708 proptype_T *prop = NULL;
1709 buf_T *buf = NULL;
1710
1711 if (argvars[1].v_type != VAR_UNKNOWN)
1712 {
1713 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1714 return;
1715 }
1716
1717 prop = find_prop(name, buf);
1718 if (prop != NULL)
1719 {
1720 dict_T *d = rettv->vval.v_dict;
1721
1722 if (prop->pt_hl_id > 0)
1723 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1724 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001725 dict_add_number(d, "combine",
1726 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001727 dict_add_number(d, "start_incl",
1728 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1729 dict_add_number(d, "end_incl",
1730 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1731 if (buf != NULL)
1732 dict_add_number(d, "bufnr", buf->b_fnum);
1733 }
1734 }
1735}
1736
1737 static void
1738list_types(hashtab_T *ht, list_T *l)
1739{
1740 long todo;
1741 hashitem_T *hi;
1742
1743 todo = (long)ht->ht_used;
1744 for (hi = ht->ht_array; todo > 0; ++hi)
1745 {
1746 if (!HASHITEM_EMPTY(hi))
1747 {
1748 proptype_T *prop = HI2PT(hi);
1749
1750 list_append_string(l, prop->pt_name, -1);
1751 --todo;
1752 }
1753 }
1754}
1755
1756/*
1757 * prop_type_list([{bufnr}])
1758 */
1759 void
1760f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1761{
1762 buf_T *buf = NULL;
1763
1764 if (rettv_list_alloc(rettv) == OK)
1765 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001766 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1767 return;
1768
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001769 if (argvars[0].v_type != VAR_UNKNOWN)
1770 {
1771 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1772 return;
1773 }
1774 if (buf == NULL)
1775 {
1776 if (global_proptypes != NULL)
1777 list_types(global_proptypes, rettv->vval.v_list);
1778 }
1779 else if (buf->b_proptypes != NULL)
1780 list_types(buf->b_proptypes, rettv->vval.v_list);
1781 }
1782}
1783
1784/*
1785 * Free all property types in "ht".
1786 */
1787 static void
1788clear_ht_prop_types(hashtab_T *ht)
1789{
1790 long todo;
1791 hashitem_T *hi;
1792
1793 if (ht == NULL)
1794 return;
1795
1796 todo = (long)ht->ht_used;
1797 for (hi = ht->ht_array; todo > 0; ++hi)
1798 {
1799 if (!HASHITEM_EMPTY(hi))
1800 {
1801 proptype_T *prop = HI2PT(hi);
1802
1803 vim_free(prop);
1804 --todo;
1805 }
1806 }
1807
1808 hash_clear(ht);
1809 vim_free(ht);
1810}
1811
1812#if defined(EXITFREE) || defined(PROTO)
1813/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001814 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001815 */
1816 void
1817clear_global_prop_types(void)
1818{
1819 clear_ht_prop_types(global_proptypes);
1820 global_proptypes = NULL;
1821}
1822#endif
1823
1824/*
1825 * Free all property types for "buf".
1826 */
1827 void
1828clear_buf_prop_types(buf_T *buf)
1829{
1830 clear_ht_prop_types(buf->b_proptypes);
1831 buf->b_proptypes = NULL;
1832}
1833
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001834// Struct used to return two values from adjust_prop().
1835typedef struct
1836{
1837 int dirty; // if the property was changed
1838 int can_drop; // whether after this change, the prop may be removed
1839} adjustres_T;
1840
1841/*
1842 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1843 *
1844 * Note that "col" is zero-based, while tp_col is one-based.
1845 * Only for the current buffer.
1846 * "flags" can have:
1847 * APC_SUBSTITUTE: Text is replaced, not inserted.
1848 */
1849 static adjustres_T
1850adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001851 textprop_T *prop,
1852 colnr_T col,
1853 int added,
1854 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001855{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001856 proptype_T *pt;
1857 int start_incl;
1858 int end_incl;
1859 int droppable;
1860 adjustres_T res = {TRUE, FALSE};
1861
1862 // prop after end of the line doesn't move
1863 if (prop->tp_col == MAXCOL)
1864 {
1865 res.dirty = FALSE;
1866 return res;
1867 }
1868
1869 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1870 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001871 || (flags & APC_SUBSTITUTE)
1872 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001873 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001874 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001875 // do not drop zero-width props if they later can increase in size
1876 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001877
1878 if (added > 0)
1879 {
1880 if (col + 1 <= prop->tp_col
1881 - (start_incl || (prop->tp_len == 0 && end_incl)))
1882 // Change is entirely before the text property: Only shift
1883 prop->tp_col += added;
1884 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1885 // Insertion was inside text property
1886 prop->tp_len += added;
1887 }
1888 else if (prop->tp_col > col + 1)
1889 {
1890 if (prop->tp_col + added < col + 1)
1891 {
1892 prop->tp_len += (prop->tp_col - 1 - col) + added;
1893 prop->tp_col = col + 1;
1894 if (prop->tp_len <= 0)
1895 {
1896 prop->tp_len = 0;
1897 res.can_drop = droppable;
1898 }
1899 }
1900 else
1901 prop->tp_col += added;
1902 }
1903 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1904 {
1905 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1906
1907 prop->tp_len += after > 0 ? added + after : added;
1908 res.can_drop = prop->tp_len <= 0 && droppable;
1909 }
1910 else
1911 res.dirty = FALSE;
1912
1913 return res;
1914}
1915
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001916/*
1917 * Adjust the columns of text properties in line "lnum" after position "col" to
1918 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001919 * Note that "col" is zero-based, while tp_col is one-based.
1920 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001921 * "flags" can have:
1922 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1923 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001924 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001925 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001926 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001927 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001928adjust_prop_columns(
1929 linenr_T lnum,
1930 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001931 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001932 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001933{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001934 int proplen;
1935 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001936 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001937 int ri, wi;
1938 size_t textlen;
1939
1940 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001941 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001942
1943 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1944 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001945 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001946 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001947
Bram Moolenaar196d1572019-01-02 23:47:18 +01001948 wi = 0; // write index
1949 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001950 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001951 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001952 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001953
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001954 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1955 res = adjust_prop(&prop, col, bytes_added, flags);
1956 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001957 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001958 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001959 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1960 && u_savesub(lnum) == FAIL)
1961 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001962 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001963
1964 // u_savesub() may have updated curbuf->b_ml, fetch it again
1965 if (curbuf->b_ml.ml_line_lnum != lnum)
1966 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001967 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001968 if (res.can_drop)
1969 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001970 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001971 ++wi;
1972 }
1973 if (dirty)
1974 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001975 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1976
1977 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001978 {
1979 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1980
1981 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1982 vim_free(curbuf->b_ml.ml_line_ptr);
1983 curbuf->b_ml.ml_line_ptr = p;
1984 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001985 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001986 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001987 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001988 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001989}
1990
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001991/*
1992 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001993 * "lnum_props" is the line that has the properties from before the split.
1994 * "lnum_top" is the top line.
1995 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001996 * "deleted" is the number of bytes deleted.
1997 */
1998 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001999adjust_props_for_split(
2000 linenr_T lnum_props,
2001 linenr_T lnum_top,
2002 int kept,
2003 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002004{
2005 char_u *props;
2006 int count;
2007 garray_T prevprop;
2008 garray_T nextprop;
2009 int i;
2010 int skipped = kept + deleted;
2011
2012 if (!curbuf->b_has_textprop)
2013 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002014
2015 // Get the text properties from "lnum_props".
2016 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002017 ga_init2(&prevprop, sizeof(textprop_T), 10);
2018 ga_init2(&nextprop, sizeof(textprop_T), 10);
2019
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002020 // Keep the relevant ones in the first line, reducing the length if needed.
2021 // Copy the ones that include the split to the second line.
2022 // Move the ones after the split to the second line.
2023 for (i = 0; i < count; ++i)
2024 {
2025 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002026 proptype_T *pt;
2027 int start_incl, end_incl;
2028 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002029
2030 // copy the prop to an aligned structure
2031 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2032
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002033 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2034 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2035 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002036 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2037 cont_next = prop.tp_col != MAXCOL
2038 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002039
2040 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002041 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002042 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2043
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002044 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002045 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002046 if (p->tp_col + p->tp_len >= kept)
2047 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002048 if (cont_next)
2049 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002050 }
2051
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002052 // Only add the property to the next line if the length is bigger than
2053 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002054 if ((cont_next || prop.tp_col == MAXCOL)
2055 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002056 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002057 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002058
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002059 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002060 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002061 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002062 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002063 if (p->tp_col > skipped)
2064 p->tp_col -= skipped - 1;
2065 else
2066 {
2067 p->tp_len -= skipped - p->tp_col;
2068 p->tp_col = 1;
2069 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002070 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002071 if (cont_prev)
2072 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002073 }
2074 }
2075
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002076 set_text_props(lnum_top, prevprop.ga_data,
2077 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002078 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002079 set_text_props(lnum_top + 1, nextprop.ga_data,
2080 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002081 ga_clear(&nextprop);
2082}
2083
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002084/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002085 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002086 */
2087 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002088prepend_joined_props(
2089 char_u *new_props,
2090 int propcount,
2091 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002092 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002093 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002094 long col,
2095 int removed)
2096{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002097 char_u *props;
2098 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2099 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002100
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002101 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002102 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002103 textprop_T prop;
2104 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002105
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002106 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002107 if (prop.tp_col == MAXCOL && !last_line)
2108 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002109 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2110
2111 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002112 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002113
Bram Moolenaare175dc62022-08-01 22:18:50 +01002114 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002115 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2116 &prop, sizeof(prop));
2117 else
2118 {
2119 int j;
2120 int found = FALSE;
2121
2122 // Search for continuing prop.
2123 for (j = *props_remaining; j < propcount; ++j)
2124 {
2125 textprop_T op;
2126
2127 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2128 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2129 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002130 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002131 found = TRUE;
2132 op.tp_len += op.tp_col - prop.tp_col;
2133 op.tp_col = prop.tp_col;
2134 // Start/end is taken care of when deleting joined lines
2135 op.tp_flags = prop.tp_flags;
2136 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2137 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002138 }
2139 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002140 if (!found)
2141 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002142 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002143 }
2144}
2145
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002146#endif // FEAT_PROP_POPUP