blob: 92dc645b09772e446362113bd96948405a396160 [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)
689 id = dict_get_number(dict, (char_u *)"id");
690 if (dict_find(dict, (char_u *)"type", -1))
691 {
692 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
693 proptype_T *type = lookup_prop_type(name, buf);
694
695 if (type == NULL)
696 return;
697 type_id = type->pt_id;
698 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100699 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100700 if (id == -1 && type_id == -1)
701 {
702 emsg(_("E968: Need at least one of 'id' or 'type'"));
703 return;
704 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100705 if (both && (id == -1 || type_id == -1))
706 {
707 emsg(_("E860: Need 'id' and 'type' with 'both'"));
708 return;
709 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100710
711 lnum_start = lnum;
712
713 if (rettv_dict_alloc(rettv) == FAIL)
714 return;
715
716 while (1)
717 {
718 char_u *text = ml_get_buf(buf, lnum, FALSE);
719 size_t textlen = STRLEN(text) + 1;
720 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200721 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100722 int i;
723 textprop_T prop;
724 int prop_start;
725 int prop_end;
726
727 for (i = 0; i < count; ++i)
728 {
729 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
730 sizeof(textprop_T));
731
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100732 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100733 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100734 if (dir < 0)
735 {
736 if (col < prop.tp_col)
737 break;
738 }
739 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
740 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100741 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100742 if (both ? prop.tp_id == id && prop.tp_type == type_id
743 : prop.tp_id == id || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100744 {
745 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100746 if (lnum_start == lnum
747 && col >= prop.tp_col
748 && (col <= prop.tp_col + prop.tp_len
749 - (prop.tp_len != 0)))
750 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100751
752 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
753 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
754 if (!prop_start && prop_end && dir > 0)
755 seen_end = 1;
756
757 // Skip lines without the start flag.
758 if (!prop_start)
759 {
760 // Always search backwards for start when search started
761 // on a prop and we're not skipping.
762 if (start_pos_has_prop && !skipstart)
763 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200764 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100765 }
766
767 // If skipstart is true, skip the prop at start pos (even if
768 // continued from another line).
769 if (start_pos_has_prop && skipstart && !seen_end)
770 {
771 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200772 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100773 }
774
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100775 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
776 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
777
778 return;
779 }
780 }
781
782 if (dir > 0)
783 {
784 if (lnum >= buf->b_ml.ml_line_count)
785 break;
786 lnum++;
787 }
788 else
789 {
790 if (lnum <= 1)
791 break;
792 lnum--;
793 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100794 // Adjust col to indicate that we're continuing from prev/next line.
795 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100796 }
797}
798
799/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100800 * prop_list({lnum} [, {bufnr}])
801 */
802 void
803f_prop_list(typval_T *argvars, typval_T *rettv)
804{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200805 linenr_T lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100806 buf_T *buf = curbuf;
807
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200808 if (in_vim9script()
809 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200810 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200811 return;
812
813 lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100814 if (argvars[1].v_type != VAR_UNKNOWN)
815 {
816 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
817 return;
818 }
819 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
820 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200821 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100822 return;
823 }
824
825 if (rettv_list_alloc(rettv) == OK)
826 {
827 char_u *text = ml_get_buf(buf, lnum, FALSE);
828 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100829 int count = (int)((buf->b_ml.ml_line_len - textlen)
830 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100831 int i;
832 textprop_T prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100833
834 for (i = 0; i < count; ++i)
835 {
836 dict_T *d = dict_alloc();
837
838 if (d == NULL)
839 break;
840 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
841 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100842 prop_fill_dict(d, &prop, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100843 list_append_dict(rettv->vval.v_list, d);
844 }
845 }
846}
847
848/*
849 * prop_remove({props} [, {lnum} [, {lnum_end}]])
850 */
851 void
852f_prop_remove(typval_T *argvars, typval_T *rettv)
853{
854 linenr_T start = 1;
855 linenr_T end = 0;
856 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +0200857 linenr_T first_changed = 0;
858 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100859 dict_T *dict;
860 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200861 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100862 int id = -1;
863 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200864 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100865
866 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200867
868 if (in_vim9script()
869 && (check_for_dict_arg(argvars, 0) == FAIL
870 || check_for_opt_number_arg(argvars, 1) == FAIL
871 || (argvars[1].v_type != VAR_UNKNOWN
872 && check_for_opt_number_arg(argvars, 2) == FAIL)))
873 return;
874
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100875 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100877 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100878 return;
879 }
880
881 if (argvars[1].v_type != VAR_UNKNOWN)
882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100883 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100884 end = start;
885 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100886 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100887 if (start < 1 || end < 1)
888 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200889 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100890 return;
891 }
892 }
893
894 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200895 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
896 return;
897 if (buf->b_ml.ml_mfp == NULL)
898 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100899
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200900 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100901
902 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100903 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100904 if (dict_find(dict, (char_u *)"type", -1))
905 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100906 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100907 proptype_T *type = lookup_prop_type(name, buf);
908
909 if (type == NULL)
910 return;
911 type_id = type->pt_id;
912 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200913 both = dict_get_bool(dict, (char_u *)"both", FALSE);
914
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100915 if (id == -1 && type_id == -1)
916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100917 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100918 return;
919 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100920 if (both && (id == -1 || type_id == -1))
921 {
922 emsg(_("E860: Need 'id' and 'type' with 'both'"));
923 return;
924 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100925
926 if (end == 0)
927 end = buf->b_ml.ml_line_count;
928 for (lnum = start; lnum <= end; ++lnum)
929 {
930 char_u *text;
931 size_t len;
932
933 if (lnum > buf->b_ml.ml_line_count)
934 break;
935 text = ml_get_buf(buf, lnum, FALSE);
936 len = STRLEN(text) + 1;
937 if ((size_t)buf->b_ml.ml_line_len > len)
938 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200939 static textprop_T textprop; // static because of alignment
940 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100941
942 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
943 / sizeof(textprop_T); ++idx)
944 {
945 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
946 + idx * sizeof(textprop_T);
947 size_t taillen;
948
949 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100950 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
951 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100952 {
953 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
954 {
955 char_u *newptr = alloc(buf->b_ml.ml_line_len);
956
957 // need to allocate the line to be able to change it
958 if (newptr == NULL)
959 return;
960 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
961 buf->b_ml.ml_line_len);
962 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100963 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
964
965 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200966 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100967 }
968
969 taillen = buf->b_ml.ml_line_len - len
970 - (idx + 1) * sizeof(textprop_T);
971 if (taillen > 0)
972 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
973 taillen);
974 buf->b_ml.ml_line_len -= sizeof(textprop_T);
975 --idx;
976
Bram Moolenaar965c0442021-05-17 00:22:06 +0200977 if (first_changed == 0)
978 first_changed = lnum;
979 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100980 ++rettv->vval.v_number;
981 if (!do_all)
982 break;
983 }
984 }
985 }
986 }
Bram Moolenaar965c0442021-05-17 00:22:06 +0200987 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200988 {
Bram Moolenaar965c0442021-05-17 00:22:06 +0200989 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
990 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200991 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100992}
993
994/*
995 * Common for f_prop_type_add() and f_prop_type_change().
996 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200997 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100998prop_type_set(typval_T *argvars, int add)
999{
1000 char_u *name;
1001 buf_T *buf = NULL;
1002 dict_T *dict;
1003 dictitem_T *di;
1004 proptype_T *prop;
1005
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001006 if (in_vim9script()
1007 && (check_for_string_arg(argvars, 0) == FAIL
1008 || check_for_dict_arg(argvars, 1) == FAIL))
1009 return;
1010
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001011 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001012 if (*name == NUL)
1013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001014 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001015 return;
1016 }
1017
1018 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1019 return;
1020 dict = argvars[1].vval.v_dict;
1021
1022 prop = find_prop(name, buf);
1023 if (add)
1024 {
1025 hashtab_T **htp;
1026
1027 if (prop != NULL)
1028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001029 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001030 return;
1031 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001032 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001033 if (prop == NULL)
1034 return;
1035 STRCPY(prop->pt_name, name);
1036 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001037 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001038 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1039 if (*htp == NULL)
1040 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001041 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001042 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001043 {
1044 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001045 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001046 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001047 hash_init(*htp);
1048 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001049 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001050 }
1051 else
1052 {
1053 if (prop == NULL)
1054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001055 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001056 return;
1057 }
1058 }
1059
1060 if (dict != NULL)
1061 {
1062 di = dict_find(dict, (char_u *)"highlight", -1);
1063 if (di != NULL)
1064 {
1065 char_u *highlight;
1066 int hl_id = 0;
1067
Bram Moolenaar8f667172018-12-14 15:38:31 +01001068 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001069 if (highlight != NULL && *highlight != NUL)
1070 hl_id = syn_name2id(highlight);
1071 if (hl_id <= 0)
1072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001073 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001074 highlight == NULL ? (char_u *)"" : highlight);
1075 return;
1076 }
1077 prop->pt_hl_id = hl_id;
1078 }
1079
Bram Moolenaar58187f12019-05-05 16:33:47 +02001080 di = dict_find(dict, (char_u *)"combine", -1);
1081 if (di != NULL)
1082 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001083 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001084 prop->pt_flags |= PT_FLAG_COMBINE;
1085 else
1086 prop->pt_flags &= ~PT_FLAG_COMBINE;
1087 }
1088
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001089 di = dict_find(dict, (char_u *)"priority", -1);
1090 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001091 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001092
1093 di = dict_find(dict, (char_u *)"start_incl", -1);
1094 if (di != NULL)
1095 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001096 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001097 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1098 else
1099 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1100 }
1101
1102 di = dict_find(dict, (char_u *)"end_incl", -1);
1103 if (di != NULL)
1104 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001105 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001106 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1107 else
1108 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1109 }
1110 }
1111}
1112
1113/*
1114 * prop_type_add({name}, {props})
1115 */
1116 void
1117f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1118{
1119 prop_type_set(argvars, TRUE);
1120}
1121
1122/*
1123 * prop_type_change({name}, {props})
1124 */
1125 void
1126f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1127{
1128 prop_type_set(argvars, FALSE);
1129}
1130
1131/*
1132 * prop_type_delete({name} [, {bufnr}])
1133 */
1134 void
1135f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1136{
1137 char_u *name;
1138 buf_T *buf = NULL;
1139 hashitem_T *hi;
1140
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001141 if (in_vim9script()
1142 && (check_for_string_arg(argvars, 0) == FAIL
1143 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1144 return;
1145
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001146 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001147 if (*name == NUL)
1148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001149 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001150 return;
1151 }
1152
1153 if (argvars[1].v_type != VAR_UNKNOWN)
1154 {
1155 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1156 return;
1157 }
1158
1159 hi = find_prop_hi(name, buf);
1160 if (hi != NULL)
1161 {
1162 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001163 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001164
1165 if (buf == NULL)
1166 ht = global_proptypes;
1167 else
1168 ht = buf->b_proptypes;
1169 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001170 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001171 }
1172}
1173
1174/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001175 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001176 */
1177 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001178f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001179{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001180 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001181
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001182 if (in_vim9script()
1183 && (check_for_string_arg(argvars, 0) == FAIL
1184 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1185 return;
1186
1187 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001188 if (*name == NUL)
1189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001190 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001191 return;
1192 }
1193 if (rettv_dict_alloc(rettv) == OK)
1194 {
1195 proptype_T *prop = NULL;
1196 buf_T *buf = NULL;
1197
1198 if (argvars[1].v_type != VAR_UNKNOWN)
1199 {
1200 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1201 return;
1202 }
1203
1204 prop = find_prop(name, buf);
1205 if (prop != NULL)
1206 {
1207 dict_T *d = rettv->vval.v_dict;
1208
1209 if (prop->pt_hl_id > 0)
1210 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1211 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001212 dict_add_number(d, "combine",
1213 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001214 dict_add_number(d, "start_incl",
1215 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1216 dict_add_number(d, "end_incl",
1217 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1218 if (buf != NULL)
1219 dict_add_number(d, "bufnr", buf->b_fnum);
1220 }
1221 }
1222}
1223
1224 static void
1225list_types(hashtab_T *ht, list_T *l)
1226{
1227 long todo;
1228 hashitem_T *hi;
1229
1230 todo = (long)ht->ht_used;
1231 for (hi = ht->ht_array; todo > 0; ++hi)
1232 {
1233 if (!HASHITEM_EMPTY(hi))
1234 {
1235 proptype_T *prop = HI2PT(hi);
1236
1237 list_append_string(l, prop->pt_name, -1);
1238 --todo;
1239 }
1240 }
1241}
1242
1243/*
1244 * prop_type_list([{bufnr}])
1245 */
1246 void
1247f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1248{
1249 buf_T *buf = NULL;
1250
1251 if (rettv_list_alloc(rettv) == OK)
1252 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001253 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1254 return;
1255
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001256 if (argvars[0].v_type != VAR_UNKNOWN)
1257 {
1258 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1259 return;
1260 }
1261 if (buf == NULL)
1262 {
1263 if (global_proptypes != NULL)
1264 list_types(global_proptypes, rettv->vval.v_list);
1265 }
1266 else if (buf->b_proptypes != NULL)
1267 list_types(buf->b_proptypes, rettv->vval.v_list);
1268 }
1269}
1270
1271/*
1272 * Free all property types in "ht".
1273 */
1274 static void
1275clear_ht_prop_types(hashtab_T *ht)
1276{
1277 long todo;
1278 hashitem_T *hi;
1279
1280 if (ht == NULL)
1281 return;
1282
1283 todo = (long)ht->ht_used;
1284 for (hi = ht->ht_array; todo > 0; ++hi)
1285 {
1286 if (!HASHITEM_EMPTY(hi))
1287 {
1288 proptype_T *prop = HI2PT(hi);
1289
1290 vim_free(prop);
1291 --todo;
1292 }
1293 }
1294
1295 hash_clear(ht);
1296 vim_free(ht);
1297}
1298
1299#if defined(EXITFREE) || defined(PROTO)
1300/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001301 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001302 */
1303 void
1304clear_global_prop_types(void)
1305{
1306 clear_ht_prop_types(global_proptypes);
1307 global_proptypes = NULL;
1308}
1309#endif
1310
1311/*
1312 * Free all property types for "buf".
1313 */
1314 void
1315clear_buf_prop_types(buf_T *buf)
1316{
1317 clear_ht_prop_types(buf->b_proptypes);
1318 buf->b_proptypes = NULL;
1319}
1320
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001321// Struct used to return two values from adjust_prop().
1322typedef struct
1323{
1324 int dirty; // if the property was changed
1325 int can_drop; // whether after this change, the prop may be removed
1326} adjustres_T;
1327
1328/*
1329 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1330 *
1331 * Note that "col" is zero-based, while tp_col is one-based.
1332 * Only for the current buffer.
1333 * "flags" can have:
1334 * APC_SUBSTITUTE: Text is replaced, not inserted.
1335 */
1336 static adjustres_T
1337adjust_prop(
1338 textprop_T *prop,
1339 colnr_T col,
1340 int added,
1341 int flags)
1342{
1343 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1344 int start_incl = (pt != NULL
1345 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1346 || (flags & APC_SUBSTITUTE);
1347 int end_incl = (pt != NULL
1348 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1349 // Do not drop zero-width props if they later can increase in
1350 // size.
1351 int droppable = !(start_incl || end_incl);
1352 adjustres_T res = {TRUE, FALSE};
1353
1354 if (added > 0)
1355 {
1356 if (col + 1 <= prop->tp_col
1357 - (start_incl || (prop->tp_len == 0 && end_incl)))
1358 // Change is entirely before the text property: Only shift
1359 prop->tp_col += added;
1360 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1361 // Insertion was inside text property
1362 prop->tp_len += added;
1363 }
1364 else if (prop->tp_col > col + 1)
1365 {
1366 if (prop->tp_col + added < col + 1)
1367 {
1368 prop->tp_len += (prop->tp_col - 1 - col) + added;
1369 prop->tp_col = col + 1;
1370 if (prop->tp_len <= 0)
1371 {
1372 prop->tp_len = 0;
1373 res.can_drop = droppable;
1374 }
1375 }
1376 else
1377 prop->tp_col += added;
1378 }
1379 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1380 {
1381 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1382
1383 prop->tp_len += after > 0 ? added + after : added;
1384 res.can_drop = prop->tp_len <= 0 && droppable;
1385 }
1386 else
1387 res.dirty = FALSE;
1388
1389 return res;
1390}
1391
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001392/*
1393 * Adjust the columns of text properties in line "lnum" after position "col" to
1394 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001395 * Note that "col" is zero-based, while tp_col is one-based.
1396 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001397 * "flags" can have:
1398 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1399 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001400 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001401 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001402 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001403 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001404adjust_prop_columns(
1405 linenr_T lnum,
1406 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001407 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001408 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001409{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001410 int proplen;
1411 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001412 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001413 int ri, wi;
1414 size_t textlen;
1415
1416 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001417 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001418
1419 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1420 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001421 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001422 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001423
Bram Moolenaar196d1572019-01-02 23:47:18 +01001424 wi = 0; // write index
1425 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001426 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001427 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001428 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001429
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001430 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1431 res = adjust_prop(&prop, col, bytes_added, flags);
1432 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001433 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001434 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001435 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1436 && u_savesub(lnum) == FAIL)
1437 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001438 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001439
1440 // u_savesub() may have updated curbuf->b_ml, fetch it again
1441 if (curbuf->b_ml.ml_line_lnum != lnum)
1442 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001443 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001444 if (res.can_drop)
1445 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001446 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001447 ++wi;
1448 }
1449 if (dirty)
1450 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001451 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1452
1453 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1454 curbuf->b_ml.ml_line_ptr =
1455 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001456 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001457 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001458 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001459 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001460}
1461
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001462/*
1463 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001464 * "lnum_props" is the line that has the properties from before the split.
1465 * "lnum_top" is the top line.
1466 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001467 * "deleted" is the number of bytes deleted.
1468 */
1469 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001470adjust_props_for_split(
1471 linenr_T lnum_props,
1472 linenr_T lnum_top,
1473 int kept,
1474 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001475{
1476 char_u *props;
1477 int count;
1478 garray_T prevprop;
1479 garray_T nextprop;
1480 int i;
1481 int skipped = kept + deleted;
1482
1483 if (!curbuf->b_has_textprop)
1484 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001485
1486 // Get the text properties from "lnum_props".
1487 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001488 ga_init2(&prevprop, sizeof(textprop_T), 10);
1489 ga_init2(&nextprop, sizeof(textprop_T), 10);
1490
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001491 // Keep the relevant ones in the first line, reducing the length if needed.
1492 // Copy the ones that include the split to the second line.
1493 // Move the ones after the split to the second line.
1494 for (i = 0; i < count; ++i)
1495 {
1496 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001497 proptype_T *pt;
1498 int start_incl, end_incl;
1499 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001500
1501 // copy the prop to an aligned structure
1502 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1503
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001504 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1505 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1506 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1507 cont_prev = prop.tp_col + !start_incl <= kept;
1508 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1509
1510 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001511 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001512 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1513
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001514 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001515 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001516 if (p->tp_col + p->tp_len >= kept)
1517 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001518 if (cont_next)
1519 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001520 }
1521
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001522 // Only add the property to the next line if the length is bigger than
1523 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001524 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001525 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001526 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001527 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001528 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001529 if (p->tp_col > skipped)
1530 p->tp_col -= skipped - 1;
1531 else
1532 {
1533 p->tp_len -= skipped - p->tp_col;
1534 p->tp_col = 1;
1535 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001536 if (cont_prev)
1537 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001538 }
1539 }
1540
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001541 set_text_props(lnum_top, prevprop.ga_data,
1542 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001543 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001544 set_text_props(lnum_top + 1, nextprop.ga_data,
1545 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001546 ga_clear(&nextprop);
1547}
1548
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001549/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001550 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001551 */
1552 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001553prepend_joined_props(
1554 char_u *new_props,
1555 int propcount,
1556 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001557 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001558 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001559 long col,
1560 int removed)
1561{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001562 char_u *props;
1563 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1564 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001565
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001566 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001567 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001568 textprop_T prop;
1569 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001570
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001571 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1572 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1573
1574 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001575 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001576
1577 if (add_all || end)
1578 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1579 &prop, sizeof(prop));
1580 else
1581 {
1582 int j;
1583 int found = FALSE;
1584
1585 // Search for continuing prop.
1586 for (j = *props_remaining; j < propcount; ++j)
1587 {
1588 textprop_T op;
1589
1590 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1591 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1592 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001593 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001594 found = TRUE;
1595 op.tp_len += op.tp_col - prop.tp_col;
1596 op.tp_col = prop.tp_col;
1597 // Start/end is taken care of when deleting joined lines
1598 op.tp_flags = prop.tp_flags;
1599 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1600 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001601 }
1602 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001603 if (!found)
1604 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001605 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001606 }
1607}
1608
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001609#endif // FEAT_PROP_POPUP