comparison src/eval.c @ 165:e943e5502fc9 v7.0050

updated for version 7.0050
author vimboss
date Sat, 12 Feb 2005 14:31:42 +0000
parents 06bc859d1a32
children 0e902b8f511f
comparison
equal deleted inserted replaced
164:8b0ee9d57d7f 165:e943e5502fc9
333 static void list_append __ARGS((list_T *l, listitem_T *item)); 333 static void list_append __ARGS((list_T *l, listitem_T *item));
334 static int list_append_tv __ARGS((list_T *l, typval_T *tv)); 334 static int list_append_tv __ARGS((list_T *l, typval_T *tv));
335 static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item)); 335 static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
336 static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef)); 336 static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
337 static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv)); 337 static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
338 static list_T *list_copy __ARGS((list_T *orig, int deep)); 338 static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
339 static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2)); 339 static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
340 static char_u *list2string __ARGS((typval_T *tv)); 340 static char_u *list2string __ARGS((typval_T *tv));
341 static void list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo)); 341 static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
342 342
343 static dict_T *dict_alloc __ARGS((void)); 343 static dict_T *dict_alloc __ARGS((void));
344 static void dict_unref __ARGS((dict_T *d)); 344 static void dict_unref __ARGS((dict_T *d));
345 static void dict_free __ARGS((dict_T *d)); 345 static void dict_free __ARGS((dict_T *d));
346 static dictitem_T *dictitem_alloc __ARGS((char_u *key)); 346 static dictitem_T *dictitem_alloc __ARGS((char_u *key));
347 static dictitem_T *dictitem_copy __ARGS((dictitem_T *org)); 347 static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
348 static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item)); 348 static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
349 static void dictitem_free __ARGS((dictitem_T *item)); 349 static void dictitem_free __ARGS((dictitem_T *item));
350 static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
350 static int dict_add __ARGS((dict_T *d, dictitem_T *item)); 351 static int dict_add __ARGS((dict_T *d, dictitem_T *item));
351 static long dict_len __ARGS((dict_T *d)); 352 static long dict_len __ARGS((dict_T *d));
352 static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len)); 353 static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
353 static char_u *dict2string __ARGS((typval_T *tv)); 354 static char_u *dict2string __ARGS((typval_T *tv));
354 static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 355 static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
359 static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 360 static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
360 static int find_internal_func __ARGS((char_u *name)); 361 static int find_internal_func __ARGS((char_u *name));
361 static char_u *deref_func_name __ARGS((char_u *name, int *lenp)); 362 static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
362 static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict)); 363 static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
363 static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict)); 364 static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
365 static void emsg_funcname __ARGS((char *msg, char_u *name));
364 366
365 static void f_add __ARGS((typval_T *argvars, typval_T *rettv)); 367 static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
366 static void f_append __ARGS((typval_T *argvars, typval_T *rettv)); 368 static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
367 static void f_argc __ARGS((typval_T *argvars, typval_T *rettv)); 369 static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
368 static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv)); 370 static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
560 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string)); 562 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
561 static void set_var __ARGS((char_u *name, typval_T *varp, int copy)); 563 static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
562 static int var_check_ro __ARGS((int flags, char_u *name)); 564 static int var_check_ro __ARGS((int flags, char_u *name));
563 static int tv_check_lock __ARGS((int lock, char_u *name)); 565 static int tv_check_lock __ARGS((int lock, char_u *name));
564 static void copy_tv __ARGS((typval_T *from, typval_T *to)); 566 static void copy_tv __ARGS((typval_T *from, typval_T *to));
565 static void item_copy __ARGS((typval_T *from, typval_T *to, int deep)); 567 static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
566 static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags)); 568 static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
567 static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd)); 569 static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
568 static int eval_fname_script __ARGS((char_u *p)); 570 static int eval_fname_script __ARGS((char_u *p));
569 static int eval_fname_sid __ARGS((char_u *p)); 571 static int eval_fname_sid __ARGS((char_u *p));
570 static void list_func_head __ARGS((ufunc_T *fp, int indent)); 572 static void list_func_head __ARGS((ufunc_T *fp, int indent));
2495 startarg = skipwhite(arg); 2497 startarg = skipwhite(arg);
2496 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 2498 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
2497 2499
2498 if (*startarg != '(') 2500 if (*startarg != '(')
2499 { 2501 {
2500 if (*name == K_SPECIAL) 2502 EMSG2(_("E107: Missing braces: %s"), eap->arg);
2501 EMSG2(_("E107: Missing braces: <SNR>%s"), name + 3);
2502 else
2503 EMSG2(_("E107: Missing braces: %s"), name);
2504 goto end; 2503 goto end;
2505 } 2504 }
2506 2505
2507 /* 2506 /*
2508 * When skipping, evaluate the function once, to find the end of the 2507 * When skipping, evaluate the function once, to find the end of the
4959 typval_T *tv; 4958 typval_T *tv;
4960 { 4959 {
4961 list_T *l; 4960 list_T *l;
4962 4961
4963 /* make a copy of the first list. */ 4962 /* make a copy of the first list. */
4964 l = list_copy(l1, FALSE); 4963 l = list_copy(l1, FALSE, 0);
4965 if (l == NULL) 4964 if (l == NULL)
4966 return FAIL; 4965 return FAIL;
4967 tv->v_type = VAR_LIST; 4966 tv->v_type = VAR_LIST;
4968 tv->vval.v_list = l; 4967 tv->vval.v_list = l;
4969 4968
4972 } 4971 }
4973 4972
4974 /* 4973 /*
4975 * Make a copy of list "orig". Shallow if "deep" is FALSE. 4974 * Make a copy of list "orig". Shallow if "deep" is FALSE.
4976 * The refcount of the new list is set to 1. 4975 * The refcount of the new list is set to 1.
4976 * See item_copy() for "copyID".
4977 * Returns NULL when out of memory. 4977 * Returns NULL when out of memory.
4978 */ 4978 */
4979 static list_T * 4979 static list_T *
4980 list_copy(orig, deep) 4980 list_copy(orig, deep, copyID)
4981 list_T *orig; 4981 list_T *orig;
4982 int deep; 4982 int deep;
4983 int copyID;
4983 { 4984 {
4984 list_T *copy; 4985 list_T *copy;
4985 listitem_T *item; 4986 listitem_T *item;
4986 listitem_T *ni; 4987 listitem_T *ni;
4987 4988
4989 return NULL; 4990 return NULL;
4990 4991
4991 copy = list_alloc(); 4992 copy = list_alloc();
4992 if (copy != NULL) 4993 if (copy != NULL)
4993 { 4994 {
4994 for (item = orig->lv_first; item != NULL; item = item->li_next) 4995 if (copyID != 0)
4996 {
4997 /* Do this before adding the items, because one of the items may
4998 * refer back to this list. */
4999 orig->lv_copyID = copyID;
5000 orig->lv_copylist = copy;
5001 }
5002 for (item = orig->lv_first; item != NULL && !got_int;
5003 item = item->li_next)
4995 { 5004 {
4996 ni = listitem_alloc(); 5005 ni = listitem_alloc();
4997 if (ni == NULL) 5006 if (ni == NULL)
4998 break; 5007 break;
4999 if (deep) 5008 if (deep)
5000 item_copy(&item->li_tv, &ni->li_tv, deep); 5009 {
5010 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5011 {
5012 vim_free(ni);
5013 break;
5014 }
5015 }
5001 else 5016 else
5002 copy_tv(&item->li_tv, &ni->li_tv); 5017 copy_tv(&item->li_tv, &ni->li_tv);
5003 list_append(copy, ni); 5018 list_append(copy, ni);
5004 } 5019 }
5005 ++copy->lv_refcount; 5020 ++copy->lv_refcount;
5021 if (item != NULL)
5022 {
5023 list_unref(copy);
5024 copy = NULL;
5025 }
5006 } 5026 }
5007 5027
5008 return copy; 5028 return copy;
5009 } 5029 }
5010 5030
5052 5072
5053 if (tv->vval.v_list == NULL) 5073 if (tv->vval.v_list == NULL)
5054 return NULL; 5074 return NULL;
5055 ga_init2(&ga, (int)sizeof(char), 80); 5075 ga_init2(&ga, (int)sizeof(char), 80);
5056 ga_append(&ga, '['); 5076 ga_append(&ga, '[');
5057 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE); 5077 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5078 {
5079 vim_free(ga.ga_data);
5080 return NULL;
5081 }
5058 ga_append(&ga, ']'); 5082 ga_append(&ga, ']');
5059 ga_append(&ga, NUL); 5083 ga_append(&ga, NUL);
5060 return (char_u *)ga.ga_data; 5084 return (char_u *)ga.ga_data;
5061 } 5085 }
5062 5086
5063 /* 5087 /*
5064 * Join list "l" into a string in "*gap", using separator "sep". 5088 * Join list "l" into a string in "*gap", using separator "sep".
5065 * When "echo" is TRUE use String as echoed, otherwise as inside a List. 5089 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
5066 */ 5090 * Return FAIL or OK.
5067 static void 5091 */
5092 static int
5068 list_join(gap, l, sep, echo) 5093 list_join(gap, l, sep, echo)
5069 garray_T *gap; 5094 garray_T *gap;
5070 list_T *l; 5095 list_T *l;
5071 char_u *sep; 5096 char_u *sep;
5072 int echo; 5097 int echo;
5075 char_u *tofree; 5100 char_u *tofree;
5076 char_u numbuf[NUMBUFLEN]; 5101 char_u numbuf[NUMBUFLEN];
5077 listitem_T *item; 5102 listitem_T *item;
5078 char_u *s; 5103 char_u *s;
5079 5104
5080 for (item = l->lv_first; item != NULL; item = item->li_next) 5105 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
5081 { 5106 {
5082 if (first) 5107 if (first)
5083 first = FALSE; 5108 first = FALSE;
5084 else 5109 else
5085 ga_concat(gap, sep); 5110 ga_concat(gap, sep);
5089 else 5114 else
5090 s = tv2string(&item->li_tv, &tofree, numbuf); 5115 s = tv2string(&item->li_tv, &tofree, numbuf);
5091 if (s != NULL) 5116 if (s != NULL)
5092 ga_concat(gap, s); 5117 ga_concat(gap, s);
5093 vim_free(tofree); 5118 vim_free(tofree);
5094 } 5119 if (s == NULL)
5120 return FAIL;
5121 }
5122 return OK;
5095 } 5123 }
5096 5124
5097 /* 5125 /*
5098 * Allocate an empty header for a dictionary. 5126 * Allocate an empty header for a dictionary.
5099 */ 5127 */
5106 if (d != NULL) 5134 if (d != NULL)
5107 { 5135 {
5108 hash_init(&d->dv_hashtab); 5136 hash_init(&d->dv_hashtab);
5109 d->dv_lock = 0; 5137 d->dv_lock = 0;
5110 d->dv_refcount = 0; 5138 d->dv_refcount = 0;
5139 d->dv_copyID = 0;
5111 } 5140 }
5112 return d; 5141 return d;
5113 } 5142 }
5114 5143
5115 /* 5144 /*
5220 } 5249 }
5221 5250
5222 /* 5251 /*
5223 * Make a copy of dict "d". Shallow if "deep" is FALSE. 5252 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5224 * The refcount of the new dict is set to 1. 5253 * The refcount of the new dict is set to 1.
5254 * See item_copy() for "copyID".
5225 * Returns NULL when out of memory. 5255 * Returns NULL when out of memory.
5226 */ 5256 */
5227 static dict_T * 5257 static dict_T *
5228 dict_copy(orig, deep) 5258 dict_copy(orig, deep, copyID)
5229 dict_T *orig; 5259 dict_T *orig;
5230 int deep; 5260 int deep;
5261 int copyID;
5231 { 5262 {
5232 dict_T *copy; 5263 dict_T *copy;
5233 dictitem_T *di; 5264 dictitem_T *di;
5234 int todo; 5265 int todo;
5235 hashitem_T *hi; 5266 hashitem_T *hi;
5238 return NULL; 5269 return NULL;
5239 5270
5240 copy = dict_alloc(); 5271 copy = dict_alloc();
5241 if (copy != NULL) 5272 if (copy != NULL)
5242 { 5273 {
5274 if (copyID != 0)
5275 {
5276 orig->dv_copyID = copyID;
5277 orig->dv_copydict = copy;
5278 }
5243 todo = orig->dv_hashtab.ht_used; 5279 todo = orig->dv_hashtab.ht_used;
5244 for (hi = orig->dv_hashtab.ht_array; todo > 0; ++hi) 5280 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5245 { 5281 {
5246 if (!HASHITEM_EMPTY(hi)) 5282 if (!HASHITEM_EMPTY(hi))
5247 { 5283 {
5248 --todo; 5284 --todo;
5249 5285
5250 di = dictitem_alloc(hi->hi_key); 5286 di = dictitem_alloc(hi->hi_key);
5251 if (di == NULL) 5287 if (di == NULL)
5252 break; 5288 break;
5253 if (deep) 5289 if (deep)
5254 item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep); 5290 {
5291 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5292 copyID) == FAIL)
5293 {
5294 vim_free(di);
5295 break;
5296 }
5297 }
5255 else 5298 else
5256 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv); 5299 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5257 if (dict_add(copy, di) == FAIL) 5300 if (dict_add(copy, di) == FAIL)
5258 { 5301 {
5259 dictitem_free(di); 5302 dictitem_free(di);
5261 } 5304 }
5262 } 5305 }
5263 } 5306 }
5264 5307
5265 ++copy->dv_refcount; 5308 ++copy->dv_refcount;
5309 if (todo > 0)
5310 {
5311 dict_unref(copy);
5312 copy = NULL;
5313 }
5266 } 5314 }
5267 5315
5268 return copy; 5316 return copy;
5269 } 5317 }
5270 5318
5353 return NULL; 5401 return NULL;
5354 ga_init2(&ga, (int)sizeof(char), 80); 5402 ga_init2(&ga, (int)sizeof(char), 80);
5355 ga_append(&ga, '{'); 5403 ga_append(&ga, '{');
5356 5404
5357 todo = d->dv_hashtab.ht_used; 5405 todo = d->dv_hashtab.ht_used;
5358 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 5406 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5359 { 5407 {
5360 if (!HASHITEM_EMPTY(hi)) 5408 if (!HASHITEM_EMPTY(hi))
5361 { 5409 {
5362 --todo; 5410 --todo;
5363 5411
5375 ga_concat(&ga, (char_u *)": "); 5423 ga_concat(&ga, (char_u *)": ");
5376 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf); 5424 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5377 if (s != NULL) 5425 if (s != NULL)
5378 ga_concat(&ga, s); 5426 ga_concat(&ga, s);
5379 vim_free(tofree); 5427 vim_free(tofree);
5380 } 5428 if (s == NULL)
5429 break;
5430 }
5431 }
5432 if (todo > 0)
5433 {
5434 vim_free(ga.ga_data);
5435 return NULL;
5381 } 5436 }
5382 5437
5383 ga_append(&ga, '}'); 5438 ga_append(&ga, '}');
5384 ga_append(&ga, NUL); 5439 ga_append(&ga, NUL);
5385 return (char_u *)ga.ga_data; 5440 return (char_u *)ga.ga_data;
5719 {"confirm", 1, 4, f_confirm}, 5774 {"confirm", 1, 4, f_confirm},
5720 {"copy", 1, 1, f_copy}, 5775 {"copy", 1, 1, f_copy},
5721 {"count", 2, 4, f_count}, 5776 {"count", 2, 4, f_count},
5722 {"cscope_connection",0,3, f_cscope_connection}, 5777 {"cscope_connection",0,3, f_cscope_connection},
5723 {"cursor", 2, 2, f_cursor}, 5778 {"cursor", 2, 2, f_cursor},
5724 {"deepcopy", 1, 1, f_deepcopy}, 5779 {"deepcopy", 1, 2, f_deepcopy},
5725 {"delete", 1, 1, f_delete}, 5780 {"delete", 1, 1, f_delete},
5726 {"did_filetype", 0, 0, f_did_filetype}, 5781 {"did_filetype", 0, 0, f_did_filetype},
5727 {"diff_filler", 1, 1, f_diff_filler}, 5782 {"diff_filler", 1, 1, f_diff_filler},
5728 {"diff_hlID", 2, 2, f_diff_hlID}, 5783 {"diff_hlID", 2, 2, f_diff_hlID},
5729 {"empty", 1, 1, f_empty}, 5784 {"empty", 1, 1, f_empty},
6041 ret = call_func(name, len, rettv, argcount, argvars, 6096 ret = call_func(name, len, rettv, argcount, argvars,
6042 firstline, lastline, doesrange, evaluate, selfdict); 6097 firstline, lastline, doesrange, evaluate, selfdict);
6043 else if (!aborting()) 6098 else if (!aborting())
6044 { 6099 {
6045 if (argcount == MAX_FUNC_ARGS) 6100 if (argcount == MAX_FUNC_ARGS)
6046 EMSG2(_("E740: Too many arguments for function %s"), name); 6101 emsg_funcname("E740: Too many arguments for function %s", name);
6047 else 6102 else
6048 EMSG2(_("E116: Invalid arguments for function %s"), name); 6103 emsg_funcname("E116: Invalid arguments for function %s", name);
6049 } 6104 }
6050 6105
6051 while (--argcount >= 0) 6106 while (--argcount >= 0)
6052 clear_tv(&argvars[argcount]); 6107 clear_tv(&argvars[argcount]);
6053 6108
6245 if (!aborting()) 6300 if (!aborting())
6246 { 6301 {
6247 switch (error) 6302 switch (error)
6248 { 6303 {
6249 case ERROR_UNKNOWN: 6304 case ERROR_UNKNOWN:
6250 EMSG2(_("E117: Unknown function: %s"), name); 6305 emsg_funcname("E117: Unknown function: %s", name);
6251 break; 6306 break;
6252 case ERROR_TOOMANY: 6307 case ERROR_TOOMANY:
6253 EMSG2(_(e_toomanyarg), name); 6308 emsg_funcname(e_toomanyarg, name);
6254 break; 6309 break;
6255 case ERROR_TOOFEW: 6310 case ERROR_TOOFEW:
6256 EMSG2(_("E119: Not enough arguments for function: %s"), 6311 emsg_funcname("E119: Not enough arguments for function: %s",
6257 name); 6312 name);
6258 break; 6313 break;
6259 case ERROR_SCRIPT: 6314 case ERROR_SCRIPT:
6260 EMSG2(_("E120: Using <SID> not in a script context: %s"), 6315 emsg_funcname("E120: Using <SID> not in a script context: %s",
6261 name); 6316 name);
6262 break; 6317 break;
6263 case ERROR_DICT: 6318 case ERROR_DICT:
6264 EMSG2(_("E725: Calling dict function without Dictionary: %s"), 6319 emsg_funcname("E725: Calling dict function without Dictionary: %s",
6265 name); 6320 name);
6266 break; 6321 break;
6267 } 6322 }
6268 } 6323 }
6269 6324
6270 name[len] = cc; 6325 name[len] = cc;
6271 if (fname != name && fname != fname_buf) 6326 if (fname != name && fname != fname_buf)
6272 vim_free(fname); 6327 vim_free(fname);
6273 6328
6274 return ret; 6329 return ret;
6330 }
6331
6332 /*
6333 * Give an error message with a function name. Handle <SNR> things.
6334 */
6335 static void
6336 emsg_funcname(msg, name)
6337 char *msg;
6338 char_u *name;
6339 {
6340 char_u *p;
6341
6342 if (*name == K_SPECIAL)
6343 p = concat_str((char_u *)"<SNR>", name + 3);
6344 else
6345 p = name;
6346 EMSG2(_(msg), p);
6347 if (p != name)
6348 vim_free(p);
6275 } 6349 }
6276 6350
6277 /********************************************* 6351 /*********************************************
6278 * Implementation of the built-in functions 6352 * Implementation of the built-in functions
6279 */ 6353 */
6904 static void 6978 static void
6905 f_copy(argvars, rettv) 6979 f_copy(argvars, rettv)
6906 typval_T *argvars; 6980 typval_T *argvars;
6907 typval_T *rettv; 6981 typval_T *rettv;
6908 { 6982 {
6909 item_copy(&argvars[0], rettv, FALSE); 6983 item_copy(&argvars[0], rettv, FALSE, 0);
6910 } 6984 }
6911 6985
6912 /* 6986 /*
6913 * "count()" function 6987 * "count()" function
6914 */ 6988 */
7050 static void 7124 static void
7051 f_deepcopy(argvars, rettv) 7125 f_deepcopy(argvars, rettv)
7052 typval_T *argvars; 7126 typval_T *argvars;
7053 typval_T *rettv; 7127 typval_T *rettv;
7054 { 7128 {
7055 item_copy(&argvars[0], rettv, TRUE); 7129 static int copyID = 0;
7130 int noref = 0;
7131
7132 if (argvars[1].v_type != VAR_UNKNOWN)
7133 noref = get_tv_number(&argvars[1]);
7134 if (noref < 0 || noref > 1)
7135 EMSG(_(e_invarg));
7136 else
7137 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
7056 } 7138 }
7057 7139
7058 /* 7140 /*
7059 * "delete()" function 7141 * "delete()" function
7060 */ 7142 */
10198 char_u patbuf[NUMBUFLEN]; 10280 char_u patbuf[NUMBUFLEN];
10199 char_u strbuf[NUMBUFLEN]; 10281 char_u strbuf[NUMBUFLEN];
10200 char_u *save_cpo; 10282 char_u *save_cpo;
10201 long start = 0; 10283 long start = 0;
10202 long nth = 1; 10284 long nth = 1;
10203 int match; 10285 int match = 0;
10204 list_T *l = NULL; 10286 list_T *l = NULL;
10205 listitem_T *li = NULL; 10287 listitem_T *li = NULL;
10206 long idx = 0; 10288 long idx = 0;
10207 char_u *tofree = NULL; 10289 char_u *tofree = NULL;
10208 10290
10273 match = FALSE; 10355 match = FALSE;
10274 break; 10356 break;
10275 } 10357 }
10276 vim_free(tofree); 10358 vim_free(tofree);
10277 str = echo_string(&li->li_tv, &tofree, strbuf); 10359 str = echo_string(&li->li_tv, &tofree, strbuf);
10360 if (str == NULL)
10361 break;
10278 } 10362 }
10279 10363
10280 match = vim_regexec_nl(&regmatch, str, (colnr_T)0); 10364 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
10281 10365
10282 if (match && --nth <= 0) 10366 if (match && --nth <= 0)
11392 EMSG2(_(e_listarg), "reverse()"); 11476 EMSG2(_(e_listarg), "reverse()");
11393 else if ((l = argvars[0].vval.v_list) != NULL 11477 else if ((l = argvars[0].vval.v_list) != NULL
11394 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()")) 11478 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
11395 { 11479 {
11396 li = l->lv_last; 11480 li = l->lv_last;
11397 l->lv_first = l->lv_last = li; 11481 l->lv_first = l->lv_last = NULL;
11398 l->lv_len = 0; 11482 l->lv_len = 0;
11399 while (li != NULL) 11483 while (li != NULL)
11400 { 11484 {
11401 ni = li->li_prev; 11485 ni = li->li_prev;
11402 list_append(l, li); 11486 list_append(l, li);
14336 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name); 14420 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
14337 return; 14421 return;
14338 } 14422 }
14339 if (function_exists(name)) 14423 if (function_exists(name))
14340 { 14424 {
14341 EMSG2(_("705: Variable name conflicts with existing function: %s"), name); 14425 EMSG2(_("705: Variable name conflicts with existing function: %s"),
14426 name);
14342 return; 14427 return;
14343 } 14428 }
14344 } 14429 }
14345 14430
14346 ht = find_var_ht(name, &varname); 14431 ht = find_var_ht(name, &varname);
14518 } 14603 }
14519 14604
14520 /* 14605 /*
14521 * Make a copy of an item. 14606 * Make a copy of an item.
14522 * Lists and Dictionaries are also copied. A deep copy if "deep" is set. 14607 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
14523 */ 14608 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
14524 static void 14609 * reference to an already copied list/dict can be used.
14525 item_copy(from, to, deep) 14610 * Returns FAIL or OK.
14611 */
14612 static int
14613 item_copy(from, to, deep, copyID)
14526 typval_T *from; 14614 typval_T *from;
14527 typval_T *to; 14615 typval_T *to;
14528 int deep; 14616 int deep;
14617 int copyID;
14529 { 14618 {
14530 static int recurse = 0; 14619 static int recurse = 0;
14620 int ret = OK;
14531 14621
14532 if (recurse >= DICT_MAXNEST) 14622 if (recurse >= DICT_MAXNEST)
14533 { 14623 {
14534 EMSG(_("E698: variable nested too deep for making a copy")); 14624 EMSG(_("E698: variable nested too deep for making a copy"));
14535 return; 14625 return FAIL;
14536 } 14626 }
14537 ++recurse; 14627 ++recurse;
14538 14628
14539 switch (from->v_type) 14629 switch (from->v_type)
14540 { 14630 {
14544 copy_tv(from, to); 14634 copy_tv(from, to);
14545 break; 14635 break;
14546 case VAR_LIST: 14636 case VAR_LIST:
14547 to->v_type = VAR_LIST; 14637 to->v_type = VAR_LIST;
14548 to->v_lock = 0; 14638 to->v_lock = 0;
14549 to->vval.v_list = list_copy(from->vval.v_list, deep); 14639 if (from->vval.v_list == NULL)
14640 to->vval.v_list = NULL;
14641 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
14642 {
14643 /* use the copy made earlier */
14644 to->vval.v_list = from->vval.v_list->lv_copylist;
14645 ++to->vval.v_list->lv_refcount;
14646 }
14647 else
14648 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
14649 if (to->vval.v_list == NULL)
14650 ret = FAIL;
14550 break; 14651 break;
14551 case VAR_DICT: 14652 case VAR_DICT:
14552 to->v_type = VAR_DICT; 14653 to->v_type = VAR_DICT;
14553 to->v_lock = 0; 14654 to->v_lock = 0;
14554 to->vval.v_dict = dict_copy(from->vval.v_dict, deep); 14655 if (from->vval.v_dict == NULL)
14656 to->vval.v_dict = NULL;
14657 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
14658 {
14659 /* use the copy made earlier */
14660 to->vval.v_dict = from->vval.v_dict->dv_copydict;
14661 ++to->vval.v_dict->dv_refcount;
14662 }
14663 else
14664 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
14665 if (to->vval.v_dict == NULL)
14666 ret = FAIL;
14555 break; 14667 break;
14556 default: 14668 default:
14557 EMSG2(_(e_intern2), "item_copy()"); 14669 EMSG2(_(e_intern2), "item_copy()");
14670 ret = FAIL;
14558 } 14671 }
14559 --recurse; 14672 --recurse;
14673 return ret;
14560 } 14674 }
14561 14675
14562 /* 14676 /*
14563 * ":echo expr1 ..." print each argument separated with a space, add a 14677 * ":echo expr1 ..." print each argument separated with a space, add a
14564 * newline at the end. 14678 * newline at the end.
14602 if (eap->cmdidx == CMD_echo) 14716 if (eap->cmdidx == CMD_echo)
14603 msg_start(); 14717 msg_start();
14604 } 14718 }
14605 else if (eap->cmdidx == CMD_echo) 14719 else if (eap->cmdidx == CMD_echo)
14606 msg_puts_attr((char_u *)" ", echo_attr); 14720 msg_puts_attr((char_u *)" ", echo_attr);
14607 for (p = echo_string(&rettv, &tofree, numbuf); 14721 p = echo_string(&rettv, &tofree, numbuf);
14608 *p != NUL && !got_int; ++p) 14722 if (p != NULL)
14609 if (*p == '\n' || *p == '\r' || *p == TAB) 14723 for ( ; *p != NUL && !got_int; ++p)
14610 { 14724 {
14611 if (*p != TAB && needclr) 14725 if (*p == '\n' || *p == '\r' || *p == TAB)
14612 { 14726 {
14613 /* remove any text still there from the command */ 14727 if (*p != TAB && needclr)
14614 msg_clr_eos(); 14728 {
14615 needclr = FALSE; 14729 /* remove any text still there from the command */
14616 } 14730 msg_clr_eos();
14617 msg_putchar_attr(*p, echo_attr); 14731 needclr = FALSE;
14618 } 14732 }
14619 else 14733 msg_putchar_attr(*p, echo_attr);
14620 {
14621 #ifdef FEAT_MBYTE
14622 if (has_mbyte)
14623 {
14624 int i = (*mb_ptr2len_check)(p);
14625
14626 (void)msg_outtrans_len_attr(p, i, echo_attr);
14627 p += i - 1;
14628 } 14734 }
14629 else 14735 else
14630 #endif 14736 {
14631 (void)msg_outtrans_len_attr(p, 1, echo_attr); 14737 #ifdef FEAT_MBYTE
14738 if (has_mbyte)
14739 {
14740 int i = (*mb_ptr2len_check)(p);
14741
14742 (void)msg_outtrans_len_attr(p, i, echo_attr);
14743 p += i - 1;
14744 }
14745 else
14746 #endif
14747 (void)msg_outtrans_len_attr(p, 1, echo_attr);
14748 }
14632 } 14749 }
14633 vim_free(tofree); 14750 vim_free(tofree);
14634 } 14751 }
14635 clear_tv(&rettv); 14752 clear_tv(&rettv);
14636 arg = skipwhite(arg); 14753 arg = skipwhite(arg);
14904 msg_putchar('\n'); 15021 msg_putchar('\n');
14905 msg_puts((char_u *)" endfunction"); 15022 msg_puts((char_u *)" endfunction");
14906 } 15023 }
14907 } 15024 }
14908 else 15025 else
14909 EMSG2(_("E123: Undefined function: %s"), name); 15026 emsg_funcname("E123: Undefined function: %s", name);
14910 } 15027 }
14911 goto ret_free; 15028 goto ret_free;
14912 } 15029 }
14913 15030
14914 /* 15031 /*
15017 if (!eap->skip && !eap->forceit) 15134 if (!eap->skip && !eap->forceit)
15018 { 15135 {
15019 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL) 15136 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15020 EMSG(_(e_funcdict)); 15137 EMSG(_(e_funcdict));
15021 else if (name != NULL && find_func(name) != NULL) 15138 else if (name != NULL && find_func(name) != NULL)
15022 EMSG2(_(e_funcexts), name); 15139 emsg_funcname(e_funcexts, name);
15023 } 15140 }
15024 15141
15025 msg_putchar('\n'); /* don't overwrite the function name */ 15142 msg_putchar('\n'); /* don't overwrite the function name */
15026 cmdline_row = msg_row; 15143 cmdline_row = msg_row;
15027 } 15144 }
15144 if (fudi.fd_dict == NULL) 15261 if (fudi.fd_dict == NULL)
15145 { 15262 {
15146 v = find_var(name, NULL); 15263 v = find_var(name, NULL);
15147 if (v != NULL && v->di_tv.v_type == VAR_FUNC) 15264 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
15148 { 15265 {
15149 EMSG2(_("E707: Function name conflicts with variable: %s"), name); 15266 emsg_funcname("E707: Function name conflicts with variable: %s",
15267 name);
15150 goto erret; 15268 goto erret;
15151 } 15269 }
15152 15270
15153 fp = find_func(name); 15271 fp = find_func(name);
15154 if (fp != NULL) 15272 if (fp != NULL)
15155 { 15273 {
15156 if (!eap->forceit) 15274 if (!eap->forceit)
15157 { 15275 {
15158 EMSG2(_(e_funcexts), name); 15276 emsg_funcname(e_funcexts, name);
15159 goto erret; 15277 goto erret;
15160 } 15278 }
15161 if (fp->calls > 0) 15279 if (fp->calls > 0)
15162 { 15280 {
15163 EMSG2(_("E127: Cannot redefine function %s: It is in use"), 15281 emsg_funcname("E127: Cannot redefine function %s: It is in use",
15164 name); 15282 name);
15165 goto erret; 15283 goto erret;
15166 } 15284 }
15167 /* redefine existing function */ 15285 /* redefine existing function */
15168 ga_clear_strings(&(fp->args)); 15286 ga_clear_strings(&(fp->args));
16190 */ 16308 */
16191 char_u * 16309 char_u *
16192 get_return_cmd(rettv) 16310 get_return_cmd(rettv)
16193 void *rettv; 16311 void *rettv;
16194 { 16312 {
16195 char_u *s; 16313 char_u *s = NULL;
16196 char_u *tofree = NULL; 16314 char_u *tofree = NULL;
16197 char_u numbuf[NUMBUFLEN]; 16315 char_u numbuf[NUMBUFLEN];
16198 16316
16199 if (rettv == NULL) 16317 if (rettv != NULL)
16318 s = echo_string((typval_T *)rettv, &tofree, numbuf);
16319 if (s == NULL)
16200 s = (char_u *)""; 16320 s = (char_u *)"";
16201 else
16202 s = echo_string((typval_T *)rettv, &tofree, numbuf);
16203 16321
16204 STRCPY(IObuff, ":return "); 16322 STRCPY(IObuff, ":return ");
16205 STRNCPY(IObuff + 8, s, IOSIZE - 8); 16323 STRNCPY(IObuff + 8, s, IOSIZE - 8);
16206 if (STRLEN(s) + 8 >= IOSIZE) 16324 if (STRLEN(s) + 8 >= IOSIZE)
16207 STRCPY(IObuff + IOSIZE - 4, "..."); 16325 STRCPY(IObuff + IOSIZE - 4, "...");
16366 { 16484 {
16367 hashitem_T *hi; 16485 hashitem_T *hi;
16368 dictitem_T *this_var; 16486 dictitem_T *this_var;
16369 int todo; 16487 int todo;
16370 char *s; 16488 char *s;
16489 char_u *p;
16371 char_u *tofree; 16490 char_u *tofree;
16372 char_u numbuf[NUMBUFLEN]; 16491 char_u numbuf[NUMBUFLEN];
16373 16492
16374 if (find_viminfo_parameter('!') == NULL) 16493 if (find_viminfo_parameter('!') == NULL)
16375 return; 16494 return;
16390 case VAR_STRING: s = "STR"; break; 16509 case VAR_STRING: s = "STR"; break;
16391 case VAR_NUMBER: s = "NUM"; break; 16510 case VAR_NUMBER: s = "NUM"; break;
16392 default: continue; 16511 default: continue;
16393 } 16512 }
16394 fprintf(fp, "!%s\t%s\t", this_var->di_key, s); 16513 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
16395 viminfo_writestring(fp, echo_string(&this_var->di_tv, 16514 p = echo_string(&this_var->di_tv, &tofree, numbuf);
16396 &tofree, numbuf)); 16515 if (p != NULL)
16516 viminfo_writestring(fp, p);
16397 vim_free(tofree); 16517 vim_free(tofree);
16398 } 16518 }
16399 } 16519 }
16400 } 16520 }
16401 } 16521 }