blob: bc27aef6e7b036802c901b1381fcb111c04cf335 [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/*
186 * Shared between prop_add() and popup_create().
187 * "dict_arg" is the function argument of a dict containing "bufnr".
188 * it is NULL for popup_create().
189 */
190 void
191prop_add_common(
192 linenr_T start_lnum,
193 colnr_T start_col,
194 dict_T *dict,
195 buf_T *default_buf,
196 typval_T *dict_arg)
197{
198 linenr_T lnum;
199 linenr_T end_lnum;
200 colnr_T end_col;
201 char_u *type_name;
202 proptype_T *type;
203 buf_T *buf = default_buf;
204 int id = 0;
205 char_u *newtext;
206 int proplen;
207 size_t textlen;
208 char_u *props = NULL;
209 char_u *newprops;
210 textprop_T tmp_prop;
211 int i;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100212
213 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100215 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100216 return;
217 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100218 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100219
220 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
221 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100222 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
223 if (end_lnum < start_lnum)
224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100225 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100226 return;
227 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100228 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100229 else
230 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100231
232 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100233 {
234 long length = dict_get_number(dict, (char_u *)"length");
235
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100236 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100238 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100239 return;
240 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100241 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100242 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100243 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
244 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100245 end_col = dict_get_number(dict, (char_u *)"end_col");
246 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100248 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100249 return;
250 }
251 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100252 else if (start_lnum == end_lnum)
253 end_col = start_col;
254 else
255 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100256
257 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100258 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100259
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200260 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100261 return;
262
263 type = lookup_prop_type(type_name, buf);
264 if (type == NULL)
265 return;
266
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100267 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100269 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100270 return;
271 }
272 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100274 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100275 return;
276 }
277
Bram Moolenaard79eef22019-05-24 20:41:55 +0200278 if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaar45311b52019-08-13 22:27:32 +0200279 {
280 emsg(_("E275: Cannot add text property to unloaded buffer"));
281 return;
282 }
Bram Moolenaard79eef22019-05-24 20:41:55 +0200283
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100284 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100285 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100286 colnr_T col; // start column
287 long length; // in bytes
288
289 // Fetch the line to get the ml_line_len field updated.
290 proplen = get_text_props(buf, lnum, &props, TRUE);
291 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
292
293 if (lnum == start_lnum)
294 col = start_col;
295 else
296 col = 1;
297 if (col - 1 > (colnr_T)textlen)
298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100299 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100300 return;
301 }
302
303 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100304 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100305 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100306 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100307 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100308 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100309 if (length < 0)
310 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100311
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100312 // Allocate the new line with space for the new property.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100313 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
314 if (newtext == NULL)
315 return;
316 // Copy the text, including terminating NUL.
317 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
318
319 // Find the index where to insert the new property.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200320 // Since the text properties are not aligned properly when stored with
321 // the text, we need to copy them as bytes before using it as a struct.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100322 for (i = 0; i < proplen; ++i)
323 {
324 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200325 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100326 if (tmp_prop.tp_col >= col)
327 break;
328 }
329 newprops = newtext + textlen;
330 if (i > 0)
331 mch_memmove(newprops, props, sizeof(textprop_T) * i);
332
333 tmp_prop.tp_col = col;
334 tmp_prop.tp_len = length;
335 tmp_prop.tp_id = id;
336 tmp_prop.tp_type = type->pt_id;
337 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
338 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
339 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200340 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100341
342 if (i < proplen)
343 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
344 props + i * sizeof(textprop_T),
345 sizeof(textprop_T) * (proplen - i));
346
347 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
348 vim_free(buf->b_ml.ml_line_ptr);
349 buf->b_ml.ml_line_ptr = newtext;
350 buf->b_ml.ml_line_len += sizeof(textprop_T);
351 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100352 }
353
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100354 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200355 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
356 redraw_buf_later(buf, VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100357}
358
359/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100360 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100361 * Returns the number of text properties and, when non-zero, a pointer to the
362 * first one in "props" (note that it is not aligned, therefore the char_u
363 * pointer).
364 */
365 int
366get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
367{
368 char_u *text;
369 size_t textlen;
370 size_t proplen;
371
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100372 // Be quick when no text property types have been defined or the buffer,
373 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200374 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100375 return 0;
376
377 // Fetch the line to get the ml_line_len field updated.
378 text = ml_get_buf(buf, lnum, will_change);
379 textlen = STRLEN(text) + 1;
380 proplen = buf->b_ml.ml_line_len - textlen;
381 if (proplen % sizeof(textprop_T) != 0)
382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100383 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100384 return 0;
385 }
386 if (proplen > 0)
387 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100388 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100389}
390
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200391/**
392 * Return the number of text properties on line "lnum" in the current buffer.
393 * When "only_starting" is true only text properties starting in this line will
394 * be considered.
395 */
396 int
397count_props(linenr_T lnum, int only_starting)
398{
399 char_u *props;
400 int proplen = get_text_props(curbuf, lnum, &props, 0);
401 int result = proplen;
402 int i;
403 textprop_T prop;
404
405 if (only_starting)
406 for (i = 0; i < proplen; ++i)
407 {
408 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
409 if (prop.tp_flags & TP_FLAG_CONT_PREV)
410 --result;
411 }
412 return result;
413}
414
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100415/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200416 * Find text property "type_id" in the visible lines of window "wp".
417 * Match "id" when it is > 0.
418 * Returns FAIL when not found.
419 */
420 int
421find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
422 linenr_T *found_lnum)
423{
424 linenr_T lnum;
425 char_u *props;
426 int count;
427 int i;
428
429 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100430 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200431 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
432 {
433 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
434 for (i = 0; i < count; ++i)
435 {
436 mch_memmove(prop, props + i * sizeof(textprop_T),
437 sizeof(textprop_T));
438 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
439 {
440 *found_lnum = lnum;
441 return OK;
442 }
443 }
444 }
445 return FAIL;
446}
447
448/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100449 * Set the text properties for line "lnum" to "props" with length "len".
450 * If "len" is zero text properties are removed, "props" is not used.
451 * Any existing text properties are dropped.
452 * Only works for the current buffer.
453 */
454 static void
455set_text_props(linenr_T lnum, char_u *props, int len)
456{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100457 char_u *text;
458 char_u *newtext;
459 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100460
461 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100462 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100463 newtext = alloc(textlen + len);
464 if (newtext == NULL)
465 return;
466 mch_memmove(newtext, text, textlen);
467 if (len > 0)
468 mch_memmove(newtext + textlen, props, len);
469 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
470 vim_free(curbuf->b_ml.ml_line_ptr);
471 curbuf->b_ml.ml_line_ptr = newtext;
472 curbuf->b_ml.ml_line_len = textlen + len;
473 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
474}
475
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100476 static proptype_T *
477find_type_by_id(hashtab_T *ht, int id)
478{
479 long todo;
480 hashitem_T *hi;
481
482 if (ht == NULL)
483 return NULL;
484
485 // TODO: Make this faster by keeping a list of types sorted on ID and use
486 // a binary search.
487
488 todo = (long)ht->ht_used;
489 for (hi = ht->ht_array; todo > 0; ++hi)
490 {
491 if (!HASHITEM_EMPTY(hi))
492 {
493 proptype_T *prop = HI2PT(hi);
494
495 if (prop->pt_id == id)
496 return prop;
497 --todo;
498 }
499 }
500 return NULL;
501}
502
503/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100504 * Fill 'dict' with text properties in 'prop'.
505 */
506 static void
507prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
508{
509 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200510 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100511
512 dict_add_number(dict, "col", prop->tp_col);
513 dict_add_number(dict, "length", prop->tp_len);
514 dict_add_number(dict, "id", prop->tp_id);
515 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
516 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200517
518 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
519 if (pt == NULL)
520 {
521 pt = find_type_by_id(global_proptypes, prop->tp_type);
522 buflocal = FALSE;
523 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100524 if (pt != NULL)
525 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200526
527 if (buflocal)
528 dict_add_number(dict, "type_bufnr", buf->b_fnum);
529 else
530 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100531}
532
533/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100534 * Find a property type by ID in "buf" or globally.
535 * Returns NULL if not found.
536 */
537 proptype_T *
538text_prop_type_by_id(buf_T *buf, int id)
539{
540 proptype_T *type;
541
542 type = find_type_by_id(buf->b_proptypes, id);
543 if (type == NULL)
544 type = find_type_by_id(global_proptypes, id);
545 return type;
546}
547
548/*
549 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
550 */
551 void
552f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
553{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200554 linenr_T start;
555 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100556 linenr_T lnum;
557 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100558 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100559
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200560 if (in_vim9script()
561 && (check_for_number_arg(argvars, 0) == FAIL
562 || check_for_opt_number_arg(argvars, 1) == FAIL
563 || (argvars[1].v_type != VAR_UNKNOWN
564 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
565 return;
566
567 start = tv_get_number(&argvars[0]);
568 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100569 if (argvars[1].v_type != VAR_UNKNOWN)
570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100571 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100572 if (argvars[2].v_type != VAR_UNKNOWN)
573 {
574 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
575 return;
576 }
577 }
578 if (start < 1 || end < 1)
579 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200580 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100581 return;
582 }
583
584 for (lnum = start; lnum <= end; ++lnum)
585 {
586 char_u *text;
587 size_t len;
588
589 if (lnum > buf->b_ml.ml_line_count)
590 break;
591 text = ml_get_buf(buf, lnum, FALSE);
592 len = STRLEN(text) + 1;
593 if ((size_t)buf->b_ml.ml_line_len > len)
594 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100595 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100596 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
597 {
598 char_u *newtext = vim_strsave(text);
599
600 // need to allocate the line now
601 if (newtext == NULL)
602 return;
603 buf->b_ml.ml_line_ptr = newtext;
604 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
605 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100606 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100607 }
608 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100609 if (did_clear)
610 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100611}
612
613/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100614 * prop_find({props} [, {direction}])
615 */
616 void
617f_prop_find(typval_T *argvars, typval_T *rettv)
618{
619 pos_T *cursor = &curwin->w_cursor;
620 dict_T *dict;
621 buf_T *buf = curbuf;
622 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200623 int lnum_start;
624 int start_pos_has_prop = 0;
625 int seen_end = 0;
626 int id = -1;
627 int type_id = -1;
628 int skipstart = 0;
629 int lnum = -1;
630 int col = -1;
631 int dir = 1; // 1 = forward, -1 = backward
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100632 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100633
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200634 if (in_vim9script()
635 && (check_for_dict_arg(argvars, 0) == FAIL
636 || check_for_opt_string_arg(argvars, 1) == FAIL))
637 return;
638
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100639 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
640 {
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200641 emsg(_(e_dictreq));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100642 return;
643 }
644 dict = argvars[0].vval.v_dict;
645
646 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
647 return;
648 if (buf->b_ml.ml_mfp == NULL)
649 return;
650
651 if (argvars[1].v_type != VAR_UNKNOWN)
652 {
653 char_u *dir_s = tv_get_string(&argvars[1]);
654
655 if (*dir_s == 'b')
656 dir = -1;
657 else if (*dir_s != 'f')
658 {
659 emsg(_(e_invarg));
660 return;
661 }
662 }
663
664 di = dict_find(dict, (char_u *)"lnum", -1);
665 if (di != NULL)
666 lnum = tv_get_number(&di->di_tv);
667
668 di = dict_find(dict, (char_u *)"col", -1);
669 if (di != NULL)
670 col = tv_get_number(&di->di_tv);
671
672 if (lnum == -1)
673 {
674 lnum = cursor->lnum;
675 col = cursor->col + 1;
676 }
677 else if (col == -1)
678 col = 1;
679
680 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
681 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200682 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100683 return;
684 }
685
Bram Moolenaareb245562020-09-03 22:33:44 +0200686 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100687
688 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200689 {
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100690 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200691 if (id == -1)
692 id = -2;
693 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100694 if (dict_find(dict, (char_u *)"type", -1))
695 {
696 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
697 proptype_T *type = lookup_prop_type(name, buf);
698
699 if (type == NULL)
700 return;
701 type_id = type->pt_id;
702 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100703 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100704 if (id == -1 && type_id == -1)
705 {
706 emsg(_("E968: Need at least one of 'id' or 'type'"));
707 return;
708 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100709 if (both && (id == -1 || type_id == -1))
710 {
711 emsg(_("E860: Need 'id' and 'type' with 'both'"));
712 return;
713 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100714
715 lnum_start = lnum;
716
717 if (rettv_dict_alloc(rettv) == FAIL)
718 return;
719
720 while (1)
721 {
722 char_u *text = ml_get_buf(buf, lnum, FALSE);
723 size_t textlen = STRLEN(text) + 1;
724 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200725 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100726 int i;
727 textprop_T prop;
728 int prop_start;
729 int prop_end;
730
731 for (i = 0; i < count; ++i)
732 {
733 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
734 sizeof(textprop_T));
735
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100736 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100737 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100738 if (dir < 0)
739 {
740 if (col < prop.tp_col)
741 break;
742 }
743 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
744 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100745 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100746 if (both ? prop.tp_id == id && prop.tp_type == type_id
747 : prop.tp_id == id || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100748 {
749 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100750 if (lnum_start == lnum
751 && col >= prop.tp_col
752 && (col <= prop.tp_col + prop.tp_len
753 - (prop.tp_len != 0)))
754 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100755
756 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
757 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
758 if (!prop_start && prop_end && dir > 0)
759 seen_end = 1;
760
761 // Skip lines without the start flag.
762 if (!prop_start)
763 {
764 // Always search backwards for start when search started
765 // on a prop and we're not skipping.
766 if (start_pos_has_prop && !skipstart)
767 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200768 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100769 }
770
771 // If skipstart is true, skip the prop at start pos (even if
772 // continued from another line).
773 if (start_pos_has_prop && skipstart && !seen_end)
774 {
775 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200776 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100777 }
778
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100779 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
780 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
781
782 return;
783 }
784 }
785
786 if (dir > 0)
787 {
788 if (lnum >= buf->b_ml.ml_line_count)
789 break;
790 lnum++;
791 }
792 else
793 {
794 if (lnum <= 1)
795 break;
796 lnum--;
797 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100798 // Adjust col to indicate that we're continuing from prev/next line.
799 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100800 }
801}
802
803/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100804 * prop_list({lnum} [, {bufnr}])
805 */
806 void
807f_prop_list(typval_T *argvars, typval_T *rettv)
808{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200809 linenr_T lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100810 buf_T *buf = curbuf;
811
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200812 if (in_vim9script()
813 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200814 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200815 return;
816
817 lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100818 if (argvars[1].v_type != VAR_UNKNOWN)
819 {
820 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
821 return;
822 }
823 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
824 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200825 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100826 return;
827 }
828
829 if (rettv_list_alloc(rettv) == OK)
830 {
831 char_u *text = ml_get_buf(buf, lnum, FALSE);
832 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100833 int count = (int)((buf->b_ml.ml_line_len - textlen)
834 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100835 int i;
836 textprop_T prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100837
838 for (i = 0; i < count; ++i)
839 {
840 dict_T *d = dict_alloc();
841
842 if (d == NULL)
843 break;
844 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
845 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100846 prop_fill_dict(d, &prop, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100847 list_append_dict(rettv->vval.v_list, d);
848 }
849 }
850}
851
852/*
853 * prop_remove({props} [, {lnum} [, {lnum_end}]])
854 */
855 void
856f_prop_remove(typval_T *argvars, typval_T *rettv)
857{
858 linenr_T start = 1;
859 linenr_T end = 0;
860 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +0200861 linenr_T first_changed = 0;
862 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100863 dict_T *dict;
864 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200865 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100866 int id = -1;
867 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200868 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100869
870 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200871
872 if (in_vim9script()
873 && (check_for_dict_arg(argvars, 0) == FAIL
874 || check_for_opt_number_arg(argvars, 1) == FAIL
875 || (argvars[1].v_type != VAR_UNKNOWN
876 && check_for_opt_number_arg(argvars, 2) == FAIL)))
877 return;
878
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100879 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100881 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100882 return;
883 }
884
885 if (argvars[1].v_type != VAR_UNKNOWN)
886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100887 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100888 end = start;
889 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100890 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100891 if (start < 1 || end < 1)
892 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200893 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100894 return;
895 }
896 }
897
898 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200899 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
900 return;
901 if (buf->b_ml.ml_mfp == NULL)
902 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100903
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200904 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100905
906 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100907 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100908 if (dict_find(dict, (char_u *)"type", -1))
909 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100910 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100911 proptype_T *type = lookup_prop_type(name, buf);
912
913 if (type == NULL)
914 return;
915 type_id = type->pt_id;
916 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200917 both = dict_get_bool(dict, (char_u *)"both", FALSE);
918
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100919 if (id == -1 && type_id == -1)
920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100921 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100922 return;
923 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100924 if (both && (id == -1 || type_id == -1))
925 {
926 emsg(_("E860: Need 'id' and 'type' with 'both'"));
927 return;
928 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100929
930 if (end == 0)
931 end = buf->b_ml.ml_line_count;
932 for (lnum = start; lnum <= end; ++lnum)
933 {
934 char_u *text;
935 size_t len;
936
937 if (lnum > buf->b_ml.ml_line_count)
938 break;
939 text = ml_get_buf(buf, lnum, FALSE);
940 len = STRLEN(text) + 1;
941 if ((size_t)buf->b_ml.ml_line_len > len)
942 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200943 static textprop_T textprop; // static because of alignment
944 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100945
946 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
947 / sizeof(textprop_T); ++idx)
948 {
949 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
950 + idx * sizeof(textprop_T);
951 size_t taillen;
952
953 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100954 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
955 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100956 {
957 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
958 {
959 char_u *newptr = alloc(buf->b_ml.ml_line_len);
960
961 // need to allocate the line to be able to change it
962 if (newptr == NULL)
963 return;
964 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
965 buf->b_ml.ml_line_len);
966 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100967 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
968
969 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200970 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100971 }
972
973 taillen = buf->b_ml.ml_line_len - len
974 - (idx + 1) * sizeof(textprop_T);
975 if (taillen > 0)
976 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
977 taillen);
978 buf->b_ml.ml_line_len -= sizeof(textprop_T);
979 --idx;
980
Bram Moolenaar965c0442021-05-17 00:22:06 +0200981 if (first_changed == 0)
982 first_changed = lnum;
983 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100984 ++rettv->vval.v_number;
985 if (!do_all)
986 break;
987 }
988 }
989 }
990 }
Bram Moolenaar965c0442021-05-17 00:22:06 +0200991 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200992 {
Bram Moolenaar965c0442021-05-17 00:22:06 +0200993 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
994 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200995 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100996}
997
998/*
999 * Common for f_prop_type_add() and f_prop_type_change().
1000 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001001 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001002prop_type_set(typval_T *argvars, int add)
1003{
1004 char_u *name;
1005 buf_T *buf = NULL;
1006 dict_T *dict;
1007 dictitem_T *di;
1008 proptype_T *prop;
1009
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001010 if (in_vim9script()
1011 && (check_for_string_arg(argvars, 0) == FAIL
1012 || check_for_dict_arg(argvars, 1) == FAIL))
1013 return;
1014
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001015 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001016 if (*name == NUL)
1017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001019 return;
1020 }
1021
1022 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1023 return;
1024 dict = argvars[1].vval.v_dict;
1025
1026 prop = find_prop(name, buf);
1027 if (add)
1028 {
1029 hashtab_T **htp;
1030
1031 if (prop != NULL)
1032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001033 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001034 return;
1035 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001036 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001037 if (prop == NULL)
1038 return;
1039 STRCPY(prop->pt_name, name);
1040 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001041 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001042 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1043 if (*htp == NULL)
1044 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001045 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001046 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001047 {
1048 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001049 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001050 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001051 hash_init(*htp);
1052 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001053 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001054 }
1055 else
1056 {
1057 if (prop == NULL)
1058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001059 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001060 return;
1061 }
1062 }
1063
1064 if (dict != NULL)
1065 {
1066 di = dict_find(dict, (char_u *)"highlight", -1);
1067 if (di != NULL)
1068 {
1069 char_u *highlight;
1070 int hl_id = 0;
1071
Bram Moolenaar8f667172018-12-14 15:38:31 +01001072 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001073 if (highlight != NULL && *highlight != NUL)
1074 hl_id = syn_name2id(highlight);
1075 if (hl_id <= 0)
1076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001077 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001078 highlight == NULL ? (char_u *)"" : highlight);
1079 return;
1080 }
1081 prop->pt_hl_id = hl_id;
1082 }
1083
Bram Moolenaar58187f12019-05-05 16:33:47 +02001084 di = dict_find(dict, (char_u *)"combine", -1);
1085 if (di != NULL)
1086 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001087 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001088 prop->pt_flags |= PT_FLAG_COMBINE;
1089 else
1090 prop->pt_flags &= ~PT_FLAG_COMBINE;
1091 }
1092
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001093 di = dict_find(dict, (char_u *)"priority", -1);
1094 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001095 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001096
1097 di = dict_find(dict, (char_u *)"start_incl", -1);
1098 if (di != NULL)
1099 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001100 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001101 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1102 else
1103 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1104 }
1105
1106 di = dict_find(dict, (char_u *)"end_incl", -1);
1107 if (di != NULL)
1108 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001109 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001110 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1111 else
1112 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1113 }
1114 }
1115}
1116
1117/*
1118 * prop_type_add({name}, {props})
1119 */
1120 void
1121f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1122{
1123 prop_type_set(argvars, TRUE);
1124}
1125
1126/*
1127 * prop_type_change({name}, {props})
1128 */
1129 void
1130f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1131{
1132 prop_type_set(argvars, FALSE);
1133}
1134
1135/*
1136 * prop_type_delete({name} [, {bufnr}])
1137 */
1138 void
1139f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1140{
1141 char_u *name;
1142 buf_T *buf = NULL;
1143 hashitem_T *hi;
1144
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001145 if (in_vim9script()
1146 && (check_for_string_arg(argvars, 0) == FAIL
1147 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1148 return;
1149
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001150 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001151 if (*name == NUL)
1152 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001153 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001154 return;
1155 }
1156
1157 if (argvars[1].v_type != VAR_UNKNOWN)
1158 {
1159 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1160 return;
1161 }
1162
1163 hi = find_prop_hi(name, buf);
1164 if (hi != NULL)
1165 {
1166 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001167 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001168
1169 if (buf == NULL)
1170 ht = global_proptypes;
1171 else
1172 ht = buf->b_proptypes;
1173 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001174 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001175 }
1176}
1177
1178/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001179 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001180 */
1181 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001182f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001183{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001184 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001185
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001186 if (in_vim9script()
1187 && (check_for_string_arg(argvars, 0) == FAIL
1188 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1189 return;
1190
1191 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001192 if (*name == NUL)
1193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001195 return;
1196 }
1197 if (rettv_dict_alloc(rettv) == OK)
1198 {
1199 proptype_T *prop = NULL;
1200 buf_T *buf = NULL;
1201
1202 if (argvars[1].v_type != VAR_UNKNOWN)
1203 {
1204 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1205 return;
1206 }
1207
1208 prop = find_prop(name, buf);
1209 if (prop != NULL)
1210 {
1211 dict_T *d = rettv->vval.v_dict;
1212
1213 if (prop->pt_hl_id > 0)
1214 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1215 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001216 dict_add_number(d, "combine",
1217 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001218 dict_add_number(d, "start_incl",
1219 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1220 dict_add_number(d, "end_incl",
1221 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1222 if (buf != NULL)
1223 dict_add_number(d, "bufnr", buf->b_fnum);
1224 }
1225 }
1226}
1227
1228 static void
1229list_types(hashtab_T *ht, list_T *l)
1230{
1231 long todo;
1232 hashitem_T *hi;
1233
1234 todo = (long)ht->ht_used;
1235 for (hi = ht->ht_array; todo > 0; ++hi)
1236 {
1237 if (!HASHITEM_EMPTY(hi))
1238 {
1239 proptype_T *prop = HI2PT(hi);
1240
1241 list_append_string(l, prop->pt_name, -1);
1242 --todo;
1243 }
1244 }
1245}
1246
1247/*
1248 * prop_type_list([{bufnr}])
1249 */
1250 void
1251f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1252{
1253 buf_T *buf = NULL;
1254
1255 if (rettv_list_alloc(rettv) == OK)
1256 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001257 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1258 return;
1259
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001260 if (argvars[0].v_type != VAR_UNKNOWN)
1261 {
1262 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1263 return;
1264 }
1265 if (buf == NULL)
1266 {
1267 if (global_proptypes != NULL)
1268 list_types(global_proptypes, rettv->vval.v_list);
1269 }
1270 else if (buf->b_proptypes != NULL)
1271 list_types(buf->b_proptypes, rettv->vval.v_list);
1272 }
1273}
1274
1275/*
1276 * Free all property types in "ht".
1277 */
1278 static void
1279clear_ht_prop_types(hashtab_T *ht)
1280{
1281 long todo;
1282 hashitem_T *hi;
1283
1284 if (ht == NULL)
1285 return;
1286
1287 todo = (long)ht->ht_used;
1288 for (hi = ht->ht_array; todo > 0; ++hi)
1289 {
1290 if (!HASHITEM_EMPTY(hi))
1291 {
1292 proptype_T *prop = HI2PT(hi);
1293
1294 vim_free(prop);
1295 --todo;
1296 }
1297 }
1298
1299 hash_clear(ht);
1300 vim_free(ht);
1301}
1302
1303#if defined(EXITFREE) || defined(PROTO)
1304/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001305 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001306 */
1307 void
1308clear_global_prop_types(void)
1309{
1310 clear_ht_prop_types(global_proptypes);
1311 global_proptypes = NULL;
1312}
1313#endif
1314
1315/*
1316 * Free all property types for "buf".
1317 */
1318 void
1319clear_buf_prop_types(buf_T *buf)
1320{
1321 clear_ht_prop_types(buf->b_proptypes);
1322 buf->b_proptypes = NULL;
1323}
1324
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001325// Struct used to return two values from adjust_prop().
1326typedef struct
1327{
1328 int dirty; // if the property was changed
1329 int can_drop; // whether after this change, the prop may be removed
1330} adjustres_T;
1331
1332/*
1333 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1334 *
1335 * Note that "col" is zero-based, while tp_col is one-based.
1336 * Only for the current buffer.
1337 * "flags" can have:
1338 * APC_SUBSTITUTE: Text is replaced, not inserted.
1339 */
1340 static adjustres_T
1341adjust_prop(
1342 textprop_T *prop,
1343 colnr_T col,
1344 int added,
1345 int flags)
1346{
1347 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1348 int start_incl = (pt != NULL
1349 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1350 || (flags & APC_SUBSTITUTE);
1351 int end_incl = (pt != NULL
1352 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1353 // Do not drop zero-width props if they later can increase in
1354 // size.
1355 int droppable = !(start_incl || end_incl);
1356 adjustres_T res = {TRUE, FALSE};
1357
1358 if (added > 0)
1359 {
1360 if (col + 1 <= prop->tp_col
1361 - (start_incl || (prop->tp_len == 0 && end_incl)))
1362 // Change is entirely before the text property: Only shift
1363 prop->tp_col += added;
1364 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1365 // Insertion was inside text property
1366 prop->tp_len += added;
1367 }
1368 else if (prop->tp_col > col + 1)
1369 {
1370 if (prop->tp_col + added < col + 1)
1371 {
1372 prop->tp_len += (prop->tp_col - 1 - col) + added;
1373 prop->tp_col = col + 1;
1374 if (prop->tp_len <= 0)
1375 {
1376 prop->tp_len = 0;
1377 res.can_drop = droppable;
1378 }
1379 }
1380 else
1381 prop->tp_col += added;
1382 }
1383 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1384 {
1385 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1386
1387 prop->tp_len += after > 0 ? added + after : added;
1388 res.can_drop = prop->tp_len <= 0 && droppable;
1389 }
1390 else
1391 res.dirty = FALSE;
1392
1393 return res;
1394}
1395
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001396/*
1397 * Adjust the columns of text properties in line "lnum" after position "col" to
1398 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001399 * Note that "col" is zero-based, while tp_col is one-based.
1400 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001401 * "flags" can have:
1402 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1403 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001404 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001405 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001406 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001407 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001408adjust_prop_columns(
1409 linenr_T lnum,
1410 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001411 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001412 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001413{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001414 int proplen;
1415 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001416 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001417 int ri, wi;
1418 size_t textlen;
1419
1420 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001421 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001422
1423 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1424 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001425 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001426 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001427
Bram Moolenaar196d1572019-01-02 23:47:18 +01001428 wi = 0; // write index
1429 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001430 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001431 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001432 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001433
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001434 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1435 res = adjust_prop(&prop, col, bytes_added, flags);
1436 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001437 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001438 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001439 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1440 && u_savesub(lnum) == FAIL)
1441 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001442 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001443
1444 // u_savesub() may have updated curbuf->b_ml, fetch it again
1445 if (curbuf->b_ml.ml_line_lnum != lnum)
1446 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001447 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001448 if (res.can_drop)
1449 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001450 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001451 ++wi;
1452 }
1453 if (dirty)
1454 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001455 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1456
1457 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1458 curbuf->b_ml.ml_line_ptr =
1459 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001460 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001461 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001462 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001463 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001464}
1465
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001466/*
1467 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001468 * "lnum_props" is the line that has the properties from before the split.
1469 * "lnum_top" is the top line.
1470 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001471 * "deleted" is the number of bytes deleted.
1472 */
1473 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001474adjust_props_for_split(
1475 linenr_T lnum_props,
1476 linenr_T lnum_top,
1477 int kept,
1478 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001479{
1480 char_u *props;
1481 int count;
1482 garray_T prevprop;
1483 garray_T nextprop;
1484 int i;
1485 int skipped = kept + deleted;
1486
1487 if (!curbuf->b_has_textprop)
1488 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001489
1490 // Get the text properties from "lnum_props".
1491 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001492 ga_init2(&prevprop, sizeof(textprop_T), 10);
1493 ga_init2(&nextprop, sizeof(textprop_T), 10);
1494
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001495 // Keep the relevant ones in the first line, reducing the length if needed.
1496 // Copy the ones that include the split to the second line.
1497 // Move the ones after the split to the second line.
1498 for (i = 0; i < count; ++i)
1499 {
1500 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001501 proptype_T *pt;
1502 int start_incl, end_incl;
1503 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001504
1505 // copy the prop to an aligned structure
1506 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1507
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001508 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1509 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1510 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1511 cont_prev = prop.tp_col + !start_incl <= kept;
1512 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1513
1514 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001515 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001516 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1517
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001518 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001519 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001520 if (p->tp_col + p->tp_len >= kept)
1521 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001522 if (cont_next)
1523 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001524 }
1525
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001526 // Only add the property to the next line if the length is bigger than
1527 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001528 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001529 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001530 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001531 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001532 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001533 if (p->tp_col > skipped)
1534 p->tp_col -= skipped - 1;
1535 else
1536 {
1537 p->tp_len -= skipped - p->tp_col;
1538 p->tp_col = 1;
1539 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001540 if (cont_prev)
1541 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001542 }
1543 }
1544
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001545 set_text_props(lnum_top, prevprop.ga_data,
1546 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001547 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001548 set_text_props(lnum_top + 1, nextprop.ga_data,
1549 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001550 ga_clear(&nextprop);
1551}
1552
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001553/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001554 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001555 */
1556 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001557prepend_joined_props(
1558 char_u *new_props,
1559 int propcount,
1560 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001561 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001562 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001563 long col,
1564 int removed)
1565{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001566 char_u *props;
1567 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1568 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001569
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001570 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001571 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001572 textprop_T prop;
1573 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001574
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001575 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1576 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1577
1578 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001579 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001580
1581 if (add_all || end)
1582 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1583 &prop, sizeof(prop));
1584 else
1585 {
1586 int j;
1587 int found = FALSE;
1588
1589 // Search for continuing prop.
1590 for (j = *props_remaining; j < propcount; ++j)
1591 {
1592 textprop_T op;
1593
1594 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1595 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1596 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001597 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001598 found = TRUE;
1599 op.tp_len += op.tp_col - prop.tp_col;
1600 op.tp_col = prop.tp_col;
1601 // Start/end is taken care of when deleting joined lines
1602 op.tp_flags = prop.tp_flags;
1603 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1604 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001605 }
1606 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001607 if (!found)
1608 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001609 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001610 }
1611}
1612
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001613#endif // FEAT_PROP_POPUP