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