blob: 679535375ca38c86364959e31455989fb04cba6c [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 * TODO:
Bram Moolenaarb56ac042018-12-28 23:22:40 +010014 * - Checking the text length to detect text properties is slow. Use a flag in
15 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010016 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
17 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010018 * - Perhaps have a window-local option to disable highlighting from text
19 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010020 */
21
22#include "vim.h"
23
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010024#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010025
26/*
27 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
28 * This avoids adding a pointer to the hashtable item.
29 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
30 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
31 * HI2PT() converts a hashitem pointer to a proptype pointer.
32 */
33#define PT2HIKEY(p) ((p)->pt_name)
34#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
35#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
36
37// The global text property types.
38static hashtab_T *global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +010039static proptype_T **global_proparray = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +010040
41// The last used text property type ID.
42static int proptype_id = 0;
43
Bram Moolenaar98aefe72018-12-13 22:20:09 +010044/*
45 * Find a property type by name, return the hashitem.
46 * Returns NULL if the item can't be found.
47 */
48 static hashitem_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010049find_prop_type_hi(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010050{
51 hashtab_T *ht;
52 hashitem_T *hi;
53
54 if (*name == NUL)
55 return NULL;
56 if (buf == NULL)
57 ht = global_proptypes;
58 else
59 ht = buf->b_proptypes;
60
61 if (ht == NULL)
62 return NULL;
63 hi = hash_find(ht, name);
64 if (HASHITEM_EMPTY(hi))
65 return NULL;
66 return hi;
67}
68
69/*
Bram Moolenaare44336b2022-08-07 18:20:08 +010070 * Like find_prop_type_hi() but return the property type.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010071 */
72 static proptype_T *
Bram Moolenaare44336b2022-08-07 18:20:08 +010073find_prop_type(char_u *name, buf_T *buf)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010074{
Bram Moolenaare44336b2022-08-07 18:20:08 +010075 hashitem_T *hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +010076
77 if (hi == NULL)
78 return NULL;
79 return HI2PT(hi);
80}
81
82/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020083 * Get the prop type ID of "name".
84 * When not found return zero.
85 */
86 int
87find_prop_type_id(char_u *name, buf_T *buf)
88{
Bram Moolenaare44336b2022-08-07 18:20:08 +010089 proptype_T *pt = find_prop_type(name, buf);
Bram Moolenaar12034e22019-08-25 22:25:02 +020090
91 if (pt == NULL)
92 return 0;
93 return pt->pt_id;
94}
95
96/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +010097 * Lookup a property type by name. First in "buf" and when not found in the
98 * global types.
99 * When not found gives an error message and returns NULL.
100 */
101 static proptype_T *
102lookup_prop_type(char_u *name, buf_T *buf)
103{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100104 proptype_T *type = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100105
106 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100107 type = find_prop_type(name, NULL);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100108 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100109 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100110 return type;
111}
112
113/*
114 * Get an optional "bufnr" item from the dict in "arg".
115 * When the argument is not used or "bufnr" is not present then "buf" is
116 * unchanged.
117 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100118 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100119 */
120 static int
121get_bufnr_from_arg(typval_T *arg, buf_T **buf)
122{
123 dictitem_T *di;
124
125 if (arg->v_type != VAR_DICT)
126 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000127 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100128 return FAIL;
129 }
130 if (arg->vval.v_dict == NULL)
131 return OK; // NULL dict is like an empty dict
132 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200133 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
134 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100135 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200136 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100137 if (*buf == NULL)
138 return FAIL;
139 }
140 return OK;
141}
142
143/*
144 * prop_add({lnum}, {col}, {props})
145 */
146 void
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100147f_prop_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100148{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100149 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100150 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100151
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200152 if (in_vim9script()
153 && (check_for_number_arg(argvars, 0) == FAIL
154 || check_for_number_arg(argvars, 1) == FAIL
155 || check_for_dict_arg(argvars, 2) == FAIL))
156 return;
157
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100158 start_lnum = tv_get_number(&argvars[0]);
159 start_col = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100160 if (argvars[2].v_type != VAR_DICT)
161 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000162 emsg(_(e_dictionary_required));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100163 return;
164 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200165
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100166 rettv->vval.v_number = prop_add_common(start_lnum, start_col,
167 argvars[2].vval.v_dict, curbuf, &argvars[2]);
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200168}
169
170/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200171 * Attach a text property 'type_name' to the text starting
172 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100173 * the buffer "buf" and assign identifier "id".
174 * When "text" is not NULL add it to buf->b_textprop_text[-id - 1].
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200175 */
176 static int
177prop_add_one(
178 buf_T *buf,
179 char_u *type_name,
180 int id,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100181 char_u *text_arg,
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100182 int text_flags,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200183 linenr_T start_lnum,
184 linenr_T end_lnum,
185 colnr_T start_col,
186 colnr_T end_col)
187{
188 proptype_T *type;
189 linenr_T lnum;
190 int proplen;
191 char_u *props = NULL;
192 char_u *newprops;
193 size_t textlen;
194 char_u *newtext;
195 int i;
196 textprop_T tmp_prop;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 char_u *text = text_arg;
198 int res = FAIL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200199
200 type = lookup_prop_type(type_name, buf);
201 if (type == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100202 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200203
204 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
205 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000206 semsg(_(e_invalid_line_number_nr), (long)start_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100207 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200208 }
209 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
210 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000211 semsg(_(e_invalid_line_number_nr), (long)end_lnum);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100212 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200213 }
214
215 if (buf->b_ml.ml_mfp == NULL)
216 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000217 emsg(_(e_cannot_add_text_property_to_unloaded_buffer));
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100218 goto theend;
219 }
220
221 if (text != NULL)
222 {
Bram Moolenaar783ef722022-08-01 16:11:06 +0100223 garray_T *gap = &buf->b_textprop_text;
224 char_u *p;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100225
226 // double check we got the right ID
227 if (-id - 1 != gap->ga_len)
228 iemsg("text prop ID mismatch");
229 if (gap->ga_growsize == 0)
230 ga_init2(gap, sizeof(char *), 50);
231 if (ga_grow(gap, 1) == FAIL)
232 goto theend;
233 ((char_u **)gap->ga_data)[gap->ga_len++] = text;
Bram Moolenaar783ef722022-08-01 16:11:06 +0100234
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100235 // change any control character (Tab, Newline, etc.) to a Space to make
236 // it simpler to compute the size
Bram Moolenaar783ef722022-08-01 16:11:06 +0100237 for (p = text; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100238 if (*p < ' ')
Bram Moolenaar783ef722022-08-01 16:11:06 +0100239 *p = ' ';
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100240 text = NULL;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200241 }
242
243 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
244 {
245 colnr_T col; // start column
246 long length; // in bytes
247
248 // Fetch the line to get the ml_line_len field updated.
249 proplen = get_text_props(buf, lnum, &props, TRUE);
250 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
251
252 if (lnum == start_lnum)
253 col = start_col;
254 else
255 col = 1;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100256 if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200257 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +0000258 semsg(_(e_invalid_column_number_nr), (long)start_col);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100259 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200260 }
261
262 if (lnum == end_lnum)
263 length = end_col - col;
264 else
265 length = (int)textlen - col + 1;
266 if (length > (long)textlen)
267 length = (int)textlen; // can include the end-of-line
268 if (length < 0)
269 length = 0; // zero-width property
270
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100271 if (text_arg != NULL)
272 {
273 length = 1; // text is placed on one character
274 if (col == 0)
275 col = MAXCOL; // after the line
276 }
277
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200278 // Allocate the new line with space for the new property.
279 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
280 if (newtext == NULL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100281 goto theend;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200282 // Copy the text, including terminating NUL.
283 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
284
285 // Find the index where to insert the new property.
286 // Since the text properties are not aligned properly when stored with
287 // the text, we need to copy them as bytes before using it as a struct.
288 for (i = 0; i < proplen; ++i)
289 {
290 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
291 sizeof(textprop_T));
292 if (tmp_prop.tp_col >= col)
293 break;
294 }
295 newprops = newtext + textlen;
296 if (i > 0)
297 mch_memmove(newprops, props, sizeof(textprop_T) * i);
298
299 tmp_prop.tp_col = col;
300 tmp_prop.tp_len = length;
301 tmp_prop.tp_id = id;
302 tmp_prop.tp_type = type->pt_id;
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100303 tmp_prop.tp_flags = text_flags
304 | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
305 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200306 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
307 sizeof(textprop_T));
308
309 if (i < proplen)
310 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
311 props + i * sizeof(textprop_T),
312 sizeof(textprop_T) * (proplen - i));
313
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100314 if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200315 vim_free(buf->b_ml.ml_line_ptr);
316 buf->b_ml.ml_line_ptr = newtext;
317 buf->b_ml.ml_line_len += sizeof(textprop_T);
318 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
319 }
320
321 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100322 res = OK;
323
324theend:
325 vim_free(text);
326 return res;
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200327}
328
329/*
330 * prop_add_list()
331 * First argument specifies the text property:
332 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
333 * Second argument is a List where each item is a List with the following
334 * entries: [lnum, start_col, end_col]
335 */
336 void
337f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
338{
339 dict_T *dict;
340 char_u *type_name;
341 buf_T *buf = curbuf;
342 int id = 0;
343 listitem_T *li;
344 list_T *pos_list;
345 linenr_T start_lnum;
346 colnr_T start_col;
347 linenr_T end_lnum;
348 colnr_T end_col;
349 int error = FALSE;
350
351 if (check_for_dict_arg(argvars, 0) == FAIL
352 || check_for_list_arg(argvars, 1) == FAIL)
353 return;
354
355 if (argvars[1].vval.v_list == NULL)
356 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000357 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200358 return;
359 }
360
361 dict = argvars[0].vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100362 if (dict == NULL || !dict_has_key(dict, "type"))
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200363 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000364 emsg(_(e_missing_property_type_name));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200365 return;
366 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100367 type_name = dict_get_string(dict, "type", FALSE);
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200368
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100369 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +0100370 id = dict_get_number(dict, "id");
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200371
372 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
373 return;
374
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100375 // This must be done _before_ we start adding properties because property
376 // changes trigger buffer (memline) reorganisation, which needs this flag
377 // to be correctly set.
378 buf->b_has_textprop = TRUE; // this is never reset
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200379 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
380 {
381 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
382 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000383 emsg(_(e_list_required));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200384 return;
385 }
386
387 pos_list = li->li_tv.vval.v_list;
388 start_lnum = list_find_nr(pos_list, 0L, &error);
389 start_col = list_find_nr(pos_list, 1L, &error);
390 end_lnum = list_find_nr(pos_list, 2L, &error);
391 end_col = list_find_nr(pos_list, 3L, &error);
392 if (error || start_lnum <= 0 || start_col <= 0
393 || end_lnum <= 0 || end_col <= 0)
394 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000395 emsg(_(e_invalid_argument));
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200396 return;
397 }
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100398 if (prop_add_one(buf, type_name, id, NULL, 0, start_lnum, end_lnum,
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200399 start_col, end_col) == FAIL)
400 return;
401 }
402
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200403 redraw_buf_later(buf, VALID);
404}
405
406/*
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100407 * Get the next ID to use for a textprop with text in buffer "buf".
408 */
409 static int
410get_textprop_id(buf_T *buf)
411{
412 // TODO: recycle deleted entries
413 return -(buf->b_textprop_text.ga_len + 1);
414}
415
416/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200417 * Shared between prop_add() and popup_create().
418 * "dict_arg" is the function argument of a dict containing "bufnr".
419 * it is NULL for popup_create().
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100420 * Returns the "id" used for "text" or zero.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200421 */
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100422 int
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200423prop_add_common(
424 linenr_T start_lnum,
425 colnr_T start_col,
426 dict_T *dict,
427 buf_T *default_buf,
428 typval_T *dict_arg)
429{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200430 linenr_T end_lnum;
431 colnr_T end_col;
432 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200433 buf_T *buf = default_buf;
434 int id = 0;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100435 char_u *text = NULL;
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;
499 if (STRCMP(p, "right") == 0)
500 flags |= TP_FLAG_ALIGN_RIGHT;
501 else if (STRCMP(p, "below") == 0)
502 flags |= TP_FLAG_ALIGN_BELOW;
503 else if (STRCMP(p, "after") != 0)
504 {
505 semsg(_(e_invalid_value_for_argument_str_str), "text_align", p);
506 goto theend;
507 }
508 }
509
510 if (dict_has_key(dict, "text_wrap"))
511 {
512 char_u *p = dict_get_string(dict, "text_wrap", FALSE);
513 if (p == NULL)
514 goto theend;
515 if (STRCMP(p, "wrap") == 0)
516 flags |= TP_FLAG_WRAP;
517 else if (STRCMP(p, "truncate") != 0)
518 {
519 semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p);
520 goto theend;
521 }
522 }
523 }
524
525 // Column must be 1 or more for a normal text property; when "text" is
526 // present zero means it goes after the line.
527 if (start_col < (text == NULL ? 1 : 0))
528 {
529 semsg(_(e_invalid_column_number_nr), (long)start_col);
530 goto theend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100531 }
532
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200533 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100534 goto theend;
535
536 if (id < 0 && buf->b_textprop_text.ga_len > 0)
537 {
538 emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text));
539 goto theend;
540 }
541 if (text != NULL)
542 id = get_textprop_id(buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100543
Paul Ollis4c3d21a2022-05-24 21:26:37 +0100544 // This must be done _before_ we add the property because property changes
545 // trigger buffer (memline) reorganisation, which needs this flag to be
546 // correctly set.
547 buf->b_has_textprop = TRUE; // this is never reset
548
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100549 prop_add_one(buf, type_name, id, text, flags,
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100550 start_lnum, end_lnum, start_col, end_col);
551 text = NULL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100552
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200553 redraw_buf_later(buf, VALID);
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100554
555theend:
556 vim_free(text);
557 return id;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100558}
559
560/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100561 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100562 * Returns the number of text properties and, when non-zero, a pointer to the
563 * first one in "props" (note that it is not aligned, therefore the char_u
564 * pointer).
565 */
566 int
567get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
568{
569 char_u *text;
570 size_t textlen;
571 size_t proplen;
572
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100573 // Be quick when no text property types have been defined or the buffer,
574 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200575 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100576 return 0;
577
578 // Fetch the line to get the ml_line_len field updated.
579 text = ml_get_buf(buf, lnum, will_change);
580 textlen = STRLEN(text) + 1;
581 proplen = buf->b_ml.ml_line_len - textlen;
582 if (proplen % sizeof(textprop_T) != 0)
583 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000584 iemsg(_(e_text_property_info_corrupted));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100585 return 0;
586 }
587 if (proplen > 0)
588 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100589 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100590}
591
Bram Moolenaar4d91d342022-08-06 13:48:20 +0100592/*
593 * Return the number of text properties with "below" alignment in line "lnum".
594 */
595 int
596prop_count_below(buf_T *buf, linenr_T lnum)
597{
598 char_u *props;
599 int count = get_text_props(buf, lnum, &props, FALSE);
600 int result = 0;
601 textprop_T prop;
602 int i;
603
604 if (count == 0)
605 return 0;
606 for (i = 0; i < count; ++i)
607 {
608 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
609 if (prop.tp_col == MAXCOL && (prop.tp_flags & TP_FLAG_ALIGN_BELOW))
610 ++result;
611 }
612 return result;
613}
614
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200615/**
616 * Return the number of text properties on line "lnum" in the current buffer.
617 * When "only_starting" is true only text properties starting in this line will
618 * be considered.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100619 * When "last_line" is FALSE then text properties after the line are not
620 * counted.
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200621 */
622 int
Bram Moolenaare175dc62022-08-01 22:18:50 +0100623count_props(linenr_T lnum, int only_starting, int last_line)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200624{
625 char_u *props;
626 int proplen = get_text_props(curbuf, lnum, &props, 0);
627 int result = proplen;
628 int i;
629 textprop_T prop;
630
Bram Moolenaare175dc62022-08-01 22:18:50 +0100631 for (i = 0; i < proplen; ++i)
632 {
633 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100634 // A prop is dropped when in the first line and it continues from the
Bram Moolenaare175dc62022-08-01 22:18:50 +0100635 // previous line, or when not in the last line and it is virtual text
636 // after the line.
637 if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV))
638 || (!last_line && prop.tp_col == MAXCOL))
639 --result;
640 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200641 return result;
642}
643
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100644/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200645 * Find text property "type_id" in the visible lines of window "wp".
646 * Match "id" when it is > 0.
647 * Returns FAIL when not found.
648 */
649 int
650find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
651 linenr_T *found_lnum)
652{
653 linenr_T lnum;
654 char_u *props;
655 int count;
656 int i;
657
658 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100659 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200660 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
661 {
662 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
663 for (i = 0; i < count; ++i)
664 {
665 mch_memmove(prop, props + i * sizeof(textprop_T),
666 sizeof(textprop_T));
667 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
668 {
669 *found_lnum = lnum;
670 return OK;
671 }
672 }
673 }
674 return FAIL;
675}
676
677/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100678 * Set the text properties for line "lnum" to "props" with length "len".
679 * If "len" is zero text properties are removed, "props" is not used.
680 * Any existing text properties are dropped.
681 * Only works for the current buffer.
682 */
683 static void
684set_text_props(linenr_T lnum, char_u *props, int len)
685{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100686 char_u *text;
687 char_u *newtext;
688 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100689
690 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100691 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100692 newtext = alloc(textlen + len);
693 if (newtext == NULL)
694 return;
695 mch_memmove(newtext, text, textlen);
696 if (len > 0)
697 mch_memmove(newtext + textlen, props, len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100698 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100699 vim_free(curbuf->b_ml.ml_line_ptr);
700 curbuf->b_ml.ml_line_ptr = newtext;
701 curbuf->b_ml.ml_line_len = textlen + len;
702 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
703}
704
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100705/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +0100706 * Add "text_props" with "text_prop_count" text properties to line "lnum".
Bram Moolenaar213bbaf2022-08-05 19:46:48 +0100707 */
708 void
709add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count)
710{
711 char_u *text;
712 char_u *newtext;
713 int proplen = text_prop_count * (int)sizeof(textprop_T);
714
715 text = ml_get(lnum);
716 newtext = alloc(curbuf->b_ml.ml_line_len + proplen);
717 if (newtext == NULL)
718 return;
719 mch_memmove(newtext, text, curbuf->b_ml.ml_line_len);
720 mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen);
721 if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED))
722 vim_free(curbuf->b_ml.ml_line_ptr);
723 curbuf->b_ml.ml_line_ptr = newtext;
724 curbuf->b_ml.ml_line_len += proplen;
725 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
726}
727
Bram Moolenaare44336b2022-08-07 18:20:08 +0100728/*
729 * Function passed to qsort() for sorting proptype_T on pt_id.
730 */
731 static int
732compare_pt(const void *s1, const void *s2)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100733{
Bram Moolenaare44336b2022-08-07 18:20:08 +0100734 proptype_T *tp1 = *(proptype_T **)s1;
735 proptype_T *tp2 = *(proptype_T **)s2;
736
737 return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1;
738}
739
740 static proptype_T *
741find_type_by_id(hashtab_T *ht, proptype_T ***array, int id)
742{
743 int low = 0;
744 int high;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100745
746 if (ht == NULL)
747 return NULL;
748
Bram Moolenaare44336b2022-08-07 18:20:08 +0100749 // Make the loopup faster by creating an array with pointers to
750 // hashtable entries, sorted on pt_id.
751 if (*array == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100752 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100753 long todo;
754 hashitem_T *hi;
755 int i = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100756
Bram Moolenaare44336b2022-08-07 18:20:08 +0100757 *array = ALLOC_MULT(proptype_T *, ht->ht_used);
758 if (*array == NULL)
759 return NULL;
760 todo = (long)ht->ht_used;
761 for (hi = ht->ht_array; todo > 0; ++hi)
762 {
763 if (!HASHITEM_EMPTY(hi))
764 {
765 (*array)[i++] = HI2PT(hi);
766 --todo;
767 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100768 }
Bram Moolenaare44336b2022-08-07 18:20:08 +0100769 qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt);
770 }
771
772 // binary search in the sorted array
773 high = ht->ht_used;
774 while (high > low)
775 {
776 int m = (high + low) / 2;
777
778 if ((*array)[m]->pt_id == id)
779 return (*array)[m];
780 if ((*array)[m]->pt_id > id)
781 high = m;
782 else
783 low = m + 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100784 }
785 return NULL;
786}
787
788/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100789 * Fill 'dict' with text properties in 'prop'.
790 */
791 static void
792prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
793{
794 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200795 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100796
797 dict_add_number(dict, "col", prop->tp_col);
798 dict_add_number(dict, "length", prop->tp_len);
799 dict_add_number(dict, "id", prop->tp_id);
800 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
801 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200802
Bram Moolenaare44336b2022-08-07 18:20:08 +0100803 pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200804 if (pt == NULL)
805 {
Bram Moolenaare44336b2022-08-07 18:20:08 +0100806 pt = find_type_by_id(global_proptypes, &global_proparray,
807 prop->tp_type);
Martin Tournoije2390c72021-07-28 13:30:16 +0200808 buflocal = FALSE;
809 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100810 if (pt != NULL)
811 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200812
813 if (buflocal)
814 dict_add_number(dict, "type_bufnr", buf->b_fnum);
815 else
816 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100817}
818
819/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100820 * Find a property type by ID in "buf" or globally.
821 * Returns NULL if not found.
822 */
823 proptype_T *
824text_prop_type_by_id(buf_T *buf, int id)
825{
826 proptype_T *type;
827
Bram Moolenaare44336b2022-08-07 18:20:08 +0100828 type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100829 if (type == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +0100830 type = find_type_by_id(global_proptypes, &global_proparray, id);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100831 return type;
832}
833
834/*
835 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
836 */
837 void
838f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
839{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200840 linenr_T start;
841 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100842 linenr_T lnum;
843 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100844 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100845
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200846 if (in_vim9script()
847 && (check_for_number_arg(argvars, 0) == FAIL
848 || check_for_opt_number_arg(argvars, 1) == FAIL
849 || (argvars[1].v_type != VAR_UNKNOWN
850 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
851 return;
852
853 start = tv_get_number(&argvars[0]);
854 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100855 if (argvars[1].v_type != VAR_UNKNOWN)
856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100857 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100858 if (argvars[2].v_type != VAR_UNKNOWN)
859 {
860 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
861 return;
862 }
863 }
864 if (start < 1 || end < 1)
865 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200866 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100867 return;
868 }
869
870 for (lnum = start; lnum <= end; ++lnum)
871 {
872 char_u *text;
873 size_t len;
874
875 if (lnum > buf->b_ml.ml_line_count)
876 break;
877 text = ml_get_buf(buf, lnum, FALSE);
878 len = STRLEN(text) + 1;
879 if ((size_t)buf->b_ml.ml_line_len > len)
880 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100881 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100882 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
883 {
884 char_u *newtext = vim_strsave(text);
885
886 // need to allocate the line now
887 if (newtext == NULL)
888 return;
Bram Moolenaarfa4873c2022-06-30 22:13:59 +0100889 if (buf->b_ml.ml_flags & ML_ALLOCATED)
890 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100891 buf->b_ml.ml_line_ptr = newtext;
892 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
893 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100894 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100895 }
896 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100897 if (did_clear)
898 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100899}
900
901/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100902 * prop_find({props} [, {direction}])
903 */
904 void
905f_prop_find(typval_T *argvars, typval_T *rettv)
906{
907 pos_T *cursor = &curwin->w_cursor;
908 dict_T *dict;
909 buf_T *buf = curbuf;
910 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200911 int lnum_start;
912 int start_pos_has_prop = 0;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100913 int seen_end = FALSE;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200914 int id = 0;
915 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200916 int type_id = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100917 int skipstart = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200918 int lnum = -1;
919 int col = -1;
LemonBoy9bd3ce22022-04-18 21:54:02 +0100920 int dir = FORWARD; // FORWARD == 1, BACKWARD == -1
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100921 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100922
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200923 if (in_vim9script()
924 && (check_for_dict_arg(argvars, 0) == FAIL
925 || check_for_opt_string_arg(argvars, 1) == FAIL))
926 return;
927
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100928 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
929 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000930 emsg(_(e_dictionary_required));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100931 return;
932 }
933 dict = argvars[0].vval.v_dict;
934
935 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
936 return;
937 if (buf->b_ml.ml_mfp == NULL)
938 return;
939
940 if (argvars[1].v_type != VAR_UNKNOWN)
941 {
942 char_u *dir_s = tv_get_string(&argvars[1]);
943
944 if (*dir_s == 'b')
LemonBoy9bd3ce22022-04-18 21:54:02 +0100945 dir = BACKWARD;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100946 else if (*dir_s != 'f')
947 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000948 emsg(_(e_invalid_argument));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100949 return;
950 }
951 }
952
953 di = dict_find(dict, (char_u *)"lnum", -1);
954 if (di != NULL)
955 lnum = tv_get_number(&di->di_tv);
956
957 di = dict_find(dict, (char_u *)"col", -1);
958 if (di != NULL)
959 col = tv_get_number(&di->di_tv);
960
961 if (lnum == -1)
962 {
963 lnum = cursor->lnum;
964 col = cursor->col + 1;
965 }
966 else if (col == -1)
967 col = 1;
968
969 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
970 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200971 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100972 return;
973 }
974
Bram Moolenaard61efa52022-07-23 09:52:04 +0100975 skipstart = dict_get_bool(dict, "skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100976
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100977 if (dict_has_key(dict, "id"))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200978 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100979 id = dict_get_number(dict, "id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200980 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200981 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100982 if (dict_has_key(dict, "type"))
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100983 {
Bram Moolenaard61efa52022-07-23 09:52:04 +0100984 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100985 proptype_T *type = lookup_prop_type(name, buf);
986
987 if (type == NULL)
988 return;
989 type_id = type->pt_id;
990 }
Bram Moolenaard61efa52022-07-23 09:52:04 +0100991 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200992 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100993 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000994 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100995 return;
996 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200997 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100998 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000999 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001000 return;
1001 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001002
1003 lnum_start = lnum;
1004
1005 if (rettv_dict_alloc(rettv) == FAIL)
1006 return;
1007
1008 while (1)
1009 {
1010 char_u *text = ml_get_buf(buf, lnum, FALSE);
1011 size_t textlen = STRLEN(text) + 1;
1012 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001013 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001014 int i;
1015 textprop_T prop;
1016 int prop_start;
1017 int prop_end;
1018
LemonBoy9bd3ce22022-04-18 21:54:02 +01001019 for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001020 {
1021 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
LemonBoy9bd3ce22022-04-18 21:54:02 +01001022 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001023
LemonBoy9bd3ce22022-04-18 21:54:02 +01001024 // For the very first line try to find the first property before or
1025 // after `col`, depending on the search direction.
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001026 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001027 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001028 if (dir == BACKWARD)
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001029 {
LemonBoy9bd3ce22022-04-18 21:54:02 +01001030 if (prop.tp_col > col)
1031 continue;
Bram Moolenaar346f18e2020-03-13 21:36:40 +01001032 }
1033 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
1034 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +01001035 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +01001036 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +02001037 : (id_found && prop.tp_id == id)
1038 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001039 {
1040 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +01001041 if (lnum_start == lnum
1042 && col >= prop.tp_col
1043 && (col <= prop.tp_col + prop.tp_len
1044 - (prop.tp_len != 0)))
1045 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001046
LemonBoy9bd3ce22022-04-18 21:54:02 +01001047 // The property was not continued from last line, it starts on
1048 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001049 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001050 // The property does not continue on the next line, it ends on
1051 // this line.
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001052 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
LemonBoy9bd3ce22022-04-18 21:54:02 +01001053 if (!prop_start && prop_end && dir == FORWARD)
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001054 seen_end = 1;
1055
1056 // Skip lines without the start flag.
1057 if (!prop_start)
1058 {
1059 // Always search backwards for start when search started
1060 // on a prop and we're not skipping.
1061 if (start_pos_has_prop && !skipstart)
LemonBoy9bd3ce22022-04-18 21:54:02 +01001062 dir = BACKWARD;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001063 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001064 }
1065
1066 // If skipstart is true, skip the prop at start pos (even if
1067 // continued from another line).
1068 if (start_pos_has_prop && skipstart && !seen_end)
1069 {
1070 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +02001071 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001072 }
1073
Bram Moolenaare05a89a2020-01-10 19:56:46 +01001074 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
1075 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
1076
1077 return;
1078 }
1079 }
1080
1081 if (dir > 0)
1082 {
1083 if (lnum >= buf->b_ml.ml_line_count)
1084 break;
1085 lnum++;
1086 }
1087 else
1088 {
1089 if (lnum <= 1)
1090 break;
1091 lnum--;
1092 }
1093 }
1094}
1095
1096/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001097 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
1098 */
1099 static int
1100prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
1101{
1102 int i;
1103
1104 for (i = 0; i < len; i++)
1105 if (types_or_ids[i] == type_or_id)
1106 return TRUE;
1107
1108 return FALSE;
1109}
1110
1111/*
1112 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
1113 * If 'prop_types' is not NULL, then return only the text properties with
1114 * matching property type in the 'prop_types' array.
1115 * If 'prop_ids' is not NULL, then return only the text properties with
1116 * an identifier in the 'props_ids' array.
1117 * If 'add_lnum' is TRUE, then add the line number also to the text property
1118 * dictionary.
1119 */
1120 static void
1121get_props_in_line(
1122 buf_T *buf,
1123 linenr_T lnum,
1124 int *prop_types,
1125 int prop_types_len,
1126 int *prop_ids,
1127 int prop_ids_len,
1128 list_T *retlist,
1129 int add_lnum)
1130{
1131 char_u *text = ml_get_buf(buf, lnum, FALSE);
1132 size_t textlen = STRLEN(text) + 1;
1133 int count;
1134 int i;
1135 textprop_T prop;
1136
1137 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
1138 for (i = 0; i < count; ++i)
1139 {
1140 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
1141 sizeof(textprop_T));
1142 if ((prop_types == NULL
1143 || prop_type_or_id_in_list(prop_types, prop_types_len,
1144 prop.tp_type))
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01001145 && (prop_ids == NULL
1146 || prop_type_or_id_in_list(prop_ids, prop_ids_len,
1147 prop.tp_id)))
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001148 {
1149 dict_T *d = dict_alloc();
1150
1151 if (d == NULL)
1152 break;
1153 prop_fill_dict(d, &prop, buf);
1154 if (add_lnum)
1155 dict_add_number(d, "lnum", lnum);
1156 list_append_dict(retlist, d);
1157 }
1158 }
1159}
1160
1161/*
1162 * Convert a List of property type names into an array of property type
1163 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1164 * error. 'num_types' is set to the number of returned property types.
1165 */
1166 static int *
1167get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
1168{
1169 int *prop_types;
1170 listitem_T *li;
1171 int i;
1172 char_u *name;
1173 proptype_T *type;
1174
1175 *num_types = 0;
1176
1177 prop_types = ALLOC_MULT(int, list_len(l));
1178 if (prop_types == NULL)
1179 return NULL;
1180
1181 i = 0;
1182 FOR_ALL_LIST_ITEMS(l, li)
1183 {
1184 if (li->li_tv.v_type != VAR_STRING)
1185 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001186 emsg(_(e_string_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001187 goto errret;
1188 }
1189 name = li->li_tv.vval.v_string;
1190 if (name == NULL)
1191 goto errret;
1192
1193 type = lookup_prop_type(name, buf);
1194 if (type == NULL)
1195 goto errret;
1196 prop_types[i++] = type->pt_id;
1197 }
1198
1199 *num_types = i;
1200 return prop_types;
1201
1202errret:
1203 VIM_CLEAR(prop_types);
1204 return NULL;
1205}
1206
1207/*
1208 * Convert a List of property identifiers into an array of property
1209 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1210 * error. 'num_ids' is set to the number of returned property identifiers.
1211 */
1212 static int *
1213get_prop_ids_from_list(list_T *l, int *num_ids)
1214{
1215 int *prop_ids;
1216 listitem_T *li;
1217 int i;
1218 int id;
1219 int error;
1220
1221 *num_ids = 0;
1222
1223 prop_ids = ALLOC_MULT(int, list_len(l));
1224 if (prop_ids == NULL)
1225 return NULL;
1226
1227 i = 0;
1228 FOR_ALL_LIST_ITEMS(l, li)
1229 {
1230 error = FALSE;
1231 id = tv_get_number_chk(&li->li_tv, &error);
1232 if (error)
1233 goto errret;
1234
1235 prop_ids[i++] = id;
1236 }
1237
1238 *num_ids = i;
1239 return prop_ids;
1240
1241errret:
1242 VIM_CLEAR(prop_ids);
1243 return NULL;
1244}
1245
1246/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001247 * prop_list({lnum} [, {bufnr}])
1248 */
1249 void
1250f_prop_list(typval_T *argvars, typval_T *rettv)
1251{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001252 linenr_T lnum;
1253 linenr_T start_lnum;
1254 linenr_T end_lnum;
1255 buf_T *buf = curbuf;
1256 int add_lnum = FALSE;
1257 int *prop_types = NULL;
1258 int prop_types_len = 0;
1259 int *prop_ids = NULL;
1260 int prop_ids_len = 0;
1261 list_T *l;
1262 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001263
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001264 if (in_vim9script()
1265 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001266 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001267 return;
1268
Bram Moolenaar93a10962022-06-16 11:42:09 +01001269 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001270 return;
1271
1272 // default: get text properties on current line
1273 start_lnum = tv_get_number(&argvars[0]);
1274 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001275 if (argvars[1].v_type != VAR_UNKNOWN)
1276 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001277 dict_T *d;
1278
1279 if (argvars[1].v_type != VAR_DICT)
1280 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001281 emsg(_(e_dictionary_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001282 return;
1283 }
1284 d = argvars[1].vval.v_dict;
1285
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001286 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1287 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001288
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001289 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001290 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001291 if (di->di_tv.v_type != VAR_NUMBER)
1292 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001293 emsg(_(e_number_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001294 return;
1295 }
1296 end_lnum = tv_get_number(&di->di_tv);
1297 if (end_lnum < 0)
1298 // negative end_lnum is used as an offset from the last buffer
1299 // line
1300 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1301 else if (end_lnum > buf->b_ml.ml_line_count)
1302 end_lnum = buf->b_ml.ml_line_count;
1303 add_lnum = TRUE;
1304 }
1305 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1306 {
1307 if (di->di_tv.v_type != VAR_LIST)
1308 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001309 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001310 return;
1311 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001312
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001313 l = di->di_tv.vval.v_list;
1314 if (l != NULL && list_len(l) > 0)
1315 {
1316 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1317 if (prop_types == NULL)
1318 return;
1319 }
1320 }
1321 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1322 {
1323 if (di->di_tv.v_type != VAR_LIST)
1324 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001325 emsg(_(e_list_required));
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001326 goto errret;
1327 }
1328
1329 l = di->di_tv.vval.v_list;
1330 if (l != NULL && list_len(l) > 0)
1331 {
1332 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1333 if (prop_ids == NULL)
1334 goto errret;
1335 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001336 }
1337 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001338 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1339 || end_lnum < 1 || end_lnum < start_lnum)
1340 emsg(_(e_invalid_range));
1341 else
1342 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1343 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1344 prop_ids, prop_ids_len,
1345 rettv->vval.v_list, add_lnum);
1346
1347errret:
1348 VIM_CLEAR(prop_types);
1349 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001350}
1351
1352/*
1353 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1354 */
1355 void
1356f_prop_remove(typval_T *argvars, typval_T *rettv)
1357{
1358 linenr_T start = 1;
1359 linenr_T end = 0;
1360 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001361 linenr_T first_changed = 0;
1362 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001363 dict_T *dict;
1364 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001365 int do_all;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001366 int id = -MAXCOL;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001367 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001368 int both;
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001369 int did_remove_text = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001370
1371 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001372
1373 if (in_vim9script()
1374 && (check_for_dict_arg(argvars, 0) == FAIL
1375 || check_for_opt_number_arg(argvars, 1) == FAIL
1376 || (argvars[1].v_type != VAR_UNKNOWN
1377 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1378 return;
1379
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001380 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1381 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001382 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001383 return;
1384 }
1385
1386 if (argvars[1].v_type != VAR_UNKNOWN)
1387 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001388 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001389 end = start;
1390 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001391 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001392 if (start < 1 || end < 1)
1393 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001394 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001395 return;
1396 }
1397 }
1398
1399 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001400 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1401 return;
1402 if (buf->b_ml.ml_mfp == NULL)
1403 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001404
Bram Moolenaard61efa52022-07-23 09:52:04 +01001405 do_all = dict_get_bool(dict, "all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001406
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001407 if (dict_has_key(dict, "id"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01001408 id = dict_get_number(dict, "id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001409 if (dict_has_key(dict, "type"))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001410 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01001411 char_u *name = dict_get_string(dict, "type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001412 proptype_T *type = lookup_prop_type(name, buf);
1413
1414 if (type == NULL)
1415 return;
1416 type_id = type->pt_id;
1417 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01001418 both = dict_get_bool(dict, "both", FALSE);
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001419
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001420 if (id == -MAXCOL && type_id == -1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001421 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001422 emsg(_(e_need_at_least_one_of_id_or_type));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001423 return;
1424 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001425 if (both && (id == -MAXCOL || type_id == -1))
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001426 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001427 emsg(_(e_need_id_and_type_with_both));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001428 return;
1429 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001430
1431 if (end == 0)
1432 end = buf->b_ml.ml_line_count;
1433 for (lnum = start; lnum <= end; ++lnum)
1434 {
1435 char_u *text;
1436 size_t len;
1437
1438 if (lnum > buf->b_ml.ml_line_count)
1439 break;
1440 text = ml_get_buf(buf, lnum, FALSE);
1441 len = STRLEN(text) + 1;
1442 if ((size_t)buf->b_ml.ml_line_len > len)
1443 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001444 static textprop_T textprop; // static because of alignment
1445 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001446
1447 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1448 / sizeof(textprop_T); ++idx)
1449 {
1450 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1451 + idx * sizeof(textprop_T);
1452 size_t taillen;
1453
1454 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001455 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1456 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001457 {
1458 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1459 {
1460 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1461
1462 // need to allocate the line to be able to change it
1463 if (newptr == NULL)
1464 return;
1465 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1466 buf->b_ml.ml_line_len);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001467 if (buf->b_ml.ml_flags & ML_ALLOCATED)
1468 vim_free(buf->b_ml.ml_line_ptr);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001469 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001470 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1471
1472 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001473 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001474 }
1475
1476 taillen = buf->b_ml.ml_line_len - len
1477 - (idx + 1) * sizeof(textprop_T);
1478 if (taillen > 0)
1479 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1480 taillen);
1481 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1482 --idx;
1483
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001484 if (textprop.tp_id < 0)
1485 {
1486 garray_T *gap = &buf->b_textprop_text;
1487 int ii = -textprop.tp_id - 1;
1488
1489 // negative ID: property with text - free the text
1490 if (ii < gap->ga_len)
1491 {
1492 char_u **p = ((char_u **)gap->ga_data) + ii;
1493 vim_free(*p);
1494 *p = NULL;
1495 did_remove_text = TRUE;
1496 }
1497 }
1498
Bram Moolenaar965c0442021-05-17 00:22:06 +02001499 if (first_changed == 0)
1500 first_changed = lnum;
1501 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001502 ++rettv->vval.v_number;
1503 if (!do_all)
1504 break;
1505 }
1506 }
1507 }
1508 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001509
Bram Moolenaar965c0442021-05-17 00:22:06 +02001510 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001511 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001512 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1513 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001514 }
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01001515
1516 if (did_remove_text)
1517 {
1518 garray_T *gap = &buf->b_textprop_text;
1519
1520 // Reduce the growarray size for NULL pointers at the end.
1521 while (gap->ga_len > 0
1522 && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL)
1523 --gap->ga_len;
1524 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001525}
1526
1527/*
1528 * Common for f_prop_type_add() and f_prop_type_change().
1529 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001530 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001531prop_type_set(typval_T *argvars, int add)
1532{
1533 char_u *name;
1534 buf_T *buf = NULL;
1535 dict_T *dict;
1536 dictitem_T *di;
1537 proptype_T *prop;
1538
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001539 if (in_vim9script()
1540 && (check_for_string_arg(argvars, 0) == FAIL
1541 || check_for_dict_arg(argvars, 1) == FAIL))
1542 return;
1543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001544 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001545 if (*name == NUL)
1546 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001547 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001548 return;
1549 }
1550
1551 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1552 return;
1553 dict = argvars[1].vval.v_dict;
1554
Bram Moolenaare44336b2022-08-07 18:20:08 +01001555 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001556 if (add)
1557 {
1558 hashtab_T **htp;
1559
1560 if (prop != NULL)
1561 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001562 semsg(_(e_property_type_str_already_defined), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001563 return;
1564 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001565 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001566 if (prop == NULL)
1567 return;
1568 STRCPY(prop->pt_name, name);
1569 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001570 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001571 if (buf == NULL)
1572 {
1573 htp = &global_proptypes;
1574 VIM_CLEAR(global_proparray);
1575 }
1576 else
1577 {
1578 htp = &buf->b_proptypes;
1579 VIM_CLEAR(buf->b_proparray);
1580 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001581 if (*htp == NULL)
1582 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001583 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001584 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001585 {
1586 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001587 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001588 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001589 hash_init(*htp);
1590 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001591 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001592 }
1593 else
1594 {
1595 if (prop == NULL)
1596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001597 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001598 return;
1599 }
1600 }
1601
1602 if (dict != NULL)
1603 {
1604 di = dict_find(dict, (char_u *)"highlight", -1);
1605 if (di != NULL)
1606 {
1607 char_u *highlight;
1608 int hl_id = 0;
1609
Bram Moolenaard61efa52022-07-23 09:52:04 +01001610 highlight = dict_get_string(dict, "highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001611 if (highlight != NULL && *highlight != NUL)
1612 hl_id = syn_name2id(highlight);
1613 if (hl_id <= 0)
1614 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001615 semsg(_(e_unknown_highlight_group_name_str),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001616 highlight == NULL ? (char_u *)"" : highlight);
1617 return;
1618 }
1619 prop->pt_hl_id = hl_id;
1620 }
1621
Bram Moolenaar58187f12019-05-05 16:33:47 +02001622 di = dict_find(dict, (char_u *)"combine", -1);
1623 if (di != NULL)
1624 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001625 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001626 prop->pt_flags |= PT_FLAG_COMBINE;
1627 else
1628 prop->pt_flags &= ~PT_FLAG_COMBINE;
1629 }
1630
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001631 di = dict_find(dict, (char_u *)"override", -1);
1632 if (di != NULL)
1633 {
1634 if (tv_get_bool(&di->di_tv))
1635 prop->pt_flags |= PT_FLAG_OVERRIDE;
1636 else
1637 prop->pt_flags &= ~PT_FLAG_OVERRIDE;
1638 }
1639
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001640 di = dict_find(dict, (char_u *)"priority", -1);
1641 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001642 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001643
1644 di = dict_find(dict, (char_u *)"start_incl", -1);
1645 if (di != NULL)
1646 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001647 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001648 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1649 else
1650 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1651 }
1652
1653 di = dict_find(dict, (char_u *)"end_incl", -1);
1654 if (di != NULL)
1655 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001656 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001657 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1658 else
1659 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1660 }
1661 }
1662}
1663
1664/*
1665 * prop_type_add({name}, {props})
1666 */
1667 void
1668f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1669{
1670 prop_type_set(argvars, TRUE);
1671}
1672
1673/*
1674 * prop_type_change({name}, {props})
1675 */
1676 void
1677f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1678{
1679 prop_type_set(argvars, FALSE);
1680}
1681
1682/*
1683 * prop_type_delete({name} [, {bufnr}])
1684 */
1685 void
1686f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1687{
1688 char_u *name;
1689 buf_T *buf = NULL;
1690 hashitem_T *hi;
1691
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001692 if (in_vim9script()
1693 && (check_for_string_arg(argvars, 0) == FAIL
1694 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1695 return;
1696
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001697 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001698 if (*name == NUL)
1699 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001700 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001701 return;
1702 }
1703
1704 if (argvars[1].v_type != VAR_UNKNOWN)
1705 {
1706 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1707 return;
1708 }
1709
Bram Moolenaare44336b2022-08-07 18:20:08 +01001710 hi = find_prop_type_hi(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001711 if (hi != NULL)
1712 {
1713 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001714 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001715
1716 if (buf == NULL)
Bram Moolenaare44336b2022-08-07 18:20:08 +01001717 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001718 ht = global_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001719 VIM_CLEAR(global_proparray);
1720 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001721 else
Bram Moolenaare44336b2022-08-07 18:20:08 +01001722 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001723 ht = buf->b_proptypes;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001724 VIM_CLEAR(buf->b_proparray);
1725 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001726 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001727 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001728 }
1729}
1730
1731/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001732 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001733 */
1734 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001735f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001736{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001737 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001738
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001739 if (in_vim9script()
1740 && (check_for_string_arg(argvars, 0) == FAIL
1741 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1742 return;
1743
1744 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001745 if (*name == NUL)
1746 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001747 emsg(_(e_invalid_argument));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001748 return;
1749 }
1750 if (rettv_dict_alloc(rettv) == OK)
1751 {
1752 proptype_T *prop = NULL;
1753 buf_T *buf = NULL;
1754
1755 if (argvars[1].v_type != VAR_UNKNOWN)
1756 {
1757 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1758 return;
1759 }
1760
Bram Moolenaare44336b2022-08-07 18:20:08 +01001761 prop = find_prop_type(name, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001762 if (prop != NULL)
1763 {
1764 dict_T *d = rettv->vval.v_dict;
1765
1766 if (prop->pt_hl_id > 0)
1767 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1768 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001769 dict_add_number(d, "combine",
1770 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001771 dict_add_number(d, "start_incl",
1772 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1773 dict_add_number(d, "end_incl",
1774 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1775 if (buf != NULL)
1776 dict_add_number(d, "bufnr", buf->b_fnum);
1777 }
1778 }
1779}
1780
1781 static void
1782list_types(hashtab_T *ht, list_T *l)
1783{
1784 long todo;
1785 hashitem_T *hi;
1786
1787 todo = (long)ht->ht_used;
1788 for (hi = ht->ht_array; todo > 0; ++hi)
1789 {
1790 if (!HASHITEM_EMPTY(hi))
1791 {
1792 proptype_T *prop = HI2PT(hi);
1793
1794 list_append_string(l, prop->pt_name, -1);
1795 --todo;
1796 }
1797 }
1798}
1799
1800/*
1801 * prop_type_list([{bufnr}])
1802 */
1803 void
1804f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1805{
1806 buf_T *buf = NULL;
1807
1808 if (rettv_list_alloc(rettv) == OK)
1809 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001810 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1811 return;
1812
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001813 if (argvars[0].v_type != VAR_UNKNOWN)
1814 {
1815 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1816 return;
1817 }
1818 if (buf == NULL)
1819 {
1820 if (global_proptypes != NULL)
1821 list_types(global_proptypes, rettv->vval.v_list);
1822 }
1823 else if (buf->b_proptypes != NULL)
1824 list_types(buf->b_proptypes, rettv->vval.v_list);
1825 }
1826}
1827
1828/*
1829 * Free all property types in "ht".
1830 */
1831 static void
1832clear_ht_prop_types(hashtab_T *ht)
1833{
1834 long todo;
1835 hashitem_T *hi;
1836
1837 if (ht == NULL)
1838 return;
1839
1840 todo = (long)ht->ht_used;
1841 for (hi = ht->ht_array; todo > 0; ++hi)
1842 {
1843 if (!HASHITEM_EMPTY(hi))
1844 {
1845 proptype_T *prop = HI2PT(hi);
1846
1847 vim_free(prop);
1848 --todo;
1849 }
1850 }
1851
1852 hash_clear(ht);
1853 vim_free(ht);
1854}
1855
1856#if defined(EXITFREE) || defined(PROTO)
1857/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001858 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001859 */
1860 void
1861clear_global_prop_types(void)
1862{
1863 clear_ht_prop_types(global_proptypes);
1864 global_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001865 VIM_CLEAR(global_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001866}
1867#endif
1868
1869/*
1870 * Free all property types for "buf".
1871 */
1872 void
1873clear_buf_prop_types(buf_T *buf)
1874{
1875 clear_ht_prop_types(buf->b_proptypes);
1876 buf->b_proptypes = NULL;
Bram Moolenaare44336b2022-08-07 18:20:08 +01001877 VIM_CLEAR(buf->b_proparray);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001878}
1879
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001880// Struct used to return two values from adjust_prop().
1881typedef struct
1882{
1883 int dirty; // if the property was changed
1884 int can_drop; // whether after this change, the prop may be removed
1885} adjustres_T;
1886
1887/*
1888 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1889 *
1890 * Note that "col" is zero-based, while tp_col is one-based.
1891 * Only for the current buffer.
1892 * "flags" can have:
1893 * APC_SUBSTITUTE: Text is replaced, not inserted.
1894 */
1895 static adjustres_T
1896adjust_prop(
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001897 textprop_T *prop,
1898 colnr_T col,
1899 int added,
1900 int flags)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001901{
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001902 proptype_T *pt;
1903 int start_incl;
1904 int end_incl;
1905 int droppable;
1906 adjustres_T res = {TRUE, FALSE};
1907
1908 // prop after end of the line doesn't move
1909 if (prop->tp_col == MAXCOL)
1910 {
1911 res.dirty = FALSE;
1912 return res;
1913 }
1914
1915 pt = text_prop_type_by_id(curbuf, prop->tp_type);
1916 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001917 || (flags & APC_SUBSTITUTE)
1918 || (prop->tp_flags & TP_FLAG_CONT_PREV);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001919 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
LemonBoy698cb4c2022-05-14 18:10:15 +01001920 || (prop->tp_flags & TP_FLAG_CONT_NEXT);
Bram Moolenaarb7963df2022-07-31 17:12:43 +01001921 // do not drop zero-width props if they later can increase in size
1922 droppable = !(start_incl || end_incl);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001923
1924 if (added > 0)
1925 {
1926 if (col + 1 <= prop->tp_col
1927 - (start_incl || (prop->tp_len == 0 && end_incl)))
1928 // Change is entirely before the text property: Only shift
1929 prop->tp_col += added;
1930 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1931 // Insertion was inside text property
1932 prop->tp_len += added;
1933 }
1934 else if (prop->tp_col > col + 1)
1935 {
1936 if (prop->tp_col + added < col + 1)
1937 {
1938 prop->tp_len += (prop->tp_col - 1 - col) + added;
1939 prop->tp_col = col + 1;
1940 if (prop->tp_len <= 0)
1941 {
1942 prop->tp_len = 0;
1943 res.can_drop = droppable;
1944 }
1945 }
1946 else
1947 prop->tp_col += added;
1948 }
1949 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1950 {
1951 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1952
1953 prop->tp_len += after > 0 ? added + after : added;
1954 res.can_drop = prop->tp_len <= 0 && droppable;
1955 }
1956 else
1957 res.dirty = FALSE;
1958
1959 return res;
1960}
1961
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001962/*
1963 * Adjust the columns of text properties in line "lnum" after position "col" to
1964 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001965 * Note that "col" is zero-based, while tp_col is one-based.
1966 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001967 * "flags" can have:
1968 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1969 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001970 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001971 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001972 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001973 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001974adjust_prop_columns(
1975 linenr_T lnum,
1976 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001977 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001978 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001979{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001980 int proplen;
1981 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001982 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001983 int ri, wi;
1984 size_t textlen;
1985
1986 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001987 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001988
1989 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1990 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001991 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001992 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001993
Bram Moolenaar196d1572019-01-02 23:47:18 +01001994 wi = 0; // write index
1995 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001996 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001997 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001998 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001999
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002000 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
2001 res = adjust_prop(&prop, col, bytes_added, flags);
2002 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02002003 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002004 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02002005 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
2006 && u_savesub(lnum) == FAIL)
2007 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002008 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02002009
2010 // u_savesub() may have updated curbuf->b_ml, fetch it again
2011 if (curbuf->b_ml.ml_line_lnum != lnum)
2012 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002013 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002014 if (res.can_drop)
2015 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01002016 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01002017 ++wi;
2018 }
2019 if (dirty)
2020 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01002021 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
2022
2023 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002024 {
2025 char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
2026
2027 if (curbuf->b_ml.ml_flags & ML_ALLOCATED)
2028 vim_free(curbuf->b_ml.ml_line_ptr);
2029 curbuf->b_ml.ml_line_ptr = p;
2030 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01002031 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01002032 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002033 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02002034 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01002035}
2036
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002037/*
2038 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002039 * "lnum_props" is the line that has the properties from before the split.
2040 * "lnum_top" is the top line.
2041 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002042 * "deleted" is the number of bytes deleted.
2043 */
2044 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002045adjust_props_for_split(
2046 linenr_T lnum_props,
2047 linenr_T lnum_top,
2048 int kept,
2049 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002050{
2051 char_u *props;
2052 int count;
2053 garray_T prevprop;
2054 garray_T nextprop;
2055 int i;
2056 int skipped = kept + deleted;
2057
2058 if (!curbuf->b_has_textprop)
2059 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002060
2061 // Get the text properties from "lnum_props".
2062 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002063 ga_init2(&prevprop, sizeof(textprop_T), 10);
2064 ga_init2(&nextprop, sizeof(textprop_T), 10);
2065
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002066 // Keep the relevant ones in the first line, reducing the length if needed.
2067 // Copy the ones that include the split to the second line.
2068 // Move the ones after the split to the second line.
2069 for (i = 0; i < count; ++i)
2070 {
2071 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002072 proptype_T *pt;
2073 int start_incl, end_incl;
2074 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002075
2076 // copy the prop to an aligned structure
2077 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
2078
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002079 pt = text_prop_type_by_id(curbuf, prop.tp_type);
2080 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
2081 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002082 cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept;
2083 cont_next = prop.tp_col != MAXCOL
2084 && skipped <= prop.tp_col + prop.tp_len - !end_incl;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002085
2086 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002087 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002088 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
2089
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002090 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002091 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002092 if (p->tp_col + p->tp_len >= kept)
2093 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002094 if (cont_next)
2095 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002096 }
2097
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02002098 // Only add the property to the next line if the length is bigger than
2099 // zero.
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002100 if ((cont_next || prop.tp_col == MAXCOL)
2101 && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002102 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002103 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002104
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002105 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002106 ++nextprop.ga_len;
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002107 if (p->tp_col != MAXCOL)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002108 {
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002109 if (p->tp_col > skipped)
2110 p->tp_col -= skipped - 1;
2111 else
2112 {
2113 p->tp_len -= skipped - p->tp_col;
2114 p->tp_col = 1;
2115 }
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002116 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002117 if (cont_prev)
2118 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002119 }
2120 }
2121
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002122 set_text_props(lnum_top, prevprop.ga_data,
2123 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002124 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002125 set_text_props(lnum_top + 1, nextprop.ga_data,
2126 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01002127 ga_clear(&nextprop);
2128}
2129
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002130/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002131 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002132 */
2133 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002134prepend_joined_props(
2135 char_u *new_props,
2136 int propcount,
2137 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002138 linenr_T lnum,
Bram Moolenaare175dc62022-08-01 22:18:50 +01002139 int last_line,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002140 long col,
2141 int removed)
2142{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002143 char_u *props;
2144 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
2145 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002146
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002147 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002148 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002149 textprop_T prop;
2150 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002151
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002152 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
Bram Moolenaare175dc62022-08-01 22:18:50 +01002153 if (prop.tp_col == MAXCOL && !last_line)
2154 continue; // drop property with text after the line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002155 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
2156
2157 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002158 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002159
Bram Moolenaare175dc62022-08-01 22:18:50 +01002160 if (last_line || end)
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002161 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
2162 &prop, sizeof(prop));
2163 else
2164 {
2165 int j;
2166 int found = FALSE;
2167
2168 // Search for continuing prop.
2169 for (j = *props_remaining; j < propcount; ++j)
2170 {
2171 textprop_T op;
2172
2173 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
2174 if ((op.tp_flags & TP_FLAG_CONT_PREV)
2175 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002176 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002177 found = TRUE;
2178 op.tp_len += op.tp_col - prop.tp_col;
2179 op.tp_col = prop.tp_col;
2180 // Start/end is taken care of when deleting joined lines
2181 op.tp_flags = prop.tp_flags;
2182 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
2183 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002184 }
2185 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002186 if (!found)
2187 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002188 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002189 }
2190}
2191
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002192#endif // FEAT_PROP_POPUP