blob: c2000a3c3e0b7391697900f29d9888c6f2926c8b [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);
143 if (di != NULL)
144 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200145 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100146 if (*buf == NULL)
147 return FAIL;
148 }
149 return OK;
150}
151
152/*
153 * prop_add({lnum}, {col}, {props})
154 */
155 void
156f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
157{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100158 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100159 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100160
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100161 start_lnum = tv_get_number(&argvars[0]);
162 start_col = tv_get_number(&argvars[1]);
163 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100165 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100166 return;
167 }
168 if (argvars[2].v_type != VAR_DICT)
169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100170 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100171 return;
172 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200173
174 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
175 curbuf, &argvars[2]);
176}
177
178/*
179 * Shared between prop_add() and popup_create().
180 * "dict_arg" is the function argument of a dict containing "bufnr".
181 * it is NULL for popup_create().
182 */
183 void
184prop_add_common(
185 linenr_T start_lnum,
186 colnr_T start_col,
187 dict_T *dict,
188 buf_T *default_buf,
189 typval_T *dict_arg)
190{
191 linenr_T lnum;
192 linenr_T end_lnum;
193 colnr_T end_col;
194 char_u *type_name;
195 proptype_T *type;
196 buf_T *buf = default_buf;
197 int id = 0;
198 char_u *newtext;
199 int proplen;
200 size_t textlen;
201 char_u *props = NULL;
202 char_u *newprops;
203 textprop_T tmp_prop;
204 int i;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100205
206 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100208 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100209 return;
210 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100211 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100212
213 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
214 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100215 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
216 if (end_lnum < start_lnum)
217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100218 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100219 return;
220 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100221 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100222 else
223 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100224
225 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100226 {
227 long length = dict_get_number(dict, (char_u *)"length");
228
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100229 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100231 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100232 return;
233 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100234 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100235 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100236 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
237 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100238 end_col = dict_get_number(dict, (char_u *)"end_col");
239 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100241 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100242 return;
243 }
244 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100245 else if (start_lnum == end_lnum)
246 end_col = start_col;
247 else
248 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100249
250 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100251 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100252
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200253 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100254 return;
255
256 type = lookup_prop_type(type_name, buf);
257 if (type == NULL)
258 return;
259
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100260 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100262 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100263 return;
264 }
265 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100267 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100268 return;
269 }
270
Bram Moolenaard79eef22019-05-24 20:41:55 +0200271 if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaar45311b52019-08-13 22:27:32 +0200272 {
273 emsg(_("E275: Cannot add text property to unloaded buffer"));
274 return;
275 }
Bram Moolenaard79eef22019-05-24 20:41:55 +0200276
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100277 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100278 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100279 colnr_T col; // start column
280 long length; // in bytes
281
282 // Fetch the line to get the ml_line_len field updated.
283 proplen = get_text_props(buf, lnum, &props, TRUE);
284 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
285
286 if (lnum == start_lnum)
287 col = start_col;
288 else
289 col = 1;
290 if (col - 1 > (colnr_T)textlen)
291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100292 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100293 return;
294 }
295
296 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100297 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100298 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100299 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100300 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100301 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100302 if (length < 0)
303 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100304
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100305 // Allocate the new line with space for the new property.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100306 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
307 if (newtext == NULL)
308 return;
309 // Copy the text, including terminating NUL.
310 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
311
312 // Find the index where to insert the new property.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200313 // Since the text properties are not aligned properly when stored with
314 // the text, we need to copy them as bytes before using it as a struct.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100315 for (i = 0; i < proplen; ++i)
316 {
317 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200318 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100319 if (tmp_prop.tp_col >= col)
320 break;
321 }
322 newprops = newtext + textlen;
323 if (i > 0)
324 mch_memmove(newprops, props, sizeof(textprop_T) * i);
325
326 tmp_prop.tp_col = col;
327 tmp_prop.tp_len = length;
328 tmp_prop.tp_id = id;
329 tmp_prop.tp_type = type->pt_id;
330 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
331 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
332 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200333 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100334
335 if (i < proplen)
336 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
337 props + i * sizeof(textprop_T),
338 sizeof(textprop_T) * (proplen - i));
339
340 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
341 vim_free(buf->b_ml.ml_line_ptr);
342 buf->b_ml.ml_line_ptr = newtext;
343 buf->b_ml.ml_line_len += sizeof(textprop_T);
344 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100345 }
346
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100347 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200348 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
349 redraw_buf_later(buf, VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100350}
351
352/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100353 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100354 * Returns the number of text properties and, when non-zero, a pointer to the
355 * first one in "props" (note that it is not aligned, therefore the char_u
356 * pointer).
357 */
358 int
359get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
360{
361 char_u *text;
362 size_t textlen;
363 size_t proplen;
364
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100365 // Be quick when no text property types have been defined or the buffer,
366 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200367 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100368 return 0;
369
370 // Fetch the line to get the ml_line_len field updated.
371 text = ml_get_buf(buf, lnum, will_change);
372 textlen = STRLEN(text) + 1;
373 proplen = buf->b_ml.ml_line_len - textlen;
374 if (proplen % sizeof(textprop_T) != 0)
375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100376 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100377 return 0;
378 }
379 if (proplen > 0)
380 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100381 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100382}
383
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200384/**
385 * Return the number of text properties on line "lnum" in the current buffer.
386 * When "only_starting" is true only text properties starting in this line will
387 * be considered.
388 */
389 int
390count_props(linenr_T lnum, int only_starting)
391{
392 char_u *props;
393 int proplen = get_text_props(curbuf, lnum, &props, 0);
394 int result = proplen;
395 int i;
396 textprop_T prop;
397
398 if (only_starting)
399 for (i = 0; i < proplen; ++i)
400 {
401 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
402 if (prop.tp_flags & TP_FLAG_CONT_PREV)
403 --result;
404 }
405 return result;
406}
407
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100408/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200409 * Find text property "type_id" in the visible lines of window "wp".
410 * Match "id" when it is > 0.
411 * Returns FAIL when not found.
412 */
413 int
414find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
415 linenr_T *found_lnum)
416{
417 linenr_T lnum;
418 char_u *props;
419 int count;
420 int i;
421
422 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100423 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200424 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
425 {
426 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
427 for (i = 0; i < count; ++i)
428 {
429 mch_memmove(prop, props + i * sizeof(textprop_T),
430 sizeof(textprop_T));
431 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
432 {
433 *found_lnum = lnum;
434 return OK;
435 }
436 }
437 }
438 return FAIL;
439}
440
441/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100442 * Set the text properties for line "lnum" to "props" with length "len".
443 * If "len" is zero text properties are removed, "props" is not used.
444 * Any existing text properties are dropped.
445 * Only works for the current buffer.
446 */
447 static void
448set_text_props(linenr_T lnum, char_u *props, int len)
449{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100450 char_u *text;
451 char_u *newtext;
452 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100453
454 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100455 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100456 newtext = alloc(textlen + len);
457 if (newtext == NULL)
458 return;
459 mch_memmove(newtext, text, textlen);
460 if (len > 0)
461 mch_memmove(newtext + textlen, props, len);
462 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
463 vim_free(curbuf->b_ml.ml_line_ptr);
464 curbuf->b_ml.ml_line_ptr = newtext;
465 curbuf->b_ml.ml_line_len = textlen + len;
466 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
467}
468
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469 static proptype_T *
470find_type_by_id(hashtab_T *ht, int id)
471{
472 long todo;
473 hashitem_T *hi;
474
475 if (ht == NULL)
476 return NULL;
477
478 // TODO: Make this faster by keeping a list of types sorted on ID and use
479 // a binary search.
480
481 todo = (long)ht->ht_used;
482 for (hi = ht->ht_array; todo > 0; ++hi)
483 {
484 if (!HASHITEM_EMPTY(hi))
485 {
486 proptype_T *prop = HI2PT(hi);
487
488 if (prop->pt_id == id)
489 return prop;
490 --todo;
491 }
492 }
493 return NULL;
494}
495
496/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100497 * Fill 'dict' with text properties in 'prop'.
498 */
499 static void
500prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
501{
502 proptype_T *pt;
503
504 dict_add_number(dict, "col", prop->tp_col);
505 dict_add_number(dict, "length", prop->tp_len);
506 dict_add_number(dict, "id", prop->tp_id);
507 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
508 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
509 pt = text_prop_type_by_id(buf, prop->tp_type);
510 if (pt != NULL)
511 dict_add_string(dict, "type", pt->pt_name);
512}
513
514/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100515 * Find a property type by ID in "buf" or globally.
516 * Returns NULL if not found.
517 */
518 proptype_T *
519text_prop_type_by_id(buf_T *buf, int id)
520{
521 proptype_T *type;
522
523 type = find_type_by_id(buf->b_proptypes, id);
524 if (type == NULL)
525 type = find_type_by_id(global_proptypes, id);
526 return type;
527}
528
529/*
530 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
531 */
532 void
533f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
534{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100535 linenr_T start = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100536 linenr_T end = start;
537 linenr_T lnum;
538 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100539 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100540
541 if (argvars[1].v_type != VAR_UNKNOWN)
542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100543 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100544 if (argvars[2].v_type != VAR_UNKNOWN)
545 {
546 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
547 return;
548 }
549 }
550 if (start < 1 || end < 1)
551 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200552 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100553 return;
554 }
555
556 for (lnum = start; lnum <= end; ++lnum)
557 {
558 char_u *text;
559 size_t len;
560
561 if (lnum > buf->b_ml.ml_line_count)
562 break;
563 text = ml_get_buf(buf, lnum, FALSE);
564 len = STRLEN(text) + 1;
565 if ((size_t)buf->b_ml.ml_line_len > len)
566 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100567 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100568 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
569 {
570 char_u *newtext = vim_strsave(text);
571
572 // need to allocate the line now
573 if (newtext == NULL)
574 return;
575 buf->b_ml.ml_line_ptr = newtext;
576 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
577 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100578 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100579 }
580 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100581 if (did_clear)
582 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100583}
584
585/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100586 * prop_find({props} [, {direction}])
587 */
588 void
589f_prop_find(typval_T *argvars, typval_T *rettv)
590{
591 pos_T *cursor = &curwin->w_cursor;
592 dict_T *dict;
593 buf_T *buf = curbuf;
594 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200595 int lnum_start;
596 int start_pos_has_prop = 0;
597 int seen_end = 0;
598 int id = -1;
599 int type_id = -1;
600 int skipstart = 0;
601 int lnum = -1;
602 int col = -1;
603 int dir = 1; // 1 = forward, -1 = backward
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100604 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100605
606 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
607 {
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200608 emsg(_(e_dictreq));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100609 return;
610 }
611 dict = argvars[0].vval.v_dict;
612
613 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
614 return;
615 if (buf->b_ml.ml_mfp == NULL)
616 return;
617
618 if (argvars[1].v_type != VAR_UNKNOWN)
619 {
620 char_u *dir_s = tv_get_string(&argvars[1]);
621
622 if (*dir_s == 'b')
623 dir = -1;
624 else if (*dir_s != 'f')
625 {
626 emsg(_(e_invarg));
627 return;
628 }
629 }
630
631 di = dict_find(dict, (char_u *)"lnum", -1);
632 if (di != NULL)
633 lnum = tv_get_number(&di->di_tv);
634
635 di = dict_find(dict, (char_u *)"col", -1);
636 if (di != NULL)
637 col = tv_get_number(&di->di_tv);
638
639 if (lnum == -1)
640 {
641 lnum = cursor->lnum;
642 col = cursor->col + 1;
643 }
644 else if (col == -1)
645 col = 1;
646
647 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
648 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200649 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100650 return;
651 }
652
Bram Moolenaareb245562020-09-03 22:33:44 +0200653 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100654
655 if (dict_find(dict, (char_u *)"id", -1) != NULL)
656 id = dict_get_number(dict, (char_u *)"id");
657 if (dict_find(dict, (char_u *)"type", -1))
658 {
659 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
660 proptype_T *type = lookup_prop_type(name, buf);
661
662 if (type == NULL)
663 return;
664 type_id = type->pt_id;
665 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100666 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100667 if (id == -1 && type_id == -1)
668 {
669 emsg(_("E968: Need at least one of 'id' or 'type'"));
670 return;
671 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100672 if (both && (id == -1 || type_id == -1))
673 {
674 emsg(_("E860: Need 'id' and 'type' with 'both'"));
675 return;
676 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100677
678 lnum_start = lnum;
679
680 if (rettv_dict_alloc(rettv) == FAIL)
681 return;
682
683 while (1)
684 {
685 char_u *text = ml_get_buf(buf, lnum, FALSE);
686 size_t textlen = STRLEN(text) + 1;
687 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200688 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100689 int i;
690 textprop_T prop;
691 int prop_start;
692 int prop_end;
693
694 for (i = 0; i < count; ++i)
695 {
696 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
697 sizeof(textprop_T));
698
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100699 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100700 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100701 if (dir < 0)
702 {
703 if (col < prop.tp_col)
704 break;
705 }
706 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
707 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100708 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100709 if (both ? prop.tp_id == id && prop.tp_type == type_id
710 : prop.tp_id == id || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100711 {
712 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100713 if (lnum_start == lnum
714 && col >= prop.tp_col
715 && (col <= prop.tp_col + prop.tp_len
716 - (prop.tp_len != 0)))
717 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100718
719 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
720 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
721 if (!prop_start && prop_end && dir > 0)
722 seen_end = 1;
723
724 // Skip lines without the start flag.
725 if (!prop_start)
726 {
727 // Always search backwards for start when search started
728 // on a prop and we're not skipping.
729 if (start_pos_has_prop && !skipstart)
730 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200731 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100732 }
733
734 // If skipstart is true, skip the prop at start pos (even if
735 // continued from another line).
736 if (start_pos_has_prop && skipstart && !seen_end)
737 {
738 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200739 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100740 }
741
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100742 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
743 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
744
745 return;
746 }
747 }
748
749 if (dir > 0)
750 {
751 if (lnum >= buf->b_ml.ml_line_count)
752 break;
753 lnum++;
754 }
755 else
756 {
757 if (lnum <= 1)
758 break;
759 lnum--;
760 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100761 // Adjust col to indicate that we're continuing from prev/next line.
762 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100763 }
764}
765
766/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100767 * prop_list({lnum} [, {bufnr}])
768 */
769 void
770f_prop_list(typval_T *argvars, typval_T *rettv)
771{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200772 linenr_T lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100773 buf_T *buf = curbuf;
774
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200775 if (in_vim9script()
776 && (check_for_number_arg(argvars, 0) == FAIL
777 || (argvars[1].v_type != VAR_UNKNOWN &&
778 check_for_dict_arg(argvars, 1) == FAIL)))
779 return;
780
781 lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100782 if (argvars[1].v_type != VAR_UNKNOWN)
783 {
784 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
785 return;
786 }
787 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
788 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200789 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100790 return;
791 }
792
793 if (rettv_list_alloc(rettv) == OK)
794 {
795 char_u *text = ml_get_buf(buf, lnum, FALSE);
796 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100797 int count = (int)((buf->b_ml.ml_line_len - textlen)
798 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100799 int i;
800 textprop_T prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100801
802 for (i = 0; i < count; ++i)
803 {
804 dict_T *d = dict_alloc();
805
806 if (d == NULL)
807 break;
808 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
809 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100810 prop_fill_dict(d, &prop, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100811 list_append_dict(rettv->vval.v_list, d);
812 }
813 }
814}
815
816/*
817 * prop_remove({props} [, {lnum} [, {lnum_end}]])
818 */
819 void
820f_prop_remove(typval_T *argvars, typval_T *rettv)
821{
822 linenr_T start = 1;
823 linenr_T end = 0;
824 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +0200825 linenr_T first_changed = 0;
826 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100827 dict_T *dict;
828 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200829 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100830 int id = -1;
831 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200832 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100833
834 rettv->vval.v_number = 0;
835 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
836 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100838 return;
839 }
840
841 if (argvars[1].v_type != VAR_UNKNOWN)
842 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100843 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100844 end = start;
845 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100846 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100847 if (start < 1 || end < 1)
848 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200849 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100850 return;
851 }
852 }
853
854 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200855 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
856 return;
857 if (buf->b_ml.ml_mfp == NULL)
858 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100859
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200860 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100861
862 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100863 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100864 if (dict_find(dict, (char_u *)"type", -1))
865 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100866 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100867 proptype_T *type = lookup_prop_type(name, buf);
868
869 if (type == NULL)
870 return;
871 type_id = type->pt_id;
872 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200873 both = dict_get_bool(dict, (char_u *)"both", FALSE);
874
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100875 if (id == -1 && type_id == -1)
876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100877 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100878 return;
879 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100880 if (both && (id == -1 || type_id == -1))
881 {
882 emsg(_("E860: Need 'id' and 'type' with 'both'"));
883 return;
884 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100885
886 if (end == 0)
887 end = buf->b_ml.ml_line_count;
888 for (lnum = start; lnum <= end; ++lnum)
889 {
890 char_u *text;
891 size_t len;
892
893 if (lnum > buf->b_ml.ml_line_count)
894 break;
895 text = ml_get_buf(buf, lnum, FALSE);
896 len = STRLEN(text) + 1;
897 if ((size_t)buf->b_ml.ml_line_len > len)
898 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200899 static textprop_T textprop; // static because of alignment
900 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100901
902 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
903 / sizeof(textprop_T); ++idx)
904 {
905 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
906 + idx * sizeof(textprop_T);
907 size_t taillen;
908
909 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100910 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
911 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100912 {
913 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
914 {
915 char_u *newptr = alloc(buf->b_ml.ml_line_len);
916
917 // need to allocate the line to be able to change it
918 if (newptr == NULL)
919 return;
920 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
921 buf->b_ml.ml_line_len);
922 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100923 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
924
925 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200926 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100927 }
928
929 taillen = buf->b_ml.ml_line_len - len
930 - (idx + 1) * sizeof(textprop_T);
931 if (taillen > 0)
932 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
933 taillen);
934 buf->b_ml.ml_line_len -= sizeof(textprop_T);
935 --idx;
936
Bram Moolenaar965c0442021-05-17 00:22:06 +0200937 if (first_changed == 0)
938 first_changed = lnum;
939 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100940 ++rettv->vval.v_number;
941 if (!do_all)
942 break;
943 }
944 }
945 }
946 }
Bram Moolenaar965c0442021-05-17 00:22:06 +0200947 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200948 {
Bram Moolenaar965c0442021-05-17 00:22:06 +0200949 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
950 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200951 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100952}
953
954/*
955 * Common for f_prop_type_add() and f_prop_type_change().
956 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200957 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100958prop_type_set(typval_T *argvars, int add)
959{
960 char_u *name;
961 buf_T *buf = NULL;
962 dict_T *dict;
963 dictitem_T *di;
964 proptype_T *prop;
965
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100966 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100967 if (*name == NUL)
968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100969 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100970 return;
971 }
972
973 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
974 return;
975 dict = argvars[1].vval.v_dict;
976
977 prop = find_prop(name, buf);
978 if (add)
979 {
980 hashtab_T **htp;
981
982 if (prop != NULL)
983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100984 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100985 return;
986 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200987 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100988 if (prop == NULL)
989 return;
990 STRCPY(prop->pt_name, name);
991 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +0100992 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100993 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
994 if (*htp == NULL)
995 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200996 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100997 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100998 {
999 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001000 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001001 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001002 hash_init(*htp);
1003 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001004 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001005 }
1006 else
1007 {
1008 if (prop == NULL)
1009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001010 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001011 return;
1012 }
1013 }
1014
1015 if (dict != NULL)
1016 {
1017 di = dict_find(dict, (char_u *)"highlight", -1);
1018 if (di != NULL)
1019 {
1020 char_u *highlight;
1021 int hl_id = 0;
1022
Bram Moolenaar8f667172018-12-14 15:38:31 +01001023 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001024 if (highlight != NULL && *highlight != NUL)
1025 hl_id = syn_name2id(highlight);
1026 if (hl_id <= 0)
1027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001028 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001029 highlight == NULL ? (char_u *)"" : highlight);
1030 return;
1031 }
1032 prop->pt_hl_id = hl_id;
1033 }
1034
Bram Moolenaar58187f12019-05-05 16:33:47 +02001035 di = dict_find(dict, (char_u *)"combine", -1);
1036 if (di != NULL)
1037 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001038 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001039 prop->pt_flags |= PT_FLAG_COMBINE;
1040 else
1041 prop->pt_flags &= ~PT_FLAG_COMBINE;
1042 }
1043
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001044 di = dict_find(dict, (char_u *)"priority", -1);
1045 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001046 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001047
1048 di = dict_find(dict, (char_u *)"start_incl", -1);
1049 if (di != NULL)
1050 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001051 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001052 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1053 else
1054 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1055 }
1056
1057 di = dict_find(dict, (char_u *)"end_incl", -1);
1058 if (di != NULL)
1059 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001060 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001061 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1062 else
1063 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1064 }
1065 }
1066}
1067
1068/*
1069 * prop_type_add({name}, {props})
1070 */
1071 void
1072f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1073{
1074 prop_type_set(argvars, TRUE);
1075}
1076
1077/*
1078 * prop_type_change({name}, {props})
1079 */
1080 void
1081f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1082{
1083 prop_type_set(argvars, FALSE);
1084}
1085
1086/*
1087 * prop_type_delete({name} [, {bufnr}])
1088 */
1089 void
1090f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1091{
1092 char_u *name;
1093 buf_T *buf = NULL;
1094 hashitem_T *hi;
1095
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001096 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001097 if (*name == NUL)
1098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001099 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001100 return;
1101 }
1102
1103 if (argvars[1].v_type != VAR_UNKNOWN)
1104 {
1105 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1106 return;
1107 }
1108
1109 hi = find_prop_hi(name, buf);
1110 if (hi != NULL)
1111 {
1112 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001113 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001114
1115 if (buf == NULL)
1116 ht = global_proptypes;
1117 else
1118 ht = buf->b_proptypes;
1119 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001120 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001121 }
1122}
1123
1124/*
1125 * prop_type_get({name} [, {bufnr}])
1126 */
1127 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001128f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001129{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001130 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001131
1132 if (*name == NUL)
1133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001134 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001135 return;
1136 }
1137 if (rettv_dict_alloc(rettv) == OK)
1138 {
1139 proptype_T *prop = NULL;
1140 buf_T *buf = NULL;
1141
1142 if (argvars[1].v_type != VAR_UNKNOWN)
1143 {
1144 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1145 return;
1146 }
1147
1148 prop = find_prop(name, buf);
1149 if (prop != NULL)
1150 {
1151 dict_T *d = rettv->vval.v_dict;
1152
1153 if (prop->pt_hl_id > 0)
1154 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1155 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001156 dict_add_number(d, "combine",
1157 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001158 dict_add_number(d, "start_incl",
1159 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1160 dict_add_number(d, "end_incl",
1161 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1162 if (buf != NULL)
1163 dict_add_number(d, "bufnr", buf->b_fnum);
1164 }
1165 }
1166}
1167
1168 static void
1169list_types(hashtab_T *ht, list_T *l)
1170{
1171 long todo;
1172 hashitem_T *hi;
1173
1174 todo = (long)ht->ht_used;
1175 for (hi = ht->ht_array; todo > 0; ++hi)
1176 {
1177 if (!HASHITEM_EMPTY(hi))
1178 {
1179 proptype_T *prop = HI2PT(hi);
1180
1181 list_append_string(l, prop->pt_name, -1);
1182 --todo;
1183 }
1184 }
1185}
1186
1187/*
1188 * prop_type_list([{bufnr}])
1189 */
1190 void
1191f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1192{
1193 buf_T *buf = NULL;
1194
1195 if (rettv_list_alloc(rettv) == OK)
1196 {
1197 if (argvars[0].v_type != VAR_UNKNOWN)
1198 {
1199 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1200 return;
1201 }
1202 if (buf == NULL)
1203 {
1204 if (global_proptypes != NULL)
1205 list_types(global_proptypes, rettv->vval.v_list);
1206 }
1207 else if (buf->b_proptypes != NULL)
1208 list_types(buf->b_proptypes, rettv->vval.v_list);
1209 }
1210}
1211
1212/*
1213 * Free all property types in "ht".
1214 */
1215 static void
1216clear_ht_prop_types(hashtab_T *ht)
1217{
1218 long todo;
1219 hashitem_T *hi;
1220
1221 if (ht == NULL)
1222 return;
1223
1224 todo = (long)ht->ht_used;
1225 for (hi = ht->ht_array; todo > 0; ++hi)
1226 {
1227 if (!HASHITEM_EMPTY(hi))
1228 {
1229 proptype_T *prop = HI2PT(hi);
1230
1231 vim_free(prop);
1232 --todo;
1233 }
1234 }
1235
1236 hash_clear(ht);
1237 vim_free(ht);
1238}
1239
1240#if defined(EXITFREE) || defined(PROTO)
1241/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001242 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001243 */
1244 void
1245clear_global_prop_types(void)
1246{
1247 clear_ht_prop_types(global_proptypes);
1248 global_proptypes = NULL;
1249}
1250#endif
1251
1252/*
1253 * Free all property types for "buf".
1254 */
1255 void
1256clear_buf_prop_types(buf_T *buf)
1257{
1258 clear_ht_prop_types(buf->b_proptypes);
1259 buf->b_proptypes = NULL;
1260}
1261
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001262// Struct used to return two values from adjust_prop().
1263typedef struct
1264{
1265 int dirty; // if the property was changed
1266 int can_drop; // whether after this change, the prop may be removed
1267} adjustres_T;
1268
1269/*
1270 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1271 *
1272 * Note that "col" is zero-based, while tp_col is one-based.
1273 * Only for the current buffer.
1274 * "flags" can have:
1275 * APC_SUBSTITUTE: Text is replaced, not inserted.
1276 */
1277 static adjustres_T
1278adjust_prop(
1279 textprop_T *prop,
1280 colnr_T col,
1281 int added,
1282 int flags)
1283{
1284 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1285 int start_incl = (pt != NULL
1286 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1287 || (flags & APC_SUBSTITUTE);
1288 int end_incl = (pt != NULL
1289 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1290 // Do not drop zero-width props if they later can increase in
1291 // size.
1292 int droppable = !(start_incl || end_incl);
1293 adjustres_T res = {TRUE, FALSE};
1294
1295 if (added > 0)
1296 {
1297 if (col + 1 <= prop->tp_col
1298 - (start_incl || (prop->tp_len == 0 && end_incl)))
1299 // Change is entirely before the text property: Only shift
1300 prop->tp_col += added;
1301 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1302 // Insertion was inside text property
1303 prop->tp_len += added;
1304 }
1305 else if (prop->tp_col > col + 1)
1306 {
1307 if (prop->tp_col + added < col + 1)
1308 {
1309 prop->tp_len += (prop->tp_col - 1 - col) + added;
1310 prop->tp_col = col + 1;
1311 if (prop->tp_len <= 0)
1312 {
1313 prop->tp_len = 0;
1314 res.can_drop = droppable;
1315 }
1316 }
1317 else
1318 prop->tp_col += added;
1319 }
1320 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1321 {
1322 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1323
1324 prop->tp_len += after > 0 ? added + after : added;
1325 res.can_drop = prop->tp_len <= 0 && droppable;
1326 }
1327 else
1328 res.dirty = FALSE;
1329
1330 return res;
1331}
1332
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001333/*
1334 * Adjust the columns of text properties in line "lnum" after position "col" to
1335 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001336 * Note that "col" is zero-based, while tp_col is one-based.
1337 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001338 * "flags" can have:
1339 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1340 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001341 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001342 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001343 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001344 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001345adjust_prop_columns(
1346 linenr_T lnum,
1347 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001348 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001349 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001350{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001351 int proplen;
1352 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001353 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001354 int ri, wi;
1355 size_t textlen;
1356
1357 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001358 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001359
1360 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1361 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001362 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001363 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001364
Bram Moolenaar196d1572019-01-02 23:47:18 +01001365 wi = 0; // write index
1366 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001367 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001368 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001369 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001370
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001371 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1372 res = adjust_prop(&prop, col, bytes_added, flags);
1373 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001374 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001375 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001376 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1377 && u_savesub(lnum) == FAIL)
1378 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001379 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001380
1381 // u_savesub() may have updated curbuf->b_ml, fetch it again
1382 if (curbuf->b_ml.ml_line_lnum != lnum)
1383 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001384 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001385 if (res.can_drop)
1386 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001387 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001388 ++wi;
1389 }
1390 if (dirty)
1391 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001392 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1393
1394 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1395 curbuf->b_ml.ml_line_ptr =
1396 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001397 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001398 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001399 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001400 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001401}
1402
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001403/*
1404 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001405 * "lnum_props" is the line that has the properties from before the split.
1406 * "lnum_top" is the top line.
1407 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001408 * "deleted" is the number of bytes deleted.
1409 */
1410 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001411adjust_props_for_split(
1412 linenr_T lnum_props,
1413 linenr_T lnum_top,
1414 int kept,
1415 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001416{
1417 char_u *props;
1418 int count;
1419 garray_T prevprop;
1420 garray_T nextprop;
1421 int i;
1422 int skipped = kept + deleted;
1423
1424 if (!curbuf->b_has_textprop)
1425 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001426
1427 // Get the text properties from "lnum_props".
1428 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001429 ga_init2(&prevprop, sizeof(textprop_T), 10);
1430 ga_init2(&nextprop, sizeof(textprop_T), 10);
1431
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001432 // Keep the relevant ones in the first line, reducing the length if needed.
1433 // Copy the ones that include the split to the second line.
1434 // Move the ones after the split to the second line.
1435 for (i = 0; i < count; ++i)
1436 {
1437 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001438 proptype_T *pt;
1439 int start_incl, end_incl;
1440 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001441
1442 // copy the prop to an aligned structure
1443 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1444
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001445 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1446 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1447 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1448 cont_prev = prop.tp_col + !start_incl <= kept;
1449 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1450
1451 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001452 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001453 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1454
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001455 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001456 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001457 if (p->tp_col + p->tp_len >= kept)
1458 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001459 if (cont_next)
1460 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001461 }
1462
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001463 // Only add the property to the next line if the length is bigger than
1464 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001465 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001466 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001467 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001468 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001469 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001470 if (p->tp_col > skipped)
1471 p->tp_col -= skipped - 1;
1472 else
1473 {
1474 p->tp_len -= skipped - p->tp_col;
1475 p->tp_col = 1;
1476 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001477 if (cont_prev)
1478 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001479 }
1480 }
1481
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001482 set_text_props(lnum_top, prevprop.ga_data,
1483 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001484 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001485 set_text_props(lnum_top + 1, nextprop.ga_data,
1486 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001487 ga_clear(&nextprop);
1488}
1489
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001490/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001491 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001492 */
1493 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001494prepend_joined_props(
1495 char_u *new_props,
1496 int propcount,
1497 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001498 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001499 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001500 long col,
1501 int removed)
1502{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001503 char_u *props;
1504 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1505 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001506
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001507 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001508 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001509 textprop_T prop;
1510 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001511
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001512 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1513 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1514
1515 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001516 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001517
1518 if (add_all || end)
1519 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1520 &prop, sizeof(prop));
1521 else
1522 {
1523 int j;
1524 int found = FALSE;
1525
1526 // Search for continuing prop.
1527 for (j = *props_remaining; j < propcount; ++j)
1528 {
1529 textprop_T op;
1530
1531 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1532 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1533 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001534 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001535 found = TRUE;
1536 op.tp_len += op.tp_col - prop.tp_col;
1537 op.tp_col = prop.tp_col;
1538 // Start/end is taken care of when deleting joined lines
1539 op.tp_flags = prop.tp_flags;
1540 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1541 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001542 }
1543 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001544 if (!found)
1545 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001546 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001547 }
1548}
1549
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001550#endif // FEAT_PROP_POPUP