Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 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 | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 11 | * list.c: List support and container (List, Dict, Blob) functions. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 17 | |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 18 | static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob"); |
| 19 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 20 | // List heads for garbage collection. |
| 21 | static list_T *first_list = NULL; // list of all lists |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 22 | |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 23 | #define FOR_ALL_WATCHERS(l, lw) \ |
| 24 | for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next) |
| 25 | |
Bram Moolenaar | bdff012 | 2020-04-05 18:56:05 +0200 | [diff] [blame] | 26 | static void list_free_item(list_T *l, listitem_T *item); |
| 27 | |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 28 | /* |
| 29 | * Add a watcher to a list. |
| 30 | */ |
| 31 | void |
| 32 | list_add_watch(list_T *l, listwatch_T *lw) |
| 33 | { |
| 34 | lw->lw_next = l->lv_watch; |
| 35 | l->lv_watch = lw; |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Remove a watcher from a list. |
| 40 | * No warning when it isn't found... |
| 41 | */ |
| 42 | void |
| 43 | list_rem_watch(list_T *l, listwatch_T *lwrem) |
| 44 | { |
| 45 | listwatch_T *lw, **lwp; |
| 46 | |
| 47 | lwp = &l->lv_watch; |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 48 | FOR_ALL_WATCHERS(l, lw) |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 49 | { |
| 50 | if (lw == lwrem) |
| 51 | { |
| 52 | *lwp = lw->lw_next; |
| 53 | break; |
| 54 | } |
| 55 | lwp = &lw->lw_next; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Just before removing an item from a list: advance watchers to the next |
| 61 | * item. |
| 62 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 63 | static void |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 64 | list_fix_watch(list_T *l, listitem_T *item) |
| 65 | { |
| 66 | listwatch_T *lw; |
| 67 | |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 68 | FOR_ALL_WATCHERS(l, lw) |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 69 | if (lw->lw_item == item) |
| 70 | lw->lw_item = item->li_next; |
| 71 | } |
| 72 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 73 | static void |
| 74 | list_init(list_T *l) |
| 75 | { |
| 76 | // Prepend the list to the list of lists for garbage collection. |
| 77 | if (first_list != NULL) |
| 78 | first_list->lv_used_prev = l; |
| 79 | l->lv_used_prev = NULL; |
| 80 | l->lv_used_next = first_list; |
| 81 | first_list = l; |
| 82 | } |
| 83 | |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 84 | /* |
| 85 | * Allocate an empty header for a list. |
| 86 | * Caller should take care of the reference count. |
| 87 | */ |
| 88 | list_T * |
| 89 | list_alloc(void) |
| 90 | { |
| 91 | list_T *l; |
| 92 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 93 | l = ALLOC_CLEAR_ONE(list_T); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 94 | if (l != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 95 | list_init(l); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 96 | return l; |
| 97 | } |
| 98 | |
| 99 | /* |
Bram Moolenaar | f49cc60 | 2018-11-11 15:21:05 +0100 | [diff] [blame] | 100 | * list_alloc() with an ID for alloc_fail(). |
| 101 | */ |
| 102 | list_T * |
| 103 | list_alloc_id(alloc_id_T id UNUSED) |
| 104 | { |
| 105 | #ifdef FEAT_EVAL |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 106 | if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T))) |
Bram Moolenaar | f49cc60 | 2018-11-11 15:21:05 +0100 | [diff] [blame] | 107 | return NULL; |
| 108 | #endif |
| 109 | return (list_alloc()); |
| 110 | } |
| 111 | |
| 112 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 113 | * Allocate space for a list, plus "count" items. |
| 114 | * Next list_set_item() must be called for each item. |
| 115 | */ |
| 116 | list_T * |
| 117 | list_alloc_with_items(int count) |
| 118 | { |
| 119 | list_T *l; |
| 120 | |
| 121 | l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T)); |
| 122 | if (l != NULL) |
| 123 | { |
| 124 | list_init(l); |
| 125 | |
| 126 | if (count > 0) |
| 127 | { |
| 128 | listitem_T *li = (listitem_T *)(l + 1); |
| 129 | int i; |
| 130 | |
| 131 | l->lv_len = count; |
| 132 | l->lv_with_items = count; |
| 133 | l->lv_first = li; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 134 | l->lv_u.mat.lv_last = li + count - 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | for (i = 0; i < count; ++i) |
| 136 | { |
| 137 | if (i == 0) |
| 138 | li->li_prev = NULL; |
| 139 | else |
| 140 | li->li_prev = li - 1; |
| 141 | if (i == count - 1) |
| 142 | li->li_next = NULL; |
| 143 | else |
| 144 | li->li_next = li + 1; |
| 145 | ++li; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | return l; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Set item "idx" for a list previously allocated with list_alloc_with_items(). |
| 154 | * The contents of "tv" is moved into the list item. |
| 155 | * Each item must be set exactly once. |
| 156 | */ |
| 157 | void |
| 158 | list_set_item(list_T *l, int idx, typval_T *tv) |
| 159 | { |
| 160 | listitem_T *li = (listitem_T *)(l + 1) + idx; |
| 161 | |
| 162 | li->li_tv = *tv; |
| 163 | } |
| 164 | |
| 165 | /* |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 166 | * Allocate an empty list for a return value, with reference count set. |
| 167 | * Returns OK or FAIL. |
| 168 | */ |
| 169 | int |
| 170 | rettv_list_alloc(typval_T *rettv) |
| 171 | { |
| 172 | list_T *l = list_alloc(); |
| 173 | |
| 174 | if (l == NULL) |
| 175 | return FAIL; |
| 176 | |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 177 | rettv->v_lock = 0; |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 178 | rettv_list_set(rettv, l); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 179 | return OK; |
| 180 | } |
| 181 | |
| 182 | /* |
Bram Moolenaar | 162b714 | 2018-12-21 15:17:36 +0100 | [diff] [blame] | 183 | * Same as rettv_list_alloc() but uses an allocation id for testing. |
| 184 | */ |
| 185 | int |
| 186 | rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED) |
| 187 | { |
| 188 | #ifdef FEAT_EVAL |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 189 | if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T))) |
Bram Moolenaar | 162b714 | 2018-12-21 15:17:36 +0100 | [diff] [blame] | 190 | return FAIL; |
| 191 | #endif |
| 192 | return rettv_list_alloc(rettv); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /* |
Bram Moolenaar | e809a4e | 2019-07-04 17:35:05 +0200 | [diff] [blame] | 197 | * Set a list as the return value. Increments the reference count. |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 198 | */ |
| 199 | void |
| 200 | rettv_list_set(typval_T *rettv, list_T *l) |
| 201 | { |
| 202 | rettv->v_type = VAR_LIST; |
| 203 | rettv->vval.v_list = l; |
| 204 | if (l != NULL) |
| 205 | ++l->lv_refcount; |
| 206 | } |
| 207 | |
| 208 | /* |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 209 | * Unreference a list: decrement the reference count and free it when it |
| 210 | * becomes zero. |
| 211 | */ |
| 212 | void |
| 213 | list_unref(list_T *l) |
| 214 | { |
| 215 | if (l != NULL && --l->lv_refcount <= 0) |
| 216 | list_free(l); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Free a list, including all non-container items it points to. |
| 221 | * Ignores the reference count. |
| 222 | */ |
| 223 | static void |
| 224 | list_free_contents(list_T *l) |
| 225 | { |
| 226 | listitem_T *item; |
| 227 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 228 | if (l->lv_first != &range_list_item) |
| 229 | for (item = l->lv_first; item != NULL; item = l->lv_first) |
| 230 | { |
| 231 | // Remove the item before deleting it. |
| 232 | l->lv_first = item->li_next; |
| 233 | clear_tv(&item->li_tv); |
| 234 | list_free_item(l, item); |
| 235 | } |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Go through the list of lists and free items without the copyID. |
| 240 | * But don't free a list that has a watcher (used in a for loop), these |
| 241 | * are not referenced anywhere. |
| 242 | */ |
| 243 | int |
| 244 | list_free_nonref(int copyID) |
| 245 | { |
| 246 | list_T *ll; |
| 247 | int did_free = FALSE; |
| 248 | |
| 249 | for (ll = first_list; ll != NULL; ll = ll->lv_used_next) |
| 250 | if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK) |
| 251 | && ll->lv_watch == NULL) |
| 252 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 253 | // Free the List and ordinary items it contains, but don't recurse |
| 254 | // into Lists and Dictionaries, they will be in the list of dicts |
| 255 | // or list of lists. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 256 | list_free_contents(ll); |
| 257 | did_free = TRUE; |
| 258 | } |
| 259 | return did_free; |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | list_free_list(list_T *l) |
| 264 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 265 | // Remove the list from the list of lists for garbage collection. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 266 | if (l->lv_used_prev == NULL) |
| 267 | first_list = l->lv_used_next; |
| 268 | else |
| 269 | l->lv_used_prev->lv_used_next = l->lv_used_next; |
| 270 | if (l->lv_used_next != NULL) |
| 271 | l->lv_used_next->lv_used_prev = l->lv_used_prev; |
| 272 | |
| 273 | vim_free(l); |
| 274 | } |
| 275 | |
| 276 | void |
| 277 | list_free_items(int copyID) |
| 278 | { |
| 279 | list_T *ll, *ll_next; |
| 280 | |
| 281 | for (ll = first_list; ll != NULL; ll = ll_next) |
| 282 | { |
| 283 | ll_next = ll->lv_used_next; |
| 284 | if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK) |
| 285 | && ll->lv_watch == NULL) |
| 286 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 287 | // Free the List and ordinary items it contains, but don't recurse |
| 288 | // into Lists and Dictionaries, they will be in the list of dicts |
| 289 | // or list of lists. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 290 | list_free_list(ll); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void |
| 296 | list_free(list_T *l) |
| 297 | { |
| 298 | if (!in_free_unref_items) |
| 299 | { |
| 300 | list_free_contents(l); |
| 301 | list_free_list(l); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Allocate a list item. |
| 307 | * It is not initialized, don't forget to set v_lock. |
| 308 | */ |
| 309 | listitem_T * |
| 310 | listitem_alloc(void) |
| 311 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 312 | return ALLOC_ONE(listitem_T); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 316 | * Free a list item, unless it was allocated together with the list itself. |
| 317 | * Does not clear the value. Does not notify watchers. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 318 | */ |
Bram Moolenaar | bdff012 | 2020-04-05 18:56:05 +0200 | [diff] [blame] | 319 | static void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 320 | list_free_item(list_T *l, listitem_T *item) |
| 321 | { |
| 322 | if (l->lv_with_items == 0 || item < (listitem_T *)l |
| 323 | || item >= (listitem_T *)(l + 1) + l->lv_with_items) |
| 324 | vim_free(item); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Free a list item, unless it was allocated together with the list itself. |
| 329 | * Also clears the value. Does not notify watchers. |
| 330 | */ |
| 331 | void |
| 332 | listitem_free(list_T *l, listitem_T *item) |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 333 | { |
| 334 | clear_tv(&item->li_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 335 | list_free_item(l, item); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Remove a list item from a List and free it. Also clears the value. |
| 340 | */ |
| 341 | void |
| 342 | listitem_remove(list_T *l, listitem_T *item) |
| 343 | { |
| 344 | vimlist_remove(l, item, item); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 345 | listitem_free(l, item); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Get the number of items in a list. |
| 350 | */ |
| 351 | long |
| 352 | list_len(list_T *l) |
| 353 | { |
| 354 | if (l == NULL) |
| 355 | return 0L; |
| 356 | return l->lv_len; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Return TRUE when two lists have exactly the same values. |
| 361 | */ |
| 362 | int |
| 363 | list_equal( |
| 364 | list_T *l1, |
| 365 | list_T *l2, |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 366 | int ic, // ignore case for strings |
| 367 | int recursive) // TRUE when used recursively |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 368 | { |
| 369 | listitem_T *item1, *item2; |
| 370 | |
| 371 | if (l1 == NULL || l2 == NULL) |
| 372 | return FALSE; |
| 373 | if (l1 == l2) |
| 374 | return TRUE; |
| 375 | if (list_len(l1) != list_len(l2)) |
| 376 | return FALSE; |
| 377 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 378 | range_list_materialize(l1); |
| 379 | range_list_materialize(l2); |
| 380 | |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 381 | for (item1 = l1->lv_first, item2 = l2->lv_first; |
| 382 | item1 != NULL && item2 != NULL; |
| 383 | item1 = item1->li_next, item2 = item2->li_next) |
| 384 | if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive)) |
| 385 | return FALSE; |
| 386 | return item1 == NULL && item2 == NULL; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Locate item with index "n" in list "l" and return it. |
| 391 | * A negative index is counted from the end; -1 is the last item. |
| 392 | * Returns NULL when "n" is out of range. |
| 393 | */ |
| 394 | listitem_T * |
| 395 | list_find(list_T *l, long n) |
| 396 | { |
| 397 | listitem_T *item; |
| 398 | long idx; |
| 399 | |
| 400 | if (l == NULL) |
| 401 | return NULL; |
| 402 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 403 | // Negative index is relative to the end. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 404 | if (n < 0) |
| 405 | n = l->lv_len + n; |
| 406 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 407 | // Check for index out of range. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 408 | if (n < 0 || n >= l->lv_len) |
| 409 | return NULL; |
| 410 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 411 | range_list_materialize(l); |
| 412 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 413 | // When there is a cached index may start search from there. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 414 | if (l->lv_u.mat.lv_idx_item != NULL) |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 415 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 416 | if (n < l->lv_u.mat.lv_idx / 2) |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 417 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 418 | // closest to the start of the list |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 419 | item = l->lv_first; |
| 420 | idx = 0; |
| 421 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 422 | else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2) |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 423 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 424 | // closest to the end of the list |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 425 | item = l->lv_u.mat.lv_last; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 426 | idx = l->lv_len - 1; |
| 427 | } |
| 428 | else |
| 429 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 430 | // closest to the cached index |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 431 | item = l->lv_u.mat.lv_idx_item; |
| 432 | idx = l->lv_u.mat.lv_idx; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | if (n < l->lv_len / 2) |
| 438 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 439 | // closest to the start of the list |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 440 | item = l->lv_first; |
| 441 | idx = 0; |
| 442 | } |
| 443 | else |
| 444 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 445 | // closest to the end of the list |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 446 | item = l->lv_u.mat.lv_last; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 447 | idx = l->lv_len - 1; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | while (n > idx) |
| 452 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 453 | // search forward |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 454 | item = item->li_next; |
| 455 | ++idx; |
| 456 | } |
| 457 | while (n < idx) |
| 458 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 459 | // search backward |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 460 | item = item->li_prev; |
| 461 | --idx; |
| 462 | } |
| 463 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 464 | // cache the used index |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 465 | l->lv_u.mat.lv_idx = idx; |
| 466 | l->lv_u.mat.lv_idx_item = item; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 467 | |
| 468 | return item; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Get list item "l[idx]" as a number. |
| 473 | */ |
| 474 | long |
| 475 | list_find_nr( |
| 476 | list_T *l, |
| 477 | long idx, |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 478 | int *errorp) // set to TRUE when something wrong |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 479 | { |
| 480 | listitem_T *li; |
| 481 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 482 | if (l != NULL && l->lv_first == &range_list_item) |
| 483 | { |
| 484 | long n = idx; |
| 485 | |
| 486 | // not materialized range() list: compute the value. |
| 487 | // Negative index is relative to the end. |
| 488 | if (n < 0) |
| 489 | n = l->lv_len + n; |
| 490 | |
| 491 | // Check for index out of range. |
| 492 | if (n < 0 || n >= l->lv_len) |
| 493 | { |
| 494 | if (errorp != NULL) |
| 495 | *errorp = TRUE; |
| 496 | return -1L; |
| 497 | } |
| 498 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 499 | return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 502 | li = list_find(l, idx); |
| 503 | if (li == NULL) |
| 504 | { |
| 505 | if (errorp != NULL) |
| 506 | *errorp = TRUE; |
| 507 | return -1L; |
| 508 | } |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 509 | return (long)tv_get_number_chk(&li->li_tv, errorp); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Get list item "l[idx - 1]" as a string. Returns NULL for failure. |
| 514 | */ |
| 515 | char_u * |
| 516 | list_find_str(list_T *l, long idx) |
| 517 | { |
| 518 | listitem_T *li; |
| 519 | |
| 520 | li = list_find(l, idx - 1); |
| 521 | if (li == NULL) |
| 522 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 523 | semsg(_(e_listidx), idx); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 526 | return tv_get_string(&li->li_tv); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Locate "item" list "l" and return its index. |
| 531 | * Returns -1 when "item" is not in the list. |
| 532 | */ |
| 533 | long |
| 534 | list_idx_of_item(list_T *l, listitem_T *item) |
| 535 | { |
| 536 | long idx = 0; |
| 537 | listitem_T *li; |
| 538 | |
| 539 | if (l == NULL) |
| 540 | return -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 541 | range_list_materialize(l); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 542 | idx = 0; |
| 543 | for (li = l->lv_first; li != NULL && li != item; li = li->li_next) |
| 544 | ++idx; |
| 545 | if (li == NULL) |
| 546 | return -1; |
| 547 | return idx; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Append item "item" to the end of list "l". |
| 552 | */ |
| 553 | void |
| 554 | list_append(list_T *l, listitem_T *item) |
| 555 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 556 | range_list_materialize(l); |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 557 | if (l->lv_u.mat.lv_last == NULL) |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 558 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 559 | // empty list |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 560 | l->lv_first = item; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 561 | l->lv_u.mat.lv_last = item; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 562 | item->li_prev = NULL; |
| 563 | } |
| 564 | else |
| 565 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 566 | l->lv_u.mat.lv_last->li_next = item; |
| 567 | item->li_prev = l->lv_u.mat.lv_last; |
| 568 | l->lv_u.mat.lv_last = item; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 569 | } |
| 570 | ++l->lv_len; |
| 571 | item->li_next = NULL; |
| 572 | } |
| 573 | |
| 574 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 575 | * Append typval_T "tv" to the end of list "l". "tv" is copied. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 576 | * Return FAIL when out of memory. |
| 577 | */ |
| 578 | int |
| 579 | list_append_tv(list_T *l, typval_T *tv) |
| 580 | { |
| 581 | listitem_T *li = listitem_alloc(); |
| 582 | |
| 583 | if (li == NULL) |
| 584 | return FAIL; |
| 585 | copy_tv(tv, &li->li_tv); |
| 586 | list_append(l, li); |
| 587 | return OK; |
| 588 | } |
| 589 | |
| 590 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 591 | * As list_append_tv() but move the value instead of copying it. |
| 592 | * Return FAIL when out of memory. |
| 593 | */ |
| 594 | int |
| 595 | list_append_tv_move(list_T *l, typval_T *tv) |
| 596 | { |
| 597 | listitem_T *li = listitem_alloc(); |
| 598 | |
| 599 | if (li == NULL) |
| 600 | return FAIL; |
| 601 | li->li_tv = *tv; |
| 602 | list_append(l, li); |
| 603 | return OK; |
| 604 | } |
| 605 | |
| 606 | /* |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 607 | * Add a dictionary to a list. Used by getqflist(). |
| 608 | * Return FAIL when out of memory. |
| 609 | */ |
| 610 | int |
| 611 | list_append_dict(list_T *list, dict_T *dict) |
| 612 | { |
| 613 | listitem_T *li = listitem_alloc(); |
| 614 | |
| 615 | if (li == NULL) |
| 616 | return FAIL; |
| 617 | li->li_tv.v_type = VAR_DICT; |
| 618 | li->li_tv.v_lock = 0; |
| 619 | li->li_tv.vval.v_dict = dict; |
| 620 | list_append(list, li); |
| 621 | ++dict->dv_refcount; |
| 622 | return OK; |
| 623 | } |
| 624 | |
| 625 | /* |
Bram Moolenaar | 4f50588 | 2018-02-10 21:06:32 +0100 | [diff] [blame] | 626 | * Append list2 to list1. |
| 627 | * Return FAIL when out of memory. |
| 628 | */ |
| 629 | int |
Bram Moolenaar | 6f8d2ac | 2018-07-25 19:49:45 +0200 | [diff] [blame] | 630 | list_append_list(list_T *list1, list_T *list2) |
Bram Moolenaar | 4f50588 | 2018-02-10 21:06:32 +0100 | [diff] [blame] | 631 | { |
| 632 | listitem_T *li = listitem_alloc(); |
| 633 | |
| 634 | if (li == NULL) |
| 635 | return FAIL; |
| 636 | li->li_tv.v_type = VAR_LIST; |
| 637 | li->li_tv.v_lock = 0; |
| 638 | li->li_tv.vval.v_list = list2; |
| 639 | list_append(list1, li); |
| 640 | ++list2->lv_refcount; |
| 641 | return OK; |
| 642 | } |
| 643 | |
| 644 | /* |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 645 | * Make a copy of "str" and append it as an item to list "l". |
| 646 | * When "len" >= 0 use "str[len]". |
| 647 | * Returns FAIL when out of memory. |
| 648 | */ |
| 649 | int |
| 650 | list_append_string(list_T *l, char_u *str, int len) |
| 651 | { |
| 652 | listitem_T *li = listitem_alloc(); |
| 653 | |
| 654 | if (li == NULL) |
| 655 | return FAIL; |
| 656 | list_append(l, li); |
| 657 | li->li_tv.v_type = VAR_STRING; |
| 658 | li->li_tv.v_lock = 0; |
| 659 | if (str == NULL) |
| 660 | li->li_tv.vval.v_string = NULL; |
| 661 | else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len) |
| 662 | : vim_strsave(str))) == NULL) |
| 663 | return FAIL; |
| 664 | return OK; |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | * Append "n" to list "l". |
| 669 | * Returns FAIL when out of memory. |
| 670 | */ |
| 671 | int |
| 672 | list_append_number(list_T *l, varnumber_T n) |
| 673 | { |
| 674 | listitem_T *li; |
| 675 | |
| 676 | li = listitem_alloc(); |
| 677 | if (li == NULL) |
| 678 | return FAIL; |
| 679 | li->li_tv.v_type = VAR_NUMBER; |
| 680 | li->li_tv.v_lock = 0; |
| 681 | li->li_tv.vval.v_number = n; |
| 682 | list_append(l, li); |
| 683 | return OK; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Insert typval_T "tv" in list "l" before "item". |
| 688 | * If "item" is NULL append at the end. |
| 689 | * Return FAIL when out of memory. |
| 690 | */ |
| 691 | int |
| 692 | list_insert_tv(list_T *l, typval_T *tv, listitem_T *item) |
| 693 | { |
| 694 | listitem_T *ni = listitem_alloc(); |
| 695 | |
| 696 | if (ni == NULL) |
| 697 | return FAIL; |
| 698 | copy_tv(tv, &ni->li_tv); |
| 699 | list_insert(l, ni, item); |
| 700 | return OK; |
| 701 | } |
| 702 | |
| 703 | void |
| 704 | list_insert(list_T *l, listitem_T *ni, listitem_T *item) |
| 705 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 706 | range_list_materialize(l); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 707 | if (item == NULL) |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 708 | // Append new item at end of list. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 709 | list_append(l, ni); |
| 710 | else |
| 711 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 712 | // Insert new item before existing item. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 713 | ni->li_prev = item->li_prev; |
| 714 | ni->li_next = item; |
| 715 | if (item->li_prev == NULL) |
| 716 | { |
| 717 | l->lv_first = ni; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 718 | ++l->lv_u.mat.lv_idx; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 719 | } |
| 720 | else |
| 721 | { |
| 722 | item->li_prev->li_next = ni; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 723 | l->lv_u.mat.lv_idx_item = NULL; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 724 | } |
| 725 | item->li_prev = ni; |
| 726 | ++l->lv_len; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Extend "l1" with "l2". |
| 732 | * If "bef" is NULL append at the end, otherwise insert before this item. |
| 733 | * Returns FAIL when out of memory. |
| 734 | */ |
| 735 | int |
| 736 | list_extend(list_T *l1, list_T *l2, listitem_T *bef) |
| 737 | { |
| 738 | listitem_T *item; |
| 739 | int todo = l2->lv_len; |
| 740 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 741 | range_list_materialize(l1); |
| 742 | range_list_materialize(l2); |
| 743 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 744 | // We also quit the loop when we have inserted the original item count of |
| 745 | // the list, avoid a hang when we extend a list with itself. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 746 | for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next) |
| 747 | if (list_insert_tv(l1, &item->li_tv, bef) == FAIL) |
| 748 | return FAIL; |
| 749 | return OK; |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Concatenate lists "l1" and "l2" into a new list, stored in "tv". |
| 754 | * Return FAIL when out of memory. |
| 755 | */ |
| 756 | int |
| 757 | list_concat(list_T *l1, list_T *l2, typval_T *tv) |
| 758 | { |
| 759 | list_T *l; |
| 760 | |
| 761 | if (l1 == NULL || l2 == NULL) |
| 762 | return FAIL; |
| 763 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 764 | // make a copy of the first list. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 765 | l = list_copy(l1, FALSE, 0); |
| 766 | if (l == NULL) |
| 767 | return FAIL; |
| 768 | tv->v_type = VAR_LIST; |
| 769 | tv->vval.v_list = l; |
| 770 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 771 | // append all items from the second list |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 772 | return list_extend(l, l2, NULL); |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Make a copy of list "orig". Shallow if "deep" is FALSE. |
| 777 | * The refcount of the new list is set to 1. |
| 778 | * See item_copy() for "copyID". |
| 779 | * Returns NULL when out of memory. |
| 780 | */ |
| 781 | list_T * |
| 782 | list_copy(list_T *orig, int deep, int copyID) |
| 783 | { |
| 784 | list_T *copy; |
| 785 | listitem_T *item; |
| 786 | listitem_T *ni; |
| 787 | |
| 788 | if (orig == NULL) |
| 789 | return NULL; |
| 790 | |
| 791 | copy = list_alloc(); |
| 792 | if (copy != NULL) |
| 793 | { |
| 794 | if (copyID != 0) |
| 795 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 796 | // Do this before adding the items, because one of the items may |
| 797 | // refer back to this list. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 798 | orig->lv_copyID = copyID; |
| 799 | orig->lv_copylist = copy; |
| 800 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 801 | range_list_materialize(orig); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 802 | for (item = orig->lv_first; item != NULL && !got_int; |
| 803 | item = item->li_next) |
| 804 | { |
| 805 | ni = listitem_alloc(); |
| 806 | if (ni == NULL) |
| 807 | break; |
| 808 | if (deep) |
| 809 | { |
| 810 | if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL) |
| 811 | { |
| 812 | vim_free(ni); |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | else |
| 817 | copy_tv(&item->li_tv, &ni->li_tv); |
| 818 | list_append(copy, ni); |
| 819 | } |
| 820 | ++copy->lv_refcount; |
| 821 | if (item != NULL) |
| 822 | { |
| 823 | list_unref(copy); |
| 824 | copy = NULL; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | return copy; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Remove items "item" to "item2" from list "l". |
| 833 | * Does not free the listitem or the value! |
| 834 | * This used to be called list_remove, but that conflicts with a Sun header |
| 835 | * file. |
| 836 | */ |
| 837 | void |
| 838 | vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2) |
| 839 | { |
| 840 | listitem_T *ip; |
| 841 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 842 | range_list_materialize(l); |
| 843 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 844 | // notify watchers |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 845 | for (ip = item; ip != NULL; ip = ip->li_next) |
| 846 | { |
| 847 | --l->lv_len; |
| 848 | list_fix_watch(l, ip); |
| 849 | if (ip == item2) |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | if (item2->li_next == NULL) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 854 | l->lv_u.mat.lv_last = item->li_prev; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 855 | else |
| 856 | item2->li_next->li_prev = item->li_prev; |
| 857 | if (item->li_prev == NULL) |
| 858 | l->lv_first = item2->li_next; |
| 859 | else |
| 860 | item->li_prev->li_next = item2->li_next; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 861 | l->lv_u.mat.lv_idx_item = NULL; |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /* |
| 865 | * Return an allocated string with the string representation of a list. |
| 866 | * May return NULL. |
| 867 | */ |
| 868 | char_u * |
| 869 | list2string(typval_T *tv, int copyID, int restore_copyID) |
| 870 | { |
| 871 | garray_T ga; |
| 872 | |
| 873 | if (tv->vval.v_list == NULL) |
| 874 | return NULL; |
| 875 | ga_init2(&ga, (int)sizeof(char), 80); |
| 876 | ga_append(&ga, '['); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 877 | range_list_materialize(tv->vval.v_list); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 878 | if (list_join(&ga, tv->vval.v_list, (char_u *)", ", |
| 879 | FALSE, restore_copyID, copyID) == FAIL) |
| 880 | { |
| 881 | vim_free(ga.ga_data); |
| 882 | return NULL; |
| 883 | } |
| 884 | ga_append(&ga, ']'); |
| 885 | ga_append(&ga, NUL); |
| 886 | return (char_u *)ga.ga_data; |
| 887 | } |
| 888 | |
| 889 | typedef struct join_S { |
| 890 | char_u *s; |
| 891 | char_u *tofree; |
| 892 | } join_T; |
| 893 | |
| 894 | static int |
| 895 | list_join_inner( |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 896 | garray_T *gap, // to store the result in |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 897 | list_T *l, |
| 898 | char_u *sep, |
| 899 | int echo_style, |
| 900 | int restore_copyID, |
| 901 | int copyID, |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 902 | garray_T *join_gap) // to keep each list item string |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 903 | { |
| 904 | int i; |
| 905 | join_T *p; |
| 906 | int len; |
| 907 | int sumlen = 0; |
| 908 | int first = TRUE; |
| 909 | char_u *tofree; |
| 910 | char_u numbuf[NUMBUFLEN]; |
| 911 | listitem_T *item; |
| 912 | char_u *s; |
| 913 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 914 | // Stringify each item in the list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 915 | range_list_materialize(l); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 916 | for (item = l->lv_first; item != NULL && !got_int; item = item->li_next) |
| 917 | { |
| 918 | s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID, |
Bram Moolenaar | 35422f4 | 2017-08-05 16:33:56 +0200 | [diff] [blame] | 919 | echo_style, restore_copyID, !echo_style); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 920 | if (s == NULL) |
| 921 | return FAIL; |
| 922 | |
| 923 | len = (int)STRLEN(s); |
| 924 | sumlen += len; |
| 925 | |
| 926 | (void)ga_grow(join_gap, 1); |
| 927 | p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++); |
| 928 | if (tofree != NULL || s != numbuf) |
| 929 | { |
| 930 | p->s = s; |
| 931 | p->tofree = tofree; |
| 932 | } |
| 933 | else |
| 934 | { |
| 935 | p->s = vim_strnsave(s, len); |
| 936 | p->tofree = p->s; |
| 937 | } |
| 938 | |
| 939 | line_breakcheck(); |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 940 | if (did_echo_string_emsg) // recursion error, bail out |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 941 | break; |
| 942 | } |
| 943 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 944 | // Allocate result buffer with its total size, avoid re-allocation and |
| 945 | // multiple copy operations. Add 2 for a tailing ']' and NUL. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 946 | if (join_gap->ga_len >= 2) |
| 947 | sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1); |
| 948 | if (ga_grow(gap, sumlen + 2) == FAIL) |
| 949 | return FAIL; |
| 950 | |
| 951 | for (i = 0; i < join_gap->ga_len && !got_int; ++i) |
| 952 | { |
| 953 | if (first) |
| 954 | first = FALSE; |
| 955 | else |
| 956 | ga_concat(gap, sep); |
| 957 | p = ((join_T *)join_gap->ga_data) + i; |
| 958 | |
| 959 | if (p->s != NULL) |
| 960 | ga_concat(gap, p->s); |
| 961 | line_breakcheck(); |
| 962 | } |
| 963 | |
| 964 | return OK; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Join list "l" into a string in "*gap", using separator "sep". |
| 969 | * When "echo_style" is TRUE use String as echoed, otherwise as inside a List. |
| 970 | * Return FAIL or OK. |
| 971 | */ |
| 972 | int |
| 973 | list_join( |
| 974 | garray_T *gap, |
| 975 | list_T *l, |
| 976 | char_u *sep, |
| 977 | int echo_style, |
| 978 | int restore_copyID, |
| 979 | int copyID) |
| 980 | { |
| 981 | garray_T join_ga; |
| 982 | int retval; |
| 983 | join_T *p; |
| 984 | int i; |
| 985 | |
| 986 | if (l->lv_len < 1) |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 987 | return OK; // nothing to do |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 988 | ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len); |
| 989 | retval = list_join_inner(gap, l, sep, echo_style, restore_copyID, |
| 990 | copyID, &join_ga); |
| 991 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 992 | // Dispose each item in join_ga. |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 993 | if (join_ga.ga_data != NULL) |
| 994 | { |
| 995 | p = (join_T *)join_ga.ga_data; |
| 996 | for (i = 0; i < join_ga.ga_len; ++i) |
| 997 | { |
| 998 | vim_free(p->tofree); |
| 999 | ++p; |
| 1000 | } |
| 1001 | ga_clear(&join_ga); |
| 1002 | } |
| 1003 | |
| 1004 | return retval; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1008 | * "join()" function |
| 1009 | */ |
| 1010 | void |
| 1011 | f_join(typval_T *argvars, typval_T *rettv) |
| 1012 | { |
| 1013 | garray_T ga; |
| 1014 | char_u *sep; |
| 1015 | |
| 1016 | if (argvars[0].v_type != VAR_LIST) |
| 1017 | { |
| 1018 | emsg(_(e_listreq)); |
| 1019 | return; |
| 1020 | } |
| 1021 | if (argvars[0].vval.v_list == NULL) |
| 1022 | return; |
| 1023 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 1024 | sep = (char_u *)" "; |
| 1025 | else |
| 1026 | sep = tv_get_string_chk(&argvars[1]); |
| 1027 | |
| 1028 | rettv->v_type = VAR_STRING; |
| 1029 | |
| 1030 | if (sep != NULL) |
| 1031 | { |
| 1032 | ga_init2(&ga, (int)sizeof(char), 80); |
| 1033 | list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0); |
| 1034 | ga_append(&ga, NUL); |
| 1035 | rettv->vval.v_string = (char_u *)ga.ga_data; |
| 1036 | } |
| 1037 | else |
| 1038 | rettv->vval.v_string = NULL; |
| 1039 | } |
| 1040 | |
| 1041 | /* |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 1042 | * Allocate a variable for a List and fill it from "*arg". |
| 1043 | * Return OK or FAIL. |
| 1044 | */ |
| 1045 | int |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1046 | get_list_tv(char_u **arg, typval_T *rettv, int evaluate, int do_error) |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 1047 | { |
| 1048 | list_T *l = NULL; |
| 1049 | typval_T tv; |
| 1050 | listitem_T *item; |
| 1051 | |
| 1052 | if (evaluate) |
| 1053 | { |
| 1054 | l = list_alloc(); |
| 1055 | if (l == NULL) |
| 1056 | return FAIL; |
| 1057 | } |
| 1058 | |
| 1059 | *arg = skipwhite(*arg + 1); |
| 1060 | while (**arg != ']' && **arg != NUL) |
| 1061 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1062 | if (eval1(arg, &tv, evaluate) == FAIL) // recursive! |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 1063 | goto failret; |
| 1064 | if (evaluate) |
| 1065 | { |
| 1066 | item = listitem_alloc(); |
| 1067 | if (item != NULL) |
| 1068 | { |
| 1069 | item->li_tv = tv; |
| 1070 | item->li_tv.v_lock = 0; |
| 1071 | list_append(l, item); |
| 1072 | } |
| 1073 | else |
| 1074 | clear_tv(&tv); |
| 1075 | } |
| 1076 | |
| 1077 | if (**arg == ']') |
| 1078 | break; |
| 1079 | if (**arg != ',') |
| 1080 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1081 | if (do_error) |
| 1082 | semsg(_("E696: Missing comma in List: %s"), *arg); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 1083 | goto failret; |
| 1084 | } |
| 1085 | *arg = skipwhite(*arg + 1); |
| 1086 | } |
| 1087 | |
| 1088 | if (**arg != ']') |
| 1089 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1090 | if (do_error) |
Bram Moolenaar | ee619e5 | 2020-03-28 21:38:06 +0100 | [diff] [blame] | 1091 | semsg(_(e_list_end), *arg); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 1092 | failret: |
| 1093 | if (evaluate) |
| 1094 | list_free(l); |
| 1095 | return FAIL; |
| 1096 | } |
| 1097 | |
| 1098 | *arg = skipwhite(*arg + 1); |
| 1099 | if (evaluate) |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 1100 | rettv_list_set(rettv, l); |
Bram Moolenaar | da861d6 | 2016-07-17 15:46:27 +0200 | [diff] [blame] | 1101 | |
| 1102 | return OK; |
| 1103 | } |
| 1104 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1105 | /* |
Bram Moolenaar | caa55b6 | 2017-01-10 13:51:09 +0100 | [diff] [blame] | 1106 | * Write "list" of strings to file "fd". |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1107 | */ |
| 1108 | int |
| 1109 | write_list(FILE *fd, list_T *list, int binary) |
| 1110 | { |
| 1111 | listitem_T *li; |
| 1112 | int c; |
| 1113 | int ret = OK; |
| 1114 | char_u *s; |
| 1115 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1116 | range_list_materialize(list); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 1117 | FOR_ALL_LIST_ITEMS(list, li) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1118 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1119 | for (s = tv_get_string(&li->li_tv); *s != NUL; ++s) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1120 | { |
| 1121 | if (*s == '\n') |
| 1122 | c = putc(NUL, fd); |
| 1123 | else |
| 1124 | c = putc(*s, fd); |
| 1125 | if (c == EOF) |
| 1126 | { |
| 1127 | ret = FAIL; |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | if (!binary || li->li_next != NULL) |
| 1132 | if (putc('\n', fd) == EOF) |
| 1133 | { |
| 1134 | ret = FAIL; |
| 1135 | break; |
| 1136 | } |
| 1137 | if (ret == FAIL) |
| 1138 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1139 | emsg(_(e_write)); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1140 | break; |
| 1141 | } |
| 1142 | } |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
Bram Moolenaar | df48fb4 | 2016-07-22 21:50:18 +0200 | [diff] [blame] | 1146 | /* |
| 1147 | * Initialize a static list with 10 items. |
| 1148 | */ |
| 1149 | void |
| 1150 | init_static_list(staticList10_T *sl) |
| 1151 | { |
| 1152 | list_T *l = &sl->sl_list; |
| 1153 | int i; |
| 1154 | |
| 1155 | memset(sl, 0, sizeof(staticList10_T)); |
| 1156 | l->lv_first = &sl->sl_items[0]; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 1157 | l->lv_u.mat.lv_last = &sl->sl_items[9]; |
Bram Moolenaar | df48fb4 | 2016-07-22 21:50:18 +0200 | [diff] [blame] | 1158 | l->lv_refcount = DO_NOT_FREE_CNT; |
| 1159 | l->lv_lock = VAR_FIXED; |
| 1160 | sl->sl_list.lv_len = 10; |
| 1161 | |
| 1162 | for (i = 0; i < 10; ++i) |
| 1163 | { |
| 1164 | listitem_T *li = &sl->sl_items[i]; |
| 1165 | |
| 1166 | if (i == 0) |
| 1167 | li->li_prev = NULL; |
| 1168 | else |
| 1169 | li->li_prev = li - 1; |
| 1170 | if (i == 9) |
| 1171 | li->li_next = NULL; |
| 1172 | else |
| 1173 | li->li_next = li + 1; |
| 1174 | } |
| 1175 | } |
| 1176 | |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1177 | /* |
| 1178 | * "list2str()" function |
| 1179 | */ |
| 1180 | void |
| 1181 | f_list2str(typval_T *argvars, typval_T *rettv) |
| 1182 | { |
| 1183 | list_T *l; |
| 1184 | listitem_T *li; |
| 1185 | garray_T ga; |
| 1186 | int utf8 = FALSE; |
| 1187 | |
| 1188 | rettv->v_type = VAR_STRING; |
| 1189 | rettv->vval.v_string = NULL; |
| 1190 | if (argvars[0].v_type != VAR_LIST) |
| 1191 | { |
| 1192 | emsg(_(e_invarg)); |
| 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | l = argvars[0].vval.v_list; |
| 1197 | if (l == NULL) |
| 1198 | return; // empty list results in empty string |
| 1199 | |
| 1200 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1201 | utf8 = (int)tv_get_number_chk(&argvars[1], NULL); |
| 1202 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1203 | range_list_materialize(l); |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1204 | ga_init2(&ga, 1, 80); |
| 1205 | if (has_mbyte || utf8) |
| 1206 | { |
| 1207 | char_u buf[MB_MAXBYTES + 1]; |
| 1208 | int (*char2bytes)(int, char_u *); |
| 1209 | |
| 1210 | if (utf8 || enc_utf8) |
| 1211 | char2bytes = utf_char2bytes; |
| 1212 | else |
| 1213 | char2bytes = mb_char2bytes; |
| 1214 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 1215 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1216 | { |
| 1217 | buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL; |
| 1218 | ga_concat(&ga, buf); |
| 1219 | } |
| 1220 | ga_append(&ga, NUL); |
| 1221 | } |
| 1222 | else if (ga_grow(&ga, list_len(l) + 1) == OK) |
| 1223 | { |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 1224 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1225 | ga_append(&ga, tv_get_number(&li->li_tv)); |
| 1226 | ga_append(&ga, NUL); |
| 1227 | } |
| 1228 | |
| 1229 | rettv->v_type = VAR_STRING; |
| 1230 | rettv->vval.v_string = ga.ga_data; |
| 1231 | } |
| 1232 | |
Bram Moolenaar | bdff012 | 2020-04-05 18:56:05 +0200 | [diff] [blame] | 1233 | static void |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1234 | list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg) |
| 1235 | { |
| 1236 | list_T *l; |
| 1237 | listitem_T *item, *item2; |
| 1238 | listitem_T *li; |
| 1239 | int error = FALSE; |
| 1240 | int idx; |
| 1241 | |
| 1242 | if ((l = argvars[0].vval.v_list) == NULL |
| 1243 | || var_check_lock(l->lv_lock, arg_errmsg, TRUE)) |
| 1244 | return; |
| 1245 | |
| 1246 | idx = (long)tv_get_number_chk(&argvars[1], &error); |
| 1247 | if (error) |
| 1248 | ; // type error: do nothing, errmsg already given |
| 1249 | else if ((item = list_find(l, idx)) == NULL) |
| 1250 | semsg(_(e_listidx), idx); |
| 1251 | else |
| 1252 | { |
| 1253 | if (argvars[2].v_type == VAR_UNKNOWN) |
| 1254 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1255 | // Remove one item, return its value. |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1256 | vimlist_remove(l, item, item); |
| 1257 | *rettv = item->li_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1258 | list_free_item(l, item); |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1259 | } |
| 1260 | else |
| 1261 | { |
| 1262 | // Remove range of items, return list with values. |
| 1263 | int end = (long)tv_get_number_chk(&argvars[2], &error); |
| 1264 | |
| 1265 | if (error) |
| 1266 | ; // type error: do nothing |
| 1267 | else if ((item2 = list_find(l, end)) == NULL) |
| 1268 | semsg(_(e_listidx), end); |
| 1269 | else |
| 1270 | { |
| 1271 | int cnt = 0; |
| 1272 | |
| 1273 | for (li = item; li != NULL; li = li->li_next) |
| 1274 | { |
| 1275 | ++cnt; |
| 1276 | if (li == item2) |
| 1277 | break; |
| 1278 | } |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1279 | if (li == NULL) // didn't find "item2" after "item" |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1280 | emsg(_(e_invrange)); |
| 1281 | else |
| 1282 | { |
| 1283 | vimlist_remove(l, item, item2); |
| 1284 | if (rettv_list_alloc(rettv) == OK) |
| 1285 | { |
| 1286 | l = rettv->vval.v_list; |
| 1287 | l->lv_first = item; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 1288 | l->lv_u.mat.lv_last = item2; |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1289 | item->li_prev = NULL; |
| 1290 | item2->li_next = NULL; |
| 1291 | l->lv_len = cnt; |
| 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | static int item_compare(const void *s1, const void *s2); |
| 1300 | static int item_compare2(const void *s1, const void *s2); |
| 1301 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1302 | // struct used in the array that's given to qsort() |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1303 | typedef struct |
| 1304 | { |
| 1305 | listitem_T *item; |
| 1306 | int idx; |
| 1307 | } sortItem_T; |
| 1308 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1309 | // struct storing information about current sort |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1310 | typedef struct |
| 1311 | { |
| 1312 | int item_compare_ic; |
| 1313 | int item_compare_numeric; |
| 1314 | int item_compare_numbers; |
| 1315 | #ifdef FEAT_FLOAT |
| 1316 | int item_compare_float; |
| 1317 | #endif |
| 1318 | char_u *item_compare_func; |
| 1319 | partial_T *item_compare_partial; |
| 1320 | dict_T *item_compare_selfdict; |
| 1321 | int item_compare_func_err; |
| 1322 | int item_compare_keep_zero; |
| 1323 | } sortinfo_T; |
| 1324 | static sortinfo_T *sortinfo = NULL; |
| 1325 | #define ITEM_COMPARE_FAIL 999 |
| 1326 | |
| 1327 | /* |
| 1328 | * Compare functions for f_sort() and f_uniq() below. |
| 1329 | */ |
| 1330 | static int |
| 1331 | item_compare(const void *s1, const void *s2) |
| 1332 | { |
| 1333 | sortItem_T *si1, *si2; |
| 1334 | typval_T *tv1, *tv2; |
| 1335 | char_u *p1, *p2; |
| 1336 | char_u *tofree1 = NULL, *tofree2 = NULL; |
| 1337 | int res; |
| 1338 | char_u numbuf1[NUMBUFLEN]; |
| 1339 | char_u numbuf2[NUMBUFLEN]; |
| 1340 | |
| 1341 | si1 = (sortItem_T *)s1; |
| 1342 | si2 = (sortItem_T *)s2; |
| 1343 | tv1 = &si1->item->li_tv; |
| 1344 | tv2 = &si2->item->li_tv; |
| 1345 | |
| 1346 | if (sortinfo->item_compare_numbers) |
| 1347 | { |
| 1348 | varnumber_T v1 = tv_get_number(tv1); |
| 1349 | varnumber_T v2 = tv_get_number(tv2); |
| 1350 | |
| 1351 | return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; |
| 1352 | } |
| 1353 | |
| 1354 | #ifdef FEAT_FLOAT |
| 1355 | if (sortinfo->item_compare_float) |
| 1356 | { |
| 1357 | float_T v1 = tv_get_float(tv1); |
| 1358 | float_T v2 = tv_get_float(tv2); |
| 1359 | |
| 1360 | return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; |
| 1361 | } |
| 1362 | #endif |
| 1363 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1364 | // tv2string() puts quotes around a string and allocates memory. Don't do |
| 1365 | // that for string variables. Use a single quote when comparing with a |
| 1366 | // non-string to do what the docs promise. |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1367 | if (tv1->v_type == VAR_STRING) |
| 1368 | { |
| 1369 | if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric) |
| 1370 | p1 = (char_u *)"'"; |
| 1371 | else |
| 1372 | p1 = tv1->vval.v_string; |
| 1373 | } |
| 1374 | else |
| 1375 | p1 = tv2string(tv1, &tofree1, numbuf1, 0); |
| 1376 | if (tv2->v_type == VAR_STRING) |
| 1377 | { |
| 1378 | if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric) |
| 1379 | p2 = (char_u *)"'"; |
| 1380 | else |
| 1381 | p2 = tv2->vval.v_string; |
| 1382 | } |
| 1383 | else |
| 1384 | p2 = tv2string(tv2, &tofree2, numbuf2, 0); |
| 1385 | if (p1 == NULL) |
| 1386 | p1 = (char_u *)""; |
| 1387 | if (p2 == NULL) |
| 1388 | p2 = (char_u *)""; |
| 1389 | if (!sortinfo->item_compare_numeric) |
| 1390 | { |
| 1391 | if (sortinfo->item_compare_ic) |
| 1392 | res = STRICMP(p1, p2); |
| 1393 | else |
| 1394 | res = STRCMP(p1, p2); |
| 1395 | } |
| 1396 | else |
| 1397 | { |
| 1398 | double n1, n2; |
| 1399 | n1 = strtod((char *)p1, (char **)&p1); |
| 1400 | n2 = strtod((char *)p2, (char **)&p2); |
| 1401 | res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1; |
| 1402 | } |
| 1403 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1404 | // When the result would be zero, compare the item indexes. Makes the |
| 1405 | // sort stable. |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1406 | if (res == 0 && !sortinfo->item_compare_keep_zero) |
| 1407 | res = si1->idx > si2->idx ? 1 : -1; |
| 1408 | |
| 1409 | vim_free(tofree1); |
| 1410 | vim_free(tofree2); |
| 1411 | return res; |
| 1412 | } |
| 1413 | |
| 1414 | static int |
| 1415 | item_compare2(const void *s1, const void *s2) |
| 1416 | { |
| 1417 | sortItem_T *si1, *si2; |
| 1418 | int res; |
| 1419 | typval_T rettv; |
| 1420 | typval_T argv[3]; |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1421 | char_u *func_name; |
| 1422 | partial_T *partial = sortinfo->item_compare_partial; |
Bram Moolenaar | c6538bc | 2019-08-03 18:17:11 +0200 | [diff] [blame] | 1423 | funcexe_T funcexe; |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1424 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1425 | // shortcut after failure in previous call; compare all items equal |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1426 | if (sortinfo->item_compare_func_err) |
| 1427 | return 0; |
| 1428 | |
| 1429 | si1 = (sortItem_T *)s1; |
| 1430 | si2 = (sortItem_T *)s2; |
| 1431 | |
| 1432 | if (partial == NULL) |
| 1433 | func_name = sortinfo->item_compare_func; |
| 1434 | else |
| 1435 | func_name = partial_name(partial); |
| 1436 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1437 | // Copy the values. This is needed to be able to set v_lock to VAR_FIXED |
| 1438 | // in the copy without changing the original list items. |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1439 | copy_tv(&si1->item->li_tv, &argv[0]); |
| 1440 | copy_tv(&si2->item->li_tv, &argv[1]); |
| 1441 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1442 | rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this |
Bram Moolenaar | c6538bc | 2019-08-03 18:17:11 +0200 | [diff] [blame] | 1443 | vim_memset(&funcexe, 0, sizeof(funcexe)); |
| 1444 | funcexe.evaluate = TRUE; |
| 1445 | funcexe.partial = partial; |
| 1446 | funcexe.selfdict = sortinfo->item_compare_selfdict; |
| 1447 | res = call_func(func_name, -1, &rettv, 2, argv, &funcexe); |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1448 | clear_tv(&argv[0]); |
| 1449 | clear_tv(&argv[1]); |
| 1450 | |
| 1451 | if (res == FAIL) |
| 1452 | res = ITEM_COMPARE_FAIL; |
| 1453 | else |
| 1454 | res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err); |
| 1455 | if (sortinfo->item_compare_func_err) |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1456 | res = ITEM_COMPARE_FAIL; // return value has wrong type |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1457 | clear_tv(&rettv); |
| 1458 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1459 | // When the result would be zero, compare the pointers themselves. Makes |
| 1460 | // the sort stable. |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1461 | if (res == 0 && !sortinfo->item_compare_keep_zero) |
| 1462 | res = si1->idx > si2->idx ? 1 : -1; |
| 1463 | |
| 1464 | return res; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * "sort()" or "uniq()" function |
| 1469 | */ |
| 1470 | static void |
| 1471 | do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort) |
| 1472 | { |
| 1473 | list_T *l; |
| 1474 | listitem_T *li; |
| 1475 | sortItem_T *ptrs; |
| 1476 | sortinfo_T *old_sortinfo; |
| 1477 | sortinfo_T info; |
| 1478 | long len; |
| 1479 | long i; |
| 1480 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1481 | // Pointer to current info struct used in compare function. Save and |
| 1482 | // restore the current one for nested calls. |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1483 | old_sortinfo = sortinfo; |
| 1484 | sortinfo = &info; |
| 1485 | |
| 1486 | if (argvars[0].v_type != VAR_LIST) |
| 1487 | semsg(_(e_listarg), sort ? "sort()" : "uniq()"); |
| 1488 | else |
| 1489 | { |
| 1490 | l = argvars[0].vval.v_list; |
| 1491 | if (l == NULL || var_check_lock(l->lv_lock, |
| 1492 | (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")), |
| 1493 | TRUE)) |
| 1494 | goto theend; |
| 1495 | rettv_list_set(rettv, l); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1496 | range_list_materialize(l); |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1497 | |
| 1498 | len = list_len(l); |
| 1499 | if (len <= 1) |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1500 | goto theend; // short list sorts pretty quickly |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1501 | |
| 1502 | info.item_compare_ic = FALSE; |
| 1503 | info.item_compare_numeric = FALSE; |
| 1504 | info.item_compare_numbers = FALSE; |
| 1505 | #ifdef FEAT_FLOAT |
| 1506 | info.item_compare_float = FALSE; |
| 1507 | #endif |
| 1508 | info.item_compare_func = NULL; |
| 1509 | info.item_compare_partial = NULL; |
| 1510 | info.item_compare_selfdict = NULL; |
| 1511 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1512 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1513 | // optional second argument: {func} |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1514 | if (argvars[1].v_type == VAR_FUNC) |
| 1515 | info.item_compare_func = argvars[1].vval.v_string; |
| 1516 | else if (argvars[1].v_type == VAR_PARTIAL) |
| 1517 | info.item_compare_partial = argvars[1].vval.v_partial; |
| 1518 | else |
| 1519 | { |
| 1520 | int error = FALSE; |
| 1521 | |
| 1522 | i = (long)tv_get_number_chk(&argvars[1], &error); |
| 1523 | if (error) |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1524 | goto theend; // type error; errmsg already given |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1525 | if (i == 1) |
| 1526 | info.item_compare_ic = TRUE; |
| 1527 | else if (argvars[1].v_type != VAR_NUMBER) |
| 1528 | info.item_compare_func = tv_get_string(&argvars[1]); |
| 1529 | else if (i != 0) |
| 1530 | { |
| 1531 | emsg(_(e_invarg)); |
| 1532 | goto theend; |
| 1533 | } |
| 1534 | if (info.item_compare_func != NULL) |
| 1535 | { |
| 1536 | if (*info.item_compare_func == NUL) |
| 1537 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1538 | // empty string means default sort |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1539 | info.item_compare_func = NULL; |
| 1540 | } |
| 1541 | else if (STRCMP(info.item_compare_func, "n") == 0) |
| 1542 | { |
| 1543 | info.item_compare_func = NULL; |
| 1544 | info.item_compare_numeric = TRUE; |
| 1545 | } |
| 1546 | else if (STRCMP(info.item_compare_func, "N") == 0) |
| 1547 | { |
| 1548 | info.item_compare_func = NULL; |
| 1549 | info.item_compare_numbers = TRUE; |
| 1550 | } |
| 1551 | #ifdef FEAT_FLOAT |
| 1552 | else if (STRCMP(info.item_compare_func, "f") == 0) |
| 1553 | { |
| 1554 | info.item_compare_func = NULL; |
| 1555 | info.item_compare_float = TRUE; |
| 1556 | } |
| 1557 | #endif |
| 1558 | else if (STRCMP(info.item_compare_func, "i") == 0) |
| 1559 | { |
| 1560 | info.item_compare_func = NULL; |
| 1561 | info.item_compare_ic = TRUE; |
| 1562 | } |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1567 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1568 | // optional third argument: {dict} |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1569 | if (argvars[2].v_type != VAR_DICT) |
| 1570 | { |
| 1571 | emsg(_(e_dictreq)); |
| 1572 | goto theend; |
| 1573 | } |
| 1574 | info.item_compare_selfdict = argvars[2].vval.v_dict; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1578 | // Make an array with each entry pointing to an item in the List. |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1579 | ptrs = ALLOC_MULT(sortItem_T, len); |
| 1580 | if (ptrs == NULL) |
| 1581 | goto theend; |
| 1582 | |
| 1583 | i = 0; |
| 1584 | if (sort) |
| 1585 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1586 | // sort(): ptrs will be the list to sort |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 1587 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1588 | { |
| 1589 | ptrs[i].item = li; |
| 1590 | ptrs[i].idx = i; |
| 1591 | ++i; |
| 1592 | } |
| 1593 | |
| 1594 | info.item_compare_func_err = FALSE; |
| 1595 | info.item_compare_keep_zero = FALSE; |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1596 | // test the compare function |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1597 | if ((info.item_compare_func != NULL |
| 1598 | || info.item_compare_partial != NULL) |
| 1599 | && item_compare2((void *)&ptrs[0], (void *)&ptrs[1]) |
| 1600 | == ITEM_COMPARE_FAIL) |
| 1601 | emsg(_("E702: Sort compare function failed")); |
| 1602 | else |
| 1603 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1604 | // Sort the array with item pointers. |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1605 | qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T), |
| 1606 | info.item_compare_func == NULL |
| 1607 | && info.item_compare_partial == NULL |
| 1608 | ? item_compare : item_compare2); |
| 1609 | |
| 1610 | if (!info.item_compare_func_err) |
| 1611 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1612 | // Clear the List and append the items in sorted order. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 1613 | l->lv_first = l->lv_u.mat.lv_last |
| 1614 | = l->lv_u.mat.lv_idx_item = NULL; |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1615 | l->lv_len = 0; |
| 1616 | for (i = 0; i < len; ++i) |
| 1617 | list_append(l, ptrs[i].item); |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | else |
| 1622 | { |
| 1623 | int (*item_compare_func_ptr)(const void *, const void *); |
| 1624 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1625 | // f_uniq(): ptrs will be a stack of items to remove |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1626 | info.item_compare_func_err = FALSE; |
| 1627 | info.item_compare_keep_zero = TRUE; |
| 1628 | item_compare_func_ptr = info.item_compare_func != NULL |
| 1629 | || info.item_compare_partial != NULL |
| 1630 | ? item_compare2 : item_compare; |
| 1631 | |
| 1632 | for (li = l->lv_first; li != NULL && li->li_next != NULL; |
| 1633 | li = li->li_next) |
| 1634 | { |
| 1635 | if (item_compare_func_ptr((void *)&li, (void *)&li->li_next) |
| 1636 | == 0) |
| 1637 | ptrs[i++].item = li; |
| 1638 | if (info.item_compare_func_err) |
| 1639 | { |
| 1640 | emsg(_("E882: Uniq compare function failed")); |
| 1641 | break; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | if (!info.item_compare_func_err) |
| 1646 | { |
| 1647 | while (--i >= 0) |
| 1648 | { |
| 1649 | li = ptrs[i].item->li_next; |
| 1650 | ptrs[i].item->li_next = li->li_next; |
| 1651 | if (li->li_next != NULL) |
| 1652 | li->li_next->li_prev = ptrs[i].item; |
| 1653 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 1654 | l->lv_u.mat.lv_last = ptrs[i].item; |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1655 | list_fix_watch(l, li); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1656 | listitem_free(l, li); |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1657 | l->lv_len--; |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | vim_free(ptrs); |
| 1663 | } |
| 1664 | theend: |
| 1665 | sortinfo = old_sortinfo; |
| 1666 | } |
| 1667 | |
| 1668 | /* |
| 1669 | * "sort({list})" function |
| 1670 | */ |
| 1671 | void |
| 1672 | f_sort(typval_T *argvars, typval_T *rettv) |
| 1673 | { |
| 1674 | do_sort_uniq(argvars, rettv, TRUE); |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * "uniq({list})" function |
| 1679 | */ |
| 1680 | void |
| 1681 | f_uniq(typval_T *argvars, typval_T *rettv) |
| 1682 | { |
| 1683 | do_sort_uniq(argvars, rettv, FALSE); |
| 1684 | } |
| 1685 | |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1686 | /* |
| 1687 | * Handle one item for map() and filter(). |
| 1688 | */ |
| 1689 | static int |
| 1690 | filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp) |
| 1691 | { |
| 1692 | typval_T rettv; |
| 1693 | typval_T argv[3]; |
| 1694 | int retval = FAIL; |
| 1695 | |
| 1696 | copy_tv(tv, get_vim_var_tv(VV_VAL)); |
| 1697 | argv[0] = *get_vim_var_tv(VV_KEY); |
| 1698 | argv[1] = *get_vim_var_tv(VV_VAL); |
| 1699 | if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL) |
| 1700 | goto theend; |
| 1701 | if (map) |
| 1702 | { |
| 1703 | // map(): replace the list item value |
| 1704 | clear_tv(tv); |
| 1705 | rettv.v_lock = 0; |
| 1706 | *tv = rettv; |
| 1707 | } |
| 1708 | else |
| 1709 | { |
| 1710 | int error = FALSE; |
| 1711 | |
| 1712 | // filter(): when expr is zero remove the item |
| 1713 | *remp = (tv_get_number_chk(&rettv, &error) == 0); |
| 1714 | clear_tv(&rettv); |
| 1715 | // On type error, nothing has been removed; return FAIL to stop the |
| 1716 | // loop. The error message was given by tv_get_number_chk(). |
| 1717 | if (error) |
| 1718 | goto theend; |
| 1719 | } |
| 1720 | retval = OK; |
| 1721 | theend: |
| 1722 | clear_tv(get_vim_var_tv(VV_VAL)); |
| 1723 | return retval; |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | * Implementation of map() and filter(). |
| 1728 | */ |
| 1729 | static void |
| 1730 | filter_map(typval_T *argvars, typval_T *rettv, int map) |
| 1731 | { |
| 1732 | typval_T *expr; |
| 1733 | listitem_T *li, *nli; |
| 1734 | list_T *l = NULL; |
| 1735 | dictitem_T *di; |
| 1736 | hashtab_T *ht; |
| 1737 | hashitem_T *hi; |
| 1738 | dict_T *d = NULL; |
| 1739 | blob_T *b = NULL; |
| 1740 | int rem; |
| 1741 | int todo; |
| 1742 | char_u *ermsg = (char_u *)(map ? "map()" : "filter()"); |
| 1743 | char_u *arg_errmsg = (char_u *)(map ? N_("map() argument") |
| 1744 | : N_("filter() argument")); |
| 1745 | int save_did_emsg; |
| 1746 | int idx = 0; |
| 1747 | |
| 1748 | if (argvars[0].v_type == VAR_BLOB) |
| 1749 | { |
| 1750 | if ((b = argvars[0].vval.v_blob) == NULL) |
| 1751 | return; |
| 1752 | } |
| 1753 | else if (argvars[0].v_type == VAR_LIST) |
| 1754 | { |
| 1755 | if ((l = argvars[0].vval.v_list) == NULL |
| 1756 | || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE))) |
| 1757 | return; |
| 1758 | } |
| 1759 | else if (argvars[0].v_type == VAR_DICT) |
| 1760 | { |
| 1761 | if ((d = argvars[0].vval.v_dict) == NULL |
| 1762 | || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE))) |
| 1763 | return; |
| 1764 | } |
| 1765 | else |
| 1766 | { |
| 1767 | semsg(_(e_listdictarg), ermsg); |
| 1768 | return; |
| 1769 | } |
| 1770 | |
| 1771 | expr = &argvars[1]; |
| 1772 | // On type errors, the preceding call has already displayed an error |
| 1773 | // message. Avoid a misleading error message for an empty string that |
| 1774 | // was not passed as argument. |
| 1775 | if (expr->v_type != VAR_UNKNOWN) |
| 1776 | { |
| 1777 | typval_T save_val; |
| 1778 | typval_T save_key; |
| 1779 | |
| 1780 | prepare_vimvar(VV_VAL, &save_val); |
| 1781 | prepare_vimvar(VV_KEY, &save_key); |
| 1782 | |
| 1783 | // We reset "did_emsg" to be able to detect whether an error |
| 1784 | // occurred during evaluation of the expression. |
| 1785 | save_did_emsg = did_emsg; |
| 1786 | did_emsg = FALSE; |
| 1787 | |
| 1788 | if (argvars[0].v_type == VAR_DICT) |
| 1789 | { |
Bram Moolenaar | db661fb | 2020-01-29 22:17:16 +0100 | [diff] [blame] | 1790 | int prev_lock = d->dv_lock; |
| 1791 | |
| 1792 | if (map && d->dv_lock == 0) |
| 1793 | d->dv_lock = VAR_LOCKED; |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1794 | ht = &d->dv_hashtab; |
| 1795 | hash_lock(ht); |
| 1796 | todo = (int)ht->ht_used; |
| 1797 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 1798 | { |
| 1799 | if (!HASHITEM_EMPTY(hi)) |
| 1800 | { |
| 1801 | int r; |
| 1802 | |
| 1803 | --todo; |
| 1804 | di = HI2DI(hi); |
| 1805 | if (map && (var_check_lock(di->di_tv.v_lock, |
| 1806 | arg_errmsg, TRUE) |
| 1807 | || var_check_ro(di->di_flags, |
| 1808 | arg_errmsg, TRUE))) |
| 1809 | break; |
| 1810 | set_vim_var_string(VV_KEY, di->di_key, -1); |
| 1811 | r = filter_map_one(&di->di_tv, expr, map, &rem); |
| 1812 | clear_tv(get_vim_var_tv(VV_KEY)); |
| 1813 | if (r == FAIL || did_emsg) |
| 1814 | break; |
| 1815 | if (!map && rem) |
| 1816 | { |
| 1817 | if (var_check_fixed(di->di_flags, arg_errmsg, TRUE) |
| 1818 | || var_check_ro(di->di_flags, arg_errmsg, TRUE)) |
| 1819 | break; |
| 1820 | dictitem_remove(d, di); |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | hash_unlock(ht); |
Bram Moolenaar | db661fb | 2020-01-29 22:17:16 +0100 | [diff] [blame] | 1825 | d->dv_lock = prev_lock; |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1826 | } |
| 1827 | else if (argvars[0].v_type == VAR_BLOB) |
| 1828 | { |
| 1829 | int i; |
| 1830 | typval_T tv; |
Bram Moolenaar | 49c57ce | 2020-01-15 20:51:34 +0100 | [diff] [blame] | 1831 | varnumber_T val; |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1832 | |
| 1833 | // set_vim_var_nr() doesn't set the type |
| 1834 | set_vim_var_type(VV_KEY, VAR_NUMBER); |
| 1835 | |
| 1836 | for (i = 0; i < b->bv_ga.ga_len; i++) |
| 1837 | { |
| 1838 | tv.v_type = VAR_NUMBER; |
Bram Moolenaar | 49c57ce | 2020-01-15 20:51:34 +0100 | [diff] [blame] | 1839 | val = blob_get(b, i); |
| 1840 | tv.vval.v_number = val; |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1841 | set_vim_var_nr(VV_KEY, idx); |
| 1842 | if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg) |
| 1843 | break; |
| 1844 | if (tv.v_type != VAR_NUMBER) |
| 1845 | { |
| 1846 | emsg(_(e_invalblob)); |
| 1847 | break; |
| 1848 | } |
Bram Moolenaar | 49c57ce | 2020-01-15 20:51:34 +0100 | [diff] [blame] | 1849 | if (map) |
| 1850 | { |
| 1851 | if (tv.vval.v_number != val) |
| 1852 | blob_set(b, i, tv.vval.v_number); |
| 1853 | } |
| 1854 | else if (rem) |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1855 | { |
| 1856 | char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data; |
| 1857 | |
Bram Moolenaar | 49c57ce | 2020-01-15 20:51:34 +0100 | [diff] [blame] | 1858 | mch_memmove(p + i, p + i + 1, |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1859 | (size_t)b->bv_ga.ga_len - i - 1); |
| 1860 | --b->bv_ga.ga_len; |
| 1861 | --i; |
| 1862 | } |
Bram Moolenaar | 49c57ce | 2020-01-15 20:51:34 +0100 | [diff] [blame] | 1863 | ++idx; |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1864 | } |
| 1865 | } |
| 1866 | else // argvars[0].v_type == VAR_LIST |
| 1867 | { |
Bram Moolenaar | db661fb | 2020-01-29 22:17:16 +0100 | [diff] [blame] | 1868 | int prev_lock = l->lv_lock; |
| 1869 | |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1870 | // set_vim_var_nr() doesn't set the type |
| 1871 | set_vim_var_type(VV_KEY, VAR_NUMBER); |
| 1872 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1873 | range_list_materialize(l); |
Bram Moolenaar | db661fb | 2020-01-29 22:17:16 +0100 | [diff] [blame] | 1874 | if (map && l->lv_lock == 0) |
| 1875 | l->lv_lock = VAR_LOCKED; |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1876 | for (li = l->lv_first; li != NULL; li = nli) |
| 1877 | { |
| 1878 | if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE)) |
| 1879 | break; |
| 1880 | nli = li->li_next; |
| 1881 | set_vim_var_nr(VV_KEY, idx); |
| 1882 | if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL |
| 1883 | || did_emsg) |
| 1884 | break; |
| 1885 | if (!map && rem) |
| 1886 | listitem_remove(l, li); |
| 1887 | ++idx; |
| 1888 | } |
Bram Moolenaar | db661fb | 2020-01-29 22:17:16 +0100 | [diff] [blame] | 1889 | l->lv_lock = prev_lock; |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | restore_vimvar(VV_KEY, &save_key); |
| 1893 | restore_vimvar(VV_VAL, &save_val); |
| 1894 | |
| 1895 | did_emsg |= save_did_emsg; |
| 1896 | } |
| 1897 | |
| 1898 | copy_tv(&argvars[0], rettv); |
| 1899 | } |
| 1900 | |
| 1901 | /* |
| 1902 | * "filter()" function |
| 1903 | */ |
| 1904 | void |
| 1905 | f_filter(typval_T *argvars, typval_T *rettv) |
| 1906 | { |
| 1907 | filter_map(argvars, rettv, FALSE); |
| 1908 | } |
| 1909 | |
| 1910 | /* |
| 1911 | * "map()" function |
| 1912 | */ |
| 1913 | void |
| 1914 | f_map(typval_T *argvars, typval_T *rettv) |
| 1915 | { |
| 1916 | filter_map(argvars, rettv, TRUE); |
| 1917 | } |
| 1918 | |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 1919 | /* |
| 1920 | * "add(list, item)" function |
| 1921 | */ |
| 1922 | void |
| 1923 | f_add(typval_T *argvars, typval_T *rettv) |
| 1924 | { |
| 1925 | list_T *l; |
| 1926 | blob_T *b; |
| 1927 | |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 1928 | rettv->vval.v_number = 1; // Default: Failed |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 1929 | if (argvars[0].v_type == VAR_LIST) |
| 1930 | { |
| 1931 | if ((l = argvars[0].vval.v_list) != NULL |
| 1932 | && !var_check_lock(l->lv_lock, |
| 1933 | (char_u *)N_("add() argument"), TRUE) |
| 1934 | && list_append_tv(l, &argvars[1]) == OK) |
| 1935 | copy_tv(&argvars[0], rettv); |
| 1936 | } |
| 1937 | else if (argvars[0].v_type == VAR_BLOB) |
| 1938 | { |
| 1939 | if ((b = argvars[0].vval.v_blob) != NULL |
| 1940 | && !var_check_lock(b->bv_lock, |
| 1941 | (char_u *)N_("add() argument"), TRUE)) |
| 1942 | { |
| 1943 | int error = FALSE; |
| 1944 | varnumber_T n = tv_get_number_chk(&argvars[1], &error); |
| 1945 | |
| 1946 | if (!error) |
| 1947 | { |
| 1948 | ga_append(&b->bv_ga, (int)n); |
| 1949 | copy_tv(&argvars[0], rettv); |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | else |
| 1954 | emsg(_(e_listblobreq)); |
| 1955 | } |
| 1956 | |
| 1957 | /* |
| 1958 | * "count()" function |
| 1959 | */ |
| 1960 | void |
| 1961 | f_count(typval_T *argvars, typval_T *rettv) |
| 1962 | { |
| 1963 | long n = 0; |
| 1964 | int ic = FALSE; |
| 1965 | int error = FALSE; |
| 1966 | |
| 1967 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1968 | ic = (int)tv_get_number_chk(&argvars[2], &error); |
| 1969 | |
| 1970 | if (argvars[0].v_type == VAR_STRING) |
| 1971 | { |
| 1972 | char_u *expr = tv_get_string_chk(&argvars[1]); |
| 1973 | char_u *p = argvars[0].vval.v_string; |
| 1974 | char_u *next; |
| 1975 | |
| 1976 | if (!error && expr != NULL && *expr != NUL && p != NULL) |
| 1977 | { |
| 1978 | if (ic) |
| 1979 | { |
| 1980 | size_t len = STRLEN(expr); |
| 1981 | |
| 1982 | while (*p != NUL) |
| 1983 | { |
| 1984 | if (MB_STRNICMP(p, expr, len) == 0) |
| 1985 | { |
| 1986 | ++n; |
| 1987 | p += len; |
| 1988 | } |
| 1989 | else |
| 1990 | MB_PTR_ADV(p); |
| 1991 | } |
| 1992 | } |
| 1993 | else |
| 1994 | while ((next = (char_u *)strstr((char *)p, (char *)expr)) |
| 1995 | != NULL) |
| 1996 | { |
| 1997 | ++n; |
| 1998 | p = next + STRLEN(expr); |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | } |
| 2003 | else if (argvars[0].v_type == VAR_LIST) |
| 2004 | { |
| 2005 | listitem_T *li; |
| 2006 | list_T *l; |
| 2007 | long idx; |
| 2008 | |
| 2009 | if ((l = argvars[0].vval.v_list) != NULL) |
| 2010 | { |
Bram Moolenaar | 89bfc82 | 2020-01-27 22:37:23 +0100 | [diff] [blame] | 2011 | range_list_materialize(l); |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 2012 | li = l->lv_first; |
| 2013 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2014 | { |
| 2015 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 2016 | { |
| 2017 | idx = (long)tv_get_number_chk(&argvars[3], &error); |
| 2018 | if (!error) |
| 2019 | { |
| 2020 | li = list_find(l, idx); |
| 2021 | if (li == NULL) |
| 2022 | semsg(_(e_listidx), idx); |
| 2023 | } |
| 2024 | } |
| 2025 | if (error) |
| 2026 | li = NULL; |
| 2027 | } |
| 2028 | |
| 2029 | for ( ; li != NULL; li = li->li_next) |
| 2030 | if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE)) |
| 2031 | ++n; |
| 2032 | } |
| 2033 | } |
| 2034 | else if (argvars[0].v_type == VAR_DICT) |
| 2035 | { |
| 2036 | int todo; |
| 2037 | dict_T *d; |
| 2038 | hashitem_T *hi; |
| 2039 | |
| 2040 | if ((d = argvars[0].vval.v_dict) != NULL) |
| 2041 | { |
| 2042 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2043 | { |
| 2044 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 2045 | emsg(_(e_invarg)); |
| 2046 | } |
| 2047 | |
| 2048 | todo = error ? 0 : (int)d->dv_hashtab.ht_used; |
| 2049 | for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) |
| 2050 | { |
| 2051 | if (!HASHITEM_EMPTY(hi)) |
| 2052 | { |
| 2053 | --todo; |
| 2054 | if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE)) |
| 2055 | ++n; |
| 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | } |
| 2060 | else |
| 2061 | semsg(_(e_listdictarg), "count()"); |
| 2062 | rettv->vval.v_number = n; |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | * "extend(list, list [, idx])" function |
| 2067 | * "extend(dict, dict [, action])" function |
| 2068 | */ |
| 2069 | void |
| 2070 | f_extend(typval_T *argvars, typval_T *rettv) |
| 2071 | { |
| 2072 | char_u *arg_errmsg = (char_u *)N_("extend() argument"); |
| 2073 | |
| 2074 | if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST) |
| 2075 | { |
| 2076 | list_T *l1, *l2; |
| 2077 | listitem_T *item; |
| 2078 | long before; |
| 2079 | int error = FALSE; |
| 2080 | |
| 2081 | l1 = argvars[0].vval.v_list; |
| 2082 | l2 = argvars[1].vval.v_list; |
| 2083 | if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE) |
| 2084 | && l2 != NULL) |
| 2085 | { |
| 2086 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2087 | { |
| 2088 | before = (long)tv_get_number_chk(&argvars[2], &error); |
| 2089 | if (error) |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 2090 | return; // type error; errmsg already given |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 2091 | |
| 2092 | if (before == l1->lv_len) |
| 2093 | item = NULL; |
| 2094 | else |
| 2095 | { |
| 2096 | item = list_find(l1, before); |
| 2097 | if (item == NULL) |
| 2098 | { |
| 2099 | semsg(_(e_listidx), before); |
| 2100 | return; |
| 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | else |
| 2105 | item = NULL; |
| 2106 | list_extend(l1, l2, item); |
| 2107 | |
| 2108 | copy_tv(&argvars[0], rettv); |
| 2109 | } |
| 2110 | } |
| 2111 | else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT) |
| 2112 | { |
| 2113 | dict_T *d1, *d2; |
| 2114 | char_u *action; |
| 2115 | int i; |
| 2116 | |
| 2117 | d1 = argvars[0].vval.v_dict; |
| 2118 | d2 = argvars[1].vval.v_dict; |
| 2119 | if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE) |
| 2120 | && d2 != NULL) |
| 2121 | { |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 2122 | // Check the third argument. |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 2123 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2124 | { |
| 2125 | static char *(av[]) = {"keep", "force", "error"}; |
| 2126 | |
| 2127 | action = tv_get_string_chk(&argvars[2]); |
| 2128 | if (action == NULL) |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 2129 | return; // type error; errmsg already given |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 2130 | for (i = 0; i < 3; ++i) |
| 2131 | if (STRCMP(action, av[i]) == 0) |
| 2132 | break; |
| 2133 | if (i == 3) |
| 2134 | { |
| 2135 | semsg(_(e_invarg2), action); |
| 2136 | return; |
| 2137 | } |
| 2138 | } |
| 2139 | else |
| 2140 | action = (char_u *)"force"; |
| 2141 | |
| 2142 | dict_extend(d1, d2, action); |
| 2143 | |
| 2144 | copy_tv(&argvars[0], rettv); |
| 2145 | } |
| 2146 | } |
| 2147 | else |
| 2148 | semsg(_(e_listdictarg), "extend()"); |
| 2149 | } |
| 2150 | |
| 2151 | /* |
| 2152 | * "insert()" function |
| 2153 | */ |
| 2154 | void |
| 2155 | f_insert(typval_T *argvars, typval_T *rettv) |
| 2156 | { |
| 2157 | long before = 0; |
| 2158 | listitem_T *item; |
| 2159 | list_T *l; |
| 2160 | int error = FALSE; |
| 2161 | |
| 2162 | if (argvars[0].v_type == VAR_BLOB) |
| 2163 | { |
| 2164 | int val, len; |
| 2165 | char_u *p; |
| 2166 | |
| 2167 | len = blob_len(argvars[0].vval.v_blob); |
| 2168 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2169 | { |
| 2170 | before = (long)tv_get_number_chk(&argvars[2], &error); |
| 2171 | if (error) |
| 2172 | return; // type error; errmsg already given |
| 2173 | if (before < 0 || before > len) |
| 2174 | { |
| 2175 | semsg(_(e_invarg2), tv_get_string(&argvars[2])); |
| 2176 | return; |
| 2177 | } |
| 2178 | } |
| 2179 | val = tv_get_number_chk(&argvars[1], &error); |
| 2180 | if (error) |
| 2181 | return; |
| 2182 | if (val < 0 || val > 255) |
| 2183 | { |
| 2184 | semsg(_(e_invarg2), tv_get_string(&argvars[1])); |
| 2185 | return; |
| 2186 | } |
| 2187 | |
| 2188 | if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL) |
| 2189 | return; |
| 2190 | p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data; |
| 2191 | mch_memmove(p + before + 1, p + before, (size_t)len - before); |
| 2192 | *(p + before) = val; |
| 2193 | ++argvars[0].vval.v_blob->bv_ga.ga_len; |
| 2194 | |
| 2195 | copy_tv(&argvars[0], rettv); |
| 2196 | } |
| 2197 | else if (argvars[0].v_type != VAR_LIST) |
| 2198 | semsg(_(e_listblobarg), "insert()"); |
| 2199 | else if ((l = argvars[0].vval.v_list) != NULL |
| 2200 | && !var_check_lock(l->lv_lock, |
| 2201 | (char_u *)N_("insert() argument"), TRUE)) |
| 2202 | { |
| 2203 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2204 | before = (long)tv_get_number_chk(&argvars[2], &error); |
| 2205 | if (error) |
Bram Moolenaar | 4ba37b5 | 2019-12-04 21:57:43 +0100 | [diff] [blame] | 2206 | return; // type error; errmsg already given |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 2207 | |
| 2208 | if (before == l->lv_len) |
| 2209 | item = NULL; |
| 2210 | else |
| 2211 | { |
| 2212 | item = list_find(l, before); |
| 2213 | if (item == NULL) |
| 2214 | { |
| 2215 | semsg(_(e_listidx), before); |
| 2216 | l = NULL; |
| 2217 | } |
| 2218 | } |
| 2219 | if (l != NULL) |
| 2220 | { |
| 2221 | list_insert_tv(l, &argvars[1], item); |
| 2222 | copy_tv(&argvars[0], rettv); |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | /* |
| 2228 | * "remove()" function |
| 2229 | */ |
| 2230 | void |
| 2231 | f_remove(typval_T *argvars, typval_T *rettv) |
| 2232 | { |
| 2233 | char_u *arg_errmsg = (char_u *)N_("remove() argument"); |
| 2234 | |
| 2235 | if (argvars[0].v_type == VAR_DICT) |
| 2236 | dict_remove(argvars, rettv, arg_errmsg); |
| 2237 | else if (argvars[0].v_type == VAR_BLOB) |
| 2238 | blob_remove(argvars, rettv); |
| 2239 | else if (argvars[0].v_type == VAR_LIST) |
| 2240 | list_remove(argvars, rettv, arg_errmsg); |
| 2241 | else |
| 2242 | semsg(_(e_listdictblobarg), "remove()"); |
| 2243 | } |
| 2244 | |
| 2245 | /* |
| 2246 | * "reverse({list})" function |
| 2247 | */ |
| 2248 | void |
| 2249 | f_reverse(typval_T *argvars, typval_T *rettv) |
| 2250 | { |
| 2251 | list_T *l; |
| 2252 | listitem_T *li, *ni; |
| 2253 | |
| 2254 | if (argvars[0].v_type == VAR_BLOB) |
| 2255 | { |
| 2256 | blob_T *b = argvars[0].vval.v_blob; |
| 2257 | int i, len = blob_len(b); |
| 2258 | |
| 2259 | for (i = 0; i < len / 2; i++) |
| 2260 | { |
| 2261 | int tmp = blob_get(b, i); |
| 2262 | |
| 2263 | blob_set(b, i, blob_get(b, len - i - 1)); |
| 2264 | blob_set(b, len - i - 1, tmp); |
| 2265 | } |
| 2266 | rettv_blob_set(rettv, b); |
| 2267 | return; |
| 2268 | } |
| 2269 | |
| 2270 | if (argvars[0].v_type != VAR_LIST) |
| 2271 | semsg(_(e_listblobarg), "reverse()"); |
| 2272 | else if ((l = argvars[0].vval.v_list) != NULL |
| 2273 | && !var_check_lock(l->lv_lock, |
| 2274 | (char_u *)N_("reverse() argument"), TRUE)) |
| 2275 | { |
Bram Moolenaar | 89bfc82 | 2020-01-27 22:37:23 +0100 | [diff] [blame] | 2276 | if (l->lv_first == &range_list_item) |
| 2277 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 2278 | varnumber_T new_start = l->lv_u.nonmat.lv_start |
| 2279 | + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride; |
| 2280 | l->lv_u.nonmat.lv_end = new_start |
| 2281 | - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start); |
| 2282 | l->lv_u.nonmat.lv_start = new_start; |
| 2283 | l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride; |
Bram Moolenaar | 89bfc82 | 2020-01-27 22:37:23 +0100 | [diff] [blame] | 2284 | rettv_list_set(rettv, l); |
| 2285 | return; |
| 2286 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 2287 | li = l->lv_u.mat.lv_last; |
| 2288 | l->lv_first = l->lv_u.mat.lv_last = NULL; |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 2289 | l->lv_len = 0; |
| 2290 | while (li != NULL) |
| 2291 | { |
| 2292 | ni = li->li_prev; |
| 2293 | list_append(l, li); |
| 2294 | li = ni; |
| 2295 | } |
| 2296 | rettv_list_set(rettv, l); |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 2297 | l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1; |
Bram Moolenaar | 08c308a | 2019-09-04 17:48:15 +0200 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | |
Bram Moolenaar | 1e1d300 | 2019-09-04 14:41:14 +0200 | [diff] [blame] | 2301 | #endif // defined(FEAT_EVAL) |