blob: 88156c272e1ce0a02a2e0d48696758ad27200f2e [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".
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100593 * A "right" aligned property also goes below after a "below" or other "right"
594 * aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100595 */
596 int
597prop_count_below(buf_T *buf, linenr_T lnum)
598{
599 char_u *props;
600 int count = get_text_props(buf, lnum, &props, FALSE);
601 int result = 0;
602 textprop_T prop;
603 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100604 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100605
606 if (count == 0)
607 return 0;
608 for (i = 0; i < count; ++i)
609 {
610 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100611 if (prop.tp_col == MAXCOL)
612 {
613 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
614 || (next_right_goes_below
615 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
616 {
617 next_right_goes_below = TRUE;
618 ++result;
619 }
620 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
621 next_right_goes_below = TRUE;
622 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100623 }
624 return result;
625}
626
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200627/**
628 * Return the number of text properties on line "lnum" in the current buffer.
629 * When "only_starting" is true only text properties starting in this line will
630 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100631 * When "last_line" is FALSE then text properties after the line are not
632 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200633 */
634 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100635count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200636{
637 char_u *props;
638 int proplen = get_text_props(curbuf, lnum, &props, 0);
639 int result = proplen;
640 int i;
641 textprop_T prop;
642
Bram Moolenaare175dc62022-08-01 22:18:50 +0100643 for (i = 0; i < proplen; ++i)
644 {
645 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100646 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100647 // previous line, or when not in the last line and it is virtual text
648 // after the line.
649 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
650 || (!last_line && prop.tp_col == MAXCOL))
651 --result;
652 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200653 return result;
654}
655
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100656/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200657 * Find text property "type_id" in the visible lines of window "wp".
658 * Match "id" when it is > 0.
659 * Returns FAIL when not found.
660 */
661 int
662find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
663 linenr_T *found_lnum)
664{
665 linenr_T lnum;
666 char_u *props;
667 int count;
668 int i;
669
670 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100671 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200672 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
673 {
674 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
675 for (i = 0; i < count; ++i)
676 {
677 mch_memmove(prop, props + i * sizeof(textprop_T),
678 sizeof(textprop_T));
679 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
680 {
681 *found_lnum = lnum;
682 return OK;
683 }
684 }
685 }
686 return FAIL;
687}
688
689/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100690 * Set the text properties for line "lnum" to "props" with length "len".
691 * If "len" is zero text properties are removed, "props" is not used.
692 * Any existing text properties are dropped.
693 * Only works for the current buffer.
694 */
695 static void
696set_text_props(linenr_T lnum, char_u *props, int len)
697{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100698 char_u *text;
699 char_u *newtext;
700 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100701
702 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100703 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100704 newtext = alloc(textlen + len);
705 if (newtext == NULL)
706 return;
707 mch_memmove(newtext, text, textlen);
708 if (len > 0)
709 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100710 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100711 vim_free(curbuf->b_ml.ml_line_ptr);
712 curbuf->b_ml.ml_line_ptr = newtext;
713 curbuf->b_ml.ml_line_len = textlen + len;
714 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
715}
716
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100717/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100718 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100719 */
720 void
721add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
722{
723 char_u *text;
724 char_u *newtext;
725 int proplen = text_prop_count * (int)sizeof(textprop_T);
726
727 text = ml_get(lnum);
728 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
729 if (newtext == NULL)
730 return;
731 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
732 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
733 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
734 vim_free(curbuf->b_ml.ml_line_ptr);
735 curbuf->b_ml.ml_line_ptr = newtext;
736 curbuf->b_ml.ml_line_len += proplen;
737 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
738}
739
Bram Moolenaare44336b2022-08-07 18:20:08 +0100740/*
741 * Function passed to qsort() for sorting proptype_T on pt_id.
742 */
743 static int
744compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100745{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100746 proptype_T *tp1 = *(proptype_T **)s1;
747 proptype_T *tp2 = *(proptype_T **)s2;
748
749 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
750}
751
752 static proptype_T *
753find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
754{
755 int low = 0;
756 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100757
Bram Moolenaar10246902022-08-08 17:08:05 +0100758 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100759 return NULL;
760
Bram Moolenaare44336b2022-08-07 18:20:08 +0100761 // Make the loopup faster by creating an array with pointers to
762 // hashtable entries, sorted on pt_id.
763 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100764 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100765 long todo;
766 hashitem_T *hi;
767 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100768
Bram Moolenaare44336b2022-08-07 18:20:08 +0100769 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
770 if (*array == NULL)
771 return NULL;
772 todo = (long)ht->ht_used;
773 for (hi = ht->ht_array; todo > 0; ++hi)
774 {
775 if (!HASHITEM_EMPTY(hi))
776 {
777 (*array)[i++] = HI2PT(hi);
778 --todo;
779 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100780 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100781 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
782 }
783
784 // binary search in the sorted array
785 high = ht->ht_used;
786 while (high > low)
787 {
788 int m = (high + low) / 2;
789
790 if ((*array)[m]->pt_id == id)
791 return (*array)[m];
792 if ((*array)[m]->pt_id > id)
793 high = m;
794 else
795 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100796 }
797 return NULL;
798}
799
800/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100801 * Fill 'dict' with text properties in 'prop'.
802 */
803 static void
804prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
805{
806 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200807 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100808
809 dict_add_number(dict, "col", prop->tp_col);
810 dict_add_number(dict, "length", prop->tp_len);
811 dict_add_number(dict, "id", prop->tp_id);
812 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
813 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200814
Bram Moolenaare44336b2022-08-07 18:20:08 +0100815 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200816 if (pt == NULL)
817 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100818 pt = find_type_by_id(global_proptypes, &global_proparray,
819 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200820 buflocal = FALSE;
821 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100822 if (pt != NULL)
823 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200824
825 if (buflocal)
826 dict_add_number(dict, "type_bufnr", buf->b_fnum);
827 else
828 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100829}
830
831/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100832 * Find a property type by ID in "buf" or globally.
833 * Returns NULL if not found.
834 */
835 proptype_T *
836text_prop_type_by_id(buf_T *buf, int id)
837{
838 proptype_T *type;
839
Bram Moolenaare44336b2022-08-07 18:20:08 +0100840 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100841 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100842 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100843 return type;
844}
845
846/*
847 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
848 */
849 void
850f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
851{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200852 linenr_T start;
853 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100854 linenr_T lnum;
855 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100856 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100857
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200858 if (in_vim9script()
859 && (check_for_number_arg(argvars, 0) == FAIL
860 || check_for_opt_number_arg(argvars, 1) == FAIL
861 || (argvars[1].v_type != VAR_UNKNOWN
862 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
863 return;
864
865 start = tv_get_number(&argvars[0]);
866 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100867 if (argvars[1].v_type != VAR_UNKNOWN)
868 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100869 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100870 if (argvars[2].v_type != VAR_UNKNOWN)
871 {
872 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
873 return;
874 }
875 }
876 if (start < 1 || end < 1)
877 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200878 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100879 return;
880 }
881
882 for (lnum = start; lnum <= end; ++lnum)
883 {
884 char_u *text;
885 size_t len;
886
887 if (lnum > buf->b_ml.ml_line_count)
888 break;
889 text = ml_get_buf(buf, lnum, FALSE);
890 len = STRLEN(text) + 1;
891 if ((size_t)buf->b_ml.ml_line_len > len)
892 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100893 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100894 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
895 {
896 char_u *newtext = vim_strsave(text);
897
898 // need to allocate the line now
899 if (newtext == NULL)
900 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100901 if (buf->b_ml.ml_flags & ML_ALLOCATED)
902 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100903 buf->b_ml.ml_line_ptr = newtext;
904 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
905 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100906 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100907 }
908 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100909 if (did_clear)
910 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100911}
912
913/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100914 * prop_find({props} [, {direction}])
915 */
916 void
917f_prop_find(typval_T *argvars, typval_T *rettv)
918{
919 pos_T *cursor = &curwin->w_cursor;
920 dict_T *dict;
921 buf_T *buf = curbuf;
922 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200923 int lnum_start;
924 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100925 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200926 int id = 0;
927 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200928 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100929 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200930 int lnum = -1;
931 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100932 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100933 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100934
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200935 if (in_vim9script()
936 && (check_for_dict_arg(argvars, 0) == FAIL
937 || check_for_opt_string_arg(argvars, 1) == FAIL))
938 return;
939
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100940 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
941 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000942 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100943 return;
944 }
945 dict = argvars[0].vval.v_dict;
946
947 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
948 return;
949 if (buf->b_ml.ml_mfp == NULL)
950 return;
951
952 if (argvars[1].v_type != VAR_UNKNOWN)
953 {
954 char_u *dir_s = tv_get_string(&argvars[1]);
955
956 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100957 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100958 else if (*dir_s != 'f')
959 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000960 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100961 return;
962 }
963 }
964
965 di = dict_find(dict, (char_u *)"lnum", -1);
966 if (di != NULL)
967 lnum = tv_get_number(&di->di_tv);
968
969 di = dict_find(dict, (char_u *)"col", -1);
970 if (di != NULL)
971 col = tv_get_number(&di->di_tv);
972
973 if (lnum == -1)
974 {
975 lnum = cursor->lnum;
976 col = cursor->col + 1;
977 }
978 else if (col == -1)
979 col = 1;
980
981 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
982 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200983 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100984 return;
985 }
986
Bram Moolenaard61efa52022-07-23 09:52:04 +0100987 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100988
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100989 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200990 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100991 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200992 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200993 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100994 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100995 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100996 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100997 proptype_T *type = lookup_prop_type(name, buf);
998
999 if (type == NULL)
1000 return;
1001 type_id = type->pt_id;
1002 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001003 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001004 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001005 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001006 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001007 return;
1008 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001009 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001010 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001011 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001012 return;
1013 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001014
1015 lnum_start = lnum;
1016
1017 if (rettv_dict_alloc(rettv) == FAIL)
1018 return;
1019
1020 while (1)
1021 {
1022 char_u *text = ml_get_buf(buf, lnum, FALSE);
1023 size_t textlen = STRLEN(text) + 1;
1024 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001025 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001026 int i;
1027 textprop_T prop;
1028 int prop_start;
1029 int prop_end;
1030
LemonBoy9bd3ce22022-04-18 21:54:02 +01001031 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001032 {
1033 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001034 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001035
LemonBoy9bd3ce22022-04-18 21:54:02 +01001036 // For the very first line try to find the first property before or
1037 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001038 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001039 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001040 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001041 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001042 if (prop.tp_col > col)
1043 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001044 }
1045 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1046 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001047 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001048 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001049 : (id_found && prop.tp_id == id)
1050 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001051 {
1052 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001053 if (lnum_start == lnum
1054 && col >= prop.tp_col
1055 && (col <= prop.tp_col + prop.tp_len
1056 - (prop.tp_len != 0)))
1057 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001058
LemonBoy9bd3ce22022-04-18 21:54:02 +01001059 // The property was not continued from last line, it starts on
1060 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001061 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001062 // The property does not continue on the next line, it ends on
1063 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001064 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001065 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001066 seen_end = 1;
1067
1068 // Skip lines without the start flag.
1069 if (!prop_start)
1070 {
1071 // Always search backwards for start when search started
1072 // on a prop and we're not skipping.
1073 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001074 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001075 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001076 }
1077
1078 // If skipstart is true, skip the prop at start pos (even if
1079 // continued from another line).
1080 if (start_pos_has_prop && skipstart && !seen_end)
1081 {
1082 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001083 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001084 }
1085
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001086 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1087 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1088
1089 return;
1090 }
1091 }
1092
1093 if (dir > 0)
1094 {
1095 if (lnum >= buf->b_ml.ml_line_count)
1096 break;
1097 lnum++;
1098 }
1099 else
1100 {
1101 if (lnum <= 1)
1102 break;
1103 lnum--;
1104 }
1105 }
1106}
1107
1108/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001109 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1110 */
1111 static int
1112prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1113{
1114 int i;
1115
1116 for (i = 0; i < len; i++)
1117 if (types_or_ids[i] == type_or_id)
1118 return TRUE;
1119
1120 return FALSE;
1121}
1122
1123/*
1124 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1125 * If 'prop_types' is not NULL, then return only the text properties with
1126 * matching property type in the 'prop_types' array.
1127 * If 'prop_ids' is not NULL, then return only the text properties with
1128 * an identifier in the 'props_ids' array.
1129 * If 'add_lnum' is TRUE, then add the line number also to the text property
1130 * dictionary.
1131 */
1132 static void
1133get_props_in_line(
1134 buf_T *buf,
1135 linenr_T lnum,
1136 int *prop_types,
1137 int prop_types_len,
1138 int *prop_ids,
1139 int prop_ids_len,
1140 list_T *retlist,
1141 int add_lnum)
1142{
1143 char_u *text = ml_get_buf(buf, lnum, FALSE);
1144 size_t textlen = STRLEN(text) + 1;
1145 int count;
1146 int i;
1147 textprop_T prop;
1148
1149 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1150 for (i = 0; i < count; ++i)
1151 {
1152 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1153 sizeof(textprop_T));
1154 if ((prop_types == NULL
1155 || prop_type_or_id_in_list(prop_types, prop_types_len,
1156 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001157 && (prop_ids == NULL
1158 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1159 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001160 {
1161 dict_T *d = dict_alloc();
1162
1163 if (d == NULL)
1164 break;
1165 prop_fill_dict(d, &prop, buf);
1166 if (add_lnum)
1167 dict_add_number(d, "lnum", lnum);
1168 list_append_dict(retlist, d);
1169 }
1170 }
1171}
1172
1173/*
1174 * Convert a List of property type names into an array of property type
1175 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1176 * error. 'num_types' is set to the number of returned property types.
1177 */
1178 static int *
1179get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1180{
1181 int *prop_types;
1182 listitem_T *li;
1183 int i;
1184 char_u *name;
1185 proptype_T *type;
1186
1187 *num_types = 0;
1188
1189 prop_types = ALLOC_MULT(int, list_len(l));
1190 if (prop_types == NULL)
1191 return NULL;
1192
1193 i = 0;
1194 FOR_ALL_LIST_ITEMS(l, li)
1195 {
1196 if (li->li_tv.v_type != VAR_STRING)
1197 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001198 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001199 goto errret;
1200 }
1201 name = li->li_tv.vval.v_string;
1202 if (name == NULL)
1203 goto errret;
1204
1205 type = lookup_prop_type(name, buf);
1206 if (type == NULL)
1207 goto errret;
1208 prop_types[i++] = type->pt_id;
1209 }
1210
1211 *num_types = i;
1212 return prop_types;
1213
1214errret:
1215 VIM_CLEAR(prop_types);
1216 return NULL;
1217}
1218
1219/*
1220 * Convert a List of property identifiers into an array of property
1221 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1222 * error. 'num_ids' is set to the number of returned property identifiers.
1223 */
1224 static int *
1225get_prop_ids_from_list(list_T *l, int *num_ids)
1226{
1227 int *prop_ids;
1228 listitem_T *li;
1229 int i;
1230 int id;
1231 int error;
1232
1233 *num_ids = 0;
1234
1235 prop_ids = ALLOC_MULT(int, list_len(l));
1236 if (prop_ids == NULL)
1237 return NULL;
1238
1239 i = 0;
1240 FOR_ALL_LIST_ITEMS(l, li)
1241 {
1242 error = FALSE;
1243 id = tv_get_number_chk(&li->li_tv, &error);
1244 if (error)
1245 goto errret;
1246
1247 prop_ids[i++] = id;
1248 }
1249
1250 *num_ids = i;
1251 return prop_ids;
1252
1253errret:
1254 VIM_CLEAR(prop_ids);
1255 return NULL;
1256}
1257
1258/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001259 * prop_list({lnum} [, {bufnr}])
1260 */
1261 void
1262f_prop_list(typval_T *argvars, typval_T *rettv)
1263{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001264 linenr_T lnum;
1265 linenr_T start_lnum;
1266 linenr_T end_lnum;
1267 buf_T *buf = curbuf;
1268 int add_lnum = FALSE;
1269 int *prop_types = NULL;
1270 int prop_types_len = 0;
1271 int *prop_ids = NULL;
1272 int prop_ids_len = 0;
1273 list_T *l;
1274 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001275
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001276 if (in_vim9script()
1277 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001278 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001279 return;
1280
Bram Moolenaar93a10962022-06-16 11:42:09 +01001281 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001282 return;
1283
1284 // default: get text properties on current line
1285 start_lnum = tv_get_number(&argvars[0]);
1286 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001287 if (argvars[1].v_type != VAR_UNKNOWN)
1288 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001289 dict_T *d;
1290
1291 if (argvars[1].v_type != VAR_DICT)
1292 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001293 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001294 return;
1295 }
1296 d = argvars[1].vval.v_dict;
1297
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001298 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1299 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001300
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001301 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001302 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001303 if (di->di_tv.v_type != VAR_NUMBER)
1304 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001305 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001306 return;
1307 }
1308 end_lnum = tv_get_number(&di->di_tv);
1309 if (end_lnum < 0)
1310 // negative end_lnum is used as an offset from the last buffer
1311 // line
1312 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1313 else if (end_lnum > buf->b_ml.ml_line_count)
1314 end_lnum = buf->b_ml.ml_line_count;
1315 add_lnum = TRUE;
1316 }
1317 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1318 {
1319 if (di->di_tv.v_type != VAR_LIST)
1320 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001321 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001322 return;
1323 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001324
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001325 l = di->di_tv.vval.v_list;
1326 if (l != NULL && list_len(l) > 0)
1327 {
1328 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1329 if (prop_types == NULL)
1330 return;
1331 }
1332 }
1333 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1334 {
1335 if (di->di_tv.v_type != VAR_LIST)
1336 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001337 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001338 goto errret;
1339 }
1340
1341 l = di->di_tv.vval.v_list;
1342 if (l != NULL && list_len(l) > 0)
1343 {
1344 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1345 if (prop_ids == NULL)
1346 goto errret;
1347 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001348 }
1349 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001350 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1351 || end_lnum < 1 || end_lnum < start_lnum)
1352 emsg(_(e_invalid_range));
1353 else
1354 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1355 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1356 prop_ids, prop_ids_len,
1357 rettv->vval.v_list, add_lnum);
1358
1359errret:
1360 VIM_CLEAR(prop_types);
1361 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001362}
1363
1364/*
1365 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1366 */
1367 void
1368f_prop_remove(typval_T *argvars, typval_T *rettv)
1369{
1370 linenr_T start = 1;
1371 linenr_T end = 0;
1372 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001373 linenr_T first_changed = 0;
1374 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001375 dict_T *dict;
1376 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001377 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001378 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001379 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001380 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001381 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001382
1383 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001384
1385 if (in_vim9script()
1386 && (check_for_dict_arg(argvars, 0) == FAIL
1387 || check_for_opt_number_arg(argvars, 1) == FAIL
1388 || (argvars[1].v_type != VAR_UNKNOWN
1389 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1390 return;
1391
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1393 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001394 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001395 return;
1396 }
1397
1398 if (argvars[1].v_type != VAR_UNKNOWN)
1399 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001400 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001401 end = start;
1402 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001403 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001404 if (start < 1 || end < 1)
1405 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001406 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001407 return;
1408 }
1409 }
1410
1411 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001412 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1413 return;
1414 if (buf->b_ml.ml_mfp == NULL)
1415 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001416
Bram Moolenaard61efa52022-07-23 09:52:04 +01001417 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001418
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001419 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001420 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001421 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001422 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001423 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001424 proptype_T *type = lookup_prop_type(name, buf);
1425
1426 if (type == NULL)
1427 return;
1428 type_id = type->pt_id;
1429 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001430 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001431
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001432 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001433 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001434 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001435 return;
1436 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001437 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001438 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001439 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001440 return;
1441 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001442
1443 if (end == 0)
1444 end = buf->b_ml.ml_line_count;
1445 for (lnum = start; lnum <= end; ++lnum)
1446 {
1447 char_u *text;
1448 size_t len;
1449
1450 if (lnum > buf->b_ml.ml_line_count)
1451 break;
1452 text = ml_get_buf(buf, lnum, FALSE);
1453 len = STRLEN(text) + 1;
1454 if ((size_t)buf->b_ml.ml_line_len > len)
1455 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001456 static textprop_T textprop; // static because of alignment
1457 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001458
1459 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1460 / sizeof(textprop_T); ++idx)
1461 {
1462 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1463 + idx * sizeof(textprop_T);
1464 size_t taillen;
1465
1466 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001467 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1468 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001469 {
1470 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1471 {
1472 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1473
1474 // need to allocate the line to be able to change it
1475 if (newptr == NULL)
1476 return;
1477 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1478 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001479 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1480 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001481 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001482 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1483
1484 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001485 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001486 }
1487
1488 taillen = buf->b_ml.ml_line_len - len
1489 - (idx + 1) * sizeof(textprop_T);
1490 if (taillen > 0)
1491 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1492 taillen);
1493 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1494 --idx;
1495
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001496 if (textprop.tp_id < 0)
1497 {
1498 garray_T *gap = &buf->b_textprop_text;
1499 int ii = -textprop.tp_id - 1;
1500
1501 // negative ID: property with text - free the text
1502 if (ii < gap->ga_len)
1503 {
1504 char_u **p = ((char_u **)gap->ga_data) + ii;
1505 vim_free(*p);
1506 *p = NULL;
1507 did_remove_text = TRUE;
1508 }
1509 }
1510
Bram Moolenaar965c0442021-05-17 00:22:06 +02001511 if (first_changed == 0)
1512 first_changed = lnum;
1513 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001514 ++rettv->vval.v_number;
1515 if (!do_all)
1516 break;
1517 }
1518 }
1519 }
1520 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001521
Bram Moolenaar965c0442021-05-17 00:22:06 +02001522 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001523 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001524 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001525 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1526 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001527 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001528
1529 if (did_remove_text)
1530 {
1531 garray_T *gap = &buf->b_textprop_text;
1532
1533 // Reduce the growarray size for NULL pointers at the end.
1534 while (gap->ga_len > 0
1535 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1536 --gap->ga_len;
1537 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001538}
1539
1540/*
1541 * Common for f_prop_type_add() and f_prop_type_change().
1542 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001543 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001544prop_type_set(typval_T *argvars, int add)
1545{
1546 char_u *name;
1547 buf_T *buf = NULL;
1548 dict_T *dict;
1549 dictitem_T *di;
1550 proptype_T *prop;
1551
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001552 if (in_vim9script()
1553 && (check_for_string_arg(argvars, 0) == FAIL
1554 || check_for_dict_arg(argvars, 1) == FAIL))
1555 return;
1556
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001557 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001558 if (*name == NUL)
1559 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001560 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001561 return;
1562 }
1563
1564 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1565 return;
1566 dict = argvars[1].vval.v_dict;
1567
Bram Moolenaare44336b2022-08-07 18:20:08 +01001568 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001569 if (add)
1570 {
1571 hashtab_T **htp;
1572
1573 if (prop != NULL)
1574 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001575 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001576 return;
1577 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001578 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001579 if (prop == NULL)
1580 return;
1581 STRCPY(prop->pt_name, name);
1582 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001583 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001584 if (buf == NULL)
1585 {
1586 htp = &global_proptypes;
1587 VIM_CLEAR(global_proparray);
1588 }
1589 else
1590 {
1591 htp = &buf->b_proptypes;
1592 VIM_CLEAR(buf->b_proparray);
1593 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001594 if (*htp == NULL)
1595 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001596 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001597 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001598 {
1599 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001600 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001601 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001602 hash_init(*htp);
1603 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001604 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001605 }
1606 else
1607 {
1608 if (prop == NULL)
1609 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001610 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001611 return;
1612 }
1613 }
1614
1615 if (dict != NULL)
1616 {
1617 di = dict_find(dict, (char_u *)"highlight", -1);
1618 if (di != NULL)
1619 {
1620 char_u *highlight;
1621 int hl_id = 0;
1622
Bram Moolenaard61efa52022-07-23 09:52:04 +01001623 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001624 if (highlight != NULL && *highlight != NUL)
1625 hl_id = syn_name2id(highlight);
1626 if (hl_id <= 0)
1627 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001628 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001629 highlight == NULL ? (char_u *)"" : highlight);
1630 return;
1631 }
1632 prop->pt_hl_id = hl_id;
1633 }
1634
Bram Moolenaar58187f12019-05-05 16:33:47 +02001635 di = dict_find(dict, (char_u *)"combine", -1);
1636 if (di != NULL)
1637 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001638 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001639 prop->pt_flags |= PT_FLAG_COMBINE;
1640 else
1641 prop->pt_flags &= ~PT_FLAG_COMBINE;
1642 }
1643
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001644 di = dict_find(dict, (char_u *)"override", -1);
1645 if (di != NULL)
1646 {
1647 if (tv_get_bool(&di->di_tv))
1648 prop->pt_flags |= PT_FLAG_OVERRIDE;
1649 else
1650 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1651 }
1652
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001653 di = dict_find(dict, (char_u *)"priority", -1);
1654 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001655 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001656
1657 di = dict_find(dict, (char_u *)"start_incl", -1);
1658 if (di != NULL)
1659 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001660 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001661 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1662 else
1663 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1664 }
1665
1666 di = dict_find(dict, (char_u *)"end_incl", -1);
1667 if (di != NULL)
1668 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001669 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001670 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1671 else
1672 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1673 }
1674 }
1675}
1676
1677/*
1678 * prop_type_add({name}, {props})
1679 */
1680 void
1681f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1682{
1683 prop_type_set(argvars, TRUE);
1684}
1685
1686/*
1687 * prop_type_change({name}, {props})
1688 */
1689 void
1690f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1691{
1692 prop_type_set(argvars, FALSE);
1693}
1694
1695/*
1696 * prop_type_delete({name} [, {bufnr}])
1697 */
1698 void
1699f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1700{
1701 char_u *name;
1702 buf_T *buf = NULL;
1703 hashitem_T *hi;
1704
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001705 if (in_vim9script()
1706 && (check_for_string_arg(argvars, 0) == FAIL
1707 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1708 return;
1709
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001710 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001711 if (*name == NUL)
1712 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001713 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001714 return;
1715 }
1716
1717 if (argvars[1].v_type != VAR_UNKNOWN)
1718 {
1719 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1720 return;
1721 }
1722
Bram Moolenaare44336b2022-08-07 18:20:08 +01001723 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001724 if (hi != NULL)
1725 {
1726 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001727 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001728
1729 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001730 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001731 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001732 VIM_CLEAR(global_proparray);
1733 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001734 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001735 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001736 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001737 VIM_CLEAR(buf->b_proparray);
1738 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001739 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001740 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001741 }
1742}
1743
1744/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001745 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001746 */
1747 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001748f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001749{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001750 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001751
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001752 if (in_vim9script()
1753 && (check_for_string_arg(argvars, 0) == FAIL
1754 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1755 return;
1756
1757 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001758 if (*name == NUL)
1759 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001760 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001761 return;
1762 }
1763 if (rettv_dict_alloc(rettv) == OK)
1764 {
1765 proptype_T *prop = NULL;
1766 buf_T *buf = NULL;
1767
1768 if (argvars[1].v_type != VAR_UNKNOWN)
1769 {
1770 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1771 return;
1772 }
1773
Bram Moolenaare44336b2022-08-07 18:20:08 +01001774 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001775 if (prop != NULL)
1776 {
1777 dict_T *d = rettv->vval.v_dict;
1778
1779 if (prop->pt_hl_id > 0)
1780 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1781 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001782 dict_add_number(d, "combine",
1783 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001784 dict_add_number(d, "start_incl",
1785 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1786 dict_add_number(d, "end_incl",
1787 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1788 if (buf != NULL)
1789 dict_add_number(d, "bufnr", buf->b_fnum);
1790 }
1791 }
1792}
1793
1794 static void
1795list_types(hashtab_T *ht, list_T *l)
1796{
1797 long todo;
1798 hashitem_T *hi;
1799
1800 todo = (long)ht->ht_used;
1801 for (hi = ht->ht_array; todo > 0; ++hi)
1802 {
1803 if (!HASHITEM_EMPTY(hi))
1804 {
1805 proptype_T *prop = HI2PT(hi);
1806
1807 list_append_string(l, prop->pt_name, -1);
1808 --todo;
1809 }
1810 }
1811}
1812
1813/*
1814 * prop_type_list([{bufnr}])
1815 */
1816 void
1817f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1818{
1819 buf_T *buf = NULL;
1820
1821 if (rettv_list_alloc(rettv) == OK)
1822 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001823 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1824 return;
1825
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001826 if (argvars[0].v_type != VAR_UNKNOWN)
1827 {
1828 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1829 return;
1830 }
1831 if (buf == NULL)
1832 {
1833 if (global_proptypes != NULL)
1834 list_types(global_proptypes, rettv->vval.v_list);
1835 }
1836 else if (buf->b_proptypes != NULL)
1837 list_types(buf->b_proptypes, rettv->vval.v_list);
1838 }
1839}
1840
1841/*
1842 * Free all property types in "ht".
1843 */
1844 static void
1845clear_ht_prop_types(hashtab_T *ht)
1846{
1847 long todo;
1848 hashitem_T *hi;
1849
1850 if (ht == NULL)
1851 return;
1852
1853 todo = (long)ht->ht_used;
1854 for (hi = ht->ht_array; todo > 0; ++hi)
1855 {
1856 if (!HASHITEM_EMPTY(hi))
1857 {
1858 proptype_T *prop = HI2PT(hi);
1859
1860 vim_free(prop);
1861 --todo;
1862 }
1863 }
1864
1865 hash_clear(ht);
1866 vim_free(ht);
1867}
1868
1869#if defined(EXITFREE) || defined(PROTO)
1870/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001871 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001872 */
1873 void
1874clear_global_prop_types(void)
1875{
1876 clear_ht_prop_types(global_proptypes);
1877 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001878 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001879}
1880#endif
1881
1882/*
1883 * Free all property types for "buf".
1884 */
1885 void
1886clear_buf_prop_types(buf_T *buf)
1887{
1888 clear_ht_prop_types(buf->b_proptypes);
1889 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001890 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001891}
1892
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001893// Struct used to return two values from adjust_prop().
1894typedef struct
1895{
1896 int dirty; // if the property was changed
1897 int can_drop; // whether after this change, the prop may be removed
1898} adjustres_T;
1899
1900/*
1901 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1902 *
1903 * Note that "col" is zero-based, while tp_col is one-based.
1904 * Only for the current buffer.
1905 * "flags" can have:
1906 * APC_SUBSTITUTE: Text is replaced, not inserted.
1907 */
1908 static adjustres_T
1909adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001910 textprop_T *prop,
1911 colnr_T col,
1912 int added,
1913 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001914{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001915 proptype_T *pt;
1916 int start_incl;
1917 int end_incl;
1918 int droppable;
1919 adjustres_T res = {TRUE, FALSE};
1920
1921 // prop after end of the line doesn't move
1922 if (prop->tp_col == MAXCOL)
1923 {
1924 res.dirty = FALSE;
1925 return res;
1926 }
1927
1928 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1929 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001930 || (flags & APC_SUBSTITUTE)
1931 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001932 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001933 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001934 // do not drop zero-width props if they later can increase in size
1935 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001936
1937 if (added > 0)
1938 {
1939 if (col + 1 <= prop->tp_col
1940 - (start_incl || (prop->tp_len == 0 && end_incl)))
1941 // Change is entirely before the text property: Only shift
1942 prop->tp_col += added;
1943 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1944 // Insertion was inside text property
1945 prop->tp_len += added;
1946 }
1947 else if (prop->tp_col > col + 1)
1948 {
1949 if (prop->tp_col + added < col + 1)
1950 {
1951 prop->tp_len += (prop->tp_col - 1 - col) + added;
1952 prop->tp_col = col + 1;
1953 if (prop->tp_len <= 0)
1954 {
1955 prop->tp_len = 0;
1956 res.can_drop = droppable;
1957 }
1958 }
1959 else
1960 prop->tp_col += added;
1961 }
1962 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1963 {
1964 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1965
1966 prop->tp_len += after > 0 ? added + after : added;
1967 res.can_drop = prop->tp_len <= 0 && droppable;
1968 }
1969 else
1970 res.dirty = FALSE;
1971
1972 return res;
1973}
1974
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001975/*
1976 * Adjust the columns of text properties in line "lnum" after position "col" to
1977 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001978 * Note that "col" is zero-based, while tp_col is one-based.
1979 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001980 * "flags" can have:
1981 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1982 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001983 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001984 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001985 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001986 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001987adjust_prop_columns(
1988 linenr_T lnum,
1989 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001990 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001991 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001992{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001993 int proplen;
1994 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001995 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001996 int ri, wi;
1997 size_t textlen;
1998
1999 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002000 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002001
2002 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2003 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002004 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002005 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002006
Bram Moolenaar196d1572019-01-02 23:47:18 +01002007 wi = 0; // write index
2008 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002009 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002010 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002011 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002012
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002013 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2014 res = adjust_prop(&prop, col, bytes_added, flags);
2015 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002016 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002017 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002018 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2019 && u_savesub(lnum) == FAIL)
2020 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002021 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002022
2023 // u_savesub() may have updated curbuf->b_ml, fetch it again
2024 if (curbuf->b_ml.ml_line_lnum != lnum)
2025 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002026 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002027 if (res.can_drop)
2028 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002029 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002030 ++wi;
2031 }
2032 if (dirty)
2033 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002034 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2035
2036 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002037 {
2038 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2039
2040 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2041 vim_free(curbuf->b_ml.ml_line_ptr);
2042 curbuf->b_ml.ml_line_ptr = p;
2043 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002044 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002045 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002046 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002047 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002048}
2049
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002050/*
2051 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002052 * "lnum_props" is the line that has the properties from before the split.
2053 * "lnum_top" is the top line.
2054 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002055 * "deleted" is the number of bytes deleted.
2056 */
2057 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002058adjust_props_for_split(
2059 linenr_T lnum_props,
2060 linenr_T lnum_top,
2061 int kept,
2062 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002063{
2064 char_u *props;
2065 int count;
2066 garray_T prevprop;
2067 garray_T nextprop;
2068 int i;
2069 int skipped = kept + deleted;
2070
2071 if (!curbuf->b_has_textprop)
2072 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002073
2074 // Get the text properties from "lnum_props".
2075 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002076 ga_init2(&prevprop, sizeof(textprop_T), 10);
2077 ga_init2(&nextprop, sizeof(textprop_T), 10);
2078
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002079 // Keep the relevant ones in the first line, reducing the length if needed.
2080 // Copy the ones that include the split to the second line.
2081 // Move the ones after the split to the second line.
2082 for (i = 0; i < count; ++i)
2083 {
2084 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002085 proptype_T *pt;
2086 int start_incl, end_incl;
2087 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002088
2089 // copy the prop to an aligned structure
2090 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2091
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002092 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2093 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2094 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002095 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2096 cont_next = prop.tp_col != MAXCOL
2097 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002098
2099 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002100 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002101 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2102
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002103 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002104 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002105 if (p->tp_col + p->tp_len >= kept)
2106 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002107 if (cont_next)
2108 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002109 }
2110
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002111 // Only add the property to the next line if the length is bigger than
2112 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002113 if ((cont_next || prop.tp_col == MAXCOL)
2114 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002115 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002116 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002117
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002118 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002119 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002120 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002121 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002122 if (p->tp_col > skipped)
2123 p->tp_col -= skipped - 1;
2124 else
2125 {
2126 p->tp_len -= skipped - p->tp_col;
2127 p->tp_col = 1;
2128 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002129 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002130 if (cont_prev)
2131 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002132 }
2133 }
2134
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002135 set_text_props(lnum_top, prevprop.ga_data,
2136 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002137 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002138 set_text_props(lnum_top + 1, nextprop.ga_data,
2139 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002140 ga_clear(&nextprop);
2141}
2142
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002143/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002144 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002145 */
2146 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002147prepend_joined_props(
2148 char_u *new_props,
2149 int propcount,
2150 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002151 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002152 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002153 long col,
2154 int removed)
2155{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002156 char_u *props;
2157 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2158 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002159
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002160 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002161 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002162 textprop_T prop;
2163 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002164
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002165 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002166 if (prop.tp_col == MAXCOL && !last_line)
2167 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002168 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2169
2170 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002171 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002172
Bram Moolenaare175dc62022-08-01 22:18:50 +01002173 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002174 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2175 &prop, sizeof(prop));
2176 else
2177 {
2178 int j;
2179 int found = FALSE;
2180
2181 // Search for continuing prop.
2182 for (j = *props_remaining; j < propcount; ++j)
2183 {
2184 textprop_T op;
2185
2186 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2187 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2188 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002189 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002190 found = TRUE;
2191 op.tp_len += op.tp_col - prop.tp_col;
2192 op.tp_col = prop.tp_col;
2193 // Start/end is taken care of when deleting joined lines
2194 op.tp_flags = prop.tp_flags;
2195 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2196 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002197 }
2198 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002199 if (!found)
2200 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002201 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002202 }
2203}
2204
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002205#endif // FEAT_PROP_POPUP