blob: 7947d348f772e5cb78bbd3a07f93fa7c6bbe7812 [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/*
Bram Moolenaar12034e22019-08-25 22:25:02 +020094 * Get the prop type ID of "name".
95 * When not found return zero.
96 */
97 int
98find_prop_type_id(char_u *name, buf_T *buf)
99{
100 proptype_T *pt = find_prop(name, buf);
101
102 if (pt == NULL)
103 return 0;
104 return pt->pt_id;
105}
106
107/*
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100108 * Lookup a property type by name. First in "buf" and when not found in the
109 * global types.
110 * When not found gives an error message and returns NULL.
111 */
112 static proptype_T *
113lookup_prop_type(char_u *name, buf_T *buf)
114{
115 proptype_T *type = find_prop(name, buf);
116
117 if (type == NULL)
118 type = find_prop(name, NULL);
119 if (type == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100120 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100121 return type;
122}
123
124/*
125 * Get an optional "bufnr" item from the dict in "arg".
126 * When the argument is not used or "bufnr" is not present then "buf" is
127 * unchanged.
128 * If "bufnr" is valid or not present return OK.
129 * When "arg" is not a dict or "bufnr" is invalide return FAIL.
130 */
131 static int
132get_bufnr_from_arg(typval_T *arg, buf_T **buf)
133{
134 dictitem_T *di;
135
136 if (arg->v_type != VAR_DICT)
137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100138 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100139 return FAIL;
140 }
141 if (arg->vval.v_dict == NULL)
142 return OK; // NULL dict is like an empty dict
143 di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1);
144 if (di != NULL)
145 {
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200146 *buf = get_buf_arg(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100147 if (*buf == NULL)
148 return FAIL;
149 }
150 return OK;
151}
152
153/*
154 * prop_add({lnum}, {col}, {props})
155 */
156 void
157f_prop_add(typval_T *argvars, typval_T *rettv UNUSED)
158{
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100159 linenr_T start_lnum;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100160 colnr_T start_col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100161
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100162 start_lnum = tv_get_number(&argvars[0]);
163 start_col = tv_get_number(&argvars[1]);
164 if (start_col < 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100166 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100167 return;
168 }
169 if (argvars[2].v_type != VAR_DICT)
170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100171 emsg(_(e_dictreq));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100172 return;
173 }
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200174
175 prop_add_common(start_lnum, start_col, argvars[2].vval.v_dict,
176 curbuf, &argvars[2]);
177}
178
179/*
180 * Shared between prop_add() and popup_create().
181 * "dict_arg" is the function argument of a dict containing "bufnr".
182 * it is NULL for popup_create().
183 */
184 void
185prop_add_common(
186 linenr_T start_lnum,
187 colnr_T start_col,
188 dict_T *dict,
189 buf_T *default_buf,
190 typval_T *dict_arg)
191{
192 linenr_T lnum;
193 linenr_T end_lnum;
194 colnr_T end_col;
195 char_u *type_name;
196 proptype_T *type;
197 buf_T *buf = default_buf;
198 int id = 0;
199 char_u *newtext;
200 int proplen;
201 size_t textlen;
202 char_u *props = NULL;
203 char_u *newprops;
204 textprop_T tmp_prop;
205 int i;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100206
207 if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL)
208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100209 emsg(_("E965: missing property type name"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100210 return;
211 }
Bram Moolenaar8f667172018-12-14 15:38:31 +0100212 type_name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100213
214 if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL)
215 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100216 end_lnum = dict_get_number(dict, (char_u *)"end_lnum");
217 if (end_lnum < start_lnum)
218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100219 semsg(_(e_invargval), "end_lnum");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100220 return;
221 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100222 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100223 else
224 end_lnum = start_lnum;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100225
226 if (dict_find(dict, (char_u *)"length", -1) != NULL)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100227 {
228 long length = dict_get_number(dict, (char_u *)"length");
229
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100230 if (length < 0 || end_lnum > start_lnum)
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100232 semsg(_(e_invargval), "length");
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100233 return;
234 }
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100235 end_col = start_col + length;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100236 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100237 else if (dict_find(dict, (char_u *)"end_col", -1) != NULL)
238 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100239 end_col = dict_get_number(dict, (char_u *)"end_col");
240 if (end_col <= 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100242 semsg(_(e_invargval), "end_col");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100243 return;
244 }
245 }
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100246 else if (start_lnum == end_lnum)
247 end_col = start_col;
248 else
249 end_col = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100250
251 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100252 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100253
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200254 if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100255 return;
256
257 type = lookup_prop_type(type_name, buf);
258 if (type == NULL)
259 return;
260
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100261 if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100263 semsg(_(e_invalid_lnum), (long)start_lnum);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100264 return;
265 }
266 if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count)
267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100268 semsg(_(e_invalid_lnum), (long)end_lnum);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100269 return;
270 }
271
Bram Moolenaard79eef22019-05-24 20:41:55 +0200272 if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaar45311b52019-08-13 22:27:32 +0200273 {
274 emsg(_("E275: Cannot add text property to unloaded buffer"));
275 return;
276 }
Bram Moolenaard79eef22019-05-24 20:41:55 +0200277
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100278 for (lnum = start_lnum; lnum <= end_lnum; ++lnum)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100279 {
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100280 colnr_T col; // start column
281 long length; // in bytes
282
283 // Fetch the line to get the ml_line_len field updated.
284 proplen = get_text_props(buf, lnum, &props, TRUE);
285 textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
286
287 if (lnum == start_lnum)
288 col = start_col;
289 else
290 col = 1;
291 if (col - 1 > (colnr_T)textlen)
292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100293 semsg(_(e_invalid_col), (long)start_col);
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100294 return;
295 }
296
297 if (lnum == end_lnum)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100298 length = end_col - col;
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100299 else
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100300 length = (int)textlen - col + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100301 if (length > (long)textlen)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100302 length = (int)textlen; // can include the end-of-line
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100303 if (length < 0)
304 length = 0; // zero-width property
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100305
306 // Allocate the new line with space for the new proprety.
307 newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T));
308 if (newtext == NULL)
309 return;
310 // Copy the text, including terminating NUL.
311 mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen);
312
313 // Find the index where to insert the new property.
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200314 // Since the text properties are not aligned properly when stored with
315 // the text, we need to copy them as bytes before using it as a struct.
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100316 for (i = 0; i < proplen; ++i)
317 {
318 mch_memmove(&tmp_prop, props + i * sizeof(textprop_T),
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200319 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100320 if (tmp_prop.tp_col >= col)
321 break;
322 }
323 newprops = newtext + textlen;
324 if (i > 0)
325 mch_memmove(newprops, props, sizeof(textprop_T) * i);
326
327 tmp_prop.tp_col = col;
328 tmp_prop.tp_len = length;
329 tmp_prop.tp_id = id;
330 tmp_prop.tp_type = type->pt_id;
331 tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0)
332 | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0);
333 mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar7a8d0272019-05-26 23:32:06 +0200334 sizeof(textprop_T));
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100335
336 if (i < proplen)
337 mch_memmove(newprops + (i + 1) * sizeof(textprop_T),
338 props + i * sizeof(textprop_T),
339 sizeof(textprop_T) * (proplen - i));
340
341 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
342 vim_free(buf->b_ml.ml_line_ptr);
343 buf->b_ml.ml_line_ptr = newtext;
344 buf->b_ml.ml_line_len += sizeof(textprop_T);
345 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100346 }
347
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100348 buf->b_has_textprop = TRUE; // this is never reset
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100349 redraw_buf_later(buf, NOT_VALID);
350}
351
352/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100353 * Fetch the text properties for line "lnum" in buffer "buf".
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100354 * Returns the number of text properties and, when non-zero, a pointer to the
355 * first one in "props" (note that it is not aligned, therefore the char_u
356 * pointer).
357 */
358 int
359get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change)
360{
361 char_u *text;
362 size_t textlen;
363 size_t proplen;
364
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100365 // Be quick when no text property types have been defined or the buffer,
366 // unless we are adding one.
Bram Moolenaard79eef22019-05-24 20:41:55 +0200367 if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100368 return 0;
369
370 // Fetch the line to get the ml_line_len field updated.
371 text = ml_get_buf(buf, lnum, will_change);
372 textlen = STRLEN(text) + 1;
373 proplen = buf->b_ml.ml_line_len - textlen;
374 if (proplen % sizeof(textprop_T) != 0)
375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100376 iemsg(_("E967: text property info corrupted"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100377 return 0;
378 }
379 if (proplen > 0)
380 *props = text + textlen;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100381 return (int)(proplen / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100382}
383
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100384/*
Bram Moolenaar12034e22019-08-25 22:25:02 +0200385 * Find text property "type_id" in the visible lines of window "wp".
386 * Match "id" when it is > 0.
387 * Returns FAIL when not found.
388 */
389 int
390find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop,
391 linenr_T *found_lnum)
392{
393 linenr_T lnum;
394 char_u *props;
395 int count;
396 int i;
397
398 // w_botline may not have been updated yet.
399 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count)
400 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
401 for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum)
402 {
403 count = get_text_props(wp->w_buffer, lnum, &props, FALSE);
404 for (i = 0; i < count; ++i)
405 {
406 mch_memmove(prop, props + i * sizeof(textprop_T),
407 sizeof(textprop_T));
408 if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id))
409 {
410 *found_lnum = lnum;
411 return OK;
412 }
413 }
414 }
415 return FAIL;
416}
417
418/*
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100419 * Set the text properties for line "lnum" to "props" with length "len".
420 * If "len" is zero text properties are removed, "props" is not used.
421 * Any existing text properties are dropped.
422 * Only works for the current buffer.
423 */
424 static void
425set_text_props(linenr_T lnum, char_u *props, int len)
426{
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100427 char_u *text;
428 char_u *newtext;
429 int textlen;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100430
431 text = ml_get(lnum);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +0100432 textlen = (int)STRLEN(text) + 1;
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100433 newtext = alloc(textlen + len);
434 if (newtext == NULL)
435 return;
436 mch_memmove(newtext, text, textlen);
437 if (len > 0)
438 mch_memmove(newtext + textlen, props, len);
439 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY)
440 vim_free(curbuf->b_ml.ml_line_ptr);
441 curbuf->b_ml.ml_line_ptr = newtext;
442 curbuf->b_ml.ml_line_len = textlen + len;
443 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
444}
445
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100446 static proptype_T *
447find_type_by_id(hashtab_T *ht, int id)
448{
449 long todo;
450 hashitem_T *hi;
451
452 if (ht == NULL)
453 return NULL;
454
455 // TODO: Make this faster by keeping a list of types sorted on ID and use
456 // a binary search.
457
458 todo = (long)ht->ht_used;
459 for (hi = ht->ht_array; todo > 0; ++hi)
460 {
461 if (!HASHITEM_EMPTY(hi))
462 {
463 proptype_T *prop = HI2PT(hi);
464
465 if (prop->pt_id == id)
466 return prop;
467 --todo;
468 }
469 }
470 return NULL;
471}
472
473/*
474 * Find a property type by ID in "buf" or globally.
475 * Returns NULL if not found.
476 */
477 proptype_T *
478text_prop_type_by_id(buf_T *buf, int id)
479{
480 proptype_T *type;
481
482 type = find_type_by_id(buf->b_proptypes, id);
483 if (type == NULL)
484 type = find_type_by_id(global_proptypes, id);
485 return type;
486}
487
488/*
489 * prop_clear({lnum} [, {lnum_end} [, {bufnr}]])
490 */
491 void
492f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED)
493{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100494 linenr_T start = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100495 linenr_T end = start;
496 linenr_T lnum;
497 buf_T *buf = curbuf;
498
499 if (argvars[1].v_type != VAR_UNKNOWN)
500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100501 end = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100502 if (argvars[2].v_type != VAR_UNKNOWN)
503 {
504 if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL)
505 return;
506 }
507 }
508 if (start < 1 || end < 1)
509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100510 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100511 return;
512 }
513
514 for (lnum = start; lnum <= end; ++lnum)
515 {
516 char_u *text;
517 size_t len;
518
519 if (lnum > buf->b_ml.ml_line_count)
520 break;
521 text = ml_get_buf(buf, lnum, FALSE);
522 len = STRLEN(text) + 1;
523 if ((size_t)buf->b_ml.ml_line_len > len)
524 {
525 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
526 {
527 char_u *newtext = vim_strsave(text);
528
529 // need to allocate the line now
530 if (newtext == NULL)
531 return;
532 buf->b_ml.ml_line_ptr = newtext;
533 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
534 }
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100535 buf->b_ml.ml_line_len = (int)len;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100536 }
537 }
538 redraw_buf_later(buf, NOT_VALID);
539}
540
541/*
542 * prop_list({lnum} [, {bufnr}])
543 */
544 void
545f_prop_list(typval_T *argvars, typval_T *rettv)
546{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100547 linenr_T lnum = tv_get_number(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100548 buf_T *buf = curbuf;
549
550 if (argvars[1].v_type != VAR_UNKNOWN)
551 {
552 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
553 return;
554 }
555 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100557 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100558 return;
559 }
560
561 if (rettv_list_alloc(rettv) == OK)
562 {
563 char_u *text = ml_get_buf(buf, lnum, FALSE);
564 size_t textlen = STRLEN(text) + 1;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +0100565 int count = (int)((buf->b_ml.ml_line_len - textlen)
566 / sizeof(textprop_T));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100567 int i;
568 textprop_T prop;
569 proptype_T *pt;
570
571 for (i = 0; i < count; ++i)
572 {
573 dict_T *d = dict_alloc();
574
575 if (d == NULL)
576 break;
577 mch_memmove(&prop, text + textlen + i * sizeof(textprop_T),
578 sizeof(textprop_T));
579 dict_add_number(d, "col", prop.tp_col);
580 dict_add_number(d, "length", prop.tp_len);
581 dict_add_number(d, "id", prop.tp_id);
582 dict_add_number(d, "start", !(prop.tp_flags & TP_FLAG_CONT_PREV));
583 dict_add_number(d, "end", !(prop.tp_flags & TP_FLAG_CONT_NEXT));
584 pt = text_prop_type_by_id(buf, prop.tp_type);
585 if (pt != NULL)
586 dict_add_string(d, "type", pt->pt_name);
587
588 list_append_dict(rettv->vval.v_list, d);
589 }
590 }
591}
592
593/*
594 * prop_remove({props} [, {lnum} [, {lnum_end}]])
595 */
596 void
597f_prop_remove(typval_T *argvars, typval_T *rettv)
598{
599 linenr_T start = 1;
600 linenr_T end = 0;
601 linenr_T lnum;
602 dict_T *dict;
603 buf_T *buf = curbuf;
604 dictitem_T *di;
605 int do_all = FALSE;
606 int id = -1;
607 int type_id = -1;
608
609 rettv->vval.v_number = 0;
610 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100612 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100613 return;
614 }
615
616 if (argvars[1].v_type != VAR_UNKNOWN)
617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100618 start = tv_get_number(&argvars[1]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100619 end = start;
620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100621 end = tv_get_number(&argvars[2]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100622 if (start < 1 || end < 1)
623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100624 emsg(_(e_invrange));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100625 return;
626 }
627 }
628
629 dict = argvars[0].vval.v_dict;
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200630 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
631 return;
632 if (buf->b_ml.ml_mfp == NULL)
633 return;
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100634
635 di = dict_find(dict, (char_u*)"all", -1);
636 if (di != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100637 do_all = dict_get_number(dict, (char_u *)"all");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100638
639 if (dict_find(dict, (char_u *)"id", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +0100640 id = dict_get_number(dict, (char_u *)"id");
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100641 if (dict_find(dict, (char_u *)"type", -1))
642 {
Bram Moolenaar8f667172018-12-14 15:38:31 +0100643 char_u *name = dict_get_string(dict, (char_u *)"type", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100644 proptype_T *type = lookup_prop_type(name, buf);
645
646 if (type == NULL)
647 return;
648 type_id = type->pt_id;
649 }
650 if (id == -1 && type_id == -1)
651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100652 emsg(_("E968: Need at least one of 'id' or 'type'"));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100653 return;
654 }
655
656 if (end == 0)
657 end = buf->b_ml.ml_line_count;
658 for (lnum = start; lnum <= end; ++lnum)
659 {
660 char_u *text;
661 size_t len;
662
663 if (lnum > buf->b_ml.ml_line_count)
664 break;
665 text = ml_get_buf(buf, lnum, FALSE);
666 len = STRLEN(text) + 1;
667 if ((size_t)buf->b_ml.ml_line_len > len)
668 {
669 static textprop_T textprop; // static because of alignment
670 unsigned idx;
671
672 for (idx = 0; idx < (buf->b_ml.ml_line_len - len)
673 / sizeof(textprop_T); ++idx)
674 {
675 char_u *cur_prop = buf->b_ml.ml_line_ptr + len
676 + idx * sizeof(textprop_T);
677 size_t taillen;
678
679 mch_memmove(&textprop, cur_prop, sizeof(textprop_T));
680 if (textprop.tp_id == id || textprop.tp_type == type_id)
681 {
682 if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY))
683 {
684 char_u *newptr = alloc(buf->b_ml.ml_line_len);
685
686 // need to allocate the line to be able to change it
687 if (newptr == NULL)
688 return;
689 mch_memmove(newptr, buf->b_ml.ml_line_ptr,
690 buf->b_ml.ml_line_len);
691 buf->b_ml.ml_line_ptr = newptr;
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100692 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
693
694 cur_prop = buf->b_ml.ml_line_ptr + len
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200695 + idx * sizeof(textprop_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100696 }
697
698 taillen = buf->b_ml.ml_line_len - len
699 - (idx + 1) * sizeof(textprop_T);
700 if (taillen > 0)
701 mch_memmove(cur_prop, cur_prop + sizeof(textprop_T),
702 taillen);
703 buf->b_ml.ml_line_len -= sizeof(textprop_T);
704 --idx;
705
706 ++rettv->vval.v_number;
707 if (!do_all)
708 break;
709 }
710 }
711 }
712 }
713 redraw_buf_later(buf, NOT_VALID);
714}
715
716/*
717 * Common for f_prop_type_add() and f_prop_type_change().
718 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200719 static void
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100720prop_type_set(typval_T *argvars, int add)
721{
722 char_u *name;
723 buf_T *buf = NULL;
724 dict_T *dict;
725 dictitem_T *di;
726 proptype_T *prop;
727
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100728 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100729 if (*name == NUL)
730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100731 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100732 return;
733 }
734
735 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
736 return;
737 dict = argvars[1].vval.v_dict;
738
739 prop = find_prop(name, buf);
740 if (add)
741 {
742 hashtab_T **htp;
743
744 if (prop != NULL)
745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100746 semsg(_("E969: Property type %s already defined"), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100747 return;
748 }
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200749 prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100750 if (prop == NULL)
751 return;
752 STRCPY(prop->pt_name, name);
753 prop->pt_id = ++proptype_id;
754 htp = buf == NULL ? &global_proptypes : &buf->b_proptypes;
755 if (*htp == NULL)
756 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200757 *htp = ALLOC_ONE(hashtab_T);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100758 if (*htp == NULL)
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100759 {
760 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100761 return;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100762 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100763 hash_init(*htp);
764 }
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100765 hash_add(*htp, PT2HIKEY(prop));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100766 }
767 else
768 {
769 if (prop == NULL)
770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100771 semsg(_(e_type_not_exist), name);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100772 return;
773 }
774 }
775
776 if (dict != NULL)
777 {
778 di = dict_find(dict, (char_u *)"highlight", -1);
779 if (di != NULL)
780 {
781 char_u *highlight;
782 int hl_id = 0;
783
Bram Moolenaar8f667172018-12-14 15:38:31 +0100784 highlight = dict_get_string(dict, (char_u *)"highlight", FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100785 if (highlight != NULL && *highlight != NUL)
786 hl_id = syn_name2id(highlight);
787 if (hl_id <= 0)
788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100789 semsg(_("E970: Unknown highlight group name: '%s'"),
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100790 highlight == NULL ? (char_u *)"" : highlight);
791 return;
792 }
793 prop->pt_hl_id = hl_id;
794 }
795
Bram Moolenaar58187f12019-05-05 16:33:47 +0200796 di = dict_find(dict, (char_u *)"combine", -1);
797 if (di != NULL)
798 {
799 if (tv_get_number(&di->di_tv))
800 prop->pt_flags |= PT_FLAG_COMBINE;
801 else
802 prop->pt_flags &= ~PT_FLAG_COMBINE;
803 }
804
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100805 di = dict_find(dict, (char_u *)"priority", -1);
806 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100807 prop->pt_priority = tv_get_number(&di->di_tv);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100808
809 di = dict_find(dict, (char_u *)"start_incl", -1);
810 if (di != NULL)
811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100812 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100813 prop->pt_flags |= PT_FLAG_INS_START_INCL;
814 else
815 prop->pt_flags &= ~PT_FLAG_INS_START_INCL;
816 }
817
818 di = dict_find(dict, (char_u *)"end_incl", -1);
819 if (di != NULL)
820 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100821 if (tv_get_number(&di->di_tv))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100822 prop->pt_flags |= PT_FLAG_INS_END_INCL;
823 else
824 prop->pt_flags &= ~PT_FLAG_INS_END_INCL;
825 }
826 }
827}
828
829/*
830 * prop_type_add({name}, {props})
831 */
832 void
833f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED)
834{
835 prop_type_set(argvars, TRUE);
836}
837
838/*
839 * prop_type_change({name}, {props})
840 */
841 void
842f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED)
843{
844 prop_type_set(argvars, FALSE);
845}
846
847/*
848 * prop_type_delete({name} [, {bufnr}])
849 */
850 void
851f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED)
852{
853 char_u *name;
854 buf_T *buf = NULL;
855 hashitem_T *hi;
856
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100857 name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100858 if (*name == NUL)
859 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100860 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100861 return;
862 }
863
864 if (argvars[1].v_type != VAR_UNKNOWN)
865 {
866 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
867 return;
868 }
869
870 hi = find_prop_hi(name, buf);
871 if (hi != NULL)
872 {
873 hashtab_T *ht;
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100874 proptype_T *prop = HI2PT(hi);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100875
876 if (buf == NULL)
877 ht = global_proptypes;
878 else
879 ht = buf->b_proptypes;
880 hash_remove(ht, hi);
Bram Moolenaarfb95e212018-12-14 12:18:11 +0100881 vim_free(prop);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100882 }
883}
884
885/*
886 * prop_type_get({name} [, {bufnr}])
887 */
888 void
889f_prop_type_get(typval_T *argvars, typval_T *rettv UNUSED)
890{
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100891 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100892
893 if (*name == NUL)
894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100895 emsg(_(e_invarg));
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100896 return;
897 }
898 if (rettv_dict_alloc(rettv) == OK)
899 {
900 proptype_T *prop = NULL;
901 buf_T *buf = NULL;
902
903 if (argvars[1].v_type != VAR_UNKNOWN)
904 {
905 if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL)
906 return;
907 }
908
909 prop = find_prop(name, buf);
910 if (prop != NULL)
911 {
912 dict_T *d = rettv->vval.v_dict;
913
914 if (prop->pt_hl_id > 0)
915 dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id));
916 dict_add_number(d, "priority", prop->pt_priority);
Bram Moolenaar58187f12019-05-05 16:33:47 +0200917 dict_add_number(d, "combine",
918 (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0);
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100919 dict_add_number(d, "start_incl",
920 (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0);
921 dict_add_number(d, "end_incl",
922 (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0);
923 if (buf != NULL)
924 dict_add_number(d, "bufnr", buf->b_fnum);
925 }
926 }
927}
928
929 static void
930list_types(hashtab_T *ht, list_T *l)
931{
932 long todo;
933 hashitem_T *hi;
934
935 todo = (long)ht->ht_used;
936 for (hi = ht->ht_array; todo > 0; ++hi)
937 {
938 if (!HASHITEM_EMPTY(hi))
939 {
940 proptype_T *prop = HI2PT(hi);
941
942 list_append_string(l, prop->pt_name, -1);
943 --todo;
944 }
945 }
946}
947
948/*
949 * prop_type_list([{bufnr}])
950 */
951 void
952f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED)
953{
954 buf_T *buf = NULL;
955
956 if (rettv_list_alloc(rettv) == OK)
957 {
958 if (argvars[0].v_type != VAR_UNKNOWN)
959 {
960 if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL)
961 return;
962 }
963 if (buf == NULL)
964 {
965 if (global_proptypes != NULL)
966 list_types(global_proptypes, rettv->vval.v_list);
967 }
968 else if (buf->b_proptypes != NULL)
969 list_types(buf->b_proptypes, rettv->vval.v_list);
970 }
971}
972
973/*
974 * Free all property types in "ht".
975 */
976 static void
977clear_ht_prop_types(hashtab_T *ht)
978{
979 long todo;
980 hashitem_T *hi;
981
982 if (ht == NULL)
983 return;
984
985 todo = (long)ht->ht_used;
986 for (hi = ht->ht_array; todo > 0; ++hi)
987 {
988 if (!HASHITEM_EMPTY(hi))
989 {
990 proptype_T *prop = HI2PT(hi);
991
992 vim_free(prop);
993 --todo;
994 }
995 }
996
997 hash_clear(ht);
998 vim_free(ht);
999}
1000
1001#if defined(EXITFREE) || defined(PROTO)
1002/*
Bram Moolenaarfb95e212018-12-14 12:18:11 +01001003 * Free all global property types.
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001004 */
1005 void
1006clear_global_prop_types(void)
1007{
1008 clear_ht_prop_types(global_proptypes);
1009 global_proptypes = NULL;
1010}
1011#endif
1012
1013/*
1014 * Free all property types for "buf".
1015 */
1016 void
1017clear_buf_prop_types(buf_T *buf)
1018{
1019 clear_ht_prop_types(buf->b_proptypes);
1020 buf->b_proptypes = NULL;
1021}
1022
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001023/*
1024 * Adjust the columns of text properties in line "lnum" after position "col" to
1025 * shift by "bytes_added" (can be negative).
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001026 * Note that "col" is zero-based, while tp_col is one-based.
1027 * Only for the current buffer.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001028 * "flags" can have:
1029 * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line.
1030 * APC_SUBSTITUTE: Text is replaced, not inserted.
Bram Moolenaar8055d172019-05-17 22:57:26 +02001031 * Caller is expected to check b_has_textprop and "bytes_added" being non-zero.
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001032 * Returns TRUE when props were changed.
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001033 */
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001034 int
Bram Moolenaar196d1572019-01-02 23:47:18 +01001035adjust_prop_columns(
1036 linenr_T lnum,
1037 colnr_T col,
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001038 int bytes_added,
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001039 int flags)
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001040{
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001041 int proplen;
1042 char_u *props;
1043 textprop_T tmp_prop;
1044 proptype_T *pt;
1045 int dirty = FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001046 int ri, wi;
1047 size_t textlen;
1048
1049 if (text_prop_frozen > 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001050 return FALSE;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001051
1052 proplen = get_text_props(curbuf, lnum, &props, TRUE);
1053 if (proplen == 0)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001054 return FALSE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001055 textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001056
Bram Moolenaar196d1572019-01-02 23:47:18 +01001057 wi = 0; // write index
1058 for (ri = 0; ri < proplen; ++ri)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001059 {
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001060 int start_incl;
1061
Bram Moolenaar196d1572019-01-02 23:47:18 +01001062 mch_memmove(&tmp_prop, props + ri * sizeof(textprop_T),
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001063 sizeof(textprop_T));
1064 pt = text_prop_type_by_id(curbuf, tmp_prop.tp_type);
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001065 start_incl = (flags & APC_SUBSTITUTE) ||
1066 (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL));
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001067
Bram Moolenaar196d1572019-01-02 23:47:18 +01001068 if (bytes_added > 0
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001069 && (tmp_prop.tp_col >= col + (start_incl ? 2 : 1)))
1070 {
1071 if (tmp_prop.tp_col < col + (start_incl ? 2 : 1))
1072 {
1073 tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
1074 tmp_prop.tp_col = col + 1;
1075 }
1076 else
1077 tmp_prop.tp_col += bytes_added;
1078 // Save for undo if requested and not done yet.
1079 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
1080 u_savesub(lnum);
1081 dirty = TRUE;
1082 }
1083 else if (bytes_added <= 0 && (tmp_prop.tp_col > col + 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001084 {
Bram Moolenaar8055d172019-05-17 22:57:26 +02001085 if (tmp_prop.tp_col + bytes_added < col + 1)
1086 {
1087 tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
1088 tmp_prop.tp_col = col + 1;
1089 }
1090 else
1091 tmp_prop.tp_col += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001092 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001093 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001094 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001095 dirty = TRUE;
Bram Moolenaar8055d172019-05-17 22:57:26 +02001096 if (tmp_prop.tp_len <= 0)
1097 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001098 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001099 else if (tmp_prop.tp_len > 0
1100 && tmp_prop.tp_col + tmp_prop.tp_len > col
Bram Moolenaar4614f532019-01-06 12:54:55 +01001101 + ((pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL))
Bram Moolenaar196d1572019-01-02 23:47:18 +01001102 ? 0 : 1))
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001103 {
Bram Moolenaar8055d172019-05-17 22:57:26 +02001104 int after = col - bytes_added
1105 - (tmp_prop.tp_col - 1 + tmp_prop.tp_len);
1106 if (after > 0)
1107 tmp_prop.tp_len += bytes_added + after;
1108 else
1109 tmp_prop.tp_len += bytes_added;
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001110 // Save for undo if requested and not done yet.
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001111 if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001112 u_savesub(lnum);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001113 dirty = TRUE;
Bram Moolenaar196d1572019-01-02 23:47:18 +01001114 if (tmp_prop.tp_len <= 0)
1115 continue; // drop this text property
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001116 }
Bram Moolenaar196d1572019-01-02 23:47:18 +01001117 mch_memmove(props + wi * sizeof(textprop_T), &tmp_prop,
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001118 sizeof(textprop_T));
Bram Moolenaar196d1572019-01-02 23:47:18 +01001119 ++wi;
1120 }
1121 if (dirty)
1122 {
Bram Moolenaar4614f532019-01-06 12:54:55 +01001123 colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T);
1124
1125 if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0)
1126 curbuf->b_ml.ml_line_ptr =
1127 vim_memsave(curbuf->b_ml.ml_line_ptr, newlen);
Bram Moolenaar196d1572019-01-02 23:47:18 +01001128 curbuf->b_ml.ml_flags |= ML_LINE_DIRTY;
Bram Moolenaar4614f532019-01-06 12:54:55 +01001129 curbuf->b_ml.ml_line_len = newlen;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001130 }
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001131 return dirty;
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001132}
1133
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001134/*
1135 * Adjust text properties for a line that was split in two.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001136 * "lnum_props" is the line that has the properties from before the split.
1137 * "lnum_top" is the top line.
1138 * "kept" is the number of bytes kept in the first line, while
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001139 * "deleted" is the number of bytes deleted.
1140 */
1141 void
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001142adjust_props_for_split(
1143 linenr_T lnum_props,
1144 linenr_T lnum_top,
1145 int kept,
1146 int deleted)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001147{
1148 char_u *props;
1149 int count;
1150 garray_T prevprop;
1151 garray_T nextprop;
1152 int i;
1153 int skipped = kept + deleted;
1154
1155 if (!curbuf->b_has_textprop)
1156 return;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001157
1158 // Get the text properties from "lnum_props".
1159 count = get_text_props(curbuf, lnum_props, &props, FALSE);
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001160 ga_init2(&prevprop, sizeof(textprop_T), 10);
1161 ga_init2(&nextprop, sizeof(textprop_T), 10);
1162
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001163 // Keep the relevant ones in the first line, reducing the length if needed.
1164 // Copy the ones that include the split to the second line.
1165 // Move the ones after the split to the second line.
1166 for (i = 0; i < count; ++i)
1167 {
1168 textprop_T prop;
1169 textprop_T *p;
1170
1171 // copy the prop to an aligned structure
1172 mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T));
1173
1174 if (prop.tp_col < kept && ga_grow(&prevprop, 1) == OK)
1175 {
1176 p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len;
1177 *p = prop;
1178 if (p->tp_col + p->tp_len >= kept)
1179 p->tp_len = kept - p->tp_col;
1180 ++prevprop.ga_len;
1181 }
1182
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +02001183 // Only add the property to the next line if the length is bigger than
1184 // zero.
1185 if (prop.tp_col + prop.tp_len > skipped && ga_grow(&nextprop, 1) == OK)
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001186 {
1187 p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len;
1188 *p = prop;
1189 if (p->tp_col > skipped)
1190 p->tp_col -= skipped - 1;
1191 else
1192 {
1193 p->tp_len -= skipped - p->tp_col;
1194 p->tp_col = 1;
1195 }
1196 ++nextprop.ga_len;
1197 }
1198 }
1199
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001200 set_text_props(lnum_top, prevprop.ga_data,
1201 prevprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001202 ga_clear(&prevprop);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02001203 set_text_props(lnum_top + 1, nextprop.ga_data,
1204 nextprop.ga_len * sizeof(textprop_T));
Bram Moolenaar4164bb22019-01-04 23:09:49 +01001205 ga_clear(&nextprop);
1206}
1207
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001208/*
1209 * Line "lnum" has been joined and will end up at column "col" in the new line.
1210 * "removed" bytes have been removed from the start of the line, properties
1211 * there are to be discarded.
1212 * Move the adjusted text properties to an allocated string, store it in
1213 * "prop_line" and adjust the columns.
1214 */
1215 void
1216adjust_props_for_join(
1217 linenr_T lnum,
1218 textprop_T **prop_line,
1219 int *prop_length,
1220 long col,
1221 int removed)
1222{
1223 int proplen;
1224 char_u *props;
1225 int ri;
1226 int wi = 0;
1227
1228 proplen = get_text_props(curbuf, lnum, &props, FALSE);
1229 if (proplen > 0)
1230 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001231 *prop_line = ALLOC_MULT(textprop_T, proplen);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001232 if (*prop_line != NULL)
1233 {
1234 for (ri = 0; ri < proplen; ++ri)
1235 {
1236 textprop_T *cp = *prop_line + wi;
1237
1238 mch_memmove(cp, props + ri * sizeof(textprop_T),
1239 sizeof(textprop_T));
1240 if (cp->tp_col + cp->tp_len > removed)
1241 {
1242 if (cp->tp_col > removed)
1243 cp->tp_col += col;
1244 else
1245 {
1246 // property was partly deleted, make it shorter
1247 cp->tp_len -= removed - cp->tp_col;
1248 cp->tp_col = col;
1249 }
1250 ++wi;
1251 }
1252 }
1253 }
1254 *prop_length = wi;
1255 }
1256}
1257
1258/*
1259 * After joining lines: concatenate the text and the properties of all joined
1260 * lines into one line and replace the line.
1261 */
1262 void
1263join_prop_lines(
1264 linenr_T lnum,
1265 char_u *newp,
1266 textprop_T **prop_lines,
1267 int *prop_lengths,
1268 int count)
1269{
1270 size_t proplen = 0;
1271 size_t oldproplen;
1272 char_u *props;
1273 int i;
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001274 size_t len;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001275 char_u *line;
1276 size_t l;
1277
1278 for (i = 0; i < count - 1; ++i)
1279 proplen += prop_lengths[i];
1280 if (proplen == 0)
1281 {
1282 ml_replace(lnum, newp, FALSE);
1283 return;
1284 }
1285
1286 // get existing properties of the joined line
1287 oldproplen = get_text_props(curbuf, lnum, &props, FALSE);
1288
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001289 len = STRLEN(newp) + 1;
Bram Moolenaar51e14382019-05-25 20:21:28 +02001290 line = alloc(len + (oldproplen + proplen) * sizeof(textprop_T));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001291 if (line == NULL)
1292 return;
1293 mch_memmove(line, newp, len);
Bram Moolenaar277e79a2019-06-04 19:16:29 +02001294 if (oldproplen > 0)
1295 {
1296 l = oldproplen * sizeof(textprop_T);
1297 mch_memmove(line + len, props, l);
1298 len += l;
1299 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001300
1301 for (i = 0; i < count - 1; ++i)
1302 if (prop_lines[i] != NULL)
1303 {
1304 l = prop_lengths[i] * sizeof(textprop_T);
1305 mch_memmove(line + len, prop_lines[i], l);
1306 len += l;
1307 vim_free(prop_lines[i]);
1308 }
1309
Bram Moolenaare2ad8262019-05-24 13:22:22 +02001310 ml_replace_len(lnum, line, (colnr_T)len, TRUE, FALSE);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001311 vim_free(newp);
1312 vim_free(prop_lines);
1313 vim_free(prop_lengths);
1314}
1315
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001316#endif // FEAT_TEXT_PROP