blob: 26c7f52084169d1f2297caf7445242a2a6d452e3 [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 *
13 * TODO:
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010014 * - Adjust text property column and length when text is inserted/deleted.
Bram Moolenaar4164bb22019-01-04 23:09:49 +010015 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010016 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020017 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010018 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaar32aa1022019-11-02 22:54:41 +010019 * - Add an array for global_proptypes, to quickly lookup a prop type by ID
20 * - Add an array for b_proptypes, to quickly lookup a prop type by ID
Bram Moolenaarb56ac042018-12-28 23:22:40 +010021 * - Checking the text length to detect text properties is slow. Use a flag in
22 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010023 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
24 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010025 * - Perhaps have a window-local option to disable highlighting from text
26 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010027 */
28
29#include "vim.h"
30
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010031#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33/*
34 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
35 * This avoids adding a pointer to the hashtable item.
36 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
37 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
38 * HI2PT() converts a hashitem pointer to a proptype pointer.
39 */
40#define PT2HIKEY(p) ((p)->pt_name)
41#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
42#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
43
44// The global text property types.
45static hashtab_T *global_proptypes = NULL;
46
47// The last used text property type ID.
48static int proptype_id = 0;
49
50static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
51static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld");
Bram Moolenaare3d31b02018-12-24 23:07:04 +010052static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010053
54/*
55 * Find a property type by name, return the hashitem.
56 * Returns NULL if the item can't be found.
57 */
58 static hashitem_T *
59find_prop_hi(char_u *name, buf_T *buf)
60{
61 hashtab_T *ht;
62 hashitem_T *hi;
63
64 if (*name == NUL)
65 return NULL;
66 if (buf == NULL)
67 ht = global_proptypes;
68 else
69 ht = buf->b_proptypes;
70
71 if (ht == NULL)
72 return NULL;
73 hi = hash_find(ht, name);
74 if (HASHITEM_EMPTY(hi))
75 return NULL;
76 return hi;
77}
78
79/*
80 * Like find_prop_hi() but return the property type.
81 */
82 static proptype_T *
83find_prop(char_u *name, buf_T *buf)
84{
85 hashitem_T *hi = find_prop_hi(name, buf);
86
87 if (hi == NULL)
88 return NULL;
89 return HI2PT(hi);
90}
91
92/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020093 * Get the prop type ID of "name".
94 * When not found return zero.
95 */
96 int
97find_prop_type_id(char_u *name, buf_T *buf)
98{
99 proptype_T *pt = find_prop(name, buf);
100
101 if (pt == NULL)
102 return 0;
103 return pt->pt_id;
104}
105
106/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100107 * Lookup a property type by name. First in "buf" and when not found in the
108 * global types.
109 * When not found gives an error message and returns NULL.
110 */
111 static proptype_T *
112lookup_prop_type(char_u *name, buf_T *buf)
113{
114 proptype_T *type = find_prop(name, buf);
115
116 if (type == NULL)
117 type = find_prop(name, NULL);
118 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100119 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return type;
121}
122
123/*
124 * Get an optional "bufnr" item from the dict in "arg".
125 * When the argument is not used or "bufnr" is not present then "buf" is
126 * unchanged.
127 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100128 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 */
130 static int
131get_bufnr_from_arg(typval_T *arg, buf_T **buf)
132{
133 dictitem_T *di;
134
135 if (arg->v_type != VAR_DICT)
136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100137 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100138 return FAIL;
139 }
140 if (arg->vval.v_dict == NULL)
141 return OK; // NULL dict is like an empty dict
142 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
Martin Tournoije2390c72021-07-28 13:30:16 +0200143 if (di != NULL && (di->di_tv.v_type != VAR_NUMBER
144 || di->di_tv.vval.v_number != 0))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100145 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200146 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100147 if (*buf == NULL)
148 return FAIL;
149 }
150 return OK;
151}
152
153/*
154 * prop_add({lnum}, {col}, {props})
155 */
156 void
157f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
158{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100159 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100160 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100161
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200162 if (in_vim9script()
163 && (check_for_number_arg(argvars, 0) == FAIL
164 || check_for_number_arg(argvars, 1) == FAIL
165 || check_for_dict_arg(argvars, 2) == FAIL))
166 return;
167
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100168 start_lnum = tv_get_number(&argvars[0]);
169 start_col = tv_get_number(&argvars[1]);
170 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100172 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100173 return;
174 }
175 if (argvars[2].v_type != VAR_DICT)
176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100177 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100178 return;
179 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200180
181 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
182 curbuf, &argvars[2]);
183}
184
185/*
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200186 * Attach a text property 'type_name' to the text starting
187 * at [start_lnum, start_col] and ending at [end_lnum, end_col] in
188 * the buffer 'buf' and assign identifier 'id'.
189 */
190 static int
191prop_add_one(
192 buf_T *buf,
193 char_u *type_name,
194 int id,
195 linenr_T start_lnum,
196 linenr_T end_lnum,
197 colnr_T start_col,
198 colnr_T end_col)
199{
200 proptype_T *type;
201 linenr_T lnum;
202 int proplen;
203 char_u *props = NULL;
204 char_u *newprops;
205 size_t textlen;
206 char_u *newtext;
207 int i;
208 textprop_T tmp_prop;
209
210 type = lookup_prop_type(type_name, buf);
211 if (type == NULL)
212 return FAIL;
213
214 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
215 {
216 semsg(_(e_invalid_lnum), (long)start_lnum);
217 return FAIL;
218 }
219 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
220 {
221 semsg(_(e_invalid_lnum), (long)end_lnum);
222 return FAIL;
223 }
224
225 if (buf->b_ml.ml_mfp == NULL)
226 {
227 emsg(_("E275: Cannot add text property to unloaded buffer"));
228 return FAIL;
229 }
230
231 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
232 {
233 colnr_T col; // start column
234 long length; // in bytes
235
236 // Fetch the line to get the ml_line_len field updated.
237 proplen = get_text_props(buf, lnum, &props, TRUE);
238 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
239
240 if (lnum == start_lnum)
241 col = start_col;
242 else
243 col = 1;
244 if (col - 1 > (colnr_T)textlen)
245 {
246 semsg(_(e_invalid_col), (long)start_col);
247 return FAIL;
248 }
249
250 if (lnum == end_lnum)
251 length = end_col - col;
252 else
253 length = (int)textlen - col + 1;
254 if (length > (long)textlen)
255 length = (int)textlen; // can include the end-of-line
256 if (length < 0)
257 length = 0; // zero-width property
258
259 // Allocate the new line with space for the new property.
260 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
261 if (newtext == NULL)
262 return FAIL;
263 // Copy the text, including terminating NUL.
264 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
265
266 // Find the index where to insert the new property.
267 // Since the text properties are not aligned properly when stored with
268 // the text, we need to copy them as bytes before using it as a struct.
269 for (i = 0; i < proplen; ++i)
270 {
271 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
272 sizeof(textprop_T));
273 if (tmp_prop.tp_col >= col)
274 break;
275 }
276 newprops = newtext + textlen;
277 if (i > 0)
278 mch_memmove(newprops, props, sizeof(textprop_T) * i);
279
280 tmp_prop.tp_col = col;
281 tmp_prop.tp_len = length;
282 tmp_prop.tp_id = id;
283 tmp_prop.tp_type = type->pt_id;
284 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
285 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
286 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
287 sizeof(textprop_T));
288
289 if (i < proplen)
290 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
291 props + i * sizeof(textprop_T),
292 sizeof(textprop_T) * (proplen - i));
293
294 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
295 vim_free(buf->b_ml.ml_line_ptr);
296 buf->b_ml.ml_line_ptr = newtext;
297 buf->b_ml.ml_line_len += sizeof(textprop_T);
298 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
299 }
300
301 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
302 return OK;
303}
304
305/*
306 * prop_add_list()
307 * First argument specifies the text property:
308 * {'type': <str>, 'id': <num>, 'bufnr': <num>}
309 * Second argument is a List where each item is a List with the following
310 * entries: [lnum, start_col, end_col]
311 */
312 void
313f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED)
314{
315 dict_T *dict;
316 char_u *type_name;
317 buf_T *buf = curbuf;
318 int id = 0;
319 listitem_T *li;
320 list_T *pos_list;
321 linenr_T start_lnum;
322 colnr_T start_col;
323 linenr_T end_lnum;
324 colnr_T end_col;
325 int error = FALSE;
326
327 if (check_for_dict_arg(argvars, 0) == FAIL
328 || check_for_list_arg(argvars, 1) == FAIL)
329 return;
330
331 if (argvars[1].vval.v_list == NULL)
332 {
333 emsg(_(e_listreq));
334 return;
335 }
336
337 dict = argvars[0].vval.v_dict;
338 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
339 {
340 emsg(_("E965: missing property type name"));
341 return;
342 }
343 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
344
345 if (dict_find(dict, (char_u *)"id", -1) != NULL)
346 id = dict_get_number(dict, (char_u *)"id");
347
348 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
349 return;
350
351 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
352 {
353 if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
354 {
355 emsg(_(e_listreq));
356 return;
357 }
358
359 pos_list = li->li_tv.vval.v_list;
360 start_lnum = list_find_nr(pos_list, 0L, &error);
361 start_col = list_find_nr(pos_list, 1L, &error);
362 end_lnum = list_find_nr(pos_list, 2L, &error);
363 end_col = list_find_nr(pos_list, 3L, &error);
364 if (error || start_lnum <= 0 || start_col <= 0
365 || end_lnum <= 0 || end_col <= 0)
366 {
367 emsg(_(e_invarg));
368 return;
369 }
370 if (prop_add_one(buf, type_name, id, start_lnum, end_lnum,
371 start_col, end_col) == FAIL)
372 return;
373 }
374
375 buf->b_has_textprop = TRUE; // this is never reset
376 redraw_buf_later(buf, VALID);
377}
378
379/*
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200380 * Shared between prop_add() and popup_create().
381 * "dict_arg" is the function argument of a dict containing "bufnr".
382 * it is NULL for popup_create().
383 */
384 void
385prop_add_common(
386 linenr_T start_lnum,
387 colnr_T start_col,
388 dict_T *dict,
389 buf_T *default_buf,
390 typval_T *dict_arg)
391{
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200392 linenr_T end_lnum;
393 colnr_T end_col;
394 char_u *type_name;
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200395 buf_T *buf = default_buf;
396 int id = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100397
398 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100400 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100401 return;
402 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100403 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100404
405 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
406 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100407 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
408 if (end_lnum < start_lnum)
409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100410 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100411 return;
412 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100413 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100414 else
415 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100416
417 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100418 {
419 long length = dict_get_number(dict, (char_u *)"length");
420
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100421 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100424 return;
425 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100426 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100427 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100428 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
429 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100430 end_col = dict_get_number(dict, (char_u *)"end_col");
431 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100433 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100434 return;
435 }
436 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100437 else if (start_lnum == end_lnum)
438 end_col = start_col;
439 else
440 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100441
442 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100443 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100444
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200445 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100446 return;
447
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200448 prop_add_one(buf, type_name, id, start_lnum, end_lnum, start_col, end_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100449
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100450 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200451 redraw_buf_later(buf, VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100452}
453
454/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100455 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100456 * Returns the number of text properties and, when non-zero, a pointer to the
457 * first one in "props" (note that it is not aligned, therefore the char_u
458 * pointer).
459 */
460 int
461get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
462{
463 char_u *text;
464 size_t textlen;
465 size_t proplen;
466
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100467 // Be quick when no text property types have been defined or the buffer,
468 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200469 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100470 return 0;
471
472 // Fetch the line to get the ml_line_len field updated.
473 text = ml_get_buf(buf, lnum, will_change);
474 textlen = STRLEN(text) + 1;
475 proplen = buf->b_ml.ml_line_len - textlen;
476 if (proplen % sizeof(textprop_T) != 0)
477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100478 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100479 return 0;
480 }
481 if (proplen > 0)
482 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100483 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100484}
485
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200486/**
487 * Return the number of text properties on line "lnum" in the current buffer.
488 * When "only_starting" is true only text properties starting in this line will
489 * be considered.
490 */
491 int
492count_props(linenr_T lnum, int only_starting)
493{
494 char_u *props;
495 int proplen = get_text_props(curbuf, lnum, &props, 0);
496 int result = proplen;
497 int i;
498 textprop_T prop;
499
500 if (only_starting)
501 for (i = 0; i < proplen; ++i)
502 {
503 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
504 if (prop.tp_flags & TP_FLAG_CONT_PREV)
505 --result;
506 }
507 return result;
508}
509
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100510/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200511 * Find text property "type_id" in the visible lines of window "wp".
512 * Match "id" when it is > 0.
513 * Returns FAIL when not found.
514 */
515 int
516find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
517 linenr_T *found_lnum)
518{
519 linenr_T lnum;
520 char_u *props;
521 int count;
522 int i;
523
524 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100525 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200526 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
527 {
528 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
529 for (i = 0; i < count; ++i)
530 {
531 mch_memmove(prop, props + i * sizeof(textprop_T),
532 sizeof(textprop_T));
533 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
534 {
535 *found_lnum = lnum;
536 return OK;
537 }
538 }
539 }
540 return FAIL;
541}
542
543/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100544 * Set the text properties for line "lnum" to "props" with length "len".
545 * If "len" is zero text properties are removed, "props" is not used.
546 * Any existing text properties are dropped.
547 * Only works for the current buffer.
548 */
549 static void
550set_text_props(linenr_T lnum, char_u *props, int len)
551{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100552 char_u *text;
553 char_u *newtext;
554 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100555
556 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100557 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100558 newtext = alloc(textlen + len);
559 if (newtext == NULL)
560 return;
561 mch_memmove(newtext, text, textlen);
562 if (len > 0)
563 mch_memmove(newtext + textlen, props, len);
564 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
565 vim_free(curbuf->b_ml.ml_line_ptr);
566 curbuf->b_ml.ml_line_ptr = newtext;
567 curbuf->b_ml.ml_line_len = textlen + len;
568 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
569}
570
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100571 static proptype_T *
572find_type_by_id(hashtab_T *ht, int id)
573{
574 long todo;
575 hashitem_T *hi;
576
577 if (ht == NULL)
578 return NULL;
579
580 // TODO: Make this faster by keeping a list of types sorted on ID and use
581 // a binary search.
582
583 todo = (long)ht->ht_used;
584 for (hi = ht->ht_array; todo > 0; ++hi)
585 {
586 if (!HASHITEM_EMPTY(hi))
587 {
588 proptype_T *prop = HI2PT(hi);
589
590 if (prop->pt_id == id)
591 return prop;
592 --todo;
593 }
594 }
595 return NULL;
596}
597
598/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100599 * Fill 'dict' with text properties in 'prop'.
600 */
601 static void
602prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
603{
604 proptype_T *pt;
Martin Tournoije2390c72021-07-28 13:30:16 +0200605 int buflocal = TRUE;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100606
607 dict_add_number(dict, "col", prop->tp_col);
608 dict_add_number(dict, "length", prop->tp_len);
609 dict_add_number(dict, "id", prop->tp_id);
610 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
611 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
Martin Tournoije2390c72021-07-28 13:30:16 +0200612
613 pt = find_type_by_id(buf->b_proptypes, prop->tp_type);
614 if (pt == NULL)
615 {
616 pt = find_type_by_id(global_proptypes, prop->tp_type);
617 buflocal = FALSE;
618 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100619 if (pt != NULL)
620 dict_add_string(dict, "type", pt->pt_name);
Martin Tournoije2390c72021-07-28 13:30:16 +0200621
622 if (buflocal)
623 dict_add_number(dict, "type_bufnr", buf->b_fnum);
624 else
625 dict_add_number(dict, "type_bufnr", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100626}
627
628/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100629 * Find a property type by ID in "buf" or globally.
630 * Returns NULL if not found.
631 */
632 proptype_T *
633text_prop_type_by_id(buf_T *buf, int id)
634{
635 proptype_T *type;
636
637 type = find_type_by_id(buf->b_proptypes, id);
638 if (type == NULL)
639 type = find_type_by_id(global_proptypes, id);
640 return type;
641}
642
643/*
644 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
645 */
646 void
647f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
648{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200649 linenr_T start;
650 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100651 linenr_T lnum;
652 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100653 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100654
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200655 if (in_vim9script()
656 && (check_for_number_arg(argvars, 0) == FAIL
657 || check_for_opt_number_arg(argvars, 1) == FAIL
658 || (argvars[1].v_type != VAR_UNKNOWN
659 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
660 return;
661
662 start = tv_get_number(&argvars[0]);
663 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100664 if (argvars[1].v_type != VAR_UNKNOWN)
665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100666 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100667 if (argvars[2].v_type != VAR_UNKNOWN)
668 {
669 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
670 return;
671 }
672 }
673 if (start < 1 || end < 1)
674 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200675 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100676 return;
677 }
678
679 for (lnum = start; lnum <= end; ++lnum)
680 {
681 char_u *text;
682 size_t len;
683
684 if (lnum > buf->b_ml.ml_line_count)
685 break;
686 text = ml_get_buf(buf, lnum, FALSE);
687 len = STRLEN(text) + 1;
688 if ((size_t)buf->b_ml.ml_line_len > len)
689 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100690 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100691 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
692 {
693 char_u *newtext = vim_strsave(text);
694
695 // need to allocate the line now
696 if (newtext == NULL)
697 return;
698 buf->b_ml.ml_line_ptr = newtext;
699 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
700 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100701 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100702 }
703 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100704 if (did_clear)
705 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100706}
707
708/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100709 * prop_find({props} [, {direction}])
710 */
711 void
712f_prop_find(typval_T *argvars, typval_T *rettv)
713{
714 pos_T *cursor = &curwin->w_cursor;
715 dict_T *dict;
716 buf_T *buf = curbuf;
717 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200718 int lnum_start;
719 int start_pos_has_prop = 0;
720 int seen_end = 0;
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200721 int id = 0;
722 int id_found = FALSE;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200723 int type_id = -1;
724 int skipstart = 0;
725 int lnum = -1;
726 int col = -1;
727 int dir = 1; // 1 = forward, -1 = backward
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100728 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100729
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200730 if (in_vim9script()
731 && (check_for_dict_arg(argvars, 0) == FAIL
732 || check_for_opt_string_arg(argvars, 1) == FAIL))
733 return;
734
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100735 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
736 {
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200737 emsg(_(e_dictreq));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100738 return;
739 }
740 dict = argvars[0].vval.v_dict;
741
742 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
743 return;
744 if (buf->b_ml.ml_mfp == NULL)
745 return;
746
747 if (argvars[1].v_type != VAR_UNKNOWN)
748 {
749 char_u *dir_s = tv_get_string(&argvars[1]);
750
751 if (*dir_s == 'b')
752 dir = -1;
753 else if (*dir_s != 'f')
754 {
755 emsg(_(e_invarg));
756 return;
757 }
758 }
759
760 di = dict_find(dict, (char_u *)"lnum", -1);
761 if (di != NULL)
762 lnum = tv_get_number(&di->di_tv);
763
764 di = dict_find(dict, (char_u *)"col", -1);
765 if (di != NULL)
766 col = tv_get_number(&di->di_tv);
767
768 if (lnum == -1)
769 {
770 lnum = cursor->lnum;
771 col = cursor->col + 1;
772 }
773 else if (col == -1)
774 col = 1;
775
776 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
777 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200778 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100779 return;
780 }
781
Bram Moolenaareb245562020-09-03 22:33:44 +0200782 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100783
784 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200785 {
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100786 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaare041dde2021-08-01 21:30:12 +0200787 id_found = TRUE;
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200788 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100789 if (dict_find(dict, (char_u *)"type", -1))
790 {
791 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
792 proptype_T *type = lookup_prop_type(name, buf);
793
794 if (type == NULL)
795 return;
796 type_id = type->pt_id;
797 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100798 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200799 if (!id_found && type_id == -1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100800 {
801 emsg(_("E968: Need at least one of 'id' or 'type'"));
802 return;
803 }
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200804 if (both && (!id_found || type_id == -1))
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100805 {
806 emsg(_("E860: Need 'id' and 'type' with 'both'"));
807 return;
808 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100809
810 lnum_start = lnum;
811
812 if (rettv_dict_alloc(rettv) == FAIL)
813 return;
814
815 while (1)
816 {
817 char_u *text = ml_get_buf(buf, lnum, FALSE);
818 size_t textlen = STRLEN(text) + 1;
819 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200820 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100821 int i;
822 textprop_T prop;
823 int prop_start;
824 int prop_end;
825
826 for (i = 0; i < count; ++i)
827 {
828 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
829 sizeof(textprop_T));
830
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100831 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100832 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100833 if (dir < 0)
834 {
835 if (col < prop.tp_col)
836 break;
837 }
838 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
839 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100840 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100841 if (both ? prop.tp_id == id && prop.tp_type == type_id
Bram Moolenaar0d4d9ee2021-08-01 19:28:15 +0200842 : (id_found && prop.tp_id == id)
843 || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100844 {
845 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100846 if (lnum_start == lnum
847 && col >= prop.tp_col
848 && (col <= prop.tp_col + prop.tp_len
849 - (prop.tp_len != 0)))
850 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100851
852 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
853 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
854 if (!prop_start && prop_end && dir > 0)
855 seen_end = 1;
856
857 // Skip lines without the start flag.
858 if (!prop_start)
859 {
860 // Always search backwards for start when search started
861 // on a prop and we're not skipping.
862 if (start_pos_has_prop && !skipstart)
863 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200864 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100865 }
866
867 // If skipstart is true, skip the prop at start pos (even if
868 // continued from another line).
869 if (start_pos_has_prop && skipstart && !seen_end)
870 {
871 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200872 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100873 }
874
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100875 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
876 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
877
878 return;
879 }
880 }
881
882 if (dir > 0)
883 {
884 if (lnum >= buf->b_ml.ml_line_count)
885 break;
886 lnum++;
887 }
888 else
889 {
890 if (lnum <= 1)
891 break;
892 lnum--;
893 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100894 // Adjust col to indicate that we're continuing from prev/next line.
895 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100896 }
897}
898
899/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100900 * prop_list({lnum} [, {bufnr}])
901 */
902 void
903f_prop_list(typval_T *argvars, typval_T *rettv)
904{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200905 linenr_T lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100906 buf_T *buf = curbuf;
907
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200908 if (in_vim9script()
909 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200910 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200911 return;
912
913 lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100914 if (argvars[1].v_type != VAR_UNKNOWN)
915 {
916 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
917 return;
918 }
919 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
920 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200921 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100922 return;
923 }
924
925 if (rettv_list_alloc(rettv) == OK)
926 {
927 char_u *text = ml_get_buf(buf, lnum, FALSE);
928 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100929 int count = (int)((buf->b_ml.ml_line_len - textlen)
930 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100931 int i;
932 textprop_T prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100933
934 for (i = 0; i < count; ++i)
935 {
936 dict_T *d = dict_alloc();
937
938 if (d == NULL)
939 break;
940 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
941 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100942 prop_fill_dict(d, &prop, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100943 list_append_dict(rettv->vval.v_list, d);
944 }
945 }
946}
947
948/*
949 * prop_remove({props} [, {lnum} [, {lnum_end}]])
950 */
951 void
952f_prop_remove(typval_T *argvars, typval_T *rettv)
953{
954 linenr_T start = 1;
955 linenr_T end = 0;
956 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +0200957 linenr_T first_changed = 0;
958 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100959 dict_T *dict;
960 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200961 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100962 int id = -1;
963 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200964 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100965
966 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200967
968 if (in_vim9script()
969 && (check_for_dict_arg(argvars, 0) == FAIL
970 || check_for_opt_number_arg(argvars, 1) == FAIL
971 || (argvars[1].v_type != VAR_UNKNOWN
972 && check_for_opt_number_arg(argvars, 2) == FAIL)))
973 return;
974
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100975 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
976 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100977 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100978 return;
979 }
980
981 if (argvars[1].v_type != VAR_UNKNOWN)
982 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100983 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100984 end = start;
985 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100986 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100987 if (start < 1 || end < 1)
988 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200989 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100990 return;
991 }
992 }
993
994 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200995 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
996 return;
997 if (buf->b_ml.ml_mfp == NULL)
998 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100999
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001000 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001001
1002 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01001003 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001004 if (dict_find(dict, (char_u *)"type", -1))
1005 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01001006 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001007 proptype_T *type = lookup_prop_type(name, buf);
1008
1009 if (type == NULL)
1010 return;
1011 type_id = type->pt_id;
1012 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +02001013 both = dict_get_bool(dict, (char_u *)"both", FALSE);
1014
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001015 if (id == -1 && type_id == -1)
1016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001017 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001018 return;
1019 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001020 if (both && (id == -1 || type_id == -1))
1021 {
1022 emsg(_("E860: Need 'id' and 'type' with 'both'"));
1023 return;
1024 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001025
1026 if (end == 0)
1027 end = buf->b_ml.ml_line_count;
1028 for (lnum = start; lnum <= end; ++lnum)
1029 {
1030 char_u *text;
1031 size_t len;
1032
1033 if (lnum > buf->b_ml.ml_line_count)
1034 break;
1035 text = ml_get_buf(buf, lnum, FALSE);
1036 len = STRLEN(text) + 1;
1037 if ((size_t)buf->b_ml.ml_line_len > len)
1038 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001039 static textprop_T textprop; // static because of alignment
1040 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001041
1042 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
1043 / sizeof(textprop_T); ++idx)
1044 {
1045 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
1046 + idx * sizeof(textprop_T);
1047 size_t taillen;
1048
1049 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +01001050 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
1051 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001052 {
1053 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
1054 {
1055 char_u *newptr = alloc(buf->b_ml.ml_line_len);
1056
1057 // need to allocate the line to be able to change it
1058 if (newptr == NULL)
1059 return;
1060 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
1061 buf->b_ml.ml_line_len);
1062 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +01001063 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
1064
1065 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001066 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001067 }
1068
1069 taillen = buf->b_ml.ml_line_len - len
1070 - (idx + 1) * sizeof(textprop_T);
1071 if (taillen > 0)
1072 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
1073 taillen);
1074 buf->b_ml.ml_line_len -= sizeof(textprop_T);
1075 --idx;
1076
Bram Moolenaar965c0442021-05-17 00:22:06 +02001077 if (first_changed == 0)
1078 first_changed = lnum;
1079 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001080 ++rettv->vval.v_number;
1081 if (!do_all)
1082 break;
1083 }
1084 }
1085 }
1086 }
Bram Moolenaar965c0442021-05-17 00:22:06 +02001087 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001088 {
Bram Moolenaar965c0442021-05-17 00:22:06 +02001089 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
1090 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +02001091 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001092}
1093
1094/*
1095 * Common for f_prop_type_add() and f_prop_type_change().
1096 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001097 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001098prop_type_set(typval_T *argvars, int add)
1099{
1100 char_u *name;
1101 buf_T *buf = NULL;
1102 dict_T *dict;
1103 dictitem_T *di;
1104 proptype_T *prop;
1105
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001106 if (in_vim9script()
1107 && (check_for_string_arg(argvars, 0) == FAIL
1108 || check_for_dict_arg(argvars, 1) == FAIL))
1109 return;
1110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001111 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001112 if (*name == NUL)
1113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001114 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001115 return;
1116 }
1117
1118 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1119 return;
1120 dict = argvars[1].vval.v_dict;
1121
1122 prop = find_prop(name, buf);
1123 if (add)
1124 {
1125 hashtab_T **htp;
1126
1127 if (prop != NULL)
1128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001129 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001130 return;
1131 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001132 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001133 if (prop == NULL)
1134 return;
1135 STRCPY(prop->pt_name, name);
1136 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001137 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001138 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1139 if (*htp == NULL)
1140 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001141 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001142 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001143 {
1144 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001145 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001146 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001147 hash_init(*htp);
1148 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001149 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001150 }
1151 else
1152 {
1153 if (prop == NULL)
1154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001155 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001156 return;
1157 }
1158 }
1159
1160 if (dict != NULL)
1161 {
1162 di = dict_find(dict, (char_u *)"highlight", -1);
1163 if (di != NULL)
1164 {
1165 char_u *highlight;
1166 int hl_id = 0;
1167
Bram Moolenaar8f667172018-12-14 15:38:31 +01001168 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001169 if (highlight != NULL && *highlight != NUL)
1170 hl_id = syn_name2id(highlight);
1171 if (hl_id <= 0)
1172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001173 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001174 highlight == NULL ? (char_u *)"" : highlight);
1175 return;
1176 }
1177 prop->pt_hl_id = hl_id;
1178 }
1179
Bram Moolenaar58187f12019-05-05 16:33:47 +02001180 di = dict_find(dict, (char_u *)"combine", -1);
1181 if (di != NULL)
1182 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001183 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001184 prop->pt_flags |= PT_FLAG_COMBINE;
1185 else
1186 prop->pt_flags &= ~PT_FLAG_COMBINE;
1187 }
1188
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001189 di = dict_find(dict, (char_u *)"priority", -1);
1190 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001191 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001192
1193 di = dict_find(dict, (char_u *)"start_incl", -1);
1194 if (di != NULL)
1195 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001196 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001197 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1198 else
1199 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1200 }
1201
1202 di = dict_find(dict, (char_u *)"end_incl", -1);
1203 if (di != NULL)
1204 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001205 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001206 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1207 else
1208 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1209 }
1210 }
1211}
1212
1213/*
1214 * prop_type_add({name}, {props})
1215 */
1216 void
1217f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1218{
1219 prop_type_set(argvars, TRUE);
1220}
1221
1222/*
1223 * prop_type_change({name}, {props})
1224 */
1225 void
1226f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1227{
1228 prop_type_set(argvars, FALSE);
1229}
1230
1231/*
1232 * prop_type_delete({name} [, {bufnr}])
1233 */
1234 void
1235f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1236{
1237 char_u *name;
1238 buf_T *buf = NULL;
1239 hashitem_T *hi;
1240
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001241 if (in_vim9script()
1242 && (check_for_string_arg(argvars, 0) == FAIL
1243 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1244 return;
1245
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001246 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001247 if (*name == NUL)
1248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001249 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001250 return;
1251 }
1252
1253 if (argvars[1].v_type != VAR_UNKNOWN)
1254 {
1255 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1256 return;
1257 }
1258
1259 hi = find_prop_hi(name, buf);
1260 if (hi != NULL)
1261 {
1262 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001263 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001264
1265 if (buf == NULL)
1266 ht = global_proptypes;
1267 else
1268 ht = buf->b_proptypes;
1269 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001270 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001271 }
1272}
1273
1274/*
Martin Tournoije2390c72021-07-28 13:30:16 +02001275 * prop_type_get({name} [, {props}])
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001276 */
1277 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001278f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001279{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001280 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001281
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001282 if (in_vim9script()
1283 && (check_for_string_arg(argvars, 0) == FAIL
1284 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1285 return;
1286
1287 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001288 if (*name == NUL)
1289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001290 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001291 return;
1292 }
1293 if (rettv_dict_alloc(rettv) == OK)
1294 {
1295 proptype_T *prop = NULL;
1296 buf_T *buf = NULL;
1297
1298 if (argvars[1].v_type != VAR_UNKNOWN)
1299 {
1300 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1301 return;
1302 }
1303
1304 prop = find_prop(name, buf);
1305 if (prop != NULL)
1306 {
1307 dict_T *d = rettv->vval.v_dict;
1308
1309 if (prop->pt_hl_id > 0)
1310 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1311 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001312 dict_add_number(d, "combine",
1313 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001314 dict_add_number(d, "start_incl",
1315 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1316 dict_add_number(d, "end_incl",
1317 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1318 if (buf != NULL)
1319 dict_add_number(d, "bufnr", buf->b_fnum);
1320 }
1321 }
1322}
1323
1324 static void
1325list_types(hashtab_T *ht, list_T *l)
1326{
1327 long todo;
1328 hashitem_T *hi;
1329
1330 todo = (long)ht->ht_used;
1331 for (hi = ht->ht_array; todo > 0; ++hi)
1332 {
1333 if (!HASHITEM_EMPTY(hi))
1334 {
1335 proptype_T *prop = HI2PT(hi);
1336
1337 list_append_string(l, prop->pt_name, -1);
1338 --todo;
1339 }
1340 }
1341}
1342
1343/*
1344 * prop_type_list([{bufnr}])
1345 */
1346 void
1347f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1348{
1349 buf_T *buf = NULL;
1350
1351 if (rettv_list_alloc(rettv) == OK)
1352 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001353 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1354 return;
1355
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001356 if (argvars[0].v_type != VAR_UNKNOWN)
1357 {
1358 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1359 return;
1360 }
1361 if (buf == NULL)
1362 {
1363 if (global_proptypes != NULL)
1364 list_types(global_proptypes, rettv->vval.v_list);
1365 }
1366 else if (buf->b_proptypes != NULL)
1367 list_types(buf->b_proptypes, rettv->vval.v_list);
1368 }
1369}
1370
1371/*
1372 * Free all property types in "ht".
1373 */
1374 static void
1375clear_ht_prop_types(hashtab_T *ht)
1376{
1377 long todo;
1378 hashitem_T *hi;
1379
1380 if (ht == NULL)
1381 return;
1382
1383 todo = (long)ht->ht_used;
1384 for (hi = ht->ht_array; todo > 0; ++hi)
1385 {
1386 if (!HASHITEM_EMPTY(hi))
1387 {
1388 proptype_T *prop = HI2PT(hi);
1389
1390 vim_free(prop);
1391 --todo;
1392 }
1393 }
1394
1395 hash_clear(ht);
1396 vim_free(ht);
1397}
1398
1399#if defined(EXITFREE) || defined(PROTO)
1400/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001401 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001402 */
1403 void
1404clear_global_prop_types(void)
1405{
1406 clear_ht_prop_types(global_proptypes);
1407 global_proptypes = NULL;
1408}
1409#endif
1410
1411/*
1412 * Free all property types for "buf".
1413 */
1414 void
1415clear_buf_prop_types(buf_T *buf)
1416{
1417 clear_ht_prop_types(buf->b_proptypes);
1418 buf->b_proptypes = NULL;
1419}
1420
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001421// Struct used to return two values from adjust_prop().
1422typedef struct
1423{
1424 int dirty; // if the property was changed
1425 int can_drop; // whether after this change, the prop may be removed
1426} adjustres_T;
1427
1428/*
1429 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1430 *
1431 * Note that "col" is zero-based, while tp_col is one-based.
1432 * Only for the current buffer.
1433 * "flags" can have:
1434 * APC_SUBSTITUTE: Text is replaced, not inserted.
1435 */
1436 static adjustres_T
1437adjust_prop(
1438 textprop_T *prop,
1439 colnr_T col,
1440 int added,
1441 int flags)
1442{
1443 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1444 int start_incl = (pt != NULL
1445 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1446 || (flags & APC_SUBSTITUTE);
1447 int end_incl = (pt != NULL
1448 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1449 // Do not drop zero-width props if they later can increase in
1450 // size.
1451 int droppable = !(start_incl || end_incl);
1452 adjustres_T res = {TRUE, FALSE};
1453
1454 if (added > 0)
1455 {
1456 if (col + 1 <= prop->tp_col
1457 - (start_incl || (prop->tp_len == 0 && end_incl)))
1458 // Change is entirely before the text property: Only shift
1459 prop->tp_col += added;
1460 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1461 // Insertion was inside text property
1462 prop->tp_len += added;
1463 }
1464 else if (prop->tp_col > col + 1)
1465 {
1466 if (prop->tp_col + added < col + 1)
1467 {
1468 prop->tp_len += (prop->tp_col - 1 - col) + added;
1469 prop->tp_col = col + 1;
1470 if (prop->tp_len <= 0)
1471 {
1472 prop->tp_len = 0;
1473 res.can_drop = droppable;
1474 }
1475 }
1476 else
1477 prop->tp_col += added;
1478 }
1479 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1480 {
1481 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1482
1483 prop->tp_len += after > 0 ? added + after : added;
1484 res.can_drop = prop->tp_len <= 0 && droppable;
1485 }
1486 else
1487 res.dirty = FALSE;
1488
1489 return res;
1490}
1491
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001492/*
1493 * Adjust the columns of text properties in line "lnum" after position "col" to
1494 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001495 * Note that "col" is zero-based, while tp_col is one-based.
1496 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001497 * "flags" can have:
1498 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1499 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001500 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001501 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001502 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001503 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001504adjust_prop_columns(
1505 linenr_T lnum,
1506 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001507 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001508 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001509{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001510 int proplen;
1511 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001512 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001513 int ri, wi;
1514 size_t textlen;
1515
1516 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001517 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001518
1519 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1520 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001521 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001522 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001523
Bram Moolenaar196d1572019-01-02 23:47:18 +01001524 wi = 0; // write index
1525 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001526 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001527 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001528 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001529
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001530 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1531 res = adjust_prop(&prop, col, bytes_added, flags);
1532 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001533 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001534 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001535 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1536 && u_savesub(lnum) == FAIL)
1537 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001538 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001539
1540 // u_savesub() may have updated curbuf->b_ml, fetch it again
1541 if (curbuf->b_ml.ml_line_lnum != lnum)
1542 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001543 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001544 if (res.can_drop)
1545 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001546 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001547 ++wi;
1548 }
1549 if (dirty)
1550 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001551 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1552
1553 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1554 curbuf->b_ml.ml_line_ptr =
1555 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001556 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001557 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001558 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001559 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001560}
1561
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001562/*
1563 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001564 * "lnum_props" is the line that has the properties from before the split.
1565 * "lnum_top" is the top line.
1566 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001567 * "deleted" is the number of bytes deleted.
1568 */
1569 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001570adjust_props_for_split(
1571 linenr_T lnum_props,
1572 linenr_T lnum_top,
1573 int kept,
1574 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001575{
1576 char_u *props;
1577 int count;
1578 garray_T prevprop;
1579 garray_T nextprop;
1580 int i;
1581 int skipped = kept + deleted;
1582
1583 if (!curbuf->b_has_textprop)
1584 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001585
1586 // Get the text properties from "lnum_props".
1587 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001588 ga_init2(&prevprop, sizeof(textprop_T), 10);
1589 ga_init2(&nextprop, sizeof(textprop_T), 10);
1590
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001591 // Keep the relevant ones in the first line, reducing the length if needed.
1592 // Copy the ones that include the split to the second line.
1593 // Move the ones after the split to the second line.
1594 for (i = 0; i < count; ++i)
1595 {
1596 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001597 proptype_T *pt;
1598 int start_incl, end_incl;
1599 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001600
1601 // copy the prop to an aligned structure
1602 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1603
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001604 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1605 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1606 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1607 cont_prev = prop.tp_col + !start_incl <= kept;
1608 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1609
1610 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001611 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001612 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1613
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001614 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001615 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001616 if (p->tp_col + p->tp_len >= kept)
1617 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001618 if (cont_next)
1619 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001620 }
1621
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001622 // Only add the property to the next line if the length is bigger than
1623 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001624 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001625 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001626 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001627 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001628 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001629 if (p->tp_col > skipped)
1630 p->tp_col -= skipped - 1;
1631 else
1632 {
1633 p->tp_len -= skipped - p->tp_col;
1634 p->tp_col = 1;
1635 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001636 if (cont_prev)
1637 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001638 }
1639 }
1640
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001641 set_text_props(lnum_top, prevprop.ga_data,
1642 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001643 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001644 set_text_props(lnum_top + 1, nextprop.ga_data,
1645 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001646 ga_clear(&nextprop);
1647}
1648
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001649/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001650 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001651 */
1652 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001653prepend_joined_props(
1654 char_u *new_props,
1655 int propcount,
1656 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001657 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001658 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001659 long col,
1660 int removed)
1661{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001662 char_u *props;
1663 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1664 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001665
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001666 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001667 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001668 textprop_T prop;
1669 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001670
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001671 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1672 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1673
1674 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001675 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001676
1677 if (add_all || end)
1678 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1679 &prop, sizeof(prop));
1680 else
1681 {
1682 int j;
1683 int found = FALSE;
1684
1685 // Search for continuing prop.
1686 for (j = *props_remaining; j < propcount; ++j)
1687 {
1688 textprop_T op;
1689
1690 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1691 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1692 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001693 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001694 found = TRUE;
1695 op.tp_len += op.tp_col - prop.tp_col;
1696 op.tp_col = prop.tp_col;
1697 // Start/end is taken care of when deleting joined lines
1698 op.tp_flags = prop.tp_flags;
1699 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1700 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001701 }
1702 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001703 if (!found)
1704 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001705 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001706 }
1707}
1708
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001709#endif // FEAT_PROP_POPUP