blob: 93787f3c46259bd104b444b5306ecb2b0df67426 [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 Moolenaard79eef22019-05-24 20:41:55 +020015 * -> :substitute with multiple matches, issue #4427
Bram Moolenaar4164bb22019-01-04 23:09:49 +010016 * -> a :substitute with a multi-line match
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010017 * -> search for changed_bytes() from misc1.c
Bram Moolenaar80e737c2019-05-17 19:56:34 +020018 * -> search for mark_col_adjust()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010019 * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV?
Bram Moolenaarb56ac042018-12-28 23:22:40 +010020 * - Add an arrray for global_proptypes, to quickly lookup a prop type by ID
21 * - Add an arrray for b_proptypes, to quickly lookup a prop type by ID
22 * - Checking the text length to detect text properties is slow. Use a flag in
23 * the index, like DB_MARKED?
Bram Moolenaarb413d2e2018-12-25 23:15:46 +010024 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
25 * into account.
Bram Moolenaarc6663882019-02-22 19:14:54 +010026 * - Perhaps have a window-local option to disable highlighting from text
27 * properties?
Bram Moolenaar98aefe72018-12-13 22:20:09 +010028 */
29
30#include "vim.h"
31
32#if defined(FEAT_TEXT_PROP) || defined(PROTO)
33
34/*
35 * In a hashtable item "hi_key" points to "pt_name" in a proptype_T.
36 * This avoids adding a pointer to the hashtable item.
37 * PT2HIKEY() converts a proptype pointer to a hashitem key pointer.
38 * HIKEY2PT() converts a hashitem key pointer to a proptype pointer.
39 * HI2PT() converts a hashitem pointer to a proptype pointer.
40 */
41#define PT2HIKEY(p) ((p)->pt_name)
42#define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name)))
43#define HI2PT(hi) HIKEY2PT((hi)->hi_key)
44
45// The global text property types.
46static hashtab_T *global_proptypes = NULL;
47
48// The last used text property type ID.
49static int proptype_id = 0;
50
51static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist");
52static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld");
Bram Moolenaare3d31b02018-12-24 23:07:04 +010053static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld");
Bram Moolenaar98aefe72018-12-13 22:20:09 +010054
55/*
56 * Find a property type by name, return the hashitem.
57 * Returns NULL if the item can't be found.
58 */
59 static hashitem_T *
60find_prop_hi(char_u *name, buf_T *buf)
61{
62 hashtab_T *ht;
63 hashitem_T *hi;
64
65 if (*name == NUL)
66 return NULL;
67 if (buf == NULL)
68 ht = global_proptypes;
69 else
70 ht = buf->b_proptypes;
71
72 if (ht == NULL)
73 return NULL;
74 hi = hash_find(ht, name);
75 if (HASHITEM_EMPTY(hi))
76 return NULL;
77 return hi;
78}
79
80/*
81 * Like find_prop_hi() but return the property type.
82 */
83 static proptype_T *
84find_prop(char_u *name, buf_T *buf)
85{
86 hashitem_T *hi = find_prop_hi(name, buf);
87
88 if (hi == NULL)
89 return NULL;
90 return HI2PT(hi);
91}
92
93/*
94 * Lookup a property type by name. First in "buf" and when not found in the
95 * global types.
96 * When not found gives an error message and returns NULL.
97 */
98 static proptype_T *
99lookup_prop_type(char_u *name, buf_T *buf)
100{
101 proptype_T *type = find_prop(name, buf);
102
103 if (type == NULL)
104 type = find_prop(name, NULL);
105 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100106 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100107 return type;
108}
109
110/*
111 * Get an optional "bufnr" item from the dict in "arg".
112 * When the argument is not used or "bufnr" is not present then "buf" is
113 * unchanged.
114 * If "bufnr" is valid or not present return OK.
115 * When "arg" is not a dict or "bufnr" is invalide return FAIL.
116 */
117 static int
118get_bufnr_from_arg(typval_T *arg, buf_T **buf)
119{
120 dictitem_T *di;
121
122 if (arg->v_type != VAR_DICT)
123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100124 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100125 return FAIL;
126 }
127 if (arg->vval.v_dict == NULL)
128 return OK; // NULL dict is like an empty dict
129 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
130 if (di != NULL)
131 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200132 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100133 if (*buf == NULL)
134 return FAIL;
135 }
136 return OK;
137}
138
139/*
140 * prop_add({lnum}, {col}, {props})
141 */
142 void
143f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
144{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100145 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100146 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100147
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100148 start_lnum = tv_get_number(&argvars[0]);
149 start_col = tv_get_number(&argvars[1]);
150 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100152 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100153 return;
154 }
155 if (argvars[2].v_type != VAR_DICT)
156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100157 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100158 return;
159 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200160
161 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
162 curbuf, &argvars[2]);
163}
164
165/*
166 * Shared between prop_add() and popup_create().
167 * "dict_arg" is the function argument of a dict containing "bufnr".
168 * it is NULL for popup_create().
169 */
170 void
171prop_add_common(
172 linenr_T start_lnum,
173 colnr_T start_col,
174 dict_T *dict,
175 buf_T *default_buf,
176 typval_T *dict_arg)
177{
178 linenr_T lnum;
179 linenr_T end_lnum;
180 colnr_T end_col;
181 char_u *type_name;
182 proptype_T *type;
183 buf_T *buf = default_buf;
184 int id = 0;
185 char_u *newtext;
186 int proplen;
187 size_t textlen;
188 char_u *props = NULL;
189 char_u *newprops;
190 textprop_T tmp_prop;
191 int i;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100192
193 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100195 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100196 return;
197 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100198 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100199
200 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
201 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100202 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
203 if (end_lnum < start_lnum)
204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100205 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100206 return;
207 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100208 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100209 else
210 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100211
212 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100213 {
214 long length = dict_get_number(dict, (char_u *)"length");
215
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100216 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100218 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100219 return;
220 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100221 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100222 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100223 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
224 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100225 end_col = dict_get_number(dict, (char_u *)"end_col");
226 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100228 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100229 return;
230 }
231 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100232 else if (start_lnum == end_lnum)
233 end_col = start_col;
234 else
235 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100236
237 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100238 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100239
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200240 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100241 return;
242
243 type = lookup_prop_type(type_name, buf);
244 if (type == NULL)
245 return;
246
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100247 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100249 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100250 return;
251 }
252 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
253 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100254 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100255 return;
256 }
257
Bram Moolenaard79eef22019-05-24 20:41:55 +0200258 if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaar45311b52019-08-13 22:27:32 +0200259 {
260 emsg(_("E275: Cannot add text property to unloaded buffer"));
261 return;
262 }
Bram Moolenaard79eef22019-05-24 20:41:55 +0200263
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100264 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100265 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100266 colnr_T col; // start column
267 long length; // in bytes
268
269 // Fetch the line to get the ml_line_len field updated.
270 proplen = get_text_props(buf, lnum, &props, TRUE);
271 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
272
273 if (lnum == start_lnum)
274 col = start_col;
275 else
276 col = 1;
277 if (col - 1 > (colnr_T)textlen)
278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100279 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100280 return;
281 }
282
283 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100284 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100285 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100286 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100287 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100288 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100289 if (length < 0)
290 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100291
292 // Allocate the new line with space for the new proprety.
293 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
294 if (newtext == NULL)
295 return;
296 // Copy the text, including terminating NUL.
297 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
298
299 // Find the index where to insert the new property.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200300 // Since the text properties are not aligned properly when stored with
301 // the text, we need to copy them as bytes before using it as a struct.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100302 for (i = 0; i < proplen; ++i)
303 {
304 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200305 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100306 if (tmp_prop.tp_col >= col)
307 break;
308 }
309 newprops = newtext + textlen;
310 if (i > 0)
311 mch_memmove(newprops, props, sizeof(textprop_T) * i);
312
313 tmp_prop.tp_col = col;
314 tmp_prop.tp_len = length;
315 tmp_prop.tp_id = id;
316 tmp_prop.tp_type = type->pt_id;
317 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
318 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
319 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200320 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100321
322 if (i < proplen)
323 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
324 props + i * sizeof(textprop_T),
325 sizeof(textprop_T) * (proplen - i));
326
327 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
328 vim_free(buf->b_ml.ml_line_ptr);
329 buf->b_ml.ml_line_ptr = newtext;
330 buf->b_ml.ml_line_len += sizeof(textprop_T);
331 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100332 }
333
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100334 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100335 redraw_buf_later(buf, NOT_VALID);
336}
337
338/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100339 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100340 * Returns the number of text properties and, when non-zero, a pointer to the
341 * first one in "props" (note that it is not aligned, therefore the char_u
342 * pointer).
343 */
344 int
345get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
346{
347 char_u *text;
348 size_t textlen;
349 size_t proplen;
350
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100351 // Be quick when no text property types have been defined or the buffer,
352 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200353 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100354 return 0;
355
356 // Fetch the line to get the ml_line_len field updated.
357 text = ml_get_buf(buf, lnum, will_change);
358 textlen = STRLEN(text) + 1;
359 proplen = buf->b_ml.ml_line_len - textlen;
360 if (proplen % sizeof(textprop_T) != 0)
361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100362 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100363 return 0;
364 }
365 if (proplen > 0)
366 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100367 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100368}
369
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100370/*
371 * Set the text properties for line "lnum" to "props" with length "len".
372 * If "len" is zero text properties are removed, "props" is not used.
373 * Any existing text properties are dropped.
374 * Only works for the current buffer.
375 */
376 static void
377set_text_props(linenr_T lnum, char_u *props, int len)
378{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100379 char_u *text;
380 char_u *newtext;
381 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100382
383 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100384 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100385 newtext = alloc(textlen + len);
386 if (newtext == NULL)
387 return;
388 mch_memmove(newtext, text, textlen);
389 if (len > 0)
390 mch_memmove(newtext + textlen, props, len);
391 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
392 vim_free(curbuf->b_ml.ml_line_ptr);
393 curbuf->b_ml.ml_line_ptr = newtext;
394 curbuf->b_ml.ml_line_len = textlen + len;
395 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
396}
397
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100398 static proptype_T *
399find_type_by_id(hashtab_T *ht, int id)
400{
401 long todo;
402 hashitem_T *hi;
403
404 if (ht == NULL)
405 return NULL;
406
407 // TODO: Make this faster by keeping a list of types sorted on ID and use
408 // a binary search.
409
410 todo = (long)ht->ht_used;
411 for (hi = ht->ht_array; todo > 0; ++hi)
412 {
413 if (!HASHITEM_EMPTY(hi))
414 {
415 proptype_T *prop = HI2PT(hi);
416
417 if (prop->pt_id == id)
418 return prop;
419 --todo;
420 }
421 }
422 return NULL;
423}
424
425/*
426 * Find a property type by ID in "buf" or globally.
427 * Returns NULL if not found.
428 */
429 proptype_T *
430text_prop_type_by_id(buf_T *buf, int id)
431{
432 proptype_T *type;
433
434 type = find_type_by_id(buf->b_proptypes, id);
435 if (type == NULL)
436 type = find_type_by_id(global_proptypes, id);
437 return type;
438}
439
440/*
441 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
442 */
443 void
444f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
445{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100446 linenr_T start = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100447 linenr_T end = start;
448 linenr_T lnum;
449 buf_T *buf = curbuf;
450
451 if (argvars[1].v_type != VAR_UNKNOWN)
452 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100453 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100454 if (argvars[2].v_type != VAR_UNKNOWN)
455 {
456 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
457 return;
458 }
459 }
460 if (start < 1 || end < 1)
461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100462 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100463 return;
464 }
465
466 for (lnum = start; lnum <= end; ++lnum)
467 {
468 char_u *text;
469 size_t len;
470
471 if (lnum > buf->b_ml.ml_line_count)
472 break;
473 text = ml_get_buf(buf, lnum, FALSE);
474 len = STRLEN(text) + 1;
475 if ((size_t)buf->b_ml.ml_line_len > len)
476 {
477 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
478 {
479 char_u *newtext = vim_strsave(text);
480
481 // need to allocate the line now
482 if (newtext == NULL)
483 return;
484 buf->b_ml.ml_line_ptr = newtext;
485 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
486 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100487 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100488 }
489 }
490 redraw_buf_later(buf, NOT_VALID);
491}
492
493/*
494 * prop_list({lnum} [, {bufnr}])
495 */
496 void
497f_prop_list(typval_T *argvars, typval_T *rettv)
498{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100499 linenr_T lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100500 buf_T *buf = curbuf;
501
502 if (argvars[1].v_type != VAR_UNKNOWN)
503 {
504 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
505 return;
506 }
507 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100509 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100510 return;
511 }
512
513 if (rettv_list_alloc(rettv) == OK)
514 {
515 char_u *text = ml_get_buf(buf, lnum, FALSE);
516 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100517 int count = (int)((buf->b_ml.ml_line_len - textlen)
518 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100519 int i;
520 textprop_T prop;
521 proptype_T *pt;
522
523 for (i = 0; i < count; ++i)
524 {
525 dict_T *d = dict_alloc();
526
527 if (d == NULL)
528 break;
529 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
530 sizeof(textprop_T));
531 dict_add_number(d, "col", prop.tp_col);
532 dict_add_number(d, "length", prop.tp_len);
533 dict_add_number(d, "id", prop.tp_id);
534 dict_add_number(d, "start", !(prop.tp_flags & TP_FLAG_CONT_PREV));
535 dict_add_number(d, "end", !(prop.tp_flags & TP_FLAG_CONT_NEXT));
536 pt = text_prop_type_by_id(buf, prop.tp_type);
537 if (pt != NULL)
538 dict_add_string(d, "type", pt->pt_name);
539
540 list_append_dict(rettv->vval.v_list, d);
541 }
542 }
543}
544
545/*
546 * prop_remove({props} [, {lnum} [, {lnum_end}]])
547 */
548 void
549f_prop_remove(typval_T *argvars, typval_T *rettv)
550{
551 linenr_T start = 1;
552 linenr_T end = 0;
553 linenr_T lnum;
554 dict_T *dict;
555 buf_T *buf = curbuf;
556 dictitem_T *di;
557 int do_all = FALSE;
558 int id = -1;
559 int type_id = -1;
560
561 rettv->vval.v_number = 0;
562 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100564 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100565 return;
566 }
567
568 if (argvars[1].v_type != VAR_UNKNOWN)
569 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100570 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100571 end = start;
572 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100573 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100574 if (start < 1 || end < 1)
575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100576 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100577 return;
578 }
579 }
580
581 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200582 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
583 return;
584 if (buf->b_ml.ml_mfp == NULL)
585 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100586
587 di = dict_find(dict, (char_u*)"all", -1);
588 if (di != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100589 do_all = dict_get_number(dict, (char_u *)"all");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100590
591 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100592 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100593 if (dict_find(dict, (char_u *)"type", -1))
594 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100595 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100596 proptype_T *type = lookup_prop_type(name, buf);
597
598 if (type == NULL)
599 return;
600 type_id = type->pt_id;
601 }
602 if (id == -1 && type_id == -1)
603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100604 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100605 return;
606 }
607
608 if (end == 0)
609 end = buf->b_ml.ml_line_count;
610 for (lnum = start; lnum <= end; ++lnum)
611 {
612 char_u *text;
613 size_t len;
614
615 if (lnum > buf->b_ml.ml_line_count)
616 break;
617 text = ml_get_buf(buf, lnum, FALSE);
618 len = STRLEN(text) + 1;
619 if ((size_t)buf->b_ml.ml_line_len > len)
620 {
621 static textprop_T textprop; // static because of alignment
622 unsigned idx;
623
624 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
625 / sizeof(textprop_T); ++idx)
626 {
627 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
628 + idx * sizeof(textprop_T);
629 size_t taillen;
630
631 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
632 if (textprop.tp_id == id || textprop.tp_type == type_id)
633 {
634 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
635 {
636 char_u *newptr = alloc(buf->b_ml.ml_line_len);
637
638 // need to allocate the line to be able to change it
639 if (newptr == NULL)
640 return;
641 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
642 buf->b_ml.ml_line_len);
643 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100644 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
645
646 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200647 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100648 }
649
650 taillen = buf->b_ml.ml_line_len - len
651 - (idx + 1) * sizeof(textprop_T);
652 if (taillen > 0)
653 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
654 taillen);
655 buf->b_ml.ml_line_len -= sizeof(textprop_T);
656 --idx;
657
658 ++rettv->vval.v_number;
659 if (!do_all)
660 break;
661 }
662 }
663 }
664 }
665 redraw_buf_later(buf, NOT_VALID);
666}
667
668/*
669 * Common for f_prop_type_add() and f_prop_type_change().
670 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200671 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100672prop_type_set(typval_T *argvars, int add)
673{
674 char_u *name;
675 buf_T *buf = NULL;
676 dict_T *dict;
677 dictitem_T *di;
678 proptype_T *prop;
679
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100680 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100681 if (*name == NUL)
682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100683 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100684 return;
685 }
686
687 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
688 return;
689 dict = argvars[1].vval.v_dict;
690
691 prop = find_prop(name, buf);
692 if (add)
693 {
694 hashtab_T **htp;
695
696 if (prop != NULL)
697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100698 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100699 return;
700 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200701 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100702 if (prop == NULL)
703 return;
704 STRCPY(prop->pt_name, name);
705 prop->pt_id = ++proptype_id;
706 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
707 if (*htp == NULL)
708 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200709 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100710 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100711 {
712 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100713 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100714 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100715 hash_init(*htp);
716 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100717 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100718 }
719 else
720 {
721 if (prop == NULL)
722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100723 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100724 return;
725 }
726 }
727
728 if (dict != NULL)
729 {
730 di = dict_find(dict, (char_u *)"highlight", -1);
731 if (di != NULL)
732 {
733 char_u *highlight;
734 int hl_id = 0;
735
Bram Moolenaar8f667172018-12-14 15:38:31 +0100736 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100737 if (highlight != NULL && *highlight != NUL)
738 hl_id = syn_name2id(highlight);
739 if (hl_id <= 0)
740 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100741 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100742 highlight == NULL ? (char_u *)"" : highlight);
743 return;
744 }
745 prop->pt_hl_id = hl_id;
746 }
747
Bram Moolenaar58187f12019-05-05 16:33:47 +0200748 di = dict_find(dict, (char_u *)"combine", -1);
749 if (di != NULL)
750 {
751 if (tv_get_number(&di->di_tv))
752 prop->pt_flags |= PT_FLAG_COMBINE;
753 else
754 prop->pt_flags &= ~PT_FLAG_COMBINE;
755 }
756
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100757 di = dict_find(dict, (char_u *)"priority", -1);
758 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100759 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100760
761 di = dict_find(dict, (char_u *)"start_incl", -1);
762 if (di != NULL)
763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100764 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100765 prop->pt_flags |= PT_FLAG_INS_START_INCL;
766 else
767 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
768 }
769
770 di = dict_find(dict, (char_u *)"end_incl", -1);
771 if (di != NULL)
772 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100773 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100774 prop->pt_flags |= PT_FLAG_INS_END_INCL;
775 else
776 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
777 }
778 }
779}
780
781/*
782 * prop_type_add({name}, {props})
783 */
784 void
785f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
786{
787 prop_type_set(argvars, TRUE);
788}
789
790/*
791 * prop_type_change({name}, {props})
792 */
793 void
794f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
795{
796 prop_type_set(argvars, FALSE);
797}
798
799/*
800 * prop_type_delete({name} [, {bufnr}])
801 */
802 void
803f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
804{
805 char_u *name;
806 buf_T *buf = NULL;
807 hashitem_T *hi;
808
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100809 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100810 if (*name == NUL)
811 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100812 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100813 return;
814 }
815
816 if (argvars[1].v_type != VAR_UNKNOWN)
817 {
818 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
819 return;
820 }
821
822 hi = find_prop_hi(name, buf);
823 if (hi != NULL)
824 {
825 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100826 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100827
828 if (buf == NULL)
829 ht = global_proptypes;
830 else
831 ht = buf->b_proptypes;
832 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100833 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100834 }
835}
836
837/*
838 * prop_type_get({name} [, {bufnr}])
839 */
840 void
841f_prop_type_get(typval_T *argvars, typval_T *rettv UNUSED)
842{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100843 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100844
845 if (*name == NUL)
846 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100848 return;
849 }
850 if (rettv_dict_alloc(rettv) == OK)
851 {
852 proptype_T *prop = NULL;
853 buf_T *buf = NULL;
854
855 if (argvars[1].v_type != VAR_UNKNOWN)
856 {
857 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
858 return;
859 }
860
861 prop = find_prop(name, buf);
862 if (prop != NULL)
863 {
864 dict_T *d = rettv->vval.v_dict;
865
866 if (prop->pt_hl_id > 0)
867 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
868 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +0200869 dict_add_number(d, "combine",
870 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100871 dict_add_number(d, "start_incl",
872 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
873 dict_add_number(d, "end_incl",
874 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
875 if (buf != NULL)
876 dict_add_number(d, "bufnr", buf->b_fnum);
877 }
878 }
879}
880
881 static void
882list_types(hashtab_T *ht, list_T *l)
883{
884 long todo;
885 hashitem_T *hi;
886
887 todo = (long)ht->ht_used;
888 for (hi = ht->ht_array; todo > 0; ++hi)
889 {
890 if (!HASHITEM_EMPTY(hi))
891 {
892 proptype_T *prop = HI2PT(hi);
893
894 list_append_string(l, prop->pt_name, -1);
895 --todo;
896 }
897 }
898}
899
900/*
901 * prop_type_list([{bufnr}])
902 */
903 void
904f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
905{
906 buf_T *buf = NULL;
907
908 if (rettv_list_alloc(rettv) == OK)
909 {
910 if (argvars[0].v_type != VAR_UNKNOWN)
911 {
912 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
913 return;
914 }
915 if (buf == NULL)
916 {
917 if (global_proptypes != NULL)
918 list_types(global_proptypes, rettv->vval.v_list);
919 }
920 else if (buf->b_proptypes != NULL)
921 list_types(buf->b_proptypes, rettv->vval.v_list);
922 }
923}
924
925/*
926 * Free all property types in "ht".
927 */
928 static void
929clear_ht_prop_types(hashtab_T *ht)
930{
931 long todo;
932 hashitem_T *hi;
933
934 if (ht == NULL)
935 return;
936
937 todo = (long)ht->ht_used;
938 for (hi = ht->ht_array; todo > 0; ++hi)
939 {
940 if (!HASHITEM_EMPTY(hi))
941 {
942 proptype_T *prop = HI2PT(hi);
943
944 vim_free(prop);
945 --todo;
946 }
947 }
948
949 hash_clear(ht);
950 vim_free(ht);
951}
952
953#if defined(EXITFREE) || defined(PROTO)
954/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100955 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100956 */
957 void
958clear_global_prop_types(void)
959{
960 clear_ht_prop_types(global_proptypes);
961 global_proptypes = NULL;
962}
963#endif
964
965/*
966 * Free all property types for "buf".
967 */
968 void
969clear_buf_prop_types(buf_T *buf)
970{
971 clear_ht_prop_types(buf->b_proptypes);
972 buf->b_proptypes = NULL;
973}
974
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100975/*
976 * Adjust the columns of text properties in line "lnum" after position "col" to
977 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100978 * Note that "col" is zero-based, while tp_col is one-based.
979 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200980 * "flags" can have:
981 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
982 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +0200983 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200984 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100985 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200986 int
Bram Moolenaar196d1572019-01-02 23:47:18 +0100987adjust_prop_columns(
988 linenr_T lnum,
989 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200990 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200991 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100992{
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100993 int proplen;
994 char_u *props;
995 textprop_T tmp_prop;
996 proptype_T *pt;
997 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +0100998 int ri, wi;
999 size_t textlen;
1000
1001 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001002 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001003
1004 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1005 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001006 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001007 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001008
Bram Moolenaar196d1572019-01-02 23:47:18 +01001009 wi = 0; // write index
1010 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001011 {
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001012 int start_incl;
1013
Bram Moolenaar196d1572019-01-02 23:47:18 +01001014 mch_memmove(&tmp_prop, props + ri * sizeof(textprop_T),
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001015 sizeof(textprop_T));
1016 pt = text_prop_type_by_id(curbuf, tmp_prop.tp_type);
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001017 start_incl = (flags & APC_SUBSTITUTE) ||
1018 (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001019
Bram Moolenaar196d1572019-01-02 23:47:18 +01001020 if (bytes_added > 0
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001021 && (tmp_prop.tp_col >= col + (start_incl ? 2 : 1)))
1022 {
1023 if (tmp_prop.tp_col < col + (start_incl ? 2 : 1))
1024 {
1025 tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
1026 tmp_prop.tp_col = col + 1;
1027 }
1028 else
1029 tmp_prop.tp_col += bytes_added;
1030 // Save for undo if requested and not done yet.
1031 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
1032 u_savesub(lnum);
1033 dirty = TRUE;
1034 }
1035 else if (bytes_added <= 0 && (tmp_prop.tp_col > col + 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001036 {
Bram Moolenaar8055d172019-05-17 22:57:26 +02001037 if (tmp_prop.tp_col + bytes_added < col + 1)
1038 {
1039 tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
1040 tmp_prop.tp_col = col + 1;
1041 }
1042 else
1043 tmp_prop.tp_col += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001044 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001045 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001046 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001047 dirty = TRUE;
Bram Moolenaar8055d172019-05-17 22:57:26 +02001048 if (tmp_prop.tp_len <= 0)
1049 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001050 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001051 else if (tmp_prop.tp_len > 0
1052 && tmp_prop.tp_col + tmp_prop.tp_len > col
Bram Moolenaar4614f532019-01-06 12:54:55 +01001053 + ((pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
Bram Moolenaar196d1572019-01-02 23:47:18 +01001054 ? 0 : 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001055 {
Bram Moolenaar8055d172019-05-17 22:57:26 +02001056 int after = col - bytes_added
1057 - (tmp_prop.tp_col - 1 + tmp_prop.tp_len);
1058 if (after > 0)
1059 tmp_prop.tp_len += bytes_added + after;
1060 else
1061 tmp_prop.tp_len += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001062 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001063 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001064 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001065 dirty = TRUE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001066 if (tmp_prop.tp_len <= 0)
1067 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001068 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001069 mch_memmove(props + wi * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001070 sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001071 ++wi;
1072 }
1073 if (dirty)
1074 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001075 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1076
1077 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1078 curbuf->b_ml.ml_line_ptr =
1079 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001080 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001081 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001082 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001083 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001084}
1085
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001086/*
1087 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001088 * "lnum_props" is the line that has the properties from before the split.
1089 * "lnum_top" is the top line.
1090 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001091 * "deleted" is the number of bytes deleted.
1092 */
1093 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001094adjust_props_for_split(
1095 linenr_T lnum_props,
1096 linenr_T lnum_top,
1097 int kept,
1098 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001099{
1100 char_u *props;
1101 int count;
1102 garray_T prevprop;
1103 garray_T nextprop;
1104 int i;
1105 int skipped = kept + deleted;
1106
1107 if (!curbuf->b_has_textprop)
1108 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001109
1110 // Get the text properties from "lnum_props".
1111 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001112 ga_init2(&prevprop, sizeof(textprop_T), 10);
1113 ga_init2(&nextprop, sizeof(textprop_T), 10);
1114
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001115 // Keep the relevant ones in the first line, reducing the length if needed.
1116 // Copy the ones that include the split to the second line.
1117 // Move the ones after the split to the second line.
1118 for (i = 0; i < count; ++i)
1119 {
1120 textprop_T prop;
1121 textprop_T *p;
1122
1123 // copy the prop to an aligned structure
1124 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1125
1126 if (prop.tp_col < kept && ga_grow(&prevprop, 1) == OK)
1127 {
1128 p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1129 *p = prop;
1130 if (p->tp_col + p->tp_len >= kept)
1131 p->tp_len = kept - p->tp_col;
1132 ++prevprop.ga_len;
1133 }
1134
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001135 // Only add the property to the next line if the length is bigger than
1136 // zero.
1137 if (prop.tp_col + prop.tp_len > skipped && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001138 {
1139 p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
1140 *p = prop;
1141 if (p->tp_col > skipped)
1142 p->tp_col -= skipped - 1;
1143 else
1144 {
1145 p->tp_len -= skipped - p->tp_col;
1146 p->tp_col = 1;
1147 }
1148 ++nextprop.ga_len;
1149 }
1150 }
1151
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001152 set_text_props(lnum_top, prevprop.ga_data,
1153 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001154 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001155 set_text_props(lnum_top + 1, nextprop.ga_data,
1156 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001157 ga_clear(&nextprop);
1158}
1159
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001160/*
1161 * Line "lnum" has been joined and will end up at column "col" in the new line.
1162 * "removed" bytes have been removed from the start of the line, properties
1163 * there are to be discarded.
1164 * Move the adjusted text properties to an allocated string, store it in
1165 * "prop_line" and adjust the columns.
1166 */
1167 void
1168adjust_props_for_join(
1169 linenr_T lnum,
1170 textprop_T **prop_line,
1171 int *prop_length,
1172 long col,
1173 int removed)
1174{
1175 int proplen;
1176 char_u *props;
1177 int ri;
1178 int wi = 0;
1179
1180 proplen = get_text_props(curbuf, lnum, &props, FALSE);
1181 if (proplen > 0)
1182 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001183 *prop_line = ALLOC_MULT(textprop_T, proplen);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001184 if (*prop_line != NULL)
1185 {
1186 for (ri = 0; ri < proplen; ++ri)
1187 {
1188 textprop_T *cp = *prop_line + wi;
1189
1190 mch_memmove(cp, props + ri * sizeof(textprop_T),
1191 sizeof(textprop_T));
1192 if (cp->tp_col + cp->tp_len > removed)
1193 {
1194 if (cp->tp_col > removed)
1195 cp->tp_col += col;
1196 else
1197 {
1198 // property was partly deleted, make it shorter
1199 cp->tp_len -= removed - cp->tp_col;
1200 cp->tp_col = col;
1201 }
1202 ++wi;
1203 }
1204 }
1205 }
1206 *prop_length = wi;
1207 }
1208}
1209
1210/*
1211 * After joining lines: concatenate the text and the properties of all joined
1212 * lines into one line and replace the line.
1213 */
1214 void
1215join_prop_lines(
1216 linenr_T lnum,
1217 char_u *newp,
1218 textprop_T **prop_lines,
1219 int *prop_lengths,
1220 int count)
1221{
1222 size_t proplen = 0;
1223 size_t oldproplen;
1224 char_u *props;
1225 int i;
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001226 size_t len;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001227 char_u *line;
1228 size_t l;
1229
1230 for (i = 0; i < count - 1; ++i)
1231 proplen += prop_lengths[i];
1232 if (proplen == 0)
1233 {
1234 ml_replace(lnum, newp, FALSE);
1235 return;
1236 }
1237
1238 // get existing properties of the joined line
1239 oldproplen = get_text_props(curbuf, lnum, &props, FALSE);
1240
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001241 len = STRLEN(newp) + 1;
Bram Moolenaar51e14382019-05-25 20:21:28 +02001242 line = alloc(len + (oldproplen + proplen) * sizeof(textprop_T));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001243 if (line == NULL)
1244 return;
1245 mch_memmove(line, newp, len);
Bram Moolenaar277e79a2019-06-04 19:16:29 +02001246 if (oldproplen > 0)
1247 {
1248 l = oldproplen * sizeof(textprop_T);
1249 mch_memmove(line + len, props, l);
1250 len += l;
1251 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001252
1253 for (i = 0; i < count - 1; ++i)
1254 if (prop_lines[i] != NULL)
1255 {
1256 l = prop_lengths[i] * sizeof(textprop_T);
1257 mch_memmove(line + len, prop_lines[i], l);
1258 len += l;
1259 vim_free(prop_lines[i]);
1260 }
1261
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001262 ml_replace_len(lnum, line, (colnr_T)len, TRUE, FALSE);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001263 vim_free(newp);
1264 vim_free(prop_lines);
1265 vim_free(prop_lengths);
1266}
1267
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001268#endif // FEAT_TEXT_PROP