Mercurial > vim
comparison src/eval.c @ 3618:c052f3b79b99 v7.3.569
updated for version 7.3.569
Problem: Evaluating Vim expression in Python is insufficient.
Solution: Add vim.bindeval(). Also add pyeval() and py3eval(). (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Fri, 29 Jun 2012 12:54:53 +0200 |
parents | 0abd38ba6455 |
children | 46896c29edd7 |
comparison
equal
deleted
inserted
replaced
3617:74d62fbf05c9 | 3618:c052f3b79b99 |
---|---|
422 static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); | 422 static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); |
423 static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); | 423 static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); |
424 static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); | 424 static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); |
425 static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); | 425 static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); |
426 static int rettv_list_alloc __ARGS((typval_T *rettv)); | 426 static int rettv_list_alloc __ARGS((typval_T *rettv)); |
427 static listitem_T *listitem_alloc __ARGS((void)); | |
428 static void listitem_free __ARGS((listitem_T *item)); | 427 static void listitem_free __ARGS((listitem_T *item)); |
429 static void listitem_remove __ARGS((list_T *l, listitem_T *item)); | |
430 static long list_len __ARGS((list_T *l)); | 428 static long list_len __ARGS((list_T *l)); |
431 static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive)); | 429 static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive)); |
432 static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive)); | 430 static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive)); |
433 static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive)); | 431 static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive)); |
434 static listitem_T *list_find __ARGS((list_T *l, long n)); | |
435 static long list_find_nr __ARGS((list_T *l, long idx, int *errorp)); | 432 static long list_find_nr __ARGS((list_T *l, long idx, int *errorp)); |
436 static long list_idx_of_item __ARGS((list_T *l, listitem_T *item)); | 433 static long list_idx_of_item __ARGS((list_T *l, listitem_T *item)); |
437 static void list_append __ARGS((list_T *l, listitem_T *item)); | |
438 static int list_append_number __ARGS((list_T *l, varnumber_T n)); | 434 static int list_append_number __ARGS((list_T *l, varnumber_T n)); |
439 static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item)); | |
440 static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef)); | 435 static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef)); |
441 static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv)); | 436 static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv)); |
442 static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID)); | 437 static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID)); |
443 static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2)); | |
444 static char_u *list2string __ARGS((typval_T *tv, int copyID)); | 438 static char_u *list2string __ARGS((typval_T *tv, int copyID)); |
445 static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap)); | 439 static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap)); |
446 static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID)); | 440 static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID)); |
447 static int free_unref_items __ARGS((int copyID)); | 441 static int free_unref_items __ARGS((int copyID)); |
448 static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID)); | |
449 static void set_ref_in_list __ARGS((list_T *l, int copyID)); | |
450 static void set_ref_in_item __ARGS((typval_T *tv, int copyID)); | |
451 static int rettv_dict_alloc __ARGS((typval_T *rettv)); | 442 static int rettv_dict_alloc __ARGS((typval_T *rettv)); |
452 static void dict_free __ARGS((dict_T *d, int recurse)); | 443 static void dict_free __ARGS((dict_T *d, int recurse)); |
453 static dictitem_T *dictitem_copy __ARGS((dictitem_T *org)); | 444 static dictitem_T *dictitem_copy __ARGS((dictitem_T *org)); |
454 static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item)); | 445 static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item)); |
455 static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID)); | 446 static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID)); |
652 static void f_pow __ARGS((typval_T *argvars, typval_T *rettv)); | 643 static void f_pow __ARGS((typval_T *argvars, typval_T *rettv)); |
653 #endif | 644 #endif |
654 static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv)); | 645 static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv)); |
655 static void f_printf __ARGS((typval_T *argvars, typval_T *rettv)); | 646 static void f_printf __ARGS((typval_T *argvars, typval_T *rettv)); |
656 static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv)); | 647 static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv)); |
648 #ifdef FEAT_PYTHON3 | |
649 static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv)); | |
650 #endif | |
651 #ifdef FEAT_PYTHON | |
652 static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv)); | |
653 #endif | |
657 static void f_range __ARGS((typval_T *argvars, typval_T *rettv)); | 654 static void f_range __ARGS((typval_T *argvars, typval_T *rettv)); |
658 static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv)); | 655 static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv)); |
659 static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv)); | 656 static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv)); |
660 static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv)); | 657 static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv)); |
661 static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv)); | 658 static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv)); |
822 #endif | 819 #endif |
823 static int script_autoload __ARGS((char_u *name, int reload)); | 820 static int script_autoload __ARGS((char_u *name, int reload)); |
824 static char_u *autoload_name __ARGS((char_u *name)); | 821 static char_u *autoload_name __ARGS((char_u *name)); |
825 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); | 822 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); |
826 static void func_free __ARGS((ufunc_T *fp)); | 823 static void func_free __ARGS((ufunc_T *fp)); |
827 static void func_unref __ARGS((char_u *name)); | |
828 static void func_ref __ARGS((char_u *name)); | |
829 static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict)); | 824 static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict)); |
830 static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ; | 825 static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ; |
831 static void free_funccal __ARGS((funccall_T *fc, int free_val)); | 826 static void free_funccal __ARGS((funccall_T *fc, int free_val)); |
832 static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr)); | 827 static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr)); |
833 static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp)); | 828 static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp)); |
5925 } | 5920 } |
5926 | 5921 |
5927 /* | 5922 /* |
5928 * Allocate a list item. | 5923 * Allocate a list item. |
5929 */ | 5924 */ |
5930 static listitem_T * | 5925 listitem_T * |
5931 listitem_alloc() | 5926 listitem_alloc() |
5932 { | 5927 { |
5933 return (listitem_T *)alloc(sizeof(listitem_T)); | 5928 return (listitem_T *)alloc(sizeof(listitem_T)); |
5934 } | 5929 } |
5935 | 5930 |
5945 } | 5940 } |
5946 | 5941 |
5947 /* | 5942 /* |
5948 * Remove a list item from a List and free it. Also clears the value. | 5943 * Remove a list item from a List and free it. Also clears the value. |
5949 */ | 5944 */ |
5950 static void | 5945 void |
5951 listitem_remove(l, item) | 5946 listitem_remove(l, item) |
5952 list_T *l; | 5947 list_T *l; |
5953 listitem_T *item; | 5948 listitem_T *item; |
5954 { | 5949 { |
5955 list_remove(l, item, item); | 5950 list_remove(l, item, item); |
6121 /* | 6116 /* |
6122 * Locate item with index "n" in list "l" and return it. | 6117 * Locate item with index "n" in list "l" and return it. |
6123 * A negative index is counted from the end; -1 is the last item. | 6118 * A negative index is counted from the end; -1 is the last item. |
6124 * Returns NULL when "n" is out of range. | 6119 * Returns NULL when "n" is out of range. |
6125 */ | 6120 */ |
6126 static listitem_T * | 6121 listitem_T * |
6127 list_find(l, n) | 6122 list_find(l, n) |
6128 list_T *l; | 6123 list_T *l; |
6129 long n; | 6124 long n; |
6130 { | 6125 { |
6131 listitem_T *item; | 6126 listitem_T *item; |
6263 } | 6258 } |
6264 | 6259 |
6265 /* | 6260 /* |
6266 * Append item "item" to the end of list "l". | 6261 * Append item "item" to the end of list "l". |
6267 */ | 6262 */ |
6268 static void | 6263 void |
6269 list_append(l, item) | 6264 list_append(l, item) |
6270 list_T *l; | 6265 list_T *l; |
6271 listitem_T *item; | 6266 listitem_T *item; |
6272 { | 6267 { |
6273 if (l->lv_last == NULL) | 6268 if (l->lv_last == NULL) |
6376 /* | 6371 /* |
6377 * Insert typval_T "tv" in list "l" before "item". | 6372 * Insert typval_T "tv" in list "l" before "item". |
6378 * If "item" is NULL append at the end. | 6373 * If "item" is NULL append at the end. |
6379 * Return FAIL when out of memory. | 6374 * Return FAIL when out of memory. |
6380 */ | 6375 */ |
6381 static int | 6376 int |
6382 list_insert_tv(l, tv, item) | 6377 list_insert_tv(l, tv, item) |
6383 list_T *l; | 6378 list_T *l; |
6384 typval_T *tv; | 6379 typval_T *tv; |
6385 listitem_T *item; | 6380 listitem_T *item; |
6386 { | 6381 { |
6521 | 6516 |
6522 /* | 6517 /* |
6523 * Remove items "item" to "item2" from list "l". | 6518 * Remove items "item" to "item2" from list "l". |
6524 * Does not free the listitem or the value! | 6519 * Does not free the listitem or the value! |
6525 */ | 6520 */ |
6526 static void | 6521 void |
6527 list_remove(l, item, item2) | 6522 list_remove(l, item, item2) |
6528 list_T *l; | 6523 list_T *l; |
6529 listitem_T *item; | 6524 listitem_T *item; |
6530 listitem_T *item2; | 6525 listitem_T *item2; |
6531 { | 6526 { |
6783 | 6778 |
6784 #ifdef FEAT_LUA | 6779 #ifdef FEAT_LUA |
6785 set_ref_in_lua(copyID); | 6780 set_ref_in_lua(copyID); |
6786 #endif | 6781 #endif |
6787 | 6782 |
6783 #ifdef FEAT_PYTHON | |
6784 set_ref_in_python(copyID); | |
6785 #endif | |
6786 | |
6787 #ifdef FEAT_PYTHON3 | |
6788 set_ref_in_python3(copyID); | |
6789 #endif | |
6790 | |
6788 /* | 6791 /* |
6789 * 2. Free lists and dictionaries that are not referenced. | 6792 * 2. Free lists and dictionaries that are not referenced. |
6790 */ | 6793 */ |
6791 did_free = free_unref_items(copyID); | 6794 did_free = free_unref_items(copyID); |
6792 | 6795 |
6868 } | 6871 } |
6869 | 6872 |
6870 /* | 6873 /* |
6871 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". | 6874 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". |
6872 */ | 6875 */ |
6873 static void | 6876 void |
6874 set_ref_in_ht(ht, copyID) | 6877 set_ref_in_ht(ht, copyID) |
6875 hashtab_T *ht; | 6878 hashtab_T *ht; |
6876 int copyID; | 6879 int copyID; |
6877 { | 6880 { |
6878 int todo; | 6881 int todo; |
6888 } | 6891 } |
6889 | 6892 |
6890 /* | 6893 /* |
6891 * Mark all lists and dicts referenced through list "l" with "copyID". | 6894 * Mark all lists and dicts referenced through list "l" with "copyID". |
6892 */ | 6895 */ |
6893 static void | 6896 void |
6894 set_ref_in_list(l, copyID) | 6897 set_ref_in_list(l, copyID) |
6895 list_T *l; | 6898 list_T *l; |
6896 int copyID; | 6899 int copyID; |
6897 { | 6900 { |
6898 listitem_T *li; | 6901 listitem_T *li; |
6902 } | 6905 } |
6903 | 6906 |
6904 /* | 6907 /* |
6905 * Mark all lists and dicts referenced through typval "tv" with "copyID". | 6908 * Mark all lists and dicts referenced through typval "tv" with "copyID". |
6906 */ | 6909 */ |
6907 static void | 6910 void |
6908 set_ref_in_item(tv, copyID) | 6911 set_ref_in_item(tv, copyID) |
6909 typval_T *tv; | 6912 typval_T *tv; |
6910 int copyID; | 6913 int copyID; |
6911 { | 6914 { |
6912 dict_T *dd; | 6915 dict_T *dd; |
7984 {"pow", 2, 2, f_pow}, | 7987 {"pow", 2, 2, f_pow}, |
7985 #endif | 7988 #endif |
7986 {"prevnonblank", 1, 1, f_prevnonblank}, | 7989 {"prevnonblank", 1, 1, f_prevnonblank}, |
7987 {"printf", 2, 19, f_printf}, | 7990 {"printf", 2, 19, f_printf}, |
7988 {"pumvisible", 0, 0, f_pumvisible}, | 7991 {"pumvisible", 0, 0, f_pumvisible}, |
7992 #ifdef FEAT_PYTHON3 | |
7993 {"py3eval", 1, 1, f_py3eval}, | |
7994 #endif | |
7995 #ifdef FEAT_PYTHON | |
7996 {"pyeval", 1, 1, f_pyeval}, | |
7997 #endif | |
7989 {"range", 1, 3, f_range}, | 7998 {"range", 1, 3, f_range}, |
7990 {"readfile", 1, 3, f_readfile}, | 7999 {"readfile", 1, 3, f_readfile}, |
7991 {"reltime", 0, 2, f_reltime}, | 8000 {"reltime", 0, 2, f_reltime}, |
7992 {"reltimestr", 1, 1, f_reltimestr}, | 8001 {"reltimestr", 1, 1, f_reltimestr}, |
7993 {"remote_expr", 2, 3, f_remote_expr}, | 8002 {"remote_expr", 2, 3, f_remote_expr}, |
9148 if ((size_t)idx <= STRLEN(str)) | 9157 if ((size_t)idx <= STRLEN(str)) |
9149 rettv->vval.v_number = idx; | 9158 rettv->vval.v_number = idx; |
9150 #endif | 9159 #endif |
9151 } | 9160 } |
9152 | 9161 |
9162 int | |
9163 func_call(name, args, selfdict, rettv) | |
9164 char_u *name; | |
9165 typval_T *args; | |
9166 dict_T *selfdict; | |
9167 typval_T *rettv; | |
9168 { | |
9169 listitem_T *item; | |
9170 typval_T argv[MAX_FUNC_ARGS + 1]; | |
9171 int argc = 0; | |
9172 int dummy; | |
9173 int r = 0; | |
9174 | |
9175 for (item = args->vval.v_list->lv_first; item != NULL; | |
9176 item = item->li_next) | |
9177 { | |
9178 if (argc == MAX_FUNC_ARGS) | |
9179 { | |
9180 EMSG(_("E699: Too many arguments")); | |
9181 break; | |
9182 } | |
9183 /* Make a copy of each argument. This is needed to be able to set | |
9184 * v_lock to VAR_FIXED in the copy without changing the original list. | |
9185 */ | |
9186 copy_tv(&item->li_tv, &argv[argc++]); | |
9187 } | |
9188 | |
9189 if (item == NULL) | |
9190 r = call_func(name, (int)STRLEN(name), rettv, argc, argv, | |
9191 curwin->w_cursor.lnum, curwin->w_cursor.lnum, | |
9192 &dummy, TRUE, selfdict); | |
9193 | |
9194 /* Free the arguments. */ | |
9195 while (argc > 0) | |
9196 clear_tv(&argv[--argc]); | |
9197 | |
9198 return r; | |
9199 } | |
9200 | |
9153 /* | 9201 /* |
9154 * "call(func, arglist)" function | 9202 * "call(func, arglist)" function |
9155 */ | 9203 */ |
9156 static void | 9204 static void |
9157 f_call(argvars, rettv) | 9205 f_call(argvars, rettv) |
9158 typval_T *argvars; | 9206 typval_T *argvars; |
9159 typval_T *rettv; | 9207 typval_T *rettv; |
9160 { | 9208 { |
9161 char_u *func; | 9209 char_u *func; |
9162 typval_T argv[MAX_FUNC_ARGS + 1]; | |
9163 int argc = 0; | |
9164 listitem_T *item; | |
9165 int dummy; | |
9166 dict_T *selfdict = NULL; | 9210 dict_T *selfdict = NULL; |
9167 | 9211 |
9168 if (argvars[1].v_type != VAR_LIST) | 9212 if (argvars[1].v_type != VAR_LIST) |
9169 { | 9213 { |
9170 EMSG(_(e_listreq)); | 9214 EMSG(_(e_listreq)); |
9188 return; | 9232 return; |
9189 } | 9233 } |
9190 selfdict = argvars[2].vval.v_dict; | 9234 selfdict = argvars[2].vval.v_dict; |
9191 } | 9235 } |
9192 | 9236 |
9193 for (item = argvars[1].vval.v_list->lv_first; item != NULL; | 9237 (void)func_call(func, &argvars[1], selfdict, rettv); |
9194 item = item->li_next) | |
9195 { | |
9196 if (argc == MAX_FUNC_ARGS) | |
9197 { | |
9198 EMSG(_("E699: Too many arguments")); | |
9199 break; | |
9200 } | |
9201 /* Make a copy of each argument. This is needed to be able to set | |
9202 * v_lock to VAR_FIXED in the copy without changing the original list. | |
9203 */ | |
9204 copy_tv(&item->li_tv, &argv[argc++]); | |
9205 } | |
9206 | |
9207 if (item == NULL) | |
9208 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv, | |
9209 curwin->w_cursor.lnum, curwin->w_cursor.lnum, | |
9210 &dummy, TRUE, selfdict); | |
9211 | |
9212 /* Free the arguments. */ | |
9213 while (argc > 0) | |
9214 clear_tv(&argv[--argc]); | |
9215 } | 9238 } |
9216 | 9239 |
9217 #ifdef FEAT_FLOAT | 9240 #ifdef FEAT_FLOAT |
9218 /* | 9241 /* |
9219 * "ceil({float})" function | 9242 * "ceil({float})" function |
14421 #ifdef FEAT_INS_EXPAND | 14444 #ifdef FEAT_INS_EXPAND |
14422 if (pum_visible()) | 14445 if (pum_visible()) |
14423 rettv->vval.v_number = 1; | 14446 rettv->vval.v_number = 1; |
14424 #endif | 14447 #endif |
14425 } | 14448 } |
14449 | |
14450 #ifdef FEAT_PYTHON3 | |
14451 /* | |
14452 * "py3eval()" function | |
14453 */ | |
14454 static void | |
14455 f_py3eval(argvars, rettv) | |
14456 typval_T *argvars; | |
14457 typval_T *rettv; | |
14458 { | |
14459 char_u *str; | |
14460 char_u buf[NUMBUFLEN]; | |
14461 | |
14462 str = get_tv_string_buf(&argvars[0], buf); | |
14463 do_py3eval(str, rettv); | |
14464 } | |
14465 #endif | |
14466 | |
14467 #ifdef FEAT_PYTHON | |
14468 /* | |
14469 * "pyeval()" function | |
14470 */ | |
14471 static void | |
14472 f_pyeval(argvars, rettv) | |
14473 typval_T *argvars; | |
14474 typval_T *rettv; | |
14475 { | |
14476 char_u *str; | |
14477 char_u buf[NUMBUFLEN]; | |
14478 | |
14479 str = get_tv_string_buf(&argvars[0], buf); | |
14480 do_pyeval(str, rettv); | |
14481 } | |
14482 #endif | |
14426 | 14483 |
14427 /* | 14484 /* |
14428 * "range()" function | 14485 * "range()" function |
14429 */ | 14486 */ |
14430 static void | 14487 static void |
22137 | 22194 |
22138 /* | 22195 /* |
22139 * Unreference a Function: decrement the reference count and free it when it | 22196 * Unreference a Function: decrement the reference count and free it when it |
22140 * becomes zero. Only for numbered functions. | 22197 * becomes zero. Only for numbered functions. |
22141 */ | 22198 */ |
22142 static void | 22199 void |
22143 func_unref(name) | 22200 func_unref(name) |
22144 char_u *name; | 22201 char_u *name; |
22145 { | 22202 { |
22146 ufunc_T *fp; | 22203 ufunc_T *fp; |
22147 | 22204 |
22161 } | 22218 } |
22162 | 22219 |
22163 /* | 22220 /* |
22164 * Count a reference to a Function. | 22221 * Count a reference to a Function. |
22165 */ | 22222 */ |
22166 static void | 22223 void |
22167 func_ref(name) | 22224 func_ref(name) |
22168 char_u *name; | 22225 char_u *name; |
22169 { | 22226 { |
22170 ufunc_T *fp; | 22227 ufunc_T *fp; |
22171 | 22228 |