Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1 | /* 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 Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 11 | * Text properties implementation. See ":help text-properties". |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 12 | * |
| 13 | * TODO: |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 14 | * - Adjust text property column and length when text is inserted/deleted. |
Bram Moolenaar | d79eef2 | 2019-05-24 20:41:55 +0200 | [diff] [blame^] | 15 | * -> :substitute with multiple matches, issue #4427 |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 16 | * -> a :substitute with a multi-line match |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 17 | * -> search for changed_bytes() from misc1.c |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 18 | * -> search for mark_col_adjust() |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 19 | * - Perhaps we only need TP_FLAG_CONT_NEXT and can drop TP_FLAG_CONT_PREV? |
Bram Moolenaar | b56ac04 | 2018-12-28 23:22:40 +0100 | [diff] [blame] | 20 | * - 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 Moolenaar | b413d2e | 2018-12-25 23:15:46 +0100 | [diff] [blame] | 24 | * - Also test line2byte() with many lines, so that ml_updatechunk() is taken |
| 25 | * into account. |
Bram Moolenaar | c666388 | 2019-02-22 19:14:54 +0100 | [diff] [blame] | 26 | * - Perhaps have a window-local option to disable highlighting from text |
| 27 | * properties? |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 28 | */ |
| 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. |
| 46 | static hashtab_T *global_proptypes = NULL; |
| 47 | |
| 48 | // The last used text property type ID. |
| 49 | static int proptype_id = 0; |
| 50 | |
| 51 | static char_u e_type_not_exist[] = N_("E971: Property type %s does not exist"); |
| 52 | static char_u e_invalid_col[] = N_("E964: Invalid column number: %ld"); |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 53 | static char_u e_invalid_lnum[] = N_("E966: Invalid line number: %ld"); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 54 | |
| 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 * |
| 60 | find_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 * |
| 84 | find_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 * |
| 99 | lookup_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 Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 106 | semsg(_(e_type_not_exist), name); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 107 | 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 |
| 118 | get_bufnr_from_arg(typval_T *arg, buf_T **buf) |
| 119 | { |
| 120 | dictitem_T *di; |
| 121 | |
| 122 | if (arg->v_type != VAR_DICT) |
| 123 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 124 | emsg(_(e_dictreq)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 125 | 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 Moolenaar | f2d79fa | 2019-01-03 22:19:27 +0100 | [diff] [blame] | 132 | *buf = tv_get_buf(&di->di_tv, FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 133 | if (*buf == NULL) |
| 134 | return FAIL; |
| 135 | } |
| 136 | return OK; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * prop_add({lnum}, {col}, {props}) |
| 141 | */ |
| 142 | void |
| 143 | f_prop_add(typval_T *argvars, typval_T *rettv UNUSED) |
| 144 | { |
| 145 | linenr_T lnum; |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 146 | linenr_T start_lnum; |
| 147 | linenr_T end_lnum; |
| 148 | colnr_T start_col; |
| 149 | colnr_T end_col; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 150 | dict_T *dict; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 151 | char_u *type_name; |
| 152 | proptype_T *type; |
| 153 | buf_T *buf = curbuf; |
| 154 | int id = 0; |
| 155 | char_u *newtext; |
| 156 | int proplen; |
| 157 | size_t textlen; |
Bram Moolenaar | c666388 | 2019-02-22 19:14:54 +0100 | [diff] [blame] | 158 | char_u *props = NULL; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 159 | char_u *newprops; |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 160 | textprop_T tmp_prop; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 161 | int i; |
| 162 | |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 163 | start_lnum = tv_get_number(&argvars[0]); |
| 164 | start_col = tv_get_number(&argvars[1]); |
| 165 | if (start_col < 1) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 166 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 167 | semsg(_(e_invalid_col), (long)start_col); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | if (argvars[2].v_type != VAR_DICT) |
| 171 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 172 | emsg(_(e_dictreq)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | dict = argvars[2].vval.v_dict; |
| 176 | |
| 177 | if (dict == NULL || dict_find(dict, (char_u *)"type", -1) == NULL) |
| 178 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 179 | emsg(_("E965: missing property type name")); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 180 | return; |
| 181 | } |
Bram Moolenaar | 8f66717 | 2018-12-14 15:38:31 +0100 | [diff] [blame] | 182 | type_name = dict_get_string(dict, (char_u *)"type", FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 183 | |
| 184 | if (dict_find(dict, (char_u *)"end_lnum", -1) != NULL) |
| 185 | { |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 186 | end_lnum = dict_get_number(dict, (char_u *)"end_lnum"); |
| 187 | if (end_lnum < start_lnum) |
| 188 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 189 | semsg(_(e_invargval), "end_lnum"); |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 190 | return; |
| 191 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 192 | } |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 193 | else |
| 194 | end_lnum = start_lnum; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 195 | |
| 196 | if (dict_find(dict, (char_u *)"length", -1) != NULL) |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 197 | { |
| 198 | long length = dict_get_number(dict, (char_u *)"length"); |
| 199 | |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 200 | if (length < 0 || end_lnum > start_lnum) |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 201 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 202 | semsg(_(e_invargval), "length"); |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 203 | return; |
| 204 | } |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 205 | end_col = start_col + length; |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 206 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 207 | else if (dict_find(dict, (char_u *)"end_col", -1) != NULL) |
| 208 | { |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 209 | end_col = dict_get_number(dict, (char_u *)"end_col"); |
| 210 | if (end_col <= 0) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 211 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 212 | semsg(_(e_invargval), "end_col"); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | } |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 216 | else if (start_lnum == end_lnum) |
| 217 | end_col = start_col; |
| 218 | else |
| 219 | end_col = 1; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 220 | |
| 221 | if (dict_find(dict, (char_u *)"id", -1) != NULL) |
Bram Moolenaar | 8f66717 | 2018-12-14 15:38:31 +0100 | [diff] [blame] | 222 | id = dict_get_number(dict, (char_u *)"id"); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 223 | |
| 224 | if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL) |
| 225 | return; |
| 226 | |
| 227 | type = lookup_prop_type(type_name, buf); |
| 228 | if (type == NULL) |
| 229 | return; |
| 230 | |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 231 | if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 232 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 233 | semsg(_(e_invalid_lnum), (long)start_lnum); |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count) |
| 237 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 238 | semsg(_(e_invalid_lnum), (long)end_lnum); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
Bram Moolenaar | d79eef2 | 2019-05-24 20:41:55 +0200 | [diff] [blame^] | 242 | if (buf->b_ml.ml_mfp == NULL) |
| 243 | ml_open(buf); |
| 244 | |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 245 | for (lnum = start_lnum; lnum <= end_lnum; ++lnum) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 246 | { |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 247 | colnr_T col; // start column |
| 248 | long length; // in bytes |
| 249 | |
| 250 | // Fetch the line to get the ml_line_len field updated. |
| 251 | proplen = get_text_props(buf, lnum, &props, TRUE); |
| 252 | textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T); |
| 253 | |
| 254 | if (lnum == start_lnum) |
| 255 | col = start_col; |
| 256 | else |
| 257 | col = 1; |
| 258 | if (col - 1 > (colnr_T)textlen) |
| 259 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 260 | semsg(_(e_invalid_col), (long)start_col); |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | |
| 264 | if (lnum == end_lnum) |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 265 | length = end_col - col; |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 266 | else |
Bram Moolenaar | 4b7214e | 2019-01-03 21:55:32 +0100 | [diff] [blame] | 267 | length = (int)textlen - col + 1; |
Bram Moolenaar | b413d2e | 2018-12-25 23:15:46 +0100 | [diff] [blame] | 268 | if (length > (long)textlen) |
Bram Moolenaar | 4b7214e | 2019-01-03 21:55:32 +0100 | [diff] [blame] | 269 | length = (int)textlen; // can include the end-of-line |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 270 | if (length < 0) |
| 271 | length = 0; // zero-width property |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 272 | |
| 273 | // Allocate the new line with space for the new proprety. |
| 274 | newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T)); |
| 275 | if (newtext == NULL) |
| 276 | return; |
| 277 | // Copy the text, including terminating NUL. |
| 278 | mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen); |
| 279 | |
| 280 | // Find the index where to insert the new property. |
| 281 | // Since the text properties are not aligned properly when stored with the |
| 282 | // text, we need to copy them as bytes before using it as a struct. |
| 283 | for (i = 0; i < proplen; ++i) |
| 284 | { |
| 285 | mch_memmove(&tmp_prop, props + i * sizeof(textprop_T), |
| 286 | sizeof(textprop_T)); |
| 287 | if (tmp_prop.tp_col >= col) |
| 288 | break; |
| 289 | } |
| 290 | newprops = newtext + textlen; |
| 291 | if (i > 0) |
| 292 | mch_memmove(newprops, props, sizeof(textprop_T) * i); |
| 293 | |
| 294 | tmp_prop.tp_col = col; |
| 295 | tmp_prop.tp_len = length; |
| 296 | tmp_prop.tp_id = id; |
| 297 | tmp_prop.tp_type = type->pt_id; |
| 298 | tmp_prop.tp_flags = (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0) |
| 299 | | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0); |
| 300 | mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop, |
| 301 | sizeof(textprop_T)); |
| 302 | |
| 303 | if (i < proplen) |
| 304 | mch_memmove(newprops + (i + 1) * sizeof(textprop_T), |
| 305 | props + i * sizeof(textprop_T), |
| 306 | sizeof(textprop_T) * (proplen - i)); |
| 307 | |
| 308 | if (buf->b_ml.ml_flags & ML_LINE_DIRTY) |
| 309 | vim_free(buf->b_ml.ml_line_ptr); |
| 310 | buf->b_ml.ml_line_ptr = newtext; |
| 311 | buf->b_ml.ml_line_len += sizeof(textprop_T); |
| 312 | buf->b_ml.ml_flags |= ML_LINE_DIRTY; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Bram Moolenaar | b413d2e | 2018-12-25 23:15:46 +0100 | [diff] [blame] | 315 | buf->b_has_textprop = TRUE; // this is never reset |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 316 | redraw_buf_later(buf, NOT_VALID); |
| 317 | } |
| 318 | |
| 319 | /* |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 320 | * Fetch the text properties for line "lnum" in buffer "buf". |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 321 | * Returns the number of text properties and, when non-zero, a pointer to the |
| 322 | * first one in "props" (note that it is not aligned, therefore the char_u |
| 323 | * pointer). |
| 324 | */ |
| 325 | int |
| 326 | get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change) |
| 327 | { |
| 328 | char_u *text; |
| 329 | size_t textlen; |
| 330 | size_t proplen; |
| 331 | |
Bram Moolenaar | b413d2e | 2018-12-25 23:15:46 +0100 | [diff] [blame] | 332 | // Be quick when no text property types have been defined or the buffer, |
| 333 | // unless we are adding one. |
Bram Moolenaar | d79eef2 | 2019-05-24 20:41:55 +0200 | [diff] [blame^] | 334 | if ((!buf->b_has_textprop && !will_change) || buf->b_ml.ml_mfp == NULL) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 335 | return 0; |
| 336 | |
| 337 | // Fetch the line to get the ml_line_len field updated. |
| 338 | text = ml_get_buf(buf, lnum, will_change); |
| 339 | textlen = STRLEN(text) + 1; |
| 340 | proplen = buf->b_ml.ml_line_len - textlen; |
| 341 | if (proplen % sizeof(textprop_T) != 0) |
| 342 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 343 | iemsg(_("E967: text property info corrupted")); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | if (proplen > 0) |
| 347 | *props = text + textlen; |
Bram Moolenaar | 4efe73b | 2018-12-16 14:37:39 +0100 | [diff] [blame] | 348 | return (int)(proplen / sizeof(textprop_T)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 351 | /* |
| 352 | * Set the text properties for line "lnum" to "props" with length "len". |
| 353 | * If "len" is zero text properties are removed, "props" is not used. |
| 354 | * Any existing text properties are dropped. |
| 355 | * Only works for the current buffer. |
| 356 | */ |
| 357 | static void |
| 358 | set_text_props(linenr_T lnum, char_u *props, int len) |
| 359 | { |
Bram Moolenaar | 8aef43b | 2019-01-08 20:14:35 +0100 | [diff] [blame] | 360 | char_u *text; |
| 361 | char_u *newtext; |
| 362 | int textlen; |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 363 | |
| 364 | text = ml_get(lnum); |
Bram Moolenaar | 8aef43b | 2019-01-08 20:14:35 +0100 | [diff] [blame] | 365 | textlen = (int)STRLEN(text) + 1; |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 366 | newtext = alloc(textlen + len); |
| 367 | if (newtext == NULL) |
| 368 | return; |
| 369 | mch_memmove(newtext, text, textlen); |
| 370 | if (len > 0) |
| 371 | mch_memmove(newtext + textlen, props, len); |
| 372 | if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY) |
| 373 | vim_free(curbuf->b_ml.ml_line_ptr); |
| 374 | curbuf->b_ml.ml_line_ptr = newtext; |
| 375 | curbuf->b_ml.ml_line_len = textlen + len; |
| 376 | curbuf->b_ml.ml_flags |= ML_LINE_DIRTY; |
| 377 | } |
| 378 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 379 | static proptype_T * |
| 380 | find_type_by_id(hashtab_T *ht, int id) |
| 381 | { |
| 382 | long todo; |
| 383 | hashitem_T *hi; |
| 384 | |
| 385 | if (ht == NULL) |
| 386 | return NULL; |
| 387 | |
| 388 | // TODO: Make this faster by keeping a list of types sorted on ID and use |
| 389 | // a binary search. |
| 390 | |
| 391 | todo = (long)ht->ht_used; |
| 392 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 393 | { |
| 394 | if (!HASHITEM_EMPTY(hi)) |
| 395 | { |
| 396 | proptype_T *prop = HI2PT(hi); |
| 397 | |
| 398 | if (prop->pt_id == id) |
| 399 | return prop; |
| 400 | --todo; |
| 401 | } |
| 402 | } |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Find a property type by ID in "buf" or globally. |
| 408 | * Returns NULL if not found. |
| 409 | */ |
| 410 | proptype_T * |
| 411 | text_prop_type_by_id(buf_T *buf, int id) |
| 412 | { |
| 413 | proptype_T *type; |
| 414 | |
| 415 | type = find_type_by_id(buf->b_proptypes, id); |
| 416 | if (type == NULL) |
| 417 | type = find_type_by_id(global_proptypes, id); |
| 418 | return type; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * prop_clear({lnum} [, {lnum_end} [, {bufnr}]]) |
| 423 | */ |
| 424 | void |
| 425 | f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED) |
| 426 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 427 | linenr_T start = tv_get_number(&argvars[0]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 428 | linenr_T end = start; |
| 429 | linenr_T lnum; |
| 430 | buf_T *buf = curbuf; |
| 431 | |
| 432 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 433 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 434 | end = tv_get_number(&argvars[1]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 435 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 436 | { |
| 437 | if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL) |
| 438 | return; |
| 439 | } |
| 440 | } |
| 441 | if (start < 1 || end < 1) |
| 442 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 443 | emsg(_(e_invrange)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
| 447 | for (lnum = start; lnum <= end; ++lnum) |
| 448 | { |
| 449 | char_u *text; |
| 450 | size_t len; |
| 451 | |
| 452 | if (lnum > buf->b_ml.ml_line_count) |
| 453 | break; |
| 454 | text = ml_get_buf(buf, lnum, FALSE); |
| 455 | len = STRLEN(text) + 1; |
| 456 | if ((size_t)buf->b_ml.ml_line_len > len) |
| 457 | { |
| 458 | if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY)) |
| 459 | { |
| 460 | char_u *newtext = vim_strsave(text); |
| 461 | |
| 462 | // need to allocate the line now |
| 463 | if (newtext == NULL) |
| 464 | return; |
| 465 | buf->b_ml.ml_line_ptr = newtext; |
| 466 | buf->b_ml.ml_flags |= ML_LINE_DIRTY; |
| 467 | } |
Bram Moolenaar | 4efe73b | 2018-12-16 14:37:39 +0100 | [diff] [blame] | 468 | buf->b_ml.ml_line_len = (int)len; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | redraw_buf_later(buf, NOT_VALID); |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * prop_list({lnum} [, {bufnr}]) |
| 476 | */ |
| 477 | void |
| 478 | f_prop_list(typval_T *argvars, typval_T *rettv) |
| 479 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 480 | linenr_T lnum = tv_get_number(&argvars[0]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 481 | buf_T *buf = curbuf; |
| 482 | |
| 483 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 484 | { |
| 485 | if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL) |
| 486 | return; |
| 487 | } |
| 488 | if (lnum < 1 || lnum > buf->b_ml.ml_line_count) |
| 489 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 490 | emsg(_(e_invrange)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 491 | return; |
| 492 | } |
| 493 | |
| 494 | if (rettv_list_alloc(rettv) == OK) |
| 495 | { |
| 496 | char_u *text = ml_get_buf(buf, lnum, FALSE); |
| 497 | size_t textlen = STRLEN(text) + 1; |
Bram Moolenaar | 4efe73b | 2018-12-16 14:37:39 +0100 | [diff] [blame] | 498 | int count = (int)((buf->b_ml.ml_line_len - textlen) |
| 499 | / sizeof(textprop_T)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 500 | int i; |
| 501 | textprop_T prop; |
| 502 | proptype_T *pt; |
| 503 | |
| 504 | for (i = 0; i < count; ++i) |
| 505 | { |
| 506 | dict_T *d = dict_alloc(); |
| 507 | |
| 508 | if (d == NULL) |
| 509 | break; |
| 510 | mch_memmove(&prop, text + textlen + i * sizeof(textprop_T), |
| 511 | sizeof(textprop_T)); |
| 512 | dict_add_number(d, "col", prop.tp_col); |
| 513 | dict_add_number(d, "length", prop.tp_len); |
| 514 | dict_add_number(d, "id", prop.tp_id); |
| 515 | dict_add_number(d, "start", !(prop.tp_flags & TP_FLAG_CONT_PREV)); |
| 516 | dict_add_number(d, "end", !(prop.tp_flags & TP_FLAG_CONT_NEXT)); |
| 517 | pt = text_prop_type_by_id(buf, prop.tp_type); |
| 518 | if (pt != NULL) |
| 519 | dict_add_string(d, "type", pt->pt_name); |
| 520 | |
| 521 | list_append_dict(rettv->vval.v_list, d); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * prop_remove({props} [, {lnum} [, {lnum_end}]]) |
| 528 | */ |
| 529 | void |
| 530 | f_prop_remove(typval_T *argvars, typval_T *rettv) |
| 531 | { |
| 532 | linenr_T start = 1; |
| 533 | linenr_T end = 0; |
| 534 | linenr_T lnum; |
| 535 | dict_T *dict; |
| 536 | buf_T *buf = curbuf; |
| 537 | dictitem_T *di; |
| 538 | int do_all = FALSE; |
| 539 | int id = -1; |
| 540 | int type_id = -1; |
| 541 | |
| 542 | rettv->vval.v_number = 0; |
| 543 | if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL) |
| 544 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 545 | emsg(_(e_invarg)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 546 | return; |
| 547 | } |
| 548 | |
| 549 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 550 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 551 | start = tv_get_number(&argvars[1]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 552 | end = start; |
| 553 | if (argvars[2].v_type != VAR_UNKNOWN) |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 554 | end = tv_get_number(&argvars[2]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 555 | if (start < 1 || end < 1) |
| 556 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 557 | emsg(_(e_invrange)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | dict = argvars[0].vval.v_dict; |
| 563 | di = dict_find(dict, (char_u *)"bufnr", -1); |
| 564 | if (di != NULL) |
| 565 | { |
Bram Moolenaar | f2d79fa | 2019-01-03 22:19:27 +0100 | [diff] [blame] | 566 | buf = tv_get_buf(&di->di_tv, FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 567 | if (buf == NULL) |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | di = dict_find(dict, (char_u*)"all", -1); |
| 572 | if (di != NULL) |
Bram Moolenaar | 8f66717 | 2018-12-14 15:38:31 +0100 | [diff] [blame] | 573 | do_all = dict_get_number(dict, (char_u *)"all"); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 574 | |
| 575 | if (dict_find(dict, (char_u *)"id", -1) != NULL) |
Bram Moolenaar | 8f66717 | 2018-12-14 15:38:31 +0100 | [diff] [blame] | 576 | id = dict_get_number(dict, (char_u *)"id"); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 577 | if (dict_find(dict, (char_u *)"type", -1)) |
| 578 | { |
Bram Moolenaar | 8f66717 | 2018-12-14 15:38:31 +0100 | [diff] [blame] | 579 | char_u *name = dict_get_string(dict, (char_u *)"type", FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 580 | proptype_T *type = lookup_prop_type(name, buf); |
| 581 | |
| 582 | if (type == NULL) |
| 583 | return; |
| 584 | type_id = type->pt_id; |
| 585 | } |
| 586 | if (id == -1 && type_id == -1) |
| 587 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 588 | emsg(_("E968: Need at least one of 'id' or 'type'")); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | |
| 592 | if (end == 0) |
| 593 | end = buf->b_ml.ml_line_count; |
| 594 | for (lnum = start; lnum <= end; ++lnum) |
| 595 | { |
| 596 | char_u *text; |
| 597 | size_t len; |
| 598 | |
| 599 | if (lnum > buf->b_ml.ml_line_count) |
| 600 | break; |
| 601 | text = ml_get_buf(buf, lnum, FALSE); |
| 602 | len = STRLEN(text) + 1; |
| 603 | if ((size_t)buf->b_ml.ml_line_len > len) |
| 604 | { |
| 605 | static textprop_T textprop; // static because of alignment |
| 606 | unsigned idx; |
| 607 | |
| 608 | for (idx = 0; idx < (buf->b_ml.ml_line_len - len) |
| 609 | / sizeof(textprop_T); ++idx) |
| 610 | { |
| 611 | char_u *cur_prop = buf->b_ml.ml_line_ptr + len |
| 612 | + idx * sizeof(textprop_T); |
| 613 | size_t taillen; |
| 614 | |
| 615 | mch_memmove(&textprop, cur_prop, sizeof(textprop_T)); |
| 616 | if (textprop.tp_id == id || textprop.tp_type == type_id) |
| 617 | { |
| 618 | if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY)) |
| 619 | { |
| 620 | char_u *newptr = alloc(buf->b_ml.ml_line_len); |
| 621 | |
| 622 | // need to allocate the line to be able to change it |
| 623 | if (newptr == NULL) |
| 624 | return; |
| 625 | mch_memmove(newptr, buf->b_ml.ml_line_ptr, |
| 626 | buf->b_ml.ml_line_len); |
| 627 | buf->b_ml.ml_line_ptr = newptr; |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 628 | buf->b_ml.ml_flags |= ML_LINE_DIRTY; |
| 629 | |
| 630 | cur_prop = buf->b_ml.ml_line_ptr + len |
| 631 | + idx * sizeof(textprop_T); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | taillen = buf->b_ml.ml_line_len - len |
| 635 | - (idx + 1) * sizeof(textprop_T); |
| 636 | if (taillen > 0) |
| 637 | mch_memmove(cur_prop, cur_prop + sizeof(textprop_T), |
| 638 | taillen); |
| 639 | buf->b_ml.ml_line_len -= sizeof(textprop_T); |
| 640 | --idx; |
| 641 | |
| 642 | ++rettv->vval.v_number; |
| 643 | if (!do_all) |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | redraw_buf_later(buf, NOT_VALID); |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Common for f_prop_type_add() and f_prop_type_change(). |
| 654 | */ |
| 655 | void |
| 656 | prop_type_set(typval_T *argvars, int add) |
| 657 | { |
| 658 | char_u *name; |
| 659 | buf_T *buf = NULL; |
| 660 | dict_T *dict; |
| 661 | dictitem_T *di; |
| 662 | proptype_T *prop; |
| 663 | |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 664 | name = tv_get_string(&argvars[0]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 665 | if (*name == NUL) |
| 666 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 667 | emsg(_(e_invarg)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 668 | return; |
| 669 | } |
| 670 | |
| 671 | if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL) |
| 672 | return; |
| 673 | dict = argvars[1].vval.v_dict; |
| 674 | |
| 675 | prop = find_prop(name, buf); |
| 676 | if (add) |
| 677 | { |
| 678 | hashtab_T **htp; |
| 679 | |
| 680 | if (prop != NULL) |
| 681 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 682 | semsg(_("E969: Property type %s already defined"), name); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 683 | return; |
| 684 | } |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 685 | prop = (proptype_T *)alloc_clear(sizeof(proptype_T) + STRLEN(name)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 686 | if (prop == NULL) |
| 687 | return; |
| 688 | STRCPY(prop->pt_name, name); |
| 689 | prop->pt_id = ++proptype_id; |
| 690 | htp = buf == NULL ? &global_proptypes : &buf->b_proptypes; |
| 691 | if (*htp == NULL) |
| 692 | { |
| 693 | *htp = (hashtab_T *)alloc(sizeof(hashtab_T)); |
| 694 | if (*htp == NULL) |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 695 | { |
| 696 | vim_free(prop); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 697 | return; |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 698 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 699 | hash_init(*htp); |
| 700 | } |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 701 | hash_add(*htp, PT2HIKEY(prop)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 702 | } |
| 703 | else |
| 704 | { |
| 705 | if (prop == NULL) |
| 706 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 707 | semsg(_(e_type_not_exist), name); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 708 | return; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | if (dict != NULL) |
| 713 | { |
| 714 | di = dict_find(dict, (char_u *)"highlight", -1); |
| 715 | if (di != NULL) |
| 716 | { |
| 717 | char_u *highlight; |
| 718 | int hl_id = 0; |
| 719 | |
Bram Moolenaar | 8f66717 | 2018-12-14 15:38:31 +0100 | [diff] [blame] | 720 | highlight = dict_get_string(dict, (char_u *)"highlight", FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 721 | if (highlight != NULL && *highlight != NUL) |
| 722 | hl_id = syn_name2id(highlight); |
| 723 | if (hl_id <= 0) |
| 724 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 725 | semsg(_("E970: Unknown highlight group name: '%s'"), |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 726 | highlight == NULL ? (char_u *)"" : highlight); |
| 727 | return; |
| 728 | } |
| 729 | prop->pt_hl_id = hl_id; |
| 730 | } |
| 731 | |
Bram Moolenaar | 58187f1 | 2019-05-05 16:33:47 +0200 | [diff] [blame] | 732 | di = dict_find(dict, (char_u *)"combine", -1); |
| 733 | if (di != NULL) |
| 734 | { |
| 735 | if (tv_get_number(&di->di_tv)) |
| 736 | prop->pt_flags |= PT_FLAG_COMBINE; |
| 737 | else |
| 738 | prop->pt_flags &= ~PT_FLAG_COMBINE; |
| 739 | } |
| 740 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 741 | di = dict_find(dict, (char_u *)"priority", -1); |
| 742 | if (di != NULL) |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 743 | prop->pt_priority = tv_get_number(&di->di_tv); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 744 | |
| 745 | di = dict_find(dict, (char_u *)"start_incl", -1); |
| 746 | if (di != NULL) |
| 747 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 748 | if (tv_get_number(&di->di_tv)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 749 | prop->pt_flags |= PT_FLAG_INS_START_INCL; |
| 750 | else |
| 751 | prop->pt_flags &= ~PT_FLAG_INS_START_INCL; |
| 752 | } |
| 753 | |
| 754 | di = dict_find(dict, (char_u *)"end_incl", -1); |
| 755 | if (di != NULL) |
| 756 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 757 | if (tv_get_number(&di->di_tv)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 758 | prop->pt_flags |= PT_FLAG_INS_END_INCL; |
| 759 | else |
| 760 | prop->pt_flags &= ~PT_FLAG_INS_END_INCL; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * prop_type_add({name}, {props}) |
| 767 | */ |
| 768 | void |
| 769 | f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED) |
| 770 | { |
| 771 | prop_type_set(argvars, TRUE); |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * prop_type_change({name}, {props}) |
| 776 | */ |
| 777 | void |
| 778 | f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED) |
| 779 | { |
| 780 | prop_type_set(argvars, FALSE); |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * prop_type_delete({name} [, {bufnr}]) |
| 785 | */ |
| 786 | void |
| 787 | f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED) |
| 788 | { |
| 789 | char_u *name; |
| 790 | buf_T *buf = NULL; |
| 791 | hashitem_T *hi; |
| 792 | |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 793 | name = tv_get_string(&argvars[0]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 794 | if (*name == NUL) |
| 795 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 796 | emsg(_(e_invarg)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | |
| 800 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 801 | { |
| 802 | if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL) |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | hi = find_prop_hi(name, buf); |
| 807 | if (hi != NULL) |
| 808 | { |
| 809 | hashtab_T *ht; |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 810 | proptype_T *prop = HI2PT(hi); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 811 | |
| 812 | if (buf == NULL) |
| 813 | ht = global_proptypes; |
| 814 | else |
| 815 | ht = buf->b_proptypes; |
| 816 | hash_remove(ht, hi); |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 817 | vim_free(prop); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * prop_type_get({name} [, {bufnr}]) |
| 823 | */ |
| 824 | void |
| 825 | f_prop_type_get(typval_T *argvars, typval_T *rettv UNUSED) |
| 826 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 827 | char_u *name = tv_get_string(&argvars[0]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 828 | |
| 829 | if (*name == NUL) |
| 830 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 831 | emsg(_(e_invarg)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | if (rettv_dict_alloc(rettv) == OK) |
| 835 | { |
| 836 | proptype_T *prop = NULL; |
| 837 | buf_T *buf = NULL; |
| 838 | |
| 839 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 840 | { |
| 841 | if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL) |
| 842 | return; |
| 843 | } |
| 844 | |
| 845 | prop = find_prop(name, buf); |
| 846 | if (prop != NULL) |
| 847 | { |
| 848 | dict_T *d = rettv->vval.v_dict; |
| 849 | |
| 850 | if (prop->pt_hl_id > 0) |
| 851 | dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id)); |
| 852 | dict_add_number(d, "priority", prop->pt_priority); |
Bram Moolenaar | 58187f1 | 2019-05-05 16:33:47 +0200 | [diff] [blame] | 853 | dict_add_number(d, "combine", |
| 854 | (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 855 | dict_add_number(d, "start_incl", |
| 856 | (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0); |
| 857 | dict_add_number(d, "end_incl", |
| 858 | (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0); |
| 859 | if (buf != NULL) |
| 860 | dict_add_number(d, "bufnr", buf->b_fnum); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | static void |
| 866 | list_types(hashtab_T *ht, list_T *l) |
| 867 | { |
| 868 | long todo; |
| 869 | hashitem_T *hi; |
| 870 | |
| 871 | todo = (long)ht->ht_used; |
| 872 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 873 | { |
| 874 | if (!HASHITEM_EMPTY(hi)) |
| 875 | { |
| 876 | proptype_T *prop = HI2PT(hi); |
| 877 | |
| 878 | list_append_string(l, prop->pt_name, -1); |
| 879 | --todo; |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * prop_type_list([{bufnr}]) |
| 886 | */ |
| 887 | void |
| 888 | f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED) |
| 889 | { |
| 890 | buf_T *buf = NULL; |
| 891 | |
| 892 | if (rettv_list_alloc(rettv) == OK) |
| 893 | { |
| 894 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 895 | { |
| 896 | if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL) |
| 897 | return; |
| 898 | } |
| 899 | if (buf == NULL) |
| 900 | { |
| 901 | if (global_proptypes != NULL) |
| 902 | list_types(global_proptypes, rettv->vval.v_list); |
| 903 | } |
| 904 | else if (buf->b_proptypes != NULL) |
| 905 | list_types(buf->b_proptypes, rettv->vval.v_list); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Free all property types in "ht". |
| 911 | */ |
| 912 | static void |
| 913 | clear_ht_prop_types(hashtab_T *ht) |
| 914 | { |
| 915 | long todo; |
| 916 | hashitem_T *hi; |
| 917 | |
| 918 | if (ht == NULL) |
| 919 | return; |
| 920 | |
| 921 | todo = (long)ht->ht_used; |
| 922 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 923 | { |
| 924 | if (!HASHITEM_EMPTY(hi)) |
| 925 | { |
| 926 | proptype_T *prop = HI2PT(hi); |
| 927 | |
| 928 | vim_free(prop); |
| 929 | --todo; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | hash_clear(ht); |
| 934 | vim_free(ht); |
| 935 | } |
| 936 | |
| 937 | #if defined(EXITFREE) || defined(PROTO) |
| 938 | /* |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 939 | * Free all global property types. |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 940 | */ |
| 941 | void |
| 942 | clear_global_prop_types(void) |
| 943 | { |
| 944 | clear_ht_prop_types(global_proptypes); |
| 945 | global_proptypes = NULL; |
| 946 | } |
| 947 | #endif |
| 948 | |
| 949 | /* |
| 950 | * Free all property types for "buf". |
| 951 | */ |
| 952 | void |
| 953 | clear_buf_prop_types(buf_T *buf) |
| 954 | { |
| 955 | clear_ht_prop_types(buf->b_proptypes); |
| 956 | buf->b_proptypes = NULL; |
| 957 | } |
| 958 | |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 959 | /* |
| 960 | * Adjust the columns of text properties in line "lnum" after position "col" to |
| 961 | * shift by "bytes_added" (can be negative). |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 962 | * Note that "col" is zero-based, while tp_col is one-based. |
| 963 | * Only for the current buffer. |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 964 | * "flags" can have: |
| 965 | * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line. |
| 966 | * APC_SUBSTITUTE: Text is replaced, not inserted. |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 967 | * Caller is expected to check b_has_textprop and "bytes_added" being non-zero. |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 968 | * Returns TRUE when props were changed. |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 969 | */ |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 970 | int |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 971 | adjust_prop_columns( |
| 972 | linenr_T lnum, |
| 973 | colnr_T col, |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 974 | int bytes_added, |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 975 | int flags) |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 976 | { |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 977 | int proplen; |
| 978 | char_u *props; |
| 979 | textprop_T tmp_prop; |
| 980 | proptype_T *pt; |
| 981 | int dirty = FALSE; |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 982 | int ri, wi; |
| 983 | size_t textlen; |
| 984 | |
| 985 | if (text_prop_frozen > 0) |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 986 | return FALSE; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 987 | |
| 988 | proplen = get_text_props(curbuf, lnum, &props, TRUE); |
| 989 | if (proplen == 0) |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 990 | return FALSE; |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 991 | textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 992 | |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 993 | wi = 0; // write index |
| 994 | for (ri = 0; ri < proplen; ++ri) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 995 | { |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 996 | int start_incl; |
| 997 | |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 998 | mch_memmove(&tmp_prop, props + ri * sizeof(textprop_T), |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 999 | sizeof(textprop_T)); |
| 1000 | pt = text_prop_type_by_id(curbuf, tmp_prop.tp_type); |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 1001 | start_incl = (flags & APC_SUBSTITUTE) || |
| 1002 | (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL)); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1003 | |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 1004 | if (bytes_added > 0 |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 1005 | && (tmp_prop.tp_col >= col + (start_incl ? 2 : 1))) |
| 1006 | { |
| 1007 | if (tmp_prop.tp_col < col + (start_incl ? 2 : 1)) |
| 1008 | { |
| 1009 | tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added; |
| 1010 | tmp_prop.tp_col = col + 1; |
| 1011 | } |
| 1012 | else |
| 1013 | tmp_prop.tp_col += bytes_added; |
| 1014 | // Save for undo if requested and not done yet. |
| 1015 | if ((flags & APC_SAVE_FOR_UNDO) && !dirty) |
| 1016 | u_savesub(lnum); |
| 1017 | dirty = TRUE; |
| 1018 | } |
| 1019 | else if (bytes_added <= 0 && (tmp_prop.tp_col > col + 1)) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1020 | { |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 1021 | if (tmp_prop.tp_col + bytes_added < col + 1) |
| 1022 | { |
| 1023 | tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added; |
| 1024 | tmp_prop.tp_col = col + 1; |
| 1025 | } |
| 1026 | else |
| 1027 | tmp_prop.tp_col += bytes_added; |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 1028 | // Save for undo if requested and not done yet. |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 1029 | if ((flags & APC_SAVE_FOR_UNDO) && !dirty) |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 1030 | u_savesub(lnum); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1031 | dirty = TRUE; |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 1032 | if (tmp_prop.tp_len <= 0) |
| 1033 | continue; // drop this text property |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1034 | } |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 1035 | else if (tmp_prop.tp_len > 0 |
| 1036 | && tmp_prop.tp_col + tmp_prop.tp_len > col |
Bram Moolenaar | 4614f53 | 2019-01-06 12:54:55 +0100 | [diff] [blame] | 1037 | + ((pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL)) |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 1038 | ? 0 : 1)) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1039 | { |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 1040 | int after = col - bytes_added |
| 1041 | - (tmp_prop.tp_col - 1 + tmp_prop.tp_len); |
| 1042 | if (after > 0) |
| 1043 | tmp_prop.tp_len += bytes_added + after; |
| 1044 | else |
| 1045 | tmp_prop.tp_len += bytes_added; |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 1046 | // Save for undo if requested and not done yet. |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 1047 | if ((flags & APC_SAVE_FOR_UNDO) && !dirty) |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 1048 | u_savesub(lnum); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1049 | dirty = TRUE; |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 1050 | if (tmp_prop.tp_len <= 0) |
| 1051 | continue; // drop this text property |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1052 | } |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 1053 | mch_memmove(props + wi * sizeof(textprop_T), &tmp_prop, |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1054 | sizeof(textprop_T)); |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 1055 | ++wi; |
| 1056 | } |
| 1057 | if (dirty) |
| 1058 | { |
Bram Moolenaar | 4614f53 | 2019-01-06 12:54:55 +0100 | [diff] [blame] | 1059 | colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T); |
| 1060 | |
| 1061 | if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0) |
| 1062 | curbuf->b_ml.ml_line_ptr = |
| 1063 | vim_memsave(curbuf->b_ml.ml_line_ptr, newlen); |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 1064 | curbuf->b_ml.ml_flags |= ML_LINE_DIRTY; |
Bram Moolenaar | 4614f53 | 2019-01-06 12:54:55 +0100 | [diff] [blame] | 1065 | curbuf->b_ml.ml_line_len = newlen; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 1066 | } |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 1067 | return dirty; |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 1070 | /* |
| 1071 | * Adjust text properties for a line that was split in two. |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 1072 | * "lnum_props" is the line that has the properties from before the split. |
| 1073 | * "lnum_top" is the top line. |
| 1074 | * "kept" is the number of bytes kept in the first line, while |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 1075 | * "deleted" is the number of bytes deleted. |
| 1076 | */ |
| 1077 | void |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 1078 | adjust_props_for_split( |
| 1079 | linenr_T lnum_props, |
| 1080 | linenr_T lnum_top, |
| 1081 | int kept, |
| 1082 | int deleted) |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 1083 | { |
| 1084 | char_u *props; |
| 1085 | int count; |
| 1086 | garray_T prevprop; |
| 1087 | garray_T nextprop; |
| 1088 | int i; |
| 1089 | int skipped = kept + deleted; |
| 1090 | |
| 1091 | if (!curbuf->b_has_textprop) |
| 1092 | return; |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 1093 | |
| 1094 | // Get the text properties from "lnum_props". |
| 1095 | count = get_text_props(curbuf, lnum_props, &props, FALSE); |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 1096 | ga_init2(&prevprop, sizeof(textprop_T), 10); |
| 1097 | ga_init2(&nextprop, sizeof(textprop_T), 10); |
| 1098 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 1099 | // Keep the relevant ones in the first line, reducing the length if needed. |
| 1100 | // Copy the ones that include the split to the second line. |
| 1101 | // Move the ones after the split to the second line. |
| 1102 | for (i = 0; i < count; ++i) |
| 1103 | { |
| 1104 | textprop_T prop; |
| 1105 | textprop_T *p; |
| 1106 | |
| 1107 | // copy the prop to an aligned structure |
| 1108 | mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T)); |
| 1109 | |
| 1110 | if (prop.tp_col < kept && ga_grow(&prevprop, 1) == OK) |
| 1111 | { |
| 1112 | p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len; |
| 1113 | *p = prop; |
| 1114 | if (p->tp_col + p->tp_len >= kept) |
| 1115 | p->tp_len = kept - p->tp_col; |
| 1116 | ++prevprop.ga_len; |
| 1117 | } |
| 1118 | |
Bram Moolenaar | 5c65e6a | 2019-05-17 11:08:56 +0200 | [diff] [blame] | 1119 | // Only add the property to the next line if the length is bigger than |
| 1120 | // zero. |
| 1121 | if (prop.tp_col + prop.tp_len > skipped && ga_grow(&nextprop, 1) == OK) |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 1122 | { |
| 1123 | p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len; |
| 1124 | *p = prop; |
| 1125 | if (p->tp_col > skipped) |
| 1126 | p->tp_col -= skipped - 1; |
| 1127 | else |
| 1128 | { |
| 1129 | p->tp_len -= skipped - p->tp_col; |
| 1130 | p->tp_col = 1; |
| 1131 | } |
| 1132 | ++nextprop.ga_len; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 1136 | set_text_props(lnum_top, prevprop.ga_data, |
| 1137 | prevprop.ga_len * sizeof(textprop_T)); |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 1138 | ga_clear(&prevprop); |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 1139 | set_text_props(lnum_top + 1, nextprop.ga_data, |
| 1140 | nextprop.ga_len * sizeof(textprop_T)); |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 1141 | ga_clear(&nextprop); |
| 1142 | } |
| 1143 | |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 1144 | /* |
| 1145 | * Line "lnum" has been joined and will end up at column "col" in the new line. |
| 1146 | * "removed" bytes have been removed from the start of the line, properties |
| 1147 | * there are to be discarded. |
| 1148 | * Move the adjusted text properties to an allocated string, store it in |
| 1149 | * "prop_line" and adjust the columns. |
| 1150 | */ |
| 1151 | void |
| 1152 | adjust_props_for_join( |
| 1153 | linenr_T lnum, |
| 1154 | textprop_T **prop_line, |
| 1155 | int *prop_length, |
| 1156 | long col, |
| 1157 | int removed) |
| 1158 | { |
| 1159 | int proplen; |
| 1160 | char_u *props; |
| 1161 | int ri; |
| 1162 | int wi = 0; |
| 1163 | |
| 1164 | proplen = get_text_props(curbuf, lnum, &props, FALSE); |
| 1165 | if (proplen > 0) |
| 1166 | { |
| 1167 | *prop_line = (textprop_T *)alloc(proplen * (int)sizeof(textprop_T)); |
| 1168 | if (*prop_line != NULL) |
| 1169 | { |
| 1170 | for (ri = 0; ri < proplen; ++ri) |
| 1171 | { |
| 1172 | textprop_T *cp = *prop_line + wi; |
| 1173 | |
| 1174 | mch_memmove(cp, props + ri * sizeof(textprop_T), |
| 1175 | sizeof(textprop_T)); |
| 1176 | if (cp->tp_col + cp->tp_len > removed) |
| 1177 | { |
| 1178 | if (cp->tp_col > removed) |
| 1179 | cp->tp_col += col; |
| 1180 | else |
| 1181 | { |
| 1182 | // property was partly deleted, make it shorter |
| 1183 | cp->tp_len -= removed - cp->tp_col; |
| 1184 | cp->tp_col = col; |
| 1185 | } |
| 1186 | ++wi; |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | *prop_length = wi; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * After joining lines: concatenate the text and the properties of all joined |
| 1196 | * lines into one line and replace the line. |
| 1197 | */ |
| 1198 | void |
| 1199 | join_prop_lines( |
| 1200 | linenr_T lnum, |
| 1201 | char_u *newp, |
| 1202 | textprop_T **prop_lines, |
| 1203 | int *prop_lengths, |
| 1204 | int count) |
| 1205 | { |
| 1206 | size_t proplen = 0; |
| 1207 | size_t oldproplen; |
| 1208 | char_u *props; |
| 1209 | int i; |
Bram Moolenaar | e2ad826 | 2019-05-24 13:22:22 +0200 | [diff] [blame] | 1210 | size_t len; |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 1211 | char_u *line; |
| 1212 | size_t l; |
| 1213 | |
| 1214 | for (i = 0; i < count - 1; ++i) |
| 1215 | proplen += prop_lengths[i]; |
| 1216 | if (proplen == 0) |
| 1217 | { |
| 1218 | ml_replace(lnum, newp, FALSE); |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | // get existing properties of the joined line |
| 1223 | oldproplen = get_text_props(curbuf, lnum, &props, FALSE); |
| 1224 | |
Bram Moolenaar | e2ad826 | 2019-05-24 13:22:22 +0200 | [diff] [blame] | 1225 | len = STRLEN(newp) + 1; |
| 1226 | line = alloc((int)(len + (oldproplen + proplen) * sizeof(textprop_T))); |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 1227 | if (line == NULL) |
| 1228 | return; |
| 1229 | mch_memmove(line, newp, len); |
| 1230 | l = oldproplen * sizeof(textprop_T); |
| 1231 | mch_memmove(line + len, props, l); |
| 1232 | len += l; |
| 1233 | |
| 1234 | for (i = 0; i < count - 1; ++i) |
| 1235 | if (prop_lines[i] != NULL) |
| 1236 | { |
| 1237 | l = prop_lengths[i] * sizeof(textprop_T); |
| 1238 | mch_memmove(line + len, prop_lines[i], l); |
| 1239 | len += l; |
| 1240 | vim_free(prop_lines[i]); |
| 1241 | } |
| 1242 | |
Bram Moolenaar | e2ad826 | 2019-05-24 13:22:22 +0200 | [diff] [blame] | 1243 | ml_replace_len(lnum, line, (colnr_T)len, TRUE, FALSE); |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 1244 | vim_free(newp); |
| 1245 | vim_free(prop_lines); |
| 1246 | vim_free(prop_lengths); |
| 1247 | } |
| 1248 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1249 | #endif // FEAT_TEXT_PROP |