blob: fe0c8d20cbd4604c88091844bfa679d2ef1a6379 [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 Moolenaara9fa8c52023-01-02 18:10:04 +0000101 semsg(_(e_property_type_str_does_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]);
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100152 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153 return;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200154
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100155 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
156 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157}
158
159/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200160 * Attach a text property 'type_name' to the text starting
161 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100162 * the buffer "buf" and assign identifier "id".
163 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200164 */
165 static int
166prop_add_one(
167 buf_T *buf,
168 char_u *type_name,
169 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100170 char_u *text_arg,
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100171 int text_padding_left,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100172 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200173 linenr_T start_lnum,
174 linenr_T end_lnum,
175 colnr_T start_col,
176 colnr_T end_col)
177{
178 proptype_T *type;
179 linenr_T lnum;
180 int proplen;
181 char_u *props = NULL;
182 char_u *newprops;
183 size_t textlen;
184 char_u *newtext;
185 int i;
186 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text = text_arg;
188 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189
190 type = lookup_prop_type(type_name, buf);
191 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200193
194 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
195 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000196 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200198 }
199 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
200 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000201 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200203 }
204
205 if (buf->b_ml.ml_mfp == NULL)
206 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000207 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
209 }
210
211 if (text != NULL)
212 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100213 garray_T *gap = &buf->b_textprop_text;
214 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100215
216 // double check we got the right ID
217 if (-id - 1 != gap->ga_len)
218 iemsg("text prop ID mismatch");
219 if (gap->ga_growsize == 0)
220 ga_init2(gap, sizeof(char *), 50);
221 if (ga_grow(gap, 1) == FAIL)
222 goto theend;
223 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100224
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100225 // change any control character (Tab, Newline, etc.) to a Space to make
226 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100227 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100228 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100230 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200231 }
232
233 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
234 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100235 colnr_T col; // start column use in tp_col
236 colnr_T sort_col; // column where it appears
237 long length; // in bytes
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200238
239 // Fetch the line to get the ml_line_len field updated.
240 proplen = get_text_props(buf, lnum, &props, TRUE);
241 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
242
243 if (lnum == start_lnum)
244 col = start_col;
245 else
246 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100247 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200248 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000249 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100250 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200251 }
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100252 sort_col = col;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200253
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)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100267 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100268 col = MAXCOL; // before or after the line
269 if ((text_flags & TP_FLAG_ALIGN_ABOVE) == 0)
270 sort_col = MAXCOL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100271 length += text_padding_left;
272 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100273 }
274
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200275 // Allocate the new line with space for the new property.
276 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
277 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100278 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200279 // Copy the text, including terminating NUL.
280 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
281
282 // Find the index where to insert the new property.
283 // Since the text properties are not aligned properly when stored with
284 // the text, we need to copy them as bytes before using it as a struct.
285 for (i = 0; i < proplen; ++i)
286 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100287 colnr_T prop_col;
288
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200289 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
290 sizeof(textprop_T));
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100291 // col is MAXCOL when the text goes above or after the line, when
292 // above we should use column zero for sorting
293 prop_col = (tmp_prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
294 ? 0 : tmp_prop.tp_col;
295 if (prop_col >= sort_col)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200296 break;
297 }
298 newprops = newtext + textlen;
299 if (i > 0)
300 mch_memmove(newprops, props, sizeof(textprop_T) * i);
301
302 tmp_prop.tp_col = col;
303 tmp_prop.tp_len = length;
304 tmp_prop.tp_id = id;
305 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100306 tmp_prop.tp_flags = text_flags
307 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100308 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
309 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
310 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200311 tmp_prop.tp_padleft = text_padding_left;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200312 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
313 sizeof(textprop_T));
314
315 if (i < proplen)
316 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
317 props + i * sizeof(textprop_T),
318 sizeof(textprop_T) * (proplen - i));
319
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100320 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200321 vim_free(buf->b_ml.ml_line_ptr);
322 buf->b_ml.ml_line_ptr = newtext;
323 buf->b_ml.ml_line_len += sizeof(textprop_T);
324 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
325 }
326
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100327 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200328 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100329 res = OK;
330
331theend:
332 vim_free(text);
333 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200334}
335
336/*
337 * prop_add_list()
338 * First argument specifies the text property:
339 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
340 * Second argument is a List where each item is a List with the following
341 * entries: [lnum, start_col, end_col]
342 */
343 void
344f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
345{
346 dict_T *dict;
347 char_u *type_name;
348 buf_T *buf = curbuf;
349 int id = 0;
350 listitem_T *li;
351 list_T *pos_list;
352 linenr_T start_lnum;
353 colnr_T start_col;
354 linenr_T end_lnum;
355 colnr_T end_col;
356 int error = FALSE;
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100357 int prev_did_emsg = did_emsg;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200358
359 if (check_for_dict_arg(argvars, 0) == FAIL
360 || check_for_list_arg(argvars, 1) == FAIL)
361 return;
362
Bram Moolenaard83392a2022-09-01 12:22:46 +0100363 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200365
366 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100367 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200368 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000369 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200370 return;
371 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100372 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200373
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100374 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100375 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200376
377 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
378 return;
379
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100380 // This must be done _before_ we start adding properties because property
381 // changes trigger buffer (memline) reorganisation, which needs this flag
382 // to be correctly set.
383 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200384 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
385 {
386 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
387 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000388 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200389 return;
390 }
391
392 pos_list = li->li_tv.vval.v_list;
393 start_lnum = list_find_nr(pos_list, 0L, &error);
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100394 if (!error)
395 start_col = list_find_nr(pos_list, 1L, &error);
396 if (!error)
397 end_lnum = list_find_nr(pos_list, 2L, &error);
398 if (!error)
399 end_col = list_find_nr(pos_list, 3L, &error);
Bram Moolenaard93009e2022-10-13 14:35:24 +0100400 int this_id = id;
401 if (!error && pos_list->lv_len > 4)
402 this_id = list_find_nr(pos_list, 4L, &error);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200403 if (error || start_lnum <= 0 || start_col <= 0
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100404 || end_lnum <= 0 || end_col <= 0)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200405 {
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100406 if (prev_did_emsg == did_emsg)
407 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200408 return;
409 }
Bram Moolenaard93009e2022-10-13 14:35:24 +0100410 if (prop_add_one(buf, type_name, this_id, NULL, 0, 0,
411 start_lnum, end_lnum, start_col, end_col) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200412 return;
413 }
414
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100415 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200416}
417
418/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100419 * Get the next ID to use for a textprop with text in buffer "buf".
420 */
421 static int
422get_textprop_id(buf_T *buf)
423{
424 // TODO: recycle deleted entries
425 return -(buf->b_textprop_text.ga_len + 1);
426}
427
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000428// Flag that is set when a negative ID isused for a normal text property.
429// It is then impossible to use virtual text properties.
430static int did_use_negative_pop_id = FALSE;
431
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100432/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200433 * Shared between prop_add() and popup_create().
434 * "dict_arg" is the function argument of a dict containing "bufnr".
435 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100436 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200437 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100438 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200439prop_add_common(
440 linenr_T start_lnum,
441 colnr_T start_col,
442 dict_T *dict,
443 buf_T *default_buf,
444 typval_T *dict_arg)
445{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200446 linenr_T end_lnum;
447 colnr_T end_col;
448 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200449 buf_T *buf = default_buf;
450 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100451 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100452 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100453 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100454
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100455 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100456 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000457 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100458 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100459 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100460 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100461
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100462 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100463 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100464 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100465 if (end_lnum < start_lnum)
466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000467 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100468 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100469 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100470 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 else
472 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100474 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100475 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100476 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100477
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100478 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100479 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000480 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100481 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100482 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100483 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100484 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100485 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100486 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100487 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100488 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100489 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000490 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100491 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100492 }
493 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100494 else if (start_lnum == end_lnum)
495 end_col = start_col;
496 else
497 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100498
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100499 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100500 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100501
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100502 if (dict_has_key(dict, "text"))
503 {
Bram Moolenaarfb593c52022-09-17 18:57:36 +0100504 if (dict_has_key(dict, "length")
505 || dict_has_key(dict, "end_col")
506 || dict_has_key(dict, "end_lnum"))
507 {
508 emsg(_(e_cannot_use_length_endcol_and_endlnum_with_text));
509 goto theend;
510 }
511
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100512 text = dict_get_string(dict, "text", TRUE);
513 if (text == NULL)
514 goto theend;
515 // use a default length of 1 to make multiple props show up
516 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100517
518 if (dict_has_key(dict, "text_align"))
519 {
520 char_u *p = dict_get_string(dict, "text_align", FALSE);
521
522 if (p == NULL)
523 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100524 if (start_col != 0)
525 {
526 emsg(_(e_can_only_use_text_align_when_column_is_zero));
527 goto theend;
528 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100529 if (STRCMP(p, "right") == 0)
530 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100531 else if (STRCMP(p, "above") == 0)
532 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100533 else if (STRCMP(p, "below") == 0)
534 flags |= TP_FLAG_ALIGN_BELOW;
535 else if (STRCMP(p, "after") != 0)
536 {
537 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
538 goto theend;
539 }
540 }
541
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100542 if (dict_has_key(dict, "text_padding_left"))
543 {
544 text_padding_left = dict_get_number(dict, "text_padding_left");
545 if (text_padding_left < 0)
546 {
547 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
548 goto theend;
549 }
550 }
551
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100552 if (dict_has_key(dict, "text_wrap"))
553 {
554 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100555
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100556 if (p == NULL)
557 goto theend;
558 if (STRCMP(p, "wrap") == 0)
559 flags |= TP_FLAG_WRAP;
560 else if (STRCMP(p, "truncate") != 0)
561 {
562 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
563 goto theend;
564 }
565 }
566 }
567
568 // Column must be 1 or more for a normal text property; when "text" is
569 // present zero means it goes after the line.
570 if (start_col < (text == NULL ? 1 : 0))
571 {
572 semsg(_(e_invalid_column_number_nr), (long)start_col);
573 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100574 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100575 if (start_col > 0 && text_padding_left > 0)
576 {
577 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
578 goto theend;
579 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100580
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200581 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100582 goto theend;
583
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000584 if (id < 0)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100585 {
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000586 if (buf->b_textprop_text.ga_len > 0)
587 {
588 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
589 goto theend;
590 }
591 did_use_negative_pop_id = TRUE;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100592 }
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000593
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100594 if (text != NULL)
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000595 {
596 if (did_use_negative_pop_id)
597 {
598 emsg(_(e_cannot_add_textprop_with_text_after_using_textprop_with_negative_id));
599 goto theend;
600 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100601 id = get_textprop_id(buf);
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000602 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100603
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100604 // This must be done _before_ we add the property because property changes
605 // trigger buffer (memline) reorganisation, which needs this flag to be
606 // correctly set.
607 buf->b_has_textprop = TRUE; // this is never reset
608
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100609 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100610 start_lnum, end_lnum, start_col, end_col);
611 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100612
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100613 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100614
615theend:
616 vim_free(text);
617 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100618}
619
620/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100621 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100622 * Returns the number of text properties and, when non-zero, a pointer to the
623 * first one in "props" (note that it is not aligned, therefore the char_u
624 * pointer).
625 */
626 int
627get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
628{
629 char_u *text;
630 size_t textlen;
631 size_t proplen;
632
Christian Brabandt0d0b3b12023-12-03 17:56:43 +0100633 // Be quick when no text property types have been defined for the buffer,
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100634 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200635 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100636 return 0;
637
638 // Fetch the line to get the ml_line_len field updated.
639 text = ml_get_buf(buf, lnum, will_change);
zeertzjq94b7c322024-03-12 21:50:32 +0100640 textlen = ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100641 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100642 if (proplen == 0)
643 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100644 if (proplen % sizeof(textprop_T) != 0)
645 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100646 iemsg(e_text_property_info_corrupted);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100647 return 0;
648 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100649 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100650 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100651}
652
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100653/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100654 * Return the number of text properties with "above" or "below" alignment in
655 * line "lnum". A "right" aligned property also goes below after a "below" or
656 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100657 */
658 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100659prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100660{
661 char_u *props;
662 int count = get_text_props(buf, lnum, &props, FALSE);
663 int result = 0;
664 textprop_T prop;
665 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100666 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100667
668 if (count == 0)
669 return 0;
670 for (i = 0; i < count; ++i)
671 {
672 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar89469d12022-12-02 20:46:26 +0000673 if (prop.tp_col == MAXCOL && text_prop_type_valid(buf, &prop))
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100674 {
Dylan Thacker-Smith515f7342024-03-28 12:01:14 +0100675 if ((prop.tp_flags & (TP_FLAG_ALIGN_ABOVE | TP_FLAG_ALIGN_BELOW))
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100676 || (next_right_goes_below
677 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
678 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100679 ++result;
680 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100681 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
682 next_right_goes_below = TRUE;
683 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100684 }
685 return result;
686}
687
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200688/**
689 * Return the number of text properties on line "lnum" in the current buffer.
690 * When "only_starting" is true only text properties starting in this line will
691 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100692 * When "last_line" is FALSE then text properties after the line are not
693 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200694 */
695 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100696count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200697{
698 char_u *props;
699 int proplen = get_text_props(curbuf, lnum, &props, 0);
700 int result = proplen;
701 int i;
702 textprop_T prop;
703
Bram Moolenaare175dc62022-08-01 22:18:50 +0100704 for (i = 0; i < proplen; ++i)
705 {
706 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100707 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100708 // previous line, or when not in the last line and it is virtual text
709 // after the line.
710 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
Christian Brabandt0d0b3b12023-12-03 17:56:43 +0100711 || (!last_line && prop.tp_col == MAXCOL))
Bram Moolenaare175dc62022-08-01 22:18:50 +0100712 --result;
713 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200714 return result;
715}
716
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100717static textprop_T *text_prop_compare_props;
718static buf_T *text_prop_compare_buf;
719
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000720/*
721 * Score for sorting on position of the text property: 0: above,
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100722 * 1: after (default), 2: right, 3: below (comes last)
723 */
724 static int
725text_prop_order(int flags)
726{
727 if (flags & TP_FLAG_ALIGN_ABOVE)
728 return 0;
729 if (flags & TP_FLAG_ALIGN_RIGHT)
730 return 2;
731 if (flags & TP_FLAG_ALIGN_BELOW)
732 return 3;
733 return 1;
734}
735
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100736/*
737 * Function passed to qsort() to sort text properties.
738 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
739 * both have the same priority.
740 */
741 static int
742text_prop_compare(const void *s1, const void *s2)
743{
744 int idx1, idx2;
745 textprop_T *tp1, *tp2;
746 proptype_T *pt1, *pt2;
747 colnr_T col1, col2;
748
749 idx1 = *(int *)s1;
750 idx2 = *(int *)s2;
751 tp1 = &text_prop_compare_props[idx1];
752 tp2 = &text_prop_compare_props[idx2];
753 col1 = tp1->tp_col;
754 col2 = tp2->tp_col;
Dylan Thacker-Smith80557212024-02-21 21:00:59 +0100755
756 // property that inserts text has priority over one that doesn't
757 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
758 return tp1->tp_id < 0 ? 1 : -1;
759
zeertzjqce53e3e2023-09-01 18:49:30 +0200760 if (col1 == MAXCOL || col2 == MAXCOL)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100761 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100762 int order1 = text_prop_order(tp1->tp_flags);
763 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100764
zeertzjqce53e3e2023-09-01 18:49:30 +0200765 // sort on order where it is added
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100766 if (order1 != order2)
767 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100768 }
769
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100770 // check highest priority, defined by the type
771 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
772 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
773 if (pt1 != pt2)
774 {
775 if (pt1 == NULL)
776 return -1;
777 if (pt2 == NULL)
778 return 1;
779 if (pt1->pt_priority != pt2->pt_priority)
780 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
781 }
782
783 // same priority, one that starts first wins
784 if (col1 != col2)
785 return col1 < col2 ? 1 : -1;
786
787 // for a property with text the id can be used as tie breaker
788 if (tp1->tp_id < 0)
789 return tp1->tp_id > tp2->tp_id ? 1 : -1;
790
791 return 0;
792}
793
794/*
795 * Sort "count" text properties using an array if indexes "idxs" into the list
796 * of text props "props" for buffer "buf".
797 */
798 void
799sort_text_props(
800 buf_T *buf,
801 textprop_T *props,
802 int *idxs,
803 int count)
804{
805 text_prop_compare_buf = buf;
806 text_prop_compare_props = props;
807 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
808}
809
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100810/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200811 * Find text property "type_id" in the visible lines of window "wp".
812 * Match "id" when it is > 0.
813 * Returns FAIL when not found.
814 */
815 int
Bram Moolenaar89469d12022-12-02 20:46:26 +0000816find_visible_prop(
817 win_T *wp,
818 int type_id,
819 int id,
820 textprop_T *prop,
821 linenr_T *found_lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200822{
Bram Moolenaar89469d12022-12-02 20:46:26 +0000823 // return when "type_id" no longer exists
824 if (text_prop_type_by_id(wp->w_buffer, type_id) == NULL)
825 return FAIL;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200826
827 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100828 validate_botline_win(wp);
Bram Moolenaar89469d12022-12-02 20:46:26 +0000829 for (linenr_T lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200830 {
Bram Moolenaar89469d12022-12-02 20:46:26 +0000831 char_u *props;
832 int count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
833 for (int i = 0; i < count; ++i)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200834 {
835 mch_memmove(prop, props + i * sizeof(textprop_T),
836 sizeof(textprop_T));
837 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
838 {
839 *found_lnum = lnum;
840 return OK;
841 }
842 }
843 }
844 return FAIL;
845}
846
847/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100848 * Set the text properties for line "lnum" to "props" with length "len".
849 * If "len" is zero text properties are removed, "props" is not used.
850 * Any existing text properties are dropped.
851 * Only works for the current buffer.
852 */
853 static void
854set_text_props(linenr_T lnum, char_u *props, int len)
855{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100856 char_u *text;
857 char_u *newtext;
858 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100859
860 text = ml_get(lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100861 textlen = ml_get_len(lnum) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100862 newtext = alloc(textlen + len);
863 if (newtext == NULL)
864 return;
865 mch_memmove(newtext, text, textlen);
866 if (len > 0)
867 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100868 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100869 vim_free(curbuf->b_ml.ml_line_ptr);
870 curbuf->b_ml.ml_line_ptr = newtext;
871 curbuf->b_ml.ml_line_len = textlen + len;
872 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
873}
874
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100875/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100876 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100877 */
878 void
879add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
880{
881 char_u *text;
882 char_u *newtext;
883 int proplen = text_prop_count * (int)sizeof(textprop_T);
884
885 text = ml_get(lnum);
886 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
887 if (newtext == NULL)
888 return;
889 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
890 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
891 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
892 vim_free(curbuf->b_ml.ml_line_ptr);
893 curbuf->b_ml.ml_line_ptr = newtext;
894 curbuf->b_ml.ml_line_len += proplen;
895 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
896}
897
Bram Moolenaare44336b2022-08-07 18:20:08 +0100898/*
899 * Function passed to qsort() for sorting proptype_T on pt_id.
900 */
901 static int
902compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100903{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100904 proptype_T *tp1 = *(proptype_T **)s1;
905 proptype_T *tp2 = *(proptype_T **)s2;
906
907 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
908}
909
910 static proptype_T *
911find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
912{
913 int low = 0;
914 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100915
Bram Moolenaar10246902022-08-08 17:08:05 +0100916 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100917 return NULL;
918
dundargocc57b5bc2022-11-02 13:30:51 +0000919 // Make the lookup faster by creating an array with pointers to
Bram Moolenaare44336b2022-08-07 18:20:08 +0100920 // hashtable entries, sorted on pt_id.
921 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100922 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100923 long todo;
924 hashitem_T *hi;
925 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100926
Bram Moolenaare44336b2022-08-07 18:20:08 +0100927 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
928 if (*array == NULL)
929 return NULL;
930 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000931 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100932 {
933 if (!HASHITEM_EMPTY(hi))
934 {
935 (*array)[i++] = HI2PT(hi);
936 --todo;
937 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100938 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100939 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
940 }
941
942 // binary search in the sorted array
943 high = ht->ht_used;
944 while (high > low)
945 {
946 int m = (high + low) / 2;
947
948 if ((*array)[m]->pt_id == id)
949 return (*array)[m];
950 if ((*array)[m]->pt_id > id)
951 high = m;
952 else
953 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100954 }
955 return NULL;
956}
957
958/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100959 * Fill 'dict' with text properties in 'prop'.
960 */
961 static void
962prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
963{
964 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200965 int buflocal = TRUE;
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200966 int virtualtext_prop = prop->tp_id < 0;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100967
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200968 dict_add_number(dict, "col", (prop->tp_col == MAXCOL) ? 0 : prop->tp_col);
969 if (!virtualtext_prop)
970 {
971 dict_add_number(dict, "length", prop->tp_len);
972 dict_add_number(dict, "id", prop->tp_id);
973 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100974 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
975 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200976
Bram Moolenaare44336b2022-08-07 18:20:08 +0100977 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200978 if (pt == NULL)
979 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100980 pt = find_type_by_id(global_proptypes, &global_proparray,
981 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200982 buflocal = FALSE;
983 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100984 if (pt != NULL)
985 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200986
987 if (buflocal)
988 dict_add_number(dict, "type_bufnr", buf->b_fnum);
989 else
990 dict_add_number(dict, "type_bufnr", 0);
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200991 if (virtualtext_prop)
Yegappan Lakshmananf9037f12023-08-20 18:27:45 +0200992 {
993 // virtual text property
994 garray_T *gap = &buf->b_textprop_text;
995 char_u *text;
996
997 // negate the property id to get the string index
998 text = ((char_u **)gap->ga_data)[-prop->tp_id - 1];
999 dict_add_string(dict, "text", text);
1000
1001 // text_align
1002 char_u *text_align = NULL;
1003 if (prop->tp_flags & TP_FLAG_ALIGN_RIGHT)
1004 text_align = (char_u *)"right";
1005 else if (prop->tp_flags & TP_FLAG_ALIGN_ABOVE)
1006 text_align = (char_u *)"above";
1007 else if (prop->tp_flags & TP_FLAG_ALIGN_BELOW)
1008 text_align = (char_u *)"below";
1009 if (text_align != NULL)
1010 dict_add_string(dict, "text_align", text_align);
1011
1012 // text_wrap
1013 if (prop->tp_flags & TP_FLAG_WRAP)
1014 dict_add_string(dict, "text_wrap", (char_u *)"wrap");
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +02001015 if (prop->tp_padleft != 0)
1016 dict_add_number(dict, "text_padding_left", prop->tp_padleft);
Yegappan Lakshmananf9037f12023-08-20 18:27:45 +02001017 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001018}
1019
1020/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001021 * Find a property type by ID in "buf" or globally.
1022 * Returns NULL if not found.
1023 */
1024 proptype_T *
1025text_prop_type_by_id(buf_T *buf, int id)
1026{
1027 proptype_T *type;
1028
Bram Moolenaare44336b2022-08-07 18:20:08 +01001029 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001030 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001031 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001032 return type;
1033}
1034
1035/*
Bram Moolenaar89469d12022-12-02 20:46:26 +00001036 * Return TRUE if "prop" is a valid text property type.
1037 */
1038 int
1039text_prop_type_valid(buf_T *buf, textprop_T *prop)
1040{
1041 return text_prop_type_by_id(buf, prop->tp_type) != NULL;
1042}
1043
1044/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001045 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
1046 */
1047 void
1048f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
1049{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001050 linenr_T start;
1051 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001052 linenr_T lnum;
1053 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001054 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001055
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001056 if (in_vim9script()
1057 && (check_for_number_arg(argvars, 0) == FAIL
1058 || check_for_opt_number_arg(argvars, 1) == FAIL
1059 || (argvars[1].v_type != VAR_UNKNOWN
1060 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1061 return;
1062
1063 start = tv_get_number(&argvars[0]);
1064 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001065 if (argvars[1].v_type != VAR_UNKNOWN)
1066 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001067 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001068 if (argvars[2].v_type != VAR_UNKNOWN)
1069 {
1070 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
1071 return;
1072 }
1073 }
1074 if (start < 1 || end < 1)
1075 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001076 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001077 return;
1078 }
1079
1080 for (lnum = start; lnum <= end; ++lnum)
1081 {
1082 char_u *text;
1083 size_t len;
1084
1085 if (lnum > buf->b_ml.ml_line_count)
1086 break;
1087 text = ml_get_buf(buf, lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001088 len = ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001089 if ((size_t)buf->b_ml.ml_line_len > len)
1090 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001091 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001092 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1093 {
1094 char_u *newtext = vim_strsave(text);
1095
1096 // need to allocate the line now
1097 if (newtext == NULL)
1098 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001099 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1100 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001101 buf->b_ml.ml_line_ptr = newtext;
1102 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1103 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001104 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001105 }
1106 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001107 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001108 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001109}
1110
1111/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001112 * prop_find({props} [, {direction}])
1113 */
1114 void
1115f_prop_find(typval_T *argvars, typval_T *rettv)
1116{
1117 pos_T *cursor = &curwin->w_cursor;
1118 dict_T *dict;
1119 buf_T *buf = curbuf;
1120 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001121 int lnum_start;
1122 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001123 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001124 int id = 0;
1125 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001126 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001127 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001128 int lnum = -1;
1129 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001130 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001131 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001132
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001133 if (in_vim9script()
1134 && (check_for_dict_arg(argvars, 0) == FAIL
1135 || check_for_opt_string_arg(argvars, 1) == FAIL))
1136 return;
1137
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001138 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001139 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001140 dict = argvars[0].vval.v_dict;
1141
1142 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1143 return;
1144 if (buf->b_ml.ml_mfp == NULL)
1145 return;
1146
1147 if (argvars[1].v_type != VAR_UNKNOWN)
1148 {
1149 char_u *dir_s = tv_get_string(&argvars[1]);
1150
1151 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001152 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001153 else if (*dir_s != 'f')
1154 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001155 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001156 return;
1157 }
1158 }
1159
1160 di = dict_find(dict, (char_u *)"lnum", -1);
1161 if (di != NULL)
1162 lnum = tv_get_number(&di->di_tv);
1163
1164 di = dict_find(dict, (char_u *)"col", -1);
1165 if (di != NULL)
1166 col = tv_get_number(&di->di_tv);
1167
1168 if (lnum == -1)
1169 {
1170 lnum = cursor->lnum;
1171 col = cursor->col + 1;
1172 }
1173 else if (col == -1)
1174 col = 1;
1175
1176 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1177 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001178 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001179 return;
1180 }
1181
Bram Moolenaard61efa52022-07-23 09:52:04 +01001182 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001183
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001184 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001185 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001186 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001187 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001188 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001189 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001190 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001191 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001192 proptype_T *type = lookup_prop_type(name, buf);
1193
1194 if (type == NULL)
1195 return;
1196 type_id = type->pt_id;
1197 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001198 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001199 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001200 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001201 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001202 return;
1203 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001204 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001205 {
Ben Jacksona7704222022-08-20 20:54:51 +01001206 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001207 return;
1208 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001209
1210 lnum_start = lnum;
1211
1212 if (rettv_dict_alloc(rettv) == FAIL)
1213 return;
1214
1215 while (1)
1216 {
1217 char_u *text = ml_get_buf(buf, lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001218 size_t textlen = ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001219 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001220 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001221 int i;
1222 textprop_T prop;
1223 int prop_start;
1224 int prop_end;
1225
LemonBoy9bd3ce22022-04-18 21:54:02 +01001226 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001227 {
1228 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001229 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001230
LemonBoy9bd3ce22022-04-18 21:54:02 +01001231 // For the very first line try to find the first property before or
1232 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001233 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001234 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001235 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001236 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001237 if (prop.tp_col > col)
1238 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001239 }
1240 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1241 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001242 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001243 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001244 : (id_found && prop.tp_id == id)
1245 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001246 {
1247 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001248 if (lnum_start == lnum
1249 && col >= prop.tp_col
1250 && (col <= prop.tp_col + prop.tp_len
1251 - (prop.tp_len != 0)))
1252 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001253
LemonBoy9bd3ce22022-04-18 21:54:02 +01001254 // The property was not continued from last line, it starts on
1255 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001256 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001257 // The property does not continue on the next line, it ends on
1258 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001259 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001260 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001261 seen_end = 1;
1262
1263 // Skip lines without the start flag.
1264 if (!prop_start)
1265 {
1266 // Always search backwards for start when search started
1267 // on a prop and we're not skipping.
1268 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001269 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001270 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001271 }
1272
1273 // If skipstart is true, skip the prop at start pos (even if
1274 // continued from another line).
1275 if (start_pos_has_prop && skipstart && !seen_end)
1276 {
1277 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001278 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001279 }
1280
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001281 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1282 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1283
1284 return;
1285 }
1286 }
1287
1288 if (dir > 0)
1289 {
1290 if (lnum >= buf->b_ml.ml_line_count)
1291 break;
1292 lnum++;
1293 }
1294 else
1295 {
1296 if (lnum <= 1)
1297 break;
1298 lnum--;
1299 }
1300 }
1301}
1302
1303/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001304 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1305 */
1306 static int
1307prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1308{
1309 int i;
1310
1311 for (i = 0; i < len; i++)
1312 if (types_or_ids[i] == type_or_id)
1313 return TRUE;
1314
1315 return FALSE;
1316}
1317
1318/*
1319 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1320 * If 'prop_types' is not NULL, then return only the text properties with
1321 * matching property type in the 'prop_types' array.
1322 * If 'prop_ids' is not NULL, then return only the text properties with
1323 * an identifier in the 'props_ids' array.
1324 * If 'add_lnum' is TRUE, then add the line number also to the text property
1325 * dictionary.
1326 */
1327 static void
1328get_props_in_line(
1329 buf_T *buf,
1330 linenr_T lnum,
1331 int *prop_types,
1332 int prop_types_len,
1333 int *prop_ids,
1334 int prop_ids_len,
1335 list_T *retlist,
1336 int add_lnum)
1337{
1338 char_u *text = ml_get_buf(buf, lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001339 size_t textlen = ml_get_buf_len(buf, lnum) + 1;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001340 int count;
1341 int i;
1342 textprop_T prop;
1343
1344 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1345 for (i = 0; i < count; ++i)
1346 {
1347 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1348 sizeof(textprop_T));
1349 if ((prop_types == NULL
1350 || prop_type_or_id_in_list(prop_types, prop_types_len,
1351 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001352 && (prop_ids == NULL
1353 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1354 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001355 {
1356 dict_T *d = dict_alloc();
1357
1358 if (d == NULL)
1359 break;
1360 prop_fill_dict(d, &prop, buf);
1361 if (add_lnum)
1362 dict_add_number(d, "lnum", lnum);
1363 list_append_dict(retlist, d);
1364 }
1365 }
1366}
1367
1368/*
1369 * Convert a List of property type names into an array of property type
1370 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1371 * error. 'num_types' is set to the number of returned property types.
1372 */
1373 static int *
1374get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1375{
1376 int *prop_types;
1377 listitem_T *li;
1378 int i;
1379 char_u *name;
1380 proptype_T *type;
1381
1382 *num_types = 0;
1383
1384 prop_types = ALLOC_MULT(int, list_len(l));
1385 if (prop_types == NULL)
1386 return NULL;
1387
1388 i = 0;
1389 FOR_ALL_LIST_ITEMS(l, li)
1390 {
1391 if (li->li_tv.v_type != VAR_STRING)
1392 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001393 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001394 goto errret;
1395 }
1396 name = li->li_tv.vval.v_string;
1397 if (name == NULL)
1398 goto errret;
1399
1400 type = lookup_prop_type(name, buf);
1401 if (type == NULL)
1402 goto errret;
1403 prop_types[i++] = type->pt_id;
1404 }
1405
1406 *num_types = i;
1407 return prop_types;
1408
1409errret:
1410 VIM_CLEAR(prop_types);
1411 return NULL;
1412}
1413
1414/*
1415 * Convert a List of property identifiers into an array of property
1416 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1417 * error. 'num_ids' is set to the number of returned property identifiers.
1418 */
1419 static int *
1420get_prop_ids_from_list(list_T *l, int *num_ids)
1421{
1422 int *prop_ids;
1423 listitem_T *li;
1424 int i;
1425 int id;
1426 int error;
1427
1428 *num_ids = 0;
1429
1430 prop_ids = ALLOC_MULT(int, list_len(l));
1431 if (prop_ids == NULL)
1432 return NULL;
1433
1434 i = 0;
1435 FOR_ALL_LIST_ITEMS(l, li)
1436 {
1437 error = FALSE;
1438 id = tv_get_number_chk(&li->li_tv, &error);
1439 if (error)
1440 goto errret;
1441
1442 prop_ids[i++] = id;
1443 }
1444
1445 *num_ids = i;
1446 return prop_ids;
1447
1448errret:
1449 VIM_CLEAR(prop_ids);
1450 return NULL;
1451}
1452
1453/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001454 * prop_list({lnum} [, {bufnr}])
1455 */
1456 void
1457f_prop_list(typval_T *argvars, typval_T *rettv)
1458{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001459 linenr_T lnum;
1460 linenr_T start_lnum;
1461 linenr_T end_lnum;
1462 buf_T *buf = curbuf;
1463 int add_lnum = FALSE;
1464 int *prop_types = NULL;
1465 int prop_types_len = 0;
1466 int *prop_ids = NULL;
1467 int prop_ids_len = 0;
1468 list_T *l;
1469 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001470
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001471 if (in_vim9script()
1472 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001473 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001474 return;
1475
Bram Moolenaar93a10962022-06-16 11:42:09 +01001476 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001477 return;
1478
1479 // default: get text properties on current line
1480 start_lnum = tv_get_number(&argvars[0]);
1481 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001482 if (argvars[1].v_type != VAR_UNKNOWN)
1483 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001484 dict_T *d;
1485
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001486 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001487 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001488 d = argvars[1].vval.v_dict;
1489
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001490 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1491 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001492
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001493 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001494 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001495 if (di->di_tv.v_type != VAR_NUMBER)
1496 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001497 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001498 return;
1499 }
1500 end_lnum = tv_get_number(&di->di_tv);
1501 if (end_lnum < 0)
1502 // negative end_lnum is used as an offset from the last buffer
1503 // line
1504 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1505 else if (end_lnum > buf->b_ml.ml_line_count)
1506 end_lnum = buf->b_ml.ml_line_count;
1507 add_lnum = TRUE;
1508 }
1509 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1510 {
1511 if (di->di_tv.v_type != VAR_LIST)
1512 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001513 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001514 return;
1515 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001516
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001517 l = di->di_tv.vval.v_list;
1518 if (l != NULL && list_len(l) > 0)
1519 {
1520 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1521 if (prop_types == NULL)
1522 return;
1523 }
1524 }
1525 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1526 {
1527 if (di->di_tv.v_type != VAR_LIST)
1528 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001529 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001530 goto errret;
1531 }
1532
1533 l = di->di_tv.vval.v_list;
1534 if (l != NULL && list_len(l) > 0)
1535 {
1536 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1537 if (prop_ids == NULL)
1538 goto errret;
1539 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001540 }
1541 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001542 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1543 || end_lnum < 1 || end_lnum < start_lnum)
1544 emsg(_(e_invalid_range));
1545 else
1546 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1547 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1548 prop_ids, prop_ids_len,
1549 rettv->vval.v_list, add_lnum);
1550
1551errret:
1552 VIM_CLEAR(prop_types);
1553 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001554}
1555
1556/*
1557 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1558 */
1559 void
1560f_prop_remove(typval_T *argvars, typval_T *rettv)
1561{
1562 linenr_T start = 1;
1563 linenr_T end = 0;
1564 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001565 linenr_T first_changed = 0;
1566 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001567 dict_T *dict;
1568 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001569 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001570 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001571 int type_id = -1; // for a single "type"
1572 int *type_ids = NULL; // array, for a list of "types", allocated
1573 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001574 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001575 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001576
1577 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001578
1579 if (in_vim9script()
1580 && (check_for_dict_arg(argvars, 0) == FAIL
1581 || check_for_opt_number_arg(argvars, 1) == FAIL
1582 || (argvars[1].v_type != VAR_UNKNOWN
1583 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1584 return;
1585
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001586 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001587 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001588
1589 if (argvars[1].v_type != VAR_UNKNOWN)
1590 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001591 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001592 end = start;
1593 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001594 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001595 if (start < 1 || end < 1)
1596 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001597 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001598 return;
1599 }
1600 }
1601
1602 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001603 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1604 return;
1605 if (buf->b_ml.ml_mfp == NULL)
1606 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001607
Bram Moolenaard61efa52022-07-23 09:52:04 +01001608 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001609
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001610 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001611 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001612
1613 // if a specific type was supplied "type": check that (and ignore "types".
1614 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001615 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001616 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001617 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001618 proptype_T *type = lookup_prop_type(name, buf);
1619
1620 if (type == NULL)
1621 return;
1622 type_id = type->pt_id;
1623 }
Ben Jacksona7704222022-08-20 20:54:51 +01001624 if (dict_has_key(dict, "types"))
1625 {
1626 typval_T types;
1627 listitem_T *li = NULL;
1628
1629 dict_get_tv(dict, "types", &types);
1630 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1631 {
1632 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1633
1634 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1635 {
1636 proptype_T *prop_type;
1637
1638 if (li->li_tv.v_type != VAR_STRING)
1639 continue;
1640
1641 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1642
1643 if (!prop_type)
1644 goto cleanup_prop_remove;
1645
1646 type_ids[num_type_ids++] = prop_type->pt_id;
1647 }
1648 }
1649 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001650 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001651
Ben Jacksona7704222022-08-20 20:54:51 +01001652 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001653 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001654 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001655 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001656 }
Ben Jacksona7704222022-08-20 20:54:51 +01001657 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001658 {
Ben Jacksona7704222022-08-20 20:54:51 +01001659 emsg(_(e_need_id_and_type_or_types_with_both));
1660 goto cleanup_prop_remove;
1661 }
1662 if (type_id != -1 && num_type_ids > 0)
1663 {
1664 emsg(_(e_cannot_specify_both_type_and_types));
1665 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001666 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001667
1668 if (end == 0)
1669 end = buf->b_ml.ml_line_count;
1670 for (lnum = start; lnum <= end; ++lnum)
1671 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001672 size_t len;
1673
1674 if (lnum > buf->b_ml.ml_line_count)
1675 break;
zeertzjq94b7c322024-03-12 21:50:32 +01001676 len = ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001677 if ((size_t)buf->b_ml.ml_line_len > len)
1678 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001679 static textprop_T textprop; // static because of alignment
1680 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001681
1682 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1683 / sizeof(textprop_T); ++idx)
1684 {
1685 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1686 + idx * sizeof(textprop_T);
1687 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001688 int matches_id = 0;
1689 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001690
1691 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001692
1693 matches_id = textprop.tp_id == id;
1694 if (num_type_ids > 0)
1695 {
1696 int idx2;
1697
1698 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1699 matches_type = textprop.tp_type == type_ids[idx2];
1700 }
1701 else
1702 {
1703 matches_type = textprop.tp_type == type_id;
1704 }
1705
1706 if (both ? matches_id && matches_type
1707 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001708 {
1709 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1710 {
1711 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1712
1713 // need to allocate the line to be able to change it
1714 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001715 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001716 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1717 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001718 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1719 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001720 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001721 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1722
1723 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001724 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001725 }
1726
1727 taillen = buf->b_ml.ml_line_len - len
1728 - (idx + 1) * sizeof(textprop_T);
1729 if (taillen > 0)
1730 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1731 taillen);
1732 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1733 --idx;
1734
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001735 if (textprop.tp_id < 0)
1736 {
1737 garray_T *gap = &buf->b_textprop_text;
1738 int ii = -textprop.tp_id - 1;
1739
1740 // negative ID: property with text - free the text
1741 if (ii < gap->ga_len)
1742 {
1743 char_u **p = ((char_u **)gap->ga_data) + ii;
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00001744 VIM_CLEAR(*p);
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001745 did_remove_text = TRUE;
1746 }
1747 }
1748
Bram Moolenaar965c0442021-05-17 00:22:06 +02001749 if (first_changed == 0)
1750 first_changed = lnum;
1751 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001752 ++rettv->vval.v_number;
1753 if (!do_all)
1754 break;
1755 }
1756 }
1757 }
1758 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001759
Bram Moolenaar965c0442021-05-17 00:22:06 +02001760 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001761 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001762 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001763 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001764 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001765 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001766
1767 if (did_remove_text)
1768 {
1769 garray_T *gap = &buf->b_textprop_text;
1770
1771 // Reduce the growarray size for NULL pointers at the end.
1772 while (gap->ga_len > 0
1773 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1774 --gap->ga_len;
1775 }
Ben Jacksona7704222022-08-20 20:54:51 +01001776
1777cleanup_prop_remove:
1778 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001779}
1780
1781/*
1782 * Common for f_prop_type_add() and f_prop_type_change().
1783 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001784 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001785prop_type_set(typval_T *argvars, int add)
1786{
1787 char_u *name;
1788 buf_T *buf = NULL;
1789 dict_T *dict;
1790 dictitem_T *di;
1791 proptype_T *prop;
1792
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001793 if (in_vim9script()
1794 && (check_for_string_arg(argvars, 0) == FAIL
1795 || check_for_dict_arg(argvars, 1) == FAIL))
1796 return;
1797
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001798 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001799 if (*name == NUL)
1800 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001801 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001802 return;
1803 }
1804
1805 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1806 return;
1807 dict = argvars[1].vval.v_dict;
1808
Bram Moolenaare44336b2022-08-07 18:20:08 +01001809 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001810 if (add)
1811 {
1812 hashtab_T **htp;
1813
1814 if (prop != NULL)
1815 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001816 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001817 return;
1818 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001819 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001820 if (prop == NULL)
1821 return;
1822 STRCPY(prop->pt_name, name);
1823 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001824 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001825 if (buf == NULL)
1826 {
1827 htp = &global_proptypes;
1828 VIM_CLEAR(global_proparray);
1829 }
1830 else
1831 {
1832 htp = &buf->b_proptypes;
1833 VIM_CLEAR(buf->b_proparray);
1834 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001835 if (*htp == NULL)
1836 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001837 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001838 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001839 {
1840 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001841 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001842 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001843 hash_init(*htp);
1844 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001845 hash_add(*htp, PT2HIKEY(prop), "prop type");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001846 }
1847 else
1848 {
1849 if (prop == NULL)
1850 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001851 semsg(_(e_property_type_str_does_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001852 return;
1853 }
1854 }
1855
1856 if (dict != NULL)
1857 {
1858 di = dict_find(dict, (char_u *)"highlight", -1);
1859 if (di != NULL)
1860 {
1861 char_u *highlight;
1862 int hl_id = 0;
1863
Bram Moolenaard61efa52022-07-23 09:52:04 +01001864 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001865 if (highlight != NULL && *highlight != NUL)
1866 hl_id = syn_name2id(highlight);
1867 if (hl_id <= 0)
1868 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001869 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001870 highlight == NULL ? (char_u *)"" : highlight);
1871 return;
1872 }
1873 prop->pt_hl_id = hl_id;
1874 }
1875
Bram Moolenaar58187f12019-05-05 16:33:47 +02001876 di = dict_find(dict, (char_u *)"combine", -1);
1877 if (di != NULL)
1878 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001879 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001880 prop->pt_flags |= PT_FLAG_COMBINE;
1881 else
1882 prop->pt_flags &= ~PT_FLAG_COMBINE;
1883 }
1884
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001885 di = dict_find(dict, (char_u *)"override", -1);
1886 if (di != NULL)
1887 {
1888 if (tv_get_bool(&di->di_tv))
1889 prop->pt_flags |= PT_FLAG_OVERRIDE;
1890 else
1891 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1892 }
1893
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001894 di = dict_find(dict, (char_u *)"priority", -1);
1895 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001896 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001897
1898 di = dict_find(dict, (char_u *)"start_incl", -1);
1899 if (di != NULL)
1900 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001901 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001902 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1903 else
1904 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1905 }
1906
1907 di = dict_find(dict, (char_u *)"end_incl", -1);
1908 if (di != NULL)
1909 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001910 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001911 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1912 else
1913 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1914 }
1915 }
1916}
1917
1918/*
1919 * prop_type_add({name}, {props})
1920 */
1921 void
1922f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1923{
1924 prop_type_set(argvars, TRUE);
1925}
1926
1927/*
1928 * prop_type_change({name}, {props})
1929 */
1930 void
1931f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1932{
1933 prop_type_set(argvars, FALSE);
1934}
1935
1936/*
1937 * prop_type_delete({name} [, {bufnr}])
1938 */
1939 void
1940f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1941{
1942 char_u *name;
1943 buf_T *buf = NULL;
1944 hashitem_T *hi;
1945
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001946 if (in_vim9script()
1947 && (check_for_string_arg(argvars, 0) == FAIL
1948 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1949 return;
1950
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001951 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001952 if (*name == NUL)
1953 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001954 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001955 return;
1956 }
1957
1958 if (argvars[1].v_type != VAR_UNKNOWN)
1959 {
1960 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1961 return;
1962 }
1963
Bram Moolenaare44336b2022-08-07 18:20:08 +01001964 hi = find_prop_type_hi(name, buf);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001965 if (hi == NULL)
1966 return;
1967
1968 hashtab_T *ht;
1969 proptype_T *prop = HI2PT(hi);
1970
1971 if (buf == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001972 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001973 ht = global_proptypes;
1974 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001975 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001976 else
1977 {
1978 ht = buf->b_proptypes;
1979 VIM_CLEAR(buf->b_proparray);
1980 }
1981 hash_remove(ht, hi, "prop type delete");
1982 vim_free(prop);
1983
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001984 // currently visible text properties will disappear
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001985 redraw_all_later(UPD_CLEAR);
1986 changed_window_setting_buf(buf == NULL ? curbuf : buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001987}
1988
1989/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001990 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001991 */
1992 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001993f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001994{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001995 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001996
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001997 if (in_vim9script()
1998 && (check_for_string_arg(argvars, 0) == FAIL
1999 || check_for_opt_dict_arg(argvars, 1) == FAIL))
2000 return;
2001
2002 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002003 if (*name == NUL)
2004 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00002005 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002006 return;
2007 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002008
2009 if (rettv_dict_alloc(rettv) == FAIL)
2010 return;
2011
2012 proptype_T *prop = NULL;
2013 buf_T *buf = NULL;
2014
2015 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002016 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002017 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
2018 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002019 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002020
2021 prop = find_prop_type(name, buf);
2022 if (prop == NULL)
2023 return;
2024
2025 dict_T *d = rettv->vval.v_dict;
2026
2027 if (prop->pt_hl_id > 0)
2028 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
2029 dict_add_number(d, "priority", prop->pt_priority);
2030 dict_add_number(d, "combine",
2031 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
2032 dict_add_number(d, "start_incl",
2033 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
2034 dict_add_number(d, "end_incl",
2035 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
2036 if (buf != NULL)
2037 dict_add_number(d, "bufnr", buf->b_fnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002038}
2039
2040 static void
2041list_types(hashtab_T *ht, list_T *l)
2042{
2043 long todo;
2044 hashitem_T *hi;
2045
2046 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002047 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002048 {
2049 if (!HASHITEM_EMPTY(hi))
2050 {
2051 proptype_T *prop = HI2PT(hi);
2052
2053 list_append_string(l, prop->pt_name, -1);
2054 --todo;
2055 }
2056 }
2057}
2058
2059/*
2060 * prop_type_list([{bufnr}])
2061 */
2062 void
Dominique Pellé0268ff32024-07-28 21:12:20 +02002063f_prop_type_list(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002064{
2065 buf_T *buf = NULL;
2066
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002067 if (rettv_list_alloc(rettv) == FAIL)
2068 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002069
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002070 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
2071 return;
2072
2073 if (argvars[0].v_type != VAR_UNKNOWN)
2074 {
2075 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2076 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002077 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002078 if (buf == NULL)
2079 {
2080 if (global_proptypes != NULL)
2081 list_types(global_proptypes, rettv->vval.v_list);
2082 }
2083 else if (buf->b_proptypes != NULL)
2084 list_types(buf->b_proptypes, rettv->vval.v_list);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002085}
2086
2087/*
2088 * Free all property types in "ht".
2089 */
2090 static void
2091clear_ht_prop_types(hashtab_T *ht)
2092{
2093 long todo;
2094 hashitem_T *hi;
2095
2096 if (ht == NULL)
2097 return;
2098
2099 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002100 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002101 {
2102 if (!HASHITEM_EMPTY(hi))
2103 {
2104 proptype_T *prop = HI2PT(hi);
2105
2106 vim_free(prop);
2107 --todo;
2108 }
2109 }
2110
2111 hash_clear(ht);
2112 vim_free(ht);
2113}
2114
2115#if defined(EXITFREE) || defined(PROTO)
2116/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002117 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002118 */
2119 void
2120clear_global_prop_types(void)
2121{
2122 clear_ht_prop_types(global_proptypes);
2123 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002124 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002125}
2126#endif
2127
2128/*
2129 * Free all property types for "buf".
2130 */
2131 void
2132clear_buf_prop_types(buf_T *buf)
2133{
2134 clear_ht_prop_types(buf->b_proptypes);
2135 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002136 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002137}
2138
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002139// Struct used to return two values from adjust_prop().
2140typedef struct
2141{
2142 int dirty; // if the property was changed
2143 int can_drop; // whether after this change, the prop may be removed
2144} adjustres_T;
2145
2146/*
2147 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2148 *
2149 * Note that "col" is zero-based, while tp_col is one-based.
2150 * Only for the current buffer.
2151 * "flags" can have:
2152 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002153 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002154 */
2155 static adjustres_T
2156adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002157 textprop_T *prop,
2158 colnr_T col,
2159 int added,
2160 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002161{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002162 proptype_T *pt;
2163 int start_incl;
2164 int end_incl;
2165 int droppable;
2166 adjustres_T res = {TRUE, FALSE};
2167
2168 // prop after end of the line doesn't move
2169 if (prop->tp_col == MAXCOL)
2170 {
2171 res.dirty = FALSE;
2172 return res;
2173 }
2174
2175 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2176 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002177 || (flags & APC_SUBSTITUTE)
2178 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002179 if (prop->tp_id < 0 && (flags & APC_INDENT))
2180 // when inserting indent just before a character with virtual text
2181 // shift the text property
2182 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002183 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002184 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002185 // do not drop zero-width props if they later can increase in size
2186 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002187
2188 if (added > 0)
2189 {
2190 if (col + 1 <= prop->tp_col
2191 - (start_incl || (prop->tp_len == 0 && end_incl)))
2192 // Change is entirely before the text property: Only shift
2193 prop->tp_col += added;
2194 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2195 // Insertion was inside text property
2196 prop->tp_len += added;
2197 }
2198 else if (prop->tp_col > col + 1)
2199 {
2200 if (prop->tp_col + added < col + 1)
2201 {
2202 prop->tp_len += (prop->tp_col - 1 - col) + added;
2203 prop->tp_col = col + 1;
2204 if (prop->tp_len <= 0)
2205 {
2206 prop->tp_len = 0;
2207 res.can_drop = droppable;
2208 }
2209 }
2210 else
2211 prop->tp_col += added;
2212 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002213 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2214 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002215 {
2216 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2217
2218 prop->tp_len += after > 0 ? added + after : added;
2219 res.can_drop = prop->tp_len <= 0 && droppable;
2220 }
2221 else
2222 res.dirty = FALSE;
2223
2224 return res;
2225}
2226
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002227/*
2228 * Adjust the columns of text properties in line "lnum" after position "col" to
2229 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002230 * Note that "col" is zero-based, while tp_col is one-based.
2231 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002232 * "flags" can have:
2233 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2234 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002235 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002236 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002237 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002238 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002239 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002240adjust_prop_columns(
2241 linenr_T lnum,
2242 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002243 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002244 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002245{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002246 int proplen;
2247 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002248 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002249 int ri, wi;
2250 size_t textlen;
2251
2252 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002253 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002254
2255 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2256 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002257 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002258 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002259
Bram Moolenaar196d1572019-01-02 23:47:18 +01002260 wi = 0; // write index
2261 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002262 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002263 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002264 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002265
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002266 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2267 res = adjust_prop(&prop, col, bytes_added, flags);
2268 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002269 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002270 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002271 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2272 && u_savesub(lnum) == FAIL)
2273 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002274 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002275
2276 // u_savesub() may have updated curbuf->b_ml, fetch it again
2277 if (curbuf->b_ml.ml_line_lnum != lnum)
2278 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002279 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002280 if (res.can_drop)
2281 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002282 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002283 ++wi;
2284 }
2285 if (dirty)
2286 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002287 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2288
2289 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002290 {
2291 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2292
2293 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2294 vim_free(curbuf->b_ml.ml_line_ptr);
2295 curbuf->b_ml.ml_line_ptr = p;
2296 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002297 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002298 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002299 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002300 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002301}
2302
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002303/*
2304 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002305 * "lnum_props" is the line that has the properties from before the split.
2306 * "lnum_top" is the top line.
2307 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002308 * "deleted" is the number of bytes deleted.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002309 * "at_eol" is true if the split is after the end of the line.
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002310 */
2311 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002312adjust_props_for_split(
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002313 linenr_T lnum_props,
2314 linenr_T lnum_top,
2315 int kept,
2316 int deleted,
2317 int at_eol)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002318{
2319 char_u *props;
2320 int count;
2321 garray_T prevprop;
2322 garray_T nextprop;
2323 int i;
2324 int skipped = kept + deleted;
2325
2326 if (!curbuf->b_has_textprop)
2327 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002328
2329 // Get the text properties from "lnum_props".
2330 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002331 ga_init2(&prevprop, sizeof(textprop_T), 10);
2332 ga_init2(&nextprop, sizeof(textprop_T), 10);
2333
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002334 // Keep the relevant ones in the first line, reducing the length if needed.
2335 // Copy the ones that include the split to the second line.
2336 // Move the ones after the split to the second line.
2337 for (i = 0; i < count; ++i)
2338 {
2339 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002340 proptype_T *pt;
2341 int start_incl, end_incl;
2342 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002343 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002344
2345 // copy the prop to an aligned structure
2346 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2347
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002348 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2349 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2350 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002351
2352 // a text prop "above" behaves like it is on the first text column
2353 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2354
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002355 if (prop_col == MAXCOL)
2356 {
2357 cont_prev = at_eol;
2358 cont_next = !at_eol;
2359 }
2360 else
2361 {
2362 cont_prev = prop_col + !start_incl <= kept;
2363 cont_next = skipped <= prop_col + prop.tp_len - !end_incl;
2364 }
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002365 // when a prop has text it is never copied
2366 if (prop.tp_id < 0 && cont_next)
2367 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002368
2369 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002370 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002371 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2372
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002373 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002374 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002375 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002376 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002377 if (cont_next)
2378 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002379 }
2380
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002381 // Only add the property to the next line if the length is bigger than
2382 // zero.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002383 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002384 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002385 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002386
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002387 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002388 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002389 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002390 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002391 if (p->tp_col > skipped)
2392 p->tp_col -= skipped - 1;
2393 else
2394 {
2395 p->tp_len -= skipped - p->tp_col;
2396 p->tp_col = 1;
2397 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002398 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002399 if (cont_prev)
2400 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002401 }
2402 }
2403
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002404 set_text_props(lnum_top, prevprop.ga_data,
2405 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002406 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002407 set_text_props(lnum_top + 1, nextprop.ga_data,
2408 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002409 ga_clear(&nextprop);
2410}
2411
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002412/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002413 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002414 */
2415 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002416prepend_joined_props(
2417 char_u *new_props,
2418 int propcount,
2419 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002420 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002421 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002422 long col,
2423 int removed)
2424{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002425 char_u *props;
2426 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2427 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002428
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002429 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002430 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002431 textprop_T prop;
2432 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002433
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002434 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002435 if (prop.tp_col == MAXCOL && !last_line)
2436 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002437 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2438
2439 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002440 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002441
Bram Moolenaare175dc62022-08-01 22:18:50 +01002442 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002443 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2444 &prop, sizeof(prop));
2445 else
2446 {
2447 int j;
2448 int found = FALSE;
2449
2450 // Search for continuing prop.
2451 for (j = *props_remaining; j < propcount; ++j)
2452 {
2453 textprop_T op;
2454
2455 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2456 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2457 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002458 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002459 found = TRUE;
2460 op.tp_len += op.tp_col - prop.tp_col;
2461 op.tp_col = prop.tp_col;
2462 // Start/end is taken care of when deleting joined lines
2463 op.tp_flags = prop.tp_flags;
2464 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2465 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002466 }
2467 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002468 if (!found)
2469 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002470 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002471 }
2472}
2473
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002474#endif // FEAT_PROP_POPUP