blob: c6e17951d4836bf8de445a1d481f83f86cf5cc17 [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010017
18/*
19 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
20 * This avoids adding a pointer to the hashtable item.
21 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
22 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
23 * HI2PT() converts a hashitem pointer to a proptype pointer.
24 */
25#define PT2HIKEY(p) ((p)->pt_name)
26#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
27#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
28
29// The global text property types.
30static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010031static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33// The last used text property type ID.
34static int proptype_id = 0;
35
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036/*
37 * Find a property type by name, return the hashitem.
38 * Returns NULL if the item can't be found.
39 */
40 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010041find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010042{
43 hashtab_T *ht;
44 hashitem_T *hi;
45
46 if (*name == NUL)
47 return NULL;
48 if (buf == NULL)
49 ht = global_proptypes;
50 else
51 ht = buf->b_proptypes;
52
53 if (ht == NULL)
54 return NULL;
55 hi = hash_find(ht, name);
56 if (HASHITEM_EMPTY(hi))
57 return NULL;
58 return hi;
59}
60
61/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010062 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010063 */
64 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010065find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010066{
Bram Moolenaare44336b2022-08-07 18:20:08 +010067 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010068
69 if (hi == NULL)
70 return NULL;
71 return HI2PT(hi);
72}
73
74/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020075 * Get the prop type ID of "name".
76 * When not found return zero.
77 */
78 int
79find_prop_type_id(char_u *name, buf_T *buf)
80{
Bram Moolenaare44336b2022-08-07 18:20:08 +010081 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020082
83 if (pt == NULL)
84 return 0;
85 return pt->pt_id;
86}
87
88/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089 * Lookup a property type by name. First in "buf" and when not found in the
90 * global types.
91 * When not found gives an error message and returns NULL.
92 */
93 static proptype_T *
94lookup_prop_type(char_u *name, buf_T *buf)
95{
Bram Moolenaare44336b2022-08-07 18:20:08 +010096 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097
98 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +010099 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 if (type == NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000101 semsg(_(e_property_type_str_does_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100152 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153 return;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200154
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100155 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
156 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157}
158
159/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200160 * Attach a text property 'type_name' to the text starting
161 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100162 * the buffer "buf" and assign identifier "id".
163 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200164 */
165 static int
166prop_add_one(
167 buf_T *buf,
168 char_u *type_name,
169 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100170 char_u *text_arg,
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100171 int text_padding_left,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100172 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200173 linenr_T start_lnum,
174 linenr_T end_lnum,
175 colnr_T start_col,
176 colnr_T end_col)
177{
178 proptype_T *type;
179 linenr_T lnum;
180 int proplen;
181 char_u *props = NULL;
182 char_u *newprops;
183 size_t textlen;
184 char_u *newtext;
185 int i;
186 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100187 char_u *text = text_arg;
188 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200189
190 type = lookup_prop_type(type_name, buf);
191 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200193
194 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
195 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000196 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200198 }
199 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
200 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000201 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200203 }
204
205 if (buf->b_ml.ml_mfp == NULL)
206 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000207 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100208 goto theend;
209 }
210
211 if (text != NULL)
212 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100213 garray_T *gap = &buf->b_textprop_text;
214 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100215
216 // double check we got the right ID
217 if (-id - 1 != gap->ga_len)
218 iemsg("text prop ID mismatch");
219 if (gap->ga_growsize == 0)
220 ga_init2(gap, sizeof(char *), 50);
221 if (ga_grow(gap, 1) == FAIL)
222 goto theend;
223 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100224
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100225 // change any control character (Tab, Newline, etc.) to a Space to make
226 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100227 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100228 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100229 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100230 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200231 }
232
233 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
234 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100235 colnr_T col; // start column use in tp_col
236 colnr_T sort_col; // column where it appears
237 long length; // in bytes
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200238
239 // Fetch the line to get the ml_line_len field updated.
240 proplen = get_text_props(buf, lnum, &props, TRUE);
241 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
242
243 if (lnum == start_lnum)
244 col = start_col;
245 else
246 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100247 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200248 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000249 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100250 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200251 }
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100252 sort_col = col;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200253
254 if (lnum == end_lnum)
255 length = end_col - col;
256 else
257 length = (int)textlen - col + 1;
258 if (length > (long)textlen)
259 length = (int)textlen; // can include the end-of-line
260 if (length < 0)
261 length = 0; // zero-width property
262
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100263 if (text_arg != NULL)
264 {
265 length = 1; // text is placed on one character
266 if (col == 0)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100267 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100268 col = MAXCOL; // before or after the line
269 if ((text_flags & TP_FLAG_ALIGN_ABOVE) == 0)
270 sort_col = MAXCOL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100271 length += text_padding_left;
272 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100273 }
274
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200275 // Allocate the new line with space for the new property.
276 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
277 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100278 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200279 // Copy the text, including terminating NUL.
280 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
281
282 // Find the index where to insert the new property.
283 // Since the text properties are not aligned properly when stored with
284 // the text, we need to copy them as bytes before using it as a struct.
285 for (i = 0; i < proplen; ++i)
286 {
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100287 colnr_T prop_col;
288
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200289 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
290 sizeof(textprop_T));
Bram Moolenaar6eda17d2022-09-12 19:25:11 +0100291 // col is MAXCOL when the text goes above or after the line, when
292 // above we should use column zero for sorting
293 prop_col = (tmp_prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
294 ? 0 : tmp_prop.tp_col;
295 if (prop_col >= sort_col)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200296 break;
297 }
298 newprops = newtext + textlen;
299 if (i > 0)
300 mch_memmove(newprops, props, sizeof(textprop_T) * i);
301
302 tmp_prop.tp_col = col;
303 tmp_prop.tp_len = length;
304 tmp_prop.tp_id = id;
305 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100306 tmp_prop.tp_flags = text_flags
307 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100308 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
309 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
310 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200311 tmp_prop.tp_padleft = text_padding_left;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200312 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
313 sizeof(textprop_T));
314
315 if (i < proplen)
316 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
317 props + i * sizeof(textprop_T),
318 sizeof(textprop_T) * (proplen - i));
319
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100320 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200321 vim_free(buf->b_ml.ml_line_ptr);
322 buf->b_ml.ml_line_ptr = newtext;
323 buf->b_ml.ml_line_len += sizeof(textprop_T);
324 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
325 }
326
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100327 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200328 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100329 res = OK;
330
331theend:
332 vim_free(text);
333 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200334}
335
336/*
337 * prop_add_list()
338 * First argument specifies the text property:
339 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
340 * Second argument is a List where each item is a List with the following
341 * entries: [lnum, start_col, end_col]
342 */
343 void
344f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
345{
346 dict_T *dict;
347 char_u *type_name;
348 buf_T *buf = curbuf;
349 int id = 0;
350 listitem_T *li;
351 list_T *pos_list;
352 linenr_T start_lnum;
353 colnr_T start_col;
354 linenr_T end_lnum;
355 colnr_T end_col;
356 int error = FALSE;
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100357 int prev_did_emsg = did_emsg;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200358
359 if (check_for_dict_arg(argvars, 0) == FAIL
360 || check_for_list_arg(argvars, 1) == FAIL)
361 return;
362
Bram Moolenaard83392a2022-09-01 12:22:46 +0100363 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364 return;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200365
366 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100367 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200368 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000369 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200370 return;
371 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100372 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200373
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100374 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100375 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200376
377 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
378 return;
379
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100380 // This must be done _before_ we start adding properties because property
381 // changes trigger buffer (memline) reorganisation, which needs this flag
382 // to be correctly set.
383 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200384 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
385 {
386 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
387 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000388 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200389 return;
390 }
391
392 pos_list = li->li_tv.vval.v_list;
393 start_lnum = list_find_nr(pos_list, 0L, &error);
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100394 if (!error)
395 start_col = list_find_nr(pos_list, 1L, &error);
396 if (!error)
397 end_lnum = list_find_nr(pos_list, 2L, &error);
398 if (!error)
399 end_col = list_find_nr(pos_list, 3L, &error);
Bram Moolenaard93009e2022-10-13 14:35:24 +0100400 int this_id = id;
401 if (!error && pos_list->lv_len > 4)
402 this_id = list_find_nr(pos_list, 4L, &error);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200403 if (error || start_lnum <= 0 || start_col <= 0
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100404 || end_lnum <= 0 || end_col <= 0)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200405 {
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100406 if (prev_did_emsg == did_emsg)
407 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200408 return;
409 }
Bram Moolenaard93009e2022-10-13 14:35:24 +0100410 if (prop_add_one(buf, type_name, this_id, NULL, 0, 0,
411 start_lnum, end_lnum, start_col, end_col) == FAIL)
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200412 return;
413 }
414
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100415 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200416}
417
418/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100419 * Get the next ID to use for a textprop with text in buffer "buf".
420 */
421 static int
422get_textprop_id(buf_T *buf)
423{
424 // TODO: recycle deleted entries
425 return -(buf->b_textprop_text.ga_len + 1);
426}
427
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000428// Flag that is set when a negative ID isused for a normal text property.
429// It is then impossible to use virtual text properties.
430static int did_use_negative_pop_id = FALSE;
431
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100432/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200433 * Shared between prop_add() and popup_create().
434 * "dict_arg" is the function argument of a dict containing "bufnr".
435 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100436 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200437 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100438 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200439prop_add_common(
440 linenr_T start_lnum,
441 colnr_T start_col,
442 dict_T *dict,
443 buf_T *default_buf,
444 typval_T *dict_arg)
445{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200446 linenr_T end_lnum;
447 colnr_T end_col;
448 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200449 buf_T *buf = default_buf;
450 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100451 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100452 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100453 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100454
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100455 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100456 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000457 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100458 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100459 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100460 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100461
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100462 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100463 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100464 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100465 if (end_lnum < start_lnum)
466 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000467 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100468 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100469 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100470 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 else
472 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100474 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100475 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100476 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100477
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100478 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100479 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000480 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100481 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100482 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100483 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100484 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100485 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100486 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100487 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100488 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100489 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000490 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100491 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100492 }
493 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100494 else if (start_lnum == end_lnum)
495 end_col = start_col;
496 else
497 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100498
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100499 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100500 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100501
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100502 if (dict_has_key(dict, "text"))
503 {
Bram Moolenaarfb593c52022-09-17 18:57:36 +0100504 if (dict_has_key(dict, "length")
505 || dict_has_key(dict, "end_col")
506 || dict_has_key(dict, "end_lnum"))
507 {
508 emsg(_(e_cannot_use_length_endcol_and_endlnum_with_text));
509 goto theend;
510 }
511
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100512 text = dict_get_string(dict, "text", TRUE);
513 if (text == NULL)
514 goto theend;
515 // use a default length of 1 to make multiple props show up
516 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100517
518 if (dict_has_key(dict, "text_align"))
519 {
520 char_u *p = dict_get_string(dict, "text_align", FALSE);
521
522 if (p == NULL)
523 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100524 if (start_col != 0)
525 {
526 emsg(_(e_can_only_use_text_align_when_column_is_zero));
527 goto theend;
528 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100529 if (STRCMP(p, "right") == 0)
530 flags |= TP_FLAG_ALIGN_RIGHT;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100531 else if (STRCMP(p, "above") == 0)
532 flags |= TP_FLAG_ALIGN_ABOVE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100533 else if (STRCMP(p, "below") == 0)
534 flags |= TP_FLAG_ALIGN_BELOW;
535 else if (STRCMP(p, "after") != 0)
536 {
537 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
538 goto theend;
539 }
540 }
541
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100542 if (dict_has_key(dict, "text_padding_left"))
543 {
544 text_padding_left = dict_get_number(dict, "text_padding_left");
545 if (text_padding_left < 0)
546 {
547 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
548 goto theend;
549 }
550 }
551
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100552 if (dict_has_key(dict, "text_wrap"))
553 {
554 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100555
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100556 if (p == NULL)
557 goto theend;
558 if (STRCMP(p, "wrap") == 0)
559 flags |= TP_FLAG_WRAP;
560 else if (STRCMP(p, "truncate") != 0)
561 {
562 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
563 goto theend;
564 }
565 }
566 }
567
568 // Column must be 1 or more for a normal text property; when "text" is
569 // present zero means it goes after the line.
570 if (start_col < (text == NULL ? 1 : 0))
571 {
572 semsg(_(e_invalid_column_number_nr), (long)start_col);
573 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100574 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100575 if (start_col > 0 && text_padding_left > 0)
576 {
577 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
578 goto theend;
579 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100580
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200581 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100582 goto theend;
583
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000584 if (id < 0)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100585 {
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000586 if (buf->b_textprop_text.ga_len > 0)
587 {
588 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
589 goto theend;
590 }
591 did_use_negative_pop_id = TRUE;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100592 }
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000593
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100594 if (text != NULL)
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000595 {
596 if (did_use_negative_pop_id)
597 {
598 emsg(_(e_cannot_add_textprop_with_text_after_using_textprop_with_negative_id));
599 goto theend;
600 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100601 id = get_textprop_id(buf);
Bram Moolenaar4ce1f992022-12-19 13:31:06 +0000602 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100603
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100604 // This must be done _before_ we add the property because property changes
605 // trigger buffer (memline) reorganisation, which needs this flag to be
606 // correctly set.
607 buf->b_has_textprop = TRUE; // this is never reset
608
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100609 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100610 start_lnum, end_lnum, start_col, end_col);
611 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100612
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100613 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100614
615theend:
616 vim_free(text);
617 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100618}
619
620/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100621 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100622 * Returns the number of text properties and, when non-zero, a pointer to the
623 * first one in "props" (note that it is not aligned, therefore the char_u
624 * pointer).
625 */
626 int
627get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
628{
629 char_u *text;
630 size_t textlen;
631 size_t proplen;
632
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100633 // Be quick when no text property types have been defined or the buffer,
634 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200635 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100636 return 0;
637
638 // Fetch the line to get the ml_line_len field updated.
639 text = ml_get_buf(buf, lnum, will_change);
640 textlen = STRLEN(text) + 1;
641 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100642 if (proplen == 0)
643 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100644 if (proplen % sizeof(textprop_T) != 0)
645 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100646 iemsg(e_text_property_info_corrupted);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100647 return 0;
648 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100649 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100650 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100651}
652
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100653/*
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100654 * Return the number of text properties with "above" or "below" alignment in
655 * line "lnum". A "right" aligned property also goes below after a "below" or
656 * other "right" aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100657 */
658 int
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100659prop_count_above_below(buf_T *buf, linenr_T lnum)
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100660{
661 char_u *props;
662 int count = get_text_props(buf, lnum, &props, FALSE);
663 int result = 0;
664 textprop_T prop;
665 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100666 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100667
668 if (count == 0)
669 return 0;
670 for (i = 0; i < count; ++i)
671 {
672 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar89469d12022-12-02 20:46:26 +0000673 if (prop.tp_col == MAXCOL && text_prop_type_valid(buf, &prop))
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100674 {
675 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
676 || (next_right_goes_below
677 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
678 {
679 next_right_goes_below = TRUE;
680 ++result;
681 }
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +0100682 else if (prop.tp_flags & TP_FLAG_ALIGN_ABOVE)
683 {
684 next_right_goes_below = FALSE;
685 ++result;
686 }
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100687 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
688 next_right_goes_below = TRUE;
689 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100690 }
691 return result;
692}
693
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200694/**
695 * Return the number of text properties on line "lnum" in the current buffer.
696 * When "only_starting" is true only text properties starting in this line will
697 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100698 * When "last_line" is FALSE then text properties after the line are not
699 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200700 */
701 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100702count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200703{
704 char_u *props;
705 int proplen = get_text_props(curbuf, lnum, &props, 0);
706 int result = proplen;
707 int i;
708 textprop_T prop;
709
Bram Moolenaare175dc62022-08-01 22:18:50 +0100710 for (i = 0; i < proplen; ++i)
711 {
712 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100713 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100714 // previous line, or when not in the last line and it is virtual text
715 // after the line.
716 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
Bram Moolenaar89469d12022-12-02 20:46:26 +0000717 || (!last_line && prop.tp_col == MAXCOL)
718 || !text_prop_type_valid(curbuf, &prop))
Bram Moolenaare175dc62022-08-01 22:18:50 +0100719 --result;
720 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200721 return result;
722}
723
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100724static textprop_T *text_prop_compare_props;
725static buf_T *text_prop_compare_buf;
726
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000727/*
728 * Score for sorting on position of the text property: 0: above,
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100729 * 1: after (default), 2: right, 3: below (comes last)
730 */
731 static int
732text_prop_order(int flags)
733{
734 if (flags & TP_FLAG_ALIGN_ABOVE)
735 return 0;
736 if (flags & TP_FLAG_ALIGN_RIGHT)
737 return 2;
738 if (flags & TP_FLAG_ALIGN_BELOW)
739 return 3;
740 return 1;
741}
742
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100743/*
744 * Function passed to qsort() to sort text properties.
745 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
746 * both have the same priority.
747 */
748 static int
749text_prop_compare(const void *s1, const void *s2)
750{
751 int idx1, idx2;
752 textprop_T *tp1, *tp2;
753 proptype_T *pt1, *pt2;
754 colnr_T col1, col2;
755
756 idx1 = *(int *)s1;
757 idx2 = *(int *)s2;
758 tp1 = &text_prop_compare_props[idx1];
759 tp2 = &text_prop_compare_props[idx2];
760 col1 = tp1->tp_col;
761 col2 = tp2->tp_col;
762 if (col1 == MAXCOL && col2 == MAXCOL)
763 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100764 int order1 = text_prop_order(tp1->tp_flags);
765 int order2 = text_prop_order(tp2->tp_flags);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100766
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100767 // both props add text before or after the line, sort on order where it
768 // is added
769 if (order1 != order2)
770 return order1 < order2 ? 1 : -1;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100771 }
772
773 // property that inserts text has priority over one that doesn't
774 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
775 return tp1->tp_id < 0 ? 1 : -1;
776
777 // check highest priority, defined by the type
778 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
779 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
780 if (pt1 != pt2)
781 {
782 if (pt1 == NULL)
783 return -1;
784 if (pt2 == NULL)
785 return 1;
786 if (pt1->pt_priority != pt2->pt_priority)
787 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
788 }
789
790 // same priority, one that starts first wins
791 if (col1 != col2)
792 return col1 < col2 ? 1 : -1;
793
794 // for a property with text the id can be used as tie breaker
795 if (tp1->tp_id < 0)
796 return tp1->tp_id > tp2->tp_id ? 1 : -1;
797
798 return 0;
799}
800
801/*
802 * Sort "count" text properties using an array if indexes "idxs" into the list
803 * of text props "props" for buffer "buf".
804 */
805 void
806sort_text_props(
807 buf_T *buf,
808 textprop_T *props,
809 int *idxs,
810 int count)
811{
812 text_prop_compare_buf = buf;
813 text_prop_compare_props = props;
814 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
815}
816
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100817/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200818 * Find text property "type_id" in the visible lines of window "wp".
819 * Match "id" when it is > 0.
820 * Returns FAIL when not found.
821 */
822 int
Bram Moolenaar89469d12022-12-02 20:46:26 +0000823find_visible_prop(
824 win_T *wp,
825 int type_id,
826 int id,
827 textprop_T *prop,
828 linenr_T *found_lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200829{
Bram Moolenaar89469d12022-12-02 20:46:26 +0000830 // return when "type_id" no longer exists
831 if (text_prop_type_by_id(wp->w_buffer, type_id) == NULL)
832 return FAIL;
Bram Moolenaar12034e22019-08-25 22:25:02 +0200833
834 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100835 validate_botline_win(wp);
Bram Moolenaar89469d12022-12-02 20:46:26 +0000836 for (linenr_T lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200837 {
Bram Moolenaar89469d12022-12-02 20:46:26 +0000838 char_u *props;
839 int count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
840 for (int i = 0; i < count; ++i)
Bram Moolenaar12034e22019-08-25 22:25:02 +0200841 {
842 mch_memmove(prop, props + i * sizeof(textprop_T),
843 sizeof(textprop_T));
844 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
845 {
846 *found_lnum = lnum;
847 return OK;
848 }
849 }
850 }
851 return FAIL;
852}
853
854/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100855 * Set the text properties for line "lnum" to "props" with length "len".
856 * If "len" is zero text properties are removed, "props" is not used.
857 * Any existing text properties are dropped.
858 * Only works for the current buffer.
859 */
860 static void
861set_text_props(linenr_T lnum, char_u *props, int len)
862{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100863 char_u *text;
864 char_u *newtext;
865 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100866
867 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100868 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100869 newtext = alloc(textlen + len);
870 if (newtext == NULL)
871 return;
872 mch_memmove(newtext, text, textlen);
873 if (len > 0)
874 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100875 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100876 vim_free(curbuf->b_ml.ml_line_ptr);
877 curbuf->b_ml.ml_line_ptr = newtext;
878 curbuf->b_ml.ml_line_len = textlen + len;
879 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
880}
881
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100882/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100883 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100884 */
885 void
886add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
887{
888 char_u *text;
889 char_u *newtext;
890 int proplen = text_prop_count * (int)sizeof(textprop_T);
891
892 text = ml_get(lnum);
893 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
894 if (newtext == NULL)
895 return;
896 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
897 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
898 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
899 vim_free(curbuf->b_ml.ml_line_ptr);
900 curbuf->b_ml.ml_line_ptr = newtext;
901 curbuf->b_ml.ml_line_len += proplen;
902 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
903}
904
Bram Moolenaare44336b2022-08-07 18:20:08 +0100905/*
906 * Function passed to qsort() for sorting proptype_T on pt_id.
907 */
908 static int
909compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100910{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100911 proptype_T *tp1 = *(proptype_T **)s1;
912 proptype_T *tp2 = *(proptype_T **)s2;
913
914 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
915}
916
917 static proptype_T *
918find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
919{
920 int low = 0;
921 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100922
Bram Moolenaar10246902022-08-08 17:08:05 +0100923 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100924 return NULL;
925
dundargocc57b5bc2022-11-02 13:30:51 +0000926 // Make the lookup faster by creating an array with pointers to
Bram Moolenaare44336b2022-08-07 18:20:08 +0100927 // hashtable entries, sorted on pt_id.
928 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100929 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100930 long todo;
931 hashitem_T *hi;
932 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100933
Bram Moolenaare44336b2022-08-07 18:20:08 +0100934 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
935 if (*array == NULL)
936 return NULL;
937 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000938 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100939 {
940 if (!HASHITEM_EMPTY(hi))
941 {
942 (*array)[i++] = HI2PT(hi);
943 --todo;
944 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100945 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100946 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
947 }
948
949 // binary search in the sorted array
950 high = ht->ht_used;
951 while (high > low)
952 {
953 int m = (high + low) / 2;
954
955 if ((*array)[m]->pt_id == id)
956 return (*array)[m];
957 if ((*array)[m]->pt_id > id)
958 high = m;
959 else
960 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100961 }
962 return NULL;
963}
964
965/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100966 * Fill 'dict' with text properties in 'prop'.
967 */
968 static void
969prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
970{
971 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200972 int buflocal = TRUE;
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200973 int virtualtext_prop = prop->tp_id < 0;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100974
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200975 dict_add_number(dict, "col", (prop->tp_col == MAXCOL) ? 0 : prop->tp_col);
976 if (!virtualtext_prop)
977 {
978 dict_add_number(dict, "length", prop->tp_len);
979 dict_add_number(dict, "id", prop->tp_id);
980 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100981 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
982 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200983
Bram Moolenaare44336b2022-08-07 18:20:08 +0100984 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200985 if (pt == NULL)
986 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100987 pt = find_type_by_id(global_proptypes, &global_proparray,
988 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200989 buflocal = FALSE;
990 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100991 if (pt != NULL)
992 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200993
994 if (buflocal)
995 dict_add_number(dict, "type_bufnr", buf->b_fnum);
996 else
997 dict_add_number(dict, "type_bufnr", 0);
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +0200998 if (virtualtext_prop)
Yegappan Lakshmananf9037f12023-08-20 18:27:45 +0200999 {
1000 // virtual text property
1001 garray_T *gap = &buf->b_textprop_text;
1002 char_u *text;
1003
1004 // negate the property id to get the string index
1005 text = ((char_u **)gap->ga_data)[-prop->tp_id - 1];
1006 dict_add_string(dict, "text", text);
1007
1008 // text_align
1009 char_u *text_align = NULL;
1010 if (prop->tp_flags & TP_FLAG_ALIGN_RIGHT)
1011 text_align = (char_u *)"right";
1012 else if (prop->tp_flags & TP_FLAG_ALIGN_ABOVE)
1013 text_align = (char_u *)"above";
1014 else if (prop->tp_flags & TP_FLAG_ALIGN_BELOW)
1015 text_align = (char_u *)"below";
1016 if (text_align != NULL)
1017 dict_add_string(dict, "text_align", text_align);
1018
1019 // text_wrap
1020 if (prop->tp_flags & TP_FLAG_WRAP)
1021 dict_add_string(dict, "text_wrap", (char_u *)"wrap");
Yegappan Lakshmanan171c5b92023-08-22 21:48:50 +02001022 if (prop->tp_padleft != 0)
1023 dict_add_number(dict, "text_padding_left", prop->tp_padleft);
Yegappan Lakshmananf9037f12023-08-20 18:27:45 +02001024 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001025}
1026
1027/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001028 * Find a property type by ID in "buf" or globally.
1029 * Returns NULL if not found.
1030 */
1031 proptype_T *
1032text_prop_type_by_id(buf_T *buf, int id)
1033{
1034 proptype_T *type;
1035
Bram Moolenaare44336b2022-08-07 18:20:08 +01001036 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001037 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001038 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001039 return type;
1040}
1041
1042/*
Bram Moolenaar89469d12022-12-02 20:46:26 +00001043 * Return TRUE if "prop" is a valid text property type.
1044 */
1045 int
1046text_prop_type_valid(buf_T *buf, textprop_T *prop)
1047{
1048 return text_prop_type_by_id(buf, prop->tp_type) != NULL;
1049}
1050
1051/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001052 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
1053 */
1054 void
1055f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
1056{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001057 linenr_T start;
1058 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001059 linenr_T lnum;
1060 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001061 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001062
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001063 if (in_vim9script()
1064 && (check_for_number_arg(argvars, 0) == FAIL
1065 || check_for_opt_number_arg(argvars, 1) == FAIL
1066 || (argvars[1].v_type != VAR_UNKNOWN
1067 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1068 return;
1069
1070 start = tv_get_number(&argvars[0]);
1071 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001072 if (argvars[1].v_type != VAR_UNKNOWN)
1073 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001074 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001075 if (argvars[2].v_type != VAR_UNKNOWN)
1076 {
1077 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
1078 return;
1079 }
1080 }
1081 if (start < 1 || end < 1)
1082 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001083 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001084 return;
1085 }
1086
1087 for (lnum = start; lnum <= end; ++lnum)
1088 {
1089 char_u *text;
1090 size_t len;
1091
1092 if (lnum > buf->b_ml.ml_line_count)
1093 break;
1094 text = ml_get_buf(buf, lnum, FALSE);
1095 len = STRLEN(text) + 1;
1096 if ((size_t)buf->b_ml.ml_line_len > len)
1097 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001098 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001099 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1100 {
1101 char_u *newtext = vim_strsave(text);
1102
1103 // need to allocate the line now
1104 if (newtext == NULL)
1105 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001106 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1107 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001108 buf->b_ml.ml_line_ptr = newtext;
1109 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1110 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001111 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001112 }
1113 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001114 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001115 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001116}
1117
1118/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001119 * prop_find({props} [, {direction}])
1120 */
1121 void
1122f_prop_find(typval_T *argvars, typval_T *rettv)
1123{
1124 pos_T *cursor = &curwin->w_cursor;
1125 dict_T *dict;
1126 buf_T *buf = curbuf;
1127 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001128 int lnum_start;
1129 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001130 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001131 int id = 0;
1132 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001133 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001134 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001135 int lnum = -1;
1136 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001137 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001138 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001139
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001140 if (in_vim9script()
1141 && (check_for_dict_arg(argvars, 0) == FAIL
1142 || check_for_opt_string_arg(argvars, 1) == FAIL))
1143 return;
1144
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001145 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001146 return;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001147 dict = argvars[0].vval.v_dict;
1148
1149 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1150 return;
1151 if (buf->b_ml.ml_mfp == NULL)
1152 return;
1153
1154 if (argvars[1].v_type != VAR_UNKNOWN)
1155 {
1156 char_u *dir_s = tv_get_string(&argvars[1]);
1157
1158 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001159 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001160 else if (*dir_s != 'f')
1161 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001162 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001163 return;
1164 }
1165 }
1166
1167 di = dict_find(dict, (char_u *)"lnum", -1);
1168 if (di != NULL)
1169 lnum = tv_get_number(&di->di_tv);
1170
1171 di = dict_find(dict, (char_u *)"col", -1);
1172 if (di != NULL)
1173 col = tv_get_number(&di->di_tv);
1174
1175 if (lnum == -1)
1176 {
1177 lnum = cursor->lnum;
1178 col = cursor->col + 1;
1179 }
1180 else if (col == -1)
1181 col = 1;
1182
1183 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1184 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001185 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001186 return;
1187 }
1188
Bram Moolenaard61efa52022-07-23 09:52:04 +01001189 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001190
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001191 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001192 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001193 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001194 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001195 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001196 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001197 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001198 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001199 proptype_T *type = lookup_prop_type(name, buf);
1200
1201 if (type == NULL)
1202 return;
1203 type_id = type->pt_id;
1204 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001205 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001206 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001207 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001208 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001209 return;
1210 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001211 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001212 {
Ben Jacksona7704222022-08-20 20:54:51 +01001213 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001214 return;
1215 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001216
1217 lnum_start = lnum;
1218
1219 if (rettv_dict_alloc(rettv) == FAIL)
1220 return;
1221
1222 while (1)
1223 {
1224 char_u *text = ml_get_buf(buf, lnum, FALSE);
1225 size_t textlen = STRLEN(text) + 1;
1226 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001227 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001228 int i;
1229 textprop_T prop;
1230 int prop_start;
1231 int prop_end;
1232
LemonBoy9bd3ce22022-04-18 21:54:02 +01001233 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001234 {
1235 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001236 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001237
LemonBoy9bd3ce22022-04-18 21:54:02 +01001238 // For the very first line try to find the first property before or
1239 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001240 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001241 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001242 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001243 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001244 if (prop.tp_col > col)
1245 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001246 }
1247 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1248 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001249 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001250 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001251 : (id_found && prop.tp_id == id)
1252 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001253 {
1254 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001255 if (lnum_start == lnum
1256 && col >= prop.tp_col
1257 && (col <= prop.tp_col + prop.tp_len
1258 - (prop.tp_len != 0)))
1259 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001260
LemonBoy9bd3ce22022-04-18 21:54:02 +01001261 // The property was not continued from last line, it starts on
1262 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001263 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001264 // The property does not continue on the next line, it ends on
1265 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001266 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001267 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001268 seen_end = 1;
1269
1270 // Skip lines without the start flag.
1271 if (!prop_start)
1272 {
1273 // Always search backwards for start when search started
1274 // on a prop and we're not skipping.
1275 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001276 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001277 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001278 }
1279
1280 // If skipstart is true, skip the prop at start pos (even if
1281 // continued from another line).
1282 if (start_pos_has_prop && skipstart && !seen_end)
1283 {
1284 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001285 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001286 }
1287
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001288 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1289 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1290
1291 return;
1292 }
1293 }
1294
1295 if (dir > 0)
1296 {
1297 if (lnum >= buf->b_ml.ml_line_count)
1298 break;
1299 lnum++;
1300 }
1301 else
1302 {
1303 if (lnum <= 1)
1304 break;
1305 lnum--;
1306 }
1307 }
1308}
1309
1310/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001311 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1312 */
1313 static int
1314prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1315{
1316 int i;
1317
1318 for (i = 0; i < len; i++)
1319 if (types_or_ids[i] == type_or_id)
1320 return TRUE;
1321
1322 return FALSE;
1323}
1324
1325/*
1326 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1327 * If 'prop_types' is not NULL, then return only the text properties with
1328 * matching property type in the 'prop_types' array.
1329 * If 'prop_ids' is not NULL, then return only the text properties with
1330 * an identifier in the 'props_ids' array.
1331 * If 'add_lnum' is TRUE, then add the line number also to the text property
1332 * dictionary.
1333 */
1334 static void
1335get_props_in_line(
1336 buf_T *buf,
1337 linenr_T lnum,
1338 int *prop_types,
1339 int prop_types_len,
1340 int *prop_ids,
1341 int prop_ids_len,
1342 list_T *retlist,
1343 int add_lnum)
1344{
1345 char_u *text = ml_get_buf(buf, lnum, FALSE);
1346 size_t textlen = STRLEN(text) + 1;
1347 int count;
1348 int i;
1349 textprop_T prop;
1350
1351 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1352 for (i = 0; i < count; ++i)
1353 {
1354 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1355 sizeof(textprop_T));
1356 if ((prop_types == NULL
1357 || prop_type_or_id_in_list(prop_types, prop_types_len,
1358 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001359 && (prop_ids == NULL
1360 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1361 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001362 {
1363 dict_T *d = dict_alloc();
1364
1365 if (d == NULL)
1366 break;
1367 prop_fill_dict(d, &prop, buf);
1368 if (add_lnum)
1369 dict_add_number(d, "lnum", lnum);
1370 list_append_dict(retlist, d);
1371 }
1372 }
1373}
1374
1375/*
1376 * Convert a List of property type names into an array of property type
1377 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1378 * error. 'num_types' is set to the number of returned property types.
1379 */
1380 static int *
1381get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1382{
1383 int *prop_types;
1384 listitem_T *li;
1385 int i;
1386 char_u *name;
1387 proptype_T *type;
1388
1389 *num_types = 0;
1390
1391 prop_types = ALLOC_MULT(int, list_len(l));
1392 if (prop_types == NULL)
1393 return NULL;
1394
1395 i = 0;
1396 FOR_ALL_LIST_ITEMS(l, li)
1397 {
1398 if (li->li_tv.v_type != VAR_STRING)
1399 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001400 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001401 goto errret;
1402 }
1403 name = li->li_tv.vval.v_string;
1404 if (name == NULL)
1405 goto errret;
1406
1407 type = lookup_prop_type(name, buf);
1408 if (type == NULL)
1409 goto errret;
1410 prop_types[i++] = type->pt_id;
1411 }
1412
1413 *num_types = i;
1414 return prop_types;
1415
1416errret:
1417 VIM_CLEAR(prop_types);
1418 return NULL;
1419}
1420
1421/*
1422 * Convert a List of property identifiers into an array of property
1423 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1424 * error. 'num_ids' is set to the number of returned property identifiers.
1425 */
1426 static int *
1427get_prop_ids_from_list(list_T *l, int *num_ids)
1428{
1429 int *prop_ids;
1430 listitem_T *li;
1431 int i;
1432 int id;
1433 int error;
1434
1435 *num_ids = 0;
1436
1437 prop_ids = ALLOC_MULT(int, list_len(l));
1438 if (prop_ids == NULL)
1439 return NULL;
1440
1441 i = 0;
1442 FOR_ALL_LIST_ITEMS(l, li)
1443 {
1444 error = FALSE;
1445 id = tv_get_number_chk(&li->li_tv, &error);
1446 if (error)
1447 goto errret;
1448
1449 prop_ids[i++] = id;
1450 }
1451
1452 *num_ids = i;
1453 return prop_ids;
1454
1455errret:
1456 VIM_CLEAR(prop_ids);
1457 return NULL;
1458}
1459
1460/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001461 * prop_list({lnum} [, {bufnr}])
1462 */
1463 void
1464f_prop_list(typval_T *argvars, typval_T *rettv)
1465{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001466 linenr_T lnum;
1467 linenr_T start_lnum;
1468 linenr_T end_lnum;
1469 buf_T *buf = curbuf;
1470 int add_lnum = FALSE;
1471 int *prop_types = NULL;
1472 int prop_types_len = 0;
1473 int *prop_ids = NULL;
1474 int prop_ids_len = 0;
1475 list_T *l;
1476 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001477
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001478 if (in_vim9script()
1479 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001480 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001481 return;
1482
Bram Moolenaar93a10962022-06-16 11:42:09 +01001483 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001484 return;
1485
1486 // default: get text properties on current line
1487 start_lnum = tv_get_number(&argvars[0]);
1488 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001489 if (argvars[1].v_type != VAR_UNKNOWN)
1490 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001491 dict_T *d;
1492
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001493 if (check_for_dict_arg(argvars, 1) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001494 return;
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001495 d = argvars[1].vval.v_dict;
1496
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001497 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1498 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001499
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001500 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001501 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001502 if (di->di_tv.v_type != VAR_NUMBER)
1503 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001504 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001505 return;
1506 }
1507 end_lnum = tv_get_number(&di->di_tv);
1508 if (end_lnum < 0)
1509 // negative end_lnum is used as an offset from the last buffer
1510 // line
1511 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1512 else if (end_lnum > buf->b_ml.ml_line_count)
1513 end_lnum = buf->b_ml.ml_line_count;
1514 add_lnum = TRUE;
1515 }
1516 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1517 {
1518 if (di->di_tv.v_type != VAR_LIST)
1519 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001520 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001521 return;
1522 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001523
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001524 l = di->di_tv.vval.v_list;
1525 if (l != NULL && list_len(l) > 0)
1526 {
1527 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1528 if (prop_types == NULL)
1529 return;
1530 }
1531 }
1532 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1533 {
1534 if (di->di_tv.v_type != VAR_LIST)
1535 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001536 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001537 goto errret;
1538 }
1539
1540 l = di->di_tv.vval.v_list;
1541 if (l != NULL && list_len(l) > 0)
1542 {
1543 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1544 if (prop_ids == NULL)
1545 goto errret;
1546 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001547 }
1548 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001549 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1550 || end_lnum < 1 || end_lnum < start_lnum)
1551 emsg(_(e_invalid_range));
1552 else
1553 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1554 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1555 prop_ids, prop_ids_len,
1556 rettv->vval.v_list, add_lnum);
1557
1558errret:
1559 VIM_CLEAR(prop_types);
1560 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001561}
1562
1563/*
1564 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1565 */
1566 void
1567f_prop_remove(typval_T *argvars, typval_T *rettv)
1568{
1569 linenr_T start = 1;
1570 linenr_T end = 0;
1571 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001572 linenr_T first_changed = 0;
1573 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001574 dict_T *dict;
1575 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001576 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001577 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001578 int type_id = -1; // for a single "type"
1579 int *type_ids = NULL; // array, for a list of "types", allocated
1580 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001581 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001582 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001583
1584 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001585
1586 if (in_vim9script()
1587 && (check_for_dict_arg(argvars, 0) == FAIL
1588 || check_for_opt_number_arg(argvars, 1) == FAIL
1589 || (argvars[1].v_type != VAR_UNKNOWN
1590 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1591 return;
1592
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001593 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001594 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001595
1596 if (argvars[1].v_type != VAR_UNKNOWN)
1597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001598 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001599 end = start;
1600 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001601 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001602 if (start < 1 || end < 1)
1603 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001604 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001605 return;
1606 }
1607 }
1608
1609 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001610 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1611 return;
1612 if (buf->b_ml.ml_mfp == NULL)
1613 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001614
Bram Moolenaard61efa52022-07-23 09:52:04 +01001615 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001616
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001617 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001618 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001619
1620 // if a specific type was supplied "type": check that (and ignore "types".
1621 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001622 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001623 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001624 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001625 proptype_T *type = lookup_prop_type(name, buf);
1626
1627 if (type == NULL)
1628 return;
1629 type_id = type->pt_id;
1630 }
Ben Jacksona7704222022-08-20 20:54:51 +01001631 if (dict_has_key(dict, "types"))
1632 {
1633 typval_T types;
1634 listitem_T *li = NULL;
1635
1636 dict_get_tv(dict, "types", &types);
1637 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1638 {
1639 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1640
1641 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1642 {
1643 proptype_T *prop_type;
1644
1645 if (li->li_tv.v_type != VAR_STRING)
1646 continue;
1647
1648 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1649
1650 if (!prop_type)
1651 goto cleanup_prop_remove;
1652
1653 type_ids[num_type_ids++] = prop_type->pt_id;
1654 }
1655 }
1656 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001657 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001658
Ben Jacksona7704222022-08-20 20:54:51 +01001659 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001660 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001661 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001662 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001663 }
Ben Jacksona7704222022-08-20 20:54:51 +01001664 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001665 {
Ben Jacksona7704222022-08-20 20:54:51 +01001666 emsg(_(e_need_id_and_type_or_types_with_both));
1667 goto cleanup_prop_remove;
1668 }
1669 if (type_id != -1 && num_type_ids > 0)
1670 {
1671 emsg(_(e_cannot_specify_both_type_and_types));
1672 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001673 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001674
1675 if (end == 0)
1676 end = buf->b_ml.ml_line_count;
1677 for (lnum = start; lnum <= end; ++lnum)
1678 {
1679 char_u *text;
1680 size_t len;
1681
1682 if (lnum > buf->b_ml.ml_line_count)
1683 break;
1684 text = ml_get_buf(buf, lnum, FALSE);
1685 len = STRLEN(text) + 1;
1686 if ((size_t)buf->b_ml.ml_line_len > len)
1687 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001688 static textprop_T textprop; // static because of alignment
1689 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001690
1691 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1692 / sizeof(textprop_T); ++idx)
1693 {
1694 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1695 + idx * sizeof(textprop_T);
1696 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001697 int matches_id = 0;
1698 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001699
1700 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001701
1702 matches_id = textprop.tp_id == id;
1703 if (num_type_ids > 0)
1704 {
1705 int idx2;
1706
1707 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1708 matches_type = textprop.tp_type == type_ids[idx2];
1709 }
1710 else
1711 {
1712 matches_type = textprop.tp_type == type_id;
1713 }
1714
1715 if (both ? matches_id && matches_type
1716 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001717 {
1718 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1719 {
1720 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1721
1722 // need to allocate the line to be able to change it
1723 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001724 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001725 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1726 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001727 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1728 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001729 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001730 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1731
1732 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001733 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001734 }
1735
1736 taillen = buf->b_ml.ml_line_len - len
1737 - (idx + 1) * sizeof(textprop_T);
1738 if (taillen > 0)
1739 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1740 taillen);
1741 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1742 --idx;
1743
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001744 if (textprop.tp_id < 0)
1745 {
1746 garray_T *gap = &buf->b_textprop_text;
1747 int ii = -textprop.tp_id - 1;
1748
1749 // negative ID: property with text - free the text
1750 if (ii < gap->ga_len)
1751 {
1752 char_u **p = ((char_u **)gap->ga_data) + ii;
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00001753 VIM_CLEAR(*p);
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001754 did_remove_text = TRUE;
1755 }
1756 }
1757
Bram Moolenaar965c0442021-05-17 00:22:06 +02001758 if (first_changed == 0)
1759 first_changed = lnum;
1760 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001761 ++rettv->vval.v_number;
1762 if (!do_all)
1763 break;
1764 }
1765 }
1766 }
1767 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001768
Bram Moolenaar965c0442021-05-17 00:22:06 +02001769 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001770 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001771 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001772 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001773 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001774 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001775
1776 if (did_remove_text)
1777 {
1778 garray_T *gap = &buf->b_textprop_text;
1779
1780 // Reduce the growarray size for NULL pointers at the end.
1781 while (gap->ga_len > 0
1782 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1783 --gap->ga_len;
1784 }
Ben Jacksona7704222022-08-20 20:54:51 +01001785
1786cleanup_prop_remove:
1787 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001788}
1789
1790/*
1791 * Common for f_prop_type_add() and f_prop_type_change().
1792 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001793 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001794prop_type_set(typval_T *argvars, int add)
1795{
1796 char_u *name;
1797 buf_T *buf = NULL;
1798 dict_T *dict;
1799 dictitem_T *di;
1800 proptype_T *prop;
1801
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001802 if (in_vim9script()
1803 && (check_for_string_arg(argvars, 0) == FAIL
1804 || check_for_dict_arg(argvars, 1) == FAIL))
1805 return;
1806
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001807 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001808 if (*name == NUL)
1809 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001810 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001811 return;
1812 }
1813
1814 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1815 return;
1816 dict = argvars[1].vval.v_dict;
1817
Bram Moolenaare44336b2022-08-07 18:20:08 +01001818 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001819 if (add)
1820 {
1821 hashtab_T **htp;
1822
1823 if (prop != NULL)
1824 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001825 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001826 return;
1827 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001828 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001829 if (prop == NULL)
1830 return;
1831 STRCPY(prop->pt_name, name);
1832 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001833 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001834 if (buf == NULL)
1835 {
1836 htp = &global_proptypes;
1837 VIM_CLEAR(global_proparray);
1838 }
1839 else
1840 {
1841 htp = &buf->b_proptypes;
1842 VIM_CLEAR(buf->b_proparray);
1843 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001844 if (*htp == NULL)
1845 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001846 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001847 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001848 {
1849 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001850 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001851 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001852 hash_init(*htp);
1853 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001854 hash_add(*htp, PT2HIKEY(prop), "prop type");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001855 }
1856 else
1857 {
1858 if (prop == NULL)
1859 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001860 semsg(_(e_property_type_str_does_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001861 return;
1862 }
1863 }
1864
1865 if (dict != NULL)
1866 {
1867 di = dict_find(dict, (char_u *)"highlight", -1);
1868 if (di != NULL)
1869 {
1870 char_u *highlight;
1871 int hl_id = 0;
1872
Bram Moolenaard61efa52022-07-23 09:52:04 +01001873 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001874 if (highlight != NULL && *highlight != NUL)
1875 hl_id = syn_name2id(highlight);
1876 if (hl_id <= 0)
1877 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001878 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001879 highlight == NULL ? (char_u *)"" : highlight);
1880 return;
1881 }
1882 prop->pt_hl_id = hl_id;
1883 }
1884
Bram Moolenaar58187f12019-05-05 16:33:47 +02001885 di = dict_find(dict, (char_u *)"combine", -1);
1886 if (di != NULL)
1887 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001888 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001889 prop->pt_flags |= PT_FLAG_COMBINE;
1890 else
1891 prop->pt_flags &= ~PT_FLAG_COMBINE;
1892 }
1893
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001894 di = dict_find(dict, (char_u *)"override", -1);
1895 if (di != NULL)
1896 {
1897 if (tv_get_bool(&di->di_tv))
1898 prop->pt_flags |= PT_FLAG_OVERRIDE;
1899 else
1900 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1901 }
1902
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001903 di = dict_find(dict, (char_u *)"priority", -1);
1904 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001905 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001906
1907 di = dict_find(dict, (char_u *)"start_incl", -1);
1908 if (di != NULL)
1909 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001910 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001911 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1912 else
1913 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1914 }
1915
1916 di = dict_find(dict, (char_u *)"end_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_END_INCL;
1921 else
1922 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1923 }
1924 }
1925}
1926
1927/*
1928 * prop_type_add({name}, {props})
1929 */
1930 void
1931f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1932{
1933 prop_type_set(argvars, TRUE);
1934}
1935
1936/*
1937 * prop_type_change({name}, {props})
1938 */
1939 void
1940f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1941{
1942 prop_type_set(argvars, FALSE);
1943}
1944
1945/*
1946 * prop_type_delete({name} [, {bufnr}])
1947 */
1948 void
1949f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1950{
1951 char_u *name;
1952 buf_T *buf = NULL;
1953 hashitem_T *hi;
1954
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001955 if (in_vim9script()
1956 && (check_for_string_arg(argvars, 0) == FAIL
1957 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1958 return;
1959
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001960 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001961 if (*name == NUL)
1962 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00001963 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001964 return;
1965 }
1966
1967 if (argvars[1].v_type != VAR_UNKNOWN)
1968 {
1969 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1970 return;
1971 }
1972
Bram Moolenaare44336b2022-08-07 18:20:08 +01001973 hi = find_prop_type_hi(name, buf);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001974 if (hi == NULL)
1975 return;
1976
1977 hashtab_T *ht;
1978 proptype_T *prop = HI2PT(hi);
1979
1980 if (buf == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001981 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001982 ht = global_proptypes;
1983 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001984 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001985 else
1986 {
1987 ht = buf->b_proptypes;
1988 VIM_CLEAR(buf->b_proparray);
1989 }
1990 hash_remove(ht, hi, "prop type delete");
1991 vim_free(prop);
1992
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001993 // currently visible text properties will disappear
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001994 redraw_all_later(UPD_CLEAR);
1995 changed_window_setting_buf(buf == NULL ? curbuf : buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001996}
1997
1998/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001999 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002000 */
2001 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01002002f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002003{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002004 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002005
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002006 if (in_vim9script()
2007 && (check_for_string_arg(argvars, 0) == FAIL
2008 || check_for_opt_dict_arg(argvars, 1) == FAIL))
2009 return;
2010
2011 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002012 if (*name == NUL)
2013 {
Bram Moolenaar89469d12022-12-02 20:46:26 +00002014 semsg(_(e_invalid_argument_str), "\"\"");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002015 return;
2016 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002017
2018 if (rettv_dict_alloc(rettv) == FAIL)
2019 return;
2020
2021 proptype_T *prop = NULL;
2022 buf_T *buf = NULL;
2023
2024 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002025 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002026 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
2027 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002028 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002029
2030 prop = find_prop_type(name, buf);
2031 if (prop == NULL)
2032 return;
2033
2034 dict_T *d = rettv->vval.v_dict;
2035
2036 if (prop->pt_hl_id > 0)
2037 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
2038 dict_add_number(d, "priority", prop->pt_priority);
2039 dict_add_number(d, "combine",
2040 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
2041 dict_add_number(d, "start_incl",
2042 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
2043 dict_add_number(d, "end_incl",
2044 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
2045 if (buf != NULL)
2046 dict_add_number(d, "bufnr", buf->b_fnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002047}
2048
2049 static void
2050list_types(hashtab_T *ht, list_T *l)
2051{
2052 long todo;
2053 hashitem_T *hi;
2054
2055 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002056 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002057 {
2058 if (!HASHITEM_EMPTY(hi))
2059 {
2060 proptype_T *prop = HI2PT(hi);
2061
2062 list_append_string(l, prop->pt_name, -1);
2063 --todo;
2064 }
2065 }
2066}
2067
2068/*
2069 * prop_type_list([{bufnr}])
2070 */
2071 void
2072f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
2073{
2074 buf_T *buf = NULL;
2075
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002076 if (rettv_list_alloc(rettv) == FAIL)
2077 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002078
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002079 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
2080 return;
2081
2082 if (argvars[0].v_type != VAR_UNKNOWN)
2083 {
2084 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
2085 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002086 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002087 if (buf == NULL)
2088 {
2089 if (global_proptypes != NULL)
2090 list_types(global_proptypes, rettv->vval.v_list);
2091 }
2092 else if (buf->b_proptypes != NULL)
2093 list_types(buf->b_proptypes, rettv->vval.v_list);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002094}
2095
2096/*
2097 * Free all property types in "ht".
2098 */
2099 static void
2100clear_ht_prop_types(hashtab_T *ht)
2101{
2102 long todo;
2103 hashitem_T *hi;
2104
2105 if (ht == NULL)
2106 return;
2107
2108 todo = (long)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002109 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002110 {
2111 if (!HASHITEM_EMPTY(hi))
2112 {
2113 proptype_T *prop = HI2PT(hi);
2114
2115 vim_free(prop);
2116 --todo;
2117 }
2118 }
2119
2120 hash_clear(ht);
2121 vim_free(ht);
2122}
2123
2124#if defined(EXITFREE) || defined(PROTO)
2125/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002126 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002127 */
2128 void
2129clear_global_prop_types(void)
2130{
2131 clear_ht_prop_types(global_proptypes);
2132 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002133 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002134}
2135#endif
2136
2137/*
2138 * Free all property types for "buf".
2139 */
2140 void
2141clear_buf_prop_types(buf_T *buf)
2142{
2143 clear_ht_prop_types(buf->b_proptypes);
2144 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002145 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002146}
2147
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002148// Struct used to return two values from adjust_prop().
2149typedef struct
2150{
2151 int dirty; // if the property was changed
2152 int can_drop; // whether after this change, the prop may be removed
2153} adjustres_T;
2154
2155/*
2156 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2157 *
2158 * Note that "col" is zero-based, while tp_col is one-based.
2159 * Only for the current buffer.
2160 * "flags" can have:
2161 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002162 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002163 */
2164 static adjustres_T
2165adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002166 textprop_T *prop,
2167 colnr_T col,
2168 int added,
2169 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002170{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002171 proptype_T *pt;
2172 int start_incl;
2173 int end_incl;
2174 int droppable;
2175 adjustres_T res = {TRUE, FALSE};
2176
2177 // prop after end of the line doesn't move
2178 if (prop->tp_col == MAXCOL)
2179 {
2180 res.dirty = FALSE;
2181 return res;
2182 }
2183
2184 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2185 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002186 || (flags & APC_SUBSTITUTE)
2187 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002188 if (prop->tp_id < 0 && (flags & APC_INDENT))
2189 // when inserting indent just before a character with virtual text
2190 // shift the text property
2191 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002192 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002193 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002194 // do not drop zero-width props if they later can increase in size
2195 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002196
2197 if (added > 0)
2198 {
2199 if (col + 1 <= prop->tp_col
2200 - (start_incl || (prop->tp_len == 0 && end_incl)))
2201 // Change is entirely before the text property: Only shift
2202 prop->tp_col += added;
2203 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2204 // Insertion was inside text property
2205 prop->tp_len += added;
2206 }
2207 else if (prop->tp_col > col + 1)
2208 {
2209 if (prop->tp_col + added < col + 1)
2210 {
2211 prop->tp_len += (prop->tp_col - 1 - col) + added;
2212 prop->tp_col = col + 1;
2213 if (prop->tp_len <= 0)
2214 {
2215 prop->tp_len = 0;
2216 res.can_drop = droppable;
2217 }
2218 }
2219 else
2220 prop->tp_col += added;
2221 }
Bram Moolenaarf5240b92022-08-24 12:24:37 +01002222 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col
2223 && prop->tp_id >= 0) // don't change length for virtual text
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002224 {
2225 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2226
2227 prop->tp_len += after > 0 ? added + after : added;
2228 res.can_drop = prop->tp_len <= 0 && droppable;
2229 }
2230 else
2231 res.dirty = FALSE;
2232
2233 return res;
2234}
2235
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002236/*
2237 * Adjust the columns of text properties in line "lnum" after position "col" to
2238 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002239 * Note that "col" is zero-based, while tp_col is one-based.
2240 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002241 * "flags" can have:
2242 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2243 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002244 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002245 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002246 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002247 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002248 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002249adjust_prop_columns(
2250 linenr_T lnum,
2251 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002252 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002253 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002254{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002255 int proplen;
2256 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002257 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002258 int ri, wi;
2259 size_t textlen;
2260
2261 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002262 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002263
2264 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2265 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002266 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002267 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002268
Bram Moolenaar196d1572019-01-02 23:47:18 +01002269 wi = 0; // write index
2270 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002271 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002272 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002273 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002274
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002275 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2276 res = adjust_prop(&prop, col, bytes_added, flags);
2277 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002278 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002279 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002280 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2281 && u_savesub(lnum) == FAIL)
2282 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002283 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002284
2285 // u_savesub() may have updated curbuf->b_ml, fetch it again
2286 if (curbuf->b_ml.ml_line_lnum != lnum)
2287 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002288 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002289 if (res.can_drop)
2290 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002291 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002292 ++wi;
2293 }
2294 if (dirty)
2295 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002296 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2297
2298 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002299 {
2300 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2301
2302 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2303 vim_free(curbuf->b_ml.ml_line_ptr);
2304 curbuf->b_ml.ml_line_ptr = p;
2305 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002306 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002307 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002308 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002309 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002310}
2311
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002312/*
2313 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002314 * "lnum_props" is the line that has the properties from before the split.
2315 * "lnum_top" is the top line.
2316 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002317 * "deleted" is the number of bytes deleted.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002318 * "at_eol" is true if the split is after the end of the line.
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002319 */
2320 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002321adjust_props_for_split(
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002322 linenr_T lnum_props,
2323 linenr_T lnum_top,
2324 int kept,
2325 int deleted,
2326 int at_eol)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002327{
2328 char_u *props;
2329 int count;
2330 garray_T prevprop;
2331 garray_T nextprop;
2332 int i;
2333 int skipped = kept + deleted;
2334
2335 if (!curbuf->b_has_textprop)
2336 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002337
2338 // Get the text properties from "lnum_props".
2339 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002340 ga_init2(&prevprop, sizeof(textprop_T), 10);
2341 ga_init2(&nextprop, sizeof(textprop_T), 10);
2342
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002343 // Keep the relevant ones in the first line, reducing the length if needed.
2344 // Copy the ones that include the split to the second line.
2345 // Move the ones after the split to the second line.
2346 for (i = 0; i < count; ++i)
2347 {
2348 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002349 proptype_T *pt;
2350 int start_incl, end_incl;
2351 int cont_prev, cont_next;
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002352 int prop_col;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002353
2354 // copy the prop to an aligned structure
2355 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2356
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002357 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2358 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2359 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002360
2361 // a text prop "above" behaves like it is on the first text column
2362 prop_col = (prop.tp_flags & TP_FLAG_ALIGN_ABOVE) ? 1 : prop.tp_col;
2363
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002364 if (prop_col == MAXCOL)
2365 {
2366 cont_prev = at_eol;
2367 cont_next = !at_eol;
2368 }
2369 else
2370 {
2371 cont_prev = prop_col + !start_incl <= kept;
2372 cont_next = skipped <= prop_col + prop.tp_len - !end_incl;
2373 }
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002374 // when a prop has text it is never copied
2375 if (prop.tp_id < 0 && cont_next)
2376 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002377
2378 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002379 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002380 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2381
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002382 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002383 ++prevprop.ga_len;
Bram Moolenaar98b37302022-09-14 12:06:53 +01002384 if (p->tp_col != MAXCOL && p->tp_col + p->tp_len >= kept)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002385 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002386 if (cont_next)
2387 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002388 }
2389
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002390 // Only add the property to the next line if the length is bigger than
2391 // zero.
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002392 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002393 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002394 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002395
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002396 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002397 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002398 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002399 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002400 if (p->tp_col > skipped)
2401 p->tp_col -= skipped - 1;
2402 else
2403 {
2404 p->tp_len -= skipped - p->tp_col;
2405 p->tp_col = 1;
2406 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002407 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002408 if (cont_prev)
2409 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002410 }
2411 }
2412
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002413 set_text_props(lnum_top, prevprop.ga_data,
2414 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002415 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002416 set_text_props(lnum_top + 1, nextprop.ga_data,
2417 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002418 ga_clear(&nextprop);
2419}
2420
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002421/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002422 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002423 */
2424 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002425prepend_joined_props(
2426 char_u *new_props,
2427 int propcount,
2428 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002429 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002430 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002431 long col,
2432 int removed)
2433{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002434 char_u *props;
2435 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2436 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002437
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002438 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002439 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002440 textprop_T prop;
2441 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002442
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002443 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002444 if (prop.tp_col == MAXCOL && !last_line)
2445 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002446 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2447
2448 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002449 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002450
Bram Moolenaare175dc62022-08-01 22:18:50 +01002451 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002452 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2453 &prop, sizeof(prop));
2454 else
2455 {
2456 int j;
2457 int found = FALSE;
2458
2459 // Search for continuing prop.
2460 for (j = *props_remaining; j < propcount; ++j)
2461 {
2462 textprop_T op;
2463
2464 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2465 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2466 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002467 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002468 found = TRUE;
2469 op.tp_len += op.tp_col - prop.tp_col;
2470 op.tp_col = prop.tp_col;
2471 // Start/end is taken care of when deleting joined lines
2472 op.tp_flags = prop.tp_flags;
2473 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2474 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002475 }
2476 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002477 if (!found)
2478 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002479 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002480 }
2481}
2482
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002483#endif // FEAT_PROP_POPUP