Mercurial > vim
comparison src/eval.c @ 169:0e902b8f511f v7.0051
updated for version 7.0051
author | vimboss |
---|---|
date | Tue, 22 Feb 2005 08:56:13 +0000 |
parents | e943e5502fc9 |
children | 8c60f65311fa |
comparison
equal
deleted
inserted
replaced
168:4d9eabb1396e | 169:0e902b8f511f |
---|---|
146 */ | 146 */ |
147 typedef struct ufunc ufunc_T; | 147 typedef struct ufunc ufunc_T; |
148 | 148 |
149 struct ufunc | 149 struct ufunc |
150 { | 150 { |
151 ufunc_T *next; /* next function in list */ | 151 int uf_varargs; /* variable nr of arguments */ |
152 char_u *name; /* name of function; can start with <SNR>123_ | 152 int uf_flags; |
153 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */ | 153 int uf_calls; /* nr of active calls */ |
154 int varargs; /* variable nr of arguments */ | 154 garray_T uf_args; /* arguments */ |
155 int flags; | 155 garray_T uf_lines; /* function lines */ |
156 int calls; /* nr of active calls */ | 156 scid_T uf_script_ID; /* ID of script where function was defined, |
157 garray_T args; /* arguments */ | |
158 garray_T lines; /* function lines */ | |
159 scid_T script_ID; /* ID of script where function was defined, | |
160 used for s: variables */ | 157 used for s: variables */ |
161 int refcount; /* for numbered function: reference count */ | 158 int uf_refcount; /* for numbered function: reference count */ |
159 char_u uf_name[1]; /* name of function (actually longer); can | |
160 start with <SNR>123_ (<SNR> is K_SPECIAL | |
161 KS_EXTRA KE_SNR) */ | |
162 }; | 162 }; |
163 | 163 |
164 /* function flags */ | 164 /* function flags */ |
165 #define FC_ABORT 1 /* abort function on error */ | 165 #define FC_ABORT 1 /* abort function on error */ |
166 #define FC_RANGE 2 /* function accepts range */ | 166 #define FC_RANGE 2 /* function accepts range */ |
167 #define FC_DICT 4 /* Dict function, uses "self" */ | 167 #define FC_DICT 4 /* Dict function, uses "self" */ |
168 | 168 |
169 /* | 169 /* |
170 * All user-defined functions are found in the forward-linked function list. | 170 * All user-defined functions are found in this hash table. |
171 * The first function is pointed at by firstfunc. | 171 */ |
172 */ | 172 hashtab_T func_hashtab; |
173 ufunc_T *firstfunc = NULL; | 173 |
174 | 174 /* From user function to hashitem and back. */ |
175 #define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j] | 175 static ufunc_T dumuf; |
176 #define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j] | 176 #define UF2HIKEY(fp) ((fp)->uf_name) |
177 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf))) | |
178 #define HI2UF(hi) HIKEY2UF((hi)->hi_key) | |
179 | |
180 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j] | |
181 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j] | |
177 | 182 |
178 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */ | 183 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */ |
179 #define VAR_SHORT_LEN 20 /* short variable name length */ | 184 #define VAR_SHORT_LEN 20 /* short variable name length */ |
180 #define FIXVAR_CNT 12 /* number of fixed variables */ | 185 #define FIXVAR_CNT 12 /* number of fixed variables */ |
181 | 186 |
473 static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv)); | 478 static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv)); |
474 static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv)); | 479 static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv)); |
475 static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv)); | 480 static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv)); |
476 static void f_max __ARGS((typval_T *argvars, typval_T *rettv)); | 481 static void f_max __ARGS((typval_T *argvars, typval_T *rettv)); |
477 static void f_min __ARGS((typval_T *argvars, typval_T *rettv)); | 482 static void f_min __ARGS((typval_T *argvars, typval_T *rettv)); |
483 #ifdef vim_mkdir | |
484 static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv)); | |
485 #endif | |
478 static void f_mode __ARGS((typval_T *argvars, typval_T *rettv)); | 486 static void f_mode __ARGS((typval_T *argvars, typval_T *rettv)); |
479 static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv)); | 487 static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv)); |
480 static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv)); | 488 static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv)); |
481 static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv)); | 489 static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv)); |
482 static void f_range __ARGS((typval_T *argvars, typval_T *rettv)); | 490 static void f_range __ARGS((typval_T *argvars, typval_T *rettv)); |
552 static long get_tv_number __ARGS((typval_T *varp)); | 560 static long get_tv_number __ARGS((typval_T *varp)); |
553 static linenr_T get_tv_lnum __ARGS((typval_T *argvars)); | 561 static linenr_T get_tv_lnum __ARGS((typval_T *argvars)); |
554 static char_u *get_tv_string __ARGS((typval_T *varp)); | 562 static char_u *get_tv_string __ARGS((typval_T *varp)); |
555 static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf)); | 563 static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf)); |
556 static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp)); | 564 static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp)); |
557 static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname)); | 565 static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing)); |
558 static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname)); | 566 static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname)); |
559 static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val)); | 567 static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val)); |
560 static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi)); | 568 static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi)); |
561 static void list_one_var __ARGS((dictitem_T *v, char_u *prefix)); | 569 static void list_one_var __ARGS((dictitem_T *v, char_u *prefix)); |
562 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string)); | 570 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string)); |
572 static void list_func_head __ARGS((ufunc_T *fp, int indent)); | 580 static void list_func_head __ARGS((ufunc_T *fp, int indent)); |
573 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); | 581 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); |
574 static ufunc_T *find_func __ARGS((char_u *name)); | 582 static ufunc_T *find_func __ARGS((char_u *name)); |
575 static int function_exists __ARGS((char_u *name)); | 583 static int function_exists __ARGS((char_u *name)); |
576 static int builtin_function __ARGS((char_u *name)); | 584 static int builtin_function __ARGS((char_u *name)); |
577 static int func_autoload __ARGS((char_u *name)); | 585 static int script_autoload __ARGS((char_u *name)); |
586 static char_u *autoload_name __ARGS((char_u *name)); | |
578 static void func_free __ARGS((ufunc_T *fp)); | 587 static void func_free __ARGS((ufunc_T *fp)); |
579 static void func_unref __ARGS((char_u *name)); | 588 static void func_unref __ARGS((char_u *name)); |
580 static void func_ref __ARGS((char_u *name)); | 589 static void func_ref __ARGS((char_u *name)); |
581 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)); | 590 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)); |
582 static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr)); | 591 static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr)); |
603 static void list_fix_watch __ARGS((list_T *l, listitem_T *item)); | 612 static void list_fix_watch __ARGS((list_T *l, listitem_T *item)); |
604 static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep)); | 613 static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep)); |
605 static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit)); | 614 static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit)); |
606 static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock)); | 615 static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock)); |
607 static void item_lock __ARGS((typval_T *tv, int deep, int lock)); | 616 static void item_lock __ARGS((typval_T *tv, int deep, int lock)); |
617 static int tv_islocked __ARGS((typval_T *tv)); | |
608 | 618 |
609 /* | 619 /* |
610 * Initialize the global and v: variables. | 620 * Initialize the global and v: variables. |
611 */ | 621 */ |
612 void | 622 void |
616 struct vimvar *p; | 626 struct vimvar *p; |
617 | 627 |
618 init_var_dict(&globvardict, &globvars_var); | 628 init_var_dict(&globvardict, &globvars_var); |
619 init_var_dict(&vimvardict, &vimvars_var); | 629 init_var_dict(&vimvardict, &vimvars_var); |
620 hash_init(&compat_hashtab); | 630 hash_init(&compat_hashtab); |
631 hash_init(&func_hashtab); | |
621 | 632 |
622 for (i = 0; i < VV_LEN; ++i) | 633 for (i = 0; i < VV_LEN; ++i) |
623 { | 634 { |
624 p = &vimvars[i]; | 635 p = &vimvars[i]; |
625 STRCPY(p->vv_di.di_key, p->vv_name); | 636 STRCPY(p->vv_di.di_key, p->vv_name); |
644 */ | 655 */ |
645 char_u * | 656 char_u * |
646 func_name(cookie) | 657 func_name(cookie) |
647 void *cookie; | 658 void *cookie; |
648 { | 659 { |
649 return ((funccall_T *)cookie)->func->name; | 660 return ((funccall_T *)cookie)->func->uf_name; |
650 } | 661 } |
651 | 662 |
652 /* | 663 /* |
653 * Return the address holding the next breakpoint line for a funccall cookie. | 664 * Return the address holding the next breakpoint line for a funccall cookie. |
654 */ | 665 */ |
712 { | 723 { |
713 set_var(name, tvp, FALSE); | 724 set_var(name, tvp, FALSE); |
714 free_tv(tvp); | 725 free_tv(tvp); |
715 } | 726 } |
716 } | 727 } |
728 } | |
729 | |
730 static lval_T *redir_lval = NULL; | |
731 static char_u *redir_endp = NULL; | |
732 static char_u *redir_varname = NULL; | |
733 | |
734 /* | |
735 * Start recording command output to a variable | |
736 * Returns OK if successfully completed the setup. FAIL otherwise. | |
737 */ | |
738 int | |
739 var_redir_start(name, append) | |
740 char_u *name; | |
741 int append; /* append to an existing variable */ | |
742 { | |
743 int save_emsg; | |
744 int err; | |
745 typval_T tv; | |
746 | |
747 /* Make sure a valid variable name is specified */ | |
748 if (!eval_isnamec(*name) || VIM_ISDIGIT(*name)) | |
749 { | |
750 EMSG(_(e_invarg)); | |
751 return FAIL; | |
752 } | |
753 | |
754 redir_varname = vim_strsave(name); | |
755 if (redir_varname == NULL) | |
756 return FAIL; | |
757 | |
758 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T)); | |
759 if (redir_lval == NULL) | |
760 { | |
761 var_redir_stop(); | |
762 return FAIL; | |
763 } | |
764 | |
765 /* Parse the variable name (can be a dict or list entry). */ | |
766 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE); | |
767 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL) | |
768 { | |
769 if (redir_endp != NULL && *redir_endp != NUL) | |
770 /* Trailing characters are present after the variable name */ | |
771 EMSG(_(e_trailing)); | |
772 else | |
773 EMSG(_(e_invarg)); | |
774 var_redir_stop(); | |
775 return FAIL; | |
776 } | |
777 | |
778 /* check if we can write to the variable: set it to or append an empty | |
779 * string */ | |
780 save_emsg = did_emsg; | |
781 did_emsg = FALSE; | |
782 tv.v_type = VAR_STRING; | |
783 tv.vval.v_string = (char_u *)""; | |
784 if (append) | |
785 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"."); | |
786 else | |
787 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"="); | |
788 err = did_emsg; | |
789 did_emsg += save_emsg; | |
790 if (err) | |
791 { | |
792 var_redir_stop(); | |
793 return FAIL; | |
794 } | |
795 if (redir_lval->ll_newkey != NULL) | |
796 { | |
797 /* Dictionary item was created, don't do it again. */ | |
798 vim_free(redir_lval->ll_newkey); | |
799 redir_lval->ll_newkey = NULL; | |
800 } | |
801 | |
802 return OK; | |
803 } | |
804 | |
805 /* | |
806 * Append "value[len]" to the variable set by var_redir_start(). | |
807 */ | |
808 void | |
809 var_redir_str(value, len) | |
810 char_u *value; | |
811 int len; | |
812 { | |
813 char_u *val; | |
814 typval_T tv; | |
815 int save_emsg; | |
816 int err; | |
817 | |
818 if (redir_lval == NULL) | |
819 return; | |
820 | |
821 if (len == -1) | |
822 /* Append the entire string */ | |
823 val = vim_strsave(value); | |
824 else | |
825 /* Append only the specified number of characters */ | |
826 val = vim_strnsave(value, len); | |
827 if (val == NULL) | |
828 return; | |
829 | |
830 tv.v_type = VAR_STRING; | |
831 tv.vval.v_string = val; | |
832 | |
833 save_emsg = did_emsg; | |
834 did_emsg = FALSE; | |
835 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)"."); | |
836 err = did_emsg; | |
837 did_emsg += save_emsg; | |
838 if (err) | |
839 var_redir_stop(); | |
840 | |
841 vim_free(tv.vval.v_string); | |
842 } | |
843 | |
844 /* | |
845 * Stop redirecting command output to a variable. | |
846 */ | |
847 void | |
848 var_redir_stop() | |
849 { | |
850 if (redir_lval != NULL) | |
851 { | |
852 clear_lval(redir_lval); | |
853 vim_free(redir_lval); | |
854 redir_lval = NULL; | |
855 } | |
856 vim_free(redir_varname); | |
857 redir_varname = NULL; | |
717 } | 858 } |
718 | 859 |
719 # if defined(FEAT_MBYTE) || defined(PROTO) | 860 # if defined(FEAT_MBYTE) || defined(PROTO) |
720 int | 861 int |
721 eval_charconvert(enc_from, enc_to, fname_from, fname_to) | 862 eval_charconvert(enc_from, enc_to, fname_from, fname_to) |
5864 {"matchend", 2, 4, f_matchend}, | 6005 {"matchend", 2, 4, f_matchend}, |
5865 {"matchlist", 2, 4, f_matchlist}, | 6006 {"matchlist", 2, 4, f_matchlist}, |
5866 {"matchstr", 2, 4, f_matchstr}, | 6007 {"matchstr", 2, 4, f_matchstr}, |
5867 {"max", 1, 1, f_max}, | 6008 {"max", 1, 1, f_max}, |
5868 {"min", 1, 1, f_min}, | 6009 {"min", 1, 1, f_min}, |
6010 #ifdef vim_mkdir | |
6011 {"mkdir", 1, 3, f_mkdir}, | |
6012 #endif | |
5869 {"mode", 0, 0, f_mode}, | 6013 {"mode", 0, 0, f_mode}, |
5870 {"nextnonblank", 1, 1, f_nextnonblank}, | 6014 {"nextnonblank", 1, 1, f_nextnonblank}, |
5871 {"nr2char", 1, 1, f_nr2char}, | 6015 {"nr2char", 1, 1, f_nr2char}, |
5872 {"prevnonblank", 1, 1, f_prevnonblank}, | 6016 {"prevnonblank", 1, 1, f_prevnonblank}, |
5873 {"range", 1, 3, f_range}, | 6017 {"range", 1, 3, f_range}, |
5874 {"readfile", 1, 2, f_readfile}, | 6018 {"readfile", 1, 3, f_readfile}, |
5875 {"remote_expr", 2, 3, f_remote_expr}, | 6019 {"remote_expr", 2, 3, f_remote_expr}, |
5876 {"remote_foreground", 1, 1, f_remote_foreground}, | 6020 {"remote_foreground", 1, 1, f_remote_foreground}, |
5877 {"remote_peek", 1, 2, f_remote_peek}, | 6021 {"remote_peek", 1, 2, f_remote_peek}, |
5878 {"remote_read", 1, 1, f_remote_read}, | 6022 {"remote_read", 1, 1, f_remote_read}, |
5879 {"remote_send", 2, 3, f_remote_send}, | 6023 {"remote_send", 2, 3, f_remote_send}, |
6216 /* executed an autocommand, search for the function again */ | 6360 /* executed an autocommand, search for the function again */ |
6217 fp = find_func(fname); | 6361 fp = find_func(fname); |
6218 } | 6362 } |
6219 #endif | 6363 #endif |
6220 /* Try loading a package. */ | 6364 /* Try loading a package. */ |
6221 if (fp == NULL && func_autoload(fname) && !aborting()) | 6365 if (fp == NULL && script_autoload(fname) && !aborting()) |
6222 { | 6366 { |
6223 /* loaded a package, search for the function again */ | 6367 /* loaded a package, search for the function again */ |
6224 fp = find_func(fname); | 6368 fp = find_func(fname); |
6225 } | 6369 } |
6226 | 6370 |
6227 if (fp != NULL) | 6371 if (fp != NULL) |
6228 { | 6372 { |
6229 if (fp->flags & FC_RANGE) | 6373 if (fp->uf_flags & FC_RANGE) |
6230 *doesrange = TRUE; | 6374 *doesrange = TRUE; |
6231 if (argcount < fp->args.ga_len) | 6375 if (argcount < fp->uf_args.ga_len) |
6232 error = ERROR_TOOFEW; | 6376 error = ERROR_TOOFEW; |
6233 else if (!fp->varargs && argcount > fp->args.ga_len) | 6377 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len) |
6234 error = ERROR_TOOMANY; | 6378 error = ERROR_TOOMANY; |
6235 else if ((fp->flags & FC_DICT) && selfdict == NULL) | 6379 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL) |
6236 error = ERROR_DICT; | 6380 error = ERROR_DICT; |
6237 else | 6381 else |
6238 { | 6382 { |
6239 /* | 6383 /* |
6240 * Call the user function. | 6384 * Call the user function. |
6241 * Save and restore search patterns, script variables and | 6385 * Save and restore search patterns, script variables and |
6242 * redo buffer. | 6386 * redo buffer. |
6243 */ | 6387 */ |
6244 save_search_patterns(); | 6388 save_search_patterns(); |
6245 saveRedobuff(); | 6389 saveRedobuff(); |
6246 ++fp->calls; | 6390 ++fp->uf_calls; |
6247 call_user_func(fp, argcount, argvars, rettv, | 6391 call_user_func(fp, argcount, argvars, rettv, |
6248 firstline, lastline, | 6392 firstline, lastline, |
6249 (fp->flags & FC_DICT) ? selfdict : NULL); | 6393 (fp->uf_flags & FC_DICT) ? selfdict : NULL); |
6250 if (--fp->calls <= 0 && isdigit(*fp->name) | 6394 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name) |
6251 && fp->refcount <= 0) | 6395 && fp->uf_refcount <= 0) |
6252 /* Function was unreferenced while being used, free it | 6396 /* Function was unreferenced while being used, free it |
6253 * now. */ | 6397 * now. */ |
6254 func_free(fp); | 6398 func_free(fp); |
6255 restoreRedobuff(); | 6399 restoreRedobuff(); |
6256 restore_search_patterns(); | 6400 restore_search_patterns(); |
8196 curbuf = save_curbuf; | 8340 curbuf = save_curbuf; |
8197 } | 8341 } |
8198 else | 8342 else |
8199 { | 8343 { |
8200 /* look up the variable */ | 8344 /* look up the variable */ |
8201 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname); | 8345 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE); |
8202 if (v != NULL) | 8346 if (v != NULL) |
8203 copy_tv(&v->di_tv, rettv); | 8347 copy_tv(&v->di_tv, rettv); |
8204 } | 8348 } |
8205 } | 8349 } |
8206 | 8350 |
8721 curwin = oldcurwin; | 8865 curwin = oldcurwin; |
8722 } | 8866 } |
8723 else | 8867 else |
8724 { | 8868 { |
8725 /* look up the variable */ | 8869 /* look up the variable */ |
8726 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname); | 8870 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE); |
8727 if (v != NULL) | 8871 if (v != NULL) |
8728 copy_tv(&v->di_tv, rettv); | 8872 copy_tv(&v->di_tv, rettv); |
8729 } | 8873 } |
8730 } | 8874 } |
8731 | 8875 |
8906 #ifdef FEAT_FKMAP | 9050 #ifdef FEAT_FKMAP |
8907 "farsi", | 9051 "farsi", |
8908 #endif | 9052 #endif |
8909 #ifdef FEAT_SEARCHPATH | 9053 #ifdef FEAT_SEARCHPATH |
8910 "file_in_path", | 9054 "file_in_path", |
9055 #endif | |
9056 #if defined(UNIX) && !defined(USE_SYSTEM) | |
9057 "filterpipe", | |
8911 #endif | 9058 #endif |
8912 #ifdef FEAT_FIND_ID | 9059 #ifdef FEAT_FIND_ID |
8913 "find_in_path", | 9060 "find_in_path", |
8914 #endif | 9061 #endif |
8915 #ifdef FEAT_FOLDING | 9062 #ifdef FEAT_FOLDING |
9769 typval_T *argvars; | 9916 typval_T *argvars; |
9770 typval_T *rettv; | 9917 typval_T *rettv; |
9771 { | 9918 { |
9772 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0])); | 9919 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0])); |
9773 } | 9920 } |
9774 | |
9775 static int tv_islocked __ARGS((typval_T *tv)); | |
9776 | 9921 |
9777 /* | 9922 /* |
9778 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or | 9923 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or |
9779 * it refers to a List or Dictionary that is locked. | 9924 * it refers to a List or Dictionary that is locked. |
9780 */ | 9925 */ |
10567 typval_T *rettv; | 10712 typval_T *rettv; |
10568 { | 10713 { |
10569 max_min(argvars, rettv, FALSE); | 10714 max_min(argvars, rettv, FALSE); |
10570 } | 10715 } |
10571 | 10716 |
10717 static int mkdir_recurse __ARGS((char_u *dir, int prot)); | |
10718 | |
10719 /* | |
10720 * Create the directory in which "dir" is located, and higher levels when | |
10721 * needed. | |
10722 */ | |
10723 static int | |
10724 mkdir_recurse(dir, prot) | |
10725 char_u *dir; | |
10726 int prot; | |
10727 { | |
10728 char_u *p; | |
10729 char_u *updir; | |
10730 int r = FAIL; | |
10731 | |
10732 /* Get end of directory name in "dir". | |
10733 * We're done when it's "/" or "c:/". */ | |
10734 p = gettail_sep(dir); | |
10735 if (p <= get_past_head(dir)) | |
10736 return OK; | |
10737 | |
10738 /* If the directory exists we're done. Otherwise: create it.*/ | |
10739 updir = vim_strnsave(dir, (int)(p - dir)); | |
10740 if (updir == NULL) | |
10741 return FAIL; | |
10742 if (mch_isdir(updir)) | |
10743 r = OK; | |
10744 else if (mkdir_recurse(updir, prot) == OK) | |
10745 r = vim_mkdir_emsg(updir, prot); | |
10746 vim_free(updir); | |
10747 return r; | |
10748 } | |
10749 | |
10750 #ifdef vim_mkdir | |
10751 /* | |
10752 * "mkdir()" function | |
10753 */ | |
10754 static void | |
10755 f_mkdir(argvars, rettv) | |
10756 typval_T *argvars; | |
10757 typval_T *rettv; | |
10758 { | |
10759 char_u *dir; | |
10760 char_u buf[NUMBUFLEN]; | |
10761 int prot = 0755; | |
10762 | |
10763 rettv->vval.v_number = FAIL; | |
10764 if (check_restricted() || check_secure()) | |
10765 return; | |
10766 | |
10767 dir = get_tv_string_buf(&argvars[0], buf); | |
10768 if (argvars[1].v_type != VAR_UNKNOWN) | |
10769 { | |
10770 if (argvars[2].v_type != VAR_UNKNOWN) | |
10771 prot = get_tv_number(&argvars[2]); | |
10772 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0) | |
10773 mkdir_recurse(dir, prot); | |
10774 } | |
10775 rettv->vval.v_number = vim_mkdir_emsg(dir, prot); | |
10776 } | |
10777 #endif | |
10778 | |
10572 /* | 10779 /* |
10573 * "mode()" function | 10780 * "mode()" function |
10574 */ | 10781 */ |
10575 /*ARGSUSED*/ | 10782 /*ARGSUSED*/ |
10576 static void | 10783 static void |
10752 int chop; /* how many CR to chop off */ | 10959 int chop; /* how many CR to chop off */ |
10753 char_u *prev = NULL; /* previously read bytes, if any */ | 10960 char_u *prev = NULL; /* previously read bytes, if any */ |
10754 int prevlen = 0; /* length of "prev" if not NULL */ | 10961 int prevlen = 0; /* length of "prev" if not NULL */ |
10755 char_u *s; | 10962 char_u *s; |
10756 int len; | 10963 int len; |
10757 | 10964 long maxline = MAXLNUM; |
10758 if (argvars[1].v_type != VAR_UNKNOWN | 10965 long cnt = 0; |
10759 && STRCMP(get_tv_string(&argvars[1]), "b") == 0) | 10966 |
10760 binary = TRUE; | 10967 if (argvars[1].v_type != VAR_UNKNOWN) |
10968 { | |
10969 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0) | |
10970 binary = TRUE; | |
10971 if (argvars[2].v_type != VAR_UNKNOWN) | |
10972 maxline = get_tv_number(&argvars[2]); | |
10973 } | |
10761 | 10974 |
10762 l = list_alloc(); | 10975 l = list_alloc(); |
10763 if (l == NULL) | 10976 if (l == NULL) |
10764 return; | 10977 return; |
10765 rettv->v_type = VAR_LIST; | 10978 rettv->v_type = VAR_LIST; |
10774 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname); | 10987 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname); |
10775 return; | 10988 return; |
10776 } | 10989 } |
10777 | 10990 |
10778 filtd = 0; | 10991 filtd = 0; |
10779 for (;;) | 10992 while (cnt < maxline) |
10780 { | 10993 { |
10781 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd); | 10994 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd); |
10782 buflen = filtd + readlen; | 10995 buflen = filtd + readlen; |
10783 tolist = 0; | 10996 tolist = 0; |
10784 for ( ; filtd < buflen || readlen <= 0; ++filtd) | 10997 for ( ; filtd < buflen || readlen <= 0; ++filtd) |
10823 li->li_tv.v_type = VAR_STRING; | 11036 li->li_tv.v_type = VAR_STRING; |
10824 li->li_tv.v_lock = 0; | 11037 li->li_tv.v_lock = 0; |
10825 li->li_tv.vval.v_string = s; | 11038 li->li_tv.vval.v_string = s; |
10826 list_append(l, li); | 11039 list_append(l, li); |
10827 | 11040 |
11041 if (++cnt >= maxline) | |
11042 break; | |
10828 if (readlen <= 0) | 11043 if (readlen <= 0) |
10829 break; | 11044 break; |
10830 } | 11045 } |
10831 else if (buf[filtd] == NUL) | 11046 else if (buf[filtd] == NUL) |
10832 buf[filtd] = '\n'; | 11047 buf[filtd] = '\n'; |
10861 mch_memmove(buf, buf + tolist, buflen - tolist); | 11076 mch_memmove(buf, buf + tolist, buflen - tolist); |
10862 filtd -= tolist; | 11077 filtd -= tolist; |
10863 } | 11078 } |
10864 } | 11079 } |
10865 | 11080 |
11081 vim_free(prev); | |
10866 fclose(fd); | 11082 fclose(fd); |
10867 } | 11083 } |
10868 | 11084 |
10869 | 11085 |
10870 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) | 11086 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) |
14130 ht = find_var_ht(name, &varname); | 14346 ht = find_var_ht(name, &varname); |
14131 if (htp != NULL) | 14347 if (htp != NULL) |
14132 *htp = ht; | 14348 *htp = ht; |
14133 if (ht == NULL) | 14349 if (ht == NULL) |
14134 return NULL; | 14350 return NULL; |
14135 return find_var_in_ht(ht, varname); | 14351 return find_var_in_ht(ht, varname, htp != NULL); |
14136 } | 14352 } |
14137 | 14353 |
14138 /* | 14354 /* |
14139 * Find variable "varname" in hashtab "ht". | 14355 * Find variable "varname" in hashtab "ht". |
14140 * Returns NULL if not found. | 14356 * Returns NULL if not found. |
14141 */ | 14357 */ |
14142 static dictitem_T * | 14358 static dictitem_T * |
14143 find_var_in_ht(ht, varname) | 14359 find_var_in_ht(ht, varname, writing) |
14144 hashtab_T *ht; | 14360 hashtab_T *ht; |
14145 char_u *varname; | 14361 char_u *varname; |
14362 int writing; | |
14146 { | 14363 { |
14147 hashitem_T *hi; | 14364 hashitem_T *hi; |
14148 | 14365 |
14149 if (*varname == NUL) | 14366 if (*varname == NUL) |
14150 { | 14367 { |
14162 return NULL; | 14379 return NULL; |
14163 } | 14380 } |
14164 | 14381 |
14165 hi = hash_find(ht, varname); | 14382 hi = hash_find(ht, varname); |
14166 if (HASHITEM_EMPTY(hi)) | 14383 if (HASHITEM_EMPTY(hi)) |
14167 return NULL; | 14384 { |
14385 /* For global variables we may try auto-loading the script. If it | |
14386 * worked find the variable again. */ | |
14387 if (ht == &globvarht && !writing | |
14388 && script_autoload(varname) && !aborting()) | |
14389 hi = hash_find(ht, varname); | |
14390 if (HASHITEM_EMPTY(hi)) | |
14391 return NULL; | |
14392 } | |
14168 return HI2DI(hi); | 14393 return HI2DI(hi); |
14169 } | 14394 } |
14170 | 14395 |
14171 /* | 14396 /* |
14172 * Find the hashtab used for a variable name. | 14397 * Find the hashtab used for a variable name. |
14177 char_u *name; | 14402 char_u *name; |
14178 char_u **varname; | 14403 char_u **varname; |
14179 { | 14404 { |
14180 if (name[1] != ':') | 14405 if (name[1] != ':') |
14181 { | 14406 { |
14182 /* If not "x:name" there must not be any ":" in the name. */ | 14407 /* The name must not start with a colon. */ |
14183 if (vim_strchr(name, ':') != NULL) | 14408 if (name[0] == ':') |
14184 return NULL; | 14409 return NULL; |
14185 *varname = name; | 14410 *varname = name; |
14186 | 14411 |
14187 /* "version" is "v:version" in all scopes */ | 14412 /* "version" is "v:version" in all scopes */ |
14188 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name))) | 14413 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name))) |
14191 if (current_funccal == NULL) | 14416 if (current_funccal == NULL) |
14192 return &globvarht; /* global variable */ | 14417 return &globvarht; /* global variable */ |
14193 return ¤t_funccal->l_vars.dv_hashtab; /* l: variable */ | 14418 return ¤t_funccal->l_vars.dv_hashtab; /* l: variable */ |
14194 } | 14419 } |
14195 *varname = name + 2; | 14420 *varname = name + 2; |
14421 if (*name == 'g') /* global variable */ | |
14422 return &globvarht; | |
14423 /* There must be no ':' in the rest of the name, unless g: is used */ | |
14424 if (vim_strchr(name + 2, ':') != NULL) | |
14425 return NULL; | |
14196 if (*name == 'b') /* buffer variable */ | 14426 if (*name == 'b') /* buffer variable */ |
14197 return &curbuf->b_vars.dv_hashtab; | 14427 return &curbuf->b_vars.dv_hashtab; |
14198 if (*name == 'w') /* window variable */ | 14428 if (*name == 'w') /* window variable */ |
14199 return &curwin->w_vars.dv_hashtab; | 14429 return &curwin->w_vars.dv_hashtab; |
14200 if (*name == 'g') /* global variable */ | |
14201 return &globvarht; | |
14202 if (*name == 'v') /* v: variable */ | 14430 if (*name == 'v') /* v: variable */ |
14203 return &vimvarht; | 14431 return &vimvarht; |
14204 if (*name == 'a' && current_funccal != NULL) /* function argument */ | 14432 if (*name == 'a' && current_funccal != NULL) /* function argument */ |
14205 return ¤t_funccal->l_avars.dv_hashtab; | 14433 return ¤t_funccal->l_avars.dv_hashtab; |
14206 if (*name == 'l' && current_funccal != NULL) /* local function variable */ | 14434 if (*name == 'l' && current_funccal != NULL) /* local function variable */ |
14433 { | 14661 { |
14434 EMSG2(_("E461: Illegal variable name: %s"), name); | 14662 EMSG2(_("E461: Illegal variable name: %s"), name); |
14435 return; | 14663 return; |
14436 } | 14664 } |
14437 | 14665 |
14438 v = find_var_in_ht(ht, varname); | 14666 v = find_var_in_ht(ht, varname, TRUE); |
14439 if (v != NULL) | 14667 if (v != NULL) |
14440 { | 14668 { |
14441 /* existing variable, need to clear the value */ | 14669 /* existing variable, need to clear the value */ |
14442 if (var_check_ro(v->di_flags, name) | 14670 if (var_check_ro(v->di_flags, name) |
14443 || tv_check_lock(v->di_tv.v_lock, name)) | 14671 || tv_check_lock(v->di_tv.v_lock, name)) |
14930 char_u *skip_until = NULL; | 15158 char_u *skip_until = NULL; |
14931 dictitem_T *v; | 15159 dictitem_T *v; |
14932 funcdict_T fudi; | 15160 funcdict_T fudi; |
14933 static int func_nr = 0; /* number for nameless function */ | 15161 static int func_nr = 0; /* number for nameless function */ |
14934 int paren; | 15162 int paren; |
15163 hashtab_T *ht; | |
15164 int todo; | |
15165 hashitem_T *hi; | |
14935 | 15166 |
14936 /* | 15167 /* |
14937 * ":function" without argument: list functions. | 15168 * ":function" without argument: list functions. |
14938 */ | 15169 */ |
14939 if (ends_excmd(*eap->arg)) | 15170 if (ends_excmd(*eap->arg)) |
14940 { | 15171 { |
14941 if (!eap->skip) | 15172 if (!eap->skip) |
14942 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next) | 15173 { |
14943 if (!isdigit(*fp->name)) | 15174 todo = globvarht.ht_used; |
14944 list_func_head(fp, FALSE); | 15175 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi) |
15176 { | |
15177 if (!HASHITEM_EMPTY(hi)) | |
15178 { | |
15179 --todo; | |
15180 fp = HI2UF(hi); | |
15181 if (!isdigit(*fp->uf_name)) | |
15182 list_func_head(fp, FALSE); | |
15183 } | |
15184 } | |
15185 } | |
14945 eap->nextcmd = check_nextcmd(eap->arg); | 15186 eap->nextcmd = check_nextcmd(eap->arg); |
14946 return; | 15187 return; |
14947 } | 15188 } |
14948 | 15189 |
14949 /* | 15190 /* |
15002 { | 15243 { |
15003 fp = find_func(name); | 15244 fp = find_func(name); |
15004 if (fp != NULL) | 15245 if (fp != NULL) |
15005 { | 15246 { |
15006 list_func_head(fp, TRUE); | 15247 list_func_head(fp, TRUE); |
15007 for (j = 0; j < fp->lines.ga_len && !got_int; ++j) | 15248 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j) |
15008 { | 15249 { |
15009 msg_putchar('\n'); | 15250 msg_putchar('\n'); |
15010 msg_outnum((long)(j + 1)); | 15251 msg_outnum((long)(j + 1)); |
15011 if (j < 9) | 15252 if (j < 9) |
15012 msg_putchar(' '); | 15253 msg_putchar(' '); |
15013 if (j < 99) | 15254 if (j < 99) |
15014 msg_putchar(' '); | 15255 msg_putchar(' '); |
15015 msg_prt_line(FUNCLINE(fp, j)); | 15256 msg_prt_line(FUNCLINE(fp, j), FALSE); |
15016 out_flush(); /* show a line at a time */ | 15257 out_flush(); /* show a line at a time */ |
15017 ui_breakcheck(); | 15258 ui_breakcheck(); |
15018 } | 15259 } |
15019 if (!got_int) | 15260 if (!got_int) |
15020 { | 15261 { |
15258 /* | 15499 /* |
15259 * If there are no errors, add the function | 15500 * If there are no errors, add the function |
15260 */ | 15501 */ |
15261 if (fudi.fd_dict == NULL) | 15502 if (fudi.fd_dict == NULL) |
15262 { | 15503 { |
15263 v = find_var(name, NULL); | 15504 v = find_var(name, &ht); |
15264 if (v != NULL && v->di_tv.v_type == VAR_FUNC) | 15505 if (v != NULL && v->di_tv.v_type == VAR_FUNC) |
15265 { | 15506 { |
15266 emsg_funcname("E707: Function name conflicts with variable: %s", | 15507 emsg_funcname("E707: Function name conflicts with variable: %s", |
15267 name); | 15508 name); |
15268 goto erret; | 15509 goto erret; |
15274 if (!eap->forceit) | 15515 if (!eap->forceit) |
15275 { | 15516 { |
15276 emsg_funcname(e_funcexts, name); | 15517 emsg_funcname(e_funcexts, name); |
15277 goto erret; | 15518 goto erret; |
15278 } | 15519 } |
15279 if (fp->calls > 0) | 15520 if (fp->uf_calls > 0) |
15280 { | 15521 { |
15281 emsg_funcname("E127: Cannot redefine function %s: It is in use", | 15522 emsg_funcname("E127: Cannot redefine function %s: It is in use", |
15282 name); | 15523 name); |
15283 goto erret; | 15524 goto erret; |
15284 } | 15525 } |
15285 /* redefine existing function */ | 15526 /* redefine existing function */ |
15286 ga_clear_strings(&(fp->args)); | 15527 ga_clear_strings(&(fp->uf_args)); |
15287 ga_clear_strings(&(fp->lines)); | 15528 ga_clear_strings(&(fp->uf_lines)); |
15288 vim_free(name); | 15529 vim_free(name); |
15289 name = NULL; | 15530 name = NULL; |
15290 } | 15531 } |
15291 } | 15532 } |
15292 else | 15533 else |
15318 goto erret; | 15559 goto erret; |
15319 } | 15560 } |
15320 | 15561 |
15321 if (fp == NULL) | 15562 if (fp == NULL) |
15322 { | 15563 { |
15323 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T)); | 15564 if (fudi.fd_dict == NULL && vim_strchr(name, ':') != NULL) |
15565 { | |
15566 int slen, plen; | |
15567 char_u *scriptname; | |
15568 | |
15569 /* Check that the autoload name matches the script name. */ | |
15570 j = FAIL; | |
15571 if (sourcing_name != NULL) | |
15572 { | |
15573 scriptname = autoload_name(name); | |
15574 if (scriptname != NULL) | |
15575 { | |
15576 p = vim_strchr(scriptname, '/'); | |
15577 plen = STRLEN(p); | |
15578 slen = STRLEN(sourcing_name); | |
15579 if (slen > plen && fnamecmp(p, | |
15580 sourcing_name + slen - plen) == 0) | |
15581 j = OK; | |
15582 vim_free(scriptname); | |
15583 } | |
15584 } | |
15585 if (j == FAIL) | |
15586 { | |
15587 EMSG2(_("E746: Function name does not match script file name: %s"), name); | |
15588 goto erret; | |
15589 } | |
15590 } | |
15591 | |
15592 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name))); | |
15324 if (fp == NULL) | 15593 if (fp == NULL) |
15325 goto erret; | 15594 goto erret; |
15326 | 15595 |
15327 if (fudi.fd_dict != NULL) | 15596 if (fudi.fd_dict != NULL) |
15328 { | 15597 { |
15345 /* overwrite existing dict entry */ | 15614 /* overwrite existing dict entry */ |
15346 clear_tv(&fudi.fd_di->di_tv); | 15615 clear_tv(&fudi.fd_di->di_tv); |
15347 fudi.fd_di->di_tv.v_type = VAR_FUNC; | 15616 fudi.fd_di->di_tv.v_type = VAR_FUNC; |
15348 fudi.fd_di->di_tv.v_lock = 0; | 15617 fudi.fd_di->di_tv.v_lock = 0; |
15349 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name); | 15618 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name); |
15350 fp->refcount = 1; | 15619 fp->uf_refcount = 1; |
15351 } | 15620 } |
15352 | 15621 |
15353 /* insert the new function in the function list */ | 15622 /* insert the new function in the function list */ |
15354 fp->name = name; | 15623 STRCPY(fp->uf_name, name); |
15355 name = NULL; | 15624 hash_add(&func_hashtab, UF2HIKEY(fp)); |
15356 fp->next = firstfunc; | 15625 } |
15357 firstfunc = fp; | 15626 fp->uf_args = newargs; |
15358 } | 15627 fp->uf_lines = newlines; |
15359 fp->args = newargs; | 15628 fp->uf_varargs = varargs; |
15360 fp->lines = newlines; | 15629 fp->uf_flags = flags; |
15361 fp->varargs = varargs; | 15630 fp->uf_calls = 0; |
15362 fp->flags = flags; | 15631 fp->uf_script_ID = current_SID; |
15363 fp->calls = 0; | |
15364 fp->script_ID = current_SID; | |
15365 goto ret_free; | 15632 goto ret_free; |
15366 | 15633 |
15367 erret: | 15634 erret: |
15368 ga_clear_strings(&newargs); | 15635 ga_clear_strings(&newargs); |
15369 ga_clear_strings(&newlines); | 15636 ga_clear_strings(&newlines); |
15571 | 15838 |
15572 msg_start(); | 15839 msg_start(); |
15573 if (indent) | 15840 if (indent) |
15574 MSG_PUTS(" "); | 15841 MSG_PUTS(" "); |
15575 MSG_PUTS("function "); | 15842 MSG_PUTS("function "); |
15576 if (fp->name[0] == K_SPECIAL) | 15843 if (fp->uf_name[0] == K_SPECIAL) |
15577 { | 15844 { |
15578 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8)); | 15845 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8)); |
15579 msg_puts(fp->name + 3); | 15846 msg_puts(fp->uf_name + 3); |
15580 } | 15847 } |
15581 else | 15848 else |
15582 msg_puts(fp->name); | 15849 msg_puts(fp->uf_name); |
15583 msg_putchar('('); | 15850 msg_putchar('('); |
15584 for (j = 0; j < fp->args.ga_len; ++j) | 15851 for (j = 0; j < fp->uf_args.ga_len; ++j) |
15585 { | 15852 { |
15586 if (j) | 15853 if (j) |
15587 MSG_PUTS(", "); | 15854 MSG_PUTS(", "); |
15588 msg_puts(FUNCARG(fp, j)); | 15855 msg_puts(FUNCARG(fp, j)); |
15589 } | 15856 } |
15590 if (fp->varargs) | 15857 if (fp->uf_varargs) |
15591 { | 15858 { |
15592 if (j) | 15859 if (j) |
15593 MSG_PUTS(", "); | 15860 MSG_PUTS(", "); |
15594 MSG_PUTS("..."); | 15861 MSG_PUTS("..."); |
15595 } | 15862 } |
15602 */ | 15869 */ |
15603 static ufunc_T * | 15870 static ufunc_T * |
15604 find_func(name) | 15871 find_func(name) |
15605 char_u *name; | 15872 char_u *name; |
15606 { | 15873 { |
15607 ufunc_T *fp; | 15874 hashitem_T *hi; |
15608 | 15875 |
15609 for (fp = firstfunc; fp != NULL; fp = fp->next) | 15876 hi = hash_find(&func_hashtab, name); |
15610 if (STRCMP(name, fp->name) == 0) | 15877 if (!HASHITEM_EMPTY(hi)) |
15611 break; | 15878 return HI2UF(hi); |
15612 return fp; | 15879 return NULL; |
15613 } | 15880 } |
15614 | 15881 |
15615 /* | 15882 /* |
15616 * Return TRUE if a function "name" exists. | 15883 * Return TRUE if a function "name" exists. |
15617 */ | 15884 */ |
15644 { | 15911 { |
15645 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL; | 15912 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL; |
15646 } | 15913 } |
15647 | 15914 |
15648 /* | 15915 /* |
15649 * If "name" has a package name try autoloading the script. | 15916 * If "name" has a package name try autoloading the script for it. |
15650 * Return TRUE if a package was loaded. | 15917 * Return TRUE if a package was loaded. |
15651 */ | 15918 */ |
15652 static int | 15919 static int |
15653 func_autoload(name) | 15920 script_autoload(name) |
15654 char_u *name; | 15921 char_u *name; |
15655 { | 15922 { |
15656 char_u *p; | 15923 char_u *p; |
15657 char_u *scriptname; | 15924 char_u *scriptname; |
15658 int ret = FALSE; | 15925 int ret = FALSE; |
15659 | 15926 |
15660 /* If there is no colon after name[1] there is no package name. */ | 15927 /* If there is no colon after name[1] there is no package name. */ |
15661 p = vim_strchr(name, ':'); | 15928 p = vim_strchr(name, ':'); |
15662 if (p == NULL || p <= name + 2) | 15929 if (p == NULL || p <= name + 2) |
15663 return FALSE; | 15930 return FALSE; |
15931 | |
15932 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */ | |
15933 scriptname = autoload_name(name); | |
15934 if (cmd_runtime(scriptname, FALSE) == OK) | |
15935 ret = TRUE; | |
15936 | |
15937 vim_free(scriptname); | |
15938 return ret; | |
15939 } | |
15940 | |
15941 /* | |
15942 * Return the autoload script name for a function or variable name. | |
15943 * Returns NULL when out of memory. | |
15944 */ | |
15945 static char_u * | |
15946 autoload_name(name) | |
15947 char_u *name; | |
15948 { | |
15949 char_u *p; | |
15950 char_u *scriptname; | |
15664 | 15951 |
15665 /* Get the script file name: replace ':' with '/', append ".vim". */ | 15952 /* Get the script file name: replace ':' with '/', append ".vim". */ |
15666 scriptname = alloc((unsigned)(STRLEN(name) + 14)); | 15953 scriptname = alloc((unsigned)(STRLEN(name) + 14)); |
15667 if (scriptname == NULL) | 15954 if (scriptname == NULL) |
15668 return FALSE; | 15955 return FALSE; |
15670 STRCAT(scriptname, name); | 15957 STRCAT(scriptname, name); |
15671 *vim_strrchr(scriptname, ':') = NUL; | 15958 *vim_strrchr(scriptname, ':') = NUL; |
15672 STRCAT(scriptname, ".vim"); | 15959 STRCAT(scriptname, ".vim"); |
15673 while ((p = vim_strchr(scriptname, ':')) != NULL) | 15960 while ((p = vim_strchr(scriptname, ':')) != NULL) |
15674 *p = '/'; | 15961 *p = '/'; |
15675 | 15962 return scriptname; |
15676 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */ | |
15677 if (cmd_runtime(scriptname, FALSE) == OK) | |
15678 ret = TRUE; | |
15679 | |
15680 vim_free(scriptname); | |
15681 return ret; | |
15682 } | 15963 } |
15683 | 15964 |
15684 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | 15965 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
15685 | 15966 |
15686 /* | 15967 /* |
15690 char_u * | 15971 char_u * |
15691 get_user_func_name(xp, idx) | 15972 get_user_func_name(xp, idx) |
15692 expand_T *xp; | 15973 expand_T *xp; |
15693 int idx; | 15974 int idx; |
15694 { | 15975 { |
15695 static ufunc_T *fp = NULL; | 15976 static long_u done; |
15977 static hashitem_T *hi; | |
15978 ufunc_T *fp; | |
15696 | 15979 |
15697 if (idx == 0) | 15980 if (idx == 0) |
15698 fp = firstfunc; | 15981 { |
15699 if (fp != NULL) | 15982 done = 0; |
15700 { | 15983 hi = func_hashtab.ht_array; |
15701 if (STRLEN(fp->name) + 4 >= IOSIZE) | 15984 } |
15702 return fp->name; /* prevents overflow */ | 15985 if (done < func_hashtab.ht_used) |
15986 { | |
15987 if (done++ > 0) | |
15988 ++hi; | |
15989 while (HASHITEM_EMPTY(hi)) | |
15990 ++hi; | |
15991 fp = HI2UF(hi); | |
15992 | |
15993 if (STRLEN(fp->uf_name) + 4 >= IOSIZE) | |
15994 return fp->uf_name; /* prevents overflow */ | |
15703 | 15995 |
15704 cat_func_name(IObuff, fp); | 15996 cat_func_name(IObuff, fp); |
15705 if (xp->xp_context != EXPAND_USER_FUNC) | 15997 if (xp->xp_context != EXPAND_USER_FUNC) |
15706 { | 15998 { |
15707 STRCAT(IObuff, "("); | 15999 STRCAT(IObuff, "("); |
15708 if (!fp->varargs && fp->args.ga_len == 0) | 16000 if (!fp->uf_varargs && fp->uf_args.ga_len == 0) |
15709 STRCAT(IObuff, ")"); | 16001 STRCAT(IObuff, ")"); |
15710 } | 16002 } |
15711 | |
15712 fp = fp->next; | |
15713 return IObuff; | 16003 return IObuff; |
15714 } | 16004 } |
15715 return NULL; | 16005 return NULL; |
15716 } | 16006 } |
15717 | 16007 |
15725 static void | 16015 static void |
15726 cat_func_name(buf, fp) | 16016 cat_func_name(buf, fp) |
15727 char_u *buf; | 16017 char_u *buf; |
15728 ufunc_T *fp; | 16018 ufunc_T *fp; |
15729 { | 16019 { |
15730 if (fp->name[0] == K_SPECIAL) | 16020 if (fp->uf_name[0] == K_SPECIAL) |
15731 { | 16021 { |
15732 STRCPY(buf, "<SNR>"); | 16022 STRCPY(buf, "<SNR>"); |
15733 STRCAT(buf, fp->name + 3); | 16023 STRCAT(buf, fp->uf_name + 3); |
15734 } | 16024 } |
15735 else | 16025 else |
15736 STRCPY(buf, fp->name); | 16026 STRCPY(buf, fp->uf_name); |
15737 } | 16027 } |
15738 | 16028 |
15739 /* | 16029 /* |
15740 * ":delfunction {name}" | 16030 * ":delfunction {name}" |
15741 */ | 16031 */ |
15776 if (fp == NULL) | 16066 if (fp == NULL) |
15777 { | 16067 { |
15778 EMSG2(_("E130: Undefined function: %s"), eap->arg); | 16068 EMSG2(_("E130: Undefined function: %s"), eap->arg); |
15779 return; | 16069 return; |
15780 } | 16070 } |
15781 if (fp->calls > 0) | 16071 if (fp->uf_calls > 0) |
15782 { | 16072 { |
15783 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg); | 16073 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg); |
15784 return; | 16074 return; |
15785 } | 16075 } |
15786 | 16076 |
15800 */ | 16090 */ |
15801 static void | 16091 static void |
15802 func_free(fp) | 16092 func_free(fp) |
15803 ufunc_T *fp; | 16093 ufunc_T *fp; |
15804 { | 16094 { |
15805 ufunc_T *pfp; | 16095 hashitem_T *hi; |
15806 | 16096 |
15807 /* clear this function */ | 16097 /* clear this function */ |
15808 vim_free(fp->name); | 16098 ga_clear_strings(&(fp->uf_args)); |
15809 ga_clear_strings(&(fp->args)); | 16099 ga_clear_strings(&(fp->uf_lines)); |
15810 ga_clear_strings(&(fp->lines)); | 16100 |
15811 | 16101 /* remove the function from the function hashtable */ |
15812 /* remove the function from the function list */ | 16102 hi = hash_find(&func_hashtab, UF2HIKEY(fp)); |
15813 if (firstfunc == fp) | 16103 if (HASHITEM_EMPTY(hi)) |
15814 firstfunc = fp->next; | 16104 EMSG2(_(e_intern2), "func_free()"); |
15815 else | 16105 else |
15816 { | 16106 hash_remove(&func_hashtab, hi); |
15817 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next) | 16107 |
15818 if (pfp->next == fp) | |
15819 { | |
15820 pfp->next = fp->next; | |
15821 break; | |
15822 } | |
15823 } | |
15824 vim_free(fp); | 16108 vim_free(fp); |
15825 } | 16109 } |
15826 | 16110 |
15827 /* | 16111 /* |
15828 * Unreference a Function: decrement the reference count and free it when it | 16112 * Unreference a Function: decrement the reference count and free it when it |
15837 if (name != NULL && isdigit(*name)) | 16121 if (name != NULL && isdigit(*name)) |
15838 { | 16122 { |
15839 fp = find_func(name); | 16123 fp = find_func(name); |
15840 if (fp == NULL) | 16124 if (fp == NULL) |
15841 EMSG2(_(e_intern2), "func_unref()"); | 16125 EMSG2(_(e_intern2), "func_unref()"); |
15842 else if (--fp->refcount <= 0) | 16126 else if (--fp->uf_refcount <= 0) |
15843 { | 16127 { |
15844 /* Only delete it when it's not being used. Otherwise it's done | 16128 /* Only delete it when it's not being used. Otherwise it's done |
15845 * when "calls" becomes zero. */ | 16129 * when "uf_calls" becomes zero. */ |
15846 if (fp->calls == 0) | 16130 if (fp->uf_calls == 0) |
15847 func_free(fp); | 16131 func_free(fp); |
15848 } | 16132 } |
15849 } | 16133 } |
15850 } | 16134 } |
15851 | 16135 |
15862 { | 16146 { |
15863 fp = find_func(name); | 16147 fp = find_func(name); |
15864 if (fp == NULL) | 16148 if (fp == NULL) |
15865 EMSG2(_(e_intern2), "func_ref()"); | 16149 EMSG2(_(e_intern2), "func_ref()"); |
15866 else | 16150 else |
15867 ++fp->refcount; | 16151 ++fp->uf_refcount; |
15868 } | 16152 } |
15869 } | 16153 } |
15870 | 16154 |
15871 /* | 16155 /* |
15872 * Call a user function. | 16156 * Call a user function. |
15913 rettv->vval.v_number = 0; | 16197 rettv->vval.v_number = 0; |
15914 fc.linenr = 0; | 16198 fc.linenr = 0; |
15915 fc.returned = FALSE; | 16199 fc.returned = FALSE; |
15916 fc.level = ex_nesting_level; | 16200 fc.level = ex_nesting_level; |
15917 /* Check if this function has a breakpoint. */ | 16201 /* Check if this function has a breakpoint. */ |
15918 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0); | 16202 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0); |
15919 fc.dbg_tick = debug_tick; | 16203 fc.dbg_tick = debug_tick; |
15920 | 16204 |
15921 /* | 16205 /* |
15922 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables | 16206 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables |
15923 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free | 16207 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free |
15945 * Set a:0 to "argcount". | 16229 * Set a:0 to "argcount". |
15946 * Set a:000 to a list with room for the "..." arguments. | 16230 * Set a:000 to a list with room for the "..." arguments. |
15947 */ | 16231 */ |
15948 init_var_dict(&fc.l_avars, &fc.l_avars_var); | 16232 init_var_dict(&fc.l_avars, &fc.l_avars_var); |
15949 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0", | 16233 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0", |
15950 (varnumber_T)(argcount - fp->args.ga_len)); | 16234 (varnumber_T)(argcount - fp->uf_args.ga_len)); |
15951 v = &fc.fixvar[fixvar_idx++].var; | 16235 v = &fc.fixvar[fixvar_idx++].var; |
15952 STRCPY(v->di_key, "000"); | 16236 STRCPY(v->di_key, "000"); |
15953 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; | 16237 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; |
15954 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v)); | 16238 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v)); |
15955 v->di_tv.v_type = VAR_LIST; | 16239 v->di_tv.v_type = VAR_LIST; |
15967 (varnumber_T)firstline); | 16251 (varnumber_T)firstline); |
15968 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline", | 16252 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline", |
15969 (varnumber_T)lastline); | 16253 (varnumber_T)lastline); |
15970 for (i = 0; i < argcount; ++i) | 16254 for (i = 0; i < argcount; ++i) |
15971 { | 16255 { |
15972 ai = i - fp->args.ga_len; | 16256 ai = i - fp->uf_args.ga_len; |
15973 if (ai < 0) | 16257 if (ai < 0) |
15974 /* named argument a:name */ | 16258 /* named argument a:name */ |
15975 name = FUNCARG(fp, i); | 16259 name = FUNCARG(fp, i); |
15976 else | 16260 else |
15977 { | 16261 { |
16012 ++RedrawingDisabled; | 16296 ++RedrawingDisabled; |
16013 save_sourcing_name = sourcing_name; | 16297 save_sourcing_name = sourcing_name; |
16014 save_sourcing_lnum = sourcing_lnum; | 16298 save_sourcing_lnum = sourcing_lnum; |
16015 sourcing_lnum = 1; | 16299 sourcing_lnum = 1; |
16016 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0 | 16300 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0 |
16017 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13)); | 16301 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13)); |
16018 if (sourcing_name != NULL) | 16302 if (sourcing_name != NULL) |
16019 { | 16303 { |
16020 if (save_sourcing_name != NULL | 16304 if (save_sourcing_name != NULL |
16021 && STRNCMP(save_sourcing_name, "function ", 9) == 0) | 16305 && STRNCMP(save_sourcing_name, "function ", 9) == 0) |
16022 sprintf((char *)sourcing_name, "%s..", save_sourcing_name); | 16306 sprintf((char *)sourcing_name, "%s..", save_sourcing_name); |
16056 cmdline_row = msg_row; | 16340 cmdline_row = msg_row; |
16057 --no_wait_return; | 16341 --no_wait_return; |
16058 } | 16342 } |
16059 } | 16343 } |
16060 save_current_SID = current_SID; | 16344 save_current_SID = current_SID; |
16061 current_SID = fp->script_ID; | 16345 current_SID = fp->uf_script_ID; |
16062 save_did_emsg = did_emsg; | 16346 save_did_emsg = did_emsg; |
16063 did_emsg = FALSE; | 16347 did_emsg = FALSE; |
16064 | 16348 |
16065 /* call do_cmdline() to execute the lines */ | 16349 /* call do_cmdline() to execute the lines */ |
16066 do_cmdline(NULL, get_func_line, (void *)&fc, | 16350 do_cmdline(NULL, get_func_line, (void *)&fc, |
16067 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | 16351 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); |
16068 | 16352 |
16069 --RedrawingDisabled; | 16353 --RedrawingDisabled; |
16070 | 16354 |
16071 /* when the function was aborted because of an error, return -1 */ | 16355 /* when the function was aborted because of an error, return -1 */ |
16072 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN) | 16356 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN) |
16073 { | 16357 { |
16074 clear_tv(rettv); | 16358 clear_tv(rettv); |
16075 rettv->v_type = VAR_NUMBER; | 16359 rettv->v_type = VAR_NUMBER; |
16076 rettv->vval.v_number = -1; | 16360 rettv->vval.v_number = -1; |
16077 } | 16361 } |
16344 garray_T *gap; /* growarray with function lines */ | 16628 garray_T *gap; /* growarray with function lines */ |
16345 | 16629 |
16346 /* If breakpoints have been added/deleted need to check for it. */ | 16630 /* If breakpoints have been added/deleted need to check for it. */ |
16347 if (fcp->dbg_tick != debug_tick) | 16631 if (fcp->dbg_tick != debug_tick) |
16348 { | 16632 { |
16349 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name, | 16633 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->uf_name, |
16350 sourcing_lnum); | 16634 sourcing_lnum); |
16351 fcp->dbg_tick = debug_tick; | 16635 fcp->dbg_tick = debug_tick; |
16352 } | 16636 } |
16353 | 16637 |
16354 gap = &fcp->func->lines; | 16638 gap = &fcp->func->uf_lines; |
16355 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try()) | 16639 if ((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) |
16356 retval = NULL; | 16640 retval = NULL; |
16357 else if (fcp->returned || fcp->linenr >= gap->ga_len) | 16641 else if (fcp->returned || fcp->linenr >= gap->ga_len) |
16358 retval = NULL; | 16642 retval = NULL; |
16359 else | 16643 else |
16360 { | 16644 { |
16363 } | 16647 } |
16364 | 16648 |
16365 /* Did we encounter a breakpoint? */ | 16649 /* Did we encounter a breakpoint? */ |
16366 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum) | 16650 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum) |
16367 { | 16651 { |
16368 dbg_breakpoint(fcp->func->name, sourcing_lnum); | 16652 dbg_breakpoint(fcp->func->uf_name, sourcing_lnum); |
16369 /* Find next breakpoint. */ | 16653 /* Find next breakpoint. */ |
16370 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name, | 16654 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->uf_name, |
16371 sourcing_lnum); | 16655 sourcing_lnum); |
16372 fcp->dbg_tick = debug_tick; | 16656 fcp->dbg_tick = debug_tick; |
16373 } | 16657 } |
16374 | 16658 |
16375 return retval; | 16659 return retval; |
16385 { | 16669 { |
16386 funccall_T *fcp = (funccall_T *)cookie; | 16670 funccall_T *fcp = (funccall_T *)cookie; |
16387 | 16671 |
16388 /* Ignore the "abort" flag if the abortion behavior has been changed due to | 16672 /* Ignore the "abort" flag if the abortion behavior has been changed due to |
16389 * an error inside a try conditional. */ | 16673 * an error inside a try conditional. */ |
16390 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try()) | 16674 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) |
16391 || fcp->returned); | 16675 || fcp->returned); |
16392 } | 16676 } |
16393 | 16677 |
16394 /* | 16678 /* |
16395 * return TRUE if cookie indicates a function which "abort"s on errors. | 16679 * return TRUE if cookie indicates a function which "abort"s on errors. |
16396 */ | 16680 */ |
16397 int | 16681 int |
16398 func_has_abort(cookie) | 16682 func_has_abort(cookie) |
16399 void *cookie; | 16683 void *cookie; |
16400 { | 16684 { |
16401 return ((funccall_T *)cookie)->func->flags & FC_ABORT; | 16685 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT; |
16402 } | 16686 } |
16403 | 16687 |
16404 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION) | 16688 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION) |
16405 typedef enum | 16689 typedef enum |
16406 { | 16690 { |