blob: beb82a35b5119224c97646c622feddec1a7f039d [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 Moolenaarb9c67a52019-01-01 19:49:20 +010014 * - Adjust text property column and length when text is inserted/deleted.
Bram Moolenaar4164bb22019-01-04 23:09:49 +010015 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010016 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020017 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010018 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaar32aa1022019-11-02 22:54:41 +010019 * - Add an array for global_proptypes, to quickly lookup a prop type by ID
20 * - Add an array for b_proptypes, to quickly lookup a prop type by ID
Bram Moolenaarb56ac042018-12-28 23:22:40 +010021 * - Checking the text length to detect text properties is slow. Use a flag in
22 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010023 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
24 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010025 * - Perhaps have a window-local option to disable highlighting from text
26 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010027 */
28
29#include "vim.h"
30
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010031#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33/*
34 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
35 * This avoids adding a pointer to the hashtable item.
36 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
37 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
38 * HI2PT() converts a hashitem pointer to a proptype pointer.
39 */
40#define PT2HIKEY(p) ((p)->pt_name)
41#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
42#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
43
44// The global text property types.
45static hashtab_T *global_proptypes = NULL;
46
47// The last used text property type ID.
48static int proptype_id = 0;
49
50static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
51static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld");
Bram Moolenaare3d31b02018-12-24 23:07:04 +010052static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010053
54/*
55 * Find a property type by name, return the hashitem.
56 * Returns NULL if the item can't be found.
57 */
58 static hashitem_T *
59find_prop_hi(char_u *name, buf_T *buf)
60{
61 hashtab_T *ht;
62 hashitem_T *hi;
63
64 if (*name == NUL)
65 return NULL;
66 if (buf == NULL)
67 ht = global_proptypes;
68 else
69 ht = buf->b_proptypes;
70
71 if (ht == NULL)
72 return NULL;
73 hi = hash_find(ht, name);
74 if (HASHITEM_EMPTY(hi))
75 return NULL;
76 return hi;
77}
78
79/*
80 * Like find_prop_hi() but return the property type.
81 */
82 static proptype_T *
83find_prop(char_u *name, buf_T *buf)
84{
85 hashitem_T *hi = find_prop_hi(name, buf);
86
87 if (hi == NULL)
88 return NULL;
89 return HI2PT(hi);
90}
91
92/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020093 * Get the prop type ID of "name".
94 * When not found return zero.
95 */
96 int
97find_prop_type_id(char_u *name, buf_T *buf)
98{
99 proptype_T *pt = find_prop(name, buf);
100
101 if (pt == NULL)
102 return 0;
103 return pt->pt_id;
104}
105
106/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100107 * Lookup a property type by name. First in "buf" and when not found in the
108 * global types.
109 * When not found gives an error message and returns NULL.
110 */
111 static proptype_T *
112lookup_prop_type(char_u *name, buf_T *buf)
113{
114 proptype_T *type = find_prop(name, buf);
115
116 if (type == NULL)
117 type = find_prop(name, NULL);
118 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100119 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return type;
121}
122
123/*
124 * Get an optional "bufnr" item from the dict in "arg".
125 * When the argument is not used or "bufnr" is not present then "buf" is
126 * unchanged.
127 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100128 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 */
130 static int
131get_bufnr_from_arg(typval_T *arg, buf_T **buf)
132{
133 dictitem_T *di;
134
135 if (arg->v_type != VAR_DICT)
136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100137 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100138 return FAIL;
139 }
140 if (arg->vval.v_dict == NULL)
141 return OK; // NULL dict is like an empty dict
142 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200143 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
144 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100145 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200146 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100147 if (*buf == NULL)
148 return FAIL;
149 }
150 return OK;
151}
152
153/*
154 * prop_add({lnum}, {col}, {props})
155 */
156 void
157f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
158{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100159 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100160 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100161
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200162 if (in_vim9script()
163 && (check_for_number_arg(argvars, 0) == FAIL
164 || check_for_number_arg(argvars, 1) == FAIL
165 || check_for_dict_arg(argvars, 2) == FAIL))
166 return;
167
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100168 start_lnum = tv_get_number(&argvars[0]);
169 start_col = tv_get_number(&argvars[1]);
170 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100172 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100173 return;
174 }
175 if (argvars[2].v_type != VAR_DICT)
176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100177 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100178 return;
179 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200180
181 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
182 curbuf, &argvars[2]);
183}
184
185/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200186 * Attach a text property 'type_name' to the text starting
187 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
188 * the buffer 'buf' and assign identifier 'id'.
189 */
190 static int
191prop_add_one(
192 buf_T *buf,
193 char_u *type_name,
194 int id,
195 linenr_T start_lnum,
196 linenr_T end_lnum,
197 colnr_T start_col,
198 colnr_T end_col)
199{
200 proptype_T *type;
201 linenr_T lnum;
202 int proplen;
203 char_u *props = NULL;
204 char_u *newprops;
205 size_t textlen;
206 char_u *newtext;
207 int i;
208 textprop_T tmp_prop;
209
210 type = lookup_prop_type(type_name, buf);
211 if (type == NULL)
212 return FAIL;
213
214 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
215 {
216 semsg(_(e_invalid_lnum), (long)start_lnum);
217 return FAIL;
218 }
219 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
220 {
221 semsg(_(e_invalid_lnum), (long)end_lnum);
222 return FAIL;
223 }
224
225 if (buf->b_ml.ml_mfp == NULL)
226 {
227 emsg(_("E275: Cannot add text property to unloaded buffer"));
228 return FAIL;
229 }
230
231 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
232 {
233 colnr_T col; // start column
234 long length; // in bytes
235
236 // Fetch the line to get the ml_line_len field updated.
237 proplen = get_text_props(buf, lnum, &props, TRUE);
238 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
239
240 if (lnum == start_lnum)
241 col = start_col;
242 else
243 col = 1;
244 if (col - 1 > (colnr_T)textlen)
245 {
246 semsg(_(e_invalid_col), (long)start_col);
247 return FAIL;
248 }
249
250 if (lnum == end_lnum)
251 length = end_col - col;
252 else
253 length = (int)textlen - col + 1;
254 if (length > (long)textlen)
255 length = (int)textlen; // can include the end-of-line
256 if (length < 0)
257 length = 0; // zero-width property
258
259 // Allocate the new line with space for the new property.
260 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
261 if (newtext == NULL)
262 return FAIL;
263 // Copy the text, including terminating NUL.
264 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
265
266 // Find the index where to insert the new property.
267 // Since the text properties are not aligned properly when stored with
268 // the text, we need to copy them as bytes before using it as a struct.
269 for (i = 0; i < proplen; ++i)
270 {
271 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
272 sizeof(textprop_T));
273 if (tmp_prop.tp_col >= col)
274 break;
275 }
276 newprops = newtext + textlen;
277 if (i > 0)
278 mch_memmove(newprops, props, sizeof(textprop_T) * i);
279
280 tmp_prop.tp_col = col;
281 tmp_prop.tp_len = length;
282 tmp_prop.tp_id = id;
283 tmp_prop.tp_type = type->pt_id;
284 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
285 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
286 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
287 sizeof(textprop_T));
288
289 if (i < proplen)
290 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
291 props + i * sizeof(textprop_T),
292 sizeof(textprop_T) * (proplen - i));
293
294 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
295 vim_free(buf->b_ml.ml_line_ptr);
296 buf->b_ml.ml_line_ptr = newtext;
297 buf->b_ml.ml_line_len += sizeof(textprop_T);
298 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
299 }
300
301 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
302 return OK;
303}
304
305/*
306 * prop_add_list()
307 * First argument specifies the text property:
308 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
309 * Second argument is a List where each item is a List with the following
310 * entries: [lnum, start_col, end_col]
311 */
312 void
313f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
314{
315 dict_T *dict;
316 char_u *type_name;
317 buf_T *buf = curbuf;
318 int id = 0;
319 listitem_T *li;
320 list_T *pos_list;
321 linenr_T start_lnum;
322 colnr_T start_col;
323 linenr_T end_lnum;
324 colnr_T end_col;
325 int error = FALSE;
326
327 if (check_for_dict_arg(argvars, 0) == FAIL
328 || check_for_list_arg(argvars, 1) == FAIL)
329 return;
330
331 if (argvars[1].vval.v_list == NULL)
332 {
333 emsg(_(e_listreq));
334 return;
335 }
336
337 dict = argvars[0].vval.v_dict;
338 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
339 {
340 emsg(_("E965: missing property type name"));
341 return;
342 }
343 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
344
345 if (dict_find(dict, (char_u *)"id", -1) != NULL)
346 id = dict_get_number(dict, (char_u *)"id");
347
348 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
349 return;
350
351 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
352 {
353 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
354 {
355 emsg(_(e_listreq));
356 return;
357 }
358
359 pos_list = li->li_tv.vval.v_list;
360 start_lnum = list_find_nr(pos_list, 0L, &error);
361 start_col = list_find_nr(pos_list, 1L, &error);
362 end_lnum = list_find_nr(pos_list, 2L, &error);
363 end_col = list_find_nr(pos_list, 3L, &error);
364 if (error || start_lnum <= 0 || start_col <= 0
365 || end_lnum <= 0 || end_col <= 0)
366 {
367 emsg(_(e_invarg));
368 return;
369 }
370 if (prop_add_one(buf, type_name, id, start_lnum, end_lnum,
371 start_col, end_col) == FAIL)
372 return;
373 }
374
375 buf->b_has_textprop = TRUE; // this is never reset
376 redraw_buf_later(buf, VALID);
377}
378
379/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200380 * Shared between prop_add() and popup_create().
381 * "dict_arg" is the function argument of a dict containing "bufnr".
382 * it is NULL for popup_create().
383 */
384 void
385prop_add_common(
386 linenr_T start_lnum,
387 colnr_T start_col,
388 dict_T *dict,
389 buf_T *default_buf,
390 typval_T *dict_arg)
391{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200392 linenr_T end_lnum;
393 colnr_T end_col;
394 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200395 buf_T *buf = default_buf;
396 int id = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100397
398 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100400 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100401 return;
402 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100403 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100404
405 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
406 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100407 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
408 if (end_lnum < start_lnum)
409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100410 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100411 return;
412 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100413 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100414 else
415 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100416
417 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100418 {
419 long length = dict_get_number(dict, (char_u *)"length");
420
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100421 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100424 return;
425 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100426 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100427 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100428 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
429 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100430 end_col = dict_get_number(dict, (char_u *)"end_col");
431 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100433 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100434 return;
435 }
436 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100437 else if (start_lnum == end_lnum)
438 end_col = start_col;
439 else
440 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100441
442 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100443 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100444
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200445 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100446 return;
447
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200448 prop_add_one(buf, type_name, id, start_lnum, end_lnum, start_col, end_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100449
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100450 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200451 redraw_buf_later(buf, VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100452}
453
454/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100455 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100456 * Returns the number of text properties and, when non-zero, a pointer to the
457 * first one in "props" (note that it is not aligned, therefore the char_u
458 * pointer).
459 */
460 int
461get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
462{
463 char_u *text;
464 size_t textlen;
465 size_t proplen;
466
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100467 // Be quick when no text property types have been defined or the buffer,
468 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200469 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100470 return 0;
471
472 // Fetch the line to get the ml_line_len field updated.
473 text = ml_get_buf(buf, lnum, will_change);
474 textlen = STRLEN(text) + 1;
475 proplen = buf->b_ml.ml_line_len - textlen;
476 if (proplen % sizeof(textprop_T) != 0)
477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100478 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100479 return 0;
480 }
481 if (proplen > 0)
482 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100483 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100484}
485
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200486/**
487 * Return the number of text properties on line "lnum" in the current buffer.
488 * When "only_starting" is true only text properties starting in this line will
489 * be considered.
490 */
491 int
492count_props(linenr_T lnum, int only_starting)
493{
494 char_u *props;
495 int proplen = get_text_props(curbuf, lnum, &props, 0);
496 int result = proplen;
497 int i;
498 textprop_T prop;
499
500 if (only_starting)
501 for (i = 0; i < proplen; ++i)
502 {
503 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
504 if (prop.tp_flags & TP_FLAG_CONT_PREV)
505 --result;
506 }
507 return result;
508}
509
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100510/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200511 * Find text property "type_id" in the visible lines of window "wp".
512 * Match "id" when it is > 0.
513 * Returns FAIL when not found.
514 */
515 int
516find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
517 linenr_T *found_lnum)
518{
519 linenr_T lnum;
520 char_u *props;
521 int count;
522 int i;
523
524 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100525 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200526 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
527 {
528 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
529 for (i = 0; i < count; ++i)
530 {
531 mch_memmove(prop, props + i * sizeof(textprop_T),
532 sizeof(textprop_T));
533 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
534 {
535 *found_lnum = lnum;
536 return OK;
537 }
538 }
539 }
540 return FAIL;
541}
542
543/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100544 * Set the text properties for line "lnum" to "props" with length "len".
545 * If "len" is zero text properties are removed, "props" is not used.
546 * Any existing text properties are dropped.
547 * Only works for the current buffer.
548 */
549 static void
550set_text_props(linenr_T lnum, char_u *props, int len)
551{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100552 char_u *text;
553 char_u *newtext;
554 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100555
556 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100557 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100558 newtext = alloc(textlen + len);
559 if (newtext == NULL)
560 return;
561 mch_memmove(newtext, text, textlen);
562 if (len > 0)
563 mch_memmove(newtext + textlen, props, len);
564 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
565 vim_free(curbuf->b_ml.ml_line_ptr);
566 curbuf->b_ml.ml_line_ptr = newtext;
567 curbuf->b_ml.ml_line_len = textlen + len;
568 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
569}
570
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100571 static proptype_T *
572find_type_by_id(hashtab_T *ht, int id)
573{
574 long todo;
575 hashitem_T *hi;
576
577 if (ht == NULL)
578 return NULL;
579
580 // TODO: Make this faster by keeping a list of types sorted on ID and use
581 // a binary search.
582
583 todo = (long)ht->ht_used;
584 for (hi = ht->ht_array; todo > 0; ++hi)
585 {
586 if (!HASHITEM_EMPTY(hi))
587 {
588 proptype_T *prop = HI2PT(hi);
589
590 if (prop->pt_id == id)
591 return prop;
592 --todo;
593 }
594 }
595 return NULL;
596}
597
598/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100599 * Fill 'dict' with text properties in 'prop'.
600 */
601 static void
602prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
603{
604 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200605 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100606
607 dict_add_number(dict, "col", prop->tp_col);
608 dict_add_number(dict, "length", prop->tp_len);
609 dict_add_number(dict, "id", prop->tp_id);
610 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
611 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200612
613 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
614 if (pt == NULL)
615 {
616 pt = find_type_by_id(global_proptypes, prop->tp_type);
617 buflocal = FALSE;
618 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100619 if (pt != NULL)
620 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200621
622 if (buflocal)
623 dict_add_number(dict, "type_bufnr", buf->b_fnum);
624 else
625 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100626}
627
628/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100629 * Find a property type by ID in "buf" or globally.
630 * Returns NULL if not found.
631 */
632 proptype_T *
633text_prop_type_by_id(buf_T *buf, int id)
634{
635 proptype_T *type;
636
637 type = find_type_by_id(buf->b_proptypes, id);
638 if (type == NULL)
639 type = find_type_by_id(global_proptypes, id);
640 return type;
641}
642
643/*
644 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
645 */
646 void
647f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
648{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200649 linenr_T start;
650 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100651 linenr_T lnum;
652 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100653 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100654
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200655 if (in_vim9script()
656 && (check_for_number_arg(argvars, 0) == FAIL
657 || check_for_opt_number_arg(argvars, 1) == FAIL
658 || (argvars[1].v_type != VAR_UNKNOWN
659 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
660 return;
661
662 start = tv_get_number(&argvars[0]);
663 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100664 if (argvars[1].v_type != VAR_UNKNOWN)
665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100666 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100667 if (argvars[2].v_type != VAR_UNKNOWN)
668 {
669 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
670 return;
671 }
672 }
673 if (start < 1 || end < 1)
674 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200675 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100676 return;
677 }
678
679 for (lnum = start; lnum <= end; ++lnum)
680 {
681 char_u *text;
682 size_t len;
683
684 if (lnum > buf->b_ml.ml_line_count)
685 break;
686 text = ml_get_buf(buf, lnum, FALSE);
687 len = STRLEN(text) + 1;
688 if ((size_t)buf->b_ml.ml_line_len > len)
689 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100690 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100691 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
692 {
693 char_u *newtext = vim_strsave(text);
694
695 // need to allocate the line now
696 if (newtext == NULL)
697 return;
698 buf->b_ml.ml_line_ptr = newtext;
699 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
700 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100701 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100702 }
703 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100704 if (did_clear)
705 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100706}
707
708/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100709 * prop_find({props} [, {direction}])
710 */
711 void
712f_prop_find(typval_T *argvars, typval_T *rettv)
713{
714 pos_T *cursor = &curwin->w_cursor;
715 dict_T *dict;
716 buf_T *buf = curbuf;
717 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200718 int lnum_start;
719 int start_pos_has_prop = 0;
720 int seen_end = 0;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200721 int id = 0;
722 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200723 int type_id = -1;
724 int skipstart = 0;
725 int lnum = -1;
726 int col = -1;
727 int dir = 1; // 1 = forward, -1 = backward
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100728 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100729
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200730 if (in_vim9script()
731 && (check_for_dict_arg(argvars, 0) == FAIL
732 || check_for_opt_string_arg(argvars, 1) == FAIL))
733 return;
734
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100735 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
736 {
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200737 emsg(_(e_dictreq));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100738 return;
739 }
740 dict = argvars[0].vval.v_dict;
741
742 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
743 return;
744 if (buf->b_ml.ml_mfp == NULL)
745 return;
746
747 if (argvars[1].v_type != VAR_UNKNOWN)
748 {
749 char_u *dir_s = tv_get_string(&argvars[1]);
750
751 if (*dir_s == 'b')
752 dir = -1;
753 else if (*dir_s != 'f')
754 {
755 emsg(_(e_invarg));
756 return;
757 }
758 }
759
760 di = dict_find(dict, (char_u *)"lnum", -1);
761 if (di != NULL)
762 lnum = tv_get_number(&di->di_tv);
763
764 di = dict_find(dict, (char_u *)"col", -1);
765 if (di != NULL)
766 col = tv_get_number(&di->di_tv);
767
768 if (lnum == -1)
769 {
770 lnum = cursor->lnum;
771 col = cursor->col + 1;
772 }
773 else if (col == -1)
774 col = 1;
775
776 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
777 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200778 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100779 return;
780 }
781
Bram Moolenaareb245562020-09-03 22:33:44 +0200782 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100783
784 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200785 {
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100786 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200787 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200788 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100789 if (dict_find(dict, (char_u *)"type", -1))
790 {
791 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
792 proptype_T *type = lookup_prop_type(name, buf);
793
794 if (type == NULL)
795 return;
796 type_id = type->pt_id;
797 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100798 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200799 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100800 {
801 emsg(_("E968: Need at least one of 'id' or 'type'"));
802 return;
803 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200804 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100805 {
806 emsg(_("E860: Need 'id' and 'type' with 'both'"));
807 return;
808 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100809
810 lnum_start = lnum;
811
812 if (rettv_dict_alloc(rettv) == FAIL)
813 return;
814
815 while (1)
816 {
817 char_u *text = ml_get_buf(buf, lnum, FALSE);
818 size_t textlen = STRLEN(text) + 1;
819 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200820 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100821 int i;
822 textprop_T prop;
823 int prop_start;
824 int prop_end;
825
826 for (i = 0; i < count; ++i)
827 {
828 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
829 sizeof(textprop_T));
830
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100831 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100832 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100833 if (dir < 0)
834 {
835 if (col < prop.tp_col)
836 break;
837 }
838 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
839 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100840 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100841 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200842 : (id_found && prop.tp_id == id)
843 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100844 {
845 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100846 if (lnum_start == lnum
847 && col >= prop.tp_col
848 && (col <= prop.tp_col + prop.tp_len
849 - (prop.tp_len != 0)))
850 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100851
852 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
853 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
854 if (!prop_start && prop_end && dir > 0)
855 seen_end = 1;
856
857 // Skip lines without the start flag.
858 if (!prop_start)
859 {
860 // Always search backwards for start when search started
861 // on a prop and we're not skipping.
862 if (start_pos_has_prop && !skipstart)
863 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200864 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100865 }
866
867 // If skipstart is true, skip the prop at start pos (even if
868 // continued from another line).
869 if (start_pos_has_prop && skipstart && !seen_end)
870 {
871 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200872 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100873 }
874
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100875 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
876 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
877
878 return;
879 }
880 }
881
882 if (dir > 0)
883 {
884 if (lnum >= buf->b_ml.ml_line_count)
885 break;
886 lnum++;
887 }
888 else
889 {
890 if (lnum <= 1)
891 break;
892 lnum--;
893 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100894 // Adjust col to indicate that we're continuing from prev/next line.
895 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100896 }
897}
898
899/*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000900 * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list.
901 */
902 static int
903prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id)
904{
905 int i;
906
907 for (i = 0; i < len; i++)
908 if (types_or_ids[i] == type_or_id)
909 return TRUE;
910
911 return FALSE;
912}
913
914/*
915 * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'.
916 * If 'prop_types' is not NULL, then return only the text properties with
917 * matching property type in the 'prop_types' array.
918 * If 'prop_ids' is not NULL, then return only the text properties with
919 * an identifier in the 'props_ids' array.
920 * If 'add_lnum' is TRUE, then add the line number also to the text property
921 * dictionary.
922 */
923 static void
924get_props_in_line(
925 buf_T *buf,
926 linenr_T lnum,
927 int *prop_types,
928 int prop_types_len,
929 int *prop_ids,
930 int prop_ids_len,
931 list_T *retlist,
932 int add_lnum)
933{
934 char_u *text = ml_get_buf(buf, lnum, FALSE);
935 size_t textlen = STRLEN(text) + 1;
936 int count;
937 int i;
938 textprop_T prop;
939
940 count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T));
941 for (i = 0; i < count; ++i)
942 {
943 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
944 sizeof(textprop_T));
945 if ((prop_types == NULL
946 || prop_type_or_id_in_list(prop_types, prop_types_len,
947 prop.tp_type))
948 && (prop_ids == NULL ||
949 prop_type_or_id_in_list(prop_ids, prop_ids_len,
950 prop.tp_id)))
951 {
952 dict_T *d = dict_alloc();
953
954 if (d == NULL)
955 break;
956 prop_fill_dict(d, &prop, buf);
957 if (add_lnum)
958 dict_add_number(d, "lnum", lnum);
959 list_append_dict(retlist, d);
960 }
961 }
962}
963
964/*
965 * Convert a List of property type names into an array of property type
966 * identifiers. Returns a pointer to the allocated array. Returns NULL on
967 * error. 'num_types' is set to the number of returned property types.
968 */
969 static int *
970get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types)
971{
972 int *prop_types;
973 listitem_T *li;
974 int i;
975 char_u *name;
976 proptype_T *type;
977
978 *num_types = 0;
979
980 prop_types = ALLOC_MULT(int, list_len(l));
981 if (prop_types == NULL)
982 return NULL;
983
984 i = 0;
985 FOR_ALL_LIST_ITEMS(l, li)
986 {
987 if (li->li_tv.v_type != VAR_STRING)
988 {
989 emsg(_(e_stringreq));
990 goto errret;
991 }
992 name = li->li_tv.vval.v_string;
993 if (name == NULL)
994 goto errret;
995
996 type = lookup_prop_type(name, buf);
997 if (type == NULL)
998 goto errret;
999 prop_types[i++] = type->pt_id;
1000 }
1001
1002 *num_types = i;
1003 return prop_types;
1004
1005errret:
1006 VIM_CLEAR(prop_types);
1007 return NULL;
1008}
1009
1010/*
1011 * Convert a List of property identifiers into an array of property
1012 * identifiers. Returns a pointer to the allocated array. Returns NULL on
1013 * error. 'num_ids' is set to the number of returned property identifiers.
1014 */
1015 static int *
1016get_prop_ids_from_list(list_T *l, int *num_ids)
1017{
1018 int *prop_ids;
1019 listitem_T *li;
1020 int i;
1021 int id;
1022 int error;
1023
1024 *num_ids = 0;
1025
1026 prop_ids = ALLOC_MULT(int, list_len(l));
1027 if (prop_ids == NULL)
1028 return NULL;
1029
1030 i = 0;
1031 FOR_ALL_LIST_ITEMS(l, li)
1032 {
1033 error = FALSE;
1034 id = tv_get_number_chk(&li->li_tv, &error);
1035 if (error)
1036 goto errret;
1037
1038 prop_ids[i++] = id;
1039 }
1040
1041 *num_ids = i;
1042 return prop_ids;
1043
1044errret:
1045 VIM_CLEAR(prop_ids);
1046 return NULL;
1047}
1048
1049/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001050 * prop_list({lnum} [, {bufnr}])
1051 */
1052 void
1053f_prop_list(typval_T *argvars, typval_T *rettv)
1054{
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001055 linenr_T lnum;
1056 linenr_T start_lnum;
1057 linenr_T end_lnum;
1058 buf_T *buf = curbuf;
1059 int add_lnum = FALSE;
1060 int *prop_types = NULL;
1061 int prop_types_len = 0;
1062 int *prop_ids = NULL;
1063 int prop_ids_len = 0;
1064 list_T *l;
1065 dictitem_T *di;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001066
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001067 if (in_vim9script()
1068 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001069 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02001070 return;
1071
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001072 if (rettv_list_alloc(rettv) != OK)
1073 return;
1074
1075 // default: get text properties on current line
1076 start_lnum = tv_get_number(&argvars[0]);
1077 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001078 if (argvars[1].v_type != VAR_UNKNOWN)
1079 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001080 dict_T *d;
1081
1082 if (argvars[1].v_type != VAR_DICT)
1083 {
1084 emsg(_(e_dictreq));
1085 return;
1086 }
1087 d = argvars[1].vval.v_dict;
1088
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001089 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1090 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001091
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001092 if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001093 {
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001094 if (di->di_tv.v_type != VAR_NUMBER)
1095 {
1096 emsg(_(e_numberreq));
1097 return;
1098 }
1099 end_lnum = tv_get_number(&di->di_tv);
1100 if (end_lnum < 0)
1101 // negative end_lnum is used as an offset from the last buffer
1102 // line
1103 end_lnum = buf->b_ml.ml_line_count + end_lnum + 1;
1104 else if (end_lnum > buf->b_ml.ml_line_count)
1105 end_lnum = buf->b_ml.ml_line_count;
1106 add_lnum = TRUE;
1107 }
1108 if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL)
1109 {
1110 if (di->di_tv.v_type != VAR_LIST)
1111 {
1112 emsg(_(e_listreq));
1113 return;
1114 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001115
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001116 l = di->di_tv.vval.v_list;
1117 if (l != NULL && list_len(l) > 0)
1118 {
1119 prop_types = get_prop_types_from_names(l, buf, &prop_types_len);
1120 if (prop_types == NULL)
1121 return;
1122 }
1123 }
1124 if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL)
1125 {
1126 if (di->di_tv.v_type != VAR_LIST)
1127 {
1128 emsg(_(e_listreq));
1129 goto errret;
1130 }
1131
1132 l = di->di_tv.vval.v_list;
1133 if (l != NULL && list_len(l) > 0)
1134 {
1135 prop_ids = get_prop_ids_from_list(l, &prop_ids_len);
1136 if (prop_ids == NULL)
1137 goto errret;
1138 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001139 }
1140 }
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001141 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count
1142 || end_lnum < 1 || end_lnum < start_lnum)
1143 emsg(_(e_invalid_range));
1144 else
1145 for (lnum = start_lnum; lnum <= end_lnum; lnum++)
1146 get_props_in_line(buf, lnum, prop_types, prop_types_len,
1147 prop_ids, prop_ids_len,
1148 rettv->vval.v_list, add_lnum);
1149
1150errret:
1151 VIM_CLEAR(prop_types);
1152 VIM_CLEAR(prop_ids);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001153}
1154
1155/*
1156 * prop_remove({props} [, {lnum} [, {lnum_end}]])
1157 */
1158 void
1159f_prop_remove(typval_T *argvars, typval_T *rettv)
1160{
1161 linenr_T start = 1;
1162 linenr_T end = 0;
1163 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +02001164 linenr_T first_changed = 0;
1165 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001166 dict_T *dict;
1167 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001168 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001169 int id = -1;
1170 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001171 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001172
1173 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001174
1175 if (in_vim9script()
1176 && (check_for_dict_arg(argvars, 0) == FAIL
1177 || check_for_opt_number_arg(argvars, 1) == FAIL
1178 || (argvars[1].v_type != VAR_UNKNOWN
1179 && check_for_opt_number_arg(argvars, 2) == FAIL)))
1180 return;
1181
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001182 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
1183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001184 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001185 return;
1186 }
1187
1188 if (argvars[1].v_type != VAR_UNKNOWN)
1189 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001190 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001191 end = start;
1192 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001193 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001194 if (start < 1 || end < 1)
1195 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001196 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001197 return;
1198 }
1199 }
1200
1201 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001202 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1203 return;
1204 if (buf->b_ml.ml_mfp == NULL)
1205 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001206
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001207 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001208
1209 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01001210 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001211 if (dict_find(dict, (char_u *)"type", -1))
1212 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01001213 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001214 proptype_T *type = lookup_prop_type(name, buf);
1215
1216 if (type == NULL)
1217 return;
1218 type_id = type->pt_id;
1219 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001220 both = dict_get_bool(dict, (char_u *)"both", FALSE);
1221
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001222 if (id == -1 && type_id == -1)
1223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001224 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001225 return;
1226 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001227 if (both && (id == -1 || type_id == -1))
1228 {
1229 emsg(_("E860: Need 'id' and 'type' with 'both'"));
1230 return;
1231 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001232
1233 if (end == 0)
1234 end = buf->b_ml.ml_line_count;
1235 for (lnum = start; lnum <= end; ++lnum)
1236 {
1237 char_u *text;
1238 size_t len;
1239
1240 if (lnum > buf->b_ml.ml_line_count)
1241 break;
1242 text = ml_get_buf(buf, lnum, FALSE);
1243 len = STRLEN(text) + 1;
1244 if ((size_t)buf->b_ml.ml_line_len > len)
1245 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001246 static textprop_T textprop; // static because of alignment
1247 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001248
1249 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1250 / sizeof(textprop_T); ++idx)
1251 {
1252 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1253 + idx * sizeof(textprop_T);
1254 size_t taillen;
1255
1256 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001257 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1258 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001259 {
1260 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1261 {
1262 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1263
1264 // need to allocate the line to be able to change it
1265 if (newptr == NULL)
1266 return;
1267 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1268 buf->b_ml.ml_line_len);
1269 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001270 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1271
1272 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001273 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001274 }
1275
1276 taillen = buf->b_ml.ml_line_len - len
1277 - (idx + 1) * sizeof(textprop_T);
1278 if (taillen > 0)
1279 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1280 taillen);
1281 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1282 --idx;
1283
Bram Moolenaar965c0442021-05-17 00:22:06 +02001284 if (first_changed == 0)
1285 first_changed = lnum;
1286 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001287 ++rettv->vval.v_number;
1288 if (!do_all)
1289 break;
1290 }
1291 }
1292 }
1293 }
Bram Moolenaar965c0442021-05-17 00:22:06 +02001294 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001295 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001296 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1297 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001298 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001299}
1300
1301/*
1302 * Common for f_prop_type_add() and f_prop_type_change().
1303 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001304 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001305prop_type_set(typval_T *argvars, int add)
1306{
1307 char_u *name;
1308 buf_T *buf = NULL;
1309 dict_T *dict;
1310 dictitem_T *di;
1311 proptype_T *prop;
1312
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001313 if (in_vim9script()
1314 && (check_for_string_arg(argvars, 0) == FAIL
1315 || check_for_dict_arg(argvars, 1) == FAIL))
1316 return;
1317
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001318 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001319 if (*name == NUL)
1320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001321 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001322 return;
1323 }
1324
1325 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1326 return;
1327 dict = argvars[1].vval.v_dict;
1328
1329 prop = find_prop(name, buf);
1330 if (add)
1331 {
1332 hashtab_T **htp;
1333
1334 if (prop != NULL)
1335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001336 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001337 return;
1338 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001339 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001340 if (prop == NULL)
1341 return;
1342 STRCPY(prop->pt_name, name);
1343 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001344 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001345 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1346 if (*htp == NULL)
1347 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001348 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001349 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001350 {
1351 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001352 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001353 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001354 hash_init(*htp);
1355 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001356 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001357 }
1358 else
1359 {
1360 if (prop == NULL)
1361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001362 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001363 return;
1364 }
1365 }
1366
1367 if (dict != NULL)
1368 {
1369 di = dict_find(dict, (char_u *)"highlight", -1);
1370 if (di != NULL)
1371 {
1372 char_u *highlight;
1373 int hl_id = 0;
1374
Bram Moolenaar8f667172018-12-14 15:38:31 +01001375 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001376 if (highlight != NULL && *highlight != NUL)
1377 hl_id = syn_name2id(highlight);
1378 if (hl_id <= 0)
1379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001380 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001381 highlight == NULL ? (char_u *)"" : highlight);
1382 return;
1383 }
1384 prop->pt_hl_id = hl_id;
1385 }
1386
Bram Moolenaar58187f12019-05-05 16:33:47 +02001387 di = dict_find(dict, (char_u *)"combine", -1);
1388 if (di != NULL)
1389 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001390 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001391 prop->pt_flags |= PT_FLAG_COMBINE;
1392 else
1393 prop->pt_flags &= ~PT_FLAG_COMBINE;
1394 }
1395
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001396 di = dict_find(dict, (char_u *)"priority", -1);
1397 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001398 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001399
1400 di = dict_find(dict, (char_u *)"start_incl", -1);
1401 if (di != NULL)
1402 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001403 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001404 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1405 else
1406 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1407 }
1408
1409 di = dict_find(dict, (char_u *)"end_incl", -1);
1410 if (di != NULL)
1411 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001412 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001413 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1414 else
1415 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1416 }
1417 }
1418}
1419
1420/*
1421 * prop_type_add({name}, {props})
1422 */
1423 void
1424f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1425{
1426 prop_type_set(argvars, TRUE);
1427}
1428
1429/*
1430 * prop_type_change({name}, {props})
1431 */
1432 void
1433f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1434{
1435 prop_type_set(argvars, FALSE);
1436}
1437
1438/*
1439 * prop_type_delete({name} [, {bufnr}])
1440 */
1441 void
1442f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1443{
1444 char_u *name;
1445 buf_T *buf = NULL;
1446 hashitem_T *hi;
1447
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001448 if (in_vim9script()
1449 && (check_for_string_arg(argvars, 0) == FAIL
1450 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1451 return;
1452
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001453 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001454 if (*name == NUL)
1455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001456 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001457 return;
1458 }
1459
1460 if (argvars[1].v_type != VAR_UNKNOWN)
1461 {
1462 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1463 return;
1464 }
1465
1466 hi = find_prop_hi(name, buf);
1467 if (hi != NULL)
1468 {
1469 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001470 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001471
1472 if (buf == NULL)
1473 ht = global_proptypes;
1474 else
1475 ht = buf->b_proptypes;
1476 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001477 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001478 }
1479}
1480
1481/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001482 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001483 */
1484 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001485f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001486{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001487 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001488
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001489 if (in_vim9script()
1490 && (check_for_string_arg(argvars, 0) == FAIL
1491 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1492 return;
1493
1494 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001495 if (*name == NUL)
1496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001497 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001498 return;
1499 }
1500 if (rettv_dict_alloc(rettv) == OK)
1501 {
1502 proptype_T *prop = NULL;
1503 buf_T *buf = NULL;
1504
1505 if (argvars[1].v_type != VAR_UNKNOWN)
1506 {
1507 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1508 return;
1509 }
1510
1511 prop = find_prop(name, buf);
1512 if (prop != NULL)
1513 {
1514 dict_T *d = rettv->vval.v_dict;
1515
1516 if (prop->pt_hl_id > 0)
1517 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1518 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001519 dict_add_number(d, "combine",
1520 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001521 dict_add_number(d, "start_incl",
1522 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1523 dict_add_number(d, "end_incl",
1524 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1525 if (buf != NULL)
1526 dict_add_number(d, "bufnr", buf->b_fnum);
1527 }
1528 }
1529}
1530
1531 static void
1532list_types(hashtab_T *ht, list_T *l)
1533{
1534 long todo;
1535 hashitem_T *hi;
1536
1537 todo = (long)ht->ht_used;
1538 for (hi = ht->ht_array; todo > 0; ++hi)
1539 {
1540 if (!HASHITEM_EMPTY(hi))
1541 {
1542 proptype_T *prop = HI2PT(hi);
1543
1544 list_append_string(l, prop->pt_name, -1);
1545 --todo;
1546 }
1547 }
1548}
1549
1550/*
1551 * prop_type_list([{bufnr}])
1552 */
1553 void
1554f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1555{
1556 buf_T *buf = NULL;
1557
1558 if (rettv_list_alloc(rettv) == OK)
1559 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001560 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1561 return;
1562
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001563 if (argvars[0].v_type != VAR_UNKNOWN)
1564 {
1565 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1566 return;
1567 }
1568 if (buf == NULL)
1569 {
1570 if (global_proptypes != NULL)
1571 list_types(global_proptypes, rettv->vval.v_list);
1572 }
1573 else if (buf->b_proptypes != NULL)
1574 list_types(buf->b_proptypes, rettv->vval.v_list);
1575 }
1576}
1577
1578/*
1579 * Free all property types in "ht".
1580 */
1581 static void
1582clear_ht_prop_types(hashtab_T *ht)
1583{
1584 long todo;
1585 hashitem_T *hi;
1586
1587 if (ht == NULL)
1588 return;
1589
1590 todo = (long)ht->ht_used;
1591 for (hi = ht->ht_array; todo > 0; ++hi)
1592 {
1593 if (!HASHITEM_EMPTY(hi))
1594 {
1595 proptype_T *prop = HI2PT(hi);
1596
1597 vim_free(prop);
1598 --todo;
1599 }
1600 }
1601
1602 hash_clear(ht);
1603 vim_free(ht);
1604}
1605
1606#if defined(EXITFREE) || defined(PROTO)
1607/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001608 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001609 */
1610 void
1611clear_global_prop_types(void)
1612{
1613 clear_ht_prop_types(global_proptypes);
1614 global_proptypes = NULL;
1615}
1616#endif
1617
1618/*
1619 * Free all property types for "buf".
1620 */
1621 void
1622clear_buf_prop_types(buf_T *buf)
1623{
1624 clear_ht_prop_types(buf->b_proptypes);
1625 buf->b_proptypes = NULL;
1626}
1627
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001628// Struct used to return two values from adjust_prop().
1629typedef struct
1630{
1631 int dirty; // if the property was changed
1632 int can_drop; // whether after this change, the prop may be removed
1633} adjustres_T;
1634
1635/*
1636 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1637 *
1638 * Note that "col" is zero-based, while tp_col is one-based.
1639 * Only for the current buffer.
1640 * "flags" can have:
1641 * APC_SUBSTITUTE: Text is replaced, not inserted.
1642 */
1643 static adjustres_T
1644adjust_prop(
1645 textprop_T *prop,
1646 colnr_T col,
1647 int added,
1648 int flags)
1649{
1650 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1651 int start_incl = (pt != NULL
1652 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1653 || (flags & APC_SUBSTITUTE);
1654 int end_incl = (pt != NULL
1655 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1656 // Do not drop zero-width props if they later can increase in
1657 // size.
1658 int droppable = !(start_incl || end_incl);
1659 adjustres_T res = {TRUE, FALSE};
1660
1661 if (added > 0)
1662 {
1663 if (col + 1 <= prop->tp_col
1664 - (start_incl || (prop->tp_len == 0 && end_incl)))
1665 // Change is entirely before the text property: Only shift
1666 prop->tp_col += added;
1667 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1668 // Insertion was inside text property
1669 prop->tp_len += added;
1670 }
1671 else if (prop->tp_col > col + 1)
1672 {
1673 if (prop->tp_col + added < col + 1)
1674 {
1675 prop->tp_len += (prop->tp_col - 1 - col) + added;
1676 prop->tp_col = col + 1;
1677 if (prop->tp_len <= 0)
1678 {
1679 prop->tp_len = 0;
1680 res.can_drop = droppable;
1681 }
1682 }
1683 else
1684 prop->tp_col += added;
1685 }
1686 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1687 {
1688 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1689
1690 prop->tp_len += after > 0 ? added + after : added;
1691 res.can_drop = prop->tp_len <= 0 && droppable;
1692 }
1693 else
1694 res.dirty = FALSE;
1695
1696 return res;
1697}
1698
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001699/*
1700 * Adjust the columns of text properties in line "lnum" after position "col" to
1701 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001702 * Note that "col" is zero-based, while tp_col is one-based.
1703 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001704 * "flags" can have:
1705 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1706 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001707 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001708 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001709 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001710 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001711adjust_prop_columns(
1712 linenr_T lnum,
1713 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001714 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001715 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001716{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001717 int proplen;
1718 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001719 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001720 int ri, wi;
1721 size_t textlen;
1722
1723 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001724 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001725
1726 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1727 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001728 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001729 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001730
Bram Moolenaar196d1572019-01-02 23:47:18 +01001731 wi = 0; // write index
1732 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001733 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001734 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001735 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001736
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001737 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1738 res = adjust_prop(&prop, col, bytes_added, flags);
1739 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001740 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001741 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001742 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1743 && u_savesub(lnum) == FAIL)
1744 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001745 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001746
1747 // u_savesub() may have updated curbuf->b_ml, fetch it again
1748 if (curbuf->b_ml.ml_line_lnum != lnum)
1749 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001750 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001751 if (res.can_drop)
1752 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001753 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001754 ++wi;
1755 }
1756 if (dirty)
1757 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001758 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1759
1760 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1761 curbuf->b_ml.ml_line_ptr =
1762 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001763 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001764 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001765 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001766 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001767}
1768
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001769/*
1770 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001771 * "lnum_props" is the line that has the properties from before the split.
1772 * "lnum_top" is the top line.
1773 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001774 * "deleted" is the number of bytes deleted.
1775 */
1776 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001777adjust_props_for_split(
1778 linenr_T lnum_props,
1779 linenr_T lnum_top,
1780 int kept,
1781 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001782{
1783 char_u *props;
1784 int count;
1785 garray_T prevprop;
1786 garray_T nextprop;
1787 int i;
1788 int skipped = kept + deleted;
1789
1790 if (!curbuf->b_has_textprop)
1791 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001792
1793 // Get the text properties from "lnum_props".
1794 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001795 ga_init2(&prevprop, sizeof(textprop_T), 10);
1796 ga_init2(&nextprop, sizeof(textprop_T), 10);
1797
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001798 // Keep the relevant ones in the first line, reducing the length if needed.
1799 // Copy the ones that include the split to the second line.
1800 // Move the ones after the split to the second line.
1801 for (i = 0; i < count; ++i)
1802 {
1803 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001804 proptype_T *pt;
1805 int start_incl, end_incl;
1806 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001807
1808 // copy the prop to an aligned structure
1809 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1810
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001811 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1812 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1813 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1814 cont_prev = prop.tp_col + !start_incl <= kept;
1815 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1816
1817 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001818 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001819 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1820
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001821 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001822 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001823 if (p->tp_col + p->tp_len >= kept)
1824 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001825 if (cont_next)
1826 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001827 }
1828
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001829 // Only add the property to the next line if the length is bigger than
1830 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001831 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001832 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001833 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001834 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001835 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001836 if (p->tp_col > skipped)
1837 p->tp_col -= skipped - 1;
1838 else
1839 {
1840 p->tp_len -= skipped - p->tp_col;
1841 p->tp_col = 1;
1842 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001843 if (cont_prev)
1844 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001845 }
1846 }
1847
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001848 set_text_props(lnum_top, prevprop.ga_data,
1849 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001850 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001851 set_text_props(lnum_top + 1, nextprop.ga_data,
1852 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001853 ga_clear(&nextprop);
1854}
1855
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001856/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001857 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001858 */
1859 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001860prepend_joined_props(
1861 char_u *new_props,
1862 int propcount,
1863 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001864 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001865 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001866 long col,
1867 int removed)
1868{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001869 char_u *props;
1870 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1871 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001872
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001873 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001874 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001875 textprop_T prop;
1876 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001877
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001878 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1879 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1880
1881 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001882 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001883
1884 if (add_all || end)
1885 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1886 &prop, sizeof(prop));
1887 else
1888 {
1889 int j;
1890 int found = FALSE;
1891
1892 // Search for continuing prop.
1893 for (j = *props_remaining; j < propcount; ++j)
1894 {
1895 textprop_T op;
1896
1897 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1898 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1899 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001900 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001901 found = TRUE;
1902 op.tp_len += op.tp_col - prop.tp_col;
1903 op.tp_col = prop.tp_col;
1904 // Start/end is taken care of when deleting joined lines
1905 op.tp_flags = prop.tp_flags;
1906 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1907 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001908 }
1909 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001910 if (!found)
1911 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001912 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001913 }
1914}
1915
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001916#endif // FEAT_PROP_POPUP