blob: 9e643f718544268d8692a8591b0f484114cc507c [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 Moolenaar87be9be2020-05-30 15:32:02 +0200597/**
598 * Return the number of text properties on line "lnum" in the current buffer.
599 * When "only_starting" is true only text properties starting in this line will
600 * be considered.
601 */
602 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100603count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200604{
605 char_u *props;
606 int proplen = get_text_props(curbuf, lnum, &props, 0);
607 int result = proplen;
608 int i;
609 textprop_T prop;
610
Bram Moolenaare175dc62022-08-01 22:18:50 +0100611 for (i = 0; i < proplen; ++i)
612 {
613 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
614 // A prop is droppend when in the first line and it continues from the
615 // previous line, or when not in the last line and it is virtual text
616 // after the line.
617 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
618 || (!last_line && prop.tp_col == MAXCOL))
619 --result;
620 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200621 return result;
622}
623
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100624/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200625 * Find text property "type_id" in the visible lines of window "wp".
626 * Match "id" when it is > 0.
627 * Returns FAIL when not found.
628 */
629 int
630find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
631 linenr_T *found_lnum)
632{
633 linenr_T lnum;
634 char_u *props;
635 int count;
636 int i;
637
638 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100639 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200640 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
641 {
642 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
643 for (i = 0; i < count; ++i)
644 {
645 mch_memmove(prop, props + i * sizeof(textprop_T),
646 sizeof(textprop_T));
647 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
648 {
649 *found_lnum = lnum;
650 return OK;
651 }
652 }
653 }
654 return FAIL;
655}
656
657/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100658 * Set the text properties for line "lnum" to "props" with length "len".
659 * If "len" is zero text properties are removed, "props" is not used.
660 * Any existing text properties are dropped.
661 * Only works for the current buffer.
662 */
663 static void
664set_text_props(linenr_T lnum, char_u *props, int len)
665{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100666 char_u *text;
667 char_u *newtext;
668 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100669
670 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100671 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100672 newtext = alloc(textlen + len);
673 if (newtext == NULL)
674 return;
675 mch_memmove(newtext, text, textlen);
676 if (len > 0)
677 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100678 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100679 vim_free(curbuf->b_ml.ml_line_ptr);
680 curbuf->b_ml.ml_line_ptr = newtext;
681 curbuf->b_ml.ml_line_len = textlen + len;
682 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
683}
684
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100685/*
686 * Add "text_props" with "text_prop_count" text propertis to line "lnum".
687 */
688 void
689add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
690{
691 char_u *text;
692 char_u *newtext;
693 int proplen = text_prop_count * (int)sizeof(textprop_T);
694
695 text = ml_get(lnum);
696 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
697 if (newtext == NULL)
698 return;
699 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
700 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
701 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
702 vim_free(curbuf->b_ml.ml_line_ptr);
703 curbuf->b_ml.ml_line_ptr = newtext;
704 curbuf->b_ml.ml_line_len += proplen;
705 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
706}
707
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100708 static proptype_T *
709find_type_by_id(hashtab_T *ht, int id)
710{
711 long todo;
712 hashitem_T *hi;
713
714 if (ht == NULL)
715 return NULL;
716
717 // TODO: Make this faster by keeping a list of types sorted on ID and use
718 // a binary search.
719
720 todo = (long)ht->ht_used;
721 for (hi = ht->ht_array; todo > 0; ++hi)
722 {
723 if (!HASHITEM_EMPTY(hi))
724 {
725 proptype_T *prop = HI2PT(hi);
726
727 if (prop->pt_id == id)
728 return prop;
729 --todo;
730 }
731 }
732 return NULL;
733}
734
735/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100736 * Fill 'dict' with text properties in 'prop'.
737 */
738 static void
739prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
740{
741 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200742 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100743
744 dict_add_number(dict, "col", prop->tp_col);
745 dict_add_number(dict, "length", prop->tp_len);
746 dict_add_number(dict, "id", prop->tp_id);
747 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
748 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200749
750 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
751 if (pt == NULL)
752 {
753 pt = find_type_by_id(global_proptypes, prop->tp_type);
754 buflocal = FALSE;
755 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100756 if (pt != NULL)
757 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200758
759 if (buflocal)
760 dict_add_number(dict, "type_bufnr", buf->b_fnum);
761 else
762 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100763}
764
765/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100766 * Find a property type by ID in "buf" or globally.
767 * Returns NULL if not found.
768 */
769 proptype_T *
770text_prop_type_by_id(buf_T *buf, int id)
771{
772 proptype_T *type;
773
774 type = find_type_by_id(buf->b_proptypes, id);
775 if (type == NULL)
776 type = find_type_by_id(global_proptypes, id);
777 return type;
778}
779
780/*
781 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
782 */
783 void
784f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
785{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200786 linenr_T start;
787 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100788 linenr_T lnum;
789 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100790 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100791
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200792 if (in_vim9script()
793 && (check_for_number_arg(argvars, 0) == FAIL
794 || check_for_opt_number_arg(argvars, 1) == FAIL
795 || (argvars[1].v_type != VAR_UNKNOWN
796 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
797 return;
798
799 start = tv_get_number(&argvars[0]);
800 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100801 if (argvars[1].v_type != VAR_UNKNOWN)
802 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100803 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100804 if (argvars[2].v_type != VAR_UNKNOWN)
805 {
806 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
807 return;
808 }
809 }
810 if (start < 1 || end < 1)
811 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200812 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100813 return;
814 }
815
816 for (lnum = start; lnum <= end; ++lnum)
817 {
818 char_u *text;
819 size_t len;
820
821 if (lnum > buf->b_ml.ml_line_count)
822 break;
823 text = ml_get_buf(buf, lnum, FALSE);
824 len = STRLEN(text) + 1;
825 if ((size_t)buf->b_ml.ml_line_len > len)
826 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100827 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100828 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
829 {
830 char_u *newtext = vim_strsave(text);
831
832 // need to allocate the line now
833 if (newtext == NULL)
834 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100835 if (buf->b_ml.ml_flags & ML_ALLOCATED)
836 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100837 buf->b_ml.ml_line_ptr = newtext;
838 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
839 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100840 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100841 }
842 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100843 if (did_clear)
844 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100845}
846
847/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100848 * prop_find({props} [, {direction}])
849 */
850 void
851f_prop_find(typval_T *argvars, typval_T *rettv)
852{
853 pos_T *cursor = &curwin->w_cursor;
854 dict_T *dict;
855 buf_T *buf = curbuf;
856 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200857 int lnum_start;
858 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100859 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200860 int id = 0;
861 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200862 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100863 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200864 int lnum = -1;
865 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100866 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100867 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100868
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200869 if (in_vim9script()
870 && (check_for_dict_arg(argvars, 0) == FAIL
871 || check_for_opt_string_arg(argvars, 1) == FAIL))
872 return;
873
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100874 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
875 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000876 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100877 return;
878 }
879 dict = argvars[0].vval.v_dict;
880
881 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
882 return;
883 if (buf->b_ml.ml_mfp == NULL)
884 return;
885
886 if (argvars[1].v_type != VAR_UNKNOWN)
887 {
888 char_u *dir_s = tv_get_string(&argvars[1]);
889
890 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100891 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100892 else if (*dir_s != 'f')
893 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000894 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100895 return;
896 }
897 }
898
899 di = dict_find(dict, (char_u *)"lnum", -1);
900 if (di != NULL)
901 lnum = tv_get_number(&di->di_tv);
902
903 di = dict_find(dict, (char_u *)"col", -1);
904 if (di != NULL)
905 col = tv_get_number(&di->di_tv);
906
907 if (lnum == -1)
908 {
909 lnum = cursor->lnum;
910 col = cursor->col + 1;
911 }
912 else if (col == -1)
913 col = 1;
914
915 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
916 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200917 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100918 return;
919 }
920
Bram Moolenaard61efa52022-07-23 09:52:04 +0100921 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100922
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100923 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200924 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100925 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200926 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200927 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100928 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100929 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100930 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100931 proptype_T *type = lookup_prop_type(name, buf);
932
933 if (type == NULL)
934 return;
935 type_id = type->pt_id;
936 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100937 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200938 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100939 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000940 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100941 return;
942 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200943 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100944 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000945 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100946 return;
947 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100948
949 lnum_start = lnum;
950
951 if (rettv_dict_alloc(rettv) == FAIL)
952 return;
953
954 while (1)
955 {
956 char_u *text = ml_get_buf(buf, lnum, FALSE);
957 size_t textlen = STRLEN(text) + 1;
958 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200959 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100960 int i;
961 textprop_T prop;
962 int prop_start;
963 int prop_end;
964
LemonBoy9bd3ce22022-04-18 21:54:02 +0100965 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100966 {
967 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100968 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100969
LemonBoy9bd3ce22022-04-18 21:54:02 +0100970 // For the very first line try to find the first property before or
971 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100972 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100973 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100974 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100975 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100976 if (prop.tp_col > col)
977 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100978 }
979 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
980 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100981 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100982 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200983 : (id_found && prop.tp_id == id)
984 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100985 {
986 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100987 if (lnum_start == lnum
988 && col >= prop.tp_col
989 && (col <= prop.tp_col + prop.tp_len
990 - (prop.tp_len != 0)))
991 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100992
LemonBoy9bd3ce22022-04-18 21:54:02 +0100993 // The property was not continued from last line, it starts on
994 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100995 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100996 // The property does not continue on the next line, it ends on
997 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100998 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100999 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001000 seen_end = 1;
1001
1002 // Skip lines without the start flag.
1003 if (!prop_start)
1004 {
1005 // Always search backwards for start when search started
1006 // on a prop and we're not skipping.
1007 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001008 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001009 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001010 }
1011
1012 // If skipstart is true, skip the prop at start pos (even if
1013 // continued from another line).
1014 if (start_pos_has_prop && skipstart && !seen_end)
1015 {
1016 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001017 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001018 }
1019
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001020 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1021 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1022
1023 return;
1024 }
1025 }
1026
1027 if (dir > 0)
1028 {
1029 if (lnum >= buf->b_ml.ml_line_count)
1030 break;
1031 lnum++;
1032 }
1033 else
1034 {
1035 if (lnum <= 1)
1036 break;
1037 lnum--;
1038 }
1039 }
1040}
1041
1042/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001043 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1044 */
1045 static int
1046prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1047{
1048 int i;
1049
1050 for (i = 0; i < len; i++)
1051 if (types_or_ids[i] == type_or_id)
1052 return TRUE;
1053
1054 return FALSE;
1055}
1056
1057/*
1058 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1059 * If 'prop_types' is not NULL, then return only the text properties with
1060 * matching property type in the 'prop_types' array.
1061 * If 'prop_ids' is not NULL, then return only the text properties with
1062 * an identifier in the 'props_ids' array.
1063 * If 'add_lnum' is TRUE, then add the line number also to the text property
1064 * dictionary.
1065 */
1066 static void
1067get_props_in_line(
1068 buf_T *buf,
1069 linenr_T lnum,
1070 int *prop_types,
1071 int prop_types_len,
1072 int *prop_ids,
1073 int prop_ids_len,
1074 list_T *retlist,
1075 int add_lnum)
1076{
1077 char_u *text = ml_get_buf(buf, lnum, FALSE);
1078 size_t textlen = STRLEN(text) + 1;
1079 int count;
1080 int i;
1081 textprop_T prop;
1082
1083 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1084 for (i = 0; i < count; ++i)
1085 {
1086 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1087 sizeof(textprop_T));
1088 if ((prop_types == NULL
1089 || prop_type_or_id_in_list(prop_types, prop_types_len,
1090 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001091 && (prop_ids == NULL
1092 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1093 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001094 {
1095 dict_T *d = dict_alloc();
1096
1097 if (d == NULL)
1098 break;
1099 prop_fill_dict(d, &prop, buf);
1100 if (add_lnum)
1101 dict_add_number(d, "lnum", lnum);
1102 list_append_dict(retlist, d);
1103 }
1104 }
1105}
1106
1107/*
1108 * Convert a List of property type names into an array of property type
1109 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1110 * error. 'num_types' is set to the number of returned property types.
1111 */
1112 static int *
1113get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1114{
1115 int *prop_types;
1116 listitem_T *li;
1117 int i;
1118 char_u *name;
1119 proptype_T *type;
1120
1121 *num_types = 0;
1122
1123 prop_types = ALLOC_MULT(int, list_len(l));
1124 if (prop_types == NULL)
1125 return NULL;
1126
1127 i = 0;
1128 FOR_ALL_LIST_ITEMS(l, li)
1129 {
1130 if (li->li_tv.v_type != VAR_STRING)
1131 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001132 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001133 goto errret;
1134 }
1135 name = li->li_tv.vval.v_string;
1136 if (name == NULL)
1137 goto errret;
1138
1139 type = lookup_prop_type(name, buf);
1140 if (type == NULL)
1141 goto errret;
1142 prop_types[i++] = type->pt_id;
1143 }
1144
1145 *num_types = i;
1146 return prop_types;
1147
1148errret:
1149 VIM_CLEAR(prop_types);
1150 return NULL;
1151}
1152
1153/*
1154 * Convert a List of property identifiers into an array of property
1155 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1156 * error. 'num_ids' is set to the number of returned property identifiers.
1157 */
1158 static int *
1159get_prop_ids_from_list(list_T *l, int *num_ids)
1160{
1161 int *prop_ids;
1162 listitem_T *li;
1163 int i;
1164 int id;
1165 int error;
1166
1167 *num_ids = 0;
1168
1169 prop_ids = ALLOC_MULT(int, list_len(l));
1170 if (prop_ids == NULL)
1171 return NULL;
1172
1173 i = 0;
1174 FOR_ALL_LIST_ITEMS(l, li)
1175 {
1176 error = FALSE;
1177 id = tv_get_number_chk(&li->li_tv, &error);
1178 if (error)
1179 goto errret;
1180
1181 prop_ids[i++] = id;
1182 }
1183
1184 *num_ids = i;
1185 return prop_ids;
1186
1187errret:
1188 VIM_CLEAR(prop_ids);
1189 return NULL;
1190}
1191
1192/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001193 * prop_list({lnum} [, {bufnr}])
1194 */
1195 void
1196f_prop_list(typval_T *argvars, typval_T *rettv)
1197{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001198 linenr_T lnum;
1199 linenr_T start_lnum;
1200 linenr_T end_lnum;
1201 buf_T *buf = curbuf;
1202 int add_lnum = FALSE;
1203 int *prop_types = NULL;
1204 int prop_types_len = 0;
1205 int *prop_ids = NULL;
1206 int prop_ids_len = 0;
1207 list_T *l;
1208 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001209
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001210 if (in_vim9script()
1211 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001212 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001213 return;
1214
Bram Moolenaar93a10962022-06-16 11:42:09 +01001215 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001216 return;
1217
1218 // default: get text properties on current line
1219 start_lnum = tv_get_number(&argvars[0]);
1220 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001221 if (argvars[1].v_type != VAR_UNKNOWN)
1222 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001223 dict_T *d;
1224
1225 if (argvars[1].v_type != VAR_DICT)
1226 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001227 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001228 return;
1229 }
1230 d = argvars[1].vval.v_dict;
1231
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001232 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1233 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001234
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001235 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001236 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001237 if (di->di_tv.v_type != VAR_NUMBER)
1238 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001239 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001240 return;
1241 }
1242 end_lnum = tv_get_number(&di->di_tv);
1243 if (end_lnum < 0)
1244 // negative end_lnum is used as an offset from the last buffer
1245 // line
1246 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1247 else if (end_lnum > buf->b_ml.ml_line_count)
1248 end_lnum = buf->b_ml.ml_line_count;
1249 add_lnum = TRUE;
1250 }
1251 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1252 {
1253 if (di->di_tv.v_type != VAR_LIST)
1254 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001255 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001256 return;
1257 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001258
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001259 l = di->di_tv.vval.v_list;
1260 if (l != NULL && list_len(l) > 0)
1261 {
1262 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1263 if (prop_types == NULL)
1264 return;
1265 }
1266 }
1267 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1268 {
1269 if (di->di_tv.v_type != VAR_LIST)
1270 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001271 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001272 goto errret;
1273 }
1274
1275 l = di->di_tv.vval.v_list;
1276 if (l != NULL && list_len(l) > 0)
1277 {
1278 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1279 if (prop_ids == NULL)
1280 goto errret;
1281 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001282 }
1283 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001284 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1285 || end_lnum < 1 || end_lnum < start_lnum)
1286 emsg(_(e_invalid_range));
1287 else
1288 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1289 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1290 prop_ids, prop_ids_len,
1291 rettv->vval.v_list, add_lnum);
1292
1293errret:
1294 VIM_CLEAR(prop_types);
1295 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001296}
1297
1298/*
1299 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1300 */
1301 void
1302f_prop_remove(typval_T *argvars, typval_T *rettv)
1303{
1304 linenr_T start = 1;
1305 linenr_T end = 0;
1306 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001307 linenr_T first_changed = 0;
1308 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001309 dict_T *dict;
1310 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001311 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001312 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001313 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001314 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001315 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001316
1317 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001318
1319 if (in_vim9script()
1320 && (check_for_dict_arg(argvars, 0) == FAIL
1321 || check_for_opt_number_arg(argvars, 1) == FAIL
1322 || (argvars[1].v_type != VAR_UNKNOWN
1323 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1324 return;
1325
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001326 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1327 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001328 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001329 return;
1330 }
1331
1332 if (argvars[1].v_type != VAR_UNKNOWN)
1333 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001334 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001335 end = start;
1336 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001337 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001338 if (start < 1 || end < 1)
1339 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001340 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001341 return;
1342 }
1343 }
1344
1345 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001346 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1347 return;
1348 if (buf->b_ml.ml_mfp == NULL)
1349 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001350
Bram Moolenaard61efa52022-07-23 09:52:04 +01001351 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001352
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001353 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001354 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001355 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001356 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001357 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001358 proptype_T *type = lookup_prop_type(name, buf);
1359
1360 if (type == NULL)
1361 return;
1362 type_id = type->pt_id;
1363 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001364 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001365
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001366 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001367 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001368 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001369 return;
1370 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001371 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001372 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001373 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001374 return;
1375 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001376
1377 if (end == 0)
1378 end = buf->b_ml.ml_line_count;
1379 for (lnum = start; lnum <= end; ++lnum)
1380 {
1381 char_u *text;
1382 size_t len;
1383
1384 if (lnum > buf->b_ml.ml_line_count)
1385 break;
1386 text = ml_get_buf(buf, lnum, FALSE);
1387 len = STRLEN(text) + 1;
1388 if ((size_t)buf->b_ml.ml_line_len > len)
1389 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001390 static textprop_T textprop; // static because of alignment
1391 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392
1393 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1394 / sizeof(textprop_T); ++idx)
1395 {
1396 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1397 + idx * sizeof(textprop_T);
1398 size_t taillen;
1399
1400 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001401 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1402 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001403 {
1404 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1405 {
1406 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1407
1408 // need to allocate the line to be able to change it
1409 if (newptr == NULL)
1410 return;
1411 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1412 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001413 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1414 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001415 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001416 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1417
1418 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001419 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001420 }
1421
1422 taillen = buf->b_ml.ml_line_len - len
1423 - (idx + 1) * sizeof(textprop_T);
1424 if (taillen > 0)
1425 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1426 taillen);
1427 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1428 --idx;
1429
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001430 if (textprop.tp_id < 0)
1431 {
1432 garray_T *gap = &buf->b_textprop_text;
1433 int ii = -textprop.tp_id - 1;
1434
1435 // negative ID: property with text - free the text
1436 if (ii < gap->ga_len)
1437 {
1438 char_u **p = ((char_u **)gap->ga_data) + ii;
1439 vim_free(*p);
1440 *p = NULL;
1441 did_remove_text = TRUE;
1442 }
1443 }
1444
Bram Moolenaar965c0442021-05-17 00:22:06 +02001445 if (first_changed == 0)
1446 first_changed = lnum;
1447 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001448 ++rettv->vval.v_number;
1449 if (!do_all)
1450 break;
1451 }
1452 }
1453 }
1454 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001455
Bram Moolenaar965c0442021-05-17 00:22:06 +02001456 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001457 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001458 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1459 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001460 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001461
1462 if (did_remove_text)
1463 {
1464 garray_T *gap = &buf->b_textprop_text;
1465
1466 // Reduce the growarray size for NULL pointers at the end.
1467 while (gap->ga_len > 0
1468 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1469 --gap->ga_len;
1470 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001471}
1472
1473/*
1474 * Common for f_prop_type_add() and f_prop_type_change().
1475 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001476 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001477prop_type_set(typval_T *argvars, int add)
1478{
1479 char_u *name;
1480 buf_T *buf = NULL;
1481 dict_T *dict;
1482 dictitem_T *di;
1483 proptype_T *prop;
1484
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001485 if (in_vim9script()
1486 && (check_for_string_arg(argvars, 0) == FAIL
1487 || check_for_dict_arg(argvars, 1) == FAIL))
1488 return;
1489
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001490 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001491 if (*name == NUL)
1492 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001493 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494 return;
1495 }
1496
1497 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1498 return;
1499 dict = argvars[1].vval.v_dict;
1500
1501 prop = find_prop(name, buf);
1502 if (add)
1503 {
1504 hashtab_T **htp;
1505
1506 if (prop != NULL)
1507 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001508 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001509 return;
1510 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001511 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001512 if (prop == NULL)
1513 return;
1514 STRCPY(prop->pt_name, name);
1515 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001516 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001517 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1518 if (*htp == NULL)
1519 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001520 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001521 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001522 {
1523 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001524 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001525 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001526 hash_init(*htp);
1527 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001528 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001529 }
1530 else
1531 {
1532 if (prop == NULL)
1533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001534 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001535 return;
1536 }
1537 }
1538
1539 if (dict != NULL)
1540 {
1541 di = dict_find(dict, (char_u *)"highlight", -1);
1542 if (di != NULL)
1543 {
1544 char_u *highlight;
1545 int hl_id = 0;
1546
Bram Moolenaard61efa52022-07-23 09:52:04 +01001547 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001548 if (highlight != NULL && *highlight != NUL)
1549 hl_id = syn_name2id(highlight);
1550 if (hl_id <= 0)
1551 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001552 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001553 highlight == NULL ? (char_u *)"" : highlight);
1554 return;
1555 }
1556 prop->pt_hl_id = hl_id;
1557 }
1558
Bram Moolenaar58187f12019-05-05 16:33:47 +02001559 di = dict_find(dict, (char_u *)"combine", -1);
1560 if (di != NULL)
1561 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001562 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001563 prop->pt_flags |= PT_FLAG_COMBINE;
1564 else
1565 prop->pt_flags &= ~PT_FLAG_COMBINE;
1566 }
1567
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001568 di = dict_find(dict, (char_u *)"override", -1);
1569 if (di != NULL)
1570 {
1571 if (tv_get_bool(&di->di_tv))
1572 prop->pt_flags |= PT_FLAG_OVERRIDE;
1573 else
1574 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1575 }
1576
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001577 di = dict_find(dict, (char_u *)"priority", -1);
1578 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001579 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001580
1581 di = dict_find(dict, (char_u *)"start_incl", -1);
1582 if (di != NULL)
1583 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001584 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001585 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1586 else
1587 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1588 }
1589
1590 di = dict_find(dict, (char_u *)"end_incl", -1);
1591 if (di != NULL)
1592 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001593 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001594 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1595 else
1596 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1597 }
1598 }
1599}
1600
1601/*
1602 * prop_type_add({name}, {props})
1603 */
1604 void
1605f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1606{
1607 prop_type_set(argvars, TRUE);
1608}
1609
1610/*
1611 * prop_type_change({name}, {props})
1612 */
1613 void
1614f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1615{
1616 prop_type_set(argvars, FALSE);
1617}
1618
1619/*
1620 * prop_type_delete({name} [, {bufnr}])
1621 */
1622 void
1623f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1624{
1625 char_u *name;
1626 buf_T *buf = NULL;
1627 hashitem_T *hi;
1628
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001629 if (in_vim9script()
1630 && (check_for_string_arg(argvars, 0) == FAIL
1631 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1632 return;
1633
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001634 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001635 if (*name == NUL)
1636 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001637 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001638 return;
1639 }
1640
1641 if (argvars[1].v_type != VAR_UNKNOWN)
1642 {
1643 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1644 return;
1645 }
1646
1647 hi = find_prop_hi(name, buf);
1648 if (hi != NULL)
1649 {
1650 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001651 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001652
1653 if (buf == NULL)
1654 ht = global_proptypes;
1655 else
1656 ht = buf->b_proptypes;
1657 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001658 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001659 }
1660}
1661
1662/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001663 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001664 */
1665 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001666f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001667{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001668 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001669
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001670 if (in_vim9script()
1671 && (check_for_string_arg(argvars, 0) == FAIL
1672 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1673 return;
1674
1675 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001676 if (*name == NUL)
1677 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001678 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001679 return;
1680 }
1681 if (rettv_dict_alloc(rettv) == OK)
1682 {
1683 proptype_T *prop = NULL;
1684 buf_T *buf = NULL;
1685
1686 if (argvars[1].v_type != VAR_UNKNOWN)
1687 {
1688 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1689 return;
1690 }
1691
1692 prop = find_prop(name, buf);
1693 if (prop != NULL)
1694 {
1695 dict_T *d = rettv->vval.v_dict;
1696
1697 if (prop->pt_hl_id > 0)
1698 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1699 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001700 dict_add_number(d, "combine",
1701 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001702 dict_add_number(d, "start_incl",
1703 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1704 dict_add_number(d, "end_incl",
1705 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1706 if (buf != NULL)
1707 dict_add_number(d, "bufnr", buf->b_fnum);
1708 }
1709 }
1710}
1711
1712 static void
1713list_types(hashtab_T *ht, list_T *l)
1714{
1715 long todo;
1716 hashitem_T *hi;
1717
1718 todo = (long)ht->ht_used;
1719 for (hi = ht->ht_array; todo > 0; ++hi)
1720 {
1721 if (!HASHITEM_EMPTY(hi))
1722 {
1723 proptype_T *prop = HI2PT(hi);
1724
1725 list_append_string(l, prop->pt_name, -1);
1726 --todo;
1727 }
1728 }
1729}
1730
1731/*
1732 * prop_type_list([{bufnr}])
1733 */
1734 void
1735f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1736{
1737 buf_T *buf = NULL;
1738
1739 if (rettv_list_alloc(rettv) == OK)
1740 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001741 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1742 return;
1743
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001744 if (argvars[0].v_type != VAR_UNKNOWN)
1745 {
1746 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1747 return;
1748 }
1749 if (buf == NULL)
1750 {
1751 if (global_proptypes != NULL)
1752 list_types(global_proptypes, rettv->vval.v_list);
1753 }
1754 else if (buf->b_proptypes != NULL)
1755 list_types(buf->b_proptypes, rettv->vval.v_list);
1756 }
1757}
1758
1759/*
1760 * Free all property types in "ht".
1761 */
1762 static void
1763clear_ht_prop_types(hashtab_T *ht)
1764{
1765 long todo;
1766 hashitem_T *hi;
1767
1768 if (ht == NULL)
1769 return;
1770
1771 todo = (long)ht->ht_used;
1772 for (hi = ht->ht_array; todo > 0; ++hi)
1773 {
1774 if (!HASHITEM_EMPTY(hi))
1775 {
1776 proptype_T *prop = HI2PT(hi);
1777
1778 vim_free(prop);
1779 --todo;
1780 }
1781 }
1782
1783 hash_clear(ht);
1784 vim_free(ht);
1785}
1786
1787#if defined(EXITFREE) || defined(PROTO)
1788/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001789 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001790 */
1791 void
1792clear_global_prop_types(void)
1793{
1794 clear_ht_prop_types(global_proptypes);
1795 global_proptypes = NULL;
1796}
1797#endif
1798
1799/*
1800 * Free all property types for "buf".
1801 */
1802 void
1803clear_buf_prop_types(buf_T *buf)
1804{
1805 clear_ht_prop_types(buf->b_proptypes);
1806 buf->b_proptypes = NULL;
1807}
1808
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001809// Struct used to return two values from adjust_prop().
1810typedef struct
1811{
1812 int dirty; // if the property was changed
1813 int can_drop; // whether after this change, the prop may be removed
1814} adjustres_T;
1815
1816/*
1817 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1818 *
1819 * Note that "col" is zero-based, while tp_col is one-based.
1820 * Only for the current buffer.
1821 * "flags" can have:
1822 * APC_SUBSTITUTE: Text is replaced, not inserted.
1823 */
1824 static adjustres_T
1825adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001826 textprop_T *prop,
1827 colnr_T col,
1828 int added,
1829 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001830{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001831 proptype_T *pt;
1832 int start_incl;
1833 int end_incl;
1834 int droppable;
1835 adjustres_T res = {TRUE, FALSE};
1836
1837 // prop after end of the line doesn't move
1838 if (prop->tp_col == MAXCOL)
1839 {
1840 res.dirty = FALSE;
1841 return res;
1842 }
1843
1844 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1845 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001846 || (flags & APC_SUBSTITUTE)
1847 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001848 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001849 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001850 // do not drop zero-width props if they later can increase in size
1851 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001852
1853 if (added > 0)
1854 {
1855 if (col + 1 <= prop->tp_col
1856 - (start_incl || (prop->tp_len == 0 && end_incl)))
1857 // Change is entirely before the text property: Only shift
1858 prop->tp_col += added;
1859 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1860 // Insertion was inside text property
1861 prop->tp_len += added;
1862 }
1863 else if (prop->tp_col > col + 1)
1864 {
1865 if (prop->tp_col + added < col + 1)
1866 {
1867 prop->tp_len += (prop->tp_col - 1 - col) + added;
1868 prop->tp_col = col + 1;
1869 if (prop->tp_len <= 0)
1870 {
1871 prop->tp_len = 0;
1872 res.can_drop = droppable;
1873 }
1874 }
1875 else
1876 prop->tp_col += added;
1877 }
1878 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1879 {
1880 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1881
1882 prop->tp_len += after > 0 ? added + after : added;
1883 res.can_drop = prop->tp_len <= 0 && droppable;
1884 }
1885 else
1886 res.dirty = FALSE;
1887
1888 return res;
1889}
1890
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001891/*
1892 * Adjust the columns of text properties in line "lnum" after position "col" to
1893 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001894 * Note that "col" is zero-based, while tp_col is one-based.
1895 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001896 * "flags" can have:
1897 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1898 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001899 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001900 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001901 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001902 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001903adjust_prop_columns(
1904 linenr_T lnum,
1905 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001906 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001907 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001908{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001909 int proplen;
1910 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001911 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001912 int ri, wi;
1913 size_t textlen;
1914
1915 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001916 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001917
1918 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1919 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001920 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001921 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001922
Bram Moolenaar196d1572019-01-02 23:47:18 +01001923 wi = 0; // write index
1924 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001925 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001926 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001927 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001928
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001929 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1930 res = adjust_prop(&prop, col, bytes_added, flags);
1931 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001932 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001933 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001934 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1935 && u_savesub(lnum) == FAIL)
1936 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001937 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001938
1939 // u_savesub() may have updated curbuf->b_ml, fetch it again
1940 if (curbuf->b_ml.ml_line_lnum != lnum)
1941 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001942 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001943 if (res.can_drop)
1944 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001945 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001946 ++wi;
1947 }
1948 if (dirty)
1949 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001950 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1951
1952 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001953 {
1954 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1955
1956 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1957 vim_free(curbuf->b_ml.ml_line_ptr);
1958 curbuf->b_ml.ml_line_ptr = p;
1959 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001960 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001961 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001962 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001963 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001964}
1965
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001966/*
1967 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001968 * "lnum_props" is the line that has the properties from before the split.
1969 * "lnum_top" is the top line.
1970 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001971 * "deleted" is the number of bytes deleted.
1972 */
1973 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001974adjust_props_for_split(
1975 linenr_T lnum_props,
1976 linenr_T lnum_top,
1977 int kept,
1978 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001979{
1980 char_u *props;
1981 int count;
1982 garray_T prevprop;
1983 garray_T nextprop;
1984 int i;
1985 int skipped = kept + deleted;
1986
1987 if (!curbuf->b_has_textprop)
1988 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001989
1990 // Get the text properties from "lnum_props".
1991 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001992 ga_init2(&prevprop, sizeof(textprop_T), 10);
1993 ga_init2(&nextprop, sizeof(textprop_T), 10);
1994
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001995 // Keep the relevant ones in the first line, reducing the length if needed.
1996 // Copy the ones that include the split to the second line.
1997 // Move the ones after the split to the second line.
1998 for (i = 0; i < count; ++i)
1999 {
2000 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002001 proptype_T *pt;
2002 int start_incl, end_incl;
2003 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002004
2005 // copy the prop to an aligned structure
2006 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2007
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002008 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2009 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2010 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
2011 cont_prev = prop.tp_col + !start_incl <= kept;
2012 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
2013
2014 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002015 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002016 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2017
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002018 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002019 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002020 if (p->tp_col + p->tp_len >= kept)
2021 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002022 if (cont_next)
2023 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002024 }
2025
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002026 // Only add the property to the next line if the length is bigger than
2027 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002028 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002029 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002030 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002031 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002032 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002033 if (p->tp_col > skipped)
2034 p->tp_col -= skipped - 1;
2035 else
2036 {
2037 p->tp_len -= skipped - p->tp_col;
2038 p->tp_col = 1;
2039 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002040 if (cont_prev)
2041 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002042 }
2043 }
2044
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002045 set_text_props(lnum_top, prevprop.ga_data,
2046 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002047 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002048 set_text_props(lnum_top + 1, nextprop.ga_data,
2049 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002050 ga_clear(&nextprop);
2051}
2052
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002053/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002054 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002055 */
2056 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002057prepend_joined_props(
2058 char_u *new_props,
2059 int propcount,
2060 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002061 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002062 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002063 long col,
2064 int removed)
2065{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002066 char_u *props;
2067 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2068 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002069
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002070 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002071 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002072 textprop_T prop;
2073 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002074
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002075 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002076 if (prop.tp_col == MAXCOL && !last_line)
2077 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002078 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2079
2080 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002081 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002082
Bram Moolenaare175dc62022-08-01 22:18:50 +01002083 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002084 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2085 &prop, sizeof(prop));
2086 else
2087 {
2088 int j;
2089 int found = FALSE;
2090
2091 // Search for continuing prop.
2092 for (j = *props_remaining; j < propcount; ++j)
2093 {
2094 textprop_T op;
2095
2096 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2097 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2098 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002099 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002100 found = TRUE;
2101 op.tp_len += op.tp_col - prop.tp_col;
2102 op.tp_col = prop.tp_col;
2103 // Start/end is taken care of when deleting joined lines
2104 op.tp_flags = prop.tp_flags;
2105 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2106 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002107 }
2108 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002109 if (!found)
2110 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002111 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002112 }
2113}
2114
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002115#endif // FEAT_PROP_POPUP