comparison src/eval.c @ 117:d4ea645c7748 v7.0041

updated for version 7.0041
author vimboss
date Mon, 17 Jan 2005 22:23:00 +0000
parents 3b1d692e5a2c
children 86d71ae0c85a
comparison
equal deleted inserted replaced
116:973ccf576e2c 117:d4ea645c7748
168 listvar *ll_list; /* The list or NULL. */ 168 listvar *ll_list; /* The list or NULL. */
169 int ll_range; /* TRUE when a [i:j] range was used */ 169 int ll_range; /* TRUE when a [i:j] range was used */
170 long ll_n1; /* First index for list */ 170 long ll_n1; /* First index for list */
171 long ll_n2; /* Second index for list range */ 171 long ll_n2; /* Second index for list range */
172 int ll_empty2; /* Second index is empty: [i:] */ 172 int ll_empty2; /* Second index is empty: [i:] */
173 dictvar *ll_dict; /* The Dictionary or NULL */
174 dictitem *ll_di; /* The dictitem or NULL */
173 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */ 175 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
174 dictitem *ll_di; /* The dictitem or NULL */ 176 dictitem **ll_pdi; /* di_next field pointing to found dictitem */
175 dictitem **ll_pdi; /* field that points to found dictitem */
176 } lval; 177 } lval;
177 178
178 179
179 static char *e_letunexp = N_("E18: Unexpected characters in :let"); 180 static char *e_letunexp = N_("E18: Unexpected characters in :let");
180 static char *e_listidx = N_("E684: list index out of range: %ld"); 181 static char *e_listidx = N_("E684: list index out of range: %ld");
181 static char *e_undefvar = N_("E121: Undefined variable: %s"); 182 static char *e_undefvar = N_("E121: Undefined variable: %s");
182 static char *e_missbrac = N_("E111: Missing ']'"); 183 static char *e_missbrac = N_("E111: Missing ']'");
183 static char *e_intern2 = N_("E685: Internal error: %s"); 184 static char *e_intern2 = N_("E685: Internal error: %s");
184 static char *e_listarg = N_("E686: Argument of %s must be a List"); 185 static char *e_listarg = N_("E686: Argument of %s must be a List");
185 static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary"); 186 static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
186 static char *e_emptykey = N_("E999: Empty key in Dictionary"); 187 static char *e_emptykey = N_("E713: Empty key in Dictionary");
187 static char *e_listreq = N_("E999: List required"); 188 static char *e_listreq = N_("E714: List required");
188 static char *e_dictreq = N_("E999: Dictionary required"); 189 static char *e_dictreq = N_("E715: Dictionary required");
189 static char *e_toomanyarg = N_("E118: Too many arguments for function: %s"); 190 static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
190 static char *e_dictkey = N_("E999: key not found in Dictionary: %s"); 191 static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
192 static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
193 static char *e_funcdict = N_("E717: Dictionary entry already exists");
194 static char *e_funcref = N_("E718: Funcref required");
195 static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
196 static char *e_letwrong = N_("E734: Wrong variable type for %s=");
191 197
192 /* 198 /*
193 * All user-defined global variables are stored in "variables". 199 * All user-defined global variables are stored in "variables".
194 */ 200 */
195 garray_T variables = {0, 0, sizeof(var), 4, NULL}; 201 garray_T variables = {0, 0, sizeof(var), 4, NULL};
205 #define VAR_GAP_ENTRY(idx, gap) (((VAR)((gap)->ga_data))[idx]) 211 #define VAR_GAP_ENTRY(idx, gap) (((VAR)((gap)->ga_data))[idx])
206 #define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx]) 212 #define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
207 #define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx]) 213 #define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
208 214
209 static int echo_attr = 0; /* attributes used for ":echo" */ 215 static int echo_attr = 0; /* attributes used for ":echo" */
216
217 /* Values for trans_function_name() argument: */
218 #define TFN_INT 1 /* internal function name OK */
219 #define TFN_QUIET 2 /* no error messages */
210 220
211 /* 221 /*
212 * Structure to hold info for a user function. 222 * Structure to hold info for a user function.
213 */ 223 */
214 typedef struct ufunc ufunc_T; 224 typedef struct ufunc ufunc_T;
223 int calls; /* nr of active calls */ 233 int calls; /* nr of active calls */
224 garray_T args; /* arguments */ 234 garray_T args; /* arguments */
225 garray_T lines; /* function lines */ 235 garray_T lines; /* function lines */
226 scid_T script_ID; /* ID of script where function was defined, 236 scid_T script_ID; /* ID of script where function was defined,
227 used for s: variables */ 237 used for s: variables */
238 int refcount; /* for numbered function: reference count */
228 }; 239 };
229 240
230 /* function flags */ 241 /* function flags */
231 #define FC_ABORT 1 /* abort function on error */ 242 #define FC_ABORT 1 /* abort function on error */
232 #define FC_RANGE 2 /* function accepts range */ 243 #define FC_RANGE 2 /* function accepts range */
269 listwatch fi_lw; /* keep an eye on the item used. */ 280 listwatch fi_lw; /* keep an eye on the item used. */
270 listvar *fi_list; /* list being used */ 281 listvar *fi_list; /* list being used */
271 } forinfo; 282 } forinfo;
272 283
273 /* 284 /*
285 * Struct used by trans_function_name()
286 */
287 typedef struct
288 {
289 dictvar *fd_dict; /* Dictionary used */
290 char_u *fd_newkey; /* new key in "dict" */
291 dictitem *fd_di; /* Dictionary item used */
292 dictitem **fd_pdi; /* field that points to "fd_di" */
293 } funcdict;
294
295 /*
274 * Return the name of the executed function. 296 * Return the name of the executed function.
275 */ 297 */
276 char_u * 298 char_u *
277 func_name(cookie) 299 func_name(cookie)
278 void *cookie; 300 void *cookie;
343 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */ 365 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
344 } vimvars[VV_LEN] = 366 } vimvars[VV_LEN] =
345 { 367 {
346 /* 368 /*
347 * The order here must match the VV_ defines in vim.h! 369 * The order here must match the VV_ defines in vim.h!
370 * Initializing a union does not work, leave tv.vval empty to get zero's.
348 */ 371 */
349 {VV_NAME("count"), {VAR_NUMBER, {NULL}}, VV_COMPAT+VV_RO}, 372 {VV_NAME("count"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
350 {VV_NAME("count1"), {VAR_NUMBER, {NULL}}, VV_RO}, 373 {VV_NAME("count1"), {VAR_NUMBER}, VV_RO},
351 {VV_NAME("prevcount"), {VAR_NUMBER, {NULL}}, VV_RO}, 374 {VV_NAME("prevcount"), {VAR_NUMBER}, VV_RO},
352 {VV_NAME("errmsg"), {VAR_STRING, {NULL}}, VV_COMPAT}, 375 {VV_NAME("errmsg"), {VAR_STRING}, VV_COMPAT},
353 {VV_NAME("warningmsg"), {VAR_STRING, {NULL}}, 0}, 376 {VV_NAME("warningmsg"), {VAR_STRING}, 0},
354 {VV_NAME("statusmsg"), {VAR_STRING, {NULL}}, 0}, 377 {VV_NAME("statusmsg"), {VAR_STRING}, 0},
355 {VV_NAME("shell_error"), {VAR_NUMBER, {NULL}}, VV_COMPAT+VV_RO}, 378 {VV_NAME("shell_error"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
356 {VV_NAME("this_session"), {VAR_STRING, {NULL}}, VV_COMPAT}, 379 {VV_NAME("this_session"), {VAR_STRING}, VV_COMPAT},
357 {VV_NAME("version"), {VAR_NUMBER, {VIM_VERSION_100}}, VV_COMPAT+VV_RO}, 380 {VV_NAME("version"), {VAR_NUMBER}, VV_COMPAT+VV_RO},
358 {VV_NAME("lnum"), {VAR_NUMBER, {NULL}}, VV_RO_SBX}, 381 {VV_NAME("lnum"), {VAR_NUMBER}, VV_RO_SBX},
359 {VV_NAME("termresponse"), {VAR_STRING, {NULL}}, VV_RO}, 382 {VV_NAME("termresponse"), {VAR_STRING}, VV_RO},
360 {VV_NAME("fname"), {VAR_STRING, {NULL}}, VV_RO}, 383 {VV_NAME("fname"), {VAR_STRING}, VV_RO},
361 {VV_NAME("lang"), {VAR_STRING, {NULL}}, VV_RO}, 384 {VV_NAME("lang"), {VAR_STRING}, VV_RO},
362 {VV_NAME("lc_time"), {VAR_STRING, {NULL}}, VV_RO}, 385 {VV_NAME("lc_time"), {VAR_STRING}, VV_RO},
363 {VV_NAME("ctype"), {VAR_STRING, {NULL}}, VV_RO}, 386 {VV_NAME("ctype"), {VAR_STRING}, VV_RO},
364 {VV_NAME("charconvert_from"), {VAR_STRING, {NULL}}, VV_RO}, 387 {VV_NAME("charconvert_from"), {VAR_STRING}, VV_RO},
365 {VV_NAME("charconvert_to"), {VAR_STRING, {NULL}}, VV_RO}, 388 {VV_NAME("charconvert_to"), {VAR_STRING}, VV_RO},
366 {VV_NAME("fname_in"), {VAR_STRING, {NULL}}, VV_RO}, 389 {VV_NAME("fname_in"), {VAR_STRING}, VV_RO},
367 {VV_NAME("fname_out"), {VAR_STRING, {NULL}}, VV_RO}, 390 {VV_NAME("fname_out"), {VAR_STRING}, VV_RO},
368 {VV_NAME("fname_new"), {VAR_STRING, {NULL}}, VV_RO}, 391 {VV_NAME("fname_new"), {VAR_STRING}, VV_RO},
369 {VV_NAME("fname_diff"), {VAR_STRING, {NULL}}, VV_RO}, 392 {VV_NAME("fname_diff"), {VAR_STRING}, VV_RO},
370 {VV_NAME("cmdarg"), {VAR_STRING, {NULL}}, VV_RO}, 393 {VV_NAME("cmdarg"), {VAR_STRING}, VV_RO},
371 {VV_NAME("foldstart"), {VAR_NUMBER, {NULL}}, VV_RO_SBX}, 394 {VV_NAME("foldstart"), {VAR_NUMBER}, VV_RO_SBX},
372 {VV_NAME("foldend"), {VAR_NUMBER, {NULL}}, VV_RO_SBX}, 395 {VV_NAME("foldend"), {VAR_NUMBER}, VV_RO_SBX},
373 {VV_NAME("folddashes"), {VAR_STRING, {NULL}}, VV_RO_SBX}, 396 {VV_NAME("folddashes"), {VAR_STRING}, VV_RO_SBX},
374 {VV_NAME("foldlevel"), {VAR_NUMBER, {NULL}}, VV_RO_SBX}, 397 {VV_NAME("foldlevel"), {VAR_NUMBER}, VV_RO_SBX},
375 {VV_NAME("progname"), {VAR_STRING, {NULL}}, VV_RO}, 398 {VV_NAME("progname"), {VAR_STRING}, VV_RO},
376 {VV_NAME("servername"), {VAR_STRING, {NULL}}, VV_RO}, 399 {VV_NAME("servername"), {VAR_STRING}, VV_RO},
377 {VV_NAME("dying"), {VAR_NUMBER, {NULL}}, VV_RO}, 400 {VV_NAME("dying"), {VAR_NUMBER}, VV_RO},
378 {VV_NAME("exception"), {VAR_STRING, {NULL}}, VV_RO}, 401 {VV_NAME("exception"), {VAR_STRING}, VV_RO},
379 {VV_NAME("throwpoint"), {VAR_STRING, {NULL}}, VV_RO}, 402 {VV_NAME("throwpoint"), {VAR_STRING}, VV_RO},
380 {VV_NAME("register"), {VAR_STRING, {NULL}}, VV_RO}, 403 {VV_NAME("register"), {VAR_STRING}, VV_RO},
381 {VV_NAME("cmdbang"), {VAR_NUMBER, {NULL}}, VV_RO}, 404 {VV_NAME("cmdbang"), {VAR_NUMBER}, VV_RO},
382 {VV_NAME("insertmode"), {VAR_STRING, {NULL}}, VV_RO}, 405 {VV_NAME("insertmode"), {VAR_STRING}, VV_RO},
383 {VV_NAME("val"), {VAR_UNKNOWN, {NULL}}, VV_RO}, 406 {VV_NAME("val"), {VAR_UNKNOWN}, VV_RO},
384 {VV_NAME("key"), {VAR_UNKNOWN, {NULL}}, VV_RO}, 407 {VV_NAME("key"), {VAR_UNKNOWN}, VV_RO},
385 }; 408 };
386 409
387 /* shorthand */ 410 /* shorthand */
388 #define vv_nr tv.vval.v_number 411 #define vv_nr tv.vval.v_number
389 #define vv_str tv.vval.v_string 412 #define vv_str tv.vval.v_string
407 static listitem *listitem_alloc __ARGS((void)); 430 static listitem *listitem_alloc __ARGS((void));
408 static void listitem_free __ARGS((listitem *item)); 431 static void listitem_free __ARGS((listitem *item));
409 static void listitem_remove __ARGS((listvar *l, listitem *item)); 432 static void listitem_remove __ARGS((listvar *l, listitem *item));
410 static long list_len __ARGS((listvar *l)); 433 static long list_len __ARGS((listvar *l));
411 static int list_equal __ARGS((listvar *l1, listvar *l2, int ic)); 434 static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
435 static int dict_equal __ARGS((dictvar *d1, dictvar *d2, int ic));
412 static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic)); 436 static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
413 static int string_isa_number __ARGS((char_u *s)); 437 static int string_isa_number __ARGS((char_u *s));
414 static listitem *list_find __ARGS((listvar *l, long n)); 438 static listitem *list_find __ARGS((listvar *l, long n));
415 static long list_idx_of_item __ARGS((listvar *l, listitem *item)); 439 static long list_idx_of_item __ARGS((listvar *l, listitem *item));
416 static listitem *list_find_ext __ARGS((listvar *l, long *ip)); 440 static listitem *list_find_ext __ARGS((listvar *l, long *ip));
429 static void dict_free __ARGS((dictvar *d)); 453 static void dict_free __ARGS((dictvar *d));
430 static dictitem *dictitem_alloc __ARGS((void)); 454 static dictitem *dictitem_alloc __ARGS((void));
431 static dictitem *dictitem_copy __ARGS((dictitem *org)); 455 static dictitem *dictitem_copy __ARGS((dictitem *org));
432 static void dictitem_free __ARGS((dictitem *item)); 456 static void dictitem_free __ARGS((dictitem *item));
433 static void dict_add __ARGS((dictvar *d, dictitem *item)); 457 static void dict_add __ARGS((dictvar *d, dictitem *item));
458 static long dict_len __ARGS((dictvar *d));
434 static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len, dictitem ***pdi)); 459 static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len, dictitem ***pdi));
435 static char_u *dict2string __ARGS((typeval *tv)); 460 static char_u *dict2string __ARGS((typeval *tv));
436 static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate)); 461 static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
437 462
438 static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf)); 463 static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
638 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string)); 663 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
639 static void set_var __ARGS((char_u *name, typeval *varp, int copy)); 664 static void set_var __ARGS((char_u *name, typeval *varp, int copy));
640 static void copy_tv __ARGS((typeval *from, typeval *to)); 665 static void copy_tv __ARGS((typeval *from, typeval *to));
641 static void item_copy __ARGS((typeval *from, typeval *to, int deep)); 666 static void item_copy __ARGS((typeval *from, typeval *to, int deep));
642 static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags)); 667 static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
643 static char_u *trans_function_name __ARGS((char_u **pp, int skip, int exists)); 668 static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict *fd));
644 static int eval_fname_script __ARGS((char_u *p)); 669 static int eval_fname_script __ARGS((char_u *p));
645 static int eval_fname_sid __ARGS((char_u *p)); 670 static int eval_fname_sid __ARGS((char_u *p));
646 static void list_func_head __ARGS((ufunc_T *fp, int indent)); 671 static void list_func_head __ARGS((ufunc_T *fp, int indent));
647 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); 672 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
648 static ufunc_T *find_func __ARGS((char_u *name)); 673 static ufunc_T *find_func __ARGS((char_u *name));
649 static int function_exists __ARGS((char_u *name)); 674 static int function_exists __ARGS((char_u *name));
675 static void func_free __ARGS((ufunc_T *fp));
676 static void func_unref __ARGS((char_u *name));
677 static void func_ref __ARGS((char_u *name));
650 static void call_user_func __ARGS((ufunc_T *fp, int argcount, typeval *argvars, typeval *rettv, linenr_T firstline, linenr_T lastline, dictvar *selfdict)); 678 static void call_user_func __ARGS((ufunc_T *fp, int argcount, typeval *argvars, typeval *rettv, linenr_T firstline, linenr_T lastline, dictvar *selfdict));
651 679
652 #define get_var_string(p) get_tv_string(&(p)->tv) 680 #define get_var_string(p) get_tv_string(&(p)->tv)
653 #define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b)) 681 #define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
654 #define get_var_number(p) get_tv_number(&((p)->tv)) 682 #define get_var_number(p) get_tv_number(&((p)->tv))
658 static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars)); 686 static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
659 static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon)); 687 static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
660 static char_u *skip_var_one __ARGS((char_u *arg)); 688 static char_u *skip_var_one __ARGS((char_u *arg));
661 static void list_all_vars __ARGS((void)); 689 static void list_all_vars __ARGS((void));
662 static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg)); 690 static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
663 static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars)); 691 static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars, char_u *op));
664 static int check_changedtick __ARGS((char_u *arg)); 692 static int check_changedtick __ARGS((char_u *arg));
665 static char_u *get_lval __ARGS((char_u *name, typeval *rettv, lval *lp, int unlet, int skip, int quiet)); 693 static char_u *get_lval __ARGS((char_u *name, typeval *rettv, lval *lp, int unlet, int skip, int quiet));
666 static void clear_lval __ARGS((lval *lp)); 694 static void clear_lval __ARGS((lval *lp));
667 static void set_var_lval __ARGS((lval *lp, char_u *endp, typeval *rettv, int copy)); 695 static void set_var_lval __ARGS((lval *lp, char_u *endp, typeval *rettv, int copy, char_u *op));
696 static int tv_op __ARGS((typeval *tv1, typeval *tv2, char_u *op));
668 static void list_add_watch __ARGS((listvar *l, listwatch *lw)); 697 static void list_add_watch __ARGS((listvar *l, listwatch *lw));
669 static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem)); 698 static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem));
670 static void list_fix_watch __ARGS((listvar *l, listitem *item)); 699 static void list_fix_watch __ARGS((listvar *l, listitem *item));
671 static int do_unlet_var __ARGS((lval *lp, char_u *name_end, int forceit)); 700 static int do_unlet_var __ARGS((lval *lp, char_u *name_end, int forceit));
672 701
1168 1197
1169 /* 1198 /*
1170 * ":let" list all variable values 1199 * ":let" list all variable values
1171 * ":let var1 var2" list variable values 1200 * ":let var1 var2" list variable values
1172 * ":let var = expr" assignment command. 1201 * ":let var = expr" assignment command.
1202 * ":let var += expr" assignment command.
1203 * ":let var -= expr" assignment command.
1204 * ":let var .= expr" assignment command.
1173 * ":let [var1, var2] = expr" unpack list. 1205 * ":let [var1, var2] = expr" unpack list.
1174 */ 1206 */
1175 void 1207 void
1176 ex_let(eap) 1208 ex_let(eap)
1177 exarg_T *eap; 1209 exarg_T *eap;
1180 char_u *expr = NULL; 1212 char_u *expr = NULL;
1181 typeval rettv; 1213 typeval rettv;
1182 int i; 1214 int i;
1183 int var_count = 0; 1215 int var_count = 0;
1184 int semicolon = 0; 1216 int semicolon = 0;
1217 char_u op[2];
1185 1218
1186 expr = skip_var_list(arg, &var_count, &semicolon); 1219 expr = skip_var_list(arg, &var_count, &semicolon);
1187 if (expr == NULL) 1220 if (expr == NULL)
1188 return; 1221 return;
1189 expr = vim_strchr(expr, '='); 1222 expr = vim_strchr(expr, '=');
1199 list_all_vars(); 1232 list_all_vars();
1200 eap->nextcmd = check_nextcmd(arg); 1233 eap->nextcmd = check_nextcmd(arg);
1201 } 1234 }
1202 else 1235 else
1203 { 1236 {
1237 op[0] = '=';
1238 op[1] = NUL;
1239 if (expr > arg)
1240 {
1241 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1242 op[0] = expr[-1]; /* +=, -= or .= */
1243 }
1204 expr = skipwhite(expr + 1); 1244 expr = skipwhite(expr + 1);
1205 1245
1206 if (eap->skip) 1246 if (eap->skip)
1207 ++emsg_skip; 1247 ++emsg_skip;
1208 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip); 1248 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
1213 --emsg_skip; 1253 --emsg_skip;
1214 } 1254 }
1215 else if (i != FAIL) 1255 else if (i != FAIL)
1216 { 1256 {
1217 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count, 1257 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1218 (char_u *)"="); 1258 op);
1219 clear_tv(&rettv); 1259 clear_tv(&rettv);
1220 } 1260 }
1221 } 1261 }
1222 } 1262 }
1223 1263
1224 /* 1264 /*
1225 * Assign the typevalue "tv" to the variable or variables at "arg_start". 1265 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1226 * Handles both "var" with any type and "[var, var; var]" with a list type. 1266 * Handles both "var" with any type and "[var, var; var]" with a list type.
1267 * When "nextchars" is not NULL it points to a string with characters that
1268 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1269 * or concatenate.
1227 * Returns OK or FAIL; 1270 * Returns OK or FAIL;
1228 */ 1271 */
1229 static int 1272 static int
1230 ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars) 1273 ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1231 char_u *arg_start; 1274 char_u *arg_start;
1232 typeval *tv; 1275 typeval *tv;
1233 int copy; /* copy values from "tv", don't move */ 1276 int copy; /* copy values from "tv", don't move */
1234 int semicolon; /* from skip_var_list() */ 1277 int semicolon; /* from skip_var_list() */
1235 int var_count; /* from skip_var_list() */ 1278 int var_count; /* from skip_var_list() */
1236 char_u *nextchars; /* characters that must follow or NULL */ 1279 char_u *nextchars;
1237 { 1280 {
1238 char_u *arg = arg_start; 1281 char_u *arg = arg_start;
1239 listvar *l; 1282 listvar *l;
1240 int i; 1283 int i;
1241 listitem *item; 1284 listitem *item;
1244 if (*arg != '[') 1287 if (*arg != '[')
1245 { 1288 {
1246 /* 1289 /*
1247 * ":let var = expr" or ":for var in list" 1290 * ":let var = expr" or ":for var in list"
1248 */ 1291 */
1249 if (ex_let_one(arg, tv, copy, nextchars) == NULL) 1292 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
1250 return FAIL; 1293 return FAIL;
1251 return OK; 1294 return OK;
1252 } 1295 }
1253 1296
1254 /* 1297 /*
1275 1318
1276 item = l->lv_first; 1319 item = l->lv_first;
1277 while (*arg != ']') 1320 while (*arg != ']')
1278 { 1321 {
1279 arg = skipwhite(arg + 1); 1322 arg = skipwhite(arg + 1);
1280 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]"); 1323 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
1281 item = item->li_next; 1324 item = item->li_next;
1282 if (arg == NULL) 1325 if (arg == NULL)
1283 return FAIL; 1326 return FAIL;
1284 1327
1285 arg = skipwhite(arg); 1328 arg = skipwhite(arg);
1298 1341
1299 ltv.v_type = VAR_LIST; 1342 ltv.v_type = VAR_LIST;
1300 ltv.vval.v_list = l; 1343 ltv.vval.v_list = l;
1301 l->lv_refcount = 1; 1344 l->lv_refcount = 1;
1302 1345
1303 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, (char_u *)"]"); 1346 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1347 (char_u *)"]", nextchars);
1304 clear_tv(&ltv); 1348 clear_tv(&ltv);
1305 if (arg == NULL) 1349 if (arg == NULL)
1306 return FAIL; 1350 return FAIL;
1307 break; 1351 break;
1308 } 1352 }
1522 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value. 1566 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1523 * Returns a pointer to the char just after the var name. 1567 * Returns a pointer to the char just after the var name.
1524 * Returns NULL if there is an error. 1568 * Returns NULL if there is an error.
1525 */ 1569 */
1526 static char_u * 1570 static char_u *
1527 ex_let_one(arg, tv, copy, endchars) 1571 ex_let_one(arg, tv, copy, endchars, op)
1528 char_u *arg; /* points to variable name */ 1572 char_u *arg; /* points to variable name */
1529 typeval *tv; /* value to assign to variable */ 1573 typeval *tv; /* value to assign to variable */
1530 int copy; /* copy value from "tv" */ 1574 int copy; /* copy value from "tv" */
1531 char_u *endchars; /* valid chars after variable name or NULL */ 1575 char_u *endchars; /* valid chars after variable name or NULL */
1576 char_u *op; /* "+", "-", "." or NULL*/
1532 { 1577 {
1533 int c1; 1578 int c1;
1534 char_u *name; 1579 char_u *name;
1535 char_u *p; 1580 char_u *p;
1536 char_u *arg_end = NULL; 1581 char_u *arg_end = NULL;
1537 int len; 1582 int len;
1538 int opt_flags; 1583 int opt_flags;
1584 char_u *tofree = NULL;
1539 1585
1540 /* 1586 /*
1541 * ":let $VAR = expr": Set environment variable. 1587 * ":let $VAR = expr": Set environment variable.
1542 */ 1588 */
1543 if (*arg == '$') 1589 if (*arg == '$')
1548 len = get_env_len(&arg); 1594 len = get_env_len(&arg);
1549 if (len == 0) 1595 if (len == 0)
1550 EMSG2(_(e_invarg2), name - 1); 1596 EMSG2(_(e_invarg2), name - 1);
1551 else 1597 else
1552 { 1598 {
1553 if (endchars != NULL 1599 if (op != NULL && (*op == '+' || *op == '-'))
1600 EMSG2(_(e_letwrong), op);
1601 else if (endchars != NULL
1554 && vim_strchr(endchars, *skipwhite(arg)) == NULL) 1602 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1555 EMSG(_(e_letunexp)); 1603 EMSG(_(e_letunexp));
1556 else 1604 else
1557 { 1605 {
1558 c1 = name[len]; 1606 c1 = name[len];
1559 name[len] = NUL; 1607 name[len] = NUL;
1560 p = get_tv_string(tv); 1608 p = get_tv_string(tv);
1561 vim_setenv(name, p); 1609 if (op != NULL && *op == '.')
1610 {
1611 int mustfree = FALSE;
1612 char_u *s = vim_getenv(name, &mustfree);
1613
1614 if (s != NULL)
1615 {
1616 p = tofree = concat_str(s, p);
1617 if (mustfree)
1618 vim_free(s);
1619 }
1620 }
1621 if (p != NULL)
1622 vim_setenv(name, p);
1562 if (STRICMP(name, "HOME") == 0) 1623 if (STRICMP(name, "HOME") == 0)
1563 init_homedir(); 1624 init_homedir();
1564 else if (didset_vim && STRICMP(name, "VIM") == 0) 1625 else if (didset_vim && STRICMP(name, "VIM") == 0)
1565 didset_vim = FALSE; 1626 didset_vim = FALSE;
1566 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0) 1627 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1567 didset_vimruntime = FALSE; 1628 didset_vimruntime = FALSE;
1568 name[len] = c1; 1629 name[len] = c1;
1569 arg_end = arg; 1630 arg_end = arg;
1631 vim_free(tofree);
1570 } 1632 }
1571 } 1633 }
1572 } 1634 }
1573 1635
1574 /* 1636 /*
1583 if (p == NULL || (endchars != NULL 1645 if (p == NULL || (endchars != NULL
1584 && vim_strchr(endchars, *skipwhite(p)) == NULL)) 1646 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1585 EMSG(_(e_letunexp)); 1647 EMSG(_(e_letunexp));
1586 else 1648 else
1587 { 1649 {
1650 long n;
1651 int opt_type;
1652 long numval;
1653 char_u *stringval = NULL;
1654 char_u *s;
1655
1588 c1 = *p; 1656 c1 = *p;
1589 *p = NUL; 1657 *p = NUL;
1590 set_option_value(arg, get_tv_number(tv), 1658
1591 get_tv_string(tv), opt_flags); 1659 n = get_tv_number(tv);
1660 s = get_tv_string(tv);
1661 if (op != NULL && *op != '=')
1662 {
1663 opt_type = get_option_value(arg, &numval,
1664 &stringval, opt_flags);
1665 if ((opt_type == 1 && *op == '.')
1666 || (opt_type == 0 && *op != '.'))
1667 EMSG2(_(e_letwrong), op);
1668 else
1669 {
1670 if (opt_type == 1) /* number */
1671 {
1672 if (*op == '+')
1673 n = numval + n;
1674 else
1675 n = numval - n;
1676 }
1677 else if (opt_type == 0 && stringval != NULL) /* string */
1678 {
1679 s = concat_str(stringval, s);
1680 vim_free(stringval);
1681 stringval = s;
1682 }
1683 }
1684 }
1685 set_option_value(arg, n, s, opt_flags);
1592 *p = c1; 1686 *p = c1;
1593 arg_end = p; 1687 arg_end = p;
1688 vim_free(stringval);
1594 } 1689 }
1595 } 1690 }
1596 1691
1597 /* 1692 /*
1598 * ":let @r = expr": Set register contents. 1693 * ":let @r = expr": Set register contents.
1599 */ 1694 */
1600 else if (*arg == '@') 1695 else if (*arg == '@')
1601 { 1696 {
1602 ++arg; 1697 ++arg;
1603 if (endchars != NULL 1698 if (op != NULL && (*op == '+' || *op == '-'))
1699 EMSG2(_(e_letwrong), op);
1700 else if (endchars != NULL
1604 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL) 1701 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1605 EMSG(_(e_letunexp)); 1702 EMSG(_(e_letunexp));
1606 else 1703 else
1607 { 1704 {
1608 write_reg_contents(*arg == '@' ? '"' : *arg, 1705 char_u *tofree = NULL;
1609 get_tv_string(tv), -1, FALSE); 1706 char_u *s;
1707
1708 p = get_tv_string(tv);
1709 if (op != NULL && *op == '.')
1710 {
1711 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1712 if (s != NULL)
1713 {
1714 p = tofree = concat_str(s, p);
1715 vim_free(s);
1716 }
1717 }
1718 if (p != NULL)
1719 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1610 arg_end = arg + 1; 1720 arg_end = arg + 1;
1721 vim_free(tofree);
1611 } 1722 }
1612 } 1723 }
1613 1724
1614 /* 1725 /*
1615 * ":let var = expr": Set internal variable. 1726 * ":let var = expr": Set internal variable.
1624 { 1735 {
1625 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL) 1736 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1626 EMSG(_(e_letunexp)); 1737 EMSG(_(e_letunexp));
1627 else 1738 else
1628 { 1739 {
1629 set_var_lval(&lv, p, tv, copy); 1740 set_var_lval(&lv, p, tv, copy, op);
1630 arg_end = p; 1741 arg_end = p;
1631 } 1742 }
1632 } 1743 }
1633 clear_lval(&lv); 1744 clear_lval(&lv);
1634 } 1745 }
1794 if (*p == ':') 1905 if (*p == ':')
1795 { 1906 {
1796 if (lp->ll_tv->v_type == VAR_DICT) 1907 if (lp->ll_tv->v_type == VAR_DICT)
1797 { 1908 {
1798 if (!quiet) 1909 if (!quiet)
1799 EMSG(_("E999: Cannot use [:] with a Dictionary")); 1910 EMSG(_(e_dictrange));
1800 if (!empty1) 1911 if (!empty1)
1801 clear_tv(&var1); 1912 clear_tv(&var1);
1802 return NULL; 1913 return NULL;
1803 } 1914 }
1804 if (rettv != NULL && (rettv->v_type != VAR_LIST 1915 if (rettv != NULL && (rettv->v_type != VAR_LIST
1855 EMSG(_(e_emptykey)); 1966 EMSG(_(e_emptykey));
1856 clear_tv(&var1); 1967 clear_tv(&var1);
1857 return NULL; 1968 return NULL;
1858 } 1969 }
1859 } 1970 }
1860 lp->ll_di = dict_find(lp->ll_tv->vval.v_dict, key, len, 1971 lp->ll_list = NULL;
1861 &lp->ll_pdi); 1972 lp->ll_dict = lp->ll_tv->vval.v_dict;
1973 lp->ll_di = dict_find(lp->ll_dict, key, len, &lp->ll_pdi);
1862 if (lp->ll_di == NULL) 1974 if (lp->ll_di == NULL)
1863 { 1975 {
1864 /* Key does not exist in dict: may need toadd it. */ 1976 /* Key does not exist in dict: may need toadd it. */
1865 if (*p == '[' || *p == '.' || unlet) 1977 if (*p == '[' || *p == '.' || unlet)
1866 { 1978 {
1867 if (!quiet) 1979 if (!quiet)
1868 EMSG2(_("E999: Key does not exist in Dictionary: %s"), 1980 EMSG2(_(e_dictkey), key);
1869 key);
1870 if (len == -1) 1981 if (len == -1)
1871 clear_tv(&var1); 1982 clear_tv(&var1);
1872 return NULL; 1983 return NULL;
1873 } 1984 }
1874 if (len == -1) 1985 if (len == -1)
1895 else 2006 else
1896 { 2007 {
1897 lp->ll_n1 = get_tv_number(&var1); 2008 lp->ll_n1 = get_tv_number(&var1);
1898 clear_tv(&var1); 2009 clear_tv(&var1);
1899 } 2010 }
2011 lp->ll_dict = NULL;
1900 lp->ll_list = lp->ll_tv->vval.v_list; 2012 lp->ll_list = lp->ll_tv->vval.v_list;
1901 lp->ll_li = list_find(lp->ll_list, lp->ll_n1); 2013 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1902 if (lp->ll_li == NULL) 2014 if (lp->ll_li == NULL)
1903 { 2015 {
1904 if (!quiet) 2016 if (!quiet)
1960 } 2072 }
1961 2073
1962 /* 2074 /*
1963 * Set a variable that was parsed by get_lval(). 2075 * Set a variable that was parsed by get_lval().
1964 * "endp" points to just after the parsed name. 2076 * "endp" points to just after the parsed name.
1965 */ 2077 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
1966 static void 2078 */
1967 set_var_lval(lp, endp, rettv, copy) 2079 static void
2080 set_var_lval(lp, endp, rettv, copy, op)
1968 lval *lp; 2081 lval *lp;
1969 char_u *endp; 2082 char_u *endp;
1970 typeval *rettv; 2083 typeval *rettv;
1971 int copy; 2084 int copy;
2085 char_u *op;
1972 { 2086 {
1973 int cc; 2087 int cc;
1974 listitem *ni; 2088 listitem *ni;
1975 listitem *ri; 2089 listitem *ri;
1976 dictitem *di; 2090 dictitem *di;
1979 { 2093 {
1980 if (!check_changedtick(lp->ll_name)) 2094 if (!check_changedtick(lp->ll_name))
1981 { 2095 {
1982 cc = *endp; 2096 cc = *endp;
1983 *endp = NUL; 2097 *endp = NUL;
1984 set_var(lp->ll_name, rettv, copy); 2098 if (op != NULL && *op != '=')
2099 {
2100 typeval tv;
2101
2102 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), &tv) == OK)
2103 {
2104 if (tv_op(&tv, rettv, op) == OK)
2105 set_var(lp->ll_name, &tv, FALSE);
2106 clear_tv(&tv);
2107 }
2108 }
2109 else
2110 set_var(lp->ll_name, rettv, copy);
1985 *endp = cc; 2111 *endp = cc;
1986 } 2112 }
1987 } 2113 }
1988 else if (lp->ll_range) 2114 else if (lp->ll_range)
1989 { 2115 {
1990 /* 2116 /*
1991 * Assign the List values to the list items. 2117 * Assign the List values to the list items.
1992 */ 2118 */
1993 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) 2119 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
1994 { 2120 {
1995 clear_tv(&lp->ll_li->li_tv); 2121 if (op != NULL && *op != '=')
1996 copy_tv(&ri->li_tv, &lp->ll_li->li_tv); 2122 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2123 else
2124 {
2125 clear_tv(&lp->ll_li->li_tv);
2126 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2127 }
1997 ri = ri->li_next; 2128 ri = ri->li_next;
1998 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1)) 2129 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1999 break; 2130 break;
2000 if (lp->ll_li->li_next == NULL) 2131 if (lp->ll_li->li_next == NULL)
2001 { 2132 {
2013 lp->ll_li = lp->ll_li->li_next; 2144 lp->ll_li = lp->ll_li->li_next;
2014 ++lp->ll_n1; 2145 ++lp->ll_n1;
2015 } 2146 }
2016 if (ri != NULL) 2147 if (ri != NULL)
2017 EMSG(_("E710: List value has more items than target")); 2148 EMSG(_("E710: List value has more items than target"));
2018 else if (lp->ll_empty2 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL) 2149 else if (lp->ll_empty2
2150 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
2019 : lp->ll_n1 != lp->ll_n2) 2151 : lp->ll_n1 != lp->ll_n2)
2020 EMSG(_("E711: List value has not enough items")); 2152 EMSG(_("E711: List value has not enough items"));
2021 } 2153 }
2022 else 2154 else
2023 { 2155 {
2024 /* 2156 /*
2025 * Assign to a List or Dictionary item. 2157 * Assign to a List or Dictionary item.
2026 */ 2158 */
2027 if (lp->ll_newkey != NULL) 2159 if (lp->ll_newkey != NULL)
2028 { 2160 {
2161 if (op != NULL && *op != '=')
2162 {
2163 EMSG2(_(e_letwrong), op);
2164 return;
2165 }
2166
2029 /* Need to add an item to the Dictionary. */ 2167 /* Need to add an item to the Dictionary. */
2030 di = dictitem_alloc(); 2168 di = dictitem_alloc();
2031 if (di == NULL) 2169 if (di == NULL)
2032 return; 2170 return;
2033 di->di_key = lp->ll_newkey; 2171 di->di_key = lp->ll_newkey;
2034 lp->ll_newkey = NULL; 2172 lp->ll_newkey = NULL;
2035 dict_add(lp->ll_tv->vval.v_dict, di); 2173 dict_add(lp->ll_tv->vval.v_dict, di);
2036 lp->ll_tv = &di->di_tv; 2174 lp->ll_tv = &di->di_tv;
2037 } 2175 }
2176 else if (op != NULL && *op != '=')
2177 {
2178 tv_op(lp->ll_tv, rettv, op);
2179 return;
2180 }
2038 else 2181 else
2039 clear_tv(lp->ll_tv); 2182 clear_tv(lp->ll_tv);
2040 2183
2041 /* 2184 /*
2042 * Assign the value to the variable or list item. 2185 * Assign the value to the variable or list item.
2047 { 2190 {
2048 *lp->ll_tv = *rettv; 2191 *lp->ll_tv = *rettv;
2049 init_tv(rettv); 2192 init_tv(rettv);
2050 } 2193 }
2051 } 2194 }
2195 }
2196
2197 /*
2198 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2199 * Returns OK or FAIL.
2200 */
2201 static int
2202 tv_op(tv1, tv2, op)
2203 typeval *tv1;
2204 typeval *tv2;
2205 char_u *op;
2206 {
2207 long n;
2208 char_u numbuf[NUMBUFLEN];
2209 char_u *s;
2210
2211 /* Can't do anything with a Funcref or a Dict on the right. */
2212 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2213 {
2214 switch (tv1->v_type)
2215 {
2216 case VAR_DICT:
2217 case VAR_FUNC:
2218 break;
2219
2220 case VAR_LIST:
2221 if (*op != '+' || tv2->v_type != VAR_LIST)
2222 break;
2223 /* List += List */
2224 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2225 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2226 return OK;
2227
2228 case VAR_NUMBER:
2229 case VAR_STRING:
2230 if (tv2->v_type == VAR_LIST)
2231 break;
2232 if (*op == '+' || *op == '-')
2233 {
2234 /* nr += nr or nr -= nr*/
2235 n = get_tv_number(tv1);
2236 if (*op == '+')
2237 n += get_tv_number(tv2);
2238 else
2239 n -= get_tv_number(tv2);
2240 clear_tv(tv1);
2241 tv1->v_type = VAR_NUMBER;
2242 tv1->vval.v_number = n;
2243 }
2244 else
2245 {
2246 /* str .= str */
2247 s = get_tv_string(tv1);
2248 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2249 clear_tv(tv1);
2250 tv1->v_type = VAR_STRING;
2251 tv1->vval.v_string = s;
2252 }
2253 return OK;
2254 }
2255 }
2256
2257 EMSG2(_(e_letwrong), op);
2258 return FAIL;
2052 } 2259 }
2053 2260
2054 /* 2261 /*
2055 * Add a watcher to a list. 2262 * Add a watcher to a list.
2056 */ 2263 */
2316 ex_call(eap) 2523 ex_call(eap)
2317 exarg_T *eap; 2524 exarg_T *eap;
2318 { 2525 {
2319 char_u *arg = eap->arg; 2526 char_u *arg = eap->arg;
2320 char_u *startarg; 2527 char_u *startarg;
2321 char_u *alias;
2322 char_u *name; 2528 char_u *name;
2529 char_u *tofree;
2530 int len;
2323 typeval rettv; 2531 typeval rettv;
2324 int len;
2325 linenr_T lnum; 2532 linenr_T lnum;
2326 int doesrange; 2533 int doesrange;
2327 int failed = FALSE; 2534 int failed = FALSE;
2328 2535
2329 name = arg; 2536 tofree = trans_function_name(&arg, eap->skip, TFN_INT, NULL);
2330 len = get_func_len(&arg, &alias, !eap->skip); 2537 if (tofree == NULL)
2331 if (len == 0) 2538 return;
2332 goto end; 2539
2333 if (alias != NULL) 2540 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2334 name = alias; 2541 len = STRLEN(tofree);
2542 name = deref_func_name(tofree, &len);
2335 2543
2336 startarg = arg; 2544 startarg = arg;
2337 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 2545 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
2338 2546
2339 if (*startarg != '(') 2547 if (*startarg != '(')
2361 { 2569 {
2362 curwin->w_cursor.lnum = lnum; 2570 curwin->w_cursor.lnum = lnum;
2363 curwin->w_cursor.col = 0; 2571 curwin->w_cursor.col = 0;
2364 } 2572 }
2365 arg = startarg; 2573 arg = startarg;
2366 if (get_func_tv(name, len, &rettv, &arg, eap->line1, eap->line2, 2574 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
2367 &doesrange, !eap->skip, NULL) == FAIL) 2575 eap->line1, eap->line2, &doesrange, !eap->skip, NULL) == FAIL)
2368 { 2576 {
2369 failed = TRUE; 2577 failed = TRUE;
2370 break; 2578 break;
2371 } 2579 }
2372 clear_tv(&rettv); 2580 clear_tv(&rettv);
2393 else 2601 else
2394 eap->nextcmd = check_nextcmd(arg); 2602 eap->nextcmd = check_nextcmd(arg);
2395 } 2603 }
2396 2604
2397 end: 2605 end:
2398 if (alias != NULL) 2606 vim_free(tofree);
2399 vim_free(alias);
2400 } 2607 }
2401 2608
2402 /* 2609 /*
2403 * ":unlet[!] var1 ... " command. 2610 * ":unlet[!] var1 ... " command.
2404 */ 2611 */
3046 } 3253 }
3047 else 3254 else
3048 { 3255 {
3049 /* Compare two Lists for being equal or unequal. */ 3256 /* Compare two Lists for being equal or unequal. */
3050 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic); 3257 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3258 if (type == TYPE_NEQUAL)
3259 n1 = !n1;
3260 }
3261 }
3262
3263 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3264 {
3265 if (type_is)
3266 {
3267 n1 = (rettv->v_type == var2.v_type
3268 && rettv->vval.v_dict == var2.vval.v_dict);
3269 if (type == TYPE_NEQUAL)
3270 n1 = !n1;
3271 }
3272 else if (rettv->v_type != var2.v_type
3273 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3274 {
3275 if (rettv->v_type != var2.v_type)
3276 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3277 else
3278 EMSG(_("E736: Invalid operation for Dictionary"));
3279 clear_tv(rettv);
3280 clear_tv(&var2);
3281 return FAIL;
3282 }
3283 else
3284 {
3285 /* Compare two Dictionaries for being equal or unequal. */
3286 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3051 if (type == TYPE_NEQUAL) 3287 if (type == TYPE_NEQUAL)
3052 n1 = !n1; 3288 n1 = !n1;
3053 } 3289 }
3054 } 3290 }
3055 3291
3208 */ 3444 */
3209 if (op == '.') 3445 if (op == '.')
3210 { 3446 {
3211 s1 = get_tv_string_buf(rettv, buf1); 3447 s1 = get_tv_string_buf(rettv, buf1);
3212 s2 = get_tv_string_buf(&var2, buf2); 3448 s2 = get_tv_string_buf(&var2, buf2);
3213 op = (int)STRLEN(s1); 3449 p = concat_str(s1, s2);
3214 p = alloc((unsigned)(op + STRLEN(s2) + 1));
3215 if (p != NULL)
3216 {
3217 STRCPY(p, s1);
3218 STRCPY(p + op, s2);
3219 }
3220 clear_tv(rettv); 3450 clear_tv(rettv);
3221 rettv->v_type = VAR_STRING; 3451 rettv->v_type = VAR_STRING;
3222 rettv->vval.v_string = p; 3452 rettv->vval.v_string = p;
3223 } 3453 }
3224 else if (op == '+' && rettv->v_type == VAR_LIST 3454 else if (op == '+' && rettv->v_type == VAR_LIST
3516 ret = FAIL; 3746 ret = FAIL;
3517 } 3747 }
3518 } 3748 }
3519 else if (evaluate) 3749 else if (evaluate)
3520 ret = get_var_tv(s, len, rettv); 3750 ret = get_var_tv(s, len, rettv);
3751 else
3752 ret = OK;
3521 } 3753 }
3522 3754
3523 if (alias != NULL) 3755 if (alias != NULL)
3524 vim_free(alias); 3756 vim_free(alias);
3525 } 3757 }
3780 break; 4012 break;
3781 4013
3782 case VAR_DICT: 4014 case VAR_DICT:
3783 if (range) 4015 if (range)
3784 { 4016 {
3785 EMSG(_("E999: Using range with Dictionary")); 4017 EMSG(_(e_dictrange));
3786 if (len == -1) 4018 if (len == -1)
3787 clear_tv(&var1); 4019 clear_tv(&var1);
3788 return FAIL; 4020 return FAIL;
3789 } 4021 }
3790 { 4022 {
3793 if (len == -1) 4025 if (len == -1)
3794 { 4026 {
3795 key = get_tv_string(&var1); 4027 key = get_tv_string(&var1);
3796 if (*key == NUL) 4028 if (*key == NUL)
3797 { 4029 {
3798 EMSG(_("E999: Empty key for Dictionary")); 4030 EMSG(_(e_emptykey));
3799 clear_tv(&var1); 4031 clear_tv(&var1);
3800 return FAIL; 4032 return FAIL;
3801 } 4033 }
3802 } 4034 }
3803 4035
3804 item = dict_find(rettv->vval.v_dict, key, (int)len, NULL); 4036 item = dict_find(rettv->vval.v_dict, key, (int)len, NULL);
3805 4037
3806 if (item == NULL) 4038 if (item == NULL)
3807 EMSG2(_("E999: Key not found in Dictionary: %s"), key); 4039 EMSG2(_(e_dictkey), key);
3808 if (len == -1) 4040 if (len == -1)
3809 clear_tv(&var1); 4041 clear_tv(&var1);
3810 if (item == NULL) 4042 if (item == NULL)
3811 return FAIL; 4043 return FAIL;
3812 4044
4269 listvar *l2; 4501 listvar *l2;
4270 int ic; /* ignore case for strings */ 4502 int ic; /* ignore case for strings */
4271 { 4503 {
4272 listitem *item1, *item2; 4504 listitem *item1, *item2;
4273 4505
4506 if (list_len(l1) != list_len(l2))
4507 return FALSE;
4508
4274 for (item1 = l1->lv_first, item2 = l2->lv_first; 4509 for (item1 = l1->lv_first, item2 = l2->lv_first;
4275 item1 != NULL && item2 != NULL; 4510 item1 != NULL && item2 != NULL;
4276 item1 = item1->li_next, item2 = item2->li_next) 4511 item1 = item1->li_next, item2 = item2->li_next)
4277 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic)) 4512 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4278 return FALSE; 4513 return FALSE;
4279 return item1 == NULL && item2 == NULL; 4514 return item1 == NULL && item2 == NULL;
4515 }
4516
4517 /*
4518 * Return TRUE when two dictionaries have exactly the same key/values.
4519 */
4520 static int
4521 dict_equal(d1, d2, ic)
4522 dictvar *d1;
4523 dictvar *d2;
4524 int ic; /* ignore case for strings */
4525 {
4526 dictitem *item1, *item2;
4527
4528 if (dict_len(d1) != dict_len(d2))
4529 return FALSE;
4530
4531 for (item1 = d1->dv_first; item1 != NULL; item1 = item1->di_next)
4532 {
4533 item2 = dict_find(d2, item1->di_key, -1, NULL);
4534 if (item2 == NULL)
4535 return FALSE;
4536 if (!tv_equal(&item1->di_tv, &item2->di_tv, ic))
4537 return FALSE;
4538 }
4539 return TRUE;
4280 } 4540 }
4281 4541
4282 /* 4542 /*
4283 * Return TRUE if "tv1" and "tv2" have the same value. 4543 * Return TRUE if "tv1" and "tv2" have the same value.
4284 * Compares the items just like "==" would compare them. 4544 * Compares the items just like "==" would compare them.
4992 { 5252 {
4993 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */ 5253 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4994 goto failret; 5254 goto failret;
4995 if (**arg != ':') 5255 if (**arg != ':')
4996 { 5256 {
4997 EMSG2(_("E999: Missing colon in Dictionary: %s"), *arg); 5257 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
4998 clear_tv(&tv); 5258 clear_tv(&tv);
4999 goto failret; 5259 goto failret;
5000 } 5260 }
5001 key = get_tv_string(&tv); 5261 key = get_tv_string(&tv);
5002 if (*key == NUL) 5262 if (*key == NUL)
5019 if (evaluate) 5279 if (evaluate)
5020 { 5280 {
5021 item = dict_find(d, key, -1, NULL); 5281 item = dict_find(d, key, -1, NULL);
5022 if (item != NULL) 5282 if (item != NULL)
5023 { 5283 {
5024 EMSG(_("E999: Duplicate key in Dictionary")); 5284 EMSG(_("E721: Duplicate key in Dictionary"));
5025 vim_free(key); 5285 vim_free(key);
5026 clear_tv(&tv); 5286 clear_tv(&tv);
5027 goto failret; 5287 goto failret;
5028 } 5288 }
5029 item = dictitem_alloc(); 5289 item = dictitem_alloc();
5039 5299
5040 if (**arg == '}') 5300 if (**arg == '}')
5041 break; 5301 break;
5042 if (**arg != ',') 5302 if (**arg != ',')
5043 { 5303 {
5044 EMSG2(_("E999: Missing comma in Dictionary: %s"), *arg); 5304 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
5045 goto failret; 5305 goto failret;
5046 } 5306 }
5047 *arg = skipwhite(*arg + 1); 5307 *arg = skipwhite(*arg + 1);
5048 } 5308 }
5049 5309
5050 if (**arg != '}') 5310 if (**arg != '}')
5051 { 5311 {
5052 EMSG2(_("E999: Missing end of Dictionary '}': %s"), *arg); 5312 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
5053 failret: 5313 failret:
5054 if (evaluate) 5314 if (evaluate)
5055 dict_free(d); 5315 dict_free(d);
5056 return FAIL; 5316 return FAIL;
5057 } 5317 }
5064 ++d->dv_refcount; 5324 ++d->dv_refcount;
5065 } 5325 }
5066 5326
5067 return OK; 5327 return OK;
5068 } 5328 }
5069
5070 5329
5071 /* 5330 /*
5072 * Return a string with the string representation of a variable. 5331 * Return a string with the string representation of a variable.
5073 * If the memory is allocated "tofree" is set to it, otherwise NULL. 5332 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5074 * "numbuf" is used for a number. 5333 * "numbuf" is used for a number.
5084 static int recurse = 0; 5343 static int recurse = 0;
5085 char_u *r = NULL; 5344 char_u *r = NULL;
5086 5345
5087 if (recurse >= VAR_MAXNEST) 5346 if (recurse >= VAR_MAXNEST)
5088 { 5347 {
5089 EMSG(_("E999: variable nested too deep for displaying")); 5348 EMSG(_("E724: variable nested too deep for displaying"));
5090 *tofree = NULL; 5349 *tofree = NULL;
5091 return NULL; 5350 return NULL;
5092 } 5351 }
5093 ++recurse; 5352 ++recurse;
5094 5353
5733 saveRedobuff(); 5992 saveRedobuff();
5734 ++fp->calls; 5993 ++fp->calls;
5735 call_user_func(fp, argcount, argvars, rettv, 5994 call_user_func(fp, argcount, argvars, rettv,
5736 firstline, lastline, 5995 firstline, lastline,
5737 (fp->flags & FC_DICT) ? selfdict : NULL); 5996 (fp->flags & FC_DICT) ? selfdict : NULL);
5738 --fp->calls; 5997 if (--fp->calls <= 0 && isdigit(*fp->name))
5998 /* Function was unreferenced while being used, free it
5999 * now. */
6000 func_free(fp);
5739 restoreRedobuff(); 6001 restoreRedobuff();
5740 restore_search_patterns(); 6002 restore_search_patterns();
5741 error = ERROR_NONE; 6003 error = ERROR_NONE;
5742 } 6004 }
5743 } 6005 }
5798 case ERROR_SCRIPT: 6060 case ERROR_SCRIPT:
5799 EMSG2(_("E120: Using <SID> not in a script context: %s"), 6061 EMSG2(_("E120: Using <SID> not in a script context: %s"),
5800 name); 6062 name);
5801 break; 6063 break;
5802 case ERROR_DICT: 6064 case ERROR_DICT:
5803 EMSG2(_("E999: Calling dict function without Dictionary: %s"), 6065 EMSG2(_("E725: Calling dict function without Dictionary: %s"),
5804 name); 6066 name);
5805 break; 6067 break;
5806 } 6068 }
5807 } 6069 }
5808 6070
9942 stride = get_tv_number(&argvars[2]); 10204 stride = get_tv_number(&argvars[2]);
9943 } 10205 }
9944 10206
9945 rettv->vval.v_number = 0; 10207 rettv->vval.v_number = 0;
9946 if (stride == 0) 10208 if (stride == 0)
9947 EMSG(_("E999: Stride is zero")); 10209 EMSG(_("E726: Stride is zero"));
9948 else if (stride > 0 ? end < start : end > start) 10210 else if (stride > 0 ? end < start : end > start)
9949 EMSG(_("E999: Start past end")); 10211 EMSG(_("E727: Start past end"));
9950 else 10212 else
9951 { 10213 {
9952 l = list_alloc(); 10214 l = list_alloc();
9953 if (l != NULL) 10215 if (l != NULL)
9954 { 10216 {
10438 { 10700 {
10439 if (remain == NULL) 10701 if (remain == NULL)
10440 remain = vim_strsave(q - 1); 10702 remain = vim_strsave(q - 1);
10441 else 10703 else
10442 { 10704 {
10443 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain)); 10705 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
10444 if (cpy != NULL) 10706 if (cpy != NULL)
10445 { 10707 {
10446 STRCAT(cpy, remain); 10708 STRCAT(cpy, remain);
10447 vim_free(remain); 10709 vim_free(remain);
10448 remain = cpy; 10710 remain = cpy;
10512 || (p[1] == '.' 10774 || (p[1] == '.'
10513 && (p[2] == NUL 10775 && (p[2] == NUL
10514 || vim_ispathsep(p[2])))))) 10776 || vim_ispathsep(p[2]))))))
10515 { 10777 {
10516 /* Prepend "./". */ 10778 /* Prepend "./". */
10517 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p)); 10779 cpy = concat_str((char_u *)"./", p);
10518 if (cpy != NULL) 10780 if (cpy != NULL)
10519 { 10781 {
10520 STRCAT(cpy, p);
10521 vim_free(p); 10782 vim_free(p);
10522 p = cpy; 10783 p = cpy;
10523 } 10784 }
10524 } 10785 }
10525 else if (!is_relative_to_current) 10786 else if (!is_relative_to_current)
12601 set_vim_var_string(idx, val, len) 12862 set_vim_var_string(idx, val, len)
12602 int idx; 12863 int idx;
12603 char_u *val; 12864 char_u *val;
12604 int len; /* length of "val" to use or -1 (whole string) */ 12865 int len; /* length of "val" to use or -1 (whole string) */
12605 { 12866 {
12867 /* Need to do this (at least) once, since we can't initialize a union.
12868 * Will always be invoked when "v:progname" is set. */
12869 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
12870
12606 vim_free(vimvars[idx].vv_str); 12871 vim_free(vimvars[idx].vv_str);
12607 if (val == NULL) 12872 if (val == NULL)
12608 vimvars[idx].vv_str = NULL; 12873 vimvars[idx].vv_str = NULL;
12609 else if (len == -1) 12874 else if (len == -1)
12610 vimvars[idx].vv_str = vim_strsave(val); 12875 vimvars[idx].vv_str = vim_strsave(val);
12828 { 13093 {
12829 if (varp != NULL) 13094 if (varp != NULL)
12830 { 13095 {
12831 switch (varp->v_type) 13096 switch (varp->v_type)
12832 { 13097 {
13098 case VAR_FUNC:
13099 func_unref(varp->vval.v_string);
13100 /*FALLTHROUGH*/
12833 case VAR_STRING: 13101 case VAR_STRING:
12834 case VAR_FUNC:
12835 vim_free(varp->vval.v_string); 13102 vim_free(varp->vval.v_string);
12836 break; 13103 break;
12837 case VAR_LIST: 13104 case VAR_LIST:
12838 list_unref(varp->vval.v_list); 13105 list_unref(varp->vval.v_list);
12839 break; 13106 break;
13107 case VAR_DICT:
13108 dict_unref(varp->vval.v_dict);
13109 break;
12840 default: 13110 default:
12841 break; 13111 break;
12842 } 13112 }
12843 vim_free(varp); 13113 vim_free(varp);
12844 } 13114 }
12853 { 13123 {
12854 if (varp != NULL) 13124 if (varp != NULL)
12855 { 13125 {
12856 switch (varp->v_type) 13126 switch (varp->v_type)
12857 { 13127 {
13128 case VAR_FUNC:
13129 func_unref(varp->vval.v_string);
13130 /*FALLTHROUGH*/
12858 case VAR_STRING: 13131 case VAR_STRING:
12859 case VAR_FUNC:
12860 vim_free(varp->vval.v_string); 13132 vim_free(varp->vval.v_string);
12861 varp->vval.v_string = NULL; 13133 varp->vval.v_string = NULL;
12862 break; 13134 break;
12863 case VAR_LIST: 13135 case VAR_LIST:
12864 list_unref(varp->vval.v_list); 13136 list_unref(varp->vval.v_list);
12912 TRUE, TRUE, &n, NULL); 13184 TRUE, TRUE, &n, NULL);
12913 break; 13185 break;
12914 case VAR_LIST: 13186 case VAR_LIST:
12915 EMSG(_("E703: Using a List as a number")); 13187 EMSG(_("E703: Using a List as a number"));
12916 break; 13188 break;
13189 case VAR_DICT:
13190 EMSG(_("E728: Using a Dictionary as a number"));
13191 break;
12917 default: 13192 default:
13193 EMSG2(_(e_intern2), "get_tv_number()");
12918 break; 13194 break;
12919 } 13195 }
12920 return n; 13196 return n;
12921 } 13197 }
12922 13198
12967 { 13243 {
12968 case VAR_NUMBER: 13244 case VAR_NUMBER:
12969 sprintf((char *)buf, "%ld", (long)varp->vval.v_number); 13245 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
12970 return buf; 13246 return buf;
12971 case VAR_FUNC: 13247 case VAR_FUNC:
12972 EMSG(_("E999: using Funcref as a String")); 13248 EMSG(_("E729: using Funcref as a String"));
12973 break; 13249 break;
12974 case VAR_LIST: 13250 case VAR_LIST:
12975 EMSG(_("E999: using List as a String")); 13251 EMSG(_("E730: using List as a String"));
12976 break; 13252 break;
12977 case VAR_DICT: 13253 case VAR_DICT:
12978 EMSG(_("E999: using Dictionary as a String")); 13254 EMSG(_("E731: using Dictionary as a String"));
12979 break; 13255 break;
12980 case VAR_STRING: 13256 case VAR_STRING:
12981 if (varp->vval.v_string != NULL) 13257 if (varp->vval.v_string != NULL)
12982 return varp->vval.v_string; 13258 return varp->vval.v_string;
12983 break; 13259 break;
13359 case VAR_STRING: 13635 case VAR_STRING:
13360 case VAR_FUNC: 13636 case VAR_FUNC:
13361 if (from->vval.v_string == NULL) 13637 if (from->vval.v_string == NULL)
13362 to->vval.v_string = NULL; 13638 to->vval.v_string = NULL;
13363 else 13639 else
13640 {
13364 to->vval.v_string = vim_strsave(from->vval.v_string); 13641 to->vval.v_string = vim_strsave(from->vval.v_string);
13642 if (from->v_type == VAR_FUNC)
13643 func_ref(to->vval.v_string);
13644 }
13365 break; 13645 break;
13366 case VAR_LIST: 13646 case VAR_LIST:
13367 if (from->vval.v_list == NULL) 13647 if (from->vval.v_list == NULL)
13368 to->vval.v_list = NULL; 13648 to->vval.v_list = NULL;
13369 else 13649 else
13677 int flags = 0; 13957 int flags = 0;
13678 ufunc_T *fp; 13958 ufunc_T *fp;
13679 int indent; 13959 int indent;
13680 int nesting; 13960 int nesting;
13681 char_u *skip_until = NULL; 13961 char_u *skip_until = NULL;
13682 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
13683 VAR v; 13962 VAR v;
13963 funcdict fudi;
13964 static int func_nr = 0; /* number for nameless function */
13965 int paren;
13684 13966
13685 /* 13967 /*
13686 * ":function" without argument: list functions. 13968 * ":function" without argument: list functions.
13687 */ 13969 */
13688 if (ends_excmd(*eap->arg)) 13970 if (ends_excmd(*eap->arg))
13689 { 13971 {
13690 if (!eap->skip) 13972 if (!eap->skip)
13691 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next) 13973 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
13692 list_func_head(fp, FALSE); 13974 if (!isdigit(*fp->name))
13975 list_func_head(fp, FALSE);
13693 eap->nextcmd = check_nextcmd(eap->arg); 13976 eap->nextcmd = check_nextcmd(eap->arg);
13694 return; 13977 return;
13695 } 13978 }
13696 13979
13980 /*
13981 * Get the function name. There are these situations:
13982 * func normal function name
13983 * "name" == func, "fudi.fd_dict" == NULL
13984 * dict.func new dictionary entry
13985 * "name" == NULL, "fudi.fd_dict" set,
13986 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
13987 * dict.func existing dict entry with a Funcref
13988 * "name" == fname, "fudi.fd_dict" set,
13989 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
13990 * dict.func existing dict entry that's not a Funcref
13991 * "name" == NULL, "fudi.fd_dict" set,
13992 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
13993 */
13697 p = eap->arg; 13994 p = eap->arg;
13698 name = trans_function_name(&p, eap->skip, FALSE); 13995 name = trans_function_name(&p, eap->skip, 0, &fudi);
13699 if (name == NULL && !eap->skip) 13996 paren = (vim_strchr(p, '(') != NULL);
13997 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
13700 { 13998 {
13701 /* 13999 /*
13702 * Return on an invalid expression in braces, unless the expression 14000 * Return on an invalid expression in braces, unless the expression
13703 * evaluation has been cancelled due to an aborting error, an 14001 * evaluation has been cancelled due to an aborting error, an
13704 * interrupt, or an exception. 14002 * interrupt, or an exception.
13705 */ 14003 */
13706 if (!aborting()) 14004 if (!aborting())
14005 {
14006 vim_free(fudi.fd_newkey);
13707 return; 14007 return;
14008 }
13708 else 14009 else
13709 eap->skip = TRUE; 14010 eap->skip = TRUE;
13710 } 14011 }
13711 /* An error in a function call during evaluation of an expression in magic 14012 /* An error in a function call during evaluation of an expression in magic
13712 * braces should not cause the function not to be defined. */ 14013 * braces should not cause the function not to be defined. */
13714 did_emsg = FALSE; 14015 did_emsg = FALSE;
13715 14016
13716 /* 14017 /*
13717 * ":function func" with only function name: list function. 14018 * ":function func" with only function name: list function.
13718 */ 14019 */
13719 if (vim_strchr(p, '(') == NULL) 14020 if (!paren)
13720 { 14021 {
13721 if (!ends_excmd(*skipwhite(p))) 14022 if (!ends_excmd(*skipwhite(p)))
13722 { 14023 {
13723 EMSG(_(e_trailing)); 14024 EMSG(_(e_trailing));
13724 goto erret_name; 14025 goto ret_free;
13725 } 14026 }
13726 eap->nextcmd = check_nextcmd(p); 14027 eap->nextcmd = check_nextcmd(p);
13727 if (eap->nextcmd != NULL) 14028 if (eap->nextcmd != NULL)
13728 *p = NUL; 14029 *p = NUL;
13729 if (!eap->skip && !got_int) 14030 if (!eap->skip && !got_int)
13751 } 14052 }
13752 } 14053 }
13753 else 14054 else
13754 EMSG2(_("E123: Undefined function: %s"), name); 14055 EMSG2(_("E123: Undefined function: %s"), name);
13755 } 14056 }
13756 goto erret_name; 14057 goto ret_free;
13757 } 14058 }
13758 14059
13759 /* 14060 /*
13760 * ":function name(arg1, arg2)" Define function. 14061 * ":function name(arg1, arg2)" Define function.
13761 */ 14062 */
13763 if (*p != '(') 14064 if (*p != '(')
13764 { 14065 {
13765 if (!eap->skip) 14066 if (!eap->skip)
13766 { 14067 {
13767 EMSG2(_("E124: Missing '(': %s"), eap->arg); 14068 EMSG2(_("E124: Missing '(': %s"), eap->arg);
13768 goto erret_name; 14069 goto ret_free;
13769 } 14070 }
13770 /* attempt to continue by skipping some text */ 14071 /* attempt to continue by skipping some text */
13771 if (vim_strchr(p, '(') != NULL) 14072 if (vim_strchr(p, '(') != NULL)
13772 p = vim_strchr(p, '('); 14073 p = vim_strchr(p, '(');
13773 } 14074 }
13857 if (KeyTyped) 14158 if (KeyTyped)
13858 { 14159 {
13859 /* Check if the function already exists, don't let the user type the 14160 /* Check if the function already exists, don't let the user type the
13860 * whole function before telling him it doesn't work! For a script we 14161 * whole function before telling him it doesn't work! For a script we
13861 * need to skip the body to be able to find what follows. */ 14162 * need to skip the body to be able to find what follows. */
13862 if (!eap->skip && !eap->forceit && find_func(name) != NULL) 14163 if (!eap->skip && !eap->forceit)
13863 EMSG2(_(e_funcexts), name); 14164 {
14165 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
14166 EMSG(_(e_funcdict));
14167 else if (name != NULL && find_func(name) != NULL)
14168 EMSG2(_(e_funcexts), name);
14169 }
13864 14170
13865 msg_putchar('\n'); /* don't overwrite the function name */ 14171 msg_putchar('\n'); /* don't overwrite the function name */
13866 cmdline_row = msg_row; 14172 cmdline_row = msg_row;
13867 } 14173 }
13868 14174
13923 if (*p == '!') 14229 if (*p == '!')
13924 p = skipwhite(p + 1); 14230 p = skipwhite(p + 1);
13925 p += eval_fname_script(p); 14231 p += eval_fname_script(p);
13926 if (ASCII_ISALPHA(*p)) 14232 if (ASCII_ISALPHA(*p))
13927 { 14233 {
13928 vim_free(trans_function_name(&p, TRUE, FALSE)); 14234 vim_free(trans_function_name(&p, TRUE, 0, NULL));
13929 if (*skipwhite(p) == '(') 14235 if (*skipwhite(p) == '(')
13930 { 14236 {
13931 ++nesting; 14237 ++nesting;
13932 indent += 2; 14238 indent += 2;
13933 } 14239 }
13979 goto erret; 14285 goto erret;
13980 14286
13981 /* 14287 /*
13982 * If there are no errors, add the function 14288 * If there are no errors, add the function
13983 */ 14289 */
13984 v = find_var(name, FALSE); 14290 if (fudi.fd_dict == NULL)
13985 if (v != NULL && v->tv.v_type == VAR_FUNC) 14291 {
13986 { 14292 v = find_var(name, FALSE);
13987 EMSG2(_("E707: Function name conflicts with variable: %s"), name); 14293 if (v != NULL && v->tv.v_type == VAR_FUNC)
13988 goto erret; 14294 {
13989 } 14295 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
13990
13991 fp = find_func(name);
13992 if (fp != NULL)
13993 {
13994 if (!eap->forceit)
13995 {
13996 EMSG2(_(e_funcexts), name);
13997 goto erret; 14296 goto erret;
13998 } 14297 }
13999 if (fp->calls) 14298
14000 { 14299 fp = find_func(name);
14001 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name); 14300 if (fp != NULL)
14301 {
14302 if (!eap->forceit)
14303 {
14304 EMSG2(_(e_funcexts), name);
14305 goto erret;
14306 }
14307 if (fp->calls > 0)
14308 {
14309 EMSG2(_("E127: Cannot redefine function %s: It is in use"),
14310 name);
14311 goto erret;
14312 }
14313 /* redefine existing function */
14314 ga_clear_strings(&(fp->args));
14315 ga_clear_strings(&(fp->lines));
14316 vim_free(name);
14317 name = NULL;
14318 }
14319 }
14320 else
14321 {
14322 char numbuf[20];
14323
14324 fp = NULL;
14325 if (fudi.fd_newkey == NULL && !eap->forceit)
14326 {
14327 EMSG(_(e_funcdict));
14002 goto erret; 14328 goto erret;
14003 } 14329 }
14004 /* redefine existing function */ 14330
14005 ga_clear_strings(&(fp->args)); 14331 /* Give the function a sequential number. Can only be used with a
14006 ga_clear_strings(&(fp->lines)); 14332 * Funcref! */
14007 vim_free(name); 14333 vim_free(name);
14008 } 14334 sprintf(numbuf, "%d", ++func_nr);
14009 else 14335 name = vim_strsave((char_u *)numbuf);
14336 if (name == NULL)
14337 goto erret;
14338 }
14339
14340 if (fp == NULL)
14010 { 14341 {
14011 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T)); 14342 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
14012 if (fp == NULL) 14343 if (fp == NULL)
14013 goto erret; 14344 goto erret;
14345
14346 if (fudi.fd_dict != NULL)
14347 {
14348 if (fudi.fd_di == NULL)
14349 {
14350 /* add new dict entry */
14351 fudi.fd_di = dictitem_alloc();
14352 if (fudi.fd_di == NULL)
14353 {
14354 vim_free(fp);
14355 goto erret;
14356 }
14357 fudi.fd_di->di_key = fudi.fd_newkey;
14358 fudi.fd_newkey = NULL;
14359 dict_add(fudi.fd_dict, fudi.fd_di);
14360 }
14361 else
14362 /* overwrite existing dict entry */
14363 clear_tv(&fudi.fd_di->di_tv);
14364 fudi.fd_di->di_tv.v_type = VAR_FUNC;
14365 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
14366 fp->refcount = 1;
14367 }
14368
14014 /* insert the new function in the function list */ 14369 /* insert the new function in the function list */
14370 fp->name = name;
14371 name = NULL;
14015 fp->next = firstfunc; 14372 fp->next = firstfunc;
14016 firstfunc = fp; 14373 firstfunc = fp;
14017 fp->name = name;
14018 } 14374 }
14019 fp->args = newargs; 14375 fp->args = newargs;
14020 fp->lines = newlines; 14376 fp->lines = newlines;
14021 fp->varargs = varargs; 14377 fp->varargs = varargs;
14022 fp->flags = flags; 14378 fp->flags = flags;
14023 fp->calls = 0; 14379 fp->calls = 0;
14024 fp->script_ID = current_SID; 14380 fp->script_ID = current_SID;
14025 did_emsg |= saved_did_emsg; 14381 goto ret_free;
14026 vim_free(skip_until);
14027 return;
14028 14382
14029 erret: 14383 erret:
14030 vim_free(skip_until);
14031 ga_clear_strings(&newargs); 14384 ga_clear_strings(&newargs);
14032 ga_clear_strings(&newlines); 14385 ga_clear_strings(&newlines);
14033 erret_name: 14386 ret_free:
14387 vim_free(skip_until);
14388 vim_free(fudi.fd_newkey);
14034 vim_free(name); 14389 vim_free(name);
14035 did_emsg |= saved_did_emsg; 14390 did_emsg |= saved_did_emsg;
14036 } 14391 }
14037 14392
14038 /* 14393 /*
14039 * Get a function name, translating "<SID>" and "<SNR>". 14394 * Get a function name, translating "<SID>" and "<SNR>".
14040 * Returns the function name in allocated memory, or NULL for failure. 14395 * Returns the function name in allocated memory, or NULL for failure.
14396 * flags:
14397 * TFN_INT: internal function name OK
14398 * TFN_QUIET: be quiet
14041 * Advances "pp" to just after the function name (if no error). 14399 * Advances "pp" to just after the function name (if no error).
14042 */ 14400 */
14043 static char_u * 14401 static char_u *
14044 trans_function_name(pp, skip, exists) 14402 trans_function_name(pp, skip, flags, fdp)
14045 char_u **pp; 14403 char_u **pp;
14046 int skip; /* only find the end, don't evaluate */ 14404 int skip; /* only find the end, don't evaluate */
14047 int exists; /* TRUE for exists(): internal function name 14405 int flags;
14048 OK and be quiet. */ 14406 funcdict *fdp; /* return: info about dictionary used */
14049 { 14407 {
14050 char_u *name = NULL; 14408 char_u *name = NULL;
14051 char_u *start; 14409 char_u *start;
14052 char_u *end; 14410 char_u *end;
14053 int lead; 14411 int lead;
14054 char_u sid_buf[20]; 14412 char_u sid_buf[20];
14055 int len; 14413 int len;
14056 #if 0
14057 char_u *expr_start, *expr_end;
14058 char_u *temp_string = NULL;
14059 #else
14060 lval lv; 14414 lval lv;
14061 #endif 14415
14416 if (fdp != NULL)
14417 vim_memset(fdp, 0, sizeof(funcdict));
14062 14418
14063 /* A name starting with "<SID>" or "<SNR>" is local to a script. */ 14419 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
14064 start = *pp; 14420 start = *pp;
14065 lead = eval_fname_script(start); 14421 lead = eval_fname_script(start);
14066 if (lead > 0) 14422 if (lead > 0)
14067 start += lead; 14423 start += lead;
14068 14424
14069 #if 1 14425 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
14070 end = get_lval(start, NULL, &lv, FALSE, skip, exists);
14071 if (end == start) 14426 if (end == start)
14072 { 14427 {
14073 if (!skip) 14428 if (!skip)
14074 EMSG(_("E129: Function name required")); 14429 EMSG(_("E129: Function name required"));
14075 goto theend; 14430 goto theend;
14091 goto theend; 14446 goto theend;
14092 } 14447 }
14093 14448
14094 if (lv.ll_tv != NULL) 14449 if (lv.ll_tv != NULL)
14095 { 14450 {
14096 /* TODO: When defining a function accept a Dict here. */ 14451 if (fdp != NULL)
14452 {
14453 fdp->fd_dict = lv.ll_dict;
14454 fdp->fd_newkey = lv.ll_newkey;
14455 lv.ll_newkey = NULL;
14456 fdp->fd_di = lv.ll_di;
14457 fdp->fd_pdi = lv.ll_pdi;
14458 }
14097 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL) 14459 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
14098 { 14460 {
14099 name = vim_strsave(lv.ll_tv->vval.v_string); 14461 name = vim_strsave(lv.ll_tv->vval.v_string);
14100 *pp = end; 14462 *pp = end;
14101 } 14463 }
14102 else 14464 else
14103 { 14465 {
14104 if (!skip && !exists) 14466 if (!skip && !(flags & TFN_QUIET)
14105 EMSG(_("E999: Funcref required")); 14467 && (fdp == NULL || lv.ll_dict == NULL))
14468 EMSG(_(e_funcref));
14469 else
14470 *pp = end;
14106 name = NULL; 14471 name = NULL;
14107 } 14472 }
14108 goto theend; 14473 goto theend;
14109 } 14474 }
14110 14475
14139 } 14504 }
14140 sprintf((char *)sid_buf, "%ld_", (long)current_SID); 14505 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
14141 lead += (int)STRLEN(sid_buf); 14506 lead += (int)STRLEN(sid_buf);
14142 } 14507 }
14143 } 14508 }
14144 else if (!exists && !ASCII_ISUPPER(*lv.ll_name)) 14509 else if (!(flags & TFN_INT) && !ASCII_ISUPPER(*lv.ll_name))
14145 { 14510 {
14146 EMSG2(_("E128: Function name must start with a capital: %s"), 14511 EMSG2(_("E128: Function name must start with a capital: %s"),
14147 lv.ll_name); 14512 lv.ll_name);
14148 goto theend; 14513 goto theend;
14149 } 14514 }
14164 *pp = end; 14529 *pp = end;
14165 14530
14166 theend: 14531 theend:
14167 clear_lval(&lv); 14532 clear_lval(&lv);
14168 return name; 14533 return name;
14169 #endif
14170
14171 #if 0
14172
14173 end = find_name_end(start, &expr_start, &expr_end, FALSE);
14174 if (end == start)
14175 {
14176 if (!skip)
14177 EMSG(_("E129: Function name required"));
14178 return NULL;
14179 }
14180 if (expr_start != NULL && !skip)
14181 {
14182 /* expand magic curlies */
14183 temp_string = make_expanded_name(start, expr_start, expr_end, end);
14184 if (temp_string == NULL)
14185 {
14186 /*
14187 * Report an invalid expression in braces, unless the expression
14188 * evaluation has been cancelled due to an aborting error, an
14189 * interrupt, or an exception.
14190 */
14191 if (!aborting())
14192 EMSG2(_(e_invarg2), start);
14193 else
14194 *pp = end;
14195 return NULL;
14196 }
14197 start = temp_string;
14198 len = (int)STRLEN(temp_string);
14199 }
14200 else
14201 len = (int)(end - start);
14202
14203 /*
14204 * Copy the function name to allocated memory.
14205 * Accept <SID>name() inside a script, translate into <SNR>123_name().
14206 * Accept <SNR>123_name() outside a script.
14207 */
14208 if (skip)
14209 lead = 0; /* do nothing */
14210 else if (lead > 0)
14211 {
14212 lead = 3;
14213 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14214 {
14215 if (current_SID <= 0)
14216 {
14217 EMSG(_(e_usingsid));
14218 return NULL;
14219 }
14220 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
14221 lead += (int)STRLEN(sid_buf);
14222 }
14223 }
14224 else if (!exists && !ASCII_ISUPPER(*start))
14225 {
14226 EMSG2(_("E128: Function name must start with a capital: %s"), start);
14227 return NULL;
14228 }
14229 name = alloc((unsigned)(len + lead + 1));
14230 if (name != NULL)
14231 {
14232 if (lead > 0)
14233 {
14234 name[0] = K_SPECIAL;
14235 name[1] = KS_EXTRA;
14236 name[2] = (int)KE_SNR;
14237 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
14238 STRCPY(name + 3, sid_buf);
14239 }
14240 mch_memmove(name + lead, start, (size_t)len);
14241 name[len + lead] = NUL;
14242 }
14243 *pp = end;
14244
14245 vim_free(temp_string);
14246 return name;
14247 #endif
14248 } 14534 }
14249 14535
14250 /* 14536 /*
14251 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case). 14537 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
14252 * Return 2 if "p" starts with "s:". 14538 * Return 2 if "p" starts with "s:".
14336 char_u *name; 14622 char_u *name;
14337 { 14623 {
14338 char_u *p = name; 14624 char_u *p = name;
14339 int n = FALSE; 14625 int n = FALSE;
14340 14626
14341 p = trans_function_name(&p, FALSE, TRUE); 14627 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
14342 if (p != NULL) 14628 if (p != NULL)
14343 { 14629 {
14344 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL) 14630 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
14345 n = (find_func(p) != NULL); 14631 n = (find_func(p) != NULL);
14346 else if (ASCII_ISLOWER(*p)) 14632 else if (ASCII_ISLOWER(*p))
14410 */ 14696 */
14411 void 14697 void
14412 ex_delfunction(eap) 14698 ex_delfunction(eap)
14413 exarg_T *eap; 14699 exarg_T *eap;
14414 { 14700 {
14415 ufunc_T *fp = NULL, *pfp; 14701 ufunc_T *fp = NULL;
14416 char_u *p; 14702 char_u *p;
14417 char_u *name; 14703 char_u *name;
14704 funcdict fudi;
14418 14705
14419 p = eap->arg; 14706 p = eap->arg;
14420 name = trans_function_name(&p, eap->skip, FALSE); 14707 name = trans_function_name(&p, eap->skip, 0, &fudi);
14708 vim_free(fudi.fd_newkey);
14421 if (name == NULL) 14709 if (name == NULL)
14710 {
14711 if (fudi.fd_dict != NULL && !eap->skip)
14712 EMSG(_(e_funcref));
14422 return; 14713 return;
14714 }
14423 if (!ends_excmd(*skipwhite(p))) 14715 if (!ends_excmd(*skipwhite(p)))
14424 { 14716 {
14425 vim_free(name); 14717 vim_free(name);
14426 EMSG(_(e_trailing)); 14718 EMSG(_(e_trailing));
14427 return; 14719 return;
14439 if (fp == NULL) 14731 if (fp == NULL)
14440 { 14732 {
14441 EMSG2(_("E130: Undefined function: %s"), eap->arg); 14733 EMSG2(_("E130: Undefined function: %s"), eap->arg);
14442 return; 14734 return;
14443 } 14735 }
14444 if (fp->calls) 14736 if (fp->calls > 0)
14445 { 14737 {
14446 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg); 14738 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
14447 return; 14739 return;
14448 } 14740 }
14449 14741
14450 /* clear this function */ 14742 if (fudi.fd_dict != NULL)
14451 vim_free(fp->name); 14743 {
14452 ga_clear_strings(&(fp->args)); 14744 /* Delete the dict item that refers to the function, it will
14453 ga_clear_strings(&(fp->lines)); 14745 * invoke func_unref() and possibly delete the function. */
14454 14746 *fudi.fd_pdi = fudi.fd_di->di_next;
14455 /* remove the function from the function list */ 14747 dictitem_free(fudi.fd_di);
14456 if (firstfunc == fp) 14748 }
14457 firstfunc = fp->next;
14458 else 14749 else
14459 { 14750 func_free(fp);
14460 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next) 14751 }
14461 if (pfp->next == fp) 14752 }
14462 { 14753
14463 pfp->next = fp->next; 14754 /*
14464 break; 14755 * Free a function and remove it from the list of functions.
14465 } 14756 */
14466 } 14757 static void
14467 vim_free(fp); 14758 func_free(fp)
14759 ufunc_T *fp;
14760 {
14761 ufunc_T *pfp;
14762
14763 /* clear this function */
14764 vim_free(fp->name);
14765 ga_clear_strings(&(fp->args));
14766 ga_clear_strings(&(fp->lines));
14767
14768 /* remove the function from the function list */
14769 if (firstfunc == fp)
14770 firstfunc = fp->next;
14771 else
14772 {
14773 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
14774 if (pfp->next == fp)
14775 {
14776 pfp->next = fp->next;
14777 break;
14778 }
14779 }
14780 vim_free(fp);
14781 }
14782
14783 /*
14784 * Unreference a Function: decrement the reference count and free it when it
14785 * becomes zero. Only for numbered functions.
14786 */
14787 static void
14788 func_unref(name)
14789 char_u *name;
14790 {
14791 ufunc_T *fp;
14792
14793 if (name != NULL && isdigit(*name))
14794 {
14795 fp = find_func(name);
14796 if (fp == NULL)
14797 EMSG2(_(e_intern2), "func_unref()");
14798 else if (--fp->refcount <= 0)
14799 {
14800 /* Only delete it when it's not being used. Otherwise it's done
14801 * when "calls" becomes zero. */
14802 if (fp->calls == 0)
14803 func_free(fp);
14804 }
14805 }
14806 }
14807
14808 /*
14809 * Count a reference to a Function.
14810 */
14811 static void
14812 func_ref(name)
14813 char_u *name;
14814 {
14815 ufunc_T *fp;
14816
14817 if (name != NULL && isdigit(*name))
14818 {
14819 fp = find_func(name);
14820 if (fp == NULL)
14821 EMSG2(_(e_intern2), "func_ref()");
14822 else
14823 ++fp->refcount;
14468 } 14824 }
14469 } 14825 }
14470 14826
14471 /* 14827 /*
14472 * Call a user function. 14828 * Call a user function.