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