blob: a06605d3ddebc6d15b13297a1ba99276698f82fa [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"))
Christian Brabandt701c8632024-09-08 20:05:23 +0200375 {
376 vimlong_T x;
377 x = dict_get_number(dict, "id");
378 if (x > INT_MAX || x <= INT_MIN)
379 {
380 semsg(_(e_val_too_large), dict_get_string(dict, "id", FALSE));
381 return;
382 }
383 id = (int)x;
384 }
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200385
386 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
387 return;
388
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100389 // This must be done _before_ we start adding properties because property
390 // changes trigger buffer (memline) reorganisation, which needs this flag
391 // to be correctly set.
392 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200393 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
394 {
395 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
396 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000397 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200398 return;
399 }
400
401 pos_list = li->li_tv.vval.v_list;
402 start_lnum = list_find_nr(pos_list, 0L, &error);
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100403 if (!error)
404 start_col = list_find_nr(pos_list, 1L, &error);
405 if (!error)
406 end_lnum = list_find_nr(pos_list, 2L, &error);
407 if (!error)
408 end_col = list_find_nr(pos_list, 3L, &error);
Bram Moolenaard93009e2022-10-13 14:35:24 +0100409 int this_id = id;
410 if (!error && pos_list->lv_len > 4)
411 this_id = list_find_nr(pos_list, 4L, &error);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200412 if (error || start_lnum <= 0 || start_col <= 0
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100413 || end_lnum <= 0 || end_col <= 0)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200414 {
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100415 if (prev_did_emsg == did_emsg)
416 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200417 return;
418 }
Bram Moolenaard93009e2022-10-13 14:35:24 +0100419 if (prop_add_one(buf, type_name, this_id, NULL, 0, 0,
420 start_lnum, end_lnum, start_col, end_col) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200421 return;
422 }
423
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100424 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200425}
426
427/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100428 * Get the next ID to use for a textprop with text in buffer "buf".
429 */
430 static int
431get_textprop_id(buf_T *buf)
432{
433 // TODO: recycle deleted entries
434 return -(buf->b_textprop_text.ga_len + 1);
435}
436
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000437// Flag that is set when a negative ID isused for a normal text property.
438// It is then impossible to use virtual text properties.
439static int did_use_negative_pop_id = FALSE;
440
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100441/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200442 * Shared between prop_add() and popup_create().
443 * "dict_arg" is the function argument of a dict containing "bufnr".
444 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100445 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200446 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100447 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200448prop_add_common(
449 linenr_T start_lnum,
450 colnr_T start_col,
451 dict_T *dict,
452 buf_T *default_buf,
453 typval_T *dict_arg)
454{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200455 linenr_T end_lnum;
456 colnr_T end_col;
457 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200458 buf_T *buf = default_buf;
459 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100460 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100461 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100462 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100463
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100464 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100465 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000466 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100467 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100468 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100469 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100470
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100471 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100472 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100473 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100474 if (end_lnum < start_lnum)
475 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000476 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100477 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100478 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100479 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100480 else
481 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100482
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100483 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100484 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100485 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100486
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100487 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100488 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000489 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100490 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100491 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100492 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100493 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100494 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100495 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100496 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100497 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100498 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000499 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100500 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100501 }
502 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100503 else if (start_lnum == end_lnum)
504 end_col = start_col;
505 else
506 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100507
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100508 if (dict_has_key(dict, "id"))
Christian Brabandt701c8632024-09-08 20:05:23 +0200509 {
510 vimlong_T x;
511 x = dict_get_number(dict, "id");
512 if (x > INT_MAX || x <= INT_MIN)
513 {
514 semsg(_(e_val_too_large), dict_get_string(dict, "id", FALSE));
515 goto theend;
516 }
517 id = (int)x;
518 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100519
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100520 if (dict_has_key(dict, "text"))
521 {
Bram Moolenaarfb593c52022-09-17 18:57:36 +0100522 if (dict_has_key(dict, "length")
523 || dict_has_key(dict, "end_col")
524 || dict_has_key(dict, "end_lnum"))
525 {
526 emsg(_(e_cannot_use_length_endcol_and_endlnum_with_text));
527 goto theend;
528 }
529
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100530 text = dict_get_string(dict, "text", TRUE);
531 if (text == NULL)
532 goto theend;
533 // use a default length of 1 to make multiple props show up
534 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100535
536 if (dict_has_key(dict, "text_align"))
537 {
538 char_u *p = dict_get_string(dict, "text_align", FALSE);
539
540 if (p == NULL)
541 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100542 if (start_col != 0)
543 {
544 emsg(_(e_can_only_use_text_align_when_column_is_zero));
545 goto theend;
546 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100547 if (STRCMP(p, "right") == 0)
548 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100549 else if (STRCMP(p, "above") == 0)
550 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100551 else if (STRCMP(p, "below") == 0)
552 flags |= TP_FLAG_ALIGN_BELOW;
553 else if (STRCMP(p, "after") != 0)
554 {
555 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
556 goto theend;
557 }
558 }
559
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100560 if (dict_has_key(dict, "text_padding_left"))
561 {
562 text_padding_left = dict_get_number(dict, "text_padding_left");
563 if (text_padding_left < 0)
564 {
565 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
566 goto theend;
567 }
568 }
569
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100570 if (dict_has_key(dict, "text_wrap"))
571 {
572 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100573
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100574 if (p == NULL)
575 goto theend;
576 if (STRCMP(p, "wrap") == 0)
577 flags |= TP_FLAG_WRAP;
578 else if (STRCMP(p, "truncate") != 0)
579 {
580 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
581 goto theend;
582 }
583 }
584 }
585
586 // Column must be 1 or more for a normal text property; when "text" is
587 // present zero means it goes after the line.
588 if (start_col < (text == NULL ? 1 : 0))
589 {
590 semsg(_(e_invalid_column_number_nr), (long)start_col);
591 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100592 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100593 if (start_col > 0 && text_padding_left > 0)
594 {
595 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
596 goto theend;
597 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100598
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200599 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100600 goto theend;
601
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000602 if (id < 0)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100603 {
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000604 if (buf->b_textprop_text.ga_len > 0)
605 {
606 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
607 goto theend;
608 }
609 did_use_negative_pop_id = TRUE;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100610 }
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000611
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100612 if (text != NULL)
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000613 {
614 if (did_use_negative_pop_id)
615 {
616 emsg(_(e_cannot_add_textprop_with_text_after_using_textprop_with_negative_id));
617 goto theend;
618 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100619 id = get_textprop_id(buf);
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000620 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100621
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100622 // This must be done _before_ we add the property because property changes
623 // trigger buffer (memline) reorganisation, which needs this flag to be
624 // correctly set.
625 buf->b_has_textprop = TRUE; // this is never reset
626
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100627 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100628 start_lnum, end_lnum, start_col, end_col);
629 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100630
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100631 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100632
633theend:
634 vim_free(text);
635 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100636}
637
638/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100639 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100640 * Returns the number of text properties and, when non-zero, a pointer to the
641 * first one in "props" (note that it is not aligned, therefore the char_u
642 * pointer).
643 */
644 int
645get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
646{
647 char_u *text;
648 size_t textlen;
649 size_t proplen;
650
Christian Brabandt0d0b3b12023-12-03 17:56:43 +0100651 // Be quick when no text property types have been defined for the buffer,
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100652 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200653 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100654 return 0;
655
656 // Fetch the line to get the ml_line_len field updated.
657 text = ml_get_buf(buf, lnum, will_change);
zeertzjq94b7c322024-03-12 21:50:32 +0100658 textlen = ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100659 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100660 if (proplen == 0)
661 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100662 if (proplen % sizeof(textprop_T) != 0)
663 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100664 iemsg(e_text_property_info_corrupted);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100665 return 0;
666 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100667 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100668 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100669}
670
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100671/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100672 * Return the number of text properties with "above" or "below" alignment in
673 * line "lnum". A "right" aligned property also goes below after a "below" or
674 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100675 */
676 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100677prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100678{
679 char_u *props;
680 int count = get_text_props(buf, lnum, &props, FALSE);
681 int result = 0;
682 textprop_T prop;
683 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100684 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100685
686 if (count == 0)
687 return 0;
688 for (i = 0; i < count; ++i)
689 {
690 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar89469d12022-12-02 20:46:26 +0000691 if (prop.tp_col == MAXCOL && text_prop_type_valid(buf, &prop))
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100692 {
Dylan Thacker-Smith515f7342024-03-28 12:01:14 +0100693 if ((prop.tp_flags & (TP_FLAG_ALIGN_ABOVE | TP_FLAG_ALIGN_BELOW))
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100694 || (next_right_goes_below
695 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
696 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100697 ++result;
698 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100699 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
700 next_right_goes_below = TRUE;
701 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100702 }
703 return result;
704}
705
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200706/**
707 * Return the number of text properties on line "lnum" in the current buffer.
708 * When "only_starting" is true only text properties starting in this line will
709 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100710 * When "last_line" is FALSE then text properties after the line are not
711 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200712 */
713 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100714count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200715{
716 char_u *props;
717 int proplen = get_text_props(curbuf, lnum, &props, 0);
718 int result = proplen;
719 int i;
720 textprop_T prop;
721
Bram Moolenaare175dc62022-08-01 22:18:50 +0100722 for (i = 0; i < proplen; ++i)
723 {
724 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100725 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100726 // previous line, or when not in the last line and it is virtual text
727 // after the line.
728 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
Christian Brabandt0d0b3b12023-12-03 17:56:43 +0100729 || (!last_line && prop.tp_col == MAXCOL))
Bram Moolenaare175dc62022-08-01 22:18:50 +0100730 --result;
731 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200732 return result;
733}
734
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100735static textprop_T *text_prop_compare_props;
736static buf_T *text_prop_compare_buf;
737
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000738/*
739 * Score for sorting on position of the text property: 0: above,
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100740 * 1: after (default), 2: right, 3: below (comes last)
741 */
742 static int
743text_prop_order(int flags)
744{
745 if (flags & TP_FLAG_ALIGN_ABOVE)
746 return 0;
747 if (flags & TP_FLAG_ALIGN_RIGHT)
748 return 2;
749 if (flags & TP_FLAG_ALIGN_BELOW)
750 return 3;
751 return 1;
752}
753
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100754/*
755 * Function passed to qsort() to sort text properties.
756 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
757 * both have the same priority.
758 */
759 static int
760text_prop_compare(const void *s1, const void *s2)
761{
762 int idx1, idx2;
763 textprop_T *tp1, *tp2;
764 proptype_T *pt1, *pt2;
765 colnr_T col1, col2;
766
767 idx1 = *(int *)s1;
768 idx2 = *(int *)s2;
769 tp1 = &text_prop_compare_props[idx1];
770 tp2 = &text_prop_compare_props[idx2];
771 col1 = tp1->tp_col;
772 col2 = tp2->tp_col;
Dylan Thacker-Smith80557212024-02-21 21:00:59 +0100773
774 // property that inserts text has priority over one that doesn't
775 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
776 return tp1->tp_id < 0 ? 1 : -1;
777
zeertzjqce53e3e2023-09-01 18:49:30 +0200778 if (col1 == MAXCOL || col2 == MAXCOL)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100779 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100780 int order1 = text_prop_order(tp1->tp_flags);
781 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100782
zeertzjqce53e3e2023-09-01 18:49:30 +0200783 // sort on order where it is added
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100784 if (order1 != order2)
785 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100786 }
787
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100788 // check highest priority, defined by the type
789 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
790 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
791 if (pt1 != pt2)
792 {
793 if (pt1 == NULL)
794 return -1;
795 if (pt2 == NULL)
796 return 1;
797 if (pt1->pt_priority != pt2->pt_priority)
798 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
799 }
800
801 // same priority, one that starts first wins
802 if (col1 != col2)
803 return col1 < col2 ? 1 : -1;
804
805 // for a property with text the id can be used as tie breaker
806 if (tp1->tp_id < 0)
807 return tp1->tp_id > tp2->tp_id ? 1 : -1;
808
809 return 0;
810}
811
812/*
813 * Sort "count" text properties using an array if indexes "idxs" into the list
814 * of text props "props" for buffer "buf".
815 */
816 void
817sort_text_props(
818 buf_T *buf,
819 textprop_T *props,
820 int *idxs,
821 int count)
822{
823 text_prop_compare_buf = buf;
824 text_prop_compare_props = props;
825 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
826}
827
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100828/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200829 * Find text property "type_id" in the visible lines of window "wp".
830 * Match "id" when it is > 0.
831 * Returns FAIL when not found.
832 */
833 int
Bram Moolenaar89469d12022-12-02 20:46:26 +0000834find_visible_prop(
835 win_T *wp,
836 int type_id,
837 int id,
838 textprop_T *prop,
839 linenr_T *found_lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200840{
Bram Moolenaar89469d12022-12-02 20:46:26 +0000841 // return when "type_id" no longer exists
842 if (text_prop_type_by_id(wp->w_buffer, type_id) == NULL)
843 return FAIL;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200844
845 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100846 validate_botline_win(wp);
Bram Moolenaar89469d12022-12-02 20:46:26 +0000847 for (linenr_T lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200848 {
Bram Moolenaar89469d12022-12-02 20:46:26 +0000849 char_u *props;
850 int count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
851 for (int i = 0; i < count; ++i)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200852 {
853 mch_memmove(prop, props + i * sizeof(textprop_T),
854 sizeof(textprop_T));
855 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
856 {
857 *found_lnum = lnum;
858 return OK;
859 }
860 }
861 }
862 return FAIL;
863}
864
865/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100866 * Set the text properties for line "lnum" to "props" with length "len".
867 * If "len" is zero text properties are removed, "props" is not used.
868 * Any existing text properties are dropped.
869 * Only works for the current buffer.
870 */
871 static void
872set_text_props(linenr_T lnum, char_u *props, int len)
873{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100874 char_u *text;
875 char_u *newtext;
876 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100877
878 text = ml_get(lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100879 textlen = ml_get_len(lnum) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100880 newtext = alloc(textlen + len);
881 if (newtext == NULL)
882 return;
883 mch_memmove(newtext, text, textlen);
884 if (len > 0)
885 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100886 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100887 vim_free(curbuf->b_ml.ml_line_ptr);
888 curbuf->b_ml.ml_line_ptr = newtext;
889 curbuf->b_ml.ml_line_len = textlen + len;
890 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
891}
892
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100893/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100894 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100895 */
896 void
897add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
898{
899 char_u *text;
900 char_u *newtext;
901 int proplen = text_prop_count * (int)sizeof(textprop_T);
902
903 text = ml_get(lnum);
904 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
905 if (newtext == NULL)
906 return;
907 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
908 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
909 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
910 vim_free(curbuf->b_ml.ml_line_ptr);
911 curbuf->b_ml.ml_line_ptr = newtext;
912 curbuf->b_ml.ml_line_len += proplen;
913 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
914}
915
Bram Moolenaare44336b2022-08-07 18:20:08 +0100916/*
917 * Function passed to qsort() for sorting proptype_T on pt_id.
918 */
919 static int
920compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100921{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100922 proptype_T *tp1 = *(proptype_T **)s1;
923 proptype_T *tp2 = *(proptype_T **)s2;
924
925 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
926}
927
928 static proptype_T *
929find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
930{
931 int low = 0;
932 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100933
Bram Moolenaar10246902022-08-08 17:08:05 +0100934 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100935 return NULL;
936
dundargocc57b5bc2022-11-02 13:30:51 +0000937 // Make the lookup faster by creating an array with pointers to
Bram Moolenaare44336b2022-08-07 18:20:08 +0100938 // hashtable entries, sorted on pt_id.
939 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100940 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100941 long todo;
942 hashitem_T *hi;
943 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100944
Bram Moolenaare44336b2022-08-07 18:20:08 +0100945 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
946 if (*array == NULL)
947 return NULL;
948 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000949 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100950 {
951 if (!HASHITEM_EMPTY(hi))
952 {
953 (*array)[i++] = HI2PT(hi);
954 --todo;
955 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100956 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100957 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
958 }
959
960 // binary search in the sorted array
961 high = ht->ht_used;
962 while (high > low)
963 {
964 int m = (high + low) / 2;
965
966 if ((*array)[m]->pt_id == id)
967 return (*array)[m];
968 if ((*array)[m]->pt_id > id)
969 high = m;
970 else
971 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100972 }
973 return NULL;
974}
975
976/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100977 * Fill 'dict' with text properties in 'prop'.
978 */
979 static void
980prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
981{
982 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200983 int buflocal = TRUE;
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200984 int virtualtext_prop = prop->tp_id < 0;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100985
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200986 dict_add_number(dict, "col", (prop->tp_col == MAXCOL) ? 0 : prop->tp_col);
987 if (!virtualtext_prop)
988 {
989 dict_add_number(dict, "length", prop->tp_len);
990 dict_add_number(dict, "id", prop->tp_id);
991 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100992 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
993 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200994
Bram Moolenaare44336b2022-08-07 18:20:08 +0100995 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200996 if (pt == NULL)
997 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100998 pt = find_type_by_id(global_proptypes, &global_proparray,
999 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +02001000 buflocal = FALSE;
1001 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001002 if (pt != NULL)
1003 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +02001004
1005 if (buflocal)
1006 dict_add_number(dict, "type_bufnr", buf->b_fnum);
1007 else
1008 dict_add_number(dict, "type_bufnr", 0);
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +02001009 if (virtualtext_prop)
Yegappan Lakshmananf9037f12023-08-20 18:27:45 +02001010 {
1011 // virtual text property
1012 garray_T *gap = &buf->b_textprop_text;
1013 char_u *text;
1014
1015 // negate the property id to get the string index
1016 text = ((char_u **)gap->ga_data)[-prop->tp_id - 1];
1017 dict_add_string(dict, "text", text);
1018
1019 // text_align
1020 char_u *text_align = NULL;
1021 if (prop->tp_flags & TP_FLAG_ALIGN_RIGHT)
1022 text_align = (char_u *)"right";
1023 else if (prop->tp_flags & TP_FLAG_ALIGN_ABOVE)
1024 text_align = (char_u *)"above";
1025 else if (prop->tp_flags & TP_FLAG_ALIGN_BELOW)
1026 text_align = (char_u *)"below";
1027 if (text_align != NULL)
1028 dict_add_string(dict, "text_align", text_align);
1029
1030 // text_wrap
1031 if (prop->tp_flags & TP_FLAG_WRAP)
1032 dict_add_string(dict, "text_wrap", (char_u *)"wrap");
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +02001033 if (prop->tp_padleft != 0)
1034 dict_add_number(dict, "text_padding_left", prop->tp_padleft);
Yegappan Lakshmananf9037f12023-08-20 18:27:45 +02001035 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001036}
1037
1038/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001039 * Find a property type by ID in "buf" or globally.
1040 * Returns NULL if not found.
1041 */
1042 proptype_T *
1043text_prop_type_by_id(buf_T *buf, int id)
1044{
1045 proptype_T *type;
1046
Bram Moolenaare44336b2022-08-07 18:20:08 +01001047 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001048 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001049 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001050 return type;
1051}
1052
1053/*
Bram Moolenaar89469d12022-12-02 20:46:26 +00001054 * Return TRUE if "prop" is a valid text property type.
1055 */
1056 int
1057text_prop_type_valid(buf_T *buf, textprop_T *prop)
1058{
1059 return text_prop_type_by_id(buf, prop->tp_type) != NULL;
1060}
1061
1062/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001063 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
1064 */
1065 void
1066f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
1067{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001068 linenr_T start;
1069 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001070 linenr_T lnum;
1071 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001072 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001073
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001074 if (in_vim9script()
1075 && (check_for_number_arg(argvars, 0) == FAIL
1076 || check_for_opt_number_arg(argvars, 1) == FAIL
1077 || (argvars[1].v_type != VAR_UNKNOWN
1078 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1079 return;
1080
1081 start = tv_get_number(&argvars[0]);
1082 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001083 if (argvars[1].v_type != VAR_UNKNOWN)
1084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001085 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001086 if (argvars[2].v_type != VAR_UNKNOWN)
1087 {
1088 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
1089 return;
1090 }
1091 }
1092 if (start < 1 || end < 1)
1093 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001094 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001095 return;
1096 }
1097
1098 for (lnum = start; lnum <= end; ++lnum)
1099 {
1100 char_u *text;
1101 size_t len;
1102
1103 if (lnum > buf->b_ml.ml_line_count)
1104 break;
1105 text = ml_get_buf(buf, lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001106 len = ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001107 if ((size_t)buf->b_ml.ml_line_len > len)
1108 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001109 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001110 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1111 {
1112 char_u *newtext = vim_strsave(text);
1113
1114 // need to allocate the line now
1115 if (newtext == NULL)
1116 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001117 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1118 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001119 buf->b_ml.ml_line_ptr = newtext;
1120 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1121 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001122 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001123 }
1124 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001125 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001126 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001127}
1128
1129/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001130 * prop_find({props} [, {direction}])
1131 */
1132 void
1133f_prop_find(typval_T *argvars, typval_T *rettv)
1134{
1135 pos_T *cursor = &curwin->w_cursor;
1136 dict_T *dict;
1137 buf_T *buf = curbuf;
1138 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001139 int lnum_start;
1140 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001141 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001142 int id = 0;
1143 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001144 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001145 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001146 int lnum = -1;
1147 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001148 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001149 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001150
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001151 if (in_vim9script()
1152 && (check_for_dict_arg(argvars, 0) == FAIL
1153 || check_for_opt_string_arg(argvars, 1) == FAIL))
1154 return;
1155
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001156 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001157 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001158 dict = argvars[0].vval.v_dict;
1159
1160 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1161 return;
1162 if (buf->b_ml.ml_mfp == NULL)
1163 return;
1164
1165 if (argvars[1].v_type != VAR_UNKNOWN)
1166 {
1167 char_u *dir_s = tv_get_string(&argvars[1]);
1168
1169 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001170 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001171 else if (*dir_s != 'f')
1172 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001173 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001174 return;
1175 }
1176 }
1177
1178 di = dict_find(dict, (char_u *)"lnum", -1);
1179 if (di != NULL)
1180 lnum = tv_get_number(&di->di_tv);
1181
1182 di = dict_find(dict, (char_u *)"col", -1);
1183 if (di != NULL)
1184 col = tv_get_number(&di->di_tv);
1185
1186 if (lnum == -1)
1187 {
1188 lnum = cursor->lnum;
1189 col = cursor->col + 1;
1190 }
1191 else if (col == -1)
1192 col = 1;
1193
1194 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1195 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001196 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001197 return;
1198 }
1199
Bram Moolenaard61efa52022-07-23 09:52:04 +01001200 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001201
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001202 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001203 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001204 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001205 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001206 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001207 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001208 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001209 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001210 proptype_T *type = lookup_prop_type(name, buf);
1211
1212 if (type == NULL)
1213 return;
1214 type_id = type->pt_id;
1215 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001216 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001217 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001218 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001219 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001220 return;
1221 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001222 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001223 {
Ben Jacksona7704222022-08-20 20:54:51 +01001224 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001225 return;
1226 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001227
1228 lnum_start = lnum;
1229
1230 if (rettv_dict_alloc(rettv) == FAIL)
1231 return;
1232
1233 while (1)
1234 {
1235 char_u *text = ml_get_buf(buf, lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001236 size_t textlen = ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001237 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001238 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001239 int i;
1240 textprop_T prop;
1241 int prop_start;
1242 int prop_end;
1243
LemonBoy9bd3ce22022-04-18 21:54:02 +01001244 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001245 {
1246 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001247 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001248
LemonBoy9bd3ce22022-04-18 21:54:02 +01001249 // For the very first line try to find the first property before or
1250 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001251 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001252 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001253 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001254 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001255 if (prop.tp_col > col)
1256 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001257 }
1258 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1259 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001260 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001261 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001262 : (id_found && prop.tp_id == id)
1263 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001264 {
1265 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001266 if (lnum_start == lnum
1267 && col >= prop.tp_col
1268 && (col <= prop.tp_col + prop.tp_len
1269 - (prop.tp_len != 0)))
1270 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001271
LemonBoy9bd3ce22022-04-18 21:54:02 +01001272 // The property was not continued from last line, it starts on
1273 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001274 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001275 // The property does not continue on the next line, it ends on
1276 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001277 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001278 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001279 seen_end = 1;
1280
1281 // Skip lines without the start flag.
1282 if (!prop_start)
1283 {
1284 // Always search backwards for start when search started
1285 // on a prop and we're not skipping.
1286 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001287 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001288 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001289 }
1290
1291 // If skipstart is true, skip the prop at start pos (even if
1292 // continued from another line).
1293 if (start_pos_has_prop && skipstart && !seen_end)
1294 {
1295 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001296 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001297 }
1298
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001299 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1300 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1301
1302 return;
1303 }
1304 }
1305
1306 if (dir > 0)
1307 {
1308 if (lnum >= buf->b_ml.ml_line_count)
1309 break;
1310 lnum++;
1311 }
1312 else
1313 {
1314 if (lnum <= 1)
1315 break;
1316 lnum--;
1317 }
1318 }
1319}
1320
1321/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001322 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1323 */
1324 static int
1325prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1326{
1327 int i;
1328
1329 for (i = 0; i < len; i++)
1330 if (types_or_ids[i] == type_or_id)
1331 return TRUE;
1332
1333 return FALSE;
1334}
1335
1336/*
1337 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1338 * If 'prop_types' is not NULL, then return only the text properties with
1339 * matching property type in the 'prop_types' array.
1340 * If 'prop_ids' is not NULL, then return only the text properties with
1341 * an identifier in the 'props_ids' array.
1342 * If 'add_lnum' is TRUE, then add the line number also to the text property
1343 * dictionary.
1344 */
1345 static void
1346get_props_in_line(
1347 buf_T *buf,
1348 linenr_T lnum,
1349 int *prop_types,
1350 int prop_types_len,
1351 int *prop_ids,
1352 int prop_ids_len,
1353 list_T *retlist,
1354 int add_lnum)
1355{
1356 char_u *text = ml_get_buf(buf, lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001357 size_t textlen = ml_get_buf_len(buf, lnum) + 1;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001358 int count;
1359 int i;
1360 textprop_T prop;
1361
1362 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1363 for (i = 0; i < count; ++i)
1364 {
1365 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1366 sizeof(textprop_T));
1367 if ((prop_types == NULL
1368 || prop_type_or_id_in_list(prop_types, prop_types_len,
1369 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001370 && (prop_ids == NULL
1371 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1372 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001373 {
1374 dict_T *d = dict_alloc();
1375
1376 if (d == NULL)
1377 break;
1378 prop_fill_dict(d, &prop, buf);
1379 if (add_lnum)
1380 dict_add_number(d, "lnum", lnum);
1381 list_append_dict(retlist, d);
1382 }
1383 }
1384}
1385
1386/*
1387 * Convert a List of property type names into an array of property type
1388 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1389 * error. 'num_types' is set to the number of returned property types.
1390 */
1391 static int *
1392get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1393{
1394 int *prop_types;
1395 listitem_T *li;
1396 int i;
1397 char_u *name;
1398 proptype_T *type;
1399
1400 *num_types = 0;
1401
1402 prop_types = ALLOC_MULT(int, list_len(l));
1403 if (prop_types == NULL)
1404 return NULL;
1405
1406 i = 0;
1407 FOR_ALL_LIST_ITEMS(l, li)
1408 {
1409 if (li->li_tv.v_type != VAR_STRING)
1410 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001411 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001412 goto errret;
1413 }
1414 name = li->li_tv.vval.v_string;
1415 if (name == NULL)
1416 goto errret;
1417
1418 type = lookup_prop_type(name, buf);
1419 if (type == NULL)
1420 goto errret;
1421 prop_types[i++] = type->pt_id;
1422 }
1423
1424 *num_types = i;
1425 return prop_types;
1426
1427errret:
1428 VIM_CLEAR(prop_types);
1429 return NULL;
1430}
1431
1432/*
1433 * Convert a List of property identifiers into an array of property
1434 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1435 * error. 'num_ids' is set to the number of returned property identifiers.
1436 */
1437 static int *
1438get_prop_ids_from_list(list_T *l, int *num_ids)
1439{
1440 int *prop_ids;
1441 listitem_T *li;
Christian Brabandt56b12072025-05-21 21:01:40 +02001442 int i = 0;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001443 int id;
1444 int error;
1445
1446 *num_ids = 0;
1447
1448 prop_ids = ALLOC_MULT(int, list_len(l));
1449 if (prop_ids == NULL)
1450 return NULL;
1451
Christian Brabandt56b12072025-05-21 21:01:40 +02001452 CHECK_LIST_MATERIALIZE(l);
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001453 FOR_ALL_LIST_ITEMS(l, li)
1454 {
1455 error = FALSE;
1456 id = tv_get_number_chk(&li->li_tv, &error);
1457 if (error)
1458 goto errret;
1459
1460 prop_ids[i++] = id;
1461 }
1462
1463 *num_ids = i;
1464 return prop_ids;
1465
1466errret:
1467 VIM_CLEAR(prop_ids);
1468 return NULL;
1469}
1470
1471/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001472 * prop_list({lnum} [, {bufnr}])
1473 */
1474 void
1475f_prop_list(typval_T *argvars, typval_T *rettv)
1476{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001477 linenr_T lnum;
1478 linenr_T start_lnum;
1479 linenr_T end_lnum;
1480 buf_T *buf = curbuf;
1481 int add_lnum = FALSE;
1482 int *prop_types = NULL;
1483 int prop_types_len = 0;
1484 int *prop_ids = NULL;
1485 int prop_ids_len = 0;
1486 list_T *l;
1487 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001488
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001489 if (in_vim9script()
1490 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001491 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001492 return;
1493
Bram Moolenaar93a10962022-06-16 11:42:09 +01001494 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001495 return;
1496
1497 // default: get text properties on current line
1498 start_lnum = tv_get_number(&argvars[0]);
1499 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001500 if (argvars[1].v_type != VAR_UNKNOWN)
1501 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001502 dict_T *d;
1503
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001504 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001505 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001506 d = argvars[1].vval.v_dict;
1507
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001508 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1509 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001510
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001511 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001512 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001513 if (di->di_tv.v_type != VAR_NUMBER)
1514 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001515 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001516 return;
1517 }
1518 end_lnum = tv_get_number(&di->di_tv);
1519 if (end_lnum < 0)
1520 // negative end_lnum is used as an offset from the last buffer
1521 // line
1522 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1523 else if (end_lnum > buf->b_ml.ml_line_count)
1524 end_lnum = buf->b_ml.ml_line_count;
1525 add_lnum = TRUE;
1526 }
1527 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1528 {
1529 if (di->di_tv.v_type != VAR_LIST)
1530 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001531 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001532 return;
1533 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001534
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001535 l = di->di_tv.vval.v_list;
1536 if (l != NULL && list_len(l) > 0)
1537 {
1538 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1539 if (prop_types == NULL)
1540 return;
1541 }
1542 }
1543 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1544 {
1545 if (di->di_tv.v_type != VAR_LIST)
1546 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001547 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001548 goto errret;
1549 }
1550
1551 l = di->di_tv.vval.v_list;
1552 if (l != NULL && list_len(l) > 0)
1553 {
1554 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1555 if (prop_ids == NULL)
1556 goto errret;
1557 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001558 }
1559 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001560 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1561 || end_lnum < 1 || end_lnum < start_lnum)
1562 emsg(_(e_invalid_range));
1563 else
1564 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1565 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1566 prop_ids, prop_ids_len,
1567 rettv->vval.v_list, add_lnum);
1568
1569errret:
1570 VIM_CLEAR(prop_types);
1571 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001572}
1573
1574/*
1575 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1576 */
1577 void
1578f_prop_remove(typval_T *argvars, typval_T *rettv)
1579{
1580 linenr_T start = 1;
1581 linenr_T end = 0;
1582 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001583 linenr_T first_changed = 0;
1584 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001585 dict_T *dict;
1586 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001587 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001588 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001589 int type_id = -1; // for a single "type"
1590 int *type_ids = NULL; // array, for a list of "types", allocated
1591 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001592 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001593 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001594
1595 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001596
1597 if (in_vim9script()
1598 && (check_for_dict_arg(argvars, 0) == FAIL
1599 || check_for_opt_number_arg(argvars, 1) == FAIL
1600 || (argvars[1].v_type != VAR_UNKNOWN
1601 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1602 return;
1603
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001604 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001605 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001606
1607 if (argvars[1].v_type != VAR_UNKNOWN)
1608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001609 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001610 end = start;
1611 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001612 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001613 if (start < 1 || end < 1)
1614 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001615 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001616 return;
1617 }
1618 }
1619
1620 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001621 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1622 return;
1623 if (buf->b_ml.ml_mfp == NULL)
1624 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001625
Bram Moolenaard61efa52022-07-23 09:52:04 +01001626 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001627
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001628 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001629 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001630
1631 // if a specific type was supplied "type": check that (and ignore "types".
1632 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001633 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001634 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001635 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001636 proptype_T *type = lookup_prop_type(name, buf);
1637
1638 if (type == NULL)
1639 return;
1640 type_id = type->pt_id;
1641 }
Ben Jacksona7704222022-08-20 20:54:51 +01001642 if (dict_has_key(dict, "types"))
1643 {
1644 typval_T types;
1645 listitem_T *li = NULL;
1646
1647 dict_get_tv(dict, "types", &types);
1648 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1649 {
1650 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1651
1652 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1653 {
1654 proptype_T *prop_type;
1655
1656 if (li->li_tv.v_type != VAR_STRING)
1657 continue;
1658
1659 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1660
1661 if (!prop_type)
1662 goto cleanup_prop_remove;
1663
1664 type_ids[num_type_ids++] = prop_type->pt_id;
1665 }
1666 }
1667 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001668 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001669
Ben Jacksona7704222022-08-20 20:54:51 +01001670 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001671 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001672 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001673 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001674 }
Ben Jacksona7704222022-08-20 20:54:51 +01001675 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001676 {
Ben Jacksona7704222022-08-20 20:54:51 +01001677 emsg(_(e_need_id_and_type_or_types_with_both));
1678 goto cleanup_prop_remove;
1679 }
1680 if (type_id != -1 && num_type_ids > 0)
1681 {
1682 emsg(_(e_cannot_specify_both_type_and_types));
1683 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001684 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001685
1686 if (end == 0)
1687 end = buf->b_ml.ml_line_count;
1688 for (lnum = start; lnum <= end; ++lnum)
1689 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001690 size_t len;
1691
1692 if (lnum > buf->b_ml.ml_line_count)
1693 break;
zeertzjq94b7c322024-03-12 21:50:32 +01001694 len = ml_get_buf_len(buf, lnum) + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001695 if ((size_t)buf->b_ml.ml_line_len > len)
1696 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001697 static textprop_T textprop; // static because of alignment
1698 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001699
1700 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1701 / sizeof(textprop_T); ++idx)
1702 {
1703 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1704 + idx * sizeof(textprop_T);
1705 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001706 int matches_id = 0;
1707 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001708
1709 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001710
1711 matches_id = textprop.tp_id == id;
1712 if (num_type_ids > 0)
1713 {
1714 int idx2;
1715
1716 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1717 matches_type = textprop.tp_type == type_ids[idx2];
1718 }
1719 else
1720 {
1721 matches_type = textprop.tp_type == type_id;
1722 }
1723
1724 if (both ? matches_id && matches_type
1725 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001726 {
1727 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1728 {
1729 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1730
1731 // need to allocate the line to be able to change it
1732 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001733 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001734 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1735 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001736 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1737 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001738 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001739 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1740
1741 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001742 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001743 }
1744
1745 taillen = buf->b_ml.ml_line_len - len
1746 - (idx + 1) * sizeof(textprop_T);
1747 if (taillen > 0)
1748 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1749 taillen);
1750 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1751 --idx;
1752
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001753 if (textprop.tp_id < 0)
1754 {
1755 garray_T *gap = &buf->b_textprop_text;
1756 int ii = -textprop.tp_id - 1;
1757
1758 // negative ID: property with text - free the text
1759 if (ii < gap->ga_len)
1760 {
1761 char_u **p = ((char_u **)gap->ga_data) + ii;
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00001762 VIM_CLEAR(*p);
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001763 did_remove_text = TRUE;
1764 }
1765 }
1766
Bram Moolenaar965c0442021-05-17 00:22:06 +02001767 if (first_changed == 0)
1768 first_changed = lnum;
1769 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001770 ++rettv->vval.v_number;
1771 if (!do_all)
1772 break;
1773 }
1774 }
1775 }
1776 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001777
Bram Moolenaar965c0442021-05-17 00:22:06 +02001778 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001779 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001780 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001781 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001782 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001783 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001784
1785 if (did_remove_text)
1786 {
1787 garray_T *gap = &buf->b_textprop_text;
1788
1789 // Reduce the growarray size for NULL pointers at the end.
1790 while (gap->ga_len > 0
1791 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1792 --gap->ga_len;
1793 }
Ben Jacksona7704222022-08-20 20:54:51 +01001794
1795cleanup_prop_remove:
1796 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001797}
1798
1799/*
1800 * Common for f_prop_type_add() and f_prop_type_change().
1801 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001802 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001803prop_type_set(typval_T *argvars, int add)
1804{
1805 char_u *name;
1806 buf_T *buf = NULL;
1807 dict_T *dict;
1808 dictitem_T *di;
1809 proptype_T *prop;
1810
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001811 if (in_vim9script()
1812 && (check_for_string_arg(argvars, 0) == FAIL
1813 || check_for_dict_arg(argvars, 1) == FAIL))
1814 return;
1815
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001816 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001817 if (*name == NUL)
1818 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001819 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001820 return;
1821 }
1822
1823 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1824 return;
1825 dict = argvars[1].vval.v_dict;
1826
Bram Moolenaare44336b2022-08-07 18:20:08 +01001827 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001828 if (add)
1829 {
1830 hashtab_T **htp;
1831
1832 if (prop != NULL)
1833 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001834 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001835 return;
1836 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001837 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001838 if (prop == NULL)
1839 return;
1840 STRCPY(prop->pt_name, name);
1841 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001842 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001843 if (buf == NULL)
1844 {
1845 htp = &global_proptypes;
1846 VIM_CLEAR(global_proparray);
1847 }
1848 else
1849 {
1850 htp = &buf->b_proptypes;
1851 VIM_CLEAR(buf->b_proparray);
1852 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001853 if (*htp == NULL)
1854 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001855 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001856 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001857 {
1858 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001859 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001860 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001861 hash_init(*htp);
1862 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001863 hash_add(*htp, PT2HIKEY(prop), "prop type");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001864 }
1865 else
1866 {
1867 if (prop == NULL)
1868 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001869 semsg(_(e_property_type_str_does_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001870 return;
1871 }
1872 }
1873
1874 if (dict != NULL)
1875 {
1876 di = dict_find(dict, (char_u *)"highlight", -1);
1877 if (di != NULL)
1878 {
1879 char_u *highlight;
1880 int hl_id = 0;
1881
Bram Moolenaard61efa52022-07-23 09:52:04 +01001882 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001883 if (highlight != NULL && *highlight != NUL)
1884 hl_id = syn_name2id(highlight);
1885 if (hl_id <= 0)
1886 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001887 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001888 highlight == NULL ? (char_u *)"" : highlight);
1889 return;
1890 }
1891 prop->pt_hl_id = hl_id;
1892 }
1893
Bram Moolenaar58187f12019-05-05 16:33:47 +02001894 di = dict_find(dict, (char_u *)"combine", -1);
1895 if (di != NULL)
1896 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001897 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001898 prop->pt_flags |= PT_FLAG_COMBINE;
1899 else
1900 prop->pt_flags &= ~PT_FLAG_COMBINE;
1901 }
1902
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001903 di = dict_find(dict, (char_u *)"override", -1);
1904 if (di != NULL)
1905 {
1906 if (tv_get_bool(&di->di_tv))
1907 prop->pt_flags |= PT_FLAG_OVERRIDE;
1908 else
1909 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1910 }
1911
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001912 di = dict_find(dict, (char_u *)"priority", -1);
1913 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001914 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001915
1916 di = dict_find(dict, (char_u *)"start_incl", -1);
1917 if (di != NULL)
1918 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001919 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001920 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1921 else
1922 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1923 }
1924
1925 di = dict_find(dict, (char_u *)"end_incl", -1);
1926 if (di != NULL)
1927 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001928 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001929 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1930 else
1931 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1932 }
1933 }
1934}
1935
1936/*
1937 * prop_type_add({name}, {props})
1938 */
1939 void
1940f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1941{
1942 prop_type_set(argvars, TRUE);
1943}
1944
1945/*
1946 * prop_type_change({name}, {props})
1947 */
1948 void
1949f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1950{
1951 prop_type_set(argvars, FALSE);
1952}
1953
1954/*
1955 * prop_type_delete({name} [, {bufnr}])
1956 */
1957 void
1958f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1959{
1960 char_u *name;
1961 buf_T *buf = NULL;
1962 hashitem_T *hi;
1963
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001964 if (in_vim9script()
1965 && (check_for_string_arg(argvars, 0) == FAIL
1966 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1967 return;
1968
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001969 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001970 if (*name == NUL)
1971 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001972 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001973 return;
1974 }
1975
1976 if (argvars[1].v_type != VAR_UNKNOWN)
1977 {
1978 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1979 return;
1980 }
1981
Bram Moolenaare44336b2022-08-07 18:20:08 +01001982 hi = find_prop_type_hi(name, buf);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001983 if (hi == NULL)
1984 return;
1985
1986 hashtab_T *ht;
1987 proptype_T *prop = HI2PT(hi);
1988
1989 if (buf == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001990 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001991 ht = global_proptypes;
1992 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001993 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001994 else
1995 {
1996 ht = buf->b_proptypes;
1997 VIM_CLEAR(buf->b_proparray);
1998 }
1999 hash_remove(ht, hi, "prop type delete");
2000 vim_free(prop);
2001
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002002 // currently visible text properties will disappear
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002003 redraw_all_later(UPD_CLEAR);
2004 changed_window_setting_buf(buf == NULL ? curbuf : buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002005}
2006
2007/*
Martin Tournoije2390c72021-07-28 13:30:16 +02002008 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002009 */
2010 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01002011f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002012{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002013 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002014
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002015 if (in_vim9script()
2016 && (check_for_string_arg(argvars, 0) == FAIL
2017 || check_for_opt_dict_arg(argvars, 1) == FAIL))
2018 return;
2019
2020 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002021 if (*name == NUL)
2022 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00002023 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002024 return;
2025 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002026
2027 if (rettv_dict_alloc(rettv) == FAIL)
2028 return;
2029
2030 proptype_T *prop = NULL;
2031 buf_T *buf = NULL;
2032
2033 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002034 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002035 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
2036 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002037 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002038
2039 prop = find_prop_type(name, buf);
2040 if (prop == NULL)
2041 return;
2042
2043 dict_T *d = rettv->vval.v_dict;
2044
2045 if (prop->pt_hl_id > 0)
2046 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
2047 dict_add_number(d, "priority", prop->pt_priority);
2048 dict_add_number(d, "combine",
2049 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
2050 dict_add_number(d, "start_incl",
2051 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
2052 dict_add_number(d, "end_incl",
2053 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
2054 if (buf != NULL)
2055 dict_add_number(d, "bufnr", buf->b_fnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002056}
2057
2058 static void
2059list_types(hashtab_T *ht, list_T *l)
2060{
2061 long todo;
2062 hashitem_T *hi;
2063
2064 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002065 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002066 {
2067 if (!HASHITEM_EMPTY(hi))
2068 {
2069 proptype_T *prop = HI2PT(hi);
2070
2071 list_append_string(l, prop->pt_name, -1);
2072 --todo;
2073 }
2074 }
2075}
2076
2077/*
2078 * prop_type_list([{bufnr}])
2079 */
2080 void
Dominique Pellé0268ff32024-07-28 21:12:20 +02002081f_prop_type_list(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002082{
2083 buf_T *buf = NULL;
2084
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002085 if (rettv_list_alloc(rettv) == FAIL)
2086 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002087
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002088 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
2089 return;
2090
2091 if (argvars[0].v_type != VAR_UNKNOWN)
2092 {
2093 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2094 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002095 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002096 if (buf == NULL)
2097 {
2098 if (global_proptypes != NULL)
2099 list_types(global_proptypes, rettv->vval.v_list);
2100 }
2101 else if (buf->b_proptypes != NULL)
2102 list_types(buf->b_proptypes, rettv->vval.v_list);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002103}
2104
2105/*
2106 * Free all property types in "ht".
2107 */
2108 static void
2109clear_ht_prop_types(hashtab_T *ht)
2110{
2111 long todo;
2112 hashitem_T *hi;
2113
2114 if (ht == NULL)
2115 return;
2116
2117 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002118 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002119 {
2120 if (!HASHITEM_EMPTY(hi))
2121 {
2122 proptype_T *prop = HI2PT(hi);
2123
2124 vim_free(prop);
2125 --todo;
2126 }
2127 }
2128
2129 hash_clear(ht);
2130 vim_free(ht);
2131}
2132
2133#if defined(EXITFREE) || defined(PROTO)
2134/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002135 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002136 */
2137 void
2138clear_global_prop_types(void)
2139{
2140 clear_ht_prop_types(global_proptypes);
2141 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002142 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002143}
2144#endif
2145
2146/*
2147 * Free all property types for "buf".
2148 */
2149 void
2150clear_buf_prop_types(buf_T *buf)
2151{
2152 clear_ht_prop_types(buf->b_proptypes);
2153 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002154 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002155}
2156
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002157// Struct used to return two values from adjust_prop().
2158typedef struct
2159{
2160 int dirty; // if the property was changed
2161 int can_drop; // whether after this change, the prop may be removed
2162} adjustres_T;
2163
2164/*
2165 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2166 *
2167 * Note that "col" is zero-based, while tp_col is one-based.
2168 * Only for the current buffer.
2169 * "flags" can have:
2170 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002171 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002172 */
2173 static adjustres_T
2174adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002175 textprop_T *prop,
2176 colnr_T col,
2177 int added,
2178 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002179{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002180 proptype_T *pt;
2181 int start_incl;
2182 int end_incl;
2183 int droppable;
2184 adjustres_T res = {TRUE, FALSE};
2185
2186 // prop after end of the line doesn't move
2187 if (prop->tp_col == MAXCOL)
2188 {
2189 res.dirty = FALSE;
2190 return res;
2191 }
2192
2193 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2194 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002195 || (flags & APC_SUBSTITUTE)
2196 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002197 if (prop->tp_id < 0 && (flags & APC_INDENT))
2198 // when inserting indent just before a character with virtual text
2199 // shift the text property
2200 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002201 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002202 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002203 // do not drop zero-width props if they later can increase in size
2204 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002205
2206 if (added > 0)
2207 {
2208 if (col + 1 <= prop->tp_col
2209 - (start_incl || (prop->tp_len == 0 && end_incl)))
2210 // Change is entirely before the text property: Only shift
2211 prop->tp_col += added;
2212 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2213 // Insertion was inside text property
2214 prop->tp_len += added;
2215 }
2216 else if (prop->tp_col > col + 1)
2217 {
2218 if (prop->tp_col + added < col + 1)
2219 {
2220 prop->tp_len += (prop->tp_col - 1 - col) + added;
2221 prop->tp_col = col + 1;
2222 if (prop->tp_len <= 0)
2223 {
2224 prop->tp_len = 0;
2225 res.can_drop = droppable;
2226 }
2227 }
2228 else
2229 prop->tp_col += added;
2230 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002231 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2232 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002233 {
2234 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2235
2236 prop->tp_len += after > 0 ? added + after : added;
2237 res.can_drop = prop->tp_len <= 0 && droppable;
2238 }
2239 else
2240 res.dirty = FALSE;
2241
2242 return res;
2243}
2244
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002245/*
2246 * Adjust the columns of text properties in line "lnum" after position "col" to
2247 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002248 * Note that "col" is zero-based, while tp_col is one-based.
2249 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002250 * "flags" can have:
2251 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2252 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002253 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002254 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002255 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002256 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002257 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002258adjust_prop_columns(
2259 linenr_T lnum,
2260 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002261 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002262 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002263{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002264 int proplen;
2265 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002266 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002267 int ri, wi;
2268 size_t textlen;
2269
2270 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002271 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002272
2273 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2274 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002275 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002276 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002277
Bram Moolenaar196d1572019-01-02 23:47:18 +01002278 wi = 0; // write index
2279 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002280 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002281 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002282 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002283
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002284 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2285 res = adjust_prop(&prop, col, bytes_added, flags);
2286 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002287 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002288 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002289 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2290 && u_savesub(lnum) == FAIL)
2291 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002292 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002293
2294 // u_savesub() may have updated curbuf->b_ml, fetch it again
2295 if (curbuf->b_ml.ml_line_lnum != lnum)
2296 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002297 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002298 if (res.can_drop)
2299 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002300 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002301 ++wi;
2302 }
2303 if (dirty)
2304 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002305 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2306
2307 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002308 {
2309 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2310
2311 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2312 vim_free(curbuf->b_ml.ml_line_ptr);
2313 curbuf->b_ml.ml_line_ptr = p;
2314 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002315 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002316 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002317 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002318 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002319}
2320
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002321/*
2322 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002323 * "lnum_props" is the line that has the properties from before the split.
2324 * "lnum_top" is the top line.
2325 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002326 * "deleted" is the number of bytes deleted.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002327 * "at_eol" is true if the split is after the end of the line.
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002328 */
2329 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002330adjust_props_for_split(
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002331 linenr_T lnum_props,
2332 linenr_T lnum_top,
2333 int kept,
2334 int deleted,
2335 int at_eol)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002336{
2337 char_u *props;
2338 int count;
2339 garray_T prevprop;
2340 garray_T nextprop;
2341 int i;
2342 int skipped = kept + deleted;
2343
2344 if (!curbuf->b_has_textprop)
2345 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002346
2347 // Get the text properties from "lnum_props".
2348 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002349 ga_init2(&prevprop, sizeof(textprop_T), 10);
2350 ga_init2(&nextprop, sizeof(textprop_T), 10);
2351
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002352 // Keep the relevant ones in the first line, reducing the length if needed.
2353 // Copy the ones that include the split to the second line.
2354 // Move the ones after the split to the second line.
2355 for (i = 0; i < count; ++i)
2356 {
2357 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002358 proptype_T *pt;
2359 int start_incl, end_incl;
2360 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002361 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002362
2363 // copy the prop to an aligned structure
2364 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2365
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002366 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2367 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2368 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002369
2370 // a text prop "above" behaves like it is on the first text column
2371 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2372
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002373 if (prop_col == MAXCOL)
2374 {
2375 cont_prev = at_eol;
2376 cont_next = !at_eol;
2377 }
2378 else
2379 {
2380 cont_prev = prop_col + !start_incl <= kept;
2381 cont_next = skipped <= prop_col + prop.tp_len - !end_incl;
2382 }
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002383 // when a prop has text it is never copied
2384 if (prop.tp_id < 0 && cont_next)
2385 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002386
2387 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002388 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002389 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2390
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002391 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002392 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002393 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002394 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002395 if (cont_next)
2396 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002397 }
2398
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002399 // Only add the property to the next line if the length is bigger than
2400 // zero.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002401 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002402 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002403 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002404
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002405 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002406 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002407 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002408 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002409 if (p->tp_col > skipped)
2410 p->tp_col -= skipped - 1;
2411 else
2412 {
2413 p->tp_len -= skipped - p->tp_col;
2414 p->tp_col = 1;
2415 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002416 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002417 if (cont_prev)
2418 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002419 }
2420 }
2421
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002422 set_text_props(lnum_top, prevprop.ga_data,
2423 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002424 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002425 set_text_props(lnum_top + 1, nextprop.ga_data,
2426 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002427 ga_clear(&nextprop);
2428}
2429
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002430/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002431 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002432 */
2433 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002434prepend_joined_props(
2435 char_u *new_props,
2436 int propcount,
2437 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002438 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002439 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002440 long col,
2441 int removed)
2442{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002443 char_u *props;
2444 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2445 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002446
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002447 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002448 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002449 textprop_T prop;
2450 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002451
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002452 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002453 if (prop.tp_col == MAXCOL && !last_line)
2454 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002455 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2456
2457 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002458 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002459
Bram Moolenaare175dc62022-08-01 22:18:50 +01002460 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002461 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2462 &prop, sizeof(prop));
2463 else
2464 {
2465 int j;
2466 int found = FALSE;
2467
2468 // Search for continuing prop.
2469 for (j = *props_remaining; j < propcount; ++j)
2470 {
2471 textprop_T op;
2472
2473 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2474 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2475 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002476 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002477 found = TRUE;
2478 op.tp_len += op.tp_col - prop.tp_col;
2479 op.tp_col = prop.tp_col;
2480 // Start/end is taken care of when deleting joined lines
2481 op.tp_flags = prop.tp_flags;
2482 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2483 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002484 }
2485 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002486 if (!found)
2487 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002488 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002489 }
2490}
2491
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002492#endif // FEAT_PROP_POPUP