blob: 6fefc6d24cf5efef4f27286974f23615eb696495 [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 Moolenaar4164bb22019-01-04 23:09:49 +010015 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010016 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020017 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010018 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaar32aa1022019-11-02 22:54:41 +010019 * - Add an array for global_proptypes, to quickly lookup a prop type by ID
20 * - Add an array for b_proptypes, to quickly lookup a prop type by ID
Bram Moolenaarb56ac042018-12-28 23:22:40 +010021 * - Checking the text length to detect text properties is slow. Use a flag in
22 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010023 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
24 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010025 * - Perhaps have a window-local option to disable highlighting from text
26 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010027 */
28
29#include "vim.h"
30
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010031#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33/*
34 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
35 * This avoids adding a pointer to the hashtable item.
36 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
37 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
38 * HI2PT() converts a hashitem pointer to a proptype pointer.
39 */
40#define PT2HIKEY(p) ((p)->pt_name)
41#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
42#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
43
44// The global text property types.
45static hashtab_T *global_proptypes = NULL;
46
47// The last used text property type ID.
48static int proptype_id = 0;
49
Bram Moolenaar98aefe72018-12-13 22:20:09 +010050/*
51 * Find a property type by name, return the hashitem.
52 * Returns NULL if the item can't be found.
53 */
54 static hashitem_T *
55find_prop_hi(char_u *name, buf_T *buf)
56{
57 hashtab_T *ht;
58 hashitem_T *hi;
59
60 if (*name == NUL)
61 return NULL;
62 if (buf == NULL)
63 ht = global_proptypes;
64 else
65 ht = buf->b_proptypes;
66
67 if (ht == NULL)
68 return NULL;
69 hi = hash_find(ht, name);
70 if (HASHITEM_EMPTY(hi))
71 return NULL;
72 return hi;
73}
74
75/*
76 * Like find_prop_hi() but return the property type.
77 */
78 static proptype_T *
79find_prop(char_u *name, buf_T *buf)
80{
81 hashitem_T *hi = find_prop_hi(name, buf);
82
83 if (hi == NULL)
84 return NULL;
85 return HI2PT(hi);
86}
87
88/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020089 * Get the prop type ID of "name".
90 * When not found return zero.
91 */
92 int
93find_prop_type_id(char_u *name, buf_T *buf)
94{
95 proptype_T *pt = find_prop(name, buf);
96
97 if (pt == NULL)
98 return 0;
99 return pt->pt_id;
100}
101
102/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100103 * Lookup a property type by name. First in "buf" and when not found in the
104 * global types.
105 * When not found gives an error message and returns NULL.
106 */
107 static proptype_T *
108lookup_prop_type(char_u *name, buf_T *buf)
109{
110 proptype_T *type = find_prop(name, buf);
111
112 if (type == NULL)
113 type = find_prop(name, NULL);
114 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100115 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100116 return type;
117}
118
119/*
120 * Get an optional "bufnr" item from the dict in "arg".
121 * When the argument is not used or "bufnr" is not present then "buf" is
122 * unchanged.
123 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100124 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100125 */
126 static int
127get_bufnr_from_arg(typval_T *arg, buf_T **buf)
128{
129 dictitem_T *di;
130
131 if (arg->v_type != VAR_DICT)
132 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000133 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100134 return FAIL;
135 }
136 if (arg->vval.v_dict == NULL)
137 return OK; // NULL dict is like an empty dict
138 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200139 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
140 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100141 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200142 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143 if (*buf == NULL)
144 return FAIL;
145 }
146 return OK;
147}
148
149/*
150 * prop_add({lnum}, {col}, {props})
151 */
152 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100153f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100154{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100155 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100156 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100157
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200158 if (in_vim9script()
159 && (check_for_number_arg(argvars, 0) == FAIL
160 || check_for_number_arg(argvars, 1) == FAIL
161 || check_for_dict_arg(argvars, 2) == FAIL))
162 return;
163
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100164 start_lnum = tv_get_number(&argvars[0]);
165 start_col = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100166 if (argvars[2].v_type != VAR_DICT)
167 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000168 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100169 return;
170 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200171
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100172 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
173 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200174}
175
176/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200177 * Attach a text property 'type_name' to the text starting
178 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100179 * the buffer "buf" and assign identifier "id".
180 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200181 */
182 static int
183prop_add_one(
184 buf_T *buf,
185 char_u *type_name,
186 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text_arg,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100188 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189 linenr_T start_lnum,
190 linenr_T end_lnum,
191 colnr_T start_col,
192 colnr_T end_col)
193{
194 proptype_T *type;
195 linenr_T lnum;
196 int proplen;
197 char_u *props = NULL;
198 char_u *newprops;
199 size_t textlen;
200 char_u *newtext;
201 int i;
202 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100203 char_u *text = text_arg;
204 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200205
206 type = lookup_prop_type(type_name, buf);
207 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200209
210 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
211 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000212 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100213 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200214 }
215 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
216 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000217 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100218 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200219 }
220
221 if (buf->b_ml.ml_mfp == NULL)
222 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000223 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100224 goto theend;
225 }
226
227 if (text != NULL)
228 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 garray_T *gap = &buf->b_textprop_text;
230 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100231
232 // double check we got the right ID
233 if (-id - 1 != gap->ga_len)
234 iemsg("text prop ID mismatch");
235 if (gap->ga_growsize == 0)
236 ga_init2(gap, sizeof(char *), 50);
237 if (ga_grow(gap, 1) == FAIL)
238 goto theend;
239 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100240
241 // change any Tab to a Space to make it simpler to compute the size
242 for (p = text; *p != NUL; MB_PTR_ADV(p))
243 if (*p == TAB)
244 *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
603count_props(linenr_T lnum, int only_starting)
604{
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
611 if (only_starting)
612 for (i = 0; i < proplen; ++i)
613 {
614 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
615 if (prop.tp_flags & TP_FLAG_CONT_PREV)
616 --result;
617 }
618 return result;
619}
620
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100621/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200622 * Find text property "type_id" in the visible lines of window "wp".
623 * Match "id" when it is > 0.
624 * Returns FAIL when not found.
625 */
626 int
627find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
628 linenr_T *found_lnum)
629{
630 linenr_T lnum;
631 char_u *props;
632 int count;
633 int i;
634
635 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100636 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200637 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
638 {
639 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
640 for (i = 0; i < count; ++i)
641 {
642 mch_memmove(prop, props + i * sizeof(textprop_T),
643 sizeof(textprop_T));
644 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
645 {
646 *found_lnum = lnum;
647 return OK;
648 }
649 }
650 }
651 return FAIL;
652}
653
654/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100655 * Set the text properties for line "lnum" to "props" with length "len".
656 * If "len" is zero text properties are removed, "props" is not used.
657 * Any existing text properties are dropped.
658 * Only works for the current buffer.
659 */
660 static void
661set_text_props(linenr_T lnum, char_u *props, int len)
662{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100663 char_u *text;
664 char_u *newtext;
665 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100666
667 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100668 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100669 newtext = alloc(textlen + len);
670 if (newtext == NULL)
671 return;
672 mch_memmove(newtext, text, textlen);
673 if (len > 0)
674 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100675 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100676 vim_free(curbuf->b_ml.ml_line_ptr);
677 curbuf->b_ml.ml_line_ptr = newtext;
678 curbuf->b_ml.ml_line_len = textlen + len;
679 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
680}
681
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100682 static proptype_T *
683find_type_by_id(hashtab_T *ht, int id)
684{
685 long todo;
686 hashitem_T *hi;
687
688 if (ht == NULL)
689 return NULL;
690
691 // TODO: Make this faster by keeping a list of types sorted on ID and use
692 // a binary search.
693
694 todo = (long)ht->ht_used;
695 for (hi = ht->ht_array; todo > 0; ++hi)
696 {
697 if (!HASHITEM_EMPTY(hi))
698 {
699 proptype_T *prop = HI2PT(hi);
700
701 if (prop->pt_id == id)
702 return prop;
703 --todo;
704 }
705 }
706 return NULL;
707}
708
709/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100710 * Fill 'dict' with text properties in 'prop'.
711 */
712 static void
713prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
714{
715 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200716 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100717
718 dict_add_number(dict, "col", prop->tp_col);
719 dict_add_number(dict, "length", prop->tp_len);
720 dict_add_number(dict, "id", prop->tp_id);
721 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
722 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200723
724 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
725 if (pt == NULL)
726 {
727 pt = find_type_by_id(global_proptypes, prop->tp_type);
728 buflocal = FALSE;
729 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100730 if (pt != NULL)
731 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200732
733 if (buflocal)
734 dict_add_number(dict, "type_bufnr", buf->b_fnum);
735 else
736 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100737}
738
739/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100740 * Find a property type by ID in "buf" or globally.
741 * Returns NULL if not found.
742 */
743 proptype_T *
744text_prop_type_by_id(buf_T *buf, int id)
745{
746 proptype_T *type;
747
748 type = find_type_by_id(buf->b_proptypes, id);
749 if (type == NULL)
750 type = find_type_by_id(global_proptypes, id);
751 return type;
752}
753
754/*
755 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
756 */
757 void
758f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
759{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200760 linenr_T start;
761 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100762 linenr_T lnum;
763 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100764 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100765
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200766 if (in_vim9script()
767 && (check_for_number_arg(argvars, 0) == FAIL
768 || check_for_opt_number_arg(argvars, 1) == FAIL
769 || (argvars[1].v_type != VAR_UNKNOWN
770 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
771 return;
772
773 start = tv_get_number(&argvars[0]);
774 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100775 if (argvars[1].v_type != VAR_UNKNOWN)
776 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100777 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100778 if (argvars[2].v_type != VAR_UNKNOWN)
779 {
780 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
781 return;
782 }
783 }
784 if (start < 1 || end < 1)
785 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200786 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100787 return;
788 }
789
790 for (lnum = start; lnum <= end; ++lnum)
791 {
792 char_u *text;
793 size_t len;
794
795 if (lnum > buf->b_ml.ml_line_count)
796 break;
797 text = ml_get_buf(buf, lnum, FALSE);
798 len = STRLEN(text) + 1;
799 if ((size_t)buf->b_ml.ml_line_len > len)
800 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100801 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100802 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
803 {
804 char_u *newtext = vim_strsave(text);
805
806 // need to allocate the line now
807 if (newtext == NULL)
808 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100809 if (buf->b_ml.ml_flags & ML_ALLOCATED)
810 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100811 buf->b_ml.ml_line_ptr = newtext;
812 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
813 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100814 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100815 }
816 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100817 if (did_clear)
818 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100819}
820
821/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100822 * prop_find({props} [, {direction}])
823 */
824 void
825f_prop_find(typval_T *argvars, typval_T *rettv)
826{
827 pos_T *cursor = &curwin->w_cursor;
828 dict_T *dict;
829 buf_T *buf = curbuf;
830 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200831 int lnum_start;
832 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100833 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200834 int id = 0;
835 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200836 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100837 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200838 int lnum = -1;
839 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100840 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100841 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100842
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200843 if (in_vim9script()
844 && (check_for_dict_arg(argvars, 0) == FAIL
845 || check_for_opt_string_arg(argvars, 1) == FAIL))
846 return;
847
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100848 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
849 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000850 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100851 return;
852 }
853 dict = argvars[0].vval.v_dict;
854
855 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
856 return;
857 if (buf->b_ml.ml_mfp == NULL)
858 return;
859
860 if (argvars[1].v_type != VAR_UNKNOWN)
861 {
862 char_u *dir_s = tv_get_string(&argvars[1]);
863
864 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100865 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100866 else if (*dir_s != 'f')
867 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000868 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100869 return;
870 }
871 }
872
873 di = dict_find(dict, (char_u *)"lnum", -1);
874 if (di != NULL)
875 lnum = tv_get_number(&di->di_tv);
876
877 di = dict_find(dict, (char_u *)"col", -1);
878 if (di != NULL)
879 col = tv_get_number(&di->di_tv);
880
881 if (lnum == -1)
882 {
883 lnum = cursor->lnum;
884 col = cursor->col + 1;
885 }
886 else if (col == -1)
887 col = 1;
888
889 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
890 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200891 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100892 return;
893 }
894
Bram Moolenaard61efa52022-07-23 09:52:04 +0100895 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100896
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100897 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200898 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100899 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200900 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200901 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100902 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100903 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100904 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100905 proptype_T *type = lookup_prop_type(name, buf);
906
907 if (type == NULL)
908 return;
909 type_id = type->pt_id;
910 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100911 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200912 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100913 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000914 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100915 return;
916 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200917 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100918 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000919 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100920 return;
921 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100922
923 lnum_start = lnum;
924
925 if (rettv_dict_alloc(rettv) == FAIL)
926 return;
927
928 while (1)
929 {
930 char_u *text = ml_get_buf(buf, lnum, FALSE);
931 size_t textlen = STRLEN(text) + 1;
932 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200933 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100934 int i;
935 textprop_T prop;
936 int prop_start;
937 int prop_end;
938
LemonBoy9bd3ce22022-04-18 21:54:02 +0100939 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100940 {
941 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100942 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100943
LemonBoy9bd3ce22022-04-18 21:54:02 +0100944 // For the very first line try to find the first property before or
945 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100946 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100947 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100948 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100949 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100950 if (prop.tp_col > col)
951 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100952 }
953 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
954 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100955 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100956 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200957 : (id_found && prop.tp_id == id)
958 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100959 {
960 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100961 if (lnum_start == lnum
962 && col >= prop.tp_col
963 && (col <= prop.tp_col + prop.tp_len
964 - (prop.tp_len != 0)))
965 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100966
LemonBoy9bd3ce22022-04-18 21:54:02 +0100967 // The property was not continued from last line, it starts on
968 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100969 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100970 // The property does not continue on the next line, it ends on
971 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100972 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100973 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100974 seen_end = 1;
975
976 // Skip lines without the start flag.
977 if (!prop_start)
978 {
979 // Always search backwards for start when search started
980 // on a prop and we're not skipping.
981 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +0100982 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200983 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100984 }
985
986 // If skipstart is true, skip the prop at start pos (even if
987 // continued from another line).
988 if (start_pos_has_prop && skipstart && !seen_end)
989 {
990 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200991 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100992 }
993
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100994 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
995 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
996
997 return;
998 }
999 }
1000
1001 if (dir > 0)
1002 {
1003 if (lnum >= buf->b_ml.ml_line_count)
1004 break;
1005 lnum++;
1006 }
1007 else
1008 {
1009 if (lnum <= 1)
1010 break;
1011 lnum--;
1012 }
1013 }
1014}
1015
1016/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001017 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1018 */
1019 static int
1020prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1021{
1022 int i;
1023
1024 for (i = 0; i < len; i++)
1025 if (types_or_ids[i] == type_or_id)
1026 return TRUE;
1027
1028 return FALSE;
1029}
1030
1031/*
1032 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1033 * If 'prop_types' is not NULL, then return only the text properties with
1034 * matching property type in the 'prop_types' array.
1035 * If 'prop_ids' is not NULL, then return only the text properties with
1036 * an identifier in the 'props_ids' array.
1037 * If 'add_lnum' is TRUE, then add the line number also to the text property
1038 * dictionary.
1039 */
1040 static void
1041get_props_in_line(
1042 buf_T *buf,
1043 linenr_T lnum,
1044 int *prop_types,
1045 int prop_types_len,
1046 int *prop_ids,
1047 int prop_ids_len,
1048 list_T *retlist,
1049 int add_lnum)
1050{
1051 char_u *text = ml_get_buf(buf, lnum, FALSE);
1052 size_t textlen = STRLEN(text) + 1;
1053 int count;
1054 int i;
1055 textprop_T prop;
1056
1057 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1058 for (i = 0; i < count; ++i)
1059 {
1060 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1061 sizeof(textprop_T));
1062 if ((prop_types == NULL
1063 || prop_type_or_id_in_list(prop_types, prop_types_len,
1064 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001065 && (prop_ids == NULL
1066 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1067 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001068 {
1069 dict_T *d = dict_alloc();
1070
1071 if (d == NULL)
1072 break;
1073 prop_fill_dict(d, &prop, buf);
1074 if (add_lnum)
1075 dict_add_number(d, "lnum", lnum);
1076 list_append_dict(retlist, d);
1077 }
1078 }
1079}
1080
1081/*
1082 * Convert a List of property type names into an array of property type
1083 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1084 * error. 'num_types' is set to the number of returned property types.
1085 */
1086 static int *
1087get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1088{
1089 int *prop_types;
1090 listitem_T *li;
1091 int i;
1092 char_u *name;
1093 proptype_T *type;
1094
1095 *num_types = 0;
1096
1097 prop_types = ALLOC_MULT(int, list_len(l));
1098 if (prop_types == NULL)
1099 return NULL;
1100
1101 i = 0;
1102 FOR_ALL_LIST_ITEMS(l, li)
1103 {
1104 if (li->li_tv.v_type != VAR_STRING)
1105 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001106 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001107 goto errret;
1108 }
1109 name = li->li_tv.vval.v_string;
1110 if (name == NULL)
1111 goto errret;
1112
1113 type = lookup_prop_type(name, buf);
1114 if (type == NULL)
1115 goto errret;
1116 prop_types[i++] = type->pt_id;
1117 }
1118
1119 *num_types = i;
1120 return prop_types;
1121
1122errret:
1123 VIM_CLEAR(prop_types);
1124 return NULL;
1125}
1126
1127/*
1128 * Convert a List of property identifiers into an array of property
1129 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1130 * error. 'num_ids' is set to the number of returned property identifiers.
1131 */
1132 static int *
1133get_prop_ids_from_list(list_T *l, int *num_ids)
1134{
1135 int *prop_ids;
1136 listitem_T *li;
1137 int i;
1138 int id;
1139 int error;
1140
1141 *num_ids = 0;
1142
1143 prop_ids = ALLOC_MULT(int, list_len(l));
1144 if (prop_ids == NULL)
1145 return NULL;
1146
1147 i = 0;
1148 FOR_ALL_LIST_ITEMS(l, li)
1149 {
1150 error = FALSE;
1151 id = tv_get_number_chk(&li->li_tv, &error);
1152 if (error)
1153 goto errret;
1154
1155 prop_ids[i++] = id;
1156 }
1157
1158 *num_ids = i;
1159 return prop_ids;
1160
1161errret:
1162 VIM_CLEAR(prop_ids);
1163 return NULL;
1164}
1165
1166/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001167 * prop_list({lnum} [, {bufnr}])
1168 */
1169 void
1170f_prop_list(typval_T *argvars, typval_T *rettv)
1171{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001172 linenr_T lnum;
1173 linenr_T start_lnum;
1174 linenr_T end_lnum;
1175 buf_T *buf = curbuf;
1176 int add_lnum = FALSE;
1177 int *prop_types = NULL;
1178 int prop_types_len = 0;
1179 int *prop_ids = NULL;
1180 int prop_ids_len = 0;
1181 list_T *l;
1182 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001183
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001184 if (in_vim9script()
1185 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001186 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001187 return;
1188
Bram Moolenaar93a10962022-06-16 11:42:09 +01001189 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001190 return;
1191
1192 // default: get text properties on current line
1193 start_lnum = tv_get_number(&argvars[0]);
1194 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001195 if (argvars[1].v_type != VAR_UNKNOWN)
1196 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001197 dict_T *d;
1198
1199 if (argvars[1].v_type != VAR_DICT)
1200 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001201 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001202 return;
1203 }
1204 d = argvars[1].vval.v_dict;
1205
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001206 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1207 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001208
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001209 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001210 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001211 if (di->di_tv.v_type != VAR_NUMBER)
1212 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001213 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001214 return;
1215 }
1216 end_lnum = tv_get_number(&di->di_tv);
1217 if (end_lnum < 0)
1218 // negative end_lnum is used as an offset from the last buffer
1219 // line
1220 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1221 else if (end_lnum > buf->b_ml.ml_line_count)
1222 end_lnum = buf->b_ml.ml_line_count;
1223 add_lnum = TRUE;
1224 }
1225 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1226 {
1227 if (di->di_tv.v_type != VAR_LIST)
1228 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001229 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001230 return;
1231 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001232
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001233 l = di->di_tv.vval.v_list;
1234 if (l != NULL && list_len(l) > 0)
1235 {
1236 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1237 if (prop_types == NULL)
1238 return;
1239 }
1240 }
1241 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1242 {
1243 if (di->di_tv.v_type != VAR_LIST)
1244 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001245 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001246 goto errret;
1247 }
1248
1249 l = di->di_tv.vval.v_list;
1250 if (l != NULL && list_len(l) > 0)
1251 {
1252 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1253 if (prop_ids == NULL)
1254 goto errret;
1255 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001256 }
1257 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001258 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1259 || end_lnum < 1 || end_lnum < start_lnum)
1260 emsg(_(e_invalid_range));
1261 else
1262 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1263 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1264 prop_ids, prop_ids_len,
1265 rettv->vval.v_list, add_lnum);
1266
1267errret:
1268 VIM_CLEAR(prop_types);
1269 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001270}
1271
1272/*
1273 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1274 */
1275 void
1276f_prop_remove(typval_T *argvars, typval_T *rettv)
1277{
1278 linenr_T start = 1;
1279 linenr_T end = 0;
1280 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001281 linenr_T first_changed = 0;
1282 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001283 dict_T *dict;
1284 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001285 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001286 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001287 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001288 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001289 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001290
1291 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001292
1293 if (in_vim9script()
1294 && (check_for_dict_arg(argvars, 0) == FAIL
1295 || check_for_opt_number_arg(argvars, 1) == FAIL
1296 || (argvars[1].v_type != VAR_UNKNOWN
1297 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1298 return;
1299
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001300 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1301 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001302 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001303 return;
1304 }
1305
1306 if (argvars[1].v_type != VAR_UNKNOWN)
1307 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001308 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001309 end = start;
1310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001311 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001312 if (start < 1 || end < 1)
1313 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001314 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001315 return;
1316 }
1317 }
1318
1319 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001320 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1321 return;
1322 if (buf->b_ml.ml_mfp == NULL)
1323 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001324
Bram Moolenaard61efa52022-07-23 09:52:04 +01001325 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001326
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001327 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001328 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001329 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001330 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001331 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001332 proptype_T *type = lookup_prop_type(name, buf);
1333
1334 if (type == NULL)
1335 return;
1336 type_id = type->pt_id;
1337 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001338 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001339
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001340 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001341 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001342 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001343 return;
1344 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001345 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001346 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001347 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001348 return;
1349 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001350
1351 if (end == 0)
1352 end = buf->b_ml.ml_line_count;
1353 for (lnum = start; lnum <= end; ++lnum)
1354 {
1355 char_u *text;
1356 size_t len;
1357
1358 if (lnum > buf->b_ml.ml_line_count)
1359 break;
1360 text = ml_get_buf(buf, lnum, FALSE);
1361 len = STRLEN(text) + 1;
1362 if ((size_t)buf->b_ml.ml_line_len > len)
1363 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001364 static textprop_T textprop; // static because of alignment
1365 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001366
1367 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1368 / sizeof(textprop_T); ++idx)
1369 {
1370 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1371 + idx * sizeof(textprop_T);
1372 size_t taillen;
1373
1374 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001375 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1376 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001377 {
1378 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1379 {
1380 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1381
1382 // need to allocate the line to be able to change it
1383 if (newptr == NULL)
1384 return;
1385 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1386 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001387 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1388 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001389 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001390 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1391
1392 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001393 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001394 }
1395
1396 taillen = buf->b_ml.ml_line_len - len
1397 - (idx + 1) * sizeof(textprop_T);
1398 if (taillen > 0)
1399 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1400 taillen);
1401 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1402 --idx;
1403
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001404 if (textprop.tp_id < 0)
1405 {
1406 garray_T *gap = &buf->b_textprop_text;
1407 int ii = -textprop.tp_id - 1;
1408
1409 // negative ID: property with text - free the text
1410 if (ii < gap->ga_len)
1411 {
1412 char_u **p = ((char_u **)gap->ga_data) + ii;
1413 vim_free(*p);
1414 *p = NULL;
1415 did_remove_text = TRUE;
1416 }
1417 }
1418
Bram Moolenaar965c0442021-05-17 00:22:06 +02001419 if (first_changed == 0)
1420 first_changed = lnum;
1421 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001422 ++rettv->vval.v_number;
1423 if (!do_all)
1424 break;
1425 }
1426 }
1427 }
1428 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001429
Bram Moolenaar965c0442021-05-17 00:22:06 +02001430 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001431 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001432 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1433 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001434 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001435
1436 if (did_remove_text)
1437 {
1438 garray_T *gap = &buf->b_textprop_text;
1439
1440 // Reduce the growarray size for NULL pointers at the end.
1441 while (gap->ga_len > 0
1442 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1443 --gap->ga_len;
1444 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001445}
1446
1447/*
1448 * Common for f_prop_type_add() and f_prop_type_change().
1449 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001450 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001451prop_type_set(typval_T *argvars, int add)
1452{
1453 char_u *name;
1454 buf_T *buf = NULL;
1455 dict_T *dict;
1456 dictitem_T *di;
1457 proptype_T *prop;
1458
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001459 if (in_vim9script()
1460 && (check_for_string_arg(argvars, 0) == FAIL
1461 || check_for_dict_arg(argvars, 1) == FAIL))
1462 return;
1463
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001464 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001465 if (*name == NUL)
1466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001467 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001468 return;
1469 }
1470
1471 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1472 return;
1473 dict = argvars[1].vval.v_dict;
1474
1475 prop = find_prop(name, buf);
1476 if (add)
1477 {
1478 hashtab_T **htp;
1479
1480 if (prop != NULL)
1481 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001482 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001483 return;
1484 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001485 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001486 if (prop == NULL)
1487 return;
1488 STRCPY(prop->pt_name, name);
1489 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001490 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001491 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1492 if (*htp == NULL)
1493 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001494 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001496 {
1497 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001498 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001499 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001500 hash_init(*htp);
1501 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001502 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001503 }
1504 else
1505 {
1506 if (prop == NULL)
1507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001508 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001509 return;
1510 }
1511 }
1512
1513 if (dict != NULL)
1514 {
1515 di = dict_find(dict, (char_u *)"highlight", -1);
1516 if (di != NULL)
1517 {
1518 char_u *highlight;
1519 int hl_id = 0;
1520
Bram Moolenaard61efa52022-07-23 09:52:04 +01001521 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001522 if (highlight != NULL && *highlight != NUL)
1523 hl_id = syn_name2id(highlight);
1524 if (hl_id <= 0)
1525 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001526 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001527 highlight == NULL ? (char_u *)"" : highlight);
1528 return;
1529 }
1530 prop->pt_hl_id = hl_id;
1531 }
1532
Bram Moolenaar58187f12019-05-05 16:33:47 +02001533 di = dict_find(dict, (char_u *)"combine", -1);
1534 if (di != NULL)
1535 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001536 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001537 prop->pt_flags |= PT_FLAG_COMBINE;
1538 else
1539 prop->pt_flags &= ~PT_FLAG_COMBINE;
1540 }
1541
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001542 di = dict_find(dict, (char_u *)"priority", -1);
1543 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001544 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001545
1546 di = dict_find(dict, (char_u *)"start_incl", -1);
1547 if (di != NULL)
1548 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001549 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001550 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1551 else
1552 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1553 }
1554
1555 di = dict_find(dict, (char_u *)"end_incl", -1);
1556 if (di != NULL)
1557 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001558 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001559 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1560 else
1561 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1562 }
1563 }
1564}
1565
1566/*
1567 * prop_type_add({name}, {props})
1568 */
1569 void
1570f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1571{
1572 prop_type_set(argvars, TRUE);
1573}
1574
1575/*
1576 * prop_type_change({name}, {props})
1577 */
1578 void
1579f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1580{
1581 prop_type_set(argvars, FALSE);
1582}
1583
1584/*
1585 * prop_type_delete({name} [, {bufnr}])
1586 */
1587 void
1588f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1589{
1590 char_u *name;
1591 buf_T *buf = NULL;
1592 hashitem_T *hi;
1593
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001594 if (in_vim9script()
1595 && (check_for_string_arg(argvars, 0) == FAIL
1596 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1597 return;
1598
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001599 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001600 if (*name == NUL)
1601 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001602 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001603 return;
1604 }
1605
1606 if (argvars[1].v_type != VAR_UNKNOWN)
1607 {
1608 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1609 return;
1610 }
1611
1612 hi = find_prop_hi(name, buf);
1613 if (hi != NULL)
1614 {
1615 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001616 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001617
1618 if (buf == NULL)
1619 ht = global_proptypes;
1620 else
1621 ht = buf->b_proptypes;
1622 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001623 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001624 }
1625}
1626
1627/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001628 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001629 */
1630 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001631f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001632{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001633 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001634
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001635 if (in_vim9script()
1636 && (check_for_string_arg(argvars, 0) == FAIL
1637 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1638 return;
1639
1640 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001641 if (*name == NUL)
1642 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001643 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001644 return;
1645 }
1646 if (rettv_dict_alloc(rettv) == OK)
1647 {
1648 proptype_T *prop = NULL;
1649 buf_T *buf = NULL;
1650
1651 if (argvars[1].v_type != VAR_UNKNOWN)
1652 {
1653 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1654 return;
1655 }
1656
1657 prop = find_prop(name, buf);
1658 if (prop != NULL)
1659 {
1660 dict_T *d = rettv->vval.v_dict;
1661
1662 if (prop->pt_hl_id > 0)
1663 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1664 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001665 dict_add_number(d, "combine",
1666 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001667 dict_add_number(d, "start_incl",
1668 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1669 dict_add_number(d, "end_incl",
1670 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1671 if (buf != NULL)
1672 dict_add_number(d, "bufnr", buf->b_fnum);
1673 }
1674 }
1675}
1676
1677 static void
1678list_types(hashtab_T *ht, list_T *l)
1679{
1680 long todo;
1681 hashitem_T *hi;
1682
1683 todo = (long)ht->ht_used;
1684 for (hi = ht->ht_array; todo > 0; ++hi)
1685 {
1686 if (!HASHITEM_EMPTY(hi))
1687 {
1688 proptype_T *prop = HI2PT(hi);
1689
1690 list_append_string(l, prop->pt_name, -1);
1691 --todo;
1692 }
1693 }
1694}
1695
1696/*
1697 * prop_type_list([{bufnr}])
1698 */
1699 void
1700f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1701{
1702 buf_T *buf = NULL;
1703
1704 if (rettv_list_alloc(rettv) == OK)
1705 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001706 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1707 return;
1708
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001709 if (argvars[0].v_type != VAR_UNKNOWN)
1710 {
1711 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1712 return;
1713 }
1714 if (buf == NULL)
1715 {
1716 if (global_proptypes != NULL)
1717 list_types(global_proptypes, rettv->vval.v_list);
1718 }
1719 else if (buf->b_proptypes != NULL)
1720 list_types(buf->b_proptypes, rettv->vval.v_list);
1721 }
1722}
1723
1724/*
1725 * Free all property types in "ht".
1726 */
1727 static void
1728clear_ht_prop_types(hashtab_T *ht)
1729{
1730 long todo;
1731 hashitem_T *hi;
1732
1733 if (ht == NULL)
1734 return;
1735
1736 todo = (long)ht->ht_used;
1737 for (hi = ht->ht_array; todo > 0; ++hi)
1738 {
1739 if (!HASHITEM_EMPTY(hi))
1740 {
1741 proptype_T *prop = HI2PT(hi);
1742
1743 vim_free(prop);
1744 --todo;
1745 }
1746 }
1747
1748 hash_clear(ht);
1749 vim_free(ht);
1750}
1751
1752#if defined(EXITFREE) || defined(PROTO)
1753/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001754 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001755 */
1756 void
1757clear_global_prop_types(void)
1758{
1759 clear_ht_prop_types(global_proptypes);
1760 global_proptypes = NULL;
1761}
1762#endif
1763
1764/*
1765 * Free all property types for "buf".
1766 */
1767 void
1768clear_buf_prop_types(buf_T *buf)
1769{
1770 clear_ht_prop_types(buf->b_proptypes);
1771 buf->b_proptypes = NULL;
1772}
1773
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001774// Struct used to return two values from adjust_prop().
1775typedef struct
1776{
1777 int dirty; // if the property was changed
1778 int can_drop; // whether after this change, the prop may be removed
1779} adjustres_T;
1780
1781/*
1782 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1783 *
1784 * Note that "col" is zero-based, while tp_col is one-based.
1785 * Only for the current buffer.
1786 * "flags" can have:
1787 * APC_SUBSTITUTE: Text is replaced, not inserted.
1788 */
1789 static adjustres_T
1790adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001791 textprop_T *prop,
1792 colnr_T col,
1793 int added,
1794 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001795{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001796 proptype_T *pt;
1797 int start_incl;
1798 int end_incl;
1799 int droppable;
1800 adjustres_T res = {TRUE, FALSE};
1801
1802 // prop after end of the line doesn't move
1803 if (prop->tp_col == MAXCOL)
1804 {
1805 res.dirty = FALSE;
1806 return res;
1807 }
1808
1809 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1810 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001811 || (flags & APC_SUBSTITUTE)
1812 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001813 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001814 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001815 // do not drop zero-width props if they later can increase in size
1816 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001817
1818 if (added > 0)
1819 {
1820 if (col + 1 <= prop->tp_col
1821 - (start_incl || (prop->tp_len == 0 && end_incl)))
1822 // Change is entirely before the text property: Only shift
1823 prop->tp_col += added;
1824 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1825 // Insertion was inside text property
1826 prop->tp_len += added;
1827 }
1828 else if (prop->tp_col > col + 1)
1829 {
1830 if (prop->tp_col + added < col + 1)
1831 {
1832 prop->tp_len += (prop->tp_col - 1 - col) + added;
1833 prop->tp_col = col + 1;
1834 if (prop->tp_len <= 0)
1835 {
1836 prop->tp_len = 0;
1837 res.can_drop = droppable;
1838 }
1839 }
1840 else
1841 prop->tp_col += added;
1842 }
1843 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1844 {
1845 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1846
1847 prop->tp_len += after > 0 ? added + after : added;
1848 res.can_drop = prop->tp_len <= 0 && droppable;
1849 }
1850 else
1851 res.dirty = FALSE;
1852
1853 return res;
1854}
1855
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001856/*
1857 * Adjust the columns of text properties in line "lnum" after position "col" to
1858 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001859 * Note that "col" is zero-based, while tp_col is one-based.
1860 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001861 * "flags" can have:
1862 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1863 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001864 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001865 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001866 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001867 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001868adjust_prop_columns(
1869 linenr_T lnum,
1870 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001871 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001872 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001873{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001874 int proplen;
1875 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001876 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001877 int ri, wi;
1878 size_t textlen;
1879
1880 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001881 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001882
1883 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1884 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001885 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001886 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001887
Bram Moolenaar196d1572019-01-02 23:47:18 +01001888 wi = 0; // write index
1889 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001890 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001891 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001892 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001893
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001894 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1895 res = adjust_prop(&prop, col, bytes_added, flags);
1896 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001897 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001898 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001899 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1900 && u_savesub(lnum) == FAIL)
1901 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001902 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001903
1904 // u_savesub() may have updated curbuf->b_ml, fetch it again
1905 if (curbuf->b_ml.ml_line_lnum != lnum)
1906 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001907 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001908 if (res.can_drop)
1909 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001910 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001911 ++wi;
1912 }
1913 if (dirty)
1914 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001915 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1916
1917 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001918 {
1919 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1920
1921 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1922 vim_free(curbuf->b_ml.ml_line_ptr);
1923 curbuf->b_ml.ml_line_ptr = p;
1924 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001925 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001926 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001927 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001928 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001929}
1930
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001931/*
1932 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001933 * "lnum_props" is the line that has the properties from before the split.
1934 * "lnum_top" is the top line.
1935 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001936 * "deleted" is the number of bytes deleted.
1937 */
1938 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001939adjust_props_for_split(
1940 linenr_T lnum_props,
1941 linenr_T lnum_top,
1942 int kept,
1943 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001944{
1945 char_u *props;
1946 int count;
1947 garray_T prevprop;
1948 garray_T nextprop;
1949 int i;
1950 int skipped = kept + deleted;
1951
1952 if (!curbuf->b_has_textprop)
1953 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001954
1955 // Get the text properties from "lnum_props".
1956 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001957 ga_init2(&prevprop, sizeof(textprop_T), 10);
1958 ga_init2(&nextprop, sizeof(textprop_T), 10);
1959
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001960 // Keep the relevant ones in the first line, reducing the length if needed.
1961 // Copy the ones that include the split to the second line.
1962 // Move the ones after the split to the second line.
1963 for (i = 0; i < count; ++i)
1964 {
1965 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001966 proptype_T *pt;
1967 int start_incl, end_incl;
1968 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001969
1970 // copy the prop to an aligned structure
1971 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1972
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001973 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1974 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1975 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1976 cont_prev = prop.tp_col + !start_incl <= kept;
1977 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1978
1979 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001980 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001981 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1982
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001983 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001984 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001985 if (p->tp_col + p->tp_len >= kept)
1986 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001987 if (cont_next)
1988 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001989 }
1990
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001991 // Only add the property to the next line if the length is bigger than
1992 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001993 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001994 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001995 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001996 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001997 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001998 if (p->tp_col > skipped)
1999 p->tp_col -= skipped - 1;
2000 else
2001 {
2002 p->tp_len -= skipped - p->tp_col;
2003 p->tp_col = 1;
2004 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002005 if (cont_prev)
2006 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002007 }
2008 }
2009
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002010 set_text_props(lnum_top, prevprop.ga_data,
2011 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002012 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002013 set_text_props(lnum_top + 1, nextprop.ga_data,
2014 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002015 ga_clear(&nextprop);
2016}
2017
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002018/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002019 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002020 */
2021 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002022prepend_joined_props(
2023 char_u *new_props,
2024 int propcount,
2025 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002026 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002027 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002028 long col,
2029 int removed)
2030{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002031 char_u *props;
2032 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2033 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002034
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002035 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002036 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002037 textprop_T prop;
2038 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002039
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002040 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
2041 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2042
2043 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002044 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002045
2046 if (add_all || end)
2047 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2048 &prop, sizeof(prop));
2049 else
2050 {
2051 int j;
2052 int found = FALSE;
2053
2054 // Search for continuing prop.
2055 for (j = *props_remaining; j < propcount; ++j)
2056 {
2057 textprop_T op;
2058
2059 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2060 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2061 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002062 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002063 found = TRUE;
2064 op.tp_len += op.tp_col - prop.tp_col;
2065 op.tp_col = prop.tp_col;
2066 // Start/end is taken care of when deleting joined lines
2067 op.tp_flags = prop.tp_flags;
2068 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2069 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002070 }
2071 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002072 if (!found)
2073 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002074 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002075 }
2076}
2077
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002078#endif // FEAT_PROP_POPUP