Macros | Functions | Variables
userfunc.c File Reference
#include "nvim/ascii.h"
#include "nvim/charset.h"
#include "nvim/edit.h"
#include "nvim/eval.h"
#include "nvim/eval/encode.h"
#include "nvim/eval/userfunc.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_getln.h"
#include "nvim/fileio.h"
#include "nvim/getchar.h"
#include "nvim/globals.h"
#include "nvim/lua/executor.h"
#include "nvim/misc1.h"
#include "nvim/os/input.h"
#include "nvim/regexp.h"
#include "nvim/search.h"
#include "nvim/ui.h"
#include "nvim/vim.h"


#define FC_ABORT   0x01
#define FC_RANGE   0x02
#define FC_DICT   0x04
#define FC_CLOSURE   0x08
#define FC_DELETED   0x10
#define FC_REMOVED   0x20
#define FC_SANDBOX   0x40
#define FC_CFUNC   0x80
#define FLEN_FIXED   40


void func_init (void)
char_uget_lambda_name (void)
 Get a name for a lambda. Returned in static memory. More...
int get_lambda_tv (char_u **arg, typval_T *rettv, bool evaluate)
char_uderef_func_name (const char *name, int *lenp, partial_T **const partialp, bool no_autoload) FUNC_ATTR_NONNULL_ARG(1
char_u if (v!=NULL &&v->di_tv.v_type==VAR_FUNC)
 return (char_u *)
int get_func_tv (const char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict)
ufunc_Tfind_func (const char_u *name)
void call_user_func (ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict) FUNC_ATTR_NONNULL_ARG(1
 if (depth >=p_mfd)
 save_search_patterns ()
 if (!ins_compl_active())
 line_breakcheck ()
ga_initfc (ufunc_T *), 1
 func_ptr_ref (fp)
 if (STRNCMP(fp->uf_name,"<lambda>", 8)==0)
 if (selfdict!=NULL)
 STRCPY (name,"000")
add_nr_var & fc (dictitem_T *)&fc->fixvar[fixvar_idx++],"firstline",(varnumber_T) firstline
 for (int i=0;i< argcount;i++)
 if (fp->uf_flags &FC_SANDBOX)
 if (p_verbose >=12)
 if (func_not_yet_profiling_but_should)
 if (func_or_func_caller_profiling)
 if (do_profiling_yes)
 do_cmdline (NULL, get_func_line,(void *) fc, DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT)
 if ((did_emsg &&(fp->uf_flags &FC_ABORT))||rettv->v_type==VAR_UNKNOWN)
 xfree (sourcing_name)
 if (using_sandbox)
 if (p_verbose >=12 &&sourcing_name!=NULL)
 cleanup_function_call (fc)
 if (--fp->uf_calls<=0 &&fp->uf_refcount<=0)
 if (did_save_redo)
 restore_search_patterns ()
void save_funccal (funccal_entry_T *entry)
void restore_funccal (void)
funccall_Tget_current_funccal (void)
void set_current_funccal (funccall_T *fc)
int func_call (char_u *name, typval_T *args, partial_T *partial, dict_T *selfdict, typval_T *rettv)
int call_func (const char_u *funcname, int len, typval_T *rettv, int argcount_in, typval_T *argvars_in, ArgvFunc argv_func, linenr_T firstline, linenr_T lastline, int *doesrange, bool evaluate, partial_T *partial, dict_T *selfdict_in) FUNC_ATTR_NONNULL_ARG(1
 if (partial!=NULL)
 if (error==ERROR_NONE &&evaluate)
 while (argv_clear > 0)
 xfree (tofree)
 xfree (name)
char_utrans_function_name (char_u **pp, bool skip, int flags, funcdict_T *fdp, partial_T **partial) FUNC_ATTR_NONNULL_ARG(1)
void ex_function (exarg_T *eap)
int eval_fname_script (const char *const p)
bool translated_function_exists (const char *name)
bool function_exists (const char *const name, bool no_deref)
char_uget_user_func_name (expand_T *xp, int idx)
void ex_delfunction (exarg_T *eap)
 ":delfunction {name}" More...
void func_unref (char_u *name)
void func_ptr_unref (ufunc_T *fp)
void func_ref (char_u *name)
 Count a reference to a Function. More...
void func_ptr_ref (ufunc_T *fp)
 Count a reference to a Function. More...
void ex_return (exarg_T *eap)
void ex_call (exarg_T *eap)
int do_return (exarg_T *eap, int reanimate, int is_cmd, void *rettv)
char_uget_return_cmd (void *rettv)
char_uget_func_line (int c, void *cookie, int indent, bool do_concat)
int func_has_ended (void *cookie)
int func_has_abort (void *cookie)
void make_partial (dict_T *const selfdict, typval_T *const rettv)
char_ufunc_name (void *cookie)
linenr_Tfunc_breakpoint (void *cookie)
int * func_dbg_tick (void *cookie)
int func_level (void *cookie)
int current_func_returned (void)
bool free_unref_funccal (int copyID, int testing)
funccall_Tget_funccal (void)
hashtab_Tget_funccal_local_ht (void)
dictitem_Tget_funccal_local_var (void)
hashtab_Tget_funccal_args_ht (void)
dictitem_Tget_funccal_args_var (void)
void list_func_vars (int *first)
dict_Tget_current_funccal_dict (hashtab_T *ht)
hashitem_Tfind_hi_in_scoped_ht (const char *name, hashtab_T **pht)
 Search hashitem in parent scope. More...
dictitem_Tfind_var_in_scoped_ht (const char *name, const size_t namelen, int no_autoload)
 Search variable in parent scope. More...
bool set_ref_in_previous_funccal (int copyID)
 Set "copyID + 1" in previous_funccal and callers. More...
bool set_ref_in_call_stack (int copyID)
 Set "copyID" in all local vars and arguments in the call stack. More...
bool set_ref_in_functions (int copyID)
 Set "copyID" in all functions available by name. More...
bool set_ref_in_func_args (int copyID)
 Set "copyID" in all function arguments. More...
bool set_ref_in_func (char_u *name, ufunc_T *fp_in, int copyID)
char_uregister_cfunc (cfunc_T cb, cfunc_free_T cb_free, void *state)
 Registers a C extension user function. More...


hashtab_T func_hashtab
void linenr_T save_sourcing_lnum
bool using_sandbox = false
funccall_Tfc = xcalloc(1, sizeof(funccall_T))
int save_did_emsg = did_emsg
dictitem_Tv = (dictitem_T *)&fc->fixvar[fixvar_idx++]
int fixvar_idx = 0
int ai
bool islambda = false
char_u numbuf [NUMBUFLEN]
char_uname = v->di_key
proftime_T wait_start
proftime_T call_start
int started_profiling = false
bool did_save_redo = false
save_redo_T save_redo
fp uf_calls
fc caller = current_funccal
fc func = fp
fc rettv = rettv
rettv vval v_number = 0
fc linenr = 0
fc returned = FALSE
fc level = ex_nesting_level
fc breakpoint = dbg_find_breakpoint(false, fp->uf_name, (linenr_T)0)
fc dbg_tick = debug_tick
fc fc_refcount = 0
fc fc_copyID = 0
fc l_avars dv_lock = VAR_FIXED
v di_flags = DI_FLAGS_RO | DI_FLAGS_FIX
v di_tv v_type = VAR_LIST
v di_tv v_lock = VAR_FIXED
v di_tv vval v_list = &fc->l_varlist
 save_sourcing_name = sourcing_name
 sourcing_lnum = 1
size_t len
 sourcing_name = xmalloc(len)
const bool do_profiling_yes = do_profiling == PROF_YES
bool func_not_yet_profiling_but_should
bool func_or_func_caller_profiling
const sctx_T save_current_sctx = current_sctx
 current_sctx = fp->uf_script_ctx
 did_emsg = FALSE
int int error = ERROR_NONE
char_u fname_buf [FLEN_FIXED+1]
char_utofree = NULL
char_ufname = fname_trans_sid(name, fname_buf, &tofree, &error)
int argcount = argcount_in
typval_Targvars = argvars_in
dict_Tselfdict = selfdict_in
typval_T argv [MAX_FUNC_ARGS+1] = VAR_STRING
int argv_clear = 0
doesrange = false
return ret = OK

Macro Definition Documentation

#define FC_ABORT   0x01
#define FC_CFUNC   0x80
#define FC_CLOSURE   0x08
#define FC_DELETED   0x10
#define FC_DICT   0x04
#define FC_RANGE   0x02
#define FC_REMOVED   0x20
#define FC_SANDBOX   0x40
#define FLEN_FIXED   40

Function Documentation

int call_func ( const char_u funcname,
int  len,
typval_T rettv,
int  argcount_in,
typval_T argvars_in,
ArgvFunc  argv_func,
linenr_T  firstline,
linenr_T  lastline,
int *  doesrange,
bool  evaluate,
partial_T partial,
dict_T selfdict_in 

Call a function with its resolved parameters

"argv_func", when not NULL, can be used to fill in arguments only when the invoked function uses them. It is called like this: new_argcount = argv_func(current_argcount, argv, called_func_argcount)

FAIL if function cannot be called, else OK (even if an error occurred while executing the function! Set msg_list to capture the error, see do_cmdline()).
void call_user_func ( ufunc_T fp,
int  argcount,
typval_T argvars,
typval_T rettv,
linenr_T  firstline,
linenr_T  lastline,
dict_T selfdict 

Call a user function

fpFunction to call.
[in]argcountNumber of arguments.
[out]rettvReturn value.
[in]firstlineFirst line of range.
[in]lastlineLast line of range.
selfdictDictionary for "self" for dictionary functions.
cleanup_function_call ( fc  )
int current_func_returned ( void  )
char_u* deref_func_name ( const char *  name,
int *  lenp,
partial_T **const  partialp,
bool  no_autoload 

Return name of the function corresponding to name

If name points to variable that is either a function or partial then corresponding function name is returned. Otherwise it returns name itself.

[in]nameFunction name to check.
[in,out]lenpLocation where length of the returned name is stored. Must be set to the length of the name argument.
[out]partialpLocation where partial will be stored if found function appears to be a partial. May be NULL if this is not needed.
[in]no_autoloadIf true, do not source autoload scripts if function was not found.
name of the function.
do_cmdline ( NULL  ,
get_func_line  ,
(void *)  fc,
int do_return ( exarg_T eap,
int  reanimate,
int  is_cmd,
void rettv 
int eval_fname_script ( const char *const  p)
void ex_call ( exarg_T eap)
void ex_delfunction ( exarg_T eap)

":delfunction {name}"

void ex_function ( exarg_T eap)
void ex_return ( exarg_T eap)
ga_init& fc ( ufunc_T )
add_nr_var& fc ( dictitem_T ) -> fixvar[fixvar_idx++],"firstline",(varnumber_T) firstline
ufunc_T* find_func ( const char_u name)

Find a function by name, return pointer to it in ufuncs.

NULL for unknown function.
hashitem_T* find_hi_in_scoped_ht ( const char *  name,
hashtab_T **  pht 

Search hashitem in parent scope.

dictitem_T* find_var_in_scoped_ht ( const char *  name,
const size_t  namelen,
int  no_autoload 

Search variable in parent scope.

for ( )
bool free_unref_funccal ( int  copyID,
int  testing 
linenr_T* func_breakpoint ( void cookie)
int func_call ( char_u name,
typval_T args,
partial_T partial,
dict_T selfdict,
typval_T rettv 
int* func_dbg_tick ( void cookie)
int func_has_abort ( void cookie)
int func_has_ended ( void cookie)
void func_init ( void  )
int func_level ( void cookie)
char_u* func_name ( void cookie)
func_ptr_ref ( fp  )
void func_ptr_ref ( ufunc_T fp)

Count a reference to a Function.

void func_ptr_unref ( ufunc_T fp)

Unreference a Function: decrement the reference count and free it when it becomes zero. Unreference user function, freeing it if needed

Decrements the reference count and frees when it becomes zero.

fpFunction to unreference.
void func_ref ( char_u name)

Count a reference to a Function.

void func_unref ( char_u name)
bool function_exists ( const char *const  name,
bool  no_deref 

Check whether function with the given name exists

[in]nameFunction name.
[in]no_derefWhether to dereference a Funcref.
True if it exists, false otherwise.
funccall_T* get_current_funccal ( void  )
dict_T* get_current_funccal_dict ( hashtab_T ht)

If "ht" is the hashtable for local variables in the current funccal, return the dict that contains it. Otherwise return NULL.

char_u* get_func_line ( int  c,
void cookie,
int  indent,
bool  do_concat 
int get_func_tv ( const char_u name,
int  len,
typval_T rettv,
char_u **  arg,
linenr_T  firstline,
linenr_T  lastline,
int *  doesrange,
int  evaluate,
partial_T partial,
dict_T selfdict 
funccall_T* get_funccal ( void  )
hashtab_T* get_funccal_args_ht ( void  )

Return the hashtable used for argument in the current funccal. Return NULL if there is no current funccal.

dictitem_T* get_funccal_args_var ( void  )

Return the a: scope variable. Return NULL if there is no current funccal.

hashtab_T* get_funccal_local_ht ( void  )

Return the hashtable used for local variables in the current funccal. Return NULL if there is no current funccal.

dictitem_T* get_funccal_local_var ( void  )

Return the l: scope variable. Return NULL if there is no current funccal.

char_u* get_lambda_name ( void  )

Get a name for a lambda. Returned in static memory.

int get_lambda_tv ( char_u **  arg,
typval_T rettv,
bool  evaluate 

Parse a lambda expression and get a Funcref from "*arg".

OK or FAIL. Returns NOTDONE for dict or {expr}.
char_u* get_return_cmd ( void rettv)
char_u* get_user_func_name ( expand_T xp,
int  idx 
if ( v!  = NULL && v->di_tv.v_type == VAR_FUNC)
if ( depth >=  p_mfd)
if ( ins_compl_active())
if ( STRNCMP(fp->uf_name,"<lambda>", 8)  = = 0)
if ( selfdict!  = NULL)
if ( fp->uf_flags &  FC_SANDBOX)
if ( p_verbose >=  12)
if ( do_profiling_yes  )
if ( (did_emsg &&(fp->uf_flags &FC_ABORT))||rettv->  v_type = VAR_UNKNOWN)
if ( using_sandbox  )
if ( p_verbose >=12 &&sourcing_name!  = NULL)
if ( --fp->uf_calls<=0 &&fp->uf_refcount<=  0)
if ( did_save_redo  )
if ( partial!  = NULL)
if ( error  = ERROR_NONE && evaluate)
line_breakcheck ( )
void list_func_vars ( int *  first)
void make_partial ( dict_T *const  selfdict,
typval_T *const  rettv 

Turn "dict.Func" into a partial for "Func" bound to "dict". Changes "rettv" in-place.

char_u* register_cfunc ( cfunc_T  cb,
cfunc_free_T  cb_free,
void state 

Registers a C extension user function.

void restore_funccal ( void  )
restore_search_patterns ( )
return ( char_u )
void save_funccal ( funccal_entry_T entry)
save_search_patterns ( )
void set_current_funccal ( funccall_T fc)
bool set_ref_in_call_stack ( int  copyID)

Set "copyID" in all local vars and arguments in the call stack.

bool set_ref_in_func ( char_u name,
ufunc_T fp_in,
int  copyID 

Mark all lists and dicts referenced through function "name" with "copyID". "list_stack" is used to add lists to be marked. Can be NULL. "ht_stack" is used to add hashtabs to be marked. Can be NULL.

true if setting references failed somehow.
bool set_ref_in_func_args ( int  copyID)

Set "copyID" in all function arguments.

bool set_ref_in_functions ( int  copyID)

Set "copyID" in all functions available by name.

bool set_ref_in_previous_funccal ( int  copyID)

Set "copyID + 1" in previous_funccal and callers.

STRCPY ( name  ,
char_u* trans_function_name ( char_u **  pp,
bool  skip,
int  flags,
funcdict_T fdp,
partial_T **  partial 

Get a function name, translating "<SID>" and "<SNR>". Also handles a Funcref in a List or Dictionary. flags: TFN_INT: internal function name OK TFN_QUIET: be quiet TFN_NO_AUTOLOAD: do not use script autoloading TFN_NO_DEREF: do not dereference a Funcref Advances "pp" to just after the function name (if no error).

the function name in allocated memory, or NULL for failure.
bool translated_function_exists ( const char *  name)
while ( argv_clear  ,
xfree ( sourcing_name  )
xfree ( tofree  )
xfree ( name  )

Variable Documentation

int ai
int argcount = argcount_in
int argv_clear = 0
typval_T* argvars = argvars_in
fc breakpoint = dbg_find_breakpoint(false, fp->uf_name, (linenr_T)0)
proftime_T call_start
fc caller = current_funccal
current_sctx = fp->uf_script_ctx
fc dbg_tick = debug_tick
v di_flags = DI_FLAGS_RO | DI_FLAGS_FIX
did_emsg = FALSE
bool did_save_redo = false
const bool do_profiling_yes = do_profiling == PROF_YES
* doesrange = false
fc l_avars dv_lock = VAR_FIXED
int int error = ERROR_NONE
add_nr_var & fc = xcalloc(1, sizeof(funccall_T))
fc fc_copyID = 0
fc fc_refcount = 0
int fixvar_idx = 0
return fname = fname_trans_sid(name, fname_buf, &tofree, &error)
char_u fname_buf[FLEN_FIXED+1]
ufunc_T* fp
fc func = fp
hashtab_T func_hashtab
bool func_not_yet_profiling_but_should
Initial value:
return NULL
Definition: eval.c:9846
bool has_profiling(bool file, char_u *fname, bool *fp)
Definition: ex_cmds2.c:778
const bool do_profiling_yes
Definition: userfunc.c:1006
int uf_profiling
true when func is being profiled
Definition: typval.h:318
char_u uf_name[]
Definition: typval.h:342
ufunc_T * fp
Definition: userfunc.c:1336
bool func_or_func_caller_profiling
Initial value:
return NULL
Definition: eval.c:9846
ufunc_T * func
Function being called.
Definition: typval.h:288
const bool do_profiling_yes
Definition: userfunc.c:1006
funccall_T * fc
Definition: userfunc.c:778
int uf_profiling
true when func is being profiled
Definition: typval.h:318
funccall_T * caller
Calling function or NULL.
Definition: typval.h:304
ufunc_T * fp
Definition: userfunc.c:1336
bool islambda = false
size_t len
Initial value:
+ STRLEN(fp->uf_name) + 27
return NULL
Definition: eval.c:9846
char_u uf_name[]
Definition: typval.h:342
#define STRLEN(s)
Definition: vim.h:215
ufunc_T * fp
Definition: userfunc.c:1336
Definition: userfunc.c:936
fc level = ex_nesting_level
fc linenr = 0
* name = v->di_key
char_u numbuf[NUMBUFLEN]
return ret = OK
fc rettv = rettv
fc returned = FALSE
const sctx_T save_current_sctx = current_sctx
save_did_emsg = did_emsg
save_redo_T save_redo
Initial value:
unsigned char char_u
Definition: types.h:11
Definition: userfunc.c:936
save_sourcing_name = sourcing_name
dict_T* selfdict = selfdict_in
sourcing_lnum = 1
sourcing_name = xmalloc(len)
int started_profiling = false
char_u* tofree = NULL
fp uf_calls
bool using_sandbox = false
v = (dictitem_T *)&fc->fixvar[fixvar_idx++]
v di_tv vval v_list = &fc->l_varlist
v di_tv v_lock = VAR_FIXED
rettv vval v_number = 0
rettv v_type = VAR_LIST
proftime_T wait_start