blob: 4cde75544d4c304cf5fe62ba2d2b3a8450f95cb6 [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
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200621 if (in_vim9script()
622 && (check_for_dict_arg(argvars, 0) == FAIL
623 || check_for_opt_string_arg(argvars, 1) == FAIL))
624 return;
625
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100626 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
627 {
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200628 emsg(_(e_dictreq));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100629 return;
630 }
631 dict = argvars[0].vval.v_dict;
632
633 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
634 return;
635 if (buf->b_ml.ml_mfp == NULL)
636 return;
637
638 if (argvars[1].v_type != VAR_UNKNOWN)
639 {
640 char_u *dir_s = tv_get_string(&argvars[1]);
641
642 if (*dir_s == 'b')
643 dir = -1;
644 else if (*dir_s != 'f')
645 {
646 emsg(_(e_invarg));
647 return;
648 }
649 }
650
651 di = dict_find(dict, (char_u *)"lnum", -1);
652 if (di != NULL)
653 lnum = tv_get_number(&di->di_tv);
654
655 di = dict_find(dict, (char_u *)"col", -1);
656 if (di != NULL)
657 col = tv_get_number(&di->di_tv);
658
659 if (lnum == -1)
660 {
661 lnum = cursor->lnum;
662 col = cursor->col + 1;
663 }
664 else if (col == -1)
665 col = 1;
666
667 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
668 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200669 emsg(_(e_invalid_range));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100670 return;
671 }
672
Bram Moolenaareb245562020-09-03 22:33:44 +0200673 skipstart = dict_get_bool(dict, (char_u *)"skipstart", 0);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100674
675 if (dict_find(dict, (char_u *)"id", -1) != NULL)
676 id = dict_get_number(dict, (char_u *)"id");
677 if (dict_find(dict, (char_u *)"type", -1))
678 {
679 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
680 proptype_T *type = lookup_prop_type(name, buf);
681
682 if (type == NULL)
683 return;
684 type_id = type->pt_id;
685 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100686 both = dict_get_bool(dict, (char_u *)"both", FALSE);
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100687 if (id == -1 && type_id == -1)
688 {
689 emsg(_("E968: Need at least one of 'id' or 'type'"));
690 return;
691 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100692 if (both && (id == -1 || type_id == -1))
693 {
694 emsg(_("E860: Need 'id' and 'type' with 'both'"));
695 return;
696 }
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100697
698 lnum_start = lnum;
699
700 if (rettv_dict_alloc(rettv) == FAIL)
701 return;
702
703 while (1)
704 {
705 char_u *text = ml_get_buf(buf, lnum, FALSE);
706 size_t textlen = STRLEN(text) + 1;
707 int count = (int)((buf->b_ml.ml_line_len - textlen)
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200708 / sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100709 int i;
710 textprop_T prop;
711 int prop_start;
712 int prop_end;
713
714 for (i = 0; i < count; ++i)
715 {
716 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
717 sizeof(textprop_T));
718
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100719 if (lnum == lnum_start)
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100720 {
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100721 if (dir < 0)
722 {
723 if (col < prop.tp_col)
724 break;
725 }
726 else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col)
727 continue;
Bram Moolenaar965fd8d2020-03-14 07:46:40 +0100728 }
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100729 if (both ? prop.tp_id == id && prop.tp_type == type_id
730 : prop.tp_id == id || prop.tp_type == type_id)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100731 {
732 // Check if the starting position has text props.
Bram Moolenaar66b98852020-03-11 19:15:52 +0100733 if (lnum_start == lnum
734 && col >= prop.tp_col
735 && (col <= prop.tp_col + prop.tp_len
736 - (prop.tp_len != 0)))
737 start_pos_has_prop = 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100738
739 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
740 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
741 if (!prop_start && prop_end && dir > 0)
742 seen_end = 1;
743
744 // Skip lines without the start flag.
745 if (!prop_start)
746 {
747 // Always search backwards for start when search started
748 // on a prop and we're not skipping.
749 if (start_pos_has_prop && !skipstart)
750 dir = -1;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200751 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100752 }
753
754 // If skipstart is true, skip the prop at start pos (even if
755 // continued from another line).
756 if (start_pos_has_prop && skipstart && !seen_end)
757 {
758 start_pos_has_prop = 0;
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200759 continue;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100760 }
761
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100762 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
763 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
764
765 return;
766 }
767 }
768
769 if (dir > 0)
770 {
771 if (lnum >= buf->b_ml.ml_line_count)
772 break;
773 lnum++;
774 }
775 else
776 {
777 if (lnum <= 1)
778 break;
779 lnum--;
780 }
Bram Moolenaar66b98852020-03-11 19:15:52 +0100781 // Adjust col to indicate that we're continuing from prev/next line.
782 col = dir < 0 ? buf->b_ml.ml_line_len : 1;
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100783 }
784}
785
786/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100787 * prop_list({lnum} [, {bufnr}])
788 */
789 void
790f_prop_list(typval_T *argvars, typval_T *rettv)
791{
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200792 linenr_T lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100793 buf_T *buf = curbuf;
794
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200795 if (in_vim9script()
796 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200797 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200798 return;
799
800 lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100801 if (argvars[1].v_type != VAR_UNKNOWN)
802 {
803 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
804 return;
805 }
806 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
807 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200808 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100809 return;
810 }
811
812 if (rettv_list_alloc(rettv) == OK)
813 {
814 char_u *text = ml_get_buf(buf, lnum, FALSE);
815 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100816 int count = (int)((buf->b_ml.ml_line_len - textlen)
817 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100818 int i;
819 textprop_T prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100820
821 for (i = 0; i < count; ++i)
822 {
823 dict_T *d = dict_alloc();
824
825 if (d == NULL)
826 break;
827 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
828 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100829 prop_fill_dict(d, &prop, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100830 list_append_dict(rettv->vval.v_list, d);
831 }
832 }
833}
834
835/*
836 * prop_remove({props} [, {lnum} [, {lnum_end}]])
837 */
838 void
839f_prop_remove(typval_T *argvars, typval_T *rettv)
840{
841 linenr_T start = 1;
842 linenr_T end = 0;
843 linenr_T lnum;
Bram Moolenaar965c0442021-05-17 00:22:06 +0200844 linenr_T first_changed = 0;
845 linenr_T last_changed = 0;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100846 dict_T *dict;
847 buf_T *buf = curbuf;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200848 int do_all;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100849 int id = -1;
850 int type_id = -1;
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200851 int both;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100852
853 rettv->vval.v_number = 0;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200854
855 if (in_vim9script()
856 && (check_for_dict_arg(argvars, 0) == FAIL
857 || check_for_opt_number_arg(argvars, 1) == FAIL
858 || (argvars[1].v_type != VAR_UNKNOWN
859 && check_for_opt_number_arg(argvars, 2) == FAIL)))
860 return;
861
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100862 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100864 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100865 return;
866 }
867
868 if (argvars[1].v_type != VAR_UNKNOWN)
869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100870 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100871 end = start;
872 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100873 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100874 if (start < 1 || end < 1)
875 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200876 emsg(_(e_invalid_range));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100877 return;
878 }
879 }
880
881 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200882 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
883 return;
884 if (buf->b_ml.ml_mfp == NULL)
885 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100886
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200887 do_all = dict_get_bool(dict, (char_u *)"all", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100888
889 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100890 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100891 if (dict_find(dict, (char_u *)"type", -1))
892 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100893 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100894 proptype_T *type = lookup_prop_type(name, buf);
895
896 if (type == NULL)
897 return;
898 type_id = type->pt_id;
899 }
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200900 both = dict_get_bool(dict, (char_u *)"both", FALSE);
901
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100902 if (id == -1 && type_id == -1)
903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100904 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100905 return;
906 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100907 if (both && (id == -1 || type_id == -1))
908 {
909 emsg(_("E860: Need 'id' and 'type' with 'both'"));
910 return;
911 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100912
913 if (end == 0)
914 end = buf->b_ml.ml_line_count;
915 for (lnum = start; lnum <= end; ++lnum)
916 {
917 char_u *text;
918 size_t len;
919
920 if (lnum > buf->b_ml.ml_line_count)
921 break;
922 text = ml_get_buf(buf, lnum, FALSE);
923 len = STRLEN(text) + 1;
924 if ((size_t)buf->b_ml.ml_line_len > len)
925 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200926 static textprop_T textprop; // static because of alignment
927 unsigned idx;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100928
929 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
930 / sizeof(textprop_T); ++idx)
931 {
932 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
933 + idx * sizeof(textprop_T);
934 size_t taillen;
935
936 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100937 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
938 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100939 {
940 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
941 {
942 char_u *newptr = alloc(buf->b_ml.ml_line_len);
943
944 // need to allocate the line to be able to change it
945 if (newptr == NULL)
946 return;
947 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
948 buf->b_ml.ml_line_len);
949 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100950 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
951
952 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200953 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100954 }
955
956 taillen = buf->b_ml.ml_line_len - len
957 - (idx + 1) * sizeof(textprop_T);
958 if (taillen > 0)
959 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
960 taillen);
961 buf->b_ml.ml_line_len -= sizeof(textprop_T);
962 --idx;
963
Bram Moolenaar965c0442021-05-17 00:22:06 +0200964 if (first_changed == 0)
965 first_changed = lnum;
966 last_changed = lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100967 ++rettv->vval.v_number;
968 if (!do_all)
969 break;
970 }
971 }
972 }
973 }
Bram Moolenaar965c0442021-05-17 00:22:06 +0200974 if (first_changed > 0)
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200975 {
Bram Moolenaar965c0442021-05-17 00:22:06 +0200976 changed_lines_buf(buf, first_changed, last_changed + 1, 0);
977 redraw_buf_later(buf, VALID);
Bram Moolenaarfc643e62021-05-17 00:15:18 +0200978 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100979}
980
981/*
982 * Common for f_prop_type_add() and f_prop_type_change().
983 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200984 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100985prop_type_set(typval_T *argvars, int add)
986{
987 char_u *name;
988 buf_T *buf = NULL;
989 dict_T *dict;
990 dictitem_T *di;
991 proptype_T *prop;
992
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200993 if (in_vim9script()
994 && (check_for_string_arg(argvars, 0) == FAIL
995 || check_for_dict_arg(argvars, 1) == FAIL))
996 return;
997
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100998 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100999 if (*name == NUL)
1000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001001 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001002 return;
1003 }
1004
1005 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1006 return;
1007 dict = argvars[1].vval.v_dict;
1008
1009 prop = find_prop(name, buf);
1010 if (add)
1011 {
1012 hashtab_T **htp;
1013
1014 if (prop != NULL)
1015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001016 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001017 return;
1018 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001019 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001020 if (prop == NULL)
1021 return;
1022 STRCPY(prop->pt_name, name);
1023 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +01001024 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001025 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
1026 if (*htp == NULL)
1027 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001028 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001029 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001030 {
1031 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001032 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001033 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001034 hash_init(*htp);
1035 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001036 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001037 }
1038 else
1039 {
1040 if (prop == NULL)
1041 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001042 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001043 return;
1044 }
1045 }
1046
1047 if (dict != NULL)
1048 {
1049 di = dict_find(dict, (char_u *)"highlight", -1);
1050 if (di != NULL)
1051 {
1052 char_u *highlight;
1053 int hl_id = 0;
1054
Bram Moolenaar8f667172018-12-14 15:38:31 +01001055 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001056 if (highlight != NULL && *highlight != NUL)
1057 hl_id = syn_name2id(highlight);
1058 if (hl_id <= 0)
1059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001060 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001061 highlight == NULL ? (char_u *)"" : highlight);
1062 return;
1063 }
1064 prop->pt_hl_id = hl_id;
1065 }
1066
Bram Moolenaar58187f12019-05-05 16:33:47 +02001067 di = dict_find(dict, (char_u *)"combine", -1);
1068 if (di != NULL)
1069 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001070 if (tv_get_bool(&di->di_tv))
Bram Moolenaar58187f12019-05-05 16:33:47 +02001071 prop->pt_flags |= PT_FLAG_COMBINE;
1072 else
1073 prop->pt_flags &= ~PT_FLAG_COMBINE;
1074 }
1075
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001076 di = dict_find(dict, (char_u *)"priority", -1);
1077 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001078 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001079
1080 di = dict_find(dict, (char_u *)"start_incl", -1);
1081 if (di != NULL)
1082 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001083 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001084 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1085 else
1086 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1087 }
1088
1089 di = dict_find(dict, (char_u *)"end_incl", -1);
1090 if (di != NULL)
1091 {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +02001092 if (tv_get_bool(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001093 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1094 else
1095 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1096 }
1097 }
1098}
1099
1100/*
1101 * prop_type_add({name}, {props})
1102 */
1103 void
1104f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1105{
1106 prop_type_set(argvars, TRUE);
1107}
1108
1109/*
1110 * prop_type_change({name}, {props})
1111 */
1112 void
1113f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1114{
1115 prop_type_set(argvars, FALSE);
1116}
1117
1118/*
1119 * prop_type_delete({name} [, {bufnr}])
1120 */
1121 void
1122f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1123{
1124 char_u *name;
1125 buf_T *buf = NULL;
1126 hashitem_T *hi;
1127
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001128 if (in_vim9script()
1129 && (check_for_string_arg(argvars, 0) == FAIL
1130 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1131 return;
1132
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001133 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001134 if (*name == NUL)
1135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001136 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001137 return;
1138 }
1139
1140 if (argvars[1].v_type != VAR_UNKNOWN)
1141 {
1142 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1143 return;
1144 }
1145
1146 hi = find_prop_hi(name, buf);
1147 if (hi != NULL)
1148 {
1149 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001150 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001151
1152 if (buf == NULL)
1153 ht = global_proptypes;
1154 else
1155 ht = buf->b_proptypes;
1156 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001157 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001158 }
1159}
1160
1161/*
1162 * prop_type_get({name} [, {bufnr}])
1163 */
1164 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001165f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001166{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001167 char_u *name;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001168
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001169 if (in_vim9script()
1170 && (check_for_string_arg(argvars, 0) == FAIL
1171 || check_for_opt_dict_arg(argvars, 1) == FAIL))
1172 return;
1173
1174 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001175 if (*name == NUL)
1176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001177 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001178 return;
1179 }
1180 if (rettv_dict_alloc(rettv) == OK)
1181 {
1182 proptype_T *prop = NULL;
1183 buf_T *buf = NULL;
1184
1185 if (argvars[1].v_type != VAR_UNKNOWN)
1186 {
1187 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1188 return;
1189 }
1190
1191 prop = find_prop(name, buf);
1192 if (prop != NULL)
1193 {
1194 dict_T *d = rettv->vval.v_dict;
1195
1196 if (prop->pt_hl_id > 0)
1197 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1198 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001199 dict_add_number(d, "combine",
1200 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001201 dict_add_number(d, "start_incl",
1202 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1203 dict_add_number(d, "end_incl",
1204 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1205 if (buf != NULL)
1206 dict_add_number(d, "bufnr", buf->b_fnum);
1207 }
1208 }
1209}
1210
1211 static void
1212list_types(hashtab_T *ht, list_T *l)
1213{
1214 long todo;
1215 hashitem_T *hi;
1216
1217 todo = (long)ht->ht_used;
1218 for (hi = ht->ht_array; todo > 0; ++hi)
1219 {
1220 if (!HASHITEM_EMPTY(hi))
1221 {
1222 proptype_T *prop = HI2PT(hi);
1223
1224 list_append_string(l, prop->pt_name, -1);
1225 --todo;
1226 }
1227 }
1228}
1229
1230/*
1231 * prop_type_list([{bufnr}])
1232 */
1233 void
1234f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1235{
1236 buf_T *buf = NULL;
1237
1238 if (rettv_list_alloc(rettv) == OK)
1239 {
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001240 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
1241 return;
1242
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001243 if (argvars[0].v_type != VAR_UNKNOWN)
1244 {
1245 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1246 return;
1247 }
1248 if (buf == NULL)
1249 {
1250 if (global_proptypes != NULL)
1251 list_types(global_proptypes, rettv->vval.v_list);
1252 }
1253 else if (buf->b_proptypes != NULL)
1254 list_types(buf->b_proptypes, rettv->vval.v_list);
1255 }
1256}
1257
1258/*
1259 * Free all property types in "ht".
1260 */
1261 static void
1262clear_ht_prop_types(hashtab_T *ht)
1263{
1264 long todo;
1265 hashitem_T *hi;
1266
1267 if (ht == NULL)
1268 return;
1269
1270 todo = (long)ht->ht_used;
1271 for (hi = ht->ht_array; todo > 0; ++hi)
1272 {
1273 if (!HASHITEM_EMPTY(hi))
1274 {
1275 proptype_T *prop = HI2PT(hi);
1276
1277 vim_free(prop);
1278 --todo;
1279 }
1280 }
1281
1282 hash_clear(ht);
1283 vim_free(ht);
1284}
1285
1286#if defined(EXITFREE) || defined(PROTO)
1287/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001288 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001289 */
1290 void
1291clear_global_prop_types(void)
1292{
1293 clear_ht_prop_types(global_proptypes);
1294 global_proptypes = NULL;
1295}
1296#endif
1297
1298/*
1299 * Free all property types for "buf".
1300 */
1301 void
1302clear_buf_prop_types(buf_T *buf)
1303{
1304 clear_ht_prop_types(buf->b_proptypes);
1305 buf->b_proptypes = NULL;
1306}
1307
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001308// Struct used to return two values from adjust_prop().
1309typedef struct
1310{
1311 int dirty; // if the property was changed
1312 int can_drop; // whether after this change, the prop may be removed
1313} adjustres_T;
1314
1315/*
1316 * Adjust the property for "added" bytes (can be negative) inserted at "col".
1317 *
1318 * Note that "col" is zero-based, while tp_col is one-based.
1319 * Only for the current buffer.
1320 * "flags" can have:
1321 * APC_SUBSTITUTE: Text is replaced, not inserted.
1322 */
1323 static adjustres_T
1324adjust_prop(
1325 textprop_T *prop,
1326 colnr_T col,
1327 int added,
1328 int flags)
1329{
1330 proptype_T *pt = text_prop_type_by_id(curbuf, prop->tp_type);
1331 int start_incl = (pt != NULL
1332 && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1333 || (flags & APC_SUBSTITUTE);
1334 int end_incl = (pt != NULL
1335 && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1336 // Do not drop zero-width props if they later can increase in
1337 // size.
1338 int droppable = !(start_incl || end_incl);
1339 adjustres_T res = {TRUE, FALSE};
1340
1341 if (added > 0)
1342 {
1343 if (col + 1 <= prop->tp_col
1344 - (start_incl || (prop->tp_len == 0 && end_incl)))
1345 // Change is entirely before the text property: Only shift
1346 prop->tp_col += added;
1347 else if (col + 1 < prop->tp_col + prop->tp_len + end_incl)
1348 // Insertion was inside text property
1349 prop->tp_len += added;
1350 }
1351 else if (prop->tp_col > col + 1)
1352 {
1353 if (prop->tp_col + added < col + 1)
1354 {
1355 prop->tp_len += (prop->tp_col - 1 - col) + added;
1356 prop->tp_col = col + 1;
1357 if (prop->tp_len <= 0)
1358 {
1359 prop->tp_len = 0;
1360 res.can_drop = droppable;
1361 }
1362 }
1363 else
1364 prop->tp_col += added;
1365 }
1366 else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col)
1367 {
1368 int after = col - added - (prop->tp_col - 1 + prop->tp_len);
1369
1370 prop->tp_len += after > 0 ? added + after : added;
1371 res.can_drop = prop->tp_len <= 0 && droppable;
1372 }
1373 else
1374 res.dirty = FALSE;
1375
1376 return res;
1377}
1378
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001379/*
1380 * Adjust the columns of text properties in line "lnum" after position "col" to
1381 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001382 * Note that "col" is zero-based, while tp_col is one-based.
1383 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001384 * "flags" can have:
1385 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1386 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001387 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001388 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001389 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001390 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001391adjust_prop_columns(
1392 linenr_T lnum,
1393 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001394 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001395 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001396{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001397 int proplen;
1398 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001399 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001400 int ri, wi;
1401 size_t textlen;
1402
1403 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001404 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001405
1406 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1407 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001408 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001409 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001410
Bram Moolenaar196d1572019-01-02 23:47:18 +01001411 wi = 0; // write index
1412 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001413 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001414 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001415 adjustres_T res;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001416
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001417 mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop));
1418 res = adjust_prop(&prop, col, bytes_added, flags);
1419 if (res.dirty)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001420 {
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001421 // Save for undo if requested and not done yet.
Bram Moolenaarcf070112020-06-29 23:02:21 +02001422 if ((flags & APC_SAVE_FOR_UNDO) && !dirty
1423 && u_savesub(lnum) == FAIL)
1424 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001425 dirty = TRUE;
Bram Moolenaar8902b312020-09-20 21:04:35 +02001426
1427 // u_savesub() may have updated curbuf->b_ml, fetch it again
1428 if (curbuf->b_ml.ml_line_lnum != lnum)
1429 proplen = get_text_props(curbuf, lnum, &props, TRUE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001430 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001431 if (res.can_drop)
1432 continue; // Drop this text property
Bram Moolenaar12f20032020-02-26 22:06:00 +01001433 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001434 ++wi;
1435 }
1436 if (dirty)
1437 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001438 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1439
1440 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1441 curbuf->b_ml.ml_line_ptr =
1442 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001443 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001444 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001445 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001446 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001447}
1448
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001449/*
1450 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001451 * "lnum_props" is the line that has the properties from before the split.
1452 * "lnum_top" is the top line.
1453 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001454 * "deleted" is the number of bytes deleted.
1455 */
1456 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001457adjust_props_for_split(
1458 linenr_T lnum_props,
1459 linenr_T lnum_top,
1460 int kept,
1461 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001462{
1463 char_u *props;
1464 int count;
1465 garray_T prevprop;
1466 garray_T nextprop;
1467 int i;
1468 int skipped = kept + deleted;
1469
1470 if (!curbuf->b_has_textprop)
1471 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001472
1473 // Get the text properties from "lnum_props".
1474 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001475 ga_init2(&prevprop, sizeof(textprop_T), 10);
1476 ga_init2(&nextprop, sizeof(textprop_T), 10);
1477
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001478 // Keep the relevant ones in the first line, reducing the length if needed.
1479 // Copy the ones that include the split to the second line.
1480 // Move the ones after the split to the second line.
1481 for (i = 0; i < count; ++i)
1482 {
1483 textprop_T prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001484 proptype_T *pt;
1485 int start_incl, end_incl;
1486 int cont_prev, cont_next;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001487
1488 // copy the prop to an aligned structure
1489 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1490
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001491 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1492 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
1493 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1494 cont_prev = prop.tp_col + !start_incl <= kept;
1495 cont_next = skipped <= prop.tp_col + prop.tp_len - !end_incl;
1496
1497 if (cont_prev && ga_grow(&prevprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001498 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001499 textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1500
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001501 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001502 ++prevprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001503 if (p->tp_col + p->tp_len >= kept)
1504 p->tp_len = kept - p->tp_col;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001505 if (cont_next)
1506 p->tp_flags |= TP_FLAG_CONT_NEXT;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001507 }
1508
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001509 // Only add the property to the next line if the length is bigger than
1510 // zero.
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001511 if (cont_next && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001512 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001513 textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001514 *p = prop;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001515 ++nextprop.ga_len;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001516 if (p->tp_col > skipped)
1517 p->tp_col -= skipped - 1;
1518 else
1519 {
1520 p->tp_len -= skipped - p->tp_col;
1521 p->tp_col = 1;
1522 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001523 if (cont_prev)
1524 p->tp_flags |= TP_FLAG_CONT_PREV;
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001525 }
1526 }
1527
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001528 set_text_props(lnum_top, prevprop.ga_data,
1529 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001530 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001531 set_text_props(lnum_top + 1, nextprop.ga_data,
1532 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001533 ga_clear(&nextprop);
1534}
1535
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001536/*
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001537 * Prepend properties of joined line "lnum" to "new_props".
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001538 */
1539 void
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001540prepend_joined_props(
1541 char_u *new_props,
1542 int propcount,
1543 int *props_remaining,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001544 linenr_T lnum,
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001545 int add_all,
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001546 long col,
1547 int removed)
1548{
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001549 char_u *props;
1550 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
1551 int i;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001552
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001553 for (i = proplen; i-- > 0; )
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001554 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001555 textprop_T prop;
1556 int end;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001557
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001558 mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop));
1559 end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
1560
1561 adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001562 adjust_prop(&prop, -1, col, 0); // Make line start at its final column
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001563
1564 if (add_all || end)
1565 mch_memmove(new_props + --(*props_remaining) * sizeof(prop),
1566 &prop, sizeof(prop));
1567 else
1568 {
1569 int j;
1570 int found = FALSE;
1571
1572 // Search for continuing prop.
1573 for (j = *props_remaining; j < propcount; ++j)
1574 {
1575 textprop_T op;
1576
1577 mch_memmove(&op, new_props + j * sizeof(op), sizeof(op));
1578 if ((op.tp_flags & TP_FLAG_CONT_PREV)
1579 && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type)
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001580 {
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001581 found = TRUE;
1582 op.tp_len += op.tp_col - prop.tp_col;
1583 op.tp_col = prop.tp_col;
1584 // Start/end is taken care of when deleting joined lines
1585 op.tp_flags = prop.tp_flags;
1586 mch_memmove(new_props + j * sizeof(op), &op, sizeof(op));
1587 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001588 }
1589 }
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001590 if (!found)
1591 internal_error("text property above joined line not found");
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001592 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001593 }
1594}
1595
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001596#endif // FEAT_PROP_POPUP