blob: 2cd85b4221af598dca038ef68c96867a5f95243d [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.
624 */
625 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100626count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200627{
628 char_u *props;
629 int proplen = get_text_props(curbuf, lnum, &props, 0);
630 int result = proplen;
631 int i;
632 textprop_T prop;
633
Bram Moolenaare175dc62022-08-01 22:18:50 +0100634 for (i = 0; i < proplen; ++i)
635 {
636 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
637 // A prop is droppend when in the first line and it continues from the
638 // previous line, or when not in the last line and it is virtual text
639 // after the line.
640 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
641 || (!last_line && prop.tp_col == MAXCOL))
642 --result;
643 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200644 return result;
645}
646
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100647/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200648 * Find text property "type_id" in the visible lines of window "wp".
649 * Match "id" when it is > 0.
650 * Returns FAIL when not found.
651 */
652 int
653find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
654 linenr_T *found_lnum)
655{
656 linenr_T lnum;
657 char_u *props;
658 int count;
659 int i;
660
661 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100662 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200663 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
664 {
665 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
666 for (i = 0; i < count; ++i)
667 {
668 mch_memmove(prop, props + i * sizeof(textprop_T),
669 sizeof(textprop_T));
670 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
671 {
672 *found_lnum = lnum;
673 return OK;
674 }
675 }
676 }
677 return FAIL;
678}
679
680/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100681 * Set the text properties for line "lnum" to "props" with length "len".
682 * If "len" is zero text properties are removed, "props" is not used.
683 * Any existing text properties are dropped.
684 * Only works for the current buffer.
685 */
686 static void
687set_text_props(linenr_T lnum, char_u *props, int len)
688{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100689 char_u *text;
690 char_u *newtext;
691 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100692
693 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100694 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100695 newtext = alloc(textlen + len);
696 if (newtext == NULL)
697 return;
698 mch_memmove(newtext, text, textlen);
699 if (len > 0)
700 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100701 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100702 vim_free(curbuf->b_ml.ml_line_ptr);
703 curbuf->b_ml.ml_line_ptr = newtext;
704 curbuf->b_ml.ml_line_len = textlen + len;
705 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
706}
707
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100708/*
709 * Add "text_props" with "text_prop_count" text propertis to line "lnum".
710 */
711 void
712add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
713{
714 char_u *text;
715 char_u *newtext;
716 int proplen = text_prop_count * (int)sizeof(textprop_T);
717
718 text = ml_get(lnum);
719 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
720 if (newtext == NULL)
721 return;
722 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
723 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
724 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
725 vim_free(curbuf->b_ml.ml_line_ptr);
726 curbuf->b_ml.ml_line_ptr = newtext;
727 curbuf->b_ml.ml_line_len += proplen;
728 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
729}
730
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100731 static proptype_T *
732find_type_by_id(hashtab_T *ht, int id)
733{
734 long todo;
735 hashitem_T *hi;
736
737 if (ht == NULL)
738 return NULL;
739
740 // TODO: Make this faster by keeping a list of types sorted on ID and use
741 // a binary search.
742
743 todo = (long)ht->ht_used;
744 for (hi = ht->ht_array; todo > 0; ++hi)
745 {
746 if (!HASHITEM_EMPTY(hi))
747 {
748 proptype_T *prop = HI2PT(hi);
749
750 if (prop->pt_id == id)
751 return prop;
752 --todo;
753 }
754 }
755 return NULL;
756}
757
758/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100759 * Fill 'dict' with text properties in 'prop'.
760 */
761 static void
762prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
763{
764 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200765 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100766
767 dict_add_number(dict, "col", prop->tp_col);
768 dict_add_number(dict, "length", prop->tp_len);
769 dict_add_number(dict, "id", prop->tp_id);
770 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
771 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200772
773 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
774 if (pt == NULL)
775 {
776 pt = find_type_by_id(global_proptypes, prop->tp_type);
777 buflocal = FALSE;
778 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100779 if (pt != NULL)
780 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200781
782 if (buflocal)
783 dict_add_number(dict, "type_bufnr", buf->b_fnum);
784 else
785 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100786}
787
788/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100789 * Find a property type by ID in "buf" or globally.
790 * Returns NULL if not found.
791 */
792 proptype_T *
793text_prop_type_by_id(buf_T *buf, int id)
794{
795 proptype_T *type;
796
797 type = find_type_by_id(buf->b_proptypes, id);
798 if (type == NULL)
799 type = find_type_by_id(global_proptypes, id);
800 return type;
801}
802
803/*
804 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
805 */
806 void
807f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
808{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200809 linenr_T start;
810 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100811 linenr_T lnum;
812 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100813 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100814
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200815 if (in_vim9script()
816 && (check_for_number_arg(argvars, 0) == FAIL
817 || check_for_opt_number_arg(argvars, 1) == FAIL
818 || (argvars[1].v_type != VAR_UNKNOWN
819 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
820 return;
821
822 start = tv_get_number(&argvars[0]);
823 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100824 if (argvars[1].v_type != VAR_UNKNOWN)
825 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100826 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100827 if (argvars[2].v_type != VAR_UNKNOWN)
828 {
829 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
830 return;
831 }
832 }
833 if (start < 1 || end < 1)
834 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200835 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100836 return;
837 }
838
839 for (lnum = start; lnum <= end; ++lnum)
840 {
841 char_u *text;
842 size_t len;
843
844 if (lnum > buf->b_ml.ml_line_count)
845 break;
846 text = ml_get_buf(buf, lnum, FALSE);
847 len = STRLEN(text) + 1;
848 if ((size_t)buf->b_ml.ml_line_len > len)
849 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100850 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100851 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
852 {
853 char_u *newtext = vim_strsave(text);
854
855 // need to allocate the line now
856 if (newtext == NULL)
857 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100858 if (buf->b_ml.ml_flags & ML_ALLOCATED)
859 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100860 buf->b_ml.ml_line_ptr = newtext;
861 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
862 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100863 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100864 }
865 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100866 if (did_clear)
867 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100868}
869
870/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100871 * prop_find({props} [, {direction}])
872 */
873 void
874f_prop_find(typval_T *argvars, typval_T *rettv)
875{
876 pos_T *cursor = &curwin->w_cursor;
877 dict_T *dict;
878 buf_T *buf = curbuf;
879 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200880 int lnum_start;
881 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100882 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200883 int id = 0;
884 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200885 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100886 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200887 int lnum = -1;
888 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100889 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100890 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100891
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200892 if (in_vim9script()
893 && (check_for_dict_arg(argvars, 0) == FAIL
894 || check_for_opt_string_arg(argvars, 1) == FAIL))
895 return;
896
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100897 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
898 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000899 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100900 return;
901 }
902 dict = argvars[0].vval.v_dict;
903
904 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
905 return;
906 if (buf->b_ml.ml_mfp == NULL)
907 return;
908
909 if (argvars[1].v_type != VAR_UNKNOWN)
910 {
911 char_u *dir_s = tv_get_string(&argvars[1]);
912
913 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100914 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100915 else if (*dir_s != 'f')
916 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000917 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100918 return;
919 }
920 }
921
922 di = dict_find(dict, (char_u *)"lnum", -1);
923 if (di != NULL)
924 lnum = tv_get_number(&di->di_tv);
925
926 di = dict_find(dict, (char_u *)"col", -1);
927 if (di != NULL)
928 col = tv_get_number(&di->di_tv);
929
930 if (lnum == -1)
931 {
932 lnum = cursor->lnum;
933 col = cursor->col + 1;
934 }
935 else if (col == -1)
936 col = 1;
937
938 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
939 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200940 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100941 return;
942 }
943
Bram Moolenaard61efa52022-07-23 09:52:04 +0100944 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100945
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100946 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200947 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100948 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200949 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200950 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100951 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100952 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100953 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100954 proptype_T *type = lookup_prop_type(name, buf);
955
956 if (type == NULL)
957 return;
958 type_id = type->pt_id;
959 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100960 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200961 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100962 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000963 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100964 return;
965 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200966 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100967 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000968 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100969 return;
970 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100971
972 lnum_start = lnum;
973
974 if (rettv_dict_alloc(rettv) == FAIL)
975 return;
976
977 while (1)
978 {
979 char_u *text = ml_get_buf(buf, lnum, FALSE);
980 size_t textlen = STRLEN(text) + 1;
981 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200982 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100983 int i;
984 textprop_T prop;
985 int prop_start;
986 int prop_end;
987
LemonBoy9bd3ce22022-04-18 21:54:02 +0100988 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100989 {
990 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100991 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100992
LemonBoy9bd3ce22022-04-18 21:54:02 +0100993 // For the very first line try to find the first property before or
994 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100995 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100996 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100997 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100998 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100999 if (prop.tp_col > col)
1000 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001001 }
1002 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1003 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001004 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001005 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001006 : (id_found && prop.tp_id == id)
1007 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001008 {
1009 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001010 if (lnum_start == lnum
1011 && col >= prop.tp_col
1012 && (col <= prop.tp_col + prop.tp_len
1013 - (prop.tp_len != 0)))
1014 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001015
LemonBoy9bd3ce22022-04-18 21:54:02 +01001016 // The property was not continued from last line, it starts on
1017 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001018 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001019 // The property does not continue on the next line, it ends on
1020 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001021 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001022 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001023 seen_end = 1;
1024
1025 // Skip lines without the start flag.
1026 if (!prop_start)
1027 {
1028 // Always search backwards for start when search started
1029 // on a prop and we're not skipping.
1030 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001031 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001032 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001033 }
1034
1035 // If skipstart is true, skip the prop at start pos (even if
1036 // continued from another line).
1037 if (start_pos_has_prop && skipstart && !seen_end)
1038 {
1039 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001040 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001041 }
1042
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001043 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1044 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1045
1046 return;
1047 }
1048 }
1049
1050 if (dir > 0)
1051 {
1052 if (lnum >= buf->b_ml.ml_line_count)
1053 break;
1054 lnum++;
1055 }
1056 else
1057 {
1058 if (lnum <= 1)
1059 break;
1060 lnum--;
1061 }
1062 }
1063}
1064
1065/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001066 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1067 */
1068 static int
1069prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1070{
1071 int i;
1072
1073 for (i = 0; i < len; i++)
1074 if (types_or_ids[i] == type_or_id)
1075 return TRUE;
1076
1077 return FALSE;
1078}
1079
1080/*
1081 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1082 * If 'prop_types' is not NULL, then return only the text properties with
1083 * matching property type in the 'prop_types' array.
1084 * If 'prop_ids' is not NULL, then return only the text properties with
1085 * an identifier in the 'props_ids' array.
1086 * If 'add_lnum' is TRUE, then add the line number also to the text property
1087 * dictionary.
1088 */
1089 static void
1090get_props_in_line(
1091 buf_T *buf,
1092 linenr_T lnum,
1093 int *prop_types,
1094 int prop_types_len,
1095 int *prop_ids,
1096 int prop_ids_len,
1097 list_T *retlist,
1098 int add_lnum)
1099{
1100 char_u *text = ml_get_buf(buf, lnum, FALSE);
1101 size_t textlen = STRLEN(text) + 1;
1102 int count;
1103 int i;
1104 textprop_T prop;
1105
1106 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1107 for (i = 0; i < count; ++i)
1108 {
1109 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1110 sizeof(textprop_T));
1111 if ((prop_types == NULL
1112 || prop_type_or_id_in_list(prop_types, prop_types_len,
1113 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001114 && (prop_ids == NULL
1115 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1116 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001117 {
1118 dict_T *d = dict_alloc();
1119
1120 if (d == NULL)
1121 break;
1122 prop_fill_dict(d, &prop, buf);
1123 if (add_lnum)
1124 dict_add_number(d, "lnum", lnum);
1125 list_append_dict(retlist, d);
1126 }
1127 }
1128}
1129
1130/*
1131 * Convert a List of property type names into an array of property type
1132 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1133 * error. 'num_types' is set to the number of returned property types.
1134 */
1135 static int *
1136get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1137{
1138 int *prop_types;
1139 listitem_T *li;
1140 int i;
1141 char_u *name;
1142 proptype_T *type;
1143
1144 *num_types = 0;
1145
1146 prop_types = ALLOC_MULT(int, list_len(l));
1147 if (prop_types == NULL)
1148 return NULL;
1149
1150 i = 0;
1151 FOR_ALL_LIST_ITEMS(l, li)
1152 {
1153 if (li->li_tv.v_type != VAR_STRING)
1154 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001155 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001156 goto errret;
1157 }
1158 name = li->li_tv.vval.v_string;
1159 if (name == NULL)
1160 goto errret;
1161
1162 type = lookup_prop_type(name, buf);
1163 if (type == NULL)
1164 goto errret;
1165 prop_types[i++] = type->pt_id;
1166 }
1167
1168 *num_types = i;
1169 return prop_types;
1170
1171errret:
1172 VIM_CLEAR(prop_types);
1173 return NULL;
1174}
1175
1176/*
1177 * Convert a List of property identifiers into an array of property
1178 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1179 * error. 'num_ids' is set to the number of returned property identifiers.
1180 */
1181 static int *
1182get_prop_ids_from_list(list_T *l, int *num_ids)
1183{
1184 int *prop_ids;
1185 listitem_T *li;
1186 int i;
1187 int id;
1188 int error;
1189
1190 *num_ids = 0;
1191
1192 prop_ids = ALLOC_MULT(int, list_len(l));
1193 if (prop_ids == NULL)
1194 return NULL;
1195
1196 i = 0;
1197 FOR_ALL_LIST_ITEMS(l, li)
1198 {
1199 error = FALSE;
1200 id = tv_get_number_chk(&li->li_tv, &error);
1201 if (error)
1202 goto errret;
1203
1204 prop_ids[i++] = id;
1205 }
1206
1207 *num_ids = i;
1208 return prop_ids;
1209
1210errret:
1211 VIM_CLEAR(prop_ids);
1212 return NULL;
1213}
1214
1215/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001216 * prop_list({lnum} [, {bufnr}])
1217 */
1218 void
1219f_prop_list(typval_T *argvars, typval_T *rettv)
1220{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001221 linenr_T lnum;
1222 linenr_T start_lnum;
1223 linenr_T end_lnum;
1224 buf_T *buf = curbuf;
1225 int add_lnum = FALSE;
1226 int *prop_types = NULL;
1227 int prop_types_len = 0;
1228 int *prop_ids = NULL;
1229 int prop_ids_len = 0;
1230 list_T *l;
1231 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001232
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001233 if (in_vim9script()
1234 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001235 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001236 return;
1237
Bram Moolenaar93a10962022-06-16 11:42:09 +01001238 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001239 return;
1240
1241 // default: get text properties on current line
1242 start_lnum = tv_get_number(&argvars[0]);
1243 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001244 if (argvars[1].v_type != VAR_UNKNOWN)
1245 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001246 dict_T *d;
1247
1248 if (argvars[1].v_type != VAR_DICT)
1249 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001250 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001251 return;
1252 }
1253 d = argvars[1].vval.v_dict;
1254
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001255 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1256 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001257
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001258 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001259 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001260 if (di->di_tv.v_type != VAR_NUMBER)
1261 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001262 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001263 return;
1264 }
1265 end_lnum = tv_get_number(&di->di_tv);
1266 if (end_lnum < 0)
1267 // negative end_lnum is used as an offset from the last buffer
1268 // line
1269 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1270 else if (end_lnum > buf->b_ml.ml_line_count)
1271 end_lnum = buf->b_ml.ml_line_count;
1272 add_lnum = TRUE;
1273 }
1274 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1275 {
1276 if (di->di_tv.v_type != VAR_LIST)
1277 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001278 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001279 return;
1280 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001281
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001282 l = di->di_tv.vval.v_list;
1283 if (l != NULL && list_len(l) > 0)
1284 {
1285 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1286 if (prop_types == NULL)
1287 return;
1288 }
1289 }
1290 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1291 {
1292 if (di->di_tv.v_type != VAR_LIST)
1293 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001294 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001295 goto errret;
1296 }
1297
1298 l = di->di_tv.vval.v_list;
1299 if (l != NULL && list_len(l) > 0)
1300 {
1301 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1302 if (prop_ids == NULL)
1303 goto errret;
1304 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001305 }
1306 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001307 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1308 || end_lnum < 1 || end_lnum < start_lnum)
1309 emsg(_(e_invalid_range));
1310 else
1311 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1312 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1313 prop_ids, prop_ids_len,
1314 rettv->vval.v_list, add_lnum);
1315
1316errret:
1317 VIM_CLEAR(prop_types);
1318 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001319}
1320
1321/*
1322 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1323 */
1324 void
1325f_prop_remove(typval_T *argvars, typval_T *rettv)
1326{
1327 linenr_T start = 1;
1328 linenr_T end = 0;
1329 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001330 linenr_T first_changed = 0;
1331 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001332 dict_T *dict;
1333 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001334 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001335 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001336 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001337 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001338 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001339
1340 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001341
1342 if (in_vim9script()
1343 && (check_for_dict_arg(argvars, 0) == FAIL
1344 || check_for_opt_number_arg(argvars, 1) == FAIL
1345 || (argvars[1].v_type != VAR_UNKNOWN
1346 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1347 return;
1348
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001349 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1350 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001351 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001352 return;
1353 }
1354
1355 if (argvars[1].v_type != VAR_UNKNOWN)
1356 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001357 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001358 end = start;
1359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001360 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001361 if (start < 1 || end < 1)
1362 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001363 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001364 return;
1365 }
1366 }
1367
1368 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001369 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1370 return;
1371 if (buf->b_ml.ml_mfp == NULL)
1372 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001373
Bram Moolenaard61efa52022-07-23 09:52:04 +01001374 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001375
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001376 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001377 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001378 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001379 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001380 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001381 proptype_T *type = lookup_prop_type(name, buf);
1382
1383 if (type == NULL)
1384 return;
1385 type_id = type->pt_id;
1386 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001387 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001388
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001389 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001390 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001391 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392 return;
1393 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001394 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001395 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001396 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001397 return;
1398 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001399
1400 if (end == 0)
1401 end = buf->b_ml.ml_line_count;
1402 for (lnum = start; lnum <= end; ++lnum)
1403 {
1404 char_u *text;
1405 size_t len;
1406
1407 if (lnum > buf->b_ml.ml_line_count)
1408 break;
1409 text = ml_get_buf(buf, lnum, FALSE);
1410 len = STRLEN(text) + 1;
1411 if ((size_t)buf->b_ml.ml_line_len > len)
1412 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001413 static textprop_T textprop; // static because of alignment
1414 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001415
1416 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1417 / sizeof(textprop_T); ++idx)
1418 {
1419 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1420 + idx * sizeof(textprop_T);
1421 size_t taillen;
1422
1423 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001424 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1425 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001426 {
1427 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1428 {
1429 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1430
1431 // need to allocate the line to be able to change it
1432 if (newptr == NULL)
1433 return;
1434 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1435 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001436 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1437 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001438 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001439 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1440
1441 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001442 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001443 }
1444
1445 taillen = buf->b_ml.ml_line_len - len
1446 - (idx + 1) * sizeof(textprop_T);
1447 if (taillen > 0)
1448 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1449 taillen);
1450 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1451 --idx;
1452
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001453 if (textprop.tp_id < 0)
1454 {
1455 garray_T *gap = &buf->b_textprop_text;
1456 int ii = -textprop.tp_id - 1;
1457
1458 // negative ID: property with text - free the text
1459 if (ii < gap->ga_len)
1460 {
1461 char_u **p = ((char_u **)gap->ga_data) + ii;
1462 vim_free(*p);
1463 *p = NULL;
1464 did_remove_text = TRUE;
1465 }
1466 }
1467
Bram Moolenaar965c0442021-05-17 00:22:06 +02001468 if (first_changed == 0)
1469 first_changed = lnum;
1470 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001471 ++rettv->vval.v_number;
1472 if (!do_all)
1473 break;
1474 }
1475 }
1476 }
1477 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001478
Bram Moolenaar965c0442021-05-17 00:22:06 +02001479 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001480 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001481 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1482 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001483 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001484
1485 if (did_remove_text)
1486 {
1487 garray_T *gap = &buf->b_textprop_text;
1488
1489 // Reduce the growarray size for NULL pointers at the end.
1490 while (gap->ga_len > 0
1491 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1492 --gap->ga_len;
1493 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494}
1495
1496/*
1497 * Common for f_prop_type_add() and f_prop_type_change().
1498 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001499 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001500prop_type_set(typval_T *argvars, int add)
1501{
1502 char_u *name;
1503 buf_T *buf = NULL;
1504 dict_T *dict;
1505 dictitem_T *di;
1506 proptype_T *prop;
1507
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001508 if (in_vim9script()
1509 && (check_for_string_arg(argvars, 0) == FAIL
1510 || check_for_dict_arg(argvars, 1) == FAIL))
1511 return;
1512
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001513 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001514 if (*name == NUL)
1515 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001516 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001517 return;
1518 }
1519
1520 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1521 return;
1522 dict = argvars[1].vval.v_dict;
1523
1524 prop = find_prop(name, buf);
1525 if (add)
1526 {
1527 hashtab_T **htp;
1528
1529 if (prop != NULL)
1530 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001531 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001532 return;
1533 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001534 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001535 if (prop == NULL)
1536 return;
1537 STRCPY(prop->pt_name, name);
1538 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001539 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001540 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1541 if (*htp == NULL)
1542 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001543 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001544 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001545 {
1546 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001547 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001548 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001549 hash_init(*htp);
1550 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001551 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001552 }
1553 else
1554 {
1555 if (prop == NULL)
1556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001557 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001558 return;
1559 }
1560 }
1561
1562 if (dict != NULL)
1563 {
1564 di = dict_find(dict, (char_u *)"highlight", -1);
1565 if (di != NULL)
1566 {
1567 char_u *highlight;
1568 int hl_id = 0;
1569
Bram Moolenaard61efa52022-07-23 09:52:04 +01001570 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001571 if (highlight != NULL && *highlight != NUL)
1572 hl_id = syn_name2id(highlight);
1573 if (hl_id <= 0)
1574 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001575 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001576 highlight == NULL ? (char_u *)"" : highlight);
1577 return;
1578 }
1579 prop->pt_hl_id = hl_id;
1580 }
1581
Bram Moolenaar58187f12019-05-05 16:33:47 +02001582 di = dict_find(dict, (char_u *)"combine", -1);
1583 if (di != NULL)
1584 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001585 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001586 prop->pt_flags |= PT_FLAG_COMBINE;
1587 else
1588 prop->pt_flags &= ~PT_FLAG_COMBINE;
1589 }
1590
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001591 di = dict_find(dict, (char_u *)"override", -1);
1592 if (di != NULL)
1593 {
1594 if (tv_get_bool(&di->di_tv))
1595 prop->pt_flags |= PT_FLAG_OVERRIDE;
1596 else
1597 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1598 }
1599
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001600 di = dict_find(dict, (char_u *)"priority", -1);
1601 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001602 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001603
1604 di = dict_find(dict, (char_u *)"start_incl", -1);
1605 if (di != NULL)
1606 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001607 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001608 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1609 else
1610 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1611 }
1612
1613 di = dict_find(dict, (char_u *)"end_incl", -1);
1614 if (di != NULL)
1615 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001616 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001617 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1618 else
1619 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1620 }
1621 }
1622}
1623
1624/*
1625 * prop_type_add({name}, {props})
1626 */
1627 void
1628f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1629{
1630 prop_type_set(argvars, TRUE);
1631}
1632
1633/*
1634 * prop_type_change({name}, {props})
1635 */
1636 void
1637f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1638{
1639 prop_type_set(argvars, FALSE);
1640}
1641
1642/*
1643 * prop_type_delete({name} [, {bufnr}])
1644 */
1645 void
1646f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1647{
1648 char_u *name;
1649 buf_T *buf = NULL;
1650 hashitem_T *hi;
1651
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001652 if (in_vim9script()
1653 && (check_for_string_arg(argvars, 0) == FAIL
1654 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1655 return;
1656
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001657 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001658 if (*name == NUL)
1659 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001660 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001661 return;
1662 }
1663
1664 if (argvars[1].v_type != VAR_UNKNOWN)
1665 {
1666 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1667 return;
1668 }
1669
1670 hi = find_prop_hi(name, buf);
1671 if (hi != NULL)
1672 {
1673 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001674 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001675
1676 if (buf == NULL)
1677 ht = global_proptypes;
1678 else
1679 ht = buf->b_proptypes;
1680 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001681 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001682 }
1683}
1684
1685/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001686 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001687 */
1688 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001689f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001690{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001691 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001692
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001693 if (in_vim9script()
1694 && (check_for_string_arg(argvars, 0) == FAIL
1695 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1696 return;
1697
1698 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001699 if (*name == NUL)
1700 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001701 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001702 return;
1703 }
1704 if (rettv_dict_alloc(rettv) == OK)
1705 {
1706 proptype_T *prop = NULL;
1707 buf_T *buf = NULL;
1708
1709 if (argvars[1].v_type != VAR_UNKNOWN)
1710 {
1711 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1712 return;
1713 }
1714
1715 prop = find_prop(name, buf);
1716 if (prop != NULL)
1717 {
1718 dict_T *d = rettv->vval.v_dict;
1719
1720 if (prop->pt_hl_id > 0)
1721 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1722 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001723 dict_add_number(d, "combine",
1724 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001725 dict_add_number(d, "start_incl",
1726 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1727 dict_add_number(d, "end_incl",
1728 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1729 if (buf != NULL)
1730 dict_add_number(d, "bufnr", buf->b_fnum);
1731 }
1732 }
1733}
1734
1735 static void
1736list_types(hashtab_T *ht, list_T *l)
1737{
1738 long todo;
1739 hashitem_T *hi;
1740
1741 todo = (long)ht->ht_used;
1742 for (hi = ht->ht_array; todo > 0; ++hi)
1743 {
1744 if (!HASHITEM_EMPTY(hi))
1745 {
1746 proptype_T *prop = HI2PT(hi);
1747
1748 list_append_string(l, prop->pt_name, -1);
1749 --todo;
1750 }
1751 }
1752}
1753
1754/*
1755 * prop_type_list([{bufnr}])
1756 */
1757 void
1758f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1759{
1760 buf_T *buf = NULL;
1761
1762 if (rettv_list_alloc(rettv) == OK)
1763 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001764 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1765 return;
1766
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001767 if (argvars[0].v_type != VAR_UNKNOWN)
1768 {
1769 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1770 return;
1771 }
1772 if (buf == NULL)
1773 {
1774 if (global_proptypes != NULL)
1775 list_types(global_proptypes, rettv->vval.v_list);
1776 }
1777 else if (buf->b_proptypes != NULL)
1778 list_types(buf->b_proptypes, rettv->vval.v_list);
1779 }
1780}
1781
1782/*
1783 * Free all property types in "ht".
1784 */
1785 static void
1786clear_ht_prop_types(hashtab_T *ht)
1787{
1788 long todo;
1789 hashitem_T *hi;
1790
1791 if (ht == NULL)
1792 return;
1793
1794 todo = (long)ht->ht_used;
1795 for (hi = ht->ht_array; todo > 0; ++hi)
1796 {
1797 if (!HASHITEM_EMPTY(hi))
1798 {
1799 proptype_T *prop = HI2PT(hi);
1800
1801 vim_free(prop);
1802 --todo;
1803 }
1804 }
1805
1806 hash_clear(ht);
1807 vim_free(ht);
1808}
1809
1810#if defined(EXITFREE) || defined(PROTO)
1811/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001812 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001813 */
1814 void
1815clear_global_prop_types(void)
1816{
1817 clear_ht_prop_types(global_proptypes);
1818 global_proptypes = NULL;
1819}
1820#endif
1821
1822/*
1823 * Free all property types for "buf".
1824 */
1825 void
1826clear_buf_prop_types(buf_T *buf)
1827{
1828 clear_ht_prop_types(buf->b_proptypes);
1829 buf->b_proptypes = NULL;
1830}
1831
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001832// Struct used to return two values from adjust_prop().
1833typedef struct
1834{
1835 int dirty; // if the property was changed
1836 int can_drop; // whether after this change, the prop may be removed
1837} adjustres_T;
1838
1839/*
1840 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1841 *
1842 * Note that "col" is zero-based, while tp_col is one-based.
1843 * Only for the current buffer.
1844 * "flags" can have:
1845 * APC_SUBSTITUTE: Text is replaced, not inserted.
1846 */
1847 static adjustres_T
1848adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001849 textprop_T *prop,
1850 colnr_T col,
1851 int added,
1852 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001853{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001854 proptype_T *pt;
1855 int start_incl;
1856 int end_incl;
1857 int droppable;
1858 adjustres_T res = {TRUE, FALSE};
1859
1860 // prop after end of the line doesn't move
1861 if (prop->tp_col == MAXCOL)
1862 {
1863 res.dirty = FALSE;
1864 return res;
1865 }
1866
1867 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1868 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001869 || (flags & APC_SUBSTITUTE)
1870 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001871 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001872 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001873 // do not drop zero-width props if they later can increase in size
1874 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001875
1876 if (added > 0)
1877 {
1878 if (col + 1 <= prop->tp_col
1879 - (start_incl || (prop->tp_len == 0 && end_incl)))
1880 // Change is entirely before the text property: Only shift
1881 prop->tp_col += added;
1882 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1883 // Insertion was inside text property
1884 prop->tp_len += added;
1885 }
1886 else if (prop->tp_col > col + 1)
1887 {
1888 if (prop->tp_col + added < col + 1)
1889 {
1890 prop->tp_len += (prop->tp_col - 1 - col) + added;
1891 prop->tp_col = col + 1;
1892 if (prop->tp_len <= 0)
1893 {
1894 prop->tp_len = 0;
1895 res.can_drop = droppable;
1896 }
1897 }
1898 else
1899 prop->tp_col += added;
1900 }
1901 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1902 {
1903 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1904
1905 prop->tp_len += after > 0 ? added + after : added;
1906 res.can_drop = prop->tp_len <= 0 && droppable;
1907 }
1908 else
1909 res.dirty = FALSE;
1910
1911 return res;
1912}
1913
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001914/*
1915 * Adjust the columns of text properties in line "lnum" after position "col" to
1916 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001917 * Note that "col" is zero-based, while tp_col is one-based.
1918 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001919 * "flags" can have:
1920 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1921 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001922 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001923 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001924 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001925 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001926adjust_prop_columns(
1927 linenr_T lnum,
1928 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001929 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001930 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001931{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001932 int proplen;
1933 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001934 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001935 int ri, wi;
1936 size_t textlen;
1937
1938 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001939 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001940
1941 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1942 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001943 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001944 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001945
Bram Moolenaar196d1572019-01-02 23:47:18 +01001946 wi = 0; // write index
1947 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001948 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001949 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001950 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001951
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001952 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1953 res = adjust_prop(&prop, col, bytes_added, flags);
1954 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001955 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001956 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001957 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1958 && u_savesub(lnum) == FAIL)
1959 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001960 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001961
1962 // u_savesub() may have updated curbuf->b_ml, fetch it again
1963 if (curbuf->b_ml.ml_line_lnum != lnum)
1964 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001965 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001966 if (res.can_drop)
1967 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001968 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001969 ++wi;
1970 }
1971 if (dirty)
1972 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001973 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1974
1975 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001976 {
1977 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1978
1979 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1980 vim_free(curbuf->b_ml.ml_line_ptr);
1981 curbuf->b_ml.ml_line_ptr = p;
1982 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001983 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001984 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001985 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001986 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001987}
1988
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001989/*
1990 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001991 * "lnum_props" is the line that has the properties from before the split.
1992 * "lnum_top" is the top line.
1993 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001994 * "deleted" is the number of bytes deleted.
1995 */
1996 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001997adjust_props_for_split(
1998 linenr_T lnum_props,
1999 linenr_T lnum_top,
2000 int kept,
2001 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002002{
2003 char_u *props;
2004 int count;
2005 garray_T prevprop;
2006 garray_T nextprop;
2007 int i;
2008 int skipped = kept + deleted;
2009
2010 if (!curbuf->b_has_textprop)
2011 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002012
2013 // Get the text properties from "lnum_props".
2014 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002015 ga_init2(&prevprop, sizeof(textprop_T), 10);
2016 ga_init2(&nextprop, sizeof(textprop_T), 10);
2017
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002018 // Keep the relevant ones in the first line, reducing the length if needed.
2019 // Copy the ones that include the split to the second line.
2020 // Move the ones after the split to the second line.
2021 for (i = 0; i < count; ++i)
2022 {
2023 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002024 proptype_T *pt;
2025 int start_incl, end_incl;
2026 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002027
2028 // copy the prop to an aligned structure
2029 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2030
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002031 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2032 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2033 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
2034 cont_prev = prop.tp_col + !start_incl <= kept;
2035 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
2036
2037 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002038 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002039 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2040
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002041 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002042 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002043 if (p->tp_col + p->tp_len >= kept)
2044 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002045 if (cont_next)
2046 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002047 }
2048
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002049 // Only add the property to the next line if the length is bigger than
2050 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002051 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002052 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002053 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002054 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002055 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002056 if (p->tp_col > skipped)
2057 p->tp_col -= skipped - 1;
2058 else
2059 {
2060 p->tp_len -= skipped - p->tp_col;
2061 p->tp_col = 1;
2062 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002063 if (cont_prev)
2064 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002065 }
2066 }
2067
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002068 set_text_props(lnum_top, prevprop.ga_data,
2069 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002070 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002071 set_text_props(lnum_top + 1, nextprop.ga_data,
2072 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002073 ga_clear(&nextprop);
2074}
2075
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002076/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002077 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002078 */
2079 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002080prepend_joined_props(
2081 char_u *new_props,
2082 int propcount,
2083 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002084 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002085 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002086 long col,
2087 int removed)
2088{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002089 char_u *props;
2090 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2091 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002092
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002093 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002094 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002095 textprop_T prop;
2096 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002097
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002098 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002099 if (prop.tp_col == MAXCOL && !last_line)
2100 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002101 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2102
2103 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002104 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002105
Bram Moolenaare175dc62022-08-01 22:18:50 +01002106 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002107 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2108 &prop, sizeof(prop));
2109 else
2110 {
2111 int j;
2112 int found = FALSE;
2113
2114 // Search for continuing prop.
2115 for (j = *props_remaining; j < propcount; ++j)
2116 {
2117 textprop_T op;
2118
2119 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2120 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2121 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002122 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002123 found = TRUE;
2124 op.tp_len += op.tp_col - prop.tp_col;
2125 op.tp_col = prop.tp_col;
2126 // Start/end is taken care of when deleting joined lines
2127 op.tp_flags = prop.tp_flags;
2128 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2129 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002130 }
2131 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002132 if (!found)
2133 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002134 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002135 }
2136}
2137
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002138#endif // FEAT_PROP_POPUP