blob: 2827af437b7efcefd4b084ecb42695788e79fc41 [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar45dd07f2019-05-15 22:45:37 +020011 * Text properties implementation. See ":help text-properties".
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012 *
13 * TODO:
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010014 * - Adjust text property column and length when text is inserted/deleted.
Bram Moolenaar4164bb22019-01-04 23:09:49 +010015 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010016 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020017 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010018 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaar32aa1022019-11-02 22:54:41 +010019 * - Add an array for global_proptypes, to quickly lookup a prop type by ID
20 * - Add an array for b_proptypes, to quickly lookup a prop type by ID
Bram Moolenaarb56ac042018-12-28 23:22:40 +010021 * - Checking the text length to detect text properties is slow. Use a flag in
22 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010023 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
24 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010025 * - Perhaps have a window-local option to disable highlighting from text
26 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010027 */
28
29#include "vim.h"
30
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010031#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010032
33/*
34 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
35 * This avoids adding a pointer to the hashtable item.
36 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
37 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
38 * HI2PT() converts a hashitem pointer to a proptype pointer.
39 */
40#define PT2HIKEY(p) ((p)->pt_name)
41#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
42#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
43
44// The global text property types.
45static hashtab_T *global_proptypes = NULL;
46
47// The last used text property type ID.
48static int proptype_id = 0;
49
50static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
51static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld");
Bram Moolenaare3d31b02018-12-24 23:07:04 +010052static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010053
54/*
55 * Find a property type by name, return the hashitem.
56 * Returns NULL if the item can't be found.
57 */
58 static hashitem_T *
59find_prop_hi(char_u *name, buf_T *buf)
60{
61 hashtab_T *ht;
62 hashitem_T *hi;
63
64 if (*name == NUL)
65 return NULL;
66 if (buf == NULL)
67 ht = global_proptypes;
68 else
69 ht = buf->b_proptypes;
70
71 if (ht == NULL)
72 return NULL;
73 hi = hash_find(ht, name);
74 if (HASHITEM_EMPTY(hi))
75 return NULL;
76 return hi;
77}
78
79/*
80 * Like find_prop_hi() but return the property type.
81 */
82 static proptype_T *
83find_prop(char_u *name, buf_T *buf)
84{
85 hashitem_T *hi = find_prop_hi(name, buf);
86
87 if (hi == NULL)
88 return NULL;
89 return HI2PT(hi);
90}
91
92/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020093 * Get the prop type ID of "name".
94 * When not found return zero.
95 */
96 int
97find_prop_type_id(char_u *name, buf_T *buf)
98{
99 proptype_T *pt = find_prop(name, buf);
100
101 if (pt == NULL)
102 return 0;
103 return pt->pt_id;
104}
105
106/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100107 * Lookup a property type by name. First in "buf" and when not found in the
108 * global types.
109 * When not found gives an error message and returns NULL.
110 */
111 static proptype_T *
112lookup_prop_type(char_u *name, buf_T *buf)
113{
114 proptype_T *type = find_prop(name, buf);
115
116 if (type == NULL)
117 type = find_prop(name, NULL);
118 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100119 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120 return type;
121}
122
123/*
124 * Get an optional "bufnr" item from the dict in "arg".
125 * When the argument is not used or "bufnr" is not present then "buf" is
126 * unchanged.
127 * If "bufnr" is valid or not present return OK.
Bram Moolenaar32aa1022019-11-02 22:54:41 +0100128 * When "arg" is not a dict or "bufnr" is invalid return FAIL.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100129 */
130 static int
131get_bufnr_from_arg(typval_T *arg, buf_T **buf)
132{
133 dictitem_T *di;
134
135 if (arg->v_type != VAR_DICT)
136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100137 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100138 return FAIL;
139 }
140 if (arg->vval.v_dict == NULL)
141 return OK; // NULL dict is like an empty dict
142 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
143 if (di != NULL)
144 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200145 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100146 if (*buf == NULL)
147 return FAIL;
148 }
149 return OK;
150}
151
152/*
153 * prop_add({lnum}, {col}, {props})
154 */
155 void
156f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
157{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100158 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100159 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100160
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100161 start_lnum = tv_get_number(&argvars[0]);
162 start_col = tv_get_number(&argvars[1]);
163 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100165 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100166 return;
167 }
168 if (argvars[2].v_type != VAR_DICT)
169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100170 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100171 return;
172 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200173
174 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
175 curbuf, &argvars[2]);
176}
177
178/*
179 * Shared between prop_add() and popup_create().
180 * "dict_arg" is the function argument of a dict containing "bufnr".
181 * it is NULL for popup_create().
182 */
183 void
184prop_add_common(
185 linenr_T start_lnum,
186 colnr_T start_col,
187 dict_T *dict,
188 buf_T *default_buf,
189 typval_T *dict_arg)
190{
191 linenr_T lnum;
192 linenr_T end_lnum;
193 colnr_T end_col;
194 char_u *type_name;
195 proptype_T *type;
196 buf_T *buf = default_buf;
197 int id = 0;
198 char_u *newtext;
199 int proplen;
200 size_t textlen;
201 char_u *props = NULL;
202 char_u *newprops;
203 textprop_T tmp_prop;
204 int i;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100205
206 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100208 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100209 return;
210 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100211 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100212
213 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
214 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100215 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
216 if (end_lnum < start_lnum)
217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100218 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100219 return;
220 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100221 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100222 else
223 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100224
225 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100226 {
227 long length = dict_get_number(dict, (char_u *)"length");
228
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100229 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100231 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100232 return;
233 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100234 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100235 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100236 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
237 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100238 end_col = dict_get_number(dict, (char_u *)"end_col");
239 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100241 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100242 return;
243 }
244 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100245 else if (start_lnum == end_lnum)
246 end_col = start_col;
247 else
248 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100249
250 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100251 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100252
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200253 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100254 return;
255
256 type = lookup_prop_type(type_name, buf);
257 if (type == NULL)
258 return;
259
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100260 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100262 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100263 return;
264 }
265 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100267 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100268 return;
269 }
270
Bram Moolenaard79eef22019-05-24 20:41:55 +0200271 if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaar45311b52019-08-13 22:27:32 +0200272 {
273 emsg(_("E275: Cannot add text property to unloaded buffer"));
274 return;
275 }
Bram Moolenaard79eef22019-05-24 20:41:55 +0200276
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100277 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100278 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100279 colnr_T col; // start column
280 long length; // in bytes
281
282 // Fetch the line to get the ml_line_len field updated.
283 proplen = get_text_props(buf, lnum, &props, TRUE);
284 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
285
286 if (lnum == start_lnum)
287 col = start_col;
288 else
289 col = 1;
290 if (col - 1 > (colnr_T)textlen)
291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100292 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100293 return;
294 }
295
296 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100297 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100298 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100299 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100300 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100301 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100302 if (length < 0)
303 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100304
305 // Allocate the new line with space for the new proprety.
306 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
307 if (newtext == NULL)
308 return;
309 // Copy the text, including terminating NUL.
310 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
311
312 // Find the index where to insert the new property.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200313 // Since the text properties are not aligned properly when stored with
314 // the text, we need to copy them as bytes before using it as a struct.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100315 for (i = 0; i < proplen; ++i)
316 {
317 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200318 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100319 if (tmp_prop.tp_col >= col)
320 break;
321 }
322 newprops = newtext + textlen;
323 if (i > 0)
324 mch_memmove(newprops, props, sizeof(textprop_T) * i);
325
326 tmp_prop.tp_col = col;
327 tmp_prop.tp_len = length;
328 tmp_prop.tp_id = id;
329 tmp_prop.tp_type = type->pt_id;
330 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
331 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
332 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200333 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100334
335 if (i < proplen)
336 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
337 props + i * sizeof(textprop_T),
338 sizeof(textprop_T) * (proplen - i));
339
340 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
341 vim_free(buf->b_ml.ml_line_ptr);
342 buf->b_ml.ml_line_ptr = newtext;
343 buf->b_ml.ml_line_len += sizeof(textprop_T);
344 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100345 }
346
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100347 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100348 redraw_buf_later(buf, NOT_VALID);
349}
350
351/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100352 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100353 * Returns the number of text properties and, when non-zero, a pointer to the
354 * first one in "props" (note that it is not aligned, therefore the char_u
355 * pointer).
356 */
357 int
358get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
359{
360 char_u *text;
361 size_t textlen;
362 size_t proplen;
363
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100364 // Be quick when no text property types have been defined or the buffer,
365 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200366 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100367 return 0;
368
369 // Fetch the line to get the ml_line_len field updated.
370 text = ml_get_buf(buf, lnum, will_change);
371 textlen = STRLEN(text) + 1;
372 proplen = buf->b_ml.ml_line_len - textlen;
373 if (proplen % sizeof(textprop_T) != 0)
374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100375 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100376 return 0;
377 }
378 if (proplen > 0)
379 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100380 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100381}
382
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100383/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200384 * Find text property "type_id" in the visible lines of window "wp".
385 * Match "id" when it is > 0.
386 * Returns FAIL when not found.
387 */
388 int
389find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
390 linenr_T *found_lnum)
391{
392 linenr_T lnum;
393 char_u *props;
394 int count;
395 int i;
396
397 // w_botline may not have been updated yet.
Bram Moolenaar8e956362019-08-25 23:08:17 +0200398 validate_botline();
Bram Moolenaar12034e22019-08-25 22:25:02 +0200399 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
400 {
401 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
402 for (i = 0; i < count; ++i)
403 {
404 mch_memmove(prop, props + i * sizeof(textprop_T),
405 sizeof(textprop_T));
406 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
407 {
408 *found_lnum = lnum;
409 return OK;
410 }
411 }
412 }
413 return FAIL;
414}
415
416/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100417 * Set the text properties for line "lnum" to "props" with length "len".
418 * If "len" is zero text properties are removed, "props" is not used.
419 * Any existing text properties are dropped.
420 * Only works for the current buffer.
421 */
422 static void
423set_text_props(linenr_T lnum, char_u *props, int len)
424{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100425 char_u *text;
426 char_u *newtext;
427 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100428
429 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100430 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100431 newtext = alloc(textlen + len);
432 if (newtext == NULL)
433 return;
434 mch_memmove(newtext, text, textlen);
435 if (len > 0)
436 mch_memmove(newtext + textlen, props, len);
437 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
438 vim_free(curbuf->b_ml.ml_line_ptr);
439 curbuf->b_ml.ml_line_ptr = newtext;
440 curbuf->b_ml.ml_line_len = textlen + len;
441 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
442}
443
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100444 static proptype_T *
445find_type_by_id(hashtab_T *ht, int id)
446{
447 long todo;
448 hashitem_T *hi;
449
450 if (ht == NULL)
451 return NULL;
452
453 // TODO: Make this faster by keeping a list of types sorted on ID and use
454 // a binary search.
455
456 todo = (long)ht->ht_used;
457 for (hi = ht->ht_array; todo > 0; ++hi)
458 {
459 if (!HASHITEM_EMPTY(hi))
460 {
461 proptype_T *prop = HI2PT(hi);
462
463 if (prop->pt_id == id)
464 return prop;
465 --todo;
466 }
467 }
468 return NULL;
469}
470
471/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100472 * Fill 'dict' with text properties in 'prop'.
473 */
474 static void
475prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf)
476{
477 proptype_T *pt;
478
479 dict_add_number(dict, "col", prop->tp_col);
480 dict_add_number(dict, "length", prop->tp_len);
481 dict_add_number(dict, "id", prop->tp_id);
482 dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV));
483 dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT));
484 pt = text_prop_type_by_id(buf, prop->tp_type);
485 if (pt != NULL)
486 dict_add_string(dict, "type", pt->pt_name);
487}
488
489/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100490 * Find a property type by ID in "buf" or globally.
491 * Returns NULL if not found.
492 */
493 proptype_T *
494text_prop_type_by_id(buf_T *buf, int id)
495{
496 proptype_T *type;
497
498 type = find_type_by_id(buf->b_proptypes, id);
499 if (type == NULL)
500 type = find_type_by_id(global_proptypes, id);
501 return type;
502}
503
504/*
505 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
506 */
507 void
508f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
509{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100510 linenr_T start = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100511 linenr_T end = start;
512 linenr_T lnum;
513 buf_T *buf = curbuf;
514
515 if (argvars[1].v_type != VAR_UNKNOWN)
516 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100517 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100518 if (argvars[2].v_type != VAR_UNKNOWN)
519 {
520 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
521 return;
522 }
523 }
524 if (start < 1 || end < 1)
525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100526 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100527 return;
528 }
529
530 for (lnum = start; lnum <= end; ++lnum)
531 {
532 char_u *text;
533 size_t len;
534
535 if (lnum > buf->b_ml.ml_line_count)
536 break;
537 text = ml_get_buf(buf, lnum, FALSE);
538 len = STRLEN(text) + 1;
539 if ((size_t)buf->b_ml.ml_line_len > len)
540 {
541 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
542 {
543 char_u *newtext = vim_strsave(text);
544
545 // need to allocate the line now
546 if (newtext == NULL)
547 return;
548 buf->b_ml.ml_line_ptr = newtext;
549 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
550 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100551 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100552 }
553 }
554 redraw_buf_later(buf, NOT_VALID);
555}
556
557/*
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100558 * prop_find({props} [, {direction}])
559 */
560 void
561f_prop_find(typval_T *argvars, typval_T *rettv)
562{
563 pos_T *cursor = &curwin->w_cursor;
564 dict_T *dict;
565 buf_T *buf = curbuf;
566 dictitem_T *di;
567 int lnum_start;
568 int start_pos_has_prop = 0;
569 int seen_end = 0;
570 int id = -1;
571 int type_id = -1;
572 int skipstart = 0;
573 int lnum = -1;
574 int col = -1;
575 int dir = 1; // 1 = forward, -1 = backward
576
577 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
578 {
579 emsg(_(e_invarg));
580 return;
581 }
582 dict = argvars[0].vval.v_dict;
583
584 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
585 return;
586 if (buf->b_ml.ml_mfp == NULL)
587 return;
588
589 if (argvars[1].v_type != VAR_UNKNOWN)
590 {
591 char_u *dir_s = tv_get_string(&argvars[1]);
592
593 if (*dir_s == 'b')
594 dir = -1;
595 else if (*dir_s != 'f')
596 {
597 emsg(_(e_invarg));
598 return;
599 }
600 }
601
602 di = dict_find(dict, (char_u *)"lnum", -1);
603 if (di != NULL)
604 lnum = tv_get_number(&di->di_tv);
605
606 di = dict_find(dict, (char_u *)"col", -1);
607 if (di != NULL)
608 col = tv_get_number(&di->di_tv);
609
610 if (lnum == -1)
611 {
612 lnum = cursor->lnum;
613 col = cursor->col + 1;
614 }
615 else if (col == -1)
616 col = 1;
617
618 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
619 {
620 emsg(_(e_invrange));
621 return;
622 }
623
624 di = dict_find(dict, (char_u *)"skipstart", -1);
625 if (di != NULL)
626 skipstart = tv_get_number(&di->di_tv);
627
628 if (dict_find(dict, (char_u *)"id", -1) != NULL)
629 id = dict_get_number(dict, (char_u *)"id");
630 if (dict_find(dict, (char_u *)"type", -1))
631 {
632 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
633 proptype_T *type = lookup_prop_type(name, buf);
634
635 if (type == NULL)
636 return;
637 type_id = type->pt_id;
638 }
639 if (id == -1 && type_id == -1)
640 {
641 emsg(_("E968: Need at least one of 'id' or 'type'"));
642 return;
643 }
644
645 lnum_start = lnum;
646
647 if (rettv_dict_alloc(rettv) == FAIL)
648 return;
649
650 while (1)
651 {
652 char_u *text = ml_get_buf(buf, lnum, FALSE);
653 size_t textlen = STRLEN(text) + 1;
654 int count = (int)((buf->b_ml.ml_line_len - textlen)
655 / sizeof(textprop_T));
656 int i;
657 textprop_T prop;
658 int prop_start;
659 int prop_end;
660
661 for (i = 0; i < count; ++i)
662 {
663 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
664 sizeof(textprop_T));
665
666 if (prop.tp_id == id || prop.tp_type == type_id)
667 {
668 // Check if the starting position has text props.
669 if (lnum_start == lnum)
670 {
671 if (col >= prop.tp_col
672 && (col <= prop.tp_col + prop.tp_len-1))
673 start_pos_has_prop = 1;
674 }
675 else
676 {
677 // Not at the first line of the search so adjust col to
678 // indicate that we're continuing from prev/next line.
679 if (dir < 0)
680 col = buf->b_ml.ml_line_len;
681 else
682 col = 1;
683 }
684
685 prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV);
686 prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT);
687 if (!prop_start && prop_end && dir > 0)
688 seen_end = 1;
689
690 // Skip lines without the start flag.
691 if (!prop_start)
692 {
693 // Always search backwards for start when search started
694 // on a prop and we're not skipping.
695 if (start_pos_has_prop && !skipstart)
696 dir = -1;
697 break;
698 }
699
700 // If skipstart is true, skip the prop at start pos (even if
701 // continued from another line).
702 if (start_pos_has_prop && skipstart && !seen_end)
703 {
704 start_pos_has_prop = 0;
705 break;
706 }
707
708 if (dir < 0)
709 {
710 if (col < prop.tp_col)
711 break;
712 }
713 else
714 {
715 if (col > prop.tp_col + prop.tp_len-1)
716 break;
717 }
718
719 prop_fill_dict(rettv->vval.v_dict, &prop, buf);
720 dict_add_number(rettv->vval.v_dict, "lnum", lnum);
721
722 return;
723 }
724 }
725
726 if (dir > 0)
727 {
728 if (lnum >= buf->b_ml.ml_line_count)
729 break;
730 lnum++;
731 }
732 else
733 {
734 if (lnum <= 1)
735 break;
736 lnum--;
737 }
738 }
739}
740
741/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100742 * prop_list({lnum} [, {bufnr}])
743 */
744 void
745f_prop_list(typval_T *argvars, typval_T *rettv)
746{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100747 linenr_T lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100748 buf_T *buf = curbuf;
749
750 if (argvars[1].v_type != VAR_UNKNOWN)
751 {
752 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
753 return;
754 }
755 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
756 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100757 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100758 return;
759 }
760
761 if (rettv_list_alloc(rettv) == OK)
762 {
763 char_u *text = ml_get_buf(buf, lnum, FALSE);
764 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100765 int count = (int)((buf->b_ml.ml_line_len - textlen)
766 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100767 int i;
768 textprop_T prop;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100769
770 for (i = 0; i < count; ++i)
771 {
772 dict_T *d = dict_alloc();
773
774 if (d == NULL)
775 break;
776 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
777 sizeof(textprop_T));
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100778 prop_fill_dict(d, &prop, buf);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100779 list_append_dict(rettv->vval.v_list, d);
780 }
781 }
782}
783
784/*
785 * prop_remove({props} [, {lnum} [, {lnum_end}]])
786 */
787 void
788f_prop_remove(typval_T *argvars, typval_T *rettv)
789{
790 linenr_T start = 1;
791 linenr_T end = 0;
792 linenr_T lnum;
793 dict_T *dict;
794 buf_T *buf = curbuf;
795 dictitem_T *di;
796 int do_all = FALSE;
797 int id = -1;
798 int type_id = -1;
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100799 int both = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100800
801 rettv->vval.v_number = 0;
802 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
803 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100804 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100805 return;
806 }
807
808 if (argvars[1].v_type != VAR_UNKNOWN)
809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100810 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100811 end = start;
812 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100813 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100814 if (start < 1 || end < 1)
815 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100816 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100817 return;
818 }
819 }
820
821 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200822 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
823 return;
824 if (buf->b_ml.ml_mfp == NULL)
825 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100826
827 di = dict_find(dict, (char_u*)"all", -1);
828 if (di != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100829 do_all = dict_get_number(dict, (char_u *)"all");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100830
831 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100832 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100833 if (dict_find(dict, (char_u *)"type", -1))
834 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100835 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100836 proptype_T *type = lookup_prop_type(name, buf);
837
838 if (type == NULL)
839 return;
840 type_id = type->pt_id;
841 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100842 if (dict_find(dict, (char_u *)"both", -1) != NULL)
843 both = dict_get_number(dict, (char_u *)"both");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100844 if (id == -1 && type_id == -1)
845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100847 return;
848 }
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100849 if (both && (id == -1 || type_id == -1))
850 {
851 emsg(_("E860: Need 'id' and 'type' with 'both'"));
852 return;
853 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100854
855 if (end == 0)
856 end = buf->b_ml.ml_line_count;
857 for (lnum = start; lnum <= end; ++lnum)
858 {
859 char_u *text;
860 size_t len;
861
862 if (lnum > buf->b_ml.ml_line_count)
863 break;
864 text = ml_get_buf(buf, lnum, FALSE);
865 len = STRLEN(text) + 1;
866 if ((size_t)buf->b_ml.ml_line_len > len)
867 {
868 static textprop_T textprop; // static because of alignment
869 unsigned idx;
870
871 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
872 / sizeof(textprop_T); ++idx)
873 {
874 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
875 + idx * sizeof(textprop_T);
876 size_t taillen;
877
878 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100879 if (both ? textprop.tp_id == id && textprop.tp_type == type_id
880 : textprop.tp_id == id || textprop.tp_type == type_id)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100881 {
882 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
883 {
884 char_u *newptr = alloc(buf->b_ml.ml_line_len);
885
886 // need to allocate the line to be able to change it
887 if (newptr == NULL)
888 return;
889 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
890 buf->b_ml.ml_line_len);
891 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100892 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
893
894 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200895 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100896 }
897
898 taillen = buf->b_ml.ml_line_len - len
899 - (idx + 1) * sizeof(textprop_T);
900 if (taillen > 0)
901 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
902 taillen);
903 buf->b_ml.ml_line_len -= sizeof(textprop_T);
904 --idx;
905
906 ++rettv->vval.v_number;
907 if (!do_all)
908 break;
909 }
910 }
911 }
912 }
913 redraw_buf_later(buf, NOT_VALID);
914}
915
916/*
917 * Common for f_prop_type_add() and f_prop_type_change().
918 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200919 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100920prop_type_set(typval_T *argvars, int add)
921{
922 char_u *name;
923 buf_T *buf = NULL;
924 dict_T *dict;
925 dictitem_T *di;
926 proptype_T *prop;
927
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100928 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100929 if (*name == NUL)
930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100931 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100932 return;
933 }
934
935 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
936 return;
937 dict = argvars[1].vval.v_dict;
938
939 prop = find_prop(name, buf);
940 if (add)
941 {
942 hashtab_T **htp;
943
944 if (prop != NULL)
945 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100946 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100947 return;
948 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200949 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100950 if (prop == NULL)
951 return;
952 STRCPY(prop->pt_name, name);
953 prop->pt_id = ++proptype_id;
Bram Moolenaar0743ef92019-11-13 16:37:31 +0100954 prop->pt_flags = PT_FLAG_COMBINE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100955 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
956 if (*htp == NULL)
957 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200958 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100959 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100960 {
961 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100962 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100963 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100964 hash_init(*htp);
965 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100966 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100967 }
968 else
969 {
970 if (prop == NULL)
971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100973 return;
974 }
975 }
976
977 if (dict != NULL)
978 {
979 di = dict_find(dict, (char_u *)"highlight", -1);
980 if (di != NULL)
981 {
982 char_u *highlight;
983 int hl_id = 0;
984
Bram Moolenaar8f667172018-12-14 15:38:31 +0100985 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100986 if (highlight != NULL && *highlight != NUL)
987 hl_id = syn_name2id(highlight);
988 if (hl_id <= 0)
989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100990 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100991 highlight == NULL ? (char_u *)"" : highlight);
992 return;
993 }
994 prop->pt_hl_id = hl_id;
995 }
996
Bram Moolenaar58187f12019-05-05 16:33:47 +0200997 di = dict_find(dict, (char_u *)"combine", -1);
998 if (di != NULL)
999 {
1000 if (tv_get_number(&di->di_tv))
1001 prop->pt_flags |= PT_FLAG_COMBINE;
1002 else
1003 prop->pt_flags &= ~PT_FLAG_COMBINE;
1004 }
1005
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001006 di = dict_find(dict, (char_u *)"priority", -1);
1007 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001008 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001009
1010 di = dict_find(dict, (char_u *)"start_incl", -1);
1011 if (di != NULL)
1012 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001013 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001014 prop->pt_flags |= PT_FLAG_INS_START_INCL;
1015 else
1016 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
1017 }
1018
1019 di = dict_find(dict, (char_u *)"end_incl", -1);
1020 if (di != NULL)
1021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001022 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001023 prop->pt_flags |= PT_FLAG_INS_END_INCL;
1024 else
1025 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
1026 }
1027 }
1028}
1029
1030/*
1031 * prop_type_add({name}, {props})
1032 */
1033 void
1034f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
1035{
1036 prop_type_set(argvars, TRUE);
1037}
1038
1039/*
1040 * prop_type_change({name}, {props})
1041 */
1042 void
1043f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
1044{
1045 prop_type_set(argvars, FALSE);
1046}
1047
1048/*
1049 * prop_type_delete({name} [, {bufnr}])
1050 */
1051 void
1052f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
1053{
1054 char_u *name;
1055 buf_T *buf = NULL;
1056 hashitem_T *hi;
1057
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001058 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001059 if (*name == NUL)
1060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001061 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001062 return;
1063 }
1064
1065 if (argvars[1].v_type != VAR_UNKNOWN)
1066 {
1067 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1068 return;
1069 }
1070
1071 hi = find_prop_hi(name, buf);
1072 if (hi != NULL)
1073 {
1074 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001075 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001076
1077 if (buf == NULL)
1078 ht = global_proptypes;
1079 else
1080 ht = buf->b_proptypes;
1081 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001082 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001083 }
1084}
1085
1086/*
1087 * prop_type_get({name} [, {bufnr}])
1088 */
1089 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01001090f_prop_type_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001091{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001092 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001093
1094 if (*name == NUL)
1095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001096 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001097 return;
1098 }
1099 if (rettv_dict_alloc(rettv) == OK)
1100 {
1101 proptype_T *prop = NULL;
1102 buf_T *buf = NULL;
1103
1104 if (argvars[1].v_type != VAR_UNKNOWN)
1105 {
1106 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
1107 return;
1108 }
1109
1110 prop = find_prop(name, buf);
1111 if (prop != NULL)
1112 {
1113 dict_T *d = rettv->vval.v_dict;
1114
1115 if (prop->pt_hl_id > 0)
1116 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
1117 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +02001118 dict_add_number(d, "combine",
1119 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001120 dict_add_number(d, "start_incl",
1121 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
1122 dict_add_number(d, "end_incl",
1123 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
1124 if (buf != NULL)
1125 dict_add_number(d, "bufnr", buf->b_fnum);
1126 }
1127 }
1128}
1129
1130 static void
1131list_types(hashtab_T *ht, list_T *l)
1132{
1133 long todo;
1134 hashitem_T *hi;
1135
1136 todo = (long)ht->ht_used;
1137 for (hi = ht->ht_array; todo > 0; ++hi)
1138 {
1139 if (!HASHITEM_EMPTY(hi))
1140 {
1141 proptype_T *prop = HI2PT(hi);
1142
1143 list_append_string(l, prop->pt_name, -1);
1144 --todo;
1145 }
1146 }
1147}
1148
1149/*
1150 * prop_type_list([{bufnr}])
1151 */
1152 void
1153f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
1154{
1155 buf_T *buf = NULL;
1156
1157 if (rettv_list_alloc(rettv) == OK)
1158 {
1159 if (argvars[0].v_type != VAR_UNKNOWN)
1160 {
1161 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
1162 return;
1163 }
1164 if (buf == NULL)
1165 {
1166 if (global_proptypes != NULL)
1167 list_types(global_proptypes, rettv->vval.v_list);
1168 }
1169 else if (buf->b_proptypes != NULL)
1170 list_types(buf->b_proptypes, rettv->vval.v_list);
1171 }
1172}
1173
1174/*
1175 * Free all property types in "ht".
1176 */
1177 static void
1178clear_ht_prop_types(hashtab_T *ht)
1179{
1180 long todo;
1181 hashitem_T *hi;
1182
1183 if (ht == NULL)
1184 return;
1185
1186 todo = (long)ht->ht_used;
1187 for (hi = ht->ht_array; todo > 0; ++hi)
1188 {
1189 if (!HASHITEM_EMPTY(hi))
1190 {
1191 proptype_T *prop = HI2PT(hi);
1192
1193 vim_free(prop);
1194 --todo;
1195 }
1196 }
1197
1198 hash_clear(ht);
1199 vim_free(ht);
1200}
1201
1202#if defined(EXITFREE) || defined(PROTO)
1203/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001204 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001205 */
1206 void
1207clear_global_prop_types(void)
1208{
1209 clear_ht_prop_types(global_proptypes);
1210 global_proptypes = NULL;
1211}
1212#endif
1213
1214/*
1215 * Free all property types for "buf".
1216 */
1217 void
1218clear_buf_prop_types(buf_T *buf)
1219{
1220 clear_ht_prop_types(buf->b_proptypes);
1221 buf->b_proptypes = NULL;
1222}
1223
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001224/*
1225 * Adjust the columns of text properties in line "lnum" after position "col" to
1226 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001227 * Note that "col" is zero-based, while tp_col is one-based.
1228 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001229 * "flags" can have:
1230 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1231 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001232 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001233 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001234 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001235 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001236adjust_prop_columns(
1237 linenr_T lnum,
1238 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001239 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001240 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001241{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001242 int proplen;
1243 char_u *props;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001244 proptype_T *pt;
1245 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001246 int ri, wi;
1247 size_t textlen;
1248
1249 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001250 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001251
1252 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1253 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001254 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001255 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001256
Bram Moolenaar196d1572019-01-02 23:47:18 +01001257 wi = 0; // write index
1258 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001259 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001260 textprop_T prop;
1261 int start_incl, end_incl;
1262 int can_drop;
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001263
Bram Moolenaar12f20032020-02-26 22:06:00 +01001264 mch_memmove(&prop, props + ri * sizeof(textprop_T), sizeof(textprop_T));
1265 pt = text_prop_type_by_id(curbuf, prop.tp_type);
1266 start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL))
1267 || (flags & APC_SUBSTITUTE);
1268 end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL));
1269 // Do not drop zero-width props if they later can increase in size
1270 can_drop = !(start_incl || end_incl);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001271
Bram Moolenaar12f20032020-02-26 22:06:00 +01001272 if (bytes_added > 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001273 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001274 if (col + 1 <= prop.tp_col
1275 - (start_incl || (prop.tp_len == 0 && end_incl)))
1276 {
1277 // Change is entirely before the text property: Only shift
1278 prop.tp_col += bytes_added;
1279 // Save for undo if requested and not done yet.
1280 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
1281 u_savesub(lnum);
1282 dirty = TRUE;
1283 }
1284 else if (col + 1 < prop.tp_col + prop.tp_len + end_incl)
1285 {
1286 // Insertion was inside text property
1287 prop.tp_len += bytes_added;
1288 // Save for undo if requested and not done yet.
1289 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
1290 u_savesub(lnum);
1291 dirty = TRUE;
1292 }
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001293 }
Bram Moolenaar12f20032020-02-26 22:06:00 +01001294 else if (prop.tp_col > col + 1)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001295 {
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001296 int len_changed = FALSE;
1297
Bram Moolenaar12f20032020-02-26 22:06:00 +01001298 if (prop.tp_col + bytes_added < col + 1)
Bram Moolenaar8055d172019-05-17 22:57:26 +02001299 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001300 prop.tp_len += (prop.tp_col - 1 - col) + bytes_added;
1301 prop.tp_col = col + 1;
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001302 len_changed = TRUE;
Bram Moolenaar8055d172019-05-17 22:57:26 +02001303 }
1304 else
Bram Moolenaar12f20032020-02-26 22:06:00 +01001305 prop.tp_col += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001306 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001307 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001308 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001309 dirty = TRUE;
Bram Moolenaar12f20032020-02-26 22:06:00 +01001310 if (len_changed && prop.tp_len <= 0)
1311 {
1312 prop.tp_len = 0;
1313 if (can_drop)
1314 continue; // drop this text property
1315 }
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001316 }
Bram Moolenaar12f20032020-02-26 22:06:00 +01001317 else if (prop.tp_len > 0 && prop.tp_col + prop.tp_len > col)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001318 {
Bram Moolenaar12f20032020-02-26 22:06:00 +01001319 int after = col - bytes_added - (prop.tp_col - 1 + prop.tp_len);
1320
Bram Moolenaar8055d172019-05-17 22:57:26 +02001321 if (after > 0)
Bram Moolenaar12f20032020-02-26 22:06:00 +01001322 prop.tp_len += bytes_added + after;
Bram Moolenaar8055d172019-05-17 22:57:26 +02001323 else
Bram Moolenaar12f20032020-02-26 22:06:00 +01001324 prop.tp_len += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001325 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001326 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001327 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001328 dirty = TRUE;
Bram Moolenaar12f20032020-02-26 22:06:00 +01001329 if (prop.tp_len <= 0 && can_drop)
Bram Moolenaar196d1572019-01-02 23:47:18 +01001330 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001331 }
Bram Moolenaar12f20032020-02-26 22:06:00 +01001332
1333 mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001334 ++wi;
1335 }
1336 if (dirty)
1337 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001338 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1339
1340 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1341 curbuf->b_ml.ml_line_ptr =
1342 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001343 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001344 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001345 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001346 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001347}
1348
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001349/*
1350 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001351 * "lnum_props" is the line that has the properties from before the split.
1352 * "lnum_top" is the top line.
1353 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001354 * "deleted" is the number of bytes deleted.
1355 */
1356 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001357adjust_props_for_split(
1358 linenr_T lnum_props,
1359 linenr_T lnum_top,
1360 int kept,
1361 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001362{
1363 char_u *props;
1364 int count;
1365 garray_T prevprop;
1366 garray_T nextprop;
1367 int i;
1368 int skipped = kept + deleted;
1369
1370 if (!curbuf->b_has_textprop)
1371 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001372
1373 // Get the text properties from "lnum_props".
1374 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001375 ga_init2(&prevprop, sizeof(textprop_T), 10);
1376 ga_init2(&nextprop, sizeof(textprop_T), 10);
1377
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001378 // Keep the relevant ones in the first line, reducing the length if needed.
1379 // Copy the ones that include the split to the second line.
1380 // Move the ones after the split to the second line.
1381 for (i = 0; i < count; ++i)
1382 {
1383 textprop_T prop;
1384 textprop_T *p;
1385
1386 // copy the prop to an aligned structure
1387 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1388
1389 if (prop.tp_col < kept && ga_grow(&prevprop, 1) == OK)
1390 {
1391 p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1392 *p = prop;
1393 if (p->tp_col + p->tp_len >= kept)
1394 p->tp_len = kept - p->tp_col;
1395 ++prevprop.ga_len;
1396 }
1397
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001398 // Only add the property to the next line if the length is bigger than
1399 // zero.
1400 if (prop.tp_col + prop.tp_len > skipped && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001401 {
1402 p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
1403 *p = prop;
1404 if (p->tp_col > skipped)
1405 p->tp_col -= skipped - 1;
1406 else
1407 {
1408 p->tp_len -= skipped - p->tp_col;
1409 p->tp_col = 1;
1410 }
1411 ++nextprop.ga_len;
1412 }
1413 }
1414
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001415 set_text_props(lnum_top, prevprop.ga_data,
1416 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001417 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001418 set_text_props(lnum_top + 1, nextprop.ga_data,
1419 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001420 ga_clear(&nextprop);
1421}
1422
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001423/*
1424 * Line "lnum" has been joined and will end up at column "col" in the new line.
1425 * "removed" bytes have been removed from the start of the line, properties
1426 * there are to be discarded.
1427 * Move the adjusted text properties to an allocated string, store it in
1428 * "prop_line" and adjust the columns.
1429 */
1430 void
1431adjust_props_for_join(
1432 linenr_T lnum,
1433 textprop_T **prop_line,
1434 int *prop_length,
1435 long col,
1436 int removed)
1437{
1438 int proplen;
1439 char_u *props;
1440 int ri;
1441 int wi = 0;
1442
1443 proplen = get_text_props(curbuf, lnum, &props, FALSE);
1444 if (proplen > 0)
1445 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001446 *prop_line = ALLOC_MULT(textprop_T, proplen);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001447 if (*prop_line != NULL)
1448 {
1449 for (ri = 0; ri < proplen; ++ri)
1450 {
1451 textprop_T *cp = *prop_line + wi;
1452
1453 mch_memmove(cp, props + ri * sizeof(textprop_T),
1454 sizeof(textprop_T));
1455 if (cp->tp_col + cp->tp_len > removed)
1456 {
1457 if (cp->tp_col > removed)
1458 cp->tp_col += col;
1459 else
1460 {
1461 // property was partly deleted, make it shorter
1462 cp->tp_len -= removed - cp->tp_col;
1463 cp->tp_col = col;
1464 }
1465 ++wi;
1466 }
1467 }
1468 }
1469 *prop_length = wi;
1470 }
1471}
1472
1473/*
1474 * After joining lines: concatenate the text and the properties of all joined
1475 * lines into one line and replace the line.
1476 */
1477 void
1478join_prop_lines(
1479 linenr_T lnum,
1480 char_u *newp,
1481 textprop_T **prop_lines,
1482 int *prop_lengths,
1483 int count)
1484{
1485 size_t proplen = 0;
1486 size_t oldproplen;
1487 char_u *props;
1488 int i;
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001489 size_t len;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001490 char_u *line;
1491 size_t l;
1492
1493 for (i = 0; i < count - 1; ++i)
1494 proplen += prop_lengths[i];
1495 if (proplen == 0)
1496 {
1497 ml_replace(lnum, newp, FALSE);
1498 return;
1499 }
1500
1501 // get existing properties of the joined line
1502 oldproplen = get_text_props(curbuf, lnum, &props, FALSE);
1503
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001504 len = STRLEN(newp) + 1;
Bram Moolenaar51e14382019-05-25 20:21:28 +02001505 line = alloc(len + (oldproplen + proplen) * sizeof(textprop_T));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001506 if (line == NULL)
1507 return;
1508 mch_memmove(line, newp, len);
Bram Moolenaar277e79a2019-06-04 19:16:29 +02001509 if (oldproplen > 0)
1510 {
1511 l = oldproplen * sizeof(textprop_T);
1512 mch_memmove(line + len, props, l);
1513 len += l;
1514 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001515
1516 for (i = 0; i < count - 1; ++i)
1517 if (prop_lines[i] != NULL)
1518 {
1519 l = prop_lengths[i] * sizeof(textprop_T);
1520 mch_memmove(line + len, prop_lines[i], l);
1521 len += l;
1522 vim_free(prop_lines[i]);
1523 }
1524
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001525 ml_replace_len(lnum, line, (colnr_T)len, TRUE, FALSE);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001526 vim_free(newp);
1527 vim_free(prop_lines);
1528 vim_free(prop_lengths);
1529}
1530
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001531#endif // FEAT_PROP_POPUP