blob: 7d594da161fead8e4496d27748c2311b8ac78d20 [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)
297 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200298 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
299 sizeof(textprop_T));
300
301 if (i < proplen)
302 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
303 props + i * sizeof(textprop_T),
304 sizeof(textprop_T) * (proplen - i));
305
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100306 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200307 vim_free(buf->b_ml.ml_line_ptr);
308 buf->b_ml.ml_line_ptr = newtext;
309 buf->b_ml.ml_line_len += sizeof(textprop_T);
310 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
311 }
312
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100313 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200314 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100315 res = OK;
316
317theend:
318 vim_free(text);
319 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200320}
321
322/*
323 * prop_add_list()
324 * First argument specifies the text property:
325 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
326 * Second argument is a List where each item is a List with the following
327 * entries: [lnum, start_col, end_col]
328 */
329 void
330f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
331{
332 dict_T *dict;
333 char_u *type_name;
334 buf_T *buf = curbuf;
335 int id = 0;
336 listitem_T *li;
337 list_T *pos_list;
338 linenr_T start_lnum;
339 colnr_T start_col;
340 linenr_T end_lnum;
341 colnr_T end_col;
342 int error = FALSE;
343
344 if (check_for_dict_arg(argvars, 0) == FAIL
345 || check_for_list_arg(argvars, 1) == FAIL)
346 return;
347
348 if (argvars[1].vval.v_list == NULL)
349 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000350 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200351 return;
352 }
353
354 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100355 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200356 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000357 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200358 return;
359 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100360 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200361
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100362 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100363 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364
365 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
366 return;
367
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100368 // This must be done _before_ we start adding properties because property
369 // changes trigger buffer (memline) reorganisation, which needs this flag
370 // to be correctly set.
371 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200372 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
373 {
374 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
375 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000376 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200377 return;
378 }
379
380 pos_list = li->li_tv.vval.v_list;
381 start_lnum = list_find_nr(pos_list, 0L, &error);
382 start_col = list_find_nr(pos_list, 1L, &error);
383 end_lnum = list_find_nr(pos_list, 2L, &error);
384 end_col = list_find_nr(pos_list, 3L, &error);
385 if (error || start_lnum <= 0 || start_col <= 0
386 || end_lnum <= 0 || end_col <= 0)
387 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000388 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200389 return;
390 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100391 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200392 start_col, end_col) == FAIL)
393 return;
394 }
395
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200396 redraw_buf_later(buf, VALID);
397}
398
399/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100400 * Get the next ID to use for a textprop with text in buffer "buf".
401 */
402 static int
403get_textprop_id(buf_T *buf)
404{
405 // TODO: recycle deleted entries
406 return -(buf->b_textprop_text.ga_len + 1);
407}
408
409/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200410 * Shared between prop_add() and popup_create().
411 * "dict_arg" is the function argument of a dict containing "bufnr".
412 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100413 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200414 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100415 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200416prop_add_common(
417 linenr_T start_lnum,
418 colnr_T start_col,
419 dict_T *dict,
420 buf_T *default_buf,
421 typval_T *dict_arg)
422{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200423 linenr_T end_lnum;
424 colnr_T end_col;
425 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200426 buf_T *buf = default_buf;
427 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100428 char_u *text = NULL;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100429 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100430
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100431 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100432 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000433 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100434 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100435 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100436 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100437
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100438 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100439 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100440 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100441 if (end_lnum < start_lnum)
442 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000443 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100444 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100445 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100446 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100447 else
448 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100449
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100450 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100451 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100452 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100453
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100454 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100455 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000456 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100457 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100458 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100459 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100460 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100461 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100462 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100463 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100464 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100465 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000466 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100467 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100468 }
469 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100470 else if (start_lnum == end_lnum)
471 end_col = start_col;
472 else
473 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100474
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100475 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100476 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100477
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100478 if (dict_has_key(dict, "text"))
479 {
480 text = dict_get_string(dict, "text", TRUE);
481 if (text == NULL)
482 goto theend;
483 // use a default length of 1 to make multiple props show up
484 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100485
486 if (dict_has_key(dict, "text_align"))
487 {
488 char_u *p = dict_get_string(dict, "text_align", FALSE);
489
490 if (p == NULL)
491 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100492 if (start_col != 0)
493 {
494 emsg(_(e_can_only_use_text_align_when_column_is_zero));
495 goto theend;
496 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100497 if (STRCMP(p, "right") == 0)
498 flags |= TP_FLAG_ALIGN_RIGHT;
499 else if (STRCMP(p, "below") == 0)
500 flags |= TP_FLAG_ALIGN_BELOW;
501 else if (STRCMP(p, "after") != 0)
502 {
503 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
504 goto theend;
505 }
506 }
507
508 if (dict_has_key(dict, "text_wrap"))
509 {
510 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
511 if (p == NULL)
512 goto theend;
513 if (STRCMP(p, "wrap") == 0)
514 flags |= TP_FLAG_WRAP;
515 else if (STRCMP(p, "truncate") != 0)
516 {
517 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
518 goto theend;
519 }
520 }
521 }
522
523 // Column must be 1 or more for a normal text property; when "text" is
524 // present zero means it goes after the line.
525 if (start_col < (text == NULL ? 1 : 0))
526 {
527 semsg(_(e_invalid_column_number_nr), (long)start_col);
528 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100529 }
530
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200531 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100532 goto theend;
533
534 if (id < 0 && buf->b_textprop_text.ga_len > 0)
535 {
536 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
537 goto theend;
538 }
539 if (text != NULL)
540 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100541
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100542 // This must be done _before_ we add the property because property changes
543 // trigger buffer (memline) reorganisation, which needs this flag to be
544 // correctly set.
545 buf->b_has_textprop = TRUE; // this is never reset
546
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100547 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100548 start_lnum, end_lnum, start_col, end_col);
549 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100550
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200551 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100552
553theend:
554 vim_free(text);
555 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100556}
557
558/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100559 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100560 * Returns the number of text properties and, when non-zero, a pointer to the
561 * first one in "props" (note that it is not aligned, therefore the char_u
562 * pointer).
563 */
564 int
565get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
566{
567 char_u *text;
568 size_t textlen;
569 size_t proplen;
570
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100571 // Be quick when no text property types have been defined or the buffer,
572 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200573 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100574 return 0;
575
576 // Fetch the line to get the ml_line_len field updated.
577 text = ml_get_buf(buf, lnum, will_change);
578 textlen = STRLEN(text) + 1;
579 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100580 if (proplen == 0)
581 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100582 if (proplen % sizeof(textprop_T) != 0)
583 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000584 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100585 return 0;
586 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100587 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100588 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100589}
590
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100591/*
592 * Return the number of text properties with "below" alignment in line "lnum".
593 */
594 int
595prop_count_below(buf_T *buf, linenr_T lnum)
596{
597 char_u *props;
598 int count = get_text_props(buf, lnum, &props, FALSE);
599 int result = 0;
600 textprop_T prop;
601 int i;
602
603 if (count == 0)
604 return 0;
605 for (i = 0; i < count; ++i)
606 {
607 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
608 if (prop.tp_col == MAXCOL && (prop.tp_flags & TP_FLAG_ALIGN_BELOW))
609 ++result;
610 }
611 return result;
612}
613
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200614/**
615 * Return the number of text properties on line "lnum" in the current buffer.
616 * When "only_starting" is true only text properties starting in this line will
617 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100618 * When "last_line" is FALSE then text properties after the line are not
619 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200620 */
621 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100622count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200623{
624 char_u *props;
625 int proplen = get_text_props(curbuf, lnum, &props, 0);
626 int result = proplen;
627 int i;
628 textprop_T prop;
629
Bram Moolenaare175dc62022-08-01 22:18:50 +0100630 for (i = 0; i < proplen; ++i)
631 {
632 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100633 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100634 // previous line, or when not in the last line and it is virtual text
635 // after the line.
636 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
637 || (!last_line && prop.tp_col == MAXCOL))
638 --result;
639 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200640 return result;
641}
642
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100643/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200644 * Find text property "type_id" in the visible lines of window "wp".
645 * Match "id" when it is > 0.
646 * Returns FAIL when not found.
647 */
648 int
649find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
650 linenr_T *found_lnum)
651{
652 linenr_T lnum;
653 char_u *props;
654 int count;
655 int i;
656
657 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100658 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200659 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
660 {
661 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
662 for (i = 0; i < count; ++i)
663 {
664 mch_memmove(prop, props + i * sizeof(textprop_T),
665 sizeof(textprop_T));
666 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
667 {
668 *found_lnum = lnum;
669 return OK;
670 }
671 }
672 }
673 return FAIL;
674}
675
676/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100677 * Set the text properties for line "lnum" to "props" with length "len".
678 * If "len" is zero text properties are removed, "props" is not used.
679 * Any existing text properties are dropped.
680 * Only works for the current buffer.
681 */
682 static void
683set_text_props(linenr_T lnum, char_u *props, int len)
684{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100685 char_u *text;
686 char_u *newtext;
687 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100688
689 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100690 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100691 newtext = alloc(textlen + len);
692 if (newtext == NULL)
693 return;
694 mch_memmove(newtext, text, textlen);
695 if (len > 0)
696 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100697 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100698 vim_free(curbuf->b_ml.ml_line_ptr);
699 curbuf->b_ml.ml_line_ptr = newtext;
700 curbuf->b_ml.ml_line_len = textlen + len;
701 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
702}
703
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100704/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100705 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100706 */
707 void
708add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
709{
710 char_u *text;
711 char_u *newtext;
712 int proplen = text_prop_count * (int)sizeof(textprop_T);
713
714 text = ml_get(lnum);
715 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
716 if (newtext == NULL)
717 return;
718 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
719 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
720 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
721 vim_free(curbuf->b_ml.ml_line_ptr);
722 curbuf->b_ml.ml_line_ptr = newtext;
723 curbuf->b_ml.ml_line_len += proplen;
724 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
725}
726
Bram Moolenaare44336b2022-08-07 18:20:08 +0100727/*
728 * Function passed to qsort() for sorting proptype_T on pt_id.
729 */
730 static int
731compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100732{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100733 proptype_T *tp1 = *(proptype_T **)s1;
734 proptype_T *tp2 = *(proptype_T **)s2;
735
736 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
737}
738
739 static proptype_T *
740find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
741{
742 int low = 0;
743 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100744
Bram Moolenaar10246902022-08-08 17:08:05 +0100745 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100746 return NULL;
747
Bram Moolenaare44336b2022-08-07 18:20:08 +0100748 // Make the loopup faster by creating an array with pointers to
749 // hashtable entries, sorted on pt_id.
750 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100751 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100752 long todo;
753 hashitem_T *hi;
754 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100755
Bram Moolenaare44336b2022-08-07 18:20:08 +0100756 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
757 if (*array == NULL)
758 return NULL;
759 todo = (long)ht->ht_used;
760 for (hi = ht->ht_array; todo > 0; ++hi)
761 {
762 if (!HASHITEM_EMPTY(hi))
763 {
764 (*array)[i++] = HI2PT(hi);
765 --todo;
766 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100767 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100768 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
769 }
770
771 // binary search in the sorted array
772 high = ht->ht_used;
773 while (high > low)
774 {
775 int m = (high + low) / 2;
776
777 if ((*array)[m]->pt_id == id)
778 return (*array)[m];
779 if ((*array)[m]->pt_id > id)
780 high = m;
781 else
782 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100783 }
784 return NULL;
785}
786
787/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100788 * Fill 'dict' with text properties in 'prop'.
789 */
790 static void
791prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
792{
793 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200794 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100795
796 dict_add_number(dict, "col", prop->tp_col);
797 dict_add_number(dict, "length", prop->tp_len);
798 dict_add_number(dict, "id", prop->tp_id);
799 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
800 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200801
Bram Moolenaare44336b2022-08-07 18:20:08 +0100802 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200803 if (pt == NULL)
804 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100805 pt = find_type_by_id(global_proptypes, &global_proparray,
806 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200807 buflocal = FALSE;
808 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100809 if (pt != NULL)
810 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200811
812 if (buflocal)
813 dict_add_number(dict, "type_bufnr", buf->b_fnum);
814 else
815 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100816}
817
818/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100819 * Find a property type by ID in "buf" or globally.
820 * Returns NULL if not found.
821 */
822 proptype_T *
823text_prop_type_by_id(buf_T *buf, int id)
824{
825 proptype_T *type;
826
Bram Moolenaare44336b2022-08-07 18:20:08 +0100827 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100828 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100829 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100830 return type;
831}
832
833/*
834 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
835 */
836 void
837f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
838{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200839 linenr_T start;
840 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100841 linenr_T lnum;
842 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100843 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100844
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200845 if (in_vim9script()
846 && (check_for_number_arg(argvars, 0) == FAIL
847 || check_for_opt_number_arg(argvars, 1) == FAIL
848 || (argvars[1].v_type != VAR_UNKNOWN
849 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
850 return;
851
852 start = tv_get_number(&argvars[0]);
853 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100854 if (argvars[1].v_type != VAR_UNKNOWN)
855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100856 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100857 if (argvars[2].v_type != VAR_UNKNOWN)
858 {
859 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
860 return;
861 }
862 }
863 if (start < 1 || end < 1)
864 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200865 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100866 return;
867 }
868
869 for (lnum = start; lnum <= end; ++lnum)
870 {
871 char_u *text;
872 size_t len;
873
874 if (lnum > buf->b_ml.ml_line_count)
875 break;
876 text = ml_get_buf(buf, lnum, FALSE);
877 len = STRLEN(text) + 1;
878 if ((size_t)buf->b_ml.ml_line_len > len)
879 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100880 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100881 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
882 {
883 char_u *newtext = vim_strsave(text);
884
885 // need to allocate the line now
886 if (newtext == NULL)
887 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100888 if (buf->b_ml.ml_flags & ML_ALLOCATED)
889 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100890 buf->b_ml.ml_line_ptr = newtext;
891 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
892 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100893 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100894 }
895 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100896 if (did_clear)
897 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100898}
899
900/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100901 * prop_find({props} [, {direction}])
902 */
903 void
904f_prop_find(typval_T *argvars, typval_T *rettv)
905{
906 pos_T *cursor = &curwin->w_cursor;
907 dict_T *dict;
908 buf_T *buf = curbuf;
909 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200910 int lnum_start;
911 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100912 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200913 int id = 0;
914 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200915 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100916 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200917 int lnum = -1;
918 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100919 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100920 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100921
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200922 if (in_vim9script()
923 && (check_for_dict_arg(argvars, 0) == FAIL
924 || check_for_opt_string_arg(argvars, 1) == FAIL))
925 return;
926
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100927 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
928 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000929 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100930 return;
931 }
932 dict = argvars[0].vval.v_dict;
933
934 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
935 return;
936 if (buf->b_ml.ml_mfp == NULL)
937 return;
938
939 if (argvars[1].v_type != VAR_UNKNOWN)
940 {
941 char_u *dir_s = tv_get_string(&argvars[1]);
942
943 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100944 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100945 else if (*dir_s != 'f')
946 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000947 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100948 return;
949 }
950 }
951
952 di = dict_find(dict, (char_u *)"lnum", -1);
953 if (di != NULL)
954 lnum = tv_get_number(&di->di_tv);
955
956 di = dict_find(dict, (char_u *)"col", -1);
957 if (di != NULL)
958 col = tv_get_number(&di->di_tv);
959
960 if (lnum == -1)
961 {
962 lnum = cursor->lnum;
963 col = cursor->col + 1;
964 }
965 else if (col == -1)
966 col = 1;
967
968 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
969 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200970 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100971 return;
972 }
973
Bram Moolenaard61efa52022-07-23 09:52:04 +0100974 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100975
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100976 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200977 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100978 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200979 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200980 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100981 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100982 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100983 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100984 proptype_T *type = lookup_prop_type(name, buf);
985
986 if (type == NULL)
987 return;
988 type_id = type->pt_id;
989 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100990 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200991 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100992 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000993 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100994 return;
995 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200996 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100997 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000998 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100999 return;
1000 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001001
1002 lnum_start = lnum;
1003
1004 if (rettv_dict_alloc(rettv) == FAIL)
1005 return;
1006
1007 while (1)
1008 {
1009 char_u *text = ml_get_buf(buf, lnum, FALSE);
1010 size_t textlen = STRLEN(text) + 1;
1011 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001012 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001013 int i;
1014 textprop_T prop;
1015 int prop_start;
1016 int prop_end;
1017
LemonBoy9bd3ce22022-04-18 21:54:02 +01001018 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001019 {
1020 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001021 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001022
LemonBoy9bd3ce22022-04-18 21:54:02 +01001023 // For the very first line try to find the first property before or
1024 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001025 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001026 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001027 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001028 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001029 if (prop.tp_col > col)
1030 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001031 }
1032 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1033 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001034 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001035 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001036 : (id_found && prop.tp_id == id)
1037 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001038 {
1039 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001040 if (lnum_start == lnum
1041 && col >= prop.tp_col
1042 && (col <= prop.tp_col + prop.tp_len
1043 - (prop.tp_len != 0)))
1044 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001045
LemonBoy9bd3ce22022-04-18 21:54:02 +01001046 // The property was not continued from last line, it starts on
1047 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001048 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001049 // The property does not continue on the next line, it ends on
1050 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001051 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001052 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001053 seen_end = 1;
1054
1055 // Skip lines without the start flag.
1056 if (!prop_start)
1057 {
1058 // Always search backwards for start when search started
1059 // on a prop and we're not skipping.
1060 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001061 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001062 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001063 }
1064
1065 // If skipstart is true, skip the prop at start pos (even if
1066 // continued from another line).
1067 if (start_pos_has_prop && skipstart && !seen_end)
1068 {
1069 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001070 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001071 }
1072
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001073 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1074 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1075
1076 return;
1077 }
1078 }
1079
1080 if (dir > 0)
1081 {
1082 if (lnum >= buf->b_ml.ml_line_count)
1083 break;
1084 lnum++;
1085 }
1086 else
1087 {
1088 if (lnum <= 1)
1089 break;
1090 lnum--;
1091 }
1092 }
1093}
1094
1095/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001096 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1097 */
1098 static int
1099prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1100{
1101 int i;
1102
1103 for (i = 0; i < len; i++)
1104 if (types_or_ids[i] == type_or_id)
1105 return TRUE;
1106
1107 return FALSE;
1108}
1109
1110/*
1111 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1112 * If 'prop_types' is not NULL, then return only the text properties with
1113 * matching property type in the 'prop_types' array.
1114 * If 'prop_ids' is not NULL, then return only the text properties with
1115 * an identifier in the 'props_ids' array.
1116 * If 'add_lnum' is TRUE, then add the line number also to the text property
1117 * dictionary.
1118 */
1119 static void
1120get_props_in_line(
1121 buf_T *buf,
1122 linenr_T lnum,
1123 int *prop_types,
1124 int prop_types_len,
1125 int *prop_ids,
1126 int prop_ids_len,
1127 list_T *retlist,
1128 int add_lnum)
1129{
1130 char_u *text = ml_get_buf(buf, lnum, FALSE);
1131 size_t textlen = STRLEN(text) + 1;
1132 int count;
1133 int i;
1134 textprop_T prop;
1135
1136 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1137 for (i = 0; i < count; ++i)
1138 {
1139 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1140 sizeof(textprop_T));
1141 if ((prop_types == NULL
1142 || prop_type_or_id_in_list(prop_types, prop_types_len,
1143 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001144 && (prop_ids == NULL
1145 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1146 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001147 {
1148 dict_T *d = dict_alloc();
1149
1150 if (d == NULL)
1151 break;
1152 prop_fill_dict(d, &prop, buf);
1153 if (add_lnum)
1154 dict_add_number(d, "lnum", lnum);
1155 list_append_dict(retlist, d);
1156 }
1157 }
1158}
1159
1160/*
1161 * Convert a List of property type names into an array of property type
1162 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1163 * error. 'num_types' is set to the number of returned property types.
1164 */
1165 static int *
1166get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1167{
1168 int *prop_types;
1169 listitem_T *li;
1170 int i;
1171 char_u *name;
1172 proptype_T *type;
1173
1174 *num_types = 0;
1175
1176 prop_types = ALLOC_MULT(int, list_len(l));
1177 if (prop_types == NULL)
1178 return NULL;
1179
1180 i = 0;
1181 FOR_ALL_LIST_ITEMS(l, li)
1182 {
1183 if (li->li_tv.v_type != VAR_STRING)
1184 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001185 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001186 goto errret;
1187 }
1188 name = li->li_tv.vval.v_string;
1189 if (name == NULL)
1190 goto errret;
1191
1192 type = lookup_prop_type(name, buf);
1193 if (type == NULL)
1194 goto errret;
1195 prop_types[i++] = type->pt_id;
1196 }
1197
1198 *num_types = i;
1199 return prop_types;
1200
1201errret:
1202 VIM_CLEAR(prop_types);
1203 return NULL;
1204}
1205
1206/*
1207 * Convert a List of property identifiers into an array of property
1208 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1209 * error. 'num_ids' is set to the number of returned property identifiers.
1210 */
1211 static int *
1212get_prop_ids_from_list(list_T *l, int *num_ids)
1213{
1214 int *prop_ids;
1215 listitem_T *li;
1216 int i;
1217 int id;
1218 int error;
1219
1220 *num_ids = 0;
1221
1222 prop_ids = ALLOC_MULT(int, list_len(l));
1223 if (prop_ids == NULL)
1224 return NULL;
1225
1226 i = 0;
1227 FOR_ALL_LIST_ITEMS(l, li)
1228 {
1229 error = FALSE;
1230 id = tv_get_number_chk(&li->li_tv, &error);
1231 if (error)
1232 goto errret;
1233
1234 prop_ids[i++] = id;
1235 }
1236
1237 *num_ids = i;
1238 return prop_ids;
1239
1240errret:
1241 VIM_CLEAR(prop_ids);
1242 return NULL;
1243}
1244
1245/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001246 * prop_list({lnum} [, {bufnr}])
1247 */
1248 void
1249f_prop_list(typval_T *argvars, typval_T *rettv)
1250{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001251 linenr_T lnum;
1252 linenr_T start_lnum;
1253 linenr_T end_lnum;
1254 buf_T *buf = curbuf;
1255 int add_lnum = FALSE;
1256 int *prop_types = NULL;
1257 int prop_types_len = 0;
1258 int *prop_ids = NULL;
1259 int prop_ids_len = 0;
1260 list_T *l;
1261 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001262
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001263 if (in_vim9script()
1264 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001265 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001266 return;
1267
Bram Moolenaar93a10962022-06-16 11:42:09 +01001268 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001269 return;
1270
1271 // default: get text properties on current line
1272 start_lnum = tv_get_number(&argvars[0]);
1273 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001274 if (argvars[1].v_type != VAR_UNKNOWN)
1275 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001276 dict_T *d;
1277
1278 if (argvars[1].v_type != VAR_DICT)
1279 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001280 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001281 return;
1282 }
1283 d = argvars[1].vval.v_dict;
1284
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001285 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1286 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001287
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001288 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001289 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001290 if (di->di_tv.v_type != VAR_NUMBER)
1291 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001292 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001293 return;
1294 }
1295 end_lnum = tv_get_number(&di->di_tv);
1296 if (end_lnum < 0)
1297 // negative end_lnum is used as an offset from the last buffer
1298 // line
1299 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1300 else if (end_lnum > buf->b_ml.ml_line_count)
1301 end_lnum = buf->b_ml.ml_line_count;
1302 add_lnum = TRUE;
1303 }
1304 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1305 {
1306 if (di->di_tv.v_type != VAR_LIST)
1307 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001308 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001309 return;
1310 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001311
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001312 l = di->di_tv.vval.v_list;
1313 if (l != NULL && list_len(l) > 0)
1314 {
1315 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1316 if (prop_types == NULL)
1317 return;
1318 }
1319 }
1320 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1321 {
1322 if (di->di_tv.v_type != VAR_LIST)
1323 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001324 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001325 goto errret;
1326 }
1327
1328 l = di->di_tv.vval.v_list;
1329 if (l != NULL && list_len(l) > 0)
1330 {
1331 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1332 if (prop_ids == NULL)
1333 goto errret;
1334 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001335 }
1336 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001337 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1338 || end_lnum < 1 || end_lnum < start_lnum)
1339 emsg(_(e_invalid_range));
1340 else
1341 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1342 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1343 prop_ids, prop_ids_len,
1344 rettv->vval.v_list, add_lnum);
1345
1346errret:
1347 VIM_CLEAR(prop_types);
1348 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001349}
1350
1351/*
1352 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1353 */
1354 void
1355f_prop_remove(typval_T *argvars, typval_T *rettv)
1356{
1357 linenr_T start = 1;
1358 linenr_T end = 0;
1359 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001360 linenr_T first_changed = 0;
1361 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001362 dict_T *dict;
1363 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001364 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001365 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001366 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001367 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001368 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001369
1370 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001371
1372 if (in_vim9script()
1373 && (check_for_dict_arg(argvars, 0) == FAIL
1374 || check_for_opt_number_arg(argvars, 1) == FAIL
1375 || (argvars[1].v_type != VAR_UNKNOWN
1376 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1377 return;
1378
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001379 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1380 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001381 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001382 return;
1383 }
1384
1385 if (argvars[1].v_type != VAR_UNKNOWN)
1386 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001387 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001388 end = start;
1389 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001390 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001391 if (start < 1 || end < 1)
1392 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001393 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001394 return;
1395 }
1396 }
1397
1398 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001399 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1400 return;
1401 if (buf->b_ml.ml_mfp == NULL)
1402 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001403
Bram Moolenaard61efa52022-07-23 09:52:04 +01001404 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001405
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001406 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001407 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001408 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001409 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001410 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001411 proptype_T *type = lookup_prop_type(name, buf);
1412
1413 if (type == NULL)
1414 return;
1415 type_id = type->pt_id;
1416 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001417 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001418
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001419 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001420 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001421 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001422 return;
1423 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001424 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001425 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001426 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001427 return;
1428 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001429
1430 if (end == 0)
1431 end = buf->b_ml.ml_line_count;
1432 for (lnum = start; lnum <= end; ++lnum)
1433 {
1434 char_u *text;
1435 size_t len;
1436
1437 if (lnum > buf->b_ml.ml_line_count)
1438 break;
1439 text = ml_get_buf(buf, lnum, FALSE);
1440 len = STRLEN(text) + 1;
1441 if ((size_t)buf->b_ml.ml_line_len > len)
1442 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001443 static textprop_T textprop; // static because of alignment
1444 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001445
1446 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1447 / sizeof(textprop_T); ++idx)
1448 {
1449 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1450 + idx * sizeof(textprop_T);
1451 size_t taillen;
1452
1453 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001454 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1455 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001456 {
1457 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1458 {
1459 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1460
1461 // need to allocate the line to be able to change it
1462 if (newptr == NULL)
1463 return;
1464 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1465 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001466 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1467 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001468 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001469 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1470
1471 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001472 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001473 }
1474
1475 taillen = buf->b_ml.ml_line_len - len
1476 - (idx + 1) * sizeof(textprop_T);
1477 if (taillen > 0)
1478 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1479 taillen);
1480 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1481 --idx;
1482
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001483 if (textprop.tp_id < 0)
1484 {
1485 garray_T *gap = &buf->b_textprop_text;
1486 int ii = -textprop.tp_id - 1;
1487
1488 // negative ID: property with text - free the text
1489 if (ii < gap->ga_len)
1490 {
1491 char_u **p = ((char_u **)gap->ga_data) + ii;
1492 vim_free(*p);
1493 *p = NULL;
1494 did_remove_text = TRUE;
1495 }
1496 }
1497
Bram Moolenaar965c0442021-05-17 00:22:06 +02001498 if (first_changed == 0)
1499 first_changed = lnum;
1500 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001501 ++rettv->vval.v_number;
1502 if (!do_all)
1503 break;
1504 }
1505 }
1506 }
1507 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001508
Bram Moolenaar965c0442021-05-17 00:22:06 +02001509 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001510 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001511 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001512 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1513 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001514 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001515
1516 if (did_remove_text)
1517 {
1518 garray_T *gap = &buf->b_textprop_text;
1519
1520 // Reduce the growarray size for NULL pointers at the end.
1521 while (gap->ga_len > 0
1522 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1523 --gap->ga_len;
1524 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001525}
1526
1527/*
1528 * Common for f_prop_type_add() and f_prop_type_change().
1529 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001530 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001531prop_type_set(typval_T *argvars, int add)
1532{
1533 char_u *name;
1534 buf_T *buf = NULL;
1535 dict_T *dict;
1536 dictitem_T *di;
1537 proptype_T *prop;
1538
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001539 if (in_vim9script()
1540 && (check_for_string_arg(argvars, 0) == FAIL
1541 || check_for_dict_arg(argvars, 1) == FAIL))
1542 return;
1543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001544 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001545 if (*name == NUL)
1546 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001547 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001548 return;
1549 }
1550
1551 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1552 return;
1553 dict = argvars[1].vval.v_dict;
1554
Bram Moolenaare44336b2022-08-07 18:20:08 +01001555 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001556 if (add)
1557 {
1558 hashtab_T **htp;
1559
1560 if (prop != NULL)
1561 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001562 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001563 return;
1564 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001565 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001566 if (prop == NULL)
1567 return;
1568 STRCPY(prop->pt_name, name);
1569 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001570 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001571 if (buf == NULL)
1572 {
1573 htp = &global_proptypes;
1574 VIM_CLEAR(global_proparray);
1575 }
1576 else
1577 {
1578 htp = &buf->b_proptypes;
1579 VIM_CLEAR(buf->b_proparray);
1580 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001581 if (*htp == NULL)
1582 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001583 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001584 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001585 {
1586 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001587 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001588 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001589 hash_init(*htp);
1590 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001591 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001592 }
1593 else
1594 {
1595 if (prop == NULL)
1596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001597 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001598 return;
1599 }
1600 }
1601
1602 if (dict != NULL)
1603 {
1604 di = dict_find(dict, (char_u *)"highlight", -1);
1605 if (di != NULL)
1606 {
1607 char_u *highlight;
1608 int hl_id = 0;
1609
Bram Moolenaard61efa52022-07-23 09:52:04 +01001610 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001611 if (highlight != NULL && *highlight != NUL)
1612 hl_id = syn_name2id(highlight);
1613 if (hl_id <= 0)
1614 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001615 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001616 highlight == NULL ? (char_u *)"" : highlight);
1617 return;
1618 }
1619 prop->pt_hl_id = hl_id;
1620 }
1621
Bram Moolenaar58187f12019-05-05 16:33:47 +02001622 di = dict_find(dict, (char_u *)"combine", -1);
1623 if (di != NULL)
1624 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001625 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001626 prop->pt_flags |= PT_FLAG_COMBINE;
1627 else
1628 prop->pt_flags &= ~PT_FLAG_COMBINE;
1629 }
1630
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001631 di = dict_find(dict, (char_u *)"override", -1);
1632 if (di != NULL)
1633 {
1634 if (tv_get_bool(&di->di_tv))
1635 prop->pt_flags |= PT_FLAG_OVERRIDE;
1636 else
1637 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1638 }
1639
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001640 di = dict_find(dict, (char_u *)"priority", -1);
1641 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001642 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001643
1644 di = dict_find(dict, (char_u *)"start_incl", -1);
1645 if (di != NULL)
1646 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001647 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001648 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1649 else
1650 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1651 }
1652
1653 di = dict_find(dict, (char_u *)"end_incl", -1);
1654 if (di != NULL)
1655 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001656 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001657 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1658 else
1659 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1660 }
1661 }
1662}
1663
1664/*
1665 * prop_type_add({name}, {props})
1666 */
1667 void
1668f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1669{
1670 prop_type_set(argvars, TRUE);
1671}
1672
1673/*
1674 * prop_type_change({name}, {props})
1675 */
1676 void
1677f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1678{
1679 prop_type_set(argvars, FALSE);
1680}
1681
1682/*
1683 * prop_type_delete({name} [, {bufnr}])
1684 */
1685 void
1686f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1687{
1688 char_u *name;
1689 buf_T *buf = NULL;
1690 hashitem_T *hi;
1691
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001692 if (in_vim9script()
1693 && (check_for_string_arg(argvars, 0) == FAIL
1694 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1695 return;
1696
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001697 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001698 if (*name == NUL)
1699 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001700 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001701 return;
1702 }
1703
1704 if (argvars[1].v_type != VAR_UNKNOWN)
1705 {
1706 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1707 return;
1708 }
1709
Bram Moolenaare44336b2022-08-07 18:20:08 +01001710 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001711 if (hi != NULL)
1712 {
1713 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001714 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001715
1716 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001717 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001718 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001719 VIM_CLEAR(global_proparray);
1720 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001721 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001722 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001723 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001724 VIM_CLEAR(buf->b_proparray);
1725 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001726 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001727 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001728 }
1729}
1730
1731/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001732 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001733 */
1734 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001735f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001736{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001737 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001738
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001739 if (in_vim9script()
1740 && (check_for_string_arg(argvars, 0) == FAIL
1741 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1742 return;
1743
1744 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001745 if (*name == NUL)
1746 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001747 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001748 return;
1749 }
1750 if (rettv_dict_alloc(rettv) == OK)
1751 {
1752 proptype_T *prop = NULL;
1753 buf_T *buf = NULL;
1754
1755 if (argvars[1].v_type != VAR_UNKNOWN)
1756 {
1757 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1758 return;
1759 }
1760
Bram Moolenaare44336b2022-08-07 18:20:08 +01001761 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001762 if (prop != NULL)
1763 {
1764 dict_T *d = rettv->vval.v_dict;
1765
1766 if (prop->pt_hl_id > 0)
1767 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1768 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001769 dict_add_number(d, "combine",
1770 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001771 dict_add_number(d, "start_incl",
1772 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1773 dict_add_number(d, "end_incl",
1774 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1775 if (buf != NULL)
1776 dict_add_number(d, "bufnr", buf->b_fnum);
1777 }
1778 }
1779}
1780
1781 static void
1782list_types(hashtab_T *ht, list_T *l)
1783{
1784 long todo;
1785 hashitem_T *hi;
1786
1787 todo = (long)ht->ht_used;
1788 for (hi = ht->ht_array; todo > 0; ++hi)
1789 {
1790 if (!HASHITEM_EMPTY(hi))
1791 {
1792 proptype_T *prop = HI2PT(hi);
1793
1794 list_append_string(l, prop->pt_name, -1);
1795 --todo;
1796 }
1797 }
1798}
1799
1800/*
1801 * prop_type_list([{bufnr}])
1802 */
1803 void
1804f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1805{
1806 buf_T *buf = NULL;
1807
1808 if (rettv_list_alloc(rettv) == OK)
1809 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001810 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1811 return;
1812
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001813 if (argvars[0].v_type != VAR_UNKNOWN)
1814 {
1815 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1816 return;
1817 }
1818 if (buf == NULL)
1819 {
1820 if (global_proptypes != NULL)
1821 list_types(global_proptypes, rettv->vval.v_list);
1822 }
1823 else if (buf->b_proptypes != NULL)
1824 list_types(buf->b_proptypes, rettv->vval.v_list);
1825 }
1826}
1827
1828/*
1829 * Free all property types in "ht".
1830 */
1831 static void
1832clear_ht_prop_types(hashtab_T *ht)
1833{
1834 long todo;
1835 hashitem_T *hi;
1836
1837 if (ht == NULL)
1838 return;
1839
1840 todo = (long)ht->ht_used;
1841 for (hi = ht->ht_array; todo > 0; ++hi)
1842 {
1843 if (!HASHITEM_EMPTY(hi))
1844 {
1845 proptype_T *prop = HI2PT(hi);
1846
1847 vim_free(prop);
1848 --todo;
1849 }
1850 }
1851
1852 hash_clear(ht);
1853 vim_free(ht);
1854}
1855
1856#if defined(EXITFREE) || defined(PROTO)
1857/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001858 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001859 */
1860 void
1861clear_global_prop_types(void)
1862{
1863 clear_ht_prop_types(global_proptypes);
1864 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001865 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001866}
1867#endif
1868
1869/*
1870 * Free all property types for "buf".
1871 */
1872 void
1873clear_buf_prop_types(buf_T *buf)
1874{
1875 clear_ht_prop_types(buf->b_proptypes);
1876 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001877 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001878}
1879
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001880// Struct used to return two values from adjust_prop().
1881typedef struct
1882{
1883 int dirty; // if the property was changed
1884 int can_drop; // whether after this change, the prop may be removed
1885} adjustres_T;
1886
1887/*
1888 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1889 *
1890 * Note that "col" is zero-based, while tp_col is one-based.
1891 * Only for the current buffer.
1892 * "flags" can have:
1893 * APC_SUBSTITUTE: Text is replaced, not inserted.
1894 */
1895 static adjustres_T
1896adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001897 textprop_T *prop,
1898 colnr_T col,
1899 int added,
1900 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001901{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001902 proptype_T *pt;
1903 int start_incl;
1904 int end_incl;
1905 int droppable;
1906 adjustres_T res = {TRUE, FALSE};
1907
1908 // prop after end of the line doesn't move
1909 if (prop->tp_col == MAXCOL)
1910 {
1911 res.dirty = FALSE;
1912 return res;
1913 }
1914
1915 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1916 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001917 || (flags & APC_SUBSTITUTE)
1918 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001919 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001920 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001921 // do not drop zero-width props if they later can increase in size
1922 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001923
1924 if (added > 0)
1925 {
1926 if (col + 1 <= prop->tp_col
1927 - (start_incl || (prop->tp_len == 0 && end_incl)))
1928 // Change is entirely before the text property: Only shift
1929 prop->tp_col += added;
1930 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1931 // Insertion was inside text property
1932 prop->tp_len += added;
1933 }
1934 else if (prop->tp_col > col + 1)
1935 {
1936 if (prop->tp_col + added < col + 1)
1937 {
1938 prop->tp_len += (prop->tp_col - 1 - col) + added;
1939 prop->tp_col = col + 1;
1940 if (prop->tp_len <= 0)
1941 {
1942 prop->tp_len = 0;
1943 res.can_drop = droppable;
1944 }
1945 }
1946 else
1947 prop->tp_col += added;
1948 }
1949 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1950 {
1951 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1952
1953 prop->tp_len += after > 0 ? added + after : added;
1954 res.can_drop = prop->tp_len <= 0 && droppable;
1955 }
1956 else
1957 res.dirty = FALSE;
1958
1959 return res;
1960}
1961
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001962/*
1963 * Adjust the columns of text properties in line "lnum" after position "col" to
1964 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001965 * Note that "col" is zero-based, while tp_col is one-based.
1966 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001967 * "flags" can have:
1968 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1969 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001970 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001971 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001972 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001973 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001974adjust_prop_columns(
1975 linenr_T lnum,
1976 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001977 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001978 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001979{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001980 int proplen;
1981 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001982 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001983 int ri, wi;
1984 size_t textlen;
1985
1986 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001987 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001988
1989 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1990 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001991 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001992 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001993
Bram Moolenaar196d1572019-01-02 23:47:18 +01001994 wi = 0; // write index
1995 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001996 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001997 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001998 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001999
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002000 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2001 res = adjust_prop(&prop, col, bytes_added, flags);
2002 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002003 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002004 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002005 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2006 && u_savesub(lnum) == FAIL)
2007 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002008 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002009
2010 // u_savesub() may have updated curbuf->b_ml, fetch it again
2011 if (curbuf->b_ml.ml_line_lnum != lnum)
2012 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002013 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002014 if (res.can_drop)
2015 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002016 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002017 ++wi;
2018 }
2019 if (dirty)
2020 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002021 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2022
2023 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002024 {
2025 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2026
2027 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2028 vim_free(curbuf->b_ml.ml_line_ptr);
2029 curbuf->b_ml.ml_line_ptr = p;
2030 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002031 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002032 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002033 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002034 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002035}
2036
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002037/*
2038 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002039 * "lnum_props" is the line that has the properties from before the split.
2040 * "lnum_top" is the top line.
2041 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002042 * "deleted" is the number of bytes deleted.
2043 */
2044 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002045adjust_props_for_split(
2046 linenr_T lnum_props,
2047 linenr_T lnum_top,
2048 int kept,
2049 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002050{
2051 char_u *props;
2052 int count;
2053 garray_T prevprop;
2054 garray_T nextprop;
2055 int i;
2056 int skipped = kept + deleted;
2057
2058 if (!curbuf->b_has_textprop)
2059 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002060
2061 // Get the text properties from "lnum_props".
2062 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002063 ga_init2(&prevprop, sizeof(textprop_T), 10);
2064 ga_init2(&nextprop, sizeof(textprop_T), 10);
2065
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002066 // Keep the relevant ones in the first line, reducing the length if needed.
2067 // Copy the ones that include the split to the second line.
2068 // Move the ones after the split to the second line.
2069 for (i = 0; i < count; ++i)
2070 {
2071 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002072 proptype_T *pt;
2073 int start_incl, end_incl;
2074 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002075
2076 // copy the prop to an aligned structure
2077 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2078
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002079 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2080 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2081 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002082 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2083 cont_next = prop.tp_col != MAXCOL
2084 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002085
2086 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002087 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002088 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2089
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002090 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002091 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002092 if (p->tp_col + p->tp_len >= kept)
2093 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002094 if (cont_next)
2095 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002096 }
2097
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002098 // Only add the property to the next line if the length is bigger than
2099 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002100 if ((cont_next || prop.tp_col == MAXCOL)
2101 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002102 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002103 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002104
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002105 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002106 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002107 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002108 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002109 if (p->tp_col > skipped)
2110 p->tp_col -= skipped - 1;
2111 else
2112 {
2113 p->tp_len -= skipped - p->tp_col;
2114 p->tp_col = 1;
2115 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002116 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002117 if (cont_prev)
2118 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002119 }
2120 }
2121
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002122 set_text_props(lnum_top, prevprop.ga_data,
2123 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002124 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002125 set_text_props(lnum_top + 1, nextprop.ga_data,
2126 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002127 ga_clear(&nextprop);
2128}
2129
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002130/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002131 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002132 */
2133 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002134prepend_joined_props(
2135 char_u *new_props,
2136 int propcount,
2137 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002138 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002139 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002140 long col,
2141 int removed)
2142{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002143 char_u *props;
2144 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2145 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002146
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002147 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002148 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002149 textprop_T prop;
2150 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002151
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002152 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002153 if (prop.tp_col == MAXCOL && !last_line)
2154 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002155 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2156
2157 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002158 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002159
Bram Moolenaare175dc62022-08-01 22:18:50 +01002160 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002161 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2162 &prop, sizeof(prop));
2163 else
2164 {
2165 int j;
2166 int found = FALSE;
2167
2168 // Search for continuing prop.
2169 for (j = *props_remaining; j < propcount; ++j)
2170 {
2171 textprop_T op;
2172
2173 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2174 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2175 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002176 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002177 found = TRUE;
2178 op.tp_len += op.tp_col - prop.tp_col;
2179 op.tp_col = prop.tp_col;
2180 // Start/end is taken care of when deleting joined lines
2181 op.tp_flags = prop.tp_flags;
2182 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2183 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002184 }
2185 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002186 if (!found)
2187 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002188 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002189 }
2190}
2191
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002192#endif // FEAT_PROP_POPUP