blob: e5cc366b621fcb4209333fd1ce02d3054beea7cb [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;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200626 int id = 0;
627 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200628 int type_id = -1;
629 int skipstart = 0;
630 int lnum = -1;
631 int col = -1;
632 int dir = 1; // 1 = forward, -1 = backward
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100633 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100634
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200635 if (in_vim9script()
636 && (check_for_dict_arg(argvars, 0) == FAIL
637 || check_for_opt_string_arg(argvars, 1) == FAIL))
638 return;
639
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100640 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
641 {
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200642 emsg(_(e_dictreq));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100643 return;
644 }
645 dict = argvars[0].vval.v_dict;
646
647 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
648 return;
649 if (buf->b_ml.ml_mfp == NULL)
650 return;
651
652 if (argvars[1].v_type != VAR_UNKNOWN)
653 {
654 char_u *dir_s = tv_get_string(&argvars[1]);
655
656 if (*dir_s == 'b')
657 dir = -1;
658 else if (*dir_s != 'f')
659 {
660 emsg(_(e_invarg));
661 return;
662 }
663 }
664
665 di = dict_find(dict, (char_u *)"lnum", -1);
666 if (di != NULL)
667 lnum = tv_get_number(&di->di_tv);
668
669 di = dict_find(dict, (char_u *)"col", -1);
670 if (di != NULL)
671 col = tv_get_number(&di->di_tv);
672
673 if (lnum == -1)
674 {
675 lnum = cursor->lnum;
676 col = cursor->col + 1;
677 }
678 else if (col == -1)
679 col = 1;
680
681 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
682 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200683 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100684 return;
685 }
686
Bram Moolenaareb245562020-09-03 22:33:44 +0200687 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100688
689 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200690 {
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100691 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200692 id_found = id != 0;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200693 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100694 if (dict_find(dict, (char_u *)"type", -1))
695 {
696 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
697 proptype_T *type = lookup_prop_type(name, buf);
698
699 if (type == NULL)
700 return;
701 type_id = type->pt_id;
702 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100703 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200704 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100705 {
706 emsg(_("E968: Need at least one of 'id' or 'type'"));
707 return;
708 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200709 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100710 {
711 emsg(_("E860: Need 'id' and 'type' with 'both'"));
712 return;
713 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100714
715 lnum_start = lnum;
716
717 if (rettv_dict_alloc(rettv) == FAIL)
718 return;
719
720 while (1)
721 {
722 char_u *text = ml_get_buf(buf, lnum, FALSE);
723 size_t textlen = STRLEN(text) + 1;
724 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200725 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100726 int i;
727 textprop_T prop;
728 int prop_start;
729 int prop_end;
730
731 for (i = 0; i < count; ++i)
732 {
733 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
734 sizeof(textprop_T));
735
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100736 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100737 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100738 if (dir < 0)
739 {
740 if (col < prop.tp_col)
741 break;
742 }
743 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
744 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100745 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100746 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200747 : (id_found && prop.tp_id == id)
748 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100749 {
750 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100751 if (lnum_start == lnum
752 && col >= prop.tp_col
753 && (col <= prop.tp_col + prop.tp_len
754 - (prop.tp_len != 0)))
755 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100756
757 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
758 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
759 if (!prop_start && prop_end && dir > 0)
760 seen_end = 1;
761
762 // Skip lines without the start flag.
763 if (!prop_start)
764 {
765 // Always search backwards for start when search started
766 // on a prop and we're not skipping.
767 if (start_pos_has_prop && !skipstart)
768 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200769 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100770 }
771
772 // If skipstart is true, skip the prop at start pos (even if
773 // continued from another line).
774 if (start_pos_has_prop && skipstart && !seen_end)
775 {
776 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200777 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100778 }
779
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100780 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
781 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
782
783 return;
784 }
785 }
786
787 if (dir > 0)
788 {
789 if (lnum >= buf->b_ml.ml_line_count)
790 break;
791 lnum++;
792 }
793 else
794 {
795 if (lnum <= 1)
796 break;
797 lnum--;
798 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100799 // Adjust col to indicate that we're continuing from prev/next line.
800 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100801 }
802}
803
804/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100805 * prop_list({lnum} [, {bufnr}])
806 */
807 void
808f_prop_list(typval_T *argvars, typval_T *rettv)
809{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200810 linenr_T lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100811 buf_T *buf = curbuf;
812
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200813 if (in_vim9script()
814 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200815 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200816 return;
817
818 lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100819 if (argvars[1].v_type != VAR_UNKNOWN)
820 {
821 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
822 return;
823 }
824 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
825 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200826 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100827 return;
828 }
829
830 if (rettv_list_alloc(rettv) == OK)
831 {
832 char_u *text = ml_get_buf(buf, lnum, FALSE);
833 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100834 int count = (int)((buf->b_ml.ml_line_len - textlen)
835 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100836 int i;
837 textprop_T prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100838
839 for (i = 0; i < count; ++i)
840 {
841 dict_T *d = dict_alloc();
842
843 if (d == NULL)
844 break;
845 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
846 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100847 prop_fill_dict(d, &prop, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100848 list_append_dict(rettv->vval.v_list, d);
849 }
850 }
851}
852
853/*
854 * prop_remove({props} [, {lnum} [, {lnum_end}]])
855 */
856 void
857f_prop_remove(typval_T *argvars, typval_T *rettv)
858{
859 linenr_T start = 1;
860 linenr_T end = 0;
861 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +0200862 linenr_T first_changed = 0;
863 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100864 dict_T *dict;
865 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200866 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100867 int id = -1;
868 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200869 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100870
871 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200872
873 if (in_vim9script()
874 && (check_for_dict_arg(argvars, 0) == FAIL
875 || check_for_opt_number_arg(argvars, 1) == FAIL
876 || (argvars[1].v_type != VAR_UNKNOWN
877 && check_for_opt_number_arg(argvars, 2) == FAIL)))
878 return;
879
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100880 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100882 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100883 return;
884 }
885
886 if (argvars[1].v_type != VAR_UNKNOWN)
887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100888 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100889 end = start;
890 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100891 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100892 if (start < 1 || end < 1)
893 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200894 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100895 return;
896 }
897 }
898
899 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200900 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
901 return;
902 if (buf->b_ml.ml_mfp == NULL)
903 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100904
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200905 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100906
907 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100908 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100909 if (dict_find(dict, (char_u *)"type", -1))
910 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100911 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100912 proptype_T *type = lookup_prop_type(name, buf);
913
914 if (type == NULL)
915 return;
916 type_id = type->pt_id;
917 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200918 both = dict_get_bool(dict, (char_u *)"both", FALSE);
919
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100920 if (id == -1 && type_id == -1)
921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100923 return;
924 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100925 if (both && (id == -1 || type_id == -1))
926 {
927 emsg(_("E860: Need 'id' and 'type' with 'both'"));
928 return;
929 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100930
931 if (end == 0)
932 end = buf->b_ml.ml_line_count;
933 for (lnum = start; lnum <= end; ++lnum)
934 {
935 char_u *text;
936 size_t len;
937
938 if (lnum > buf->b_ml.ml_line_count)
939 break;
940 text = ml_get_buf(buf, lnum, FALSE);
941 len = STRLEN(text) + 1;
942 if ((size_t)buf->b_ml.ml_line_len > len)
943 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200944 static textprop_T textprop; // static because of alignment
945 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100946
947 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
948 / sizeof(textprop_T); ++idx)
949 {
950 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
951 + idx * sizeof(textprop_T);
952 size_t taillen;
953
954 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100955 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
956 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100957 {
958 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
959 {
960 char_u *newptr = alloc(buf->b_ml.ml_line_len);
961
962 // need to allocate the line to be able to change it
963 if (newptr == NULL)
964 return;
965 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
966 buf->b_ml.ml_line_len);
967 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100968 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
969
970 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200971 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100972 }
973
974 taillen = buf->b_ml.ml_line_len - len
975 - (idx + 1) * sizeof(textprop_T);
976 if (taillen > 0)
977 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
978 taillen);
979 buf->b_ml.ml_line_len -= sizeof(textprop_T);
980 --idx;
981
Bram Moolenaar965c0442021-05-17 00:22:06 +0200982 if (first_changed == 0)
983 first_changed = lnum;
984 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100985 ++rettv->vval.v_number;
986 if (!do_all)
987 break;
988 }
989 }
990 }
991 }
Bram Moolenaar965c0442021-05-17 00:22:06 +0200992 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200993 {
Bram Moolenaar965c0442021-05-17 00:22:06 +0200994 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
995 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200996 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100997}
998
999/*
1000 * Common for f_prop_type_add() and f_prop_type_change().
1001 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001002 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001003prop_type_set(typval_T *argvars, int add)
1004{
1005 char_u *name;
1006 buf_T *buf = NULL;
1007 dict_T *dict;
1008 dictitem_T *di;
1009 proptype_T *prop;
1010
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001011 if (in_vim9script()
1012 && (check_for_string_arg(argvars, 0) == FAIL
1013 || check_for_dict_arg(argvars, 1) == FAIL))
1014 return;
1015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001016 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001017 if (*name == NUL)
1018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001020 return;
1021 }
1022
1023 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1024 return;
1025 dict = argvars[1].vval.v_dict;
1026
1027 prop = find_prop(name, buf);
1028 if (add)
1029 {
1030 hashtab_T **htp;
1031
1032 if (prop != NULL)
1033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001034 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001035 return;
1036 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001037 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001038 if (prop == NULL)
1039 return;
1040 STRCPY(prop->pt_name, name);
1041 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001042 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001043 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1044 if (*htp == NULL)
1045 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001046 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001047 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001048 {
1049 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001050 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001051 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001052 hash_init(*htp);
1053 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001054 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001055 }
1056 else
1057 {
1058 if (prop == NULL)
1059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001060 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001061 return;
1062 }
1063 }
1064
1065 if (dict != NULL)
1066 {
1067 di = dict_find(dict, (char_u *)"highlight", -1);
1068 if (di != NULL)
1069 {
1070 char_u *highlight;
1071 int hl_id = 0;
1072
Bram Moolenaar8f667172018-12-14 15:38:31 +01001073 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001074 if (highlight != NULL && *highlight != NUL)
1075 hl_id = syn_name2id(highlight);
1076 if (hl_id <= 0)
1077 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001078 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001079 highlight == NULL ? (char_u *)"" : highlight);
1080 return;
1081 }
1082 prop->pt_hl_id = hl_id;
1083 }
1084
Bram Moolenaar58187f12019-05-05 16:33:47 +02001085 di = dict_find(dict, (char_u *)"combine", -1);
1086 if (di != NULL)
1087 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001088 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001089 prop->pt_flags |= PT_FLAG_COMBINE;
1090 else
1091 prop->pt_flags &= ~PT_FLAG_COMBINE;
1092 }
1093
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001094 di = dict_find(dict, (char_u *)"priority", -1);
1095 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001096 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001097
1098 di = dict_find(dict, (char_u *)"start_incl", -1);
1099 if (di != NULL)
1100 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001101 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001102 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1103 else
1104 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1105 }
1106
1107 di = dict_find(dict, (char_u *)"end_incl", -1);
1108 if (di != NULL)
1109 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001110 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001111 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1112 else
1113 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1114 }
1115 }
1116}
1117
1118/*
1119 * prop_type_add({name}, {props})
1120 */
1121 void
1122f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1123{
1124 prop_type_set(argvars, TRUE);
1125}
1126
1127/*
1128 * prop_type_change({name}, {props})
1129 */
1130 void
1131f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1132{
1133 prop_type_set(argvars, FALSE);
1134}
1135
1136/*
1137 * prop_type_delete({name} [, {bufnr}])
1138 */
1139 void
1140f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1141{
1142 char_u *name;
1143 buf_T *buf = NULL;
1144 hashitem_T *hi;
1145
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001146 if (in_vim9script()
1147 && (check_for_string_arg(argvars, 0) == FAIL
1148 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1149 return;
1150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001151 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001152 if (*name == NUL)
1153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001154 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001155 return;
1156 }
1157
1158 if (argvars[1].v_type != VAR_UNKNOWN)
1159 {
1160 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1161 return;
1162 }
1163
1164 hi = find_prop_hi(name, buf);
1165 if (hi != NULL)
1166 {
1167 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001168 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001169
1170 if (buf == NULL)
1171 ht = global_proptypes;
1172 else
1173 ht = buf->b_proptypes;
1174 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001175 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001176 }
1177}
1178
1179/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001180 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001181 */
1182 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001183f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001184{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001185 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001186
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001187 if (in_vim9script()
1188 && (check_for_string_arg(argvars, 0) == FAIL
1189 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1190 return;
1191
1192 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001193 if (*name == NUL)
1194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001195 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001196 return;
1197 }
1198 if (rettv_dict_alloc(rettv) == OK)
1199 {
1200 proptype_T *prop = NULL;
1201 buf_T *buf = NULL;
1202
1203 if (argvars[1].v_type != VAR_UNKNOWN)
1204 {
1205 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1206 return;
1207 }
1208
1209 prop = find_prop(name, buf);
1210 if (prop != NULL)
1211 {
1212 dict_T *d = rettv->vval.v_dict;
1213
1214 if (prop->pt_hl_id > 0)
1215 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1216 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001217 dict_add_number(d, "combine",
1218 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001219 dict_add_number(d, "start_incl",
1220 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1221 dict_add_number(d, "end_incl",
1222 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1223 if (buf != NULL)
1224 dict_add_number(d, "bufnr", buf->b_fnum);
1225 }
1226 }
1227}
1228
1229 static void
1230list_types(hashtab_T *ht, list_T *l)
1231{
1232 long todo;
1233 hashitem_T *hi;
1234
1235 todo = (long)ht->ht_used;
1236 for (hi = ht->ht_array; todo > 0; ++hi)
1237 {
1238 if (!HASHITEM_EMPTY(hi))
1239 {
1240 proptype_T *prop = HI2PT(hi);
1241
1242 list_append_string(l, prop->pt_name, -1);
1243 --todo;
1244 }
1245 }
1246}
1247
1248/*
1249 * prop_type_list([{bufnr}])
1250 */
1251 void
1252f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1253{
1254 buf_T *buf = NULL;
1255
1256 if (rettv_list_alloc(rettv) == OK)
1257 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001258 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1259 return;
1260
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001261 if (argvars[0].v_type != VAR_UNKNOWN)
1262 {
1263 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1264 return;
1265 }
1266 if (buf == NULL)
1267 {
1268 if (global_proptypes != NULL)
1269 list_types(global_proptypes, rettv->vval.v_list);
1270 }
1271 else if (buf->b_proptypes != NULL)
1272 list_types(buf->b_proptypes, rettv->vval.v_list);
1273 }
1274}
1275
1276/*
1277 * Free all property types in "ht".
1278 */
1279 static void
1280clear_ht_prop_types(hashtab_T *ht)
1281{
1282 long todo;
1283 hashitem_T *hi;
1284
1285 if (ht == NULL)
1286 return;
1287
1288 todo = (long)ht->ht_used;
1289 for (hi = ht->ht_array; todo > 0; ++hi)
1290 {
1291 if (!HASHITEM_EMPTY(hi))
1292 {
1293 proptype_T *prop = HI2PT(hi);
1294
1295 vim_free(prop);
1296 --todo;
1297 }
1298 }
1299
1300 hash_clear(ht);
1301 vim_free(ht);
1302}
1303
1304#if defined(EXITFREE) || defined(PROTO)
1305/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001306 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001307 */
1308 void
1309clear_global_prop_types(void)
1310{
1311 clear_ht_prop_types(global_proptypes);
1312 global_proptypes = NULL;
1313}
1314#endif
1315
1316/*
1317 * Free all property types for "buf".
1318 */
1319 void
1320clear_buf_prop_types(buf_T *buf)
1321{
1322 clear_ht_prop_types(buf->b_proptypes);
1323 buf->b_proptypes = NULL;
1324}
1325
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001326// Struct used to return two values from adjust_prop().
1327typedef struct
1328{
1329 int dirty; // if the property was changed
1330 int can_drop; // whether after this change, the prop may be removed
1331} adjustres_T;
1332
1333/*
1334 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1335 *
1336 * Note that "col" is zero-based, while tp_col is one-based.
1337 * Only for the current buffer.
1338 * "flags" can have:
1339 * APC_SUBSTITUTE: Text is replaced, not inserted.
1340 */
1341 static adjustres_T
1342adjust_prop(
1343 textprop_T *prop,
1344 colnr_T col,
1345 int added,
1346 int flags)
1347{
1348 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1349 int start_incl = (pt != NULL
1350 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1351 || (flags & APC_SUBSTITUTE);
1352 int end_incl = (pt != NULL
1353 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1354 // Do not drop zero-width props if they later can increase in
1355 // size.
1356 int droppable = !(start_incl || end_incl);
1357 adjustres_T res = {TRUE, FALSE};
1358
1359 if (added > 0)
1360 {
1361 if (col + 1 <= prop->tp_col
1362 - (start_incl || (prop->tp_len == 0 && end_incl)))
1363 // Change is entirely before the text property: Only shift
1364 prop->tp_col += added;
1365 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1366 // Insertion was inside text property
1367 prop->tp_len += added;
1368 }
1369 else if (prop->tp_col > col + 1)
1370 {
1371 if (prop->tp_col + added < col + 1)
1372 {
1373 prop->tp_len += (prop->tp_col - 1 - col) + added;
1374 prop->tp_col = col + 1;
1375 if (prop->tp_len <= 0)
1376 {
1377 prop->tp_len = 0;
1378 res.can_drop = droppable;
1379 }
1380 }
1381 else
1382 prop->tp_col += added;
1383 }
1384 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1385 {
1386 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1387
1388 prop->tp_len += after > 0 ? added + after : added;
1389 res.can_drop = prop->tp_len <= 0 && droppable;
1390 }
1391 else
1392 res.dirty = FALSE;
1393
1394 return res;
1395}
1396
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001397/*
1398 * Adjust the columns of text properties in line "lnum" after position "col" to
1399 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001400 * Note that "col" is zero-based, while tp_col is one-based.
1401 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001402 * "flags" can have:
1403 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1404 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001405 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001406 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001407 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001408 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001409adjust_prop_columns(
1410 linenr_T lnum,
1411 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001412 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001413 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001414{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001415 int proplen;
1416 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001417 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001418 int ri, wi;
1419 size_t textlen;
1420
1421 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001422 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001423
1424 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1425 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001426 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001427 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001428
Bram Moolenaar196d1572019-01-02 23:47:18 +01001429 wi = 0; // write index
1430 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001431 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001432 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001433 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001434
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001435 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1436 res = adjust_prop(&prop, col, bytes_added, flags);
1437 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001438 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001439 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001440 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1441 && u_savesub(lnum) == FAIL)
1442 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001443 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001444
1445 // u_savesub() may have updated curbuf->b_ml, fetch it again
1446 if (curbuf->b_ml.ml_line_lnum != lnum)
1447 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001448 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001449 if (res.can_drop)
1450 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001451 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001452 ++wi;
1453 }
1454 if (dirty)
1455 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001456 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1457
1458 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1459 curbuf->b_ml.ml_line_ptr =
1460 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001461 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001462 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001463 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001464 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001465}
1466
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001467/*
1468 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001469 * "lnum_props" is the line that has the properties from before the split.
1470 * "lnum_top" is the top line.
1471 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001472 * "deleted" is the number of bytes deleted.
1473 */
1474 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001475adjust_props_for_split(
1476 linenr_T lnum_props,
1477 linenr_T lnum_top,
1478 int kept,
1479 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001480{
1481 char_u *props;
1482 int count;
1483 garray_T prevprop;
1484 garray_T nextprop;
1485 int i;
1486 int skipped = kept + deleted;
1487
1488 if (!curbuf->b_has_textprop)
1489 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001490
1491 // Get the text properties from "lnum_props".
1492 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001493 ga_init2(&prevprop, sizeof(textprop_T), 10);
1494 ga_init2(&nextprop, sizeof(textprop_T), 10);
1495
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001496 // Keep the relevant ones in the first line, reducing the length if needed.
1497 // Copy the ones that include the split to the second line.
1498 // Move the ones after the split to the second line.
1499 for (i = 0; i < count; ++i)
1500 {
1501 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001502 proptype_T *pt;
1503 int start_incl, end_incl;
1504 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001505
1506 // copy the prop to an aligned structure
1507 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1508
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001509 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1510 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1511 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1512 cont_prev = prop.tp_col + !start_incl <= kept;
1513 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1514
1515 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001516 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001517 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1518
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001519 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001520 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001521 if (p->tp_col + p->tp_len >= kept)
1522 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001523 if (cont_next)
1524 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001525 }
1526
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001527 // Only add the property to the next line if the length is bigger than
1528 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001529 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001530 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001531 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001532 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001533 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001534 if (p->tp_col > skipped)
1535 p->tp_col -= skipped - 1;
1536 else
1537 {
1538 p->tp_len -= skipped - p->tp_col;
1539 p->tp_col = 1;
1540 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001541 if (cont_prev)
1542 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001543 }
1544 }
1545
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001546 set_text_props(lnum_top, prevprop.ga_data,
1547 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001548 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001549 set_text_props(lnum_top + 1, nextprop.ga_data,
1550 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001551 ga_clear(&nextprop);
1552}
1553
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001554/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001555 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001556 */
1557 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001558prepend_joined_props(
1559 char_u *new_props,
1560 int propcount,
1561 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001562 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001563 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001564 long col,
1565 int removed)
1566{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001567 char_u *props;
1568 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1569 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001570
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001571 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001572 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001573 textprop_T prop;
1574 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001575
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001576 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1577 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1578
1579 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001580 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001581
1582 if (add_all || end)
1583 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1584 &prop, sizeof(prop));
1585 else
1586 {
1587 int j;
1588 int found = FALSE;
1589
1590 // Search for continuing prop.
1591 for (j = *props_remaining; j < propcount; ++j)
1592 {
1593 textprop_T op;
1594
1595 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1596 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1597 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001598 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001599 found = TRUE;
1600 op.tp_len += op.tp_col - prop.tp_col;
1601 op.tp_col = prop.tp_col;
1602 // Start/end is taken care of when deleting joined lines
1603 op.tp_flags = prop.tp_flags;
1604 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1605 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001606 }
1607 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001608 if (!found)
1609 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001610 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001611 }
1612}
1613
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001614#endif // FEAT_PROP_POPUP