blob: 86b0dbf883d57c0c9b620c5f2a74b2b5bc06de53 [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
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100241 // change any control character (Tab, Newline, etc.) to a Space to make
242 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100243 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100244 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100245 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100246 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200247 }
248
249 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
250 {
251 colnr_T col; // start column
252 long length; // in bytes
253
254 // Fetch the line to get the ml_line_len field updated.
255 proplen = get_text_props(buf, lnum, &props, TRUE);
256 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
257
258 if (lnum == start_lnum)
259 col = start_col;
260 else
261 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100262 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200263 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000264 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100265 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200266 }
267
268 if (lnum == end_lnum)
269 length = end_col - col;
270 else
271 length = (int)textlen - col + 1;
272 if (length > (long)textlen)
273 length = (int)textlen; // can include the end-of-line
274 if (length < 0)
275 length = 0; // zero-width property
276
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100277 if (text_arg != NULL)
278 {
279 length = 1; // text is placed on one character
280 if (col == 0)
281 col = MAXCOL; // after the line
282 }
283
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200284 // Allocate the new line with space for the new property.
285 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
286 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100287 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200288 // Copy the text, including terminating NUL.
289 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
290
291 // Find the index where to insert the new property.
292 // Since the text properties are not aligned properly when stored with
293 // the text, we need to copy them as bytes before using it as a struct.
294 for (i = 0; i < proplen; ++i)
295 {
296 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
297 sizeof(textprop_T));
298 if (tmp_prop.tp_col >= col)
299 break;
300 }
301 newprops = newtext + textlen;
302 if (i > 0)
303 mch_memmove(newprops, props, sizeof(textprop_T) * i);
304
305 tmp_prop.tp_col = col;
306 tmp_prop.tp_len = length;
307 tmp_prop.tp_id = id;
308 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100309 tmp_prop.tp_flags = text_flags
310 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
311 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200312 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
313 sizeof(textprop_T));
314
315 if (i < proplen)
316 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
317 props + i * sizeof(textprop_T),
318 sizeof(textprop_T) * (proplen - i));
319
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100320 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200321 vim_free(buf->b_ml.ml_line_ptr);
322 buf->b_ml.ml_line_ptr = newtext;
323 buf->b_ml.ml_line_len += sizeof(textprop_T);
324 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
325 }
326
327 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100328 res = OK;
329
330theend:
331 vim_free(text);
332 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200333}
334
335/*
336 * prop_add_list()
337 * First argument specifies the text property:
338 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
339 * Second argument is a List where each item is a List with the following
340 * entries: [lnum, start_col, end_col]
341 */
342 void
343f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
344{
345 dict_T *dict;
346 char_u *type_name;
347 buf_T *buf = curbuf;
348 int id = 0;
349 listitem_T *li;
350 list_T *pos_list;
351 linenr_T start_lnum;
352 colnr_T start_col;
353 linenr_T end_lnum;
354 colnr_T end_col;
355 int error = FALSE;
356
357 if (check_for_dict_arg(argvars, 0) == FAIL
358 || check_for_list_arg(argvars, 1) == FAIL)
359 return;
360
361 if (argvars[1].vval.v_list == NULL)
362 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000363 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364 return;
365 }
366
367 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100368 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200369 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000370 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200371 return;
372 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100373 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200374
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100375 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100376 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200377
378 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
379 return;
380
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100381 // This must be done _before_ we start adding properties because property
382 // changes trigger buffer (memline) reorganisation, which needs this flag
383 // to be correctly set.
384 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200385 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
386 {
387 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
388 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000389 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200390 return;
391 }
392
393 pos_list = li->li_tv.vval.v_list;
394 start_lnum = list_find_nr(pos_list, 0L, &error);
395 start_col = list_find_nr(pos_list, 1L, &error);
396 end_lnum = list_find_nr(pos_list, 2L, &error);
397 end_col = list_find_nr(pos_list, 3L, &error);
398 if (error || start_lnum <= 0 || start_col <= 0
399 || end_lnum <= 0 || end_col <= 0)
400 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000401 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200402 return;
403 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100404 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200405 start_col, end_col) == FAIL)
406 return;
407 }
408
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200409 redraw_buf_later(buf, VALID);
410}
411
412/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100413 * Get the next ID to use for a textprop with text in buffer "buf".
414 */
415 static int
416get_textprop_id(buf_T *buf)
417{
418 // TODO: recycle deleted entries
419 return -(buf->b_textprop_text.ga_len + 1);
420}
421
422/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200423 * Shared between prop_add() and popup_create().
424 * "dict_arg" is the function argument of a dict containing "bufnr".
425 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100426 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200427 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100428 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200429prop_add_common(
430 linenr_T start_lnum,
431 colnr_T start_col,
432 dict_T *dict,
433 buf_T *default_buf,
434 typval_T *dict_arg)
435{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200436 linenr_T end_lnum;
437 colnr_T end_col;
438 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200439 buf_T *buf = default_buf;
440 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100441 char_u *text = NULL;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100442 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100443
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100444 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100445 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000446 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100447 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100448 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100449 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100450
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100451 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100452 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100453 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100454 if (end_lnum < start_lnum)
455 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000456 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100457 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100458 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100459 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100460 else
461 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100462
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100463 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100465 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100466
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100467 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100468 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000469 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100470 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100472 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100473 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100474 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100475 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100476 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100477 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100478 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000479 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100480 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100481 }
482 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100483 else if (start_lnum == end_lnum)
484 end_col = start_col;
485 else
486 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100487
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100488 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100489 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100490
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100491 if (dict_has_key(dict, "text"))
492 {
493 text = dict_get_string(dict, "text", TRUE);
494 if (text == NULL)
495 goto theend;
496 // use a default length of 1 to make multiple props show up
497 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100498
499 if (dict_has_key(dict, "text_align"))
500 {
501 char_u *p = dict_get_string(dict, "text_align", FALSE);
502
503 if (p == NULL)
504 goto theend;
505 if (STRCMP(p, "right") == 0)
506 flags |= TP_FLAG_ALIGN_RIGHT;
507 else if (STRCMP(p, "below") == 0)
508 flags |= TP_FLAG_ALIGN_BELOW;
509 else if (STRCMP(p, "after") != 0)
510 {
511 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
512 goto theend;
513 }
514 }
515
516 if (dict_has_key(dict, "text_wrap"))
517 {
518 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
519 if (p == NULL)
520 goto theend;
521 if (STRCMP(p, "wrap") == 0)
522 flags |= TP_FLAG_WRAP;
523 else if (STRCMP(p, "truncate") != 0)
524 {
525 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
526 goto theend;
527 }
528 }
529 }
530
531 // Column must be 1 or more for a normal text property; when "text" is
532 // present zero means it goes after the line.
533 if (start_col < (text == NULL ? 1 : 0))
534 {
535 semsg(_(e_invalid_column_number_nr), (long)start_col);
536 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100537 }
538
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200539 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100540 goto theend;
541
542 if (id < 0 && buf->b_textprop_text.ga_len > 0)
543 {
544 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
545 goto theend;
546 }
547 if (text != NULL)
548 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100549
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100550 // This must be done _before_ we add the property because property changes
551 // trigger buffer (memline) reorganisation, which needs this flag to be
552 // correctly set.
553 buf->b_has_textprop = TRUE; // this is never reset
554
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100555 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100556 start_lnum, end_lnum, start_col, end_col);
557 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100558
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200559 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100560
561theend:
562 vim_free(text);
563 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100564}
565
566/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100567 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100568 * Returns the number of text properties and, when non-zero, a pointer to the
569 * first one in "props" (note that it is not aligned, therefore the char_u
570 * pointer).
571 */
572 int
573get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
574{
575 char_u *text;
576 size_t textlen;
577 size_t proplen;
578
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100579 // Be quick when no text property types have been defined or the buffer,
580 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200581 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100582 return 0;
583
584 // Fetch the line to get the ml_line_len field updated.
585 text = ml_get_buf(buf, lnum, will_change);
586 textlen = STRLEN(text) + 1;
587 proplen = buf->b_ml.ml_line_len - textlen;
588 if (proplen % sizeof(textprop_T) != 0)
589 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000590 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100591 return 0;
592 }
593 if (proplen > 0)
594 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100595 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100596}
597
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200598/**
599 * Return the number of text properties on line "lnum" in the current buffer.
600 * When "only_starting" is true only text properties starting in this line will
601 * be considered.
602 */
603 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100604count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200605{
606 char_u *props;
607 int proplen = get_text_props(curbuf, lnum, &props, 0);
608 int result = proplen;
609 int i;
610 textprop_T prop;
611
Bram Moolenaare175dc62022-08-01 22:18:50 +0100612 for (i = 0; i < proplen; ++i)
613 {
614 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
615 // A prop is droppend when in the first line and it continues from the
616 // previous line, or when not in the last line and it is virtual text
617 // after the line.
618 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
619 || (!last_line && prop.tp_col == MAXCOL))
620 --result;
621 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200622 return result;
623}
624
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100625/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200626 * Find text property "type_id" in the visible lines of window "wp".
627 * Match "id" when it is > 0.
628 * Returns FAIL when not found.
629 */
630 int
631find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
632 linenr_T *found_lnum)
633{
634 linenr_T lnum;
635 char_u *props;
636 int count;
637 int i;
638
639 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100640 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200641 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
642 {
643 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
644 for (i = 0; i < count; ++i)
645 {
646 mch_memmove(prop, props + i * sizeof(textprop_T),
647 sizeof(textprop_T));
648 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
649 {
650 *found_lnum = lnum;
651 return OK;
652 }
653 }
654 }
655 return FAIL;
656}
657
658/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100659 * Set the text properties for line "lnum" to "props" with length "len".
660 * If "len" is zero text properties are removed, "props" is not used.
661 * Any existing text properties are dropped.
662 * Only works for the current buffer.
663 */
664 static void
665set_text_props(linenr_T lnum, char_u *props, int len)
666{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100667 char_u *text;
668 char_u *newtext;
669 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100670
671 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100672 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100673 newtext = alloc(textlen + len);
674 if (newtext == NULL)
675 return;
676 mch_memmove(newtext, text, textlen);
677 if (len > 0)
678 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100679 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100680 vim_free(curbuf->b_ml.ml_line_ptr);
681 curbuf->b_ml.ml_line_ptr = newtext;
682 curbuf->b_ml.ml_line_len = textlen + len;
683 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
684}
685
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100686 static proptype_T *
687find_type_by_id(hashtab_T *ht, int id)
688{
689 long todo;
690 hashitem_T *hi;
691
692 if (ht == NULL)
693 return NULL;
694
695 // TODO: Make this faster by keeping a list of types sorted on ID and use
696 // a binary search.
697
698 todo = (long)ht->ht_used;
699 for (hi = ht->ht_array; todo > 0; ++hi)
700 {
701 if (!HASHITEM_EMPTY(hi))
702 {
703 proptype_T *prop = HI2PT(hi);
704
705 if (prop->pt_id == id)
706 return prop;
707 --todo;
708 }
709 }
710 return NULL;
711}
712
713/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100714 * Fill 'dict' with text properties in 'prop'.
715 */
716 static void
717prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
718{
719 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200720 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100721
722 dict_add_number(dict, "col", prop->tp_col);
723 dict_add_number(dict, "length", prop->tp_len);
724 dict_add_number(dict, "id", prop->tp_id);
725 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
726 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200727
728 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
729 if (pt == NULL)
730 {
731 pt = find_type_by_id(global_proptypes, prop->tp_type);
732 buflocal = FALSE;
733 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100734 if (pt != NULL)
735 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200736
737 if (buflocal)
738 dict_add_number(dict, "type_bufnr", buf->b_fnum);
739 else
740 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100741}
742
743/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100744 * Find a property type by ID in "buf" or globally.
745 * Returns NULL if not found.
746 */
747 proptype_T *
748text_prop_type_by_id(buf_T *buf, int id)
749{
750 proptype_T *type;
751
752 type = find_type_by_id(buf->b_proptypes, id);
753 if (type == NULL)
754 type = find_type_by_id(global_proptypes, id);
755 return type;
756}
757
758/*
759 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
760 */
761 void
762f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
763{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200764 linenr_T start;
765 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100766 linenr_T lnum;
767 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100768 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100769
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200770 if (in_vim9script()
771 && (check_for_number_arg(argvars, 0) == FAIL
772 || check_for_opt_number_arg(argvars, 1) == FAIL
773 || (argvars[1].v_type != VAR_UNKNOWN
774 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
775 return;
776
777 start = tv_get_number(&argvars[0]);
778 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100779 if (argvars[1].v_type != VAR_UNKNOWN)
780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100781 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100782 if (argvars[2].v_type != VAR_UNKNOWN)
783 {
784 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
785 return;
786 }
787 }
788 if (start < 1 || end < 1)
789 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200790 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100791 return;
792 }
793
794 for (lnum = start; lnum <= end; ++lnum)
795 {
796 char_u *text;
797 size_t len;
798
799 if (lnum > buf->b_ml.ml_line_count)
800 break;
801 text = ml_get_buf(buf, lnum, FALSE);
802 len = STRLEN(text) + 1;
803 if ((size_t)buf->b_ml.ml_line_len > len)
804 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100805 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100806 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
807 {
808 char_u *newtext = vim_strsave(text);
809
810 // need to allocate the line now
811 if (newtext == NULL)
812 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100813 if (buf->b_ml.ml_flags & ML_ALLOCATED)
814 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100815 buf->b_ml.ml_line_ptr = newtext;
816 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
817 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100818 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100819 }
820 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100821 if (did_clear)
822 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100823}
824
825/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100826 * prop_find({props} [, {direction}])
827 */
828 void
829f_prop_find(typval_T *argvars, typval_T *rettv)
830{
831 pos_T *cursor = &curwin->w_cursor;
832 dict_T *dict;
833 buf_T *buf = curbuf;
834 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200835 int lnum_start;
836 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100837 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200838 int id = 0;
839 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200840 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100841 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200842 int lnum = -1;
843 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100844 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100845 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100846
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200847 if (in_vim9script()
848 && (check_for_dict_arg(argvars, 0) == FAIL
849 || check_for_opt_string_arg(argvars, 1) == FAIL))
850 return;
851
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100852 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
853 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000854 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100855 return;
856 }
857 dict = argvars[0].vval.v_dict;
858
859 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
860 return;
861 if (buf->b_ml.ml_mfp == NULL)
862 return;
863
864 if (argvars[1].v_type != VAR_UNKNOWN)
865 {
866 char_u *dir_s = tv_get_string(&argvars[1]);
867
868 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100869 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100870 else if (*dir_s != 'f')
871 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000872 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100873 return;
874 }
875 }
876
877 di = dict_find(dict, (char_u *)"lnum", -1);
878 if (di != NULL)
879 lnum = tv_get_number(&di->di_tv);
880
881 di = dict_find(dict, (char_u *)"col", -1);
882 if (di != NULL)
883 col = tv_get_number(&di->di_tv);
884
885 if (lnum == -1)
886 {
887 lnum = cursor->lnum;
888 col = cursor->col + 1;
889 }
890 else if (col == -1)
891 col = 1;
892
893 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
894 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200895 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100896 return;
897 }
898
Bram Moolenaard61efa52022-07-23 09:52:04 +0100899 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100900
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100901 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200902 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100903 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200904 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200905 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100906 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100907 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100908 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100909 proptype_T *type = lookup_prop_type(name, buf);
910
911 if (type == NULL)
912 return;
913 type_id = type->pt_id;
914 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100915 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200916 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100917 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000918 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100919 return;
920 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200921 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100922 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000923 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100924 return;
925 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100926
927 lnum_start = lnum;
928
929 if (rettv_dict_alloc(rettv) == FAIL)
930 return;
931
932 while (1)
933 {
934 char_u *text = ml_get_buf(buf, lnum, FALSE);
935 size_t textlen = STRLEN(text) + 1;
936 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200937 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100938 int i;
939 textprop_T prop;
940 int prop_start;
941 int prop_end;
942
LemonBoy9bd3ce22022-04-18 21:54:02 +0100943 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100944 {
945 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +0100946 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100947
LemonBoy9bd3ce22022-04-18 21:54:02 +0100948 // For the very first line try to find the first property before or
949 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100950 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100951 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100952 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100953 {
LemonBoy9bd3ce22022-04-18 21:54:02 +0100954 if (prop.tp_col > col)
955 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100956 }
957 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
958 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100959 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100960 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200961 : (id_found && prop.tp_id == id)
962 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100963 {
964 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100965 if (lnum_start == lnum
966 && col >= prop.tp_col
967 && (col <= prop.tp_col + prop.tp_len
968 - (prop.tp_len != 0)))
969 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100970
LemonBoy9bd3ce22022-04-18 21:54:02 +0100971 // The property was not continued from last line, it starts on
972 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100973 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100974 // The property does not continue on the next line, it ends on
975 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100976 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +0100977 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100978 seen_end = 1;
979
980 // Skip lines without the start flag.
981 if (!prop_start)
982 {
983 // Always search backwards for start when search started
984 // on a prop and we're not skipping.
985 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +0100986 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200987 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100988 }
989
990 // If skipstart is true, skip the prop at start pos (even if
991 // continued from another line).
992 if (start_pos_has_prop && skipstart && !seen_end)
993 {
994 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200995 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100996 }
997
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100998 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
999 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1000
1001 return;
1002 }
1003 }
1004
1005 if (dir > 0)
1006 {
1007 if (lnum >= buf->b_ml.ml_line_count)
1008 break;
1009 lnum++;
1010 }
1011 else
1012 {
1013 if (lnum <= 1)
1014 break;
1015 lnum--;
1016 }
1017 }
1018}
1019
1020/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001021 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1022 */
1023 static int
1024prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1025{
1026 int i;
1027
1028 for (i = 0; i < len; i++)
1029 if (types_or_ids[i] == type_or_id)
1030 return TRUE;
1031
1032 return FALSE;
1033}
1034
1035/*
1036 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1037 * If 'prop_types' is not NULL, then return only the text properties with
1038 * matching property type in the 'prop_types' array.
1039 * If 'prop_ids' is not NULL, then return only the text properties with
1040 * an identifier in the 'props_ids' array.
1041 * If 'add_lnum' is TRUE, then add the line number also to the text property
1042 * dictionary.
1043 */
1044 static void
1045get_props_in_line(
1046 buf_T *buf,
1047 linenr_T lnum,
1048 int *prop_types,
1049 int prop_types_len,
1050 int *prop_ids,
1051 int prop_ids_len,
1052 list_T *retlist,
1053 int add_lnum)
1054{
1055 char_u *text = ml_get_buf(buf, lnum, FALSE);
1056 size_t textlen = STRLEN(text) + 1;
1057 int count;
1058 int i;
1059 textprop_T prop;
1060
1061 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1062 for (i = 0; i < count; ++i)
1063 {
1064 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1065 sizeof(textprop_T));
1066 if ((prop_types == NULL
1067 || prop_type_or_id_in_list(prop_types, prop_types_len,
1068 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001069 && (prop_ids == NULL
1070 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1071 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001072 {
1073 dict_T *d = dict_alloc();
1074
1075 if (d == NULL)
1076 break;
1077 prop_fill_dict(d, &prop, buf);
1078 if (add_lnum)
1079 dict_add_number(d, "lnum", lnum);
1080 list_append_dict(retlist, d);
1081 }
1082 }
1083}
1084
1085/*
1086 * Convert a List of property type names into an array of property type
1087 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1088 * error. 'num_types' is set to the number of returned property types.
1089 */
1090 static int *
1091get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1092{
1093 int *prop_types;
1094 listitem_T *li;
1095 int i;
1096 char_u *name;
1097 proptype_T *type;
1098
1099 *num_types = 0;
1100
1101 prop_types = ALLOC_MULT(int, list_len(l));
1102 if (prop_types == NULL)
1103 return NULL;
1104
1105 i = 0;
1106 FOR_ALL_LIST_ITEMS(l, li)
1107 {
1108 if (li->li_tv.v_type != VAR_STRING)
1109 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001110 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001111 goto errret;
1112 }
1113 name = li->li_tv.vval.v_string;
1114 if (name == NULL)
1115 goto errret;
1116
1117 type = lookup_prop_type(name, buf);
1118 if (type == NULL)
1119 goto errret;
1120 prop_types[i++] = type->pt_id;
1121 }
1122
1123 *num_types = i;
1124 return prop_types;
1125
1126errret:
1127 VIM_CLEAR(prop_types);
1128 return NULL;
1129}
1130
1131/*
1132 * Convert a List of property identifiers into an array of property
1133 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1134 * error. 'num_ids' is set to the number of returned property identifiers.
1135 */
1136 static int *
1137get_prop_ids_from_list(list_T *l, int *num_ids)
1138{
1139 int *prop_ids;
1140 listitem_T *li;
1141 int i;
1142 int id;
1143 int error;
1144
1145 *num_ids = 0;
1146
1147 prop_ids = ALLOC_MULT(int, list_len(l));
1148 if (prop_ids == NULL)
1149 return NULL;
1150
1151 i = 0;
1152 FOR_ALL_LIST_ITEMS(l, li)
1153 {
1154 error = FALSE;
1155 id = tv_get_number_chk(&li->li_tv, &error);
1156 if (error)
1157 goto errret;
1158
1159 prop_ids[i++] = id;
1160 }
1161
1162 *num_ids = i;
1163 return prop_ids;
1164
1165errret:
1166 VIM_CLEAR(prop_ids);
1167 return NULL;
1168}
1169
1170/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001171 * prop_list({lnum} [, {bufnr}])
1172 */
1173 void
1174f_prop_list(typval_T *argvars, typval_T *rettv)
1175{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001176 linenr_T lnum;
1177 linenr_T start_lnum;
1178 linenr_T end_lnum;
1179 buf_T *buf = curbuf;
1180 int add_lnum = FALSE;
1181 int *prop_types = NULL;
1182 int prop_types_len = 0;
1183 int *prop_ids = NULL;
1184 int prop_ids_len = 0;
1185 list_T *l;
1186 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001187
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001188 if (in_vim9script()
1189 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001190 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001191 return;
1192
Bram Moolenaar93a10962022-06-16 11:42:09 +01001193 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001194 return;
1195
1196 // default: get text properties on current line
1197 start_lnum = tv_get_number(&argvars[0]);
1198 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001199 if (argvars[1].v_type != VAR_UNKNOWN)
1200 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001201 dict_T *d;
1202
1203 if (argvars[1].v_type != VAR_DICT)
1204 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001205 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001206 return;
1207 }
1208 d = argvars[1].vval.v_dict;
1209
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001210 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1211 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001212
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001213 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001214 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001215 if (di->di_tv.v_type != VAR_NUMBER)
1216 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001217 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001218 return;
1219 }
1220 end_lnum = tv_get_number(&di->di_tv);
1221 if (end_lnum < 0)
1222 // negative end_lnum is used as an offset from the last buffer
1223 // line
1224 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1225 else if (end_lnum > buf->b_ml.ml_line_count)
1226 end_lnum = buf->b_ml.ml_line_count;
1227 add_lnum = TRUE;
1228 }
1229 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1230 {
1231 if (di->di_tv.v_type != VAR_LIST)
1232 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001233 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001234 return;
1235 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001236
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001237 l = di->di_tv.vval.v_list;
1238 if (l != NULL && list_len(l) > 0)
1239 {
1240 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1241 if (prop_types == NULL)
1242 return;
1243 }
1244 }
1245 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1246 {
1247 if (di->di_tv.v_type != VAR_LIST)
1248 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001249 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001250 goto errret;
1251 }
1252
1253 l = di->di_tv.vval.v_list;
1254 if (l != NULL && list_len(l) > 0)
1255 {
1256 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1257 if (prop_ids == NULL)
1258 goto errret;
1259 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001260 }
1261 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001262 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1263 || end_lnum < 1 || end_lnum < start_lnum)
1264 emsg(_(e_invalid_range));
1265 else
1266 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1267 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1268 prop_ids, prop_ids_len,
1269 rettv->vval.v_list, add_lnum);
1270
1271errret:
1272 VIM_CLEAR(prop_types);
1273 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001274}
1275
1276/*
1277 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1278 */
1279 void
1280f_prop_remove(typval_T *argvars, typval_T *rettv)
1281{
1282 linenr_T start = 1;
1283 linenr_T end = 0;
1284 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001285 linenr_T first_changed = 0;
1286 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001287 dict_T *dict;
1288 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001289 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001290 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001291 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001292 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001293 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001294
1295 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001296
1297 if (in_vim9script()
1298 && (check_for_dict_arg(argvars, 0) == FAIL
1299 || check_for_opt_number_arg(argvars, 1) == FAIL
1300 || (argvars[1].v_type != VAR_UNKNOWN
1301 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1302 return;
1303
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001304 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1305 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001306 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001307 return;
1308 }
1309
1310 if (argvars[1].v_type != VAR_UNKNOWN)
1311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001312 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001313 end = start;
1314 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001315 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001316 if (start < 1 || end < 1)
1317 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001318 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001319 return;
1320 }
1321 }
1322
1323 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001324 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1325 return;
1326 if (buf->b_ml.ml_mfp == NULL)
1327 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001328
Bram Moolenaard61efa52022-07-23 09:52:04 +01001329 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001330
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001331 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001332 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001333 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001334 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001335 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001336 proptype_T *type = lookup_prop_type(name, buf);
1337
1338 if (type == NULL)
1339 return;
1340 type_id = type->pt_id;
1341 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001342 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001343
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001344 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001345 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001346 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001347 return;
1348 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001349 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001350 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001351 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001352 return;
1353 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001354
1355 if (end == 0)
1356 end = buf->b_ml.ml_line_count;
1357 for (lnum = start; lnum <= end; ++lnum)
1358 {
1359 char_u *text;
1360 size_t len;
1361
1362 if (lnum > buf->b_ml.ml_line_count)
1363 break;
1364 text = ml_get_buf(buf, lnum, FALSE);
1365 len = STRLEN(text) + 1;
1366 if ((size_t)buf->b_ml.ml_line_len > len)
1367 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001368 static textprop_T textprop; // static because of alignment
1369 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001370
1371 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1372 / sizeof(textprop_T); ++idx)
1373 {
1374 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1375 + idx * sizeof(textprop_T);
1376 size_t taillen;
1377
1378 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001379 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1380 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001381 {
1382 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1383 {
1384 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1385
1386 // need to allocate the line to be able to change it
1387 if (newptr == NULL)
1388 return;
1389 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1390 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001391 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1392 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001393 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001394 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1395
1396 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001397 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001398 }
1399
1400 taillen = buf->b_ml.ml_line_len - len
1401 - (idx + 1) * sizeof(textprop_T);
1402 if (taillen > 0)
1403 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1404 taillen);
1405 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1406 --idx;
1407
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001408 if (textprop.tp_id < 0)
1409 {
1410 garray_T *gap = &buf->b_textprop_text;
1411 int ii = -textprop.tp_id - 1;
1412
1413 // negative ID: property with text - free the text
1414 if (ii < gap->ga_len)
1415 {
1416 char_u **p = ((char_u **)gap->ga_data) + ii;
1417 vim_free(*p);
1418 *p = NULL;
1419 did_remove_text = TRUE;
1420 }
1421 }
1422
Bram Moolenaar965c0442021-05-17 00:22:06 +02001423 if (first_changed == 0)
1424 first_changed = lnum;
1425 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001426 ++rettv->vval.v_number;
1427 if (!do_all)
1428 break;
1429 }
1430 }
1431 }
1432 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001433
Bram Moolenaar965c0442021-05-17 00:22:06 +02001434 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001435 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001436 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1437 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001438 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001439
1440 if (did_remove_text)
1441 {
1442 garray_T *gap = &buf->b_textprop_text;
1443
1444 // Reduce the growarray size for NULL pointers at the end.
1445 while (gap->ga_len > 0
1446 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1447 --gap->ga_len;
1448 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001449}
1450
1451/*
1452 * Common for f_prop_type_add() and f_prop_type_change().
1453 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001454 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001455prop_type_set(typval_T *argvars, int add)
1456{
1457 char_u *name;
1458 buf_T *buf = NULL;
1459 dict_T *dict;
1460 dictitem_T *di;
1461 proptype_T *prop;
1462
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001463 if (in_vim9script()
1464 && (check_for_string_arg(argvars, 0) == FAIL
1465 || check_for_dict_arg(argvars, 1) == FAIL))
1466 return;
1467
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001468 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001469 if (*name == NUL)
1470 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001471 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001472 return;
1473 }
1474
1475 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1476 return;
1477 dict = argvars[1].vval.v_dict;
1478
1479 prop = find_prop(name, buf);
1480 if (add)
1481 {
1482 hashtab_T **htp;
1483
1484 if (prop != NULL)
1485 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001486 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001487 return;
1488 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001489 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001490 if (prop == NULL)
1491 return;
1492 STRCPY(prop->pt_name, name);
1493 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001494 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1496 if (*htp == NULL)
1497 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001498 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001499 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001500 {
1501 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001502 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001503 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001504 hash_init(*htp);
1505 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001506 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001507 }
1508 else
1509 {
1510 if (prop == NULL)
1511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001512 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001513 return;
1514 }
1515 }
1516
1517 if (dict != NULL)
1518 {
1519 di = dict_find(dict, (char_u *)"highlight", -1);
1520 if (di != NULL)
1521 {
1522 char_u *highlight;
1523 int hl_id = 0;
1524
Bram Moolenaard61efa52022-07-23 09:52:04 +01001525 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001526 if (highlight != NULL && *highlight != NUL)
1527 hl_id = syn_name2id(highlight);
1528 if (hl_id <= 0)
1529 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001530 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001531 highlight == NULL ? (char_u *)"" : highlight);
1532 return;
1533 }
1534 prop->pt_hl_id = hl_id;
1535 }
1536
Bram Moolenaar58187f12019-05-05 16:33:47 +02001537 di = dict_find(dict, (char_u *)"combine", -1);
1538 if (di != NULL)
1539 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001540 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001541 prop->pt_flags |= PT_FLAG_COMBINE;
1542 else
1543 prop->pt_flags &= ~PT_FLAG_COMBINE;
1544 }
1545
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001546 di = dict_find(dict, (char_u *)"override", -1);
1547 if (di != NULL)
1548 {
1549 if (tv_get_bool(&di->di_tv))
1550 prop->pt_flags |= PT_FLAG_OVERRIDE;
1551 else
1552 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1553 }
1554
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001555 di = dict_find(dict, (char_u *)"priority", -1);
1556 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001557 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001558
1559 di = dict_find(dict, (char_u *)"start_incl", -1);
1560 if (di != NULL)
1561 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001562 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001563 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1564 else
1565 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1566 }
1567
1568 di = dict_find(dict, (char_u *)"end_incl", -1);
1569 if (di != NULL)
1570 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001571 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001572 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1573 else
1574 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1575 }
1576 }
1577}
1578
1579/*
1580 * prop_type_add({name}, {props})
1581 */
1582 void
1583f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1584{
1585 prop_type_set(argvars, TRUE);
1586}
1587
1588/*
1589 * prop_type_change({name}, {props})
1590 */
1591 void
1592f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1593{
1594 prop_type_set(argvars, FALSE);
1595}
1596
1597/*
1598 * prop_type_delete({name} [, {bufnr}])
1599 */
1600 void
1601f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1602{
1603 char_u *name;
1604 buf_T *buf = NULL;
1605 hashitem_T *hi;
1606
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001607 if (in_vim9script()
1608 && (check_for_string_arg(argvars, 0) == FAIL
1609 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1610 return;
1611
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001612 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001613 if (*name == NUL)
1614 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001615 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001616 return;
1617 }
1618
1619 if (argvars[1].v_type != VAR_UNKNOWN)
1620 {
1621 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1622 return;
1623 }
1624
1625 hi = find_prop_hi(name, buf);
1626 if (hi != NULL)
1627 {
1628 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001629 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001630
1631 if (buf == NULL)
1632 ht = global_proptypes;
1633 else
1634 ht = buf->b_proptypes;
1635 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001636 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001637 }
1638}
1639
1640/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001641 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001642 */
1643 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001644f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001645{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001646 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001647
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001648 if (in_vim9script()
1649 && (check_for_string_arg(argvars, 0) == FAIL
1650 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1651 return;
1652
1653 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001654 if (*name == NUL)
1655 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001656 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001657 return;
1658 }
1659 if (rettv_dict_alloc(rettv) == OK)
1660 {
1661 proptype_T *prop = NULL;
1662 buf_T *buf = NULL;
1663
1664 if (argvars[1].v_type != VAR_UNKNOWN)
1665 {
1666 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1667 return;
1668 }
1669
1670 prop = find_prop(name, buf);
1671 if (prop != NULL)
1672 {
1673 dict_T *d = rettv->vval.v_dict;
1674
1675 if (prop->pt_hl_id > 0)
1676 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1677 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001678 dict_add_number(d, "combine",
1679 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001680 dict_add_number(d, "start_incl",
1681 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1682 dict_add_number(d, "end_incl",
1683 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1684 if (buf != NULL)
1685 dict_add_number(d, "bufnr", buf->b_fnum);
1686 }
1687 }
1688}
1689
1690 static void
1691list_types(hashtab_T *ht, list_T *l)
1692{
1693 long todo;
1694 hashitem_T *hi;
1695
1696 todo = (long)ht->ht_used;
1697 for (hi = ht->ht_array; todo > 0; ++hi)
1698 {
1699 if (!HASHITEM_EMPTY(hi))
1700 {
1701 proptype_T *prop = HI2PT(hi);
1702
1703 list_append_string(l, prop->pt_name, -1);
1704 --todo;
1705 }
1706 }
1707}
1708
1709/*
1710 * prop_type_list([{bufnr}])
1711 */
1712 void
1713f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1714{
1715 buf_T *buf = NULL;
1716
1717 if (rettv_list_alloc(rettv) == OK)
1718 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001719 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1720 return;
1721
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001722 if (argvars[0].v_type != VAR_UNKNOWN)
1723 {
1724 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1725 return;
1726 }
1727 if (buf == NULL)
1728 {
1729 if (global_proptypes != NULL)
1730 list_types(global_proptypes, rettv->vval.v_list);
1731 }
1732 else if (buf->b_proptypes != NULL)
1733 list_types(buf->b_proptypes, rettv->vval.v_list);
1734 }
1735}
1736
1737/*
1738 * Free all property types in "ht".
1739 */
1740 static void
1741clear_ht_prop_types(hashtab_T *ht)
1742{
1743 long todo;
1744 hashitem_T *hi;
1745
1746 if (ht == NULL)
1747 return;
1748
1749 todo = (long)ht->ht_used;
1750 for (hi = ht->ht_array; todo > 0; ++hi)
1751 {
1752 if (!HASHITEM_EMPTY(hi))
1753 {
1754 proptype_T *prop = HI2PT(hi);
1755
1756 vim_free(prop);
1757 --todo;
1758 }
1759 }
1760
1761 hash_clear(ht);
1762 vim_free(ht);
1763}
1764
1765#if defined(EXITFREE) || defined(PROTO)
1766/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001767 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001768 */
1769 void
1770clear_global_prop_types(void)
1771{
1772 clear_ht_prop_types(global_proptypes);
1773 global_proptypes = NULL;
1774}
1775#endif
1776
1777/*
1778 * Free all property types for "buf".
1779 */
1780 void
1781clear_buf_prop_types(buf_T *buf)
1782{
1783 clear_ht_prop_types(buf->b_proptypes);
1784 buf->b_proptypes = NULL;
1785}
1786
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001787// Struct used to return two values from adjust_prop().
1788typedef struct
1789{
1790 int dirty; // if the property was changed
1791 int can_drop; // whether after this change, the prop may be removed
1792} adjustres_T;
1793
1794/*
1795 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1796 *
1797 * Note that "col" is zero-based, while tp_col is one-based.
1798 * Only for the current buffer.
1799 * "flags" can have:
1800 * APC_SUBSTITUTE: Text is replaced, not inserted.
1801 */
1802 static adjustres_T
1803adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001804 textprop_T *prop,
1805 colnr_T col,
1806 int added,
1807 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001808{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001809 proptype_T *pt;
1810 int start_incl;
1811 int end_incl;
1812 int droppable;
1813 adjustres_T res = {TRUE, FALSE};
1814
1815 // prop after end of the line doesn't move
1816 if (prop->tp_col == MAXCOL)
1817 {
1818 res.dirty = FALSE;
1819 return res;
1820 }
1821
1822 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1823 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001824 || (flags & APC_SUBSTITUTE)
1825 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001826 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001827 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001828 // do not drop zero-width props if they later can increase in size
1829 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001830
1831 if (added > 0)
1832 {
1833 if (col + 1 <= prop->tp_col
1834 - (start_incl || (prop->tp_len == 0 && end_incl)))
1835 // Change is entirely before the text property: Only shift
1836 prop->tp_col += added;
1837 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1838 // Insertion was inside text property
1839 prop->tp_len += added;
1840 }
1841 else if (prop->tp_col > col + 1)
1842 {
1843 if (prop->tp_col + added < col + 1)
1844 {
1845 prop->tp_len += (prop->tp_col - 1 - col) + added;
1846 prop->tp_col = col + 1;
1847 if (prop->tp_len <= 0)
1848 {
1849 prop->tp_len = 0;
1850 res.can_drop = droppable;
1851 }
1852 }
1853 else
1854 prop->tp_col += added;
1855 }
1856 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1857 {
1858 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1859
1860 prop->tp_len += after > 0 ? added + after : added;
1861 res.can_drop = prop->tp_len <= 0 && droppable;
1862 }
1863 else
1864 res.dirty = FALSE;
1865
1866 return res;
1867}
1868
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001869/*
1870 * Adjust the columns of text properties in line "lnum" after position "col" to
1871 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001872 * Note that "col" is zero-based, while tp_col is one-based.
1873 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001874 * "flags" can have:
1875 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1876 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001877 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001878 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001879 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001880 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001881adjust_prop_columns(
1882 linenr_T lnum,
1883 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001884 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001885 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001886{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001887 int proplen;
1888 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001889 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001890 int ri, wi;
1891 size_t textlen;
1892
1893 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001894 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001895
1896 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1897 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001898 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001899 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001900
Bram Moolenaar196d1572019-01-02 23:47:18 +01001901 wi = 0; // write index
1902 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001903 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001904 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001905 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001906
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001907 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1908 res = adjust_prop(&prop, col, bytes_added, flags);
1909 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001910 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001911 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001912 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1913 && u_savesub(lnum) == FAIL)
1914 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001915 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001916
1917 // u_savesub() may have updated curbuf->b_ml, fetch it again
1918 if (curbuf->b_ml.ml_line_lnum != lnum)
1919 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001920 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001921 if (res.can_drop)
1922 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001923 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001924 ++wi;
1925 }
1926 if (dirty)
1927 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001928 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1929
1930 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001931 {
1932 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
1933
1934 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
1935 vim_free(curbuf->b_ml.ml_line_ptr);
1936 curbuf->b_ml.ml_line_ptr = p;
1937 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001938 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001939 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001940 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001941 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001942}
1943
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001944/*
1945 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001946 * "lnum_props" is the line that has the properties from before the split.
1947 * "lnum_top" is the top line.
1948 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001949 * "deleted" is the number of bytes deleted.
1950 */
1951 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001952adjust_props_for_split(
1953 linenr_T lnum_props,
1954 linenr_T lnum_top,
1955 int kept,
1956 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001957{
1958 char_u *props;
1959 int count;
1960 garray_T prevprop;
1961 garray_T nextprop;
1962 int i;
1963 int skipped = kept + deleted;
1964
1965 if (!curbuf->b_has_textprop)
1966 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001967
1968 // Get the text properties from "lnum_props".
1969 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001970 ga_init2(&prevprop, sizeof(textprop_T), 10);
1971 ga_init2(&nextprop, sizeof(textprop_T), 10);
1972
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001973 // Keep the relevant ones in the first line, reducing the length if needed.
1974 // Copy the ones that include the split to the second line.
1975 // Move the ones after the split to the second line.
1976 for (i = 0; i < count; ++i)
1977 {
1978 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001979 proptype_T *pt;
1980 int start_incl, end_incl;
1981 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001982
1983 // copy the prop to an aligned structure
1984 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1985
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001986 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1987 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1988 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1989 cont_prev = prop.tp_col + !start_incl <= kept;
1990 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1991
1992 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001993 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001994 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1995
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001996 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001997 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001998 if (p->tp_col + p->tp_len >= kept)
1999 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002000 if (cont_next)
2001 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002002 }
2003
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002004 // Only add the property to the next line if the length is bigger than
2005 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002006 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002007 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002008 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002009 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002010 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002011 if (p->tp_col > skipped)
2012 p->tp_col -= skipped - 1;
2013 else
2014 {
2015 p->tp_len -= skipped - p->tp_col;
2016 p->tp_col = 1;
2017 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002018 if (cont_prev)
2019 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002020 }
2021 }
2022
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002023 set_text_props(lnum_top, prevprop.ga_data,
2024 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002025 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002026 set_text_props(lnum_top + 1, nextprop.ga_data,
2027 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002028 ga_clear(&nextprop);
2029}
2030
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002031/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002032 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002033 */
2034 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002035prepend_joined_props(
2036 char_u *new_props,
2037 int propcount,
2038 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002039 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002040 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002041 long col,
2042 int removed)
2043{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002044 char_u *props;
2045 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2046 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002047
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002048 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002049 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002050 textprop_T prop;
2051 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002052
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002053 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002054 if (prop.tp_col == MAXCOL && !last_line)
2055 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002056 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2057
2058 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002059 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002060
Bram Moolenaare175dc62022-08-01 22:18:50 +01002061 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002062 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2063 &prop, sizeof(prop));
2064 else
2065 {
2066 int j;
2067 int found = FALSE;
2068
2069 // Search for continuing prop.
2070 for (j = *props_remaining; j < propcount; ++j)
2071 {
2072 textprop_T op;
2073
2074 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2075 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2076 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002077 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002078 found = TRUE;
2079 op.tp_len += op.tp_col - prop.tp_col;
2080 op.tp_col = prop.tp_col;
2081 // Start/end is taken care of when deleting joined lines
2082 op.tp_flags = prop.tp_flags;
2083 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2084 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002085 }
2086 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002087 if (!found)
2088 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002089 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002090 }
2091}
2092
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002093#endif // FEAT_PROP_POPUP