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