comparison src/eval.c @ 142:0ef5b70c3eaf v7.0045

updated for version 7.0045
author vimboss
date Thu, 27 Jan 2005 14:44:31 +0000
parents 3e7d17e425b0
children 40a0699b6c62
comparison
equal deleted inserted replaced
141:88256bf8571c 142:0ef5b70c3eaf
114 static dict_T globvardict; 114 static dict_T globvardict;
115 static dictitem_T globvars_var; 115 static dictitem_T globvars_var;
116 #define globvarht globvardict.dv_hashtab 116 #define globvarht globvardict.dv_hashtab
117 117
118 /* 118 /*
119 * Old Vim variables such as "v:version" are also available without the "v:".
120 * Also in functions. We need a special hashtable for them.
121 */
122 hashtab_T compat_hashtab;
123
124 /*
119 * Array to hold the hashtab with variables local to each sourced script. 125 * Array to hold the hashtab with variables local to each sourced script.
120 * Each item holds a variable (nameless) that points to the dict_T. 126 * Each item holds a variable (nameless) that points to the dict_T.
121 */ 127 */
122 typedef struct 128 typedef struct
123 { 129 {
211 * Struct used by trans_function_name() 217 * Struct used by trans_function_name()
212 */ 218 */
213 typedef struct 219 typedef struct
214 { 220 {
215 dict_T *fd_dict; /* Dictionary used */ 221 dict_T *fd_dict; /* Dictionary used */
216 char_u *fd_newkey; /* new key in "dict" */ 222 char_u *fd_newkey; /* new key in "dict" in allocated memory */
217 dictitem_T *fd_di; /* Dictionary item used */ 223 dictitem_T *fd_di; /* Dictionary item used */
218 } funcdict_T; 224 } funcdict_T;
219 225
220 226
221 /* 227 /*
597 int i; 603 int i;
598 struct vimvar *p; 604 struct vimvar *p;
599 605
600 init_var_dict(&globvardict, &globvars_var); 606 init_var_dict(&globvardict, &globvars_var);
601 init_var_dict(&vimvardict, &vimvars_var); 607 init_var_dict(&vimvardict, &vimvars_var);
608 hash_init(&compat_hashtab);
602 609
603 for (i = 0; i < VV_LEN; ++i) 610 for (i = 0; i < VV_LEN; ++i)
604 { 611 {
605 p = &vimvars[i]; 612 p = &vimvars[i];
606 STRCPY(p->vv_di.di_key, p->vv_name); 613 STRCPY(p->vv_di.di_key, p->vv_name);
611 else 618 else
612 p->vv_di.di_flags = DI_FLAGS_FIX; 619 p->vv_di.di_flags = DI_FLAGS_FIX;
613 /* add to v: scope dict */ 620 /* add to v: scope dict */
614 hash_add(&vimvarht, p->vv_di.di_key); 621 hash_add(&vimvarht, p->vv_di.di_key);
615 if (p->vv_flags & VV_COMPAT) 622 if (p->vv_flags & VV_COMPAT)
616 /* add to g: scope dict */ 623 /* add to compat scope dict */
617 hash_add(&globvardict.dv_hashtab, p->vv_di.di_key); 624 hash_add(&compat_hashtab, p->vv_di.di_key);
618 } 625 }
619 } 626 }
620 627
621 /* 628 /*
622 * Return the name of the executed function. 629 * Return the name of the executed function.
2546 2553
2547 /* If it is the name of a variable of type VAR_FUNC use its contents. */ 2554 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2548 len = STRLEN(tofree); 2555 len = STRLEN(tofree);
2549 name = deref_func_name(tofree, &len); 2556 name = deref_func_name(tofree, &len);
2550 2557
2551 startarg = arg; 2558 /* Skip white space to allow ":call func ()". Not good, but required for
2559 * backward compatibility. */
2560 startarg = skipwhite(arg);
2552 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 2561 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
2553 2562
2554 if (*startarg != '(') 2563 if (*startarg != '(')
2555 { 2564 {
2556 EMSG2(_("E107: Missing braces: %s"), name); 2565 EMSG2(_("E107: Missing braces: %s"), name);
2813 char_u * 2822 char_u *
2814 get_user_var_name(xp, idx) 2823 get_user_var_name(xp, idx)
2815 expand_T *xp; 2824 expand_T *xp;
2816 int idx; 2825 int idx;
2817 { 2826 {
2818 static int gdone; 2827 static long_u gdone;
2819 static int bdone; 2828 static long_u bdone;
2820 static int wdone; 2829 static long_u wdone;
2821 static int vidx; 2830 static int vidx;
2822 static hashitem_T *hi; 2831 static hashitem_T *hi;
2823 hashtab_T *ht; 2832 hashtab_T *ht;
2824 2833
2825 if (idx == 0) 2834 if (idx == 0)
2826 gdone = bdone = wdone = vidx = 0; 2835 gdone = bdone = wdone = vidx = 0;
2827 2836
2828 /* Global variables */ 2837 /* Global variables */
2829 if (gdone < globvarht.ht_used) 2838 if (gdone < globvarht.ht_used)
2830 { 2839 {
2831 if (gdone++ == 0) 2840 if (gdone++ == 0)
2832 hi = globvarht.ht_array; 2841 hi = globvarht.ht_array;
2842 else
2843 ++hi;
2833 while (HASHITEM_EMPTY(hi)) 2844 while (HASHITEM_EMPTY(hi))
2834 ++hi; 2845 ++hi;
2835 if (STRNCMP("g:", xp->xp_pattern, 2) == 0) 2846 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2836 return cat_prefix_varname('g', hi->hi_key); 2847 return cat_prefix_varname('g', hi->hi_key);
2837 return hi->hi_key; 2848 return hi->hi_key;
2841 ht = &curbuf->b_vars.dv_hashtab; 2852 ht = &curbuf->b_vars.dv_hashtab;
2842 if (bdone < ht->ht_used) 2853 if (bdone < ht->ht_used)
2843 { 2854 {
2844 if (bdone++ == 0) 2855 if (bdone++ == 0)
2845 hi = ht->ht_array; 2856 hi = ht->ht_array;
2857 else
2858 ++hi;
2846 while (HASHITEM_EMPTY(hi)) 2859 while (HASHITEM_EMPTY(hi))
2847 ++hi; 2860 ++hi;
2848 return cat_prefix_varname('b', hi->hi_key); 2861 return cat_prefix_varname('b', hi->hi_key);
2849 } 2862 }
2850 if (bdone == ht->ht_used) 2863 if (bdone == ht->ht_used)
2857 ht = &curwin->w_vars.dv_hashtab; 2870 ht = &curwin->w_vars.dv_hashtab;
2858 if (wdone < ht->ht_used) 2871 if (wdone < ht->ht_used)
2859 { 2872 {
2860 if (bdone++ == 0) 2873 if (bdone++ == 0)
2861 hi = ht->ht_array; 2874 hi = ht->ht_array;
2875 else
2876 ++hi;
2862 while (HASHITEM_EMPTY(hi)) 2877 while (HASHITEM_EMPTY(hi))
2863 ++hi; 2878 ++hi;
2864 return cat_prefix_varname('w', hi->hi_key); 2879 return cat_prefix_varname('w', hi->hi_key);
2865 } 2880 }
2866 2881
5715 #endif 5730 #endif
5716 {"stridx", 2, 3, f_stridx}, 5731 {"stridx", 2, 3, f_stridx},
5717 {"string", 1, 1, f_string}, 5732 {"string", 1, 1, f_string},
5718 {"strlen", 1, 1, f_strlen}, 5733 {"strlen", 1, 1, f_strlen},
5719 {"strpart", 2, 3, f_strpart}, 5734 {"strpart", 2, 3, f_strpart},
5720 {"strridx", 2, 2, f_strridx}, 5735 {"strridx", 2, 3, f_strridx},
5721 {"strtrans", 1, 1, f_strtrans}, 5736 {"strtrans", 1, 1, f_strtrans},
5722 {"submatch", 1, 1, f_submatch}, 5737 {"submatch", 1, 1, f_submatch},
5723 {"substitute", 4, 4, f_substitute}, 5738 {"substitute", 4, 4, f_substitute},
5724 {"synID", 3, 3, f_synID}, 5739 {"synID", 3, 3, f_synID},
5725 {"synIDattr", 2, 3, f_synIDattr}, 5740 {"synIDattr", 2, 3, f_synIDattr},
11789 rettv->vval.v_number = -1; 11804 rettv->vval.v_number = -1;
11790 11805
11791 if (argvars[2].v_type != VAR_UNKNOWN) 11806 if (argvars[2].v_type != VAR_UNKNOWN)
11792 { 11807 {
11793 start_idx = get_tv_number(&argvars[2]); 11808 start_idx = get_tv_number(&argvars[2]);
11794 if (start_idx < 0 || start_idx >= (int)STRLEN(haystack)) 11809 if (start_idx >= (int)STRLEN(haystack))
11795 return; 11810 return;
11796 haystack += start_idx; 11811 if (start_idx >= 0)
11812 haystack += start_idx;
11797 } 11813 }
11798 11814
11799 pos = (char_u *)strstr((char *)haystack, (char *)needle); 11815 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11800 if (pos != NULL) 11816 if (pos != NULL)
11801 rettv->vval.v_number = (varnumber_T)(pos - save_haystack); 11817 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11883 char_u buf[NUMBUFLEN]; 11899 char_u buf[NUMBUFLEN];
11884 char_u *needle; 11900 char_u *needle;
11885 char_u *haystack; 11901 char_u *haystack;
11886 char_u *rest; 11902 char_u *rest;
11887 char_u *lastmatch = NULL; 11903 char_u *lastmatch = NULL;
11904 int haystack_len, end_idx;
11888 11905
11889 needle = get_tv_string(&argvars[1]); 11906 needle = get_tv_string(&argvars[1]);
11890 haystack = get_tv_string_buf(&argvars[0], buf); 11907 haystack = get_tv_string_buf(&argvars[0], buf);
11908 haystack_len = STRLEN(haystack);
11891 if (*needle == NUL) 11909 if (*needle == NUL)
11892 /* Empty string matches past the end. */ 11910 /* Empty string matches past the end. */
11893 lastmatch = haystack + STRLEN(haystack); 11911 lastmatch = haystack + haystack_len;
11894 else 11912 else
11913 {
11914 if (argvars[2].v_type != VAR_UNKNOWN)
11915 {
11916 /* Third argument: upper limit for index */
11917 end_idx = get_tv_number(&argvars[2]);
11918 if (end_idx < 0)
11919 {
11920 /* can never find a match */
11921 rettv->vval.v_number = -1;
11922 return;
11923 }
11924 }
11925 else
11926 end_idx = haystack_len;
11927
11895 for (rest = haystack; *rest != '\0'; ++rest) 11928 for (rest = haystack; *rest != '\0'; ++rest)
11896 { 11929 {
11897 rest = (char_u *)strstr((char *)rest, (char *)needle); 11930 rest = (char_u *)strstr((char *)rest, (char *)needle);
11898 if (rest == NULL) 11931 if (rest == NULL || rest > haystack + end_idx)
11899 break; 11932 break;
11900 lastmatch = rest; 11933 lastmatch = rest;
11901 } 11934 }
11935 }
11902 11936
11903 if (lastmatch == NULL) 11937 if (lastmatch == NULL)
11904 rettv->vval.v_number = -1; 11938 rettv->vval.v_number = -1;
11905 else 11939 else
11906 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack); 11940 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13406 { 13440 {
13407 /* If not "x:name" there must not be any ":" in the name. */ 13441 /* If not "x:name" there must not be any ":" in the name. */
13408 if (vim_strchr(name, ':') != NULL) 13442 if (vim_strchr(name, ':') != NULL)
13409 return NULL; 13443 return NULL;
13410 *varname = name; 13444 *varname = name;
13445
13446 /* "version" is "v:version" in all scopes */
13447 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
13448 return &compat_hashtab;
13449
13411 if (current_funccal == NULL) 13450 if (current_funccal == NULL)
13412 return &globvarht; /* global variable */ 13451 return &globvarht; /* global variable */
13413 return &current_funccal->l_vars.dv_hashtab; /* l: variable */ 13452 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
13414 } 13453 }
13415 *varname = name + 2; 13454 *varname = name + 2;