blob: 31e1be3bbf03eebec4fbda3e6f474d804499b5c5 [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 */
13
14#include "vim.h"
15
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010016#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010017
18/*
19 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
20 * This avoids adding a pointer to the hashtable item.
21 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
22 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
23 * HI2PT() converts a hashitem pointer to a proptype pointer.
24 */
25#define PT2HIKEY(p) ((p)->pt_name)
26#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
27#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
28
29// The global text property types.
30static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010031static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33// The last used text property type ID.
34static int proptype_id = 0;
35
Bram Moolenaar98aefe72018-12-13 22:20:09 +010036/*
37 * Find a property type by name, return the hashitem.
38 * Returns NULL if the item can't be found.
39 */
40 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010041find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010042{
43 hashtab_T *ht;
44 hashitem_T *hi;
45
46 if (*name == NUL)
47 return NULL;
48 if (buf == NULL)
49 ht = global_proptypes;
50 else
51 ht = buf->b_proptypes;
52
53 if (ht == NULL)
54 return NULL;
55 hi = hash_find(ht, name);
56 if (HASHITEM_EMPTY(hi))
57 return NULL;
58 return hi;
59}
60
61/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010062 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010063 */
64 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010065find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010066{
Bram Moolenaare44336b2022-08-07 18:20:08 +010067 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010068
69 if (hi == NULL)
70 return NULL;
71 return HI2PT(hi);
72}
73
74/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020075 * Get the prop type ID of "name".
76 * When not found return zero.
77 */
78 int
79find_prop_type_id(char_u *name, buf_T *buf)
80{
Bram Moolenaare44336b2022-08-07 18:20:08 +010081 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020082
83 if (pt == NULL)
84 return 0;
85 return pt->pt_id;
86}
87
88/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089 * Lookup a property type by name. First in "buf" and when not found in the
90 * global types.
91 * When not found gives an error message and returns NULL.
92 */
93 static proptype_T *
94lookup_prop_type(char_u *name, buf_T *buf)
95{
Bram Moolenaare44336b2022-08-07 18:20:08 +010096 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097
98 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +010099 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100101 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 return type;
103}
104
105/*
106 * Get an optional "bufnr" item from the dict in "arg".
107 * When the argument is not used or "bufnr" is not present then "buf" is
108 * unchanged.
109 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100110 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 */
112 static int
113get_bufnr_from_arg(typval_T *arg, buf_T **buf)
114{
115 dictitem_T *di;
116
117 if (arg->v_type != VAR_DICT)
118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000119 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return FAIL;
121 }
122 if (arg->vval.v_dict == NULL)
123 return OK; // NULL dict is like an empty dict
124 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200125 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
126 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 if (*buf == NULL)
130 return FAIL;
131 }
132 return OK;
133}
134
135/*
136 * prop_add({lnum}, {col}, {props})
137 */
138 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100139f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100140{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100141 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100142 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200144 if (in_vim9script()
145 && (check_for_number_arg(argvars, 0) == FAIL
146 || check_for_number_arg(argvars, 1) == FAIL
147 || check_for_dict_arg(argvars, 2) == FAIL))
148 return;
149
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 start_lnum = tv_get_number(&argvars[0]);
151 start_col = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100152 if (argvars[2].v_type != VAR_DICT)
153 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000154 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100155 return;
156 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200157
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100158 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
159 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200160}
161
162/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200163 * Attach a text property 'type_name' to the text starting
164 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100165 * the buffer "buf" and assign identifier "id".
166 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200167 */
168 static int
169prop_add_one(
170 buf_T *buf,
171 char_u *type_name,
172 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100173 char_u *text_arg,
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100174 int text_padding_left,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100175 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200176 linenr_T start_lnum,
177 linenr_T end_lnum,
178 colnr_T start_col,
179 colnr_T end_col)
180{
181 proptype_T *type;
182 linenr_T lnum;
183 int proplen;
184 char_u *props = NULL;
185 char_u *newprops;
186 size_t textlen;
187 char_u *newtext;
188 int i;
189 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100190 char_u *text = text_arg;
191 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200192
193 type = lookup_prop_type(type_name, buf);
194 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100195 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200196
197 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
198 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000199 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100200 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200201 }
202 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
203 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000204 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100205 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200206 }
207
208 if (buf->b_ml.ml_mfp == NULL)
209 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000210 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100211 goto theend;
212 }
213
214 if (text != NULL)
215 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100216 garray_T *gap = &buf->b_textprop_text;
217 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100218
219 // double check we got the right ID
220 if (-id - 1 != gap->ga_len)
221 iemsg("text prop ID mismatch");
222 if (gap->ga_growsize == 0)
223 ga_init2(gap, sizeof(char *), 50);
224 if (ga_grow(gap, 1) == FAIL)
225 goto theend;
226 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100227
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100228 // change any control character (Tab, Newline, etc.) to a Space to make
229 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100230 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100231 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100232 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100233 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200234 }
235
236 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
237 {
238 colnr_T col; // start column
239 long length; // in bytes
240
241 // Fetch the line to get the ml_line_len field updated.
242 proplen = get_text_props(buf, lnum, &props, TRUE);
243 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
244
245 if (lnum == start_lnum)
246 col = start_col;
247 else
248 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100249 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200250 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000251 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100252 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200253 }
254
255 if (lnum == end_lnum)
256 length = end_col - col;
257 else
258 length = (int)textlen - col + 1;
259 if (length > (long)textlen)
260 length = (int)textlen; // can include the end-of-line
261 if (length < 0)
262 length = 0; // zero-width property
263
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100264 if (text_arg != NULL)
265 {
266 length = 1; // text is placed on one character
267 if (col == 0)
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100268 {
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100269 col = MAXCOL; // after the line
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100270 length += text_padding_left;
271 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100272 }
273
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200274 // Allocate the new line with space for the new property.
275 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
276 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100277 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200278 // Copy the text, including terminating NUL.
279 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
280
281 // Find the index where to insert the new property.
282 // Since the text properties are not aligned properly when stored with
283 // the text, we need to copy them as bytes before using it as a struct.
284 for (i = 0; i < proplen; ++i)
285 {
286 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
287 sizeof(textprop_T));
288 if (tmp_prop.tp_col >= col)
289 break;
290 }
291 newprops = newtext + textlen;
292 if (i > 0)
293 mch_memmove(newprops, props, sizeof(textprop_T) * i);
294
295 tmp_prop.tp_col = col;
296 tmp_prop.tp_len = length;
297 tmp_prop.tp_id = id;
298 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100299 tmp_prop.tp_flags = text_flags
300 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
Bram Moolenaar28c9f892022-08-14 13:28:55 +0100301 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0)
302 | ((type->pt_flags & PT_FLAG_INS_START_INCL)
303 ? TP_FLAG_START_INCL : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200304 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
305 sizeof(textprop_T));
306
307 if (i < proplen)
308 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
309 props + i * sizeof(textprop_T),
310 sizeof(textprop_T) * (proplen - i));
311
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100312 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200313 vim_free(buf->b_ml.ml_line_ptr);
314 buf->b_ml.ml_line_ptr = newtext;
315 buf->b_ml.ml_line_len += sizeof(textprop_T);
316 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
317 }
318
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100319 changed_line_display_buf(buf);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200320 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100321 res = OK;
322
323theend:
324 vim_free(text);
325 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200326}
327
328/*
329 * prop_add_list()
330 * First argument specifies the text property:
331 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
332 * Second argument is a List where each item is a List with the following
333 * entries: [lnum, start_col, end_col]
334 */
335 void
336f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
337{
338 dict_T *dict;
339 char_u *type_name;
340 buf_T *buf = curbuf;
341 int id = 0;
342 listitem_T *li;
343 list_T *pos_list;
344 linenr_T start_lnum;
345 colnr_T start_col;
346 linenr_T end_lnum;
347 colnr_T end_col;
348 int error = FALSE;
349
350 if (check_for_dict_arg(argvars, 0) == FAIL
351 || check_for_list_arg(argvars, 1) == FAIL)
352 return;
353
354 if (argvars[1].vval.v_list == NULL)
355 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000356 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200357 return;
358 }
359
360 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100361 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200362 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000363 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200364 return;
365 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100366 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200367
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100368 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100369 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200370
371 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
372 return;
373
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100374 // This must be done _before_ we start adding properties because property
375 // changes trigger buffer (memline) reorganisation, which needs this flag
376 // to be correctly set.
377 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200378 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
379 {
380 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
381 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000382 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200383 return;
384 }
385
386 pos_list = li->li_tv.vval.v_list;
387 start_lnum = list_find_nr(pos_list, 0L, &error);
388 start_col = list_find_nr(pos_list, 1L, &error);
389 end_lnum = list_find_nr(pos_list, 2L, &error);
390 end_col = list_find_nr(pos_list, 3L, &error);
391 if (error || start_lnum <= 0 || start_col <= 0
392 || end_lnum <= 0 || end_col <= 0)
393 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000394 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200395 return;
396 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100397 if (prop_add_one(buf, type_name, id, NULL, 0, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200398 start_col, end_col) == FAIL)
399 return;
400 }
401
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100402 redraw_buf_later(buf, UPD_VALID);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200403}
404
405/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100406 * Get the next ID to use for a textprop with text in buffer "buf".
407 */
408 static int
409get_textprop_id(buf_T *buf)
410{
411 // TODO: recycle deleted entries
412 return -(buf->b_textprop_text.ga_len + 1);
413}
414
415/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200416 * Shared between prop_add() and popup_create().
417 * "dict_arg" is the function argument of a dict containing "bufnr".
418 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100419 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200420 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100421 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200422prop_add_common(
423 linenr_T start_lnum,
424 colnr_T start_col,
425 dict_T *dict,
426 buf_T *default_buf,
427 typval_T *dict_arg)
428{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200429 linenr_T end_lnum;
430 colnr_T end_col;
431 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200432 buf_T *buf = default_buf;
433 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100434 char_u *text = NULL;
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100435 int text_padding_left = 0;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100436 int flags = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100437
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100438 if (dict == NULL || !dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100439 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000440 emsg(_(e_missing_property_type_name));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100441 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100442 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100443 type_name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100444
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100445 if (dict_has_key(dict, "end_lnum"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100446 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100447 end_lnum = dict_get_number(dict, "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100448 if (end_lnum < start_lnum)
449 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000450 semsg(_(e_invalid_value_for_argument_str), "end_lnum");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100451 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100452 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100453 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100454 else
455 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100456
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100457 if (dict_has_key(dict, "length"))
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100458 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100459 long length = dict_get_number(dict, "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100460
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100461 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100462 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000463 semsg(_(e_invalid_value_for_argument_str), "length");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100464 goto theend;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100465 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100466 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100467 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100468 else if (dict_has_key(dict, "end_col"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100470 end_col = dict_get_number(dict, "end_col");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100471 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100472 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000473 semsg(_(e_invalid_value_for_argument_str), "end_col");
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100474 goto theend;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100475 }
476 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100477 else if (start_lnum == end_lnum)
478 end_col = start_col;
479 else
480 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100481
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100482 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100483 id = dict_get_number(dict, "id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100484
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100485 if (dict_has_key(dict, "text"))
486 {
487 text = dict_get_string(dict, "text", TRUE);
488 if (text == NULL)
489 goto theend;
490 // use a default length of 1 to make multiple props show up
491 end_col = start_col + 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100492
493 if (dict_has_key(dict, "text_align"))
494 {
495 char_u *p = dict_get_string(dict, "text_align", FALSE);
496
497 if (p == NULL)
498 goto theend;
Bram Moolenaar82b14c12022-08-10 19:50:47 +0100499 if (start_col != 0)
500 {
501 emsg(_(e_can_only_use_text_align_when_column_is_zero));
502 goto theend;
503 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100504 if (STRCMP(p, "right") == 0)
505 flags |= TP_FLAG_ALIGN_RIGHT;
506 else if (STRCMP(p, "below") == 0)
507 flags |= TP_FLAG_ALIGN_BELOW;
508 else if (STRCMP(p, "after") != 0)
509 {
510 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
511 goto theend;
512 }
513 }
514
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100515 if (dict_has_key(dict, "text_padding_left"))
516 {
517 text_padding_left = dict_get_number(dict, "text_padding_left");
518 if (text_padding_left < 0)
519 {
520 semsg(_(e_argument_must_be_positive_str), "text_padding_left");
521 goto theend;
522 }
523 }
524
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100525 if (dict_has_key(dict, "text_wrap"))
526 {
527 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100528
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100529 if (p == NULL)
530 goto theend;
531 if (STRCMP(p, "wrap") == 0)
532 flags |= TP_FLAG_WRAP;
533 else if (STRCMP(p, "truncate") != 0)
534 {
535 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
536 goto theend;
537 }
538 }
539 }
540
541 // Column must be 1 or more for a normal text property; when "text" is
542 // present zero means it goes after the line.
543 if (start_col < (text == NULL ? 1 : 0))
544 {
545 semsg(_(e_invalid_column_number_nr), (long)start_col);
546 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100547 }
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100548 if (start_col > 0 && text_padding_left > 0)
549 {
550 emsg(_(e_can_only_use_left_padding_when_column_is_zero));
551 goto theend;
552 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100553
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200554 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100555 goto theend;
556
557 if (id < 0 && buf->b_textprop_text.ga_len > 0)
558 {
559 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
560 goto theend;
561 }
562 if (text != NULL)
563 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100564
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100565 // This must be done _before_ we add the property because property changes
566 // trigger buffer (memline) reorganisation, which needs this flag to be
567 // correctly set.
568 buf->b_has_textprop = TRUE; // this is never reset
569
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100570 prop_add_one(buf, type_name, id, text, text_padding_left, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100571 start_lnum, end_lnum, start_col, end_col);
572 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100573
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100574 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100575
576theend:
577 vim_free(text);
578 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100579}
580
581/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100582 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100583 * Returns the number of text properties and, when non-zero, a pointer to the
584 * first one in "props" (note that it is not aligned, therefore the char_u
585 * pointer).
586 */
587 int
588get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
589{
590 char_u *text;
591 size_t textlen;
592 size_t proplen;
593
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100594 // Be quick when no text property types have been defined or the buffer,
595 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200596 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100597 return 0;
598
599 // Fetch the line to get the ml_line_len field updated.
600 text = ml_get_buf(buf, lnum, will_change);
601 textlen = STRLEN(text) + 1;
602 proplen = buf->b_ml.ml_line_len - textlen;
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100603 if (proplen == 0)
604 return 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100605 if (proplen % sizeof(textprop_T) != 0)
606 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000607 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100608 return 0;
609 }
Bram Moolenaar38ea2732022-08-07 22:04:56 +0100610 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100611 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100612}
613
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100614/*
615 * Return the number of text properties with "below" alignment in line "lnum".
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100616 * A "right" aligned property also goes below after a "below" or other "right"
617 * aligned property.
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100618 */
619 int
620prop_count_below(buf_T *buf, linenr_T lnum)
621{
622 char_u *props;
623 int count = get_text_props(buf, lnum, &props, FALSE);
624 int result = 0;
625 textprop_T prop;
626 int i;
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100627 int next_right_goes_below = FALSE;
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100628
629 if (count == 0)
630 return 0;
631 for (i = 0; i < count; ++i)
632 {
633 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar8f369fb2022-08-13 19:35:05 +0100634 if (prop.tp_col == MAXCOL)
635 {
636 if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW)
637 || (next_right_goes_below
638 && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)))
639 {
640 next_right_goes_below = TRUE;
641 ++result;
642 }
643 else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT)
644 next_right_goes_below = TRUE;
645 }
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100646 }
647 return result;
648}
649
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200650/**
651 * Return the number of text properties on line "lnum" in the current buffer.
652 * When "only_starting" is true only text properties starting in this line will
653 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100654 * When "last_line" is FALSE then text properties after the line are not
655 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200656 */
657 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100658count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200659{
660 char_u *props;
661 int proplen = get_text_props(curbuf, lnum, &props, 0);
662 int result = proplen;
663 int i;
664 textprop_T prop;
665
Bram Moolenaare175dc62022-08-01 22:18:50 +0100666 for (i = 0; i < proplen; ++i)
667 {
668 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100669 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100670 // previous line, or when not in the last line and it is virtual text
671 // after the line.
672 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
673 || (!last_line && prop.tp_col == MAXCOL))
674 --result;
675 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200676 return result;
677}
678
Bram Moolenaarf396ce82022-08-23 18:39:37 +0100679static textprop_T *text_prop_compare_props;
680static buf_T *text_prop_compare_buf;
681
682/*
683 * Function passed to qsort() to sort text properties.
684 * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if
685 * both have the same priority.
686 */
687 static int
688text_prop_compare(const void *s1, const void *s2)
689{
690 int idx1, idx2;
691 textprop_T *tp1, *tp2;
692 proptype_T *pt1, *pt2;
693 colnr_T col1, col2;
694
695 idx1 = *(int *)s1;
696 idx2 = *(int *)s2;
697 tp1 = &text_prop_compare_props[idx1];
698 tp2 = &text_prop_compare_props[idx2];
699 col1 = tp1->tp_col;
700 col2 = tp2->tp_col;
701 if (col1 == MAXCOL && col2 == MAXCOL)
702 {
703 int flags1 = 0;
704 int flags2 = 0;
705
706 // both props add text are after the line, order on 0: after (default),
707 // 1: right, 2: below (comes last)
708 if (tp1->tp_flags & TP_FLAG_ALIGN_RIGHT)
709 flags1 = 1;
710 if (tp1->tp_flags & TP_FLAG_ALIGN_BELOW)
711 flags1 = 2;
712 if (tp2->tp_flags & TP_FLAG_ALIGN_RIGHT)
713 flags2 = 1;
714 if (tp2->tp_flags & TP_FLAG_ALIGN_BELOW)
715 flags2 = 2;
716 if (flags1 != flags2)
717 return flags1 < flags2 ? 1 : -1;
718 }
719
720 // property that inserts text has priority over one that doesn't
721 if ((tp1->tp_id < 0) != (tp2->tp_id < 0))
722 return tp1->tp_id < 0 ? 1 : -1;
723
724 // check highest priority, defined by the type
725 pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type);
726 pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type);
727 if (pt1 != pt2)
728 {
729 if (pt1 == NULL)
730 return -1;
731 if (pt2 == NULL)
732 return 1;
733 if (pt1->pt_priority != pt2->pt_priority)
734 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
735 }
736
737 // same priority, one that starts first wins
738 if (col1 != col2)
739 return col1 < col2 ? 1 : -1;
740
741 // for a property with text the id can be used as tie breaker
742 if (tp1->tp_id < 0)
743 return tp1->tp_id > tp2->tp_id ? 1 : -1;
744
745 return 0;
746}
747
748/*
749 * Sort "count" text properties using an array if indexes "idxs" into the list
750 * of text props "props" for buffer "buf".
751 */
752 void
753sort_text_props(
754 buf_T *buf,
755 textprop_T *props,
756 int *idxs,
757 int count)
758{
759 text_prop_compare_buf = buf;
760 text_prop_compare_props = props;
761 qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare);
762}
763
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100764/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200765 * Find text property "type_id" in the visible lines of window "wp".
766 * Match "id" when it is > 0.
767 * Returns FAIL when not found.
768 */
769 int
770find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
771 linenr_T *found_lnum)
772{
773 linenr_T lnum;
774 char_u *props;
775 int count;
776 int i;
777
778 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100779 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200780 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
781 {
782 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
783 for (i = 0; i < count; ++i)
784 {
785 mch_memmove(prop, props + i * sizeof(textprop_T),
786 sizeof(textprop_T));
787 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
788 {
789 *found_lnum = lnum;
790 return OK;
791 }
792 }
793 }
794 return FAIL;
795}
796
797/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100798 * Set the text properties for line "lnum" to "props" with length "len".
799 * If "len" is zero text properties are removed, "props" is not used.
800 * Any existing text properties are dropped.
801 * Only works for the current buffer.
802 */
803 static void
804set_text_props(linenr_T lnum, char_u *props, int len)
805{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100806 char_u *text;
807 char_u *newtext;
808 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100809
810 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100811 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100812 newtext = alloc(textlen + len);
813 if (newtext == NULL)
814 return;
815 mch_memmove(newtext, text, textlen);
816 if (len > 0)
817 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100818 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100819 vim_free(curbuf->b_ml.ml_line_ptr);
820 curbuf->b_ml.ml_line_ptr = newtext;
821 curbuf->b_ml.ml_line_len = textlen + len;
822 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
823}
824
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100825/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100826 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100827 */
828 void
829add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
830{
831 char_u *text;
832 char_u *newtext;
833 int proplen = text_prop_count * (int)sizeof(textprop_T);
834
835 text = ml_get(lnum);
836 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
837 if (newtext == NULL)
838 return;
839 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
840 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
841 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
842 vim_free(curbuf->b_ml.ml_line_ptr);
843 curbuf->b_ml.ml_line_ptr = newtext;
844 curbuf->b_ml.ml_line_len += proplen;
845 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
846}
847
Bram Moolenaare44336b2022-08-07 18:20:08 +0100848/*
849 * Function passed to qsort() for sorting proptype_T on pt_id.
850 */
851 static int
852compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100853{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100854 proptype_T *tp1 = *(proptype_T **)s1;
855 proptype_T *tp2 = *(proptype_T **)s2;
856
857 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
858}
859
860 static proptype_T *
861find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
862{
863 int low = 0;
864 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100865
Bram Moolenaar10246902022-08-08 17:08:05 +0100866 if (ht == NULL || ht->ht_used == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100867 return NULL;
868
Bram Moolenaare44336b2022-08-07 18:20:08 +0100869 // Make the loopup faster by creating an array with pointers to
870 // hashtable entries, sorted on pt_id.
871 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100872 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100873 long todo;
874 hashitem_T *hi;
875 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100876
Bram Moolenaare44336b2022-08-07 18:20:08 +0100877 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
878 if (*array == NULL)
879 return NULL;
880 todo = (long)ht->ht_used;
881 for (hi = ht->ht_array; todo > 0; ++hi)
882 {
883 if (!HASHITEM_EMPTY(hi))
884 {
885 (*array)[i++] = HI2PT(hi);
886 --todo;
887 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100888 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100889 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
890 }
891
892 // binary search in the sorted array
893 high = ht->ht_used;
894 while (high > low)
895 {
896 int m = (high + low) / 2;
897
898 if ((*array)[m]->pt_id == id)
899 return (*array)[m];
900 if ((*array)[m]->pt_id > id)
901 high = m;
902 else
903 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100904 }
905 return NULL;
906}
907
908/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100909 * Fill 'dict' with text properties in 'prop'.
910 */
911 static void
912prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
913{
914 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200915 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100916
917 dict_add_number(dict, "col", prop->tp_col);
918 dict_add_number(dict, "length", prop->tp_len);
919 dict_add_number(dict, "id", prop->tp_id);
920 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
921 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200922
Bram Moolenaare44336b2022-08-07 18:20:08 +0100923 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200924 if (pt == NULL)
925 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100926 pt = find_type_by_id(global_proptypes, &global_proparray,
927 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200928 buflocal = FALSE;
929 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100930 if (pt != NULL)
931 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200932
933 if (buflocal)
934 dict_add_number(dict, "type_bufnr", buf->b_fnum);
935 else
936 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100937}
938
939/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100940 * Find a property type by ID in "buf" or globally.
941 * Returns NULL if not found.
942 */
943 proptype_T *
944text_prop_type_by_id(buf_T *buf, int id)
945{
946 proptype_T *type;
947
Bram Moolenaare44336b2022-08-07 18:20:08 +0100948 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100949 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100950 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100951 return type;
952}
953
954/*
955 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
956 */
957 void
958f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
959{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200960 linenr_T start;
961 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100962 linenr_T lnum;
963 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100964 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100965
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200966 if (in_vim9script()
967 && (check_for_number_arg(argvars, 0) == FAIL
968 || check_for_opt_number_arg(argvars, 1) == FAIL
969 || (argvars[1].v_type != VAR_UNKNOWN
970 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
971 return;
972
973 start = tv_get_number(&argvars[0]);
974 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100975 if (argvars[1].v_type != VAR_UNKNOWN)
976 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100977 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100978 if (argvars[2].v_type != VAR_UNKNOWN)
979 {
980 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
981 return;
982 }
983 }
984 if (start < 1 || end < 1)
985 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200986 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100987 return;
988 }
989
990 for (lnum = start; lnum <= end; ++lnum)
991 {
992 char_u *text;
993 size_t len;
994
995 if (lnum > buf->b_ml.ml_line_count)
996 break;
997 text = ml_get_buf(buf, lnum, FALSE);
998 len = STRLEN(text) + 1;
999 if ((size_t)buf->b_ml.ml_line_len > len)
1000 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001001 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001002 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1003 {
1004 char_u *newtext = vim_strsave(text);
1005
1006 // need to allocate the line now
1007 if (newtext == NULL)
1008 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001009 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1010 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001011 buf->b_ml.ml_line_ptr = newtext;
1012 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1013 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01001014 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001015 }
1016 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +01001017 if (did_clear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001018 redraw_buf_later(buf, UPD_NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001019}
1020
1021/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001022 * prop_find({props} [, {direction}])
1023 */
1024 void
1025f_prop_find(typval_T *argvars, typval_T *rettv)
1026{
1027 pos_T *cursor = &curwin->w_cursor;
1028 dict_T *dict;
1029 buf_T *buf = curbuf;
1030 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001031 int lnum_start;
1032 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001033 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001034 int id = 0;
1035 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001036 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001037 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001038 int lnum = -1;
1039 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +01001040 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001041 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001042
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001043 if (in_vim9script()
1044 && (check_for_dict_arg(argvars, 0) == FAIL
1045 || check_for_opt_string_arg(argvars, 1) == FAIL))
1046 return;
1047
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001048 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1049 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001050 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001051 return;
1052 }
1053 dict = argvars[0].vval.v_dict;
1054
1055 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1056 return;
1057 if (buf->b_ml.ml_mfp == NULL)
1058 return;
1059
1060 if (argvars[1].v_type != VAR_UNKNOWN)
1061 {
1062 char_u *dir_s = tv_get_string(&argvars[1]);
1063
1064 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +01001065 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001066 else if (*dir_s != 'f')
1067 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001068 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001069 return;
1070 }
1071 }
1072
1073 di = dict_find(dict, (char_u *)"lnum", -1);
1074 if (di != NULL)
1075 lnum = tv_get_number(&di->di_tv);
1076
1077 di = dict_find(dict, (char_u *)"col", -1);
1078 if (di != NULL)
1079 col = tv_get_number(&di->di_tv);
1080
1081 if (lnum == -1)
1082 {
1083 lnum = cursor->lnum;
1084 col = cursor->col + 1;
1085 }
1086 else if (col == -1)
1087 col = 1;
1088
1089 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
1090 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001091 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001092 return;
1093 }
1094
Bram Moolenaard61efa52022-07-23 09:52:04 +01001095 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001096
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001097 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001098 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001099 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +02001100 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +02001101 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001102 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001103 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001104 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001105 proptype_T *type = lookup_prop_type(name, buf);
1106
1107 if (type == NULL)
1108 return;
1109 type_id = type->pt_id;
1110 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001111 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001112 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001113 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001114 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001115 return;
1116 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001117 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001118 {
Ben Jacksona7704222022-08-20 20:54:51 +01001119 emsg(_(e_need_id_and_type_or_types_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001120 return;
1121 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001122
1123 lnum_start = lnum;
1124
1125 if (rettv_dict_alloc(rettv) == FAIL)
1126 return;
1127
1128 while (1)
1129 {
1130 char_u *text = ml_get_buf(buf, lnum, FALSE);
1131 size_t textlen = STRLEN(text) + 1;
1132 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001133 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001134 int i;
1135 textprop_T prop;
1136 int prop_start;
1137 int prop_end;
1138
LemonBoy9bd3ce22022-04-18 21:54:02 +01001139 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001140 {
1141 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001142 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001143
LemonBoy9bd3ce22022-04-18 21:54:02 +01001144 // For the very first line try to find the first property before or
1145 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001146 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001147 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001148 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001149 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001150 if (prop.tp_col > col)
1151 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001152 }
1153 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1154 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001155 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001156 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001157 : (id_found && prop.tp_id == id)
1158 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001159 {
1160 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001161 if (lnum_start == lnum
1162 && col >= prop.tp_col
1163 && (col <= prop.tp_col + prop.tp_len
1164 - (prop.tp_len != 0)))
1165 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001166
LemonBoy9bd3ce22022-04-18 21:54:02 +01001167 // The property was not continued from last line, it starts on
1168 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001169 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001170 // The property does not continue on the next line, it ends on
1171 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001172 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001173 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001174 seen_end = 1;
1175
1176 // Skip lines without the start flag.
1177 if (!prop_start)
1178 {
1179 // Always search backwards for start when search started
1180 // on a prop and we're not skipping.
1181 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001182 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001183 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001184 }
1185
1186 // If skipstart is true, skip the prop at start pos (even if
1187 // continued from another line).
1188 if (start_pos_has_prop && skipstart && !seen_end)
1189 {
1190 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001191 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001192 }
1193
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001194 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1195 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1196
1197 return;
1198 }
1199 }
1200
1201 if (dir > 0)
1202 {
1203 if (lnum >= buf->b_ml.ml_line_count)
1204 break;
1205 lnum++;
1206 }
1207 else
1208 {
1209 if (lnum <= 1)
1210 break;
1211 lnum--;
1212 }
1213 }
1214}
1215
1216/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001217 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1218 */
1219 static int
1220prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1221{
1222 int i;
1223
1224 for (i = 0; i < len; i++)
1225 if (types_or_ids[i] == type_or_id)
1226 return TRUE;
1227
1228 return FALSE;
1229}
1230
1231/*
1232 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1233 * If 'prop_types' is not NULL, then return only the text properties with
1234 * matching property type in the 'prop_types' array.
1235 * If 'prop_ids' is not NULL, then return only the text properties with
1236 * an identifier in the 'props_ids' array.
1237 * If 'add_lnum' is TRUE, then add the line number also to the text property
1238 * dictionary.
1239 */
1240 static void
1241get_props_in_line(
1242 buf_T *buf,
1243 linenr_T lnum,
1244 int *prop_types,
1245 int prop_types_len,
1246 int *prop_ids,
1247 int prop_ids_len,
1248 list_T *retlist,
1249 int add_lnum)
1250{
1251 char_u *text = ml_get_buf(buf, lnum, FALSE);
1252 size_t textlen = STRLEN(text) + 1;
1253 int count;
1254 int i;
1255 textprop_T prop;
1256
1257 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1258 for (i = 0; i < count; ++i)
1259 {
1260 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1261 sizeof(textprop_T));
1262 if ((prop_types == NULL
1263 || prop_type_or_id_in_list(prop_types, prop_types_len,
1264 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001265 && (prop_ids == NULL
1266 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1267 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001268 {
1269 dict_T *d = dict_alloc();
1270
1271 if (d == NULL)
1272 break;
1273 prop_fill_dict(d, &prop, buf);
1274 if (add_lnum)
1275 dict_add_number(d, "lnum", lnum);
1276 list_append_dict(retlist, d);
1277 }
1278 }
1279}
1280
1281/*
1282 * Convert a List of property type names into an array of property type
1283 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1284 * error. 'num_types' is set to the number of returned property types.
1285 */
1286 static int *
1287get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1288{
1289 int *prop_types;
1290 listitem_T *li;
1291 int i;
1292 char_u *name;
1293 proptype_T *type;
1294
1295 *num_types = 0;
1296
1297 prop_types = ALLOC_MULT(int, list_len(l));
1298 if (prop_types == NULL)
1299 return NULL;
1300
1301 i = 0;
1302 FOR_ALL_LIST_ITEMS(l, li)
1303 {
1304 if (li->li_tv.v_type != VAR_STRING)
1305 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001306 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001307 goto errret;
1308 }
1309 name = li->li_tv.vval.v_string;
1310 if (name == NULL)
1311 goto errret;
1312
1313 type = lookup_prop_type(name, buf);
1314 if (type == NULL)
1315 goto errret;
1316 prop_types[i++] = type->pt_id;
1317 }
1318
1319 *num_types = i;
1320 return prop_types;
1321
1322errret:
1323 VIM_CLEAR(prop_types);
1324 return NULL;
1325}
1326
1327/*
1328 * Convert a List of property identifiers into an array of property
1329 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1330 * error. 'num_ids' is set to the number of returned property identifiers.
1331 */
1332 static int *
1333get_prop_ids_from_list(list_T *l, int *num_ids)
1334{
1335 int *prop_ids;
1336 listitem_T *li;
1337 int i;
1338 int id;
1339 int error;
1340
1341 *num_ids = 0;
1342
1343 prop_ids = ALLOC_MULT(int, list_len(l));
1344 if (prop_ids == NULL)
1345 return NULL;
1346
1347 i = 0;
1348 FOR_ALL_LIST_ITEMS(l, li)
1349 {
1350 error = FALSE;
1351 id = tv_get_number_chk(&li->li_tv, &error);
1352 if (error)
1353 goto errret;
1354
1355 prop_ids[i++] = id;
1356 }
1357
1358 *num_ids = i;
1359 return prop_ids;
1360
1361errret:
1362 VIM_CLEAR(prop_ids);
1363 return NULL;
1364}
1365
1366/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001367 * prop_list({lnum} [, {bufnr}])
1368 */
1369 void
1370f_prop_list(typval_T *argvars, typval_T *rettv)
1371{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001372 linenr_T lnum;
1373 linenr_T start_lnum;
1374 linenr_T end_lnum;
1375 buf_T *buf = curbuf;
1376 int add_lnum = FALSE;
1377 int *prop_types = NULL;
1378 int prop_types_len = 0;
1379 int *prop_ids = NULL;
1380 int prop_ids_len = 0;
1381 list_T *l;
1382 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001383
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001384 if (in_vim9script()
1385 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001386 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001387 return;
1388
Bram Moolenaar93a10962022-06-16 11:42:09 +01001389 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001390 return;
1391
1392 // default: get text properties on current line
1393 start_lnum = tv_get_number(&argvars[0]);
1394 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001395 if (argvars[1].v_type != VAR_UNKNOWN)
1396 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001397 dict_T *d;
1398
1399 if (argvars[1].v_type != VAR_DICT)
1400 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001401 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001402 return;
1403 }
1404 d = argvars[1].vval.v_dict;
1405
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001406 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1407 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001408
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001409 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001410 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001411 if (di->di_tv.v_type != VAR_NUMBER)
1412 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001413 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001414 return;
1415 }
1416 end_lnum = tv_get_number(&di->di_tv);
1417 if (end_lnum < 0)
1418 // negative end_lnum is used as an offset from the last buffer
1419 // line
1420 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1421 else if (end_lnum > buf->b_ml.ml_line_count)
1422 end_lnum = buf->b_ml.ml_line_count;
1423 add_lnum = TRUE;
1424 }
1425 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1426 {
1427 if (di->di_tv.v_type != VAR_LIST)
1428 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001429 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001430 return;
1431 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001432
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001433 l = di->di_tv.vval.v_list;
1434 if (l != NULL && list_len(l) > 0)
1435 {
1436 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1437 if (prop_types == NULL)
1438 return;
1439 }
1440 }
1441 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1442 {
1443 if (di->di_tv.v_type != VAR_LIST)
1444 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001445 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001446 goto errret;
1447 }
1448
1449 l = di->di_tv.vval.v_list;
1450 if (l != NULL && list_len(l) > 0)
1451 {
1452 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1453 if (prop_ids == NULL)
1454 goto errret;
1455 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001456 }
1457 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001458 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1459 || end_lnum < 1 || end_lnum < start_lnum)
1460 emsg(_(e_invalid_range));
1461 else
1462 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1463 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1464 prop_ids, prop_ids_len,
1465 rettv->vval.v_list, add_lnum);
1466
1467errret:
1468 VIM_CLEAR(prop_types);
1469 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001470}
1471
1472/*
1473 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1474 */
1475 void
1476f_prop_remove(typval_T *argvars, typval_T *rettv)
1477{
1478 linenr_T start = 1;
1479 linenr_T end = 0;
1480 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001481 linenr_T first_changed = 0;
1482 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001483 dict_T *dict;
1484 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001485 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001486 int id = -MAXCOL;
Ben Jacksona7704222022-08-20 20:54:51 +01001487 int type_id = -1; // for a single "type"
1488 int *type_ids = NULL; // array, for a list of "types", allocated
1489 int num_type_ids = 0; // number of elements in "type_ids"
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001490 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001491 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001492
1493 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001494
1495 if (in_vim9script()
1496 && (check_for_dict_arg(argvars, 0) == FAIL
1497 || check_for_opt_number_arg(argvars, 1) == FAIL
1498 || (argvars[1].v_type != VAR_UNKNOWN
1499 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1500 return;
1501
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001502 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1503 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001504 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001505 return;
1506 }
1507
1508 if (argvars[1].v_type != VAR_UNKNOWN)
1509 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001510 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001511 end = start;
1512 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001513 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001514 if (start < 1 || end < 1)
1515 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001516 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001517 return;
1518 }
1519 }
1520
1521 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001522 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1523 return;
1524 if (buf->b_ml.ml_mfp == NULL)
1525 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001526
Bram Moolenaard61efa52022-07-23 09:52:04 +01001527 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001528
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001529 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001530 id = dict_get_number(dict, "id");
Ben Jacksona7704222022-08-20 20:54:51 +01001531
1532 // if a specific type was supplied "type": check that (and ignore "types".
1533 // Otherwise check against the list of "types".
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001534 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001535 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001536 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001537 proptype_T *type = lookup_prop_type(name, buf);
1538
1539 if (type == NULL)
1540 return;
1541 type_id = type->pt_id;
1542 }
Ben Jacksona7704222022-08-20 20:54:51 +01001543 if (dict_has_key(dict, "types"))
1544 {
1545 typval_T types;
1546 listitem_T *li = NULL;
1547
1548 dict_get_tv(dict, "types", &types);
1549 if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0)
1550 {
1551 type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len );
1552
1553 FOR_ALL_LIST_ITEMS(types.vval.v_list, li)
1554 {
1555 proptype_T *prop_type;
1556
1557 if (li->li_tv.v_type != VAR_STRING)
1558 continue;
1559
1560 prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf);
1561
1562 if (!prop_type)
1563 goto cleanup_prop_remove;
1564
1565 type_ids[num_type_ids++] = prop_type->pt_id;
1566 }
1567 }
1568 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001569 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001570
Ben Jacksona7704222022-08-20 20:54:51 +01001571 if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001572 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001573 emsg(_(e_need_at_least_one_of_id_or_type));
Ben Jacksona7704222022-08-20 20:54:51 +01001574 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001575 }
Ben Jacksona7704222022-08-20 20:54:51 +01001576 if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0)))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001577 {
Ben Jacksona7704222022-08-20 20:54:51 +01001578 emsg(_(e_need_id_and_type_or_types_with_both));
1579 goto cleanup_prop_remove;
1580 }
1581 if (type_id != -1 && num_type_ids > 0)
1582 {
1583 emsg(_(e_cannot_specify_both_type_and_types));
1584 goto cleanup_prop_remove;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001585 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001586
1587 if (end == 0)
1588 end = buf->b_ml.ml_line_count;
1589 for (lnum = start; lnum <= end; ++lnum)
1590 {
1591 char_u *text;
1592 size_t len;
1593
1594 if (lnum > buf->b_ml.ml_line_count)
1595 break;
1596 text = ml_get_buf(buf, lnum, FALSE);
1597 len = STRLEN(text) + 1;
1598 if ((size_t)buf->b_ml.ml_line_len > len)
1599 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001600 static textprop_T textprop; // static because of alignment
1601 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001602
1603 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1604 / sizeof(textprop_T); ++idx)
1605 {
1606 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1607 + idx * sizeof(textprop_T);
1608 size_t taillen;
Ben Jacksona7704222022-08-20 20:54:51 +01001609 int matches_id = 0;
1610 int matches_type = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001611
1612 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Ben Jacksona7704222022-08-20 20:54:51 +01001613
1614 matches_id = textprop.tp_id == id;
1615 if (num_type_ids > 0)
1616 {
1617 int idx2;
1618
1619 for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2)
1620 matches_type = textprop.tp_type == type_ids[idx2];
1621 }
1622 else
1623 {
1624 matches_type = textprop.tp_type == type_id;
1625 }
1626
1627 if (both ? matches_id && matches_type
1628 : matches_id || matches_type)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001629 {
1630 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1631 {
1632 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1633
1634 // need to allocate the line to be able to change it
1635 if (newptr == NULL)
Ben Jacksona7704222022-08-20 20:54:51 +01001636 goto cleanup_prop_remove;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001637 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1638 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001639 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1640 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001641 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001642 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1643
1644 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001645 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001646 }
1647
1648 taillen = buf->b_ml.ml_line_len - len
1649 - (idx + 1) * sizeof(textprop_T);
1650 if (taillen > 0)
1651 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1652 taillen);
1653 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1654 --idx;
1655
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001656 if (textprop.tp_id < 0)
1657 {
1658 garray_T *gap = &buf->b_textprop_text;
1659 int ii = -textprop.tp_id - 1;
1660
1661 // negative ID: property with text - free the text
1662 if (ii < gap->ga_len)
1663 {
1664 char_u **p = ((char_u **)gap->ga_data) + ii;
1665 vim_free(*p);
1666 *p = NULL;
1667 did_remove_text = TRUE;
1668 }
1669 }
1670
Bram Moolenaar965c0442021-05-17 00:22:06 +02001671 if (first_changed == 0)
1672 first_changed = lnum;
1673 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001674 ++rettv->vval.v_number;
1675 if (!do_all)
1676 break;
1677 }
1678 }
1679 }
1680 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001681
Bram Moolenaar965c0442021-05-17 00:22:06 +02001682 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001683 {
Bram Moolenaar326c5d32022-08-12 13:05:49 +01001684 changed_line_display_buf(buf);
Bram Moolenaar965c0442021-05-17 00:22:06 +02001685 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001686 redraw_buf_later(buf, UPD_VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001687 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001688
1689 if (did_remove_text)
1690 {
1691 garray_T *gap = &buf->b_textprop_text;
1692
1693 // Reduce the growarray size for NULL pointers at the end.
1694 while (gap->ga_len > 0
1695 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1696 --gap->ga_len;
1697 }
Ben Jacksona7704222022-08-20 20:54:51 +01001698
1699cleanup_prop_remove:
1700 vim_free(type_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001701}
1702
1703/*
1704 * Common for f_prop_type_add() and f_prop_type_change().
1705 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001706 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001707prop_type_set(typval_T *argvars, int add)
1708{
1709 char_u *name;
1710 buf_T *buf = NULL;
1711 dict_T *dict;
1712 dictitem_T *di;
1713 proptype_T *prop;
1714
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001715 if (in_vim9script()
1716 && (check_for_string_arg(argvars, 0) == FAIL
1717 || check_for_dict_arg(argvars, 1) == FAIL))
1718 return;
1719
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001720 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001721 if (*name == NUL)
1722 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001723 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001724 return;
1725 }
1726
1727 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1728 return;
1729 dict = argvars[1].vval.v_dict;
1730
Bram Moolenaare44336b2022-08-07 18:20:08 +01001731 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001732 if (add)
1733 {
1734 hashtab_T **htp;
1735
1736 if (prop != NULL)
1737 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001738 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001739 return;
1740 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001741 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001742 if (prop == NULL)
1743 return;
1744 STRCPY(prop->pt_name, name);
1745 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001746 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001747 if (buf == NULL)
1748 {
1749 htp = &global_proptypes;
1750 VIM_CLEAR(global_proparray);
1751 }
1752 else
1753 {
1754 htp = &buf->b_proptypes;
1755 VIM_CLEAR(buf->b_proparray);
1756 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001757 if (*htp == NULL)
1758 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001759 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001760 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001761 {
1762 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001763 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001764 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001765 hash_init(*htp);
1766 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001767 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001768 }
1769 else
1770 {
1771 if (prop == NULL)
1772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001773 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001774 return;
1775 }
1776 }
1777
1778 if (dict != NULL)
1779 {
1780 di = dict_find(dict, (char_u *)"highlight", -1);
1781 if (di != NULL)
1782 {
1783 char_u *highlight;
1784 int hl_id = 0;
1785
Bram Moolenaard61efa52022-07-23 09:52:04 +01001786 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001787 if (highlight != NULL && *highlight != NUL)
1788 hl_id = syn_name2id(highlight);
1789 if (hl_id <= 0)
1790 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001791 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001792 highlight == NULL ? (char_u *)"" : highlight);
1793 return;
1794 }
1795 prop->pt_hl_id = hl_id;
1796 }
1797
Bram Moolenaar58187f12019-05-05 16:33:47 +02001798 di = dict_find(dict, (char_u *)"combine", -1);
1799 if (di != NULL)
1800 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001801 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001802 prop->pt_flags |= PT_FLAG_COMBINE;
1803 else
1804 prop->pt_flags &= ~PT_FLAG_COMBINE;
1805 }
1806
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001807 di = dict_find(dict, (char_u *)"override", -1);
1808 if (di != NULL)
1809 {
1810 if (tv_get_bool(&di->di_tv))
1811 prop->pt_flags |= PT_FLAG_OVERRIDE;
1812 else
1813 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1814 }
1815
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001816 di = dict_find(dict, (char_u *)"priority", -1);
1817 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001818 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001819
1820 di = dict_find(dict, (char_u *)"start_incl", -1);
1821 if (di != NULL)
1822 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001823 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001824 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1825 else
1826 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1827 }
1828
1829 di = dict_find(dict, (char_u *)"end_incl", -1);
1830 if (di != NULL)
1831 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001832 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001833 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1834 else
1835 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1836 }
1837 }
1838}
1839
1840/*
1841 * prop_type_add({name}, {props})
1842 */
1843 void
1844f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1845{
1846 prop_type_set(argvars, TRUE);
1847}
1848
1849/*
1850 * prop_type_change({name}, {props})
1851 */
1852 void
1853f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1854{
1855 prop_type_set(argvars, FALSE);
1856}
1857
1858/*
1859 * prop_type_delete({name} [, {bufnr}])
1860 */
1861 void
1862f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1863{
1864 char_u *name;
1865 buf_T *buf = NULL;
1866 hashitem_T *hi;
1867
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001868 if (in_vim9script()
1869 && (check_for_string_arg(argvars, 0) == FAIL
1870 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1871 return;
1872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001873 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001874 if (*name == NUL)
1875 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001876 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001877 return;
1878 }
1879
1880 if (argvars[1].v_type != VAR_UNKNOWN)
1881 {
1882 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1883 return;
1884 }
1885
Bram Moolenaare44336b2022-08-07 18:20:08 +01001886 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001887 if (hi != NULL)
1888 {
1889 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001890 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001891
1892 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001893 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001894 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001895 VIM_CLEAR(global_proparray);
1896 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001897 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001898 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001899 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001900 VIM_CLEAR(buf->b_proparray);
1901 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001902 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001903 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001904 }
1905}
1906
1907/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001908 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001909 */
1910 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001911f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001912{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001913 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001914
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001915 if (in_vim9script()
1916 && (check_for_string_arg(argvars, 0) == FAIL
1917 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1918 return;
1919
1920 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001921 if (*name == NUL)
1922 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001923 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001924 return;
1925 }
1926 if (rettv_dict_alloc(rettv) == OK)
1927 {
1928 proptype_T *prop = NULL;
1929 buf_T *buf = NULL;
1930
1931 if (argvars[1].v_type != VAR_UNKNOWN)
1932 {
1933 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1934 return;
1935 }
1936
Bram Moolenaare44336b2022-08-07 18:20:08 +01001937 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001938 if (prop != NULL)
1939 {
1940 dict_T *d = rettv->vval.v_dict;
1941
1942 if (prop->pt_hl_id > 0)
1943 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1944 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001945 dict_add_number(d, "combine",
1946 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001947 dict_add_number(d, "start_incl",
1948 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1949 dict_add_number(d, "end_incl",
1950 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1951 if (buf != NULL)
1952 dict_add_number(d, "bufnr", buf->b_fnum);
1953 }
1954 }
1955}
1956
1957 static void
1958list_types(hashtab_T *ht, list_T *l)
1959{
1960 long todo;
1961 hashitem_T *hi;
1962
1963 todo = (long)ht->ht_used;
1964 for (hi = ht->ht_array; todo > 0; ++hi)
1965 {
1966 if (!HASHITEM_EMPTY(hi))
1967 {
1968 proptype_T *prop = HI2PT(hi);
1969
1970 list_append_string(l, prop->pt_name, -1);
1971 --todo;
1972 }
1973 }
1974}
1975
1976/*
1977 * prop_type_list([{bufnr}])
1978 */
1979 void
1980f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1981{
1982 buf_T *buf = NULL;
1983
1984 if (rettv_list_alloc(rettv) == OK)
1985 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001986 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1987 return;
1988
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001989 if (argvars[0].v_type != VAR_UNKNOWN)
1990 {
1991 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1992 return;
1993 }
1994 if (buf == NULL)
1995 {
1996 if (global_proptypes != NULL)
1997 list_types(global_proptypes, rettv->vval.v_list);
1998 }
1999 else if (buf->b_proptypes != NULL)
2000 list_types(buf->b_proptypes, rettv->vval.v_list);
2001 }
2002}
2003
2004/*
2005 * Free all property types in "ht".
2006 */
2007 static void
2008clear_ht_prop_types(hashtab_T *ht)
2009{
2010 long todo;
2011 hashitem_T *hi;
2012
2013 if (ht == NULL)
2014 return;
2015
2016 todo = (long)ht->ht_used;
2017 for (hi = ht->ht_array; todo > 0; ++hi)
2018 {
2019 if (!HASHITEM_EMPTY(hi))
2020 {
2021 proptype_T *prop = HI2PT(hi);
2022
2023 vim_free(prop);
2024 --todo;
2025 }
2026 }
2027
2028 hash_clear(ht);
2029 vim_free(ht);
2030}
2031
2032#if defined(EXITFREE) || defined(PROTO)
2033/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01002034 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002035 */
2036 void
2037clear_global_prop_types(void)
2038{
2039 clear_ht_prop_types(global_proptypes);
2040 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002041 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002042}
2043#endif
2044
2045/*
2046 * Free all property types for "buf".
2047 */
2048 void
2049clear_buf_prop_types(buf_T *buf)
2050{
2051 clear_ht_prop_types(buf->b_proptypes);
2052 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01002053 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002054}
2055
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002056// Struct used to return two values from adjust_prop().
2057typedef struct
2058{
2059 int dirty; // if the property was changed
2060 int can_drop; // whether after this change, the prop may be removed
2061} adjustres_T;
2062
2063/*
2064 * Adjust the property for "added" bytes (can be negative) inserted at "col".
2065 *
2066 * Note that "col" is zero-based, while tp_col is one-based.
2067 * Only for the current buffer.
2068 * "flags" can have:
2069 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002070 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002071 */
2072 static adjustres_T
2073adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002074 textprop_T *prop,
2075 colnr_T col,
2076 int added,
2077 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002078{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002079 proptype_T *pt;
2080 int start_incl;
2081 int end_incl;
2082 int droppable;
2083 adjustres_T res = {TRUE, FALSE};
2084
2085 // prop after end of the line doesn't move
2086 if (prop->tp_col == MAXCOL)
2087 {
2088 res.dirty = FALSE;
2089 return res;
2090 }
2091
2092 pt = text_prop_type_by_id(curbuf, prop->tp_type);
2093 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002094 || (flags & APC_SUBSTITUTE)
2095 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002096 if (prop->tp_id < 0 && (flags & APC_INDENT))
2097 // when inserting indent just before a character with virtual text
2098 // shift the text property
2099 start_incl = FALSE;
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002100 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01002101 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002102 // do not drop zero-width props if they later can increase in size
2103 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002104
2105 if (added > 0)
2106 {
2107 if (col + 1 <= prop->tp_col
2108 - (start_incl || (prop->tp_len == 0 && end_incl)))
2109 // Change is entirely before the text property: Only shift
2110 prop->tp_col += added;
2111 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
2112 // Insertion was inside text property
2113 prop->tp_len += added;
2114 }
2115 else if (prop->tp_col > col + 1)
2116 {
2117 if (prop->tp_col + added < col + 1)
2118 {
2119 prop->tp_len += (prop->tp_col - 1 - col) + added;
2120 prop->tp_col = col + 1;
2121 if (prop->tp_len <= 0)
2122 {
2123 prop->tp_len = 0;
2124 res.can_drop = droppable;
2125 }
2126 }
2127 else
2128 prop->tp_col += added;
2129 }
2130 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
2131 {
2132 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
2133
2134 prop->tp_len += after > 0 ? added + after : added;
2135 res.can_drop = prop->tp_len <= 0 && droppable;
2136 }
2137 else
2138 res.dirty = FALSE;
2139
2140 return res;
2141}
2142
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002143/*
2144 * Adjust the columns of text properties in line "lnum" after position "col" to
2145 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002146 * Note that "col" is zero-based, while tp_col is one-based.
2147 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002148 * "flags" can have:
2149 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
2150 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002151 * APC_INDENT: Text is inserted before virtual text prop
Bram Moolenaar8055d172019-05-17 22:57:26 +02002152 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002153 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002154 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002155 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01002156adjust_prop_columns(
2157 linenr_T lnum,
2158 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002159 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002160 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002161{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002162 int proplen;
2163 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002164 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002165 int ri, wi;
2166 size_t textlen;
2167
2168 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002169 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002170
2171 proplen = get_text_props(curbuf, lnum, &props, TRUE);
2172 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002173 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01002174 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002175
Bram Moolenaar196d1572019-01-02 23:47:18 +01002176 wi = 0; // write index
2177 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002178 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01002179 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002180 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002181
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002182 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2183 res = adjust_prop(&prop, col, bytes_added, flags);
2184 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002185 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002186 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002187 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2188 && u_savesub(lnum) == FAIL)
2189 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002190 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002191
2192 // u_savesub() may have updated curbuf->b_ml, fetch it again
2193 if (curbuf->b_ml.ml_line_lnum != lnum)
2194 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002195 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002196 if (res.can_drop)
2197 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002198 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002199 ++wi;
2200 }
2201 if (dirty)
2202 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002203 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2204
2205 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002206 {
2207 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2208
2209 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2210 vim_free(curbuf->b_ml.ml_line_ptr);
2211 curbuf->b_ml.ml_line_ptr = p;
2212 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002213 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002214 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002215 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002216 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002217}
2218
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002219/*
2220 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002221 * "lnum_props" is the line that has the properties from before the split.
2222 * "lnum_top" is the top line.
2223 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002224 * "deleted" is the number of bytes deleted.
2225 */
2226 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002227adjust_props_for_split(
2228 linenr_T lnum_props,
2229 linenr_T lnum_top,
2230 int kept,
2231 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002232{
2233 char_u *props;
2234 int count;
2235 garray_T prevprop;
2236 garray_T nextprop;
2237 int i;
2238 int skipped = kept + deleted;
2239
2240 if (!curbuf->b_has_textprop)
2241 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002242
2243 // Get the text properties from "lnum_props".
2244 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002245 ga_init2(&prevprop, sizeof(textprop_T), 10);
2246 ga_init2(&nextprop, sizeof(textprop_T), 10);
2247
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002248 // Keep the relevant ones in the first line, reducing the length if needed.
2249 // Copy the ones that include the split to the second line.
2250 // Move the ones after the split to the second line.
2251 for (i = 0; i < count; ++i)
2252 {
2253 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002254 proptype_T *pt;
2255 int start_incl, end_incl;
2256 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002257
2258 // copy the prop to an aligned structure
2259 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2260
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002261 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2262 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2263 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002264 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2265 cont_next = prop.tp_col != MAXCOL
2266 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01002267 // when a prop has text it is never copied
2268 if (prop.tp_id < 0 && cont_next)
2269 cont_prev = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002270
2271 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002272 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002273 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2274
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002275 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002276 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002277 if (p->tp_col + p->tp_len >= kept)
2278 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002279 if (cont_next)
2280 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002281 }
2282
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002283 // Only add the property to the next line if the length is bigger than
2284 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002285 if ((cont_next || prop.tp_col == MAXCOL)
2286 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002287 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002288 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002289
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002290 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002291 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002292 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002293 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002294 if (p->tp_col > skipped)
2295 p->tp_col -= skipped - 1;
2296 else
2297 {
2298 p->tp_len -= skipped - p->tp_col;
2299 p->tp_col = 1;
2300 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002301 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002302 if (cont_prev)
2303 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002304 }
2305 }
2306
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002307 set_text_props(lnum_top, prevprop.ga_data,
2308 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002309 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002310 set_text_props(lnum_top + 1, nextprop.ga_data,
2311 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002312 ga_clear(&nextprop);
2313}
2314
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002315/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002316 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002317 */
2318 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002319prepend_joined_props(
2320 char_u *new_props,
2321 int propcount,
2322 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002323 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002324 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002325 long col,
2326 int removed)
2327{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002328 char_u *props;
2329 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2330 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002331
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002332 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002333 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002334 textprop_T prop;
2335 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002336
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002337 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002338 if (prop.tp_col == MAXCOL && !last_line)
2339 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002340 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2341
2342 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002343 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002344
Bram Moolenaare175dc62022-08-01 22:18:50 +01002345 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002346 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2347 &prop, sizeof(prop));
2348 else
2349 {
2350 int j;
2351 int found = FALSE;
2352
2353 // Search for continuing prop.
2354 for (j = *props_remaining; j < propcount; ++j)
2355 {
2356 textprop_T op;
2357
2358 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2359 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2360 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002361 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002362 found = TRUE;
2363 op.tp_len += op.tp_col - prop.tp_col;
2364 op.tp_col = prop.tp_col;
2365 // Start/end is taken care of when deleting joined lines
2366 op.tp_flags = prop.tp_flags;
2367 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2368 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002369 }
2370 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002371 if (!found)
2372 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002373 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002374 }
2375}
2376
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002377#endif // FEAT_PROP_POPUP