comparison src/eval.c @ 100:1f3902f3eb5c v7.0038

updated for version 7.0038
author vimboss
date Fri, 14 Jan 2005 21:53:12 +0000
parents d4f3db33d782
children 2b913f7eb9d2
comparison
equal deleted inserted replaced
99:04f2e519ab18 100:1f3902f3eb5c
45 union 45 union
46 { 46 {
47 varnumber_T v_number; /* number value */ 47 varnumber_T v_number; /* number value */
48 char_u *v_string; /* string value (can be NULL!) */ 48 char_u *v_string; /* string value (can be NULL!) */
49 struct listvar_S *v_list; /* list value (can be NULL!) */ 49 struct listvar_S *v_list; /* list value (can be NULL!) */
50 struct dictvar_S *v_dict; /* dict value (can be NULL!) */
50 } vval; 51 } vval;
51 } typeval; 52 } typeval;
52 53
53 /* Values for "v_type". */ 54 /* Values for "v_type". */
54 #define VAR_UNKNOWN 0 55 #define VAR_UNKNOWN 0
55 #define VAR_NUMBER 1 /* "v_number" is used */ 56 #define VAR_NUMBER 1 /* "v_number" is used */
56 #define VAR_STRING 2 /* "v_string" is used */ 57 #define VAR_STRING 2 /* "v_string" is used */
57 #define VAR_FUNC 3 /* "v_string" is function name */ 58 #define VAR_FUNC 3 /* "v_string" is function name */
58 #define VAR_LIST 4 /* "v_list" is used */ 59 #define VAR_LIST 4 /* "v_list" is used */
60 #define VAR_DICT 5 /* "v_dict" is used */
59 61
60 /* 62 /*
61 * Structure to hold an internal variable with a name. 63 * Structure to hold an internal variable with a name.
62 * The "tv" must come first, so that this can be used as a "typeval" as well. 64 * The "tv" must come first, so that this can be used as a "typeval" as well.
63 */ 65 */
102 }; 104 };
103 105
104 typedef struct listvar_S listvar; 106 typedef struct listvar_S listvar;
105 107
106 #define VAR_LIST_MAXNEST 100 /* maximum nesting of lists */ 108 #define VAR_LIST_MAXNEST 100 /* maximum nesting of lists */
109
110 /*
111 * Structure to hold an item of a Dictionary.
112 */
113 struct dictitem_S
114 {
115 struct dictitem_S *di_next; /* next item in list */
116 char_u *di_key; /* key string */
117 typeval di_tv; /* type and value of the variable */
118 };
119
120 typedef struct dictitem_S dictitem;
121
122 /*
123 * Structure to hold info about a Dictionary.
124 */
125 struct dictvar_S
126 {
127 int dv_refcount; /* reference count */
128 dictitem *dv_first; /* first item, NULL if none */
129 };
130
131 typedef struct dictvar_S dictvar;
132
133
107 static char *e_letunexp = N_("E18: Unexpected characters in :let"); 134 static char *e_letunexp = N_("E18: Unexpected characters in :let");
108 static char *e_listidx = N_("E684: list index out of range: %ld"); 135 static char *e_listidx = N_("E684: list index out of range: %ld");
109 static char *e_undefvar = N_("E121: Undefined variable: %s"); 136 static char *e_undefvar = N_("E121: Undefined variable: %s");
110 static char *e_missbrac = N_("E111: Missing ']'"); 137 static char *e_missbrac = N_("E111: Missing ']'");
111 static char *e_intern2 = N_("E685: Internal error: %s"); 138 static char *e_intern2 = N_("E685: Internal error: %s");
112 static char *e_listarg = N_("E686: Argument of %s must be a list"); 139 static char *e_listarg = N_("E686: Argument of %s must be a List");
140 static char *e_listdictarg = N_("E999: Argument of %s must be a List or Dictionaary");
141 static char *e_emptykey = N_("E999: Empty key in Dictionary");
142 static char *e_listreq = N_("E999: List required");
143 static char *e_dictreq = N_("E999: Dictionary required");
144 static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
145 static char *e_dictkey = N_("E999: key not found in Dictionary: %s");
113 146
114 /* 147 /*
115 * All user-defined global variables are stored in "variables". 148 * All user-defined global variables are stored in "variables".
116 */ 149 */
117 garray_T variables = {0, 0, sizeof(var), 4, NULL}; 150 garray_T variables = {0, 0, sizeof(var), 4, NULL};
314 static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate)); 347 static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
315 static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate)); 348 static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
316 static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate)); 349 static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
317 static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate)); 350 static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
318 static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate)); 351 static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
319 static int get_sharp_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
320 static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate)); 352 static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
321 static listvar *list_alloc __ARGS((void)); 353 static listvar *list_alloc __ARGS((void));
322 static void list_unref __ARGS((listvar *l)); 354 static void list_unref __ARGS((listvar *l));
323 static void list_free __ARGS((listvar *l)); 355 static void list_free __ARGS((listvar *l));
324 static listitem *listitem_alloc __ARGS((void)); 356 static listitem *listitem_alloc __ARGS((void));
337 static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv)); 369 static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
338 static listvar *list_copy __ARGS((listvar *orig, int deep)); 370 static listvar *list_copy __ARGS((listvar *orig, int deep));
339 static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2)); 371 static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2));
340 static char_u *list2string __ARGS((typeval *tv)); 372 static char_u *list2string __ARGS((typeval *tv));
341 static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo)); 373 static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo));
374
375 static dictvar *dict_alloc __ARGS((void));
376 static void dict_unref __ARGS((dictvar *d));
377 static void dict_free __ARGS((dictvar *d));
378 static dictitem *dictitem_alloc __ARGS((void));
379 static void dictitem_free __ARGS((dictitem *item));
380 static void dict_add __ARGS((dictvar *d, dictitem *item));
381 static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len));
382 static char_u *dict2string __ARGS((typeval *tv));
383 static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
342 384
343 static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf)); 385 static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
344 static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf)); 386 static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
345 static char_u *string_quote __ARGS((char_u *str, int function)); 387 static char_u *string_quote __ARGS((char_u *str, int function));
346 static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate)); 388 static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
436 static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv)); 478 static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
437 static void f_inputsave __ARGS((typeval *argvars, typeval *rettv)); 479 static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
438 static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv)); 480 static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
439 static void f_insert __ARGS((typeval *argvars, typeval *rettv)); 481 static void f_insert __ARGS((typeval *argvars, typeval *rettv));
440 static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv)); 482 static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
483 static void f_items __ARGS((typeval *argvars, typeval *rettv));
441 static void f_join __ARGS((typeval *argvars, typeval *rettv)); 484 static void f_join __ARGS((typeval *argvars, typeval *rettv));
485 static void f_keys __ARGS((typeval *argvars, typeval *rettv));
442 static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv)); 486 static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
443 static void f_len __ARGS((typeval *argvars, typeval *rettv)); 487 static void f_len __ARGS((typeval *argvars, typeval *rettv));
444 static void f_libcall __ARGS((typeval *argvars, typeval *rettv)); 488 static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
445 static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv)); 489 static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
446 static void f_line __ARGS((typeval *argvars, typeval *rettv)); 490 static void f_line __ARGS((typeval *argvars, typeval *rettv));
457 static void f_min __ARGS((typeval *argvars, typeval *rettv)); 501 static void f_min __ARGS((typeval *argvars, typeval *rettv));
458 static void f_mode __ARGS((typeval *argvars, typeval *rettv)); 502 static void f_mode __ARGS((typeval *argvars, typeval *rettv));
459 static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv)); 503 static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
460 static void f_nr2char __ARGS((typeval *argvars, typeval *rettv)); 504 static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
461 static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv)); 505 static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
506 static void f_range __ARGS((typeval *argvars, typeval *rettv));
462 static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv)); 507 static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
463 static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv)); 508 static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
464 static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv)); 509 static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
465 static void f_remote_read __ARGS((typeval *argvars, typeval *rettv)); 510 static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
466 static void f_remote_send __ARGS((typeval *argvars, typeval *rettv)); 511 static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
499 static void f_tempname __ARGS((typeval *argvars, typeval *rettv)); 544 static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
500 static void f_tolower __ARGS((typeval *argvars, typeval *rettv)); 545 static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
501 static void f_toupper __ARGS((typeval *argvars, typeval *rettv)); 546 static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
502 static void f_tr __ARGS((typeval *argvars, typeval *rettv)); 547 static void f_tr __ARGS((typeval *argvars, typeval *rettv));
503 static void f_type __ARGS((typeval *argvars, typeval *rettv)); 548 static void f_type __ARGS((typeval *argvars, typeval *rettv));
549 static void f_values __ARGS((typeval *argvars, typeval *rettv));
504 static void f_virtcol __ARGS((typeval *argvars, typeval *rettv)); 550 static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
505 static void f_visualmode __ARGS((typeval *argvars, typeval *rettv)); 551 static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
506 static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv)); 552 static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
507 static void f_wincol __ARGS((typeval *argvars, typeval *rettv)); 553 static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
508 static void f_winheight __ARGS((typeval *argvars, typeval *rettv)); 554 static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
559 static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon)); 605 static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
560 static char_u *skip_var_one __ARGS((char_u *arg)); 606 static char_u *skip_var_one __ARGS((char_u *arg));
561 static void list_all_vars __ARGS((void)); 607 static void list_all_vars __ARGS((void));
562 static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg)); 608 static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
563 static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars)); 609 static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars));
610 static int check_changedtick __ARGS((char_u *arg));
564 static char_u *set_var_idx __ARGS((char_u *name, char_u *ip, typeval *rettv, int copy, char_u *endchars)); 611 static char_u *set_var_idx __ARGS((char_u *name, char_u *ip, typeval *rettv, int copy, char_u *endchars));
565 static void list_add_watch __ARGS((listvar *l, listwatch *lw)); 612 static void list_add_watch __ARGS((listvar *l, listwatch *lw));
613 static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem));
614 static void list_fix_watch __ARGS((listvar *l, listitem *item));
615 static int do_unlet_var __ARGS((char_u *name, int forceit));
566 616
567 /* 617 /*
568 * Set an internal variable to a string value. Creates the variable if it does 618 * Set an internal variable to a string value. Creates the variable if it does
569 * not already exist. 619 * not already exist.
570 */ 620 */
1527 * expression evaluation has been cancelled due to an 1577 * expression evaluation has been cancelled due to an
1528 * aborting error, an interrupt, or an exception. */ 1578 * aborting error, an interrupt, or an exception. */
1529 if (!aborting()) 1579 if (!aborting())
1530 EMSG2(_(e_invarg2), arg); 1580 EMSG2(_(e_invarg2), arg);
1531 } 1581 }
1532 else if (*p == '[') 1582 else if (*p == '[' || *p == '.')
1533 arg_end = set_var_idx(arg, p, tv, copy, endchars); 1583 arg_end = set_var_idx(arg, p, tv, copy, endchars);
1534 else if (endchars != NULL 1584 else if (endchars != NULL
1535 && vim_strchr(endchars, *skipwhite(p)) == NULL) 1585 && vim_strchr(endchars, *skipwhite(p)) == NULL)
1536 EMSG(_(e_letunexp)); 1586 EMSG(_(e_letunexp));
1537 else if (STRNCMP(arg, "b:changedtick", 13) == 0 1587 else if (!check_changedtick(arg))
1538 && !eval_isnamec(arg[13]))
1539 EMSG2(_(e_readonlyvar), arg);
1540 else
1541 { 1588 {
1542 c1 = *p; 1589 c1 = *p;
1543 *p = NUL; 1590 *p = NUL;
1544 set_var(arg, tv, copy); 1591 set_var(arg, tv, copy);
1545 *p = c1; 1592 *p = c1;
1554 1601
1555 return arg_end; 1602 return arg_end;
1556 } 1603 }
1557 1604
1558 /* 1605 /*
1606 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1607 */
1608 static int
1609 check_changedtick(arg)
1610 char_u *arg;
1611 {
1612 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1613 {
1614 EMSG2(_(e_readonlyvar), arg);
1615 return TRUE;
1616 }
1617 return FALSE;
1618 }
1619
1620 /*
1559 * Set a variable with an index: "name[expr]", "name[expr:expr]", 1621 * Set a variable with an index: "name[expr]", "name[expr:expr]",
1560 * "name[expr][expr]", etc. Only works if "name" is an existing List. 1622 * "name[expr][expr]", "name.key", "name.key[expr]" etc.
1623 * Only works if "name" is an existing List or Dictionary.
1561 * "ip" points to the first '['. 1624 * "ip" points to the first '['.
1562 * Returns a pointer to just after the last used ']'; NULL for error. 1625 * Returns a pointer to just after the last used ']'; NULL for error.
1563 */ 1626 */
1564 static char_u * 1627 static char_u *
1565 set_var_idx(name, ip, rettv, copy, endchars) 1628 set_var_idx(name, ip, rettv, copy, endchars)
1575 typeval var1; 1638 typeval var1;
1576 typeval var2; 1639 typeval var2;
1577 int range = FALSE; 1640 int range = FALSE;
1578 typeval *tv; 1641 typeval *tv;
1579 long n1 = 0, n2 = 0; 1642 long n1 = 0, n2 = 0;
1580 int empty1, empty2 = FALSE; 1643 int empty1 = FALSE, empty2 = FALSE;
1581 listitem *item = NULL; 1644 listitem *li = NULL;
1582 listitem *ni; 1645 listitem *ni;
1583 listitem *ri; 1646 listitem *ri;
1584 listvar *l = NULL; 1647 listvar *l = NULL;
1648 dictitem *di;
1649 char_u *key = NULL;
1650 char_u *newkey = NULL;
1651 int len;
1585 1652
1586 c1 = *ip; 1653 c1 = *ip;
1587 *ip = NUL; 1654 *ip = NUL;
1588 v = find_var(name, TRUE); 1655 v = find_var(name, TRUE);
1589 if (v == NULL) 1656 if (v == NULL)
1591 *ip = c1; 1658 *ip = c1;
1592 if (v == NULL) 1659 if (v == NULL)
1593 return NULL; 1660 return NULL;
1594 1661
1595 tv = &v->tv; 1662 tv = &v->tv;
1596 for (p = ip; *p == '['; p = skipwhite(p + 1)) 1663 for (p = ip; *p == '[' || (*p == '.' && tv->v_type == VAR_DICT); )
1597 { 1664 {
1598 if (tv->v_type != VAR_LIST || tv->vval.v_list == NULL) 1665 if (!(tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
1599 { 1666 && !(tv->v_type == VAR_DICT && tv->vval.v_dict != NULL))
1600 EMSG(_("E689: Can only index a List")); 1667 {
1668 EMSG(_("E689: Can only index a List or Dictionary"));
1601 p = NULL; 1669 p = NULL;
1602 break; 1670 break;
1603 } 1671 }
1604 if (range) 1672 if (range)
1605 { 1673 {
1606 EMSG(_("E708: [:] must come last")); 1674 EMSG(_("E708: [:] must come last"));
1607 p = NULL; 1675 p = NULL;
1608 break; 1676 break;
1609 } 1677 }
1610 1678
1611 /* Get the index [expr] or the first index [expr: ]. */ 1679 len = -1;
1612 p = skipwhite(p + 1); 1680 if (*p == '.')
1613 if (*p == ':') 1681 {
1614 empty1 = TRUE; 1682 key = p + 1;
1615 else 1683 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1616 { 1684 ;
1617 empty1 = FALSE; 1685 if (len == 0)
1618 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
1619 { 1686 {
1687 EMSG(_(e_emptykey));
1620 p = NULL; 1688 p = NULL;
1621 break; 1689 break;
1622 } 1690 }
1623 } 1691 p = key + len;
1624 1692 }
1625 /* Optionally get the second index [ :expr]. */ 1693 else
1626 if (*p == ':') 1694 {
1627 { 1695 /* Get the index [expr] or the first index [expr: ]. */
1628 if (rettv->v_type != VAR_LIST || rettv->vval.v_list == NULL)
1629 {
1630 EMSG(_("E709: [:] requires a List value"));
1631 p = NULL;
1632 if (!empty1)
1633 clear_tv(&var1);
1634 break;
1635 }
1636 p = skipwhite(p + 1); 1696 p = skipwhite(p + 1);
1637 if (*p == ']') 1697 if (*p == ':')
1638 empty2 = TRUE; 1698 empty1 = TRUE;
1639 else 1699 else
1640 { 1700 {
1641 empty2 = FALSE; 1701 empty1 = FALSE;
1642 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */ 1702 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
1643 { 1703 {
1704 p = NULL;
1705 break;
1706 }
1707 }
1708
1709 /* Optionally get the second index [ :expr]. */
1710 if (*p == ':')
1711 {
1712 if (tv->v_type == VAR_DICT)
1713 {
1714 EMSG(_("E999: Cannot use [:] with a Dictionary"));
1644 p = NULL; 1715 p = NULL;
1645 if (!empty1) 1716 if (!empty1)
1646 clear_tv(&var1); 1717 clear_tv(&var1);
1647 break; 1718 break;
1648 } 1719 }
1720 if (rettv->v_type != VAR_LIST || rettv->vval.v_list == NULL)
1721 {
1722 EMSG(_("E709: [:] requires a List value"));
1723 p = NULL;
1724 if (!empty1)
1725 clear_tv(&var1);
1726 break;
1727 }
1728 p = skipwhite(p + 1);
1729 if (*p == ']')
1730 empty2 = TRUE;
1731 else
1732 {
1733 empty2 = FALSE;
1734 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1735 {
1736 p = NULL;
1737 if (!empty1)
1738 clear_tv(&var1);
1739 break;
1740 }
1741 }
1742 range = TRUE;
1649 } 1743 }
1650 range = TRUE; 1744 else
1745 range = FALSE;
1746
1747 if (*p != ']')
1748 {
1749 EMSG(_(e_missbrac));
1750 if (!empty1)
1751 clear_tv(&var1);
1752 if (range && !empty2)
1753 clear_tv(&var2);
1754 p = NULL;
1755 break;
1756 }
1757
1758 /* Skip to past ']'. */
1759 ++p;
1760 }
1761
1762 if (tv->v_type == VAR_DICT)
1763 {
1764 if (len == -1)
1765 {
1766 key = get_tv_string(&var1);
1767 if (*key == NUL)
1768 {
1769 EMSG(_(e_emptykey));
1770 clear_tv(&var1);
1771 p = NULL;
1772 break;
1773 }
1774 }
1775 di = dict_find(tv->vval.v_dict, key, len);
1776 if (di == NULL)
1777 {
1778 /* Key does not exist in dict: may need toadd it. */
1779 if (*p == '[' || *p == '.')
1780 {
1781 EMSG2(_("E999: Key does not exist in Dictionary: %s"), key);
1782 p = NULL;
1783 if (len == -1)
1784 clear_tv(&var1);
1785 break;
1786 }
1787 if (len == -1)
1788 newkey = vim_strsave(key);
1789 else
1790 newkey = vim_strnsave(key, len);
1791 if (len == -1)
1792 clear_tv(&var1);
1793 if (newkey == NULL)
1794 p = NULL;
1795 break;
1796 }
1797 if (len == -1)
1798 clear_tv(&var1);
1799 tv = &di->di_tv;
1651 } 1800 }
1652 else 1801 else
1653 range = FALSE; 1802 {
1654 1803 /*
1655 if (*p != ']') 1804 * Get the number and item for the only or first index of the List.
1656 { 1805 */
1657 EMSG(_(e_missbrac)); 1806 if (empty1)
1658 if (!empty1) 1807 n1 = 0;
1808 else
1809 {
1810 n1 = get_tv_number(&var1);
1659 clear_tv(&var1); 1811 clear_tv(&var1);
1812 }
1813 l = tv->vval.v_list;
1814 li = list_find(l, n1);
1815 if (li == NULL)
1816 {
1817 EMSGN(_(e_listidx), n1);
1818 p = NULL;
1819 if (range && !empty2)
1820 clear_tv(&var2);
1821 break;
1822 }
1823
1824 /*
1825 * May need to find the item or absolute index for the second
1826 * index of a range.
1827 * When no index given: "empty2" is TRUE.
1828 * Otherwise "n2" is set to the second index.
1829 */
1660 if (range && !empty2) 1830 if (range && !empty2)
1831 {
1832 n2 = get_tv_number(&var2);
1661 clear_tv(&var2); 1833 clear_tv(&var2);
1662 p = NULL; 1834 if (n2 < 0)
1663 break; 1835 {
1664 } 1836 ni = list_find(l, n2);
1665 1837 if (ni == NULL)
1666 /* 1838 {
1667 * Get the number and item for the only or first index. 1839 EMSGN(_(e_listidx), n2);
1668 */ 1840 p = NULL;
1669 if (empty1) 1841 break;
1670 n1 = 0; 1842 }
1671 else 1843 n2 = list_idx_of_item(l, ni);
1672 { 1844 }
1673 n1 = get_tv_number(&var1); 1845
1674 clear_tv(&var1); 1846 /* Check that n2 isn't before n1. */
1675 } 1847 if (n1 < 0)
1676 l = tv->vval.v_list; 1848 n1 = list_idx_of_item(l, li);
1677 item = list_find(l, n1); 1849 if (n2 < n1)
1678 if (item == NULL)
1679 {
1680 EMSGN(_(e_listidx), n1);
1681 p = NULL;
1682 if (range && !empty2)
1683 clear_tv(&var2);
1684 break;
1685 }
1686
1687 /*
1688 * May need to find the item or absolute index for the second index of
1689 * a range.
1690 * When no index given: "empty2" is TRUE.
1691 * Otherwise "n2" is set to the second index.
1692 */
1693 if (range && !empty2)
1694 {
1695 n2 = get_tv_number(&var2);
1696 clear_tv(&var2);
1697 if (n2 < 0)
1698 {
1699 ni = list_find(l, n2);
1700 if (ni == NULL)
1701 { 1850 {
1702 EMSGN(_(e_listidx), n2); 1851 EMSGN(_(e_listidx), n2);
1703 p = NULL; 1852 p = NULL;
1704 break; 1853 break;
1705 } 1854 }
1706 n2 = list_idx_of_item(l, ni);
1707 } 1855 }
1708 1856
1709 /* Check that n2 isn't before n1. */ 1857 tv = &li->li_tv;
1710 if (n1 < 0) 1858 }
1711 n1 = list_idx_of_item(l, item);
1712 if (n2 < n1)
1713 {
1714 EMSGN(_(e_listidx), n2);
1715 p = NULL;
1716 break;
1717 }
1718 }
1719
1720 tv = &item->li_tv;
1721 } 1859 }
1722 1860
1723 if (p != NULL) 1861 if (p != NULL)
1724 { 1862 {
1863 p = skipwhite(p);
1725 if (endchars != NULL && vim_strchr(endchars, *p) == NULL) 1864 if (endchars != NULL && vim_strchr(endchars, *p) == NULL)
1726 { 1865 {
1727 EMSG(_(e_letunexp)); 1866 EMSG(_(e_letunexp));
1728 p = NULL; 1867 p = NULL;
1729 } 1868 }
1732 /* 1871 /*
1733 * Assign the List values to the list items. 1872 * Assign the List values to the list items.
1734 */ 1873 */
1735 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) 1874 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
1736 { 1875 {
1737 clear_tv(&item->li_tv); 1876 clear_tv(&li->li_tv);
1738 copy_tv(&ri->li_tv, &item->li_tv); 1877 copy_tv(&ri->li_tv, &li->li_tv);
1739 ri = ri->li_next; 1878 ri = ri->li_next;
1740 if (ri == NULL || (!empty2 && n2 == n1)) 1879 if (ri == NULL || (!empty2 && n2 == n1))
1741 break; 1880 break;
1742 if (item->li_next == NULL) 1881 if (li->li_next == NULL)
1743 { 1882 {
1744 /* Need to add an empty item. */ 1883 /* Need to add an empty item. */
1745 ni = listitem_alloc(); 1884 ni = listitem_alloc();
1746 if (ni == NULL) 1885 if (ni == NULL)
1747 { 1886 {
1750 } 1889 }
1751 ni->li_tv.v_type = VAR_NUMBER; 1890 ni->li_tv.v_type = VAR_NUMBER;
1752 ni->li_tv.vval.v_number = 0; 1891 ni->li_tv.vval.v_number = 0;
1753 list_append(l, ni); 1892 list_append(l, ni);
1754 } 1893 }
1755 item = item->li_next; 1894 li = li->li_next;
1756 ++n1; 1895 ++n1;
1757 } 1896 }
1758 if (ri != NULL) 1897 if (ri != NULL)
1759 EMSG(_("E710: List value has more items than target")); 1898 EMSG(_("E710: List value has more items than target"));
1760 else if (empty2 ? item != NULL && item->li_next != NULL : n1 != n2) 1899 else if (empty2 ? li != NULL && li->li_next != NULL : n1 != n2)
1761 EMSG(_("E711: List value has not enough items")); 1900 EMSG(_("E711: List value has not enough items"));
1762 } 1901 }
1763 else 1902 else
1764 { 1903 {
1904 if (newkey != NULL)
1905 {
1906 /* Need to add the item to the dictionary. */
1907 di = dictitem_alloc();
1908 if (di == NULL)
1909 p = NULL;
1910 else
1911 {
1912 di->di_key = newkey;
1913 newkey = NULL;
1914 dict_add(tv->vval.v_dict, di);
1915 tv = &di->di_tv;
1916 }
1917 }
1918 else
1919 clear_tv(tv);
1920
1765 /* 1921 /*
1766 * Assign the value to the variable or list item. 1922 * Assign the value to the variable or list item.
1767 */ 1923 */
1768 clear_tv(tv); 1924 if (p != NULL)
1769 if (copy)
1770 copy_tv(rettv, tv);
1771 else
1772 { 1925 {
1773 *tv = *rettv; 1926 if (copy)
1774 init_tv(rettv); 1927 copy_tv(rettv, tv);
1928 else
1929 {
1930 *tv = *rettv;
1931 init_tv(rettv);
1932 }
1775 } 1933 }
1776 } 1934 }
1777 } 1935 }
1936 vim_free(newkey);
1778 return p; 1937 return p;
1779 } 1938 }
1780 1939
1781 /* 1940 /*
1782 * Add a watcher to a list. 1941 * Add a watcher to a list.
2002 ++xp->xp_pattern; 2161 ++xp->xp_pattern;
2003 xp->xp_context = EXPAND_NOTHING; 2162 xp->xp_context = EXPAND_NOTHING;
2004 } 2163 }
2005 else if (c == '\'') /* literal string */ 2164 else if (c == '\'') /* literal string */
2006 { 2165 {
2166 /* Trick: '' is like stopping and starting a literal string. */
2007 while ((c = *++xp->xp_pattern) != NUL && c != '\'') 2167 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2008 /* skip */ ;
2009 xp->xp_context = EXPAND_NOTHING;
2010 }
2011 else if (c == '#') /* sharp string */
2012 {
2013 /* Trick: ## is like stopping and starting a sharp string. */
2014 while ((c = *++xp->xp_pattern) != NUL && c != '#')
2015 /* skip */ ; 2168 /* skip */ ;
2016 xp->xp_context = EXPAND_NOTHING; 2169 xp->xp_context = EXPAND_NOTHING;
2017 } 2170 }
2018 else if (c == '|') 2171 else if (c == '|')
2019 { 2172 {
2147 int error = FALSE; 2300 int error = FALSE;
2148 2301
2149 do 2302 do
2150 { 2303 {
2151 /* Find the end of the name. */ 2304 /* Find the end of the name. */
2152 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE); 2305 name_end = find_name_end(arg, &expr_start, &expr_end, TRUE);
2153 2306
2154 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end)) 2307 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
2155 { 2308 {
2156 emsg_severe = TRUE; 2309 emsg_severe = TRUE;
2157 EMSG(_(e_trailing)); 2310 EMSG(_(e_trailing));
2181 } 2334 }
2182 error = TRUE; 2335 error = TRUE;
2183 } 2336 }
2184 else 2337 else
2185 { 2338 {
2186 if (do_unlet(temp_string) == FAIL && !eap->forceit) 2339 if (do_unlet_var(temp_string, eap->forceit) == FAIL)
2187 {
2188 EMSG2(_("E108: No such variable: \"%s\""), temp_string);
2189 error = TRUE; 2340 error = TRUE;
2190 }
2191 vim_free(temp_string); 2341 vim_free(temp_string);
2192 } 2342 }
2193 } 2343 }
2194 else 2344 else
2195 { 2345 {
2196 cc = *name_end; 2346 cc = *name_end;
2197 *name_end = NUL; 2347 *name_end = NUL;
2198 2348 if (do_unlet_var(arg, eap->forceit) == FAIL)
2199 if (do_unlet(arg) == FAIL && !eap->forceit)
2200 {
2201 EMSG2(_("E108: No such variable: \"%s\""), arg);
2202 error = TRUE; 2349 error = TRUE;
2203 }
2204
2205 *name_end = cc; 2350 *name_end = cc;
2206 } 2351 }
2207 } 2352 }
2208 arg = skipwhite(name_end); 2353 arg = skipwhite(name_end);
2209 } while (!ends_excmd(*arg)); 2354 } while (!ends_excmd(*arg));
2210 2355
2211 eap->nextcmd = check_nextcmd(arg); 2356 eap->nextcmd = check_nextcmd(arg);
2357 }
2358
2359 static int
2360 do_unlet_var(name, forceit)
2361 char_u *name;
2362 int forceit;
2363 {
2364 if (check_changedtick(name))
2365 return FAIL;
2366 if (do_unlet(name) == FAIL && !forceit)
2367 {
2368 EMSG2(_("E108: No such variable: \"%s\""), name);
2369 return FAIL;
2370 }
2371 return OK;
2212 } 2372 }
2213 2373
2214 /* 2374 /*
2215 * "unlet" a variable. Return OK if it existed, FAIL if not. 2375 * "unlet" a variable. Return OK if it existed, FAIL if not.
2216 */ 2376 */
3059 * 3219 *
3060 * Also handle: 3220 * Also handle:
3061 * ! in front logical NOT 3221 * ! in front logical NOT
3062 * - in front unary minus 3222 * - in front unary minus
3063 * + in front unary plus (ignored) 3223 * + in front unary plus (ignored)
3064 * trailing [] subscript in String 3224 * trailing [] subscript in String or List
3225 * trailing .name entry in Dictionary
3065 * 3226 *
3066 * "arg" must point to the first non-white of the expression. 3227 * "arg" must point to the first non-white of the expression.
3067 * "arg" is advanced to the next non-white after the recognized expression. 3228 * "arg" is advanced to the next non-white after the recognized expression.
3068 * 3229 *
3069 * Return OK or FAIL. 3230 * Return OK or FAIL.
3125 */ 3286 */
3126 case '"': ret = get_string_tv(arg, rettv, evaluate); 3287 case '"': ret = get_string_tv(arg, rettv, evaluate);
3127 break; 3288 break;
3128 3289
3129 /* 3290 /*
3130 * Literal string constant: 'string'. 3291 * Literal string constant: 'str''ing'.
3131 */ 3292 */
3132 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate); 3293 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
3133 break;
3134
3135 /*
3136 * Sharp string constant: #str##ing#.
3137 */
3138 case '#': ret = get_sharp_string_tv(arg, rettv, evaluate);
3139 break; 3294 break;
3140 3295
3141 /* 3296 /*
3142 * List: [expr, expr] 3297 * List: [expr, expr]
3143 */ 3298 */
3144 case '[': ret = get_list_tv(arg, rettv, evaluate); 3299 case '[': ret = get_list_tv(arg, rettv, evaluate);
3300 break;
3301
3302 /*
3303 * Dictionary: {key: val, key: val}
3304 */
3305 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3145 break; 3306 break;
3146 3307
3147 /* 3308 /*
3148 * Option value: &name or map() item "&". 3309 * Option value: &name or map() item "&".
3149 */ 3310 */
3189 clear_tv(rettv); 3350 clear_tv(rettv);
3190 ret = FAIL; 3351 ret = FAIL;
3191 } 3352 }
3192 break; 3353 break;
3193 3354
3355 default: ret = NOTDONE;
3356 break;
3357 }
3358
3359 if (ret == NOTDONE)
3360 {
3361 /*
3362 * Must be a variable or function name.
3363 * Can also be a curly-braces kind of name: {expr}.
3364 */
3365 s = *arg;
3366 len = get_func_len(arg, &alias, evaluate);
3367 if (alias != NULL)
3368 s = alias;
3369
3370 if (len == 0)
3371 ret = FAIL;
3372 else
3373 {
3374 if (**arg == '(') /* recursive! */
3375 {
3376 /* If "s" is the name of a variable of type VAR_FUNC
3377 * use its contents. */
3378 s = deref_func_name(s, &len);
3379
3380 /* Invoke the function. */
3381 ret = get_func_tv(s, len, rettv, arg,
3382 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3383 &len, evaluate);
3384 /* Stop the expression evaluation when immediately
3385 * aborting on error, or when an interrupt occurred or
3386 * an exception was thrown but not caught. */
3387 if (aborting())
3388 {
3389 if (ret == OK)
3390 clear_tv(rettv);
3391 ret = FAIL;
3392 }
3393 }
3394 else if (evaluate)
3395 ret = get_var_tv(s, len, rettv);
3396 }
3397
3398 if (alias != NULL)
3399 vim_free(alias);
3400 }
3401
3402 *arg = skipwhite(*arg);
3403
3194 /* 3404 /*
3195 * Must be a variable or function name then. 3405 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
3196 */ 3406 */
3197 default: s = *arg; 3407 while ((**arg == '[' || (**arg == '.' && rettv->v_type == VAR_DICT))
3198 len = get_func_len(arg, &alias, evaluate); 3408 && !vim_iswhite(*(*arg - 1)) && ret == OK)
3199 if (alias != NULL)
3200 s = alias;
3201
3202 if (len == 0)
3203 ret = FAIL;
3204 else
3205 {
3206 if (**arg == '(') /* recursive! */
3207 {
3208 /* If "s" is the name of a variable of type VAR_FUNC
3209 * use its contents. */
3210 s = deref_func_name(s, &len);
3211
3212 /* Invoke the function. */
3213 ret = get_func_tv(s, len, rettv, arg,
3214 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3215 &len, evaluate);
3216 /* Stop the expression evaluation when immediately
3217 * aborting on error, or when an interrupt occurred or
3218 * an exception was thrown but not caught. */
3219 if (aborting())
3220 {
3221 if (ret == OK)
3222 clear_tv(rettv);
3223 ret = FAIL;
3224 }
3225 }
3226 else if (evaluate)
3227 ret = get_var_tv(s, len, rettv);
3228 }
3229
3230 if (alias != NULL)
3231 vim_free(alias);
3232
3233 break;
3234 }
3235 *arg = skipwhite(*arg);
3236
3237 /*
3238 * Handle expr[expr] and expr[expr:expr] subscript.
3239 */
3240 while (**arg == '[' && ret == OK)
3241 { 3409 {
3242 if (eval_index(arg, rettv, evaluate) == FAIL) 3410 if (eval_index(arg, rettv, evaluate) == FAIL)
3243 { 3411 {
3244 clear_tv(rettv); 3412 clear_tv(rettv);
3245 return FAIL; 3413 return FAIL;
3280 int evaluate; 3448 int evaluate;
3281 { 3449 {
3282 int empty1 = FALSE, empty2 = FALSE; 3450 int empty1 = FALSE, empty2 = FALSE;
3283 typeval var1, var2; 3451 typeval var1, var2;
3284 long n1, n2 = 0; 3452 long n1, n2 = 0;
3285 long len; 3453 long len = -1;
3286 int range; 3454 int range = FALSE;
3287 char_u *s; 3455 char_u *s;
3456 char_u *key = NULL;
3288 3457
3289 if (rettv->v_type == VAR_FUNC) 3458 if (rettv->v_type == VAR_FUNC)
3290 { 3459 {
3291 EMSG(_("E695: Cannot index a Funcref")); 3460 EMSG(_("E695: Cannot index a Funcref"));
3292 return FAIL; 3461 return FAIL;
3293 } 3462 }
3294 3463
3295 /* 3464 if (**arg == '.')
3296 * Get the (first) variable from inside the []. 3465 {
3297 */ 3466 /*
3298 *arg = skipwhite(*arg + 1); 3467 * dict.name
3299 if (**arg == ':') 3468 */
3300 empty1 = TRUE; 3469 key = *arg + 1;
3301 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */ 3470 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3302 return FAIL; 3471 ;
3303 3472 if (len == 0)
3304 /* 3473 return FAIL;
3305 * Get the second variable from inside the [:]. 3474 *arg = skipwhite(key + len);
3306 */ 3475 }
3307 if (**arg == ':') 3476 else
3308 { 3477 {
3309 range = TRUE; 3478 /*
3479 * something[idx]
3480 *
3481 * Get the (first) variable from inside the [].
3482 */
3310 *arg = skipwhite(*arg + 1); 3483 *arg = skipwhite(*arg + 1);
3311 if (**arg == ']') 3484 if (**arg == ':')
3312 empty2 = TRUE; 3485 empty1 = TRUE;
3313 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */ 3486 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3314 { 3487 return FAIL;
3488
3489 /*
3490 * Get the second variable from inside the [:].
3491 */
3492 if (**arg == ':')
3493 {
3494 range = TRUE;
3495 *arg = skipwhite(*arg + 1);
3496 if (**arg == ']')
3497 empty2 = TRUE;
3498 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3499 {
3500 clear_tv(&var1);
3501 return FAIL;
3502 }
3503 }
3504
3505 /* Check for the ']'. */
3506 if (**arg != ']')
3507 {
3508 EMSG(_(e_missbrac));
3315 clear_tv(&var1); 3509 clear_tv(&var1);
3510 if (range)
3511 clear_tv(&var2);
3316 return FAIL; 3512 return FAIL;
3317 } 3513 }
3318 } 3514 *arg = skipwhite(*arg + 1); /* skip the ']' */
3319 else
3320 range = FALSE;
3321
3322 /* Check for the ']'. */
3323 if (**arg != ']')
3324 {
3325 EMSG(_(e_missbrac));
3326 clear_tv(&var1);
3327 if (range)
3328 clear_tv(&var2);
3329 return FAIL;
3330 } 3515 }
3331 3516
3332 if (evaluate) 3517 if (evaluate)
3333 { 3518 {
3334 if (empty1) 3519 n1 = 0;
3335 n1 = 0; 3520 if (!empty1 && rettv->v_type != VAR_DICT)
3336 else
3337 { 3521 {
3338 n1 = get_tv_number(&var1); 3522 n1 = get_tv_number(&var1);
3339 clear_tv(&var1); 3523 clear_tv(&var1);
3340 } 3524 }
3341 if (range) 3525 if (range)
3403 listvar *l; 3587 listvar *l;
3404 listitem *item; 3588 listitem *item;
3405 3589
3406 if (n2 < 0) 3590 if (n2 < 0)
3407 n2 = len + n2; 3591 n2 = len + n2;
3408 if (!empty2 && (n2 < 0 || n2 >= len || n2 < n1)) 3592 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
3409 { 3593 {
3410 EMSGN(_(e_listidx), n2); 3594 EMSGN(_(e_listidx), n2);
3411 return FAIL; 3595 return FAIL;
3412 } 3596 }
3413 l = list_alloc(); 3597 l = list_alloc();
3434 &var1); 3618 &var1);
3435 clear_tv(rettv); 3619 clear_tv(rettv);
3436 *rettv = var1; 3620 *rettv = var1;
3437 } 3621 }
3438 break; 3622 break;
3439 } 3623
3440 } 3624 case VAR_DICT:
3441 3625 if (range)
3442 *arg = skipwhite(*arg + 1); /* skip the ']' */ 3626 {
3627 EMSG(_("E999: Using range with Dictionary"));
3628 if (len == -1)
3629 clear_tv(&var1);
3630 return FAIL;
3631 }
3632 {
3633 dictitem *item;
3634
3635 if (len == -1)
3636 {
3637 key = get_tv_string(&var1);
3638 if (*key == NUL)
3639 {
3640 EMSG(_("E999: Empty key for Dictionary"));
3641 clear_tv(&var1);
3642 return FAIL;
3643 }
3644 }
3645
3646 item = dict_find(rettv->vval.v_dict, key, (int)len);
3647
3648 if (item == NULL)
3649 EMSG2(_("E999: Key not found in Dictionary: %s"), key);
3650 if (len == -1)
3651 clear_tv(&var1);
3652 if (item == NULL)
3653 return FAIL;
3654
3655 copy_tv(&item->di_tv, &var1);
3656 clear_tv(rettv);
3657 *rettv = var1;
3658 }
3659 break;
3660 }
3661 }
3662
3443 return OK; 3663 return OK;
3444 } 3664 }
3445 3665
3446 /* 3666 /*
3447 * Get an option value. 3667 * Get an option value.
3534 typeval *rettv; 3754 typeval *rettv;
3535 int evaluate; 3755 int evaluate;
3536 { 3756 {
3537 char_u *p; 3757 char_u *p;
3538 char_u *name; 3758 char_u *name;
3539 int i;
3540 int extra = 0; 3759 int extra = 0;
3541 3760
3542 /* 3761 /*
3543 * Find the end of the string, skipping backslashed characters. 3762 * Find the end of the string, skipping backslashed characters.
3544 */ 3763 */
3572 * characters. 3791 * characters.
3573 */ 3792 */
3574 name = alloc((unsigned)(p - *arg + extra)); 3793 name = alloc((unsigned)(p - *arg + extra));
3575 if (name == NULL) 3794 if (name == NULL)
3576 return FAIL; 3795 return FAIL;
3577 3796 rettv->v_type = VAR_STRING;
3578 i = 0; 3797 rettv->vval.v_string = name;
3579 for (p = *arg + 1; *p != NUL && *p != '"'; ++p) 3798
3799 for (p = *arg + 1; *p != NUL && *p != '"'; )
3580 { 3800 {
3581 if (*p == '\\') 3801 if (*p == '\\')
3582 { 3802 {
3583 switch (*++p) 3803 switch (*++p)
3584 { 3804 {
3585 case 'b': name[i++] = BS; break; 3805 case 'b': *name++ = BS; ++p; break;
3586 case 'e': name[i++] = ESC; break; 3806 case 'e': *name++ = ESC; ++p; break;
3587 case 'f': name[i++] = FF; break; 3807 case 'f': *name++ = FF; ++p; break;
3588 case 'n': name[i++] = NL; break; 3808 case 'n': *name++ = NL; ++p; break;
3589 case 'r': name[i++] = CAR; break; 3809 case 'r': *name++ = CAR; ++p; break;
3590 case 't': name[i++] = TAB; break; 3810 case 't': *name++ = TAB; ++p; break;
3591 3811
3592 case 'X': /* hex: "\x1", "\x12" */ 3812 case 'X': /* hex: "\x1", "\x12" */
3593 case 'x': 3813 case 'x':
3594 case 'u': /* Unicode: "\u0023" */ 3814 case 'u': /* Unicode: "\u0023" */
3595 case 'U': 3815 case 'U':
3606 while (--n >= 0 && vim_isxdigit(p[1])) 3826 while (--n >= 0 && vim_isxdigit(p[1]))
3607 { 3827 {
3608 ++p; 3828 ++p;
3609 nr = (nr << 4) + hex2nr(*p); 3829 nr = (nr << 4) + hex2nr(*p);
3610 } 3830 }
3831 ++p;
3611 #ifdef FEAT_MBYTE 3832 #ifdef FEAT_MBYTE
3612 /* For "\u" store the number according to 3833 /* For "\u" store the number according to
3613 * 'encoding'. */ 3834 * 'encoding'. */
3614 if (c != 'X') 3835 if (c != 'X')
3615 i += (*mb_char2bytes)(nr, name + i); 3836 name += (*mb_char2bytes)(nr, name);
3616 else 3837 else
3617 #endif 3838 #endif
3618 name[i++] = nr; 3839 *name++ = nr;
3619 } 3840 }
3620 else
3621 name[i++] = *p;
3622 break; 3841 break;
3623 3842
3624 /* octal: "\1", "\12", "\123" */ 3843 /* octal: "\1", "\12", "\123" */
3625 case '0': 3844 case '0':
3626 case '1': 3845 case '1':
3627 case '2': 3846 case '2':
3628 case '3': 3847 case '3':
3629 case '4': 3848 case '4':
3630 case '5': 3849 case '5':
3631 case '6': 3850 case '6':
3632 case '7': name[i] = *p - '0'; 3851 case '7': *name = *p++ - '0';
3633 if (p[1] >= '0' && p[1] <= '7') 3852 if (*p >= '0' && *p <= '7')
3634 { 3853 {
3635 ++p; 3854 *name = (*name << 3) + *p++ - '0';
3636 name[i] = (name[i] << 3) + *p - '0'; 3855 if (*p >= '0' && *p <= '7')
3637 if (p[1] >= '0' && p[1] <= '7') 3856 *name = (*name << 3) + *p++ - '0';
3638 {
3639 ++p;
3640 name[i] = (name[i] << 3) + *p - '0';
3641 }
3642 } 3857 }
3643 ++i; 3858 ++name;
3644 break; 3859 break;
3645 3860
3646 /* Special key, e.g.: "\<C-W>" */ 3861 /* Special key, e.g.: "\<C-W>" */
3647 case '<': extra = trans_special(&p, name + i, TRUE); 3862 case '<': extra = trans_special(&p, name, TRUE);
3648 if (extra != 0) 3863 if (extra != 0)
3649 { 3864 {
3650 i += extra; 3865 name += extra;
3651 --p;
3652 break; 3866 break;
3653 } 3867 }
3654 /* FALLTHROUGH */ 3868 /* FALLTHROUGH */
3655 3869
3656 default: name[i++] = *p; 3870 default: MB_COPY_CHAR(p, name);
3657 break; 3871 break;
3658 } 3872 }
3659 } 3873 }
3660 else 3874 else
3661 name[i++] = *p; 3875 MB_COPY_CHAR(p, name);
3662 3876
3663 #ifdef FEAT_MBYTE 3877 }
3664 /* For a multi-byte character copy the bytes after the first one. */ 3878 *name = NUL;
3665 if (has_mbyte)
3666 {
3667 int l = (*mb_ptr2len_check)(p);
3668
3669 while (--l > 0)
3670 name[i++] = *++p;
3671 }
3672 #endif
3673 }
3674 name[i] = NUL;
3675 *arg = p + 1; 3879 *arg = p + 1;
3676 3880
3677 rettv->v_type = VAR_STRING;
3678 rettv->vval.v_string = name;
3679
3680 return OK; 3881 return OK;
3681 } 3882 }
3682 3883
3683 /* 3884 /*
3684 * Allocate a variable for an backtick-string constant. 3885 * Allocate a variable for a 'str''ing' constant.
3685 * Return OK or FAIL. 3886 * Return OK or FAIL.
3686 */ 3887 */
3687 static int 3888 static int
3688 get_lit_string_tv(arg, rettv, evaluate) 3889 get_lit_string_tv(arg, rettv, evaluate)
3689 char_u **arg; 3890 char_u **arg;
3690 typeval *rettv; 3891 typeval *rettv;
3691 int evaluate; 3892 int evaluate;
3692 { 3893 {
3693 char_u *p; 3894 char_u *p;
3694 char_u *name; 3895 char_u *str;
3896 int reduce = 0;
3695 3897
3696 /* 3898 /*
3697 * Find the end of the string. 3899 * Find the end of the string, skipping ''.
3698 */
3699 p = vim_strchr(*arg + 1, '\'');
3700 if (p == NULL)
3701 {
3702 EMSG2(_("E115: Missing quote: %s"), *arg);
3703 return FAIL;
3704 }
3705
3706 if (evaluate)
3707 {
3708 /*
3709 * Copy the string into allocated memory.
3710 */
3711 name = vim_strnsave(*arg + 1, (int)(p - (*arg + 1)));
3712 if (name == NULL)
3713 return FAIL;
3714
3715 rettv->v_type = VAR_STRING;
3716 rettv->vval.v_string = name;
3717 }
3718
3719 *arg = p + 1;
3720
3721 return OK;
3722 }
3723
3724 /*
3725 * Allocate a variable for a #string# constant.
3726 * Return OK or FAIL.
3727 */
3728 static int
3729 get_sharp_string_tv(arg, rettv, evaluate)
3730 char_u **arg;
3731 typeval *rettv;
3732 int evaluate;
3733 {
3734 char_u *p;
3735 char_u *str;
3736 int i;
3737 int reduce = 0;
3738
3739 /*
3740 * Find the end of the string, skipping ##.
3741 */ 3900 */
3742 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p)) 3901 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
3743 { 3902 {
3744 if (*p == '#') 3903 if (*p == '\'')
3745 { 3904 {
3746 if (p[1] != '#') 3905 if (p[1] != '\'')
3747 break; 3906 break;
3748 ++reduce; 3907 ++reduce;
3749 ++p; 3908 ++p;
3750 } 3909 }
3751 } 3910 }
3752 3911
3753 if (*p != '#') 3912 if (*p != '\'')
3754 { 3913 {
3755 EMSG2(_("E999: Missing #: %s"), *arg); 3914 EMSG2(_("E115: Missing quote: %s"), *arg);
3756 return FAIL; 3915 return FAIL;
3757 } 3916 }
3758 3917
3759 /* If only parsing, set *arg and return here */ 3918 /* If only parsing return after setting "*arg" */
3760 if (!evaluate) 3919 if (!evaluate)
3761 { 3920 {
3762 *arg = p + 1; 3921 *arg = p + 1;
3763 return OK; 3922 return OK;
3764 } 3923 }
3765 3924
3766 /* 3925 /*
3767 * Copy the string into allocated memory, handling ## to # reduction. 3926 * Copy the string into allocated memory, handling '' to ' reduction.
3768 */ 3927 */
3769 str = alloc((unsigned)((p - *arg) - reduce)); 3928 str = alloc((unsigned)((p - *arg) - reduce));
3770 if (str == NULL) 3929 if (str == NULL)
3771 return FAIL; 3930 return FAIL;
3772 3931 rettv->v_type = VAR_STRING;
3773 i = 0; 3932 rettv->vval.v_string = str;
3774 for (p = *arg + 1; *p != NUL; ++p) 3933
3775 { 3934 for (p = *arg + 1; *p != NUL; )
3776 if (*p == '#') 3935 {
3777 { 3936 if (*p == '\'')
3778 if (p[1] != '#') 3937 {
3938 if (p[1] != '\'')
3779 break; 3939 break;
3780 ++p; 3940 ++p;
3781 } 3941 }
3782 str[i++] = *p; 3942 MB_COPY_CHAR(p, str);
3783 3943 }
3784 #ifdef FEAT_MBYTE 3944 *str = NUL;
3785 /* For a multi-byte character copy the bytes after the first one. */
3786 if (has_mbyte)
3787 {
3788 int l = (*mb_ptr2len_check)(p);
3789
3790 while (--l > 0)
3791 str[i++] = *++p;
3792 }
3793 #endif
3794 }
3795 str[i] = NUL;
3796 *arg = p + 1; 3945 *arg = p + 1;
3797
3798 rettv->v_type = VAR_STRING;
3799 rettv->vval.v_string = str;
3800 3946
3801 return OK; 3947 return OK;
3802 } 3948 }
3803 3949
3804 /* 3950 /*
3839 3985
3840 if (**arg == ']') 3986 if (**arg == ']')
3841 break; 3987 break;
3842 if (**arg != ',') 3988 if (**arg != ',')
3843 { 3989 {
3844 EMSG2(_("E696: Missing comma in list: %s"), *arg); 3990 EMSG2(_("E696: Missing comma in List: %s"), *arg);
3845 goto failret; 3991 goto failret;
3846 } 3992 }
3847 *arg = skipwhite(*arg + 1); 3993 *arg = skipwhite(*arg + 1);
3848 } 3994 }
3849 3995
3850 if (**arg != ']') 3996 if (**arg != ']')
3851 { 3997 {
3852 EMSG2(_("E697: Missing end of list ']': %s"), *arg); 3998 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
3853 failret: 3999 failret:
3854 if (evaluate) 4000 if (evaluate)
3855 list_free(l); 4001 list_free(l);
3856 return FAIL; 4002 return FAIL;
3857 } 4003 }
4364 vim_free(tofree); 4510 vim_free(tofree);
4365 } 4511 }
4366 } 4512 }
4367 4513
4368 /* 4514 /*
4515 * Allocate an empty header for a dictionary.
4516 */
4517 static dictvar *
4518 dict_alloc()
4519 {
4520 return (dictvar *)alloc_clear(sizeof(dictvar));
4521 }
4522
4523 /*
4524 * Unreference a Dictionary: decrement the reference count and free it when it
4525 * becomes zero.
4526 */
4527 static void
4528 dict_unref(d)
4529 dictvar *d;
4530 {
4531 if (d != NULL && --d->dv_refcount <= 0)
4532 dict_free(d);
4533 }
4534
4535 /*
4536 * Free a Dictionary, including all items it contains.
4537 * Ignores the reference count.
4538 */
4539 static void
4540 dict_free(d)
4541 dictvar *d;
4542 {
4543 dictitem *item;
4544 dictitem *next;
4545
4546 for (item = d->dv_first; item != NULL; item = next)
4547 {
4548 next = item->di_next;
4549 dictitem_free(item);
4550 }
4551 vim_free(d);
4552 }
4553
4554 /*
4555 * Allocate a Dictionary item.
4556 */
4557 static dictitem *
4558 dictitem_alloc()
4559 {
4560 return (dictitem *)alloc(sizeof(dictitem));
4561 }
4562
4563 /*
4564 * Free a dict item. Also clears the value.
4565 */
4566 static void
4567 dictitem_free(item)
4568 dictitem *item;
4569 {
4570 vim_free(item->di_key);
4571 clear_tv(&item->di_tv);
4572 vim_free(item);
4573 }
4574
4575 /*
4576 * Add item "item" to Dictionary "d".
4577 */
4578 static void
4579 dict_add(d, item)
4580 dictvar *d;
4581 dictitem *item;
4582 {
4583 item->di_next = d->dv_first;
4584 d->dv_first = item;
4585 }
4586
4587 #if 0 /* not currently used */
4588 static void dict_set_item __ARGS((dictvar *d, int type, char *key, void *val));
4589
4590 /*
4591 * Add an item to Dictionary "d" with type "type", key "key" and value "val".
4592 * If it already exists it is overwritten.
4593 * The key and value are copied to allocated memory.
4594 */
4595 static void
4596 dict_set_item(d, type, key, val)
4597 dictvar *d;
4598 int type;
4599 char *key;
4600 void *val;
4601 {
4602 dictitem *di;
4603 char_u *dkey;
4604
4605 di = dict_find(d, (char_u *)key, -1);
4606 if (di == NULL)
4607 {
4608 dkey = vim_strsave((char_u *)key);
4609 if (dkey != NULL)
4610 {
4611 di = dictitem_alloc();
4612 if (di == NULL)
4613 vim_free(dkey);
4614 else
4615 di->di_key = dkey;
4616 }
4617 }
4618 else
4619 clear_tv(&di->di_tv);
4620
4621 if (di != NULL)
4622 {
4623 di->di_tv.v_type = type;
4624 switch (type)
4625 {
4626 case VAR_NUMBER:
4627 di->di_tv.vval.v_number = (varnumber_T)val;
4628 break;
4629 case VAR_FUNC:
4630 case VAR_STRING:
4631 di->di_tv.vval.v_string = vim_strsave((char_u *)val);
4632 break;
4633 default:
4634 EMSG2(_(e_intern2), "dict_set_item()");
4635 }
4636 dict_add(d, di);
4637 }
4638 }
4639 #endif
4640
4641 /*
4642 * Find item "key[len]" in Dictionary "d".
4643 * If "len" is negative use strlen(key).
4644 * Returns NULL when not found.
4645 */
4646 static dictitem *
4647 dict_find(d, key, len)
4648 dictvar *d;
4649 char_u *key;
4650 int len;
4651 {
4652 static dictitem *di;
4653
4654 for (di = d->dv_first; di != NULL; di = di->di_next)
4655 if (len < 0
4656 ? STRCMP(di->di_key, key) == 0
4657 : STRNCMP(di->di_key, key, len) == 0 && di->di_key[len] == NUL)
4658 return di;
4659 return NULL;
4660 }
4661
4662 /*
4663 * Return an allocated string with the string representation of a Dictionary.
4664 * May return NULL.
4665 */
4666 static char_u *
4667 dict2string(tv)
4668 typeval *tv;
4669 {
4670 garray_T ga;
4671 int first = TRUE;
4672 char_u *tofree;
4673 char_u numbuf[NUMBUFLEN];
4674 dictitem *item;
4675 char_u *s;
4676
4677 if (tv->vval.v_dict == NULL)
4678 return NULL;
4679 ga_init2(&ga, (int)sizeof(char), 80);
4680 ga_append(&ga, '{');
4681
4682 for (item = tv->vval.v_dict->dv_first; item != NULL; item = item->di_next)
4683 {
4684 if (first)
4685 first = FALSE;
4686 else
4687 ga_concat(&ga, (char_u *)", ");
4688
4689 tofree = string_quote(item->di_key, FALSE);
4690 if (tofree != NULL)
4691 {
4692 ga_concat(&ga, tofree);
4693 vim_free(tofree);
4694 }
4695 ga_concat(&ga, (char_u *)": ");
4696 s = tv2string(&item->di_tv, &tofree, numbuf);
4697 if (s != NULL)
4698 ga_concat(&ga, s);
4699 vim_free(tofree);
4700 }
4701
4702 ga_append(&ga, '}');
4703 ga_append(&ga, NUL);
4704 return (char_u *)ga.ga_data;
4705 }
4706
4707 /*
4708 * Allocate a variable for a Dictionary and fill it from "*arg".
4709 * Return OK or FAIL. Returns NOTDONE for {expr}.
4710 */
4711 static int
4712 get_dict_tv(arg, rettv, evaluate)
4713 char_u **arg;
4714 typeval *rettv;
4715 int evaluate;
4716 {
4717 dictvar *d = NULL;
4718 typeval tv;
4719 char_u *key;
4720 dictitem *item;
4721 char_u *start = skipwhite(*arg + 1);
4722
4723 /*
4724 * First check if it's not a curly-braces thing: {expr}.
4725 * Must do this without evaluating, otherwise a function may be called
4726 * twice. Unfortunately this means we need to call eval1() twice for the
4727 * first item.
4728 */
4729 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
4730 return FAIL;
4731 if (*start == '}')
4732 return NOTDONE;
4733
4734 if (evaluate)
4735 {
4736 d = dict_alloc();
4737 if (d == NULL)
4738 return FAIL;
4739 }
4740
4741 *arg = skipwhite(*arg + 1);
4742 while (**arg != '}' && **arg != NUL)
4743 {
4744 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4745 goto failret;
4746 if (**arg != ':')
4747 {
4748 EMSG2(_("E999: Missing colon in Dictionary: %s"), *arg);
4749 clear_tv(&tv);
4750 goto failret;
4751 }
4752 key = get_tv_string(&tv);
4753 if (*key == NUL)
4754 {
4755 EMSG(_(e_emptykey));
4756 clear_tv(&tv);
4757 goto failret;
4758 }
4759 key = vim_strsave(key);
4760 clear_tv(&tv);
4761 if (key == NULL)
4762 goto failret;
4763
4764 *arg = skipwhite(*arg + 1);
4765 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4766 {
4767 vim_free(key);
4768 goto failret;
4769 }
4770 if (evaluate)
4771 {
4772 item = dict_find(d, key, -1);
4773 if (item != NULL)
4774 {
4775 EMSG(_("E999: Duplicate key in Dictionary"));
4776 vim_free(key);
4777 clear_tv(&tv);
4778 goto failret;
4779 }
4780 item = dictitem_alloc();
4781 if (item == NULL)
4782 vim_free(key);
4783 else
4784 {
4785 item->di_key = key;
4786 item->di_tv = tv;
4787 dict_add(d, item);
4788 }
4789 }
4790
4791 if (**arg == '}')
4792 break;
4793 if (**arg != ',')
4794 {
4795 EMSG2(_("E999: Missing comma in Dictionary: %s"), *arg);
4796 goto failret;
4797 }
4798 *arg = skipwhite(*arg + 1);
4799 }
4800
4801 if (**arg != '}')
4802 {
4803 EMSG2(_("E999: Missing end of Dictionary '}': %s"), *arg);
4804 failret:
4805 if (evaluate)
4806 dict_free(d);
4807 return FAIL;
4808 }
4809
4810 *arg = skipwhite(*arg + 1);
4811 if (evaluate)
4812 {
4813 rettv->v_type = VAR_DICT;
4814 rettv->vval.v_dict = d;
4815 ++d->dv_refcount;
4816 }
4817
4818 return OK;
4819 }
4820
4821
4822 /*
4369 * Return a string with the string representation of a variable. 4823 * Return a string with the string representation of a variable.
4370 * If the memory is allocated "tofree" is set to it, otherwise NULL. 4824 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4371 * "numbuf" is used for a number. 4825 * "numbuf" is used for a number.
4372 * Does not put quotes around strings, as ":echo" displays values. 4826 * Does not put quotes around strings, as ":echo" displays values.
4373 * May return NULL; 4827 * May return NULL;
4383 case VAR_FUNC: 4837 case VAR_FUNC:
4384 *tofree = NULL; 4838 *tofree = NULL;
4385 return tv->vval.v_string; 4839 return tv->vval.v_string;
4386 case VAR_LIST: 4840 case VAR_LIST:
4387 *tofree = list2string(tv); 4841 *tofree = list2string(tv);
4842 return *tofree;
4843 case VAR_DICT:
4844 *tofree = dict2string(tv);
4388 return *tofree; 4845 return *tofree;
4389 case VAR_STRING: 4846 case VAR_STRING:
4390 case VAR_NUMBER: 4847 case VAR_NUMBER:
4391 break; 4848 break;
4392 default: 4849 default:
4420 *tofree = string_quote(tv->vval.v_string, FALSE); 4877 *tofree = string_quote(tv->vval.v_string, FALSE);
4421 return *tofree; 4878 return *tofree;
4422 case VAR_LIST: 4879 case VAR_LIST:
4423 *tofree = list2string(tv); 4880 *tofree = list2string(tv);
4424 return *tofree; 4881 return *tofree;
4882 case VAR_DICT:
4883 *tofree = dict2string(tv);
4884 return *tofree;
4425 default: 4885 default:
4426 EMSG2(_(e_intern2), "tv2string()"); 4886 EMSG2(_(e_intern2), "tv2string()");
4427 } 4887 }
4428 *tofree = NULL; 4888 *tofree = NULL;
4429 return get_tv_string_buf(tv, numbuf); 4889 return get_tv_string_buf(tv, numbuf);
4430 } 4890 }
4431 4891
4432 /* 4892 /*
4433 * Return a string in # quotes, doubling # characters. 4893 * Return a string in ' quotes, doubling ' characters.
4434 * If "function" is TRUE make it function(#string#). 4894 * If "function" is TRUE make it function('string').
4435 */ 4895 */
4436 static char_u * 4896 static char_u *
4437 string_quote(str, function) 4897 string_quote(str, function)
4438 char_u *str; 4898 char_u *str;
4439 int function; 4899 int function;
4440 { 4900 {
4441 unsigned len = function ? 13 : 3; 4901 unsigned len = function ? 13 : 3;
4442 char_u *p, *r, *s; 4902 char_u *p, *r, *s;
4443 4903
4444 for (p = str; *p != NUL; mb_ptr_adv(p)) 4904 for (p = str; *p != NUL; mb_ptr_adv(p))
4445 if (*p == '#') 4905 if (*p == '\'')
4446 ++len; 4906 ++len;
4447 s = r = alloc(len); 4907 s = r = alloc(len);
4448 if (r != NULL) 4908 if (r != NULL)
4449 { 4909 {
4450 if (function) 4910 if (function)
4451 { 4911 {
4452 STRCPY(r, "function(#"); 4912 STRCPY(r, "function('");
4453 r += 10; 4913 r += 10;
4454 } 4914 }
4455 else 4915 else
4456 *r++ = '#'; 4916 *r++ = '\'';
4457 for (p = str; *p != NUL; ++p) 4917 for (p = str; *p != NUL; )
4458 { 4918 {
4459 if (*p == '#') 4919 if (*p == '\'')
4460 *r++ = '#'; 4920 *r++ = '\'';
4461 *r++ = *p; 4921 MB_COPY_CHAR(p, r);
4462 #ifdef FEAT_MBYTE 4922 }
4463 /* For a multi-byte character copy the bytes after the first one. */ 4923 *r++ = '\'';
4464 if (has_mbyte)
4465 {
4466 int l = (*mb_ptr2len_check)(p);
4467
4468 while (--l > 0)
4469 *r++ = *++p;
4470 }
4471 #endif
4472 }
4473 *r++ = '#';
4474 if (function) 4924 if (function)
4475 *r++ = ')'; 4925 *r++ = ')';
4476 *r++ = NUL; 4926 *r++ = NUL;
4477 } 4927 }
4478 return s; 4928 return s;
4633 {"inputrestore", 0, 0, f_inputrestore}, 5083 {"inputrestore", 0, 0, f_inputrestore},
4634 {"inputsave", 0, 0, f_inputsave}, 5084 {"inputsave", 0, 0, f_inputsave},
4635 {"inputsecret", 1, 2, f_inputsecret}, 5085 {"inputsecret", 1, 2, f_inputsecret},
4636 {"insert", 2, 3, f_insert}, 5086 {"insert", 2, 3, f_insert},
4637 {"isdirectory", 1, 1, f_isdirectory}, 5087 {"isdirectory", 1, 1, f_isdirectory},
5088 {"items", 1, 1, f_items},
4638 {"join", 1, 2, f_join}, 5089 {"join", 1, 2, f_join},
5090 {"keys", 1, 1, f_keys},
4639 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */ 5091 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
4640 {"len", 1, 1, f_len}, 5092 {"len", 1, 1, f_len},
4641 {"libcall", 3, 3, f_libcall}, 5093 {"libcall", 3, 3, f_libcall},
4642 {"libcallnr", 3, 3, f_libcallnr}, 5094 {"libcallnr", 3, 3, f_libcallnr},
4643 {"line", 1, 1, f_line}, 5095 {"line", 1, 1, f_line},
4654 {"min", 1, 1, f_min}, 5106 {"min", 1, 1, f_min},
4655 {"mode", 0, 0, f_mode}, 5107 {"mode", 0, 0, f_mode},
4656 {"nextnonblank", 1, 1, f_nextnonblank}, 5108 {"nextnonblank", 1, 1, f_nextnonblank},
4657 {"nr2char", 1, 1, f_nr2char}, 5109 {"nr2char", 1, 1, f_nr2char},
4658 {"prevnonblank", 1, 1, f_prevnonblank}, 5110 {"prevnonblank", 1, 1, f_prevnonblank},
5111 {"range", 1, 3, f_range},
4659 {"remote_expr", 2, 3, f_remote_expr}, 5112 {"remote_expr", 2, 3, f_remote_expr},
4660 {"remote_foreground", 1, 1, f_remote_foreground}, 5113 {"remote_foreground", 1, 1, f_remote_foreground},
4661 {"remote_peek", 1, 2, f_remote_peek}, 5114 {"remote_peek", 1, 2, f_remote_peek},
4662 {"remote_read", 1, 1, f_remote_read}, 5115 {"remote_read", 1, 1, f_remote_read},
4663 {"remote_send", 2, 3, f_remote_send}, 5116 {"remote_send", 2, 3, f_remote_send},
4696 {"tempname", 0, 0, f_tempname}, 5149 {"tempname", 0, 0, f_tempname},
4697 {"tolower", 1, 1, f_tolower}, 5150 {"tolower", 1, 1, f_tolower},
4698 {"toupper", 1, 1, f_toupper}, 5151 {"toupper", 1, 1, f_toupper},
4699 {"tr", 3, 3, f_tr}, 5152 {"tr", 3, 3, f_tr},
4700 {"type", 1, 1, f_type}, 5153 {"type", 1, 1, f_type},
5154 {"values", 1, 1, f_values},
4701 {"virtcol", 1, 1, f_virtcol}, 5155 {"virtcol", 1, 1, f_virtcol},
4702 {"visualmode", 0, 1, f_visualmode}, 5156 {"visualmode", 0, 1, f_visualmode},
4703 {"winbufnr", 1, 1, f_winbufnr}, 5157 {"winbufnr", 1, 1, f_winbufnr},
4704 {"wincol", 0, 0, f_wincol}, 5158 {"wincol", 0, 0, f_wincol},
4705 {"winheight", 1, 1, f_winheight}, 5159 {"winheight", 1, 1, f_winheight},
4903 linenr_T lastline; /* last line of range */ 5357 linenr_T lastline; /* last line of range */
4904 int *doesrange; /* return: function handled range */ 5358 int *doesrange; /* return: function handled range */
4905 int evaluate; 5359 int evaluate;
4906 { 5360 {
4907 int ret = FAIL; 5361 int ret = FAIL;
4908 static char *errors[] =
4909 {N_("E117: Unknown function: %s"),
4910 N_("E118: Too many arguments for function: %s"),
4911 N_("E119: Not enough arguments for function: %s"),
4912 N_("E120: Using <SID> not in a script context: %s"),
4913 };
4914 #define ERROR_UNKNOWN 0 5362 #define ERROR_UNKNOWN 0
4915 #define ERROR_TOOMANY 1 5363 #define ERROR_TOOMANY 1
4916 #define ERROR_TOOFEW 2 5364 #define ERROR_TOOFEW 2
4917 #define ERROR_SCRIPT 3 5365 #define ERROR_SCRIPT 3
4918 #define ERROR_NONE 4 5366 #define ERROR_NONE 4
5061 5509
5062 /* 5510 /*
5063 * Report an error unless the argument evaluation or function call has been 5511 * Report an error unless the argument evaluation or function call has been
5064 * cancelled due to an aborting error, an interrupt, or an exception. 5512 * cancelled due to an aborting error, an interrupt, or an exception.
5065 */ 5513 */
5066 if (error < ERROR_NONE && !aborting()) 5514 if (!aborting())
5067 EMSG2((char_u *)_(errors[error]), name); 5515 {
5516 switch (error)
5517 {
5518 case ERROR_UNKNOWN:
5519 EMSG2(_("E117: Unknown function: %s"), name);
5520 break;
5521 case ERROR_TOOMANY:
5522 EMSG2(_(e_toomanyarg), name);
5523 break;
5524 case ERROR_TOOFEW:
5525 EMSG2(_("E119: Not enough arguments for function: %s"),
5526 name);
5527 break;
5528 case ERROR_SCRIPT:
5529 EMSG2(_("E120: Using <SID> not in a script context: %s"),
5530 name);
5531 break;
5532 }
5533 }
5068 5534
5069 name[len] = cc; 5535 name[len] = cc;
5070 if (fname != name && fname != fname_buf) 5536 if (fname != name && fname != fname_buf)
5071 vim_free(fname); 5537 vim_free(fname);
5072 5538
8206 typeval *rettv; 8672 typeval *rettv;
8207 { 8673 {
8208 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0])); 8674 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
8209 } 8675 }
8210 8676
8677 static void dict_list __ARGS((typeval *argvars, typeval *rettv, int what));
8678
8679 /*
8680 * Turn a dict into a list:
8681 * "what" == 0: list of keys
8682 * "what" == 1: list of values
8683 * "what" == 2: list of items
8684 */
8685 static void
8686 dict_list(argvars, rettv, what)
8687 typeval *argvars;
8688 typeval *rettv;
8689 int what;
8690 {
8691 listvar *l;
8692 listvar *l2;
8693 dictitem *di;
8694 listitem *li;
8695 listitem *li2;
8696
8697 rettv->vval.v_number = 0;
8698 if (argvars[0].v_type != VAR_DICT)
8699 {
8700 EMSG(_(e_dictreq));
8701 return;
8702 }
8703 if (argvars[0].vval.v_dict == NULL)
8704 return;
8705
8706 l = list_alloc();
8707 if (l == NULL)
8708 return;
8709 rettv->v_type = VAR_LIST;
8710 rettv->vval.v_list = l;
8711 ++l->lv_refcount;
8712
8713 for (di = argvars[0].vval.v_dict->dv_first; di != NULL; di = di->di_next)
8714 {
8715 li = listitem_alloc();
8716 if (li == NULL)
8717 break;
8718 list_append(l, li);
8719
8720 if (what == 0)
8721 {
8722 /* keys() */
8723 li->li_tv.v_type = VAR_STRING;
8724 li->li_tv.vval.v_string = vim_strsave(di->di_key);
8725 }
8726 else if (what == 1)
8727 {
8728 /* values() */
8729 copy_tv(&di->di_tv, &li->li_tv);
8730 }
8731 else
8732 {
8733 /* items() */
8734 l2 = list_alloc();
8735 li->li_tv.v_type = VAR_LIST;
8736 li->li_tv.vval.v_list = l2;
8737 if (l2 == NULL)
8738 break;
8739 ++l2->lv_refcount;
8740
8741 li2 = listitem_alloc();
8742 if (li2 == NULL)
8743 break;
8744 list_append(l2, li2);
8745 li2->li_tv.v_type = VAR_STRING;
8746 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
8747
8748 li2 = listitem_alloc();
8749 if (li2 == NULL)
8750 break;
8751 list_append(l2, li2);
8752 copy_tv(&di->di_tv, &li2->li_tv);
8753 }
8754 }
8755 }
8756
8757 /*
8758 * "items(dict)" function
8759 */
8760 static void
8761 f_items(argvars, rettv)
8762 typeval *argvars;
8763 typeval *rettv;
8764 {
8765 dict_list(argvars, rettv, 2);
8766 }
8767
8211 /* 8768 /*
8212 * "join()" function 8769 * "join()" function
8213 */ 8770 */
8214 static void 8771 static void
8215 f_join(argvars, rettv) 8772 f_join(argvars, rettv)
8236 list_join(&ga, argvars[0].vval.v_list, sep, TRUE); 8793 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
8237 ga_append(&ga, NUL); 8794 ga_append(&ga, NUL);
8238 8795
8239 rettv->v_type = VAR_STRING; 8796 rettv->v_type = VAR_STRING;
8240 rettv->vval.v_string = (char_u *)ga.ga_data; 8797 rettv->vval.v_string = (char_u *)ga.ga_data;
8798 }
8799
8800 /*
8801 * "keys()" function
8802 */
8803 static void
8804 f_keys(argvars, rettv)
8805 typeval *argvars;
8806 typeval *rettv;
8807 {
8808 dict_list(argvars, rettv, 0);
8241 } 8809 }
8242 8810
8243 /* 8811 /*
8244 * "last_buffer_nr()" function. 8812 * "last_buffer_nr()" function.
8245 */ 8813 */
8857 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL) 9425 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8858 --lnum; 9426 --lnum;
8859 rettv->vval.v_number = lnum; 9427 rettv->vval.v_number = lnum;
8860 } 9428 }
8861 9429
9430 /*
9431 * "range()" function
9432 */
9433 static void
9434 f_range(argvars, rettv)
9435 typeval *argvars;
9436 typeval *rettv;
9437 {
9438 long start;
9439 long end;
9440 long stride = 1;
9441 long i;
9442 listvar *l;
9443 listitem *li;
9444
9445 start = get_tv_number(&argvars[0]);
9446 if (argvars[1].v_type == VAR_UNKNOWN)
9447 {
9448 end = start - 1;
9449 start = 0;
9450 }
9451 else
9452 {
9453 end = get_tv_number(&argvars[1]);
9454 if (argvars[2].v_type != VAR_UNKNOWN)
9455 stride = get_tv_number(&argvars[2]);
9456 }
9457
9458 rettv->vval.v_number = 0;
9459 if (stride == 0)
9460 EMSG(_("E999: Stride is zero"));
9461 else if (stride > 0 ? end < start : end > start)
9462 EMSG(_("E999: Start past end"));
9463 else
9464 {
9465 l = list_alloc();
9466 if (l != NULL)
9467 {
9468 rettv->v_type = VAR_LIST;
9469 rettv->vval.v_list = l;
9470 ++l->lv_refcount;
9471
9472 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9473 {
9474 li = listitem_alloc();
9475 if (li == NULL)
9476 break;
9477 li->li_tv.v_type = VAR_NUMBER;
9478 li->li_tv.vval.v_number = i;
9479 list_append(l, li);
9480 }
9481 }
9482 }
9483 }
9484
8862 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) 9485 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8863 static void make_connection __ARGS((void)); 9486 static void make_connection __ARGS((void));
8864 static int check_connection __ARGS((void)); 9487 static int check_connection __ARGS((void));
8865 9488
8866 static void 9489 static void
9084 remote_common(argvars, rettv, FALSE); 9707 remote_common(argvars, rettv, FALSE);
9085 #endif 9708 #endif
9086 } 9709 }
9087 9710
9088 /* 9711 /*
9089 * "remove({list}, {idx} [, {end}])" function 9712 * "remove()" function
9090 */ 9713 */
9091 static void 9714 static void
9092 f_remove(argvars, rettv) 9715 f_remove(argvars, rettv)
9093 typeval *argvars; 9716 typeval *argvars;
9094 typeval *rettv; 9717 typeval *rettv;
9096 listvar *l; 9719 listvar *l;
9097 listitem *item, *item2; 9720 listitem *item, *item2;
9098 listitem *li; 9721 listitem *li;
9099 long idx; 9722 long idx;
9100 long end; 9723 long end;
9724 char_u *key;
9725 dictvar *d;
9726 dictitem *di, **pdi;
9101 9727
9102 rettv->vval.v_number = 0; 9728 rettv->vval.v_number = 0;
9103 if (argvars[0].v_type != VAR_LIST) 9729 if (argvars[0].v_type == VAR_DICT)
9104 EMSG2(_(e_listarg), "remove()"); 9730 {
9731 if (argvars[2].v_type != VAR_UNKNOWN)
9732 EMSG2(_(e_toomanyarg), "remove()");
9733 else if ((d = argvars[0].vval.v_dict) != NULL)
9734 {
9735 key = get_tv_string(&argvars[1]);
9736 pdi = &d->dv_first;
9737 for (di = d->dv_first; di != NULL; pdi = &di->di_next, di = *pdi)
9738 if (STRCMP(di->di_key, key) == 0)
9739 {
9740 *pdi = di->di_next;
9741 dictitem_free(di);
9742 break;
9743 }
9744 if (di == NULL)
9745 EMSG2(_(e_dictkey), key);
9746 }
9747 }
9748 else if (argvars[0].v_type != VAR_LIST)
9749 EMSG2(_(e_listdictarg), "remove()");
9105 else if ((l = argvars[0].vval.v_list) != NULL) 9750 else if ((l = argvars[0].vval.v_list) != NULL)
9106 { 9751 {
9107 idx = get_tv_number(&argvars[1]); 9752 idx = get_tv_number(&argvars[1]);
9108 item = list_find(l, idx); 9753 item = list_find(l, idx);
9109 if (item == NULL) 9754 if (item == NULL)
10928 } 11573 }
10929 rettv->vval.v_number = n; 11574 rettv->vval.v_number = n;
10930 } 11575 }
10931 11576
10932 /* 11577 /*
11578 * "values(dict)" function
11579 */
11580 static void
11581 f_values(argvars, rettv)
11582 typeval *argvars;
11583 typeval *rettv;
11584 {
11585 dict_list(argvars, rettv, 1);
11586 }
11587
11588 /*
10933 * "virtcol(string)" function 11589 * "virtcol(string)" function
10934 */ 11590 */
10935 static void 11591 static void
10936 f_virtcol(argvars, rettv) 11592 f_virtcol(argvars, rettv)
10937 typeval *argvars; 11593 typeval *argvars;
11323 static char_u * 11979 static char_u *
11324 find_name_end(arg, expr_start, expr_end, incl_br) 11980 find_name_end(arg, expr_start, expr_end, incl_br)
11325 char_u *arg; 11981 char_u *arg;
11326 char_u **expr_start; 11982 char_u **expr_start;
11327 char_u **expr_end; 11983 char_u **expr_end;
11328 int incl_br; /* Include [] indexes */ 11984 int incl_br; /* Include [] indexes and .name */
11329 { 11985 {
11330 int mb_nest = 0; 11986 int mb_nest = 0;
11331 int br_nest = 0; 11987 int br_nest = 0;
11332 char_u *p; 11988 char_u *p;
11333 11989
11337 *expr_end = NULL; 11993 *expr_end = NULL;
11338 } 11994 }
11339 11995
11340 for (p = arg; *p != NUL 11996 for (p = arg; *p != NUL
11341 && (eval_isnamec(*p) 11997 && (eval_isnamec(*p)
11342 || (*p == '[' && incl_br) 11998 || (incl_br && (*p == '[' || *p == '.'))
11343 || mb_nest != 0 11999 || mb_nest != 0
11344 || br_nest != 0); ++p) 12000 || br_nest != 0); ++p)
11345 { 12001 {
11346 if (mb_nest == 0) 12002 if (mb_nest == 0)
11347 { 12003 {
11718 varp->vval.v_string = NULL; 12374 varp->vval.v_string = NULL;
11719 break; 12375 break;
11720 case VAR_LIST: 12376 case VAR_LIST:
11721 list_unref(varp->vval.v_list); 12377 list_unref(varp->vval.v_list);
11722 break; 12378 break;
12379 case VAR_DICT:
12380 dict_unref(varp->vval.v_dict);
12381 break;
11723 case VAR_NUMBER: 12382 case VAR_NUMBER:
11724 varp->vval.v_number = 0; 12383 varp->vval.v_number = 0;
11725 break; 12384 break;
11726 case VAR_UNKNOWN: 12385 case VAR_UNKNOWN:
11727 break; 12386 break;
11821 { 12480 {
11822 case VAR_NUMBER: 12481 case VAR_NUMBER:
11823 sprintf((char *)buf, "%ld", (long)varp->vval.v_number); 12482 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
11824 return buf; 12483 return buf;
11825 case VAR_FUNC: 12484 case VAR_FUNC:
11826 EMSG(_("E99: using Funcref as a String")); 12485 EMSG(_("E999: using Funcref as a String"));
11827 break; 12486 break;
11828 case VAR_LIST: 12487 case VAR_LIST:
11829 EMSG(_("E99: using List as a String")); 12488 EMSG(_("E999: using List as a String"));
12489 break;
12490 case VAR_DICT:
12491 EMSG(_("E999: using Dictionary as a String"));
11830 break; 12492 break;
11831 case VAR_STRING: 12493 case VAR_STRING:
11832 if (varp->vval.v_string != NULL) 12494 if (varp->vval.v_string != NULL)
11833 return varp->vval.v_string; 12495 return varp->vval.v_string;
11834 break; 12496 break;
12084 msg_putchar('*'); 12746 msg_putchar('*');
12085 else if (type == VAR_LIST) 12747 else if (type == VAR_LIST)
12086 { 12748 {
12087 msg_putchar('['); 12749 msg_putchar('[');
12088 if (*string == '[') 12750 if (*string == '[')
12751 ++string;
12752 }
12753 else if (type == VAR_DICT)
12754 {
12755 msg_putchar('{');
12756 if (*string == '{')
12089 ++string; 12757 ++string;
12090 } 12758 }
12091 else 12759 else
12092 msg_putchar(' '); 12760 msg_putchar(' ');
12093 12761
12235 to->vval.v_list = NULL; 12903 to->vval.v_list = NULL;
12236 else 12904 else
12237 { 12905 {
12238 to->vval.v_list = from->vval.v_list; 12906 to->vval.v_list = from->vval.v_list;
12239 ++to->vval.v_list->lv_refcount; 12907 ++to->vval.v_list->lv_refcount;
12908 }
12909 break;
12910 case VAR_DICT:
12911 if (from->vval.v_dict == NULL)
12912 to->vval.v_dict = NULL;
12913 else
12914 {
12915 to->vval.v_dict = from->vval.v_dict;
12916 ++to->vval.v_dict->dv_refcount;
12240 } 12917 }
12241 break; 12918 break;
12242 default: 12919 default:
12243 EMSG2(_(e_intern2), "copy_tv()"); 12920 EMSG2(_(e_intern2), "copy_tv()");
12244 break; 12921 break;