blob: fdbb14df874d8fc043d5669826565a9b3bd811ba [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 *
13 * TODO:
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010014 * - Adjust text property column and length when text is inserted/deleted.
Bram Moolenaar4164bb22019-01-04 23:09:49 +010015 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010016 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020017 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010018 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaar32aa1022019-11-02 22:54:41 +010019 * - Add an array for global_proptypes, to quickly lookup a prop type by ID
20 * - Add an array for b_proptypes, to quickly lookup a prop type by ID
Bram Moolenaarb56ac042018-12-28 23:22:40 +010021 * - Checking the text length to detect text properties is slow. Use a flag in
22 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010023 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
24 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010025 * - Perhaps have a window-local option to disable highlighting from text
26 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010027 */
28
29#include "vim.h"
30
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010031#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33/*
34 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
35 * This avoids adding a pointer to the hashtable item.
36 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
37 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
38 * HI2PT() converts a hashitem pointer to a proptype pointer.
39 */
40#define PT2HIKEY(p) ((p)->pt_name)
41#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
42#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
43
44// The global text property types.
45static hashtab_T *global_proptypes = NULL;
46
47// The last used text property type ID.
48static int proptype_id = 0;
49
50static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
51static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld");
Bram Moolenaare3d31b02018-12-24 23:07:04 +010052static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010053
54/*
55 * Find a property type by name, return the hashitem.
56 * Returns NULL if the item can't be found.
57 */
58 static hashitem_T *
59find_prop_hi(char_u *name, buf_T *buf)
60{
61 hashtab_T *ht;
62 hashitem_T *hi;
63
64 if (*name == NUL)
65 return NULL;
66 if (buf == NULL)
67 ht = global_proptypes;
68 else
69 ht = buf->b_proptypes;
70
71 if (ht == NULL)
72 return NULL;
73 hi = hash_find(ht, name);
74 if (HASHITEM_EMPTY(hi))
75 return NULL;
76 return hi;
77}
78
79/*
80 * Like find_prop_hi() but return the property type.
81 */
82 static proptype_T *
83find_prop(char_u *name, buf_T *buf)
84{
85 hashitem_T *hi = find_prop_hi(name, buf);
86
87 if (hi == NULL)
88 return NULL;
89 return HI2PT(hi);
90}
91
92/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020093 * Get the prop type ID of "name".
94 * When not found return zero.
95 */
96 int
97find_prop_type_id(char_u *name, buf_T *buf)
98{
99 proptype_T *pt = find_prop(name, buf);
100
101 if (pt == NULL)
102 return 0;
103 return pt->pt_id;
104}
105
106/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100107 * Lookup a property type by name. First in "buf" and when not found in the
108 * global types.
109 * When not found gives an error message and returns NULL.
110 */
111 static proptype_T *
112lookup_prop_type(char_u *name, buf_T *buf)
113{
114 proptype_T *type = find_prop(name, buf);
115
116 if (type == NULL)
117 type = find_prop(name, NULL);
118 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100119 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return type;
121}
122
123/*
124 * Get an optional "bufnr" item from the dict in "arg".
125 * When the argument is not used or "bufnr" is not present then "buf" is
126 * unchanged.
127 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100128 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 */
130 static int
131get_bufnr_from_arg(typval_T *arg, buf_T **buf)
132{
133 dictitem_T *di;
134
135 if (arg->v_type != VAR_DICT)
136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100137 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100138 return FAIL;
139 }
140 if (arg->vval.v_dict == NULL)
141 return OK; // NULL dict is like an empty dict
142 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
143 if (di != NULL)
144 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200145 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100146 if (*buf == NULL)
147 return FAIL;
148 }
149 return OK;
150}
151
152/*
153 * prop_add({lnum}, {col}, {props})
154 */
155 void
156f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
157{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100158 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100159 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100160
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200161 if (in_vim9script()
162 && (check_for_number_arg(argvars, 0) == FAIL
163 || check_for_number_arg(argvars, 1) == FAIL
164 || check_for_dict_arg(argvars, 2) == FAIL))
165 return;
166
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100167 start_lnum = tv_get_number(&argvars[0]);
168 start_col = tv_get_number(&argvars[1]);
169 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100171 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100172 return;
173 }
174 if (argvars[2].v_type != VAR_DICT)
175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100176 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100177 return;
178 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200179
180 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
181 curbuf, &argvars[2]);
182}
183
184/*
185 * Shared between prop_add() and popup_create().
186 * "dict_arg" is the function argument of a dict containing "bufnr".
187 * it is NULL for popup_create().
188 */
189 void
190prop_add_common(
191 linenr_T start_lnum,
192 colnr_T start_col,
193 dict_T *dict,
194 buf_T *default_buf,
195 typval_T *dict_arg)
196{
197 linenr_T lnum;
198 linenr_T end_lnum;
199 colnr_T end_col;
200 char_u *type_name;
201 proptype_T *type;
202 buf_T *buf = default_buf;
203 int id = 0;
204 char_u *newtext;
205 int proplen;
206 size_t textlen;
207 char_u *props = NULL;
208 char_u *newprops;
209 textprop_T tmp_prop;
210 int i;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100211
212 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
213 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100214 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100215 return;
216 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100217 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100218
219 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
220 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100221 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
222 if (end_lnum < start_lnum)
223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100224 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100225 return;
226 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100227 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100228 else
229 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100230
231 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100232 {
233 long length = dict_get_number(dict, (char_u *)"length");
234
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100235 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100237 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100238 return;
239 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100240 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100241 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100242 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
243 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100244 end_col = dict_get_number(dict, (char_u *)"end_col");
245 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100247 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100248 return;
249 }
250 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100251 else if (start_lnum == end_lnum)
252 end_col = start_col;
253 else
254 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100255
256 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100257 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100258
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200259 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100260 return;
261
262 type = lookup_prop_type(type_name, buf);
263 if (type == NULL)
264 return;
265
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100266 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100268 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100269 return;
270 }
271 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100273 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100274 return;
275 }
276
Bram Moolenaard79eef22019-05-24 20:41:55 +0200277 if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaar45311b52019-08-13 22:27:32 +0200278 {
279 emsg(_("E275: Cannot add text property to unloaded buffer"));
280 return;
281 }
Bram Moolenaard79eef22019-05-24 20:41:55 +0200282
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100283 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100284 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100285 colnr_T col; // start column
286 long length; // in bytes
287
288 // Fetch the line to get the ml_line_len field updated.
289 proplen = get_text_props(buf, lnum, &props, TRUE);
290 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
291
292 if (lnum == start_lnum)
293 col = start_col;
294 else
295 col = 1;
296 if (col - 1 > (colnr_T)textlen)
297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100298 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100299 return;
300 }
301
302 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100303 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100304 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100305 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100306 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100307 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100308 if (length < 0)
309 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100310
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100311 // Allocate the new line with space for the new property.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100312 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
313 if (newtext == NULL)
314 return;
315 // Copy the text, including terminating NUL.
316 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
317
318 // Find the index where to insert the new property.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200319 // Since the text properties are not aligned properly when stored with
320 // the text, we need to copy them as bytes before using it as a struct.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100321 for (i = 0; i < proplen; ++i)
322 {
323 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200324 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100325 if (tmp_prop.tp_col >= col)
326 break;
327 }
328 newprops = newtext + textlen;
329 if (i > 0)
330 mch_memmove(newprops, props, sizeof(textprop_T) * i);
331
332 tmp_prop.tp_col = col;
333 tmp_prop.tp_len = length;
334 tmp_prop.tp_id = id;
335 tmp_prop.tp_type = type->pt_id;
336 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
337 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
338 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200339 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100340
341 if (i < proplen)
342 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
343 props + i * sizeof(textprop_T),
344 sizeof(textprop_T) * (proplen - i));
345
346 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
347 vim_free(buf->b_ml.ml_line_ptr);
348 buf->b_ml.ml_line_ptr = newtext;
349 buf->b_ml.ml_line_len += sizeof(textprop_T);
350 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100351 }
352
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100353 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200354 changed_lines_buf(buf, start_lnum, end_lnum + 1, 0);
355 redraw_buf_later(buf, VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100356}
357
358/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100359 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100360 * Returns the number of text properties and, when non-zero, a pointer to the
361 * first one in "props" (note that it is not aligned, therefore the char_u
362 * pointer).
363 */
364 int
365get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
366{
367 char_u *text;
368 size_t textlen;
369 size_t proplen;
370
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100371 // Be quick when no text property types have been defined or the buffer,
372 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200373 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100374 return 0;
375
376 // Fetch the line to get the ml_line_len field updated.
377 text = ml_get_buf(buf, lnum, will_change);
378 textlen = STRLEN(text) + 1;
379 proplen = buf->b_ml.ml_line_len - textlen;
380 if (proplen % sizeof(textprop_T) != 0)
381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100382 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100383 return 0;
384 }
385 if (proplen > 0)
386 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100387 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100388}
389
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200390/**
391 * Return the number of text properties on line "lnum" in the current buffer.
392 * When "only_starting" is true only text properties starting in this line will
393 * be considered.
394 */
395 int
396count_props(linenr_T lnum, int only_starting)
397{
398 char_u *props;
399 int proplen = get_text_props(curbuf, lnum, &props, 0);
400 int result = proplen;
401 int i;
402 textprop_T prop;
403
404 if (only_starting)
405 for (i = 0; i < proplen; ++i)
406 {
407 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
408 if (prop.tp_flags & TP_FLAG_CONT_PREV)
409 --result;
410 }
411 return result;
412}
413
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100414/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200415 * Find text property "type_id" in the visible lines of window "wp".
416 * Match "id" when it is > 0.
417 * Returns FAIL when not found.
418 */
419 int
420find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
421 linenr_T *found_lnum)
422{
423 linenr_T lnum;
424 char_u *props;
425 int count;
426 int i;
427
428 // w_botline may not have been updated yet.
Bram Moolenaar23999d72020-12-23 14:36:00 +0100429 validate_botline_win(wp);
Bram Moolenaar12034e22019-08-25 22:25:02 +0200430 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
431 {
432 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
433 for (i = 0; i < count; ++i)
434 {
435 mch_memmove(prop, props + i * sizeof(textprop_T),
436 sizeof(textprop_T));
437 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
438 {
439 *found_lnum = lnum;
440 return OK;
441 }
442 }
443 }
444 return FAIL;
445}
446
447/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100448 * Set the text properties for line "lnum" to "props" with length "len".
449 * If "len" is zero text properties are removed, "props" is not used.
450 * Any existing text properties are dropped.
451 * Only works for the current buffer.
452 */
453 static void
454set_text_props(linenr_T lnum, char_u *props, int len)
455{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100456 char_u *text;
457 char_u *newtext;
458 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100459
460 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100461 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100462 newtext = alloc(textlen + len);
463 if (newtext == NULL)
464 return;
465 mch_memmove(newtext, text, textlen);
466 if (len > 0)
467 mch_memmove(newtext + textlen, props, len);
468 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
469 vim_free(curbuf->b_ml.ml_line_ptr);
470 curbuf->b_ml.ml_line_ptr = newtext;
471 curbuf->b_ml.ml_line_len = textlen + len;
472 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
473}
474
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100475 static proptype_T *
476find_type_by_id(hashtab_T *ht, int id)
477{
478 long todo;
479 hashitem_T *hi;
480
481 if (ht == NULL)
482 return NULL;
483
484 // TODO: Make this faster by keeping a list of types sorted on ID and use
485 // a binary search.
486
487 todo = (long)ht->ht_used;
488 for (hi = ht->ht_array; todo > 0; ++hi)
489 {
490 if (!HASHITEM_EMPTY(hi))
491 {
492 proptype_T *prop = HI2PT(hi);
493
494 if (prop->pt_id == id)
495 return prop;
496 --todo;
497 }
498 }
499 return NULL;
500}
501
502/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100503 * Fill 'dict' with text properties in 'prop'.
504 */
505 static void
506prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
507{
508 proptype_T *pt;
509
510 dict_add_number(dict, "col", prop->tp_col);
511 dict_add_number(dict, "length", prop->tp_len);
512 dict_add_number(dict, "id", prop->tp_id);
513 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
514 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
515 pt = text_prop_type_by_id(buf, prop->tp_type);
516 if (pt != NULL)
517 dict_add_string(dict, "type", pt->pt_name);
518}
519
520/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100521 * Find a property type by ID in "buf" or globally.
522 * Returns NULL if not found.
523 */
524 proptype_T *
525text_prop_type_by_id(buf_T *buf, int id)
526{
527 proptype_T *type;
528
529 type = find_type_by_id(buf->b_proptypes, id);
530 if (type == NULL)
531 type = find_type_by_id(global_proptypes, id);
532 return type;
533}
534
535/*
536 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
537 */
538 void
539f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
540{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200541 linenr_T start;
542 linenr_T end;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100543 linenr_T lnum;
544 buf_T *buf = curbuf;
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100545 int did_clear = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100546
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200547 if (in_vim9script()
548 && (check_for_number_arg(argvars, 0) == FAIL
549 || check_for_opt_number_arg(argvars, 1) == FAIL
550 || (argvars[1].v_type != VAR_UNKNOWN
551 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
552 return;
553
554 start = tv_get_number(&argvars[0]);
555 end = start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100556 if (argvars[1].v_type != VAR_UNKNOWN)
557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100558 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100559 if (argvars[2].v_type != VAR_UNKNOWN)
560 {
561 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
562 return;
563 }
564 }
565 if (start < 1 || end < 1)
566 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200567 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100568 return;
569 }
570
571 for (lnum = start; lnum <= end; ++lnum)
572 {
573 char_u *text;
574 size_t len;
575
576 if (lnum > buf->b_ml.ml_line_count)
577 break;
578 text = ml_get_buf(buf, lnum, FALSE);
579 len = STRLEN(text) + 1;
580 if ((size_t)buf->b_ml.ml_line_len > len)
581 {
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100582 did_clear = TRUE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100583 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
584 {
585 char_u *newtext = vim_strsave(text);
586
587 // need to allocate the line now
588 if (newtext == NULL)
589 return;
590 buf->b_ml.ml_line_ptr = newtext;
591 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
592 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100593 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100594 }
595 }
Bram Moolenaarda1dbed2021-03-22 19:43:34 +0100596 if (did_clear)
597 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100598}
599
600/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100601 * prop_find({props} [, {direction}])
602 */
603 void
604f_prop_find(typval_T *argvars, typval_T *rettv)
605{
606 pos_T *cursor = &curwin->w_cursor;
607 dict_T *dict;
608 buf_T *buf = curbuf;
609 dictitem_T *di;
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200610 int lnum_start;
611 int start_pos_has_prop = 0;
612 int seen_end = 0;
613 int id = -1;
614 int type_id = -1;
615 int skipstart = 0;
616 int lnum = -1;
617 int col = -1;
618 int dir = 1; // 1 = forward, -1 = backward
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100619 int both;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100620
621 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
622 {
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200623 emsg(_(e_dictreq));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100624 return;
625 }
626 dict = argvars[0].vval.v_dict;
627
628 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
629 return;
630 if (buf->b_ml.ml_mfp == NULL)
631 return;
632
633 if (argvars[1].v_type != VAR_UNKNOWN)
634 {
635 char_u *dir_s = tv_get_string(&argvars[1]);
636
637 if (*dir_s == 'b')
638 dir = -1;
639 else if (*dir_s != 'f')
640 {
641 emsg(_(e_invarg));
642 return;
643 }
644 }
645
646 di = dict_find(dict, (char_u *)"lnum", -1);
647 if (di != NULL)
648 lnum = tv_get_number(&di->di_tv);
649
650 di = dict_find(dict, (char_u *)"col", -1);
651 if (di != NULL)
652 col = tv_get_number(&di->di_tv);
653
654 if (lnum == -1)
655 {
656 lnum = cursor->lnum;
657 col = cursor->col + 1;
658 }
659 else if (col == -1)
660 col = 1;
661
662 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
663 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200664 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100665 return;
666 }
667
Bram Moolenaareb245562020-09-03 22:33:44 +0200668 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100669
670 if (dict_find(dict, (char_u *)"id", -1) != NULL)
671 id = dict_get_number(dict, (char_u *)"id");
672 if (dict_find(dict, (char_u *)"type", -1))
673 {
674 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
675 proptype_T *type = lookup_prop_type(name, buf);
676
677 if (type == NULL)
678 return;
679 type_id = type->pt_id;
680 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100681 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100682 if (id == -1 && type_id == -1)
683 {
684 emsg(_("E968: Need at least one of 'id' or 'type'"));
685 return;
686 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100687 if (both && (id == -1 || type_id == -1))
688 {
689 emsg(_("E860: Need 'id' and 'type' with 'both'"));
690 return;
691 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100692
693 lnum_start = lnum;
694
695 if (rettv_dict_alloc(rettv) == FAIL)
696 return;
697
698 while (1)
699 {
700 char_u *text = ml_get_buf(buf, lnum, FALSE);
701 size_t textlen = STRLEN(text) + 1;
702 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200703 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100704 int i;
705 textprop_T prop;
706 int prop_start;
707 int prop_end;
708
709 for (i = 0; i < count; ++i)
710 {
711 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
712 sizeof(textprop_T));
713
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100714 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100715 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100716 if (dir < 0)
717 {
718 if (col < prop.tp_col)
719 break;
720 }
721 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
722 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100723 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100724 if (both ? prop.tp_id == id && prop.tp_type == type_id
725 : prop.tp_id == id || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100726 {
727 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100728 if (lnum_start == lnum
729 && col >= prop.tp_col
730 && (col <= prop.tp_col + prop.tp_len
731 - (prop.tp_len != 0)))
732 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100733
734 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
735 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
736 if (!prop_start && prop_end && dir > 0)
737 seen_end = 1;
738
739 // Skip lines without the start flag.
740 if (!prop_start)
741 {
742 // Always search backwards for start when search started
743 // on a prop and we're not skipping.
744 if (start_pos_has_prop && !skipstart)
745 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200746 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100747 }
748
749 // If skipstart is true, skip the prop at start pos (even if
750 // continued from another line).
751 if (start_pos_has_prop && skipstart && !seen_end)
752 {
753 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200754 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100755 }
756
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100757 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
758 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
759
760 return;
761 }
762 }
763
764 if (dir > 0)
765 {
766 if (lnum >= buf->b_ml.ml_line_count)
767 break;
768 lnum++;
769 }
770 else
771 {
772 if (lnum <= 1)
773 break;
774 lnum--;
775 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100776 // Adjust col to indicate that we're continuing from prev/next line.
777 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100778 }
779}
780
781/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100782 * prop_list({lnum} [, {bufnr}])
783 */
784 void
785f_prop_list(typval_T *argvars, typval_T *rettv)
786{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200787 linenr_T lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100788 buf_T *buf = curbuf;
789
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200790 if (in_vim9script()
791 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200792 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200793 return;
794
795 lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100796 if (argvars[1].v_type != VAR_UNKNOWN)
797 {
798 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
799 return;
800 }
801 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
802 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200803 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100804 return;
805 }
806
807 if (rettv_list_alloc(rettv) == OK)
808 {
809 char_u *text = ml_get_buf(buf, lnum, FALSE);
810 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100811 int count = (int)((buf->b_ml.ml_line_len - textlen)
812 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100813 int i;
814 textprop_T prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100815
816 for (i = 0; i < count; ++i)
817 {
818 dict_T *d = dict_alloc();
819
820 if (d == NULL)
821 break;
822 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
823 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100824 prop_fill_dict(d, &prop, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100825 list_append_dict(rettv->vval.v_list, d);
826 }
827 }
828}
829
830/*
831 * prop_remove({props} [, {lnum} [, {lnum_end}]])
832 */
833 void
834f_prop_remove(typval_T *argvars, typval_T *rettv)
835{
836 linenr_T start = 1;
837 linenr_T end = 0;
838 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +0200839 linenr_T first_changed = 0;
840 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100841 dict_T *dict;
842 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200843 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100844 int id = -1;
845 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200846 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100847
848 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200849
850 if (in_vim9script()
851 && (check_for_dict_arg(argvars, 0) == FAIL
852 || check_for_opt_number_arg(argvars, 1) == FAIL
853 || (argvars[1].v_type != VAR_UNKNOWN
854 && check_for_opt_number_arg(argvars, 2) == FAIL)))
855 return;
856
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100857 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100859 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100860 return;
861 }
862
863 if (argvars[1].v_type != VAR_UNKNOWN)
864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100865 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100866 end = start;
867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100868 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100869 if (start < 1 || end < 1)
870 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200871 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100872 return;
873 }
874 }
875
876 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200877 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
878 return;
879 if (buf->b_ml.ml_mfp == NULL)
880 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100881
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200882 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100883
884 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100885 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100886 if (dict_find(dict, (char_u *)"type", -1))
887 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100888 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100889 proptype_T *type = lookup_prop_type(name, buf);
890
891 if (type == NULL)
892 return;
893 type_id = type->pt_id;
894 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200895 both = dict_get_bool(dict, (char_u *)"both", FALSE);
896
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100897 if (id == -1 && type_id == -1)
898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100899 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100900 return;
901 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100902 if (both && (id == -1 || type_id == -1))
903 {
904 emsg(_("E860: Need 'id' and 'type' with 'both'"));
905 return;
906 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100907
908 if (end == 0)
909 end = buf->b_ml.ml_line_count;
910 for (lnum = start; lnum <= end; ++lnum)
911 {
912 char_u *text;
913 size_t len;
914
915 if (lnum > buf->b_ml.ml_line_count)
916 break;
917 text = ml_get_buf(buf, lnum, FALSE);
918 len = STRLEN(text) + 1;
919 if ((size_t)buf->b_ml.ml_line_len > len)
920 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200921 static textprop_T textprop; // static because of alignment
922 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100923
924 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
925 / sizeof(textprop_T); ++idx)
926 {
927 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
928 + idx * sizeof(textprop_T);
929 size_t taillen;
930
931 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100932 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
933 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100934 {
935 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
936 {
937 char_u *newptr = alloc(buf->b_ml.ml_line_len);
938
939 // need to allocate the line to be able to change it
940 if (newptr == NULL)
941 return;
942 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
943 buf->b_ml.ml_line_len);
944 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100945 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
946
947 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200948 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100949 }
950
951 taillen = buf->b_ml.ml_line_len - len
952 - (idx + 1) * sizeof(textprop_T);
953 if (taillen > 0)
954 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
955 taillen);
956 buf->b_ml.ml_line_len -= sizeof(textprop_T);
957 --idx;
958
Bram Moolenaar965c0442021-05-17 00:22:06 +0200959 if (first_changed == 0)
960 first_changed = lnum;
961 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100962 ++rettv->vval.v_number;
963 if (!do_all)
964 break;
965 }
966 }
967 }
968 }
Bram Moolenaar965c0442021-05-17 00:22:06 +0200969 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200970 {
Bram Moolenaar965c0442021-05-17 00:22:06 +0200971 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
972 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200973 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100974}
975
976/*
977 * Common for f_prop_type_add() and f_prop_type_change().
978 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200979 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100980prop_type_set(typval_T *argvars, int add)
981{
982 char_u *name;
983 buf_T *buf = NULL;
984 dict_T *dict;
985 dictitem_T *di;
986 proptype_T *prop;
987
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100988 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100989 if (*name == NUL)
990 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100991 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100992 return;
993 }
994
995 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
996 return;
997 dict = argvars[1].vval.v_dict;
998
999 prop = find_prop(name, buf);
1000 if (add)
1001 {
1002 hashtab_T **htp;
1003
1004 if (prop != NULL)
1005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001006 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001007 return;
1008 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001009 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001010 if (prop == NULL)
1011 return;
1012 STRCPY(prop->pt_name, name);
1013 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001014 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001015 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1016 if (*htp == NULL)
1017 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001018 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001019 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001020 {
1021 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001022 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001023 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001024 hash_init(*htp);
1025 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001026 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001027 }
1028 else
1029 {
1030 if (prop == NULL)
1031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001032 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001033 return;
1034 }
1035 }
1036
1037 if (dict != NULL)
1038 {
1039 di = dict_find(dict, (char_u *)"highlight", -1);
1040 if (di != NULL)
1041 {
1042 char_u *highlight;
1043 int hl_id = 0;
1044
Bram Moolenaar8f667172018-12-14 15:38:31 +01001045 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001046 if (highlight != NULL && *highlight != NUL)
1047 hl_id = syn_name2id(highlight);
1048 if (hl_id <= 0)
1049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001050 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001051 highlight == NULL ? (char_u *)"" : highlight);
1052 return;
1053 }
1054 prop->pt_hl_id = hl_id;
1055 }
1056
Bram Moolenaar58187f12019-05-05 16:33:47 +02001057 di = dict_find(dict, (char_u *)"combine", -1);
1058 if (di != NULL)
1059 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001060 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001061 prop->pt_flags |= PT_FLAG_COMBINE;
1062 else
1063 prop->pt_flags &= ~PT_FLAG_COMBINE;
1064 }
1065
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001066 di = dict_find(dict, (char_u *)"priority", -1);
1067 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001068 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001069
1070 di = dict_find(dict, (char_u *)"start_incl", -1);
1071 if (di != NULL)
1072 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001073 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001074 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1075 else
1076 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1077 }
1078
1079 di = dict_find(dict, (char_u *)"end_incl", -1);
1080 if (di != NULL)
1081 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001082 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001083 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1084 else
1085 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1086 }
1087 }
1088}
1089
1090/*
1091 * prop_type_add({name}, {props})
1092 */
1093 void
1094f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1095{
1096 prop_type_set(argvars, TRUE);
1097}
1098
1099/*
1100 * prop_type_change({name}, {props})
1101 */
1102 void
1103f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1104{
1105 prop_type_set(argvars, FALSE);
1106}
1107
1108/*
1109 * prop_type_delete({name} [, {bufnr}])
1110 */
1111 void
1112f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1113{
1114 char_u *name;
1115 buf_T *buf = NULL;
1116 hashitem_T *hi;
1117
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001118 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001119 if (*name == NUL)
1120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001121 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001122 return;
1123 }
1124
1125 if (argvars[1].v_type != VAR_UNKNOWN)
1126 {
1127 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1128 return;
1129 }
1130
1131 hi = find_prop_hi(name, buf);
1132 if (hi != NULL)
1133 {
1134 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001135 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001136
1137 if (buf == NULL)
1138 ht = global_proptypes;
1139 else
1140 ht = buf->b_proptypes;
1141 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001142 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001143 }
1144}
1145
1146/*
1147 * prop_type_get({name} [, {bufnr}])
1148 */
1149 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001150f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001151{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001152 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001153
1154 if (*name == NUL)
1155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001156 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001157 return;
1158 }
1159 if (rettv_dict_alloc(rettv) == OK)
1160 {
1161 proptype_T *prop = NULL;
1162 buf_T *buf = NULL;
1163
1164 if (argvars[1].v_type != VAR_UNKNOWN)
1165 {
1166 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1167 return;
1168 }
1169
1170 prop = find_prop(name, buf);
1171 if (prop != NULL)
1172 {
1173 dict_T *d = rettv->vval.v_dict;
1174
1175 if (prop->pt_hl_id > 0)
1176 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1177 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001178 dict_add_number(d, "combine",
1179 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001180 dict_add_number(d, "start_incl",
1181 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1182 dict_add_number(d, "end_incl",
1183 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1184 if (buf != NULL)
1185 dict_add_number(d, "bufnr", buf->b_fnum);
1186 }
1187 }
1188}
1189
1190 static void
1191list_types(hashtab_T *ht, list_T *l)
1192{
1193 long todo;
1194 hashitem_T *hi;
1195
1196 todo = (long)ht->ht_used;
1197 for (hi = ht->ht_array; todo > 0; ++hi)
1198 {
1199 if (!HASHITEM_EMPTY(hi))
1200 {
1201 proptype_T *prop = HI2PT(hi);
1202
1203 list_append_string(l, prop->pt_name, -1);
1204 --todo;
1205 }
1206 }
1207}
1208
1209/*
1210 * prop_type_list([{bufnr}])
1211 */
1212 void
1213f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1214{
1215 buf_T *buf = NULL;
1216
1217 if (rettv_list_alloc(rettv) == OK)
1218 {
1219 if (argvars[0].v_type != VAR_UNKNOWN)
1220 {
1221 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1222 return;
1223 }
1224 if (buf == NULL)
1225 {
1226 if (global_proptypes != NULL)
1227 list_types(global_proptypes, rettv->vval.v_list);
1228 }
1229 else if (buf->b_proptypes != NULL)
1230 list_types(buf->b_proptypes, rettv->vval.v_list);
1231 }
1232}
1233
1234/*
1235 * Free all property types in "ht".
1236 */
1237 static void
1238clear_ht_prop_types(hashtab_T *ht)
1239{
1240 long todo;
1241 hashitem_T *hi;
1242
1243 if (ht == NULL)
1244 return;
1245
1246 todo = (long)ht->ht_used;
1247 for (hi = ht->ht_array; todo > 0; ++hi)
1248 {
1249 if (!HASHITEM_EMPTY(hi))
1250 {
1251 proptype_T *prop = HI2PT(hi);
1252
1253 vim_free(prop);
1254 --todo;
1255 }
1256 }
1257
1258 hash_clear(ht);
1259 vim_free(ht);
1260}
1261
1262#if defined(EXITFREE) || defined(PROTO)
1263/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001264 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001265 */
1266 void
1267clear_global_prop_types(void)
1268{
1269 clear_ht_prop_types(global_proptypes);
1270 global_proptypes = NULL;
1271}
1272#endif
1273
1274/*
1275 * Free all property types for "buf".
1276 */
1277 void
1278clear_buf_prop_types(buf_T *buf)
1279{
1280 clear_ht_prop_types(buf->b_proptypes);
1281 buf->b_proptypes = NULL;
1282}
1283
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001284// Struct used to return two values from adjust_prop().
1285typedef struct
1286{
1287 int dirty; // if the property was changed
1288 int can_drop; // whether after this change, the prop may be removed
1289} adjustres_T;
1290
1291/*
1292 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1293 *
1294 * Note that "col" is zero-based, while tp_col is one-based.
1295 * Only for the current buffer.
1296 * "flags" can have:
1297 * APC_SUBSTITUTE: Text is replaced, not inserted.
1298 */
1299 static adjustres_T
1300adjust_prop(
1301 textprop_T *prop,
1302 colnr_T col,
1303 int added,
1304 int flags)
1305{
1306 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1307 int start_incl = (pt != NULL
1308 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1309 || (flags & APC_SUBSTITUTE);
1310 int end_incl = (pt != NULL
1311 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1312 // Do not drop zero-width props if they later can increase in
1313 // size.
1314 int droppable = !(start_incl || end_incl);
1315 adjustres_T res = {TRUE, FALSE};
1316
1317 if (added > 0)
1318 {
1319 if (col + 1 <= prop->tp_col
1320 - (start_incl || (prop->tp_len == 0 && end_incl)))
1321 // Change is entirely before the text property: Only shift
1322 prop->tp_col += added;
1323 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1324 // Insertion was inside text property
1325 prop->tp_len += added;
1326 }
1327 else if (prop->tp_col > col + 1)
1328 {
1329 if (prop->tp_col + added < col + 1)
1330 {
1331 prop->tp_len += (prop->tp_col - 1 - col) + added;
1332 prop->tp_col = col + 1;
1333 if (prop->tp_len <= 0)
1334 {
1335 prop->tp_len = 0;
1336 res.can_drop = droppable;
1337 }
1338 }
1339 else
1340 prop->tp_col += added;
1341 }
1342 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1343 {
1344 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1345
1346 prop->tp_len += after > 0 ? added + after : added;
1347 res.can_drop = prop->tp_len <= 0 && droppable;
1348 }
1349 else
1350 res.dirty = FALSE;
1351
1352 return res;
1353}
1354
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001355/*
1356 * Adjust the columns of text properties in line "lnum" after position "col" to
1357 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001358 * Note that "col" is zero-based, while tp_col is one-based.
1359 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001360 * "flags" can have:
1361 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1362 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001363 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001364 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001365 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001366 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001367adjust_prop_columns(
1368 linenr_T lnum,
1369 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001370 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001371 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001372{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001373 int proplen;
1374 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001375 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001376 int ri, wi;
1377 size_t textlen;
1378
1379 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001380 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001381
1382 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1383 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001384 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001385 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001386
Bram Moolenaar196d1572019-01-02 23:47:18 +01001387 wi = 0; // write index
1388 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001389 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001390 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001391 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001392
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001393 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1394 res = adjust_prop(&prop, col, bytes_added, flags);
1395 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001396 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001397 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001398 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1399 && u_savesub(lnum) == FAIL)
1400 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001401 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001402
1403 // u_savesub() may have updated curbuf->b_ml, fetch it again
1404 if (curbuf->b_ml.ml_line_lnum != lnum)
1405 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001406 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001407 if (res.can_drop)
1408 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001409 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001410 ++wi;
1411 }
1412 if (dirty)
1413 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001414 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1415
1416 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1417 curbuf->b_ml.ml_line_ptr =
1418 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001419 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001420 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001421 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001422 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001423}
1424
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001425/*
1426 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001427 * "lnum_props" is the line that has the properties from before the split.
1428 * "lnum_top" is the top line.
1429 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001430 * "deleted" is the number of bytes deleted.
1431 */
1432 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001433adjust_props_for_split(
1434 linenr_T lnum_props,
1435 linenr_T lnum_top,
1436 int kept,
1437 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001438{
1439 char_u *props;
1440 int count;
1441 garray_T prevprop;
1442 garray_T nextprop;
1443 int i;
1444 int skipped = kept + deleted;
1445
1446 if (!curbuf->b_has_textprop)
1447 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001448
1449 // Get the text properties from "lnum_props".
1450 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001451 ga_init2(&prevprop, sizeof(textprop_T), 10);
1452 ga_init2(&nextprop, sizeof(textprop_T), 10);
1453
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001454 // Keep the relevant ones in the first line, reducing the length if needed.
1455 // Copy the ones that include the split to the second line.
1456 // Move the ones after the split to the second line.
1457 for (i = 0; i < count; ++i)
1458 {
1459 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001460 proptype_T *pt;
1461 int start_incl, end_incl;
1462 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001463
1464 // copy the prop to an aligned structure
1465 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1466
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001467 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1468 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1469 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1470 cont_prev = prop.tp_col + !start_incl <= kept;
1471 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1472
1473 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001474 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001475 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1476
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001477 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001478 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001479 if (p->tp_col + p->tp_len >= kept)
1480 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001481 if (cont_next)
1482 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001483 }
1484
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001485 // Only add the property to the next line if the length is bigger than
1486 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001487 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001488 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001489 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001490 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001491 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001492 if (p->tp_col > skipped)
1493 p->tp_col -= skipped - 1;
1494 else
1495 {
1496 p->tp_len -= skipped - p->tp_col;
1497 p->tp_col = 1;
1498 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001499 if (cont_prev)
1500 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001501 }
1502 }
1503
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001504 set_text_props(lnum_top, prevprop.ga_data,
1505 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001506 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001507 set_text_props(lnum_top + 1, nextprop.ga_data,
1508 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001509 ga_clear(&nextprop);
1510}
1511
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001512/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001513 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001514 */
1515 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001516prepend_joined_props(
1517 char_u *new_props,
1518 int propcount,
1519 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001520 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001521 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001522 long col,
1523 int removed)
1524{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001525 char_u *props;
1526 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1527 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001528
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001529 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001530 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001531 textprop_T prop;
1532 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001533
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001534 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1535 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1536
1537 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001538 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001539
1540 if (add_all || end)
1541 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1542 &prop, sizeof(prop));
1543 else
1544 {
1545 int j;
1546 int found = FALSE;
1547
1548 // Search for continuing prop.
1549 for (j = *props_remaining; j < propcount; ++j)
1550 {
1551 textprop_T op;
1552
1553 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1554 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1555 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001556 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001557 found = TRUE;
1558 op.tp_len += op.tp_col - prop.tp_col;
1559 op.tp_col = prop.tp_col;
1560 // Start/end is taken care of when deleting joined lines
1561 op.tp_flags = prop.tp_flags;
1562 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1563 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001564 }
1565 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001566 if (!found)
1567 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001568 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001569 }
1570}
1571
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001572#endif // FEAT_PROP_POPUP