diff options
Diffstat (limited to 'src/nvim/eval.c')
-rw-r--r-- | src/nvim/eval.c | 4666 |
1 files changed, 3064 insertions, 1602 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 96c008b0e0..9c041ca790 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -44,7 +44,6 @@ #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/misc2.h" #include "nvim/keymap.h" #include "nvim/map.h" #include "nvim/file_search.h" @@ -63,10 +62,12 @@ #include "nvim/search.h" #include "nvim/sha256.h" #include "nvim/spell.h" +#include "nvim/state.h" #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/tag.h" #include "nvim/ui.h" +#include "nvim/main.h" #include "nvim/mouse.h" #include "nvim/terminal.h" #include "nvim/undo.h" @@ -76,9 +77,10 @@ #include "nvim/eval/decode.h" #include "nvim/os/os.h" #include "nvim/event/libuv_process.h" -#include "nvim/event/pty_process.h" +#include "nvim/os/pty_process.h" #include "nvim/event/rstream.h" #include "nvim/event/wstream.h" +#include "nvim/event/time.h" #include "nvim/os/time.h" #include "nvim/msgpack_rpc/channel.h" #include "nvim/msgpack_rpc/server.h" @@ -88,7 +90,10 @@ #include "nvim/os/dl.h" #include "nvim/os/input.h" #include "nvim/event/loop.h" +#include "nvim/lib/kvec.h" +#include "nvim/lib/khash.h" #include "nvim/lib/queue.h" +#include "nvim/eval/typval_encode.h" #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */ @@ -153,7 +158,7 @@ static char *e_listdictarg = N_( static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary"); static char *e_listreq = N_("E714: List required"); static char *e_dictreq = N_("E715: Dictionary required"); -static char *e_strreq = N_("E114: String required"); +static char *e_stringreq = N_("E928: String required"); static char *e_toomanyarg = N_("E118: Too many arguments for function: %s"); static char *e_dictkey = N_("E716: Key not present in Dictionary: %s"); static char *e_funcexts = N_( @@ -212,9 +217,11 @@ static int echo_attr = 0; /* attributes used for ":echo" */ /* The names of packages that once were loaded are remembered. */ static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; -/* list heads for garbage collection */ -static dict_T *first_dict = NULL; /* list of all dicts */ -static list_T *first_list = NULL; /* list of all lists */ +// List heads for garbage collection. Although there can be a reference loop +// from partial to dict to partial, we don't need to keep track of the partial, +// since it will get freed when the dict is unused and gets freed. +static dict_T *first_dict = NULL; // list of all dicts +static list_T *first_list = NULL; // list of all lists #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j] #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j] @@ -287,8 +294,8 @@ typedef enum { .vv_di = { \ .di_tv = { .v_type = type }, \ .di_flags = 0, \ + .di_key = { 0 }, \ }, \ - .vv_filler = { 0 }, \ .vv_flags = flags, \ } @@ -298,8 +305,7 @@ typedef enum { // variables with the VV_ defines. static struct vimvar { char *vv_name; ///< Name of the variable, without v:. - dictitem_T vv_di; ///< Value of the variable, with name. - char vv_filler[16]; ///< Space for longest name from below. + dictitem16_T vv_di; ///< Value and name for key (max 16 chars) char vv_flags; ///< Flags: #VV_COMPAT, #VV_RO, #VV_RO_SBX. } vimvars[] = { @@ -350,6 +356,7 @@ static struct vimvar { VV(VV_FCS_CHOICE, "fcs_choice", VAR_STRING, 0), VV(VV_BEVAL_BUFNR, "beval_bufnr", VAR_NUMBER, VV_RO), VV(VV_BEVAL_WINNR, "beval_winnr", VAR_NUMBER, VV_RO), + VV(VV_BEVAL_WINID, "beval_winid", VAR_NUMBER, VV_RO), VV(VV_BEVAL_LNUM, "beval_lnum", VAR_NUMBER, VV_RO), VV(VV_BEVAL_COL, "beval_col", VAR_NUMBER, VV_RO), VV(VV_BEVAL_TEXT, "beval_text", VAR_STRING, VV_RO), @@ -359,6 +366,7 @@ static struct vimvar { VV(VV_SWAPCOMMAND, "swapcommand", VAR_STRING, VV_RO), VV(VV_CHAR, "char", VAR_STRING, 0), VV(VV_MOUSE_WIN, "mouse_win", VAR_NUMBER, 0), + VV(VV_MOUSE_WINID, "mouse_winid", VAR_NUMBER, 0), VV(VV_MOUSE_LNUM, "mouse_lnum", VAR_NUMBER, 0), VV(VV_MOUSE_COL, "mouse_col", VAR_NUMBER, 0), VV(VV_OP, "operator", VAR_STRING, VV_RO), @@ -380,6 +388,15 @@ static struct vimvar { VV(VV_NULL, "null", VAR_SPECIAL, VV_RO), VV(VV__NULL_LIST, "_null_list", VAR_LIST, VV_RO), VV(VV__NULL_DICT, "_null_dict", VAR_DICT, VV_RO), + VV(VV_VIM_DID_ENTER, "vim_did_enter", VAR_NUMBER, VV_RO), + VV(VV_TYPE_NUMBER, "t_number", VAR_NUMBER, VV_RO), + VV(VV_TYPE_STRING, "t_string", VAR_NUMBER, VV_RO), + VV(VV_TYPE_FUNC, "t_func", VAR_NUMBER, VV_RO), + VV(VV_TYPE_LIST, "t_list", VAR_NUMBER, VV_RO), + VV(VV_TYPE_DICT, "t_dict", VAR_NUMBER, VV_RO), + VV(VV_TYPE_FLOAT, "t_float", VAR_NUMBER, VV_RO), + VV(VV_TYPE_BOOL, "t_bool", VAR_NUMBER, VV_RO), + VV(VV_EXITING, "exiting", VAR_NUMBER, VV_RO), }; #undef VV @@ -393,28 +410,43 @@ static struct vimvar { #define vv_dict vv_di.di_tv.vval.v_dict #define vv_tv vv_di.di_tv -static dictitem_T vimvars_var; /* variable used for v: */ +static dictitem_T vimvars_var; // variable used for v: #define vimvarht vimvardict.dv_hashtab +typedef enum { + kCallbackNone, + kCallbackFuncref, + kCallbackPartial, +} CallbackType; + +typedef struct { + union { + char_u *funcref; + partial_T *partial; + } data; + CallbackType type; +} Callback; +#define CALLBACK_NONE ((Callback){ .type = kCallbackNone }) + typedef struct { union { LibuvProcess uv; PtyProcess pty; } proc; - Stream in, out, err; + Stream in, out, err; // Initialized in common_job_start(). Terminal *term; bool stopped; bool exited; + bool rpc; int refcount; - ufunc_T *on_stdout, *on_stderr, *on_exit; - dict_T *self; + Callback on_stdout, on_stderr, on_exit; int *status_ptr; uint64_t id; - Queue *events; + MultiQueue *events; } TerminalJobData; typedef struct dict_watcher { - ufunc_T *callback; + Callback callback; char *key_pattern; QUEUE node; bool busy; // prevent recursion if the dict is changed in the callback @@ -422,12 +454,38 @@ typedef struct dict_watcher { typedef struct { TerminalJobData *data; - ufunc_T *callback; + Callback *callback; const char *type; list_T *received; int status; } JobEvent; +typedef struct { + TimeWatcher tw; + int timer_id; + int repeat_count; + int refcount; + long timeout; + bool stopped; + Callback callback; +} timer_T; + +typedef void (*FunPtr)(void); + +/// Prototype of C function that implements VimL function +typedef void (*VimLFunc)(typval_T *args, typval_T *rvar, FunPtr data); + +/// Structure holding VimL function definition +typedef struct fst { + char *name; ///< Name of the function. + uint8_t min_argc; ///< Minimal number of arguments. + uint8_t max_argc; ///< Maximal number of arguments. + VimLFunc func; ///< Function implementation. + FunPtr data; ///< Userdata for function implementation. +} VimLFuncDef; + +KHASH_MAP_INIT_STR(functions, VimLFuncDef) + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "eval.c.generated.h" #endif @@ -435,8 +493,10 @@ typedef struct { #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */ #define FNE_CHECK_START 2 /* find_name_end(): check name starts with valid character */ -static uint64_t current_job_id = 1; -static PMap(uint64_t) *jobs = NULL; +static PMap(uint64_t) *jobs = NULL; + +static uint64_t last_timer_id = 0; +static PMap(uint64_t) *timers = NULL; static const char *const msgpack_type_names[] = { [kMPNil] = "nil", @@ -469,6 +529,7 @@ void eval_init(void) vimvars[VV_VERSION].vv_nr = VIM_VERSION_100; jobs = pmap_new(uint64_t)(); + timers = pmap_new(uint64_t)(); struct vimvar *p; init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE); @@ -479,6 +540,7 @@ void eval_init(void) for (size_t i = 0; i < ARRAY_SIZE(vimvars); i++) { p = &vimvars[i]; + assert(STRLEN(p->vv_name) <= 16); STRCPY(p->vv_di.di_key, p->vv_name); if (p->vv_flags & VV_RO) p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; @@ -494,6 +556,7 @@ void eval_init(void) /* add to compat scope dict */ hash_add(&compat_hashtab, p->vv_di.di_key); } + vimvars[VV_VERSION].vv_nr = VIM_VERSION_100; dict_T *const msgpack_types_dict = dict_alloc(); for (size_t i = 0; i < ARRAY_SIZE(msgpack_type_names); i++) { @@ -523,10 +586,19 @@ void eval_init(void) set_vim_var_list(VV_ERRORS, list_alloc()); set_vim_var_nr(VV_SEARCHFORWARD, 1L); set_vim_var_nr(VV_HLSEARCH, 1L); + set_vim_var_nr(VV_COUNT1, 1); + set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER); + set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING); + set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC); + set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST); + set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT); + set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT); + set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL); set_vim_var_special(VV_FALSE, kSpecialVarFalse); set_vim_var_special(VV_TRUE, kSpecialVarTrue); set_vim_var_special(VV_NULL, kSpecialVarNull); + set_vim_var_special(VV_EXITING, kSpecialVarNull); set_reg_var(0); // default for v:register is not 0 but '"' } @@ -1152,7 +1224,7 @@ int call_vim_function( rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */ ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, curwin->w_cursor.lnum, curwin->w_cursor.lnum, - &doesrange, true, NULL); + &doesrange, true, NULL, NULL); if (safe) { --sandbox; restore_funccal(save_funccalp); @@ -1876,7 +1948,7 @@ ex_let_one ( } } if (p != NULL) { - write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE); + write_reg_contents(*arg == '@' ? '"' : *arg, p, STRLEN(p), false); arg_end = arg + 1; } xfree(ptofree); @@ -2106,11 +2178,9 @@ get_lval ( if (lp->ll_tv->v_type == VAR_DICT) { if (len == -1) { - /* "[key]": get key from "var1" */ - key = get_tv_string(&var1); /* is number or string */ - if (*key == NUL) { - if (!quiet) - EMSG(_(e_emptykey)); + // "[key]": get key from "var1" + key = get_tv_string_chk(&var1); // is number or string + if (key == NULL) { clear_tv(&var1); return NULL; } @@ -2401,6 +2471,7 @@ static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op) switch (tv1->v_type) { case VAR_DICT: case VAR_FUNC: + case VAR_PARTIAL: case VAR_SPECIAL: break; @@ -2550,9 +2621,12 @@ void *eval_for_line(char_u *arg, int *errp, char_u **nextcmdp, int skip) *errp = FALSE; if (!skip) { l = tv.vval.v_list; - if (tv.v_type != VAR_LIST || l == NULL) { + if (tv.v_type != VAR_LIST) { EMSG(_(e_listreq)); clear_tv(&tv); + } else if (l == NULL) { + // a null list is like an empty list: do nothing + clear_tv(&tv); } else { /* No need to increment the refcount, it's already set for the * list being used in "tv". */ @@ -2648,6 +2722,10 @@ void set_context_for_expression(expand_T *xp, char_u *arg, cmdidx_T cmdidx) } else if (c == '=') { got_eq = TRUE; xp->xp_context = EXPAND_EXPRESSION; + } else if (c == '#' + && xp->xp_context == EXPAND_EXPRESSION) { + // Autoload function/variable contains '#' + break; } else if ((c == '<' || c == '#') && xp->xp_context == EXPAND_FUNCTIONS && vim_strchr(xp->xp_pattern, '(') == NULL) { @@ -2698,8 +2776,9 @@ void ex_call(exarg_T *eap) typval_T rettv; linenr_T lnum; int doesrange; - int failed = FALSE; + bool failed = false; funcdict_T fudi; + partial_T *partial = NULL; if (eap->skip) { /* trans_function_name() doesn't work well when skipping, use eval0() @@ -2712,7 +2791,7 @@ void ex_call(exarg_T *eap) return; } - tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi); + tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial); if (fudi.fd_newkey != NULL) { /* Still need to give an error message for missing key. */ EMSG2(_(e_dictkey), fudi.fd_newkey); @@ -2726,9 +2805,12 @@ void ex_call(exarg_T *eap) if (fudi.fd_dict != NULL) ++fudi.fd_dict->dv_refcount; - /* If it is the name of a variable of type VAR_FUNC use its contents. */ + // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its + // contents. For VAR_PARTIAL get its partial, unless we already have one + // from trans_function_name(). len = (int)STRLEN(tofree); - name = deref_func_name(tofree, &len, FALSE); + name = deref_func_name(tofree, &len, + partial != NULL ? NULL : &partial, false); /* Skip white space to allow ":call func ()". Not good, but required for * backward compatibility. */ @@ -2759,9 +2841,9 @@ void ex_call(exarg_T *eap) } arg = startarg; if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg, - eap->line1, eap->line2, &doesrange, - !eap->skip, fudi.fd_dict) == FAIL) { - failed = TRUE; + eap->line1, eap->line2, &doesrange, + !eap->skip, partial, fudi.fd_dict) == FAIL) { + failed = true; break; } @@ -2964,11 +3046,16 @@ int do_unlet(char_u *name, int forceit) } else if (current_funccal != NULL && ht == ¤t_funccal->l_vars.dv_hashtab) { d = ¤t_funccal->l_vars; + } else if (ht == &compat_hashtab) { + d = &vimvardict; } else { di = find_var_in_ht(ht, *name, (char_u *)"", false); d = di->di_tv.vval.v_dict; } - + if (d == NULL) { + EMSG2(_(e_intern2), "do_unlet()"); + return FAIL; + } hi = hash_find(ht, varname); if (!HASHITEM_EMPTY(hi)) { di = HI2DI(hi); @@ -2977,6 +3064,11 @@ int do_unlet(char_u *name, int forceit) || tv_check_lock(d->dv_lock, name, false)) { return FAIL; } + + if (d == NULL || tv_check_lock(d->dv_lock, name, false)) { + return FAIL; + } + typval_T oldtv; bool watched = is_watched(dict); @@ -3114,6 +3206,7 @@ static void item_lock(typval_T *tv, int deep, int lock) case VAR_FLOAT: case VAR_STRING: case VAR_FUNC: + case VAR_PARTIAL: case VAR_SPECIAL: break; case VAR_UNKNOWN: @@ -3270,6 +3363,26 @@ char_u *get_user_var_name(expand_T *xp, int idx) } +/// Return TRUE if "pat" matches "text". +/// Does not use 'cpo' and always uses 'magic'. +static int pattern_match(char_u *pat, char_u *text, int ic) +{ + int matches = 0; + regmatch_T regmatch; + + // avoid 'l' flag in 'cpoptions' + char_u *save_cpo = p_cpo; + p_cpo = (char_u *)""; + regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); + if (regmatch.regprog != NULL) { + regmatch.rm_ic = ic; + matches = vim_regexec_nl(®match, text, (colnr_T)0); + vim_regfree(regmatch.regprog); + } + p_cpo = save_cpo; + return matches; +} + /* * types for expressions. */ @@ -3545,9 +3658,7 @@ static int eval4(char_u **arg, typval_T *rettv, int evaluate) long n1, n2; char_u *s1, *s2; char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; - regmatch_T regmatch; int ic; - char_u *save_cpo; /* * Get the first variable. @@ -3668,26 +3779,37 @@ static int eval4(char_u **arg, typval_T *rettv, int evaluate) if (type == TYPE_NEQUAL) n1 = !n1; } - } else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC) { - if (rettv->v_type != var2.v_type - || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) { - if (rettv->v_type != var2.v_type) - EMSG(_("E693: Can only compare Funcref with Funcref")); - else - EMSG(_("E694: Invalid operation for Funcrefs")); + } else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC + || rettv->v_type == VAR_PARTIAL + || var2.v_type == VAR_PARTIAL) { + if (type != TYPE_EQUAL && type != TYPE_NEQUAL) { + EMSG(_("E694: Invalid operation for Funcrefs")); clear_tv(rettv); clear_tv(&var2); return FAIL; + } + if ((rettv->v_type == VAR_PARTIAL + && rettv->vval.v_partial == NULL) + || (var2.v_type == VAR_PARTIAL + && var2.vval.v_partial == NULL)) { + // when a partial is NULL assume not equal + n1 = false; + } else if (type_is) { + if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC) { + // strings are considered the same if their value is + // the same + n1 = tv_equal(rettv, &var2, ic, false); + } else if (rettv->v_type == VAR_PARTIAL + && var2.v_type == VAR_PARTIAL) { + n1 = (rettv->vval.v_partial == var2.vval.v_partial); + } else { + n1 = false; + } } else { - /* Compare two Funcrefs for being equal or unequal. */ - if (rettv->vval.v_string == NULL - || var2.vval.v_string == NULL) - n1 = FALSE; - else - n1 = STRCMP(rettv->vval.v_string, - var2.vval.v_string) == 0; - if (type == TYPE_NEQUAL) - n1 = !n1; + n1 = tv_equal(rettv, &var2, ic, false); + } + if (type == TYPE_NEQUAL) { + n1 = !n1; } } /* @@ -3756,19 +3878,10 @@ static int eval4(char_u **arg, typval_T *rettv, int evaluate) case TYPE_MATCH: case TYPE_NOMATCH: - /* avoid 'l' flag in 'cpoptions' */ - save_cpo = p_cpo; - p_cpo = (char_u *)""; - regmatch.regprog = vim_regcomp(s2, - RE_MAGIC + RE_STRING); - regmatch.rm_ic = ic; - if (regmatch.regprog != NULL) { - n1 = vim_regexec_nl(®match, s1, (colnr_T)0); - vim_regfree(regmatch.regprog); - if (type == TYPE_NOMATCH) - n1 = !n1; + n1 = pattern_match(s2, s1, ic); + if (type == TYPE_NOMATCH) { + n1 = !n1; } - p_cpo = save_cpo; break; case TYPE_UNKNOWN: break; /* avoid gcc warning */ @@ -4231,14 +4344,15 @@ static int eval7( ret = FAIL; } else { if (**arg == '(') { // recursive! + partial_T *partial; // If "s" is the name of a variable of type VAR_FUNC // use its contents. - s = deref_func_name(s, &len, !evaluate); + s = deref_func_name(s, &len, &partial, !evaluate); // Invoke the function. ret = get_func_tv(s, len, rettv, arg, - curwin->w_cursor.lnum, curwin->w_cursor.lnum, - &len, evaluate, NULL); + curwin->w_cursor.lnum, curwin->w_cursor.lnum, + &len, evaluate, partial, NULL); // If evaluate is false rettv->v_type was not set in // get_func_tv, but it's needed in handle_subscript() to parse @@ -4341,7 +4455,8 @@ eval_index ( char_u *key = NULL; switch (rettv->v_type) { - case VAR_FUNC: { + case VAR_FUNC: + case VAR_PARTIAL: { if (verbose) { EMSG(_("E695: Cannot index a Funcref")); } @@ -4538,10 +4653,8 @@ eval_index ( dictitem_T *item; if (len == -1) { - key = get_tv_string(&var1); - if (*key == NUL) { - if (verbose) - EMSG(_(e_emptykey)); + key = get_tv_string_chk(&var1); + if (key == NULL) { clear_tv(&var1); return FAIL; } @@ -4563,6 +4676,7 @@ eval_index ( break; case VAR_SPECIAL: case VAR_FUNC: + case VAR_PARTIAL: case VAR_FLOAT: case VAR_UNKNOWN: break; // Not evaluating, skipping over subscript @@ -4816,10 +4930,29 @@ static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate) return OK; } -/* - * Allocate a variable for a List and fill it from "*arg". - * Return OK or FAIL. - */ +static void partial_free(partial_T *pt) +{ + for (int i = 0; i < pt->pt_argc; i++) { + clear_tv(&pt->pt_argv[i]); + } + xfree(pt->pt_argv); + dict_unref(pt->pt_dict); + func_unref(pt->pt_name); + xfree(pt->pt_name); + xfree(pt); +} + +/// Unreference a closure: decrement the reference count and free it when it +/// becomes zero. +void partial_unref(partial_T *pt) +{ + if (pt != NULL && --pt->pt_refcount <= 0) { + partial_free(pt); + } +} + +/// Allocate a variable for a List and fill it from "*arg". +/// Return OK or FAIL. static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate) { list_T *l = NULL; @@ -4853,8 +4986,9 @@ static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate) if (**arg != ']') { EMSG2(_("E697: Missing end of List ']': %s"), *arg); failret: - if (evaluate) - list_free(l, TRUE); + if (evaluate) { + list_free(l); + } return FAIL; } @@ -4893,51 +5027,53 @@ static list_T *rettv_list_alloc(typval_T *rettv) list_T *l = list_alloc(); rettv->vval.v_list = l; rettv->v_type = VAR_LIST; - ++l->lv_refcount; + rettv->v_lock = VAR_UNLOCKED; + l->lv_refcount++; return l; } -/* - * Unreference a list: decrement the reference count and free it when it - * becomes zero. - */ -void list_unref(list_T *l) -{ - if (l != NULL && --l->lv_refcount <= 0) - list_free(l, TRUE); +/// Unreference a list: decrement the reference count and free it when it +/// becomes zero. +void list_unref(list_T *l) { + if (l != NULL && --l->lv_refcount <= 0) { + list_free(l); + } } -/* - * Free a list, including all items it points to. - * Ignores the reference count. - */ -void -list_free ( - list_T *l, - int recurse /* Free Lists and Dictionaries recursively. */ -) -{ +/// Free a list, including all items it points to. +/// Ignores the reference count. +static void list_free_contents(list_T *l) { listitem_T *item; - /* Remove the list from the list of lists for garbage collection. */ - if (l->lv_used_prev == NULL) - first_list = l->lv_used_next; - else - l->lv_used_prev->lv_used_next = l->lv_used_next; - if (l->lv_used_next != NULL) - l->lv_used_next->lv_used_prev = l->lv_used_prev; - for (item = l->lv_first; item != NULL; item = l->lv_first) { - /* Remove the item before deleting it. */ + // Remove the item before deleting it. l->lv_first = item->li_next; - if (recurse || (item->li_tv.v_type != VAR_LIST - && item->li_tv.v_type != VAR_DICT)) - clear_tv(&item->li_tv); + clear_tv(&item->li_tv); xfree(item); } +} + +static void list_free_list(list_T *l) { + // Remove the list from the list of lists for garbage collection. + if (l->lv_used_prev == NULL) { + first_list = l->lv_used_next; + } else { + l->lv_used_prev->lv_used_next = l->lv_used_next; + } + if (l->lv_used_next != NULL) { + l->lv_used_next->lv_used_prev = l->lv_used_prev; + } + xfree(l); } +void list_free(list_T *l) { + if (!in_free_unref_items) { + list_free_contents(l); + list_free_list(l); + } +} + /* * Allocate a list item. * It is not initialized, don't forget to set v_lock. @@ -5026,12 +5162,18 @@ dict_equal ( dictitem_T *item2; int todo; - if (d1 == NULL || d2 == NULL) - return FALSE; - if (d1 == d2) - return TRUE; - if (dict_len(d1) != dict_len(d2)) - return FALSE; + if (d1 == NULL && d2 == NULL) { + return true; + } + if (d1 == NULL || d2 == NULL) { + return false; + } + if (d1 == d2) { + return true; + } + if (dict_len(d1) != dict_len(d2)) { + return false; + } todo = (int)d1->dv_hashtab.ht_used; for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi) { @@ -5049,6 +5191,60 @@ dict_equal ( static int tv_equal_recurse_limit; +static bool func_equal( + typval_T *tv1, + typval_T *tv2, + bool ic // ignore case +) { + char_u *s1, *s2; + dict_T *d1, *d2; + int a1, a2; + + // empty and NULL function name considered the same + s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string + : tv1->vval.v_partial->pt_name; + if (s1 != NULL && *s1 == NUL) { + s1 = NULL; + } + s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string + : tv2->vval.v_partial->pt_name; + if (s2 != NULL && *s2 == NUL) { + s2 = NULL; + } + if (s1 == NULL || s2 == NULL) { + if (s1 != s2) { + return false; + } + } else if (STRCMP(s1, s2) != 0) { + return false; + } + + // empty dict and NULL dict is different + d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict; + d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict; + if (d1 == NULL || d2 == NULL) { + if (d1 != d2) { + return false; + } + } else if (!dict_equal(d1, d2, ic, true)) { + return false; + } + + // empty list and no list considered the same + a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc; + a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc; + if (a1 != a2) { + return false; + } + for (int i = 0; i < a1; i++) { + if (!tv_equal(tv1->vval.v_partial->pt_argv + i, + tv2->vval.v_partial->pt_argv + i, ic, true)) { + return false; + } + } + return true; +} + /* * Return TRUE if "tv1" and "tv2" have the same value. * Compares the items just like "==" would compare them, but strings and @@ -5067,9 +5263,6 @@ tv_equal ( static int recursive_cnt = 0; /* catch recursive loops */ int r; - if (tv1->v_type != tv2->v_type) - return FALSE; - /* Catch lists and dicts that have an endless loop by limiting * recursiveness to a limit. We guess they are equal then. * A fixed limit has the problem of still taking an awful long time. @@ -5083,6 +5276,20 @@ tv_equal ( return TRUE; } + // For VAR_FUNC and VAR_PARTIAL only compare the function name. + if ((tv1->v_type == VAR_FUNC + || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL)) + && (tv2->v_type == VAR_FUNC + || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL))) { + recursive_cnt++; + r = func_equal(tv1, tv2, ic); + recursive_cnt--; + return r; + } + if (tv1->v_type != tv2->v_type) { + return false; + } + switch (tv1->v_type) { case VAR_LIST: ++recursive_cnt; @@ -5096,11 +5303,6 @@ tv_equal ( --recursive_cnt; return r; - case VAR_FUNC: - return tv1->vval.v_string != NULL - && tv2->vval.v_string != NULL - && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0; - case VAR_NUMBER: return tv1->vval.v_number == tv2->vval.v_number; @@ -5115,6 +5317,8 @@ tv_equal ( case VAR_SPECIAL: return tv1->vval.v_special == tv2->vval.v_special; + case VAR_FUNC: + case VAR_PARTIAL: case VAR_UNKNOWN: // VAR_UNKNOWN can be the result of an invalid expression, let’s say it does // not equal anything, not even self. @@ -5728,7 +5932,17 @@ bool garbage_collect(void) { TerminalJobData *data; map_foreach_value(jobs, data, { - ABORTING(set_ref_dict)(data->self, copyID); + set_ref_in_callback(&data->on_stdout, copyID, NULL, NULL); + set_ref_in_callback(&data->on_stderr, copyID, NULL, NULL); + set_ref_in_callback(&data->on_exit, copyID, NULL, NULL); + }) + } + + // Timers + { + timer_T *timer; + map_foreach_value(timers, timer, { + set_ref_in_callback(&timer->callback, copyID, NULL, NULL); }) } @@ -5804,42 +6018,63 @@ bool garbage_collect(void) /// @return true, if something was freed. static int free_unref_items(int copyID) { + dict_T *dd, *dd_next; + list_T *ll, *ll_next; bool did_free = false; + // Let all "free" functions know that we are here. This means no + // dictionaries, lists, or jobs are to be freed, because we will + // do that here. + in_free_unref_items = true; + + // PASS 1: free the contents of the items. We don't free the items + // themselves yet, so that it is possible to decrement refcount counters. + // Go through the list of dicts and free items without the copyID. // Don't free dicts that are referenced internally. - for (dict_T *dd = first_dict; dd != NULL; ) { + for (dict_T *dd = first_dict; dd != NULL; dd = dd->dv_used_next) { if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)) { // Free the Dictionary and ordinary items it contains, but don't // recurse into Lists and Dictionaries, they will be in the list - // of dicts or list of lists. */ - dict_T *dd_next = dd->dv_used_next; - dict_free(dd, FALSE); + // of dicts or list of lists. + dict_free_contents(dd); did_free = true; - dd = dd_next; - } else { - dd = dd->dv_used_next; } } // Go through the list of lists and free items without the copyID. // But don't free a list that has a watcher (used in a for loop), these // are not referenced anywhere. - for (list_T *ll = first_list; ll != NULL;) { + for (list_T *ll = first_list; ll != NULL; ll = ll->lv_used_next) { if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK) && ll->lv_watch == NULL) { // Free the List and ordinary items it contains, but don't recurse // into Lists and Dictionaries, they will be in the list of dicts // or list of lists. - list_T* ll_next = ll->lv_used_next; - list_free(ll, FALSE); + list_free_contents(ll); did_free = true; - ll = ll_next; - } else { - ll = ll->lv_used_next; } } + // PASS 2: free the items themselves. + for (dd = first_dict; dd != NULL; dd = dd_next) { + dd_next = dd->dv_used_next; + if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)) { + dict_free_dict(dd); + } + } + + for (ll = first_list; ll != NULL; ll = ll_next) { + ll_next = ll->lv_used_next; + if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK) + && ll->lv_watch == NULL) { + // Free the List and ordinary items it contains, but don't recurse + // into Lists and Dictionaries, they will be in the list of dicts + // or list of lists. + list_free_list(ll); + } + } + in_free_unref_items = false; return did_free; } @@ -5957,6 +6192,24 @@ bool set_ref_in_item(typval_T *tv, int copyID, ht_stack_T **ht_stack, *ht_stack = newitem; } } + + QUEUE *w = NULL; + DictWatcher *watcher = NULL; + QUEUE_FOREACH(w, &dd->watchers) { + watcher = dictwatcher_node_data(w); + set_ref_in_callback(&watcher->callback, copyID, ht_stack, list_stack); + } + } + if (tv->v_type == VAR_PARTIAL) { + partial_T *pt = tv->vval.v_partial; + int i; + + if (pt != NULL) { + for (i = 0; i < pt->pt_argc; i++) { + abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID, + ht_stack, list_stack); + } + } } break; } @@ -5982,6 +6235,26 @@ bool set_ref_in_item(typval_T *tv, int copyID, ht_stack_T **ht_stack, break; } + case VAR_PARTIAL: { + partial_T *pt = tv->vval.v_partial; + + // A partial does not have a copyID, because it cannot contain itself. + if (pt != NULL) { + if (pt->pt_dict != NULL) { + typval_T dtv; + + dtv.v_type = VAR_DICT; + dtv.vval.v_dict = pt->pt_dict; + abort = abort || set_ref_in_item(&dtv, copyID, ht_stack, list_stack); + } + + for (int i = 0; i < pt->pt_argc; i++) { + abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID, + ht_stack, list_stack); + } + } + break; + } case VAR_FUNC: case VAR_UNKNOWN: case VAR_SPECIAL: @@ -6073,7 +6346,8 @@ static void rettv_dict_alloc(typval_T *rettv) rettv->vval.v_dict = d; rettv->v_type = VAR_DICT; - ++d->dv_refcount; + rettv->v_lock = VAR_UNLOCKED; + d->dv_refcount++; } /// Clear all the keys of a Dictionary. "d" remains a valid empty Dictionary. @@ -6104,31 +6378,18 @@ void dict_clear(dict_T *d) */ void dict_unref(dict_T *d) { - if (d != NULL && --d->dv_refcount <= 0) - dict_free(d, TRUE); + if (d != NULL && --d->dv_refcount <= 0) { + dict_free(d); + } } -/* - * Free a Dictionary, including all items it contains. - * Ignores the reference count. - */ -void -dict_free ( - dict_T *d, - int recurse /* Free Lists and Dictionaries recursively. */ -) -{ +/// Free a Dictionary, including all items it contains. +/// Ignores the reference count. +static void dict_free_contents(dict_T *d) { int todo; hashitem_T *hi; dictitem_T *di; - /* Remove the dict from the list of dicts for garbage collection. */ - if (d->dv_used_prev == NULL) - first_dict = d->dv_used_next; - else - d->dv_used_prev->dv_used_next = d->dv_used_next; - if (d->dv_used_next != NULL) - d->dv_used_next->dv_used_prev = d->dv_used_prev; /* Lock the hashtab, we don't want it to resize while freeing items. */ hash_lock(&d->dv_hashtab); @@ -6140,9 +6401,7 @@ dict_free ( * something recursive causing trouble. */ di = HI2DI(hi); hash_remove(&d->dv_hashtab, hi); - if (recurse || (di->di_tv.v_type != VAR_LIST - && di->di_tv.v_type != VAR_DICT)) - clear_tv(&di->di_tv); + clear_tv(&di->di_tv); xfree(di); --todo; } @@ -6151,14 +6410,34 @@ dict_free ( while (!QUEUE_EMPTY(&d->watchers)) { QUEUE *w = QUEUE_HEAD(&d->watchers); DictWatcher *watcher = dictwatcher_node_data(w); - dictwatcher_free(watcher); QUEUE_REMOVE(w); + dictwatcher_free(watcher); } hash_clear(&d->dv_hashtab); +} + +static void dict_free_dict(dict_T *d) { + // Remove the dict from the list of dicts for garbage collection. + if (d->dv_used_prev == NULL) { + first_dict = d->dv_used_next; + } else { + d->dv_used_prev->dv_used_next = d->dv_used_next; + } + if (d->dv_used_next != NULL) { + d->dv_used_next->dv_used_prev = d->dv_used_prev; + } + xfree(d); } +void dict_free(dict_T *d) { + if (!in_free_unref_items) { + dict_free_contents(d); + dict_free_dict(d); + } +} + /* * Allocate a Dictionary item. * The "key" is copied to the new item. @@ -6166,7 +6445,7 @@ dict_free ( */ dictitem_T *dictitem_alloc(char_u *key) FUNC_ATTR_NONNULL_RET { - dictitem_T *di = xmalloc(sizeof(dictitem_T) + STRLEN(key)); + dictitem_T *di = xmalloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1); #ifndef __clang_analyzer__ STRCPY(di->di_key, key); #endif @@ -6338,6 +6617,23 @@ int dict_add_list(dict_T *d, char *key, list_T *list) return OK; } +/// Add a dict entry to dictionary "d". +/// Returns FAIL when out of memory and when key already exists. +int dict_add_dict(dict_T *d, char *key, dict_T *dict) +{ + dictitem_T *item = dictitem_alloc((char_u *)key); + + item->di_tv.v_lock = 0; + item->di_tv.v_type = VAR_DICT; + item->di_tv.vval.v_dict = dict; + if (dict_add(d, item) == FAIL) { + dictitem_free(item); + return FAIL; + } + dict->dv_refcount++; + return OK; +} + /// Set all existing keys in "dict" as read-only. /// /// This does not protect against adding new keys to the Dictionary. @@ -6379,9 +6675,12 @@ dictitem_T *dict_find(dict_T *d, char_u *key, int len) char_u *tofree = NULL; hashitem_T *hi; - if (len < 0) + if (d == NULL) { + return NULL; + } + if (len < 0) { akey = key; - else if (len >= AKEYLEN) { + } else if (len >= AKEYLEN) { tofree = akey = vim_strnsave(key, len); } else { /* Avoid a malloc/free by using buf[]. */ @@ -6400,64 +6699,45 @@ dictitem_T *dict_find(dict_T *d, char_u *key, int len) /// @param[out] result The address where a pointer to the wanted callback /// will be left. /// @return true/false on success/failure. -static bool get_dict_callback(dict_T *d, char *key, ufunc_T **result) +static bool get_dict_callback(dict_T *d, char *key, Callback *result) { dictitem_T *di = dict_find(d, (uint8_t *)key, -1); if (di == NULL) { - *result = NULL; + result->type = kCallbackNone; return true; } - - if (di->di_tv.v_type != VAR_FUNC && di->di_tv.v_type != VAR_STRING) { - EMSG(_("Argument is not a function or function name")); - *result = NULL; - return false; - } - if ((*result = find_ufunc(di->di_tv.vval.v_string)) == NULL) { + if (di->di_tv.v_type != VAR_FUNC && di->di_tv.v_type != VAR_STRING + && di->di_tv.v_type != VAR_PARTIAL) { + EMSG(_("Argument is not a function or function name")); + result->type = kCallbackNone; return false; } - (*result)->uf_refcount++; - return true; -} - -static ufunc_T *find_ufunc(uint8_t *name) -{ - uint8_t *n = name; - ufunc_T *rv = NULL; - if (*n > '9' || *n < '0') { - if ((n = trans_function_name(&n, false, TFN_INT|TFN_QUIET, NULL))) { - rv = find_func(n); - xfree(n); - } - } else { - // dict function, name is already translated - rv = find_func(n); - } - - if (!rv) { - EMSG2(_("Function %s doesn't exist"), name); - return NULL; - } - - return rv; + typval_T tv; + copy_tv(&di->di_tv, &tv); + set_selfdict(&tv, d); + bool res = callback_from_typval(result, &tv); + clear_tv(&tv); + return res; } -/* - * Get a string item from a dictionary. - * When "save" is TRUE allocate memory for it. - * Returns NULL if the entry doesn't exist. - */ -char_u *get_dict_string(dict_T *d, char_u *key, int save) +/// Get a string item from a dictionary. +/// +/// @param save whether memory should be allocated for the return value +/// when false a shared buffer is used, can only be used once! +/// +/// @return the entry or NULL if the entry doesn't exist. +char_u *get_dict_string(dict_T *d, char *key, bool save) { dictitem_T *di; char_u *s; - di = dict_find(d, key, -1); - if (di == NULL) + di = dict_find(d, (char_u *)key, -1); + if (di == NULL) { return NULL; + } s = get_tv_string(&di->di_tv); if (save) { s = vim_strsave(s); @@ -6465,15 +6745,15 @@ char_u *get_dict_string(dict_T *d, char_u *key, int save) return s; } -/* - * Get a number item from a dictionary. - * Returns 0 if the entry doesn't exist. - */ -long get_dict_number(dict_T *d, char_u *key) +/// Get a number item from a dictionary. +/// +/// @return the entry or 0 if the entry doesn't exist. +long get_dict_number(dict_T *d, char *key) { - dictitem_T *di = dict_find(d, key, -1); - if (di == NULL) + dictitem_T *di = dict_find(d, (char_u *)key, -1); + if (di == NULL) { return 0; + } return get_tv_number(&di->di_tv); } @@ -6522,10 +6802,8 @@ static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate) } if (evaluate) { key = get_tv_string_buf_chk(&tvkey, buf); - if (key == NULL || *key == NUL) { - /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */ - if (key != NULL) - EMSG(_(e_emptykey)); + if (key == NULL) { + // "key" is NULL when get_tv_string_buf_chk() gave an errmsg clear_tv(&tvkey); goto failret; } @@ -6566,8 +6844,9 @@ static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate) if (**arg != '}') { EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg); failret: - if (evaluate) - dict_free(d, TRUE); + if (evaluate) { + dict_free(d); + } return FAIL; } @@ -6643,319 +6922,9 @@ static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate) return OK; } -/* - * Array with names and number of arguments of all internal functions - * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH! - */ -static struct fst { - char *f_name; /* function name */ - char f_min_argc; /* minimal number of arguments */ - char f_max_argc; /* maximal number of arguments */ - void (*f_func)(typval_T *args, typval_T *rvar); - /* implementation of function */ -} functions[] = -{ - { "abs", 1, 1, f_abs }, - { "acos", 1, 1, f_acos }, // WJMc - { "add", 2, 2, f_add }, - { "and", 2, 2, f_and }, - { "append", 2, 2, f_append }, - { "argc", 0, 0, f_argc }, - { "argidx", 0, 0, f_argidx }, - { "arglistid", 0, 2, f_arglistid }, - { "argv", 0, 1, f_argv }, - { "asin", 1, 1, f_asin }, // WJMc - { "assert_equal", 2, 3, f_assert_equal }, - { "assert_exception", 1, 2, f_assert_exception }, - { "assert_false", 1, 2, f_assert_false }, - { "assert_true", 1, 2, f_assert_true }, - { "atan", 1, 1, f_atan }, - { "atan2", 2, 2, f_atan2 }, - { "browse", 4, 4, f_browse }, - { "browsedir", 2, 2, f_browsedir }, - { "bufexists", 1, 1, f_bufexists }, - { "buffer_exists", 1, 1, f_bufexists }, // obsolete - { "buffer_name", 1, 1, f_bufname }, // obsolete - { "buffer_number", 1, 1, f_bufnr }, // obsolete - { "buflisted", 1, 1, f_buflisted }, - { "bufloaded", 1, 1, f_bufloaded }, - { "bufname", 1, 1, f_bufname }, - { "bufnr", 1, 2, f_bufnr }, - { "bufwinnr", 1, 1, f_bufwinnr }, - { "byte2line", 1, 1, f_byte2line }, - { "byteidx", 2, 2, f_byteidx }, - { "byteidxcomp", 2, 2, f_byteidxcomp }, - { "call", 2, 3, f_call }, - { "ceil", 1, 1, f_ceil }, - { "changenr", 0, 0, f_changenr }, - { "char2nr", 1, 2, f_char2nr }, - { "cindent", 1, 1, f_cindent }, - { "clearmatches", 0, 0, f_clearmatches }, - { "col", 1, 1, f_col }, - { "complete", 2, 2, f_complete }, - { "complete_add", 1, 1, f_complete_add }, - { "complete_check", 0, 0, f_complete_check }, - { "confirm", 1, 4, f_confirm }, - { "copy", 1, 1, f_copy }, - { "cos", 1, 1, f_cos }, - { "cosh", 1, 1, f_cosh }, - { "count", 2, 4, f_count }, - { "cscope_connection", 0, 3, f_cscope_connection }, - { "cursor", 1, 3, f_cursor }, - { "deepcopy", 1, 2, f_deepcopy }, - { "delete", 1, 2, f_delete }, - { "dictwatcheradd", 3, 3, f_dictwatcheradd }, - { "dictwatcherdel", 3, 3, f_dictwatcherdel }, - { "did_filetype", 0, 0, f_did_filetype }, - { "diff_filler", 1, 1, f_diff_filler }, - { "diff_hlID", 2, 2, f_diff_hlID }, - { "empty", 1, 1, f_empty }, - { "escape", 2, 2, f_escape }, - { "eval", 1, 1, f_eval }, - { "eventhandler", 0, 0, f_eventhandler }, - { "executable", 1, 1, f_executable }, - { "exepath", 1, 1, f_exepath }, - { "exists", 1, 1, f_exists }, - { "exp", 1, 1, f_exp }, - { "expand", 1, 3, f_expand }, - { "extend", 2, 3, f_extend }, - { "feedkeys", 1, 2, f_feedkeys }, - { "file_readable", 1, 1, f_filereadable }, // obsolete - { "filereadable", 1, 1, f_filereadable }, - { "filewritable", 1, 1, f_filewritable }, - { "filter", 2, 2, f_filter }, - { "finddir", 1, 3, f_finddir }, - { "findfile", 1, 3, f_findfile }, - { "float2nr", 1, 1, f_float2nr }, - { "floor", 1, 1, f_floor }, - { "fmod", 2, 2, f_fmod }, - { "fnameescape", 1, 1, f_fnameescape }, - { "fnamemodify", 2, 2, f_fnamemodify }, - { "foldclosed", 1, 1, f_foldclosed }, - { "foldclosedend", 1, 1, f_foldclosedend }, - { "foldlevel", 1, 1, f_foldlevel }, - { "foldtext", 0, 0, f_foldtext }, - { "foldtextresult", 1, 1, f_foldtextresult }, - { "foreground", 0, 0, f_foreground }, - { "function", 1, 1, f_function }, - { "garbagecollect", 0, 1, f_garbagecollect }, - { "get", 2, 3, f_get }, - { "getbufline", 2, 3, f_getbufline }, - { "getbufvar", 2, 3, f_getbufvar }, - { "getchar", 0, 1, f_getchar }, - { "getcharmod", 0, 0, f_getcharmod }, - { "getcharsearch", 0, 0, f_getcharsearch }, - { "getcmdline", 0, 0, f_getcmdline }, - { "getcmdpos", 0, 0, f_getcmdpos }, - { "getcmdtype", 0, 0, f_getcmdtype }, - { "getcmdwintype", 0, 0, f_getcmdwintype }, - { "getcurpos", 0, 0, f_getcurpos }, - { "getcwd", 0, 2, f_getcwd }, - { "getfontname", 0, 1, f_getfontname }, - { "getfperm", 1, 1, f_getfperm }, - { "getfsize", 1, 1, f_getfsize }, - { "getftime", 1, 1, f_getftime }, - { "getftype", 1, 1, f_getftype }, - { "getline", 1, 2, f_getline }, - { "getloclist", 1, 1, f_getqflist }, - { "getmatches", 0, 0, f_getmatches }, - { "getpid", 0, 0, f_getpid }, - { "getpos", 1, 1, f_getpos }, - { "getqflist", 0, 0, f_getqflist }, - { "getreg", 0, 3, f_getreg }, - { "getregtype", 0, 1, f_getregtype }, - { "gettabvar", 2, 3, f_gettabvar }, - { "gettabwinvar", 3, 4, f_gettabwinvar }, - { "getwinposx", 0, 0, f_getwinposx }, - { "getwinposy", 0, 0, f_getwinposy }, - { "getwinvar", 2, 3, f_getwinvar }, - { "glob", 1, 4, f_glob }, - { "glob2regpat", 1, 1, f_glob2regpat }, - { "globpath", 2, 5, f_globpath }, - { "has", 1, 1, f_has }, - { "has_key", 2, 2, f_has_key }, - { "haslocaldir", 0, 2, f_haslocaldir }, - { "hasmapto", 1, 3, f_hasmapto }, - { "highlightID", 1, 1, f_hlID }, // obsolete - { "highlight_exists", 1, 1, f_hlexists }, // obsolete - { "histadd", 2, 2, f_histadd }, - { "histdel", 1, 2, f_histdel }, - { "histget", 1, 2, f_histget }, - { "histnr", 1, 1, f_histnr }, - { "hlID", 1, 1, f_hlID }, - { "hlexists", 1, 1, f_hlexists }, - { "hostname", 0, 0, f_hostname }, - { "iconv", 3, 3, f_iconv }, - { "indent", 1, 1, f_indent }, - { "index", 2, 4, f_index }, - { "input", 1, 3, f_input }, - { "inputdialog", 1, 3, f_inputdialog }, - { "inputlist", 1, 1, f_inputlist }, - { "inputrestore", 0, 0, f_inputrestore }, - { "inputsave", 0, 0, f_inputsave }, - { "inputsecret", 1, 2, f_inputsecret }, - { "insert", 2, 3, f_insert }, - { "invert", 1, 1, f_invert }, - { "isdirectory", 1, 1, f_isdirectory }, - { "islocked", 1, 1, f_islocked }, - { "items", 1, 1, f_items }, - { "jobclose", 1, 2, f_jobclose }, - { "jobpid", 1, 1, f_jobpid }, - { "jobresize", 3, 3, f_jobresize }, - { "jobsend", 2, 2, f_jobsend }, - { "jobstart", 1, 2, f_jobstart }, - { "jobstop", 1, 1, f_jobstop }, - { "jobwait", 1, 2, f_jobwait }, - { "join", 1, 2, f_join }, - { "json_decode", 1, 1, f_json_decode }, - { "json_encode", 1, 1, f_json_encode }, - { "keys", 1, 1, f_keys }, - { "last_buffer_nr", 0, 0, f_last_buffer_nr }, // obsolete - { "len", 1, 1, f_len }, - { "libcall", 3, 3, f_libcall }, - { "libcallnr", 3, 3, f_libcallnr }, - { "line", 1, 1, f_line }, - { "line2byte", 1, 1, f_line2byte }, - { "lispindent", 1, 1, f_lispindent }, - { "localtime", 0, 0, f_localtime }, - { "log", 1, 1, f_log }, - { "log10", 1, 1, f_log10 }, - { "map", 2, 2, f_map }, - { "maparg", 1, 4, f_maparg }, - { "mapcheck", 1, 3, f_mapcheck }, - { "match", 2, 4, f_match }, - { "matchadd", 2, 5, f_matchadd }, - { "matchaddpos", 2, 5, f_matchaddpos }, - { "matcharg", 1, 1, f_matcharg }, - { "matchdelete", 1, 1, f_matchdelete }, - { "matchend", 2, 4, f_matchend }, - { "matchlist", 2, 4, f_matchlist }, - { "matchstr", 2, 4, f_matchstr }, - { "max", 1, 1, f_max }, - { "min", 1, 1, f_min }, - { "mkdir", 1, 3, f_mkdir }, - { "mode", 0, 1, f_mode }, - { "msgpackdump", 1, 1, f_msgpackdump }, - { "msgpackparse", 1, 1, f_msgpackparse }, - { "nextnonblank", 1, 1, f_nextnonblank }, - { "nr2char", 1, 2, f_nr2char }, - { "or", 2, 2, f_or }, - { "pathshorten", 1, 1, f_pathshorten }, - { "pow", 2, 2, f_pow }, - { "prevnonblank", 1, 1, f_prevnonblank }, - { "printf", 2, MAX_FUNC_ARGS, f_printf }, - { "pumvisible", 0, 0, f_pumvisible }, - { "py3eval", 1, 1, f_py3eval }, - { "pyeval", 1, 1, f_pyeval }, - { "range", 1, 3, f_range }, - { "readfile", 1, 3, f_readfile }, - { "reltime", 0, 2, f_reltime }, - { "reltimefloat", 1, 1, f_reltimefloat }, - { "reltimestr", 1, 1, f_reltimestr }, - { "remove", 2, 3, f_remove }, - { "rename", 2, 2, f_rename }, - { "repeat", 2, 2, f_repeat }, - { "resolve", 1, 1, f_resolve }, - { "reverse", 1, 1, f_reverse }, - { "round", 1, 1, f_round }, - { "rpcnotify", 2, MAX_FUNC_ARGS, f_rpcnotify }, - { "rpcrequest", 2, MAX_FUNC_ARGS, f_rpcrequest }, - { "rpcstart", 1, 2, f_rpcstart }, - { "rpcstop", 1, 1, f_rpcstop }, - { "screenattr", 2, 2, f_screenattr }, - { "screenchar", 2, 2, f_screenchar }, - { "screencol", 0, 0, f_screencol }, - { "screenrow", 0, 0, f_screenrow }, - { "search", 1, 4, f_search }, - { "searchdecl", 1, 3, f_searchdecl }, - { "searchpair", 3, 7, f_searchpair }, - { "searchpairpos", 3, 7, f_searchpairpos }, - { "searchpos", 1, 4, f_searchpos }, - { "serverlist", 0, 0, f_serverlist }, - { "serverstart", 0, 1, f_serverstart }, - { "serverstop", 1, 1, f_serverstop }, - { "setbufvar", 3, 3, f_setbufvar }, - { "setcharsearch", 1, 1, f_setcharsearch }, - { "setcmdpos", 1, 1, f_setcmdpos }, - { "setfperm", 2, 2, f_setfperm }, - { "setline", 2, 2, f_setline }, - { "setloclist", 2, 4, f_setloclist }, - { "setmatches", 1, 1, f_setmatches }, - { "setpos", 2, 2, f_setpos }, - { "setqflist", 1, 3, f_setqflist }, - { "setreg", 2, 3, f_setreg }, - { "settabvar", 3, 3, f_settabvar }, - { "settabwinvar", 4, 4, f_settabwinvar }, - { "setwinvar", 3, 3, f_setwinvar }, - { "sha256", 1, 1, f_sha256 }, - { "shellescape", 1, 2, f_shellescape }, - { "shiftwidth", 0, 0, f_shiftwidth }, - { "simplify", 1, 1, f_simplify }, - { "sin", 1, 1, f_sin }, - { "sinh", 1, 1, f_sinh }, - { "sort", 1, 3, f_sort }, - { "soundfold", 1, 1, f_soundfold }, - { "spellbadword", 0, 1, f_spellbadword }, - { "spellsuggest", 1, 3, f_spellsuggest }, - { "split", 1, 3, f_split }, - { "sqrt", 1, 1, f_sqrt }, - { "str2float", 1, 1, f_str2float }, - { "str2nr", 1, 2, f_str2nr }, - { "strchars", 1, 2, f_strchars }, - { "strdisplaywidth", 1, 2, f_strdisplaywidth }, - { "strftime", 1, 2, f_strftime }, - { "stridx", 2, 3, f_stridx }, - { "string", 1, 1, f_string }, - { "strlen", 1, 1, f_strlen }, - { "strpart", 2, 3, f_strpart }, - { "strridx", 2, 3, f_strridx }, - { "strtrans", 1, 1, f_strtrans }, - { "strwidth", 1, 1, f_strwidth }, - { "submatch", 1, 2, f_submatch }, - { "substitute", 4, 4, f_substitute }, - { "synID", 3, 3, f_synID }, - { "synIDattr", 2, 3, f_synIDattr }, - { "synIDtrans", 1, 1, f_synIDtrans }, - { "synconcealed", 2, 2, f_synconcealed }, - { "synstack", 2, 2, f_synstack }, - { "system", 1, 2, f_system }, - { "systemlist", 1, 3, f_systemlist }, - { "tabpagebuflist", 0, 1, f_tabpagebuflist }, - { "tabpagenr", 0, 1, f_tabpagenr }, - { "tabpagewinnr", 1, 2, f_tabpagewinnr }, - { "tagfiles", 0, 0, f_tagfiles }, - { "taglist", 1, 1, f_taglist }, - { "tan", 1, 1, f_tan }, - { "tanh", 1, 1, f_tanh }, - { "tempname", 0, 0, f_tempname }, - { "termopen", 1, 2, f_termopen }, - { "test", 1, 1, f_test }, - { "tolower", 1, 1, f_tolower }, - { "toupper", 1, 1, f_toupper }, - { "tr", 3, 3, f_tr }, - { "trunc", 1, 1, f_trunc }, - { "type", 1, 1, f_type }, - { "undofile", 1, 1, f_undofile }, - { "undotree", 0, 0, f_undotree }, - { "uniq", 1, 3, f_uniq }, - { "values", 1, 1, f_values }, - { "virtcol", 1, 1, f_virtcol }, - { "visualmode", 0, 1, f_visualmode }, - { "wildmenumode", 0, 0, f_wildmenumode }, - { "winbufnr", 1, 1, f_winbufnr }, - { "wincol", 0, 0, f_wincol }, - { "winheight", 1, 1, f_winheight }, - { "winline", 0, 0, f_winline }, - { "winnr", 0, 1, f_winnr }, - { "winrestcmd", 0, 0, f_winrestcmd }, - { "winrestview", 1, 1, f_winrestview }, - { "winsaveview", 0, 0, f_winsaveview }, - { "winwidth", 1, 1, f_winwidth }, - { "wordcount", 0, 0, f_wordcount }, - { "writefile", 2, 3, f_writefile }, - { "xor", 2, 2, f_xor }, -}; - +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "funcs.generated.h" +#endif /* * Function given to ExpandGeneric() to obtain the list of internal @@ -6973,15 +6942,25 @@ char_u *get_function_name(expand_T *xp, int idx) if (name != NULL) return name; } - if (++intidx < (int)ARRAY_SIZE(functions)) { - STRCPY(IObuff, functions[intidx].f_name); - STRCAT(IObuff, "("); - if (functions[intidx].f_max_argc == 0) - STRCAT(IObuff, ")"); - return IObuff; + while ( (size_t)++intidx < ARRAY_SIZE(functions) + && functions[intidx].name[0] == '\0') { } - return NULL; + if ((size_t)intidx >= ARRAY_SIZE(functions)) { + return NULL; + } + + const char *const key = functions[intidx].name; + const size_t key_len = strlen(key); + memcpy(IObuff, key, key_len); + IObuff[key_len] = '('; + if (functions[intidx].max_argc == 0) { + IObuff[key_len + 1] = ')'; + IObuff[key_len + 2] = NUL; + } else { + IObuff[key_len + 1] = NUL; + } + return IObuff; } /* @@ -7006,42 +6985,32 @@ char_u *get_expr_name(expand_T *xp, int idx) -/* - * Find internal function in table above. - * Return index, or -1 if not found - */ -static int -find_internal_func ( - char_u *name /* name of the function */ -) +/// Find internal function in hash functions +/// +/// @param[in] name Name of the function. +/// +/// Returns pointer to the function definition or NULL if not found. +static VimLFuncDef *find_internal_func(const char *const name) + FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE FUNC_ATTR_NONNULL_ALL { - int first = 0; - int last = (int)ARRAY_SIZE(functions) - 1; - - /* - * Find the function name in the table. Binary search. - */ - while (first <= last) { - int x = first + ((unsigned)(last - first)) / 2; - int cmp = STRCMP(name, functions[x].f_name); - if (cmp < 0) - last = x - 1; - else if (cmp > 0) - first = x + 1; - else - return x; - } - return -1; + size_t len = strlen(name); + return find_internal_func_gperf(name, len); } -/* - * Check if "name" is a variable of type VAR_FUNC. If so, return the function - * name it contains, otherwise return "name". - */ -static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload) +/// Check if "name" is a variable of type VAR_FUNC. If so, return the function +/// name it contains, otherwise return "name". +/// If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set +/// "partialp". +static char_u *deref_func_name( + char_u *name, int *lenp, + partial_T **partialp, bool no_autoload +) { dictitem_T *v; int cc; + if (partialp != NULL) { + *partialp = NULL; + } cc = name[*lenp]; name[*lenp] = NUL; @@ -7056,6 +7025,20 @@ static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload) return v->di_tv.vval.v_string; } + if (v != NULL && v->di_tv.v_type == VAR_PARTIAL) { + partial_T *pt = v->di_tv.vval.v_partial; + + if (pt == NULL) { + *lenp = 0; + return (char_u *)""; // just in case + } + if (partialp != NULL) { + *partialp = pt; + } + *lenp = (int)STRLEN(pt->pt_name); + return pt->pt_name; + } + return name; } @@ -7073,7 +7056,8 @@ get_func_tv ( linenr_T lastline, /* last line of range */ int *doesrange, /* return: function handled range */ int evaluate, - dict_T *selfdict /* Dictionary for "self" */ + partial_T *partial, // for extra arguments + dict_T *selfdict // Dictionary for "self" ) { char_u *argp; @@ -7085,10 +7069,11 @@ get_func_tv ( * Get the arguments. */ argp = *arg; - while (argcount < MAX_FUNC_ARGS) { - argp = skipwhite(argp + 1); /* skip the '(' or ',' */ - if (*argp == ')' || *argp == ',' || *argp == NUL) + while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc)) { + argp = skipwhite(argp + 1); // skip the '(' or ',' + if (*argp == ')' || *argp == ',' || *argp == NUL) { break; + } if (eval1(&argp, &argvars[argcount], evaluate) == FAIL) { ret = FAIL; break; @@ -7102,14 +7087,16 @@ get_func_tv ( else ret = FAIL; - if (ret == OK) + if (ret == OK) { ret = call_func(name, len, rettv, argcount, argvars, - firstline, lastline, doesrange, evaluate, selfdict); - else if (!aborting()) { - if (argcount == MAX_FUNC_ARGS) + firstline, lastline, doesrange, evaluate, + partial, selfdict); + } else if (!aborting()) { + if (argcount == MAX_FUNC_ARGS) { emsg_funcname(N_("E740: Too many arguments for function %s"), name); - else + } else { emsg_funcname(N_("E116: Invalid arguments for function %s"), name); + } } while (--argcount >= 0) @@ -7119,28 +7106,6 @@ get_func_tv ( return ret; } - -/* - * Call a function with its resolved parameters - * Return FAIL when the function can't be called, OK otherwise. - * Also returns OK when an error was encountered while executing the function. - */ -int -call_func ( - char_u *funcname, /* name of the function */ - int len, /* length of "name" */ - typval_T *rettv, /* return value goes here */ - int argcount, /* number of "argvars" */ - typval_T *argvars, /* vars for arguments, must have "argcount" - PLUS ONE elements! */ - linenr_T firstline, /* first line of range */ - linenr_T lastline, /* last line of range */ - int *doesrange, /* return: function handled range */ - int evaluate, - dict_T *selfdict /* Dictionary for "self" */ -) -{ - int ret = FAIL; #define ERROR_UNKNOWN 0 #define ERROR_TOOMANY 1 #define ERROR_TOOFEW 2 @@ -7148,35 +7113,31 @@ call_func ( #define ERROR_DICT 4 #define ERROR_NONE 5 #define ERROR_OTHER 6 - int error = ERROR_NONE; - int i; - int llen; - ufunc_T *fp; +#define ERROR_BOTH 7 #define FLEN_FIXED 40 - char_u fname_buf[FLEN_FIXED + 1]; - char_u *fname; - char_u *name; - /* Make a copy of the name, if it comes from a funcref variable it could - * be changed or deleted in the called function. */ - name = vim_strnsave(funcname, len); +/// In a script change <SID>name() and s:name() to K_SNR 123_name(). +/// Change <SNR>123_name() to K_SNR 123_name(). +/// Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory +/// (slow). +static char_u * +fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error) { + int llen; + char_u *fname; + int i; - /* - * In a script change <SID>name() and s:name() to K_SNR 123_name(). - * Change <SNR>123_name() to K_SNR 123_name(). - * Use fname_buf[] when it fits, otherwise allocate memory (slow). - */ llen = eval_fname_script(name); if (llen > 0) { fname_buf[0] = K_SPECIAL; fname_buf[1] = KS_EXTRA; fname_buf[2] = (int)KE_SNR; i = 3; - if (eval_fname_sid(name)) { /* "<SID>" or "s:" */ - if (current_SID <= 0) - error = ERROR_SCRIPT; - else { - sprintf((char *)fname_buf + 3, "%" PRId64 "_", (int64_t)current_SID); + if (eval_fname_sid(name)) { // "<SID>" or "s:" + if (current_SID <= 0) { + *error = ERROR_SCRIPT; + } else { + snprintf((char *)fname_buf + 3, FLEN_FIXED + 1, "%" PRId64 "_", + (int64_t)current_SID); i = (int)STRLEN(fname_buf); } } @@ -7185,13 +7146,83 @@ call_func ( fname = fname_buf; } else { fname = xmalloc(i + STRLEN(name + llen) + 1); - memmove(fname, fname_buf, (size_t)i); - STRCPY(fname + i, name + llen); + if (fname == NULL) { + *error = ERROR_OTHER; + } else { + *tofree = fname; + memmove(fname, fname_buf, (size_t)i); + STRCPY(fname + i, name + llen); + } } - } else + } else { fname = name; + } - *doesrange = FALSE; + return fname; +} + +/// Call a function with its resolved parameters +/// Return FAIL when the function can't be called, OK otherwise. +/// Also returns OK when an error was encountered while executing the function. +int +call_func( + char_u *funcname, // name of the function + int len, // length of "name" + typval_T *rettv, // return value goes here + int argcount_in, // number of "argvars" + typval_T *argvars_in, // vars for arguments, must have "argcount" + // PLUS ONE elements! + linenr_T firstline, // first line of range + linenr_T lastline, // last line of range + int *doesrange, // return: function handled range + bool evaluate, + partial_T *partial, // optional, can be NULL + dict_T *selfdict_in // Dictionary for "self" +) +{ + int ret = FAIL; + int error = ERROR_NONE; + ufunc_T *fp; + char_u fname_buf[FLEN_FIXED + 1]; + char_u *tofree = NULL; + char_u *fname; + char_u *name; + int argcount = argcount_in; + typval_T *argvars = argvars_in; + dict_T *selfdict = selfdict_in; + typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" is not NULL + int argv_clear = 0; + + // Make a copy of the name, if it comes from a funcref variable it could + // be changed or deleted in the called function. + name = vim_strnsave(funcname, len); + if (name == NULL) { + return ret; + } + + fname = fname_trans_sid(name, fname_buf, &tofree, &error); + + *doesrange = false; + + if (partial != NULL) { + // When the function has a partial with a dict and there is a dict + // argument, use the dict argument. That is backwards compatible. + // When the dict was bound explicitly use the one from the partial. + if (partial->pt_dict != NULL + && (selfdict_in == NULL || !partial->pt_auto)) { + selfdict = partial->pt_dict; + } + if (error == ERROR_NONE && partial->pt_argc > 0) { + for (argv_clear = 0; argv_clear < partial->pt_argc; argv_clear++) { + copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]); + } + for (int i = 0; i < argcount_in; i++) { + argv[i + argv_clear] = argvars_in[i]; + } + argvars = argv; + argcount = partial->pt_argc + argcount_in; + } + } /* execute the function if no errors detected and executing */ @@ -7243,18 +7274,16 @@ call_func ( } } } else { - /* - * Find the function name in the table, call its implementation. - */ - i = find_internal_func(fname); - if (i >= 0) { - if (argcount < functions[i].f_min_argc) + // Find the function name in the table, call its implementation. + VimLFuncDef *const fdef = find_internal_func((char *)fname); + if (fdef != NULL) { + if (argcount < fdef->min_argc) { error = ERROR_TOOFEW; - else if (argcount > functions[i].f_max_argc) + } else if (argcount > fdef->max_argc) { error = ERROR_TOOMANY; - else { + } else { argvars[argcount].v_type = VAR_UNKNOWN; - functions[i].f_func(argvars, rettv); + fdef->func(argvars, rettv, fdef->data); error = ERROR_NONE; } } @@ -7301,8 +7330,10 @@ call_func ( } } - if (fname != name && fname != fname_buf) - xfree(fname); + while (argv_clear > 0) { + clear_tv(&argv[--argv_clear]); + } + xfree(tofree); xfree(name); return ret; @@ -7365,13 +7396,10 @@ static inline int get_float_arg(typval_T *argvars, float_T *f) // Some versions of glibc on i386 have an optimization that makes it harder to // call math functions indirectly from inside an inlined function, causing // compile-time errors. Avoid `inline` in that case. #3072 -#ifndef ARCH_32 -inline -#endif -static void float_op_wrapper(typval_T *argvars, typval_T *rettv, - float_T (*function)(float_T)) +static void float_op_wrapper(typval_T *argvars, typval_T *rettv, FunPtr fptr) { float_T f; + float_T (*function)(float_T) = (float_T (*)(float_T))fptr; rettv->v_type = VAR_FLOAT; if (get_float_arg(argvars, &f) == OK) { @@ -7381,13 +7409,40 @@ static void float_op_wrapper(typval_T *argvars, typval_T *rettv, } } +static void api_wrapper(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + ApiDispatchWrapper fn = (ApiDispatchWrapper)fptr; + + Array args = ARRAY_DICT_INIT; + + for (typval_T *tv = argvars; tv->v_type != VAR_UNKNOWN; tv++) { + ADD(args, vim_to_object(tv)); + } + + Error err = ERROR_INIT; + Object result = fn(INTERNAL_CALL, args, &err); + + if (err.set) { + nvim_err_writeln(cstr_as_string(err.msg)); + goto end; + } + + if (!object_to_vim(result, rettv, &err)) { + EMSG2(_("Error converting the call result: %s"), err.msg); + } + +end: + api_free_array(args); + api_free_object(result); +} + /* * "abs(expr)" function */ -static void f_abs(typval_T *argvars, typval_T *rettv) +static void f_abs(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (argvars[0].v_type == VAR_FLOAT) { - float_op_wrapper(argvars, rettv, &fabs); + float_op_wrapper(argvars, rettv, (FunPtr)&fabs); } else { varnumber_T n; int error = FALSE; @@ -7403,17 +7458,9 @@ static void f_abs(typval_T *argvars, typval_T *rettv) } /* - * "acos()" function - */ -static void f_acos(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &acos); -} - -/* * "add(list, item)" function */ -static void f_add(typval_T *argvars, typval_T *rettv) +static void f_add(typval_T *argvars, typval_T *rettv, FunPtr fptr) { list_T *l; @@ -7432,16 +7479,25 @@ static void f_add(typval_T *argvars, typval_T *rettv) /* * "and(expr, expr)" function */ -static void f_and(typval_T *argvars, typval_T *rettv) +static void f_and(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL) & get_tv_number_chk(&argvars[1], NULL); } + +/// "api_info()" function +static void f_api_info(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + Dictionary metadata = api_metadata(); + (void)object_to_vim(DICTIONARY_OBJ(metadata), rettv, NULL); + api_free_dictionary(metadata); +} + /* * "append(lnum, string/list)" function */ -static void f_append(typval_T *argvars, typval_T *rettv) +static void f_append(typval_T *argvars, typval_T *rettv, FunPtr fptr) { long lnum; char_u *line; @@ -7496,7 +7552,7 @@ static void f_append(typval_T *argvars, typval_T *rettv) /* * "argc()" function */ -static void f_argc(typval_T *argvars, typval_T *rettv) +static void f_argc(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = ARGCOUNT; } @@ -7504,41 +7560,25 @@ static void f_argc(typval_T *argvars, typval_T *rettv) /* * "argidx()" function */ -static void f_argidx(typval_T *argvars, typval_T *rettv) +static void f_argidx(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = curwin->w_arg_idx; } /// "arglistid" function -static void f_arglistid(typval_T *argvars, typval_T *rettv) +static void f_arglistid(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = -1; - if (argvars[0].v_type != VAR_UNKNOWN) { - tabpage_T *tp = NULL; - if (argvars[1].v_type != VAR_UNKNOWN) { - long n = get_tv_number(&argvars[1]); - if (n >= 0) { - tp = find_tabpage(n); - } - } else { - tp = curtab; - } - - if (tp != NULL) { - win_T *wp = find_win_by_nr(&argvars[0], tp); - if (wp != NULL) { - rettv->vval.v_number = wp->w_alist->id; - } - } - } else { - rettv->vval.v_number = curwin->w_alist->id; + win_T *wp = find_tabwin(&argvars[0], &argvars[1]); + if (wp != NULL) { + rettv->vval.v_number = wp->w_alist->id; } } /* * "argv(nr)" function */ -static void f_argv(typval_T *argvars, typval_T *rettv) +static void f_argv(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int idx; @@ -7581,7 +7621,7 @@ static void prepare_assert_error(garray_T *gap) // Fill "gap" with information about an assert error. static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, - typval_T *got_tv) + typval_T *got_tv, assert_type_T atype) { char_u *tofree; @@ -7590,7 +7630,11 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, ga_concat(gap, tofree); xfree(tofree); } else { - ga_concat(gap, (char_u *)"Expected "); + if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH) { + ga_concat(gap, (char_u *)"Pattern "); + } else { + ga_concat(gap, (char_u *)"Expected "); + } if (exp_str == NULL) { tofree = (char_u *) encode_tv2string(exp_tv, NULL); ga_concat(gap, tofree); @@ -7598,8 +7642,16 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, } else { ga_concat(gap, exp_str); } - tofree = (char_u *) encode_tv2string(got_tv, NULL); - ga_concat(gap, (char_u *)" but got "); + tofree = (char_u *)encode_tv2string(got_tv, NULL); + if (atype == ASSERT_MATCH) { + ga_concat(gap, (char_u *)" does not match "); + } else if (atype == ASSERT_NOTMATCH) { + ga_concat(gap, (char_u *)" does match "); + } else if (atype == ASSERT_NOTEQUAL) { + ga_concat(gap, (char_u *)" differs from "); + } else { + ga_concat(gap, (char_u *)" but got "); + } ga_concat(gap, tofree); xfree(tofree); } @@ -7618,22 +7670,34 @@ static void assert_error(garray_T *gap) gap->ga_data, gap->ga_len); } -// "assert_equal(expected, actual[, msg])" function -static void f_assert_equal(typval_T *argvars, typval_T *rettv) +static void assert_equal_common(typval_T *argvars, assert_type_T atype) { garray_T ga; - if (!tv_equal(&argvars[0], &argvars[1], false, false)) { + if (tv_equal(&argvars[0], &argvars[1], false, false) + != (atype == ASSERT_EQUAL)) { prepare_assert_error(&ga); fill_assert_error(&ga, &argvars[2], NULL, - &argvars[0], &argvars[1]); + &argvars[0], &argvars[1], atype); assert_error(&ga); ga_clear(&ga); } } +// "assert_equal(expected, actual[, msg])" function +static void f_assert_equal(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + assert_equal_common(argvars, ASSERT_EQUAL); +} + +// "assert_notequal(expected, actual[, msg])" function +static void f_assert_notequal(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + assert_equal_common(argvars, ASSERT_NOTEQUAL); +} + /// "assert_exception(string[, msg])" function -static void f_assert_exception(typval_T *argvars, typval_T *rettv) +static void f_assert_exception(typval_T *argvars, typval_T *rettv, FunPtr fptr) { garray_T ga; @@ -7643,15 +7707,53 @@ static void f_assert_exception(typval_T *argvars, typval_T *rettv) ga_concat(&ga, (char_u *)"v:exception is not set"); assert_error(&ga); ga_clear(&ga); - } else if (strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL) { + } else if (error != NULL + && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL) { prepare_assert_error(&ga); fill_assert_error(&ga, &argvars[1], NULL, &argvars[0], - &vimvars[VV_EXCEPTION].vv_tv); + &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER); assert_error(&ga); ga_clear(&ga); } } +/// "assert_fails(cmd [, error])" function +static void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + char_u *cmd = get_tv_string_chk(&argvars[0]); + garray_T ga; + + called_emsg = false; + suppress_errthrow = true; + emsg_silent = true; + do_cmdline_cmd((char *)cmd); + if (!called_emsg) { + prepare_assert_error(&ga); + ga_concat(&ga, (char_u *)"command did not fail: "); + ga_concat(&ga, cmd); + assert_error(&ga); + ga_clear(&ga); + } else if (argvars[1].v_type != VAR_UNKNOWN) { + char_u buf[NUMBUFLEN]; + char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf); + + if (error == NULL + || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL) { + prepare_assert_error(&ga); + fill_assert_error(&ga, &argvars[2], NULL, &argvars[1], + &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER); + assert_error(&ga); + ga_clear(&ga); + } + } + + called_emsg = false; + suppress_errthrow = false; + emsg_silent = false; + emsg_on_display = false; + set_vim_var_string(VV_ERRMSG, NULL, 0); +} + // Common for assert_true() and assert_false(). static void assert_bool(typval_T *argvars, bool is_true) { @@ -7669,44 +7771,58 @@ static void assert_bool(typval_T *argvars, bool is_true) prepare_assert_error(&ga); fill_assert_error(&ga, &argvars[1], (char_u *)(is_true ? "True" : "False"), - NULL, &argvars[0]); + NULL, &argvars[0], ASSERT_OTHER); assert_error(&ga); ga_clear(&ga); } } // "assert_false(actual[, msg])" function -static void f_assert_false(typval_T *argvars, typval_T *rettv) +static void f_assert_false(typval_T *argvars, typval_T *rettv, FunPtr fptr) { assert_bool(argvars, false); } -// "assert_true(actual[, msg])" function -static void f_assert_true(typval_T *argvars, typval_T *rettv) +static void assert_match_common(typval_T *argvars, assert_type_T atype) { - assert_bool(argvars, true); + char_u buf1[NUMBUFLEN]; + char_u buf2[NUMBUFLEN]; + char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1); + char_u *text = get_tv_string_buf_chk(&argvars[1], buf2); + + if (pat == NULL || text == NULL) { + EMSG(_(e_invarg)); + } else if (pattern_match(pat, text, false) != (atype == ASSERT_MATCH)) { + garray_T ga; + prepare_assert_error(&ga); + fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], atype); + assert_error(&ga); + ga_clear(&ga); + } } -/* - * "asin()" function - */ -static void f_asin(typval_T *argvars, typval_T *rettv) +/// "assert_match(pattern, actual[, msg])" function +static void f_assert_match(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - float_op_wrapper(argvars, rettv, &asin); + assert_match_common(argvars, ASSERT_MATCH); } -/* - * "atan()" function - */ -static void f_atan(typval_T *argvars, typval_T *rettv) +/// "assert_notmatch(pattern, actual[, msg])" function +static void f_assert_notmatch(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + assert_match_common(argvars, ASSERT_NOTMATCH); +} + +// "assert_true(actual[, msg])" function +static void f_assert_true(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - float_op_wrapper(argvars, rettv, &atan); + assert_bool(argvars, true); } /* * "atan2()" function */ -static void f_atan2(typval_T *argvars, typval_T *rettv) +static void f_atan2(typval_T *argvars, typval_T *rettv, FunPtr fptr) { float_T fx, fy; @@ -7721,7 +7837,7 @@ static void f_atan2(typval_T *argvars, typval_T *rettv) /* * "browse(save, title, initdir, default)" function */ -static void f_browse(typval_T *argvars, typval_T *rettv) +static void f_browse(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_string = NULL; rettv->v_type = VAR_STRING; @@ -7730,9 +7846,9 @@ static void f_browse(typval_T *argvars, typval_T *rettv) /* * "browsedir(title, initdir)" function */ -static void f_browsedir(typval_T *argvars, typval_T *rettv) +static void f_browsedir(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - f_browse(argvars, rettv); + f_browse(argvars, rettv, NULL); } @@ -7768,7 +7884,7 @@ static buf_T *find_buffer(typval_T *avar) /* * "bufexists(expr)" function */ -static void f_bufexists(typval_T *argvars, typval_T *rettv) +static void f_bufexists(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL); } @@ -7776,7 +7892,7 @@ static void f_bufexists(typval_T *argvars, typval_T *rettv) /* * "buflisted(expr)" function */ -static void f_buflisted(typval_T *argvars, typval_T *rettv) +static void f_buflisted(typval_T *argvars, typval_T *rettv, FunPtr fptr) { buf_T *buf; @@ -7787,7 +7903,7 @@ static void f_buflisted(typval_T *argvars, typval_T *rettv) /* * "bufloaded(expr)" function */ -static void f_bufloaded(typval_T *argvars, typval_T *rettv) +static void f_bufloaded(typval_T *argvars, typval_T *rettv, FunPtr fptr) { buf_T *buf; @@ -7837,7 +7953,7 @@ static buf_T *get_buf_tv(typval_T *tv, int curtab_only) /* * "bufname(expr)" function */ -static void f_bufname(typval_T *argvars, typval_T *rettv) +static void f_bufname(typval_T *argvars, typval_T *rettv, FunPtr fptr) { buf_T *buf; @@ -7855,7 +7971,7 @@ static void f_bufname(typval_T *argvars, typval_T *rettv) /* * "bufnr(expr)" function */ -static void f_bufnr(typval_T *argvars, typval_T *rettv) +static void f_bufnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { buf_T *buf; int error = FALSE; @@ -7882,32 +7998,53 @@ static void f_bufnr(typval_T *argvars, typval_T *rettv) rettv->vval.v_number = -1; } -/* - * "bufwinnr(nr)" function - */ -static void f_bufwinnr(typval_T *argvars, typval_T *rettv) +static void buf_win_common(typval_T *argvars, typval_T *rettv, bool get_nr) { - (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ - ++emsg_off; + int error = false; + (void)get_tv_number_chk(&argvars[0], &error); // issue errmsg if type error + if (error) { // the argument has an invalid type + rettv->vval.v_number = -1; + return; + } + + emsg_off++; + buf_T *buf = get_buf_tv(&argvars[0], true); + if (buf == NULL) { // no need to search if buffer was not found + rettv->vval.v_number = -1; + goto end; + } - buf_T *buf = get_buf_tv(&argvars[0], TRUE); int winnr = 0; + int winid; bool found_buf = false; FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { - ++winnr; + winnr++; if (wp->w_buffer == buf) { found_buf = true; + winid = wp->handle; break; } } - rettv->vval.v_number = (found_buf ? winnr : -1); - --emsg_off; + rettv->vval.v_number = (found_buf ? (get_nr ? winnr : winid) : -1); +end: + emsg_off--; +} + +/// "bufwinid(nr)" function +static void f_bufwinid(typval_T *argvars, typval_T *rettv, FunPtr fptr) { + buf_win_common(argvars, rettv, false); +} + +/// "bufwinnr(nr)" function +static void f_bufwinnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + buf_win_common(argvars, rettv, true); } /* * "byte2line(byte)" function */ -static void f_byte2line(typval_T *argvars, typval_T *rettv) +static void f_byte2line(typval_T *argvars, typval_T *rettv, FunPtr fptr) { long boff = 0; @@ -7946,7 +8083,7 @@ static void byteidx(typval_T *argvars, typval_T *rettv, int comp) /* * "byteidx()" function */ -static void f_byteidx(typval_T *argvars, typval_T *rettv) +static void f_byteidx(typval_T *argvars, typval_T *rettv, FunPtr fptr) { byteidx(argvars, rettv, FALSE); } @@ -7954,12 +8091,13 @@ static void f_byteidx(typval_T *argvars, typval_T *rettv) /* * "byteidxcomp()" function */ -static void f_byteidxcomp(typval_T *argvars, typval_T *rettv) +static void f_byteidxcomp(typval_T *argvars, typval_T *rettv, FunPtr fptr) { byteidx(argvars, rettv, TRUE); } -int func_call(char_u *name, typval_T *args, dict_T *selfdict, typval_T *rettv) +int func_call(char_u *name, typval_T *args, partial_T *partial, + dict_T *selfdict, typval_T *rettv) { listitem_T *item; typval_T argv[MAX_FUNC_ARGS + 1]; @@ -7969,7 +8107,7 @@ int func_call(char_u *name, typval_T *args, dict_T *selfdict, typval_T *rettv) for (item = args->vval.v_list->lv_first; item != NULL; item = item->li_next) { - if (argc == MAX_FUNC_ARGS) { + if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc)) { EMSG(_("E699: Too many arguments")); break; } @@ -7979,10 +8117,11 @@ int func_call(char_u *name, typval_T *args, dict_T *selfdict, typval_T *rettv) copy_tv(&item->li_tv, &argv[argc++]); } - if (item == NULL) + if (item == NULL) { r = call_func(name, (int)STRLEN(name), rettv, argc, argv, - curwin->w_cursor.lnum, curwin->w_cursor.lnum, - &dummy, TRUE, selfdict); + curwin->w_cursor.lnum, curwin->w_cursor.lnum, + &dummy, true, partial, selfdict); + } /* Free the arguments. */ while (argc > 0) @@ -7991,12 +8130,11 @@ int func_call(char_u *name, typval_T *args, dict_T *selfdict, typval_T *rettv) return r; } -/* - * "call(func, arglist)" function - */ -static void f_call(typval_T *argvars, typval_T *rettv) +/// "call(func, arglist [, dict])" function +static void f_call(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *func; + partial_T *partial = NULL; dict_T *selfdict = NULL; if (argvars[1].v_type != VAR_LIST) { @@ -8006,12 +8144,17 @@ static void f_call(typval_T *argvars, typval_T *rettv) if (argvars[1].vval.v_list == NULL) return; - if (argvars[0].v_type == VAR_FUNC) + if (argvars[0].v_type == VAR_FUNC) { func = argvars[0].vval.v_string; - else + } else if (argvars[0].v_type == VAR_PARTIAL) { + partial = argvars[0].vval.v_partial; + func = partial->pt_name; + } else { func = get_tv_string(&argvars[0]); - if (*func == NUL) - return; /* type error or empty name */ + } + if (*func == NUL) { + return; // type error or empty name + } if (argvars[2].v_type != VAR_UNKNOWN) { if (argvars[2].v_type != VAR_DICT) { @@ -8021,21 +8164,13 @@ static void f_call(typval_T *argvars, typval_T *rettv) selfdict = argvars[2].vval.v_dict; } - (void)func_call(func, &argvars[1], selfdict, rettv); -} - -/* - * "ceil({float})" function - */ -static void f_ceil(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &ceil); + func_call(func, &argvars[1], partial, selfdict, rettv); } /* * "changenr()" function */ -static void f_changenr(typval_T *argvars, typval_T *rettv) +static void f_changenr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = curbuf->b_u_seq_cur; } @@ -8043,7 +8178,7 @@ static void f_changenr(typval_T *argvars, typval_T *rettv) /* * "char2nr(string)" function */ -static void f_char2nr(typval_T *argvars, typval_T *rettv) +static void f_char2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (has_mbyte) { int utf8 = 0; @@ -8062,7 +8197,7 @@ static void f_char2nr(typval_T *argvars, typval_T *rettv) /* * "cindent(lnum)" function */ -static void f_cindent(typval_T *argvars, typval_T *rettv) +static void f_cindent(typval_T *argvars, typval_T *rettv, FunPtr fptr) { pos_T pos; linenr_T lnum; @@ -8080,7 +8215,7 @@ static void f_cindent(typval_T *argvars, typval_T *rettv) /* * "clearmatches()" function */ -static void f_clearmatches(typval_T *argvars, typval_T *rettv) +static void f_clearmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr) { clear_matches(curwin); } @@ -8088,7 +8223,7 @@ static void f_clearmatches(typval_T *argvars, typval_T *rettv) /* * "col(string)" function */ -static void f_col(typval_T *argvars, typval_T *rettv) +static void f_col(typval_T *argvars, typval_T *rettv, FunPtr fptr) { colnr_T col = 0; pos_T *fp; @@ -8125,7 +8260,7 @@ static void f_col(typval_T *argvars, typval_T *rettv) /* * "complete()" function */ -static void f_complete(typval_T *argvars, typval_T *rettv) +static void f_complete(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int startcol; @@ -8154,7 +8289,7 @@ static void f_complete(typval_T *argvars, typval_T *rettv) /* * "complete_add()" function */ -static void f_complete_add(typval_T *argvars, typval_T *rettv) +static void f_complete_add(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0); } @@ -8162,12 +8297,12 @@ static void f_complete_add(typval_T *argvars, typval_T *rettv) /* * "complete_check()" function */ -static void f_complete_check(typval_T *argvars, typval_T *rettv) +static void f_complete_check(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int saved = RedrawingDisabled; RedrawingDisabled = 0; - ins_compl_check_keys(0); + ins_compl_check_keys(0, true); rettv->vval.v_number = compl_interrupted; RedrawingDisabled = saved; } @@ -8175,7 +8310,7 @@ static void f_complete_check(typval_T *argvars, typval_T *rettv) /* * "confirm(message, buttons[, default [, type]])" function */ -static void f_confirm(typval_T *argvars, typval_T *rettv) +static void f_confirm(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *message; char_u *buttons = NULL; @@ -8223,31 +8358,15 @@ static void f_confirm(typval_T *argvars, typval_T *rettv) /* * "copy()" function */ -static void f_copy(typval_T *argvars, typval_T *rettv) +static void f_copy(typval_T *argvars, typval_T *rettv, FunPtr fptr) { var_item_copy(NULL, &argvars[0], rettv, false, 0); } /* - * "cos()" function - */ -static void f_cos(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &cos); -} - -/* - * "cosh()" function - */ -static void f_cosh(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &cosh); -} - -/* * "count()" function */ -static void f_count(typval_T *argvars, typval_T *rettv) +static void f_count(typval_T *argvars, typval_T *rettv, FunPtr fptr) { long n = 0; int ic = FALSE; @@ -8312,7 +8431,7 @@ static void f_count(typval_T *argvars, typval_T *rettv) * * Checks the existence of a cscope connection. */ -static void f_cscope_connection(typval_T *argvars, typval_T *rettv) +static void f_cscope_connection(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int num = 0; char_u *dbpath = NULL; @@ -8336,7 +8455,7 @@ static void f_cscope_connection(typval_T *argvars, typval_T *rettv) /// Moves the cursor to the specified line and column. /// /// @returns 0 when the position could be set, -1 otherwise. -static void f_cursor(typval_T *argvars, typval_T *rettv) +static void f_cursor(typval_T *argvars, typval_T *rettv, FunPtr fptr) { long line, col; long coladd = 0; @@ -8392,7 +8511,7 @@ static void f_cursor(typval_T *argvars, typval_T *rettv) /* * "deepcopy()" function */ -static void f_deepcopy(typval_T *argvars, typval_T *rettv) +static void f_deepcopy(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int noref = 0; @@ -8408,7 +8527,7 @@ static void f_deepcopy(typval_T *argvars, typval_T *rettv) } // "delete()" function -static void f_delete(typval_T *argvars, typval_T *rettv) +static void f_delete(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u nbuf[NUMBUFLEN]; char_u *name; @@ -8446,7 +8565,7 @@ static void f_delete(typval_T *argvars, typval_T *rettv) } // dictwatcheradd(dict, key, funcref) function -static void f_dictwatcheradd(typval_T *argvars, typval_T *rettv) +static void f_dictwatcheradd(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (check_restricted() || check_secure()) { return; @@ -8476,22 +8595,20 @@ static void f_dictwatcheradd(typval_T *argvars, typval_T *rettv) return; } - ufunc_T *func = find_ufunc(argvars[2].vval.v_string); - if (!func) { - // Invalid function name. Error already reported by `find_ufunc`. + Callback callback; + if (!callback_from_typval(&callback, &argvars[2])) { return; } - func->uf_refcount++; DictWatcher *watcher = xmalloc(sizeof(DictWatcher)); watcher->key_pattern = xmemdupz(key_pattern, key_len); - watcher->callback = func; + watcher->callback = callback; watcher->busy = false; QUEUE_INSERT_TAIL(&argvars[0].vval.v_dict->watchers, &watcher->node); } // dictwatcherdel(dict, key, funcref) function -static void f_dictwatcherdel(typval_T *argvars, typval_T *rettv) +static void f_dictwatcherdel(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (check_restricted() || check_secure()) { return; @@ -8521,9 +8638,8 @@ static void f_dictwatcherdel(typval_T *argvars, typval_T *rettv) return; } - ufunc_T *func = find_ufunc(argvars[2].vval.v_string); - if (!func) { - // Invalid function name. Error already reported by `find_ufunc`. + Callback callback; + if (!callback_from_typval(&callback, &argvars[2])) { return; } @@ -8533,13 +8649,15 @@ static void f_dictwatcherdel(typval_T *argvars, typval_T *rettv) bool matched = false; QUEUE_FOREACH(w, &dict->watchers) { watcher = dictwatcher_node_data(w); - if (func == watcher->callback + if (callback_equal(&watcher->callback, &callback) && !strcmp(watcher->key_pattern, key_pattern)) { matched = true; break; } } + callback_free(&callback); + if (!matched) { EMSG("Couldn't find a watcher matching key and callback"); return; @@ -8552,7 +8670,7 @@ static void f_dictwatcherdel(typval_T *argvars, typval_T *rettv) /* * "did_filetype()" function */ -static void f_did_filetype(typval_T *argvars, typval_T *rettv) +static void f_did_filetype(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = did_filetype; } @@ -8560,7 +8678,7 @@ static void f_did_filetype(typval_T *argvars, typval_T *rettv) /* * "diff_filler()" function */ -static void f_diff_filler(typval_T *argvars, typval_T *rettv) +static void f_diff_filler(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars)); } @@ -8568,7 +8686,7 @@ static void f_diff_filler(typval_T *argvars, typval_T *rettv) /* * "diff_hlID()" function */ -static void f_diff_hlID(typval_T *argvars, typval_T *rettv) +static void f_diff_hlID(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum = get_tv_lnum(argvars); static linenr_T prev_lnum = 0; @@ -8617,7 +8735,7 @@ static void f_diff_hlID(typval_T *argvars, typval_T *rettv) /* * "empty({expr})" function */ -static void f_empty(typval_T *argvars, typval_T *rettv) +static void f_empty(typval_T *argvars, typval_T *rettv, FunPtr fptr) { bool n = true; @@ -8627,6 +8745,9 @@ static void f_empty(typval_T *argvars, typval_T *rettv) n = argvars[0].vval.v_string == NULL || *argvars[0].vval.v_string == NUL; break; + case VAR_PARTIAL: + n = false; + break; case VAR_NUMBER: n = argvars[0].vval.v_number == 0; break; @@ -8655,7 +8776,7 @@ static void f_empty(typval_T *argvars, typval_T *rettv) /* * "escape({string}, {chars})" function */ -static void f_escape(typval_T *argvars, typval_T *rettv) +static void f_escape(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u buf[NUMBUFLEN]; @@ -8667,7 +8788,7 @@ static void f_escape(typval_T *argvars, typval_T *rettv) /* * "eval()" function */ -static void f_eval(typval_T *argvars, typval_T *rettv) +static void f_eval(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *s; @@ -8691,7 +8812,7 @@ static void f_eval(typval_T *argvars, typval_T *rettv) /* * "eventhandler()" function */ -static void f_eventhandler(typval_T *argvars, typval_T *rettv) +static void f_eventhandler(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = vgetc_busy; } @@ -8699,7 +8820,7 @@ static void f_eventhandler(typval_T *argvars, typval_T *rettv) /* * "executable()" function */ -static void f_executable(typval_T *argvars, typval_T *rettv) +static void f_executable(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *name = get_tv_string(&argvars[0]); @@ -8708,8 +8829,79 @@ static void f_executable(typval_T *argvars, typval_T *rettv) || (gettail_dir(name) != name && os_can_exe(name, NULL, false)); } +static char_u * get_list_line(int c, void *cookie, int indent) +{ + listitem_T **p = (listitem_T **)cookie; + listitem_T *item = *p; + char_u buf[NUMBUFLEN]; + char_u *s; + + if (item == NULL) { + return NULL; + } + s = get_tv_string_buf_chk(&item->li_tv, buf); + *p = item->li_next; + return s == NULL ? NULL : vim_strsave(s); +} + +// "execute(command)" function +static void f_execute(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + int save_msg_silent = msg_silent; + int save_emsg_silent = emsg_silent; + bool save_emsg_noredir = emsg_noredir; + garray_T *save_capture_ga = capture_ga; + + if (check_secure()) { + return; + } + + if (argvars[1].v_type != VAR_UNKNOWN) { + char_u buf[NUMBUFLEN]; + char_u *s = get_tv_string_buf_chk(&argvars[1], buf); + + if (s == NULL) { + return; + } + if (STRNCMP(s, "silent", 6) == 0) { + msg_silent++; + } + if (STRCMP(s, "silent!") == 0) { + emsg_silent = true; + emsg_noredir = true; + } + } else { + msg_silent++; + } + + garray_T capture_local; + ga_init(&capture_local, (int)sizeof(char), 80); + capture_ga = &capture_local; + + if (argvars[0].v_type != VAR_LIST) { + do_cmdline_cmd((char *)get_tv_string(&argvars[0])); + } else if (argvars[0].vval.v_list != NULL) { + list_T *list = argvars[0].vval.v_list; + list->lv_refcount++; + listitem_T *item = list->lv_first; + do_cmdline(NULL, get_list_line, (void *)&item, + DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED); + list->lv_refcount--; + } + msg_silent = save_msg_silent; + emsg_silent = save_emsg_silent; + emsg_noredir = save_emsg_noredir; + + ga_append(capture_ga, NUL); + rettv->v_type = VAR_STRING; + rettv->vval.v_string = vim_strsave(capture_ga->ga_data); + ga_clear(capture_ga); + + capture_ga = save_capture_ga; +} + /// "exepath()" function -static void f_exepath(typval_T *argvars, typval_T *rettv) +static void f_exepath(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *arg = get_tv_string(&argvars[0]); char_u *path = NULL; @@ -8723,7 +8915,7 @@ static void f_exepath(typval_T *argvars, typval_T *rettv) /* * "exists()" function */ -static void f_exists(typval_T *argvars, typval_T *rettv) +static void f_exists(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p; char_u *name; @@ -8784,17 +8976,9 @@ static void f_exists(typval_T *argvars, typval_T *rettv) } /* - * "exp()" function - */ -static void f_exp(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &exp); -} - -/* * "expand()" function */ -static void f_expand(typval_T *argvars, typval_T *rettv) +static void f_expand(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *s; size_t len; @@ -8923,7 +9107,7 @@ void dict_extend(dict_T *d1, dict_T *d2, char_u *action) * "extend(list, list [, idx])" function * "extend(dict, dict [, action])" function */ -static void f_extend(typval_T *argvars, typval_T *rettv) +static void f_extend(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *arg_errmsg = (char_u *)N_("extend() argument"); @@ -8995,7 +9179,7 @@ static void f_extend(typval_T *argvars, typval_T *rettv) /* * "feedkeys()" function */ -static void f_feedkeys(typval_T *argvars, typval_T *rettv) +static void f_feedkeys(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *keys, *flags = NULL; char_u nbuf[NUMBUFLEN]; @@ -9007,18 +9191,17 @@ static void f_feedkeys(typval_T *argvars, typval_T *rettv) return; keys = get_tv_string(&argvars[0]); - if (*keys != NUL) { - if (argvars[1].v_type != VAR_UNKNOWN) { - flags = get_tv_string_buf(&argvars[1], nbuf); - } - vim_feedkeys(cstr_as_string((char *)keys), - cstr_as_string((char *)flags), true); + if (argvars[1].v_type != VAR_UNKNOWN) { + flags = get_tv_string_buf(&argvars[1], nbuf); } + + nvim_feedkeys(cstr_as_string((char *)keys), + cstr_as_string((char *)flags), true); } /// "filereadable()" function -static void f_filereadable(typval_T *argvars, typval_T *rettv) +static void f_filereadable(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p = get_tv_string(&argvars[0]); rettv->vval.v_number = @@ -9029,7 +9212,7 @@ static void f_filereadable(typval_T *argvars, typval_T *rettv) * Return 0 for not writable, 1 for writable file, 2 for a dir which we have * rights to write into. */ -static void f_filewritable(typval_T *argvars, typval_T *rettv) +static void f_filewritable(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char *filename = (char *)get_tv_string(&argvars[0]); rettv->vval.v_number = os_file_is_writable(filename); @@ -9108,7 +9291,7 @@ static void filter_map(typval_T *argvars, typval_T *rettv, int map) dict_T *d = NULL; typval_T save_val; typval_T save_key; - int rem; + int rem = false; int todo; char_u *ermsg = (char_u *)(map ? "map()" : "filter()"); char_u *arg_errmsg = (char_u *)(map ? N_("map() argument") @@ -9244,7 +9427,7 @@ theend: /* * "filter()" function */ -static void f_filter(typval_T *argvars, typval_T *rettv) +static void f_filter(typval_T *argvars, typval_T *rettv, FunPtr fptr) { filter_map(argvars, rettv, FALSE); } @@ -9252,7 +9435,7 @@ static void f_filter(typval_T *argvars, typval_T *rettv) /* * "finddir({fname}[, {path}[, {count}]])" function */ -static void f_finddir(typval_T *argvars, typval_T *rettv) +static void f_finddir(typval_T *argvars, typval_T *rettv, FunPtr fptr) { findfilendir(argvars, rettv, FINDFILE_DIR); } @@ -9260,7 +9443,7 @@ static void f_finddir(typval_T *argvars, typval_T *rettv) /* * "findfile({fname}[, {path}[, {count}]])" function */ -static void f_findfile(typval_T *argvars, typval_T *rettv) +static void f_findfile(typval_T *argvars, typval_T *rettv, FunPtr fptr) { findfilendir(argvars, rettv, FINDFILE_FILE); } @@ -9268,7 +9451,7 @@ static void f_findfile(typval_T *argvars, typval_T *rettv) /* * "float2nr({float})" function */ -static void f_float2nr(typval_T *argvars, typval_T *rettv) +static void f_float2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { float_T f; @@ -9283,17 +9466,9 @@ static void f_float2nr(typval_T *argvars, typval_T *rettv) } /* - * "floor({float})" function - */ -static void f_floor(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &floor); -} - -/* * "fmod()" function */ -static void f_fmod(typval_T *argvars, typval_T *rettv) +static void f_fmod(typval_T *argvars, typval_T *rettv, FunPtr fptr) { float_T fx, fy; @@ -9308,7 +9483,7 @@ static void f_fmod(typval_T *argvars, typval_T *rettv) /* * "fnameescape({string})" function */ -static void f_fnameescape(typval_T *argvars, typval_T *rettv) +static void f_fnameescape(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_string = vim_strsave_fnameescape( get_tv_string(&argvars[0]), FALSE); @@ -9318,7 +9493,7 @@ static void f_fnameescape(typval_T *argvars, typval_T *rettv) /* * "fnamemodify({fname}, {mods})" function */ -static void f_fnamemodify(typval_T *argvars, typval_T *rettv) +static void f_fnamemodify(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *fname; char_u *mods; @@ -9369,7 +9544,7 @@ static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end) /* * "foldclosed()" function */ -static void f_foldclosed(typval_T *argvars, typval_T *rettv) +static void f_foldclosed(typval_T *argvars, typval_T *rettv, FunPtr fptr) { foldclosed_both(argvars, rettv, FALSE); } @@ -9377,7 +9552,7 @@ static void f_foldclosed(typval_T *argvars, typval_T *rettv) /* * "foldclosedend()" function */ -static void f_foldclosedend(typval_T *argvars, typval_T *rettv) +static void f_foldclosedend(typval_T *argvars, typval_T *rettv, FunPtr fptr) { foldclosed_both(argvars, rettv, TRUE); } @@ -9385,7 +9560,7 @@ static void f_foldclosedend(typval_T *argvars, typval_T *rettv) /* * "foldlevel()" function */ -static void f_foldlevel(typval_T *argvars, typval_T *rettv) +static void f_foldlevel(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; @@ -9397,26 +9572,31 @@ static void f_foldlevel(typval_T *argvars, typval_T *rettv) /* * "foldtext()" function */ -static void f_foldtext(typval_T *argvars, typval_T *rettv) +static void f_foldtext(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - linenr_T lnum; + linenr_T foldstart; + linenr_T foldend; + char_u *dashes; + linenr_T lnum; char_u *s; char_u *r; - int len; + int len; char *txt; + long count; rettv->v_type = VAR_STRING; rettv->vval.v_string = NULL; - if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0 - && (linenr_T)vimvars[VV_FOLDEND].vv_nr - <= curbuf->b_ml.ml_line_count - && vimvars[VV_FOLDDASHES].vv_str != NULL) { + + foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART); + foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND); + dashes = get_vim_var_str(VV_FOLDDASHES); + if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count + && dashes != NULL) { /* Find first non-empty line in the fold. */ - lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr; - while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr) { - if (!linewhite(lnum)) + for (lnum = foldstart; lnum < foldend; ++lnum) { + if (!linewhite(lnum)) { break; - ++lnum; + } } /* Find interesting text in this line. */ @@ -9424,21 +9604,19 @@ static void f_foldtext(typval_T *argvars, typval_T *rettv) /* skip C comment-start */ if (s[0] == '/' && (s[1] == '*' || s[1] == '/')) { s = skipwhite(s + 2); - if (*skipwhite(s) == NUL - && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr) { + if (*skipwhite(s) == NUL && lnum + 1 < foldend) { s = skipwhite(ml_get(lnum + 1)); if (*s == '*') s = skipwhite(s + 1); } } + count = (long)(foldend - foldstart + 1); txt = _("+-%s%3ld lines: "); r = xmalloc(STRLEN(txt) - + STRLEN(vimvars[VV_FOLDDASHES].vv_str) // for %s - + 20 // for %3ld - + STRLEN(s)); // concatenated - sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str, - (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr - - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1)); + + STRLEN(dashes) // for %s + + 20 // for %3ld + + STRLEN(s)); // concatenated + sprintf((char *)r, txt, dashes, count); len = (int)STRLEN(r); STRCAT(r, s); /* remove 'foldmarker' and 'commentstring' */ @@ -9450,7 +9628,7 @@ static void f_foldtext(typval_T *argvars, typval_T *rettv) /* * "foldtextresult(lnum)" function */ -static void f_foldtextresult(typval_T *argvars, typval_T *rettv) +static void f_foldtextresult(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; char_u *text; @@ -9477,46 +9655,160 @@ static void f_foldtextresult(typval_T *argvars, typval_T *rettv) /* * "foreground()" function */ -static void f_foreground(typval_T *argvars, typval_T *rettv) +static void f_foreground(typval_T *argvars, typval_T *rettv, FunPtr fptr) { } /* * "function()" function */ -static void f_function(typval_T *argvars, typval_T *rettv) +static void f_function(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *s; + char_u *name; + bool use_string = false; + partial_T *arg_pt = NULL; + + if (argvars[0].v_type == VAR_FUNC) { + // function(MyFunc, [arg], dict) + s = argvars[0].vval.v_string; + } else if (argvars[0].v_type == VAR_PARTIAL + && argvars[0].vval.v_partial != NULL) { + // function(dict.MyFunc, [arg]) + arg_pt = argvars[0].vval.v_partial; + s = arg_pt->pt_name; + } else { + // function('MyFunc', [arg], dict) + s = get_tv_string(&argvars[0]); + use_string = true; + } - s = get_tv_string(&argvars[0]); - if (s == NULL || *s == NUL || ascii_isdigit(*s)) + if (s == NULL || *s == NUL || (use_string && ascii_isdigit(*s))) { EMSG2(_(e_invarg2), s); - /* Don't check an autoload name for existence here. */ - else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s)) + } else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL + && !function_exists(s)) { + // Don't check an autoload name for existence here. EMSG2(_("E700: Unknown function: %s"), s); - else { + } else { + int dict_idx = 0; + int arg_idx = 0; + list_T *list = NULL; if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0) { char sid_buf[25]; int off = *s == 's' ? 2 : 5; - /* Expand s: and <SID> into <SNR>nr_, so that the function can - * also be called from another script. Using trans_function_name() - * would also work, but some plugins depend on the name being - * printable text. */ - sprintf(sid_buf, "<SNR>%" PRId64 "_", (int64_t)current_SID); - rettv->vval.v_string = xmalloc(STRLEN(sid_buf) + STRLEN(s + off) + 1); - STRCPY(rettv->vval.v_string, sid_buf); - STRCAT(rettv->vval.v_string, s + off); - } else - rettv->vval.v_string = vim_strsave(s); - rettv->v_type = VAR_FUNC; + // Expand s: and <SID> into <SNR>nr_, so that the function can + // also be called from another script. Using trans_function_name() + // would also work, but some plugins depend on the name being + // printable text. + snprintf(sid_buf, sizeof(sid_buf), "<SNR>%" PRId64 "_", + (int64_t)current_SID); + name = xmalloc(STRLEN(sid_buf) + STRLEN(s + off) + 1); + if (name != NULL) { + STRCPY(name, sid_buf); + STRCAT(name, s + off); + } + } else { + name = vim_strsave(s); + } + + if (argvars[1].v_type != VAR_UNKNOWN) { + if (argvars[2].v_type != VAR_UNKNOWN) { + // function(name, [args], dict) + arg_idx = 1; + dict_idx = 2; + } else if (argvars[1].v_type == VAR_DICT) { + // function(name, dict) + dict_idx = 1; + } else { + // function(name, [args]) + arg_idx = 1; + } + if (dict_idx > 0) { + if (argvars[dict_idx].v_type != VAR_DICT) { + EMSG(_("E922: expected a dict")); + xfree(name); + return; + } + if (argvars[dict_idx].vval.v_dict == NULL) { + dict_idx = 0; + } + } + if (arg_idx > 0) { + if (argvars[arg_idx].v_type != VAR_LIST) { + EMSG(_("E923: Second argument of function() must be " + "a list or a dict")); + xfree(name); + return; + } + list = argvars[arg_idx].vval.v_list; + if (list == NULL || list->lv_len == 0) { + arg_idx = 0; + } + } + } + if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL) { + partial_T *const pt = xcalloc(1, sizeof(*pt)); + + // result is a VAR_PARTIAL + if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0)) { + const int arg_len = (arg_pt == NULL ? 0 : arg_pt->pt_argc); + const int lv_len = (list == NULL ? 0 : list->lv_len); + + pt->pt_argc = arg_len + lv_len; + pt->pt_argv = xmalloc(sizeof(pt->pt_argv[0]) * pt->pt_argc); + if (pt->pt_argv == NULL) { + xfree(pt); + xfree(name); + return; + } else { + int i = 0; + for (; i < arg_len; i++) { + copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]); + } + if (lv_len > 0) { + for (listitem_T *li = list->lv_first; + li != NULL; + li = li->li_next) { + copy_tv(&li->li_tv, &pt->pt_argv[i++]); + } + } + } + } + + // For "function(dict.func, [], dict)" and "func" is a partial + // use "dict". That is backwards compatible. + if (dict_idx > 0) { + // The dict is bound explicitly, pt_auto is false + pt->pt_dict = argvars[dict_idx].vval.v_dict; + (pt->pt_dict->dv_refcount)++; + } else if (arg_pt != NULL) { + // If the dict was bound automatically the result is also + // bound automatically. + pt->pt_dict = arg_pt->pt_dict; + pt->pt_auto = arg_pt->pt_auto; + if (pt->pt_dict != NULL) { + (pt->pt_dict->dv_refcount)++; + } + } + + pt->pt_refcount = 1; + pt->pt_name = name; + func_ref(pt->pt_name); + + rettv->v_type = VAR_PARTIAL; + rettv->vval.v_partial = pt; + } else { + // result is a VAR_FUNC + rettv->v_type = VAR_FUNC; + rettv->vval.v_string = name; + func_ref(name); + } } } -/* - * "garbagecollect()" function - */ -static void f_garbagecollect(typval_T *argvars, typval_T *rettv) +/// "garbagecollect()" function +static void f_garbagecollect(typval_T *argvars, typval_T *rettv, FunPtr fptr) { /* This is postponed until we are back at the toplevel, because we may be * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */ @@ -9529,7 +9821,7 @@ static void f_garbagecollect(typval_T *argvars, typval_T *rettv) /* * "get()" function */ -static void f_get(typval_T *argvars, typval_T *rettv) +static void f_get(typval_T *argvars, typval_T *rettv, FunPtr fptr) { listitem_T *li; list_T *l; @@ -9551,8 +9843,50 @@ static void f_get(typval_T *argvars, typval_T *rettv) if (di != NULL) tv = &di->di_tv; } - } else + } else if (argvars[0].v_type == VAR_PARTIAL + || argvars[0].v_type == VAR_FUNC) { + partial_T *pt; + partial_T fref_pt; + + if (argvars[0].v_type == VAR_PARTIAL) { + pt = argvars[0].vval.v_partial; + } else { + memset(&fref_pt, 0, sizeof(fref_pt)); + fref_pt.pt_name = argvars[0].vval.v_string; + pt = &fref_pt; + } + + if (pt != NULL) { + char_u *what = get_tv_string(&argvars[1]); + + if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0) { + rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING); + if (pt->pt_name != NULL) { + rettv->vval.v_string = vim_strsave(pt->pt_name); + } + } else if (STRCMP(what, "dict") == 0) { + rettv->v_type = VAR_DICT; + rettv->vval.v_dict = pt->pt_dict; + if (pt->pt_dict != NULL) { + (pt->pt_dict->dv_refcount)++; + } + } else if (STRCMP(what, "args") == 0) { + rettv->v_type = VAR_LIST; + if (rettv_list_alloc(rettv) != NULL) { + int i; + + for (i = 0; i < pt->pt_argc; i++) { + list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]); + } + } + } else { + EMSG2(_(e_invarg2), what); + } + return; + } + } else { EMSG2(_(e_listdictarg), "get()"); + } if (tv == NULL) { if (argvars[2].v_type != VAR_UNKNOWN) @@ -9561,6 +9895,118 @@ static void f_get(typval_T *argvars, typval_T *rettv) copy_tv(tv, rettv); } +/// Returns information about signs placed in a buffer as list of dicts. +static void get_buffer_signs(buf_T *buf, list_T *l) +{ + for (signlist_T *sign = buf->b_signlist; sign; sign = sign->next) { + dict_T *d = dict_alloc(); + + dict_add_nr_str(d, "id", sign->id, NULL); + dict_add_nr_str(d, "lnum", sign->lnum, NULL); + dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr)); + + list_append_dict(l, d); + } +} + +/// Returns buffer options, variables and other attributes in a dictionary. +static dict_T *get_buffer_info(buf_T *buf) +{ + dict_T *dict = dict_alloc(); + + dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL); + dict_add_nr_str(dict, "name", 0L, + buf->b_ffname != NULL ? buf->b_ffname : (char_u *)""); + dict_add_nr_str(dict, "lnum", buflist_findlnum(buf), NULL); + dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL); + dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL); + dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL); + dict_add_nr_str(dict, "changedtick", buf->b_changedtick, NULL); + dict_add_nr_str(dict, "hidden", + buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0, + NULL); + + // Get a reference to buffer variables + dict_add_dict(dict, "variables", buf->b_vars); + + // List of windows displaying this buffer + list_T *windows = list_alloc(); + FOR_ALL_TAB_WINDOWS(tp, wp) { + if (wp->w_buffer == buf) { + list_append_number(windows, (varnumber_T)wp->handle); + } + } + dict_add_list(dict, "windows", windows); + + if (buf->b_signlist != NULL) { + // List of signs placed in this buffer + list_T *signs = list_alloc(); + get_buffer_signs(buf, signs); + dict_add_list(dict, "signs", signs); + } + + return dict; +} + +/// "getbufinfo()" function +static void f_getbufinfo(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + buf_T *argbuf = NULL; + bool filtered = false; + bool sel_buflisted = false; + bool sel_bufloaded = false; + + rettv_list_alloc(rettv); + + // List of all the buffers or selected buffers + if (argvars[0].v_type == VAR_DICT) { + dict_T *sel_d = argvars[0].vval.v_dict; + + if (sel_d != NULL) { + dictitem_T *di; + + filtered = true; + + di = dict_find(sel_d, (char_u *)"buflisted", -1); + if (di != NULL && get_tv_number(&di->di_tv)) { + sel_buflisted = true; + } + + di = dict_find(sel_d, (char_u *)"bufloaded", -1); + if (di != NULL && get_tv_number(&di->di_tv)) { + sel_bufloaded = true; + } + } + } else if (argvars[0].v_type != VAR_UNKNOWN) { + // Information about one buffer. Argument specifies the buffer + (void)get_tv_number(&argvars[0]); // issue errmsg if type error + emsg_off++; + argbuf = get_buf_tv(&argvars[0], false); + emsg_off--; + if (argbuf == NULL) { + return; + } + } + + // Return information about all the buffers or a specified buffer + FOR_ALL_BUFFERS(buf) { + if (argbuf != NULL && argbuf != buf) { + continue; + } + if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL) + || (sel_buflisted && !buf->b_p_bl))) { + continue; + } + + dict_T *d = get_buffer_info(buf); + if (d != NULL) { + list_append_dict(rettv->vval.v_list, d); + } + if (argbuf != NULL) { + return; + } + } +} /* * Get line or list of lines from buffer "buf" into "rettv". @@ -9605,7 +10051,7 @@ static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retli /* * "getbufline()" function */ -static void f_getbufline(typval_T *argvars, typval_T *rettv) +static void f_getbufline(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; linenr_T end; @@ -9628,7 +10074,7 @@ static void f_getbufline(typval_T *argvars, typval_T *rettv) /* * "getbufvar()" function */ -static void f_getbufvar(typval_T *argvars, typval_T *rettv) +static void f_getbufvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { buf_T *buf; buf_T *save_curbuf; @@ -9649,13 +10095,25 @@ static void f_getbufvar(typval_T *argvars, typval_T *rettv) save_curbuf = curbuf; curbuf = buf; - if (*varname == '&') { /* buffer-local-option */ - if (get_option_tv(&varname, rettv, TRUE) == OK) - done = TRUE; + if (*varname == '&') { // buffer-local-option + if (varname[1] == NUL) { + // get all buffer-local options in a dict + dict_T *opts = get_winbuf_options(true); + + if (opts != NULL) { + rettv->v_type = VAR_DICT; + rettv->vval.v_dict = opts; + opts->dv_refcount++; + done = true; + } + } else if (get_option_tv(&varname, rettv, true) == OK) { + // buffer-local-option + done = true; + } } else if (STRCMP(varname, "changedtick") == 0) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = curbuf->b_changedtick; - done = TRUE; + done = true; } else { /* Look up the variable. */ /* Let getbufvar({nr}, "") return the "b:" dictionary. */ @@ -9681,29 +10139,40 @@ static void f_getbufvar(typval_T *argvars, typval_T *rettv) /* * "getchar()" function */ -static void f_getchar(typval_T *argvars, typval_T *rettv) +static void f_getchar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { varnumber_T n; int error = FALSE; - /* Position the cursor. Needed after a message that ends in a space. */ - ui_cursor_goto(msg_row, msg_col); - ++no_mapping; ++allow_keys; for (;; ) { - if (argvars[0].v_type == VAR_UNKNOWN) - /* getchar(): blocking wait. */ + // Position the cursor. Needed after a message that ends in a space, + // or if event processing caused a redraw. + ui_cursor_goto(msg_row, msg_col); + + if (argvars[0].v_type == VAR_UNKNOWN) { + // getchar(): blocking wait. + if (!(char_avail() || using_script() || input_available())) { + input_enable_events(); + (void)os_inchar(NULL, 0, -1, 0); + input_disable_events(); + if (!multiqueue_empty(main_loop.events)) { + multiqueue_process_events(main_loop.events); + continue; + } + } n = safe_vgetc(); - else if (get_tv_number_chk(&argvars[0], &error) == 1) - /* getchar(1): only check if char avail */ + } else if (get_tv_number_chk(&argvars[0], &error) == 1) { + // getchar(1): only check if char avail n = vpeekc_any(); - else if (error || vpeekc_any() == NUL) - /* illegal argument or getchar(0) and no char avail: return zero */ + } else if (error || vpeekc_any() == NUL) { + // illegal argument or getchar(0) and no char avail: return zero n = 0; - else - /* getchar(0) and char avail: return char */ + } else { + // getchar(0) and char avail: return char n = safe_vgetc(); + } if (n == K_IGNORE) continue; @@ -9713,6 +10182,7 @@ static void f_getchar(typval_T *argvars, typval_T *rettv) --allow_keys; vimvars[VV_MOUSE_WIN].vv_nr = 0; + vimvars[VV_MOUSE_WINID].vv_nr = 0; vimvars[VV_MOUSE_LNUM].vv_nr = 0; vimvars[VV_MOUSE_COL].vv_nr = 0; @@ -9755,6 +10225,7 @@ static void f_getchar(typval_T *argvars, typval_T *rettv) for (wp = firstwin; wp != win; wp = wp->w_next) ++winnr; vimvars[VV_MOUSE_WIN].vv_nr = winnr; + vimvars[VV_MOUSE_WINID].vv_nr = wp->handle; vimvars[VV_MOUSE_LNUM].vv_nr = lnum; vimvars[VV_MOUSE_COL].vv_nr = col + 1; } @@ -9765,7 +10236,7 @@ static void f_getchar(typval_T *argvars, typval_T *rettv) /* * "getcharmod()" function */ -static void f_getcharmod(typval_T *argvars, typval_T *rettv) +static void f_getcharmod(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = mod_mask; } @@ -9773,7 +10244,7 @@ static void f_getcharmod(typval_T *argvars, typval_T *rettv) /* * "getcharsearch()" function */ -static void f_getcharsearch(typval_T *argvars, typval_T *rettv) +static void f_getcharsearch(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv_dict_alloc(rettv); @@ -9787,7 +10258,7 @@ static void f_getcharsearch(typval_T *argvars, typval_T *rettv) /* * "getcmdline()" function */ -static void f_getcmdline(typval_T *argvars, typval_T *rettv) +static void f_getcmdline(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = get_cmdline_str(); @@ -9796,7 +10267,7 @@ static void f_getcmdline(typval_T *argvars, typval_T *rettv) /* * "getcmdpos()" function */ -static void f_getcmdpos(typval_T *argvars, typval_T *rettv) +static void f_getcmdpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = get_cmdline_pos() + 1; } @@ -9804,7 +10275,7 @@ static void f_getcmdpos(typval_T *argvars, typval_T *rettv) /* * "getcmdtype()" function */ -static void f_getcmdtype(typval_T *argvars, typval_T *rettv) +static void f_getcmdtype(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = xmallocz(1); @@ -9814,7 +10285,7 @@ static void f_getcmdtype(typval_T *argvars, typval_T *rettv) /* * "getcmdwintype()" function */ -static void f_getcmdwintype(typval_T *argvars, typval_T *rettv) +static void f_getcmdwintype(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = NULL; @@ -9822,6 +10293,69 @@ static void f_getcmdwintype(typval_T *argvars, typval_T *rettv) rettv->vval.v_string[0] = cmdwin_type; } +// "getcompletion()" function +static void f_getcompletion(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + char_u *pat; + expand_T xpc; + bool filtered = false; + int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH + | WILD_NO_BEEP; + + if (argvars[2].v_type != VAR_UNKNOWN) { + filtered = get_tv_number_chk(&argvars[2], NULL); + } + + if (p_wic) { + options |= WILD_ICASE; + } + + // For filtered results, 'wildignore' is used + if (!filtered) { + options |= WILD_KEEP_ALL; + } + + ExpandInit(&xpc); + xpc.xp_pattern = get_tv_string(&argvars[0]); + xpc.xp_pattern_len = STRLEN(xpc.xp_pattern); + xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1])); + if (xpc.xp_context == EXPAND_NOTHING) { + if (argvars[1].v_type == VAR_STRING) { + EMSG2(_(e_invarg2), argvars[1].vval.v_string); + } else { + EMSG(_(e_invarg)); + } + return; + } + + if (xpc.xp_context == EXPAND_MENUS) { + set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, false); + xpc.xp_pattern_len = STRLEN(xpc.xp_pattern); + } + + if (xpc.xp_context == EXPAND_CSCOPE) { + set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope); + xpc.xp_pattern_len = STRLEN(xpc.xp_pattern); + } + + if (xpc.xp_context == EXPAND_SIGN) { + set_context_in_sign_cmd(&xpc, xpc.xp_pattern); + xpc.xp_pattern_len = STRLEN(xpc.xp_pattern); + } + + pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context); + rettv_list_alloc(rettv); + if (pat != NULL) { + ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP); + + for (int i = 0; i < xpc.xp_numfiles; i++) { + list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); + } + } + xfree(pat); + ExpandCleanup(&xpc); +} + /// `getcwd([{win}[, {tab}]])` function /// /// Every scope not specified implies the currently selected scope object. @@ -9831,10 +10365,10 @@ static void f_getcmdwintype(typval_T *argvars, typval_T *rettv) /// @pre An argument may not be -1 if preceding arguments are not all -1. /// /// @post The return value will be a string. -static void f_getcwd(typval_T *argvars, typval_T *rettv) +static void f_getcwd(typval_T *argvars, typval_T *rettv, FunPtr fptr) { // Possible scope of working directory to return. - CdScope scope = MIN_CD_SCOPE; + CdScope scope = kCdScopeInvalid; // Numbers of the scope objects (window, tab) we want the working directory // of. A `-1` means to skip this scope, a `0` means the current object. @@ -9863,26 +10397,27 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv) return; } scope_number[i] = argvars[i].vval.v_number; - // The scope is the current iteration step. - scope = i; // It is an error for the scope number to be less than `-1`. if (scope_number[i] < -1) { EMSG(_(e_invarg)); return; } + // Use the narrowest scope the user requested + if (scope_number[i] >= 0 && scope == kCdScopeInvalid) { + // The scope is the current iteration step. + scope = i; + } else if (scope_number[i] < 0) { + scope = i + 1; + } } - // Normalize scope, the number of the new scope will be 0. - if (scope_number[scope] < 0) { - // Arguments to `getcwd` always end at second-highest scope, so scope will - // always be <= `MAX_CD_SCOPE`. - scope++; + // If the user didn't specify anything, default to window scope + if (scope == kCdScopeInvalid) { + scope = MIN_CD_SCOPE; } // Find the tabpage by number - if (scope_number[kCdScopeTab] == -1) { - tp = NULL; - } else if (scope_number[kCdScopeTab] > 0) { + if (scope_number[kCdScopeTab] > 0) { tp = find_tabpage(scope_number[kCdScopeTab]); if (!tp) { EMSG(_("E5000: Cannot find tab number.")); @@ -9891,16 +10426,14 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv) } // Find the window in `tp` by number, `NULL` if none. - if (scope_number[kCdScopeWindow] == -1) { - win = NULL; - } else if (scope_number[kCdScopeWindow] >= 0) { - if (!tp) { + if (scope_number[kCdScopeWindow] >= 0) { + if (scope_number[kCdScopeTab] < 0) { EMSG(_("E5001: Higher scope cannot be -1 if lower scope is >= 0.")); return; } if (scope_number[kCdScopeWindow] > 0) { - win = find_win_by_nr(&argvars[0], curtab); + win = find_win_by_nr(&argvars[0], tp); if (!win) { EMSG(_("E5002: Cannot find window number.")); return; @@ -9917,24 +10450,21 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv) if (from) { break; } - case kCdScopeTab: // FALLTHROUGH + case kCdScopeTab: // FALLTHROUGH assert(tp); from = tp->localdir; if (from) { break; } - case kCdScopeGlobal: // FALLTHROUGH - // The `globaldir` variable is not always set. - if (globaldir) { + case kCdScopeGlobal: // FALLTHROUGH + if (globaldir) { // `globaldir` is not always set. from = globaldir; - } else { - // We have to copy the OS path directly into output string. - if (os_dirname(cwd, MAXPATHL) == FAIL) { - EMSG(_("E41: Could not display path.")); - goto end; - } + } else if (os_dirname(cwd, MAXPATHL) == FAIL) { // Get the OS CWD. + from = (char_u *)""; // Return empty string on failure. } break; + case kCdScopeInvalid: // We should never get here + assert(false); } if (from) { @@ -9945,14 +10475,14 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv) #ifdef BACKSLASH_IN_FILENAME slash_adjust(rettv->vval.v_string); #endif -end: + xfree(cwd); } /* * "getfontname()" function */ -static void f_getfontname(typval_T *argvars, typval_T *rettv) +static void f_getfontname(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = NULL; @@ -9961,7 +10491,7 @@ static void f_getfontname(typval_T *argvars, typval_T *rettv) /* * "getfperm({fname})" function */ -static void f_getfperm(typval_T *argvars, typval_T *rettv) +static void f_getfperm(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *perm = NULL; char_u flags[] = "rwx"; @@ -9982,7 +10512,7 @@ static void f_getfperm(typval_T *argvars, typval_T *rettv) /* * "getfsize({fname})" function */ -static void f_getfsize(typval_T *argvars, typval_T *rettv) +static void f_getfsize(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char *fname = (char *)get_tv_string(&argvars[0]); @@ -10009,7 +10539,7 @@ static void f_getfsize(typval_T *argvars, typval_T *rettv) /* * "getftime({fname})" function */ -static void f_getftime(typval_T *argvars, typval_T *rettv) +static void f_getftime(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char *fname = (char *)get_tv_string(&argvars[0]); @@ -10024,7 +10554,7 @@ static void f_getftime(typval_T *argvars, typval_T *rettv) /* * "getftype({fname})" function */ -static void f_getftype(typval_T *argvars, typval_T *rettv) +static void f_getftype(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *fname; char_u *type = NULL; @@ -10100,7 +10630,7 @@ static void f_getftype(typval_T *argvars, typval_T *rettv) /* * "getline(lnum, [end])" function */ -static void f_getline(typval_T *argvars, typval_T *rettv) +static void f_getline(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; linenr_T end; @@ -10121,7 +10651,7 @@ static void f_getline(typval_T *argvars, typval_T *rettv) /* * "getmatches()" function */ -static void f_getmatches(typval_T *argvars, typval_T *rettv) +static void f_getmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr) { matchitem_T *cur = curwin->w_match_head; int i; @@ -10170,7 +10700,7 @@ static void f_getmatches(typval_T *argvars, typval_T *rettv) /* * "getpid()" function */ -static void f_getpid(typval_T *argvars, typval_T *rettv) +static void f_getpid(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = os_get_pid(); } @@ -10206,7 +10736,7 @@ static void getpos_both(typval_T *argvars, typval_T *rettv, bool getcurpos) /* * "getcurpos(string)" function */ -static void f_getcurpos(typval_T *argvars, typval_T *rettv) +static void f_getcurpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { getpos_both(argvars, rettv, true); } @@ -10214,7 +10744,7 @@ static void f_getcurpos(typval_T *argvars, typval_T *rettv) /* * "getpos(string)" function */ -static void f_getpos(typval_T *argvars, typval_T *rettv) +static void f_getpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { getpos_both(argvars, rettv, false); } @@ -10222,7 +10752,7 @@ static void f_getpos(typval_T *argvars, typval_T *rettv) /* * "getqflist()" and "getloclist()" functions */ -static void f_getqflist(typval_T *argvars, typval_T *rettv) +static void f_getqflist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv_list_alloc(rettv); win_T *wp = NULL; @@ -10236,7 +10766,7 @@ static void f_getqflist(typval_T *argvars, typval_T *rettv) } /// "getreg()" function -static void f_getreg(typval_T *argvars, typval_T *rettv) +static void f_getreg(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *strregname; int regname; @@ -10282,7 +10812,7 @@ static void f_getreg(typval_T *argvars, typval_T *rettv) /* * "getregtype()" function */ -static void f_getregtype(typval_T *argvars, typval_T *rettv) +static void f_getregtype(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *strregname; int regname; @@ -10311,10 +10841,62 @@ static void f_getregtype(typval_T *argvars, typval_T *rettv) rettv->vval.v_string = (char_u *)xstrdup(buf); } +/// Returns information (variables, options, etc.) about a tab page +/// as a dictionary. +static dict_T *get_tabpage_info(tabpage_T *tp, int tp_idx) +{ + dict_T *dict = dict_alloc(); + + dict_add_nr_str(dict, "tabnr", tp_idx, NULL); + + list_T *l = list_alloc(); + FOR_ALL_WINDOWS_IN_TAB(wp, tp) { + list_append_number(l, (varnumber_T)wp->handle); + } + dict_add_list(dict, "windows", l); + + // Make a reference to tabpage variables + dict_add_dict(dict, "variables", tp->tp_vars); + + return dict; +} + +/// "gettabinfo()" function +static void f_gettabinfo(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + tabpage_T *tparg = NULL; + + rettv_list_alloc(rettv); + + if (argvars[0].v_type != VAR_UNKNOWN) { + // Information about one tab page + tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL)); + if (tparg == NULL) { + return; + } + } + + // Get information about a specific tab page or all tab pages + int tpnr = 0; + FOR_ALL_TABS(tp) { + tpnr++; + if (tparg != NULL && tp != tparg) { + continue; + } + dict_T *d = get_tabpage_info(tp, tpnr); + if (d != NULL) { + list_append_dict(rettv->vval.v_list, d); + } + if (tparg != NULL) { + return; + } + } +} + /* * "gettabvar()" function */ -static void f_gettabvar(typval_T *argvars, typval_T *rettv) +static void f_gettabvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { win_T *oldcurwin; tabpage_T *tp, *oldtabpage; @@ -10353,15 +10935,75 @@ static void f_gettabvar(typval_T *argvars, typval_T *rettv) /* * "gettabwinvar()" function */ -static void f_gettabwinvar(typval_T *argvars, typval_T *rettv) +static void f_gettabwinvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { getwinvar(argvars, rettv, 1); } +/// Returns information about a window as a dictionary. +static dict_T *get_win_info(win_T *wp, int16_t tpnr, int16_t winnr) +{ + dict_T *dict = dict_alloc(); + + dict_add_nr_str(dict, "tabnr", tpnr, NULL); + dict_add_nr_str(dict, "winnr", winnr, NULL); + dict_add_nr_str(dict, "winid", wp->handle, NULL); + dict_add_nr_str(dict, "height", wp->w_height, NULL); + dict_add_nr_str(dict, "width", wp->w_width, NULL); + dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL); + + dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL); + dict_add_nr_str(dict, "loclist", + (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), + NULL); + + // Add a reference to window variables + dict_add_dict(dict, "variables", wp->w_vars); + + return dict; +} + +/// "getwininfo()" function +static void f_getwininfo(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + win_T *wparg = NULL; + + rettv_list_alloc(rettv); + + if (argvars[0].v_type != VAR_UNKNOWN) { + wparg = win_id2wp(argvars); + if (wparg == NULL) { + return; + } + } + + // Collect information about either all the windows across all the tab + // pages or one particular window. + int16_t tabnr = 0; + FOR_ALL_TABS(tp) { + tabnr++; + int16_t winnr = 0; + FOR_ALL_WINDOWS_IN_TAB(wp, tp) { + if (wparg != NULL && wp != wparg) { + continue; + } + winnr++; + dict_T *d = get_win_info(wp, tabnr, winnr); + if (d != NULL) { + list_append_dict(rettv->vval.v_list, d); + } + if (wparg != NULL) { + // found information about a specific window + return; + } + } + } +} + /* * "getwinposx()" function */ -static void f_getwinposx(typval_T *argvars, typval_T *rettv) +static void f_getwinposx(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = -1; } @@ -10369,7 +11011,7 @@ static void f_getwinposx(typval_T *argvars, typval_T *rettv) /* * "getwinposy()" function */ -static void f_getwinposy(typval_T *argvars, typval_T *rettv) +static void f_getwinposy(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = -1; } @@ -10399,17 +11041,45 @@ find_win_by_nr ( } FOR_ALL_WINDOWS_IN_TAB(wp, tp) { - if (--nr <= 0) { + if (nr >= LOWEST_WIN_ID) { + if (wp->handle == nr) { + return wp; + } + } else if (--nr <= 0) { return wp; } } return NULL; } -/* - * "getwinvar()" function - */ -static void f_getwinvar(typval_T *argvars, typval_T *rettv) +/// Find window specified by "wvp" in tabpage "tvp". +static win_T *find_tabwin(typval_T *wvp, typval_T *tvp) +{ + win_T *wp = NULL; + tabpage_T *tp = NULL; + + if (wvp->v_type != VAR_UNKNOWN) { + if (tvp->v_type != VAR_UNKNOWN) { + long n = get_tv_number(tvp); + if (n >= 0) { + tp = find_tabpage(n); + } + } else { + tp = curtab; + } + + if (tp != NULL) { + wp = find_win_by_nr(wvp, tp); + } + } else { + wp = curwin; + } + + return wp; +} + +/// "getwinvar()" function +static void f_getwinvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { getwinvar(argvars, rettv, 0); } @@ -10449,8 +11119,19 @@ getwinvar ( bool need_switch_win = tp != curtab || win != curwin; if (!need_switch_win || switch_win(&oldcurwin, &oldtabpage, win, tp, true) == OK) { - if (*varname == '&') { // window-local-option - if (get_option_tv(&varname, rettv, 1) == OK) { + if (*varname == '&') { + if (varname[1] == NUL) { + // get all window-local options in a dict + dict_T *opts = get_winbuf_options(false); + + if (opts != NULL) { + rettv->v_type = VAR_DICT; + rettv->vval.v_dict = opts; + opts->dv_refcount++; + done = true; + } + } else if (get_option_tv(&varname, rettv, 1) == OK) { + // window-local-option done = true; } } else { @@ -10480,7 +11161,7 @@ getwinvar ( /* * "glob()" function */ -static void f_glob(typval_T *argvars, typval_T *rettv) +static void f_glob(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int options = WILD_SILENT|WILD_USE_NL; expand_T xpc; @@ -10524,7 +11205,7 @@ static void f_glob(typval_T *argvars, typval_T *rettv) } /// "globpath()" function -static void f_globpath(typval_T *argvars, typval_T *rettv) +static void f_globpath(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int flags = 0; // Flags for globpath. int error = false; @@ -10575,7 +11256,7 @@ static void f_globpath(typval_T *argvars, typval_T *rettv) } // "glob2regpat()" function -static void f_glob2regpat(typval_T *argvars, typval_T *rettv) +static void f_glob2regpat(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *pat = get_tv_string_chk(&argvars[0]); // NULL on type error @@ -10585,16 +11266,10 @@ static void f_glob2regpat(typval_T *argvars, typval_T *rettv) : file_pat_to_reg_pat(pat, NULL, NULL, false); } -/* - * "has()" function - */ -static void f_has(typval_T *argvars, typval_T *rettv) +/// "has()" function +static void f_has(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - int i; - char_u *name; - int n = FALSE; - static char *(has_list[]) = - { + static char *(has_list[]) = { #ifdef UNIX "unix", #endif @@ -10661,6 +11336,7 @@ static void f_has(typval_T *argvars, typval_T *rettv) "mouse", "multi_byte", "multi_lang", + "packages", "path_extra", "persistent_undo", "postscript", @@ -10688,6 +11364,7 @@ static void f_has(typval_T *argvars, typval_T *rettv) "termguicolors", "termresponse", "textobjects", + "timers", "title", "user-commands", /* was accidentally included in 5.4 */ "user_commands", @@ -10705,36 +11382,42 @@ static void f_has(typval_T *argvars, typval_T *rettv) NULL }; - name = get_tv_string(&argvars[0]); - for (i = 0; has_list[i] != NULL; ++i) + bool n = false; + char *name = (char *)get_tv_string(&argvars[0]); + + for (int i = 0; has_list[i] != NULL; i++) { if (STRICMP(name, has_list[i]) == 0) { - n = TRUE; + n = true; break; } + } - if (n == FALSE) { + if (!n) { if (STRNICMP(name, "patch", 5) == 0) { if (name[5] == '-' - && STRLEN(name) > 11 + && strlen(name) >= 11 && ascii_isdigit(name[6]) && ascii_isdigit(name[8]) && ascii_isdigit(name[10])) { - int major = atoi((char *)name + 6); - int minor = atoi((char *)name + 8); + int major = atoi(name + 6); + int minor = atoi(name + 8); // Expect "patch-9.9.01234". n = (major < VIM_VERSION_MAJOR || (major == VIM_VERSION_MAJOR && (minor < VIM_VERSION_MINOR || (minor == VIM_VERSION_MINOR - && has_patch(atoi((char *)name + 10)))))); + && has_vim_patch(atoi(name + 10)))))); } else { - n = has_patch(atoi((char *)name + 5)); + n = has_vim_patch(atoi(name + 5)); } + } else if (STRNICMP(name, "nvim-", 5) == 0) { + // Expect "nvim-x.y.z" + n = has_nvim_version(name + 5); } else if (STRICMP(name, "vim_starting") == 0) { n = (starting != 0); } else if (STRICMP(name, "multi_byte_encoding") == 0) { - n = has_mbyte; + n = has_mbyte != 0; #if defined(USE_ICONV) && defined(DYNAMIC_ICONV) } else if (STRICMP(name, "iconv") == 0) { n = iconv_enabled(false); @@ -10744,8 +11427,8 @@ static void f_has(typval_T *argvars, typval_T *rettv) } } - if (n == FALSE && eval_has_provider((char *)name)) { - n = TRUE; + if (!n && eval_has_provider(name)) { + n = true; } rettv->vval.v_number = n; @@ -10754,7 +11437,7 @@ static void f_has(typval_T *argvars, typval_T *rettv) /* * "has_key()" function */ -static void f_has_key(typval_T *argvars, typval_T *rettv) +static void f_has_key(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (argvars[0].v_type != VAR_DICT) { EMSG(_(e_dictreq)); @@ -10778,10 +11461,10 @@ static void f_has_key(typval_T *argvars, typval_T *rettv) /// @pre An argument may not be -1 if preceding arguments are not all -1. /// /// @post The return value will be either the number `1` or `0`. -static void f_haslocaldir(typval_T *argvars, typval_T *rettv) +static void f_haslocaldir(typval_T *argvars, typval_T *rettv, FunPtr fptr) { // Possible scope of working directory to return. - CdScope scope = MIN_CD_SCOPE; + CdScope scope = kCdScopeInvalid; // Numbers of the scope objects (window, tab) we want the working directory // of. A `-1` means to skip this scope, a `0` means the current object. @@ -10806,43 +11489,42 @@ static void f_haslocaldir(typval_T *argvars, typval_T *rettv) return; } scope_number[i] = argvars[i].vval.v_number; - // The scope is the current iteration step. - scope = i; if (scope_number[i] < -1) { EMSG(_(e_invarg)); return; } + // Use the narrowest scope the user requested + if (scope_number[i] >= 0 && scope == kCdScopeInvalid) { + // The scope is the current iteration step. + scope = i; + } else if (scope_number[i] < 0) { + scope = i + 1; + } } - // Normalize scope, the number of the new scope will be 0. - if (scope_number[scope] < 0) { - // Arguments to `haslocaldir` always end at second-highest scope, so scope - // will always be <= `MAX_CD_SCOPE`. - scope++; + // If the user didn't specify anything, default to window scope + if (scope == kCdScopeInvalid) { + scope = MIN_CD_SCOPE; } // Find the tabpage by number - if (scope_number[kCdScopeTab] == -1) { - tp = NULL; - } else if (scope_number[kCdScopeTab] > 0) { + if (scope_number[kCdScopeTab] > 0) { tp = find_tabpage(scope_number[kCdScopeTab]); if (!tp) { - EMSG(_("5000: Cannot find tab number.")); + EMSG(_("E5000: Cannot find tab number.")); return; } } // Find the window in `tp` by number, `NULL` if none. - if (scope_number[kCdScopeWindow] == -1) { - win = NULL; - } else if (scope_number[kCdScopeWindow] >= 0) { - if (!tp) { + if (scope_number[kCdScopeWindow] >= 0) { + if (scope_number[kCdScopeTab] < 0) { EMSG(_("E5001: Higher scope cannot be -1 if lower scope is >= 0.")); return; } if (scope_number[kCdScopeWindow] > 0) { - win = find_win_by_nr(&argvars[0], curtab); + win = find_win_by_nr(&argvars[0], tp); if (!win) { EMSG(_("E5002: Cannot find window number.")); return; @@ -10863,13 +11545,16 @@ static void f_haslocaldir(typval_T *argvars, typval_T *rettv) // The global scope never has a local directory rettv->vval.v_number = 0; break; + case kCdScopeInvalid: + // We should never get here + assert(false); } } /* * "hasmapto()" function */ -static void f_hasmapto(typval_T *argvars, typval_T *rettv) +static void f_hasmapto(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *name; char_u *mode; @@ -10894,7 +11579,7 @@ static void f_hasmapto(typval_T *argvars, typval_T *rettv) /* * "histadd()" function */ -static void f_histadd(typval_T *argvars, typval_T *rettv) +static void f_histadd(typval_T *argvars, typval_T *rettv, FunPtr fptr) { HistoryType histype; char_u *str; @@ -10920,7 +11605,7 @@ static void f_histadd(typval_T *argvars, typval_T *rettv) /* * "histdel()" function */ -static void f_histdel(typval_T *argvars, typval_T *rettv) +static void f_histdel(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int n; char_u buf[NUMBUFLEN]; @@ -10947,7 +11632,7 @@ static void f_histdel(typval_T *argvars, typval_T *rettv) /* * "histget()" function */ -static void f_histget(typval_T *argvars, typval_T *rettv) +static void f_histget(typval_T *argvars, typval_T *rettv, FunPtr fptr) { HistoryType type; int idx; @@ -10972,7 +11657,7 @@ static void f_histget(typval_T *argvars, typval_T *rettv) /* * "histnr()" function */ -static void f_histnr(typval_T *argvars, typval_T *rettv) +static void f_histnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int i; @@ -10991,7 +11676,7 @@ static void f_histnr(typval_T *argvars, typval_T *rettv) /* * "highlightID(name)" function */ -static void f_hlID(typval_T *argvars, typval_T *rettv) +static void f_hlID(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0])); } @@ -10999,7 +11684,7 @@ static void f_hlID(typval_T *argvars, typval_T *rettv) /* * "highlight_exists()" function */ -static void f_hlexists(typval_T *argvars, typval_T *rettv) +static void f_hlexists(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0])); } @@ -11007,7 +11692,7 @@ static void f_hlexists(typval_T *argvars, typval_T *rettv) /* * "hostname()" function */ -static void f_hostname(typval_T *argvars, typval_T *rettv) +static void f_hostname(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char hostname[256]; @@ -11019,7 +11704,7 @@ static void f_hostname(typval_T *argvars, typval_T *rettv) /* * iconv() function */ -static void f_iconv(typval_T *argvars, typval_T *rettv) +static void f_iconv(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u buf1[NUMBUFLEN]; char_u buf2[NUMBUFLEN]; @@ -11049,7 +11734,7 @@ static void f_iconv(typval_T *argvars, typval_T *rettv) /* * "indent()" function */ -static void f_indent(typval_T *argvars, typval_T *rettv) +static void f_indent(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; @@ -11063,7 +11748,7 @@ static void f_indent(typval_T *argvars, typval_T *rettv) /* * "index()" function */ -static void f_index(typval_T *argvars, typval_T *rettv) +static void f_index(typval_T *argvars, typval_T *rettv, FunPtr fptr) { list_T *l; listitem_T *item; @@ -11194,7 +11879,7 @@ static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog) * "input()" function * Also handles inputsecret() when inputsecret is set. */ -static void f_input(typval_T *argvars, typval_T *rettv) +static void f_input(typval_T *argvars, typval_T *rettv, FunPtr fptr) { get_user_input(argvars, rettv, FALSE); } @@ -11202,7 +11887,7 @@ static void f_input(typval_T *argvars, typval_T *rettv) /* * "inputdialog()" function */ -static void f_inputdialog(typval_T *argvars, typval_T *rettv) +static void f_inputdialog(typval_T *argvars, typval_T *rettv, FunPtr fptr) { get_user_input(argvars, rettv, TRUE); } @@ -11210,7 +11895,7 @@ static void f_inputdialog(typval_T *argvars, typval_T *rettv) /* * "inputlist()" function */ -static void f_inputlist(typval_T *argvars, typval_T *rettv) +static void f_inputlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { listitem_T *li; int selected; @@ -11246,7 +11931,7 @@ static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL}; /* * "inputrestore()" function */ -static void f_inputrestore(typval_T *argvars, typval_T *rettv) +static void f_inputrestore(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (!GA_EMPTY(&ga_userinput)) { --ga_userinput.ga_len; @@ -11262,7 +11947,7 @@ static void f_inputrestore(typval_T *argvars, typval_T *rettv) /* * "inputsave()" function */ -static void f_inputsave(typval_T *argvars, typval_T *rettv) +static void f_inputsave(typval_T *argvars, typval_T *rettv, FunPtr fptr) { // Add an entry to the stack of typeahead storage. tasave_T *p = GA_APPEND_VIA_PTR(tasave_T, &ga_userinput); @@ -11272,19 +11957,19 @@ static void f_inputsave(typval_T *argvars, typval_T *rettv) /* * "inputsecret()" function */ -static void f_inputsecret(typval_T *argvars, typval_T *rettv) +static void f_inputsecret(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - ++cmdline_star; - ++inputsecret_flag; - f_input(argvars, rettv); - --cmdline_star; - --inputsecret_flag; + cmdline_star++; + inputsecret_flag++; + f_input(argvars, rettv, NULL); + cmdline_star--; + inputsecret_flag--; } /* * "insert()" function */ -static void f_insert(typval_T *argvars, typval_T *rettv) +static void f_insert(typval_T *argvars, typval_T *rettv, FunPtr fptr) { long before = 0; listitem_T *item; @@ -11323,7 +12008,7 @@ static void f_insert(typval_T *argvars, typval_T *rettv) /* * "invert(expr)" function */ -static void f_invert(typval_T *argvars, typval_T *rettv) +static void f_invert(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL); } @@ -11331,7 +12016,7 @@ static void f_invert(typval_T *argvars, typval_T *rettv) /* * "isdirectory()" function */ -static void f_isdirectory(typval_T *argvars, typval_T *rettv) +static void f_isdirectory(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = os_isdir(get_tv_string(&argvars[0])); } @@ -11339,7 +12024,7 @@ static void f_isdirectory(typval_T *argvars, typval_T *rettv) /* * "islocked()" function */ -static void f_islocked(typval_T *argvars, typval_T *rettv) +static void f_islocked(typval_T *argvars, typval_T *rettv, FunPtr fptr) { lval_T lv; char_u *end; @@ -11451,13 +12136,13 @@ static void dict_list(typval_T *argvars, typval_T *rettv, int what) /* * "items(dict)" function */ -static void f_items(typval_T *argvars, typval_T *rettv) +static void f_items(typval_T *argvars, typval_T *rettv, FunPtr fptr) { dict_list(argvars, rettv, 2); } // "jobclose(id[, stream])" function -static void f_jobclose(typval_T *argvars, typval_T *rettv) +static void f_jobclose(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -11483,21 +12168,43 @@ static void f_jobclose(typval_T *argvars, typval_T *rettv) if (argvars[1].v_type == VAR_STRING) { char *stream = (char *)argvars[1].vval.v_string; if (!strcmp(stream, "stdin")) { - process_close_in(proc); + if (data->rpc) { + EMSG(_("Invalid stream on rpc job, use jobclose(id, 'rpc')")); + } else { + process_close_in(proc); + } } else if (!strcmp(stream, "stdout")) { - process_close_out(proc); + if (data->rpc) { + EMSG(_("Invalid stream on rpc job, use jobclose(id, 'rpc')")); + } else { + process_close_out(proc); + } } else if (!strcmp(stream, "stderr")) { process_close_err(proc); + } else if (!strcmp(stream, "rpc")) { + if (data->rpc) { + channel_close(data->id); + } else { + EMSG(_("Invalid job stream: Not an rpc job")); + } } else { EMSG2(_("Invalid job stream \"%s\""), stream); } } else { - process_close_streams(proc); + if (data->rpc) { + channel_close(data->id); + process_close_err(proc); + } else { + process_close_streams(proc); + if (proc->type == kProcessTypePty) { + pty_process_close_master(&data->proc.pty); + } + } } } // "jobpid(id)" function -static void f_jobpid(typval_T *argvars, typval_T *rettv) +static void f_jobpid(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -11522,7 +12229,7 @@ static void f_jobpid(typval_T *argvars, typval_T *rettv) } // "jobsend()" function -static void f_jobsend(typval_T *argvars, typval_T *rettv) +static void f_jobsend(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -11549,6 +12256,11 @@ static void f_jobsend(typval_T *argvars, typval_T *rettv) return; } + if (data->rpc) { + EMSG(_("Can't send raw data to rpc channel")); + return; + } + ssize_t input_len; char *input = (char *) save_tv_as_string(&argvars[1], &input_len, false); if (!input) { @@ -11562,7 +12274,7 @@ static void f_jobsend(typval_T *argvars, typval_T *rettv) } // "jobresize(job, width, height)" function -static void f_jobresize(typval_T *argvars, typval_T *rettv) +static void f_jobresize(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -11595,7 +12307,7 @@ static void f_jobresize(typval_T *argvars, typval_T *rettv) rettv->vval.v_number = 1; } -static char **tv_to_argv(typval_T *cmd_tv, char **cmd) +static char **tv_to_argv(typval_T *cmd_tv, char **cmd, bool *executable) { if (cmd_tv->v_type == VAR_STRING) { char *cmd_str = (char *)get_tv_string(cmd_tv); @@ -11613,7 +12325,7 @@ static char **tv_to_argv(typval_T *cmd_tv, char **cmd) list_T *argl = cmd_tv->vval.v_list; int argc = argl->lv_len; if (!argc) { - EMSG(_("Argument vector must have at least one item")); + EMSG(_(e_invarg)); // List must have at least one item. return NULL; } @@ -11621,9 +12333,8 @@ static char **tv_to_argv(typval_T *cmd_tv, char **cmd) const char_u *exe = get_tv_string_chk(&argl->lv_first->li_tv); if (!exe || !os_can_exe(exe, NULL, true)) { - // String is not executable - if (exe) { - EMSG2(e_jobexe, exe); + if (exe && executable) { + *executable = false; } return NULL; } @@ -11631,7 +12342,7 @@ static char **tv_to_argv(typval_T *cmd_tv, char **cmd) if (cmd) { *cmd = (char *)exe; } - + // Build the argument vector int i = 0; char **argv = xcalloc(argc + 1, sizeof(char *)); @@ -11649,7 +12360,7 @@ static char **tv_to_argv(typval_T *cmd_tv, char **cmd) } // "jobstart()" function -static void f_jobstart(typval_T *argvars, typval_T *rettv) +static void f_jobstart(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -11658,8 +12369,10 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv) return; } - char **argv = tv_to_argv(&argvars[0], NULL); + bool executable = true; + char **argv = tv_to_argv(&argvars[0], NULL, &executable); if (!argv) { + rettv->vval.v_number = executable ? 0 : -1; return; // Did error message in tv_to_argv. } @@ -11670,48 +12383,71 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv) return; } + dict_T *job_opts = NULL; - ufunc_T *on_stdout = NULL, *on_stderr = NULL, *on_exit = NULL; + bool detach = false, rpc = false, pty = false; + Callback on_stdout = CALLBACK_NONE, on_stderr = CALLBACK_NONE, + on_exit = CALLBACK_NONE; + char *cwd = NULL; if (argvars[1].v_type == VAR_DICT) { job_opts = argvars[1].vval.v_dict; + + detach = get_dict_number(job_opts, "detach") != 0; + rpc = get_dict_number(job_opts, "rpc") != 0; + pty = get_dict_number(job_opts, "pty") != 0; + if (pty && rpc) { + EMSG2(_(e_invarg2), "job cannot have both 'pty' and 'rpc' options set"); + shell_free_argv(argv); + return; + } + + char *new_cwd = (char *)get_dict_string(job_opts, "cwd", false); + if (new_cwd && strlen(new_cwd) > 0) { + cwd = new_cwd; + // The new cwd must be a directory. + if (!os_isdir((char_u *)cwd)) { + EMSG2(_(e_invarg2), "expected valid directory"); + shell_free_argv(argv); + return; + } + } + if (!common_job_callbacks(job_opts, &on_stdout, &on_stderr, &on_exit)) { shell_free_argv(argv); return; } } - bool pty = job_opts && get_dict_number(job_opts, (uint8_t *)"pty") != 0; - bool detach = job_opts && get_dict_number(job_opts, (uint8_t *)"detach") != 0; TerminalJobData *data = common_job_init(argv, on_stdout, on_stderr, on_exit, - job_opts, pty, detach); + pty, rpc, detach, cwd); Process *proc = (Process *)&data->proc; if (pty) { - uint16_t width = get_dict_number(job_opts, (uint8_t *)"width"); + uint16_t width = get_dict_number(job_opts, "width"); if (width > 0) { data->proc.pty.width = width; } - uint16_t height = get_dict_number(job_opts, (uint8_t *)"height"); + uint16_t height = get_dict_number(job_opts, "height"); if (height > 0) { data->proc.pty.height = height; } - char *term = (char *)get_dict_string(job_opts, (uint8_t *)"TERM", true); + char *term = (char *)get_dict_string(job_opts, "TERM", true); if (term) { data->proc.pty.term_name = term; } } - if (!on_stdout) { + if (!rpc && on_stdout.type == kCallbackNone) { proc->out = NULL; } - if (!on_stderr) { + if (on_stderr.type == kCallbackNone) { proc->err = NULL; } common_job_start(data, rettv); } // "jobstop()" function -static void f_jobstop(typval_T *argvars, typval_T *rettv) +static void f_jobstop(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -11739,7 +12475,7 @@ static void f_jobstop(typval_T *argvars, typval_T *rettv) } // "jobwait(ids[, timeout])" function -static void f_jobwait(typval_T *argvars, typval_T *rettv) +static void f_jobwait(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -11758,7 +12494,7 @@ static void f_jobwait(typval_T *argvars, typval_T *rettv) list_T *rv = list_alloc(); ui_busy_start(); - Queue *waiting_jobs = queue_new_parent(loop_on_put, &loop); + MultiQueue *waiting_jobs = multiqueue_new_parent(loop_on_put, &main_loop); // For each item in the input list append an integer to the output list. -3 // is used to represent an invalid job id, -2 is for a interrupted job and // -1 for jobs that were skipped or timed out. @@ -11774,8 +12510,8 @@ static void f_jobwait(typval_T *argvars, typval_T *rettv) data->status_ptr = &rv->lv_last->li_tv.vval.v_number; // Process any pending events for the job because we'll temporarily // replace the parent queue - queue_process_events(data->events); - queue_replace_parent(data->events, waiting_jobs); + multiqueue_process_events(data->events); + multiqueue_replace_parent(data->events, waiting_jobs); } } @@ -11835,11 +12571,11 @@ static void f_jobwait(typval_T *argvars, typval_T *rettv) continue; } // restore the parent queue for the job - queue_process_events(data->events); - queue_replace_parent(data->events, loop.events); + multiqueue_process_events(data->events); + multiqueue_replace_parent(data->events, main_loop.events); } - queue_free(waiting_jobs); + multiqueue_free(waiting_jobs); ui_busy_stop(); rv->lv_refcount++; rettv->v_type = VAR_LIST; @@ -11849,7 +12585,7 @@ static void f_jobwait(typval_T *argvars, typval_T *rettv) /* * "join()" function */ -static void f_join(typval_T *argvars, typval_T *rettv) +static void f_join(typval_T *argvars, typval_T *rettv, FunPtr fptr) { garray_T ga; char_u *sep; @@ -11877,7 +12613,7 @@ static void f_join(typval_T *argvars, typval_T *rettv) } /// json_decode() function -static void f_json_decode(typval_T *argvars, typval_T *rettv) +static void f_json_decode(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char numbuf[NUMBUFLEN]; char *s = NULL; @@ -11911,7 +12647,7 @@ static void f_json_decode(typval_T *argvars, typval_T *rettv) } /// json_encode() function -static void f_json_encode(typval_T *argvars, typval_T *rettv) +static void f_json_encode(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = (char_u *) encode_tv2json(&argvars[0], NULL); @@ -11920,7 +12656,7 @@ static void f_json_encode(typval_T *argvars, typval_T *rettv) /* * "keys()" function */ -static void f_keys(typval_T *argvars, typval_T *rettv) +static void f_keys(typval_T *argvars, typval_T *rettv, FunPtr fptr) { dict_list(argvars, rettv, 0); } @@ -11928,7 +12664,7 @@ static void f_keys(typval_T *argvars, typval_T *rettv) /* * "last_buffer_nr()" function. */ -static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv) +static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int n = 0; @@ -11944,7 +12680,7 @@ static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv) /* * "len()" function */ -static void f_len(typval_T *argvars, typval_T *rettv) +static void f_len(typval_T *argvars, typval_T *rettv, FunPtr fptr) { switch (argvars[0].v_type) { case VAR_STRING: @@ -11962,6 +12698,7 @@ static void f_len(typval_T *argvars, typval_T *rettv) case VAR_SPECIAL: case VAR_FLOAT: case VAR_FUNC: + case VAR_PARTIAL: EMSG(_("E701: Invalid type for len()")); break; } @@ -12015,7 +12752,7 @@ static void libcall_common(typval_T *argvars, typval_T *rettv, int out_type) /* * "libcall()" function */ -static void f_libcall(typval_T *argvars, typval_T *rettv) +static void f_libcall(typval_T *argvars, typval_T *rettv, FunPtr fptr) { libcall_common(argvars, rettv, VAR_STRING); } @@ -12023,7 +12760,7 @@ static void f_libcall(typval_T *argvars, typval_T *rettv) /* * "libcallnr()" function */ -static void f_libcallnr(typval_T *argvars, typval_T *rettv) +static void f_libcallnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { libcall_common(argvars, rettv, VAR_NUMBER); } @@ -12031,7 +12768,7 @@ static void f_libcallnr(typval_T *argvars, typval_T *rettv) /* * "line(string)" function */ -static void f_line(typval_T *argvars, typval_T *rettv) +static void f_line(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum = 0; pos_T *fp; @@ -12046,7 +12783,7 @@ static void f_line(typval_T *argvars, typval_T *rettv) /* * "line2byte(lnum)" function */ -static void f_line2byte(typval_T *argvars, typval_T *rettv) +static void f_line2byte(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; @@ -12062,7 +12799,7 @@ static void f_line2byte(typval_T *argvars, typval_T *rettv) /* * "lispindent(lnum)" function */ -static void f_lispindent(typval_T *argvars, typval_T *rettv) +static void f_lispindent(typval_T *argvars, typval_T *rettv, FunPtr fptr) { pos_T pos; linenr_T lnum; @@ -12080,7 +12817,7 @@ static void f_lispindent(typval_T *argvars, typval_T *rettv) /* * "localtime()" function */ -static void f_localtime(typval_T *argvars, typval_T *rettv) +static void f_localtime(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = (varnumber_T)time(NULL); } @@ -12156,26 +12893,9 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact) } /* - * "log()" function - */ -static void f_log(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &log); -} - -/* - * "log10()" function - */ -static void f_log10(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &log10); -} - - -/* * "map()" function */ -static void f_map(typval_T *argvars, typval_T *rettv) +static void f_map(typval_T *argvars, typval_T *rettv, FunPtr fptr) { filter_map(argvars, rettv, TRUE); } @@ -12183,7 +12903,7 @@ static void f_map(typval_T *argvars, typval_T *rettv) /* * "maparg()" function */ -static void f_maparg(typval_T *argvars, typval_T *rettv) +static void f_maparg(typval_T *argvars, typval_T *rettv, FunPtr fptr) { get_maparg(argvars, rettv, TRUE); } @@ -12191,7 +12911,7 @@ static void f_maparg(typval_T *argvars, typval_T *rettv) /* * "mapcheck()" function */ -static void f_mapcheck(typval_T *argvars, typval_T *rettv) +static void f_mapcheck(typval_T *argvars, typval_T *rettv, FunPtr fptr) { get_maparg(argvars, rettv, FALSE); } @@ -12220,9 +12940,16 @@ static void find_some_match(typval_T *argvars, typval_T *rettv, int type) p_cpo = (char_u *)""; rettv->vval.v_number = -1; - if (type == 3) { - /* return empty list when there are no matches */ + if (type == 3 || type == 4) { + // type 3: return empty list when there are no matches. + // type 4: return ["", -1, -1, -1] rettv_list_alloc(rettv); + if (type == 4) { + list_append_string(rettv->vval.v_list, (char_u *)"", 0); + list_append_number(rettv->vval.v_list, (varnumber_T)-1); + list_append_number(rettv->vval.v_list, (varnumber_T)-1); + list_append_number(rettv->vval.v_list, (varnumber_T)-1); + } } else if (type == 2) { rettv->v_type = VAR_STRING; rettv->vval.v_string = NULL; @@ -12285,7 +13012,7 @@ static void find_some_match(typval_T *argvars, typval_T *rettv, int type) break; } xfree(tofree); - tofree = str = (char_u *) encode_tv2echo(&li->li_tv, NULL); + tofree = expr = str = (char_u *)encode_tv2echo(&li->li_tv, NULL); if (str == NULL) { break; } @@ -12313,7 +13040,21 @@ static void find_some_match(typval_T *argvars, typval_T *rettv, int type) } if (match) { - if (type == 3) { + if (type == 4) { + listitem_T *li1 = rettv->vval.v_list->lv_first; + listitem_T *li2 = li1->li_next; + listitem_T *li3 = li2->li_next; + listitem_T *li4 = li3->li_next; + xfree(li1->li_tv.vval.v_string); + + int rd = (int)(regmatch.endp[0] - regmatch.startp[0]); + li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0], rd); + li3->li_tv.vval.v_number = (varnumber_T)(regmatch.startp[0] - expr); + li4->li_tv.vval.v_number = (varnumber_T)(regmatch.endp[0] - expr); + if (l != NULL) { + li2->li_tv.vval.v_number = (varnumber_T)idx; + } + } else if (type == 3) { int i; /* return list with matched string and submatches */ @@ -12348,6 +13089,11 @@ static void find_some_match(typval_T *argvars, typval_T *rettv, int type) vim_regfree(regmatch.regprog); } + if (type == 4 && l == NULL) { + // matchstrpos() without a list: drop the second item + listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first->li_next); + } + theend: xfree(tofree); p_cpo = save_cpo; @@ -12356,7 +13102,7 @@ theend: /* * "match()" function */ -static void f_match(typval_T *argvars, typval_T *rettv) +static void f_match(typval_T *argvars, typval_T *rettv, FunPtr fptr) { find_some_match(argvars, rettv, 1); } @@ -12364,7 +13110,7 @@ static void f_match(typval_T *argvars, typval_T *rettv) /* * "matchadd()" function */ -static void f_matchadd(typval_T *argvars, typval_T *rettv) +static void f_matchadd(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u buf[NUMBUFLEN]; char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */ @@ -12390,7 +13136,7 @@ static void f_matchadd(typval_T *argvars, typval_T *rettv) if (dict_find(argvars[4].vval.v_dict, (char_u *)"conceal", -1) != NULL) { conceal_char = get_dict_string(argvars[4].vval.v_dict, - (char_u *)"conceal", false); + "conceal", false); } } } @@ -12407,7 +13153,7 @@ static void f_matchadd(typval_T *argvars, typval_T *rettv) conceal_char); } -static void f_matchaddpos(typval_T *argvars, typval_T *rettv) FUNC_ATTR_NONNULL_ALL +static void f_matchaddpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = -1; @@ -12446,7 +13192,7 @@ static void f_matchaddpos(typval_T *argvars, typval_T *rettv) FUNC_ATTR_NONNULL_ if (dict_find(argvars[4].vval.v_dict, (char_u *)"conceal", -1) != NULL) { conceal_char = get_dict_string(argvars[4].vval.v_dict, - (char_u *)"conceal", false); + "conceal", false); } } } @@ -12468,7 +13214,7 @@ static void f_matchaddpos(typval_T *argvars, typval_T *rettv) FUNC_ATTR_NONNULL_ /* * "matcharg()" function */ -static void f_matcharg(typval_T *argvars, typval_T *rettv) +static void f_matcharg(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv_list_alloc(rettv); @@ -12490,7 +13236,7 @@ static void f_matcharg(typval_T *argvars, typval_T *rettv) /* * "matchdelete()" function */ -static void f_matchdelete(typval_T *argvars, typval_T *rettv) +static void f_matchdelete(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = match_delete(curwin, (int)get_tv_number(&argvars[0]), TRUE); @@ -12499,7 +13245,7 @@ static void f_matchdelete(typval_T *argvars, typval_T *rettv) /* * "matchend()" function */ -static void f_matchend(typval_T *argvars, typval_T *rettv) +static void f_matchend(typval_T *argvars, typval_T *rettv, FunPtr fptr) { find_some_match(argvars, rettv, 0); } @@ -12507,7 +13253,7 @@ static void f_matchend(typval_T *argvars, typval_T *rettv) /* * "matchlist()" function */ -static void f_matchlist(typval_T *argvars, typval_T *rettv) +static void f_matchlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { find_some_match(argvars, rettv, 3); } @@ -12515,11 +13261,16 @@ static void f_matchlist(typval_T *argvars, typval_T *rettv) /* * "matchstr()" function */ -static void f_matchstr(typval_T *argvars, typval_T *rettv) +static void f_matchstr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { find_some_match(argvars, rettv, 2); } +/// "matchstrpos()" function +static void f_matchstrpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + find_some_match(argvars, rettv, 4); +} static void max_min(typval_T *argvars, typval_T *rettv, int domax) { @@ -12575,7 +13326,7 @@ static void max_min(typval_T *argvars, typval_T *rettv, int domax) /* * "max()" function */ -static void f_max(typval_T *argvars, typval_T *rettv) +static void f_max(typval_T *argvars, typval_T *rettv, FunPtr fptr) { max_min(argvars, rettv, TRUE); } @@ -12583,7 +13334,7 @@ static void f_max(typval_T *argvars, typval_T *rettv) /* * "min()" function */ -static void f_min(typval_T *argvars, typval_T *rettv) +static void f_min(typval_T *argvars, typval_T *rettv, FunPtr fptr) { max_min(argvars, rettv, FALSE); } @@ -12591,7 +13342,7 @@ static void f_min(typval_T *argvars, typval_T *rettv) /* * "mkdir()" function */ -static void f_mkdir(typval_T *argvars, typval_T *rettv) +static void f_mkdir(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *dir; char_u buf[NUMBUFLEN]; @@ -12633,7 +13384,7 @@ static void f_mkdir(typval_T *argvars, typval_T *rettv) /* * "mode()" function */ -static void f_mode(typval_T *argvars, typval_T *rettv) +static void f_mode(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u buf[3]; @@ -12687,7 +13438,7 @@ static void f_mode(typval_T *argvars, typval_T *rettv) } /// "msgpackdump()" function -static void f_msgpackdump(typval_T *argvars, typval_T *rettv) +static void f_msgpackdump(typval_T *argvars, typval_T *rettv, FunPtr fptr) FUNC_ATTR_NONNULL_ALL { if (argvars[0].v_type != VAR_LIST) { @@ -12715,7 +13466,7 @@ static void f_msgpackdump(typval_T *argvars, typval_T *rettv) } /// "msgpackparse" function -static void f_msgpackparse(typval_T *argvars, typval_T *rettv) +static void f_msgpackparse(typval_T *argvars, typval_T *rettv, FunPtr fptr) FUNC_ATTR_NONNULL_ALL { if (argvars[0].v_type != VAR_LIST) { @@ -12796,7 +13547,7 @@ f_msgpackparse_exit: /* * "nextnonblank()" function */ -static void f_nextnonblank(typval_T *argvars, typval_T *rettv) +static void f_nextnonblank(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; @@ -12814,7 +13565,7 @@ static void f_nextnonblank(typval_T *argvars, typval_T *rettv) /* * "nr2char()" function */ -static void f_nr2char(typval_T *argvars, typval_T *rettv) +static void f_nr2char(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u buf[NUMBUFLEN]; @@ -12838,7 +13589,7 @@ static void f_nr2char(typval_T *argvars, typval_T *rettv) /* * "or(expr, expr)" function */ -static void f_or(typval_T *argvars, typval_T *rettv) +static void f_or(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL) | get_tv_number_chk(&argvars[1], NULL); @@ -12847,7 +13598,7 @@ static void f_or(typval_T *argvars, typval_T *rettv) /* * "pathshorten()" function */ -static void f_pathshorten(typval_T *argvars, typval_T *rettv) +static void f_pathshorten(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = get_tv_string_chk(&argvars[0]); @@ -12860,7 +13611,7 @@ static void f_pathshorten(typval_T *argvars, typval_T *rettv) /* * "pow()" function */ -static void f_pow(typval_T *argvars, typval_T *rettv) +static void f_pow(typval_T *argvars, typval_T *rettv, FunPtr fptr) { float_T fx, fy; @@ -12875,7 +13626,7 @@ static void f_pow(typval_T *argvars, typval_T *rettv) /* * "prevnonblank()" function */ -static void f_prevnonblank(typval_T *argvars, typval_T *rettv) +static void f_prevnonblank(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; @@ -12897,7 +13648,7 @@ static va_list ap; /* * "printf()" function */ -static void f_printf(typval_T *argvars, typval_T *rettv) +static void f_printf(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = NULL; @@ -12923,7 +13674,7 @@ static void f_printf(typval_T *argvars, typval_T *rettv) /* * "pumvisible()" function */ -static void f_pumvisible(typval_T *argvars, typval_T *rettv) +static void f_pumvisible(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (pum_visible()) rettv->vval.v_number = 1; @@ -12932,7 +13683,7 @@ static void f_pumvisible(typval_T *argvars, typval_T *rettv) /* * "pyeval()" function */ -static void f_pyeval(typval_T *argvars, typval_T *rettv) +static void f_pyeval(typval_T *argvars, typval_T *rettv, FunPtr fptr) { script_host_eval("python", argvars, rettv); } @@ -12940,7 +13691,7 @@ static void f_pyeval(typval_T *argvars, typval_T *rettv) /* * "py3eval()" function */ -static void f_py3eval(typval_T *argvars, typval_T *rettv) +static void f_py3eval(typval_T *argvars, typval_T *rettv, FunPtr fptr) { script_host_eval("python3", argvars, rettv); } @@ -12948,7 +13699,7 @@ static void f_py3eval(typval_T *argvars, typval_T *rettv) /* * "range()" function */ -static void f_range(typval_T *argvars, typval_T *rettv) +static void f_range(typval_T *argvars, typval_T *rettv, FunPtr fptr) { long start; long end; @@ -12983,7 +13734,7 @@ static void f_range(typval_T *argvars, typval_T *rettv) /* * "readfile()" function */ -static void f_readfile(typval_T *argvars, typval_T *rettv) +static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int binary = FALSE; char_u *fname; @@ -13182,7 +13933,7 @@ static int list2proftime(typval_T *arg, proftime_T *tm) FUNC_ATTR_NONNULL_ALL /// one argument it returns the time passed since the argument. /// With two arguments it returns the time passed between /// the two arguments. -static void f_reltime(typval_T *argvars, typval_T *rettv) FUNC_ATTR_NONNULL_ALL +static void f_reltime(typval_T *argvars, typval_T *rettv, FunPtr fptr) { proftime_T res; proftime_T start; @@ -13228,7 +13979,7 @@ static void f_reltime(typval_T *argvars, typval_T *rettv) FUNC_ATTR_NONNULL_ALL /// @return The string representation of the argument, the format is the /// number of seconds followed by a dot, followed by the number /// of microseconds. -static void f_reltimestr(typval_T *argvars, typval_T *rettv) +static void f_reltimestr(typval_T *argvars, typval_T *rettv, FunPtr fptr) FUNC_ATTR_NONNULL_ALL { proftime_T tm; @@ -13243,7 +13994,7 @@ static void f_reltimestr(typval_T *argvars, typval_T *rettv) /* * "remove()" function */ -static void f_remove(typval_T *argvars, typval_T *rettv) +static void f_remove(typval_T *argvars, typval_T *rettv, FunPtr fptr) { list_T *l; listitem_T *item, *item2; @@ -13328,7 +14079,7 @@ static void f_remove(typval_T *argvars, typval_T *rettv) /* * "rename({from}, {to})" function */ -static void f_rename(typval_T *argvars, typval_T *rettv) +static void f_rename(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u buf[NUMBUFLEN]; @@ -13342,7 +14093,7 @@ static void f_rename(typval_T *argvars, typval_T *rettv) /* * "repeat()" function */ -static void f_repeat(typval_T *argvars, typval_T *rettv) +static void f_repeat(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p; int n; @@ -13376,7 +14127,7 @@ static void f_repeat(typval_T *argvars, typval_T *rettv) /* * "resolve()" function */ -static void f_resolve(typval_T *argvars, typval_T *rettv) +static void f_resolve(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p; #ifdef HAVE_READLINK @@ -13388,11 +14139,12 @@ static void f_resolve(typval_T *argvars, typval_T *rettv) { char_u *v = NULL; - v = mch_resolve_shortcut(p); - if (v != NULL) + v = os_resolve_shortcut(p); + if (v != NULL) { rettv->vval.v_string = v; - else + } else { rettv->vval.v_string = vim_strsave(p); + } } #else # ifdef HAVE_READLINK @@ -13549,7 +14301,7 @@ fail: /* * "reverse({list})" function */ -static void f_reverse(typval_T *argvars, typval_T *rettv) +static void f_reverse(typval_T *argvars, typval_T *rettv, FunPtr fptr) { list_T *l; listitem_T *li, *ni; @@ -13720,16 +14472,8 @@ theend: return retval; } -/* - * "round({float})" function - */ -static void f_round(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &round); -} - // "rpcnotify()" function -static void f_rpcnotify(typval_T *argvars, typval_T *rettv) +static void f_rpcnotify(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -13765,7 +14509,7 @@ static void f_rpcnotify(typval_T *argvars, typval_T *rettv) } // "rpcrequest()" function -static void f_rpcrequest(typval_T *argvars, typval_T *rettv) +static void f_rpcrequest(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -13838,7 +14582,7 @@ static void f_rpcrequest(typval_T *argvars, typval_T *rettv) } if (err.set) { - vim_report_error(cstr_as_string(err.msg)); + nvim_err_writeln(cstr_as_string(err.msg)); goto end; } @@ -13850,8 +14594,8 @@ end: api_free_object(result); } -// "rpcstart()" function -static void f_rpcstart(typval_T *argvars, typval_T *rettv) +// "rpcstart()" function (DEPRECATED) +static void f_rpcstart(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -13903,17 +14647,15 @@ static void f_rpcstart(typval_T *argvars, typval_T *rettv) // The last item of argv must be NULL argv[i] = NULL; - uint64_t channel_id = channel_from_process(argv); - - if (!channel_id) { - EMSG(_(e_api_spawn_failed)); - } - rettv->vval.v_number = (varnumber_T)channel_id; + TerminalJobData *data = common_job_init(argv, CALLBACK_NONE, CALLBACK_NONE, + CALLBACK_NONE, false, true, false, + NULL); + common_job_start(data, rettv); } // "rpcstop()" function -static void f_rpcstop(typval_T *argvars, typval_T *rettv) +static void f_rpcstop(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_NUMBER; rettv->vval.v_number = 0; @@ -13928,13 +14670,18 @@ static void f_rpcstop(typval_T *argvars, typval_T *rettv) return; } - rettv->vval.v_number = channel_close(argvars[0].vval.v_number); + // if called with a job, stop it, else closes the channel + if (pmap_get(uint64_t)(jobs, argvars[0].vval.v_number)) { + f_jobstop(argvars, rettv, NULL); + } else { + rettv->vval.v_number = channel_close(argvars[0].vval.v_number); + } } /* * "screenattr()" function */ -static void f_screenattr(typval_T *argvars, typval_T *rettv) +static void f_screenattr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int row; int col; @@ -13953,7 +14700,7 @@ static void f_screenattr(typval_T *argvars, typval_T *rettv) /* * "screenchar()" function */ -static void f_screenchar(typval_T *argvars, typval_T *rettv) +static void f_screenchar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int row; int col; @@ -13980,7 +14727,7 @@ static void f_screenchar(typval_T *argvars, typval_T *rettv) * * First column is 1 to be consistent with virtcol(). */ -static void f_screencol(typval_T *argvars, typval_T *rettv) +static void f_screencol(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = ui_current_col() + 1; } @@ -13988,7 +14735,7 @@ static void f_screencol(typval_T *argvars, typval_T *rettv) /* * "screenrow()" function */ -static void f_screenrow(typval_T *argvars, typval_T *rettv) +static void f_screenrow(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = ui_current_row() + 1; } @@ -13996,7 +14743,7 @@ static void f_screenrow(typval_T *argvars, typval_T *rettv) /* * "search()" function */ -static void f_search(typval_T *argvars, typval_T *rettv) +static void f_search(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int flags = 0; @@ -14006,7 +14753,7 @@ static void f_search(typval_T *argvars, typval_T *rettv) /* * "searchdecl()" function */ -static void f_searchdecl(typval_T *argvars, typval_T *rettv) +static void f_searchdecl(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int locally = 1; int thisblock = 0; @@ -14099,7 +14846,7 @@ theend: /* * "searchpair()" function */ -static void f_searchpair(typval_T *argvars, typval_T *rettv) +static void f_searchpair(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = searchpair_cmn(argvars, NULL); } @@ -14107,7 +14854,7 @@ static void f_searchpair(typval_T *argvars, typval_T *rettv) /* * "searchpairpos()" function */ -static void f_searchpairpos(typval_T *argvars, typval_T *rettv) +static void f_searchpairpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { pos_T match_pos; int lnum = 0; @@ -14272,7 +15019,7 @@ do_searchpair ( /* * "searchpos()" function */ -static void f_searchpos(typval_T *argvars, typval_T *rettv) +static void f_searchpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { pos_T match_pos; int lnum = 0; @@ -14295,7 +15042,7 @@ static void f_searchpos(typval_T *argvars, typval_T *rettv) } /// "serverlist()" function -static void f_serverlist(typval_T *argvars, typval_T *rettv) +static void f_serverlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { size_t n; char **addrs = server_address_list(&n); @@ -14313,7 +15060,7 @@ static void f_serverlist(typval_T *argvars, typval_T *rettv) } /// "serverstart()" function -static void f_serverstart(typval_T *argvars, typval_T *rettv) +static void f_serverstart(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = NULL; // Address of the new server @@ -14341,7 +15088,7 @@ static void f_serverstart(typval_T *argvars, typval_T *rettv) } /// "serverstop()" function -static void f_serverstop(typval_T *argvars, typval_T *rettv) +static void f_serverstop(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (check_restricted() || check_secure()) { return; @@ -14360,10 +15107,9 @@ static void f_serverstop(typval_T *argvars, typval_T *rettv) /* * "setbufvar()" function */ -static void f_setbufvar(typval_T *argvars, typval_T *rettv) +static void f_setbufvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { buf_T *buf; - aco_save_T aco; char_u *varname, *bufvarname; typval_T *varp; char_u nbuf[NUMBUFLEN]; @@ -14376,33 +15122,38 @@ static void f_setbufvar(typval_T *argvars, typval_T *rettv) varp = &argvars[2]; if (buf != NULL && varname != NULL && varp != NULL) { - /* set curbuf to be our buf, temporarily */ - aucmd_prepbuf(&aco, buf); - if (*varname == '&') { long numval; char_u *strval; - int error = FALSE; + int error = false; + aco_save_T aco; + + // set curbuf to be our buf, temporarily + aucmd_prepbuf(&aco, buf); ++varname; numval = get_tv_number_chk(varp, &error); strval = get_tv_string_buf_chk(varp, nbuf); if (!error && strval != NULL) set_option_value(varname, numval, strval, OPT_LOCAL); + + // reset notion of buffer + aucmd_restbuf(&aco); } else { + buf_T *save_curbuf = curbuf; + bufvarname = xmalloc(STRLEN(varname) + 3); + curbuf = buf; STRCPY(bufvarname, "b:"); STRCPY(bufvarname + 2, varname); set_var(bufvarname, varp, TRUE); xfree(bufvarname); + curbuf = save_curbuf; } - - /* reset notion of buffer */ - aucmd_restbuf(&aco); } } -static void f_setcharsearch(typval_T *argvars, typval_T *rettv) +static void f_setcharsearch(typval_T *argvars, typval_T *rettv, FunPtr fptr) { dict_T *d; dictitem_T *di; @@ -14414,7 +15165,7 @@ static void f_setcharsearch(typval_T *argvars, typval_T *rettv) } if ((d = argvars[0].vval.v_dict) != NULL) { - csearch = get_dict_string(d, (char_u *)"char", FALSE); + csearch = get_dict_string(d, "char", false); if (csearch != NULL) { if (enc_utf8) { int pcc[MAX_MCO]; @@ -14439,7 +15190,7 @@ static void f_setcharsearch(typval_T *argvars, typval_T *rettv) /* * "setcmdpos()" function */ -static void f_setcmdpos(typval_T *argvars, typval_T *rettv) +static void f_setcmdpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int pos = (int)get_tv_number(&argvars[0]) - 1; @@ -14449,7 +15200,7 @@ static void f_setcmdpos(typval_T *argvars, typval_T *rettv) /// "setfperm({fname}, {mode})" function -static void f_setfperm(typval_T *argvars, typval_T *rettv) +static void f_setfperm(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = 0; @@ -14482,7 +15233,7 @@ static void f_setfperm(typval_T *argvars, typval_T *rettv) /* * "setline()" function */ -static void f_setline(typval_T *argvars, typval_T *rettv) +static void f_setline(typval_T *argvars, typval_T *rettv, FunPtr fptr) { linenr_T lnum; char_u *line = NULL; @@ -14559,6 +15310,7 @@ static void f_setline(typval_T *argvars, typval_T *rettv) static void set_qf_ll_list(win_T *wp, typval_T *args, typval_T *rettv) FUNC_ATTR_NONNULL_ARG(2, 3) { + static char *e_invact = N_("E927: Invalid action: '%s'"); char_u *title = NULL; int action = ' '; rettv->vval.v_number = -1; @@ -14574,12 +15326,15 @@ static void set_qf_ll_list(win_T *wp, typval_T *args, typval_T *rettv) // Option argument was not given. goto skip_args; } else if (action_arg->v_type != VAR_STRING) { - EMSG(_(e_strreq)); + EMSG(_(e_stringreq)); return; } char_u *act = get_tv_string_chk(action_arg); - if (*act == 'a' || *act == 'r') { + if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL) { action = *act; + } else { + EMSG2(_(e_invact), act); + return; } typval_T *title_arg = &args[2]; @@ -14607,7 +15362,7 @@ skip_args: /* * "setloclist()" function */ -static void f_setloclist(typval_T *argvars, typval_T *rettv) +static void f_setloclist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { win_T *win; @@ -14622,7 +15377,7 @@ static void f_setloclist(typval_T *argvars, typval_T *rettv) /* * "setmatches()" function */ -static void f_setmatches(typval_T *argvars, typval_T *rettv) +static void f_setmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr) { list_T *l; listitem_T *li; @@ -14688,22 +15443,23 @@ static void f_setmatches(typval_T *argvars, typval_T *rettv) } } - char_u *group = get_dict_string(d, (char_u *)"group", false); - int priority = get_dict_number(d, (char_u *)"priority"); - int id = get_dict_number(d, (char_u *)"id"); + char_u *group = get_dict_string(d, "group", true); + int priority = get_dict_number(d, "priority"); + int id = get_dict_number(d, "id"); char_u *conceal = dict_find(d, (char_u *)"conceal", -1) != NULL - ? get_dict_string(d, (char_u *)"conceal", - false) + ? get_dict_string(d, "conceal", true) : NULL; if (i == 0) { match_add(curwin, group, - get_dict_string(d, (char_u *)"pattern", false), + get_dict_string(d, "pattern", false), priority, id, NULL, conceal); } else { match_add(curwin, group, NULL, priority, id, s, conceal); list_unref(s); s = NULL; } + xfree(group); + xfree(conceal); li = li->li_next; } rettv->vval.v_number = 0; @@ -14713,7 +15469,7 @@ static void f_setmatches(typval_T *argvars, typval_T *rettv) /* * "setpos()" function */ -static void f_setpos(typval_T *argvars, typval_T *rettv) +static void f_setpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { pos_T pos; int fnum; @@ -14755,7 +15511,7 @@ static void f_setpos(typval_T *argvars, typval_T *rettv) /* * "setqflist()" function */ -static void f_setqflist(typval_T *argvars, typval_T *rettv) +static void f_setqflist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { set_qf_ll_list(NULL, argvars, rettv); } @@ -14763,7 +15519,7 @@ static void f_setqflist(typval_T *argvars, typval_T *rettv) /* * "setreg()" function */ -static void f_setreg(typval_T *argvars, typval_T *rettv) +static void f_setreg(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int regname; char_u *strregname; @@ -14811,7 +15567,10 @@ static void f_setreg(typval_T *argvars, typval_T *rettv) } if (argvars[1].v_type == VAR_LIST) { - int len = argvars[1].vval.v_list->lv_len; + list_T *ll = argvars[1].vval.v_list; + // If the list is NULL handle like an empty list. + int len = ll == NULL ? 0 : ll->lv_len; + // First half: use for pointers to result lines; second half: use for // pointers to allocated copies. char_u **lstval = xmalloc(sizeof(char_u *) * ((len + 1) * 2)); @@ -14820,7 +15579,7 @@ static void f_setreg(typval_T *argvars, typval_T *rettv) char_u **curallocval = allocval; char_u buf[NUMBUFLEN]; - for (listitem_T *li = argvars[1].vval.v_list->lv_first; + for (listitem_T *li = ll == NULL ? NULL : ll->lv_first; li != NULL; li = li->li_next) { char_u *strval = get_tv_string_buf_chk(&li->li_tv, buf); @@ -14837,7 +15596,8 @@ static void f_setreg(typval_T *argvars, typval_T *rettv) } *curval++ = NULL; - write_reg_contents_lst(regname, lstval, -1, append, yank_type, block_len); + write_reg_contents_lst(regname, lstval, STRLEN(lstval), + append, yank_type, block_len); free_lstval: while (curallocval > allocval) @@ -14848,7 +15608,8 @@ free_lstval: if (strval == NULL) { return; } - write_reg_contents_ex(regname, strval, -1, append, yank_type, block_len); + write_reg_contents_ex(regname, strval, STRLEN(strval), + append, yank_type, block_len); } rettv->vval.v_number = 0; } @@ -14856,7 +15617,7 @@ free_lstval: /* * "settabvar()" function */ -static void f_settabvar(typval_T *argvars, typval_T *rettv) +static void f_settabvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { tabpage_T *save_curtab; tabpage_T *tp; @@ -14893,7 +15654,7 @@ static void f_settabvar(typval_T *argvars, typval_T *rettv) /* * "settabwinvar()" function */ -static void f_settabwinvar(typval_T *argvars, typval_T *rettv) +static void f_settabwinvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { setwinvar(argvars, rettv, 1); } @@ -14901,7 +15662,7 @@ static void f_settabwinvar(typval_T *argvars, typval_T *rettv) /* * "setwinvar()" function */ -static void f_setwinvar(typval_T *argvars, typval_T *rettv) +static void f_setwinvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { setwinvar(argvars, rettv, 0); } @@ -14961,7 +15722,7 @@ static void setwinvar(typval_T *argvars, typval_T *rettv, int off) } /// f_sha256 - sha256({string}) function -static void f_sha256(typval_T *argvars, typval_T *rettv) +static void f_sha256(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p = get_tv_string(&argvars[0]); const char_u *hash = sha256_bytes(p, (int) STRLEN(p) , NULL, 0); @@ -14974,7 +15735,7 @@ static void f_sha256(typval_T *argvars, typval_T *rettv) /* * "shellescape({string})" function */ -static void f_shellescape(typval_T *argvars, typval_T *rettv) +static void f_shellescape(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_string = vim_strsave_shellescape( get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), true); @@ -14984,7 +15745,7 @@ static void f_shellescape(typval_T *argvars, typval_T *rettv) /* * shiftwidth() function */ -static void f_shiftwidth(typval_T *argvars, typval_T *rettv) +static void f_shiftwidth(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = get_sw_value(curbuf); } @@ -14992,7 +15753,7 @@ static void f_shiftwidth(typval_T *argvars, typval_T *rettv) /* * "simplify()" function */ -static void f_simplify(typval_T *argvars, typval_T *rettv) +static void f_simplify(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p; @@ -15002,35 +15763,25 @@ static void f_simplify(typval_T *argvars, typval_T *rettv) rettv->v_type = VAR_STRING; } -/* - * "sin()" function - */ -static void f_sin(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &sin); -} - -/* - * "sinh()" function - */ -static void f_sinh(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &sinh); -} - /// struct used in the array that's given to qsort() typedef struct { listitem_T *item; int idx; } sortItem_T; -static int item_compare_ic; -static bool item_compare_numeric; -static bool item_compare_numbers; -static bool item_compare_float; -static char_u *item_compare_func; -static dict_T *item_compare_selfdict; -static int item_compare_func_err; +/// struct storing information about current sort +typedef struct { + int item_compare_ic; + bool item_compare_numeric; + bool item_compare_numbers; + bool item_compare_float; + char_u *item_compare_func; + partial_T *item_compare_partial; + dict_T *item_compare_selfdict; + int item_compare_func_err; +} sortinfo_T; +static sortinfo_T *sortinfo = NULL; + #define ITEM_COMPARE_FAIL 999 /* @@ -15050,14 +15801,14 @@ static int item_compare(const void *s1, const void *s2, bool keep_zero) typval_T *tv1 = &si1->item->li_tv; typval_T *tv2 = &si2->item->li_tv; - if (item_compare_numbers) { + if (sortinfo->item_compare_numbers) { long v1 = get_tv_number(tv1); long v2 = get_tv_number(tv2); return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; } - if (item_compare_float) { + if (sortinfo->item_compare_float) { float_T v1 = get_tv_float(tv1); float_T v2 = get_tv_float(tv2); @@ -15068,7 +15819,7 @@ static int item_compare(const void *s1, const void *s2, bool keep_zero) // do that for string variables. Use a single quote when comparing with // a non-string to do what the docs promise. if (tv1->v_type == VAR_STRING) { - if (tv2->v_type != VAR_STRING || item_compare_numeric) { + if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric) { p1 = (char_u *)"'"; } else { p1 = tv1->vval.v_string; @@ -15077,7 +15828,7 @@ static int item_compare(const void *s1, const void *s2, bool keep_zero) tofree1 = p1 = (char_u *) encode_tv2string(tv1, NULL); } if (tv2->v_type == VAR_STRING) { - if (tv1->v_type != VAR_STRING || item_compare_numeric) { + if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric) { p2 = (char_u *)"'"; } else { p2 = tv2->vval.v_string; @@ -15085,12 +15836,14 @@ static int item_compare(const void *s1, const void *s2, bool keep_zero) } else { tofree2 = p2 = (char_u *) encode_tv2string(tv2, NULL); } - if (p1 == NULL) + if (p1 == NULL) { p1 = (char_u *)""; - if (p2 == NULL) + } + if (p2 == NULL) { p2 = (char_u *)""; - if (!item_compare_numeric) { - if (item_compare_ic) { + } + if (!sortinfo->item_compare_numeric) { + if (sortinfo->item_compare_ic) { res = STRICMP(p1, p2); } else { res = STRCMP(p1, p2); @@ -15130,32 +15883,43 @@ static int item_compare2(const void *s1, const void *s2, bool keep_zero) typval_T rettv; typval_T argv[3]; int dummy; + char_u *func_name; + partial_T *partial = sortinfo->item_compare_partial; - /* shortcut after failure in previous call; compare all items equal */ - if (item_compare_func_err) + // shortcut after failure in previous call; compare all items equal + if (sortinfo->item_compare_func_err) { return 0; + } si1 = (sortItem_T *)s1; si2 = (sortItem_T *)s2; + if (partial == NULL) { + func_name = sortinfo->item_compare_func; + } else { + func_name = partial->pt_name; + } // Copy the values. This is needed to be able to set v_lock to VAR_FIXED // in the copy without changing the original list items. copy_tv(&si1->item->li_tv, &argv[0]); copy_tv(&si2->item->li_tv, &argv[1]); - rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ - res = call_func(item_compare_func, (int)STRLEN(item_compare_func), - &rettv, 2, argv, 0L, 0L, &dummy, TRUE, - item_compare_selfdict); + rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this + res = call_func(func_name, + (int)STRLEN(func_name), + &rettv, 2, argv, 0L, 0L, &dummy, true, + partial, sortinfo->item_compare_selfdict); clear_tv(&argv[0]); clear_tv(&argv[1]); - if (res == FAIL) + if (res == FAIL) { res = ITEM_COMPARE_FAIL; - else - res = get_tv_number_chk(&rettv, &item_compare_func_err); - if (item_compare_func_err) - res = ITEM_COMPARE_FAIL; /* return value has wrong type */ + } else { + res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err); + } + if (sortinfo->item_compare_func_err) { + res = ITEM_COMPARE_FAIL; // return value has wrong type + } clear_tv(&rettv); // When the result would be zero, compare the pointers themselves. Makes @@ -15188,6 +15952,12 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) long len; long i; + // Pointer to current info struct used in compare function. Save and restore + // the current one for nested calls. + sortinfo_T info; + sortinfo_T *old_sortinfo = sortinfo; + sortinfo = &info; + if (argvars[0].v_type != VAR_LIST) { EMSG2(_(e_listarg), sort ? "sort()" : "uniq()"); } else { @@ -15198,61 +15968,73 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) ? N_("sort() argument") : N_("uniq() argument")), true)) { - return; + goto theend; } rettv->vval.v_list = l; rettv->v_type = VAR_LIST; ++l->lv_refcount; len = list_len(l); - if (len <= 1) - return; /* short list sorts pretty quickly */ + if (len <= 1) { + goto theend; // short list sorts pretty quickly + } - item_compare_ic = FALSE; - item_compare_numeric = false; - item_compare_numbers = false; - item_compare_float = false; - item_compare_func = NULL; - item_compare_selfdict = NULL; + info.item_compare_ic = false; + info.item_compare_numeric = false; + info.item_compare_numbers = false; + info.item_compare_float = false; + info.item_compare_func = NULL; + info.item_compare_partial = NULL; + info.item_compare_selfdict = NULL; if (argvars[1].v_type != VAR_UNKNOWN) { /* optional second argument: {func} */ if (argvars[1].v_type == VAR_FUNC) { - item_compare_func = argvars[1].vval.v_string; + info.item_compare_func = argvars[1].vval.v_string; + } else if (argvars[1].v_type == VAR_PARTIAL) { + info.item_compare_partial = argvars[1].vval.v_partial; } else { int error = FALSE; i = get_tv_number_chk(&argvars[1], &error); - if (error) - return; /* type error; errmsg already given */ - if (i == 1) - item_compare_ic = TRUE; - else - item_compare_func = get_tv_string(&argvars[1]); - if (item_compare_func != NULL) { - if (STRCMP(item_compare_func, "n") == 0) { - item_compare_func = NULL; - item_compare_numeric = true; - } else if (STRCMP(item_compare_func, "N") == 0) { - item_compare_func = NULL; - item_compare_numbers = true; - } else if (STRCMP(item_compare_func, "f") == 0) { - item_compare_func = NULL; - item_compare_float = true; - } else if (STRCMP(item_compare_func, "i") == 0) { - item_compare_func = NULL; - item_compare_ic = TRUE; + if (error) { + goto theend; // type error; errmsg already given + } + if (i == 1) { + info.item_compare_ic = true; + } else if (argvars[1].v_type != VAR_NUMBER) { + info.item_compare_func = get_tv_string(&argvars[1]); + } else if (i != 0) { + EMSG(_(e_invarg)); + goto theend; + } + if (info.item_compare_func != NULL) { + if (*info.item_compare_func == NUL) { + // empty string means default sort + info.item_compare_func = NULL; + } else if (STRCMP(info.item_compare_func, "n") == 0) { + info.item_compare_func = NULL; + info.item_compare_numeric = true; + } else if (STRCMP(info.item_compare_func, "N") == 0) { + info.item_compare_func = NULL; + info.item_compare_numbers = true; + } else if (STRCMP(info.item_compare_func, "f") == 0) { + info.item_compare_func = NULL; + info.item_compare_float = true; + } else if (STRCMP(info.item_compare_func, "i") == 0) { + info.item_compare_func = NULL; + info.item_compare_ic = true; } } } if (argvars[2].v_type != VAR_UNKNOWN) { - /* optional third argument: {dict} */ + // optional third argument: {dict} if (argvars[2].v_type != VAR_DICT) { EMSG(_(e_dictreq)); - return; + goto theend; } - item_compare_selfdict = argvars[2].vval.v_dict; + info.item_compare_selfdict = argvars[2].vval.v_dict; } } @@ -15268,19 +16050,22 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) i++; } - item_compare_func_err = FALSE; + info.item_compare_func_err = false; // Test the compare function. - if (item_compare_func != NULL + if ((info.item_compare_func != NULL + || info.item_compare_partial != NULL) && item_compare2_not_keeping_zero(&ptrs[0], &ptrs[1]) == ITEM_COMPARE_FAIL) { EMSG(_("E702: Sort compare function failed")); } else { // Sort the array with item pointers. qsort(ptrs, (size_t)len, sizeof (sortItem_T), - item_compare_func == NULL ? item_compare_not_keeping_zero : - item_compare2_not_keeping_zero); + (info.item_compare_func == NULL + && info.item_compare_partial == NULL ? + item_compare_not_keeping_zero : + item_compare2_not_keeping_zero)); - if (!item_compare_func_err) { + if (!info.item_compare_func_err) { // Clear the list and append the items in the sorted order. l->lv_first = NULL; l->lv_last = NULL; @@ -15296,21 +16081,25 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) int (*item_compare_func_ptr)(const void *, const void *); // f_uniq(): ptrs will be a stack of items to remove. - item_compare_func_err = FALSE; - item_compare_func_ptr = item_compare_func ? item_compare2_keeping_zero : - item_compare_keeping_zero; + info.item_compare_func_err = false; + if (info.item_compare_func != NULL + || info.item_compare_partial != NULL) { + item_compare_func_ptr = item_compare2_keeping_zero; + } else { + item_compare_func_ptr = item_compare_keeping_zero; + } for (li = l->lv_first; li != NULL && li->li_next != NULL; li = li->li_next) { if (item_compare_func_ptr(&li, &li->li_next) == 0) { ptrs[i++].item = li; } - if (item_compare_func_err) { + if (info.item_compare_func_err) { EMSG(_("E882: Uniq compare function failed")); break; } } - if (!item_compare_func_err) { + if (!info.item_compare_func_err) { while (--i >= 0) { assert(ptrs[i].item->li_next); li = ptrs[i].item->li_next; @@ -15329,16 +16118,19 @@ static void do_sort_uniq(typval_T *argvars, typval_T *rettv, bool sort) xfree(ptrs); } + +theend: + sortinfo = old_sortinfo; } /// "sort"({list})" function -static void f_sort(typval_T *argvars, typval_T *rettv) +static void f_sort(typval_T *argvars, typval_T *rettv, FunPtr fptr) { do_sort_uniq(argvars, rettv, true); } /// "uniq({list})" function -static void f_uniq(typval_T *argvars, typval_T *rettv) +static void f_uniq(typval_T *argvars, typval_T *rettv, FunPtr fptr) { do_sort_uniq(argvars, rettv, false); } @@ -15346,7 +16138,7 @@ static void f_uniq(typval_T *argvars, typval_T *rettv) // // "reltimefloat()" function // -static void f_reltimefloat(typval_T *argvars , typval_T *rettv) +static void f_reltimefloat(typval_T *argvars , typval_T *rettv, FunPtr fptr) FUNC_ATTR_NONNULL_ALL { proftime_T tm; @@ -15361,7 +16153,7 @@ static void f_reltimefloat(typval_T *argvars , typval_T *rettv) /* * "soundfold({word})" function */ -static void f_soundfold(typval_T *argvars, typval_T *rettv) +static void f_soundfold(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *s; @@ -15373,7 +16165,7 @@ static void f_soundfold(typval_T *argvars, typval_T *rettv) /* * "spellbadword()" function */ -static void f_spellbadword(typval_T *argvars, typval_T *rettv) +static void f_spellbadword(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *word = (char_u *)""; hlf_T attr = HLF_COUNT; @@ -15417,7 +16209,7 @@ static void f_spellbadword(typval_T *argvars, typval_T *rettv) /* * "spellsuggest()" function */ -static void f_spellsuggest(typval_T *argvars, typval_T *rettv) +static void f_spellsuggest(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *str; int typeerr = FALSE; @@ -15457,7 +16249,7 @@ static void f_spellsuggest(typval_T *argvars, typval_T *rettv) } } -static void f_split(typval_T *argvars, typval_T *rettv) +static void f_split(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *str; char_u *end; @@ -15526,17 +16318,9 @@ static void f_split(typval_T *argvars, typval_T *rettv) } /* - * "sqrt()" function - */ -static void f_sqrt(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &sqrt); -} - -/* * "str2float()" function */ -static void f_str2float(typval_T *argvars, typval_T *rettv) +static void f_str2float(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p = skipwhite(get_tv_string(&argvars[0])); @@ -15547,7 +16331,7 @@ static void f_str2float(typval_T *argvars, typval_T *rettv) } // "str2nr()" function -static void f_str2nr(typval_T *argvars, typval_T *rettv) +static void f_str2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int base = 10; char_u *p; @@ -15586,7 +16370,7 @@ static void f_str2nr(typval_T *argvars, typval_T *rettv) /* * "strftime({format}[, {time}])" function */ -static void f_strftime(typval_T *argvars, typval_T *rettv) +static void f_strftime(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u result_buf[256]; time_t seconds; @@ -15634,10 +16418,43 @@ static void f_strftime(typval_T *argvars, typval_T *rettv) } } +// "strgetchar()" function +static void f_strgetchar(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + char_u *str; + int len; + int error = false; + int charidx; + + rettv->vval.v_number = -1; + str = get_tv_string_chk(&argvars[0]); + if (str == NULL) { + return; + } + len = (int)STRLEN(str); + charidx = get_tv_number_chk(&argvars[1], &error); + if (error) { + return; + } + + { + int byteidx = 0; + + while (charidx >= 0 && byteidx < len) { + if (charidx == 0) { + rettv->vval.v_number = mb_ptr2char(str + byteidx); + break; + } + charidx--; + byteidx += mb_cptr2len(str + byteidx); + } + } +} + /* * "stridx()" function */ -static void f_stridx(typval_T *argvars, typval_T *rettv) +static void f_stridx(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u buf[NUMBUFLEN]; char_u *needle; @@ -15670,7 +16487,7 @@ static void f_stridx(typval_T *argvars, typval_T *rettv) /* * "string()" function */ -static void f_string(typval_T *argvars, typval_T *rettv) +static void f_string(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = (char_u *) encode_tv2string(&argvars[0], NULL); @@ -15679,7 +16496,7 @@ static void f_string(typval_T *argvars, typval_T *rettv) /* * "strlen()" function */ -static void f_strlen(typval_T *argvars, typval_T *rettv) +static void f_strlen(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = (varnumber_T)(STRLEN( get_tv_string(&argvars[0]))); @@ -15688,7 +16505,7 @@ static void f_strlen(typval_T *argvars, typval_T *rettv) /* * "strchars()" function */ -static void f_strchars(typval_T *argvars, typval_T *rettv) +static void f_strchars(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *s = get_tv_string(&argvars[0]); int skipcc = 0; @@ -15713,7 +16530,7 @@ static void f_strchars(typval_T *argvars, typval_T *rettv) /* * "strdisplaywidth()" function */ -static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv) +static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *s = get_tv_string(&argvars[0]); int col = 0; @@ -15727,17 +16544,75 @@ static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv) /* * "strwidth()" function */ -static void f_strwidth(typval_T *argvars, typval_T *rettv) +static void f_strwidth(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *s = get_tv_string(&argvars[0]); rettv->vval.v_number = (varnumber_T) mb_string2cells(s); } +// "strcharpart()" function +static void f_strcharpart(typval_T *argvars, typval_T *rettv, FunPtr fptr) { + char_u *p; + int nchar; + int nbyte = 0; + int charlen; + int len = 0; + int slen; + int error = false; + + p = get_tv_string(&argvars[0]); + slen = (int)STRLEN(p); + + nchar = get_tv_number_chk(&argvars[1], &error); + if (!error) { + if (nchar > 0) { + while (nchar > 0 && nbyte < slen) { + nbyte += mb_cptr2len(p + nbyte); + nchar--; + } + } else { + nbyte = nchar; + } + } + if (argvars[2].v_type != VAR_UNKNOWN) { + charlen = get_tv_number(&argvars[2]); + while (charlen > 0 && nbyte + len < slen) { + int off = nbyte + len; + + if (off < 0) { + len += 1; + } else { + len += mb_cptr2len(p + off); + } + charlen--; + } + } else { + len = slen - nbyte; // default: all bytes that are available. + } + + // Only return the overlap between the specified part and the actual + // string. + if (nbyte < 0) { + len += nbyte; + nbyte = 0; + } else if (nbyte > slen) { + nbyte = slen; + } + if (len < 0) { + len = 0; + } else if (nbyte + len > slen) { + len = slen - nbyte; + } + + rettv->v_type = VAR_STRING; + rettv->vval.v_string = vim_strnsave(p + nbyte, len); +} + /* * "strpart()" function */ -static void f_strpart(typval_T *argvars, typval_T *rettv) +static void f_strpart(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p; int n; @@ -15777,7 +16652,7 @@ static void f_strpart(typval_T *argvars, typval_T *rettv) /* * "strridx()" function */ -static void f_strridx(typval_T *argvars, typval_T *rettv) +static void f_strridx(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u buf[NUMBUFLEN]; char_u *needle; @@ -15823,7 +16698,7 @@ static void f_strridx(typval_T *argvars, typval_T *rettv) /* * "strtrans()" function */ -static void f_strtrans(typval_T *argvars, typval_T *rettv) +static void f_strtrans(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = transstr(get_tv_string(&argvars[0])); @@ -15832,7 +16707,7 @@ static void f_strtrans(typval_T *argvars, typval_T *rettv) /* * "submatch()" function */ -static void f_submatch(typval_T *argvars, typval_T *rettv) +static void f_submatch(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int error = FALSE; int no = (int)get_tv_number_chk(&argvars[0], &error); @@ -15861,7 +16736,7 @@ static void f_submatch(typval_T *argvars, typval_T *rettv) /* * "substitute()" function */ -static void f_substitute(typval_T *argvars, typval_T *rettv) +static void f_substitute(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u patbuf[NUMBUFLEN]; char_u subbuf[NUMBUFLEN]; @@ -15882,7 +16757,7 @@ static void f_substitute(typval_T *argvars, typval_T *rettv) /* * "synID(lnum, col, trans)" function */ -static void f_synID(typval_T *argvars, typval_T *rettv) +static void f_synID(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int id = 0; long lnum; @@ -15904,7 +16779,7 @@ static void f_synID(typval_T *argvars, typval_T *rettv) /* * "synIDattr(id, what [, mode])" function */ -static void f_synIDattr(typval_T *argvars, typval_T *rettv) +static void f_synIDattr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p = NULL; int id; @@ -15980,7 +16855,7 @@ static void f_synIDattr(typval_T *argvars, typval_T *rettv) /* * "synIDtrans(id)" function */ -static void f_synIDtrans(typval_T *argvars, typval_T *rettv) +static void f_synIDtrans(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int id; @@ -15997,7 +16872,7 @@ static void f_synIDtrans(typval_T *argvars, typval_T *rettv) /* * "synconcealed(lnum, col)" function */ -static void f_synconcealed(typval_T *argvars, typval_T *rettv) +static void f_synconcealed(typval_T *argvars, typval_T *rettv, FunPtr fptr) { long lnum; long col; @@ -16044,7 +16919,7 @@ static void f_synconcealed(typval_T *argvars, typval_T *rettv) /* * "synstack(lnum, col)" function */ -static void f_synstack(typval_T *argvars, typval_T *rettv) +static void f_synstack(typval_T *argvars, typval_T *rettv, FunPtr fptr) { long lnum; long col; @@ -16118,8 +16993,12 @@ static void get_system_output_as_rettv(typval_T *argvars, typval_T *rettv, } // get shell command to execute - char **argv = tv_to_argv(&argvars[0], NULL); + bool executable = true; + char **argv = tv_to_argv(&argvars[0], NULL, &executable); if (!argv) { + if (!executable) { + set_vim_var_nr(VV_SHELL_ERROR, (long)-1); + } xfree(input); return; // Already did emsg. } @@ -16175,12 +17054,12 @@ static void get_system_output_as_rettv(typval_T *argvars, typval_T *rettv, } /// f_system - the VimL system() function -static void f_system(typval_T *argvars, typval_T *rettv) +static void f_system(typval_T *argvars, typval_T *rettv, FunPtr fptr) { get_system_output_as_rettv(argvars, rettv, false); } -static void f_systemlist(typval_T *argvars, typval_T *rettv) +static void f_systemlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { get_system_output_as_rettv(argvars, rettv, true); } @@ -16189,7 +17068,7 @@ static void f_systemlist(typval_T *argvars, typval_T *rettv) /* * "tabpagebuflist()" function */ -static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv) +static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { tabpage_T *tp; win_T *wp = NULL; @@ -16214,7 +17093,7 @@ static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv) /* * "tabpagenr()" function */ -static void f_tabpagenr(typval_T *argvars, typval_T *rettv) +static void f_tabpagenr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int nr = 1; char_u *arg; @@ -16278,7 +17157,7 @@ static int get_winnr(tabpage_T *tp, typval_T *argvar) /* * "tabpagewinnr()" function */ -static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv) +static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int nr = 1; tabpage_T *tp; @@ -16295,7 +17174,7 @@ static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv) /* * "tagfiles()" function */ -static void f_tagfiles(typval_T *argvars, typval_T *rettv) +static void f_tagfiles(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *fname; tagname_T tn; @@ -16316,7 +17195,7 @@ static void f_tagfiles(typval_T *argvars, typval_T *rettv) /* * "taglist()" function */ -static void f_taglist(typval_T *argvars, typval_T *rettv) +static void f_taglist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *tag_pattern; @@ -16332,14 +17211,14 @@ static void f_taglist(typval_T *argvars, typval_T *rettv) /* * "tempname()" function */ -static void f_tempname(typval_T *argvars, typval_T *rettv) +static void f_tempname(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = vim_tempname(); } // "termopen(cmd[, cwd])" function -static void f_termopen(typval_T *argvars, typval_T *rettv) +static void f_termopen(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (check_restricted() || check_secure()) { return; @@ -16351,8 +17230,10 @@ static void f_termopen(typval_T *argvars, typval_T *rettv) } char *cmd; - char **argv = tv_to_argv(&argvars[0], &cmd); + bool executable = true; + char **argv = tv_to_argv(&argvars[0], &cmd, &executable); if (!argv) { + rettv->vval.v_number = executable ? 0 : -1; return; // Did error message in tv_to_argv. } @@ -16363,10 +17244,24 @@ static void f_termopen(typval_T *argvars, typval_T *rettv) return; } - ufunc_T *on_stdout = NULL, *on_stderr = NULL, *on_exit = NULL; + Callback on_stdout = CALLBACK_NONE, on_stderr = CALLBACK_NONE, + on_exit = CALLBACK_NONE; dict_T *job_opts = NULL; + char *cwd = "."; if (argvars[1].v_type == VAR_DICT) { job_opts = argvars[1].vval.v_dict; + + char *new_cwd = (char *)get_dict_string(job_opts, "cwd", false); + if (new_cwd && strlen(new_cwd) > 0) { + cwd = new_cwd; + // The new cwd must be a directory. + if (!os_isdir((char_u *)cwd)) { + EMSG2(_(e_invarg2), "expected valid directory"); + shell_free_argv(argv); + return; + } + } + if (!common_job_callbacks(job_opts, &on_stdout, &on_stderr, &on_exit)) { shell_free_argv(argv); return; @@ -16374,7 +17269,7 @@ static void f_termopen(typval_T *argvars, typval_T *rettv) } TerminalJobData *data = common_job_init(argv, on_stdout, on_stderr, on_exit, - job_opts, true, false); + true, false, false, cwd); data->proc.pty.width = curwin->w_width; data->proc.pty.height = curwin->w_height; data->proc.pty.term_name = xstrdup("xterm-256color"); @@ -16389,11 +17284,6 @@ static void f_termopen(typval_T *argvars, typval_T *rettv) topts.resize_cb = term_resize; topts.close_cb = term_close; - char *cwd = "."; - if (argvars[1].v_type == VAR_STRING - && os_isdir(argvars[1].vval.v_string)) { - cwd = (char *)argvars[1].vval.v_string; - } int pid = data->proc.pty.process.pid; char buf[1024]; @@ -16406,9 +17296,9 @@ static void f_termopen(typval_T *argvars, typval_T *rettv) // Save the job id and pid in b:terminal_job_{id,pid} Error err; dict_set_value(curbuf->b_vars, cstr_as_string("terminal_job_id"), - INTEGER_OBJ(rettv->vval.v_number), false, &err); + INTEGER_OBJ(rettv->vval.v_number), false, false, &err); dict_set_value(curbuf->b_vars, cstr_as_string("terminal_job_pid"), - INTEGER_OBJ(pid), false, &err); + INTEGER_OBJ(pid), false, false, &err); Terminal *term = terminal_open(topts); data->term = term; @@ -16417,34 +17307,264 @@ static void f_termopen(typval_T *argvars, typval_T *rettv) return; } -/* - * "test(list)" function: Just checking the walls... - */ -static void f_test(typval_T *argvars, typval_T *rettv) +static bool callback_from_typval(Callback *callback, typval_T *arg) { - /* Used for unit testing. Change the code below to your liking. */ + if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL) { + callback->data.partial = arg->vval.v_partial; + callback->data.partial->pt_refcount++; + callback->type = kCallbackPartial; + } else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING) { + char_u *name = arg->vval.v_string; + func_ref(name); + callback->data.funcref = vim_strsave(name); + callback->type = kCallbackFuncref; + } else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0) { + callback->type = kCallbackNone; + } else { + EMSG(_("E921: Invalid callback argument")); + return false; + } + return true; } -/* - * "tan()" function - */ -static void f_tan(typval_T *argvars, typval_T *rettv) + +/// Unref/free callback +static void callback_free(Callback *callback) { - float_op_wrapper(argvars, rettv, &tan); + switch (callback->type) { + case kCallbackFuncref: + func_unref(callback->data.funcref); + xfree(callback->data.funcref); + break; + + case kCallbackPartial: + partial_unref(callback->data.partial); + break; + + case kCallbackNone: + break; + + default: + abort(); + } + callback->type = kCallbackNone; } -/* - * "tanh()" function - */ -static void f_tanh(typval_T *argvars, typval_T *rettv) +static bool callback_equal(Callback *cb1, Callback *cb2) +{ + if (cb1->type != cb2->type) { + return false; + } + switch (cb1->type) { + case kCallbackFuncref: + return STRCMP(cb1->data.funcref, cb2->data.funcref) == 0; + + case kCallbackPartial: + // FIXME: this is inconsistent with tv_equal but is needed for precision + // maybe change dictwatcheradd to return a watcher id instead? + return cb1->data.partial == cb2->data.partial; + + case kCallbackNone: + return true; + + default: + abort(); + } +} + +static bool callback_call(Callback *callback, int argcount_in, + typval_T *argvars_in, typval_T *rettv) +{ + partial_T *partial; + char_u *name; + switch (callback->type) { + case kCallbackFuncref: + name = callback->data.funcref; + partial = NULL; + break; + + case kCallbackPartial: + partial = callback->data.partial; + name = partial->pt_name; + break; + + case kCallbackNone: + return false; + break; + + default: + abort(); + } + + int dummy; + return call_func(name, (int)STRLEN(name), rettv, argcount_in, argvars_in, + curwin->w_cursor.lnum, curwin->w_cursor.lnum, &dummy, + true, partial, NULL); +} + +static bool set_ref_in_callback(Callback *callback, int copyID, + ht_stack_T **ht_stack, + list_stack_T **list_stack) +{ + typval_T tv; + switch (callback->type) { + case kCallbackFuncref: + case kCallbackNone: + break; + + case kCallbackPartial: + tv.v_type = VAR_PARTIAL; + tv.vval.v_partial = callback->data.partial; + return set_ref_in_item(&tv, copyID, ht_stack, list_stack); + break; + + + default: + abort(); + } + return false; +} + + +/// "timer_start(timeout, callback, opts)" function +static void f_timer_start(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - float_op_wrapper(argvars, rettv, &tanh); + long timeout = get_tv_number(&argvars[0]); + timer_T *timer; + int repeat = 1; + dict_T *dict; + + rettv->vval.v_number = -1; + + if (argvars[2].v_type != VAR_UNKNOWN) { + if (argvars[2].v_type != VAR_DICT + || (dict = argvars[2].vval.v_dict) == NULL) { + EMSG2(_(e_invarg2), get_tv_string(&argvars[2])); + return; + } + if (dict_find(dict, (char_u *)"repeat", -1) != NULL) { + repeat = get_dict_number(dict, "repeat"); + if (repeat == 0) { + repeat = 1; + } + } + } + + Callback callback; + if (!callback_from_typval(&callback, &argvars[1])) { + return; + } + + timer = xmalloc(sizeof *timer); + timer->refcount = 1; + timer->stopped = false; + timer->repeat_count = repeat; + timer->timeout = timeout; + timer->timer_id = last_timer_id++; + timer->callback = callback; + + time_watcher_init(&main_loop, &timer->tw, timer); + timer->tw.events = multiqueue_new_child(main_loop.events); + // if main loop is blocked, don't queue up multiple events + timer->tw.blockable = true; + time_watcher_start(&timer->tw, timer_due_cb, timeout, + timeout * (repeat != 1)); + + pmap_put(uint64_t)(timers, timer->timer_id, timer); + rettv->vval.v_number = timer->timer_id; +} + + +// "timer_stop(timerid)" function +static void f_timer_stop(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + if (argvars[0].v_type != VAR_NUMBER) { + EMSG(_(e_number_exp)); + return; + } + + timer_T *timer = pmap_get(uint64_t)(timers, get_tv_number(&argvars[0])); + + if (timer == NULL) { + return; + } + + timer_stop(timer); +} + +// invoked on the main loop +static void timer_due_cb(TimeWatcher *tw, void *data) +{ + timer_T *timer = (timer_T *)data; + if (timer->stopped) { + return; + } + timer->refcount++; + // if repeat was negative repeat forever + if (timer->repeat_count >= 0 && --timer->repeat_count == 0) { + timer_stop(timer); + } + + typval_T argv[2]; + init_tv(argv); + argv[0].v_type = VAR_NUMBER; + argv[0].vval.v_number = timer->timer_id; + typval_T rettv; + + init_tv(&rettv); + callback_call(&timer->callback, 1, argv, &rettv); + clear_tv(&rettv); + + if (!timer->stopped && timer->timeout == 0) { + // special case: timeout=0 means the callback will be + // invoked again on the next event loop tick. + // we don't use uv_idle_t to not spin the event loop + // when the main loop is blocked. + time_watcher_start(&timer->tw, timer_due_cb, 0, 0); + } + timer_decref(timer); +} + +static void timer_stop(timer_T *timer) +{ + if (timer->stopped) { + // avoid double free + return; + } + timer->stopped = true; + time_watcher_stop(&timer->tw); + time_watcher_close(&timer->tw, timer_close_cb); +} + +// invoked on next event loop tick, so queue is empty +static void timer_close_cb(TimeWatcher *tw, void *data) +{ + timer_T *timer = (timer_T *)data; + multiqueue_free(timer->tw.events); + callback_free(&timer->callback); + pmap_del(uint64_t)(timers, timer->timer_id); + timer_decref(timer); +} + +static void timer_decref(timer_T *timer) +{ + if (--timer->refcount == 0) { + xfree(timer); + } +} + +void timer_teardown(void) +{ + timer_T *timer; + map_foreach_value(timers, timer, { + timer_stop(timer); + }) } /* * "tolower(string)" function */ -static void f_tolower(typval_T *argvars, typval_T *rettv) +static void f_tolower(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *p = vim_strsave(get_tv_string(&argvars[0])); rettv->v_type = VAR_STRING; @@ -16475,7 +17595,7 @@ static void f_tolower(typval_T *argvars, typval_T *rettv) /* * "toupper(string)" function */ -static void f_toupper(typval_T *argvars, typval_T *rettv) +static void f_toupper(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = strup_save(get_tv_string(&argvars[0])); @@ -16484,7 +17604,7 @@ static void f_toupper(typval_T *argvars, typval_T *rettv) /* * "tr(string, fromstr, tostr)" function */ -static void f_tr(typval_T *argvars, typval_T *rettv) +static void f_tr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u *in_str; char_u *fromstr; @@ -16582,32 +17702,25 @@ error: } /* - * "trunc({float})" function - */ -static void f_trunc(typval_T *argvars, typval_T *rettv) -{ - float_op_wrapper(argvars, rettv, &trunc); -} - -/* * "type(expr)" function */ -static void f_type(typval_T *argvars, typval_T *rettv) +static void f_type(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int n = -1; switch (argvars[0].v_type) { - case VAR_NUMBER: n = 0; break; - case VAR_STRING: n = 1; break; - case VAR_FUNC: n = 2; break; - case VAR_LIST: n = 3; break; - case VAR_DICT: n = 4; break; - case VAR_FLOAT: n = 5; break; + case VAR_NUMBER: n = VAR_TYPE_NUMBER; break; + case VAR_STRING: n = VAR_TYPE_STRING; break; + case VAR_PARTIAL: + case VAR_FUNC: n = VAR_TYPE_FUNC; break; + case VAR_LIST: n = VAR_TYPE_LIST; break; + case VAR_DICT: n = VAR_TYPE_DICT; break; + case VAR_FLOAT: n = VAR_TYPE_FLOAT; break; case VAR_SPECIAL: { switch (argvars[0].vval.v_special) { case kSpecialVarTrue: case kSpecialVarFalse: { - n = 6; + n = VAR_TYPE_BOOL; break; } case kSpecialVarNull: { @@ -16628,7 +17741,7 @@ static void f_type(typval_T *argvars, typval_T *rettv) /* * "undofile(name)" function */ -static void f_undofile(typval_T *argvars, typval_T *rettv) +static void f_undofile(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; { @@ -16651,7 +17764,7 @@ static void f_undofile(typval_T *argvars, typval_T *rettv) /* * "undotree()" function */ -static void f_undotree(typval_T *argvars, typval_T *rettv) +static void f_undotree(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv_dict_alloc(rettv); @@ -16674,7 +17787,7 @@ static void f_undotree(typval_T *argvars, typval_T *rettv) /* * "values(dict)" function */ -static void f_values(typval_T *argvars, typval_T *rettv) +static void f_values(typval_T *argvars, typval_T *rettv, FunPtr fptr) { dict_list(argvars, rettv, 1); } @@ -16682,7 +17795,7 @@ static void f_values(typval_T *argvars, typval_T *rettv) /* * "virtcol(string)" function */ -static void f_virtcol(typval_T *argvars, typval_T *rettv) +static void f_virtcol(typval_T *argvars, typval_T *rettv, FunPtr fptr) { colnr_T vcol = 0; pos_T *fp; @@ -16701,7 +17814,7 @@ static void f_virtcol(typval_T *argvars, typval_T *rettv) /* * "visualmode()" function */ -static void f_visualmode(typval_T *argvars, typval_T *rettv) +static void f_visualmode(typval_T *argvars, typval_T *rettv, FunPtr fptr) { char_u str[2]; @@ -16718,16 +17831,48 @@ static void f_visualmode(typval_T *argvars, typval_T *rettv) /* * "wildmenumode()" function */ -static void f_wildmenumode(typval_T *argvars, typval_T *rettv) +static void f_wildmenumode(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (wild_menu_showing) rettv->vval.v_number = 1; } +/// "win_findbuf()" function +static void f_win_findbuf(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv_list_alloc(rettv); + win_findbuf(argvars, rettv->vval.v_list); +} + +/// "win_getid()" function +static void f_win_getid(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->vval.v_number = win_getid(argvars); +} + +/// "win_gotoid()" function +static void f_win_gotoid(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->vval.v_number = win_gotoid(argvars); +} + +/// "win_id2tabwin()" function +static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv_list_alloc(rettv); + win_id2tabwin(argvars, rettv->vval.v_list); +} + +/// "win_id2win()" function +static void f_win_id2win(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->vval.v_number = win_id2win(argvars); +} + /* * "winbufnr(nr)" function */ -static void f_winbufnr(typval_T *argvars, typval_T *rettv) +static void f_winbufnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { win_T *wp; @@ -16741,7 +17886,7 @@ static void f_winbufnr(typval_T *argvars, typval_T *rettv) /* * "wincol()" function */ -static void f_wincol(typval_T *argvars, typval_T *rettv) +static void f_wincol(typval_T *argvars, typval_T *rettv, FunPtr fptr) { validate_cursor(); rettv->vval.v_number = curwin->w_wcol + 1; @@ -16750,7 +17895,7 @@ static void f_wincol(typval_T *argvars, typval_T *rettv) /* * "winheight(nr)" function */ -static void f_winheight(typval_T *argvars, typval_T *rettv) +static void f_winheight(typval_T *argvars, typval_T *rettv, FunPtr fptr) { win_T *wp; @@ -16764,7 +17909,7 @@ static void f_winheight(typval_T *argvars, typval_T *rettv) /* * "winline()" function */ -static void f_winline(typval_T *argvars, typval_T *rettv) +static void f_winline(typval_T *argvars, typval_T *rettv, FunPtr fptr) { validate_cursor(); rettv->vval.v_number = curwin->w_wrow + 1; @@ -16773,7 +17918,7 @@ static void f_winline(typval_T *argvars, typval_T *rettv) /* * "winnr()" function */ -static void f_winnr(typval_T *argvars, typval_T *rettv) +static void f_winnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int nr = 1; @@ -16784,7 +17929,7 @@ static void f_winnr(typval_T *argvars, typval_T *rettv) /* * "winrestcmd()" function */ -static void f_winrestcmd(typval_T *argvars, typval_T *rettv) +static void f_winrestcmd(typval_T *argvars, typval_T *rettv, FunPtr fptr) { int winnr = 1; garray_T ga; @@ -16807,7 +17952,7 @@ static void f_winrestcmd(typval_T *argvars, typval_T *rettv) /* * "winrestview()" function */ -static void f_winrestview(typval_T *argvars, typval_T *rettv) +static void f_winrestview(typval_T *argvars, typval_T *rettv, FunPtr fptr) { dict_T *dict; @@ -16816,29 +17961,29 @@ static void f_winrestview(typval_T *argvars, typval_T *rettv) EMSG(_(e_invarg)); else { if (dict_find(dict, (char_u *)"lnum", -1) != NULL) { - curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum"); + curwin->w_cursor.lnum = get_dict_number(dict, "lnum"); } if (dict_find(dict, (char_u *)"col", -1) != NULL) { - curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col"); + curwin->w_cursor.col = get_dict_number(dict, "col"); } if (dict_find(dict, (char_u *)"coladd", -1) != NULL) { - curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd"); + curwin->w_cursor.coladd = get_dict_number(dict, "coladd"); } if (dict_find(dict, (char_u *)"curswant", -1) != NULL) { - curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant"); - curwin->w_set_curswant = FALSE; + curwin->w_curswant = get_dict_number(dict, "curswant"); + curwin->w_set_curswant = false; } if (dict_find(dict, (char_u *)"topline", -1) != NULL) { - set_topline(curwin, get_dict_number(dict, (char_u *)"topline")); + set_topline(curwin, get_dict_number(dict, "topline")); } if (dict_find(dict, (char_u *)"topfill", -1) != NULL) { - curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill"); + curwin->w_topfill = get_dict_number(dict, "topfill"); } if (dict_find(dict, (char_u *)"leftcol", -1) != NULL) { - curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol"); + curwin->w_leftcol = get_dict_number(dict, "leftcol"); } if (dict_find(dict, (char_u *)"skipcol", -1) != NULL) { - curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol"); + curwin->w_skipcol = get_dict_number(dict, "skipcol"); } check_cursor(); @@ -16857,7 +18002,7 @@ static void f_winrestview(typval_T *argvars, typval_T *rettv) /* * "winsaveview()" function */ -static void f_winsaveview(typval_T *argvars, typval_T *rettv) +static void f_winsaveview(typval_T *argvars, typval_T *rettv, FunPtr fptr) { dict_T *dict; @@ -16961,7 +18106,7 @@ static char_u *save_tv_as_string(typval_T *tv, ssize_t *len, bool endnl) /* * "winwidth(nr)" function */ -static void f_winwidth(typval_T *argvars, typval_T *rettv) +static void f_winwidth(typval_T *argvars, typval_T *rettv, FunPtr fptr) { win_T *wp; @@ -16973,14 +18118,14 @@ static void f_winwidth(typval_T *argvars, typval_T *rettv) } /// "wordcount()" function -static void f_wordcount(typval_T *argvars, typval_T *rettv) +static void f_wordcount(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv_dict_alloc(rettv); cursor_pos_info(rettv->vval.v_dict); } /// "writefile()" function -static void f_writefile(typval_T *argvars, typval_T *rettv) +static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = 0; // Assuming success. @@ -17025,7 +18170,7 @@ static void f_writefile(typval_T *argvars, typval_T *rettv) /* * "xor(expr, expr)" function */ -static void f_xor(typval_T *argvars, typval_T *rettv) +static void f_xor(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL) ^ get_tv_number_chk(&argvars[1], NULL); @@ -17519,6 +18664,8 @@ void set_vcount(long count, long count1, int set_prevcount) /// @param[in] val Value to set to. void set_vim_var_nr(const VimVarIndex idx, const varnumber_T val) { + clear_tv(&vimvars[idx].vv_tv); + vimvars[idx].vv_type = VAR_NUMBER; vimvars[idx].vv_nr = val; } @@ -17528,6 +18675,8 @@ void set_vim_var_nr(const VimVarIndex idx, const varnumber_T val) /// @param[in] val Value to set to. void set_vim_var_special(const VimVarIndex idx, const SpecialVarValue val) { + clear_tv(&vimvars[idx].vv_tv); + vimvars[idx].vv_type = VAR_SPECIAL; vimvars[idx].vv_special = val; } @@ -17540,7 +18689,8 @@ void set_vim_var_special(const VimVarIndex idx, const SpecialVarValue val) void set_vim_var_string(const VimVarIndex idx, const char *const val, const ptrdiff_t len) { - xfree(vimvars[idx].vv_str); + clear_tv(&vimvars[idx].vv_di.di_tv); + vimvars[idx].vv_type = VAR_STRING; if (val == NULL) { vimvars[idx].vv_str = NULL; } else if (len == -1) { @@ -17556,7 +18706,8 @@ void set_vim_var_string(const VimVarIndex idx, const char *const val, /// @param[in,out] val Value to set to. Reference count will be incremented. void set_vim_var_list(const VimVarIndex idx, list_T *const val) { - list_unref(vimvars[idx].vv_list); + clear_tv(&vimvars[idx].vv_di.di_tv); + vimvars[idx].vv_type = VAR_LIST; vimvars[idx].vv_list = val; if (val != NULL) { val->lv_refcount++; @@ -17570,7 +18721,8 @@ void set_vim_var_list(const VimVarIndex idx, list_T *const val) /// Also keys of the dictionary will be made read-only. void set_vim_var_dict(const VimVarIndex idx, dict_T *const val) { - dict_unref(vimvars[idx].vv_dict); + clear_tv(&vimvars[idx].vv_di.di_tv); + vimvars[idx].vv_type = VAR_DICT; vimvars[idx].vv_dict = val; if (val != NULL) { @@ -17769,21 +18921,29 @@ handle_subscript ( while (ret == OK && (**arg == '[' || (**arg == '.' && rettv->v_type == VAR_DICT) - || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC))) + || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC + || rettv->v_type == VAR_PARTIAL))) && !ascii_iswhite(*(*arg - 1))) { if (**arg == '(') { - /* need to copy the funcref so that we can clear rettv */ + partial_T *pt = NULL; + // need to copy the funcref so that we can clear rettv if (evaluate) { functv = *rettv; rettv->v_type = VAR_UNKNOWN; - /* Invoke the function. Recursive! */ - s = functv.vval.v_string; - } else + // Invoke the function. Recursive! + if (functv.v_type == VAR_PARTIAL) { + pt = functv.vval.v_partial; + s = pt->pt_name; + } else { + s = functv.vval.v_string; + } + } else { s = (char_u *)""; + } ret = get_func_tv(s, (int)STRLEN(s), rettv, arg, - curwin->w_cursor.lnum, curwin->w_cursor.lnum, - &len, evaluate, selfdict); + curwin->w_cursor.lnum, curwin->w_cursor.lnum, + &len, evaluate, pt, selfdict); /* Clear the funcref afterwards, so that deleting it while * evaluating the arguments is possible (see test55). */ @@ -17814,10 +18974,82 @@ handle_subscript ( } } } + + // Turn "dict.Func" into a partial for "Func" bound to "dict". + if (selfdict != NULL + && (rettv->v_type == VAR_FUNC + || rettv->v_type == VAR_PARTIAL)) { + set_selfdict(rettv, selfdict); + } + dict_unref(selfdict); return ret; } +static void set_selfdict(typval_T *rettv, dict_T *selfdict) +{ + // Don't do this when "dict.Func" is already a partial that was bound + // explicitly (pt_auto is false). + if (rettv->v_type == VAR_PARTIAL && !rettv->vval.v_partial->pt_auto + && rettv->vval.v_partial->pt_dict != NULL) { + return; + } + char_u *fname = rettv->v_type == VAR_FUNC || rettv->v_type == VAR_STRING + ? rettv->vval.v_string + : rettv->vval.v_partial->pt_name; + char_u *tofree = NULL; + ufunc_T *fp; + char_u fname_buf[FLEN_FIXED + 1]; + int error; + + // Translate "s:func" to the stored function name. + fname = fname_trans_sid(fname, fname_buf, &tofree, &error); + + fp = find_func(fname); + xfree(tofree); + + // Turn "dict.Func" into a partial for "Func" with "dict". + if (fp != NULL && (fp->uf_flags & FC_DICT)) { + partial_T *pt = (partial_T *)xcalloc(1, sizeof(partial_T)); + + if (pt != NULL) { + pt->pt_refcount = 1; + pt->pt_dict = selfdict; + (selfdict->dv_refcount)++; + pt->pt_auto = true; + if (rettv->v_type == VAR_FUNC || rettv->v_type == VAR_STRING) { + // Just a function: Take over the function name and use selfdict. + pt->pt_name = rettv->vval.v_string; + } else { + partial_T *ret_pt = rettv->vval.v_partial; + int i; + + // Partial: copy the function name, use selfdict and copy + // args. Can't take over name or args, the partial might + // be referenced elsewhere. + pt->pt_name = vim_strsave(ret_pt->pt_name); + func_ref(pt->pt_name); + if (ret_pt->pt_argc > 0) { + size_t arg_size = sizeof(typval_T) * ret_pt->pt_argc; + pt->pt_argv = (typval_T *)xmalloc(arg_size); + if (pt->pt_argv == NULL) { + // out of memory: drop the arguments + pt->pt_argc = 0; + } else { + pt->pt_argc = ret_pt->pt_argc; + for (i = 0; i < pt->pt_argc; i++) { + copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]); + } + } + } + partial_unref(ret_pt); + } + rettv->v_type = VAR_PARTIAL; + rettv->vval.v_partial = pt; + } + } +} + /* * Free the memory for a variable type-value. */ @@ -17831,6 +19063,9 @@ void free_tv(typval_T *varp) case VAR_STRING: xfree(varp->vval.v_string); break; + case VAR_PARTIAL: + partial_unref(varp->vval.v_partial); + break; case VAR_LIST: list_unref(varp->vval.v_list); break; @@ -17847,45 +19082,244 @@ void free_tv(typval_T *varp) } } -/* - * Free the memory for a variable value and set the value to NULL or 0. - */ -void clear_tv(typval_T *varp) +#define TYPVAL_ENCODE_ALLOW_SPECIALS false + +#define TYPVAL_ENCODE_CONV_NIL(tv) \ + do { \ + tv->vval.v_special = kSpecialVarFalse; \ + tv->v_lock = VAR_UNLOCKED; \ + } while (0) + +#define TYPVAL_ENCODE_CONV_BOOL(tv, num) \ + TYPVAL_ENCODE_CONV_NIL(tv) + +#define TYPVAL_ENCODE_CONV_NUMBER(tv, num) \ + do { \ + (void)num; \ + tv->vval.v_number = 0; \ + tv->v_lock = VAR_UNLOCKED; \ + } while (0) + +#define TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER(tv, num) + +#define TYPVAL_ENCODE_CONV_FLOAT(tv, flt) \ + do { \ + tv->vval.v_float = 0; \ + tv->v_lock = VAR_UNLOCKED; \ + } while (0) + +#define TYPVAL_ENCODE_CONV_STRING(tv, buf, len) \ + do { \ + xfree(buf); \ + tv->vval.v_string = NULL; \ + tv->v_lock = VAR_UNLOCKED; \ + } while (0) + +#define TYPVAL_ENCODE_CONV_STR_STRING(tv, buf, len) + +#define TYPVAL_ENCODE_CONV_EXT_STRING(tv, buf, len, type) + +static inline int _nothing_conv_func_start(typval_T *const tv, + char_u *const fun) + FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_NONNULL_ARG(1) { - if (varp != NULL) { - switch (varp->v_type) { - case VAR_FUNC: - func_unref(varp->vval.v_string); - if (varp->vval.v_string != empty_string) { - xfree(varp->vval.v_string); - } - varp->vval.v_string = NULL; - break; - case VAR_STRING: - xfree(varp->vval.v_string); - varp->vval.v_string = NULL; - break; - case VAR_LIST: - list_unref(varp->vval.v_list); - varp->vval.v_list = NULL; - break; - case VAR_DICT: - dict_unref(varp->vval.v_dict); - varp->vval.v_dict = NULL; - break; - case VAR_NUMBER: - varp->vval.v_number = 0; - break; - case VAR_FLOAT: - varp->vval.v_float = 0.0; - break; - case VAR_SPECIAL: - varp->vval.v_special = kSpecialVarFalse; - break; - case VAR_UNKNOWN: - break; + tv->v_lock = VAR_UNLOCKED; + if (tv->v_type == VAR_PARTIAL) { + partial_T *const pt_ = tv->vval.v_partial; + if (pt_ != NULL && pt_->pt_refcount > 1) { + pt_->pt_refcount--; + tv->vval.v_partial = NULL; + return OK; + } + } else { + func_unref(fun); + if (fun != empty_string) { + xfree(fun); } - varp->v_lock = 0; + tv->vval.v_string = NULL; + } + return NOTDONE; +} +#define TYPVAL_ENCODE_CONV_FUNC_START(tv, fun) \ + do { \ + if (_nothing_conv_func_start(tv, fun) != NOTDONE) { \ + return OK; \ + } \ + } while (0) + +#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS(tv, len) +#define TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF(tv, len) + +static inline void _nothing_conv_func_end(typval_T *const tv, const int copyID) + FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_NONNULL_ALL +{ + if (tv->v_type == VAR_PARTIAL) { + partial_T *const pt = tv->vval.v_partial; + if (pt == NULL) { + return; + } + // Dictionary should already be freed by the time. + // If it was not freed then it is a part of the reference cycle. + assert(pt->pt_dict == NULL || pt->pt_dict->dv_copyID == copyID); + pt->pt_dict = NULL; + // As well as all arguments. + pt->pt_argc = 0; + assert(pt->pt_refcount <= 1); + partial_unref(pt); + tv->vval.v_partial = NULL; + assert(tv->v_lock == VAR_UNLOCKED); + } +} +#define TYPVAL_ENCODE_CONV_FUNC_END(tv) _nothing_conv_func_end(tv, copyID) + +#define TYPVAL_ENCODE_CONV_EMPTY_LIST(tv) \ + do { \ + list_unref(tv->vval.v_list); \ + tv->vval.v_list = NULL; \ + tv->v_lock = VAR_UNLOCKED; \ + } while (0) + +#define TYPVAL_ENCODE_CONV_EMPTY_DICT(tv, dict) \ + do { \ + assert((void *)&dict != (void *)&TYPVAL_ENCODE_NODICT_VAR); \ + dict_unref((dict_T *)dict); \ + *((dict_T **)&dict) = NULL; \ + if (tv != NULL) { \ + ((typval_T *)tv)->v_lock = VAR_UNLOCKED; \ + } \ + } while (0) + +static inline int _nothing_conv_real_list_after_start( + typval_T *const tv, MPConvStackVal *const mpsv) + FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_WARN_UNUSED_RESULT +{ + assert(tv != NULL); + tv->v_lock = VAR_UNLOCKED; + if (tv->vval.v_list->lv_refcount > 1) { + tv->vval.v_list->lv_refcount--; + tv->vval.v_list = NULL; + mpsv->data.l.li = NULL; + return OK; + } + return NOTDONE; +} +#define TYPVAL_ENCODE_CONV_LIST_START(tv, len) + +#define TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START(tv, mpsv) \ + do { \ + if (_nothing_conv_real_list_after_start(tv, &mpsv) != NOTDONE) { \ + goto typval_encode_stop_converting_one_item; \ + } \ + } while (0) + +#define TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS(tv) + +static inline void _nothing_conv_list_end(typval_T *const tv) + FUNC_ATTR_ALWAYS_INLINE +{ + if (tv == NULL) { + return; + } + assert(tv->v_type == VAR_LIST); + list_T *const list = tv->vval.v_list; + list_unref(list); + tv->vval.v_list = NULL; +} +#define TYPVAL_ENCODE_CONV_LIST_END(tv) _nothing_conv_list_end(tv) + +static inline int _nothing_conv_real_dict_after_start( + typval_T *const tv, dict_T **const dictp, const void *const nodictvar, + MPConvStackVal *const mpsv) + FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_WARN_UNUSED_RESULT +{ + if (tv != NULL) { + tv->v_lock = VAR_UNLOCKED; + } + if ((const void *)dictp != nodictvar && (*dictp)->dv_refcount > 1) { + (*dictp)->dv_refcount--; + *dictp = NULL; + mpsv->data.d.todo = 0; + return OK; + } + return NOTDONE; +} +#define TYPVAL_ENCODE_CONV_DICT_START(tv, dict, len) + +#define TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START(tv, dict, mpsv) \ + do { \ + if (_nothing_conv_real_dict_after_start( \ + tv, (dict_T **)&dict, (void *)&TYPVAL_ENCODE_NODICT_VAR, \ + &mpsv) != NOTDONE) { \ + goto typval_encode_stop_converting_one_item; \ + } \ + } while (0) + +#define TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK(tv, dict) +#define TYPVAL_ENCODE_CONV_DICT_AFTER_KEY(tv, dict) +#define TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS(tv, dict) + +static inline void _nothing_conv_dict_end(typval_T *const tv, + dict_T **const dictp, + const void *const nodictvar) + FUNC_ATTR_ALWAYS_INLINE +{ + if ((const void *)dictp != nodictvar) { + dict_unref(*dictp); + *dictp = NULL; + } +} +#define TYPVAL_ENCODE_CONV_DICT_END(tv, dict) \ + _nothing_conv_dict_end(tv, (dict_T **)&dict, \ + (void *)&TYPVAL_ENCODE_NODICT_VAR) + +#define TYPVAL_ENCODE_CONV_RECURSE(val, conv_type) + +#define TYPVAL_ENCODE_SCOPE static +#define TYPVAL_ENCODE_NAME nothing +#define TYPVAL_ENCODE_FIRST_ARG_TYPE const void *const +#define TYPVAL_ENCODE_FIRST_ARG_NAME ignored +#include "nvim/eval/typval_encode.c.h" +#undef TYPVAL_ENCODE_SCOPE +#undef TYPVAL_ENCODE_NAME +#undef TYPVAL_ENCODE_FIRST_ARG_TYPE +#undef TYPVAL_ENCODE_FIRST_ARG_NAME + +#undef TYPVAL_ENCODE_ALLOW_SPECIALS +#undef TYPVAL_ENCODE_CONV_NIL +#undef TYPVAL_ENCODE_CONV_BOOL +#undef TYPVAL_ENCODE_CONV_NUMBER +#undef TYPVAL_ENCODE_CONV_UNSIGNED_NUMBER +#undef TYPVAL_ENCODE_CONV_FLOAT +#undef TYPVAL_ENCODE_CONV_STRING +#undef TYPVAL_ENCODE_CONV_STR_STRING +#undef TYPVAL_ENCODE_CONV_EXT_STRING +#undef TYPVAL_ENCODE_CONV_FUNC_START +#undef TYPVAL_ENCODE_CONV_FUNC_BEFORE_ARGS +#undef TYPVAL_ENCODE_CONV_FUNC_BEFORE_SELF +#undef TYPVAL_ENCODE_CONV_FUNC_END +#undef TYPVAL_ENCODE_CONV_EMPTY_LIST +#undef TYPVAL_ENCODE_CONV_EMPTY_DICT +#undef TYPVAL_ENCODE_CONV_LIST_START +#undef TYPVAL_ENCODE_CONV_REAL_LIST_AFTER_START +#undef TYPVAL_ENCODE_CONV_LIST_BETWEEN_ITEMS +#undef TYPVAL_ENCODE_CONV_LIST_END +#undef TYPVAL_ENCODE_CONV_DICT_START +#undef TYPVAL_ENCODE_CONV_REAL_DICT_AFTER_START +#undef TYPVAL_ENCODE_SPECIAL_DICT_KEY_CHECK +#undef TYPVAL_ENCODE_CONV_DICT_AFTER_KEY +#undef TYPVAL_ENCODE_CONV_DICT_BETWEEN_ITEMS +#undef TYPVAL_ENCODE_CONV_DICT_END +#undef TYPVAL_ENCODE_CONV_RECURSE + +/// Free memory for a variable value and set the value to NULL or 0 +/// +/// @param[in,out] varp Value to free. +void clear_tv(typval_T *varp) +{ + if (varp != NULL && varp->v_type != VAR_UNKNOWN) { + const int evn_ret = encode_vim_to_nothing(varp, varp, "clear_tv argument"); + (void)evn_ret; + assert(evn_ret == OK); } } @@ -17906,7 +19340,7 @@ static void init_tv(typval_T *varp) * caller of incompatible types: it sets *denote to TRUE if "denote" * is not NULL or returns -1 otherwise. */ -static long get_tv_number(typval_T *varp) +long get_tv_number(typval_T *varp) { int error = FALSE; @@ -17924,6 +19358,7 @@ long get_tv_number_chk(typval_T *varp, int *denote) EMSG(_("E805: Using a Float as a Number")); break; case VAR_FUNC: + case VAR_PARTIAL: EMSG(_("E703: Using a Funcref as a Number")); break; case VAR_STRING: @@ -17971,6 +19406,7 @@ static float_T get_tv_float(typval_T *varp) return varp->vval.v_float; break; case VAR_FUNC: + case VAR_PARTIAL: EMSG(_("E891: Using a Funcref as a Float")); break; case VAR_STRING: @@ -18002,7 +19438,7 @@ static linenr_T get_tv_lnum(typval_T *argvars) lnum = get_tv_number_chk(&argvars[0], NULL); if (lnum == 0) { /* no valid number, try using line() */ rettv.v_type = VAR_NUMBER; - f_line(argvars, &rettv); + f_line(argvars, &rettv, NULL); lnum = rettv.vval.v_number; clear_tv(&rettv); } @@ -18067,6 +19503,7 @@ static char_u *get_tv_string_buf_chk(const typval_T *varp, char_u *buf) sprintf((char *)buf, "%" PRId64, (int64_t)varp->vval.v_number); return buf; case VAR_FUNC: + case VAR_PARTIAL: EMSG(_("E729: using Funcref as a String")); break; case VAR_LIST: @@ -18112,11 +19549,10 @@ static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload) return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL); } -/* - * Find variable "varname" in hashtab "ht" with name "htname". - * Returns NULL if not found. - */ -static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload) +/// Find variable "varname" in hashtab "ht" with name "htname". +/// Returns NULL if not found. +static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, + char_u *varname, bool no_autoload) { hashitem_T *hi; @@ -18402,11 +19838,11 @@ list_one_var_a ( msg_puts(name); msg_putchar(' '); msg_advance(22); - if (type == VAR_NUMBER) + if (type == VAR_NUMBER) { msg_putchar('#'); - else if (type == VAR_FUNC) + } else if (type == VAR_FUNC || type == VAR_PARTIAL) { msg_putchar('*'); - else if (type == VAR_LIST) { + } else if (type == VAR_LIST) { msg_putchar('['); if (*string == '[') ++string; @@ -18419,8 +19855,9 @@ list_one_var_a ( msg_outtrans(string); - if (type == VAR_FUNC) + if (type == VAR_FUNC || type == VAR_PARTIAL) { msg_puts((char_u *)"()"); + } if (*first) { msg_clr_eos(); *first = FALSE; @@ -18458,8 +19895,10 @@ set_var ( } v = find_var_in_ht(ht, 0, varname, TRUE); - if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL)) + if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) + && var_check_func_name(name, v == NULL)) { return; + } if (v != NULL) { // existing variable, need to clear the value @@ -18467,19 +19906,6 @@ set_var ( || tv_check_lock(v->di_tv.v_lock, name, false)) { return; } - if (v->di_tv.v_type != tv->v_type - && !((v->di_tv.v_type == VAR_STRING - || v->di_tv.v_type == VAR_NUMBER) - && (tv->v_type == VAR_STRING - || tv->v_type == VAR_NUMBER)) - && !((v->di_tv.v_type == VAR_NUMBER - || v->di_tv.v_type == VAR_FLOAT) - && (tv->v_type == VAR_NUMBER - || tv->v_type == VAR_FLOAT)) - ) { - EMSG2(_("E706: Variable type mismatch for: %s"), name); - return; - } // Handle setting internal v: variables separately where needed to // prevent changing the type. @@ -18489,7 +19915,7 @@ set_var ( if (copy || tv->v_type != VAR_STRING) v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv)); else { - /* Take over the string to avoid an extra alloc/free. */ + // Take over the string to avoid an extra alloc/free. v->di_tv.vval.v_string = tv->vval.v_string; tv->vval.v_string = NULL; } @@ -18672,6 +20098,14 @@ void copy_tv(typval_T *from, typval_T *to) } } break; + case VAR_PARTIAL: + if (from->vval.v_partial == NULL) { + to->vval.v_partial = NULL; + } else { + to->vval.v_partial = from->vval.v_partial; + (to->vval.v_partial->pt_refcount)++; + } + break; case VAR_LIST: if (from->vval.v_list != NULL) { to->vval.v_list->lv_refcount++; @@ -18724,6 +20158,7 @@ int var_item_copy(const vimconv_T *const conv, case VAR_NUMBER: case VAR_FLOAT: case VAR_FUNC: + case VAR_PARTIAL: case VAR_SPECIAL: copy_tv(from, to); break; @@ -19083,7 +20518,7 @@ void ex_function(exarg_T *eap) // s:func script-local function name // g:func global function name, same as "func" p = eap->arg; - name = trans_function_name(&p, eap->skip, 0, &fudi); + name = trans_function_name(&p, eap->skip, 0, &fudi, NULL); paren = (vim_strchr(p, '(') != NULL); if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip) { /* @@ -19169,7 +20604,8 @@ void ex_function(exarg_T *eap) else arg = fudi.fd_newkey; if (arg != NULL && (fudi.fd_di == NULL - || fudi.fd_di->di_tv.v_type != VAR_FUNC)) { + || (fudi.fd_di->di_tv.v_type != VAR_FUNC + && fudi.fd_di->di_tv.v_type != VAR_PARTIAL))) { int j = (*arg == K_SPECIAL) ? 3 : 0; while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j]) : eval_isnamec(arg[j]))) @@ -19350,7 +20786,7 @@ void ex_function(exarg_T *eap) p = skipwhite(p + 1); } p += eval_fname_script(p); - xfree(trans_function_name(&p, TRUE, 0, NULL)); + xfree(trans_function_name(&p, true, 0, NULL, NULL)); if (*skipwhite(p) == '(') { nesting++; indent += 2; @@ -19568,7 +21004,8 @@ trans_function_name ( char_u **pp, int skip, /* only find the end, don't evaluate */ int flags, - funcdict_T *fdp /* return: info about dictionary used */ + funcdict_T *fdp, // return: info about dictionary used + partial_T **partial // return: partial of a FuncRef ) { char_u *name = NULL; @@ -19630,6 +21067,13 @@ trans_function_name ( if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL) { name = vim_strsave(lv.ll_tv->vval.v_string); *pp = end; + } else if (lv.ll_tv->v_type == VAR_PARTIAL + && lv.ll_tv->vval.v_partial != NULL) { + name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name); + *pp = end; + if (partial != NULL) { + *partial = lv.ll_tv->vval.v_partial; + } } else { if (!skip && !(flags & TFN_QUIET) && (fdp == NULL || lv.ll_dict == NULL @@ -19652,14 +21096,17 @@ trans_function_name ( /* Check if the name is a Funcref. If so, use the value. */ if (lv.ll_exp_name != NULL) { len = (int)STRLEN(lv.ll_exp_name); - name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD); - if (name == lv.ll_exp_name) + name = deref_func_name(lv.ll_exp_name, &len, partial, + flags & TFN_NO_AUTOLOAD); + if (name == lv.ll_exp_name) { name = NULL; + } } else { len = (int)(end - *pp); - name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD); - if (name == *pp) + name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD); + if (name == *pp) { name = NULL; + } } if (name != NULL) { name = vim_strsave(name); @@ -19750,11 +21197,15 @@ theend: */ static int eval_fname_script(char_u *p) { - if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0 - || STRNICMP(p + 1, "SNR>", 4) == 0)) + // Use mb_stricmp() because in Turkish comparing the "I" may not work with + // the standard library function. + if (p[0] == '<' && (mb_strnicmp(p + 1, (char_u *)"SID>", 4) == 0 + || mb_strnicmp(p + 1, (char_u *)"SNR>", 4) == 0)) { return 5; - if (p[0] == 's' && p[1] == ':') + } + if (p[0] == 's' && p[1] == ':') { return 2; + } return 0; } @@ -19839,7 +21290,7 @@ void free_all_functions(void) int translated_function_exists(char_u *name) { if (builtin_function(name, -1)) { - return find_internal_func(name) >= 0; + return find_internal_func((char *)name) != NULL; } return find_func(name) != NULL; } @@ -19853,8 +21304,8 @@ static int function_exists(char_u *name) char_u *p; int n = FALSE; - p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD, - NULL); + p = trans_function_name(&nm, false, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD, + NULL, NULL); nm = skipwhite(nm); /* Only accept "funcname", "funcname ", "funcname (..." and @@ -20079,9 +21530,10 @@ script_autoload ( tofree = NULL; } - /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */ - if (source_runtime(scriptname, FALSE) == OK) - ret = TRUE; + // Try loading the package from $VIMRUNTIME/autoload/<name>.vim + if (source_runtime(scriptname, 0) == OK) { + ret = true; + } } xfree(tofree); @@ -20173,7 +21625,7 @@ void ex_delfunction(exarg_T *eap) funcdict_T fudi; p = eap->arg; - name = trans_function_name(&p, eap->skip, 0, &fudi); + name = trans_function_name(&p, eap->skip, 0, &fudi, NULL); xfree(fudi.fd_newkey); if (name == NULL) { if (fudi.fd_dict != NULL && !eap->skip) @@ -20254,7 +21706,13 @@ void func_unref(char_u *name) if (name != NULL && isdigit(*name)) { fp = find_func(name); if (fp == NULL) { +#ifdef EXITFREE + if (!entered_free_all_mem) { + EMSG2(_(e_intern2), "func_unref()"); + } +#else EMSG2(_(e_intern2), "func_unref()"); +#endif } else { user_func_unref(fp); } @@ -20856,7 +22314,7 @@ char_u *get_return_cmd(void *rettv) } STRCPY(IObuff, ":return "); - STRNCPY(IObuff + 8, s, IOSIZE - 8); + STRLCPY(IObuff + 8, s, IOSIZE - 8); if (STRLEN(s) + 8 >= IOSIZE) STRCPY(IObuff + IOSIZE - 4, "..."); xfree(tofree); @@ -21158,8 +22616,8 @@ void ex_oldfiles(exarg_T *eap) /* Assume "got_int" was set to truncate the listing. */ got_int = FALSE; - // File selection prompt on ":oldfiles!" - if (eap->forceit) { + // File selection prompt on ":browse oldfiles" + if (cmdmod.browse) { quit_more = false; nr = prompt_for_number(false); msg_starthere(); @@ -21532,24 +22990,25 @@ char_u *do_string_sub(char_u *str, char_u *pat, char_u *sub, char_u *flags) } static inline TerminalJobData *common_job_init(char **argv, - ufunc_T *on_stdout, - ufunc_T *on_stderr, - ufunc_T *on_exit, - dict_T *self, + Callback on_stdout, + Callback on_stderr, + Callback on_exit, bool pty, - bool detach) + bool rpc, + bool detach, + char *cwd) { TerminalJobData *data = xcalloc(1, sizeof(TerminalJobData)); data->stopped = false; data->on_stdout = on_stdout; data->on_stderr = on_stderr; data->on_exit = on_exit; - data->self = self; - data->events = queue_new_child(loop.events); + data->events = multiqueue_new_child(main_loop.events); + data->rpc = rpc; if (pty) { - data->proc.pty = pty_process_init(&loop, data); + data->proc.pty = pty_process_init(&main_loop, data); } else { - data->proc.uv = libuv_process_init(&loop, data); + data->proc.uv = libuv_process_init(&main_loop, data); } Process *proc = (Process *)&data->proc; proc->argv = argv; @@ -21558,15 +23017,18 @@ static inline TerminalJobData *common_job_init(char **argv, if (!pty) { proc->err = &data->err; } - proc->cb = on_process_exit; + proc->cb = eval_job_process_exit_cb; proc->events = data->events; proc->detach = detach; + proc->cwd = cwd; return data; } -/// Return true/false on success/failure. -static inline bool common_job_callbacks(dict_T *vopts, ufunc_T **on_stdout, - ufunc_T **on_stderr, ufunc_T **on_exit) +/// common code for getting job callbacks for jobstart, termopen and rpcstart +/// +/// @return true/false on success/failure. +static inline bool common_job_callbacks(dict_T *vopts, Callback *on_stdout, + Callback *on_stderr, Callback *on_exit) { if (get_dict_callback(vopts, "on_stdout", on_stdout) && get_dict_callback(vopts, "on_stderr", on_stderr) @@ -21574,15 +23036,10 @@ static inline bool common_job_callbacks(dict_T *vopts, ufunc_T **on_stdout, vopts->dv_refcount++; return true; } - if (*on_stdout) { - user_func_unref(*on_stdout); - } - if (*on_stderr) { - user_func_unref(*on_stderr); - } - if (*on_exit) { - user_func_unref(*on_exit); - } + + callback_free(on_stdout); + callback_free(on_stderr); + callback_free(on_exit); return false; } @@ -21591,9 +23048,15 @@ static inline bool common_job_start(TerminalJobData *data, typval_T *rettv) Process *proc = (Process *)&data->proc; if (proc->type == kProcessTypePty && proc->detach) { EMSG2(_(e_invarg2), "terminal/pty job cannot be detached"); + xfree(data->proc.pty.term_name); + shell_free_argv(proc->argv); + free_term_job_data_event((void **)&data); return false; } + data->id = next_chan_id++; + pmap_put(uint64_t)(jobs, data->id, data); + data->refcount++; char *cmd = xstrdup(proc->argv[0]); if (!process_spawn(proc)) { @@ -21608,17 +23071,22 @@ static inline bool common_job_start(TerminalJobData *data, typval_T *rettv) } xfree(cmd); - data->id = current_job_id++; - wstream_init(proc->in, 0); - if (proc->out) { - rstream_init(proc->out, 0); - rstream_start(proc->out, on_job_stdout); + + if (data->rpc) { + // the rpc channel takes over the in and out streams + channel_from_process(proc, data->id); + } else { + wstream_init(proc->in, 0); + if (proc->out) { + rstream_init(proc->out, 0); + rstream_start(proc->out, on_job_stdout, data); + } } + if (proc->err) { rstream_init(proc->err, 0); - rstream_start(proc->err, on_job_stderr); + rstream_start(proc->err, on_job_stderr, data); } - pmap_put(uint64_t)(jobs, data->id, data); rettv->vval.v_number = data->id; return true; } @@ -21626,20 +23094,12 @@ static inline bool common_job_start(TerminalJobData *data, typval_T *rettv) static inline void free_term_job_data_event(void **argv) { TerminalJobData *data = argv[0]; - if (data->on_stdout) { - user_func_unref(data->on_stdout); - } - if (data->on_stderr) { - user_func_unref(data->on_stderr); - } - if (data->on_exit) { - user_func_unref(data->on_exit); - } + callback_free(&data->on_stdout); + callback_free(&data->on_stderr); + callback_free(&data->on_exit); - if (data->self) { - dict_unref(data->self); - } - queue_free(data->events); + multiqueue_free(data->events); + pmap_del(uint64_t)(jobs, data->id); xfree(data); } @@ -21647,12 +23107,13 @@ static inline void free_term_job_data(TerminalJobData *data) { // data->queue may still be used after this function returns(process_wait), so // only free in the next event loop iteration - queue_put(loop.fast_events, free_term_job_data_event, 1, data); + multiqueue_put(main_loop.fast_events, free_term_job_data_event, 1, data); } // vimscript job callbacks must be executed on Nvim main loop -static inline void process_job_event(TerminalJobData *data, ufunc_T *callback, - const char *type, char *buf, size_t count, int status) +static inline void process_job_event(TerminalJobData *data, Callback *callback, + const char *type, char *buf, size_t count, + int status) { JobEvent event_data; event_data.received = NULL; @@ -21692,18 +23153,19 @@ static void on_job_stdout(Stream *stream, RBuffer *buf, size_t count, void *job, bool eof) { TerminalJobData *data = job; - on_job_output(stream, job, buf, count, eof, data->on_stdout, "stdout"); + on_job_output(stream, job, buf, count, eof, &data->on_stdout, "stdout"); } static void on_job_stderr(Stream *stream, RBuffer *buf, size_t count, void *job, bool eof) { TerminalJobData *data = job; - on_job_output(stream, job, buf, count, eof, data->on_stderr, "stderr"); + on_job_output(stream, job, buf, count, eof, &data->on_stderr, "stderr"); } static void on_job_output(Stream *stream, TerminalJobData *data, RBuffer *buf, - size_t count, bool eof, ufunc_T *callback, const char *type) + size_t count, bool eof, Callback *callback, + const char *type) { if (eof) { return; @@ -21720,14 +23182,13 @@ static void on_job_output(Stream *stream, TerminalJobData *data, RBuffer *buf, terminal_receive(data->term, ptr, count); } - if (callback) { + rbuffer_consumed(buf, count); + if (callback->type != kCallbackNone) { process_job_event(data, callback, type, ptr, count, 0); } - - rbuffer_consumed(buf, count); } -static void on_process_exit(Process *proc, int status, void *d) +static void eval_job_process_exit_cb(Process *proc, int status, void *d) { TerminalJobData *data = d; if (data->term && !data->exited) { @@ -21736,19 +23197,30 @@ static void on_process_exit(Process *proc, int status, void *d) snprintf(msg, sizeof msg, "\r\n[Process exited %d]", proc->status); terminal_close(data->term, msg); } + if (data->rpc) { + channel_process_exit(data->id, status); + } if (data->status_ptr) { *data->status_ptr = status; } - process_job_event(data, data->on_exit, "exit", NULL, 0, status); + process_job_event(data, &data->on_exit, "exit", NULL, 0, status); + + term_job_data_decref(data); } static void term_write(char *buf, size_t size, void *d) { - TerminalJobData *data = d; + TerminalJobData *job = d; + if (job->in.closed) { + // If the backing stream was closed abruptly, there may be write events + // ahead of the terminal close event. Just ignore the writes. + ILOG("write failed: stream is closed"); + return; + } WBuffer *wbuf = wstream_new_buffer(xmemdup(buf, size), size, 1, xfree); - wstream_write(&data->in, wbuf); + wstream_write(&job->in, wbuf); } static void term_resize(uint16_t width, uint16_t height, void *d) @@ -21761,7 +23233,7 @@ static inline void term_delayed_free(void **argv) { TerminalJobData *j = argv[0]; if (j->in.pending_reqs || j->out.pending_reqs || j->err.pending_reqs) { - queue_put(j->events, term_delayed_free, 1, j); + multiqueue_put(j->events, term_delayed_free, 1, j); return; } @@ -21776,7 +23248,7 @@ static void term_close(void *d) data->exited = true; process_stop((Process *)&data->proc); } - queue_put(data->events, term_delayed_free, 1, data); + multiqueue_put(data->events, term_delayed_free, 1, data); } static void term_job_data_decref(TerminalJobData *data) @@ -21789,49 +23261,34 @@ static void term_job_data_decref(TerminalJobData *data) static void on_job_event(JobEvent *ev) { if (!ev->callback) { - goto end; + return; } - typval_T argv[3]; - int argc = ev->callback->uf_args.ga_len; + typval_T argv[4]; - if (argc > 0) { - argv[0].v_type = VAR_NUMBER; - argv[0].v_lock = 0; - argv[0].vval.v_number = ev->data->id; - } + argv[0].v_type = VAR_NUMBER; + argv[0].v_lock = 0; + argv[0].vval.v_number = ev->data->id; - if (argc > 1) { - if (ev->received) { - argv[1].v_type = VAR_LIST; - argv[1].v_lock = 0; - argv[1].vval.v_list = ev->received; - argv[1].vval.v_list->lv_refcount++; - } else { - argv[1].v_type = VAR_NUMBER; - argv[1].v_lock = 0; - argv[1].vval.v_number = ev->status; - } + if (ev->received) { + argv[1].v_type = VAR_LIST; + argv[1].v_lock = 0; + argv[1].vval.v_list = ev->received; + argv[1].vval.v_list->lv_refcount++; + } else { + argv[1].v_type = VAR_NUMBER; + argv[1].v_lock = 0; + argv[1].vval.v_number = ev->status; } - if (argc > 2) { - argv[2].v_type = VAR_STRING; - argv[2].v_lock = 0; - argv[2].vval.v_string = (uint8_t *)ev->type; - } + argv[2].v_type = VAR_STRING; + argv[2].v_lock = 0; + argv[2].vval.v_string = (uint8_t *)ev->type; typval_T rettv; init_tv(&rettv); - call_user_func(ev->callback, argc, argv, &rettv, curwin->w_cursor.lnum, - curwin->w_cursor.lnum, ev->data->self); + callback_call(ev->callback, 3, argv, &rettv); clear_tv(&rettv); - -end: - if (!ev->received) { - // exit event, safe to free job data now - pmap_del(uint64_t)(jobs, ev->data->id); - term_job_data_decref(ev->data); - } } static TerminalJobData *find_job(uint64_t id) @@ -21851,6 +23308,7 @@ static void script_host_eval(char *name, typval_T *argvars, typval_T *rettv) if (argvars[0].v_type != VAR_STRING) { EMSG(_(e_invarg)); + return; } list_T *args = list_alloc(); @@ -21895,6 +23353,7 @@ typval_T eval_call_provider(char *provider, char *method, list_T *arguments) curwin->w_cursor.lnum, &dummy, true, + NULL, NULL); list_unref(arguments); @@ -21908,17 +23367,19 @@ typval_T eval_call_provider(char *provider, char *method, list_T *arguments) bool eval_has_provider(char *name) { - -#define check_provider(name) \ - if (has_##name == -1) { \ - has_##name = !!find_func((uint8_t *)"provider#" #name "#Call"); \ - if (!has_##name) { \ - script_autoload((uint8_t *)"provider#" #name "#Call", false); \ - has_##name = !!find_func((uint8_t *)"provider#" #name "#Call"); \ - } \ +#define check_provider(name) \ + if (has_##name == -1) { \ + has_##name = !!find_func((uint8_t *)"provider#" #name "#Call"); \ + if (!has_##name) { \ + script_autoload((uint8_t *)"provider#" #name "#Call", false); \ + has_##name = !!find_func((uint8_t *)"provider#" #name "#Call"); \ + } \ } - static int has_clipboard = -1, has_python = -1, has_python3 = -1; + static int has_clipboard = -1; + static int has_python = -1; + static int has_python3 = -1; + static int has_ruby = -1; if (!strcmp(name, "clipboard")) { check_provider(clipboard); @@ -21929,14 +23390,16 @@ bool eval_has_provider(char *name) } else if (!strcmp(name, "python")) { check_provider(python); return has_python; + } else if (!strcmp(name, "ruby")) { + check_provider(ruby); + return has_ruby; } return false; } -// Compute the `DictWatcher` address from a QUEUE node. This only exists because -// ASAN doesn't handle `QUEUE_DATA` pointer arithmetic, and we blacklist this -// function on .asan-blacklist. +// Compute the `DictWatcher` address from a QUEUE node. This only exists for +// .asan-blacklist (ASAN doesn't handle QUEUE_DATA pointer arithmetic). static DictWatcher *dictwatcher_node_data(QUEUE *q) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { @@ -21948,7 +23411,7 @@ static void dictwatcher_notify(dict_T *dict, const char *key, typval_T *newtv, typval_T *oldtv) FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(2) { - typval_T argv[3]; + typval_T argv[4]; for (size_t i = 0; i < ARRAY_SIZE(argv); i++) { init_tv(argv + i); } @@ -21981,8 +23444,7 @@ static void dictwatcher_notify(dict_T *dict, const char *key, typval_T *newtv, if (!watcher->busy && dictwatcher_matches(watcher, key)) { init_tv(&rettv); watcher->busy = true; - call_user_func(watcher->callback, ARRAY_SIZE(argv), argv, &rettv, - curwin->w_cursor.lnum, curwin->w_cursor.lnum, NULL); + callback_call(&watcher->callback, 3, argv, &rettv); watcher->busy = false; clear_tv(&rettv); } @@ -22013,7 +23475,7 @@ static bool dictwatcher_matches(DictWatcher *watcher, const char *key) static void dictwatcher_free(DictWatcher *watcher) FUNC_ATTR_NONNULL_ALL { - user_func_unref(watcher->callback); + callback_free(&watcher->callback); xfree(watcher->key_pattern); xfree(watcher); } |