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 | |
| 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 16 | #if defined(FEAT_PROP_POPUP) || defined(PROTO) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * In a hashtable item "hi_key" points to "pt_name" in a proptype_T. |
| 20 | * This avoids adding a pointer to the hashtable item. |
| 21 | * PT2HIKEY() converts a proptype pointer to a hashitem key pointer. |
| 22 | * HIKEY2PT() converts a hashitem key pointer to a proptype pointer. |
| 23 | * HI2PT() converts a hashitem pointer to a proptype pointer. |
| 24 | */ |
| 25 | #define PT2HIKEY(p) ((p)->pt_name) |
| 26 | #define HIKEY2PT(p) ((proptype_T *)((p) - offsetof(proptype_T, pt_name))) |
| 27 | #define HI2PT(hi) HIKEY2PT((hi)->hi_key) |
| 28 | |
| 29 | // The global text property types. |
| 30 | static hashtab_T *global_proptypes = NULL; |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 31 | static proptype_T **global_proparray = NULL; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 32 | |
| 33 | // The last used text property type ID. |
| 34 | static int proptype_id = 0; |
| 35 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 36 | /* |
| 37 | * Find a property type by name, return the hashitem. |
| 38 | * Returns NULL if the item can't be found. |
| 39 | */ |
| 40 | static hashitem_T * |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 41 | find_prop_type_hi(char_u *name, buf_T *buf) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 42 | { |
| 43 | hashtab_T *ht; |
| 44 | hashitem_T *hi; |
| 45 | |
| 46 | if (*name == NUL) |
| 47 | return NULL; |
| 48 | if (buf == NULL) |
| 49 | ht = global_proptypes; |
| 50 | else |
| 51 | ht = buf->b_proptypes; |
| 52 | |
| 53 | if (ht == NULL) |
| 54 | return NULL; |
| 55 | hi = hash_find(ht, name); |
| 56 | if (HASHITEM_EMPTY(hi)) |
| 57 | return NULL; |
| 58 | return hi; |
| 59 | } |
| 60 | |
| 61 | /* |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 62 | * Like find_prop_type_hi() but return the property type. |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 63 | */ |
| 64 | static proptype_T * |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 65 | find_prop_type(char_u *name, buf_T *buf) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 66 | { |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 67 | hashitem_T *hi = find_prop_type_hi(name, buf); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 68 | |
| 69 | if (hi == NULL) |
| 70 | return NULL; |
| 71 | return HI2PT(hi); |
| 72 | } |
| 73 | |
| 74 | /* |
Bram Moolenaar | 12034e2 | 2019-08-25 22:25:02 +0200 | [diff] [blame] | 75 | * Get the prop type ID of "name". |
| 76 | * When not found return zero. |
| 77 | */ |
| 78 | int |
| 79 | find_prop_type_id(char_u *name, buf_T *buf) |
| 80 | { |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 81 | proptype_T *pt = find_prop_type(name, buf); |
Bram Moolenaar | 12034e2 | 2019-08-25 22:25:02 +0200 | [diff] [blame] | 82 | |
| 83 | if (pt == NULL) |
| 84 | return 0; |
| 85 | return pt->pt_id; |
| 86 | } |
| 87 | |
| 88 | /* |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 89 | * Lookup a property type by name. First in "buf" and when not found in the |
| 90 | * global types. |
| 91 | * When not found gives an error message and returns NULL. |
| 92 | */ |
| 93 | static proptype_T * |
| 94 | lookup_prop_type(char_u *name, buf_T *buf) |
| 95 | { |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 96 | proptype_T *type = find_prop_type(name, buf); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 97 | |
| 98 | if (type == NULL) |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 99 | type = find_prop_type(name, NULL); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 100 | if (type == NULL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 101 | semsg(_(e_type_not_exist), name); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 102 | return type; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Get an optional "bufnr" item from the dict in "arg". |
| 107 | * When the argument is not used or "bufnr" is not present then "buf" is |
| 108 | * unchanged. |
| 109 | * If "bufnr" is valid or not present return OK. |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 110 | * When "arg" is not a dict or "bufnr" is invalid return FAIL. |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 111 | */ |
| 112 | static int |
| 113 | get_bufnr_from_arg(typval_T *arg, buf_T **buf) |
| 114 | { |
| 115 | dictitem_T *di; |
| 116 | |
| 117 | if (arg->v_type != VAR_DICT) |
| 118 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 119 | emsg(_(e_dictionary_required)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 120 | return FAIL; |
| 121 | } |
| 122 | if (arg->vval.v_dict == NULL) |
| 123 | return OK; // NULL dict is like an empty dict |
| 124 | di = dict_find(arg->vval.v_dict, (char_u *)"bufnr", -1); |
Martin Tournoij | e2390c7 | 2021-07-28 13:30:16 +0200 | [diff] [blame] | 125 | if (di != NULL && (di->di_tv.v_type != VAR_NUMBER |
| 126 | || di->di_tv.vval.v_number != 0)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 127 | { |
Bram Moolenaar | f0884c5 | 2019-05-24 21:22:29 +0200 | [diff] [blame] | 128 | *buf = get_buf_arg(&di->di_tv); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 129 | if (*buf == NULL) |
| 130 | return FAIL; |
| 131 | } |
| 132 | return OK; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * prop_add({lnum}, {col}, {props}) |
| 137 | */ |
| 138 | void |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 139 | f_prop_add(typval_T *argvars, typval_T *rettv) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 140 | { |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 141 | linenr_T start_lnum; |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 142 | colnr_T start_col; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 143 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 144 | if (in_vim9script() |
| 145 | && (check_for_number_arg(argvars, 0) == FAIL |
| 146 | || check_for_number_arg(argvars, 1) == FAIL |
| 147 | || check_for_dict_arg(argvars, 2) == FAIL)) |
| 148 | return; |
| 149 | |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 150 | start_lnum = tv_get_number(&argvars[0]); |
| 151 | start_col = tv_get_number(&argvars[1]); |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 152 | if (check_for_dict_arg(argvars, 2) == FAIL) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 153 | return; |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 154 | |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 155 | rettv->vval.v_number = prop_add_common(start_lnum, start_col, |
| 156 | argvars[2].vval.v_dict, curbuf, &argvars[2]); |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 160 | * Attach a text property 'type_name' to the text starting |
| 161 | * at [start_lnum, start_col] and ending at [end_lnum, end_col] in |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 162 | * the buffer "buf" and assign identifier "id". |
| 163 | * When "text" is not NULL add it to buf->b_textprop_text[-id - 1]. |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 164 | */ |
| 165 | static int |
| 166 | prop_add_one( |
| 167 | buf_T *buf, |
| 168 | char_u *type_name, |
| 169 | int id, |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 170 | char_u *text_arg, |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 171 | int text_padding_left, |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 172 | int text_flags, |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 173 | linenr_T start_lnum, |
| 174 | linenr_T end_lnum, |
| 175 | colnr_T start_col, |
| 176 | colnr_T end_col) |
| 177 | { |
| 178 | proptype_T *type; |
| 179 | linenr_T lnum; |
| 180 | int proplen; |
| 181 | char_u *props = NULL; |
| 182 | char_u *newprops; |
| 183 | size_t textlen; |
| 184 | char_u *newtext; |
| 185 | int i; |
| 186 | textprop_T tmp_prop; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 187 | char_u *text = text_arg; |
| 188 | int res = FAIL; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 189 | |
| 190 | type = lookup_prop_type(type_name, buf); |
| 191 | if (type == NULL) |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 192 | goto theend; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 193 | |
| 194 | if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count) |
| 195 | { |
Bram Moolenaar | 8dac2ac | 2021-12-27 20:57:06 +0000 | [diff] [blame] | 196 | semsg(_(e_invalid_line_number_nr), (long)start_lnum); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 197 | goto theend; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 198 | } |
| 199 | if (end_lnum < start_lnum || end_lnum > buf->b_ml.ml_line_count) |
| 200 | { |
Bram Moolenaar | 8dac2ac | 2021-12-27 20:57:06 +0000 | [diff] [blame] | 201 | semsg(_(e_invalid_line_number_nr), (long)end_lnum); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 202 | goto theend; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if (buf->b_ml.ml_mfp == NULL) |
| 206 | { |
Bram Moolenaar | 9a846fb | 2022-01-01 21:59:18 +0000 | [diff] [blame] | 207 | emsg(_(e_cannot_add_text_property_to_unloaded_buffer)); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 208 | goto theend; |
| 209 | } |
| 210 | |
| 211 | if (text != NULL) |
| 212 | { |
Bram Moolenaar | 783ef72 | 2022-08-01 16:11:06 +0100 | [diff] [blame] | 213 | garray_T *gap = &buf->b_textprop_text; |
| 214 | char_u *p; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 215 | |
| 216 | // double check we got the right ID |
| 217 | if (-id - 1 != gap->ga_len) |
| 218 | iemsg("text prop ID mismatch"); |
| 219 | if (gap->ga_growsize == 0) |
| 220 | ga_init2(gap, sizeof(char *), 50); |
| 221 | if (ga_grow(gap, 1) == FAIL) |
| 222 | goto theend; |
| 223 | ((char_u **)gap->ga_data)[gap->ga_len++] = text; |
Bram Moolenaar | 783ef72 | 2022-08-01 16:11:06 +0100 | [diff] [blame] | 224 | |
Bram Moolenaar | f4ba8bc | 2022-08-05 17:05:04 +0100 | [diff] [blame] | 225 | // change any control character (Tab, Newline, etc.) to a Space to make |
| 226 | // it simpler to compute the size |
Bram Moolenaar | 783ef72 | 2022-08-01 16:11:06 +0100 | [diff] [blame] | 227 | for (p = text; *p != NUL; MB_PTR_ADV(p)) |
Bram Moolenaar | f4ba8bc | 2022-08-05 17:05:04 +0100 | [diff] [blame] | 228 | if (*p < ' ') |
Bram Moolenaar | 783ef72 | 2022-08-01 16:11:06 +0100 | [diff] [blame] | 229 | *p = ' '; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 230 | text = NULL; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | for (lnum = start_lnum; lnum <= end_lnum; ++lnum) |
| 234 | { |
| 235 | colnr_T col; // start column |
| 236 | long length; // in bytes |
| 237 | |
| 238 | // Fetch the line to get the ml_line_len field updated. |
| 239 | proplen = get_text_props(buf, lnum, &props, TRUE); |
| 240 | textlen = buf->b_ml.ml_line_len - proplen * sizeof(textprop_T); |
| 241 | |
| 242 | if (lnum == start_lnum) |
| 243 | col = start_col; |
| 244 | else |
| 245 | col = 1; |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 246 | if (col - 1 > (colnr_T)textlen && !(col == 0 && text_arg != NULL)) |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 247 | { |
Bram Moolenaar | 8dac2ac | 2021-12-27 20:57:06 +0000 | [diff] [blame] | 248 | semsg(_(e_invalid_column_number_nr), (long)start_col); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 249 | goto theend; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | if (lnum == end_lnum) |
| 253 | length = end_col - col; |
| 254 | else |
| 255 | length = (int)textlen - col + 1; |
| 256 | if (length > (long)textlen) |
| 257 | length = (int)textlen; // can include the end-of-line |
| 258 | if (length < 0) |
| 259 | length = 0; // zero-width property |
| 260 | |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 261 | if (text_arg != NULL) |
| 262 | { |
| 263 | length = 1; // text is placed on one character |
| 264 | if (col == 0) |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 265 | { |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 266 | col = MAXCOL; // after the line |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 267 | length += text_padding_left; |
| 268 | } |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 271 | // Allocate the new line with space for the new property. |
| 272 | newtext = alloc(buf->b_ml.ml_line_len + sizeof(textprop_T)); |
| 273 | if (newtext == NULL) |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 274 | goto theend; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 275 | // Copy the text, including terminating NUL. |
| 276 | mch_memmove(newtext, buf->b_ml.ml_line_ptr, textlen); |
| 277 | |
| 278 | // Find the index where to insert the new property. |
| 279 | // Since the text properties are not aligned properly when stored with |
| 280 | // the text, we need to copy them as bytes before using it as a struct. |
| 281 | for (i = 0; i < proplen; ++i) |
| 282 | { |
| 283 | mch_memmove(&tmp_prop, props + i * sizeof(textprop_T), |
| 284 | sizeof(textprop_T)); |
| 285 | if (tmp_prop.tp_col >= col) |
| 286 | break; |
| 287 | } |
| 288 | newprops = newtext + textlen; |
| 289 | if (i > 0) |
| 290 | mch_memmove(newprops, props, sizeof(textprop_T) * i); |
| 291 | |
| 292 | tmp_prop.tp_col = col; |
| 293 | tmp_prop.tp_len = length; |
| 294 | tmp_prop.tp_id = id; |
| 295 | tmp_prop.tp_type = type->pt_id; |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 296 | tmp_prop.tp_flags = text_flags |
| 297 | | (lnum > start_lnum ? TP_FLAG_CONT_PREV : 0) |
Bram Moolenaar | 28c9f89 | 2022-08-14 13:28:55 +0100 | [diff] [blame] | 298 | | (lnum < end_lnum ? TP_FLAG_CONT_NEXT : 0) |
| 299 | | ((type->pt_flags & PT_FLAG_INS_START_INCL) |
| 300 | ? TP_FLAG_START_INCL : 0); |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 301 | mch_memmove(newprops + i * sizeof(textprop_T), &tmp_prop, |
| 302 | sizeof(textprop_T)); |
| 303 | |
| 304 | if (i < proplen) |
| 305 | mch_memmove(newprops + (i + 1) * sizeof(textprop_T), |
| 306 | props + i * sizeof(textprop_T), |
| 307 | sizeof(textprop_T) * (proplen - i)); |
| 308 | |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 309 | if (buf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED)) |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 310 | vim_free(buf->b_ml.ml_line_ptr); |
| 311 | buf->b_ml.ml_line_ptr = newtext; |
| 312 | buf->b_ml.ml_line_len += sizeof(textprop_T); |
| 313 | buf->b_ml.ml_flags |= ML_LINE_DIRTY; |
| 314 | } |
| 315 | |
Bram Moolenaar | 326c5d3 | 2022-08-12 13:05:49 +0100 | [diff] [blame] | 316 | changed_line_display_buf(buf); |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 317 | changed_lines_buf(buf, start_lnum, end_lnum + 1, 0); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 318 | res = OK; |
| 319 | |
| 320 | theend: |
| 321 | vim_free(text); |
| 322 | return res; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* |
| 326 | * prop_add_list() |
| 327 | * First argument specifies the text property: |
| 328 | * {'type': <str>, 'id': <num>, 'bufnr': <num>} |
| 329 | * Second argument is a List where each item is a List with the following |
| 330 | * entries: [lnum, start_col, end_col] |
| 331 | */ |
| 332 | void |
| 333 | f_prop_add_list(typval_T *argvars, typval_T *rettv UNUSED) |
| 334 | { |
| 335 | dict_T *dict; |
| 336 | char_u *type_name; |
| 337 | buf_T *buf = curbuf; |
| 338 | int id = 0; |
| 339 | listitem_T *li; |
| 340 | list_T *pos_list; |
| 341 | linenr_T start_lnum; |
| 342 | colnr_T start_col; |
| 343 | linenr_T end_lnum; |
| 344 | colnr_T end_col; |
| 345 | int error = FALSE; |
| 346 | |
| 347 | if (check_for_dict_arg(argvars, 0) == FAIL |
| 348 | || check_for_list_arg(argvars, 1) == FAIL) |
| 349 | return; |
| 350 | |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 351 | if (check_for_nonnull_list_arg(argvars, 1) == FAIL) |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 352 | return; |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 353 | |
| 354 | dict = argvars[0].vval.v_dict; |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 355 | if (dict == NULL || !dict_has_key(dict, "type")) |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 356 | { |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 357 | emsg(_(e_missing_property_type_name)); |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 358 | return; |
| 359 | } |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 360 | type_name = dict_get_string(dict, "type", FALSE); |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 361 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 362 | if (dict_has_key(dict, "id")) |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 363 | id = dict_get_number(dict, "id"); |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 364 | |
| 365 | if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL) |
| 366 | return; |
| 367 | |
Paul Ollis | 4c3d21a | 2022-05-24 21:26:37 +0100 | [diff] [blame] | 368 | // This must be done _before_ we start adding properties because property |
| 369 | // changes trigger buffer (memline) reorganisation, which needs this flag |
| 370 | // to be correctly set. |
| 371 | buf->b_has_textprop = TRUE; // this is never reset |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 372 | FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li) |
| 373 | { |
| 374 | if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL) |
| 375 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 376 | emsg(_(e_list_required)); |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 377 | return; |
| 378 | } |
| 379 | |
| 380 | pos_list = li->li_tv.vval.v_list; |
| 381 | start_lnum = list_find_nr(pos_list, 0L, &error); |
| 382 | start_col = list_find_nr(pos_list, 1L, &error); |
| 383 | end_lnum = list_find_nr(pos_list, 2L, &error); |
| 384 | end_col = list_find_nr(pos_list, 3L, &error); |
| 385 | if (error || start_lnum <= 0 || start_col <= 0 |
| 386 | || end_lnum <= 0 || end_col <= 0) |
| 387 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 388 | emsg(_(e_invalid_argument)); |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 389 | return; |
| 390 | } |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 391 | if (prop_add_one(buf, type_name, id, NULL, 0, 0, start_lnum, end_lnum, |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 392 | start_col, end_col) == FAIL) |
| 393 | return; |
| 394 | } |
| 395 | |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 396 | redraw_buf_later(buf, UPD_VALID); |
Yegappan Lakshmanan | ccfb7c6 | 2021-08-16 21:39:09 +0200 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 400 | * Get the next ID to use for a textprop with text in buffer "buf". |
| 401 | */ |
| 402 | static int |
| 403 | get_textprop_id(buf_T *buf) |
| 404 | { |
| 405 | // TODO: recycle deleted entries |
| 406 | return -(buf->b_textprop_text.ga_len + 1); |
| 407 | } |
| 408 | |
| 409 | /* |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 410 | * Shared between prop_add() and popup_create(). |
| 411 | * "dict_arg" is the function argument of a dict containing "bufnr". |
| 412 | * it is NULL for popup_create(). |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 413 | * Returns the "id" used for "text" or zero. |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 414 | */ |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 415 | int |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 416 | prop_add_common( |
| 417 | linenr_T start_lnum, |
| 418 | colnr_T start_col, |
| 419 | dict_T *dict, |
| 420 | buf_T *default_buf, |
| 421 | typval_T *dict_arg) |
| 422 | { |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 423 | linenr_T end_lnum; |
| 424 | colnr_T end_col; |
| 425 | char_u *type_name; |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 426 | buf_T *buf = default_buf; |
| 427 | int id = 0; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 428 | char_u *text = NULL; |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 429 | int text_padding_left = 0; |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 430 | int flags = 0; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 431 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 432 | if (dict == NULL || !dict_has_key(dict, "type")) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 433 | { |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 434 | emsg(_(e_missing_property_type_name)); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 435 | goto theend; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 436 | } |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 437 | type_name = dict_get_string(dict, "type", FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 438 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 439 | if (dict_has_key(dict, "end_lnum")) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 440 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 441 | end_lnum = dict_get_number(dict, "end_lnum"); |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 442 | if (end_lnum < start_lnum) |
| 443 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 444 | semsg(_(e_invalid_value_for_argument_str), "end_lnum"); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 445 | goto theend; |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 446 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 447 | } |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 448 | else |
| 449 | end_lnum = start_lnum; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 450 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 451 | if (dict_has_key(dict, "length")) |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 452 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 453 | long length = dict_get_number(dict, "length"); |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 454 | |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 455 | if (length < 0 || end_lnum > start_lnum) |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 456 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 457 | semsg(_(e_invalid_value_for_argument_str), "length"); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 458 | goto theend; |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 459 | } |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 460 | end_col = start_col + length; |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 461 | } |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 462 | else if (dict_has_key(dict, "end_col")) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 463 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 464 | end_col = dict_get_number(dict, "end_col"); |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 465 | if (end_col <= 0) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 466 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 467 | semsg(_(e_invalid_value_for_argument_str), "end_col"); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 468 | goto theend; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 469 | } |
| 470 | } |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 471 | else if (start_lnum == end_lnum) |
| 472 | end_col = start_col; |
| 473 | else |
| 474 | end_col = 1; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 475 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 476 | if (dict_has_key(dict, "id")) |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 477 | id = dict_get_number(dict, "id"); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 478 | |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 479 | if (dict_has_key(dict, "text")) |
| 480 | { |
| 481 | text = dict_get_string(dict, "text", TRUE); |
| 482 | if (text == NULL) |
| 483 | goto theend; |
| 484 | // use a default length of 1 to make multiple props show up |
| 485 | end_col = start_col + 1; |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 486 | |
| 487 | if (dict_has_key(dict, "text_align")) |
| 488 | { |
| 489 | char_u *p = dict_get_string(dict, "text_align", FALSE); |
| 490 | |
| 491 | if (p == NULL) |
| 492 | goto theend; |
Bram Moolenaar | 82b14c1 | 2022-08-10 19:50:47 +0100 | [diff] [blame] | 493 | if (start_col != 0) |
| 494 | { |
| 495 | emsg(_(e_can_only_use_text_align_when_column_is_zero)); |
| 496 | goto theend; |
| 497 | } |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 498 | if (STRCMP(p, "right") == 0) |
| 499 | flags |= TP_FLAG_ALIGN_RIGHT; |
| 500 | else if (STRCMP(p, "below") == 0) |
| 501 | flags |= TP_FLAG_ALIGN_BELOW; |
| 502 | else if (STRCMP(p, "after") != 0) |
| 503 | { |
| 504 | semsg(_(e_invalid_value_for_argument_str_str), "text_align", p); |
| 505 | goto theend; |
| 506 | } |
| 507 | } |
| 508 | |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 509 | if (dict_has_key(dict, "text_padding_left")) |
| 510 | { |
| 511 | text_padding_left = dict_get_number(dict, "text_padding_left"); |
| 512 | if (text_padding_left < 0) |
| 513 | { |
| 514 | semsg(_(e_argument_must_be_positive_str), "text_padding_left"); |
| 515 | goto theend; |
| 516 | } |
| 517 | } |
| 518 | |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 519 | if (dict_has_key(dict, "text_wrap")) |
| 520 | { |
| 521 | char_u *p = dict_get_string(dict, "text_wrap", FALSE); |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 522 | |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 523 | if (p == NULL) |
| 524 | goto theend; |
| 525 | if (STRCMP(p, "wrap") == 0) |
| 526 | flags |= TP_FLAG_WRAP; |
| 527 | else if (STRCMP(p, "truncate") != 0) |
| 528 | { |
| 529 | semsg(_(e_invalid_value_for_argument_str_str), "text_wrap", p); |
| 530 | goto theend; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // Column must be 1 or more for a normal text property; when "text" is |
| 536 | // present zero means it goes after the line. |
| 537 | if (start_col < (text == NULL ? 1 : 0)) |
| 538 | { |
| 539 | semsg(_(e_invalid_column_number_nr), (long)start_col); |
| 540 | goto theend; |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 541 | } |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 542 | if (start_col > 0 && text_padding_left > 0) |
| 543 | { |
| 544 | emsg(_(e_can_only_use_left_padding_when_column_is_zero)); |
| 545 | goto theend; |
| 546 | } |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 547 | |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 548 | if (dict_arg != NULL && get_bufnr_from_arg(dict_arg, &buf) == FAIL) |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 549 | goto theend; |
| 550 | |
| 551 | if (id < 0 && buf->b_textprop_text.ga_len > 0) |
| 552 | { |
| 553 | emsg(_(e_cannot_use_negative_id_after_adding_textprop_with_text)); |
| 554 | goto theend; |
| 555 | } |
| 556 | if (text != NULL) |
| 557 | id = get_textprop_id(buf); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 558 | |
Paul Ollis | 4c3d21a | 2022-05-24 21:26:37 +0100 | [diff] [blame] | 559 | // This must be done _before_ we add the property because property changes |
| 560 | // trigger buffer (memline) reorganisation, which needs this flag to be |
| 561 | // correctly set. |
| 562 | buf->b_has_textprop = TRUE; // this is never reset |
| 563 | |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 564 | prop_add_one(buf, type_name, id, text, text_padding_left, flags, |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 565 | start_lnum, end_lnum, start_col, end_col); |
| 566 | text = NULL; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 567 | |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 568 | redraw_buf_later(buf, UPD_VALID); |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 569 | |
| 570 | theend: |
| 571 | vim_free(text); |
| 572 | return id; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 576 | * Fetch the text properties for line "lnum" in buffer "buf". |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 577 | * Returns the number of text properties and, when non-zero, a pointer to the |
| 578 | * first one in "props" (note that it is not aligned, therefore the char_u |
| 579 | * pointer). |
| 580 | */ |
| 581 | int |
| 582 | get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change) |
| 583 | { |
| 584 | char_u *text; |
| 585 | size_t textlen; |
| 586 | size_t proplen; |
| 587 | |
Bram Moolenaar | b413d2e | 2018-12-25 23:15:46 +0100 | [diff] [blame] | 588 | // Be quick when no text property types have been defined or the buffer, |
| 589 | // unless we are adding one. |
Bram Moolenaar | d79eef2 | 2019-05-24 20:41:55 +0200 | [diff] [blame] | 590 | 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] | 591 | return 0; |
| 592 | |
| 593 | // Fetch the line to get the ml_line_len field updated. |
| 594 | text = ml_get_buf(buf, lnum, will_change); |
| 595 | textlen = STRLEN(text) + 1; |
| 596 | proplen = buf->b_ml.ml_line_len - textlen; |
Bram Moolenaar | 38ea273 | 2022-08-07 22:04:56 +0100 | [diff] [blame] | 597 | if (proplen == 0) |
| 598 | return 0; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 599 | if (proplen % sizeof(textprop_T) != 0) |
| 600 | { |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 601 | iemsg(_(e_text_property_info_corrupted)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 602 | return 0; |
| 603 | } |
Bram Moolenaar | 38ea273 | 2022-08-07 22:04:56 +0100 | [diff] [blame] | 604 | *props = text + textlen; |
Bram Moolenaar | 4efe73b | 2018-12-16 14:37:39 +0100 | [diff] [blame] | 605 | return (int)(proplen / sizeof(textprop_T)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 606 | } |
| 607 | |
Bram Moolenaar | 4d91d34 | 2022-08-06 13:48:20 +0100 | [diff] [blame] | 608 | /* |
| 609 | * Return the number of text properties with "below" alignment in line "lnum". |
Bram Moolenaar | 8f369fb | 2022-08-13 19:35:05 +0100 | [diff] [blame] | 610 | * A "right" aligned property also goes below after a "below" or other "right" |
| 611 | * aligned property. |
Bram Moolenaar | 4d91d34 | 2022-08-06 13:48:20 +0100 | [diff] [blame] | 612 | */ |
| 613 | int |
| 614 | prop_count_below(buf_T *buf, linenr_T lnum) |
| 615 | { |
| 616 | char_u *props; |
| 617 | int count = get_text_props(buf, lnum, &props, FALSE); |
| 618 | int result = 0; |
| 619 | textprop_T prop; |
| 620 | int i; |
Bram Moolenaar | 8f369fb | 2022-08-13 19:35:05 +0100 | [diff] [blame] | 621 | int next_right_goes_below = FALSE; |
Bram Moolenaar | 4d91d34 | 2022-08-06 13:48:20 +0100 | [diff] [blame] | 622 | |
| 623 | if (count == 0) |
| 624 | return 0; |
| 625 | for (i = 0; i < count; ++i) |
| 626 | { |
| 627 | mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop)); |
Bram Moolenaar | 8f369fb | 2022-08-13 19:35:05 +0100 | [diff] [blame] | 628 | if (prop.tp_col == MAXCOL) |
| 629 | { |
| 630 | if ((prop.tp_flags & TP_FLAG_ALIGN_BELOW) |
| 631 | || (next_right_goes_below |
| 632 | && (prop.tp_flags & TP_FLAG_ALIGN_RIGHT))) |
| 633 | { |
| 634 | next_right_goes_below = TRUE; |
| 635 | ++result; |
| 636 | } |
| 637 | else if (prop.tp_flags & TP_FLAG_ALIGN_RIGHT) |
| 638 | next_right_goes_below = TRUE; |
| 639 | } |
Bram Moolenaar | 4d91d34 | 2022-08-06 13:48:20 +0100 | [diff] [blame] | 640 | } |
| 641 | return result; |
| 642 | } |
| 643 | |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 644 | /** |
| 645 | * Return the number of text properties on line "lnum" in the current buffer. |
| 646 | * When "only_starting" is true only text properties starting in this line will |
| 647 | * be considered. |
Bram Moolenaar | 7d0f7e9 | 2022-08-06 17:10:57 +0100 | [diff] [blame] | 648 | * When "last_line" is FALSE then text properties after the line are not |
| 649 | * counted. |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 650 | */ |
| 651 | int |
Bram Moolenaar | e175dc6 | 2022-08-01 22:18:50 +0100 | [diff] [blame] | 652 | count_props(linenr_T lnum, int only_starting, int last_line) |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 653 | { |
| 654 | char_u *props; |
| 655 | int proplen = get_text_props(curbuf, lnum, &props, 0); |
| 656 | int result = proplen; |
| 657 | int i; |
| 658 | textprop_T prop; |
| 659 | |
Bram Moolenaar | e175dc6 | 2022-08-01 22:18:50 +0100 | [diff] [blame] | 660 | for (i = 0; i < proplen; ++i) |
| 661 | { |
| 662 | mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop)); |
Bram Moolenaar | 7d0f7e9 | 2022-08-06 17:10:57 +0100 | [diff] [blame] | 663 | // A prop is dropped when in the first line and it continues from the |
Bram Moolenaar | e175dc6 | 2022-08-01 22:18:50 +0100 | [diff] [blame] | 664 | // previous line, or when not in the last line and it is virtual text |
| 665 | // after the line. |
| 666 | if ((only_starting && (prop.tp_flags & TP_FLAG_CONT_PREV)) |
| 667 | || (!last_line && prop.tp_col == MAXCOL)) |
| 668 | --result; |
| 669 | } |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 670 | return result; |
| 671 | } |
| 672 | |
Bram Moolenaar | f396ce8 | 2022-08-23 18:39:37 +0100 | [diff] [blame] | 673 | static textprop_T *text_prop_compare_props; |
| 674 | static buf_T *text_prop_compare_buf; |
| 675 | |
| 676 | /* |
| 677 | * Function passed to qsort() to sort text properties. |
| 678 | * Return 1 if "s1" has priority over "s2", -1 if the other way around, zero if |
| 679 | * both have the same priority. |
| 680 | */ |
| 681 | static int |
| 682 | text_prop_compare(const void *s1, const void *s2) |
| 683 | { |
| 684 | int idx1, idx2; |
| 685 | textprop_T *tp1, *tp2; |
| 686 | proptype_T *pt1, *pt2; |
| 687 | colnr_T col1, col2; |
| 688 | |
| 689 | idx1 = *(int *)s1; |
| 690 | idx2 = *(int *)s2; |
| 691 | tp1 = &text_prop_compare_props[idx1]; |
| 692 | tp2 = &text_prop_compare_props[idx2]; |
| 693 | col1 = tp1->tp_col; |
| 694 | col2 = tp2->tp_col; |
| 695 | if (col1 == MAXCOL && col2 == MAXCOL) |
| 696 | { |
| 697 | int flags1 = 0; |
| 698 | int flags2 = 0; |
| 699 | |
| 700 | // both props add text are after the line, order on 0: after (default), |
| 701 | // 1: right, 2: below (comes last) |
| 702 | if (tp1->tp_flags & TP_FLAG_ALIGN_RIGHT) |
| 703 | flags1 = 1; |
| 704 | if (tp1->tp_flags & TP_FLAG_ALIGN_BELOW) |
| 705 | flags1 = 2; |
| 706 | if (tp2->tp_flags & TP_FLAG_ALIGN_RIGHT) |
| 707 | flags2 = 1; |
| 708 | if (tp2->tp_flags & TP_FLAG_ALIGN_BELOW) |
| 709 | flags2 = 2; |
| 710 | if (flags1 != flags2) |
| 711 | return flags1 < flags2 ? 1 : -1; |
| 712 | } |
| 713 | |
| 714 | // property that inserts text has priority over one that doesn't |
| 715 | if ((tp1->tp_id < 0) != (tp2->tp_id < 0)) |
| 716 | return tp1->tp_id < 0 ? 1 : -1; |
| 717 | |
| 718 | // check highest priority, defined by the type |
| 719 | pt1 = text_prop_type_by_id(text_prop_compare_buf, tp1->tp_type); |
| 720 | pt2 = text_prop_type_by_id(text_prop_compare_buf, tp2->tp_type); |
| 721 | if (pt1 != pt2) |
| 722 | { |
| 723 | if (pt1 == NULL) |
| 724 | return -1; |
| 725 | if (pt2 == NULL) |
| 726 | return 1; |
| 727 | if (pt1->pt_priority != pt2->pt_priority) |
| 728 | return pt1->pt_priority > pt2->pt_priority ? 1 : -1; |
| 729 | } |
| 730 | |
| 731 | // same priority, one that starts first wins |
| 732 | if (col1 != col2) |
| 733 | return col1 < col2 ? 1 : -1; |
| 734 | |
| 735 | // for a property with text the id can be used as tie breaker |
| 736 | if (tp1->tp_id < 0) |
| 737 | return tp1->tp_id > tp2->tp_id ? 1 : -1; |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Sort "count" text properties using an array if indexes "idxs" into the list |
| 744 | * of text props "props" for buffer "buf". |
| 745 | */ |
| 746 | void |
| 747 | sort_text_props( |
| 748 | buf_T *buf, |
| 749 | textprop_T *props, |
| 750 | int *idxs, |
| 751 | int count) |
| 752 | { |
| 753 | text_prop_compare_buf = buf; |
| 754 | text_prop_compare_props = props; |
| 755 | qsort((void *)idxs, (size_t)count, sizeof(int), text_prop_compare); |
| 756 | } |
| 757 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 758 | /* |
Bram Moolenaar | 12034e2 | 2019-08-25 22:25:02 +0200 | [diff] [blame] | 759 | * Find text property "type_id" in the visible lines of window "wp". |
| 760 | * Match "id" when it is > 0. |
| 761 | * Returns FAIL when not found. |
| 762 | */ |
| 763 | int |
| 764 | find_visible_prop(win_T *wp, int type_id, int id, textprop_T *prop, |
| 765 | linenr_T *found_lnum) |
| 766 | { |
| 767 | linenr_T lnum; |
| 768 | char_u *props; |
| 769 | int count; |
| 770 | int i; |
| 771 | |
| 772 | // w_botline may not have been updated yet. |
Bram Moolenaar | 23999d7 | 2020-12-23 14:36:00 +0100 | [diff] [blame] | 773 | validate_botline_win(wp); |
Bram Moolenaar | 12034e2 | 2019-08-25 22:25:02 +0200 | [diff] [blame] | 774 | for (lnum = wp->w_topline; lnum < wp->w_botline; ++lnum) |
| 775 | { |
| 776 | count = get_text_props(wp->w_buffer, lnum, &props, FALSE); |
| 777 | for (i = 0; i < count; ++i) |
| 778 | { |
| 779 | mch_memmove(prop, props + i * sizeof(textprop_T), |
| 780 | sizeof(textprop_T)); |
| 781 | if (prop->tp_type == type_id && (id <= 0 || prop->tp_id == id)) |
| 782 | { |
| 783 | *found_lnum = lnum; |
| 784 | return OK; |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | return FAIL; |
| 789 | } |
| 790 | |
| 791 | /* |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 792 | * Set the text properties for line "lnum" to "props" with length "len". |
| 793 | * If "len" is zero text properties are removed, "props" is not used. |
| 794 | * Any existing text properties are dropped. |
| 795 | * Only works for the current buffer. |
| 796 | */ |
| 797 | static void |
| 798 | set_text_props(linenr_T lnum, char_u *props, int len) |
| 799 | { |
Bram Moolenaar | 8aef43b | 2019-01-08 20:14:35 +0100 | [diff] [blame] | 800 | char_u *text; |
| 801 | char_u *newtext; |
| 802 | int textlen; |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 803 | |
| 804 | text = ml_get(lnum); |
Bram Moolenaar | 8aef43b | 2019-01-08 20:14:35 +0100 | [diff] [blame] | 805 | textlen = (int)STRLEN(text) + 1; |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 806 | newtext = alloc(textlen + len); |
| 807 | if (newtext == NULL) |
| 808 | return; |
| 809 | mch_memmove(newtext, text, textlen); |
| 810 | if (len > 0) |
| 811 | mch_memmove(newtext + textlen, props, len); |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 812 | if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED)) |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 813 | vim_free(curbuf->b_ml.ml_line_ptr); |
| 814 | curbuf->b_ml.ml_line_ptr = newtext; |
| 815 | curbuf->b_ml.ml_line_len = textlen + len; |
| 816 | curbuf->b_ml.ml_flags |= ML_LINE_DIRTY; |
| 817 | } |
| 818 | |
Bram Moolenaar | 213bbaf | 2022-08-05 19:46:48 +0100 | [diff] [blame] | 819 | /* |
Bram Moolenaar | 7d0f7e9 | 2022-08-06 17:10:57 +0100 | [diff] [blame] | 820 | * Add "text_props" with "text_prop_count" text properties to line "lnum". |
Bram Moolenaar | 213bbaf | 2022-08-05 19:46:48 +0100 | [diff] [blame] | 821 | */ |
| 822 | void |
| 823 | add_text_props(linenr_T lnum, textprop_T *text_props, int text_prop_count) |
| 824 | { |
| 825 | char_u *text; |
| 826 | char_u *newtext; |
| 827 | int proplen = text_prop_count * (int)sizeof(textprop_T); |
| 828 | |
| 829 | text = ml_get(lnum); |
| 830 | newtext = alloc(curbuf->b_ml.ml_line_len + proplen); |
| 831 | if (newtext == NULL) |
| 832 | return; |
| 833 | mch_memmove(newtext, text, curbuf->b_ml.ml_line_len); |
| 834 | mch_memmove(newtext + curbuf->b_ml.ml_line_len, text_props, proplen); |
| 835 | if (curbuf->b_ml.ml_flags & (ML_LINE_DIRTY | ML_ALLOCATED)) |
| 836 | vim_free(curbuf->b_ml.ml_line_ptr); |
| 837 | curbuf->b_ml.ml_line_ptr = newtext; |
| 838 | curbuf->b_ml.ml_line_len += proplen; |
| 839 | curbuf->b_ml.ml_flags |= ML_LINE_DIRTY; |
| 840 | } |
| 841 | |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 842 | /* |
| 843 | * Function passed to qsort() for sorting proptype_T on pt_id. |
| 844 | */ |
| 845 | static int |
| 846 | compare_pt(const void *s1, const void *s2) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 847 | { |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 848 | proptype_T *tp1 = *(proptype_T **)s1; |
| 849 | proptype_T *tp2 = *(proptype_T **)s2; |
| 850 | |
| 851 | return tp1->pt_id == tp2->pt_id ? 0 : tp1->pt_id < tp2->pt_id ? -1 : 1; |
| 852 | } |
| 853 | |
| 854 | static proptype_T * |
| 855 | find_type_by_id(hashtab_T *ht, proptype_T ***array, int id) |
| 856 | { |
| 857 | int low = 0; |
| 858 | int high; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 859 | |
Bram Moolenaar | 1024690 | 2022-08-08 17:08:05 +0100 | [diff] [blame] | 860 | if (ht == NULL || ht->ht_used == 0) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 861 | return NULL; |
| 862 | |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 863 | // Make the loopup faster by creating an array with pointers to |
| 864 | // hashtable entries, sorted on pt_id. |
| 865 | if (*array == NULL) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 866 | { |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 867 | long todo; |
| 868 | hashitem_T *hi; |
| 869 | int i = 0; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 870 | |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 871 | *array = ALLOC_MULT(proptype_T *, ht->ht_used); |
| 872 | if (*array == NULL) |
| 873 | return NULL; |
| 874 | todo = (long)ht->ht_used; |
| 875 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 876 | { |
| 877 | if (!HASHITEM_EMPTY(hi)) |
| 878 | { |
| 879 | (*array)[i++] = HI2PT(hi); |
| 880 | --todo; |
| 881 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 882 | } |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 883 | qsort((void *)*array, ht->ht_used, sizeof(proptype_T *), compare_pt); |
| 884 | } |
| 885 | |
| 886 | // binary search in the sorted array |
| 887 | high = ht->ht_used; |
| 888 | while (high > low) |
| 889 | { |
| 890 | int m = (high + low) / 2; |
| 891 | |
| 892 | if ((*array)[m]->pt_id == id) |
| 893 | return (*array)[m]; |
| 894 | if ((*array)[m]->pt_id > id) |
| 895 | high = m; |
| 896 | else |
| 897 | low = m + 1; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 898 | } |
| 899 | return NULL; |
| 900 | } |
| 901 | |
| 902 | /* |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 903 | * Fill 'dict' with text properties in 'prop'. |
| 904 | */ |
| 905 | static void |
| 906 | prop_fill_dict(dict_T *dict, textprop_T *prop, buf_T *buf) |
| 907 | { |
| 908 | proptype_T *pt; |
Martin Tournoij | e2390c7 | 2021-07-28 13:30:16 +0200 | [diff] [blame] | 909 | int buflocal = TRUE; |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 910 | |
| 911 | dict_add_number(dict, "col", prop->tp_col); |
| 912 | dict_add_number(dict, "length", prop->tp_len); |
| 913 | dict_add_number(dict, "id", prop->tp_id); |
| 914 | dict_add_number(dict, "start", !(prop->tp_flags & TP_FLAG_CONT_PREV)); |
| 915 | dict_add_number(dict, "end", !(prop->tp_flags & TP_FLAG_CONT_NEXT)); |
Martin Tournoij | e2390c7 | 2021-07-28 13:30:16 +0200 | [diff] [blame] | 916 | |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 917 | pt = find_type_by_id(buf->b_proptypes, &buf->b_proparray, prop->tp_type); |
Martin Tournoij | e2390c7 | 2021-07-28 13:30:16 +0200 | [diff] [blame] | 918 | if (pt == NULL) |
| 919 | { |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 920 | pt = find_type_by_id(global_proptypes, &global_proparray, |
| 921 | prop->tp_type); |
Martin Tournoij | e2390c7 | 2021-07-28 13:30:16 +0200 | [diff] [blame] | 922 | buflocal = FALSE; |
| 923 | } |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 924 | if (pt != NULL) |
| 925 | dict_add_string(dict, "type", pt->pt_name); |
Martin Tournoij | e2390c7 | 2021-07-28 13:30:16 +0200 | [diff] [blame] | 926 | |
| 927 | if (buflocal) |
| 928 | dict_add_number(dict, "type_bufnr", buf->b_fnum); |
| 929 | else |
| 930 | dict_add_number(dict, "type_bufnr", 0); |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | /* |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 934 | * Find a property type by ID in "buf" or globally. |
| 935 | * Returns NULL if not found. |
| 936 | */ |
| 937 | proptype_T * |
| 938 | text_prop_type_by_id(buf_T *buf, int id) |
| 939 | { |
| 940 | proptype_T *type; |
| 941 | |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 942 | type = find_type_by_id(buf->b_proptypes, &buf->b_proparray, id); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 943 | if (type == NULL) |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 944 | type = find_type_by_id(global_proptypes, &global_proparray, id); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 945 | return type; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * prop_clear({lnum} [, {lnum_end} [, {bufnr}]]) |
| 950 | */ |
| 951 | void |
| 952 | f_prop_clear(typval_T *argvars, typval_T *rettv UNUSED) |
| 953 | { |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 954 | linenr_T start; |
| 955 | linenr_T end; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 956 | linenr_T lnum; |
| 957 | buf_T *buf = curbuf; |
Bram Moolenaar | da1dbed | 2021-03-22 19:43:34 +0100 | [diff] [blame] | 958 | int did_clear = FALSE; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 959 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 960 | if (in_vim9script() |
| 961 | && (check_for_number_arg(argvars, 0) == FAIL |
| 962 | || check_for_opt_number_arg(argvars, 1) == FAIL |
| 963 | || (argvars[1].v_type != VAR_UNKNOWN |
| 964 | && check_for_opt_dict_arg(argvars, 2) == FAIL))) |
| 965 | return; |
| 966 | |
| 967 | start = tv_get_number(&argvars[0]); |
| 968 | end = start; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 969 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 970 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 971 | end = tv_get_number(&argvars[1]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 972 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 973 | { |
| 974 | if (get_bufnr_from_arg(&argvars[2], &buf) == FAIL) |
| 975 | return; |
| 976 | } |
| 977 | } |
| 978 | if (start < 1 || end < 1) |
| 979 | { |
Bram Moolenaar | 108010a | 2021-06-27 22:03:33 +0200 | [diff] [blame] | 980 | emsg(_(e_invalid_range)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 981 | return; |
| 982 | } |
| 983 | |
| 984 | for (lnum = start; lnum <= end; ++lnum) |
| 985 | { |
| 986 | char_u *text; |
| 987 | size_t len; |
| 988 | |
| 989 | if (lnum > buf->b_ml.ml_line_count) |
| 990 | break; |
| 991 | text = ml_get_buf(buf, lnum, FALSE); |
| 992 | len = STRLEN(text) + 1; |
| 993 | if ((size_t)buf->b_ml.ml_line_len > len) |
| 994 | { |
Bram Moolenaar | da1dbed | 2021-03-22 19:43:34 +0100 | [diff] [blame] | 995 | did_clear = TRUE; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 996 | if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY)) |
| 997 | { |
| 998 | char_u *newtext = vim_strsave(text); |
| 999 | |
| 1000 | // need to allocate the line now |
| 1001 | if (newtext == NULL) |
| 1002 | return; |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 1003 | if (buf->b_ml.ml_flags & ML_ALLOCATED) |
| 1004 | vim_free(buf->b_ml.ml_line_ptr); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1005 | buf->b_ml.ml_line_ptr = newtext; |
| 1006 | buf->b_ml.ml_flags |= ML_LINE_DIRTY; |
| 1007 | } |
Bram Moolenaar | 4efe73b | 2018-12-16 14:37:39 +0100 | [diff] [blame] | 1008 | buf->b_ml.ml_line_len = (int)len; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1009 | } |
| 1010 | } |
Bram Moolenaar | da1dbed | 2021-03-22 19:43:34 +0100 | [diff] [blame] | 1011 | if (did_clear) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1012 | redraw_buf_later(buf, UPD_NOT_VALID); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /* |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1016 | * prop_find({props} [, {direction}]) |
| 1017 | */ |
| 1018 | void |
| 1019 | f_prop_find(typval_T *argvars, typval_T *rettv) |
| 1020 | { |
| 1021 | pos_T *cursor = &curwin->w_cursor; |
| 1022 | dict_T *dict; |
| 1023 | buf_T *buf = curbuf; |
| 1024 | dictitem_T *di; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 1025 | int lnum_start; |
| 1026 | int start_pos_has_prop = 0; |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1027 | int seen_end = FALSE; |
Bram Moolenaar | 0d4d9ee | 2021-08-01 19:28:15 +0200 | [diff] [blame] | 1028 | int id = 0; |
| 1029 | int id_found = FALSE; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 1030 | int type_id = -1; |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1031 | int skipstart = FALSE; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 1032 | int lnum = -1; |
| 1033 | int col = -1; |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1034 | int dir = FORWARD; // FORWARD == 1, BACKWARD == -1 |
Bram Moolenaar | 24f21fd | 2021-03-27 22:07:29 +0100 | [diff] [blame] | 1035 | int both; |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1036 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1037 | if (in_vim9script() |
| 1038 | && (check_for_dict_arg(argvars, 0) == FAIL |
| 1039 | || check_for_opt_string_arg(argvars, 1) == FAIL)) |
| 1040 | return; |
| 1041 | |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1042 | if (check_for_nonnull_dict_arg(argvars, 0) == FAIL) |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1043 | return; |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1044 | dict = argvars[0].vval.v_dict; |
| 1045 | |
| 1046 | if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL) |
| 1047 | return; |
| 1048 | if (buf->b_ml.ml_mfp == NULL) |
| 1049 | return; |
| 1050 | |
| 1051 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1052 | { |
| 1053 | char_u *dir_s = tv_get_string(&argvars[1]); |
| 1054 | |
| 1055 | if (*dir_s == 'b') |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1056 | dir = BACKWARD; |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1057 | else if (*dir_s != 'f') |
| 1058 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1059 | emsg(_(e_invalid_argument)); |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1060 | return; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | di = dict_find(dict, (char_u *)"lnum", -1); |
| 1065 | if (di != NULL) |
| 1066 | lnum = tv_get_number(&di->di_tv); |
| 1067 | |
| 1068 | di = dict_find(dict, (char_u *)"col", -1); |
| 1069 | if (di != NULL) |
| 1070 | col = tv_get_number(&di->di_tv); |
| 1071 | |
| 1072 | if (lnum == -1) |
| 1073 | { |
| 1074 | lnum = cursor->lnum; |
| 1075 | col = cursor->col + 1; |
| 1076 | } |
| 1077 | else if (col == -1) |
| 1078 | col = 1; |
| 1079 | |
| 1080 | if (lnum < 1 || lnum > buf->b_ml.ml_line_count) |
| 1081 | { |
Bram Moolenaar | 108010a | 2021-06-27 22:03:33 +0200 | [diff] [blame] | 1082 | emsg(_(e_invalid_range)); |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1083 | return; |
| 1084 | } |
| 1085 | |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1086 | skipstart = dict_get_bool(dict, "skipstart", 0); |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1087 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 1088 | if (dict_has_key(dict, "id")) |
Bram Moolenaar | 8e3fc13 | 2021-07-31 18:33:57 +0200 | [diff] [blame] | 1089 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1090 | id = dict_get_number(dict, "id"); |
Bram Moolenaar | e041dde | 2021-08-01 21:30:12 +0200 | [diff] [blame] | 1091 | id_found = TRUE; |
Bram Moolenaar | 8e3fc13 | 2021-07-31 18:33:57 +0200 | [diff] [blame] | 1092 | } |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 1093 | if (dict_has_key(dict, "type")) |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1094 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1095 | char_u *name = dict_get_string(dict, "type", FALSE); |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1096 | proptype_T *type = lookup_prop_type(name, buf); |
| 1097 | |
| 1098 | if (type == NULL) |
| 1099 | return; |
| 1100 | type_id = type->pt_id; |
| 1101 | } |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1102 | both = dict_get_bool(dict, "both", FALSE); |
Bram Moolenaar | 0d4d9ee | 2021-08-01 19:28:15 +0200 | [diff] [blame] | 1103 | if (!id_found && type_id == -1) |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1104 | { |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 1105 | emsg(_(e_need_at_least_one_of_id_or_type)); |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1106 | return; |
| 1107 | } |
Bram Moolenaar | 0d4d9ee | 2021-08-01 19:28:15 +0200 | [diff] [blame] | 1108 | if (both && (!id_found || type_id == -1)) |
Bram Moolenaar | 24f21fd | 2021-03-27 22:07:29 +0100 | [diff] [blame] | 1109 | { |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1110 | emsg(_(e_need_id_and_type_or_types_with_both)); |
Bram Moolenaar | 24f21fd | 2021-03-27 22:07:29 +0100 | [diff] [blame] | 1111 | return; |
| 1112 | } |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1113 | |
| 1114 | lnum_start = lnum; |
| 1115 | |
| 1116 | if (rettv_dict_alloc(rettv) == FAIL) |
| 1117 | return; |
| 1118 | |
| 1119 | while (1) |
| 1120 | { |
| 1121 | char_u *text = ml_get_buf(buf, lnum, FALSE); |
| 1122 | size_t textlen = STRLEN(text) + 1; |
| 1123 | int count = (int)((buf->b_ml.ml_line_len - textlen) |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 1124 | / sizeof(textprop_T)); |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1125 | int i; |
| 1126 | textprop_T prop; |
| 1127 | int prop_start; |
| 1128 | int prop_end; |
| 1129 | |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1130 | for (i = dir == BACKWARD ? count - 1 : 0; i >= 0 && i < count; i += dir) |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1131 | { |
| 1132 | mch_memmove(&prop, text + textlen + i * sizeof(textprop_T), |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1133 | sizeof(textprop_T)); |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1134 | |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1135 | // For the very first line try to find the first property before or |
| 1136 | // after `col`, depending on the search direction. |
Bram Moolenaar | 346f18e | 2020-03-13 21:36:40 +0100 | [diff] [blame] | 1137 | if (lnum == lnum_start) |
Bram Moolenaar | 965fd8d | 2020-03-14 07:46:40 +0100 | [diff] [blame] | 1138 | { |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1139 | if (dir == BACKWARD) |
Bram Moolenaar | 346f18e | 2020-03-13 21:36:40 +0100 | [diff] [blame] | 1140 | { |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1141 | if (prop.tp_col > col) |
| 1142 | continue; |
Bram Moolenaar | 346f18e | 2020-03-13 21:36:40 +0100 | [diff] [blame] | 1143 | } |
| 1144 | else if (prop.tp_col + prop.tp_len - (prop.tp_len != 0) < col) |
| 1145 | continue; |
Bram Moolenaar | 965fd8d | 2020-03-14 07:46:40 +0100 | [diff] [blame] | 1146 | } |
Bram Moolenaar | 24f21fd | 2021-03-27 22:07:29 +0100 | [diff] [blame] | 1147 | if (both ? prop.tp_id == id && prop.tp_type == type_id |
Bram Moolenaar | 0d4d9ee | 2021-08-01 19:28:15 +0200 | [diff] [blame] | 1148 | : (id_found && prop.tp_id == id) |
| 1149 | || prop.tp_type == type_id) |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1150 | { |
| 1151 | // Check if the starting position has text props. |
Bram Moolenaar | 66b9885 | 2020-03-11 19:15:52 +0100 | [diff] [blame] | 1152 | if (lnum_start == lnum |
| 1153 | && col >= prop.tp_col |
| 1154 | && (col <= prop.tp_col + prop.tp_len |
| 1155 | - (prop.tp_len != 0))) |
| 1156 | start_pos_has_prop = 1; |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1157 | |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1158 | // The property was not continued from last line, it starts on |
| 1159 | // this line. |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1160 | prop_start = !(prop.tp_flags & TP_FLAG_CONT_PREV); |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1161 | // The property does not continue on the next line, it ends on |
| 1162 | // this line. |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1163 | prop_end = !(prop.tp_flags & TP_FLAG_CONT_NEXT); |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1164 | if (!prop_start && prop_end && dir == FORWARD) |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1165 | seen_end = 1; |
| 1166 | |
| 1167 | // Skip lines without the start flag. |
| 1168 | if (!prop_start) |
| 1169 | { |
| 1170 | // Always search backwards for start when search started |
| 1171 | // on a prop and we're not skipping. |
| 1172 | if (start_pos_has_prop && !skipstart) |
LemonBoy | 9bd3ce2 | 2022-04-18 21:54:02 +0100 | [diff] [blame] | 1173 | dir = BACKWARD; |
Bram Moolenaar | 4da7a25 | 2020-09-02 19:59:00 +0200 | [diff] [blame] | 1174 | continue; |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | // If skipstart is true, skip the prop at start pos (even if |
| 1178 | // continued from another line). |
| 1179 | if (start_pos_has_prop && skipstart && !seen_end) |
| 1180 | { |
| 1181 | start_pos_has_prop = 0; |
Bram Moolenaar | 4da7a25 | 2020-09-02 19:59:00 +0200 | [diff] [blame] | 1182 | continue; |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1183 | } |
| 1184 | |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 1185 | prop_fill_dict(rettv->vval.v_dict, &prop, buf); |
| 1186 | dict_add_number(rettv->vval.v_dict, "lnum", lnum); |
| 1187 | |
| 1188 | return; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | if (dir > 0) |
| 1193 | { |
| 1194 | if (lnum >= buf->b_ml.ml_line_count) |
| 1195 | break; |
| 1196 | lnum++; |
| 1197 | } |
| 1198 | else |
| 1199 | { |
| 1200 | if (lnum <= 1) |
| 1201 | break; |
| 1202 | lnum--; |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | /* |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1208 | * Returns TRUE if 'type_or_id' is in the 'types_or_ids' list. |
| 1209 | */ |
| 1210 | static int |
| 1211 | prop_type_or_id_in_list(int *types_or_ids, int len, int type_or_id) |
| 1212 | { |
| 1213 | int i; |
| 1214 | |
| 1215 | for (i = 0; i < len; i++) |
| 1216 | if (types_or_ids[i] == type_or_id) |
| 1217 | return TRUE; |
| 1218 | |
| 1219 | return FALSE; |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * Return all the text properties in line 'lnum' in buffer 'buf' in 'retlist'. |
| 1224 | * If 'prop_types' is not NULL, then return only the text properties with |
| 1225 | * matching property type in the 'prop_types' array. |
| 1226 | * If 'prop_ids' is not NULL, then return only the text properties with |
| 1227 | * an identifier in the 'props_ids' array. |
| 1228 | * If 'add_lnum' is TRUE, then add the line number also to the text property |
| 1229 | * dictionary. |
| 1230 | */ |
| 1231 | static void |
| 1232 | get_props_in_line( |
| 1233 | buf_T *buf, |
| 1234 | linenr_T lnum, |
| 1235 | int *prop_types, |
| 1236 | int prop_types_len, |
| 1237 | int *prop_ids, |
| 1238 | int prop_ids_len, |
| 1239 | list_T *retlist, |
| 1240 | int add_lnum) |
| 1241 | { |
| 1242 | char_u *text = ml_get_buf(buf, lnum, FALSE); |
| 1243 | size_t textlen = STRLEN(text) + 1; |
| 1244 | int count; |
| 1245 | int i; |
| 1246 | textprop_T prop; |
| 1247 | |
| 1248 | count = (int)((buf->b_ml.ml_line_len - textlen) / sizeof(textprop_T)); |
| 1249 | for (i = 0; i < count; ++i) |
| 1250 | { |
| 1251 | mch_memmove(&prop, text + textlen + i * sizeof(textprop_T), |
| 1252 | sizeof(textprop_T)); |
| 1253 | if ((prop_types == NULL |
| 1254 | || prop_type_or_id_in_list(prop_types, prop_types_len, |
| 1255 | prop.tp_type)) |
Bram Moolenaar | 7f9969c | 2022-07-25 18:13:54 +0100 | [diff] [blame] | 1256 | && (prop_ids == NULL |
| 1257 | || prop_type_or_id_in_list(prop_ids, prop_ids_len, |
| 1258 | prop.tp_id))) |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1259 | { |
| 1260 | dict_T *d = dict_alloc(); |
| 1261 | |
| 1262 | if (d == NULL) |
| 1263 | break; |
| 1264 | prop_fill_dict(d, &prop, buf); |
| 1265 | if (add_lnum) |
| 1266 | dict_add_number(d, "lnum", lnum); |
| 1267 | list_append_dict(retlist, d); |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * Convert a List of property type names into an array of property type |
| 1274 | * identifiers. Returns a pointer to the allocated array. Returns NULL on |
| 1275 | * error. 'num_types' is set to the number of returned property types. |
| 1276 | */ |
| 1277 | static int * |
| 1278 | get_prop_types_from_names(list_T *l, buf_T *buf, int *num_types) |
| 1279 | { |
| 1280 | int *prop_types; |
| 1281 | listitem_T *li; |
| 1282 | int i; |
| 1283 | char_u *name; |
| 1284 | proptype_T *type; |
| 1285 | |
| 1286 | *num_types = 0; |
| 1287 | |
| 1288 | prop_types = ALLOC_MULT(int, list_len(l)); |
| 1289 | if (prop_types == NULL) |
| 1290 | return NULL; |
| 1291 | |
| 1292 | i = 0; |
| 1293 | FOR_ALL_LIST_ITEMS(l, li) |
| 1294 | { |
| 1295 | if (li->li_tv.v_type != VAR_STRING) |
| 1296 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1297 | emsg(_(e_string_required)); |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1298 | goto errret; |
| 1299 | } |
| 1300 | name = li->li_tv.vval.v_string; |
| 1301 | if (name == NULL) |
| 1302 | goto errret; |
| 1303 | |
| 1304 | type = lookup_prop_type(name, buf); |
| 1305 | if (type == NULL) |
| 1306 | goto errret; |
| 1307 | prop_types[i++] = type->pt_id; |
| 1308 | } |
| 1309 | |
| 1310 | *num_types = i; |
| 1311 | return prop_types; |
| 1312 | |
| 1313 | errret: |
| 1314 | VIM_CLEAR(prop_types); |
| 1315 | return NULL; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Convert a List of property identifiers into an array of property |
| 1320 | * identifiers. Returns a pointer to the allocated array. Returns NULL on |
| 1321 | * error. 'num_ids' is set to the number of returned property identifiers. |
| 1322 | */ |
| 1323 | static int * |
| 1324 | get_prop_ids_from_list(list_T *l, int *num_ids) |
| 1325 | { |
| 1326 | int *prop_ids; |
| 1327 | listitem_T *li; |
| 1328 | int i; |
| 1329 | int id; |
| 1330 | int error; |
| 1331 | |
| 1332 | *num_ids = 0; |
| 1333 | |
| 1334 | prop_ids = ALLOC_MULT(int, list_len(l)); |
| 1335 | if (prop_ids == NULL) |
| 1336 | return NULL; |
| 1337 | |
| 1338 | i = 0; |
| 1339 | FOR_ALL_LIST_ITEMS(l, li) |
| 1340 | { |
| 1341 | error = FALSE; |
| 1342 | id = tv_get_number_chk(&li->li_tv, &error); |
| 1343 | if (error) |
| 1344 | goto errret; |
| 1345 | |
| 1346 | prop_ids[i++] = id; |
| 1347 | } |
| 1348 | |
| 1349 | *num_ids = i; |
| 1350 | return prop_ids; |
| 1351 | |
| 1352 | errret: |
| 1353 | VIM_CLEAR(prop_ids); |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | |
| 1357 | /* |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1358 | * prop_list({lnum} [, {bufnr}]) |
| 1359 | */ |
| 1360 | void |
| 1361 | f_prop_list(typval_T *argvars, typval_T *rettv) |
| 1362 | { |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1363 | linenr_T lnum; |
| 1364 | linenr_T start_lnum; |
| 1365 | linenr_T end_lnum; |
| 1366 | buf_T *buf = curbuf; |
| 1367 | int add_lnum = FALSE; |
| 1368 | int *prop_types = NULL; |
| 1369 | int prop_types_len = 0; |
| 1370 | int *prop_ids = NULL; |
| 1371 | int prop_ids_len = 0; |
| 1372 | list_T *l; |
| 1373 | dictitem_T *di; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1374 | |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 1375 | if (in_vim9script() |
| 1376 | && (check_for_number_arg(argvars, 0) == FAIL |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1377 | || check_for_opt_dict_arg(argvars, 1) == FAIL)) |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 1378 | return; |
| 1379 | |
Bram Moolenaar | 93a1096 | 2022-06-16 11:42:09 +0100 | [diff] [blame] | 1380 | if (rettv_list_alloc(rettv) == FAIL) |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1381 | return; |
| 1382 | |
| 1383 | // default: get text properties on current line |
| 1384 | start_lnum = tv_get_number(&argvars[0]); |
| 1385 | end_lnum = start_lnum; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1386 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1387 | { |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1388 | dict_T *d; |
| 1389 | |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1390 | if (check_for_dict_arg(argvars, 1) == FAIL) |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1391 | return; |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1392 | d = argvars[1].vval.v_dict; |
| 1393 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1394 | if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL) |
| 1395 | return; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1396 | |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1397 | if (d != NULL && (di = dict_find(d, (char_u *)"end_lnum", -1)) != NULL) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1398 | { |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1399 | if (di->di_tv.v_type != VAR_NUMBER) |
| 1400 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1401 | emsg(_(e_number_required)); |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1402 | return; |
| 1403 | } |
| 1404 | end_lnum = tv_get_number(&di->di_tv); |
| 1405 | if (end_lnum < 0) |
| 1406 | // negative end_lnum is used as an offset from the last buffer |
| 1407 | // line |
| 1408 | end_lnum = buf->b_ml.ml_line_count + end_lnum + 1; |
| 1409 | else if (end_lnum > buf->b_ml.ml_line_count) |
| 1410 | end_lnum = buf->b_ml.ml_line_count; |
| 1411 | add_lnum = TRUE; |
| 1412 | } |
| 1413 | if (d != NULL && (di = dict_find(d, (char_u *)"types", -1)) != NULL) |
| 1414 | { |
| 1415 | if (di->di_tv.v_type != VAR_LIST) |
| 1416 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1417 | emsg(_(e_list_required)); |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1418 | return; |
| 1419 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1420 | |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1421 | l = di->di_tv.vval.v_list; |
| 1422 | if (l != NULL && list_len(l) > 0) |
| 1423 | { |
| 1424 | prop_types = get_prop_types_from_names(l, buf, &prop_types_len); |
| 1425 | if (prop_types == NULL) |
| 1426 | return; |
| 1427 | } |
| 1428 | } |
| 1429 | if (d != NULL && (di = dict_find(d, (char_u *)"ids", -1)) != NULL) |
| 1430 | { |
| 1431 | if (di->di_tv.v_type != VAR_LIST) |
| 1432 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1433 | emsg(_(e_list_required)); |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1434 | goto errret; |
| 1435 | } |
| 1436 | |
| 1437 | l = di->di_tv.vval.v_list; |
| 1438 | if (l != NULL && list_len(l) > 0) |
| 1439 | { |
| 1440 | prop_ids = get_prop_ids_from_list(l, &prop_ids_len); |
| 1441 | if (prop_ids == NULL) |
| 1442 | goto errret; |
| 1443 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1444 | } |
| 1445 | } |
Yegappan Lakshmanan | e021662 | 2021-11-23 11:46:32 +0000 | [diff] [blame] | 1446 | if (start_lnum < 1 || start_lnum > buf->b_ml.ml_line_count |
| 1447 | || end_lnum < 1 || end_lnum < start_lnum) |
| 1448 | emsg(_(e_invalid_range)); |
| 1449 | else |
| 1450 | for (lnum = start_lnum; lnum <= end_lnum; lnum++) |
| 1451 | get_props_in_line(buf, lnum, prop_types, prop_types_len, |
| 1452 | prop_ids, prop_ids_len, |
| 1453 | rettv->vval.v_list, add_lnum); |
| 1454 | |
| 1455 | errret: |
| 1456 | VIM_CLEAR(prop_types); |
| 1457 | VIM_CLEAR(prop_ids); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /* |
| 1461 | * prop_remove({props} [, {lnum} [, {lnum_end}]]) |
| 1462 | */ |
| 1463 | void |
| 1464 | f_prop_remove(typval_T *argvars, typval_T *rettv) |
| 1465 | { |
| 1466 | linenr_T start = 1; |
| 1467 | linenr_T end = 0; |
| 1468 | linenr_T lnum; |
Bram Moolenaar | 965c044 | 2021-05-17 00:22:06 +0200 | [diff] [blame] | 1469 | linenr_T first_changed = 0; |
| 1470 | linenr_T last_changed = 0; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1471 | dict_T *dict; |
| 1472 | buf_T *buf = curbuf; |
Bram Moolenaar | a5a40c5 | 2020-09-05 20:50:49 +0200 | [diff] [blame] | 1473 | int do_all; |
Bram Moolenaar | 3a4cd39 | 2022-07-30 22:17:18 +0100 | [diff] [blame] | 1474 | int id = -MAXCOL; |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1475 | int type_id = -1; // for a single "type" |
| 1476 | int *type_ids = NULL; // array, for a list of "types", allocated |
| 1477 | int num_type_ids = 0; // number of elements in "type_ids" |
Bram Moolenaar | a5a40c5 | 2020-09-05 20:50:49 +0200 | [diff] [blame] | 1478 | int both; |
Bram Moolenaar | 3a4cd39 | 2022-07-30 22:17:18 +0100 | [diff] [blame] | 1479 | int did_remove_text = FALSE; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1480 | |
| 1481 | rettv->vval.v_number = 0; |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1482 | |
| 1483 | if (in_vim9script() |
| 1484 | && (check_for_dict_arg(argvars, 0) == FAIL |
| 1485 | || check_for_opt_number_arg(argvars, 1) == FAIL |
| 1486 | || (argvars[1].v_type != VAR_UNKNOWN |
| 1487 | && check_for_opt_number_arg(argvars, 2) == FAIL))) |
| 1488 | return; |
| 1489 | |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1490 | if (check_for_nonnull_dict_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1491 | return; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1492 | |
| 1493 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1494 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1495 | start = tv_get_number(&argvars[1]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1496 | end = start; |
| 1497 | if (argvars[2].v_type != VAR_UNKNOWN) |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1498 | end = tv_get_number(&argvars[2]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1499 | if (start < 1 || end < 1) |
| 1500 | { |
Bram Moolenaar | 108010a | 2021-06-27 22:03:33 +0200 | [diff] [blame] | 1501 | emsg(_(e_invalid_range)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1502 | return; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | dict = argvars[0].vval.v_dict; |
Bram Moolenaar | f0884c5 | 2019-05-24 21:22:29 +0200 | [diff] [blame] | 1507 | if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL) |
| 1508 | return; |
| 1509 | if (buf->b_ml.ml_mfp == NULL) |
| 1510 | return; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1511 | |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1512 | do_all = dict_get_bool(dict, "all", FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1513 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 1514 | if (dict_has_key(dict, "id")) |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1515 | id = dict_get_number(dict, "id"); |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1516 | |
| 1517 | // if a specific type was supplied "type": check that (and ignore "types". |
| 1518 | // Otherwise check against the list of "types". |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 1519 | if (dict_has_key(dict, "type")) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1520 | { |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1521 | char_u *name = dict_get_string(dict, "type", FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1522 | proptype_T *type = lookup_prop_type(name, buf); |
| 1523 | |
| 1524 | if (type == NULL) |
| 1525 | return; |
| 1526 | type_id = type->pt_id; |
| 1527 | } |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1528 | if (dict_has_key(dict, "types")) |
| 1529 | { |
| 1530 | typval_T types; |
| 1531 | listitem_T *li = NULL; |
| 1532 | |
| 1533 | dict_get_tv(dict, "types", &types); |
| 1534 | if (types.v_type == VAR_LIST && types.vval.v_list->lv_len > 0) |
| 1535 | { |
| 1536 | type_ids = alloc( sizeof(int) * types.vval.v_list->lv_len ); |
| 1537 | |
| 1538 | FOR_ALL_LIST_ITEMS(types.vval.v_list, li) |
| 1539 | { |
| 1540 | proptype_T *prop_type; |
| 1541 | |
| 1542 | if (li->li_tv.v_type != VAR_STRING) |
| 1543 | continue; |
| 1544 | |
| 1545 | prop_type = lookup_prop_type(li->li_tv.vval.v_string, buf); |
| 1546 | |
| 1547 | if (!prop_type) |
| 1548 | goto cleanup_prop_remove; |
| 1549 | |
| 1550 | type_ids[num_type_ids++] = prop_type->pt_id; |
| 1551 | } |
| 1552 | } |
| 1553 | } |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1554 | both = dict_get_bool(dict, "both", FALSE); |
Bram Moolenaar | a5a40c5 | 2020-09-05 20:50:49 +0200 | [diff] [blame] | 1555 | |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1556 | if (id == -MAXCOL && (type_id == -1 && num_type_ids == 0)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1557 | { |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 1558 | emsg(_(e_need_at_least_one_of_id_or_type)); |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1559 | goto cleanup_prop_remove; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1560 | } |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1561 | if (both && (id == -MAXCOL || (type_id == -1 && num_type_ids == 0))) |
Bram Moolenaar | 49b79bd | 2020-03-05 21:52:55 +0100 | [diff] [blame] | 1562 | { |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1563 | emsg(_(e_need_id_and_type_or_types_with_both)); |
| 1564 | goto cleanup_prop_remove; |
| 1565 | } |
| 1566 | if (type_id != -1 && num_type_ids > 0) |
| 1567 | { |
| 1568 | emsg(_(e_cannot_specify_both_type_and_types)); |
| 1569 | goto cleanup_prop_remove; |
Bram Moolenaar | 49b79bd | 2020-03-05 21:52:55 +0100 | [diff] [blame] | 1570 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1571 | |
| 1572 | if (end == 0) |
| 1573 | end = buf->b_ml.ml_line_count; |
| 1574 | for (lnum = start; lnum <= end; ++lnum) |
| 1575 | { |
| 1576 | char_u *text; |
| 1577 | size_t len; |
| 1578 | |
| 1579 | if (lnum > buf->b_ml.ml_line_count) |
| 1580 | break; |
| 1581 | text = ml_get_buf(buf, lnum, FALSE); |
| 1582 | len = STRLEN(text) + 1; |
| 1583 | if ((size_t)buf->b_ml.ml_line_len > len) |
| 1584 | { |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 1585 | static textprop_T textprop; // static because of alignment |
| 1586 | unsigned idx; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1587 | |
| 1588 | for (idx = 0; idx < (buf->b_ml.ml_line_len - len) |
| 1589 | / sizeof(textprop_T); ++idx) |
| 1590 | { |
| 1591 | char_u *cur_prop = buf->b_ml.ml_line_ptr + len |
| 1592 | + idx * sizeof(textprop_T); |
| 1593 | size_t taillen; |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1594 | int matches_id = 0; |
| 1595 | int matches_type = 0; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1596 | |
| 1597 | mch_memmove(&textprop, cur_prop, sizeof(textprop_T)); |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1598 | |
| 1599 | matches_id = textprop.tp_id == id; |
| 1600 | if (num_type_ids > 0) |
| 1601 | { |
| 1602 | int idx2; |
| 1603 | |
| 1604 | for (idx2 = 0; !matches_type && idx2 < num_type_ids; ++idx2) |
| 1605 | matches_type = textprop.tp_type == type_ids[idx2]; |
| 1606 | } |
| 1607 | else |
| 1608 | { |
| 1609 | matches_type = textprop.tp_type == type_id; |
| 1610 | } |
| 1611 | |
| 1612 | if (both ? matches_id && matches_type |
| 1613 | : matches_id || matches_type) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1614 | { |
| 1615 | if (!(buf->b_ml.ml_flags & ML_LINE_DIRTY)) |
| 1616 | { |
| 1617 | char_u *newptr = alloc(buf->b_ml.ml_line_len); |
| 1618 | |
| 1619 | // need to allocate the line to be able to change it |
| 1620 | if (newptr == NULL) |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1621 | goto cleanup_prop_remove; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1622 | mch_memmove(newptr, buf->b_ml.ml_line_ptr, |
| 1623 | buf->b_ml.ml_line_len); |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 1624 | if (buf->b_ml.ml_flags & ML_ALLOCATED) |
| 1625 | vim_free(buf->b_ml.ml_line_ptr); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1626 | buf->b_ml.ml_line_ptr = newptr; |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 1627 | buf->b_ml.ml_flags |= ML_LINE_DIRTY; |
| 1628 | |
| 1629 | cur_prop = buf->b_ml.ml_line_ptr + len |
Bram Moolenaar | f0884c5 | 2019-05-24 21:22:29 +0200 | [diff] [blame] | 1630 | + idx * sizeof(textprop_T); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | taillen = buf->b_ml.ml_line_len - len |
| 1634 | - (idx + 1) * sizeof(textprop_T); |
| 1635 | if (taillen > 0) |
| 1636 | mch_memmove(cur_prop, cur_prop + sizeof(textprop_T), |
| 1637 | taillen); |
| 1638 | buf->b_ml.ml_line_len -= sizeof(textprop_T); |
| 1639 | --idx; |
| 1640 | |
Bram Moolenaar | 3a4cd39 | 2022-07-30 22:17:18 +0100 | [diff] [blame] | 1641 | if (textprop.tp_id < 0) |
| 1642 | { |
| 1643 | garray_T *gap = &buf->b_textprop_text; |
| 1644 | int ii = -textprop.tp_id - 1; |
| 1645 | |
| 1646 | // negative ID: property with text - free the text |
| 1647 | if (ii < gap->ga_len) |
| 1648 | { |
| 1649 | char_u **p = ((char_u **)gap->ga_data) + ii; |
| 1650 | vim_free(*p); |
| 1651 | *p = NULL; |
| 1652 | did_remove_text = TRUE; |
| 1653 | } |
| 1654 | } |
| 1655 | |
Bram Moolenaar | 965c044 | 2021-05-17 00:22:06 +0200 | [diff] [blame] | 1656 | if (first_changed == 0) |
| 1657 | first_changed = lnum; |
| 1658 | last_changed = lnum; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1659 | ++rettv->vval.v_number; |
| 1660 | if (!do_all) |
| 1661 | break; |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | } |
Bram Moolenaar | 3a4cd39 | 2022-07-30 22:17:18 +0100 | [diff] [blame] | 1666 | |
Bram Moolenaar | 965c044 | 2021-05-17 00:22:06 +0200 | [diff] [blame] | 1667 | if (first_changed > 0) |
Bram Moolenaar | fc643e6 | 2021-05-17 00:15:18 +0200 | [diff] [blame] | 1668 | { |
Bram Moolenaar | 326c5d3 | 2022-08-12 13:05:49 +0100 | [diff] [blame] | 1669 | changed_line_display_buf(buf); |
Bram Moolenaar | 965c044 | 2021-05-17 00:22:06 +0200 | [diff] [blame] | 1670 | changed_lines_buf(buf, first_changed, last_changed + 1, 0); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1671 | redraw_buf_later(buf, UPD_VALID); |
Bram Moolenaar | fc643e6 | 2021-05-17 00:15:18 +0200 | [diff] [blame] | 1672 | } |
Bram Moolenaar | 3a4cd39 | 2022-07-30 22:17:18 +0100 | [diff] [blame] | 1673 | |
| 1674 | if (did_remove_text) |
| 1675 | { |
| 1676 | garray_T *gap = &buf->b_textprop_text; |
| 1677 | |
| 1678 | // Reduce the growarray size for NULL pointers at the end. |
| 1679 | while (gap->ga_len > 0 |
| 1680 | && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL) |
| 1681 | --gap->ga_len; |
| 1682 | } |
Ben Jackson | a770422 | 2022-08-20 20:54:51 +0100 | [diff] [blame] | 1683 | |
| 1684 | cleanup_prop_remove: |
| 1685 | vim_free(type_ids); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Common for f_prop_type_add() and f_prop_type_change(). |
| 1690 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 1691 | static void |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1692 | prop_type_set(typval_T *argvars, int add) |
| 1693 | { |
| 1694 | char_u *name; |
| 1695 | buf_T *buf = NULL; |
| 1696 | dict_T *dict; |
| 1697 | dictitem_T *di; |
| 1698 | proptype_T *prop; |
| 1699 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1700 | if (in_vim9script() |
| 1701 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1702 | || check_for_dict_arg(argvars, 1) == FAIL)) |
| 1703 | return; |
| 1704 | |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1705 | name = tv_get_string(&argvars[0]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1706 | if (*name == NUL) |
| 1707 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1708 | emsg(_(e_invalid_argument)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1709 | return; |
| 1710 | } |
| 1711 | |
| 1712 | if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL) |
| 1713 | return; |
| 1714 | dict = argvars[1].vval.v_dict; |
| 1715 | |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 1716 | prop = find_prop_type(name, buf); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1717 | if (add) |
| 1718 | { |
| 1719 | hashtab_T **htp; |
| 1720 | |
| 1721 | if (prop != NULL) |
| 1722 | { |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 1723 | semsg(_(e_property_type_str_already_defined), name); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1724 | return; |
| 1725 | } |
Bram Moolenaar | 47ed553 | 2019-08-08 20:49:14 +0200 | [diff] [blame] | 1726 | prop = alloc_clear(offsetof(proptype_T, pt_name) + STRLEN(name) + 1); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1727 | if (prop == NULL) |
| 1728 | return; |
| 1729 | STRCPY(prop->pt_name, name); |
| 1730 | prop->pt_id = ++proptype_id; |
Bram Moolenaar | 0743ef9 | 2019-11-13 16:37:31 +0100 | [diff] [blame] | 1731 | prop->pt_flags = PT_FLAG_COMBINE; |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 1732 | if (buf == NULL) |
| 1733 | { |
| 1734 | htp = &global_proptypes; |
| 1735 | VIM_CLEAR(global_proparray); |
| 1736 | } |
| 1737 | else |
| 1738 | { |
| 1739 | htp = &buf->b_proptypes; |
| 1740 | VIM_CLEAR(buf->b_proparray); |
| 1741 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1742 | if (*htp == NULL) |
| 1743 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 1744 | *htp = ALLOC_ONE(hashtab_T); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1745 | if (*htp == NULL) |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 1746 | { |
| 1747 | vim_free(prop); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1748 | return; |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 1749 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1750 | hash_init(*htp); |
| 1751 | } |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 1752 | hash_add(*htp, PT2HIKEY(prop)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1753 | } |
| 1754 | else |
| 1755 | { |
| 1756 | if (prop == NULL) |
| 1757 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1758 | semsg(_(e_type_not_exist), name); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1759 | return; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | if (dict != NULL) |
| 1764 | { |
| 1765 | di = dict_find(dict, (char_u *)"highlight", -1); |
| 1766 | if (di != NULL) |
| 1767 | { |
| 1768 | char_u *highlight; |
| 1769 | int hl_id = 0; |
| 1770 | |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 1771 | highlight = dict_get_string(dict, "highlight", FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1772 | if (highlight != NULL && *highlight != NUL) |
| 1773 | hl_id = syn_name2id(highlight); |
| 1774 | if (hl_id <= 0) |
| 1775 | { |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 1776 | semsg(_(e_unknown_highlight_group_name_str), |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1777 | highlight == NULL ? (char_u *)"" : highlight); |
| 1778 | return; |
| 1779 | } |
| 1780 | prop->pt_hl_id = hl_id; |
| 1781 | } |
| 1782 | |
Bram Moolenaar | 58187f1 | 2019-05-05 16:33:47 +0200 | [diff] [blame] | 1783 | di = dict_find(dict, (char_u *)"combine", -1); |
| 1784 | if (di != NULL) |
| 1785 | { |
Bram Moolenaar | fa2e38d | 2020-09-05 21:00:00 +0200 | [diff] [blame] | 1786 | if (tv_get_bool(&di->di_tv)) |
Bram Moolenaar | 58187f1 | 2019-05-05 16:33:47 +0200 | [diff] [blame] | 1787 | prop->pt_flags |= PT_FLAG_COMBINE; |
| 1788 | else |
| 1789 | prop->pt_flags &= ~PT_FLAG_COMBINE; |
| 1790 | } |
| 1791 | |
Bram Moolenaar | f4ba8bc | 2022-08-05 17:05:04 +0100 | [diff] [blame] | 1792 | di = dict_find(dict, (char_u *)"override", -1); |
| 1793 | if (di != NULL) |
| 1794 | { |
| 1795 | if (tv_get_bool(&di->di_tv)) |
| 1796 | prop->pt_flags |= PT_FLAG_OVERRIDE; |
| 1797 | else |
| 1798 | prop->pt_flags &= ~PT_FLAG_OVERRIDE; |
| 1799 | } |
| 1800 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1801 | di = dict_find(dict, (char_u *)"priority", -1); |
| 1802 | if (di != NULL) |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1803 | prop->pt_priority = tv_get_number(&di->di_tv); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1804 | |
| 1805 | di = dict_find(dict, (char_u *)"start_incl", -1); |
| 1806 | if (di != NULL) |
| 1807 | { |
Bram Moolenaar | fa2e38d | 2020-09-05 21:00:00 +0200 | [diff] [blame] | 1808 | if (tv_get_bool(&di->di_tv)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1809 | prop->pt_flags |= PT_FLAG_INS_START_INCL; |
| 1810 | else |
| 1811 | prop->pt_flags &= ~PT_FLAG_INS_START_INCL; |
| 1812 | } |
| 1813 | |
| 1814 | di = dict_find(dict, (char_u *)"end_incl", -1); |
| 1815 | if (di != NULL) |
| 1816 | { |
Bram Moolenaar | fa2e38d | 2020-09-05 21:00:00 +0200 | [diff] [blame] | 1817 | if (tv_get_bool(&di->di_tv)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1818 | prop->pt_flags |= PT_FLAG_INS_END_INCL; |
| 1819 | else |
| 1820 | prop->pt_flags &= ~PT_FLAG_INS_END_INCL; |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | /* |
| 1826 | * prop_type_add({name}, {props}) |
| 1827 | */ |
| 1828 | void |
| 1829 | f_prop_type_add(typval_T *argvars, typval_T *rettv UNUSED) |
| 1830 | { |
| 1831 | prop_type_set(argvars, TRUE); |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * prop_type_change({name}, {props}) |
| 1836 | */ |
| 1837 | void |
| 1838 | f_prop_type_change(typval_T *argvars, typval_T *rettv UNUSED) |
| 1839 | { |
| 1840 | prop_type_set(argvars, FALSE); |
| 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * prop_type_delete({name} [, {bufnr}]) |
| 1845 | */ |
| 1846 | void |
| 1847 | f_prop_type_delete(typval_T *argvars, typval_T *rettv UNUSED) |
| 1848 | { |
| 1849 | char_u *name; |
| 1850 | buf_T *buf = NULL; |
| 1851 | hashitem_T *hi; |
| 1852 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1853 | if (in_vim9script() |
| 1854 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1855 | || check_for_opt_dict_arg(argvars, 1) == FAIL)) |
| 1856 | return; |
| 1857 | |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1858 | name = tv_get_string(&argvars[0]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1859 | if (*name == NUL) |
| 1860 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1861 | emsg(_(e_invalid_argument)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1862 | return; |
| 1863 | } |
| 1864 | |
| 1865 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1866 | { |
| 1867 | if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL) |
| 1868 | return; |
| 1869 | } |
| 1870 | |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 1871 | hi = find_prop_type_hi(name, buf); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1872 | if (hi != NULL) |
| 1873 | { |
| 1874 | hashtab_T *ht; |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 1875 | proptype_T *prop = HI2PT(hi); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1876 | |
| 1877 | if (buf == NULL) |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 1878 | { |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1879 | ht = global_proptypes; |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 1880 | VIM_CLEAR(global_proparray); |
| 1881 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1882 | else |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 1883 | { |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1884 | ht = buf->b_proptypes; |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 1885 | VIM_CLEAR(buf->b_proparray); |
| 1886 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1887 | hash_remove(ht, hi); |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 1888 | vim_free(prop); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | /* |
Martin Tournoij | e2390c7 | 2021-07-28 13:30:16 +0200 | [diff] [blame] | 1893 | * prop_type_get({name} [, {props}]) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1894 | */ |
| 1895 | void |
Bram Moolenaar | 3d8a513 | 2020-01-04 16:13:49 +0100 | [diff] [blame] | 1896 | f_prop_type_get(typval_T *argvars, typval_T *rettv) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1897 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1898 | char_u *name; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1899 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1900 | if (in_vim9script() |
| 1901 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1902 | || check_for_opt_dict_arg(argvars, 1) == FAIL)) |
| 1903 | return; |
| 1904 | |
| 1905 | name = tv_get_string(&argvars[0]); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1906 | if (*name == NUL) |
| 1907 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1908 | emsg(_(e_invalid_argument)); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1909 | return; |
| 1910 | } |
| 1911 | if (rettv_dict_alloc(rettv) == OK) |
| 1912 | { |
| 1913 | proptype_T *prop = NULL; |
| 1914 | buf_T *buf = NULL; |
| 1915 | |
| 1916 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1917 | { |
| 1918 | if (get_bufnr_from_arg(&argvars[1], &buf) == FAIL) |
| 1919 | return; |
| 1920 | } |
| 1921 | |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 1922 | prop = find_prop_type(name, buf); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1923 | if (prop != NULL) |
| 1924 | { |
| 1925 | dict_T *d = rettv->vval.v_dict; |
| 1926 | |
| 1927 | if (prop->pt_hl_id > 0) |
| 1928 | dict_add_string(d, "highlight", syn_id2name(prop->pt_hl_id)); |
| 1929 | dict_add_number(d, "priority", prop->pt_priority); |
Bram Moolenaar | 58187f1 | 2019-05-05 16:33:47 +0200 | [diff] [blame] | 1930 | dict_add_number(d, "combine", |
| 1931 | (prop->pt_flags & PT_FLAG_COMBINE) ? 1 : 0); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1932 | dict_add_number(d, "start_incl", |
| 1933 | (prop->pt_flags & PT_FLAG_INS_START_INCL) ? 1 : 0); |
| 1934 | dict_add_number(d, "end_incl", |
| 1935 | (prop->pt_flags & PT_FLAG_INS_END_INCL) ? 1 : 0); |
| 1936 | if (buf != NULL) |
| 1937 | dict_add_number(d, "bufnr", buf->b_fnum); |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | static void |
| 1943 | list_types(hashtab_T *ht, list_T *l) |
| 1944 | { |
| 1945 | long todo; |
| 1946 | hashitem_T *hi; |
| 1947 | |
| 1948 | todo = (long)ht->ht_used; |
| 1949 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 1950 | { |
| 1951 | if (!HASHITEM_EMPTY(hi)) |
| 1952 | { |
| 1953 | proptype_T *prop = HI2PT(hi); |
| 1954 | |
| 1955 | list_append_string(l, prop->pt_name, -1); |
| 1956 | --todo; |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | /* |
| 1962 | * prop_type_list([{bufnr}]) |
| 1963 | */ |
| 1964 | void |
| 1965 | f_prop_type_list(typval_T *argvars, typval_T *rettv UNUSED) |
| 1966 | { |
| 1967 | buf_T *buf = NULL; |
| 1968 | |
| 1969 | if (rettv_list_alloc(rettv) == OK) |
| 1970 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1971 | if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL) |
| 1972 | return; |
| 1973 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1974 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 1975 | { |
| 1976 | if (get_bufnr_from_arg(&argvars[0], &buf) == FAIL) |
| 1977 | return; |
| 1978 | } |
| 1979 | if (buf == NULL) |
| 1980 | { |
| 1981 | if (global_proptypes != NULL) |
| 1982 | list_types(global_proptypes, rettv->vval.v_list); |
| 1983 | } |
| 1984 | else if (buf->b_proptypes != NULL) |
| 1985 | list_types(buf->b_proptypes, rettv->vval.v_list); |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | /* |
| 1990 | * Free all property types in "ht". |
| 1991 | */ |
| 1992 | static void |
| 1993 | clear_ht_prop_types(hashtab_T *ht) |
| 1994 | { |
| 1995 | long todo; |
| 1996 | hashitem_T *hi; |
| 1997 | |
| 1998 | if (ht == NULL) |
| 1999 | return; |
| 2000 | |
| 2001 | todo = (long)ht->ht_used; |
| 2002 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 2003 | { |
| 2004 | if (!HASHITEM_EMPTY(hi)) |
| 2005 | { |
| 2006 | proptype_T *prop = HI2PT(hi); |
| 2007 | |
| 2008 | vim_free(prop); |
| 2009 | --todo; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | hash_clear(ht); |
| 2014 | vim_free(ht); |
| 2015 | } |
| 2016 | |
| 2017 | #if defined(EXITFREE) || defined(PROTO) |
| 2018 | /* |
Bram Moolenaar | fb95e21 | 2018-12-14 12:18:11 +0100 | [diff] [blame] | 2019 | * Free all global property types. |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2020 | */ |
| 2021 | void |
| 2022 | clear_global_prop_types(void) |
| 2023 | { |
| 2024 | clear_ht_prop_types(global_proptypes); |
| 2025 | global_proptypes = NULL; |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 2026 | VIM_CLEAR(global_proparray); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2027 | } |
| 2028 | #endif |
| 2029 | |
| 2030 | /* |
| 2031 | * Free all property types for "buf". |
| 2032 | */ |
| 2033 | void |
| 2034 | clear_buf_prop_types(buf_T *buf) |
| 2035 | { |
| 2036 | clear_ht_prop_types(buf->b_proptypes); |
| 2037 | buf->b_proptypes = NULL; |
Bram Moolenaar | e44336b | 2022-08-07 18:20:08 +0100 | [diff] [blame] | 2038 | VIM_CLEAR(buf->b_proparray); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2039 | } |
| 2040 | |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2041 | // Struct used to return two values from adjust_prop(). |
| 2042 | typedef struct |
| 2043 | { |
| 2044 | int dirty; // if the property was changed |
| 2045 | int can_drop; // whether after this change, the prop may be removed |
| 2046 | } adjustres_T; |
| 2047 | |
| 2048 | /* |
| 2049 | * Adjust the property for "added" bytes (can be negative) inserted at "col". |
| 2050 | * |
| 2051 | * Note that "col" is zero-based, while tp_col is one-based. |
| 2052 | * Only for the current buffer. |
| 2053 | * "flags" can have: |
| 2054 | * APC_SUBSTITUTE: Text is replaced, not inserted. |
Bram Moolenaar | d8d4cfc | 2022-08-15 15:55:10 +0100 | [diff] [blame] | 2055 | * APC_INDENT: Text is inserted before virtual text prop |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2056 | */ |
| 2057 | static adjustres_T |
| 2058 | adjust_prop( |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 2059 | textprop_T *prop, |
| 2060 | colnr_T col, |
| 2061 | int added, |
| 2062 | int flags) |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2063 | { |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 2064 | proptype_T *pt; |
| 2065 | int start_incl; |
| 2066 | int end_incl; |
| 2067 | int droppable; |
| 2068 | adjustres_T res = {TRUE, FALSE}; |
| 2069 | |
| 2070 | // prop after end of the line doesn't move |
| 2071 | if (prop->tp_col == MAXCOL) |
| 2072 | { |
| 2073 | res.dirty = FALSE; |
| 2074 | return res; |
| 2075 | } |
| 2076 | |
| 2077 | pt = text_prop_type_by_id(curbuf, prop->tp_type); |
| 2078 | start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL)) |
LemonBoy | 698cb4c | 2022-05-14 18:10:15 +0100 | [diff] [blame] | 2079 | || (flags & APC_SUBSTITUTE) |
| 2080 | || (prop->tp_flags & TP_FLAG_CONT_PREV); |
Bram Moolenaar | d8d4cfc | 2022-08-15 15:55:10 +0100 | [diff] [blame] | 2081 | if (prop->tp_id < 0 && (flags & APC_INDENT)) |
| 2082 | // when inserting indent just before a character with virtual text |
| 2083 | // shift the text property |
| 2084 | start_incl = FALSE; |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 2085 | end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL)) |
LemonBoy | 698cb4c | 2022-05-14 18:10:15 +0100 | [diff] [blame] | 2086 | || (prop->tp_flags & TP_FLAG_CONT_NEXT); |
Bram Moolenaar | b7963df | 2022-07-31 17:12:43 +0100 | [diff] [blame] | 2087 | // do not drop zero-width props if they later can increase in size |
| 2088 | droppable = !(start_incl || end_incl); |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2089 | |
| 2090 | if (added > 0) |
| 2091 | { |
| 2092 | if (col + 1 <= prop->tp_col |
| 2093 | - (start_incl || (prop->tp_len == 0 && end_incl))) |
| 2094 | // Change is entirely before the text property: Only shift |
| 2095 | prop->tp_col += added; |
| 2096 | else if (col + 1 < prop->tp_col + prop->tp_len + end_incl) |
| 2097 | // Insertion was inside text property |
| 2098 | prop->tp_len += added; |
| 2099 | } |
| 2100 | else if (prop->tp_col > col + 1) |
| 2101 | { |
| 2102 | if (prop->tp_col + added < col + 1) |
| 2103 | { |
| 2104 | prop->tp_len += (prop->tp_col - 1 - col) + added; |
| 2105 | prop->tp_col = col + 1; |
| 2106 | if (prop->tp_len <= 0) |
| 2107 | { |
| 2108 | prop->tp_len = 0; |
| 2109 | res.can_drop = droppable; |
| 2110 | } |
| 2111 | } |
| 2112 | else |
| 2113 | prop->tp_col += added; |
| 2114 | } |
Bram Moolenaar | f5240b9 | 2022-08-24 12:24:37 +0100 | [diff] [blame] | 2115 | else if (prop->tp_len > 0 && prop->tp_col + prop->tp_len > col |
| 2116 | && prop->tp_id >= 0) // don't change length for virtual text |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2117 | { |
| 2118 | int after = col - added - (prop->tp_col - 1 + prop->tp_len); |
| 2119 | |
| 2120 | prop->tp_len += after > 0 ? added + after : added; |
| 2121 | res.can_drop = prop->tp_len <= 0 && droppable; |
| 2122 | } |
| 2123 | else |
| 2124 | res.dirty = FALSE; |
| 2125 | |
| 2126 | return res; |
| 2127 | } |
| 2128 | |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 2129 | /* |
| 2130 | * Adjust the columns of text properties in line "lnum" after position "col" to |
| 2131 | * shift by "bytes_added" (can be negative). |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2132 | * Note that "col" is zero-based, while tp_col is one-based. |
| 2133 | * Only for the current buffer. |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 2134 | * "flags" can have: |
| 2135 | * APC_SAVE_FOR_UNDO: Call u_savesub() before making changes to the line. |
| 2136 | * APC_SUBSTITUTE: Text is replaced, not inserted. |
Bram Moolenaar | d8d4cfc | 2022-08-15 15:55:10 +0100 | [diff] [blame] | 2137 | * APC_INDENT: Text is inserted before virtual text prop |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 2138 | * 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] | 2139 | * Returns TRUE when props were changed. |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 2140 | */ |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 2141 | int |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 2142 | adjust_prop_columns( |
| 2143 | linenr_T lnum, |
| 2144 | colnr_T col, |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 2145 | int bytes_added, |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 2146 | int flags) |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 2147 | { |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2148 | int proplen; |
| 2149 | char_u *props; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2150 | int dirty = FALSE; |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 2151 | int ri, wi; |
| 2152 | size_t textlen; |
| 2153 | |
| 2154 | if (text_prop_frozen > 0) |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 2155 | return FALSE; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2156 | |
| 2157 | proplen = get_text_props(curbuf, lnum, &props, TRUE); |
| 2158 | if (proplen == 0) |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 2159 | return FALSE; |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 2160 | textlen = curbuf->b_ml.ml_line_len - proplen * sizeof(textprop_T); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2161 | |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 2162 | wi = 0; // write index |
| 2163 | for (ri = 0; ri < proplen; ++ri) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2164 | { |
Bram Moolenaar | 12f2003 | 2020-02-26 22:06:00 +0100 | [diff] [blame] | 2165 | textprop_T prop; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2166 | adjustres_T res; |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 2167 | |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2168 | mch_memmove(&prop, props + ri * sizeof(prop), sizeof(prop)); |
| 2169 | res = adjust_prop(&prop, col, bytes_added, flags); |
| 2170 | if (res.dirty) |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 2171 | { |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 2172 | // Save for undo if requested and not done yet. |
Bram Moolenaar | cf07011 | 2020-06-29 23:02:21 +0200 | [diff] [blame] | 2173 | if ((flags & APC_SAVE_FOR_UNDO) && !dirty |
| 2174 | && u_savesub(lnum) == FAIL) |
| 2175 | return FALSE; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2176 | dirty = TRUE; |
Bram Moolenaar | 8902b31 | 2020-09-20 21:04:35 +0200 | [diff] [blame] | 2177 | |
| 2178 | // u_savesub() may have updated curbuf->b_ml, fetch it again |
| 2179 | if (curbuf->b_ml.ml_line_lnum != lnum) |
| 2180 | proplen = get_text_props(curbuf, lnum, &props, TRUE); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2181 | } |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2182 | if (res.can_drop) |
| 2183 | continue; // Drop this text property |
Bram Moolenaar | 12f2003 | 2020-02-26 22:06:00 +0100 | [diff] [blame] | 2184 | mch_memmove(props + wi * sizeof(textprop_T), &prop, sizeof(textprop_T)); |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 2185 | ++wi; |
| 2186 | } |
| 2187 | if (dirty) |
| 2188 | { |
Bram Moolenaar | 4614f53 | 2019-01-06 12:54:55 +0100 | [diff] [blame] | 2189 | colnr_T newlen = (int)textlen + wi * (colnr_T)sizeof(textprop_T); |
| 2190 | |
| 2191 | if ((curbuf->b_ml.ml_flags & ML_LINE_DIRTY) == 0) |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 2192 | { |
| 2193 | char_u *p = vim_memsave(curbuf->b_ml.ml_line_ptr, newlen); |
| 2194 | |
| 2195 | if (curbuf->b_ml.ml_flags & ML_ALLOCATED) |
| 2196 | vim_free(curbuf->b_ml.ml_line_ptr); |
| 2197 | curbuf->b_ml.ml_line_ptr = p; |
| 2198 | } |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 2199 | curbuf->b_ml.ml_flags |= ML_LINE_DIRTY; |
Bram Moolenaar | 4614f53 | 2019-01-06 12:54:55 +0100 | [diff] [blame] | 2200 | curbuf->b_ml.ml_line_len = newlen; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2201 | } |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 2202 | return dirty; |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2205 | /* |
| 2206 | * Adjust text properties for a line that was split in two. |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 2207 | * "lnum_props" is the line that has the properties from before the split. |
| 2208 | * "lnum_top" is the top line. |
| 2209 | * "kept" is the number of bytes kept in the first line, while |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2210 | * "deleted" is the number of bytes deleted. |
| 2211 | */ |
| 2212 | void |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 2213 | adjust_props_for_split( |
| 2214 | linenr_T lnum_props, |
| 2215 | linenr_T lnum_top, |
| 2216 | int kept, |
| 2217 | int deleted) |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2218 | { |
| 2219 | char_u *props; |
| 2220 | int count; |
| 2221 | garray_T prevprop; |
| 2222 | garray_T nextprop; |
| 2223 | int i; |
| 2224 | int skipped = kept + deleted; |
| 2225 | |
| 2226 | if (!curbuf->b_has_textprop) |
| 2227 | return; |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 2228 | |
| 2229 | // Get the text properties from "lnum_props". |
| 2230 | count = get_text_props(curbuf, lnum_props, &props, FALSE); |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2231 | ga_init2(&prevprop, sizeof(textprop_T), 10); |
| 2232 | ga_init2(&nextprop, sizeof(textprop_T), 10); |
| 2233 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2234 | // Keep the relevant ones in the first line, reducing the length if needed. |
| 2235 | // Copy the ones that include the split to the second line. |
| 2236 | // Move the ones after the split to the second line. |
| 2237 | for (i = 0; i < count; ++i) |
| 2238 | { |
| 2239 | textprop_T prop; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2240 | proptype_T *pt; |
| 2241 | int start_incl, end_incl; |
| 2242 | int cont_prev, cont_next; |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2243 | |
| 2244 | // copy the prop to an aligned structure |
| 2245 | mch_memmove(&prop, props + i * sizeof(textprop_T), sizeof(textprop_T)); |
| 2246 | |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2247 | pt = text_prop_type_by_id(curbuf, prop.tp_type); |
| 2248 | start_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_START_INCL)); |
| 2249 | end_incl = (pt != NULL && (pt->pt_flags & PT_FLAG_INS_END_INCL)); |
Bram Moolenaar | 7d0f7e9 | 2022-08-06 17:10:57 +0100 | [diff] [blame] | 2250 | cont_prev = prop.tp_col != MAXCOL && prop.tp_col + !start_incl <= kept; |
| 2251 | cont_next = prop.tp_col != MAXCOL |
| 2252 | && skipped <= prop.tp_col + prop.tp_len - !end_incl; |
Bram Moolenaar | d8d4cfc | 2022-08-15 15:55:10 +0100 | [diff] [blame] | 2253 | // when a prop has text it is never copied |
| 2254 | if (prop.tp_id < 0 && cont_next) |
| 2255 | cont_prev = FALSE; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2256 | |
| 2257 | if (cont_prev && ga_grow(&prevprop, 1) == OK) |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2258 | { |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2259 | textprop_T *p = ((textprop_T *)prevprop.ga_data) + prevprop.ga_len; |
| 2260 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2261 | *p = prop; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2262 | ++prevprop.ga_len; |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2263 | if (p->tp_col + p->tp_len >= kept) |
| 2264 | p->tp_len = kept - p->tp_col; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2265 | if (cont_next) |
| 2266 | p->tp_flags |= TP_FLAG_CONT_NEXT; |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2267 | } |
| 2268 | |
Bram Moolenaar | 5c65e6a | 2019-05-17 11:08:56 +0200 | [diff] [blame] | 2269 | // Only add the property to the next line if the length is bigger than |
| 2270 | // zero. |
Bram Moolenaar | 7d0f7e9 | 2022-08-06 17:10:57 +0100 | [diff] [blame] | 2271 | if ((cont_next || prop.tp_col == MAXCOL) |
| 2272 | && ga_grow(&nextprop, 1) == OK) |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2273 | { |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2274 | textprop_T *p = ((textprop_T *)nextprop.ga_data) + nextprop.ga_len; |
Bram Moolenaar | 7d0f7e9 | 2022-08-06 17:10:57 +0100 | [diff] [blame] | 2275 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2276 | *p = prop; |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2277 | ++nextprop.ga_len; |
Bram Moolenaar | 7d0f7e9 | 2022-08-06 17:10:57 +0100 | [diff] [blame] | 2278 | if (p->tp_col != MAXCOL) |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2279 | { |
Bram Moolenaar | 7d0f7e9 | 2022-08-06 17:10:57 +0100 | [diff] [blame] | 2280 | if (p->tp_col > skipped) |
| 2281 | p->tp_col -= skipped - 1; |
| 2282 | else |
| 2283 | { |
| 2284 | p->tp_len -= skipped - p->tp_col; |
| 2285 | p->tp_col = 1; |
| 2286 | } |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2287 | } |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2288 | if (cont_prev) |
| 2289 | p->tp_flags |= TP_FLAG_CONT_PREV; |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 2293 | set_text_props(lnum_top, prevprop.ga_data, |
| 2294 | prevprop.ga_len * sizeof(textprop_T)); |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2295 | ga_clear(&prevprop); |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 2296 | set_text_props(lnum_top + 1, nextprop.ga_data, |
| 2297 | nextprop.ga_len * sizeof(textprop_T)); |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 2298 | ga_clear(&nextprop); |
| 2299 | } |
| 2300 | |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2301 | /* |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2302 | * Prepend properties of joined line "lnum" to "new_props". |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2303 | */ |
| 2304 | void |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2305 | prepend_joined_props( |
| 2306 | char_u *new_props, |
| 2307 | int propcount, |
| 2308 | int *props_remaining, |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2309 | linenr_T lnum, |
Bram Moolenaar | e175dc6 | 2022-08-01 22:18:50 +0100 | [diff] [blame] | 2310 | int last_line, |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2311 | long col, |
| 2312 | int removed) |
| 2313 | { |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2314 | char_u *props; |
| 2315 | int proplen = get_text_props(curbuf, lnum, &props, FALSE); |
| 2316 | int i; |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2317 | |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2318 | for (i = proplen; i-- > 0; ) |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2319 | { |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2320 | textprop_T prop; |
| 2321 | int end; |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2322 | |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2323 | mch_memmove(&prop, props + i * sizeof(prop), sizeof(prop)); |
Bram Moolenaar | e175dc6 | 2022-08-01 22:18:50 +0100 | [diff] [blame] | 2324 | if (prop.tp_col == MAXCOL && !last_line) |
| 2325 | continue; // drop property with text after the line |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2326 | end = !(prop.tp_flags & TP_FLAG_CONT_NEXT); |
| 2327 | |
| 2328 | adjust_prop(&prop, 0, -removed, 0); // Remove leading spaces |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 2329 | adjust_prop(&prop, -1, col, 0); // Make line start at its final column |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2330 | |
Bram Moolenaar | e175dc6 | 2022-08-01 22:18:50 +0100 | [diff] [blame] | 2331 | if (last_line || end) |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2332 | mch_memmove(new_props + --(*props_remaining) * sizeof(prop), |
| 2333 | &prop, sizeof(prop)); |
| 2334 | else |
| 2335 | { |
| 2336 | int j; |
| 2337 | int found = FALSE; |
| 2338 | |
| 2339 | // Search for continuing prop. |
| 2340 | for (j = *props_remaining; j < propcount; ++j) |
| 2341 | { |
| 2342 | textprop_T op; |
| 2343 | |
| 2344 | mch_memmove(&op, new_props + j * sizeof(op), sizeof(op)); |
| 2345 | if ((op.tp_flags & TP_FLAG_CONT_PREV) |
| 2346 | && op.tp_id == prop.tp_id && op.tp_type == prop.tp_type) |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2347 | { |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2348 | found = TRUE; |
| 2349 | op.tp_len += op.tp_col - prop.tp_col; |
| 2350 | op.tp_col = prop.tp_col; |
| 2351 | // Start/end is taken care of when deleting joined lines |
| 2352 | op.tp_flags = prop.tp_flags; |
| 2353 | mch_memmove(new_props + j * sizeof(op), &op, sizeof(op)); |
| 2354 | break; |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2355 | } |
| 2356 | } |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 2357 | if (!found) |
| 2358 | internal_error("text property above joined line not found"); |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2359 | } |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 2363 | #endif // FEAT_PROP_POPUP |