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 static void list_vim_vars __ARGS((void)); 373 static void list_script_vars __ARGS((void)); 374 static void list_func_vars __ARGS((void)); 375 static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg)); 376 static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op)); 377 static int check_changedtick __ARGS((char_u *arg)); 378 static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags)); 379 static void clear_lval __ARGS((lval_T *lp)); 380 static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op)); 381 static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op)); 382 static void list_add_watch __ARGS((list_T *l, listwatch_T *lw)); 383 static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem)); 384 static void list_fix_watch __ARGS((list_T *l, listitem_T *item)); 385 static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep)); 386 static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit)); 387 static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock)); 388 static void item_lock __ARGS((typval_T *tv, int deep, int lock)); 389 static int tv_islocked __ARGS((typval_T *tv)); 390 391 static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate)); 392 static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 393 static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 394 static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 395 static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 396 static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 397 static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 398 static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 399 400 static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose)); 401 static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 402 static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 403 static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 404 static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 405 static int rettv_list_alloc __ARGS((typval_T *rettv)); 406 static listitem_T *listitem_alloc __ARGS((void)); 407 static void listitem_free __ARGS((listitem_T *item)); 408 static void listitem_remove __ARGS((list_T *l, listitem_T *item)); 409 static long list_len __ARGS((list_T *l)); 410 static int list_equal __ARGS((list_T *l1, list_T *l2, int ic)); 411 static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic)); 412 static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic)); 413 static listitem_T *list_find __ARGS((list_T *l, long n)); 414 static long list_find_nr __ARGS((list_T *l, long idx, int *errorp)); 415 static long list_idx_of_item __ARGS((list_T *l, listitem_T *item)); 416 static void list_append __ARGS((list_T *l, listitem_T *item)); 417 static int list_append_tv __ARGS((list_T *l, typval_T *tv)); 418 static int list_append_string __ARGS((list_T *l, char_u *str, int len)); 419 static int list_append_number __ARGS((list_T *l, varnumber_T n)); 420 static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item)); 421 static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef)); 422 static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv)); 423 static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID)); 424 static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2)); 425 static char_u *list2string __ARGS((typval_T *tv, int copyID)); 426 static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID)); 427 static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID)); 428 static void set_ref_in_list __ARGS((list_T *l, int copyID)); 429 static void set_ref_in_item __ARGS((typval_T *tv, int copyID)); 430 static void dict_unref __ARGS((dict_T *d)); 431 static void dict_free __ARGS((dict_T *d)); 432 static dictitem_T *dictitem_alloc __ARGS((char_u *key)); 433 static dictitem_T *dictitem_copy __ARGS((dictitem_T *org)); 434 static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item)); 435 static void dictitem_free __ARGS((dictitem_T *item)); 436 static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID)); 437 static int dict_add __ARGS((dict_T *d, dictitem_T *item)); 438 static long dict_len __ARGS((dict_T *d)); 439 static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len)); 440 static char_u *dict2string __ARGS((typval_T *tv, int copyID)); 441 static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 442 static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID)); 443 static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID)); 444 static char_u *string_quote __ARGS((char_u *str, int function)); 445 static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate)); 446 static int find_internal_func __ARGS((char_u *name)); 447 static char_u *deref_func_name __ARGS((char_u *name, int *lenp)); 448 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)); 449 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)); 450 static void emsg_funcname __ARGS((char *msg, char_u *name)); 451 452 static void f_add __ARGS((typval_T *argvars, typval_T *rettv)); 453 static void f_append __ARGS((typval_T *argvars, typval_T *rettv)); 454 static void f_argc __ARGS((typval_T *argvars, typval_T *rettv)); 455 static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv)); 456 static void f_argv __ARGS((typval_T *argvars, typval_T *rettv)); 457 static void f_browse __ARGS((typval_T *argvars, typval_T *rettv)); 458 static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv)); 459 static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv)); 460 static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv)); 461 static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv)); 462 static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv)); 463 static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv)); 464 static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv)); 465 static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv)); 466 static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv)); 467 static void f_call __ARGS((typval_T *argvars, typval_T *rettv)); 468 static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv)); 469 static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv)); 470 static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv)); 471 static void f_col __ARGS((typval_T *argvars, typval_T *rettv)); 472 #if defined(FEAT_INS_EXPAND) 473 static void f_complete __ARGS((typval_T *argvars, typval_T *rettv)); 474 static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv)); 475 static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv)); 476 #endif 477 static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv)); 478 static void f_copy __ARGS((typval_T *argvars, typval_T *rettv)); 479 static void f_count __ARGS((typval_T *argvars, typval_T *rettv)); 480 static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv)); 481 static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv)); 482 static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv)); 483 static void f_delete __ARGS((typval_T *argvars, typval_T *rettv)); 484 static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv)); 485 static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv)); 486 static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv)); 487 static void f_empty __ARGS((typval_T *argvars, typval_T *rettv)); 488 static void f_escape __ARGS((typval_T *argvars, typval_T *rettv)); 489 static void f_eval __ARGS((typval_T *argvars, typval_T *rettv)); 490 static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv)); 491 static void f_executable __ARGS((typval_T *argvars, typval_T *rettv)); 492 static void f_exists __ARGS((typval_T *argvars, typval_T *rettv)); 493 static void f_expand __ARGS((typval_T *argvars, typval_T *rettv)); 494 static void f_extend __ARGS((typval_T *argvars, typval_T *rettv)); 495 static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv)); 496 static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv)); 497 static void f_filter __ARGS((typval_T *argvars, typval_T *rettv)); 498 static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv)); 499 static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv)); 500 static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv)); 501 static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv)); 502 static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv)); 503 static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv)); 504 static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv)); 505 static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv)); 506 static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv)); 507 static void f_function __ARGS((typval_T *argvars, typval_T *rettv)); 508 static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv)); 509 static void f_get __ARGS((typval_T *argvars, typval_T *rettv)); 510 static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv)); 511 static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv)); 512 static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv)); 513 static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv)); 514 static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv)); 515 static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv)); 516 static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv)); 517 static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv)); 518 static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv)); 519 static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv)); 520 static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv)); 521 static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv)); 522 static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv)); 523 static void f_getline __ARGS((typval_T *argvars, typval_T *rettv)); 524 static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv)); 525 static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv)); 526 static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv)); 527 static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv)); 528 static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv)); 529 static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv)); 530 static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv)); 531 static void f_glob __ARGS((typval_T *argvars, typval_T *rettv)); 532 static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv)); 533 static void f_has __ARGS((typval_T *argvars, typval_T *rettv)); 534 static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv)); 535 static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv)); 536 static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv)); 537 static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv)); 538 static void f_histget __ARGS((typval_T *argvars, typval_T *rettv)); 539 static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv)); 540 static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv)); 541 static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv)); 542 static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv)); 543 static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv)); 544 static void f_indent __ARGS((typval_T *argvars, typval_T *rettv)); 545 static void f_index __ARGS((typval_T *argvars, typval_T *rettv)); 546 static void f_input __ARGS((typval_T *argvars, typval_T *rettv)); 547 static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv)); 548 static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv)); 549 static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv)); 550 static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv)); 551 static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv)); 552 static void f_insert __ARGS((typval_T *argvars, typval_T *rettv)); 553 static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv)); 554 static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv)); 555 static void f_items __ARGS((typval_T *argvars, typval_T *rettv)); 556 static void f_join __ARGS((typval_T *argvars, typval_T *rettv)); 557 static void f_keys __ARGS((typval_T *argvars, typval_T *rettv)); 558 static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv)); 559 static void f_len __ARGS((typval_T *argvars, typval_T *rettv)); 560 static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv)); 561 static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv)); 562 static void f_line __ARGS((typval_T *argvars, typval_T *rettv)); 563 static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv)); 564 static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv)); 565 static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv)); 566 static void f_map __ARGS((typval_T *argvars, typval_T *rettv)); 567 static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv)); 568 static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv)); 569 static void f_match __ARGS((typval_T *argvars, typval_T *rettv)); 570 static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv)); 571 static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv)); 572 static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv)); 573 static void f_max __ARGS((typval_T *argvars, typval_T *rettv)); 574 static void f_min __ARGS((typval_T *argvars, typval_T *rettv)); 575 #ifdef vim_mkdir 576 static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv)); 577 #endif 578 static void f_mode __ARGS((typval_T *argvars, typval_T *rettv)); 579 static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv)); 580 static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv)); 581 static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv)); 582 static void f_printf __ARGS((typval_T *argvars, typval_T *rettv)); 583 static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv)); 584 static void f_range __ARGS((typval_T *argvars, typval_T *rettv)); 585 static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv)); 586 static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv)); 587 static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv)); 588 static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv)); 589 static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv)); 590 static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv)); 591 static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv)); 592 static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv)); 593 static void f_remove __ARGS((typval_T *argvars, typval_T *rettv)); 594 static void f_rename __ARGS((typval_T *argvars, typval_T *rettv)); 595 static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv)); 596 static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv)); 597 static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv)); 598 static void f_search __ARGS((typval_T *argvars, typval_T *rettv)); 599 static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv)); 600 static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv)); 601 static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv)); 602 static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv)); 603 static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv)); 604 static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv)); 605 static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv)); 606 static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv)); 607 static void f_setline __ARGS((typval_T *argvars, typval_T *rettv)); 608 static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv)); 609 static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv)); 610 static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv)); 611 static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv)); 612 static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv)); 613 static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv)); 614 static void f_sort __ARGS((typval_T *argvars, typval_T *rettv)); 615 static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv)); 616 static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv)); 617 static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv)); 618 static void f_split __ARGS((typval_T *argvars, typval_T *rettv)); 619 static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv)); 620 #ifdef HAVE_STRFTIME 621 static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv)); 622 #endif 623 static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv)); 624 static void f_string __ARGS((typval_T *argvars, typval_T *rettv)); 625 static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv)); 626 static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv)); 627 static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv)); 628 static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv)); 629 static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv)); 630 static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv)); 631 static void f_synID __ARGS((typval_T *argvars, typval_T *rettv)); 632 static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv)); 633 static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv)); 634 static void f_system __ARGS((typval_T *argvars, typval_T *rettv)); 635 static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv)); 636 static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv)); 637 static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv)); 638 static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv)); 639 static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv)); 640 static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv)); 641 static void f_test __ARGS((typval_T *argvars, typval_T *rettv)); 642 static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv)); 643 static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv)); 644 static void f_tr __ARGS((typval_T *argvars, typval_T *rettv)); 645 static void f_type __ARGS((typval_T *argvars, typval_T *rettv)); 646 static void f_values __ARGS((typval_T *argvars, typval_T *rettv)); 647 static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv)); 648 static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv)); 649 static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv)); 650 static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv)); 651 static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv)); 652 static void f_winline __ARGS((typval_T *argvars, typval_T *rettv)); 653 static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv)); 654 static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv)); 655 static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv)); 656 static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv)); 657 static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv)); 658 static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv)); 659 660 static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump)); 661 static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum)); 662 static int get_env_len __ARGS((char_u **arg)); 663 static int get_id_len __ARGS((char_u **arg)); 664 static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose)); 665 static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags)); 666 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */ 667 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with 668 valid character */ 669 static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end)); 670 static int eval_isnamec __ARGS((int c)); 671 static int eval_isnamec1 __ARGS((int c)); 672 static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose)); 673 static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose)); 674 static typval_T *alloc_tv __ARGS((void)); 675 static typval_T *alloc_string_tv __ARGS((char_u *string)); 676 static void init_tv __ARGS((typval_T *varp)); 677 static long get_tv_number __ARGS((typval_T *varp)); 678 static linenr_T get_tv_lnum __ARGS((typval_T *argvars)); 679 static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf)); 680 static char_u *get_tv_string __ARGS((typval_T *varp)); 681 static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf)); 682 static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf)); 683 static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp)); 684 static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing)); 685 static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname)); 686 static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val)); 687 static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi)); 688 static void list_one_var __ARGS((dictitem_T *v, char_u *prefix)); 689 static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string)); 690 static void set_var __ARGS((char_u *name, typval_T *varp, int copy)); 691 static int var_check_ro __ARGS((int flags, char_u *name)); 692 static int tv_check_lock __ARGS((int lock, char_u *name)); 693 static void copy_tv __ARGS((typval_T *from, typval_T *to)); 694 static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID)); 695 static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags)); 696 static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd)); 697 static int eval_fname_script __ARGS((char_u *p)); 698 static int eval_fname_sid __ARGS((char_u *p)); 699 static void list_func_head __ARGS((ufunc_T *fp, int indent)); 700 static ufunc_T *find_func __ARGS((char_u *name)); 701 static int function_exists __ARGS((char_u *name)); 702 static int builtin_function __ARGS((char_u *name)); 703 #ifdef FEAT_PROFILE 704 static void func_do_profile __ARGS((ufunc_T *fp)); 705 static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self)); 706 static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self)); 707 static int 708 # ifdef __BORLANDC__ 709 _RTLENTRYF 710 # endif 711 prof_total_cmp __ARGS((const void *s1, const void *s2)); 712 static int 713 # ifdef __BORLANDC__ 714 _RTLENTRYF 715 # endif 716 prof_self_cmp __ARGS((const void *s1, const void *s2)); 717 #endif 718 static int script_autoload __ARGS((char_u *name, int reload)); 719 static char_u *autoload_name __ARGS((char_u *name)); 720 static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp)); 721 static void func_free __ARGS((ufunc_T *fp)); 722 static void func_unref __ARGS((char_u *name)); 723 static void func_ref __ARGS((char_u *name)); 724 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)); 725 static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr)); 726 static win_T *find_win_by_nr __ARGS((typval_T *vp)); 727 static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos)); 728 static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp)); 729 730 /* Character used as separated in autoload function/variable names. */ 731 #define AUTOLOAD_CHAR '#' 732 733 /* 734 * Initialize the global and v: variables. 735 */ 736 void 737 eval_init() 738 { 739 int i; 740 struct vimvar *p; 741 742 init_var_dict(&globvardict, &globvars_var); 743 init_var_dict(&vimvardict, &vimvars_var); 744 hash_init(&compat_hashtab); 745 hash_init(&func_hashtab); 746 747 for (i = 0; i < VV_LEN; ++i) 748 { 749 p = &vimvars[i]; 750 STRCPY(p->vv_di.di_key, p->vv_name); 751 if (p->vv_flags & VV_RO) 752 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 753 else if (p->vv_flags & VV_RO_SBX) 754 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX; 755 else 756 p->vv_di.di_flags = DI_FLAGS_FIX; 757 758 /* add to v: scope dict, unless the value is not always available */ 759 if (p->vv_type != VAR_UNKNOWN) 760 hash_add(&vimvarht, p->vv_di.di_key); 761 if (p->vv_flags & VV_COMPAT) 762 /* add to compat scope dict */ 763 hash_add(&compat_hashtab, p->vv_di.di_key); 764 } 765 } 766 767 #if defined(EXITFREE) || defined(PROTO) 768 void 769 eval_clear() 770 { 771 int i; 772 struct vimvar *p; 773 774 for (i = 0; i < VV_LEN; ++i) 775 { 776 p = &vimvars[i]; 777 if (p->vv_di.di_tv.v_type == VAR_STRING) 778 { 779 vim_free(p->vv_di.di_tv.vval.v_string); 780 p->vv_di.di_tv.vval.v_string = NULL; 781 } 782 } 783 hash_clear(&vimvarht); 784 hash_clear(&compat_hashtab); 785 786 /* script-local variables */ 787 for (i = 1; i <= ga_scripts.ga_len; ++i) 788 vars_clear(&SCRIPT_VARS(i)); 789 ga_clear(&ga_scripts); 790 free_scriptnames(); 791 792 /* global variables */ 793 vars_clear(&globvarht); 794 795 /* functions */ 796 free_all_functions(); 797 hash_clear(&func_hashtab); 798 799 /* unreferenced lists and dicts */ 800 (void)garbage_collect(); 801 } 802 #endif 803 804 /* 805 * Return the name of the executed function. 806 */ 807 char_u * 808 func_name(cookie) 809 void *cookie; 810 { 811 return ((funccall_T *)cookie)->func->uf_name; 812 } 813 814 /* 815 * Return the address holding the next breakpoint line for a funccall cookie. 816 */ 817 linenr_T * 818 func_breakpoint(cookie) 819 void *cookie; 820 { 821 return &((funccall_T *)cookie)->breakpoint; 822 } 823 824 /* 825 * Return the address holding the debug tick for a funccall cookie. 826 */ 827 int * 828 func_dbg_tick(cookie) 829 void *cookie; 830 { 831 return &((funccall_T *)cookie)->dbg_tick; 832 } 833 834 /* 835 * Return the nesting level for a funccall cookie. 836 */ 837 int 838 func_level(cookie) 839 void *cookie; 840 { 841 return ((funccall_T *)cookie)->level; 842 } 843 844 /* pointer to funccal for currently active function */ 845 funccall_T *current_funccal = NULL; 846 847 /* 848 * Return TRUE when a function was ended by a ":return" command. 849 */ 850 int 851 current_func_returned() 852 { 853 return current_funccal->returned; 854 } 855 856 857 /* 858 * Set an internal variable to a string value. Creates the variable if it does 859 * not already exist. 860 */ 861 void 862 set_internal_string_var(name, value) 863 char_u *name; 864 char_u *value; 865 { 866 char_u *val; 867 typval_T *tvp; 868 869 val = vim_strsave(value); 870 if (val != NULL) 871 { 872 tvp = alloc_string_tv(val); 873 if (tvp != NULL) 874 { 875 set_var(name, tvp, FALSE); 876 free_tv(tvp); 877 } 878 } 879 } 880 881 static lval_T *redir_lval = NULL; 882 static char_u *redir_endp = NULL; 883 static char_u *redir_varname = NULL; 884 885 /* 886 * Start recording command output to a variable 887 * Returns OK if successfully completed the setup. FAIL otherwise. 888 */ 889 int 890 var_redir_start(name, append) 891 char_u *name; 892 int append; /* append to an existing variable */ 893 { 894 int save_emsg; 895 int err; 896 typval_T tv; 897 898 /* Make sure a valid variable name is specified */ 899 if (!eval_isnamec1(*name)) 900 { 901 EMSG(_(e_invarg)); 902 return FAIL; 903 } 904 905 redir_varname = vim_strsave(name); 906 if (redir_varname == NULL) 907 return FAIL; 908 909 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T)); 910 if (redir_lval == NULL) 911 { 912 var_redir_stop(); 913 return FAIL; 914 } 915 916 /* Parse the variable name (can be a dict or list entry). */ 917 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE, 918 FNE_CHECK_START); 919 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL) 920 { 921 if (redir_endp != NULL && *redir_endp != NUL) 922 /* Trailing characters are present after the variable name */ 923 EMSG(_(e_trailing)); 924 else 925 EMSG(_(e_invarg)); 926 var_redir_stop(); 927 return FAIL; 928 } 929 930 /* check if we can write to the variable: set it to or append an empty 931 * string */ 932 save_emsg = did_emsg; 933 did_emsg = FALSE; 934 tv.v_type = VAR_STRING; 935 tv.vval.v_string = (char_u *)""; 936 if (append) 937 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"."); 938 else 939 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"="); 940 err = did_emsg; 941 did_emsg |= save_emsg; 942 if (err) 943 { 944 var_redir_stop(); 945 return FAIL; 946 } 947 if (redir_lval->ll_newkey != NULL) 948 { 949 /* Dictionary item was created, don't do it again. */ 950 vim_free(redir_lval->ll_newkey); 951 redir_lval->ll_newkey = NULL; 952 } 953 954 return OK; 955 } 956 957 /* 958 * Append "value[len]" to the variable set by var_redir_start(). 959 */ 960 void 961 var_redir_str(value, len) 962 char_u *value; 963 int len; 964 { 965 char_u *val; 966 typval_T tv; 967 int save_emsg; 968 int err; 969 970 if (redir_lval == NULL) 971 return; 972 973 if (len == -1) 974 /* Append the entire string */ 975 val = vim_strsave(value); 976 else 977 /* Append only the specified number of characters */ 978 val = vim_strnsave(value, len); 979 if (val == NULL) 980 return; 981 982 tv.v_type = VAR_STRING; 983 tv.vval.v_string = val; 984 985 save_emsg = did_emsg; 986 did_emsg = FALSE; 987 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)"."); 988 err = did_emsg; 989 did_emsg |= save_emsg; 990 if (err) 991 var_redir_stop(); 992 993 vim_free(tv.vval.v_string); 994 } 995 996 /* 997 * Stop redirecting command output to a variable. 998 */ 999 void 1000 var_redir_stop() 1001 { 1002 if (redir_lval != NULL) 1003 { 1004 clear_lval(redir_lval); 1005 vim_free(redir_lval); 1006 redir_lval = NULL; 1007 } 1008 vim_free(redir_varname); 1009 redir_varname = NULL; 1010 } 1011 1012 # if defined(FEAT_MBYTE) || defined(PROTO) 1013 int 1014 eval_charconvert(enc_from, enc_to, fname_from, fname_to) 1015 char_u *enc_from; 1016 char_u *enc_to; 1017 char_u *fname_from; 1018 char_u *fname_to; 1019 { 1020 int err = FALSE; 1021 1022 set_vim_var_string(VV_CC_FROM, enc_from, -1); 1023 set_vim_var_string(VV_CC_TO, enc_to, -1); 1024 set_vim_var_string(VV_FNAME_IN, fname_from, -1); 1025 set_vim_var_string(VV_FNAME_OUT, fname_to, -1); 1026 if (eval_to_bool(p_ccv, &err, NULL, FALSE)) 1027 err = TRUE; 1028 set_vim_var_string(VV_CC_FROM, NULL, -1); 1029 set_vim_var_string(VV_CC_TO, NULL, -1); 1030 set_vim_var_string(VV_FNAME_IN, NULL, -1); 1031 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 1032 1033 if (err) 1034 return FAIL; 1035 return OK; 1036 } 1037 # endif 1038 1039 # if defined(FEAT_POSTSCRIPT) || defined(PROTO) 1040 int 1041 eval_printexpr(fname, args) 1042 char_u *fname; 1043 char_u *args; 1044 { 1045 int err = FALSE; 1046 1047 set_vim_var_string(VV_FNAME_IN, fname, -1); 1048 set_vim_var_string(VV_CMDARG, args, -1); 1049 if (eval_to_bool(p_pexpr, &err, NULL, FALSE)) 1050 err = TRUE; 1051 set_vim_var_string(VV_FNAME_IN, NULL, -1); 1052 set_vim_var_string(VV_CMDARG, NULL, -1); 1053 1054 if (err) 1055 { 1056 mch_remove(fname); 1057 return FAIL; 1058 } 1059 return OK; 1060 } 1061 # endif 1062 1063 # if defined(FEAT_DIFF) || defined(PROTO) 1064 void 1065 eval_diff(origfile, newfile, outfile) 1066 char_u *origfile; 1067 char_u *newfile; 1068 char_u *outfile; 1069 { 1070 int err = FALSE; 1071 1072 set_vim_var_string(VV_FNAME_IN, origfile, -1); 1073 set_vim_var_string(VV_FNAME_NEW, newfile, -1); 1074 set_vim_var_string(VV_FNAME_OUT, outfile, -1); 1075 (void)eval_to_bool(p_dex, &err, NULL, FALSE); 1076 set_vim_var_string(VV_FNAME_IN, NULL, -1); 1077 set_vim_var_string(VV_FNAME_NEW, NULL, -1); 1078 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 1079 } 1080 1081 void 1082 eval_patch(origfile, difffile, outfile) 1083 char_u *origfile; 1084 char_u *difffile; 1085 char_u *outfile; 1086 { 1087 int err; 1088 1089 set_vim_var_string(VV_FNAME_IN, origfile, -1); 1090 set_vim_var_string(VV_FNAME_DIFF, difffile, -1); 1091 set_vim_var_string(VV_FNAME_OUT, outfile, -1); 1092 (void)eval_to_bool(p_pex, &err, NULL, FALSE); 1093 set_vim_var_string(VV_FNAME_IN, NULL, -1); 1094 set_vim_var_string(VV_FNAME_DIFF, NULL, -1); 1095 set_vim_var_string(VV_FNAME_OUT, NULL, -1); 1096 } 1097 # endif 1098 1099 /* 1100 * Top level evaluation function, returning a boolean. 1101 * Sets "error" to TRUE if there was an error. 1102 * Return TRUE or FALSE. 1103 */ 1104 int 1105 eval_to_bool(arg, error, nextcmd, skip) 1106 char_u *arg; 1107 int *error; 1108 char_u **nextcmd; 1109 int skip; /* only parse, don't execute */ 1110 { 1111 typval_T tv; 1112 int retval = FALSE; 1113 1114 if (skip) 1115 ++emsg_skip; 1116 if (eval0(arg, &tv, nextcmd, !skip) == FAIL) 1117 *error = TRUE; 1118 else 1119 { 1120 *error = FALSE; 1121 if (!skip) 1122 { 1123 retval = (get_tv_number_chk(&tv, error) != 0); 1124 clear_tv(&tv); 1125 } 1126 } 1127 if (skip) 1128 --emsg_skip; 1129 1130 return retval; 1131 } 1132 1133 /* 1134 * Top level evaluation function, returning a string. If "skip" is TRUE, 1135 * only parsing to "nextcmd" is done, without reporting errors. Return 1136 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE. 1137 */ 1138 char_u * 1139 eval_to_string_skip(arg, nextcmd, skip) 1140 char_u *arg; 1141 char_u **nextcmd; 1142 int skip; /* only parse, don't execute */ 1143 { 1144 typval_T tv; 1145 char_u *retval; 1146 1147 if (skip) 1148 ++emsg_skip; 1149 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip) 1150 retval = NULL; 1151 else 1152 { 1153 retval = vim_strsave(get_tv_string(&tv)); 1154 clear_tv(&tv); 1155 } 1156 if (skip) 1157 --emsg_skip; 1158 1159 return retval; 1160 } 1161 1162 /* 1163 * Skip over an expression at "*pp". 1164 * Return FAIL for an error, OK otherwise. 1165 */ 1166 int 1167 skip_expr(pp) 1168 char_u **pp; 1169 { 1170 typval_T rettv; 1171 1172 *pp = skipwhite(*pp); 1173 return eval1(pp, &rettv, FALSE); 1174 } 1175 1176 /* 1177 * Top level evaluation function, returning a string. 1178 * Return pointer to allocated memory, or NULL for failure. 1179 */ 1180 char_u * 1181 eval_to_string(arg, nextcmd, dolist) 1182 char_u *arg; 1183 char_u **nextcmd; 1184 int dolist; /* turn List into sequence of lines */ 1185 { 1186 typval_T tv; 1187 char_u *retval; 1188 garray_T ga; 1189 1190 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL) 1191 retval = NULL; 1192 else 1193 { 1194 if (dolist && tv.v_type == VAR_LIST) 1195 { 1196 ga_init2(&ga, (int)sizeof(char), 80); 1197 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0); 1198 ga_append(&ga, NUL); 1199 retval = (char_u *)ga.ga_data; 1200 } 1201 else 1202 retval = vim_strsave(get_tv_string(&tv)); 1203 clear_tv(&tv); 1204 } 1205 1206 return retval; 1207 } 1208 1209 /* 1210 * Call eval_to_string() without using current local variables and using 1211 * textlock. When "use_sandbox" is TRUE use the sandbox. 1212 */ 1213 char_u * 1214 eval_to_string_safe(arg, nextcmd, use_sandbox) 1215 char_u *arg; 1216 char_u **nextcmd; 1217 int use_sandbox; 1218 { 1219 char_u *retval; 1220 void *save_funccalp; 1221 1222 save_funccalp = save_funccal(); 1223 if (use_sandbox) 1224 ++sandbox; 1225 ++textlock; 1226 retval = eval_to_string(arg, nextcmd, FALSE); 1227 if (use_sandbox) 1228 --sandbox; 1229 --textlock; 1230 restore_funccal(save_funccalp); 1231 return retval; 1232 } 1233 1234 /* 1235 * Top level evaluation function, returning a number. 1236 * Evaluates "expr" silently. 1237 * Returns -1 for an error. 1238 */ 1239 int 1240 eval_to_number(expr) 1241 char_u *expr; 1242 { 1243 typval_T rettv; 1244 int retval; 1245 char_u *p = skipwhite(expr); 1246 1247 ++emsg_off; 1248 1249 if (eval1(&p, &rettv, TRUE) == FAIL) 1250 retval = -1; 1251 else 1252 { 1253 retval = get_tv_number_chk(&rettv, NULL); 1254 clear_tv(&rettv); 1255 } 1256 --emsg_off; 1257 1258 return retval; 1259 } 1260 1261 /* 1262 * Prepare v: variable "idx" to be used. 1263 * Save the current typeval in "save_tv". 1264 * When not used yet add the variable to the v: hashtable. 1265 */ 1266 static void 1267 prepare_vimvar(idx, save_tv) 1268 int idx; 1269 typval_T *save_tv; 1270 { 1271 *save_tv = vimvars[idx].vv_tv; 1272 if (vimvars[idx].vv_type == VAR_UNKNOWN) 1273 hash_add(&vimvarht, vimvars[idx].vv_di.di_key); 1274 } 1275 1276 /* 1277 * Restore v: variable "idx" to typeval "save_tv". 1278 * When no longer defined, remove the variable from the v: hashtable. 1279 */ 1280 static void 1281 restore_vimvar(idx, save_tv) 1282 int idx; 1283 typval_T *save_tv; 1284 { 1285 hashitem_T *hi; 1286 1287 clear_tv(&vimvars[idx].vv_tv); 1288 vimvars[idx].vv_tv = *save_tv; 1289 if (vimvars[idx].vv_type == VAR_UNKNOWN) 1290 { 1291 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key); 1292 if (HASHITEM_EMPTY(hi)) 1293 EMSG2(_(e_intern2), "restore_vimvar()"); 1294 else 1295 hash_remove(&vimvarht, hi); 1296 } 1297 } 1298 1299 #if defined(FEAT_SPELL) || defined(PROTO) 1300 /* 1301 * Evaluate an expression to a list with suggestions. 1302 * For the "expr:" part of 'spellsuggest'. 1303 */ 1304 list_T * 1305 eval_spell_expr(badword, expr) 1306 char_u *badword; 1307 char_u *expr; 1308 { 1309 typval_T save_val; 1310 typval_T rettv; 1311 list_T *list = NULL; 1312 char_u *p = skipwhite(expr); 1313 1314 /* Set "v:val" to the bad word. */ 1315 prepare_vimvar(VV_VAL, &save_val); 1316 vimvars[VV_VAL].vv_type = VAR_STRING; 1317 vimvars[VV_VAL].vv_str = badword; 1318 if (p_verbose == 0) 1319 ++emsg_off; 1320 1321 if (eval1(&p, &rettv, TRUE) == OK) 1322 { 1323 if (rettv.v_type != VAR_LIST) 1324 clear_tv(&rettv); 1325 else 1326 list = rettv.vval.v_list; 1327 } 1328 1329 if (p_verbose == 0) 1330 --emsg_off; 1331 vimvars[VV_VAL].vv_str = NULL; 1332 restore_vimvar(VV_VAL, &save_val); 1333 1334 return list; 1335 } 1336 1337 /* 1338 * "list" is supposed to contain two items: a word and a number. Return the 1339 * word in "pp" and the number as the return value. 1340 * Return -1 if anything isn't right. 1341 * Used to get the good word and score from the eval_spell_expr() result. 1342 */ 1343 int 1344 get_spellword(list, pp) 1345 list_T *list; 1346 char_u **pp; 1347 { 1348 listitem_T *li; 1349 1350 li = list->lv_first; 1351 if (li == NULL) 1352 return -1; 1353 *pp = get_tv_string(&li->li_tv); 1354 1355 li = li->li_next; 1356 if (li == NULL) 1357 return -1; 1358 return get_tv_number(&li->li_tv); 1359 } 1360 #endif 1361 1362 /* 1363 * Top level evaluation function. 1364 * Returns an allocated typval_T with the result. 1365 * Returns NULL when there is an error. 1366 */ 1367 typval_T * 1368 eval_expr(arg, nextcmd) 1369 char_u *arg; 1370 char_u **nextcmd; 1371 { 1372 typval_T *tv; 1373 1374 tv = (typval_T *)alloc(sizeof(typval_T)); 1375 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL) 1376 { 1377 vim_free(tv); 1378 tv = NULL; 1379 } 1380 1381 return tv; 1382 } 1383 1384 1385 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO) 1386 /* 1387 * Call some vimL function and return the result in "*rettv". 1388 * Uses argv[argc] for the function arguments. 1389 * Returns OK or FAIL. 1390 */ 1391 static int 1392 call_vim_function(func, argc, argv, safe, rettv) 1393 char_u *func; 1394 int argc; 1395 char_u **argv; 1396 int safe; /* use the sandbox */ 1397 typval_T *rettv; 1398 { 1399 typval_T *argvars; 1400 long n; 1401 int len; 1402 int i; 1403 int doesrange; 1404 void *save_funccalp = NULL; 1405 int ret; 1406 1407 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T))); 1408 if (argvars == NULL) 1409 return FAIL; 1410 1411 for (i = 0; i < argc; i++) 1412 { 1413 /* Pass a NULL or empty argument as an empty string */ 1414 if (argv[i] == NULL || *argv[i] == NUL) 1415 { 1416 argvars[i].v_type = VAR_STRING; 1417 argvars[i].vval.v_string = (char_u *)""; 1418 continue; 1419 } 1420 1421 /* Recognize a number argument, the others must be strings. */ 1422 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL); 1423 if (len != 0 && len == (int)STRLEN(argv[i])) 1424 { 1425 argvars[i].v_type = VAR_NUMBER; 1426 argvars[i].vval.v_number = n; 1427 } 1428 else 1429 { 1430 argvars[i].v_type = VAR_STRING; 1431 argvars[i].vval.v_string = argv[i]; 1432 } 1433 } 1434 1435 if (safe) 1436 { 1437 save_funccalp = save_funccal(); 1438 ++sandbox; 1439 } 1440 1441 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 1442 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars, 1443 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 1444 &doesrange, TRUE, NULL); 1445 if (safe) 1446 { 1447 --sandbox; 1448 restore_funccal(save_funccalp); 1449 } 1450 vim_free(argvars); 1451 1452 if (ret == FAIL) 1453 clear_tv(rettv); 1454 1455 return ret; 1456 } 1457 1458 /* 1459 * Call vimL function "func" and return the result as a string. 1460 * Returns NULL when calling the function fails. 1461 * Uses argv[argc] for the function arguments. 1462 */ 1463 void * 1464 call_func_retstr(func, argc, argv, safe) 1465 char_u *func; 1466 int argc; 1467 char_u **argv; 1468 int safe; /* use the sandbox */ 1469 { 1470 typval_T rettv; 1471 char_u *retval; 1472 1473 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL) 1474 return NULL; 1475 1476 retval = vim_strsave(get_tv_string(&rettv)); 1477 clear_tv(&rettv); 1478 return retval; 1479 } 1480 1481 #if defined(FEAT_COMPL_FUNC) || defined(PROTO) 1482 /* 1483 * Call vimL function "func" and return the result as a number. 1484 * Returns -1 when calling the function fails. 1485 * Uses argv[argc] for the function arguments. 1486 */ 1487 long 1488 call_func_retnr(func, argc, argv, safe) 1489 char_u *func; 1490 int argc; 1491 char_u **argv; 1492 int safe; /* use the sandbox */ 1493 { 1494 typval_T rettv; 1495 long retval; 1496 1497 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL) 1498 return -1; 1499 1500 retval = get_tv_number_chk(&rettv, NULL); 1501 clear_tv(&rettv); 1502 return retval; 1503 } 1504 #endif 1505 1506 /* 1507 * Call vimL function "func" and return the result as a list 1508 * Uses argv[argc] for the function arguments. 1509 */ 1510 void * 1511 call_func_retlist(func, argc, argv, safe) 1512 char_u *func; 1513 int argc; 1514 char_u **argv; 1515 int safe; /* use the sandbox */ 1516 { 1517 typval_T rettv; 1518 1519 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL) 1520 return NULL; 1521 1522 if (rettv.v_type != VAR_LIST) 1523 { 1524 clear_tv(&rettv); 1525 return NULL; 1526 } 1527 1528 return rettv.vval.v_list; 1529 } 1530 1531 #endif 1532 1533 /* 1534 * Save the current function call pointer, and set it to NULL. 1535 * Used when executing autocommands and for ":source". 1536 */ 1537 void * 1538 save_funccal() 1539 { 1540 funccall_T *fc = current_funccal; 1541 1542 current_funccal = NULL; 1543 return (void *)fc; 1544 } 1545 1546 void 1547 restore_funccal(vfc) 1548 void *vfc; 1549 { 1550 funccall_T *fc = (funccall_T *)vfc; 1551 1552 current_funccal = fc; 1553 } 1554 1555 #if defined(FEAT_PROFILE) || defined(PROTO) 1556 /* 1557 * Prepare profiling for entering a child or something else that is not 1558 * counted for the script/function itself. 1559 * Should always be called in pair with prof_child_exit(). 1560 */ 1561 void 1562 prof_child_enter(tm) 1563 proftime_T *tm; /* place to store waittime */ 1564 { 1565 funccall_T *fc = current_funccal; 1566 1567 if (fc != NULL && fc->func->uf_profiling) 1568 profile_start(&fc->prof_child); 1569 script_prof_save(tm); 1570 } 1571 1572 /* 1573 * Take care of time spent in a child. 1574 * Should always be called after prof_child_enter(). 1575 */ 1576 void 1577 prof_child_exit(tm) 1578 proftime_T *tm; /* where waittime was stored */ 1579 { 1580 funccall_T *fc = current_funccal; 1581 1582 if (fc != NULL && fc->func->uf_profiling) 1583 { 1584 profile_end(&fc->prof_child); 1585 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */ 1586 profile_add(&fc->func->uf_tm_children, &fc->prof_child); 1587 profile_add(&fc->func->uf_tml_children, &fc->prof_child); 1588 } 1589 script_prof_restore(tm); 1590 } 1591 #endif 1592 1593 1594 #ifdef FEAT_FOLDING 1595 /* 1596 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding 1597 * it in "*cp". Doesn't give error messages. 1598 */ 1599 int 1600 eval_foldexpr(arg, cp) 1601 char_u *arg; 1602 int *cp; 1603 { 1604 typval_T tv; 1605 int retval; 1606 char_u *s; 1607 int use_sandbox = was_set_insecurely((char_u *)"foldexpr", 1608 OPT_LOCAL); 1609 1610 ++emsg_off; 1611 if (use_sandbox) 1612 ++sandbox; 1613 ++textlock; 1614 *cp = NUL; 1615 if (eval0(arg, &tv, NULL, TRUE) == FAIL) 1616 retval = 0; 1617 else 1618 { 1619 /* If the result is a number, just return the number. */ 1620 if (tv.v_type == VAR_NUMBER) 1621 retval = tv.vval.v_number; 1622 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL) 1623 retval = 0; 1624 else 1625 { 1626 /* If the result is a string, check if there is a non-digit before 1627 * the number. */ 1628 s = tv.vval.v_string; 1629 if (!VIM_ISDIGIT(*s) && *s != '-') 1630 *cp = *s++; 1631 retval = atol((char *)s); 1632 } 1633 clear_tv(&tv); 1634 } 1635 --emsg_off; 1636 if (use_sandbox) 1637 --sandbox; 1638 --textlock; 1639 1640 return retval; 1641 } 1642 #endif 1643 1644 /* 1645 * ":let" list all variable values 1646 * ":let var1 var2" list variable values 1647 * ":let var = expr" assignment command. 1648 * ":let var += expr" assignment command. 1649 * ":let var -= expr" assignment command. 1650 * ":let var .= expr" assignment command. 1651 * ":let [var1, var2] = expr" unpack list. 1652 */ 1653 void 1654 ex_let(eap) 1655 exarg_T *eap; 1656 { 1657 char_u *arg = eap->arg; 1658 char_u *expr = NULL; 1659 typval_T rettv; 1660 int i; 1661 int var_count = 0; 1662 int semicolon = 0; 1663 char_u op[2]; 1664 char_u *argend; 1665 1666 argend = skip_var_list(arg, &var_count, &semicolon); 1667 if (argend == NULL) 1668 return; 1669 if (argend > arg && argend[-1] == '.') /* for var.='str' */ 1670 --argend; 1671 expr = vim_strchr(argend, '='); 1672 if (expr == NULL) 1673 { 1674 /* 1675 * ":let" without "=": list variables 1676 */ 1677 if (*arg == '[') 1678 EMSG(_(e_invarg)); 1679 else if (!ends_excmd(*arg)) 1680 /* ":let var1 var2" */ 1681 arg = list_arg_vars(eap, arg); 1682 else if (!eap->skip) 1683 { 1684 /* ":let" */ 1685 list_glob_vars(); 1686 list_buf_vars(); 1687 list_win_vars(); 1688 list_script_vars(); 1689 list_func_vars(); 1690 list_vim_vars(); 1691 } 1692 eap->nextcmd = check_nextcmd(arg); 1693 } 1694 else 1695 { 1696 op[0] = '='; 1697 op[1] = NUL; 1698 if (expr > argend) 1699 { 1700 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL) 1701 op[0] = expr[-1]; /* +=, -= or .= */ 1702 } 1703 expr = skipwhite(expr + 1); 1704 1705 if (eap->skip) 1706 ++emsg_skip; 1707 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip); 1708 if (eap->skip) 1709 { 1710 if (i != FAIL) 1711 clear_tv(&rettv); 1712 --emsg_skip; 1713 } 1714 else if (i != FAIL) 1715 { 1716 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count, 1717 op); 1718 clear_tv(&rettv); 1719 } 1720 } 1721 } 1722 1723 /* 1724 * Assign the typevalue "tv" to the variable or variables at "arg_start". 1725 * Handles both "var" with any type and "[var, var; var]" with a list type. 1726 * When "nextchars" is not NULL it points to a string with characters that 1727 * must appear after the variable(s). Use "+", "-" or "." for add, subtract 1728 * or concatenate. 1729 * Returns OK or FAIL; 1730 */ 1731 static int 1732 ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars) 1733 char_u *arg_start; 1734 typval_T *tv; 1735 int copy; /* copy values from "tv", don't move */ 1736 int semicolon; /* from skip_var_list() */ 1737 int var_count; /* from skip_var_list() */ 1738 char_u *nextchars; 1739 { 1740 char_u *arg = arg_start; 1741 list_T *l; 1742 int i; 1743 listitem_T *item; 1744 typval_T ltv; 1745 1746 if (*arg != '[') 1747 { 1748 /* 1749 * ":let var = expr" or ":for var in list" 1750 */ 1751 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL) 1752 return FAIL; 1753 return OK; 1754 } 1755 1756 /* 1757 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist" 1758 */ 1759 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL) 1760 { 1761 EMSG(_(e_listreq)); 1762 return FAIL; 1763 } 1764 1765 i = list_len(l); 1766 if (semicolon == 0 && var_count < i) 1767 { 1768 EMSG(_("E687: Less targets than List items")); 1769 return FAIL; 1770 } 1771 if (var_count - semicolon > i) 1772 { 1773 EMSG(_("E688: More targets than List items")); 1774 return FAIL; 1775 } 1776 1777 item = l->lv_first; 1778 while (*arg != ']') 1779 { 1780 arg = skipwhite(arg + 1); 1781 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars); 1782 item = item->li_next; 1783 if (arg == NULL) 1784 return FAIL; 1785 1786 arg = skipwhite(arg); 1787 if (*arg == ';') 1788 { 1789 /* Put the rest of the list (may be empty) in the var after ';'. 1790 * Create a new list for this. */ 1791 l = list_alloc(); 1792 if (l == NULL) 1793 return FAIL; 1794 while (item != NULL) 1795 { 1796 list_append_tv(l, &item->li_tv); 1797 item = item->li_next; 1798 } 1799 1800 ltv.v_type = VAR_LIST; 1801 ltv.v_lock = 0; 1802 ltv.vval.v_list = l; 1803 l->lv_refcount = 1; 1804 1805 arg = ex_let_one(skipwhite(arg + 1), <v, FALSE, 1806 (char_u *)"]", nextchars); 1807 clear_tv(<v); 1808 if (arg == NULL) 1809 return FAIL; 1810 break; 1811 } 1812 else if (*arg != ',' && *arg != ']') 1813 { 1814 EMSG2(_(e_intern2), "ex_let_vars()"); 1815 return FAIL; 1816 } 1817 } 1818 1819 return OK; 1820 } 1821 1822 /* 1823 * Skip over assignable variable "var" or list of variables "[var, var]". 1824 * Used for ":let varvar = expr" and ":for varvar in expr". 1825 * For "[var, var]" increment "*var_count" for each variable. 1826 * for "[var, var; var]" set "semicolon". 1827 * Return NULL for an error. 1828 */ 1829 static char_u * 1830 skip_var_list(arg, var_count, semicolon) 1831 char_u *arg; 1832 int *var_count; 1833 int *semicolon; 1834 { 1835 char_u *p, *s; 1836 1837 if (*arg == '[') 1838 { 1839 /* "[var, var]": find the matching ']'. */ 1840 p = arg; 1841 for (;;) 1842 { 1843 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */ 1844 s = skip_var_one(p); 1845 if (s == p) 1846 { 1847 EMSG2(_(e_invarg2), p); 1848 return NULL; 1849 } 1850 ++*var_count; 1851 1852 p = skipwhite(s); 1853 if (*p == ']') 1854 break; 1855 else if (*p == ';') 1856 { 1857 if (*semicolon == 1) 1858 { 1859 EMSG(_("Double ; in list of variables")); 1860 return NULL; 1861 } 1862 *semicolon = 1; 1863 } 1864 else if (*p != ',') 1865 { 1866 EMSG2(_(e_invarg2), p); 1867 return NULL; 1868 } 1869 } 1870 return p + 1; 1871 } 1872 else 1873 return skip_var_one(arg); 1874 } 1875 1876 /* 1877 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key, 1878 * l[idx]. 1879 */ 1880 static char_u * 1881 skip_var_one(arg) 1882 char_u *arg; 1883 { 1884 if (*arg == '@' && arg[1] != NUL) 1885 return arg + 2; 1886 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg, 1887 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); 1888 } 1889 1890 /* 1891 * List variables for hashtab "ht" with prefix "prefix". 1892 * If "empty" is TRUE also list NULL strings as empty strings. 1893 */ 1894 static void 1895 list_hashtable_vars(ht, prefix, empty) 1896 hashtab_T *ht; 1897 char_u *prefix; 1898 int empty; 1899 { 1900 hashitem_T *hi; 1901 dictitem_T *di; 1902 int todo; 1903 1904 todo = ht->ht_used; 1905 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi) 1906 { 1907 if (!HASHITEM_EMPTY(hi)) 1908 { 1909 --todo; 1910 di = HI2DI(hi); 1911 if (empty || di->di_tv.v_type != VAR_STRING 1912 || di->di_tv.vval.v_string != NULL) 1913 list_one_var(di, prefix); 1914 } 1915 } 1916 } 1917 1918 /* 1919 * List global variables. 1920 */ 1921 static void 1922 list_glob_vars() 1923 { 1924 list_hashtable_vars(&globvarht, (char_u *)"", TRUE); 1925 } 1926 1927 /* 1928 * List buffer variables. 1929 */ 1930 static void 1931 list_buf_vars() 1932 { 1933 char_u numbuf[NUMBUFLEN]; 1934 1935 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE); 1936 1937 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick); 1938 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf); 1939 } 1940 1941 /* 1942 * List window variables. 1943 */ 1944 static void 1945 list_win_vars() 1946 { 1947 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE); 1948 } 1949 1950 /* 1951 * List Vim variables. 1952 */ 1953 static void 1954 list_vim_vars() 1955 { 1956 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE); 1957 } 1958 1959 /* 1960 * List script-local variables, if there is a script. 1961 */ 1962 static void 1963 list_script_vars() 1964 { 1965 if (current_SID > 0 && current_SID <= ga_scripts.ga_len) 1966 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE); 1967 } 1968 1969 /* 1970 * List function variables, if there is a function. 1971 */ 1972 static void 1973 list_func_vars() 1974 { 1975 if (current_funccal != NULL) 1976 list_hashtable_vars(¤t_funccal->l_vars.dv_hashtab, 1977 (char_u *)"l:", FALSE); 1978 } 1979 1980 /* 1981 * List variables in "arg". 1982 */ 1983 static char_u * 1984 list_arg_vars(eap, arg) 1985 exarg_T *eap; 1986 char_u *arg; 1987 { 1988 int error = FALSE; 1989 int len; 1990 char_u *name; 1991 char_u *name_start; 1992 char_u *arg_subsc; 1993 char_u *tofree; 1994 typval_T tv; 1995 1996 while (!ends_excmd(*arg) && !got_int) 1997 { 1998 if (error || eap->skip) 1999 { 2000 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); 2001 if (!vim_iswhite(*arg) && !ends_excmd(*arg)) 2002 { 2003 emsg_severe = TRUE; 2004 EMSG(_(e_trailing)); 2005 break; 2006 } 2007 } 2008 else 2009 { 2010 /* get_name_len() takes care of expanding curly braces */ 2011 name_start = name = arg; 2012 len = get_name_len(&arg, &tofree, TRUE, TRUE); 2013 if (len <= 0) 2014 { 2015 /* This is mainly to keep test 49 working: when expanding 2016 * curly braces fails overrule the exception error message. */ 2017 if (len < 0 && !aborting()) 2018 { 2019 emsg_severe = TRUE; 2020 EMSG2(_(e_invarg2), arg); 2021 break; 2022 } 2023 error = TRUE; 2024 } 2025 else 2026 { 2027 if (tofree != NULL) 2028 name = tofree; 2029 if (get_var_tv(name, len, &tv, TRUE) == FAIL) 2030 error = TRUE; 2031 else 2032 { 2033 /* handle d.key, l[idx], f(expr) */ 2034 arg_subsc = arg; 2035 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL) 2036 error = TRUE; 2037 else 2038 { 2039 if (arg == arg_subsc && len == 2 && name[1] == ':') 2040 { 2041 switch (*name) 2042 { 2043 case 'g': list_glob_vars(); break; 2044 case 'b': list_buf_vars(); break; 2045 case 'w': list_win_vars(); break; 2046 case 'v': list_vim_vars(); break; 2047 case 's': list_script_vars(); break; 2048 case 'l': list_func_vars(); break; 2049 default: 2050 EMSG2(_("E738: Can't list variables for %s"), name); 2051 } 2052 } 2053 else 2054 { 2055 char_u numbuf[NUMBUFLEN]; 2056 char_u *tf; 2057 int c; 2058 char_u *s; 2059 2060 s = echo_string(&tv, &tf, numbuf, 0); 2061 c = *arg; 2062 *arg = NUL; 2063 list_one_var_a((char_u *)"", 2064 arg == arg_subsc ? name : name_start, 2065 tv.v_type, s == NULL ? (char_u *)"" : s); 2066 *arg = c; 2067 vim_free(tf); 2068 } 2069 clear_tv(&tv); 2070 } 2071 } 2072 } 2073 2074 vim_free(tofree); 2075 } 2076 2077 arg = skipwhite(arg); 2078 } 2079 2080 return arg; 2081 } 2082 2083 /* 2084 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value. 2085 * Returns a pointer to the char just after the var name. 2086 * Returns NULL if there is an error. 2087 */ 2088 static char_u * 2089 ex_let_one(arg, tv, copy, endchars, op) 2090 char_u *arg; /* points to variable name */ 2091 typval_T *tv; /* value to assign to variable */ 2092 int copy; /* copy value from "tv" */ 2093 char_u *endchars; /* valid chars after variable name or NULL */ 2094 char_u *op; /* "+", "-", "." or NULL*/ 2095 { 2096 int c1; 2097 char_u *name; 2098 char_u *p; 2099 char_u *arg_end = NULL; 2100 int len; 2101 int opt_flags; 2102 char_u *tofree = NULL; 2103 2104 /* 2105 * ":let $VAR = expr": Set environment variable. 2106 */ 2107 if (*arg == '$') 2108 { 2109 /* Find the end of the name. */ 2110 ++arg; 2111 name = arg; 2112 len = get_env_len(&arg); 2113 if (len == 0) 2114 EMSG2(_(e_invarg2), name - 1); 2115 else 2116 { 2117 if (op != NULL && (*op == '+' || *op == '-')) 2118 EMSG2(_(e_letwrong), op); 2119 else if (endchars != NULL 2120 && vim_strchr(endchars, *skipwhite(arg)) == NULL) 2121 EMSG(_(e_letunexp)); 2122 else 2123 { 2124 c1 = name[len]; 2125 name[len] = NUL; 2126 p = get_tv_string_chk(tv); 2127 if (p != NULL && op != NULL && *op == '.') 2128 { 2129 int mustfree = FALSE; 2130 char_u *s = vim_getenv(name, &mustfree); 2131 2132 if (s != NULL) 2133 { 2134 p = tofree = concat_str(s, p); 2135 if (mustfree) 2136 vim_free(s); 2137 } 2138 } 2139 if (p != NULL) 2140 { 2141 vim_setenv(name, p); 2142 if (STRICMP(name, "HOME") == 0) 2143 init_homedir(); 2144 else if (didset_vim && STRICMP(name, "VIM") == 0) 2145 didset_vim = FALSE; 2146 else if (didset_vimruntime 2147 && STRICMP(name, "VIMRUNTIME") == 0) 2148 didset_vimruntime = FALSE; 2149 arg_end = arg; 2150 } 2151 name[len] = c1; 2152 vim_free(tofree); 2153 } 2154 } 2155 } 2156 2157 /* 2158 * ":let &option = expr": Set option value. 2159 * ":let &l:option = expr": Set local option value. 2160 * ":let &g:option = expr": Set global option value. 2161 */ 2162 else if (*arg == '&') 2163 { 2164 /* Find the end of the name. */ 2165 p = find_option_end(&arg, &opt_flags); 2166 if (p == NULL || (endchars != NULL 2167 && vim_strchr(endchars, *skipwhite(p)) == NULL)) 2168 EMSG(_(e_letunexp)); 2169 else 2170 { 2171 long n; 2172 int opt_type; 2173 long numval; 2174 char_u *stringval = NULL; 2175 char_u *s; 2176 2177 c1 = *p; 2178 *p = NUL; 2179 2180 n = get_tv_number(tv); 2181 s = get_tv_string_chk(tv); /* != NULL if number or string */ 2182 if (s != NULL && op != NULL && *op != '=') 2183 { 2184 opt_type = get_option_value(arg, &numval, 2185 &stringval, opt_flags); 2186 if ((opt_type == 1 && *op == '.') 2187 || (opt_type == 0 && *op != '.')) 2188 EMSG2(_(e_letwrong), op); 2189 else 2190 { 2191 if (opt_type == 1) /* number */ 2192 { 2193 if (*op == '+') 2194 n = numval + n; 2195 else 2196 n = numval - n; 2197 } 2198 else if (opt_type == 0 && stringval != NULL) /* string */ 2199 { 2200 s = concat_str(stringval, s); 2201 vim_free(stringval); 2202 stringval = s; 2203 } 2204 } 2205 } 2206 if (s != NULL) 2207 { 2208 set_option_value(arg, n, s, opt_flags); 2209 arg_end = p; 2210 } 2211 *p = c1; 2212 vim_free(stringval); 2213 } 2214 } 2215 2216 /* 2217 * ":let @r = expr": Set register contents. 2218 */ 2219 else if (*arg == '@') 2220 { 2221 ++arg; 2222 if (op != NULL && (*op == '+' || *op == '-')) 2223 EMSG2(_(e_letwrong), op); 2224 else if (endchars != NULL 2225 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL) 2226 EMSG(_(e_letunexp)); 2227 else 2228 { 2229 char_u *tofree = NULL; 2230 char_u *s; 2231 2232 p = get_tv_string_chk(tv); 2233 if (p != NULL && op != NULL && *op == '.') 2234 { 2235 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE); 2236 if (s != NULL) 2237 { 2238 p = tofree = concat_str(s, p); 2239 vim_free(s); 2240 } 2241 } 2242 if (p != NULL) 2243 { 2244 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE); 2245 arg_end = arg + 1; 2246 } 2247 vim_free(tofree); 2248 } 2249 } 2250 2251 /* 2252 * ":let var = expr": Set internal variable. 2253 * ":let {expr} = expr": Idem, name made with curly braces 2254 */ 2255 else if (eval_isnamec1(*arg) || *arg == '{') 2256 { 2257 lval_T lv; 2258 2259 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START); 2260 if (p != NULL && lv.ll_name != NULL) 2261 { 2262 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL) 2263 EMSG(_(e_letunexp)); 2264 else 2265 { 2266 set_var_lval(&lv, p, tv, copy, op); 2267 arg_end = p; 2268 } 2269 } 2270 clear_lval(&lv); 2271 } 2272 2273 else 2274 EMSG2(_(e_invarg2), arg); 2275 2276 return arg_end; 2277 } 2278 2279 /* 2280 * If "arg" is equal to "b:changedtick" give an error and return TRUE. 2281 */ 2282 static int 2283 check_changedtick(arg) 2284 char_u *arg; 2285 { 2286 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13])) 2287 { 2288 EMSG2(_(e_readonlyvar), arg); 2289 return TRUE; 2290 } 2291 return FALSE; 2292 } 2293 2294 /* 2295 * Get an lval: variable, Dict item or List item that can be assigned a value 2296 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", 2297 * "name.key", "name.key[expr]" etc. 2298 * Indexing only works if "name" is an existing List or Dictionary. 2299 * "name" points to the start of the name. 2300 * If "rettv" is not NULL it points to the value to be assigned. 2301 * "unlet" is TRUE for ":unlet": slightly different behavior when something is 2302 * wrong; must end in space or cmd separator. 2303 * 2304 * Returns a pointer to just after the name, including indexes. 2305 * When an evaluation error occurs "lp->ll_name" is NULL; 2306 * Returns NULL for a parsing error. Still need to free items in "lp"! 2307 */ 2308 static char_u * 2309 get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags) 2310 char_u *name; 2311 typval_T *rettv; 2312 lval_T *lp; 2313 int unlet; 2314 int skip; 2315 int quiet; /* don't give error messages */ 2316 int fne_flags; /* flags for find_name_end() */ 2317 { 2318 char_u *p; 2319 char_u *expr_start, *expr_end; 2320 int cc; 2321 dictitem_T *v; 2322 typval_T var1; 2323 typval_T var2; 2324 int empty1 = FALSE; 2325 listitem_T *ni; 2326 char_u *key = NULL; 2327 int len; 2328 hashtab_T *ht; 2329 2330 /* Clear everything in "lp". */ 2331 vim_memset(lp, 0, sizeof(lval_T)); 2332 2333 if (skip) 2334 { 2335 /* When skipping just find the end of the name. */ 2336 lp->ll_name = name; 2337 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags); 2338 } 2339 2340 /* Find the end of the name. */ 2341 p = find_name_end(name, &expr_start, &expr_end, fne_flags); 2342 if (expr_start != NULL) 2343 { 2344 /* Don't expand the name when we already know there is an error. */ 2345 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p) 2346 && *p != '[' && *p != '.') 2347 { 2348 EMSG(_(e_trailing)); 2349 return NULL; 2350 } 2351 2352 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p); 2353 if (lp->ll_exp_name == NULL) 2354 { 2355 /* Report an invalid expression in braces, unless the 2356 * expression evaluation has been cancelled due to an 2357 * aborting error, an interrupt, or an exception. */ 2358 if (!aborting() && !quiet) 2359 { 2360 emsg_severe = TRUE; 2361 EMSG2(_(e_invarg2), name); 2362 return NULL; 2363 } 2364 } 2365 lp->ll_name = lp->ll_exp_name; 2366 } 2367 else 2368 lp->ll_name = name; 2369 2370 /* Without [idx] or .key we are done. */ 2371 if ((*p != '[' && *p != '.') || lp->ll_name == NULL) 2372 return p; 2373 2374 cc = *p; 2375 *p = NUL; 2376 v = find_var(lp->ll_name, &ht); 2377 if (v == NULL && !quiet) 2378 EMSG2(_(e_undefvar), lp->ll_name); 2379 *p = cc; 2380 if (v == NULL) 2381 return NULL; 2382 2383 /* 2384 * Loop until no more [idx] or .key is following. 2385 */ 2386 lp->ll_tv = &v->di_tv; 2387 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT)) 2388 { 2389 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL) 2390 && !(lp->ll_tv->v_type == VAR_DICT 2391 && lp->ll_tv->vval.v_dict != NULL)) 2392 { 2393 if (!quiet) 2394 EMSG(_("E689: Can only index a List or Dictionary")); 2395 return NULL; 2396 } 2397 if (lp->ll_range) 2398 { 2399 if (!quiet) 2400 EMSG(_("E708: [:] must come last")); 2401 return NULL; 2402 } 2403 2404 len = -1; 2405 if (*p == '.') 2406 { 2407 key = p + 1; 2408 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 2409 ; 2410 if (len == 0) 2411 { 2412 if (!quiet) 2413 EMSG(_(e_emptykey)); 2414 return NULL; 2415 } 2416 p = key + len; 2417 } 2418 else 2419 { 2420 /* Get the index [expr] or the first index [expr: ]. */ 2421 p = skipwhite(p + 1); 2422 if (*p == ':') 2423 empty1 = TRUE; 2424 else 2425 { 2426 empty1 = FALSE; 2427 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */ 2428 return NULL; 2429 if (get_tv_string_chk(&var1) == NULL) 2430 { 2431 /* not a number or string */ 2432 clear_tv(&var1); 2433 return NULL; 2434 } 2435 } 2436 2437 /* Optionally get the second index [ :expr]. */ 2438 if (*p == ':') 2439 { 2440 if (lp->ll_tv->v_type == VAR_DICT) 2441 { 2442 if (!quiet) 2443 EMSG(_(e_dictrange)); 2444 if (!empty1) 2445 clear_tv(&var1); 2446 return NULL; 2447 } 2448 if (rettv != NULL && (rettv->v_type != VAR_LIST 2449 || rettv->vval.v_list == NULL)) 2450 { 2451 if (!quiet) 2452 EMSG(_("E709: [:] requires a List value")); 2453 if (!empty1) 2454 clear_tv(&var1); 2455 return NULL; 2456 } 2457 p = skipwhite(p + 1); 2458 if (*p == ']') 2459 lp->ll_empty2 = TRUE; 2460 else 2461 { 2462 lp->ll_empty2 = FALSE; 2463 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */ 2464 { 2465 if (!empty1) 2466 clear_tv(&var1); 2467 return NULL; 2468 } 2469 if (get_tv_string_chk(&var2) == NULL) 2470 { 2471 /* not a number or string */ 2472 if (!empty1) 2473 clear_tv(&var1); 2474 clear_tv(&var2); 2475 return NULL; 2476 } 2477 } 2478 lp->ll_range = TRUE; 2479 } 2480 else 2481 lp->ll_range = FALSE; 2482 2483 if (*p != ']') 2484 { 2485 if (!quiet) 2486 EMSG(_(e_missbrac)); 2487 if (!empty1) 2488 clear_tv(&var1); 2489 if (lp->ll_range && !lp->ll_empty2) 2490 clear_tv(&var2); 2491 return NULL; 2492 } 2493 2494 /* Skip to past ']'. */ 2495 ++p; 2496 } 2497 2498 if (lp->ll_tv->v_type == VAR_DICT) 2499 { 2500 if (len == -1) 2501 { 2502 /* "[key]": get key from "var1" */ 2503 key = get_tv_string(&var1); /* is number or string */ 2504 if (*key == NUL) 2505 { 2506 if (!quiet) 2507 EMSG(_(e_emptykey)); 2508 clear_tv(&var1); 2509 return NULL; 2510 } 2511 } 2512 lp->ll_list = NULL; 2513 lp->ll_dict = lp->ll_tv->vval.v_dict; 2514 lp->ll_di = dict_find(lp->ll_dict, key, len); 2515 if (lp->ll_di == NULL) 2516 { 2517 /* Key does not exist in dict: may need to add it. */ 2518 if (*p == '[' || *p == '.' || unlet) 2519 { 2520 if (!quiet) 2521 EMSG2(_(e_dictkey), key); 2522 if (len == -1) 2523 clear_tv(&var1); 2524 return NULL; 2525 } 2526 if (len == -1) 2527 lp->ll_newkey = vim_strsave(key); 2528 else 2529 lp->ll_newkey = vim_strnsave(key, len); 2530 if (len == -1) 2531 clear_tv(&var1); 2532 if (lp->ll_newkey == NULL) 2533 p = NULL; 2534 break; 2535 } 2536 if (len == -1) 2537 clear_tv(&var1); 2538 lp->ll_tv = &lp->ll_di->di_tv; 2539 } 2540 else 2541 { 2542 /* 2543 * Get the number and item for the only or first index of the List. 2544 */ 2545 if (empty1) 2546 lp->ll_n1 = 0; 2547 else 2548 { 2549 lp->ll_n1 = get_tv_number(&var1); /* is number or string */ 2550 clear_tv(&var1); 2551 } 2552 lp->ll_dict = NULL; 2553 lp->ll_list = lp->ll_tv->vval.v_list; 2554 lp->ll_li = list_find(lp->ll_list, lp->ll_n1); 2555 if (lp->ll_li == NULL) 2556 { 2557 if (!quiet) 2558 EMSGN(_(e_listidx), lp->ll_n1); 2559 if (lp->ll_range && !lp->ll_empty2) 2560 clear_tv(&var2); 2561 return NULL; 2562 } 2563 2564 /* 2565 * May need to find the item or absolute index for the second 2566 * index of a range. 2567 * When no index given: "lp->ll_empty2" is TRUE. 2568 * Otherwise "lp->ll_n2" is set to the second index. 2569 */ 2570 if (lp->ll_range && !lp->ll_empty2) 2571 { 2572 lp->ll_n2 = get_tv_number(&var2); /* is number or string */ 2573 clear_tv(&var2); 2574 if (lp->ll_n2 < 0) 2575 { 2576 ni = list_find(lp->ll_list, lp->ll_n2); 2577 if (ni == NULL) 2578 { 2579 if (!quiet) 2580 EMSGN(_(e_listidx), lp->ll_n2); 2581 return NULL; 2582 } 2583 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni); 2584 } 2585 2586 /* Check that lp->ll_n2 isn't before lp->ll_n1. */ 2587 if (lp->ll_n1 < 0) 2588 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li); 2589 if (lp->ll_n2 < lp->ll_n1) 2590 { 2591 if (!quiet) 2592 EMSGN(_(e_listidx), lp->ll_n2); 2593 return NULL; 2594 } 2595 } 2596 2597 lp->ll_tv = &lp->ll_li->li_tv; 2598 } 2599 } 2600 2601 return p; 2602 } 2603 2604 /* 2605 * Clear lval "lp" that was filled by get_lval(). 2606 */ 2607 static void 2608 clear_lval(lp) 2609 lval_T *lp; 2610 { 2611 vim_free(lp->ll_exp_name); 2612 vim_free(lp->ll_newkey); 2613 } 2614 2615 /* 2616 * Set a variable that was parsed by get_lval() to "rettv". 2617 * "endp" points to just after the parsed name. 2618 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=". 2619 */ 2620 static void 2621 set_var_lval(lp, endp, rettv, copy, op) 2622 lval_T *lp; 2623 char_u *endp; 2624 typval_T *rettv; 2625 int copy; 2626 char_u *op; 2627 { 2628 int cc; 2629 listitem_T *ri; 2630 dictitem_T *di; 2631 2632 if (lp->ll_tv == NULL) 2633 { 2634 if (!check_changedtick(lp->ll_name)) 2635 { 2636 cc = *endp; 2637 *endp = NUL; 2638 if (op != NULL && *op != '=') 2639 { 2640 typval_T tv; 2641 2642 /* handle +=, -= and .= */ 2643 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name), 2644 &tv, TRUE) == OK) 2645 { 2646 if (tv_op(&tv, rettv, op) == OK) 2647 set_var(lp->ll_name, &tv, FALSE); 2648 clear_tv(&tv); 2649 } 2650 } 2651 else 2652 set_var(lp->ll_name, rettv, copy); 2653 *endp = cc; 2654 } 2655 } 2656 else if (tv_check_lock(lp->ll_newkey == NULL 2657 ? lp->ll_tv->v_lock 2658 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name)) 2659 ; 2660 else if (lp->ll_range) 2661 { 2662 /* 2663 * Assign the List values to the list items. 2664 */ 2665 for (ri = rettv->vval.v_list->lv_first; ri != NULL; ) 2666 { 2667 if (op != NULL && *op != '=') 2668 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op); 2669 else 2670 { 2671 clear_tv(&lp->ll_li->li_tv); 2672 copy_tv(&ri->li_tv, &lp->ll_li->li_tv); 2673 } 2674 ri = ri->li_next; 2675 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1)) 2676 break; 2677 if (lp->ll_li->li_next == NULL) 2678 { 2679 /* Need to add an empty item. */ 2680 if (list_append_number(lp->ll_list, 0) == FAIL) 2681 { 2682 ri = NULL; 2683 break; 2684 } 2685 } 2686 lp->ll_li = lp->ll_li->li_next; 2687 ++lp->ll_n1; 2688 } 2689 if (ri != NULL) 2690 EMSG(_("E710: List value has more items than target")); 2691 else if (lp->ll_empty2 2692 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL) 2693 : lp->ll_n1 != lp->ll_n2) 2694 EMSG(_("E711: List value has not enough items")); 2695 } 2696 else 2697 { 2698 /* 2699 * Assign to a List or Dictionary item. 2700 */ 2701 if (lp->ll_newkey != NULL) 2702 { 2703 if (op != NULL && *op != '=') 2704 { 2705 EMSG2(_(e_letwrong), op); 2706 return; 2707 } 2708 2709 /* Need to add an item to the Dictionary. */ 2710 di = dictitem_alloc(lp->ll_newkey); 2711 if (di == NULL) 2712 return; 2713 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL) 2714 { 2715 vim_free(di); 2716 return; 2717 } 2718 lp->ll_tv = &di->di_tv; 2719 } 2720 else if (op != NULL && *op != '=') 2721 { 2722 tv_op(lp->ll_tv, rettv, op); 2723 return; 2724 } 2725 else 2726 clear_tv(lp->ll_tv); 2727 2728 /* 2729 * Assign the value to the variable or list item. 2730 */ 2731 if (copy) 2732 copy_tv(rettv, lp->ll_tv); 2733 else 2734 { 2735 *lp->ll_tv = *rettv; 2736 lp->ll_tv->v_lock = 0; 2737 init_tv(rettv); 2738 } 2739 } 2740 } 2741 2742 /* 2743 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2" 2744 * Returns OK or FAIL. 2745 */ 2746 static int 2747 tv_op(tv1, tv2, op) 2748 typval_T *tv1; 2749 typval_T *tv2; 2750 char_u *op; 2751 { 2752 long n; 2753 char_u numbuf[NUMBUFLEN]; 2754 char_u *s; 2755 2756 /* Can't do anything with a Funcref or a Dict on the right. */ 2757 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT) 2758 { 2759 switch (tv1->v_type) 2760 { 2761 case VAR_DICT: 2762 case VAR_FUNC: 2763 break; 2764 2765 case VAR_LIST: 2766 if (*op != '+' || tv2->v_type != VAR_LIST) 2767 break; 2768 /* List += List */ 2769 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL) 2770 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL); 2771 return OK; 2772 2773 case VAR_NUMBER: 2774 case VAR_STRING: 2775 if (tv2->v_type == VAR_LIST) 2776 break; 2777 if (*op == '+' || *op == '-') 2778 { 2779 /* nr += nr or nr -= nr*/ 2780 n = get_tv_number(tv1); 2781 if (*op == '+') 2782 n += get_tv_number(tv2); 2783 else 2784 n -= get_tv_number(tv2); 2785 clear_tv(tv1); 2786 tv1->v_type = VAR_NUMBER; 2787 tv1->vval.v_number = n; 2788 } 2789 else 2790 { 2791 /* str .= str */ 2792 s = get_tv_string(tv1); 2793 s = concat_str(s, get_tv_string_buf(tv2, numbuf)); 2794 clear_tv(tv1); 2795 tv1->v_type = VAR_STRING; 2796 tv1->vval.v_string = s; 2797 } 2798 return OK; 2799 } 2800 } 2801 2802 EMSG2(_(e_letwrong), op); 2803 return FAIL; 2804 } 2805 2806 /* 2807 * Add a watcher to a list. 2808 */ 2809 static void 2810 list_add_watch(l, lw) 2811 list_T *l; 2812 listwatch_T *lw; 2813 { 2814 lw->lw_next = l->lv_watch; 2815 l->lv_watch = lw; 2816 } 2817 2818 /* 2819 * Remove a watcher from a list. 2820 * No warning when it isn't found... 2821 */ 2822 static void 2823 list_rem_watch(l, lwrem) 2824 list_T *l; 2825 listwatch_T *lwrem; 2826 { 2827 listwatch_T *lw, **lwp; 2828 2829 lwp = &l->lv_watch; 2830 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next) 2831 { 2832 if (lw == lwrem) 2833 { 2834 *lwp = lw->lw_next; 2835 break; 2836 } 2837 lwp = &lw->lw_next; 2838 } 2839 } 2840 2841 /* 2842 * Just before removing an item from a list: advance watchers to the next 2843 * item. 2844 */ 2845 static void 2846 list_fix_watch(l, item) 2847 list_T *l; 2848 listitem_T *item; 2849 { 2850 listwatch_T *lw; 2851 2852 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next) 2853 if (lw->lw_item == item) 2854 lw->lw_item = item->li_next; 2855 } 2856 2857 /* 2858 * Evaluate the expression used in a ":for var in expr" command. 2859 * "arg" points to "var". 2860 * Set "*errp" to TRUE for an error, FALSE otherwise; 2861 * Return a pointer that holds the info. Null when there is an error. 2862 */ 2863 void * 2864 eval_for_line(arg, errp, nextcmdp, skip) 2865 char_u *arg; 2866 int *errp; 2867 char_u **nextcmdp; 2868 int skip; 2869 { 2870 forinfo_T *fi; 2871 char_u *expr; 2872 typval_T tv; 2873 list_T *l; 2874 2875 *errp = TRUE; /* default: there is an error */ 2876 2877 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T)); 2878 if (fi == NULL) 2879 return NULL; 2880 2881 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon); 2882 if (expr == NULL) 2883 return fi; 2884 2885 expr = skipwhite(expr); 2886 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2])) 2887 { 2888 EMSG(_("E690: Missing \"in\" after :for")); 2889 return fi; 2890 } 2891 2892 if (skip) 2893 ++emsg_skip; 2894 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK) 2895 { 2896 *errp = FALSE; 2897 if (!skip) 2898 { 2899 l = tv.vval.v_list; 2900 if (tv.v_type != VAR_LIST || l == NULL) 2901 { 2902 EMSG(_(e_listreq)); 2903 clear_tv(&tv); 2904 } 2905 else 2906 { 2907 /* No need to increment the refcount, it's already set for the 2908 * list being used in "tv". */ 2909 fi->fi_list = l; 2910 list_add_watch(l, &fi->fi_lw); 2911 fi->fi_lw.lw_item = l->lv_first; 2912 } 2913 } 2914 } 2915 if (skip) 2916 --emsg_skip; 2917 2918 return fi; 2919 } 2920 2921 /* 2922 * Use the first item in a ":for" list. Advance to the next. 2923 * Assign the values to the variable (list). "arg" points to the first one. 2924 * Return TRUE when a valid item was found, FALSE when at end of list or 2925 * something wrong. 2926 */ 2927 int 2928 next_for_item(fi_void, arg) 2929 void *fi_void; 2930 char_u *arg; 2931 { 2932 forinfo_T *fi = (forinfo_T *)fi_void; 2933 int result; 2934 listitem_T *item; 2935 2936 item = fi->fi_lw.lw_item; 2937 if (item == NULL) 2938 result = FALSE; 2939 else 2940 { 2941 fi->fi_lw.lw_item = item->li_next; 2942 result = (ex_let_vars(arg, &item->li_tv, TRUE, 2943 fi->fi_semicolon, fi->fi_varcount, NULL) == OK); 2944 } 2945 return result; 2946 } 2947 2948 /* 2949 * Free the structure used to store info used by ":for". 2950 */ 2951 void 2952 free_for_info(fi_void) 2953 void *fi_void; 2954 { 2955 forinfo_T *fi = (forinfo_T *)fi_void; 2956 2957 if (fi != NULL && fi->fi_list != NULL) 2958 { 2959 list_rem_watch(fi->fi_list, &fi->fi_lw); 2960 list_unref(fi->fi_list); 2961 } 2962 vim_free(fi); 2963 } 2964 2965 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 2966 2967 void 2968 set_context_for_expression(xp, arg, cmdidx) 2969 expand_T *xp; 2970 char_u *arg; 2971 cmdidx_T cmdidx; 2972 { 2973 int got_eq = FALSE; 2974 int c; 2975 char_u *p; 2976 2977 if (cmdidx == CMD_let) 2978 { 2979 xp->xp_context = EXPAND_USER_VARS; 2980 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL) 2981 { 2982 /* ":let var1 var2 ...": find last space. */ 2983 for (p = arg + STRLEN(arg); p >= arg; ) 2984 { 2985 xp->xp_pattern = p; 2986 mb_ptr_back(arg, p); 2987 if (vim_iswhite(*p)) 2988 break; 2989 } 2990 return; 2991 } 2992 } 2993 else 2994 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS 2995 : EXPAND_EXPRESSION; 2996 while ((xp->xp_pattern = vim_strpbrk(arg, 2997 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL) 2998 { 2999 c = *xp->xp_pattern; 3000 if (c == '&') 3001 { 3002 c = xp->xp_pattern[1]; 3003 if (c == '&') 3004 { 3005 ++xp->xp_pattern; 3006 xp->xp_context = cmdidx != CMD_let || got_eq 3007 ? EXPAND_EXPRESSION : EXPAND_NOTHING; 3008 } 3009 else if (c != ' ') 3010 { 3011 xp->xp_context = EXPAND_SETTINGS; 3012 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':') 3013 xp->xp_pattern += 2; 3014 3015 } 3016 } 3017 else if (c == '$') 3018 { 3019 /* environment variable */ 3020 xp->xp_context = EXPAND_ENV_VARS; 3021 } 3022 else if (c == '=') 3023 { 3024 got_eq = TRUE; 3025 xp->xp_context = EXPAND_EXPRESSION; 3026 } 3027 else if (c == '<' 3028 && xp->xp_context == EXPAND_FUNCTIONS 3029 && vim_strchr(xp->xp_pattern, '(') == NULL) 3030 { 3031 /* Function name can start with "<SNR>" */ 3032 break; 3033 } 3034 else if (cmdidx != CMD_let || got_eq) 3035 { 3036 if (c == '"') /* string */ 3037 { 3038 while ((c = *++xp->xp_pattern) != NUL && c != '"') 3039 if (c == '\\' && xp->xp_pattern[1] != NUL) 3040 ++xp->xp_pattern; 3041 xp->xp_context = EXPAND_NOTHING; 3042 } 3043 else if (c == '\'') /* literal string */ 3044 { 3045 /* Trick: '' is like stopping and starting a literal string. */ 3046 while ((c = *++xp->xp_pattern) != NUL && c != '\'') 3047 /* skip */ ; 3048 xp->xp_context = EXPAND_NOTHING; 3049 } 3050 else if (c == '|') 3051 { 3052 if (xp->xp_pattern[1] == '|') 3053 { 3054 ++xp->xp_pattern; 3055 xp->xp_context = EXPAND_EXPRESSION; 3056 } 3057 else 3058 xp->xp_context = EXPAND_COMMANDS; 3059 } 3060 else 3061 xp->xp_context = EXPAND_EXPRESSION; 3062 } 3063 else 3064 /* Doesn't look like something valid, expand as an expression 3065 * anyway. */ 3066 xp->xp_context = EXPAND_EXPRESSION; 3067 arg = xp->xp_pattern; 3068 if (*arg != NUL) 3069 while ((c = *++arg) != NUL && (c == ' ' || c == '\t')) 3070 /* skip */ ; 3071 } 3072 xp->xp_pattern = arg; 3073 } 3074 3075 #endif /* FEAT_CMDL_COMPL */ 3076 3077 /* 3078 * ":1,25call func(arg1, arg2)" function call. 3079 */ 3080 void 3081 ex_call(eap) 3082 exarg_T *eap; 3083 { 3084 char_u *arg = eap->arg; 3085 char_u *startarg; 3086 char_u *name; 3087 char_u *tofree; 3088 int len; 3089 typval_T rettv; 3090 linenr_T lnum; 3091 int doesrange; 3092 int failed = FALSE; 3093 funcdict_T fudi; 3094 3095 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi); 3096 vim_free(fudi.fd_newkey); 3097 if (tofree == NULL) 3098 return; 3099 3100 /* Increase refcount on dictionary, it could get deleted when evaluating 3101 * the arguments. */ 3102 if (fudi.fd_dict != NULL) 3103 ++fudi.fd_dict->dv_refcount; 3104 3105 /* If it is the name of a variable of type VAR_FUNC use its contents. */ 3106 len = STRLEN(tofree); 3107 name = deref_func_name(tofree, &len); 3108 3109 /* Skip white space to allow ":call func ()". Not good, but required for 3110 * backward compatibility. */ 3111 startarg = skipwhite(arg); 3112 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 3113 3114 if (*startarg != '(') 3115 { 3116 EMSG2(_("E107: Missing braces: %s"), eap->arg); 3117 goto end; 3118 } 3119 3120 /* 3121 * When skipping, evaluate the function once, to find the end of the 3122 * arguments. 3123 * When the function takes a range, this is discovered after the first 3124 * call, and the loop is broken. 3125 */ 3126 if (eap->skip) 3127 { 3128 ++emsg_skip; 3129 lnum = eap->line2; /* do it once, also with an invalid range */ 3130 } 3131 else 3132 lnum = eap->line1; 3133 for ( ; lnum <= eap->line2; ++lnum) 3134 { 3135 if (!eap->skip && eap->addr_count > 0) 3136 { 3137 curwin->w_cursor.lnum = lnum; 3138 curwin->w_cursor.col = 0; 3139 } 3140 arg = startarg; 3141 if (get_func_tv(name, STRLEN(name), &rettv, &arg, 3142 eap->line1, eap->line2, &doesrange, 3143 !eap->skip, fudi.fd_dict) == FAIL) 3144 { 3145 failed = TRUE; 3146 break; 3147 } 3148 clear_tv(&rettv); 3149 if (doesrange || eap->skip) 3150 break; 3151 /* Stop when immediately aborting on error, or when an interrupt 3152 * occurred or an exception was thrown but not caught. 3153 * get_func_tv() returned OK, so that the check for trailing 3154 * characters below is executed. */ 3155 if (aborting()) 3156 break; 3157 } 3158 if (eap->skip) 3159 --emsg_skip; 3160 3161 if (!failed) 3162 { 3163 /* Check for trailing illegal characters and a following command. */ 3164 if (!ends_excmd(*arg)) 3165 { 3166 emsg_severe = TRUE; 3167 EMSG(_(e_trailing)); 3168 } 3169 else 3170 eap->nextcmd = check_nextcmd(arg); 3171 } 3172 3173 end: 3174 dict_unref(fudi.fd_dict); 3175 vim_free(tofree); 3176 } 3177 3178 /* 3179 * ":unlet[!] var1 ... " command. 3180 */ 3181 void 3182 ex_unlet(eap) 3183 exarg_T *eap; 3184 { 3185 ex_unletlock(eap, eap->arg, 0); 3186 } 3187 3188 /* 3189 * ":lockvar" and ":unlockvar" commands 3190 */ 3191 void 3192 ex_lockvar(eap) 3193 exarg_T *eap; 3194 { 3195 char_u *arg = eap->arg; 3196 int deep = 2; 3197 3198 if (eap->forceit) 3199 deep = -1; 3200 else if (vim_isdigit(*arg)) 3201 { 3202 deep = getdigits(&arg); 3203 arg = skipwhite(arg); 3204 } 3205 3206 ex_unletlock(eap, arg, deep); 3207 } 3208 3209 /* 3210 * ":unlet", ":lockvar" and ":unlockvar" are quite similar. 3211 */ 3212 static void 3213 ex_unletlock(eap, argstart, deep) 3214 exarg_T *eap; 3215 char_u *argstart; 3216 int deep; 3217 { 3218 char_u *arg = argstart; 3219 char_u *name_end; 3220 int error = FALSE; 3221 lval_T lv; 3222 3223 do 3224 { 3225 /* Parse the name and find the end. */ 3226 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE, 3227 FNE_CHECK_START); 3228 if (lv.ll_name == NULL) 3229 error = TRUE; /* error but continue parsing */ 3230 if (name_end == NULL || (!vim_iswhite(*name_end) 3231 && !ends_excmd(*name_end))) 3232 { 3233 if (name_end != NULL) 3234 { 3235 emsg_severe = TRUE; 3236 EMSG(_(e_trailing)); 3237 } 3238 if (!(eap->skip || error)) 3239 clear_lval(&lv); 3240 break; 3241 } 3242 3243 if (!error && !eap->skip) 3244 { 3245 if (eap->cmdidx == CMD_unlet) 3246 { 3247 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL) 3248 error = TRUE; 3249 } 3250 else 3251 { 3252 if (do_lock_var(&lv, name_end, deep, 3253 eap->cmdidx == CMD_lockvar) == FAIL) 3254 error = TRUE; 3255 } 3256 } 3257 3258 if (!eap->skip) 3259 clear_lval(&lv); 3260 3261 arg = skipwhite(name_end); 3262 } while (!ends_excmd(*arg)); 3263 3264 eap->nextcmd = check_nextcmd(arg); 3265 } 3266 3267 static int 3268 do_unlet_var(lp, name_end, forceit) 3269 lval_T *lp; 3270 char_u *name_end; 3271 int forceit; 3272 { 3273 int ret = OK; 3274 int cc; 3275 3276 if (lp->ll_tv == NULL) 3277 { 3278 cc = *name_end; 3279 *name_end = NUL; 3280 3281 /* Normal name or expanded name. */ 3282 if (check_changedtick(lp->ll_name)) 3283 ret = FAIL; 3284 else if (do_unlet(lp->ll_name, forceit) == FAIL) 3285 ret = FAIL; 3286 *name_end = cc; 3287 } 3288 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name)) 3289 return FAIL; 3290 else if (lp->ll_range) 3291 { 3292 listitem_T *li; 3293 3294 /* Delete a range of List items. */ 3295 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) 3296 { 3297 li = lp->ll_li->li_next; 3298 listitem_remove(lp->ll_list, lp->ll_li); 3299 lp->ll_li = li; 3300 ++lp->ll_n1; 3301 } 3302 } 3303 else 3304 { 3305 if (lp->ll_list != NULL) 3306 /* unlet a List item. */ 3307 listitem_remove(lp->ll_list, lp->ll_li); 3308 else 3309 /* unlet a Dictionary item. */ 3310 dictitem_remove(lp->ll_dict, lp->ll_di); 3311 } 3312 3313 return ret; 3314 } 3315 3316 /* 3317 * "unlet" a variable. Return OK if it existed, FAIL if not. 3318 * When "forceit" is TRUE don't complain if the variable doesn't exist. 3319 */ 3320 int 3321 do_unlet(name, forceit) 3322 char_u *name; 3323 int forceit; 3324 { 3325 hashtab_T *ht; 3326 hashitem_T *hi; 3327 char_u *varname; 3328 3329 ht = find_var_ht(name, &varname); 3330 if (ht != NULL && *varname != NUL) 3331 { 3332 hi = hash_find(ht, varname); 3333 if (!HASHITEM_EMPTY(hi)) 3334 { 3335 if (var_check_ro(HI2DI(hi)->di_flags, name)) 3336 return FAIL; 3337 delete_var(ht, hi); 3338 return OK; 3339 } 3340 } 3341 if (forceit) 3342 return OK; 3343 EMSG2(_("E108: No such variable: \"%s\""), name); 3344 return FAIL; 3345 } 3346 3347 /* 3348 * Lock or unlock variable indicated by "lp". 3349 * "deep" is the levels to go (-1 for unlimited); 3350 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar". 3351 */ 3352 static int 3353 do_lock_var(lp, name_end, deep, lock) 3354 lval_T *lp; 3355 char_u *name_end; 3356 int deep; 3357 int lock; 3358 { 3359 int ret = OK; 3360 int cc; 3361 dictitem_T *di; 3362 3363 if (deep == 0) /* nothing to do */ 3364 return OK; 3365 3366 if (lp->ll_tv == NULL) 3367 { 3368 cc = *name_end; 3369 *name_end = NUL; 3370 3371 /* Normal name or expanded name. */ 3372 if (check_changedtick(lp->ll_name)) 3373 ret = FAIL; 3374 else 3375 { 3376 di = find_var(lp->ll_name, NULL); 3377 if (di == NULL) 3378 ret = FAIL; 3379 else 3380 { 3381 if (lock) 3382 di->di_flags |= DI_FLAGS_LOCK; 3383 else 3384 di->di_flags &= ~DI_FLAGS_LOCK; 3385 item_lock(&di->di_tv, deep, lock); 3386 } 3387 } 3388 *name_end = cc; 3389 } 3390 else if (lp->ll_range) 3391 { 3392 listitem_T *li = lp->ll_li; 3393 3394 /* (un)lock a range of List items. */ 3395 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) 3396 { 3397 item_lock(&li->li_tv, deep, lock); 3398 li = li->li_next; 3399 ++lp->ll_n1; 3400 } 3401 } 3402 else if (lp->ll_list != NULL) 3403 /* (un)lock a List item. */ 3404 item_lock(&lp->ll_li->li_tv, deep, lock); 3405 else 3406 /* un(lock) a Dictionary item. */ 3407 item_lock(&lp->ll_di->di_tv, deep, lock); 3408 3409 return ret; 3410 } 3411 3412 /* 3413 * Lock or unlock an item. "deep" is nr of levels to go. 3414 */ 3415 static void 3416 item_lock(tv, deep, lock) 3417 typval_T *tv; 3418 int deep; 3419 int lock; 3420 { 3421 static int recurse = 0; 3422 list_T *l; 3423 listitem_T *li; 3424 dict_T *d; 3425 hashitem_T *hi; 3426 int todo; 3427 3428 if (recurse >= DICT_MAXNEST) 3429 { 3430 EMSG(_("E743: variable nested too deep for (un)lock")); 3431 return; 3432 } 3433 if (deep == 0) 3434 return; 3435 ++recurse; 3436 3437 /* lock/unlock the item itself */ 3438 if (lock) 3439 tv->v_lock |= VAR_LOCKED; 3440 else 3441 tv->v_lock &= ~VAR_LOCKED; 3442 3443 switch (tv->v_type) 3444 { 3445 case VAR_LIST: 3446 if ((l = tv->vval.v_list) != NULL) 3447 { 3448 if (lock) 3449 l->lv_lock |= VAR_LOCKED; 3450 else 3451 l->lv_lock &= ~VAR_LOCKED; 3452 if (deep < 0 || deep > 1) 3453 /* recursive: lock/unlock the items the List contains */ 3454 for (li = l->lv_first; li != NULL; li = li->li_next) 3455 item_lock(&li->li_tv, deep - 1, lock); 3456 } 3457 break; 3458 case VAR_DICT: 3459 if ((d = tv->vval.v_dict) != NULL) 3460 { 3461 if (lock) 3462 d->dv_lock |= VAR_LOCKED; 3463 else 3464 d->dv_lock &= ~VAR_LOCKED; 3465 if (deep < 0 || deep > 1) 3466 { 3467 /* recursive: lock/unlock the items the List contains */ 3468 todo = d->dv_hashtab.ht_used; 3469 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 3470 { 3471 if (!HASHITEM_EMPTY(hi)) 3472 { 3473 --todo; 3474 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock); 3475 } 3476 } 3477 } 3478 } 3479 } 3480 --recurse; 3481 } 3482 3483 /* 3484 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or 3485 * it refers to a List or Dictionary that is locked. 3486 */ 3487 static int 3488 tv_islocked(tv) 3489 typval_T *tv; 3490 { 3491 return (tv->v_lock & VAR_LOCKED) 3492 || (tv->v_type == VAR_LIST 3493 && tv->vval.v_list != NULL 3494 && (tv->vval.v_list->lv_lock & VAR_LOCKED)) 3495 || (tv->v_type == VAR_DICT 3496 && tv->vval.v_dict != NULL 3497 && (tv->vval.v_dict->dv_lock & VAR_LOCKED)); 3498 } 3499 3500 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO) 3501 /* 3502 * Delete all "menutrans_" variables. 3503 */ 3504 void 3505 del_menutrans_vars() 3506 { 3507 hashitem_T *hi; 3508 int todo; 3509 3510 hash_lock(&globvarht); 3511 todo = globvarht.ht_used; 3512 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi) 3513 { 3514 if (!HASHITEM_EMPTY(hi)) 3515 { 3516 --todo; 3517 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0) 3518 delete_var(&globvarht, hi); 3519 } 3520 } 3521 hash_unlock(&globvarht); 3522 } 3523 #endif 3524 3525 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 3526 3527 /* 3528 * Local string buffer for the next two functions to store a variable name 3529 * with its prefix. Allocated in cat_prefix_varname(), freed later in 3530 * get_user_var_name(). 3531 */ 3532 3533 static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name)); 3534 3535 static char_u *varnamebuf = NULL; 3536 static int varnamebuflen = 0; 3537 3538 /* 3539 * Function to concatenate a prefix and a variable name. 3540 */ 3541 static char_u * 3542 cat_prefix_varname(prefix, name) 3543 int prefix; 3544 char_u *name; 3545 { 3546 int len; 3547 3548 len = (int)STRLEN(name) + 3; 3549 if (len > varnamebuflen) 3550 { 3551 vim_free(varnamebuf); 3552 len += 10; /* some additional space */ 3553 varnamebuf = alloc(len); 3554 if (varnamebuf == NULL) 3555 { 3556 varnamebuflen = 0; 3557 return NULL; 3558 } 3559 varnamebuflen = len; 3560 } 3561 *varnamebuf = prefix; 3562 varnamebuf[1] = ':'; 3563 STRCPY(varnamebuf + 2, name); 3564 return varnamebuf; 3565 } 3566 3567 /* 3568 * Function given to ExpandGeneric() to obtain the list of user defined 3569 * (global/buffer/window/built-in) variable names. 3570 */ 3571 /*ARGSUSED*/ 3572 char_u * 3573 get_user_var_name(xp, idx) 3574 expand_T *xp; 3575 int idx; 3576 { 3577 static long_u gdone; 3578 static long_u bdone; 3579 static long_u wdone; 3580 static int vidx; 3581 static hashitem_T *hi; 3582 hashtab_T *ht; 3583 3584 if (idx == 0) 3585 gdone = bdone = wdone = vidx = 0; 3586 3587 /* Global variables */ 3588 if (gdone < globvarht.ht_used) 3589 { 3590 if (gdone++ == 0) 3591 hi = globvarht.ht_array; 3592 else 3593 ++hi; 3594 while (HASHITEM_EMPTY(hi)) 3595 ++hi; 3596 if (STRNCMP("g:", xp->xp_pattern, 2) == 0) 3597 return cat_prefix_varname('g', hi->hi_key); 3598 return hi->hi_key; 3599 } 3600 3601 /* b: variables */ 3602 ht = &curbuf->b_vars.dv_hashtab; 3603 if (bdone < ht->ht_used) 3604 { 3605 if (bdone++ == 0) 3606 hi = ht->ht_array; 3607 else 3608 ++hi; 3609 while (HASHITEM_EMPTY(hi)) 3610 ++hi; 3611 return cat_prefix_varname('b', hi->hi_key); 3612 } 3613 if (bdone == ht->ht_used) 3614 { 3615 ++bdone; 3616 return (char_u *)"b:changedtick"; 3617 } 3618 3619 /* w: variables */ 3620 ht = &curwin->w_vars.dv_hashtab; 3621 if (wdone < ht->ht_used) 3622 { 3623 if (wdone++ == 0) 3624 hi = ht->ht_array; 3625 else 3626 ++hi; 3627 while (HASHITEM_EMPTY(hi)) 3628 ++hi; 3629 return cat_prefix_varname('w', hi->hi_key); 3630 } 3631 3632 /* v: variables */ 3633 if (vidx < VV_LEN) 3634 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name); 3635 3636 vim_free(varnamebuf); 3637 varnamebuf = NULL; 3638 varnamebuflen = 0; 3639 return NULL; 3640 } 3641 3642 #endif /* FEAT_CMDL_COMPL */ 3643 3644 /* 3645 * types for expressions. 3646 */ 3647 typedef enum 3648 { 3649 TYPE_UNKNOWN = 0 3650 , TYPE_EQUAL /* == */ 3651 , TYPE_NEQUAL /* != */ 3652 , TYPE_GREATER /* > */ 3653 , TYPE_GEQUAL /* >= */ 3654 , TYPE_SMALLER /* < */ 3655 , TYPE_SEQUAL /* <= */ 3656 , TYPE_MATCH /* =~ */ 3657 , TYPE_NOMATCH /* !~ */ 3658 } exptype_T; 3659 3660 /* 3661 * The "evaluate" argument: When FALSE, the argument is only parsed but not 3662 * executed. The function may return OK, but the rettv will be of type 3663 * VAR_UNKNOWN. The function still returns FAIL for a syntax error. 3664 */ 3665 3666 /* 3667 * Handle zero level expression. 3668 * This calls eval1() and handles error message and nextcmd. 3669 * Put the result in "rettv" when returning OK and "evaluate" is TRUE. 3670 * Note: "rettv.v_lock" is not set. 3671 * Return OK or FAIL. 3672 */ 3673 static int 3674 eval0(arg, rettv, nextcmd, evaluate) 3675 char_u *arg; 3676 typval_T *rettv; 3677 char_u **nextcmd; 3678 int evaluate; 3679 { 3680 int ret; 3681 char_u *p; 3682 3683 p = skipwhite(arg); 3684 ret = eval1(&p, rettv, evaluate); 3685 if (ret == FAIL || !ends_excmd(*p)) 3686 { 3687 if (ret != FAIL) 3688 clear_tv(rettv); 3689 /* 3690 * Report the invalid expression unless the expression evaluation has 3691 * been cancelled due to an aborting error, an interrupt, or an 3692 * exception. 3693 */ 3694 if (!aborting()) 3695 EMSG2(_(e_invexpr2), arg); 3696 ret = FAIL; 3697 } 3698 if (nextcmd != NULL) 3699 *nextcmd = check_nextcmd(p); 3700 3701 return ret; 3702 } 3703 3704 /* 3705 * Handle top level expression: 3706 * expr1 ? expr0 : expr0 3707 * 3708 * "arg" must point to the first non-white of the expression. 3709 * "arg" is advanced to the next non-white after the recognized expression. 3710 * 3711 * Note: "rettv.v_lock" is not set. 3712 * 3713 * Return OK or FAIL. 3714 */ 3715 static int 3716 eval1(arg, rettv, evaluate) 3717 char_u **arg; 3718 typval_T *rettv; 3719 int evaluate; 3720 { 3721 int result; 3722 typval_T var2; 3723 3724 /* 3725 * Get the first variable. 3726 */ 3727 if (eval2(arg, rettv, evaluate) == FAIL) 3728 return FAIL; 3729 3730 if ((*arg)[0] == '?') 3731 { 3732 result = FALSE; 3733 if (evaluate) 3734 { 3735 int error = FALSE; 3736 3737 if (get_tv_number_chk(rettv, &error) != 0) 3738 result = TRUE; 3739 clear_tv(rettv); 3740 if (error) 3741 return FAIL; 3742 } 3743 3744 /* 3745 * Get the second variable. 3746 */ 3747 *arg = skipwhite(*arg + 1); 3748 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */ 3749 return FAIL; 3750 3751 /* 3752 * Check for the ":". 3753 */ 3754 if ((*arg)[0] != ':') 3755 { 3756 EMSG(_("E109: Missing ':' after '?'")); 3757 if (evaluate && result) 3758 clear_tv(rettv); 3759 return FAIL; 3760 } 3761 3762 /* 3763 * Get the third variable. 3764 */ 3765 *arg = skipwhite(*arg + 1); 3766 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */ 3767 { 3768 if (evaluate && result) 3769 clear_tv(rettv); 3770 return FAIL; 3771 } 3772 if (evaluate && !result) 3773 *rettv = var2; 3774 } 3775 3776 return OK; 3777 } 3778 3779 /* 3780 * Handle first level expression: 3781 * expr2 || expr2 || expr2 logical OR 3782 * 3783 * "arg" must point to the first non-white of the expression. 3784 * "arg" is advanced to the next non-white after the recognized expression. 3785 * 3786 * Return OK or FAIL. 3787 */ 3788 static int 3789 eval2(arg, rettv, evaluate) 3790 char_u **arg; 3791 typval_T *rettv; 3792 int evaluate; 3793 { 3794 typval_T var2; 3795 long result; 3796 int first; 3797 int error = FALSE; 3798 3799 /* 3800 * Get the first variable. 3801 */ 3802 if (eval3(arg, rettv, evaluate) == FAIL) 3803 return FAIL; 3804 3805 /* 3806 * Repeat until there is no following "||". 3807 */ 3808 first = TRUE; 3809 result = FALSE; 3810 while ((*arg)[0] == '|' && (*arg)[1] == '|') 3811 { 3812 if (evaluate && first) 3813 { 3814 if (get_tv_number_chk(rettv, &error) != 0) 3815 result = TRUE; 3816 clear_tv(rettv); 3817 if (error) 3818 return FAIL; 3819 first = FALSE; 3820 } 3821 3822 /* 3823 * Get the second variable. 3824 */ 3825 *arg = skipwhite(*arg + 2); 3826 if (eval3(arg, &var2, evaluate && !result) == FAIL) 3827 return FAIL; 3828 3829 /* 3830 * Compute the result. 3831 */ 3832 if (evaluate && !result) 3833 { 3834 if (get_tv_number_chk(&var2, &error) != 0) 3835 result = TRUE; 3836 clear_tv(&var2); 3837 if (error) 3838 return FAIL; 3839 } 3840 if (evaluate) 3841 { 3842 rettv->v_type = VAR_NUMBER; 3843 rettv->vval.v_number = result; 3844 } 3845 } 3846 3847 return OK; 3848 } 3849 3850 /* 3851 * Handle second level expression: 3852 * expr3 && expr3 && expr3 logical AND 3853 * 3854 * "arg" must point to the first non-white of the expression. 3855 * "arg" is advanced to the next non-white after the recognized expression. 3856 * 3857 * Return OK or FAIL. 3858 */ 3859 static int 3860 eval3(arg, rettv, evaluate) 3861 char_u **arg; 3862 typval_T *rettv; 3863 int evaluate; 3864 { 3865 typval_T var2; 3866 long result; 3867 int first; 3868 int error = FALSE; 3869 3870 /* 3871 * Get the first variable. 3872 */ 3873 if (eval4(arg, rettv, evaluate) == FAIL) 3874 return FAIL; 3875 3876 /* 3877 * Repeat until there is no following "&&". 3878 */ 3879 first = TRUE; 3880 result = TRUE; 3881 while ((*arg)[0] == '&' && (*arg)[1] == '&') 3882 { 3883 if (evaluate && first) 3884 { 3885 if (get_tv_number_chk(rettv, &error) == 0) 3886 result = FALSE; 3887 clear_tv(rettv); 3888 if (error) 3889 return FAIL; 3890 first = FALSE; 3891 } 3892 3893 /* 3894 * Get the second variable. 3895 */ 3896 *arg = skipwhite(*arg + 2); 3897 if (eval4(arg, &var2, evaluate && result) == FAIL) 3898 return FAIL; 3899 3900 /* 3901 * Compute the result. 3902 */ 3903 if (evaluate && result) 3904 { 3905 if (get_tv_number_chk(&var2, &error) == 0) 3906 result = FALSE; 3907 clear_tv(&var2); 3908 if (error) 3909 return FAIL; 3910 } 3911 if (evaluate) 3912 { 3913 rettv->v_type = VAR_NUMBER; 3914 rettv->vval.v_number = result; 3915 } 3916 } 3917 3918 return OK; 3919 } 3920 3921 /* 3922 * Handle third level expression: 3923 * var1 == var2 3924 * var1 =~ var2 3925 * var1 != var2 3926 * var1 !~ var2 3927 * var1 > var2 3928 * var1 >= var2 3929 * var1 < var2 3930 * var1 <= var2 3931 * var1 is var2 3932 * var1 isnot var2 3933 * 3934 * "arg" must point to the first non-white of the expression. 3935 * "arg" is advanced to the next non-white after the recognized expression. 3936 * 3937 * Return OK or FAIL. 3938 */ 3939 static int 3940 eval4(arg, rettv, evaluate) 3941 char_u **arg; 3942 typval_T *rettv; 3943 int evaluate; 3944 { 3945 typval_T var2; 3946 char_u *p; 3947 int i; 3948 exptype_T type = TYPE_UNKNOWN; 3949 int type_is = FALSE; /* TRUE for "is" and "isnot" */ 3950 int len = 2; 3951 long n1, n2; 3952 char_u *s1, *s2; 3953 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 3954 regmatch_T regmatch; 3955 int ic; 3956 char_u *save_cpo; 3957 3958 /* 3959 * Get the first variable. 3960 */ 3961 if (eval5(arg, rettv, evaluate) == FAIL) 3962 return FAIL; 3963 3964 p = *arg; 3965 switch (p[0]) 3966 { 3967 case '=': if (p[1] == '=') 3968 type = TYPE_EQUAL; 3969 else if (p[1] == '~') 3970 type = TYPE_MATCH; 3971 break; 3972 case '!': if (p[1] == '=') 3973 type = TYPE_NEQUAL; 3974 else if (p[1] == '~') 3975 type = TYPE_NOMATCH; 3976 break; 3977 case '>': if (p[1] != '=') 3978 { 3979 type = TYPE_GREATER; 3980 len = 1; 3981 } 3982 else 3983 type = TYPE_GEQUAL; 3984 break; 3985 case '<': if (p[1] != '=') 3986 { 3987 type = TYPE_SMALLER; 3988 len = 1; 3989 } 3990 else 3991 type = TYPE_SEQUAL; 3992 break; 3993 case 'i': if (p[1] == 's') 3994 { 3995 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') 3996 len = 5; 3997 if (!vim_isIDc(p[len])) 3998 { 3999 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL; 4000 type_is = TRUE; 4001 } 4002 } 4003 break; 4004 } 4005 4006 /* 4007 * If there is a comparitive operator, use it. 4008 */ 4009 if (type != TYPE_UNKNOWN) 4010 { 4011 /* extra question mark appended: ignore case */ 4012 if (p[len] == '?') 4013 { 4014 ic = TRUE; 4015 ++len; 4016 } 4017 /* extra '#' appended: match case */ 4018 else if (p[len] == '#') 4019 { 4020 ic = FALSE; 4021 ++len; 4022 } 4023 /* nothing appened: use 'ignorecase' */ 4024 else 4025 ic = p_ic; 4026 4027 /* 4028 * Get the second variable. 4029 */ 4030 *arg = skipwhite(p + len); 4031 if (eval5(arg, &var2, evaluate) == FAIL) 4032 { 4033 clear_tv(rettv); 4034 return FAIL; 4035 } 4036 4037 if (evaluate) 4038 { 4039 if (type_is && rettv->v_type != var2.v_type) 4040 { 4041 /* For "is" a different type always means FALSE, for "notis" 4042 * it means TRUE. */ 4043 n1 = (type == TYPE_NEQUAL); 4044 } 4045 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST) 4046 { 4047 if (type_is) 4048 { 4049 n1 = (rettv->v_type == var2.v_type 4050 && rettv->vval.v_list == var2.vval.v_list); 4051 if (type == TYPE_NEQUAL) 4052 n1 = !n1; 4053 } 4054 else if (rettv->v_type != var2.v_type 4055 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 4056 { 4057 if (rettv->v_type != var2.v_type) 4058 EMSG(_("E691: Can only compare List with List")); 4059 else 4060 EMSG(_("E692: Invalid operation for Lists")); 4061 clear_tv(rettv); 4062 clear_tv(&var2); 4063 return FAIL; 4064 } 4065 else 4066 { 4067 /* Compare two Lists for being equal or unequal. */ 4068 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic); 4069 if (type == TYPE_NEQUAL) 4070 n1 = !n1; 4071 } 4072 } 4073 4074 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT) 4075 { 4076 if (type_is) 4077 { 4078 n1 = (rettv->v_type == var2.v_type 4079 && rettv->vval.v_dict == var2.vval.v_dict); 4080 if (type == TYPE_NEQUAL) 4081 n1 = !n1; 4082 } 4083 else if (rettv->v_type != var2.v_type 4084 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 4085 { 4086 if (rettv->v_type != var2.v_type) 4087 EMSG(_("E735: Can only compare Dictionary with Dictionary")); 4088 else 4089 EMSG(_("E736: Invalid operation for Dictionary")); 4090 clear_tv(rettv); 4091 clear_tv(&var2); 4092 return FAIL; 4093 } 4094 else 4095 { 4096 /* Compare two Dictionaries for being equal or unequal. */ 4097 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic); 4098 if (type == TYPE_NEQUAL) 4099 n1 = !n1; 4100 } 4101 } 4102 4103 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC) 4104 { 4105 if (rettv->v_type != var2.v_type 4106 || (type != TYPE_EQUAL && type != TYPE_NEQUAL)) 4107 { 4108 if (rettv->v_type != var2.v_type) 4109 EMSG(_("E693: Can only compare Funcref with Funcref")); 4110 else 4111 EMSG(_("E694: Invalid operation for Funcrefs")); 4112 clear_tv(rettv); 4113 clear_tv(&var2); 4114 return FAIL; 4115 } 4116 else 4117 { 4118 /* Compare two Funcrefs for being equal or unequal. */ 4119 if (rettv->vval.v_string == NULL 4120 || var2.vval.v_string == NULL) 4121 n1 = FALSE; 4122 else 4123 n1 = STRCMP(rettv->vval.v_string, 4124 var2.vval.v_string) == 0; 4125 if (type == TYPE_NEQUAL) 4126 n1 = !n1; 4127 } 4128 } 4129 4130 /* 4131 * If one of the two variables is a number, compare as a number. 4132 * When using "=~" or "!~", always compare as string. 4133 */ 4134 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER) 4135 && type != TYPE_MATCH && type != TYPE_NOMATCH) 4136 { 4137 n1 = get_tv_number(rettv); 4138 n2 = get_tv_number(&var2); 4139 switch (type) 4140 { 4141 case TYPE_EQUAL: n1 = (n1 == n2); break; 4142 case TYPE_NEQUAL: n1 = (n1 != n2); break; 4143 case TYPE_GREATER: n1 = (n1 > n2); break; 4144 case TYPE_GEQUAL: n1 = (n1 >= n2); break; 4145 case TYPE_SMALLER: n1 = (n1 < n2); break; 4146 case TYPE_SEQUAL: n1 = (n1 <= n2); break; 4147 case TYPE_UNKNOWN: 4148 case TYPE_MATCH: 4149 case TYPE_NOMATCH: break; /* avoid gcc warning */ 4150 } 4151 } 4152 else 4153 { 4154 s1 = get_tv_string_buf(rettv, buf1); 4155 s2 = get_tv_string_buf(&var2, buf2); 4156 if (type != TYPE_MATCH && type != TYPE_NOMATCH) 4157 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2); 4158 else 4159 i = 0; 4160 n1 = FALSE; 4161 switch (type) 4162 { 4163 case TYPE_EQUAL: n1 = (i == 0); break; 4164 case TYPE_NEQUAL: n1 = (i != 0); break; 4165 case TYPE_GREATER: n1 = (i > 0); break; 4166 case TYPE_GEQUAL: n1 = (i >= 0); break; 4167 case TYPE_SMALLER: n1 = (i < 0); break; 4168 case TYPE_SEQUAL: n1 = (i <= 0); break; 4169 4170 case TYPE_MATCH: 4171 case TYPE_NOMATCH: 4172 /* avoid 'l' flag in 'cpoptions' */ 4173 save_cpo = p_cpo; 4174 p_cpo = (char_u *)""; 4175 regmatch.regprog = vim_regcomp(s2, 4176 RE_MAGIC + RE_STRING); 4177 regmatch.rm_ic = ic; 4178 if (regmatch.regprog != NULL) 4179 { 4180 n1 = vim_regexec_nl(®match, s1, (colnr_T)0); 4181 vim_free(regmatch.regprog); 4182 if (type == TYPE_NOMATCH) 4183 n1 = !n1; 4184 } 4185 p_cpo = save_cpo; 4186 break; 4187 4188 case TYPE_UNKNOWN: break; /* avoid gcc warning */ 4189 } 4190 } 4191 clear_tv(rettv); 4192 clear_tv(&var2); 4193 rettv->v_type = VAR_NUMBER; 4194 rettv->vval.v_number = n1; 4195 } 4196 } 4197 4198 return OK; 4199 } 4200 4201 /* 4202 * Handle fourth level expression: 4203 * + number addition 4204 * - number subtraction 4205 * . string concatenation 4206 * 4207 * "arg" must point to the first non-white of the expression. 4208 * "arg" is advanced to the next non-white after the recognized expression. 4209 * 4210 * Return OK or FAIL. 4211 */ 4212 static int 4213 eval5(arg, rettv, evaluate) 4214 char_u **arg; 4215 typval_T *rettv; 4216 int evaluate; 4217 { 4218 typval_T var2; 4219 typval_T var3; 4220 int op; 4221 long n1, n2; 4222 char_u *s1, *s2; 4223 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 4224 char_u *p; 4225 4226 /* 4227 * Get the first variable. 4228 */ 4229 if (eval6(arg, rettv, evaluate) == FAIL) 4230 return FAIL; 4231 4232 /* 4233 * Repeat computing, until no '+', '-' or '.' is following. 4234 */ 4235 for (;;) 4236 { 4237 op = **arg; 4238 if (op != '+' && op != '-' && op != '.') 4239 break; 4240 4241 if (op != '+' || rettv->v_type != VAR_LIST) 4242 { 4243 /* For "list + ...", an illegal use of the first operand as 4244 * a number cannot be determined before evaluating the 2nd 4245 * operand: if this is also a list, all is ok. 4246 * For "something . ...", "something - ..." or "non-list + ...", 4247 * we know that the first operand needs to be a string or number 4248 * without evaluating the 2nd operand. So check before to avoid 4249 * side effects after an error. */ 4250 if (evaluate && get_tv_string_chk(rettv) == NULL) 4251 { 4252 clear_tv(rettv); 4253 return FAIL; 4254 } 4255 } 4256 4257 /* 4258 * Get the second variable. 4259 */ 4260 *arg = skipwhite(*arg + 1); 4261 if (eval6(arg, &var2, evaluate) == FAIL) 4262 { 4263 clear_tv(rettv); 4264 return FAIL; 4265 } 4266 4267 if (evaluate) 4268 { 4269 /* 4270 * Compute the result. 4271 */ 4272 if (op == '.') 4273 { 4274 s1 = get_tv_string_buf(rettv, buf1); /* already checked */ 4275 s2 = get_tv_string_buf_chk(&var2, buf2); 4276 if (s2 == NULL) /* type error ? */ 4277 { 4278 clear_tv(rettv); 4279 clear_tv(&var2); 4280 return FAIL; 4281 } 4282 p = concat_str(s1, s2); 4283 clear_tv(rettv); 4284 rettv->v_type = VAR_STRING; 4285 rettv->vval.v_string = p; 4286 } 4287 else if (op == '+' && rettv->v_type == VAR_LIST 4288 && var2.v_type == VAR_LIST) 4289 { 4290 /* concatenate Lists */ 4291 if (list_concat(rettv->vval.v_list, var2.vval.v_list, 4292 &var3) == FAIL) 4293 { 4294 clear_tv(rettv); 4295 clear_tv(&var2); 4296 return FAIL; 4297 } 4298 clear_tv(rettv); 4299 *rettv = var3; 4300 } 4301 else 4302 { 4303 int error = FALSE; 4304 4305 n1 = get_tv_number_chk(rettv, &error); 4306 if (error) 4307 { 4308 /* This can only happen for "list + non-list". 4309 * For "non-list + ..." or "something - ...", we returned 4310 * before evaluating the 2nd operand. */ 4311 clear_tv(rettv); 4312 return FAIL; 4313 } 4314 n2 = get_tv_number_chk(&var2, &error); 4315 if (error) 4316 { 4317 clear_tv(rettv); 4318 clear_tv(&var2); 4319 return FAIL; 4320 } 4321 clear_tv(rettv); 4322 if (op == '+') 4323 n1 = n1 + n2; 4324 else 4325 n1 = n1 - n2; 4326 rettv->v_type = VAR_NUMBER; 4327 rettv->vval.v_number = n1; 4328 } 4329 clear_tv(&var2); 4330 } 4331 } 4332 return OK; 4333 } 4334 4335 /* 4336 * Handle fifth level expression: 4337 * * number multiplication 4338 * / number division 4339 * % number modulo 4340 * 4341 * "arg" must point to the first non-white of the expression. 4342 * "arg" is advanced to the next non-white after the recognized expression. 4343 * 4344 * Return OK or FAIL. 4345 */ 4346 static int 4347 eval6(arg, rettv, evaluate) 4348 char_u **arg; 4349 typval_T *rettv; 4350 int evaluate; 4351 { 4352 typval_T var2; 4353 int op; 4354 long n1, n2; 4355 int error = FALSE; 4356 4357 /* 4358 * Get the first variable. 4359 */ 4360 if (eval7(arg, rettv, evaluate) == FAIL) 4361 return FAIL; 4362 4363 /* 4364 * Repeat computing, until no '*', '/' or '%' is following. 4365 */ 4366 for (;;) 4367 { 4368 op = **arg; 4369 if (op != '*' && op != '/' && op != '%') 4370 break; 4371 4372 if (evaluate) 4373 { 4374 n1 = get_tv_number_chk(rettv, &error); 4375 clear_tv(rettv); 4376 if (error) 4377 return FAIL; 4378 } 4379 else 4380 n1 = 0; 4381 4382 /* 4383 * Get the second variable. 4384 */ 4385 *arg = skipwhite(*arg + 1); 4386 if (eval7(arg, &var2, evaluate) == FAIL) 4387 return FAIL; 4388 4389 if (evaluate) 4390 { 4391 n2 = get_tv_number_chk(&var2, &error); 4392 clear_tv(&var2); 4393 if (error) 4394 return FAIL; 4395 4396 /* 4397 * Compute the result. 4398 */ 4399 if (op == '*') 4400 n1 = n1 * n2; 4401 else if (op == '/') 4402 { 4403 if (n2 == 0) /* give an error message? */ 4404 n1 = 0x7fffffffL; 4405 else 4406 n1 = n1 / n2; 4407 } 4408 else 4409 { 4410 if (n2 == 0) /* give an error message? */ 4411 n1 = 0; 4412 else 4413 n1 = n1 % n2; 4414 } 4415 rettv->v_type = VAR_NUMBER; 4416 rettv->vval.v_number = n1; 4417 } 4418 } 4419 4420 return OK; 4421 } 4422 4423 /* 4424 * Handle sixth level expression: 4425 * number number constant 4426 * "string" string contstant 4427 * 'string' literal string contstant 4428 * &option-name option value 4429 * @r register contents 4430 * identifier variable value 4431 * function() function call 4432 * $VAR environment variable 4433 * (expression) nested expression 4434 * [expr, expr] List 4435 * {key: val, key: val} Dictionary 4436 * 4437 * Also handle: 4438 * ! in front logical NOT 4439 * - in front unary minus 4440 * + in front unary plus (ignored) 4441 * trailing [] subscript in String or List 4442 * trailing .name entry in Dictionary 4443 * 4444 * "arg" must point to the first non-white of the expression. 4445 * "arg" is advanced to the next non-white after the recognized expression. 4446 * 4447 * Return OK or FAIL. 4448 */ 4449 static int 4450 eval7(arg, rettv, evaluate) 4451 char_u **arg; 4452 typval_T *rettv; 4453 int evaluate; 4454 { 4455 long n; 4456 int len; 4457 char_u *s; 4458 int val; 4459 char_u *start_leader, *end_leader; 4460 int ret = OK; 4461 char_u *alias; 4462 4463 /* 4464 * Initialise variable so that clear_tv() can't mistake this for a 4465 * string and free a string that isn't there. 4466 */ 4467 rettv->v_type = VAR_UNKNOWN; 4468 4469 /* 4470 * Skip '!' and '-' characters. They are handled later. 4471 */ 4472 start_leader = *arg; 4473 while (**arg == '!' || **arg == '-' || **arg == '+') 4474 *arg = skipwhite(*arg + 1); 4475 end_leader = *arg; 4476 4477 switch (**arg) 4478 { 4479 /* 4480 * Number constant. 4481 */ 4482 case '0': 4483 case '1': 4484 case '2': 4485 case '3': 4486 case '4': 4487 case '5': 4488 case '6': 4489 case '7': 4490 case '8': 4491 case '9': 4492 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL); 4493 *arg += len; 4494 if (evaluate) 4495 { 4496 rettv->v_type = VAR_NUMBER; 4497 rettv->vval.v_number = n; 4498 } 4499 break; 4500 4501 /* 4502 * String constant: "string". 4503 */ 4504 case '"': ret = get_string_tv(arg, rettv, evaluate); 4505 break; 4506 4507 /* 4508 * Literal string constant: 'str''ing'. 4509 */ 4510 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate); 4511 break; 4512 4513 /* 4514 * List: [expr, expr] 4515 */ 4516 case '[': ret = get_list_tv(arg, rettv, evaluate); 4517 break; 4518 4519 /* 4520 * Dictionary: {key: val, key: val} 4521 */ 4522 case '{': ret = get_dict_tv(arg, rettv, evaluate); 4523 break; 4524 4525 /* 4526 * Option value: &name 4527 */ 4528 case '&': ret = get_option_tv(arg, rettv, evaluate); 4529 break; 4530 4531 /* 4532 * Environment variable: $VAR. 4533 */ 4534 case '$': ret = get_env_tv(arg, rettv, evaluate); 4535 break; 4536 4537 /* 4538 * Register contents: @r. 4539 */ 4540 case '@': ++*arg; 4541 if (evaluate) 4542 { 4543 rettv->v_type = VAR_STRING; 4544 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE); 4545 } 4546 if (**arg != NUL) 4547 ++*arg; 4548 break; 4549 4550 /* 4551 * nested expression: (expression). 4552 */ 4553 case '(': *arg = skipwhite(*arg + 1); 4554 ret = eval1(arg, rettv, evaluate); /* recursive! */ 4555 if (**arg == ')') 4556 ++*arg; 4557 else if (ret == OK) 4558 { 4559 EMSG(_("E110: Missing ')'")); 4560 clear_tv(rettv); 4561 ret = FAIL; 4562 } 4563 break; 4564 4565 default: ret = NOTDONE; 4566 break; 4567 } 4568 4569 if (ret == NOTDONE) 4570 { 4571 /* 4572 * Must be a variable or function name. 4573 * Can also be a curly-braces kind of name: {expr}. 4574 */ 4575 s = *arg; 4576 len = get_name_len(arg, &alias, evaluate, TRUE); 4577 if (alias != NULL) 4578 s = alias; 4579 4580 if (len <= 0) 4581 ret = FAIL; 4582 else 4583 { 4584 if (**arg == '(') /* recursive! */ 4585 { 4586 /* If "s" is the name of a variable of type VAR_FUNC 4587 * use its contents. */ 4588 s = deref_func_name(s, &len); 4589 4590 /* Invoke the function. */ 4591 ret = get_func_tv(s, len, rettv, arg, 4592 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 4593 &len, evaluate, NULL); 4594 /* Stop the expression evaluation when immediately 4595 * aborting on error, or when an interrupt occurred or 4596 * an exception was thrown but not caught. */ 4597 if (aborting()) 4598 { 4599 if (ret == OK) 4600 clear_tv(rettv); 4601 ret = FAIL; 4602 } 4603 } 4604 else if (evaluate) 4605 ret = get_var_tv(s, len, rettv, TRUE); 4606 else 4607 ret = OK; 4608 } 4609 4610 if (alias != NULL) 4611 vim_free(alias); 4612 } 4613 4614 *arg = skipwhite(*arg); 4615 4616 /* Handle following '[', '(' and '.' for expr[expr], expr.name, 4617 * expr(expr). */ 4618 if (ret == OK) 4619 ret = handle_subscript(arg, rettv, evaluate, TRUE); 4620 4621 /* 4622 * Apply logical NOT and unary '-', from right to left, ignore '+'. 4623 */ 4624 if (ret == OK && evaluate && end_leader > start_leader) 4625 { 4626 int error = FALSE; 4627 4628 val = get_tv_number_chk(rettv, &error); 4629 if (error) 4630 { 4631 clear_tv(rettv); 4632 ret = FAIL; 4633 } 4634 else 4635 { 4636 while (end_leader > start_leader) 4637 { 4638 --end_leader; 4639 if (*end_leader == '!') 4640 val = !val; 4641 else if (*end_leader == '-') 4642 val = -val; 4643 } 4644 clear_tv(rettv); 4645 rettv->v_type = VAR_NUMBER; 4646 rettv->vval.v_number = val; 4647 } 4648 } 4649 4650 return ret; 4651 } 4652 4653 /* 4654 * Evaluate an "[expr]" or "[expr:expr]" index. 4655 * "*arg" points to the '['. 4656 * Returns FAIL or OK. "*arg" is advanced to after the ']'. 4657 */ 4658 static int 4659 eval_index(arg, rettv, evaluate, verbose) 4660 char_u **arg; 4661 typval_T *rettv; 4662 int evaluate; 4663 int verbose; /* give error messages */ 4664 { 4665 int empty1 = FALSE, empty2 = FALSE; 4666 typval_T var1, var2; 4667 long n1, n2 = 0; 4668 long len = -1; 4669 int range = FALSE; 4670 char_u *s; 4671 char_u *key = NULL; 4672 4673 if (rettv->v_type == VAR_FUNC) 4674 { 4675 if (verbose) 4676 EMSG(_("E695: Cannot index a Funcref")); 4677 return FAIL; 4678 } 4679 4680 if (**arg == '.') 4681 { 4682 /* 4683 * dict.name 4684 */ 4685 key = *arg + 1; 4686 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len) 4687 ; 4688 if (len == 0) 4689 return FAIL; 4690 *arg = skipwhite(key + len); 4691 } 4692 else 4693 { 4694 /* 4695 * something[idx] 4696 * 4697 * Get the (first) variable from inside the []. 4698 */ 4699 *arg = skipwhite(*arg + 1); 4700 if (**arg == ':') 4701 empty1 = TRUE; 4702 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */ 4703 return FAIL; 4704 else if (evaluate && get_tv_string_chk(&var1) == NULL) 4705 { 4706 /* not a number or string */ 4707 clear_tv(&var1); 4708 return FAIL; 4709 } 4710 4711 /* 4712 * Get the second variable from inside the [:]. 4713 */ 4714 if (**arg == ':') 4715 { 4716 range = TRUE; 4717 *arg = skipwhite(*arg + 1); 4718 if (**arg == ']') 4719 empty2 = TRUE; 4720 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */ 4721 { 4722 if (!empty1) 4723 clear_tv(&var1); 4724 return FAIL; 4725 } 4726 else if (evaluate && get_tv_string_chk(&var2) == NULL) 4727 { 4728 /* not a number or string */ 4729 if (!empty1) 4730 clear_tv(&var1); 4731 clear_tv(&var2); 4732 return FAIL; 4733 } 4734 } 4735 4736 /* Check for the ']'. */ 4737 if (**arg != ']') 4738 { 4739 if (verbose) 4740 EMSG(_(e_missbrac)); 4741 clear_tv(&var1); 4742 if (range) 4743 clear_tv(&var2); 4744 return FAIL; 4745 } 4746 *arg = skipwhite(*arg + 1); /* skip the ']' */ 4747 } 4748 4749 if (evaluate) 4750 { 4751 n1 = 0; 4752 if (!empty1 && rettv->v_type != VAR_DICT) 4753 { 4754 n1 = get_tv_number(&var1); 4755 clear_tv(&var1); 4756 } 4757 if (range) 4758 { 4759 if (empty2) 4760 n2 = -1; 4761 else 4762 { 4763 n2 = get_tv_number(&var2); 4764 clear_tv(&var2); 4765 } 4766 } 4767 4768 switch (rettv->v_type) 4769 { 4770 case VAR_NUMBER: 4771 case VAR_STRING: 4772 s = get_tv_string(rettv); 4773 len = (long)STRLEN(s); 4774 if (range) 4775 { 4776 /* The resulting variable is a substring. If the indexes 4777 * are out of range the result is empty. */ 4778 if (n1 < 0) 4779 { 4780 n1 = len + n1; 4781 if (n1 < 0) 4782 n1 = 0; 4783 } 4784 if (n2 < 0) 4785 n2 = len + n2; 4786 else if (n2 >= len) 4787 n2 = len; 4788 if (n1 >= len || n2 < 0 || n1 > n2) 4789 s = NULL; 4790 else 4791 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1)); 4792 } 4793 else 4794 { 4795 /* The resulting variable is a string of a single 4796 * character. If the index is too big or negative the 4797 * result is empty. */ 4798 if (n1 >= len || n1 < 0) 4799 s = NULL; 4800 else 4801 s = vim_strnsave(s + n1, 1); 4802 } 4803 clear_tv(rettv); 4804 rettv->v_type = VAR_STRING; 4805 rettv->vval.v_string = s; 4806 break; 4807 4808 case VAR_LIST: 4809 len = list_len(rettv->vval.v_list); 4810 if (n1 < 0) 4811 n1 = len + n1; 4812 if (!empty1 && (n1 < 0 || n1 >= len)) 4813 { 4814 if (verbose) 4815 EMSGN(_(e_listidx), n1); 4816 return FAIL; 4817 } 4818 if (range) 4819 { 4820 list_T *l; 4821 listitem_T *item; 4822 4823 if (n2 < 0) 4824 n2 = len + n2; 4825 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1)) 4826 { 4827 if (verbose) 4828 EMSGN(_(e_listidx), n2); 4829 return FAIL; 4830 } 4831 l = list_alloc(); 4832 if (l == NULL) 4833 return FAIL; 4834 for (item = list_find(rettv->vval.v_list, n1); 4835 n1 <= n2; ++n1) 4836 { 4837 if (list_append_tv(l, &item->li_tv) == FAIL) 4838 { 4839 list_free(l); 4840 return FAIL; 4841 } 4842 item = item->li_next; 4843 } 4844 clear_tv(rettv); 4845 rettv->v_type = VAR_LIST; 4846 rettv->vval.v_list = l; 4847 ++l->lv_refcount; 4848 } 4849 else 4850 { 4851 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, 4852 &var1); 4853 clear_tv(rettv); 4854 *rettv = var1; 4855 } 4856 break; 4857 4858 case VAR_DICT: 4859 if (range) 4860 { 4861 if (verbose) 4862 EMSG(_(e_dictrange)); 4863 if (len == -1) 4864 clear_tv(&var1); 4865 return FAIL; 4866 } 4867 { 4868 dictitem_T *item; 4869 4870 if (len == -1) 4871 { 4872 key = get_tv_string(&var1); 4873 if (*key == NUL) 4874 { 4875 if (verbose) 4876 EMSG(_(e_emptykey)); 4877 clear_tv(&var1); 4878 return FAIL; 4879 } 4880 } 4881 4882 item = dict_find(rettv->vval.v_dict, key, (int)len); 4883 4884 if (item == NULL && verbose) 4885 EMSG2(_(e_dictkey), key); 4886 if (len == -1) 4887 clear_tv(&var1); 4888 if (item == NULL) 4889 return FAIL; 4890 4891 copy_tv(&item->di_tv, &var1); 4892 clear_tv(rettv); 4893 *rettv = var1; 4894 } 4895 break; 4896 } 4897 } 4898 4899 return OK; 4900 } 4901 4902 /* 4903 * Get an option value. 4904 * "arg" points to the '&' or '+' before the option name. 4905 * "arg" is advanced to character after the option name. 4906 * Return OK or FAIL. 4907 */ 4908 static int 4909 get_option_tv(arg, rettv, evaluate) 4910 char_u **arg; 4911 typval_T *rettv; /* when NULL, only check if option exists */ 4912 int evaluate; 4913 { 4914 char_u *option_end; 4915 long numval; 4916 char_u *stringval; 4917 int opt_type; 4918 int c; 4919 int working = (**arg == '+'); /* has("+option") */ 4920 int ret = OK; 4921 int opt_flags; 4922 4923 /* 4924 * Isolate the option name and find its value. 4925 */ 4926 option_end = find_option_end(arg, &opt_flags); 4927 if (option_end == NULL) 4928 { 4929 if (rettv != NULL) 4930 EMSG2(_("E112: Option name missing: %s"), *arg); 4931 return FAIL; 4932 } 4933 4934 if (!evaluate) 4935 { 4936 *arg = option_end; 4937 return OK; 4938 } 4939 4940 c = *option_end; 4941 *option_end = NUL; 4942 opt_type = get_option_value(*arg, &numval, 4943 rettv == NULL ? NULL : &stringval, opt_flags); 4944 4945 if (opt_type == -3) /* invalid name */ 4946 { 4947 if (rettv != NULL) 4948 EMSG2(_("E113: Unknown option: %s"), *arg); 4949 ret = FAIL; 4950 } 4951 else if (rettv != NULL) 4952 { 4953 if (opt_type == -2) /* hidden string option */ 4954 { 4955 rettv->v_type = VAR_STRING; 4956 rettv->vval.v_string = NULL; 4957 } 4958 else if (opt_type == -1) /* hidden number option */ 4959 { 4960 rettv->v_type = VAR_NUMBER; 4961 rettv->vval.v_number = 0; 4962 } 4963 else if (opt_type == 1) /* number option */ 4964 { 4965 rettv->v_type = VAR_NUMBER; 4966 rettv->vval.v_number = numval; 4967 } 4968 else /* string option */ 4969 { 4970 rettv->v_type = VAR_STRING; 4971 rettv->vval.v_string = stringval; 4972 } 4973 } 4974 else if (working && (opt_type == -2 || opt_type == -1)) 4975 ret = FAIL; 4976 4977 *option_end = c; /* put back for error messages */ 4978 *arg = option_end; 4979 4980 return ret; 4981 } 4982 4983 /* 4984 * Allocate a variable for a string constant. 4985 * Return OK or FAIL. 4986 */ 4987 static int 4988 get_string_tv(arg, rettv, evaluate) 4989 char_u **arg; 4990 typval_T *rettv; 4991 int evaluate; 4992 { 4993 char_u *p; 4994 char_u *name; 4995 int extra = 0; 4996 4997 /* 4998 * Find the end of the string, skipping backslashed characters. 4999 */ 5000 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p)) 5001 { 5002 if (*p == '\\' && p[1] != NUL) 5003 { 5004 ++p; 5005 /* A "\<x>" form occupies at least 4 characters, and produces up 5006 * to 6 characters: reserve space for 2 extra */ 5007 if (*p == '<') 5008 extra += 2; 5009 } 5010 } 5011 5012 if (*p != '"') 5013 { 5014 EMSG2(_("E114: Missing quote: %s"), *arg); 5015 return FAIL; 5016 } 5017 5018 /* If only parsing, set *arg and return here */ 5019 if (!evaluate) 5020 { 5021 *arg = p + 1; 5022 return OK; 5023 } 5024 5025 /* 5026 * Copy the string into allocated memory, handling backslashed 5027 * characters. 5028 */ 5029 name = alloc((unsigned)(p - *arg + extra)); 5030 if (name == NULL) 5031 return FAIL; 5032 rettv->v_type = VAR_STRING; 5033 rettv->vval.v_string = name; 5034 5035 for (p = *arg + 1; *p != NUL && *p != '"'; ) 5036 { 5037 if (*p == '\\') 5038 { 5039 switch (*++p) 5040 { 5041 case 'b': *name++ = BS; ++p; break; 5042 case 'e': *name++ = ESC; ++p; break; 5043 case 'f': *name++ = FF; ++p; break; 5044 case 'n': *name++ = NL; ++p; break; 5045 case 'r': *name++ = CAR; ++p; break; 5046 case 't': *name++ = TAB; ++p; break; 5047 5048 case 'X': /* hex: "\x1", "\x12" */ 5049 case 'x': 5050 case 'u': /* Unicode: "\u0023" */ 5051 case 'U': 5052 if (vim_isxdigit(p[1])) 5053 { 5054 int n, nr; 5055 int c = toupper(*p); 5056 5057 if (c == 'X') 5058 n = 2; 5059 else 5060 n = 4; 5061 nr = 0; 5062 while (--n >= 0 && vim_isxdigit(p[1])) 5063 { 5064 ++p; 5065 nr = (nr << 4) + hex2nr(*p); 5066 } 5067 ++p; 5068 #ifdef FEAT_MBYTE 5069 /* For "\u" store the number according to 5070 * 'encoding'. */ 5071 if (c != 'X') 5072 name += (*mb_char2bytes)(nr, name); 5073 else 5074 #endif 5075 *name++ = nr; 5076 } 5077 break; 5078 5079 /* octal: "\1", "\12", "\123" */ 5080 case '0': 5081 case '1': 5082 case '2': 5083 case '3': 5084 case '4': 5085 case '5': 5086 case '6': 5087 case '7': *name = *p++ - '0'; 5088 if (*p >= '0' && *p <= '7') 5089 { 5090 *name = (*name << 3) + *p++ - '0'; 5091 if (*p >= '0' && *p <= '7') 5092 *name = (*name << 3) + *p++ - '0'; 5093 } 5094 ++name; 5095 break; 5096 5097 /* Special key, e.g.: "\<C-W>" */ 5098 case '<': extra = trans_special(&p, name, TRUE); 5099 if (extra != 0) 5100 { 5101 name += extra; 5102 break; 5103 } 5104 /* FALLTHROUGH */ 5105 5106 default: MB_COPY_CHAR(p, name); 5107 break; 5108 } 5109 } 5110 else 5111 MB_COPY_CHAR(p, name); 5112 5113 } 5114 *name = NUL; 5115 *arg = p + 1; 5116 5117 return OK; 5118 } 5119 5120 /* 5121 * Allocate a variable for a 'str''ing' constant. 5122 * Return OK or FAIL. 5123 */ 5124 static int 5125 get_lit_string_tv(arg, rettv, evaluate) 5126 char_u **arg; 5127 typval_T *rettv; 5128 int evaluate; 5129 { 5130 char_u *p; 5131 char_u *str; 5132 int reduce = 0; 5133 5134 /* 5135 * Find the end of the string, skipping ''. 5136 */ 5137 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p)) 5138 { 5139 if (*p == '\'') 5140 { 5141 if (p[1] != '\'') 5142 break; 5143 ++reduce; 5144 ++p; 5145 } 5146 } 5147 5148 if (*p != '\'') 5149 { 5150 EMSG2(_("E115: Missing quote: %s"), *arg); 5151 return FAIL; 5152 } 5153 5154 /* If only parsing return after setting "*arg" */ 5155 if (!evaluate) 5156 { 5157 *arg = p + 1; 5158 return OK; 5159 } 5160 5161 /* 5162 * Copy the string into allocated memory, handling '' to ' reduction. 5163 */ 5164 str = alloc((unsigned)((p - *arg) - reduce)); 5165 if (str == NULL) 5166 return FAIL; 5167 rettv->v_type = VAR_STRING; 5168 rettv->vval.v_string = str; 5169 5170 for (p = *arg + 1; *p != NUL; ) 5171 { 5172 if (*p == '\'') 5173 { 5174 if (p[1] != '\'') 5175 break; 5176 ++p; 5177 } 5178 MB_COPY_CHAR(p, str); 5179 } 5180 *str = NUL; 5181 *arg = p + 1; 5182 5183 return OK; 5184 } 5185 5186 /* 5187 * Allocate a variable for a List and fill it from "*arg". 5188 * Return OK or FAIL. 5189 */ 5190 static int 5191 get_list_tv(arg, rettv, evaluate) 5192 char_u **arg; 5193 typval_T *rettv; 5194 int evaluate; 5195 { 5196 list_T *l = NULL; 5197 typval_T tv; 5198 listitem_T *item; 5199 5200 if (evaluate) 5201 { 5202 l = list_alloc(); 5203 if (l == NULL) 5204 return FAIL; 5205 } 5206 5207 *arg = skipwhite(*arg + 1); 5208 while (**arg != ']' && **arg != NUL) 5209 { 5210 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */ 5211 goto failret; 5212 if (evaluate) 5213 { 5214 item = listitem_alloc(); 5215 if (item != NULL) 5216 { 5217 item->li_tv = tv; 5218 item->li_tv.v_lock = 0; 5219 list_append(l, item); 5220 } 5221 else 5222 clear_tv(&tv); 5223 } 5224 5225 if (**arg == ']') 5226 break; 5227 if (**arg != ',') 5228 { 5229 EMSG2(_("E696: Missing comma in List: %s"), *arg); 5230 goto failret; 5231 } 5232 *arg = skipwhite(*arg + 1); 5233 } 5234 5235 if (**arg != ']') 5236 { 5237 EMSG2(_("E697: Missing end of List ']': %s"), *arg); 5238 failret: 5239 if (evaluate) 5240 list_free(l); 5241 return FAIL; 5242 } 5243 5244 *arg = skipwhite(*arg + 1); 5245 if (evaluate) 5246 { 5247 rettv->v_type = VAR_LIST; 5248 rettv->vval.v_list = l; 5249 ++l->lv_refcount; 5250 } 5251 5252 return OK; 5253 } 5254 5255 /* 5256 * Allocate an empty header for a list. 5257 * Caller should take care of the reference count. 5258 */ 5259 list_T * 5260 list_alloc() 5261 { 5262 list_T *l; 5263 5264 l = (list_T *)alloc_clear(sizeof(list_T)); 5265 if (l != NULL) 5266 { 5267 /* Prepend the list to the list of lists for garbage collection. */ 5268 if (first_list != NULL) 5269 first_list->lv_used_prev = l; 5270 l->lv_used_prev = NULL; 5271 l->lv_used_next = first_list; 5272 first_list = l; 5273 } 5274 return l; 5275 } 5276 5277 /* 5278 * Allocate an empty list for a return value. 5279 * Returns OK or FAIL. 5280 */ 5281 static int 5282 rettv_list_alloc(rettv) 5283 typval_T *rettv; 5284 { 5285 list_T *l = list_alloc(); 5286 5287 if (l == NULL) 5288 return FAIL; 5289 5290 rettv->vval.v_list = l; 5291 rettv->v_type = VAR_LIST; 5292 ++l->lv_refcount; 5293 return OK; 5294 } 5295 5296 /* 5297 * Unreference a list: decrement the reference count and free it when it 5298 * becomes zero. 5299 */ 5300 void 5301 list_unref(l) 5302 list_T *l; 5303 { 5304 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0) 5305 list_free(l); 5306 } 5307 5308 /* 5309 * Free a list, including all items it points to. 5310 * Ignores the reference count. 5311 */ 5312 void 5313 list_free(l) 5314 list_T *l; 5315 { 5316 listitem_T *item; 5317 5318 /* Avoid that recursive reference to the list frees us again. */ 5319 l->lv_refcount = DEL_REFCOUNT; 5320 5321 /* Remove the list from the list of lists for garbage collection. */ 5322 if (l->lv_used_prev == NULL) 5323 first_list = l->lv_used_next; 5324 else 5325 l->lv_used_prev->lv_used_next = l->lv_used_next; 5326 if (l->lv_used_next != NULL) 5327 l->lv_used_next->lv_used_prev = l->lv_used_prev; 5328 5329 for (item = l->lv_first; item != NULL; item = l->lv_first) 5330 { 5331 /* Remove the item before deleting it. */ 5332 l->lv_first = item->li_next; 5333 listitem_free(item); 5334 } 5335 vim_free(l); 5336 } 5337 5338 /* 5339 * Allocate a list item. 5340 */ 5341 static listitem_T * 5342 listitem_alloc() 5343 { 5344 return (listitem_T *)alloc(sizeof(listitem_T)); 5345 } 5346 5347 /* 5348 * Free a list item. Also clears the value. Does not notify watchers. 5349 */ 5350 static void 5351 listitem_free(item) 5352 listitem_T *item; 5353 { 5354 clear_tv(&item->li_tv); 5355 vim_free(item); 5356 } 5357 5358 /* 5359 * Remove a list item from a List and free it. Also clears the value. 5360 */ 5361 static void 5362 listitem_remove(l, item) 5363 list_T *l; 5364 listitem_T *item; 5365 { 5366 list_remove(l, item, item); 5367 listitem_free(item); 5368 } 5369 5370 /* 5371 * Get the number of items in a list. 5372 */ 5373 static long 5374 list_len(l) 5375 list_T *l; 5376 { 5377 if (l == NULL) 5378 return 0L; 5379 return l->lv_len; 5380 } 5381 5382 /* 5383 * Return TRUE when two lists have exactly the same values. 5384 */ 5385 static int 5386 list_equal(l1, l2, ic) 5387 list_T *l1; 5388 list_T *l2; 5389 int ic; /* ignore case for strings */ 5390 { 5391 listitem_T *item1, *item2; 5392 5393 if (list_len(l1) != list_len(l2)) 5394 return FALSE; 5395 5396 for (item1 = l1->lv_first, item2 = l2->lv_first; 5397 item1 != NULL && item2 != NULL; 5398 item1 = item1->li_next, item2 = item2->li_next) 5399 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic)) 5400 return FALSE; 5401 return item1 == NULL && item2 == NULL; 5402 } 5403 5404 #if defined(FEAT_PYTHON) || defined(PROTO) 5405 /* 5406 * Return the dictitem that an entry in a hashtable points to. 5407 */ 5408 dictitem_T * 5409 dict_lookup(hi) 5410 hashitem_T *hi; 5411 { 5412 return HI2DI(hi); 5413 } 5414 #endif 5415 5416 /* 5417 * Return TRUE when two dictionaries have exactly the same key/values. 5418 */ 5419 static int 5420 dict_equal(d1, d2, ic) 5421 dict_T *d1; 5422 dict_T *d2; 5423 int ic; /* ignore case for strings */ 5424 { 5425 hashitem_T *hi; 5426 dictitem_T *item2; 5427 int todo; 5428 5429 if (dict_len(d1) != dict_len(d2)) 5430 return FALSE; 5431 5432 todo = d1->dv_hashtab.ht_used; 5433 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi) 5434 { 5435 if (!HASHITEM_EMPTY(hi)) 5436 { 5437 item2 = dict_find(d2, hi->hi_key, -1); 5438 if (item2 == NULL) 5439 return FALSE; 5440 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic)) 5441 return FALSE; 5442 --todo; 5443 } 5444 } 5445 return TRUE; 5446 } 5447 5448 /* 5449 * Return TRUE if "tv1" and "tv2" have the same value. 5450 * Compares the items just like "==" would compare them, but strings and 5451 * numbers are different. 5452 */ 5453 static int 5454 tv_equal(tv1, tv2, ic) 5455 typval_T *tv1; 5456 typval_T *tv2; 5457 int ic; /* ignore case */ 5458 { 5459 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; 5460 char_u *s1, *s2; 5461 5462 if (tv1->v_type != tv2->v_type) 5463 return FALSE; 5464 5465 switch (tv1->v_type) 5466 { 5467 case VAR_LIST: 5468 /* recursive! */ 5469 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic); 5470 5471 case VAR_DICT: 5472 /* recursive! */ 5473 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic); 5474 5475 case VAR_FUNC: 5476 return (tv1->vval.v_string != NULL 5477 && tv2->vval.v_string != NULL 5478 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0); 5479 5480 case VAR_NUMBER: 5481 return tv1->vval.v_number == tv2->vval.v_number; 5482 5483 case VAR_STRING: 5484 s1 = get_tv_string_buf(tv1, buf1); 5485 s2 = get_tv_string_buf(tv2, buf2); 5486 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0); 5487 } 5488 5489 EMSG2(_(e_intern2), "tv_equal()"); 5490 return TRUE; 5491 } 5492 5493 /* 5494 * Locate item with index "n" in list "l" and return it. 5495 * A negative index is counted from the end; -1 is the last item. 5496 * Returns NULL when "n" is out of range. 5497 */ 5498 static listitem_T * 5499 list_find(l, n) 5500 list_T *l; 5501 long n; 5502 { 5503 listitem_T *item; 5504 long idx; 5505 5506 if (l == NULL) 5507 return NULL; 5508 5509 /* Negative index is relative to the end. */ 5510 if (n < 0) 5511 n = l->lv_len + n; 5512 5513 /* Check for index out of range. */ 5514 if (n < 0 || n >= l->lv_len) 5515 return NULL; 5516 5517 /* When there is a cached index may start search from there. */ 5518 if (l->lv_idx_item != NULL) 5519 { 5520 if (n < l->lv_idx / 2) 5521 { 5522 /* closest to the start of the list */ 5523 item = l->lv_first; 5524 idx = 0; 5525 } 5526 else if (n > (l->lv_idx + l->lv_len) / 2) 5527 { 5528 /* closest to the end of the list */ 5529 item = l->lv_last; 5530 idx = l->lv_len - 1; 5531 } 5532 else 5533 { 5534 /* closest to the cached index */ 5535 item = l->lv_idx_item; 5536 idx = l->lv_idx; 5537 } 5538 } 5539 else 5540 { 5541 if (n < l->lv_len / 2) 5542 { 5543 /* closest to the start of the list */ 5544 item = l->lv_first; 5545 idx = 0; 5546 } 5547 else 5548 { 5549 /* closest to the end of the list */ 5550 item = l->lv_last; 5551 idx = l->lv_len - 1; 5552 } 5553 } 5554 5555 while (n > idx) 5556 { 5557 /* search forward */ 5558 item = item->li_next; 5559 ++idx; 5560 } 5561 while (n < idx) 5562 { 5563 /* search backward */ 5564 item = item->li_prev; 5565 --idx; 5566 } 5567 5568 /* cache the used index */ 5569 l->lv_idx = idx; 5570 l->lv_idx_item = item; 5571 5572 return item; 5573 } 5574 5575 /* 5576 * Get list item "l[idx]" as a number. 5577 */ 5578 static long 5579 list_find_nr(l, idx, errorp) 5580 list_T *l; 5581 long idx; 5582 int *errorp; /* set to TRUE when something wrong */ 5583 { 5584 listitem_T *li; 5585 5586 li = list_find(l, idx); 5587 if (li == NULL) 5588 { 5589 if (errorp != NULL) 5590 *errorp = TRUE; 5591 return -1L; 5592 } 5593 return get_tv_number_chk(&li->li_tv, errorp); 5594 } 5595 5596 /* 5597 * Locate "item" list "l" and return its index. 5598 * Returns -1 when "item" is not in the list. 5599 */ 5600 static long 5601 list_idx_of_item(l, item) 5602 list_T *l; 5603 listitem_T *item; 5604 { 5605 long idx = 0; 5606 listitem_T *li; 5607 5608 if (l == NULL) 5609 return -1; 5610 idx = 0; 5611 for (li = l->lv_first; li != NULL && li != item; li = li->li_next) 5612 ++idx; 5613 if (li == NULL) 5614 return -1; 5615 return idx; 5616 } 5617 5618 /* 5619 * Append item "item" to the end of list "l". 5620 */ 5621 static void 5622 list_append(l, item) 5623 list_T *l; 5624 listitem_T *item; 5625 { 5626 if (l->lv_last == NULL) 5627 { 5628 /* empty list */ 5629 l->lv_first = item; 5630 l->lv_last = item; 5631 item->li_prev = NULL; 5632 } 5633 else 5634 { 5635 l->lv_last->li_next = item; 5636 item->li_prev = l->lv_last; 5637 l->lv_last = item; 5638 } 5639 ++l->lv_len; 5640 item->li_next = NULL; 5641 } 5642 5643 /* 5644 * Append typval_T "tv" to the end of list "l". 5645 * Return FAIL when out of memory. 5646 */ 5647 static int 5648 list_append_tv(l, tv) 5649 list_T *l; 5650 typval_T *tv; 5651 { 5652 listitem_T *li = listitem_alloc(); 5653 5654 if (li == NULL) 5655 return FAIL; 5656 copy_tv(tv, &li->li_tv); 5657 list_append(l, li); 5658 return OK; 5659 } 5660 5661 /* 5662 * Add a dictionary to a list. Used by getqflist(). 5663 * Return FAIL when out of memory. 5664 */ 5665 int 5666 list_append_dict(list, dict) 5667 list_T *list; 5668 dict_T *dict; 5669 { 5670 listitem_T *li = listitem_alloc(); 5671 5672 if (li == NULL) 5673 return FAIL; 5674 li->li_tv.v_type = VAR_DICT; 5675 li->li_tv.v_lock = 0; 5676 li->li_tv.vval.v_dict = dict; 5677 list_append(list, li); 5678 ++dict->dv_refcount; 5679 return OK; 5680 } 5681 5682 /* 5683 * Make a copy of "str" and append it as an item to list "l". 5684 * When "len" >= 0 use "str[len]". 5685 * Returns FAIL when out of memory. 5686 */ 5687 static int 5688 list_append_string(l, str, len) 5689 list_T *l; 5690 char_u *str; 5691 int len; 5692 { 5693 listitem_T *li = listitem_alloc(); 5694 5695 if (li == NULL) 5696 return FAIL; 5697 list_append(l, li); 5698 li->li_tv.v_type = VAR_STRING; 5699 li->li_tv.v_lock = 0; 5700 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len) 5701 : vim_strsave(str))) == NULL) 5702 return FAIL; 5703 return OK; 5704 } 5705 5706 /* 5707 * Append "n" to list "l". 5708 * Returns FAIL when out of memory. 5709 */ 5710 static int 5711 list_append_number(l, n) 5712 list_T *l; 5713 varnumber_T n; 5714 { 5715 listitem_T *li; 5716 5717 li = listitem_alloc(); 5718 if (li == NULL) 5719 return FAIL; 5720 li->li_tv.v_type = VAR_NUMBER; 5721 li->li_tv.v_lock = 0; 5722 li->li_tv.vval.v_number = n; 5723 list_append(l, li); 5724 return OK; 5725 } 5726 5727 /* 5728 * Insert typval_T "tv" in list "l" before "item". 5729 * If "item" is NULL append at the end. 5730 * Return FAIL when out of memory. 5731 */ 5732 static int 5733 list_insert_tv(l, tv, item) 5734 list_T *l; 5735 typval_T *tv; 5736 listitem_T *item; 5737 { 5738 listitem_T *ni = listitem_alloc(); 5739 5740 if (ni == NULL) 5741 return FAIL; 5742 copy_tv(tv, &ni->li_tv); 5743 if (item == NULL) 5744 /* Append new item at end of list. */ 5745 list_append(l, ni); 5746 else 5747 { 5748 /* Insert new item before existing item. */ 5749 ni->li_prev = item->li_prev; 5750 ni->li_next = item; 5751 if (item->li_prev == NULL) 5752 { 5753 l->lv_first = ni; 5754 ++l->lv_idx; 5755 } 5756 else 5757 { 5758 item->li_prev->li_next = ni; 5759 l->lv_idx_item = NULL; 5760 } 5761 item->li_prev = ni; 5762 ++l->lv_len; 5763 } 5764 return OK; 5765 } 5766 5767 /* 5768 * Extend "l1" with "l2". 5769 * If "bef" is NULL append at the end, otherwise insert before this item. 5770 * Returns FAIL when out of memory. 5771 */ 5772 static int 5773 list_extend(l1, l2, bef) 5774 list_T *l1; 5775 list_T *l2; 5776 listitem_T *bef; 5777 { 5778 listitem_T *item; 5779 5780 for (item = l2->lv_first; item != NULL; item = item->li_next) 5781 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL) 5782 return FAIL; 5783 return OK; 5784 } 5785 5786 /* 5787 * Concatenate lists "l1" and "l2" into a new list, stored in "tv". 5788 * Return FAIL when out of memory. 5789 */ 5790 static int 5791 list_concat(l1, l2, tv) 5792 list_T *l1; 5793 list_T *l2; 5794 typval_T *tv; 5795 { 5796 list_T *l; 5797 5798 /* make a copy of the first list. */ 5799 l = list_copy(l1, FALSE, 0); 5800 if (l == NULL) 5801 return FAIL; 5802 tv->v_type = VAR_LIST; 5803 tv->vval.v_list = l; 5804 5805 /* append all items from the second list */ 5806 return list_extend(l, l2, NULL); 5807 } 5808 5809 /* 5810 * Make a copy of list "orig". Shallow if "deep" is FALSE. 5811 * The refcount of the new list is set to 1. 5812 * See item_copy() for "copyID". 5813 * Returns NULL when out of memory. 5814 */ 5815 static list_T * 5816 list_copy(orig, deep, copyID) 5817 list_T *orig; 5818 int deep; 5819 int copyID; 5820 { 5821 list_T *copy; 5822 listitem_T *item; 5823 listitem_T *ni; 5824 5825 if (orig == NULL) 5826 return NULL; 5827 5828 copy = list_alloc(); 5829 if (copy != NULL) 5830 { 5831 if (copyID != 0) 5832 { 5833 /* Do this before adding the items, because one of the items may 5834 * refer back to this list. */ 5835 orig->lv_copyID = copyID; 5836 orig->lv_copylist = copy; 5837 } 5838 for (item = orig->lv_first; item != NULL && !got_int; 5839 item = item->li_next) 5840 { 5841 ni = listitem_alloc(); 5842 if (ni == NULL) 5843 break; 5844 if (deep) 5845 { 5846 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL) 5847 { 5848 vim_free(ni); 5849 break; 5850 } 5851 } 5852 else 5853 copy_tv(&item->li_tv, &ni->li_tv); 5854 list_append(copy, ni); 5855 } 5856 ++copy->lv_refcount; 5857 if (item != NULL) 5858 { 5859 list_unref(copy); 5860 copy = NULL; 5861 } 5862 } 5863 5864 return copy; 5865 } 5866 5867 /* 5868 * Remove items "item" to "item2" from list "l". 5869 * Does not free the listitem or the value! 5870 */ 5871 static void 5872 list_remove(l, item, item2) 5873 list_T *l; 5874 listitem_T *item; 5875 listitem_T *item2; 5876 { 5877 listitem_T *ip; 5878 5879 /* notify watchers */ 5880 for (ip = item; ip != NULL; ip = ip->li_next) 5881 { 5882 --l->lv_len; 5883 list_fix_watch(l, ip); 5884 if (ip == item2) 5885 break; 5886 } 5887 5888 if (item2->li_next == NULL) 5889 l->lv_last = item->li_prev; 5890 else 5891 item2->li_next->li_prev = item->li_prev; 5892 if (item->li_prev == NULL) 5893 l->lv_first = item2->li_next; 5894 else 5895 item->li_prev->li_next = item2->li_next; 5896 l->lv_idx_item = NULL; 5897 } 5898 5899 /* 5900 * Return an allocated string with the string representation of a list. 5901 * May return NULL. 5902 */ 5903 static char_u * 5904 list2string(tv, copyID) 5905 typval_T *tv; 5906 int copyID; 5907 { 5908 garray_T ga; 5909 5910 if (tv->vval.v_list == NULL) 5911 return NULL; 5912 ga_init2(&ga, (int)sizeof(char), 80); 5913 ga_append(&ga, '['); 5914 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL) 5915 { 5916 vim_free(ga.ga_data); 5917 return NULL; 5918 } 5919 ga_append(&ga, ']'); 5920 ga_append(&ga, NUL); 5921 return (char_u *)ga.ga_data; 5922 } 5923 5924 /* 5925 * Join list "l" into a string in "*gap", using separator "sep". 5926 * When "echo" is TRUE use String as echoed, otherwise as inside a List. 5927 * Return FAIL or OK. 5928 */ 5929 static int 5930 list_join(gap, l, sep, echo, copyID) 5931 garray_T *gap; 5932 list_T *l; 5933 char_u *sep; 5934 int echo; 5935 int copyID; 5936 { 5937 int first = TRUE; 5938 char_u *tofree; 5939 char_u numbuf[NUMBUFLEN]; 5940 listitem_T *item; 5941 char_u *s; 5942 5943 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next) 5944 { 5945 if (first) 5946 first = FALSE; 5947 else 5948 ga_concat(gap, sep); 5949 5950 if (echo) 5951 s = echo_string(&item->li_tv, &tofree, numbuf, copyID); 5952 else 5953 s = tv2string(&item->li_tv, &tofree, numbuf, copyID); 5954 if (s != NULL) 5955 ga_concat(gap, s); 5956 vim_free(tofree); 5957 if (s == NULL) 5958 return FAIL; 5959 } 5960 return OK; 5961 } 5962 5963 /* 5964 * Garbage collection for lists and dictionaries. 5965 * 5966 * We use reference counts to be able to free most items right away when they 5967 * are no longer used. But for composite items it's possible that it becomes 5968 * unused while the reference count is > 0: When there is a recursive 5969 * reference. Example: 5970 * :let l = [1, 2, 3] 5971 * :let d = {9: l} 5972 * :let l[1] = d 5973 * 5974 * Since this is quite unusual we handle this with garbage collection: every 5975 * once in a while find out which lists and dicts are not referenced from any 5976 * variable. 5977 * 5978 * Here is a good reference text about garbage collection (refers to Python 5979 * but it applies to all reference-counting mechanisms): 5980 * http://python.ca/nas/python/gc/ 5981 */ 5982 5983 /* 5984 * Do garbage collection for lists and dicts. 5985 * Return TRUE if some memory was freed. 5986 */ 5987 int 5988 garbage_collect() 5989 { 5990 dict_T *dd; 5991 list_T *ll; 5992 int copyID = ++current_copyID; 5993 buf_T *buf; 5994 win_T *wp; 5995 int i; 5996 funccall_T *fc; 5997 int did_free = FALSE; 5998 5999 /* 6000 * 1. Go through all accessible variables and mark all lists and dicts 6001 * with copyID. 6002 */ 6003 /* script-local variables */ 6004 for (i = 1; i <= ga_scripts.ga_len; ++i) 6005 set_ref_in_ht(&SCRIPT_VARS(i), copyID); 6006 6007 /* buffer-local variables */ 6008 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 6009 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID); 6010 6011 /* window-local variables */ 6012 FOR_ALL_WINDOWS(wp) 6013 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID); 6014 6015 /* global variables */ 6016 set_ref_in_ht(&globvarht, copyID); 6017 6018 /* function-local variables */ 6019 for (fc = current_funccal; fc != NULL; fc = fc->caller) 6020 { 6021 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID); 6022 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID); 6023 } 6024 6025 /* 6026 * 2. Go through the list of dicts and free items without the copyID. 6027 */ 6028 for (dd = first_dict; dd != NULL; ) 6029 if (dd->dv_copyID != copyID) 6030 { 6031 dict_free(dd); 6032 did_free = TRUE; 6033 6034 /* restart, next dict may also have been freed */ 6035 dd = first_dict; 6036 } 6037 else 6038 dd = dd->dv_used_next; 6039 6040 /* 6041 * 3. Go through the list of lists and free items without the copyID. 6042 * But don't free a list that has a watcher (used in a for loop), these 6043 * are not referenced anywhere. 6044 */ 6045 for (ll = first_list; ll != NULL; ) 6046 if (ll->lv_copyID != copyID && ll->lv_watch == NULL) 6047 { 6048 list_free(ll); 6049 did_free = TRUE; 6050 6051 /* restart, next list may also have been freed */ 6052 ll = first_list; 6053 } 6054 else 6055 ll = ll->lv_used_next; 6056 6057 return did_free; 6058 } 6059 6060 /* 6061 * Mark all lists and dicts referenced through hashtab "ht" with "copyID". 6062 */ 6063 static void 6064 set_ref_in_ht(ht, copyID) 6065 hashtab_T *ht; 6066 int copyID; 6067 { 6068 int todo; 6069 hashitem_T *hi; 6070 6071 todo = ht->ht_used; 6072 for (hi = ht->ht_array; todo > 0; ++hi) 6073 if (!HASHITEM_EMPTY(hi)) 6074 { 6075 --todo; 6076 set_ref_in_item(&HI2DI(hi)->di_tv, copyID); 6077 } 6078 } 6079 6080 /* 6081 * Mark all lists and dicts referenced through list "l" with "copyID". 6082 */ 6083 static void 6084 set_ref_in_list(l, copyID) 6085 list_T *l; 6086 int copyID; 6087 { 6088 listitem_T *li; 6089 6090 for (li = l->lv_first; li != NULL; li = li->li_next) 6091 set_ref_in_item(&li->li_tv, copyID); 6092 } 6093 6094 /* 6095 * Mark all lists and dicts referenced through typval "tv" with "copyID". 6096 */ 6097 static void 6098 set_ref_in_item(tv, copyID) 6099 typval_T *tv; 6100 int copyID; 6101 { 6102 dict_T *dd; 6103 list_T *ll; 6104 6105 switch (tv->v_type) 6106 { 6107 case VAR_DICT: 6108 dd = tv->vval.v_dict; 6109 if (dd->dv_copyID != copyID) 6110 { 6111 /* Didn't see this dict yet. */ 6112 dd->dv_copyID = copyID; 6113 set_ref_in_ht(&dd->dv_hashtab, copyID); 6114 } 6115 break; 6116 6117 case VAR_LIST: 6118 ll = tv->vval.v_list; 6119 if (ll->lv_copyID != copyID) 6120 { 6121 /* Didn't see this list yet. */ 6122 ll->lv_copyID = copyID; 6123 set_ref_in_list(ll, copyID); 6124 } 6125 break; 6126 } 6127 return; 6128 } 6129 6130 /* 6131 * Allocate an empty header for a dictionary. 6132 */ 6133 dict_T * 6134 dict_alloc() 6135 { 6136 dict_T *d; 6137 6138 d = (dict_T *)alloc(sizeof(dict_T)); 6139 if (d != NULL) 6140 { 6141 /* Add the list to the hashtable for garbage collection. */ 6142 if (first_dict != NULL) 6143 first_dict->dv_used_prev = d; 6144 d->dv_used_next = first_dict; 6145 d->dv_used_prev = NULL; 6146 6147 hash_init(&d->dv_hashtab); 6148 d->dv_lock = 0; 6149 d->dv_refcount = 0; 6150 d->dv_copyID = 0; 6151 } 6152 return d; 6153 } 6154 6155 /* 6156 * Unreference a Dictionary: decrement the reference count and free it when it 6157 * becomes zero. 6158 */ 6159 static void 6160 dict_unref(d) 6161 dict_T *d; 6162 { 6163 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0) 6164 dict_free(d); 6165 } 6166 6167 /* 6168 * Free a Dictionary, including all items it contains. 6169 * Ignores the reference count. 6170 */ 6171 static void 6172 dict_free(d) 6173 dict_T *d; 6174 { 6175 int todo; 6176 hashitem_T *hi; 6177 dictitem_T *di; 6178 6179 /* Avoid that recursive reference to the dict frees us again. */ 6180 d->dv_refcount = DEL_REFCOUNT; 6181 6182 /* Remove the dict from the list of dicts for garbage collection. */ 6183 if (d->dv_used_prev == NULL) 6184 first_dict = d->dv_used_next; 6185 else 6186 d->dv_used_prev->dv_used_next = d->dv_used_next; 6187 if (d->dv_used_next != NULL) 6188 d->dv_used_next->dv_used_prev = d->dv_used_prev; 6189 6190 /* Lock the hashtab, we don't want it to resize while freeing items. */ 6191 hash_lock(&d->dv_hashtab); 6192 todo = d->dv_hashtab.ht_used; 6193 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 6194 { 6195 if (!HASHITEM_EMPTY(hi)) 6196 { 6197 /* Remove the item before deleting it, just in case there is 6198 * something recursive causing trouble. */ 6199 di = HI2DI(hi); 6200 hash_remove(&d->dv_hashtab, hi); 6201 dictitem_free(di); 6202 --todo; 6203 } 6204 } 6205 hash_clear(&d->dv_hashtab); 6206 vim_free(d); 6207 } 6208 6209 /* 6210 * Allocate a Dictionary item. 6211 * The "key" is copied to the new item. 6212 * Note that the value of the item "di_tv" still needs to be initialized! 6213 * Returns NULL when out of memory. 6214 */ 6215 static dictitem_T * 6216 dictitem_alloc(key) 6217 char_u *key; 6218 { 6219 dictitem_T *di; 6220 6221 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key)); 6222 if (di != NULL) 6223 { 6224 STRCPY(di->di_key, key); 6225 di->di_flags = 0; 6226 } 6227 return di; 6228 } 6229 6230 /* 6231 * Make a copy of a Dictionary item. 6232 */ 6233 static dictitem_T * 6234 dictitem_copy(org) 6235 dictitem_T *org; 6236 { 6237 dictitem_T *di; 6238 6239 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key)); 6240 if (di != NULL) 6241 { 6242 STRCPY(di->di_key, org->di_key); 6243 di->di_flags = 0; 6244 copy_tv(&org->di_tv, &di->di_tv); 6245 } 6246 return di; 6247 } 6248 6249 /* 6250 * Remove item "item" from Dictionary "dict" and free it. 6251 */ 6252 static void 6253 dictitem_remove(dict, item) 6254 dict_T *dict; 6255 dictitem_T *item; 6256 { 6257 hashitem_T *hi; 6258 6259 hi = hash_find(&dict->dv_hashtab, item->di_key); 6260 if (HASHITEM_EMPTY(hi)) 6261 EMSG2(_(e_intern2), "dictitem_remove()"); 6262 else 6263 hash_remove(&dict->dv_hashtab, hi); 6264 dictitem_free(item); 6265 } 6266 6267 /* 6268 * Free a dict item. Also clears the value. 6269 */ 6270 static void 6271 dictitem_free(item) 6272 dictitem_T *item; 6273 { 6274 clear_tv(&item->di_tv); 6275 vim_free(item); 6276 } 6277 6278 /* 6279 * Make a copy of dict "d". Shallow if "deep" is FALSE. 6280 * The refcount of the new dict is set to 1. 6281 * See item_copy() for "copyID". 6282 * Returns NULL when out of memory. 6283 */ 6284 static dict_T * 6285 dict_copy(orig, deep, copyID) 6286 dict_T *orig; 6287 int deep; 6288 int copyID; 6289 { 6290 dict_T *copy; 6291 dictitem_T *di; 6292 int todo; 6293 hashitem_T *hi; 6294 6295 if (orig == NULL) 6296 return NULL; 6297 6298 copy = dict_alloc(); 6299 if (copy != NULL) 6300 { 6301 if (copyID != 0) 6302 { 6303 orig->dv_copyID = copyID; 6304 orig->dv_copydict = copy; 6305 } 6306 todo = orig->dv_hashtab.ht_used; 6307 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) 6308 { 6309 if (!HASHITEM_EMPTY(hi)) 6310 { 6311 --todo; 6312 6313 di = dictitem_alloc(hi->hi_key); 6314 if (di == NULL) 6315 break; 6316 if (deep) 6317 { 6318 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep, 6319 copyID) == FAIL) 6320 { 6321 vim_free(di); 6322 break; 6323 } 6324 } 6325 else 6326 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv); 6327 if (dict_add(copy, di) == FAIL) 6328 { 6329 dictitem_free(di); 6330 break; 6331 } 6332 } 6333 } 6334 6335 ++copy->dv_refcount; 6336 if (todo > 0) 6337 { 6338 dict_unref(copy); 6339 copy = NULL; 6340 } 6341 } 6342 6343 return copy; 6344 } 6345 6346 /* 6347 * Add item "item" to Dictionary "d". 6348 * Returns FAIL when out of memory and when key already existed. 6349 */ 6350 static int 6351 dict_add(d, item) 6352 dict_T *d; 6353 dictitem_T *item; 6354 { 6355 return hash_add(&d->dv_hashtab, item->di_key); 6356 } 6357 6358 /* 6359 * Add a number or string entry to dictionary "d". 6360 * When "str" is NULL use number "nr", otherwise use "str". 6361 * Returns FAIL when out of memory and when key already exists. 6362 */ 6363 int 6364 dict_add_nr_str(d, key, nr, str) 6365 dict_T *d; 6366 char *key; 6367 long nr; 6368 char_u *str; 6369 { 6370 dictitem_T *item; 6371 6372 item = dictitem_alloc((char_u *)key); 6373 if (item == NULL) 6374 return FAIL; 6375 item->di_tv.v_lock = 0; 6376 if (str == NULL) 6377 { 6378 item->di_tv.v_type = VAR_NUMBER; 6379 item->di_tv.vval.v_number = nr; 6380 } 6381 else 6382 { 6383 item->di_tv.v_type = VAR_STRING; 6384 item->di_tv.vval.v_string = vim_strsave(str); 6385 } 6386 if (dict_add(d, item) == FAIL) 6387 { 6388 dictitem_free(item); 6389 return FAIL; 6390 } 6391 return OK; 6392 } 6393 6394 /* 6395 * Get the number of items in a Dictionary. 6396 */ 6397 static long 6398 dict_len(d) 6399 dict_T *d; 6400 { 6401 if (d == NULL) 6402 return 0L; 6403 return d->dv_hashtab.ht_used; 6404 } 6405 6406 /* 6407 * Find item "key[len]" in Dictionary "d". 6408 * If "len" is negative use strlen(key). 6409 * Returns NULL when not found. 6410 */ 6411 static dictitem_T * 6412 dict_find(d, key, len) 6413 dict_T *d; 6414 char_u *key; 6415 int len; 6416 { 6417 #define AKEYLEN 200 6418 char_u buf[AKEYLEN]; 6419 char_u *akey; 6420 char_u *tofree = NULL; 6421 hashitem_T *hi; 6422 6423 if (len < 0) 6424 akey = key; 6425 else if (len >= AKEYLEN) 6426 { 6427 tofree = akey = vim_strnsave(key, len); 6428 if (akey == NULL) 6429 return NULL; 6430 } 6431 else 6432 { 6433 /* Avoid a malloc/free by using buf[]. */ 6434 vim_strncpy(buf, key, len); 6435 akey = buf; 6436 } 6437 6438 hi = hash_find(&d->dv_hashtab, akey); 6439 vim_free(tofree); 6440 if (HASHITEM_EMPTY(hi)) 6441 return NULL; 6442 return HI2DI(hi); 6443 } 6444 6445 /* 6446 * Get a string item from a dictionary. 6447 * When "save" is TRUE allocate memory for it. 6448 * Returns NULL if the entry doesn't exist or out of memory. 6449 */ 6450 char_u * 6451 get_dict_string(d, key, save) 6452 dict_T *d; 6453 char_u *key; 6454 int save; 6455 { 6456 dictitem_T *di; 6457 char_u *s; 6458 6459 di = dict_find(d, key, -1); 6460 if (di == NULL) 6461 return NULL; 6462 s = get_tv_string(&di->di_tv); 6463 if (save && s != NULL) 6464 s = vim_strsave(s); 6465 return s; 6466 } 6467 6468 /* 6469 * Get a number item from a dictionary. 6470 * Returns 0 if the entry doesn't exist or out of memory. 6471 */ 6472 long 6473 get_dict_number(d, key) 6474 dict_T *d; 6475 char_u *key; 6476 { 6477 dictitem_T *di; 6478 6479 di = dict_find(d, key, -1); 6480 if (di == NULL) 6481 return 0; 6482 return get_tv_number(&di->di_tv); 6483 } 6484 6485 /* 6486 * Return an allocated string with the string representation of a Dictionary. 6487 * May return NULL. 6488 */ 6489 static char_u * 6490 dict2string(tv, copyID) 6491 typval_T *tv; 6492 int copyID; 6493 { 6494 garray_T ga; 6495 int first = TRUE; 6496 char_u *tofree; 6497 char_u numbuf[NUMBUFLEN]; 6498 hashitem_T *hi; 6499 char_u *s; 6500 dict_T *d; 6501 int todo; 6502 6503 if ((d = tv->vval.v_dict) == NULL) 6504 return NULL; 6505 ga_init2(&ga, (int)sizeof(char), 80); 6506 ga_append(&ga, '{'); 6507 6508 todo = d->dv_hashtab.ht_used; 6509 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) 6510 { 6511 if (!HASHITEM_EMPTY(hi)) 6512 { 6513 --todo; 6514 6515 if (first) 6516 first = FALSE; 6517 else 6518 ga_concat(&ga, (char_u *)", "); 6519 6520 tofree = string_quote(hi->hi_key, FALSE); 6521 if (tofree != NULL) 6522 { 6523 ga_concat(&ga, tofree); 6524 vim_free(tofree); 6525 } 6526 ga_concat(&ga, (char_u *)": "); 6527 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID); 6528 if (s != NULL) 6529 ga_concat(&ga, s); 6530 vim_free(tofree); 6531 if (s == NULL) 6532 break; 6533 } 6534 } 6535 if (todo > 0) 6536 { 6537 vim_free(ga.ga_data); 6538 return NULL; 6539 } 6540 6541 ga_append(&ga, '}'); 6542 ga_append(&ga, NUL); 6543 return (char_u *)ga.ga_data; 6544 } 6545 6546 /* 6547 * Allocate a variable for a Dictionary and fill it from "*arg". 6548 * Return OK or FAIL. Returns NOTDONE for {expr}. 6549 */ 6550 static int 6551 get_dict_tv(arg, rettv, evaluate) 6552 char_u **arg; 6553 typval_T *rettv; 6554 int evaluate; 6555 { 6556 dict_T *d = NULL; 6557 typval_T tvkey; 6558 typval_T tv; 6559 char_u *key; 6560 dictitem_T *item; 6561 char_u *start = skipwhite(*arg + 1); 6562 char_u buf[NUMBUFLEN]; 6563 6564 /* 6565 * First check if it's not a curly-braces thing: {expr}. 6566 * Must do this without evaluating, otherwise a function may be called 6567 * twice. Unfortunately this means we need to call eval1() twice for the 6568 * first item. 6569 * But {} is an empty Dictionary. 6570 */ 6571 if (*start != '}') 6572 { 6573 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */ 6574 return FAIL; 6575 if (*start == '}') 6576 return NOTDONE; 6577 } 6578 6579 if (evaluate) 6580 { 6581 d = dict_alloc(); 6582 if (d == NULL) 6583 return FAIL; 6584 } 6585 tvkey.v_type = VAR_UNKNOWN; 6586 tv.v_type = VAR_UNKNOWN; 6587 6588 *arg = skipwhite(*arg + 1); 6589 while (**arg != '}' && **arg != NUL) 6590 { 6591 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */ 6592 goto failret; 6593 if (**arg != ':') 6594 { 6595 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg); 6596 clear_tv(&tvkey); 6597 goto failret; 6598 } 6599 key = get_tv_string_buf_chk(&tvkey, buf); 6600 if (key == NULL || *key == NUL) 6601 { 6602 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */ 6603 if (key != NULL) 6604 EMSG(_(e_emptykey)); 6605 clear_tv(&tvkey); 6606 goto failret; 6607 } 6608 6609 *arg = skipwhite(*arg + 1); 6610 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */ 6611 { 6612 clear_tv(&tvkey); 6613 goto failret; 6614 } 6615 if (evaluate) 6616 { 6617 item = dict_find(d, key, -1); 6618 if (item != NULL) 6619 { 6620 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key); 6621 clear_tv(&tvkey); 6622 clear_tv(&tv); 6623 goto failret; 6624 } 6625 item = dictitem_alloc(key); 6626 clear_tv(&tvkey); 6627 if (item != NULL) 6628 { 6629 item->di_tv = tv; 6630 item->di_tv.v_lock = 0; 6631 if (dict_add(d, item) == FAIL) 6632 dictitem_free(item); 6633 } 6634 } 6635 6636 if (**arg == '}') 6637 break; 6638 if (**arg != ',') 6639 { 6640 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg); 6641 goto failret; 6642 } 6643 *arg = skipwhite(*arg + 1); 6644 } 6645 6646 if (**arg != '}') 6647 { 6648 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg); 6649 failret: 6650 if (evaluate) 6651 dict_free(d); 6652 return FAIL; 6653 } 6654 6655 *arg = skipwhite(*arg + 1); 6656 if (evaluate) 6657 { 6658 rettv->v_type = VAR_DICT; 6659 rettv->vval.v_dict = d; 6660 ++d->dv_refcount; 6661 } 6662 6663 return OK; 6664 } 6665 6666 /* 6667 * Return a string with the string representation of a variable. 6668 * If the memory is allocated "tofree" is set to it, otherwise NULL. 6669 * "numbuf" is used for a number. 6670 * Does not put quotes around strings, as ":echo" displays values. 6671 * When "copyID" is not NULL replace recursive lists and dicts with "...". 6672 * May return NULL; 6673 */ 6674 static char_u * 6675 echo_string(tv, tofree, numbuf, copyID) 6676 typval_T *tv; 6677 char_u **tofree; 6678 char_u *numbuf; 6679 int copyID; 6680 { 6681 static int recurse = 0; 6682 char_u *r = NULL; 6683 6684 if (recurse >= DICT_MAXNEST) 6685 { 6686 EMSG(_("E724: variable nested too deep for displaying")); 6687 *tofree = NULL; 6688 return NULL; 6689 } 6690 ++recurse; 6691 6692 switch (tv->v_type) 6693 { 6694 case VAR_FUNC: 6695 *tofree = NULL; 6696 r = tv->vval.v_string; 6697 break; 6698 6699 case VAR_LIST: 6700 if (tv->vval.v_list == NULL) 6701 { 6702 *tofree = NULL; 6703 r = NULL; 6704 } 6705 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID) 6706 { 6707 *tofree = NULL; 6708 r = (char_u *)"[...]"; 6709 } 6710 else 6711 { 6712 tv->vval.v_list->lv_copyID = copyID; 6713 *tofree = list2string(tv, copyID); 6714 r = *tofree; 6715 } 6716 break; 6717 6718 case VAR_DICT: 6719 if (tv->vval.v_dict == NULL) 6720 { 6721 *tofree = NULL; 6722 r = NULL; 6723 } 6724 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID) 6725 { 6726 *tofree = NULL; 6727 r = (char_u *)"{...}"; 6728 } 6729 else 6730 { 6731 tv->vval.v_dict->dv_copyID = copyID; 6732 *tofree = dict2string(tv, copyID); 6733 r = *tofree; 6734 } 6735 break; 6736 6737 case VAR_STRING: 6738 case VAR_NUMBER: 6739 *tofree = NULL; 6740 r = get_tv_string_buf(tv, numbuf); 6741 break; 6742 6743 default: 6744 EMSG2(_(e_intern2), "echo_string()"); 6745 *tofree = NULL; 6746 } 6747 6748 --recurse; 6749 return r; 6750 } 6751 6752 /* 6753 * Return a string with the string representation of a variable. 6754 * If the memory is allocated "tofree" is set to it, otherwise NULL. 6755 * "numbuf" is used for a number. 6756 * Puts quotes around strings, so that they can be parsed back by eval(). 6757 * May return NULL; 6758 */ 6759 static char_u * 6760 tv2string(tv, tofree, numbuf, copyID) 6761 typval_T *tv; 6762 char_u **tofree; 6763 char_u *numbuf; 6764 int copyID; 6765 { 6766 switch (tv->v_type) 6767 { 6768 case VAR_FUNC: 6769 *tofree = string_quote(tv->vval.v_string, TRUE); 6770 return *tofree; 6771 case VAR_STRING: 6772 *tofree = string_quote(tv->vval.v_string, FALSE); 6773 return *tofree; 6774 case VAR_NUMBER: 6775 case VAR_LIST: 6776 case VAR_DICT: 6777 break; 6778 default: 6779 EMSG2(_(e_intern2), "tv2string()"); 6780 } 6781 return echo_string(tv, tofree, numbuf, copyID); 6782 } 6783 6784 /* 6785 * Return string "str" in ' quotes, doubling ' characters. 6786 * If "str" is NULL an empty string is assumed. 6787 * If "function" is TRUE make it function('string'). 6788 */ 6789 static char_u * 6790 string_quote(str, function) 6791 char_u *str; 6792 int function; 6793 { 6794 unsigned len; 6795 char_u *p, *r, *s; 6796 6797 len = (function ? 13 : 3); 6798 if (str != NULL) 6799 { 6800 len += STRLEN(str); 6801 for (p = str; *p != NUL; mb_ptr_adv(p)) 6802 if (*p == '\'') 6803 ++len; 6804 } 6805 s = r = alloc(len); 6806 if (r != NULL) 6807 { 6808 if (function) 6809 { 6810 STRCPY(r, "function('"); 6811 r += 10; 6812 } 6813 else 6814 *r++ = '\''; 6815 if (str != NULL) 6816 for (p = str; *p != NUL; ) 6817 { 6818 if (*p == '\'') 6819 *r++ = '\''; 6820 MB_COPY_CHAR(p, r); 6821 } 6822 *r++ = '\''; 6823 if (function) 6824 *r++ = ')'; 6825 *r++ = NUL; 6826 } 6827 return s; 6828 } 6829 6830 /* 6831 * Get the value of an environment variable. 6832 * "arg" is pointing to the '$'. It is advanced to after the name. 6833 * If the environment variable was not set, silently assume it is empty. 6834 * Always return OK. 6835 */ 6836 static int 6837 get_env_tv(arg, rettv, evaluate) 6838 char_u **arg; 6839 typval_T *rettv; 6840 int evaluate; 6841 { 6842 char_u *string = NULL; 6843 int len; 6844 int cc; 6845 char_u *name; 6846 int mustfree = FALSE; 6847 6848 ++*arg; 6849 name = *arg; 6850 len = get_env_len(arg); 6851 if (evaluate) 6852 { 6853 if (len != 0) 6854 { 6855 cc = name[len]; 6856 name[len] = NUL; 6857 /* first try vim_getenv(), fast for normal environment vars */ 6858 string = vim_getenv(name, &mustfree); 6859 if (string != NULL && *string != NUL) 6860 { 6861 if (!mustfree) 6862 string = vim_strsave(string); 6863 } 6864 else 6865 { 6866 if (mustfree) 6867 vim_free(string); 6868 6869 /* next try expanding things like $VIM and ${HOME} */ 6870 string = expand_env_save(name - 1); 6871 if (string != NULL && *string == '$') 6872 { 6873 vim_free(string); 6874 string = NULL; 6875 } 6876 } 6877 name[len] = cc; 6878 } 6879 rettv->v_type = VAR_STRING; 6880 rettv->vval.v_string = string; 6881 } 6882 6883 return OK; 6884 } 6885 6886 /* 6887 * Array with names and number of arguments of all internal functions 6888 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH! 6889 */ 6890 static struct fst 6891 { 6892 char *f_name; /* function name */ 6893 char f_min_argc; /* minimal number of arguments */ 6894 char f_max_argc; /* maximal number of arguments */ 6895 void (*f_func) __ARGS((typval_T *args, typval_T *rvar)); 6896 /* implemenation of function */ 6897 } functions[] = 6898 { 6899 {"add", 2, 2, f_add}, 6900 {"append", 2, 2, f_append}, 6901 {"argc", 0, 0, f_argc}, 6902 {"argidx", 0, 0, f_argidx}, 6903 {"argv", 1, 1, f_argv}, 6904 {"browse", 4, 4, f_browse}, 6905 {"browsedir", 2, 2, f_browsedir}, 6906 {"bufexists", 1, 1, f_bufexists}, 6907 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */ 6908 {"buffer_name", 1, 1, f_bufname}, /* obsolete */ 6909 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */ 6910 {"buflisted", 1, 1, f_buflisted}, 6911 {"bufloaded", 1, 1, f_bufloaded}, 6912 {"bufname", 1, 1, f_bufname}, 6913 {"bufnr", 1, 2, f_bufnr}, 6914 {"bufwinnr", 1, 1, f_bufwinnr}, 6915 {"byte2line", 1, 1, f_byte2line}, 6916 {"byteidx", 2, 2, f_byteidx}, 6917 {"call", 2, 3, f_call}, 6918 {"changenr", 0, 0, f_changenr}, 6919 {"char2nr", 1, 1, f_char2nr}, 6920 {"cindent", 1, 1, f_cindent}, 6921 {"col", 1, 1, f_col}, 6922 #if defined(FEAT_INS_EXPAND) 6923 {"complete", 2, 2, f_complete}, 6924 {"complete_add", 1, 1, f_complete_add}, 6925 {"complete_check", 0, 0, f_complete_check}, 6926 #endif 6927 {"confirm", 1, 4, f_confirm}, 6928 {"copy", 1, 1, f_copy}, 6929 {"count", 2, 4, f_count}, 6930 {"cscope_connection",0,3, f_cscope_connection}, 6931 {"cursor", 1, 3, f_cursor}, 6932 {"deepcopy", 1, 2, f_deepcopy}, 6933 {"delete", 1, 1, f_delete}, 6934 {"did_filetype", 0, 0, f_did_filetype}, 6935 {"diff_filler", 1, 1, f_diff_filler}, 6936 {"diff_hlID", 2, 2, f_diff_hlID}, 6937 {"empty", 1, 1, f_empty}, 6938 {"escape", 2, 2, f_escape}, 6939 {"eval", 1, 1, f_eval}, 6940 {"eventhandler", 0, 0, f_eventhandler}, 6941 {"executable", 1, 1, f_executable}, 6942 {"exists", 1, 1, f_exists}, 6943 {"expand", 1, 2, f_expand}, 6944 {"extend", 2, 3, f_extend}, 6945 {"file_readable", 1, 1, f_filereadable}, /* obsolete */ 6946 {"filereadable", 1, 1, f_filereadable}, 6947 {"filewritable", 1, 1, f_filewritable}, 6948 {"filter", 2, 2, f_filter}, 6949 {"finddir", 1, 3, f_finddir}, 6950 {"findfile", 1, 3, f_findfile}, 6951 {"fnamemodify", 2, 2, f_fnamemodify}, 6952 {"foldclosed", 1, 1, f_foldclosed}, 6953 {"foldclosedend", 1, 1, f_foldclosedend}, 6954 {"foldlevel", 1, 1, f_foldlevel}, 6955 {"foldtext", 0, 0, f_foldtext}, 6956 {"foldtextresult", 1, 1, f_foldtextresult}, 6957 {"foreground", 0, 0, f_foreground}, 6958 {"function", 1, 1, f_function}, 6959 {"garbagecollect", 0, 0, f_garbagecollect}, 6960 {"get", 2, 3, f_get}, 6961 {"getbufline", 2, 3, f_getbufline}, 6962 {"getbufvar", 2, 2, f_getbufvar}, 6963 {"getchar", 0, 1, f_getchar}, 6964 {"getcharmod", 0, 0, f_getcharmod}, 6965 {"getcmdline", 0, 0, f_getcmdline}, 6966 {"getcmdpos", 0, 0, f_getcmdpos}, 6967 {"getcmdtype", 0, 0, f_getcmdtype}, 6968 {"getcwd", 0, 0, f_getcwd}, 6969 {"getfontname", 0, 1, f_getfontname}, 6970 {"getfperm", 1, 1, f_getfperm}, 6971 {"getfsize", 1, 1, f_getfsize}, 6972 {"getftime", 1, 1, f_getftime}, 6973 {"getftype", 1, 1, f_getftype}, 6974 {"getline", 1, 2, f_getline}, 6975 {"getloclist", 1, 1, f_getqflist}, 6976 {"getpos", 1, 1, f_getpos}, 6977 {"getqflist", 0, 0, f_getqflist}, 6978 {"getreg", 0, 2, f_getreg}, 6979 {"getregtype", 0, 1, f_getregtype}, 6980 {"getwinposx", 0, 0, f_getwinposx}, 6981 {"getwinposy", 0, 0, f_getwinposy}, 6982 {"getwinvar", 2, 2, f_getwinvar}, 6983 {"glob", 1, 1, f_glob}, 6984 {"globpath", 2, 2, f_globpath}, 6985 {"has", 1, 1, f_has}, 6986 {"has_key", 2, 2, f_has_key}, 6987 {"hasmapto", 1, 3, f_hasmapto}, 6988 {"highlightID", 1, 1, f_hlID}, /* obsolete */ 6989 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */ 6990 {"histadd", 2, 2, f_histadd}, 6991 {"histdel", 1, 2, f_histdel}, 6992 {"histget", 1, 2, f_histget}, 6993 {"histnr", 1, 1, f_histnr}, 6994 {"hlID", 1, 1, f_hlID}, 6995 {"hlexists", 1, 1, f_hlexists}, 6996 {"hostname", 0, 0, f_hostname}, 6997 {"iconv", 3, 3, f_iconv}, 6998 {"indent", 1, 1, f_indent}, 6999 {"index", 2, 4, f_index}, 7000 {"input", 1, 3, f_input}, 7001 {"inputdialog", 1, 3, f_inputdialog}, 7002 {"inputlist", 1, 1, f_inputlist}, 7003 {"inputrestore", 0, 0, f_inputrestore}, 7004 {"inputsave", 0, 0, f_inputsave}, 7005 {"inputsecret", 1, 2, f_inputsecret}, 7006 {"insert", 2, 3, f_insert}, 7007 {"isdirectory", 1, 1, f_isdirectory}, 7008 {"islocked", 1, 1, f_islocked}, 7009 {"items", 1, 1, f_items}, 7010 {"join", 1, 2, f_join}, 7011 {"keys", 1, 1, f_keys}, 7012 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */ 7013 {"len", 1, 1, f_len}, 7014 {"libcall", 3, 3, f_libcall}, 7015 {"libcallnr", 3, 3, f_libcallnr}, 7016 {"line", 1, 1, f_line}, 7017 {"line2byte", 1, 1, f_line2byte}, 7018 {"lispindent", 1, 1, f_lispindent}, 7019 {"localtime", 0, 0, f_localtime}, 7020 {"map", 2, 2, f_map}, 7021 {"maparg", 1, 3, f_maparg}, 7022 {"mapcheck", 1, 3, f_mapcheck}, 7023 {"match", 2, 4, f_match}, 7024 {"matchend", 2, 4, f_matchend}, 7025 {"matchlist", 2, 4, f_matchlist}, 7026 {"matchstr", 2, 4, f_matchstr}, 7027 {"max", 1, 1, f_max}, 7028 {"min", 1, 1, f_min}, 7029 #ifdef vim_mkdir 7030 {"mkdir", 1, 3, f_mkdir}, 7031 #endif 7032 {"mode", 0, 0, f_mode}, 7033 {"nextnonblank", 1, 1, f_nextnonblank}, 7034 {"nr2char", 1, 1, f_nr2char}, 7035 {"prevnonblank", 1, 1, f_prevnonblank}, 7036 {"printf", 2, 19, f_printf}, 7037 {"pumvisible", 0, 0, f_pumvisible}, 7038 {"range", 1, 3, f_range}, 7039 {"readfile", 1, 3, f_readfile}, 7040 {"reltime", 0, 2, f_reltime}, 7041 {"reltimestr", 1, 1, f_reltimestr}, 7042 {"remote_expr", 2, 3, f_remote_expr}, 7043 {"remote_foreground", 1, 1, f_remote_foreground}, 7044 {"remote_peek", 1, 2, f_remote_peek}, 7045 {"remote_read", 1, 1, f_remote_read}, 7046 {"remote_send", 2, 3, f_remote_send}, 7047 {"remove", 2, 3, f_remove}, 7048 {"rename", 2, 2, f_rename}, 7049 {"repeat", 2, 2, f_repeat}, 7050 {"resolve", 1, 1, f_resolve}, 7051 {"reverse", 1, 1, f_reverse}, 7052 {"search", 1, 3, f_search}, 7053 {"searchdecl", 1, 3, f_searchdecl}, 7054 {"searchpair", 3, 6, f_searchpair}, 7055 {"searchpairpos", 3, 6, f_searchpairpos}, 7056 {"searchpos", 1, 3, f_searchpos}, 7057 {"server2client", 2, 2, f_server2client}, 7058 {"serverlist", 0, 0, f_serverlist}, 7059 {"setbufvar", 3, 3, f_setbufvar}, 7060 {"setcmdpos", 1, 1, f_setcmdpos}, 7061 {"setline", 2, 2, f_setline}, 7062 {"setloclist", 2, 3, f_setloclist}, 7063 {"setpos", 2, 2, f_setpos}, 7064 {"setqflist", 1, 2, f_setqflist}, 7065 {"setreg", 2, 3, f_setreg}, 7066 {"setwinvar", 3, 3, f_setwinvar}, 7067 {"simplify", 1, 1, f_simplify}, 7068 {"sort", 1, 2, f_sort}, 7069 {"soundfold", 1, 1, f_soundfold}, 7070 {"spellbadword", 0, 1, f_spellbadword}, 7071 {"spellsuggest", 1, 3, f_spellsuggest}, 7072 {"split", 1, 3, f_split}, 7073 {"str2nr", 1, 2, f_str2nr}, 7074 #ifdef HAVE_STRFTIME 7075 {"strftime", 1, 2, f_strftime}, 7076 #endif 7077 {"stridx", 2, 3, f_stridx}, 7078 {"string", 1, 1, f_string}, 7079 {"strlen", 1, 1, f_strlen}, 7080 {"strpart", 2, 3, f_strpart}, 7081 {"strridx", 2, 3, f_strridx}, 7082 {"strtrans", 1, 1, f_strtrans}, 7083 {"submatch", 1, 1, f_submatch}, 7084 {"substitute", 4, 4, f_substitute}, 7085 {"synID", 3, 3, f_synID}, 7086 {"synIDattr", 2, 3, f_synIDattr}, 7087 {"synIDtrans", 1, 1, f_synIDtrans}, 7088 {"system", 1, 2, f_system}, 7089 {"tabpagebuflist", 0, 1, f_tabpagebuflist}, 7090 {"tabpagenr", 0, 1, f_tabpagenr}, 7091 {"tabpagewinnr", 1, 2, f_tabpagewinnr}, 7092 {"tagfiles", 0, 0, f_tagfiles}, 7093 {"taglist", 1, 1, f_taglist}, 7094 {"tempname", 0, 0, f_tempname}, 7095 {"test", 1, 1, f_test}, 7096 {"tolower", 1, 1, f_tolower}, 7097 {"toupper", 1, 1, f_toupper}, 7098 {"tr", 3, 3, f_tr}, 7099 {"type", 1, 1, f_type}, 7100 {"values", 1, 1, f_values}, 7101 {"virtcol", 1, 1, f_virtcol}, 7102 {"visualmode", 0, 1, f_visualmode}, 7103 {"winbufnr", 1, 1, f_winbufnr}, 7104 {"wincol", 0, 0, f_wincol}, 7105 {"winheight", 1, 1, f_winheight}, 7106 {"winline", 0, 0, f_winline}, 7107 {"winnr", 0, 1, f_winnr}, 7108 {"winrestcmd", 0, 0, f_winrestcmd}, 7109 {"winrestview", 1, 1, f_winrestview}, 7110 {"winsaveview", 0, 0, f_winsaveview}, 7111 {"winwidth", 1, 1, f_winwidth}, 7112 {"writefile", 2, 3, f_writefile}, 7113 }; 7114 7115 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 7116 7117 /* 7118 * Function given to ExpandGeneric() to obtain the list of internal 7119 * or user defined function names. 7120 */ 7121 char_u * 7122 get_function_name(xp, idx) 7123 expand_T *xp; 7124 int idx; 7125 { 7126 static int intidx = -1; 7127 char_u *name; 7128 7129 if (idx == 0) 7130 intidx = -1; 7131 if (intidx < 0) 7132 { 7133 name = get_user_func_name(xp, idx); 7134 if (name != NULL) 7135 return name; 7136 } 7137 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst))) 7138 { 7139 STRCPY(IObuff, functions[intidx].f_name); 7140 STRCAT(IObuff, "("); 7141 if (functions[intidx].f_max_argc == 0) 7142 STRCAT(IObuff, ")"); 7143 return IObuff; 7144 } 7145 7146 return NULL; 7147 } 7148 7149 /* 7150 * Function given to ExpandGeneric() to obtain the list of internal or 7151 * user defined variable or function names. 7152 */ 7153 /*ARGSUSED*/ 7154 char_u * 7155 get_expr_name(xp, idx) 7156 expand_T *xp; 7157 int idx; 7158 { 7159 static int intidx = -1; 7160 char_u *name; 7161 7162 if (idx == 0) 7163 intidx = -1; 7164 if (intidx < 0) 7165 { 7166 name = get_function_name(xp, idx); 7167 if (name != NULL) 7168 return name; 7169 } 7170 return get_user_var_name(xp, ++intidx); 7171 } 7172 7173 #endif /* FEAT_CMDL_COMPL */ 7174 7175 /* 7176 * Find internal function in table above. 7177 * Return index, or -1 if not found 7178 */ 7179 static int 7180 find_internal_func(name) 7181 char_u *name; /* name of the function */ 7182 { 7183 int first = 0; 7184 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1; 7185 int cmp; 7186 int x; 7187 7188 /* 7189 * Find the function name in the table. Binary search. 7190 */ 7191 while (first <= last) 7192 { 7193 x = first + ((unsigned)(last - first) >> 1); 7194 cmp = STRCMP(name, functions[x].f_name); 7195 if (cmp < 0) 7196 last = x - 1; 7197 else if (cmp > 0) 7198 first = x + 1; 7199 else 7200 return x; 7201 } 7202 return -1; 7203 } 7204 7205 /* 7206 * Check if "name" is a variable of type VAR_FUNC. If so, return the function 7207 * name it contains, otherwise return "name". 7208 */ 7209 static char_u * 7210 deref_func_name(name, lenp) 7211 char_u *name; 7212 int *lenp; 7213 { 7214 dictitem_T *v; 7215 int cc; 7216 7217 cc = name[*lenp]; 7218 name[*lenp] = NUL; 7219 v = find_var(name, NULL); 7220 name[*lenp] = cc; 7221 if (v != NULL && v->di_tv.v_type == VAR_FUNC) 7222 { 7223 if (v->di_tv.vval.v_string == NULL) 7224 { 7225 *lenp = 0; 7226 return (char_u *)""; /* just in case */ 7227 } 7228 *lenp = STRLEN(v->di_tv.vval.v_string); 7229 return v->di_tv.vval.v_string; 7230 } 7231 7232 return name; 7233 } 7234 7235 /* 7236 * Allocate a variable for the result of a function. 7237 * Return OK or FAIL. 7238 */ 7239 static int 7240 get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange, 7241 evaluate, selfdict) 7242 char_u *name; /* name of the function */ 7243 int len; /* length of "name" */ 7244 typval_T *rettv; 7245 char_u **arg; /* argument, pointing to the '(' */ 7246 linenr_T firstline; /* first line of range */ 7247 linenr_T lastline; /* last line of range */ 7248 int *doesrange; /* return: function handled range */ 7249 int evaluate; 7250 dict_T *selfdict; /* Dictionary for "self" */ 7251 { 7252 char_u *argp; 7253 int ret = OK; 7254 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */ 7255 int argcount = 0; /* number of arguments found */ 7256 7257 /* 7258 * Get the arguments. 7259 */ 7260 argp = *arg; 7261 while (argcount < MAX_FUNC_ARGS) 7262 { 7263 argp = skipwhite(argp + 1); /* skip the '(' or ',' */ 7264 if (*argp == ')' || *argp == ',' || *argp == NUL) 7265 break; 7266 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL) 7267 { 7268 ret = FAIL; 7269 break; 7270 } 7271 ++argcount; 7272 if (*argp != ',') 7273 break; 7274 } 7275 if (*argp == ')') 7276 ++argp; 7277 else 7278 ret = FAIL; 7279 7280 if (ret == OK) 7281 ret = call_func(name, len, rettv, argcount, argvars, 7282 firstline, lastline, doesrange, evaluate, selfdict); 7283 else if (!aborting()) 7284 { 7285 if (argcount == MAX_FUNC_ARGS) 7286 emsg_funcname("E740: Too many arguments for function %s", name); 7287 else 7288 emsg_funcname("E116: Invalid arguments for function %s", name); 7289 } 7290 7291 while (--argcount >= 0) 7292 clear_tv(&argvars[argcount]); 7293 7294 *arg = skipwhite(argp); 7295 return ret; 7296 } 7297 7298 7299 /* 7300 * Call a function with its resolved parameters 7301 * Return OK when the function can't be called, FAIL otherwise. 7302 * Also returns OK when an error was encountered while executing the function. 7303 */ 7304 static int 7305 call_func(name, len, rettv, argcount, argvars, firstline, lastline, 7306 doesrange, evaluate, selfdict) 7307 char_u *name; /* name of the function */ 7308 int len; /* length of "name" */ 7309 typval_T *rettv; /* return value goes here */ 7310 int argcount; /* number of "argvars" */ 7311 typval_T *argvars; /* vars for arguments */ 7312 linenr_T firstline; /* first line of range */ 7313 linenr_T lastline; /* last line of range */ 7314 int *doesrange; /* return: function handled range */ 7315 int evaluate; 7316 dict_T *selfdict; /* Dictionary for "self" */ 7317 { 7318 int ret = FAIL; 7319 #define ERROR_UNKNOWN 0 7320 #define ERROR_TOOMANY 1 7321 #define ERROR_TOOFEW 2 7322 #define ERROR_SCRIPT 3 7323 #define ERROR_DICT 4 7324 #define ERROR_NONE 5 7325 #define ERROR_OTHER 6 7326 int error = ERROR_NONE; 7327 int i; 7328 int llen; 7329 ufunc_T *fp; 7330 int cc; 7331 #define FLEN_FIXED 40 7332 char_u fname_buf[FLEN_FIXED + 1]; 7333 char_u *fname; 7334 7335 /* 7336 * In a script change <SID>name() and s:name() to K_SNR 123_name(). 7337 * Change <SNR>123_name() to K_SNR 123_name(). 7338 * Use fname_buf[] when it fits, otherwise allocate memory (slow). 7339 */ 7340 cc = name[len]; 7341 name[len] = NUL; 7342 llen = eval_fname_script(name); 7343 if (llen > 0) 7344 { 7345 fname_buf[0] = K_SPECIAL; 7346 fname_buf[1] = KS_EXTRA; 7347 fname_buf[2] = (int)KE_SNR; 7348 i = 3; 7349 if (eval_fname_sid(name)) /* "<SID>" or "s:" */ 7350 { 7351 if (current_SID <= 0) 7352 error = ERROR_SCRIPT; 7353 else 7354 { 7355 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID); 7356 i = (int)STRLEN(fname_buf); 7357 } 7358 } 7359 if (i + STRLEN(name + llen) < FLEN_FIXED) 7360 { 7361 STRCPY(fname_buf + i, name + llen); 7362 fname = fname_buf; 7363 } 7364 else 7365 { 7366 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1)); 7367 if (fname == NULL) 7368 error = ERROR_OTHER; 7369 else 7370 { 7371 mch_memmove(fname, fname_buf, (size_t)i); 7372 STRCPY(fname + i, name + llen); 7373 } 7374 } 7375 } 7376 else 7377 fname = name; 7378 7379 *doesrange = FALSE; 7380 7381 7382 /* execute the function if no errors detected and executing */ 7383 if (evaluate && error == ERROR_NONE) 7384 { 7385 rettv->v_type = VAR_NUMBER; /* default is number rettv */ 7386 error = ERROR_UNKNOWN; 7387 7388 if (!builtin_function(fname)) 7389 { 7390 /* 7391 * User defined function. 7392 */ 7393 fp = find_func(fname); 7394 7395 #ifdef FEAT_AUTOCMD 7396 /* Trigger FuncUndefined event, may load the function. */ 7397 if (fp == NULL 7398 && apply_autocmds(EVENT_FUNCUNDEFINED, 7399 fname, fname, TRUE, NULL) 7400 && !aborting()) 7401 { 7402 /* executed an autocommand, search for the function again */ 7403 fp = find_func(fname); 7404 } 7405 #endif 7406 /* Try loading a package. */ 7407 if (fp == NULL && script_autoload(fname, TRUE) && !aborting()) 7408 { 7409 /* loaded a package, search for the function again */ 7410 fp = find_func(fname); 7411 } 7412 7413 if (fp != NULL) 7414 { 7415 if (fp->uf_flags & FC_RANGE) 7416 *doesrange = TRUE; 7417 if (argcount < fp->uf_args.ga_len) 7418 error = ERROR_TOOFEW; 7419 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len) 7420 error = ERROR_TOOMANY; 7421 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL) 7422 error = ERROR_DICT; 7423 else 7424 { 7425 /* 7426 * Call the user function. 7427 * Save and restore search patterns, script variables and 7428 * redo buffer. 7429 */ 7430 save_search_patterns(); 7431 saveRedobuff(); 7432 ++fp->uf_calls; 7433 call_user_func(fp, argcount, argvars, rettv, 7434 firstline, lastline, 7435 (fp->uf_flags & FC_DICT) ? selfdict : NULL); 7436 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name) 7437 && fp->uf_refcount <= 0) 7438 /* Function was unreferenced while being used, free it 7439 * now. */ 7440 func_free(fp); 7441 restoreRedobuff(); 7442 restore_search_patterns(); 7443 error = ERROR_NONE; 7444 } 7445 } 7446 } 7447 else 7448 { 7449 /* 7450 * Find the function name in the table, call its implementation. 7451 */ 7452 i = find_internal_func(fname); 7453 if (i >= 0) 7454 { 7455 if (argcount < functions[i].f_min_argc) 7456 error = ERROR_TOOFEW; 7457 else if (argcount > functions[i].f_max_argc) 7458 error = ERROR_TOOMANY; 7459 else 7460 { 7461 argvars[argcount].v_type = VAR_UNKNOWN; 7462 functions[i].f_func(argvars, rettv); 7463 error = ERROR_NONE; 7464 } 7465 } 7466 } 7467 /* 7468 * The function call (or "FuncUndefined" autocommand sequence) might 7469 * have been aborted by an error, an interrupt, or an explicitly thrown 7470 * exception that has not been caught so far. This situation can be 7471 * tested for by calling aborting(). For an error in an internal 7472 * function or for the "E132" error in call_user_func(), however, the 7473 * throw point at which the "force_abort" flag (temporarily reset by 7474 * emsg()) is normally updated has not been reached yet. We need to 7475 * update that flag first to make aborting() reliable. 7476 */ 7477 update_force_abort(); 7478 } 7479 if (error == ERROR_NONE) 7480 ret = OK; 7481 7482 /* 7483 * Report an error unless the argument evaluation or function call has been 7484 * cancelled due to an aborting error, an interrupt, or an exception. 7485 */ 7486 if (!aborting()) 7487 { 7488 switch (error) 7489 { 7490 case ERROR_UNKNOWN: 7491 emsg_funcname("E117: Unknown function: %s", name); 7492 break; 7493 case ERROR_TOOMANY: 7494 emsg_funcname(e_toomanyarg, name); 7495 break; 7496 case ERROR_TOOFEW: 7497 emsg_funcname("E119: Not enough arguments for function: %s", 7498 name); 7499 break; 7500 case ERROR_SCRIPT: 7501 emsg_funcname("E120: Using <SID> not in a script context: %s", 7502 name); 7503 break; 7504 case ERROR_DICT: 7505 emsg_funcname("E725: Calling dict function without Dictionary: %s", 7506 name); 7507 break; 7508 } 7509 } 7510 7511 name[len] = cc; 7512 if (fname != name && fname != fname_buf) 7513 vim_free(fname); 7514 7515 return ret; 7516 } 7517 7518 /* 7519 * Give an error message with a function name. Handle <SNR> things. 7520 */ 7521 static void 7522 emsg_funcname(msg, name) 7523 char *msg; 7524 char_u *name; 7525 { 7526 char_u *p; 7527 7528 if (*name == K_SPECIAL) 7529 p = concat_str((char_u *)"<SNR>", name + 3); 7530 else 7531 p = name; 7532 EMSG2(_(msg), p); 7533 if (p != name) 7534 vim_free(p); 7535 } 7536 7537 /********************************************* 7538 * Implementation of the built-in functions 7539 */ 7540 7541 /* 7542 * "add(list, item)" function 7543 */ 7544 static void 7545 f_add(argvars, rettv) 7546 typval_T *argvars; 7547 typval_T *rettv; 7548 { 7549 list_T *l; 7550 7551 rettv->vval.v_number = 1; /* Default: Failed */ 7552 if (argvars[0].v_type == VAR_LIST) 7553 { 7554 if ((l = argvars[0].vval.v_list) != NULL 7555 && !tv_check_lock(l->lv_lock, (char_u *)"add()") 7556 && list_append_tv(l, &argvars[1]) == OK) 7557 copy_tv(&argvars[0], rettv); 7558 } 7559 else 7560 EMSG(_(e_listreq)); 7561 } 7562 7563 /* 7564 * "append(lnum, string/list)" function 7565 */ 7566 static void 7567 f_append(argvars, rettv) 7568 typval_T *argvars; 7569 typval_T *rettv; 7570 { 7571 long lnum; 7572 char_u *line; 7573 list_T *l = NULL; 7574 listitem_T *li = NULL; 7575 typval_T *tv; 7576 long added = 0; 7577 7578 lnum = get_tv_lnum(argvars); 7579 if (lnum >= 0 7580 && lnum <= curbuf->b_ml.ml_line_count 7581 && u_save(lnum, lnum + 1) == OK) 7582 { 7583 if (argvars[1].v_type == VAR_LIST) 7584 { 7585 l = argvars[1].vval.v_list; 7586 if (l == NULL) 7587 return; 7588 li = l->lv_first; 7589 } 7590 rettv->vval.v_number = 0; /* Default: Success */ 7591 for (;;) 7592 { 7593 if (l == NULL) 7594 tv = &argvars[1]; /* append a string */ 7595 else if (li == NULL) 7596 break; /* end of list */ 7597 else 7598 tv = &li->li_tv; /* append item from list */ 7599 line = get_tv_string_chk(tv); 7600 if (line == NULL) /* type error */ 7601 { 7602 rettv->vval.v_number = 1; /* Failed */ 7603 break; 7604 } 7605 ml_append(lnum + added, line, (colnr_T)0, FALSE); 7606 ++added; 7607 if (l == NULL) 7608 break; 7609 li = li->li_next; 7610 } 7611 7612 appended_lines_mark(lnum, added); 7613 if (curwin->w_cursor.lnum > lnum) 7614 curwin->w_cursor.lnum += added; 7615 } 7616 else 7617 rettv->vval.v_number = 1; /* Failed */ 7618 } 7619 7620 /* 7621 * "argc()" function 7622 */ 7623 /* ARGSUSED */ 7624 static void 7625 f_argc(argvars, rettv) 7626 typval_T *argvars; 7627 typval_T *rettv; 7628 { 7629 rettv->vval.v_number = ARGCOUNT; 7630 } 7631 7632 /* 7633 * "argidx()" function 7634 */ 7635 /* ARGSUSED */ 7636 static void 7637 f_argidx(argvars, rettv) 7638 typval_T *argvars; 7639 typval_T *rettv; 7640 { 7641 rettv->vval.v_number = curwin->w_arg_idx; 7642 } 7643 7644 /* 7645 * "argv(nr)" function 7646 */ 7647 static void 7648 f_argv(argvars, rettv) 7649 typval_T *argvars; 7650 typval_T *rettv; 7651 { 7652 int idx; 7653 7654 idx = get_tv_number_chk(&argvars[0], NULL); 7655 if (idx >= 0 && idx < ARGCOUNT) 7656 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx])); 7657 else 7658 rettv->vval.v_string = NULL; 7659 rettv->v_type = VAR_STRING; 7660 } 7661 7662 /* 7663 * "browse(save, title, initdir, default)" function 7664 */ 7665 /* ARGSUSED */ 7666 static void 7667 f_browse(argvars, rettv) 7668 typval_T *argvars; 7669 typval_T *rettv; 7670 { 7671 #ifdef FEAT_BROWSE 7672 int save; 7673 char_u *title; 7674 char_u *initdir; 7675 char_u *defname; 7676 char_u buf[NUMBUFLEN]; 7677 char_u buf2[NUMBUFLEN]; 7678 int error = FALSE; 7679 7680 save = get_tv_number_chk(&argvars[0], &error); 7681 title = get_tv_string_chk(&argvars[1]); 7682 initdir = get_tv_string_buf_chk(&argvars[2], buf); 7683 defname = get_tv_string_buf_chk(&argvars[3], buf2); 7684 7685 if (error || title == NULL || initdir == NULL || defname == NULL) 7686 rettv->vval.v_string = NULL; 7687 else 7688 rettv->vval.v_string = 7689 do_browse(save ? BROWSE_SAVE : 0, 7690 title, defname, NULL, initdir, NULL, curbuf); 7691 #else 7692 rettv->vval.v_string = NULL; 7693 #endif 7694 rettv->v_type = VAR_STRING; 7695 } 7696 7697 /* 7698 * "browsedir(title, initdir)" function 7699 */ 7700 /* ARGSUSED */ 7701 static void 7702 f_browsedir(argvars, rettv) 7703 typval_T *argvars; 7704 typval_T *rettv; 7705 { 7706 #ifdef FEAT_BROWSE 7707 char_u *title; 7708 char_u *initdir; 7709 char_u buf[NUMBUFLEN]; 7710 7711 title = get_tv_string_chk(&argvars[0]); 7712 initdir = get_tv_string_buf_chk(&argvars[1], buf); 7713 7714 if (title == NULL || initdir == NULL) 7715 rettv->vval.v_string = NULL; 7716 else 7717 rettv->vval.v_string = do_browse(BROWSE_DIR, 7718 title, NULL, NULL, initdir, NULL, curbuf); 7719 #else 7720 rettv->vval.v_string = NULL; 7721 #endif 7722 rettv->v_type = VAR_STRING; 7723 } 7724 7725 static buf_T *find_buffer __ARGS((typval_T *avar)); 7726 7727 /* 7728 * Find a buffer by number or exact name. 7729 */ 7730 static buf_T * 7731 find_buffer(avar) 7732 typval_T *avar; 7733 { 7734 buf_T *buf = NULL; 7735 7736 if (avar->v_type == VAR_NUMBER) 7737 buf = buflist_findnr((int)avar->vval.v_number); 7738 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL) 7739 { 7740 buf = buflist_findname_exp(avar->vval.v_string); 7741 if (buf == NULL) 7742 { 7743 /* No full path name match, try a match with a URL or a "nofile" 7744 * buffer, these don't use the full path. */ 7745 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 7746 if (buf->b_fname != NULL 7747 && (path_with_url(buf->b_fname) 7748 #ifdef FEAT_QUICKFIX 7749 || bt_nofile(buf) 7750 #endif 7751 ) 7752 && STRCMP(buf->b_fname, avar->vval.v_string) == 0) 7753 break; 7754 } 7755 } 7756 return buf; 7757 } 7758 7759 /* 7760 * "bufexists(expr)" function 7761 */ 7762 static void 7763 f_bufexists(argvars, rettv) 7764 typval_T *argvars; 7765 typval_T *rettv; 7766 { 7767 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL); 7768 } 7769 7770 /* 7771 * "buflisted(expr)" function 7772 */ 7773 static void 7774 f_buflisted(argvars, rettv) 7775 typval_T *argvars; 7776 typval_T *rettv; 7777 { 7778 buf_T *buf; 7779 7780 buf = find_buffer(&argvars[0]); 7781 rettv->vval.v_number = (buf != NULL && buf->b_p_bl); 7782 } 7783 7784 /* 7785 * "bufloaded(expr)" function 7786 */ 7787 static void 7788 f_bufloaded(argvars, rettv) 7789 typval_T *argvars; 7790 typval_T *rettv; 7791 { 7792 buf_T *buf; 7793 7794 buf = find_buffer(&argvars[0]); 7795 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL); 7796 } 7797 7798 static buf_T *get_buf_tv __ARGS((typval_T *tv)); 7799 7800 /* 7801 * Get buffer by number or pattern. 7802 */ 7803 static buf_T * 7804 get_buf_tv(tv) 7805 typval_T *tv; 7806 { 7807 char_u *name = tv->vval.v_string; 7808 int save_magic; 7809 char_u *save_cpo; 7810 buf_T *buf; 7811 7812 if (tv->v_type == VAR_NUMBER) 7813 return buflist_findnr((int)tv->vval.v_number); 7814 if (tv->v_type != VAR_STRING) 7815 return NULL; 7816 if (name == NULL || *name == NUL) 7817 return curbuf; 7818 if (name[0] == '$' && name[1] == NUL) 7819 return lastbuf; 7820 7821 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */ 7822 save_magic = p_magic; 7823 p_magic = TRUE; 7824 save_cpo = p_cpo; 7825 p_cpo = (char_u *)""; 7826 7827 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name), 7828 TRUE, FALSE)); 7829 7830 p_magic = save_magic; 7831 p_cpo = save_cpo; 7832 7833 /* If not found, try expanding the name, like done for bufexists(). */ 7834 if (buf == NULL) 7835 buf = find_buffer(tv); 7836 7837 return buf; 7838 } 7839 7840 /* 7841 * "bufname(expr)" function 7842 */ 7843 static void 7844 f_bufname(argvars, rettv) 7845 typval_T *argvars; 7846 typval_T *rettv; 7847 { 7848 buf_T *buf; 7849 7850 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 7851 ++emsg_off; 7852 buf = get_buf_tv(&argvars[0]); 7853 rettv->v_type = VAR_STRING; 7854 if (buf != NULL && buf->b_fname != NULL) 7855 rettv->vval.v_string = vim_strsave(buf->b_fname); 7856 else 7857 rettv->vval.v_string = NULL; 7858 --emsg_off; 7859 } 7860 7861 /* 7862 * "bufnr(expr)" function 7863 */ 7864 static void 7865 f_bufnr(argvars, rettv) 7866 typval_T *argvars; 7867 typval_T *rettv; 7868 { 7869 buf_T *buf; 7870 int error = FALSE; 7871 char_u *name; 7872 7873 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 7874 ++emsg_off; 7875 buf = get_buf_tv(&argvars[0]); 7876 --emsg_off; 7877 7878 /* If the buffer isn't found and the second argument is not zero create a 7879 * new buffer. */ 7880 if (buf == NULL 7881 && argvars[1].v_type != VAR_UNKNOWN 7882 && get_tv_number_chk(&argvars[1], &error) != 0 7883 && !error 7884 && (name = get_tv_string_chk(&argvars[0])) != NULL 7885 && !error) 7886 buf = buflist_new(name, NULL, (linenr_T)1, 0); 7887 7888 if (buf != NULL) 7889 rettv->vval.v_number = buf->b_fnum; 7890 else 7891 rettv->vval.v_number = -1; 7892 } 7893 7894 /* 7895 * "bufwinnr(nr)" function 7896 */ 7897 static void 7898 f_bufwinnr(argvars, rettv) 7899 typval_T *argvars; 7900 typval_T *rettv; 7901 { 7902 #ifdef FEAT_WINDOWS 7903 win_T *wp; 7904 int winnr = 0; 7905 #endif 7906 buf_T *buf; 7907 7908 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 7909 ++emsg_off; 7910 buf = get_buf_tv(&argvars[0]); 7911 #ifdef FEAT_WINDOWS 7912 for (wp = firstwin; wp; wp = wp->w_next) 7913 { 7914 ++winnr; 7915 if (wp->w_buffer == buf) 7916 break; 7917 } 7918 rettv->vval.v_number = (wp != NULL ? winnr : -1); 7919 #else 7920 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1); 7921 #endif 7922 --emsg_off; 7923 } 7924 7925 /* 7926 * "byte2line(byte)" function 7927 */ 7928 /*ARGSUSED*/ 7929 static void 7930 f_byte2line(argvars, rettv) 7931 typval_T *argvars; 7932 typval_T *rettv; 7933 { 7934 #ifndef FEAT_BYTEOFF 7935 rettv->vval.v_number = -1; 7936 #else 7937 long boff = 0; 7938 7939 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */ 7940 if (boff < 0) 7941 rettv->vval.v_number = -1; 7942 else 7943 rettv->vval.v_number = ml_find_line_or_offset(curbuf, 7944 (linenr_T)0, &boff); 7945 #endif 7946 } 7947 7948 /* 7949 * "byteidx()" function 7950 */ 7951 /*ARGSUSED*/ 7952 static void 7953 f_byteidx(argvars, rettv) 7954 typval_T *argvars; 7955 typval_T *rettv; 7956 { 7957 #ifdef FEAT_MBYTE 7958 char_u *t; 7959 #endif 7960 char_u *str; 7961 long idx; 7962 7963 str = get_tv_string_chk(&argvars[0]); 7964 idx = get_tv_number_chk(&argvars[1], NULL); 7965 rettv->vval.v_number = -1; 7966 if (str == NULL || idx < 0) 7967 return; 7968 7969 #ifdef FEAT_MBYTE 7970 t = str; 7971 for ( ; idx > 0; idx--) 7972 { 7973 if (*t == NUL) /* EOL reached */ 7974 return; 7975 t += (*mb_ptr2len)(t); 7976 } 7977 rettv->vval.v_number = t - str; 7978 #else 7979 if (idx <= STRLEN(str)) 7980 rettv->vval.v_number = idx; 7981 #endif 7982 } 7983 7984 /* 7985 * "call(func, arglist)" function 7986 */ 7987 static void 7988 f_call(argvars, rettv) 7989 typval_T *argvars; 7990 typval_T *rettv; 7991 { 7992 char_u *func; 7993 typval_T argv[MAX_FUNC_ARGS]; 7994 int argc = 0; 7995 listitem_T *item; 7996 int dummy; 7997 dict_T *selfdict = NULL; 7998 7999 rettv->vval.v_number = 0; 8000 if (argvars[1].v_type != VAR_LIST) 8001 { 8002 EMSG(_(e_listreq)); 8003 return; 8004 } 8005 if (argvars[1].vval.v_list == NULL) 8006 return; 8007 8008 if (argvars[0].v_type == VAR_FUNC) 8009 func = argvars[0].vval.v_string; 8010 else 8011 func = get_tv_string(&argvars[0]); 8012 if (*func == NUL) 8013 return; /* type error or empty name */ 8014 8015 if (argvars[2].v_type != VAR_UNKNOWN) 8016 { 8017 if (argvars[2].v_type != VAR_DICT) 8018 { 8019 EMSG(_(e_dictreq)); 8020 return; 8021 } 8022 selfdict = argvars[2].vval.v_dict; 8023 } 8024 8025 for (item = argvars[1].vval.v_list->lv_first; item != NULL; 8026 item = item->li_next) 8027 { 8028 if (argc == MAX_FUNC_ARGS) 8029 { 8030 EMSG(_("E699: Too many arguments")); 8031 break; 8032 } 8033 /* Make a copy of each argument. This is needed to be able to set 8034 * v_lock to VAR_FIXED in the copy without changing the original list. 8035 */ 8036 copy_tv(&item->li_tv, &argv[argc++]); 8037 } 8038 8039 if (item == NULL) 8040 (void)call_func(func, STRLEN(func), rettv, argc, argv, 8041 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 8042 &dummy, TRUE, selfdict); 8043 8044 /* Free the arguments. */ 8045 while (argc > 0) 8046 clear_tv(&argv[--argc]); 8047 } 8048 8049 /* 8050 * "changenr()" function 8051 */ 8052 /*ARGSUSED*/ 8053 static void 8054 f_changenr(argvars, rettv) 8055 typval_T *argvars; 8056 typval_T *rettv; 8057 { 8058 rettv->vval.v_number = curbuf->b_u_seq_cur; 8059 } 8060 8061 /* 8062 * "char2nr(string)" function 8063 */ 8064 static void 8065 f_char2nr(argvars, rettv) 8066 typval_T *argvars; 8067 typval_T *rettv; 8068 { 8069 #ifdef FEAT_MBYTE 8070 if (has_mbyte) 8071 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0])); 8072 else 8073 #endif 8074 rettv->vval.v_number = get_tv_string(&argvars[0])[0]; 8075 } 8076 8077 /* 8078 * "cindent(lnum)" function 8079 */ 8080 static void 8081 f_cindent(argvars, rettv) 8082 typval_T *argvars; 8083 typval_T *rettv; 8084 { 8085 #ifdef FEAT_CINDENT 8086 pos_T pos; 8087 linenr_T lnum; 8088 8089 pos = curwin->w_cursor; 8090 lnum = get_tv_lnum(argvars); 8091 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 8092 { 8093 curwin->w_cursor.lnum = lnum; 8094 rettv->vval.v_number = get_c_indent(); 8095 curwin->w_cursor = pos; 8096 } 8097 else 8098 #endif 8099 rettv->vval.v_number = -1; 8100 } 8101 8102 /* 8103 * "col(string)" function 8104 */ 8105 static void 8106 f_col(argvars, rettv) 8107 typval_T *argvars; 8108 typval_T *rettv; 8109 { 8110 colnr_T col = 0; 8111 pos_T *fp; 8112 int fnum = curbuf->b_fnum; 8113 8114 fp = var2fpos(&argvars[0], FALSE, &fnum); 8115 if (fp != NULL && fnum == curbuf->b_fnum) 8116 { 8117 if (fp->col == MAXCOL) 8118 { 8119 /* '> can be MAXCOL, get the length of the line then */ 8120 if (fp->lnum <= curbuf->b_ml.ml_line_count) 8121 col = STRLEN(ml_get(fp->lnum)) + 1; 8122 else 8123 col = MAXCOL; 8124 } 8125 else 8126 { 8127 col = fp->col + 1; 8128 #ifdef FEAT_VIRTUALEDIT 8129 /* col(".") when the cursor is on the NUL at the end of the line 8130 * because of "coladd" can be seen as an extra column. */ 8131 if (virtual_active() && fp == &curwin->w_cursor) 8132 { 8133 char_u *p = ml_get_cursor(); 8134 8135 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p, 8136 curwin->w_virtcol - curwin->w_cursor.coladd)) 8137 { 8138 # ifdef FEAT_MBYTE 8139 int l; 8140 8141 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL) 8142 col += l; 8143 # else 8144 if (*p != NUL && p[1] == NUL) 8145 ++col; 8146 # endif 8147 } 8148 } 8149 #endif 8150 } 8151 } 8152 rettv->vval.v_number = col; 8153 } 8154 8155 #if defined(FEAT_INS_EXPAND) 8156 /* 8157 * "complete()" function 8158 */ 8159 /*ARGSUSED*/ 8160 static void 8161 f_complete(argvars, rettv) 8162 typval_T *argvars; 8163 typval_T *rettv; 8164 { 8165 int startcol; 8166 8167 if ((State & INSERT) == 0) 8168 { 8169 EMSG(_("E785: complete() can only be used in Insert mode")); 8170 return; 8171 } 8172 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL) 8173 { 8174 EMSG(_(e_invarg)); 8175 return; 8176 } 8177 8178 startcol = get_tv_number_chk(&argvars[0], NULL); 8179 if (startcol <= 0) 8180 return; 8181 8182 set_completion(startcol - 1, argvars[1].vval.v_list); 8183 } 8184 8185 /* 8186 * "complete_add()" function 8187 */ 8188 /*ARGSUSED*/ 8189 static void 8190 f_complete_add(argvars, rettv) 8191 typval_T *argvars; 8192 typval_T *rettv; 8193 { 8194 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0); 8195 } 8196 8197 /* 8198 * "complete_check()" function 8199 */ 8200 /*ARGSUSED*/ 8201 static void 8202 f_complete_check(argvars, rettv) 8203 typval_T *argvars; 8204 typval_T *rettv; 8205 { 8206 int saved = RedrawingDisabled; 8207 8208 RedrawingDisabled = 0; 8209 ins_compl_check_keys(0); 8210 rettv->vval.v_number = compl_interrupted; 8211 RedrawingDisabled = saved; 8212 } 8213 #endif 8214 8215 /* 8216 * "confirm(message, buttons[, default [, type]])" function 8217 */ 8218 /*ARGSUSED*/ 8219 static void 8220 f_confirm(argvars, rettv) 8221 typval_T *argvars; 8222 typval_T *rettv; 8223 { 8224 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 8225 char_u *message; 8226 char_u *buttons = NULL; 8227 char_u buf[NUMBUFLEN]; 8228 char_u buf2[NUMBUFLEN]; 8229 int def = 1; 8230 int type = VIM_GENERIC; 8231 char_u *typestr; 8232 int error = FALSE; 8233 8234 message = get_tv_string_chk(&argvars[0]); 8235 if (message == NULL) 8236 error = TRUE; 8237 if (argvars[1].v_type != VAR_UNKNOWN) 8238 { 8239 buttons = get_tv_string_buf_chk(&argvars[1], buf); 8240 if (buttons == NULL) 8241 error = TRUE; 8242 if (argvars[2].v_type != VAR_UNKNOWN) 8243 { 8244 def = get_tv_number_chk(&argvars[2], &error); 8245 if (argvars[3].v_type != VAR_UNKNOWN) 8246 { 8247 typestr = get_tv_string_buf_chk(&argvars[3], buf2); 8248 if (typestr == NULL) 8249 error = TRUE; 8250 else 8251 { 8252 switch (TOUPPER_ASC(*typestr)) 8253 { 8254 case 'E': type = VIM_ERROR; break; 8255 case 'Q': type = VIM_QUESTION; break; 8256 case 'I': type = VIM_INFO; break; 8257 case 'W': type = VIM_WARNING; break; 8258 case 'G': type = VIM_GENERIC; break; 8259 } 8260 } 8261 } 8262 } 8263 } 8264 8265 if (buttons == NULL || *buttons == NUL) 8266 buttons = (char_u *)_("&Ok"); 8267 8268 if (error) 8269 rettv->vval.v_number = 0; 8270 else 8271 rettv->vval.v_number = do_dialog(type, NULL, message, buttons, 8272 def, NULL); 8273 #else 8274 rettv->vval.v_number = 0; 8275 #endif 8276 } 8277 8278 /* 8279 * "copy()" function 8280 */ 8281 static void 8282 f_copy(argvars, rettv) 8283 typval_T *argvars; 8284 typval_T *rettv; 8285 { 8286 item_copy(&argvars[0], rettv, FALSE, 0); 8287 } 8288 8289 /* 8290 * "count()" function 8291 */ 8292 static void 8293 f_count(argvars, rettv) 8294 typval_T *argvars; 8295 typval_T *rettv; 8296 { 8297 long n = 0; 8298 int ic = FALSE; 8299 8300 if (argvars[0].v_type == VAR_LIST) 8301 { 8302 listitem_T *li; 8303 list_T *l; 8304 long idx; 8305 8306 if ((l = argvars[0].vval.v_list) != NULL) 8307 { 8308 li = l->lv_first; 8309 if (argvars[2].v_type != VAR_UNKNOWN) 8310 { 8311 int error = FALSE; 8312 8313 ic = get_tv_number_chk(&argvars[2], &error); 8314 if (argvars[3].v_type != VAR_UNKNOWN) 8315 { 8316 idx = get_tv_number_chk(&argvars[3], &error); 8317 if (!error) 8318 { 8319 li = list_find(l, idx); 8320 if (li == NULL) 8321 EMSGN(_(e_listidx), idx); 8322 } 8323 } 8324 if (error) 8325 li = NULL; 8326 } 8327 8328 for ( ; li != NULL; li = li->li_next) 8329 if (tv_equal(&li->li_tv, &argvars[1], ic)) 8330 ++n; 8331 } 8332 } 8333 else if (argvars[0].v_type == VAR_DICT) 8334 { 8335 int todo; 8336 dict_T *d; 8337 hashitem_T *hi; 8338 8339 if ((d = argvars[0].vval.v_dict) != NULL) 8340 { 8341 int error = FALSE; 8342 8343 if (argvars[2].v_type != VAR_UNKNOWN) 8344 { 8345 ic = get_tv_number_chk(&argvars[2], &error); 8346 if (argvars[3].v_type != VAR_UNKNOWN) 8347 EMSG(_(e_invarg)); 8348 } 8349 8350 todo = error ? 0 : d->dv_hashtab.ht_used; 8351 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 8352 { 8353 if (!HASHITEM_EMPTY(hi)) 8354 { 8355 --todo; 8356 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic)) 8357 ++n; 8358 } 8359 } 8360 } 8361 } 8362 else 8363 EMSG2(_(e_listdictarg), "count()"); 8364 rettv->vval.v_number = n; 8365 } 8366 8367 /* 8368 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function 8369 * 8370 * Checks the existence of a cscope connection. 8371 */ 8372 /*ARGSUSED*/ 8373 static void 8374 f_cscope_connection(argvars, rettv) 8375 typval_T *argvars; 8376 typval_T *rettv; 8377 { 8378 #ifdef FEAT_CSCOPE 8379 int num = 0; 8380 char_u *dbpath = NULL; 8381 char_u *prepend = NULL; 8382 char_u buf[NUMBUFLEN]; 8383 8384 if (argvars[0].v_type != VAR_UNKNOWN 8385 && argvars[1].v_type != VAR_UNKNOWN) 8386 { 8387 num = (int)get_tv_number(&argvars[0]); 8388 dbpath = get_tv_string(&argvars[1]); 8389 if (argvars[2].v_type != VAR_UNKNOWN) 8390 prepend = get_tv_string_buf(&argvars[2], buf); 8391 } 8392 8393 rettv->vval.v_number = cs_connection(num, dbpath, prepend); 8394 #else 8395 rettv->vval.v_number = 0; 8396 #endif 8397 } 8398 8399 /* 8400 * "cursor(lnum, col)" function 8401 * 8402 * Moves the cursor to the specified line and column 8403 */ 8404 /*ARGSUSED*/ 8405 static void 8406 f_cursor(argvars, rettv) 8407 typval_T *argvars; 8408 typval_T *rettv; 8409 { 8410 long line, col; 8411 #ifdef FEAT_VIRTUALEDIT 8412 long coladd = 0; 8413 #endif 8414 8415 if (argvars[1].v_type == VAR_UNKNOWN) 8416 { 8417 pos_T pos; 8418 8419 if (list2fpos(argvars, &pos, NULL) == FAIL) 8420 return; 8421 line = pos.lnum; 8422 col = pos.col; 8423 #ifdef FEAT_VIRTUALEDIT 8424 coladd = pos.coladd; 8425 #endif 8426 } 8427 else 8428 { 8429 line = get_tv_lnum(argvars); 8430 col = get_tv_number_chk(&argvars[1], NULL); 8431 #ifdef FEAT_VIRTUALEDIT 8432 if (argvars[2].v_type != VAR_UNKNOWN) 8433 coladd = get_tv_number_chk(&argvars[2], NULL); 8434 #endif 8435 } 8436 if (line < 0 || col < 0 8437 #ifdef FEAT_VIRTUALEDIT 8438 || coladd < 0 8439 #endif 8440 ) 8441 return; /* type error; errmsg already given */ 8442 if (line > 0) 8443 curwin->w_cursor.lnum = line; 8444 if (col > 0) 8445 curwin->w_cursor.col = col - 1; 8446 #ifdef FEAT_VIRTUALEDIT 8447 curwin->w_cursor.coladd = coladd; 8448 #endif 8449 8450 /* Make sure the cursor is in a valid position. */ 8451 check_cursor(); 8452 #ifdef FEAT_MBYTE 8453 /* Correct cursor for multi-byte character. */ 8454 if (has_mbyte) 8455 mb_adjust_cursor(); 8456 #endif 8457 8458 curwin->w_set_curswant = TRUE; 8459 } 8460 8461 /* 8462 * "deepcopy()" function 8463 */ 8464 static void 8465 f_deepcopy(argvars, rettv) 8466 typval_T *argvars; 8467 typval_T *rettv; 8468 { 8469 int noref = 0; 8470 8471 if (argvars[1].v_type != VAR_UNKNOWN) 8472 noref = get_tv_number_chk(&argvars[1], NULL); 8473 if (noref < 0 || noref > 1) 8474 EMSG(_(e_invarg)); 8475 else 8476 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0); 8477 } 8478 8479 /* 8480 * "delete()" function 8481 */ 8482 static void 8483 f_delete(argvars, rettv) 8484 typval_T *argvars; 8485 typval_T *rettv; 8486 { 8487 if (check_restricted() || check_secure()) 8488 rettv->vval.v_number = -1; 8489 else 8490 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0])); 8491 } 8492 8493 /* 8494 * "did_filetype()" function 8495 */ 8496 /*ARGSUSED*/ 8497 static void 8498 f_did_filetype(argvars, rettv) 8499 typval_T *argvars; 8500 typval_T *rettv; 8501 { 8502 #ifdef FEAT_AUTOCMD 8503 rettv->vval.v_number = did_filetype; 8504 #else 8505 rettv->vval.v_number = 0; 8506 #endif 8507 } 8508 8509 /* 8510 * "diff_filler()" function 8511 */ 8512 /*ARGSUSED*/ 8513 static void 8514 f_diff_filler(argvars, rettv) 8515 typval_T *argvars; 8516 typval_T *rettv; 8517 { 8518 #ifdef FEAT_DIFF 8519 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars)); 8520 #endif 8521 } 8522 8523 /* 8524 * "diff_hlID()" function 8525 */ 8526 /*ARGSUSED*/ 8527 static void 8528 f_diff_hlID(argvars, rettv) 8529 typval_T *argvars; 8530 typval_T *rettv; 8531 { 8532 #ifdef FEAT_DIFF 8533 linenr_T lnum = get_tv_lnum(argvars); 8534 static linenr_T prev_lnum = 0; 8535 static int changedtick = 0; 8536 static int fnum = 0; 8537 static int change_start = 0; 8538 static int change_end = 0; 8539 static hlf_T hlID = 0; 8540 int filler_lines; 8541 int col; 8542 8543 if (lnum < 0) /* ignore type error in {lnum} arg */ 8544 lnum = 0; 8545 if (lnum != prev_lnum 8546 || changedtick != curbuf->b_changedtick 8547 || fnum != curbuf->b_fnum) 8548 { 8549 /* New line, buffer, change: need to get the values. */ 8550 filler_lines = diff_check(curwin, lnum); 8551 if (filler_lines < 0) 8552 { 8553 if (filler_lines == -1) 8554 { 8555 change_start = MAXCOL; 8556 change_end = -1; 8557 if (diff_find_change(curwin, lnum, &change_start, &change_end)) 8558 hlID = HLF_ADD; /* added line */ 8559 else 8560 hlID = HLF_CHD; /* changed line */ 8561 } 8562 else 8563 hlID = HLF_ADD; /* added line */ 8564 } 8565 else 8566 hlID = (hlf_T)0; 8567 prev_lnum = lnum; 8568 changedtick = curbuf->b_changedtick; 8569 fnum = curbuf->b_fnum; 8570 } 8571 8572 if (hlID == HLF_CHD || hlID == HLF_TXD) 8573 { 8574 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */ 8575 if (col >= change_start && col <= change_end) 8576 hlID = HLF_TXD; /* changed text */ 8577 else 8578 hlID = HLF_CHD; /* changed line */ 8579 } 8580 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID; 8581 #endif 8582 } 8583 8584 /* 8585 * "empty({expr})" function 8586 */ 8587 static void 8588 f_empty(argvars, rettv) 8589 typval_T *argvars; 8590 typval_T *rettv; 8591 { 8592 int n; 8593 8594 switch (argvars[0].v_type) 8595 { 8596 case VAR_STRING: 8597 case VAR_FUNC: 8598 n = argvars[0].vval.v_string == NULL 8599 || *argvars[0].vval.v_string == NUL; 8600 break; 8601 case VAR_NUMBER: 8602 n = argvars[0].vval.v_number == 0; 8603 break; 8604 case VAR_LIST: 8605 n = argvars[0].vval.v_list == NULL 8606 || argvars[0].vval.v_list->lv_first == NULL; 8607 break; 8608 case VAR_DICT: 8609 n = argvars[0].vval.v_dict == NULL 8610 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0; 8611 break; 8612 default: 8613 EMSG2(_(e_intern2), "f_empty()"); 8614 n = 0; 8615 } 8616 8617 rettv->vval.v_number = n; 8618 } 8619 8620 /* 8621 * "escape({string}, {chars})" function 8622 */ 8623 static void 8624 f_escape(argvars, rettv) 8625 typval_T *argvars; 8626 typval_T *rettv; 8627 { 8628 char_u buf[NUMBUFLEN]; 8629 8630 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]), 8631 get_tv_string_buf(&argvars[1], buf)); 8632 rettv->v_type = VAR_STRING; 8633 } 8634 8635 /* 8636 * "eval()" function 8637 */ 8638 /*ARGSUSED*/ 8639 static void 8640 f_eval(argvars, rettv) 8641 typval_T *argvars; 8642 typval_T *rettv; 8643 { 8644 char_u *s; 8645 8646 s = get_tv_string_chk(&argvars[0]); 8647 if (s != NULL) 8648 s = skipwhite(s); 8649 8650 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL) 8651 { 8652 rettv->v_type = VAR_NUMBER; 8653 rettv->vval.v_number = 0; 8654 } 8655 else if (*s != NUL) 8656 EMSG(_(e_trailing)); 8657 } 8658 8659 /* 8660 * "eventhandler()" function 8661 */ 8662 /*ARGSUSED*/ 8663 static void 8664 f_eventhandler(argvars, rettv) 8665 typval_T *argvars; 8666 typval_T *rettv; 8667 { 8668 rettv->vval.v_number = vgetc_busy; 8669 } 8670 8671 /* 8672 * "executable()" function 8673 */ 8674 static void 8675 f_executable(argvars, rettv) 8676 typval_T *argvars; 8677 typval_T *rettv; 8678 { 8679 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0])); 8680 } 8681 8682 /* 8683 * "exists()" function 8684 */ 8685 static void 8686 f_exists(argvars, rettv) 8687 typval_T *argvars; 8688 typval_T *rettv; 8689 { 8690 char_u *p; 8691 char_u *name; 8692 int n = FALSE; 8693 int len = 0; 8694 8695 p = get_tv_string(&argvars[0]); 8696 if (*p == '$') /* environment variable */ 8697 { 8698 /* first try "normal" environment variables (fast) */ 8699 if (mch_getenv(p + 1) != NULL) 8700 n = TRUE; 8701 else 8702 { 8703 /* try expanding things like $VIM and ${HOME} */ 8704 p = expand_env_save(p); 8705 if (p != NULL && *p != '$') 8706 n = TRUE; 8707 vim_free(p); 8708 } 8709 } 8710 else if (*p == '&' || *p == '+') /* option */ 8711 n = (get_option_tv(&p, NULL, TRUE) == OK); 8712 else if (*p == '*') /* internal or user defined function */ 8713 { 8714 n = function_exists(p + 1); 8715 } 8716 else if (*p == ':') 8717 { 8718 n = cmd_exists(p + 1); 8719 } 8720 else if (*p == '#') 8721 { 8722 #ifdef FEAT_AUTOCMD 8723 if (p[1] == '#') 8724 n = autocmd_supported(p + 2); 8725 else 8726 n = au_exists(p + 1); 8727 #endif 8728 } 8729 else /* internal variable */ 8730 { 8731 char_u *tofree; 8732 typval_T tv; 8733 8734 /* get_name_len() takes care of expanding curly braces */ 8735 name = p; 8736 len = get_name_len(&p, &tofree, TRUE, FALSE); 8737 if (len > 0) 8738 { 8739 if (tofree != NULL) 8740 name = tofree; 8741 n = (get_var_tv(name, len, &tv, FALSE) == OK); 8742 if (n) 8743 { 8744 /* handle d.key, l[idx], f(expr) */ 8745 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK); 8746 if (n) 8747 clear_tv(&tv); 8748 } 8749 } 8750 8751 vim_free(tofree); 8752 } 8753 8754 rettv->vval.v_number = n; 8755 } 8756 8757 /* 8758 * "expand()" function 8759 */ 8760 static void 8761 f_expand(argvars, rettv) 8762 typval_T *argvars; 8763 typval_T *rettv; 8764 { 8765 char_u *s; 8766 int len; 8767 char_u *errormsg; 8768 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND; 8769 expand_T xpc; 8770 int error = FALSE; 8771 8772 rettv->v_type = VAR_STRING; 8773 s = get_tv_string(&argvars[0]); 8774 if (*s == '%' || *s == '#' || *s == '<') 8775 { 8776 ++emsg_off; 8777 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s); 8778 --emsg_off; 8779 } 8780 else 8781 { 8782 /* When the optional second argument is non-zero, don't remove matches 8783 * for 'suffixes' and 'wildignore' */ 8784 if (argvars[1].v_type != VAR_UNKNOWN 8785 && get_tv_number_chk(&argvars[1], &error)) 8786 flags |= WILD_KEEP_ALL; 8787 if (!error) 8788 { 8789 ExpandInit(&xpc); 8790 xpc.xp_context = EXPAND_FILES; 8791 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL); 8792 ExpandCleanup(&xpc); 8793 } 8794 else 8795 rettv->vval.v_string = NULL; 8796 } 8797 } 8798 8799 /* 8800 * "extend(list, list [, idx])" function 8801 * "extend(dict, dict [, action])" function 8802 */ 8803 static void 8804 f_extend(argvars, rettv) 8805 typval_T *argvars; 8806 typval_T *rettv; 8807 { 8808 rettv->vval.v_number = 0; 8809 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST) 8810 { 8811 list_T *l1, *l2; 8812 listitem_T *item; 8813 long before; 8814 int error = FALSE; 8815 8816 l1 = argvars[0].vval.v_list; 8817 l2 = argvars[1].vval.v_list; 8818 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()") 8819 && l2 != NULL) 8820 { 8821 if (argvars[2].v_type != VAR_UNKNOWN) 8822 { 8823 before = get_tv_number_chk(&argvars[2], &error); 8824 if (error) 8825 return; /* type error; errmsg already given */ 8826 8827 if (before == l1->lv_len) 8828 item = NULL; 8829 else 8830 { 8831 item = list_find(l1, before); 8832 if (item == NULL) 8833 { 8834 EMSGN(_(e_listidx), before); 8835 return; 8836 } 8837 } 8838 } 8839 else 8840 item = NULL; 8841 list_extend(l1, l2, item); 8842 8843 copy_tv(&argvars[0], rettv); 8844 } 8845 } 8846 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT) 8847 { 8848 dict_T *d1, *d2; 8849 dictitem_T *di1; 8850 char_u *action; 8851 int i; 8852 hashitem_T *hi2; 8853 int todo; 8854 8855 d1 = argvars[0].vval.v_dict; 8856 d2 = argvars[1].vval.v_dict; 8857 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()") 8858 && d2 != NULL) 8859 { 8860 /* Check the third argument. */ 8861 if (argvars[2].v_type != VAR_UNKNOWN) 8862 { 8863 static char *(av[]) = {"keep", "force", "error"}; 8864 8865 action = get_tv_string_chk(&argvars[2]); 8866 if (action == NULL) 8867 return; /* type error; errmsg already given */ 8868 for (i = 0; i < 3; ++i) 8869 if (STRCMP(action, av[i]) == 0) 8870 break; 8871 if (i == 3) 8872 { 8873 EMSGN(_(e_invarg2), action); 8874 return; 8875 } 8876 } 8877 else 8878 action = (char_u *)"force"; 8879 8880 /* Go over all entries in the second dict and add them to the 8881 * first dict. */ 8882 todo = d2->dv_hashtab.ht_used; 8883 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2) 8884 { 8885 if (!HASHITEM_EMPTY(hi2)) 8886 { 8887 --todo; 8888 di1 = dict_find(d1, hi2->hi_key, -1); 8889 if (di1 == NULL) 8890 { 8891 di1 = dictitem_copy(HI2DI(hi2)); 8892 if (di1 != NULL && dict_add(d1, di1) == FAIL) 8893 dictitem_free(di1); 8894 } 8895 else if (*action == 'e') 8896 { 8897 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key); 8898 break; 8899 } 8900 else if (*action == 'f') 8901 { 8902 clear_tv(&di1->di_tv); 8903 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv); 8904 } 8905 } 8906 } 8907 8908 copy_tv(&argvars[0], rettv); 8909 } 8910 } 8911 else 8912 EMSG2(_(e_listdictarg), "extend()"); 8913 } 8914 8915 /* 8916 * "filereadable()" function 8917 */ 8918 static void 8919 f_filereadable(argvars, rettv) 8920 typval_T *argvars; 8921 typval_T *rettv; 8922 { 8923 FILE *fd; 8924 char_u *p; 8925 int n; 8926 8927 p = get_tv_string(&argvars[0]); 8928 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL) 8929 { 8930 n = TRUE; 8931 fclose(fd); 8932 } 8933 else 8934 n = FALSE; 8935 8936 rettv->vval.v_number = n; 8937 } 8938 8939 /* 8940 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have 8941 * rights to write into. 8942 */ 8943 static void 8944 f_filewritable(argvars, rettv) 8945 typval_T *argvars; 8946 typval_T *rettv; 8947 { 8948 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0])); 8949 } 8950 8951 static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir)); 8952 8953 static void 8954 findfilendir(argvars, rettv, dir) 8955 typval_T *argvars; 8956 typval_T *rettv; 8957 int dir; 8958 { 8959 #ifdef FEAT_SEARCHPATH 8960 char_u *fname; 8961 char_u *fresult = NULL; 8962 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path; 8963 char_u *p; 8964 char_u pathbuf[NUMBUFLEN]; 8965 int count = 1; 8966 int first = TRUE; 8967 int error = FALSE; 8968 #endif 8969 8970 rettv->vval.v_string = NULL; 8971 rettv->v_type = VAR_STRING; 8972 8973 #ifdef FEAT_SEARCHPATH 8974 fname = get_tv_string(&argvars[0]); 8975 8976 if (argvars[1].v_type != VAR_UNKNOWN) 8977 { 8978 p = get_tv_string_buf_chk(&argvars[1], pathbuf); 8979 if (p == NULL) 8980 error = TRUE; 8981 else 8982 { 8983 if (*p != NUL) 8984 path = p; 8985 8986 if (argvars[2].v_type != VAR_UNKNOWN) 8987 count = get_tv_number_chk(&argvars[2], &error); 8988 } 8989 } 8990 8991 if (count < 0 && rettv_list_alloc(rettv) == FAIL) 8992 error = TRUE; 8993 8994 if (*fname != NUL && !error) 8995 { 8996 do 8997 { 8998 if (rettv->v_type == VAR_STRING) 8999 vim_free(fresult); 9000 fresult = find_file_in_path_option(first ? fname : NULL, 9001 first ? (int)STRLEN(fname) : 0, 9002 0, first, path, dir, NULL, 9003 dir ? (char_u *)"" : curbuf->b_p_sua); 9004 first = FALSE; 9005 9006 if (fresult != NULL && rettv->v_type == VAR_LIST) 9007 list_append_string(rettv->vval.v_list, fresult, -1); 9008 9009 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL); 9010 } 9011 9012 if (rettv->v_type == VAR_STRING) 9013 rettv->vval.v_string = fresult; 9014 #endif 9015 } 9016 9017 static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map)); 9018 static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp)); 9019 9020 /* 9021 * Implementation of map() and filter(). 9022 */ 9023 static void 9024 filter_map(argvars, rettv, map) 9025 typval_T *argvars; 9026 typval_T *rettv; 9027 int map; 9028 { 9029 char_u buf[NUMBUFLEN]; 9030 char_u *expr; 9031 listitem_T *li, *nli; 9032 list_T *l = NULL; 9033 dictitem_T *di; 9034 hashtab_T *ht; 9035 hashitem_T *hi; 9036 dict_T *d = NULL; 9037 typval_T save_val; 9038 typval_T save_key; 9039 int rem; 9040 int todo; 9041 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()"; 9042 int save_did_emsg; 9043 9044 rettv->vval.v_number = 0; 9045 if (argvars[0].v_type == VAR_LIST) 9046 { 9047 if ((l = argvars[0].vval.v_list) == NULL 9048 || (map && tv_check_lock(l->lv_lock, msg))) 9049 return; 9050 } 9051 else if (argvars[0].v_type == VAR_DICT) 9052 { 9053 if ((d = argvars[0].vval.v_dict) == NULL 9054 || (map && tv_check_lock(d->dv_lock, msg))) 9055 return; 9056 } 9057 else 9058 { 9059 EMSG2(_(e_listdictarg), msg); 9060 return; 9061 } 9062 9063 expr = get_tv_string_buf_chk(&argvars[1], buf); 9064 /* On type errors, the preceding call has already displayed an error 9065 * message. Avoid a misleading error message for an empty string that 9066 * was not passed as argument. */ 9067 if (expr != NULL) 9068 { 9069 prepare_vimvar(VV_VAL, &save_val); 9070 expr = skipwhite(expr); 9071 9072 /* We reset "did_emsg" to be able to detect whether an error 9073 * occurred during evaluation of the expression. */ 9074 save_did_emsg = did_emsg; 9075 did_emsg = FALSE; 9076 9077 if (argvars[0].v_type == VAR_DICT) 9078 { 9079 prepare_vimvar(VV_KEY, &save_key); 9080 vimvars[VV_KEY].vv_type = VAR_STRING; 9081 9082 ht = &d->dv_hashtab; 9083 hash_lock(ht); 9084 todo = ht->ht_used; 9085 for (hi = ht->ht_array; todo > 0; ++hi) 9086 { 9087 if (!HASHITEM_EMPTY(hi)) 9088 { 9089 --todo; 9090 di = HI2DI(hi); 9091 if (tv_check_lock(di->di_tv.v_lock, msg)) 9092 break; 9093 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key); 9094 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL 9095 || did_emsg) 9096 break; 9097 if (!map && rem) 9098 dictitem_remove(d, di); 9099 clear_tv(&vimvars[VV_KEY].vv_tv); 9100 } 9101 } 9102 hash_unlock(ht); 9103 9104 restore_vimvar(VV_KEY, &save_key); 9105 } 9106 else 9107 { 9108 for (li = l->lv_first; li != NULL; li = nli) 9109 { 9110 if (tv_check_lock(li->li_tv.v_lock, msg)) 9111 break; 9112 nli = li->li_next; 9113 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL 9114 || did_emsg) 9115 break; 9116 if (!map && rem) 9117 listitem_remove(l, li); 9118 } 9119 } 9120 9121 restore_vimvar(VV_VAL, &save_val); 9122 9123 did_emsg |= save_did_emsg; 9124 } 9125 9126 copy_tv(&argvars[0], rettv); 9127 } 9128 9129 static int 9130 filter_map_one(tv, expr, map, remp) 9131 typval_T *tv; 9132 char_u *expr; 9133 int map; 9134 int *remp; 9135 { 9136 typval_T rettv; 9137 char_u *s; 9138 9139 copy_tv(tv, &vimvars[VV_VAL].vv_tv); 9140 s = expr; 9141 if (eval1(&s, &rettv, TRUE) == FAIL) 9142 return FAIL; 9143 if (*s != NUL) /* check for trailing chars after expr */ 9144 { 9145 EMSG2(_(e_invexpr2), s); 9146 return FAIL; 9147 } 9148 if (map) 9149 { 9150 /* map(): replace the list item value */ 9151 clear_tv(tv); 9152 rettv.v_lock = 0; 9153 *tv = rettv; 9154 } 9155 else 9156 { 9157 int error = FALSE; 9158 9159 /* filter(): when expr is zero remove the item */ 9160 *remp = (get_tv_number_chk(&rettv, &error) == 0); 9161 clear_tv(&rettv); 9162 /* On type error, nothing has been removed; return FAIL to stop the 9163 * loop. The error message was given by get_tv_number_chk(). */ 9164 if (error) 9165 return FAIL; 9166 } 9167 clear_tv(&vimvars[VV_VAL].vv_tv); 9168 return OK; 9169 } 9170 9171 /* 9172 * "filter()" function 9173 */ 9174 static void 9175 f_filter(argvars, rettv) 9176 typval_T *argvars; 9177 typval_T *rettv; 9178 { 9179 filter_map(argvars, rettv, FALSE); 9180 } 9181 9182 /* 9183 * "finddir({fname}[, {path}[, {count}]])" function 9184 */ 9185 static void 9186 f_finddir(argvars, rettv) 9187 typval_T *argvars; 9188 typval_T *rettv; 9189 { 9190 findfilendir(argvars, rettv, TRUE); 9191 } 9192 9193 /* 9194 * "findfile({fname}[, {path}[, {count}]])" function 9195 */ 9196 static void 9197 f_findfile(argvars, rettv) 9198 typval_T *argvars; 9199 typval_T *rettv; 9200 { 9201 findfilendir(argvars, rettv, FALSE); 9202 } 9203 9204 /* 9205 * "fnamemodify({fname}, {mods})" function 9206 */ 9207 static void 9208 f_fnamemodify(argvars, rettv) 9209 typval_T *argvars; 9210 typval_T *rettv; 9211 { 9212 char_u *fname; 9213 char_u *mods; 9214 int usedlen = 0; 9215 int len; 9216 char_u *fbuf = NULL; 9217 char_u buf[NUMBUFLEN]; 9218 9219 fname = get_tv_string_chk(&argvars[0]); 9220 mods = get_tv_string_buf_chk(&argvars[1], buf); 9221 if (fname == NULL || mods == NULL) 9222 fname = NULL; 9223 else 9224 { 9225 len = (int)STRLEN(fname); 9226 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len); 9227 } 9228 9229 rettv->v_type = VAR_STRING; 9230 if (fname == NULL) 9231 rettv->vval.v_string = NULL; 9232 else 9233 rettv->vval.v_string = vim_strnsave(fname, len); 9234 vim_free(fbuf); 9235 } 9236 9237 static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end)); 9238 9239 /* 9240 * "foldclosed()" function 9241 */ 9242 static void 9243 foldclosed_both(argvars, rettv, end) 9244 typval_T *argvars; 9245 typval_T *rettv; 9246 int end; 9247 { 9248 #ifdef FEAT_FOLDING 9249 linenr_T lnum; 9250 linenr_T first, last; 9251 9252 lnum = get_tv_lnum(argvars); 9253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 9254 { 9255 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL)) 9256 { 9257 if (end) 9258 rettv->vval.v_number = (varnumber_T)last; 9259 else 9260 rettv->vval.v_number = (varnumber_T)first; 9261 return; 9262 } 9263 } 9264 #endif 9265 rettv->vval.v_number = -1; 9266 } 9267 9268 /* 9269 * "foldclosed()" function 9270 */ 9271 static void 9272 f_foldclosed(argvars, rettv) 9273 typval_T *argvars; 9274 typval_T *rettv; 9275 { 9276 foldclosed_both(argvars, rettv, FALSE); 9277 } 9278 9279 /* 9280 * "foldclosedend()" function 9281 */ 9282 static void 9283 f_foldclosedend(argvars, rettv) 9284 typval_T *argvars; 9285 typval_T *rettv; 9286 { 9287 foldclosed_both(argvars, rettv, TRUE); 9288 } 9289 9290 /* 9291 * "foldlevel()" function 9292 */ 9293 static void 9294 f_foldlevel(argvars, rettv) 9295 typval_T *argvars; 9296 typval_T *rettv; 9297 { 9298 #ifdef FEAT_FOLDING 9299 linenr_T lnum; 9300 9301 lnum = get_tv_lnum(argvars); 9302 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 9303 rettv->vval.v_number = foldLevel(lnum); 9304 else 9305 #endif 9306 rettv->vval.v_number = 0; 9307 } 9308 9309 /* 9310 * "foldtext()" function 9311 */ 9312 /*ARGSUSED*/ 9313 static void 9314 f_foldtext(argvars, rettv) 9315 typval_T *argvars; 9316 typval_T *rettv; 9317 { 9318 #ifdef FEAT_FOLDING 9319 linenr_T lnum; 9320 char_u *s; 9321 char_u *r; 9322 int len; 9323 char *txt; 9324 #endif 9325 9326 rettv->v_type = VAR_STRING; 9327 rettv->vval.v_string = NULL; 9328 #ifdef FEAT_FOLDING 9329 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0 9330 && (linenr_T)vimvars[VV_FOLDEND].vv_nr 9331 <= curbuf->b_ml.ml_line_count 9332 && vimvars[VV_FOLDDASHES].vv_str != NULL) 9333 { 9334 /* Find first non-empty line in the fold. */ 9335 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr; 9336 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr) 9337 { 9338 if (!linewhite(lnum)) 9339 break; 9340 ++lnum; 9341 } 9342 9343 /* Find interesting text in this line. */ 9344 s = skipwhite(ml_get(lnum)); 9345 /* skip C comment-start */ 9346 if (s[0] == '/' && (s[1] == '*' || s[1] == '/')) 9347 { 9348 s = skipwhite(s + 2); 9349 if (*skipwhite(s) == NUL 9350 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr) 9351 { 9352 s = skipwhite(ml_get(lnum + 1)); 9353 if (*s == '*') 9354 s = skipwhite(s + 1); 9355 } 9356 } 9357 txt = _("+-%s%3ld lines: "); 9358 r = alloc((unsigned)(STRLEN(txt) 9359 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */ 9360 + 20 /* for %3ld */ 9361 + STRLEN(s))); /* concatenated */ 9362 if (r != NULL) 9363 { 9364 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str, 9365 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr 9366 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1)); 9367 len = (int)STRLEN(r); 9368 STRCAT(r, s); 9369 /* remove 'foldmarker' and 'commentstring' */ 9370 foldtext_cleanup(r + len); 9371 rettv->vval.v_string = r; 9372 } 9373 } 9374 #endif 9375 } 9376 9377 /* 9378 * "foldtextresult(lnum)" function 9379 */ 9380 /*ARGSUSED*/ 9381 static void 9382 f_foldtextresult(argvars, rettv) 9383 typval_T *argvars; 9384 typval_T *rettv; 9385 { 9386 #ifdef FEAT_FOLDING 9387 linenr_T lnum; 9388 char_u *text; 9389 char_u buf[51]; 9390 foldinfo_T foldinfo; 9391 int fold_count; 9392 #endif 9393 9394 rettv->v_type = VAR_STRING; 9395 rettv->vval.v_string = NULL; 9396 #ifdef FEAT_FOLDING 9397 lnum = get_tv_lnum(argvars); 9398 /* treat illegal types and illegal string values for {lnum} the same */ 9399 if (lnum < 0) 9400 lnum = 0; 9401 fold_count = foldedCount(curwin, lnum, &foldinfo); 9402 if (fold_count > 0) 9403 { 9404 text = get_foldtext(curwin, lnum, lnum + fold_count - 1, 9405 &foldinfo, buf); 9406 if (text == buf) 9407 text = vim_strsave(text); 9408 rettv->vval.v_string = text; 9409 } 9410 #endif 9411 } 9412 9413 /* 9414 * "foreground()" function 9415 */ 9416 /*ARGSUSED*/ 9417 static void 9418 f_foreground(argvars, rettv) 9419 typval_T *argvars; 9420 typval_T *rettv; 9421 { 9422 rettv->vval.v_number = 0; 9423 #ifdef FEAT_GUI 9424 if (gui.in_use) 9425 gui_mch_set_foreground(); 9426 #else 9427 # ifdef WIN32 9428 win32_set_foreground(); 9429 # endif 9430 #endif 9431 } 9432 9433 /* 9434 * "function()" function 9435 */ 9436 /*ARGSUSED*/ 9437 static void 9438 f_function(argvars, rettv) 9439 typval_T *argvars; 9440 typval_T *rettv; 9441 { 9442 char_u *s; 9443 9444 rettv->vval.v_number = 0; 9445 s = get_tv_string(&argvars[0]); 9446 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s)) 9447 EMSG2(_(e_invarg2), s); 9448 else if (!function_exists(s)) 9449 EMSG2(_("E700: Unknown function: %s"), s); 9450 else 9451 { 9452 rettv->vval.v_string = vim_strsave(s); 9453 rettv->v_type = VAR_FUNC; 9454 } 9455 } 9456 9457 /* 9458 * "garbagecollect()" function 9459 */ 9460 /*ARGSUSED*/ 9461 static void 9462 f_garbagecollect(argvars, rettv) 9463 typval_T *argvars; 9464 typval_T *rettv; 9465 { 9466 garbage_collect(); 9467 } 9468 9469 /* 9470 * "get()" function 9471 */ 9472 static void 9473 f_get(argvars, rettv) 9474 typval_T *argvars; 9475 typval_T *rettv; 9476 { 9477 listitem_T *li; 9478 list_T *l; 9479 dictitem_T *di; 9480 dict_T *d; 9481 typval_T *tv = NULL; 9482 9483 if (argvars[0].v_type == VAR_LIST) 9484 { 9485 if ((l = argvars[0].vval.v_list) != NULL) 9486 { 9487 int error = FALSE; 9488 9489 li = list_find(l, get_tv_number_chk(&argvars[1], &error)); 9490 if (!error && li != NULL) 9491 tv = &li->li_tv; 9492 } 9493 } 9494 else if (argvars[0].v_type == VAR_DICT) 9495 { 9496 if ((d = argvars[0].vval.v_dict) != NULL) 9497 { 9498 di = dict_find(d, get_tv_string(&argvars[1]), -1); 9499 if (di != NULL) 9500 tv = &di->di_tv; 9501 } 9502 } 9503 else 9504 EMSG2(_(e_listdictarg), "get()"); 9505 9506 if (tv == NULL) 9507 { 9508 if (argvars[2].v_type == VAR_UNKNOWN) 9509 rettv->vval.v_number = 0; 9510 else 9511 copy_tv(&argvars[2], rettv); 9512 } 9513 else 9514 copy_tv(tv, rettv); 9515 } 9516 9517 static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv)); 9518 9519 /* 9520 * Get line or list of lines from buffer "buf" into "rettv". 9521 * Return a range (from start to end) of lines in rettv from the specified 9522 * buffer. 9523 * If 'retlist' is TRUE, then the lines are returned as a Vim List. 9524 */ 9525 static void 9526 get_buffer_lines(buf, start, end, retlist, rettv) 9527 buf_T *buf; 9528 linenr_T start; 9529 linenr_T end; 9530 int retlist; 9531 typval_T *rettv; 9532 { 9533 char_u *p; 9534 9535 if (retlist) 9536 { 9537 if (rettv_list_alloc(rettv) == FAIL) 9538 return; 9539 } 9540 else 9541 rettv->vval.v_number = 0; 9542 9543 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0) 9544 return; 9545 9546 if (!retlist) 9547 { 9548 if (start >= 1 && start <= buf->b_ml.ml_line_count) 9549 p = ml_get_buf(buf, start, FALSE); 9550 else 9551 p = (char_u *)""; 9552 9553 rettv->v_type = VAR_STRING; 9554 rettv->vval.v_string = vim_strsave(p); 9555 } 9556 else 9557 { 9558 if (end < start) 9559 return; 9560 9561 if (start < 1) 9562 start = 1; 9563 if (end > buf->b_ml.ml_line_count) 9564 end = buf->b_ml.ml_line_count; 9565 while (start <= end) 9566 if (list_append_string(rettv->vval.v_list, 9567 ml_get_buf(buf, start++, FALSE), -1) == FAIL) 9568 break; 9569 } 9570 } 9571 9572 /* 9573 * "getbufline()" function 9574 */ 9575 static void 9576 f_getbufline(argvars, rettv) 9577 typval_T *argvars; 9578 typval_T *rettv; 9579 { 9580 linenr_T lnum; 9581 linenr_T end; 9582 buf_T *buf; 9583 9584 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 9585 ++emsg_off; 9586 buf = get_buf_tv(&argvars[0]); 9587 --emsg_off; 9588 9589 lnum = get_tv_lnum_buf(&argvars[1], buf); 9590 if (argvars[2].v_type == VAR_UNKNOWN) 9591 end = lnum; 9592 else 9593 end = get_tv_lnum_buf(&argvars[2], buf); 9594 9595 get_buffer_lines(buf, lnum, end, TRUE, rettv); 9596 } 9597 9598 /* 9599 * "getbufvar()" function 9600 */ 9601 static void 9602 f_getbufvar(argvars, rettv) 9603 typval_T *argvars; 9604 typval_T *rettv; 9605 { 9606 buf_T *buf; 9607 buf_T *save_curbuf; 9608 char_u *varname; 9609 dictitem_T *v; 9610 9611 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 9612 varname = get_tv_string_chk(&argvars[1]); 9613 ++emsg_off; 9614 buf = get_buf_tv(&argvars[0]); 9615 9616 rettv->v_type = VAR_STRING; 9617 rettv->vval.v_string = NULL; 9618 9619 if (buf != NULL && varname != NULL) 9620 { 9621 if (*varname == '&') /* buffer-local-option */ 9622 { 9623 /* set curbuf to be our buf, temporarily */ 9624 save_curbuf = curbuf; 9625 curbuf = buf; 9626 9627 get_option_tv(&varname, rettv, TRUE); 9628 9629 /* restore previous notion of curbuf */ 9630 curbuf = save_curbuf; 9631 } 9632 else 9633 { 9634 if (*varname == NUL) 9635 /* let getbufvar({nr}, "") return the "b:" dictionary. The 9636 * scope prefix before the NUL byte is required by 9637 * find_var_in_ht(). */ 9638 varname = (char_u *)"b:" + 2; 9639 /* look up the variable */ 9640 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE); 9641 if (v != NULL) 9642 copy_tv(&v->di_tv, rettv); 9643 } 9644 } 9645 9646 --emsg_off; 9647 } 9648 9649 /* 9650 * "getchar()" function 9651 */ 9652 static void 9653 f_getchar(argvars, rettv) 9654 typval_T *argvars; 9655 typval_T *rettv; 9656 { 9657 varnumber_T n; 9658 int error = FALSE; 9659 9660 ++no_mapping; 9661 ++allow_keys; 9662 if (argvars[0].v_type == VAR_UNKNOWN) 9663 /* getchar(): blocking wait. */ 9664 n = safe_vgetc(); 9665 else if (get_tv_number_chk(&argvars[0], &error) == 1) 9666 /* getchar(1): only check if char avail */ 9667 n = vpeekc(); 9668 else if (error || vpeekc() == NUL) 9669 /* illegal argument or getchar(0) and no char avail: return zero */ 9670 n = 0; 9671 else 9672 /* getchar(0) and char avail: return char */ 9673 n = safe_vgetc(); 9674 --no_mapping; 9675 --allow_keys; 9676 9677 rettv->vval.v_number = n; 9678 if (IS_SPECIAL(n) || mod_mask != 0) 9679 { 9680 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */ 9681 int i = 0; 9682 9683 /* Turn a special key into three bytes, plus modifier. */ 9684 if (mod_mask != 0) 9685 { 9686 temp[i++] = K_SPECIAL; 9687 temp[i++] = KS_MODIFIER; 9688 temp[i++] = mod_mask; 9689 } 9690 if (IS_SPECIAL(n)) 9691 { 9692 temp[i++] = K_SPECIAL; 9693 temp[i++] = K_SECOND(n); 9694 temp[i++] = K_THIRD(n); 9695 } 9696 #ifdef FEAT_MBYTE 9697 else if (has_mbyte) 9698 i += (*mb_char2bytes)(n, temp + i); 9699 #endif 9700 else 9701 temp[i++] = n; 9702 temp[i++] = NUL; 9703 rettv->v_type = VAR_STRING; 9704 rettv->vval.v_string = vim_strsave(temp); 9705 } 9706 } 9707 9708 /* 9709 * "getcharmod()" function 9710 */ 9711 /*ARGSUSED*/ 9712 static void 9713 f_getcharmod(argvars, rettv) 9714 typval_T *argvars; 9715 typval_T *rettv; 9716 { 9717 rettv->vval.v_number = mod_mask; 9718 } 9719 9720 /* 9721 * "getcmdline()" function 9722 */ 9723 /*ARGSUSED*/ 9724 static void 9725 f_getcmdline(argvars, rettv) 9726 typval_T *argvars; 9727 typval_T *rettv; 9728 { 9729 rettv->v_type = VAR_STRING; 9730 rettv->vval.v_string = get_cmdline_str(); 9731 } 9732 9733 /* 9734 * "getcmdpos()" function 9735 */ 9736 /*ARGSUSED*/ 9737 static void 9738 f_getcmdpos(argvars, rettv) 9739 typval_T *argvars; 9740 typval_T *rettv; 9741 { 9742 rettv->vval.v_number = get_cmdline_pos() + 1; 9743 } 9744 9745 /* 9746 * "getcmdtype()" function 9747 */ 9748 /*ARGSUSED*/ 9749 static void 9750 f_getcmdtype(argvars, rettv) 9751 typval_T *argvars; 9752 typval_T *rettv; 9753 { 9754 rettv->v_type = VAR_STRING; 9755 rettv->vval.v_string = alloc(2); 9756 if (rettv->vval.v_string != NULL) 9757 { 9758 rettv->vval.v_string[0] = get_cmdline_type(); 9759 rettv->vval.v_string[1] = NUL; 9760 } 9761 } 9762 9763 /* 9764 * "getcwd()" function 9765 */ 9766 /*ARGSUSED*/ 9767 static void 9768 f_getcwd(argvars, rettv) 9769 typval_T *argvars; 9770 typval_T *rettv; 9771 { 9772 char_u cwd[MAXPATHL]; 9773 9774 rettv->v_type = VAR_STRING; 9775 if (mch_dirname(cwd, MAXPATHL) == FAIL) 9776 rettv->vval.v_string = NULL; 9777 else 9778 { 9779 rettv->vval.v_string = vim_strsave(cwd); 9780 #ifdef BACKSLASH_IN_FILENAME 9781 if (rettv->vval.v_string != NULL) 9782 slash_adjust(rettv->vval.v_string); 9783 #endif 9784 } 9785 } 9786 9787 /* 9788 * "getfontname()" function 9789 */ 9790 /*ARGSUSED*/ 9791 static void 9792 f_getfontname(argvars, rettv) 9793 typval_T *argvars; 9794 typval_T *rettv; 9795 { 9796 rettv->v_type = VAR_STRING; 9797 rettv->vval.v_string = NULL; 9798 #ifdef FEAT_GUI 9799 if (gui.in_use) 9800 { 9801 GuiFont font; 9802 char_u *name = NULL; 9803 9804 if (argvars[0].v_type == VAR_UNKNOWN) 9805 { 9806 /* Get the "Normal" font. Either the name saved by 9807 * hl_set_font_name() or from the font ID. */ 9808 font = gui.norm_font; 9809 name = hl_get_font_name(); 9810 } 9811 else 9812 { 9813 name = get_tv_string(&argvars[0]); 9814 if (STRCMP(name, "*") == 0) /* don't use font dialog */ 9815 return; 9816 font = gui_mch_get_font(name, FALSE); 9817 if (font == NOFONT) 9818 return; /* Invalid font name, return empty string. */ 9819 } 9820 rettv->vval.v_string = gui_mch_get_fontname(font, name); 9821 if (argvars[0].v_type != VAR_UNKNOWN) 9822 gui_mch_free_font(font); 9823 } 9824 #endif 9825 } 9826 9827 /* 9828 * "getfperm({fname})" function 9829 */ 9830 static void 9831 f_getfperm(argvars, rettv) 9832 typval_T *argvars; 9833 typval_T *rettv; 9834 { 9835 char_u *fname; 9836 struct stat st; 9837 char_u *perm = NULL; 9838 char_u flags[] = "rwx"; 9839 int i; 9840 9841 fname = get_tv_string(&argvars[0]); 9842 9843 rettv->v_type = VAR_STRING; 9844 if (mch_stat((char *)fname, &st) >= 0) 9845 { 9846 perm = vim_strsave((char_u *)"---------"); 9847 if (perm != NULL) 9848 { 9849 for (i = 0; i < 9; i++) 9850 { 9851 if (st.st_mode & (1 << (8 - i))) 9852 perm[i] = flags[i % 3]; 9853 } 9854 } 9855 } 9856 rettv->vval.v_string = perm; 9857 } 9858 9859 /* 9860 * "getfsize({fname})" function 9861 */ 9862 static void 9863 f_getfsize(argvars, rettv) 9864 typval_T *argvars; 9865 typval_T *rettv; 9866 { 9867 char_u *fname; 9868 struct stat st; 9869 9870 fname = get_tv_string(&argvars[0]); 9871 9872 rettv->v_type = VAR_NUMBER; 9873 9874 if (mch_stat((char *)fname, &st) >= 0) 9875 { 9876 if (mch_isdir(fname)) 9877 rettv->vval.v_number = 0; 9878 else 9879 rettv->vval.v_number = (varnumber_T)st.st_size; 9880 } 9881 else 9882 rettv->vval.v_number = -1; 9883 } 9884 9885 /* 9886 * "getftime({fname})" function 9887 */ 9888 static void 9889 f_getftime(argvars, rettv) 9890 typval_T *argvars; 9891 typval_T *rettv; 9892 { 9893 char_u *fname; 9894 struct stat st; 9895 9896 fname = get_tv_string(&argvars[0]); 9897 9898 if (mch_stat((char *)fname, &st) >= 0) 9899 rettv->vval.v_number = (varnumber_T)st.st_mtime; 9900 else 9901 rettv->vval.v_number = -1; 9902 } 9903 9904 /* 9905 * "getftype({fname})" function 9906 */ 9907 static void 9908 f_getftype(argvars, rettv) 9909 typval_T *argvars; 9910 typval_T *rettv; 9911 { 9912 char_u *fname; 9913 struct stat st; 9914 char_u *type = NULL; 9915 char *t; 9916 9917 fname = get_tv_string(&argvars[0]); 9918 9919 rettv->v_type = VAR_STRING; 9920 if (mch_lstat((char *)fname, &st) >= 0) 9921 { 9922 #ifdef S_ISREG 9923 if (S_ISREG(st.st_mode)) 9924 t = "file"; 9925 else if (S_ISDIR(st.st_mode)) 9926 t = "dir"; 9927 # ifdef S_ISLNK 9928 else if (S_ISLNK(st.st_mode)) 9929 t = "link"; 9930 # endif 9931 # ifdef S_ISBLK 9932 else if (S_ISBLK(st.st_mode)) 9933 t = "bdev"; 9934 # endif 9935 # ifdef S_ISCHR 9936 else if (S_ISCHR(st.st_mode)) 9937 t = "cdev"; 9938 # endif 9939 # ifdef S_ISFIFO 9940 else if (S_ISFIFO(st.st_mode)) 9941 t = "fifo"; 9942 # endif 9943 # ifdef S_ISSOCK 9944 else if (S_ISSOCK(st.st_mode)) 9945 t = "fifo"; 9946 # endif 9947 else 9948 t = "other"; 9949 #else 9950 # ifdef S_IFMT 9951 switch (st.st_mode & S_IFMT) 9952 { 9953 case S_IFREG: t = "file"; break; 9954 case S_IFDIR: t = "dir"; break; 9955 # ifdef S_IFLNK 9956 case S_IFLNK: t = "link"; break; 9957 # endif 9958 # ifdef S_IFBLK 9959 case S_IFBLK: t = "bdev"; break; 9960 # endif 9961 # ifdef S_IFCHR 9962 case S_IFCHR: t = "cdev"; break; 9963 # endif 9964 # ifdef S_IFIFO 9965 case S_IFIFO: t = "fifo"; break; 9966 # endif 9967 # ifdef S_IFSOCK 9968 case S_IFSOCK: t = "socket"; break; 9969 # endif 9970 default: t = "other"; 9971 } 9972 # else 9973 if (mch_isdir(fname)) 9974 t = "dir"; 9975 else 9976 t = "file"; 9977 # endif 9978 #endif 9979 type = vim_strsave((char_u *)t); 9980 } 9981 rettv->vval.v_string = type; 9982 } 9983 9984 /* 9985 * "getline(lnum, [end])" function 9986 */ 9987 static void 9988 f_getline(argvars, rettv) 9989 typval_T *argvars; 9990 typval_T *rettv; 9991 { 9992 linenr_T lnum; 9993 linenr_T end; 9994 int retlist; 9995 9996 lnum = get_tv_lnum(argvars); 9997 if (argvars[1].v_type == VAR_UNKNOWN) 9998 { 9999 end = 0; 10000 retlist = FALSE; 10001 } 10002 else 10003 { 10004 end = get_tv_lnum(&argvars[1]); 10005 retlist = TRUE; 10006 } 10007 10008 get_buffer_lines(curbuf, lnum, end, retlist, rettv); 10009 } 10010 10011 /* 10012 * "getpos(string)" function 10013 */ 10014 static void 10015 f_getpos(argvars, rettv) 10016 typval_T *argvars; 10017 typval_T *rettv; 10018 { 10019 pos_T *fp; 10020 list_T *l; 10021 int fnum = -1; 10022 10023 if (rettv_list_alloc(rettv) == OK) 10024 { 10025 l = rettv->vval.v_list; 10026 fp = var2fpos(&argvars[0], TRUE, &fnum); 10027 if (fnum != -1) 10028 list_append_number(l, (varnumber_T)fnum); 10029 else 10030 list_append_number(l, (varnumber_T)0); 10031 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum 10032 : (varnumber_T)0); 10033 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1 10034 : (varnumber_T)0); 10035 list_append_number(l, 10036 #ifdef FEAT_VIRTUALEDIT 10037 (fp != NULL) ? (varnumber_T)fp->coladd : 10038 #endif 10039 (varnumber_T)0); 10040 } 10041 else 10042 rettv->vval.v_number = FALSE; 10043 } 10044 10045 /* 10046 * "getqflist()" and "getloclist()" functions 10047 */ 10048 /*ARGSUSED*/ 10049 static void 10050 f_getqflist(argvars, rettv) 10051 typval_T *argvars; 10052 typval_T *rettv; 10053 { 10054 #ifdef FEAT_QUICKFIX 10055 win_T *wp; 10056 #endif 10057 10058 rettv->vval.v_number = FALSE; 10059 #ifdef FEAT_QUICKFIX 10060 if (rettv_list_alloc(rettv) == OK) 10061 { 10062 wp = NULL; 10063 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */ 10064 { 10065 wp = find_win_by_nr(&argvars[0]); 10066 if (wp == NULL) 10067 return; 10068 } 10069 10070 (void)get_errorlist(wp, rettv->vval.v_list); 10071 } 10072 #endif 10073 } 10074 10075 /* 10076 * "getreg()" function 10077 */ 10078 static void 10079 f_getreg(argvars, rettv) 10080 typval_T *argvars; 10081 typval_T *rettv; 10082 { 10083 char_u *strregname; 10084 int regname; 10085 int arg2 = FALSE; 10086 int error = FALSE; 10087 10088 if (argvars[0].v_type != VAR_UNKNOWN) 10089 { 10090 strregname = get_tv_string_chk(&argvars[0]); 10091 error = strregname == NULL; 10092 if (argvars[1].v_type != VAR_UNKNOWN) 10093 arg2 = get_tv_number_chk(&argvars[1], &error); 10094 } 10095 else 10096 strregname = vimvars[VV_REG].vv_str; 10097 regname = (strregname == NULL ? '"' : *strregname); 10098 if (regname == 0) 10099 regname = '"'; 10100 10101 rettv->v_type = VAR_STRING; 10102 rettv->vval.v_string = error ? NULL : 10103 get_reg_contents(regname, TRUE, arg2); 10104 } 10105 10106 /* 10107 * "getregtype()" function 10108 */ 10109 static void 10110 f_getregtype(argvars, rettv) 10111 typval_T *argvars; 10112 typval_T *rettv; 10113 { 10114 char_u *strregname; 10115 int regname; 10116 char_u buf[NUMBUFLEN + 2]; 10117 long reglen = 0; 10118 10119 if (argvars[0].v_type != VAR_UNKNOWN) 10120 { 10121 strregname = get_tv_string_chk(&argvars[0]); 10122 if (strregname == NULL) /* type error; errmsg already given */ 10123 { 10124 rettv->v_type = VAR_STRING; 10125 rettv->vval.v_string = NULL; 10126 return; 10127 } 10128 } 10129 else 10130 /* Default to v:register */ 10131 strregname = vimvars[VV_REG].vv_str; 10132 10133 regname = (strregname == NULL ? '"' : *strregname); 10134 if (regname == 0) 10135 regname = '"'; 10136 10137 buf[0] = NUL; 10138 buf[1] = NUL; 10139 switch (get_reg_type(regname, ®len)) 10140 { 10141 case MLINE: buf[0] = 'V'; break; 10142 case MCHAR: buf[0] = 'v'; break; 10143 #ifdef FEAT_VISUAL 10144 case MBLOCK: 10145 buf[0] = Ctrl_V; 10146 sprintf((char *)buf + 1, "%ld", reglen + 1); 10147 break; 10148 #endif 10149 } 10150 rettv->v_type = VAR_STRING; 10151 rettv->vval.v_string = vim_strsave(buf); 10152 } 10153 10154 /* 10155 * "getwinposx()" function 10156 */ 10157 /*ARGSUSED*/ 10158 static void 10159 f_getwinposx(argvars, rettv) 10160 typval_T *argvars; 10161 typval_T *rettv; 10162 { 10163 rettv->vval.v_number = -1; 10164 #ifdef FEAT_GUI 10165 if (gui.in_use) 10166 { 10167 int x, y; 10168 10169 if (gui_mch_get_winpos(&x, &y) == OK) 10170 rettv->vval.v_number = x; 10171 } 10172 #endif 10173 } 10174 10175 /* 10176 * "getwinposy()" function 10177 */ 10178 /*ARGSUSED*/ 10179 static void 10180 f_getwinposy(argvars, rettv) 10181 typval_T *argvars; 10182 typval_T *rettv; 10183 { 10184 rettv->vval.v_number = -1; 10185 #ifdef FEAT_GUI 10186 if (gui.in_use) 10187 { 10188 int x, y; 10189 10190 if (gui_mch_get_winpos(&x, &y) == OK) 10191 rettv->vval.v_number = y; 10192 } 10193 #endif 10194 } 10195 10196 static win_T * 10197 find_win_by_nr(vp) 10198 typval_T *vp; 10199 { 10200 #ifdef FEAT_WINDOWS 10201 win_T *wp; 10202 #endif 10203 int nr; 10204 10205 nr = get_tv_number_chk(vp, NULL); 10206 10207 #ifdef FEAT_WINDOWS 10208 if (nr < 0) 10209 return NULL; 10210 if (nr == 0) 10211 return curwin; 10212 10213 for (wp = firstwin; wp != NULL; wp = wp->w_next) 10214 if (--nr <= 0) 10215 break; 10216 return wp; 10217 #else 10218 if (nr == 0 || nr == 1) 10219 return curwin; 10220 return NULL; 10221 #endif 10222 } 10223 10224 /* 10225 * "getwinvar()" function 10226 */ 10227 static void 10228 f_getwinvar(argvars, rettv) 10229 typval_T *argvars; 10230 typval_T *rettv; 10231 { 10232 win_T *win, *oldcurwin; 10233 char_u *varname; 10234 dictitem_T *v; 10235 10236 win = find_win_by_nr(&argvars[0]); 10237 varname = get_tv_string_chk(&argvars[1]); 10238 ++emsg_off; 10239 10240 rettv->v_type = VAR_STRING; 10241 rettv->vval.v_string = NULL; 10242 10243 if (win != NULL && varname != NULL) 10244 { 10245 if (*varname == '&') /* window-local-option */ 10246 { 10247 /* Set curwin to be our win, temporarily. Also set curbuf, so 10248 * that we can get buffer-local options. */ 10249 oldcurwin = curwin; 10250 curwin = win; 10251 curbuf = win->w_buffer; 10252 10253 get_option_tv(&varname, rettv, 1); 10254 10255 /* restore previous notion of curwin */ 10256 curwin = oldcurwin; 10257 curbuf = curwin->w_buffer; 10258 } 10259 else 10260 { 10261 if (*varname == NUL) 10262 /* let getwinvar({nr}, "") return the "w:" dictionary. The 10263 * scope prefix before the NUL byte is required by 10264 * find_var_in_ht(). */ 10265 varname = (char_u *)"w:" + 2; 10266 /* look up the variable */ 10267 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE); 10268 if (v != NULL) 10269 copy_tv(&v->di_tv, rettv); 10270 } 10271 } 10272 10273 --emsg_off; 10274 } 10275 10276 /* 10277 * "glob()" function 10278 */ 10279 static void 10280 f_glob(argvars, rettv) 10281 typval_T *argvars; 10282 typval_T *rettv; 10283 { 10284 expand_T xpc; 10285 10286 ExpandInit(&xpc); 10287 xpc.xp_context = EXPAND_FILES; 10288 rettv->v_type = VAR_STRING; 10289 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]), 10290 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL); 10291 ExpandCleanup(&xpc); 10292 } 10293 10294 /* 10295 * "globpath()" function 10296 */ 10297 static void 10298 f_globpath(argvars, rettv) 10299 typval_T *argvars; 10300 typval_T *rettv; 10301 { 10302 char_u buf1[NUMBUFLEN]; 10303 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1); 10304 10305 rettv->v_type = VAR_STRING; 10306 if (file == NULL) 10307 rettv->vval.v_string = NULL; 10308 else 10309 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file); 10310 } 10311 10312 /* 10313 * "has()" function 10314 */ 10315 static void 10316 f_has(argvars, rettv) 10317 typval_T *argvars; 10318 typval_T *rettv; 10319 { 10320 int i; 10321 char_u *name; 10322 int n = FALSE; 10323 static char *(has_list[]) = 10324 { 10325 #ifdef AMIGA 10326 "amiga", 10327 # ifdef FEAT_ARP 10328 "arp", 10329 # endif 10330 #endif 10331 #ifdef __BEOS__ 10332 "beos", 10333 #endif 10334 #ifdef MSDOS 10335 # ifdef DJGPP 10336 "dos32", 10337 # else 10338 "dos16", 10339 # endif 10340 #endif 10341 #ifdef MACOS 10342 "mac", 10343 #endif 10344 #if defined(MACOS_X_UNIX) 10345 "macunix", 10346 #endif 10347 #ifdef OS2 10348 "os2", 10349 #endif 10350 #ifdef __QNX__ 10351 "qnx", 10352 #endif 10353 #ifdef RISCOS 10354 "riscos", 10355 #endif 10356 #ifdef UNIX 10357 "unix", 10358 #endif 10359 #ifdef VMS 10360 "vms", 10361 #endif 10362 #ifdef WIN16 10363 "win16", 10364 #endif 10365 #ifdef WIN32 10366 "win32", 10367 #endif 10368 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__)) 10369 "win32unix", 10370 #endif 10371 #ifdef WIN64 10372 "win64", 10373 #endif 10374 #ifdef EBCDIC 10375 "ebcdic", 10376 #endif 10377 #ifndef CASE_INSENSITIVE_FILENAME 10378 "fname_case", 10379 #endif 10380 #ifdef FEAT_ARABIC 10381 "arabic", 10382 #endif 10383 #ifdef FEAT_AUTOCMD 10384 "autocmd", 10385 #endif 10386 #ifdef FEAT_BEVAL 10387 "balloon_eval", 10388 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */ 10389 "balloon_multiline", 10390 # endif 10391 #endif 10392 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS) 10393 "builtin_terms", 10394 # ifdef ALL_BUILTIN_TCAPS 10395 "all_builtin_terms", 10396 # endif 10397 #endif 10398 #ifdef FEAT_BYTEOFF 10399 "byte_offset", 10400 #endif 10401 #ifdef FEAT_CINDENT 10402 "cindent", 10403 #endif 10404 #ifdef FEAT_CLIENTSERVER 10405 "clientserver", 10406 #endif 10407 #ifdef FEAT_CLIPBOARD 10408 "clipboard", 10409 #endif 10410 #ifdef FEAT_CMDL_COMPL 10411 "cmdline_compl", 10412 #endif 10413 #ifdef FEAT_CMDHIST 10414 "cmdline_hist", 10415 #endif 10416 #ifdef FEAT_COMMENTS 10417 "comments", 10418 #endif 10419 #ifdef FEAT_CRYPT 10420 "cryptv", 10421 #endif 10422 #ifdef FEAT_CSCOPE 10423 "cscope", 10424 #endif 10425 #ifdef CURSOR_SHAPE 10426 "cursorshape", 10427 #endif 10428 #ifdef DEBUG 10429 "debug", 10430 #endif 10431 #ifdef FEAT_CON_DIALOG 10432 "dialog_con", 10433 #endif 10434 #ifdef FEAT_GUI_DIALOG 10435 "dialog_gui", 10436 #endif 10437 #ifdef FEAT_DIFF 10438 "diff", 10439 #endif 10440 #ifdef FEAT_DIGRAPHS 10441 "digraphs", 10442 #endif 10443 #ifdef FEAT_DND 10444 "dnd", 10445 #endif 10446 #ifdef FEAT_EMACS_TAGS 10447 "emacs_tags", 10448 #endif 10449 "eval", /* always present, of course! */ 10450 #ifdef FEAT_EX_EXTRA 10451 "ex_extra", 10452 #endif 10453 #ifdef FEAT_SEARCH_EXTRA 10454 "extra_search", 10455 #endif 10456 #ifdef FEAT_FKMAP 10457 "farsi", 10458 #endif 10459 #ifdef FEAT_SEARCHPATH 10460 "file_in_path", 10461 #endif 10462 #if defined(UNIX) && !defined(USE_SYSTEM) 10463 "filterpipe", 10464 #endif 10465 #ifdef FEAT_FIND_ID 10466 "find_in_path", 10467 #endif 10468 #ifdef FEAT_FOLDING 10469 "folding", 10470 #endif 10471 #ifdef FEAT_FOOTER 10472 "footer", 10473 #endif 10474 #if !defined(USE_SYSTEM) && defined(UNIX) 10475 "fork", 10476 #endif 10477 #ifdef FEAT_GETTEXT 10478 "gettext", 10479 #endif 10480 #ifdef FEAT_GUI 10481 "gui", 10482 #endif 10483 #ifdef FEAT_GUI_ATHENA 10484 # ifdef FEAT_GUI_NEXTAW 10485 "gui_neXtaw", 10486 # else 10487 "gui_athena", 10488 # endif 10489 #endif 10490 #ifdef FEAT_GUI_GTK 10491 "gui_gtk", 10492 # ifdef HAVE_GTK2 10493 "gui_gtk2", 10494 # endif 10495 #endif 10496 #ifdef FEAT_GUI_MAC 10497 "gui_mac", 10498 #endif 10499 #ifdef FEAT_GUI_MOTIF 10500 "gui_motif", 10501 #endif 10502 #ifdef FEAT_GUI_PHOTON 10503 "gui_photon", 10504 #endif 10505 #ifdef FEAT_GUI_W16 10506 "gui_win16", 10507 #endif 10508 #ifdef FEAT_GUI_W32 10509 "gui_win32", 10510 #endif 10511 #ifdef FEAT_HANGULIN 10512 "hangul_input", 10513 #endif 10514 #if defined(HAVE_ICONV_H) && defined(USE_ICONV) 10515 "iconv", 10516 #endif 10517 #ifdef FEAT_INS_EXPAND 10518 "insert_expand", 10519 #endif 10520 #ifdef FEAT_JUMPLIST 10521 "jumplist", 10522 #endif 10523 #ifdef FEAT_KEYMAP 10524 "keymap", 10525 #endif 10526 #ifdef FEAT_LANGMAP 10527 "langmap", 10528 #endif 10529 #ifdef FEAT_LIBCALL 10530 "libcall", 10531 #endif 10532 #ifdef FEAT_LINEBREAK 10533 "linebreak", 10534 #endif 10535 #ifdef FEAT_LISP 10536 "lispindent", 10537 #endif 10538 #ifdef FEAT_LISTCMDS 10539 "listcmds", 10540 #endif 10541 #ifdef FEAT_LOCALMAP 10542 "localmap", 10543 #endif 10544 #ifdef FEAT_MENU 10545 "menu", 10546 #endif 10547 #ifdef FEAT_SESSION 10548 "mksession", 10549 #endif 10550 #ifdef FEAT_MODIFY_FNAME 10551 "modify_fname", 10552 #endif 10553 #ifdef FEAT_MOUSE 10554 "mouse", 10555 #endif 10556 #ifdef FEAT_MOUSESHAPE 10557 "mouseshape", 10558 #endif 10559 #if defined(UNIX) || defined(VMS) 10560 # ifdef FEAT_MOUSE_DEC 10561 "mouse_dec", 10562 # endif 10563 # ifdef FEAT_MOUSE_GPM 10564 "mouse_gpm", 10565 # endif 10566 # ifdef FEAT_MOUSE_JSB 10567 "mouse_jsbterm", 10568 # endif 10569 # ifdef FEAT_MOUSE_NET 10570 "mouse_netterm", 10571 # endif 10572 # ifdef FEAT_MOUSE_PTERM 10573 "mouse_pterm", 10574 # endif 10575 # ifdef FEAT_MOUSE_XTERM 10576 "mouse_xterm", 10577 # endif 10578 #endif 10579 #ifdef FEAT_MBYTE 10580 "multi_byte", 10581 #endif 10582 #ifdef FEAT_MBYTE_IME 10583 "multi_byte_ime", 10584 #endif 10585 #ifdef FEAT_MULTI_LANG 10586 "multi_lang", 10587 #endif 10588 #ifdef FEAT_MZSCHEME 10589 #ifndef DYNAMIC_MZSCHEME 10590 "mzscheme", 10591 #endif 10592 #endif 10593 #ifdef FEAT_OLE 10594 "ole", 10595 #endif 10596 #ifdef FEAT_OSFILETYPE 10597 "osfiletype", 10598 #endif 10599 #ifdef FEAT_PATH_EXTRA 10600 "path_extra", 10601 #endif 10602 #ifdef FEAT_PERL 10603 #ifndef DYNAMIC_PERL 10604 "perl", 10605 #endif 10606 #endif 10607 #ifdef FEAT_PYTHON 10608 #ifndef DYNAMIC_PYTHON 10609 "python", 10610 #endif 10611 #endif 10612 #ifdef FEAT_POSTSCRIPT 10613 "postscript", 10614 #endif 10615 #ifdef FEAT_PRINTER 10616 "printer", 10617 #endif 10618 #ifdef FEAT_PROFILE 10619 "profile", 10620 #endif 10621 #ifdef FEAT_RELTIME 10622 "reltime", 10623 #endif 10624 #ifdef FEAT_QUICKFIX 10625 "quickfix", 10626 #endif 10627 #ifdef FEAT_RIGHTLEFT 10628 "rightleft", 10629 #endif 10630 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY) 10631 "ruby", 10632 #endif 10633 #ifdef FEAT_SCROLLBIND 10634 "scrollbind", 10635 #endif 10636 #ifdef FEAT_CMDL_INFO 10637 "showcmd", 10638 "cmdline_info", 10639 #endif 10640 #ifdef FEAT_SIGNS 10641 "signs", 10642 #endif 10643 #ifdef FEAT_SMARTINDENT 10644 "smartindent", 10645 #endif 10646 #ifdef FEAT_SNIFF 10647 "sniff", 10648 #endif 10649 #ifdef FEAT_STL_OPT 10650 "statusline", 10651 #endif 10652 #ifdef FEAT_SUN_WORKSHOP 10653 "sun_workshop", 10654 #endif 10655 #ifdef FEAT_NETBEANS_INTG 10656 "netbeans_intg", 10657 #endif 10658 #ifdef FEAT_SPELL 10659 "spell", 10660 #endif 10661 #ifdef FEAT_SYN_HL 10662 "syntax", 10663 #endif 10664 #if defined(USE_SYSTEM) || !defined(UNIX) 10665 "system", 10666 #endif 10667 #ifdef FEAT_TAG_BINS 10668 "tag_binary", 10669 #endif 10670 #ifdef FEAT_TAG_OLDSTATIC 10671 "tag_old_static", 10672 #endif 10673 #ifdef FEAT_TAG_ANYWHITE 10674 "tag_any_white", 10675 #endif 10676 #ifdef FEAT_TCL 10677 # ifndef DYNAMIC_TCL 10678 "tcl", 10679 # endif 10680 #endif 10681 #ifdef TERMINFO 10682 "terminfo", 10683 #endif 10684 #ifdef FEAT_TERMRESPONSE 10685 "termresponse", 10686 #endif 10687 #ifdef FEAT_TEXTOBJ 10688 "textobjects", 10689 #endif 10690 #ifdef HAVE_TGETENT 10691 "tgetent", 10692 #endif 10693 #ifdef FEAT_TITLE 10694 "title", 10695 #endif 10696 #ifdef FEAT_TOOLBAR 10697 "toolbar", 10698 #endif 10699 #ifdef FEAT_USR_CMDS 10700 "user-commands", /* was accidentally included in 5.4 */ 10701 "user_commands", 10702 #endif 10703 #ifdef FEAT_VIMINFO 10704 "viminfo", 10705 #endif 10706 #ifdef FEAT_VERTSPLIT 10707 "vertsplit", 10708 #endif 10709 #ifdef FEAT_VIRTUALEDIT 10710 "virtualedit", 10711 #endif 10712 #ifdef FEAT_VISUAL 10713 "visual", 10714 #endif 10715 #ifdef FEAT_VISUALEXTRA 10716 "visualextra", 10717 #endif 10718 #ifdef FEAT_VREPLACE 10719 "vreplace", 10720 #endif 10721 #ifdef FEAT_WILDIGN 10722 "wildignore", 10723 #endif 10724 #ifdef FEAT_WILDMENU 10725 "wildmenu", 10726 #endif 10727 #ifdef FEAT_WINDOWS 10728 "windows", 10729 #endif 10730 #ifdef FEAT_WAK 10731 "winaltkeys", 10732 #endif 10733 #ifdef FEAT_WRITEBACKUP 10734 "writebackup", 10735 #endif 10736 #ifdef FEAT_XIM 10737 "xim", 10738 #endif 10739 #ifdef FEAT_XFONTSET 10740 "xfontset", 10741 #endif 10742 #ifdef USE_XSMP 10743 "xsmp", 10744 #endif 10745 #ifdef USE_XSMP_INTERACT 10746 "xsmp_interact", 10747 #endif 10748 #ifdef FEAT_XCLIPBOARD 10749 "xterm_clipboard", 10750 #endif 10751 #ifdef FEAT_XTERM_SAVE 10752 "xterm_save", 10753 #endif 10754 #if defined(UNIX) && defined(FEAT_X11) 10755 "X11", 10756 #endif 10757 NULL 10758 }; 10759 10760 name = get_tv_string(&argvars[0]); 10761 for (i = 0; has_list[i] != NULL; ++i) 10762 if (STRICMP(name, has_list[i]) == 0) 10763 { 10764 n = TRUE; 10765 break; 10766 } 10767 10768 if (n == FALSE) 10769 { 10770 if (STRNICMP(name, "patch", 5) == 0) 10771 n = has_patch(atoi((char *)name + 5)); 10772 else if (STRICMP(name, "vim_starting") == 0) 10773 n = (starting != 0); 10774 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32) 10775 else if (STRICMP(name, "balloon_multiline") == 0) 10776 n = multiline_balloon_available(); 10777 #endif 10778 #ifdef DYNAMIC_TCL 10779 else if (STRICMP(name, "tcl") == 0) 10780 n = tcl_enabled(FALSE); 10781 #endif 10782 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV) 10783 else if (STRICMP(name, "iconv") == 0) 10784 n = iconv_enabled(FALSE); 10785 #endif 10786 #ifdef DYNAMIC_MZSCHEME 10787 else if (STRICMP(name, "mzscheme") == 0) 10788 n = mzscheme_enabled(FALSE); 10789 #endif 10790 #ifdef DYNAMIC_RUBY 10791 else if (STRICMP(name, "ruby") == 0) 10792 n = ruby_enabled(FALSE); 10793 #endif 10794 #ifdef DYNAMIC_PYTHON 10795 else if (STRICMP(name, "python") == 0) 10796 n = python_enabled(FALSE); 10797 #endif 10798 #ifdef DYNAMIC_PERL 10799 else if (STRICMP(name, "perl") == 0) 10800 n = perl_enabled(FALSE); 10801 #endif 10802 #ifdef FEAT_GUI 10803 else if (STRICMP(name, "gui_running") == 0) 10804 n = (gui.in_use || gui.starting); 10805 # ifdef FEAT_GUI_W32 10806 else if (STRICMP(name, "gui_win32s") == 0) 10807 n = gui_is_win32s(); 10808 # endif 10809 # ifdef FEAT_BROWSE 10810 else if (STRICMP(name, "browse") == 0) 10811 n = gui.in_use; /* gui_mch_browse() works when GUI is running */ 10812 # endif 10813 #endif 10814 #ifdef FEAT_SYN_HL 10815 else if (STRICMP(name, "syntax_items") == 0) 10816 n = syntax_present(curbuf); 10817 #endif 10818 #if defined(WIN3264) 10819 else if (STRICMP(name, "win95") == 0) 10820 n = mch_windows95(); 10821 #endif 10822 #ifdef FEAT_NETBEANS_INTG 10823 else if (STRICMP(name, "netbeans_enabled") == 0) 10824 n = usingNetbeans; 10825 #endif 10826 } 10827 10828 rettv->vval.v_number = n; 10829 } 10830 10831 /* 10832 * "has_key()" function 10833 */ 10834 static void 10835 f_has_key(argvars, rettv) 10836 typval_T *argvars; 10837 typval_T *rettv; 10838 { 10839 rettv->vval.v_number = 0; 10840 if (argvars[0].v_type != VAR_DICT) 10841 { 10842 EMSG(_(e_dictreq)); 10843 return; 10844 } 10845 if (argvars[0].vval.v_dict == NULL) 10846 return; 10847 10848 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict, 10849 get_tv_string(&argvars[1]), -1) != NULL; 10850 } 10851 10852 /* 10853 * "hasmapto()" function 10854 */ 10855 static void 10856 f_hasmapto(argvars, rettv) 10857 typval_T *argvars; 10858 typval_T *rettv; 10859 { 10860 char_u *name; 10861 char_u *mode; 10862 char_u buf[NUMBUFLEN]; 10863 int abbr = FALSE; 10864 10865 name = get_tv_string(&argvars[0]); 10866 if (argvars[1].v_type == VAR_UNKNOWN) 10867 mode = (char_u *)"nvo"; 10868 else 10869 { 10870 mode = get_tv_string_buf(&argvars[1], buf); 10871 if (argvars[2].v_type != VAR_UNKNOWN) 10872 abbr = get_tv_number(&argvars[2]); 10873 } 10874 10875 if (map_to_exists(name, mode, abbr)) 10876 rettv->vval.v_number = TRUE; 10877 else 10878 rettv->vval.v_number = FALSE; 10879 } 10880 10881 /* 10882 * "histadd()" function 10883 */ 10884 /*ARGSUSED*/ 10885 static void 10886 f_histadd(argvars, rettv) 10887 typval_T *argvars; 10888 typval_T *rettv; 10889 { 10890 #ifdef FEAT_CMDHIST 10891 int histype; 10892 char_u *str; 10893 char_u buf[NUMBUFLEN]; 10894 #endif 10895 10896 rettv->vval.v_number = FALSE; 10897 if (check_restricted() || check_secure()) 10898 return; 10899 #ifdef FEAT_CMDHIST 10900 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ 10901 histype = str != NULL ? get_histtype(str) : -1; 10902 if (histype >= 0) 10903 { 10904 str = get_tv_string_buf(&argvars[1], buf); 10905 if (*str != NUL) 10906 { 10907 add_to_history(histype, str, FALSE, NUL); 10908 rettv->vval.v_number = TRUE; 10909 return; 10910 } 10911 } 10912 #endif 10913 } 10914 10915 /* 10916 * "histdel()" function 10917 */ 10918 /*ARGSUSED*/ 10919 static void 10920 f_histdel(argvars, rettv) 10921 typval_T *argvars; 10922 typval_T *rettv; 10923 { 10924 #ifdef FEAT_CMDHIST 10925 int n; 10926 char_u buf[NUMBUFLEN]; 10927 char_u *str; 10928 10929 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ 10930 if (str == NULL) 10931 n = 0; 10932 else if (argvars[1].v_type == VAR_UNKNOWN) 10933 /* only one argument: clear entire history */ 10934 n = clr_history(get_histtype(str)); 10935 else if (argvars[1].v_type == VAR_NUMBER) 10936 /* index given: remove that entry */ 10937 n = del_history_idx(get_histtype(str), 10938 (int)get_tv_number(&argvars[1])); 10939 else 10940 /* string given: remove all matching entries */ 10941 n = del_history_entry(get_histtype(str), 10942 get_tv_string_buf(&argvars[1], buf)); 10943 rettv->vval.v_number = n; 10944 #else 10945 rettv->vval.v_number = 0; 10946 #endif 10947 } 10948 10949 /* 10950 * "histget()" function 10951 */ 10952 /*ARGSUSED*/ 10953 static void 10954 f_histget(argvars, rettv) 10955 typval_T *argvars; 10956 typval_T *rettv; 10957 { 10958 #ifdef FEAT_CMDHIST 10959 int type; 10960 int idx; 10961 char_u *str; 10962 10963 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ 10964 if (str == NULL) 10965 rettv->vval.v_string = NULL; 10966 else 10967 { 10968 type = get_histtype(str); 10969 if (argvars[1].v_type == VAR_UNKNOWN) 10970 idx = get_history_idx(type); 10971 else 10972 idx = (int)get_tv_number_chk(&argvars[1], NULL); 10973 /* -1 on type error */ 10974 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx)); 10975 } 10976 #else 10977 rettv->vval.v_string = NULL; 10978 #endif 10979 rettv->v_type = VAR_STRING; 10980 } 10981 10982 /* 10983 * "histnr()" function 10984 */ 10985 /*ARGSUSED*/ 10986 static void 10987 f_histnr(argvars, rettv) 10988 typval_T *argvars; 10989 typval_T *rettv; 10990 { 10991 int i; 10992 10993 #ifdef FEAT_CMDHIST 10994 char_u *history = get_tv_string_chk(&argvars[0]); 10995 10996 i = history == NULL ? HIST_CMD - 1 : get_histtype(history); 10997 if (i >= HIST_CMD && i < HIST_COUNT) 10998 i = get_history_idx(i); 10999 else 11000 #endif 11001 i = -1; 11002 rettv->vval.v_number = i; 11003 } 11004 11005 /* 11006 * "highlightID(name)" function 11007 */ 11008 static void 11009 f_hlID(argvars, rettv) 11010 typval_T *argvars; 11011 typval_T *rettv; 11012 { 11013 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0])); 11014 } 11015 11016 /* 11017 * "highlight_exists()" function 11018 */ 11019 static void 11020 f_hlexists(argvars, rettv) 11021 typval_T *argvars; 11022 typval_T *rettv; 11023 { 11024 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0])); 11025 } 11026 11027 /* 11028 * "hostname()" function 11029 */ 11030 /*ARGSUSED*/ 11031 static void 11032 f_hostname(argvars, rettv) 11033 typval_T *argvars; 11034 typval_T *rettv; 11035 { 11036 char_u hostname[256]; 11037 11038 mch_get_host_name(hostname, 256); 11039 rettv->v_type = VAR_STRING; 11040 rettv->vval.v_string = vim_strsave(hostname); 11041 } 11042 11043 /* 11044 * iconv() function 11045 */ 11046 /*ARGSUSED*/ 11047 static void 11048 f_iconv(argvars, rettv) 11049 typval_T *argvars; 11050 typval_T *rettv; 11051 { 11052 #ifdef FEAT_MBYTE 11053 char_u buf1[NUMBUFLEN]; 11054 char_u buf2[NUMBUFLEN]; 11055 char_u *from, *to, *str; 11056 vimconv_T vimconv; 11057 #endif 11058 11059 rettv->v_type = VAR_STRING; 11060 rettv->vval.v_string = NULL; 11061 11062 #ifdef FEAT_MBYTE 11063 str = get_tv_string(&argvars[0]); 11064 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1))); 11065 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2))); 11066 vimconv.vc_type = CONV_NONE; 11067 convert_setup(&vimconv, from, to); 11068 11069 /* If the encodings are equal, no conversion needed. */ 11070 if (vimconv.vc_type == CONV_NONE) 11071 rettv->vval.v_string = vim_strsave(str); 11072 else 11073 rettv->vval.v_string = string_convert(&vimconv, str, NULL); 11074 11075 convert_setup(&vimconv, NULL, NULL); 11076 vim_free(from); 11077 vim_free(to); 11078 #endif 11079 } 11080 11081 /* 11082 * "indent()" function 11083 */ 11084 static void 11085 f_indent(argvars, rettv) 11086 typval_T *argvars; 11087 typval_T *rettv; 11088 { 11089 linenr_T lnum; 11090 11091 lnum = get_tv_lnum(argvars); 11092 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 11093 rettv->vval.v_number = get_indent_lnum(lnum); 11094 else 11095 rettv->vval.v_number = -1; 11096 } 11097 11098 /* 11099 * "index()" function 11100 */ 11101 static void 11102 f_index(argvars, rettv) 11103 typval_T *argvars; 11104 typval_T *rettv; 11105 { 11106 list_T *l; 11107 listitem_T *item; 11108 long idx = 0; 11109 int ic = FALSE; 11110 11111 rettv->vval.v_number = -1; 11112 if (argvars[0].v_type != VAR_LIST) 11113 { 11114 EMSG(_(e_listreq)); 11115 return; 11116 } 11117 l = argvars[0].vval.v_list; 11118 if (l != NULL) 11119 { 11120 item = l->lv_first; 11121 if (argvars[2].v_type != VAR_UNKNOWN) 11122 { 11123 int error = FALSE; 11124 11125 /* Start at specified item. Use the cached index that list_find() 11126 * sets, so that a negative number also works. */ 11127 item = list_find(l, get_tv_number_chk(&argvars[2], &error)); 11128 idx = l->lv_idx; 11129 if (argvars[3].v_type != VAR_UNKNOWN) 11130 ic = get_tv_number_chk(&argvars[3], &error); 11131 if (error) 11132 item = NULL; 11133 } 11134 11135 for ( ; item != NULL; item = item->li_next, ++idx) 11136 if (tv_equal(&item->li_tv, &argvars[1], ic)) 11137 { 11138 rettv->vval.v_number = idx; 11139 break; 11140 } 11141 } 11142 } 11143 11144 static int inputsecret_flag = 0; 11145 11146 /* 11147 * "input()" function 11148 * Also handles inputsecret() when inputsecret is set. 11149 */ 11150 static void 11151 f_input(argvars, rettv) 11152 typval_T *argvars; 11153 typval_T *rettv; 11154 { 11155 char_u *prompt = get_tv_string_chk(&argvars[0]); 11156 char_u *p = NULL; 11157 int c; 11158 char_u buf[NUMBUFLEN]; 11159 int cmd_silent_save = cmd_silent; 11160 char_u *defstr = (char_u *)""; 11161 int xp_type = EXPAND_NOTHING; 11162 char_u *xp_arg = NULL; 11163 11164 rettv->v_type = VAR_STRING; 11165 11166 #ifdef NO_CONSOLE_INPUT 11167 /* While starting up, there is no place to enter text. */ 11168 if (no_console_input()) 11169 { 11170 rettv->vval.v_string = NULL; 11171 return; 11172 } 11173 #endif 11174 11175 cmd_silent = FALSE; /* Want to see the prompt. */ 11176 if (prompt != NULL) 11177 { 11178 /* Only the part of the message after the last NL is considered as 11179 * prompt for the command line */ 11180 p = vim_strrchr(prompt, '\n'); 11181 if (p == NULL) 11182 p = prompt; 11183 else 11184 { 11185 ++p; 11186 c = *p; 11187 *p = NUL; 11188 msg_start(); 11189 msg_clr_eos(); 11190 msg_puts_attr(prompt, echo_attr); 11191 msg_didout = FALSE; 11192 msg_starthere(); 11193 *p = c; 11194 } 11195 cmdline_row = msg_row; 11196 11197 if (argvars[1].v_type != VAR_UNKNOWN) 11198 { 11199 defstr = get_tv_string_buf_chk(&argvars[1], buf); 11200 if (defstr != NULL) 11201 stuffReadbuffSpec(defstr); 11202 11203 if (argvars[2].v_type != VAR_UNKNOWN) 11204 { 11205 char_u *xp_name; 11206 int xp_namelen; 11207 long argt; 11208 11209 rettv->vval.v_string = NULL; 11210 11211 xp_name = get_tv_string_buf_chk(&argvars[2], buf); 11212 if (xp_name == NULL) 11213 return; 11214 11215 xp_namelen = STRLEN(xp_name); 11216 11217 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt, 11218 &xp_arg) == FAIL) 11219 return; 11220 } 11221 } 11222 11223 if (defstr != NULL) 11224 rettv->vval.v_string = 11225 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr, 11226 xp_type, xp_arg); 11227 11228 vim_free(xp_arg); 11229 11230 /* since the user typed this, no need to wait for return */ 11231 need_wait_return = FALSE; 11232 msg_didout = FALSE; 11233 } 11234 cmd_silent = cmd_silent_save; 11235 } 11236 11237 /* 11238 * "inputdialog()" function 11239 */ 11240 static void 11241 f_inputdialog(argvars, rettv) 11242 typval_T *argvars; 11243 typval_T *rettv; 11244 { 11245 #if defined(FEAT_GUI_TEXTDIALOG) 11246 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */ 11247 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL) 11248 { 11249 char_u *message; 11250 char_u buf[NUMBUFLEN]; 11251 char_u *defstr = (char_u *)""; 11252 11253 message = get_tv_string_chk(&argvars[0]); 11254 if (argvars[1].v_type != VAR_UNKNOWN 11255 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL) 11256 vim_strncpy(IObuff, defstr, IOSIZE - 1); 11257 else 11258 IObuff[0] = NUL; 11259 if (message != NULL && defstr != NULL 11260 && do_dialog(VIM_QUESTION, NULL, message, 11261 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1) 11262 rettv->vval.v_string = vim_strsave(IObuff); 11263 else 11264 { 11265 if (message != NULL && defstr != NULL 11266 && argvars[1].v_type != VAR_UNKNOWN 11267 && argvars[2].v_type != VAR_UNKNOWN) 11268 rettv->vval.v_string = vim_strsave( 11269 get_tv_string_buf(&argvars[2], buf)); 11270 else 11271 rettv->vval.v_string = NULL; 11272 } 11273 rettv->v_type = VAR_STRING; 11274 } 11275 else 11276 #endif 11277 f_input(argvars, rettv); 11278 } 11279 11280 /* 11281 * "inputlist()" function 11282 */ 11283 static void 11284 f_inputlist(argvars, rettv) 11285 typval_T *argvars; 11286 typval_T *rettv; 11287 { 11288 listitem_T *li; 11289 int selected; 11290 int mouse_used; 11291 11292 rettv->vval.v_number = 0; 11293 #ifdef NO_CONSOLE_INPUT 11294 /* While starting up, there is no place to enter text. */ 11295 if (no_console_input()) 11296 return; 11297 #endif 11298 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL) 11299 { 11300 EMSG2(_(e_listarg), "inputlist()"); 11301 return; 11302 } 11303 11304 msg_start(); 11305 lines_left = Rows; /* avoid more prompt */ 11306 msg_scroll = TRUE; 11307 msg_clr_eos(); 11308 11309 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next) 11310 { 11311 msg_puts(get_tv_string(&li->li_tv)); 11312 msg_putchar('\n'); 11313 } 11314 11315 /* Ask for choice. */ 11316 selected = prompt_for_number(&mouse_used); 11317 if (mouse_used) 11318 selected -= lines_left; 11319 11320 rettv->vval.v_number = selected; 11321 } 11322 11323 11324 static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL}; 11325 11326 /* 11327 * "inputrestore()" function 11328 */ 11329 /*ARGSUSED*/ 11330 static void 11331 f_inputrestore(argvars, rettv) 11332 typval_T *argvars; 11333 typval_T *rettv; 11334 { 11335 if (ga_userinput.ga_len > 0) 11336 { 11337 --ga_userinput.ga_len; 11338 restore_typeahead((tasave_T *)(ga_userinput.ga_data) 11339 + ga_userinput.ga_len); 11340 rettv->vval.v_number = 0; /* OK */ 11341 } 11342 else if (p_verbose > 1) 11343 { 11344 verb_msg((char_u *)_("called inputrestore() more often than inputsave()")); 11345 rettv->vval.v_number = 1; /* Failed */ 11346 } 11347 } 11348 11349 /* 11350 * "inputsave()" function 11351 */ 11352 /*ARGSUSED*/ 11353 static void 11354 f_inputsave(argvars, rettv) 11355 typval_T *argvars; 11356 typval_T *rettv; 11357 { 11358 /* Add an entry to the stack of typehead storage. */ 11359 if (ga_grow(&ga_userinput, 1) == OK) 11360 { 11361 save_typeahead((tasave_T *)(ga_userinput.ga_data) 11362 + ga_userinput.ga_len); 11363 ++ga_userinput.ga_len; 11364 rettv->vval.v_number = 0; /* OK */ 11365 } 11366 else 11367 rettv->vval.v_number = 1; /* Failed */ 11368 } 11369 11370 /* 11371 * "inputsecret()" function 11372 */ 11373 static void 11374 f_inputsecret(argvars, rettv) 11375 typval_T *argvars; 11376 typval_T *rettv; 11377 { 11378 ++cmdline_star; 11379 ++inputsecret_flag; 11380 f_input(argvars, rettv); 11381 --cmdline_star; 11382 --inputsecret_flag; 11383 } 11384 11385 /* 11386 * "insert()" function 11387 */ 11388 static void 11389 f_insert(argvars, rettv) 11390 typval_T *argvars; 11391 typval_T *rettv; 11392 { 11393 long before = 0; 11394 listitem_T *item; 11395 list_T *l; 11396 int error = FALSE; 11397 11398 rettv->vval.v_number = 0; 11399 if (argvars[0].v_type != VAR_LIST) 11400 EMSG2(_(e_listarg), "insert()"); 11401 else if ((l = argvars[0].vval.v_list) != NULL 11402 && !tv_check_lock(l->lv_lock, (char_u *)"insert()")) 11403 { 11404 if (argvars[2].v_type != VAR_UNKNOWN) 11405 before = get_tv_number_chk(&argvars[2], &error); 11406 if (error) 11407 return; /* type error; errmsg already given */ 11408 11409 if (before == l->lv_len) 11410 item = NULL; 11411 else 11412 { 11413 item = list_find(l, before); 11414 if (item == NULL) 11415 { 11416 EMSGN(_(e_listidx), before); 11417 l = NULL; 11418 } 11419 } 11420 if (l != NULL) 11421 { 11422 list_insert_tv(l, &argvars[1], item); 11423 copy_tv(&argvars[0], rettv); 11424 } 11425 } 11426 } 11427 11428 /* 11429 * "isdirectory()" function 11430 */ 11431 static void 11432 f_isdirectory(argvars, rettv) 11433 typval_T *argvars; 11434 typval_T *rettv; 11435 { 11436 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0])); 11437 } 11438 11439 /* 11440 * "islocked()" function 11441 */ 11442 static void 11443 f_islocked(argvars, rettv) 11444 typval_T *argvars; 11445 typval_T *rettv; 11446 { 11447 lval_T lv; 11448 char_u *end; 11449 dictitem_T *di; 11450 11451 rettv->vval.v_number = -1; 11452 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE, 11453 FNE_CHECK_START); 11454 if (end != NULL && lv.ll_name != NULL) 11455 { 11456 if (*end != NUL) 11457 EMSG(_(e_trailing)); 11458 else 11459 { 11460 if (lv.ll_tv == NULL) 11461 { 11462 if (check_changedtick(lv.ll_name)) 11463 rettv->vval.v_number = 1; /* always locked */ 11464 else 11465 { 11466 di = find_var(lv.ll_name, NULL); 11467 if (di != NULL) 11468 { 11469 /* Consider a variable locked when: 11470 * 1. the variable itself is locked 11471 * 2. the value of the variable is locked. 11472 * 3. the List or Dict value is locked. 11473 */ 11474 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK) 11475 || tv_islocked(&di->di_tv)); 11476 } 11477 } 11478 } 11479 else if (lv.ll_range) 11480 EMSG(_("E745: Range not allowed")); 11481 else if (lv.ll_newkey != NULL) 11482 EMSG2(_(e_dictkey), lv.ll_newkey); 11483 else if (lv.ll_list != NULL) 11484 /* List item. */ 11485 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv); 11486 else 11487 /* Dictionary item. */ 11488 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv); 11489 } 11490 } 11491 11492 clear_lval(&lv); 11493 } 11494 11495 static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what)); 11496 11497 /* 11498 * Turn a dict into a list: 11499 * "what" == 0: list of keys 11500 * "what" == 1: list of values 11501 * "what" == 2: list of items 11502 */ 11503 static void 11504 dict_list(argvars, rettv, what) 11505 typval_T *argvars; 11506 typval_T *rettv; 11507 int what; 11508 { 11509 list_T *l2; 11510 dictitem_T *di; 11511 hashitem_T *hi; 11512 listitem_T *li; 11513 listitem_T *li2; 11514 dict_T *d; 11515 int todo; 11516 11517 rettv->vval.v_number = 0; 11518 if (argvars[0].v_type != VAR_DICT) 11519 { 11520 EMSG(_(e_dictreq)); 11521 return; 11522 } 11523 if ((d = argvars[0].vval.v_dict) == NULL) 11524 return; 11525 11526 if (rettv_list_alloc(rettv) == FAIL) 11527 return; 11528 11529 todo = d->dv_hashtab.ht_used; 11530 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 11531 { 11532 if (!HASHITEM_EMPTY(hi)) 11533 { 11534 --todo; 11535 di = HI2DI(hi); 11536 11537 li = listitem_alloc(); 11538 if (li == NULL) 11539 break; 11540 list_append(rettv->vval.v_list, li); 11541 11542 if (what == 0) 11543 { 11544 /* keys() */ 11545 li->li_tv.v_type = VAR_STRING; 11546 li->li_tv.v_lock = 0; 11547 li->li_tv.vval.v_string = vim_strsave(di->di_key); 11548 } 11549 else if (what == 1) 11550 { 11551 /* values() */ 11552 copy_tv(&di->di_tv, &li->li_tv); 11553 } 11554 else 11555 { 11556 /* items() */ 11557 l2 = list_alloc(); 11558 li->li_tv.v_type = VAR_LIST; 11559 li->li_tv.v_lock = 0; 11560 li->li_tv.vval.v_list = l2; 11561 if (l2 == NULL) 11562 break; 11563 ++l2->lv_refcount; 11564 11565 li2 = listitem_alloc(); 11566 if (li2 == NULL) 11567 break; 11568 list_append(l2, li2); 11569 li2->li_tv.v_type = VAR_STRING; 11570 li2->li_tv.v_lock = 0; 11571 li2->li_tv.vval.v_string = vim_strsave(di->di_key); 11572 11573 li2 = listitem_alloc(); 11574 if (li2 == NULL) 11575 break; 11576 list_append(l2, li2); 11577 copy_tv(&di->di_tv, &li2->li_tv); 11578 } 11579 } 11580 } 11581 } 11582 11583 /* 11584 * "items(dict)" function 11585 */ 11586 static void 11587 f_items(argvars, rettv) 11588 typval_T *argvars; 11589 typval_T *rettv; 11590 { 11591 dict_list(argvars, rettv, 2); 11592 } 11593 11594 /* 11595 * "join()" function 11596 */ 11597 static void 11598 f_join(argvars, rettv) 11599 typval_T *argvars; 11600 typval_T *rettv; 11601 { 11602 garray_T ga; 11603 char_u *sep; 11604 11605 rettv->vval.v_number = 0; 11606 if (argvars[0].v_type != VAR_LIST) 11607 { 11608 EMSG(_(e_listreq)); 11609 return; 11610 } 11611 if (argvars[0].vval.v_list == NULL) 11612 return; 11613 if (argvars[1].v_type == VAR_UNKNOWN) 11614 sep = (char_u *)" "; 11615 else 11616 sep = get_tv_string_chk(&argvars[1]); 11617 11618 rettv->v_type = VAR_STRING; 11619 11620 if (sep != NULL) 11621 { 11622 ga_init2(&ga, (int)sizeof(char), 80); 11623 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0); 11624 ga_append(&ga, NUL); 11625 rettv->vval.v_string = (char_u *)ga.ga_data; 11626 } 11627 else 11628 rettv->vval.v_string = NULL; 11629 } 11630 11631 /* 11632 * "keys()" function 11633 */ 11634 static void 11635 f_keys(argvars, rettv) 11636 typval_T *argvars; 11637 typval_T *rettv; 11638 { 11639 dict_list(argvars, rettv, 0); 11640 } 11641 11642 /* 11643 * "last_buffer_nr()" function. 11644 */ 11645 /*ARGSUSED*/ 11646 static void 11647 f_last_buffer_nr(argvars, rettv) 11648 typval_T *argvars; 11649 typval_T *rettv; 11650 { 11651 int n = 0; 11652 buf_T *buf; 11653 11654 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 11655 if (n < buf->b_fnum) 11656 n = buf->b_fnum; 11657 11658 rettv->vval.v_number = n; 11659 } 11660 11661 /* 11662 * "len()" function 11663 */ 11664 static void 11665 f_len(argvars, rettv) 11666 typval_T *argvars; 11667 typval_T *rettv; 11668 { 11669 switch (argvars[0].v_type) 11670 { 11671 case VAR_STRING: 11672 case VAR_NUMBER: 11673 rettv->vval.v_number = (varnumber_T)STRLEN( 11674 get_tv_string(&argvars[0])); 11675 break; 11676 case VAR_LIST: 11677 rettv->vval.v_number = list_len(argvars[0].vval.v_list); 11678 break; 11679 case VAR_DICT: 11680 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict); 11681 break; 11682 default: 11683 EMSG(_("E701: Invalid type for len()")); 11684 break; 11685 } 11686 } 11687 11688 static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type)); 11689 11690 static void 11691 libcall_common(argvars, rettv, type) 11692 typval_T *argvars; 11693 typval_T *rettv; 11694 int type; 11695 { 11696 #ifdef FEAT_LIBCALL 11697 char_u *string_in; 11698 char_u **string_result; 11699 int nr_result; 11700 #endif 11701 11702 rettv->v_type = type; 11703 if (type == VAR_NUMBER) 11704 rettv->vval.v_number = 0; 11705 else 11706 rettv->vval.v_string = NULL; 11707 11708 if (check_restricted() || check_secure()) 11709 return; 11710 11711 #ifdef FEAT_LIBCALL 11712 /* The first two args must be strings, otherwise its meaningless */ 11713 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING) 11714 { 11715 string_in = NULL; 11716 if (argvars[2].v_type == VAR_STRING) 11717 string_in = argvars[2].vval.v_string; 11718 if (type == VAR_NUMBER) 11719 string_result = NULL; 11720 else 11721 string_result = &rettv->vval.v_string; 11722 if (mch_libcall(argvars[0].vval.v_string, 11723 argvars[1].vval.v_string, 11724 string_in, 11725 argvars[2].vval.v_number, 11726 string_result, 11727 &nr_result) == OK 11728 && type == VAR_NUMBER) 11729 rettv->vval.v_number = nr_result; 11730 } 11731 #endif 11732 } 11733 11734 /* 11735 * "libcall()" function 11736 */ 11737 static void 11738 f_libcall(argvars, rettv) 11739 typval_T *argvars; 11740 typval_T *rettv; 11741 { 11742 libcall_common(argvars, rettv, VAR_STRING); 11743 } 11744 11745 /* 11746 * "libcallnr()" function 11747 */ 11748 static void 11749 f_libcallnr(argvars, rettv) 11750 typval_T *argvars; 11751 typval_T *rettv; 11752 { 11753 libcall_common(argvars, rettv, VAR_NUMBER); 11754 } 11755 11756 /* 11757 * "line(string)" function 11758 */ 11759 static void 11760 f_line(argvars, rettv) 11761 typval_T *argvars; 11762 typval_T *rettv; 11763 { 11764 linenr_T lnum = 0; 11765 pos_T *fp; 11766 int fnum; 11767 11768 fp = var2fpos(&argvars[0], TRUE, &fnum); 11769 if (fp != NULL) 11770 lnum = fp->lnum; 11771 rettv->vval.v_number = lnum; 11772 } 11773 11774 /* 11775 * "line2byte(lnum)" function 11776 */ 11777 /*ARGSUSED*/ 11778 static void 11779 f_line2byte(argvars, rettv) 11780 typval_T *argvars; 11781 typval_T *rettv; 11782 { 11783 #ifndef FEAT_BYTEOFF 11784 rettv->vval.v_number = -1; 11785 #else 11786 linenr_T lnum; 11787 11788 lnum = get_tv_lnum(argvars); 11789 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) 11790 rettv->vval.v_number = -1; 11791 else 11792 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL); 11793 if (rettv->vval.v_number >= 0) 11794 ++rettv->vval.v_number; 11795 #endif 11796 } 11797 11798 /* 11799 * "lispindent(lnum)" function 11800 */ 11801 static void 11802 f_lispindent(argvars, rettv) 11803 typval_T *argvars; 11804 typval_T *rettv; 11805 { 11806 #ifdef FEAT_LISP 11807 pos_T pos; 11808 linenr_T lnum; 11809 11810 pos = curwin->w_cursor; 11811 lnum = get_tv_lnum(argvars); 11812 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) 11813 { 11814 curwin->w_cursor.lnum = lnum; 11815 rettv->vval.v_number = get_lisp_indent(); 11816 curwin->w_cursor = pos; 11817 } 11818 else 11819 #endif 11820 rettv->vval.v_number = -1; 11821 } 11822 11823 /* 11824 * "localtime()" function 11825 */ 11826 /*ARGSUSED*/ 11827 static void 11828 f_localtime(argvars, rettv) 11829 typval_T *argvars; 11830 typval_T *rettv; 11831 { 11832 rettv->vval.v_number = (varnumber_T)time(NULL); 11833 } 11834 11835 static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact)); 11836 11837 static void 11838 get_maparg(argvars, rettv, exact) 11839 typval_T *argvars; 11840 typval_T *rettv; 11841 int exact; 11842 { 11843 char_u *keys; 11844 char_u *which; 11845 char_u buf[NUMBUFLEN]; 11846 char_u *keys_buf = NULL; 11847 char_u *rhs; 11848 int mode; 11849 garray_T ga; 11850 int abbr = FALSE; 11851 11852 /* return empty string for failure */ 11853 rettv->v_type = VAR_STRING; 11854 rettv->vval.v_string = NULL; 11855 11856 keys = get_tv_string(&argvars[0]); 11857 if (*keys == NUL) 11858 return; 11859 11860 if (argvars[1].v_type != VAR_UNKNOWN) 11861 { 11862 which = get_tv_string_buf_chk(&argvars[1], buf); 11863 if (argvars[2].v_type != VAR_UNKNOWN) 11864 abbr = get_tv_number(&argvars[2]); 11865 } 11866 else 11867 which = (char_u *)""; 11868 if (which == NULL) 11869 return; 11870 11871 mode = get_map_mode(&which, 0); 11872 11873 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE); 11874 rhs = check_map(keys, mode, exact, FALSE, abbr); 11875 vim_free(keys_buf); 11876 if (rhs != NULL) 11877 { 11878 ga_init(&ga); 11879 ga.ga_itemsize = 1; 11880 ga.ga_growsize = 40; 11881 11882 while (*rhs != NUL) 11883 ga_concat(&ga, str2special(&rhs, FALSE)); 11884 11885 ga_append(&ga, NUL); 11886 rettv->vval.v_string = (char_u *)ga.ga_data; 11887 } 11888 } 11889 11890 /* 11891 * "map()" function 11892 */ 11893 static void 11894 f_map(argvars, rettv) 11895 typval_T *argvars; 11896 typval_T *rettv; 11897 { 11898 filter_map(argvars, rettv, TRUE); 11899 } 11900 11901 /* 11902 * "maparg()" function 11903 */ 11904 static void 11905 f_maparg(argvars, rettv) 11906 typval_T *argvars; 11907 typval_T *rettv; 11908 { 11909 get_maparg(argvars, rettv, TRUE); 11910 } 11911 11912 /* 11913 * "mapcheck()" function 11914 */ 11915 static void 11916 f_mapcheck(argvars, rettv) 11917 typval_T *argvars; 11918 typval_T *rettv; 11919 { 11920 get_maparg(argvars, rettv, FALSE); 11921 } 11922 11923 static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start)); 11924 11925 static void 11926 find_some_match(argvars, rettv, type) 11927 typval_T *argvars; 11928 typval_T *rettv; 11929 int type; 11930 { 11931 char_u *str = NULL; 11932 char_u *expr = NULL; 11933 char_u *pat; 11934 regmatch_T regmatch; 11935 char_u patbuf[NUMBUFLEN]; 11936 char_u strbuf[NUMBUFLEN]; 11937 char_u *save_cpo; 11938 long start = 0; 11939 long nth = 1; 11940 colnr_T startcol = 0; 11941 int match = 0; 11942 list_T *l = NULL; 11943 listitem_T *li = NULL; 11944 long idx = 0; 11945 char_u *tofree = NULL; 11946 11947 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 11948 save_cpo = p_cpo; 11949 p_cpo = (char_u *)""; 11950 11951 rettv->vval.v_number = -1; 11952 if (type == 3) 11953 { 11954 /* return empty list when there are no matches */ 11955 if (rettv_list_alloc(rettv) == FAIL) 11956 goto theend; 11957 } 11958 else if (type == 2) 11959 { 11960 rettv->v_type = VAR_STRING; 11961 rettv->vval.v_string = NULL; 11962 } 11963 11964 if (argvars[0].v_type == VAR_LIST) 11965 { 11966 if ((l = argvars[0].vval.v_list) == NULL) 11967 goto theend; 11968 li = l->lv_first; 11969 } 11970 else 11971 expr = str = get_tv_string(&argvars[0]); 11972 11973 pat = get_tv_string_buf_chk(&argvars[1], patbuf); 11974 if (pat == NULL) 11975 goto theend; 11976 11977 if (argvars[2].v_type != VAR_UNKNOWN) 11978 { 11979 int error = FALSE; 11980 11981 start = get_tv_number_chk(&argvars[2], &error); 11982 if (error) 11983 goto theend; 11984 if (l != NULL) 11985 { 11986 li = list_find(l, start); 11987 if (li == NULL) 11988 goto theend; 11989 idx = l->lv_idx; /* use the cached index */ 11990 } 11991 else 11992 { 11993 if (start < 0) 11994 start = 0; 11995 if (start > (long)STRLEN(str)) 11996 goto theend; 11997 /* When "count" argument is there ignore matches before "start", 11998 * otherwise skip part of the string. Differs when pattern is "^" 11999 * or "\<". */ 12000 if (argvars[3].v_type != VAR_UNKNOWN) 12001 startcol = start; 12002 else 12003 str += start; 12004 } 12005 12006 if (argvars[3].v_type != VAR_UNKNOWN) 12007 nth = get_tv_number_chk(&argvars[3], &error); 12008 if (error) 12009 goto theend; 12010 } 12011 12012 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 12013 if (regmatch.regprog != NULL) 12014 { 12015 regmatch.rm_ic = p_ic; 12016 12017 for (;;) 12018 { 12019 if (l != NULL) 12020 { 12021 if (li == NULL) 12022 { 12023 match = FALSE; 12024 break; 12025 } 12026 vim_free(tofree); 12027 str = echo_string(&li->li_tv, &tofree, strbuf, 0); 12028 if (str == NULL) 12029 break; 12030 } 12031 12032 match = vim_regexec_nl(®match, str, (colnr_T)startcol); 12033 12034 if (match && --nth <= 0) 12035 break; 12036 if (l == NULL && !match) 12037 break; 12038 12039 /* Advance to just after the match. */ 12040 if (l != NULL) 12041 { 12042 li = li->li_next; 12043 ++idx; 12044 } 12045 else 12046 { 12047 #ifdef FEAT_MBYTE 12048 startcol = regmatch.startp[0] 12049 + (*mb_ptr2len)(regmatch.startp[0]) - str; 12050 #else 12051 startcol = regmatch.startp[0] + 1 - str; 12052 #endif 12053 } 12054 } 12055 12056 if (match) 12057 { 12058 if (type == 3) 12059 { 12060 int i; 12061 12062 /* return list with matched string and submatches */ 12063 for (i = 0; i < NSUBEXP; ++i) 12064 { 12065 if (regmatch.endp[i] == NULL) 12066 break; 12067 if (list_append_string(rettv->vval.v_list, 12068 regmatch.startp[i], 12069 (int)(regmatch.endp[i] - regmatch.startp[i])) 12070 == FAIL) 12071 break; 12072 } 12073 } 12074 else if (type == 2) 12075 { 12076 /* return matched string */ 12077 if (l != NULL) 12078 copy_tv(&li->li_tv, rettv); 12079 else 12080 rettv->vval.v_string = vim_strnsave(regmatch.startp[0], 12081 (int)(regmatch.endp[0] - regmatch.startp[0])); 12082 } 12083 else if (l != NULL) 12084 rettv->vval.v_number = idx; 12085 else 12086 { 12087 if (type != 0) 12088 rettv->vval.v_number = 12089 (varnumber_T)(regmatch.startp[0] - str); 12090 else 12091 rettv->vval.v_number = 12092 (varnumber_T)(regmatch.endp[0] - str); 12093 rettv->vval.v_number += str - expr; 12094 } 12095 } 12096 vim_free(regmatch.regprog); 12097 } 12098 12099 theend: 12100 vim_free(tofree); 12101 p_cpo = save_cpo; 12102 } 12103 12104 /* 12105 * "match()" function 12106 */ 12107 static void 12108 f_match(argvars, rettv) 12109 typval_T *argvars; 12110 typval_T *rettv; 12111 { 12112 find_some_match(argvars, rettv, 1); 12113 } 12114 12115 /* 12116 * "matchend()" function 12117 */ 12118 static void 12119 f_matchend(argvars, rettv) 12120 typval_T *argvars; 12121 typval_T *rettv; 12122 { 12123 find_some_match(argvars, rettv, 0); 12124 } 12125 12126 /* 12127 * "matchlist()" function 12128 */ 12129 static void 12130 f_matchlist(argvars, rettv) 12131 typval_T *argvars; 12132 typval_T *rettv; 12133 { 12134 find_some_match(argvars, rettv, 3); 12135 } 12136 12137 /* 12138 * "matchstr()" function 12139 */ 12140 static void 12141 f_matchstr(argvars, rettv) 12142 typval_T *argvars; 12143 typval_T *rettv; 12144 { 12145 find_some_match(argvars, rettv, 2); 12146 } 12147 12148 static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax)); 12149 12150 static void 12151 max_min(argvars, rettv, domax) 12152 typval_T *argvars; 12153 typval_T *rettv; 12154 int domax; 12155 { 12156 long n = 0; 12157 long i; 12158 int error = FALSE; 12159 12160 if (argvars[0].v_type == VAR_LIST) 12161 { 12162 list_T *l; 12163 listitem_T *li; 12164 12165 l = argvars[0].vval.v_list; 12166 if (l != NULL) 12167 { 12168 li = l->lv_first; 12169 if (li != NULL) 12170 { 12171 n = get_tv_number_chk(&li->li_tv, &error); 12172 for (;;) 12173 { 12174 li = li->li_next; 12175 if (li == NULL) 12176 break; 12177 i = get_tv_number_chk(&li->li_tv, &error); 12178 if (domax ? i > n : i < n) 12179 n = i; 12180 } 12181 } 12182 } 12183 } 12184 else if (argvars[0].v_type == VAR_DICT) 12185 { 12186 dict_T *d; 12187 int first = TRUE; 12188 hashitem_T *hi; 12189 int todo; 12190 12191 d = argvars[0].vval.v_dict; 12192 if (d != NULL) 12193 { 12194 todo = d->dv_hashtab.ht_used; 12195 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) 12196 { 12197 if (!HASHITEM_EMPTY(hi)) 12198 { 12199 --todo; 12200 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error); 12201 if (first) 12202 { 12203 n = i; 12204 first = FALSE; 12205 } 12206 else if (domax ? i > n : i < n) 12207 n = i; 12208 } 12209 } 12210 } 12211 } 12212 else 12213 EMSG(_(e_listdictarg)); 12214 rettv->vval.v_number = error ? 0 : n; 12215 } 12216 12217 /* 12218 * "max()" function 12219 */ 12220 static void 12221 f_max(argvars, rettv) 12222 typval_T *argvars; 12223 typval_T *rettv; 12224 { 12225 max_min(argvars, rettv, TRUE); 12226 } 12227 12228 /* 12229 * "min()" function 12230 */ 12231 static void 12232 f_min(argvars, rettv) 12233 typval_T *argvars; 12234 typval_T *rettv; 12235 { 12236 max_min(argvars, rettv, FALSE); 12237 } 12238 12239 static int mkdir_recurse __ARGS((char_u *dir, int prot)); 12240 12241 /* 12242 * Create the directory in which "dir" is located, and higher levels when 12243 * needed. 12244 */ 12245 static int 12246 mkdir_recurse(dir, prot) 12247 char_u *dir; 12248 int prot; 12249 { 12250 char_u *p; 12251 char_u *updir; 12252 int r = FAIL; 12253 12254 /* Get end of directory name in "dir". 12255 * We're done when it's "/" or "c:/". */ 12256 p = gettail_sep(dir); 12257 if (p <= get_past_head(dir)) 12258 return OK; 12259 12260 /* If the directory exists we're done. Otherwise: create it.*/ 12261 updir = vim_strnsave(dir, (int)(p - dir)); 12262 if (updir == NULL) 12263 return FAIL; 12264 if (mch_isdir(updir)) 12265 r = OK; 12266 else if (mkdir_recurse(updir, prot) == OK) 12267 r = vim_mkdir_emsg(updir, prot); 12268 vim_free(updir); 12269 return r; 12270 } 12271 12272 #ifdef vim_mkdir 12273 /* 12274 * "mkdir()" function 12275 */ 12276 static void 12277 f_mkdir(argvars, rettv) 12278 typval_T *argvars; 12279 typval_T *rettv; 12280 { 12281 char_u *dir; 12282 char_u buf[NUMBUFLEN]; 12283 int prot = 0755; 12284 12285 rettv->vval.v_number = FAIL; 12286 if (check_restricted() || check_secure()) 12287 return; 12288 12289 dir = get_tv_string_buf(&argvars[0], buf); 12290 if (argvars[1].v_type != VAR_UNKNOWN) 12291 { 12292 if (argvars[2].v_type != VAR_UNKNOWN) 12293 prot = get_tv_number_chk(&argvars[2], NULL); 12294 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0) 12295 mkdir_recurse(dir, prot); 12296 } 12297 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0; 12298 } 12299 #endif 12300 12301 /* 12302 * "mode()" function 12303 */ 12304 /*ARGSUSED*/ 12305 static void 12306 f_mode(argvars, rettv) 12307 typval_T *argvars; 12308 typval_T *rettv; 12309 { 12310 char_u buf[2]; 12311 12312 #ifdef FEAT_VISUAL 12313 if (VIsual_active) 12314 { 12315 if (VIsual_select) 12316 buf[0] = VIsual_mode + 's' - 'v'; 12317 else 12318 buf[0] = VIsual_mode; 12319 } 12320 else 12321 #endif 12322 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE) 12323 buf[0] = 'r'; 12324 else if (State & INSERT) 12325 { 12326 if (State & REPLACE_FLAG) 12327 buf[0] = 'R'; 12328 else 12329 buf[0] = 'i'; 12330 } 12331 else if (State & CMDLINE) 12332 buf[0] = 'c'; 12333 else 12334 buf[0] = 'n'; 12335 12336 buf[1] = NUL; 12337 rettv->vval.v_string = vim_strsave(buf); 12338 rettv->v_type = VAR_STRING; 12339 } 12340 12341 /* 12342 * "nextnonblank()" function 12343 */ 12344 static void 12345 f_nextnonblank(argvars, rettv) 12346 typval_T *argvars; 12347 typval_T *rettv; 12348 { 12349 linenr_T lnum; 12350 12351 for (lnum = get_tv_lnum(argvars); ; ++lnum) 12352 { 12353 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count) 12354 { 12355 lnum = 0; 12356 break; 12357 } 12358 if (*skipwhite(ml_get(lnum)) != NUL) 12359 break; 12360 } 12361 rettv->vval.v_number = lnum; 12362 } 12363 12364 /* 12365 * "nr2char()" function 12366 */ 12367 static void 12368 f_nr2char(argvars, rettv) 12369 typval_T *argvars; 12370 typval_T *rettv; 12371 { 12372 char_u buf[NUMBUFLEN]; 12373 12374 #ifdef FEAT_MBYTE 12375 if (has_mbyte) 12376 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL; 12377 else 12378 #endif 12379 { 12380 buf[0] = (char_u)get_tv_number(&argvars[0]); 12381 buf[1] = NUL; 12382 } 12383 rettv->v_type = VAR_STRING; 12384 rettv->vval.v_string = vim_strsave(buf); 12385 } 12386 12387 /* 12388 * "prevnonblank()" function 12389 */ 12390 static void 12391 f_prevnonblank(argvars, rettv) 12392 typval_T *argvars; 12393 typval_T *rettv; 12394 { 12395 linenr_T lnum; 12396 12397 lnum = get_tv_lnum(argvars); 12398 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) 12399 lnum = 0; 12400 else 12401 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL) 12402 --lnum; 12403 rettv->vval.v_number = lnum; 12404 } 12405 12406 #ifdef HAVE_STDARG_H 12407 /* This dummy va_list is here because: 12408 * - passing a NULL pointer doesn't work when va_list isn't a pointer 12409 * - locally in the function results in a "used before set" warning 12410 * - using va_start() to initialize it gives "function with fixed args" error */ 12411 static va_list ap; 12412 #endif 12413 12414 /* 12415 * "printf()" function 12416 */ 12417 static void 12418 f_printf(argvars, rettv) 12419 typval_T *argvars; 12420 typval_T *rettv; 12421 { 12422 rettv->v_type = VAR_STRING; 12423 rettv->vval.v_string = NULL; 12424 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */ 12425 { 12426 char_u buf[NUMBUFLEN]; 12427 int len; 12428 char_u *s; 12429 int saved_did_emsg = did_emsg; 12430 char *fmt; 12431 12432 /* Get the required length, allocate the buffer and do it for real. */ 12433 did_emsg = FALSE; 12434 fmt = (char *)get_tv_string_buf(&argvars[0], buf); 12435 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1); 12436 if (!did_emsg) 12437 { 12438 s = alloc(len + 1); 12439 if (s != NULL) 12440 { 12441 rettv->vval.v_string = s; 12442 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1); 12443 } 12444 } 12445 did_emsg |= saved_did_emsg; 12446 } 12447 #endif 12448 } 12449 12450 /* 12451 * "pumvisible()" function 12452 */ 12453 /*ARGSUSED*/ 12454 static void 12455 f_pumvisible(argvars, rettv) 12456 typval_T *argvars; 12457 typval_T *rettv; 12458 { 12459 rettv->vval.v_number = 0; 12460 #ifdef FEAT_INS_EXPAND 12461 if (pum_visible()) 12462 rettv->vval.v_number = 1; 12463 #endif 12464 } 12465 12466 /* 12467 * "range()" function 12468 */ 12469 static void 12470 f_range(argvars, rettv) 12471 typval_T *argvars; 12472 typval_T *rettv; 12473 { 12474 long start; 12475 long end; 12476 long stride = 1; 12477 long i; 12478 int error = FALSE; 12479 12480 start = get_tv_number_chk(&argvars[0], &error); 12481 if (argvars[1].v_type == VAR_UNKNOWN) 12482 { 12483 end = start - 1; 12484 start = 0; 12485 } 12486 else 12487 { 12488 end = get_tv_number_chk(&argvars[1], &error); 12489 if (argvars[2].v_type != VAR_UNKNOWN) 12490 stride = get_tv_number_chk(&argvars[2], &error); 12491 } 12492 12493 rettv->vval.v_number = 0; 12494 if (error) 12495 return; /* type error; errmsg already given */ 12496 if (stride == 0) 12497 EMSG(_("E726: Stride is zero")); 12498 else if (stride > 0 ? end + 1 < start : end - 1 > start) 12499 EMSG(_("E727: Start past end")); 12500 else 12501 { 12502 if (rettv_list_alloc(rettv) == OK) 12503 for (i = start; stride > 0 ? i <= end : i >= end; i += stride) 12504 if (list_append_number(rettv->vval.v_list, 12505 (varnumber_T)i) == FAIL) 12506 break; 12507 } 12508 } 12509 12510 /* 12511 * "readfile()" function 12512 */ 12513 static void 12514 f_readfile(argvars, rettv) 12515 typval_T *argvars; 12516 typval_T *rettv; 12517 { 12518 int binary = FALSE; 12519 char_u *fname; 12520 FILE *fd; 12521 listitem_T *li; 12522 #define FREAD_SIZE 200 /* optimized for text lines */ 12523 char_u buf[FREAD_SIZE]; 12524 int readlen; /* size of last fread() */ 12525 int buflen; /* nr of valid chars in buf[] */ 12526 int filtd; /* how much in buf[] was NUL -> '\n' filtered */ 12527 int tolist; /* first byte in buf[] still to be put in list */ 12528 int chop; /* how many CR to chop off */ 12529 char_u *prev = NULL; /* previously read bytes, if any */ 12530 int prevlen = 0; /* length of "prev" if not NULL */ 12531 char_u *s; 12532 int len; 12533 long maxline = MAXLNUM; 12534 long cnt = 0; 12535 12536 if (argvars[1].v_type != VAR_UNKNOWN) 12537 { 12538 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0) 12539 binary = TRUE; 12540 if (argvars[2].v_type != VAR_UNKNOWN) 12541 maxline = get_tv_number(&argvars[2]); 12542 } 12543 12544 if (rettv_list_alloc(rettv) == FAIL) 12545 return; 12546 12547 /* Always open the file in binary mode, library functions have a mind of 12548 * their own about CR-LF conversion. */ 12549 fname = get_tv_string(&argvars[0]); 12550 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL) 12551 { 12552 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname); 12553 return; 12554 } 12555 12556 filtd = 0; 12557 while (cnt < maxline || maxline < 0) 12558 { 12559 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd); 12560 buflen = filtd + readlen; 12561 tolist = 0; 12562 for ( ; filtd < buflen || readlen <= 0; ++filtd) 12563 { 12564 if (buf[filtd] == '\n' || readlen <= 0) 12565 { 12566 /* Only when in binary mode add an empty list item when the 12567 * last line ends in a '\n'. */ 12568 if (!binary && readlen == 0 && filtd == 0) 12569 break; 12570 12571 /* Found end-of-line or end-of-file: add a text line to the 12572 * list. */ 12573 chop = 0; 12574 if (!binary) 12575 while (filtd - chop - 1 >= tolist 12576 && buf[filtd - chop - 1] == '\r') 12577 ++chop; 12578 len = filtd - tolist - chop; 12579 if (prev == NULL) 12580 s = vim_strnsave(buf + tolist, len); 12581 else 12582 { 12583 s = alloc((unsigned)(prevlen + len + 1)); 12584 if (s != NULL) 12585 { 12586 mch_memmove(s, prev, prevlen); 12587 vim_free(prev); 12588 prev = NULL; 12589 mch_memmove(s + prevlen, buf + tolist, len); 12590 s[prevlen + len] = NUL; 12591 } 12592 } 12593 tolist = filtd + 1; 12594 12595 li = listitem_alloc(); 12596 if (li == NULL) 12597 { 12598 vim_free(s); 12599 break; 12600 } 12601 li->li_tv.v_type = VAR_STRING; 12602 li->li_tv.v_lock = 0; 12603 li->li_tv.vval.v_string = s; 12604 list_append(rettv->vval.v_list, li); 12605 12606 if (++cnt >= maxline && maxline >= 0) 12607 break; 12608 if (readlen <= 0) 12609 break; 12610 } 12611 else if (buf[filtd] == NUL) 12612 buf[filtd] = '\n'; 12613 } 12614 if (readlen <= 0) 12615 break; 12616 12617 if (tolist == 0) 12618 { 12619 /* "buf" is full, need to move text to an allocated buffer */ 12620 if (prev == NULL) 12621 { 12622 prev = vim_strnsave(buf, buflen); 12623 prevlen = buflen; 12624 } 12625 else 12626 { 12627 s = alloc((unsigned)(prevlen + buflen)); 12628 if (s != NULL) 12629 { 12630 mch_memmove(s, prev, prevlen); 12631 mch_memmove(s + prevlen, buf, buflen); 12632 vim_free(prev); 12633 prev = s; 12634 prevlen += buflen; 12635 } 12636 } 12637 filtd = 0; 12638 } 12639 else 12640 { 12641 mch_memmove(buf, buf + tolist, buflen - tolist); 12642 filtd -= tolist; 12643 } 12644 } 12645 12646 /* 12647 * For a negative line count use only the lines at the end of the file, 12648 * free the rest. 12649 */ 12650 if (maxline < 0) 12651 while (cnt > -maxline) 12652 { 12653 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first); 12654 --cnt; 12655 } 12656 12657 vim_free(prev); 12658 fclose(fd); 12659 } 12660 12661 #if defined(FEAT_RELTIME) 12662 static int list2proftime __ARGS((typval_T *arg, proftime_T *tm)); 12663 12664 /* 12665 * Convert a List to proftime_T. 12666 * Return FAIL when there is something wrong. 12667 */ 12668 static int 12669 list2proftime(arg, tm) 12670 typval_T *arg; 12671 proftime_T *tm; 12672 { 12673 long n1, n2; 12674 int error = FALSE; 12675 12676 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL 12677 || arg->vval.v_list->lv_len != 2) 12678 return FAIL; 12679 n1 = list_find_nr(arg->vval.v_list, 0L, &error); 12680 n2 = list_find_nr(arg->vval.v_list, 1L, &error); 12681 # ifdef WIN3264 12682 tm->HighPart = n1; 12683 tm->LowPart = n2; 12684 # else 12685 tm->tv_sec = n1; 12686 tm->tv_usec = n2; 12687 # endif 12688 return error ? FAIL : OK; 12689 } 12690 #endif /* FEAT_RELTIME */ 12691 12692 /* 12693 * "reltime()" function 12694 */ 12695 static void 12696 f_reltime(argvars, rettv) 12697 typval_T *argvars; 12698 typval_T *rettv; 12699 { 12700 #ifdef FEAT_RELTIME 12701 proftime_T res; 12702 proftime_T start; 12703 12704 if (argvars[0].v_type == VAR_UNKNOWN) 12705 { 12706 /* No arguments: get current time. */ 12707 profile_start(&res); 12708 } 12709 else if (argvars[1].v_type == VAR_UNKNOWN) 12710 { 12711 if (list2proftime(&argvars[0], &res) == FAIL) 12712 return; 12713 profile_end(&res); 12714 } 12715 else 12716 { 12717 /* Two arguments: compute the difference. */ 12718 if (list2proftime(&argvars[0], &start) == FAIL 12719 || list2proftime(&argvars[1], &res) == FAIL) 12720 return; 12721 profile_sub(&res, &start); 12722 } 12723 12724 if (rettv_list_alloc(rettv) == OK) 12725 { 12726 long n1, n2; 12727 12728 # ifdef WIN3264 12729 n1 = res.HighPart; 12730 n2 = res.LowPart; 12731 # else 12732 n1 = res.tv_sec; 12733 n2 = res.tv_usec; 12734 # endif 12735 list_append_number(rettv->vval.v_list, (varnumber_T)n1); 12736 list_append_number(rettv->vval.v_list, (varnumber_T)n2); 12737 } 12738 #endif 12739 } 12740 12741 /* 12742 * "reltimestr()" function 12743 */ 12744 static void 12745 f_reltimestr(argvars, rettv) 12746 typval_T *argvars; 12747 typval_T *rettv; 12748 { 12749 #ifdef FEAT_RELTIME 12750 proftime_T tm; 12751 #endif 12752 12753 rettv->v_type = VAR_STRING; 12754 rettv->vval.v_string = NULL; 12755 #ifdef FEAT_RELTIME 12756 if (list2proftime(&argvars[0], &tm) == OK) 12757 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm)); 12758 #endif 12759 } 12760 12761 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) 12762 static void make_connection __ARGS((void)); 12763 static int check_connection __ARGS((void)); 12764 12765 static void 12766 make_connection() 12767 { 12768 if (X_DISPLAY == NULL 12769 # ifdef FEAT_GUI 12770 && !gui.in_use 12771 # endif 12772 ) 12773 { 12774 x_force_connect = TRUE; 12775 setup_term_clip(); 12776 x_force_connect = FALSE; 12777 } 12778 } 12779 12780 static int 12781 check_connection() 12782 { 12783 make_connection(); 12784 if (X_DISPLAY == NULL) 12785 { 12786 EMSG(_("E240: No connection to Vim server")); 12787 return FAIL; 12788 } 12789 return OK; 12790 } 12791 #endif 12792 12793 #ifdef FEAT_CLIENTSERVER 12794 static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr)); 12795 12796 static void 12797 remote_common(argvars, rettv, expr) 12798 typval_T *argvars; 12799 typval_T *rettv; 12800 int expr; 12801 { 12802 char_u *server_name; 12803 char_u *keys; 12804 char_u *r = NULL; 12805 char_u buf[NUMBUFLEN]; 12806 # ifdef WIN32 12807 HWND w; 12808 # else 12809 Window w; 12810 # endif 12811 12812 if (check_restricted() || check_secure()) 12813 return; 12814 12815 # ifdef FEAT_X11 12816 if (check_connection() == FAIL) 12817 return; 12818 # endif 12819 12820 server_name = get_tv_string_chk(&argvars[0]); 12821 if (server_name == NULL) 12822 return; /* type error; errmsg already given */ 12823 keys = get_tv_string_buf(&argvars[1], buf); 12824 # ifdef WIN32 12825 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0) 12826 # else 12827 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE) 12828 < 0) 12829 # endif 12830 { 12831 if (r != NULL) 12832 EMSG(r); /* sending worked but evaluation failed */ 12833 else 12834 EMSG2(_("E241: Unable to send to %s"), server_name); 12835 return; 12836 } 12837 12838 rettv->vval.v_string = r; 12839 12840 if (argvars[2].v_type != VAR_UNKNOWN) 12841 { 12842 dictitem_T v; 12843 char_u str[30]; 12844 char_u *idvar; 12845 12846 sprintf((char *)str, "0x%x", (unsigned int)w); 12847 v.di_tv.v_type = VAR_STRING; 12848 v.di_tv.vval.v_string = vim_strsave(str); 12849 idvar = get_tv_string_chk(&argvars[2]); 12850 if (idvar != NULL) 12851 set_var(idvar, &v.di_tv, FALSE); 12852 vim_free(v.di_tv.vval.v_string); 12853 } 12854 } 12855 #endif 12856 12857 /* 12858 * "remote_expr()" function 12859 */ 12860 /*ARGSUSED*/ 12861 static void 12862 f_remote_expr(argvars, rettv) 12863 typval_T *argvars; 12864 typval_T *rettv; 12865 { 12866 rettv->v_type = VAR_STRING; 12867 rettv->vval.v_string = NULL; 12868 #ifdef FEAT_CLIENTSERVER 12869 remote_common(argvars, rettv, TRUE); 12870 #endif 12871 } 12872 12873 /* 12874 * "remote_foreground()" function 12875 */ 12876 /*ARGSUSED*/ 12877 static void 12878 f_remote_foreground(argvars, rettv) 12879 typval_T *argvars; 12880 typval_T *rettv; 12881 { 12882 rettv->vval.v_number = 0; 12883 #ifdef FEAT_CLIENTSERVER 12884 # ifdef WIN32 12885 /* On Win32 it's done in this application. */ 12886 { 12887 char_u *server_name = get_tv_string_chk(&argvars[0]); 12888 12889 if (server_name != NULL) 12890 serverForeground(server_name); 12891 } 12892 # else 12893 /* Send a foreground() expression to the server. */ 12894 argvars[1].v_type = VAR_STRING; 12895 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()"); 12896 argvars[2].v_type = VAR_UNKNOWN; 12897 remote_common(argvars, rettv, TRUE); 12898 vim_free(argvars[1].vval.v_string); 12899 # endif 12900 #endif 12901 } 12902 12903 /*ARGSUSED*/ 12904 static void 12905 f_remote_peek(argvars, rettv) 12906 typval_T *argvars; 12907 typval_T *rettv; 12908 { 12909 #ifdef FEAT_CLIENTSERVER 12910 dictitem_T v; 12911 char_u *s = NULL; 12912 # ifdef WIN32 12913 int n = 0; 12914 # endif 12915 char_u *serverid; 12916 12917 if (check_restricted() || check_secure()) 12918 { 12919 rettv->vval.v_number = -1; 12920 return; 12921 } 12922 serverid = get_tv_string_chk(&argvars[0]); 12923 if (serverid == NULL) 12924 { 12925 rettv->vval.v_number = -1; 12926 return; /* type error; errmsg already given */ 12927 } 12928 # ifdef WIN32 12929 sscanf(serverid, "%x", &n); 12930 if (n == 0) 12931 rettv->vval.v_number = -1; 12932 else 12933 { 12934 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE); 12935 rettv->vval.v_number = (s != NULL); 12936 } 12937 # else 12938 rettv->vval.v_number = 0; 12939 if (check_connection() == FAIL) 12940 return; 12941 12942 rettv->vval.v_number = serverPeekReply(X_DISPLAY, 12943 serverStrToWin(serverid), &s); 12944 # endif 12945 12946 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0) 12947 { 12948 char_u *retvar; 12949 12950 v.di_tv.v_type = VAR_STRING; 12951 v.di_tv.vval.v_string = vim_strsave(s); 12952 retvar = get_tv_string_chk(&argvars[1]); 12953 if (retvar != NULL) 12954 set_var(retvar, &v.di_tv, FALSE); 12955 vim_free(v.di_tv.vval.v_string); 12956 } 12957 #else 12958 rettv->vval.v_number = -1; 12959 #endif 12960 } 12961 12962 /*ARGSUSED*/ 12963 static void 12964 f_remote_read(argvars, rettv) 12965 typval_T *argvars; 12966 typval_T *rettv; 12967 { 12968 char_u *r = NULL; 12969 12970 #ifdef FEAT_CLIENTSERVER 12971 char_u *serverid = get_tv_string_chk(&argvars[0]); 12972 12973 if (serverid != NULL && !check_restricted() && !check_secure()) 12974 { 12975 # ifdef WIN32 12976 /* The server's HWND is encoded in the 'id' parameter */ 12977 int n = 0; 12978 12979 sscanf(serverid, "%x", &n); 12980 if (n != 0) 12981 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE); 12982 if (r == NULL) 12983 # else 12984 if (check_connection() == FAIL || serverReadReply(X_DISPLAY, 12985 serverStrToWin(serverid), &r, FALSE) < 0) 12986 # endif 12987 EMSG(_("E277: Unable to read a server reply")); 12988 } 12989 #endif 12990 rettv->v_type = VAR_STRING; 12991 rettv->vval.v_string = r; 12992 } 12993 12994 /* 12995 * "remote_send()" function 12996 */ 12997 /*ARGSUSED*/ 12998 static void 12999 f_remote_send(argvars, rettv) 13000 typval_T *argvars; 13001 typval_T *rettv; 13002 { 13003 rettv->v_type = VAR_STRING; 13004 rettv->vval.v_string = NULL; 13005 #ifdef FEAT_CLIENTSERVER 13006 remote_common(argvars, rettv, FALSE); 13007 #endif 13008 } 13009 13010 /* 13011 * "remove()" function 13012 */ 13013 static void 13014 f_remove(argvars, rettv) 13015 typval_T *argvars; 13016 typval_T *rettv; 13017 { 13018 list_T *l; 13019 listitem_T *item, *item2; 13020 listitem_T *li; 13021 long idx; 13022 long end; 13023 char_u *key; 13024 dict_T *d; 13025 dictitem_T *di; 13026 13027 rettv->vval.v_number = 0; 13028 if (argvars[0].v_type == VAR_DICT) 13029 { 13030 if (argvars[2].v_type != VAR_UNKNOWN) 13031 EMSG2(_(e_toomanyarg), "remove()"); 13032 else if ((d = argvars[0].vval.v_dict) != NULL 13033 && !tv_check_lock(d->dv_lock, (char_u *)"remove()")) 13034 { 13035 key = get_tv_string_chk(&argvars[1]); 13036 if (key != NULL) 13037 { 13038 di = dict_find(d, key, -1); 13039 if (di == NULL) 13040 EMSG2(_(e_dictkey), key); 13041 else 13042 { 13043 *rettv = di->di_tv; 13044 init_tv(&di->di_tv); 13045 dictitem_remove(d, di); 13046 } 13047 } 13048 } 13049 } 13050 else if (argvars[0].v_type != VAR_LIST) 13051 EMSG2(_(e_listdictarg), "remove()"); 13052 else if ((l = argvars[0].vval.v_list) != NULL 13053 && !tv_check_lock(l->lv_lock, (char_u *)"remove()")) 13054 { 13055 int error = FALSE; 13056 13057 idx = get_tv_number_chk(&argvars[1], &error); 13058 if (error) 13059 ; /* type error: do nothing, errmsg already given */ 13060 else if ((item = list_find(l, idx)) == NULL) 13061 EMSGN(_(e_listidx), idx); 13062 else 13063 { 13064 if (argvars[2].v_type == VAR_UNKNOWN) 13065 { 13066 /* Remove one item, return its value. */ 13067 list_remove(l, item, item); 13068 *rettv = item->li_tv; 13069 vim_free(item); 13070 } 13071 else 13072 { 13073 /* Remove range of items, return list with values. */ 13074 end = get_tv_number_chk(&argvars[2], &error); 13075 if (error) 13076 ; /* type error: do nothing */ 13077 else if ((item2 = list_find(l, end)) == NULL) 13078 EMSGN(_(e_listidx), end); 13079 else 13080 { 13081 int cnt = 0; 13082 13083 for (li = item; li != NULL; li = li->li_next) 13084 { 13085 ++cnt; 13086 if (li == item2) 13087 break; 13088 } 13089 if (li == NULL) /* didn't find "item2" after "item" */ 13090 EMSG(_(e_invrange)); 13091 else 13092 { 13093 list_remove(l, item, item2); 13094 if (rettv_list_alloc(rettv) == OK) 13095 { 13096 l = rettv->vval.v_list; 13097 l->lv_first = item; 13098 l->lv_last = item2; 13099 item->li_prev = NULL; 13100 item2->li_next = NULL; 13101 l->lv_len = cnt; 13102 } 13103 } 13104 } 13105 } 13106 } 13107 } 13108 } 13109 13110 /* 13111 * "rename({from}, {to})" function 13112 */ 13113 static void 13114 f_rename(argvars, rettv) 13115 typval_T *argvars; 13116 typval_T *rettv; 13117 { 13118 char_u buf[NUMBUFLEN]; 13119 13120 if (check_restricted() || check_secure()) 13121 rettv->vval.v_number = -1; 13122 else 13123 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]), 13124 get_tv_string_buf(&argvars[1], buf)); 13125 } 13126 13127 /* 13128 * "repeat()" function 13129 */ 13130 /*ARGSUSED*/ 13131 static void 13132 f_repeat(argvars, rettv) 13133 typval_T *argvars; 13134 typval_T *rettv; 13135 { 13136 char_u *p; 13137 int n; 13138 int slen; 13139 int len; 13140 char_u *r; 13141 int i; 13142 13143 n = get_tv_number(&argvars[1]); 13144 if (argvars[0].v_type == VAR_LIST) 13145 { 13146 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL) 13147 while (n-- > 0) 13148 if (list_extend(rettv->vval.v_list, 13149 argvars[0].vval.v_list, NULL) == FAIL) 13150 break; 13151 } 13152 else 13153 { 13154 p = get_tv_string(&argvars[0]); 13155 rettv->v_type = VAR_STRING; 13156 rettv->vval.v_string = NULL; 13157 13158 slen = (int)STRLEN(p); 13159 len = slen * n; 13160 if (len <= 0) 13161 return; 13162 13163 r = alloc(len + 1); 13164 if (r != NULL) 13165 { 13166 for (i = 0; i < n; i++) 13167 mch_memmove(r + i * slen, p, (size_t)slen); 13168 r[len] = NUL; 13169 } 13170 13171 rettv->vval.v_string = r; 13172 } 13173 } 13174 13175 /* 13176 * "resolve()" function 13177 */ 13178 static void 13179 f_resolve(argvars, rettv) 13180 typval_T *argvars; 13181 typval_T *rettv; 13182 { 13183 char_u *p; 13184 13185 p = get_tv_string(&argvars[0]); 13186 #ifdef FEAT_SHORTCUT 13187 { 13188 char_u *v = NULL; 13189 13190 v = mch_resolve_shortcut(p); 13191 if (v != NULL) 13192 rettv->vval.v_string = v; 13193 else 13194 rettv->vval.v_string = vim_strsave(p); 13195 } 13196 #else 13197 # ifdef HAVE_READLINK 13198 { 13199 char_u buf[MAXPATHL + 1]; 13200 char_u *cpy; 13201 int len; 13202 char_u *remain = NULL; 13203 char_u *q; 13204 int is_relative_to_current = FALSE; 13205 int has_trailing_pathsep = FALSE; 13206 int limit = 100; 13207 13208 p = vim_strsave(p); 13209 13210 if (p[0] == '.' && (vim_ispathsep(p[1]) 13211 || (p[1] == '.' && (vim_ispathsep(p[2]))))) 13212 is_relative_to_current = TRUE; 13213 13214 len = STRLEN(p); 13215 if (len > 0 && after_pathsep(p, p + len)) 13216 has_trailing_pathsep = TRUE; 13217 13218 q = getnextcomp(p); 13219 if (*q != NUL) 13220 { 13221 /* Separate the first path component in "p", and keep the 13222 * remainder (beginning with the path separator). */ 13223 remain = vim_strsave(q - 1); 13224 q[-1] = NUL; 13225 } 13226 13227 for (;;) 13228 { 13229 for (;;) 13230 { 13231 len = readlink((char *)p, (char *)buf, MAXPATHL); 13232 if (len <= 0) 13233 break; 13234 buf[len] = NUL; 13235 13236 if (limit-- == 0) 13237 { 13238 vim_free(p); 13239 vim_free(remain); 13240 EMSG(_("E655: Too many symbolic links (cycle?)")); 13241 rettv->vval.v_string = NULL; 13242 goto fail; 13243 } 13244 13245 /* Ensure that the result will have a trailing path separator 13246 * if the argument has one. */ 13247 if (remain == NULL && has_trailing_pathsep) 13248 add_pathsep(buf); 13249 13250 /* Separate the first path component in the link value and 13251 * concatenate the remainders. */ 13252 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf); 13253 if (*q != NUL) 13254 { 13255 if (remain == NULL) 13256 remain = vim_strsave(q - 1); 13257 else 13258 { 13259 cpy = concat_str(q - 1, remain); 13260 if (cpy != NULL) 13261 { 13262 vim_free(remain); 13263 remain = cpy; 13264 } 13265 } 13266 q[-1] = NUL; 13267 } 13268 13269 q = gettail(p); 13270 if (q > p && *q == NUL) 13271 { 13272 /* Ignore trailing path separator. */ 13273 q[-1] = NUL; 13274 q = gettail(p); 13275 } 13276 if (q > p && !mch_isFullName(buf)) 13277 { 13278 /* symlink is relative to directory of argument */ 13279 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1)); 13280 if (cpy != NULL) 13281 { 13282 STRCPY(cpy, p); 13283 STRCPY(gettail(cpy), buf); 13284 vim_free(p); 13285 p = cpy; 13286 } 13287 } 13288 else 13289 { 13290 vim_free(p); 13291 p = vim_strsave(buf); 13292 } 13293 } 13294 13295 if (remain == NULL) 13296 break; 13297 13298 /* Append the first path component of "remain" to "p". */ 13299 q = getnextcomp(remain + 1); 13300 len = q - remain - (*q != NUL); 13301 cpy = vim_strnsave(p, STRLEN(p) + len); 13302 if (cpy != NULL) 13303 { 13304 STRNCAT(cpy, remain, len); 13305 vim_free(p); 13306 p = cpy; 13307 } 13308 /* Shorten "remain". */ 13309 if (*q != NUL) 13310 STRCPY(remain, q - 1); 13311 else 13312 { 13313 vim_free(remain); 13314 remain = NULL; 13315 } 13316 } 13317 13318 /* If the result is a relative path name, make it explicitly relative to 13319 * the current directory if and only if the argument had this form. */ 13320 if (!vim_ispathsep(*p)) 13321 { 13322 if (is_relative_to_current 13323 && *p != NUL 13324 && !(p[0] == '.' 13325 && (p[1] == NUL 13326 || vim_ispathsep(p[1]) 13327 || (p[1] == '.' 13328 && (p[2] == NUL 13329 || vim_ispathsep(p[2])))))) 13330 { 13331 /* Prepend "./". */ 13332 cpy = concat_str((char_u *)"./", p); 13333 if (cpy != NULL) 13334 { 13335 vim_free(p); 13336 p = cpy; 13337 } 13338 } 13339 else if (!is_relative_to_current) 13340 { 13341 /* Strip leading "./". */ 13342 q = p; 13343 while (q[0] == '.' && vim_ispathsep(q[1])) 13344 q += 2; 13345 if (q > p) 13346 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1); 13347 } 13348 } 13349 13350 /* Ensure that the result will have no trailing path separator 13351 * if the argument had none. But keep "/" or "//". */ 13352 if (!has_trailing_pathsep) 13353 { 13354 q = p + STRLEN(p); 13355 if (after_pathsep(p, q)) 13356 *gettail_sep(p) = NUL; 13357 } 13358 13359 rettv->vval.v_string = p; 13360 } 13361 # else 13362 rettv->vval.v_string = vim_strsave(p); 13363 # endif 13364 #endif 13365 13366 simplify_filename(rettv->vval.v_string); 13367 13368 #ifdef HAVE_READLINK 13369 fail: 13370 #endif 13371 rettv->v_type = VAR_STRING; 13372 } 13373 13374 /* 13375 * "reverse({list})" function 13376 */ 13377 static void 13378 f_reverse(argvars, rettv) 13379 typval_T *argvars; 13380 typval_T *rettv; 13381 { 13382 list_T *l; 13383 listitem_T *li, *ni; 13384 13385 rettv->vval.v_number = 0; 13386 if (argvars[0].v_type != VAR_LIST) 13387 EMSG2(_(e_listarg), "reverse()"); 13388 else if ((l = argvars[0].vval.v_list) != NULL 13389 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()")) 13390 { 13391 li = l->lv_last; 13392 l->lv_first = l->lv_last = NULL; 13393 l->lv_len = 0; 13394 while (li != NULL) 13395 { 13396 ni = li->li_prev; 13397 list_append(l, li); 13398 li = ni; 13399 } 13400 rettv->vval.v_list = l; 13401 rettv->v_type = VAR_LIST; 13402 ++l->lv_refcount; 13403 } 13404 } 13405 13406 #define SP_NOMOVE 0x01 /* don't move cursor */ 13407 #define SP_REPEAT 0x02 /* repeat to find outer pair */ 13408 #define SP_RETCOUNT 0x04 /* return matchcount */ 13409 #define SP_SETPCMARK 0x08 /* set previous context mark */ 13410 #define SP_START 0x10 /* accept match at start position */ 13411 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */ 13412 #define SP_END 0x40 /* leave cursor at end of match */ 13413 13414 static int get_search_arg __ARGS((typval_T *varp, int *flagsp)); 13415 13416 /* 13417 * Get flags for a search function. 13418 * Possibly sets "p_ws". 13419 * Returns BACKWARD, FORWARD or zero (for an error). 13420 */ 13421 static int 13422 get_search_arg(varp, flagsp) 13423 typval_T *varp; 13424 int *flagsp; 13425 { 13426 int dir = FORWARD; 13427 char_u *flags; 13428 char_u nbuf[NUMBUFLEN]; 13429 int mask; 13430 13431 if (varp->v_type != VAR_UNKNOWN) 13432 { 13433 flags = get_tv_string_buf_chk(varp, nbuf); 13434 if (flags == NULL) 13435 return 0; /* type error; errmsg already given */ 13436 while (*flags != NUL) 13437 { 13438 switch (*flags) 13439 { 13440 case 'b': dir = BACKWARD; break; 13441 case 'w': p_ws = TRUE; break; 13442 case 'W': p_ws = FALSE; break; 13443 default: mask = 0; 13444 if (flagsp != NULL) 13445 switch (*flags) 13446 { 13447 case 'c': mask = SP_START; break; 13448 case 'e': mask = SP_END; break; 13449 case 'm': mask = SP_RETCOUNT; break; 13450 case 'n': mask = SP_NOMOVE; break; 13451 case 'p': mask = SP_SUBPAT; break; 13452 case 'r': mask = SP_REPEAT; break; 13453 case 's': mask = SP_SETPCMARK; break; 13454 } 13455 if (mask == 0) 13456 { 13457 EMSG2(_(e_invarg2), flags); 13458 dir = 0; 13459 } 13460 else 13461 *flagsp |= mask; 13462 } 13463 if (dir == 0) 13464 break; 13465 ++flags; 13466 } 13467 } 13468 return dir; 13469 } 13470 13471 /* 13472 * Shared by search() and searchpos() functions 13473 */ 13474 static int 13475 search_cmn(argvars, match_pos, flagsp) 13476 typval_T *argvars; 13477 pos_T *match_pos; 13478 int *flagsp; 13479 { 13480 int flags; 13481 char_u *pat; 13482 pos_T pos; 13483 pos_T save_cursor; 13484 int save_p_ws = p_ws; 13485 int dir; 13486 int retval = 0; /* default: FAIL */ 13487 long lnum_stop = 0; 13488 int options = SEARCH_KEEP; 13489 int subpatnum; 13490 13491 pat = get_tv_string(&argvars[0]); 13492 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */ 13493 if (dir == 0) 13494 goto theend; 13495 flags = *flagsp; 13496 if (flags & SP_START) 13497 options |= SEARCH_START; 13498 if (flags & SP_END) 13499 options |= SEARCH_END; 13500 13501 /* Optional extra argument: line number to stop searching. */ 13502 if (argvars[1].v_type != VAR_UNKNOWN 13503 && argvars[2].v_type != VAR_UNKNOWN) 13504 { 13505 lnum_stop = get_tv_number_chk(&argvars[2], NULL); 13506 if (lnum_stop < 0) 13507 goto theend; 13508 } 13509 13510 /* 13511 * This function does not accept SP_REPEAT and SP_RETCOUNT flags. 13512 * Check to make sure only those flags are set. 13513 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both 13514 * flags cannot be set. Check for that condition also. 13515 */ 13516 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0) 13517 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))) 13518 { 13519 EMSG2(_(e_invarg2), get_tv_string(&argvars[1])); 13520 goto theend; 13521 } 13522 13523 pos = save_cursor = curwin->w_cursor; 13524 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L, 13525 options, RE_SEARCH, (linenr_T)lnum_stop); 13526 if (subpatnum != FAIL) 13527 { 13528 if (flags & SP_SUBPAT) 13529 retval = subpatnum; 13530 else 13531 retval = pos.lnum; 13532 if (flags & SP_SETPCMARK) 13533 setpcmark(); 13534 curwin->w_cursor = pos; 13535 if (match_pos != NULL) 13536 { 13537 /* Store the match cursor position */ 13538 match_pos->lnum = pos.lnum; 13539 match_pos->col = pos.col + 1; 13540 } 13541 /* "/$" will put the cursor after the end of the line, may need to 13542 * correct that here */ 13543 check_cursor(); 13544 } 13545 13546 /* If 'n' flag is used: restore cursor position. */ 13547 if (flags & SP_NOMOVE) 13548 curwin->w_cursor = save_cursor; 13549 theend: 13550 p_ws = save_p_ws; 13551 13552 return retval; 13553 } 13554 13555 /* 13556 * "search()" function 13557 */ 13558 static void 13559 f_search(argvars, rettv) 13560 typval_T *argvars; 13561 typval_T *rettv; 13562 { 13563 int flags = 0; 13564 13565 rettv->vval.v_number = search_cmn(argvars, NULL, &flags); 13566 } 13567 13568 /* 13569 * "searchdecl()" function 13570 */ 13571 static void 13572 f_searchdecl(argvars, rettv) 13573 typval_T *argvars; 13574 typval_T *rettv; 13575 { 13576 int locally = 1; 13577 int thisblock = 0; 13578 int error = FALSE; 13579 char_u *name; 13580 13581 rettv->vval.v_number = 1; /* default: FAIL */ 13582 13583 name = get_tv_string_chk(&argvars[0]); 13584 if (argvars[1].v_type != VAR_UNKNOWN) 13585 { 13586 locally = get_tv_number_chk(&argvars[1], &error) == 0; 13587 if (!error && argvars[2].v_type != VAR_UNKNOWN) 13588 thisblock = get_tv_number_chk(&argvars[2], &error) != 0; 13589 } 13590 if (!error && name != NULL) 13591 rettv->vval.v_number = find_decl(name, (int)STRLEN(name), 13592 locally, thisblock, SEARCH_KEEP) == FAIL; 13593 } 13594 13595 /* 13596 * Used by searchpair() and searchpairpos() 13597 */ 13598 static int 13599 searchpair_cmn(argvars, match_pos) 13600 typval_T *argvars; 13601 pos_T *match_pos; 13602 { 13603 char_u *spat, *mpat, *epat; 13604 char_u *skip; 13605 int save_p_ws = p_ws; 13606 int dir; 13607 int flags = 0; 13608 char_u nbuf1[NUMBUFLEN]; 13609 char_u nbuf2[NUMBUFLEN]; 13610 char_u nbuf3[NUMBUFLEN]; 13611 int retval = 0; /* default: FAIL */ 13612 long lnum_stop = 0; 13613 13614 /* Get the three pattern arguments: start, middle, end. */ 13615 spat = get_tv_string_chk(&argvars[0]); 13616 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1); 13617 epat = get_tv_string_buf_chk(&argvars[2], nbuf2); 13618 if (spat == NULL || mpat == NULL || epat == NULL) 13619 goto theend; /* type error */ 13620 13621 /* Handle the optional fourth argument: flags */ 13622 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */ 13623 if (dir == 0) 13624 goto theend; 13625 13626 /* Don't accept SP_END or SP_SUBPAT. 13627 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set. 13628 */ 13629 if ((flags & (SP_END | SP_SUBPAT)) != 0 13630 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))) 13631 { 13632 EMSG2(_(e_invarg2), get_tv_string(&argvars[3])); 13633 goto theend; 13634 } 13635 13636 /* Optional fifth argument: skip expression */ 13637 if (argvars[3].v_type == VAR_UNKNOWN 13638 || argvars[4].v_type == VAR_UNKNOWN) 13639 skip = (char_u *)""; 13640 else 13641 { 13642 skip = get_tv_string_buf_chk(&argvars[4], nbuf3); 13643 if (argvars[5].v_type != VAR_UNKNOWN) 13644 { 13645 lnum_stop = get_tv_number_chk(&argvars[5], NULL); 13646 if (lnum_stop < 0) 13647 goto theend; 13648 } 13649 } 13650 if (skip == NULL) 13651 goto theend; /* type error */ 13652 13653 retval = do_searchpair(spat, mpat, epat, dir, skip, flags, 13654 match_pos, lnum_stop); 13655 13656 theend: 13657 p_ws = save_p_ws; 13658 13659 return retval; 13660 } 13661 13662 /* 13663 * "searchpair()" function 13664 */ 13665 static void 13666 f_searchpair(argvars, rettv) 13667 typval_T *argvars; 13668 typval_T *rettv; 13669 { 13670 rettv->vval.v_number = searchpair_cmn(argvars, NULL); 13671 } 13672 13673 /* 13674 * "searchpairpos()" function 13675 */ 13676 static void 13677 f_searchpairpos(argvars, rettv) 13678 typval_T *argvars; 13679 typval_T *rettv; 13680 { 13681 pos_T match_pos; 13682 int lnum = 0; 13683 int col = 0; 13684 13685 rettv->vval.v_number = 0; 13686 13687 if (rettv_list_alloc(rettv) == FAIL) 13688 return; 13689 13690 if (searchpair_cmn(argvars, &match_pos) > 0) 13691 { 13692 lnum = match_pos.lnum; 13693 col = match_pos.col; 13694 } 13695 13696 list_append_number(rettv->vval.v_list, (varnumber_T)lnum); 13697 list_append_number(rettv->vval.v_list, (varnumber_T)col); 13698 } 13699 13700 /* 13701 * Search for a start/middle/end thing. 13702 * Used by searchpair(), see its documentation for the details. 13703 * Returns 0 or -1 for no match, 13704 */ 13705 long 13706 do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop) 13707 char_u *spat; /* start pattern */ 13708 char_u *mpat; /* middle pattern */ 13709 char_u *epat; /* end pattern */ 13710 int dir; /* BACKWARD or FORWARD */ 13711 char_u *skip; /* skip expression */ 13712 int flags; /* SP_SETPCMARK and other SP_ values */ 13713 pos_T *match_pos; 13714 linenr_T lnum_stop; /* stop at this line if not zero */ 13715 { 13716 char_u *save_cpo; 13717 char_u *pat, *pat2 = NULL, *pat3 = NULL; 13718 long retval = 0; 13719 pos_T pos; 13720 pos_T firstpos; 13721 pos_T foundpos; 13722 pos_T save_cursor; 13723 pos_T save_pos; 13724 int n; 13725 int r; 13726 int nest = 1; 13727 int err; 13728 int options = SEARCH_KEEP; 13729 13730 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 13731 save_cpo = p_cpo; 13732 p_cpo = (char_u *)""; 13733 13734 /* Make two search patterns: start/end (pat2, for in nested pairs) and 13735 * start/middle/end (pat3, for the top pair). */ 13736 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15)); 13737 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23)); 13738 if (pat2 == NULL || pat3 == NULL) 13739 goto theend; 13740 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat); 13741 if (*mpat == NUL) 13742 STRCPY(pat3, pat2); 13743 else 13744 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)", 13745 spat, epat, mpat); 13746 if (flags & SP_START) 13747 options |= SEARCH_START; 13748 13749 save_cursor = curwin->w_cursor; 13750 pos = curwin->w_cursor; 13751 clearpos(&firstpos); 13752 clearpos(&foundpos); 13753 pat = pat3; 13754 for (;;) 13755 { 13756 n = searchit(curwin, curbuf, &pos, dir, pat, 1L, 13757 options, RE_SEARCH, lnum_stop); 13758 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos))) 13759 /* didn't find it or found the first match again: FAIL */ 13760 break; 13761 13762 if (firstpos.lnum == 0) 13763 firstpos = pos; 13764 if (equalpos(pos, foundpos)) 13765 { 13766 /* Found the same position again. Can happen with a pattern that 13767 * has "\zs" at the end and searching backwards. Advance one 13768 * character and try again. */ 13769 if (dir == BACKWARD) 13770 decl(&pos); 13771 else 13772 incl(&pos); 13773 } 13774 foundpos = pos; 13775 13776 /* If the skip pattern matches, ignore this match. */ 13777 if (*skip != NUL) 13778 { 13779 save_pos = curwin->w_cursor; 13780 curwin->w_cursor = pos; 13781 r = eval_to_bool(skip, &err, NULL, FALSE); 13782 curwin->w_cursor = save_pos; 13783 if (err) 13784 { 13785 /* Evaluating {skip} caused an error, break here. */ 13786 curwin->w_cursor = save_cursor; 13787 retval = -1; 13788 break; 13789 } 13790 if (r) 13791 continue; 13792 } 13793 13794 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2)) 13795 { 13796 /* Found end when searching backwards or start when searching 13797 * forward: nested pair. */ 13798 ++nest; 13799 pat = pat2; /* nested, don't search for middle */ 13800 } 13801 else 13802 { 13803 /* Found end when searching forward or start when searching 13804 * backward: end of (nested) pair; or found middle in outer pair. */ 13805 if (--nest == 1) 13806 pat = pat3; /* outer level, search for middle */ 13807 } 13808 13809 if (nest == 0) 13810 { 13811 /* Found the match: return matchcount or line number. */ 13812 if (flags & SP_RETCOUNT) 13813 ++retval; 13814 else 13815 retval = pos.lnum; 13816 if (flags & SP_SETPCMARK) 13817 setpcmark(); 13818 curwin->w_cursor = pos; 13819 if (!(flags & SP_REPEAT)) 13820 break; 13821 nest = 1; /* search for next unmatched */ 13822 } 13823 } 13824 13825 if (match_pos != NULL) 13826 { 13827 /* Store the match cursor position */ 13828 match_pos->lnum = curwin->w_cursor.lnum; 13829 match_pos->col = curwin->w_cursor.col + 1; 13830 } 13831 13832 /* If 'n' flag is used or search failed: restore cursor position. */ 13833 if ((flags & SP_NOMOVE) || retval == 0) 13834 curwin->w_cursor = save_cursor; 13835 13836 theend: 13837 vim_free(pat2); 13838 vim_free(pat3); 13839 p_cpo = save_cpo; 13840 13841 return retval; 13842 } 13843 13844 /* 13845 * "searchpos()" function 13846 */ 13847 static void 13848 f_searchpos(argvars, rettv) 13849 typval_T *argvars; 13850 typval_T *rettv; 13851 { 13852 pos_T match_pos; 13853 int lnum = 0; 13854 int col = 0; 13855 int n; 13856 int flags = 0; 13857 13858 rettv->vval.v_number = 0; 13859 13860 if (rettv_list_alloc(rettv) == FAIL) 13861 return; 13862 13863 n = search_cmn(argvars, &match_pos, &flags); 13864 if (n > 0) 13865 { 13866 lnum = match_pos.lnum; 13867 col = match_pos.col; 13868 } 13869 13870 list_append_number(rettv->vval.v_list, (varnumber_T)lnum); 13871 list_append_number(rettv->vval.v_list, (varnumber_T)col); 13872 if (flags & SP_SUBPAT) 13873 list_append_number(rettv->vval.v_list, (varnumber_T)n); 13874 } 13875 13876 13877 /*ARGSUSED*/ 13878 static void 13879 f_server2client(argvars, rettv) 13880 typval_T *argvars; 13881 typval_T *rettv; 13882 { 13883 #ifdef FEAT_CLIENTSERVER 13884 char_u buf[NUMBUFLEN]; 13885 char_u *server = get_tv_string_chk(&argvars[0]); 13886 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf); 13887 13888 rettv->vval.v_number = -1; 13889 if (server == NULL || reply == NULL) 13890 return; 13891 if (check_restricted() || check_secure()) 13892 return; 13893 # ifdef FEAT_X11 13894 if (check_connection() == FAIL) 13895 return; 13896 # endif 13897 13898 if (serverSendReply(server, reply) < 0) 13899 { 13900 EMSG(_("E258: Unable to send to client")); 13901 return; 13902 } 13903 rettv->vval.v_number = 0; 13904 #else 13905 rettv->vval.v_number = -1; 13906 #endif 13907 } 13908 13909 /*ARGSUSED*/ 13910 static void 13911 f_serverlist(argvars, rettv) 13912 typval_T *argvars; 13913 typval_T *rettv; 13914 { 13915 char_u *r = NULL; 13916 13917 #ifdef FEAT_CLIENTSERVER 13918 # ifdef WIN32 13919 r = serverGetVimNames(); 13920 # else 13921 make_connection(); 13922 if (X_DISPLAY != NULL) 13923 r = serverGetVimNames(X_DISPLAY); 13924 # endif 13925 #endif 13926 rettv->v_type = VAR_STRING; 13927 rettv->vval.v_string = r; 13928 } 13929 13930 /* 13931 * "setbufvar()" function 13932 */ 13933 /*ARGSUSED*/ 13934 static void 13935 f_setbufvar(argvars, rettv) 13936 typval_T *argvars; 13937 typval_T *rettv; 13938 { 13939 buf_T *buf; 13940 #ifdef FEAT_AUTOCMD 13941 aco_save_T aco; 13942 #else 13943 buf_T *save_curbuf; 13944 #endif 13945 char_u *varname, *bufvarname; 13946 typval_T *varp; 13947 char_u nbuf[NUMBUFLEN]; 13948 13949 rettv->vval.v_number = 0; 13950 13951 if (check_restricted() || check_secure()) 13952 return; 13953 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ 13954 varname = get_tv_string_chk(&argvars[1]); 13955 buf = get_buf_tv(&argvars[0]); 13956 varp = &argvars[2]; 13957 13958 if (buf != NULL && varname != NULL && varp != NULL) 13959 { 13960 /* set curbuf to be our buf, temporarily */ 13961 #ifdef FEAT_AUTOCMD 13962 aucmd_prepbuf(&aco, buf); 13963 #else 13964 save_curbuf = curbuf; 13965 curbuf = buf; 13966 #endif 13967 13968 if (*varname == '&') 13969 { 13970 long numval; 13971 char_u *strval; 13972 int error = FALSE; 13973 13974 ++varname; 13975 numval = get_tv_number_chk(varp, &error); 13976 strval = get_tv_string_buf_chk(varp, nbuf); 13977 if (!error && strval != NULL) 13978 set_option_value(varname, numval, strval, OPT_LOCAL); 13979 } 13980 else 13981 { 13982 bufvarname = alloc((unsigned)STRLEN(varname) + 3); 13983 if (bufvarname != NULL) 13984 { 13985 STRCPY(bufvarname, "b:"); 13986 STRCPY(bufvarname + 2, varname); 13987 set_var(bufvarname, varp, TRUE); 13988 vim_free(bufvarname); 13989 } 13990 } 13991 13992 /* reset notion of buffer */ 13993 #ifdef FEAT_AUTOCMD 13994 aucmd_restbuf(&aco); 13995 #else 13996 curbuf = save_curbuf; 13997 #endif 13998 } 13999 } 14000 14001 /* 14002 * "setcmdpos()" function 14003 */ 14004 static void 14005 f_setcmdpos(argvars, rettv) 14006 typval_T *argvars; 14007 typval_T *rettv; 14008 { 14009 int pos = (int)get_tv_number(&argvars[0]) - 1; 14010 14011 if (pos >= 0) 14012 rettv->vval.v_number = set_cmdline_pos(pos); 14013 } 14014 14015 /* 14016 * "setline()" function 14017 */ 14018 static void 14019 f_setline(argvars, rettv) 14020 typval_T *argvars; 14021 typval_T *rettv; 14022 { 14023 linenr_T lnum; 14024 char_u *line = NULL; 14025 list_T *l = NULL; 14026 listitem_T *li = NULL; 14027 long added = 0; 14028 linenr_T lcount = curbuf->b_ml.ml_line_count; 14029 14030 lnum = get_tv_lnum(&argvars[0]); 14031 if (argvars[1].v_type == VAR_LIST) 14032 { 14033 l = argvars[1].vval.v_list; 14034 li = l->lv_first; 14035 } 14036 else 14037 line = get_tv_string_chk(&argvars[1]); 14038 14039 rettv->vval.v_number = 0; /* OK */ 14040 for (;;) 14041 { 14042 if (l != NULL) 14043 { 14044 /* list argument, get next string */ 14045 if (li == NULL) 14046 break; 14047 line = get_tv_string_chk(&li->li_tv); 14048 li = li->li_next; 14049 } 14050 14051 rettv->vval.v_number = 1; /* FAIL */ 14052 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) 14053 break; 14054 if (lnum <= curbuf->b_ml.ml_line_count) 14055 { 14056 /* existing line, replace it */ 14057 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK) 14058 { 14059 changed_bytes(lnum, 0); 14060 check_cursor_col(); 14061 rettv->vval.v_number = 0; /* OK */ 14062 } 14063 } 14064 else if (added > 0 || u_save(lnum - 1, lnum) == OK) 14065 { 14066 /* lnum is one past the last line, append the line */ 14067 ++added; 14068 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK) 14069 rettv->vval.v_number = 0; /* OK */ 14070 } 14071 14072 if (l == NULL) /* only one string argument */ 14073 break; 14074 ++lnum; 14075 } 14076 14077 if (added > 0) 14078 appended_lines_mark(lcount, added); 14079 } 14080 14081 /* 14082 * Used by "setqflist()" and "setloclist()" functions 14083 */ 14084 /*ARGSUSED*/ 14085 static void 14086 set_qf_ll_list(wp, list_arg, action_arg, rettv) 14087 win_T *wp; 14088 typval_T *list_arg; 14089 typval_T *action_arg; 14090 typval_T *rettv; 14091 { 14092 #ifdef FEAT_QUICKFIX 14093 char_u *act; 14094 int action = ' '; 14095 #endif 14096 14097 rettv->vval.v_number = -1; 14098 14099 #ifdef FEAT_QUICKFIX 14100 if (list_arg->v_type != VAR_LIST) 14101 EMSG(_(e_listreq)); 14102 else 14103 { 14104 list_T *l = list_arg->vval.v_list; 14105 14106 if (action_arg->v_type == VAR_STRING) 14107 { 14108 act = get_tv_string_chk(action_arg); 14109 if (act == NULL) 14110 return; /* type error; errmsg already given */ 14111 if (*act == 'a' || *act == 'r') 14112 action = *act; 14113 } 14114 14115 if (l != NULL && set_errorlist(wp, l, action) == OK) 14116 rettv->vval.v_number = 0; 14117 } 14118 #endif 14119 } 14120 14121 /* 14122 * "setloclist()" function 14123 */ 14124 /*ARGSUSED*/ 14125 static void 14126 f_setloclist(argvars, rettv) 14127 typval_T *argvars; 14128 typval_T *rettv; 14129 { 14130 win_T *win; 14131 14132 rettv->vval.v_number = -1; 14133 14134 win = find_win_by_nr(&argvars[0]); 14135 if (win != NULL) 14136 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv); 14137 } 14138 14139 /* 14140 * "setpos()" function 14141 */ 14142 /*ARGSUSED*/ 14143 static void 14144 f_setpos(argvars, rettv) 14145 typval_T *argvars; 14146 typval_T *rettv; 14147 { 14148 pos_T pos; 14149 int fnum; 14150 char_u *name; 14151 14152 name = get_tv_string_chk(argvars); 14153 if (name != NULL) 14154 { 14155 if (list2fpos(&argvars[1], &pos, &fnum) == OK) 14156 { 14157 --pos.col; 14158 if (name[0] == '.') /* cursor */ 14159 { 14160 if (fnum == curbuf->b_fnum) 14161 { 14162 curwin->w_cursor = pos; 14163 check_cursor(); 14164 } 14165 else 14166 EMSG(_(e_invarg)); 14167 } 14168 else if (name[0] == '\'') /* mark */ 14169 (void)setmark_pos(name[1], &pos, fnum); 14170 else 14171 EMSG(_(e_invarg)); 14172 } 14173 } 14174 } 14175 14176 /* 14177 * "setqflist()" function 14178 */ 14179 /*ARGSUSED*/ 14180 static void 14181 f_setqflist(argvars, rettv) 14182 typval_T *argvars; 14183 typval_T *rettv; 14184 { 14185 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv); 14186 } 14187 14188 /* 14189 * "setreg()" function 14190 */ 14191 static void 14192 f_setreg(argvars, rettv) 14193 typval_T *argvars; 14194 typval_T *rettv; 14195 { 14196 int regname; 14197 char_u *strregname; 14198 char_u *stropt; 14199 char_u *strval; 14200 int append; 14201 char_u yank_type; 14202 long block_len; 14203 14204 block_len = -1; 14205 yank_type = MAUTO; 14206 append = FALSE; 14207 14208 strregname = get_tv_string_chk(argvars); 14209 rettv->vval.v_number = 1; /* FAIL is default */ 14210 14211 if (strregname == NULL) 14212 return; /* type error; errmsg already given */ 14213 regname = *strregname; 14214 if (regname == 0 || regname == '@') 14215 regname = '"'; 14216 else if (regname == '=') 14217 return; 14218 14219 if (argvars[2].v_type != VAR_UNKNOWN) 14220 { 14221 stropt = get_tv_string_chk(&argvars[2]); 14222 if (stropt == NULL) 14223 return; /* type error */ 14224 for (; *stropt != NUL; ++stropt) 14225 switch (*stropt) 14226 { 14227 case 'a': case 'A': /* append */ 14228 append = TRUE; 14229 break; 14230 case 'v': case 'c': /* character-wise selection */ 14231 yank_type = MCHAR; 14232 break; 14233 case 'V': case 'l': /* line-wise selection */ 14234 yank_type = MLINE; 14235 break; 14236 #ifdef FEAT_VISUAL 14237 case 'b': case Ctrl_V: /* block-wise selection */ 14238 yank_type = MBLOCK; 14239 if (VIM_ISDIGIT(stropt[1])) 14240 { 14241 ++stropt; 14242 block_len = getdigits(&stropt) - 1; 14243 --stropt; 14244 } 14245 break; 14246 #endif 14247 } 14248 } 14249 14250 strval = get_tv_string_chk(&argvars[1]); 14251 if (strval != NULL) 14252 write_reg_contents_ex(regname, strval, -1, 14253 append, yank_type, block_len); 14254 rettv->vval.v_number = 0; 14255 } 14256 14257 14258 /* 14259 * "setwinvar(expr)" function 14260 */ 14261 /*ARGSUSED*/ 14262 static void 14263 f_setwinvar(argvars, rettv) 14264 typval_T *argvars; 14265 typval_T *rettv; 14266 { 14267 win_T *win; 14268 #ifdef FEAT_WINDOWS 14269 win_T *save_curwin; 14270 #endif 14271 char_u *varname, *winvarname; 14272 typval_T *varp; 14273 char_u nbuf[NUMBUFLEN]; 14274 14275 rettv->vval.v_number = 0; 14276 14277 if (check_restricted() || check_secure()) 14278 return; 14279 win = find_win_by_nr(&argvars[0]); 14280 varname = get_tv_string_chk(&argvars[1]); 14281 varp = &argvars[2]; 14282 14283 if (win != NULL && varname != NULL && varp != NULL) 14284 { 14285 #ifdef FEAT_WINDOWS 14286 /* set curwin to be our win, temporarily */ 14287 save_curwin = curwin; 14288 curwin = win; 14289 curbuf = curwin->w_buffer; 14290 #endif 14291 14292 if (*varname == '&') 14293 { 14294 long numval; 14295 char_u *strval; 14296 int error = FALSE; 14297 14298 ++varname; 14299 numval = get_tv_number_chk(varp, &error); 14300 strval = get_tv_string_buf_chk(varp, nbuf); 14301 if (!error && strval != NULL) 14302 set_option_value(varname, numval, strval, OPT_LOCAL); 14303 } 14304 else 14305 { 14306 winvarname = alloc((unsigned)STRLEN(varname) + 3); 14307 if (winvarname != NULL) 14308 { 14309 STRCPY(winvarname, "w:"); 14310 STRCPY(winvarname + 2, varname); 14311 set_var(winvarname, varp, TRUE); 14312 vim_free(winvarname); 14313 } 14314 } 14315 14316 #ifdef FEAT_WINDOWS 14317 /* Restore current window, if it's still valid (autocomands can make 14318 * it invalid). */ 14319 if (win_valid(save_curwin)) 14320 { 14321 curwin = save_curwin; 14322 curbuf = curwin->w_buffer; 14323 } 14324 #endif 14325 } 14326 } 14327 14328 /* 14329 * "simplify()" function 14330 */ 14331 static void 14332 f_simplify(argvars, rettv) 14333 typval_T *argvars; 14334 typval_T *rettv; 14335 { 14336 char_u *p; 14337 14338 p = get_tv_string(&argvars[0]); 14339 rettv->vval.v_string = vim_strsave(p); 14340 simplify_filename(rettv->vval.v_string); /* simplify in place */ 14341 rettv->v_type = VAR_STRING; 14342 } 14343 14344 static int 14345 #ifdef __BORLANDC__ 14346 _RTLENTRYF 14347 #endif 14348 item_compare __ARGS((const void *s1, const void *s2)); 14349 static int 14350 #ifdef __BORLANDC__ 14351 _RTLENTRYF 14352 #endif 14353 item_compare2 __ARGS((const void *s1, const void *s2)); 14354 14355 static int item_compare_ic; 14356 static char_u *item_compare_func; 14357 static int item_compare_func_err; 14358 #define ITEM_COMPARE_FAIL 999 14359 14360 /* 14361 * Compare functions for f_sort() below. 14362 */ 14363 static int 14364 #ifdef __BORLANDC__ 14365 _RTLENTRYF 14366 #endif 14367 item_compare(s1, s2) 14368 const void *s1; 14369 const void *s2; 14370 { 14371 char_u *p1, *p2; 14372 char_u *tofree1, *tofree2; 14373 int res; 14374 char_u numbuf1[NUMBUFLEN]; 14375 char_u numbuf2[NUMBUFLEN]; 14376 14377 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0); 14378 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0); 14379 if (item_compare_ic) 14380 res = STRICMP(p1, p2); 14381 else 14382 res = STRCMP(p1, p2); 14383 vim_free(tofree1); 14384 vim_free(tofree2); 14385 return res; 14386 } 14387 14388 static int 14389 #ifdef __BORLANDC__ 14390 _RTLENTRYF 14391 #endif 14392 item_compare2(s1, s2) 14393 const void *s1; 14394 const void *s2; 14395 { 14396 int res; 14397 typval_T rettv; 14398 typval_T argv[2]; 14399 int dummy; 14400 14401 /* shortcut after failure in previous call; compare all items equal */ 14402 if (item_compare_func_err) 14403 return 0; 14404 14405 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED 14406 * in the copy without changing the original list items. */ 14407 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]); 14408 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]); 14409 14410 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ 14411 res = call_func(item_compare_func, STRLEN(item_compare_func), 14412 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL); 14413 clear_tv(&argv[0]); 14414 clear_tv(&argv[1]); 14415 14416 if (res == FAIL) 14417 res = ITEM_COMPARE_FAIL; 14418 else 14419 /* return value has wrong type */ 14420 res = get_tv_number_chk(&rettv, &item_compare_func_err); 14421 if (item_compare_func_err) 14422 res = ITEM_COMPARE_FAIL; 14423 clear_tv(&rettv); 14424 return res; 14425 } 14426 14427 /* 14428 * "sort({list})" function 14429 */ 14430 static void 14431 f_sort(argvars, rettv) 14432 typval_T *argvars; 14433 typval_T *rettv; 14434 { 14435 list_T *l; 14436 listitem_T *li; 14437 listitem_T **ptrs; 14438 long len; 14439 long i; 14440 14441 rettv->vval.v_number = 0; 14442 if (argvars[0].v_type != VAR_LIST) 14443 EMSG2(_(e_listarg), "sort()"); 14444 else 14445 { 14446 l = argvars[0].vval.v_list; 14447 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()")) 14448 return; 14449 rettv->vval.v_list = l; 14450 rettv->v_type = VAR_LIST; 14451 ++l->lv_refcount; 14452 14453 len = list_len(l); 14454 if (len <= 1) 14455 return; /* short list sorts pretty quickly */ 14456 14457 item_compare_ic = FALSE; 14458 item_compare_func = NULL; 14459 if (argvars[1].v_type != VAR_UNKNOWN) 14460 { 14461 if (argvars[1].v_type == VAR_FUNC) 14462 item_compare_func = argvars[1].vval.v_string; 14463 else 14464 { 14465 int error = FALSE; 14466 14467 i = get_tv_number_chk(&argvars[1], &error); 14468 if (error) 14469 return; /* type error; errmsg already given */ 14470 if (i == 1) 14471 item_compare_ic = TRUE; 14472 else 14473 item_compare_func = get_tv_string(&argvars[1]); 14474 } 14475 } 14476 14477 /* Make an array with each entry pointing to an item in the List. */ 14478 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *))); 14479 if (ptrs == NULL) 14480 return; 14481 i = 0; 14482 for (li = l->lv_first; li != NULL; li = li->li_next) 14483 ptrs[i++] = li; 14484 14485 item_compare_func_err = FALSE; 14486 /* test the compare function */ 14487 if (item_compare_func != NULL 14488 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1]) 14489 == ITEM_COMPARE_FAIL) 14490 EMSG(_("E702: Sort compare function failed")); 14491 else 14492 { 14493 /* Sort the array with item pointers. */ 14494 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *), 14495 item_compare_func == NULL ? item_compare : item_compare2); 14496 14497 if (!item_compare_func_err) 14498 { 14499 /* Clear the List and append the items in the sorted order. */ 14500 l->lv_first = l->lv_last = NULL; 14501 l->lv_len = 0; 14502 for (i = 0; i < len; ++i) 14503 list_append(l, ptrs[i]); 14504 } 14505 } 14506 14507 vim_free(ptrs); 14508 } 14509 } 14510 14511 /* 14512 * "soundfold({word})" function 14513 */ 14514 static void 14515 f_soundfold(argvars, rettv) 14516 typval_T *argvars; 14517 typval_T *rettv; 14518 { 14519 char_u *s; 14520 14521 rettv->v_type = VAR_STRING; 14522 s = get_tv_string(&argvars[0]); 14523 #ifdef FEAT_SPELL 14524 rettv->vval.v_string = eval_soundfold(s); 14525 #else 14526 rettv->vval.v_string = vim_strsave(s); 14527 #endif 14528 } 14529 14530 /* 14531 * "spellbadword()" function 14532 */ 14533 /* ARGSUSED */ 14534 static void 14535 f_spellbadword(argvars, rettv) 14536 typval_T *argvars; 14537 typval_T *rettv; 14538 { 14539 char_u *word = (char_u *)""; 14540 hlf_T attr = HLF_COUNT; 14541 int len = 0; 14542 14543 if (rettv_list_alloc(rettv) == FAIL) 14544 return; 14545 14546 #ifdef FEAT_SPELL 14547 if (argvars[0].v_type == VAR_UNKNOWN) 14548 { 14549 /* Find the start and length of the badly spelled word. */ 14550 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr); 14551 if (len != 0) 14552 word = ml_get_cursor(); 14553 } 14554 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) 14555 { 14556 char_u *str = get_tv_string_chk(&argvars[0]); 14557 int capcol = -1; 14558 14559 if (str != NULL) 14560 { 14561 /* Check the argument for spelling. */ 14562 while (*str != NUL) 14563 { 14564 len = spell_check(curwin, str, &attr, &capcol, FALSE); 14565 if (attr != HLF_COUNT) 14566 { 14567 word = str; 14568 break; 14569 } 14570 str += len; 14571 } 14572 } 14573 } 14574 #endif 14575 14576 list_append_string(rettv->vval.v_list, word, len); 14577 list_append_string(rettv->vval.v_list, (char_u *)( 14578 attr == HLF_SPB ? "bad" : 14579 attr == HLF_SPR ? "rare" : 14580 attr == HLF_SPL ? "local" : 14581 attr == HLF_SPC ? "caps" : 14582 ""), -1); 14583 } 14584 14585 /* 14586 * "spellsuggest()" function 14587 */ 14588 /*ARGSUSED*/ 14589 static void 14590 f_spellsuggest(argvars, rettv) 14591 typval_T *argvars; 14592 typval_T *rettv; 14593 { 14594 #ifdef FEAT_SPELL 14595 char_u *str; 14596 int typeerr = FALSE; 14597 int maxcount; 14598 garray_T ga; 14599 int i; 14600 listitem_T *li; 14601 int need_capital = FALSE; 14602 #endif 14603 14604 if (rettv_list_alloc(rettv) == FAIL) 14605 return; 14606 14607 #ifdef FEAT_SPELL 14608 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) 14609 { 14610 str = get_tv_string(&argvars[0]); 14611 if (argvars[1].v_type != VAR_UNKNOWN) 14612 { 14613 maxcount = get_tv_number_chk(&argvars[1], &typeerr); 14614 if (maxcount <= 0) 14615 return; 14616 if (argvars[2].v_type != VAR_UNKNOWN) 14617 { 14618 need_capital = get_tv_number_chk(&argvars[2], &typeerr); 14619 if (typeerr) 14620 return; 14621 } 14622 } 14623 else 14624 maxcount = 25; 14625 14626 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE); 14627 14628 for (i = 0; i < ga.ga_len; ++i) 14629 { 14630 str = ((char_u **)ga.ga_data)[i]; 14631 14632 li = listitem_alloc(); 14633 if (li == NULL) 14634 vim_free(str); 14635 else 14636 { 14637 li->li_tv.v_type = VAR_STRING; 14638 li->li_tv.v_lock = 0; 14639 li->li_tv.vval.v_string = str; 14640 list_append(rettv->vval.v_list, li); 14641 } 14642 } 14643 ga_clear(&ga); 14644 } 14645 #endif 14646 } 14647 14648 static void 14649 f_split(argvars, rettv) 14650 typval_T *argvars; 14651 typval_T *rettv; 14652 { 14653 char_u *str; 14654 char_u *end; 14655 char_u *pat = NULL; 14656 regmatch_T regmatch; 14657 char_u patbuf[NUMBUFLEN]; 14658 char_u *save_cpo; 14659 int match; 14660 colnr_T col = 0; 14661 int keepempty = FALSE; 14662 int typeerr = FALSE; 14663 14664 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 14665 save_cpo = p_cpo; 14666 p_cpo = (char_u *)""; 14667 14668 str = get_tv_string(&argvars[0]); 14669 if (argvars[1].v_type != VAR_UNKNOWN) 14670 { 14671 pat = get_tv_string_buf_chk(&argvars[1], patbuf); 14672 if (pat == NULL) 14673 typeerr = TRUE; 14674 if (argvars[2].v_type != VAR_UNKNOWN) 14675 keepempty = get_tv_number_chk(&argvars[2], &typeerr); 14676 } 14677 if (pat == NULL || *pat == NUL) 14678 pat = (char_u *)"[\\x01- ]\\+"; 14679 14680 if (rettv_list_alloc(rettv) == FAIL) 14681 return; 14682 if (typeerr) 14683 return; 14684 14685 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 14686 if (regmatch.regprog != NULL) 14687 { 14688 regmatch.rm_ic = FALSE; 14689 while (*str != NUL || keepempty) 14690 { 14691 if (*str == NUL) 14692 match = FALSE; /* empty item at the end */ 14693 else 14694 match = vim_regexec_nl(®match, str, col); 14695 if (match) 14696 end = regmatch.startp[0]; 14697 else 14698 end = str + STRLEN(str); 14699 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0 14700 && *str != NUL && match && end < regmatch.endp[0])) 14701 { 14702 if (list_append_string(rettv->vval.v_list, str, 14703 (int)(end - str)) == FAIL) 14704 break; 14705 } 14706 if (!match) 14707 break; 14708 /* Advance to just after the match. */ 14709 if (regmatch.endp[0] > str) 14710 col = 0; 14711 else 14712 { 14713 /* Don't get stuck at the same match. */ 14714 #ifdef FEAT_MBYTE 14715 col = (*mb_ptr2len)(regmatch.endp[0]); 14716 #else 14717 col = 1; 14718 #endif 14719 } 14720 str = regmatch.endp[0]; 14721 } 14722 14723 vim_free(regmatch.regprog); 14724 } 14725 14726 p_cpo = save_cpo; 14727 } 14728 14729 /* 14730 * "str2nr()" function 14731 */ 14732 static void 14733 f_str2nr(argvars, rettv) 14734 typval_T *argvars; 14735 typval_T *rettv; 14736 { 14737 int base = 10; 14738 char_u *p; 14739 long n; 14740 14741 if (argvars[1].v_type != VAR_UNKNOWN) 14742 { 14743 base = get_tv_number(&argvars[1]); 14744 if (base != 8 && base != 10 && base != 16) 14745 { 14746 EMSG(_(e_invarg)); 14747 return; 14748 } 14749 } 14750 14751 p = skipwhite(get_tv_string(&argvars[0])); 14752 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL); 14753 rettv->vval.v_number = n; 14754 } 14755 14756 #ifdef HAVE_STRFTIME 14757 /* 14758 * "strftime({format}[, {time}])" function 14759 */ 14760 static void 14761 f_strftime(argvars, rettv) 14762 typval_T *argvars; 14763 typval_T *rettv; 14764 { 14765 char_u result_buf[256]; 14766 struct tm *curtime; 14767 time_t seconds; 14768 char_u *p; 14769 14770 rettv->v_type = VAR_STRING; 14771 14772 p = get_tv_string(&argvars[0]); 14773 if (argvars[1].v_type == VAR_UNKNOWN) 14774 seconds = time(NULL); 14775 else 14776 seconds = (time_t)get_tv_number(&argvars[1]); 14777 curtime = localtime(&seconds); 14778 /* MSVC returns NULL for an invalid value of seconds. */ 14779 if (curtime == NULL) 14780 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)")); 14781 else 14782 { 14783 # ifdef FEAT_MBYTE 14784 vimconv_T conv; 14785 char_u *enc; 14786 14787 conv.vc_type = CONV_NONE; 14788 enc = enc_locale(); 14789 convert_setup(&conv, p_enc, enc); 14790 if (conv.vc_type != CONV_NONE) 14791 p = string_convert(&conv, p, NULL); 14792 # endif 14793 if (p != NULL) 14794 (void)strftime((char *)result_buf, sizeof(result_buf), 14795 (char *)p, curtime); 14796 else 14797 result_buf[0] = NUL; 14798 14799 # ifdef FEAT_MBYTE 14800 if (conv.vc_type != CONV_NONE) 14801 vim_free(p); 14802 convert_setup(&conv, enc, p_enc); 14803 if (conv.vc_type != CONV_NONE) 14804 rettv->vval.v_string = string_convert(&conv, result_buf, NULL); 14805 else 14806 # endif 14807 rettv->vval.v_string = vim_strsave(result_buf); 14808 14809 # ifdef FEAT_MBYTE 14810 /* Release conversion descriptors */ 14811 convert_setup(&conv, NULL, NULL); 14812 vim_free(enc); 14813 # endif 14814 } 14815 } 14816 #endif 14817 14818 /* 14819 * "stridx()" function 14820 */ 14821 static void 14822 f_stridx(argvars, rettv) 14823 typval_T *argvars; 14824 typval_T *rettv; 14825 { 14826 char_u buf[NUMBUFLEN]; 14827 char_u *needle; 14828 char_u *haystack; 14829 char_u *save_haystack; 14830 char_u *pos; 14831 int start_idx; 14832 14833 needle = get_tv_string_chk(&argvars[1]); 14834 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf); 14835 rettv->vval.v_number = -1; 14836 if (needle == NULL || haystack == NULL) 14837 return; /* type error; errmsg already given */ 14838 14839 if (argvars[2].v_type != VAR_UNKNOWN) 14840 { 14841 int error = FALSE; 14842 14843 start_idx = get_tv_number_chk(&argvars[2], &error); 14844 if (error || start_idx >= (int)STRLEN(haystack)) 14845 return; 14846 if (start_idx >= 0) 14847 haystack += start_idx; 14848 } 14849 14850 pos = (char_u *)strstr((char *)haystack, (char *)needle); 14851 if (pos != NULL) 14852 rettv->vval.v_number = (varnumber_T)(pos - save_haystack); 14853 } 14854 14855 /* 14856 * "string()" function 14857 */ 14858 static void 14859 f_string(argvars, rettv) 14860 typval_T *argvars; 14861 typval_T *rettv; 14862 { 14863 char_u *tofree; 14864 char_u numbuf[NUMBUFLEN]; 14865 14866 rettv->v_type = VAR_STRING; 14867 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0); 14868 if (tofree == NULL) 14869 rettv->vval.v_string = vim_strsave(rettv->vval.v_string); 14870 } 14871 14872 /* 14873 * "strlen()" function 14874 */ 14875 static void 14876 f_strlen(argvars, rettv) 14877 typval_T *argvars; 14878 typval_T *rettv; 14879 { 14880 rettv->vval.v_number = (varnumber_T)(STRLEN( 14881 get_tv_string(&argvars[0]))); 14882 } 14883 14884 /* 14885 * "strpart()" function 14886 */ 14887 static void 14888 f_strpart(argvars, rettv) 14889 typval_T *argvars; 14890 typval_T *rettv; 14891 { 14892 char_u *p; 14893 int n; 14894 int len; 14895 int slen; 14896 int error = FALSE; 14897 14898 p = get_tv_string(&argvars[0]); 14899 slen = (int)STRLEN(p); 14900 14901 n = get_tv_number_chk(&argvars[1], &error); 14902 if (error) 14903 len = 0; 14904 else if (argvars[2].v_type != VAR_UNKNOWN) 14905 len = get_tv_number(&argvars[2]); 14906 else 14907 len = slen - n; /* default len: all bytes that are available. */ 14908 14909 /* 14910 * Only return the overlap between the specified part and the actual 14911 * string. 14912 */ 14913 if (n < 0) 14914 { 14915 len += n; 14916 n = 0; 14917 } 14918 else if (n > slen) 14919 n = slen; 14920 if (len < 0) 14921 len = 0; 14922 else if (n + len > slen) 14923 len = slen - n; 14924 14925 rettv->v_type = VAR_STRING; 14926 rettv->vval.v_string = vim_strnsave(p + n, len); 14927 } 14928 14929 /* 14930 * "strridx()" function 14931 */ 14932 static void 14933 f_strridx(argvars, rettv) 14934 typval_T *argvars; 14935 typval_T *rettv; 14936 { 14937 char_u buf[NUMBUFLEN]; 14938 char_u *needle; 14939 char_u *haystack; 14940 char_u *rest; 14941 char_u *lastmatch = NULL; 14942 int haystack_len, end_idx; 14943 14944 needle = get_tv_string_chk(&argvars[1]); 14945 haystack = get_tv_string_buf_chk(&argvars[0], buf); 14946 haystack_len = STRLEN(haystack); 14947 14948 rettv->vval.v_number = -1; 14949 if (needle == NULL || haystack == NULL) 14950 return; /* type error; errmsg already given */ 14951 if (argvars[2].v_type != VAR_UNKNOWN) 14952 { 14953 /* Third argument: upper limit for index */ 14954 end_idx = get_tv_number_chk(&argvars[2], NULL); 14955 if (end_idx < 0) 14956 return; /* can never find a match */ 14957 } 14958 else 14959 end_idx = haystack_len; 14960 14961 if (*needle == NUL) 14962 { 14963 /* Empty string matches past the end. */ 14964 lastmatch = haystack + end_idx; 14965 } 14966 else 14967 { 14968 for (rest = haystack; *rest != '\0'; ++rest) 14969 { 14970 rest = (char_u *)strstr((char *)rest, (char *)needle); 14971 if (rest == NULL || rest > haystack + end_idx) 14972 break; 14973 lastmatch = rest; 14974 } 14975 } 14976 14977 if (lastmatch == NULL) 14978 rettv->vval.v_number = -1; 14979 else 14980 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack); 14981 } 14982 14983 /* 14984 * "strtrans()" function 14985 */ 14986 static void 14987 f_strtrans(argvars, rettv) 14988 typval_T *argvars; 14989 typval_T *rettv; 14990 { 14991 rettv->v_type = VAR_STRING; 14992 rettv->vval.v_string = transstr(get_tv_string(&argvars[0])); 14993 } 14994 14995 /* 14996 * "submatch()" function 14997 */ 14998 static void 14999 f_submatch(argvars, rettv) 15000 typval_T *argvars; 15001 typval_T *rettv; 15002 { 15003 rettv->v_type = VAR_STRING; 15004 rettv->vval.v_string = 15005 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL)); 15006 } 15007 15008 /* 15009 * "substitute()" function 15010 */ 15011 static void 15012 f_substitute(argvars, rettv) 15013 typval_T *argvars; 15014 typval_T *rettv; 15015 { 15016 char_u patbuf[NUMBUFLEN]; 15017 char_u subbuf[NUMBUFLEN]; 15018 char_u flagsbuf[NUMBUFLEN]; 15019 15020 char_u *str = get_tv_string_chk(&argvars[0]); 15021 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf); 15022 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf); 15023 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf); 15024 15025 rettv->v_type = VAR_STRING; 15026 if (str == NULL || pat == NULL || sub == NULL || flg == NULL) 15027 rettv->vval.v_string = NULL; 15028 else 15029 rettv->vval.v_string = do_string_sub(str, pat, sub, flg); 15030 } 15031 15032 /* 15033 * "synID(lnum, col, trans)" function 15034 */ 15035 /*ARGSUSED*/ 15036 static void 15037 f_synID(argvars, rettv) 15038 typval_T *argvars; 15039 typval_T *rettv; 15040 { 15041 int id = 0; 15042 #ifdef FEAT_SYN_HL 15043 long lnum; 15044 long col; 15045 int trans; 15046 int transerr = FALSE; 15047 15048 lnum = get_tv_lnum(argvars); /* -1 on type error */ 15049 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */ 15050 trans = get_tv_number_chk(&argvars[2], &transerr); 15051 15052 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count 15053 && col >= 0 && col < (long)STRLEN(ml_get(lnum))) 15054 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL); 15055 #endif 15056 15057 rettv->vval.v_number = id; 15058 } 15059 15060 /* 15061 * "synIDattr(id, what [, mode])" function 15062 */ 15063 /*ARGSUSED*/ 15064 static void 15065 f_synIDattr(argvars, rettv) 15066 typval_T *argvars; 15067 typval_T *rettv; 15068 { 15069 char_u *p = NULL; 15070 #ifdef FEAT_SYN_HL 15071 int id; 15072 char_u *what; 15073 char_u *mode; 15074 char_u modebuf[NUMBUFLEN]; 15075 int modec; 15076 15077 id = get_tv_number(&argvars[0]); 15078 what = get_tv_string(&argvars[1]); 15079 if (argvars[2].v_type != VAR_UNKNOWN) 15080 { 15081 mode = get_tv_string_buf(&argvars[2], modebuf); 15082 modec = TOLOWER_ASC(mode[0]); 15083 if (modec != 't' && modec != 'c' 15084 #ifdef FEAT_GUI 15085 && modec != 'g' 15086 #endif 15087 ) 15088 modec = 0; /* replace invalid with current */ 15089 } 15090 else 15091 { 15092 #ifdef FEAT_GUI 15093 if (gui.in_use) 15094 modec = 'g'; 15095 else 15096 #endif 15097 if (t_colors > 1) 15098 modec = 'c'; 15099 else 15100 modec = 't'; 15101 } 15102 15103 15104 switch (TOLOWER_ASC(what[0])) 15105 { 15106 case 'b': 15107 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */ 15108 p = highlight_color(id, what, modec); 15109 else /* bold */ 15110 p = highlight_has_attr(id, HL_BOLD, modec); 15111 break; 15112 15113 case 'f': /* fg[#] */ 15114 p = highlight_color(id, what, modec); 15115 break; 15116 15117 case 'i': 15118 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */ 15119 p = highlight_has_attr(id, HL_INVERSE, modec); 15120 else /* italic */ 15121 p = highlight_has_attr(id, HL_ITALIC, modec); 15122 break; 15123 15124 case 'n': /* name */ 15125 p = get_highlight_name(NULL, id - 1); 15126 break; 15127 15128 case 'r': /* reverse */ 15129 p = highlight_has_attr(id, HL_INVERSE, modec); 15130 break; 15131 15132 case 's': /* standout */ 15133 p = highlight_has_attr(id, HL_STANDOUT, modec); 15134 break; 15135 15136 case 'u': 15137 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c') 15138 /* underline */ 15139 p = highlight_has_attr(id, HL_UNDERLINE, modec); 15140 else 15141 /* undercurl */ 15142 p = highlight_has_attr(id, HL_UNDERCURL, modec); 15143 break; 15144 } 15145 15146 if (p != NULL) 15147 p = vim_strsave(p); 15148 #endif 15149 rettv->v_type = VAR_STRING; 15150 rettv->vval.v_string = p; 15151 } 15152 15153 /* 15154 * "synIDtrans(id)" function 15155 */ 15156 /*ARGSUSED*/ 15157 static void 15158 f_synIDtrans(argvars, rettv) 15159 typval_T *argvars; 15160 typval_T *rettv; 15161 { 15162 int id; 15163 15164 #ifdef FEAT_SYN_HL 15165 id = get_tv_number(&argvars[0]); 15166 15167 if (id > 0) 15168 id = syn_get_final_id(id); 15169 else 15170 #endif 15171 id = 0; 15172 15173 rettv->vval.v_number = id; 15174 } 15175 15176 /* 15177 * "system()" function 15178 */ 15179 static void 15180 f_system(argvars, rettv) 15181 typval_T *argvars; 15182 typval_T *rettv; 15183 { 15184 char_u *res = NULL; 15185 char_u *p; 15186 char_u *infile = NULL; 15187 char_u buf[NUMBUFLEN]; 15188 int err = FALSE; 15189 FILE *fd; 15190 15191 if (argvars[1].v_type != VAR_UNKNOWN) 15192 { 15193 /* 15194 * Write the string to a temp file, to be used for input of the shell 15195 * command. 15196 */ 15197 if ((infile = vim_tempname('i')) == NULL) 15198 { 15199 EMSG(_(e_notmp)); 15200 return; 15201 } 15202 15203 fd = mch_fopen((char *)infile, WRITEBIN); 15204 if (fd == NULL) 15205 { 15206 EMSG2(_(e_notopen), infile); 15207 goto done; 15208 } 15209 p = get_tv_string_buf_chk(&argvars[1], buf); 15210 if (p == NULL) 15211 goto done; /* type error; errmsg already given */ 15212 if (fwrite(p, STRLEN(p), 1, fd) != 1) 15213 err = TRUE; 15214 if (fclose(fd) != 0) 15215 err = TRUE; 15216 if (err) 15217 { 15218 EMSG(_("E677: Error writing temp file")); 15219 goto done; 15220 } 15221 } 15222 15223 res = get_cmd_output(get_tv_string(&argvars[0]), infile, 15224 SHELL_SILENT | SHELL_COOKED); 15225 15226 #ifdef USE_CR 15227 /* translate <CR> into <NL> */ 15228 if (res != NULL) 15229 { 15230 char_u *s; 15231 15232 for (s = res; *s; ++s) 15233 { 15234 if (*s == CAR) 15235 *s = NL; 15236 } 15237 } 15238 #else 15239 # ifdef USE_CRNL 15240 /* translate <CR><NL> into <NL> */ 15241 if (res != NULL) 15242 { 15243 char_u *s, *d; 15244 15245 d = res; 15246 for (s = res; *s; ++s) 15247 { 15248 if (s[0] == CAR && s[1] == NL) 15249 ++s; 15250 *d++ = *s; 15251 } 15252 *d = NUL; 15253 } 15254 # endif 15255 #endif 15256 15257 done: 15258 if (infile != NULL) 15259 { 15260 mch_remove(infile); 15261 vim_free(infile); 15262 } 15263 rettv->v_type = VAR_STRING; 15264 rettv->vval.v_string = res; 15265 } 15266 15267 /* 15268 * "tabpagebuflist()" function 15269 */ 15270 /* ARGSUSED */ 15271 static void 15272 f_tabpagebuflist(argvars, rettv) 15273 typval_T *argvars; 15274 typval_T *rettv; 15275 { 15276 #ifndef FEAT_WINDOWS 15277 rettv->vval.v_number = 0; 15278 #else 15279 tabpage_T *tp; 15280 win_T *wp = NULL; 15281 15282 if (argvars[0].v_type == VAR_UNKNOWN) 15283 wp = firstwin; 15284 else 15285 { 15286 tp = find_tabpage((int)get_tv_number(&argvars[0])); 15287 if (tp != NULL) 15288 wp = (tp == curtab) ? firstwin : tp->tp_firstwin; 15289 } 15290 if (wp == NULL) 15291 rettv->vval.v_number = 0; 15292 else 15293 { 15294 if (rettv_list_alloc(rettv) == FAIL) 15295 rettv->vval.v_number = 0; 15296 else 15297 { 15298 for (; wp != NULL; wp = wp->w_next) 15299 if (list_append_number(rettv->vval.v_list, 15300 wp->w_buffer->b_fnum) == FAIL) 15301 break; 15302 } 15303 } 15304 #endif 15305 } 15306 15307 15308 /* 15309 * "tabpagenr()" function 15310 */ 15311 /* ARGSUSED */ 15312 static void 15313 f_tabpagenr(argvars, rettv) 15314 typval_T *argvars; 15315 typval_T *rettv; 15316 { 15317 int nr = 1; 15318 #ifdef FEAT_WINDOWS 15319 char_u *arg; 15320 15321 if (argvars[0].v_type != VAR_UNKNOWN) 15322 { 15323 arg = get_tv_string_chk(&argvars[0]); 15324 nr = 0; 15325 if (arg != NULL) 15326 { 15327 if (STRCMP(arg, "$") == 0) 15328 nr = tabpage_index(NULL) - 1; 15329 else 15330 EMSG2(_(e_invexpr2), arg); 15331 } 15332 } 15333 else 15334 nr = tabpage_index(curtab); 15335 #endif 15336 rettv->vval.v_number = nr; 15337 } 15338 15339 15340 #ifdef FEAT_WINDOWS 15341 static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar)); 15342 15343 /* 15344 * Common code for tabpagewinnr() and winnr(). 15345 */ 15346 static int 15347 get_winnr(tp, argvar) 15348 tabpage_T *tp; 15349 typval_T *argvar; 15350 { 15351 win_T *twin; 15352 int nr = 1; 15353 win_T *wp; 15354 char_u *arg; 15355 15356 twin = (tp == curtab) ? curwin : tp->tp_curwin; 15357 if (argvar->v_type != VAR_UNKNOWN) 15358 { 15359 arg = get_tv_string_chk(argvar); 15360 if (arg == NULL) 15361 nr = 0; /* type error; errmsg already given */ 15362 else if (STRCMP(arg, "$") == 0) 15363 twin = (tp == curtab) ? lastwin : tp->tp_lastwin; 15364 else if (STRCMP(arg, "#") == 0) 15365 { 15366 twin = (tp == curtab) ? prevwin : tp->tp_prevwin; 15367 if (twin == NULL) 15368 nr = 0; 15369 } 15370 else 15371 { 15372 EMSG2(_(e_invexpr2), arg); 15373 nr = 0; 15374 } 15375 } 15376 15377 if (nr > 0) 15378 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin; 15379 wp != twin; wp = wp->w_next) 15380 ++nr; 15381 return nr; 15382 } 15383 #endif 15384 15385 /* 15386 * "tabpagewinnr()" function 15387 */ 15388 /* ARGSUSED */ 15389 static void 15390 f_tabpagewinnr(argvars, rettv) 15391 typval_T *argvars; 15392 typval_T *rettv; 15393 { 15394 int nr = 1; 15395 #ifdef FEAT_WINDOWS 15396 tabpage_T *tp; 15397 15398 tp = find_tabpage((int)get_tv_number(&argvars[0])); 15399 if (tp == NULL) 15400 nr = 0; 15401 else 15402 nr = get_winnr(tp, &argvars[1]); 15403 #endif 15404 rettv->vval.v_number = nr; 15405 } 15406 15407 15408 /* 15409 * "tagfiles()" function 15410 */ 15411 /*ARGSUSED*/ 15412 static void 15413 f_tagfiles(argvars, rettv) 15414 typval_T *argvars; 15415 typval_T *rettv; 15416 { 15417 char_u fname[MAXPATHL + 1]; 15418 tagname_T tn; 15419 int first; 15420 15421 if (rettv_list_alloc(rettv) == FAIL) 15422 { 15423 rettv->vval.v_number = 0; 15424 return; 15425 } 15426 15427 for (first = TRUE; ; first = FALSE) 15428 if (get_tagfname(&tn, first, fname) == FAIL 15429 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL) 15430 break; 15431 tagname_free(&tn); 15432 } 15433 15434 /* 15435 * "taglist()" function 15436 */ 15437 static void 15438 f_taglist(argvars, rettv) 15439 typval_T *argvars; 15440 typval_T *rettv; 15441 { 15442 char_u *tag_pattern; 15443 15444 tag_pattern = get_tv_string(&argvars[0]); 15445 15446 rettv->vval.v_number = FALSE; 15447 if (*tag_pattern == NUL) 15448 return; 15449 15450 if (rettv_list_alloc(rettv) == OK) 15451 (void)get_tags(rettv->vval.v_list, tag_pattern); 15452 } 15453 15454 /* 15455 * "tempname()" function 15456 */ 15457 /*ARGSUSED*/ 15458 static void 15459 f_tempname(argvars, rettv) 15460 typval_T *argvars; 15461 typval_T *rettv; 15462 { 15463 static int x = 'A'; 15464 15465 rettv->v_type = VAR_STRING; 15466 rettv->vval.v_string = vim_tempname(x); 15467 15468 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different 15469 * names. Skip 'I' and 'O', they are used for shell redirection. */ 15470 do 15471 { 15472 if (x == 'Z') 15473 x = '0'; 15474 else if (x == '9') 15475 x = 'A'; 15476 else 15477 { 15478 #ifdef EBCDIC 15479 if (x == 'I') 15480 x = 'J'; 15481 else if (x == 'R') 15482 x = 'S'; 15483 else 15484 #endif 15485 ++x; 15486 } 15487 } while (x == 'I' || x == 'O'); 15488 } 15489 15490 /* 15491 * "test(list)" function: Just checking the walls... 15492 */ 15493 /*ARGSUSED*/ 15494 static void 15495 f_test(argvars, rettv) 15496 typval_T *argvars; 15497 typval_T *rettv; 15498 { 15499 /* Used for unit testing. Change the code below to your liking. */ 15500 #if 0 15501 listitem_T *li; 15502 list_T *l; 15503 char_u *bad, *good; 15504 15505 if (argvars[0].v_type != VAR_LIST) 15506 return; 15507 l = argvars[0].vval.v_list; 15508 if (l == NULL) 15509 return; 15510 li = l->lv_first; 15511 if (li == NULL) 15512 return; 15513 bad = get_tv_string(&li->li_tv); 15514 li = li->li_next; 15515 if (li == NULL) 15516 return; 15517 good = get_tv_string(&li->li_tv); 15518 rettv->vval.v_number = test_edit_score(bad, good); 15519 #endif 15520 } 15521 15522 /* 15523 * "tolower(string)" function 15524 */ 15525 static void 15526 f_tolower(argvars, rettv) 15527 typval_T *argvars; 15528 typval_T *rettv; 15529 { 15530 char_u *p; 15531 15532 p = vim_strsave(get_tv_string(&argvars[0])); 15533 rettv->v_type = VAR_STRING; 15534 rettv->vval.v_string = p; 15535 15536 if (p != NULL) 15537 while (*p != NUL) 15538 { 15539 #ifdef FEAT_MBYTE 15540 int l; 15541 15542 if (enc_utf8) 15543 { 15544 int c, lc; 15545 15546 c = utf_ptr2char(p); 15547 lc = utf_tolower(c); 15548 l = utf_ptr2len(p); 15549 /* TODO: reallocate string when byte count changes. */ 15550 if (utf_char2len(lc) == l) 15551 utf_char2bytes(lc, p); 15552 p += l; 15553 } 15554 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) 15555 p += l; /* skip multi-byte character */ 15556 else 15557 #endif 15558 { 15559 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */ 15560 ++p; 15561 } 15562 } 15563 } 15564 15565 /* 15566 * "toupper(string)" function 15567 */ 15568 static void 15569 f_toupper(argvars, rettv) 15570 typval_T *argvars; 15571 typval_T *rettv; 15572 { 15573 rettv->v_type = VAR_STRING; 15574 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0])); 15575 } 15576 15577 /* 15578 * "tr(string, fromstr, tostr)" function 15579 */ 15580 static void 15581 f_tr(argvars, rettv) 15582 typval_T *argvars; 15583 typval_T *rettv; 15584 { 15585 char_u *instr; 15586 char_u *fromstr; 15587 char_u *tostr; 15588 char_u *p; 15589 #ifdef FEAT_MBYTE 15590 int inlen; 15591 int fromlen; 15592 int tolen; 15593 int idx; 15594 char_u *cpstr; 15595 int cplen; 15596 int first = TRUE; 15597 #endif 15598 char_u buf[NUMBUFLEN]; 15599 char_u buf2[NUMBUFLEN]; 15600 garray_T ga; 15601 15602 instr = get_tv_string(&argvars[0]); 15603 fromstr = get_tv_string_buf_chk(&argvars[1], buf); 15604 tostr = get_tv_string_buf_chk(&argvars[2], buf2); 15605 15606 /* Default return value: empty string. */ 15607 rettv->v_type = VAR_STRING; 15608 rettv->vval.v_string = NULL; 15609 if (fromstr == NULL || tostr == NULL) 15610 return; /* type error; errmsg already given */ 15611 ga_init2(&ga, (int)sizeof(char), 80); 15612 15613 #ifdef FEAT_MBYTE 15614 if (!has_mbyte) 15615 #endif 15616 /* not multi-byte: fromstr and tostr must be the same length */ 15617 if (STRLEN(fromstr) != STRLEN(tostr)) 15618 { 15619 #ifdef FEAT_MBYTE 15620 error: 15621 #endif 15622 EMSG2(_(e_invarg2), fromstr); 15623 ga_clear(&ga); 15624 return; 15625 } 15626 15627 /* fromstr and tostr have to contain the same number of chars */ 15628 while (*instr != NUL) 15629 { 15630 #ifdef FEAT_MBYTE 15631 if (has_mbyte) 15632 { 15633 inlen = (*mb_ptr2len)(instr); 15634 cpstr = instr; 15635 cplen = inlen; 15636 idx = 0; 15637 for (p = fromstr; *p != NUL; p += fromlen) 15638 { 15639 fromlen = (*mb_ptr2len)(p); 15640 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0) 15641 { 15642 for (p = tostr; *p != NUL; p += tolen) 15643 { 15644 tolen = (*mb_ptr2len)(p); 15645 if (idx-- == 0) 15646 { 15647 cplen = tolen; 15648 cpstr = p; 15649 break; 15650 } 15651 } 15652 if (*p == NUL) /* tostr is shorter than fromstr */ 15653 goto error; 15654 break; 15655 } 15656 ++idx; 15657 } 15658 15659 if (first && cpstr == instr) 15660 { 15661 /* Check that fromstr and tostr have the same number of 15662 * (multi-byte) characters. Done only once when a character 15663 * of instr doesn't appear in fromstr. */ 15664 first = FALSE; 15665 for (p = tostr; *p != NUL; p += tolen) 15666 { 15667 tolen = (*mb_ptr2len)(p); 15668 --idx; 15669 } 15670 if (idx != 0) 15671 goto error; 15672 } 15673 15674 ga_grow(&ga, cplen); 15675 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen); 15676 ga.ga_len += cplen; 15677 15678 instr += inlen; 15679 } 15680 else 15681 #endif 15682 { 15683 /* When not using multi-byte chars we can do it faster. */ 15684 p = vim_strchr(fromstr, *instr); 15685 if (p != NULL) 15686 ga_append(&ga, tostr[p - fromstr]); 15687 else 15688 ga_append(&ga, *instr); 15689 ++instr; 15690 } 15691 } 15692 15693 rettv->vval.v_string = ga.ga_data; 15694 } 15695 15696 /* 15697 * "type(expr)" function 15698 */ 15699 static void 15700 f_type(argvars, rettv) 15701 typval_T *argvars; 15702 typval_T *rettv; 15703 { 15704 int n; 15705 15706 switch (argvars[0].v_type) 15707 { 15708 case VAR_NUMBER: n = 0; break; 15709 case VAR_STRING: n = 1; break; 15710 case VAR_FUNC: n = 2; break; 15711 case VAR_LIST: n = 3; break; 15712 case VAR_DICT: n = 4; break; 15713 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break; 15714 } 15715 rettv->vval.v_number = n; 15716 } 15717 15718 /* 15719 * "values(dict)" function 15720 */ 15721 static void 15722 f_values(argvars, rettv) 15723 typval_T *argvars; 15724 typval_T *rettv; 15725 { 15726 dict_list(argvars, rettv, 1); 15727 } 15728 15729 /* 15730 * "virtcol(string)" function 15731 */ 15732 static void 15733 f_virtcol(argvars, rettv) 15734 typval_T *argvars; 15735 typval_T *rettv; 15736 { 15737 colnr_T vcol = 0; 15738 pos_T *fp; 15739 int fnum = curbuf->b_fnum; 15740 15741 fp = var2fpos(&argvars[0], FALSE, &fnum); 15742 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count 15743 && fnum == curbuf->b_fnum) 15744 { 15745 getvvcol(curwin, fp, NULL, NULL, &vcol); 15746 ++vcol; 15747 } 15748 15749 rettv->vval.v_number = vcol; 15750 } 15751 15752 /* 15753 * "visualmode()" function 15754 */ 15755 /*ARGSUSED*/ 15756 static void 15757 f_visualmode(argvars, rettv) 15758 typval_T *argvars; 15759 typval_T *rettv; 15760 { 15761 #ifdef FEAT_VISUAL 15762 char_u str[2]; 15763 15764 rettv->v_type = VAR_STRING; 15765 str[0] = curbuf->b_visual_mode_eval; 15766 str[1] = NUL; 15767 rettv->vval.v_string = vim_strsave(str); 15768 15769 /* A non-zero number or non-empty string argument: reset mode. */ 15770 if ((argvars[0].v_type == VAR_NUMBER 15771 && argvars[0].vval.v_number != 0) 15772 || (argvars[0].v_type == VAR_STRING 15773 && *get_tv_string(&argvars[0]) != NUL)) 15774 curbuf->b_visual_mode_eval = NUL; 15775 #else 15776 rettv->vval.v_number = 0; /* return anything, it won't work anyway */ 15777 #endif 15778 } 15779 15780 /* 15781 * "winbufnr(nr)" function 15782 */ 15783 static void 15784 f_winbufnr(argvars, rettv) 15785 typval_T *argvars; 15786 typval_T *rettv; 15787 { 15788 win_T *wp; 15789 15790 wp = find_win_by_nr(&argvars[0]); 15791 if (wp == NULL) 15792 rettv->vval.v_number = -1; 15793 else 15794 rettv->vval.v_number = wp->w_buffer->b_fnum; 15795 } 15796 15797 /* 15798 * "wincol()" function 15799 */ 15800 /*ARGSUSED*/ 15801 static void 15802 f_wincol(argvars, rettv) 15803 typval_T *argvars; 15804 typval_T *rettv; 15805 { 15806 validate_cursor(); 15807 rettv->vval.v_number = curwin->w_wcol + 1; 15808 } 15809 15810 /* 15811 * "winheight(nr)" function 15812 */ 15813 static void 15814 f_winheight(argvars, rettv) 15815 typval_T *argvars; 15816 typval_T *rettv; 15817 { 15818 win_T *wp; 15819 15820 wp = find_win_by_nr(&argvars[0]); 15821 if (wp == NULL) 15822 rettv->vval.v_number = -1; 15823 else 15824 rettv->vval.v_number = wp->w_height; 15825 } 15826 15827 /* 15828 * "winline()" function 15829 */ 15830 /*ARGSUSED*/ 15831 static void 15832 f_winline(argvars, rettv) 15833 typval_T *argvars; 15834 typval_T *rettv; 15835 { 15836 validate_cursor(); 15837 rettv->vval.v_number = curwin->w_wrow + 1; 15838 } 15839 15840 /* 15841 * "winnr()" function 15842 */ 15843 /* ARGSUSED */ 15844 static void 15845 f_winnr(argvars, rettv) 15846 typval_T *argvars; 15847 typval_T *rettv; 15848 { 15849 int nr = 1; 15850 15851 #ifdef FEAT_WINDOWS 15852 nr = get_winnr(curtab, &argvars[0]); 15853 #endif 15854 rettv->vval.v_number = nr; 15855 } 15856 15857 /* 15858 * "winrestcmd()" function 15859 */ 15860 /* ARGSUSED */ 15861 static void 15862 f_winrestcmd(argvars, rettv) 15863 typval_T *argvars; 15864 typval_T *rettv; 15865 { 15866 #ifdef FEAT_WINDOWS 15867 win_T *wp; 15868 int winnr = 1; 15869 garray_T ga; 15870 char_u buf[50]; 15871 15872 ga_init2(&ga, (int)sizeof(char), 70); 15873 for (wp = firstwin; wp != NULL; wp = wp->w_next) 15874 { 15875 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height); 15876 ga_concat(&ga, buf); 15877 # ifdef FEAT_VERTSPLIT 15878 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width); 15879 ga_concat(&ga, buf); 15880 # endif 15881 ++winnr; 15882 } 15883 ga_append(&ga, NUL); 15884 15885 rettv->vval.v_string = ga.ga_data; 15886 #else 15887 rettv->vval.v_string = NULL; 15888 #endif 15889 rettv->v_type = VAR_STRING; 15890 } 15891 15892 /* 15893 * "winrestview()" function 15894 */ 15895 /* ARGSUSED */ 15896 static void 15897 f_winrestview(argvars, rettv) 15898 typval_T *argvars; 15899 typval_T *rettv; 15900 { 15901 dict_T *dict; 15902 15903 if (argvars[0].v_type != VAR_DICT 15904 || (dict = argvars[0].vval.v_dict) == NULL) 15905 EMSG(_(e_invarg)); 15906 else 15907 { 15908 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum"); 15909 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col"); 15910 #ifdef FEAT_VIRTUALEDIT 15911 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd"); 15912 #endif 15913 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant"); 15914 curwin->w_set_curswant = FALSE; 15915 15916 curwin->w_topline = get_dict_number(dict, (char_u *)"topline"); 15917 #ifdef FEAT_DIFF 15918 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill"); 15919 #endif 15920 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol"); 15921 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol"); 15922 15923 check_cursor(); 15924 changed_cline_bef_curs(); 15925 invalidate_botline(); 15926 redraw_later(VALID); 15927 15928 if (curwin->w_topline == 0) 15929 curwin->w_topline = 1; 15930 if (curwin->w_topline > curbuf->b_ml.ml_line_count) 15931 curwin->w_topline = curbuf->b_ml.ml_line_count; 15932 #ifdef FEAT_DIFF 15933 check_topfill(curwin, TRUE); 15934 #endif 15935 } 15936 } 15937 15938 /* 15939 * "winsaveview()" function 15940 */ 15941 /* ARGSUSED */ 15942 static void 15943 f_winsaveview(argvars, rettv) 15944 typval_T *argvars; 15945 typval_T *rettv; 15946 { 15947 dict_T *dict; 15948 15949 dict = dict_alloc(); 15950 if (dict == NULL) 15951 return; 15952 rettv->v_type = VAR_DICT; 15953 rettv->vval.v_dict = dict; 15954 ++dict->dv_refcount; 15955 15956 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL); 15957 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL); 15958 #ifdef FEAT_VIRTUALEDIT 15959 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL); 15960 #endif 15961 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL); 15962 15963 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL); 15964 #ifdef FEAT_DIFF 15965 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL); 15966 #endif 15967 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL); 15968 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL); 15969 } 15970 15971 /* 15972 * "winwidth(nr)" function 15973 */ 15974 static void 15975 f_winwidth(argvars, rettv) 15976 typval_T *argvars; 15977 typval_T *rettv; 15978 { 15979 win_T *wp; 15980 15981 wp = find_win_by_nr(&argvars[0]); 15982 if (wp == NULL) 15983 rettv->vval.v_number = -1; 15984 else 15985 #ifdef FEAT_VERTSPLIT 15986 rettv->vval.v_number = wp->w_width; 15987 #else 15988 rettv->vval.v_number = Columns; 15989 #endif 15990 } 15991 15992 /* 15993 * "writefile()" function 15994 */ 15995 static void 15996 f_writefile(argvars, rettv) 15997 typval_T *argvars; 15998 typval_T *rettv; 15999 { 16000 int binary = FALSE; 16001 char_u *fname; 16002 FILE *fd; 16003 listitem_T *li; 16004 char_u *s; 16005 int ret = 0; 16006 int c; 16007 16008 if (argvars[0].v_type != VAR_LIST) 16009 { 16010 EMSG2(_(e_listarg), "writefile()"); 16011 return; 16012 } 16013 if (argvars[0].vval.v_list == NULL) 16014 return; 16015 16016 if (argvars[2].v_type != VAR_UNKNOWN 16017 && STRCMP(get_tv_string(&argvars[2]), "b") == 0) 16018 binary = TRUE; 16019 16020 /* Always open the file in binary mode, library functions have a mind of 16021 * their own about CR-LF conversion. */ 16022 fname = get_tv_string(&argvars[1]); 16023 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL) 16024 { 16025 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname); 16026 ret = -1; 16027 } 16028 else 16029 { 16030 for (li = argvars[0].vval.v_list->lv_first; li != NULL; 16031 li = li->li_next) 16032 { 16033 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s) 16034 { 16035 if (*s == '\n') 16036 c = putc(NUL, fd); 16037 else 16038 c = putc(*s, fd); 16039 if (c == EOF) 16040 { 16041 ret = -1; 16042 break; 16043 } 16044 } 16045 if (!binary || li->li_next != NULL) 16046 if (putc('\n', fd) == EOF) 16047 { 16048 ret = -1; 16049 break; 16050 } 16051 if (ret < 0) 16052 { 16053 EMSG(_(e_write)); 16054 break; 16055 } 16056 } 16057 fclose(fd); 16058 } 16059 16060 rettv->vval.v_number = ret; 16061 } 16062 16063 /* 16064 * Translate a String variable into a position. 16065 * Returns NULL when there is an error. 16066 */ 16067 static pos_T * 16068 var2fpos(varp, lnum, fnum) 16069 typval_T *varp; 16070 int lnum; /* TRUE when $ is last line */ 16071 int *fnum; /* set to fnum for '0, 'A, etc. */ 16072 { 16073 char_u *name; 16074 static pos_T pos; 16075 pos_T *pp; 16076 16077 /* Argument can be [lnum, col, coladd]. */ 16078 if (varp->v_type == VAR_LIST) 16079 { 16080 list_T *l; 16081 int len; 16082 int error = FALSE; 16083 16084 l = varp->vval.v_list; 16085 if (l == NULL) 16086 return NULL; 16087 16088 /* Get the line number */ 16089 pos.lnum = list_find_nr(l, 0L, &error); 16090 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count) 16091 return NULL; /* invalid line number */ 16092 16093 /* Get the column number */ 16094 pos.col = list_find_nr(l, 1L, &error); 16095 if (error) 16096 return NULL; 16097 len = (long)STRLEN(ml_get(pos.lnum)); 16098 /* Accept a position up to the NUL after the line. */ 16099 if (pos.col <= 0 || (int)pos.col > len + 1) 16100 return NULL; /* invalid column number */ 16101 --pos.col; 16102 16103 #ifdef FEAT_VIRTUALEDIT 16104 /* Get the virtual offset. Defaults to zero. */ 16105 pos.coladd = list_find_nr(l, 2L, &error); 16106 if (error) 16107 pos.coladd = 0; 16108 #endif 16109 16110 return &pos; 16111 } 16112 16113 name = get_tv_string_chk(varp); 16114 if (name == NULL) 16115 return NULL; 16116 if (name[0] == '.') /* cursor */ 16117 return &curwin->w_cursor; 16118 if (name[0] == '\'') /* mark */ 16119 { 16120 pp = getmark_fnum(name[1], FALSE, fnum); 16121 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0) 16122 return NULL; 16123 return pp; 16124 } 16125 16126 #ifdef FEAT_VIRTUALEDIT 16127 pos.coladd = 0; 16128 #endif 16129 16130 if (name[0] == 'w' && lnum) 16131 { 16132 pos.col = 0; 16133 if (name[1] == '0') /* "w0": first visible line */ 16134 { 16135 update_topline(); 16136 pos.lnum = curwin->w_topline; 16137 return &pos; 16138 } 16139 else if (name[1] == '$') /* "w$": last visible line */ 16140 { 16141 validate_botline(); 16142 pos.lnum = curwin->w_botline - 1; 16143 return &pos; 16144 } 16145 } 16146 else if (name[0] == '$') /* last column or line */ 16147 { 16148 if (lnum) 16149 { 16150 pos.lnum = curbuf->b_ml.ml_line_count; 16151 pos.col = 0; 16152 } 16153 else 16154 { 16155 pos.lnum = curwin->w_cursor.lnum; 16156 pos.col = (colnr_T)STRLEN(ml_get_curline()); 16157 } 16158 return &pos; 16159 } 16160 return NULL; 16161 } 16162 16163 /* 16164 * Convert list in "arg" into a position and optional file number. 16165 * When "fnump" is NULL there is no file number, only 3 items. 16166 * Note that the column is passed on as-is, the caller may want to decrement 16167 * it to use 1 for the first column. 16168 * Return FAIL when conversion is not possible, doesn't check the position for 16169 * validity. 16170 */ 16171 static int 16172 list2fpos(arg, posp, fnump) 16173 typval_T *arg; 16174 pos_T *posp; 16175 int *fnump; 16176 { 16177 list_T *l = arg->vval.v_list; 16178 long i = 0; 16179 long n; 16180 16181 /* List must be: [fnum, lnum, col, coladd] */ 16182 if (arg->v_type != VAR_LIST || l == NULL 16183 || l->lv_len != (fnump == NULL ? 3 : 4)) 16184 return FAIL; 16185 16186 if (fnump != NULL) 16187 { 16188 n = list_find_nr(l, i++, NULL); /* fnum */ 16189 if (n < 0) 16190 return FAIL; 16191 if (n == 0) 16192 n = curbuf->b_fnum; /* current buffer */ 16193 *fnump = n; 16194 } 16195 16196 n = list_find_nr(l, i++, NULL); /* lnum */ 16197 if (n < 0) 16198 return FAIL; 16199 posp->lnum = n; 16200 16201 n = list_find_nr(l, i++, NULL); /* col */ 16202 if (n < 0) 16203 return FAIL; 16204 posp->col = n; 16205 16206 #ifdef FEAT_VIRTUALEDIT 16207 n = list_find_nr(l, i, NULL); 16208 if (n < 0) 16209 return FAIL; 16210 posp->coladd = n; 16211 #endif 16212 16213 return OK; 16214 } 16215 16216 /* 16217 * Get the length of an environment variable name. 16218 * Advance "arg" to the first character after the name. 16219 * Return 0 for error. 16220 */ 16221 static int 16222 get_env_len(arg) 16223 char_u **arg; 16224 { 16225 char_u *p; 16226 int len; 16227 16228 for (p = *arg; vim_isIDc(*p); ++p) 16229 ; 16230 if (p == *arg) /* no name found */ 16231 return 0; 16232 16233 len = (int)(p - *arg); 16234 *arg = p; 16235 return len; 16236 } 16237 16238 /* 16239 * Get the length of the name of a function or internal variable. 16240 * "arg" is advanced to the first non-white character after the name. 16241 * Return 0 if something is wrong. 16242 */ 16243 static int 16244 get_id_len(arg) 16245 char_u **arg; 16246 { 16247 char_u *p; 16248 int len; 16249 16250 /* Find the end of the name. */ 16251 for (p = *arg; eval_isnamec(*p); ++p) 16252 ; 16253 if (p == *arg) /* no name found */ 16254 return 0; 16255 16256 len = (int)(p - *arg); 16257 *arg = skipwhite(p); 16258 16259 return len; 16260 } 16261 16262 /* 16263 * Get the length of the name of a variable or function. 16264 * Only the name is recognized, does not handle ".key" or "[idx]". 16265 * "arg" is advanced to the first non-white character after the name. 16266 * Return -1 if curly braces expansion failed. 16267 * Return 0 if something else is wrong. 16268 * If the name contains 'magic' {}'s, expand them and return the 16269 * expanded name in an allocated string via 'alias' - caller must free. 16270 */ 16271 static int 16272 get_name_len(arg, alias, evaluate, verbose) 16273 char_u **arg; 16274 char_u **alias; 16275 int evaluate; 16276 int verbose; 16277 { 16278 int len; 16279 char_u *p; 16280 char_u *expr_start; 16281 char_u *expr_end; 16282 16283 *alias = NULL; /* default to no alias */ 16284 16285 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA 16286 && (*arg)[2] == (int)KE_SNR) 16287 { 16288 /* hard coded <SNR>, already translated */ 16289 *arg += 3; 16290 return get_id_len(arg) + 3; 16291 } 16292 len = eval_fname_script(*arg); 16293 if (len > 0) 16294 { 16295 /* literal "<SID>", "s:" or "<SNR>" */ 16296 *arg += len; 16297 } 16298 16299 /* 16300 * Find the end of the name; check for {} construction. 16301 */ 16302 p = find_name_end(*arg, &expr_start, &expr_end, 16303 len > 0 ? 0 : FNE_CHECK_START); 16304 if (expr_start != NULL) 16305 { 16306 char_u *temp_string; 16307 16308 if (!evaluate) 16309 { 16310 len += (int)(p - *arg); 16311 *arg = skipwhite(p); 16312 return len; 16313 } 16314 16315 /* 16316 * Include any <SID> etc in the expanded string: 16317 * Thus the -len here. 16318 */ 16319 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p); 16320 if (temp_string == NULL) 16321 return -1; 16322 *alias = temp_string; 16323 *arg = skipwhite(p); 16324 return (int)STRLEN(temp_string); 16325 } 16326 16327 len += get_id_len(arg); 16328 if (len == 0 && verbose) 16329 EMSG2(_(e_invexpr2), *arg); 16330 16331 return len; 16332 } 16333 16334 /* 16335 * Find the end of a variable or function name, taking care of magic braces. 16336 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the 16337 * start and end of the first magic braces item. 16338 * "flags" can have FNE_INCL_BR and FNE_CHECK_START. 16339 * Return a pointer to just after the name. Equal to "arg" if there is no 16340 * valid name. 16341 */ 16342 static char_u * 16343 find_name_end(arg, expr_start, expr_end, flags) 16344 char_u *arg; 16345 char_u **expr_start; 16346 char_u **expr_end; 16347 int flags; 16348 { 16349 int mb_nest = 0; 16350 int br_nest = 0; 16351 char_u *p; 16352 16353 if (expr_start != NULL) 16354 { 16355 *expr_start = NULL; 16356 *expr_end = NULL; 16357 } 16358 16359 /* Quick check for valid starting character. */ 16360 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{') 16361 return arg; 16362 16363 for (p = arg; *p != NUL 16364 && (eval_isnamec(*p) 16365 || *p == '{' 16366 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.')) 16367 || mb_nest != 0 16368 || br_nest != 0); mb_ptr_adv(p)) 16369 { 16370 if (*p == '\'') 16371 { 16372 /* skip over 'string' to avoid counting [ and ] inside it. */ 16373 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p)) 16374 ; 16375 if (*p == NUL) 16376 break; 16377 } 16378 else if (*p == '"') 16379 { 16380 /* skip over "str\"ing" to avoid counting [ and ] inside it. */ 16381 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p)) 16382 if (*p == '\\' && p[1] != NUL) 16383 ++p; 16384 if (*p == NUL) 16385 break; 16386 } 16387 16388 if (mb_nest == 0) 16389 { 16390 if (*p == '[') 16391 ++br_nest; 16392 else if (*p == ']') 16393 --br_nest; 16394 } 16395 16396 if (br_nest == 0) 16397 { 16398 if (*p == '{') 16399 { 16400 mb_nest++; 16401 if (expr_start != NULL && *expr_start == NULL) 16402 *expr_start = p; 16403 } 16404 else if (*p == '}') 16405 { 16406 mb_nest--; 16407 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL) 16408 *expr_end = p; 16409 } 16410 } 16411 } 16412 16413 return p; 16414 } 16415 16416 /* 16417 * Expands out the 'magic' {}'s in a variable/function name. 16418 * Note that this can call itself recursively, to deal with 16419 * constructs like foo{bar}{baz}{bam} 16420 * The four pointer arguments point to "foo{expre}ss{ion}bar" 16421 * "in_start" ^ 16422 * "expr_start" ^ 16423 * "expr_end" ^ 16424 * "in_end" ^ 16425 * 16426 * Returns a new allocated string, which the caller must free. 16427 * Returns NULL for failure. 16428 */ 16429 static char_u * 16430 make_expanded_name(in_start, expr_start, expr_end, in_end) 16431 char_u *in_start; 16432 char_u *expr_start; 16433 char_u *expr_end; 16434 char_u *in_end; 16435 { 16436 char_u c1; 16437 char_u *retval = NULL; 16438 char_u *temp_result; 16439 char_u *nextcmd = NULL; 16440 16441 if (expr_end == NULL || in_end == NULL) 16442 return NULL; 16443 *expr_start = NUL; 16444 *expr_end = NUL; 16445 c1 = *in_end; 16446 *in_end = NUL; 16447 16448 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE); 16449 if (temp_result != NULL && nextcmd == NULL) 16450 { 16451 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start) 16452 + (in_end - expr_end) + 1)); 16453 if (retval != NULL) 16454 { 16455 STRCPY(retval, in_start); 16456 STRCAT(retval, temp_result); 16457 STRCAT(retval, expr_end + 1); 16458 } 16459 } 16460 vim_free(temp_result); 16461 16462 *in_end = c1; /* put char back for error messages */ 16463 *expr_start = '{'; 16464 *expr_end = '}'; 16465 16466 if (retval != NULL) 16467 { 16468 temp_result = find_name_end(retval, &expr_start, &expr_end, 0); 16469 if (expr_start != NULL) 16470 { 16471 /* Further expansion! */ 16472 temp_result = make_expanded_name(retval, expr_start, 16473 expr_end, temp_result); 16474 vim_free(retval); 16475 retval = temp_result; 16476 } 16477 } 16478 16479 return retval; 16480 } 16481 16482 /* 16483 * Return TRUE if character "c" can be used in a variable or function name. 16484 * Does not include '{' or '}' for magic braces. 16485 */ 16486 static int 16487 eval_isnamec(c) 16488 int c; 16489 { 16490 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR); 16491 } 16492 16493 /* 16494 * Return TRUE if character "c" can be used as the first character in a 16495 * variable or function name (excluding '{' and '}'). 16496 */ 16497 static int 16498 eval_isnamec1(c) 16499 int c; 16500 { 16501 return (ASCII_ISALPHA(c) || c == '_'); 16502 } 16503 16504 /* 16505 * Set number v: variable to "val". 16506 */ 16507 void 16508 set_vim_var_nr(idx, val) 16509 int idx; 16510 long val; 16511 { 16512 vimvars[idx].vv_nr = val; 16513 } 16514 16515 /* 16516 * Get number v: variable value. 16517 */ 16518 long 16519 get_vim_var_nr(idx) 16520 int idx; 16521 { 16522 return vimvars[idx].vv_nr; 16523 } 16524 16525 #if defined(FEAT_AUTOCMD) || defined(PROTO) 16526 /* 16527 * Get string v: variable value. Uses a static buffer, can only be used once. 16528 */ 16529 char_u * 16530 get_vim_var_str(idx) 16531 int idx; 16532 { 16533 return get_tv_string(&vimvars[idx].vv_tv); 16534 } 16535 #endif 16536 16537 /* 16538 * Set v:count, v:count1 and v:prevcount. 16539 */ 16540 void 16541 set_vcount(count, count1) 16542 long count; 16543 long count1; 16544 { 16545 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr; 16546 vimvars[VV_COUNT].vv_nr = count; 16547 vimvars[VV_COUNT1].vv_nr = count1; 16548 } 16549 16550 /* 16551 * Set string v: variable to a copy of "val". 16552 */ 16553 void 16554 set_vim_var_string(idx, val, len) 16555 int idx; 16556 char_u *val; 16557 int len; /* length of "val" to use or -1 (whole string) */ 16558 { 16559 /* Need to do this (at least) once, since we can't initialize a union. 16560 * Will always be invoked when "v:progname" is set. */ 16561 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100; 16562 16563 vim_free(vimvars[idx].vv_str); 16564 if (val == NULL) 16565 vimvars[idx].vv_str = NULL; 16566 else if (len == -1) 16567 vimvars[idx].vv_str = vim_strsave(val); 16568 else 16569 vimvars[idx].vv_str = vim_strnsave(val, len); 16570 } 16571 16572 /* 16573 * Set v:register if needed. 16574 */ 16575 void 16576 set_reg_var(c) 16577 int c; 16578 { 16579 char_u regname; 16580 16581 if (c == 0 || c == ' ') 16582 regname = '"'; 16583 else 16584 regname = c; 16585 /* Avoid free/alloc when the value is already right. */ 16586 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c) 16587 set_vim_var_string(VV_REG, ®name, 1); 16588 } 16589 16590 /* 16591 * Get or set v:exception. If "oldval" == NULL, return the current value. 16592 * Otherwise, restore the value to "oldval" and return NULL. 16593 * Must always be called in pairs to save and restore v:exception! Does not 16594 * take care of memory allocations. 16595 */ 16596 char_u * 16597 v_exception(oldval) 16598 char_u *oldval; 16599 { 16600 if (oldval == NULL) 16601 return vimvars[VV_EXCEPTION].vv_str; 16602 16603 vimvars[VV_EXCEPTION].vv_str = oldval; 16604 return NULL; 16605 } 16606 16607 /* 16608 * Get or set v:throwpoint. If "oldval" == NULL, return the current value. 16609 * Otherwise, restore the value to "oldval" and return NULL. 16610 * Must always be called in pairs to save and restore v:throwpoint! Does not 16611 * take care of memory allocations. 16612 */ 16613 char_u * 16614 v_throwpoint(oldval) 16615 char_u *oldval; 16616 { 16617 if (oldval == NULL) 16618 return vimvars[VV_THROWPOINT].vv_str; 16619 16620 vimvars[VV_THROWPOINT].vv_str = oldval; 16621 return NULL; 16622 } 16623 16624 #if defined(FEAT_AUTOCMD) || defined(PROTO) 16625 /* 16626 * Set v:cmdarg. 16627 * If "eap" != NULL, use "eap" to generate the value and return the old value. 16628 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL. 16629 * Must always be called in pairs! 16630 */ 16631 char_u * 16632 set_cmdarg(eap, oldarg) 16633 exarg_T *eap; 16634 char_u *oldarg; 16635 { 16636 char_u *oldval; 16637 char_u *newval; 16638 unsigned len; 16639 16640 oldval = vimvars[VV_CMDARG].vv_str; 16641 if (eap == NULL) 16642 { 16643 vim_free(oldval); 16644 vimvars[VV_CMDARG].vv_str = oldarg; 16645 return NULL; 16646 } 16647 16648 if (eap->force_bin == FORCE_BIN) 16649 len = 6; 16650 else if (eap->force_bin == FORCE_NOBIN) 16651 len = 8; 16652 else 16653 len = 0; 16654 if (eap->force_ff != 0) 16655 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6; 16656 # ifdef FEAT_MBYTE 16657 if (eap->force_enc != 0) 16658 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7; 16659 if (eap->bad_char != 0) 16660 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7; 16661 # endif 16662 16663 newval = alloc(len + 1); 16664 if (newval == NULL) 16665 return NULL; 16666 16667 if (eap->force_bin == FORCE_BIN) 16668 sprintf((char *)newval, " ++bin"); 16669 else if (eap->force_bin == FORCE_NOBIN) 16670 sprintf((char *)newval, " ++nobin"); 16671 else 16672 *newval = NUL; 16673 if (eap->force_ff != 0) 16674 sprintf((char *)newval + STRLEN(newval), " ++ff=%s", 16675 eap->cmd + eap->force_ff); 16676 # ifdef FEAT_MBYTE 16677 if (eap->force_enc != 0) 16678 sprintf((char *)newval + STRLEN(newval), " ++enc=%s", 16679 eap->cmd + eap->force_enc); 16680 if (eap->bad_char != 0) 16681 sprintf((char *)newval + STRLEN(newval), " ++bad=%s", 16682 eap->cmd + eap->bad_char); 16683 # endif 16684 vimvars[VV_CMDARG].vv_str = newval; 16685 return oldval; 16686 } 16687 #endif 16688 16689 /* 16690 * Get the value of internal variable "name". 16691 * Return OK or FAIL. 16692 */ 16693 static int 16694 get_var_tv(name, len, rettv, verbose) 16695 char_u *name; 16696 int len; /* length of "name" */ 16697 typval_T *rettv; /* NULL when only checking existence */ 16698 int verbose; /* may give error message */ 16699 { 16700 int ret = OK; 16701 typval_T *tv = NULL; 16702 typval_T atv; 16703 dictitem_T *v; 16704 int cc; 16705 16706 /* truncate the name, so that we can use strcmp() */ 16707 cc = name[len]; 16708 name[len] = NUL; 16709 16710 /* 16711 * Check for "b:changedtick". 16712 */ 16713 if (STRCMP(name, "b:changedtick") == 0) 16714 { 16715 atv.v_type = VAR_NUMBER; 16716 atv.vval.v_number = curbuf->b_changedtick; 16717 tv = &atv; 16718 } 16719 16720 /* 16721 * Check for user-defined variables. 16722 */ 16723 else 16724 { 16725 v = find_var(name, NULL); 16726 if (v != NULL) 16727 tv = &v->di_tv; 16728 } 16729 16730 if (tv == NULL) 16731 { 16732 if (rettv != NULL && verbose) 16733 EMSG2(_(e_undefvar), name); 16734 ret = FAIL; 16735 } 16736 else if (rettv != NULL) 16737 copy_tv(tv, rettv); 16738 16739 name[len] = cc; 16740 16741 return ret; 16742 } 16743 16744 /* 16745 * Handle expr[expr], expr[expr:expr] subscript and .name lookup. 16746 * Also handle function call with Funcref variable: func(expr) 16747 * Can all be combined: dict.func(expr)[idx]['func'](expr) 16748 */ 16749 static int 16750 handle_subscript(arg, rettv, evaluate, verbose) 16751 char_u **arg; 16752 typval_T *rettv; 16753 int evaluate; /* do more than finding the end */ 16754 int verbose; /* give error messages */ 16755 { 16756 int ret = OK; 16757 dict_T *selfdict = NULL; 16758 char_u *s; 16759 int len; 16760 typval_T functv; 16761 16762 while (ret == OK 16763 && (**arg == '[' 16764 || (**arg == '.' && rettv->v_type == VAR_DICT) 16765 || (**arg == '(' && rettv->v_type == VAR_FUNC)) 16766 && !vim_iswhite(*(*arg - 1))) 16767 { 16768 if (**arg == '(') 16769 { 16770 /* need to copy the funcref so that we can clear rettv */ 16771 functv = *rettv; 16772 rettv->v_type = VAR_UNKNOWN; 16773 16774 /* Invoke the function. Recursive! */ 16775 s = functv.vval.v_string; 16776 ret = get_func_tv(s, STRLEN(s), rettv, arg, 16777 curwin->w_cursor.lnum, curwin->w_cursor.lnum, 16778 &len, evaluate, selfdict); 16779 16780 /* Clear the funcref afterwards, so that deleting it while 16781 * evaluating the arguments is possible (see test55). */ 16782 clear_tv(&functv); 16783 16784 /* Stop the expression evaluation when immediately aborting on 16785 * error, or when an interrupt occurred or an exception was thrown 16786 * but not caught. */ 16787 if (aborting()) 16788 { 16789 if (ret == OK) 16790 clear_tv(rettv); 16791 ret = FAIL; 16792 } 16793 dict_unref(selfdict); 16794 selfdict = NULL; 16795 } 16796 else /* **arg == '[' || **arg == '.' */ 16797 { 16798 dict_unref(selfdict); 16799 if (rettv->v_type == VAR_DICT) 16800 { 16801 selfdict = rettv->vval.v_dict; 16802 if (selfdict != NULL) 16803 ++selfdict->dv_refcount; 16804 } 16805 else 16806 selfdict = NULL; 16807 if (eval_index(arg, rettv, evaluate, verbose) == FAIL) 16808 { 16809 clear_tv(rettv); 16810 ret = FAIL; 16811 } 16812 } 16813 } 16814 dict_unref(selfdict); 16815 return ret; 16816 } 16817 16818 /* 16819 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL 16820 * value). 16821 */ 16822 static typval_T * 16823 alloc_tv() 16824 { 16825 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T)); 16826 } 16827 16828 /* 16829 * Allocate memory for a variable type-value, and assign a string to it. 16830 * The string "s" must have been allocated, it is consumed. 16831 * Return NULL for out of memory, the variable otherwise. 16832 */ 16833 static typval_T * 16834 alloc_string_tv(s) 16835 char_u *s; 16836 { 16837 typval_T *rettv; 16838 16839 rettv = alloc_tv(); 16840 if (rettv != NULL) 16841 { 16842 rettv->v_type = VAR_STRING; 16843 rettv->vval.v_string = s; 16844 } 16845 else 16846 vim_free(s); 16847 return rettv; 16848 } 16849 16850 /* 16851 * Free the memory for a variable type-value. 16852 */ 16853 void 16854 free_tv(varp) 16855 typval_T *varp; 16856 { 16857 if (varp != NULL) 16858 { 16859 switch (varp->v_type) 16860 { 16861 case VAR_FUNC: 16862 func_unref(varp->vval.v_string); 16863 /*FALLTHROUGH*/ 16864 case VAR_STRING: 16865 vim_free(varp->vval.v_string); 16866 break; 16867 case VAR_LIST: 16868 list_unref(varp->vval.v_list); 16869 break; 16870 case VAR_DICT: 16871 dict_unref(varp->vval.v_dict); 16872 break; 16873 case VAR_NUMBER: 16874 case VAR_UNKNOWN: 16875 break; 16876 default: 16877 EMSG2(_(e_intern2), "free_tv()"); 16878 break; 16879 } 16880 vim_free(varp); 16881 } 16882 } 16883 16884 /* 16885 * Free the memory for a variable value and set the value to NULL or 0. 16886 */ 16887 void 16888 clear_tv(varp) 16889 typval_T *varp; 16890 { 16891 if (varp != NULL) 16892 { 16893 switch (varp->v_type) 16894 { 16895 case VAR_FUNC: 16896 func_unref(varp->vval.v_string); 16897 /*FALLTHROUGH*/ 16898 case VAR_STRING: 16899 vim_free(varp->vval.v_string); 16900 varp->vval.v_string = NULL; 16901 break; 16902 case VAR_LIST: 16903 list_unref(varp->vval.v_list); 16904 varp->vval.v_list = NULL; 16905 break; 16906 case VAR_DICT: 16907 dict_unref(varp->vval.v_dict); 16908 varp->vval.v_dict = NULL; 16909 break; 16910 case VAR_NUMBER: 16911 varp->vval.v_number = 0; 16912 break; 16913 case VAR_UNKNOWN: 16914 break; 16915 default: 16916 EMSG2(_(e_intern2), "clear_tv()"); 16917 } 16918 varp->v_lock = 0; 16919 } 16920 } 16921 16922 /* 16923 * Set the value of a variable to NULL without freeing items. 16924 */ 16925 static void 16926 init_tv(varp) 16927 typval_T *varp; 16928 { 16929 if (varp != NULL) 16930 vim_memset(varp, 0, sizeof(typval_T)); 16931 } 16932 16933 /* 16934 * Get the number value of a variable. 16935 * If it is a String variable, uses vim_str2nr(). 16936 * For incompatible types, return 0. 16937 * get_tv_number_chk() is similar to get_tv_number(), but informs the 16938 * caller of incompatible types: it sets *denote to TRUE if "denote" 16939 * is not NULL or returns -1 otherwise. 16940 */ 16941 static long 16942 get_tv_number(varp) 16943 typval_T *varp; 16944 { 16945 int error = FALSE; 16946 16947 return get_tv_number_chk(varp, &error); /* return 0L on error */ 16948 } 16949 16950 long 16951 get_tv_number_chk(varp, denote) 16952 typval_T *varp; 16953 int *denote; 16954 { 16955 long n = 0L; 16956 16957 switch (varp->v_type) 16958 { 16959 case VAR_NUMBER: 16960 return (long)(varp->vval.v_number); 16961 case VAR_FUNC: 16962 EMSG(_("E703: Using a Funcref as a number")); 16963 break; 16964 case VAR_STRING: 16965 if (varp->vval.v_string != NULL) 16966 vim_str2nr(varp->vval.v_string, NULL, NULL, 16967 TRUE, TRUE, &n, NULL); 16968 return n; 16969 case VAR_LIST: 16970 EMSG(_("E745: Using a List as a number")); 16971 break; 16972 case VAR_DICT: 16973 EMSG(_("E728: Using a Dictionary as a number")); 16974 break; 16975 default: 16976 EMSG2(_(e_intern2), "get_tv_number()"); 16977 break; 16978 } 16979 if (denote == NULL) /* useful for values that must be unsigned */ 16980 n = -1; 16981 else 16982 *denote = TRUE; 16983 return n; 16984 } 16985 16986 /* 16987 * Get the lnum from the first argument. 16988 * Also accepts ".", "$", etc., but that only works for the current buffer. 16989 * Returns -1 on error. 16990 */ 16991 static linenr_T 16992 get_tv_lnum(argvars) 16993 typval_T *argvars; 16994 { 16995 typval_T rettv; 16996 linenr_T lnum; 16997 16998 lnum = get_tv_number_chk(&argvars[0], NULL); 16999 if (lnum == 0) /* no valid number, try using line() */ 17000 { 17001 rettv.v_type = VAR_NUMBER; 17002 f_line(argvars, &rettv); 17003 lnum = rettv.vval.v_number; 17004 clear_tv(&rettv); 17005 } 17006 return lnum; 17007 } 17008 17009 /* 17010 * Get the lnum from the first argument. 17011 * Also accepts "$", then "buf" is used. 17012 * Returns 0 on error. 17013 */ 17014 static linenr_T 17015 get_tv_lnum_buf(argvars, buf) 17016 typval_T *argvars; 17017 buf_T *buf; 17018 { 17019 if (argvars[0].v_type == VAR_STRING 17020 && argvars[0].vval.v_string != NULL 17021 && argvars[0].vval.v_string[0] == '$' 17022 && buf != NULL) 17023 return buf->b_ml.ml_line_count; 17024 return get_tv_number_chk(&argvars[0], NULL); 17025 } 17026 17027 /* 17028 * Get the string value of a variable. 17029 * If it is a Number variable, the number is converted into a string. 17030 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE! 17031 * get_tv_string_buf() uses a given buffer. 17032 * If the String variable has never been set, return an empty string. 17033 * Never returns NULL; 17034 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return 17035 * NULL on error. 17036 */ 17037 static char_u * 17038 get_tv_string(varp) 17039 typval_T *varp; 17040 { 17041 static char_u mybuf[NUMBUFLEN]; 17042 17043 return get_tv_string_buf(varp, mybuf); 17044 } 17045 17046 static char_u * 17047 get_tv_string_buf(varp, buf) 17048 typval_T *varp; 17049 char_u *buf; 17050 { 17051 char_u *res = get_tv_string_buf_chk(varp, buf); 17052 17053 return res != NULL ? res : (char_u *)""; 17054 } 17055 17056 char_u * 17057 get_tv_string_chk(varp) 17058 typval_T *varp; 17059 { 17060 static char_u mybuf[NUMBUFLEN]; 17061 17062 return get_tv_string_buf_chk(varp, mybuf); 17063 } 17064 17065 static char_u * 17066 get_tv_string_buf_chk(varp, buf) 17067 typval_T *varp; 17068 char_u *buf; 17069 { 17070 switch (varp->v_type) 17071 { 17072 case VAR_NUMBER: 17073 sprintf((char *)buf, "%ld", (long)varp->vval.v_number); 17074 return buf; 17075 case VAR_FUNC: 17076 EMSG(_("E729: using Funcref as a String")); 17077 break; 17078 case VAR_LIST: 17079 EMSG(_("E730: using List as a String")); 17080 break; 17081 case VAR_DICT: 17082 EMSG(_("E731: using Dictionary as a String")); 17083 break; 17084 case VAR_STRING: 17085 if (varp->vval.v_string != NULL) 17086 return varp->vval.v_string; 17087 return (char_u *)""; 17088 default: 17089 EMSG2(_(e_intern2), "get_tv_string_buf()"); 17090 break; 17091 } 17092 return NULL; 17093 } 17094 17095 /* 17096 * Find variable "name" in the list of variables. 17097 * Return a pointer to it if found, NULL if not found. 17098 * Careful: "a:0" variables don't have a name. 17099 * When "htp" is not NULL we are writing to the variable, set "htp" to the 17100 * hashtab_T used. 17101 */ 17102 static dictitem_T * 17103 find_var(name, htp) 17104 char_u *name; 17105 hashtab_T **htp; 17106 { 17107 char_u *varname; 17108 hashtab_T *ht; 17109 17110 ht = find_var_ht(name, &varname); 17111 if (htp != NULL) 17112 *htp = ht; 17113 if (ht == NULL) 17114 return NULL; 17115 return find_var_in_ht(ht, varname, htp != NULL); 17116 } 17117 17118 /* 17119 * Find variable "varname" in hashtab "ht". 17120 * Returns NULL if not found. 17121 */ 17122 static dictitem_T * 17123 find_var_in_ht(ht, varname, writing) 17124 hashtab_T *ht; 17125 char_u *varname; 17126 int writing; 17127 { 17128 hashitem_T *hi; 17129 17130 if (*varname == NUL) 17131 { 17132 /* Must be something like "s:", otherwise "ht" would be NULL. */ 17133 switch (varname[-2]) 17134 { 17135 case 's': return &SCRIPT_SV(current_SID).sv_var; 17136 case 'g': return &globvars_var; 17137 case 'v': return &vimvars_var; 17138 case 'b': return &curbuf->b_bufvar; 17139 case 'w': return &curwin->w_winvar; 17140 case 'l': return current_funccal == NULL 17141 ? NULL : ¤t_funccal->l_vars_var; 17142 case 'a': return current_funccal == NULL 17143 ? NULL : ¤t_funccal->l_avars_var; 17144 } 17145 return NULL; 17146 } 17147 17148 hi = hash_find(ht, varname); 17149 if (HASHITEM_EMPTY(hi)) 17150 { 17151 /* For global variables we may try auto-loading the script. If it 17152 * worked find the variable again. Don't auto-load a script if it was 17153 * loaded already, otherwise it would be loaded every time when 17154 * checking if a function name is a Funcref variable. */ 17155 if (ht == &globvarht && !writing 17156 && script_autoload(varname, FALSE) && !aborting()) 17157 hi = hash_find(ht, varname); 17158 if (HASHITEM_EMPTY(hi)) 17159 return NULL; 17160 } 17161 return HI2DI(hi); 17162 } 17163 17164 /* 17165 * Find the hashtab used for a variable name. 17166 * Set "varname" to the start of name without ':'. 17167 */ 17168 static hashtab_T * 17169 find_var_ht(name, varname) 17170 char_u *name; 17171 char_u **varname; 17172 { 17173 hashitem_T *hi; 17174 17175 if (name[1] != ':') 17176 { 17177 /* The name must not start with a colon or #. */ 17178 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR) 17179 return NULL; 17180 *varname = name; 17181 17182 /* "version" is "v:version" in all scopes */ 17183 hi = hash_find(&compat_hashtab, name); 17184 if (!HASHITEM_EMPTY(hi)) 17185 return &compat_hashtab; 17186 17187 if (current_funccal == NULL) 17188 return &globvarht; /* global variable */ 17189 return ¤t_funccal->l_vars.dv_hashtab; /* l: variable */ 17190 } 17191 *varname = name + 2; 17192 if (*name == 'g') /* global variable */ 17193 return &globvarht; 17194 /* There must be no ':' or '#' in the rest of the name, unless g: is used 17195 */ 17196 if (vim_strchr(name + 2, ':') != NULL 17197 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL) 17198 return NULL; 17199 if (*name == 'b') /* buffer variable */ 17200 return &curbuf->b_vars.dv_hashtab; 17201 if (*name == 'w') /* window variable */ 17202 return &curwin->w_vars.dv_hashtab; 17203 if (*name == 'v') /* v: variable */ 17204 return &vimvarht; 17205 if (*name == 'a' && current_funccal != NULL) /* function argument */ 17206 return ¤t_funccal->l_avars.dv_hashtab; 17207 if (*name == 'l' && current_funccal != NULL) /* local function variable */ 17208 return ¤t_funccal->l_vars.dv_hashtab; 17209 if (*name == 's' /* script variable */ 17210 && current_SID > 0 && current_SID <= ga_scripts.ga_len) 17211 return &SCRIPT_VARS(current_SID); 17212 return NULL; 17213 } 17214 17215 /* 17216 * Get the string value of a (global/local) variable. 17217 * Returns NULL when it doesn't exist. 17218 */ 17219 char_u * 17220 get_var_value(name) 17221 char_u *name; 17222 { 17223 dictitem_T *v; 17224 17225 v = find_var(name, NULL); 17226 if (v == NULL) 17227 return NULL; 17228 return get_tv_string(&v->di_tv); 17229 } 17230 17231 /* 17232 * Allocate a new hashtab for a sourced script. It will be used while 17233 * sourcing this script and when executing functions defined in the script. 17234 */ 17235 void 17236 new_script_vars(id) 17237 scid_T id; 17238 { 17239 int i; 17240 hashtab_T *ht; 17241 scriptvar_T *sv; 17242 17243 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK) 17244 { 17245 /* Re-allocating ga_data means that an ht_array pointing to 17246 * ht_smallarray becomes invalid. We can recognize this: ht_mask is 17247 * at its init value. Also reset "v_dict", it's always the same. */ 17248 for (i = 1; i <= ga_scripts.ga_len; ++i) 17249 { 17250 ht = &SCRIPT_VARS(i); 17251 if (ht->ht_mask == HT_INIT_SIZE - 1) 17252 ht->ht_array = ht->ht_smallarray; 17253 sv = &SCRIPT_SV(i); 17254 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict; 17255 } 17256 17257 while (ga_scripts.ga_len < id) 17258 { 17259 sv = &SCRIPT_SV(ga_scripts.ga_len + 1); 17260 init_var_dict(&sv->sv_dict, &sv->sv_var); 17261 ++ga_scripts.ga_len; 17262 } 17263 } 17264 } 17265 17266 /* 17267 * Initialize dictionary "dict" as a scope and set variable "dict_var" to 17268 * point to it. 17269 */ 17270 void 17271 init_var_dict(dict, dict_var) 17272 dict_T *dict; 17273 dictitem_T *dict_var; 17274 { 17275 hash_init(&dict->dv_hashtab); 17276 dict->dv_refcount = 99999; 17277 dict_var->di_tv.vval.v_dict = dict; 17278 dict_var->di_tv.v_type = VAR_DICT; 17279 dict_var->di_tv.v_lock = VAR_FIXED; 17280 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 17281 dict_var->di_key[0] = NUL; 17282 } 17283 17284 /* 17285 * Clean up a list of internal variables. 17286 * Frees all allocated variables and the value they contain. 17287 * Clears hashtab "ht", does not free it. 17288 */ 17289 void 17290 vars_clear(ht) 17291 hashtab_T *ht; 17292 { 17293 vars_clear_ext(ht, TRUE); 17294 } 17295 17296 /* 17297 * Like vars_clear(), but only free the value if "free_val" is TRUE. 17298 */ 17299 static void 17300 vars_clear_ext(ht, free_val) 17301 hashtab_T *ht; 17302 int free_val; 17303 { 17304 int todo; 17305 hashitem_T *hi; 17306 dictitem_T *v; 17307 17308 hash_lock(ht); 17309 todo = ht->ht_used; 17310 for (hi = ht->ht_array; todo > 0; ++hi) 17311 { 17312 if (!HASHITEM_EMPTY(hi)) 17313 { 17314 --todo; 17315 17316 /* Free the variable. Don't remove it from the hashtab, 17317 * ht_array might change then. hash_clear() takes care of it 17318 * later. */ 17319 v = HI2DI(hi); 17320 if (free_val) 17321 clear_tv(&v->di_tv); 17322 if ((v->di_flags & DI_FLAGS_FIX) == 0) 17323 vim_free(v); 17324 } 17325 } 17326 hash_clear(ht); 17327 ht->ht_used = 0; 17328 } 17329 17330 /* 17331 * Delete a variable from hashtab "ht" at item "hi". 17332 * Clear the variable value and free the dictitem. 17333 */ 17334 static void 17335 delete_var(ht, hi) 17336 hashtab_T *ht; 17337 hashitem_T *hi; 17338 { 17339 dictitem_T *di = HI2DI(hi); 17340 17341 hash_remove(ht, hi); 17342 clear_tv(&di->di_tv); 17343 vim_free(di); 17344 } 17345 17346 /* 17347 * List the value of one internal variable. 17348 */ 17349 static void 17350 list_one_var(v, prefix) 17351 dictitem_T *v; 17352 char_u *prefix; 17353 { 17354 char_u *tofree; 17355 char_u *s; 17356 char_u numbuf[NUMBUFLEN]; 17357 17358 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID); 17359 list_one_var_a(prefix, v->di_key, v->di_tv.v_type, 17360 s == NULL ? (char_u *)"" : s); 17361 vim_free(tofree); 17362 } 17363 17364 static void 17365 list_one_var_a(prefix, name, type, string) 17366 char_u *prefix; 17367 char_u *name; 17368 int type; 17369 char_u *string; 17370 { 17371 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */ 17372 if (name != NULL) /* "a:" vars don't have a name stored */ 17373 msg_puts(name); 17374 msg_putchar(' '); 17375 msg_advance(22); 17376 if (type == VAR_NUMBER) 17377 msg_putchar('#'); 17378 else if (type == VAR_FUNC) 17379 msg_putchar('*'); 17380 else if (type == VAR_LIST) 17381 { 17382 msg_putchar('['); 17383 if (*string == '[') 17384 ++string; 17385 } 17386 else if (type == VAR_DICT) 17387 { 17388 msg_putchar('{'); 17389 if (*string == '{') 17390 ++string; 17391 } 17392 else 17393 msg_putchar(' '); 17394 17395 msg_outtrans(string); 17396 17397 if (type == VAR_FUNC) 17398 msg_puts((char_u *)"()"); 17399 } 17400 17401 /* 17402 * Set variable "name" to value in "tv". 17403 * If the variable already exists, the value is updated. 17404 * Otherwise the variable is created. 17405 */ 17406 static void 17407 set_var(name, tv, copy) 17408 char_u *name; 17409 typval_T *tv; 17410 int copy; /* make copy of value in "tv" */ 17411 { 17412 dictitem_T *v; 17413 char_u *varname; 17414 hashtab_T *ht; 17415 char_u *p; 17416 17417 if (tv->v_type == VAR_FUNC) 17418 { 17419 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':') 17420 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':') 17421 ? name[2] : name[0])) 17422 { 17423 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name); 17424 return; 17425 } 17426 if (function_exists(name)) 17427 { 17428 EMSG2(_("E705: Variable name conflicts with existing function: %s"), 17429 name); 17430 return; 17431 } 17432 } 17433 17434 ht = find_var_ht(name, &varname); 17435 if (ht == NULL || *varname == NUL) 17436 { 17437 EMSG2(_(e_illvar), name); 17438 return; 17439 } 17440 17441 v = find_var_in_ht(ht, varname, TRUE); 17442 if (v != NULL) 17443 { 17444 /* existing variable, need to clear the value */ 17445 if (var_check_ro(v->di_flags, name) 17446 || tv_check_lock(v->di_tv.v_lock, name)) 17447 return; 17448 if (v->di_tv.v_type != tv->v_type 17449 && !((v->di_tv.v_type == VAR_STRING 17450 || v->di_tv.v_type == VAR_NUMBER) 17451 && (tv->v_type == VAR_STRING 17452 || tv->v_type == VAR_NUMBER))) 17453 { 17454 EMSG2(_("E706: Variable type mismatch for: %s"), name); 17455 return; 17456 } 17457 17458 /* 17459 * Handle setting internal v: variables separately: we don't change 17460 * the type. 17461 */ 17462 if (ht == &vimvarht) 17463 { 17464 if (v->di_tv.v_type == VAR_STRING) 17465 { 17466 vim_free(v->di_tv.vval.v_string); 17467 if (copy || tv->v_type != VAR_STRING) 17468 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv)); 17469 else 17470 { 17471 /* Take over the string to avoid an extra alloc/free. */ 17472 v->di_tv.vval.v_string = tv->vval.v_string; 17473 tv->vval.v_string = NULL; 17474 } 17475 } 17476 else if (v->di_tv.v_type != VAR_NUMBER) 17477 EMSG2(_(e_intern2), "set_var()"); 17478 else 17479 v->di_tv.vval.v_number = get_tv_number(tv); 17480 return; 17481 } 17482 17483 clear_tv(&v->di_tv); 17484 } 17485 else /* add a new variable */ 17486 { 17487 /* Make sure the variable name is valid. */ 17488 for (p = varname; *p != NUL; ++p) 17489 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)) 17490 && *p != AUTOLOAD_CHAR) 17491 { 17492 EMSG2(_(e_illvar), varname); 17493 return; 17494 } 17495 17496 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) 17497 + STRLEN(varname))); 17498 if (v == NULL) 17499 return; 17500 STRCPY(v->di_key, varname); 17501 if (hash_add(ht, DI2HIKEY(v)) == FAIL) 17502 { 17503 vim_free(v); 17504 return; 17505 } 17506 v->di_flags = 0; 17507 } 17508 17509 if (copy || tv->v_type == VAR_NUMBER) 17510 copy_tv(tv, &v->di_tv); 17511 else 17512 { 17513 v->di_tv = *tv; 17514 v->di_tv.v_lock = 0; 17515 init_tv(tv); 17516 } 17517 } 17518 17519 /* 17520 * Return TRUE if di_flags "flags" indicate read-only variable "name". 17521 * Also give an error message. 17522 */ 17523 static int 17524 var_check_ro(flags, name) 17525 int flags; 17526 char_u *name; 17527 { 17528 if (flags & DI_FLAGS_RO) 17529 { 17530 EMSG2(_(e_readonlyvar), name); 17531 return TRUE; 17532 } 17533 if ((flags & DI_FLAGS_RO_SBX) && sandbox) 17534 { 17535 EMSG2(_(e_readonlysbx), name); 17536 return TRUE; 17537 } 17538 return FALSE; 17539 } 17540 17541 /* 17542 * Return TRUE if typeval "tv" is set to be locked (immutable). 17543 * Also give an error message, using "name". 17544 */ 17545 static int 17546 tv_check_lock(lock, name) 17547 int lock; 17548 char_u *name; 17549 { 17550 if (lock & VAR_LOCKED) 17551 { 17552 EMSG2(_("E741: Value is locked: %s"), 17553 name == NULL ? (char_u *)_("Unknown") : name); 17554 return TRUE; 17555 } 17556 if (lock & VAR_FIXED) 17557 { 17558 EMSG2(_("E742: Cannot change value of %s"), 17559 name == NULL ? (char_u *)_("Unknown") : name); 17560 return TRUE; 17561 } 17562 return FALSE; 17563 } 17564 17565 /* 17566 * Copy the values from typval_T "from" to typval_T "to". 17567 * When needed allocates string or increases reference count. 17568 * Does not make a copy of a list or dict but copies the reference! 17569 */ 17570 static void 17571 copy_tv(from, to) 17572 typval_T *from; 17573 typval_T *to; 17574 { 17575 to->v_type = from->v_type; 17576 to->v_lock = 0; 17577 switch (from->v_type) 17578 { 17579 case VAR_NUMBER: 17580 to->vval.v_number = from->vval.v_number; 17581 break; 17582 case VAR_STRING: 17583 case VAR_FUNC: 17584 if (from->vval.v_string == NULL) 17585 to->vval.v_string = NULL; 17586 else 17587 { 17588 to->vval.v_string = vim_strsave(from->vval.v_string); 17589 if (from->v_type == VAR_FUNC) 17590 func_ref(to->vval.v_string); 17591 } 17592 break; 17593 case VAR_LIST: 17594 if (from->vval.v_list == NULL) 17595 to->vval.v_list = NULL; 17596 else 17597 { 17598 to->vval.v_list = from->vval.v_list; 17599 ++to->vval.v_list->lv_refcount; 17600 } 17601 break; 17602 case VAR_DICT: 17603 if (from->vval.v_dict == NULL) 17604 to->vval.v_dict = NULL; 17605 else 17606 { 17607 to->vval.v_dict = from->vval.v_dict; 17608 ++to->vval.v_dict->dv_refcount; 17609 } 17610 break; 17611 default: 17612 EMSG2(_(e_intern2), "copy_tv()"); 17613 break; 17614 } 17615 } 17616 17617 /* 17618 * Make a copy of an item. 17619 * Lists and Dictionaries are also copied. A deep copy if "deep" is set. 17620 * For deepcopy() "copyID" is zero for a full copy or the ID for when a 17621 * reference to an already copied list/dict can be used. 17622 * Returns FAIL or OK. 17623 */ 17624 static int 17625 item_copy(from, to, deep, copyID) 17626 typval_T *from; 17627 typval_T *to; 17628 int deep; 17629 int copyID; 17630 { 17631 static int recurse = 0; 17632 int ret = OK; 17633 17634 if (recurse >= DICT_MAXNEST) 17635 { 17636 EMSG(_("E698: variable nested too deep for making a copy")); 17637 return FAIL; 17638 } 17639 ++recurse; 17640 17641 switch (from->v_type) 17642 { 17643 case VAR_NUMBER: 17644 case VAR_STRING: 17645 case VAR_FUNC: 17646 copy_tv(from, to); 17647 break; 17648 case VAR_LIST: 17649 to->v_type = VAR_LIST; 17650 to->v_lock = 0; 17651 if (from->vval.v_list == NULL) 17652 to->vval.v_list = NULL; 17653 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID) 17654 { 17655 /* use the copy made earlier */ 17656 to->vval.v_list = from->vval.v_list->lv_copylist; 17657 ++to->vval.v_list->lv_refcount; 17658 } 17659 else 17660 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID); 17661 if (to->vval.v_list == NULL) 17662 ret = FAIL; 17663 break; 17664 case VAR_DICT: 17665 to->v_type = VAR_DICT; 17666 to->v_lock = 0; 17667 if (from->vval.v_dict == NULL) 17668 to->vval.v_dict = NULL; 17669 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID) 17670 { 17671 /* use the copy made earlier */ 17672 to->vval.v_dict = from->vval.v_dict->dv_copydict; 17673 ++to->vval.v_dict->dv_refcount; 17674 } 17675 else 17676 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID); 17677 if (to->vval.v_dict == NULL) 17678 ret = FAIL; 17679 break; 17680 default: 17681 EMSG2(_(e_intern2), "item_copy()"); 17682 ret = FAIL; 17683 } 17684 --recurse; 17685 return ret; 17686 } 17687 17688 /* 17689 * ":echo expr1 ..." print each argument separated with a space, add a 17690 * newline at the end. 17691 * ":echon expr1 ..." print each argument plain. 17692 */ 17693 void 17694 ex_echo(eap) 17695 exarg_T *eap; 17696 { 17697 char_u *arg = eap->arg; 17698 typval_T rettv; 17699 char_u *tofree; 17700 char_u *p; 17701 int needclr = TRUE; 17702 int atstart = TRUE; 17703 char_u numbuf[NUMBUFLEN]; 17704 17705 if (eap->skip) 17706 ++emsg_skip; 17707 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int) 17708 { 17709 p = arg; 17710 if (eval1(&arg, &rettv, !eap->skip) == FAIL) 17711 { 17712 /* 17713 * Report the invalid expression unless the expression evaluation 17714 * has been cancelled due to an aborting error, an interrupt, or an 17715 * exception. 17716 */ 17717 if (!aborting()) 17718 EMSG2(_(e_invexpr2), p); 17719 break; 17720 } 17721 if (!eap->skip) 17722 { 17723 if (atstart) 17724 { 17725 atstart = FALSE; 17726 /* Call msg_start() after eval1(), evaluating the expression 17727 * may cause a message to appear. */ 17728 if (eap->cmdidx == CMD_echo) 17729 msg_start(); 17730 } 17731 else if (eap->cmdidx == CMD_echo) 17732 msg_puts_attr((char_u *)" ", echo_attr); 17733 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID); 17734 if (p != NULL) 17735 for ( ; *p != NUL && !got_int; ++p) 17736 { 17737 if (*p == '\n' || *p == '\r' || *p == TAB) 17738 { 17739 if (*p != TAB && needclr) 17740 { 17741 /* remove any text still there from the command */ 17742 msg_clr_eos(); 17743 needclr = FALSE; 17744 } 17745 msg_putchar_attr(*p, echo_attr); 17746 } 17747 else 17748 { 17749 #ifdef FEAT_MBYTE 17750 if (has_mbyte) 17751 { 17752 int i = (*mb_ptr2len)(p); 17753 17754 (void)msg_outtrans_len_attr(p, i, echo_attr); 17755 p += i - 1; 17756 } 17757 else 17758 #endif 17759 (void)msg_outtrans_len_attr(p, 1, echo_attr); 17760 } 17761 } 17762 vim_free(tofree); 17763 } 17764 clear_tv(&rettv); 17765 arg = skipwhite(arg); 17766 } 17767 eap->nextcmd = check_nextcmd(arg); 17768 17769 if (eap->skip) 17770 --emsg_skip; 17771 else 17772 { 17773 /* remove text that may still be there from the command */ 17774 if (needclr) 17775 msg_clr_eos(); 17776 if (eap->cmdidx == CMD_echo) 17777 msg_end(); 17778 } 17779 } 17780 17781 /* 17782 * ":echohl {name}". 17783 */ 17784 void 17785 ex_echohl(eap) 17786 exarg_T *eap; 17787 { 17788 int id; 17789 17790 id = syn_name2id(eap->arg); 17791 if (id == 0) 17792 echo_attr = 0; 17793 else 17794 echo_attr = syn_id2attr(id); 17795 } 17796 17797 /* 17798 * ":execute expr1 ..." execute the result of an expression. 17799 * ":echomsg expr1 ..." Print a message 17800 * ":echoerr expr1 ..." Print an error 17801 * Each gets spaces around each argument and a newline at the end for 17802 * echo commands 17803 */ 17804 void 17805 ex_execute(eap) 17806 exarg_T *eap; 17807 { 17808 char_u *arg = eap->arg; 17809 typval_T rettv; 17810 int ret = OK; 17811 char_u *p; 17812 garray_T ga; 17813 int len; 17814 int save_did_emsg; 17815 17816 ga_init2(&ga, 1, 80); 17817 17818 if (eap->skip) 17819 ++emsg_skip; 17820 while (*arg != NUL && *arg != '|' && *arg != '\n') 17821 { 17822 p = arg; 17823 if (eval1(&arg, &rettv, !eap->skip) == FAIL) 17824 { 17825 /* 17826 * Report the invalid expression unless the expression evaluation 17827 * has been cancelled due to an aborting error, an interrupt, or an 17828 * exception. 17829 */ 17830 if (!aborting()) 17831 EMSG2(_(e_invexpr2), p); 17832 ret = FAIL; 17833 break; 17834 } 17835 17836 if (!eap->skip) 17837 { 17838 p = get_tv_string(&rettv); 17839 len = (int)STRLEN(p); 17840 if (ga_grow(&ga, len + 2) == FAIL) 17841 { 17842 clear_tv(&rettv); 17843 ret = FAIL; 17844 break; 17845 } 17846 if (ga.ga_len) 17847 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; 17848 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); 17849 ga.ga_len += len; 17850 } 17851 17852 clear_tv(&rettv); 17853 arg = skipwhite(arg); 17854 } 17855 17856 if (ret != FAIL && ga.ga_data != NULL) 17857 { 17858 if (eap->cmdidx == CMD_echomsg) 17859 { 17860 MSG_ATTR(ga.ga_data, echo_attr); 17861 out_flush(); 17862 } 17863 else if (eap->cmdidx == CMD_echoerr) 17864 { 17865 /* We don't want to abort following commands, restore did_emsg. */ 17866 save_did_emsg = did_emsg; 17867 EMSG((char_u *)ga.ga_data); 17868 if (!force_abort) 17869 did_emsg = save_did_emsg; 17870 } 17871 else if (eap->cmdidx == CMD_execute) 17872 do_cmdline((char_u *)ga.ga_data, 17873 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE); 17874 } 17875 17876 ga_clear(&ga); 17877 17878 if (eap->skip) 17879 --emsg_skip; 17880 17881 eap->nextcmd = check_nextcmd(arg); 17882 } 17883 17884 /* 17885 * Skip over the name of an option: "&option", "&g:option" or "&l:option". 17886 * "arg" points to the "&" or '+' when called, to "option" when returning. 17887 * Returns NULL when no option name found. Otherwise pointer to the char 17888 * after the option name. 17889 */ 17890 static char_u * 17891 find_option_end(arg, opt_flags) 17892 char_u **arg; 17893 int *opt_flags; 17894 { 17895 char_u *p = *arg; 17896 17897 ++p; 17898 if (*p == 'g' && p[1] == ':') 17899 { 17900 *opt_flags = OPT_GLOBAL; 17901 p += 2; 17902 } 17903 else if (*p == 'l' && p[1] == ':') 17904 { 17905 *opt_flags = OPT_LOCAL; 17906 p += 2; 17907 } 17908 else 17909 *opt_flags = 0; 17910 17911 if (!ASCII_ISALPHA(*p)) 17912 return NULL; 17913 *arg = p; 17914 17915 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL) 17916 p += 4; /* termcap option */ 17917 else 17918 while (ASCII_ISALPHA(*p)) 17919 ++p; 17920 return p; 17921 } 17922 17923 /* 17924 * ":function" 17925 */ 17926 void 17927 ex_function(eap) 17928 exarg_T *eap; 17929 { 17930 char_u *theline; 17931 int j; 17932 int c; 17933 int saved_did_emsg; 17934 char_u *name = NULL; 17935 char_u *p; 17936 char_u *arg; 17937 char_u *line_arg = NULL; 17938 garray_T newargs; 17939 garray_T newlines; 17940 int varargs = FALSE; 17941 int mustend = FALSE; 17942 int flags = 0; 17943 ufunc_T *fp; 17944 int indent; 17945 int nesting; 17946 char_u *skip_until = NULL; 17947 dictitem_T *v; 17948 funcdict_T fudi; 17949 static int func_nr = 0; /* number for nameless function */ 17950 int paren; 17951 hashtab_T *ht; 17952 int todo; 17953 hashitem_T *hi; 17954 int sourcing_lnum_off; 17955 17956 /* 17957 * ":function" without argument: list functions. 17958 */ 17959 if (ends_excmd(*eap->arg)) 17960 { 17961 if (!eap->skip) 17962 { 17963 todo = func_hashtab.ht_used; 17964 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) 17965 { 17966 if (!HASHITEM_EMPTY(hi)) 17967 { 17968 --todo; 17969 fp = HI2UF(hi); 17970 if (!isdigit(*fp->uf_name)) 17971 list_func_head(fp, FALSE); 17972 } 17973 } 17974 } 17975 eap->nextcmd = check_nextcmd(eap->arg); 17976 return; 17977 } 17978 17979 /* 17980 * ":function /pat": list functions matching pattern. 17981 */ 17982 if (*eap->arg == '/') 17983 { 17984 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL); 17985 if (!eap->skip) 17986 { 17987 regmatch_T regmatch; 17988 17989 c = *p; 17990 *p = NUL; 17991 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC); 17992 *p = c; 17993 if (regmatch.regprog != NULL) 17994 { 17995 regmatch.rm_ic = p_ic; 17996 17997 todo = func_hashtab.ht_used; 17998 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi) 17999 { 18000 if (!HASHITEM_EMPTY(hi)) 18001 { 18002 --todo; 18003 fp = HI2UF(hi); 18004 if (!isdigit(*fp->uf_name) 18005 && vim_regexec(®match, fp->uf_name, 0)) 18006 list_func_head(fp, FALSE); 18007 } 18008 } 18009 } 18010 } 18011 if (*p == '/') 18012 ++p; 18013 eap->nextcmd = check_nextcmd(p); 18014 return; 18015 } 18016 18017 /* 18018 * Get the function name. There are these situations: 18019 * func normal function name 18020 * "name" == func, "fudi.fd_dict" == NULL 18021 * dict.func new dictionary entry 18022 * "name" == NULL, "fudi.fd_dict" set, 18023 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func 18024 * dict.func existing dict entry with a Funcref 18025 * "name" == func, "fudi.fd_dict" set, 18026 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL 18027 * dict.func existing dict entry that's not a Funcref 18028 * "name" == NULL, "fudi.fd_dict" set, 18029 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL 18030 */ 18031 p = eap->arg; 18032 name = trans_function_name(&p, eap->skip, 0, &fudi); 18033 paren = (vim_strchr(p, '(') != NULL); 18034 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip) 18035 { 18036 /* 18037 * Return on an invalid expression in braces, unless the expression 18038 * evaluation has been cancelled due to an aborting error, an 18039 * interrupt, or an exception. 18040 */ 18041 if (!aborting()) 18042 { 18043 if (!eap->skip && fudi.fd_newkey != NULL) 18044 EMSG2(_(e_dictkey), fudi.fd_newkey); 18045 vim_free(fudi.fd_newkey); 18046 return; 18047 } 18048 else 18049 eap->skip = TRUE; 18050 } 18051 18052 /* An error in a function call during evaluation of an expression in magic 18053 * braces should not cause the function not to be defined. */ 18054 saved_did_emsg = did_emsg; 18055 did_emsg = FALSE; 18056 18057 /* 18058 * ":function func" with only function name: list function. 18059 */ 18060 if (!paren) 18061 { 18062 if (!ends_excmd(*skipwhite(p))) 18063 { 18064 EMSG(_(e_trailing)); 18065 goto ret_free; 18066 } 18067 eap->nextcmd = check_nextcmd(p); 18068 if (eap->nextcmd != NULL) 18069 *p = NUL; 18070 if (!eap->skip && !got_int) 18071 { 18072 fp = find_func(name); 18073 if (fp != NULL) 18074 { 18075 list_func_head(fp, TRUE); 18076 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j) 18077 { 18078 if (FUNCLINE(fp, j) == NULL) 18079 continue; 18080 msg_putchar('\n'); 18081 msg_outnum((long)(j + 1)); 18082 if (j < 9) 18083 msg_putchar(' '); 18084 if (j < 99) 18085 msg_putchar(' '); 18086 msg_prt_line(FUNCLINE(fp, j), FALSE); 18087 out_flush(); /* show a line at a time */ 18088 ui_breakcheck(); 18089 } 18090 if (!got_int) 18091 { 18092 msg_putchar('\n'); 18093 msg_puts((char_u *)" endfunction"); 18094 } 18095 } 18096 else 18097 emsg_funcname("E123: Undefined function: %s", name); 18098 } 18099 goto ret_free; 18100 } 18101 18102 /* 18103 * ":function name(arg1, arg2)" Define function. 18104 */ 18105 p = skipwhite(p); 18106 if (*p != '(') 18107 { 18108 if (!eap->skip) 18109 { 18110 EMSG2(_("E124: Missing '(': %s"), eap->arg); 18111 goto ret_free; 18112 } 18113 /* attempt to continue by skipping some text */ 18114 if (vim_strchr(p, '(') != NULL) 18115 p = vim_strchr(p, '('); 18116 } 18117 p = skipwhite(p + 1); 18118 18119 ga_init2(&newargs, (int)sizeof(char_u *), 3); 18120 ga_init2(&newlines, (int)sizeof(char_u *), 3); 18121 18122 if (!eap->skip) 18123 { 18124 /* Check the name of the function. */ 18125 if (name != NULL) 18126 arg = name; 18127 else 18128 arg = fudi.fd_newkey; 18129 if (arg != NULL) 18130 { 18131 if (*arg == K_SPECIAL) 18132 j = 3; 18133 else 18134 j = 0; 18135 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j]) 18136 : eval_isnamec(arg[j]))) 18137 ++j; 18138 if (arg[j] != NUL) 18139 emsg_funcname(_(e_invarg2), arg); 18140 } 18141 } 18142 18143 /* 18144 * Isolate the arguments: "arg1, arg2, ...)" 18145 */ 18146 while (*p != ')') 18147 { 18148 if (p[0] == '.' && p[1] == '.' && p[2] == '.') 18149 { 18150 varargs = TRUE; 18151 p += 3; 18152 mustend = TRUE; 18153 } 18154 else 18155 { 18156 arg = p; 18157 while (ASCII_ISALNUM(*p) || *p == '_') 18158 ++p; 18159 if (arg == p || isdigit(*arg) 18160 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0) 18161 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0)) 18162 { 18163 if (!eap->skip) 18164 EMSG2(_("E125: Illegal argument: %s"), arg); 18165 break; 18166 } 18167 if (ga_grow(&newargs, 1) == FAIL) 18168 goto erret; 18169 c = *p; 18170 *p = NUL; 18171 arg = vim_strsave(arg); 18172 if (arg == NULL) 18173 goto erret; 18174 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg; 18175 *p = c; 18176 newargs.ga_len++; 18177 if (*p == ',') 18178 ++p; 18179 else 18180 mustend = TRUE; 18181 } 18182 p = skipwhite(p); 18183 if (mustend && *p != ')') 18184 { 18185 if (!eap->skip) 18186 EMSG2(_(e_invarg2), eap->arg); 18187 break; 18188 } 18189 } 18190 ++p; /* skip the ')' */ 18191 18192 /* find extra arguments "range", "dict" and "abort" */ 18193 for (;;) 18194 { 18195 p = skipwhite(p); 18196 if (STRNCMP(p, "range", 5) == 0) 18197 { 18198 flags |= FC_RANGE; 18199 p += 5; 18200 } 18201 else if (STRNCMP(p, "dict", 4) == 0) 18202 { 18203 flags |= FC_DICT; 18204 p += 4; 18205 } 18206 else if (STRNCMP(p, "abort", 5) == 0) 18207 { 18208 flags |= FC_ABORT; 18209 p += 5; 18210 } 18211 else 18212 break; 18213 } 18214 18215 /* When there is a line break use what follows for the function body. 18216 * Makes 'exe "func Test()\n...\nendfunc"' work. */ 18217 if (*p == '\n') 18218 line_arg = p + 1; 18219 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg) 18220 EMSG(_(e_trailing)); 18221 18222 /* 18223 * Read the body of the function, until ":endfunction" is found. 18224 */ 18225 if (KeyTyped) 18226 { 18227 /* Check if the function already exists, don't let the user type the 18228 * whole function before telling him it doesn't work! For a script we 18229 * need to skip the body to be able to find what follows. */ 18230 if (!eap->skip && !eap->forceit) 18231 { 18232 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL) 18233 EMSG(_(e_funcdict)); 18234 else if (name != NULL && find_func(name) != NULL) 18235 emsg_funcname(e_funcexts, name); 18236 } 18237 18238 if (!eap->skip && did_emsg) 18239 goto erret; 18240 18241 msg_putchar('\n'); /* don't overwrite the function name */ 18242 cmdline_row = msg_row; 18243 } 18244 18245 indent = 2; 18246 nesting = 0; 18247 for (;;) 18248 { 18249 msg_scroll = TRUE; 18250 need_wait_return = FALSE; 18251 sourcing_lnum_off = sourcing_lnum; 18252 18253 if (line_arg != NULL) 18254 { 18255 /* Use eap->arg, split up in parts by line breaks. */ 18256 theline = line_arg; 18257 p = vim_strchr(theline, '\n'); 18258 if (p == NULL) 18259 line_arg += STRLEN(line_arg); 18260 else 18261 { 18262 *p = NUL; 18263 line_arg = p + 1; 18264 } 18265 } 18266 else if (eap->getline == NULL) 18267 theline = getcmdline(':', 0L, indent); 18268 else 18269 theline = eap->getline(':', eap->cookie, indent); 18270 if (KeyTyped) 18271 lines_left = Rows - 1; 18272 if (theline == NULL) 18273 { 18274 EMSG(_("E126: Missing :endfunction")); 18275 goto erret; 18276 } 18277 18278 /* Detect line continuation: sourcing_lnum increased more than one. */ 18279 if (sourcing_lnum > sourcing_lnum_off + 1) 18280 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1; 18281 else 18282 sourcing_lnum_off = 0; 18283 18284 if (skip_until != NULL) 18285 { 18286 /* between ":append" and "." and between ":python <<EOF" and "EOF" 18287 * don't check for ":endfunc". */ 18288 if (STRCMP(theline, skip_until) == 0) 18289 { 18290 vim_free(skip_until); 18291 skip_until = NULL; 18292 } 18293 } 18294 else 18295 { 18296 /* skip ':' and blanks*/ 18297 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p) 18298 ; 18299 18300 /* Check for "endfunction". */ 18301 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0) 18302 { 18303 if (line_arg == NULL) 18304 vim_free(theline); 18305 break; 18306 } 18307 18308 /* Increase indent inside "if", "while", "for" and "try", decrease 18309 * at "end". */ 18310 if (indent > 2 && STRNCMP(p, "end", 3) == 0) 18311 indent -= 2; 18312 else if (STRNCMP(p, "if", 2) == 0 18313 || STRNCMP(p, "wh", 2) == 0 18314 || STRNCMP(p, "for", 3) == 0 18315 || STRNCMP(p, "try", 3) == 0) 18316 indent += 2; 18317 18318 /* Check for defining a function inside this function. */ 18319 if (checkforcmd(&p, "function", 2)) 18320 { 18321 if (*p == '!') 18322 p = skipwhite(p + 1); 18323 p += eval_fname_script(p); 18324 if (ASCII_ISALPHA(*p)) 18325 { 18326 vim_free(trans_function_name(&p, TRUE, 0, NULL)); 18327 if (*skipwhite(p) == '(') 18328 { 18329 ++nesting; 18330 indent += 2; 18331 } 18332 } 18333 } 18334 18335 /* Check for ":append" or ":insert". */ 18336 p = skip_range(p, NULL); 18337 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p')) 18338 || (p[0] == 'i' 18339 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n' 18340 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's')))))) 18341 skip_until = vim_strsave((char_u *)"."); 18342 18343 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */ 18344 arg = skipwhite(skiptowhite(p)); 18345 if (arg[0] == '<' && arg[1] =='<' 18346 && ((p[0] == 'p' && p[1] == 'y' 18347 && (!ASCII_ISALPHA(p[2]) || p[2] == 't')) 18348 || (p[0] == 'p' && p[1] == 'e' 18349 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r')) 18350 || (p[0] == 't' && p[1] == 'c' 18351 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l')) 18352 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b' 18353 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y')) 18354 || (p[0] == 'm' && p[1] == 'z' 18355 && (!ASCII_ISALPHA(p[2]) || p[2] == 's')) 18356 )) 18357 { 18358 /* ":python <<" continues until a dot, like ":append" */ 18359 p = skipwhite(arg + 2); 18360 if (*p == NUL) 18361 skip_until = vim_strsave((char_u *)"."); 18362 else 18363 skip_until = vim_strsave(p); 18364 } 18365 } 18366 18367 /* Add the line to the function. */ 18368 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL) 18369 { 18370 if (line_arg == NULL) 18371 vim_free(theline); 18372 goto erret; 18373 } 18374 18375 /* Copy the line to newly allocated memory. get_one_sourceline() 18376 * allocates 250 bytes per line, this saves 80% on average. The cost 18377 * is an extra alloc/free. */ 18378 p = vim_strsave(theline); 18379 if (p != NULL) 18380 { 18381 if (line_arg == NULL) 18382 vim_free(theline); 18383 theline = p; 18384 } 18385 18386 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline; 18387 18388 /* Add NULL lines for continuation lines, so that the line count is 18389 * equal to the index in the growarray. */ 18390 while (sourcing_lnum_off-- > 0) 18391 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL; 18392 18393 /* Check for end of eap->arg. */ 18394 if (line_arg != NULL && *line_arg == NUL) 18395 line_arg = NULL; 18396 } 18397 18398 /* Don't define the function when skipping commands or when an error was 18399 * detected. */ 18400 if (eap->skip || did_emsg) 18401 goto erret; 18402 18403 /* 18404 * If there are no errors, add the function 18405 */ 18406 if (fudi.fd_dict == NULL) 18407 { 18408 v = find_var(name, &ht); 18409 if (v != NULL && v->di_tv.v_type == VAR_FUNC) 18410 { 18411 emsg_funcname("E707: Function name conflicts with variable: %s", 18412 name); 18413 goto erret; 18414 } 18415 18416 fp = find_func(name); 18417 if (fp != NULL) 18418 { 18419 if (!eap->forceit) 18420 { 18421 emsg_funcname(e_funcexts, name); 18422 goto erret; 18423 } 18424 if (fp->uf_calls > 0) 18425 { 18426 emsg_funcname("E127: Cannot redefine function %s: It is in use", 18427 name); 18428 goto erret; 18429 } 18430 /* redefine existing function */ 18431 ga_clear_strings(&(fp->uf_args)); 18432 ga_clear_strings(&(fp->uf_lines)); 18433 vim_free(name); 18434 name = NULL; 18435 } 18436 } 18437 else 18438 { 18439 char numbuf[20]; 18440 18441 fp = NULL; 18442 if (fudi.fd_newkey == NULL && !eap->forceit) 18443 { 18444 EMSG(_(e_funcdict)); 18445 goto erret; 18446 } 18447 if (fudi.fd_di == NULL) 18448 { 18449 /* Can't add a function to a locked dictionary */ 18450 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg)) 18451 goto erret; 18452 } 18453 /* Can't change an existing function if it is locked */ 18454 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg)) 18455 goto erret; 18456 18457 /* Give the function a sequential number. Can only be used with a 18458 * Funcref! */ 18459 vim_free(name); 18460 sprintf(numbuf, "%d", ++func_nr); 18461 name = vim_strsave((char_u *)numbuf); 18462 if (name == NULL) 18463 goto erret; 18464 } 18465 18466 if (fp == NULL) 18467 { 18468 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL) 18469 { 18470 int slen, plen; 18471 char_u *scriptname; 18472 18473 /* Check that the autoload name matches the script name. */ 18474 j = FAIL; 18475 if (sourcing_name != NULL) 18476 { 18477 scriptname = autoload_name(name); 18478 if (scriptname != NULL) 18479 { 18480 p = vim_strchr(scriptname, '/'); 18481 plen = STRLEN(p); 18482 slen = STRLEN(sourcing_name); 18483 if (slen > plen && fnamecmp(p, 18484 sourcing_name + slen - plen) == 0) 18485 j = OK; 18486 vim_free(scriptname); 18487 } 18488 } 18489 if (j == FAIL) 18490 { 18491 EMSG2(_("E746: Function name does not match script file name: %s"), name); 18492 goto erret; 18493 } 18494 } 18495 18496 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name))); 18497 if (fp == NULL) 18498 goto erret; 18499 18500 if (fudi.fd_dict != NULL) 18501 { 18502 if (fudi.fd_di == NULL) 18503 { 18504 /* add new dict entry */ 18505 fudi.fd_di = dictitem_alloc(fudi.fd_newkey); 18506 if (fudi.fd_di == NULL) 18507 { 18508 vim_free(fp); 18509 goto erret; 18510 } 18511 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL) 18512 { 18513 vim_free(fudi.fd_di); 18514 goto erret; 18515 } 18516 } 18517 else 18518 /* overwrite existing dict entry */ 18519 clear_tv(&fudi.fd_di->di_tv); 18520 fudi.fd_di->di_tv.v_type = VAR_FUNC; 18521 fudi.fd_di->di_tv.v_lock = 0; 18522 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name); 18523 fp->uf_refcount = 1; 18524 } 18525 18526 /* insert the new function in the function list */ 18527 STRCPY(fp->uf_name, name); 18528 hash_add(&func_hashtab, UF2HIKEY(fp)); 18529 } 18530 fp->uf_args = newargs; 18531 fp->uf_lines = newlines; 18532 #ifdef FEAT_PROFILE 18533 fp->uf_tml_count = NULL; 18534 fp->uf_tml_total = NULL; 18535 fp->uf_tml_self = NULL; 18536 fp->uf_profiling = FALSE; 18537 if (prof_def_func()) 18538 func_do_profile(fp); 18539 #endif 18540 fp->uf_varargs = varargs; 18541 fp->uf_flags = flags; 18542 fp->uf_calls = 0; 18543 fp->uf_script_ID = current_SID; 18544 goto ret_free; 18545 18546 erret: 18547 ga_clear_strings(&newargs); 18548 ga_clear_strings(&newlines); 18549 ret_free: 18550 vim_free(skip_until); 18551 vim_free(fudi.fd_newkey); 18552 vim_free(name); 18553 did_emsg |= saved_did_emsg; 18554 } 18555 18556 /* 18557 * Get a function name, translating "<SID>" and "<SNR>". 18558 * Also handles a Funcref in a List or Dictionary. 18559 * Returns the function name in allocated memory, or NULL for failure. 18560 * flags: 18561 * TFN_INT: internal function name OK 18562 * TFN_QUIET: be quiet 18563 * Advances "pp" to just after the function name (if no error). 18564 */ 18565 static char_u * 18566 trans_function_name(pp, skip, flags, fdp) 18567 char_u **pp; 18568 int skip; /* only find the end, don't evaluate */ 18569 int flags; 18570 funcdict_T *fdp; /* return: info about dictionary used */ 18571 { 18572 char_u *name = NULL; 18573 char_u *start; 18574 char_u *end; 18575 int lead; 18576 char_u sid_buf[20]; 18577 int len; 18578 lval_T lv; 18579 18580 if (fdp != NULL) 18581 vim_memset(fdp, 0, sizeof(funcdict_T)); 18582 start = *pp; 18583 18584 /* Check for hard coded <SNR>: already translated function ID (from a user 18585 * command). */ 18586 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA 18587 && (*pp)[2] == (int)KE_SNR) 18588 { 18589 *pp += 3; 18590 len = get_id_len(pp) + 3; 18591 return vim_strnsave(start, len); 18592 } 18593 18594 /* A name starting with "<SID>" or "<SNR>" is local to a script. But 18595 * don't skip over "s:", get_lval() needs it for "s:dict.func". */ 18596 lead = eval_fname_script(start); 18597 if (lead > 2) 18598 start += lead; 18599 18600 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET, 18601 lead > 2 ? 0 : FNE_CHECK_START); 18602 if (end == start) 18603 { 18604 if (!skip) 18605 EMSG(_("E129: Function name required")); 18606 goto theend; 18607 } 18608 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range))) 18609 { 18610 /* 18611 * Report an invalid expression in braces, unless the expression 18612 * evaluation has been cancelled due to an aborting error, an 18613 * interrupt, or an exception. 18614 */ 18615 if (!aborting()) 18616 { 18617 if (end != NULL) 18618 EMSG2(_(e_invarg2), start); 18619 } 18620 else 18621 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR); 18622 goto theend; 18623 } 18624 18625 if (lv.ll_tv != NULL) 18626 { 18627 if (fdp != NULL) 18628 { 18629 fdp->fd_dict = lv.ll_dict; 18630 fdp->fd_newkey = lv.ll_newkey; 18631 lv.ll_newkey = NULL; 18632 fdp->fd_di = lv.ll_di; 18633 } 18634 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL) 18635 { 18636 name = vim_strsave(lv.ll_tv->vval.v_string); 18637 *pp = end; 18638 } 18639 else 18640 { 18641 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL 18642 || lv.ll_dict == NULL || fdp->fd_newkey == NULL)) 18643 EMSG(_(e_funcref)); 18644 else 18645 *pp = end; 18646 name = NULL; 18647 } 18648 goto theend; 18649 } 18650 18651 if (lv.ll_name == NULL) 18652 { 18653 /* Error found, but continue after the function name. */ 18654 *pp = end; 18655 goto theend; 18656 } 18657 18658 if (lv.ll_exp_name != NULL) 18659 { 18660 len = STRLEN(lv.ll_exp_name); 18661 if (lead <= 2 && lv.ll_name == lv.ll_exp_name 18662 && STRNCMP(lv.ll_name, "s:", 2) == 0) 18663 { 18664 /* When there was "s:" already or the name expanded to get a 18665 * leading "s:" then remove it. */ 18666 lv.ll_name += 2; 18667 len -= 2; 18668 lead = 2; 18669 } 18670 } 18671 else 18672 { 18673 if (lead == 2) /* skip over "s:" */ 18674 lv.ll_name += 2; 18675 len = (int)(end - lv.ll_name); 18676 } 18677 18678 /* 18679 * Copy the function name to allocated memory. 18680 * Accept <SID>name() inside a script, translate into <SNR>123_name(). 18681 * Accept <SNR>123_name() outside a script. 18682 */ 18683 if (skip) 18684 lead = 0; /* do nothing */ 18685 else if (lead > 0) 18686 { 18687 lead = 3; 18688 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp)) 18689 { 18690 /* It's "s:" or "<SID>" */ 18691 if (current_SID <= 0) 18692 { 18693 EMSG(_(e_usingsid)); 18694 goto theend; 18695 } 18696 sprintf((char *)sid_buf, "%ld_", (long)current_SID); 18697 lead += (int)STRLEN(sid_buf); 18698 } 18699 } 18700 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name)) 18701 { 18702 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name); 18703 goto theend; 18704 } 18705 name = alloc((unsigned)(len + lead + 1)); 18706 if (name != NULL) 18707 { 18708 if (lead > 0) 18709 { 18710 name[0] = K_SPECIAL; 18711 name[1] = KS_EXTRA; 18712 name[2] = (int)KE_SNR; 18713 if (lead > 3) /* If it's "<SID>" */ 18714 STRCPY(name + 3, sid_buf); 18715 } 18716 mch_memmove(name + lead, lv.ll_name, (size_t)len); 18717 name[len + lead] = NUL; 18718 } 18719 *pp = end; 18720 18721 theend: 18722 clear_lval(&lv); 18723 return name; 18724 } 18725 18726 /* 18727 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case). 18728 * Return 2 if "p" starts with "s:". 18729 * Return 0 otherwise. 18730 */ 18731 static int 18732 eval_fname_script(p) 18733 char_u *p; 18734 { 18735 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0 18736 || STRNICMP(p + 1, "SNR>", 4) == 0)) 18737 return 5; 18738 if (p[0] == 's' && p[1] == ':') 18739 return 2; 18740 return 0; 18741 } 18742 18743 /* 18744 * Return TRUE if "p" starts with "<SID>" or "s:". 18745 * Only works if eval_fname_script() returned non-zero for "p"! 18746 */ 18747 static int 18748 eval_fname_sid(p) 18749 char_u *p; 18750 { 18751 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I'); 18752 } 18753 18754 /* 18755 * List the head of the function: "name(arg1, arg2)". 18756 */ 18757 static void 18758 list_func_head(fp, indent) 18759 ufunc_T *fp; 18760 int indent; 18761 { 18762 int j; 18763 18764 msg_start(); 18765 if (indent) 18766 MSG_PUTS(" "); 18767 MSG_PUTS("function "); 18768 if (fp->uf_name[0] == K_SPECIAL) 18769 { 18770 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8)); 18771 msg_puts(fp->uf_name + 3); 18772 } 18773 else 18774 msg_puts(fp->uf_name); 18775 msg_putchar('('); 18776 for (j = 0; j < fp->uf_args.ga_len; ++j) 18777 { 18778 if (j) 18779 MSG_PUTS(", "); 18780 msg_puts(FUNCARG(fp, j)); 18781 } 18782 if (fp->uf_varargs) 18783 { 18784 if (j) 18785 MSG_PUTS(", "); 18786 MSG_PUTS("..."); 18787 } 18788 msg_putchar(')'); 18789 msg_clr_eos(); 18790 if (p_verbose > 0) 18791 last_set_msg(fp->uf_script_ID); 18792 } 18793 18794 /* 18795 * Find a function by name, return pointer to it in ufuncs. 18796 * Return NULL for unknown function. 18797 */ 18798 static ufunc_T * 18799 find_func(name) 18800 char_u *name; 18801 { 18802 hashitem_T *hi; 18803 18804 hi = hash_find(&func_hashtab, name); 18805 if (!HASHITEM_EMPTY(hi)) 18806 return HI2UF(hi); 18807 return NULL; 18808 } 18809 18810 #if defined(EXITFREE) || defined(PROTO) 18811 void 18812 free_all_functions() 18813 { 18814 hashitem_T *hi; 18815 18816 /* Need to start all over every time, because func_free() may change the 18817 * hash table. */ 18818 while (func_hashtab.ht_used > 0) 18819 for (hi = func_hashtab.ht_array; ; ++hi) 18820 if (!HASHITEM_EMPTY(hi)) 18821 { 18822 func_free(HI2UF(hi)); 18823 break; 18824 } 18825 } 18826 #endif 18827 18828 /* 18829 * Return TRUE if a function "name" exists. 18830 */ 18831 static int 18832 function_exists(name) 18833 char_u *name; 18834 { 18835 char_u *p = name; 18836 int n = FALSE; 18837 18838 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL); 18839 if (p != NULL) 18840 { 18841 if (builtin_function(p)) 18842 n = (find_internal_func(p) >= 0); 18843 else 18844 n = (find_func(p) != NULL); 18845 vim_free(p); 18846 } 18847 return n; 18848 } 18849 18850 /* 18851 * Return TRUE if "name" looks like a builtin function name: starts with a 18852 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR. 18853 */ 18854 static int 18855 builtin_function(name) 18856 char_u *name; 18857 { 18858 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL 18859 && vim_strchr(name, AUTOLOAD_CHAR) == NULL; 18860 } 18861 18862 #if defined(FEAT_PROFILE) || defined(PROTO) 18863 /* 18864 * Start profiling function "fp". 18865 */ 18866 static void 18867 func_do_profile(fp) 18868 ufunc_T *fp; 18869 { 18870 fp->uf_tm_count = 0; 18871 profile_zero(&fp->uf_tm_self); 18872 profile_zero(&fp->uf_tm_total); 18873 if (fp->uf_tml_count == NULL) 18874 fp->uf_tml_count = (int *)alloc_clear((unsigned) 18875 (sizeof(int) * fp->uf_lines.ga_len)); 18876 if (fp->uf_tml_total == NULL) 18877 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned) 18878 (sizeof(proftime_T) * fp->uf_lines.ga_len)); 18879 if (fp->uf_tml_self == NULL) 18880 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned) 18881 (sizeof(proftime_T) * fp->uf_lines.ga_len)); 18882 fp->uf_tml_idx = -1; 18883 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL 18884 || fp->uf_tml_self == NULL) 18885 return; /* out of memory */ 18886 18887 fp->uf_profiling = TRUE; 18888 } 18889 18890 /* 18891 * Dump the profiling results for all functions in file "fd". 18892 */ 18893 void 18894 func_dump_profile(fd) 18895 FILE *fd; 18896 { 18897 hashitem_T *hi; 18898 int todo; 18899 ufunc_T *fp; 18900 int i; 18901 ufunc_T **sorttab; 18902 int st_len = 0; 18903 18904 todo = func_hashtab.ht_used; 18905 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo)); 18906 18907 for (hi = func_hashtab.ht_array; todo > 0; ++hi) 18908 { 18909 if (!HASHITEM_EMPTY(hi)) 18910 { 18911 --todo; 18912 fp = HI2UF(hi); 18913 if (fp->uf_profiling) 18914 { 18915 if (sorttab != NULL) 18916 sorttab[st_len++] = fp; 18917 18918 if (fp->uf_name[0] == K_SPECIAL) 18919 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3); 18920 else 18921 fprintf(fd, "FUNCTION %s()\n", fp->uf_name); 18922 if (fp->uf_tm_count == 1) 18923 fprintf(fd, "Called 1 time\n"); 18924 else 18925 fprintf(fd, "Called %d times\n", fp->uf_tm_count); 18926 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total)); 18927 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self)); 18928 fprintf(fd, "\n"); 18929 fprintf(fd, "count total (s) self (s)\n"); 18930 18931 for (i = 0; i < fp->uf_lines.ga_len; ++i) 18932 { 18933 if (FUNCLINE(fp, i) == NULL) 18934 continue; 18935 prof_func_line(fd, fp->uf_tml_count[i], 18936 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE); 18937 fprintf(fd, "%s\n", FUNCLINE(fp, i)); 18938 } 18939 fprintf(fd, "\n"); 18940 } 18941 } 18942 } 18943 18944 if (sorttab != NULL && st_len > 0) 18945 { 18946 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *), 18947 prof_total_cmp); 18948 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE); 18949 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *), 18950 prof_self_cmp); 18951 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE); 18952 } 18953 } 18954 18955 static void 18956 prof_sort_list(fd, sorttab, st_len, title, prefer_self) 18957 FILE *fd; 18958 ufunc_T **sorttab; 18959 int st_len; 18960 char *title; 18961 int prefer_self; /* when equal print only self time */ 18962 { 18963 int i; 18964 ufunc_T *fp; 18965 18966 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title); 18967 fprintf(fd, "count total (s) self (s) function\n"); 18968 for (i = 0; i < 20 && i < st_len; ++i) 18969 { 18970 fp = sorttab[i]; 18971 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self, 18972 prefer_self); 18973 if (fp->uf_name[0] == K_SPECIAL) 18974 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3); 18975 else 18976 fprintf(fd, " %s()\n", fp->uf_name); 18977 } 18978 fprintf(fd, "\n"); 18979 } 18980 18981 /* 18982 * Print the count and times for one function or function line. 18983 */ 18984 static void 18985 prof_func_line(fd, count, total, self, prefer_self) 18986 FILE *fd; 18987 int count; 18988 proftime_T *total; 18989 proftime_T *self; 18990 int prefer_self; /* when equal print only self time */ 18991 { 18992 if (count > 0) 18993 { 18994 fprintf(fd, "%5d ", count); 18995 if (prefer_self && profile_equal(total, self)) 18996 fprintf(fd, " "); 18997 else 18998 fprintf(fd, "%s ", profile_msg(total)); 18999 if (!prefer_self && profile_equal(total, self)) 19000 fprintf(fd, " "); 19001 else 19002 fprintf(fd, "%s ", profile_msg(self)); 19003 } 19004 else 19005 fprintf(fd, " "); 19006 } 19007 19008 /* 19009 * Compare function for total time sorting. 19010 */ 19011 static int 19012 #ifdef __BORLANDC__ 19013 _RTLENTRYF 19014 #endif 19015 prof_total_cmp(s1, s2) 19016 const void *s1; 19017 const void *s2; 19018 { 19019 ufunc_T *p1, *p2; 19020 19021 p1 = *(ufunc_T **)s1; 19022 p2 = *(ufunc_T **)s2; 19023 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total); 19024 } 19025 19026 /* 19027 * Compare function for self time sorting. 19028 */ 19029 static int 19030 #ifdef __BORLANDC__ 19031 _RTLENTRYF 19032 #endif 19033 prof_self_cmp(s1, s2) 19034 const void *s1; 19035 const void *s2; 19036 { 19037 ufunc_T *p1, *p2; 19038 19039 p1 = *(ufunc_T **)s1; 19040 p2 = *(ufunc_T **)s2; 19041 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self); 19042 } 19043 19044 #endif 19045 19046 /* The names of packages that once were loaded is remembered. */ 19047 static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; 19048 19049 /* 19050 * If "name" has a package name try autoloading the script for it. 19051 * Return TRUE if a package was loaded. 19052 */ 19053 static int 19054 script_autoload(name, reload) 19055 char_u *name; 19056 int reload; /* load script again when already loaded */ 19057 { 19058 char_u *p; 19059 char_u *scriptname, *tofree; 19060 int ret = FALSE; 19061 int i; 19062 19063 /* If there is no '#' after name[0] there is no package name. */ 19064 p = vim_strchr(name, AUTOLOAD_CHAR); 19065 if (p == NULL || p == name) 19066 return FALSE; 19067 19068 tofree = scriptname = autoload_name(name); 19069 19070 /* Find the name in the list of previously loaded package names. Skip 19071 * "autoload/", it's always the same. */ 19072 for (i = 0; i < ga_loaded.ga_len; ++i) 19073 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0) 19074 break; 19075 if (!reload && i < ga_loaded.ga_len) 19076 ret = FALSE; /* was loaded already */ 19077 else 19078 { 19079 /* Remember the name if it wasn't loaded already. */ 19080 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK) 19081 { 19082 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname; 19083 tofree = NULL; 19084 } 19085 19086 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */ 19087 if (source_runtime(scriptname, FALSE) == OK) 19088 ret = TRUE; 19089 } 19090 19091 vim_free(tofree); 19092 return ret; 19093 } 19094 19095 /* 19096 * Return the autoload script name for a function or variable name. 19097 * Returns NULL when out of memory. 19098 */ 19099 static char_u * 19100 autoload_name(name) 19101 char_u *name; 19102 { 19103 char_u *p; 19104 char_u *scriptname; 19105 19106 /* Get the script file name: replace '#' with '/', append ".vim". */ 19107 scriptname = alloc((unsigned)(STRLEN(name) + 14)); 19108 if (scriptname == NULL) 19109 return FALSE; 19110 STRCPY(scriptname, "autoload/"); 19111 STRCAT(scriptname, name); 19112 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL; 19113 STRCAT(scriptname, ".vim"); 19114 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL) 19115 *p = '/'; 19116 return scriptname; 19117 } 19118 19119 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 19120 19121 /* 19122 * Function given to ExpandGeneric() to obtain the list of user defined 19123 * function names. 19124 */ 19125 char_u * 19126 get_user_func_name(xp, idx) 19127 expand_T *xp; 19128 int idx; 19129 { 19130 static long_u done; 19131 static hashitem_T *hi; 19132 ufunc_T *fp; 19133 19134 if (idx == 0) 19135 { 19136 done = 0; 19137 hi = func_hashtab.ht_array; 19138 } 19139 if (done < func_hashtab.ht_used) 19140 { 19141 if (done++ > 0) 19142 ++hi; 19143 while (HASHITEM_EMPTY(hi)) 19144 ++hi; 19145 fp = HI2UF(hi); 19146 19147 if (STRLEN(fp->uf_name) + 4 >= IOSIZE) 19148 return fp->uf_name; /* prevents overflow */ 19149 19150 cat_func_name(IObuff, fp); 19151 if (xp->xp_context != EXPAND_USER_FUNC) 19152 { 19153 STRCAT(IObuff, "("); 19154 if (!fp->uf_varargs && fp->uf_args.ga_len == 0) 19155 STRCAT(IObuff, ")"); 19156 } 19157 return IObuff; 19158 } 19159 return NULL; 19160 } 19161 19162 #endif /* FEAT_CMDL_COMPL */ 19163 19164 /* 19165 * Copy the function name of "fp" to buffer "buf". 19166 * "buf" must be able to hold the function name plus three bytes. 19167 * Takes care of script-local function names. 19168 */ 19169 static void 19170 cat_func_name(buf, fp) 19171 char_u *buf; 19172 ufunc_T *fp; 19173 { 19174 if (fp->uf_name[0] == K_SPECIAL) 19175 { 19176 STRCPY(buf, "<SNR>"); 19177 STRCAT(buf, fp->uf_name + 3); 19178 } 19179 else 19180 STRCPY(buf, fp->uf_name); 19181 } 19182 19183 /* 19184 * ":delfunction {name}" 19185 */ 19186 void 19187 ex_delfunction(eap) 19188 exarg_T *eap; 19189 { 19190 ufunc_T *fp = NULL; 19191 char_u *p; 19192 char_u *name; 19193 funcdict_T fudi; 19194 19195 p = eap->arg; 19196 name = trans_function_name(&p, eap->skip, 0, &fudi); 19197 vim_free(fudi.fd_newkey); 19198 if (name == NULL) 19199 { 19200 if (fudi.fd_dict != NULL && !eap->skip) 19201 EMSG(_(e_funcref)); 19202 return; 19203 } 19204 if (!ends_excmd(*skipwhite(p))) 19205 { 19206 vim_free(name); 19207 EMSG(_(e_trailing)); 19208 return; 19209 } 19210 eap->nextcmd = check_nextcmd(p); 19211 if (eap->nextcmd != NULL) 19212 *p = NUL; 19213 19214 if (!eap->skip) 19215 fp = find_func(name); 19216 vim_free(name); 19217 19218 if (!eap->skip) 19219 { 19220 if (fp == NULL) 19221 { 19222 EMSG2(_(e_nofunc), eap->arg); 19223 return; 19224 } 19225 if (fp->uf_calls > 0) 19226 { 19227 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg); 19228 return; 19229 } 19230 19231 if (fudi.fd_dict != NULL) 19232 { 19233 /* Delete the dict item that refers to the function, it will 19234 * invoke func_unref() and possibly delete the function. */ 19235 dictitem_remove(fudi.fd_dict, fudi.fd_di); 19236 } 19237 else 19238 func_free(fp); 19239 } 19240 } 19241 19242 /* 19243 * Free a function and remove it from the list of functions. 19244 */ 19245 static void 19246 func_free(fp) 19247 ufunc_T *fp; 19248 { 19249 hashitem_T *hi; 19250 19251 /* clear this function */ 19252 ga_clear_strings(&(fp->uf_args)); 19253 ga_clear_strings(&(fp->uf_lines)); 19254 #ifdef FEAT_PROFILE 19255 vim_free(fp->uf_tml_count); 19256 vim_free(fp->uf_tml_total); 19257 vim_free(fp->uf_tml_self); 19258 #endif 19259 19260 /* remove the function from the function hashtable */ 19261 hi = hash_find(&func_hashtab, UF2HIKEY(fp)); 19262 if (HASHITEM_EMPTY(hi)) 19263 EMSG2(_(e_intern2), "func_free()"); 19264 else 19265 hash_remove(&func_hashtab, hi); 19266 19267 vim_free(fp); 19268 } 19269 19270 /* 19271 * Unreference a Function: decrement the reference count and free it when it 19272 * becomes zero. Only for numbered functions. 19273 */ 19274 static void 19275 func_unref(name) 19276 char_u *name; 19277 { 19278 ufunc_T *fp; 19279 19280 if (name != NULL && isdigit(*name)) 19281 { 19282 fp = find_func(name); 19283 if (fp == NULL) 19284 EMSG2(_(e_intern2), "func_unref()"); 19285 else if (--fp->uf_refcount <= 0) 19286 { 19287 /* Only delete it when it's not being used. Otherwise it's done 19288 * when "uf_calls" becomes zero. */ 19289 if (fp->uf_calls == 0) 19290 func_free(fp); 19291 } 19292 } 19293 } 19294 19295 /* 19296 * Count a reference to a Function. 19297 */ 19298 static void 19299 func_ref(name) 19300 char_u *name; 19301 { 19302 ufunc_T *fp; 19303 19304 if (name != NULL && isdigit(*name)) 19305 { 19306 fp = find_func(name); 19307 if (fp == NULL) 19308 EMSG2(_(e_intern2), "func_ref()"); 19309 else 19310 ++fp->uf_refcount; 19311 } 19312 } 19313 19314 /* 19315 * Call a user function. 19316 */ 19317 static void 19318 call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict) 19319 ufunc_T *fp; /* pointer to function */ 19320 int argcount; /* nr of args */ 19321 typval_T *argvars; /* arguments */ 19322 typval_T *rettv; /* return value */ 19323 linenr_T firstline; /* first line of range */ 19324 linenr_T lastline; /* last line of range */ 19325 dict_T *selfdict; /* Dictionary for "self" */ 19326 { 19327 char_u *save_sourcing_name; 19328 linenr_T save_sourcing_lnum; 19329 scid_T save_current_SID; 19330 funccall_T fc; 19331 int save_did_emsg; 19332 static int depth = 0; 19333 dictitem_T *v; 19334 int fixvar_idx = 0; /* index in fixvar[] */ 19335 int i; 19336 int ai; 19337 char_u numbuf[NUMBUFLEN]; 19338 char_u *name; 19339 #ifdef FEAT_PROFILE 19340 proftime_T wait_start; 19341 #endif 19342 19343 /* If depth of calling is getting too high, don't execute the function */ 19344 if (depth >= p_mfd) 19345 { 19346 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'")); 19347 rettv->v_type = VAR_NUMBER; 19348 rettv->vval.v_number = -1; 19349 return; 19350 } 19351 ++depth; 19352 19353 line_breakcheck(); /* check for CTRL-C hit */ 19354 19355 fc.caller = current_funccal; 19356 current_funccal = &fc; 19357 fc.func = fp; 19358 fc.rettv = rettv; 19359 rettv->vval.v_number = 0; 19360 fc.linenr = 0; 19361 fc.returned = FALSE; 19362 fc.level = ex_nesting_level; 19363 /* Check if this function has a breakpoint. */ 19364 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0); 19365 fc.dbg_tick = debug_tick; 19366 19367 /* 19368 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables 19369 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free 19370 * each argument variable and saves a lot of time. 19371 */ 19372 /* 19373 * Init l: variables. 19374 */ 19375 init_var_dict(&fc.l_vars, &fc.l_vars_var); 19376 if (selfdict != NULL) 19377 { 19378 /* Set l:self to "selfdict". Use "name" to avoid a warning from 19379 * some compiler that checks the destination size. */ 19380 v = &fc.fixvar[fixvar_idx++].var; 19381 name = v->di_key; 19382 STRCPY(name, "self"); 19383 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX; 19384 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v)); 19385 v->di_tv.v_type = VAR_DICT; 19386 v->di_tv.v_lock = 0; 19387 v->di_tv.vval.v_dict = selfdict; 19388 ++selfdict->dv_refcount; 19389 } 19390 19391 /* 19392 * Init a: variables. 19393 * Set a:0 to "argcount". 19394 * Set a:000 to a list with room for the "..." arguments. 19395 */ 19396 init_var_dict(&fc.l_avars, &fc.l_avars_var); 19397 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0", 19398 (varnumber_T)(argcount - fp->uf_args.ga_len)); 19399 v = &fc.fixvar[fixvar_idx++].var; 19400 STRCPY(v->di_key, "000"); 19401 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 19402 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v)); 19403 v->di_tv.v_type = VAR_LIST; 19404 v->di_tv.v_lock = VAR_FIXED; 19405 v->di_tv.vval.v_list = &fc.l_varlist; 19406 vim_memset(&fc.l_varlist, 0, sizeof(list_T)); 19407 fc.l_varlist.lv_refcount = 99999; 19408 19409 /* 19410 * Set a:firstline to "firstline" and a:lastline to "lastline". 19411 * Set a:name to named arguments. 19412 * Set a:N to the "..." arguments. 19413 */ 19414 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline", 19415 (varnumber_T)firstline); 19416 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline", 19417 (varnumber_T)lastline); 19418 for (i = 0; i < argcount; ++i) 19419 { 19420 ai = i - fp->uf_args.ga_len; 19421 if (ai < 0) 19422 /* named argument a:name */ 19423 name = FUNCARG(fp, i); 19424 else 19425 { 19426 /* "..." argument a:1, a:2, etc. */ 19427 sprintf((char *)numbuf, "%d", ai + 1); 19428 name = numbuf; 19429 } 19430 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN) 19431 { 19432 v = &fc.fixvar[fixvar_idx++].var; 19433 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 19434 } 19435 else 19436 { 19437 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) 19438 + STRLEN(name))); 19439 if (v == NULL) 19440 break; 19441 v->di_flags = DI_FLAGS_RO; 19442 } 19443 STRCPY(v->di_key, name); 19444 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v)); 19445 19446 /* Note: the values are copied directly to avoid alloc/free. 19447 * "argvars" must have VAR_FIXED for v_lock. */ 19448 v->di_tv = argvars[i]; 19449 v->di_tv.v_lock = VAR_FIXED; 19450 19451 if (ai >= 0 && ai < MAX_FUNC_ARGS) 19452 { 19453 list_append(&fc.l_varlist, &fc.l_listitems[ai]); 19454 fc.l_listitems[ai].li_tv = argvars[i]; 19455 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED; 19456 } 19457 } 19458 19459 /* Don't redraw while executing the function. */ 19460 ++RedrawingDisabled; 19461 save_sourcing_name = sourcing_name; 19462 save_sourcing_lnum = sourcing_lnum; 19463 sourcing_lnum = 1; 19464 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0 19465 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13)); 19466 if (sourcing_name != NULL) 19467 { 19468 if (save_sourcing_name != NULL 19469 && STRNCMP(save_sourcing_name, "function ", 9) == 0) 19470 sprintf((char *)sourcing_name, "%s..", save_sourcing_name); 19471 else 19472 STRCPY(sourcing_name, "function "); 19473 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp); 19474 19475 if (p_verbose >= 12) 19476 { 19477 ++no_wait_return; 19478 verbose_enter_scroll(); 19479 19480 smsg((char_u *)_("calling %s"), sourcing_name); 19481 if (p_verbose >= 14) 19482 { 19483 char_u buf[MSG_BUF_LEN]; 19484 char_u numbuf[NUMBUFLEN]; 19485 char_u *tofree; 19486 19487 msg_puts((char_u *)"("); 19488 for (i = 0; i < argcount; ++i) 19489 { 19490 if (i > 0) 19491 msg_puts((char_u *)", "); 19492 if (argvars[i].v_type == VAR_NUMBER) 19493 msg_outnum((long)argvars[i].vval.v_number); 19494 else 19495 { 19496 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0), 19497 buf, MSG_BUF_CLEN); 19498 msg_puts(buf); 19499 vim_free(tofree); 19500 } 19501 } 19502 msg_puts((char_u *)")"); 19503 } 19504 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 19505 19506 verbose_leave_scroll(); 19507 --no_wait_return; 19508 } 19509 } 19510 #ifdef FEAT_PROFILE 19511 if (do_profiling == PROF_YES) 19512 { 19513 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL)) 19514 func_do_profile(fp); 19515 if (fp->uf_profiling 19516 || (fc.caller != NULL && &fc.caller->func->uf_profiling)) 19517 { 19518 ++fp->uf_tm_count; 19519 profile_start(&fp->uf_tm_start); 19520 profile_zero(&fp->uf_tm_children); 19521 } 19522 script_prof_save(&wait_start); 19523 } 19524 #endif 19525 19526 save_current_SID = current_SID; 19527 current_SID = fp->uf_script_ID; 19528 save_did_emsg = did_emsg; 19529 did_emsg = FALSE; 19530 19531 /* call do_cmdline() to execute the lines */ 19532 do_cmdline(NULL, get_func_line, (void *)&fc, 19533 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); 19534 19535 --RedrawingDisabled; 19536 19537 /* when the function was aborted because of an error, return -1 */ 19538 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN) 19539 { 19540 clear_tv(rettv); 19541 rettv->v_type = VAR_NUMBER; 19542 rettv->vval.v_number = -1; 19543 } 19544 19545 #ifdef FEAT_PROFILE 19546 if (do_profiling == PROF_YES && (fp->uf_profiling 19547 || (fc.caller != NULL && &fc.caller->func->uf_profiling))) 19548 { 19549 profile_end(&fp->uf_tm_start); 19550 profile_sub_wait(&wait_start, &fp->uf_tm_start); 19551 profile_add(&fp->uf_tm_total, &fp->uf_tm_start); 19552 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children); 19553 if (fc.caller != NULL && &fc.caller->func->uf_profiling) 19554 { 19555 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start); 19556 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start); 19557 } 19558 } 19559 #endif 19560 19561 /* when being verbose, mention the return value */ 19562 if (p_verbose >= 12) 19563 { 19564 ++no_wait_return; 19565 verbose_enter_scroll(); 19566 19567 if (aborting()) 19568 smsg((char_u *)_("%s aborted"), sourcing_name); 19569 else if (fc.rettv->v_type == VAR_NUMBER) 19570 smsg((char_u *)_("%s returning #%ld"), sourcing_name, 19571 (long)fc.rettv->vval.v_number); 19572 else 19573 { 19574 char_u buf[MSG_BUF_LEN]; 19575 char_u numbuf[NUMBUFLEN]; 19576 char_u *tofree; 19577 19578 /* The value may be very long. Skip the middle part, so that we 19579 * have some idea how it starts and ends. smsg() would always 19580 * truncate it at the end. */ 19581 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0), 19582 buf, MSG_BUF_CLEN); 19583 smsg((char_u *)_("%s returning %s"), sourcing_name, buf); 19584 vim_free(tofree); 19585 } 19586 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 19587 19588 verbose_leave_scroll(); 19589 --no_wait_return; 19590 } 19591 19592 vim_free(sourcing_name); 19593 sourcing_name = save_sourcing_name; 19594 sourcing_lnum = save_sourcing_lnum; 19595 current_SID = save_current_SID; 19596 #ifdef FEAT_PROFILE 19597 if (do_profiling == PROF_YES) 19598 script_prof_restore(&wait_start); 19599 #endif 19600 19601 if (p_verbose >= 12 && sourcing_name != NULL) 19602 { 19603 ++no_wait_return; 19604 verbose_enter_scroll(); 19605 19606 smsg((char_u *)_("continuing in %s"), sourcing_name); 19607 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 19608 19609 verbose_leave_scroll(); 19610 --no_wait_return; 19611 } 19612 19613 did_emsg |= save_did_emsg; 19614 current_funccal = fc.caller; 19615 19616 /* The a: variables typevals were not alloced, only free the allocated 19617 * variables. */ 19618 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE); 19619 19620 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */ 19621 --depth; 19622 } 19623 19624 /* 19625 * Add a number variable "name" to dict "dp" with value "nr". 19626 */ 19627 static void 19628 add_nr_var(dp, v, name, nr) 19629 dict_T *dp; 19630 dictitem_T *v; 19631 char *name; 19632 varnumber_T nr; 19633 { 19634 STRCPY(v->di_key, name); 19635 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; 19636 hash_add(&dp->dv_hashtab, DI2HIKEY(v)); 19637 v->di_tv.v_type = VAR_NUMBER; 19638 v->di_tv.v_lock = VAR_FIXED; 19639 v->di_tv.vval.v_number = nr; 19640 } 19641 19642 /* 19643 * ":return [expr]" 19644 */ 19645 void 19646 ex_return(eap) 19647 exarg_T *eap; 19648 { 19649 char_u *arg = eap->arg; 19650 typval_T rettv; 19651 int returning = FALSE; 19652 19653 if (current_funccal == NULL) 19654 { 19655 EMSG(_("E133: :return not inside a function")); 19656 return; 19657 } 19658 19659 if (eap->skip) 19660 ++emsg_skip; 19661 19662 eap->nextcmd = NULL; 19663 if ((*arg != NUL && *arg != '|' && *arg != '\n') 19664 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL) 19665 { 19666 if (!eap->skip) 19667 returning = do_return(eap, FALSE, TRUE, &rettv); 19668 else 19669 clear_tv(&rettv); 19670 } 19671 /* It's safer to return also on error. */ 19672 else if (!eap->skip) 19673 { 19674 /* 19675 * Return unless the expression evaluation has been cancelled due to an 19676 * aborting error, an interrupt, or an exception. 19677 */ 19678 if (!aborting()) 19679 returning = do_return(eap, FALSE, TRUE, NULL); 19680 } 19681 19682 /* When skipping or the return gets pending, advance to the next command 19683 * in this line (!returning). Otherwise, ignore the rest of the line. 19684 * Following lines will be ignored by get_func_line(). */ 19685 if (returning) 19686 eap->nextcmd = NULL; 19687 else if (eap->nextcmd == NULL) /* no argument */ 19688 eap->nextcmd = check_nextcmd(arg); 19689 19690 if (eap->skip) 19691 --emsg_skip; 19692 } 19693 19694 /* 19695 * Return from a function. Possibly makes the return pending. Also called 19696 * for a pending return at the ":endtry" or after returning from an extra 19697 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set 19698 * when called due to a ":return" command. "rettv" may point to a typval_T 19699 * with the return rettv. Returns TRUE when the return can be carried out, 19700 * FALSE when the return gets pending. 19701 */ 19702 int 19703 do_return(eap, reanimate, is_cmd, rettv) 19704 exarg_T *eap; 19705 int reanimate; 19706 int is_cmd; 19707 void *rettv; 19708 { 19709 int idx; 19710 struct condstack *cstack = eap->cstack; 19711 19712 if (reanimate) 19713 /* Undo the return. */ 19714 current_funccal->returned = FALSE; 19715 19716 /* 19717 * Cleanup (and inactivate) conditionals, but stop when a try conditional 19718 * not in its finally clause (which then is to be executed next) is found. 19719 * In this case, make the ":return" pending for execution at the ":endtry". 19720 * Otherwise, return normally. 19721 */ 19722 idx = cleanup_conditionals(eap->cstack, 0, TRUE); 19723 if (idx >= 0) 19724 { 19725 cstack->cs_pending[idx] = CSTP_RETURN; 19726 19727 if (!is_cmd && !reanimate) 19728 /* A pending return again gets pending. "rettv" points to an 19729 * allocated variable with the rettv of the original ":return"'s 19730 * argument if present or is NULL else. */ 19731 cstack->cs_rettv[idx] = rettv; 19732 else 19733 { 19734 /* When undoing a return in order to make it pending, get the stored 19735 * return rettv. */ 19736 if (reanimate) 19737 rettv = current_funccal->rettv; 19738 19739 if (rettv != NULL) 19740 { 19741 /* Store the value of the pending return. */ 19742 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL) 19743 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv; 19744 else 19745 EMSG(_(e_outofmem)); 19746 } 19747 else 19748 cstack->cs_rettv[idx] = NULL; 19749 19750 if (reanimate) 19751 { 19752 /* The pending return value could be overwritten by a ":return" 19753 * without argument in a finally clause; reset the default 19754 * return value. */ 19755 current_funccal->rettv->v_type = VAR_NUMBER; 19756 current_funccal->rettv->vval.v_number = 0; 19757 } 19758 } 19759 report_make_pending(CSTP_RETURN, rettv); 19760 } 19761 else 19762 { 19763 current_funccal->returned = TRUE; 19764 19765 /* If the return is carried out now, store the return value. For 19766 * a return immediately after reanimation, the value is already 19767 * there. */ 19768 if (!reanimate && rettv != NULL) 19769 { 19770 clear_tv(current_funccal->rettv); 19771 *current_funccal->rettv = *(typval_T *)rettv; 19772 if (!is_cmd) 19773 vim_free(rettv); 19774 } 19775 } 19776 19777 return idx < 0; 19778 } 19779 19780 /* 19781 * Free the variable with a pending return value. 19782 */ 19783 void 19784 discard_pending_return(rettv) 19785 void *rettv; 19786 { 19787 free_tv((typval_T *)rettv); 19788 } 19789 19790 /* 19791 * Generate a return command for producing the value of "rettv". The result 19792 * is an allocated string. Used by report_pending() for verbose messages. 19793 */ 19794 char_u * 19795 get_return_cmd(rettv) 19796 void *rettv; 19797 { 19798 char_u *s = NULL; 19799 char_u *tofree = NULL; 19800 char_u numbuf[NUMBUFLEN]; 19801 19802 if (rettv != NULL) 19803 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0); 19804 if (s == NULL) 19805 s = (char_u *)""; 19806 19807 STRCPY(IObuff, ":return "); 19808 STRNCPY(IObuff + 8, s, IOSIZE - 8); 19809 if (STRLEN(s) + 8 >= IOSIZE) 19810 STRCPY(IObuff + IOSIZE - 4, "..."); 19811 vim_free(tofree); 19812 return vim_strsave(IObuff); 19813 } 19814 19815 /* 19816 * Get next function line. 19817 * Called by do_cmdline() to get the next line. 19818 * Returns allocated string, or NULL for end of function. 19819 */ 19820 /* ARGSUSED */ 19821 char_u * 19822 get_func_line(c, cookie, indent) 19823 int c; /* not used */ 19824 void *cookie; 19825 int indent; /* not used */ 19826 { 19827 funccall_T *fcp = (funccall_T *)cookie; 19828 ufunc_T *fp = fcp->func; 19829 char_u *retval; 19830 garray_T *gap; /* growarray with function lines */ 19831 19832 /* If breakpoints have been added/deleted need to check for it. */ 19833 if (fcp->dbg_tick != debug_tick) 19834 { 19835 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, 19836 sourcing_lnum); 19837 fcp->dbg_tick = debug_tick; 19838 } 19839 #ifdef FEAT_PROFILE 19840 if (do_profiling == PROF_YES) 19841 func_line_end(cookie); 19842 #endif 19843 19844 gap = &fp->uf_lines; 19845 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) 19846 || fcp->returned) 19847 retval = NULL; 19848 else 19849 { 19850 /* Skip NULL lines (continuation lines). */ 19851 while (fcp->linenr < gap->ga_len 19852 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL) 19853 ++fcp->linenr; 19854 if (fcp->linenr >= gap->ga_len) 19855 retval = NULL; 19856 else 19857 { 19858 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]); 19859 sourcing_lnum = fcp->linenr; 19860 #ifdef FEAT_PROFILE 19861 if (do_profiling == PROF_YES) 19862 func_line_start(cookie); 19863 #endif 19864 } 19865 } 19866 19867 /* Did we encounter a breakpoint? */ 19868 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum) 19869 { 19870 dbg_breakpoint(fp->uf_name, sourcing_lnum); 19871 /* Find next breakpoint. */ 19872 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, 19873 sourcing_lnum); 19874 fcp->dbg_tick = debug_tick; 19875 } 19876 19877 return retval; 19878 } 19879 19880 #if defined(FEAT_PROFILE) || defined(PROTO) 19881 /* 19882 * Called when starting to read a function line. 19883 * "sourcing_lnum" must be correct! 19884 * When skipping lines it may not actually be executed, but we won't find out 19885 * until later and we need to store the time now. 19886 */ 19887 void 19888 func_line_start(cookie) 19889 void *cookie; 19890 { 19891 funccall_T *fcp = (funccall_T *)cookie; 19892 ufunc_T *fp = fcp->func; 19893 19894 if (fp->uf_profiling && sourcing_lnum >= 1 19895 && sourcing_lnum <= fp->uf_lines.ga_len) 19896 { 19897 fp->uf_tml_idx = sourcing_lnum - 1; 19898 /* Skip continuation lines. */ 19899 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL) 19900 --fp->uf_tml_idx; 19901 fp->uf_tml_execed = FALSE; 19902 profile_start(&fp->uf_tml_start); 19903 profile_zero(&fp->uf_tml_children); 19904 profile_get_wait(&fp->uf_tml_wait); 19905 } 19906 } 19907 19908 /* 19909 * Called when actually executing a function line. 19910 */ 19911 void 19912 func_line_exec(cookie) 19913 void *cookie; 19914 { 19915 funccall_T *fcp = (funccall_T *)cookie; 19916 ufunc_T *fp = fcp->func; 19917 19918 if (fp->uf_profiling && fp->uf_tml_idx >= 0) 19919 fp->uf_tml_execed = TRUE; 19920 } 19921 19922 /* 19923 * Called when done with a function line. 19924 */ 19925 void 19926 func_line_end(cookie) 19927 void *cookie; 19928 { 19929 funccall_T *fcp = (funccall_T *)cookie; 19930 ufunc_T *fp = fcp->func; 19931 19932 if (fp->uf_profiling && fp->uf_tml_idx >= 0) 19933 { 19934 if (fp->uf_tml_execed) 19935 { 19936 ++fp->uf_tml_count[fp->uf_tml_idx]; 19937 profile_end(&fp->uf_tml_start); 19938 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start); 19939 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start); 19940 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start, 19941 &fp->uf_tml_children); 19942 } 19943 fp->uf_tml_idx = -1; 19944 } 19945 } 19946 #endif 19947 19948 /* 19949 * Return TRUE if the currently active function should be ended, because a 19950 * return was encountered or an error occured. Used inside a ":while". 19951 */ 19952 int 19953 func_has_ended(cookie) 19954 void *cookie; 19955 { 19956 funccall_T *fcp = (funccall_T *)cookie; 19957 19958 /* Ignore the "abort" flag if the abortion behavior has been changed due to 19959 * an error inside a try conditional. */ 19960 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try()) 19961 || fcp->returned); 19962 } 19963 19964 /* 19965 * return TRUE if cookie indicates a function which "abort"s on errors. 19966 */ 19967 int 19968 func_has_abort(cookie) 19969 void *cookie; 19970 { 19971 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT; 19972 } 19973 19974 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION) 19975 typedef enum 19976 { 19977 VAR_FLAVOUR_DEFAULT, 19978 VAR_FLAVOUR_SESSION, 19979 VAR_FLAVOUR_VIMINFO 19980 } var_flavour_T; 19981 19982 static var_flavour_T var_flavour __ARGS((char_u *varname)); 19983 19984 static var_flavour_T 19985 var_flavour(varname) 19986 char_u *varname; 19987 { 19988 char_u *p = varname; 19989 19990 if (ASCII_ISUPPER(*p)) 19991 { 19992 while (*(++p)) 19993 if (ASCII_ISLOWER(*p)) 19994 return VAR_FLAVOUR_SESSION; 19995 return VAR_FLAVOUR_VIMINFO; 19996 } 19997 else 19998 return VAR_FLAVOUR_DEFAULT; 19999 } 20000 #endif 20001 20002 #if defined(FEAT_VIMINFO) || defined(PROTO) 20003 /* 20004 * Restore global vars that start with a capital from the viminfo file 20005 */ 20006 int 20007 read_viminfo_varlist(virp, writing) 20008 vir_T *virp; 20009 int writing; 20010 { 20011 char_u *tab; 20012 int is_string = FALSE; 20013 typval_T tv; 20014 20015 if (!writing && (find_viminfo_parameter('!') != NULL)) 20016 { 20017 tab = vim_strchr(virp->vir_line + 1, '\t'); 20018 if (tab != NULL) 20019 { 20020 *tab++ = '\0'; /* isolate the variable name */ 20021 if (*tab == 'S') /* string var */ 20022 is_string = TRUE; 20023 20024 tab = vim_strchr(tab, '\t'); 20025 if (tab != NULL) 20026 { 20027 if (is_string) 20028 { 20029 tv.v_type = VAR_STRING; 20030 tv.vval.v_string = viminfo_readstring(virp, 20031 (int)(tab - virp->vir_line + 1), TRUE); 20032 } 20033 else 20034 { 20035 tv.v_type = VAR_NUMBER; 20036 tv.vval.v_number = atol((char *)tab + 1); 20037 } 20038 set_var(virp->vir_line + 1, &tv, FALSE); 20039 if (is_string) 20040 vim_free(tv.vval.v_string); 20041 } 20042 } 20043 } 20044 20045 return viminfo_readline(virp); 20046 } 20047 20048 /* 20049 * Write global vars that start with a capital to the viminfo file 20050 */ 20051 void 20052 write_viminfo_varlist(fp) 20053 FILE *fp; 20054 { 20055 hashitem_T *hi; 20056 dictitem_T *this_var; 20057 int todo; 20058 char *s; 20059 char_u *p; 20060 char_u *tofree; 20061 char_u numbuf[NUMBUFLEN]; 20062 20063 if (find_viminfo_parameter('!') == NULL) 20064 return; 20065 20066 fprintf(fp, _("\n# global variables:\n")); 20067 20068 todo = globvarht.ht_used; 20069 for (hi = globvarht.ht_array; todo > 0; ++hi) 20070 { 20071 if (!HASHITEM_EMPTY(hi)) 20072 { 20073 --todo; 20074 this_var = HI2DI(hi); 20075 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO) 20076 { 20077 switch (this_var->di_tv.v_type) 20078 { 20079 case VAR_STRING: s = "STR"; break; 20080 case VAR_NUMBER: s = "NUM"; break; 20081 default: continue; 20082 } 20083 fprintf(fp, "!%s\t%s\t", this_var->di_key, s); 20084 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0); 20085 if (p != NULL) 20086 viminfo_writestring(fp, p); 20087 vim_free(tofree); 20088 } 20089 } 20090 } 20091 } 20092 #endif 20093 20094 #if defined(FEAT_SESSION) || defined(PROTO) 20095 int 20096 store_session_globals(fd) 20097 FILE *fd; 20098 { 20099 hashitem_T *hi; 20100 dictitem_T *this_var; 20101 int todo; 20102 char_u *p, *t; 20103 20104 todo = globvarht.ht_used; 20105 for (hi = globvarht.ht_array; todo > 0; ++hi) 20106 { 20107 if (!HASHITEM_EMPTY(hi)) 20108 { 20109 --todo; 20110 this_var = HI2DI(hi); 20111 if ((this_var->di_tv.v_type == VAR_NUMBER 20112 || this_var->di_tv.v_type == VAR_STRING) 20113 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION) 20114 { 20115 /* Escape special characters with a backslash. Turn a LF and 20116 * CR into \n and \r. */ 20117 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv), 20118 (char_u *)"\\\"\n\r"); 20119 if (p == NULL) /* out of memory */ 20120 break; 20121 for (t = p; *t != NUL; ++t) 20122 if (*t == '\n') 20123 *t = 'n'; 20124 else if (*t == '\r') 20125 *t = 'r'; 20126 if ((fprintf(fd, "let %s = %c%s%c", 20127 this_var->di_key, 20128 (this_var->di_tv.v_type == VAR_STRING) ? '"' 20129 : ' ', 20130 p, 20131 (this_var->di_tv.v_type == VAR_STRING) ? '"' 20132 : ' ') < 0) 20133 || put_eol(fd) == FAIL) 20134 { 20135 vim_free(p); 20136 return FAIL; 20137 } 20138 vim_free(p); 20139 } 20140 } 20141 } 20142 return OK; 20143 } 20144 #endif 20145 20146 /* 20147 * Display script name where an item was last set. 20148 * Should only be invoked when 'verbose' is non-zero. 20149 */ 20150 void 20151 last_set_msg(scriptID) 20152 scid_T scriptID; 20153 { 20154 char_u *p; 20155 20156 if (scriptID != 0) 20157 { 20158 p = home_replace_save(NULL, get_scriptname(scriptID)); 20159 if (p != NULL) 20160 { 20161 verbose_enter(); 20162 MSG_PUTS(_("\n\tLast set from ")); 20163 MSG_PUTS(p); 20164 vim_free(p); 20165 verbose_leave(); 20166 } 20167 } 20168 } 20169 20170 #endif /* FEAT_EVAL */ 20171 20172 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) 20173 20174 20175 #ifdef WIN3264 20176 /* 20177 * Functions for ":8" filename modifier: get 8.3 version of a filename. 20178 */ 20179 static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen)); 20180 static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen)); 20181 static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen)); 20182 20183 /* 20184 * Get the short pathname of a file. 20185 * Returns 1 on success. *fnamelen is 0 for nonexistant path. 20186 */ 20187 static int 20188 get_short_pathname(fnamep, bufp, fnamelen) 20189 char_u **fnamep; 20190 char_u **bufp; 20191 int *fnamelen; 20192 { 20193 int l,len; 20194 char_u *newbuf; 20195 20196 len = *fnamelen; 20197 20198 l = GetShortPathName(*fnamep, *fnamep, len); 20199 if (l > len - 1) 20200 { 20201 /* If that doesn't work (not enough space), then save the string 20202 * and try again with a new buffer big enough 20203 */ 20204 newbuf = vim_strnsave(*fnamep, l); 20205 if (newbuf == NULL) 20206 return 0; 20207 20208 vim_free(*bufp); 20209 *fnamep = *bufp = newbuf; 20210 20211 l = GetShortPathName(*fnamep,*fnamep,l+1); 20212 20213 /* Really should always succeed, as the buffer is big enough */ 20214 } 20215 20216 *fnamelen = l; 20217 return 1; 20218 } 20219 20220 /* 20221 * Create a short path name. Returns the length of the buffer it needs. 20222 * Doesn't copy over the end of the buffer passed in. 20223 */ 20224 static int 20225 shortpath_for_invalid_fname(fname, bufp, fnamelen) 20226 char_u **fname; 20227 char_u **bufp; 20228 int *fnamelen; 20229 { 20230 char_u *s, *p, *pbuf2, *pbuf3; 20231 char_u ch; 20232 int len, len2, plen, slen; 20233 20234 /* Make a copy */ 20235 len2 = *fnamelen; 20236 pbuf2 = vim_strnsave(*fname, len2); 20237 pbuf3 = NULL; 20238 20239 s = pbuf2 + len2 - 1; /* Find the end */ 20240 slen = 1; 20241 plen = len2; 20242 20243 if (after_pathsep(pbuf2, s + 1)) 20244 { 20245 --s; 20246 ++slen; 20247 --plen; 20248 } 20249 20250 do 20251 { 20252 /* Go back one path-seperator */ 20253 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1)) 20254 { 20255 --s; 20256 ++slen; 20257 --plen; 20258 } 20259 if (s <= pbuf2) 20260 break; 20261 20262 /* Remeber the character that is about to be blatted */ 20263 ch = *s; 20264 *s = 0; /* get_short_pathname requires a null-terminated string */ 20265 20266 /* Try it in situ */ 20267 p = pbuf2; 20268 if (!get_short_pathname(&p, &pbuf3, &plen)) 20269 { 20270 vim_free(pbuf2); 20271 return -1; 20272 } 20273 *s = ch; /* Preserve the string */ 20274 } while (plen == 0); 20275 20276 if (plen > 0) 20277 { 20278 /* Remeber the length of the new string. */ 20279 *fnamelen = len = plen + slen; 20280 vim_free(*bufp); 20281 if (len > len2) 20282 { 20283 /* If there's not enough space in the currently allocated string, 20284 * then copy it to a buffer big enough. 20285 */ 20286 *fname= *bufp = vim_strnsave(p, len); 20287 if (*fname == NULL) 20288 return -1; 20289 } 20290 else 20291 { 20292 /* Transfer pbuf2 to being the main buffer (it's big enough) */ 20293 *fname = *bufp = pbuf2; 20294 if (p != pbuf2) 20295 strncpy(*fname, p, plen); 20296 pbuf2 = NULL; 20297 } 20298 /* Concat the next bit */ 20299 strncpy(*fname + plen, s, slen); 20300 (*fname)[len] = '\0'; 20301 } 20302 vim_free(pbuf3); 20303 vim_free(pbuf2); 20304 return 0; 20305 } 20306 20307 /* 20308 * Get a pathname for a partial path. 20309 */ 20310 static int 20311 shortpath_for_partial(fnamep, bufp, fnamelen) 20312 char_u **fnamep; 20313 char_u **bufp; 20314 int *fnamelen; 20315 { 20316 int sepcount, len, tflen; 20317 char_u *p; 20318 char_u *pbuf, *tfname; 20319 int hasTilde; 20320 20321 /* Count up the path seperators from the RHS.. so we know which part 20322 * of the path to return. 20323 */ 20324 sepcount = 0; 20325 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p)) 20326 if (vim_ispathsep(*p)) 20327 ++sepcount; 20328 20329 /* Need full path first (use expand_env() to remove a "~/") */ 20330 hasTilde = (**fnamep == '~'); 20331 if (hasTilde) 20332 pbuf = tfname = expand_env_save(*fnamep); 20333 else 20334 pbuf = tfname = FullName_save(*fnamep, FALSE); 20335 20336 len = tflen = STRLEN(tfname); 20337 20338 if (!get_short_pathname(&tfname, &pbuf, &len)) 20339 return -1; 20340 20341 if (len == 0) 20342 { 20343 /* Don't have a valid filename, so shorten the rest of the 20344 * path if we can. This CAN give us invalid 8.3 filenames, but 20345 * there's not a lot of point in guessing what it might be. 20346 */ 20347 len = tflen; 20348 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1) 20349 return -1; 20350 } 20351 20352 /* Count the paths backward to find the beginning of the desired string. */ 20353 for (p = tfname + len - 1; p >= tfname; --p) 20354 { 20355 #ifdef FEAT_MBYTE 20356 if (has_mbyte) 20357 p -= mb_head_off(tfname, p); 20358 #endif 20359 if (vim_ispathsep(*p)) 20360 { 20361 if (sepcount == 0 || (hasTilde && sepcount == 1)) 20362 break; 20363 else 20364 sepcount --; 20365 } 20366 } 20367 if (hasTilde) 20368 { 20369 --p; 20370 if (p >= tfname) 20371 *p = '~'; 20372 else 20373 return -1; 20374 } 20375 else 20376 ++p; 20377 20378 /* Copy in the string - p indexes into tfname - allocated at pbuf */ 20379 vim_free(*bufp); 20380 *fnamelen = (int)STRLEN(p); 20381 *bufp = pbuf; 20382 *fnamep = p; 20383 20384 return 0; 20385 } 20386 #endif /* WIN3264 */ 20387 20388 /* 20389 * Adjust a filename, according to a string of modifiers. 20390 * *fnamep must be NUL terminated when called. When returning, the length is 20391 * determined by *fnamelen. 20392 * Returns valid flags. 20393 * When there is an error, *fnamep is set to NULL. 20394 */ 20395 int 20396 modify_fname(src, usedlen, fnamep, bufp, fnamelen) 20397 char_u *src; /* string with modifiers */ 20398 int *usedlen; /* characters after src that are used */ 20399 char_u **fnamep; /* file name so far */ 20400 char_u **bufp; /* buffer for allocated file name or NULL */ 20401 int *fnamelen; /* length of fnamep */ 20402 { 20403 int valid = 0; 20404 char_u *tail; 20405 char_u *s, *p, *pbuf; 20406 char_u dirname[MAXPATHL]; 20407 int c; 20408 int has_fullname = 0; 20409 #ifdef WIN3264 20410 int has_shortname = 0; 20411 #endif 20412 20413 repeat: 20414 /* ":p" - full path/file_name */ 20415 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') 20416 { 20417 has_fullname = 1; 20418 20419 valid |= VALID_PATH; 20420 *usedlen += 2; 20421 20422 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */ 20423 if ((*fnamep)[0] == '~' 20424 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME)) 20425 && ((*fnamep)[1] == '/' 20426 # ifdef BACKSLASH_IN_FILENAME 20427 || (*fnamep)[1] == '\\' 20428 # endif 20429 || (*fnamep)[1] == NUL) 20430 20431 #endif 20432 ) 20433 { 20434 *fnamep = expand_env_save(*fnamep); 20435 vim_free(*bufp); /* free any allocated file name */ 20436 *bufp = *fnamep; 20437 if (*fnamep == NULL) 20438 return -1; 20439 } 20440 20441 /* When "/." or "/.." is used: force expansion to get rid of it. */ 20442 for (p = *fnamep; *p != NUL; mb_ptr_adv(p)) 20443 { 20444 if (vim_ispathsep(*p) 20445 && p[1] == '.' 20446 && (p[2] == NUL 20447 || vim_ispathsep(p[2]) 20448 || (p[2] == '.' 20449 && (p[3] == NUL || vim_ispathsep(p[3]))))) 20450 break; 20451 } 20452 20453 /* FullName_save() is slow, don't use it when not needed. */ 20454 if (*p != NUL || !vim_isAbsName(*fnamep)) 20455 { 20456 *fnamep = FullName_save(*fnamep, *p != NUL); 20457 vim_free(*bufp); /* free any allocated file name */ 20458 *bufp = *fnamep; 20459 if (*fnamep == NULL) 20460 return -1; 20461 } 20462 20463 /* Append a path separator to a directory. */ 20464 if (mch_isdir(*fnamep)) 20465 { 20466 /* Make room for one or two extra characters. */ 20467 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2); 20468 vim_free(*bufp); /* free any allocated file name */ 20469 *bufp = *fnamep; 20470 if (*fnamep == NULL) 20471 return -1; 20472 add_pathsep(*fnamep); 20473 } 20474 } 20475 20476 /* ":." - path relative to the current directory */ 20477 /* ":~" - path relative to the home directory */ 20478 /* ":8" - shortname path - postponed till after */ 20479 while (src[*usedlen] == ':' 20480 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8')) 20481 { 20482 *usedlen += 2; 20483 if (c == '8') 20484 { 20485 #ifdef WIN3264 20486 has_shortname = 1; /* Postpone this. */ 20487 #endif 20488 continue; 20489 } 20490 pbuf = NULL; 20491 /* Need full path first (use expand_env() to remove a "~/") */ 20492 if (!has_fullname) 20493 { 20494 if (c == '.' && **fnamep == '~') 20495 p = pbuf = expand_env_save(*fnamep); 20496 else 20497 p = pbuf = FullName_save(*fnamep, FALSE); 20498 } 20499 else 20500 p = *fnamep; 20501 20502 has_fullname = 0; 20503 20504 if (p != NULL) 20505 { 20506 if (c == '.') 20507 { 20508 mch_dirname(dirname, MAXPATHL); 20509 s = shorten_fname(p, dirname); 20510 if (s != NULL) 20511 { 20512 *fnamep = s; 20513 if (pbuf != NULL) 20514 { 20515 vim_free(*bufp); /* free any allocated file name */ 20516 *bufp = pbuf; 20517 pbuf = NULL; 20518 } 20519 } 20520 } 20521 else 20522 { 20523 home_replace(NULL, p, dirname, MAXPATHL, TRUE); 20524 /* Only replace it when it starts with '~' */ 20525 if (*dirname == '~') 20526 { 20527 s = vim_strsave(dirname); 20528 if (s != NULL) 20529 { 20530 *fnamep = s; 20531 vim_free(*bufp); 20532 *bufp = s; 20533 } 20534 } 20535 } 20536 vim_free(pbuf); 20537 } 20538 } 20539 20540 tail = gettail(*fnamep); 20541 *fnamelen = (int)STRLEN(*fnamep); 20542 20543 /* ":h" - head, remove "/file_name", can be repeated */ 20544 /* Don't remove the first "/" or "c:\" */ 20545 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h') 20546 { 20547 valid |= VALID_HEAD; 20548 *usedlen += 2; 20549 s = get_past_head(*fnamep); 20550 while (tail > s && after_pathsep(s, tail)) 20551 --tail; 20552 *fnamelen = (int)(tail - *fnamep); 20553 #ifdef VMS 20554 if (*fnamelen > 0) 20555 *fnamelen += 1; /* the path separator is part of the path */ 20556 #endif 20557 while (tail > s && !after_pathsep(s, tail)) 20558 mb_ptr_back(*fnamep, tail); 20559 } 20560 20561 /* ":8" - shortname */ 20562 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8') 20563 { 20564 *usedlen += 2; 20565 #ifdef WIN3264 20566 has_shortname = 1; 20567 #endif 20568 } 20569 20570 #ifdef WIN3264 20571 /* Check shortname after we have done 'heads' and before we do 'tails' 20572 */ 20573 if (has_shortname) 20574 { 20575 pbuf = NULL; 20576 /* Copy the string if it is shortened by :h */ 20577 if (*fnamelen < (int)STRLEN(*fnamep)) 20578 { 20579 p = vim_strnsave(*fnamep, *fnamelen); 20580 if (p == 0) 20581 return -1; 20582 vim_free(*bufp); 20583 *bufp = *fnamep = p; 20584 } 20585 20586 /* Split into two implementations - makes it easier. First is where 20587 * there isn't a full name already, second is where there is. 20588 */ 20589 if (!has_fullname && !vim_isAbsName(*fnamep)) 20590 { 20591 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1) 20592 return -1; 20593 } 20594 else 20595 { 20596 int l; 20597 20598 /* Simple case, already have the full-name 20599 * Nearly always shorter, so try first time. */ 20600 l = *fnamelen; 20601 if (!get_short_pathname(fnamep, bufp, &l)) 20602 return -1; 20603 20604 if (l == 0) 20605 { 20606 /* Couldn't find the filename.. search the paths. 20607 */ 20608 l = *fnamelen; 20609 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1) 20610 return -1; 20611 } 20612 *fnamelen = l; 20613 } 20614 } 20615 #endif /* WIN3264 */ 20616 20617 /* ":t" - tail, just the basename */ 20618 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't') 20619 { 20620 *usedlen += 2; 20621 *fnamelen -= (int)(tail - *fnamep); 20622 *fnamep = tail; 20623 } 20624 20625 /* ":e" - extension, can be repeated */ 20626 /* ":r" - root, without extension, can be repeated */ 20627 while (src[*usedlen] == ':' 20628 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r')) 20629 { 20630 /* find a '.' in the tail: 20631 * - for second :e: before the current fname 20632 * - otherwise: The last '.' 20633 */ 20634 if (src[*usedlen + 1] == 'e' && *fnamep > tail) 20635 s = *fnamep - 2; 20636 else 20637 s = *fnamep + *fnamelen - 1; 20638 for ( ; s > tail; --s) 20639 if (s[0] == '.') 20640 break; 20641 if (src[*usedlen + 1] == 'e') /* :e */ 20642 { 20643 if (s > tail) 20644 { 20645 *fnamelen += (int)(*fnamep - (s + 1)); 20646 *fnamep = s + 1; 20647 #ifdef VMS 20648 /* cut version from the extension */ 20649 s = *fnamep + *fnamelen - 1; 20650 for ( ; s > *fnamep; --s) 20651 if (s[0] == ';') 20652 break; 20653 if (s > *fnamep) 20654 *fnamelen = s - *fnamep; 20655 #endif 20656 } 20657 else if (*fnamep <= tail) 20658 *fnamelen = 0; 20659 } 20660 else /* :r */ 20661 { 20662 if (s > tail) /* remove one extension */ 20663 *fnamelen = (int)(s - *fnamep); 20664 } 20665 *usedlen += 2; 20666 } 20667 20668 /* ":s?pat?foo?" - substitute */ 20669 /* ":gs?pat?foo?" - global substitute */ 20670 if (src[*usedlen] == ':' 20671 && (src[*usedlen + 1] == 's' 20672 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's'))) 20673 { 20674 char_u *str; 20675 char_u *pat; 20676 char_u *sub; 20677 int sep; 20678 char_u *flags; 20679 int didit = FALSE; 20680 20681 flags = (char_u *)""; 20682 s = src + *usedlen + 2; 20683 if (src[*usedlen + 1] == 'g') 20684 { 20685 flags = (char_u *)"g"; 20686 ++s; 20687 } 20688 20689 sep = *s++; 20690 if (sep) 20691 { 20692 /* find end of pattern */ 20693 p = vim_strchr(s, sep); 20694 if (p != NULL) 20695 { 20696 pat = vim_strnsave(s, (int)(p - s)); 20697 if (pat != NULL) 20698 { 20699 s = p + 1; 20700 /* find end of substitution */ 20701 p = vim_strchr(s, sep); 20702 if (p != NULL) 20703 { 20704 sub = vim_strnsave(s, (int)(p - s)); 20705 str = vim_strnsave(*fnamep, *fnamelen); 20706 if (sub != NULL && str != NULL) 20707 { 20708 *usedlen = (int)(p + 1 - src); 20709 s = do_string_sub(str, pat, sub, flags); 20710 if (s != NULL) 20711 { 20712 *fnamep = s; 20713 *fnamelen = (int)STRLEN(s); 20714 vim_free(*bufp); 20715 *bufp = s; 20716 didit = TRUE; 20717 } 20718 } 20719 vim_free(sub); 20720 vim_free(str); 20721 } 20722 vim_free(pat); 20723 } 20724 } 20725 /* after using ":s", repeat all the modifiers */ 20726 if (didit) 20727 goto repeat; 20728 } 20729 } 20730 20731 return valid; 20732 } 20733 20734 /* 20735 * Perform a substitution on "str" with pattern "pat" and substitute "sub". 20736 * "flags" can be "g" to do a global substitute. 20737 * Returns an allocated string, NULL for error. 20738 */ 20739 char_u * 20740 do_string_sub(str, pat, sub, flags) 20741 char_u *str; 20742 char_u *pat; 20743 char_u *sub; 20744 char_u *flags; 20745 { 20746 int sublen; 20747 regmatch_T regmatch; 20748 int i; 20749 int do_all; 20750 char_u *tail; 20751 garray_T ga; 20752 char_u *ret; 20753 char_u *save_cpo; 20754 20755 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */ 20756 save_cpo = p_cpo; 20757 p_cpo = (char_u *)""; 20758 20759 ga_init2(&ga, 1, 200); 20760 20761 do_all = (flags[0] == 'g'); 20762 20763 regmatch.rm_ic = p_ic; 20764 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 20765 if (regmatch.regprog != NULL) 20766 { 20767 tail = str; 20768 while (vim_regexec_nl(®match, str, (colnr_T)(tail - str))) 20769 { 20770 /* 20771 * Get some space for a temporary buffer to do the substitution 20772 * into. It will contain: 20773 * - The text up to where the match is. 20774 * - The substituted text. 20775 * - The text after the match. 20776 */ 20777 sublen = vim_regsub(®match, sub, tail, FALSE, TRUE, FALSE); 20778 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen - 20779 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL) 20780 { 20781 ga_clear(&ga); 20782 break; 20783 } 20784 20785 /* copy the text up to where the match is */ 20786 i = (int)(regmatch.startp[0] - tail); 20787 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i); 20788 /* add the substituted text */ 20789 (void)vim_regsub(®match, sub, (char_u *)ga.ga_data 20790 + ga.ga_len + i, TRUE, TRUE, FALSE); 20791 ga.ga_len += i + sublen - 1; 20792 /* avoid getting stuck on a match with an empty string */ 20793 if (tail == regmatch.endp[0]) 20794 { 20795 if (*tail == NUL) 20796 break; 20797 *((char_u *)ga.ga_data + ga.ga_len) = *tail++; 20798 ++ga.ga_len; 20799 } 20800 else 20801 { 20802 tail = regmatch.endp[0]; 20803 if (*tail == NUL) 20804 break; 20805 } 20806 if (!do_all) 20807 break; 20808 } 20809 20810 if (ga.ga_data != NULL) 20811 STRCPY((char *)ga.ga_data + ga.ga_len, tail); 20812 20813 vim_free(regmatch.regprog); 20814 } 20815 20816 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data); 20817 ga_clear(&ga); 20818 p_cpo = save_cpo; 20819 20820 return ret; 20821 } 20822 20823 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */ 20824