Mercurial > vim
comparison src/eval.c @ 110:3b1d692e5a2c v7.0040
updated for version 7.0040
author | vimboss |
---|---|
date | Sun, 16 Jan 2005 22:07:38 +0000 |
parents | 2b913f7eb9d2 |
children | d4ea645c7748 |
comparison
equal
deleted
inserted
replaced
109:448c4c08f5b5 | 110:3b1d692e5a2c |
---|---|
127 int dv_refcount; /* reference count */ | 127 int dv_refcount; /* reference count */ |
128 dictitem *dv_first; /* first item, NULL if none */ | 128 dictitem *dv_first; /* first item, NULL if none */ |
129 }; | 129 }; |
130 | 130 |
131 typedef struct dictvar_S dictvar; | 131 typedef struct dictvar_S dictvar; |
132 | |
133 /* | |
134 * Structure returned by get_lval() and used by set_var_lval(). | |
135 * For a plain name: | |
136 * "name" points to the variable name. | |
137 * "exp_name" is NULL. | |
138 * "tv" is NULL | |
139 * For a magic braces name: | |
140 * "name" points to the expanded variable name. | |
141 * "exp_name" is non-NULL, to be freed later. | |
142 * "tv" is NULL | |
143 * For an index in a list: | |
144 * "name" points to the (expanded) variable name. | |
145 * "exp_name" NULL or non-NULL, to be freed later. | |
146 * "tv" points to the (first) list item value | |
147 * "li" points to the (first) list item | |
148 * "range", "n1", "n2" and "empty2" indicate what items are used. | |
149 * For an existing Dict item: | |
150 * "name" points to the (expanded) variable name. | |
151 * "exp_name" NULL or non-NULL, to be freed later. | |
152 * "tv" points to the dict item value | |
153 * "newkey" is NULL | |
154 * For a non-existing Dict item: | |
155 * "name" points to the (expanded) variable name. | |
156 * "exp_name" NULL or non-NULL, to be freed later. | |
157 * "tv" points to the Dictionary typeval | |
158 * "newkey" is the key for the new item. | |
159 */ | |
160 typedef struct lval_S | |
161 { | |
162 char_u *ll_name; /* start of variable name (can be NULL) */ | |
163 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */ | |
164 typeval *ll_tv; /* Typeval of item being used. If "newkey" | |
165 isn't NULL it's the Dict to which to add | |
166 the item. */ | |
167 listitem *ll_li; /* The list item or NULL. */ | |
168 listvar *ll_list; /* The list or NULL. */ | |
169 int ll_range; /* TRUE when a [i:j] range was used */ | |
170 long ll_n1; /* First index for list */ | |
171 long ll_n2; /* Second index for list range */ | |
172 int ll_empty2; /* Second index is empty: [i:] */ | |
173 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */ | |
174 dictitem *ll_di; /* The dictitem or NULL */ | |
175 dictitem **ll_pdi; /* field that points to found dictitem */ | |
176 } lval; | |
132 | 177 |
133 | 178 |
134 static char *e_letunexp = N_("E18: Unexpected characters in :let"); | 179 static char *e_letunexp = N_("E18: Unexpected characters in :let"); |
135 static char *e_listidx = N_("E684: list index out of range: %ld"); | 180 static char *e_listidx = N_("E684: list index out of range: %ld"); |
136 static char *e_undefvar = N_("E121: Undefined variable: %s"); | 181 static char *e_undefvar = N_("E121: Undefined variable: %s"); |
359 static listvar *list_alloc __ARGS((void)); | 404 static listvar *list_alloc __ARGS((void)); |
360 static void list_unref __ARGS((listvar *l)); | 405 static void list_unref __ARGS((listvar *l)); |
361 static void list_free __ARGS((listvar *l)); | 406 static void list_free __ARGS((listvar *l)); |
362 static listitem *listitem_alloc __ARGS((void)); | 407 static listitem *listitem_alloc __ARGS((void)); |
363 static void listitem_free __ARGS((listitem *item)); | 408 static void listitem_free __ARGS((listitem *item)); |
409 static void listitem_remove __ARGS((listvar *l, listitem *item)); | |
364 static long list_len __ARGS((listvar *l)); | 410 static long list_len __ARGS((listvar *l)); |
365 static int list_equal __ARGS((listvar *l1, listvar *l2, int ic)); | 411 static int list_equal __ARGS((listvar *l1, listvar *l2, int ic)); |
366 static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic)); | 412 static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic)); |
367 static int string_isa_number __ARGS((char_u *s)); | 413 static int string_isa_number __ARGS((char_u *s)); |
368 static listitem *list_find __ARGS((listvar *l, long n)); | 414 static listitem *list_find __ARGS((listvar *l, long n)); |
372 static int list_append_tv __ARGS((listvar *l, typeval *tv)); | 418 static int list_append_tv __ARGS((listvar *l, typeval *tv)); |
373 static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item)); | 419 static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item)); |
374 static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef)); | 420 static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef)); |
375 static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv)); | 421 static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv)); |
376 static listvar *list_copy __ARGS((listvar *orig, int deep)); | 422 static listvar *list_copy __ARGS((listvar *orig, int deep)); |
377 static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2)); | 423 static void list_remove __ARGS((listvar *l, listitem *item, listitem *item2)); |
378 static char_u *list2string __ARGS((typeval *tv)); | 424 static char_u *list2string __ARGS((typeval *tv)); |
379 static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo)); | 425 static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo)); |
380 | 426 |
381 static dictvar *dict_alloc __ARGS((void)); | 427 static dictvar *dict_alloc __ARGS((void)); |
382 static void dict_unref __ARGS((dictvar *d)); | 428 static void dict_unref __ARGS((dictvar *d)); |
383 static void dict_free __ARGS((dictvar *d)); | 429 static void dict_free __ARGS((dictvar *d)); |
384 static dictitem *dictitem_alloc __ARGS((void)); | 430 static dictitem *dictitem_alloc __ARGS((void)); |
385 static dictitem *dictitem_copy __ARGS((dictitem *org)); | 431 static dictitem *dictitem_copy __ARGS((dictitem *org)); |
386 static void dictitem_free __ARGS((dictitem *item)); | 432 static void dictitem_free __ARGS((dictitem *item)); |
387 static void dict_add __ARGS((dictvar *d, dictitem *item)); | 433 static void dict_add __ARGS((dictvar *d, dictitem *item)); |
388 static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len)); | 434 static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len, dictitem ***pdi)); |
389 static char_u *dict2string __ARGS((typeval *tv)); | 435 static char_u *dict2string __ARGS((typeval *tv)); |
390 static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate)); | 436 static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate)); |
391 | 437 |
392 static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf)); | 438 static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf)); |
393 static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf)); | 439 static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf)); |
592 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string)); | 638 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string)); |
593 static void set_var __ARGS((char_u *name, typeval *varp, int copy)); | 639 static void set_var __ARGS((char_u *name, typeval *varp, int copy)); |
594 static void copy_tv __ARGS((typeval *from, typeval *to)); | 640 static void copy_tv __ARGS((typeval *from, typeval *to)); |
595 static void item_copy __ARGS((typeval *from, typeval *to, int deep)); | 641 static void item_copy __ARGS((typeval *from, typeval *to, int deep)); |
596 static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags)); | 642 static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags)); |
597 static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal)); | 643 static char_u *trans_function_name __ARGS((char_u **pp, int skip, int exists)); |
598 static int eval_fname_script __ARGS((char_u *p)); | 644 static int eval_fname_script __ARGS((char_u *p)); |
599 static int eval_fname_sid __ARGS((char_u *p)); | 645 static int eval_fname_sid __ARGS((char_u *p)); |
600 static void list_func_head __ARGS((ufunc_T *fp, int indent)); | 646 static void list_func_head __ARGS((ufunc_T *fp, int indent)); |
601 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); | 647 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); |
602 static ufunc_T *find_func __ARGS((char_u *name)); | 648 static ufunc_T *find_func __ARGS((char_u *name)); |
614 static char_u *skip_var_one __ARGS((char_u *arg)); | 660 static char_u *skip_var_one __ARGS((char_u *arg)); |
615 static void list_all_vars __ARGS((void)); | 661 static void list_all_vars __ARGS((void)); |
616 static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg)); | 662 static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg)); |
617 static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars)); | 663 static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars)); |
618 static int check_changedtick __ARGS((char_u *arg)); | 664 static int check_changedtick __ARGS((char_u *arg)); |
619 static char_u *set_var_idx __ARGS((char_u *name, char_u *ip, typeval *rettv, int copy, char_u *endchars)); | 665 static char_u *get_lval __ARGS((char_u *name, typeval *rettv, lval *lp, int unlet, int skip, int quiet)); |
666 static void clear_lval __ARGS((lval *lp)); | |
667 static void set_var_lval __ARGS((lval *lp, char_u *endp, typeval *rettv, int copy)); | |
620 static void list_add_watch __ARGS((listvar *l, listwatch *lw)); | 668 static void list_add_watch __ARGS((listvar *l, listwatch *lw)); |
621 static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem)); | 669 static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem)); |
622 static void list_fix_watch __ARGS((listvar *l, listitem *item)); | 670 static void list_fix_watch __ARGS((listvar *l, listitem *item)); |
623 static int do_unlet_var __ARGS((char_u *name, int forceit)); | 671 static int do_unlet_var __ARGS((lval *lp, char_u *name_end, int forceit)); |
624 | 672 |
625 /* | 673 /* |
626 * Set an internal variable to a string value. Creates the variable if it does | 674 * Set an internal variable to a string value. Creates the variable if it does |
627 * not already exist. | 675 * not already exist. |
628 */ | 676 */ |
1563 } | 1611 } |
1564 } | 1612 } |
1565 | 1613 |
1566 /* | 1614 /* |
1567 * ":let var = expr": Set internal variable. | 1615 * ":let var = expr": Set internal variable. |
1616 * ":let {expr} = expr": Idem, name made with curly braces | |
1568 */ | 1617 */ |
1569 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{') | 1618 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{') |
1570 { | 1619 { |
1571 char_u *exp_name = NULL; | 1620 lval lv; |
1572 char_u *expr_start, *expr_end; | 1621 |
1573 | 1622 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE); |
1574 /* Find the end of the name. */ | 1623 if (p != NULL && lv.ll_name != NULL) |
1575 p = find_name_end(arg, &expr_start, &expr_end, FALSE); | 1624 { |
1576 if (expr_start != NULL) | 1625 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL) |
1577 { | 1626 EMSG(_(e_letunexp)); |
1578 exp_name = make_expanded_name(arg, expr_start, expr_end, p); | 1627 else |
1579 arg = exp_name; | 1628 { |
1580 } | 1629 set_var_lval(&lv, p, tv, copy); |
1581 | 1630 arg_end = p; |
1582 if (arg == NULL) | 1631 } |
1632 } | |
1633 clear_lval(&lv); | |
1634 } | |
1635 | |
1636 else | |
1637 EMSG2(_(e_invarg2), arg); | |
1638 | |
1639 return arg_end; | |
1640 } | |
1641 | |
1642 /* | |
1643 * If "arg" is equal to "b:changedtick" give an error and return TRUE. | |
1644 */ | |
1645 static int | |
1646 check_changedtick(arg) | |
1647 char_u *arg; | |
1648 { | |
1649 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13])) | |
1650 { | |
1651 EMSG2(_(e_readonlyvar), arg); | |
1652 return TRUE; | |
1653 } | |
1654 return FALSE; | |
1655 } | |
1656 | |
1657 /* | |
1658 * Get an lval: variable, Dict item or List item that can be assigned a value | |
1659 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", | |
1660 * "name.key", "name.key[expr]" etc. | |
1661 * Indexing only works if "name" is an existing List or Dictionary. | |
1662 * "name" points to the start of the name. | |
1663 * If "rettv" is not NULL it points to the value to be assigned. | |
1664 * "unlet" is TRUE for ":unlet": slightly different behavior when something is | |
1665 * wrong; must end in space or cmd separator. | |
1666 * | |
1667 * Returns a pointer to just after the name, including indexes. | |
1668 * When an evaluation error occurs "lp->name" is NULL; | |
1669 * Returns NULL for a parsing error. Still need to free items in "lp"! | |
1670 */ | |
1671 static char_u * | |
1672 get_lval(name, rettv, lp, unlet, skip, quiet) | |
1673 char_u *name; | |
1674 typeval *rettv; | |
1675 lval *lp; | |
1676 int unlet; | |
1677 int skip; | |
1678 int quiet; /* don't give error messages */ | |
1679 { | |
1680 char_u *p; | |
1681 char_u *expr_start, *expr_end; | |
1682 int cc; | |
1683 VAR v; | |
1684 typeval var1; | |
1685 typeval var2; | |
1686 int empty1 = FALSE; | |
1687 listitem *ni; | |
1688 char_u *key = NULL; | |
1689 int len; | |
1690 | |
1691 /* Clear everything in "lp". */ | |
1692 vim_memset(lp, 0, sizeof(lval)); | |
1693 | |
1694 if (skip) | |
1695 { | |
1696 /* When skipping just find the end of the name. */ | |
1697 lp->ll_name = name; | |
1698 return find_name_end(name, NULL, NULL, TRUE); | |
1699 } | |
1700 | |
1701 /* Find the end of the name. */ | |
1702 p = find_name_end(name, &expr_start, &expr_end, FALSE); | |
1703 if (expr_start != NULL) | |
1704 { | |
1705 /* Don't expand the name when we already know there is an error. */ | |
1706 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p) | |
1707 && *p != '[' && *p != '.') | |
1708 { | |
1709 EMSG(_(e_trailing)); | |
1710 return NULL; | |
1711 } | |
1712 | |
1713 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p); | |
1714 if (lp->ll_exp_name == NULL) | |
1583 { | 1715 { |
1584 /* Report an invalid expression in braces, unless the | 1716 /* Report an invalid expression in braces, unless the |
1585 * expression evaluation has been cancelled due to an | 1717 * expression evaluation has been cancelled due to an |
1586 * aborting error, an interrupt, or an exception. */ | 1718 * aborting error, an interrupt, or an exception. */ |
1587 if (!aborting()) | 1719 if (!aborting() && !quiet) |
1588 EMSG2(_(e_invarg2), arg); | 1720 { |
1589 } | 1721 if (unlet) |
1590 else if (*p == '[' || *p == '.') | 1722 emsg_severe = TRUE; |
1591 arg_end = set_var_idx(arg, p, tv, copy, endchars); | 1723 EMSG2(_(e_invarg2), name); |
1592 else if (endchars != NULL | 1724 return NULL; |
1593 && vim_strchr(endchars, *skipwhite(p)) == NULL) | 1725 } |
1594 EMSG(_(e_letunexp)); | 1726 } |
1595 else if (!check_changedtick(arg)) | 1727 lp->ll_name = lp->ll_exp_name; |
1596 { | 1728 } |
1597 c1 = *p; | |
1598 *p = NUL; | |
1599 set_var(arg, tv, copy); | |
1600 *p = c1; | |
1601 arg_end = p; | |
1602 } | |
1603 | |
1604 vim_free(exp_name); | |
1605 } | |
1606 | |
1607 else | 1729 else |
1608 EMSG2(_(e_invarg2), arg); | 1730 lp->ll_name = name; |
1609 | 1731 |
1610 return arg_end; | 1732 /* Without [idx] or .key we are done. */ |
1611 } | 1733 if ((*p != '[' && *p != '.') || lp->ll_name == NULL) |
1612 | 1734 return p; |
1613 /* | 1735 |
1614 * If "arg" is equal to "b:changedtick" give an error and return TRUE. | 1736 cc = *p; |
1615 */ | 1737 *p = NUL; |
1616 static int | 1738 v = find_var(lp->ll_name, TRUE); |
1617 check_changedtick(arg) | 1739 if (v == NULL && !quiet) |
1618 char_u *arg; | 1740 EMSG2(_(e_undefvar), lp->ll_name); |
1619 { | 1741 *p = cc; |
1620 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13])) | |
1621 { | |
1622 EMSG2(_(e_readonlyvar), arg); | |
1623 return TRUE; | |
1624 } | |
1625 return FALSE; | |
1626 } | |
1627 | |
1628 /* | |
1629 * Set a variable with an index: "name[expr]", "name[expr:expr]", | |
1630 * "name[expr][expr]", "name.key", "name.key[expr]" etc. | |
1631 * Only works if "name" is an existing List or Dictionary. | |
1632 * "ip" points to the first '['. | |
1633 * Returns a pointer to just after the last used ']'; NULL for error. | |
1634 */ | |
1635 static char_u * | |
1636 set_var_idx(name, ip, rettv, copy, endchars) | |
1637 char_u *name; | |
1638 char_u *ip; | |
1639 typeval *rettv; | |
1640 int copy; | |
1641 char_u *endchars; | |
1642 { | |
1643 VAR v; | |
1644 int c1; | |
1645 char_u *p; | |
1646 typeval var1; | |
1647 typeval var2; | |
1648 int range = FALSE; | |
1649 typeval *tv; | |
1650 long n1 = 0, n2 = 0; | |
1651 int empty1 = FALSE, empty2 = FALSE; | |
1652 listitem *li = NULL; | |
1653 listitem *ni; | |
1654 listitem *ri; | |
1655 listvar *l = NULL; | |
1656 dictitem *di; | |
1657 char_u *key = NULL; | |
1658 char_u *newkey = NULL; | |
1659 int len; | |
1660 | |
1661 c1 = *ip; | |
1662 *ip = NUL; | |
1663 v = find_var(name, TRUE); | |
1664 if (v == NULL) | |
1665 EMSG2(_(e_undefvar), name); | |
1666 *ip = c1; | |
1667 if (v == NULL) | 1742 if (v == NULL) |
1668 return NULL; | 1743 return NULL; |
1669 | 1744 |
1670 tv = &v->tv; | 1745 /* |
1671 for (p = ip; *p == '[' || (*p == '.' && tv->v_type == VAR_DICT); ) | 1746 * Loop until no more [idx] or .key is following. |
1672 { | 1747 */ |
1673 if (!(tv->v_type == VAR_LIST && tv->vval.v_list != NULL) | 1748 lp->ll_tv = &v->tv; |
1674 && !(tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)) | 1749 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT)) |
1675 { | 1750 { |
1676 EMSG(_("E689: Can only index a List or Dictionary")); | 1751 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL) |
1677 p = NULL; | 1752 && !(lp->ll_tv->v_type == VAR_DICT |
1678 break; | 1753 && lp->ll_tv->vval.v_dict != NULL)) |
1679 } | 1754 { |
1680 if (range) | 1755 if (!quiet) |
1681 { | 1756 EMSG(_("E689: Can only index a List or Dictionary")); |
1682 EMSG(_("E708: [:] must come last")); | 1757 return NULL; |
1683 p = NULL; | 1758 } |
1684 break; | 1759 if (lp->ll_range) |
1760 { | |
1761 if (!quiet) | |
1762 EMSG(_("E708: [:] must come last")); | |
1763 return NULL; | |
1685 } | 1764 } |
1686 | 1765 |
1687 len = -1; | 1766 len = -1; |
1688 if (*p == '.') | 1767 if (*p == '.') |
1689 { | 1768 { |
1690 key = p + 1; | 1769 key = p + 1; |
1691 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) | 1770 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) |
1692 ; | 1771 ; |
1693 if (len == 0) | 1772 if (len == 0) |
1694 { | 1773 { |
1695 EMSG(_(e_emptykey)); | 1774 if (!quiet) |
1696 p = NULL; | 1775 EMSG(_(e_emptykey)); |
1697 break; | 1776 return NULL; |
1698 } | 1777 } |
1699 p = key + len; | 1778 p = key + len; |
1700 } | 1779 } |
1701 else | 1780 else |
1702 { | 1781 { |
1706 empty1 = TRUE; | 1785 empty1 = TRUE; |
1707 else | 1786 else |
1708 { | 1787 { |
1709 empty1 = FALSE; | 1788 empty1 = FALSE; |
1710 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */ | 1789 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */ |
1711 { | 1790 return NULL; |
1712 p = NULL; | |
1713 break; | |
1714 } | |
1715 } | 1791 } |
1716 | 1792 |
1717 /* Optionally get the second index [ :expr]. */ | 1793 /* Optionally get the second index [ :expr]. */ |
1718 if (*p == ':') | 1794 if (*p == ':') |
1719 { | 1795 { |
1720 if (tv->v_type == VAR_DICT) | 1796 if (lp->ll_tv->v_type == VAR_DICT) |
1721 { | 1797 { |
1722 EMSG(_("E999: Cannot use [:] with a Dictionary")); | 1798 if (!quiet) |
1723 p = NULL; | 1799 EMSG(_("E999: Cannot use [:] with a Dictionary")); |
1724 if (!empty1) | 1800 if (!empty1) |
1725 clear_tv(&var1); | 1801 clear_tv(&var1); |
1726 break; | 1802 return NULL; |
1727 } | 1803 } |
1728 if (rettv->v_type != VAR_LIST || rettv->vval.v_list == NULL) | 1804 if (rettv != NULL && (rettv->v_type != VAR_LIST |
1805 || rettv->vval.v_list == NULL)) | |
1729 { | 1806 { |
1730 EMSG(_("E709: [:] requires a List value")); | 1807 if (!quiet) |
1731 p = NULL; | 1808 EMSG(_("E709: [:] requires a List value")); |
1732 if (!empty1) | 1809 if (!empty1) |
1733 clear_tv(&var1); | 1810 clear_tv(&var1); |
1734 break; | 1811 return NULL; |
1735 } | 1812 } |
1736 p = skipwhite(p + 1); | 1813 p = skipwhite(p + 1); |
1737 if (*p == ']') | 1814 if (*p == ']') |
1738 empty2 = TRUE; | 1815 lp->ll_empty2 = TRUE; |
1739 else | 1816 else |
1740 { | 1817 { |
1741 empty2 = FALSE; | 1818 lp->ll_empty2 = FALSE; |
1742 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */ | 1819 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */ |
1743 { | 1820 { |
1744 p = NULL; | |
1745 if (!empty1) | 1821 if (!empty1) |
1746 clear_tv(&var1); | 1822 clear_tv(&var1); |
1747 break; | 1823 return NULL; |
1748 } | 1824 } |
1749 } | 1825 } |
1750 range = TRUE; | 1826 lp->ll_range = TRUE; |
1751 } | 1827 } |
1752 else | 1828 else |
1753 range = FALSE; | 1829 lp->ll_range = FALSE; |
1754 | 1830 |
1755 if (*p != ']') | 1831 if (*p != ']') |
1756 { | 1832 { |
1757 EMSG(_(e_missbrac)); | 1833 if (!quiet) |
1834 EMSG(_(e_missbrac)); | |
1758 if (!empty1) | 1835 if (!empty1) |
1759 clear_tv(&var1); | 1836 clear_tv(&var1); |
1760 if (range && !empty2) | 1837 if (lp->ll_range && !lp->ll_empty2) |
1761 clear_tv(&var2); | 1838 clear_tv(&var2); |
1762 p = NULL; | 1839 return NULL; |
1763 break; | |
1764 } | 1840 } |
1765 | 1841 |
1766 /* Skip to past ']'. */ | 1842 /* Skip to past ']'. */ |
1767 ++p; | 1843 ++p; |
1768 } | 1844 } |
1769 | 1845 |
1770 if (tv->v_type == VAR_DICT) | 1846 if (lp->ll_tv->v_type == VAR_DICT) |
1771 { | 1847 { |
1772 if (len == -1) | 1848 if (len == -1) |
1773 { | 1849 { |
1850 /* "[key]": get key from "var1" */ | |
1774 key = get_tv_string(&var1); | 1851 key = get_tv_string(&var1); |
1775 if (*key == NUL) | 1852 if (*key == NUL) |
1776 { | 1853 { |
1777 EMSG(_(e_emptykey)); | 1854 if (!quiet) |
1855 EMSG(_(e_emptykey)); | |
1778 clear_tv(&var1); | 1856 clear_tv(&var1); |
1779 p = NULL; | 1857 return NULL; |
1780 break; | |
1781 } | 1858 } |
1782 } | 1859 } |
1783 di = dict_find(tv->vval.v_dict, key, len); | 1860 lp->ll_di = dict_find(lp->ll_tv->vval.v_dict, key, len, |
1784 if (di == NULL) | 1861 &lp->ll_pdi); |
1862 if (lp->ll_di == NULL) | |
1785 { | 1863 { |
1786 /* Key does not exist in dict: may need toadd it. */ | 1864 /* Key does not exist in dict: may need toadd it. */ |
1787 if (*p == '[' || *p == '.') | 1865 if (*p == '[' || *p == '.' || unlet) |
1788 { | 1866 { |
1789 EMSG2(_("E999: Key does not exist in Dictionary: %s"), key); | 1867 if (!quiet) |
1790 p = NULL; | 1868 EMSG2(_("E999: Key does not exist in Dictionary: %s"), |
1869 key); | |
1791 if (len == -1) | 1870 if (len == -1) |
1792 clear_tv(&var1); | 1871 clear_tv(&var1); |
1793 break; | 1872 return NULL; |
1794 } | 1873 } |
1795 if (len == -1) | 1874 if (len == -1) |
1796 newkey = vim_strsave(key); | 1875 lp->ll_newkey = vim_strsave(key); |
1797 else | 1876 else |
1798 newkey = vim_strnsave(key, len); | 1877 lp->ll_newkey = vim_strnsave(key, len); |
1799 if (len == -1) | 1878 if (len == -1) |
1800 clear_tv(&var1); | 1879 clear_tv(&var1); |
1801 if (newkey == NULL) | 1880 if (lp->ll_newkey == NULL) |
1802 p = NULL; | 1881 p = NULL; |
1803 break; | 1882 break; |
1804 } | 1883 } |
1805 if (len == -1) | 1884 if (len == -1) |
1806 clear_tv(&var1); | 1885 clear_tv(&var1); |
1807 tv = &di->di_tv; | 1886 lp->ll_tv = &lp->ll_di->di_tv; |
1808 } | 1887 } |
1809 else | 1888 else |
1810 { | 1889 { |
1811 /* | 1890 /* |
1812 * Get the number and item for the only or first index of the List. | 1891 * Get the number and item for the only or first index of the List. |
1813 */ | 1892 */ |
1814 if (empty1) | 1893 if (empty1) |
1815 n1 = 0; | 1894 lp->ll_n1 = 0; |
1816 else | 1895 else |
1817 { | 1896 { |
1818 n1 = get_tv_number(&var1); | 1897 lp->ll_n1 = get_tv_number(&var1); |
1819 clear_tv(&var1); | 1898 clear_tv(&var1); |
1820 } | 1899 } |
1821 l = tv->vval.v_list; | 1900 lp->ll_list = lp->ll_tv->vval.v_list; |
1822 li = list_find(l, n1); | 1901 lp->ll_li = list_find(lp->ll_list, lp->ll_n1); |
1823 if (li == NULL) | 1902 if (lp->ll_li == NULL) |
1824 { | 1903 { |
1825 EMSGN(_(e_listidx), n1); | 1904 if (!quiet) |
1826 p = NULL; | 1905 EMSGN(_(e_listidx), lp->ll_n1); |
1827 if (range && !empty2) | 1906 if (lp->ll_range && !lp->ll_empty2) |
1828 clear_tv(&var2); | 1907 clear_tv(&var2); |
1829 break; | 1908 return NULL; |
1830 } | 1909 } |
1831 | 1910 |
1832 /* | 1911 /* |
1833 * May need to find the item or absolute index for the second | 1912 * May need to find the item or absolute index for the second |
1834 * index of a range. | 1913 * index of a range. |
1835 * When no index given: "empty2" is TRUE. | 1914 * When no index given: "lp->ll_empty2" is TRUE. |
1836 * Otherwise "n2" is set to the second index. | 1915 * Otherwise "lp->ll_n2" is set to the second index. |
1837 */ | 1916 */ |
1838 if (range && !empty2) | 1917 if (lp->ll_range && !lp->ll_empty2) |
1839 { | 1918 { |
1840 n2 = get_tv_number(&var2); | 1919 lp->ll_n2 = get_tv_number(&var2); |
1841 clear_tv(&var2); | 1920 clear_tv(&var2); |
1842 if (n2 < 0) | 1921 if (lp->ll_n2 < 0) |
1843 { | 1922 { |
1844 ni = list_find(l, n2); | 1923 ni = list_find(lp->ll_list, lp->ll_n2); |
1845 if (ni == NULL) | 1924 if (ni == NULL) |
1846 { | 1925 { |
1847 EMSGN(_(e_listidx), n2); | 1926 if (!quiet) |
1848 p = NULL; | 1927 EMSGN(_(e_listidx), lp->ll_n2); |
1849 break; | 1928 return NULL; |
1850 } | 1929 } |
1851 n2 = list_idx_of_item(l, ni); | 1930 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni); |
1852 } | 1931 } |
1853 | 1932 |
1854 /* Check that n2 isn't before n1. */ | 1933 /* Check that lp->ll_n2 isn't before lp->ll_n1. */ |
1855 if (n1 < 0) | 1934 if (lp->ll_n1 < 0) |
1856 n1 = list_idx_of_item(l, li); | 1935 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li); |
1857 if (n2 < n1) | 1936 if (lp->ll_n2 < lp->ll_n1) |
1858 { | 1937 { |
1859 EMSGN(_(e_listidx), n2); | 1938 if (!quiet) |
1860 p = NULL; | 1939 EMSGN(_(e_listidx), lp->ll_n2); |
1940 return NULL; | |
1941 } | |
1942 } | |
1943 | |
1944 lp->ll_tv = &lp->ll_li->li_tv; | |
1945 } | |
1946 } | |
1947 | |
1948 return p; | |
1949 } | |
1950 | |
1951 /* | |
1952 * Clear an "lval" that was filled by get_lval(). | |
1953 */ | |
1954 static void | |
1955 clear_lval(lp) | |
1956 lval *lp; | |
1957 { | |
1958 vim_free(lp->ll_exp_name); | |
1959 vim_free(lp->ll_newkey); | |
1960 } | |
1961 | |
1962 /* | |
1963 * Set a variable that was parsed by get_lval(). | |
1964 * "endp" points to just after the parsed name. | |
1965 */ | |
1966 static void | |
1967 set_var_lval(lp, endp, rettv, copy) | |
1968 lval *lp; | |
1969 char_u *endp; | |
1970 typeval *rettv; | |
1971 int copy; | |
1972 { | |
1973 int cc; | |
1974 listitem *ni; | |
1975 listitem *ri; | |
1976 dictitem *di; | |
1977 | |
1978 if (lp->ll_tv == NULL) | |
1979 { | |
1980 if (!check_changedtick(lp->ll_name)) | |
1981 { | |
1982 cc = *endp; | |
1983 *endp = NUL; | |
1984 set_var(lp->ll_name, rettv, copy); | |
1985 *endp = cc; | |
1986 } | |
1987 } | |
1988 else if (lp->ll_range) | |
1989 { | |
1990 /* | |
1991 * Assign the List values to the list items. | |
1992 */ | |
1993 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) | |
1994 { | |
1995 clear_tv(&lp->ll_li->li_tv); | |
1996 copy_tv(&ri->li_tv, &lp->ll_li->li_tv); | |
1997 ri = ri->li_next; | |
1998 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1)) | |
1999 break; | |
2000 if (lp->ll_li->li_next == NULL) | |
2001 { | |
2002 /* Need to add an empty item. */ | |
2003 ni = listitem_alloc(); | |
2004 if (ni == NULL) | |
2005 { | |
2006 ri = NULL; | |
1861 break; | 2007 break; |
1862 } | 2008 } |
2009 ni->li_tv.v_type = VAR_NUMBER; | |
2010 ni->li_tv.vval.v_number = 0; | |
2011 list_append(lp->ll_list, ni); | |
1863 } | 2012 } |
1864 | 2013 lp->ll_li = lp->ll_li->li_next; |
1865 tv = &li->li_tv; | 2014 ++lp->ll_n1; |
1866 } | 2015 } |
1867 } | 2016 if (ri != NULL) |
1868 | 2017 EMSG(_("E710: List value has more items than target")); |
1869 if (p != NULL) | 2018 else if (lp->ll_empty2 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL) |
1870 { | 2019 : lp->ll_n1 != lp->ll_n2) |
1871 p = skipwhite(p); | 2020 EMSG(_("E711: List value has not enough items")); |
1872 if (endchars != NULL && vim_strchr(endchars, *p) == NULL) | 2021 } |
1873 { | 2022 else |
1874 EMSG(_(e_letunexp)); | 2023 { |
1875 p = NULL; | 2024 /* |
1876 } | 2025 * Assign to a List or Dictionary item. |
1877 else if (range) | 2026 */ |
1878 { | 2027 if (lp->ll_newkey != NULL) |
1879 /* | 2028 { |
1880 * Assign the List values to the list items. | 2029 /* Need to add an item to the Dictionary. */ |
1881 */ | 2030 di = dictitem_alloc(); |
1882 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) | 2031 if (di == NULL) |
1883 { | 2032 return; |
1884 clear_tv(&li->li_tv); | 2033 di->di_key = lp->ll_newkey; |
1885 copy_tv(&ri->li_tv, &li->li_tv); | 2034 lp->ll_newkey = NULL; |
1886 ri = ri->li_next; | 2035 dict_add(lp->ll_tv->vval.v_dict, di); |
1887 if (ri == NULL || (!empty2 && n2 == n1)) | 2036 lp->ll_tv = &di->di_tv; |
1888 break; | |
1889 if (li->li_next == NULL) | |
1890 { | |
1891 /* Need to add an empty item. */ | |
1892 ni = listitem_alloc(); | |
1893 if (ni == NULL) | |
1894 { | |
1895 ri = NULL; | |
1896 break; | |
1897 } | |
1898 ni->li_tv.v_type = VAR_NUMBER; | |
1899 ni->li_tv.vval.v_number = 0; | |
1900 list_append(l, ni); | |
1901 } | |
1902 li = li->li_next; | |
1903 ++n1; | |
1904 } | |
1905 if (ri != NULL) | |
1906 EMSG(_("E710: List value has more items than target")); | |
1907 else if (empty2 ? li != NULL && li->li_next != NULL : n1 != n2) | |
1908 EMSG(_("E711: List value has not enough items")); | |
1909 } | 2037 } |
1910 else | 2038 else |
1911 { | 2039 clear_tv(lp->ll_tv); |
1912 if (newkey != NULL) | 2040 |
1913 { | 2041 /* |
1914 /* Need to add the item to the dictionary. */ | 2042 * Assign the value to the variable or list item. |
1915 di = dictitem_alloc(); | 2043 */ |
1916 if (di == NULL) | 2044 if (copy) |
1917 p = NULL; | 2045 copy_tv(rettv, lp->ll_tv); |
1918 else | 2046 else |
1919 { | 2047 { |
1920 di->di_key = newkey; | 2048 *lp->ll_tv = *rettv; |
1921 newkey = NULL; | 2049 init_tv(rettv); |
1922 dict_add(tv->vval.v_dict, di); | 2050 } |
1923 tv = &di->di_tv; | 2051 } |
1924 } | |
1925 } | |
1926 else | |
1927 clear_tv(tv); | |
1928 | |
1929 /* | |
1930 * Assign the value to the variable or list item. | |
1931 */ | |
1932 if (p != NULL) | |
1933 { | |
1934 if (copy) | |
1935 copy_tv(rettv, tv); | |
1936 else | |
1937 { | |
1938 *tv = *rettv; | |
1939 init_tv(rettv); | |
1940 } | |
1941 } | |
1942 } | |
1943 } | |
1944 vim_free(newkey); | |
1945 return p; | |
1946 } | 2052 } |
1947 | 2053 |
1948 /* | 2054 /* |
1949 * Add a watcher to a list. | 2055 * Add a watcher to a list. |
1950 */ | 2056 */ |
2300 ex_unlet(eap) | 2406 ex_unlet(eap) |
2301 exarg_T *eap; | 2407 exarg_T *eap; |
2302 { | 2408 { |
2303 char_u *arg = eap->arg; | 2409 char_u *arg = eap->arg; |
2304 char_u *name_end; | 2410 char_u *name_end; |
2305 char_u cc; | |
2306 char_u *expr_start; | |
2307 char_u *expr_end; | |
2308 int error = FALSE; | 2411 int error = FALSE; |
2309 | 2412 |
2310 do | 2413 do |
2311 { | 2414 { |
2312 /* Find the end of the name. */ | 2415 lval lv; |
2313 name_end = find_name_end(arg, &expr_start, &expr_end, TRUE); | 2416 |
2314 | 2417 /* Parse the name and find the end. */ |
2315 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end)) | 2418 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE); |
2316 { | 2419 if (lv.ll_name == NULL) |
2317 emsg_severe = TRUE; | 2420 error = TRUE; /* error but continue parsing */ |
2318 EMSG(_(e_trailing)); | 2421 if (name_end == NULL || (!vim_iswhite(*name_end) |
2422 && !ends_excmd(*name_end))) | |
2423 { | |
2424 if (name_end != NULL) | |
2425 { | |
2426 emsg_severe = TRUE; | |
2427 EMSG(_(e_trailing)); | |
2428 } | |
2429 if (!(eap->skip || error)) | |
2430 clear_lval(&lv); | |
2319 break; | 2431 break; |
2320 } | 2432 } |
2321 | 2433 |
2322 if (!error && !eap->skip) | 2434 if (!error && !eap->skip) |
2323 { | 2435 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL) |
2324 if (expr_start != NULL) | 2436 error = TRUE; |
2325 { | 2437 |
2326 char_u *temp_string; | 2438 if (!eap->skip) |
2327 | 2439 clear_lval(&lv); |
2328 temp_string = make_expanded_name(arg, expr_start, | 2440 |
2329 expr_end, name_end); | |
2330 if (temp_string == NULL) | |
2331 { | |
2332 /* | |
2333 * Report an invalid expression in braces, unless the | |
2334 * expression evaluation has been cancelled due to an | |
2335 * aborting error, an interrupt, or an exception. | |
2336 */ | |
2337 if (!aborting()) | |
2338 { | |
2339 emsg_severe = TRUE; | |
2340 EMSG2(_(e_invarg2), arg); | |
2341 break; | |
2342 } | |
2343 error = TRUE; | |
2344 } | |
2345 else | |
2346 { | |
2347 if (do_unlet_var(temp_string, eap->forceit) == FAIL) | |
2348 error = TRUE; | |
2349 vim_free(temp_string); | |
2350 } | |
2351 } | |
2352 else | |
2353 { | |
2354 cc = *name_end; | |
2355 *name_end = NUL; | |
2356 if (do_unlet_var(arg, eap->forceit) == FAIL) | |
2357 error = TRUE; | |
2358 *name_end = cc; | |
2359 } | |
2360 } | |
2361 arg = skipwhite(name_end); | 2441 arg = skipwhite(name_end); |
2362 } while (!ends_excmd(*arg)); | 2442 } while (!ends_excmd(*arg)); |
2363 | 2443 |
2364 eap->nextcmd = check_nextcmd(arg); | 2444 eap->nextcmd = check_nextcmd(arg); |
2365 } | 2445 } |
2366 | 2446 |
2447 | |
2367 static int | 2448 static int |
2368 do_unlet_var(name, forceit) | 2449 do_unlet_var(lp, name_end, forceit) |
2369 char_u *name; | 2450 lval *lp; |
2451 char_u *name_end; | |
2370 int forceit; | 2452 int forceit; |
2371 { | 2453 { |
2372 if (check_changedtick(name)) | 2454 int ret = OK; |
2373 return FAIL; | 2455 int cc; |
2374 if (do_unlet(name) == FAIL && !forceit) | 2456 |
2375 { | 2457 if (lp->ll_tv == NULL) |
2376 EMSG2(_("E108: No such variable: \"%s\""), name); | 2458 { |
2377 return FAIL; | 2459 cc = *name_end; |
2378 } | 2460 *name_end = NUL; |
2379 return OK; | 2461 |
2462 /* Normal name or expanded name. */ | |
2463 if (check_changedtick(lp->ll_name)) | |
2464 ret = FAIL; | |
2465 else if (do_unlet(lp->ll_name) == FAIL && !forceit) | |
2466 { | |
2467 EMSG2(_("E108: No such variable: \"%s\""), lp->ll_name); | |
2468 ret = FAIL; | |
2469 } | |
2470 *name_end = cc; | |
2471 } | |
2472 else if (lp->ll_range) | |
2473 { | |
2474 listitem *li; | |
2475 | |
2476 /* Delete a range of List items. */ | |
2477 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) | |
2478 { | |
2479 li = lp->ll_li->li_next; | |
2480 listitem_remove(lp->ll_list, lp->ll_li); | |
2481 lp->ll_li = li; | |
2482 ++lp->ll_n1; | |
2483 } | |
2484 } | |
2485 else | |
2486 { | |
2487 clear_tv(lp->ll_tv); | |
2488 if (lp->ll_list != NULL) | |
2489 { | |
2490 /* unlet a List item. */ | |
2491 listitem_remove(lp->ll_list, lp->ll_li); | |
2492 } | |
2493 else | |
2494 { | |
2495 /* unlet a Dictionary item. */ | |
2496 *lp->ll_pdi = lp->ll_di->di_next; | |
2497 dictitem_free(lp->ll_di); | |
2498 } | |
2499 } | |
2500 | |
2501 return ret; | |
2380 } | 2502 } |
2381 | 2503 |
2382 /* | 2504 /* |
2383 * "unlet" a variable. Return OK if it existed, FAIL if not. | 2505 * "unlet" a variable. Return OK if it existed, FAIL if not. |
2384 */ | 2506 */ |
3677 clear_tv(&var1); | 3799 clear_tv(&var1); |
3678 return FAIL; | 3800 return FAIL; |
3679 } | 3801 } |
3680 } | 3802 } |
3681 | 3803 |
3682 item = dict_find(rettv->vval.v_dict, key, (int)len); | 3804 item = dict_find(rettv->vval.v_dict, key, (int)len, NULL); |
3683 | 3805 |
3684 if (item == NULL) | 3806 if (item == NULL) |
3685 EMSG2(_("E999: Key not found in Dictionary: %s"), key); | 3807 EMSG2(_("E999: Key not found in Dictionary: %s"), key); |
3686 if (len == -1) | 3808 if (len == -1) |
3687 clear_tv(&var1); | 3809 clear_tv(&var1); |
4108 clear_tv(&item->li_tv); | 4230 clear_tv(&item->li_tv); |
4109 vim_free(item); | 4231 vim_free(item); |
4110 } | 4232 } |
4111 | 4233 |
4112 /* | 4234 /* |
4235 * Remove a list item from a List and free it. Also clears the value. | |
4236 */ | |
4237 static void | |
4238 listitem_remove(l, item) | |
4239 listvar *l; | |
4240 listitem *item; | |
4241 { | |
4242 list_remove(l, item, item); | |
4243 listitem_free(item); | |
4244 } | |
4245 | |
4246 /* | |
4113 * Get the number of items in a list. | 4247 * Get the number of items in a list. |
4114 */ | 4248 */ |
4115 static long | 4249 static long |
4116 list_len(l) | 4250 list_len(l) |
4117 listvar *l; | 4251 listvar *l; |
4447 return copy; | 4581 return copy; |
4448 } | 4582 } |
4449 | 4583 |
4450 /* | 4584 /* |
4451 * Remove items "item" to "item2" from list "l". | 4585 * Remove items "item" to "item2" from list "l". |
4452 */ | 4586 * Does not free the listitem or the value! |
4453 static void | 4587 */ |
4454 list_getrem(l, item, item2) | 4588 static void |
4589 list_remove(l, item, item2) | |
4455 listvar *l; | 4590 listvar *l; |
4456 listitem *item; | 4591 listitem *item; |
4457 listitem *item2; | 4592 listitem *item2; |
4458 { | 4593 { |
4459 listitem *ip; | 4594 listitem *ip; |
4685 void *val; | 4820 void *val; |
4686 { | 4821 { |
4687 dictitem *di; | 4822 dictitem *di; |
4688 char_u *dkey; | 4823 char_u *dkey; |
4689 | 4824 |
4690 di = dict_find(d, (char_u *)key, -1); | 4825 di = dict_find(d, (char_u *)key, -1, NULL); |
4691 if (di == NULL) | 4826 if (di == NULL) |
4692 { | 4827 { |
4693 dkey = vim_strsave((char_u *)key); | 4828 dkey = vim_strsave((char_u *)key); |
4694 if (dkey != NULL) | 4829 if (dkey != NULL) |
4695 { | 4830 { |
4741 } | 4876 } |
4742 | 4877 |
4743 /* | 4878 /* |
4744 * Find item "key[len]" in Dictionary "d". | 4879 * Find item "key[len]" in Dictionary "d". |
4745 * If "len" is negative use strlen(key). | 4880 * If "len" is negative use strlen(key). |
4881 * Sets "*pdi" to pointer to found item, unless "pdi" is NULL. | |
4746 * Returns NULL when not found. | 4882 * Returns NULL when not found. |
4747 */ | 4883 */ |
4748 static dictitem * | 4884 static dictitem * |
4749 dict_find(d, key, len) | 4885 dict_find(d, key, len, pdi) |
4750 dictvar *d; | 4886 dictvar *d; |
4751 char_u *key; | 4887 char_u *key; |
4752 int len; | 4888 int len; |
4889 dictitem ***pdi; | |
4753 { | 4890 { |
4754 static dictitem *di; | 4891 static dictitem *di; |
4755 | 4892 |
4893 if (pdi != NULL) | |
4894 *pdi = &d->dv_first; | |
4756 for (di = d->dv_first; di != NULL; di = di->di_next) | 4895 for (di = d->dv_first; di != NULL; di = di->di_next) |
4896 { | |
4757 if (len < 0 | 4897 if (len < 0 |
4758 ? STRCMP(di->di_key, key) == 0 | 4898 ? STRCMP(di->di_key, key) == 0 |
4759 : STRNCMP(di->di_key, key, len) == 0 && di->di_key[len] == NUL) | 4899 : STRNCMP(di->di_key, key, len) == 0 && di->di_key[len] == NUL) |
4760 return di; | 4900 return di; |
4901 if (pdi != NULL) | |
4902 *pdi = &di->di_next; | |
4903 } | |
4761 return NULL; | 4904 return NULL; |
4762 } | 4905 } |
4763 | 4906 |
4764 /* | 4907 /* |
4765 * Return an allocated string with the string representation of a Dictionary. | 4908 * Return an allocated string with the string representation of a Dictionary. |
4873 vim_free(key); | 5016 vim_free(key); |
4874 goto failret; | 5017 goto failret; |
4875 } | 5018 } |
4876 if (evaluate) | 5019 if (evaluate) |
4877 { | 5020 { |
4878 item = dict_find(d, key, -1); | 5021 item = dict_find(d, key, -1, NULL); |
4879 if (item != NULL) | 5022 if (item != NULL) |
4880 { | 5023 { |
4881 EMSG(_("E999: Duplicate key in Dictionary")); | 5024 EMSG(_("E999: Duplicate key in Dictionary")); |
4882 vim_free(key); | 5025 vim_free(key); |
4883 clear_tv(&tv); | 5026 clear_tv(&tv); |
5015 static char_u * | 5158 static char_u * |
5016 string_quote(str, function) | 5159 string_quote(str, function) |
5017 char_u *str; | 5160 char_u *str; |
5018 int function; | 5161 int function; |
5019 { | 5162 { |
5020 unsigned len = function ? 13 : 3; | 5163 unsigned len = STRLEN(str) + (function ? 13 : 3); |
5021 char_u *p, *r, *s; | 5164 char_u *p, *r, *s; |
5022 | 5165 |
5023 for (p = str; *p != NUL; mb_ptr_adv(p)) | 5166 for (p = str; *p != NUL; mb_ptr_adv(p)) |
5024 if (*p == '\'') | 5167 if (*p == '\'') |
5025 ++len; | 5168 ++len; |
6820 | 6963 |
6821 /* Go over all entries in the second dict and add them to the | 6964 /* Go over all entries in the second dict and add them to the |
6822 * first dict. */ | 6965 * first dict. */ |
6823 for (d2i = d2->dv_first; d2i != NULL; d2i = d2i->di_next) | 6966 for (d2i = d2->dv_first; d2i != NULL; d2i = d2i->di_next) |
6824 { | 6967 { |
6825 d1i = dict_find(d1, d2i->di_key, -1); | 6968 d1i = dict_find(d1, d2i->di_key, -1, NULL); |
6826 if (d1i == NULL) | 6969 if (d1i == NULL) |
6827 { | 6970 { |
6828 d1i = dictitem_copy(d2i); | 6971 d1i = dictitem_copy(d2i); |
6829 if (d1i != NULL) | 6972 if (d1i != NULL) |
6830 dict_add(d1, d1i); | 6973 dict_add(d1, d1i); |
7027 { | 7170 { |
7028 nli = li->li_next; | 7171 nli = li->li_next; |
7029 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL) | 7172 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL) |
7030 break; | 7173 break; |
7031 if (!map && rem) | 7174 if (!map && rem) |
7032 { | 7175 listitem_remove(l, li); |
7033 clear_tv(&li->li_tv); | |
7034 list_getrem(l, li, li); | |
7035 } | |
7036 } | 7176 } |
7037 } | 7177 } |
7038 | 7178 |
7039 clear_tv(&vimvars[VV_VAL].tv); | 7179 clear_tv(&vimvars[VV_VAL].tv); |
7040 vimvars[VV_VAL].tv = save_val; | 7180 vimvars[VV_VAL].tv = save_val; |
7380 } | 7520 } |
7381 else if (argvars[0].v_type == VAR_DICT) | 7521 else if (argvars[0].v_type == VAR_DICT) |
7382 { | 7522 { |
7383 if ((d = argvars[0].vval.v_dict) != NULL) | 7523 if ((d = argvars[0].vval.v_dict) != NULL) |
7384 { | 7524 { |
7385 di = dict_find(d, get_tv_string(&argvars[1]), -1); | 7525 di = dict_find(d, get_tv_string(&argvars[1]), -1, NULL); |
7386 if (di != NULL) | 7526 if (di != NULL) |
7387 tv = &di->di_tv; | 7527 tv = &di->di_tv; |
7388 } | 7528 } |
7389 } | 7529 } |
7390 else | 7530 else |
8513 } | 8653 } |
8514 if (argvars[0].vval.v_dict == NULL) | 8654 if (argvars[0].vval.v_dict == NULL) |
8515 return; | 8655 return; |
8516 | 8656 |
8517 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict, | 8657 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict, |
8518 get_tv_string(&argvars[1]), -1) != NULL; | 8658 get_tv_string(&argvars[1]), -1, NULL) != NULL; |
8519 } | 8659 } |
8520 | 8660 |
8521 /* | 8661 /* |
8522 * "hasmapto()" function | 8662 * "hasmapto()" function |
8523 */ | 8663 */ |
10105 else | 10245 else |
10106 { | 10246 { |
10107 if (argvars[2].v_type == VAR_UNKNOWN) | 10247 if (argvars[2].v_type == VAR_UNKNOWN) |
10108 { | 10248 { |
10109 /* Remove one item, return its value. */ | 10249 /* Remove one item, return its value. */ |
10110 list_getrem(l, item, item); | 10250 list_remove(l, item, item); |
10111 *rettv = item->li_tv; | 10251 *rettv = item->li_tv; |
10112 vim_free(item); | 10252 vim_free(item); |
10113 } | 10253 } |
10114 else | 10254 else |
10115 { | 10255 { |
10124 ; | 10264 ; |
10125 if (li == NULL) /* didn't find "item2" after "item" */ | 10265 if (li == NULL) /* didn't find "item2" after "item" */ |
10126 EMSG(_(e_invrange)); | 10266 EMSG(_(e_invrange)); |
10127 else | 10267 else |
10128 { | 10268 { |
10129 list_getrem(l, item, item2); | 10269 list_remove(l, item, item2); |
10130 l = list_alloc(); | 10270 l = list_alloc(); |
10131 if (l != NULL) | 10271 if (l != NULL) |
10132 { | 10272 { |
10133 rettv->v_type = VAR_LIST; | 10273 rettv->v_type = VAR_LIST; |
10134 rettv->vval.v_list = l; | 10274 rettv->vval.v_list = l; |
13609 msg_putchar('\n'); | 13749 msg_putchar('\n'); |
13610 msg_puts((char_u *)" endfunction"); | 13750 msg_puts((char_u *)" endfunction"); |
13611 } | 13751 } |
13612 } | 13752 } |
13613 else | 13753 else |
13614 EMSG2(_("E123: Undefined function: %s"), eap->arg); | 13754 EMSG2(_("E123: Undefined function: %s"), name); |
13615 } | 13755 } |
13616 goto erret_name; | 13756 goto erret_name; |
13617 } | 13757 } |
13618 | 13758 |
13619 /* | 13759 /* |
13899 * Get a function name, translating "<SID>" and "<SNR>". | 14039 * Get a function name, translating "<SID>" and "<SNR>". |
13900 * Returns the function name in allocated memory, or NULL for failure. | 14040 * Returns the function name in allocated memory, or NULL for failure. |
13901 * Advances "pp" to just after the function name (if no error). | 14041 * Advances "pp" to just after the function name (if no error). |
13902 */ | 14042 */ |
13903 static char_u * | 14043 static char_u * |
13904 trans_function_name(pp, skip, internal) | 14044 trans_function_name(pp, skip, exists) |
13905 char_u **pp; | 14045 char_u **pp; |
13906 int skip; /* only find the end, don't evaluate */ | 14046 int skip; /* only find the end, don't evaluate */ |
13907 int internal; /* TRUE if internal function name OK */ | 14047 int exists; /* TRUE for exists(): internal function name |
13908 { | 14048 OK and be quiet. */ |
13909 char_u *name; | 14049 { |
14050 char_u *name = NULL; | |
13910 char_u *start; | 14051 char_u *start; |
13911 char_u *end; | 14052 char_u *end; |
13912 int lead; | 14053 int lead; |
13913 char_u sid_buf[20]; | 14054 char_u sid_buf[20]; |
14055 int len; | |
14056 #if 0 | |
14057 char_u *expr_start, *expr_end; | |
13914 char_u *temp_string = NULL; | 14058 char_u *temp_string = NULL; |
13915 char_u *expr_start, *expr_end; | 14059 #else |
13916 int len; | 14060 lval lv; |
14061 #endif | |
13917 | 14062 |
13918 /* A name starting with "<SID>" or "<SNR>" is local to a script. */ | 14063 /* A name starting with "<SID>" or "<SNR>" is local to a script. */ |
13919 start = *pp; | 14064 start = *pp; |
13920 lead = eval_fname_script(start); | 14065 lead = eval_fname_script(start); |
13921 if (lead > 0) | 14066 if (lead > 0) |
13922 start += lead; | 14067 start += lead; |
14068 | |
14069 #if 1 | |
14070 end = get_lval(start, NULL, &lv, FALSE, skip, exists); | |
14071 if (end == start) | |
14072 { | |
14073 if (!skip) | |
14074 EMSG(_("E129: Function name required")); | |
14075 goto theend; | |
14076 } | |
14077 if (end == NULL || (lv.ll_tv != NULL && (lead > 0 || lv.ll_range))) | |
14078 { | |
14079 /* | |
14080 * Report an invalid expression in braces, unless the expression | |
14081 * evaluation has been cancelled due to an aborting error, an | |
14082 * interrupt, or an exception. | |
14083 */ | |
14084 if (!aborting()) | |
14085 { | |
14086 if (end != NULL) | |
14087 EMSG2(_(e_invarg2), start); | |
14088 } | |
14089 else | |
14090 *pp = find_name_end(start, NULL, NULL, TRUE); | |
14091 goto theend; | |
14092 } | |
14093 | |
14094 if (lv.ll_tv != NULL) | |
14095 { | |
14096 /* TODO: When defining a function accept a Dict here. */ | |
14097 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL) | |
14098 { | |
14099 name = vim_strsave(lv.ll_tv->vval.v_string); | |
14100 *pp = end; | |
14101 } | |
14102 else | |
14103 { | |
14104 if (!skip && !exists) | |
14105 EMSG(_("E999: Funcref required")); | |
14106 name = NULL; | |
14107 } | |
14108 goto theend; | |
14109 } | |
14110 | |
14111 if (lv.ll_name == NULL) | |
14112 { | |
14113 /* Error found, but continue after the function name. */ | |
14114 *pp = end; | |
14115 goto theend; | |
14116 } | |
14117 | |
14118 if (lv.ll_exp_name != NULL) | |
14119 len = STRLEN(lv.ll_exp_name); | |
14120 else | |
14121 len = (int)(end - start); | |
14122 | |
14123 /* | |
14124 * Copy the function name to allocated memory. | |
14125 * Accept <SID>name() inside a script, translate into <SNR>123_name(). | |
14126 * Accept <SNR>123_name() outside a script. | |
14127 */ | |
14128 if (skip) | |
14129 lead = 0; /* do nothing */ | |
14130 else if (lead > 0) | |
14131 { | |
14132 lead = 3; | |
14133 if (eval_fname_sid(*pp)) /* If it's "<SID>" */ | |
14134 { | |
14135 if (current_SID <= 0) | |
14136 { | |
14137 EMSG(_(e_usingsid)); | |
14138 goto theend; | |
14139 } | |
14140 sprintf((char *)sid_buf, "%ld_", (long)current_SID); | |
14141 lead += (int)STRLEN(sid_buf); | |
14142 } | |
14143 } | |
14144 else if (!exists && !ASCII_ISUPPER(*lv.ll_name)) | |
14145 { | |
14146 EMSG2(_("E128: Function name must start with a capital: %s"), | |
14147 lv.ll_name); | |
14148 goto theend; | |
14149 } | |
14150 name = alloc((unsigned)(len + lead + 1)); | |
14151 if (name != NULL) | |
14152 { | |
14153 if (lead > 0) | |
14154 { | |
14155 name[0] = K_SPECIAL; | |
14156 name[1] = KS_EXTRA; | |
14157 name[2] = (int)KE_SNR; | |
14158 if (eval_fname_sid(*pp)) /* If it's "<SID>" */ | |
14159 STRCPY(name + 3, sid_buf); | |
14160 } | |
14161 mch_memmove(name + lead, lv.ll_name, (size_t)len); | |
14162 name[len + lead] = NUL; | |
14163 } | |
14164 *pp = end; | |
14165 | |
14166 theend: | |
14167 clear_lval(&lv); | |
14168 return name; | |
14169 #endif | |
14170 | |
14171 #if 0 | |
14172 | |
13923 end = find_name_end(start, &expr_start, &expr_end, FALSE); | 14173 end = find_name_end(start, &expr_start, &expr_end, FALSE); |
13924 if (end == start) | 14174 if (end == start) |
13925 { | 14175 { |
13926 if (!skip) | 14176 if (!skip) |
13927 EMSG(_("E129: Function name required")); | 14177 EMSG(_("E129: Function name required")); |
13969 } | 14219 } |
13970 sprintf((char *)sid_buf, "%ld_", (long)current_SID); | 14220 sprintf((char *)sid_buf, "%ld_", (long)current_SID); |
13971 lead += (int)STRLEN(sid_buf); | 14221 lead += (int)STRLEN(sid_buf); |
13972 } | 14222 } |
13973 } | 14223 } |
13974 else if (!internal && !ASCII_ISUPPER(*start)) | 14224 else if (!exists && !ASCII_ISUPPER(*start)) |
13975 { | 14225 { |
13976 EMSG2(_("E128: Function name must start with a capital: %s"), start); | 14226 EMSG2(_("E128: Function name must start with a capital: %s"), start); |
13977 return NULL; | 14227 return NULL; |
13978 } | 14228 } |
13979 name = alloc((unsigned)(len + lead + 1)); | 14229 name = alloc((unsigned)(len + lead + 1)); |
13992 } | 14242 } |
13993 *pp = end; | 14243 *pp = end; |
13994 | 14244 |
13995 vim_free(temp_string); | 14245 vim_free(temp_string); |
13996 return name; | 14246 return name; |
14247 #endif | |
13997 } | 14248 } |
13998 | 14249 |
13999 /* | 14250 /* |
14000 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case). | 14251 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case). |
14001 * Return 2 if "p" starts with "s:". | 14252 * Return 2 if "p" starts with "s:". |