blob: 1df4840f207e2ae07b49608c1de46bed7bce6bfd [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
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010017
18/*
19 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
20 * This avoids adding a pointer to the hashtable item.
21 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
22 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
23 * HI2PT() converts a hashitem pointer to a proptype pointer.
24 */
25#define PT2HIKEY(p) ((p)->pt_name)
26#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
27#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
28
29// The global text property types.
30static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010031static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33// The last used text property type ID.
34static int proptype_id = 0;
35
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036/*
37 * Find a property type by name, return the hashitem.
38 * Returns NULL if the item can't be found.
39 */
40 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010041find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010042{
43 hashtab_T *ht;
44 hashitem_T *hi;
45
46 if (*name == NUL)
47 return NULL;
48 if (buf == NULL)
49 ht = global_proptypes;
50 else
51 ht = buf->b_proptypes;
52
53 if (ht == NULL)
54 return NULL;
55 hi = hash_find(ht, name);
56 if (HASHITEM_EMPTY(hi))
57 return NULL;
58 return hi;
59}
60
61/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010062 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010063 */
64 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010065find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010066{
Bram Moolenaare44336b2022-08-07 18:20:08 +010067 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010068
69 if (hi == NULL)
70 return NULL;
71 return HI2PT(hi);
72}
73
74/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020075 * Get the prop type ID of "name".
76 * When not found return zero.
77 */
78 int
79find_prop_type_id(char_u *name, buf_T *buf)
80{
Bram Moolenaare44336b2022-08-07 18:20:08 +010081 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020082
83 if (pt == NULL)
84 return 0;
85 return pt->pt_id;
86}
87
88/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089 * Lookup a property type by name. First in "buf" and when not found in the
90 * global types.
91 * When not found gives an error message and returns NULL.
92 */
93 static proptype_T *
94lookup_prop_type(char_u *name, buf_T *buf)
95{
Bram Moolenaare44336b2022-08-07 18:20:08 +010096 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097
98 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +010099 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100101 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100152 if (argvars[2].v_type != VAR_DICT)
153 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000154 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100155 return;
156 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100158 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
159 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200160}
161
162/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200163 * Attach a text property 'type_name' to the text starting
164 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100165 * the buffer "buf" and assign identifier "id".
166 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200167 */
168 static int
169prop_add_one(
170 buf_T *buf,
171 char_u *type_name,
172 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100173 char_u *text_arg,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100174 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200175 linenr_T start_lnum,
176 linenr_T end_lnum,
177 colnr_T start_col,
178 colnr_T end_col)
179{
180 proptype_T *type;
181 linenr_T lnum;
182 int proplen;
183 char_u *props = NULL;
184 char_u *newprops;
185 size_t textlen;
186 char_u *newtext;
187 int i;
188 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100189 char_u *text = text_arg;
190 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200191
192 type = lookup_prop_type(type_name, buf);
193 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100194 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200195
196 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
197 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000198 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100199 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200200 }
201 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
202 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000203 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100204 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200205 }
206
207 if (buf->b_ml.ml_mfp == NULL)
208 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000209 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100210 goto theend;
211 }
212
213 if (text != NULL)
214 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100215 garray_T *gap = &buf->b_textprop_text;
216 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100217
218 // double check we got the right ID
219 if (-id - 1 != gap->ga_len)
220 iemsg("text prop ID mismatch");
221 if (gap->ga_growsize == 0)
222 ga_init2(gap, sizeof(char *), 50);
223 if (ga_grow(gap, 1) == FAIL)
224 goto theend;
225 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100226
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100227 // change any control character (Tab, Newline, etc.) to a Space to make
228 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100230 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100231 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100232 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200233 }
234
235 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
236 {
237 colnr_T col; // start column
238 long length; // in bytes
239
240 // Fetch the line to get the ml_line_len field updated.
241 proplen = get_text_props(buf, lnum, &props, TRUE);
242 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
243
244 if (lnum == start_lnum)
245 col = start_col;
246 else
247 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100248 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200249 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000250 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100251 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200252 }
253
254 if (lnum == end_lnum)
255 length = end_col - col;
256 else
257 length = (int)textlen - col + 1;
258 if (length > (long)textlen)
259 length = (int)textlen; // can include the end-of-line
260 if (length < 0)
261 length = 0; // zero-width property
262
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100263 if (text_arg != NULL)
264 {
265 length = 1; // text is placed on one character
266 if (col == 0)
267 col = MAXCOL; // after the line
268 }
269
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200270 // Allocate the new line with space for the new property.
271 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
272 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100273 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200274 // Copy the text, including terminating NUL.
275 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
276
277 // Find the index where to insert the new property.
278 // Since the text properties are not aligned properly when stored with
279 // the text, we need to copy them as bytes before using it as a struct.
280 for (i = 0; i < proplen; ++i)
281 {
282 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
283 sizeof(textprop_T));
284 if (tmp_prop.tp_col >= col)
285 break;
286 }
287 newprops = newtext + textlen;
288 if (i > 0)
289 mch_memmove(newprops, props, sizeof(textprop_T) * i);
290
291 tmp_prop.tp_col = col;
292 tmp_prop.tp_len = length;
293 tmp_prop.tp_id = id;
294 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100295 tmp_prop.tp_flags = text_flags
296 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100297 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
298 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
299 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200300 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
301 sizeof(textprop_T));
302
303 if (i < proplen)
304 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
305 props + i * sizeof(textprop_T),
306 sizeof(textprop_T) * (proplen - i));
307
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100308 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200309 vim_free(buf->b_ml.ml_line_ptr);
310 buf->b_ml.ml_line_ptr = newtext;
311 buf->b_ml.ml_line_len += sizeof(textprop_T);
312 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
313 }
314
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100315 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200316 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100317 res = OK;
318
319theend:
320 vim_free(text);
321 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200322}
323
324/*
325 * prop_add_list()
326 * First argument specifies the text property:
327 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
328 * Second argument is a List where each item is a List with the following
329 * entries: [lnum, start_col, end_col]
330 */
331 void
332f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
333{
334 dict_T *dict;
335 char_u *type_name;
336 buf_T *buf = curbuf;
337 int id = 0;
338 listitem_T *li;
339 list_T *pos_list;
340 linenr_T start_lnum;
341 colnr_T start_col;
342 linenr_T end_lnum;
343 colnr_T end_col;
344 int error = FALSE;
345
346 if (check_for_dict_arg(argvars, 0) == FAIL
347 || check_for_list_arg(argvars, 1) == FAIL)
348 return;
349
350 if (argvars[1].vval.v_list == NULL)
351 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000352 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200353 return;
354 }
355
356 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100357 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200358 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000359 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200360 return;
361 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100362 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100364 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100365 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200366
367 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
368 return;
369
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100370 // This must be done _before_ we start adding properties because property
371 // changes trigger buffer (memline) reorganisation, which needs this flag
372 // to be correctly set.
373 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200374 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
375 {
376 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
377 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000378 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200379 return;
380 }
381
382 pos_list = li->li_tv.vval.v_list;
383 start_lnum = list_find_nr(pos_list, 0L, &error);
384 start_col = list_find_nr(pos_list, 1L, &error);
385 end_lnum = list_find_nr(pos_list, 2L, &error);
386 end_col = list_find_nr(pos_list, 3L, &error);
387 if (error || start_lnum <= 0 || start_col <= 0
388 || end_lnum <= 0 || end_col <= 0)
389 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000390 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200391 return;
392 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100393 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200394 start_col, end_col) == FAIL)
395 return;
396 }
397
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100398 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200399}
400
401/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100402 * Get the next ID to use for a textprop with text in buffer "buf".
403 */
404 static int
405get_textprop_id(buf_T *buf)
406{
407 // TODO: recycle deleted entries
408 return -(buf->b_textprop_text.ga_len + 1);
409}
410
411/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200412 * Shared between prop_add() and popup_create().
413 * "dict_arg" is the function argument of a dict containing "bufnr".
414 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100415 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200416 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100417 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200418prop_add_common(
419 linenr_T start_lnum,
420 colnr_T start_col,
421 dict_T *dict,
422 buf_T *default_buf,
423 typval_T *dict_arg)
424{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200425 linenr_T end_lnum;
426 colnr_T end_col;
427 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200428 buf_T *buf = default_buf;
429 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100430 char_u *text = NULL;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100431 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100432
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100433 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100434 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000435 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100436 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100437 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100438 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100439
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100440 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100441 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100442 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100443 if (end_lnum < start_lnum)
444 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000445 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100446 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100447 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100448 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100449 else
450 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100451
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100452 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100453 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100454 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100455
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100456 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100457 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000458 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100459 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100460 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100461 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100462 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100463 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100464 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100465 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100466 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100467 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000468 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100469 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100470 }
471 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100472 else if (start_lnum == end_lnum)
473 end_col = start_col;
474 else
475 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100476
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100477 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100478 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100479
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100480 if (dict_has_key(dict, "text"))
481 {
482 text = dict_get_string(dict, "text", TRUE);
483 if (text == NULL)
484 goto theend;
485 // use a default length of 1 to make multiple props show up
486 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100487
488 if (dict_has_key(dict, "text_align"))
489 {
490 char_u *p = dict_get_string(dict, "text_align", FALSE);
491
492 if (p == NULL)
493 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100494 if (start_col != 0)
495 {
496 emsg(_(e_can_only_use_text_align_when_column_is_zero));
497 goto theend;
498 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100499 if (STRCMP(p, "right") == 0)
500 flags |= TP_FLAG_ALIGN_RIGHT;
501 else if (STRCMP(p, "below") == 0)
502 flags |= TP_FLAG_ALIGN_BELOW;
503 else if (STRCMP(p, "after") != 0)
504 {
505 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
506 goto theend;
507 }
508 }
509
510 if (dict_has_key(dict, "text_wrap"))
511 {
512 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
513 if (p == NULL)
514 goto theend;
515 if (STRCMP(p, "wrap") == 0)
516 flags |= TP_FLAG_WRAP;
517 else if (STRCMP(p, "truncate") != 0)
518 {
519 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
520 goto theend;
521 }
522 }
523 }
524
525 // Column must be 1 or more for a normal text property; when "text" is
526 // present zero means it goes after the line.
527 if (start_col < (text == NULL ? 1 : 0))
528 {
529 semsg(_(e_invalid_column_number_nr), (long)start_col);
530 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100531 }
532
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200533 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100534 goto theend;
535
536 if (id < 0 && buf->b_textprop_text.ga_len > 0)
537 {
538 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
539 goto theend;
540 }
541 if (text != NULL)
542 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100543
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100544 // This must be done _before_ we add the property because property changes
545 // trigger buffer (memline) reorganisation, which needs this flag to be
546 // correctly set.
547 buf->b_has_textprop = TRUE; // this is never reset
548
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100549 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100550 start_lnum, end_lnum, start_col, end_col);
551 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100552
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100553 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100554
555theend:
556 vim_free(text);
557 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100558}
559
560/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100561 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100562 * Returns the number of text properties and, when non-zero, a pointer to the
563 * first one in "props" (note that it is not aligned, therefore the char_u
564 * pointer).
565 */
566 int
567get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
568{
569 char_u *text;
570 size_t textlen;
571 size_t proplen;
572
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100573 // Be quick when no text property types have been defined or the buffer,
574 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200575 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100576 return 0;
577
578 // Fetch the line to get the ml_line_len field updated.
579 text = ml_get_buf(buf, lnum, will_change);
580 textlen = STRLEN(text) + 1;
581 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100582 if (proplen == 0)
583 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100584 if (proplen % sizeof(textprop_T) != 0)
585 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000586 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100587 return 0;
588 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100589 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100590 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100591}
592
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100593/*
594 * Return the number of text properties with "below" alignment in line "lnum".
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100595 * A "right" aligned property also goes below after a "below" or other "right"
596 * aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100597 */
598 int
599prop_count_below(buf_T *buf, linenr_T lnum)
600{
601 char_u *props;
602 int count = get_text_props(buf, lnum, &props, FALSE);
603 int result = 0;
604 textprop_T prop;
605 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100606 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100607
608 if (count == 0)
609 return 0;
610 for (i = 0; i < count; ++i)
611 {
612 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100613 if (prop.tp_col == MAXCOL)
614 {
615 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
616 || (next_right_goes_below
617 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
618 {
619 next_right_goes_below = TRUE;
620 ++result;
621 }
622 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
623 next_right_goes_below = TRUE;
624 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100625 }
626 return result;
627}
628
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200629/**
630 * Return the number of text properties on line "lnum" in the current buffer.
631 * When "only_starting" is true only text properties starting in this line will
632 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100633 * When "last_line" is FALSE then text properties after the line are not
634 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200635 */
636 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100637count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200638{
639 char_u *props;
640 int proplen = get_text_props(curbuf, lnum, &props, 0);
641 int result = proplen;
642 int i;
643 textprop_T prop;
644
Bram Moolenaare175dc62022-08-01 22:18:50 +0100645 for (i = 0; i < proplen; ++i)
646 {
647 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100648 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100649 // previous line, or when not in the last line and it is virtual text
650 // after the line.
651 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
652 || (!last_line && prop.tp_col == MAXCOL))
653 --result;
654 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200655 return result;
656}
657
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100658/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200659 * Find text property "type_id" in the visible lines of window "wp".
660 * Match "id" when it is > 0.
661 * Returns FAIL when not found.
662 */
663 int
664find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
665 linenr_T *found_lnum)
666{
667 linenr_T lnum;
668 char_u *props;
669 int count;
670 int i;
671
672 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100673 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200674 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
675 {
676 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
677 for (i = 0; i < count; ++i)
678 {
679 mch_memmove(prop, props + i * sizeof(textprop_T),
680 sizeof(textprop_T));
681 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
682 {
683 *found_lnum = lnum;
684 return OK;
685 }
686 }
687 }
688 return FAIL;
689}
690
691/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100692 * Set the text properties for line "lnum" to "props" with length "len".
693 * If "len" is zero text properties are removed, "props" is not used.
694 * Any existing text properties are dropped.
695 * Only works for the current buffer.
696 */
697 static void
698set_text_props(linenr_T lnum, char_u *props, int len)
699{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100700 char_u *text;
701 char_u *newtext;
702 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100703
704 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100705 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100706 newtext = alloc(textlen + len);
707 if (newtext == NULL)
708 return;
709 mch_memmove(newtext, text, textlen);
710 if (len > 0)
711 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100712 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100713 vim_free(curbuf->b_ml.ml_line_ptr);
714 curbuf->b_ml.ml_line_ptr = newtext;
715 curbuf->b_ml.ml_line_len = textlen + len;
716 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
717}
718
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100719/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100720 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100721 */
722 void
723add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
724{
725 char_u *text;
726 char_u *newtext;
727 int proplen = text_prop_count * (int)sizeof(textprop_T);
728
729 text = ml_get(lnum);
730 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
731 if (newtext == NULL)
732 return;
733 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
734 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
735 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
736 vim_free(curbuf->b_ml.ml_line_ptr);
737 curbuf->b_ml.ml_line_ptr = newtext;
738 curbuf->b_ml.ml_line_len += proplen;
739 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
740}
741
Bram Moolenaare44336b2022-08-07 18:20:08 +0100742/*
743 * Function passed to qsort() for sorting proptype_T on pt_id.
744 */
745 static int
746compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100747{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100748 proptype_T *tp1 = *(proptype_T **)s1;
749 proptype_T *tp2 = *(proptype_T **)s2;
750
751 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
752}
753
754 static proptype_T *
755find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
756{
757 int low = 0;
758 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100759
Bram Moolenaar10246902022-08-08 17:08:05 +0100760 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100761 return NULL;
762
Bram Moolenaare44336b2022-08-07 18:20:08 +0100763 // Make the loopup faster by creating an array with pointers to
764 // hashtable entries, sorted on pt_id.
765 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100766 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100767 long todo;
768 hashitem_T *hi;
769 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100770
Bram Moolenaare44336b2022-08-07 18:20:08 +0100771 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
772 if (*array == NULL)
773 return NULL;
774 todo = (long)ht->ht_used;
775 for (hi = ht->ht_array; todo > 0; ++hi)
776 {
777 if (!HASHITEM_EMPTY(hi))
778 {
779 (*array)[i++] = HI2PT(hi);
780 --todo;
781 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100782 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100783 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
784 }
785
786 // binary search in the sorted array
787 high = ht->ht_used;
788 while (high > low)
789 {
790 int m = (high + low) / 2;
791
792 if ((*array)[m]->pt_id == id)
793 return (*array)[m];
794 if ((*array)[m]->pt_id > id)
795 high = m;
796 else
797 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100798 }
799 return NULL;
800}
801
802/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100803 * Fill 'dict' with text properties in 'prop'.
804 */
805 static void
806prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
807{
808 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200809 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100810
811 dict_add_number(dict, "col", prop->tp_col);
812 dict_add_number(dict, "length", prop->tp_len);
813 dict_add_number(dict, "id", prop->tp_id);
814 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
815 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200816
Bram Moolenaare44336b2022-08-07 18:20:08 +0100817 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200818 if (pt == NULL)
819 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100820 pt = find_type_by_id(global_proptypes, &global_proparray,
821 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200822 buflocal = FALSE;
823 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100824 if (pt != NULL)
825 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200826
827 if (buflocal)
828 dict_add_number(dict, "type_bufnr", buf->b_fnum);
829 else
830 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100831}
832
833/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100834 * Find a property type by ID in "buf" or globally.
835 * Returns NULL if not found.
836 */
837 proptype_T *
838text_prop_type_by_id(buf_T *buf, int id)
839{
840 proptype_T *type;
841
Bram Moolenaare44336b2022-08-07 18:20:08 +0100842 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100843 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100844 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100845 return type;
846}
847
848/*
849 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
850 */
851 void
852f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
853{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200854 linenr_T start;
855 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100856 linenr_T lnum;
857 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100858 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100859
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200860 if (in_vim9script()
861 && (check_for_number_arg(argvars, 0) == FAIL
862 || check_for_opt_number_arg(argvars, 1) == FAIL
863 || (argvars[1].v_type != VAR_UNKNOWN
864 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
865 return;
866
867 start = tv_get_number(&argvars[0]);
868 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100869 if (argvars[1].v_type != VAR_UNKNOWN)
870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100871 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100872 if (argvars[2].v_type != VAR_UNKNOWN)
873 {
874 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
875 return;
876 }
877 }
878 if (start < 1 || end < 1)
879 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200880 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100881 return;
882 }
883
884 for (lnum = start; lnum <= end; ++lnum)
885 {
886 char_u *text;
887 size_t len;
888
889 if (lnum > buf->b_ml.ml_line_count)
890 break;
891 text = ml_get_buf(buf, lnum, FALSE);
892 len = STRLEN(text) + 1;
893 if ((size_t)buf->b_ml.ml_line_len > len)
894 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100895 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100896 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
897 {
898 char_u *newtext = vim_strsave(text);
899
900 // need to allocate the line now
901 if (newtext == NULL)
902 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100903 if (buf->b_ml.ml_flags & ML_ALLOCATED)
904 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100905 buf->b_ml.ml_line_ptr = newtext;
906 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
907 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100908 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100909 }
910 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100911 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100912 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100913}
914
915/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100916 * prop_find({props} [, {direction}])
917 */
918 void
919f_prop_find(typval_T *argvars, typval_T *rettv)
920{
921 pos_T *cursor = &curwin->w_cursor;
922 dict_T *dict;
923 buf_T *buf = curbuf;
924 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200925 int lnum_start;
926 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100927 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200928 int id = 0;
929 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200930 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100931 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200932 int lnum = -1;
933 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100934 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100935 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100936
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200937 if (in_vim9script()
938 && (check_for_dict_arg(argvars, 0) == FAIL
939 || check_for_opt_string_arg(argvars, 1) == FAIL))
940 return;
941
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100942 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
943 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000944 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100945 return;
946 }
947 dict = argvars[0].vval.v_dict;
948
949 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
950 return;
951 if (buf->b_ml.ml_mfp == NULL)
952 return;
953
954 if (argvars[1].v_type != VAR_UNKNOWN)
955 {
956 char_u *dir_s = tv_get_string(&argvars[1]);
957
958 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100959 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100960 else if (*dir_s != 'f')
961 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000962 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100963 return;
964 }
965 }
966
967 di = dict_find(dict, (char_u *)"lnum", -1);
968 if (di != NULL)
969 lnum = tv_get_number(&di->di_tv);
970
971 di = dict_find(dict, (char_u *)"col", -1);
972 if (di != NULL)
973 col = tv_get_number(&di->di_tv);
974
975 if (lnum == -1)
976 {
977 lnum = cursor->lnum;
978 col = cursor->col + 1;
979 }
980 else if (col == -1)
981 col = 1;
982
983 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
984 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200985 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100986 return;
987 }
988
Bram Moolenaard61efa52022-07-23 09:52:04 +0100989 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100990
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100991 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200992 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100993 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200994 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200995 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100996 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100997 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100998 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100999 proptype_T *type = lookup_prop_type(name, buf);
1000
1001 if (type == NULL)
1002 return;
1003 type_id = type->pt_id;
1004 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001005 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001006 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001007 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001008 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001009 return;
1010 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001011 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001012 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001013 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001014 return;
1015 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001016
1017 lnum_start = lnum;
1018
1019 if (rettv_dict_alloc(rettv) == FAIL)
1020 return;
1021
1022 while (1)
1023 {
1024 char_u *text = ml_get_buf(buf, lnum, FALSE);
1025 size_t textlen = STRLEN(text) + 1;
1026 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001027 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001028 int i;
1029 textprop_T prop;
1030 int prop_start;
1031 int prop_end;
1032
LemonBoy9bd3ce22022-04-18 21:54:02 +01001033 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001034 {
1035 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001036 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001037
LemonBoy9bd3ce22022-04-18 21:54:02 +01001038 // For the very first line try to find the first property before or
1039 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001040 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001041 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001042 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001043 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001044 if (prop.tp_col > col)
1045 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001046 }
1047 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1048 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001049 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001050 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001051 : (id_found && prop.tp_id == id)
1052 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001053 {
1054 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001055 if (lnum_start == lnum
1056 && col >= prop.tp_col
1057 && (col <= prop.tp_col + prop.tp_len
1058 - (prop.tp_len != 0)))
1059 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001060
LemonBoy9bd3ce22022-04-18 21:54:02 +01001061 // The property was not continued from last line, it starts on
1062 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001063 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001064 // The property does not continue on the next line, it ends on
1065 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001066 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001067 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001068 seen_end = 1;
1069
1070 // Skip lines without the start flag.
1071 if (!prop_start)
1072 {
1073 // Always search backwards for start when search started
1074 // on a prop and we're not skipping.
1075 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001076 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001077 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001078 }
1079
1080 // If skipstart is true, skip the prop at start pos (even if
1081 // continued from another line).
1082 if (start_pos_has_prop && skipstart && !seen_end)
1083 {
1084 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001085 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001086 }
1087
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001088 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1089 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1090
1091 return;
1092 }
1093 }
1094
1095 if (dir > 0)
1096 {
1097 if (lnum >= buf->b_ml.ml_line_count)
1098 break;
1099 lnum++;
1100 }
1101 else
1102 {
1103 if (lnum <= 1)
1104 break;
1105 lnum--;
1106 }
1107 }
1108}
1109
1110/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001111 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1112 */
1113 static int
1114prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1115{
1116 int i;
1117
1118 for (i = 0; i < len; i++)
1119 if (types_or_ids[i] == type_or_id)
1120 return TRUE;
1121
1122 return FALSE;
1123}
1124
1125/*
1126 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1127 * If 'prop_types' is not NULL, then return only the text properties with
1128 * matching property type in the 'prop_types' array.
1129 * If 'prop_ids' is not NULL, then return only the text properties with
1130 * an identifier in the 'props_ids' array.
1131 * If 'add_lnum' is TRUE, then add the line number also to the text property
1132 * dictionary.
1133 */
1134 static void
1135get_props_in_line(
1136 buf_T *buf,
1137 linenr_T lnum,
1138 int *prop_types,
1139 int prop_types_len,
1140 int *prop_ids,
1141 int prop_ids_len,
1142 list_T *retlist,
1143 int add_lnum)
1144{
1145 char_u *text = ml_get_buf(buf, lnum, FALSE);
1146 size_t textlen = STRLEN(text) + 1;
1147 int count;
1148 int i;
1149 textprop_T prop;
1150
1151 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1152 for (i = 0; i < count; ++i)
1153 {
1154 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1155 sizeof(textprop_T));
1156 if ((prop_types == NULL
1157 || prop_type_or_id_in_list(prop_types, prop_types_len,
1158 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001159 && (prop_ids == NULL
1160 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1161 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001162 {
1163 dict_T *d = dict_alloc();
1164
1165 if (d == NULL)
1166 break;
1167 prop_fill_dict(d, &prop, buf);
1168 if (add_lnum)
1169 dict_add_number(d, "lnum", lnum);
1170 list_append_dict(retlist, d);
1171 }
1172 }
1173}
1174
1175/*
1176 * Convert a List of property type names into an array of property type
1177 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1178 * error. 'num_types' is set to the number of returned property types.
1179 */
1180 static int *
1181get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1182{
1183 int *prop_types;
1184 listitem_T *li;
1185 int i;
1186 char_u *name;
1187 proptype_T *type;
1188
1189 *num_types = 0;
1190
1191 prop_types = ALLOC_MULT(int, list_len(l));
1192 if (prop_types == NULL)
1193 return NULL;
1194
1195 i = 0;
1196 FOR_ALL_LIST_ITEMS(l, li)
1197 {
1198 if (li->li_tv.v_type != VAR_STRING)
1199 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001200 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001201 goto errret;
1202 }
1203 name = li->li_tv.vval.v_string;
1204 if (name == NULL)
1205 goto errret;
1206
1207 type = lookup_prop_type(name, buf);
1208 if (type == NULL)
1209 goto errret;
1210 prop_types[i++] = type->pt_id;
1211 }
1212
1213 *num_types = i;
1214 return prop_types;
1215
1216errret:
1217 VIM_CLEAR(prop_types);
1218 return NULL;
1219}
1220
1221/*
1222 * Convert a List of property identifiers into an array of property
1223 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1224 * error. 'num_ids' is set to the number of returned property identifiers.
1225 */
1226 static int *
1227get_prop_ids_from_list(list_T *l, int *num_ids)
1228{
1229 int *prop_ids;
1230 listitem_T *li;
1231 int i;
1232 int id;
1233 int error;
1234
1235 *num_ids = 0;
1236
1237 prop_ids = ALLOC_MULT(int, list_len(l));
1238 if (prop_ids == NULL)
1239 return NULL;
1240
1241 i = 0;
1242 FOR_ALL_LIST_ITEMS(l, li)
1243 {
1244 error = FALSE;
1245 id = tv_get_number_chk(&li->li_tv, &error);
1246 if (error)
1247 goto errret;
1248
1249 prop_ids[i++] = id;
1250 }
1251
1252 *num_ids = i;
1253 return prop_ids;
1254
1255errret:
1256 VIM_CLEAR(prop_ids);
1257 return NULL;
1258}
1259
1260/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001261 * prop_list({lnum} [, {bufnr}])
1262 */
1263 void
1264f_prop_list(typval_T *argvars, typval_T *rettv)
1265{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001266 linenr_T lnum;
1267 linenr_T start_lnum;
1268 linenr_T end_lnum;
1269 buf_T *buf = curbuf;
1270 int add_lnum = FALSE;
1271 int *prop_types = NULL;
1272 int prop_types_len = 0;
1273 int *prop_ids = NULL;
1274 int prop_ids_len = 0;
1275 list_T *l;
1276 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001277
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001278 if (in_vim9script()
1279 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001280 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001281 return;
1282
Bram Moolenaar93a10962022-06-16 11:42:09 +01001283 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001284 return;
1285
1286 // default: get text properties on current line
1287 start_lnum = tv_get_number(&argvars[0]);
1288 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001289 if (argvars[1].v_type != VAR_UNKNOWN)
1290 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001291 dict_T *d;
1292
1293 if (argvars[1].v_type != VAR_DICT)
1294 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001295 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001296 return;
1297 }
1298 d = argvars[1].vval.v_dict;
1299
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001300 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1301 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001302
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001303 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001304 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001305 if (di->di_tv.v_type != VAR_NUMBER)
1306 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001307 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001308 return;
1309 }
1310 end_lnum = tv_get_number(&di->di_tv);
1311 if (end_lnum < 0)
1312 // negative end_lnum is used as an offset from the last buffer
1313 // line
1314 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1315 else if (end_lnum > buf->b_ml.ml_line_count)
1316 end_lnum = buf->b_ml.ml_line_count;
1317 add_lnum = TRUE;
1318 }
1319 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1320 {
1321 if (di->di_tv.v_type != VAR_LIST)
1322 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001323 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001324 return;
1325 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001326
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001327 l = di->di_tv.vval.v_list;
1328 if (l != NULL && list_len(l) > 0)
1329 {
1330 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1331 if (prop_types == NULL)
1332 return;
1333 }
1334 }
1335 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1336 {
1337 if (di->di_tv.v_type != VAR_LIST)
1338 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001339 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001340 goto errret;
1341 }
1342
1343 l = di->di_tv.vval.v_list;
1344 if (l != NULL && list_len(l) > 0)
1345 {
1346 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1347 if (prop_ids == NULL)
1348 goto errret;
1349 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001350 }
1351 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001352 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1353 || end_lnum < 1 || end_lnum < start_lnum)
1354 emsg(_(e_invalid_range));
1355 else
1356 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1357 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1358 prop_ids, prop_ids_len,
1359 rettv->vval.v_list, add_lnum);
1360
1361errret:
1362 VIM_CLEAR(prop_types);
1363 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001364}
1365
1366/*
1367 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1368 */
1369 void
1370f_prop_remove(typval_T *argvars, typval_T *rettv)
1371{
1372 linenr_T start = 1;
1373 linenr_T end = 0;
1374 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001375 linenr_T first_changed = 0;
1376 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001377 dict_T *dict;
1378 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001379 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001380 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001381 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001382 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001383 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001384
1385 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001386
1387 if (in_vim9script()
1388 && (check_for_dict_arg(argvars, 0) == FAIL
1389 || check_for_opt_number_arg(argvars, 1) == FAIL
1390 || (argvars[1].v_type != VAR_UNKNOWN
1391 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1392 return;
1393
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001394 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1395 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001396 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001397 return;
1398 }
1399
1400 if (argvars[1].v_type != VAR_UNKNOWN)
1401 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001402 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001403 end = start;
1404 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001405 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001406 if (start < 1 || end < 1)
1407 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001408 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001409 return;
1410 }
1411 }
1412
1413 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001414 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1415 return;
1416 if (buf->b_ml.ml_mfp == NULL)
1417 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001418
Bram Moolenaard61efa52022-07-23 09:52:04 +01001419 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001420
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001421 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001422 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001423 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001424 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001425 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001426 proptype_T *type = lookup_prop_type(name, buf);
1427
1428 if (type == NULL)
1429 return;
1430 type_id = type->pt_id;
1431 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001432 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001433
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001434 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001435 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001436 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001437 return;
1438 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001439 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001440 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001441 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001442 return;
1443 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001444
1445 if (end == 0)
1446 end = buf->b_ml.ml_line_count;
1447 for (lnum = start; lnum <= end; ++lnum)
1448 {
1449 char_u *text;
1450 size_t len;
1451
1452 if (lnum > buf->b_ml.ml_line_count)
1453 break;
1454 text = ml_get_buf(buf, lnum, FALSE);
1455 len = STRLEN(text) + 1;
1456 if ((size_t)buf->b_ml.ml_line_len > len)
1457 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001458 static textprop_T textprop; // static because of alignment
1459 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001460
1461 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1462 / sizeof(textprop_T); ++idx)
1463 {
1464 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1465 + idx * sizeof(textprop_T);
1466 size_t taillen;
1467
1468 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001469 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1470 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001471 {
1472 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1473 {
1474 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1475
1476 // need to allocate the line to be able to change it
1477 if (newptr == NULL)
1478 return;
1479 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1480 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001481 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1482 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001483 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001484 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1485
1486 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001487 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001488 }
1489
1490 taillen = buf->b_ml.ml_line_len - len
1491 - (idx + 1) * sizeof(textprop_T);
1492 if (taillen > 0)
1493 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1494 taillen);
1495 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1496 --idx;
1497
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001498 if (textprop.tp_id < 0)
1499 {
1500 garray_T *gap = &buf->b_textprop_text;
1501 int ii = -textprop.tp_id - 1;
1502
1503 // negative ID: property with text - free the text
1504 if (ii < gap->ga_len)
1505 {
1506 char_u **p = ((char_u **)gap->ga_data) + ii;
1507 vim_free(*p);
1508 *p = NULL;
1509 did_remove_text = TRUE;
1510 }
1511 }
1512
Bram Moolenaar965c0442021-05-17 00:22:06 +02001513 if (first_changed == 0)
1514 first_changed = lnum;
1515 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001516 ++rettv->vval.v_number;
1517 if (!do_all)
1518 break;
1519 }
1520 }
1521 }
1522 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001523
Bram Moolenaar965c0442021-05-17 00:22:06 +02001524 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001525 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001526 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001527 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001528 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001529 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001530
1531 if (did_remove_text)
1532 {
1533 garray_T *gap = &buf->b_textprop_text;
1534
1535 // Reduce the growarray size for NULL pointers at the end.
1536 while (gap->ga_len > 0
1537 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1538 --gap->ga_len;
1539 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001540}
1541
1542/*
1543 * Common for f_prop_type_add() and f_prop_type_change().
1544 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001545 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001546prop_type_set(typval_T *argvars, int add)
1547{
1548 char_u *name;
1549 buf_T *buf = NULL;
1550 dict_T *dict;
1551 dictitem_T *di;
1552 proptype_T *prop;
1553
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001554 if (in_vim9script()
1555 && (check_for_string_arg(argvars, 0) == FAIL
1556 || check_for_dict_arg(argvars, 1) == FAIL))
1557 return;
1558
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001559 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001560 if (*name == NUL)
1561 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001562 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001563 return;
1564 }
1565
1566 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1567 return;
1568 dict = argvars[1].vval.v_dict;
1569
Bram Moolenaare44336b2022-08-07 18:20:08 +01001570 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001571 if (add)
1572 {
1573 hashtab_T **htp;
1574
1575 if (prop != NULL)
1576 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001577 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001578 return;
1579 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001580 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001581 if (prop == NULL)
1582 return;
1583 STRCPY(prop->pt_name, name);
1584 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001585 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001586 if (buf == NULL)
1587 {
1588 htp = &global_proptypes;
1589 VIM_CLEAR(global_proparray);
1590 }
1591 else
1592 {
1593 htp = &buf->b_proptypes;
1594 VIM_CLEAR(buf->b_proparray);
1595 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001596 if (*htp == NULL)
1597 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001598 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001599 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001600 {
1601 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001602 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001603 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001604 hash_init(*htp);
1605 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001606 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001607 }
1608 else
1609 {
1610 if (prop == NULL)
1611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001612 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001613 return;
1614 }
1615 }
1616
1617 if (dict != NULL)
1618 {
1619 di = dict_find(dict, (char_u *)"highlight", -1);
1620 if (di != NULL)
1621 {
1622 char_u *highlight;
1623 int hl_id = 0;
1624
Bram Moolenaard61efa52022-07-23 09:52:04 +01001625 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001626 if (highlight != NULL && *highlight != NUL)
1627 hl_id = syn_name2id(highlight);
1628 if (hl_id <= 0)
1629 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001630 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001631 highlight == NULL ? (char_u *)"" : highlight);
1632 return;
1633 }
1634 prop->pt_hl_id = hl_id;
1635 }
1636
Bram Moolenaar58187f12019-05-05 16:33:47 +02001637 di = dict_find(dict, (char_u *)"combine", -1);
1638 if (di != NULL)
1639 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001640 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001641 prop->pt_flags |= PT_FLAG_COMBINE;
1642 else
1643 prop->pt_flags &= ~PT_FLAG_COMBINE;
1644 }
1645
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001646 di = dict_find(dict, (char_u *)"override", -1);
1647 if (di != NULL)
1648 {
1649 if (tv_get_bool(&di->di_tv))
1650 prop->pt_flags |= PT_FLAG_OVERRIDE;
1651 else
1652 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1653 }
1654
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001655 di = dict_find(dict, (char_u *)"priority", -1);
1656 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001657 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001658
1659 di = dict_find(dict, (char_u *)"start_incl", -1);
1660 if (di != NULL)
1661 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001662 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001663 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1664 else
1665 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1666 }
1667
1668 di = dict_find(dict, (char_u *)"end_incl", -1);
1669 if (di != NULL)
1670 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001671 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001672 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1673 else
1674 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1675 }
1676 }
1677}
1678
1679/*
1680 * prop_type_add({name}, {props})
1681 */
1682 void
1683f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1684{
1685 prop_type_set(argvars, TRUE);
1686}
1687
1688/*
1689 * prop_type_change({name}, {props})
1690 */
1691 void
1692f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1693{
1694 prop_type_set(argvars, FALSE);
1695}
1696
1697/*
1698 * prop_type_delete({name} [, {bufnr}])
1699 */
1700 void
1701f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1702{
1703 char_u *name;
1704 buf_T *buf = NULL;
1705 hashitem_T *hi;
1706
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001707 if (in_vim9script()
1708 && (check_for_string_arg(argvars, 0) == FAIL
1709 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1710 return;
1711
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001712 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001713 if (*name == NUL)
1714 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001715 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001716 return;
1717 }
1718
1719 if (argvars[1].v_type != VAR_UNKNOWN)
1720 {
1721 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1722 return;
1723 }
1724
Bram Moolenaare44336b2022-08-07 18:20:08 +01001725 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001726 if (hi != NULL)
1727 {
1728 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001729 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001730
1731 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001732 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001733 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001734 VIM_CLEAR(global_proparray);
1735 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001736 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001737 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001738 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001739 VIM_CLEAR(buf->b_proparray);
1740 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001741 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001742 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001743 }
1744}
1745
1746/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001747 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001748 */
1749 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001750f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001751{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001752 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001753
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001754 if (in_vim9script()
1755 && (check_for_string_arg(argvars, 0) == FAIL
1756 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1757 return;
1758
1759 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001760 if (*name == NUL)
1761 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001762 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001763 return;
1764 }
1765 if (rettv_dict_alloc(rettv) == OK)
1766 {
1767 proptype_T *prop = NULL;
1768 buf_T *buf = NULL;
1769
1770 if (argvars[1].v_type != VAR_UNKNOWN)
1771 {
1772 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1773 return;
1774 }
1775
Bram Moolenaare44336b2022-08-07 18:20:08 +01001776 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001777 if (prop != NULL)
1778 {
1779 dict_T *d = rettv->vval.v_dict;
1780
1781 if (prop->pt_hl_id > 0)
1782 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1783 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001784 dict_add_number(d, "combine",
1785 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001786 dict_add_number(d, "start_incl",
1787 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1788 dict_add_number(d, "end_incl",
1789 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1790 if (buf != NULL)
1791 dict_add_number(d, "bufnr", buf->b_fnum);
1792 }
1793 }
1794}
1795
1796 static void
1797list_types(hashtab_T *ht, list_T *l)
1798{
1799 long todo;
1800 hashitem_T *hi;
1801
1802 todo = (long)ht->ht_used;
1803 for (hi = ht->ht_array; todo > 0; ++hi)
1804 {
1805 if (!HASHITEM_EMPTY(hi))
1806 {
1807 proptype_T *prop = HI2PT(hi);
1808
1809 list_append_string(l, prop->pt_name, -1);
1810 --todo;
1811 }
1812 }
1813}
1814
1815/*
1816 * prop_type_list([{bufnr}])
1817 */
1818 void
1819f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1820{
1821 buf_T *buf = NULL;
1822
1823 if (rettv_list_alloc(rettv) == OK)
1824 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001825 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1826 return;
1827
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001828 if (argvars[0].v_type != VAR_UNKNOWN)
1829 {
1830 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1831 return;
1832 }
1833 if (buf == NULL)
1834 {
1835 if (global_proptypes != NULL)
1836 list_types(global_proptypes, rettv->vval.v_list);
1837 }
1838 else if (buf->b_proptypes != NULL)
1839 list_types(buf->b_proptypes, rettv->vval.v_list);
1840 }
1841}
1842
1843/*
1844 * Free all property types in "ht".
1845 */
1846 static void
1847clear_ht_prop_types(hashtab_T *ht)
1848{
1849 long todo;
1850 hashitem_T *hi;
1851
1852 if (ht == NULL)
1853 return;
1854
1855 todo = (long)ht->ht_used;
1856 for (hi = ht->ht_array; todo > 0; ++hi)
1857 {
1858 if (!HASHITEM_EMPTY(hi))
1859 {
1860 proptype_T *prop = HI2PT(hi);
1861
1862 vim_free(prop);
1863 --todo;
1864 }
1865 }
1866
1867 hash_clear(ht);
1868 vim_free(ht);
1869}
1870
1871#if defined(EXITFREE) || defined(PROTO)
1872/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001873 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001874 */
1875 void
1876clear_global_prop_types(void)
1877{
1878 clear_ht_prop_types(global_proptypes);
1879 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001880 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001881}
1882#endif
1883
1884/*
1885 * Free all property types for "buf".
1886 */
1887 void
1888clear_buf_prop_types(buf_T *buf)
1889{
1890 clear_ht_prop_types(buf->b_proptypes);
1891 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001892 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001893}
1894
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001895// Struct used to return two values from adjust_prop().
1896typedef struct
1897{
1898 int dirty; // if the property was changed
1899 int can_drop; // whether after this change, the prop may be removed
1900} adjustres_T;
1901
1902/*
1903 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1904 *
1905 * Note that "col" is zero-based, while tp_col is one-based.
1906 * Only for the current buffer.
1907 * "flags" can have:
1908 * APC_SUBSTITUTE: Text is replaced, not inserted.
1909 */
1910 static adjustres_T
1911adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001912 textprop_T *prop,
1913 colnr_T col,
1914 int added,
1915 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001916{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001917 proptype_T *pt;
1918 int start_incl;
1919 int end_incl;
1920 int droppable;
1921 adjustres_T res = {TRUE, FALSE};
1922
1923 // prop after end of the line doesn't move
1924 if (prop->tp_col == MAXCOL)
1925 {
1926 res.dirty = FALSE;
1927 return res;
1928 }
1929
1930 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1931 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001932 || (flags & APC_SUBSTITUTE)
1933 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001934 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001935 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001936 // do not drop zero-width props if they later can increase in size
1937 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001938
1939 if (added > 0)
1940 {
1941 if (col + 1 <= prop->tp_col
1942 - (start_incl || (prop->tp_len == 0 && end_incl)))
1943 // Change is entirely before the text property: Only shift
1944 prop->tp_col += added;
1945 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1946 // Insertion was inside text property
1947 prop->tp_len += added;
1948 }
1949 else if (prop->tp_col > col + 1)
1950 {
1951 if (prop->tp_col + added < col + 1)
1952 {
1953 prop->tp_len += (prop->tp_col - 1 - col) + added;
1954 prop->tp_col = col + 1;
1955 if (prop->tp_len <= 0)
1956 {
1957 prop->tp_len = 0;
1958 res.can_drop = droppable;
1959 }
1960 }
1961 else
1962 prop->tp_col += added;
1963 }
1964 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1965 {
1966 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1967
1968 prop->tp_len += after > 0 ? added + after : added;
1969 res.can_drop = prop->tp_len <= 0 && droppable;
1970 }
1971 else
1972 res.dirty = FALSE;
1973
1974 return res;
1975}
1976
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001977/*
1978 * Adjust the columns of text properties in line "lnum" after position "col" to
1979 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001980 * Note that "col" is zero-based, while tp_col is one-based.
1981 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001982 * "flags" can have:
1983 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1984 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001985 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001986 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001987 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001988 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001989adjust_prop_columns(
1990 linenr_T lnum,
1991 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001992 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001993 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001994{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001995 int proplen;
1996 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001997 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001998 int ri, wi;
1999 size_t textlen;
2000
2001 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002002 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002003
2004 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2005 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002006 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002007 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002008
Bram Moolenaar196d1572019-01-02 23:47:18 +01002009 wi = 0; // write index
2010 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002011 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002012 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002013 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002014
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002015 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2016 res = adjust_prop(&prop, col, bytes_added, flags);
2017 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002018 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002019 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002020 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2021 && u_savesub(lnum) == FAIL)
2022 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002023 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002024
2025 // u_savesub() may have updated curbuf->b_ml, fetch it again
2026 if (curbuf->b_ml.ml_line_lnum != lnum)
2027 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002028 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002029 if (res.can_drop)
2030 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002031 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002032 ++wi;
2033 }
2034 if (dirty)
2035 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002036 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2037
2038 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002039 {
2040 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2041
2042 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2043 vim_free(curbuf->b_ml.ml_line_ptr);
2044 curbuf->b_ml.ml_line_ptr = p;
2045 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002046 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002047 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002048 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002049 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002050}
2051
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002052/*
2053 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002054 * "lnum_props" is the line that has the properties from before the split.
2055 * "lnum_top" is the top line.
2056 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002057 * "deleted" is the number of bytes deleted.
2058 */
2059 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002060adjust_props_for_split(
2061 linenr_T lnum_props,
2062 linenr_T lnum_top,
2063 int kept,
2064 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002065{
2066 char_u *props;
2067 int count;
2068 garray_T prevprop;
2069 garray_T nextprop;
2070 int i;
2071 int skipped = kept + deleted;
2072
2073 if (!curbuf->b_has_textprop)
2074 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002075
2076 // Get the text properties from "lnum_props".
2077 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002078 ga_init2(&prevprop, sizeof(textprop_T), 10);
2079 ga_init2(&nextprop, sizeof(textprop_T), 10);
2080
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002081 // Keep the relevant ones in the first line, reducing the length if needed.
2082 // Copy the ones that include the split to the second line.
2083 // Move the ones after the split to the second line.
2084 for (i = 0; i < count; ++i)
2085 {
2086 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002087 proptype_T *pt;
2088 int start_incl, end_incl;
2089 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002090
2091 // copy the prop to an aligned structure
2092 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2093
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002094 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2095 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2096 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002097 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2098 cont_next = prop.tp_col != MAXCOL
2099 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002100
2101 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002102 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002103 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2104
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002105 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002106 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002107 if (p->tp_col + p->tp_len >= kept)
2108 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002109 if (cont_next)
2110 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002111 }
2112
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002113 // Only add the property to the next line if the length is bigger than
2114 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002115 if ((cont_next || prop.tp_col == MAXCOL)
2116 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002117 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002118 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002119
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002120 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002121 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002122 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002123 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002124 if (p->tp_col > skipped)
2125 p->tp_col -= skipped - 1;
2126 else
2127 {
2128 p->tp_len -= skipped - p->tp_col;
2129 p->tp_col = 1;
2130 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002131 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002132 if (cont_prev)
2133 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002134 }
2135 }
2136
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002137 set_text_props(lnum_top, prevprop.ga_data,
2138 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002139 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002140 set_text_props(lnum_top + 1, nextprop.ga_data,
2141 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002142 ga_clear(&nextprop);
2143}
2144
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002145/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002146 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002147 */
2148 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002149prepend_joined_props(
2150 char_u *new_props,
2151 int propcount,
2152 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002153 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002154 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002155 long col,
2156 int removed)
2157{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002158 char_u *props;
2159 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2160 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002161
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002162 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002163 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002164 textprop_T prop;
2165 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002166
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002167 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002168 if (prop.tp_col == MAXCOL && !last_line)
2169 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002170 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2171
2172 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002173 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002174
Bram Moolenaare175dc62022-08-01 22:18:50 +01002175 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002176 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2177 &prop, sizeof(prop));
2178 else
2179 {
2180 int j;
2181 int found = FALSE;
2182
2183 // Search for continuing prop.
2184 for (j = *props_remaining; j < propcount; ++j)
2185 {
2186 textprop_T op;
2187
2188 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2189 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2190 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002191 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002192 found = TRUE;
2193 op.tp_len += op.tp_col - prop.tp_col;
2194 op.tp_col = prop.tp_col;
2195 // Start/end is taken care of when deleting joined lines
2196 op.tp_flags = prop.tp_flags;
2197 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2198 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002199 }
2200 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002201 if (!found)
2202 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002203 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002204 }
2205}
2206
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002207#endif // FEAT_PROP_POPUP