1 /* vi:set ts=8 sts=4 sw=4: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * eval.c: Expression evaluation. 12 */ 13 #if defined(MSDOS) || defined(MSWIN) 14 # include "vimio.h" /* for mch_open(), must be before vim.h */ 15 #endif 16 17 #include "vim.h" 18 19 #ifdef AMIGA 20 # include <time.h> /* for strftime() */ 21 #endif 22 23 #ifdef MACOS 24 # include <time.h> /* for time_t */ 25 #endif 26 27 #ifdef HAVE_FCNTL_H 28 # include <fcntl.h> 29 #endif 30 31 #if defined(FEAT_EVAL) || defined(PROTO) 32 33 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */ 34 35 /* 36 * In a hashtab item "hi_key" points to "di_key" in a dictitem. 37 * This avoids adding a pointer to the hashtab item. 38 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer. 39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer. 40 * HI2DI() converts a hashitem pointer to a dictitem pointer. 41 */ 42 static dictitem_T dumdi; 43 #define DI2HIKEY(di) ((di)->di_key) 44 #define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi))) 45 #define HI2DI(hi) HIKEY2DI((hi)->hi_key) 46 47 /* 48 * Structure returned by get_lval() and used by set_var_lval(). 49 * For a plain name: 50 * "name" points to the variable name. 51 * "exp_name" is NULL. 52 * "tv" is NULL 53 * For a magic braces name: 54 * "name" points to the expanded variable name. 55 * "exp_name" is non-NULL, to be freed later. 56 * "tv" is NULL 57 * For an index in a list: 58 * "name" points to the (expanded) variable name. 59 * "exp_name" NULL or non-NULL, to be freed later. 60 * "tv" points to the (first) list item value 61 * "li" points to the (first) list item 62 * "range", "n1", "n2" and "empty2" indicate what items are used. 63 * For an existing Dict item: 64 * "name" points to the (expanded) variable name. 65 * "exp_name" NULL or non-NULL, to be freed later. 66 * "tv" points to the dict item value 67 * "newkey" is NULL 68 * For a non-existing Dict item: 69 * "name" points to the (expanded) variable name. 70 * "exp_name" NULL or non-NULL, to be freed later. 71 * "tv" points to the Dictionary typval_T 72 * "newkey" is the key for the new item. 73 */ 74 typedef struct lval_S 75 { 76 char_u *ll_name; /* start of variable name (can be NULL) */ 77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */ 78 typval_T *ll_tv; /* Typeval of item being used. If "newkey" 79 isn't NULL it's the Dict to which to add 80 the item. */ 81 listitem_T *ll_li; /* The list item or NULL. */ 82 list_T *ll_list; /* The list or NULL. */ 83 int ll_range; /* TRUE when a [i:j] range was used */ 84 long ll_n1; /* First index for list */ 85 long ll_n2; /* Second index for list range */ 86 int ll_empty2; /* Second index is empty: [i:] */ 87 dict_T *ll_dict; /* The Dictionary or NULL */ 88 dictitem_T *ll_di; /* The dictitem or NULL */ 89 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */ 90 } lval_T; 91 92 93 static char *e_letunexp = N_("E18: Unexpected characters in :let"); 94 static char *e_listidx = N_("E684: list index out of range: %ld"); 95 static char *e_undefvar = N_("E121: Undefined variable: %s"); 96 static char *e_missbrac = N_("E111: Missing ']'"); 97 static char *e_listarg = N_("E686: Argument of %s must be a List"); 98 static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary"); 99 static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary"); 100 static char *e_listreq = N_("E714: List required"); 101 static char *e_dictreq = N_("E715: Dictionary required"); 102 static char *e_toomanyarg = N_("E118: Too many arguments for function: %s"); 103 static char *e_dictkey = N_("E716: Key not present in Dictionary: %s"); 104 static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it"); 105 static char *e_funcdict = N_("E717: Dictionary entry already exists"); 106 static char *e_funcref = N_("E718: Funcref required"); 107 static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary"); 108 static char *e_letwrong = N_("E734: Wrong variable type for %s="); 109 static char *e_nofunc = N_("E130: Unknown function: %s"); 110 static char *e_illvar = N_("E461: Illegal variable name: %s"); 111 /* 112 * All user-defined global variables are stored in dictionary "globvardict". 113 * "globvars_var" is the variable that is used for "g:". 114 */ 115 static dict_T globvardict; 116 static dictitem_T globvars_var; 117 #define globvarht globvardict.dv_hashtab 118 119 /* 120 * Old Vim variables such as "v:version" are also available without the "v:". 121 * Also in functions. We need a special hashtable for them. 122 */ 123 static hashtab_T compat_hashtab; 124 125 /* 126 * When recursively copying lists and dicts we need to remember which ones we 127 * have done to avoid endless recursiveness. This unique ID is used for that. 128 */ 129 static int current_copyID = 0; 130 131 /* 132 * Array to hold the hashtab with variables local to each sourced script. 133 * Each item holds a variable (nameless) that points to the dict_T. 134 */ 135 typedef struct 136 { 137 dictitem_T sv_var; 138 dict_T sv_dict; 139 } scriptvar_T; 140 141 static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL}; 142 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1]) 143 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab) 144 145 static int echo_attr = 0; /* attributes used for ":echo" */ 146 147 /* Values for trans_function_name() argument: */ 148 #define TFN_INT 1 /* internal function name OK */ 149 #define TFN_QUIET 2 /* no error messages */ 150 151 /* 152 * Structure to hold info for a user function. 153 */ 154 typedef struct ufunc ufunc_T; 155 156 struct ufunc 157 { 158 int uf_varargs; /* variable nr of arguments */ 159 int uf_flags; 160 int uf_calls; /* nr of active calls */ 161 garray_T uf_args; /* arguments */ 162 garray_T uf_lines; /* function lines */ 163 #ifdef FEAT_PROFILE 164 int uf_profiling; /* TRUE when func is being profiled */ 165 /* profiling the function as a whole */ 166 int uf_tm_count; /* nr of calls */ 167 proftime_T uf_tm_total; /* time spend in function + children */ 168 proftime_T uf_tm_self; /* time spend in function itself */ 169 proftime_T uf_tm_start; /* time at function call */ 170 proftime_T uf_tm_children; /* time spent in children this call */ 171 /* profiling the function per line */ 172 int *uf_tml_count; /* nr of times line was executed */ 173 proftime_T *uf_tml_total; /* time spend in a line + children */ 174 proftime_T *uf_tml_self; /* time spend in a line itself */ 175 proftime_T uf_tml_start; /* start time for current line */ 176 proftime_T uf_tml_children; /* time spent in children for this line */ 177 proftime_T uf_tml_wait; /* start wait time for current line */ 178 int uf_tml_idx; /* index of line being timed; -1 if none */ 179 int uf_tml_execed; /* line being timed was executed */ 180 #endif 181 scid_T uf_script_ID; /* ID of script where function was defined, 182 used for s: variables */ 183 int uf_refcount; /* for numbered function: reference count */ 184 char_u uf_name[1]; /* name of function (actually longer); can 185 start with <SNR>123_ (<SNR> is K_SPECIAL 186 KS_EXTRA KE_SNR) */ 187 }; 188 189 /* function flags */ 190 #define FC_ABORT 1 /* abort function on error */ 191 #define FC_RANGE 2 /* function accepts range */ 192 #define FC_DICT 4 /* Dict function, uses "self" */ 193 194 #define DEL_REFCOUNT 999999 /* list/dict is being deleted */ 195 196 /* 197 * All user-defined functions are found in this hashtable. 198 */ 199 static hashtab_T func_hashtab; 200 201 /* list heads for garbage collection */ 202 static dict_T *first_dict = NULL; /* list of all dicts */ 203 static list_T *first_list = NULL; /* list of all lists */ 204 205 /* From user function to hashitem and back. */ 206 static ufunc_T dumuf; 207 #define UF2HIKEY(fp) ((fp)->uf_name) 208 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf))) 209 #define HI2UF(hi) HIKEY2UF((hi)->hi_key) 210 211 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j] 212 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j] 213 214 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */ 215 #define VAR_SHORT_LEN 20 /* short variable name length */ 216 #define FIXVAR_CNT 12 /* number of fixed variables */ 217 218 /* structure to hold info for a function that is currently being executed. */ 219 typedef struct funccall_S funccall_T; 220 221 struct funccall_S 222 { 223 ufunc_T *func; /* function being called */ 224 int linenr; /* next line to be executed */ 225 int returned; /* ":return" used */ 226 struct /* fixed variables for arguments */ 227 { 228 dictitem_T var; /* variable (without room for name) */ 229 char_u room[VAR_SHORT_LEN]; /* room for the name */ 230 } fixvar[FIXVAR_CNT]; 231 dict_T l_vars; /* l: local function variables */ 232 dictitem_T l_vars_var; /* variable for l: scope */ 233 dict_T l_avars; /* a: argument variables */ 234 dictitem_T l_avars_var; /* variable for a: scope */ 235 list_T l_varlist; /* list for a:000 */ 236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */ 237 typval_T *rettv; /* return value */ 238 linenr_T breakpoint; /* next line with breakpoint or zero */ 239 int dbg_tick; /* debug_tick when breakpoint was set */ 240 int level; /* top nesting level of executed function */ 241 #ifdef FEAT_PROFILE 242 proftime_T prof_child; /* time spent in a child */ 243 #endif 244 funccall_T *caller; /* calling function or NULL */ 245 }; 246 247 /* 248 * Info used by a ":for" loop. 249 */ 250 typedef struct 251 { 252 int fi_semicolon; /* TRUE if ending in '; var]' */ 253 int fi_varcount; /* nr of variables in the list */ 254 listwatch_T fi_lw; /* keep an eye on the item used. */ 255 list_T *fi_list; /* list being used */ 256 } forinfo_T; 257 258 /* 259 * Struct used by trans_function_name() 260 */ 261 typedef struct 262 { 263 dict_T *fd_dict; /* Dictionary used */ 264 char_u *fd_newkey; /* new key in "dict" in allocated memory */ 265 dictitem_T *fd_di; /* Dictionary item used */ 266 } funcdict_T; 267 268 269 /* 270 * Array to hold the value of v: variables. 271 * The value is in a dictitem, so that it can also be used in the v: scope. 272 * The reason to use this table anyway is for very quick access to the 273 * variables with the VV_ defines. 274 */ 275 #include "version.h" 276 277 /* values for vv_flags: */ 278 #define VV_COMPAT 1 /* compatible, also used without "v:" */ 279 #define VV_RO 2 /* read-only */ 280 #define VV_RO_SBX 4 /* read-only in the sandbox */ 281 282 #define VV_NAME(s, t) s, {{t}}, {0} 283 284 static struct vimvar 285 { 286 char *vv_name; /* name of variable, without v: */ 287 dictitem_T vv_di; /* value and name for key */ 288 char vv_filler[16]; /* space for LONGEST name below!!! */ 289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */ 290 } vimvars[VV_LEN] = 291 { 292 /* 293 * The order here must match the VV_ defines in vim.h! 294 * Initializing a union does not work, leave tv.vval empty to get zero's. 295 */ 296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO}, 297 {VV_NAME("count1", VAR_NUMBER), VV_RO}, 298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO}, 299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT}, 300 {VV_NAME("warningmsg", VAR_STRING), 0}, 301 {VV_NAME("statusmsg", VAR_STRING), 0}, 302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO}, 303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT}, 304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO}, 305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX}, 306 {VV_NAME("termresponse", VAR_STRING), VV_RO}, 307 {VV_NAME("fname", VAR_STRING), VV_RO}, 308 {VV_NAME("lang", VAR_STRING), VV_RO}, 309 {VV_NAME("lc_time", VAR_STRING), VV_RO}, 310 {VV_NAME("ctype", VAR_STRING), VV_RO}, 311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO}, 312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO}, 313 {VV_NAME("fname_in", VAR_STRING), VV_RO}, 314 {VV_NAME("fname_out", VAR_STRING), VV_RO}, 315 {VV_NAME("fname_new", VAR_STRING), VV_RO}, 316 {VV_NAME("fname_diff", VAR_STRING), VV_RO}, 317 {VV_NAME("cmdarg", VAR_STRING), VV_RO}, 318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX}, 319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX}, 320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX}, 321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX}, 322 {VV_NAME("progname", VAR_STRING), VV_RO}, 323 {VV_NAME("servername", VAR_STRING), VV_RO}, 324 {VV_NAME("dying", VAR_NUMBER), VV_RO}, 325 {VV_NAME("exception", VAR_STRING), VV_RO}, 326 {VV_NAME("throwpoint", VAR_STRING), VV_RO}, 327 {VV_NAME("register", VAR_STRING), VV_RO}, 328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO}, 329 {VV_NAME("insertmode", VAR_STRING), VV_RO}, 330 {VV_NAME("val", VAR_UNKNOWN), VV_RO}, 331 {VV_NAME("key", VAR_UNKNOWN), VV_RO}, 332 {VV_NAME("profiling", VAR_NUMBER), VV_RO}, 333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO}, 334 {VV_NAME("fcs_choice", VAR_STRING), 0}, 335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO}, 336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO}, 337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO}, 338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO}, 339 {VV_NAME("beval_text", VAR_STRING), VV_RO}, 340 {VV_NAME("scrollstart", VAR_STRING), 0}, 341 {VV_NAME("swapname", VAR_STRING), VV_RO}, 342 {VV_NAME("swapchoice", VAR_STRING), 0}, 343 {VV_NAME("swapcommand", VAR_STRING), VV_RO}, 344 }; 345 346 /* shorthand */ 347 #define vv_type vv_di.di_tv.v_type 348 #define vv_nr vv_di.di_tv.vval.v_number 349 #define vv_str vv_di.di_tv.vval.v_string 350 #define vv_tv vv_di.di_tv 351 352 /* 353 * The v: variables are stored in dictionary "vimvardict". 354 * "vimvars_var" is the variable that is used for the "l:" scope. 355 */ 356 static dict_T vimvardict; 357 static dictitem_T vimvars_var; 358 #define vimvarht vimvardict.dv_hashtab 359 360 static void prepare_vimvar __ARGS((int idx, typval_T *save_tv)); 361 static void restore_vimvar __ARGS((int idx, typval_T *save_tv)); 362 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) 363 static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv)); 364 #endif 365 static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars)); 366 static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon)); 367 static char_u *skip_var_one __ARGS((char_u *arg)); 368 static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty)); 369 static void list_glob_vars __ARGS((void)); 370 static void list_buf_vars __ARGS((void)); 371 static void list_win_vars __ARGS((void)); 372 #ifdef FEAT_WINDOWS 373 static void list_tab_vars __ARGS((void)); 374 #endif 375 static void list_vim_vars __ARGS((void)); 376 static void list_script_vars __ARGS((void)); 377 static void list_func_vars __ARGS((void)); 378 static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg)); 379 static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op)); 380 static int check_changedtick __ARGS((char_u *arg)); 381 static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags)); 382 static void clear_lval __ARGS((lval_T *lp)); 383 static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op)); 384 static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op)); 385 static void list_add_watch __ARGS((list_T *l, listwatch_T *lw)); 386 static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem)); 387 static void list_fix_watch __ARGS((list_T *l, listitem_T *item)); 388 static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep)); 389 static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit)); 390 static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock)); 391 static void item_lock __ARGS((typval_T *tv, int deep, int lock)); 392 static int tv_islocked __ARGS((typval_T *tv)); 393 394 static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate)); 395 static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 396 static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 397 static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 398 static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 399 static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 400 static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 401 static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 402 403 static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose)); 404 static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 405 static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 406 static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 407 static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 408 static int rettv_list_alloc __ARGS((typval_T *rettv)); 409 static listitem_T *listitem_alloc __ARGS((void)); 410 static void listitem_free __ARGS((listitem_T *item)); 411 static void listitem_remove __ARGS((list_T *l, listitem_T *item)); 412 static long list_len __ARGS((list_T *l)); 413 static int list_equal __ARGS((list_T *l1, list_T *l2, int ic)); 414 static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic)); 415 static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic)); 416 static listitem_T *list_find __ARGS((list_T *l, long n)); 417 static long list_find_nr __ARGS((list_T *l, long idx, int *errorp)); 418 static long list_idx_of_item __ARGS((list_T *l, listitem_T *item)); 419 static void list_append __ARGS((list_T *l, listitem_T *item)); 420 static int list_append_tv __ARGS((list_T *l, typval_T *tv)); 421 static int list_append_string __ARGS((list_T *l, char_u *str, int len)); 422 static int list_append_number __ARGS((list_T *l, varnumber_T n)); 423 static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item)); 424 static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef)); 425 static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv)); 426 static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID)); 427 static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2)); 428 static char_u *list2string __ARGS((typval_T *tv, int copyID)); 429 static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID)); 430 static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID)); 431 static void set_ref_in_list __ARGS((list_T *l, int copyID)); 432 static void set_ref_in_item __ARGS((typval_T *tv, int copyID)); 433 static void dict_unref __ARGS((dict_T *d)); 434 static void dict_free __ARGS((dict_T *d)); 435 static dictitem_T *dictitem_alloc __ARGS((char_u *key)); 436 static dictitem_T *dictitem_copy __ARGS((dictitem_T *org)); 437 static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item)); 438 static void dictitem_free __ARGS((dictitem_T *item)); 439 static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID)); 440 static int dict_add __ARGS((dict_T *d, dictitem_T *item)); 441 static long dict_len __ARGS((dict_T *d)); 442 static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len)); 443 static char_u *dict2string __ARGS((typval_T *tv, int copyID)); 444 static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 445 static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID)); 446 static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID)); 447 static char_u *string_quote __ARGS((char_u *str, int function)); 448 static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 449 static int find_internal_func __ARGS((char_u *name)); 450 static char_u *deref_func_name __ARGS((char_u *name, int *lenp)); 451 static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict)); 452 static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict)); 453 static void emsg_funcname __ARGS((char *msg, char_u *name)); 454 455 static void f_add __ARGS((typval_T *argvars, typval_T *rettv)); 456 static void f_append __ARGS((typval_T *argvars, typval_T *rettv)); 457 static void f_argc __ARGS((typval_T *argvars, typval_T *rettv)); 458 static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv)); 459 static void f_argv __ARGS((typval_T *argvars, typval_T *rettv)); 460 static void f_browse __ARGS((typval_T *argvars, typval_T *rettv)); 461 static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv)); 462 static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv)); 463 static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv)); 464 static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv)); 465 static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv)); 466 static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv)); 467 static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv)); 468 static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv)); 469 static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv)); 470 static void f_call __ARGS((typval_T *argvars, typval_T *rettv)); 471 static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv)); 472 static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv)); 473 static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv)); 474 static void f_col __ARGS((typval_T *argvars, typval_T *rettv)); 475 #if defined(FEAT_INS_EXPAND) 476 static void f_complete __ARGS((typval_T *argvars, typval_T *rettv)); 477 static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv)); 478 static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv)); 479 #endif 480 static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv)); 481 static void f_copy __ARGS((typval_T *argvars, typval_T *rettv)); 482 static void f_count __ARGS((typval_T *argvars, typval_T *rettv)); 483 static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv)); 484 static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv)); 485 static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv)); 486 static void f_delete __ARGS((typval_T *argvars, typval_T *rettv)); 487 static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv)); 488 static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv)); 489 static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv)); 490 static void f_empty __ARGS((typval_T *argvars, typval_T *rettv)); 491 static void f_escape __ARGS((typval_T *argvars, typval_T *rettv)); 492 static void f_eval __ARGS((typval_T *argvars, typval_T *rettv)); 493 static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv)); 494 static void f_executable __ARGS((typval_T *argvars, typval_T *rettv)); 495 static void f_exists __ARGS((typval_T *argvars, typval_T *rettv)); 496 static void f_expand __ARGS((typval_T *argvars, typval_T *rettv)); 497 static void f_extend __ARGS((typval_T *argvars, typval_T *rettv)); 498 static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv)); 499 static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv)); 500 static void f_filter __ARGS((typval_T *argvars, typval_T *rettv)); 501 static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv)); 502 static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv)); 503 static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv)); 504 static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv)); 505 static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv)); 506 static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv)); 507 static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv)); 508 static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv)); 509 static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv)); 510 static void f_function __ARGS((typval_T *argvars, typval_T *rettv)); 511 static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv)); 512 static void f_get __ARGS((typval_T *argvars, typval_T *rettv)); 513 static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv)); 514 static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv)); 515 static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv)); 516 static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv)); 517 static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv)); 518 static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv)); 519 static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv)); 520 static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv)); 521 static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv)); 522 static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv)); 523 static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv)); 524 static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv)); 525 static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv)); 526 static void f_getline __ARGS((typval_T *argvars, typval_T *rettv)); 527 static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv)); 528 static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv)); 529 static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv)); 530 static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv)); 531 static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv)); 532 static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv)); 533 static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv)); 534 static void f_glob __ARGS((typval_T *argvars, typval_T *rettv)); 535 static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv)); 536 static void f_has __ARGS((typval_T *argvars, typval_T *rettv)); 537 static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv)); 538 static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv)); 539 static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv)); 540 static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv)); 541 static void f_histget __ARGS((typval_T *argvars, typval_T *rettv)); 542 static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv)); 543 static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv)); 544 static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv)); 545 static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv)); 546 static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv)); 547 static void f_indent __ARGS((typval_T *argvars, typval_T *rettv)); 548 static void f_index __ARGS((typval_T *argvars, typval_T *rettv)); 549 static void f_input __ARGS((typval_T *argvars, typval_T *rettv)); 550 static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv)); 551 static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv)); 552 static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv)); 553 static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv)); 554 static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv)); 555 static void f_insert __ARGS((typval_T *argvars, typval_T *rettv)); 556 static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv)); 557 static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv)); 558 static void f_items __ARGS((typval_T *argvars, typval_T *rettv)); 559 static void f_join __ARGS((typval_T *argvars, typval_T *rettv)); 560 static void f_keys __ARGS((typval_T *argvars, typval_T *rettv)); 561 static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv)); 562 static void f_len __ARGS((typval_T *argvars, typval_T *rettv)); 563 static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv)); 564 static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv)); 565 static void f_line __ARGS((typval_T *argvars, typval_T *rettv)); 566 static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv)); 567 static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv)); 568 static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv)); 569 static void f_map __ARGS((typval_T *argvars, typval_T *rettv)); 570 static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv)); 571 static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv)); 572 static void f_match __ARGS((typval_T *argvars, typval_T *rettv)); 573 static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv)); 574 static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv)); 575 static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv)); 576 static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv)); 577 static void f_max __ARGS((typval_T *argvars, typval_T *rettv)); 578 static void f_min __ARGS((typval_T *argvars, typval_T *rettv)); 579 #ifdef vim_mkdir 580 static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv)); 581 #endif 582 static void f_mode __ARGS((typval_T *argvars, typval_T *rettv)); 583 static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv)); 584 static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv)); 585 static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv)); 586 static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv)); 587 static void f_printf __ARGS((typval_T *argvars, typval_T *rettv)); 588 static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv)); 589 static void f_range __ARGS((typval_T *argvars, typval_T *rettv)); 590 static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv)); 591 static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv)); 592 static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv)); 593 static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv)); 594 static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv)); 595 static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv)); 596 static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv)); 597 static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv)); 598 static void f_remove __ARGS((typval_T *argvars, typval_T *rettv)); 599 static void f_rename __ARGS((typval_T *argvars, typval_T *rettv)); 600 static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv)); 601 static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv)); 602 static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv)); 603 static void f_search __ARGS((typval_T *argvars, typval_T *rettv)); 604 static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv)); 605 static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv)); 606 static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv)); 607 static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv)); 608 static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv)); 609 static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv)); 610 static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv)); 611 static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv)); 612 static void f_setline __ARGS((typval_T *argvars, typval_T *rettv)); 613 static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv)); 614 static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv)); 615 static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv)); 616 static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv)); 617 static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv)); 618 static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv)); 619 static void f_sort __ARGS((typval_T *argvars, typval_T *rettv)); 620 static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv)); 621 static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv)); 622 static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv)); 623 static void f_split __ARGS((typval_T *argvars, typval_T *rettv)); 624 static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv)); 625 #ifdef HAVE_STRFTIME 626 static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv)); 627 #endif 628 static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv)); 629 static void f_string __ARGS((typval_T *argvars, typval_T *rettv)); 630 static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv)); 631 static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv)); 632 static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv)); 633 static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv)); 634 static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv)); 635 static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv)); 636 static void f_synID __ARGS((typval_T *argvars, typval_T *rettv)); 637 static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv)); 638 static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv)); 639 static void f_system __ARGS((typval_T *argvars, typval_T *rettv)); 640 static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv)); 641 static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv)); 642 static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv)); 643 static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv)); 644 static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv)); 645 static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv)); 646 static void f_test __ARGS((typval_T *argvars, typval_T *rettv)); 647 static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv)); 648 static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv)); 649 static void f_tr __ARGS((typval_T *argvars, typval_T *rettv)); 650 static void f_type __ARGS((typval_T *argvars, typval_T *rettv)); 651 static void f_values __ARGS((typval_T *argvars, typval_T *rettv)); 652 static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv)); 653 static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv)); 654 static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv)); 655 static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv)); 656 static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv)); 657 static void f_winline __ARGS((typval_T *argvars, typval_T *rettv)); 658 static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv)); 659 static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv)); 660 static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv)); 661 static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv)); 662 static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv)); 663 static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv)); 664 665 static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump)); 666 static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum)); 667 static int get_env_len __ARGS((char_u **arg)); 668 static int get_id_len __ARGS((char_u **arg)); 669 static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose)); 670 static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags)); 671 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */ 672 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with 673 valid character */ 674 static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end)); 675 static int eval_isnamec __ARGS((int c)); 676 static int eval_isnamec1 __ARGS((int c)); 677 static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose)); 678 static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose)); 679 static typval_T *alloc_tv __ARGS((void)); 680 static typval_T *alloc_string_tv __ARGS((char_u *string)); 681 static void init_tv __ARGS((typval_T *varp)); 682 static long get_tv_number __ARGS((typval_T *varp)); 683 static linenr_T get_tv_lnum __ARGS((typval_T *argvars)); 684 static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf)); 685 static char_u *get_tv_string __ARGS((typval_T *varp)); 686 static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf)); 687 static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf)); 688 static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp)); 689 static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing)); 690 static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname)); 691 static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val)); 692 static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi)); 693 static void list_one_var __ARGS((dictitem_T *v, char_u *prefix)); 694 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string)); 695 static void set_var __ARGS((char_u *name, typval_T *varp, int copy)); 696 static int var_check_ro __ARGS((int flags, char_u *name)); 697 static int tv_check_lock __ARGS((int lock, char_u *name)); 698 static void copy_tv __ARGS((typval_T *from, typval_T *to)); 699 static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID)); 700 static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags)); 701 static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd)); 702 static int eval_fname_script __ARGS((char_u *p)); 703 static int eval_fname_sid __ARGS((char_u *p)); 704 static void list_func_head __ARGS((ufunc_T *fp, int indent)); 705 static ufunc_T *find_func __ARGS((char_u *name)); 706 static int function_exists __ARGS((char_u *name)); 707 static int builtin_function __ARGS((char_u *name)); 708 #ifdef FEAT_PROFILE 709 static void func_do_profile __ARGS((ufunc_T *fp)); 710 static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self)); 711 static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self)); 712 static int 713 # ifdef __BORLANDC__ 714 _RTLENTRYF 715 # endif 716 prof_total_cmp __ARGS((const void *s1, const void *s2)); 717 static int 718 # ifdef __BORLANDC__ 719 _RTLENTRYF 720 # endif 721 prof_self_cmp __ARGS((const void *s1, const void *s2)); 722 #endif 723 static int script_autoload __ARGS((char_u *name, int reload)); 724 static char_u *autoload_name __ARGS((char_u *name)); 725 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); 726 static void func_free __ARGS((ufunc_T *fp)); 727 static void func_unref __ARGS((char_u *name)); 728 static void func_ref __ARGS((char_u *name)); 729 static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict)); 730 static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr)); 731 static win_T *find_win_by_nr __ARGS((typval_T *vp)); 732 static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos)); 733 static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp)); 734 735 /* Character used as separated in autoload function/variable names. */ 736 #define AUTOLOAD_CHAR '#' 737 738 /* 739 * Initialize the global and v: variables. 740 */ 741 void 742 eval_init() 743 { 744 int i; 745 struct vimvar *p; 746 747 init_var_dict(&globvardict, &globvars_var); 748 init_var_dict(&vimvardict, &vimvars_var); 749 hash_init(&compat_hashtab); 750 hash_init(&func_hashtab); 751 752 for (i = 0; i < VV_LEN; ++i) 753 { 754 p = &vimvars[i]; 755 STRCPY(p->vv_di.di_key, p->vv_name); 756 if (p->vv_flags & VV_RO) 757 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 758 else if (p->vv_flags & VV_RO_SBX) 759 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX; 760 else 761 p->vv_di.di_flags = DI_FLAGS_FIX; 762 763 /* add to v: scope dict, unless the value is not always available */ 764 if (p->vv_type != VAR_UNKNOWN) 765 hash_add(&vimvarht, p->vv_di.di_key); 766 if (p->vv_flags & VV_COMPAT) 767 /* add to compat scope dict */ 768 hash_add(&compat_hashtab, p->vv_di.di_key); 769 } 770 } 771 772 #if defined(EXITFREE) || defined(PROTO) 773 void 774 eval_clear() 775 { 776 int i; 777 struct vimvar *p; 778 779 for (i = 0; i < VV_LEN; ++i) 780 { 781 p = &vimvars[i]; 782 if (p->vv_di.di_tv.v_type == VAR_STRING) 783 { 784 vim_free(p->vv_di.di_tv.vval.v_string); 785 p->vv_di.di_tv.vval.v_string = NULL; 786 } 787 } 788 hash_clear(&vimvarht); 789 hash_clear(&compat_hashtab); 790 791 /* script-local variables */ 792 for (i = 1; i <= ga_scripts.ga_len; ++i) 793 vars_clear(&SCRIPT_VARS(i)); 794 ga_clear(&ga_scripts); 795 free_scriptnames(); 796 797 /* global variables */ 798 vars_clear(&globvarht); 799 800 /* functions */ 801 free_all_functions(); 802 hash_clear(&func_hashtab); 803 804 /* unreferenced lists and dicts */ 805 (void)garbage_collect(); 806 } 807 #endif 808 809 /* 810 * Return the name of the executed function. 811 */ 812 char_u * 813 func_name(cookie) 814 void *cookie; 815 { 816 return ((funccall_T *)cookie)->func->uf_name; 817 } 818 819 /* 820 * Return the address holding the next breakpoint line for a funccall cookie. 821 */ 822 linenr_T * 823 func_breakpoint(cookie) 824 void *cookie; 825 { 826 return &((funccall_T *)cookie)->breakpoint; 827 } 828 829 /* 830 * Return the address holding the debug tick for a funccall cookie. 831 */ 832 int * 833 func_dbg_tick(cookie) 834 void *cookie; 835 { 836 return &((funccall_T *)cookie)->dbg_tick; 837 } 838 839 /* 840 * Return the nesting level for a funccall cookie. 841 */ 842 int 843 func_level(cookie) 844 void *cookie; 845 { 846 return ((funccall_T *)cookie)->level; 847 } 848 849 /* pointer to funccal for currently active function */ 850 funccall_T *current_funccal = NULL; 851 852 /* 853 * Return TRUE when a function was ended by a ":return" command. 854 */ 855 int 856 current_func_returned() 857 { 858 return current_funccal->returned; 859 } 860 861 862 /* 863 * Set an internal variable to a string value. Creates the variable if it does 864 * not already exist. 865 */ 866 void 867 set_internal_string_var(name, value) 868 char_u *name; 869 char_u *value; 870 { 871 char_u *val; 872 typval_T *tvp; 873 874 val = vim_strsave(value); 875 if (val != NULL) 876 { 877 tvp = alloc_string_tv(val); 878 if (tvp != NULL) 879 { 880 set_var(name, tvp, FALSE); 881 free_tv(tvp); 882 } 883 } 884 } 885 886 static lval_T *redir_lval = NULL; 887 static char_u *redir_endp = NULL; 888 static char_u *redir_varname = NULL; 889 890 /* 891 * Start recording command output to a variable 892 * Returns OK if successfully completed the setup. FAIL otherwise. 893 */ 894 int 895 var_redir_start(name, append) 896 char_u *name; 897 int append; /* append to an existing variable */ 898 { 899 int save_emsg; 900 int err; 901 typval_T tv; 902 903 /* Make sure a valid variable name is specified */ 904 if (!eval_isnamec1(*name)) 905 { 906 EMSG(_(e_invarg)); 907 return FAIL; 908 } 909 910 redir_varname = vim_strsave(name); 911 if (redir_varname == NULL) 912 return FAIL; 913 914 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T)); 915 if (redir_lval == NULL) 916 { 917 var_redir_stop(); 918 return FAIL; 919 } 920 921 /* Parse the variable name (can be a dict or list entry). */ 922 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE, 923 FNE_CHECK_START); 924 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL) 925 { 926 if (redir_endp != NULL && *redir_endp != NUL) 927 /* Trailing characters are present after the variable name */ 928 EMSG(_(e_trailing)); 929 else 930 EMSG(_(e_invarg)); 931 var_redir_stop(); 932 return FAIL; 933 } 934 935 /* check if we can write to the variable: set it to or append an empty 936 * string */ 937 save_emsg = did_emsg; 938 did_emsg = FALSE; 939 tv.v_type = VAR_STRING; 940 tv.vval.v_string = (char_u *)""; 941 if (append) 942 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"."); 943 else 944 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"="); 945 err = did_emsg; 946 did_emsg |= save_emsg; 947 if (err) 948 { 949 var_redir_stop(); 950 return FAIL; 951 } 952 if (redir_lval->ll_newkey != NULL) 953 { 954 /* Dictionary item was created, don't do it again. */ 955 vim_free(redir_lval->ll_newkey); 956 redir_lval->ll_newkey = NULL; 957 } 958 959 return OK; 960 } 961 962 /* 963 * Append "value[len]" to the variable set by var_redir_start(). 964 */ 965 void 966 var_redir_str(value, len) 967 char_u *value; 968 int len; 969 { 970 char_u *val; 971 typval_T tv; 972 int save_emsg; 973 int err; 974 975 if (redir_lval == NULL) 976 return; 977 978 if (len == -1) 979 /* Append the entire string */ 980 val = vim_strsave(value); 981 else 982 /* Append only the specified number of characters */ 983 val = vim_strnsave(value, len); 984 if (val == NULL) 985 return; 986 987 tv.v_type = VAR_STRING; 988 tv.vval.v_string = val; 989 990 save_emsg = did_emsg; 991 did_emsg = FALSE; 992 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)"."); 993 err = did_emsg; 994 did_emsg |= save_emsg; 995 if (err) 996 var_redir_stop(); 997 998 vim_free(tv.vval.v_string); 999 } 1000 1001 /* 1002 * Stop redirecting command output to a variable. 1003 */ 1004 void 1005 var_redir_stop() 1006 { 1007 if (redir_lval != NULL) 1008 { 1009 clear_lval(redir_lval); 1010 vim_free(redir_lval); 1011 redir_lval = NULL; 1012 } 1013 vim_free(redir_varname); 1014 redir_varname = NULL; 1015 } 1016 1017 # if defined(FEAT_MBYTE) || defined(PROTO) 1018 int 1019 eval_charconvert(enc_from, enc_to, fname_from, fname_to) 1020 char_u *enc_from; 1021 char_u *enc_to; 1022 char_u *fname_from; 1023 char_u *fname_to; 1024 { 1025 int err = FALSE; 1026 1027 set_vim_var_string(VV_CC_FROM, enc_from, -1); 1028 set_vim_var_string(VV_CC_TO, enc_to, -1); 1029 set_vim_var_string(VV_FNAME_IN, fname_from, -1); 1030 set_vim_var_string(VV_FNAME_OUT, fname_to, -1); 1031 if (eval_to_bool(p_ccv, &err, NULL, FALSE)) 1032 err = TRUE; 1033 set_vim_var_string(VV_CC_FROM, NULL, -1); 1034 set_vim_var_string(VV_CC_TO, NULL, -1); 1035 set_vim_var_string(VV_FNAME_IN, NULL, -1); 1036 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 1037 1038 if (err) 1039 return FAIL; 1040 return OK; 1041 } 1042 # endif 1043 1044 # if defined(FEAT_POSTSCRIPT) || defined(PROTO) 1045 int 1046 eval_printexpr(fname, args) 1047 char_u *fname; 1048 char_u *args; 1049 { 1050 int err = FALSE; 1051 1052 set_vim_var_string(VV_FNAME_IN, fname, -1); 1053 set_vim_var_string(VV_CMDARG, args, -1); 1054 if (eval_to_bool(p_pexpr, &err, NULL, FALSE)) 1055 err = TRUE; 1056 set_vim_var_string(VV_FNAME_IN, NULL, -1); 1057 set_vim_var_string(VV_CMDARG, NULL, -1); 1058 1059 if (err) 1060 { 1061 mch_remove(fname); 1062 return FAIL; 1063 } 1064 return OK; 1065 } 1066 # endif 1067 1068 # if defined(FEAT_DIFF) || defined(PROTO) 1069 void 1070 eval_diff(origfile, newfile, outfile) 1071 char_u *origfile; 1072 char_u *newfile; 1073 char_u *outfile; 1074 { 1075 int err = FALSE; 1076 1077 set_vim_var_string(VV_FNAME_IN, origfile, -1); 1078 set_vim_var_string(VV_FNAME_NEW, newfile, -1); 1079 set_vim_var_string(VV_FNAME_OUT, outfile, -1); 1080 (void)eval_to_bool(p_dex, &err, NULL, FALSE); 1081 set_vim_var_string(VV_FNAME_IN, NULL, -1); 1082 set_vim_var_string(VV_FNAME_NEW, NULL, -1); 1083 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 1084 } 1085 1086 void 1087 eval_patch(origfile, difffile, outfile) 1088 char_u *origfile; 1089 char_u *difffile; 1090 char_u *outfile; 1091 { 1092 int err; 1093 1094 set_vim_var_string(VV_FNAME_IN, origfile, -1); 1095 set_vim_var_string(VV_FNAME_DIFF, difffile, -1); 1096 set_vim_var_string(VV_FNAME_OUT, outfile, -1); 1097 (void)eval_to_bool(p_pex, &err, NULL, FALSE); 1098 set_vim_var_string(VV_FNAME_IN, NULL, -1); 1099 set_vim_var_string(VV_FNAME_DIFF, NULL, -1); 1100 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 1101 } 1102 # endif 1103 1104 /* 1105 * Top level evaluation function, returning a boolean. 1106 * Sets "error" to TRUE if there was an error. 1107 * Return TRUE or FALSE. 1108 */ 1109 int 1110 eval_to_bool(arg, error, nextcmd, skip) 1111 char_u *arg; 1112 int *error; 1113 char_u **nextcmd; 1114 int skip; /* only parse, don't execute */ 1115 { 1116 typval_T tv; 1117 int retval = FALSE; 1118 1119 if (skip) 1120 ++emsg_skip; 1121 if (eval0(arg, &tv, nextcmd, !skip) == FAIL) 1122 *error = TRUE; 1123 else 1124 { 1125 *error = FALSE; 1126 if (!skip) 1127 { 1128 retval = (get_tv_number_chk(&tv, error) != 0); 1129 clear_tv(&tv); 1130 } 1131 } 1132 if (skip) 1133 --emsg_skip; 1134 1135 return retval; 1136 } 1137 1138 /* 1139 * Top level evaluation function, returning a string. If "skip" is TRUE, 1140 * only parsing to "nextcmd" is done, without reporting errors. Return 1141 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE. 1142 */ 1143 char_u * 1144 eval_to_string_skip(arg, nextcmd, skip) 1145 char_u *arg; 1146 char_u **nextcmd; 1147 int skip; /* only parse, don't execute */ 1148 { 1149 typval_T tv; 1150 char_u *retval; 1151 1152 if (skip) 1153 ++emsg_skip; 1154 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip) 1155 retval = NULL; 1156 else 1157 { 1158 retval = vim_strsave(get_tv_string(&tv)); 1159 clear_tv(&tv); 1160 } 1161 if (skip) 1162 --emsg_skip; 1163 1164 return retval; 1165 } 1166 1167 /* 1168 * Skip over an expression at "*pp". 1169 * Return FAIL for an error, OK otherwise. 1170 */ 1171 int 1172 skip_expr(pp) 1173 char_u **pp; 1174 { 1175 typval_T rettv; 1176 1177 *pp = skipwhite(*pp); 1178 return eval1(pp, &rettv, FALSE); 1179 } 1180 1181 /* 1182 * Top level evaluation function, returning a string. 1183 * Return pointer to allocated memory, or NULL for failure. 1184 */ 1185 char_u * 1186 eval_to_string(arg, nextcmd, dolist) 1187 char_u *arg; 1188 char_u **nextcmd; 1189 int dolist; /* turn List into sequence of lines */ 1190 { 1191 typval_T tv; 1192 char_u *retval; 1193 garray_T ga; 1194 1195 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL) 1196 retval = NULL; 1197 else 1198 { 1199 if (dolist && tv.v_type == VAR_LIST) 1200 { 1201 ga_init2(&ga, (int)sizeof(char), 80); 1202 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0); 1203 ga_append(&ga, NUL); 1204 retval = (char_u *)ga.ga_data; 1205 } 1206 else 1207 retval = vim_strsave(get_tv_string(&tv)); 1208 clear_tv(&tv); 1209 } 1210 1211 return retval; 1212 } 1213 1214 /* 1215 * Call eval_to_string() without using current local variables and using 1216 * textlock. When "use_sandbox" is TRUE use the sandbox. 1217 */ 1218 char_u * 1219 eval_to_string_safe(arg, nextcmd, use_sandbox) 1220 char_u *arg; 1221 char_u **nextcmd; 1222 int use_sandbox; 1223 { 1224 char_u *retval; 1225 void *save_funccalp; 1226 1227 save_funccalp = save_funccal(); 1228 if (use_sandbox) 1229 ++sandbox; 1230 ++textlock; 1231 retval = eval_to_string(arg, nextcmd, FALSE); 1232 if (use_sandbox) 1233 --sandbox; 1234 --textlock; 1235 restore_funccal(save_funccalp); 1236 return retval; 1237 } 1238 1239 /* 1240 * Top level evaluation function, returning a number. 1241 * Evaluates "expr" silently. 1242 * Returns -1 for an error. 1243 */ 1244 int 1245 eval_to_number(expr) 1246 char_u *expr; 1247 { 1248 typval_T rettv; 1249 int retval; 1250 char_u *p = skipwhite(expr); 1251 1252 ++emsg_off; 1253 1254 if (eval1(&p, &rettv, TRUE) == FAIL) 1255 retval = -1; 1256 else 1257 { 1258 retval = get_tv_number_chk(&rettv, NULL); 1259 clear_tv(&rettv); 1260 } 1261 --emsg_off; 1262 1263 return retval; 1264 } 1265 1266 /* 1267 * Prepare v: variable "idx" to be used. 1268 * Save the current typeval in "save_tv". 1269 * When not used yet add the variable to the v: hashtable. 1270 */ 1271 static void 1272 prepare_vimvar(idx, save_tv) 1273 int idx; 1274 typval_T *save_tv; 1275 { 1276 *save_tv = vimvars[idx].vv_tv; 1277 if (vimvars[idx].vv_type == VAR_UNKNOWN) 1278 hash_add(&vimvarht, vimvars[idx].vv_di.di_key); 1279 } 1280 1281 /* 1282 * Restore v: variable "idx" to typeval "save_tv". 1283 * When no longer defined, remove the variable from the v: hashtable. 1284 */ 1285 static void 1286 restore_vimvar(idx, save_tv) 1287 int idx; 1288 typval_T *save_tv; 1289 { 1290 hashitem_T *hi; 1291 1292 clear_tv(&vimvars[idx].vv_tv); 1293 vimvars[idx].vv_tv = *save_tv; 1294 if (vimvars[idx].vv_type == VAR_UNKNOWN) 1295 { 1296 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key); 1297 if (HASHITEM_EMPTY(hi)) 1298 EMSG2(_(e_intern2), "restore_vimvar()"); 1299 else 1300 hash_remove(&vimvarht, hi); 1301 } 1302 } 1303 1304 #if defined(FEAT_SPELL) || defined(PROTO) 1305 /* 1306 * Evaluate an expression to a list with suggestions. 1307 * For the "expr:" part of 'spellsuggest'. 1308 */ 1309 list_T * 1310 eval_spell_expr(badword, expr) 1311 char_u *badword; 1312 char_u *expr; 1313 { 1314 typval_T save_val; 1315 typval_T rettv; 1316 list_T *list = NULL; 1317 char_u *p = skipwhite(expr); 1318 1319 /* Set "v:val" to the bad word. */ 1320 prepare_vimvar(VV_VAL, &save_val); 1321 vimvars[VV_VAL].vv_type = VAR_STRING; 1322 vimvars[VV_VAL].vv_str = badword; 1323 if (p_verbose == 0) 1324 ++emsg_off; 1325 1326 if (eval1(&p, &rettv, TRUE) == OK) 1327 { 1328 if (rettv.v_type != VAR_LIST) 1329 clear_tv(&rettv); 1330 else 1331 list = rettv.vval.v_list; 1332 } 1333 1334 if (p_verbose == 0) 1335 --emsg_off; 1336 vimvars[VV_VAL].vv_str = NULL; 1337 restore_vimvar(VV_VAL, &save_val); 1338 1339 return list; 1340 } 1341 1342 /* 1343 * "list" is supposed to contain two items: a word and a number. Return the 1344 * word in "pp" and the number as the return value. 1345 * Return -1 if anything isn't right. 1346 * Used to get the good word and score from the eval_spell_expr() result. 1347 */ 1348 int 1349 get_spellword(list, pp) 1350 list_T *list; 1351 char_u **pp; 1352 { 1353 listitem_T *li; 1354 1355 li = list->lv_first; 1356 if (li == NULL) 1357 return -1; 1358 *pp = get_tv_string(&li->li_tv); 1359 1360 li = li->li_next; 1361 if (li == NULL) 1362 return -1; 1363 return get_tv_number(&li->li_tv); 1364 } 1365 #endif 1366 1367 /* 1368 * Top level evaluation function. 1369 * Returns an allocated typval_T with the result. 1370 * Returns NULL when there is an error. 1371 */ 1372 typval_T * 1373 eval_expr(arg, nextcmd) 1374 char_u *arg; 1375 char_u **nextcmd; 1376 { 1377 typval_T *tv; 1378 1379 tv = (typval_T *)alloc(sizeof(typval_T)); 1380 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL) 1381 { 1382 vim_free(tv); 1383 tv = NULL; 1384 } 1385 1386 return tv; 1387 } 1388 1389 1390 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO) 1391 /* 1392 * Call some vimL function and return the result in "*rettv". 1393 * Uses argv[argc] for the function arguments. 1394 * Returns OK or FAIL. 1395 */ 1396 static int 1397 call_vim_function(func, argc, argv, safe, rettv) 1398 char_u *func; 1399 int argc; 1400 char_u **argv; 1401 int safe; /* use the sandbox */ 1402 typval_T *rettv; 1403 { 1404 typval_T *argvars; 1405 long n; 1406 int len; 1407 int i; 1408 int doesrange; 1409 void *save_funccalp = NULL; 1410 int ret; 1411 1412 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T))); 1413 if (argvars == NULL) 1414 return FAIL; 1415 1416 for (i = 0; i < argc; i++) 1417 { 1418 /* Pass a NULL or empty argument as an empty string */ 1419 if (argv[i] == NULL || *argv[i] == NUL) 1420 { 1421 argvars[i].v_type = VAR_STRING; 1422 argvars[i].vval.v_string = (char_u *)""; 1423 continue; 1424 } 1425 1426 /* Recognize a number argument, the others must be strings. */ 1427 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL); 1428 if (len != 0 && len == (int)STRLEN(argv[i])) 1429 { 1430 argvars[i].v_type = VAR_NUMBER; 1431 argvars[i].vval.v_number = n; 1432 } 1433 else 1434 { 1435 argvars[i].v_type = VAR_STRING; 1436 argvars[i].vval.v_string = argv[i]; 1437 } 1438 } 1439 1440 if (safe) 1441 { 1442 save_funccalp = save_funccal(); 1443 ++sandbox; 1444 } 1445 1446 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 1447 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, 1448 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 1449 &doesrange, TRUE, NULL); 1450 if (safe) 1451 { 1452 --sandbox; 1453 restore_funccal(save_funccalp); 1454 } 1455 vim_free(argvars); 1456 1457 if (ret == FAIL) 1458 clear_tv(rettv); 1459 1460 return ret; 1461 } 1462 1463 /* 1464 * Call vimL function "func" and return the result as a string. 1465 * Returns NULL when calling the function fails. 1466 * Uses argv[argc] for the function arguments. 1467 */ 1468 void * 1469 call_func_retstr(func, argc, argv, safe) 1470 char_u *func; 1471 int argc; 1472 char_u **argv; 1473 int safe; /* use the sandbox */ 1474 { 1475 typval_T rettv; 1476 char_u *retval; 1477 1478 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL) 1479 return NULL; 1480 1481 retval = vim_strsave(get_tv_string(&rettv)); 1482 clear_tv(&rettv); 1483 return retval; 1484 } 1485 1486 #if defined(FEAT_COMPL_FUNC) || defined(PROTO) 1487 /* 1488 * Call vimL function "func" and return the result as a number. 1489 * Returns -1 when calling the function fails. 1490 * Uses argv[argc] for the function arguments. 1491 */ 1492 long 1493 call_func_retnr(func, argc, argv, safe) 1494 char_u *func; 1495 int argc; 1496 char_u **argv; 1497 int safe; /* use the sandbox */ 1498 { 1499 typval_T rettv; 1500 long retval; 1501 1502 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL) 1503 return -1; 1504 1505 retval = get_tv_number_chk(&rettv, NULL); 1506 clear_tv(&rettv); 1507 return retval; 1508 } 1509 #endif 1510 1511 /* 1512 * Call vimL function "func" and return the result as a list 1513 * Uses argv[argc] for the function arguments. 1514 */ 1515 void * 1516 call_func_retlist(func, argc, argv, safe) 1517 char_u *func; 1518 int argc; 1519 char_u **argv; 1520 int safe; /* use the sandbox */ 1521 { 1522 typval_T rettv; 1523 1524 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL) 1525 return NULL; 1526 1527 if (rettv.v_type != VAR_LIST) 1528 { 1529 clear_tv(&rettv); 1530 return NULL; 1531 } 1532 1533 return rettv.vval.v_list; 1534 } 1535 1536 #endif 1537 1538 /* 1539 * Save the current function call pointer, and set it to NULL. 1540 * Used when executing autocommands and for ":source". 1541 */ 1542 void * 1543 save_funccal() 1544 { 1545 funccall_T *fc = current_funccal; 1546 1547 current_funccal = NULL; 1548 return (void *)fc; 1549 } 1550 1551 void 1552 restore_funccal(vfc) 1553 void *vfc; 1554 { 1555 funccall_T *fc = (funccall_T *)vfc; 1556 1557 current_funccal = fc; 1558 } 1559 1560 #if defined(FEAT_PROFILE) || defined(PROTO) 1561 /* 1562 * Prepare profiling for entering a child or something else that is not 1563 * counted for the script/function itself. 1564 * Should always be called in pair with prof_child_exit(). 1565 */ 1566 void 1567 prof_child_enter(tm) 1568 proftime_T *tm; /* place to store waittime */ 1569 { 1570 funccall_T *fc = current_funccal; 1571 1572 if (fc != NULL && fc->func->uf_profiling) 1573 profile_start(&fc->prof_child); 1574 script_prof_save(tm); 1575 } 1576 1577 /* 1578 * Take care of time spent in a child. 1579 * Should always be called after prof_child_enter(). 1580 */ 1581 void 1582 prof_child_exit(tm) 1583 proftime_T *tm; /* where waittime was stored */ 1584 { 1585 funccall_T *fc = current_funccal; 1586 1587 if (fc != NULL && fc->func->uf_profiling) 1588 { 1589 profile_end(&fc->prof_child); 1590 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */ 1591 profile_add(&fc->func->uf_tm_children, &fc->prof_child); 1592 profile_add(&fc->func->uf_tml_children, &fc->prof_child); 1593 } 1594 script_prof_restore(tm); 1595 } 1596 #endif 1597 1598 1599 #ifdef FEAT_FOLDING 1600 /* 1601 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding 1602 * it in "*cp". Doesn't give error messages. 1603 */ 1604 int 1605 eval_foldexpr(arg, cp) 1606 char_u *arg; 1607 int *cp; 1608 { 1609 typval_T tv; 1610 int retval; 1611 char_u *s; 1612 int use_sandbox = was_set_insecurely((char_u *)"foldexpr", 1613 OPT_LOCAL); 1614 1615 ++emsg_off; 1616 if (use_sandbox) 1617 ++sandbox; 1618 ++textlock; 1619 *cp = NUL; 1620 if (eval0(arg, &tv, NULL, TRUE) == FAIL) 1621 retval = 0; 1622 else 1623 { 1624 /* If the result is a number, just return the number. */ 1625 if (tv.v_type == VAR_NUMBER) 1626 retval = tv.vval.v_number; 1627 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL) 1628 retval = 0; 1629 else 1630 { 1631 /* If the result is a string, check if there is a non-digit before 1632 * the number. */ 1633 s = tv.vval.v_string; 1634 if (!VIM_ISDIGIT(*s) && *s != '-') 1635 *cp = *s++; 1636 retval = atol((char *)s); 1637 } 1638 clear_tv(&tv); 1639 } 1640 --emsg_off; 1641 if (use_sandbox) 1642 --sandbox; 1643 --textlock; 1644 1645 return retval; 1646 } 1647 #endif 1648 1649 /* 1650 * ":let" list all variable values 1651 * ":let var1 var2" list variable values 1652 * ":let var = expr" assignment command. 1653 * ":let var += expr" assignment command. 1654 * ":let var -= expr" assignment command. 1655 * ":let var .= expr" assignment command. 1656 * ":let [var1, var2] = expr" unpack list. 1657 */ 1658 void 1659 ex_let(eap) 1660 exarg_T *eap; 1661 { 1662 char_u *arg = eap->arg; 1663 char_u *expr = NULL; 1664 typval_T rettv; 1665 int i; 1666 int var_count = 0; 1667 int semicolon = 0; 1668 char_u op[2]; 1669 char_u *argend; 1670 1671 argend = skip_var_list(arg, &var_count, &semicolon); 1672 if (argend == NULL) 1673 return; 1674 if (argend > arg && argend[-1] == '.') /* for var.='str' */ 1675 --argend; 1676 expr = vim_strchr(argend, '='); 1677 if (expr == NULL) 1678 { 1679 /* 1680 * ":let" without "=": list variables 1681 */ 1682 if (*arg == '[') 1683 EMSG(_(e_invarg)); 1684 else if (!ends_excmd(*arg)) 1685 /* ":let var1 var2" */ 1686 arg = list_arg_vars(eap, arg); 1687 else if (!eap->skip) 1688 { 1689 /* ":let" */ 1690 list_glob_vars(); 1691 list_buf_vars(); 1692 list_win_vars(); 1693 #ifdef FEAT_WINDOWS 1694 list_tab_vars(); 1695 #endif 1696 list_script_vars(); 1697 list_func_vars(); 1698 list_vim_vars(); 1699 } 1700 eap->nextcmd = check_nextcmd(arg); 1701 } 1702 else 1703 { 1704 op[0] = '='; 1705 op[1] = NUL; 1706 if (expr > argend) 1707 { 1708 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL) 1709 op[0] = expr[-1]; /* +=, -= or .= */ 1710 } 1711 expr = skipwhite(expr + 1); 1712 1713 if (eap->skip) 1714 ++emsg_skip; 1715 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip); 1716 if (eap->skip) 1717 { 1718 if (i != FAIL) 1719 clear_tv(&rettv); 1720 --emsg_skip; 1721 } 1722 else if (i != FAIL) 1723 { 1724 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count, 1725 op); 1726 clear_tv(&rettv); 1727 } 1728 } 1729 } 1730 1731 /* 1732 * Assign the typevalue "tv" to the variable or variables at "arg_start". 1733 * Handles both "var" with any type and "[var, var; var]" with a list type. 1734 * When "nextchars" is not NULL it points to a string with characters that 1735 * must appear after the variable(s). Use "+", "-" or "." for add, subtract 1736 * or concatenate. 1737 * Returns OK or FAIL; 1738 */ 1739 static int 1740 ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars) 1741 char_u *arg_start; 1742 typval_T *tv; 1743 int copy; /* copy values from "tv", don't move */ 1744 int semicolon; /* from skip_var_list() */ 1745 int var_count; /* from skip_var_list() */ 1746 char_u *nextchars; 1747 { 1748 char_u *arg = arg_start; 1749 list_T *l; 1750 int i; 1751 listitem_T *item; 1752 typval_T ltv; 1753 1754 if (*arg != '[') 1755 { 1756 /* 1757 * ":let var = expr" or ":for var in list" 1758 */ 1759 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL) 1760 return FAIL; 1761 return OK; 1762 } 1763 1764 /* 1765 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist" 1766 */ 1767 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL) 1768 { 1769 EMSG(_(e_listreq)); 1770 return FAIL; 1771 } 1772 1773 i = list_len(l); 1774 if (semicolon == 0 && var_count < i) 1775 { 1776 EMSG(_("E687: Less targets than List items")); 1777 return FAIL; 1778 } 1779 if (var_count - semicolon > i) 1780 { 1781 EMSG(_("E688: More targets than List items")); 1782 return FAIL; 1783 } 1784 1785 item = l->lv_first; 1786 while (*arg != ']') 1787 { 1788 arg = skipwhite(arg + 1); 1789 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars); 1790 item = item->li_next; 1791 if (arg == NULL) 1792 return FAIL; 1793 1794 arg = skipwhite(arg); 1795 if (*arg == ';') 1796 { 1797 /* Put the rest of the list (may be empty) in the var after ';'. 1798 * Create a new list for this. */ 1799 l = list_alloc(); 1800 if (l == NULL) 1801 return FAIL; 1802 while (item != NULL) 1803 { 1804 list_append_tv(l, &item->li_tv); 1805 item = item->li_next; 1806 } 1807 1808 ltv.v_type = VAR_LIST; 1809 ltv.v_lock = 0; 1810 ltv.vval.v_list = l; 1811 l->lv_refcount = 1; 1812 1813 arg = ex_let_one(skipwhite(arg + 1), <v, FALSE, 1814 (char_u *)"]", nextchars); 1815 clear_tv(<v); 1816 if (arg == NULL) 1817 return FAIL; 1818 break; 1819 } 1820 else if (*arg != ',' && *arg != ']') 1821 { 1822 EMSG2(_(e_intern2), "ex_let_vars()"); 1823 return FAIL; 1824 } 1825 } 1826 1827 return OK; 1828 } 1829 1830 /* 1831 * Skip over assignable variable "var" or list of variables "[var, var]". 1832 * Used for ":let varvar = expr" and ":for varvar in expr". 1833 * For "[var, var]" increment "*var_count" for each variable. 1834 * for "[var, var; var]" set "semicolon". 1835 * Return NULL for an error. 1836 */ 1837 static char_u * 1838 skip_var_list(arg, var_count, semicolon) 1839 char_u *arg; 1840 int *var_count; 1841 int *semicolon; 1842 { 1843 char_u *p, *s; 1844 1845 if (*arg == '[') 1846 { 1847 /* "[var, var]": find the matching ']'. */ 1848 p = arg; 1849 for (;;) 1850 { 1851 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */ 1852 s = skip_var_one(p); 1853 if (s == p) 1854 { 1855 EMSG2(_(e_invarg2), p); 1856 return NULL; 1857 } 1858 ++*var_count; 1859 1860 p = skipwhite(s); 1861 if (*p == ']') 1862 break; 1863 else if (*p == ';') 1864 { 1865 if (*semicolon == 1) 1866 { 1867 EMSG(_("Double ; in list of variables")); 1868 return NULL; 1869 } 1870 *semicolon = 1; 1871 } 1872 else if (*p != ',') 1873 { 1874 EMSG2(_(e_invarg2), p); 1875 return NULL; 1876 } 1877 } 1878 return p + 1; 1879 } 1880 else 1881 return skip_var_one(arg); 1882 } 1883 1884 /* 1885 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key, 1886 * l[idx]. 1887 */ 1888 static char_u * 1889 skip_var_one(arg) 1890 char_u *arg; 1891 { 1892 if (*arg == '@' && arg[1] != NUL) 1893 return arg + 2; 1894 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg, 1895 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); 1896 } 1897 1898 /* 1899 * List variables for hashtab "ht" with prefix "prefix". 1900 * If "empty" is TRUE also list NULL strings as empty strings. 1901 */ 1902 static void 1903 list_hashtable_vars(ht, prefix, empty) 1904 hashtab_T *ht; 1905 char_u *prefix; 1906 int empty; 1907 { 1908 hashitem_T *hi; 1909 dictitem_T *di; 1910 int todo; 1911 1912 todo = ht->ht_used; 1913 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi) 1914 { 1915 if (!HASHITEM_EMPTY(hi)) 1916 { 1917 --todo; 1918 di = HI2DI(hi); 1919 if (empty || di->di_tv.v_type != VAR_STRING 1920 || di->di_tv.vval.v_string != NULL) 1921 list_one_var(di, prefix); 1922 } 1923 } 1924 } 1925 1926 /* 1927 * List global variables. 1928 */ 1929 static void 1930 list_glob_vars() 1931 { 1932 list_hashtable_vars(&globvarht, (char_u *)"", TRUE); 1933 } 1934 1935 /* 1936 * List buffer variables. 1937 */ 1938 static void 1939 list_buf_vars() 1940 { 1941 char_u numbuf[NUMBUFLEN]; 1942 1943 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE); 1944 1945 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick); 1946 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf); 1947 } 1948 1949 /* 1950 * List window variables. 1951 */ 1952 static void 1953 list_win_vars() 1954 { 1955 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE); 1956 } 1957 1958 #ifdef FEAT_WINDOWS 1959 /* 1960 * List tab page variables. 1961 */ 1962 static void 1963 list_tab_vars() 1964 { 1965 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE); 1966 } 1967 #endif 1968 1969 /* 1970 * List Vim variables. 1971 */ 1972 static void 1973 list_vim_vars() 1974 { 1975 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE); 1976 } 1977 1978 /* 1979 * List script-local variables, if there is a script. 1980 */ 1981 static void 1982 list_script_vars() 1983 { 1984 if (current_SID > 0 && current_SID <= ga_scripts.ga_len) 1985 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE); 1986 } 1987 1988 /* 1989 * List function variables, if there is a function. 1990 */ 1991 static void 1992 list_func_vars() 1993 { 1994 if (current_funccal != NULL) 1995 list_hashtable_vars(¤t_funccal->l_vars.dv_hashtab, 1996 (char_u *)"l:", FALSE); 1997 } 1998 1999 /* 2000 * List variables in "arg". 2001 */ 2002 static char_u * 2003 list_arg_vars(eap, arg) 2004 exarg_T *eap; 2005 char_u *arg; 2006 { 2007 int error = FALSE; 2008 int len; 2009 char_u *name; 2010 char_u *name_start; 2011 char_u *arg_subsc; 2012 char_u *tofree; 2013 typval_T tv; 2014 2015 while (!ends_excmd(*arg) && !got_int) 2016 { 2017 if (error || eap->skip) 2018 { 2019 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); 2020 if (!vim_iswhite(*arg) && !ends_excmd(*arg)) 2021 { 2022 emsg_severe = TRUE; 2023 EMSG(_(e_trailing)); 2024 break; 2025 } 2026 } 2027 else 2028 { 2029 /* get_name_len() takes care of expanding curly braces */ 2030 name_start = name = arg; 2031 len = get_name_len(&arg, &tofree, TRUE, TRUE); 2032 if (len <= 0) 2033 { 2034 /* This is mainly to keep test 49 working: when expanding 2035 * curly braces fails overrule the exception error message. */ 2036 if (len < 0 && !aborting()) 2037 { 2038 emsg_severe = TRUE; 2039 EMSG2(_(e_invarg2), arg); 2040 break; 2041 } 2042 error = TRUE; 2043 } 2044 else 2045 { 2046 if (tofree != NULL) 2047 name = tofree; 2048 if (get_var_tv(name, len, &tv, TRUE) == FAIL) 2049 error = TRUE; 2050 else 2051 { 2052 /* handle d.key, l[idx], f(expr) */ 2053 arg_subsc = arg; 2054 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL) 2055 error = TRUE; 2056 else 2057 { 2058 if (arg == arg_subsc && len == 2 && name[1] == ':') 2059 { 2060 switch (*name) 2061 { 2062 case 'g': list_glob_vars(); break; 2063 case 'b': list_buf_vars(); break; 2064 case 'w': list_win_vars(); break; 2065 #ifdef FEAT_WINDOWS 2066 case 't': list_tab_vars(); break; 2067 #endif 2068 case 'v': list_vim_vars(); break; 2069 case 's': list_script_vars(); break; 2070 case 'l': list_func_vars(); break; 2071 default: 2072 EMSG2(_("E738: Can't list variables for %s"), name); 2073 } 2074 } 2075 else 2076 { 2077 char_u numbuf[NUMBUFLEN]; 2078 char_u *tf; 2079 int c; 2080 char_u *s; 2081 2082 s = echo_string(&tv, &tf, numbuf, 0); 2083 c = *arg; 2084 *arg = NUL; 2085 list_one_var_a((char_u *)"", 2086 arg == arg_subsc ? name : name_start, 2087 tv.v_type, s == NULL ? (char_u *)"" : s); 2088 *arg = c; 2089 vim_free(tf); 2090 } 2091 clear_tv(&tv); 2092 } 2093 } 2094 } 2095 2096 vim_free(tofree); 2097 } 2098 2099 arg = skipwhite(arg); 2100 } 2101 2102 return arg; 2103 } 2104 2105 /* 2106 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value. 2107 * Returns a pointer to the char just after the var name. 2108 * Returns NULL if there is an error. 2109 */ 2110 static char_u * 2111 ex_let_one(arg, tv, copy, endchars, op) 2112 char_u *arg; /* points to variable name */ 2113 typval_T *tv; /* value to assign to variable */ 2114 int copy; /* copy value from "tv" */ 2115 char_u *endchars; /* valid chars after variable name or NULL */ 2116 char_u *op; /* "+", "-", "." or NULL*/ 2117 { 2118 int c1; 2119 char_u *name; 2120 char_u *p; 2121 char_u *arg_end = NULL; 2122 int len; 2123 int opt_flags; 2124 char_u *tofree = NULL; 2125 2126 /* 2127 * ":let $VAR = expr": Set environment variable. 2128 */ 2129 if (*arg == '$') 2130 { 2131 /* Find the end of the name. */ 2132 ++arg; 2133 name = arg; 2134 len = get_env_len(&arg); 2135 if (len == 0) 2136 EMSG2(_(e_invarg2), name - 1); 2137 else 2138 { 2139 if (op != NULL && (*op == '+' || *op == '-')) 2140 EMSG2(_(e_letwrong), op); 2141 else if (endchars != NULL 2142 && vim_strchr(endchars, *skipwhite(arg)) == NULL) 2143 EMSG(_(e_letunexp)); 2144 else 2145 { 2146 c1 = name[len]; 2147 name[len] = NUL; 2148 p = get_tv_string_chk(tv); 2149 if (p != NULL && op != NULL && *op == '.') 2150 { 2151 int mustfree = FALSE; 2152 char_u *s = vim_getenv(name, &mustfree); 2153 2154 if (s != NULL) 2155 { 2156 p = tofree = concat_str(s, p); 2157 if (mustfree) 2158 vim_free(s); 2159 } 2160 } 2161 if (p != NULL) 2162 { 2163 vim_setenv(name, p); 2164 if (STRICMP(name, "HOME") == 0) 2165 init_homedir(); 2166 else if (didset_vim && STRICMP(name, "VIM") == 0) 2167 didset_vim = FALSE; 2168 else if (didset_vimruntime 2169 && STRICMP(name, "VIMRUNTIME") == 0) 2170 didset_vimruntime = FALSE; 2171 arg_end = arg; 2172 } 2173 name[len] = c1; 2174 vim_free(tofree); 2175 } 2176 } 2177 } 2178 2179 /* 2180 * ":let &option = expr": Set option value. 2181 * ":let &l:option = expr": Set local option value. 2182 * ":let &g:option = expr": Set global option value. 2183 */ 2184 else if (*arg == '&') 2185 { 2186 /* Find the end of the name. */ 2187 p = find_option_end(&arg, &opt_flags); 2188 if (p == NULL || (endchars != NULL 2189 && vim_strchr(endchars, *skipwhite(p)) == NULL)) 2190 EMSG(_(e_letunexp)); 2191 else 2192 { 2193 long n; 2194 int opt_type; 2195 long numval; 2196 char_u *stringval = NULL; 2197 char_u *s; 2198 2199 c1 = *p; 2200 *p = NUL; 2201 2202 n = get_tv_number(tv); 2203 s = get_tv_string_chk(tv); /* != NULL if number or string */ 2204 if (s != NULL && op != NULL && *op != '=') 2205 { 2206 opt_type = get_option_value(arg, &numval, 2207 &stringval, opt_flags); 2208 if ((opt_type == 1 && *op == '.') 2209 || (opt_type == 0 && *op != '.')) 2210 EMSG2(_(e_letwrong), op); 2211 else 2212 { 2213 if (opt_type == 1) /* number */ 2214 { 2215 if (*op == '+') 2216 n = numval + n; 2217 else 2218 n = numval - n; 2219 } 2220 else if (opt_type == 0 && stringval != NULL) /* string */ 2221 { 2222 s = concat_str(stringval, s); 2223 vim_free(stringval); 2224 stringval = s; 2225 } 2226 } 2227 } 2228 if (s != NULL) 2229 { 2230 set_option_value(arg, n, s, opt_flags); 2231 arg_end = p; 2232 } 2233 *p = c1; 2234 vim_free(stringval); 2235 } 2236 } 2237 2238 /* 2239 * ":let @r = expr": Set register contents. 2240 */ 2241 else if (*arg == '@') 2242 { 2243 ++arg; 2244 if (op != NULL && (*op == '+' || *op == '-')) 2245 EMSG2(_(e_letwrong), op); 2246 else if (endchars != NULL 2247 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL) 2248 EMSG(_(e_letunexp)); 2249 else 2250 { 2251 char_u *tofree = NULL; 2252 char_u *s; 2253 2254 p = get_tv_string_chk(tv); 2255 if (p != NULL && op != NULL && *op == '.') 2256 { 2257 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE); 2258 if (s != NULL) 2259 { 2260 p = tofree = concat_str(s, p); 2261 vim_free(s); 2262 } 2263 } 2264 if (p != NULL) 2265 { 2266 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE); 2267 arg_end = arg + 1; 2268 } 2269 vim_free(tofree); 2270 } 2271 } 2272 2273 /* 2274 * ":let var = expr": Set internal variable. 2275 * ":let {expr} = expr": Idem, name made with curly braces 2276 */ 2277 else if (eval_isnamec1(*arg) || *arg == '{') 2278 { 2279 lval_T lv; 2280 2281 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START); 2282 if (p != NULL && lv.ll_name != NULL) 2283 { 2284 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL) 2285 EMSG(_(e_letunexp)); 2286 else 2287 { 2288 set_var_lval(&lv, p, tv, copy, op); 2289 arg_end = p; 2290 } 2291 } 2292 clear_lval(&lv); 2293 } 2294 2295 else 2296 EMSG2(_(e_invarg2), arg); 2297 2298 return arg_end; 2299 } 2300 2301 /* 2302 * If "arg" is equal to "b:changedtick" give an error and return TRUE. 2303 */ 2304 static int 2305 check_changedtick(arg) 2306 char_u *arg; 2307 { 2308 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13])) 2309 { 2310 EMSG2(_(e_readonlyvar), arg); 2311 return TRUE; 2312 } 2313 return FALSE; 2314 } 2315 2316 /* 2317 * Get an lval: variable, Dict item or List item that can be assigned a value 2318 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", 2319 * "name.key", "name.key[expr]" etc. 2320 * Indexing only works if "name" is an existing List or Dictionary. 2321 * "name" points to the start of the name. 2322 * If "rettv" is not NULL it points to the value to be assigned. 2323 * "unlet" is TRUE for ":unlet": slightly different behavior when something is 2324 * wrong; must end in space or cmd separator. 2325 * 2326 * Returns a pointer to just after the name, including indexes. 2327 * When an evaluation error occurs "lp->ll_name" is NULL; 2328 * Returns NULL for a parsing error. Still need to free items in "lp"! 2329 */ 2330 static char_u * 2331 get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags) 2332 char_u *name; 2333 typval_T *rettv; 2334 lval_T *lp; 2335 int unlet; 2336 int skip; 2337 int quiet; /* don't give error messages */ 2338 int fne_flags; /* flags for find_name_end() */ 2339 { 2340 char_u *p; 2341 char_u *expr_start, *expr_end; 2342 int cc; 2343 dictitem_T *v; 2344 typval_T var1; 2345 typval_T var2; 2346 int empty1 = FALSE; 2347 listitem_T *ni; 2348 char_u *key = NULL; 2349 int len; 2350 hashtab_T *ht; 2351 2352 /* Clear everything in "lp". */ 2353 vim_memset(lp, 0, sizeof(lval_T)); 2354 2355 if (skip) 2356 { 2357 /* When skipping just find the end of the name. */ 2358 lp->ll_name = name; 2359 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags); 2360 } 2361 2362 /* Find the end of the name. */ 2363 p = find_name_end(name, &expr_start, &expr_end, fne_flags); 2364 if (expr_start != NULL) 2365 { 2366 /* Don't expand the name when we already know there is an error. */ 2367 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p) 2368 && *p != '[' && *p != '.') 2369 { 2370 EMSG(_(e_trailing)); 2371 return NULL; 2372 } 2373 2374 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p); 2375 if (lp->ll_exp_name == NULL) 2376 { 2377 /* Report an invalid expression in braces, unless the 2378 * expression evaluation has been cancelled due to an 2379 * aborting error, an interrupt, or an exception. */ 2380 if (!aborting() && !quiet) 2381 { 2382 emsg_severe = TRUE; 2383 EMSG2(_(e_invarg2), name); 2384 return NULL; 2385 } 2386 } 2387 lp->ll_name = lp->ll_exp_name; 2388 } 2389 else 2390 lp->ll_name = name; 2391 2392 /* Without [idx] or .key we are done. */ 2393 if ((*p != '[' && *p != '.') || lp->ll_name == NULL) 2394 return p; 2395 2396 cc = *p; 2397 *p = NUL; 2398 v = find_var(lp->ll_name, &ht); 2399 if (v == NULL && !quiet) 2400 EMSG2(_(e_undefvar), lp->ll_name); 2401 *p = cc; 2402 if (v == NULL) 2403 return NULL; 2404 2405 /* 2406 * Loop until no more [idx] or .key is following. 2407 */ 2408 lp->ll_tv = &v->di_tv; 2409 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT)) 2410 { 2411 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL) 2412 && !(lp->ll_tv->v_type == VAR_DICT 2413 && lp->ll_tv->vval.v_dict != NULL)) 2414 { 2415 if (!quiet) 2416 EMSG(_("E689: Can only index a List or Dictionary")); 2417 return NULL; 2418 } 2419 if (lp->ll_range) 2420 { 2421 if (!quiet) 2422 EMSG(_("E708: [:] must come last")); 2423 return NULL; 2424 } 2425 2426 len = -1; 2427 if (*p == '.') 2428 { 2429 key = p + 1; 2430 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 2431 ; 2432 if (len == 0) 2433 { 2434 if (!quiet) 2435 EMSG(_(e_emptykey)); 2436 return NULL; 2437 } 2438 p = key + len; 2439 } 2440 else 2441 { 2442 /* Get the index [expr] or the first index [expr: ]. */ 2443 p = skipwhite(p + 1); 2444 if (*p == ':') 2445 empty1 = TRUE; 2446 else 2447 { 2448 empty1 = FALSE; 2449 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */ 2450 return NULL; 2451 if (get_tv_string_chk(&var1) == NULL) 2452 { 2453 /* not a number or string */ 2454 clear_tv(&var1); 2455 return NULL; 2456 } 2457 } 2458 2459 /* Optionally get the second index [ :expr]. */ 2460 if (*p == ':') 2461 { 2462 if (lp->ll_tv->v_type == VAR_DICT) 2463 { 2464 if (!quiet) 2465 EMSG(_(e_dictrange)); 2466 if (!empty1) 2467 clear_tv(&var1); 2468 return NULL; 2469 } 2470 if (rettv != NULL && (rettv->v_type != VAR_LIST 2471 || rettv->vval.v_list == NULL)) 2472 { 2473 if (!quiet) 2474 EMSG(_("E709: [:] requires a List value")); 2475 if (!empty1) 2476 clear_tv(&var1); 2477 return NULL; 2478 } 2479 p = skipwhite(p + 1); 2480 if (*p == ']') 2481 lp->ll_empty2 = TRUE; 2482 else 2483 { 2484 lp->ll_empty2 = FALSE; 2485 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */ 2486 { 2487 if (!empty1) 2488 clear_tv(&var1); 2489 return NULL; 2490 } 2491 if (get_tv_string_chk(&var2) == NULL) 2492 { 2493 /* not a number or string */ 2494 if (!empty1) 2495 clear_tv(&var1); 2496 clear_tv(&var2); 2497 return NULL; 2498 } 2499 } 2500 lp->ll_range = TRUE; 2501 } 2502 else 2503 lp->ll_range = FALSE; 2504 2505 if (*p != ']') 2506 { 2507 if (!quiet) 2508 EMSG(_(e_missbrac)); 2509 if (!empty1) 2510 clear_tv(&var1); 2511 if (lp->ll_range && !lp->ll_empty2) 2512 clear_tv(&var2); 2513 return NULL; 2514 } 2515 2516 /* Skip to past ']'. */ 2517 ++p; 2518 } 2519 2520 if (lp->ll_tv->v_type == VAR_DICT) 2521 { 2522 if (len == -1) 2523 { 2524 /* "[key]": get key from "var1" */ 2525 key = get_tv_string(&var1); /* is number or string */ 2526 if (*key == NUL) 2527 { 2528 if (!quiet) 2529 EMSG(_(e_emptykey)); 2530 clear_tv(&var1); 2531 return NULL; 2532 } 2533 } 2534 lp->ll_list = NULL; 2535 lp->ll_dict = lp->ll_tv->vval.v_dict; 2536 lp->ll_di = dict_find(lp->ll_dict, key, len); 2537 if (lp->ll_di == NULL) 2538 { 2539 /* Key does not exist in dict: may need to add it. */ 2540 if (*p == '[' || *p == '.' || unlet) 2541 { 2542 if (!quiet) 2543 EMSG2(_(e_dictkey), key); 2544 if (len == -1) 2545 clear_tv(&var1); 2546 return NULL; 2547 } 2548 if (len == -1) 2549 lp->ll_newkey = vim_strsave(key); 2550 else 2551 lp->ll_newkey = vim_strnsave(key, len); 2552 if (len == -1) 2553 clear_tv(&var1); 2554 if (lp->ll_newkey == NULL) 2555 p = NULL; 2556 break; 2557 } 2558 if (len == -1) 2559 clear_tv(&var1); 2560 lp->ll_tv = &lp->ll_di->di_tv; 2561 } 2562 else 2563 { 2564 /* 2565 * Get the number and item for the only or first index of the List. 2566 */ 2567 if (empty1) 2568 lp->ll_n1 = 0; 2569 else 2570 { 2571 lp->ll_n1 = get_tv_number(&var1); /* is number or string */ 2572 clear_tv(&var1); 2573 } 2574 lp->ll_dict = NULL; 2575 lp->ll_list = lp->ll_tv->vval.v_list; 2576 lp->ll_li = list_find(lp->ll_list, lp->ll_n1); 2577 if (lp->ll_li == NULL) 2578 { 2579 if (!quiet) 2580 EMSGN(_(e_listidx), lp->ll_n1); 2581 if (lp->ll_range && !lp->ll_empty2) 2582 clear_tv(&var2); 2583 return NULL; 2584 } 2585 2586 /* 2587 * May need to find the item or absolute index for the second 2588 * index of a range. 2589 * When no index given: "lp->ll_empty2" is TRUE. 2590 * Otherwise "lp->ll_n2" is set to the second index. 2591 */ 2592 if (lp->ll_range && !lp->ll_empty2) 2593 { 2594 lp->ll_n2 = get_tv_number(&var2); /* is number or string */ 2595 clear_tv(&var2); 2596 if (lp->ll_n2 < 0) 2597 { 2598 ni = list_find(lp->ll_list, lp->ll_n2); 2599 if (ni == NULL) 2600 { 2601 if (!quiet) 2602 EMSGN(_(e_listidx), lp->ll_n2); 2603 return NULL; 2604 } 2605 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni); 2606 } 2607 2608 /* Check that lp->ll_n2 isn't before lp->ll_n1. */ 2609 if (lp->ll_n1 < 0) 2610 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li); 2611 if (lp->ll_n2 < lp->ll_n1) 2612 { 2613 if (!quiet) 2614 EMSGN(_(e_listidx), lp->ll_n2); 2615 return NULL; 2616 } 2617 } 2618 2619 lp->ll_tv = &lp->ll_li->li_tv; 2620 } 2621 } 2622 2623 return p; 2624 } 2625 2626 /* 2627 * Clear lval "lp" that was filled by get_lval(). 2628 */ 2629 static void 2630 clear_lval(lp) 2631 lval_T *lp; 2632 { 2633 vim_free(lp->ll_exp_name); 2634 vim_free(lp->ll_newkey); 2635 } 2636 2637 /* 2638 * Set a variable that was parsed by get_lval() to "rettv". 2639 * "endp" points to just after the parsed name. 2640 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=". 2641 */ 2642 static void 2643 set_var_lval(lp, endp, rettv, copy, op) 2644 lval_T *lp; 2645 char_u *endp; 2646 typval_T *rettv; 2647 int copy; 2648 char_u *op; 2649 { 2650 int cc; 2651 listitem_T *ri; 2652 dictitem_T *di; 2653 2654 if (lp->ll_tv == NULL) 2655 { 2656 if (!check_changedtick(lp->ll_name)) 2657 { 2658 cc = *endp; 2659 *endp = NUL; 2660 if (op != NULL && *op != '=') 2661 { 2662 typval_T tv; 2663 2664 /* handle +=, -= and .= */ 2665 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), 2666 &tv, TRUE) == OK) 2667 { 2668 if (tv_op(&tv, rettv, op) == OK) 2669 set_var(lp->ll_name, &tv, FALSE); 2670 clear_tv(&tv); 2671 } 2672 } 2673 else 2674 set_var(lp->ll_name, rettv, copy); 2675 *endp = cc; 2676 } 2677 } 2678 else if (tv_check_lock(lp->ll_newkey == NULL 2679 ? lp->ll_tv->v_lock 2680 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name)) 2681 ; 2682 else if (lp->ll_range) 2683 { 2684 /* 2685 * Assign the List values to the list items. 2686 */ 2687 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) 2688 { 2689 if (op != NULL && *op != '=') 2690 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op); 2691 else 2692 { 2693 clear_tv(&lp->ll_li->li_tv); 2694 copy_tv(&ri->li_tv, &lp->ll_li->li_tv); 2695 } 2696 ri = ri->li_next; 2697 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1)) 2698 break; 2699 if (lp->ll_li->li_next == NULL) 2700 { 2701 /* Need to add an empty item. */ 2702 if (list_append_number(lp->ll_list, 0) == FAIL) 2703 { 2704 ri = NULL; 2705 break; 2706 } 2707 } 2708 lp->ll_li = lp->ll_li->li_next; 2709 ++lp->ll_n1; 2710 } 2711 if (ri != NULL) 2712 EMSG(_("E710: List value has more items than target")); 2713 else if (lp->ll_empty2 2714 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL) 2715 : lp->ll_n1 != lp->ll_n2) 2716 EMSG(_("E711: List value has not enough items")); 2717 } 2718 else 2719 { 2720 /* 2721 * Assign to a List or Dictionary item. 2722 */ 2723 if (lp->ll_newkey != NULL) 2724 { 2725 if (op != NULL && *op != '=') 2726 { 2727 EMSG2(_(e_letwrong), op); 2728 return; 2729 } 2730 2731 /* Need to add an item to the Dictionary. */ 2732 di = dictitem_alloc(lp->ll_newkey); 2733 if (di == NULL) 2734 return; 2735 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL) 2736 { 2737 vim_free(di); 2738 return; 2739 } 2740 lp->ll_tv = &di->di_tv; 2741 } 2742 else if (op != NULL && *op != '=') 2743 { 2744 tv_op(lp->ll_tv, rettv, op); 2745 return; 2746 } 2747 else 2748 clear_tv(lp->ll_tv); 2749 2750 /* 2751 * Assign the value to the variable or list item. 2752 */ 2753 if (copy) 2754 copy_tv(rettv, lp->ll_tv); 2755 else 2756 { 2757 *lp->ll_tv = *rettv; 2758 lp->ll_tv->v_lock = 0; 2759 init_tv(rettv); 2760 } 2761 } 2762 } 2763 2764 /* 2765 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2" 2766 * Returns OK or FAIL. 2767 */ 2768 static int 2769 tv_op(tv1, tv2, op) 2770 typval_T *tv1; 2771 typval_T *tv2; 2772 char_u *op; 2773 { 2774 long n; 2775 char_u numbuf[NUMBUFLEN]; 2776 char_u *s; 2777 2778 /* Can't do anything with a Funcref or a Dict on the right. */ 2779 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT) 2780 { 2781 switch (tv1->v_type) 2782 { 2783 case VAR_DICT: 2784 case VAR_FUNC: 2785 break; 2786 2787 case VAR_LIST: 2788 if (*op != '+' || tv2->v_type != VAR_LIST) 2789 break; 2790 /* List += List */ 2791 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL) 2792 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL); 2793 return OK; 2794 2795 case VAR_NUMBER: 2796 case VAR_STRING: 2797 if (tv2->v_type == VAR_LIST) 2798 break; 2799 if (*op == '+' || *op == '-') 2800 { 2801 /* nr += nr or nr -= nr*/ 2802 n = get_tv_number(tv1); 2803 if (*op == '+') 2804 n += get_tv_number(tv2); 2805 else 2806 n -= get_tv_number(tv2); 2807 clear_tv(tv1); 2808 tv1->v_type = VAR_NUMBER; 2809 tv1->vval.v_number = n; 2810 } 2811 else 2812 { 2813 /* str .= str */ 2814 s = get_tv_string(tv1); 2815 s = concat_str(s, get_tv_string_buf(tv2, numbuf)); 2816 clear_tv(tv1); 2817 tv1->v_type = VAR_STRING; 2818 tv1->vval.v_string = s; 2819 } 2820 return OK; 2821 } 2822 } 2823 2824 EMSG2(_(e_letwrong), op); 2825 return FAIL; 2826 } 2827 2828 /* 2829 * Add a watcher to a list. 2830 */ 2831 static void 2832 list_add_watch(l, lw) 2833 list_T *l; 2834 listwatch_T *lw; 2835 { 2836 lw->lw_next = l->lv_watch; 2837 l->lv_watch = lw; 2838 } 2839 2840 /* 2841 * Remove a watcher from a list. 2842 * No warning when it isn't found... 2843 */ 2844 static void 2845 list_rem_watch(l, lwrem) 2846 list_T *l; 2847 listwatch_T *lwrem; 2848 { 2849 listwatch_T *lw, **lwp; 2850 2851 lwp = &l->lv_watch; 2852 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next) 2853 { 2854 if (lw == lwrem) 2855 { 2856 *lwp = lw->lw_next; 2857 break; 2858 } 2859 lwp = &lw->lw_next; 2860 } 2861 } 2862 2863 /* 2864 * Just before removing an item from a list: advance watchers to the next 2865 * item. 2866 */ 2867 static void 2868 list_fix_watch(l, item) 2869 list_T *l; 2870 listitem_T *item; 2871 { 2872 listwatch_T *lw; 2873 2874 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next) 2875 if (lw->lw_item == item) 2876 lw->lw_item = item->li_next; 2877 } 2878 2879 /* 2880 * Evaluate the expression used in a ":for var in expr" command. 2881 * "arg" points to "var". 2882 * Set "*errp" to TRUE for an error, FALSE otherwise; 2883 * Return a pointer that holds the info. Null when there is an error. 2884 */ 2885 void * 2886 eval_for_line(arg, errp, nextcmdp, skip) 2887 char_u *arg; 2888 int *errp; 2889 char_u **nextcmdp; 2890 int skip; 2891 { 2892 forinfo_T *fi; 2893 char_u *expr; 2894 typval_T tv; 2895 list_T *l; 2896 2897 *errp = TRUE; /* default: there is an error */ 2898 2899 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T)); 2900 if (fi == NULL) 2901 return NULL; 2902 2903 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon); 2904 if (expr == NULL) 2905 return fi; 2906 2907 expr = skipwhite(expr); 2908 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2])) 2909 { 2910 EMSG(_("E690: Missing \"in\" after :for")); 2911 return fi; 2912 } 2913 2914 if (skip) 2915 ++emsg_skip; 2916 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK) 2917 { 2918 *errp = FALSE; 2919 if (!skip) 2920 { 2921 l = tv.vval.v_list; 2922 if (tv.v_type != VAR_LIST || l == NULL) 2923 { 2924 EMSG(_(e_listreq)); 2925 clear_tv(&tv); 2926 } 2927 else 2928 { 2929 /* No need to increment the refcount, it's already set for the 2930 * list being used in "tv". */ 2931 fi->fi_list = l; 2932 list_add_watch(l, &fi->fi_lw); 2933 fi->fi_lw.lw_item = l->lv_first; 2934 } 2935 } 2936 } 2937 if (skip) 2938 --emsg_skip; 2939 2940 return fi; 2941 } 2942 2943 /* 2944 * Use the first item in a ":for" list. Advance to the next. 2945 * Assign the values to the variable (list). "arg" points to the first one. 2946 * Return TRUE when a valid item was found, FALSE when at end of list or 2947 * something wrong. 2948 */ 2949 int 2950 next_for_item(fi_void, arg) 2951 void *fi_void; 2952 char_u *arg; 2953 { 2954 forinfo_T *fi = (forinfo_T *)fi_void; 2955 int result; 2956 listitem_T *item; 2957 2958 item = fi->fi_lw.lw_item; 2959 if (item == NULL) 2960 result = FALSE; 2961 else 2962 { 2963 fi->fi_lw.lw_item = item->li_next; 2964 result = (ex_let_vars(arg, &item->li_tv, TRUE, 2965 fi->fi_semicolon, fi->fi_varcount, NULL) == OK); 2966 } 2967 return result; 2968 } 2969 2970 /* 2971 * Free the structure used to store info used by ":for". 2972 */ 2973 void 2974 free_for_info(fi_void) 2975 void *fi_void; 2976 { 2977 forinfo_T *fi = (forinfo_T *)fi_void; 2978 2979 if (fi != NULL && fi->fi_list != NULL) 2980 { 2981 list_rem_watch(fi->fi_list, &fi->fi_lw); 2982 list_unref(fi->fi_list); 2983 } 2984 vim_free(fi); 2985 } 2986 2987 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 2988 2989 void 2990 set_context_for_expression(xp, arg, cmdidx) 2991 expand_T *xp; 2992 char_u *arg; 2993 cmdidx_T cmdidx; 2994 { 2995 int got_eq = FALSE; 2996 int c; 2997 char_u *p; 2998 2999 if (cmdidx == CMD_let) 3000 { 3001 xp->xp_context = EXPAND_USER_VARS; 3002 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL) 3003 { 3004 /* ":let var1 var2 ...": find last space. */ 3005 for (p = arg + STRLEN(arg); p >= arg; ) 3006 { 3007 xp->xp_pattern = p; 3008 mb_ptr_back(arg, p); 3009 if (vim_iswhite(*p)) 3010 break; 3011 } 3012 return; 3013 } 3014 } 3015 else 3016 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS 3017 : EXPAND_EXPRESSION; 3018 while ((xp->xp_pattern = vim_strpbrk(arg, 3019 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL) 3020 { 3021 c = *xp->xp_pattern; 3022 if (c == '&') 3023 { 3024 c = xp->xp_pattern[1]; 3025 if (c == '&') 3026 { 3027 ++xp->xp_pattern; 3028 xp->xp_context = cmdidx != CMD_let || got_eq 3029 ? EXPAND_EXPRESSION : EXPAND_NOTHING; 3030 } 3031 else if (c != ' ') 3032 { 3033 xp->xp_context = EXPAND_SETTINGS; 3034 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':') 3035 xp->xp_pattern += 2; 3036 3037 } 3038 } 3039 else if (c == '$') 3040 { 3041 /* environment variable */ 3042 xp->xp_context = EXPAND_ENV_VARS; 3043 } 3044 else if (c == '=') 3045 { 3046 got_eq = TRUE; 3047 xp->xp_context = EXPAND_EXPRESSION; 3048 } 3049 else if (c == '<' 3050 && xp->xp_context == EXPAND_FUNCTIONS 3051 && vim_strchr(xp->xp_pattern, '(') == NULL) 3052 { 3053 /* Function name can start with "<SNR>" */ 3054 break; 3055 } 3056 else if (cmdidx != CMD_let || got_eq) 3057 { 3058 if (c == '"') /* string */ 3059 { 3060 while ((c = *++xp->xp_pattern) != NUL && c != '"') 3061 if (c == '\\' && xp->xp_pattern[1] != NUL) 3062 ++xp->xp_pattern; 3063 xp->xp_context = EXPAND_NOTHING; 3064 } 3065 else if (c == '\'') /* literal string */ 3066 { 3067 /* Trick: '' is like stopping and starting a literal string. */ 3068 while ((c = *++xp->xp_pattern) != NUL && c != '\'') 3069 /* skip */ ; 3070 xp->xp_context = EXPAND_NOTHING; 3071 } 3072 else if (c == '|') 3073 { 3074 if (xp->xp_pattern[1] == '|') 3075 { 3076 ++xp->xp_pattern; 3077 xp->xp_context = EXPAND_EXPRESSION; 3078 } 3079 else 3080 xp->xp_context = EXPAND_COMMANDS; 3081 } 3082 else 3083 xp->xp_context = EXPAND_EXPRESSION; 3084 } 3085 else 3086 /* Doesn't look like something valid, expand as an expression 3087 * anyway. */ 3088 xp->xp_context = EXPAND_EXPRESSION; 3089 arg = xp->xp_pattern; 3090 if (*arg != NUL) 3091 while ((c = *++arg) != NUL && (c == ' ' || c == '\t')) 3092 /* skip */ ; 3093 } 3094 xp->xp_pattern = arg; 3095 } 3096 3097 #endif /* FEAT_CMDL_COMPL */ 3098 3099 /* 3100 * ":1,25call func(arg1, arg2)" function call. 3101 */ 3102 void 3103 ex_call(eap) 3104 exarg_T *eap; 3105 { 3106 char_u *arg = eap->arg; 3107 char_u *startarg; 3108 char_u *name; 3109 char_u *tofree; 3110 int len; 3111 typval_T rettv; 3112 linenr_T lnum; 3113 int doesrange; 3114 int failed = FALSE; 3115 funcdict_T fudi; 3116 3117 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi); 3118 vim_free(fudi.fd_newkey); 3119 if (tofree == NULL) 3120 return; 3121 3122 /* Increase refcount on dictionary, it could get deleted when evaluating 3123 * the arguments. */ 3124 if (fudi.fd_dict != NULL) 3125 ++fudi.fd_dict->dv_refcount; 3126 3127 /* If it is the name of a variable of type VAR_FUNC use its contents. */ 3128 len = STRLEN(tofree); 3129 name = deref_func_name(tofree, &len); 3130 3131 /* Skip white space to allow ":call func ()". Not good, but required for 3132 * backward compatibility. */ 3133 startarg = skipwhite(arg); 3134 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 3135 3136 if (*startarg != '(') 3137 { 3138 EMSG2(_("E107: Missing braces: %s"), eap->arg); 3139 goto end; 3140 } 3141 3142 /* 3143 * When skipping, evaluate the function once, to find the end of the 3144 * arguments. 3145 * When the function takes a range, this is discovered after the first 3146 * call, and the loop is broken. 3147 */ 3148 if (eap->skip) 3149 { 3150 ++emsg_skip; 3151 lnum = eap->line2; /* do it once, also with an invalid range */ 3152 } 3153 else 3154 lnum = eap->line1; 3155 for ( ; lnum <= eap->line2; ++lnum) 3156 { 3157 if (!eap->skip && eap->addr_count > 0) 3158 { 3159 curwin->w_cursor.lnum = lnum; 3160 curwin->w_cursor.col = 0; 3161 } 3162 arg = startarg; 3163 if (get_func_tv(name, STRLEN(name), &rettv, &arg, 3164 eap->line1, eap->line2, &doesrange, 3165 !eap->skip, fudi.fd_dict) == FAIL) 3166 { 3167 failed = TRUE; 3168 break; 3169 } 3170 clear_tv(&rettv); 3171 if (doesrange || eap->skip) 3172 break; 3173 /* Stop when immediately aborting on error, or when an interrupt 3174 * occurred or an exception was thrown but not caught. 3175 * get_func_tv() returned OK, so that the check for trailing 3176 * characters below is executed. */ 3177 if (aborting()) 3178 break; 3179 } 3180 if (eap->skip) 3181 --emsg_skip; 3182 3183 if (!failed) 3184 { 3185 /* Check for trailing illegal characters and a following command. */ 3186 if (!ends_excmd(*arg)) 3187 { 3188 emsg_severe = TRUE; 3189 EMSG(_(e_trailing)); 3190 } 3191 else 3192 eap->nextcmd = check_nextcmd(arg); 3193 } 3194 3195 end: 3196 dict_unref(fudi.fd_dict); 3197 vim_free(tofree); 3198 } 3199 3200 /* 3201 * ":unlet[!] var1 ... " command. 3202 */ 3203 void 3204 ex_unlet(eap) 3205 exarg_T *eap; 3206 { 3207 ex_unletlock(eap, eap->arg, 0); 3208 } 3209 3210 /* 3211 * ":lockvar" and ":unlockvar" commands 3212 */ 3213 void 3214 ex_lockvar(eap) 3215 exarg_T *eap; 3216 { 3217 char_u *arg = eap->arg; 3218 int deep = 2; 3219 3220 if (eap->forceit) 3221 deep = -1; 3222 else if (vim_isdigit(*arg)) 3223 { 3224 deep = getdigits(&arg); 3225 arg = skipwhite(arg); 3226 } 3227 3228 ex_unletlock(eap, arg, deep); 3229 } 3230 3231 /* 3232 * ":unlet", ":lockvar" and ":unlockvar" are quite similar. 3233 */ 3234 static void 3235 ex_unletlock(eap, argstart, deep) 3236 exarg_T *eap; 3237 char_u *argstart; 3238 int deep; 3239 { 3240 char_u *arg = argstart; 3241 char_u *name_end; 3242 int error = FALSE; 3243 lval_T lv; 3244 3245 do 3246 { 3247 /* Parse the name and find the end. */ 3248 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE, 3249 FNE_CHECK_START); 3250 if (lv.ll_name == NULL) 3251 error = TRUE; /* error but continue parsing */ 3252 if (name_end == NULL || (!vim_iswhite(*name_end) 3253 && !ends_excmd(*name_end))) 3254 { 3255 if (name_end != NULL) 3256 { 3257 emsg_severe = TRUE; 3258 EMSG(_(e_trailing)); 3259 } 3260 if (!(eap->skip || error)) 3261 clear_lval(&lv); 3262 break; 3263 } 3264 3265 if (!error && !eap->skip) 3266 { 3267 if (eap->cmdidx == CMD_unlet) 3268 { 3269 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL) 3270 error = TRUE; 3271 } 3272 else 3273 { 3274 if (do_lock_var(&lv, name_end, deep, 3275 eap->cmdidx == CMD_lockvar) == FAIL) 3276 error = TRUE; 3277 } 3278 } 3279 3280 if (!eap->skip) 3281 clear_lval(&lv); 3282 3283 arg = skipwhite(name_end); 3284 } while (!ends_excmd(*arg)); 3285 3286 eap->nextcmd = check_nextcmd(arg); 3287 } 3288 3289 static int 3290 do_unlet_var(lp, name_end, forceit) 3291 lval_T *lp; 3292 char_u *name_end; 3293 int forceit; 3294 { 3295 int ret = OK; 3296 int cc; 3297 3298 if (lp->ll_tv == NULL) 3299 { 3300 cc = *name_end; 3301 *name_end = NUL; 3302 3303 /* Normal name or expanded name. */ 3304 if (check_changedtick(lp->ll_name)) 3305 ret = FAIL; 3306 else if (do_unlet(lp->ll_name, forceit) == FAIL) 3307 ret = FAIL; 3308 *name_end = cc; 3309 } 3310 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name)) 3311 return FAIL; 3312 else if (lp->ll_range) 3313 { 3314 listitem_T *li; 3315 3316 /* Delete a range of List items. */ 3317 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) 3318 { 3319 li = lp->ll_li->li_next; 3320 listitem_remove(lp->ll_list, lp->ll_li); 3321 lp->ll_li = li; 3322 ++lp->ll_n1; 3323 } 3324 } 3325 else 3326 { 3327 if (lp->ll_list != NULL) 3328 /* unlet a List item. */ 3329 listitem_remove(lp->ll_list, lp->ll_li); 3330 else 3331 /* unlet a Dictionary item. */ 3332 dictitem_remove(lp->ll_dict, lp->ll_di); 3333 } 3334 3335 return ret; 3336 } 3337 3338 /* 3339 * "unlet" a variable. Return OK if it existed, FAIL if not. 3340 * When "forceit" is TRUE don't complain if the variable doesn't exist. 3341 */ 3342 int 3343 do_unlet(name, forceit) 3344 char_u *name; 3345 int forceit; 3346 { 3347 hashtab_T *ht; 3348 hashitem_T *hi; 3349 char_u *varname; 3350 3351 ht = find_var_ht(name, &varname); 3352 if (ht != NULL && *varname != NUL) 3353 { 3354 hi = hash_find(ht, varname); 3355 if (!HASHITEM_EMPTY(hi)) 3356 { 3357 if (var_check_ro(HI2DI(hi)->di_flags, name)) 3358 return FAIL; 3359 delete_var(ht, hi); 3360 return OK; 3361 } 3362 } 3363 if (forceit) 3364 return OK; 3365 EMSG2(_("E108: No such variable: \"%s\""), name); 3366 return FAIL; 3367 } 3368 3369 /* 3370 * Lock or unlock variable indicated by "lp". 3371 * "deep" is the levels to go (-1 for unlimited); 3372 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar". 3373 */ 3374 static int 3375 do_lock_var(lp, name_end, deep, lock) 3376 lval_T *lp; 3377 char_u *name_end; 3378 int deep; 3379 int lock; 3380 { 3381 int ret = OK; 3382 int cc; 3383 dictitem_T *di; 3384 3385 if (deep == 0) /* nothing to do */ 3386 return OK; 3387 3388 if (lp->ll_tv == NULL) 3389 { 3390 cc = *name_end; 3391 *name_end = NUL; 3392 3393 /* Normal name or expanded name. */ 3394 if (check_changedtick(lp->ll_name)) 3395 ret = FAIL; 3396 else 3397 { 3398 di = find_var(lp->ll_name, NULL); 3399 if (di == NULL) 3400 ret = FAIL; 3401 else 3402 { 3403 if (lock) 3404 di->di_flags |= DI_FLAGS_LOCK; 3405 else 3406 di->di_flags &= ~DI_FLAGS_LOCK; 3407 item_lock(&di->di_tv, deep, lock); 3408 } 3409 } 3410 *name_end = cc; 3411 } 3412 else if (lp->ll_range) 3413 { 3414 listitem_T *li = lp->ll_li; 3415 3416 /* (un)lock a range of List items. */ 3417 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) 3418 { 3419 item_lock(&li->li_tv, deep, lock); 3420 li = li->li_next; 3421 ++lp->ll_n1; 3422 } 3423 } 3424 else if (lp->ll_list != NULL) 3425 /* (un)lock a List item. */ 3426 item_lock(&lp->ll_li->li_tv, deep, lock); 3427 else 3428 /* un(lock) a Dictionary item. */ 3429 item_lock(&lp->ll_di->di_tv, deep, lock); 3430 3431 return ret; 3432 } 3433 3434 /* 3435 * Lock or unlock an item. "deep" is nr of levels to go. 3436 */ 3437 static void 3438 item_lock(tv, deep, lock) 3439 typval_T *tv; 3440 int deep; 3441 int lock; 3442 { 3443 static int recurse = 0; 3444 list_T *l; 3445 listitem_T *li; 3446 dict_T *d; 3447 hashitem_T *hi; 3448 int todo; 3449 3450 if (recurse >= DICT_MAXNEST) 3451 { 3452 EMSG(_("E743: variable nested too deep for (un)lock")); 3453 return; 3454 } 3455 if (deep == 0) 3456 return; 3457 ++recurse; 3458 3459 /* lock/unlock the item itself */ 3460 if (lock) 3461 tv->v_lock |= VAR_LOCKED; 3462 else 3463 tv->v_lock &= ~VAR_LOCKED; 3464 3465 switch (tv->v_type) 3466 { 3467 case VAR_LIST: 3468 if ((l = tv->vval.v_list) != NULL) 3469 { 3470 if (lock) 3471 l->lv_lock |= VAR_LOCKED; 3472 else 3473 l->lv_lock &= ~VAR_LOCKED; 3474 if (deep < 0 || deep > 1) 3475 /* recursive: lock/unlock the items the List contains */ 3476 for (li = l->lv_first; li != NULL; li = li->li_next) 3477 item_lock(&li->li_tv, deep - 1, lock); 3478 } 3479 break; 3480 case VAR_DICT: 3481 if ((d = tv->vval.v_dict) != NULL) 3482 { 3483 if (lock) 3484 d->dv_lock |= VAR_LOCKED; 3485 else 3486 d->dv_lock &= ~VAR_LOCKED; 3487 if (deep < 0 || deep > 1) 3488 { 3489 /* recursive: lock/unlock the items the List contains */ 3490 todo = d->dv_hashtab.ht_used; 3491 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 3492 { 3493 if (!HASHITEM_EMPTY(hi)) 3494 { 3495 --todo; 3496 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock); 3497 } 3498 } 3499 } 3500 } 3501 } 3502 --recurse; 3503 } 3504 3505 /* 3506 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or 3507 * it refers to a List or Dictionary that is locked. 3508 */ 3509 static int 3510 tv_islocked(tv) 3511 typval_T *tv; 3512 { 3513 return (tv->v_lock & VAR_LOCKED) 3514 || (tv->v_type == VAR_LIST 3515 && tv->vval.v_list != NULL 3516 && (tv->vval.v_list->lv_lock & VAR_LOCKED)) 3517 || (tv->v_type == VAR_DICT 3518 && tv->vval.v_dict != NULL 3519 && (tv->vval.v_dict->dv_lock & VAR_LOCKED)); 3520 } 3521 3522 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO) 3523 /* 3524 * Delete all "menutrans_" variables. 3525 */ 3526 void 3527 del_menutrans_vars() 3528 { 3529 hashitem_T *hi; 3530 int todo; 3531 3532 hash_lock(&globvarht); 3533 todo = globvarht.ht_used; 3534 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi) 3535 { 3536 if (!HASHITEM_EMPTY(hi)) 3537 { 3538 --todo; 3539 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0) 3540 delete_var(&globvarht, hi); 3541 } 3542 } 3543 hash_unlock(&globvarht); 3544 } 3545 #endif 3546 3547 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 3548 3549 /* 3550 * Local string buffer for the next two functions to store a variable name 3551 * with its prefix. Allocated in cat_prefix_varname(), freed later in 3552 * get_user_var_name(). 3553 */ 3554 3555 static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name)); 3556 3557 static char_u *varnamebuf = NULL; 3558 static int varnamebuflen = 0; 3559 3560 /* 3561 * Function to concatenate a prefix and a variable name. 3562 */ 3563 static char_u * 3564 cat_prefix_varname(prefix, name) 3565 int prefix; 3566 char_u *name; 3567 { 3568 int len; 3569 3570 len = (int)STRLEN(name) + 3; 3571 if (len > varnamebuflen) 3572 { 3573 vim_free(varnamebuf); 3574 len += 10; /* some additional space */ 3575 varnamebuf = alloc(len); 3576 if (varnamebuf == NULL) 3577 { 3578 varnamebuflen = 0; 3579 return NULL; 3580 } 3581 varnamebuflen = len; 3582 } 3583 *varnamebuf = prefix; 3584 varnamebuf[1] = ':'; 3585 STRCPY(varnamebuf + 2, name); 3586 return varnamebuf; 3587 } 3588 3589 /* 3590 * Function given to ExpandGeneric() to obtain the list of user defined 3591 * (global/buffer/window/built-in) variable names. 3592 */ 3593 /*ARGSUSED*/ 3594 char_u * 3595 get_user_var_name(xp, idx) 3596 expand_T *xp; 3597 int idx; 3598 { 3599 static long_u gdone; 3600 static long_u bdone; 3601 static long_u wdone; 3602 #ifdef FEAT_WINDOWS 3603 static long_u tdone; 3604 #endif 3605 static int vidx; 3606 static hashitem_T *hi; 3607 hashtab_T *ht; 3608 3609 if (idx == 0) 3610 { 3611 gdone = bdone = wdone = vidx = 0; 3612 #ifdef FEAT_WINDOWS 3613 tdone = 0; 3614 #endif 3615 } 3616 3617 /* Global variables */ 3618 if (gdone < globvarht.ht_used) 3619 { 3620 if (gdone++ == 0) 3621 hi = globvarht.ht_array; 3622 else 3623 ++hi; 3624 while (HASHITEM_EMPTY(hi)) 3625 ++hi; 3626 if (STRNCMP("g:", xp->xp_pattern, 2) == 0) 3627 return cat_prefix_varname('g', hi->hi_key); 3628 return hi->hi_key; 3629 } 3630 3631 /* b: variables */ 3632 ht = &curbuf->b_vars.dv_hashtab; 3633 if (bdone < ht->ht_used) 3634 { 3635 if (bdone++ == 0) 3636 hi = ht->ht_array; 3637 else 3638 ++hi; 3639 while (HASHITEM_EMPTY(hi)) 3640 ++hi; 3641 return cat_prefix_varname('b', hi->hi_key); 3642 } 3643 if (bdone == ht->ht_used) 3644 { 3645 ++bdone; 3646 return (char_u *)"b:changedtick"; 3647 } 3648 3649 /* w: variables */ 3650 ht = &curwin->w_vars.dv_hashtab; 3651 if (wdone < ht->ht_used) 3652 { 3653 if (wdone++ == 0) 3654 hi = ht->ht_array; 3655 else 3656 ++hi; 3657 while (HASHITEM_EMPTY(hi)) 3658 ++hi; 3659 return cat_prefix_varname('w', hi->hi_key); 3660 } 3661 3662 #ifdef FEAT_WINDOWS 3663 /* t: variables */ 3664 ht = &curtab->tp_vars.dv_hashtab; 3665 if (tdone < ht->ht_used) 3666 { 3667 if (tdone++ == 0) 3668 hi = ht->ht_array; 3669 else 3670 ++hi; 3671 while (HASHITEM_EMPTY(hi)) 3672 ++hi; 3673 return cat_prefix_varname('t', hi->hi_key); 3674 } 3675 #endif 3676 3677 /* v: variables */ 3678 if (vidx < VV_LEN) 3679 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name); 3680 3681 vim_free(varnamebuf); 3682 varnamebuf = NULL; 3683 varnamebuflen = 0; 3684 return NULL; 3685 } 3686 3687 #endif /* FEAT_CMDL_COMPL */ 3688 3689 /* 3690 * types for expressions. 3691 */ 3692 typedef enum 3693 { 3694 TYPE_UNKNOWN = 0 3695 , TYPE_EQUAL /* == */ 3696 , TYPE_NEQUAL /* != */ 3697 , TYPE_GREATER /* > */ 3698 , TYPE_GEQUAL /* >= */ 3699 , TYPE_SMALLER /* < */ 3700 , TYPE_SEQUAL /* <= */ 3701 , TYPE_MATCH /* =~ */ 3702 , TYPE_NOMATCH /* !~ */ 3703 } exptype_T; 3704 3705 /* 3706 * The "evaluate" argument: When FALSE, the argument is only parsed but not 3707 * executed. The function may return OK, but the rettv will be of type 3708 * VAR_UNKNOWN. The function still returns FAIL for a syntax error. 3709 */ 3710 3711 /* 3712 * Handle zero level expression. 3713 * This calls eval1() and handles error message and nextcmd. 3714 * Put the result in "rettv" when returning OK and "evaluate" is TRUE. 3715 * Note: "rettv.v_lock" is not set. 3716 * Return OK or FAIL. 3717 */ 3718 static int 3719 eval0(arg, rettv, nextcmd, evaluate) 3720 char_u *arg; 3721 typval_T *rettv; 3722 char_u **nextcmd; 3723 int evaluate; 3724 { 3725 int ret; 3726 char_u *p; 3727 3728 p = skipwhite(arg); 3729 ret = eval1(&p, rettv, evaluate); 3730 if (ret == FAIL || !ends_excmd(*p)) 3731 { 3732 if (ret != FAIL) 3733 clear_tv(rettv); 3734 /* 3735 * Report the invalid expression unless the expression evaluation has 3736 * been cancelled due to an aborting error, an interrupt, or an 3737 * exception. 3738 */ 3739 if (!aborting()) 3740 EMSG2(_(e_invexpr2), arg); 3741 ret = FAIL; 3742 } 3743 if (nextcmd != NULL) 3744 *nextcmd = check_nextcmd(p); 3745 3746 return ret; 3747 } 3748 3749 /* 3750 * Handle top level expression: 3751 * expr1 ? expr0 : expr0 3752 * 3753 * "arg" must point to the first non-white of the expression. 3754 * "arg" is advanced to the next non-white after the recognized expression. 3755 * 3756 * Note: "rettv.v_lock" is not set. 3757 * 3758 * Return OK or FAIL. 3759 */ 3760 static int 3761 eval1(arg, rettv, evaluate) 3762 char_u **arg; 3763 typval_T *rettv; 3764 int evaluate; 3765 { 3766 int result; 3767 typval_T var2; 3768 3769 /* 3770 * Get the first variable. 3771 */ 3772 if (eval2(arg, rettv, evaluate) == FAIL) 3773 return FAIL; 3774 3775 if ((*arg)[0] == '?') 3776 { 3777 result = FALSE; 3778 if (evaluate) 3779 { 3780 int error = FALSE; 3781 3782 if (get_tv_number_chk(rettv, &error) != 0) 3783 result = TRUE; 3784 clear_tv(rettv); 3785 if (error) 3786 return FAIL; 3787 } 3788 3789 /* 3790 * Get the second variable. 3791 */ 3792 *arg = skipwhite(*arg + 1); 3793 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */ 3794 return FAIL; 3795 3796 /* 3797 * Check for the ":". 3798 */ 3799 if ((*arg)[0] != ':') 3800 { 3801 EMSG(_("E109: Missing ':' after '?'")); 3802 if (evaluate && result) 3803 clear_tv(rettv); 3804 return FAIL; 3805 } 3806 3807 /* 3808 * Get the third variable. 3809 */ 3810 *arg = skipwhite(*arg + 1); 3811 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */ 3812 { 3813 if (evaluate && result) 3814 clear_tv(rettv); 3815 return FAIL; 3816 } 3817 if (evaluate && !result) 3818 *rettv = var2; 3819 } 3820 3821 return OK; 3822 } 3823 3824 /* 3825 * Handle first level expression: 3826 * expr2 || expr2 || expr2 logical OR 3827 * 3828 * "arg" must point to the first non-white of the expression. 3829 * "arg" is advanced to the next non-white after the recognized expression. 3830 * 3831 * Return OK or FAIL. 3832 */ 3833 static int 3834 eval2(arg, rettv, evaluate) 3835 char_u **arg; 3836 typval_T *rettv; 3837 int evaluate; 3838 { 3839 typval_T var2; 3840 long result; 3841 int first; 3842 int error = FALSE; 3843 3844 /* 3845 * Get the first variable. 3846 */ 3847 if (eval3(arg, rettv, evaluate) == FAIL) 3848 return FAIL; 3849 3850 /* 3851 * Repeat until there is no following "||". 3852 */ 3853 first = TRUE; 3854 result = FALSE; 3855 while ((*arg)[0] == '|' && (*arg)[1] == '|') 3856 { 3857 if (evaluate && first) 3858 { 3859 if (get_tv_number_chk(rettv, &error) != 0) 3860 result = TRUE; 3861 clear_tv(rettv); 3862 if (error) 3863 return FAIL; 3864 first = FALSE; 3865 } 3866 3867 /* 3868 * Get the second variable. 3869 */ 3870 *arg = skipwhite(*arg + 2); 3871 if (eval3(arg, &var2, evaluate && !result) == FAIL) 3872 return FAIL; 3873 3874 /* 3875 * Compute the result. 3876 */ 3877 if (evaluate && !result) 3878 { 3879 if (get_tv_number_chk(&var2, &error) != 0) 3880 result = TRUE; 3881 clear_tv(&var2); 3882 if (error) 3883 return FAIL; 3884 } 3885 if (evaluate) 3886 { 3887 rettv->v_type = VAR_NUMBER; 3888 rettv->vval.v_number = result; 3889 } 3890 } 3891 3892 return OK; 3893 } 3894 3895 /* 3896 * Handle second level expression: 3897 * expr3 && expr3 && expr3 logical AND 3898 * 3899 * "arg" must point to the first non-white of the expression. 3900 * "arg" is advanced to the next non-white after the recognized expression. 3901 * 3902 * Return OK or FAIL. 3903 */ 3904 static int 3905 eval3(arg, rettv, evaluate) 3906 char_u **arg; 3907 typval_T *rettv; 3908 int evaluate; 3909 { 3910 typval_T var2; 3911 long result; 3912 int first; 3913 int error = FALSE; 3914 3915 /* 3916 * Get the first variable. 3917 */ 3918 if (eval4(arg, rettv, evaluate) == FAIL) 3919 return FAIL; 3920 3921 /* 3922 * Repeat until there is no following "&&". 3923 */ 3924 first = TRUE; 3925 result = TRUE; 3926 while ((*arg)[0] == '&' && (*arg)[1] == '&') 3927 { 3928 if (evaluate && first) 3929 { 3930 if (get_tv_number_chk(rettv, &error) == 0) 3931 result = FALSE; 3932 clear_tv(rettv); 3933 if (error) 3934 return FAIL; 3935 first = FALSE; 3936 } 3937 3938 /* 3939 * Get the second variable. 3940 */ 3941 *arg = skipwhite(*arg + 2); 3942 if (eval4(arg, &var2, evaluate && result) == FAIL) 3943 return FAIL; 3944 3945 /* 3946 * Compute the result. 3947 */ 3948 if (evaluate && result) 3949 { 3950 if (get_tv_number_chk(&var2, &error) == 0) 3951 result = FALSE; 3952 clear_tv(&var2); 3953 if (error) 3954 return FAIL; 3955 } 3956 if (evaluate) 3957 { 3958 rettv->v_type = VAR_NUMBER; 3959 rettv->vval.v_number = result; 3960 } 3961 } 3962 3963 return OK; 3964 } 3965 3966 /* 3967 * Handle third level expression: 3968 * var1 == var2 3969 * var1 =~ var2 3970 * var1 != var2 3971 * var1 !~ var2 3972 * var1 > var2 3973 * var1 >= var2 3974 * var1 < var2 3975 * var1 <= var2 3976 * var1 is var2 3977 * var1 isnot var2 3978 * 3979 * "arg" must point to the first non-white of the expression. 3980 * "arg" is advanced to the next non-white after the recognized expression. 3981 * 3982 * Return OK or FAIL. 3983 */ 3984 static int 3985 eval4(arg, rettv, evaluate) 3986 char_u **arg; 3987 typval_T *rettv; 3988 int evaluate; 3989 { 3990 typval_T var2; 3991 char_u *p; 3992 int i; 3993 exptype_T type = TYPE_UNKNOWN; 3994 int type_is = FALSE; /* TRUE for "is" and "isnot" */ 3995 int len = 2; 3996 long n1, n2; 3997 char_u *s1, *s2; 3998 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 3999 regmatch_T regmatch; 4000 int ic; 4001 char_u *save_cpo; 4002 4003 /* 4004 * Get the first variable. 4005 */ 4006 if (eval5(arg, rettv, evaluate) == FAIL) 4007 return FAIL; 4008 4009 p = *arg; 4010 switch (p[0]) 4011 { 4012 case '=': if (p[1] == '=') 4013 type = TYPE_EQUAL; 4014 else if (p[1] == '~') 4015 type = TYPE_MATCH; 4016 break; 4017 case '!': if (p[1] == '=') 4018 type = TYPE_NEQUAL; 4019 else if (p[1] == '~') 4020 type = TYPE_NOMATCH; 4021 break; 4022 case '>': if (p[1] != '=') 4023 { 4024 type = TYPE_GREATER; 4025 len = 1; 4026 } 4027 else 4028 type = TYPE_GEQUAL; 4029 break; 4030 case '<': if (p[1] != '=') 4031 { 4032 type = TYPE_SMALLER; 4033 len = 1; 4034 } 4035 else 4036 type = TYPE_SEQUAL; 4037 break; 4038 case 'i': if (p[1] == 's') 4039 { 4040 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') 4041 len = 5; 4042 if (!vim_isIDc(p[len])) 4043 { 4044 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL; 4045 type_is = TRUE; 4046 } 4047 } 4048 break; 4049 } 4050 4051 /* 4052 * If there is a comparitive operator, use it. 4053 */ 4054 if (type != TYPE_UNKNOWN) 4055 { 4056 /* extra question mark appended: ignore case */ 4057 if (p[len] == '?') 4058 { 4059 ic = TRUE; 4060 ++len; 4061 } 4062 /* extra '#' appended: match case */ 4063 else if (p[len] == '#') 4064 { 4065 ic = FALSE; 4066 ++len; 4067 } 4068 /* nothing appened: use 'ignorecase' */ 4069 else 4070 ic = p_ic; 4071 4072 /* 4073 * Get the second variable. 4074 */ 4075 *arg = skipwhite(p + len); 4076 if (eval5(arg, &var2, evaluate) == FAIL) 4077 { 4078 clear_tv(rettv); 4079 return FAIL; 4080 } 4081 4082 if (evaluate) 4083 { 4084 if (type_is && rettv->v_type != var2.v_type) 4085 { 4086 /* For "is" a different type always means FALSE, for "notis" 4087 * it means TRUE. */ 4088 n1 = (type == TYPE_NEQUAL); 4089 } 4090 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST) 4091 { 4092 if (type_is) 4093 { 4094 n1 = (rettv->v_type == var2.v_type 4095 && rettv->vval.v_list == var2.vval.v_list); 4096 if (type == TYPE_NEQUAL) 4097 n1 = !n1; 4098 } 4099 else if (rettv->v_type != var2.v_type 4100 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 4101 { 4102 if (rettv->v_type != var2.v_type) 4103 EMSG(_("E691: Can only compare List with List")); 4104 else 4105 EMSG(_("E692: Invalid operation for Lists")); 4106 clear_tv(rettv); 4107 clear_tv(&var2); 4108 return FAIL; 4109 } 4110 else 4111 { 4112 /* Compare two Lists for being equal or unequal. */ 4113 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic); 4114 if (type == TYPE_NEQUAL) 4115 n1 = !n1; 4116 } 4117 } 4118 4119 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT) 4120 { 4121 if (type_is) 4122 { 4123 n1 = (rettv->v_type == var2.v_type 4124 && rettv->vval.v_dict == var2.vval.v_dict); 4125 if (type == TYPE_NEQUAL) 4126 n1 = !n1; 4127 } 4128 else if (rettv->v_type != var2.v_type 4129 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 4130 { 4131 if (rettv->v_type != var2.v_type) 4132 EMSG(_("E735: Can only compare Dictionary with Dictionary")); 4133 else 4134 EMSG(_("E736: Invalid operation for Dictionary")); 4135 clear_tv(rettv); 4136 clear_tv(&var2); 4137 return FAIL; 4138 } 4139 else 4140 { 4141 /* Compare two Dictionaries for being equal or unequal. */ 4142 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic); 4143 if (type == TYPE_NEQUAL) 4144 n1 = !n1; 4145 } 4146 } 4147 4148 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC) 4149 { 4150 if (rettv->v_type != var2.v_type 4151 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 4152 { 4153 if (rettv->v_type != var2.v_type) 4154 EMSG(_("E693: Can only compare Funcref with Funcref")); 4155 else 4156 EMSG(_("E694: Invalid operation for Funcrefs")); 4157 clear_tv(rettv); 4158 clear_tv(&var2); 4159 return FAIL; 4160 } 4161 else 4162 { 4163 /* Compare two Funcrefs for being equal or unequal. */ 4164 if (rettv->vval.v_string == NULL 4165 || var2.vval.v_string == NULL) 4166 n1 = FALSE; 4167 else 4168 n1 = STRCMP(rettv->vval.v_string, 4169 var2.vval.v_string) == 0; 4170 if (type == TYPE_NEQUAL) 4171 n1 = !n1; 4172 } 4173 } 4174 4175 /* 4176 * If one of the two variables is a number, compare as a number. 4177 * When using "=~" or "!~", always compare as string. 4178 */ 4179 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER) 4180 && type != TYPE_MATCH && type != TYPE_NOMATCH) 4181 { 4182 n1 = get_tv_number(rettv); 4183 n2 = get_tv_number(&var2); 4184 switch (type) 4185 { 4186 case TYPE_EQUAL: n1 = (n1 == n2); break; 4187 case TYPE_NEQUAL: n1 = (n1 != n2); break; 4188 case TYPE_GREATER: n1 = (n1 > n2); break; 4189 case TYPE_GEQUAL: n1 = (n1 >= n2); break; 4190 case TYPE_SMALLER: n1 = (n1 < n2); break; 4191 case TYPE_SEQUAL: n1 = (n1 <= n2); break; 4192 case TYPE_UNKNOWN: 4193 case TYPE_MATCH: 4194 case TYPE_NOMATCH: break; /* avoid gcc warning */ 4195 } 4196 } 4197 else 4198 { 4199 s1 = get_tv_string_buf(rettv, buf1); 4200 s2 = get_tv_string_buf(&var2, buf2); 4201 if (type != TYPE_MATCH && type != TYPE_NOMATCH) 4202 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2); 4203 else 4204 i = 0; 4205 n1 = FALSE; 4206 switch (type) 4207 { 4208 case TYPE_EQUAL: n1 = (i == 0); break; 4209 case TYPE_NEQUAL: n1 = (i != 0); break; 4210 case TYPE_GREATER: n1 = (i > 0); break; 4211 case TYPE_GEQUAL: n1 = (i >= 0); break; 4212 case TYPE_SMALLER: n1 = (i < 0); break; 4213 case TYPE_SEQUAL: n1 = (i <= 0); break; 4214 4215 case TYPE_MATCH: 4216 case TYPE_NOMATCH: 4217 /* avoid 'l' flag in 'cpoptions' */ 4218 save_cpo = p_cpo; 4219 p_cpo = (char_u *)""; 4220 regmatch.regprog = vim_regcomp(s2, 4221 RE_MAGIC + RE_STRING); 4222 regmatch.rm_ic = ic; 4223 if (regmatch.regprog != NULL) 4224 { 4225 n1 = vim_regexec_nl(®match, s1, (colnr_T)0); 4226 vim_free(regmatch.regprog); 4227 if (type == TYPE_NOMATCH) 4228 n1 = !n1; 4229 } 4230 p_cpo = save_cpo; 4231 break; 4232 4233 case TYPE_UNKNOWN: break; /* avoid gcc warning */ 4234 } 4235 } 4236 clear_tv(rettv); 4237 clear_tv(&var2); 4238 rettv->v_type = VAR_NUMBER; 4239 rettv->vval.v_number = n1; 4240 } 4241 } 4242 4243 return OK; 4244 } 4245 4246 /* 4247 * Handle fourth level expression: 4248 * + number addition 4249 * - number subtraction 4250 * . string concatenation 4251 * 4252 * "arg" must point to the first non-white of the expression. 4253 * "arg" is advanced to the next non-white after the recognized expression. 4254 * 4255 * Return OK or FAIL. 4256 */ 4257 static int 4258 eval5(arg, rettv, evaluate) 4259 char_u **arg; 4260 typval_T *rettv; 4261 int evaluate; 4262 { 4263 typval_T var2; 4264 typval_T var3; 4265 int op; 4266 long n1, n2; 4267 char_u *s1, *s2; 4268 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 4269 char_u *p; 4270 4271 /* 4272 * Get the first variable. 4273 */ 4274 if (eval6(arg, rettv, evaluate) == FAIL) 4275 return FAIL; 4276 4277 /* 4278 * Repeat computing, until no '+', '-' or '.' is following. 4279 */ 4280 for (;;) 4281 { 4282 op = **arg; 4283 if (op != '+' && op != '-' && op != '.') 4284 break; 4285 4286 if (op != '+' || rettv->v_type != VAR_LIST) 4287 { 4288 /* For "list + ...", an illegal use of the first operand as 4289 * a number cannot be determined before evaluating the 2nd 4290 * operand: if this is also a list, all is ok. 4291 * For "something . ...", "something - ..." or "non-list + ...", 4292 * we know that the first operand needs to be a string or number 4293 * without evaluating the 2nd operand. So check before to avoid 4294 * side effects after an error. */ 4295 if (evaluate && get_tv_string_chk(rettv) == NULL) 4296 { 4297 clear_tv(rettv); 4298 return FAIL; 4299 } 4300 } 4301 4302 /* 4303 * Get the second variable. 4304 */ 4305 *arg = skipwhite(*arg + 1); 4306 if (eval6(arg, &var2, evaluate) == FAIL) 4307 { 4308 clear_tv(rettv); 4309 return FAIL; 4310 } 4311 4312 if (evaluate) 4313 { 4314 /* 4315 * Compute the result. 4316 */ 4317 if (op == '.') 4318 { 4319 s1 = get_tv_string_buf(rettv, buf1); /* already checked */ 4320 s2 = get_tv_string_buf_chk(&var2, buf2); 4321 if (s2 == NULL) /* type error ? */ 4322 { 4323 clear_tv(rettv); 4324 clear_tv(&var2); 4325 return FAIL; 4326 } 4327 p = concat_str(s1, s2); 4328 clear_tv(rettv); 4329 rettv->v_type = VAR_STRING; 4330 rettv->vval.v_string = p; 4331 } 4332 else if (op == '+' && rettv->v_type == VAR_LIST 4333 && var2.v_type == VAR_LIST) 4334 { 4335 /* concatenate Lists */ 4336 if (list_concat(rettv->vval.v_list, var2.vval.v_list, 4337 &var3) == FAIL) 4338 { 4339 clear_tv(rettv); 4340 clear_tv(&var2); 4341 return FAIL; 4342 } 4343 clear_tv(rettv); 4344 *rettv = var3; 4345 } 4346 else 4347 { 4348 int error = FALSE; 4349 4350 n1 = get_tv_number_chk(rettv, &error); 4351 if (error) 4352 { 4353 /* This can only happen for "list + non-list". 4354 * For "non-list + ..." or "something - ...", we returned 4355 * before evaluating the 2nd operand. */ 4356 clear_tv(rettv); 4357 return FAIL; 4358 } 4359 n2 = get_tv_number_chk(&var2, &error); 4360 if (error) 4361 { 4362 clear_tv(rettv); 4363 clear_tv(&var2); 4364 return FAIL; 4365 } 4366 clear_tv(rettv); 4367 if (op == '+') 4368 n1 = n1 + n2; 4369 else 4370 n1 = n1 - n2; 4371 rettv->v_type = VAR_NUMBER; 4372 rettv->vval.v_number = n1; 4373 } 4374 clear_tv(&var2); 4375 } 4376 } 4377 return OK; 4378 } 4379 4380 /* 4381 * Handle fifth level expression: 4382 * * number multiplication 4383 * / number division 4384 * % number modulo 4385 * 4386 * "arg" must point to the first non-white of the expression. 4387 * "arg" is advanced to the next non-white after the recognized expression. 4388 * 4389 * Return OK or FAIL. 4390 */ 4391 static int 4392 eval6(arg, rettv, evaluate) 4393 char_u **arg; 4394 typval_T *rettv; 4395 int evaluate; 4396 { 4397 typval_T var2; 4398 int op; 4399 long n1, n2; 4400 int error = FALSE; 4401 4402 /* 4403 * Get the first variable. 4404 */ 4405 if (eval7(arg, rettv, evaluate) == FAIL) 4406 return FAIL; 4407 4408 /* 4409 * Repeat computing, until no '*', '/' or '%' is following. 4410 */ 4411 for (;;) 4412 { 4413 op = **arg; 4414 if (op != '*' && op != '/' && op != '%') 4415 break; 4416 4417 if (evaluate) 4418 { 4419 n1 = get_tv_number_chk(rettv, &error); 4420 clear_tv(rettv); 4421 if (error) 4422 return FAIL; 4423 } 4424 else 4425 n1 = 0; 4426 4427 /* 4428 * Get the second variable. 4429 */ 4430 *arg = skipwhite(*arg + 1); 4431 if (eval7(arg, &var2, evaluate) == FAIL) 4432 return FAIL; 4433 4434 if (evaluate) 4435 { 4436 n2 = get_tv_number_chk(&var2, &error); 4437 clear_tv(&var2); 4438 if (error) 4439 return FAIL; 4440 4441 /* 4442 * Compute the result. 4443 */ 4444 if (op == '*') 4445 n1 = n1 * n2; 4446 else if (op == '/') 4447 { 4448 if (n2 == 0) /* give an error message? */ 4449 n1 = 0x7fffffffL; 4450 else 4451 n1 = n1 / n2; 4452 } 4453 else 4454 { 4455 if (n2 == 0) /* give an error message? */ 4456 n1 = 0; 4457 else 4458 n1 = n1 % n2; 4459 } 4460 rettv->v_type = VAR_NUMBER; 4461 rettv->vval.v_number = n1; 4462 } 4463 } 4464 4465 return OK; 4466 } 4467 4468 /* 4469 * Handle sixth level expression: 4470 * number number constant 4471 * "string" string contstant 4472 * 'string' literal string contstant 4473 * &option-name option value 4474 * @r register contents 4475 * identifier variable value 4476 * function() function call 4477 * $VAR environment variable 4478 * (expression) nested expression 4479 * [expr, expr] List 4480 * {key: val, key: val} Dictionary 4481 * 4482 * Also handle: 4483 * ! in front logical NOT 4484 * - in front unary minus 4485 * + in front unary plus (ignored) 4486 * trailing [] subscript in String or List 4487 * trailing .name entry in Dictionary 4488 * 4489 * "arg" must point to the first non-white of the expression. 4490 * "arg" is advanced to the next non-white after the recognized expression. 4491 * 4492 * Return OK or FAIL. 4493 */ 4494 static int 4495 eval7(arg, rettv, evaluate) 4496 char_u **arg; 4497 typval_T *rettv; 4498 int evaluate; 4499 { 4500 long n; 4501 int len; 4502 char_u *s; 4503 int val; 4504 char_u *start_leader, *end_leader; 4505 int ret = OK; 4506 char_u *alias; 4507 4508 /* 4509 * Initialise variable so that clear_tv() can't mistake this for a 4510 * string and free a string that isn't there. 4511 */ 4512 rettv->v_type = VAR_UNKNOWN; 4513 4514 /* 4515 * Skip '!' and '-' characters. They are handled later. 4516 */ 4517 start_leader = *arg; 4518 while (**arg == '!' || **arg == '-' || **arg == '+') 4519 *arg = skipwhite(*arg + 1); 4520 end_leader = *arg; 4521 4522 switch (**arg) 4523 { 4524 /* 4525 * Number constant. 4526 */ 4527 case '0': 4528 case '1': 4529 case '2': 4530 case '3': 4531 case '4': 4532 case '5': 4533 case '6': 4534 case '7': 4535 case '8': 4536 case '9': 4537 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL); 4538 *arg += len; 4539 if (evaluate) 4540 { 4541 rettv->v_type = VAR_NUMBER; 4542 rettv->vval.v_number = n; 4543 } 4544 break; 4545 4546 /* 4547 * String constant: "string". 4548 */ 4549 case '"': ret = get_string_tv(arg, rettv, evaluate); 4550 break; 4551 4552 /* 4553 * Literal string constant: 'str''ing'. 4554 */ 4555 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate); 4556 break; 4557 4558 /* 4559 * List: [expr, expr] 4560 */ 4561 case '[': ret = get_list_tv(arg, rettv, evaluate); 4562 break; 4563 4564 /* 4565 * Dictionary: {key: val, key: val} 4566 */ 4567 case '{': ret = get_dict_tv(arg, rettv, evaluate); 4568 break; 4569 4570 /* 4571 * Option value: &name 4572 */ 4573 case '&': ret = get_option_tv(arg, rettv, evaluate); 4574 break; 4575 4576 /* 4577 * Environment variable: $VAR. 4578 */ 4579 case '$': ret = get_env_tv(arg, rettv, evaluate); 4580 break; 4581 4582 /* 4583 * Register contents: @r. 4584 */ 4585 case '@': ++*arg; 4586 if (evaluate) 4587 { 4588 rettv->v_type = VAR_STRING; 4589 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE); 4590 } 4591 if (**arg != NUL) 4592 ++*arg; 4593 break; 4594 4595 /* 4596 * nested expression: (expression). 4597 */ 4598 case '(': *arg = skipwhite(*arg + 1); 4599 ret = eval1(arg, rettv, evaluate); /* recursive! */ 4600 if (**arg == ')') 4601 ++*arg; 4602 else if (ret == OK) 4603 { 4604 EMSG(_("E110: Missing ')'")); 4605 clear_tv(rettv); 4606 ret = FAIL; 4607 } 4608 break; 4609 4610 default: ret = NOTDONE; 4611 break; 4612 } 4613 4614 if (ret == NOTDONE) 4615 { 4616 /* 4617 * Must be a variable or function name. 4618 * Can also be a curly-braces kind of name: {expr}. 4619 */ 4620 s = *arg; 4621 len = get_name_len(arg, &alias, evaluate, TRUE); 4622 if (alias != NULL) 4623 s = alias; 4624 4625 if (len <= 0) 4626 ret = FAIL; 4627 else 4628 { 4629 if (**arg == '(') /* recursive! */ 4630 { 4631 /* If "s" is the name of a variable of type VAR_FUNC 4632 * use its contents. */ 4633 s = deref_func_name(s, &len); 4634 4635 /* Invoke the function. */ 4636 ret = get_func_tv(s, len, rettv, arg, 4637 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 4638 &len, evaluate, NULL); 4639 /* Stop the expression evaluation when immediately 4640 * aborting on error, or when an interrupt occurred or 4641 * an exception was thrown but not caught. */ 4642 if (aborting()) 4643 { 4644 if (ret == OK) 4645 clear_tv(rettv); 4646 ret = FAIL; 4647 } 4648 } 4649 else if (evaluate) 4650 ret = get_var_tv(s, len, rettv, TRUE); 4651 else 4652 ret = OK; 4653 } 4654 4655 if (alias != NULL) 4656 vim_free(alias); 4657 } 4658 4659 *arg = skipwhite(*arg); 4660 4661 /* Handle following '[', '(' and '.' for expr[expr], expr.name, 4662 * expr(expr). */ 4663 if (ret == OK) 4664 ret = handle_subscript(arg, rettv, evaluate, TRUE); 4665 4666 /* 4667 * Apply logical NOT and unary '-', from right to left, ignore '+'. 4668 */ 4669 if (ret == OK && evaluate && end_leader > start_leader) 4670 { 4671 int error = FALSE; 4672 4673 val = get_tv_number_chk(rettv, &error); 4674 if (error) 4675 { 4676 clear_tv(rettv); 4677 ret = FAIL; 4678 } 4679 else 4680 { 4681 while (end_leader > start_leader) 4682 { 4683 --end_leader; 4684 if (*end_leader == '!') 4685 val = !val; 4686 else if (*end_leader == '-') 4687 val = -val; 4688 } 4689 clear_tv(rettv); 4690 rettv->v_type = VAR_NUMBER; 4691 rettv->vval.v_number = val; 4692 } 4693 } 4694 4695 return ret; 4696 } 4697 4698 /* 4699 * Evaluate an "[expr]" or "[expr:expr]" index. 4700 * "*arg" points to the '['. 4701 * Returns FAIL or OK. "*arg" is advanced to after the ']'. 4702 */ 4703 static int 4704 eval_index(arg, rettv, evaluate, verbose) 4705 char_u **arg; 4706 typval_T *rettv; 4707 int evaluate; 4708 int verbose; /* give error messages */ 4709 { 4710 int empty1 = FALSE, empty2 = FALSE; 4711 typval_T var1, var2; 4712 long n1, n2 = 0; 4713 long len = -1; 4714 int range = FALSE; 4715 char_u *s; 4716 char_u *key = NULL; 4717 4718 if (rettv->v_type == VAR_FUNC) 4719 { 4720 if (verbose) 4721 EMSG(_("E695: Cannot index a Funcref")); 4722 return FAIL; 4723 } 4724 4725 if (**arg == '.') 4726 { 4727 /* 4728 * dict.name 4729 */ 4730 key = *arg + 1; 4731 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 4732 ; 4733 if (len == 0) 4734 return FAIL; 4735 *arg = skipwhite(key + len); 4736 } 4737 else 4738 { 4739 /* 4740 * something[idx] 4741 * 4742 * Get the (first) variable from inside the []. 4743 */ 4744 *arg = skipwhite(*arg + 1); 4745 if (**arg == ':') 4746 empty1 = TRUE; 4747 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */ 4748 return FAIL; 4749 else if (evaluate && get_tv_string_chk(&var1) == NULL) 4750 { 4751 /* not a number or string */ 4752 clear_tv(&var1); 4753 return FAIL; 4754 } 4755 4756 /* 4757 * Get the second variable from inside the [:]. 4758 */ 4759 if (**arg == ':') 4760 { 4761 range = TRUE; 4762 *arg = skipwhite(*arg + 1); 4763 if (**arg == ']') 4764 empty2 = TRUE; 4765 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */ 4766 { 4767 if (!empty1) 4768 clear_tv(&var1); 4769 return FAIL; 4770 } 4771 else if (evaluate && get_tv_string_chk(&var2) == NULL) 4772 { 4773 /* not a number or string */ 4774 if (!empty1) 4775 clear_tv(&var1); 4776 clear_tv(&var2); 4777 return FAIL; 4778 } 4779 } 4780 4781 /* Check for the ']'. */ 4782 if (**arg != ']') 4783 { 4784 if (verbose) 4785 EMSG(_(e_missbrac)); 4786 clear_tv(&var1); 4787 if (range) 4788 clear_tv(&var2); 4789 return FAIL; 4790 } 4791 *arg = skipwhite(*arg + 1); /* skip the ']' */ 4792 } 4793 4794 if (evaluate) 4795 { 4796 n1 = 0; 4797 if (!empty1 && rettv->v_type != VAR_DICT) 4798 { 4799 n1 = get_tv_number(&var1); 4800 clear_tv(&var1); 4801 } 4802 if (range) 4803 { 4804 if (empty2) 4805 n2 = -1; 4806 else 4807 { 4808 n2 = get_tv_number(&var2); 4809 clear_tv(&var2); 4810 } 4811 } 4812 4813 switch (rettv->v_type) 4814 { 4815 case VAR_NUMBER: 4816 case VAR_STRING: 4817 s = get_tv_string(rettv); 4818 len = (long)STRLEN(s); 4819 if (range) 4820 { 4821 /* The resulting variable is a substring. If the indexes 4822 * are out of range the result is empty. */ 4823 if (n1 < 0) 4824 { 4825 n1 = len + n1; 4826 if (n1 < 0) 4827 n1 = 0; 4828 } 4829 if (n2 < 0) 4830 n2 = len + n2; 4831 else if (n2 >= len) 4832 n2 = len; 4833 if (n1 >= len || n2 < 0 || n1 > n2) 4834 s = NULL; 4835 else 4836 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1)); 4837 } 4838 else 4839 { 4840 /* The resulting variable is a string of a single 4841 * character. If the index is too big or negative the 4842 * result is empty. */ 4843 if (n1 >= len || n1 < 0) 4844 s = NULL; 4845 else 4846 s = vim_strnsave(s + n1, 1); 4847 } 4848 clear_tv(rettv); 4849 rettv->v_type = VAR_STRING; 4850 rettv->vval.v_string = s; 4851 break; 4852 4853 case VAR_LIST: 4854 len = list_len(rettv->vval.v_list); 4855 if (n1 < 0) 4856 n1 = len + n1; 4857 if (!empty1 && (n1 < 0 || n1 >= len)) 4858 { 4859 if (verbose) 4860 EMSGN(_(e_listidx), n1); 4861 return FAIL; 4862 } 4863 if (range) 4864 { 4865 list_T *l; 4866 listitem_T *item; 4867 4868 if (n2 < 0) 4869 n2 = len + n2; 4870 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1)) 4871 { 4872 if (verbose) 4873 EMSGN(_(e_listidx), n2); 4874 return FAIL; 4875 } 4876 l = list_alloc(); 4877 if (l == NULL) 4878 return FAIL; 4879 for (item = list_find(rettv->vval.v_list, n1); 4880 n1 <= n2; ++n1) 4881 { 4882 if (list_append_tv(l, &item->li_tv) == FAIL) 4883 { 4884 list_free(l); 4885 return FAIL; 4886 } 4887 item = item->li_next; 4888 } 4889 clear_tv(rettv); 4890 rettv->v_type = VAR_LIST; 4891 rettv->vval.v_list = l; 4892 ++l->lv_refcount; 4893 } 4894 else 4895 { 4896 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, 4897 &var1); 4898 clear_tv(rettv); 4899 *rettv = var1; 4900 } 4901 break; 4902 4903 case VAR_DICT: 4904 if (range) 4905 { 4906 if (verbose) 4907 EMSG(_(e_dictrange)); 4908 if (len == -1) 4909 clear_tv(&var1); 4910 return FAIL; 4911 } 4912 { 4913 dictitem_T *item; 4914 4915 if (len == -1) 4916 { 4917 key = get_tv_string(&var1); 4918 if (*key == NUL) 4919 { 4920 if (verbose) 4921 EMSG(_(e_emptykey)); 4922 clear_tv(&var1); 4923 return FAIL; 4924 } 4925 } 4926 4927 item = dict_find(rettv->vval.v_dict, key, (int)len); 4928 4929 if (item == NULL && verbose) 4930 EMSG2(_(e_dictkey), key); 4931 if (len == -1) 4932 clear_tv(&var1); 4933 if (item == NULL) 4934 return FAIL; 4935 4936 copy_tv(&item->di_tv, &var1); 4937 clear_tv(rettv); 4938 *rettv = var1; 4939 } 4940 break; 4941 } 4942 } 4943 4944 return OK; 4945 } 4946 4947 /* 4948 * Get an option value. 4949 * "arg" points to the '&' or '+' before the option name. 4950 * "arg" is advanced to character after the option name. 4951 * Return OK or FAIL. 4952 */ 4953 static int 4954 get_option_tv(arg, rettv, evaluate) 4955 char_u **arg; 4956 typval_T *rettv; /* when NULL, only check if option exists */ 4957 int evaluate; 4958 { 4959 char_u *option_end; 4960 long numval; 4961 char_u *stringval; 4962 int opt_type; 4963 int c; 4964 int working = (**arg == '+'); /* has("+option") */ 4965 int ret = OK; 4966 int opt_flags; 4967 4968 /* 4969 * Isolate the option name and find its value. 4970 */ 4971 option_end = find_option_end(arg, &opt_flags); 4972 if (option_end == NULL) 4973 { 4974 if (rettv != NULL) 4975 EMSG2(_("E112: Option name missing: %s"), *arg); 4976 return FAIL; 4977 } 4978 4979 if (!evaluate) 4980 { 4981 *arg = option_end; 4982 return OK; 4983 } 4984 4985 c = *option_end; 4986 *option_end = NUL; 4987 opt_type = get_option_value(*arg, &numval, 4988 rettv == NULL ? NULL : &stringval, opt_flags); 4989 4990 if (opt_type == -3) /* invalid name */ 4991 { 4992 if (rettv != NULL) 4993 EMSG2(_("E113: Unknown option: %s"), *arg); 4994 ret = FAIL; 4995 } 4996 else if (rettv != NULL) 4997 { 4998 if (opt_type == -2) /* hidden string option */ 4999 { 5000 rettv->v_type = VAR_STRING; 5001 rettv->vval.v_string = NULL; 5002 } 5003 else if (opt_type == -1) /* hidden number option */ 5004 { 5005 rettv->v_type = VAR_NUMBER; 5006 rettv->vval.v_number = 0; 5007 } 5008 else if (opt_type == 1) /* number option */ 5009 { 5010 rettv->v_type = VAR_NUMBER; 5011 rettv->vval.v_number = numval; 5012 } 5013 else /* string option */ 5014 { 5015 rettv->v_type = VAR_STRING; 5016 rettv->vval.v_string = stringval; 5017 } 5018 } 5019 else if (working && (opt_type == -2 || opt_type == -1)) 5020 ret = FAIL; 5021 5022 *option_end = c; /* put back for error messages */ 5023 *arg = option_end; 5024 5025 return ret; 5026 } 5027 5028 /* 5029 * Allocate a variable for a string constant. 5030 * Return OK or FAIL. 5031 */ 5032 static int 5033 get_string_tv(arg, rettv, evaluate) 5034 char_u **arg; 5035 typval_T *rettv; 5036 int evaluate; 5037 { 5038 char_u *p; 5039 char_u *name; 5040 int extra = 0; 5041 5042 /* 5043 * Find the end of the string, skipping backslashed characters. 5044 */ 5045 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p)) 5046 { 5047 if (*p == '\\' && p[1] != NUL) 5048 { 5049 ++p; 5050 /* A "\<x>" form occupies at least 4 characters, and produces up 5051 * to 6 characters: reserve space for 2 extra */ 5052 if (*p == '<') 5053 extra += 2; 5054 } 5055 } 5056 5057 if (*p != '"') 5058 { 5059 EMSG2(_("E114: Missing quote: %s"), *arg); 5060 return FAIL; 5061 } 5062 5063 /* If only parsing, set *arg and return here */ 5064 if (!evaluate) 5065 { 5066 *arg = p + 1; 5067 return OK; 5068 } 5069 5070 /* 5071 * Copy the string into allocated memory, handling backslashed 5072 * characters. 5073 */ 5074 name = alloc((unsigned)(p - *arg + extra)); 5075 if (name == NULL) 5076 return FAIL; 5077 rettv->v_type = VAR_STRING; 5078 rettv->vval.v_string = name; 5079 5080 for (p = *arg + 1; *p != NUL && *p != '"'; ) 5081 { 5082 if (*p == '\\') 5083 { 5084 switch (*++p) 5085 { 5086 case 'b': *name++ = BS; ++p; break; 5087 case 'e': *name++ = ESC; ++p; break; 5088 case 'f': *name++ = FF; ++p; break; 5089 case 'n': *name++ = NL; ++p; break; 5090 case 'r': *name++ = CAR; ++p; break; 5091 case 't': *name++ = TAB; ++p; break; 5092 5093 case 'X': /* hex: "\x1", "\x12" */ 5094 case 'x': 5095 case 'u': /* Unicode: "\u0023" */ 5096 case 'U': 5097 if (vim_isxdigit(p[1])) 5098 { 5099 int n, nr; 5100 int c = toupper(*p); 5101 5102 if (c == 'X') 5103 n = 2; 5104 else 5105 n = 4; 5106 nr = 0; 5107 while (--n >= 0 && vim_isxdigit(p[1])) 5108 { 5109 ++p; 5110 nr = (nr << 4) + hex2nr(*p); 5111 } 5112 ++p; 5113 #ifdef FEAT_MBYTE 5114 /* For "\u" store the number according to 5115 * 'encoding'. */ 5116 if (c != 'X') 5117 name += (*mb_char2bytes)(nr, name); 5118 else 5119 #endif 5120 *name++ = nr; 5121 } 5122 break; 5123 5124 /* octal: "\1", "\12", "\123" */ 5125 case '0': 5126 case '1': 5127 case '2': 5128 case '3': 5129 case '4': 5130 case '5': 5131 case '6': 5132 case '7': *name = *p++ - '0'; 5133 if (*p >= '0' && *p <= '7') 5134 { 5135 *name = (*name << 3) + *p++ - '0'; 5136 if (*p >= '0' && *p <= '7') 5137 *name = (*name << 3) + *p++ - '0'; 5138 } 5139 ++name; 5140 break; 5141 5142 /* Special key, e.g.: "\<C-W>" */ 5143 case '<': extra = trans_special(&p, name, TRUE); 5144 if (extra != 0) 5145 { 5146 name += extra; 5147 break; 5148 } 5149 /* FALLTHROUGH */ 5150 5151 default: MB_COPY_CHAR(p, name); 5152 break; 5153 } 5154 } 5155 else 5156 MB_COPY_CHAR(p, name); 5157 5158 } 5159 *name = NUL; 5160 *arg = p + 1; 5161 5162 return OK; 5163 } 5164 5165 /* 5166 * Allocate a variable for a 'str''ing' constant. 5167 * Return OK or FAIL. 5168 */ 5169 static int 5170 get_lit_string_tv(arg, rettv, evaluate) 5171 char_u **arg; 5172 typval_T *rettv; 5173 int evaluate; 5174 { 5175 char_u *p; 5176 char_u *str; 5177 int reduce = 0; 5178 5179 /* 5180 * Find the end of the string, skipping ''. 5181 */ 5182 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p)) 5183 { 5184 if (*p == '\'') 5185 { 5186 if (p[1] != '\'') 5187 break; 5188 ++reduce; 5189 ++p; 5190 } 5191 } 5192 5193 if (*p != '\'') 5194 { 5195 EMSG2(_("E115: Missing quote: %s"), *arg); 5196 return FAIL; 5197 } 5198 5199 /* If only parsing return after setting "*arg" */ 5200 if (!evaluate) 5201 { 5202 *arg = p + 1; 5203 return OK; 5204 } 5205 5206 /* 5207 * Copy the string into allocated memory, handling '' to ' reduction. 5208 */ 5209 str = alloc((unsigned)((p - *arg) - reduce)); 5210 if (str == NULL) 5211 return FAIL; 5212 rettv->v_type = VAR_STRING; 5213 rettv->vval.v_string = str; 5214 5215 for (p = *arg + 1; *p != NUL; ) 5216 { 5217 if (*p == '\'') 5218 { 5219 if (p[1] != '\'') 5220 break; 5221 ++p; 5222 } 5223 MB_COPY_CHAR(p, str); 5224 } 5225 *str = NUL; 5226 *arg = p + 1; 5227 5228 return OK; 5229 } 5230 5231 /* 5232 * Allocate a variable for a List and fill it from "*arg". 5233 * Return OK or FAIL. 5234 */ 5235 static int 5236 get_list_tv(arg, rettv, evaluate) 5237 char_u **arg; 5238 typval_T *rettv; 5239 int evaluate; 5240 { 5241 list_T *l = NULL; 5242 typval_T tv; 5243 listitem_T *item; 5244 5245 if (evaluate) 5246 { 5247 l = list_alloc(); 5248 if (l == NULL) 5249 return FAIL; 5250 } 5251 5252 *arg = skipwhite(*arg + 1); 5253 while (**arg != ']' && **arg != NUL) 5254 { 5255 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */ 5256 goto failret; 5257 if (evaluate) 5258 { 5259 item = listitem_alloc(); 5260 if (item != NULL) 5261 { 5262 item->li_tv = tv; 5263 item->li_tv.v_lock = 0; 5264 list_append(l, item); 5265 } 5266 else 5267 clear_tv(&tv); 5268 } 5269 5270 if (**arg == ']') 5271 break; 5272 if (**arg != ',') 5273 { 5274 EMSG2(_("E696: Missing comma in List: %s"), *arg); 5275 goto failret; 5276 } 5277 *arg = skipwhite(*arg + 1); 5278 } 5279 5280 if (**arg != ']') 5281 { 5282 EMSG2(_("E697: Missing end of List ']': %s"), *arg); 5283 failret: 5284 if (evaluate) 5285 list_free(l); 5286 return FAIL; 5287 } 5288 5289 *arg = skipwhite(*arg + 1); 5290 if (evaluate) 5291 { 5292 rettv->v_type = VAR_LIST; 5293 rettv->vval.v_list = l; 5294 ++l->lv_refcount; 5295 } 5296 5297 return OK; 5298 } 5299 5300 /* 5301 * Allocate an empty header for a list. 5302 * Caller should take care of the reference count. 5303 */ 5304 list_T * 5305 list_alloc() 5306 { 5307 list_T *l; 5308 5309 l = (list_T *)alloc_clear(sizeof(list_T)); 5310 if (l != NULL) 5311 { 5312 /* Prepend the list to the list of lists for garbage collection. */ 5313 if (first_list != NULL) 5314 first_list->lv_used_prev = l; 5315 l->lv_used_prev = NULL; 5316 l->lv_used_next = first_list; 5317 first_list = l; 5318 } 5319 return l; 5320 } 5321 5322 /* 5323 * Allocate an empty list for a return value. 5324 * Returns OK or FAIL. 5325 */ 5326 static int 5327 rettv_list_alloc(rettv) 5328 typval_T *rettv; 5329 { 5330 list_T *l = list_alloc(); 5331 5332 if (l == NULL) 5333 return FAIL; 5334 5335 rettv->vval.v_list = l; 5336 rettv->v_type = VAR_LIST; 5337 ++l->lv_refcount; 5338 return OK; 5339 } 5340 5341 /* 5342 * Unreference a list: decrement the reference count and free it when it 5343 * becomes zero. 5344 */ 5345 void 5346 list_unref(l) 5347 list_T *l; 5348 { 5349 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0) 5350 list_free(l); 5351 } 5352 5353 /* 5354 * Free a list, including all items it points to. 5355 * Ignores the reference count. 5356 */ 5357 void 5358 list_free(l) 5359 list_T *l; 5360 { 5361 listitem_T *item; 5362 5363 /* Avoid that recursive reference to the list frees us again. */ 5364 l->lv_refcount = DEL_REFCOUNT; 5365 5366 /* Remove the list from the list of lists for garbage collection. */ 5367 if (l->lv_used_prev == NULL) 5368 first_list = l->lv_used_next; 5369 else 5370 l->lv_used_prev->lv_used_next = l->lv_used_next; 5371 if (l->lv_used_next != NULL) 5372 l->lv_used_next->lv_used_prev = l->lv_used_prev; 5373 5374 for (item = l->lv_first; item != NULL; item = l->lv_first) 5375 { 5376 /* Remove the item before deleting it. */ 5377 l->lv_first = item->li_next; 5378 listitem_free(item); 5379 } 5380 vim_free(l); 5381 } 5382 5383 /* 5384 * Allocate a list item. 5385 */ 5386 static listitem_T * 5387 listitem_alloc() 5388 { 5389 return (listitem_T *)alloc(sizeof(listitem_T)); 5390 } 5391 5392 /* 5393 * Free a list item. Also clears the value. Does not notify watchers. 5394 */ 5395 static void 5396 listitem_free(item) 5397 listitem_T *item; 5398 { 5399 clear_tv(&item->li_tv); 5400 vim_free(item); 5401 } 5402 5403 /* 5404 * Remove a list item from a List and free it. Also clears the value. 5405 */ 5406 static void 5407 listitem_remove(l, item) 5408 list_T *l; 5409 listitem_T *item; 5410 { 5411 list_remove(l, item, item); 5412 listitem_free(item); 5413 } 5414 5415 /* 5416 * Get the number of items in a list. 5417 */ 5418 static long 5419 list_len(l) 5420 list_T *l; 5421 { 5422 if (l == NULL) 5423 return 0L; 5424 return l->lv_len; 5425 } 5426 5427 /* 5428 * Return TRUE when two lists have exactly the same values. 5429 */ 5430 static int 5431 list_equal(l1, l2, ic) 5432 list_T *l1; 5433 list_T *l2; 5434 int ic; /* ignore case for strings */ 5435 { 5436 listitem_T *item1, *item2; 5437 5438 if (list_len(l1) != list_len(l2)) 5439 return FALSE; 5440 5441 for (item1 = l1->lv_first, item2 = l2->lv_first; 5442 item1 != NULL && item2 != NULL; 5443 item1 = item1->li_next, item2 = item2->li_next) 5444 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic)) 5445 return FALSE; 5446 return item1 == NULL && item2 == NULL; 5447 } 5448 5449 #if defined(FEAT_PYTHON) || defined(PROTO) 5450 /* 5451 * Return the dictitem that an entry in a hashtable points to. 5452 */ 5453 dictitem_T * 5454 dict_lookup(hi) 5455 hashitem_T *hi; 5456 { 5457 return HI2DI(hi); 5458 } 5459 #endif 5460 5461 /* 5462 * Return TRUE when two dictionaries have exactly the same key/values. 5463 */ 5464 static int 5465 dict_equal(d1, d2, ic) 5466 dict_T *d1; 5467 dict_T *d2; 5468 int ic; /* ignore case for strings */ 5469 { 5470 hashitem_T *hi; 5471 dictitem_T *item2; 5472 int todo; 5473 5474 if (dict_len(d1) != dict_len(d2)) 5475 return FALSE; 5476 5477 todo = d1->dv_hashtab.ht_used; 5478 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi) 5479 { 5480 if (!HASHITEM_EMPTY(hi)) 5481 { 5482 item2 = dict_find(d2, hi->hi_key, -1); 5483 if (item2 == NULL) 5484 return FALSE; 5485 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic)) 5486 return FALSE; 5487 --todo; 5488 } 5489 } 5490 return TRUE; 5491 } 5492 5493 /* 5494 * Return TRUE if "tv1" and "tv2" have the same value. 5495 * Compares the items just like "==" would compare them, but strings and 5496 * numbers are different. 5497 */ 5498 static int 5499 tv_equal(tv1, tv2, ic) 5500 typval_T *tv1; 5501 typval_T *tv2; 5502 int ic; /* ignore case */ 5503 { 5504 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 5505 char_u *s1, *s2; 5506 5507 if (tv1->v_type != tv2->v_type) 5508 return FALSE; 5509 5510 switch (tv1->v_type) 5511 { 5512 case VAR_LIST: 5513 /* recursive! */ 5514 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic); 5515 5516 case VAR_DICT: 5517 /* recursive! */ 5518 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic); 5519 5520 case VAR_FUNC: 5521 return (tv1->vval.v_string != NULL 5522 && tv2->vval.v_string != NULL 5523 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0); 5524 5525 case VAR_NUMBER: 5526 return tv1->vval.v_number == tv2->vval.v_number; 5527 5528 case VAR_STRING: 5529 s1 = get_tv_string_buf(tv1, buf1); 5530 s2 = get_tv_string_buf(tv2, buf2); 5531 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0); 5532 } 5533 5534 EMSG2(_(e_intern2), "tv_equal()"); 5535 return TRUE; 5536 } 5537 5538 /* 5539 * Locate item with index "n" in list "l" and return it. 5540 * A negative index is counted from the end; -1 is the last item. 5541 * Returns NULL when "n" is out of range. 5542 */ 5543 static listitem_T * 5544 list_find(l, n) 5545 list_T *l; 5546 long n; 5547 { 5548 listitem_T *item; 5549 long idx; 5550 5551 if (l == NULL) 5552 return NULL; 5553 5554 /* Negative index is relative to the end. */ 5555 if (n < 0) 5556 n = l->lv_len + n; 5557 5558 /* Check for index out of range. */ 5559 if (n < 0 || n >= l->lv_len) 5560 return NULL; 5561 5562 /* When there is a cached index may start search from there. */ 5563 if (l->lv_idx_item != NULL) 5564 { 5565 if (n < l->lv_idx / 2) 5566 { 5567 /* closest to the start of the list */ 5568 item = l->lv_first; 5569 idx = 0; 5570 } 5571 else if (n > (l->lv_idx + l->lv_len) / 2) 5572 { 5573 /* closest to the end of the list */ 5574 item = l->lv_last; 5575 idx = l->lv_len - 1; 5576 } 5577 else 5578 { 5579 /* closest to the cached index */ 5580 item = l->lv_idx_item; 5581 idx = l->lv_idx; 5582 } 5583 } 5584 else 5585 { 5586 if (n < l->lv_len / 2) 5587 { 5588 /* closest to the start of the list */ 5589 item = l->lv_first; 5590 idx = 0; 5591 } 5592 else 5593 { 5594 /* closest to the end of the list */ 5595 item = l->lv_last; 5596 idx = l->lv_len - 1; 5597 } 5598 } 5599 5600 while (n > idx) 5601 { 5602 /* search forward */ 5603 item = item->li_next; 5604 ++idx; 5605 } 5606 while (n < idx) 5607 { 5608 /* search backward */ 5609 item = item->li_prev; 5610 --idx; 5611 } 5612 5613 /* cache the used index */ 5614 l->lv_idx = idx; 5615 l->lv_idx_item = item; 5616 5617 return item; 5618 } 5619 5620 /* 5621 * Get list item "l[idx]" as a number. 5622 */ 5623 static long 5624 list_find_nr(l, idx, errorp) 5625 list_T *l; 5626 long idx; 5627 int *errorp; /* set to TRUE when something wrong */ 5628 { 5629 listitem_T *li; 5630 5631 li = list_find(l, idx); 5632 if (li == NULL) 5633 { 5634 if (errorp != NULL) 5635 *errorp = TRUE; 5636 return -1L; 5637 } 5638 return get_tv_number_chk(&li->li_tv, errorp); 5639 } 5640 5641 /* 5642 * Locate "item" list "l" and return its index. 5643 * Returns -1 when "item" is not in the list. 5644 */ 5645 static long 5646 list_idx_of_item(l, item) 5647 list_T *l; 5648 listitem_T *item; 5649 { 5650 long idx = 0; 5651 listitem_T *li; 5652 5653 if (l == NULL) 5654 return -1; 5655 idx = 0; 5656 for (li = l->lv_first; li != NULL && li != item; li = li->li_next) 5657 ++idx; 5658 if (li == NULL) 5659 return -1; 5660 return idx; 5661 } 5662 5663 /* 5664 * Append item "item" to the end of list "l". 5665 */ 5666 static void 5667 list_append(l, item) 5668 list_T *l; 5669 listitem_T *item; 5670 { 5671 if (l->lv_last == NULL) 5672 { 5673 /* empty list */ 5674 l->lv_first = item; 5675 l->lv_last = item; 5676 item->li_prev = NULL; 5677 } 5678 else 5679 { 5680 l->lv_last->li_next = item; 5681 item->li_prev = l->lv_last; 5682 l->lv_last = item; 5683 } 5684 ++l->lv_len; 5685 item->li_next = NULL; 5686 } 5687 5688 /* 5689 * Append typval_T "tv" to the end of list "l". 5690 * Return FAIL when out of memory. 5691 */ 5692 static int 5693 list_append_tv(l, tv) 5694 list_T *l; 5695 typval_T *tv; 5696 { 5697 listitem_T *li = listitem_alloc(); 5698 5699 if (li == NULL) 5700 return FAIL; 5701 copy_tv(tv, &li->li_tv); 5702 list_append(l, li); 5703 return OK; 5704 } 5705 5706 /* 5707 * Add a dictionary to a list. Used by getqflist(). 5708 * Return FAIL when out of memory. 5709 */ 5710 int 5711 list_append_dict(list, dict) 5712 list_T *list; 5713 dict_T *dict; 5714 { 5715 listitem_T *li = listitem_alloc(); 5716 5717 if (li == NULL) 5718 return FAIL; 5719 li->li_tv.v_type = VAR_DICT; 5720 li->li_tv.v_lock = 0; 5721 li->li_tv.vval.v_dict = dict; 5722 list_append(list, li); 5723 ++dict->dv_refcount; 5724 return OK; 5725 } 5726 5727 /* 5728 * Make a copy of "str" and append it as an item to list "l". 5729 * When "len" >= 0 use "str[len]". 5730 * Returns FAIL when out of memory. 5731 */ 5732 static int 5733 list_append_string(l, str, len) 5734 list_T *l; 5735 char_u *str; 5736 int len; 5737 { 5738 listitem_T *li = listitem_alloc(); 5739 5740 if (li == NULL) 5741 return FAIL; 5742 list_append(l, li); 5743 li->li_tv.v_type = VAR_STRING; 5744 li->li_tv.v_lock = 0; 5745 if (str == NULL) 5746 li->li_tv.vval.v_string = NULL; 5747 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len) 5748 : vim_strsave(str))) == NULL) 5749 return FAIL; 5750 return OK; 5751 } 5752 5753 /* 5754 * Append "n" to list "l". 5755 * Returns FAIL when out of memory. 5756 */ 5757 static int 5758 list_append_number(l, n) 5759 list_T *l; 5760 varnumber_T n; 5761 { 5762 listitem_T *li; 5763 5764 li = listitem_alloc(); 5765 if (li == NULL) 5766 return FAIL; 5767 li->li_tv.v_type = VAR_NUMBER; 5768 li->li_tv.v_lock = 0; 5769 li->li_tv.vval.v_number = n; 5770 list_append(l, li); 5771 return OK; 5772 } 5773 5774 /* 5775 * Insert typval_T "tv" in list "l" before "item". 5776 * If "item" is NULL append at the end. 5777 * Return FAIL when out of memory. 5778 */ 5779 static int 5780 list_insert_tv(l, tv, item) 5781 list_T *l; 5782 typval_T *tv; 5783 listitem_T *item; 5784 { 5785 listitem_T *ni = listitem_alloc(); 5786 5787 if (ni == NULL) 5788 return FAIL; 5789 copy_tv(tv, &ni->li_tv); 5790 if (item == NULL) 5791 /* Append new item at end of list. */ 5792 list_append(l, ni); 5793 else 5794 { 5795 /* Insert new item before existing item. */ 5796 ni->li_prev = item->li_prev; 5797 ni->li_next = item; 5798 if (item->li_prev == NULL) 5799 { 5800 l->lv_first = ni; 5801 ++l->lv_idx; 5802 } 5803 else 5804 { 5805 item->li_prev->li_next = ni; 5806 l->lv_idx_item = NULL; 5807 } 5808 item->li_prev = ni; 5809 ++l->lv_len; 5810 } 5811 return OK; 5812 } 5813 5814 /* 5815 * Extend "l1" with "l2". 5816 * If "bef" is NULL append at the end, otherwise insert before this item. 5817 * Returns FAIL when out of memory. 5818 */ 5819 static int 5820 list_extend(l1, l2, bef) 5821 list_T *l1; 5822 list_T *l2; 5823 listitem_T *bef; 5824 { 5825 listitem_T *item; 5826 5827 for (item = l2->lv_first; item != NULL; item = item->li_next) 5828 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL) 5829 return FAIL; 5830 return OK; 5831 } 5832 5833 /* 5834 * Concatenate lists "l1" and "l2" into a new list, stored in "tv". 5835 * Return FAIL when out of memory. 5836 */ 5837 static int 5838 list_concat(l1, l2, tv) 5839 list_T *l1; 5840 list_T *l2; 5841 typval_T *tv; 5842 { 5843 list_T *l; 5844 5845 /* make a copy of the first list. */ 5846 l = list_copy(l1, FALSE, 0); 5847 if (l == NULL) 5848 return FAIL; 5849 tv->v_type = VAR_LIST; 5850 tv->vval.v_list = l; 5851 5852 /* append all items from the second list */ 5853 return list_extend(l, l2, NULL); 5854 } 5855 5856 /* 5857 * Make a copy of list "orig". Shallow if "deep" is FALSE. 5858 * The refcount of the new list is set to 1. 5859 * See item_copy() for "copyID". 5860 * Returns NULL when out of memory. 5861 */ 5862 static list_T * 5863 list_copy(orig, deep, copyID) 5864 list_T *orig; 5865 int deep; 5866 int copyID; 5867 { 5868 list_T *copy; 5869 listitem_T *item; 5870 listitem_T *ni; 5871 5872 if (orig == NULL) 5873 return NULL; 5874 5875 copy = list_alloc(); 5876 if (copy != NULL) 5877 { 5878 if (copyID != 0) 5879 { 5880 /* Do this before adding the items, because one of the items may 5881 * refer back to this list. */ 5882 orig->lv_copyID = copyID; 5883 orig->lv_copylist = copy; 5884 } 5885 for (item = orig->lv_first; item != NULL && !got_int; 5886 item = item->li_next) 5887 { 5888 ni = listitem_alloc(); 5889 if (ni == NULL) 5890 break; 5891 if (deep) 5892 { 5893 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL) 5894 { 5895 vim_free(ni); 5896 break; 5897 } 5898 } 5899 else 5900 copy_tv(&item->li_tv, &ni->li_tv); 5901 list_append(copy, ni); 5902 } 5903 ++copy->lv_refcount; 5904 if (item != NULL) 5905 { 5906 list_unref(copy); 5907 copy = NULL; 5908 } 5909 } 5910 5911 return copy; 5912 } 5913 5914 /* 5915 * Remove items "item" to "item2" from list "l". 5916 * Does not free the listitem or the value! 5917 */ 5918 static void 5919 list_remove(l, item, item2) 5920 list_T *l; 5921 listitem_T *item; 5922 listitem_T *item2; 5923 { 5924 listitem_T *ip; 5925 5926 /* notify watchers */ 5927 for (ip = item; ip != NULL; ip = ip->li_next) 5928 { 5929 --l->lv_len; 5930 list_fix_watch(l, ip); 5931 if (ip == item2) 5932 break; 5933 } 5934 5935 if (item2->li_next == NULL) 5936 l->lv_last = item->li_prev; 5937 else 5938 item2->li_next->li_prev = item->li_prev; 5939 if (item->li_prev == NULL) 5940 l->lv_first = item2->li_next; 5941 else 5942 item->li_prev->li_next = item2->li_next; 5943 l->lv_idx_item = NULL; 5944 } 5945 5946 /* 5947 * Return an allocated string with the string representation of a list. 5948 * May return NULL. 5949 */ 5950 static char_u * 5951 list2string(tv, copyID) 5952 typval_T *tv; 5953 int copyID; 5954 { 5955 garray_T ga; 5956 5957 if (tv->vval.v_list == NULL) 5958 return NULL; 5959 ga_init2(&ga, (int)sizeof(char), 80); 5960 ga_append(&ga, '['); 5961 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL) 5962 { 5963 vim_free(ga.ga_data); 5964 return NULL; 5965 } 5966 ga_append(&ga, ']'); 5967 ga_append(&ga, NUL); 5968 return (char_u *)ga.ga_data; 5969 } 5970 5971 /* 5972 * Join list "l" into a string in "*gap", using separator "sep". 5973 * When "echo" is TRUE use String as echoed, otherwise as inside a List. 5974 * Return FAIL or OK. 5975 */ 5976 static int 5977 list_join(gap, l, sep, echo, copyID) 5978 garray_T *gap; 5979 list_T *l; 5980 char_u *sep; 5981 int echo; 5982 int copyID; 5983 { 5984 int first = TRUE; 5985 char_u *tofree; 5986 char_u numbuf[NUMBUFLEN]; 5987 listitem_T *item; 5988 char_u *s; 5989 5990 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next) 5991 { 5992 if (first) 5993 first = FALSE; 5994 else 5995 ga_concat(gap, sep); 5996 5997 if (echo) 5998 s = echo_string(&item->li_tv, &tofree, numbuf, copyID); 5999 else 6000 s = tv2string(&item->li_tv, &tofree, numbuf, copyID); 6001 if (s != NULL) 6002 ga_concat(gap, s); 6003 vim_free(tofree); 6004 if (s == NULL) 6005 return FAIL; 6006 } 6007 return OK; 6008 } 6009 6010 /* 6011 * Garbage collection for lists and dictionaries. 6012 * 6013 * We use reference counts to be able to free most items right away when they 6014 * are no longer used. But for composite items it's possible that it becomes 6015 * unused while the reference count is > 0: When there is a recursive 6016 * reference. Example: 6017 * :let l = [1, 2, 3] 6018 * :let d = {9: l} 6019 * :let l[1] = d 6020 * 6021 * Since this is quite unusual we handle this with garbage collection: every 6022 * once in a while find out which lists and dicts are not referenced from any 6023 * variable. 6024 * 6025 * Here is a good reference text about garbage collection (refers to Python 6026 * but it applies to all reference-counting mechanisms): 6027 * http://python.ca/nas/python/gc/ 6028 */ 6029 6030 /* 6031 * Do garbage collection for lists and dicts. 6032 * Return TRUE if some memory was freed. 6033 */ 6034 int 6035 garbage_collect() 6036 { 6037 dict_T *dd; 6038 list_T *ll; 6039 int copyID = ++current_copyID; 6040 buf_T *buf; 6041 win_T *wp; 6042 int i; 6043 funccall_T *fc; 6044 int did_free = FALSE; 6045 #ifdef FEAT_WINDOWS 6046 tabpage_T *tp; 6047 #endif 6048 6049 /* 6050 * 1. Go through all accessible variables and mark all lists and dicts 6051 * with copyID. 6052 */ 6053 /* script-local variables */ 6054 for (i = 1; i <= ga_scripts.ga_len; ++i) 6055 set_ref_in_ht(&SCRIPT_VARS(i), copyID); 6056 6057 /* buffer-local variables */ 6058 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 6059 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID); 6060 6061 /* window-local variables */ 6062 FOR_ALL_TAB_WINDOWS(tp, wp) 6063 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID); 6064 6065 #ifdef FEAT_WINDOWS 6066 /* tabpage-local variables */ 6067 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) 6068 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID); 6069 #endif 6070 6071 /* global variables */ 6072 set_ref_in_ht(&globvarht, copyID); 6073 6074 /* function-local variables */ 6075 for (fc = current_funccal; fc != NULL; fc = fc->caller) 6076 { 6077 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID); 6078 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID); 6079 } 6080 6081 /* 6082 * 2. Go through the list of dicts and free items without the copyID. 6083 */ 6084 for (dd = first_dict; dd != NULL; ) 6085 if (dd->dv_copyID != copyID) 6086 { 6087 dict_free(dd); 6088 did_free = TRUE; 6089 6090 /* restart, next dict may also have been freed */ 6091 dd = first_dict; 6092 } 6093 else 6094 dd = dd->dv_used_next; 6095 6096 /* 6097 * 3. Go through the list of lists and free items without the copyID. 6098 * But don't free a list that has a watcher (used in a for loop), these 6099 * are not referenced anywhere. 6100 */ 6101 for (ll = first_list; ll != NULL; ) 6102 if (ll->lv_copyID != copyID && ll->lv_watch == NULL) 6103 { 6104 list_free(ll); 6105 did_free = TRUE; 6106 6107 /* restart, next list may also have been freed */ 6108 ll = first_list; 6109 } 6110 else 6111 ll = ll->lv_used_next; 6112 6113 return did_free; 6114 } 6115 6116 /* 6117 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". 6118 */ 6119 static void 6120 set_ref_in_ht(ht, copyID) 6121 hashtab_T *ht; 6122 int copyID; 6123 { 6124 int todo; 6125 hashitem_T *hi; 6126 6127 todo = ht->ht_used; 6128 for (hi = ht->ht_array; todo > 0; ++hi) 6129 if (!HASHITEM_EMPTY(hi)) 6130 { 6131 --todo; 6132 set_ref_in_item(&HI2DI(hi)->di_tv, copyID); 6133 } 6134 } 6135 6136 /* 6137 * Mark all lists and dicts referenced through list "l" with "copyID". 6138 */ 6139 static void 6140 set_ref_in_list(l, copyID) 6141 list_T *l; 6142 int copyID; 6143 { 6144 listitem_T *li; 6145 6146 for (li = l->lv_first; li != NULL; li = li->li_next) 6147 set_ref_in_item(&li->li_tv, copyID); 6148 } 6149 6150 /* 6151 * Mark all lists and dicts referenced through typval "tv" with "copyID". 6152 */ 6153 static void 6154 set_ref_in_item(tv, copyID) 6155 typval_T *tv; 6156 int copyID; 6157 { 6158 dict_T *dd; 6159 list_T *ll; 6160 6161 switch (tv->v_type) 6162 { 6163 case VAR_DICT: 6164 dd = tv->vval.v_dict; 6165 if (dd->dv_copyID != copyID) 6166 { 6167 /* Didn't see this dict yet. */ 6168 dd->dv_copyID = copyID; 6169 set_ref_in_ht(&dd->dv_hashtab, copyID); 6170 } 6171 break; 6172 6173 case VAR_LIST: 6174 ll = tv->vval.v_list; 6175 if (ll->lv_copyID != copyID) 6176 { 6177 /* Didn't see this list yet. */ 6178 ll->lv_copyID = copyID; 6179 set_ref_in_list(ll, copyID); 6180 } 6181 break; 6182 } 6183 return; 6184 } 6185 6186 /* 6187 * Allocate an empty header for a dictionary. 6188 */ 6189 dict_T * 6190 dict_alloc() 6191 { 6192 dict_T *d; 6193 6194 d = (dict_T *)alloc(sizeof(dict_T)); 6195 if (d != NULL) 6196 { 6197 /* Add the list to the hashtable for garbage collection. */ 6198 if (first_dict != NULL) 6199 first_dict->dv_used_prev = d; 6200 d->dv_used_next = first_dict; 6201 d->dv_used_prev = NULL; 6202 6203 hash_init(&d->dv_hashtab); 6204 d->dv_lock = 0; 6205 d->dv_refcount = 0; 6206 d->dv_copyID = 0; 6207 } 6208 return d; 6209 } 6210 6211 /* 6212 * Unreference a Dictionary: decrement the reference count and free it when it 6213 * becomes zero. 6214 */ 6215 static void 6216 dict_unref(d) 6217 dict_T *d; 6218 { 6219 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0) 6220 dict_free(d); 6221 } 6222 6223 /* 6224 * Free a Dictionary, including all items it contains. 6225 * Ignores the reference count. 6226 */ 6227 static void 6228 dict_free(d) 6229 dict_T *d; 6230 { 6231 int todo; 6232 hashitem_T *hi; 6233 dictitem_T *di; 6234 6235 /* Avoid that recursive reference to the dict frees us again. */ 6236 d->dv_refcount = DEL_REFCOUNT; 6237 6238 /* Remove the dict from the list of dicts for garbage collection. */ 6239 if (d->dv_used_prev == NULL) 6240 first_dict = d->dv_used_next; 6241 else 6242 d->dv_used_prev->dv_used_next = d->dv_used_next; 6243 if (d->dv_used_next != NULL) 6244 d->dv_used_next->dv_used_prev = d->dv_used_prev; 6245 6246 /* Lock the hashtab, we don't want it to resize while freeing items. */ 6247 hash_lock(&d->dv_hashtab); 6248 todo = d->dv_hashtab.ht_used; 6249 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 6250 { 6251 if (!HASHITEM_EMPTY(hi)) 6252 { 6253 /* Remove the item before deleting it, just in case there is 6254 * something recursive causing trouble. */ 6255 di = HI2DI(hi); 6256 hash_remove(&d->dv_hashtab, hi); 6257 dictitem_free(di); 6258 --todo; 6259 } 6260 } 6261 hash_clear(&d->dv_hashtab); 6262 vim_free(d); 6263 } 6264 6265 /* 6266 * Allocate a Dictionary item. 6267 * The "key" is copied to the new item. 6268 * Note that the value of the item "di_tv" still needs to be initialized! 6269 * Returns NULL when out of memory. 6270 */ 6271 static dictitem_T * 6272 dictitem_alloc(key) 6273 char_u *key; 6274 { 6275 dictitem_T *di; 6276 6277 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key)); 6278 if (di != NULL) 6279 { 6280 STRCPY(di->di_key, key); 6281 di->di_flags = 0; 6282 } 6283 return di; 6284 } 6285 6286 /* 6287 * Make a copy of a Dictionary item. 6288 */ 6289 static dictitem_T * 6290 dictitem_copy(org) 6291 dictitem_T *org; 6292 { 6293 dictitem_T *di; 6294 6295 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key)); 6296 if (di != NULL) 6297 { 6298 STRCPY(di->di_key, org->di_key); 6299 di->di_flags = 0; 6300 copy_tv(&org->di_tv, &di->di_tv); 6301 } 6302 return di; 6303 } 6304 6305 /* 6306 * Remove item "item" from Dictionary "dict" and free it. 6307 */ 6308 static void 6309 dictitem_remove(dict, item) 6310 dict_T *dict; 6311 dictitem_T *item; 6312 { 6313 hashitem_T *hi; 6314 6315 hi = hash_find(&dict->dv_hashtab, item->di_key); 6316 if (HASHITEM_EMPTY(hi)) 6317 EMSG2(_(e_intern2), "dictitem_remove()"); 6318 else 6319 hash_remove(&dict->dv_hashtab, hi); 6320 dictitem_free(item); 6321 } 6322 6323 /* 6324 * Free a dict item. Also clears the value. 6325 */ 6326 static void 6327 dictitem_free(item) 6328 dictitem_T *item; 6329 { 6330 clear_tv(&item->di_tv); 6331 vim_free(item); 6332 } 6333 6334 /* 6335 * Make a copy of dict "d". Shallow if "deep" is FALSE. 6336 * The refcount of the new dict is set to 1. 6337 * See item_copy() for "copyID". 6338 * Returns NULL when out of memory. 6339 */ 6340 static dict_T * 6341 dict_copy(orig, deep, copyID) 6342 dict_T *orig; 6343 int deep; 6344 int copyID; 6345 { 6346 dict_T *copy; 6347 dictitem_T *di; 6348 int todo; 6349 hashitem_T *hi; 6350 6351 if (orig == NULL) 6352 return NULL; 6353 6354 copy = dict_alloc(); 6355 if (copy != NULL) 6356 { 6357 if (copyID != 0) 6358 { 6359 orig->dv_copyID = copyID; 6360 orig->dv_copydict = copy; 6361 } 6362 todo = orig->dv_hashtab.ht_used; 6363 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) 6364 { 6365 if (!HASHITEM_EMPTY(hi)) 6366 { 6367 --todo; 6368 6369 di = dictitem_alloc(hi->hi_key); 6370 if (di == NULL) 6371 break; 6372 if (deep) 6373 { 6374 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep, 6375 copyID) == FAIL) 6376 { 6377 vim_free(di); 6378 break; 6379 } 6380 } 6381 else 6382 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv); 6383 if (dict_add(copy, di) == FAIL) 6384 { 6385 dictitem_free(di); 6386 break; 6387 } 6388 } 6389 } 6390 6391 ++copy->dv_refcount; 6392 if (todo > 0) 6393 { 6394 dict_unref(copy); 6395 copy = NULL; 6396 } 6397 } 6398 6399 return copy; 6400 } 6401 6402 /* 6403 * Add item "item" to Dictionary "d". 6404 * Returns FAIL when out of memory and when key already existed. 6405 */ 6406 static int 6407 dict_add(d, item) 6408 dict_T *d; 6409 dictitem_T *item; 6410 { 6411 return hash_add(&d->dv_hashtab, item->di_key); 6412 } 6413 6414 /* 6415 * Add a number or string entry to dictionary "d". 6416 * When "str" is NULL use number "nr", otherwise use "str". 6417 * Returns FAIL when out of memory and when key already exists. 6418 */ 6419 int 6420 dict_add_nr_str(d, key, nr, str) 6421 dict_T *d; 6422 char *key; 6423 long nr; 6424 char_u *str; 6425 { 6426 dictitem_T *item; 6427 6428 item = dictitem_alloc((char_u *)key); 6429 if (item == NULL) 6430 return FAIL; 6431 item->di_tv.v_lock = 0; 6432 if (str == NULL) 6433 { 6434 item->di_tv.v_type = VAR_NUMBER; 6435 item->di_tv.vval.v_number = nr; 6436 } 6437 else 6438 { 6439 item->di_tv.v_type = VAR_STRING; 6440 item->di_tv.vval.v_string = vim_strsave(str); 6441 } 6442 if (dict_add(d, item) == FAIL) 6443 { 6444 dictitem_free(item); 6445 return FAIL; 6446 } 6447 return OK; 6448 } 6449 6450 /* 6451 * Get the number of items in a Dictionary. 6452 */ 6453 static long 6454 dict_len(d) 6455 dict_T *d; 6456 { 6457 if (d == NULL) 6458 return 0L; 6459 return d->dv_hashtab.ht_used; 6460 } 6461 6462 /* 6463 * Find item "key[len]" in Dictionary "d". 6464 * If "len" is negative use strlen(key). 6465 * Returns NULL when not found. 6466 */ 6467 static dictitem_T * 6468 dict_find(d, key, len) 6469 dict_T *d; 6470 char_u *key; 6471 int len; 6472 { 6473 #define AKEYLEN 200 6474 char_u buf[AKEYLEN]; 6475 char_u *akey; 6476 char_u *tofree = NULL; 6477 hashitem_T *hi; 6478 6479 if (len < 0) 6480 akey = key; 6481 else if (len >= AKEYLEN) 6482 { 6483 tofree = akey = vim_strnsave(key, len); 6484 if (akey == NULL) 6485 return NULL; 6486 } 6487 else 6488 { 6489 /* Avoid a malloc/free by using buf[]. */ 6490 vim_strncpy(buf, key, len); 6491 akey = buf; 6492 } 6493 6494 hi = hash_find(&d->dv_hashtab, akey); 6495 vim_free(tofree); 6496 if (HASHITEM_EMPTY(hi)) 6497 return NULL; 6498 return HI2DI(hi); 6499 } 6500 6501 /* 6502 * Get a string item from a dictionary. 6503 * When "save" is TRUE allocate memory for it. 6504 * Returns NULL if the entry doesn't exist or out of memory. 6505 */ 6506 char_u * 6507 get_dict_string(d, key, save) 6508 dict_T *d; 6509 char_u *key; 6510 int save; 6511 { 6512 dictitem_T *di; 6513 char_u *s; 6514 6515 di = dict_find(d, key, -1); 6516 if (di == NULL) 6517 return NULL; 6518 s = get_tv_string(&di->di_tv); 6519 if (save && s != NULL) 6520 s = vim_strsave(s); 6521 return s; 6522 } 6523 6524 /* 6525 * Get a number item from a dictionary. 6526 * Returns 0 if the entry doesn't exist or out of memory. 6527 */ 6528 long 6529 get_dict_number(d, key) 6530 dict_T *d; 6531 char_u *key; 6532 { 6533 dictitem_T *di; 6534 6535 di = dict_find(d, key, -1); 6536 if (di == NULL) 6537 return 0; 6538 return get_tv_number(&di->di_tv); 6539 } 6540 6541 /* 6542 * Return an allocated string with the string representation of a Dictionary. 6543 * May return NULL. 6544 */ 6545 static char_u * 6546 dict2string(tv, copyID) 6547 typval_T *tv; 6548 int copyID; 6549 { 6550 garray_T ga; 6551 int first = TRUE; 6552 char_u *tofree; 6553 char_u numbuf[NUMBUFLEN]; 6554 hashitem_T *hi; 6555 char_u *s; 6556 dict_T *d; 6557 int todo; 6558 6559 if ((d = tv->vval.v_dict) == NULL) 6560 return NULL; 6561 ga_init2(&ga, (int)sizeof(char), 80); 6562 ga_append(&ga, '{'); 6563 6564 todo = d->dv_hashtab.ht_used; 6565 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) 6566 { 6567 if (!HASHITEM_EMPTY(hi)) 6568 { 6569 --todo; 6570 6571 if (first) 6572 first = FALSE; 6573 else 6574 ga_concat(&ga, (char_u *)", "); 6575 6576 tofree = string_quote(hi->hi_key, FALSE); 6577 if (tofree != NULL) 6578 { 6579 ga_concat(&ga, tofree); 6580 vim_free(tofree); 6581 } 6582 ga_concat(&ga, (char_u *)": "); 6583 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID); 6584 if (s != NULL) 6585 ga_concat(&ga, s); 6586 vim_free(tofree); 6587 if (s == NULL) 6588 break; 6589 } 6590 } 6591 if (todo > 0) 6592 { 6593 vim_free(ga.ga_data); 6594 return NULL; 6595 } 6596 6597 ga_append(&ga, '}'); 6598 ga_append(&ga, NUL); 6599 return (char_u *)ga.ga_data; 6600 } 6601 6602 /* 6603 * Allocate a variable for a Dictionary and fill it from "*arg". 6604 * Return OK or FAIL. Returns NOTDONE for {expr}. 6605 */ 6606 static int 6607 get_dict_tv(arg, rettv, evaluate) 6608 char_u **arg; 6609 typval_T *rettv; 6610 int evaluate; 6611 { 6612 dict_T *d = NULL; 6613 typval_T tvkey; 6614 typval_T tv; 6615 char_u *key; 6616 dictitem_T *item; 6617 char_u *start = skipwhite(*arg + 1); 6618 char_u buf[NUMBUFLEN]; 6619 6620 /* 6621 * First check if it's not a curly-braces thing: {expr}. 6622 * Must do this without evaluating, otherwise a function may be called 6623 * twice. Unfortunately this means we need to call eval1() twice for the 6624 * first item. 6625 * But {} is an empty Dictionary. 6626 */ 6627 if (*start != '}') 6628 { 6629 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */ 6630 return FAIL; 6631 if (*start == '}') 6632 return NOTDONE; 6633 } 6634 6635 if (evaluate) 6636 { 6637 d = dict_alloc(); 6638 if (d == NULL) 6639 return FAIL; 6640 } 6641 tvkey.v_type = VAR_UNKNOWN; 6642 tv.v_type = VAR_UNKNOWN; 6643 6644 *arg = skipwhite(*arg + 1); 6645 while (**arg != '}' && **arg != NUL) 6646 { 6647 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */ 6648 goto failret; 6649 if (**arg != ':') 6650 { 6651 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg); 6652 clear_tv(&tvkey); 6653 goto failret; 6654 } 6655 key = get_tv_string_buf_chk(&tvkey, buf); 6656 if (key == NULL || *key == NUL) 6657 { 6658 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */ 6659 if (key != NULL) 6660 EMSG(_(e_emptykey)); 6661 clear_tv(&tvkey); 6662 goto failret; 6663 } 6664 6665 *arg = skipwhite(*arg + 1); 6666 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */ 6667 { 6668 clear_tv(&tvkey); 6669 goto failret; 6670 } 6671 if (evaluate) 6672 { 6673 item = dict_find(d, key, -1); 6674 if (item != NULL) 6675 { 6676 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key); 6677 clear_tv(&tvkey); 6678 clear_tv(&tv); 6679 goto failret; 6680 } 6681 item = dictitem_alloc(key); 6682 clear_tv(&tvkey); 6683 if (item != NULL) 6684 { 6685 item->di_tv = tv; 6686 item->di_tv.v_lock = 0; 6687 if (dict_add(d, item) == FAIL) 6688 dictitem_free(item); 6689 } 6690 } 6691 6692 if (**arg == '}') 6693 break; 6694 if (**arg != ',') 6695 { 6696 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg); 6697 goto failret; 6698 } 6699 *arg = skipwhite(*arg + 1); 6700 } 6701 6702 if (**arg != '}') 6703 { 6704 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg); 6705 failret: 6706 if (evaluate) 6707 dict_free(d); 6708 return FAIL; 6709 } 6710 6711 *arg = skipwhite(*arg + 1); 6712 if (evaluate) 6713 { 6714 rettv->v_type = VAR_DICT; 6715 rettv->vval.v_dict = d; 6716 ++d->dv_refcount; 6717 } 6718 6719 return OK; 6720 } 6721 6722 /* 6723 * Return a string with the string representation of a variable. 6724 * If the memory is allocated "tofree" is set to it, otherwise NULL. 6725 * "numbuf" is used for a number. 6726 * Does not put quotes around strings, as ":echo" displays values. 6727 * When "copyID" is not NULL replace recursive lists and dicts with "...". 6728 * May return NULL; 6729 */ 6730 static char_u * 6731 echo_string(tv, tofree, numbuf, copyID) 6732 typval_T *tv; 6733 char_u **tofree; 6734 char_u *numbuf; 6735 int copyID; 6736 { 6737 static int recurse = 0; 6738 char_u *r = NULL; 6739 6740 if (recurse >= DICT_MAXNEST) 6741 { 6742 EMSG(_("E724: variable nested too deep for displaying")); 6743 *tofree = NULL; 6744 return NULL; 6745 } 6746 ++recurse; 6747 6748 switch (tv->v_type) 6749 { 6750 case VAR_FUNC: 6751 *tofree = NULL; 6752 r = tv->vval.v_string; 6753 break; 6754 6755 case VAR_LIST: 6756 if (tv->vval.v_list == NULL) 6757 { 6758 *tofree = NULL; 6759 r = NULL; 6760 } 6761 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID) 6762 { 6763 *tofree = NULL; 6764 r = (char_u *)"[...]"; 6765 } 6766 else 6767 { 6768 tv->vval.v_list->lv_copyID = copyID; 6769 *tofree = list2string(tv, copyID); 6770 r = *tofree; 6771 } 6772 break; 6773 6774 case VAR_DICT: 6775 if (tv->vval.v_dict == NULL) 6776 { 6777 *tofree = NULL; 6778 r = NULL; 6779 } 6780 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID) 6781 { 6782 *tofree = NULL; 6783 r = (char_u *)"{...}"; 6784 } 6785 else 6786 { 6787 tv->vval.v_dict->dv_copyID = copyID; 6788 *tofree = dict2string(tv, copyID); 6789 r = *tofree; 6790 } 6791 break; 6792 6793 case VAR_STRING: 6794 case VAR_NUMBER: 6795 *tofree = NULL; 6796 r = get_tv_string_buf(tv, numbuf); 6797 break; 6798 6799 default: 6800 EMSG2(_(e_intern2), "echo_string()"); 6801 *tofree = NULL; 6802 } 6803 6804 --recurse; 6805 return r; 6806 } 6807 6808 /* 6809 * Return a string with the string representation of a variable. 6810 * If the memory is allocated "tofree" is set to it, otherwise NULL. 6811 * "numbuf" is used for a number. 6812 * Puts quotes around strings, so that they can be parsed back by eval(). 6813 * May return NULL; 6814 */ 6815 static char_u * 6816 tv2string(tv, tofree, numbuf, copyID) 6817 typval_T *tv; 6818 char_u **tofree; 6819 char_u *numbuf; 6820 int copyID; 6821 { 6822 switch (tv->v_type) 6823 { 6824 case VAR_FUNC: 6825 *tofree = string_quote(tv->vval.v_string, TRUE); 6826 return *tofree; 6827 case VAR_STRING: 6828 *tofree = string_quote(tv->vval.v_string, FALSE); 6829 return *tofree; 6830 case VAR_NUMBER: 6831 case VAR_LIST: 6832 case VAR_DICT: 6833 break; 6834 default: 6835 EMSG2(_(e_intern2), "tv2string()"); 6836 } 6837 return echo_string(tv, tofree, numbuf, copyID); 6838 } 6839 6840 /* 6841 * Return string "str" in ' quotes, doubling ' characters. 6842 * If "str" is NULL an empty string is assumed. 6843 * If "function" is TRUE make it function('string'). 6844 */ 6845 static char_u * 6846 string_quote(str, function) 6847 char_u *str; 6848 int function; 6849 { 6850 unsigned len; 6851 char_u *p, *r, *s; 6852 6853 len = (function ? 13 : 3); 6854 if (str != NULL) 6855 { 6856 len += STRLEN(str); 6857 for (p = str; *p != NUL; mb_ptr_adv(p)) 6858 if (*p == '\'') 6859 ++len; 6860 } 6861 s = r = alloc(len); 6862 if (r != NULL) 6863 { 6864 if (function) 6865 { 6866 STRCPY(r, "function('"); 6867 r += 10; 6868 } 6869 else 6870 *r++ = '\''; 6871 if (str != NULL) 6872 for (p = str; *p != NUL; ) 6873 { 6874 if (*p == '\'') 6875 *r++ = '\''; 6876 MB_COPY_CHAR(p, r); 6877 } 6878 *r++ = '\''; 6879 if (function) 6880 *r++ = ')'; 6881 *r++ = NUL; 6882 } 6883 return s; 6884 } 6885 6886 /* 6887 * Get the value of an environment variable. 6888 * "arg" is pointing to the '$'. It is advanced to after the name. 6889 * If the environment variable was not set, silently assume it is empty. 6890 * Always return OK. 6891 */ 6892 static int 6893 get_env_tv(arg, rettv, evaluate) 6894 char_u **arg; 6895 typval_T *rettv; 6896 int evaluate; 6897 { 6898 char_u *string = NULL; 6899 int len; 6900 int cc; 6901 char_u *name; 6902 int mustfree = FALSE; 6903 6904 ++*arg; 6905 name = *arg; 6906 len = get_env_len(arg); 6907 if (evaluate) 6908 { 6909 if (len != 0) 6910 { 6911 cc = name[len]; 6912 name[len] = NUL; 6913 /* first try vim_getenv(), fast for normal environment vars */ 6914 string = vim_getenv(name, &mustfree); 6915 if (string != NULL && *string != NUL) 6916 { 6917 if (!mustfree) 6918 string = vim_strsave(string); 6919 } 6920 else 6921 { 6922 if (mustfree) 6923 vim_free(string); 6924 6925 /* next try expanding things like $VIM and ${HOME} */ 6926 string = expand_env_save(name - 1); 6927 if (string != NULL && *string == '$') 6928 { 6929 vim_free(string); 6930 string = NULL; 6931 } 6932 } 6933 name[len] = cc; 6934 } 6935 rettv->v_type = VAR_STRING; 6936 rettv->vval.v_string = string; 6937 } 6938 6939 return OK; 6940 } 6941 6942 /* 6943 * Array with names and number of arguments of all internal functions 6944 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH! 6945 */ 6946 static struct fst 6947 { 6948 char *f_name; /* function name */ 6949 char f_min_argc; /* minimal number of arguments */ 6950 char f_max_argc; /* maximal number of arguments */ 6951 void (*f_func) __ARGS((typval_T *args, typval_T *rvar)); 6952 /* implemenation of function */ 6953 } functions[] = 6954 { 6955 {"add", 2, 2, f_add}, 6956 {"append", 2, 2, f_append}, 6957 {"argc", 0, 0, f_argc}, 6958 {"argidx", 0, 0, f_argidx}, 6959 {"argv", 0, 1, f_argv}, 6960 {"browse", 4, 4, f_browse}, 6961 {"browsedir", 2, 2, f_browsedir}, 6962 {"bufexists", 1, 1, f_bufexists}, 6963 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */ 6964 {"buffer_name", 1, 1, f_bufname}, /* obsolete */ 6965 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */ 6966 {"buflisted", 1, 1, f_buflisted}, 6967 {"bufloaded", 1, 1, f_bufloaded}, 6968 {"bufname", 1, 1, f_bufname}, 6969 {"bufnr", 1, 2, f_bufnr}, 6970 {"bufwinnr", 1, 1, f_bufwinnr}, 6971 {"byte2line", 1, 1, f_byte2line}, 6972 {"byteidx", 2, 2, f_byteidx}, 6973 {"call", 2, 3, f_call}, 6974 {"changenr", 0, 0, f_changenr}, 6975 {"char2nr", 1, 1, f_char2nr}, 6976 {"cindent", 1, 1, f_cindent}, 6977 {"col", 1, 1, f_col}, 6978 #if defined(FEAT_INS_EXPAND) 6979 {"complete", 2, 2, f_complete}, 6980 {"complete_add", 1, 1, f_complete_add}, 6981 {"complete_check", 0, 0, f_complete_check}, 6982 #endif 6983 {"confirm", 1, 4, f_confirm}, 6984 {"copy", 1, 1, f_copy}, 6985 {"count", 2, 4, f_count}, 6986 {"cscope_connection",0,3, f_cscope_connection}, 6987 {"cursor", 1, 3, f_cursor}, 6988 {"deepcopy", 1, 2, f_deepcopy}, 6989 {"delete", 1, 1, f_delete}, 6990 {"did_filetype", 0, 0, f_did_filetype}, 6991 {"diff_filler", 1, 1, f_diff_filler}, 6992 {"diff_hlID", 2, 2, f_diff_hlID}, 6993 {"empty", 1, 1, f_empty}, 6994 {"escape", 2, 2, f_escape}, 6995 {"eval", 1, 1, f_eval}, 6996 {"eventhandler", 0, 0, f_eventhandler}, 6997 {"executable", 1, 1, f_executable}, 6998 {"exists", 1, 1, f_exists}, 6999 {"expand", 1, 2, f_expand}, 7000 {"extend", 2, 3, f_extend}, 7001 {"file_readable", 1, 1, f_filereadable}, /* obsolete */ 7002 {"filereadable", 1, 1, f_filereadable}, 7003 {"filewritable", 1, 1, f_filewritable}, 7004 {"filter", 2, 2, f_filter}, 7005 {"finddir", 1, 3, f_finddir}, 7006 {"findfile", 1, 3, f_findfile}, 7007 {"fnamemodify", 2, 2, f_fnamemodify}, 7008 {"foldclosed", 1, 1, f_foldclosed}, 7009 {"foldclosedend", 1, 1, f_foldclosedend}, 7010 {"foldlevel", 1, 1, f_foldlevel}, 7011 {"foldtext", 0, 0, f_foldtext}, 7012 {"foldtextresult", 1, 1, f_foldtextresult}, 7013 {"foreground", 0, 0, f_foreground}, 7014 {"function", 1, 1, f_function}, 7015 {"garbagecollect", 0, 0, f_garbagecollect}, 7016 {"get", 2, 3, f_get}, 7017 {"getbufline", 2, 3, f_getbufline}, 7018 {"getbufvar", 2, 2, f_getbufvar}, 7019 {"getchar", 0, 1, f_getchar}, 7020 {"getcharmod", 0, 0, f_getcharmod}, 7021 {"getcmdline", 0, 0, f_getcmdline}, 7022 {"getcmdpos", 0, 0, f_getcmdpos}, 7023 {"getcmdtype", 0, 0, f_getcmdtype}, 7024 {"getcwd", 0, 0, f_getcwd}, 7025 {"getfontname", 0, 1, f_getfontname}, 7026 {"getfperm", 1, 1, f_getfperm}, 7027 {"getfsize", 1, 1, f_getfsize}, 7028 {"getftime", 1, 1, f_getftime}, 7029 {"getftype", 1, 1, f_getftype}, 7030 {"getline", 1, 2, f_getline}, 7031 {"getloclist", 1, 1, f_getqflist}, 7032 {"getpos", 1, 1, f_getpos}, 7033 {"getqflist", 0, 0, f_getqflist}, 7034 {"getreg", 0, 2, f_getreg}, 7035 {"getregtype", 0, 1, f_getregtype}, 7036 {"getwinposx", 0, 0, f_getwinposx}, 7037 {"getwinposy", 0, 0, f_getwinposy}, 7038 {"getwinvar", 2, 2, f_getwinvar}, 7039 {"glob", 1, 1, f_glob}, 7040 {"globpath", 2, 2, f_globpath}, 7041 {"has", 1, 1, f_has}, 7042 {"has_key", 2, 2, f_has_key}, 7043 {"hasmapto", 1, 3, f_hasmapto}, 7044 {"highlightID", 1, 1, f_hlID}, /* obsolete */ 7045 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */ 7046 {"histadd", 2, 2, f_histadd}, 7047 {"histdel", 1, 2, f_histdel}, 7048 {"histget", 1, 2, f_histget}, 7049 {"histnr", 1, 1, f_histnr}, 7050 {"hlID", 1, 1, f_hlID}, 7051 {"hlexists", 1, 1, f_hlexists}, 7052 {"hostname", 0, 0, f_hostname}, 7053 {"iconv", 3, 3, f_iconv}, 7054 {"indent", 1, 1, f_indent}, 7055 {"index", 2, 4, f_index}, 7056 {"input", 1, 3, f_input}, 7057 {"inputdialog", 1, 3, f_inputdialog}, 7058 {"inputlist", 1, 1, f_inputlist}, 7059 {"inputrestore", 0, 0, f_inputrestore}, 7060 {"inputsave", 0, 0, f_inputsave}, 7061 {"inputsecret", 1, 2, f_inputsecret}, 7062 {"insert", 2, 3, f_insert}, 7063 {"isdirectory", 1, 1, f_isdirectory}, 7064 {"islocked", 1, 1, f_islocked}, 7065 {"items", 1, 1, f_items}, 7066 {"join", 1, 2, f_join}, 7067 {"keys", 1, 1, f_keys}, 7068 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */ 7069 {"len", 1, 1, f_len}, 7070 {"libcall", 3, 3, f_libcall}, 7071 {"libcallnr", 3, 3, f_libcallnr}, 7072 {"line", 1, 1, f_line}, 7073 {"line2byte", 1, 1, f_line2byte}, 7074 {"lispindent", 1, 1, f_lispindent}, 7075 {"localtime", 0, 0, f_localtime}, 7076 {"map", 2, 2, f_map}, 7077 {"maparg", 1, 3, f_maparg}, 7078 {"mapcheck", 1, 3, f_mapcheck}, 7079 {"match", 2, 4, f_match}, 7080 {"matcharg", 1, 1, f_matcharg}, 7081 {"matchend", 2, 4, f_matchend}, 7082 {"matchlist", 2, 4, f_matchlist}, 7083 {"matchstr", 2, 4, f_matchstr}, 7084 {"max", 1, 1, f_max}, 7085 {"min", 1, 1, f_min}, 7086 #ifdef vim_mkdir 7087 {"mkdir", 1, 3, f_mkdir}, 7088 #endif 7089 {"mode", 0, 0, f_mode}, 7090 {"nextnonblank", 1, 1, f_nextnonblank}, 7091 {"nr2char", 1, 1, f_nr2char}, 7092 {"pathshorten", 1, 1, f_pathshorten}, 7093 {"prevnonblank", 1, 1, f_prevnonblank}, 7094 {"printf", 2, 19, f_printf}, 7095 {"pumvisible", 0, 0, f_pumvisible}, 7096 {"range", 1, 3, f_range}, 7097 {"readfile", 1, 3, f_readfile}, 7098 {"reltime", 0, 2, f_reltime}, 7099 {"reltimestr", 1, 1, f_reltimestr}, 7100 {"remote_expr", 2, 3, f_remote_expr}, 7101 {"remote_foreground", 1, 1, f_remote_foreground}, 7102 {"remote_peek", 1, 2, f_remote_peek}, 7103 {"remote_read", 1, 1, f_remote_read}, 7104 {"remote_send", 2, 3, f_remote_send}, 7105 {"remove", 2, 3, f_remove}, 7106 {"rename", 2, 2, f_rename}, 7107 {"repeat", 2, 2, f_repeat}, 7108 {"resolve", 1, 1, f_resolve}, 7109 {"reverse", 1, 1, f_reverse}, 7110 {"search", 1, 3, f_search}, 7111 {"searchdecl", 1, 3, f_searchdecl}, 7112 {"searchpair", 3, 6, f_searchpair}, 7113 {"searchpairpos", 3, 6, f_searchpairpos}, 7114 {"searchpos", 1, 3, f_searchpos}, 7115 {"server2client", 2, 2, f_server2client}, 7116 {"serverlist", 0, 0, f_serverlist}, 7117 {"setbufvar", 3, 3, f_setbufvar}, 7118 {"setcmdpos", 1, 1, f_setcmdpos}, 7119 {"setline", 2, 2, f_setline}, 7120 {"setloclist", 2, 3, f_setloclist}, 7121 {"setpos", 2, 2, f_setpos}, 7122 {"setqflist", 1, 2, f_setqflist}, 7123 {"setreg", 2, 3, f_setreg}, 7124 {"setwinvar", 3, 3, f_setwinvar}, 7125 {"simplify", 1, 1, f_simplify}, 7126 {"sort", 1, 2, f_sort}, 7127 {"soundfold", 1, 1, f_soundfold}, 7128 {"spellbadword", 0, 1, f_spellbadword}, 7129 {"spellsuggest", 1, 3, f_spellsuggest}, 7130 {"split", 1, 3, f_split}, 7131 {"str2nr", 1, 2, f_str2nr}, 7132 #ifdef HAVE_STRFTIME 7133 {"strftime", 1, 2, f_strftime}, 7134 #endif 7135 {"stridx", 2, 3, f_stridx}, 7136 {"string", 1, 1, f_string}, 7137 {"strlen", 1, 1, f_strlen}, 7138 {"strpart", 2, 3, f_strpart}, 7139 {"strridx", 2, 3, f_strridx}, 7140 {"strtrans", 1, 1, f_strtrans}, 7141 {"submatch", 1, 1, f_submatch}, 7142 {"substitute", 4, 4, f_substitute}, 7143 {"synID", 3, 3, f_synID}, 7144 {"synIDattr", 2, 3, f_synIDattr}, 7145 {"synIDtrans", 1, 1, f_synIDtrans}, 7146 {"system", 1, 2, f_system}, 7147 {"tabpagebuflist", 0, 1, f_tabpagebuflist}, 7148 {"tabpagenr", 0, 1, f_tabpagenr}, 7149 {"tabpagewinnr", 1, 2, f_tabpagewinnr}, 7150 {"tagfiles", 0, 0, f_tagfiles}, 7151 {"taglist", 1, 1, f_taglist}, 7152 {"tempname", 0, 0, f_tempname}, 7153 {"test", 1, 1, f_test}, 7154 {"tolower", 1, 1, f_tolower}, 7155 {"toupper", 1, 1, f_toupper}, 7156 {"tr", 3, 3, f_tr}, 7157 {"type", 1, 1, f_type}, 7158 {"values", 1, 1, f_values}, 7159 {"virtcol", 1, 1, f_virtcol}, 7160 {"visualmode", 0, 1, f_visualmode}, 7161 {"winbufnr", 1, 1, f_winbufnr}, 7162 {"wincol", 0, 0, f_wincol}, 7163 {"winheight", 1, 1, f_winheight}, 7164 {"winline", 0, 0, f_winline}, 7165 {"winnr", 0, 1, f_winnr}, 7166 {"winrestcmd", 0, 0, f_winrestcmd}, 7167 {"winrestview", 1, 1, f_winrestview}, 7168 {"winsaveview", 0, 0, f_winsaveview}, 7169 {"winwidth", 1, 1, f_winwidth}, 7170 {"writefile", 2, 3, f_writefile}, 7171 }; 7172 7173 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 7174 7175 /* 7176 * Function given to ExpandGeneric() to obtain the list of internal 7177 * or user defined function names. 7178 */ 7179 char_u * 7180 get_function_name(xp, idx) 7181 expand_T *xp; 7182 int idx; 7183 { 7184 static int intidx = -1; 7185 char_u *name; 7186 7187 if (idx == 0) 7188 intidx = -1; 7189 if (intidx < 0) 7190 { 7191 name = get_user_func_name(xp, idx); 7192 if (name != NULL) 7193 return name; 7194 } 7195 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst))) 7196 { 7197 STRCPY(IObuff, functions[intidx].f_name); 7198 STRCAT(IObuff, "("); 7199 if (functions[intidx].f_max_argc == 0) 7200 STRCAT(IObuff, ")"); 7201 return IObuff; 7202 } 7203 7204 return NULL; 7205 } 7206 7207 /* 7208 * Function given to ExpandGeneric() to obtain the list of internal or 7209 * user defined variable or function names. 7210 */ 7211 /*ARGSUSED*/ 7212 char_u * 7213 get_expr_name(xp, idx) 7214 expand_T *xp; 7215 int idx; 7216 { 7217 static int intidx = -1; 7218 char_u *name; 7219 7220 if (idx == 0) 7221 intidx = -1; 7222 if (intidx < 0) 7223 { 7224 name = get_function_name(xp, idx); 7225 if (name != NULL) 7226 return name; 7227 } 7228 return get_user_var_name(xp, ++intidx); 7229 } 7230 7231 #endif /* FEAT_CMDL_COMPL */ 7232 7233 /* 7234 * Find internal function in table above. 7235 * Return index, or -1 if not found 7236 */ 7237 static int 7238 find_internal_func(name) 7239 char_u *name; /* name of the function */ 7240 { 7241 int first = 0; 7242 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1; 7243 int cmp; 7244 int x; 7245 7246 /* 7247 * Find the function name in the table. Binary search. 7248 */ 7249 while (first <= last) 7250 { 7251 x = first + ((unsigned)(last - first) >> 1); 7252 cmp = STRCMP(name, functions[x].f_name); 7253 if (cmp < 0) 7254 last = x - 1; 7255 else if (cmp > 0) 7256 first = x + 1; 7257 else 7258 return x; 7259 } 7260 return -1; 7261 } 7262 7263 /* 7264 * Check if "name" is a variable of type VAR_FUNC. If so, return the function 7265 * name it contains, otherwise return "name". 7266 */ 7267 static char_u * 7268 deref_func_name(name, lenp) 7269 char_u *name; 7270 int *lenp; 7271 { 7272 dictitem_T *v; 7273 int cc; 7274 7275 cc = name[*lenp]; 7276 name[*lenp] = NUL; 7277 v = find_var(name, NULL); 7278 name[*lenp] = cc; 7279 if (v != NULL && v->di_tv.v_type == VAR_FUNC) 7280 { 7281 if (v->di_tv.vval.v_string == NULL) 7282 { 7283 *lenp = 0; 7284 return (char_u *)""; /* just in case */ 7285 } 7286 *lenp = STRLEN(v->di_tv.vval.v_string); 7287 return v->di_tv.vval.v_string; 7288 } 7289 7290 return name; 7291 } 7292 7293 /* 7294 * Allocate a variable for the result of a function. 7295 * Return OK or FAIL. 7296 */ 7297 static int 7298 get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange, 7299 evaluate, selfdict) 7300 char_u *name; /* name of the function */ 7301 int len; /* length of "name" */ 7302 typval_T *rettv; 7303 char_u **arg; /* argument, pointing to the '(' */ 7304 linenr_T firstline; /* first line of range */ 7305 linenr_T lastline; /* last line of range */ 7306 int *doesrange; /* return: function handled range */ 7307 int evaluate; 7308 dict_T *selfdict; /* Dictionary for "self" */ 7309 { 7310 char_u *argp; 7311 int ret = OK; 7312 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */ 7313 int argcount = 0; /* number of arguments found */ 7314 7315 /* 7316 * Get the arguments. 7317 */ 7318 argp = *arg; 7319 while (argcount < MAX_FUNC_ARGS) 7320 { 7321 argp = skipwhite(argp + 1); /* skip the '(' or ',' */ 7322 if (*argp == ')' || *argp == ',' || *argp == NUL) 7323 break; 7324 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL) 7325 { 7326 ret = FAIL; 7327 break; 7328 } 7329 ++argcount; 7330 if (*argp != ',') 7331 break; 7332 } 7333 if (*argp == ')') 7334 ++argp; 7335 else 7336 ret = FAIL; 7337 7338 if (ret == OK) 7339 ret = call_func(name, len, rettv, argcount, argvars, 7340 firstline, lastline, doesrange, evaluate, selfdict); 7341 else if (!aborting()) 7342 { 7343 if (argcount == MAX_FUNC_ARGS) 7344 emsg_funcname("E740: Too many arguments for function %s", name); 7345 else 7346 emsg_funcname("E116: Invalid arguments for function %s", name); 7347 } 7348 7349 while (--argcount >= 0) 7350 clear_tv(&argvars[argcount]); 7351 7352 *arg = skipwhite(argp); 7353 return ret; 7354 } 7355 7356 7357 /* 7358 * Call a function with its resolved parameters 7359 * Return OK when the function can't be called, FAIL otherwise. 7360 * Also returns OK when an error was encountered while executing the function. 7361 */ 7362 static int 7363 call_func(name, len, rettv, argcount, argvars, firstline, lastline, 7364 doesrange, evaluate, selfdict) 7365 char_u *name; /* name of the function */ 7366 int len; /* length of "name" */ 7367 typval_T *rettv; /* return value goes here */ 7368 int argcount; /* number of "argvars" */ 7369 typval_T *argvars; /* vars for arguments */ 7370 linenr_T firstline; /* first line of range */ 7371 linenr_T lastline; /* last line of range */ 7372 int *doesrange; /* return: function handled range */ 7373 int evaluate; 7374 dict_T *selfdict; /* Dictionary for "self" */ 7375 { 7376 int ret = FAIL; 7377 #define ERROR_UNKNOWN 0 7378 #define ERROR_TOOMANY 1 7379 #define ERROR_TOOFEW 2 7380 #define ERROR_SCRIPT 3 7381 #define ERROR_DICT 4 7382 #define ERROR_NONE 5 7383 #define ERROR_OTHER 6 7384 int error = ERROR_NONE; 7385 int i; 7386 int llen; 7387 ufunc_T *fp; 7388 int cc; 7389 #define FLEN_FIXED 40 7390 char_u fname_buf[FLEN_FIXED + 1]; 7391 char_u *fname; 7392 7393 /* 7394 * In a script change <SID>name() and s:name() to K_SNR 123_name(). 7395 * Change <SNR>123_name() to K_SNR 123_name(). 7396 * Use fname_buf[] when it fits, otherwise allocate memory (slow). 7397 */ 7398 cc = name[len]; 7399 name[len] = NUL; 7400 llen = eval_fname_script(name); 7401 if (llen > 0) 7402 { 7403 fname_buf[0] = K_SPECIAL; 7404 fname_buf[1] = KS_EXTRA; 7405 fname_buf[2] = (int)KE_SNR; 7406 i = 3; 7407 if (eval_fname_sid(name)) /* "<SID>" or "s:" */ 7408 { 7409 if (current_SID <= 0) 7410 error = ERROR_SCRIPT; 7411 else 7412 { 7413 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID); 7414 i = (int)STRLEN(fname_buf); 7415 } 7416 } 7417 if (i + STRLEN(name + llen) < FLEN_FIXED) 7418 { 7419 STRCPY(fname_buf + i, name + llen); 7420 fname = fname_buf; 7421 } 7422 else 7423 { 7424 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1)); 7425 if (fname == NULL) 7426 error = ERROR_OTHER; 7427 else 7428 { 7429 mch_memmove(fname, fname_buf, (size_t)i); 7430 STRCPY(fname + i, name + llen); 7431 } 7432 } 7433 } 7434 else 7435 fname = name; 7436 7437 *doesrange = FALSE; 7438 7439 7440 /* execute the function if no errors detected and executing */ 7441 if (evaluate && error == ERROR_NONE) 7442 { 7443 rettv->v_type = VAR_NUMBER; /* default is number rettv */ 7444 error = ERROR_UNKNOWN; 7445 7446 if (!builtin_function(fname)) 7447 { 7448 /* 7449 * User defined function. 7450 */ 7451 fp = find_func(fname); 7452 7453 #ifdef FEAT_AUTOCMD 7454 /* Trigger FuncUndefined event, may load the function. */ 7455 if (fp == NULL 7456 && apply_autocmds(EVENT_FUNCUNDEFINED, 7457 fname, fname, TRUE, NULL) 7458 && !aborting()) 7459 { 7460 /* executed an autocommand, search for the function again */ 7461 fp = find_func(fname); 7462 } 7463 #endif 7464 /* Try loading a package. */ 7465 if (fp == NULL && script_autoload(fname, TRUE) && !aborting()) 7466 { 7467 /* loaded a package, search for the function again */ 7468 fp = find_func(fname); 7469 } 7470 7471 if (fp != NULL) 7472 { 7473 if (fp->uf_flags & FC_RANGE) 7474 *doesrange = TRUE; 7475 if (argcount < fp->uf_args.ga_len) 7476 error = ERROR_TOOFEW; 7477 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len) 7478 error = ERROR_TOOMANY; 7479 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL) 7480 error = ERROR_DICT; 7481 else 7482 { 7483 /* 7484 * Call the user function. 7485 * Save and restore search patterns, script variables and 7486 * redo buffer. 7487 */ 7488 save_search_patterns(); 7489 saveRedobuff(); 7490 ++fp->uf_calls; 7491 call_user_func(fp, argcount, argvars, rettv, 7492 firstline, lastline, 7493 (fp->uf_flags & FC_DICT) ? selfdict : NULL); 7494 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name) 7495 && fp->uf_refcount <= 0) 7496 /* Function was unreferenced while being used, free it 7497 * now. */ 7498 func_free(fp); 7499 restoreRedobuff(); 7500 restore_search_patterns(); 7501 error = ERROR_NONE; 7502 } 7503 } 7504 } 7505 else 7506 { 7507 /* 7508 * Find the function name in the table, call its implementation. 7509 */ 7510 i = find_internal_func(fname); 7511 if (i >= 0) 7512 { 7513 if (argcount < functions[i].f_min_argc) 7514 error = ERROR_TOOFEW; 7515 else if (argcount > functions[i].f_max_argc) 7516 error = ERROR_TOOMANY; 7517 else 7518 { 7519 argvars[argcount].v_type = VAR_UNKNOWN; 7520 functions[i].f_func(argvars, rettv); 7521 error = ERROR_NONE; 7522 } 7523 } 7524 } 7525 /* 7526 * The function call (or "FuncUndefined" autocommand sequence) might 7527 * have been aborted by an error, an interrupt, or an explicitly thrown 7528 * exception that has not been caught so far. This situation can be 7529 * tested for by calling aborting(). For an error in an internal 7530 * function or for the "E132" error in call_user_func(), however, the 7531 * throw point at which the "force_abort" flag (temporarily reset by 7532 * emsg()) is normally updated has not been reached yet. We need to 7533 * update that flag first to make aborting() reliable. 7534 */ 7535 update_force_abort(); 7536 } 7537 if (error == ERROR_NONE) 7538 ret = OK; 7539 7540 /* 7541 * Report an error unless the argument evaluation or function call has been 7542 * cancelled due to an aborting error, an interrupt, or an exception. 7543 */ 7544 if (!aborting()) 7545 { 7546 switch (error) 7547 { 7548 case ERROR_UNKNOWN: 7549 emsg_funcname("E117: Unknown function: %s", name); 7550 break; 7551 case ERROR_TOOMANY: 7552 emsg_funcname(e_toomanyarg, name); 7553 break; 7554 case ERROR_TOOFEW: 7555 emsg_funcname("E119: Not enough arguments for function: %s", 7556 name); 7557 break; 7558 case ERROR_SCRIPT: 7559 emsg_funcname("E120: Using <SID> not in a script context: %s", 7560 name); 7561 break; 7562 case ERROR_DICT: 7563 emsg_funcname("E725: Calling dict function without Dictionary: %s", 7564 name); 7565 break; 7566 } 7567 } 7568 7569 name[len] = cc; 7570 if (fname != name && fname != fname_buf) 7571 vim_free(fname); 7572 7573 return ret; 7574 } 7575 7576 /* 7577 * Give an error message with a function name. Handle <SNR> things. 7578 */ 7579 static void 7580 emsg_funcname(msg, name) 7581 char *msg; 7582 char_u *name; 7583 { 7584 char_u *p; 7585 7586 if (*name == K_SPECIAL) 7587 p = concat_str((char_u *)"<SNR>", name + 3); 7588 else 7589 p = name; 7590 EMSG2(_(msg), p); 7591 if (p != name) 7592 vim_free(p); 7593 } 7594 7595 /********************************************* 7596 * Implementation of the built-in functions 7597 */ 7598 7599 /* 7600 * "add(list, item)" function 7601 */ 7602 static void 7603 f_add(argvars, rettv) 7604 typval_T *argvars; 7605 typval_T *rettv; 7606 { 7607 list_T *l; 7608 7609 rettv->vval.v_number = 1; /* Default: Failed */ 7610 if (argvars[0].v_type == VAR_LIST) 7611 { 7612 if ((l = argvars[0].vval.v_list) != NULL 7613 && !tv_check_lock(l->lv_lock, (char_u *)"add()") 7614 && list_append_tv(l, &argvars[1]) == OK) 7615 copy_tv(&argvars[0], rettv); 7616 } 7617 else 7618 EMSG(_(e_listreq)); 7619 } 7620 7621 /* 7622 * "append(lnum, string/list)" function 7623 */ 7624 static void 7625 f_append(argvars, rettv) 7626 typval_T *argvars; 7627 typval_T *rettv; 7628 { 7629 long lnum; 7630 char_u *line; 7631 list_T *l = NULL; 7632 listitem_T *li = NULL; 7633 typval_T *tv; 7634 long added = 0; 7635 7636 lnum = get_tv_lnum(argvars); 7637 if (lnum >= 0 7638 && lnum <= curbuf->b_ml.ml_line_count 7639 && u_save(lnum, lnum + 1) == OK) 7640 { 7641 if (argvars[1].v_type == VAR_LIST) 7642 { 7643 l = argvars[1].vval.v_list; 7644 if (l == NULL) 7645 return; 7646 li = l->lv_first; 7647 } 7648 rettv->vval.v_number = 0; /* Default: Success */ 7649 for (;;) 7650 { 7651 if (l == NULL) 7652 tv = &argvars[1]; /* append a string */ 7653 else if (li == NULL) 7654 break; /* end of list */ 7655 else 7656 tv = &li->li_tv; /* append item from list */ 7657 line = get_tv_string_chk(tv); 7658 if (line == NULL) /* type error */ 7659 { 7660 rettv->vval.v_number = 1; /* Failed */ 7661 break; 7662 } 7663 ml_append(lnum + added, line, (colnr_T)0, FALSE); 7664 ++added; 7665 if (l == NULL) 7666 break; 7667 li = li->li_next; 7668 } 7669 7670 appended_lines_mark(lnum, added); 7671 if (curwin->w_cursor.lnum > lnum) 7672 curwin->w_cursor.lnum += added; 7673 } 7674 else 7675 rettv->vval.v_number = 1; /* Failed */ 7676 } 7677 7678 /* 7679 * "argc()" function 7680 */ 7681 /* ARGSUSED */ 7682 static void 7683 f_argc(argvars, rettv) 7684 typval_T *argvars; 7685 typval_T *rettv; 7686 { 7687 rettv->vval.v_number = ARGCOUNT; 7688 } 7689 7690 /* 7691 * "argidx()" function 7692 */ 7693 /* ARGSUSED */ 7694 static void 7695 f_argidx(argvars, rettv) 7696 typval_T *argvars; 7697 typval_T *rettv; 7698 { 7699 rettv->vval.v_number = curwin->w_arg_idx; 7700 } 7701 7702 /* 7703 * "argv(nr)" function 7704 */ 7705 static void 7706 f_argv(argvars, rettv) 7707 typval_T *argvars; 7708 typval_T *rettv; 7709 { 7710 int idx; 7711 7712 if (argvars[0].v_type != VAR_UNKNOWN) 7713 { 7714 idx = get_tv_number_chk(&argvars[0], NULL); 7715 if (idx >= 0 && idx < ARGCOUNT) 7716 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx])); 7717 else 7718 rettv->vval.v_string = NULL; 7719 rettv->v_type = VAR_STRING; 7720 } 7721 else if (rettv_list_alloc(rettv) == OK) 7722 for (idx = 0; idx < ARGCOUNT; ++idx) 7723 list_append_string(rettv->vval.v_list, 7724 alist_name(&ARGLIST[idx]), -1); 7725 } 7726 7727 /* 7728 * "browse(save, title, initdir, default)" function 7729 */ 7730 /* ARGSUSED */ 7731 static void 7732 f_browse(argvars, rettv) 7733 typval_T *argvars; 7734 typval_T *rettv; 7735 { 7736 #ifdef FEAT_BROWSE 7737 int save; 7738 char_u *title; 7739 char_u *initdir; 7740 char_u *defname; 7741 char_u buf[NUMBUFLEN]; 7742 char_u buf2[NUMBUFLEN]; 7743 int error = FALSE; 7744 7745 save = get_tv_number_chk(&argvars[0], &error); 7746 title = get_tv_string_chk(&argvars[1]); 7747 initdir = get_tv_string_buf_chk(&argvars[2], buf); 7748 defname = get_tv_string_buf_chk(&argvars[3], buf2); 7749 7750 if (error || title == NULL || initdir == NULL || defname == NULL) 7751 rettv->vval.v_string = NULL; 7752 else 7753 rettv->vval.v_string = 7754 do_browse(save ? BROWSE_SAVE : 0, 7755 title, defname, NULL, initdir, NULL, curbuf); 7756 #else 7757 rettv->vval.v_string = NULL; 7758 #endif 7759 rettv->v_type = VAR_STRING; 7760 } 7761 7762 /* 7763 * "browsedir(title, initdir)" function 7764 */ 7765 /* ARGSUSED */ 7766 static void 7767 f_browsedir(argvars, rettv) 7768 typval_T *argvars; 7769 typval_T *rettv; 7770 { 7771 #ifdef FEAT_BROWSE 7772 char_u *title; 7773 char_u *initdir; 7774 char_u buf[NUMBUFLEN]; 7775 7776 title = get_tv_string_chk(&argvars[0]); 7777 initdir = get_tv_string_buf_chk(&argvars[1], buf); 7778 7779 if (title == NULL || initdir == NULL) 7780 rettv->vval.v_string = NULL; 7781 else 7782 rettv->vval.v_string = do_browse(BROWSE_DIR, 7783 title, NULL, NULL, initdir, NULL, curbuf); 7784 #else 7785 rettv->vval.v_string = NULL; 7786 #endif 7787 rettv->v_type = VAR_STRING; 7788 } 7789 7790 static buf_T *find_buffer __ARGS((typval_T *avar)); 7791 7792 /* 7793 * Find a buffer by number or exact name. 7794 */ 7795 static buf_T * 7796 find_buffer(avar) 7797 typval_T *avar; 7798 { 7799 buf_T *buf = NULL; 7800 7801 if (avar->v_type == VAR_NUMBER) 7802 buf = buflist_findnr((int)avar->vval.v_number); 7803 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL) 7804 { 7805 buf = buflist_findname_exp(avar->vval.v_string); 7806 if (buf == NULL) 7807 { 7808 /* No full path name match, try a match with a URL or a "nofile" 7809 * buffer, these don't use the full path. */ 7810 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 7811 if (buf->b_fname != NULL 7812 && (path_with_url(buf->b_fname) 7813 #ifdef FEAT_QUICKFIX 7814 || bt_nofile(buf) 7815 #endif 7816 ) 7817 && STRCMP(buf->b_fname, avar->vval.v_string) == 0) 7818 break; 7819 } 7820 } 7821 return buf; 7822 } 7823 7824 /* 7825 * "bufexists(expr)" function 7826 */ 7827 static void 7828 f_bufexists(argvars, rettv) 7829 typval_T *argvars; 7830 typval_T *rettv; 7831 { 7832 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL); 7833 } 7834 7835 /* 7836 * "buflisted(expr)" function 7837 */ 7838 static void 7839 f_buflisted(argvars, rettv) 7840 typval_T *argvars; 7841 typval_T *rettv; 7842 { 7843 buf_T *buf; 7844 7845 buf = find_buffer(&argvars[0]); 7846 rettv->vval.v_number = (buf != NULL && buf->b_p_bl); 7847 } 7848 7849 /* 7850 * "bufloaded(expr)" function 7851 */ 7852 static void 7853 f_bufloaded(argvars, rettv) 7854 typval_T *argvars; 7855 typval_T *rettv; 7856 { 7857 buf_T *buf; 7858 7859 buf = find_buffer(&argvars[0]); 7860 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL); 7861 } 7862 7863 static buf_T *get_buf_tv __ARGS((typval_T *tv)); 7864 7865 /* 7866 * Get buffer by number or pattern. 7867 */ 7868 static buf_T * 7869 get_buf_tv(tv) 7870 typval_T *tv; 7871 { 7872 char_u *name = tv->vval.v_string; 7873 int save_magic; 7874 char_u *save_cpo; 7875 buf_T *buf; 7876 7877 if (tv->v_type == VAR_NUMBER) 7878 return buflist_findnr((int)tv->vval.v_number); 7879 if (tv->v_type != VAR_STRING) 7880 return NULL; 7881 if (name == NULL || *name == NUL) 7882 return curbuf; 7883 if (name[0] == '$' && name[1] == NUL) 7884 return lastbuf; 7885 7886 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */ 7887 save_magic = p_magic; 7888 p_magic = TRUE; 7889 save_cpo = p_cpo; 7890 p_cpo = (char_u *)""; 7891 7892 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name), 7893 TRUE, FALSE)); 7894 7895 p_magic = save_magic; 7896 p_cpo = save_cpo; 7897 7898 /* If not found, try expanding the name, like done for bufexists(). */ 7899 if (buf == NULL) 7900 buf = find_buffer(tv); 7901 7902 return buf; 7903 } 7904 7905 /* 7906 * "bufname(expr)" function 7907 */ 7908 static void 7909 f_bufname(argvars, rettv) 7910 typval_T *argvars; 7911 typval_T *rettv; 7912 { 7913 buf_T *buf; 7914 7915 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 7916 ++emsg_off; 7917 buf = get_buf_tv(&argvars[0]); 7918 rettv->v_type = VAR_STRING; 7919 if (buf != NULL && buf->b_fname != NULL) 7920 rettv->vval.v_string = vim_strsave(buf->b_fname); 7921 else 7922 rettv->vval.v_string = NULL; 7923 --emsg_off; 7924 } 7925 7926 /* 7927 * "bufnr(expr)" function 7928 */ 7929 static void 7930 f_bufnr(argvars, rettv) 7931 typval_T *argvars; 7932 typval_T *rettv; 7933 { 7934 buf_T *buf; 7935 int error = FALSE; 7936 char_u *name; 7937 7938 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 7939 ++emsg_off; 7940 buf = get_buf_tv(&argvars[0]); 7941 --emsg_off; 7942 7943 /* If the buffer isn't found and the second argument is not zero create a 7944 * new buffer. */ 7945 if (buf == NULL 7946 && argvars[1].v_type != VAR_UNKNOWN 7947 && get_tv_number_chk(&argvars[1], &error) != 0 7948 && !error 7949 && (name = get_tv_string_chk(&argvars[0])) != NULL 7950 && !error) 7951 buf = buflist_new(name, NULL, (linenr_T)1, 0); 7952 7953 if (buf != NULL) 7954 rettv->vval.v_number = buf->b_fnum; 7955 else 7956 rettv->vval.v_number = -1; 7957 } 7958 7959 /* 7960 * "bufwinnr(nr)" function 7961 */ 7962 static void 7963 f_bufwinnr(argvars, rettv) 7964 typval_T *argvars; 7965 typval_T *rettv; 7966 { 7967 #ifdef FEAT_WINDOWS 7968 win_T *wp; 7969 int winnr = 0; 7970 #endif 7971 buf_T *buf; 7972 7973 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 7974 ++emsg_off; 7975 buf = get_buf_tv(&argvars[0]); 7976 #ifdef FEAT_WINDOWS 7977 for (wp = firstwin; wp; wp = wp->w_next) 7978 { 7979 ++winnr; 7980 if (wp->w_buffer == buf) 7981 break; 7982 } 7983 rettv->vval.v_number = (wp != NULL ? winnr : -1); 7984 #else 7985 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1); 7986 #endif 7987 --emsg_off; 7988 } 7989 7990 /* 7991 * "byte2line(byte)" function 7992 */ 7993 /*ARGSUSED*/ 7994 static void 7995 f_byte2line(argvars, rettv) 7996 typval_T *argvars; 7997 typval_T *rettv; 7998 { 7999 #ifndef FEAT_BYTEOFF 8000 rettv->vval.v_number = -1; 8001 #else 8002 long boff = 0; 8003 8004 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */ 8005 if (boff < 0) 8006 rettv->vval.v_number = -1; 8007 else 8008 rettv->vval.v_number = ml_find_line_or_offset(curbuf, 8009 (linenr_T)0, &boff); 8010 #endif 8011 } 8012 8013 /* 8014 * "byteidx()" function 8015 */ 8016 /*ARGSUSED*/ 8017 static void 8018 f_byteidx(argvars, rettv) 8019 typval_T *argvars; 8020 typval_T *rettv; 8021 { 8022 #ifdef FEAT_MBYTE 8023 char_u *t; 8024 #endif 8025 char_u *str; 8026 long idx; 8027 8028 str = get_tv_string_chk(&argvars[0]); 8029 idx = get_tv_number_chk(&argvars[1], NULL); 8030 rettv->vval.v_number = -1; 8031 if (str == NULL || idx < 0) 8032 return; 8033 8034 #ifdef FEAT_MBYTE 8035 t = str; 8036 for ( ; idx > 0; idx--) 8037 { 8038 if (*t == NUL) /* EOL reached */ 8039 return; 8040 t += (*mb_ptr2len)(t); 8041 } 8042 rettv->vval.v_number = t - str; 8043 #else 8044 if (idx <= STRLEN(str)) 8045 rettv->vval.v_number = idx; 8046 #endif 8047 } 8048 8049 /* 8050 * "call(func, arglist)" function 8051 */ 8052 static void 8053 f_call(argvars, rettv) 8054 typval_T *argvars; 8055 typval_T *rettv; 8056 { 8057 char_u *func; 8058 typval_T argv[MAX_FUNC_ARGS]; 8059 int argc = 0; 8060 listitem_T *item; 8061 int dummy; 8062 dict_T *selfdict = NULL; 8063 8064 rettv->vval.v_number = 0; 8065 if (argvars[1].v_type != VAR_LIST) 8066 { 8067 EMSG(_(e_listreq)); 8068 return; 8069 } 8070 if (argvars[1].vval.v_list == NULL) 8071 return; 8072 8073 if (argvars[0].v_type == VAR_FUNC) 8074 func = argvars[0].vval.v_string; 8075 else 8076 func = get_tv_string(&argvars[0]); 8077 if (*func == NUL) 8078 return; /* type error or empty name */ 8079 8080 if (argvars[2].v_type != VAR_UNKNOWN) 8081 { 8082 if (argvars[2].v_type != VAR_DICT) 8083 { 8084 EMSG(_(e_dictreq)); 8085 return; 8086 } 8087 selfdict = argvars[2].vval.v_dict; 8088 } 8089 8090 for (item = argvars[1].vval.v_list->lv_first; item != NULL; 8091 item = item->li_next) 8092 { 8093 if (argc == MAX_FUNC_ARGS) 8094 { 8095 EMSG(_("E699: Too many arguments")); 8096 break; 8097 } 8098 /* Make a copy of each argument. This is needed to be able to set 8099 * v_lock to VAR_FIXED in the copy without changing the original list. 8100 */ 8101 copy_tv(&item->li_tv, &argv[argc++]); 8102 } 8103 8104 if (item == NULL) 8105 (void)call_func(func, STRLEN(func), rettv, argc, argv, 8106 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 8107 &dummy, TRUE, selfdict); 8108 8109 /* Free the arguments. */ 8110 while (argc > 0) 8111 clear_tv(&argv[--argc]); 8112 } 8113 8114 /* 8115 * "changenr()" function 8116 */ 8117 /*ARGSUSED*/ 8118 static void 8119 f_changenr(argvars, rettv) 8120 typval_T *argvars; 8121 typval_T *rettv; 8122 { 8123 rettv->vval.v_number = curbuf->b_u_seq_cur; 8124 } 8125 8126 /* 8127 * "char2nr(string)" function 8128 */ 8129 static void 8130 f_char2nr(argvars, rettv) 8131 typval_T *argvars; 8132 typval_T *rettv; 8133 { 8134 #ifdef FEAT_MBYTE 8135 if (has_mbyte) 8136 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0])); 8137 else 8138 #endif 8139 rettv->vval.v_number = get_tv_string(&argvars[0])[0]; 8140 } 8141 8142 /* 8143 * "cindent(lnum)" function 8144 */ 8145 static void 8146 f_cindent(argvars, rettv) 8147 typval_T *argvars; 8148 typval_T *rettv; 8149 { 8150 #ifdef FEAT_CINDENT 8151 pos_T pos; 8152 linenr_T lnum; 8153 8154 pos = curwin->w_cursor; 8155 lnum = get_tv_lnum(argvars); 8156 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 8157 { 8158 curwin->w_cursor.lnum = lnum; 8159 rettv->vval.v_number = get_c_indent(); 8160 curwin->w_cursor = pos; 8161 } 8162 else 8163 #endif 8164 rettv->vval.v_number = -1; 8165 } 8166 8167 /* 8168 * "col(string)" function 8169 */ 8170 static void 8171 f_col(argvars, rettv) 8172 typval_T *argvars; 8173 typval_T *rettv; 8174 { 8175 colnr_T col = 0; 8176 pos_T *fp; 8177 int fnum = curbuf->b_fnum; 8178 8179 fp = var2fpos(&argvars[0], FALSE, &fnum); 8180 if (fp != NULL && fnum == curbuf->b_fnum) 8181 { 8182 if (fp->col == MAXCOL) 8183 { 8184 /* '> can be MAXCOL, get the length of the line then */ 8185 if (fp->lnum <= curbuf->b_ml.ml_line_count) 8186 col = STRLEN(ml_get(fp->lnum)) + 1; 8187 else 8188 col = MAXCOL; 8189 } 8190 else 8191 { 8192 col = fp->col + 1; 8193 #ifdef FEAT_VIRTUALEDIT 8194 /* col(".") when the cursor is on the NUL at the end of the line 8195 * because of "coladd" can be seen as an extra column. */ 8196 if (virtual_active() && fp == &curwin->w_cursor) 8197 { 8198 char_u *p = ml_get_cursor(); 8199 8200 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p, 8201 curwin->w_virtcol - curwin->w_cursor.coladd)) 8202 { 8203 # ifdef FEAT_MBYTE 8204 int l; 8205 8206 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL) 8207 col += l; 8208 # else 8209 if (*p != NUL && p[1] == NUL) 8210 ++col; 8211 # endif 8212 } 8213 } 8214 #endif 8215 } 8216 } 8217 rettv->vval.v_number = col; 8218 } 8219 8220 #if defined(FEAT_INS_EXPAND) 8221 /* 8222 * "complete()" function 8223 */ 8224 /*ARGSUSED*/ 8225 static void 8226 f_complete(argvars, rettv) 8227 typval_T *argvars; 8228 typval_T *rettv; 8229 { 8230 int startcol; 8231 8232 if ((State & INSERT) == 0) 8233 { 8234 EMSG(_("E785: complete() can only be used in Insert mode")); 8235 return; 8236 } 8237 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL) 8238 { 8239 EMSG(_(e_invarg)); 8240 return; 8241 } 8242 8243 startcol = get_tv_number_chk(&argvars[0], NULL); 8244 if (startcol <= 0) 8245 return; 8246 8247 set_completion(startcol - 1, argvars[1].vval.v_list); 8248 } 8249 8250 /* 8251 * "complete_add()" function 8252 */ 8253 /*ARGSUSED*/ 8254 static void 8255 f_complete_add(argvars, rettv) 8256 typval_T *argvars; 8257 typval_T *rettv; 8258 { 8259 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0); 8260 } 8261 8262 /* 8263 * "complete_check()" function 8264 */ 8265 /*ARGSUSED*/ 8266 static void 8267 f_complete_check(argvars, rettv) 8268 typval_T *argvars; 8269 typval_T *rettv; 8270 { 8271 int saved = RedrawingDisabled; 8272 8273 RedrawingDisabled = 0; 8274 ins_compl_check_keys(0); 8275 rettv->vval.v_number = compl_interrupted; 8276 RedrawingDisabled = saved; 8277 } 8278 #endif 8279 8280 /* 8281 * "confirm(message, buttons[, default [, type]])" function 8282 */ 8283 /*ARGSUSED*/ 8284 static void 8285 f_confirm(argvars, rettv) 8286 typval_T *argvars; 8287 typval_T *rettv; 8288 { 8289 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 8290 char_u *message; 8291 char_u *buttons = NULL; 8292 char_u buf[NUMBUFLEN]; 8293 char_u buf2[NUMBUFLEN]; 8294 int def = 1; 8295 int type = VIM_GENERIC; 8296 char_u *typestr; 8297 int error = FALSE; 8298 8299 message = get_tv_string_chk(&argvars[0]); 8300 if (message == NULL) 8301 error = TRUE; 8302 if (argvars[1].v_type != VAR_UNKNOWN) 8303 { 8304 buttons = get_tv_string_buf_chk(&argvars[1], buf); 8305 if (buttons == NULL) 8306 error = TRUE; 8307 if (argvars[2].v_type != VAR_UNKNOWN) 8308 { 8309 def = get_tv_number_chk(&argvars[2], &error); 8310 if (argvars[3].v_type != VAR_UNKNOWN) 8311 { 8312 typestr = get_tv_string_buf_chk(&argvars[3], buf2); 8313 if (typestr == NULL) 8314 error = TRUE; 8315 else 8316 { 8317 switch (TOUPPER_ASC(*typestr)) 8318 { 8319 case 'E': type = VIM_ERROR; break; 8320 case 'Q': type = VIM_QUESTION; break; 8321 case 'I': type = VIM_INFO; break; 8322 case 'W': type = VIM_WARNING; break; 8323 case 'G': type = VIM_GENERIC; break; 8324 } 8325 } 8326 } 8327 } 8328 } 8329 8330 if (buttons == NULL || *buttons == NUL) 8331 buttons = (char_u *)_("&Ok"); 8332 8333 if (error) 8334 rettv->vval.v_number = 0; 8335 else 8336 rettv->vval.v_number = do_dialog(type, NULL, message, buttons, 8337 def, NULL); 8338 #else 8339 rettv->vval.v_number = 0; 8340 #endif 8341 } 8342 8343 /* 8344 * "copy()" function 8345 */ 8346 static void 8347 f_copy(argvars, rettv) 8348 typval_T *argvars; 8349 typval_T *rettv; 8350 { 8351 item_copy(&argvars[0], rettv, FALSE, 0); 8352 } 8353 8354 /* 8355 * "count()" function 8356 */ 8357 static void 8358 f_count(argvars, rettv) 8359 typval_T *argvars; 8360 typval_T *rettv; 8361 { 8362 long n = 0; 8363 int ic = FALSE; 8364 8365 if (argvars[0].v_type == VAR_LIST) 8366 { 8367 listitem_T *li; 8368 list_T *l; 8369 long idx; 8370 8371 if ((l = argvars[0].vval.v_list) != NULL) 8372 { 8373 li = l->lv_first; 8374 if (argvars[2].v_type != VAR_UNKNOWN) 8375 { 8376 int error = FALSE; 8377 8378 ic = get_tv_number_chk(&argvars[2], &error); 8379 if (argvars[3].v_type != VAR_UNKNOWN) 8380 { 8381 idx = get_tv_number_chk(&argvars[3], &error); 8382 if (!error) 8383 { 8384 li = list_find(l, idx); 8385 if (li == NULL) 8386 EMSGN(_(e_listidx), idx); 8387 } 8388 } 8389 if (error) 8390 li = NULL; 8391 } 8392 8393 for ( ; li != NULL; li = li->li_next) 8394 if (tv_equal(&li->li_tv, &argvars[1], ic)) 8395 ++n; 8396 } 8397 } 8398 else if (argvars[0].v_type == VAR_DICT) 8399 { 8400 int todo; 8401 dict_T *d; 8402 hashitem_T *hi; 8403 8404 if ((d = argvars[0].vval.v_dict) != NULL) 8405 { 8406 int error = FALSE; 8407 8408 if (argvars[2].v_type != VAR_UNKNOWN) 8409 { 8410 ic = get_tv_number_chk(&argvars[2], &error); 8411 if (argvars[3].v_type != VAR_UNKNOWN) 8412 EMSG(_(e_invarg)); 8413 } 8414 8415 todo = error ? 0 : d->dv_hashtab.ht_used; 8416 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 8417 { 8418 if (!HASHITEM_EMPTY(hi)) 8419 { 8420 --todo; 8421 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic)) 8422 ++n; 8423 } 8424 } 8425 } 8426 } 8427 else 8428 EMSG2(_(e_listdictarg), "count()"); 8429 rettv->vval.v_number = n; 8430 } 8431 8432 /* 8433 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function 8434 * 8435 * Checks the existence of a cscope connection. 8436 */ 8437 /*ARGSUSED*/ 8438 static void 8439 f_cscope_connection(argvars, rettv) 8440 typval_T *argvars; 8441 typval_T *rettv; 8442 { 8443 #ifdef FEAT_CSCOPE 8444 int num = 0; 8445 char_u *dbpath = NULL; 8446 char_u *prepend = NULL; 8447 char_u buf[NUMBUFLEN]; 8448 8449 if (argvars[0].v_type != VAR_UNKNOWN 8450 && argvars[1].v_type != VAR_UNKNOWN) 8451 { 8452 num = (int)get_tv_number(&argvars[0]); 8453 dbpath = get_tv_string(&argvars[1]); 8454 if (argvars[2].v_type != VAR_UNKNOWN) 8455 prepend = get_tv_string_buf(&argvars[2], buf); 8456 } 8457 8458 rettv->vval.v_number = cs_connection(num, dbpath, prepend); 8459 #else 8460 rettv->vval.v_number = 0; 8461 #endif 8462 } 8463 8464 /* 8465 * "cursor(lnum, col)" function 8466 * 8467 * Moves the cursor to the specified line and column 8468 */ 8469 /*ARGSUSED*/ 8470 static void 8471 f_cursor(argvars, rettv) 8472 typval_T *argvars; 8473 typval_T *rettv; 8474 { 8475 long line, col; 8476 #ifdef FEAT_VIRTUALEDIT 8477 long coladd = 0; 8478 #endif 8479 8480 if (argvars[1].v_type == VAR_UNKNOWN) 8481 { 8482 pos_T pos; 8483 8484 if (list2fpos(argvars, &pos, NULL) == FAIL) 8485 return; 8486 line = pos.lnum; 8487 col = pos.col; 8488 #ifdef FEAT_VIRTUALEDIT 8489 coladd = pos.coladd; 8490 #endif 8491 } 8492 else 8493 { 8494 line = get_tv_lnum(argvars); 8495 col = get_tv_number_chk(&argvars[1], NULL); 8496 #ifdef FEAT_VIRTUALEDIT 8497 if (argvars[2].v_type != VAR_UNKNOWN) 8498 coladd = get_tv_number_chk(&argvars[2], NULL); 8499 #endif 8500 } 8501 if (line < 0 || col < 0 8502 #ifdef FEAT_VIRTUALEDIT 8503 || coladd < 0 8504 #endif 8505 ) 8506 return; /* type error; errmsg already given */ 8507 if (line > 0) 8508 curwin->w_cursor.lnum = line; 8509 if (col > 0) 8510 curwin->w_cursor.col = col - 1; 8511 #ifdef FEAT_VIRTUALEDIT 8512 curwin->w_cursor.coladd = coladd; 8513 #endif 8514 8515 /* Make sure the cursor is in a valid position. */ 8516 check_cursor(); 8517 #ifdef FEAT_MBYTE 8518 /* Correct cursor for multi-byte character. */ 8519 if (has_mbyte) 8520 mb_adjust_cursor(); 8521 #endif 8522 8523 curwin->w_set_curswant = TRUE; 8524 } 8525 8526 /* 8527 * "deepcopy()" function 8528 */ 8529 static void 8530 f_deepcopy(argvars, rettv) 8531 typval_T *argvars; 8532 typval_T *rettv; 8533 { 8534 int noref = 0; 8535 8536 if (argvars[1].v_type != VAR_UNKNOWN) 8537 noref = get_tv_number_chk(&argvars[1], NULL); 8538 if (noref < 0 || noref > 1) 8539 EMSG(_(e_invarg)); 8540 else 8541 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0); 8542 } 8543 8544 /* 8545 * "delete()" function 8546 */ 8547 static void 8548 f_delete(argvars, rettv) 8549 typval_T *argvars; 8550 typval_T *rettv; 8551 { 8552 if (check_restricted() || check_secure()) 8553 rettv->vval.v_number = -1; 8554 else 8555 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0])); 8556 } 8557 8558 /* 8559 * "did_filetype()" function 8560 */ 8561 /*ARGSUSED*/ 8562 static void 8563 f_did_filetype(argvars, rettv) 8564 typval_T *argvars; 8565 typval_T *rettv; 8566 { 8567 #ifdef FEAT_AUTOCMD 8568 rettv->vval.v_number = did_filetype; 8569 #else 8570 rettv->vval.v_number = 0; 8571 #endif 8572 } 8573 8574 /* 8575 * "diff_filler()" function 8576 */ 8577 /*ARGSUSED*/ 8578 static void 8579 f_diff_filler(argvars, rettv) 8580 typval_T *argvars; 8581 typval_T *rettv; 8582 { 8583 #ifdef FEAT_DIFF 8584 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars)); 8585 #endif 8586 } 8587 8588 /* 8589 * "diff_hlID()" function 8590 */ 8591 /*ARGSUSED*/ 8592 static void 8593 f_diff_hlID(argvars, rettv) 8594 typval_T *argvars; 8595 typval_T *rettv; 8596 { 8597 #ifdef FEAT_DIFF 8598 linenr_T lnum = get_tv_lnum(argvars); 8599 static linenr_T prev_lnum = 0; 8600 static int changedtick = 0; 8601 static int fnum = 0; 8602 static int change_start = 0; 8603 static int change_end = 0; 8604 static hlf_T hlID = 0; 8605 int filler_lines; 8606 int col; 8607 8608 if (lnum < 0) /* ignore type error in {lnum} arg */ 8609 lnum = 0; 8610 if (lnum != prev_lnum 8611 || changedtick != curbuf->b_changedtick 8612 || fnum != curbuf->b_fnum) 8613 { 8614 /* New line, buffer, change: need to get the values. */ 8615 filler_lines = diff_check(curwin, lnum); 8616 if (filler_lines < 0) 8617 { 8618 if (filler_lines == -1) 8619 { 8620 change_start = MAXCOL; 8621 change_end = -1; 8622 if (diff_find_change(curwin, lnum, &change_start, &change_end)) 8623 hlID = HLF_ADD; /* added line */ 8624 else 8625 hlID = HLF_CHD; /* changed line */ 8626 } 8627 else 8628 hlID = HLF_ADD; /* added line */ 8629 } 8630 else 8631 hlID = (hlf_T)0; 8632 prev_lnum = lnum; 8633 changedtick = curbuf->b_changedtick; 8634 fnum = curbuf->b_fnum; 8635 } 8636 8637 if (hlID == HLF_CHD || hlID == HLF_TXD) 8638 { 8639 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */ 8640 if (col >= change_start && col <= change_end) 8641 hlID = HLF_TXD; /* changed text */ 8642 else 8643 hlID = HLF_CHD; /* changed line */ 8644 } 8645 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID; 8646 #endif 8647 } 8648 8649 /* 8650 * "empty({expr})" function 8651 */ 8652 static void 8653 f_empty(argvars, rettv) 8654 typval_T *argvars; 8655 typval_T *rettv; 8656 { 8657 int n; 8658 8659 switch (argvars[0].v_type) 8660 { 8661 case VAR_STRING: 8662 case VAR_FUNC: 8663 n = argvars[0].vval.v_string == NULL 8664 || *argvars[0].vval.v_string == NUL; 8665 break; 8666 case VAR_NUMBER: 8667 n = argvars[0].vval.v_number == 0; 8668 break; 8669 case VAR_LIST: 8670 n = argvars[0].vval.v_list == NULL 8671 || argvars[0].vval.v_list->lv_first == NULL; 8672 break; 8673 case VAR_DICT: 8674 n = argvars[0].vval.v_dict == NULL 8675 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0; 8676 break; 8677 default: 8678 EMSG2(_(e_intern2), "f_empty()"); 8679 n = 0; 8680 } 8681 8682 rettv->vval.v_number = n; 8683 } 8684 8685 /* 8686 * "escape({string}, {chars})" function 8687 */ 8688 static void 8689 f_escape(argvars, rettv) 8690 typval_T *argvars; 8691 typval_T *rettv; 8692 { 8693 char_u buf[NUMBUFLEN]; 8694 8695 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]), 8696 get_tv_string_buf(&argvars[1], buf)); 8697 rettv->v_type = VAR_STRING; 8698 } 8699 8700 /* 8701 * "eval()" function 8702 */ 8703 /*ARGSUSED*/ 8704 static void 8705 f_eval(argvars, rettv) 8706 typval_T *argvars; 8707 typval_T *rettv; 8708 { 8709 char_u *s; 8710 8711 s = get_tv_string_chk(&argvars[0]); 8712 if (s != NULL) 8713 s = skipwhite(s); 8714 8715 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL) 8716 { 8717 rettv->v_type = VAR_NUMBER; 8718 rettv->vval.v_number = 0; 8719 } 8720 else if (*s != NUL) 8721 EMSG(_(e_trailing)); 8722 } 8723 8724 /* 8725 * "eventhandler()" function 8726 */ 8727 /*ARGSUSED*/ 8728 static void 8729 f_eventhandler(argvars, rettv) 8730 typval_T *argvars; 8731 typval_T *rettv; 8732 { 8733 rettv->vval.v_number = vgetc_busy; 8734 } 8735 8736 /* 8737 * "executable()" function 8738 */ 8739 static void 8740 f_executable(argvars, rettv) 8741 typval_T *argvars; 8742 typval_T *rettv; 8743 { 8744 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0])); 8745 } 8746 8747 /* 8748 * "exists()" function 8749 */ 8750 static void 8751 f_exists(argvars, rettv) 8752 typval_T *argvars; 8753 typval_T *rettv; 8754 { 8755 char_u *p; 8756 char_u *name; 8757 int n = FALSE; 8758 int len = 0; 8759 8760 p = get_tv_string(&argvars[0]); 8761 if (*p == '$') /* environment variable */ 8762 { 8763 /* first try "normal" environment variables (fast) */ 8764 if (mch_getenv(p + 1) != NULL) 8765 n = TRUE; 8766 else 8767 { 8768 /* try expanding things like $VIM and ${HOME} */ 8769 p = expand_env_save(p); 8770 if (p != NULL && *p != '$') 8771 n = TRUE; 8772 vim_free(p); 8773 } 8774 } 8775 else if (*p == '&' || *p == '+') /* option */ 8776 n = (get_option_tv(&p, NULL, TRUE) == OK); 8777 else if (*p == '*') /* internal or user defined function */ 8778 { 8779 n = function_exists(p + 1); 8780 } 8781 else if (*p == ':') 8782 { 8783 n = cmd_exists(p + 1); 8784 } 8785 else if (*p == '#') 8786 { 8787 #ifdef FEAT_AUTOCMD 8788 if (p[1] == '#') 8789 n = autocmd_supported(p + 2); 8790 else 8791 n = au_exists(p + 1); 8792 #endif 8793 } 8794 else /* internal variable */ 8795 { 8796 char_u *tofree; 8797 typval_T tv; 8798 8799 /* get_name_len() takes care of expanding curly braces */ 8800 name = p; 8801 len = get_name_len(&p, &tofree, TRUE, FALSE); 8802 if (len > 0) 8803 { 8804 if (tofree != NULL) 8805 name = tofree; 8806 n = (get_var_tv(name, len, &tv, FALSE) == OK); 8807 if (n) 8808 { 8809 /* handle d.key, l[idx], f(expr) */ 8810 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK); 8811 if (n) 8812 clear_tv(&tv); 8813 } 8814 } 8815 8816 vim_free(tofree); 8817 } 8818 8819 rettv->vval.v_number = n; 8820 } 8821 8822 /* 8823 * "expand()" function 8824 */ 8825 static void 8826 f_expand(argvars, rettv) 8827 typval_T *argvars; 8828 typval_T *rettv; 8829 { 8830 char_u *s; 8831 int len; 8832 char_u *errormsg; 8833 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND; 8834 expand_T xpc; 8835 int error = FALSE; 8836 8837 rettv->v_type = VAR_STRING; 8838 s = get_tv_string(&argvars[0]); 8839 if (*s == '%' || *s == '#' || *s == '<') 8840 { 8841 ++emsg_off; 8842 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s); 8843 --emsg_off; 8844 } 8845 else 8846 { 8847 /* When the optional second argument is non-zero, don't remove matches 8848 * for 'suffixes' and 'wildignore' */ 8849 if (argvars[1].v_type != VAR_UNKNOWN 8850 && get_tv_number_chk(&argvars[1], &error)) 8851 flags |= WILD_KEEP_ALL; 8852 if (!error) 8853 { 8854 ExpandInit(&xpc); 8855 xpc.xp_context = EXPAND_FILES; 8856 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL); 8857 ExpandCleanup(&xpc); 8858 } 8859 else 8860 rettv->vval.v_string = NULL; 8861 } 8862 } 8863 8864 /* 8865 * "extend(list, list [, idx])" function 8866 * "extend(dict, dict [, action])" function 8867 */ 8868 static void 8869 f_extend(argvars, rettv) 8870 typval_T *argvars; 8871 typval_T *rettv; 8872 { 8873 rettv->vval.v_number = 0; 8874 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST) 8875 { 8876 list_T *l1, *l2; 8877 listitem_T *item; 8878 long before; 8879 int error = FALSE; 8880 8881 l1 = argvars[0].vval.v_list; 8882 l2 = argvars[1].vval.v_list; 8883 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()") 8884 && l2 != NULL) 8885 { 8886 if (argvars[2].v_type != VAR_UNKNOWN) 8887 { 8888 before = get_tv_number_chk(&argvars[2], &error); 8889 if (error) 8890 return; /* type error; errmsg already given */ 8891 8892 if (before == l1->lv_len) 8893 item = NULL; 8894 else 8895 { 8896 item = list_find(l1, before); 8897 if (item == NULL) 8898 { 8899 EMSGN(_(e_listidx), before); 8900 return; 8901 } 8902 } 8903 } 8904 else 8905 item = NULL; 8906 list_extend(l1, l2, item); 8907 8908 copy_tv(&argvars[0], rettv); 8909 } 8910 } 8911 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT) 8912 { 8913 dict_T *d1, *d2; 8914 dictitem_T *di1; 8915 char_u *action; 8916 int i; 8917 hashitem_T *hi2; 8918 int todo; 8919 8920 d1 = argvars[0].vval.v_dict; 8921 d2 = argvars[1].vval.v_dict; 8922 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()") 8923 && d2 != NULL) 8924 { 8925 /* Check the third argument. */ 8926 if (argvars[2].v_type != VAR_UNKNOWN) 8927 { 8928 static char *(av[]) = {"keep", "force", "error"}; 8929 8930 action = get_tv_string_chk(&argvars[2]); 8931 if (action == NULL) 8932 return; /* type error; errmsg already given */ 8933 for (i = 0; i < 3; ++i) 8934 if (STRCMP(action, av[i]) == 0) 8935 break; 8936 if (i == 3) 8937 { 8938 EMSGN(_(e_invarg2), action); 8939 return; 8940 } 8941 } 8942 else 8943 action = (char_u *)"force"; 8944 8945 /* Go over all entries in the second dict and add them to the 8946 * first dict. */ 8947 todo = d2->dv_hashtab.ht_used; 8948 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2) 8949 { 8950 if (!HASHITEM_EMPTY(hi2)) 8951 { 8952 --todo; 8953 di1 = dict_find(d1, hi2->hi_key, -1); 8954 if (di1 == NULL) 8955 { 8956 di1 = dictitem_copy(HI2DI(hi2)); 8957 if (di1 != NULL && dict_add(d1, di1) == FAIL) 8958 dictitem_free(di1); 8959 } 8960 else if (*action == 'e') 8961 { 8962 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key); 8963 break; 8964 } 8965 else if (*action == 'f') 8966 { 8967 clear_tv(&di1->di_tv); 8968 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv); 8969 } 8970 } 8971 } 8972 8973 copy_tv(&argvars[0], rettv); 8974 } 8975 } 8976 else 8977 EMSG2(_(e_listdictarg), "extend()"); 8978 } 8979 8980 /* 8981 * "filereadable()" function 8982 */ 8983 static void 8984 f_filereadable(argvars, rettv) 8985 typval_T *argvars; 8986 typval_T *rettv; 8987 { 8988 FILE *fd; 8989 char_u *p; 8990 int n; 8991 8992 p = get_tv_string(&argvars[0]); 8993 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL) 8994 { 8995 n = TRUE; 8996 fclose(fd); 8997 } 8998 else 8999 n = FALSE; 9000 9001 rettv->vval.v_number = n; 9002 } 9003 9004 /* 9005 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have 9006 * rights to write into. 9007 */ 9008 static void 9009 f_filewritable(argvars, rettv) 9010 typval_T *argvars; 9011 typval_T *rettv; 9012 { 9013 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0])); 9014 } 9015 9016 static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir)); 9017 9018 static void 9019 findfilendir(argvars, rettv, dir) 9020 typval_T *argvars; 9021 typval_T *rettv; 9022 int dir; 9023 { 9024 #ifdef FEAT_SEARCHPATH 9025 char_u *fname; 9026 char_u *fresult = NULL; 9027 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path; 9028 char_u *p; 9029 char_u pathbuf[NUMBUFLEN]; 9030 int count = 1; 9031 int first = TRUE; 9032 int error = FALSE; 9033 #endif 9034 9035 rettv->vval.v_string = NULL; 9036 rettv->v_type = VAR_STRING; 9037 9038 #ifdef FEAT_SEARCHPATH 9039 fname = get_tv_string(&argvars[0]); 9040 9041 if (argvars[1].v_type != VAR_UNKNOWN) 9042 { 9043 p = get_tv_string_buf_chk(&argvars[1], pathbuf); 9044 if (p == NULL) 9045 error = TRUE; 9046 else 9047 { 9048 if (*p != NUL) 9049 path = p; 9050 9051 if (argvars[2].v_type != VAR_UNKNOWN) 9052 count = get_tv_number_chk(&argvars[2], &error); 9053 } 9054 } 9055 9056 if (count < 0 && rettv_list_alloc(rettv) == FAIL) 9057 error = TRUE; 9058 9059 if (*fname != NUL && !error) 9060 { 9061 do 9062 { 9063 if (rettv->v_type == VAR_STRING) 9064 vim_free(fresult); 9065 fresult = find_file_in_path_option(first ? fname : NULL, 9066 first ? (int)STRLEN(fname) : 0, 9067 0, first, path, dir, NULL, 9068 dir ? (char_u *)"" : curbuf->b_p_sua); 9069 first = FALSE; 9070 9071 if (fresult != NULL && rettv->v_type == VAR_LIST) 9072 list_append_string(rettv->vval.v_list, fresult, -1); 9073 9074 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL); 9075 } 9076 9077 if (rettv->v_type == VAR_STRING) 9078 rettv->vval.v_string = fresult; 9079 #endif 9080 } 9081 9082 static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map)); 9083 static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp)); 9084 9085 /* 9086 * Implementation of map() and filter(). 9087 */ 9088 static void 9089 filter_map(argvars, rettv, map) 9090 typval_T *argvars; 9091 typval_T *rettv; 9092 int map; 9093 { 9094 char_u buf[NUMBUFLEN]; 9095 char_u *expr; 9096 listitem_T *li, *nli; 9097 list_T *l = NULL; 9098 dictitem_T *di; 9099 hashtab_T *ht; 9100 hashitem_T *hi; 9101 dict_T *d = NULL; 9102 typval_T save_val; 9103 typval_T save_key; 9104 int rem; 9105 int todo; 9106 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()"; 9107 int save_did_emsg; 9108 9109 rettv->vval.v_number = 0; 9110 if (argvars[0].v_type == VAR_LIST) 9111 { 9112 if ((l = argvars[0].vval.v_list) == NULL 9113 || (map && tv_check_lock(l->lv_lock, msg))) 9114 return; 9115 } 9116 else if (argvars[0].v_type == VAR_DICT) 9117 { 9118 if ((d = argvars[0].vval.v_dict) == NULL 9119 || (map && tv_check_lock(d->dv_lock, msg))) 9120 return; 9121 } 9122 else 9123 { 9124 EMSG2(_(e_listdictarg), msg); 9125 return; 9126 } 9127 9128 expr = get_tv_string_buf_chk(&argvars[1], buf); 9129 /* On type errors, the preceding call has already displayed an error 9130 * message. Avoid a misleading error message for an empty string that 9131 * was not passed as argument. */ 9132 if (expr != NULL) 9133 { 9134 prepare_vimvar(VV_VAL, &save_val); 9135 expr = skipwhite(expr); 9136 9137 /* We reset "did_emsg" to be able to detect whether an error 9138 * occurred during evaluation of the expression. */ 9139 save_did_emsg = did_emsg; 9140 did_emsg = FALSE; 9141 9142 if (argvars[0].v_type == VAR_DICT) 9143 { 9144 prepare_vimvar(VV_KEY, &save_key); 9145 vimvars[VV_KEY].vv_type = VAR_STRING; 9146 9147 ht = &d->dv_hashtab; 9148 hash_lock(ht); 9149 todo = ht->ht_used; 9150 for (hi = ht->ht_array; todo > 0; ++hi) 9151 { 9152 if (!HASHITEM_EMPTY(hi)) 9153 { 9154 --todo; 9155 di = HI2DI(hi); 9156 if (tv_check_lock(di->di_tv.v_lock, msg)) 9157 break; 9158 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key); 9159 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL 9160 || did_emsg) 9161 break; 9162 if (!map && rem) 9163 dictitem_remove(d, di); 9164 clear_tv(&vimvars[VV_KEY].vv_tv); 9165 } 9166 } 9167 hash_unlock(ht); 9168 9169 restore_vimvar(VV_KEY, &save_key); 9170 } 9171 else 9172 { 9173 for (li = l->lv_first; li != NULL; li = nli) 9174 { 9175 if (tv_check_lock(li->li_tv.v_lock, msg)) 9176 break; 9177 nli = li->li_next; 9178 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL 9179 || did_emsg) 9180 break; 9181 if (!map && rem) 9182 listitem_remove(l, li); 9183 } 9184 } 9185 9186 restore_vimvar(VV_VAL, &save_val); 9187 9188 did_emsg |= save_did_emsg; 9189 } 9190 9191 copy_tv(&argvars[0], rettv); 9192 } 9193 9194 static int 9195 filter_map_one(tv, expr, map, remp) 9196 typval_T *tv; 9197 char_u *expr; 9198 int map; 9199 int *remp; 9200 { 9201 typval_T rettv; 9202 char_u *s; 9203 9204 copy_tv(tv, &vimvars[VV_VAL].vv_tv); 9205 s = expr; 9206 if (eval1(&s, &rettv, TRUE) == FAIL) 9207 return FAIL; 9208 if (*s != NUL) /* check for trailing chars after expr */ 9209 { 9210 EMSG2(_(e_invexpr2), s); 9211 return FAIL; 9212 } 9213 if (map) 9214 { 9215 /* map(): replace the list item value */ 9216 clear_tv(tv); 9217 rettv.v_lock = 0; 9218 *tv = rettv; 9219 } 9220 else 9221 { 9222 int error = FALSE; 9223 9224 /* filter(): when expr is zero remove the item */ 9225 *remp = (get_tv_number_chk(&rettv, &error) == 0); 9226 clear_tv(&rettv); 9227 /* On type error, nothing has been removed; return FAIL to stop the 9228 * loop. The error message was given by get_tv_number_chk(). */ 9229 if (error) 9230 return FAIL; 9231 } 9232 clear_tv(&vimvars[VV_VAL].vv_tv); 9233 return OK; 9234 } 9235 9236 /* 9237 * "filter()" function 9238 */ 9239 static void 9240 f_filter(argvars, rettv) 9241 typval_T *argvars; 9242 typval_T *rettv; 9243 { 9244 filter_map(argvars, rettv, FALSE); 9245 } 9246 9247 /* 9248 * "finddir({fname}[, {path}[, {count}]])" function 9249 */ 9250 static void 9251 f_finddir(argvars, rettv) 9252 typval_T *argvars; 9253 typval_T *rettv; 9254 { 9255 findfilendir(argvars, rettv, TRUE); 9256 } 9257 9258 /* 9259 * "findfile({fname}[, {path}[, {count}]])" function 9260 */ 9261 static void 9262 f_findfile(argvars, rettv) 9263 typval_T *argvars; 9264 typval_T *rettv; 9265 { 9266 findfilendir(argvars, rettv, FALSE); 9267 } 9268 9269 /* 9270 * "fnamemodify({fname}, {mods})" function 9271 */ 9272 static void 9273 f_fnamemodify(argvars, rettv) 9274 typval_T *argvars; 9275 typval_T *rettv; 9276 { 9277 char_u *fname; 9278 char_u *mods; 9279 int usedlen = 0; 9280 int len; 9281 char_u *fbuf = NULL; 9282 char_u buf[NUMBUFLEN]; 9283 9284 fname = get_tv_string_chk(&argvars[0]); 9285 mods = get_tv_string_buf_chk(&argvars[1], buf); 9286 if (fname == NULL || mods == NULL) 9287 fname = NULL; 9288 else 9289 { 9290 len = (int)STRLEN(fname); 9291 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len); 9292 } 9293 9294 rettv->v_type = VAR_STRING; 9295 if (fname == NULL) 9296 rettv->vval.v_string = NULL; 9297 else 9298 rettv->vval.v_string = vim_strnsave(fname, len); 9299 vim_free(fbuf); 9300 } 9301 9302 static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end)); 9303 9304 /* 9305 * "foldclosed()" function 9306 */ 9307 static void 9308 foldclosed_both(argvars, rettv, end) 9309 typval_T *argvars; 9310 typval_T *rettv; 9311 int end; 9312 { 9313 #ifdef FEAT_FOLDING 9314 linenr_T lnum; 9315 linenr_T first, last; 9316 9317 lnum = get_tv_lnum(argvars); 9318 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 9319 { 9320 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL)) 9321 { 9322 if (end) 9323 rettv->vval.v_number = (varnumber_T)last; 9324 else 9325 rettv->vval.v_number = (varnumber_T)first; 9326 return; 9327 } 9328 } 9329 #endif 9330 rettv->vval.v_number = -1; 9331 } 9332 9333 /* 9334 * "foldclosed()" function 9335 */ 9336 static void 9337 f_foldclosed(argvars, rettv) 9338 typval_T *argvars; 9339 typval_T *rettv; 9340 { 9341 foldclosed_both(argvars, rettv, FALSE); 9342 } 9343 9344 /* 9345 * "foldclosedend()" function 9346 */ 9347 static void 9348 f_foldclosedend(argvars, rettv) 9349 typval_T *argvars; 9350 typval_T *rettv; 9351 { 9352 foldclosed_both(argvars, rettv, TRUE); 9353 } 9354 9355 /* 9356 * "foldlevel()" function 9357 */ 9358 static void 9359 f_foldlevel(argvars, rettv) 9360 typval_T *argvars; 9361 typval_T *rettv; 9362 { 9363 #ifdef FEAT_FOLDING 9364 linenr_T lnum; 9365 9366 lnum = get_tv_lnum(argvars); 9367 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 9368 rettv->vval.v_number = foldLevel(lnum); 9369 else 9370 #endif 9371 rettv->vval.v_number = 0; 9372 } 9373 9374 /* 9375 * "foldtext()" function 9376 */ 9377 /*ARGSUSED*/ 9378 static void 9379 f_foldtext(argvars, rettv) 9380 typval_T *argvars; 9381 typval_T *rettv; 9382 { 9383 #ifdef FEAT_FOLDING 9384 linenr_T lnum; 9385 char_u *s; 9386 char_u *r; 9387 int len; 9388 char *txt; 9389 #endif 9390 9391 rettv->v_type = VAR_STRING; 9392 rettv->vval.v_string = NULL; 9393 #ifdef FEAT_FOLDING 9394 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0 9395 && (linenr_T)vimvars[VV_FOLDEND].vv_nr 9396 <= curbuf->b_ml.ml_line_count 9397 && vimvars[VV_FOLDDASHES].vv_str != NULL) 9398 { 9399 /* Find first non-empty line in the fold. */ 9400 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr; 9401 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr) 9402 { 9403 if (!linewhite(lnum)) 9404 break; 9405 ++lnum; 9406 } 9407 9408 /* Find interesting text in this line. */ 9409 s = skipwhite(ml_get(lnum)); 9410 /* skip C comment-start */ 9411 if (s[0] == '/' && (s[1] == '*' || s[1] == '/')) 9412 { 9413 s = skipwhite(s + 2); 9414 if (*skipwhite(s) == NUL 9415 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr) 9416 { 9417 s = skipwhite(ml_get(lnum + 1)); 9418 if (*s == '*') 9419 s = skipwhite(s + 1); 9420 } 9421 } 9422 txt = _("+-%s%3ld lines: "); 9423 r = alloc((unsigned)(STRLEN(txt) 9424 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */ 9425 + 20 /* for %3ld */ 9426 + STRLEN(s))); /* concatenated */ 9427 if (r != NULL) 9428 { 9429 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str, 9430 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr 9431 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1)); 9432 len = (int)STRLEN(r); 9433 STRCAT(r, s); 9434 /* remove 'foldmarker' and 'commentstring' */ 9435 foldtext_cleanup(r + len); 9436 rettv->vval.v_string = r; 9437 } 9438 } 9439 #endif 9440 } 9441 9442 /* 9443 * "foldtextresult(lnum)" function 9444 */ 9445 /*ARGSUSED*/ 9446 static void 9447 f_foldtextresult(argvars, rettv) 9448 typval_T *argvars; 9449 typval_T *rettv; 9450 { 9451 #ifdef FEAT_FOLDING 9452 linenr_T lnum; 9453 char_u *text; 9454 char_u buf[51]; 9455 foldinfo_T foldinfo; 9456 int fold_count; 9457 #endif 9458 9459 rettv->v_type = VAR_STRING; 9460 rettv->vval.v_string = NULL; 9461 #ifdef FEAT_FOLDING 9462 lnum = get_tv_lnum(argvars); 9463 /* treat illegal types and illegal string values for {lnum} the same */ 9464 if (lnum < 0) 9465 lnum = 0; 9466 fold_count = foldedCount(curwin, lnum, &foldinfo); 9467 if (fold_count > 0) 9468 { 9469 text = get_foldtext(curwin, lnum, lnum + fold_count - 1, 9470 &foldinfo, buf); 9471 if (text == buf) 9472 text = vim_strsave(text); 9473 rettv->vval.v_string = text; 9474 } 9475 #endif 9476 } 9477 9478 /* 9479 * "foreground()" function 9480 */ 9481 /*ARGSUSED*/ 9482 static void 9483 f_foreground(argvars, rettv) 9484 typval_T *argvars; 9485 typval_T *rettv; 9486 { 9487 rettv->vval.v_number = 0; 9488 #ifdef FEAT_GUI 9489 if (gui.in_use) 9490 gui_mch_set_foreground(); 9491 #else 9492 # ifdef WIN32 9493 win32_set_foreground(); 9494 # endif 9495 #endif 9496 } 9497 9498 /* 9499 * "function()" function 9500 */ 9501 /*ARGSUSED*/ 9502 static void 9503 f_function(argvars, rettv) 9504 typval_T *argvars; 9505 typval_T *rettv; 9506 { 9507 char_u *s; 9508 9509 rettv->vval.v_number = 0; 9510 s = get_tv_string(&argvars[0]); 9511 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s)) 9512 EMSG2(_(e_invarg2), s); 9513 else if (!function_exists(s)) 9514 EMSG2(_("E700: Unknown function: %s"), s); 9515 else 9516 { 9517 rettv->vval.v_string = vim_strsave(s); 9518 rettv->v_type = VAR_FUNC; 9519 } 9520 } 9521 9522 /* 9523 * "garbagecollect()" function 9524 */ 9525 /*ARGSUSED*/ 9526 static void 9527 f_garbagecollect(argvars, rettv) 9528 typval_T *argvars; 9529 typval_T *rettv; 9530 { 9531 garbage_collect(); 9532 } 9533 9534 /* 9535 * "get()" function 9536 */ 9537 static void 9538 f_get(argvars, rettv) 9539 typval_T *argvars; 9540 typval_T *rettv; 9541 { 9542 listitem_T *li; 9543 list_T *l; 9544 dictitem_T *di; 9545 dict_T *d; 9546 typval_T *tv = NULL; 9547 9548 if (argvars[0].v_type == VAR_LIST) 9549 { 9550 if ((l = argvars[0].vval.v_list) != NULL) 9551 { 9552 int error = FALSE; 9553 9554 li = list_find(l, get_tv_number_chk(&argvars[1], &error)); 9555 if (!error && li != NULL) 9556 tv = &li->li_tv; 9557 } 9558 } 9559 else if (argvars[0].v_type == VAR_DICT) 9560 { 9561 if ((d = argvars[0].vval.v_dict) != NULL) 9562 { 9563 di = dict_find(d, get_tv_string(&argvars[1]), -1); 9564 if (di != NULL) 9565 tv = &di->di_tv; 9566 } 9567 } 9568 else 9569 EMSG2(_(e_listdictarg), "get()"); 9570 9571 if (tv == NULL) 9572 { 9573 if (argvars[2].v_type == VAR_UNKNOWN) 9574 rettv->vval.v_number = 0; 9575 else 9576 copy_tv(&argvars[2], rettv); 9577 } 9578 else 9579 copy_tv(tv, rettv); 9580 } 9581 9582 static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv)); 9583 9584 /* 9585 * Get line or list of lines from buffer "buf" into "rettv". 9586 * Return a range (from start to end) of lines in rettv from the specified 9587 * buffer. 9588 * If 'retlist' is TRUE, then the lines are returned as a Vim List. 9589 */ 9590 static void 9591 get_buffer_lines(buf, start, end, retlist, rettv) 9592 buf_T *buf; 9593 linenr_T start; 9594 linenr_T end; 9595 int retlist; 9596 typval_T *rettv; 9597 { 9598 char_u *p; 9599 9600 if (retlist) 9601 { 9602 if (rettv_list_alloc(rettv) == FAIL) 9603 return; 9604 } 9605 else 9606 rettv->vval.v_number = 0; 9607 9608 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0) 9609 return; 9610 9611 if (!retlist) 9612 { 9613 if (start >= 1 && start <= buf->b_ml.ml_line_count) 9614 p = ml_get_buf(buf, start, FALSE); 9615 else 9616 p = (char_u *)""; 9617 9618 rettv->v_type = VAR_STRING; 9619 rettv->vval.v_string = vim_strsave(p); 9620 } 9621 else 9622 { 9623 if (end < start) 9624 return; 9625 9626 if (start < 1) 9627 start = 1; 9628 if (end > buf->b_ml.ml_line_count) 9629 end = buf->b_ml.ml_line_count; 9630 while (start <= end) 9631 if (list_append_string(rettv->vval.v_list, 9632 ml_get_buf(buf, start++, FALSE), -1) == FAIL) 9633 break; 9634 } 9635 } 9636 9637 /* 9638 * "getbufline()" function 9639 */ 9640 static void 9641 f_getbufline(argvars, rettv) 9642 typval_T *argvars; 9643 typval_T *rettv; 9644 { 9645 linenr_T lnum; 9646 linenr_T end; 9647 buf_T *buf; 9648 9649 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 9650 ++emsg_off; 9651 buf = get_buf_tv(&argvars[0]); 9652 --emsg_off; 9653 9654 lnum = get_tv_lnum_buf(&argvars[1], buf); 9655 if (argvars[2].v_type == VAR_UNKNOWN) 9656 end = lnum; 9657 else 9658 end = get_tv_lnum_buf(&argvars[2], buf); 9659 9660 get_buffer_lines(buf, lnum, end, TRUE, rettv); 9661 } 9662 9663 /* 9664 * "getbufvar()" function 9665 */ 9666 static void 9667 f_getbufvar(argvars, rettv) 9668 typval_T *argvars; 9669 typval_T *rettv; 9670 { 9671 buf_T *buf; 9672 buf_T *save_curbuf; 9673 char_u *varname; 9674 dictitem_T *v; 9675 9676 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 9677 varname = get_tv_string_chk(&argvars[1]); 9678 ++emsg_off; 9679 buf = get_buf_tv(&argvars[0]); 9680 9681 rettv->v_type = VAR_STRING; 9682 rettv->vval.v_string = NULL; 9683 9684 if (buf != NULL && varname != NULL) 9685 { 9686 if (*varname == '&') /* buffer-local-option */ 9687 { 9688 /* set curbuf to be our buf, temporarily */ 9689 save_curbuf = curbuf; 9690 curbuf = buf; 9691 9692 get_option_tv(&varname, rettv, TRUE); 9693 9694 /* restore previous notion of curbuf */ 9695 curbuf = save_curbuf; 9696 } 9697 else 9698 { 9699 if (*varname == NUL) 9700 /* let getbufvar({nr}, "") return the "b:" dictionary. The 9701 * scope prefix before the NUL byte is required by 9702 * find_var_in_ht(). */ 9703 varname = (char_u *)"b:" + 2; 9704 /* look up the variable */ 9705 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE); 9706 if (v != NULL) 9707 copy_tv(&v->di_tv, rettv); 9708 } 9709 } 9710 9711 --emsg_off; 9712 } 9713 9714 /* 9715 * "getchar()" function 9716 */ 9717 static void 9718 f_getchar(argvars, rettv) 9719 typval_T *argvars; 9720 typval_T *rettv; 9721 { 9722 varnumber_T n; 9723 int error = FALSE; 9724 9725 ++no_mapping; 9726 ++allow_keys; 9727 if (argvars[0].v_type == VAR_UNKNOWN) 9728 /* getchar(): blocking wait. */ 9729 n = safe_vgetc(); 9730 else if (get_tv_number_chk(&argvars[0], &error) == 1) 9731 /* getchar(1): only check if char avail */ 9732 n = vpeekc(); 9733 else if (error || vpeekc() == NUL) 9734 /* illegal argument or getchar(0) and no char avail: return zero */ 9735 n = 0; 9736 else 9737 /* getchar(0) and char avail: return char */ 9738 n = safe_vgetc(); 9739 --no_mapping; 9740 --allow_keys; 9741 9742 rettv->vval.v_number = n; 9743 if (IS_SPECIAL(n) || mod_mask != 0) 9744 { 9745 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */ 9746 int i = 0; 9747 9748 /* Turn a special key into three bytes, plus modifier. */ 9749 if (mod_mask != 0) 9750 { 9751 temp[i++] = K_SPECIAL; 9752 temp[i++] = KS_MODIFIER; 9753 temp[i++] = mod_mask; 9754 } 9755 if (IS_SPECIAL(n)) 9756 { 9757 temp[i++] = K_SPECIAL; 9758 temp[i++] = K_SECOND(n); 9759 temp[i++] = K_THIRD(n); 9760 } 9761 #ifdef FEAT_MBYTE 9762 else if (has_mbyte) 9763 i += (*mb_char2bytes)(n, temp + i); 9764 #endif 9765 else 9766 temp[i++] = n; 9767 temp[i++] = NUL; 9768 rettv->v_type = VAR_STRING; 9769 rettv->vval.v_string = vim_strsave(temp); 9770 } 9771 } 9772 9773 /* 9774 * "getcharmod()" function 9775 */ 9776 /*ARGSUSED*/ 9777 static void 9778 f_getcharmod(argvars, rettv) 9779 typval_T *argvars; 9780 typval_T *rettv; 9781 { 9782 rettv->vval.v_number = mod_mask; 9783 } 9784 9785 /* 9786 * "getcmdline()" function 9787 */ 9788 /*ARGSUSED*/ 9789 static void 9790 f_getcmdline(argvars, rettv) 9791 typval_T *argvars; 9792 typval_T *rettv; 9793 { 9794 rettv->v_type = VAR_STRING; 9795 rettv->vval.v_string = get_cmdline_str(); 9796 } 9797 9798 /* 9799 * "getcmdpos()" function 9800 */ 9801 /*ARGSUSED*/ 9802 static void 9803 f_getcmdpos(argvars, rettv) 9804 typval_T *argvars; 9805 typval_T *rettv; 9806 { 9807 rettv->vval.v_number = get_cmdline_pos() + 1; 9808 } 9809 9810 /* 9811 * "getcmdtype()" function 9812 */ 9813 /*ARGSUSED*/ 9814 static void 9815 f_getcmdtype(argvars, rettv) 9816 typval_T *argvars; 9817 typval_T *rettv; 9818 { 9819 rettv->v_type = VAR_STRING; 9820 rettv->vval.v_string = alloc(2); 9821 if (rettv->vval.v_string != NULL) 9822 { 9823 rettv->vval.v_string[0] = get_cmdline_type(); 9824 rettv->vval.v_string[1] = NUL; 9825 } 9826 } 9827 9828 /* 9829 * "getcwd()" function 9830 */ 9831 /*ARGSUSED*/ 9832 static void 9833 f_getcwd(argvars, rettv) 9834 typval_T *argvars; 9835 typval_T *rettv; 9836 { 9837 char_u cwd[MAXPATHL]; 9838 9839 rettv->v_type = VAR_STRING; 9840 if (mch_dirname(cwd, MAXPATHL) == FAIL) 9841 rettv->vval.v_string = NULL; 9842 else 9843 { 9844 rettv->vval.v_string = vim_strsave(cwd); 9845 #ifdef BACKSLASH_IN_FILENAME 9846 if (rettv->vval.v_string != NULL) 9847 slash_adjust(rettv->vval.v_string); 9848 #endif 9849 } 9850 } 9851 9852 /* 9853 * "getfontname()" function 9854 */ 9855 /*ARGSUSED*/ 9856 static void 9857 f_getfontname(argvars, rettv) 9858 typval_T *argvars; 9859 typval_T *rettv; 9860 { 9861 rettv->v_type = VAR_STRING; 9862 rettv->vval.v_string = NULL; 9863 #ifdef FEAT_GUI 9864 if (gui.in_use) 9865 { 9866 GuiFont font; 9867 char_u *name = NULL; 9868 9869 if (argvars[0].v_type == VAR_UNKNOWN) 9870 { 9871 /* Get the "Normal" font. Either the name saved by 9872 * hl_set_font_name() or from the font ID. */ 9873 font = gui.norm_font; 9874 name = hl_get_font_name(); 9875 } 9876 else 9877 { 9878 name = get_tv_string(&argvars[0]); 9879 if (STRCMP(name, "*") == 0) /* don't use font dialog */ 9880 return; 9881 font = gui_mch_get_font(name, FALSE); 9882 if (font == NOFONT) 9883 return; /* Invalid font name, return empty string. */ 9884 } 9885 rettv->vval.v_string = gui_mch_get_fontname(font, name); 9886 if (argvars[0].v_type != VAR_UNKNOWN) 9887 gui_mch_free_font(font); 9888 } 9889 #endif 9890 } 9891 9892 /* 9893 * "getfperm({fname})" function 9894 */ 9895 static void 9896 f_getfperm(argvars, rettv) 9897 typval_T *argvars; 9898 typval_T *rettv; 9899 { 9900 char_u *fname; 9901 struct stat st; 9902 char_u *perm = NULL; 9903 char_u flags[] = "rwx"; 9904 int i; 9905 9906 fname = get_tv_string(&argvars[0]); 9907 9908 rettv->v_type = VAR_STRING; 9909 if (mch_stat((char *)fname, &st) >= 0) 9910 { 9911 perm = vim_strsave((char_u *)"---------"); 9912 if (perm != NULL) 9913 { 9914 for (i = 0; i < 9; i++) 9915 { 9916 if (st.st_mode & (1 << (8 - i))) 9917 perm[i] = flags[i % 3]; 9918 } 9919 } 9920 } 9921 rettv->vval.v_string = perm; 9922 } 9923 9924 /* 9925 * "getfsize({fname})" function 9926 */ 9927 static void 9928 f_getfsize(argvars, rettv) 9929 typval_T *argvars; 9930 typval_T *rettv; 9931 { 9932 char_u *fname; 9933 struct stat st; 9934 9935 fname = get_tv_string(&argvars[0]); 9936 9937 rettv->v_type = VAR_NUMBER; 9938 9939 if (mch_stat((char *)fname, &st) >= 0) 9940 { 9941 if (mch_isdir(fname)) 9942 rettv->vval.v_number = 0; 9943 else 9944 rettv->vval.v_number = (varnumber_T)st.st_size; 9945 } 9946 else 9947 rettv->vval.v_number = -1; 9948 } 9949 9950 /* 9951 * "getftime({fname})" function 9952 */ 9953 static void 9954 f_getftime(argvars, rettv) 9955 typval_T *argvars; 9956 typval_T *rettv; 9957 { 9958 char_u *fname; 9959 struct stat st; 9960 9961 fname = get_tv_string(&argvars[0]); 9962 9963 if (mch_stat((char *)fname, &st) >= 0) 9964 rettv->vval.v_number = (varnumber_T)st.st_mtime; 9965 else 9966 rettv->vval.v_number = -1; 9967 } 9968 9969 /* 9970 * "getftype({fname})" function 9971 */ 9972 static void 9973 f_getftype(argvars, rettv) 9974 typval_T *argvars; 9975 typval_T *rettv; 9976 { 9977 char_u *fname; 9978 struct stat st; 9979 char_u *type = NULL; 9980 char *t; 9981 9982 fname = get_tv_string(&argvars[0]); 9983 9984 rettv->v_type = VAR_STRING; 9985 if (mch_lstat((char *)fname, &st) >= 0) 9986 { 9987 #ifdef S_ISREG 9988 if (S_ISREG(st.st_mode)) 9989 t = "file"; 9990 else if (S_ISDIR(st.st_mode)) 9991 t = "dir"; 9992 # ifdef S_ISLNK 9993 else if (S_ISLNK(st.st_mode)) 9994 t = "link"; 9995 # endif 9996 # ifdef S_ISBLK 9997 else if (S_ISBLK(st.st_mode)) 9998 t = "bdev"; 9999 # endif 10000 # ifdef S_ISCHR 10001 else if (S_ISCHR(st.st_mode)) 10002 t = "cdev"; 10003 # endif 10004 # ifdef S_ISFIFO 10005 else if (S_ISFIFO(st.st_mode)) 10006 t = "fifo"; 10007 # endif 10008 # ifdef S_ISSOCK 10009 else if (S_ISSOCK(st.st_mode)) 10010 t = "fifo"; 10011 # endif 10012 else 10013 t = "other"; 10014 #else 10015 # ifdef S_IFMT 10016 switch (st.st_mode & S_IFMT) 10017 { 10018 case S_IFREG: t = "file"; break; 10019 case S_IFDIR: t = "dir"; break; 10020 # ifdef S_IFLNK 10021 case S_IFLNK: t = "link"; break; 10022 # endif 10023 # ifdef S_IFBLK 10024 case S_IFBLK: t = "bdev"; break; 10025 # endif 10026 # ifdef S_IFCHR 10027 case S_IFCHR: t = "cdev"; break; 10028 # endif 10029 # ifdef S_IFIFO 10030 case S_IFIFO: t = "fifo"; break; 10031 # endif 10032 # ifdef S_IFSOCK 10033 case S_IFSOCK: t = "socket"; break; 10034 # endif 10035 default: t = "other"; 10036 } 10037 # else 10038 if (mch_isdir(fname)) 10039 t = "dir"; 10040 else 10041 t = "file"; 10042 # endif 10043 #endif 10044 type = vim_strsave((char_u *)t); 10045 } 10046 rettv->vval.v_string = type; 10047 } 10048 10049 /* 10050 * "getline(lnum, [end])" function 10051 */ 10052 static void 10053 f_getline(argvars, rettv) 10054 typval_T *argvars; 10055 typval_T *rettv; 10056 { 10057 linenr_T lnum; 10058 linenr_T end; 10059 int retlist; 10060 10061 lnum = get_tv_lnum(argvars); 10062 if (argvars[1].v_type == VAR_UNKNOWN) 10063 { 10064 end = 0; 10065 retlist = FALSE; 10066 } 10067 else 10068 { 10069 end = get_tv_lnum(&argvars[1]); 10070 retlist = TRUE; 10071 } 10072 10073 get_buffer_lines(curbuf, lnum, end, retlist, rettv); 10074 } 10075 10076 /* 10077 * "getpos(string)" function 10078 */ 10079 static void 10080 f_getpos(argvars, rettv) 10081 typval_T *argvars; 10082 typval_T *rettv; 10083 { 10084 pos_T *fp; 10085 list_T *l; 10086 int fnum = -1; 10087 10088 if (rettv_list_alloc(rettv) == OK) 10089 { 10090 l = rettv->vval.v_list; 10091 fp = var2fpos(&argvars[0], TRUE, &fnum); 10092 if (fnum != -1) 10093 list_append_number(l, (varnumber_T)fnum); 10094 else 10095 list_append_number(l, (varnumber_T)0); 10096 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum 10097 : (varnumber_T)0); 10098 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1 10099 : (varnumber_T)0); 10100 list_append_number(l, 10101 #ifdef FEAT_VIRTUALEDIT 10102 (fp != NULL) ? (varnumber_T)fp->coladd : 10103 #endif 10104 (varnumber_T)0); 10105 } 10106 else 10107 rettv->vval.v_number = FALSE; 10108 } 10109 10110 /* 10111 * "getqflist()" and "getloclist()" functions 10112 */ 10113 /*ARGSUSED*/ 10114 static void 10115 f_getqflist(argvars, rettv) 10116 typval_T *argvars; 10117 typval_T *rettv; 10118 { 10119 #ifdef FEAT_QUICKFIX 10120 win_T *wp; 10121 #endif 10122 10123 rettv->vval.v_number = FALSE; 10124 #ifdef FEAT_QUICKFIX 10125 if (rettv_list_alloc(rettv) == OK) 10126 { 10127 wp = NULL; 10128 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */ 10129 { 10130 wp = find_win_by_nr(&argvars[0]); 10131 if (wp == NULL) 10132 return; 10133 } 10134 10135 (void)get_errorlist(wp, rettv->vval.v_list); 10136 } 10137 #endif 10138 } 10139 10140 /* 10141 * "getreg()" function 10142 */ 10143 static void 10144 f_getreg(argvars, rettv) 10145 typval_T *argvars; 10146 typval_T *rettv; 10147 { 10148 char_u *strregname; 10149 int regname; 10150 int arg2 = FALSE; 10151 int error = FALSE; 10152 10153 if (argvars[0].v_type != VAR_UNKNOWN) 10154 { 10155 strregname = get_tv_string_chk(&argvars[0]); 10156 error = strregname == NULL; 10157 if (argvars[1].v_type != VAR_UNKNOWN) 10158 arg2 = get_tv_number_chk(&argvars[1], &error); 10159 } 10160 else 10161 strregname = vimvars[VV_REG].vv_str; 10162 regname = (strregname == NULL ? '"' : *strregname); 10163 if (regname == 0) 10164 regname = '"'; 10165 10166 rettv->v_type = VAR_STRING; 10167 rettv->vval.v_string = error ? NULL : 10168 get_reg_contents(regname, TRUE, arg2); 10169 } 10170 10171 /* 10172 * "getregtype()" function 10173 */ 10174 static void 10175 f_getregtype(argvars, rettv) 10176 typval_T *argvars; 10177 typval_T *rettv; 10178 { 10179 char_u *strregname; 10180 int regname; 10181 char_u buf[NUMBUFLEN + 2]; 10182 long reglen = 0; 10183 10184 if (argvars[0].v_type != VAR_UNKNOWN) 10185 { 10186 strregname = get_tv_string_chk(&argvars[0]); 10187 if (strregname == NULL) /* type error; errmsg already given */ 10188 { 10189 rettv->v_type = VAR_STRING; 10190 rettv->vval.v_string = NULL; 10191 return; 10192 } 10193 } 10194 else 10195 /* Default to v:register */ 10196 strregname = vimvars[VV_REG].vv_str; 10197 10198 regname = (strregname == NULL ? '"' : *strregname); 10199 if (regname == 0) 10200 regname = '"'; 10201 10202 buf[0] = NUL; 10203 buf[1] = NUL; 10204 switch (get_reg_type(regname, ®len)) 10205 { 10206 case MLINE: buf[0] = 'V'; break; 10207 case MCHAR: buf[0] = 'v'; break; 10208 #ifdef FEAT_VISUAL 10209 case MBLOCK: 10210 buf[0] = Ctrl_V; 10211 sprintf((char *)buf + 1, "%ld", reglen + 1); 10212 break; 10213 #endif 10214 } 10215 rettv->v_type = VAR_STRING; 10216 rettv->vval.v_string = vim_strsave(buf); 10217 } 10218 10219 /* 10220 * "getwinposx()" function 10221 */ 10222 /*ARGSUSED*/ 10223 static void 10224 f_getwinposx(argvars, rettv) 10225 typval_T *argvars; 10226 typval_T *rettv; 10227 { 10228 rettv->vval.v_number = -1; 10229 #ifdef FEAT_GUI 10230 if (gui.in_use) 10231 { 10232 int x, y; 10233 10234 if (gui_mch_get_winpos(&x, &y) == OK) 10235 rettv->vval.v_number = x; 10236 } 10237 #endif 10238 } 10239 10240 /* 10241 * "getwinposy()" function 10242 */ 10243 /*ARGSUSED*/ 10244 static void 10245 f_getwinposy(argvars, rettv) 10246 typval_T *argvars; 10247 typval_T *rettv; 10248 { 10249 rettv->vval.v_number = -1; 10250 #ifdef FEAT_GUI 10251 if (gui.in_use) 10252 { 10253 int x, y; 10254 10255 if (gui_mch_get_winpos(&x, &y) == OK) 10256 rettv->vval.v_number = y; 10257 } 10258 #endif 10259 } 10260 10261 static win_T * 10262 find_win_by_nr(vp) 10263 typval_T *vp; 10264 { 10265 #ifdef FEAT_WINDOWS 10266 win_T *wp; 10267 #endif 10268 int nr; 10269 10270 nr = get_tv_number_chk(vp, NULL); 10271 10272 #ifdef FEAT_WINDOWS 10273 if (nr < 0) 10274 return NULL; 10275 if (nr == 0) 10276 return curwin; 10277 10278 for (wp = firstwin; wp != NULL; wp = wp->w_next) 10279 if (--nr <= 0) 10280 break; 10281 return wp; 10282 #else 10283 if (nr == 0 || nr == 1) 10284 return curwin; 10285 return NULL; 10286 #endif 10287 } 10288 10289 /* 10290 * "getwinvar()" function 10291 */ 10292 static void 10293 f_getwinvar(argvars, rettv) 10294 typval_T *argvars; 10295 typval_T *rettv; 10296 { 10297 win_T *win, *oldcurwin; 10298 char_u *varname; 10299 dictitem_T *v; 10300 10301 win = find_win_by_nr(&argvars[0]); 10302 varname = get_tv_string_chk(&argvars[1]); 10303 ++emsg_off; 10304 10305 rettv->v_type = VAR_STRING; 10306 rettv->vval.v_string = NULL; 10307 10308 if (win != NULL && varname != NULL) 10309 { 10310 if (*varname == '&') /* window-local-option */ 10311 { 10312 /* Set curwin to be our win, temporarily. Also set curbuf, so 10313 * that we can get buffer-local options. */ 10314 oldcurwin = curwin; 10315 curwin = win; 10316 curbuf = win->w_buffer; 10317 10318 get_option_tv(&varname, rettv, 1); 10319 10320 /* restore previous notion of curwin */ 10321 curwin = oldcurwin; 10322 curbuf = curwin->w_buffer; 10323 } 10324 else 10325 { 10326 if (*varname == NUL) 10327 /* let getwinvar({nr}, "") return the "w:" dictionary. The 10328 * scope prefix before the NUL byte is required by 10329 * find_var_in_ht(). */ 10330 varname = (char_u *)"w:" + 2; 10331 /* look up the variable */ 10332 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE); 10333 if (v != NULL) 10334 copy_tv(&v->di_tv, rettv); 10335 } 10336 } 10337 10338 --emsg_off; 10339 } 10340 10341 /* 10342 * "glob()" function 10343 */ 10344 static void 10345 f_glob(argvars, rettv) 10346 typval_T *argvars; 10347 typval_T *rettv; 10348 { 10349 expand_T xpc; 10350 10351 ExpandInit(&xpc); 10352 xpc.xp_context = EXPAND_FILES; 10353 rettv->v_type = VAR_STRING; 10354 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]), 10355 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL); 10356 ExpandCleanup(&xpc); 10357 } 10358 10359 /* 10360 * "globpath()" function 10361 */ 10362 static void 10363 f_globpath(argvars, rettv) 10364 typval_T *argvars; 10365 typval_T *rettv; 10366 { 10367 char_u buf1[NUMBUFLEN]; 10368 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1); 10369 10370 rettv->v_type = VAR_STRING; 10371 if (file == NULL) 10372 rettv->vval.v_string = NULL; 10373 else 10374 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file); 10375 } 10376 10377 /* 10378 * "has()" function 10379 */ 10380 static void 10381 f_has(argvars, rettv) 10382 typval_T *argvars; 10383 typval_T *rettv; 10384 { 10385 int i; 10386 char_u *name; 10387 int n = FALSE; 10388 static char *(has_list[]) = 10389 { 10390 #ifdef AMIGA 10391 "amiga", 10392 # ifdef FEAT_ARP 10393 "arp", 10394 # endif 10395 #endif 10396 #ifdef __BEOS__ 10397 "beos", 10398 #endif 10399 #ifdef MSDOS 10400 # ifdef DJGPP 10401 "dos32", 10402 # else 10403 "dos16", 10404 # endif 10405 #endif 10406 #ifdef MACOS 10407 "mac", 10408 #endif 10409 #if defined(MACOS_X_UNIX) 10410 "macunix", 10411 #endif 10412 #ifdef OS2 10413 "os2", 10414 #endif 10415 #ifdef __QNX__ 10416 "qnx", 10417 #endif 10418 #ifdef RISCOS 10419 "riscos", 10420 #endif 10421 #ifdef UNIX 10422 "unix", 10423 #endif 10424 #ifdef VMS 10425 "vms", 10426 #endif 10427 #ifdef WIN16 10428 "win16", 10429 #endif 10430 #ifdef WIN32 10431 "win32", 10432 #endif 10433 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__)) 10434 "win32unix", 10435 #endif 10436 #ifdef WIN64 10437 "win64", 10438 #endif 10439 #ifdef EBCDIC 10440 "ebcdic", 10441 #endif 10442 #ifndef CASE_INSENSITIVE_FILENAME 10443 "fname_case", 10444 #endif 10445 #ifdef FEAT_ARABIC 10446 "arabic", 10447 #endif 10448 #ifdef FEAT_AUTOCMD 10449 "autocmd", 10450 #endif 10451 #ifdef FEAT_BEVAL 10452 "balloon_eval", 10453 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */ 10454 "balloon_multiline", 10455 # endif 10456 #endif 10457 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS) 10458 "builtin_terms", 10459 # ifdef ALL_BUILTIN_TCAPS 10460 "all_builtin_terms", 10461 # endif 10462 #endif 10463 #ifdef FEAT_BYTEOFF 10464 "byte_offset", 10465 #endif 10466 #ifdef FEAT_CINDENT 10467 "cindent", 10468 #endif 10469 #ifdef FEAT_CLIENTSERVER 10470 "clientserver", 10471 #endif 10472 #ifdef FEAT_CLIPBOARD 10473 "clipboard", 10474 #endif 10475 #ifdef FEAT_CMDL_COMPL 10476 "cmdline_compl", 10477 #endif 10478 #ifdef FEAT_CMDHIST 10479 "cmdline_hist", 10480 #endif 10481 #ifdef FEAT_COMMENTS 10482 "comments", 10483 #endif 10484 #ifdef FEAT_CRYPT 10485 "cryptv", 10486 #endif 10487 #ifdef FEAT_CSCOPE 10488 "cscope", 10489 #endif 10490 #ifdef CURSOR_SHAPE 10491 "cursorshape", 10492 #endif 10493 #ifdef DEBUG 10494 "debug", 10495 #endif 10496 #ifdef FEAT_CON_DIALOG 10497 "dialog_con", 10498 #endif 10499 #ifdef FEAT_GUI_DIALOG 10500 "dialog_gui", 10501 #endif 10502 #ifdef FEAT_DIFF 10503 "diff", 10504 #endif 10505 #ifdef FEAT_DIGRAPHS 10506 "digraphs", 10507 #endif 10508 #ifdef FEAT_DND 10509 "dnd", 10510 #endif 10511 #ifdef FEAT_EMACS_TAGS 10512 "emacs_tags", 10513 #endif 10514 "eval", /* always present, of course! */ 10515 #ifdef FEAT_EX_EXTRA 10516 "ex_extra", 10517 #endif 10518 #ifdef FEAT_SEARCH_EXTRA 10519 "extra_search", 10520 #endif 10521 #ifdef FEAT_FKMAP 10522 "farsi", 10523 #endif 10524 #ifdef FEAT_SEARCHPATH 10525 "file_in_path", 10526 #endif 10527 #if defined(UNIX) && !defined(USE_SYSTEM) 10528 "filterpipe", 10529 #endif 10530 #ifdef FEAT_FIND_ID 10531 "find_in_path", 10532 #endif 10533 #ifdef FEAT_FOLDING 10534 "folding", 10535 #endif 10536 #ifdef FEAT_FOOTER 10537 "footer", 10538 #endif 10539 #if !defined(USE_SYSTEM) && defined(UNIX) 10540 "fork", 10541 #endif 10542 #ifdef FEAT_GETTEXT 10543 "gettext", 10544 #endif 10545 #ifdef FEAT_GUI 10546 "gui", 10547 #endif 10548 #ifdef FEAT_GUI_ATHENA 10549 # ifdef FEAT_GUI_NEXTAW 10550 "gui_neXtaw", 10551 # else 10552 "gui_athena", 10553 # endif 10554 #endif 10555 #ifdef FEAT_GUI_GTK 10556 "gui_gtk", 10557 # ifdef HAVE_GTK2 10558 "gui_gtk2", 10559 # endif 10560 #endif 10561 #ifdef FEAT_GUI_MAC 10562 "gui_mac", 10563 #endif 10564 #ifdef FEAT_GUI_MOTIF 10565 "gui_motif", 10566 #endif 10567 #ifdef FEAT_GUI_PHOTON 10568 "gui_photon", 10569 #endif 10570 #ifdef FEAT_GUI_W16 10571 "gui_win16", 10572 #endif 10573 #ifdef FEAT_GUI_W32 10574 "gui_win32", 10575 #endif 10576 #ifdef FEAT_HANGULIN 10577 "hangul_input", 10578 #endif 10579 #if defined(HAVE_ICONV_H) && defined(USE_ICONV) 10580 "iconv", 10581 #endif 10582 #ifdef FEAT_INS_EXPAND 10583 "insert_expand", 10584 #endif 10585 #ifdef FEAT_JUMPLIST 10586 "jumplist", 10587 #endif 10588 #ifdef FEAT_KEYMAP 10589 "keymap", 10590 #endif 10591 #ifdef FEAT_LANGMAP 10592 "langmap", 10593 #endif 10594 #ifdef FEAT_LIBCALL 10595 "libcall", 10596 #endif 10597 #ifdef FEAT_LINEBREAK 10598 "linebreak", 10599 #endif 10600 #ifdef FEAT_LISP 10601 "lispindent", 10602 #endif 10603 #ifdef FEAT_LISTCMDS 10604 "listcmds", 10605 #endif 10606 #ifdef FEAT_LOCALMAP 10607 "localmap", 10608 #endif 10609 #ifdef FEAT_MENU 10610 "menu", 10611 #endif 10612 #ifdef FEAT_SESSION 10613 "mksession", 10614 #endif 10615 #ifdef FEAT_MODIFY_FNAME 10616 "modify_fname", 10617 #endif 10618 #ifdef FEAT_MOUSE 10619 "mouse", 10620 #endif 10621 #ifdef FEAT_MOUSESHAPE 10622 "mouseshape", 10623 #endif 10624 #if defined(UNIX) || defined(VMS) 10625 # ifdef FEAT_MOUSE_DEC 10626 "mouse_dec", 10627 # endif 10628 # ifdef FEAT_MOUSE_GPM 10629 "mouse_gpm", 10630 # endif 10631 # ifdef FEAT_MOUSE_JSB 10632 "mouse_jsbterm", 10633 # endif 10634 # ifdef FEAT_MOUSE_NET 10635 "mouse_netterm", 10636 # endif 10637 # ifdef FEAT_MOUSE_PTERM 10638 "mouse_pterm", 10639 # endif 10640 # ifdef FEAT_MOUSE_XTERM 10641 "mouse_xterm", 10642 # endif 10643 #endif 10644 #ifdef FEAT_MBYTE 10645 "multi_byte", 10646 #endif 10647 #ifdef FEAT_MBYTE_IME 10648 "multi_byte_ime", 10649 #endif 10650 #ifdef FEAT_MULTI_LANG 10651 "multi_lang", 10652 #endif 10653 #ifdef FEAT_MZSCHEME 10654 #ifndef DYNAMIC_MZSCHEME 10655 "mzscheme", 10656 #endif 10657 #endif 10658 #ifdef FEAT_OLE 10659 "ole", 10660 #endif 10661 #ifdef FEAT_OSFILETYPE 10662 "osfiletype", 10663 #endif 10664 #ifdef FEAT_PATH_EXTRA 10665 "path_extra", 10666 #endif 10667 #ifdef FEAT_PERL 10668 #ifndef DYNAMIC_PERL 10669 "perl", 10670 #endif 10671 #endif 10672 #ifdef FEAT_PYTHON 10673 #ifndef DYNAMIC_PYTHON 10674 "python", 10675 #endif 10676 #endif 10677 #ifdef FEAT_POSTSCRIPT 10678 "postscript", 10679 #endif 10680 #ifdef FEAT_PRINTER 10681 "printer", 10682 #endif 10683 #ifdef FEAT_PROFILE 10684 "profile", 10685 #endif 10686 #ifdef FEAT_RELTIME 10687 "reltime", 10688 #endif 10689 #ifdef FEAT_QUICKFIX 10690 "quickfix", 10691 #endif 10692 #ifdef FEAT_RIGHTLEFT 10693 "rightleft", 10694 #endif 10695 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY) 10696 "ruby", 10697 #endif 10698 #ifdef FEAT_SCROLLBIND 10699 "scrollbind", 10700 #endif 10701 #ifdef FEAT_CMDL_INFO 10702 "showcmd", 10703 "cmdline_info", 10704 #endif 10705 #ifdef FEAT_SIGNS 10706 "signs", 10707 #endif 10708 #ifdef FEAT_SMARTINDENT 10709 "smartindent", 10710 #endif 10711 #ifdef FEAT_SNIFF 10712 "sniff", 10713 #endif 10714 #ifdef FEAT_STL_OPT 10715 "statusline", 10716 #endif 10717 #ifdef FEAT_SUN_WORKSHOP 10718 "sun_workshop", 10719 #endif 10720 #ifdef FEAT_NETBEANS_INTG 10721 "netbeans_intg", 10722 #endif 10723 #ifdef FEAT_SPELL 10724 "spell", 10725 #endif 10726 #ifdef FEAT_SYN_HL 10727 "syntax", 10728 #endif 10729 #if defined(USE_SYSTEM) || !defined(UNIX) 10730 "system", 10731 #endif 10732 #ifdef FEAT_TAG_BINS 10733 "tag_binary", 10734 #endif 10735 #ifdef FEAT_TAG_OLDSTATIC 10736 "tag_old_static", 10737 #endif 10738 #ifdef FEAT_TAG_ANYWHITE 10739 "tag_any_white", 10740 #endif 10741 #ifdef FEAT_TCL 10742 # ifndef DYNAMIC_TCL 10743 "tcl", 10744 # endif 10745 #endif 10746 #ifdef TERMINFO 10747 "terminfo", 10748 #endif 10749 #ifdef FEAT_TERMRESPONSE 10750 "termresponse", 10751 #endif 10752 #ifdef FEAT_TEXTOBJ 10753 "textobjects", 10754 #endif 10755 #ifdef HAVE_TGETENT 10756 "tgetent", 10757 #endif 10758 #ifdef FEAT_TITLE 10759 "title", 10760 #endif 10761 #ifdef FEAT_TOOLBAR 10762 "toolbar", 10763 #endif 10764 #ifdef FEAT_USR_CMDS 10765 "user-commands", /* was accidentally included in 5.4 */ 10766 "user_commands", 10767 #endif 10768 #ifdef FEAT_VIMINFO 10769 "viminfo", 10770 #endif 10771 #ifdef FEAT_VERTSPLIT 10772 "vertsplit", 10773 #endif 10774 #ifdef FEAT_VIRTUALEDIT 10775 "virtualedit", 10776 #endif 10777 #ifdef FEAT_VISUAL 10778 "visual", 10779 #endif 10780 #ifdef FEAT_VISUALEXTRA 10781 "visualextra", 10782 #endif 10783 #ifdef FEAT_VREPLACE 10784 "vreplace", 10785 #endif 10786 #ifdef FEAT_WILDIGN 10787 "wildignore", 10788 #endif 10789 #ifdef FEAT_WILDMENU 10790 "wildmenu", 10791 #endif 10792 #ifdef FEAT_WINDOWS 10793 "windows", 10794 #endif 10795 #ifdef FEAT_WAK 10796 "winaltkeys", 10797 #endif 10798 #ifdef FEAT_WRITEBACKUP 10799 "writebackup", 10800 #endif 10801 #ifdef FEAT_XIM 10802 "xim", 10803 #endif 10804 #ifdef FEAT_XFONTSET 10805 "xfontset", 10806 #endif 10807 #ifdef USE_XSMP 10808 "xsmp", 10809 #endif 10810 #ifdef USE_XSMP_INTERACT 10811 "xsmp_interact", 10812 #endif 10813 #ifdef FEAT_XCLIPBOARD 10814 "xterm_clipboard", 10815 #endif 10816 #ifdef FEAT_XTERM_SAVE 10817 "xterm_save", 10818 #endif 10819 #if defined(UNIX) && defined(FEAT_X11) 10820 "X11", 10821 #endif 10822 NULL 10823 }; 10824 10825 name = get_tv_string(&argvars[0]); 10826 for (i = 0; has_list[i] != NULL; ++i) 10827 if (STRICMP(name, has_list[i]) == 0) 10828 { 10829 n = TRUE; 10830 break; 10831 } 10832 10833 if (n == FALSE) 10834 { 10835 if (STRNICMP(name, "patch", 5) == 0) 10836 n = has_patch(atoi((char *)name + 5)); 10837 else if (STRICMP(name, "vim_starting") == 0) 10838 n = (starting != 0); 10839 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32) 10840 else if (STRICMP(name, "balloon_multiline") == 0) 10841 n = multiline_balloon_available(); 10842 #endif 10843 #ifdef DYNAMIC_TCL 10844 else if (STRICMP(name, "tcl") == 0) 10845 n = tcl_enabled(FALSE); 10846 #endif 10847 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV) 10848 else if (STRICMP(name, "iconv") == 0) 10849 n = iconv_enabled(FALSE); 10850 #endif 10851 #ifdef DYNAMIC_MZSCHEME 10852 else if (STRICMP(name, "mzscheme") == 0) 10853 n = mzscheme_enabled(FALSE); 10854 #endif 10855 #ifdef DYNAMIC_RUBY 10856 else if (STRICMP(name, "ruby") == 0) 10857 n = ruby_enabled(FALSE); 10858 #endif 10859 #ifdef DYNAMIC_PYTHON 10860 else if (STRICMP(name, "python") == 0) 10861 n = python_enabled(FALSE); 10862 #endif 10863 #ifdef DYNAMIC_PERL 10864 else if (STRICMP(name, "perl") == 0) 10865 n = perl_enabled(FALSE); 10866 #endif 10867 #ifdef FEAT_GUI 10868 else if (STRICMP(name, "gui_running") == 0) 10869 n = (gui.in_use || gui.starting); 10870 # ifdef FEAT_GUI_W32 10871 else if (STRICMP(name, "gui_win32s") == 0) 10872 n = gui_is_win32s(); 10873 # endif 10874 # ifdef FEAT_BROWSE 10875 else if (STRICMP(name, "browse") == 0) 10876 n = gui.in_use; /* gui_mch_browse() works when GUI is running */ 10877 # endif 10878 #endif 10879 #ifdef FEAT_SYN_HL 10880 else if (STRICMP(name, "syntax_items") == 0) 10881 n = syntax_present(curbuf); 10882 #endif 10883 #if defined(WIN3264) 10884 else if (STRICMP(name, "win95") == 0) 10885 n = mch_windows95(); 10886 #endif 10887 #ifdef FEAT_NETBEANS_INTG 10888 else if (STRICMP(name, "netbeans_enabled") == 0) 10889 n = usingNetbeans; 10890 #endif 10891 } 10892 10893 rettv->vval.v_number = n; 10894 } 10895 10896 /* 10897 * "has_key()" function 10898 */ 10899 static void 10900 f_has_key(argvars, rettv) 10901 typval_T *argvars; 10902 typval_T *rettv; 10903 { 10904 rettv->vval.v_number = 0; 10905 if (argvars[0].v_type != VAR_DICT) 10906 { 10907 EMSG(_(e_dictreq)); 10908 return; 10909 } 10910 if (argvars[0].vval.v_dict == NULL) 10911 return; 10912 10913 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict, 10914 get_tv_string(&argvars[1]), -1) != NULL; 10915 } 10916 10917 /* 10918 * "hasmapto()" function 10919 */ 10920 static void 10921 f_hasmapto(argvars, rettv) 10922 typval_T *argvars; 10923 typval_T *rettv; 10924 { 10925 char_u *name; 10926 char_u *mode; 10927 char_u buf[NUMBUFLEN]; 10928 int abbr = FALSE; 10929 10930 name = get_tv_string(&argvars[0]); 10931 if (argvars[1].v_type == VAR_UNKNOWN) 10932 mode = (char_u *)"nvo"; 10933 else 10934 { 10935 mode = get_tv_string_buf(&argvars[1], buf); 10936 if (argvars[2].v_type != VAR_UNKNOWN) 10937 abbr = get_tv_number(&argvars[2]); 10938 } 10939 10940 if (map_to_exists(name, mode, abbr)) 10941 rettv->vval.v_number = TRUE; 10942 else 10943 rettv->vval.v_number = FALSE; 10944 } 10945 10946 /* 10947 * "histadd()" function 10948 */ 10949 /*ARGSUSED*/ 10950 static void 10951 f_histadd(argvars, rettv) 10952 typval_T *argvars; 10953 typval_T *rettv; 10954 { 10955 #ifdef FEAT_CMDHIST 10956 int histype; 10957 char_u *str; 10958 char_u buf[NUMBUFLEN]; 10959 #endif 10960 10961 rettv->vval.v_number = FALSE; 10962 if (check_restricted() || check_secure()) 10963 return; 10964 #ifdef FEAT_CMDHIST 10965 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ 10966 histype = str != NULL ? get_histtype(str) : -1; 10967 if (histype >= 0) 10968 { 10969 str = get_tv_string_buf(&argvars[1], buf); 10970 if (*str != NUL) 10971 { 10972 add_to_history(histype, str, FALSE, NUL); 10973 rettv->vval.v_number = TRUE; 10974 return; 10975 } 10976 } 10977 #endif 10978 } 10979 10980 /* 10981 * "histdel()" function 10982 */ 10983 /*ARGSUSED*/ 10984 static void 10985 f_histdel(argvars, rettv) 10986 typval_T *argvars; 10987 typval_T *rettv; 10988 { 10989 #ifdef FEAT_CMDHIST 10990 int n; 10991 char_u buf[NUMBUFLEN]; 10992 char_u *str; 10993 10994 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ 10995 if (str == NULL) 10996 n = 0; 10997 else if (argvars[1].v_type == VAR_UNKNOWN) 10998 /* only one argument: clear entire history */ 10999 n = clr_history(get_histtype(str)); 11000 else if (argvars[1].v_type == VAR_NUMBER) 11001 /* index given: remove that entry */ 11002 n = del_history_idx(get_histtype(str), 11003 (int)get_tv_number(&argvars[1])); 11004 else 11005 /* string given: remove all matching entries */ 11006 n = del_history_entry(get_histtype(str), 11007 get_tv_string_buf(&argvars[1], buf)); 11008 rettv->vval.v_number = n; 11009 #else 11010 rettv->vval.v_number = 0; 11011 #endif 11012 } 11013 11014 /* 11015 * "histget()" function 11016 */ 11017 /*ARGSUSED*/ 11018 static void 11019 f_histget(argvars, rettv) 11020 typval_T *argvars; 11021 typval_T *rettv; 11022 { 11023 #ifdef FEAT_CMDHIST 11024 int type; 11025 int idx; 11026 char_u *str; 11027 11028 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ 11029 if (str == NULL) 11030 rettv->vval.v_string = NULL; 11031 else 11032 { 11033 type = get_histtype(str); 11034 if (argvars[1].v_type == VAR_UNKNOWN) 11035 idx = get_history_idx(type); 11036 else 11037 idx = (int)get_tv_number_chk(&argvars[1], NULL); 11038 /* -1 on type error */ 11039 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx)); 11040 } 11041 #else 11042 rettv->vval.v_string = NULL; 11043 #endif 11044 rettv->v_type = VAR_STRING; 11045 } 11046 11047 /* 11048 * "histnr()" function 11049 */ 11050 /*ARGSUSED*/ 11051 static void 11052 f_histnr(argvars, rettv) 11053 typval_T *argvars; 11054 typval_T *rettv; 11055 { 11056 int i; 11057 11058 #ifdef FEAT_CMDHIST 11059 char_u *history = get_tv_string_chk(&argvars[0]); 11060 11061 i = history == NULL ? HIST_CMD - 1 : get_histtype(history); 11062 if (i >= HIST_CMD && i < HIST_COUNT) 11063 i = get_history_idx(i); 11064 else 11065 #endif 11066 i = -1; 11067 rettv->vval.v_number = i; 11068 } 11069 11070 /* 11071 * "highlightID(name)" function 11072 */ 11073 static void 11074 f_hlID(argvars, rettv) 11075 typval_T *argvars; 11076 typval_T *rettv; 11077 { 11078 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0])); 11079 } 11080 11081 /* 11082 * "highlight_exists()" function 11083 */ 11084 static void 11085 f_hlexists(argvars, rettv) 11086 typval_T *argvars; 11087 typval_T *rettv; 11088 { 11089 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0])); 11090 } 11091 11092 /* 11093 * "hostname()" function 11094 */ 11095 /*ARGSUSED*/ 11096 static void 11097 f_hostname(argvars, rettv) 11098 typval_T *argvars; 11099 typval_T *rettv; 11100 { 11101 char_u hostname[256]; 11102 11103 mch_get_host_name(hostname, 256); 11104 rettv->v_type = VAR_STRING; 11105 rettv->vval.v_string = vim_strsave(hostname); 11106 } 11107 11108 /* 11109 * iconv() function 11110 */ 11111 /*ARGSUSED*/ 11112 static void 11113 f_iconv(argvars, rettv) 11114 typval_T *argvars; 11115 typval_T *rettv; 11116 { 11117 #ifdef FEAT_MBYTE 11118 char_u buf1[NUMBUFLEN]; 11119 char_u buf2[NUMBUFLEN]; 11120 char_u *from, *to, *str; 11121 vimconv_T vimconv; 11122 #endif 11123 11124 rettv->v_type = VAR_STRING; 11125 rettv->vval.v_string = NULL; 11126 11127 #ifdef FEAT_MBYTE 11128 str = get_tv_string(&argvars[0]); 11129 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1))); 11130 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2))); 11131 vimconv.vc_type = CONV_NONE; 11132 convert_setup(&vimconv, from, to); 11133 11134 /* If the encodings are equal, no conversion needed. */ 11135 if (vimconv.vc_type == CONV_NONE) 11136 rettv->vval.v_string = vim_strsave(str); 11137 else 11138 rettv->vval.v_string = string_convert(&vimconv, str, NULL); 11139 11140 convert_setup(&vimconv, NULL, NULL); 11141 vim_free(from); 11142 vim_free(to); 11143 #endif 11144 } 11145 11146 /* 11147 * "indent()" function 11148 */ 11149 static void 11150 f_indent(argvars, rettv) 11151 typval_T *argvars; 11152 typval_T *rettv; 11153 { 11154 linenr_T lnum; 11155 11156 lnum = get_tv_lnum(argvars); 11157 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 11158 rettv->vval.v_number = get_indent_lnum(lnum); 11159 else 11160 rettv->vval.v_number = -1; 11161 } 11162 11163 /* 11164 * "index()" function 11165 */ 11166 static void 11167 f_index(argvars, rettv) 11168 typval_T *argvars; 11169 typval_T *rettv; 11170 { 11171 list_T *l; 11172 listitem_T *item; 11173 long idx = 0; 11174 int ic = FALSE; 11175 11176 rettv->vval.v_number = -1; 11177 if (argvars[0].v_type != VAR_LIST) 11178 { 11179 EMSG(_(e_listreq)); 11180 return; 11181 } 11182 l = argvars[0].vval.v_list; 11183 if (l != NULL) 11184 { 11185 item = l->lv_first; 11186 if (argvars[2].v_type != VAR_UNKNOWN) 11187 { 11188 int error = FALSE; 11189 11190 /* Start at specified item. Use the cached index that list_find() 11191 * sets, so that a negative number also works. */ 11192 item = list_find(l, get_tv_number_chk(&argvars[2], &error)); 11193 idx = l->lv_idx; 11194 if (argvars[3].v_type != VAR_UNKNOWN) 11195 ic = get_tv_number_chk(&argvars[3], &error); 11196 if (error) 11197 item = NULL; 11198 } 11199 11200 for ( ; item != NULL; item = item->li_next, ++idx) 11201 if (tv_equal(&item->li_tv, &argvars[1], ic)) 11202 { 11203 rettv->vval.v_number = idx; 11204 break; 11205 } 11206 } 11207 } 11208 11209 static int inputsecret_flag = 0; 11210 11211 /* 11212 * "input()" function 11213 * Also handles inputsecret() when inputsecret is set. 11214 */ 11215 static void 11216 f_input(argvars, rettv) 11217 typval_T *argvars; 11218 typval_T *rettv; 11219 { 11220 char_u *prompt = get_tv_string_chk(&argvars[0]); 11221 char_u *p = NULL; 11222 int c; 11223 char_u buf[NUMBUFLEN]; 11224 int cmd_silent_save = cmd_silent; 11225 char_u *defstr = (char_u *)""; 11226 int xp_type = EXPAND_NOTHING; 11227 char_u *xp_arg = NULL; 11228 11229 rettv->v_type = VAR_STRING; 11230 11231 #ifdef NO_CONSOLE_INPUT 11232 /* While starting up, there is no place to enter text. */ 11233 if (no_console_input()) 11234 { 11235 rettv->vval.v_string = NULL; 11236 return; 11237 } 11238 #endif 11239 11240 cmd_silent = FALSE; /* Want to see the prompt. */ 11241 if (prompt != NULL) 11242 { 11243 /* Only the part of the message after the last NL is considered as 11244 * prompt for the command line */ 11245 p = vim_strrchr(prompt, '\n'); 11246 if (p == NULL) 11247 p = prompt; 11248 else 11249 { 11250 ++p; 11251 c = *p; 11252 *p = NUL; 11253 msg_start(); 11254 msg_clr_eos(); 11255 msg_puts_attr(prompt, echo_attr); 11256 msg_didout = FALSE; 11257 msg_starthere(); 11258 *p = c; 11259 } 11260 cmdline_row = msg_row; 11261 11262 if (argvars[1].v_type != VAR_UNKNOWN) 11263 { 11264 defstr = get_tv_string_buf_chk(&argvars[1], buf); 11265 if (defstr != NULL) 11266 stuffReadbuffSpec(defstr); 11267 11268 if (argvars[2].v_type != VAR_UNKNOWN) 11269 { 11270 char_u *xp_name; 11271 int xp_namelen; 11272 long argt; 11273 11274 rettv->vval.v_string = NULL; 11275 11276 xp_name = get_tv_string_buf_chk(&argvars[2], buf); 11277 if (xp_name == NULL) 11278 return; 11279 11280 xp_namelen = STRLEN(xp_name); 11281 11282 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt, 11283 &xp_arg) == FAIL) 11284 return; 11285 } 11286 } 11287 11288 if (defstr != NULL) 11289 rettv->vval.v_string = 11290 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr, 11291 xp_type, xp_arg); 11292 11293 vim_free(xp_arg); 11294 11295 /* since the user typed this, no need to wait for return */ 11296 need_wait_return = FALSE; 11297 msg_didout = FALSE; 11298 } 11299 cmd_silent = cmd_silent_save; 11300 } 11301 11302 /* 11303 * "inputdialog()" function 11304 */ 11305 static void 11306 f_inputdialog(argvars, rettv) 11307 typval_T *argvars; 11308 typval_T *rettv; 11309 { 11310 #if defined(FEAT_GUI_TEXTDIALOG) 11311 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */ 11312 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL) 11313 { 11314 char_u *message; 11315 char_u buf[NUMBUFLEN]; 11316 char_u *defstr = (char_u *)""; 11317 11318 message = get_tv_string_chk(&argvars[0]); 11319 if (argvars[1].v_type != VAR_UNKNOWN 11320 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL) 11321 vim_strncpy(IObuff, defstr, IOSIZE - 1); 11322 else 11323 IObuff[0] = NUL; 11324 if (message != NULL && defstr != NULL 11325 && do_dialog(VIM_QUESTION, NULL, message, 11326 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1) 11327 rettv->vval.v_string = vim_strsave(IObuff); 11328 else 11329 { 11330 if (message != NULL && defstr != NULL 11331 && argvars[1].v_type != VAR_UNKNOWN 11332 && argvars[2].v_type != VAR_UNKNOWN) 11333 rettv->vval.v_string = vim_strsave( 11334 get_tv_string_buf(&argvars[2], buf)); 11335 else 11336 rettv->vval.v_string = NULL; 11337 } 11338 rettv->v_type = VAR_STRING; 11339 } 11340 else 11341 #endif 11342 f_input(argvars, rettv); 11343 } 11344 11345 /* 11346 * "inputlist()" function 11347 */ 11348 static void 11349 f_inputlist(argvars, rettv) 11350 typval_T *argvars; 11351 typval_T *rettv; 11352 { 11353 listitem_T *li; 11354 int selected; 11355 int mouse_used; 11356 11357 rettv->vval.v_number = 0; 11358 #ifdef NO_CONSOLE_INPUT 11359 /* While starting up, there is no place to enter text. */ 11360 if (no_console_input()) 11361 return; 11362 #endif 11363 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL) 11364 { 11365 EMSG2(_(e_listarg), "inputlist()"); 11366 return; 11367 } 11368 11369 msg_start(); 11370 lines_left = Rows; /* avoid more prompt */ 11371 msg_scroll = TRUE; 11372 msg_clr_eos(); 11373 11374 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next) 11375 { 11376 msg_puts(get_tv_string(&li->li_tv)); 11377 msg_putchar('\n'); 11378 } 11379 11380 /* Ask for choice. */ 11381 selected = prompt_for_number(&mouse_used); 11382 if (mouse_used) 11383 selected -= lines_left; 11384 11385 rettv->vval.v_number = selected; 11386 } 11387 11388 11389 static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL}; 11390 11391 /* 11392 * "inputrestore()" function 11393 */ 11394 /*ARGSUSED*/ 11395 static void 11396 f_inputrestore(argvars, rettv) 11397 typval_T *argvars; 11398 typval_T *rettv; 11399 { 11400 if (ga_userinput.ga_len > 0) 11401 { 11402 --ga_userinput.ga_len; 11403 restore_typeahead((tasave_T *)(ga_userinput.ga_data) 11404 + ga_userinput.ga_len); 11405 rettv->vval.v_number = 0; /* OK */ 11406 } 11407 else if (p_verbose > 1) 11408 { 11409 verb_msg((char_u *)_("called inputrestore() more often than inputsave()")); 11410 rettv->vval.v_number = 1; /* Failed */ 11411 } 11412 } 11413 11414 /* 11415 * "inputsave()" function 11416 */ 11417 /*ARGSUSED*/ 11418 static void 11419 f_inputsave(argvars, rettv) 11420 typval_T *argvars; 11421 typval_T *rettv; 11422 { 11423 /* Add an entry to the stack of typehead storage. */ 11424 if (ga_grow(&ga_userinput, 1) == OK) 11425 { 11426 save_typeahead((tasave_T *)(ga_userinput.ga_data) 11427 + ga_userinput.ga_len); 11428 ++ga_userinput.ga_len; 11429 rettv->vval.v_number = 0; /* OK */ 11430 } 11431 else 11432 rettv->vval.v_number = 1; /* Failed */ 11433 } 11434 11435 /* 11436 * "inputsecret()" function 11437 */ 11438 static void 11439 f_inputsecret(argvars, rettv) 11440 typval_T *argvars; 11441 typval_T *rettv; 11442 { 11443 ++cmdline_star; 11444 ++inputsecret_flag; 11445 f_input(argvars, rettv); 11446 --cmdline_star; 11447 --inputsecret_flag; 11448 } 11449 11450 /* 11451 * "insert()" function 11452 */ 11453 static void 11454 f_insert(argvars, rettv) 11455 typval_T *argvars; 11456 typval_T *rettv; 11457 { 11458 long before = 0; 11459 listitem_T *item; 11460 list_T *l; 11461 int error = FALSE; 11462 11463 rettv->vval.v_number = 0; 11464 if (argvars[0].v_type != VAR_LIST) 11465 EMSG2(_(e_listarg), "insert()"); 11466 else if ((l = argvars[0].vval.v_list) != NULL 11467 && !tv_check_lock(l->lv_lock, (char_u *)"insert()")) 11468 { 11469 if (argvars[2].v_type != VAR_UNKNOWN) 11470 before = get_tv_number_chk(&argvars[2], &error); 11471 if (error) 11472 return; /* type error; errmsg already given */ 11473 11474 if (before == l->lv_len) 11475 item = NULL; 11476 else 11477 { 11478 item = list_find(l, before); 11479 if (item == NULL) 11480 { 11481 EMSGN(_(e_listidx), before); 11482 l = NULL; 11483 } 11484 } 11485 if (l != NULL) 11486 { 11487 list_insert_tv(l, &argvars[1], item); 11488 copy_tv(&argvars[0], rettv); 11489 } 11490 } 11491 } 11492 11493 /* 11494 * "isdirectory()" function 11495 */ 11496 static void 11497 f_isdirectory(argvars, rettv) 11498 typval_T *argvars; 11499 typval_T *rettv; 11500 { 11501 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0])); 11502 } 11503 11504 /* 11505 * "islocked()" function 11506 */ 11507 static void 11508 f_islocked(argvars, rettv) 11509 typval_T *argvars; 11510 typval_T *rettv; 11511 { 11512 lval_T lv; 11513 char_u *end; 11514 dictitem_T *di; 11515 11516 rettv->vval.v_number = -1; 11517 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE, 11518 FNE_CHECK_START); 11519 if (end != NULL && lv.ll_name != NULL) 11520 { 11521 if (*end != NUL) 11522 EMSG(_(e_trailing)); 11523 else 11524 { 11525 if (lv.ll_tv == NULL) 11526 { 11527 if (check_changedtick(lv.ll_name)) 11528 rettv->vval.v_number = 1; /* always locked */ 11529 else 11530 { 11531 di = find_var(lv.ll_name, NULL); 11532 if (di != NULL) 11533 { 11534 /* Consider a variable locked when: 11535 * 1. the variable itself is locked 11536 * 2. the value of the variable is locked. 11537 * 3. the List or Dict value is locked. 11538 */ 11539 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK) 11540 || tv_islocked(&di->di_tv)); 11541 } 11542 } 11543 } 11544 else if (lv.ll_range) 11545 EMSG(_("E786: Range not allowed")); 11546 else if (lv.ll_newkey != NULL) 11547 EMSG2(_(e_dictkey), lv.ll_newkey); 11548 else if (lv.ll_list != NULL) 11549 /* List item. */ 11550 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv); 11551 else 11552 /* Dictionary item. */ 11553 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv); 11554 } 11555 } 11556 11557 clear_lval(&lv); 11558 } 11559 11560 static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what)); 11561 11562 /* 11563 * Turn a dict into a list: 11564 * "what" == 0: list of keys 11565 * "what" == 1: list of values 11566 * "what" == 2: list of items 11567 */ 11568 static void 11569 dict_list(argvars, rettv, what) 11570 typval_T *argvars; 11571 typval_T *rettv; 11572 int what; 11573 { 11574 list_T *l2; 11575 dictitem_T *di; 11576 hashitem_T *hi; 11577 listitem_T *li; 11578 listitem_T *li2; 11579 dict_T *d; 11580 int todo; 11581 11582 rettv->vval.v_number = 0; 11583 if (argvars[0].v_type != VAR_DICT) 11584 { 11585 EMSG(_(e_dictreq)); 11586 return; 11587 } 11588 if ((d = argvars[0].vval.v_dict) == NULL) 11589 return; 11590 11591 if (rettv_list_alloc(rettv) == FAIL) 11592 return; 11593 11594 todo = d->dv_hashtab.ht_used; 11595 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 11596 { 11597 if (!HASHITEM_EMPTY(hi)) 11598 { 11599 --todo; 11600 di = HI2DI(hi); 11601 11602 li = listitem_alloc(); 11603 if (li == NULL) 11604 break; 11605 list_append(rettv->vval.v_list, li); 11606 11607 if (what == 0) 11608 { 11609 /* keys() */ 11610 li->li_tv.v_type = VAR_STRING; 11611 li->li_tv.v_lock = 0; 11612 li->li_tv.vval.v_string = vim_strsave(di->di_key); 11613 } 11614 else if (what == 1) 11615 { 11616 /* values() */ 11617 copy_tv(&di->di_tv, &li->li_tv); 11618 } 11619 else 11620 { 11621 /* items() */ 11622 l2 = list_alloc(); 11623 li->li_tv.v_type = VAR_LIST; 11624 li->li_tv.v_lock = 0; 11625 li->li_tv.vval.v_list = l2; 11626 if (l2 == NULL) 11627 break; 11628 ++l2->lv_refcount; 11629 11630 li2 = listitem_alloc(); 11631 if (li2 == NULL) 11632 break; 11633 list_append(l2, li2); 11634 li2->li_tv.v_type = VAR_STRING; 11635 li2->li_tv.v_lock = 0; 11636 li2->li_tv.vval.v_string = vim_strsave(di->di_key); 11637 11638 li2 = listitem_alloc(); 11639 if (li2 == NULL) 11640 break; 11641 list_append(l2, li2); 11642 copy_tv(&di->di_tv, &li2->li_tv); 11643 } 11644 } 11645 } 11646 } 11647 11648 /* 11649 * "items(dict)" function 11650 */ 11651 static void 11652 f_items(argvars, rettv) 11653 typval_T *argvars; 11654 typval_T *rettv; 11655 { 11656 dict_list(argvars, rettv, 2); 11657 } 11658 11659 /* 11660 * "join()" function 11661 */ 11662 static void 11663 f_join(argvars, rettv) 11664 typval_T *argvars; 11665 typval_T *rettv; 11666 { 11667 garray_T ga; 11668 char_u *sep; 11669 11670 rettv->vval.v_number = 0; 11671 if (argvars[0].v_type != VAR_LIST) 11672 { 11673 EMSG(_(e_listreq)); 11674 return; 11675 } 11676 if (argvars[0].vval.v_list == NULL) 11677 return; 11678 if (argvars[1].v_type == VAR_UNKNOWN) 11679 sep = (char_u *)" "; 11680 else 11681 sep = get_tv_string_chk(&argvars[1]); 11682 11683 rettv->v_type = VAR_STRING; 11684 11685 if (sep != NULL) 11686 { 11687 ga_init2(&ga, (int)sizeof(char), 80); 11688 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0); 11689 ga_append(&ga, NUL); 11690 rettv->vval.v_string = (char_u *)ga.ga_data; 11691 } 11692 else 11693 rettv->vval.v_string = NULL; 11694 } 11695 11696 /* 11697 * "keys()" function 11698 */ 11699 static void 11700 f_keys(argvars, rettv) 11701 typval_T *argvars; 11702 typval_T *rettv; 11703 { 11704 dict_list(argvars, rettv, 0); 11705 } 11706 11707 /* 11708 * "last_buffer_nr()" function. 11709 */ 11710 /*ARGSUSED*/ 11711 static void 11712 f_last_buffer_nr(argvars, rettv) 11713 typval_T *argvars; 11714 typval_T *rettv; 11715 { 11716 int n = 0; 11717 buf_T *buf; 11718 11719 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 11720 if (n < buf->b_fnum) 11721 n = buf->b_fnum; 11722 11723 rettv->vval.v_number = n; 11724 } 11725 11726 /* 11727 * "len()" function 11728 */ 11729 static void 11730 f_len(argvars, rettv) 11731 typval_T *argvars; 11732 typval_T *rettv; 11733 { 11734 switch (argvars[0].v_type) 11735 { 11736 case VAR_STRING: 11737 case VAR_NUMBER: 11738 rettv->vval.v_number = (varnumber_T)STRLEN( 11739 get_tv_string(&argvars[0])); 11740 break; 11741 case VAR_LIST: 11742 rettv->vval.v_number = list_len(argvars[0].vval.v_list); 11743 break; 11744 case VAR_DICT: 11745 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict); 11746 break; 11747 default: 11748 EMSG(_("E701: Invalid type for len()")); 11749 break; 11750 } 11751 } 11752 11753 static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type)); 11754 11755 static void 11756 libcall_common(argvars, rettv, type) 11757 typval_T *argvars; 11758 typval_T *rettv; 11759 int type; 11760 { 11761 #ifdef FEAT_LIBCALL 11762 char_u *string_in; 11763 char_u **string_result; 11764 int nr_result; 11765 #endif 11766 11767 rettv->v_type = type; 11768 if (type == VAR_NUMBER) 11769 rettv->vval.v_number = 0; 11770 else 11771 rettv->vval.v_string = NULL; 11772 11773 if (check_restricted() || check_secure()) 11774 return; 11775 11776 #ifdef FEAT_LIBCALL 11777 /* The first two args must be strings, otherwise its meaningless */ 11778 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING) 11779 { 11780 string_in = NULL; 11781 if (argvars[2].v_type == VAR_STRING) 11782 string_in = argvars[2].vval.v_string; 11783 if (type == VAR_NUMBER) 11784 string_result = NULL; 11785 else 11786 string_result = &rettv->vval.v_string; 11787 if (mch_libcall(argvars[0].vval.v_string, 11788 argvars[1].vval.v_string, 11789 string_in, 11790 argvars[2].vval.v_number, 11791 string_result, 11792 &nr_result) == OK 11793 && type == VAR_NUMBER) 11794 rettv->vval.v_number = nr_result; 11795 } 11796 #endif 11797 } 11798 11799 /* 11800 * "libcall()" function 11801 */ 11802 static void 11803 f_libcall(argvars, rettv) 11804 typval_T *argvars; 11805 typval_T *rettv; 11806 { 11807 libcall_common(argvars, rettv, VAR_STRING); 11808 } 11809 11810 /* 11811 * "libcallnr()" function 11812 */ 11813 static void 11814 f_libcallnr(argvars, rettv) 11815 typval_T *argvars; 11816 typval_T *rettv; 11817 { 11818 libcall_common(argvars, rettv, VAR_NUMBER); 11819 } 11820 11821 /* 11822 * "line(string)" function 11823 */ 11824 static void 11825 f_line(argvars, rettv) 11826 typval_T *argvars; 11827 typval_T *rettv; 11828 { 11829 linenr_T lnum = 0; 11830 pos_T *fp; 11831 int fnum; 11832 11833 fp = var2fpos(&argvars[0], TRUE, &fnum); 11834 if (fp != NULL) 11835 lnum = fp->lnum; 11836 rettv->vval.v_number = lnum; 11837 } 11838 11839 /* 11840 * "line2byte(lnum)" function 11841 */ 11842 /*ARGSUSED*/ 11843 static void 11844 f_line2byte(argvars, rettv) 11845 typval_T *argvars; 11846 typval_T *rettv; 11847 { 11848 #ifndef FEAT_BYTEOFF 11849 rettv->vval.v_number = -1; 11850 #else 11851 linenr_T lnum; 11852 11853 lnum = get_tv_lnum(argvars); 11854 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) 11855 rettv->vval.v_number = -1; 11856 else 11857 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL); 11858 if (rettv->vval.v_number >= 0) 11859 ++rettv->vval.v_number; 11860 #endif 11861 } 11862 11863 /* 11864 * "lispindent(lnum)" function 11865 */ 11866 static void 11867 f_lispindent(argvars, rettv) 11868 typval_T *argvars; 11869 typval_T *rettv; 11870 { 11871 #ifdef FEAT_LISP 11872 pos_T pos; 11873 linenr_T lnum; 11874 11875 pos = curwin->w_cursor; 11876 lnum = get_tv_lnum(argvars); 11877 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 11878 { 11879 curwin->w_cursor.lnum = lnum; 11880 rettv->vval.v_number = get_lisp_indent(); 11881 curwin->w_cursor = pos; 11882 } 11883 else 11884 #endif 11885 rettv->vval.v_number = -1; 11886 } 11887 11888 /* 11889 * "localtime()" function 11890 */ 11891 /*ARGSUSED*/ 11892 static void 11893 f_localtime(argvars, rettv) 11894 typval_T *argvars; 11895 typval_T *rettv; 11896 { 11897 rettv->vval.v_number = (varnumber_T)time(NULL); 11898 } 11899 11900 static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact)); 11901 11902 static void 11903 get_maparg(argvars, rettv, exact) 11904 typval_T *argvars; 11905 typval_T *rettv; 11906 int exact; 11907 { 11908 char_u *keys; 11909 char_u *which; 11910 char_u buf[NUMBUFLEN]; 11911 char_u *keys_buf = NULL; 11912 char_u *rhs; 11913 int mode; 11914 garray_T ga; 11915 int abbr = FALSE; 11916 11917 /* return empty string for failure */ 11918 rettv->v_type = VAR_STRING; 11919 rettv->vval.v_string = NULL; 11920 11921 keys = get_tv_string(&argvars[0]); 11922 if (*keys == NUL) 11923 return; 11924 11925 if (argvars[1].v_type != VAR_UNKNOWN) 11926 { 11927 which = get_tv_string_buf_chk(&argvars[1], buf); 11928 if (argvars[2].v_type != VAR_UNKNOWN) 11929 abbr = get_tv_number(&argvars[2]); 11930 } 11931 else 11932 which = (char_u *)""; 11933 if (which == NULL) 11934 return; 11935 11936 mode = get_map_mode(&which, 0); 11937 11938 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE); 11939 rhs = check_map(keys, mode, exact, FALSE, abbr); 11940 vim_free(keys_buf); 11941 if (rhs != NULL) 11942 { 11943 ga_init(&ga); 11944 ga.ga_itemsize = 1; 11945 ga.ga_growsize = 40; 11946 11947 while (*rhs != NUL) 11948 ga_concat(&ga, str2special(&rhs, FALSE)); 11949 11950 ga_append(&ga, NUL); 11951 rettv->vval.v_string = (char_u *)ga.ga_data; 11952 } 11953 } 11954 11955 /* 11956 * "map()" function 11957 */ 11958 static void 11959 f_map(argvars, rettv) 11960 typval_T *argvars; 11961 typval_T *rettv; 11962 { 11963 filter_map(argvars, rettv, TRUE); 11964 } 11965 11966 /* 11967 * "maparg()" function 11968 */ 11969 static void 11970 f_maparg(argvars, rettv) 11971 typval_T *argvars; 11972 typval_T *rettv; 11973 { 11974 get_maparg(argvars, rettv, TRUE); 11975 } 11976 11977 /* 11978 * "mapcheck()" function 11979 */ 11980 static void 11981 f_mapcheck(argvars, rettv) 11982 typval_T *argvars; 11983 typval_T *rettv; 11984 { 11985 get_maparg(argvars, rettv, FALSE); 11986 } 11987 11988 static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start)); 11989 11990 static void 11991 find_some_match(argvars, rettv, type) 11992 typval_T *argvars; 11993 typval_T *rettv; 11994 int type; 11995 { 11996 char_u *str = NULL; 11997 char_u *expr = NULL; 11998 char_u *pat; 11999 regmatch_T regmatch; 12000 char_u patbuf[NUMBUFLEN]; 12001 char_u strbuf[NUMBUFLEN]; 12002 char_u *save_cpo; 12003 long start = 0; 12004 long nth = 1; 12005 colnr_T startcol = 0; 12006 int match = 0; 12007 list_T *l = NULL; 12008 listitem_T *li = NULL; 12009 long idx = 0; 12010 char_u *tofree = NULL; 12011 12012 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 12013 save_cpo = p_cpo; 12014 p_cpo = (char_u *)""; 12015 12016 rettv->vval.v_number = -1; 12017 if (type == 3) 12018 { 12019 /* return empty list when there are no matches */ 12020 if (rettv_list_alloc(rettv) == FAIL) 12021 goto theend; 12022 } 12023 else if (type == 2) 12024 { 12025 rettv->v_type = VAR_STRING; 12026 rettv->vval.v_string = NULL; 12027 } 12028 12029 if (argvars[0].v_type == VAR_LIST) 12030 { 12031 if ((l = argvars[0].vval.v_list) == NULL) 12032 goto theend; 12033 li = l->lv_first; 12034 } 12035 else 12036 expr = str = get_tv_string(&argvars[0]); 12037 12038 pat = get_tv_string_buf_chk(&argvars[1], patbuf); 12039 if (pat == NULL) 12040 goto theend; 12041 12042 if (argvars[2].v_type != VAR_UNKNOWN) 12043 { 12044 int error = FALSE; 12045 12046 start = get_tv_number_chk(&argvars[2], &error); 12047 if (error) 12048 goto theend; 12049 if (l != NULL) 12050 { 12051 li = list_find(l, start); 12052 if (li == NULL) 12053 goto theend; 12054 idx = l->lv_idx; /* use the cached index */ 12055 } 12056 else 12057 { 12058 if (start < 0) 12059 start = 0; 12060 if (start > (long)STRLEN(str)) 12061 goto theend; 12062 /* When "count" argument is there ignore matches before "start", 12063 * otherwise skip part of the string. Differs when pattern is "^" 12064 * or "\<". */ 12065 if (argvars[3].v_type != VAR_UNKNOWN) 12066 startcol = start; 12067 else 12068 str += start; 12069 } 12070 12071 if (argvars[3].v_type != VAR_UNKNOWN) 12072 nth = get_tv_number_chk(&argvars[3], &error); 12073 if (error) 12074 goto theend; 12075 } 12076 12077 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 12078 if (regmatch.regprog != NULL) 12079 { 12080 regmatch.rm_ic = p_ic; 12081 12082 for (;;) 12083 { 12084 if (l != NULL) 12085 { 12086 if (li == NULL) 12087 { 12088 match = FALSE; 12089 break; 12090 } 12091 vim_free(tofree); 12092 str = echo_string(&li->li_tv, &tofree, strbuf, 0); 12093 if (str == NULL) 12094 break; 12095 } 12096 12097 match = vim_regexec_nl(®match, str, (colnr_T)startcol); 12098 12099 if (match && --nth <= 0) 12100 break; 12101 if (l == NULL && !match) 12102 break; 12103 12104 /* Advance to just after the match. */ 12105 if (l != NULL) 12106 { 12107 li = li->li_next; 12108 ++idx; 12109 } 12110 else 12111 { 12112 #ifdef FEAT_MBYTE 12113 startcol = regmatch.startp[0] 12114 + (*mb_ptr2len)(regmatch.startp[0]) - str; 12115 #else 12116 startcol = regmatch.startp[0] + 1 - str; 12117 #endif 12118 } 12119 } 12120 12121 if (match) 12122 { 12123 if (type == 3) 12124 { 12125 int i; 12126 12127 /* return list with matched string and submatches */ 12128 for (i = 0; i < NSUBEXP; ++i) 12129 { 12130 if (regmatch.endp[i] == NULL) 12131 break; 12132 if (list_append_string(rettv->vval.v_list, 12133 regmatch.startp[i], 12134 (int)(regmatch.endp[i] - regmatch.startp[i])) 12135 == FAIL) 12136 break; 12137 } 12138 } 12139 else if (type == 2) 12140 { 12141 /* return matched string */ 12142 if (l != NULL) 12143 copy_tv(&li->li_tv, rettv); 12144 else 12145 rettv->vval.v_string = vim_strnsave(regmatch.startp[0], 12146 (int)(regmatch.endp[0] - regmatch.startp[0])); 12147 } 12148 else if (l != NULL) 12149 rettv->vval.v_number = idx; 12150 else 12151 { 12152 if (type != 0) 12153 rettv->vval.v_number = 12154 (varnumber_T)(regmatch.startp[0] - str); 12155 else 12156 rettv->vval.v_number = 12157 (varnumber_T)(regmatch.endp[0] - str); 12158 rettv->vval.v_number += str - expr; 12159 } 12160 } 12161 vim_free(regmatch.regprog); 12162 } 12163 12164 theend: 12165 vim_free(tofree); 12166 p_cpo = save_cpo; 12167 } 12168 12169 /* 12170 * "match()" function 12171 */ 12172 static void 12173 f_match(argvars, rettv) 12174 typval_T *argvars; 12175 typval_T *rettv; 12176 { 12177 find_some_match(argvars, rettv, 1); 12178 } 12179 12180 /* 12181 * "matcharg()" function 12182 */ 12183 static void 12184 f_matcharg(argvars, rettv) 12185 typval_T *argvars; 12186 typval_T *rettv; 12187 { 12188 if (rettv_list_alloc(rettv) == OK) 12189 { 12190 #ifdef FEAT_SEARCH_EXTRA 12191 int mi = get_tv_number(&argvars[0]); 12192 12193 if (mi >= 1 && mi <= 3) 12194 { 12195 list_append_string(rettv->vval.v_list, 12196 syn_id2name(curwin->w_match_id[mi - 1]), -1); 12197 list_append_string(rettv->vval.v_list, 12198 curwin->w_match_pat[mi - 1], -1); 12199 } 12200 #endif 12201 } 12202 } 12203 12204 /* 12205 * "matchend()" function 12206 */ 12207 static void 12208 f_matchend(argvars, rettv) 12209 typval_T *argvars; 12210 typval_T *rettv; 12211 { 12212 find_some_match(argvars, rettv, 0); 12213 } 12214 12215 /* 12216 * "matchlist()" function 12217 */ 12218 static void 12219 f_matchlist(argvars, rettv) 12220 typval_T *argvars; 12221 typval_T *rettv; 12222 { 12223 find_some_match(argvars, rettv, 3); 12224 } 12225 12226 /* 12227 * "matchstr()" function 12228 */ 12229 static void 12230 f_matchstr(argvars, rettv) 12231 typval_T *argvars; 12232 typval_T *rettv; 12233 { 12234 find_some_match(argvars, rettv, 2); 12235 } 12236 12237 static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax)); 12238 12239 static void 12240 max_min(argvars, rettv, domax) 12241 typval_T *argvars; 12242 typval_T *rettv; 12243 int domax; 12244 { 12245 long n = 0; 12246 long i; 12247 int error = FALSE; 12248 12249 if (argvars[0].v_type == VAR_LIST) 12250 { 12251 list_T *l; 12252 listitem_T *li; 12253 12254 l = argvars[0].vval.v_list; 12255 if (l != NULL) 12256 { 12257 li = l->lv_first; 12258 if (li != NULL) 12259 { 12260 n = get_tv_number_chk(&li->li_tv, &error); 12261 for (;;) 12262 { 12263 li = li->li_next; 12264 if (li == NULL) 12265 break; 12266 i = get_tv_number_chk(&li->li_tv, &error); 12267 if (domax ? i > n : i < n) 12268 n = i; 12269 } 12270 } 12271 } 12272 } 12273 else if (argvars[0].v_type == VAR_DICT) 12274 { 12275 dict_T *d; 12276 int first = TRUE; 12277 hashitem_T *hi; 12278 int todo; 12279 12280 d = argvars[0].vval.v_dict; 12281 if (d != NULL) 12282 { 12283 todo = d->dv_hashtab.ht_used; 12284 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 12285 { 12286 if (!HASHITEM_EMPTY(hi)) 12287 { 12288 --todo; 12289 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error); 12290 if (first) 12291 { 12292 n = i; 12293 first = FALSE; 12294 } 12295 else if (domax ? i > n : i < n) 12296 n = i; 12297 } 12298 } 12299 } 12300 } 12301 else 12302 EMSG(_(e_listdictarg)); 12303 rettv->vval.v_number = error ? 0 : n; 12304 } 12305 12306 /* 12307 * "max()" function 12308 */ 12309 static void 12310 f_max(argvars, rettv) 12311 typval_T *argvars; 12312 typval_T *rettv; 12313 { 12314 max_min(argvars, rettv, TRUE); 12315 } 12316 12317 /* 12318 * "min()" function 12319 */ 12320 static void 12321 f_min(argvars, rettv) 12322 typval_T *argvars; 12323 typval_T *rettv; 12324 { 12325 max_min(argvars, rettv, FALSE); 12326 } 12327 12328 static int mkdir_recurse __ARGS((char_u *dir, int prot)); 12329 12330 /* 12331 * Create the directory in which "dir" is located, and higher levels when 12332 * needed. 12333 */ 12334 static int 12335 mkdir_recurse(dir, prot) 12336 char_u *dir; 12337 int prot; 12338 { 12339 char_u *p; 12340 char_u *updir; 12341 int r = FAIL; 12342 12343 /* Get end of directory name in "dir". 12344 * We're done when it's "/" or "c:/". */ 12345 p = gettail_sep(dir); 12346 if (p <= get_past_head(dir)) 12347 return OK; 12348 12349 /* If the directory exists we're done. Otherwise: create it.*/ 12350 updir = vim_strnsave(dir, (int)(p - dir)); 12351 if (updir == NULL) 12352 return FAIL; 12353 if (mch_isdir(updir)) 12354 r = OK; 12355 else if (mkdir_recurse(updir, prot) == OK) 12356 r = vim_mkdir_emsg(updir, prot); 12357 vim_free(updir); 12358 return r; 12359 } 12360 12361 #ifdef vim_mkdir 12362 /* 12363 * "mkdir()" function 12364 */ 12365 static void 12366 f_mkdir(argvars, rettv) 12367 typval_T *argvars; 12368 typval_T *rettv; 12369 { 12370 char_u *dir; 12371 char_u buf[NUMBUFLEN]; 12372 int prot = 0755; 12373 12374 rettv->vval.v_number = FAIL; 12375 if (check_restricted() || check_secure()) 12376 return; 12377 12378 dir = get_tv_string_buf(&argvars[0], buf); 12379 if (argvars[1].v_type != VAR_UNKNOWN) 12380 { 12381 if (argvars[2].v_type != VAR_UNKNOWN) 12382 prot = get_tv_number_chk(&argvars[2], NULL); 12383 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0) 12384 mkdir_recurse(dir, prot); 12385 } 12386 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0; 12387 } 12388 #endif 12389 12390 /* 12391 * "mode()" function 12392 */ 12393 /*ARGSUSED*/ 12394 static void 12395 f_mode(argvars, rettv) 12396 typval_T *argvars; 12397 typval_T *rettv; 12398 { 12399 char_u buf[2]; 12400 12401 #ifdef FEAT_VISUAL 12402 if (VIsual_active) 12403 { 12404 if (VIsual_select) 12405 buf[0] = VIsual_mode + 's' - 'v'; 12406 else 12407 buf[0] = VIsual_mode; 12408 } 12409 else 12410 #endif 12411 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE) 12412 buf[0] = 'r'; 12413 else if (State & INSERT) 12414 { 12415 if (State & REPLACE_FLAG) 12416 buf[0] = 'R'; 12417 else 12418 buf[0] = 'i'; 12419 } 12420 else if (State & CMDLINE) 12421 buf[0] = 'c'; 12422 else 12423 buf[0] = 'n'; 12424 12425 buf[1] = NUL; 12426 rettv->vval.v_string = vim_strsave(buf); 12427 rettv->v_type = VAR_STRING; 12428 } 12429 12430 /* 12431 * "nextnonblank()" function 12432 */ 12433 static void 12434 f_nextnonblank(argvars, rettv) 12435 typval_T *argvars; 12436 typval_T *rettv; 12437 { 12438 linenr_T lnum; 12439 12440 for (lnum = get_tv_lnum(argvars); ; ++lnum) 12441 { 12442 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count) 12443 { 12444 lnum = 0; 12445 break; 12446 } 12447 if (*skipwhite(ml_get(lnum)) != NUL) 12448 break; 12449 } 12450 rettv->vval.v_number = lnum; 12451 } 12452 12453 /* 12454 * "nr2char()" function 12455 */ 12456 static void 12457 f_nr2char(argvars, rettv) 12458 typval_T *argvars; 12459 typval_T *rettv; 12460 { 12461 char_u buf[NUMBUFLEN]; 12462 12463 #ifdef FEAT_MBYTE 12464 if (has_mbyte) 12465 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL; 12466 else 12467 #endif 12468 { 12469 buf[0] = (char_u)get_tv_number(&argvars[0]); 12470 buf[1] = NUL; 12471 } 12472 rettv->v_type = VAR_STRING; 12473 rettv->vval.v_string = vim_strsave(buf); 12474 } 12475 12476 /* 12477 * "pathshorten()" function 12478 */ 12479 static void 12480 f_pathshorten(argvars, rettv) 12481 typval_T *argvars; 12482 typval_T *rettv; 12483 { 12484 char_u *p; 12485 12486 rettv->v_type = VAR_STRING; 12487 p = get_tv_string_chk(&argvars[0]); 12488 if (p == NULL) 12489 rettv->vval.v_string = NULL; 12490 else 12491 { 12492 p = vim_strsave(p); 12493 rettv->vval.v_string = p; 12494 if (p != NULL) 12495 shorten_dir(p); 12496 } 12497 } 12498 12499 /* 12500 * "prevnonblank()" function 12501 */ 12502 static void 12503 f_prevnonblank(argvars, rettv) 12504 typval_T *argvars; 12505 typval_T *rettv; 12506 { 12507 linenr_T lnum; 12508 12509 lnum = get_tv_lnum(argvars); 12510 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) 12511 lnum = 0; 12512 else 12513 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL) 12514 --lnum; 12515 rettv->vval.v_number = lnum; 12516 } 12517 12518 #ifdef HAVE_STDARG_H 12519 /* This dummy va_list is here because: 12520 * - passing a NULL pointer doesn't work when va_list isn't a pointer 12521 * - locally in the function results in a "used before set" warning 12522 * - using va_start() to initialize it gives "function with fixed args" error */ 12523 static va_list ap; 12524 #endif 12525 12526 /* 12527 * "printf()" function 12528 */ 12529 static void 12530 f_printf(argvars, rettv) 12531 typval_T *argvars; 12532 typval_T *rettv; 12533 { 12534 rettv->v_type = VAR_STRING; 12535 rettv->vval.v_string = NULL; 12536 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */ 12537 { 12538 char_u buf[NUMBUFLEN]; 12539 int len; 12540 char_u *s; 12541 int saved_did_emsg = did_emsg; 12542 char *fmt; 12543 12544 /* Get the required length, allocate the buffer and do it for real. */ 12545 did_emsg = FALSE; 12546 fmt = (char *)get_tv_string_buf(&argvars[0], buf); 12547 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1); 12548 if (!did_emsg) 12549 { 12550 s = alloc(len + 1); 12551 if (s != NULL) 12552 { 12553 rettv->vval.v_string = s; 12554 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1); 12555 } 12556 } 12557 did_emsg |= saved_did_emsg; 12558 } 12559 #endif 12560 } 12561 12562 /* 12563 * "pumvisible()" function 12564 */ 12565 /*ARGSUSED*/ 12566 static void 12567 f_pumvisible(argvars, rettv) 12568 typval_T *argvars; 12569 typval_T *rettv; 12570 { 12571 rettv->vval.v_number = 0; 12572 #ifdef FEAT_INS_EXPAND 12573 if (pum_visible()) 12574 rettv->vval.v_number = 1; 12575 #endif 12576 } 12577 12578 /* 12579 * "range()" function 12580 */ 12581 static void 12582 f_range(argvars, rettv) 12583 typval_T *argvars; 12584 typval_T *rettv; 12585 { 12586 long start; 12587 long end; 12588 long stride = 1; 12589 long i; 12590 int error = FALSE; 12591 12592 start = get_tv_number_chk(&argvars[0], &error); 12593 if (argvars[1].v_type == VAR_UNKNOWN) 12594 { 12595 end = start - 1; 12596 start = 0; 12597 } 12598 else 12599 { 12600 end = get_tv_number_chk(&argvars[1], &error); 12601 if (argvars[2].v_type != VAR_UNKNOWN) 12602 stride = get_tv_number_chk(&argvars[2], &error); 12603 } 12604 12605 rettv->vval.v_number = 0; 12606 if (error) 12607 return; /* type error; errmsg already given */ 12608 if (stride == 0) 12609 EMSG(_("E726: Stride is zero")); 12610 else if (stride > 0 ? end + 1 < start : end - 1 > start) 12611 EMSG(_("E727: Start past end")); 12612 else 12613 { 12614 if (rettv_list_alloc(rettv) == OK) 12615 for (i = start; stride > 0 ? i <= end : i >= end; i += stride) 12616 if (list_append_number(rettv->vval.v_list, 12617 (varnumber_T)i) == FAIL) 12618 break; 12619 } 12620 } 12621 12622 /* 12623 * "readfile()" function 12624 */ 12625 static void 12626 f_readfile(argvars, rettv) 12627 typval_T *argvars; 12628 typval_T *rettv; 12629 { 12630 int binary = FALSE; 12631 char_u *fname; 12632 FILE *fd; 12633 listitem_T *li; 12634 #define FREAD_SIZE 200 /* optimized for text lines */ 12635 char_u buf[FREAD_SIZE]; 12636 int readlen; /* size of last fread() */ 12637 int buflen; /* nr of valid chars in buf[] */ 12638 int filtd; /* how much in buf[] was NUL -> '\n' filtered */ 12639 int tolist; /* first byte in buf[] still to be put in list */ 12640 int chop; /* how many CR to chop off */ 12641 char_u *prev = NULL; /* previously read bytes, if any */ 12642 int prevlen = 0; /* length of "prev" if not NULL */ 12643 char_u *s; 12644 int len; 12645 long maxline = MAXLNUM; 12646 long cnt = 0; 12647 12648 if (argvars[1].v_type != VAR_UNKNOWN) 12649 { 12650 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0) 12651 binary = TRUE; 12652 if (argvars[2].v_type != VAR_UNKNOWN) 12653 maxline = get_tv_number(&argvars[2]); 12654 } 12655 12656 if (rettv_list_alloc(rettv) == FAIL) 12657 return; 12658 12659 /* Always open the file in binary mode, library functions have a mind of 12660 * their own about CR-LF conversion. */ 12661 fname = get_tv_string(&argvars[0]); 12662 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL) 12663 { 12664 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname); 12665 return; 12666 } 12667 12668 filtd = 0; 12669 while (cnt < maxline || maxline < 0) 12670 { 12671 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd); 12672 buflen = filtd + readlen; 12673 tolist = 0; 12674 for ( ; filtd < buflen || readlen <= 0; ++filtd) 12675 { 12676 if (buf[filtd] == '\n' || readlen <= 0) 12677 { 12678 /* Only when in binary mode add an empty list item when the 12679 * last line ends in a '\n'. */ 12680 if (!binary && readlen == 0 && filtd == 0) 12681 break; 12682 12683 /* Found end-of-line or end-of-file: add a text line to the 12684 * list. */ 12685 chop = 0; 12686 if (!binary) 12687 while (filtd - chop - 1 >= tolist 12688 && buf[filtd - chop - 1] == '\r') 12689 ++chop; 12690 len = filtd - tolist - chop; 12691 if (prev == NULL) 12692 s = vim_strnsave(buf + tolist, len); 12693 else 12694 { 12695 s = alloc((unsigned)(prevlen + len + 1)); 12696 if (s != NULL) 12697 { 12698 mch_memmove(s, prev, prevlen); 12699 vim_free(prev); 12700 prev = NULL; 12701 mch_memmove(s + prevlen, buf + tolist, len); 12702 s[prevlen + len] = NUL; 12703 } 12704 } 12705 tolist = filtd + 1; 12706 12707 li = listitem_alloc(); 12708 if (li == NULL) 12709 { 12710 vim_free(s); 12711 break; 12712 } 12713 li->li_tv.v_type = VAR_STRING; 12714 li->li_tv.v_lock = 0; 12715 li->li_tv.vval.v_string = s; 12716 list_append(rettv->vval.v_list, li); 12717 12718 if (++cnt >= maxline && maxline >= 0) 12719 break; 12720 if (readlen <= 0) 12721 break; 12722 } 12723 else if (buf[filtd] == NUL) 12724 buf[filtd] = '\n'; 12725 } 12726 if (readlen <= 0) 12727 break; 12728 12729 if (tolist == 0) 12730 { 12731 /* "buf" is full, need to move text to an allocated buffer */ 12732 if (prev == NULL) 12733 { 12734 prev = vim_strnsave(buf, buflen); 12735 prevlen = buflen; 12736 } 12737 else 12738 { 12739 s = alloc((unsigned)(prevlen + buflen)); 12740 if (s != NULL) 12741 { 12742 mch_memmove(s, prev, prevlen); 12743 mch_memmove(s + prevlen, buf, buflen); 12744 vim_free(prev); 12745 prev = s; 12746 prevlen += buflen; 12747 } 12748 } 12749 filtd = 0; 12750 } 12751 else 12752 { 12753 mch_memmove(buf, buf + tolist, buflen - tolist); 12754 filtd -= tolist; 12755 } 12756 } 12757 12758 /* 12759 * For a negative line count use only the lines at the end of the file, 12760 * free the rest. 12761 */ 12762 if (maxline < 0) 12763 while (cnt > -maxline) 12764 { 12765 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first); 12766 --cnt; 12767 } 12768 12769 vim_free(prev); 12770 fclose(fd); 12771 } 12772 12773 #if defined(FEAT_RELTIME) 12774 static int list2proftime __ARGS((typval_T *arg, proftime_T *tm)); 12775 12776 /* 12777 * Convert a List to proftime_T. 12778 * Return FAIL when there is something wrong. 12779 */ 12780 static int 12781 list2proftime(arg, tm) 12782 typval_T *arg; 12783 proftime_T *tm; 12784 { 12785 long n1, n2; 12786 int error = FALSE; 12787 12788 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL 12789 || arg->vval.v_list->lv_len != 2) 12790 return FAIL; 12791 n1 = list_find_nr(arg->vval.v_list, 0L, &error); 12792 n2 = list_find_nr(arg->vval.v_list, 1L, &error); 12793 # ifdef WIN3264 12794 tm->HighPart = n1; 12795 tm->LowPart = n2; 12796 # else 12797 tm->tv_sec = n1; 12798 tm->tv_usec = n2; 12799 # endif 12800 return error ? FAIL : OK; 12801 } 12802 #endif /* FEAT_RELTIME */ 12803 12804 /* 12805 * "reltime()" function 12806 */ 12807 static void 12808 f_reltime(argvars, rettv) 12809 typval_T *argvars; 12810 typval_T *rettv; 12811 { 12812 #ifdef FEAT_RELTIME 12813 proftime_T res; 12814 proftime_T start; 12815 12816 if (argvars[0].v_type == VAR_UNKNOWN) 12817 { 12818 /* No arguments: get current time. */ 12819 profile_start(&res); 12820 } 12821 else if (argvars[1].v_type == VAR_UNKNOWN) 12822 { 12823 if (list2proftime(&argvars[0], &res) == FAIL) 12824 return; 12825 profile_end(&res); 12826 } 12827 else 12828 { 12829 /* Two arguments: compute the difference. */ 12830 if (list2proftime(&argvars[0], &start) == FAIL 12831 || list2proftime(&argvars[1], &res) == FAIL) 12832 return; 12833 profile_sub(&res, &start); 12834 } 12835 12836 if (rettv_list_alloc(rettv) == OK) 12837 { 12838 long n1, n2; 12839 12840 # ifdef WIN3264 12841 n1 = res.HighPart; 12842 n2 = res.LowPart; 12843 # else 12844 n1 = res.tv_sec; 12845 n2 = res.tv_usec; 12846 # endif 12847 list_append_number(rettv->vval.v_list, (varnumber_T)n1); 12848 list_append_number(rettv->vval.v_list, (varnumber_T)n2); 12849 } 12850 #endif 12851 } 12852 12853 /* 12854 * "reltimestr()" function 12855 */ 12856 static void 12857 f_reltimestr(argvars, rettv) 12858 typval_T *argvars; 12859 typval_T *rettv; 12860 { 12861 #ifdef FEAT_RELTIME 12862 proftime_T tm; 12863 #endif 12864 12865 rettv->v_type = VAR_STRING; 12866 rettv->vval.v_string = NULL; 12867 #ifdef FEAT_RELTIME 12868 if (list2proftime(&argvars[0], &tm) == OK) 12869 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm)); 12870 #endif 12871 } 12872 12873 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) 12874 static void make_connection __ARGS((void)); 12875 static int check_connection __ARGS((void)); 12876 12877 static void 12878 make_connection() 12879 { 12880 if (X_DISPLAY == NULL 12881 # ifdef FEAT_GUI 12882 && !gui.in_use 12883 # endif 12884 ) 12885 { 12886 x_force_connect = TRUE; 12887 setup_term_clip(); 12888 x_force_connect = FALSE; 12889 } 12890 } 12891 12892 static int 12893 check_connection() 12894 { 12895 make_connection(); 12896 if (X_DISPLAY == NULL) 12897 { 12898 EMSG(_("E240: No connection to Vim server")); 12899 return FAIL; 12900 } 12901 return OK; 12902 } 12903 #endif 12904 12905 #ifdef FEAT_CLIENTSERVER 12906 static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr)); 12907 12908 static void 12909 remote_common(argvars, rettv, expr) 12910 typval_T *argvars; 12911 typval_T *rettv; 12912 int expr; 12913 { 12914 char_u *server_name; 12915 char_u *keys; 12916 char_u *r = NULL; 12917 char_u buf[NUMBUFLEN]; 12918 # ifdef WIN32 12919 HWND w; 12920 # else 12921 Window w; 12922 # endif 12923 12924 if (check_restricted() || check_secure()) 12925 return; 12926 12927 # ifdef FEAT_X11 12928 if (check_connection() == FAIL) 12929 return; 12930 # endif 12931 12932 server_name = get_tv_string_chk(&argvars[0]); 12933 if (server_name == NULL) 12934 return; /* type error; errmsg already given */ 12935 keys = get_tv_string_buf(&argvars[1], buf); 12936 # ifdef WIN32 12937 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0) 12938 # else 12939 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE) 12940 < 0) 12941 # endif 12942 { 12943 if (r != NULL) 12944 EMSG(r); /* sending worked but evaluation failed */ 12945 else 12946 EMSG2(_("E241: Unable to send to %s"), server_name); 12947 return; 12948 } 12949 12950 rettv->vval.v_string = r; 12951 12952 if (argvars[2].v_type != VAR_UNKNOWN) 12953 { 12954 dictitem_T v; 12955 char_u str[30]; 12956 char_u *idvar; 12957 12958 sprintf((char *)str, "0x%x", (unsigned int)w); 12959 v.di_tv.v_type = VAR_STRING; 12960 v.di_tv.vval.v_string = vim_strsave(str); 12961 idvar = get_tv_string_chk(&argvars[2]); 12962 if (idvar != NULL) 12963 set_var(idvar, &v.di_tv, FALSE); 12964 vim_free(v.di_tv.vval.v_string); 12965 } 12966 } 12967 #endif 12968 12969 /* 12970 * "remote_expr()" function 12971 */ 12972 /*ARGSUSED*/ 12973 static void 12974 f_remote_expr(argvars, rettv) 12975 typval_T *argvars; 12976 typval_T *rettv; 12977 { 12978 rettv->v_type = VAR_STRING; 12979 rettv->vval.v_string = NULL; 12980 #ifdef FEAT_CLIENTSERVER 12981 remote_common(argvars, rettv, TRUE); 12982 #endif 12983 } 12984 12985 /* 12986 * "remote_foreground()" function 12987 */ 12988 /*ARGSUSED*/ 12989 static void 12990 f_remote_foreground(argvars, rettv) 12991 typval_T *argvars; 12992 typval_T *rettv; 12993 { 12994 rettv->vval.v_number = 0; 12995 #ifdef FEAT_CLIENTSERVER 12996 # ifdef WIN32 12997 /* On Win32 it's done in this application. */ 12998 { 12999 char_u *server_name = get_tv_string_chk(&argvars[0]); 13000 13001 if (server_name != NULL) 13002 serverForeground(server_name); 13003 } 13004 # else 13005 /* Send a foreground() expression to the server. */ 13006 argvars[1].v_type = VAR_STRING; 13007 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()"); 13008 argvars[2].v_type = VAR_UNKNOWN; 13009 remote_common(argvars, rettv, TRUE); 13010 vim_free(argvars[1].vval.v_string); 13011 # endif 13012 #endif 13013 } 13014 13015 /*ARGSUSED*/ 13016 static void 13017 f_remote_peek(argvars, rettv) 13018 typval_T *argvars; 13019 typval_T *rettv; 13020 { 13021 #ifdef FEAT_CLIENTSERVER 13022 dictitem_T v; 13023 char_u *s = NULL; 13024 # ifdef WIN32 13025 int n = 0; 13026 # endif 13027 char_u *serverid; 13028 13029 if (check_restricted() || check_secure()) 13030 { 13031 rettv->vval.v_number = -1; 13032 return; 13033 } 13034 serverid = get_tv_string_chk(&argvars[0]); 13035 if (serverid == NULL) 13036 { 13037 rettv->vval.v_number = -1; 13038 return; /* type error; errmsg already given */ 13039 } 13040 # ifdef WIN32 13041 sscanf(serverid, "%x", &n); 13042 if (n == 0) 13043 rettv->vval.v_number = -1; 13044 else 13045 { 13046 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE); 13047 rettv->vval.v_number = (s != NULL); 13048 } 13049 # else 13050 rettv->vval.v_number = 0; 13051 if (check_connection() == FAIL) 13052 return; 13053 13054 rettv->vval.v_number = serverPeekReply(X_DISPLAY, 13055 serverStrToWin(serverid), &s); 13056 # endif 13057 13058 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0) 13059 { 13060 char_u *retvar; 13061 13062 v.di_tv.v_type = VAR_STRING; 13063 v.di_tv.vval.v_string = vim_strsave(s); 13064 retvar = get_tv_string_chk(&argvars[1]); 13065 if (retvar != NULL) 13066 set_var(retvar, &v.di_tv, FALSE); 13067 vim_free(v.di_tv.vval.v_string); 13068 } 13069 #else 13070 rettv->vval.v_number = -1; 13071 #endif 13072 } 13073 13074 /*ARGSUSED*/ 13075 static void 13076 f_remote_read(argvars, rettv) 13077 typval_T *argvars; 13078 typval_T *rettv; 13079 { 13080 char_u *r = NULL; 13081 13082 #ifdef FEAT_CLIENTSERVER 13083 char_u *serverid = get_tv_string_chk(&argvars[0]); 13084 13085 if (serverid != NULL && !check_restricted() && !check_secure()) 13086 { 13087 # ifdef WIN32 13088 /* The server's HWND is encoded in the 'id' parameter */ 13089 int n = 0; 13090 13091 sscanf(serverid, "%x", &n); 13092 if (n != 0) 13093 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE); 13094 if (r == NULL) 13095 # else 13096 if (check_connection() == FAIL || serverReadReply(X_DISPLAY, 13097 serverStrToWin(serverid), &r, FALSE) < 0) 13098 # endif 13099 EMSG(_("E277: Unable to read a server reply")); 13100 } 13101 #endif 13102 rettv->v_type = VAR_STRING; 13103 rettv->vval.v_string = r; 13104 } 13105 13106 /* 13107 * "remote_send()" function 13108 */ 13109 /*ARGSUSED*/ 13110 static void 13111 f_remote_send(argvars, rettv) 13112 typval_T *argvars; 13113 typval_T *rettv; 13114 { 13115 rettv->v_type = VAR_STRING; 13116 rettv->vval.v_string = NULL; 13117 #ifdef FEAT_CLIENTSERVER 13118 remote_common(argvars, rettv, FALSE); 13119 #endif 13120 } 13121 13122 /* 13123 * "remove()" function 13124 */ 13125 static void 13126 f_remove(argvars, rettv) 13127 typval_T *argvars; 13128 typval_T *rettv; 13129 { 13130 list_T *l; 13131 listitem_T *item, *item2; 13132 listitem_T *li; 13133 long idx; 13134 long end; 13135 char_u *key; 13136 dict_T *d; 13137 dictitem_T *di; 13138 13139 rettv->vval.v_number = 0; 13140 if (argvars[0].v_type == VAR_DICT) 13141 { 13142 if (argvars[2].v_type != VAR_UNKNOWN) 13143 EMSG2(_(e_toomanyarg), "remove()"); 13144 else if ((d = argvars[0].vval.v_dict) != NULL 13145 && !tv_check_lock(d->dv_lock, (char_u *)"remove()")) 13146 { 13147 key = get_tv_string_chk(&argvars[1]); 13148 if (key != NULL) 13149 { 13150 di = dict_find(d, key, -1); 13151 if (di == NULL) 13152 EMSG2(_(e_dictkey), key); 13153 else 13154 { 13155 *rettv = di->di_tv; 13156 init_tv(&di->di_tv); 13157 dictitem_remove(d, di); 13158 } 13159 } 13160 } 13161 } 13162 else if (argvars[0].v_type != VAR_LIST) 13163 EMSG2(_(e_listdictarg), "remove()"); 13164 else if ((l = argvars[0].vval.v_list) != NULL 13165 && !tv_check_lock(l->lv_lock, (char_u *)"remove()")) 13166 { 13167 int error = FALSE; 13168 13169 idx = get_tv_number_chk(&argvars[1], &error); 13170 if (error) 13171 ; /* type error: do nothing, errmsg already given */ 13172 else if ((item = list_find(l, idx)) == NULL) 13173 EMSGN(_(e_listidx), idx); 13174 else 13175 { 13176 if (argvars[2].v_type == VAR_UNKNOWN) 13177 { 13178 /* Remove one item, return its value. */ 13179 list_remove(l, item, item); 13180 *rettv = item->li_tv; 13181 vim_free(item); 13182 } 13183 else 13184 { 13185 /* Remove range of items, return list with values. */ 13186 end = get_tv_number_chk(&argvars[2], &error); 13187 if (error) 13188 ; /* type error: do nothing */ 13189 else if ((item2 = list_find(l, end)) == NULL) 13190 EMSGN(_(e_listidx), end); 13191 else 13192 { 13193 int cnt = 0; 13194 13195 for (li = item; li != NULL; li = li->li_next) 13196 { 13197 ++cnt; 13198 if (li == item2) 13199 break; 13200 } 13201 if (li == NULL) /* didn't find "item2" after "item" */ 13202 EMSG(_(e_invrange)); 13203 else 13204 { 13205 list_remove(l, item, item2); 13206 if (rettv_list_alloc(rettv) == OK) 13207 { 13208 l = rettv->vval.v_list; 13209 l->lv_first = item; 13210 l->lv_last = item2; 13211 item->li_prev = NULL; 13212 item2->li_next = NULL; 13213 l->lv_len = cnt; 13214 } 13215 } 13216 } 13217 } 13218 } 13219 } 13220 } 13221 13222 /* 13223 * "rename({from}, {to})" function 13224 */ 13225 static void 13226 f_rename(argvars, rettv) 13227 typval_T *argvars; 13228 typval_T *rettv; 13229 { 13230 char_u buf[NUMBUFLEN]; 13231 13232 if (check_restricted() || check_secure()) 13233 rettv->vval.v_number = -1; 13234 else 13235 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]), 13236 get_tv_string_buf(&argvars[1], buf)); 13237 } 13238 13239 /* 13240 * "repeat()" function 13241 */ 13242 /*ARGSUSED*/ 13243 static void 13244 f_repeat(argvars, rettv) 13245 typval_T *argvars; 13246 typval_T *rettv; 13247 { 13248 char_u *p; 13249 int n; 13250 int slen; 13251 int len; 13252 char_u *r; 13253 int i; 13254 13255 n = get_tv_number(&argvars[1]); 13256 if (argvars[0].v_type == VAR_LIST) 13257 { 13258 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL) 13259 while (n-- > 0) 13260 if (list_extend(rettv->vval.v_list, 13261 argvars[0].vval.v_list, NULL) == FAIL) 13262 break; 13263 } 13264 else 13265 { 13266 p = get_tv_string(&argvars[0]); 13267 rettv->v_type = VAR_STRING; 13268 rettv->vval.v_string = NULL; 13269 13270 slen = (int)STRLEN(p); 13271 len = slen * n; 13272 if (len <= 0) 13273 return; 13274 13275 r = alloc(len + 1); 13276 if (r != NULL) 13277 { 13278 for (i = 0; i < n; i++) 13279 mch_memmove(r + i * slen, p, (size_t)slen); 13280 r[len] = NUL; 13281 } 13282 13283 rettv->vval.v_string = r; 13284 } 13285 } 13286 13287 /* 13288 * "resolve()" function 13289 */ 13290 static void 13291 f_resolve(argvars, rettv) 13292 typval_T *argvars; 13293 typval_T *rettv; 13294 { 13295 char_u *p; 13296 13297 p = get_tv_string(&argvars[0]); 13298 #ifdef FEAT_SHORTCUT 13299 { 13300 char_u *v = NULL; 13301 13302 v = mch_resolve_shortcut(p); 13303 if (v != NULL) 13304 rettv->vval.v_string = v; 13305 else 13306 rettv->vval.v_string = vim_strsave(p); 13307 } 13308 #else 13309 # ifdef HAVE_READLINK 13310 { 13311 char_u buf[MAXPATHL + 1]; 13312 char_u *cpy; 13313 int len; 13314 char_u *remain = NULL; 13315 char_u *q; 13316 int is_relative_to_current = FALSE; 13317 int has_trailing_pathsep = FALSE; 13318 int limit = 100; 13319 13320 p = vim_strsave(p); 13321 13322 if (p[0] == '.' && (vim_ispathsep(p[1]) 13323 || (p[1] == '.' && (vim_ispathsep(p[2]))))) 13324 is_relative_to_current = TRUE; 13325 13326 len = STRLEN(p); 13327 if (len > 0 && after_pathsep(p, p + len)) 13328 has_trailing_pathsep = TRUE; 13329 13330 q = getnextcomp(p); 13331 if (*q != NUL) 13332 { 13333 /* Separate the first path component in "p", and keep the 13334 * remainder (beginning with the path separator). */ 13335 remain = vim_strsave(q - 1); 13336 q[-1] = NUL; 13337 } 13338 13339 for (;;) 13340 { 13341 for (;;) 13342 { 13343 len = readlink((char *)p, (char *)buf, MAXPATHL); 13344 if (len <= 0) 13345 break; 13346 buf[len] = NUL; 13347 13348 if (limit-- == 0) 13349 { 13350 vim_free(p); 13351 vim_free(remain); 13352 EMSG(_("E655: Too many symbolic links (cycle?)")); 13353 rettv->vval.v_string = NULL; 13354 goto fail; 13355 } 13356 13357 /* Ensure that the result will have a trailing path separator 13358 * if the argument has one. */ 13359 if (remain == NULL && has_trailing_pathsep) 13360 add_pathsep(buf); 13361 13362 /* Separate the first path component in the link value and 13363 * concatenate the remainders. */ 13364 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf); 13365 if (*q != NUL) 13366 { 13367 if (remain == NULL) 13368 remain = vim_strsave(q - 1); 13369 else 13370 { 13371 cpy = concat_str(q - 1, remain); 13372 if (cpy != NULL) 13373 { 13374 vim_free(remain); 13375 remain = cpy; 13376 } 13377 } 13378 q[-1] = NUL; 13379 } 13380 13381 q = gettail(p); 13382 if (q > p && *q == NUL) 13383 { 13384 /* Ignore trailing path separator. */ 13385 q[-1] = NUL; 13386 q = gettail(p); 13387 } 13388 if (q > p && !mch_isFullName(buf)) 13389 { 13390 /* symlink is relative to directory of argument */ 13391 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1)); 13392 if (cpy != NULL) 13393 { 13394 STRCPY(cpy, p); 13395 STRCPY(gettail(cpy), buf); 13396 vim_free(p); 13397 p = cpy; 13398 } 13399 } 13400 else 13401 { 13402 vim_free(p); 13403 p = vim_strsave(buf); 13404 } 13405 } 13406 13407 if (remain == NULL) 13408 break; 13409 13410 /* Append the first path component of "remain" to "p". */ 13411 q = getnextcomp(remain + 1); 13412 len = q - remain - (*q != NUL); 13413 cpy = vim_strnsave(p, STRLEN(p) + len); 13414 if (cpy != NULL) 13415 { 13416 STRNCAT(cpy, remain, len); 13417 vim_free(p); 13418 p = cpy; 13419 } 13420 /* Shorten "remain". */ 13421 if (*q != NUL) 13422 STRCPY(remain, q - 1); 13423 else 13424 { 13425 vim_free(remain); 13426 remain = NULL; 13427 } 13428 } 13429 13430 /* If the result is a relative path name, make it explicitly relative to 13431 * the current directory if and only if the argument had this form. */ 13432 if (!vim_ispathsep(*p)) 13433 { 13434 if (is_relative_to_current 13435 && *p != NUL 13436 && !(p[0] == '.' 13437 && (p[1] == NUL 13438 || vim_ispathsep(p[1]) 13439 || (p[1] == '.' 13440 && (p[2] == NUL 13441 || vim_ispathsep(p[2])))))) 13442 { 13443 /* Prepend "./". */ 13444 cpy = concat_str((char_u *)"./", p); 13445 if (cpy != NULL) 13446 { 13447 vim_free(p); 13448 p = cpy; 13449 } 13450 } 13451 else if (!is_relative_to_current) 13452 { 13453 /* Strip leading "./". */ 13454 q = p; 13455 while (q[0] == '.' && vim_ispathsep(q[1])) 13456 q += 2; 13457 if (q > p) 13458 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1); 13459 } 13460 } 13461 13462 /* Ensure that the result will have no trailing path separator 13463 * if the argument had none. But keep "/" or "//". */ 13464 if (!has_trailing_pathsep) 13465 { 13466 q = p + STRLEN(p); 13467 if (after_pathsep(p, q)) 13468 *gettail_sep(p) = NUL; 13469 } 13470 13471 rettv->vval.v_string = p; 13472 } 13473 # else 13474 rettv->vval.v_string = vim_strsave(p); 13475 # endif 13476 #endif 13477 13478 simplify_filename(rettv->vval.v_string); 13479 13480 #ifdef HAVE_READLINK 13481 fail: 13482 #endif 13483 rettv->v_type = VAR_STRING; 13484 } 13485 13486 /* 13487 * "reverse({list})" function 13488 */ 13489 static void 13490 f_reverse(argvars, rettv) 13491 typval_T *argvars; 13492 typval_T *rettv; 13493 { 13494 list_T *l; 13495 listitem_T *li, *ni; 13496 13497 rettv->vval.v_number = 0; 13498 if (argvars[0].v_type != VAR_LIST) 13499 EMSG2(_(e_listarg), "reverse()"); 13500 else if ((l = argvars[0].vval.v_list) != NULL 13501 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()")) 13502 { 13503 li = l->lv_last; 13504 l->lv_first = l->lv_last = NULL; 13505 l->lv_len = 0; 13506 while (li != NULL) 13507 { 13508 ni = li->li_prev; 13509 list_append(l, li); 13510 li = ni; 13511 } 13512 rettv->vval.v_list = l; 13513 rettv->v_type = VAR_LIST; 13514 ++l->lv_refcount; 13515 } 13516 } 13517 13518 #define SP_NOMOVE 0x01 /* don't move cursor */ 13519 #define SP_REPEAT 0x02 /* repeat to find outer pair */ 13520 #define SP_RETCOUNT 0x04 /* return matchcount */ 13521 #define SP_SETPCMARK 0x08 /* set previous context mark */ 13522 #define SP_START 0x10 /* accept match at start position */ 13523 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */ 13524 #define SP_END 0x40 /* leave cursor at end of match */ 13525 13526 static int get_search_arg __ARGS((typval_T *varp, int *flagsp)); 13527 13528 /* 13529 * Get flags for a search function. 13530 * Possibly sets "p_ws". 13531 * Returns BACKWARD, FORWARD or zero (for an error). 13532 */ 13533 static int 13534 get_search_arg(varp, flagsp) 13535 typval_T *varp; 13536 int *flagsp; 13537 { 13538 int dir = FORWARD; 13539 char_u *flags; 13540 char_u nbuf[NUMBUFLEN]; 13541 int mask; 13542 13543 if (varp->v_type != VAR_UNKNOWN) 13544 { 13545 flags = get_tv_string_buf_chk(varp, nbuf); 13546 if (flags == NULL) 13547 return 0; /* type error; errmsg already given */ 13548 while (*flags != NUL) 13549 { 13550 switch (*flags) 13551 { 13552 case 'b': dir = BACKWARD; break; 13553 case 'w': p_ws = TRUE; break; 13554 case 'W': p_ws = FALSE; break; 13555 default: mask = 0; 13556 if (flagsp != NULL) 13557 switch (*flags) 13558 { 13559 case 'c': mask = SP_START; break; 13560 case 'e': mask = SP_END; break; 13561 case 'm': mask = SP_RETCOUNT; break; 13562 case 'n': mask = SP_NOMOVE; break; 13563 case 'p': mask = SP_SUBPAT; break; 13564 case 'r': mask = SP_REPEAT; break; 13565 case 's': mask = SP_SETPCMARK; break; 13566 } 13567 if (mask == 0) 13568 { 13569 EMSG2(_(e_invarg2), flags); 13570 dir = 0; 13571 } 13572 else 13573 *flagsp |= mask; 13574 } 13575 if (dir == 0) 13576 break; 13577 ++flags; 13578 } 13579 } 13580 return dir; 13581 } 13582 13583 /* 13584 * Shared by search() and searchpos() functions 13585 */ 13586 static int 13587 search_cmn(argvars, match_pos, flagsp) 13588 typval_T *argvars; 13589 pos_T *match_pos; 13590 int *flagsp; 13591 { 13592 int flags; 13593 char_u *pat; 13594 pos_T pos; 13595 pos_T save_cursor; 13596 int save_p_ws = p_ws; 13597 int dir; 13598 int retval = 0; /* default: FAIL */ 13599 long lnum_stop = 0; 13600 int options = SEARCH_KEEP; 13601 int subpatnum; 13602 13603 pat = get_tv_string(&argvars[0]); 13604 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */ 13605 if (dir == 0) 13606 goto theend; 13607 flags = *flagsp; 13608 if (flags & SP_START) 13609 options |= SEARCH_START; 13610 if (flags & SP_END) 13611 options |= SEARCH_END; 13612 13613 /* Optional extra argument: line number to stop searching. */ 13614 if (argvars[1].v_type != VAR_UNKNOWN 13615 && argvars[2].v_type != VAR_UNKNOWN) 13616 { 13617 lnum_stop = get_tv_number_chk(&argvars[2], NULL); 13618 if (lnum_stop < 0) 13619 goto theend; 13620 } 13621 13622 /* 13623 * This function does not accept SP_REPEAT and SP_RETCOUNT flags. 13624 * Check to make sure only those flags are set. 13625 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both 13626 * flags cannot be set. Check for that condition also. 13627 */ 13628 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0) 13629 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))) 13630 { 13631 EMSG2(_(e_invarg2), get_tv_string(&argvars[1])); 13632 goto theend; 13633 } 13634 13635 pos = save_cursor = curwin->w_cursor; 13636 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L, 13637 options, RE_SEARCH, (linenr_T)lnum_stop); 13638 if (subpatnum != FAIL) 13639 { 13640 if (flags & SP_SUBPAT) 13641 retval = subpatnum; 13642 else 13643 retval = pos.lnum; 13644 if (flags & SP_SETPCMARK) 13645 setpcmark(); 13646 curwin->w_cursor = pos; 13647 if (match_pos != NULL) 13648 { 13649 /* Store the match cursor position */ 13650 match_pos->lnum = pos.lnum; 13651 match_pos->col = pos.col + 1; 13652 } 13653 /* "/$" will put the cursor after the end of the line, may need to 13654 * correct that here */ 13655 check_cursor(); 13656 } 13657 13658 /* If 'n' flag is used: restore cursor position. */ 13659 if (flags & SP_NOMOVE) 13660 curwin->w_cursor = save_cursor; 13661 theend: 13662 p_ws = save_p_ws; 13663 13664 return retval; 13665 } 13666 13667 /* 13668 * "search()" function 13669 */ 13670 static void 13671 f_search(argvars, rettv) 13672 typval_T *argvars; 13673 typval_T *rettv; 13674 { 13675 int flags = 0; 13676 13677 rettv->vval.v_number = search_cmn(argvars, NULL, &flags); 13678 } 13679 13680 /* 13681 * "searchdecl()" function 13682 */ 13683 static void 13684 f_searchdecl(argvars, rettv) 13685 typval_T *argvars; 13686 typval_T *rettv; 13687 { 13688 int locally = 1; 13689 int thisblock = 0; 13690 int error = FALSE; 13691 char_u *name; 13692 13693 rettv->vval.v_number = 1; /* default: FAIL */ 13694 13695 name = get_tv_string_chk(&argvars[0]); 13696 if (argvars[1].v_type != VAR_UNKNOWN) 13697 { 13698 locally = get_tv_number_chk(&argvars[1], &error) == 0; 13699 if (!error && argvars[2].v_type != VAR_UNKNOWN) 13700 thisblock = get_tv_number_chk(&argvars[2], &error) != 0; 13701 } 13702 if (!error && name != NULL) 13703 rettv->vval.v_number = find_decl(name, (int)STRLEN(name), 13704 locally, thisblock, SEARCH_KEEP) == FAIL; 13705 } 13706 13707 /* 13708 * Used by searchpair() and searchpairpos() 13709 */ 13710 static int 13711 searchpair_cmn(argvars, match_pos) 13712 typval_T *argvars; 13713 pos_T *match_pos; 13714 { 13715 char_u *spat, *mpat, *epat; 13716 char_u *skip; 13717 int save_p_ws = p_ws; 13718 int dir; 13719 int flags = 0; 13720 char_u nbuf1[NUMBUFLEN]; 13721 char_u nbuf2[NUMBUFLEN]; 13722 char_u nbuf3[NUMBUFLEN]; 13723 int retval = 0; /* default: FAIL */ 13724 long lnum_stop = 0; 13725 13726 /* Get the three pattern arguments: start, middle, end. */ 13727 spat = get_tv_string_chk(&argvars[0]); 13728 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1); 13729 epat = get_tv_string_buf_chk(&argvars[2], nbuf2); 13730 if (spat == NULL || mpat == NULL || epat == NULL) 13731 goto theend; /* type error */ 13732 13733 /* Handle the optional fourth argument: flags */ 13734 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */ 13735 if (dir == 0) 13736 goto theend; 13737 13738 /* Don't accept SP_END or SP_SUBPAT. 13739 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set. 13740 */ 13741 if ((flags & (SP_END | SP_SUBPAT)) != 0 13742 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))) 13743 { 13744 EMSG2(_(e_invarg2), get_tv_string(&argvars[3])); 13745 goto theend; 13746 } 13747 13748 /* Optional fifth argument: skip expression */ 13749 if (argvars[3].v_type == VAR_UNKNOWN 13750 || argvars[4].v_type == VAR_UNKNOWN) 13751 skip = (char_u *)""; 13752 else 13753 { 13754 skip = get_tv_string_buf_chk(&argvars[4], nbuf3); 13755 if (argvars[5].v_type != VAR_UNKNOWN) 13756 { 13757 lnum_stop = get_tv_number_chk(&argvars[5], NULL); 13758 if (lnum_stop < 0) 13759 goto theend; 13760 } 13761 } 13762 if (skip == NULL) 13763 goto theend; /* type error */ 13764 13765 retval = do_searchpair(spat, mpat, epat, dir, skip, flags, 13766 match_pos, lnum_stop); 13767 13768 theend: 13769 p_ws = save_p_ws; 13770 13771 return retval; 13772 } 13773 13774 /* 13775 * "searchpair()" function 13776 */ 13777 static void 13778 f_searchpair(argvars, rettv) 13779 typval_T *argvars; 13780 typval_T *rettv; 13781 { 13782 rettv->vval.v_number = searchpair_cmn(argvars, NULL); 13783 } 13784 13785 /* 13786 * "searchpairpos()" function 13787 */ 13788 static void 13789 f_searchpairpos(argvars, rettv) 13790 typval_T *argvars; 13791 typval_T *rettv; 13792 { 13793 pos_T match_pos; 13794 int lnum = 0; 13795 int col = 0; 13796 13797 rettv->vval.v_number = 0; 13798 13799 if (rettv_list_alloc(rettv) == FAIL) 13800 return; 13801 13802 if (searchpair_cmn(argvars, &match_pos) > 0) 13803 { 13804 lnum = match_pos.lnum; 13805 col = match_pos.col; 13806 } 13807 13808 list_append_number(rettv->vval.v_list, (varnumber_T)lnum); 13809 list_append_number(rettv->vval.v_list, (varnumber_T)col); 13810 } 13811 13812 /* 13813 * Search for a start/middle/end thing. 13814 * Used by searchpair(), see its documentation for the details. 13815 * Returns 0 or -1 for no match, 13816 */ 13817 long 13818 do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop) 13819 char_u *spat; /* start pattern */ 13820 char_u *mpat; /* middle pattern */ 13821 char_u *epat; /* end pattern */ 13822 int dir; /* BACKWARD or FORWARD */ 13823 char_u *skip; /* skip expression */ 13824 int flags; /* SP_SETPCMARK and other SP_ values */ 13825 pos_T *match_pos; 13826 linenr_T lnum_stop; /* stop at this line if not zero */ 13827 { 13828 char_u *save_cpo; 13829 char_u *pat, *pat2 = NULL, *pat3 = NULL; 13830 long retval = 0; 13831 pos_T pos; 13832 pos_T firstpos; 13833 pos_T foundpos; 13834 pos_T save_cursor; 13835 pos_T save_pos; 13836 int n; 13837 int r; 13838 int nest = 1; 13839 int err; 13840 int options = SEARCH_KEEP; 13841 13842 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 13843 save_cpo = p_cpo; 13844 p_cpo = (char_u *)""; 13845 13846 /* Make two search patterns: start/end (pat2, for in nested pairs) and 13847 * start/middle/end (pat3, for the top pair). */ 13848 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15)); 13849 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23)); 13850 if (pat2 == NULL || pat3 == NULL) 13851 goto theend; 13852 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat); 13853 if (*mpat == NUL) 13854 STRCPY(pat3, pat2); 13855 else 13856 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)", 13857 spat, epat, mpat); 13858 if (flags & SP_START) 13859 options |= SEARCH_START; 13860 13861 save_cursor = curwin->w_cursor; 13862 pos = curwin->w_cursor; 13863 clearpos(&firstpos); 13864 clearpos(&foundpos); 13865 pat = pat3; 13866 for (;;) 13867 { 13868 n = searchit(curwin, curbuf, &pos, dir, pat, 1L, 13869 options, RE_SEARCH, lnum_stop); 13870 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos))) 13871 /* didn't find it or found the first match again: FAIL */ 13872 break; 13873 13874 if (firstpos.lnum == 0) 13875 firstpos = pos; 13876 if (equalpos(pos, foundpos)) 13877 { 13878 /* Found the same position again. Can happen with a pattern that 13879 * has "\zs" at the end and searching backwards. Advance one 13880 * character and try again. */ 13881 if (dir == BACKWARD) 13882 decl(&pos); 13883 else 13884 incl(&pos); 13885 } 13886 foundpos = pos; 13887 13888 /* If the skip pattern matches, ignore this match. */ 13889 if (*skip != NUL) 13890 { 13891 save_pos = curwin->w_cursor; 13892 curwin->w_cursor = pos; 13893 r = eval_to_bool(skip, &err, NULL, FALSE); 13894 curwin->w_cursor = save_pos; 13895 if (err) 13896 { 13897 /* Evaluating {skip} caused an error, break here. */ 13898 curwin->w_cursor = save_cursor; 13899 retval = -1; 13900 break; 13901 } 13902 if (r) 13903 continue; 13904 } 13905 13906 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2)) 13907 { 13908 /* Found end when searching backwards or start when searching 13909 * forward: nested pair. */ 13910 ++nest; 13911 pat = pat2; /* nested, don't search for middle */ 13912 } 13913 else 13914 { 13915 /* Found end when searching forward or start when searching 13916 * backward: end of (nested) pair; or found middle in outer pair. */ 13917 if (--nest == 1) 13918 pat = pat3; /* outer level, search for middle */ 13919 } 13920 13921 if (nest == 0) 13922 { 13923 /* Found the match: return matchcount or line number. */ 13924 if (flags & SP_RETCOUNT) 13925 ++retval; 13926 else 13927 retval = pos.lnum; 13928 if (flags & SP_SETPCMARK) 13929 setpcmark(); 13930 curwin->w_cursor = pos; 13931 if (!(flags & SP_REPEAT)) 13932 break; 13933 nest = 1; /* search for next unmatched */ 13934 } 13935 } 13936 13937 if (match_pos != NULL) 13938 { 13939 /* Store the match cursor position */ 13940 match_pos->lnum = curwin->w_cursor.lnum; 13941 match_pos->col = curwin->w_cursor.col + 1; 13942 } 13943 13944 /* If 'n' flag is used or search failed: restore cursor position. */ 13945 if ((flags & SP_NOMOVE) || retval == 0) 13946 curwin->w_cursor = save_cursor; 13947 13948 theend: 13949 vim_free(pat2); 13950 vim_free(pat3); 13951 p_cpo = save_cpo; 13952 13953 return retval; 13954 } 13955 13956 /* 13957 * "searchpos()" function 13958 */ 13959 static void 13960 f_searchpos(argvars, rettv) 13961 typval_T *argvars; 13962 typval_T *rettv; 13963 { 13964 pos_T match_pos; 13965 int lnum = 0; 13966 int col = 0; 13967 int n; 13968 int flags = 0; 13969 13970 rettv->vval.v_number = 0; 13971 13972 if (rettv_list_alloc(rettv) == FAIL) 13973 return; 13974 13975 n = search_cmn(argvars, &match_pos, &flags); 13976 if (n > 0) 13977 { 13978 lnum = match_pos.lnum; 13979 col = match_pos.col; 13980 } 13981 13982 list_append_number(rettv->vval.v_list, (varnumber_T)lnum); 13983 list_append_number(rettv->vval.v_list, (varnumber_T)col); 13984 if (flags & SP_SUBPAT) 13985 list_append_number(rettv->vval.v_list, (varnumber_T)n); 13986 } 13987 13988 13989 /*ARGSUSED*/ 13990 static void 13991 f_server2client(argvars, rettv) 13992 typval_T *argvars; 13993 typval_T *rettv; 13994 { 13995 #ifdef FEAT_CLIENTSERVER 13996 char_u buf[NUMBUFLEN]; 13997 char_u *server = get_tv_string_chk(&argvars[0]); 13998 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf); 13999 14000 rettv->vval.v_number = -1; 14001 if (server == NULL || reply == NULL) 14002 return; 14003 if (check_restricted() || check_secure()) 14004 return; 14005 # ifdef FEAT_X11 14006 if (check_connection() == FAIL) 14007 return; 14008 # endif 14009 14010 if (serverSendReply(server, reply) < 0) 14011 { 14012 EMSG(_("E258: Unable to send to client")); 14013 return; 14014 } 14015 rettv->vval.v_number = 0; 14016 #else 14017 rettv->vval.v_number = -1; 14018 #endif 14019 } 14020 14021 /*ARGSUSED*/ 14022 static void 14023 f_serverlist(argvars, rettv) 14024 typval_T *argvars; 14025 typval_T *rettv; 14026 { 14027 char_u *r = NULL; 14028 14029 #ifdef FEAT_CLIENTSERVER 14030 # ifdef WIN32 14031 r = serverGetVimNames(); 14032 # else 14033 make_connection(); 14034 if (X_DISPLAY != NULL) 14035 r = serverGetVimNames(X_DISPLAY); 14036 # endif 14037 #endif 14038 rettv->v_type = VAR_STRING; 14039 rettv->vval.v_string = r; 14040 } 14041 14042 /* 14043 * "setbufvar()" function 14044 */ 14045 /*ARGSUSED*/ 14046 static void 14047 f_setbufvar(argvars, rettv) 14048 typval_T *argvars; 14049 typval_T *rettv; 14050 { 14051 buf_T *buf; 14052 #ifdef FEAT_AUTOCMD 14053 aco_save_T aco; 14054 #else 14055 buf_T *save_curbuf; 14056 #endif 14057 char_u *varname, *bufvarname; 14058 typval_T *varp; 14059 char_u nbuf[NUMBUFLEN]; 14060 14061 rettv->vval.v_number = 0; 14062 14063 if (check_restricted() || check_secure()) 14064 return; 14065 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 14066 varname = get_tv_string_chk(&argvars[1]); 14067 buf = get_buf_tv(&argvars[0]); 14068 varp = &argvars[2]; 14069 14070 if (buf != NULL && varname != NULL && varp != NULL) 14071 { 14072 /* set curbuf to be our buf, temporarily */ 14073 #ifdef FEAT_AUTOCMD 14074 aucmd_prepbuf(&aco, buf); 14075 #else 14076 save_curbuf = curbuf; 14077 curbuf = buf; 14078 #endif 14079 14080 if (*varname == '&') 14081 { 14082 long numval; 14083 char_u *strval; 14084 int error = FALSE; 14085 14086 ++varname; 14087 numval = get_tv_number_chk(varp, &error); 14088 strval = get_tv_string_buf_chk(varp, nbuf); 14089 if (!error && strval != NULL) 14090 set_option_value(varname, numval, strval, OPT_LOCAL); 14091 } 14092 else 14093 { 14094 bufvarname = alloc((unsigned)STRLEN(varname) + 3); 14095 if (bufvarname != NULL) 14096 { 14097 STRCPY(bufvarname, "b:"); 14098 STRCPY(bufvarname + 2, varname); 14099 set_var(bufvarname, varp, TRUE); 14100 vim_free(bufvarname); 14101 } 14102 } 14103 14104 /* reset notion of buffer */ 14105 #ifdef FEAT_AUTOCMD 14106 aucmd_restbuf(&aco); 14107 #else 14108 curbuf = save_curbuf; 14109 #endif 14110 } 14111 } 14112 14113 /* 14114 * "setcmdpos()" function 14115 */ 14116 static void 14117 f_setcmdpos(argvars, rettv) 14118 typval_T *argvars; 14119 typval_T *rettv; 14120 { 14121 int pos = (int)get_tv_number(&argvars[0]) - 1; 14122 14123 if (pos >= 0) 14124 rettv->vval.v_number = set_cmdline_pos(pos); 14125 } 14126 14127 /* 14128 * "setline()" function 14129 */ 14130 static void 14131 f_setline(argvars, rettv) 14132 typval_T *argvars; 14133 typval_T *rettv; 14134 { 14135 linenr_T lnum; 14136 char_u *line = NULL; 14137 list_T *l = NULL; 14138 listitem_T *li = NULL; 14139 long added = 0; 14140 linenr_T lcount = curbuf->b_ml.ml_line_count; 14141 14142 lnum = get_tv_lnum(&argvars[0]); 14143 if (argvars[1].v_type == VAR_LIST) 14144 { 14145 l = argvars[1].vval.v_list; 14146 li = l->lv_first; 14147 } 14148 else 14149 line = get_tv_string_chk(&argvars[1]); 14150 14151 rettv->vval.v_number = 0; /* OK */ 14152 for (;;) 14153 { 14154 if (l != NULL) 14155 { 14156 /* list argument, get next string */ 14157 if (li == NULL) 14158 break; 14159 line = get_tv_string_chk(&li->li_tv); 14160 li = li->li_next; 14161 } 14162 14163 rettv->vval.v_number = 1; /* FAIL */ 14164 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) 14165 break; 14166 if (lnum <= curbuf->b_ml.ml_line_count) 14167 { 14168 /* existing line, replace it */ 14169 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK) 14170 { 14171 changed_bytes(lnum, 0); 14172 check_cursor_col(); 14173 rettv->vval.v_number = 0; /* OK */ 14174 } 14175 } 14176 else if (added > 0 || u_save(lnum - 1, lnum) == OK) 14177 { 14178 /* lnum is one past the last line, append the line */ 14179 ++added; 14180 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK) 14181 rettv->vval.v_number = 0; /* OK */ 14182 } 14183 14184 if (l == NULL) /* only one string argument */ 14185 break; 14186 ++lnum; 14187 } 14188 14189 if (added > 0) 14190 appended_lines_mark(lcount, added); 14191 } 14192 14193 /* 14194 * Used by "setqflist()" and "setloclist()" functions 14195 */ 14196 /*ARGSUSED*/ 14197 static void 14198 set_qf_ll_list(wp, list_arg, action_arg, rettv) 14199 win_T *wp; 14200 typval_T *list_arg; 14201 typval_T *action_arg; 14202 typval_T *rettv; 14203 { 14204 #ifdef FEAT_QUICKFIX 14205 char_u *act; 14206 int action = ' '; 14207 #endif 14208 14209 rettv->vval.v_number = -1; 14210 14211 #ifdef FEAT_QUICKFIX 14212 if (list_arg->v_type != VAR_LIST) 14213 EMSG(_(e_listreq)); 14214 else 14215 { 14216 list_T *l = list_arg->vval.v_list; 14217 14218 if (action_arg->v_type == VAR_STRING) 14219 { 14220 act = get_tv_string_chk(action_arg); 14221 if (act == NULL) 14222 return; /* type error; errmsg already given */ 14223 if (*act == 'a' || *act == 'r') 14224 action = *act; 14225 } 14226 14227 if (l != NULL && set_errorlist(wp, l, action) == OK) 14228 rettv->vval.v_number = 0; 14229 } 14230 #endif 14231 } 14232 14233 /* 14234 * "setloclist()" function 14235 */ 14236 /*ARGSUSED*/ 14237 static void 14238 f_setloclist(argvars, rettv) 14239 typval_T *argvars; 14240 typval_T *rettv; 14241 { 14242 win_T *win; 14243 14244 rettv->vval.v_number = -1; 14245 14246 win = find_win_by_nr(&argvars[0]); 14247 if (win != NULL) 14248 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv); 14249 } 14250 14251 /* 14252 * "setpos()" function 14253 */ 14254 /*ARGSUSED*/ 14255 static void 14256 f_setpos(argvars, rettv) 14257 typval_T *argvars; 14258 typval_T *rettv; 14259 { 14260 pos_T pos; 14261 int fnum; 14262 char_u *name; 14263 14264 name = get_tv_string_chk(argvars); 14265 if (name != NULL) 14266 { 14267 if (list2fpos(&argvars[1], &pos, &fnum) == OK) 14268 { 14269 --pos.col; 14270 if (name[0] == '.') /* cursor */ 14271 { 14272 if (fnum == curbuf->b_fnum) 14273 { 14274 curwin->w_cursor = pos; 14275 check_cursor(); 14276 } 14277 else 14278 EMSG(_(e_invarg)); 14279 } 14280 else if (name[0] == '\'') /* mark */ 14281 (void)setmark_pos(name[1], &pos, fnum); 14282 else 14283 EMSG(_(e_invarg)); 14284 } 14285 } 14286 } 14287 14288 /* 14289 * "setqflist()" function 14290 */ 14291 /*ARGSUSED*/ 14292 static void 14293 f_setqflist(argvars, rettv) 14294 typval_T *argvars; 14295 typval_T *rettv; 14296 { 14297 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv); 14298 } 14299 14300 /* 14301 * "setreg()" function 14302 */ 14303 static void 14304 f_setreg(argvars, rettv) 14305 typval_T *argvars; 14306 typval_T *rettv; 14307 { 14308 int regname; 14309 char_u *strregname; 14310 char_u *stropt; 14311 char_u *strval; 14312 int append; 14313 char_u yank_type; 14314 long block_len; 14315 14316 block_len = -1; 14317 yank_type = MAUTO; 14318 append = FALSE; 14319 14320 strregname = get_tv_string_chk(argvars); 14321 rettv->vval.v_number = 1; /* FAIL is default */ 14322 14323 if (strregname == NULL) 14324 return; /* type error; errmsg already given */ 14325 regname = *strregname; 14326 if (regname == 0 || regname == '@') 14327 regname = '"'; 14328 else if (regname == '=') 14329 return; 14330 14331 if (argvars[2].v_type != VAR_UNKNOWN) 14332 { 14333 stropt = get_tv_string_chk(&argvars[2]); 14334 if (stropt == NULL) 14335 return; /* type error */ 14336 for (; *stropt != NUL; ++stropt) 14337 switch (*stropt) 14338 { 14339 case 'a': case 'A': /* append */ 14340 append = TRUE; 14341 break; 14342 case 'v': case 'c': /* character-wise selection */ 14343 yank_type = MCHAR; 14344 break; 14345 case 'V': case 'l': /* line-wise selection */ 14346 yank_type = MLINE; 14347 break; 14348 #ifdef FEAT_VISUAL 14349 case 'b': case Ctrl_V: /* block-wise selection */ 14350 yank_type = MBLOCK; 14351 if (VIM_ISDIGIT(stropt[1])) 14352 { 14353 ++stropt; 14354 block_len = getdigits(&stropt) - 1; 14355 --stropt; 14356 } 14357 break; 14358 #endif 14359 } 14360 } 14361 14362 strval = get_tv_string_chk(&argvars[1]); 14363 if (strval != NULL) 14364 write_reg_contents_ex(regname, strval, -1, 14365 append, yank_type, block_len); 14366 rettv->vval.v_number = 0; 14367 } 14368 14369 14370 /* 14371 * "setwinvar(expr)" function 14372 */ 14373 /*ARGSUSED*/ 14374 static void 14375 f_setwinvar(argvars, rettv) 14376 typval_T *argvars; 14377 typval_T *rettv; 14378 { 14379 win_T *win; 14380 #ifdef FEAT_WINDOWS 14381 win_T *save_curwin; 14382 #endif 14383 char_u *varname, *winvarname; 14384 typval_T *varp; 14385 char_u nbuf[NUMBUFLEN]; 14386 14387 rettv->vval.v_number = 0; 14388 14389 if (check_restricted() || check_secure()) 14390 return; 14391 win = find_win_by_nr(&argvars[0]); 14392 varname = get_tv_string_chk(&argvars[1]); 14393 varp = &argvars[2]; 14394 14395 if (win != NULL && varname != NULL && varp != NULL) 14396 { 14397 #ifdef FEAT_WINDOWS 14398 /* set curwin to be our win, temporarily */ 14399 save_curwin = curwin; 14400 curwin = win; 14401 curbuf = curwin->w_buffer; 14402 #endif 14403 14404 if (*varname == '&') 14405 { 14406 long numval; 14407 char_u *strval; 14408 int error = FALSE; 14409 14410 ++varname; 14411 numval = get_tv_number_chk(varp, &error); 14412 strval = get_tv_string_buf_chk(varp, nbuf); 14413 if (!error && strval != NULL) 14414 set_option_value(varname, numval, strval, OPT_LOCAL); 14415 } 14416 else 14417 { 14418 winvarname = alloc((unsigned)STRLEN(varname) + 3); 14419 if (winvarname != NULL) 14420 { 14421 STRCPY(winvarname, "w:"); 14422 STRCPY(winvarname + 2, varname); 14423 set_var(winvarname, varp, TRUE); 14424 vim_free(winvarname); 14425 } 14426 } 14427 14428 #ifdef FEAT_WINDOWS 14429 /* Restore current window, if it's still valid (autocomands can make 14430 * it invalid). */ 14431 if (win_valid(save_curwin)) 14432 { 14433 curwin = save_curwin; 14434 curbuf = curwin->w_buffer; 14435 } 14436 #endif 14437 } 14438 } 14439 14440 /* 14441 * "simplify()" function 14442 */ 14443 static void 14444 f_simplify(argvars, rettv) 14445 typval_T *argvars; 14446 typval_T *rettv; 14447 { 14448 char_u *p; 14449 14450 p = get_tv_string(&argvars[0]); 14451 rettv->vval.v_string = vim_strsave(p); 14452 simplify_filename(rettv->vval.v_string); /* simplify in place */ 14453 rettv->v_type = VAR_STRING; 14454 } 14455 14456 static int 14457 #ifdef __BORLANDC__ 14458 _RTLENTRYF 14459 #endif 14460 item_compare __ARGS((const void *s1, const void *s2)); 14461 static int 14462 #ifdef __BORLANDC__ 14463 _RTLENTRYF 14464 #endif 14465 item_compare2 __ARGS((const void *s1, const void *s2)); 14466 14467 static int item_compare_ic; 14468 static char_u *item_compare_func; 14469 static int item_compare_func_err; 14470 #define ITEM_COMPARE_FAIL 999 14471 14472 /* 14473 * Compare functions for f_sort() below. 14474 */ 14475 static int 14476 #ifdef __BORLANDC__ 14477 _RTLENTRYF 14478 #endif 14479 item_compare(s1, s2) 14480 const void *s1; 14481 const void *s2; 14482 { 14483 char_u *p1, *p2; 14484 char_u *tofree1, *tofree2; 14485 int res; 14486 char_u numbuf1[NUMBUFLEN]; 14487 char_u numbuf2[NUMBUFLEN]; 14488 14489 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0); 14490 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0); 14491 if (item_compare_ic) 14492 res = STRICMP(p1, p2); 14493 else 14494 res = STRCMP(p1, p2); 14495 vim_free(tofree1); 14496 vim_free(tofree2); 14497 return res; 14498 } 14499 14500 static int 14501 #ifdef __BORLANDC__ 14502 _RTLENTRYF 14503 #endif 14504 item_compare2(s1, s2) 14505 const void *s1; 14506 const void *s2; 14507 { 14508 int res; 14509 typval_T rettv; 14510 typval_T argv[2]; 14511 int dummy; 14512 14513 /* shortcut after failure in previous call; compare all items equal */ 14514 if (item_compare_func_err) 14515 return 0; 14516 14517 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED 14518 * in the copy without changing the original list items. */ 14519 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]); 14520 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]); 14521 14522 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 14523 res = call_func(item_compare_func, STRLEN(item_compare_func), 14524 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL); 14525 clear_tv(&argv[0]); 14526 clear_tv(&argv[1]); 14527 14528 if (res == FAIL) 14529 res = ITEM_COMPARE_FAIL; 14530 else 14531 /* return value has wrong type */ 14532 res = get_tv_number_chk(&rettv, &item_compare_func_err); 14533 if (item_compare_func_err) 14534 res = ITEM_COMPARE_FAIL; 14535 clear_tv(&rettv); 14536 return res; 14537 } 14538 14539 /* 14540 * "sort({list})" function 14541 */ 14542 static void 14543 f_sort(argvars, rettv) 14544 typval_T *argvars; 14545 typval_T *rettv; 14546 { 14547 list_T *l; 14548 listitem_T *li; 14549 listitem_T **ptrs; 14550 long len; 14551 long i; 14552 14553 rettv->vval.v_number = 0; 14554 if (argvars[0].v_type != VAR_LIST) 14555 EMSG2(_(e_listarg), "sort()"); 14556 else 14557 { 14558 l = argvars[0].vval.v_list; 14559 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()")) 14560 return; 14561 rettv->vval.v_list = l; 14562 rettv->v_type = VAR_LIST; 14563 ++l->lv_refcount; 14564 14565 len = list_len(l); 14566 if (len <= 1) 14567 return; /* short list sorts pretty quickly */ 14568 14569 item_compare_ic = FALSE; 14570 item_compare_func = NULL; 14571 if (argvars[1].v_type != VAR_UNKNOWN) 14572 { 14573 if (argvars[1].v_type == VAR_FUNC) 14574 item_compare_func = argvars[1].vval.v_string; 14575 else 14576 { 14577 int error = FALSE; 14578 14579 i = get_tv_number_chk(&argvars[1], &error); 14580 if (error) 14581 return; /* type error; errmsg already given */ 14582 if (i == 1) 14583 item_compare_ic = TRUE; 14584 else 14585 item_compare_func = get_tv_string(&argvars[1]); 14586 } 14587 } 14588 14589 /* Make an array with each entry pointing to an item in the List. */ 14590 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *))); 14591 if (ptrs == NULL) 14592 return; 14593 i = 0; 14594 for (li = l->lv_first; li != NULL; li = li->li_next) 14595 ptrs[i++] = li; 14596 14597 item_compare_func_err = FALSE; 14598 /* test the compare function */ 14599 if (item_compare_func != NULL 14600 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1]) 14601 == ITEM_COMPARE_FAIL) 14602 EMSG(_("E702: Sort compare function failed")); 14603 else 14604 { 14605 /* Sort the array with item pointers. */ 14606 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *), 14607 item_compare_func == NULL ? item_compare : item_compare2); 14608 14609 if (!item_compare_func_err) 14610 { 14611 /* Clear the List and append the items in the sorted order. */ 14612 l->lv_first = l->lv_last = NULL; 14613 l->lv_len = 0; 14614 for (i = 0; i < len; ++i) 14615 list_append(l, ptrs[i]); 14616 } 14617 } 14618 14619 vim_free(ptrs); 14620 } 14621 } 14622 14623 /* 14624 * "soundfold({word})" function 14625 */ 14626 static void 14627 f_soundfold(argvars, rettv) 14628 typval_T *argvars; 14629 typval_T *rettv; 14630 { 14631 char_u *s; 14632 14633 rettv->v_type = VAR_STRING; 14634 s = get_tv_string(&argvars[0]); 14635 #ifdef FEAT_SPELL 14636 rettv->vval.v_string = eval_soundfold(s); 14637 #else 14638 rettv->vval.v_string = vim_strsave(s); 14639 #endif 14640 } 14641 14642 /* 14643 * "spellbadword()" function 14644 */ 14645 /* ARGSUSED */ 14646 static void 14647 f_spellbadword(argvars, rettv) 14648 typval_T *argvars; 14649 typval_T *rettv; 14650 { 14651 char_u *word = (char_u *)""; 14652 hlf_T attr = HLF_COUNT; 14653 int len = 0; 14654 14655 if (rettv_list_alloc(rettv) == FAIL) 14656 return; 14657 14658 #ifdef FEAT_SPELL 14659 if (argvars[0].v_type == VAR_UNKNOWN) 14660 { 14661 /* Find the start and length of the badly spelled word. */ 14662 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr); 14663 if (len != 0) 14664 word = ml_get_cursor(); 14665 } 14666 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) 14667 { 14668 char_u *str = get_tv_string_chk(&argvars[0]); 14669 int capcol = -1; 14670 14671 if (str != NULL) 14672 { 14673 /* Check the argument for spelling. */ 14674 while (*str != NUL) 14675 { 14676 len = spell_check(curwin, str, &attr, &capcol, FALSE); 14677 if (attr != HLF_COUNT) 14678 { 14679 word = str; 14680 break; 14681 } 14682 str += len; 14683 } 14684 } 14685 } 14686 #endif 14687 14688 list_append_string(rettv->vval.v_list, word, len); 14689 list_append_string(rettv->vval.v_list, (char_u *)( 14690 attr == HLF_SPB ? "bad" : 14691 attr == HLF_SPR ? "rare" : 14692 attr == HLF_SPL ? "local" : 14693 attr == HLF_SPC ? "caps" : 14694 ""), -1); 14695 } 14696 14697 /* 14698 * "spellsuggest()" function 14699 */ 14700 /*ARGSUSED*/ 14701 static void 14702 f_spellsuggest(argvars, rettv) 14703 typval_T *argvars; 14704 typval_T *rettv; 14705 { 14706 #ifdef FEAT_SPELL 14707 char_u *str; 14708 int typeerr = FALSE; 14709 int maxcount; 14710 garray_T ga; 14711 int i; 14712 listitem_T *li; 14713 int need_capital = FALSE; 14714 #endif 14715 14716 if (rettv_list_alloc(rettv) == FAIL) 14717 return; 14718 14719 #ifdef FEAT_SPELL 14720 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) 14721 { 14722 str = get_tv_string(&argvars[0]); 14723 if (argvars[1].v_type != VAR_UNKNOWN) 14724 { 14725 maxcount = get_tv_number_chk(&argvars[1], &typeerr); 14726 if (maxcount <= 0) 14727 return; 14728 if (argvars[2].v_type != VAR_UNKNOWN) 14729 { 14730 need_capital = get_tv_number_chk(&argvars[2], &typeerr); 14731 if (typeerr) 14732 return; 14733 } 14734 } 14735 else 14736 maxcount = 25; 14737 14738 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE); 14739 14740 for (i = 0; i < ga.ga_len; ++i) 14741 { 14742 str = ((char_u **)ga.ga_data)[i]; 14743 14744 li = listitem_alloc(); 14745 if (li == NULL) 14746 vim_free(str); 14747 else 14748 { 14749 li->li_tv.v_type = VAR_STRING; 14750 li->li_tv.v_lock = 0; 14751 li->li_tv.vval.v_string = str; 14752 list_append(rettv->vval.v_list, li); 14753 } 14754 } 14755 ga_clear(&ga); 14756 } 14757 #endif 14758 } 14759 14760 static void 14761 f_split(argvars, rettv) 14762 typval_T *argvars; 14763 typval_T *rettv; 14764 { 14765 char_u *str; 14766 char_u *end; 14767 char_u *pat = NULL; 14768 regmatch_T regmatch; 14769 char_u patbuf[NUMBUFLEN]; 14770 char_u *save_cpo; 14771 int match; 14772 colnr_T col = 0; 14773 int keepempty = FALSE; 14774 int typeerr = FALSE; 14775 14776 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 14777 save_cpo = p_cpo; 14778 p_cpo = (char_u *)""; 14779 14780 str = get_tv_string(&argvars[0]); 14781 if (argvars[1].v_type != VAR_UNKNOWN) 14782 { 14783 pat = get_tv_string_buf_chk(&argvars[1], patbuf); 14784 if (pat == NULL) 14785 typeerr = TRUE; 14786 if (argvars[2].v_type != VAR_UNKNOWN) 14787 keepempty = get_tv_number_chk(&argvars[2], &typeerr); 14788 } 14789 if (pat == NULL || *pat == NUL) 14790 pat = (char_u *)"[\\x01- ]\\+"; 14791 14792 if (rettv_list_alloc(rettv) == FAIL) 14793 return; 14794 if (typeerr) 14795 return; 14796 14797 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 14798 if (regmatch.regprog != NULL) 14799 { 14800 regmatch.rm_ic = FALSE; 14801 while (*str != NUL || keepempty) 14802 { 14803 if (*str == NUL) 14804 match = FALSE; /* empty item at the end */ 14805 else 14806 match = vim_regexec_nl(®match, str, col); 14807 if (match) 14808 end = regmatch.startp[0]; 14809 else 14810 end = str + STRLEN(str); 14811 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0 14812 && *str != NUL && match && end < regmatch.endp[0])) 14813 { 14814 if (list_append_string(rettv->vval.v_list, str, 14815 (int)(end - str)) == FAIL) 14816 break; 14817 } 14818 if (!match) 14819 break; 14820 /* Advance to just after the match. */ 14821 if (regmatch.endp[0] > str) 14822 col = 0; 14823 else 14824 { 14825 /* Don't get stuck at the same match. */ 14826 #ifdef FEAT_MBYTE 14827 col = (*mb_ptr2len)(regmatch.endp[0]); 14828 #else 14829 col = 1; 14830 #endif 14831 } 14832 str = regmatch.endp[0]; 14833 } 14834 14835 vim_free(regmatch.regprog); 14836 } 14837 14838 p_cpo = save_cpo; 14839 } 14840 14841 /* 14842 * "str2nr()" function 14843 */ 14844 static void 14845 f_str2nr(argvars, rettv) 14846 typval_T *argvars; 14847 typval_T *rettv; 14848 { 14849 int base = 10; 14850 char_u *p; 14851 long n; 14852 14853 if (argvars[1].v_type != VAR_UNKNOWN) 14854 { 14855 base = get_tv_number(&argvars[1]); 14856 if (base != 8 && base != 10 && base != 16) 14857 { 14858 EMSG(_(e_invarg)); 14859 return; 14860 } 14861 } 14862 14863 p = skipwhite(get_tv_string(&argvars[0])); 14864 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL); 14865 rettv->vval.v_number = n; 14866 } 14867 14868 #ifdef HAVE_STRFTIME 14869 /* 14870 * "strftime({format}[, {time}])" function 14871 */ 14872 static void 14873 f_strftime(argvars, rettv) 14874 typval_T *argvars; 14875 typval_T *rettv; 14876 { 14877 char_u result_buf[256]; 14878 struct tm *curtime; 14879 time_t seconds; 14880 char_u *p; 14881 14882 rettv->v_type = VAR_STRING; 14883 14884 p = get_tv_string(&argvars[0]); 14885 if (argvars[1].v_type == VAR_UNKNOWN) 14886 seconds = time(NULL); 14887 else 14888 seconds = (time_t)get_tv_number(&argvars[1]); 14889 curtime = localtime(&seconds); 14890 /* MSVC returns NULL for an invalid value of seconds. */ 14891 if (curtime == NULL) 14892 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)")); 14893 else 14894 { 14895 # ifdef FEAT_MBYTE 14896 vimconv_T conv; 14897 char_u *enc; 14898 14899 conv.vc_type = CONV_NONE; 14900 enc = enc_locale(); 14901 convert_setup(&conv, p_enc, enc); 14902 if (conv.vc_type != CONV_NONE) 14903 p = string_convert(&conv, p, NULL); 14904 # endif 14905 if (p != NULL) 14906 (void)strftime((char *)result_buf, sizeof(result_buf), 14907 (char *)p, curtime); 14908 else 14909 result_buf[0] = NUL; 14910 14911 # ifdef FEAT_MBYTE 14912 if (conv.vc_type != CONV_NONE) 14913 vim_free(p); 14914 convert_setup(&conv, enc, p_enc); 14915 if (conv.vc_type != CONV_NONE) 14916 rettv->vval.v_string = string_convert(&conv, result_buf, NULL); 14917 else 14918 # endif 14919 rettv->vval.v_string = vim_strsave(result_buf); 14920 14921 # ifdef FEAT_MBYTE 14922 /* Release conversion descriptors */ 14923 convert_setup(&conv, NULL, NULL); 14924 vim_free(enc); 14925 # endif 14926 } 14927 } 14928 #endif 14929 14930 /* 14931 * "stridx()" function 14932 */ 14933 static void 14934 f_stridx(argvars, rettv) 14935 typval_T *argvars; 14936 typval_T *rettv; 14937 { 14938 char_u buf[NUMBUFLEN]; 14939 char_u *needle; 14940 char_u *haystack; 14941 char_u *save_haystack; 14942 char_u *pos; 14943 int start_idx; 14944 14945 needle = get_tv_string_chk(&argvars[1]); 14946 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf); 14947 rettv->vval.v_number = -1; 14948 if (needle == NULL || haystack == NULL) 14949 return; /* type error; errmsg already given */ 14950 14951 if (argvars[2].v_type != VAR_UNKNOWN) 14952 { 14953 int error = FALSE; 14954 14955 start_idx = get_tv_number_chk(&argvars[2], &error); 14956 if (error || start_idx >= (int)STRLEN(haystack)) 14957 return; 14958 if (start_idx >= 0) 14959 haystack += start_idx; 14960 } 14961 14962 pos = (char_u *)strstr((char *)haystack, (char *)needle); 14963 if (pos != NULL) 14964 rettv->vval.v_number = (varnumber_T)(pos - save_haystack); 14965 } 14966 14967 /* 14968 * "string()" function 14969 */ 14970 static void 14971 f_string(argvars, rettv) 14972 typval_T *argvars; 14973 typval_T *rettv; 14974 { 14975 char_u *tofree; 14976 char_u numbuf[NUMBUFLEN]; 14977 14978 rettv->v_type = VAR_STRING; 14979 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0); 14980 if (tofree == NULL) 14981 rettv->vval.v_string = vim_strsave(rettv->vval.v_string); 14982 } 14983 14984 /* 14985 * "strlen()" function 14986 */ 14987 static void 14988 f_strlen(argvars, rettv) 14989 typval_T *argvars; 14990 typval_T *rettv; 14991 { 14992 rettv->vval.v_number = (varnumber_T)(STRLEN( 14993 get_tv_string(&argvars[0]))); 14994 } 14995 14996 /* 14997 * "strpart()" function 14998 */ 14999 static void 15000 f_strpart(argvars, rettv) 15001 typval_T *argvars; 15002 typval_T *rettv; 15003 { 15004 char_u *p; 15005 int n; 15006 int len; 15007 int slen; 15008 int error = FALSE; 15009 15010 p = get_tv_string(&argvars[0]); 15011 slen = (int)STRLEN(p); 15012 15013 n = get_tv_number_chk(&argvars[1], &error); 15014 if (error) 15015 len = 0; 15016 else if (argvars[2].v_type != VAR_UNKNOWN) 15017 len = get_tv_number(&argvars[2]); 15018 else 15019 len = slen - n; /* default len: all bytes that are available. */ 15020 15021 /* 15022 * Only return the overlap between the specified part and the actual 15023 * string. 15024 */ 15025 if (n < 0) 15026 { 15027 len += n; 15028 n = 0; 15029 } 15030 else if (n > slen) 15031 n = slen; 15032 if (len < 0) 15033 len = 0; 15034 else if (n + len > slen) 15035 len = slen - n; 15036 15037 rettv->v_type = VAR_STRING; 15038 rettv->vval.v_string = vim_strnsave(p + n, len); 15039 } 15040 15041 /* 15042 * "strridx()" function 15043 */ 15044 static void 15045 f_strridx(argvars, rettv) 15046 typval_T *argvars; 15047 typval_T *rettv; 15048 { 15049 char_u buf[NUMBUFLEN]; 15050 char_u *needle; 15051 char_u *haystack; 15052 char_u *rest; 15053 char_u *lastmatch = NULL; 15054 int haystack_len, end_idx; 15055 15056 needle = get_tv_string_chk(&argvars[1]); 15057 haystack = get_tv_string_buf_chk(&argvars[0], buf); 15058 haystack_len = STRLEN(haystack); 15059 15060 rettv->vval.v_number = -1; 15061 if (needle == NULL || haystack == NULL) 15062 return; /* type error; errmsg already given */ 15063 if (argvars[2].v_type != VAR_UNKNOWN) 15064 { 15065 /* Third argument: upper limit for index */ 15066 end_idx = get_tv_number_chk(&argvars[2], NULL); 15067 if (end_idx < 0) 15068 return; /* can never find a match */ 15069 } 15070 else 15071 end_idx = haystack_len; 15072 15073 if (*needle == NUL) 15074 { 15075 /* Empty string matches past the end. */ 15076 lastmatch = haystack + end_idx; 15077 } 15078 else 15079 { 15080 for (rest = haystack; *rest != '\0'; ++rest) 15081 { 15082 rest = (char_u *)strstr((char *)rest, (char *)needle); 15083 if (rest == NULL || rest > haystack + end_idx) 15084 break; 15085 lastmatch = rest; 15086 } 15087 } 15088 15089 if (lastmatch == NULL) 15090 rettv->vval.v_number = -1; 15091 else 15092 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack); 15093 } 15094 15095 /* 15096 * "strtrans()" function 15097 */ 15098 static void 15099 f_strtrans(argvars, rettv) 15100 typval_T *argvars; 15101 typval_T *rettv; 15102 { 15103 rettv->v_type = VAR_STRING; 15104 rettv->vval.v_string = transstr(get_tv_string(&argvars[0])); 15105 } 15106 15107 /* 15108 * "submatch()" function 15109 */ 15110 static void 15111 f_submatch(argvars, rettv) 15112 typval_T *argvars; 15113 typval_T *rettv; 15114 { 15115 rettv->v_type = VAR_STRING; 15116 rettv->vval.v_string = 15117 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL)); 15118 } 15119 15120 /* 15121 * "substitute()" function 15122 */ 15123 static void 15124 f_substitute(argvars, rettv) 15125 typval_T *argvars; 15126 typval_T *rettv; 15127 { 15128 char_u patbuf[NUMBUFLEN]; 15129 char_u subbuf[NUMBUFLEN]; 15130 char_u flagsbuf[NUMBUFLEN]; 15131 15132 char_u *str = get_tv_string_chk(&argvars[0]); 15133 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf); 15134 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf); 15135 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf); 15136 15137 rettv->v_type = VAR_STRING; 15138 if (str == NULL || pat == NULL || sub == NULL || flg == NULL) 15139 rettv->vval.v_string = NULL; 15140 else 15141 rettv->vval.v_string = do_string_sub(str, pat, sub, flg); 15142 } 15143 15144 /* 15145 * "synID(lnum, col, trans)" function 15146 */ 15147 /*ARGSUSED*/ 15148 static void 15149 f_synID(argvars, rettv) 15150 typval_T *argvars; 15151 typval_T *rettv; 15152 { 15153 int id = 0; 15154 #ifdef FEAT_SYN_HL 15155 long lnum; 15156 long col; 15157 int trans; 15158 int transerr = FALSE; 15159 15160 lnum = get_tv_lnum(argvars); /* -1 on type error */ 15161 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */ 15162 trans = get_tv_number_chk(&argvars[2], &transerr); 15163 15164 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count 15165 && col >= 0 && col < (long)STRLEN(ml_get(lnum))) 15166 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL); 15167 #endif 15168 15169 rettv->vval.v_number = id; 15170 } 15171 15172 /* 15173 * "synIDattr(id, what [, mode])" function 15174 */ 15175 /*ARGSUSED*/ 15176 static void 15177 f_synIDattr(argvars, rettv) 15178 typval_T *argvars; 15179 typval_T *rettv; 15180 { 15181 char_u *p = NULL; 15182 #ifdef FEAT_SYN_HL 15183 int id; 15184 char_u *what; 15185 char_u *mode; 15186 char_u modebuf[NUMBUFLEN]; 15187 int modec; 15188 15189 id = get_tv_number(&argvars[0]); 15190 what = get_tv_string(&argvars[1]); 15191 if (argvars[2].v_type != VAR_UNKNOWN) 15192 { 15193 mode = get_tv_string_buf(&argvars[2], modebuf); 15194 modec = TOLOWER_ASC(mode[0]); 15195 if (modec != 't' && modec != 'c' 15196 #ifdef FEAT_GUI 15197 && modec != 'g' 15198 #endif 15199 ) 15200 modec = 0; /* replace invalid with current */ 15201 } 15202 else 15203 { 15204 #ifdef FEAT_GUI 15205 if (gui.in_use) 15206 modec = 'g'; 15207 else 15208 #endif 15209 if (t_colors > 1) 15210 modec = 'c'; 15211 else 15212 modec = 't'; 15213 } 15214 15215 15216 switch (TOLOWER_ASC(what[0])) 15217 { 15218 case 'b': 15219 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */ 15220 p = highlight_color(id, what, modec); 15221 else /* bold */ 15222 p = highlight_has_attr(id, HL_BOLD, modec); 15223 break; 15224 15225 case 'f': /* fg[#] */ 15226 p = highlight_color(id, what, modec); 15227 break; 15228 15229 case 'i': 15230 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */ 15231 p = highlight_has_attr(id, HL_INVERSE, modec); 15232 else /* italic */ 15233 p = highlight_has_attr(id, HL_ITALIC, modec); 15234 break; 15235 15236 case 'n': /* name */ 15237 p = get_highlight_name(NULL, id - 1); 15238 break; 15239 15240 case 'r': /* reverse */ 15241 p = highlight_has_attr(id, HL_INVERSE, modec); 15242 break; 15243 15244 case 's': /* standout */ 15245 p = highlight_has_attr(id, HL_STANDOUT, modec); 15246 break; 15247 15248 case 'u': 15249 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c') 15250 /* underline */ 15251 p = highlight_has_attr(id, HL_UNDERLINE, modec); 15252 else 15253 /* undercurl */ 15254 p = highlight_has_attr(id, HL_UNDERCURL, modec); 15255 break; 15256 } 15257 15258 if (p != NULL) 15259 p = vim_strsave(p); 15260 #endif 15261 rettv->v_type = VAR_STRING; 15262 rettv->vval.v_string = p; 15263 } 15264 15265 /* 15266 * "synIDtrans(id)" function 15267 */ 15268 /*ARGSUSED*/ 15269 static void 15270 f_synIDtrans(argvars, rettv) 15271 typval_T *argvars; 15272 typval_T *rettv; 15273 { 15274 int id; 15275 15276 #ifdef FEAT_SYN_HL 15277 id = get_tv_number(&argvars[0]); 15278 15279 if (id > 0) 15280 id = syn_get_final_id(id); 15281 else 15282 #endif 15283 id = 0; 15284 15285 rettv->vval.v_number = id; 15286 } 15287 15288 /* 15289 * "system()" function 15290 */ 15291 static void 15292 f_system(argvars, rettv) 15293 typval_T *argvars; 15294 typval_T *rettv; 15295 { 15296 char_u *res = NULL; 15297 char_u *p; 15298 char_u *infile = NULL; 15299 char_u buf[NUMBUFLEN]; 15300 int err = FALSE; 15301 FILE *fd; 15302 15303 if (argvars[1].v_type != VAR_UNKNOWN) 15304 { 15305 /* 15306 * Write the string to a temp file, to be used for input of the shell 15307 * command. 15308 */ 15309 if ((infile = vim_tempname('i')) == NULL) 15310 { 15311 EMSG(_(e_notmp)); 15312 return; 15313 } 15314 15315 fd = mch_fopen((char *)infile, WRITEBIN); 15316 if (fd == NULL) 15317 { 15318 EMSG2(_(e_notopen), infile); 15319 goto done; 15320 } 15321 p = get_tv_string_buf_chk(&argvars[1], buf); 15322 if (p == NULL) 15323 goto done; /* type error; errmsg already given */ 15324 if (fwrite(p, STRLEN(p), 1, fd) != 1) 15325 err = TRUE; 15326 if (fclose(fd) != 0) 15327 err = TRUE; 15328 if (err) 15329 { 15330 EMSG(_("E677: Error writing temp file")); 15331 goto done; 15332 } 15333 } 15334 15335 res = get_cmd_output(get_tv_string(&argvars[0]), infile, 15336 SHELL_SILENT | SHELL_COOKED); 15337 15338 #ifdef USE_CR 15339 /* translate <CR> into <NL> */ 15340 if (res != NULL) 15341 { 15342 char_u *s; 15343 15344 for (s = res; *s; ++s) 15345 { 15346 if (*s == CAR) 15347 *s = NL; 15348 } 15349 } 15350 #else 15351 # ifdef USE_CRNL 15352 /* translate <CR><NL> into <NL> */ 15353 if (res != NULL) 15354 { 15355 char_u *s, *d; 15356 15357 d = res; 15358 for (s = res; *s; ++s) 15359 { 15360 if (s[0] == CAR && s[1] == NL) 15361 ++s; 15362 *d++ = *s; 15363 } 15364 *d = NUL; 15365 } 15366 # endif 15367 #endif 15368 15369 done: 15370 if (infile != NULL) 15371 { 15372 mch_remove(infile); 15373 vim_free(infile); 15374 } 15375 rettv->v_type = VAR_STRING; 15376 rettv->vval.v_string = res; 15377 } 15378 15379 /* 15380 * "tabpagebuflist()" function 15381 */ 15382 /* ARGSUSED */ 15383 static void 15384 f_tabpagebuflist(argvars, rettv) 15385 typval_T *argvars; 15386 typval_T *rettv; 15387 { 15388 #ifndef FEAT_WINDOWS 15389 rettv->vval.v_number = 0; 15390 #else 15391 tabpage_T *tp; 15392 win_T *wp = NULL; 15393 15394 if (argvars[0].v_type == VAR_UNKNOWN) 15395 wp = firstwin; 15396 else 15397 { 15398 tp = find_tabpage((int)get_tv_number(&argvars[0])); 15399 if (tp != NULL) 15400 wp = (tp == curtab) ? firstwin : tp->tp_firstwin; 15401 } 15402 if (wp == NULL) 15403 rettv->vval.v_number = 0; 15404 else 15405 { 15406 if (rettv_list_alloc(rettv) == FAIL) 15407 rettv->vval.v_number = 0; 15408 else 15409 { 15410 for (; wp != NULL; wp = wp->w_next) 15411 if (list_append_number(rettv->vval.v_list, 15412 wp->w_buffer->b_fnum) == FAIL) 15413 break; 15414 } 15415 } 15416 #endif 15417 } 15418 15419 15420 /* 15421 * "tabpagenr()" function 15422 */ 15423 /* ARGSUSED */ 15424 static void 15425 f_tabpagenr(argvars, rettv) 15426 typval_T *argvars; 15427 typval_T *rettv; 15428 { 15429 int nr = 1; 15430 #ifdef FEAT_WINDOWS 15431 char_u *arg; 15432 15433 if (argvars[0].v_type != VAR_UNKNOWN) 15434 { 15435 arg = get_tv_string_chk(&argvars[0]); 15436 nr = 0; 15437 if (arg != NULL) 15438 { 15439 if (STRCMP(arg, "$") == 0) 15440 nr = tabpage_index(NULL) - 1; 15441 else 15442 EMSG2(_(e_invexpr2), arg); 15443 } 15444 } 15445 else 15446 nr = tabpage_index(curtab); 15447 #endif 15448 rettv->vval.v_number = nr; 15449 } 15450 15451 15452 #ifdef FEAT_WINDOWS 15453 static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar)); 15454 15455 /* 15456 * Common code for tabpagewinnr() and winnr(). 15457 */ 15458 static int 15459 get_winnr(tp, argvar) 15460 tabpage_T *tp; 15461 typval_T *argvar; 15462 { 15463 win_T *twin; 15464 int nr = 1; 15465 win_T *wp; 15466 char_u *arg; 15467 15468 twin = (tp == curtab) ? curwin : tp->tp_curwin; 15469 if (argvar->v_type != VAR_UNKNOWN) 15470 { 15471 arg = get_tv_string_chk(argvar); 15472 if (arg == NULL) 15473 nr = 0; /* type error; errmsg already given */ 15474 else if (STRCMP(arg, "$") == 0) 15475 twin = (tp == curtab) ? lastwin : tp->tp_lastwin; 15476 else if (STRCMP(arg, "#") == 0) 15477 { 15478 twin = (tp == curtab) ? prevwin : tp->tp_prevwin; 15479 if (twin == NULL) 15480 nr = 0; 15481 } 15482 else 15483 { 15484 EMSG2(_(e_invexpr2), arg); 15485 nr = 0; 15486 } 15487 } 15488 15489 if (nr > 0) 15490 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin; 15491 wp != twin; wp = wp->w_next) 15492 ++nr; 15493 return nr; 15494 } 15495 #endif 15496 15497 /* 15498 * "tabpagewinnr()" function 15499 */ 15500 /* ARGSUSED */ 15501 static void 15502 f_tabpagewinnr(argvars, rettv) 15503 typval_T *argvars; 15504 typval_T *rettv; 15505 { 15506 int nr = 1; 15507 #ifdef FEAT_WINDOWS 15508 tabpage_T *tp; 15509 15510 tp = find_tabpage((int)get_tv_number(&argvars[0])); 15511 if (tp == NULL) 15512 nr = 0; 15513 else 15514 nr = get_winnr(tp, &argvars[1]); 15515 #endif 15516 rettv->vval.v_number = nr; 15517 } 15518 15519 15520 /* 15521 * "tagfiles()" function 15522 */ 15523 /*ARGSUSED*/ 15524 static void 15525 f_tagfiles(argvars, rettv) 15526 typval_T *argvars; 15527 typval_T *rettv; 15528 { 15529 char_u fname[MAXPATHL + 1]; 15530 tagname_T tn; 15531 int first; 15532 15533 if (rettv_list_alloc(rettv) == FAIL) 15534 { 15535 rettv->vval.v_number = 0; 15536 return; 15537 } 15538 15539 for (first = TRUE; ; first = FALSE) 15540 if (get_tagfname(&tn, first, fname) == FAIL 15541 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL) 15542 break; 15543 tagname_free(&tn); 15544 } 15545 15546 /* 15547 * "taglist()" function 15548 */ 15549 static void 15550 f_taglist(argvars, rettv) 15551 typval_T *argvars; 15552 typval_T *rettv; 15553 { 15554 char_u *tag_pattern; 15555 15556 tag_pattern = get_tv_string(&argvars[0]); 15557 15558 rettv->vval.v_number = FALSE; 15559 if (*tag_pattern == NUL) 15560 return; 15561 15562 if (rettv_list_alloc(rettv) == OK) 15563 (void)get_tags(rettv->vval.v_list, tag_pattern); 15564 } 15565 15566 /* 15567 * "tempname()" function 15568 */ 15569 /*ARGSUSED*/ 15570 static void 15571 f_tempname(argvars, rettv) 15572 typval_T *argvars; 15573 typval_T *rettv; 15574 { 15575 static int x = 'A'; 15576 15577 rettv->v_type = VAR_STRING; 15578 rettv->vval.v_string = vim_tempname(x); 15579 15580 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different 15581 * names. Skip 'I' and 'O', they are used for shell redirection. */ 15582 do 15583 { 15584 if (x == 'Z') 15585 x = '0'; 15586 else if (x == '9') 15587 x = 'A'; 15588 else 15589 { 15590 #ifdef EBCDIC 15591 if (x == 'I') 15592 x = 'J'; 15593 else if (x == 'R') 15594 x = 'S'; 15595 else 15596 #endif 15597 ++x; 15598 } 15599 } while (x == 'I' || x == 'O'); 15600 } 15601 15602 /* 15603 * "test(list)" function: Just checking the walls... 15604 */ 15605 /*ARGSUSED*/ 15606 static void 15607 f_test(argvars, rettv) 15608 typval_T *argvars; 15609 typval_T *rettv; 15610 { 15611 /* Used for unit testing. Change the code below to your liking. */ 15612 #if 0 15613 listitem_T *li; 15614 list_T *l; 15615 char_u *bad, *good; 15616 15617 if (argvars[0].v_type != VAR_LIST) 15618 return; 15619 l = argvars[0].vval.v_list; 15620 if (l == NULL) 15621 return; 15622 li = l->lv_first; 15623 if (li == NULL) 15624 return; 15625 bad = get_tv_string(&li->li_tv); 15626 li = li->li_next; 15627 if (li == NULL) 15628 return; 15629 good = get_tv_string(&li->li_tv); 15630 rettv->vval.v_number = test_edit_score(bad, good); 15631 #endif 15632 } 15633 15634 /* 15635 * "tolower(string)" function 15636 */ 15637 static void 15638 f_tolower(argvars, rettv) 15639 typval_T *argvars; 15640 typval_T *rettv; 15641 { 15642 char_u *p; 15643 15644 p = vim_strsave(get_tv_string(&argvars[0])); 15645 rettv->v_type = VAR_STRING; 15646 rettv->vval.v_string = p; 15647 15648 if (p != NULL) 15649 while (*p != NUL) 15650 { 15651 #ifdef FEAT_MBYTE 15652 int l; 15653 15654 if (enc_utf8) 15655 { 15656 int c, lc; 15657 15658 c = utf_ptr2char(p); 15659 lc = utf_tolower(c); 15660 l = utf_ptr2len(p); 15661 /* TODO: reallocate string when byte count changes. */ 15662 if (utf_char2len(lc) == l) 15663 utf_char2bytes(lc, p); 15664 p += l; 15665 } 15666 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) 15667 p += l; /* skip multi-byte character */ 15668 else 15669 #endif 15670 { 15671 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */ 15672 ++p; 15673 } 15674 } 15675 } 15676 15677 /* 15678 * "toupper(string)" function 15679 */ 15680 static void 15681 f_toupper(argvars, rettv) 15682 typval_T *argvars; 15683 typval_T *rettv; 15684 { 15685 rettv->v_type = VAR_STRING; 15686 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0])); 15687 } 15688 15689 /* 15690 * "tr(string, fromstr, tostr)" function 15691 */ 15692 static void 15693 f_tr(argvars, rettv) 15694 typval_T *argvars; 15695 typval_T *rettv; 15696 { 15697 char_u *instr; 15698 char_u *fromstr; 15699 char_u *tostr; 15700 char_u *p; 15701 #ifdef FEAT_MBYTE 15702 int inlen; 15703 int fromlen; 15704 int tolen; 15705 int idx; 15706 char_u *cpstr; 15707 int cplen; 15708 int first = TRUE; 15709 #endif 15710 char_u buf[NUMBUFLEN]; 15711 char_u buf2[NUMBUFLEN]; 15712 garray_T ga; 15713 15714 instr = get_tv_string(&argvars[0]); 15715 fromstr = get_tv_string_buf_chk(&argvars[1], buf); 15716 tostr = get_tv_string_buf_chk(&argvars[2], buf2); 15717 15718 /* Default return value: empty string. */ 15719 rettv->v_type = VAR_STRING; 15720 rettv->vval.v_string = NULL; 15721 if (fromstr == NULL || tostr == NULL) 15722 return; /* type error; errmsg already given */ 15723 ga_init2(&ga, (int)sizeof(char), 80); 15724 15725 #ifdef FEAT_MBYTE 15726 if (!has_mbyte) 15727 #endif 15728 /* not multi-byte: fromstr and tostr must be the same length */ 15729 if (STRLEN(fromstr) != STRLEN(tostr)) 15730 { 15731 #ifdef FEAT_MBYTE 15732 error: 15733 #endif 15734 EMSG2(_(e_invarg2), fromstr); 15735 ga_clear(&ga); 15736 return; 15737 } 15738 15739 /* fromstr and tostr have to contain the same number of chars */ 15740 while (*instr != NUL) 15741 { 15742 #ifdef FEAT_MBYTE 15743 if (has_mbyte) 15744 { 15745 inlen = (*mb_ptr2len)(instr); 15746 cpstr = instr; 15747 cplen = inlen; 15748 idx = 0; 15749 for (p = fromstr; *p != NUL; p += fromlen) 15750 { 15751 fromlen = (*mb_ptr2len)(p); 15752 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0) 15753 { 15754 for (p = tostr; *p != NUL; p += tolen) 15755 { 15756 tolen = (*mb_ptr2len)(p); 15757 if (idx-- == 0) 15758 { 15759 cplen = tolen; 15760 cpstr = p; 15761 break; 15762 } 15763 } 15764 if (*p == NUL) /* tostr is shorter than fromstr */ 15765 goto error; 15766 break; 15767 } 15768 ++idx; 15769 } 15770 15771 if (first && cpstr == instr) 15772 { 15773 /* Check that fromstr and tostr have the same number of 15774 * (multi-byte) characters. Done only once when a character 15775 * of instr doesn't appear in fromstr. */ 15776 first = FALSE; 15777 for (p = tostr; *p != NUL; p += tolen) 15778 { 15779 tolen = (*mb_ptr2len)(p); 15780 --idx; 15781 } 15782 if (idx != 0) 15783 goto error; 15784 } 15785 15786 ga_grow(&ga, cplen); 15787 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen); 15788 ga.ga_len += cplen; 15789 15790 instr += inlen; 15791 } 15792 else 15793 #endif 15794 { 15795 /* When not using multi-byte chars we can do it faster. */ 15796 p = vim_strchr(fromstr, *instr); 15797 if (p != NULL) 15798 ga_append(&ga, tostr[p - fromstr]); 15799 else 15800 ga_append(&ga, *instr); 15801 ++instr; 15802 } 15803 } 15804 15805 rettv->vval.v_string = ga.ga_data; 15806 } 15807 15808 /* 15809 * "type(expr)" function 15810 */ 15811 static void 15812 f_type(argvars, rettv) 15813 typval_T *argvars; 15814 typval_T *rettv; 15815 { 15816 int n; 15817 15818 switch (argvars[0].v_type) 15819 { 15820 case VAR_NUMBER: n = 0; break; 15821 case VAR_STRING: n = 1; break; 15822 case VAR_FUNC: n = 2; break; 15823 case VAR_LIST: n = 3; break; 15824 case VAR_DICT: n = 4; break; 15825 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break; 15826 } 15827 rettv->vval.v_number = n; 15828 } 15829 15830 /* 15831 * "values(dict)" function 15832 */ 15833 static void 15834 f_values(argvars, rettv) 15835 typval_T *argvars; 15836 typval_T *rettv; 15837 { 15838 dict_list(argvars, rettv, 1); 15839 } 15840 15841 /* 15842 * "virtcol(string)" function 15843 */ 15844 static void 15845 f_virtcol(argvars, rettv) 15846 typval_T *argvars; 15847 typval_T *rettv; 15848 { 15849 colnr_T vcol = 0; 15850 pos_T *fp; 15851 int fnum = curbuf->b_fnum; 15852 15853 fp = var2fpos(&argvars[0], FALSE, &fnum); 15854 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count 15855 && fnum == curbuf->b_fnum) 15856 { 15857 getvvcol(curwin, fp, NULL, NULL, &vcol); 15858 ++vcol; 15859 } 15860 15861 rettv->vval.v_number = vcol; 15862 } 15863 15864 /* 15865 * "visualmode()" function 15866 */ 15867 /*ARGSUSED*/ 15868 static void 15869 f_visualmode(argvars, rettv) 15870 typval_T *argvars; 15871 typval_T *rettv; 15872 { 15873 #ifdef FEAT_VISUAL 15874 char_u str[2]; 15875 15876 rettv->v_type = VAR_STRING; 15877 str[0] = curbuf->b_visual_mode_eval; 15878 str[1] = NUL; 15879 rettv->vval.v_string = vim_strsave(str); 15880 15881 /* A non-zero number or non-empty string argument: reset mode. */ 15882 if ((argvars[0].v_type == VAR_NUMBER 15883 && argvars[0].vval.v_number != 0) 15884 || (argvars[0].v_type == VAR_STRING 15885 && *get_tv_string(&argvars[0]) != NUL)) 15886 curbuf->b_visual_mode_eval = NUL; 15887 #else 15888 rettv->vval.v_number = 0; /* return anything, it won't work anyway */ 15889 #endif 15890 } 15891 15892 /* 15893 * "winbufnr(nr)" function 15894 */ 15895 static void 15896 f_winbufnr(argvars, rettv) 15897 typval_T *argvars; 15898 typval_T *rettv; 15899 { 15900 win_T *wp; 15901 15902 wp = find_win_by_nr(&argvars[0]); 15903 if (wp == NULL) 15904 rettv->vval.v_number = -1; 15905 else 15906 rettv->vval.v_number = wp->w_buffer->b_fnum; 15907 } 15908 15909 /* 15910 * "wincol()" function 15911 */ 15912 /*ARGSUSED*/ 15913 static void 15914 f_wincol(argvars, rettv) 15915 typval_T *argvars; 15916 typval_T *rettv; 15917 { 15918 validate_cursor(); 15919 rettv->vval.v_number = curwin->w_wcol + 1; 15920 } 15921 15922 /* 15923 * "winheight(nr)" function 15924 */ 15925 static void 15926 f_winheight(argvars, rettv) 15927 typval_T *argvars; 15928 typval_T *rettv; 15929 { 15930 win_T *wp; 15931 15932 wp = find_win_by_nr(&argvars[0]); 15933 if (wp == NULL) 15934 rettv->vval.v_number = -1; 15935 else 15936 rettv->vval.v_number = wp->w_height; 15937 } 15938 15939 /* 15940 * "winline()" function 15941 */ 15942 /*ARGSUSED*/ 15943 static void 15944 f_winline(argvars, rettv) 15945 typval_T *argvars; 15946 typval_T *rettv; 15947 { 15948 validate_cursor(); 15949 rettv->vval.v_number = curwin->w_wrow + 1; 15950 } 15951 15952 /* 15953 * "winnr()" function 15954 */ 15955 /* ARGSUSED */ 15956 static void 15957 f_winnr(argvars, rettv) 15958 typval_T *argvars; 15959 typval_T *rettv; 15960 { 15961 int nr = 1; 15962 15963 #ifdef FEAT_WINDOWS 15964 nr = get_winnr(curtab, &argvars[0]); 15965 #endif 15966 rettv->vval.v_number = nr; 15967 } 15968 15969 /* 15970 * "winrestcmd()" function 15971 */ 15972 /* ARGSUSED */ 15973 static void 15974 f_winrestcmd(argvars, rettv) 15975 typval_T *argvars; 15976 typval_T *rettv; 15977 { 15978 #ifdef FEAT_WINDOWS 15979 win_T *wp; 15980 int winnr = 1; 15981 garray_T ga; 15982 char_u buf[50]; 15983 15984 ga_init2(&ga, (int)sizeof(char), 70); 15985 for (wp = firstwin; wp != NULL; wp = wp->w_next) 15986 { 15987 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height); 15988 ga_concat(&ga, buf); 15989 # ifdef FEAT_VERTSPLIT 15990 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width); 15991 ga_concat(&ga, buf); 15992 # endif 15993 ++winnr; 15994 } 15995 ga_append(&ga, NUL); 15996 15997 rettv->vval.v_string = ga.ga_data; 15998 #else 15999 rettv->vval.v_string = NULL; 16000 #endif 16001 rettv->v_type = VAR_STRING; 16002 } 16003 16004 /* 16005 * "winrestview()" function 16006 */ 16007 /* ARGSUSED */ 16008 static void 16009 f_winrestview(argvars, rettv) 16010 typval_T *argvars; 16011 typval_T *rettv; 16012 { 16013 dict_T *dict; 16014 16015 if (argvars[0].v_type != VAR_DICT 16016 || (dict = argvars[0].vval.v_dict) == NULL) 16017 EMSG(_(e_invarg)); 16018 else 16019 { 16020 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum"); 16021 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col"); 16022 #ifdef FEAT_VIRTUALEDIT 16023 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd"); 16024 #endif 16025 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant"); 16026 curwin->w_set_curswant = FALSE; 16027 16028 curwin->w_topline = get_dict_number(dict, (char_u *)"topline"); 16029 #ifdef FEAT_DIFF 16030 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill"); 16031 #endif 16032 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol"); 16033 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol"); 16034 16035 check_cursor(); 16036 changed_cline_bef_curs(); 16037 invalidate_botline(); 16038 redraw_later(VALID); 16039 16040 if (curwin->w_topline == 0) 16041 curwin->w_topline = 1; 16042 if (curwin->w_topline > curbuf->b_ml.ml_line_count) 16043 curwin->w_topline = curbuf->b_ml.ml_line_count; 16044 #ifdef FEAT_DIFF 16045 check_topfill(curwin, TRUE); 16046 #endif 16047 } 16048 } 16049 16050 /* 16051 * "winsaveview()" function 16052 */ 16053 /* ARGSUSED */ 16054 static void 16055 f_winsaveview(argvars, rettv) 16056 typval_T *argvars; 16057 typval_T *rettv; 16058 { 16059 dict_T *dict; 16060 16061 dict = dict_alloc(); 16062 if (dict == NULL) 16063 return; 16064 rettv->v_type = VAR_DICT; 16065 rettv->vval.v_dict = dict; 16066 ++dict->dv_refcount; 16067 16068 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL); 16069 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL); 16070 #ifdef FEAT_VIRTUALEDIT 16071 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL); 16072 #endif 16073 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL); 16074 16075 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL); 16076 #ifdef FEAT_DIFF 16077 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL); 16078 #endif 16079 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL); 16080 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL); 16081 } 16082 16083 /* 16084 * "winwidth(nr)" function 16085 */ 16086 static void 16087 f_winwidth(argvars, rettv) 16088 typval_T *argvars; 16089 typval_T *rettv; 16090 { 16091 win_T *wp; 16092 16093 wp = find_win_by_nr(&argvars[0]); 16094 if (wp == NULL) 16095 rettv->vval.v_number = -1; 16096 else 16097 #ifdef FEAT_VERTSPLIT 16098 rettv->vval.v_number = wp->w_width; 16099 #else 16100 rettv->vval.v_number = Columns; 16101 #endif 16102 } 16103 16104 /* 16105 * "writefile()" function 16106 */ 16107 static void 16108 f_writefile(argvars, rettv) 16109 typval_T *argvars; 16110 typval_T *rettv; 16111 { 16112 int binary = FALSE; 16113 char_u *fname; 16114 FILE *fd; 16115 listitem_T *li; 16116 char_u *s; 16117 int ret = 0; 16118 int c; 16119 16120 if (argvars[0].v_type != VAR_LIST) 16121 { 16122 EMSG2(_(e_listarg), "writefile()"); 16123 return; 16124 } 16125 if (argvars[0].vval.v_list == NULL) 16126 return; 16127 16128 if (argvars[2].v_type != VAR_UNKNOWN 16129 && STRCMP(get_tv_string(&argvars[2]), "b") == 0) 16130 binary = TRUE; 16131 16132 /* Always open the file in binary mode, library functions have a mind of 16133 * their own about CR-LF conversion. */ 16134 fname = get_tv_string(&argvars[1]); 16135 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL) 16136 { 16137 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname); 16138 ret = -1; 16139 } 16140 else 16141 { 16142 for (li = argvars[0].vval.v_list->lv_first; li != NULL; 16143 li = li->li_next) 16144 { 16145 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s) 16146 { 16147 if (*s == '\n') 16148 c = putc(NUL, fd); 16149 else 16150 c = putc(*s, fd); 16151 if (c == EOF) 16152 { 16153 ret = -1; 16154 break; 16155 } 16156 } 16157 if (!binary || li->li_next != NULL) 16158 if (putc('\n', fd) == EOF) 16159 { 16160 ret = -1; 16161 break; 16162 } 16163 if (ret < 0) 16164 { 16165 EMSG(_(e_write)); 16166 break; 16167 } 16168 } 16169 fclose(fd); 16170 } 16171 16172 rettv->vval.v_number = ret; 16173 } 16174 16175 /* 16176 * Translate a String variable into a position. 16177 * Returns NULL when there is an error. 16178 */ 16179 static pos_T * 16180 var2fpos(varp, lnum, fnum) 16181 typval_T *varp; 16182 int lnum; /* TRUE when $ is last line */ 16183 int *fnum; /* set to fnum for '0, 'A, etc. */ 16184 { 16185 char_u *name; 16186 static pos_T pos; 16187 pos_T *pp; 16188 16189 /* Argument can be [lnum, col, coladd]. */ 16190 if (varp->v_type == VAR_LIST) 16191 { 16192 list_T *l; 16193 int len; 16194 int error = FALSE; 16195 16196 l = varp->vval.v_list; 16197 if (l == NULL) 16198 return NULL; 16199 16200 /* Get the line number */ 16201 pos.lnum = list_find_nr(l, 0L, &error); 16202 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count) 16203 return NULL; /* invalid line number */ 16204 16205 /* Get the column number */ 16206 pos.col = list_find_nr(l, 1L, &error); 16207 if (error) 16208 return NULL; 16209 len = (long)STRLEN(ml_get(pos.lnum)); 16210 /* Accept a position up to the NUL after the line. */ 16211 if (pos.col == 0 || (int)pos.col > len + 1) 16212 return NULL; /* invalid column number */ 16213 --pos.col; 16214 16215 #ifdef FEAT_VIRTUALEDIT 16216 /* Get the virtual offset. Defaults to zero. */ 16217 pos.coladd = list_find_nr(l, 2L, &error); 16218 if (error) 16219 pos.coladd = 0; 16220 #endif 16221 16222 return &pos; 16223 } 16224 16225 name = get_tv_string_chk(varp); 16226 if (name == NULL) 16227 return NULL; 16228 if (name[0] == '.') /* cursor */ 16229 return &curwin->w_cursor; 16230 if (name[0] == '\'') /* mark */ 16231 { 16232 pp = getmark_fnum(name[1], FALSE, fnum); 16233 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0) 16234 return NULL; 16235 return pp; 16236 } 16237 16238 #ifdef FEAT_VIRTUALEDIT 16239 pos.coladd = 0; 16240 #endif 16241 16242 if (name[0] == 'w' && lnum) 16243 { 16244 pos.col = 0; 16245 if (name[1] == '0') /* "w0": first visible line */ 16246 { 16247 update_topline(); 16248 pos.lnum = curwin->w_topline; 16249 return &pos; 16250 } 16251 else if (name[1] == '$') /* "w$": last visible line */ 16252 { 16253 validate_botline(); 16254 pos.lnum = curwin->w_botline - 1; 16255 return &pos; 16256 } 16257 } 16258 else if (name[0] == '$') /* last column or line */ 16259 { 16260 if (lnum) 16261 { 16262 pos.lnum = curbuf->b_ml.ml_line_count; 16263 pos.col = 0; 16264 } 16265 else 16266 { 16267 pos.lnum = curwin->w_cursor.lnum; 16268 pos.col = (colnr_T)STRLEN(ml_get_curline()); 16269 } 16270 return &pos; 16271 } 16272 return NULL; 16273 } 16274 16275 /* 16276 * Convert list in "arg" into a position and optional file number. 16277 * When "fnump" is NULL there is no file number, only 3 items. 16278 * Note that the column is passed on as-is, the caller may want to decrement 16279 * it to use 1 for the first column. 16280 * Return FAIL when conversion is not possible, doesn't check the position for 16281 * validity. 16282 */ 16283 static int 16284 list2fpos(arg, posp, fnump) 16285 typval_T *arg; 16286 pos_T *posp; 16287 int *fnump; 16288 { 16289 list_T *l = arg->vval.v_list; 16290 long i = 0; 16291 long n; 16292 16293 /* List must be: [fnum, lnum, col, coladd] */ 16294 if (arg->v_type != VAR_LIST || l == NULL 16295 || l->lv_len != (fnump == NULL ? 3 : 4)) 16296 return FAIL; 16297 16298 if (fnump != NULL) 16299 { 16300 n = list_find_nr(l, i++, NULL); /* fnum */ 16301 if (n < 0) 16302 return FAIL; 16303 if (n == 0) 16304 n = curbuf->b_fnum; /* current buffer */ 16305 *fnump = n; 16306 } 16307 16308 n = list_find_nr(l, i++, NULL); /* lnum */ 16309 if (n < 0) 16310 return FAIL; 16311 posp->lnum = n; 16312 16313 n = list_find_nr(l, i++, NULL); /* col */ 16314 if (n < 0) 16315 return FAIL; 16316 posp->col = n; 16317 16318 #ifdef FEAT_VIRTUALEDIT 16319 n = list_find_nr(l, i, NULL); 16320 if (n < 0) 16321 return FAIL; 16322 posp->coladd = n; 16323 #endif 16324 16325 return OK; 16326 } 16327 16328 /* 16329 * Get the length of an environment variable name. 16330 * Advance "arg" to the first character after the name. 16331 * Return 0 for error. 16332 */ 16333 static int 16334 get_env_len(arg) 16335 char_u **arg; 16336 { 16337 char_u *p; 16338 int len; 16339 16340 for (p = *arg; vim_isIDc(*p); ++p) 16341 ; 16342 if (p == *arg) /* no name found */ 16343 return 0; 16344 16345 len = (int)(p - *arg); 16346 *arg = p; 16347 return len; 16348 } 16349 16350 /* 16351 * Get the length of the name of a function or internal variable. 16352 * "arg" is advanced to the first non-white character after the name. 16353 * Return 0 if something is wrong. 16354 */ 16355 static int 16356 get_id_len(arg) 16357 char_u **arg; 16358 { 16359 char_u *p; 16360 int len; 16361 16362 /* Find the end of the name. */ 16363 for (p = *arg; eval_isnamec(*p); ++p) 16364 ; 16365 if (p == *arg) /* no name found */ 16366 return 0; 16367 16368 len = (int)(p - *arg); 16369 *arg = skipwhite(p); 16370 16371 return len; 16372 } 16373 16374 /* 16375 * Get the length of the name of a variable or function. 16376 * Only the name is recognized, does not handle ".key" or "[idx]". 16377 * "arg" is advanced to the first non-white character after the name. 16378 * Return -1 if curly braces expansion failed. 16379 * Return 0 if something else is wrong. 16380 * If the name contains 'magic' {}'s, expand them and return the 16381 * expanded name in an allocated string via 'alias' - caller must free. 16382 */ 16383 static int 16384 get_name_len(arg, alias, evaluate, verbose) 16385 char_u **arg; 16386 char_u **alias; 16387 int evaluate; 16388 int verbose; 16389 { 16390 int len; 16391 char_u *p; 16392 char_u *expr_start; 16393 char_u *expr_end; 16394 16395 *alias = NULL; /* default to no alias */ 16396 16397 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA 16398 && (*arg)[2] == (int)KE_SNR) 16399 { 16400 /* hard coded <SNR>, already translated */ 16401 *arg += 3; 16402 return get_id_len(arg) + 3; 16403 } 16404 len = eval_fname_script(*arg); 16405 if (len > 0) 16406 { 16407 /* literal "<SID>", "s:" or "<SNR>" */ 16408 *arg += len; 16409 } 16410 16411 /* 16412 * Find the end of the name; check for {} construction. 16413 */ 16414 p = find_name_end(*arg, &expr_start, &expr_end, 16415 len > 0 ? 0 : FNE_CHECK_START); 16416 if (expr_start != NULL) 16417 { 16418 char_u *temp_string; 16419 16420 if (!evaluate) 16421 { 16422 len += (int)(p - *arg); 16423 *arg = skipwhite(p); 16424 return len; 16425 } 16426 16427 /* 16428 * Include any <SID> etc in the expanded string: 16429 * Thus the -len here. 16430 */ 16431 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p); 16432 if (temp_string == NULL) 16433 return -1; 16434 *alias = temp_string; 16435 *arg = skipwhite(p); 16436 return (int)STRLEN(temp_string); 16437 } 16438 16439 len += get_id_len(arg); 16440 if (len == 0 && verbose) 16441 EMSG2(_(e_invexpr2), *arg); 16442 16443 return len; 16444 } 16445 16446 /* 16447 * Find the end of a variable or function name, taking care of magic braces. 16448 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the 16449 * start and end of the first magic braces item. 16450 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. 16451 * Return a pointer to just after the name. Equal to "arg" if there is no 16452 * valid name. 16453 */ 16454 static char_u * 16455 find_name_end(arg, expr_start, expr_end, flags) 16456 char_u *arg; 16457 char_u **expr_start; 16458 char_u **expr_end; 16459 int flags; 16460 { 16461 int mb_nest = 0; 16462 int br_nest = 0; 16463 char_u *p; 16464 16465 if (expr_start != NULL) 16466 { 16467 *expr_start = NULL; 16468 *expr_end = NULL; 16469 } 16470 16471 /* Quick check for valid starting character. */ 16472 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{') 16473 return arg; 16474 16475 for (p = arg; *p != NUL 16476 && (eval_isnamec(*p) 16477 || *p == '{' 16478 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.')) 16479 || mb_nest != 0 16480 || br_nest != 0); mb_ptr_adv(p)) 16481 { 16482 if (*p == '\'') 16483 { 16484 /* skip over 'string' to avoid counting [ and ] inside it. */ 16485 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p)) 16486 ; 16487 if (*p == NUL) 16488 break; 16489 } 16490 else if (*p == '"') 16491 { 16492 /* skip over "str\"ing" to avoid counting [ and ] inside it. */ 16493 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p)) 16494 if (*p == '\\' && p[1] != NUL) 16495 ++p; 16496 if (*p == NUL) 16497 break; 16498 } 16499 16500 if (mb_nest == 0) 16501 { 16502 if (*p == '[') 16503 ++br_nest; 16504 else if (*p == ']') 16505 --br_nest; 16506 } 16507 16508 if (br_nest == 0) 16509 { 16510 if (*p == '{') 16511 { 16512 mb_nest++; 16513 if (expr_start != NULL && *expr_start == NULL) 16514 *expr_start = p; 16515 } 16516 else if (*p == '}') 16517 { 16518 mb_nest--; 16519 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL) 16520 *expr_end = p; 16521 } 16522 } 16523 } 16524 16525 return p; 16526 } 16527 16528 /* 16529 * Expands out the 'magic' {}'s in a variable/function name. 16530 * Note that this can call itself recursively, to deal with 16531 * constructs like foo{bar}{baz}{bam} 16532 * The four pointer arguments point to "foo{expre}ss{ion}bar" 16533 * "in_start" ^ 16534 * "expr_start" ^ 16535 * "expr_end" ^ 16536 * "in_end" ^ 16537 * 16538 * Returns a new allocated string, which the caller must free. 16539 * Returns NULL for failure. 16540 */ 16541 static char_u * 16542 make_expanded_name(in_start, expr_start, expr_end, in_end) 16543 char_u *in_start; 16544 char_u *expr_start; 16545 char_u *expr_end; 16546 char_u *in_end; 16547 { 16548 char_u c1; 16549 char_u *retval = NULL; 16550 char_u *temp_result; 16551 char_u *nextcmd = NULL; 16552 16553 if (expr_end == NULL || in_end == NULL) 16554 return NULL; 16555 *expr_start = NUL; 16556 *expr_end = NUL; 16557 c1 = *in_end; 16558 *in_end = NUL; 16559 16560 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE); 16561 if (temp_result != NULL && nextcmd == NULL) 16562 { 16563 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start) 16564 + (in_end - expr_end) + 1)); 16565 if (retval != NULL) 16566 { 16567 STRCPY(retval, in_start); 16568 STRCAT(retval, temp_result); 16569 STRCAT(retval, expr_end + 1); 16570 } 16571 } 16572 vim_free(temp_result); 16573 16574 *in_end = c1; /* put char back for error messages */ 16575 *expr_start = '{'; 16576 *expr_end = '}'; 16577 16578 if (retval != NULL) 16579 { 16580 temp_result = find_name_end(retval, &expr_start, &expr_end, 0); 16581 if (expr_start != NULL) 16582 { 16583 /* Further expansion! */ 16584 temp_result = make_expanded_name(retval, expr_start, 16585 expr_end, temp_result); 16586 vim_free(retval); 16587 retval = temp_result; 16588 } 16589 } 16590 16591 return retval; 16592 } 16593 16594 /* 16595 * Return TRUE if character "c" can be used in a variable or function name. 16596 * Does not include '{' or '}' for magic braces. 16597 */ 16598 static int 16599 eval_isnamec(c) 16600 int c; 16601 { 16602 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR); 16603 } 16604 16605 /* 16606 * Return TRUE if character "c" can be used as the first character in a 16607 * variable or function name (excluding '{' and '}'). 16608 */ 16609 static int 16610 eval_isnamec1(c) 16611 int c; 16612 { 16613 return (ASCII_ISALPHA(c) || c == '_'); 16614 } 16615 16616 /* 16617 * Set number v: variable to "val". 16618 */ 16619 void 16620 set_vim_var_nr(idx, val) 16621 int idx; 16622 long val; 16623 { 16624 vimvars[idx].vv_nr = val; 16625 } 16626 16627 /* 16628 * Get number v: variable value. 16629 */ 16630 long 16631 get_vim_var_nr(idx) 16632 int idx; 16633 { 16634 return vimvars[idx].vv_nr; 16635 } 16636 16637 #if defined(FEAT_AUTOCMD) || defined(PROTO) 16638 /* 16639 * Get string v: variable value. Uses a static buffer, can only be used once. 16640 */ 16641 char_u * 16642 get_vim_var_str(idx) 16643 int idx; 16644 { 16645 return get_tv_string(&vimvars[idx].vv_tv); 16646 } 16647 #endif 16648 16649 /* 16650 * Set v:count, v:count1 and v:prevcount. 16651 */ 16652 void 16653 set_vcount(count, count1) 16654 long count; 16655 long count1; 16656 { 16657 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr; 16658 vimvars[VV_COUNT].vv_nr = count; 16659 vimvars[VV_COUNT1].vv_nr = count1; 16660 } 16661 16662 /* 16663 * Set string v: variable to a copy of "val". 16664 */ 16665 void 16666 set_vim_var_string(idx, val, len) 16667 int idx; 16668 char_u *val; 16669 int len; /* length of "val" to use or -1 (whole string) */ 16670 { 16671 /* Need to do this (at least) once, since we can't initialize a union. 16672 * Will always be invoked when "v:progname" is set. */ 16673 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100; 16674 16675 vim_free(vimvars[idx].vv_str); 16676 if (val == NULL) 16677 vimvars[idx].vv_str = NULL; 16678 else if (len == -1) 16679 vimvars[idx].vv_str = vim_strsave(val); 16680 else 16681 vimvars[idx].vv_str = vim_strnsave(val, len); 16682 } 16683 16684 /* 16685 * Set v:register if needed. 16686 */ 16687 void 16688 set_reg_var(c) 16689 int c; 16690 { 16691 char_u regname; 16692 16693 if (c == 0 || c == ' ') 16694 regname = '"'; 16695 else 16696 regname = c; 16697 /* Avoid free/alloc when the value is already right. */ 16698 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c) 16699 set_vim_var_string(VV_REG, ®name, 1); 16700 } 16701 16702 /* 16703 * Get or set v:exception. If "oldval" == NULL, return the current value. 16704 * Otherwise, restore the value to "oldval" and return NULL. 16705 * Must always be called in pairs to save and restore v:exception! Does not 16706 * take care of memory allocations. 16707 */ 16708 char_u * 16709 v_exception(oldval) 16710 char_u *oldval; 16711 { 16712 if (oldval == NULL) 16713 return vimvars[VV_EXCEPTION].vv_str; 16714 16715 vimvars[VV_EXCEPTION].vv_str = oldval; 16716 return NULL; 16717 } 16718 16719 /* 16720 * Get or set v:throwpoint. If "oldval" == NULL, return the current value. 16721 * Otherwise, restore the value to "oldval" and return NULL. 16722 * Must always be called in pairs to save and restore v:throwpoint! Does not 16723 * take care of memory allocations. 16724 */ 16725 char_u * 16726 v_throwpoint(oldval) 16727 char_u *oldval; 16728 { 16729 if (oldval == NULL) 16730 return vimvars[VV_THROWPOINT].vv_str; 16731 16732 vimvars[VV_THROWPOINT].vv_str = oldval; 16733 return NULL; 16734 } 16735 16736 #if defined(FEAT_AUTOCMD) || defined(PROTO) 16737 /* 16738 * Set v:cmdarg. 16739 * If "eap" != NULL, use "eap" to generate the value and return the old value. 16740 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL. 16741 * Must always be called in pairs! 16742 */ 16743 char_u * 16744 set_cmdarg(eap, oldarg) 16745 exarg_T *eap; 16746 char_u *oldarg; 16747 { 16748 char_u *oldval; 16749 char_u *newval; 16750 unsigned len; 16751 16752 oldval = vimvars[VV_CMDARG].vv_str; 16753 if (eap == NULL) 16754 { 16755 vim_free(oldval); 16756 vimvars[VV_CMDARG].vv_str = oldarg; 16757 return NULL; 16758 } 16759 16760 if (eap->force_bin == FORCE_BIN) 16761 len = 6; 16762 else if (eap->force_bin == FORCE_NOBIN) 16763 len = 8; 16764 else 16765 len = 0; 16766 16767 if (eap->read_edit) 16768 len += 7; 16769 16770 if (eap->force_ff != 0) 16771 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6; 16772 # ifdef FEAT_MBYTE 16773 if (eap->force_enc != 0) 16774 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7; 16775 if (eap->bad_char != 0) 16776 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7; 16777 # endif 16778 16779 newval = alloc(len + 1); 16780 if (newval == NULL) 16781 return NULL; 16782 16783 if (eap->force_bin == FORCE_BIN) 16784 sprintf((char *)newval, " ++bin"); 16785 else if (eap->force_bin == FORCE_NOBIN) 16786 sprintf((char *)newval, " ++nobin"); 16787 else 16788 *newval = NUL; 16789 16790 if (eap->read_edit) 16791 STRCAT(newval, " ++edit"); 16792 16793 if (eap->force_ff != 0) 16794 sprintf((char *)newval + STRLEN(newval), " ++ff=%s", 16795 eap->cmd + eap->force_ff); 16796 # ifdef FEAT_MBYTE 16797 if (eap->force_enc != 0) 16798 sprintf((char *)newval + STRLEN(newval), " ++enc=%s", 16799 eap->cmd + eap->force_enc); 16800 if (eap->bad_char != 0) 16801 sprintf((char *)newval + STRLEN(newval), " ++bad=%s", 16802 eap->cmd + eap->bad_char); 16803 # endif 16804 vimvars[VV_CMDARG].vv_str = newval; 16805 return oldval; 16806 } 16807 #endif 16808 16809 /* 16810 * Get the value of internal variable "name". 16811 * Return OK or FAIL. 16812 */ 16813 static int 16814 get_var_tv(name, len, rettv, verbose) 16815 char_u *name; 16816 int len; /* length of "name" */ 16817 typval_T *rettv; /* NULL when only checking existence */ 16818 int verbose; /* may give error message */ 16819 { 16820 int ret = OK; 16821 typval_T *tv = NULL; 16822 typval_T atv; 16823 dictitem_T *v; 16824 int cc; 16825 16826 /* truncate the name, so that we can use strcmp() */ 16827 cc = name[len]; 16828 name[len] = NUL; 16829 16830 /* 16831 * Check for "b:changedtick". 16832 */ 16833 if (STRCMP(name, "b:changedtick") == 0) 16834 { 16835 atv.v_type = VAR_NUMBER; 16836 atv.vval.v_number = curbuf->b_changedtick; 16837 tv = &atv; 16838 } 16839 16840 /* 16841 * Check for user-defined variables. 16842 */ 16843 else 16844 { 16845 v = find_var(name, NULL); 16846 if (v != NULL) 16847 tv = &v->di_tv; 16848 } 16849 16850 if (tv == NULL) 16851 { 16852 if (rettv != NULL && verbose) 16853 EMSG2(_(e_undefvar), name); 16854 ret = FAIL; 16855 } 16856 else if (rettv != NULL) 16857 copy_tv(tv, rettv); 16858 16859 name[len] = cc; 16860 16861 return ret; 16862 } 16863 16864 /* 16865 * Handle expr[expr], expr[expr:expr] subscript and .name lookup. 16866 * Also handle function call with Funcref variable: func(expr) 16867 * Can all be combined: dict.func(expr)[idx]['func'](expr) 16868 */ 16869 static int 16870 handle_subscript(arg, rettv, evaluate, verbose) 16871 char_u **arg; 16872 typval_T *rettv; 16873 int evaluate; /* do more than finding the end */ 16874 int verbose; /* give error messages */ 16875 { 16876 int ret = OK; 16877 dict_T *selfdict = NULL; 16878 char_u *s; 16879 int len; 16880 typval_T functv; 16881 16882 while (ret == OK 16883 && (**arg == '[' 16884 || (**arg == '.' && rettv->v_type == VAR_DICT) 16885 || (**arg == '(' && rettv->v_type == VAR_FUNC)) 16886 && !vim_iswhite(*(*arg - 1))) 16887 { 16888 if (**arg == '(') 16889 { 16890 /* need to copy the funcref so that we can clear rettv */ 16891 functv = *rettv; 16892 rettv->v_type = VAR_UNKNOWN; 16893 16894 /* Invoke the function. Recursive! */ 16895 s = functv.vval.v_string; 16896 ret = get_func_tv(s, STRLEN(s), rettv, arg, 16897 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 16898 &len, evaluate, selfdict); 16899 16900 /* Clear the funcref afterwards, so that deleting it while 16901 * evaluating the arguments is possible (see test55). */ 16902 clear_tv(&functv); 16903 16904 /* Stop the expression evaluation when immediately aborting on 16905 * error, or when an interrupt occurred or an exception was thrown 16906 * but not caught. */ 16907 if (aborting()) 16908 { 16909 if (ret == OK) 16910 clear_tv(rettv); 16911 ret = FAIL; 16912 } 16913 dict_unref(selfdict); 16914 selfdict = NULL; 16915 } 16916 else /* **arg == '[' || **arg == '.' */ 16917 { 16918 dict_unref(selfdict); 16919 if (rettv->v_type == VAR_DICT) 16920 { 16921 selfdict = rettv->vval.v_dict; 16922 if (selfdict != NULL) 16923 ++selfdict->dv_refcount; 16924 } 16925 else 16926 selfdict = NULL; 16927 if (eval_index(arg, rettv, evaluate, verbose) == FAIL) 16928 { 16929 clear_tv(rettv); 16930 ret = FAIL; 16931 } 16932 } 16933 } 16934 dict_unref(selfdict); 16935 return ret; 16936 } 16937 16938 /* 16939 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL 16940 * value). 16941 */ 16942 static typval_T * 16943 alloc_tv() 16944 { 16945 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T)); 16946 } 16947 16948 /* 16949 * Allocate memory for a variable type-value, and assign a string to it. 16950 * The string "s" must have been allocated, it is consumed. 16951 * Return NULL for out of memory, the variable otherwise. 16952 */ 16953 static typval_T * 16954 alloc_string_tv(s) 16955 char_u *s; 16956 { 16957 typval_T *rettv; 16958 16959 rettv = alloc_tv(); 16960 if (rettv != NULL) 16961 { 16962 rettv->v_type = VAR_STRING; 16963 rettv->vval.v_string = s; 16964 } 16965 else 16966 vim_free(s); 16967 return rettv; 16968 } 16969 16970 /* 16971 * Free the memory for a variable type-value. 16972 */ 16973 void 16974 free_tv(varp) 16975 typval_T *varp; 16976 { 16977 if (varp != NULL) 16978 { 16979 switch (varp->v_type) 16980 { 16981 case VAR_FUNC: 16982 func_unref(varp->vval.v_string); 16983 /*FALLTHROUGH*/ 16984 case VAR_STRING: 16985 vim_free(varp->vval.v_string); 16986 break; 16987 case VAR_LIST: 16988 list_unref(varp->vval.v_list); 16989 break; 16990 case VAR_DICT: 16991 dict_unref(varp->vval.v_dict); 16992 break; 16993 case VAR_NUMBER: 16994 case VAR_UNKNOWN: 16995 break; 16996 default: 16997 EMSG2(_(e_intern2), "free_tv()"); 16998 break; 16999 } 17000 vim_free(varp); 17001 } 17002 } 17003 17004 /* 17005 * Free the memory for a variable value and set the value to NULL or 0. 17006 */ 17007 void 17008 clear_tv(varp) 17009 typval_T *varp; 17010 { 17011 if (varp != NULL) 17012 { 17013 switch (varp->v_type) 17014 { 17015 case VAR_FUNC: 17016 func_unref(varp->vval.v_string); 17017 /*FALLTHROUGH*/ 17018 case VAR_STRING: 17019 vim_free(varp->vval.v_string); 17020 varp->vval.v_string = NULL; 17021 break; 17022 case VAR_LIST: 17023 list_unref(varp->vval.v_list); 17024 varp->vval.v_list = NULL; 17025 break; 17026 case VAR_DICT: 17027 dict_unref(varp->vval.v_dict); 17028 varp->vval.v_dict = NULL; 17029 break; 17030 case VAR_NUMBER: 17031 varp->vval.v_number = 0; 17032 break; 17033 case VAR_UNKNOWN: 17034 break; 17035 default: 17036 EMSG2(_(e_intern2), "clear_tv()"); 17037 } 17038 varp->v_lock = 0; 17039 } 17040 } 17041 17042 /* 17043 * Set the value of a variable to NULL without freeing items. 17044 */ 17045 static void 17046 init_tv(varp) 17047 typval_T *varp; 17048 { 17049 if (varp != NULL) 17050 vim_memset(varp, 0, sizeof(typval_T)); 17051 } 17052 17053 /* 17054 * Get the number value of a variable. 17055 * If it is a String variable, uses vim_str2nr(). 17056 * For incompatible types, return 0. 17057 * get_tv_number_chk() is similar to get_tv_number(), but informs the 17058 * caller of incompatible types: it sets *denote to TRUE if "denote" 17059 * is not NULL or returns -1 otherwise. 17060 */ 17061 static long 17062 get_tv_number(varp) 17063 typval_T *varp; 17064 { 17065 int error = FALSE; 17066 17067 return get_tv_number_chk(varp, &error); /* return 0L on error */ 17068 } 17069 17070 long 17071 get_tv_number_chk(varp, denote) 17072 typval_T *varp; 17073 int *denote; 17074 { 17075 long n = 0L; 17076 17077 switch (varp->v_type) 17078 { 17079 case VAR_NUMBER: 17080 return (long)(varp->vval.v_number); 17081 case VAR_FUNC: 17082 EMSG(_("E703: Using a Funcref as a number")); 17083 break; 17084 case VAR_STRING: 17085 if (varp->vval.v_string != NULL) 17086 vim_str2nr(varp->vval.v_string, NULL, NULL, 17087 TRUE, TRUE, &n, NULL); 17088 return n; 17089 case VAR_LIST: 17090 EMSG(_("E745: Using a List as a number")); 17091 break; 17092 case VAR_DICT: 17093 EMSG(_("E728: Using a Dictionary as a number")); 17094 break; 17095 default: 17096 EMSG2(_(e_intern2), "get_tv_number()"); 17097 break; 17098 } 17099 if (denote == NULL) /* useful for values that must be unsigned */ 17100 n = -1; 17101 else 17102 *denote = TRUE; 17103 return n; 17104 } 17105 17106 /* 17107 * Get the lnum from the first argument. 17108 * Also accepts ".", "$", etc., but that only works for the current buffer. 17109 * Returns -1 on error. 17110 */ 17111 static linenr_T 17112 get_tv_lnum(argvars) 17113 typval_T *argvars; 17114 { 17115 typval_T rettv; 17116 linenr_T lnum; 17117 17118 lnum = get_tv_number_chk(&argvars[0], NULL); 17119 if (lnum == 0) /* no valid number, try using line() */ 17120 { 17121 rettv.v_type = VAR_NUMBER; 17122 f_line(argvars, &rettv); 17123 lnum = rettv.vval.v_number; 17124 clear_tv(&rettv); 17125 } 17126 return lnum; 17127 } 17128 17129 /* 17130 * Get the lnum from the first argument. 17131 * Also accepts "$", then "buf" is used. 17132 * Returns 0 on error. 17133 */ 17134 static linenr_T 17135 get_tv_lnum_buf(argvars, buf) 17136 typval_T *argvars; 17137 buf_T *buf; 17138 { 17139 if (argvars[0].v_type == VAR_STRING 17140 && argvars[0].vval.v_string != NULL 17141 && argvars[0].vval.v_string[0] == '$' 17142 && buf != NULL) 17143 return buf->b_ml.ml_line_count; 17144 return get_tv_number_chk(&argvars[0], NULL); 17145 } 17146 17147 /* 17148 * Get the string value of a variable. 17149 * If it is a Number variable, the number is converted into a string. 17150 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE! 17151 * get_tv_string_buf() uses a given buffer. 17152 * If the String variable has never been set, return an empty string. 17153 * Never returns NULL; 17154 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return 17155 * NULL on error. 17156 */ 17157 static char_u * 17158 get_tv_string(varp) 17159 typval_T *varp; 17160 { 17161 static char_u mybuf[NUMBUFLEN]; 17162 17163 return get_tv_string_buf(varp, mybuf); 17164 } 17165 17166 static char_u * 17167 get_tv_string_buf(varp, buf) 17168 typval_T *varp; 17169 char_u *buf; 17170 { 17171 char_u *res = get_tv_string_buf_chk(varp, buf); 17172 17173 return res != NULL ? res : (char_u *)""; 17174 } 17175 17176 char_u * 17177 get_tv_string_chk(varp) 17178 typval_T *varp; 17179 { 17180 static char_u mybuf[NUMBUFLEN]; 17181 17182 return get_tv_string_buf_chk(varp, mybuf); 17183 } 17184 17185 static char_u * 17186 get_tv_string_buf_chk(varp, buf) 17187 typval_T *varp; 17188 char_u *buf; 17189 { 17190 switch (varp->v_type) 17191 { 17192 case VAR_NUMBER: 17193 sprintf((char *)buf, "%ld", (long)varp->vval.v_number); 17194 return buf; 17195 case VAR_FUNC: 17196 EMSG(_("E729: using Funcref as a String")); 17197 break; 17198 case VAR_LIST: 17199 EMSG(_("E730: using List as a String")); 17200 break; 17201 case VAR_DICT: 17202 EMSG(_("E731: using Dictionary as a String")); 17203 break; 17204 case VAR_STRING: 17205 if (varp->vval.v_string != NULL) 17206 return varp->vval.v_string; 17207 return (char_u *)""; 17208 default: 17209 EMSG2(_(e_intern2), "get_tv_string_buf()"); 17210 break; 17211 } 17212 return NULL; 17213 } 17214 17215 /* 17216 * Find variable "name" in the list of variables. 17217 * Return a pointer to it if found, NULL if not found. 17218 * Careful: "a:0" variables don't have a name. 17219 * When "htp" is not NULL we are writing to the variable, set "htp" to the 17220 * hashtab_T used. 17221 */ 17222 static dictitem_T * 17223 find_var(name, htp) 17224 char_u *name; 17225 hashtab_T **htp; 17226 { 17227 char_u *varname; 17228 hashtab_T *ht; 17229 17230 ht = find_var_ht(name, &varname); 17231 if (htp != NULL) 17232 *htp = ht; 17233 if (ht == NULL) 17234 return NULL; 17235 return find_var_in_ht(ht, varname, htp != NULL); 17236 } 17237 17238 /* 17239 * Find variable "varname" in hashtab "ht". 17240 * Returns NULL if not found. 17241 */ 17242 static dictitem_T * 17243 find_var_in_ht(ht, varname, writing) 17244 hashtab_T *ht; 17245 char_u *varname; 17246 int writing; 17247 { 17248 hashitem_T *hi; 17249 17250 if (*varname == NUL) 17251 { 17252 /* Must be something like "s:", otherwise "ht" would be NULL. */ 17253 switch (varname[-2]) 17254 { 17255 case 's': return &SCRIPT_SV(current_SID).sv_var; 17256 case 'g': return &globvars_var; 17257 case 'v': return &vimvars_var; 17258 case 'b': return &curbuf->b_bufvar; 17259 case 'w': return &curwin->w_winvar; 17260 #ifdef FEAT_WINDOWS 17261 case 't': return &curtab->tp_winvar; 17262 #endif 17263 case 'l': return current_funccal == NULL 17264 ? NULL : ¤t_funccal->l_vars_var; 17265 case 'a': return current_funccal == NULL 17266 ? NULL : ¤t_funccal->l_avars_var; 17267 } 17268 return NULL; 17269 } 17270 17271 hi = hash_find(ht, varname); 17272 if (HASHITEM_EMPTY(hi)) 17273 { 17274 /* For global variables we may try auto-loading the script. If it 17275 * worked find the variable again. Don't auto-load a script if it was 17276 * loaded already, otherwise it would be loaded every time when 17277 * checking if a function name is a Funcref variable. */ 17278 if (ht == &globvarht && !writing 17279 && script_autoload(varname, FALSE) && !aborting()) 17280 hi = hash_find(ht, varname); 17281 if (HASHITEM_EMPTY(hi)) 17282 return NULL; 17283 } 17284 return HI2DI(hi); 17285 } 17286 17287 /* 17288 * Find the hashtab used for a variable name. 17289 * Set "varname" to the start of name without ':'. 17290 */ 17291 static hashtab_T * 17292 find_var_ht(name, varname) 17293 char_u *name; 17294 char_u **varname; 17295 { 17296 hashitem_T *hi; 17297 17298 if (name[1] != ':') 17299 { 17300 /* The name must not start with a colon or #. */ 17301 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR) 17302 return NULL; 17303 *varname = name; 17304 17305 /* "version" is "v:version" in all scopes */ 17306 hi = hash_find(&compat_hashtab, name); 17307 if (!HASHITEM_EMPTY(hi)) 17308 return &compat_hashtab; 17309 17310 if (current_funccal == NULL) 17311 return &globvarht; /* global variable */ 17312 return ¤t_funccal->l_vars.dv_hashtab; /* l: variable */ 17313 } 17314 *varname = name + 2; 17315 if (*name == 'g') /* global variable */ 17316 return &globvarht; 17317 /* There must be no ':' or '#' in the rest of the name, unless g: is used 17318 */ 17319 if (vim_strchr(name + 2, ':') != NULL 17320 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL) 17321 return NULL; 17322 if (*name == 'b') /* buffer variable */ 17323 return &curbuf->b_vars.dv_hashtab; 17324 if (*name == 'w') /* window variable */ 17325 return &curwin->w_vars.dv_hashtab; 17326 #ifdef FEAT_WINDOWS 17327 if (*name == 't') /* tab page variable */ 17328 return &curtab->tp_vars.dv_hashtab; 17329 #endif 17330 if (*name == 'v') /* v: variable */ 17331 return &vimvarht; 17332 if (*name == 'a' && current_funccal != NULL) /* function argument */ 17333 return ¤t_funccal->l_avars.dv_hashtab; 17334 if (*name == 'l' && current_funccal != NULL) /* local function variable */ 17335 return ¤t_funccal->l_vars.dv_hashtab; 17336 if (*name == 's' /* script variable */ 17337 && current_SID > 0 && current_SID <= ga_scripts.ga_len) 17338 return &SCRIPT_VARS(current_SID); 17339 return NULL; 17340 } 17341 17342 /* 17343 * Get the string value of a (global/local) variable. 17344 * Returns NULL when it doesn't exist. 17345 */ 17346 char_u * 17347 get_var_value(name) 17348 char_u *name; 17349 { 17350 dictitem_T *v; 17351 17352 v = find_var(name, NULL); 17353 if (v == NULL) 17354 return NULL; 17355 return get_tv_string(&v->di_tv); 17356 } 17357 17358 /* 17359 * Allocate a new hashtab for a sourced script. It will be used while 17360 * sourcing this script and when executing functions defined in the script. 17361 */ 17362 void 17363 new_script_vars(id) 17364 scid_T id; 17365 { 17366 int i; 17367 hashtab_T *ht; 17368 scriptvar_T *sv; 17369 17370 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK) 17371 { 17372 /* Re-allocating ga_data means that an ht_array pointing to 17373 * ht_smallarray becomes invalid. We can recognize this: ht_mask is 17374 * at its init value. Also reset "v_dict", it's always the same. */ 17375 for (i = 1; i <= ga_scripts.ga_len; ++i) 17376 { 17377 ht = &SCRIPT_VARS(i); 17378 if (ht->ht_mask == HT_INIT_SIZE - 1) 17379 ht->ht_array = ht->ht_smallarray; 17380 sv = &SCRIPT_SV(i); 17381 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict; 17382 } 17383 17384 while (ga_scripts.ga_len < id) 17385 { 17386 sv = &SCRIPT_SV(ga_scripts.ga_len + 1); 17387 init_var_dict(&sv->sv_dict, &sv->sv_var); 17388 ++ga_scripts.ga_len; 17389 } 17390 } 17391 } 17392 17393 /* 17394 * Initialize dictionary "dict" as a scope and set variable "dict_var" to 17395 * point to it. 17396 */ 17397 void 17398 init_var_dict(dict, dict_var) 17399 dict_T *dict; 17400 dictitem_T *dict_var; 17401 { 17402 hash_init(&dict->dv_hashtab); 17403 dict->dv_refcount = 99999; 17404 dict_var->di_tv.vval.v_dict = dict; 17405 dict_var->di_tv.v_type = VAR_DICT; 17406 dict_var->di_tv.v_lock = VAR_FIXED; 17407 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 17408 dict_var->di_key[0] = NUL; 17409 } 17410 17411 /* 17412 * Clean up a list of internal variables. 17413 * Frees all allocated variables and the value they contain. 17414 * Clears hashtab "ht", does not free it. 17415 */ 17416 void 17417 vars_clear(ht) 17418 hashtab_T *ht; 17419 { 17420 vars_clear_ext(ht, TRUE); 17421 } 17422 17423 /* 17424 * Like vars_clear(), but only free the value if "free_val" is TRUE. 17425 */ 17426 static void 17427 vars_clear_ext(ht, free_val) 17428 hashtab_T *ht; 17429 int free_val; 17430 { 17431 int todo; 17432 hashitem_T *hi; 17433 dictitem_T *v; 17434 17435 hash_lock(ht); 17436 todo = ht->ht_used; 17437 for (hi = ht->ht_array; todo > 0; ++hi) 17438 { 17439 if (!HASHITEM_EMPTY(hi)) 17440 { 17441 --todo; 17442 17443 /* Free the variable. Don't remove it from the hashtab, 17444 * ht_array might change then. hash_clear() takes care of it 17445 * later. */ 17446 v = HI2DI(hi); 17447 if (free_val) 17448 clear_tv(&v->di_tv); 17449 if ((v->di_flags & DI_FLAGS_FIX) == 0) 17450 vim_free(v); 17451 } 17452 } 17453 hash_clear(ht); 17454 ht->ht_used = 0; 17455 } 17456 17457 /* 17458 * Delete a variable from hashtab "ht" at item "hi". 17459 * Clear the variable value and free the dictitem. 17460 */ 17461 static void 17462 delete_var(ht, hi) 17463 hashtab_T *ht; 17464 hashitem_T *hi; 17465 { 17466 dictitem_T *di = HI2DI(hi); 17467 17468 hash_remove(ht, hi); 17469 clear_tv(&di->di_tv); 17470 vim_free(di); 17471 } 17472 17473 /* 17474 * List the value of one internal variable. 17475 */ 17476 static void 17477 list_one_var(v, prefix) 17478 dictitem_T *v; 17479 char_u *prefix; 17480 { 17481 char_u *tofree; 17482 char_u *s; 17483 char_u numbuf[NUMBUFLEN]; 17484 17485 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID); 17486 list_one_var_a(prefix, v->di_key, v->di_tv.v_type, 17487 s == NULL ? (char_u *)"" : s); 17488 vim_free(tofree); 17489 } 17490 17491 static void 17492 list_one_var_a(prefix, name, type, string) 17493 char_u *prefix; 17494 char_u *name; 17495 int type; 17496 char_u *string; 17497 { 17498 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */ 17499 if (name != NULL) /* "a:" vars don't have a name stored */ 17500 msg_puts(name); 17501 msg_putchar(' '); 17502 msg_advance(22); 17503 if (type == VAR_NUMBER) 17504 msg_putchar('#'); 17505 else if (type == VAR_FUNC) 17506 msg_putchar('*'); 17507 else if (type == VAR_LIST) 17508 { 17509 msg_putchar('['); 17510 if (*string == '[') 17511 ++string; 17512 } 17513 else if (type == VAR_DICT) 17514 { 17515 msg_putchar('{'); 17516 if (*string == '{') 17517 ++string; 17518 } 17519 else 17520 msg_putchar(' '); 17521 17522 msg_outtrans(string); 17523 17524 if (type == VAR_FUNC) 17525 msg_puts((char_u *)"()"); 17526 } 17527 17528 /* 17529 * Set variable "name" to value in "tv". 17530 * If the variable already exists, the value is updated. 17531 * Otherwise the variable is created. 17532 */ 17533 static void 17534 set_var(name, tv, copy) 17535 char_u *name; 17536 typval_T *tv; 17537 int copy; /* make copy of value in "tv" */ 17538 { 17539 dictitem_T *v; 17540 char_u *varname; 17541 hashtab_T *ht; 17542 char_u *p; 17543 17544 if (tv->v_type == VAR_FUNC) 17545 { 17546 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':') 17547 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':') 17548 ? name[2] : name[0])) 17549 { 17550 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name); 17551 return; 17552 } 17553 if (function_exists(name)) 17554 { 17555 EMSG2(_("E705: Variable name conflicts with existing function: %s"), 17556 name); 17557 return; 17558 } 17559 } 17560 17561 ht = find_var_ht(name, &varname); 17562 if (ht == NULL || *varname == NUL) 17563 { 17564 EMSG2(_(e_illvar), name); 17565 return; 17566 } 17567 17568 v = find_var_in_ht(ht, varname, TRUE); 17569 if (v != NULL) 17570 { 17571 /* existing variable, need to clear the value */ 17572 if (var_check_ro(v->di_flags, name) 17573 || tv_check_lock(v->di_tv.v_lock, name)) 17574 return; 17575 if (v->di_tv.v_type != tv->v_type 17576 && !((v->di_tv.v_type == VAR_STRING 17577 || v->di_tv.v_type == VAR_NUMBER) 17578 && (tv->v_type == VAR_STRING 17579 || tv->v_type == VAR_NUMBER))) 17580 { 17581 EMSG2(_("E706: Variable type mismatch for: %s"), name); 17582 return; 17583 } 17584 17585 /* 17586 * Handle setting internal v: variables separately: we don't change 17587 * the type. 17588 */ 17589 if (ht == &vimvarht) 17590 { 17591 if (v->di_tv.v_type == VAR_STRING) 17592 { 17593 vim_free(v->di_tv.vval.v_string); 17594 if (copy || tv->v_type != VAR_STRING) 17595 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv)); 17596 else 17597 { 17598 /* Take over the string to avoid an extra alloc/free. */ 17599 v->di_tv.vval.v_string = tv->vval.v_string; 17600 tv->vval.v_string = NULL; 17601 } 17602 } 17603 else if (v->di_tv.v_type != VAR_NUMBER) 17604 EMSG2(_(e_intern2), "set_var()"); 17605 else 17606 v->di_tv.vval.v_number = get_tv_number(tv); 17607 return; 17608 } 17609 17610 clear_tv(&v->di_tv); 17611 } 17612 else /* add a new variable */ 17613 { 17614 /* Make sure the variable name is valid. */ 17615 for (p = varname; *p != NUL; ++p) 17616 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)) 17617 && *p != AUTOLOAD_CHAR) 17618 { 17619 EMSG2(_(e_illvar), varname); 17620 return; 17621 } 17622 17623 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) 17624 + STRLEN(varname))); 17625 if (v == NULL) 17626 return; 17627 STRCPY(v->di_key, varname); 17628 if (hash_add(ht, DI2HIKEY(v)) == FAIL) 17629 { 17630 vim_free(v); 17631 return; 17632 } 17633 v->di_flags = 0; 17634 } 17635 17636 if (copy || tv->v_type == VAR_NUMBER) 17637 copy_tv(tv, &v->di_tv); 17638 else 17639 { 17640 v->di_tv = *tv; 17641 v->di_tv.v_lock = 0; 17642 init_tv(tv); 17643 } 17644 } 17645 17646 /* 17647 * Return TRUE if di_flags "flags" indicate read-only variable "name". 17648 * Also give an error message. 17649 */ 17650 static int 17651 var_check_ro(flags, name) 17652 int flags; 17653 char_u *name; 17654 { 17655 if (flags & DI_FLAGS_RO) 17656 { 17657 EMSG2(_(e_readonlyvar), name); 17658 return TRUE; 17659 } 17660 if ((flags & DI_FLAGS_RO_SBX) && sandbox) 17661 { 17662 EMSG2(_(e_readonlysbx), name); 17663 return TRUE; 17664 } 17665 return FALSE; 17666 } 17667 17668 /* 17669 * Return TRUE if typeval "tv" is set to be locked (immutable). 17670 * Also give an error message, using "name". 17671 */ 17672 static int 17673 tv_check_lock(lock, name) 17674 int lock; 17675 char_u *name; 17676 { 17677 if (lock & VAR_LOCKED) 17678 { 17679 EMSG2(_("E741: Value is locked: %s"), 17680 name == NULL ? (char_u *)_("Unknown") : name); 17681 return TRUE; 17682 } 17683 if (lock & VAR_FIXED) 17684 { 17685 EMSG2(_("E742: Cannot change value of %s"), 17686 name == NULL ? (char_u *)_("Unknown") : name); 17687 return TRUE; 17688 } 17689 return FALSE; 17690 } 17691 17692 /* 17693 * Copy the values from typval_T "from" to typval_T "to". 17694 * When needed allocates string or increases reference count. 17695 * Does not make a copy of a list or dict but copies the reference! 17696 */ 17697 static void 17698 copy_tv(from, to) 17699 typval_T *from; 17700 typval_T *to; 17701 { 17702 to->v_type = from->v_type; 17703 to->v_lock = 0; 17704 switch (from->v_type) 17705 { 17706 case VAR_NUMBER: 17707 to->vval.v_number = from->vval.v_number; 17708 break; 17709 case VAR_STRING: 17710 case VAR_FUNC: 17711 if (from->vval.v_string == NULL) 17712 to->vval.v_string = NULL; 17713 else 17714 { 17715 to->vval.v_string = vim_strsave(from->vval.v_string); 17716 if (from->v_type == VAR_FUNC) 17717 func_ref(to->vval.v_string); 17718 } 17719 break; 17720 case VAR_LIST: 17721 if (from->vval.v_list == NULL) 17722 to->vval.v_list = NULL; 17723 else 17724 { 17725 to->vval.v_list = from->vval.v_list; 17726 ++to->vval.v_list->lv_refcount; 17727 } 17728 break; 17729 case VAR_DICT: 17730 if (from->vval.v_dict == NULL) 17731 to->vval.v_dict = NULL; 17732 else 17733 { 17734 to->vval.v_dict = from->vval.v_dict; 17735 ++to->vval.v_dict->dv_refcount; 17736 } 17737 break; 17738 default: 17739 EMSG2(_(e_intern2), "copy_tv()"); 17740 break; 17741 } 17742 } 17743 17744 /* 17745 * Make a copy of an item. 17746 * Lists and Dictionaries are also copied. A deep copy if "deep" is set. 17747 * For deepcopy() "copyID" is zero for a full copy or the ID for when a 17748 * reference to an already copied list/dict can be used. 17749 * Returns FAIL or OK. 17750 */ 17751 static int 17752 item_copy(from, to, deep, copyID) 17753 typval_T *from; 17754 typval_T *to; 17755 int deep; 17756 int copyID; 17757 { 17758 static int recurse = 0; 17759 int ret = OK; 17760 17761 if (recurse >= DICT_MAXNEST) 17762 { 17763 EMSG(_("E698: variable nested too deep for making a copy")); 17764 return FAIL; 17765 } 17766 ++recurse; 17767 17768 switch (from->v_type) 17769 { 17770 case VAR_NUMBER: 17771 case VAR_STRING: 17772 case VAR_FUNC: 17773 copy_tv(from, to); 17774 break; 17775 case VAR_LIST: 17776 to->v_type = VAR_LIST; 17777 to->v_lock = 0; 17778 if (from->vval.v_list == NULL) 17779 to->vval.v_list = NULL; 17780 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID) 17781 { 17782 /* use the copy made earlier */ 17783 to->vval.v_list = from->vval.v_list->lv_copylist; 17784 ++to->vval.v_list->lv_refcount; 17785 } 17786 else 17787 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID); 17788 if (to->vval.v_list == NULL) 17789 ret = FAIL; 17790 break; 17791 case VAR_DICT: 17792 to->v_type = VAR_DICT; 17793 to->v_lock = 0; 17794 if (from->vval.v_dict == NULL) 17795 to->vval.v_dict = NULL; 17796 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID) 17797 { 17798 /* use the copy made earlier */ 17799 to->vval.v_dict = from->vval.v_dict->dv_copydict; 17800 ++to->vval.v_dict->dv_refcount; 17801 } 17802 else 17803 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID); 17804 if (to->vval.v_dict == NULL) 17805 ret = FAIL; 17806 break; 17807 default: 17808 EMSG2(_(e_intern2), "item_copy()"); 17809 ret = FAIL; 17810 } 17811 --recurse; 17812 return ret; 17813 } 17814 17815 /* 17816 * ":echo expr1 ..." print each argument separated with a space, add a 17817 * newline at the end. 17818 * ":echon expr1 ..." print each argument plain. 17819 */ 17820 void 17821 ex_echo(eap) 17822 exarg_T *eap; 17823 { 17824 char_u *arg = eap->arg; 17825 typval_T rettv; 17826 char_u *tofree; 17827 char_u *p; 17828 int needclr = TRUE; 17829 int atstart = TRUE; 17830 char_u numbuf[NUMBUFLEN]; 17831 17832 if (eap->skip) 17833 ++emsg_skip; 17834 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int) 17835 { 17836 p = arg; 17837 if (eval1(&arg, &rettv, !eap->skip) == FAIL) 17838 { 17839 /* 17840 * Report the invalid expression unless the expression evaluation 17841 * has been cancelled due to an aborting error, an interrupt, or an 17842 * exception. 17843 */ 17844 if (!aborting()) 17845 EMSG2(_(e_invexpr2), p); 17846 break; 17847 } 17848 if (!eap->skip) 17849 { 17850 if (atstart) 17851 { 17852 atstart = FALSE; 17853 /* Call msg_start() after eval1(), evaluating the expression 17854 * may cause a message to appear. */ 17855 if (eap->cmdidx == CMD_echo) 17856 msg_start(); 17857 } 17858 else if (eap->cmdidx == CMD_echo) 17859 msg_puts_attr((char_u *)" ", echo_attr); 17860 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID); 17861 if (p != NULL) 17862 for ( ; *p != NUL && !got_int; ++p) 17863 { 17864 if (*p == '\n' || *p == '\r' || *p == TAB) 17865 { 17866 if (*p != TAB && needclr) 17867 { 17868 /* remove any text still there from the command */ 17869 msg_clr_eos(); 17870 needclr = FALSE; 17871 } 17872 msg_putchar_attr(*p, echo_attr); 17873 } 17874 else 17875 { 17876 #ifdef FEAT_MBYTE 17877 if (has_mbyte) 17878 { 17879 int i = (*mb_ptr2len)(p); 17880 17881 (void)msg_outtrans_len_attr(p, i, echo_attr); 17882 p += i - 1; 17883 } 17884 else 17885 #endif 17886 (void)msg_outtrans_len_attr(p, 1, echo_attr); 17887 } 17888 } 17889 vim_free(tofree); 17890 } 17891 clear_tv(&rettv); 17892 arg = skipwhite(arg); 17893 } 17894 eap->nextcmd = check_nextcmd(arg); 17895 17896 if (eap->skip) 17897 --emsg_skip; 17898 else 17899 { 17900 /* remove text that may still be there from the command */ 17901 if (needclr) 17902 msg_clr_eos(); 17903 if (eap->cmdidx == CMD_echo) 17904 msg_end(); 17905 } 17906 } 17907 17908 /* 17909 * ":echohl {name}". 17910 */ 17911 void 17912 ex_echohl(eap) 17913 exarg_T *eap; 17914 { 17915 int id; 17916 17917 id = syn_name2id(eap->arg); 17918 if (id == 0) 17919 echo_attr = 0; 17920 else 17921 echo_attr = syn_id2attr(id); 17922 } 17923 17924 /* 17925 * ":execute expr1 ..." execute the result of an expression. 17926 * ":echomsg expr1 ..." Print a message 17927 * ":echoerr expr1 ..." Print an error 17928 * Each gets spaces around each argument and a newline at the end for 17929 * echo commands 17930 */ 17931 void 17932 ex_execute(eap) 17933 exarg_T *eap; 17934 { 17935 char_u *arg = eap->arg; 17936 typval_T rettv; 17937 int ret = OK; 17938 char_u *p; 17939 garray_T ga; 17940 int len; 17941 int save_did_emsg; 17942 17943 ga_init2(&ga, 1, 80); 17944 17945 if (eap->skip) 17946 ++emsg_skip; 17947 while (*arg != NUL && *arg != '|' && *arg != '\n') 17948 { 17949 p = arg; 17950 if (eval1(&arg, &rettv, !eap->skip) == FAIL) 17951 { 17952 /* 17953 * Report the invalid expression unless the expression evaluation 17954 * has been cancelled due to an aborting error, an interrupt, or an 17955 * exception. 17956 */ 17957 if (!aborting()) 17958 EMSG2(_(e_invexpr2), p); 17959 ret = FAIL; 17960 break; 17961 } 17962 17963 if (!eap->skip) 17964 { 17965 p = get_tv_string(&rettv); 17966 len = (int)STRLEN(p); 17967 if (ga_grow(&ga, len + 2) == FAIL) 17968 { 17969 clear_tv(&rettv); 17970 ret = FAIL; 17971 break; 17972 } 17973 if (ga.ga_len) 17974 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 17975 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 17976 ga.ga_len += len; 17977 } 17978 17979 clear_tv(&rettv); 17980 arg = skipwhite(arg); 17981 } 17982 17983 if (ret != FAIL && ga.ga_data != NULL) 17984 { 17985 if (eap->cmdidx == CMD_echomsg) 17986 { 17987 MSG_ATTR(ga.ga_data, echo_attr); 17988 out_flush(); 17989 } 17990 else if (eap->cmdidx == CMD_echoerr) 17991 { 17992 /* We don't want to abort following commands, restore did_emsg. */ 17993 save_did_emsg = did_emsg; 17994 EMSG((char_u *)ga.ga_data); 17995 if (!force_abort) 17996 did_emsg = save_did_emsg; 17997 } 17998 else if (eap->cmdidx == CMD_execute) 17999 do_cmdline((char_u *)ga.ga_data, 18000 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE); 18001 } 18002 18003 ga_clear(&ga); 18004 18005 if (eap->skip) 18006 --emsg_skip; 18007 18008 eap->nextcmd = check_nextcmd(arg); 18009 } 18010 18011 /* 18012 * Skip over the name of an option: "&option", "&g:option" or "&l:option". 18013 * "arg" points to the "&" or '+' when called, to "option" when returning. 18014 * Returns NULL when no option name found. Otherwise pointer to the char 18015 * after the option name. 18016 */ 18017 static char_u * 18018 find_option_end(arg, opt_flags) 18019 char_u **arg; 18020 int *opt_flags; 18021 { 18022 char_u *p = *arg; 18023 18024 ++p; 18025 if (*p == 'g' && p[1] == ':') 18026 { 18027 *opt_flags = OPT_GLOBAL; 18028 p += 2; 18029 } 18030 else if (*p == 'l' && p[1] == ':') 18031 { 18032 *opt_flags = OPT_LOCAL; 18033 p += 2; 18034 } 18035 else 18036 *opt_flags = 0; 18037 18038 if (!ASCII_ISALPHA(*p)) 18039 return NULL; 18040 *arg = p; 18041 18042 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL) 18043 p += 4; /* termcap option */ 18044 else 18045 while (ASCII_ISALPHA(*p)) 18046 ++p; 18047 return p; 18048 } 18049 18050 /* 18051 * ":function" 18052 */ 18053 void 18054 ex_function(eap) 18055 exarg_T *eap; 18056 { 18057 char_u *theline; 18058 int j; 18059 int c; 18060 int saved_did_emsg; 18061 char_u *name = NULL; 18062 char_u *p; 18063 char_u *arg; 18064 char_u *line_arg = NULL; 18065 garray_T newargs; 18066 garray_T newlines; 18067 int varargs = FALSE; 18068 int mustend = FALSE; 18069 int flags = 0; 18070 ufunc_T *fp; 18071 int indent; 18072 int nesting; 18073 char_u *skip_until = NULL; 18074 dictitem_T *v; 18075 funcdict_T fudi; 18076 static int func_nr = 0; /* number for nameless function */ 18077 int paren; 18078 hashtab_T *ht; 18079 int todo; 18080 hashitem_T *hi; 18081 int sourcing_lnum_off; 18082 18083 /* 18084 * ":function" without argument: list functions. 18085 */ 18086 if (ends_excmd(*eap->arg)) 18087 { 18088 if (!eap->skip) 18089 { 18090 todo = func_hashtab.ht_used; 18091 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) 18092 { 18093 if (!HASHITEM_EMPTY(hi)) 18094 { 18095 --todo; 18096 fp = HI2UF(hi); 18097 if (!isdigit(*fp->uf_name)) 18098 list_func_head(fp, FALSE); 18099 } 18100 } 18101 } 18102 eap->nextcmd = check_nextcmd(eap->arg); 18103 return; 18104 } 18105 18106 /* 18107 * ":function /pat": list functions matching pattern. 18108 */ 18109 if (*eap->arg == '/') 18110 { 18111 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL); 18112 if (!eap->skip) 18113 { 18114 regmatch_T regmatch; 18115 18116 c = *p; 18117 *p = NUL; 18118 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC); 18119 *p = c; 18120 if (regmatch.regprog != NULL) 18121 { 18122 regmatch.rm_ic = p_ic; 18123 18124 todo = func_hashtab.ht_used; 18125 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) 18126 { 18127 if (!HASHITEM_EMPTY(hi)) 18128 { 18129 --todo; 18130 fp = HI2UF(hi); 18131 if (!isdigit(*fp->uf_name) 18132 && vim_regexec(®match, fp->uf_name, 0)) 18133 list_func_head(fp, FALSE); 18134 } 18135 } 18136 } 18137 } 18138 if (*p == '/') 18139 ++p; 18140 eap->nextcmd = check_nextcmd(p); 18141 return; 18142 } 18143 18144 /* 18145 * Get the function name. There are these situations: 18146 * func normal function name 18147 * "name" == func, "fudi.fd_dict" == NULL 18148 * dict.func new dictionary entry 18149 * "name" == NULL, "fudi.fd_dict" set, 18150 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func 18151 * dict.func existing dict entry with a Funcref 18152 * "name" == func, "fudi.fd_dict" set, 18153 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL 18154 * dict.func existing dict entry that's not a Funcref 18155 * "name" == NULL, "fudi.fd_dict" set, 18156 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL 18157 */ 18158 p = eap->arg; 18159 name = trans_function_name(&p, eap->skip, 0, &fudi); 18160 paren = (vim_strchr(p, '(') != NULL); 18161 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip) 18162 { 18163 /* 18164 * Return on an invalid expression in braces, unless the expression 18165 * evaluation has been cancelled due to an aborting error, an 18166 * interrupt, or an exception. 18167 */ 18168 if (!aborting()) 18169 { 18170 if (!eap->skip && fudi.fd_newkey != NULL) 18171 EMSG2(_(e_dictkey), fudi.fd_newkey); 18172 vim_free(fudi.fd_newkey); 18173 return; 18174 } 18175 else 18176 eap->skip = TRUE; 18177 } 18178 18179 /* An error in a function call during evaluation of an expression in magic 18180 * braces should not cause the function not to be defined. */ 18181 saved_did_emsg = did_emsg; 18182 did_emsg = FALSE; 18183 18184 /* 18185 * ":function func" with only function name: list function. 18186 */ 18187 if (!paren) 18188 { 18189 if (!ends_excmd(*skipwhite(p))) 18190 { 18191 EMSG(_(e_trailing)); 18192 goto ret_free; 18193 } 18194 eap->nextcmd = check_nextcmd(p); 18195 if (eap->nextcmd != NULL) 18196 *p = NUL; 18197 if (!eap->skip && !got_int) 18198 { 18199 fp = find_func(name); 18200 if (fp != NULL) 18201 { 18202 list_func_head(fp, TRUE); 18203 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j) 18204 { 18205 if (FUNCLINE(fp, j) == NULL) 18206 continue; 18207 msg_putchar('\n'); 18208 msg_outnum((long)(j + 1)); 18209 if (j < 9) 18210 msg_putchar(' '); 18211 if (j < 99) 18212 msg_putchar(' '); 18213 msg_prt_line(FUNCLINE(fp, j), FALSE); 18214 out_flush(); /* show a line at a time */ 18215 ui_breakcheck(); 18216 } 18217 if (!got_int) 18218 { 18219 msg_putchar('\n'); 18220 msg_puts((char_u *)" endfunction"); 18221 } 18222 } 18223 else 18224 emsg_funcname("E123: Undefined function: %s", name); 18225 } 18226 goto ret_free; 18227 } 18228 18229 /* 18230 * ":function name(arg1, arg2)" Define function. 18231 */ 18232 p = skipwhite(p); 18233 if (*p != '(') 18234 { 18235 if (!eap->skip) 18236 { 18237 EMSG2(_("E124: Missing '(': %s"), eap->arg); 18238 goto ret_free; 18239 } 18240 /* attempt to continue by skipping some text */ 18241 if (vim_strchr(p, '(') != NULL) 18242 p = vim_strchr(p, '('); 18243 } 18244 p = skipwhite(p + 1); 18245 18246 ga_init2(&newargs, (int)sizeof(char_u *), 3); 18247 ga_init2(&newlines, (int)sizeof(char_u *), 3); 18248 18249 if (!eap->skip) 18250 { 18251 /* Check the name of the function. */ 18252 if (name != NULL) 18253 arg = name; 18254 else 18255 arg = fudi.fd_newkey; 18256 if (arg != NULL) 18257 { 18258 if (*arg == K_SPECIAL) 18259 j = 3; 18260 else 18261 j = 0; 18262 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j]) 18263 : eval_isnamec(arg[j]))) 18264 ++j; 18265 if (arg[j] != NUL) 18266 emsg_funcname(_(e_invarg2), arg); 18267 } 18268 } 18269 18270 /* 18271 * Isolate the arguments: "arg1, arg2, ...)" 18272 */ 18273 while (*p != ')') 18274 { 18275 if (p[0] == '.' && p[1] == '.' && p[2] == '.') 18276 { 18277 varargs = TRUE; 18278 p += 3; 18279 mustend = TRUE; 18280 } 18281 else 18282 { 18283 arg = p; 18284 while (ASCII_ISALNUM(*p) || *p == '_') 18285 ++p; 18286 if (arg == p || isdigit(*arg) 18287 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0) 18288 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0)) 18289 { 18290 if (!eap->skip) 18291 EMSG2(_("E125: Illegal argument: %s"), arg); 18292 break; 18293 } 18294 if (ga_grow(&newargs, 1) == FAIL) 18295 goto erret; 18296 c = *p; 18297 *p = NUL; 18298 arg = vim_strsave(arg); 18299 if (arg == NULL) 18300 goto erret; 18301 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg; 18302 *p = c; 18303 newargs.ga_len++; 18304 if (*p == ',') 18305 ++p; 18306 else 18307 mustend = TRUE; 18308 } 18309 p = skipwhite(p); 18310 if (mustend && *p != ')') 18311 { 18312 if (!eap->skip) 18313 EMSG2(_(e_invarg2), eap->arg); 18314 break; 18315 } 18316 } 18317 ++p; /* skip the ')' */ 18318 18319 /* find extra arguments "range", "dict" and "abort" */ 18320 for (;;) 18321 { 18322 p = skipwhite(p); 18323 if (STRNCMP(p, "range", 5) == 0) 18324 { 18325 flags |= FC_RANGE; 18326 p += 5; 18327 } 18328 else if (STRNCMP(p, "dict", 4) == 0) 18329 { 18330 flags |= FC_DICT; 18331 p += 4; 18332 } 18333 else if (STRNCMP(p, "abort", 5) == 0) 18334 { 18335 flags |= FC_ABORT; 18336 p += 5; 18337 } 18338 else 18339 break; 18340 } 18341 18342 /* When there is a line break use what follows for the function body. 18343 * Makes 'exe "func Test()\n...\nendfunc"' work. */ 18344 if (*p == '\n') 18345 line_arg = p + 1; 18346 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg) 18347 EMSG(_(e_trailing)); 18348 18349 /* 18350 * Read the body of the function, until ":endfunction" is found. 18351 */ 18352 if (KeyTyped) 18353 { 18354 /* Check if the function already exists, don't let the user type the 18355 * whole function before telling him it doesn't work! For a script we 18356 * need to skip the body to be able to find what follows. */ 18357 if (!eap->skip && !eap->forceit) 18358 { 18359 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL) 18360 EMSG(_(e_funcdict)); 18361 else if (name != NULL && find_func(name) != NULL) 18362 emsg_funcname(e_funcexts, name); 18363 } 18364 18365 if (!eap->skip && did_emsg) 18366 goto erret; 18367 18368 msg_putchar('\n'); /* don't overwrite the function name */ 18369 cmdline_row = msg_row; 18370 } 18371 18372 indent = 2; 18373 nesting = 0; 18374 for (;;) 18375 { 18376 msg_scroll = TRUE; 18377 need_wait_return = FALSE; 18378 sourcing_lnum_off = sourcing_lnum; 18379 18380 if (line_arg != NULL) 18381 { 18382 /* Use eap->arg, split up in parts by line breaks. */ 18383 theline = line_arg; 18384 p = vim_strchr(theline, '\n'); 18385 if (p == NULL) 18386 line_arg += STRLEN(line_arg); 18387 else 18388 { 18389 *p = NUL; 18390 line_arg = p + 1; 18391 } 18392 } 18393 else if (eap->getline == NULL) 18394 theline = getcmdline(':', 0L, indent); 18395 else 18396 theline = eap->getline(':', eap->cookie, indent); 18397 if (KeyTyped) 18398 lines_left = Rows - 1; 18399 if (theline == NULL) 18400 { 18401 EMSG(_("E126: Missing :endfunction")); 18402 goto erret; 18403 } 18404 18405 /* Detect line continuation: sourcing_lnum increased more than one. */ 18406 if (sourcing_lnum > sourcing_lnum_off + 1) 18407 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1; 18408 else 18409 sourcing_lnum_off = 0; 18410 18411 if (skip_until != NULL) 18412 { 18413 /* between ":append" and "." and between ":python <<EOF" and "EOF" 18414 * don't check for ":endfunc". */ 18415 if (STRCMP(theline, skip_until) == 0) 18416 { 18417 vim_free(skip_until); 18418 skip_until = NULL; 18419 } 18420 } 18421 else 18422 { 18423 /* skip ':' and blanks*/ 18424 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p) 18425 ; 18426 18427 /* Check for "endfunction". */ 18428 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0) 18429 { 18430 if (line_arg == NULL) 18431 vim_free(theline); 18432 break; 18433 } 18434 18435 /* Increase indent inside "if", "while", "for" and "try", decrease 18436 * at "end". */ 18437 if (indent > 2 && STRNCMP(p, "end", 3) == 0) 18438 indent -= 2; 18439 else if (STRNCMP(p, "if", 2) == 0 18440 || STRNCMP(p, "wh", 2) == 0 18441 || STRNCMP(p, "for", 3) == 0 18442 || STRNCMP(p, "try", 3) == 0) 18443 indent += 2; 18444 18445 /* Check for defining a function inside this function. */ 18446 if (checkforcmd(&p, "function", 2)) 18447 { 18448 if (*p == '!') 18449 p = skipwhite(p + 1); 18450 p += eval_fname_script(p); 18451 if (ASCII_ISALPHA(*p)) 18452 { 18453 vim_free(trans_function_name(&p, TRUE, 0, NULL)); 18454 if (*skipwhite(p) == '(') 18455 { 18456 ++nesting; 18457 indent += 2; 18458 } 18459 } 18460 } 18461 18462 /* Check for ":append" or ":insert". */ 18463 p = skip_range(p, NULL); 18464 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p')) 18465 || (p[0] == 'i' 18466 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n' 18467 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's')))))) 18468 skip_until = vim_strsave((char_u *)"."); 18469 18470 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */ 18471 arg = skipwhite(skiptowhite(p)); 18472 if (arg[0] == '<' && arg[1] =='<' 18473 && ((p[0] == 'p' && p[1] == 'y' 18474 && (!ASCII_ISALPHA(p[2]) || p[2] == 't')) 18475 || (p[0] == 'p' && p[1] == 'e' 18476 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r')) 18477 || (p[0] == 't' && p[1] == 'c' 18478 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l')) 18479 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b' 18480 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y')) 18481 || (p[0] == 'm' && p[1] == 'z' 18482 && (!ASCII_ISALPHA(p[2]) || p[2] == 's')) 18483 )) 18484 { 18485 /* ":python <<" continues until a dot, like ":append" */ 18486 p = skipwhite(arg + 2); 18487 if (*p == NUL) 18488 skip_until = vim_strsave((char_u *)"."); 18489 else 18490 skip_until = vim_strsave(p); 18491 } 18492 } 18493 18494 /* Add the line to the function. */ 18495 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL) 18496 { 18497 if (line_arg == NULL) 18498 vim_free(theline); 18499 goto erret; 18500 } 18501 18502 /* Copy the line to newly allocated memory. get_one_sourceline() 18503 * allocates 250 bytes per line, this saves 80% on average. The cost 18504 * is an extra alloc/free. */ 18505 p = vim_strsave(theline); 18506 if (p != NULL) 18507 { 18508 if (line_arg == NULL) 18509 vim_free(theline); 18510 theline = p; 18511 } 18512 18513 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline; 18514 18515 /* Add NULL lines for continuation lines, so that the line count is 18516 * equal to the index in the growarray. */ 18517 while (sourcing_lnum_off-- > 0) 18518 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL; 18519 18520 /* Check for end of eap->arg. */ 18521 if (line_arg != NULL && *line_arg == NUL) 18522 line_arg = NULL; 18523 } 18524 18525 /* Don't define the function when skipping commands or when an error was 18526 * detected. */ 18527 if (eap->skip || did_emsg) 18528 goto erret; 18529 18530 /* 18531 * If there are no errors, add the function 18532 */ 18533 if (fudi.fd_dict == NULL) 18534 { 18535 v = find_var(name, &ht); 18536 if (v != NULL && v->di_tv.v_type == VAR_FUNC) 18537 { 18538 emsg_funcname("E707: Function name conflicts with variable: %s", 18539 name); 18540 goto erret; 18541 } 18542 18543 fp = find_func(name); 18544 if (fp != NULL) 18545 { 18546 if (!eap->forceit) 18547 { 18548 emsg_funcname(e_funcexts, name); 18549 goto erret; 18550 } 18551 if (fp->uf_calls > 0) 18552 { 18553 emsg_funcname("E127: Cannot redefine function %s: It is in use", 18554 name); 18555 goto erret; 18556 } 18557 /* redefine existing function */ 18558 ga_clear_strings(&(fp->uf_args)); 18559 ga_clear_strings(&(fp->uf_lines)); 18560 vim_free(name); 18561 name = NULL; 18562 } 18563 } 18564 else 18565 { 18566 char numbuf[20]; 18567 18568 fp = NULL; 18569 if (fudi.fd_newkey == NULL && !eap->forceit) 18570 { 18571 EMSG(_(e_funcdict)); 18572 goto erret; 18573 } 18574 if (fudi.fd_di == NULL) 18575 { 18576 /* Can't add a function to a locked dictionary */ 18577 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg)) 18578 goto erret; 18579 } 18580 /* Can't change an existing function if it is locked */ 18581 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg)) 18582 goto erret; 18583 18584 /* Give the function a sequential number. Can only be used with a 18585 * Funcref! */ 18586 vim_free(name); 18587 sprintf(numbuf, "%d", ++func_nr); 18588 name = vim_strsave((char_u *)numbuf); 18589 if (name == NULL) 18590 goto erret; 18591 } 18592 18593 if (fp == NULL) 18594 { 18595 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL) 18596 { 18597 int slen, plen; 18598 char_u *scriptname; 18599 18600 /* Check that the autoload name matches the script name. */ 18601 j = FAIL; 18602 if (sourcing_name != NULL) 18603 { 18604 scriptname = autoload_name(name); 18605 if (scriptname != NULL) 18606 { 18607 p = vim_strchr(scriptname, '/'); 18608 plen = STRLEN(p); 18609 slen = STRLEN(sourcing_name); 18610 if (slen > plen && fnamecmp(p, 18611 sourcing_name + slen - plen) == 0) 18612 j = OK; 18613 vim_free(scriptname); 18614 } 18615 } 18616 if (j == FAIL) 18617 { 18618 EMSG2(_("E746: Function name does not match script file name: %s"), name); 18619 goto erret; 18620 } 18621 } 18622 18623 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name))); 18624 if (fp == NULL) 18625 goto erret; 18626 18627 if (fudi.fd_dict != NULL) 18628 { 18629 if (fudi.fd_di == NULL) 18630 { 18631 /* add new dict entry */ 18632 fudi.fd_di = dictitem_alloc(fudi.fd_newkey); 18633 if (fudi.fd_di == NULL) 18634 { 18635 vim_free(fp); 18636 goto erret; 18637 } 18638 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL) 18639 { 18640 vim_free(fudi.fd_di); 18641 goto erret; 18642 } 18643 } 18644 else 18645 /* overwrite existing dict entry */ 18646 clear_tv(&fudi.fd_di->di_tv); 18647 fudi.fd_di->di_tv.v_type = VAR_FUNC; 18648 fudi.fd_di->di_tv.v_lock = 0; 18649 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name); 18650 fp->uf_refcount = 1; 18651 18652 /* behave like "dict" was used */ 18653 flags |= FC_DICT; 18654 } 18655 18656 /* insert the new function in the function list */ 18657 STRCPY(fp->uf_name, name); 18658 hash_add(&func_hashtab, UF2HIKEY(fp)); 18659 } 18660 fp->uf_args = newargs; 18661 fp->uf_lines = newlines; 18662 #ifdef FEAT_PROFILE 18663 fp->uf_tml_count = NULL; 18664 fp->uf_tml_total = NULL; 18665 fp->uf_tml_self = NULL; 18666 fp->uf_profiling = FALSE; 18667 if (prof_def_func()) 18668 func_do_profile(fp); 18669 #endif 18670 fp->uf_varargs = varargs; 18671 fp->uf_flags = flags; 18672 fp->uf_calls = 0; 18673 fp->uf_script_ID = current_SID; 18674 goto ret_free; 18675 18676 erret: 18677 ga_clear_strings(&newargs); 18678 ga_clear_strings(&newlines); 18679 ret_free: 18680 vim_free(skip_until); 18681 vim_free(fudi.fd_newkey); 18682 vim_free(name); 18683 did_emsg |= saved_did_emsg; 18684 } 18685 18686 /* 18687 * Get a function name, translating "<SID>" and "<SNR>". 18688 * Also handles a Funcref in a List or Dictionary. 18689 * Returns the function name in allocated memory, or NULL for failure. 18690 * flags: 18691 * TFN_INT: internal function name OK 18692 * TFN_QUIET: be quiet 18693 * Advances "pp" to just after the function name (if no error). 18694 */ 18695 static char_u * 18696 trans_function_name(pp, skip, flags, fdp) 18697 char_u **pp; 18698 int skip; /* only find the end, don't evaluate */ 18699 int flags; 18700 funcdict_T *fdp; /* return: info about dictionary used */ 18701 { 18702 char_u *name = NULL; 18703 char_u *start; 18704 char_u *end; 18705 int lead; 18706 char_u sid_buf[20]; 18707 int len; 18708 lval_T lv; 18709 18710 if (fdp != NULL) 18711 vim_memset(fdp, 0, sizeof(funcdict_T)); 18712 start = *pp; 18713 18714 /* Check for hard coded <SNR>: already translated function ID (from a user 18715 * command). */ 18716 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA 18717 && (*pp)[2] == (int)KE_SNR) 18718 { 18719 *pp += 3; 18720 len = get_id_len(pp) + 3; 18721 return vim_strnsave(start, len); 18722 } 18723 18724 /* A name starting with "<SID>" or "<SNR>" is local to a script. But 18725 * don't skip over "s:", get_lval() needs it for "s:dict.func". */ 18726 lead = eval_fname_script(start); 18727 if (lead > 2) 18728 start += lead; 18729 18730 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET, 18731 lead > 2 ? 0 : FNE_CHECK_START); 18732 if (end == start) 18733 { 18734 if (!skip) 18735 EMSG(_("E129: Function name required")); 18736 goto theend; 18737 } 18738 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range))) 18739 { 18740 /* 18741 * Report an invalid expression in braces, unless the expression 18742 * evaluation has been cancelled due to an aborting error, an 18743 * interrupt, or an exception. 18744 */ 18745 if (!aborting()) 18746 { 18747 if (end != NULL) 18748 EMSG2(_(e_invarg2), start); 18749 } 18750 else 18751 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR); 18752 goto theend; 18753 } 18754 18755 if (lv.ll_tv != NULL) 18756 { 18757 if (fdp != NULL) 18758 { 18759 fdp->fd_dict = lv.ll_dict; 18760 fdp->fd_newkey = lv.ll_newkey; 18761 lv.ll_newkey = NULL; 18762 fdp->fd_di = lv.ll_di; 18763 } 18764 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL) 18765 { 18766 name = vim_strsave(lv.ll_tv->vval.v_string); 18767 *pp = end; 18768 } 18769 else 18770 { 18771 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL 18772 || lv.ll_dict == NULL || fdp->fd_newkey == NULL)) 18773 EMSG(_(e_funcref)); 18774 else 18775 *pp = end; 18776 name = NULL; 18777 } 18778 goto theend; 18779 } 18780 18781 if (lv.ll_name == NULL) 18782 { 18783 /* Error found, but continue after the function name. */ 18784 *pp = end; 18785 goto theend; 18786 } 18787 18788 if (lv.ll_exp_name != NULL) 18789 { 18790 len = STRLEN(lv.ll_exp_name); 18791 if (lead <= 2 && lv.ll_name == lv.ll_exp_name 18792 && STRNCMP(lv.ll_name, "s:", 2) == 0) 18793 { 18794 /* When there was "s:" already or the name expanded to get a 18795 * leading "s:" then remove it. */ 18796 lv.ll_name += 2; 18797 len -= 2; 18798 lead = 2; 18799 } 18800 } 18801 else 18802 { 18803 if (lead == 2) /* skip over "s:" */ 18804 lv.ll_name += 2; 18805 len = (int)(end - lv.ll_name); 18806 } 18807 18808 /* 18809 * Copy the function name to allocated memory. 18810 * Accept <SID>name() inside a script, translate into <SNR>123_name(). 18811 * Accept <SNR>123_name() outside a script. 18812 */ 18813 if (skip) 18814 lead = 0; /* do nothing */ 18815 else if (lead > 0) 18816 { 18817 lead = 3; 18818 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp)) 18819 { 18820 /* It's "s:" or "<SID>" */ 18821 if (current_SID <= 0) 18822 { 18823 EMSG(_(e_usingsid)); 18824 goto theend; 18825 } 18826 sprintf((char *)sid_buf, "%ld_", (long)current_SID); 18827 lead += (int)STRLEN(sid_buf); 18828 } 18829 } 18830 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name)) 18831 { 18832 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name); 18833 goto theend; 18834 } 18835 name = alloc((unsigned)(len + lead + 1)); 18836 if (name != NULL) 18837 { 18838 if (lead > 0) 18839 { 18840 name[0] = K_SPECIAL; 18841 name[1] = KS_EXTRA; 18842 name[2] = (int)KE_SNR; 18843 if (lead > 3) /* If it's "<SID>" */ 18844 STRCPY(name + 3, sid_buf); 18845 } 18846 mch_memmove(name + lead, lv.ll_name, (size_t)len); 18847 name[len + lead] = NUL; 18848 } 18849 *pp = end; 18850 18851 theend: 18852 clear_lval(&lv); 18853 return name; 18854 } 18855 18856 /* 18857 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case). 18858 * Return 2 if "p" starts with "s:". 18859 * Return 0 otherwise. 18860 */ 18861 static int 18862 eval_fname_script(p) 18863 char_u *p; 18864 { 18865 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0 18866 || STRNICMP(p + 1, "SNR>", 4) == 0)) 18867 return 5; 18868 if (p[0] == 's' && p[1] == ':') 18869 return 2; 18870 return 0; 18871 } 18872 18873 /* 18874 * Return TRUE if "p" starts with "<SID>" or "s:". 18875 * Only works if eval_fname_script() returned non-zero for "p"! 18876 */ 18877 static int 18878 eval_fname_sid(p) 18879 char_u *p; 18880 { 18881 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I'); 18882 } 18883 18884 /* 18885 * List the head of the function: "name(arg1, arg2)". 18886 */ 18887 static void 18888 list_func_head(fp, indent) 18889 ufunc_T *fp; 18890 int indent; 18891 { 18892 int j; 18893 18894 msg_start(); 18895 if (indent) 18896 MSG_PUTS(" "); 18897 MSG_PUTS("function "); 18898 if (fp->uf_name[0] == K_SPECIAL) 18899 { 18900 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8)); 18901 msg_puts(fp->uf_name + 3); 18902 } 18903 else 18904 msg_puts(fp->uf_name); 18905 msg_putchar('('); 18906 for (j = 0; j < fp->uf_args.ga_len; ++j) 18907 { 18908 if (j) 18909 MSG_PUTS(", "); 18910 msg_puts(FUNCARG(fp, j)); 18911 } 18912 if (fp->uf_varargs) 18913 { 18914 if (j) 18915 MSG_PUTS(", "); 18916 MSG_PUTS("..."); 18917 } 18918 msg_putchar(')'); 18919 msg_clr_eos(); 18920 if (p_verbose > 0) 18921 last_set_msg(fp->uf_script_ID); 18922 } 18923 18924 /* 18925 * Find a function by name, return pointer to it in ufuncs. 18926 * Return NULL for unknown function. 18927 */ 18928 static ufunc_T * 18929 find_func(name) 18930 char_u *name; 18931 { 18932 hashitem_T *hi; 18933 18934 hi = hash_find(&func_hashtab, name); 18935 if (!HASHITEM_EMPTY(hi)) 18936 return HI2UF(hi); 18937 return NULL; 18938 } 18939 18940 #if defined(EXITFREE) || defined(PROTO) 18941 void 18942 free_all_functions() 18943 { 18944 hashitem_T *hi; 18945 18946 /* Need to start all over every time, because func_free() may change the 18947 * hash table. */ 18948 while (func_hashtab.ht_used > 0) 18949 for (hi = func_hashtab.ht_array; ; ++hi) 18950 if (!HASHITEM_EMPTY(hi)) 18951 { 18952 func_free(HI2UF(hi)); 18953 break; 18954 } 18955 } 18956 #endif 18957 18958 /* 18959 * Return TRUE if a function "name" exists. 18960 */ 18961 static int 18962 function_exists(name) 18963 char_u *name; 18964 { 18965 char_u *p = name; 18966 int n = FALSE; 18967 18968 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL); 18969 if (p != NULL) 18970 { 18971 if (builtin_function(p)) 18972 n = (find_internal_func(p) >= 0); 18973 else 18974 n = (find_func(p) != NULL); 18975 vim_free(p); 18976 } 18977 return n; 18978 } 18979 18980 /* 18981 * Return TRUE if "name" looks like a builtin function name: starts with a 18982 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR. 18983 */ 18984 static int 18985 builtin_function(name) 18986 char_u *name; 18987 { 18988 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL 18989 && vim_strchr(name, AUTOLOAD_CHAR) == NULL; 18990 } 18991 18992 #if defined(FEAT_PROFILE) || defined(PROTO) 18993 /* 18994 * Start profiling function "fp". 18995 */ 18996 static void 18997 func_do_profile(fp) 18998 ufunc_T *fp; 18999 { 19000 fp->uf_tm_count = 0; 19001 profile_zero(&fp->uf_tm_self); 19002 profile_zero(&fp->uf_tm_total); 19003 if (fp->uf_tml_count == NULL) 19004 fp->uf_tml_count = (int *)alloc_clear((unsigned) 19005 (sizeof(int) * fp->uf_lines.ga_len)); 19006 if (fp->uf_tml_total == NULL) 19007 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned) 19008 (sizeof(proftime_T) * fp->uf_lines.ga_len)); 19009 if (fp->uf_tml_self == NULL) 19010 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned) 19011 (sizeof(proftime_T) * fp->uf_lines.ga_len)); 19012 fp->uf_tml_idx = -1; 19013 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL 19014 || fp->uf_tml_self == NULL) 19015 return; /* out of memory */ 19016 19017 fp->uf_profiling = TRUE; 19018 } 19019 19020 /* 19021 * Dump the profiling results for all functions in file "fd". 19022 */ 19023 void 19024 func_dump_profile(fd) 19025 FILE *fd; 19026 { 19027 hashitem_T *hi; 19028 int todo; 19029 ufunc_T *fp; 19030 int i; 19031 ufunc_T **sorttab; 19032 int st_len = 0; 19033 19034 todo = func_hashtab.ht_used; 19035 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo)); 19036 19037 for (hi = func_hashtab.ht_array; todo > 0; ++hi) 19038 { 19039 if (!HASHITEM_EMPTY(hi)) 19040 { 19041 --todo; 19042 fp = HI2UF(hi); 19043 if (fp->uf_profiling) 19044 { 19045 if (sorttab != NULL) 19046 sorttab[st_len++] = fp; 19047 19048 if (fp->uf_name[0] == K_SPECIAL) 19049 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3); 19050 else 19051 fprintf(fd, "FUNCTION %s()\n", fp->uf_name); 19052 if (fp->uf_tm_count == 1) 19053 fprintf(fd, "Called 1 time\n"); 19054 else 19055 fprintf(fd, "Called %d times\n", fp->uf_tm_count); 19056 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total)); 19057 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self)); 19058 fprintf(fd, "\n"); 19059 fprintf(fd, "count total (s) self (s)\n"); 19060 19061 for (i = 0; i < fp->uf_lines.ga_len; ++i) 19062 { 19063 if (FUNCLINE(fp, i) == NULL) 19064 continue; 19065 prof_func_line(fd, fp->uf_tml_count[i], 19066 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE); 19067 fprintf(fd, "%s\n", FUNCLINE(fp, i)); 19068 } 19069 fprintf(fd, "\n"); 19070 } 19071 } 19072 } 19073 19074 if (sorttab != NULL && st_len > 0) 19075 { 19076 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *), 19077 prof_total_cmp); 19078 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE); 19079 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *), 19080 prof_self_cmp); 19081 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE); 19082 } 19083 } 19084 19085 static void 19086 prof_sort_list(fd, sorttab, st_len, title, prefer_self) 19087 FILE *fd; 19088 ufunc_T **sorttab; 19089 int st_len; 19090 char *title; 19091 int prefer_self; /* when equal print only self time */ 19092 { 19093 int i; 19094 ufunc_T *fp; 19095 19096 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title); 19097 fprintf(fd, "count total (s) self (s) function\n"); 19098 for (i = 0; i < 20 && i < st_len; ++i) 19099 { 19100 fp = sorttab[i]; 19101 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self, 19102 prefer_self); 19103 if (fp->uf_name[0] == K_SPECIAL) 19104 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3); 19105 else 19106 fprintf(fd, " %s()\n", fp->uf_name); 19107 } 19108 fprintf(fd, "\n"); 19109 } 19110 19111 /* 19112 * Print the count and times for one function or function line. 19113 */ 19114 static void 19115 prof_func_line(fd, count, total, self, prefer_self) 19116 FILE *fd; 19117 int count; 19118 proftime_T *total; 19119 proftime_T *self; 19120 int prefer_self; /* when equal print only self time */ 19121 { 19122 if (count > 0) 19123 { 19124 fprintf(fd, "%5d ", count); 19125 if (prefer_self && profile_equal(total, self)) 19126 fprintf(fd, " "); 19127 else 19128 fprintf(fd, "%s ", profile_msg(total)); 19129 if (!prefer_self && profile_equal(total, self)) 19130 fprintf(fd, " "); 19131 else 19132 fprintf(fd, "%s ", profile_msg(self)); 19133 } 19134 else 19135 fprintf(fd, " "); 19136 } 19137 19138 /* 19139 * Compare function for total time sorting. 19140 */ 19141 static int 19142 #ifdef __BORLANDC__ 19143 _RTLENTRYF 19144 #endif 19145 prof_total_cmp(s1, s2) 19146 const void *s1; 19147 const void *s2; 19148 { 19149 ufunc_T *p1, *p2; 19150 19151 p1 = *(ufunc_T **)s1; 19152 p2 = *(ufunc_T **)s2; 19153 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total); 19154 } 19155 19156 /* 19157 * Compare function for self time sorting. 19158 */ 19159 static int 19160 #ifdef __BORLANDC__ 19161 _RTLENTRYF 19162 #endif 19163 prof_self_cmp(s1, s2) 19164 const void *s1; 19165 const void *s2; 19166 { 19167 ufunc_T *p1, *p2; 19168 19169 p1 = *(ufunc_T **)s1; 19170 p2 = *(ufunc_T **)s2; 19171 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self); 19172 } 19173 19174 #endif 19175 19176 /* The names of packages that once were loaded is remembered. */ 19177 static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; 19178 19179 /* 19180 * If "name" has a package name try autoloading the script for it. 19181 * Return TRUE if a package was loaded. 19182 */ 19183 static int 19184 script_autoload(name, reload) 19185 char_u *name; 19186 int reload; /* load script again when already loaded */ 19187 { 19188 char_u *p; 19189 char_u *scriptname, *tofree; 19190 int ret = FALSE; 19191 int i; 19192 19193 /* If there is no '#' after name[0] there is no package name. */ 19194 p = vim_strchr(name, AUTOLOAD_CHAR); 19195 if (p == NULL || p == name) 19196 return FALSE; 19197 19198 tofree = scriptname = autoload_name(name); 19199 19200 /* Find the name in the list of previously loaded package names. Skip 19201 * "autoload/", it's always the same. */ 19202 for (i = 0; i < ga_loaded.ga_len; ++i) 19203 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0) 19204 break; 19205 if (!reload && i < ga_loaded.ga_len) 19206 ret = FALSE; /* was loaded already */ 19207 else 19208 { 19209 /* Remember the name if it wasn't loaded already. */ 19210 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK) 19211 { 19212 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname; 19213 tofree = NULL; 19214 } 19215 19216 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */ 19217 if (source_runtime(scriptname, FALSE) == OK) 19218 ret = TRUE; 19219 } 19220 19221 vim_free(tofree); 19222 return ret; 19223 } 19224 19225 /* 19226 * Return the autoload script name for a function or variable name. 19227 * Returns NULL when out of memory. 19228 */ 19229 static char_u * 19230 autoload_name(name) 19231 char_u *name; 19232 { 19233 char_u *p; 19234 char_u *scriptname; 19235 19236 /* Get the script file name: replace '#' with '/', append ".vim". */ 19237 scriptname = alloc((unsigned)(STRLEN(name) + 14)); 19238 if (scriptname == NULL) 19239 return FALSE; 19240 STRCPY(scriptname, "autoload/"); 19241 STRCAT(scriptname, name); 19242 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL; 19243 STRCAT(scriptname, ".vim"); 19244 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL) 19245 *p = '/'; 19246 return scriptname; 19247 } 19248 19249 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 19250 19251 /* 19252 * Function given to ExpandGeneric() to obtain the list of user defined 19253 * function names. 19254 */ 19255 char_u * 19256 get_user_func_name(xp, idx) 19257 expand_T *xp; 19258 int idx; 19259 { 19260 static long_u done; 19261 static hashitem_T *hi; 19262 ufunc_T *fp; 19263 19264 if (idx == 0) 19265 { 19266 done = 0; 19267 hi = func_hashtab.ht_array; 19268 } 19269 if (done < func_hashtab.ht_used) 19270 { 19271 if (done++ > 0) 19272 ++hi; 19273 while (HASHITEM_EMPTY(hi)) 19274 ++hi; 19275 fp = HI2UF(hi); 19276 19277 if (STRLEN(fp->uf_name) + 4 >= IOSIZE) 19278 return fp->uf_name; /* prevents overflow */ 19279 19280 cat_func_name(IObuff, fp); 19281 if (xp->xp_context != EXPAND_USER_FUNC) 19282 { 19283 STRCAT(IObuff, "("); 19284 if (!fp->uf_varargs && fp->uf_args.ga_len == 0) 19285 STRCAT(IObuff, ")"); 19286 } 19287 return IObuff; 19288 } 19289 return NULL; 19290 } 19291 19292 #endif /* FEAT_CMDL_COMPL */ 19293 19294 /* 19295 * Copy the function name of "fp" to buffer "buf". 19296 * "buf" must be able to hold the function name plus three bytes. 19297 * Takes care of script-local function names. 19298 */ 19299 static void 19300 cat_func_name(buf, fp) 19301 char_u *buf; 19302 ufunc_T *fp; 19303 { 19304 if (fp->uf_name[0] == K_SPECIAL) 19305 { 19306 STRCPY(buf, "<SNR>"); 19307 STRCAT(buf, fp->uf_name + 3); 19308 } 19309 else 19310 STRCPY(buf, fp->uf_name); 19311 } 19312 19313 /* 19314 * ":delfunction {name}" 19315 */ 19316 void 19317 ex_delfunction(eap) 19318 exarg_T *eap; 19319 { 19320 ufunc_T *fp = NULL; 19321 char_u *p; 19322 char_u *name; 19323 funcdict_T fudi; 19324 19325 p = eap->arg; 19326 name = trans_function_name(&p, eap->skip, 0, &fudi); 19327 vim_free(fudi.fd_newkey); 19328 if (name == NULL) 19329 { 19330 if (fudi.fd_dict != NULL && !eap->skip) 19331 EMSG(_(e_funcref)); 19332 return; 19333 } 19334 if (!ends_excmd(*skipwhite(p))) 19335 { 19336 vim_free(name); 19337 EMSG(_(e_trailing)); 19338 return; 19339 } 19340 eap->nextcmd = check_nextcmd(p); 19341 if (eap->nextcmd != NULL) 19342 *p = NUL; 19343 19344 if (!eap->skip) 19345 fp = find_func(name); 19346 vim_free(name); 19347 19348 if (!eap->skip) 19349 { 19350 if (fp == NULL) 19351 { 19352 EMSG2(_(e_nofunc), eap->arg); 19353 return; 19354 } 19355 if (fp->uf_calls > 0) 19356 { 19357 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg); 19358 return; 19359 } 19360 19361 if (fudi.fd_dict != NULL) 19362 { 19363 /* Delete the dict item that refers to the function, it will 19364 * invoke func_unref() and possibly delete the function. */ 19365 dictitem_remove(fudi.fd_dict, fudi.fd_di); 19366 } 19367 else 19368 func_free(fp); 19369 } 19370 } 19371 19372 /* 19373 * Free a function and remove it from the list of functions. 19374 */ 19375 static void 19376 func_free(fp) 19377 ufunc_T *fp; 19378 { 19379 hashitem_T *hi; 19380 19381 /* clear this function */ 19382 ga_clear_strings(&(fp->uf_args)); 19383 ga_clear_strings(&(fp->uf_lines)); 19384 #ifdef FEAT_PROFILE 19385 vim_free(fp->uf_tml_count); 19386 vim_free(fp->uf_tml_total); 19387 vim_free(fp->uf_tml_self); 19388 #endif 19389 19390 /* remove the function from the function hashtable */ 19391 hi = hash_find(&func_hashtab, UF2HIKEY(fp)); 19392 if (HASHITEM_EMPTY(hi)) 19393 EMSG2(_(e_intern2), "func_free()"); 19394 else 19395 hash_remove(&func_hashtab, hi); 19396 19397 vim_free(fp); 19398 } 19399 19400 /* 19401 * Unreference a Function: decrement the reference count and free it when it 19402 * becomes zero. Only for numbered functions. 19403 */ 19404 static void 19405 func_unref(name) 19406 char_u *name; 19407 { 19408 ufunc_T *fp; 19409 19410 if (name != NULL && isdigit(*name)) 19411 { 19412 fp = find_func(name); 19413 if (fp == NULL) 19414 EMSG2(_(e_intern2), "func_unref()"); 19415 else if (--fp->uf_refcount <= 0) 19416 { 19417 /* Only delete it when it's not being used. Otherwise it's done 19418 * when "uf_calls" becomes zero. */ 19419 if (fp->uf_calls == 0) 19420 func_free(fp); 19421 } 19422 } 19423 } 19424 19425 /* 19426 * Count a reference to a Function. 19427 */ 19428 static void 19429 func_ref(name) 19430 char_u *name; 19431 { 19432 ufunc_T *fp; 19433 19434 if (name != NULL && isdigit(*name)) 19435 { 19436 fp = find_func(name); 19437 if (fp == NULL) 19438 EMSG2(_(e_intern2), "func_ref()"); 19439 else 19440 ++fp->uf_refcount; 19441 } 19442 } 19443 19444 /* 19445 * Call a user function. 19446 */ 19447 static void 19448 call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict) 19449 ufunc_T *fp; /* pointer to function */ 19450 int argcount; /* nr of args */ 19451 typval_T *argvars; /* arguments */ 19452 typval_T *rettv; /* return value */ 19453 linenr_T firstline; /* first line of range */ 19454 linenr_T lastline; /* last line of range */ 19455 dict_T *selfdict; /* Dictionary for "self" */ 19456 { 19457 char_u *save_sourcing_name; 19458 linenr_T save_sourcing_lnum; 19459 scid_T save_current_SID; 19460 funccall_T fc; 19461 int save_did_emsg; 19462 static int depth = 0; 19463 dictitem_T *v; 19464 int fixvar_idx = 0; /* index in fixvar[] */ 19465 int i; 19466 int ai; 19467 char_u numbuf[NUMBUFLEN]; 19468 char_u *name; 19469 #ifdef FEAT_PROFILE 19470 proftime_T wait_start; 19471 #endif 19472 19473 /* If depth of calling is getting too high, don't execute the function */ 19474 if (depth >= p_mfd) 19475 { 19476 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'")); 19477 rettv->v_type = VAR_NUMBER; 19478 rettv->vval.v_number = -1; 19479 return; 19480 } 19481 ++depth; 19482 19483 line_breakcheck(); /* check for CTRL-C hit */ 19484 19485 fc.caller = current_funccal; 19486 current_funccal = &fc; 19487 fc.func = fp; 19488 fc.rettv = rettv; 19489 rettv->vval.v_number = 0; 19490 fc.linenr = 0; 19491 fc.returned = FALSE; 19492 fc.level = ex_nesting_level; 19493 /* Check if this function has a breakpoint. */ 19494 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0); 19495 fc.dbg_tick = debug_tick; 19496 19497 /* 19498 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables 19499 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free 19500 * each argument variable and saves a lot of time. 19501 */ 19502 /* 19503 * Init l: variables. 19504 */ 19505 init_var_dict(&fc.l_vars, &fc.l_vars_var); 19506 if (selfdict != NULL) 19507 { 19508 /* Set l:self to "selfdict". Use "name" to avoid a warning from 19509 * some compiler that checks the destination size. */ 19510 v = &fc.fixvar[fixvar_idx++].var; 19511 name = v->di_key; 19512 STRCPY(name, "self"); 19513 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX; 19514 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v)); 19515 v->di_tv.v_type = VAR_DICT; 19516 v->di_tv.v_lock = 0; 19517 v->di_tv.vval.v_dict = selfdict; 19518 ++selfdict->dv_refcount; 19519 } 19520 19521 /* 19522 * Init a: variables. 19523 * Set a:0 to "argcount". 19524 * Set a:000 to a list with room for the "..." arguments. 19525 */ 19526 init_var_dict(&fc.l_avars, &fc.l_avars_var); 19527 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0", 19528 (varnumber_T)(argcount - fp->uf_args.ga_len)); 19529 v = &fc.fixvar[fixvar_idx++].var; 19530 STRCPY(v->di_key, "000"); 19531 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 19532 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v)); 19533 v->di_tv.v_type = VAR_LIST; 19534 v->di_tv.v_lock = VAR_FIXED; 19535 v->di_tv.vval.v_list = &fc.l_varlist; 19536 vim_memset(&fc.l_varlist, 0, sizeof(list_T)); 19537 fc.l_varlist.lv_refcount = 99999; 19538 19539 /* 19540 * Set a:firstline to "firstline" and a:lastline to "lastline". 19541 * Set a:name to named arguments. 19542 * Set a:N to the "..." arguments. 19543 */ 19544 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline", 19545 (varnumber_T)firstline); 19546 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline", 19547 (varnumber_T)lastline); 19548 for (i = 0; i < argcount; ++i) 19549 { 19550 ai = i - fp->uf_args.ga_len; 19551 if (ai < 0) 19552 /* named argument a:name */ 19553 name = FUNCARG(fp, i); 19554 else 19555 { 19556 /* "..." argument a:1, a:2, etc. */ 19557 sprintf((char *)numbuf, "%d", ai + 1); 19558 name = numbuf; 19559 } 19560 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN) 19561 { 19562 v = &fc.fixvar[fixvar_idx++].var; 19563 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 19564 } 19565 else 19566 { 19567 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) 19568 + STRLEN(name))); 19569 if (v == NULL) 19570 break; 19571 v->di_flags = DI_FLAGS_RO; 19572 } 19573 STRCPY(v->di_key, name); 19574 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v)); 19575 19576 /* Note: the values are copied directly to avoid alloc/free. 19577 * "argvars" must have VAR_FIXED for v_lock. */ 19578 v->di_tv = argvars[i]; 19579 v->di_tv.v_lock = VAR_FIXED; 19580 19581 if (ai >= 0 && ai < MAX_FUNC_ARGS) 19582 { 19583 list_append(&fc.l_varlist, &fc.l_listitems[ai]); 19584 fc.l_listitems[ai].li_tv = argvars[i]; 19585 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED; 19586 } 19587 } 19588 19589 /* Don't redraw while executing the function. */ 19590 ++RedrawingDisabled; 19591 save_sourcing_name = sourcing_name; 19592 save_sourcing_lnum = sourcing_lnum; 19593 sourcing_lnum = 1; 19594 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0 19595 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13)); 19596 if (sourcing_name != NULL) 19597 { 19598 if (save_sourcing_name != NULL 19599 && STRNCMP(save_sourcing_name, "function ", 9) == 0) 19600 sprintf((char *)sourcing_name, "%s..", save_sourcing_name); 19601 else 19602 STRCPY(sourcing_name, "function "); 19603 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp); 19604 19605 if (p_verbose >= 12) 19606 { 19607 ++no_wait_return; 19608 verbose_enter_scroll(); 19609 19610 smsg((char_u *)_("calling %s"), sourcing_name); 19611 if (p_verbose >= 14) 19612 { 19613 char_u buf[MSG_BUF_LEN]; 19614 char_u numbuf[NUMBUFLEN]; 19615 char_u *tofree; 19616 19617 msg_puts((char_u *)"("); 19618 for (i = 0; i < argcount; ++i) 19619 { 19620 if (i > 0) 19621 msg_puts((char_u *)", "); 19622 if (argvars[i].v_type == VAR_NUMBER) 19623 msg_outnum((long)argvars[i].vval.v_number); 19624 else 19625 { 19626 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0), 19627 buf, MSG_BUF_CLEN); 19628 msg_puts(buf); 19629 vim_free(tofree); 19630 } 19631 } 19632 msg_puts((char_u *)")"); 19633 } 19634 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 19635 19636 verbose_leave_scroll(); 19637 --no_wait_return; 19638 } 19639 } 19640 #ifdef FEAT_PROFILE 19641 if (do_profiling == PROF_YES) 19642 { 19643 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL)) 19644 func_do_profile(fp); 19645 if (fp->uf_profiling 19646 || (fc.caller != NULL && &fc.caller->func->uf_profiling)) 19647 { 19648 ++fp->uf_tm_count; 19649 profile_start(&fp->uf_tm_start); 19650 profile_zero(&fp->uf_tm_children); 19651 } 19652 script_prof_save(&wait_start); 19653 } 19654 #endif 19655 19656 save_current_SID = current_SID; 19657 current_SID = fp->uf_script_ID; 19658 save_did_emsg = did_emsg; 19659 did_emsg = FALSE; 19660 19661 /* call do_cmdline() to execute the lines */ 19662 do_cmdline(NULL, get_func_line, (void *)&fc, 19663 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); 19664 19665 --RedrawingDisabled; 19666 19667 /* when the function was aborted because of an error, return -1 */ 19668 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN) 19669 { 19670 clear_tv(rettv); 19671 rettv->v_type = VAR_NUMBER; 19672 rettv->vval.v_number = -1; 19673 } 19674 19675 #ifdef FEAT_PROFILE 19676 if (do_profiling == PROF_YES && (fp->uf_profiling 19677 || (fc.caller != NULL && &fc.caller->func->uf_profiling))) 19678 { 19679 profile_end(&fp->uf_tm_start); 19680 profile_sub_wait(&wait_start, &fp->uf_tm_start); 19681 profile_add(&fp->uf_tm_total, &fp->uf_tm_start); 19682 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children); 19683 if (fc.caller != NULL && &fc.caller->func->uf_profiling) 19684 { 19685 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start); 19686 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start); 19687 } 19688 } 19689 #endif 19690 19691 /* when being verbose, mention the return value */ 19692 if (p_verbose >= 12) 19693 { 19694 ++no_wait_return; 19695 verbose_enter_scroll(); 19696 19697 if (aborting()) 19698 smsg((char_u *)_("%s aborted"), sourcing_name); 19699 else if (fc.rettv->v_type == VAR_NUMBER) 19700 smsg((char_u *)_("%s returning #%ld"), sourcing_name, 19701 (long)fc.rettv->vval.v_number); 19702 else 19703 { 19704 char_u buf[MSG_BUF_LEN]; 19705 char_u numbuf[NUMBUFLEN]; 19706 char_u *tofree; 19707 19708 /* The value may be very long. Skip the middle part, so that we 19709 * have some idea how it starts and ends. smsg() would always 19710 * truncate it at the end. */ 19711 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0), 19712 buf, MSG_BUF_CLEN); 19713 smsg((char_u *)_("%s returning %s"), sourcing_name, buf); 19714 vim_free(tofree); 19715 } 19716 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 19717 19718 verbose_leave_scroll(); 19719 --no_wait_return; 19720 } 19721 19722 vim_free(sourcing_name); 19723 sourcing_name = save_sourcing_name; 19724 sourcing_lnum = save_sourcing_lnum; 19725 current_SID = save_current_SID; 19726 #ifdef FEAT_PROFILE 19727 if (do_profiling == PROF_YES) 19728 script_prof_restore(&wait_start); 19729 #endif 19730 19731 if (p_verbose >= 12 && sourcing_name != NULL) 19732 { 19733 ++no_wait_return; 19734 verbose_enter_scroll(); 19735 19736 smsg((char_u *)_("continuing in %s"), sourcing_name); 19737 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 19738 19739 verbose_leave_scroll(); 19740 --no_wait_return; 19741 } 19742 19743 did_emsg |= save_did_emsg; 19744 current_funccal = fc.caller; 19745 19746 /* The a: variables typevals were not alloced, only free the allocated 19747 * variables. */ 19748 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE); 19749 19750 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */ 19751 --depth; 19752 } 19753 19754 /* 19755 * Add a number variable "name" to dict "dp" with value "nr". 19756 */ 19757 static void 19758 add_nr_var(dp, v, name, nr) 19759 dict_T *dp; 19760 dictitem_T *v; 19761 char *name; 19762 varnumber_T nr; 19763 { 19764 STRCPY(v->di_key, name); 19765 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 19766 hash_add(&dp->dv_hashtab, DI2HIKEY(v)); 19767 v->di_tv.v_type = VAR_NUMBER; 19768 v->di_tv.v_lock = VAR_FIXED; 19769 v->di_tv.vval.v_number = nr; 19770 } 19771 19772 /* 19773 * ":return [expr]" 19774 */ 19775 void 19776 ex_return(eap) 19777 exarg_T *eap; 19778 { 19779 char_u *arg = eap->arg; 19780 typval_T rettv; 19781 int returning = FALSE; 19782 19783 if (current_funccal == NULL) 19784 { 19785 EMSG(_("E133: :return not inside a function")); 19786 return; 19787 } 19788 19789 if (eap->skip) 19790 ++emsg_skip; 19791 19792 eap->nextcmd = NULL; 19793 if ((*arg != NUL && *arg != '|' && *arg != '\n') 19794 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL) 19795 { 19796 if (!eap->skip) 19797 returning = do_return(eap, FALSE, TRUE, &rettv); 19798 else 19799 clear_tv(&rettv); 19800 } 19801 /* It's safer to return also on error. */ 19802 else if (!eap->skip) 19803 { 19804 /* 19805 * Return unless the expression evaluation has been cancelled due to an 19806 * aborting error, an interrupt, or an exception. 19807 */ 19808 if (!aborting()) 19809 returning = do_return(eap, FALSE, TRUE, NULL); 19810 } 19811 19812 /* When skipping or the return gets pending, advance to the next command 19813 * in this line (!returning). Otherwise, ignore the rest of the line. 19814 * Following lines will be ignored by get_func_line(). */ 19815 if (returning) 19816 eap->nextcmd = NULL; 19817 else if (eap->nextcmd == NULL) /* no argument */ 19818 eap->nextcmd = check_nextcmd(arg); 19819 19820 if (eap->skip) 19821 --emsg_skip; 19822 } 19823 19824 /* 19825 * Return from a function. Possibly makes the return pending. Also called 19826 * for a pending return at the ":endtry" or after returning from an extra 19827 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set 19828 * when called due to a ":return" command. "rettv" may point to a typval_T 19829 * with the return rettv. Returns TRUE when the return can be carried out, 19830 * FALSE when the return gets pending. 19831 */ 19832 int 19833 do_return(eap, reanimate, is_cmd, rettv) 19834 exarg_T *eap; 19835 int reanimate; 19836 int is_cmd; 19837 void *rettv; 19838 { 19839 int idx; 19840 struct condstack *cstack = eap->cstack; 19841 19842 if (reanimate) 19843 /* Undo the return. */ 19844 current_funccal->returned = FALSE; 19845 19846 /* 19847 * Cleanup (and inactivate) conditionals, but stop when a try conditional 19848 * not in its finally clause (which then is to be executed next) is found. 19849 * In this case, make the ":return" pending for execution at the ":endtry". 19850 * Otherwise, return normally. 19851 */ 19852 idx = cleanup_conditionals(eap->cstack, 0, TRUE); 19853 if (idx >= 0) 19854 { 19855 cstack->cs_pending[idx] = CSTP_RETURN; 19856 19857 if (!is_cmd && !reanimate) 19858 /* A pending return again gets pending. "rettv" points to an 19859 * allocated variable with the rettv of the original ":return"'s 19860 * argument if present or is NULL else. */ 19861 cstack->cs_rettv[idx] = rettv; 19862 else 19863 { 19864 /* When undoing a return in order to make it pending, get the stored 19865 * return rettv. */ 19866 if (reanimate) 19867 rettv = current_funccal->rettv; 19868 19869 if (rettv != NULL) 19870 { 19871 /* Store the value of the pending return. */ 19872 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL) 19873 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv; 19874 else 19875 EMSG(_(e_outofmem)); 19876 } 19877 else 19878 cstack->cs_rettv[idx] = NULL; 19879 19880 if (reanimate) 19881 { 19882 /* The pending return value could be overwritten by a ":return" 19883 * without argument in a finally clause; reset the default 19884 * return value. */ 19885 current_funccal->rettv->v_type = VAR_NUMBER; 19886 current_funccal->rettv->vval.v_number = 0; 19887 } 19888 } 19889 report_make_pending(CSTP_RETURN, rettv); 19890 } 19891 else 19892 { 19893 current_funccal->returned = TRUE; 19894 19895 /* If the return is carried out now, store the return value. For 19896 * a return immediately after reanimation, the value is already 19897 * there. */ 19898 if (!reanimate && rettv != NULL) 19899 { 19900 clear_tv(current_funccal->rettv); 19901 *current_funccal->rettv = *(typval_T *)rettv; 19902 if (!is_cmd) 19903 vim_free(rettv); 19904 } 19905 } 19906 19907 return idx < 0; 19908 } 19909 19910 /* 19911 * Free the variable with a pending return value. 19912 */ 19913 void 19914 discard_pending_return(rettv) 19915 void *rettv; 19916 { 19917 free_tv((typval_T *)rettv); 19918 } 19919 19920 /* 19921 * Generate a return command for producing the value of "rettv". The result 19922 * is an allocated string. Used by report_pending() for verbose messages. 19923 */ 19924 char_u * 19925 get_return_cmd(rettv) 19926 void *rettv; 19927 { 19928 char_u *s = NULL; 19929 char_u *tofree = NULL; 19930 char_u numbuf[NUMBUFLEN]; 19931 19932 if (rettv != NULL) 19933 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0); 19934 if (s == NULL) 19935 s = (char_u *)""; 19936 19937 STRCPY(IObuff, ":return "); 19938 STRNCPY(IObuff + 8, s, IOSIZE - 8); 19939 if (STRLEN(s) + 8 >= IOSIZE) 19940 STRCPY(IObuff + IOSIZE - 4, "..."); 19941 vim_free(tofree); 19942 return vim_strsave(IObuff); 19943 } 19944 19945 /* 19946 * Get next function line. 19947 * Called by do_cmdline() to get the next line. 19948 * Returns allocated string, or NULL for end of function. 19949 */ 19950 /* ARGSUSED */ 19951 char_u * 19952 get_func_line(c, cookie, indent) 19953 int c; /* not used */ 19954 void *cookie; 19955 int indent; /* not used */ 19956 { 19957 funccall_T *fcp = (funccall_T *)cookie; 19958 ufunc_T *fp = fcp->func; 19959 char_u *retval; 19960 garray_T *gap; /* growarray with function lines */ 19961 19962 /* If breakpoints have been added/deleted need to check for it. */ 19963 if (fcp->dbg_tick != debug_tick) 19964 { 19965 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, 19966 sourcing_lnum); 19967 fcp->dbg_tick = debug_tick; 19968 } 19969 #ifdef FEAT_PROFILE 19970 if (do_profiling == PROF_YES) 19971 func_line_end(cookie); 19972 #endif 19973 19974 gap = &fp->uf_lines; 19975 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) 19976 || fcp->returned) 19977 retval = NULL; 19978 else 19979 { 19980 /* Skip NULL lines (continuation lines). */ 19981 while (fcp->linenr < gap->ga_len 19982 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL) 19983 ++fcp->linenr; 19984 if (fcp->linenr >= gap->ga_len) 19985 retval = NULL; 19986 else 19987 { 19988 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]); 19989 sourcing_lnum = fcp->linenr; 19990 #ifdef FEAT_PROFILE 19991 if (do_profiling == PROF_YES) 19992 func_line_start(cookie); 19993 #endif 19994 } 19995 } 19996 19997 /* Did we encounter a breakpoint? */ 19998 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum) 19999 { 20000 dbg_breakpoint(fp->uf_name, sourcing_lnum); 20001 /* Find next breakpoint. */ 20002 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, 20003 sourcing_lnum); 20004 fcp->dbg_tick = debug_tick; 20005 } 20006 20007 return retval; 20008 } 20009 20010 #if defined(FEAT_PROFILE) || defined(PROTO) 20011 /* 20012 * Called when starting to read a function line. 20013 * "sourcing_lnum" must be correct! 20014 * When skipping lines it may not actually be executed, but we won't find out 20015 * until later and we need to store the time now. 20016 */ 20017 void 20018 func_line_start(cookie) 20019 void *cookie; 20020 { 20021 funccall_T *fcp = (funccall_T *)cookie; 20022 ufunc_T *fp = fcp->func; 20023 20024 if (fp->uf_profiling && sourcing_lnum >= 1 20025 && sourcing_lnum <= fp->uf_lines.ga_len) 20026 { 20027 fp->uf_tml_idx = sourcing_lnum - 1; 20028 /* Skip continuation lines. */ 20029 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL) 20030 --fp->uf_tml_idx; 20031 fp->uf_tml_execed = FALSE; 20032 profile_start(&fp->uf_tml_start); 20033 profile_zero(&fp->uf_tml_children); 20034 profile_get_wait(&fp->uf_tml_wait); 20035 } 20036 } 20037 20038 /* 20039 * Called when actually executing a function line. 20040 */ 20041 void 20042 func_line_exec(cookie) 20043 void *cookie; 20044 { 20045 funccall_T *fcp = (funccall_T *)cookie; 20046 ufunc_T *fp = fcp->func; 20047 20048 if (fp->uf_profiling && fp->uf_tml_idx >= 0) 20049 fp->uf_tml_execed = TRUE; 20050 } 20051 20052 /* 20053 * Called when done with a function line. 20054 */ 20055 void 20056 func_line_end(cookie) 20057 void *cookie; 20058 { 20059 funccall_T *fcp = (funccall_T *)cookie; 20060 ufunc_T *fp = fcp->func; 20061 20062 if (fp->uf_profiling && fp->uf_tml_idx >= 0) 20063 { 20064 if (fp->uf_tml_execed) 20065 { 20066 ++fp->uf_tml_count[fp->uf_tml_idx]; 20067 profile_end(&fp->uf_tml_start); 20068 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start); 20069 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start); 20070 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start, 20071 &fp->uf_tml_children); 20072 } 20073 fp->uf_tml_idx = -1; 20074 } 20075 } 20076 #endif 20077 20078 /* 20079 * Return TRUE if the currently active function should be ended, because a 20080 * return was encountered or an error occured. Used inside a ":while". 20081 */ 20082 int 20083 func_has_ended(cookie) 20084 void *cookie; 20085 { 20086 funccall_T *fcp = (funccall_T *)cookie; 20087 20088 /* Ignore the "abort" flag if the abortion behavior has been changed due to 20089 * an error inside a try conditional. */ 20090 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) 20091 || fcp->returned); 20092 } 20093 20094 /* 20095 * return TRUE if cookie indicates a function which "abort"s on errors. 20096 */ 20097 int 20098 func_has_abort(cookie) 20099 void *cookie; 20100 { 20101 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT; 20102 } 20103 20104 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION) 20105 typedef enum 20106 { 20107 VAR_FLAVOUR_DEFAULT, 20108 VAR_FLAVOUR_SESSION, 20109 VAR_FLAVOUR_VIMINFO 20110 } var_flavour_T; 20111 20112 static var_flavour_T var_flavour __ARGS((char_u *varname)); 20113 20114 static var_flavour_T 20115 var_flavour(varname) 20116 char_u *varname; 20117 { 20118 char_u *p = varname; 20119 20120 if (ASCII_ISUPPER(*p)) 20121 { 20122 while (*(++p)) 20123 if (ASCII_ISLOWER(*p)) 20124 return VAR_FLAVOUR_SESSION; 20125 return VAR_FLAVOUR_VIMINFO; 20126 } 20127 else 20128 return VAR_FLAVOUR_DEFAULT; 20129 } 20130 #endif 20131 20132 #if defined(FEAT_VIMINFO) || defined(PROTO) 20133 /* 20134 * Restore global vars that start with a capital from the viminfo file 20135 */ 20136 int 20137 read_viminfo_varlist(virp, writing) 20138 vir_T *virp; 20139 int writing; 20140 { 20141 char_u *tab; 20142 int is_string = FALSE; 20143 typval_T tv; 20144 20145 if (!writing && (find_viminfo_parameter('!') != NULL)) 20146 { 20147 tab = vim_strchr(virp->vir_line + 1, '\t'); 20148 if (tab != NULL) 20149 { 20150 *tab++ = '\0'; /* isolate the variable name */ 20151 if (*tab == 'S') /* string var */ 20152 is_string = TRUE; 20153 20154 tab = vim_strchr(tab, '\t'); 20155 if (tab != NULL) 20156 { 20157 if (is_string) 20158 { 20159 tv.v_type = VAR_STRING; 20160 tv.vval.v_string = viminfo_readstring(virp, 20161 (int)(tab - virp->vir_line + 1), TRUE); 20162 } 20163 else 20164 { 20165 tv.v_type = VAR_NUMBER; 20166 tv.vval.v_number = atol((char *)tab + 1); 20167 } 20168 set_var(virp->vir_line + 1, &tv, FALSE); 20169 if (is_string) 20170 vim_free(tv.vval.v_string); 20171 } 20172 } 20173 } 20174 20175 return viminfo_readline(virp); 20176 } 20177 20178 /* 20179 * Write global vars that start with a capital to the viminfo file 20180 */ 20181 void 20182 write_viminfo_varlist(fp) 20183 FILE *fp; 20184 { 20185 hashitem_T *hi; 20186 dictitem_T *this_var; 20187 int todo; 20188 char *s; 20189 char_u *p; 20190 char_u *tofree; 20191 char_u numbuf[NUMBUFLEN]; 20192 20193 if (find_viminfo_parameter('!') == NULL) 20194 return; 20195 20196 fprintf(fp, _("\n# global variables:\n")); 20197 20198 todo = globvarht.ht_used; 20199 for (hi = globvarht.ht_array; todo > 0; ++hi) 20200 { 20201 if (!HASHITEM_EMPTY(hi)) 20202 { 20203 --todo; 20204 this_var = HI2DI(hi); 20205 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO) 20206 { 20207 switch (this_var->di_tv.v_type) 20208 { 20209 case VAR_STRING: s = "STR"; break; 20210 case VAR_NUMBER: s = "NUM"; break; 20211 default: continue; 20212 } 20213 fprintf(fp, "!%s\t%s\t", this_var->di_key, s); 20214 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0); 20215 if (p != NULL) 20216 viminfo_writestring(fp, p); 20217 vim_free(tofree); 20218 } 20219 } 20220 } 20221 } 20222 #endif 20223 20224 #if defined(FEAT_SESSION) || defined(PROTO) 20225 int 20226 store_session_globals(fd) 20227 FILE *fd; 20228 { 20229 hashitem_T *hi; 20230 dictitem_T *this_var; 20231 int todo; 20232 char_u *p, *t; 20233 20234 todo = globvarht.ht_used; 20235 for (hi = globvarht.ht_array; todo > 0; ++hi) 20236 { 20237 if (!HASHITEM_EMPTY(hi)) 20238 { 20239 --todo; 20240 this_var = HI2DI(hi); 20241 if ((this_var->di_tv.v_type == VAR_NUMBER 20242 || this_var->di_tv.v_type == VAR_STRING) 20243 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION) 20244 { 20245 /* Escape special characters with a backslash. Turn a LF and 20246 * CR into \n and \r. */ 20247 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv), 20248 (char_u *)"\\\"\n\r"); 20249 if (p == NULL) /* out of memory */ 20250 break; 20251 for (t = p; *t != NUL; ++t) 20252 if (*t == '\n') 20253 *t = 'n'; 20254 else if (*t == '\r') 20255 *t = 'r'; 20256 if ((fprintf(fd, "let %s = %c%s%c", 20257 this_var->di_key, 20258 (this_var->di_tv.v_type == VAR_STRING) ? '"' 20259 : ' ', 20260 p, 20261 (this_var->di_tv.v_type == VAR_STRING) ? '"' 20262 : ' ') < 0) 20263 || put_eol(fd) == FAIL) 20264 { 20265 vim_free(p); 20266 return FAIL; 20267 } 20268 vim_free(p); 20269 } 20270 } 20271 } 20272 return OK; 20273 } 20274 #endif 20275 20276 /* 20277 * Display script name where an item was last set. 20278 * Should only be invoked when 'verbose' is non-zero. 20279 */ 20280 void 20281 last_set_msg(scriptID) 20282 scid_T scriptID; 20283 { 20284 char_u *p; 20285 20286 if (scriptID != 0) 20287 { 20288 p = home_replace_save(NULL, get_scriptname(scriptID)); 20289 if (p != NULL) 20290 { 20291 verbose_enter(); 20292 MSG_PUTS(_("\n\tLast set from ")); 20293 MSG_PUTS(p); 20294 vim_free(p); 20295 verbose_leave(); 20296 } 20297 } 20298 } 20299 20300 #endif /* FEAT_EVAL */ 20301 20302 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) 20303 20304 20305 #ifdef WIN3264 20306 /* 20307 * Functions for ":8" filename modifier: get 8.3 version of a filename. 20308 */ 20309 static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen)); 20310 static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen)); 20311 static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen)); 20312 20313 /* 20314 * Get the short pathname of a file. 20315 * Returns 1 on success. *fnamelen is 0 for nonexistant path. 20316 */ 20317 static int 20318 get_short_pathname(fnamep, bufp, fnamelen) 20319 char_u **fnamep; 20320 char_u **bufp; 20321 int *fnamelen; 20322 { 20323 int l,len; 20324 char_u *newbuf; 20325 20326 len = *fnamelen; 20327 20328 l = GetShortPathName(*fnamep, *fnamep, len); 20329 if (l > len - 1) 20330 { 20331 /* If that doesn't work (not enough space), then save the string 20332 * and try again with a new buffer big enough 20333 */ 20334 newbuf = vim_strnsave(*fnamep, l); 20335 if (newbuf == NULL) 20336 return 0; 20337 20338 vim_free(*bufp); 20339 *fnamep = *bufp = newbuf; 20340 20341 l = GetShortPathName(*fnamep,*fnamep,l+1); 20342 20343 /* Really should always succeed, as the buffer is big enough */ 20344 } 20345 20346 *fnamelen = l; 20347 return 1; 20348 } 20349 20350 /* 20351 * Create a short path name. Returns the length of the buffer it needs. 20352 * Doesn't copy over the end of the buffer passed in. 20353 */ 20354 static int 20355 shortpath_for_invalid_fname(fname, bufp, fnamelen) 20356 char_u **fname; 20357 char_u **bufp; 20358 int *fnamelen; 20359 { 20360 char_u *s, *p, *pbuf2, *pbuf3; 20361 char_u ch; 20362 int len, len2, plen, slen; 20363 20364 /* Make a copy */ 20365 len2 = *fnamelen; 20366 pbuf2 = vim_strnsave(*fname, len2); 20367 pbuf3 = NULL; 20368 20369 s = pbuf2 + len2 - 1; /* Find the end */ 20370 slen = 1; 20371 plen = len2; 20372 20373 if (after_pathsep(pbuf2, s + 1)) 20374 { 20375 --s; 20376 ++slen; 20377 --plen; 20378 } 20379 20380 do 20381 { 20382 /* Go back one path-seperator */ 20383 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1)) 20384 { 20385 --s; 20386 ++slen; 20387 --plen; 20388 } 20389 if (s <= pbuf2) 20390 break; 20391 20392 /* Remeber the character that is about to be blatted */ 20393 ch = *s; 20394 *s = 0; /* get_short_pathname requires a null-terminated string */ 20395 20396 /* Try it in situ */ 20397 p = pbuf2; 20398 if (!get_short_pathname(&p, &pbuf3, &plen)) 20399 { 20400 vim_free(pbuf2); 20401 return -1; 20402 } 20403 *s = ch; /* Preserve the string */ 20404 } while (plen == 0); 20405 20406 if (plen > 0) 20407 { 20408 /* Remeber the length of the new string. */ 20409 *fnamelen = len = plen + slen; 20410 vim_free(*bufp); 20411 if (len > len2) 20412 { 20413 /* If there's not enough space in the currently allocated string, 20414 * then copy it to a buffer big enough. 20415 */ 20416 *fname= *bufp = vim_strnsave(p, len); 20417 if (*fname == NULL) 20418 return -1; 20419 } 20420 else 20421 { 20422 /* Transfer pbuf2 to being the main buffer (it's big enough) */ 20423 *fname = *bufp = pbuf2; 20424 if (p != pbuf2) 20425 strncpy(*fname, p, plen); 20426 pbuf2 = NULL; 20427 } 20428 /* Concat the next bit */ 20429 strncpy(*fname + plen, s, slen); 20430 (*fname)[len] = '\0'; 20431 } 20432 vim_free(pbuf3); 20433 vim_free(pbuf2); 20434 return 0; 20435 } 20436 20437 /* 20438 * Get a pathname for a partial path. 20439 */ 20440 static int 20441 shortpath_for_partial(fnamep, bufp, fnamelen) 20442 char_u **fnamep; 20443 char_u **bufp; 20444 int *fnamelen; 20445 { 20446 int sepcount, len, tflen; 20447 char_u *p; 20448 char_u *pbuf, *tfname; 20449 int hasTilde; 20450 20451 /* Count up the path seperators from the RHS.. so we know which part 20452 * of the path to return. 20453 */ 20454 sepcount = 0; 20455 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p)) 20456 if (vim_ispathsep(*p)) 20457 ++sepcount; 20458 20459 /* Need full path first (use expand_env() to remove a "~/") */ 20460 hasTilde = (**fnamep == '~'); 20461 if (hasTilde) 20462 pbuf = tfname = expand_env_save(*fnamep); 20463 else 20464 pbuf = tfname = FullName_save(*fnamep, FALSE); 20465 20466 len = tflen = STRLEN(tfname); 20467 20468 if (!get_short_pathname(&tfname, &pbuf, &len)) 20469 return -1; 20470 20471 if (len == 0) 20472 { 20473 /* Don't have a valid filename, so shorten the rest of the 20474 * path if we can. This CAN give us invalid 8.3 filenames, but 20475 * there's not a lot of point in guessing what it might be. 20476 */ 20477 len = tflen; 20478 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1) 20479 return -1; 20480 } 20481 20482 /* Count the paths backward to find the beginning of the desired string. */ 20483 for (p = tfname + len - 1; p >= tfname; --p) 20484 { 20485 #ifdef FEAT_MBYTE 20486 if (has_mbyte) 20487 p -= mb_head_off(tfname, p); 20488 #endif 20489 if (vim_ispathsep(*p)) 20490 { 20491 if (sepcount == 0 || (hasTilde && sepcount == 1)) 20492 break; 20493 else 20494 sepcount --; 20495 } 20496 } 20497 if (hasTilde) 20498 { 20499 --p; 20500 if (p >= tfname) 20501 *p = '~'; 20502 else 20503 return -1; 20504 } 20505 else 20506 ++p; 20507 20508 /* Copy in the string - p indexes into tfname - allocated at pbuf */ 20509 vim_free(*bufp); 20510 *fnamelen = (int)STRLEN(p); 20511 *bufp = pbuf; 20512 *fnamep = p; 20513 20514 return 0; 20515 } 20516 #endif /* WIN3264 */ 20517 20518 /* 20519 * Adjust a filename, according to a string of modifiers. 20520 * *fnamep must be NUL terminated when called. When returning, the length is 20521 * determined by *fnamelen. 20522 * Returns valid flags. 20523 * When there is an error, *fnamep is set to NULL. 20524 */ 20525 int 20526 modify_fname(src, usedlen, fnamep, bufp, fnamelen) 20527 char_u *src; /* string with modifiers */ 20528 int *usedlen; /* characters after src that are used */ 20529 char_u **fnamep; /* file name so far */ 20530 char_u **bufp; /* buffer for allocated file name or NULL */ 20531 int *fnamelen; /* length of fnamep */ 20532 { 20533 int valid = 0; 20534 char_u *tail; 20535 char_u *s, *p, *pbuf; 20536 char_u dirname[MAXPATHL]; 20537 int c; 20538 int has_fullname = 0; 20539 #ifdef WIN3264 20540 int has_shortname = 0; 20541 #endif 20542 20543 repeat: 20544 /* ":p" - full path/file_name */ 20545 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') 20546 { 20547 has_fullname = 1; 20548 20549 valid |= VALID_PATH; 20550 *usedlen += 2; 20551 20552 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */ 20553 if ((*fnamep)[0] == '~' 20554 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME)) 20555 && ((*fnamep)[1] == '/' 20556 # ifdef BACKSLASH_IN_FILENAME 20557 || (*fnamep)[1] == '\\' 20558 # endif 20559 || (*fnamep)[1] == NUL) 20560 20561 #endif 20562 ) 20563 { 20564 *fnamep = expand_env_save(*fnamep); 20565 vim_free(*bufp); /* free any allocated file name */ 20566 *bufp = *fnamep; 20567 if (*fnamep == NULL) 20568 return -1; 20569 } 20570 20571 /* When "/." or "/.." is used: force expansion to get rid of it. */ 20572 for (p = *fnamep; *p != NUL; mb_ptr_adv(p)) 20573 { 20574 if (vim_ispathsep(*p) 20575 && p[1] == '.' 20576 && (p[2] == NUL 20577 || vim_ispathsep(p[2]) 20578 || (p[2] == '.' 20579 && (p[3] == NUL || vim_ispathsep(p[3]))))) 20580 break; 20581 } 20582 20583 /* FullName_save() is slow, don't use it when not needed. */ 20584 if (*p != NUL || !vim_isAbsName(*fnamep)) 20585 { 20586 *fnamep = FullName_save(*fnamep, *p != NUL); 20587 vim_free(*bufp); /* free any allocated file name */ 20588 *bufp = *fnamep; 20589 if (*fnamep == NULL) 20590 return -1; 20591 } 20592 20593 /* Append a path separator to a directory. */ 20594 if (mch_isdir(*fnamep)) 20595 { 20596 /* Make room for one or two extra characters. */ 20597 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2); 20598 vim_free(*bufp); /* free any allocated file name */ 20599 *bufp = *fnamep; 20600 if (*fnamep == NULL) 20601 return -1; 20602 add_pathsep(*fnamep); 20603 } 20604 } 20605 20606 /* ":." - path relative to the current directory */ 20607 /* ":~" - path relative to the home directory */ 20608 /* ":8" - shortname path - postponed till after */ 20609 while (src[*usedlen] == ':' 20610 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8')) 20611 { 20612 *usedlen += 2; 20613 if (c == '8') 20614 { 20615 #ifdef WIN3264 20616 has_shortname = 1; /* Postpone this. */ 20617 #endif 20618 continue; 20619 } 20620 pbuf = NULL; 20621 /* Need full path first (use expand_env() to remove a "~/") */ 20622 if (!has_fullname) 20623 { 20624 if (c == '.' && **fnamep == '~') 20625 p = pbuf = expand_env_save(*fnamep); 20626 else 20627 p = pbuf = FullName_save(*fnamep, FALSE); 20628 } 20629 else 20630 p = *fnamep; 20631 20632 has_fullname = 0; 20633 20634 if (p != NULL) 20635 { 20636 if (c == '.') 20637 { 20638 mch_dirname(dirname, MAXPATHL); 20639 s = shorten_fname(p, dirname); 20640 if (s != NULL) 20641 { 20642 *fnamep = s; 20643 if (pbuf != NULL) 20644 { 20645 vim_free(*bufp); /* free any allocated file name */ 20646 *bufp = pbuf; 20647 pbuf = NULL; 20648 } 20649 } 20650 } 20651 else 20652 { 20653 home_replace(NULL, p, dirname, MAXPATHL, TRUE); 20654 /* Only replace it when it starts with '~' */ 20655 if (*dirname == '~') 20656 { 20657 s = vim_strsave(dirname); 20658 if (s != NULL) 20659 { 20660 *fnamep = s; 20661 vim_free(*bufp); 20662 *bufp = s; 20663 } 20664 } 20665 } 20666 vim_free(pbuf); 20667 } 20668 } 20669 20670 tail = gettail(*fnamep); 20671 *fnamelen = (int)STRLEN(*fnamep); 20672 20673 /* ":h" - head, remove "/file_name", can be repeated */ 20674 /* Don't remove the first "/" or "c:\" */ 20675 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h') 20676 { 20677 valid |= VALID_HEAD; 20678 *usedlen += 2; 20679 s = get_past_head(*fnamep); 20680 while (tail > s && after_pathsep(s, tail)) 20681 --tail; 20682 *fnamelen = (int)(tail - *fnamep); 20683 #ifdef VMS 20684 if (*fnamelen > 0) 20685 *fnamelen += 1; /* the path separator is part of the path */ 20686 #endif 20687 while (tail > s && !after_pathsep(s, tail)) 20688 mb_ptr_back(*fnamep, tail); 20689 } 20690 20691 /* ":8" - shortname */ 20692 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8') 20693 { 20694 *usedlen += 2; 20695 #ifdef WIN3264 20696 has_shortname = 1; 20697 #endif 20698 } 20699 20700 #ifdef WIN3264 20701 /* Check shortname after we have done 'heads' and before we do 'tails' 20702 */ 20703 if (has_shortname) 20704 { 20705 pbuf = NULL; 20706 /* Copy the string if it is shortened by :h */ 20707 if (*fnamelen < (int)STRLEN(*fnamep)) 20708 { 20709 p = vim_strnsave(*fnamep, *fnamelen); 20710 if (p == 0) 20711 return -1; 20712 vim_free(*bufp); 20713 *bufp = *fnamep = p; 20714 } 20715 20716 /* Split into two implementations - makes it easier. First is where 20717 * there isn't a full name already, second is where there is. 20718 */ 20719 if (!has_fullname && !vim_isAbsName(*fnamep)) 20720 { 20721 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1) 20722 return -1; 20723 } 20724 else 20725 { 20726 int l; 20727 20728 /* Simple case, already have the full-name 20729 * Nearly always shorter, so try first time. */ 20730 l = *fnamelen; 20731 if (!get_short_pathname(fnamep, bufp, &l)) 20732 return -1; 20733 20734 if (l == 0) 20735 { 20736 /* Couldn't find the filename.. search the paths. 20737 */ 20738 l = *fnamelen; 20739 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1) 20740 return -1; 20741 } 20742 *fnamelen = l; 20743 } 20744 } 20745 #endif /* WIN3264 */ 20746 20747 /* ":t" - tail, just the basename */ 20748 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't') 20749 { 20750 *usedlen += 2; 20751 *fnamelen -= (int)(tail - *fnamep); 20752 *fnamep = tail; 20753 } 20754 20755 /* ":e" - extension, can be repeated */ 20756 /* ":r" - root, without extension, can be repeated */ 20757 while (src[*usedlen] == ':' 20758 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r')) 20759 { 20760 /* find a '.' in the tail: 20761 * - for second :e: before the current fname 20762 * - otherwise: The last '.' 20763 */ 20764 if (src[*usedlen + 1] == 'e' && *fnamep > tail) 20765 s = *fnamep - 2; 20766 else 20767 s = *fnamep + *fnamelen - 1; 20768 for ( ; s > tail; --s) 20769 if (s[0] == '.') 20770 break; 20771 if (src[*usedlen + 1] == 'e') /* :e */ 20772 { 20773 if (s > tail) 20774 { 20775 *fnamelen += (int)(*fnamep - (s + 1)); 20776 *fnamep = s + 1; 20777 #ifdef VMS 20778 /* cut version from the extension */ 20779 s = *fnamep + *fnamelen - 1; 20780 for ( ; s > *fnamep; --s) 20781 if (s[0] == ';') 20782 break; 20783 if (s > *fnamep) 20784 *fnamelen = s - *fnamep; 20785 #endif 20786 } 20787 else if (*fnamep <= tail) 20788 *fnamelen = 0; 20789 } 20790 else /* :r */ 20791 { 20792 if (s > tail) /* remove one extension */ 20793 *fnamelen = (int)(s - *fnamep); 20794 } 20795 *usedlen += 2; 20796 } 20797 20798 /* ":s?pat?foo?" - substitute */ 20799 /* ":gs?pat?foo?" - global substitute */ 20800 if (src[*usedlen] == ':' 20801 && (src[*usedlen + 1] == 's' 20802 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's'))) 20803 { 20804 char_u *str; 20805 char_u *pat; 20806 char_u *sub; 20807 int sep; 20808 char_u *flags; 20809 int didit = FALSE; 20810 20811 flags = (char_u *)""; 20812 s = src + *usedlen + 2; 20813 if (src[*usedlen + 1] == 'g') 20814 { 20815 flags = (char_u *)"g"; 20816 ++s; 20817 } 20818 20819 sep = *s++; 20820 if (sep) 20821 { 20822 /* find end of pattern */ 20823 p = vim_strchr(s, sep); 20824 if (p != NULL) 20825 { 20826 pat = vim_strnsave(s, (int)(p - s)); 20827 if (pat != NULL) 20828 { 20829 s = p + 1; 20830 /* find end of substitution */ 20831 p = vim_strchr(s, sep); 20832 if (p != NULL) 20833 { 20834 sub = vim_strnsave(s, (int)(p - s)); 20835 str = vim_strnsave(*fnamep, *fnamelen); 20836 if (sub != NULL && str != NULL) 20837 { 20838 *usedlen = (int)(p + 1 - src); 20839 s = do_string_sub(str, pat, sub, flags); 20840 if (s != NULL) 20841 { 20842 *fnamep = s; 20843 *fnamelen = (int)STRLEN(s); 20844 vim_free(*bufp); 20845 *bufp = s; 20846 didit = TRUE; 20847 } 20848 } 20849 vim_free(sub); 20850 vim_free(str); 20851 } 20852 vim_free(pat); 20853 } 20854 } 20855 /* after using ":s", repeat all the modifiers */ 20856 if (didit) 20857 goto repeat; 20858 } 20859 } 20860 20861 return valid; 20862 } 20863 20864 /* 20865 * Perform a substitution on "str" with pattern "pat" and substitute "sub". 20866 * "flags" can be "g" to do a global substitute. 20867 * Returns an allocated string, NULL for error. 20868 */ 20869 char_u * 20870 do_string_sub(str, pat, sub, flags) 20871 char_u *str; 20872 char_u *pat; 20873 char_u *sub; 20874 char_u *flags; 20875 { 20876 int sublen; 20877 regmatch_T regmatch; 20878 int i; 20879 int do_all; 20880 char_u *tail; 20881 garray_T ga; 20882 char_u *ret; 20883 char_u *save_cpo; 20884 20885 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */ 20886 save_cpo = p_cpo; 20887 p_cpo = (char_u *)""; 20888 20889 ga_init2(&ga, 1, 200); 20890 20891 do_all = (flags[0] == 'g'); 20892 20893 regmatch.rm_ic = p_ic; 20894 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 20895 if (regmatch.regprog != NULL) 20896 { 20897 tail = str; 20898 while (vim_regexec_nl(®match, str, (colnr_T)(tail - str))) 20899 { 20900 /* 20901 * Get some space for a temporary buffer to do the substitution 20902 * into. It will contain: 20903 * - The text up to where the match is. 20904 * - The substituted text. 20905 * - The text after the match. 20906 */ 20907 sublen = vim_regsub(®match, sub, tail, FALSE, TRUE, FALSE); 20908 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen - 20909 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL) 20910 { 20911 ga_clear(&ga); 20912 break; 20913 } 20914 20915 /* copy the text up to where the match is */ 20916 i = (int)(regmatch.startp[0] - tail); 20917 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); 20918 /* add the substituted text */ 20919 (void)vim_regsub(®match, sub, (char_u *)ga.ga_data 20920 + ga.ga_len + i, TRUE, TRUE, FALSE); 20921 ga.ga_len += i + sublen - 1; 20922 /* avoid getting stuck on a match with an empty string */ 20923 if (tail == regmatch.endp[0]) 20924 { 20925 if (*tail == NUL) 20926 break; 20927 *((char_u *)ga.ga_data + ga.ga_len) = *tail++; 20928 ++ga.ga_len; 20929 } 20930 else 20931 { 20932 tail = regmatch.endp[0]; 20933 if (*tail == NUL) 20934 break; 20935 } 20936 if (!do_all) 20937 break; 20938 } 20939 20940 if (ga.ga_data != NULL) 20941 STRCPY((char *)ga.ga_data + ga.ga_len, tail); 20942 20943 vim_free(regmatch.regprog); 20944 } 20945 20946 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data); 20947 ga_clear(&ga); 20948 p_cpo = save_cpo; 20949 20950 return ret; 20951 } 20952 20953 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */ 20954