comparison src/eval.c @ 159:389c8abd5925 v7.0048

updated for version 7.0048
author vimboss
date Sat, 05 Feb 2005 21:43:56 +0000
parents c5b05f6de1ad
children 06bc859d1a32
comparison
equal deleted inserted replaced
158:78423945b251 159:389c8abd5925
233 #include "version.h" 233 #include "version.h"
234 234
235 /* values for vv_flags: */ 235 /* values for vv_flags: */
236 #define VV_COMPAT 1 /* compatible, also used without "v:" */ 236 #define VV_COMPAT 1 /* compatible, also used without "v:" */
237 #define VV_RO 2 /* read-only */ 237 #define VV_RO 2 /* read-only */
238 #define VV_RO_SBX 4 /* read-only in the sandbox*/ 238 #define VV_RO_SBX 4 /* read-only in the sandbox */
239 239
240 #define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0} 240 #define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
241 241
242 static struct vimvar 242 static struct vimvar
243 { 243 {
284 {VV_NAME("exception", VAR_STRING), VV_RO}, 284 {VV_NAME("exception", VAR_STRING), VV_RO},
285 {VV_NAME("throwpoint", VAR_STRING), VV_RO}, 285 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
286 {VV_NAME("register", VAR_STRING), VV_RO}, 286 {VV_NAME("register", VAR_STRING), VV_RO},
287 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO}, 287 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
288 {VV_NAME("insertmode", VAR_STRING), VV_RO}, 288 {VV_NAME("insertmode", VAR_STRING), VV_RO},
289 {VV_NAME("val", VAR_STRING), VV_RO}, 289 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
290 {VV_NAME("key", VAR_STRING), VV_RO}, 290 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
291 }; 291 };
292 292
293 /* shorthand */ 293 /* shorthand */
294 #define vv_type vv_di.di_tv.v_type 294 #define vv_type vv_di.di_tv.v_type
295 #define vv_nr vv_di.di_tv.vval.v_number 295 #define vv_nr vv_di.di_tv.vval.v_number
310 static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 310 static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
311 static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 311 static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
312 static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 312 static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
313 static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 313 static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
314 static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 314 static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
315 static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 315 static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
316 static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 316 static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
317 static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 317 static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
318 static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 318 static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
319 static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 319 static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
320 static list_T *list_alloc __ARGS((void)); 320 static list_T *list_alloc __ARGS((void));
467 static void f_map __ARGS((typval_T *argvars, typval_T *rettv)); 467 static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
468 static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv)); 468 static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
469 static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv)); 469 static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
470 static void f_match __ARGS((typval_T *argvars, typval_T *rettv)); 470 static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
471 static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv)); 471 static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
472 static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
472 static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv)); 473 static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
473 static void f_max __ARGS((typval_T *argvars, typval_T *rettv)); 474 static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
474 static void f_min __ARGS((typval_T *argvars, typval_T *rettv)); 475 static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
475 static void f_mode __ARGS((typval_T *argvars, typval_T *rettv)); 476 static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
476 static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv)); 477 static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
477 static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv)); 478 static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
478 static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv)); 479 static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
479 static void f_range __ARGS((typval_T *argvars, typval_T *rettv)); 480 static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
481 static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
480 static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv)); 482 static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
481 static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv)); 483 static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
482 static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv)); 484 static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
483 static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv)); 485 static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
484 static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv)); 486 static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
527 static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv)); 529 static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
528 static void f_winline __ARGS((typval_T *argvars, typval_T *rettv)); 530 static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
529 static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv)); 531 static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
530 static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv)); 532 static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
531 static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv)); 533 static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
534 static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
532 535
533 static win_T *find_win_by_nr __ARGS((typval_T *vp)); 536 static win_T *find_win_by_nr __ARGS((typval_T *vp));
534 static pos_T *var2fpos __ARGS((typval_T *varp, int lnum)); 537 static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
535 static int get_env_len __ARGS((char_u **arg)); 538 static int get_env_len __ARGS((char_u **arg));
536 static int get_id_len __ARGS((char_u **arg)); 539 static int get_id_len __ARGS((char_u **arg));
537 static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate)); 540 static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
538 static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br)); 541 static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
539 static int eval_isnamec __ARGS((int c)); 542 static int eval_isnamec __ARGS((int c));
540 static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv)); 543 static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
544 static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
541 static typval_T *alloc_tv __ARGS((void)); 545 static typval_T *alloc_tv __ARGS((void));
542 static typval_T *alloc_string_tv __ARGS((char_u *string)); 546 static typval_T *alloc_string_tv __ARGS((char_u *string));
543 static void free_tv __ARGS((typval_T *varp)); 547 static void free_tv __ARGS((typval_T *varp));
544 static void clear_tv __ARGS((typval_T *varp)); 548 static void clear_tv __ARGS((typval_T *varp));
545 static void init_tv __ARGS((typval_T *varp)); 549 static void init_tv __ARGS((typval_T *varp));
619 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 623 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
620 else if (p->vv_flags & VV_RO_SBX) 624 else if (p->vv_flags & VV_RO_SBX)
621 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX; 625 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
622 else 626 else
623 p->vv_di.di_flags = DI_FLAGS_FIX; 627 p->vv_di.di_flags = DI_FLAGS_FIX;
624 /* add to v: scope dict */ 628
625 hash_add(&vimvarht, p->vv_di.di_key); 629 /* add to v: scope dict, unless the value is not always available */
630 if (p->vv_type != VAR_UNKNOWN)
631 hash_add(&vimvarht, p->vv_di.di_key);
626 if (p->vv_flags & VV_COMPAT) 632 if (p->vv_flags & VV_COMPAT)
627 /* add to compat scope dict */ 633 /* add to compat scope dict */
628 hash_add(&compat_hashtab, p->vv_di.di_key); 634 hash_add(&compat_hashtab, p->vv_di.di_key);
629 } 635 }
630 } 636 }
1074 --sandbox; 1080 --sandbox;
1075 1081
1076 return retval; 1082 return retval;
1077 } 1083 }
1078 #endif 1084 #endif
1079
1080 /*
1081 * Expands out the 'magic' {}'s in a variable/function name.
1082 * Note that this can call itself recursively, to deal with
1083 * constructs like foo{bar}{baz}{bam}
1084 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1085 * "in_start" ^
1086 * "expr_start" ^
1087 * "expr_end" ^
1088 * "in_end" ^
1089 *
1090 * Returns a new allocated string, which the caller must free.
1091 * Returns NULL for failure.
1092 */
1093 static char_u *
1094 make_expanded_name(in_start, expr_start, expr_end, in_end)
1095 char_u *in_start;
1096 char_u *expr_start;
1097 char_u *expr_end;
1098 char_u *in_end;
1099 {
1100 char_u c1;
1101 char_u *retval = NULL;
1102 char_u *temp_result;
1103 char_u *nextcmd = NULL;
1104
1105 if (expr_end == NULL || in_end == NULL)
1106 return NULL;
1107 *expr_start = NUL;
1108 *expr_end = NUL;
1109 c1 = *in_end;
1110 *in_end = NUL;
1111
1112 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1113 if (temp_result != NULL && nextcmd == NULL)
1114 {
1115 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1116 + (in_end - expr_end) + 1));
1117
1118 if (retval != NULL)
1119 {
1120 STRCPY(retval, in_start);
1121 STRCAT(retval, temp_result);
1122 STRCAT(retval, expr_end + 1);
1123 }
1124 }
1125 vim_free(temp_result);
1126
1127 *in_end = c1; /* put char back for error messages */
1128 *expr_start = '{';
1129 *expr_end = '}';
1130
1131 if (retval != NULL)
1132 {
1133 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
1134 if (expr_start != NULL)
1135 {
1136 /* Further expansion! */
1137 temp_result = make_expanded_name(retval, expr_start,
1138 expr_end, temp_result);
1139 vim_free(retval);
1140 retval = temp_result;
1141 }
1142 }
1143
1144 return retval;
1145
1146 }
1147 1085
1148 /* 1086 /*
1149 * ":let" list all variable values 1087 * ":let" list all variable values
1150 * ":let var1 var2" list variable values 1088 * ":let var1 var2" list variable values
1151 * ":let var = expr" assignment command. 1089 * ":let var = expr" assignment command.
1370 } 1308 }
1371 else 1309 else
1372 return skip_var_one(arg); 1310 return skip_var_one(arg);
1373 } 1311 }
1374 1312
1313 /*
1314 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1315 */
1375 static char_u * 1316 static char_u *
1376 skip_var_one(arg) 1317 skip_var_one(arg)
1377 char_u *arg; 1318 char_u *arg;
1378 { 1319 {
1379 if (vim_strchr((char_u *)"$@&", *arg) != NULL) 1320 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1422 * List buffer variables. 1363 * List buffer variables.
1423 */ 1364 */
1424 static void 1365 static void
1425 list_buf_vars() 1366 list_buf_vars()
1426 { 1367 {
1368 char_u numbuf[NUMBUFLEN];
1369
1427 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE); 1370 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
1371
1372 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1373 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
1428 } 1374 }
1429 1375
1430 /* 1376 /*
1431 * List window variables. 1377 * List window variables.
1432 */ 1378 */
1452 list_arg_vars(eap, arg) 1398 list_arg_vars(eap, arg)
1453 exarg_T *eap; 1399 exarg_T *eap;
1454 char_u *arg; 1400 char_u *arg;
1455 { 1401 {
1456 int error = FALSE; 1402 int error = FALSE;
1457 char_u *temp_string = NULL; 1403 int len;
1458 int arg_len;
1459 char_u *expr_start;
1460 char_u *expr_end;
1461 char_u *name_end;
1462 int c1 = 0, c2;
1463 dictitem_T *varp;
1464 char_u *name; 1404 char_u *name;
1405 char_u *name_start;
1406 char_u *arg_subsc;
1407 char_u *tofree;
1408 typval_T tv;
1465 1409
1466 while (!ends_excmd(*arg) && !got_int) 1410 while (!ends_excmd(*arg) && !got_int)
1467 { 1411 {
1468 /* Find the end of the name. */ 1412 if (error || eap->skip)
1469 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE); 1413 {
1470 1414 arg = find_name_end(arg, NULL, NULL, TRUE);
1471 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end)) 1415 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1472 {
1473 emsg_severe = TRUE;
1474 EMSG(_(e_trailing));
1475 break;
1476 }
1477 if (!error && !eap->skip)
1478 {
1479 if (expr_start != NULL)
1480 { 1416 {
1481 temp_string = make_expanded_name(arg, expr_start, 1417 emsg_severe = TRUE;
1482 expr_end, name_end); 1418 EMSG(_(e_trailing));
1483 if (temp_string == NULL) 1419 break;
1420 }
1421 }
1422 else
1423 {
1424 /* get_name_len() takes care of expanding curly braces */
1425 name_start = name = arg;
1426 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1427 if (len <= 0)
1428 {
1429 /* This is mainly to keep test 49 working: when expanding
1430 * curly braces fails overrule the exception error message. */
1431 if (len < 0 && !aborting())
1484 { 1432 {
1485 /* 1433 emsg_severe = TRUE;
1486 * Report an invalid expression in braces, unless 1434 EMSG2(_(e_invarg2), arg);
1487 * the expression evaluation has been cancelled due 1435 break;
1488 * to an aborting error, an interrupt, or an
1489 * exception.
1490 */
1491 if (!aborting())
1492 {
1493 emsg_severe = TRUE;
1494 EMSG2(_(e_invarg2), arg);
1495 break;
1496 }
1497 error = TRUE;
1498 arg = skipwhite(name_end);
1499 continue;
1500 } 1436 }
1501 arg = temp_string; 1437 error = TRUE;
1502 arg_len = STRLEN(temp_string);
1503 } 1438 }
1504 else 1439 else
1505 { 1440 {
1506 c1 = *name_end; 1441 if (tofree != NULL)
1507 *name_end = NUL; 1442 name = tofree;
1508 arg_len = (int)(name_end - arg); 1443 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
1509 } 1444 error = TRUE;
1510 if (arg_len == 2 && arg[1] == ':')
1511 {
1512 switch (*arg)
1513 {
1514 case 'g': list_glob_vars(); break;
1515 case 'b': list_buf_vars(); break;
1516 case 'w': list_win_vars(); break;
1517 case 'v': list_vim_vars(); break;
1518 default:
1519 EMSG2(_("E738: Can't list variables for %s"), arg);
1520 }
1521 }
1522 else
1523 {
1524 if (STRCMP("b:changedtick", arg) == 0)
1525 {
1526 char_u numbuf[NUMBUFLEN];
1527
1528 sprintf((char *)numbuf, "%ld",
1529 (long)curbuf->b_changedtick);
1530 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1531 VAR_NUMBER, numbuf);
1532 }
1533 else 1445 else
1534 { 1446 {
1535 varp = find_var(arg, NULL); 1447 /* handle d.key, l[idx], f(expr) */
1536 if (varp == NULL) 1448 arg_subsc = arg;
1537 { 1449 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
1538 /* Skip further arguments but do continue to
1539 * search for a trailing command. */
1540 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1541 error = TRUE; 1450 error = TRUE;
1542 }
1543 else 1451 else
1544 { 1452 {
1545 name = vim_strchr(arg, ':'); 1453 if (arg == arg_subsc && len == 2 && name[1] == ':')
1546 if (name != NULL)
1547 { 1454 {
1548 /* "a:" vars have no name stored, use whole arg */ 1455 switch (*name)
1549 if (arg[0] == 'a' && arg[1] == ':')
1550 c2 = NUL;
1551 else
1552 { 1456 {
1553 c2 = *++name; 1457 case 'g': list_glob_vars(); break;
1554 *name = NUL; 1458 case 'b': list_buf_vars(); break;
1459 case 'w': list_win_vars(); break;
1460 case 'v': list_vim_vars(); break;
1461 default:
1462 EMSG2(_("E738: Can't list variables for %s"), name);
1555 } 1463 }
1556 list_one_var(varp, arg);
1557 if (c2 != NUL)
1558 *name = c2;
1559 } 1464 }
1560 else 1465 else
1561 list_one_var(varp, (char_u *)""); 1466 {
1467 char_u numbuf[NUMBUFLEN];
1468 char_u *tf;
1469 int c;
1470 char_u *s;
1471
1472 s = echo_string(&tv, &tf, numbuf);
1473 c = *arg;
1474 *arg = NUL;
1475 list_one_var_a((char_u *)"",
1476 arg == arg_subsc ? name : name_start,
1477 tv.v_type, s == NULL ? (char_u *)"" : s);
1478 *arg = c;
1479 vim_free(tf);
1480 }
1481 clear_tv(&tv);
1562 } 1482 }
1563 } 1483 }
1564 } 1484 }
1565 if (expr_start != NULL) 1485
1566 vim_free(temp_string); 1486 vim_free(tofree);
1567 else 1487 }
1568 *name_end = c1; 1488
1569 } 1489 arg = skipwhite(arg);
1570 arg = skipwhite(name_end);
1571 } 1490 }
1572 1491
1573 return arg; 1492 return arg;
1574 } 1493 }
1575 1494
2109 if (op != NULL && *op != '=') 2028 if (op != NULL && *op != '=')
2110 { 2029 {
2111 typval_T tv; 2030 typval_T tv;
2112 2031
2113 /* handle +=, -= and .= */ 2032 /* handle +=, -= and .= */
2114 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), &tv) == OK) 2033 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2034 &tv, TRUE) == OK)
2115 { 2035 {
2116 if (tv_op(&tv, rettv, op) == OK) 2036 if (tv_op(&tv, rettv, op) == OK)
2117 set_var(lp->ll_name, &tv, FALSE); 2037 set_var(lp->ll_name, &tv, FALSE);
2118 clear_tv(&tv); 2038 clear_tv(&tv);
2119 } 2039 }
2442 char_u *p; 2362 char_u *p;
2443 2363
2444 if (cmdidx == CMD_let) 2364 if (cmdidx == CMD_let)
2445 { 2365 {
2446 xp->xp_context = EXPAND_USER_VARS; 2366 xp->xp_context = EXPAND_USER_VARS;
2447 if (vim_strchr(arg, '=') == NULL) 2367 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
2448 { 2368 {
2449 /* ":let var1 var2 ...": find last space. */ 2369 /* ":let var1 var2 ...": find last space. */
2450 for (p = arg + STRLEN(arg); p > arg; ) 2370 for (p = arg + STRLEN(arg); p >= arg; )
2451 { 2371 {
2452 xp->xp_pattern = p; 2372 xp->xp_pattern = p;
2453 mb_ptr_back(arg, p); 2373 mb_ptr_back(arg, p);
2454 if (vim_iswhite(*p)) 2374 if (vim_iswhite(*p))
2455 break; 2375 break;
3841 char_u *s; 3761 char_u *s;
3842 int val; 3762 int val;
3843 char_u *start_leader, *end_leader; 3763 char_u *start_leader, *end_leader;
3844 int ret = OK; 3764 int ret = OK;
3845 char_u *alias; 3765 char_u *alias;
3846 dict_T *selfdict;
3847 3766
3848 /* 3767 /*
3849 * Initialise variable so that clear_tv() can't mistake this for a 3768 * Initialise variable so that clear_tv() can't mistake this for a
3850 * string and free a string that isn't there. 3769 * string and free a string that isn't there.
3851 */ 3770 */
3956 /* 3875 /*
3957 * Must be a variable or function name. 3876 * Must be a variable or function name.
3958 * Can also be a curly-braces kind of name: {expr}. 3877 * Can also be a curly-braces kind of name: {expr}.
3959 */ 3878 */
3960 s = *arg; 3879 s = *arg;
3961 len = get_name_len(arg, &alias, evaluate); 3880 len = get_name_len(arg, &alias, evaluate, TRUE);
3962 if (alias != NULL) 3881 if (alias != NULL)
3963 s = alias; 3882 s = alias;
3964 3883
3965 if (len == 0) 3884 if (len <= 0)
3966 ret = FAIL; 3885 ret = FAIL;
3967 else 3886 else
3968 { 3887 {
3969 if (**arg == '(') /* recursive! */ 3888 if (**arg == '(') /* recursive! */
3970 { 3889 {
3985 clear_tv(rettv); 3904 clear_tv(rettv);
3986 ret = FAIL; 3905 ret = FAIL;
3987 } 3906 }
3988 } 3907 }
3989 else if (evaluate) 3908 else if (evaluate)
3990 ret = get_var_tv(s, len, rettv); 3909 ret = get_var_tv(s, len, rettv, TRUE);
3991 else 3910 else
3992 ret = OK; 3911 ret = OK;
3993 } 3912 }
3994 3913
3995 if (alias != NULL) 3914 if (alias != NULL)
3996 vim_free(alias); 3915 vim_free(alias);
3997 } 3916 }
3998 3917
3999 *arg = skipwhite(*arg); 3918 *arg = skipwhite(*arg);
4000 3919
4001 /* 3920 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4002 * Handle expr[expr], expr[expr:expr] subscript and .name lookup. 3921 * expr(expr). */
4003 * Also handle function call with Funcref variable: func(expr) 3922 if (ret == OK)
4004 * Can all be combined: dict.func(expr)[idx]['func'](expr) 3923 ret = handle_subscript(arg, rettv, evaluate, TRUE);
4005 */
4006 selfdict = NULL;
4007 while (ret == OK
4008 && (**arg == '['
4009 || (**arg == '.' && rettv->v_type == VAR_DICT)
4010 || (**arg == '(' && rettv->v_type == VAR_FUNC))
4011 && !vim_iswhite(*(*arg - 1)))
4012 {
4013 if (**arg == '(')
4014 {
4015 s = rettv->vval.v_string;
4016
4017 /* Invoke the function. Recursive! */
4018 ret = get_func_tv(s, STRLEN(s), rettv, arg,
4019 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4020 &len, evaluate, selfdict);
4021
4022 /* Stop the expression evaluation when immediately aborting on
4023 * error, or when an interrupt occurred or an exception was thrown
4024 * but not caught. */
4025 if (aborting())
4026 {
4027 if (ret == OK)
4028 clear_tv(rettv);
4029 ret = FAIL;
4030 }
4031 dict_unref(selfdict);
4032 selfdict = NULL;
4033 }
4034 else /* **arg == '[' || **arg == '.' */
4035 {
4036 dict_unref(selfdict);
4037 if (rettv->v_type == VAR_DICT)
4038 {
4039 selfdict = rettv->vval.v_dict;
4040 if (selfdict != NULL)
4041 ++selfdict->dv_refcount;
4042 }
4043 else
4044 selfdict = NULL;
4045 if (eval_index(arg, rettv, evaluate) == FAIL)
4046 {
4047 clear_tv(rettv);
4048 ret = FAIL;
4049 }
4050 }
4051 }
4052 dict_unref(selfdict);
4053 3924
4054 /* 3925 /*
4055 * Apply logical NOT and unary '-', from right to left, ignore '+'. 3926 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4056 */ 3927 */
4057 if (ret == OK && evaluate && end_leader > start_leader) 3928 if (ret == OK && evaluate && end_leader > start_leader)
4077 * Evaluate an "[expr]" or "[expr:expr]" index. 3948 * Evaluate an "[expr]" or "[expr:expr]" index.
4078 * "*arg" points to the '['. 3949 * "*arg" points to the '['.
4079 * Returns FAIL or OK. "*arg" is advanced to after the ']'. 3950 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4080 */ 3951 */
4081 static int 3952 static int
4082 eval_index(arg, rettv, evaluate) 3953 eval_index(arg, rettv, evaluate, verbose)
4083 char_u **arg; 3954 char_u **arg;
4084 typval_T *rettv; 3955 typval_T *rettv;
4085 int evaluate; 3956 int evaluate;
3957 int verbose; /* give error messages */
4086 { 3958 {
4087 int empty1 = FALSE, empty2 = FALSE; 3959 int empty1 = FALSE, empty2 = FALSE;
4088 typval_T var1, var2; 3960 typval_T var1, var2;
4089 long n1, n2 = 0; 3961 long n1, n2 = 0;
4090 long len = -1; 3962 long len = -1;
4092 char_u *s; 3964 char_u *s;
4093 char_u *key = NULL; 3965 char_u *key = NULL;
4094 3966
4095 if (rettv->v_type == VAR_FUNC) 3967 if (rettv->v_type == VAR_FUNC)
4096 { 3968 {
4097 EMSG(_("E695: Cannot index a Funcref")); 3969 if (verbose)
3970 EMSG(_("E695: Cannot index a Funcref"));
4098 return FAIL; 3971 return FAIL;
4099 } 3972 }
4100 3973
4101 if (**arg == '.') 3974 if (**arg == '.')
4102 { 3975 {
4140 } 4013 }
4141 4014
4142 /* Check for the ']'. */ 4015 /* Check for the ']'. */
4143 if (**arg != ']') 4016 if (**arg != ']')
4144 { 4017 {
4145 EMSG(_(e_missbrac)); 4018 if (verbose)
4019 EMSG(_(e_missbrac));
4146 clear_tv(&var1); 4020 clear_tv(&var1);
4147 if (range) 4021 if (range)
4148 clear_tv(&var2); 4022 clear_tv(&var2);
4149 return FAIL; 4023 return FAIL;
4150 } 4024 }
4214 len = list_len(rettv->vval.v_list); 4088 len = list_len(rettv->vval.v_list);
4215 if (n1 < 0) 4089 if (n1 < 0)
4216 n1 = len + n1; 4090 n1 = len + n1;
4217 if (!empty1 && (n1 < 0 || n1 >= len)) 4091 if (!empty1 && (n1 < 0 || n1 >= len))
4218 { 4092 {
4219 EMSGN(_(e_listidx), n1); 4093 if (verbose)
4094 EMSGN(_(e_listidx), n1);
4220 return FAIL; 4095 return FAIL;
4221 } 4096 }
4222 if (range) 4097 if (range)
4223 { 4098 {
4224 list_T *l; 4099 list_T *l;
4226 4101
4227 if (n2 < 0) 4102 if (n2 < 0)
4228 n2 = len + n2; 4103 n2 = len + n2;
4229 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1)) 4104 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
4230 { 4105 {
4231 EMSGN(_(e_listidx), n2); 4106 if (verbose)
4107 EMSGN(_(e_listidx), n2);
4232 return FAIL; 4108 return FAIL;
4233 } 4109 }
4234 l = list_alloc(); 4110 l = list_alloc();
4235 if (l == NULL) 4111 if (l == NULL)
4236 return FAIL; 4112 return FAIL;
4259 break; 4135 break;
4260 4136
4261 case VAR_DICT: 4137 case VAR_DICT:
4262 if (range) 4138 if (range)
4263 { 4139 {
4264 EMSG(_(e_dictrange)); 4140 if (verbose)
4141 EMSG(_(e_dictrange));
4265 if (len == -1) 4142 if (len == -1)
4266 clear_tv(&var1); 4143 clear_tv(&var1);
4267 return FAIL; 4144 return FAIL;
4268 } 4145 }
4269 { 4146 {
4272 if (len == -1) 4149 if (len == -1)
4273 { 4150 {
4274 key = get_tv_string(&var1); 4151 key = get_tv_string(&var1);
4275 if (*key == NUL) 4152 if (*key == NUL)
4276 { 4153 {
4277 EMSG(_(e_emptykey)); 4154 if (verbose)
4155 EMSG(_(e_emptykey));
4278 clear_tv(&var1); 4156 clear_tv(&var1);
4279 return FAIL; 4157 return FAIL;
4280 } 4158 }
4281 } 4159 }
4282 4160
4283 item = dict_find(rettv->vval.v_dict, key, (int)len); 4161 item = dict_find(rettv->vval.v_dict, key, (int)len);
4284 4162
4285 if (item == NULL) 4163 if (item == NULL && verbose)
4286 EMSG2(_(e_dictkey), key); 4164 EMSG2(_(e_dictkey), key);
4287 if (len == -1) 4165 if (len == -1)
4288 clear_tv(&var1); 4166 clear_tv(&var1);
4289 if (item == NULL) 4167 if (item == NULL)
4290 return FAIL; 4168 return FAIL;
5922 {"map", 2, 2, f_map}, 5800 {"map", 2, 2, f_map},
5923 {"maparg", 1, 2, f_maparg}, 5801 {"maparg", 1, 2, f_maparg},
5924 {"mapcheck", 1, 2, f_mapcheck}, 5802 {"mapcheck", 1, 2, f_mapcheck},
5925 {"match", 2, 4, f_match}, 5803 {"match", 2, 4, f_match},
5926 {"matchend", 2, 4, f_matchend}, 5804 {"matchend", 2, 4, f_matchend},
5805 {"matchlist", 2, 4, f_matchlist},
5927 {"matchstr", 2, 4, f_matchstr}, 5806 {"matchstr", 2, 4, f_matchstr},
5928 {"max", 1, 1, f_max}, 5807 {"max", 1, 1, f_max},
5929 {"min", 1, 1, f_min}, 5808 {"min", 1, 1, f_min},
5930 {"mode", 0, 0, f_mode}, 5809 {"mode", 0, 0, f_mode},
5931 {"nextnonblank", 1, 1, f_nextnonblank}, 5810 {"nextnonblank", 1, 1, f_nextnonblank},
5932 {"nr2char", 1, 1, f_nr2char}, 5811 {"nr2char", 1, 1, f_nr2char},
5933 {"prevnonblank", 1, 1, f_prevnonblank}, 5812 {"prevnonblank", 1, 1, f_prevnonblank},
5934 {"range", 1, 3, f_range}, 5813 {"range", 1, 3, f_range},
5814 {"readfile", 1, 2, f_readfile},
5935 {"remote_expr", 2, 3, f_remote_expr}, 5815 {"remote_expr", 2, 3, f_remote_expr},
5936 {"remote_foreground", 1, 1, f_remote_foreground}, 5816 {"remote_foreground", 1, 1, f_remote_foreground},
5937 {"remote_peek", 1, 2, f_remote_peek}, 5817 {"remote_peek", 1, 2, f_remote_peek},
5938 {"remote_read", 1, 1, f_remote_read}, 5818 {"remote_read", 1, 1, f_remote_read},
5939 {"remote_send", 2, 3, f_remote_send}, 5819 {"remote_send", 2, 3, f_remote_send},
5982 {"winheight", 1, 1, f_winheight}, 5862 {"winheight", 1, 1, f_winheight},
5983 {"winline", 0, 0, f_winline}, 5863 {"winline", 0, 0, f_winline},
5984 {"winnr", 0, 1, f_winnr}, 5864 {"winnr", 0, 1, f_winnr},
5985 {"winrestcmd", 0, 0, f_winrestcmd}, 5865 {"winrestcmd", 0, 0, f_winrestcmd},
5986 {"winwidth", 1, 1, f_winwidth}, 5866 {"winwidth", 1, 1, f_winwidth},
5867 {"writefile", 2, 3, f_writefile},
5987 }; 5868 };
5988 5869
5989 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 5870 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5990 5871
5991 /* 5872 /*
7408 n = au_exists(name, name + STRLEN(name), NULL); 7289 n = au_exists(name, name + STRLEN(name), NULL);
7409 #endif 7290 #endif
7410 } 7291 }
7411 else /* internal variable */ 7292 else /* internal variable */
7412 { 7293 {
7413 char_u *expr_start; 7294 char_u *tofree;
7414 char_u *expr_end; 7295 typval_T tv;
7415 char_u *temp_string = NULL; 7296
7416 char_u *s; 7297 /* get_name_len() takes care of expanding curly braces */
7417 name = p; 7298 name = p;
7418 7299 len = get_name_len(&p, &tofree, TRUE, FALSE);
7419 /* Find the end of the name. */ 7300 if (len > 0)
7420 s = find_name_end(name, &expr_start, &expr_end, FALSE); 7301 {
7421 if (expr_start != NULL) 7302 if (tofree != NULL)
7422 { 7303 name = tofree;
7423 temp_string = make_expanded_name(name, expr_start, expr_end, s); 7304 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7424 if (temp_string != NULL) 7305 if (n)
7425 { 7306 {
7426 len = STRLEN(temp_string); 7307 /* handle d.key, l[idx], f(expr) */
7427 name = temp_string; 7308 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7309 if (n)
7310 clear_tv(&tv);
7428 } 7311 }
7429 } 7312 }
7430 if (len == 0) 7313
7431 len = get_id_len(&p); 7314 vim_free(tofree);
7432 if (len != 0)
7433 n = (get_var_tv(name, len, NULL) == OK);
7434
7435 vim_free(temp_string);
7436 } 7315 }
7437 7316
7438 rettv->vval.v_number = n; 7317 rettv->vval.v_number = n;
7439 } 7318 }
7440 7319
7691 rettv->vval.v_string = NULL; 7570 rettv->vval.v_string = NULL;
7692 #endif 7571 #endif
7693 rettv->v_type = VAR_STRING; 7572 rettv->v_type = VAR_STRING;
7694 } 7573 }
7695 7574
7575 static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
7576 static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
7696 static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map)); 7577 static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7697 static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp)); 7578 static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
7579
7580 /*
7581 * Prepare v: variable "idx" to be used.
7582 * Save the current typeval in "save_tv".
7583 * When not used yet add the variable to the v: hashtable.
7584 */
7585 static void
7586 prepare_vimvar(idx, save_tv)
7587 int idx;
7588 typval_T *save_tv;
7589 {
7590 *save_tv = vimvars[idx].vv_tv;
7591 if (vimvars[idx].vv_type == VAR_UNKNOWN)
7592 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
7593 }
7594
7595 /*
7596 * Restore v: variable "idx" to typeval "save_tv".
7597 * When no longer defined, remove the variable from the v: hashtable.
7598 */
7599 static void
7600 restore_vimvar(idx, save_tv)
7601 int idx;
7602 typval_T *save_tv;
7603 {
7604 hashitem_T *hi;
7605
7606 clear_tv(&vimvars[idx].vv_tv);
7607 vimvars[idx].vv_tv = *save_tv;
7608 if (vimvars[idx].vv_type == VAR_UNKNOWN)
7609 {
7610 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
7611 if (HASHITEM_EMPTY(hi))
7612 EMSG2(_(e_intern2), "restore_vimvar()");
7613 else
7614 hash_remove(&vimvarht, hi);
7615 }
7616 }
7698 7617
7699 /* 7618 /*
7700 * Implementation of map() and filter(). 7619 * Implementation of map() and filter().
7701 */ 7620 */
7702 static void 7621 static void
7737 { 7656 {
7738 EMSG2(_(e_listdictarg), msg); 7657 EMSG2(_(e_listdictarg), msg);
7739 return; 7658 return;
7740 } 7659 }
7741 7660
7661 prepare_vimvar(VV_VAL, &save_val);
7742 expr = skipwhite(get_tv_string_buf(&argvars[1], buf)); 7662 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
7743 save_val = vimvars[VV_VAL].vv_tv;
7744 7663
7745 if (argvars[0].v_type == VAR_DICT) 7664 if (argvars[0].v_type == VAR_DICT)
7746 { 7665 {
7747 save_key = vimvars[VV_KEY].vv_tv; 7666 prepare_vimvar(VV_KEY, &save_key);
7748 vimvars[VV_KEY].vv_type = VAR_STRING; 7667 vimvars[VV_KEY].vv_type = VAR_STRING;
7749 7668
7750 ht = &d->dv_hashtab; 7669 ht = &d->dv_hashtab;
7751 hash_lock(ht); 7670 hash_lock(ht);
7752 todo = ht->ht_used; 7671 todo = ht->ht_used;
7766 clear_tv(&vimvars[VV_KEY].vv_tv); 7685 clear_tv(&vimvars[VV_KEY].vv_tv);
7767 } 7686 }
7768 } 7687 }
7769 hash_unlock(ht); 7688 hash_unlock(ht);
7770 7689
7771 clear_tv(&vimvars[VV_KEY].vv_tv); 7690 restore_vimvar(VV_KEY, &save_key);
7772 vimvars[VV_KEY].vv_tv = save_key;
7773 } 7691 }
7774 else 7692 else
7775 { 7693 {
7776 for (li = l->lv_first; li != NULL; li = nli) 7694 for (li = l->lv_first; li != NULL; li = nli)
7777 { 7695 {
7783 if (!map && rem) 7701 if (!map && rem)
7784 listitem_remove(l, li); 7702 listitem_remove(l, li);
7785 } 7703 }
7786 } 7704 }
7787 7705
7788 clear_tv(&vimvars[VV_VAL].vv_tv); 7706 restore_vimvar(VV_VAL, &save_val);
7789 vimvars[VV_VAL].vv_tv = save_val;
7790 7707
7791 copy_tv(&argvars[0], rettv); 7708 copy_tv(&argvars[0], rettv);
7792 } 7709 }
7793 7710
7794 static int 7711 static int
10273 long nth = 1; 10190 long nth = 1;
10274 int match; 10191 int match;
10275 list_T *l = NULL; 10192 list_T *l = NULL;
10276 listitem_T *li = NULL; 10193 listitem_T *li = NULL;
10277 long idx = 0; 10194 long idx = 0;
10278 char_u *tofree; 10195 char_u *tofree = NULL;
10279 10196
10280 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 10197 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10281 save_cpo = p_cpo; 10198 save_cpo = p_cpo;
10282 p_cpo = (char_u *)""; 10199 p_cpo = (char_u *)"";
10283 10200
10284 if (type == 2) 10201 rettv->vval.v_number = -1;
10202 if (type == 3)
10203 {
10204 /* return empty list when there are no matches */
10205 if ((rettv->vval.v_list = list_alloc()) == NULL)
10206 goto theend;
10207 rettv->v_type = VAR_LIST;
10208 ++rettv->vval.v_list->lv_refcount;
10209 }
10210 else if (type == 2)
10285 { 10211 {
10286 rettv->v_type = VAR_STRING; 10212 rettv->v_type = VAR_STRING;
10287 rettv->vval.v_string = NULL; 10213 rettv->vval.v_string = NULL;
10288 } 10214 }
10289 else
10290 rettv->vval.v_number = -1;
10291 10215
10292 if (argvars[0].v_type == VAR_LIST) 10216 if (argvars[0].v_type == VAR_LIST)
10293 { 10217 {
10294 if ((l = argvars[0].vval.v_list) == NULL) 10218 if ((l = argvars[0].vval.v_list) == NULL)
10295 goto theend; 10219 goto theend;
10335 if (li == NULL) 10259 if (li == NULL)
10336 { 10260 {
10337 match = FALSE; 10261 match = FALSE;
10338 break; 10262 break;
10339 } 10263 }
10264 vim_free(tofree);
10340 str = echo_string(&li->li_tv, &tofree, strbuf); 10265 str = echo_string(&li->li_tv, &tofree, strbuf);
10341 } 10266 }
10342 10267
10343 match = vim_regexec_nl(&regmatch, str, (colnr_T)0); 10268 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
10344 10269
10345 if (l != NULL)
10346 vim_free(tofree);
10347 if (match && --nth <= 0) 10270 if (match && --nth <= 0)
10348 break; 10271 break;
10349 if (l == NULL && !match) 10272 if (l == NULL && !match)
10350 break; 10273 break;
10351 10274
10365 } 10288 }
10366 } 10289 }
10367 10290
10368 if (match) 10291 if (match)
10369 { 10292 {
10370 if (type == 2) 10293 if (type == 3)
10371 { 10294 {
10295 int i;
10296
10297 /* return list with matched string and submatches */
10298 for (i = 0; i < NSUBEXP; ++i)
10299 {
10300 if (regmatch.endp[i] == NULL)
10301 break;
10302 li = listitem_alloc();
10303 if (li == NULL)
10304 break;
10305 li->li_tv.v_type = VAR_STRING;
10306 li->li_tv.v_lock = 0;
10307 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10308 (int)(regmatch.endp[i] - regmatch.startp[i]));
10309 list_append(rettv->vval.v_list, li);
10310 }
10311 }
10312 else if (type == 2)
10313 {
10314 /* return matched string */
10372 if (l != NULL) 10315 if (l != NULL)
10373 copy_tv(&li->li_tv, rettv); 10316 copy_tv(&li->li_tv, rettv);
10374 else 10317 else
10375 rettv->vval.v_string = vim_strnsave(regmatch.startp[0], 10318 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
10376 (int)(regmatch.endp[0] - regmatch.startp[0])); 10319 (int)(regmatch.endp[0] - regmatch.startp[0]));
10390 } 10333 }
10391 vim_free(regmatch.regprog); 10334 vim_free(regmatch.regprog);
10392 } 10335 }
10393 10336
10394 theend: 10337 theend:
10338 vim_free(tofree);
10395 p_cpo = save_cpo; 10339 p_cpo = save_cpo;
10396 } 10340 }
10397 10341
10398 /* 10342 /*
10399 * "match()" function 10343 * "match()" function
10413 f_matchend(argvars, rettv) 10357 f_matchend(argvars, rettv)
10414 typval_T *argvars; 10358 typval_T *argvars;
10415 typval_T *rettv; 10359 typval_T *rettv;
10416 { 10360 {
10417 find_some_match(argvars, rettv, 0); 10361 find_some_match(argvars, rettv, 0);
10362 }
10363
10364 /*
10365 * "matchlist()" function
10366 */
10367 static void
10368 f_matchlist(argvars, rettv)
10369 typval_T *argvars;
10370 typval_T *rettv;
10371 {
10372 find_some_match(argvars, rettv, 3);
10418 } 10373 }
10419 10374
10420 /* 10375 /*
10421 * "matchstr()" function 10376 * "matchstr()" function
10422 */ 10377 */
10676 list_append(l, li); 10631 list_append(l, li);
10677 } 10632 }
10678 } 10633 }
10679 } 10634 }
10680 } 10635 }
10636
10637 /*
10638 * "readfile()" function
10639 */
10640 static void
10641 f_readfile(argvars, rettv)
10642 typval_T *argvars;
10643 typval_T *rettv;
10644 {
10645 int binary = FALSE;
10646 char_u *fname;
10647 FILE *fd;
10648 list_T *l;
10649 listitem_T *li;
10650 #define FREAD_SIZE 200 /* optimized for text lines */
10651 char_u buf[FREAD_SIZE];
10652 int readlen; /* size of last fread() */
10653 int buflen; /* nr of valid chars in buf[] */
10654 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
10655 int tolist; /* first byte in buf[] still to be put in list */
10656 int chop; /* how many CR to chop off */
10657 char_u *prev = NULL; /* previously read bytes, if any */
10658 int prevlen = 0; /* length of "prev" if not NULL */
10659 char_u *s;
10660 int len;
10661
10662 if (argvars[1].v_type != VAR_UNKNOWN
10663 && STRCMP(get_tv_string(&argvars[1]), "b") == 0)
10664 binary = TRUE;
10665
10666 l = list_alloc();
10667 if (l == NULL)
10668 return;
10669 rettv->v_type = VAR_LIST;
10670 rettv->vval.v_list = l;
10671 l->lv_refcount = 1;
10672
10673 /* Always open the file in binary mode, library functions have a mind of
10674 * their own about CR-LF conversion. */
10675 fname = get_tv_string(&argvars[0]);
10676 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
10677 {
10678 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
10679 return;
10680 }
10681
10682 filtd = 0;
10683 for (;;)
10684 {
10685 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
10686 buflen = filtd + readlen;
10687 tolist = 0;
10688 for ( ; filtd < buflen || readlen <= 0; ++filtd)
10689 {
10690 if (buf[filtd] == '\n' || readlen <= 0)
10691 {
10692 /* Only when in binary mode add an empty list item when the
10693 * last line ends in a '\n'. */
10694 if (!binary && readlen == 0 && filtd == 0)
10695 break;
10696
10697 /* Found end-of-line or end-of-file: add a text line to the
10698 * list. */
10699 chop = 0;
10700 if (!binary)
10701 while (filtd - chop - 1 >= tolist
10702 && buf[filtd - chop - 1] == '\r')
10703 ++chop;
10704 len = filtd - tolist - chop;
10705 if (prev == NULL)
10706 s = vim_strnsave(buf + tolist, len);
10707 else
10708 {
10709 s = alloc((unsigned)(prevlen + len + 1));
10710 if (s != NULL)
10711 {
10712 mch_memmove(s, prev, prevlen);
10713 vim_free(prev);
10714 prev = NULL;
10715 mch_memmove(s + prevlen, buf + tolist, len);
10716 s[prevlen + len] = NUL;
10717 }
10718 }
10719 tolist = filtd + 1;
10720
10721 li = listitem_alloc();
10722 if (li == NULL)
10723 {
10724 vim_free(s);
10725 break;
10726 }
10727 li->li_tv.v_type = VAR_STRING;
10728 li->li_tv.v_lock = 0;
10729 li->li_tv.vval.v_string = s;
10730 list_append(l, li);
10731
10732 if (readlen <= 0)
10733 break;
10734 }
10735 else if (buf[filtd] == NUL)
10736 buf[filtd] = '\n';
10737 }
10738 if (readlen <= 0)
10739 break;
10740
10741 if (tolist == 0)
10742 {
10743 /* "buf" is full, need to move text to an allocated buffer */
10744 if (prev == NULL)
10745 {
10746 prev = vim_strnsave(buf, buflen);
10747 prevlen = buflen;
10748 }
10749 else
10750 {
10751 s = alloc((unsigned)(prevlen + buflen));
10752 if (s != NULL)
10753 {
10754 mch_memmove(s, prev, prevlen);
10755 mch_memmove(s + prevlen, buf, buflen);
10756 vim_free(prev);
10757 prev = s;
10758 prevlen += buflen;
10759 }
10760 }
10761 filtd = 0;
10762 }
10763 else
10764 {
10765 mch_memmove(buf, buf + tolist, buflen - tolist);
10766 filtd -= tolist;
10767 }
10768 }
10769
10770 fclose(fd);
10771 }
10772
10681 10773
10682 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) 10774 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
10683 static void make_connection __ARGS((void)); 10775 static void make_connection __ARGS((void));
10684 static int check_connection __ARGS((void)); 10776 static int check_connection __ARGS((void));
10685 10777
13053 return NULL; 13145 return NULL;
13054 #endif 13146 #endif
13055 } 13147 }
13056 13148
13057 /* 13149 /*
13150 * "writefile()" function
13151 */
13152 static void
13153 f_writefile(argvars, rettv)
13154 typval_T *argvars;
13155 typval_T *rettv;
13156 {
13157 int binary = FALSE;
13158 char_u *fname;
13159 FILE *fd;
13160 listitem_T *li;
13161 char_u *s;
13162 int ret = 0;
13163 int c;
13164
13165 if (argvars[0].v_type != VAR_LIST)
13166 {
13167 EMSG2(_(e_listarg), "writefile()");
13168 return;
13169 }
13170 if (argvars[0].vval.v_list == NULL)
13171 return;
13172
13173 if (argvars[2].v_type != VAR_UNKNOWN
13174 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13175 binary = TRUE;
13176
13177 /* Always open the file in binary mode, library functions have a mind of
13178 * their own about CR-LF conversion. */
13179 fname = get_tv_string(&argvars[1]);
13180 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13181 {
13182 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13183 ret = -1;
13184 }
13185 else
13186 {
13187 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13188 li = li->li_next)
13189 {
13190 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13191 {
13192 if (*s == '\n')
13193 c = putc(NUL, fd);
13194 else
13195 c = putc(*s, fd);
13196 if (c == EOF)
13197 {
13198 ret = -1;
13199 break;
13200 }
13201 }
13202 if (!binary || li->li_next != NULL)
13203 if (putc('\n', fd) == EOF)
13204 {
13205 ret = -1;
13206 break;
13207 }
13208 if (ret < 0)
13209 {
13210 EMSG(_(e_write));
13211 break;
13212 }
13213 }
13214 fclose(fd);
13215 }
13216
13217 rettv->vval.v_number = ret;
13218 }
13219
13220 /*
13058 * Translate a String variable into a position. 13221 * Translate a String variable into a position.
13059 */ 13222 */
13060 static pos_T * 13223 static pos_T *
13061 var2fpos(varp, lnum) 13224 var2fpos(varp, lnum)
13062 typval_T *varp; 13225 typval_T *varp;
13141 13304
13142 /* 13305 /*
13143 * Get the length of the name of a variable or function. 13306 * Get the length of the name of a variable or function.
13144 * Only the name is recognized, does not handle ".key" or "[idx]". 13307 * Only the name is recognized, does not handle ".key" or "[idx]".
13145 * "arg" is advanced to the first non-white character after the name. 13308 * "arg" is advanced to the first non-white character after the name.
13146 * Return 0 if something is wrong. 13309 * Return -1 if curly braces expansion failed.
13310 * Return 0 if something else is wrong.
13147 * If the name contains 'magic' {}'s, expand them and return the 13311 * If the name contains 'magic' {}'s, expand them and return the
13148 * expanded name in an allocated string via 'alias' - caller must free. 13312 * expanded name in an allocated string via 'alias' - caller must free.
13149 */ 13313 */
13150 static int 13314 static int
13151 get_name_len(arg, alias, evaluate) 13315 get_name_len(arg, alias, evaluate, verbose)
13152 char_u **arg; 13316 char_u **arg;
13153 char_u **alias; 13317 char_u **alias;
13154 int evaluate; 13318 int evaluate;
13319 int verbose;
13155 { 13320 {
13156 int len; 13321 int len;
13157 char_u *p; 13322 char_u *p;
13158 char_u *expr_start; 13323 char_u *expr_start;
13159 char_u *expr_end; 13324 char_u *expr_end;
13193 * Include any <SID> etc in the expanded string: 13358 * Include any <SID> etc in the expanded string:
13194 * Thus the -len here. 13359 * Thus the -len here.
13195 */ 13360 */
13196 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p); 13361 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13197 if (temp_string == NULL) 13362 if (temp_string == NULL)
13198 return 0; 13363 return -1;
13199 *alias = temp_string; 13364 *alias = temp_string;
13200 *arg = skipwhite(p); 13365 *arg = skipwhite(p);
13201 return (int)STRLEN(temp_string); 13366 return (int)STRLEN(temp_string);
13202 } 13367 }
13203 13368
13204 len += get_id_len(arg); 13369 len += get_id_len(arg);
13205 if (len == 0) 13370 if (len == 0 && verbose)
13206 EMSG2(_(e_invexpr2), *arg); 13371 EMSG2(_(e_invexpr2), *arg);
13207 13372
13208 return len; 13373 return len;
13209 } 13374 }
13210 13375
13262 } 13427 }
13263 } 13428 }
13264 } 13429 }
13265 13430
13266 return p; 13431 return p;
13432 }
13433
13434 /*
13435 * Expands out the 'magic' {}'s in a variable/function name.
13436 * Note that this can call itself recursively, to deal with
13437 * constructs like foo{bar}{baz}{bam}
13438 * The four pointer arguments point to "foo{expre}ss{ion}bar"
13439 * "in_start" ^
13440 * "expr_start" ^
13441 * "expr_end" ^
13442 * "in_end" ^
13443 *
13444 * Returns a new allocated string, which the caller must free.
13445 * Returns NULL for failure.
13446 */
13447 static char_u *
13448 make_expanded_name(in_start, expr_start, expr_end, in_end)
13449 char_u *in_start;
13450 char_u *expr_start;
13451 char_u *expr_end;
13452 char_u *in_end;
13453 {
13454 char_u c1;
13455 char_u *retval = NULL;
13456 char_u *temp_result;
13457 char_u *nextcmd = NULL;
13458
13459 if (expr_end == NULL || in_end == NULL)
13460 return NULL;
13461 *expr_start = NUL;
13462 *expr_end = NUL;
13463 c1 = *in_end;
13464 *in_end = NUL;
13465
13466 temp_result = eval_to_string(expr_start + 1, &nextcmd);
13467 if (temp_result != NULL && nextcmd == NULL)
13468 {
13469 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
13470 + (in_end - expr_end) + 1));
13471 if (retval != NULL)
13472 {
13473 STRCPY(retval, in_start);
13474 STRCAT(retval, temp_result);
13475 STRCAT(retval, expr_end + 1);
13476 }
13477 }
13478 vim_free(temp_result);
13479
13480 *in_end = c1; /* put char back for error messages */
13481 *expr_start = '{';
13482 *expr_end = '}';
13483
13484 if (retval != NULL)
13485 {
13486 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
13487 if (expr_start != NULL)
13488 {
13489 /* Further expansion! */
13490 temp_result = make_expanded_name(retval, expr_start,
13491 expr_end, temp_result);
13492 vim_free(retval);
13493 retval = temp_result;
13494 }
13495 }
13496
13497 return retval;
13267 } 13498 }
13268 13499
13269 /* 13500 /*
13270 * Return TRUE if character "c" can be used in a variable or function name. 13501 * Return TRUE if character "c" can be used in a variable or function name.
13271 * Does not include '{' or '}' for magic braces. 13502 * Does not include '{' or '}' for magic braces.
13448 /* 13679 /*
13449 * Get the value of internal variable "name". 13680 * Get the value of internal variable "name".
13450 * Return OK or FAIL. 13681 * Return OK or FAIL.
13451 */ 13682 */
13452 static int 13683 static int
13453 get_var_tv(name, len, rettv) 13684 get_var_tv(name, len, rettv, verbose)
13454 char_u *name; 13685 char_u *name;
13455 int len; /* length of "name" */ 13686 int len; /* length of "name" */
13456 typval_T *rettv; /* NULL when only checking existence */ 13687 typval_T *rettv; /* NULL when only checking existence */
13688 int verbose; /* may give error message */
13457 { 13689 {
13458 int ret = OK; 13690 int ret = OK;
13459 typval_T *tv = NULL; 13691 typval_T *tv = NULL;
13460 typval_T atv; 13692 typval_T atv;
13461 dictitem_T *v; 13693 dictitem_T *v;
13485 tv = &v->di_tv; 13717 tv = &v->di_tv;
13486 } 13718 }
13487 13719
13488 if (tv == NULL) 13720 if (tv == NULL)
13489 { 13721 {
13490 if (rettv != NULL) 13722 if (rettv != NULL && verbose)
13491 EMSG2(_(e_undefvar), name); 13723 EMSG2(_(e_undefvar), name);
13492 ret = FAIL; 13724 ret = FAIL;
13493 } 13725 }
13494 else if (rettv != NULL) 13726 else if (rettv != NULL)
13495 copy_tv(tv, rettv); 13727 copy_tv(tv, rettv);
13496 13728
13497 name[len] = cc; 13729 name[len] = cc;
13498 13730
13731 return ret;
13732 }
13733
13734 /*
13735 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
13736 * Also handle function call with Funcref variable: func(expr)
13737 * Can all be combined: dict.func(expr)[idx]['func'](expr)
13738 */
13739 static int
13740 handle_subscript(arg, rettv, evaluate, verbose)
13741 char_u **arg;
13742 typval_T *rettv;
13743 int evaluate; /* do more than finding the end */
13744 int verbose; /* give error messages */
13745 {
13746 int ret = OK;
13747 dict_T *selfdict = NULL;
13748 char_u *s;
13749 int len;
13750
13751 while (ret == OK
13752 && (**arg == '['
13753 || (**arg == '.' && rettv->v_type == VAR_DICT)
13754 || (**arg == '(' && rettv->v_type == VAR_FUNC))
13755 && !vim_iswhite(*(*arg - 1)))
13756 {
13757 if (**arg == '(')
13758 {
13759 s = rettv->vval.v_string;
13760
13761 /* Invoke the function. Recursive! */
13762 ret = get_func_tv(s, STRLEN(s), rettv, arg,
13763 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
13764 &len, evaluate, selfdict);
13765
13766 /* Stop the expression evaluation when immediately aborting on
13767 * error, or when an interrupt occurred or an exception was thrown
13768 * but not caught. */
13769 if (aborting())
13770 {
13771 if (ret == OK)
13772 clear_tv(rettv);
13773 ret = FAIL;
13774 }
13775 dict_unref(selfdict);
13776 selfdict = NULL;
13777 }
13778 else /* **arg == '[' || **arg == '.' */
13779 {
13780 dict_unref(selfdict);
13781 if (rettv->v_type == VAR_DICT)
13782 {
13783 selfdict = rettv->vval.v_dict;
13784 if (selfdict != NULL)
13785 ++selfdict->dv_refcount;
13786 }
13787 else
13788 selfdict = NULL;
13789 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
13790 {
13791 clear_tv(rettv);
13792 ret = FAIL;
13793 }
13794 }
13795 }
13796 dict_unref(selfdict);
13499 return ret; 13797 return ret;
13500 } 13798 }
13501 13799
13502 /* 13800 /*
13503 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL 13801 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL