1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
11 * eval.c: Expression evaluation.
13 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
14 # include "vimio.h" /* for mch_open(), must be before vim.h */
19 #if defined(FEAT_EVAL) || defined(PROTO)
22 # include <time.h> /* for strftime() */
26 # include <time.h> /* for time_t */
29 #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
33 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
35 #define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
39 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
41 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 static dictitem_T dumdi
;
46 #define DI2HIKEY(di) ((di)->di_key)
47 #define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
48 #define HI2DI(hi) HIKEY2DI((hi)->hi_key)
51 * Structure returned by get_lval() and used by set_var_lval().
53 * "name" points to the variable name.
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
74 * "tv" points to the Dictionary typval_T
75 * "newkey" is the key for the new item.
79 char_u
*ll_name
; /* start of variable name (can be NULL) */
80 char_u
*ll_exp_name
; /* NULL or expanded name in allocated memory. */
81 typval_T
*ll_tv
; /* Typeval of item being used. If "newkey"
82 isn't NULL it's the Dict to which to add
84 listitem_T
*ll_li
; /* The list item or NULL. */
85 list_T
*ll_list
; /* The list or NULL. */
86 int ll_range
; /* TRUE when a [i:j] range was used */
87 long ll_n1
; /* First index for list */
88 long ll_n2
; /* Second index for list range */
89 int ll_empty2
; /* Second index is empty: [i:] */
90 dict_T
*ll_dict
; /* The Dictionary or NULL */
91 dictitem_T
*ll_di
; /* The dictitem or NULL */
92 char_u
*ll_newkey
; /* New key for Dict in alloc. mem or NULL. */
96 static char *e_letunexp
= N_("E18: Unexpected characters in :let");
97 static char *e_listidx
= N_("E684: list index out of range: %ld");
98 static char *e_undefvar
= N_("E121: Undefined variable: %s");
99 static char *e_missbrac
= N_("E111: Missing ']'");
100 static char *e_listarg
= N_("E686: Argument of %s must be a List");
101 static char *e_listdictarg
= N_("E712: Argument of %s must be a List or Dictionary");
102 static char *e_emptykey
= N_("E713: Cannot use empty key for Dictionary");
103 static char *e_listreq
= N_("E714: List required");
104 static char *e_dictreq
= N_("E715: Dictionary required");
105 static char *e_toomanyarg
= N_("E118: Too many arguments for function: %s");
106 static char *e_dictkey
= N_("E716: Key not present in Dictionary: %s");
107 static char *e_funcexts
= N_("E122: Function %s already exists, add ! to replace it");
108 static char *e_funcdict
= N_("E717: Dictionary entry already exists");
109 static char *e_funcref
= N_("E718: Funcref required");
110 static char *e_dictrange
= N_("E719: Cannot use [:] with a Dictionary");
111 static char *e_letwrong
= N_("E734: Wrong variable type for %s=");
112 static char *e_nofunc
= N_("E130: Unknown function: %s");
113 static char *e_illvar
= N_("E461: Illegal variable name: %s");
116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
119 static dict_T globvardict
;
120 static dictitem_T globvars_var
;
121 #define globvarht globvardict.dv_hashtab
124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
127 static hashtab_T compat_hashtab
;
130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
133 static int current_copyID
= 0;
136 * Array to hold the hashtab with variables local to each sourced script.
137 * Each item holds a variable (nameless) that points to the dict_T.
145 static garray_T ga_scripts
= {0, 0, sizeof(scriptvar_T
), 4, NULL
};
146 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
147 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
149 static int echo_attr
= 0; /* attributes used for ":echo" */
151 /* Values for trans_function_name() argument: */
152 #define TFN_INT 1 /* internal function name OK */
153 #define TFN_QUIET 2 /* no error messages */
156 * Structure to hold info for a user function.
158 typedef struct ufunc ufunc_T
;
162 int uf_varargs
; /* variable nr of arguments */
164 int uf_calls
; /* nr of active calls */
165 garray_T uf_args
; /* arguments */
166 garray_T uf_lines
; /* function lines */
168 int uf_profiling
; /* TRUE when func is being profiled */
169 /* profiling the function as a whole */
170 int uf_tm_count
; /* nr of calls */
171 proftime_T uf_tm_total
; /* time spent in function + children */
172 proftime_T uf_tm_self
; /* time spent in function itself */
173 proftime_T uf_tm_children
; /* time spent in children this call */
174 /* profiling the function per line */
175 int *uf_tml_count
; /* nr of times line was executed */
176 proftime_T
*uf_tml_total
; /* time spent in a line + children */
177 proftime_T
*uf_tml_self
; /* time spent in a line itself */
178 proftime_T uf_tml_start
; /* start time for current line */
179 proftime_T uf_tml_children
; /* time spent in children for this line */
180 proftime_T uf_tml_wait
; /* start wait time for current line */
181 int uf_tml_idx
; /* index of line being timed; -1 if none */
182 int uf_tml_execed
; /* line being timed was executed */
184 scid_T uf_script_ID
; /* ID of script where function was defined,
185 used for s: variables */
186 int uf_refcount
; /* for numbered function: reference count */
187 char_u uf_name
[1]; /* name of function (actually longer); can
188 start with <SNR>123_ (<SNR> is K_SPECIAL
193 #define FC_ABORT 1 /* abort function on error */
194 #define FC_RANGE 2 /* function accepts range */
195 #define FC_DICT 4 /* Dict function, uses "self" */
198 * All user-defined functions are found in this hashtable.
200 static hashtab_T func_hashtab
;
202 /* The names of packages that once were loaded are remembered. */
203 static garray_T ga_loaded
= {0, 0, sizeof(char_u
*), 4, NULL
};
205 /* list heads for garbage collection */
206 static dict_T
*first_dict
= NULL
; /* list of all dicts */
207 static list_T
*first_list
= NULL
; /* list of all lists */
209 /* From user function to hashitem and back. */
210 static ufunc_T dumuf
;
211 #define UF2HIKEY(fp) ((fp)->uf_name)
212 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
213 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
215 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
216 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
218 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
219 #define VAR_SHORT_LEN 20 /* short variable name length */
220 #define FIXVAR_CNT 12 /* number of fixed variables */
222 /* structure to hold info for a function that is currently being executed. */
223 typedef struct funccall_S funccall_T
;
227 ufunc_T
*func
; /* function being called */
228 int linenr
; /* next line to be executed */
229 int returned
; /* ":return" used */
230 struct /* fixed variables for arguments */
232 dictitem_T var
; /* variable (without room for name) */
233 char_u room
[VAR_SHORT_LEN
]; /* room for the name */
234 } fixvar
[FIXVAR_CNT
];
235 dict_T l_vars
; /* l: local function variables */
236 dictitem_T l_vars_var
; /* variable for l: scope */
237 dict_T l_avars
; /* a: argument variables */
238 dictitem_T l_avars_var
; /* variable for a: scope */
239 list_T l_varlist
; /* list for a:000 */
240 listitem_T l_listitems
[MAX_FUNC_ARGS
]; /* listitems for a:000 */
241 typval_T
*rettv
; /* return value */
242 linenr_T breakpoint
; /* next line with breakpoint or zero */
243 int dbg_tick
; /* debug_tick when breakpoint was set */
244 int level
; /* top nesting level of executed function */
246 proftime_T prof_child
; /* time spent in a child */
248 funccall_T
*caller
; /* calling function or NULL */
252 * Info used by a ":for" loop.
256 int fi_semicolon
; /* TRUE if ending in '; var]' */
257 int fi_varcount
; /* nr of variables in the list */
258 listwatch_T fi_lw
; /* keep an eye on the item used. */
259 list_T
*fi_list
; /* list being used */
263 * Struct used by trans_function_name()
267 dict_T
*fd_dict
; /* Dictionary used */
268 char_u
*fd_newkey
; /* new key in "dict" in allocated memory */
269 dictitem_T
*fd_di
; /* Dictionary item used */
274 * Array to hold the value of v: variables.
275 * The value is in a dictitem, so that it can also be used in the v: scope.
276 * The reason to use this table anyway is for very quick access to the
277 * variables with the VV_ defines.
281 /* values for vv_flags: */
282 #define VV_COMPAT 1 /* compatible, also used without "v:" */
283 #define VV_RO 2 /* read-only */
284 #define VV_RO_SBX 4 /* read-only in the sandbox */
286 #define VV_NAME(s, t) s, {{t}}, {0}
290 char *vv_name
; /* name of variable, without v: */
291 dictitem_T vv_di
; /* value and name for key */
292 char vv_filler
[16]; /* space for LONGEST name below!!! */
293 char vv_flags
; /* VV_COMPAT, VV_RO, VV_RO_SBX */
297 * The order here must match the VV_ defines in vim.h!
298 * Initializing a union does not work, leave tv.vval empty to get zero's.
300 {VV_NAME("count", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
301 {VV_NAME("count1", VAR_NUMBER
), VV_RO
},
302 {VV_NAME("prevcount", VAR_NUMBER
), VV_RO
},
303 {VV_NAME("errmsg", VAR_STRING
), VV_COMPAT
},
304 {VV_NAME("warningmsg", VAR_STRING
), 0},
305 {VV_NAME("statusmsg", VAR_STRING
), 0},
306 {VV_NAME("shell_error", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
307 {VV_NAME("this_session", VAR_STRING
), VV_COMPAT
},
308 {VV_NAME("version", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
309 {VV_NAME("lnum", VAR_NUMBER
), VV_RO_SBX
},
310 {VV_NAME("termresponse", VAR_STRING
), VV_RO
},
311 {VV_NAME("fname", VAR_STRING
), VV_RO
},
312 {VV_NAME("lang", VAR_STRING
), VV_RO
},
313 {VV_NAME("lc_time", VAR_STRING
), VV_RO
},
314 {VV_NAME("ctype", VAR_STRING
), VV_RO
},
315 {VV_NAME("charconvert_from", VAR_STRING
), VV_RO
},
316 {VV_NAME("charconvert_to", VAR_STRING
), VV_RO
},
317 {VV_NAME("fname_in", VAR_STRING
), VV_RO
},
318 {VV_NAME("fname_out", VAR_STRING
), VV_RO
},
319 {VV_NAME("fname_new", VAR_STRING
), VV_RO
},
320 {VV_NAME("fname_diff", VAR_STRING
), VV_RO
},
321 {VV_NAME("cmdarg", VAR_STRING
), VV_RO
},
322 {VV_NAME("foldstart", VAR_NUMBER
), VV_RO_SBX
},
323 {VV_NAME("foldend", VAR_NUMBER
), VV_RO_SBX
},
324 {VV_NAME("folddashes", VAR_STRING
), VV_RO_SBX
},
325 {VV_NAME("foldlevel", VAR_NUMBER
), VV_RO_SBX
},
326 {VV_NAME("progname", VAR_STRING
), VV_RO
},
327 {VV_NAME("servername", VAR_STRING
), VV_RO
},
328 {VV_NAME("dying", VAR_NUMBER
), VV_RO
},
329 {VV_NAME("exception", VAR_STRING
), VV_RO
},
330 {VV_NAME("throwpoint", VAR_STRING
), VV_RO
},
331 {VV_NAME("register", VAR_STRING
), VV_RO
},
332 {VV_NAME("cmdbang", VAR_NUMBER
), VV_RO
},
333 {VV_NAME("insertmode", VAR_STRING
), VV_RO
},
334 {VV_NAME("val", VAR_UNKNOWN
), VV_RO
},
335 {VV_NAME("key", VAR_UNKNOWN
), VV_RO
},
336 {VV_NAME("profiling", VAR_NUMBER
), VV_RO
},
337 {VV_NAME("fcs_reason", VAR_STRING
), VV_RO
},
338 {VV_NAME("fcs_choice", VAR_STRING
), 0},
339 {VV_NAME("beval_bufnr", VAR_NUMBER
), VV_RO
},
340 {VV_NAME("beval_winnr", VAR_NUMBER
), VV_RO
},
341 {VV_NAME("beval_lnum", VAR_NUMBER
), VV_RO
},
342 {VV_NAME("beval_col", VAR_NUMBER
), VV_RO
},
343 {VV_NAME("beval_text", VAR_STRING
), VV_RO
},
344 {VV_NAME("scrollstart", VAR_STRING
), 0},
345 {VV_NAME("swapname", VAR_STRING
), VV_RO
},
346 {VV_NAME("swapchoice", VAR_STRING
), 0},
347 {VV_NAME("swapcommand", VAR_STRING
), VV_RO
},
348 {VV_NAME("char", VAR_STRING
), VV_RO
},
349 {VV_NAME("mouse_win", VAR_NUMBER
), 0},
350 {VV_NAME("mouse_lnum", VAR_NUMBER
), 0},
351 {VV_NAME("mouse_col", VAR_NUMBER
), 0},
352 {VV_NAME("operator", VAR_STRING
), VV_RO
},
353 {VV_NAME("searchforward", VAR_NUMBER
), 0},
354 {VV_NAME("oldfiles", VAR_LIST
), 0},
358 #define vv_type vv_di.di_tv.v_type
359 #define vv_nr vv_di.di_tv.vval.v_number
360 #define vv_float vv_di.di_tv.vval.v_float
361 #define vv_str vv_di.di_tv.vval.v_string
362 #define vv_list vv_di.di_tv.vval.v_list
363 #define vv_tv vv_di.di_tv
366 * The v: variables are stored in dictionary "vimvardict".
367 * "vimvars_var" is the variable that is used for the "l:" scope.
369 static dict_T vimvardict
;
370 static dictitem_T vimvars_var
;
371 #define vimvarht vimvardict.dv_hashtab
373 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
374 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
375 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
376 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
378 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
379 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
380 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
381 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
, int *first
));
382 static void list_glob_vars
__ARGS((int *first
));
383 static void list_buf_vars
__ARGS((int *first
));
384 static void list_win_vars
__ARGS((int *first
));
386 static void list_tab_vars
__ARGS((int *first
));
388 static void list_vim_vars
__ARGS((int *first
));
389 static void list_script_vars
__ARGS((int *first
));
390 static void list_func_vars
__ARGS((int *first
));
391 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
, int *first
));
392 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
393 static int check_changedtick
__ARGS((char_u
*arg
));
394 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
395 static void clear_lval
__ARGS((lval_T
*lp
));
396 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
397 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
398 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
399 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
400 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
401 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
402 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
403 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
404 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
405 static int tv_islocked
__ARGS((typval_T
*tv
));
407 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
408 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
409 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
410 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
411 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
412 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
413 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
414 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
416 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
417 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
418 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
419 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
420 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
421 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
422 static listitem_T
*listitem_alloc
__ARGS((void));
423 static void listitem_free
__ARGS((listitem_T
*item
));
424 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
425 static long list_len
__ARGS((list_T
*l
));
426 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
427 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
428 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
429 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
430 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
431 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
432 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
433 static int list_append_tv
__ARGS((list_T
*l
, typval_T
*tv
));
434 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
435 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
436 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
437 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
438 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
439 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
440 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
441 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
442 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
443 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
444 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
445 static void dict_unref
__ARGS((dict_T
*d
));
446 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
447 static dictitem_T
*dictitem_alloc
__ARGS((char_u
*key
));
448 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
449 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
450 static void dictitem_free
__ARGS((dictitem_T
*item
));
451 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
452 static int dict_add
__ARGS((dict_T
*d
, dictitem_T
*item
));
453 static long dict_len
__ARGS((dict_T
*d
));
454 static dictitem_T
*dict_find
__ARGS((dict_T
*d
, char_u
*key
, int len
));
455 static char_u
*dict2string
__ARGS((typval_T
*tv
, int copyID
));
456 static int get_dict_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
457 static char_u
*echo_string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
458 static char_u
*tv2string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
459 static char_u
*string_quote
__ARGS((char_u
*str
, int function
));
461 static int string2float
__ARGS((char_u
*text
, float_T
*value
));
463 static int get_env_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
464 static int find_internal_func
__ARGS((char_u
*name
));
465 static char_u
*deref_func_name
__ARGS((char_u
*name
, int *lenp
));
466 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
));
467 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
));
468 static void emsg_funcname
__ARGS((char *ermsg
, char_u
*name
));
469 static int non_zero_arg
__ARGS((typval_T
*argvars
));
472 static void f_abs
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
474 static void f_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
475 static void f_append
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
476 static void f_argc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
477 static void f_argidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
478 static void f_argv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
480 static void f_atan
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
482 static void f_browse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
483 static void f_browsedir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
484 static void f_bufexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
485 static void f_buflisted
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
486 static void f_bufloaded
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
487 static void f_bufname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
488 static void f_bufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
489 static void f_bufwinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
490 static void f_byte2line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
491 static void f_byteidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
492 static void f_call
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
494 static void f_ceil
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
496 static void f_changenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
497 static void f_char2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
498 static void f_cindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
499 static void f_clearmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
500 static void f_col
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
501 #if defined(FEAT_INS_EXPAND)
502 static void f_complete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
503 static void f_complete_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
504 static void f_complete_check
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
506 static void f_confirm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
507 static void f_copy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
509 static void f_cos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
511 static void f_count
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
512 static void f_cscope_connection
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
513 static void f_cursor
__ARGS((typval_T
*argsvars
, typval_T
*rettv
));
514 static void f_deepcopy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
515 static void f_delete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
516 static void f_did_filetype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
517 static void f_diff_filler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
518 static void f_diff_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
519 static void f_empty
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
520 static void f_escape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
521 static void f_eval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
522 static void f_eventhandler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
523 static void f_executable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
524 static void f_exists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
525 static void f_expand
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
526 static void f_extend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
527 static void f_feedkeys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
528 static void f_filereadable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
529 static void f_filewritable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
530 static void f_filter
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
531 static void f_finddir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
532 static void f_findfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
534 static void f_float2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
535 static void f_floor
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
537 static void f_fnameescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
538 static void f_fnamemodify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
539 static void f_foldclosed
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
540 static void f_foldclosedend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
541 static void f_foldlevel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
542 static void f_foldtext
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
543 static void f_foldtextresult
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
544 static void f_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
545 static void f_function
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
546 static void f_garbagecollect
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
547 static void f_get
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
548 static void f_getbufline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
549 static void f_getbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
550 static void f_getchar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
551 static void f_getcharmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
552 static void f_getcmdline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
553 static void f_getcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
554 static void f_getcmdtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
555 static void f_getcwd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
556 static void f_getfontname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
557 static void f_getfperm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
558 static void f_getfsize
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
559 static void f_getftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
560 static void f_getftype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
561 static void f_getline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
562 static void f_getmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
563 static void f_getpid
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
564 static void f_getpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
565 static void f_getqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
566 static void f_getreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
567 static void f_getregtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
568 static void f_gettabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
569 static void f_getwinposx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
570 static void f_getwinposy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
571 static void f_getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
572 static void f_glob
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
573 static void f_globpath
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
574 static void f_has
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
575 static void f_has_key
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
576 static void f_haslocaldir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
577 static void f_hasmapto
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
578 static void f_histadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
579 static void f_histdel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
580 static void f_histget
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
581 static void f_histnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
582 static void f_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
583 static void f_hlexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
584 static void f_hostname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
585 static void f_iconv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
586 static void f_indent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
587 static void f_index
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
588 static void f_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
589 static void f_inputdialog
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
590 static void f_inputlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
591 static void f_inputrestore
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
592 static void f_inputsave
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
593 static void f_inputsecret
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
594 static void f_insert
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
595 static void f_isdirectory
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
596 static void f_islocked
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
597 static void f_items
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
598 static void f_join
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
599 static void f_keys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
600 static void f_last_buffer_nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
601 static void f_len
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
602 static void f_libcall
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
603 static void f_libcallnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
604 static void f_line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
605 static void f_line2byte
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
606 static void f_lispindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
607 static void f_localtime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
609 static void f_log10
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
611 static void f_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
612 static void f_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
613 static void f_mapcheck
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
614 static void f_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
615 static void f_matchadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
616 static void f_matcharg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
617 static void f_matchdelete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
618 static void f_matchend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
619 static void f_matchlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
620 static void f_matchstr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
621 static void f_max
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
622 static void f_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
624 static void f_mkdir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
626 static void f_mode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
627 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
628 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
629 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
631 static void f_pow
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
633 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
634 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
635 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
636 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
637 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
638 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
639 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
640 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
641 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
642 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
643 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
644 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
645 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
647 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
648 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_round
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
654 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
656 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_setmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
665 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
667 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
668 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
669 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
670 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
673 static void f_sin
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
675 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
676 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
677 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
678 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
679 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
681 static void f_sqrt
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
682 static void f_str2float
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
684 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
686 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
688 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
689 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
690 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
691 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
692 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
693 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
694 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
695 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
696 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
697 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
698 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
699 static void f_synstack
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
700 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
701 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
702 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
703 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
704 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
705 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
706 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
707 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
708 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
709 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
710 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
712 static void f_trunc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
714 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
715 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
716 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
717 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
718 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
719 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
720 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
721 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
722 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
723 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
724 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
725 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
726 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
727 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
729 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
730 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int dollar_lnum
, int *fnum
));
731 static int get_env_len
__ARGS((char_u
**arg
));
732 static int get_id_len
__ARGS((char_u
**arg
));
733 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
734 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
735 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
736 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
738 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
739 static int eval_isnamec
__ARGS((int c
));
740 static int eval_isnamec1
__ARGS((int c
));
741 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
742 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
743 static typval_T
*alloc_tv
__ARGS((void));
744 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
745 static void init_tv
__ARGS((typval_T
*varp
));
746 static long get_tv_number
__ARGS((typval_T
*varp
));
747 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
748 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
749 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
750 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
751 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
752 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
753 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
754 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
755 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
756 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
757 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
, int *first
));
758 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
, int *first
));
759 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
760 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
761 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
762 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
763 static void copy_tv
__ARGS((typval_T
*from
, typval_T
*to
));
764 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
765 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
766 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
767 static int eval_fname_script
__ARGS((char_u
*p
));
768 static int eval_fname_sid
__ARGS((char_u
*p
));
769 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
770 static ufunc_T
*find_func
__ARGS((char_u
*name
));
771 static int function_exists
__ARGS((char_u
*name
));
772 static int builtin_function
__ARGS((char_u
*name
));
774 static void func_do_profile
__ARGS((ufunc_T
*fp
));
775 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
776 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
781 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
786 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
788 static int script_autoload
__ARGS((char_u
*name
, int reload
));
789 static char_u
*autoload_name
__ARGS((char_u
*name
));
790 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
791 static void func_free
__ARGS((ufunc_T
*fp
));
792 static void func_unref
__ARGS((char_u
*name
));
793 static void func_ref
__ARGS((char_u
*name
));
794 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
));
795 static int can_free_funccal
__ARGS((funccall_T
*fc
, int copyID
)) ;
796 static void free_funccal
__ARGS((funccall_T
*fc
, int free_val
));
797 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
798 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
799 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
800 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
801 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
802 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
804 /* Character used as separated in autoload function/variable names. */
805 #define AUTOLOAD_CHAR '#'
808 * Initialize the global and v: variables.
816 init_var_dict(&globvardict
, &globvars_var
);
817 init_var_dict(&vimvardict
, &vimvars_var
);
818 hash_init(&compat_hashtab
);
819 hash_init(&func_hashtab
);
821 for (i
= 0; i
< VV_LEN
; ++i
)
824 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
825 if (p
->vv_flags
& VV_RO
)
826 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
827 else if (p
->vv_flags
& VV_RO_SBX
)
828 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
830 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
832 /* add to v: scope dict, unless the value is not always available */
833 if (p
->vv_type
!= VAR_UNKNOWN
)
834 hash_add(&vimvarht
, p
->vv_di
.di_key
);
835 if (p
->vv_flags
& VV_COMPAT
)
836 /* add to compat scope dict */
837 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
839 set_vim_var_nr(VV_SEARCHFORWARD
, 1L);
842 #if defined(EXITFREE) || defined(PROTO)
849 for (i
= 0; i
< VV_LEN
; ++i
)
852 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
857 else if (p
->vv_di
.di_tv
.v_type
== VAR_LIST
)
859 list_unref(p
->vv_list
);
863 hash_clear(&vimvarht
);
864 hash_init(&vimvarht
); /* garbage_collect() will access it */
865 hash_clear(&compat_hashtab
);
867 /* script-local variables */
868 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
869 vars_clear(&SCRIPT_VARS(i
));
870 ga_clear(&ga_scripts
);
873 /* global variables */
874 vars_clear(&globvarht
);
876 /* autoloaded script names */
877 ga_clear_strings(&ga_loaded
);
879 /* unreferenced lists and dicts */
880 (void)garbage_collect();
883 free_all_functions();
884 hash_clear(&func_hashtab
);
889 * Return the name of the executed function.
895 return ((funccall_T
*)cookie
)->func
->uf_name
;
899 * Return the address holding the next breakpoint line for a funccall cookie.
902 func_breakpoint(cookie
)
905 return &((funccall_T
*)cookie
)->breakpoint
;
909 * Return the address holding the debug tick for a funccall cookie.
912 func_dbg_tick(cookie
)
915 return &((funccall_T
*)cookie
)->dbg_tick
;
919 * Return the nesting level for a funccall cookie.
925 return ((funccall_T
*)cookie
)->level
;
928 /* pointer to funccal for currently active function */
929 funccall_T
*current_funccal
= NULL
;
931 /* pointer to list of previously used funccal, still around because some
932 * item in it is still being used. */
933 funccall_T
*previous_funccal
= NULL
;
936 * Return TRUE when a function was ended by a ":return" command.
939 current_func_returned()
941 return current_funccal
->returned
;
946 * Set an internal variable to a string value. Creates the variable if it does
950 set_internal_string_var(name
, value
)
957 val
= vim_strsave(value
);
960 tvp
= alloc_string_tv(val
);
963 set_var(name
, tvp
, FALSE
);
969 static lval_T
*redir_lval
= NULL
;
970 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
971 static char_u
*redir_endp
= NULL
;
972 static char_u
*redir_varname
= NULL
;
975 * Start recording command output to a variable
976 * Returns OK if successfully completed the setup. FAIL otherwise.
979 var_redir_start(name
, append
)
981 int append
; /* append to an existing variable */
987 /* Make sure a valid variable name is specified */
988 if (!eval_isnamec1(*name
))
994 redir_varname
= vim_strsave(name
);
995 if (redir_varname
== NULL
)
998 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
999 if (redir_lval
== NULL
)
1005 /* The output is stored in growarray "redir_ga" until redirection ends. */
1006 ga_init2(&redir_ga
, (int)sizeof(char), 500);
1008 /* Parse the variable name (can be a dict or list entry). */
1009 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
1011 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
1013 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
1014 /* Trailing characters are present after the variable name */
1015 EMSG(_(e_trailing
));
1022 /* check if we can write to the variable: set it to or append an empty
1024 save_emsg
= did_emsg
;
1026 tv
.v_type
= VAR_STRING
;
1027 tv
.vval
.v_string
= (char_u
*)"";
1029 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
1031 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
1033 did_emsg
|= save_emsg
;
1039 if (redir_lval
->ll_newkey
!= NULL
)
1041 /* Dictionary item was created, don't do it again. */
1042 vim_free(redir_lval
->ll_newkey
);
1043 redir_lval
->ll_newkey
= NULL
;
1050 * Append "value[value_len]" to the variable set by var_redir_start().
1051 * The actual appending is postponed until redirection ends, because the value
1052 * appended may in fact be the string we write to, changing it may cause freed
1053 * memory to be used:
1059 var_redir_str(value
, value_len
)
1065 if (redir_lval
== NULL
)
1068 if (value_len
== -1)
1069 len
= (int)STRLEN(value
); /* Append the entire string */
1071 len
= value_len
; /* Append only "value_len" characters */
1073 if (ga_grow(&redir_ga
, len
) == OK
)
1075 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1076 redir_ga
.ga_len
+= len
;
1083 * Stop redirecting command output to a variable.
1090 if (redir_lval
!= NULL
)
1092 /* Append the trailing NUL. */
1093 ga_append(&redir_ga
, NUL
);
1095 /* Assign the text to the variable. */
1096 tv
.v_type
= VAR_STRING
;
1097 tv
.vval
.v_string
= redir_ga
.ga_data
;
1098 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1099 vim_free(tv
.vval
.v_string
);
1101 clear_lval(redir_lval
);
1102 vim_free(redir_lval
);
1105 vim_free(redir_varname
);
1106 redir_varname
= NULL
;
1109 # if defined(FEAT_MBYTE) || defined(PROTO)
1111 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1119 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1120 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1121 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1122 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1123 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1125 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1126 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1127 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1128 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1136 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1138 eval_printexpr(fname
, args
)
1144 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1145 set_vim_var_string(VV_CMDARG
, args
, -1);
1146 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1148 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1149 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1160 # if defined(FEAT_DIFF) || defined(PROTO)
1162 eval_diff(origfile
, newfile
, outfile
)
1169 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1170 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1171 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1172 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1173 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1174 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1175 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1179 eval_patch(origfile
, difffile
, outfile
)
1186 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1187 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1188 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1189 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1190 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1191 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1192 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1197 * Top level evaluation function, returning a boolean.
1198 * Sets "error" to TRUE if there was an error.
1199 * Return TRUE or FALSE.
1202 eval_to_bool(arg
, error
, nextcmd
, skip
)
1206 int skip
; /* only parse, don't execute */
1213 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1220 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1231 * Top level evaluation function, returning a string. If "skip" is TRUE,
1232 * only parsing to "nextcmd" is done, without reporting errors. Return
1233 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1236 eval_to_string_skip(arg
, nextcmd
, skip
)
1239 int skip
; /* only parse, don't execute */
1246 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1250 retval
= vim_strsave(get_tv_string(&tv
));
1260 * Skip over an expression at "*pp".
1261 * Return FAIL for an error, OK otherwise.
1269 *pp
= skipwhite(*pp
);
1270 return eval1(pp
, &rettv
, FALSE
);
1274 * Top level evaluation function, returning a string.
1275 * When "convert" is TRUE convert a List into a sequence of lines and convert
1276 * a Float to a String.
1277 * Return pointer to allocated memory, or NULL for failure.
1280 eval_to_string(arg
, nextcmd
, convert
)
1289 char_u numbuf
[NUMBUFLEN
];
1292 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1296 if (convert
&& tv
.v_type
== VAR_LIST
)
1298 ga_init2(&ga
, (int)sizeof(char), 80);
1299 if (tv
.vval
.v_list
!= NULL
)
1300 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1301 ga_append(&ga
, NUL
);
1302 retval
= (char_u
*)ga
.ga_data
;
1305 else if (convert
&& tv
.v_type
== VAR_FLOAT
)
1307 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
.vval
.v_float
);
1308 retval
= vim_strsave(numbuf
);
1312 retval
= vim_strsave(get_tv_string(&tv
));
1320 * Call eval_to_string() without using current local variables and using
1321 * textlock. When "use_sandbox" is TRUE use the sandbox.
1324 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1330 void *save_funccalp
;
1332 save_funccalp
= save_funccal();
1336 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1340 restore_funccal(save_funccalp
);
1345 * Top level evaluation function, returning a number.
1346 * Evaluates "expr" silently.
1347 * Returns -1 for an error.
1350 eval_to_number(expr
)
1355 char_u
*p
= skipwhite(expr
);
1359 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1363 retval
= get_tv_number_chk(&rettv
, NULL
);
1372 * Prepare v: variable "idx" to be used.
1373 * Save the current typeval in "save_tv".
1374 * When not used yet add the variable to the v: hashtable.
1377 prepare_vimvar(idx
, save_tv
)
1381 *save_tv
= vimvars
[idx
].vv_tv
;
1382 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1383 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1387 * Restore v: variable "idx" to typeval "save_tv".
1388 * When no longer defined, remove the variable from the v: hashtable.
1391 restore_vimvar(idx
, save_tv
)
1397 vimvars
[idx
].vv_tv
= *save_tv
;
1398 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1400 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1401 if (HASHITEM_EMPTY(hi
))
1402 EMSG2(_(e_intern2
), "restore_vimvar()");
1404 hash_remove(&vimvarht
, hi
);
1408 #if defined(FEAT_SPELL) || defined(PROTO)
1410 * Evaluate an expression to a list with suggestions.
1411 * For the "expr:" part of 'spellsuggest'.
1412 * Returns NULL when there is an error.
1415 eval_spell_expr(badword
, expr
)
1421 list_T
*list
= NULL
;
1422 char_u
*p
= skipwhite(expr
);
1424 /* Set "v:val" to the bad word. */
1425 prepare_vimvar(VV_VAL
, &save_val
);
1426 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1427 vimvars
[VV_VAL
].vv_str
= badword
;
1431 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1433 if (rettv
.v_type
!= VAR_LIST
)
1436 list
= rettv
.vval
.v_list
;
1441 restore_vimvar(VV_VAL
, &save_val
);
1447 * "list" is supposed to contain two items: a word and a number. Return the
1448 * word in "pp" and the number as the return value.
1449 * Return -1 if anything isn't right.
1450 * Used to get the good word and score from the eval_spell_expr() result.
1453 get_spellword(list
, pp
)
1459 li
= list
->lv_first
;
1462 *pp
= get_tv_string(&li
->li_tv
);
1467 return get_tv_number(&li
->li_tv
);
1472 * Top level evaluation function.
1473 * Returns an allocated typval_T with the result.
1474 * Returns NULL when there is an error.
1477 eval_expr(arg
, nextcmd
)
1483 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1484 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1494 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1495 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1497 * Call some vimL function and return the result in "*rettv".
1498 * Uses argv[argc] for the function arguments. Only Number and String
1499 * arguments are currently supported.
1500 * Returns OK or FAIL.
1503 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1507 int safe
; /* use the sandbox */
1515 void *save_funccalp
= NULL
;
1518 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1519 if (argvars
== NULL
)
1522 for (i
= 0; i
< argc
; i
++)
1524 /* Pass a NULL or empty argument as an empty string */
1525 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1527 argvars
[i
].v_type
= VAR_STRING
;
1528 argvars
[i
].vval
.v_string
= (char_u
*)"";
1532 /* Recognize a number argument, the others must be strings. */
1533 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1534 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1536 argvars
[i
].v_type
= VAR_NUMBER
;
1537 argvars
[i
].vval
.v_number
= n
;
1541 argvars
[i
].v_type
= VAR_STRING
;
1542 argvars
[i
].vval
.v_string
= argv
[i
];
1548 save_funccalp
= save_funccal();
1552 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1553 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1554 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1555 &doesrange
, TRUE
, NULL
);
1559 restore_funccal(save_funccalp
);
1569 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1571 * Call vimL function "func" and return the result as a string.
1572 * Returns NULL when calling the function fails.
1573 * Uses argv[argc] for the function arguments.
1576 call_func_retstr(func
, argc
, argv
, safe
)
1580 int safe
; /* use the sandbox */
1585 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1588 retval
= vim_strsave(get_tv_string(&rettv
));
1594 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1596 * Call vimL function "func" and return the result as a number.
1597 * Returns -1 when calling the function fails.
1598 * Uses argv[argc] for the function arguments.
1601 call_func_retnr(func
, argc
, argv
, safe
)
1605 int safe
; /* use the sandbox */
1610 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1613 retval
= get_tv_number_chk(&rettv
, NULL
);
1620 * Call vimL function "func" and return the result as a List.
1621 * Uses argv[argc] for the function arguments.
1622 * Returns NULL when there is something wrong.
1625 call_func_retlist(func
, argc
, argv
, safe
)
1629 int safe
; /* use the sandbox */
1633 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1636 if (rettv
.v_type
!= VAR_LIST
)
1642 return rettv
.vval
.v_list
;
1648 * Save the current function call pointer, and set it to NULL.
1649 * Used when executing autocommands and for ":source".
1654 funccall_T
*fc
= current_funccal
;
1656 current_funccal
= NULL
;
1661 restore_funccal(vfc
)
1664 funccall_T
*fc
= (funccall_T
*)vfc
;
1666 current_funccal
= fc
;
1669 #if defined(FEAT_PROFILE) || defined(PROTO)
1671 * Prepare profiling for entering a child or something else that is not
1672 * counted for the script/function itself.
1673 * Should always be called in pair with prof_child_exit().
1676 prof_child_enter(tm
)
1677 proftime_T
*tm
; /* place to store waittime */
1679 funccall_T
*fc
= current_funccal
;
1681 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1682 profile_start(&fc
->prof_child
);
1683 script_prof_save(tm
);
1687 * Take care of time spent in a child.
1688 * Should always be called after prof_child_enter().
1692 proftime_T
*tm
; /* where waittime was stored */
1694 funccall_T
*fc
= current_funccal
;
1696 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1698 profile_end(&fc
->prof_child
);
1699 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1700 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1701 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1703 script_prof_restore(tm
);
1710 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1711 * it in "*cp". Doesn't give error messages.
1714 eval_foldexpr(arg
, cp
)
1721 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1729 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1733 /* If the result is a number, just return the number. */
1734 if (tv
.v_type
== VAR_NUMBER
)
1735 retval
= tv
.vval
.v_number
;
1736 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1740 /* If the result is a string, check if there is a non-digit before
1742 s
= tv
.vval
.v_string
;
1743 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1745 retval
= atol((char *)s
);
1759 * ":let" list all variable values
1760 * ":let var1 var2" list variable values
1761 * ":let var = expr" assignment command.
1762 * ":let var += expr" assignment command.
1763 * ":let var -= expr" assignment command.
1764 * ":let var .= expr" assignment command.
1765 * ":let [var1, var2] = expr" unpack list.
1771 char_u
*arg
= eap
->arg
;
1772 char_u
*expr
= NULL
;
1781 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1784 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1786 expr
= vim_strchr(argend
, '=');
1790 * ":let" without "=": list variables
1794 else if (!ends_excmd(*arg
))
1795 /* ":let var1 var2" */
1796 arg
= list_arg_vars(eap
, arg
, &first
);
1797 else if (!eap
->skip
)
1800 list_glob_vars(&first
);
1801 list_buf_vars(&first
);
1802 list_win_vars(&first
);
1804 list_tab_vars(&first
);
1806 list_script_vars(&first
);
1807 list_func_vars(&first
);
1808 list_vim_vars(&first
);
1810 eap
->nextcmd
= check_nextcmd(arg
);
1818 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1819 op
[0] = expr
[-1]; /* +=, -= or .= */
1821 expr
= skipwhite(expr
+ 1);
1825 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1834 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1842 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1843 * Handles both "var" with any type and "[var, var; var]" with a list type.
1844 * When "nextchars" is not NULL it points to a string with characters that
1845 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1847 * Returns OK or FAIL;
1850 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1853 int copy
; /* copy values from "tv", don't move */
1854 int semicolon
; /* from skip_var_list() */
1855 int var_count
; /* from skip_var_list() */
1858 char_u
*arg
= arg_start
;
1867 * ":let var = expr" or ":for var in list"
1869 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1875 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1877 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1884 if (semicolon
== 0 && var_count
< i
)
1886 EMSG(_("E687: Less targets than List items"));
1889 if (var_count
- semicolon
> i
)
1891 EMSG(_("E688: More targets than List items"));
1898 arg
= skipwhite(arg
+ 1);
1899 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1900 item
= item
->li_next
;
1904 arg
= skipwhite(arg
);
1907 /* Put the rest of the list (may be empty) in the var after ';'.
1908 * Create a new list for this. */
1912 while (item
!= NULL
)
1914 list_append_tv(l
, &item
->li_tv
);
1915 item
= item
->li_next
;
1918 ltv
.v_type
= VAR_LIST
;
1920 ltv
.vval
.v_list
= l
;
1923 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1924 (char_u
*)"]", nextchars
);
1930 else if (*arg
!= ',' && *arg
!= ']')
1932 EMSG2(_(e_intern2
), "ex_let_vars()");
1941 * Skip over assignable variable "var" or list of variables "[var, var]".
1942 * Used for ":let varvar = expr" and ":for varvar in expr".
1943 * For "[var, var]" increment "*var_count" for each variable.
1944 * for "[var, var; var]" set "semicolon".
1945 * Return NULL for an error.
1948 skip_var_list(arg
, var_count
, semicolon
)
1957 /* "[var, var]": find the matching ']'. */
1961 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1962 s
= skip_var_one(p
);
1965 EMSG2(_(e_invarg2
), p
);
1975 if (*semicolon
== 1)
1977 EMSG(_("Double ; in list of variables"));
1984 EMSG2(_(e_invarg2
), p
);
1991 return skip_var_one(arg
);
1995 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
2002 if (*arg
== '@' && arg
[1] != NUL
)
2004 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
2005 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2009 * List variables for hashtab "ht" with prefix "prefix".
2010 * If "empty" is TRUE also list NULL strings as empty strings.
2013 list_hashtable_vars(ht
, prefix
, empty
, first
)
2023 todo
= (int)ht
->ht_used
;
2024 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
2026 if (!HASHITEM_EMPTY(hi
))
2030 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
2031 || di
->di_tv
.vval
.v_string
!= NULL
)
2032 list_one_var(di
, prefix
, first
);
2038 * List global variables.
2041 list_glob_vars(first
)
2044 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
2048 * List buffer variables.
2051 list_buf_vars(first
)
2054 char_u numbuf
[NUMBUFLEN
];
2056 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
2059 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
2060 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
2065 * List window variables.
2068 list_win_vars(first
)
2071 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
2072 (char_u
*)"w:", TRUE
, first
);
2077 * List tab page variables.
2080 list_tab_vars(first
)
2083 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2084 (char_u
*)"t:", TRUE
, first
);
2089 * List Vim variables.
2092 list_vim_vars(first
)
2095 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2099 * List script-local variables, if there is a script.
2102 list_script_vars(first
)
2105 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2106 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2107 (char_u
*)"s:", FALSE
, first
);
2111 * List function variables, if there is a function.
2114 list_func_vars(first
)
2117 if (current_funccal
!= NULL
)
2118 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2119 (char_u
*)"l:", FALSE
, first
);
2123 * List variables in "arg".
2126 list_arg_vars(eap
, arg
, first
)
2139 while (!ends_excmd(*arg
) && !got_int
)
2141 if (error
|| eap
->skip
)
2143 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2144 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2147 EMSG(_(e_trailing
));
2153 /* get_name_len() takes care of expanding curly braces */
2154 name_start
= name
= arg
;
2155 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2158 /* This is mainly to keep test 49 working: when expanding
2159 * curly braces fails overrule the exception error message. */
2160 if (len
< 0 && !aborting())
2163 EMSG2(_(e_invarg2
), arg
);
2172 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2176 /* handle d.key, l[idx], f(expr) */
2178 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2182 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2186 case 'g': list_glob_vars(first
); break;
2187 case 'b': list_buf_vars(first
); break;
2188 case 'w': list_win_vars(first
); break;
2190 case 't': list_tab_vars(first
); break;
2192 case 'v': list_vim_vars(first
); break;
2193 case 's': list_script_vars(first
); break;
2194 case 'l': list_func_vars(first
); break;
2196 EMSG2(_("E738: Can't list variables for %s"), name
);
2201 char_u numbuf
[NUMBUFLEN
];
2206 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2209 list_one_var_a((char_u
*)"",
2210 arg
== arg_subsc
? name
: name_start
,
2212 s
== NULL
? (char_u
*)"" : s
,
2225 arg
= skipwhite(arg
);
2232 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2233 * Returns a pointer to the char just after the var name.
2234 * Returns NULL if there is an error.
2237 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2238 char_u
*arg
; /* points to variable name */
2239 typval_T
*tv
; /* value to assign to variable */
2240 int copy
; /* copy value from "tv" */
2241 char_u
*endchars
; /* valid chars after variable name or NULL */
2242 char_u
*op
; /* "+", "-", "." or NULL*/
2247 char_u
*arg_end
= NULL
;
2250 char_u
*tofree
= NULL
;
2253 * ":let $VAR = expr": Set environment variable.
2257 /* Find the end of the name. */
2260 len
= get_env_len(&arg
);
2262 EMSG2(_(e_invarg2
), name
- 1);
2265 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2266 EMSG2(_(e_letwrong
), op
);
2267 else if (endchars
!= NULL
2268 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2269 EMSG(_(e_letunexp
));
2274 p
= get_tv_string_chk(tv
);
2275 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2277 int mustfree
= FALSE
;
2278 char_u
*s
= vim_getenv(name
, &mustfree
);
2282 p
= tofree
= concat_str(s
, p
);
2289 vim_setenv(name
, p
);
2290 if (STRICMP(name
, "HOME") == 0)
2292 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2294 else if (didset_vimruntime
2295 && STRICMP(name
, "VIMRUNTIME") == 0)
2296 didset_vimruntime
= FALSE
;
2306 * ":let &option = expr": Set option value.
2307 * ":let &l:option = expr": Set local option value.
2308 * ":let &g:option = expr": Set global option value.
2310 else if (*arg
== '&')
2312 /* Find the end of the name. */
2313 p
= find_option_end(&arg
, &opt_flags
);
2314 if (p
== NULL
|| (endchars
!= NULL
2315 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2316 EMSG(_(e_letunexp
));
2322 char_u
*stringval
= NULL
;
2328 n
= get_tv_number(tv
);
2329 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2330 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2332 opt_type
= get_option_value(arg
, &numval
,
2333 &stringval
, opt_flags
);
2334 if ((opt_type
== 1 && *op
== '.')
2335 || (opt_type
== 0 && *op
!= '.'))
2336 EMSG2(_(e_letwrong
), op
);
2339 if (opt_type
== 1) /* number */
2346 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2348 s
= concat_str(stringval
, s
);
2349 vim_free(stringval
);
2356 set_option_value(arg
, n
, s
, opt_flags
);
2360 vim_free(stringval
);
2365 * ":let @r = expr": Set register contents.
2367 else if (*arg
== '@')
2370 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2371 EMSG2(_(e_letwrong
), op
);
2372 else if (endchars
!= NULL
2373 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2374 EMSG(_(e_letunexp
));
2377 char_u
*ptofree
= NULL
;
2380 p
= get_tv_string_chk(tv
);
2381 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2383 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2386 p
= ptofree
= concat_str(s
, p
);
2392 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2400 * ":let var = expr": Set internal variable.
2401 * ":let {expr} = expr": Idem, name made with curly braces
2403 else if (eval_isnamec1(*arg
) || *arg
== '{')
2407 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2408 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2410 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2411 EMSG(_(e_letunexp
));
2414 set_var_lval(&lv
, p
, tv
, copy
, op
);
2422 EMSG2(_(e_invarg2
), arg
);
2428 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2431 check_changedtick(arg
)
2434 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2436 EMSG2(_(e_readonlyvar
), arg
);
2443 * Get an lval: variable, Dict item or List item that can be assigned a value
2444 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2445 * "name.key", "name.key[expr]" etc.
2446 * Indexing only works if "name" is an existing List or Dictionary.
2447 * "name" points to the start of the name.
2448 * If "rettv" is not NULL it points to the value to be assigned.
2449 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2450 * wrong; must end in space or cmd separator.
2452 * Returns a pointer to just after the name, including indexes.
2453 * When an evaluation error occurs "lp->ll_name" is NULL;
2454 * Returns NULL for a parsing error. Still need to free items in "lp"!
2457 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2463 int quiet
; /* don't give error messages */
2464 int fne_flags
; /* flags for find_name_end() */
2467 char_u
*expr_start
, *expr_end
;
2478 /* Clear everything in "lp". */
2479 vim_memset(lp
, 0, sizeof(lval_T
));
2483 /* When skipping just find the end of the name. */
2485 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2488 /* Find the end of the name. */
2489 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2490 if (expr_start
!= NULL
)
2492 /* Don't expand the name when we already know there is an error. */
2493 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2494 && *p
!= '[' && *p
!= '.')
2496 EMSG(_(e_trailing
));
2500 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2501 if (lp
->ll_exp_name
== NULL
)
2503 /* Report an invalid expression in braces, unless the
2504 * expression evaluation has been cancelled due to an
2505 * aborting error, an interrupt, or an exception. */
2506 if (!aborting() && !quiet
)
2509 EMSG2(_(e_invarg2
), name
);
2513 lp
->ll_name
= lp
->ll_exp_name
;
2518 /* Without [idx] or .key we are done. */
2519 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2524 v
= find_var(lp
->ll_name
, &ht
);
2525 if (v
== NULL
&& !quiet
)
2526 EMSG2(_(e_undefvar
), lp
->ll_name
);
2532 * Loop until no more [idx] or .key is following.
2534 lp
->ll_tv
= &v
->di_tv
;
2535 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2537 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2538 && !(lp
->ll_tv
->v_type
== VAR_DICT
2539 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2542 EMSG(_("E689: Can only index a List or Dictionary"));
2548 EMSG(_("E708: [:] must come last"));
2556 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2561 EMSG(_(e_emptykey
));
2568 /* Get the index [expr] or the first index [expr: ]. */
2569 p
= skipwhite(p
+ 1);
2575 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2577 if (get_tv_string_chk(&var1
) == NULL
)
2579 /* not a number or string */
2585 /* Optionally get the second index [ :expr]. */
2588 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2591 EMSG(_(e_dictrange
));
2596 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2597 || rettv
->vval
.v_list
== NULL
))
2600 EMSG(_("E709: [:] requires a List value"));
2605 p
= skipwhite(p
+ 1);
2607 lp
->ll_empty2
= TRUE
;
2610 lp
->ll_empty2
= FALSE
;
2611 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2617 if (get_tv_string_chk(&var2
) == NULL
)
2619 /* not a number or string */
2626 lp
->ll_range
= TRUE
;
2629 lp
->ll_range
= FALSE
;
2634 EMSG(_(e_missbrac
));
2637 if (lp
->ll_range
&& !lp
->ll_empty2
)
2642 /* Skip to past ']'. */
2646 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2650 /* "[key]": get key from "var1" */
2651 key
= get_tv_string(&var1
); /* is number or string */
2655 EMSG(_(e_emptykey
));
2661 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2662 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2663 if (lp
->ll_di
== NULL
)
2665 /* Key does not exist in dict: may need to add it. */
2666 if (*p
== '[' || *p
== '.' || unlet
)
2669 EMSG2(_(e_dictkey
), key
);
2675 lp
->ll_newkey
= vim_strsave(key
);
2677 lp
->ll_newkey
= vim_strnsave(key
, len
);
2680 if (lp
->ll_newkey
== NULL
)
2686 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2691 * Get the number and item for the only or first index of the List.
2697 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2701 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2702 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2703 if (lp
->ll_li
== NULL
)
2708 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2711 if (lp
->ll_li
== NULL
)
2713 if (lp
->ll_range
&& !lp
->ll_empty2
)
2719 * May need to find the item or absolute index for the second
2721 * When no index given: "lp->ll_empty2" is TRUE.
2722 * Otherwise "lp->ll_n2" is set to the second index.
2724 if (lp
->ll_range
&& !lp
->ll_empty2
)
2726 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2730 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2733 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2736 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2738 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2739 if (lp
->ll_n2
< lp
->ll_n1
)
2743 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2751 * Clear lval "lp" that was filled by get_lval().
2757 vim_free(lp
->ll_exp_name
);
2758 vim_free(lp
->ll_newkey
);
2762 * Set a variable that was parsed by get_lval() to "rettv".
2763 * "endp" points to just after the parsed name.
2764 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2767 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2778 if (lp
->ll_tv
== NULL
)
2780 if (!check_changedtick(lp
->ll_name
))
2784 if (op
!= NULL
&& *op
!= '=')
2788 /* handle +=, -= and .= */
2789 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2792 if (tv_op(&tv
, rettv
, op
) == OK
)
2793 set_var(lp
->ll_name
, &tv
, FALSE
);
2798 set_var(lp
->ll_name
, rettv
, copy
);
2802 else if (tv_check_lock(lp
->ll_newkey
== NULL
2804 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2806 else if (lp
->ll_range
)
2809 * Assign the List values to the list items.
2811 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2813 if (op
!= NULL
&& *op
!= '=')
2814 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2817 clear_tv(&lp
->ll_li
->li_tv
);
2818 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2821 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2823 if (lp
->ll_li
->li_next
== NULL
)
2825 /* Need to add an empty item. */
2826 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2832 lp
->ll_li
= lp
->ll_li
->li_next
;
2836 EMSG(_("E710: List value has more items than target"));
2837 else if (lp
->ll_empty2
2838 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2839 : lp
->ll_n1
!= lp
->ll_n2
)
2840 EMSG(_("E711: List value has not enough items"));
2845 * Assign to a List or Dictionary item.
2847 if (lp
->ll_newkey
!= NULL
)
2849 if (op
!= NULL
&& *op
!= '=')
2851 EMSG2(_(e_letwrong
), op
);
2855 /* Need to add an item to the Dictionary. */
2856 di
= dictitem_alloc(lp
->ll_newkey
);
2859 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2864 lp
->ll_tv
= &di
->di_tv
;
2866 else if (op
!= NULL
&& *op
!= '=')
2868 tv_op(lp
->ll_tv
, rettv
, op
);
2872 clear_tv(lp
->ll_tv
);
2875 * Assign the value to the variable or list item.
2878 copy_tv(rettv
, lp
->ll_tv
);
2881 *lp
->ll_tv
= *rettv
;
2882 lp
->ll_tv
->v_lock
= 0;
2889 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2890 * Returns OK or FAIL.
2899 char_u numbuf
[NUMBUFLEN
];
2902 /* Can't do anything with a Funcref or a Dict on the right. */
2903 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2905 switch (tv1
->v_type
)
2912 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2915 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2916 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2921 if (tv2
->v_type
== VAR_LIST
)
2923 if (*op
== '+' || *op
== '-')
2925 /* nr += nr or nr -= nr*/
2926 n
= get_tv_number(tv1
);
2928 if (tv2
->v_type
== VAR_FLOAT
)
2933 f
+= tv2
->vval
.v_float
;
2935 f
-= tv2
->vval
.v_float
;
2937 tv1
->v_type
= VAR_FLOAT
;
2938 tv1
->vval
.v_float
= f
;
2944 n
+= get_tv_number(tv2
);
2946 n
-= get_tv_number(tv2
);
2948 tv1
->v_type
= VAR_NUMBER
;
2949 tv1
->vval
.v_number
= n
;
2954 if (tv2
->v_type
== VAR_FLOAT
)
2958 s
= get_tv_string(tv1
);
2959 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2961 tv1
->v_type
= VAR_STRING
;
2962 tv1
->vval
.v_string
= s
;
2971 if (*op
== '.' || (tv2
->v_type
!= VAR_FLOAT
2972 && tv2
->v_type
!= VAR_NUMBER
2973 && tv2
->v_type
!= VAR_STRING
))
2975 if (tv2
->v_type
== VAR_FLOAT
)
2976 f
= tv2
->vval
.v_float
;
2978 f
= get_tv_number(tv2
);
2980 tv1
->vval
.v_float
+= f
;
2982 tv1
->vval
.v_float
-= f
;
2989 EMSG2(_(e_letwrong
), op
);
2994 * Add a watcher to a list.
2997 list_add_watch(l
, lw
)
3001 lw
->lw_next
= l
->lv_watch
;
3006 * Remove a watcher from a list.
3007 * No warning when it isn't found...
3010 list_rem_watch(l
, lwrem
)
3014 listwatch_T
*lw
, **lwp
;
3017 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3029 * Just before removing an item from a list: advance watchers to the next
3033 list_fix_watch(l
, item
)
3039 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3040 if (lw
->lw_item
== item
)
3041 lw
->lw_item
= item
->li_next
;
3045 * Evaluate the expression used in a ":for var in expr" command.
3046 * "arg" points to "var".
3047 * Set "*errp" to TRUE for an error, FALSE otherwise;
3048 * Return a pointer that holds the info. Null when there is an error.
3051 eval_for_line(arg
, errp
, nextcmdp
, skip
)
3062 *errp
= TRUE
; /* default: there is an error */
3064 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
3068 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
3072 expr
= skipwhite(expr
);
3073 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
3075 EMSG(_("E690: Missing \"in\" after :for"));
3081 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
3087 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
3094 /* No need to increment the refcount, it's already set for the
3095 * list being used in "tv". */
3097 list_add_watch(l
, &fi
->fi_lw
);
3098 fi
->fi_lw
.lw_item
= l
->lv_first
;
3109 * Use the first item in a ":for" list. Advance to the next.
3110 * Assign the values to the variable (list). "arg" points to the first one.
3111 * Return TRUE when a valid item was found, FALSE when at end of list or
3115 next_for_item(fi_void
, arg
)
3119 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3123 item
= fi
->fi_lw
.lw_item
;
3128 fi
->fi_lw
.lw_item
= item
->li_next
;
3129 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3130 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3136 * Free the structure used to store info used by ":for".
3139 free_for_info(fi_void
)
3142 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3144 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3146 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3147 list_unref(fi
->fi_list
);
3152 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3155 set_context_for_expression(xp
, arg
, cmdidx
)
3164 if (cmdidx
== CMD_let
)
3166 xp
->xp_context
= EXPAND_USER_VARS
;
3167 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3169 /* ":let var1 var2 ...": find last space. */
3170 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3173 mb_ptr_back(arg
, p
);
3174 if (vim_iswhite(*p
))
3181 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3182 : EXPAND_EXPRESSION
;
3183 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3184 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3186 c
= *xp
->xp_pattern
;
3189 c
= xp
->xp_pattern
[1];
3193 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3194 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3198 xp
->xp_context
= EXPAND_SETTINGS
;
3199 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3200 xp
->xp_pattern
+= 2;
3206 /* environment variable */
3207 xp
->xp_context
= EXPAND_ENV_VARS
;
3212 xp
->xp_context
= EXPAND_EXPRESSION
;
3215 && xp
->xp_context
== EXPAND_FUNCTIONS
3216 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3218 /* Function name can start with "<SNR>" */
3221 else if (cmdidx
!= CMD_let
|| got_eq
)
3223 if (c
== '"') /* string */
3225 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3226 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3228 xp
->xp_context
= EXPAND_NOTHING
;
3230 else if (c
== '\'') /* literal string */
3232 /* Trick: '' is like stopping and starting a literal string. */
3233 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3235 xp
->xp_context
= EXPAND_NOTHING
;
3239 if (xp
->xp_pattern
[1] == '|')
3242 xp
->xp_context
= EXPAND_EXPRESSION
;
3245 xp
->xp_context
= EXPAND_COMMANDS
;
3248 xp
->xp_context
= EXPAND_EXPRESSION
;
3251 /* Doesn't look like something valid, expand as an expression
3253 xp
->xp_context
= EXPAND_EXPRESSION
;
3254 arg
= xp
->xp_pattern
;
3256 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3259 xp
->xp_pattern
= arg
;
3262 #endif /* FEAT_CMDL_COMPL */
3265 * ":1,25call func(arg1, arg2)" function call.
3271 char_u
*arg
= eap
->arg
;
3282 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3283 if (fudi
.fd_newkey
!= NULL
)
3285 /* Still need to give an error message for missing key. */
3286 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3287 vim_free(fudi
.fd_newkey
);
3292 /* Increase refcount on dictionary, it could get deleted when evaluating
3294 if (fudi
.fd_dict
!= NULL
)
3295 ++fudi
.fd_dict
->dv_refcount
;
3297 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3298 len
= (int)STRLEN(tofree
);
3299 name
= deref_func_name(tofree
, &len
);
3301 /* Skip white space to allow ":call func ()". Not good, but required for
3302 * backward compatibility. */
3303 startarg
= skipwhite(arg
);
3304 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3306 if (*startarg
!= '(')
3308 EMSG2(_("E107: Missing parentheses: %s"), eap
->arg
);
3313 * When skipping, evaluate the function once, to find the end of the
3315 * When the function takes a range, this is discovered after the first
3316 * call, and the loop is broken.
3321 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3325 for ( ; lnum
<= eap
->line2
; ++lnum
)
3327 if (!eap
->skip
&& eap
->addr_count
> 0)
3329 curwin
->w_cursor
.lnum
= lnum
;
3330 curwin
->w_cursor
.col
= 0;
3333 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3334 eap
->line1
, eap
->line2
, &doesrange
,
3335 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3341 /* Handle a function returning a Funcref, Dictionary or List. */
3342 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3349 if (doesrange
|| eap
->skip
)
3352 /* Stop when immediately aborting on error, or when an interrupt
3353 * occurred or an exception was thrown but not caught.
3354 * get_func_tv() returned OK, so that the check for trailing
3355 * characters below is executed. */
3364 /* Check for trailing illegal characters and a following command. */
3365 if (!ends_excmd(*arg
))
3368 EMSG(_(e_trailing
));
3371 eap
->nextcmd
= check_nextcmd(arg
);
3375 dict_unref(fudi
.fd_dict
);
3380 * ":unlet[!] var1 ... " command.
3386 ex_unletlock(eap
, eap
->arg
, 0);
3390 * ":lockvar" and ":unlockvar" commands
3396 char_u
*arg
= eap
->arg
;
3401 else if (vim_isdigit(*arg
))
3403 deep
= getdigits(&arg
);
3404 arg
= skipwhite(arg
);
3407 ex_unletlock(eap
, arg
, deep
);
3411 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3414 ex_unletlock(eap
, argstart
, deep
)
3419 char_u
*arg
= argstart
;
3426 /* Parse the name and find the end. */
3427 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3429 if (lv
.ll_name
== NULL
)
3430 error
= TRUE
; /* error but continue parsing */
3431 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3432 && !ends_excmd(*name_end
)))
3434 if (name_end
!= NULL
)
3437 EMSG(_(e_trailing
));
3439 if (!(eap
->skip
|| error
))
3444 if (!error
&& !eap
->skip
)
3446 if (eap
->cmdidx
== CMD_unlet
)
3448 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3453 if (do_lock_var(&lv
, name_end
, deep
,
3454 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3462 arg
= skipwhite(name_end
);
3463 } while (!ends_excmd(*arg
));
3465 eap
->nextcmd
= check_nextcmd(arg
);
3469 do_unlet_var(lp
, name_end
, forceit
)
3477 if (lp
->ll_tv
== NULL
)
3482 /* Normal name or expanded name. */
3483 if (check_changedtick(lp
->ll_name
))
3485 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3489 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3491 else if (lp
->ll_range
)
3495 /* Delete a range of List items. */
3496 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3498 li
= lp
->ll_li
->li_next
;
3499 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3506 if (lp
->ll_list
!= NULL
)
3507 /* unlet a List item. */
3508 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3510 /* unlet a Dictionary item. */
3511 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3518 * "unlet" a variable. Return OK if it existed, FAIL if not.
3519 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3522 do_unlet(name
, forceit
)
3531 ht
= find_var_ht(name
, &varname
);
3532 if (ht
!= NULL
&& *varname
!= NUL
)
3534 hi
= hash_find(ht
, varname
);
3535 if (!HASHITEM_EMPTY(hi
))
3538 if (var_check_fixed(di
->di_flags
, name
)
3539 || var_check_ro(di
->di_flags
, name
))
3547 EMSG2(_("E108: No such variable: \"%s\""), name
);
3552 * Lock or unlock variable indicated by "lp".
3553 * "deep" is the levels to go (-1 for unlimited);
3554 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3557 do_lock_var(lp
, name_end
, deep
, lock
)
3567 if (deep
== 0) /* nothing to do */
3570 if (lp
->ll_tv
== NULL
)
3575 /* Normal name or expanded name. */
3576 if (check_changedtick(lp
->ll_name
))
3580 di
= find_var(lp
->ll_name
, NULL
);
3586 di
->di_flags
|= DI_FLAGS_LOCK
;
3588 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3589 item_lock(&di
->di_tv
, deep
, lock
);
3594 else if (lp
->ll_range
)
3596 listitem_T
*li
= lp
->ll_li
;
3598 /* (un)lock a range of List items. */
3599 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3601 item_lock(&li
->li_tv
, deep
, lock
);
3606 else if (lp
->ll_list
!= NULL
)
3607 /* (un)lock a List item. */
3608 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3610 /* un(lock) a Dictionary item. */
3611 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3617 * Lock or unlock an item. "deep" is nr of levels to go.
3620 item_lock(tv
, deep
, lock
)
3625 static int recurse
= 0;
3632 if (recurse
>= DICT_MAXNEST
)
3634 EMSG(_("E743: variable nested too deep for (un)lock"));
3641 /* lock/unlock the item itself */
3643 tv
->v_lock
|= VAR_LOCKED
;
3645 tv
->v_lock
&= ~VAR_LOCKED
;
3650 if ((l
= tv
->vval
.v_list
) != NULL
)
3653 l
->lv_lock
|= VAR_LOCKED
;
3655 l
->lv_lock
&= ~VAR_LOCKED
;
3656 if (deep
< 0 || deep
> 1)
3657 /* recursive: lock/unlock the items the List contains */
3658 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3659 item_lock(&li
->li_tv
, deep
- 1, lock
);
3663 if ((d
= tv
->vval
.v_dict
) != NULL
)
3666 d
->dv_lock
|= VAR_LOCKED
;
3668 d
->dv_lock
&= ~VAR_LOCKED
;
3669 if (deep
< 0 || deep
> 1)
3671 /* recursive: lock/unlock the items the List contains */
3672 todo
= (int)d
->dv_hashtab
.ht_used
;
3673 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3675 if (!HASHITEM_EMPTY(hi
))
3678 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3688 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3689 * or it refers to a List or Dictionary that is locked.
3695 return (tv
->v_lock
& VAR_LOCKED
)
3696 || (tv
->v_type
== VAR_LIST
3697 && tv
->vval
.v_list
!= NULL
3698 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3699 || (tv
->v_type
== VAR_DICT
3700 && tv
->vval
.v_dict
!= NULL
3701 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3704 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3706 * Delete all "menutrans_" variables.
3709 del_menutrans_vars()
3714 hash_lock(&globvarht
);
3715 todo
= (int)globvarht
.ht_used
;
3716 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3718 if (!HASHITEM_EMPTY(hi
))
3721 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3722 delete_var(&globvarht
, hi
);
3725 hash_unlock(&globvarht
);
3729 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3732 * Local string buffer for the next two functions to store a variable name
3733 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3734 * get_user_var_name().
3737 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3739 static char_u
*varnamebuf
= NULL
;
3740 static int varnamebuflen
= 0;
3743 * Function to concatenate a prefix and a variable name.
3746 cat_prefix_varname(prefix
, name
)
3752 len
= (int)STRLEN(name
) + 3;
3753 if (len
> varnamebuflen
)
3755 vim_free(varnamebuf
);
3756 len
+= 10; /* some additional space */
3757 varnamebuf
= alloc(len
);
3758 if (varnamebuf
== NULL
)
3763 varnamebuflen
= len
;
3765 *varnamebuf
= prefix
;
3766 varnamebuf
[1] = ':';
3767 STRCPY(varnamebuf
+ 2, name
);
3772 * Function given to ExpandGeneric() to obtain the list of user defined
3773 * (global/buffer/window/built-in) variable names.
3777 get_user_var_name(xp
, idx
)
3781 static long_u gdone
;
3782 static long_u bdone
;
3783 static long_u wdone
;
3785 static long_u tdone
;
3788 static hashitem_T
*hi
;
3793 gdone
= bdone
= wdone
= vidx
= 0;
3799 /* Global variables */
3800 if (gdone
< globvarht
.ht_used
)
3803 hi
= globvarht
.ht_array
;
3806 while (HASHITEM_EMPTY(hi
))
3808 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3809 return cat_prefix_varname('g', hi
->hi_key
);
3814 ht
= &curbuf
->b_vars
.dv_hashtab
;
3815 if (bdone
< ht
->ht_used
)
3821 while (HASHITEM_EMPTY(hi
))
3823 return cat_prefix_varname('b', hi
->hi_key
);
3825 if (bdone
== ht
->ht_used
)
3828 return (char_u
*)"b:changedtick";
3832 ht
= &curwin
->w_vars
.dv_hashtab
;
3833 if (wdone
< ht
->ht_used
)
3839 while (HASHITEM_EMPTY(hi
))
3841 return cat_prefix_varname('w', hi
->hi_key
);
3846 ht
= &curtab
->tp_vars
.dv_hashtab
;
3847 if (tdone
< ht
->ht_used
)
3853 while (HASHITEM_EMPTY(hi
))
3855 return cat_prefix_varname('t', hi
->hi_key
);
3861 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3863 vim_free(varnamebuf
);
3869 #endif /* FEAT_CMDL_COMPL */
3872 * types for expressions.
3877 , TYPE_EQUAL
/* == */
3878 , TYPE_NEQUAL
/* != */
3879 , TYPE_GREATER
/* > */
3880 , TYPE_GEQUAL
/* >= */
3881 , TYPE_SMALLER
/* < */
3882 , TYPE_SEQUAL
/* <= */
3883 , TYPE_MATCH
/* =~ */
3884 , TYPE_NOMATCH
/* !~ */
3888 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3889 * executed. The function may return OK, but the rettv will be of type
3890 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3894 * Handle zero level expression.
3895 * This calls eval1() and handles error message and nextcmd.
3896 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3897 * Note: "rettv.v_lock" is not set.
3898 * Return OK or FAIL.
3901 eval0(arg
, rettv
, nextcmd
, evaluate
)
3911 ret
= eval1(&p
, rettv
, evaluate
);
3912 if (ret
== FAIL
|| !ends_excmd(*p
))
3917 * Report the invalid expression unless the expression evaluation has
3918 * been cancelled due to an aborting error, an interrupt, or an
3922 EMSG2(_(e_invexpr2
), arg
);
3925 if (nextcmd
!= NULL
)
3926 *nextcmd
= check_nextcmd(p
);
3932 * Handle top level expression:
3933 * expr2 ? expr1 : expr1
3935 * "arg" must point to the first non-white of the expression.
3936 * "arg" is advanced to the next non-white after the recognized expression.
3938 * Note: "rettv.v_lock" is not set.
3940 * Return OK or FAIL.
3943 eval1(arg
, rettv
, evaluate
)
3952 * Get the first variable.
3954 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3957 if ((*arg
)[0] == '?')
3964 if (get_tv_number_chk(rettv
, &error
) != 0)
3972 * Get the second variable.
3974 *arg
= skipwhite(*arg
+ 1);
3975 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3979 * Check for the ":".
3981 if ((*arg
)[0] != ':')
3983 EMSG(_("E109: Missing ':' after '?'"));
3984 if (evaluate
&& result
)
3990 * Get the third variable.
3992 *arg
= skipwhite(*arg
+ 1);
3993 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
3995 if (evaluate
&& result
)
3999 if (evaluate
&& !result
)
4007 * Handle first level expression:
4008 * expr2 || expr2 || expr2 logical OR
4010 * "arg" must point to the first non-white of the expression.
4011 * "arg" is advanced to the next non-white after the recognized expression.
4013 * Return OK or FAIL.
4016 eval2(arg
, rettv
, evaluate
)
4027 * Get the first variable.
4029 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
4033 * Repeat until there is no following "||".
4037 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
4039 if (evaluate
&& first
)
4041 if (get_tv_number_chk(rettv
, &error
) != 0)
4050 * Get the second variable.
4052 *arg
= skipwhite(*arg
+ 2);
4053 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
4057 * Compute the result.
4059 if (evaluate
&& !result
)
4061 if (get_tv_number_chk(&var2
, &error
) != 0)
4069 rettv
->v_type
= VAR_NUMBER
;
4070 rettv
->vval
.v_number
= result
;
4078 * Handle second level expression:
4079 * expr3 && expr3 && expr3 logical AND
4081 * "arg" must point to the first non-white of the expression.
4082 * "arg" is advanced to the next non-white after the recognized expression.
4084 * Return OK or FAIL.
4087 eval3(arg
, rettv
, evaluate
)
4098 * Get the first variable.
4100 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
4104 * Repeat until there is no following "&&".
4108 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
4110 if (evaluate
&& first
)
4112 if (get_tv_number_chk(rettv
, &error
) == 0)
4121 * Get the second variable.
4123 *arg
= skipwhite(*arg
+ 2);
4124 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4128 * Compute the result.
4130 if (evaluate
&& result
)
4132 if (get_tv_number_chk(&var2
, &error
) == 0)
4140 rettv
->v_type
= VAR_NUMBER
;
4141 rettv
->vval
.v_number
= result
;
4149 * Handle third level expression:
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4164 * Return OK or FAIL.
4167 eval4(arg
, rettv
, evaluate
)
4175 exptype_T type
= TYPE_UNKNOWN
;
4176 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4180 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4181 regmatch_T regmatch
;
4186 * Get the first variable.
4188 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4194 case '=': if (p
[1] == '=')
4196 else if (p
[1] == '~')
4199 case '!': if (p
[1] == '=')
4201 else if (p
[1] == '~')
4202 type
= TYPE_NOMATCH
;
4204 case '>': if (p
[1] != '=')
4206 type
= TYPE_GREATER
;
4212 case '<': if (p
[1] != '=')
4214 type
= TYPE_SMALLER
;
4220 case 'i': if (p
[1] == 's')
4222 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4224 if (!vim_isIDc(p
[len
]))
4226 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4234 * If there is a comparative operator, use it.
4236 if (type
!= TYPE_UNKNOWN
)
4238 /* extra question mark appended: ignore case */
4244 /* extra '#' appended: match case */
4245 else if (p
[len
] == '#')
4250 /* nothing appended: use 'ignorecase' */
4255 * Get the second variable.
4257 *arg
= skipwhite(p
+ len
);
4258 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4266 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4268 /* For "is" a different type always means FALSE, for "notis"
4270 n1
= (type
== TYPE_NEQUAL
);
4272 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4276 n1
= (rettv
->v_type
== var2
.v_type
4277 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4278 if (type
== TYPE_NEQUAL
)
4281 else if (rettv
->v_type
!= var2
.v_type
4282 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4284 if (rettv
->v_type
!= var2
.v_type
)
4285 EMSG(_("E691: Can only compare List with List"));
4287 EMSG(_("E692: Invalid operation for Lists"));
4294 /* Compare two Lists for being equal or unequal. */
4295 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4296 if (type
== TYPE_NEQUAL
)
4301 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4305 n1
= (rettv
->v_type
== var2
.v_type
4306 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4307 if (type
== TYPE_NEQUAL
)
4310 else if (rettv
->v_type
!= var2
.v_type
4311 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4313 if (rettv
->v_type
!= var2
.v_type
)
4314 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4316 EMSG(_("E736: Invalid operation for Dictionary"));
4323 /* Compare two Dictionaries for being equal or unequal. */
4324 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4325 if (type
== TYPE_NEQUAL
)
4330 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4332 if (rettv
->v_type
!= var2
.v_type
4333 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4335 if (rettv
->v_type
!= var2
.v_type
)
4336 EMSG(_("E693: Can only compare Funcref with Funcref"));
4338 EMSG(_("E694: Invalid operation for Funcrefs"));
4345 /* Compare two Funcrefs for being equal or unequal. */
4346 if (rettv
->vval
.v_string
== NULL
4347 || var2
.vval
.v_string
== NULL
)
4350 n1
= STRCMP(rettv
->vval
.v_string
,
4351 var2
.vval
.v_string
) == 0;
4352 if (type
== TYPE_NEQUAL
)
4359 * If one of the two variables is a float, compare as a float.
4360 * When using "=~" or "!~", always compare as string.
4362 else if ((rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4363 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4367 if (rettv
->v_type
== VAR_FLOAT
)
4368 f1
= rettv
->vval
.v_float
;
4370 f1
= get_tv_number(rettv
);
4371 if (var2
.v_type
== VAR_FLOAT
)
4372 f2
= var2
.vval
.v_float
;
4374 f2
= get_tv_number(&var2
);
4378 case TYPE_EQUAL
: n1
= (f1
== f2
); break;
4379 case TYPE_NEQUAL
: n1
= (f1
!= f2
); break;
4380 case TYPE_GREATER
: n1
= (f1
> f2
); break;
4381 case TYPE_GEQUAL
: n1
= (f1
>= f2
); break;
4382 case TYPE_SMALLER
: n1
= (f1
< f2
); break;
4383 case TYPE_SEQUAL
: n1
= (f1
<= f2
); break;
4386 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4392 * If one of the two variables is a number, compare as a number.
4393 * When using "=~" or "!~", always compare as string.
4395 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4396 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4398 n1
= get_tv_number(rettv
);
4399 n2
= get_tv_number(&var2
);
4402 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4403 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4404 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4405 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4406 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4407 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4410 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4415 s1
= get_tv_string_buf(rettv
, buf1
);
4416 s2
= get_tv_string_buf(&var2
, buf2
);
4417 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4418 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4424 case TYPE_EQUAL
: n1
= (i
== 0); break;
4425 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4426 case TYPE_GREATER
: n1
= (i
> 0); break;
4427 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4428 case TYPE_SMALLER
: n1
= (i
< 0); break;
4429 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4433 /* avoid 'l' flag in 'cpoptions' */
4435 p_cpo
= (char_u
*)"";
4436 regmatch
.regprog
= vim_regcomp(s2
,
4437 RE_MAGIC
+ RE_STRING
);
4438 regmatch
.rm_ic
= ic
;
4439 if (regmatch
.regprog
!= NULL
)
4441 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4442 vim_free(regmatch
.regprog
);
4443 if (type
== TYPE_NOMATCH
)
4449 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4454 rettv
->v_type
= VAR_NUMBER
;
4455 rettv
->vval
.v_number
= n1
;
4463 * Handle fourth level expression:
4465 * - number subtraction
4466 * . string concatenation
4468 * "arg" must point to the first non-white of the expression.
4469 * "arg" is advanced to the next non-white after the recognized expression.
4471 * Return OK or FAIL.
4474 eval5(arg
, rettv
, evaluate
)
4484 float_T f1
= 0, f2
= 0;
4487 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4491 * Get the first variable.
4493 if (eval6(arg
, rettv
, evaluate
, FALSE
) == FAIL
)
4497 * Repeat computing, until no '+', '-' or '.' is following.
4502 if (op
!= '+' && op
!= '-' && op
!= '.')
4505 if ((op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4507 && (op
== '.' || rettv
->v_type
!= VAR_FLOAT
)
4511 /* For "list + ...", an illegal use of the first operand as
4512 * a number cannot be determined before evaluating the 2nd
4513 * operand: if this is also a list, all is ok.
4514 * For "something . ...", "something - ..." or "non-list + ...",
4515 * we know that the first operand needs to be a string or number
4516 * without evaluating the 2nd operand. So check before to avoid
4517 * side effects after an error. */
4518 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4526 * Get the second variable.
4528 *arg
= skipwhite(*arg
+ 1);
4529 if (eval6(arg
, &var2
, evaluate
, op
== '.') == FAIL
)
4538 * Compute the result.
4542 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4543 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4544 if (s2
== NULL
) /* type error ? */
4550 p
= concat_str(s1
, s2
);
4552 rettv
->v_type
= VAR_STRING
;
4553 rettv
->vval
.v_string
= p
;
4555 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4556 && var2
.v_type
== VAR_LIST
)
4558 /* concatenate Lists */
4559 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4574 if (rettv
->v_type
== VAR_FLOAT
)
4576 f1
= rettv
->vval
.v_float
;
4582 n1
= get_tv_number_chk(rettv
, &error
);
4585 /* This can only happen for "list + non-list". For
4586 * "non-list + ..." or "something - ...", we returned
4587 * before evaluating the 2nd operand. */
4592 if (var2
.v_type
== VAR_FLOAT
)
4597 if (var2
.v_type
== VAR_FLOAT
)
4599 f2
= var2
.vval
.v_float
;
4605 n2
= get_tv_number_chk(&var2
, &error
);
4613 if (rettv
->v_type
== VAR_FLOAT
)
4620 /* If there is a float on either side the result is a float. */
4621 if (rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4627 rettv
->v_type
= VAR_FLOAT
;
4628 rettv
->vval
.v_float
= f1
;
4637 rettv
->v_type
= VAR_NUMBER
;
4638 rettv
->vval
.v_number
= n1
;
4648 * Handle fifth level expression:
4649 * * number multiplication
4653 * "arg" must point to the first non-white of the expression.
4654 * "arg" is advanced to the next non-white after the recognized expression.
4656 * Return OK or FAIL.
4659 eval6(arg
, rettv
, evaluate
, want_string
)
4663 int want_string
; /* after "." operator */
4669 int use_float
= FALSE
;
4675 * Get the first variable.
4677 if (eval7(arg
, rettv
, evaluate
, want_string
) == FAIL
)
4681 * Repeat computing, until no '*', '/' or '%' is following.
4686 if (op
!= '*' && op
!= '/' && op
!= '%')
4692 if (rettv
->v_type
== VAR_FLOAT
)
4694 f1
= rettv
->vval
.v_float
;
4700 n1
= get_tv_number_chk(rettv
, &error
);
4709 * Get the second variable.
4711 *arg
= skipwhite(*arg
+ 1);
4712 if (eval7(arg
, &var2
, evaluate
, FALSE
) == FAIL
)
4718 if (var2
.v_type
== VAR_FLOAT
)
4725 f2
= var2
.vval
.v_float
;
4731 n2
= get_tv_number_chk(&var2
, &error
);
4742 * Compute the result.
4743 * When either side is a float the result is a float.
4752 /* We rely on the floating point library to handle divide
4753 * by zero to result in "inf" and not a crash. */
4758 EMSG(_("E804: Cannot use '%' with Float"));
4761 rettv
->v_type
= VAR_FLOAT
;
4762 rettv
->vval
.v_float
= f1
;
4771 if (n2
== 0) /* give an error message? */
4774 n1
= -0x7fffffffL
- 1L; /* similar to NaN */
4785 if (n2
== 0) /* give an error message? */
4790 rettv
->v_type
= VAR_NUMBER
;
4791 rettv
->vval
.v_number
= n1
;
4800 * Handle sixth level expression:
4801 * number number constant
4802 * "string" string constant
4803 * 'string' literal string constant
4804 * &option-name option value
4805 * @r register contents
4806 * identifier variable value
4807 * function() function call
4808 * $VAR environment variable
4809 * (expression) nested expression
4811 * {key: val, key: val} Dictionary
4814 * ! in front logical NOT
4815 * - in front unary minus
4816 * + in front unary plus (ignored)
4817 * trailing [] subscript in String or List
4818 * trailing .name entry in Dictionary
4820 * "arg" must point to the first non-white of the expression.
4821 * "arg" is advanced to the next non-white after the recognized expression.
4823 * Return OK or FAIL.
4826 eval7(arg
, rettv
, evaluate
, want_string
)
4830 int want_string
; /* after "." operator */
4835 char_u
*start_leader
, *end_leader
;
4840 * Initialise variable so that clear_tv() can't mistake this for a
4841 * string and free a string that isn't there.
4843 rettv
->v_type
= VAR_UNKNOWN
;
4846 * Skip '!' and '-' characters. They are handled later.
4848 start_leader
= *arg
;
4849 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4850 *arg
= skipwhite(*arg
+ 1);
4870 char_u
*p
= skipdigits(*arg
+ 1);
4871 int get_float
= FALSE
;
4873 /* We accept a float when the format matches
4874 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4875 * strict to avoid backwards compatibility problems.
4876 * Don't look for a float after the "." operator, so that
4877 * ":let vers = 1.2.3" doesn't fail. */
4878 if (!want_string
&& p
[0] == '.' && vim_isdigit(p
[1]))
4881 p
= skipdigits(p
+ 2);
4882 if (*p
== 'e' || *p
== 'E')
4885 if (*p
== '-' || *p
== '+')
4887 if (!vim_isdigit(*p
))
4890 p
= skipdigits(p
+ 1);
4892 if (ASCII_ISALPHA(*p
) || *p
== '.')
4899 *arg
+= string2float(*arg
, &f
);
4902 rettv
->v_type
= VAR_FLOAT
;
4903 rettv
->vval
.v_float
= f
;
4909 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4913 rettv
->v_type
= VAR_NUMBER
;
4914 rettv
->vval
.v_number
= n
;
4921 * String constant: "string".
4923 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4927 * Literal string constant: 'str''ing'.
4929 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4933 * List: [expr, expr]
4935 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4939 * Dictionary: {key: val, key: val}
4941 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4945 * Option value: &name
4947 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4951 * Environment variable: $VAR.
4953 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4957 * Register contents: @r.
4962 rettv
->v_type
= VAR_STRING
;
4963 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4970 * nested expression: (expression).
4972 case '(': *arg
= skipwhite(*arg
+ 1);
4973 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4978 EMSG(_("E110: Missing ')'"));
4984 default: ret
= NOTDONE
;
4991 * Must be a variable or function name.
4992 * Can also be a curly-braces kind of name: {expr}.
4995 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
5003 if (**arg
== '(') /* recursive! */
5005 /* If "s" is the name of a variable of type VAR_FUNC
5006 * use its contents. */
5007 s
= deref_func_name(s
, &len
);
5009 /* Invoke the function. */
5010 ret
= get_func_tv(s
, len
, rettv
, arg
,
5011 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
5012 &len
, evaluate
, NULL
);
5013 /* Stop the expression evaluation when immediately
5014 * aborting on error, or when an interrupt occurred or
5015 * an exception was thrown but not caught. */
5024 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
5033 *arg
= skipwhite(*arg
);
5035 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5038 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
5041 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5043 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
5050 if (rettv
->v_type
== VAR_FLOAT
)
5051 f
= rettv
->vval
.v_float
;
5054 val
= get_tv_number_chk(rettv
, &error
);
5062 while (end_leader
> start_leader
)
5065 if (*end_leader
== '!')
5068 if (rettv
->v_type
== VAR_FLOAT
)
5074 else if (*end_leader
== '-')
5077 if (rettv
->v_type
== VAR_FLOAT
)
5085 if (rettv
->v_type
== VAR_FLOAT
)
5088 rettv
->vval
.v_float
= f
;
5094 rettv
->v_type
= VAR_NUMBER
;
5095 rettv
->vval
.v_number
= val
;
5104 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5105 * "*arg" points to the '[' or '.'.
5106 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5109 eval_index(arg
, rettv
, evaluate
, verbose
)
5113 int verbose
; /* give error messages */
5115 int empty1
= FALSE
, empty2
= FALSE
;
5116 typval_T var1
, var2
;
5123 if (rettv
->v_type
== VAR_FUNC
5125 || rettv
->v_type
== VAR_FLOAT
5130 EMSG(_("E695: Cannot index a Funcref"));
5140 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
5144 *arg
= skipwhite(key
+ len
);
5151 * Get the (first) variable from inside the [].
5153 *arg
= skipwhite(*arg
+ 1);
5156 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
5158 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
5160 /* not a number or string */
5166 * Get the second variable from inside the [:].
5171 *arg
= skipwhite(*arg
+ 1);
5174 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
5180 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
5182 /* not a number or string */
5190 /* Check for the ']'. */
5194 EMSG(_(e_missbrac
));
5200 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
5206 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
5208 n1
= get_tv_number(&var1
);
5217 n2
= get_tv_number(&var2
);
5222 switch (rettv
->v_type
)
5226 s
= get_tv_string(rettv
);
5227 len
= (long)STRLEN(s
);
5230 /* The resulting variable is a substring. If the indexes
5231 * are out of range the result is empty. */
5242 if (n1
>= len
|| n2
< 0 || n1
> n2
)
5245 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
5249 /* The resulting variable is a string of a single
5250 * character. If the index is too big or negative the
5251 * result is empty. */
5252 if (n1
>= len
|| n1
< 0)
5255 s
= vim_strnsave(s
+ n1
, 1);
5258 rettv
->v_type
= VAR_STRING
;
5259 rettv
->vval
.v_string
= s
;
5263 len
= list_len(rettv
->vval
.v_list
);
5266 if (!empty1
&& (n1
< 0 || n1
>= len
))
5268 /* For a range we allow invalid values and return an empty
5269 * list. A list index out of range is an error. */
5273 EMSGN(_(e_listidx
), n1
);
5287 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
5292 for (item
= list_find(rettv
->vval
.v_list
, n1
);
5295 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
5300 item
= item
->li_next
;
5303 rettv
->v_type
= VAR_LIST
;
5304 rettv
->vval
.v_list
= l
;
5309 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
5319 EMSG(_(e_dictrange
));
5329 key
= get_tv_string(&var1
);
5333 EMSG(_(e_emptykey
));
5339 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
5341 if (item
== NULL
&& verbose
)
5342 EMSG2(_(e_dictkey
), key
);
5348 copy_tv(&item
->di_tv
, &var1
);
5360 * Get an option value.
5361 * "arg" points to the '&' or '+' before the option name.
5362 * "arg" is advanced to character after the option name.
5363 * Return OK or FAIL.
5366 get_option_tv(arg
, rettv
, evaluate
)
5368 typval_T
*rettv
; /* when NULL, only check if option exists */
5376 int working
= (**arg
== '+'); /* has("+option") */
5381 * Isolate the option name and find its value.
5383 option_end
= find_option_end(arg
, &opt_flags
);
5384 if (option_end
== NULL
)
5387 EMSG2(_("E112: Option name missing: %s"), *arg
);
5399 opt_type
= get_option_value(*arg
, &numval
,
5400 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5402 if (opt_type
== -3) /* invalid name */
5405 EMSG2(_("E113: Unknown option: %s"), *arg
);
5408 else if (rettv
!= NULL
)
5410 if (opt_type
== -2) /* hidden string option */
5412 rettv
->v_type
= VAR_STRING
;
5413 rettv
->vval
.v_string
= NULL
;
5415 else if (opt_type
== -1) /* hidden number option */
5417 rettv
->v_type
= VAR_NUMBER
;
5418 rettv
->vval
.v_number
= 0;
5420 else if (opt_type
== 1) /* number option */
5422 rettv
->v_type
= VAR_NUMBER
;
5423 rettv
->vval
.v_number
= numval
;
5425 else /* string option */
5427 rettv
->v_type
= VAR_STRING
;
5428 rettv
->vval
.v_string
= stringval
;
5431 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5434 *option_end
= c
; /* put back for error messages */
5441 * Allocate a variable for a string constant.
5442 * Return OK or FAIL.
5445 get_string_tv(arg
, rettv
, evaluate
)
5455 * Find the end of the string, skipping backslashed characters.
5457 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5459 if (*p
== '\\' && p
[1] != NUL
)
5462 /* A "\<x>" form occupies at least 4 characters, and produces up
5463 * to 6 characters: reserve space for 2 extra */
5471 EMSG2(_("E114: Missing quote: %s"), *arg
);
5475 /* If only parsing, set *arg and return here */
5483 * Copy the string into allocated memory, handling backslashed
5486 name
= alloc((unsigned)(p
- *arg
+ extra
));
5489 rettv
->v_type
= VAR_STRING
;
5490 rettv
->vval
.v_string
= name
;
5492 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5498 case 'b': *name
++ = BS
; ++p
; break;
5499 case 'e': *name
++ = ESC
; ++p
; break;
5500 case 'f': *name
++ = FF
; ++p
; break;
5501 case 'n': *name
++ = NL
; ++p
; break;
5502 case 'r': *name
++ = CAR
; ++p
; break;
5503 case 't': *name
++ = TAB
; ++p
; break;
5505 case 'X': /* hex: "\x1", "\x12" */
5507 case 'u': /* Unicode: "\u0023" */
5509 if (vim_isxdigit(p
[1]))
5512 int c
= toupper(*p
);
5519 while (--n
>= 0 && vim_isxdigit(p
[1]))
5522 nr
= (nr
<< 4) + hex2nr(*p
);
5526 /* For "\u" store the number according to
5529 name
+= (*mb_char2bytes
)(nr
, name
);
5536 /* octal: "\1", "\12", "\123" */
5544 case '7': *name
= *p
++ - '0';
5545 if (*p
>= '0' && *p
<= '7')
5547 *name
= (*name
<< 3) + *p
++ - '0';
5548 if (*p
>= '0' && *p
<= '7')
5549 *name
= (*name
<< 3) + *p
++ - '0';
5554 /* Special key, e.g.: "\<C-W>" */
5555 case '<': extra
= trans_special(&p
, name
, TRUE
);
5563 default: MB_COPY_CHAR(p
, name
);
5568 MB_COPY_CHAR(p
, name
);
5578 * Allocate a variable for a 'str''ing' constant.
5579 * Return OK or FAIL.
5582 get_lit_string_tv(arg
, rettv
, evaluate
)
5592 * Find the end of the string, skipping ''.
5594 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5607 EMSG2(_("E115: Missing quote: %s"), *arg
);
5611 /* If only parsing return after setting "*arg" */
5619 * Copy the string into allocated memory, handling '' to ' reduction.
5621 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5624 rettv
->v_type
= VAR_STRING
;
5625 rettv
->vval
.v_string
= str
;
5627 for (p
= *arg
+ 1; *p
!= NUL
; )
5635 MB_COPY_CHAR(p
, str
);
5644 * Allocate a variable for a List and fill it from "*arg".
5645 * Return OK or FAIL.
5648 get_list_tv(arg
, rettv
, evaluate
)
5664 *arg
= skipwhite(*arg
+ 1);
5665 while (**arg
!= ']' && **arg
!= NUL
)
5667 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5671 item
= listitem_alloc();
5675 item
->li_tv
.v_lock
= 0;
5676 list_append(l
, item
);
5686 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5689 *arg
= skipwhite(*arg
+ 1);
5694 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5701 *arg
= skipwhite(*arg
+ 1);
5704 rettv
->v_type
= VAR_LIST
;
5705 rettv
->vval
.v_list
= l
;
5713 * Allocate an empty header for a list.
5714 * Caller should take care of the reference count.
5721 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5724 /* Prepend the list to the list of lists for garbage collection. */
5725 if (first_list
!= NULL
)
5726 first_list
->lv_used_prev
= l
;
5727 l
->lv_used_prev
= NULL
;
5728 l
->lv_used_next
= first_list
;
5735 * Allocate an empty list for a return value.
5736 * Returns OK or FAIL.
5739 rettv_list_alloc(rettv
)
5742 list_T
*l
= list_alloc();
5747 rettv
->vval
.v_list
= l
;
5748 rettv
->v_type
= VAR_LIST
;
5754 * Unreference a list: decrement the reference count and free it when it
5761 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5766 * Free a list, including all items it points to.
5767 * Ignores the reference count.
5770 list_free(l
, recurse
)
5772 int recurse
; /* Free Lists and Dictionaries recursively. */
5776 /* Remove the list from the list of lists for garbage collection. */
5777 if (l
->lv_used_prev
== NULL
)
5778 first_list
= l
->lv_used_next
;
5780 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5781 if (l
->lv_used_next
!= NULL
)
5782 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5784 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5786 /* Remove the item before deleting it. */
5787 l
->lv_first
= item
->li_next
;
5788 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5789 && item
->li_tv
.v_type
!= VAR_DICT
))
5790 clear_tv(&item
->li_tv
);
5797 * Allocate a list item.
5802 return (listitem_T
*)alloc(sizeof(listitem_T
));
5806 * Free a list item. Also clears the value. Does not notify watchers.
5812 clear_tv(&item
->li_tv
);
5817 * Remove a list item from a List and free it. Also clears the value.
5820 listitem_remove(l
, item
)
5824 list_remove(l
, item
, item
);
5825 listitem_free(item
);
5829 * Get the number of items in a list.
5841 * Return TRUE when two lists have exactly the same values.
5844 list_equal(l1
, l2
, ic
)
5847 int ic
; /* ignore case for strings */
5849 listitem_T
*item1
, *item2
;
5851 if (l1
== NULL
|| l2
== NULL
)
5855 if (list_len(l1
) != list_len(l2
))
5858 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5859 item1
!= NULL
&& item2
!= NULL
;
5860 item1
= item1
->li_next
, item2
= item2
->li_next
)
5861 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5863 return item1
== NULL
&& item2
== NULL
;
5866 #if defined(FEAT_PYTHON) || defined(PROTO) || defined(FEAT_GUI_MACVIM)
5868 * Return the dictitem that an entry in a hashtable points to.
5879 * Return TRUE when two dictionaries have exactly the same key/values.
5882 dict_equal(d1
, d2
, ic
)
5885 int ic
; /* ignore case for strings */
5891 if (d1
== NULL
|| d2
== NULL
)
5895 if (dict_len(d1
) != dict_len(d2
))
5898 todo
= (int)d1
->dv_hashtab
.ht_used
;
5899 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5901 if (!HASHITEM_EMPTY(hi
))
5903 item2
= dict_find(d2
, hi
->hi_key
, -1);
5906 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5915 * Return TRUE if "tv1" and "tv2" have the same value.
5916 * Compares the items just like "==" would compare them, but strings and
5917 * numbers are different. Floats and numbers are also different.
5920 tv_equal(tv1
, tv2
, ic
)
5923 int ic
; /* ignore case */
5925 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5927 static int recursive
= 0; /* cach recursive loops */
5930 if (tv1
->v_type
!= tv2
->v_type
)
5932 /* Catch lists and dicts that have an endless loop by limiting
5933 * recursiveness to 1000. We guess they are equal then. */
5934 if (recursive
>= 1000)
5937 switch (tv1
->v_type
)
5941 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5947 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5952 return (tv1
->vval
.v_string
!= NULL
5953 && tv2
->vval
.v_string
!= NULL
5954 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5957 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5961 return tv1
->vval
.v_float
== tv2
->vval
.v_float
;
5965 s1
= get_tv_string_buf(tv1
, buf1
);
5966 s2
= get_tv_string_buf(tv2
, buf2
);
5967 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5970 EMSG2(_(e_intern2
), "tv_equal()");
5975 * Locate item with index "n" in list "l" and return it.
5976 * A negative index is counted from the end; -1 is the last item.
5977 * Returns NULL when "n" is out of range.
5990 /* Negative index is relative to the end. */
5994 /* Check for index out of range. */
5995 if (n
< 0 || n
>= l
->lv_len
)
5998 /* When there is a cached index may start search from there. */
5999 if (l
->lv_idx_item
!= NULL
)
6001 if (n
< l
->lv_idx
/ 2)
6003 /* closest to the start of the list */
6007 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
6009 /* closest to the end of the list */
6011 idx
= l
->lv_len
- 1;
6015 /* closest to the cached index */
6016 item
= l
->lv_idx_item
;
6022 if (n
< l
->lv_len
/ 2)
6024 /* closest to the start of the list */
6030 /* closest to the end of the list */
6032 idx
= l
->lv_len
- 1;
6038 /* search forward */
6039 item
= item
->li_next
;
6044 /* search backward */
6045 item
= item
->li_prev
;
6049 /* cache the used index */
6051 l
->lv_idx_item
= item
;
6057 * Get list item "l[idx]" as a number.
6060 list_find_nr(l
, idx
, errorp
)
6063 int *errorp
; /* set to TRUE when something wrong */
6067 li
= list_find(l
, idx
);
6074 return get_tv_number_chk(&li
->li_tv
, errorp
);
6078 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6081 list_find_str(l
, idx
)
6087 li
= list_find(l
, idx
- 1);
6090 EMSGN(_(e_listidx
), idx
);
6093 return get_tv_string(&li
->li_tv
);
6097 * Locate "item" list "l" and return its index.
6098 * Returns -1 when "item" is not in the list.
6101 list_idx_of_item(l
, item
)
6111 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
6119 * Append item "item" to the end of list "l".
6122 list_append(l
, item
)
6126 if (l
->lv_last
== NULL
)
6131 item
->li_prev
= NULL
;
6135 l
->lv_last
->li_next
= item
;
6136 item
->li_prev
= l
->lv_last
;
6140 item
->li_next
= NULL
;
6144 * Append typval_T "tv" to the end of list "l".
6145 * Return FAIL when out of memory.
6148 list_append_tv(l
, tv
)
6152 listitem_T
*li
= listitem_alloc();
6156 copy_tv(tv
, &li
->li_tv
);
6162 * Add a dictionary to a list. Used by getqflist().
6163 * Return FAIL when out of memory.
6166 list_append_dict(list
, dict
)
6170 listitem_T
*li
= listitem_alloc();
6174 li
->li_tv
.v_type
= VAR_DICT
;
6175 li
->li_tv
.v_lock
= 0;
6176 li
->li_tv
.vval
.v_dict
= dict
;
6177 list_append(list
, li
);
6178 ++dict
->dv_refcount
;
6183 * Make a copy of "str" and append it as an item to list "l".
6184 * When "len" >= 0 use "str[len]".
6185 * Returns FAIL when out of memory.
6188 list_append_string(l
, str
, len
)
6193 listitem_T
*li
= listitem_alloc();
6198 li
->li_tv
.v_type
= VAR_STRING
;
6199 li
->li_tv
.v_lock
= 0;
6201 li
->li_tv
.vval
.v_string
= NULL
;
6202 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
6203 : vim_strsave(str
))) == NULL
)
6209 * Append "n" to list "l".
6210 * Returns FAIL when out of memory.
6213 list_append_number(l
, n
)
6219 li
= listitem_alloc();
6222 li
->li_tv
.v_type
= VAR_NUMBER
;
6223 li
->li_tv
.v_lock
= 0;
6224 li
->li_tv
.vval
.v_number
= n
;
6230 * Insert typval_T "tv" in list "l" before "item".
6231 * If "item" is NULL append at the end.
6232 * Return FAIL when out of memory.
6235 list_insert_tv(l
, tv
, item
)
6240 listitem_T
*ni
= listitem_alloc();
6244 copy_tv(tv
, &ni
->li_tv
);
6246 /* Append new item at end of list. */
6250 /* Insert new item before existing item. */
6251 ni
->li_prev
= item
->li_prev
;
6253 if (item
->li_prev
== NULL
)
6260 item
->li_prev
->li_next
= ni
;
6261 l
->lv_idx_item
= NULL
;
6270 * Extend "l1" with "l2".
6271 * If "bef" is NULL append at the end, otherwise insert before this item.
6272 * Returns FAIL when out of memory.
6275 list_extend(l1
, l2
, bef
)
6281 int todo
= l2
->lv_len
;
6283 /* We also quit the loop when we have inserted the original item count of
6284 * the list, avoid a hang when we extend a list with itself. */
6285 for (item
= l2
->lv_first
; item
!= NULL
&& --todo
>= 0; item
= item
->li_next
)
6286 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
6292 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6293 * Return FAIL when out of memory.
6296 list_concat(l1
, l2
, tv
)
6303 if (l1
== NULL
|| l2
== NULL
)
6306 /* make a copy of the first list. */
6307 l
= list_copy(l1
, FALSE
, 0);
6310 tv
->v_type
= VAR_LIST
;
6311 tv
->vval
.v_list
= l
;
6313 /* append all items from the second list */
6314 return list_extend(l
, l2
, NULL
);
6318 * Make a copy of list "orig". Shallow if "deep" is FALSE.
6319 * The refcount of the new list is set to 1.
6320 * See item_copy() for "copyID".
6321 * Returns NULL when out of memory.
6324 list_copy(orig
, deep
, copyID
)
6336 copy
= list_alloc();
6341 /* Do this before adding the items, because one of the items may
6342 * refer back to this list. */
6343 orig
->lv_copyID
= copyID
;
6344 orig
->lv_copylist
= copy
;
6346 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
6347 item
= item
->li_next
)
6349 ni
= listitem_alloc();
6354 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
6361 copy_tv(&item
->li_tv
, &ni
->li_tv
);
6362 list_append(copy
, ni
);
6364 ++copy
->lv_refcount
;
6376 * Remove items "item" to "item2" from list "l".
6377 * Does not free the listitem or the value!
6380 list_remove(l
, item
, item2
)
6387 /* notify watchers */
6388 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6391 list_fix_watch(l
, ip
);
6396 if (item2
->li_next
== NULL
)
6397 l
->lv_last
= item
->li_prev
;
6399 item2
->li_next
->li_prev
= item
->li_prev
;
6400 if (item
->li_prev
== NULL
)
6401 l
->lv_first
= item2
->li_next
;
6403 item
->li_prev
->li_next
= item2
->li_next
;
6404 l
->lv_idx_item
= NULL
;
6408 * Return an allocated string with the string representation of a list.
6412 list2string(tv
, copyID
)
6418 if (tv
->vval
.v_list
== NULL
)
6420 ga_init2(&ga
, (int)sizeof(char), 80);
6421 ga_append(&ga
, '[');
6422 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6424 vim_free(ga
.ga_data
);
6427 ga_append(&ga
, ']');
6428 ga_append(&ga
, NUL
);
6429 return (char_u
*)ga
.ga_data
;
6433 * Join list "l" into a string in "*gap", using separator "sep".
6434 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6435 * Return FAIL or OK.
6438 list_join(gap
, l
, sep
, echo
, copyID
)
6447 char_u numbuf
[NUMBUFLEN
];
6451 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6456 ga_concat(gap
, sep
);
6459 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6461 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6472 * Garbage collection for lists and dictionaries.
6474 * We use reference counts to be able to free most items right away when they
6475 * are no longer used. But for composite items it's possible that it becomes
6476 * unused while the reference count is > 0: When there is a recursive
6477 * reference. Example:
6478 * :let l = [1, 2, 3]
6482 * Since this is quite unusual we handle this with garbage collection: every
6483 * once in a while find out which lists and dicts are not referenced from any
6486 * Here is a good reference text about garbage collection (refers to Python
6487 * but it applies to all reference-counting mechanisms):
6488 * http://python.ca/nas/python/gc/
6492 * Do garbage collection for lists and dicts.
6493 * Return TRUE if some memory was freed.
6500 int copyID
= ++current_copyID
;
6504 funccall_T
*fc
, **pfc
;
6505 int did_free
= FALSE
;
6510 /* Only do this once. */
6511 want_garbage_collect
= FALSE
;
6512 may_garbage_collect
= FALSE
;
6513 garbage_collect_at_exit
= FALSE
;
6516 * 1. Go through all accessible variables and mark all lists and dicts
6519 /* script-local variables */
6520 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6521 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6523 /* buffer-local variables */
6524 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6525 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6527 /* window-local variables */
6528 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6529 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6532 /* tabpage-local variables */
6533 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6534 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6537 /* global variables */
6538 set_ref_in_ht(&globvarht
, copyID
);
6540 /* function-local variables */
6541 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6543 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6544 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6548 set_ref_in_ht(&vimvarht
, copyID
);
6551 * 2. Go through the list of dicts and free items without the copyID.
6553 for (dd
= first_dict
; dd
!= NULL
; )
6554 if (dd
->dv_copyID
!= copyID
)
6556 /* Free the Dictionary and ordinary items it contains, but don't
6557 * recurse into Lists and Dictionaries, they will be in the list
6558 * of dicts or list of lists. */
6559 dict_free(dd
, FALSE
);
6562 /* restart, next dict may also have been freed */
6566 dd
= dd
->dv_used_next
;
6569 * 3. Go through the list of lists and free items without the copyID.
6570 * But don't free a list that has a watcher (used in a for loop), these
6571 * are not referenced anywhere.
6573 for (ll
= first_list
; ll
!= NULL
; )
6574 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6576 /* Free the List and ordinary items it contains, but don't recurse
6577 * into Lists and Dictionaries, they will be in the list of dicts
6578 * or list of lists. */
6579 list_free(ll
, FALSE
);
6582 /* restart, next list may also have been freed */
6586 ll
= ll
->lv_used_next
;
6588 /* check if any funccal can be freed now */
6589 for (pfc
= &previous_funccal
; *pfc
!= NULL
; )
6591 if (can_free_funccal(*pfc
, copyID
))
6595 free_funccal(fc
, TRUE
);
6599 pfc
= &(*pfc
)->caller
;
6606 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6609 set_ref_in_ht(ht
, copyID
)
6616 todo
= (int)ht
->ht_used
;
6617 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6618 if (!HASHITEM_EMPTY(hi
))
6621 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6626 * Mark all lists and dicts referenced through list "l" with "copyID".
6629 set_ref_in_list(l
, copyID
)
6635 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6636 set_ref_in_item(&li
->li_tv
, copyID
);
6640 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6643 set_ref_in_item(tv
, copyID
)
6653 dd
= tv
->vval
.v_dict
;
6654 if (dd
!= NULL
&& dd
->dv_copyID
!= copyID
)
6656 /* Didn't see this dict yet. */
6657 dd
->dv_copyID
= copyID
;
6658 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6663 ll
= tv
->vval
.v_list
;
6664 if (ll
!= NULL
&& ll
->lv_copyID
!= copyID
)
6666 /* Didn't see this list yet. */
6667 ll
->lv_copyID
= copyID
;
6668 set_ref_in_list(ll
, copyID
);
6676 * Allocate an empty header for a dictionary.
6683 d
= (dict_T
*)alloc(sizeof(dict_T
));
6686 /* Add the list to the list of dicts for garbage collection. */
6687 if (first_dict
!= NULL
)
6688 first_dict
->dv_used_prev
= d
;
6689 d
->dv_used_next
= first_dict
;
6690 d
->dv_used_prev
= NULL
;
6693 hash_init(&d
->dv_hashtab
);
6702 * Unreference a Dictionary: decrement the reference count and free it when it
6709 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6714 * Free a Dictionary, including all items it contains.
6715 * Ignores the reference count.
6718 dict_free(d
, recurse
)
6720 int recurse
; /* Free Lists and Dictionaries recursively. */
6726 /* Remove the dict from the list of dicts for garbage collection. */
6727 if (d
->dv_used_prev
== NULL
)
6728 first_dict
= d
->dv_used_next
;
6730 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6731 if (d
->dv_used_next
!= NULL
)
6732 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6734 /* Lock the hashtab, we don't want it to resize while freeing items. */
6735 hash_lock(&d
->dv_hashtab
);
6736 todo
= (int)d
->dv_hashtab
.ht_used
;
6737 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6739 if (!HASHITEM_EMPTY(hi
))
6741 /* Remove the item before deleting it, just in case there is
6742 * something recursive causing trouble. */
6744 hash_remove(&d
->dv_hashtab
, hi
);
6745 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6746 && di
->di_tv
.v_type
!= VAR_DICT
))
6747 clear_tv(&di
->di_tv
);
6752 hash_clear(&d
->dv_hashtab
);
6757 * Allocate a Dictionary item.
6758 * The "key" is copied to the new item.
6759 * Note that the value of the item "di_tv" still needs to be initialized!
6760 * Returns NULL when out of memory.
6768 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6771 STRCPY(di
->di_key
, key
);
6778 * Make a copy of a Dictionary item.
6786 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6787 + STRLEN(org
->di_key
)));
6790 STRCPY(di
->di_key
, org
->di_key
);
6792 copy_tv(&org
->di_tv
, &di
->di_tv
);
6798 * Remove item "item" from Dictionary "dict" and free it.
6801 dictitem_remove(dict
, item
)
6807 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6808 if (HASHITEM_EMPTY(hi
))
6809 EMSG2(_(e_intern2
), "dictitem_remove()");
6811 hash_remove(&dict
->dv_hashtab
, hi
);
6812 dictitem_free(item
);
6816 * Free a dict item. Also clears the value.
6822 clear_tv(&item
->di_tv
);
6827 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6828 * The refcount of the new dict is set to 1.
6829 * See item_copy() for "copyID".
6830 * Returns NULL when out of memory.
6833 dict_copy(orig
, deep
, copyID
)
6846 copy
= dict_alloc();
6851 orig
->dv_copyID
= copyID
;
6852 orig
->dv_copydict
= copy
;
6854 todo
= (int)orig
->dv_hashtab
.ht_used
;
6855 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6857 if (!HASHITEM_EMPTY(hi
))
6861 di
= dictitem_alloc(hi
->hi_key
);
6866 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6874 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6875 if (dict_add(copy
, di
) == FAIL
)
6883 ++copy
->dv_refcount
;
6895 * Add item "item" to Dictionary "d".
6896 * Returns FAIL when out of memory and when key already existed.
6903 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6907 * Add a number or string entry to dictionary "d".
6908 * When "str" is NULL use number "nr", otherwise use "str".
6909 * Returns FAIL when out of memory and when key already exists.
6912 dict_add_nr_str(d
, key
, nr
, str
)
6920 item
= dictitem_alloc((char_u
*)key
);
6923 item
->di_tv
.v_lock
= 0;
6926 item
->di_tv
.v_type
= VAR_NUMBER
;
6927 item
->di_tv
.vval
.v_number
= nr
;
6931 item
->di_tv
.v_type
= VAR_STRING
;
6932 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6934 if (dict_add(d
, item
) == FAIL
)
6936 dictitem_free(item
);
6943 * Get the number of items in a Dictionary.
6951 return (long)d
->dv_hashtab
.ht_used
;
6955 * Find item "key[len]" in Dictionary "d".
6956 * If "len" is negative use strlen(key).
6957 * Returns NULL when not found.
6960 dict_find(d
, key
, len
)
6966 char_u buf
[AKEYLEN
];
6968 char_u
*tofree
= NULL
;
6973 else if (len
>= AKEYLEN
)
6975 tofree
= akey
= vim_strnsave(key
, len
);
6981 /* Avoid a malloc/free by using buf[]. */
6982 vim_strncpy(buf
, key
, len
);
6986 hi
= hash_find(&d
->dv_hashtab
, akey
);
6988 if (HASHITEM_EMPTY(hi
))
6994 * Get a string item from a dictionary.
6995 * When "save" is TRUE allocate memory for it.
6996 * Returns NULL if the entry doesn't exist or out of memory.
6999 get_dict_string(d
, key
, save
)
7007 di
= dict_find(d
, key
, -1);
7010 s
= get_tv_string(&di
->di_tv
);
7011 if (save
&& s
!= NULL
)
7017 * Get a number item from a dictionary.
7018 * Returns 0 if the entry doesn't exist or out of memory.
7021 get_dict_number(d
, key
)
7027 di
= dict_find(d
, key
, -1);
7030 return get_tv_number(&di
->di_tv
);
7034 * Return an allocated string with the string representation of a Dictionary.
7038 dict2string(tv
, copyID
)
7045 char_u numbuf
[NUMBUFLEN
];
7051 if ((d
= tv
->vval
.v_dict
) == NULL
)
7053 ga_init2(&ga
, (int)sizeof(char), 80);
7054 ga_append(&ga
, '{');
7056 todo
= (int)d
->dv_hashtab
.ht_used
;
7057 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
7059 if (!HASHITEM_EMPTY(hi
))
7066 ga_concat(&ga
, (char_u
*)", ");
7068 tofree
= string_quote(hi
->hi_key
, FALSE
);
7071 ga_concat(&ga
, tofree
);
7074 ga_concat(&ga
, (char_u
*)": ");
7075 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
7085 vim_free(ga
.ga_data
);
7089 ga_append(&ga
, '}');
7090 ga_append(&ga
, NUL
);
7091 return (char_u
*)ga
.ga_data
;
7095 * Allocate a variable for a Dictionary and fill it from "*arg".
7096 * Return OK or FAIL. Returns NOTDONE for {expr}.
7099 get_dict_tv(arg
, rettv
, evaluate
)
7109 char_u
*start
= skipwhite(*arg
+ 1);
7110 char_u buf
[NUMBUFLEN
];
7113 * First check if it's not a curly-braces thing: {expr}.
7114 * Must do this without evaluating, otherwise a function may be called
7115 * twice. Unfortunately this means we need to call eval1() twice for the
7117 * But {} is an empty Dictionary.
7121 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
7133 tvkey
.v_type
= VAR_UNKNOWN
;
7134 tv
.v_type
= VAR_UNKNOWN
;
7136 *arg
= skipwhite(*arg
+ 1);
7137 while (**arg
!= '}' && **arg
!= NUL
)
7139 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
7143 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
7149 key
= get_tv_string_buf_chk(&tvkey
, buf
);
7150 if (key
== NULL
|| *key
== NUL
)
7152 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7154 EMSG(_(e_emptykey
));
7160 *arg
= skipwhite(*arg
+ 1);
7161 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
7169 item
= dict_find(d
, key
, -1);
7172 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
7177 item
= dictitem_alloc(key
);
7182 item
->di_tv
.v_lock
= 0;
7183 if (dict_add(d
, item
) == FAIL
)
7184 dictitem_free(item
);
7192 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
7195 *arg
= skipwhite(*arg
+ 1);
7200 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
7207 *arg
= skipwhite(*arg
+ 1);
7210 rettv
->v_type
= VAR_DICT
;
7211 rettv
->vval
.v_dict
= d
;
7219 * Return a string with the string representation of a variable.
7220 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7221 * "numbuf" is used for a number.
7222 * Does not put quotes around strings, as ":echo" displays values.
7223 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7227 echo_string(tv
, tofree
, numbuf
, copyID
)
7233 static int recurse
= 0;
7236 if (recurse
>= DICT_MAXNEST
)
7238 EMSG(_("E724: variable nested too deep for displaying"));
7248 r
= tv
->vval
.v_string
;
7252 if (tv
->vval
.v_list
== NULL
)
7257 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
7260 r
= (char_u
*)"[...]";
7264 tv
->vval
.v_list
->lv_copyID
= copyID
;
7265 *tofree
= list2string(tv
, copyID
);
7271 if (tv
->vval
.v_dict
== NULL
)
7276 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
7279 r
= (char_u
*)"{...}";
7283 tv
->vval
.v_dict
->dv_copyID
= copyID
;
7284 *tofree
= dict2string(tv
, copyID
);
7292 r
= get_tv_string_buf(tv
, numbuf
);
7298 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
->vval
.v_float
);
7304 EMSG2(_(e_intern2
), "echo_string()");
7313 * Return a string with the string representation of a variable.
7314 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7315 * "numbuf" is used for a number.
7316 * Puts quotes around strings, so that they can be parsed back by eval().
7320 tv2string(tv
, tofree
, numbuf
, copyID
)
7329 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
7332 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
7337 vim_snprintf((char *)numbuf
, NUMBUFLEN
- 1, "%g", tv
->vval
.v_float
);
7345 EMSG2(_(e_intern2
), "tv2string()");
7347 return echo_string(tv
, tofree
, numbuf
, copyID
);
7351 * Return string "str" in ' quotes, doubling ' characters.
7352 * If "str" is NULL an empty string is assumed.
7353 * If "function" is TRUE make it function('string').
7356 string_quote(str
, function
)
7363 len
= (function
? 13 : 3);
7366 len
+= (unsigned)STRLEN(str
);
7367 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
7376 STRCPY(r
, "function('");
7382 for (p
= str
; *p
!= NUL
; )
7398 * Convert the string "text" to a floating point number.
7399 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7400 * this always uses a decimal point.
7401 * Returns the length of the text that was consumed.
7404 string2float(text
, value
)
7406 float_T
*value
; /* result stored here */
7408 char *s
= (char *)text
;
7413 return (int)((char_u
*)s
- text
);
7418 * Get the value of an environment variable.
7419 * "arg" is pointing to the '$'. It is advanced to after the name.
7420 * If the environment variable was not set, silently assume it is empty.
7424 get_env_tv(arg
, rettv
, evaluate
)
7429 char_u
*string
= NULL
;
7433 int mustfree
= FALSE
;
7437 len
= get_env_len(arg
);
7444 /* first try vim_getenv(), fast for normal environment vars */
7445 string
= vim_getenv(name
, &mustfree
);
7446 if (string
!= NULL
&& *string
!= NUL
)
7449 string
= vim_strsave(string
);
7456 /* next try expanding things like $VIM and ${HOME} */
7457 string
= expand_env_save(name
- 1);
7458 if (string
!= NULL
&& *string
== '$')
7466 rettv
->v_type
= VAR_STRING
;
7467 rettv
->vval
.v_string
= string
;
7474 * Array with names and number of arguments of all internal functions
7475 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7479 char *f_name
; /* function name */
7480 char f_min_argc
; /* minimal number of arguments */
7481 char f_max_argc
; /* maximal number of arguments */
7482 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7483 /* implementation of function */
7487 {"abs", 1, 1, f_abs
},
7489 {"add", 2, 2, f_add
},
7490 {"append", 2, 2, f_append
},
7491 {"argc", 0, 0, f_argc
},
7492 {"argidx", 0, 0, f_argidx
},
7493 {"argv", 0, 1, f_argv
},
7495 {"atan", 1, 1, f_atan
},
7497 {"browse", 4, 4, f_browse
},
7498 {"browsedir", 2, 2, f_browsedir
},
7499 {"bufexists", 1, 1, f_bufexists
},
7500 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7501 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7502 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7503 {"buflisted", 1, 1, f_buflisted
},
7504 {"bufloaded", 1, 1, f_bufloaded
},
7505 {"bufname", 1, 1, f_bufname
},
7506 {"bufnr", 1, 2, f_bufnr
},
7507 {"bufwinnr", 1, 1, f_bufwinnr
},
7508 {"byte2line", 1, 1, f_byte2line
},
7509 {"byteidx", 2, 2, f_byteidx
},
7510 {"call", 2, 3, f_call
},
7512 {"ceil", 1, 1, f_ceil
},
7514 {"changenr", 0, 0, f_changenr
},
7515 {"char2nr", 1, 1, f_char2nr
},
7516 {"cindent", 1, 1, f_cindent
},
7517 {"clearmatches", 0, 0, f_clearmatches
},
7518 {"col", 1, 1, f_col
},
7519 #if defined(FEAT_INS_EXPAND)
7520 {"complete", 2, 2, f_complete
},
7521 {"complete_add", 1, 1, f_complete_add
},
7522 {"complete_check", 0, 0, f_complete_check
},
7524 {"confirm", 1, 4, f_confirm
},
7525 {"copy", 1, 1, f_copy
},
7527 {"cos", 1, 1, f_cos
},
7529 {"count", 2, 4, f_count
},
7530 {"cscope_connection",0,3, f_cscope_connection
},
7531 {"cursor", 1, 3, f_cursor
},
7532 {"deepcopy", 1, 2, f_deepcopy
},
7533 {"delete", 1, 1, f_delete
},
7534 {"did_filetype", 0, 0, f_did_filetype
},
7535 {"diff_filler", 1, 1, f_diff_filler
},
7536 {"diff_hlID", 2, 2, f_diff_hlID
},
7537 {"empty", 1, 1, f_empty
},
7538 {"escape", 2, 2, f_escape
},
7539 {"eval", 1, 1, f_eval
},
7540 {"eventhandler", 0, 0, f_eventhandler
},
7541 {"executable", 1, 1, f_executable
},
7542 {"exists", 1, 1, f_exists
},
7543 {"expand", 1, 2, f_expand
},
7544 {"extend", 2, 3, f_extend
},
7545 {"feedkeys", 1, 2, f_feedkeys
},
7546 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7547 {"filereadable", 1, 1, f_filereadable
},
7548 {"filewritable", 1, 1, f_filewritable
},
7549 {"filter", 2, 2, f_filter
},
7550 {"finddir", 1, 3, f_finddir
},
7551 {"findfile", 1, 3, f_findfile
},
7553 {"float2nr", 1, 1, f_float2nr
},
7554 {"floor", 1, 1, f_floor
},
7556 {"fnameescape", 1, 1, f_fnameescape
},
7557 {"fnamemodify", 2, 2, f_fnamemodify
},
7558 {"foldclosed", 1, 1, f_foldclosed
},
7559 {"foldclosedend", 1, 1, f_foldclosedend
},
7560 {"foldlevel", 1, 1, f_foldlevel
},
7561 {"foldtext", 0, 0, f_foldtext
},
7562 {"foldtextresult", 1, 1, f_foldtextresult
},
7563 {"foreground", 0, 0, f_foreground
},
7564 {"function", 1, 1, f_function
},
7565 {"garbagecollect", 0, 1, f_garbagecollect
},
7566 {"get", 2, 3, f_get
},
7567 {"getbufline", 2, 3, f_getbufline
},
7568 {"getbufvar", 2, 2, f_getbufvar
},
7569 {"getchar", 0, 1, f_getchar
},
7570 {"getcharmod", 0, 0, f_getcharmod
},
7571 {"getcmdline", 0, 0, f_getcmdline
},
7572 {"getcmdpos", 0, 0, f_getcmdpos
},
7573 {"getcmdtype", 0, 0, f_getcmdtype
},
7574 {"getcwd", 0, 0, f_getcwd
},
7575 {"getfontname", 0, 1, f_getfontname
},
7576 {"getfperm", 1, 1, f_getfperm
},
7577 {"getfsize", 1, 1, f_getfsize
},
7578 {"getftime", 1, 1, f_getftime
},
7579 {"getftype", 1, 1, f_getftype
},
7580 {"getline", 1, 2, f_getline
},
7581 {"getloclist", 1, 1, f_getqflist
},
7582 {"getmatches", 0, 0, f_getmatches
},
7583 {"getpid", 0, 0, f_getpid
},
7584 {"getpos", 1, 1, f_getpos
},
7585 {"getqflist", 0, 0, f_getqflist
},
7586 {"getreg", 0, 2, f_getreg
},
7587 {"getregtype", 0, 1, f_getregtype
},
7588 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7589 {"getwinposx", 0, 0, f_getwinposx
},
7590 {"getwinposy", 0, 0, f_getwinposy
},
7591 {"getwinvar", 2, 2, f_getwinvar
},
7592 {"glob", 1, 2, f_glob
},
7593 {"globpath", 2, 3, f_globpath
},
7594 {"has", 1, 1, f_has
},
7595 {"has_key", 2, 2, f_has_key
},
7596 {"haslocaldir", 0, 0, f_haslocaldir
},
7597 {"hasmapto", 1, 3, f_hasmapto
},
7598 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7599 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7600 {"histadd", 2, 2, f_histadd
},
7601 {"histdel", 1, 2, f_histdel
},
7602 {"histget", 1, 2, f_histget
},
7603 {"histnr", 1, 1, f_histnr
},
7604 {"hlID", 1, 1, f_hlID
},
7605 {"hlexists", 1, 1, f_hlexists
},
7606 {"hostname", 0, 0, f_hostname
},
7607 {"iconv", 3, 3, f_iconv
},
7608 {"indent", 1, 1, f_indent
},
7609 {"index", 2, 4, f_index
},
7610 {"input", 1, 3, f_input
},
7611 {"inputdialog", 1, 3, f_inputdialog
},
7612 {"inputlist", 1, 1, f_inputlist
},
7613 {"inputrestore", 0, 0, f_inputrestore
},
7614 {"inputsave", 0, 0, f_inputsave
},
7615 {"inputsecret", 1, 2, f_inputsecret
},
7616 {"insert", 2, 3, f_insert
},
7617 {"isdirectory", 1, 1, f_isdirectory
},
7618 {"islocked", 1, 1, f_islocked
},
7619 {"items", 1, 1, f_items
},
7620 {"join", 1, 2, f_join
},
7621 {"keys", 1, 1, f_keys
},
7622 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7623 {"len", 1, 1, f_len
},
7624 {"libcall", 3, 3, f_libcall
},
7625 {"libcallnr", 3, 3, f_libcallnr
},
7626 {"line", 1, 1, f_line
},
7627 {"line2byte", 1, 1, f_line2byte
},
7628 {"lispindent", 1, 1, f_lispindent
},
7629 {"localtime", 0, 0, f_localtime
},
7631 {"log10", 1, 1, f_log10
},
7633 {"map", 2, 2, f_map
},
7634 {"maparg", 1, 3, f_maparg
},
7635 {"mapcheck", 1, 3, f_mapcheck
},
7636 {"match", 2, 4, f_match
},
7637 {"matchadd", 2, 4, f_matchadd
},
7638 {"matcharg", 1, 1, f_matcharg
},
7639 {"matchdelete", 1, 1, f_matchdelete
},
7640 {"matchend", 2, 4, f_matchend
},
7641 {"matchlist", 2, 4, f_matchlist
},
7642 {"matchstr", 2, 4, f_matchstr
},
7643 {"max", 1, 1, f_max
},
7644 {"min", 1, 1, f_min
},
7646 {"mkdir", 1, 3, f_mkdir
},
7648 {"mode", 0, 1, f_mode
},
7649 {"nextnonblank", 1, 1, f_nextnonblank
},
7650 {"nr2char", 1, 1, f_nr2char
},
7651 {"pathshorten", 1, 1, f_pathshorten
},
7653 {"pow", 2, 2, f_pow
},
7655 {"prevnonblank", 1, 1, f_prevnonblank
},
7656 {"printf", 2, 19, f_printf
},
7657 {"pumvisible", 0, 0, f_pumvisible
},
7658 {"range", 1, 3, f_range
},
7659 {"readfile", 1, 3, f_readfile
},
7660 {"reltime", 0, 2, f_reltime
},
7661 {"reltimestr", 1, 1, f_reltimestr
},
7662 {"remote_expr", 2, 3, f_remote_expr
},
7663 {"remote_foreground", 1, 1, f_remote_foreground
},
7664 {"remote_peek", 1, 2, f_remote_peek
},
7665 {"remote_read", 1, 1, f_remote_read
},
7666 {"remote_send", 2, 3, f_remote_send
},
7667 {"remove", 2, 3, f_remove
},
7668 {"rename", 2, 2, f_rename
},
7669 {"repeat", 2, 2, f_repeat
},
7670 {"resolve", 1, 1, f_resolve
},
7671 {"reverse", 1, 1, f_reverse
},
7673 {"round", 1, 1, f_round
},
7675 {"search", 1, 4, f_search
},
7676 {"searchdecl", 1, 3, f_searchdecl
},
7677 {"searchpair", 3, 7, f_searchpair
},
7678 {"searchpairpos", 3, 7, f_searchpairpos
},
7679 {"searchpos", 1, 4, f_searchpos
},
7680 {"server2client", 2, 2, f_server2client
},
7681 {"serverlist", 0, 0, f_serverlist
},
7682 {"setbufvar", 3, 3, f_setbufvar
},
7683 {"setcmdpos", 1, 1, f_setcmdpos
},
7684 {"setline", 2, 2, f_setline
},
7685 {"setloclist", 2, 3, f_setloclist
},
7686 {"setmatches", 1, 1, f_setmatches
},
7687 {"setpos", 2, 2, f_setpos
},
7688 {"setqflist", 1, 2, f_setqflist
},
7689 {"setreg", 2, 3, f_setreg
},
7690 {"settabwinvar", 4, 4, f_settabwinvar
},
7691 {"setwinvar", 3, 3, f_setwinvar
},
7692 {"shellescape", 1, 2, f_shellescape
},
7693 {"simplify", 1, 1, f_simplify
},
7695 {"sin", 1, 1, f_sin
},
7697 {"sort", 1, 2, f_sort
},
7698 {"soundfold", 1, 1, f_soundfold
},
7699 {"spellbadword", 0, 1, f_spellbadword
},
7700 {"spellsuggest", 1, 3, f_spellsuggest
},
7701 {"split", 1, 3, f_split
},
7703 {"sqrt", 1, 1, f_sqrt
},
7704 {"str2float", 1, 1, f_str2float
},
7706 {"str2nr", 1, 2, f_str2nr
},
7707 #ifdef HAVE_STRFTIME
7708 {"strftime", 1, 2, f_strftime
},
7710 {"stridx", 2, 3, f_stridx
},
7711 {"string", 1, 1, f_string
},
7712 {"strlen", 1, 1, f_strlen
},
7713 {"strpart", 2, 3, f_strpart
},
7714 {"strridx", 2, 3, f_strridx
},
7715 {"strtrans", 1, 1, f_strtrans
},
7716 {"submatch", 1, 1, f_submatch
},
7717 {"substitute", 4, 4, f_substitute
},
7718 {"synID", 3, 3, f_synID
},
7719 {"synIDattr", 2, 3, f_synIDattr
},
7720 {"synIDtrans", 1, 1, f_synIDtrans
},
7721 {"synstack", 2, 2, f_synstack
},
7722 {"system", 1, 2, f_system
},
7723 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7724 {"tabpagenr", 0, 1, f_tabpagenr
},
7725 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7726 {"tagfiles", 0, 0, f_tagfiles
},
7727 {"taglist", 1, 1, f_taglist
},
7728 {"tempname", 0, 0, f_tempname
},
7729 {"test", 1, 1, f_test
},
7730 {"tolower", 1, 1, f_tolower
},
7731 {"toupper", 1, 1, f_toupper
},
7734 {"trunc", 1, 1, f_trunc
},
7736 {"type", 1, 1, f_type
},
7737 {"values", 1, 1, f_values
},
7738 {"virtcol", 1, 1, f_virtcol
},
7739 {"visualmode", 0, 1, f_visualmode
},
7740 {"winbufnr", 1, 1, f_winbufnr
},
7741 {"wincol", 0, 0, f_wincol
},
7742 {"winheight", 1, 1, f_winheight
},
7743 {"winline", 0, 0, f_winline
},
7744 {"winnr", 0, 1, f_winnr
},
7745 {"winrestcmd", 0, 0, f_winrestcmd
},
7746 {"winrestview", 1, 1, f_winrestview
},
7747 {"winsaveview", 0, 0, f_winsaveview
},
7748 {"winwidth", 1, 1, f_winwidth
},
7749 {"writefile", 2, 3, f_writefile
},
7752 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7755 * Function given to ExpandGeneric() to obtain the list of internal
7756 * or user defined function names.
7759 get_function_name(xp
, idx
)
7763 static int intidx
= -1;
7770 name
= get_user_func_name(xp
, idx
);
7774 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7776 STRCPY(IObuff
, functions
[intidx
].f_name
);
7777 STRCAT(IObuff
, "(");
7778 if (functions
[intidx
].f_max_argc
== 0)
7779 STRCAT(IObuff
, ")");
7787 * Function given to ExpandGeneric() to obtain the list of internal or
7788 * user defined variable or function names.
7792 get_expr_name(xp
, idx
)
7796 static int intidx
= -1;
7803 name
= get_function_name(xp
, idx
);
7807 return get_user_var_name(xp
, ++intidx
);
7810 #endif /* FEAT_CMDL_COMPL */
7813 * Find internal function in table above.
7814 * Return index, or -1 if not found
7817 find_internal_func(name
)
7818 char_u
*name
; /* name of the function */
7821 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7826 * Find the function name in the table. Binary search.
7828 while (first
<= last
)
7830 x
= first
+ ((unsigned)(last
- first
) >> 1);
7831 cmp
= STRCMP(name
, functions
[x
].f_name
);
7843 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7844 * name it contains, otherwise return "name".
7847 deref_func_name(name
, lenp
)
7856 v
= find_var(name
, NULL
);
7858 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7860 if (v
->di_tv
.vval
.v_string
== NULL
)
7863 return (char_u
*)""; /* just in case */
7865 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7866 return v
->di_tv
.vval
.v_string
;
7873 * Allocate a variable for the result of a function.
7874 * Return OK or FAIL.
7877 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7879 char_u
*name
; /* name of the function */
7880 int len
; /* length of "name" */
7882 char_u
**arg
; /* argument, pointing to the '(' */
7883 linenr_T firstline
; /* first line of range */
7884 linenr_T lastline
; /* last line of range */
7885 int *doesrange
; /* return: function handled range */
7887 dict_T
*selfdict
; /* Dictionary for "self" */
7891 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7892 int argcount
= 0; /* number of arguments found */
7895 * Get the arguments.
7898 while (argcount
< MAX_FUNC_ARGS
)
7900 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7901 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7903 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7918 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7919 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7920 else if (!aborting())
7922 if (argcount
== MAX_FUNC_ARGS
)
7923 emsg_funcname(N_("E740: Too many arguments for function %s"), name
);
7925 emsg_funcname(N_("E116: Invalid arguments for function %s"), name
);
7928 while (--argcount
>= 0)
7929 clear_tv(&argvars
[argcount
]);
7931 *arg
= skipwhite(argp
);
7937 * Call a function with its resolved parameters
7938 * Return OK when the function can't be called, FAIL otherwise.
7939 * Also returns OK when an error was encountered while executing the function.
7942 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7943 doesrange
, evaluate
, selfdict
)
7944 char_u
*name
; /* name of the function */
7945 int len
; /* length of "name" */
7946 typval_T
*rettv
; /* return value goes here */
7947 int argcount
; /* number of "argvars" */
7948 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7949 PLUS ONE elements! */
7950 linenr_T firstline
; /* first line of range */
7951 linenr_T lastline
; /* last line of range */
7952 int *doesrange
; /* return: function handled range */
7954 dict_T
*selfdict
; /* Dictionary for "self" */
7957 #define ERROR_UNKNOWN 0
7958 #define ERROR_TOOMANY 1
7959 #define ERROR_TOOFEW 2
7960 #define ERROR_SCRIPT 3
7961 #define ERROR_DICT 4
7962 #define ERROR_NONE 5
7963 #define ERROR_OTHER 6
7964 int error
= ERROR_NONE
;
7969 #define FLEN_FIXED 40
7970 char_u fname_buf
[FLEN_FIXED
+ 1];
7974 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7975 * Change <SNR>123_name() to K_SNR 123_name().
7976 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7980 llen
= eval_fname_script(name
);
7983 fname_buf
[0] = K_SPECIAL
;
7984 fname_buf
[1] = KS_EXTRA
;
7985 fname_buf
[2] = (int)KE_SNR
;
7987 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
7989 if (current_SID
<= 0)
7990 error
= ERROR_SCRIPT
;
7993 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
7994 i
= (int)STRLEN(fname_buf
);
7997 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
7999 STRCPY(fname_buf
+ i
, name
+ llen
);
8004 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
8006 error
= ERROR_OTHER
;
8009 mch_memmove(fname
, fname_buf
, (size_t)i
);
8010 STRCPY(fname
+ i
, name
+ llen
);
8020 /* execute the function if no errors detected and executing */
8021 if (evaluate
&& error
== ERROR_NONE
)
8023 rettv
->v_type
= VAR_NUMBER
; /* default rettv is number zero */
8024 rettv
->vval
.v_number
= 0;
8025 error
= ERROR_UNKNOWN
;
8027 if (!builtin_function(fname
))
8030 * User defined function.
8032 fp
= find_func(fname
);
8035 /* Trigger FuncUndefined event, may load the function. */
8037 && apply_autocmds(EVENT_FUNCUNDEFINED
,
8038 fname
, fname
, TRUE
, NULL
)
8041 /* executed an autocommand, search for the function again */
8042 fp
= find_func(fname
);
8045 /* Try loading a package. */
8046 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
8048 /* loaded a package, search for the function again */
8049 fp
= find_func(fname
);
8054 if (fp
->uf_flags
& FC_RANGE
)
8056 if (argcount
< fp
->uf_args
.ga_len
)
8057 error
= ERROR_TOOFEW
;
8058 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
8059 error
= ERROR_TOOMANY
;
8060 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
8065 * Call the user function.
8066 * Save and restore search patterns, script variables and
8069 save_search_patterns();
8072 call_user_func(fp
, argcount
, argvars
, rettv
,
8073 firstline
, lastline
,
8074 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
8075 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
8076 && fp
->uf_refcount
<= 0)
8077 /* Function was unreferenced while being used, free it
8081 restore_search_patterns();
8089 * Find the function name in the table, call its implementation.
8091 i
= find_internal_func(fname
);
8094 if (argcount
< functions
[i
].f_min_argc
)
8095 error
= ERROR_TOOFEW
;
8096 else if (argcount
> functions
[i
].f_max_argc
)
8097 error
= ERROR_TOOMANY
;
8100 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
8101 functions
[i
].f_func(argvars
, rettv
);
8107 * The function call (or "FuncUndefined" autocommand sequence) might
8108 * have been aborted by an error, an interrupt, or an explicitly thrown
8109 * exception that has not been caught so far. This situation can be
8110 * tested for by calling aborting(). For an error in an internal
8111 * function or for the "E132" error in call_user_func(), however, the
8112 * throw point at which the "force_abort" flag (temporarily reset by
8113 * emsg()) is normally updated has not been reached yet. We need to
8114 * update that flag first to make aborting() reliable.
8116 update_force_abort();
8118 if (error
== ERROR_NONE
)
8122 * Report an error unless the argument evaluation or function call has been
8123 * cancelled due to an aborting error, an interrupt, or an exception.
8130 emsg_funcname(N_("E117: Unknown function: %s"), name
);
8133 emsg_funcname(e_toomanyarg
, name
);
8136 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
8140 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
8144 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
8151 if (fname
!= name
&& fname
!= fname_buf
)
8158 * Give an error message with a function name. Handle <SNR> things.
8159 * "ermsg" is to be passed without translation, use N_() instead of _().
8162 emsg_funcname(ermsg
, name
)
8168 if (*name
== K_SPECIAL
)
8169 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
8178 * Return TRUE for a non-zero Number and a non-empty String.
8181 non_zero_arg(argvars
)
8184 return ((argvars
[0].v_type
== VAR_NUMBER
8185 && argvars
[0].vval
.v_number
!= 0)
8186 || (argvars
[0].v_type
== VAR_STRING
8187 && argvars
[0].vval
.v_string
!= NULL
8188 && *argvars
[0].vval
.v_string
!= NUL
));
8191 /*********************************************
8192 * Implementation of the built-in functions
8197 * "abs(expr)" function
8200 f_abs(argvars
, rettv
)
8204 if (argvars
[0].v_type
== VAR_FLOAT
)
8206 rettv
->v_type
= VAR_FLOAT
;
8207 rettv
->vval
.v_float
= fabs(argvars
[0].vval
.v_float
);
8214 n
= get_tv_number_chk(&argvars
[0], &error
);
8216 rettv
->vval
.v_number
= -1;
8218 rettv
->vval
.v_number
= n
;
8220 rettv
->vval
.v_number
= -n
;
8226 * "add(list, item)" function
8229 f_add(argvars
, rettv
)
8235 rettv
->vval
.v_number
= 1; /* Default: Failed */
8236 if (argvars
[0].v_type
== VAR_LIST
)
8238 if ((l
= argvars
[0].vval
.v_list
) != NULL
8239 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
8240 && list_append_tv(l
, &argvars
[1]) == OK
)
8241 copy_tv(&argvars
[0], rettv
);
8248 * "append(lnum, string/list)" function
8251 f_append(argvars
, rettv
)
8258 listitem_T
*li
= NULL
;
8262 lnum
= get_tv_lnum(argvars
);
8264 && lnum
<= curbuf
->b_ml
.ml_line_count
8265 && u_save(lnum
, lnum
+ 1) == OK
)
8267 if (argvars
[1].v_type
== VAR_LIST
)
8269 l
= argvars
[1].vval
.v_list
;
8277 tv
= &argvars
[1]; /* append a string */
8278 else if (li
== NULL
)
8279 break; /* end of list */
8281 tv
= &li
->li_tv
; /* append item from list */
8282 line
= get_tv_string_chk(tv
);
8283 if (line
== NULL
) /* type error */
8285 rettv
->vval
.v_number
= 1; /* Failed */
8288 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
8295 appended_lines_mark(lnum
, added
);
8296 if (curwin
->w_cursor
.lnum
> lnum
)
8297 curwin
->w_cursor
.lnum
+= added
;
8300 rettv
->vval
.v_number
= 1; /* Failed */
8308 f_argc(argvars
, rettv
)
8312 rettv
->vval
.v_number
= ARGCOUNT
;
8316 * "argidx()" function
8320 f_argidx(argvars
, rettv
)
8324 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
8328 * "argv(nr)" function
8331 f_argv(argvars
, rettv
)
8337 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
8339 idx
= get_tv_number_chk(&argvars
[0], NULL
);
8340 if (idx
>= 0 && idx
< ARGCOUNT
)
8341 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
8343 rettv
->vval
.v_string
= NULL
;
8344 rettv
->v_type
= VAR_STRING
;
8346 else if (rettv_list_alloc(rettv
) == OK
)
8347 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
8348 list_append_string(rettv
->vval
.v_list
,
8349 alist_name(&ARGLIST
[idx
]), -1);
8353 static int get_float_arg
__ARGS((typval_T
*argvars
, float_T
*f
));
8356 * Get the float value of "argvars[0]" into "f".
8357 * Returns FAIL when the argument is not a Number or Float.
8360 get_float_arg(argvars
, f
)
8364 if (argvars
[0].v_type
== VAR_FLOAT
)
8366 *f
= argvars
[0].vval
.v_float
;
8369 if (argvars
[0].v_type
== VAR_NUMBER
)
8371 *f
= (float_T
)argvars
[0].vval
.v_number
;
8374 EMSG(_("E808: Number or Float required"));
8382 f_atan(argvars
, rettv
)
8388 rettv
->v_type
= VAR_FLOAT
;
8389 if (get_float_arg(argvars
, &f
) == OK
)
8390 rettv
->vval
.v_float
= atan(f
);
8392 rettv
->vval
.v_float
= 0.0;
8397 * "browse(save, title, initdir, default)" function
8401 f_browse(argvars
, rettv
)
8410 char_u buf
[NUMBUFLEN
];
8411 char_u buf2
[NUMBUFLEN
];
8414 save
= get_tv_number_chk(&argvars
[0], &error
);
8415 title
= get_tv_string_chk(&argvars
[1]);
8416 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
8417 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8419 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
8420 rettv
->vval
.v_string
= NULL
;
8422 rettv
->vval
.v_string
=
8423 do_browse(save
? BROWSE_SAVE
: 0,
8424 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
8426 rettv
->vval
.v_string
= NULL
;
8428 rettv
->v_type
= VAR_STRING
;
8432 * "browsedir(title, initdir)" function
8436 f_browsedir(argvars
, rettv
)
8443 char_u buf
[NUMBUFLEN
];
8445 title
= get_tv_string_chk(&argvars
[0]);
8446 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
8448 if (title
== NULL
|| initdir
== NULL
)
8449 rettv
->vval
.v_string
= NULL
;
8451 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
8452 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
8454 rettv
->vval
.v_string
= NULL
;
8456 rettv
->v_type
= VAR_STRING
;
8459 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
8462 * Find a buffer by number or exact name.
8470 if (avar
->v_type
== VAR_NUMBER
)
8471 buf
= buflist_findnr((int)avar
->vval
.v_number
);
8472 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
8474 buf
= buflist_findname_exp(avar
->vval
.v_string
);
8477 /* No full path name match, try a match with a URL or a "nofile"
8478 * buffer, these don't use the full path. */
8479 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
8480 if (buf
->b_fname
!= NULL
8481 && (path_with_url(buf
->b_fname
)
8482 #ifdef FEAT_QUICKFIX
8486 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
8494 * "bufexists(expr)" function
8497 f_bufexists(argvars
, rettv
)
8501 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
8505 * "buflisted(expr)" function
8508 f_buflisted(argvars
, rettv
)
8514 buf
= find_buffer(&argvars
[0]);
8515 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
8519 * "bufloaded(expr)" function
8522 f_bufloaded(argvars
, rettv
)
8528 buf
= find_buffer(&argvars
[0]);
8529 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
8532 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
8535 * Get buffer by number or pattern.
8541 char_u
*name
= tv
->vval
.v_string
;
8546 if (tv
->v_type
== VAR_NUMBER
)
8547 return buflist_findnr((int)tv
->vval
.v_number
);
8548 if (tv
->v_type
!= VAR_STRING
)
8550 if (name
== NULL
|| *name
== NUL
)
8552 if (name
[0] == '$' && name
[1] == NUL
)
8555 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8556 save_magic
= p_magic
;
8559 p_cpo
= (char_u
*)"";
8561 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8564 p_magic
= save_magic
;
8567 /* If not found, try expanding the name, like done for bufexists(). */
8569 buf
= find_buffer(tv
);
8575 * "bufname(expr)" function
8578 f_bufname(argvars
, rettv
)
8584 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8586 buf
= get_buf_tv(&argvars
[0]);
8587 rettv
->v_type
= VAR_STRING
;
8588 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8589 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8591 rettv
->vval
.v_string
= NULL
;
8596 * "bufnr(expr)" function
8599 f_bufnr(argvars
, rettv
)
8607 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8609 buf
= get_buf_tv(&argvars
[0]);
8612 /* If the buffer isn't found and the second argument is not zero create a
8615 && argvars
[1].v_type
!= VAR_UNKNOWN
8616 && get_tv_number_chk(&argvars
[1], &error
) != 0
8618 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8620 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8623 rettv
->vval
.v_number
= buf
->b_fnum
;
8625 rettv
->vval
.v_number
= -1;
8629 * "bufwinnr(nr)" function
8632 f_bufwinnr(argvars
, rettv
)
8642 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8644 buf
= get_buf_tv(&argvars
[0]);
8646 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8649 if (wp
->w_buffer
== buf
)
8652 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8654 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8660 * "byte2line(byte)" function
8664 f_byte2line(argvars
, rettv
)
8668 #ifndef FEAT_BYTEOFF
8669 rettv
->vval
.v_number
= -1;
8673 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8675 rettv
->vval
.v_number
= -1;
8677 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8678 (linenr_T
)0, &boff
);
8683 * "byteidx()" function
8687 f_byteidx(argvars
, rettv
)
8697 str
= get_tv_string_chk(&argvars
[0]);
8698 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8699 rettv
->vval
.v_number
= -1;
8700 if (str
== NULL
|| idx
< 0)
8705 for ( ; idx
> 0; idx
--)
8707 if (*t
== NUL
) /* EOL reached */
8709 t
+= (*mb_ptr2len
)(t
);
8711 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8713 if ((size_t)idx
<= STRLEN(str
))
8714 rettv
->vval
.v_number
= idx
;
8719 * "call(func, arglist)" function
8722 f_call(argvars
, rettv
)
8727 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8731 dict_T
*selfdict
= NULL
;
8733 if (argvars
[1].v_type
!= VAR_LIST
)
8738 if (argvars
[1].vval
.v_list
== NULL
)
8741 if (argvars
[0].v_type
== VAR_FUNC
)
8742 func
= argvars
[0].vval
.v_string
;
8744 func
= get_tv_string(&argvars
[0]);
8746 return; /* type error or empty name */
8748 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8750 if (argvars
[2].v_type
!= VAR_DICT
)
8755 selfdict
= argvars
[2].vval
.v_dict
;
8758 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8759 item
= item
->li_next
)
8761 if (argc
== MAX_FUNC_ARGS
)
8763 EMSG(_("E699: Too many arguments"));
8766 /* Make a copy of each argument. This is needed to be able to set
8767 * v_lock to VAR_FIXED in the copy without changing the original list.
8769 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8773 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8774 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8775 &dummy
, TRUE
, selfdict
);
8777 /* Free the arguments. */
8779 clear_tv(&argv
[--argc
]);
8784 * "ceil({float})" function
8787 f_ceil(argvars
, rettv
)
8793 rettv
->v_type
= VAR_FLOAT
;
8794 if (get_float_arg(argvars
, &f
) == OK
)
8795 rettv
->vval
.v_float
= ceil(f
);
8797 rettv
->vval
.v_float
= 0.0;
8802 * "changenr()" function
8806 f_changenr(argvars
, rettv
)
8810 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8814 * "char2nr(string)" function
8817 f_char2nr(argvars
, rettv
)
8823 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8826 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8830 * "cindent(lnum)" function
8833 f_cindent(argvars
, rettv
)
8841 pos
= curwin
->w_cursor
;
8842 lnum
= get_tv_lnum(argvars
);
8843 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8845 curwin
->w_cursor
.lnum
= lnum
;
8846 rettv
->vval
.v_number
= get_c_indent();
8847 curwin
->w_cursor
= pos
;
8851 rettv
->vval
.v_number
= -1;
8855 * "clearmatches()" function
8859 f_clearmatches(argvars
, rettv
)
8863 #ifdef FEAT_SEARCH_EXTRA
8864 clear_matches(curwin
);
8869 * "col(string)" function
8872 f_col(argvars
, rettv
)
8878 int fnum
= curbuf
->b_fnum
;
8880 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8881 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8883 if (fp
->col
== MAXCOL
)
8885 /* '> can be MAXCOL, get the length of the line then */
8886 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8887 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8894 #ifdef FEAT_VIRTUALEDIT
8895 /* col(".") when the cursor is on the NUL at the end of the line
8896 * because of "coladd" can be seen as an extra column. */
8897 if (virtual_active() && fp
== &curwin
->w_cursor
)
8899 char_u
*p
= ml_get_cursor();
8901 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8902 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8907 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8910 if (*p
!= NUL
&& p
[1] == NUL
)
8918 rettv
->vval
.v_number
= col
;
8921 #if defined(FEAT_INS_EXPAND)
8923 * "complete()" function
8927 f_complete(argvars
, rettv
)
8933 if ((State
& INSERT
) == 0)
8935 EMSG(_("E785: complete() can only be used in Insert mode"));
8939 /* Check for undo allowed here, because if something was already inserted
8940 * the line was already saved for undo and this check isn't done. */
8941 if (!undo_allowed())
8944 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8950 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
8954 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
8958 * "complete_add()" function
8962 f_complete_add(argvars
, rettv
)
8966 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
8970 * "complete_check()" function
8974 f_complete_check(argvars
, rettv
)
8978 int saved
= RedrawingDisabled
;
8980 RedrawingDisabled
= 0;
8981 ins_compl_check_keys(0);
8982 rettv
->vval
.v_number
= compl_interrupted
;
8983 RedrawingDisabled
= saved
;
8988 * "confirm(message, buttons[, default [, type]])" function
8992 f_confirm(argvars
, rettv
)
8996 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8998 char_u
*buttons
= NULL
;
8999 char_u buf
[NUMBUFLEN
];
9000 char_u buf2
[NUMBUFLEN
];
9002 int type
= VIM_GENERIC
;
9006 message
= get_tv_string_chk(&argvars
[0]);
9007 if (message
== NULL
)
9009 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9011 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
9012 if (buttons
== NULL
)
9014 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9016 def
= get_tv_number_chk(&argvars
[2], &error
);
9017 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9019 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
9020 if (typestr
== NULL
)
9024 switch (TOUPPER_ASC(*typestr
))
9026 case 'E': type
= VIM_ERROR
; break;
9027 case 'Q': type
= VIM_QUESTION
; break;
9028 case 'I': type
= VIM_INFO
; break;
9029 case 'W': type
= VIM_WARNING
; break;
9030 case 'G': type
= VIM_GENERIC
; break;
9037 if (buttons
== NULL
|| *buttons
== NUL
)
9038 buttons
= (char_u
*)_("&Ok");
9041 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
9050 f_copy(argvars
, rettv
)
9054 item_copy(&argvars
[0], rettv
, FALSE
, 0);
9062 f_cos(argvars
, rettv
)
9068 rettv
->v_type
= VAR_FLOAT
;
9069 if (get_float_arg(argvars
, &f
) == OK
)
9070 rettv
->vval
.v_float
= cos(f
);
9072 rettv
->vval
.v_float
= 0.0;
9077 * "count()" function
9080 f_count(argvars
, rettv
)
9087 if (argvars
[0].v_type
== VAR_LIST
)
9093 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9096 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9100 ic
= get_tv_number_chk(&argvars
[2], &error
);
9101 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9103 idx
= get_tv_number_chk(&argvars
[3], &error
);
9106 li
= list_find(l
, idx
);
9108 EMSGN(_(e_listidx
), idx
);
9115 for ( ; li
!= NULL
; li
= li
->li_next
)
9116 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
9120 else if (argvars
[0].v_type
== VAR_DICT
)
9126 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9130 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9132 ic
= get_tv_number_chk(&argvars
[2], &error
);
9133 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9137 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
9138 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
9140 if (!HASHITEM_EMPTY(hi
))
9143 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
9150 EMSG2(_(e_listdictarg
), "count()");
9151 rettv
->vval
.v_number
= n
;
9155 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9157 * Checks the existence of a cscope connection.
9161 f_cscope_connection(argvars
, rettv
)
9167 char_u
*dbpath
= NULL
;
9168 char_u
*prepend
= NULL
;
9169 char_u buf
[NUMBUFLEN
];
9171 if (argvars
[0].v_type
!= VAR_UNKNOWN
9172 && argvars
[1].v_type
!= VAR_UNKNOWN
)
9174 num
= (int)get_tv_number(&argvars
[0]);
9175 dbpath
= get_tv_string(&argvars
[1]);
9176 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9177 prepend
= get_tv_string_buf(&argvars
[2], buf
);
9180 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
9185 * "cursor(lnum, col)" function
9187 * Moves the cursor to the specified line and column.
9188 * Returns 0 when the position could be set, -1 otherwise.
9192 f_cursor(argvars
, rettv
)
9197 #ifdef FEAT_VIRTUALEDIT
9201 rettv
->vval
.v_number
= -1;
9202 if (argvars
[1].v_type
== VAR_UNKNOWN
)
9206 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
9210 #ifdef FEAT_VIRTUALEDIT
9211 coladd
= pos
.coladd
;
9216 line
= get_tv_lnum(argvars
);
9217 col
= get_tv_number_chk(&argvars
[1], NULL
);
9218 #ifdef FEAT_VIRTUALEDIT
9219 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9220 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
9223 if (line
< 0 || col
< 0
9224 #ifdef FEAT_VIRTUALEDIT
9228 return; /* type error; errmsg already given */
9230 curwin
->w_cursor
.lnum
= line
;
9232 curwin
->w_cursor
.col
= col
- 1;
9233 #ifdef FEAT_VIRTUALEDIT
9234 curwin
->w_cursor
.coladd
= coladd
;
9237 /* Make sure the cursor is in a valid position. */
9240 /* Correct cursor for multi-byte character. */
9245 curwin
->w_set_curswant
= TRUE
;
9246 rettv
->vval
.v_number
= 0;
9250 * "deepcopy()" function
9253 f_deepcopy(argvars
, rettv
)
9259 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9260 noref
= get_tv_number_chk(&argvars
[1], NULL
);
9261 if (noref
< 0 || noref
> 1)
9264 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
9268 * "delete()" function
9271 f_delete(argvars
, rettv
)
9275 if (check_restricted() || check_secure())
9276 rettv
->vval
.v_number
= -1;
9278 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
9282 * "did_filetype()" function
9286 f_did_filetype(argvars
, rettv
)
9291 rettv
->vval
.v_number
= did_filetype
;
9296 * "diff_filler()" function
9300 f_diff_filler(argvars
, rettv
)
9305 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
9310 * "diff_hlID()" function
9314 f_diff_hlID(argvars
, rettv
)
9319 linenr_T lnum
= get_tv_lnum(argvars
);
9320 static linenr_T prev_lnum
= 0;
9321 static int changedtick
= 0;
9322 static int fnum
= 0;
9323 static int change_start
= 0;
9324 static int change_end
= 0;
9325 static hlf_T hlID
= (hlf_T
)0;
9329 if (lnum
< 0) /* ignore type error in {lnum} arg */
9331 if (lnum
!= prev_lnum
9332 || changedtick
!= curbuf
->b_changedtick
9333 || fnum
!= curbuf
->b_fnum
)
9335 /* New line, buffer, change: need to get the values. */
9336 filler_lines
= diff_check(curwin
, lnum
);
9337 if (filler_lines
< 0)
9339 if (filler_lines
== -1)
9341 change_start
= MAXCOL
;
9343 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
9344 hlID
= HLF_ADD
; /* added line */
9346 hlID
= HLF_CHD
; /* changed line */
9349 hlID
= HLF_ADD
; /* added line */
9354 changedtick
= curbuf
->b_changedtick
;
9355 fnum
= curbuf
->b_fnum
;
9358 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
9360 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
9361 if (col
>= change_start
&& col
<= change_end
)
9362 hlID
= HLF_TXD
; /* changed text */
9364 hlID
= HLF_CHD
; /* changed line */
9366 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
9371 * "empty({expr})" function
9374 f_empty(argvars
, rettv
)
9380 switch (argvars
[0].v_type
)
9384 n
= argvars
[0].vval
.v_string
== NULL
9385 || *argvars
[0].vval
.v_string
== NUL
;
9388 n
= argvars
[0].vval
.v_number
== 0;
9392 n
= argvars
[0].vval
.v_float
== 0.0;
9396 n
= argvars
[0].vval
.v_list
== NULL
9397 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
9400 n
= argvars
[0].vval
.v_dict
== NULL
9401 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
9404 EMSG2(_(e_intern2
), "f_empty()");
9408 rettv
->vval
.v_number
= n
;
9412 * "escape({string}, {chars})" function
9415 f_escape(argvars
, rettv
)
9419 char_u buf
[NUMBUFLEN
];
9421 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
9422 get_tv_string_buf(&argvars
[1], buf
));
9423 rettv
->v_type
= VAR_STRING
;
9431 f_eval(argvars
, rettv
)
9437 s
= get_tv_string_chk(&argvars
[0]);
9441 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
9443 rettv
->v_type
= VAR_NUMBER
;
9444 rettv
->vval
.v_number
= 0;
9447 EMSG(_(e_trailing
));
9451 * "eventhandler()" function
9455 f_eventhandler(argvars
, rettv
)
9459 rettv
->vval
.v_number
= vgetc_busy
;
9463 * "executable()" function
9466 f_executable(argvars
, rettv
)
9470 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
9474 * "exists()" function
9477 f_exists(argvars
, rettv
)
9486 p
= get_tv_string(&argvars
[0]);
9487 if (*p
== '$') /* environment variable */
9489 /* first try "normal" environment variables (fast) */
9490 if (mch_getenv(p
+ 1) != NULL
)
9494 /* try expanding things like $VIM and ${HOME} */
9495 p
= expand_env_save(p
);
9496 if (p
!= NULL
&& *p
!= '$')
9501 else if (*p
== '&' || *p
== '+') /* option */
9503 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
9504 if (*skipwhite(p
) != NUL
)
9505 n
= FALSE
; /* trailing garbage */
9507 else if (*p
== '*') /* internal or user defined function */
9509 n
= function_exists(p
+ 1);
9513 n
= cmd_exists(p
+ 1);
9519 n
= autocmd_supported(p
+ 2);
9521 n
= au_exists(p
+ 1);
9524 else /* internal variable */
9529 /* get_name_len() takes care of expanding curly braces */
9531 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
9536 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
9539 /* handle d.key, l[idx], f(expr) */
9540 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
9551 rettv
->vval
.v_number
= n
;
9555 * "expand()" function
9558 f_expand(argvars
, rettv
)
9565 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
9569 rettv
->v_type
= VAR_STRING
;
9570 s
= get_tv_string(&argvars
[0]);
9571 if (*s
== '%' || *s
== '#' || *s
== '<')
9574 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
9579 /* When the optional second argument is non-zero, don't remove matches
9580 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
9581 if (argvars
[1].v_type
!= VAR_UNKNOWN
9582 && get_tv_number_chk(&argvars
[1], &error
))
9583 flags
|= WILD_KEEP_ALL
;
9587 xpc
.xp_context
= EXPAND_FILES
;
9588 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
9591 rettv
->vval
.v_string
= NULL
;
9596 * "extend(list, list [, idx])" function
9597 * "extend(dict, dict [, action])" function
9600 f_extend(argvars
, rettv
)
9604 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9611 l1
= argvars
[0].vval
.v_list
;
9612 l2
= argvars
[1].vval
.v_list
;
9613 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9616 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9618 before
= get_tv_number_chk(&argvars
[2], &error
);
9620 return; /* type error; errmsg already given */
9622 if (before
== l1
->lv_len
)
9626 item
= list_find(l1
, before
);
9629 EMSGN(_(e_listidx
), before
);
9636 list_extend(l1
, l2
, item
);
9638 copy_tv(&argvars
[0], rettv
);
9641 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9650 d1
= argvars
[0].vval
.v_dict
;
9651 d2
= argvars
[1].vval
.v_dict
;
9652 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9655 /* Check the third argument. */
9656 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9658 static char *(av
[]) = {"keep", "force", "error"};
9660 action
= get_tv_string_chk(&argvars
[2]);
9662 return; /* type error; errmsg already given */
9663 for (i
= 0; i
< 3; ++i
)
9664 if (STRCMP(action
, av
[i
]) == 0)
9668 EMSG2(_(e_invarg2
), action
);
9673 action
= (char_u
*)"force";
9675 /* Go over all entries in the second dict and add them to the
9677 todo
= (int)d2
->dv_hashtab
.ht_used
;
9678 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9680 if (!HASHITEM_EMPTY(hi2
))
9683 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9686 di1
= dictitem_copy(HI2DI(hi2
));
9687 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9690 else if (*action
== 'e')
9692 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9695 else if (*action
== 'f')
9697 clear_tv(&di1
->di_tv
);
9698 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9703 copy_tv(&argvars
[0], rettv
);
9707 EMSG2(_(e_listdictarg
), "extend()");
9711 * "feedkeys()" function
9715 f_feedkeys(argvars
, rettv
)
9720 char_u
*keys
, *flags
;
9721 char_u nbuf
[NUMBUFLEN
];
9725 /* This is not allowed in the sandbox. If the commands would still be
9726 * executed in the sandbox it would be OK, but it probably happens later,
9727 * when "sandbox" is no longer set. */
9731 keys
= get_tv_string(&argvars
[0]);
9734 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9736 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9737 for ( ; *flags
!= NUL
; ++flags
)
9741 case 'n': remap
= FALSE
; break;
9742 case 'm': remap
= TRUE
; break;
9743 case 't': typed
= TRUE
; break;
9748 /* Need to escape K_SPECIAL and CSI before putting the string in the
9749 * typeahead buffer. */
9750 keys_esc
= vim_strsave_escape_csi(keys
);
9751 if (keys_esc
!= NULL
)
9753 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9754 typebuf
.tb_len
, !typed
, FALSE
);
9757 typebuf_was_filled
= TRUE
;
9763 * "filereadable()" function
9766 f_filereadable(argvars
, rettv
)
9775 # define O_NONBLOCK 0
9777 p
= get_tv_string(&argvars
[0]);
9778 if (*p
&& !mch_isdir(p
) && (fd
= mch_open((char *)p
,
9779 O_RDONLY
| O_NONBLOCK
, 0)) >= 0)
9787 rettv
->vval
.v_number
= n
;
9791 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9792 * rights to write into.
9795 f_filewritable(argvars
, rettv
)
9799 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9802 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int find_what
));
9805 findfilendir(argvars
, rettv
, find_what
)
9810 #ifdef FEAT_SEARCHPATH
9812 char_u
*fresult
= NULL
;
9813 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9815 char_u pathbuf
[NUMBUFLEN
];
9821 rettv
->vval
.v_string
= NULL
;
9822 rettv
->v_type
= VAR_STRING
;
9824 #ifdef FEAT_SEARCHPATH
9825 fname
= get_tv_string(&argvars
[0]);
9827 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9829 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9837 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9838 count
= get_tv_number_chk(&argvars
[2], &error
);
9842 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9845 if (*fname
!= NUL
&& !error
)
9849 if (rettv
->v_type
== VAR_STRING
)
9851 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9852 first
? (int)STRLEN(fname
) : 0,
9856 find_what
== FINDFILE_DIR
9857 ? (char_u
*)"" : curbuf
->b_p_sua
);
9860 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9861 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9863 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9866 if (rettv
->v_type
== VAR_STRING
)
9867 rettv
->vval
.v_string
= fresult
;
9871 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9872 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9875 * Implementation of map() and filter().
9878 filter_map(argvars
, rettv
, map
)
9883 char_u buf
[NUMBUFLEN
];
9885 listitem_T
*li
, *nli
;
9895 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9898 if (argvars
[0].v_type
== VAR_LIST
)
9900 if ((l
= argvars
[0].vval
.v_list
) == NULL
9901 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9904 else if (argvars
[0].v_type
== VAR_DICT
)
9906 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9907 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9912 EMSG2(_(e_listdictarg
), ermsg
);
9916 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9917 /* On type errors, the preceding call has already displayed an error
9918 * message. Avoid a misleading error message for an empty string that
9919 * was not passed as argument. */
9922 prepare_vimvar(VV_VAL
, &save_val
);
9923 expr
= skipwhite(expr
);
9925 /* We reset "did_emsg" to be able to detect whether an error
9926 * occurred during evaluation of the expression. */
9927 save_did_emsg
= did_emsg
;
9930 if (argvars
[0].v_type
== VAR_DICT
)
9932 prepare_vimvar(VV_KEY
, &save_key
);
9933 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9935 ht
= &d
->dv_hashtab
;
9937 todo
= (int)ht
->ht_used
;
9938 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9940 if (!HASHITEM_EMPTY(hi
))
9944 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9946 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9947 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9951 dictitem_remove(d
, di
);
9952 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9957 restore_vimvar(VV_KEY
, &save_key
);
9961 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
9963 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
9966 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
9970 listitem_remove(l
, li
);
9974 restore_vimvar(VV_VAL
, &save_val
);
9976 did_emsg
|= save_did_emsg
;
9979 copy_tv(&argvars
[0], rettv
);
9983 filter_map_one(tv
, expr
, map
, remp
)
9993 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
9995 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
9997 if (*s
!= NUL
) /* check for trailing chars after expr */
9999 EMSG2(_(e_invexpr2
), s
);
10004 /* map(): replace the list item value */
10013 /* filter(): when expr is zero remove the item */
10014 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
10016 /* On type error, nothing has been removed; return FAIL to stop the
10017 * loop. The error message was given by get_tv_number_chk(). */
10023 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
10028 * "filter()" function
10031 f_filter(argvars
, rettv
)
10035 filter_map(argvars
, rettv
, FALSE
);
10039 * "finddir({fname}[, {path}[, {count}]])" function
10042 f_finddir(argvars
, rettv
)
10046 findfilendir(argvars
, rettv
, FINDFILE_DIR
);
10050 * "findfile({fname}[, {path}[, {count}]])" function
10053 f_findfile(argvars
, rettv
)
10057 findfilendir(argvars
, rettv
, FINDFILE_FILE
);
10062 * "float2nr({float})" function
10065 f_float2nr(argvars
, rettv
)
10071 if (get_float_arg(argvars
, &f
) == OK
)
10073 if (f
< -0x7fffffff)
10074 rettv
->vval
.v_number
= -0x7fffffff;
10075 else if (f
> 0x7fffffff)
10076 rettv
->vval
.v_number
= 0x7fffffff;
10078 rettv
->vval
.v_number
= (varnumber_T
)f
;
10083 * "floor({float})" function
10086 f_floor(argvars
, rettv
)
10092 rettv
->v_type
= VAR_FLOAT
;
10093 if (get_float_arg(argvars
, &f
) == OK
)
10094 rettv
->vval
.v_float
= floor(f
);
10096 rettv
->vval
.v_float
= 0.0;
10101 * "fnameescape({string})" function
10104 f_fnameescape(argvars
, rettv
)
10108 rettv
->vval
.v_string
= vim_strsave_fnameescape(
10109 get_tv_string(&argvars
[0]), FALSE
);
10110 rettv
->v_type
= VAR_STRING
;
10114 * "fnamemodify({fname}, {mods})" function
10117 f_fnamemodify(argvars
, rettv
)
10125 char_u
*fbuf
= NULL
;
10126 char_u buf
[NUMBUFLEN
];
10128 fname
= get_tv_string_chk(&argvars
[0]);
10129 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
10130 if (fname
== NULL
|| mods
== NULL
)
10134 len
= (int)STRLEN(fname
);
10135 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
10138 rettv
->v_type
= VAR_STRING
;
10140 rettv
->vval
.v_string
= NULL
;
10142 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
10146 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
10149 * "foldclosed()" function
10152 foldclosed_both(argvars
, rettv
, end
)
10157 #ifdef FEAT_FOLDING
10159 linenr_T first
, last
;
10161 lnum
= get_tv_lnum(argvars
);
10162 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10164 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
10167 rettv
->vval
.v_number
= (varnumber_T
)last
;
10169 rettv
->vval
.v_number
= (varnumber_T
)first
;
10174 rettv
->vval
.v_number
= -1;
10178 * "foldclosed()" function
10181 f_foldclosed(argvars
, rettv
)
10185 foldclosed_both(argvars
, rettv
, FALSE
);
10189 * "foldclosedend()" function
10192 f_foldclosedend(argvars
, rettv
)
10196 foldclosed_both(argvars
, rettv
, TRUE
);
10200 * "foldlevel()" function
10203 f_foldlevel(argvars
, rettv
)
10207 #ifdef FEAT_FOLDING
10210 lnum
= get_tv_lnum(argvars
);
10211 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10212 rettv
->vval
.v_number
= foldLevel(lnum
);
10217 * "foldtext()" function
10221 f_foldtext(argvars
, rettv
)
10225 #ifdef FEAT_FOLDING
10233 rettv
->v_type
= VAR_STRING
;
10234 rettv
->vval
.v_string
= NULL
;
10235 #ifdef FEAT_FOLDING
10236 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
10237 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10238 <= curbuf
->b_ml
.ml_line_count
10239 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
10241 /* Find first non-empty line in the fold. */
10242 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
10243 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10245 if (!linewhite(lnum
))
10250 /* Find interesting text in this line. */
10251 s
= skipwhite(ml_get(lnum
));
10252 /* skip C comment-start */
10253 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
10255 s
= skipwhite(s
+ 2);
10256 if (*skipwhite(s
) == NUL
10257 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10259 s
= skipwhite(ml_get(lnum
+ 1));
10261 s
= skipwhite(s
+ 1);
10264 txt
= _("+-%s%3ld lines: ");
10265 r
= alloc((unsigned)(STRLEN(txt
)
10266 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
10267 + 20 /* for %3ld */
10268 + STRLEN(s
))); /* concatenated */
10271 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
10272 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10273 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
10274 len
= (int)STRLEN(r
);
10276 /* remove 'foldmarker' and 'commentstring' */
10277 foldtext_cleanup(r
+ len
);
10278 rettv
->vval
.v_string
= r
;
10285 * "foldtextresult(lnum)" function
10289 f_foldtextresult(argvars
, rettv
)
10293 #ifdef FEAT_FOLDING
10297 foldinfo_T foldinfo
;
10301 rettv
->v_type
= VAR_STRING
;
10302 rettv
->vval
.v_string
= NULL
;
10303 #ifdef FEAT_FOLDING
10304 lnum
= get_tv_lnum(argvars
);
10305 /* treat illegal types and illegal string values for {lnum} the same */
10308 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
10309 if (fold_count
> 0)
10311 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
10314 text
= vim_strsave(text
);
10315 rettv
->vval
.v_string
= text
;
10321 * "foreground()" function
10325 f_foreground(argvars
, rettv
)
10331 gui_mch_set_foreground();
10334 win32_set_foreground();
10340 * "function()" function
10344 f_function(argvars
, rettv
)
10350 s
= get_tv_string(&argvars
[0]);
10351 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
10352 EMSG2(_(e_invarg2
), s
);
10353 /* Don't check an autoload name for existence here. */
10354 else if (vim_strchr(s
, AUTOLOAD_CHAR
) == NULL
&& !function_exists(s
))
10355 EMSG2(_("E700: Unknown function: %s"), s
);
10358 rettv
->vval
.v_string
= vim_strsave(s
);
10359 rettv
->v_type
= VAR_FUNC
;
10364 * "garbagecollect()" function
10368 f_garbagecollect(argvars
, rettv
)
10372 /* This is postponed until we are back at the toplevel, because we may be
10373 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10374 want_garbage_collect
= TRUE
;
10376 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
10377 garbage_collect_at_exit
= TRUE
;
10384 f_get(argvars
, rettv
)
10392 typval_T
*tv
= NULL
;
10394 if (argvars
[0].v_type
== VAR_LIST
)
10396 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
10400 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
10401 if (!error
&& li
!= NULL
)
10405 else if (argvars
[0].v_type
== VAR_DICT
)
10407 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
10409 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
10415 EMSG2(_(e_listdictarg
), "get()");
10419 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
10420 copy_tv(&argvars
[2], rettv
);
10423 copy_tv(tv
, rettv
);
10426 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
10429 * Get line or list of lines from buffer "buf" into "rettv".
10430 * Return a range (from start to end) of lines in rettv from the specified
10432 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
10435 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
10444 if (retlist
&& rettv_list_alloc(rettv
) == FAIL
)
10447 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
10452 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
10453 p
= ml_get_buf(buf
, start
, FALSE
);
10457 rettv
->v_type
= VAR_STRING
;
10458 rettv
->vval
.v_string
= vim_strsave(p
);
10467 if (end
> buf
->b_ml
.ml_line_count
)
10468 end
= buf
->b_ml
.ml_line_count
;
10469 while (start
<= end
)
10470 if (list_append_string(rettv
->vval
.v_list
,
10471 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
10477 * "getbufline()" function
10480 f_getbufline(argvars
, rettv
)
10488 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10490 buf
= get_buf_tv(&argvars
[0]);
10493 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
10494 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10497 end
= get_tv_lnum_buf(&argvars
[2], buf
);
10499 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
10503 * "getbufvar()" function
10506 f_getbufvar(argvars
, rettv
)
10511 buf_T
*save_curbuf
;
10515 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10516 varname
= get_tv_string_chk(&argvars
[1]);
10518 buf
= get_buf_tv(&argvars
[0]);
10520 rettv
->v_type
= VAR_STRING
;
10521 rettv
->vval
.v_string
= NULL
;
10523 if (buf
!= NULL
&& varname
!= NULL
)
10525 /* set curbuf to be our buf, temporarily */
10526 save_curbuf
= curbuf
;
10529 if (*varname
== '&') /* buffer-local-option */
10530 get_option_tv(&varname
, rettv
, TRUE
);
10533 if (*varname
== NUL
)
10534 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10535 * scope prefix before the NUL byte is required by
10536 * find_var_in_ht(). */
10537 varname
= (char_u
*)"b:" + 2;
10538 /* look up the variable */
10539 v
= find_var_in_ht(&curbuf
->b_vars
.dv_hashtab
, varname
, FALSE
);
10541 copy_tv(&v
->di_tv
, rettv
);
10544 /* restore previous notion of curbuf */
10545 curbuf
= save_curbuf
;
10552 * "getchar()" function
10555 f_getchar(argvars
, rettv
)
10562 /* Position the cursor. Needed after a message that ends in a space. */
10563 windgoto(msg_row
, msg_col
);
10569 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10570 /* getchar(): blocking wait. */
10572 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
10573 /* getchar(1): only check if char avail */
10575 else if (error
|| vpeekc() == NUL
)
10576 /* illegal argument or getchar(0) and no char avail: return zero */
10579 /* getchar(0) and char avail: return char */
10588 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
10589 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
10590 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
10592 rettv
->vval
.v_number
= n
;
10593 if (IS_SPECIAL(n
) || mod_mask
!= 0)
10595 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10598 /* Turn a special key into three bytes, plus modifier. */
10601 temp
[i
++] = K_SPECIAL
;
10602 temp
[i
++] = KS_MODIFIER
;
10603 temp
[i
++] = mod_mask
;
10607 temp
[i
++] = K_SPECIAL
;
10608 temp
[i
++] = K_SECOND(n
);
10609 temp
[i
++] = K_THIRD(n
);
10612 else if (has_mbyte
)
10613 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
10618 rettv
->v_type
= VAR_STRING
;
10619 rettv
->vval
.v_string
= vim_strsave(temp
);
10622 if (n
== K_LEFTMOUSE
10623 || n
== K_LEFTMOUSE_NM
10625 || n
== K_LEFTRELEASE
10626 || n
== K_LEFTRELEASE_NM
10627 || n
== K_MIDDLEMOUSE
10628 || n
== K_MIDDLEDRAG
10629 || n
== K_MIDDLERELEASE
10630 || n
== K_RIGHTMOUSE
10631 || n
== K_RIGHTDRAG
10632 || n
== K_RIGHTRELEASE
10635 || n
== K_X1RELEASE
10638 || n
== K_X2RELEASE
10639 || n
== K_MOUSEDOWN
10642 int row
= mouse_row
;
10643 int col
= mouse_col
;
10646 # ifdef FEAT_WINDOWS
10651 if (row
>= 0 && col
>= 0)
10653 /* Find the window at the mouse coordinates and compute the
10654 * text position. */
10655 win
= mouse_find_win(&row
, &col
);
10656 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10657 # ifdef FEAT_WINDOWS
10658 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10661 vimvars
[VV_MOUSE_WIN
].vv_nr
= winnr
;
10662 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10663 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10671 * "getcharmod()" function
10675 f_getcharmod(argvars
, rettv
)
10679 rettv
->vval
.v_number
= mod_mask
;
10683 * "getcmdline()" function
10687 f_getcmdline(argvars
, rettv
)
10691 rettv
->v_type
= VAR_STRING
;
10692 rettv
->vval
.v_string
= get_cmdline_str();
10696 * "getcmdpos()" function
10700 f_getcmdpos(argvars
, rettv
)
10704 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10708 * "getcmdtype()" function
10712 f_getcmdtype(argvars
, rettv
)
10716 rettv
->v_type
= VAR_STRING
;
10717 rettv
->vval
.v_string
= alloc(2);
10718 if (rettv
->vval
.v_string
!= NULL
)
10720 rettv
->vval
.v_string
[0] = get_cmdline_type();
10721 rettv
->vval
.v_string
[1] = NUL
;
10726 * "getcwd()" function
10730 f_getcwd(argvars
, rettv
)
10734 char_u cwd
[MAXPATHL
];
10736 rettv
->v_type
= VAR_STRING
;
10737 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10738 rettv
->vval
.v_string
= NULL
;
10741 rettv
->vval
.v_string
= vim_strsave(cwd
);
10742 #ifdef BACKSLASH_IN_FILENAME
10743 if (rettv
->vval
.v_string
!= NULL
)
10744 slash_adjust(rettv
->vval
.v_string
);
10750 * "getfontname()" function
10754 f_getfontname(argvars
, rettv
)
10758 rettv
->v_type
= VAR_STRING
;
10759 rettv
->vval
.v_string
= NULL
;
10764 char_u
*name
= NULL
;
10766 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10768 /* Get the "Normal" font. Either the name saved by
10769 * hl_set_font_name() or from the font ID. */
10770 font
= gui
.norm_font
;
10771 name
= hl_get_font_name();
10775 name
= get_tv_string(&argvars
[0]);
10776 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10778 font
= gui_mch_get_font(name
, FALSE
);
10779 if (font
== NOFONT
)
10780 return; /* Invalid font name, return empty string. */
10782 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10783 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10784 gui_mch_free_font(font
);
10790 * "getfperm({fname})" function
10793 f_getfperm(argvars
, rettv
)
10799 char_u
*perm
= NULL
;
10800 char_u flags
[] = "rwx";
10803 fname
= get_tv_string(&argvars
[0]);
10805 rettv
->v_type
= VAR_STRING
;
10806 if (mch_stat((char *)fname
, &st
) >= 0)
10808 perm
= vim_strsave((char_u
*)"---------");
10811 for (i
= 0; i
< 9; i
++)
10813 if (st
.st_mode
& (1 << (8 - i
)))
10814 perm
[i
] = flags
[i
% 3];
10818 rettv
->vval
.v_string
= perm
;
10822 * "getfsize({fname})" function
10825 f_getfsize(argvars
, rettv
)
10832 fname
= get_tv_string(&argvars
[0]);
10834 rettv
->v_type
= VAR_NUMBER
;
10836 if (mch_stat((char *)fname
, &st
) >= 0)
10838 if (mch_isdir(fname
))
10839 rettv
->vval
.v_number
= 0;
10842 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10844 /* non-perfect check for overflow */
10845 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
10846 rettv
->vval
.v_number
= -2;
10850 rettv
->vval
.v_number
= -1;
10854 * "getftime({fname})" function
10857 f_getftime(argvars
, rettv
)
10864 fname
= get_tv_string(&argvars
[0]);
10866 if (mch_stat((char *)fname
, &st
) >= 0)
10867 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10869 rettv
->vval
.v_number
= -1;
10873 * "getftype({fname})" function
10876 f_getftype(argvars
, rettv
)
10882 char_u
*type
= NULL
;
10885 fname
= get_tv_string(&argvars
[0]);
10887 rettv
->v_type
= VAR_STRING
;
10888 if (mch_lstat((char *)fname
, &st
) >= 0)
10891 if (S_ISREG(st
.st_mode
))
10893 else if (S_ISDIR(st
.st_mode
))
10896 else if (S_ISLNK(st
.st_mode
))
10900 else if (S_ISBLK(st
.st_mode
))
10904 else if (S_ISCHR(st
.st_mode
))
10908 else if (S_ISFIFO(st
.st_mode
))
10912 else if (S_ISSOCK(st
.st_mode
))
10919 switch (st
.st_mode
& S_IFMT
)
10921 case S_IFREG
: t
= "file"; break;
10922 case S_IFDIR
: t
= "dir"; break;
10924 case S_IFLNK
: t
= "link"; break;
10927 case S_IFBLK
: t
= "bdev"; break;
10930 case S_IFCHR
: t
= "cdev"; break;
10933 case S_IFIFO
: t
= "fifo"; break;
10936 case S_IFSOCK
: t
= "socket"; break;
10938 default: t
= "other";
10941 if (mch_isdir(fname
))
10947 type
= vim_strsave((char_u
*)t
);
10949 rettv
->vval
.v_string
= type
;
10953 * "getline(lnum, [end])" function
10956 f_getline(argvars
, rettv
)
10964 lnum
= get_tv_lnum(argvars
);
10965 if (argvars
[1].v_type
== VAR_UNKNOWN
)
10972 end
= get_tv_lnum(&argvars
[1]);
10976 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
10980 * "getmatches()" function
10984 f_getmatches(argvars
, rettv
)
10988 #ifdef FEAT_SEARCH_EXTRA
10990 matchitem_T
*cur
= curwin
->w_match_head
;
10992 if (rettv_list_alloc(rettv
) == OK
)
10994 while (cur
!= NULL
)
10996 dict
= dict_alloc();
10999 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
11000 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
11001 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
11002 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
11003 list_append_dict(rettv
->vval
.v_list
, dict
);
11011 * "getpid()" function
11015 f_getpid(argvars
, rettv
)
11019 rettv
->vval
.v_number
= mch_get_pid();
11023 * "getpos(string)" function
11026 f_getpos(argvars
, rettv
)
11034 if (rettv_list_alloc(rettv
) == OK
)
11036 l
= rettv
->vval
.v_list
;
11037 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
11039 list_append_number(l
, (varnumber_T
)fnum
);
11041 list_append_number(l
, (varnumber_T
)0);
11042 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
11044 list_append_number(l
, (fp
!= NULL
)
11045 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
11047 list_append_number(l
,
11048 #ifdef FEAT_VIRTUALEDIT
11049 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
11054 rettv
->vval
.v_number
= FALSE
;
11058 * "getqflist()" and "getloclist()" functions
11062 f_getqflist(argvars
, rettv
)
11066 #ifdef FEAT_QUICKFIX
11070 #ifdef FEAT_QUICKFIX
11071 if (rettv_list_alloc(rettv
) == OK
)
11074 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
11076 wp
= find_win_by_nr(&argvars
[0], NULL
);
11081 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
11087 * "getreg()" function
11090 f_getreg(argvars
, rettv
)
11094 char_u
*strregname
;
11099 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11101 strregname
= get_tv_string_chk(&argvars
[0]);
11102 error
= strregname
== NULL
;
11103 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11104 arg2
= get_tv_number_chk(&argvars
[1], &error
);
11107 strregname
= vimvars
[VV_REG
].vv_str
;
11108 regname
= (strregname
== NULL
? '"' : *strregname
);
11112 rettv
->v_type
= VAR_STRING
;
11113 rettv
->vval
.v_string
= error
? NULL
:
11114 get_reg_contents(regname
, TRUE
, arg2
);
11118 * "getregtype()" function
11121 f_getregtype(argvars
, rettv
)
11125 char_u
*strregname
;
11127 char_u buf
[NUMBUFLEN
+ 2];
11130 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11132 strregname
= get_tv_string_chk(&argvars
[0]);
11133 if (strregname
== NULL
) /* type error; errmsg already given */
11135 rettv
->v_type
= VAR_STRING
;
11136 rettv
->vval
.v_string
= NULL
;
11141 /* Default to v:register */
11142 strregname
= vimvars
[VV_REG
].vv_str
;
11144 regname
= (strregname
== NULL
? '"' : *strregname
);
11150 switch (get_reg_type(regname
, ®len
))
11152 case MLINE
: buf
[0] = 'V'; break;
11153 case MCHAR
: buf
[0] = 'v'; break;
11157 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
11161 rettv
->v_type
= VAR_STRING
;
11162 rettv
->vval
.v_string
= vim_strsave(buf
);
11166 * "gettabwinvar()" function
11169 f_gettabwinvar(argvars
, rettv
)
11173 getwinvar(argvars
, rettv
, 1);
11177 * "getwinposx()" function
11181 f_getwinposx(argvars
, rettv
)
11185 rettv
->vval
.v_number
= -1;
11191 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11192 rettv
->vval
.v_number
= x
;
11198 * "getwinposy()" function
11202 f_getwinposy(argvars
, rettv
)
11206 rettv
->vval
.v_number
= -1;
11212 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11213 rettv
->vval
.v_number
= y
;
11219 * Find window specified by "vp" in tabpage "tp".
11222 find_win_by_nr(vp
, tp
)
11224 tabpage_T
*tp
; /* NULL for current tab page */
11226 #ifdef FEAT_WINDOWS
11231 nr
= get_tv_number_chk(vp
, NULL
);
11233 #ifdef FEAT_WINDOWS
11239 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
11240 wp
!= NULL
; wp
= wp
->w_next
)
11245 if (nr
== 0 || nr
== 1)
11252 * "getwinvar()" function
11255 f_getwinvar(argvars
, rettv
)
11259 getwinvar(argvars
, rettv
, 0);
11263 * getwinvar() and gettabwinvar()
11266 getwinvar(argvars
, rettv
, off
)
11269 int off
; /* 1 for gettabwinvar() */
11271 win_T
*win
, *oldcurwin
;
11276 #ifdef FEAT_WINDOWS
11278 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
11282 win
= find_win_by_nr(&argvars
[off
], tp
);
11283 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
11286 rettv
->v_type
= VAR_STRING
;
11287 rettv
->vval
.v_string
= NULL
;
11289 if (win
!= NULL
&& varname
!= NULL
)
11291 /* Set curwin to be our win, temporarily. Also set curbuf, so
11292 * that we can get buffer-local options. */
11293 oldcurwin
= curwin
;
11295 curbuf
= win
->w_buffer
;
11297 if (*varname
== '&') /* window-local-option */
11298 get_option_tv(&varname
, rettv
, 1);
11301 if (*varname
== NUL
)
11302 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11303 * scope prefix before the NUL byte is required by
11304 * find_var_in_ht(). */
11305 varname
= (char_u
*)"w:" + 2;
11306 /* look up the variable */
11307 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
11309 copy_tv(&v
->di_tv
, rettv
);
11312 /* restore previous notion of curwin */
11313 curwin
= oldcurwin
;
11314 curbuf
= curwin
->w_buffer
;
11321 * "glob()" function
11324 f_glob(argvars
, rettv
)
11328 int flags
= WILD_SILENT
|WILD_USE_NL
;
11332 /* When the optional second argument is non-zero, don't remove matches
11333 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11334 if (argvars
[1].v_type
!= VAR_UNKNOWN
11335 && get_tv_number_chk(&argvars
[1], &error
))
11336 flags
|= WILD_KEEP_ALL
;
11337 rettv
->v_type
= VAR_STRING
;
11341 xpc
.xp_context
= EXPAND_FILES
;
11342 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
11343 NULL
, flags
, WILD_ALL
);
11346 rettv
->vval
.v_string
= NULL
;
11350 * "globpath()" function
11353 f_globpath(argvars
, rettv
)
11358 char_u buf1
[NUMBUFLEN
];
11359 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
11362 /* When the optional second argument is non-zero, don't remove matches
11363 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11364 if (argvars
[2].v_type
!= VAR_UNKNOWN
11365 && get_tv_number_chk(&argvars
[2], &error
))
11366 flags
|= WILD_KEEP_ALL
;
11367 rettv
->v_type
= VAR_STRING
;
11368 if (file
== NULL
|| error
)
11369 rettv
->vval
.v_string
= NULL
;
11371 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
,
11379 f_has(argvars
, rettv
)
11386 static char *(has_list
[]) =
11407 #if defined(MACOS_X_UNIX)
11431 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11440 #ifndef CASE_INSENSITIVE_FILENAME
11446 #ifdef FEAT_AUTOCMD
11451 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11452 "balloon_multiline",
11455 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11457 # ifdef ALL_BUILTIN_TCAPS
11458 "all_builtin_terms",
11461 #ifdef FEAT_BYTEOFF
11464 #ifdef FEAT_CINDENT
11467 #ifdef FEAT_CLIENTSERVER
11470 #ifdef FEAT_CLIPBOARD
11473 #ifdef FEAT_CMDL_COMPL
11476 #ifdef FEAT_CMDHIST
11479 #ifdef FEAT_COMMENTS
11488 #ifdef CURSOR_SHAPE
11494 #ifdef FEAT_CON_DIALOG
11497 #ifdef FEAT_GUI_DIALOG
11503 #ifdef FEAT_DIGRAPHS
11509 #ifdef FEAT_EMACS_TAGS
11512 "eval", /* always present, of course! */
11513 #ifdef FEAT_EX_EXTRA
11516 #ifdef FEAT_SEARCH_EXTRA
11522 #ifdef FEAT_SEARCHPATH
11525 #if defined(UNIX) && !defined(USE_SYSTEM)
11528 #ifdef FEAT_FIND_ID
11534 #ifdef FEAT_FOLDING
11540 #if !defined(USE_SYSTEM) && defined(UNIX)
11543 #ifdef FEAT_FULLSCREEN
11546 #ifdef FEAT_GETTEXT
11552 #ifdef FEAT_GUI_ATHENA
11553 # ifdef FEAT_GUI_NEXTAW
11559 #ifdef FEAT_GUI_GTK
11565 #ifdef FEAT_GUI_GNOME
11568 #ifdef FEAT_GUI_MAC
11571 #ifdef FEAT_GUI_MACVIM
11574 #ifdef FEAT_GUI_MOTIF
11577 #ifdef FEAT_GUI_PHOTON
11580 #ifdef FEAT_GUI_W16
11583 #ifdef FEAT_GUI_W32
11586 #ifdef FEAT_HANGULIN
11589 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11592 #ifdef FEAT_INS_EXPAND
11595 #ifdef FEAT_JUMPLIST
11601 #ifdef FEAT_LANGMAP
11604 #ifdef FEAT_LIBCALL
11607 #ifdef FEAT_LINEBREAK
11613 #ifdef FEAT_LISTCMDS
11616 #ifdef FEAT_LOCALMAP
11622 #ifdef FEAT_SESSION
11625 #ifdef FEAT_MODIFY_FNAME
11631 #ifdef FEAT_MOUSESHAPE
11634 #if defined(UNIX) || defined(VMS)
11635 # ifdef FEAT_MOUSE_DEC
11638 # ifdef FEAT_MOUSE_GPM
11641 # ifdef FEAT_MOUSE_JSB
11644 # ifdef FEAT_MOUSE_NET
11647 # ifdef FEAT_MOUSE_PTERM
11650 # ifdef FEAT_SYSMOUSE
11653 # ifdef FEAT_MOUSE_XTERM
11660 #ifdef FEAT_MBYTE_IME
11663 #ifdef FEAT_MULTI_LANG
11666 #ifdef FEAT_MZSCHEME
11667 #ifndef DYNAMIC_MZSCHEME
11674 #ifdef FEAT_OSFILETYPE
11677 #ifdef FEAT_PATH_EXTRA
11681 #ifndef DYNAMIC_PERL
11686 #ifndef DYNAMIC_PYTHON
11690 #ifdef FEAT_POSTSCRIPT
11693 #ifdef FEAT_PRINTER
11696 #ifdef FEAT_PROFILE
11699 #ifdef FEAT_RELTIME
11702 #ifdef FEAT_QUICKFIX
11705 #ifdef FEAT_RIGHTLEFT
11708 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11711 #ifdef FEAT_SCROLLBIND
11714 #ifdef FEAT_CMDL_INFO
11721 #ifdef FEAT_SMARTINDENT
11727 #ifdef FEAT_STL_OPT
11730 #ifdef FEAT_SUN_WORKSHOP
11733 #ifdef FEAT_NETBEANS_INTG
11736 #ifdef FEAT_ODB_EDITOR
11745 #if defined(USE_SYSTEM) || !defined(UNIX)
11748 #ifdef FEAT_TAG_BINS
11751 #ifdef FEAT_TAG_OLDSTATIC
11754 #ifdef FEAT_TAG_ANYWHITE
11758 # ifndef DYNAMIC_TCL
11765 #ifdef FEAT_TERMRESPONSE
11768 #ifdef FEAT_TEXTOBJ
11771 #ifdef HAVE_TGETENT
11777 #ifdef FEAT_TOOLBAR
11780 #ifdef FEAT_TRANSPARENCY
11783 #ifdef FEAT_USR_CMDS
11784 "user-commands", /* was accidentally included in 5.4 */
11787 #ifdef FEAT_VIMINFO
11790 #ifdef FEAT_VERTSPLIT
11793 #ifdef FEAT_VIRTUALEDIT
11799 #ifdef FEAT_VISUALEXTRA
11802 #ifdef FEAT_VREPLACE
11805 #ifdef FEAT_WILDIGN
11808 #ifdef FEAT_WILDMENU
11811 #ifdef FEAT_WINDOWS
11817 #ifdef FEAT_WRITEBACKUP
11823 #ifdef FEAT_XFONTSET
11829 #ifdef USE_XSMP_INTERACT
11832 #ifdef FEAT_XCLIPBOARD
11835 #ifdef FEAT_XTERM_SAVE
11838 #if defined(UNIX) && defined(FEAT_X11)
11844 name
= get_tv_string(&argvars
[0]);
11845 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11846 if (STRICMP(name
, has_list
[i
]) == 0)
11854 if (STRNICMP(name
, "patch", 5) == 0)
11855 n
= has_patch(atoi((char *)name
+ 5));
11856 else if (STRICMP(name
, "vim_starting") == 0)
11857 n
= (starting
!= 0);
11859 else if (STRICMP(name
, "multi_byte_encoding") == 0)
11862 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11863 else if (STRICMP(name
, "balloon_multiline") == 0)
11864 n
= multiline_balloon_available();
11867 else if (STRICMP(name
, "tcl") == 0)
11868 n
= tcl_enabled(FALSE
);
11870 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11871 else if (STRICMP(name
, "iconv") == 0)
11872 n
= iconv_enabled(FALSE
);
11874 #ifdef DYNAMIC_MZSCHEME
11875 else if (STRICMP(name
, "mzscheme") == 0)
11876 n
= mzscheme_enabled(FALSE
);
11878 #ifdef DYNAMIC_RUBY
11879 else if (STRICMP(name
, "ruby") == 0)
11880 n
= ruby_enabled(FALSE
);
11882 #ifdef DYNAMIC_PYTHON
11883 else if (STRICMP(name
, "python") == 0)
11884 n
= python_enabled(FALSE
);
11886 #ifdef DYNAMIC_PERL
11887 else if (STRICMP(name
, "perl") == 0)
11888 n
= perl_enabled(FALSE
);
11891 else if (STRICMP(name
, "gui_running") == 0)
11892 n
= (gui
.in_use
|| gui
.starting
);
11893 # ifdef FEAT_GUI_W32
11894 else if (STRICMP(name
, "gui_win32s") == 0)
11895 n
= gui_is_win32s();
11897 # ifdef FEAT_BROWSE
11898 else if (STRICMP(name
, "browse") == 0)
11899 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11903 else if (STRICMP(name
, "syntax_items") == 0)
11904 n
= syntax_present(curbuf
);
11906 #if defined(WIN3264)
11907 else if (STRICMP(name
, "win95") == 0)
11908 n
= mch_windows95();
11910 #ifdef FEAT_NETBEANS_INTG
11911 else if (STRICMP(name
, "netbeans_enabled") == 0)
11916 rettv
->vval
.v_number
= n
;
11920 * "has_key()" function
11923 f_has_key(argvars
, rettv
)
11927 if (argvars
[0].v_type
!= VAR_DICT
)
11929 EMSG(_(e_dictreq
));
11932 if (argvars
[0].vval
.v_dict
== NULL
)
11935 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11936 get_tv_string(&argvars
[1]), -1) != NULL
;
11940 * "haslocaldir()" function
11944 f_haslocaldir(argvars
, rettv
)
11948 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11952 * "hasmapto()" function
11955 f_hasmapto(argvars
, rettv
)
11961 char_u buf
[NUMBUFLEN
];
11964 name
= get_tv_string(&argvars
[0]);
11965 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11966 mode
= (char_u
*)"nvo";
11969 mode
= get_tv_string_buf(&argvars
[1], buf
);
11970 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11971 abbr
= get_tv_number(&argvars
[2]);
11974 if (map_to_exists(name
, mode
, abbr
))
11975 rettv
->vval
.v_number
= TRUE
;
11977 rettv
->vval
.v_number
= FALSE
;
11981 * "histadd()" function
11985 f_histadd(argvars
, rettv
)
11989 #ifdef FEAT_CMDHIST
11992 char_u buf
[NUMBUFLEN
];
11995 rettv
->vval
.v_number
= FALSE
;
11996 if (check_restricted() || check_secure())
11998 #ifdef FEAT_CMDHIST
11999 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12000 histype
= str
!= NULL
? get_histtype(str
) : -1;
12003 str
= get_tv_string_buf(&argvars
[1], buf
);
12006 add_to_history(histype
, str
, FALSE
, NUL
);
12007 rettv
->vval
.v_number
= TRUE
;
12015 * "histdel()" function
12019 f_histdel(argvars
, rettv
)
12023 #ifdef FEAT_CMDHIST
12025 char_u buf
[NUMBUFLEN
];
12028 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12031 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
12032 /* only one argument: clear entire history */
12033 n
= clr_history(get_histtype(str
));
12034 else if (argvars
[1].v_type
== VAR_NUMBER
)
12035 /* index given: remove that entry */
12036 n
= del_history_idx(get_histtype(str
),
12037 (int)get_tv_number(&argvars
[1]));
12039 /* string given: remove all matching entries */
12040 n
= del_history_entry(get_histtype(str
),
12041 get_tv_string_buf(&argvars
[1], buf
));
12042 rettv
->vval
.v_number
= n
;
12047 * "histget()" function
12051 f_histget(argvars
, rettv
)
12055 #ifdef FEAT_CMDHIST
12060 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12062 rettv
->vval
.v_string
= NULL
;
12065 type
= get_histtype(str
);
12066 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12067 idx
= get_history_idx(type
);
12069 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
12070 /* -1 on type error */
12071 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
12074 rettv
->vval
.v_string
= NULL
;
12076 rettv
->v_type
= VAR_STRING
;
12080 * "histnr()" function
12084 f_histnr(argvars
, rettv
)
12090 #ifdef FEAT_CMDHIST
12091 char_u
*history
= get_tv_string_chk(&argvars
[0]);
12093 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
12094 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
12095 i
= get_history_idx(i
);
12099 rettv
->vval
.v_number
= i
;
12103 * "highlightID(name)" function
12106 f_hlID(argvars
, rettv
)
12110 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
12114 * "highlight_exists()" function
12117 f_hlexists(argvars
, rettv
)
12121 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
12125 * "hostname()" function
12129 f_hostname(argvars
, rettv
)
12133 char_u hostname
[256];
12135 mch_get_host_name(hostname
, 256);
12136 rettv
->v_type
= VAR_STRING
;
12137 rettv
->vval
.v_string
= vim_strsave(hostname
);
12145 f_iconv(argvars
, rettv
)
12150 char_u buf1
[NUMBUFLEN
];
12151 char_u buf2
[NUMBUFLEN
];
12152 char_u
*from
, *to
, *str
;
12156 rettv
->v_type
= VAR_STRING
;
12157 rettv
->vval
.v_string
= NULL
;
12160 str
= get_tv_string(&argvars
[0]);
12161 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
12162 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
12163 vimconv
.vc_type
= CONV_NONE
;
12164 convert_setup(&vimconv
, from
, to
);
12166 /* If the encodings are equal, no conversion needed. */
12167 if (vimconv
.vc_type
== CONV_NONE
)
12168 rettv
->vval
.v_string
= vim_strsave(str
);
12170 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
12172 convert_setup(&vimconv
, NULL
, NULL
);
12179 * "indent()" function
12182 f_indent(argvars
, rettv
)
12188 lnum
= get_tv_lnum(argvars
);
12189 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12190 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
12192 rettv
->vval
.v_number
= -1;
12196 * "index()" function
12199 f_index(argvars
, rettv
)
12208 rettv
->vval
.v_number
= -1;
12209 if (argvars
[0].v_type
!= VAR_LIST
)
12211 EMSG(_(e_listreq
));
12214 l
= argvars
[0].vval
.v_list
;
12217 item
= l
->lv_first
;
12218 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12222 /* Start at specified item. Use the cached index that list_find()
12223 * sets, so that a negative number also works. */
12224 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
12226 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12227 ic
= get_tv_number_chk(&argvars
[3], &error
);
12232 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
12233 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
12235 rettv
->vval
.v_number
= idx
;
12241 static int inputsecret_flag
= 0;
12243 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
12246 * This function is used by f_input() and f_inputdialog() functions. The third
12247 * argument to f_input() specifies the type of completion to use at the
12248 * prompt. The third argument to f_inputdialog() specifies the value to return
12249 * when the user cancels the prompt.
12252 get_user_input(argvars
, rettv
, inputdialog
)
12257 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
12260 char_u buf
[NUMBUFLEN
];
12261 int cmd_silent_save
= cmd_silent
;
12262 char_u
*defstr
= (char_u
*)"";
12263 int xp_type
= EXPAND_NOTHING
;
12264 char_u
*xp_arg
= NULL
;
12266 rettv
->v_type
= VAR_STRING
;
12267 rettv
->vval
.v_string
= NULL
;
12269 #ifdef NO_CONSOLE_INPUT
12270 /* While starting up, there is no place to enter text. */
12271 if (no_console_input())
12275 cmd_silent
= FALSE
; /* Want to see the prompt. */
12276 if (prompt
!= NULL
)
12278 /* Only the part of the message after the last NL is considered as
12279 * prompt for the command line */
12280 p
= vim_strrchr(prompt
, '\n');
12290 msg_puts_attr(prompt
, echo_attr
);
12291 msg_didout
= FALSE
;
12295 cmdline_row
= msg_row
;
12297 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12299 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
12300 if (defstr
!= NULL
)
12301 stuffReadbuffSpec(defstr
);
12303 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
12309 rettv
->vval
.v_string
= NULL
;
12311 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
12312 if (xp_name
== NULL
)
12315 xp_namelen
= (int)STRLEN(xp_name
);
12317 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
12323 if (defstr
!= NULL
)
12324 rettv
->vval
.v_string
=
12325 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
12330 /* since the user typed this, no need to wait for return */
12331 need_wait_return
= FALSE
;
12332 msg_didout
= FALSE
;
12334 cmd_silent
= cmd_silent_save
;
12338 * "input()" function
12339 * Also handles inputsecret() when inputsecret is set.
12342 f_input(argvars
, rettv
)
12346 get_user_input(argvars
, rettv
, FALSE
);
12350 * "inputdialog()" function
12353 f_inputdialog(argvars
, rettv
)
12357 #if defined(FEAT_GUI_TEXTDIALOG)
12358 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12359 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
12362 char_u buf
[NUMBUFLEN
];
12363 char_u
*defstr
= (char_u
*)"";
12365 message
= get_tv_string_chk(&argvars
[0]);
12366 if (argvars
[1].v_type
!= VAR_UNKNOWN
12367 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
12368 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
12371 if (message
!= NULL
&& defstr
!= NULL
12372 && do_dialog(VIM_QUESTION
, NULL
, message
,
12373 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
12374 rettv
->vval
.v_string
= vim_strsave(IObuff
);
12377 if (message
!= NULL
&& defstr
!= NULL
12378 && argvars
[1].v_type
!= VAR_UNKNOWN
12379 && argvars
[2].v_type
!= VAR_UNKNOWN
)
12380 rettv
->vval
.v_string
= vim_strsave(
12381 get_tv_string_buf(&argvars
[2], buf
));
12383 rettv
->vval
.v_string
= NULL
;
12385 rettv
->v_type
= VAR_STRING
;
12389 get_user_input(argvars
, rettv
, TRUE
);
12393 * "inputlist()" function
12396 f_inputlist(argvars
, rettv
)
12404 #ifdef NO_CONSOLE_INPUT
12405 /* While starting up, there is no place to enter text. */
12406 if (no_console_input())
12409 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
12411 EMSG2(_(e_listarg
), "inputlist()");
12416 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
12417 lines_left
= Rows
; /* avoid more prompt */
12421 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
12423 msg_puts(get_tv_string(&li
->li_tv
));
12427 /* Ask for choice. */
12428 selected
= prompt_for_number(&mouse_used
);
12430 selected
-= lines_left
;
12432 rettv
->vval
.v_number
= selected
;
12436 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
12439 * "inputrestore()" function
12443 f_inputrestore(argvars
, rettv
)
12447 if (ga_userinput
.ga_len
> 0)
12449 --ga_userinput
.ga_len
;
12450 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12451 + ga_userinput
.ga_len
);
12452 /* default return is zero == OK */
12454 else if (p_verbose
> 1)
12456 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
12457 rettv
->vval
.v_number
= 1; /* Failed */
12462 * "inputsave()" function
12466 f_inputsave(argvars
, rettv
)
12470 /* Add an entry to the stack of typeahead storage. */
12471 if (ga_grow(&ga_userinput
, 1) == OK
)
12473 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12474 + ga_userinput
.ga_len
);
12475 ++ga_userinput
.ga_len
;
12476 /* default return is zero == OK */
12479 rettv
->vval
.v_number
= 1; /* Failed */
12483 * "inputsecret()" function
12486 f_inputsecret(argvars
, rettv
)
12491 ++inputsecret_flag
;
12492 f_input(argvars
, rettv
);
12494 --inputsecret_flag
;
12498 * "insert()" function
12501 f_insert(argvars
, rettv
)
12510 if (argvars
[0].v_type
!= VAR_LIST
)
12511 EMSG2(_(e_listarg
), "insert()");
12512 else if ((l
= argvars
[0].vval
.v_list
) != NULL
12513 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
12515 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12516 before
= get_tv_number_chk(&argvars
[2], &error
);
12518 return; /* type error; errmsg already given */
12520 if (before
== l
->lv_len
)
12524 item
= list_find(l
, before
);
12527 EMSGN(_(e_listidx
), before
);
12533 list_insert_tv(l
, &argvars
[1], item
);
12534 copy_tv(&argvars
[0], rettv
);
12540 * "isdirectory()" function
12543 f_isdirectory(argvars
, rettv
)
12547 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
12551 * "islocked()" function
12554 f_islocked(argvars
, rettv
)
12562 rettv
->vval
.v_number
= -1;
12563 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
12565 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
12568 EMSG(_(e_trailing
));
12571 if (lv
.ll_tv
== NULL
)
12573 if (check_changedtick(lv
.ll_name
))
12574 rettv
->vval
.v_number
= 1; /* always locked */
12577 di
= find_var(lv
.ll_name
, NULL
);
12580 /* Consider a variable locked when:
12581 * 1. the variable itself is locked
12582 * 2. the value of the variable is locked.
12583 * 3. the List or Dict value is locked.
12585 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
12586 || tv_islocked(&di
->di_tv
));
12590 else if (lv
.ll_range
)
12591 EMSG(_("E786: Range not allowed"));
12592 else if (lv
.ll_newkey
!= NULL
)
12593 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
12594 else if (lv
.ll_list
!= NULL
)
12596 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
12598 /* Dictionary item. */
12599 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
12606 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
12609 * Turn a dict into a list:
12610 * "what" == 0: list of keys
12611 * "what" == 1: list of values
12612 * "what" == 2: list of items
12615 dict_list(argvars
, rettv
, what
)
12628 if (argvars
[0].v_type
!= VAR_DICT
)
12630 EMSG(_(e_dictreq
));
12633 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
12636 if (rettv_list_alloc(rettv
) == FAIL
)
12639 todo
= (int)d
->dv_hashtab
.ht_used
;
12640 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12642 if (!HASHITEM_EMPTY(hi
))
12647 li
= listitem_alloc();
12650 list_append(rettv
->vval
.v_list
, li
);
12655 li
->li_tv
.v_type
= VAR_STRING
;
12656 li
->li_tv
.v_lock
= 0;
12657 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12659 else if (what
== 1)
12662 copy_tv(&di
->di_tv
, &li
->li_tv
);
12668 li
->li_tv
.v_type
= VAR_LIST
;
12669 li
->li_tv
.v_lock
= 0;
12670 li
->li_tv
.vval
.v_list
= l2
;
12675 li2
= listitem_alloc();
12678 list_append(l2
, li2
);
12679 li2
->li_tv
.v_type
= VAR_STRING
;
12680 li2
->li_tv
.v_lock
= 0;
12681 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12683 li2
= listitem_alloc();
12686 list_append(l2
, li2
);
12687 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12694 * "items(dict)" function
12697 f_items(argvars
, rettv
)
12701 dict_list(argvars
, rettv
, 2);
12705 * "join()" function
12708 f_join(argvars
, rettv
)
12715 if (argvars
[0].v_type
!= VAR_LIST
)
12717 EMSG(_(e_listreq
));
12720 if (argvars
[0].vval
.v_list
== NULL
)
12722 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12723 sep
= (char_u
*)" ";
12725 sep
= get_tv_string_chk(&argvars
[1]);
12727 rettv
->v_type
= VAR_STRING
;
12731 ga_init2(&ga
, (int)sizeof(char), 80);
12732 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12733 ga_append(&ga
, NUL
);
12734 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12737 rettv
->vval
.v_string
= NULL
;
12741 * "keys()" function
12744 f_keys(argvars
, rettv
)
12748 dict_list(argvars
, rettv
, 0);
12752 * "last_buffer_nr()" function.
12756 f_last_buffer_nr(argvars
, rettv
)
12763 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12764 if (n
< buf
->b_fnum
)
12767 rettv
->vval
.v_number
= n
;
12774 f_len(argvars
, rettv
)
12778 switch (argvars
[0].v_type
)
12782 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12783 get_tv_string(&argvars
[0]));
12786 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12789 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12792 EMSG(_("E701: Invalid type for len()"));
12797 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12800 libcall_common(argvars
, rettv
, type
)
12805 #ifdef FEAT_LIBCALL
12807 char_u
**string_result
;
12811 rettv
->v_type
= type
;
12812 if (type
!= VAR_NUMBER
)
12813 rettv
->vval
.v_string
= NULL
;
12815 if (check_restricted() || check_secure())
12818 #ifdef FEAT_LIBCALL
12819 /* The first two args must be strings, otherwise its meaningless */
12820 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12823 if (argvars
[2].v_type
== VAR_STRING
)
12824 string_in
= argvars
[2].vval
.v_string
;
12825 if (type
== VAR_NUMBER
)
12826 string_result
= NULL
;
12828 string_result
= &rettv
->vval
.v_string
;
12829 if (mch_libcall(argvars
[0].vval
.v_string
,
12830 argvars
[1].vval
.v_string
,
12832 argvars
[2].vval
.v_number
,
12835 && type
== VAR_NUMBER
)
12836 rettv
->vval
.v_number
= nr_result
;
12842 * "libcall()" function
12845 f_libcall(argvars
, rettv
)
12849 libcall_common(argvars
, rettv
, VAR_STRING
);
12853 * "libcallnr()" function
12856 f_libcallnr(argvars
, rettv
)
12860 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12864 * "line(string)" function
12867 f_line(argvars
, rettv
)
12875 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12878 rettv
->vval
.v_number
= lnum
;
12882 * "line2byte(lnum)" function
12886 f_line2byte(argvars
, rettv
)
12890 #ifndef FEAT_BYTEOFF
12891 rettv
->vval
.v_number
= -1;
12895 lnum
= get_tv_lnum(argvars
);
12896 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12897 rettv
->vval
.v_number
= -1;
12899 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12900 if (rettv
->vval
.v_number
>= 0)
12901 ++rettv
->vval
.v_number
;
12906 * "lispindent(lnum)" function
12909 f_lispindent(argvars
, rettv
)
12917 pos
= curwin
->w_cursor
;
12918 lnum
= get_tv_lnum(argvars
);
12919 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12921 curwin
->w_cursor
.lnum
= lnum
;
12922 rettv
->vval
.v_number
= get_lisp_indent();
12923 curwin
->w_cursor
= pos
;
12927 rettv
->vval
.v_number
= -1;
12931 * "localtime()" function
12935 f_localtime(argvars
, rettv
)
12939 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12942 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12945 get_maparg(argvars
, rettv
, exact
)
12952 char_u buf
[NUMBUFLEN
];
12953 char_u
*keys_buf
= NULL
;
12959 /* return empty string for failure */
12960 rettv
->v_type
= VAR_STRING
;
12961 rettv
->vval
.v_string
= NULL
;
12963 keys
= get_tv_string(&argvars
[0]);
12967 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12969 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12970 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12971 abbr
= get_tv_number(&argvars
[2]);
12974 which
= (char_u
*)"";
12978 mode
= get_map_mode(&which
, 0);
12980 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12981 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
12982 vim_free(keys_buf
);
12986 ga
.ga_itemsize
= 1;
12987 ga
.ga_growsize
= 40;
12989 while (*rhs
!= NUL
)
12990 ga_concat(&ga
, str2special(&rhs
, FALSE
));
12992 ga_append(&ga
, NUL
);
12993 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12999 * "log10()" function
13002 f_log10(argvars
, rettv
)
13008 rettv
->v_type
= VAR_FLOAT
;
13009 if (get_float_arg(argvars
, &f
) == OK
)
13010 rettv
->vval
.v_float
= log10(f
);
13012 rettv
->vval
.v_float
= 0.0;
13020 f_map(argvars
, rettv
)
13024 filter_map(argvars
, rettv
, TRUE
);
13028 * "maparg()" function
13031 f_maparg(argvars
, rettv
)
13035 get_maparg(argvars
, rettv
, TRUE
);
13039 * "mapcheck()" function
13042 f_mapcheck(argvars
, rettv
)
13046 get_maparg(argvars
, rettv
, FALSE
);
13049 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
13052 find_some_match(argvars
, rettv
, type
)
13057 char_u
*str
= NULL
;
13058 char_u
*expr
= NULL
;
13060 regmatch_T regmatch
;
13061 char_u patbuf
[NUMBUFLEN
];
13062 char_u strbuf
[NUMBUFLEN
];
13066 colnr_T startcol
= 0;
13069 listitem_T
*li
= NULL
;
13071 char_u
*tofree
= NULL
;
13073 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13075 p_cpo
= (char_u
*)"";
13077 rettv
->vval
.v_number
= -1;
13080 /* return empty list when there are no matches */
13081 if (rettv_list_alloc(rettv
) == FAIL
)
13084 else if (type
== 2)
13086 rettv
->v_type
= VAR_STRING
;
13087 rettv
->vval
.v_string
= NULL
;
13090 if (argvars
[0].v_type
== VAR_LIST
)
13092 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
13097 expr
= str
= get_tv_string(&argvars
[0]);
13099 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
13103 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13107 start
= get_tv_number_chk(&argvars
[2], &error
);
13112 li
= list_find(l
, start
);
13115 idx
= l
->lv_idx
; /* use the cached index */
13121 if (start
> (long)STRLEN(str
))
13123 /* When "count" argument is there ignore matches before "start",
13124 * otherwise skip part of the string. Differs when pattern is "^"
13126 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13132 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13133 nth
= get_tv_number_chk(&argvars
[3], &error
);
13138 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
13139 if (regmatch
.regprog
!= NULL
)
13141 regmatch
.rm_ic
= p_ic
;
13153 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
13158 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
13160 if (match
&& --nth
<= 0)
13162 if (l
== NULL
&& !match
)
13165 /* Advance to just after the match. */
13174 startcol
= (colnr_T
)(regmatch
.startp
[0]
13175 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
13177 startcol
= regmatch
.startp
[0] + 1 - str
;
13188 /* return list with matched string and submatches */
13189 for (i
= 0; i
< NSUBEXP
; ++i
)
13191 if (regmatch
.endp
[i
] == NULL
)
13193 if (list_append_string(rettv
->vval
.v_list
,
13194 (char_u
*)"", 0) == FAIL
)
13197 else if (list_append_string(rettv
->vval
.v_list
,
13198 regmatch
.startp
[i
],
13199 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
13204 else if (type
== 2)
13206 /* return matched string */
13208 copy_tv(&li
->li_tv
, rettv
);
13210 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
13211 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
13213 else if (l
!= NULL
)
13214 rettv
->vval
.v_number
= idx
;
13218 rettv
->vval
.v_number
=
13219 (varnumber_T
)(regmatch
.startp
[0] - str
);
13221 rettv
->vval
.v_number
=
13222 (varnumber_T
)(regmatch
.endp
[0] - str
);
13223 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
13226 vim_free(regmatch
.regprog
);
13235 * "match()" function
13238 f_match(argvars
, rettv
)
13242 find_some_match(argvars
, rettv
, 1);
13246 * "matchadd()" function
13249 f_matchadd(argvars
, rettv
)
13253 #ifdef FEAT_SEARCH_EXTRA
13254 char_u buf
[NUMBUFLEN
];
13255 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
13256 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
13257 int prio
= 10; /* default priority */
13261 rettv
->vval
.v_number
= -1;
13263 if (grp
== NULL
|| pat
== NULL
)
13265 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13267 prio
= get_tv_number_chk(&argvars
[2], &error
);
13268 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13269 id
= get_tv_number_chk(&argvars
[3], &error
);
13273 if (id
>= 1 && id
<= 3)
13275 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
13279 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
13284 * "matcharg()" function
13287 f_matcharg(argvars
, rettv
)
13291 if (rettv_list_alloc(rettv
) == OK
)
13293 #ifdef FEAT_SEARCH_EXTRA
13294 int id
= get_tv_number(&argvars
[0]);
13297 if (id
>= 1 && id
<= 3)
13299 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
13301 list_append_string(rettv
->vval
.v_list
,
13302 syn_id2name(m
->hlg_id
), -1);
13303 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
13307 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13308 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13316 * "matchdelete()" function
13319 f_matchdelete(argvars
, rettv
)
13323 #ifdef FEAT_SEARCH_EXTRA
13324 rettv
->vval
.v_number
= match_delete(curwin
,
13325 (int)get_tv_number(&argvars
[0]), TRUE
);
13330 * "matchend()" function
13333 f_matchend(argvars
, rettv
)
13337 find_some_match(argvars
, rettv
, 0);
13341 * "matchlist()" function
13344 f_matchlist(argvars
, rettv
)
13348 find_some_match(argvars
, rettv
, 3);
13352 * "matchstr()" function
13355 f_matchstr(argvars
, rettv
)
13359 find_some_match(argvars
, rettv
, 2);
13362 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
13365 max_min(argvars
, rettv
, domax
)
13374 if (argvars
[0].v_type
== VAR_LIST
)
13379 l
= argvars
[0].vval
.v_list
;
13385 n
= get_tv_number_chk(&li
->li_tv
, &error
);
13391 i
= get_tv_number_chk(&li
->li_tv
, &error
);
13392 if (domax
? i
> n
: i
< n
)
13398 else if (argvars
[0].v_type
== VAR_DICT
)
13405 d
= argvars
[0].vval
.v_dict
;
13408 todo
= (int)d
->dv_hashtab
.ht_used
;
13409 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
13411 if (!HASHITEM_EMPTY(hi
))
13414 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
13420 else if (domax
? i
> n
: i
< n
)
13427 EMSG(_(e_listdictarg
));
13428 rettv
->vval
.v_number
= error
? 0 : n
;
13435 f_max(argvars
, rettv
)
13439 max_min(argvars
, rettv
, TRUE
);
13446 f_min(argvars
, rettv
)
13450 max_min(argvars
, rettv
, FALSE
);
13453 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
13456 * Create the directory in which "dir" is located, and higher levels when
13460 mkdir_recurse(dir
, prot
)
13468 /* Get end of directory name in "dir".
13469 * We're done when it's "/" or "c:/". */
13470 p
= gettail_sep(dir
);
13471 if (p
<= get_past_head(dir
))
13474 /* If the directory exists we're done. Otherwise: create it.*/
13475 updir
= vim_strnsave(dir
, (int)(p
- dir
));
13478 if (mch_isdir(updir
))
13480 else if (mkdir_recurse(updir
, prot
) == OK
)
13481 r
= vim_mkdir_emsg(updir
, prot
);
13488 * "mkdir()" function
13491 f_mkdir(argvars
, rettv
)
13496 char_u buf
[NUMBUFLEN
];
13499 rettv
->vval
.v_number
= FAIL
;
13500 if (check_restricted() || check_secure())
13503 dir
= get_tv_string_buf(&argvars
[0], buf
);
13504 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13506 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13507 prot
= get_tv_number_chk(&argvars
[2], NULL
);
13508 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
13509 mkdir_recurse(dir
, prot
);
13511 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
13516 * "mode()" function
13520 f_mode(argvars
, rettv
)
13533 buf
[0] = VIsual_mode
+ 's' - 'v';
13535 buf
[0] = VIsual_mode
;
13539 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
13540 || State
== CONFIRM
)
13543 if (State
== ASKMORE
)
13545 else if (State
== CONFIRM
)
13548 else if (State
== EXTERNCMD
)
13550 else if (State
& INSERT
)
13552 #ifdef FEAT_VREPLACE
13553 if (State
& VREPLACE_FLAG
)
13560 if (State
& REPLACE_FLAG
)
13565 else if (State
& CMDLINE
)
13571 else if (exmode_active
)
13583 /* Clear out the minor mode when the argument is not a non-zero number or
13584 * non-empty string. */
13585 if (!non_zero_arg(&argvars
[0]))
13588 rettv
->vval
.v_string
= vim_strsave(buf
);
13589 rettv
->v_type
= VAR_STRING
;
13593 * "nextnonblank()" function
13596 f_nextnonblank(argvars
, rettv
)
13602 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
13604 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
13609 if (*skipwhite(ml_get(lnum
)) != NUL
)
13612 rettv
->vval
.v_number
= lnum
;
13616 * "nr2char()" function
13619 f_nr2char(argvars
, rettv
)
13623 char_u buf
[NUMBUFLEN
];
13627 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
13631 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
13634 rettv
->v_type
= VAR_STRING
;
13635 rettv
->vval
.v_string
= vim_strsave(buf
);
13639 * "pathshorten()" function
13642 f_pathshorten(argvars
, rettv
)
13648 rettv
->v_type
= VAR_STRING
;
13649 p
= get_tv_string_chk(&argvars
[0]);
13651 rettv
->vval
.v_string
= NULL
;
13654 p
= vim_strsave(p
);
13655 rettv
->vval
.v_string
= p
;
13666 f_pow(argvars
, rettv
)
13672 rettv
->v_type
= VAR_FLOAT
;
13673 if (get_float_arg(argvars
, &fx
) == OK
13674 && get_float_arg(&argvars
[1], &fy
) == OK
)
13675 rettv
->vval
.v_float
= pow(fx
, fy
);
13677 rettv
->vval
.v_float
= 0.0;
13682 * "prevnonblank()" function
13685 f_prevnonblank(argvars
, rettv
)
13691 lnum
= get_tv_lnum(argvars
);
13692 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
13695 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
13697 rettv
->vval
.v_number
= lnum
;
13700 #ifdef HAVE_STDARG_H
13701 /* This dummy va_list is here because:
13702 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13703 * - locally in the function results in a "used before set" warning
13704 * - using va_start() to initialize it gives "function with fixed args" error */
13709 * "printf()" function
13712 f_printf(argvars
, rettv
)
13716 rettv
->v_type
= VAR_STRING
;
13717 rettv
->vval
.v_string
= NULL
;
13718 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
13720 char_u buf
[NUMBUFLEN
];
13723 int saved_did_emsg
= did_emsg
;
13726 /* Get the required length, allocate the buffer and do it for real. */
13728 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
13729 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
13732 s
= alloc(len
+ 1);
13735 rettv
->vval
.v_string
= s
;
13736 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
13739 did_emsg
|= saved_did_emsg
;
13745 * "pumvisible()" function
13749 f_pumvisible(argvars
, rettv
)
13753 #ifdef FEAT_INS_EXPAND
13755 rettv
->vval
.v_number
= 1;
13760 * "range()" function
13763 f_range(argvars
, rettv
)
13773 start
= get_tv_number_chk(&argvars
[0], &error
);
13774 if (argvars
[1].v_type
== VAR_UNKNOWN
)
13781 end
= get_tv_number_chk(&argvars
[1], &error
);
13782 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13783 stride
= get_tv_number_chk(&argvars
[2], &error
);
13787 return; /* type error; errmsg already given */
13789 EMSG(_("E726: Stride is zero"));
13790 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
13791 EMSG(_("E727: Start past end"));
13794 if (rettv_list_alloc(rettv
) == OK
)
13795 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
13796 if (list_append_number(rettv
->vval
.v_list
,
13797 (varnumber_T
)i
) == FAIL
)
13803 * "readfile()" function
13806 f_readfile(argvars
, rettv
)
13810 int binary
= FALSE
;
13814 #define FREAD_SIZE 200 /* optimized for text lines */
13815 char_u buf
[FREAD_SIZE
];
13816 int readlen
; /* size of last fread() */
13817 int buflen
; /* nr of valid chars in buf[] */
13818 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
13819 int tolist
; /* first byte in buf[] still to be put in list */
13820 int chop
; /* how many CR to chop off */
13821 char_u
*prev
= NULL
; /* previously read bytes, if any */
13822 int prevlen
= 0; /* length of "prev" if not NULL */
13825 long maxline
= MAXLNUM
;
13828 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13830 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
13832 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13833 maxline
= get_tv_number(&argvars
[2]);
13836 if (rettv_list_alloc(rettv
) == FAIL
)
13839 /* Always open the file in binary mode, library functions have a mind of
13840 * their own about CR-LF conversion. */
13841 fname
= get_tv_string(&argvars
[0]);
13842 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
13844 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
13849 while (cnt
< maxline
|| maxline
< 0)
13851 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
13852 buflen
= filtd
+ readlen
;
13854 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
13856 if (buf
[filtd
] == '\n' || readlen
<= 0)
13858 /* Only when in binary mode add an empty list item when the
13859 * last line ends in a '\n'. */
13860 if (!binary
&& readlen
== 0 && filtd
== 0)
13863 /* Found end-of-line or end-of-file: add a text line to the
13867 while (filtd
- chop
- 1 >= tolist
13868 && buf
[filtd
- chop
- 1] == '\r')
13870 len
= filtd
- tolist
- chop
;
13872 s
= vim_strnsave(buf
+ tolist
, len
);
13875 s
= alloc((unsigned)(prevlen
+ len
+ 1));
13878 mch_memmove(s
, prev
, prevlen
);
13881 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
13882 s
[prevlen
+ len
] = NUL
;
13885 tolist
= filtd
+ 1;
13887 li
= listitem_alloc();
13893 li
->li_tv
.v_type
= VAR_STRING
;
13894 li
->li_tv
.v_lock
= 0;
13895 li
->li_tv
.vval
.v_string
= s
;
13896 list_append(rettv
->vval
.v_list
, li
);
13898 if (++cnt
>= maxline
&& maxline
>= 0)
13903 else if (buf
[filtd
] == NUL
)
13911 /* "buf" is full, need to move text to an allocated buffer */
13914 prev
= vim_strnsave(buf
, buflen
);
13919 s
= alloc((unsigned)(prevlen
+ buflen
));
13922 mch_memmove(s
, prev
, prevlen
);
13923 mch_memmove(s
+ prevlen
, buf
, buflen
);
13933 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13939 * For a negative line count use only the lines at the end of the file,
13943 while (cnt
> -maxline
)
13945 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
13953 #if defined(FEAT_RELTIME)
13954 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
13957 * Convert a List to proftime_T.
13958 * Return FAIL when there is something wrong.
13961 list2proftime(arg
, tm
)
13968 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
13969 || arg
->vval
.v_list
->lv_len
!= 2)
13971 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
13972 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
13980 return error
? FAIL
: OK
;
13982 #endif /* FEAT_RELTIME */
13985 * "reltime()" function
13988 f_reltime(argvars
, rettv
)
13992 #ifdef FEAT_RELTIME
13996 if (argvars
[0].v_type
== VAR_UNKNOWN
)
13998 /* No arguments: get current time. */
13999 profile_start(&res
);
14001 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
14003 if (list2proftime(&argvars
[0], &res
) == FAIL
)
14009 /* Two arguments: compute the difference. */
14010 if (list2proftime(&argvars
[0], &start
) == FAIL
14011 || list2proftime(&argvars
[1], &res
) == FAIL
)
14013 profile_sub(&res
, &start
);
14016 if (rettv_list_alloc(rettv
) == OK
)
14027 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
14028 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
14034 * "reltimestr()" function
14037 f_reltimestr(argvars
, rettv
)
14041 #ifdef FEAT_RELTIME
14045 rettv
->v_type
= VAR_STRING
;
14046 rettv
->vval
.v_string
= NULL
;
14047 #ifdef FEAT_RELTIME
14048 if (list2proftime(&argvars
[0], &tm
) == OK
)
14049 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
14053 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14054 static void make_connection
__ARGS((void));
14055 static int check_connection
__ARGS((void));
14060 if (X_DISPLAY
== NULL
14066 x_force_connect
= TRUE
;
14068 x_force_connect
= FALSE
;
14076 if (X_DISPLAY
== NULL
)
14078 EMSG(_("E240: No connection to Vim server"));
14085 #ifdef FEAT_CLIENTSERVER
14086 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
14089 remote_common(argvars
, rettv
, expr
)
14094 char_u
*server_name
;
14097 char_u buf
[NUMBUFLEN
];
14100 # elif defined(FEAT_X11)
14102 # elif defined(MAC_CLIENTSERVER)
14103 int w
; // This is the port number ('w' is a bit confusing)
14106 if (check_restricted() || check_secure())
14110 if (check_connection() == FAIL
)
14114 server_name
= get_tv_string_chk(&argvars
[0]);
14115 if (server_name
== NULL
)
14116 return; /* type error; errmsg already given */
14117 keys
= get_tv_string_buf(&argvars
[1], buf
);
14119 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14120 # elif defined(FEAT_X11)
14121 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
14123 # elif defined(MAC_CLIENTSERVER)
14124 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14128 EMSG(r
); /* sending worked but evaluation failed */
14130 EMSG2(_("E241: Unable to send to %s"), server_name
);
14134 rettv
->vval
.v_string
= r
;
14136 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14142 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
14143 v
.di_tv
.v_type
= VAR_STRING
;
14144 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
14145 idvar
= get_tv_string_chk(&argvars
[2]);
14147 set_var(idvar
, &v
.di_tv
, FALSE
);
14148 vim_free(v
.di_tv
.vval
.v_string
);
14154 * "remote_expr()" function
14158 f_remote_expr(argvars
, rettv
)
14162 rettv
->v_type
= VAR_STRING
;
14163 rettv
->vval
.v_string
= NULL
;
14164 #ifdef FEAT_CLIENTSERVER
14165 remote_common(argvars
, rettv
, TRUE
);
14170 * "remote_foreground()" function
14174 f_remote_foreground(argvars
, rettv
)
14178 #ifdef FEAT_CLIENTSERVER
14180 /* On Win32 it's done in this application. */
14182 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
14184 if (server_name
!= NULL
)
14185 serverForeground(server_name
);
14187 # elif defined(FEAT_X11) || defined(MAC_CLIENTSERVER)
14188 /* Send a foreground() expression to the server. */
14189 argvars
[1].v_type
= VAR_STRING
;
14190 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
14191 argvars
[2].v_type
= VAR_UNKNOWN
;
14192 remote_common(argvars
, rettv
, TRUE
);
14193 vim_free(argvars
[1].vval
.v_string
);
14200 f_remote_peek(argvars
, rettv
)
14204 #ifdef FEAT_CLIENTSERVER
14212 if (check_restricted() || check_secure())
14214 rettv
->vval
.v_number
= -1;
14217 serverid
= get_tv_string_chk(&argvars
[0]);
14218 if (serverid
== NULL
)
14220 rettv
->vval
.v_number
= -1;
14221 return; /* type error; errmsg already given */
14224 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14226 rettv
->vval
.v_number
= -1;
14229 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
14230 rettv
->vval
.v_number
= (s
!= NULL
);
14232 # elif defined(FEAT_X11)
14233 if (check_connection() == FAIL
)
14236 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
14237 serverStrToWin(serverid
), &s
);
14238 # elif defined(MAC_CLIENTSERVER)
14239 rettv
->vval
.v_number
= serverPeekReply(serverStrToPort(serverid
), &s
);
14242 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
14246 v
.di_tv
.v_type
= VAR_STRING
;
14247 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
14248 retvar
= get_tv_string_chk(&argvars
[1]);
14249 if (retvar
!= NULL
)
14250 set_var(retvar
, &v
.di_tv
, FALSE
);
14251 vim_free(v
.di_tv
.vval
.v_string
);
14254 rettv
->vval
.v_number
= -1;
14260 f_remote_read(argvars
, rettv
)
14266 #ifdef FEAT_CLIENTSERVER
14267 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
14269 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
14272 /* The server's HWND is encoded in the 'id' parameter */
14275 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14277 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
14279 # elif defined(FEAT_X11)
14280 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
14281 serverStrToWin(serverid
), &r
, FALSE
) < 0)
14282 # elif defined(MAC_CLIENTSERVER)
14283 if (serverReadReply(serverStrToPort(serverid
), &r
) < 0)
14285 EMSG(_("E277: Unable to read a server reply"));
14288 rettv
->v_type
= VAR_STRING
;
14289 rettv
->vval
.v_string
= r
;
14293 * "remote_send()" function
14297 f_remote_send(argvars
, rettv
)
14301 rettv
->v_type
= VAR_STRING
;
14302 rettv
->vval
.v_string
= NULL
;
14303 #ifdef FEAT_CLIENTSERVER
14304 remote_common(argvars
, rettv
, FALSE
);
14309 * "remove()" function
14312 f_remove(argvars
, rettv
)
14317 listitem_T
*item
, *item2
;
14325 if (argvars
[0].v_type
== VAR_DICT
)
14327 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14328 EMSG2(_(e_toomanyarg
), "remove()");
14329 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
14330 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
14332 key
= get_tv_string_chk(&argvars
[1]);
14335 di
= dict_find(d
, key
, -1);
14337 EMSG2(_(e_dictkey
), key
);
14340 *rettv
= di
->di_tv
;
14341 init_tv(&di
->di_tv
);
14342 dictitem_remove(d
, di
);
14347 else if (argvars
[0].v_type
!= VAR_LIST
)
14348 EMSG2(_(e_listdictarg
), "remove()");
14349 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14350 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
14354 idx
= get_tv_number_chk(&argvars
[1], &error
);
14356 ; /* type error: do nothing, errmsg already given */
14357 else if ((item
= list_find(l
, idx
)) == NULL
)
14358 EMSGN(_(e_listidx
), idx
);
14361 if (argvars
[2].v_type
== VAR_UNKNOWN
)
14363 /* Remove one item, return its value. */
14364 list_remove(l
, item
, item
);
14365 *rettv
= item
->li_tv
;
14370 /* Remove range of items, return list with values. */
14371 end
= get_tv_number_chk(&argvars
[2], &error
);
14373 ; /* type error: do nothing */
14374 else if ((item2
= list_find(l
, end
)) == NULL
)
14375 EMSGN(_(e_listidx
), end
);
14380 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
14386 if (li
== NULL
) /* didn't find "item2" after "item" */
14387 EMSG(_(e_invrange
));
14390 list_remove(l
, item
, item2
);
14391 if (rettv_list_alloc(rettv
) == OK
)
14393 l
= rettv
->vval
.v_list
;
14394 l
->lv_first
= item
;
14395 l
->lv_last
= item2
;
14396 item
->li_prev
= NULL
;
14397 item2
->li_next
= NULL
;
14408 * "rename({from}, {to})" function
14411 f_rename(argvars
, rettv
)
14415 char_u buf
[NUMBUFLEN
];
14417 if (check_restricted() || check_secure())
14418 rettv
->vval
.v_number
= -1;
14420 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
14421 get_tv_string_buf(&argvars
[1], buf
));
14425 * "repeat()" function
14429 f_repeat(argvars
, rettv
)
14440 n
= get_tv_number(&argvars
[1]);
14441 if (argvars
[0].v_type
== VAR_LIST
)
14443 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
14445 if (list_extend(rettv
->vval
.v_list
,
14446 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
14451 p
= get_tv_string(&argvars
[0]);
14452 rettv
->v_type
= VAR_STRING
;
14453 rettv
->vval
.v_string
= NULL
;
14455 slen
= (int)STRLEN(p
);
14460 r
= alloc(len
+ 1);
14463 for (i
= 0; i
< n
; i
++)
14464 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
14468 rettv
->vval
.v_string
= r
;
14473 * "resolve()" function
14476 f_resolve(argvars
, rettv
)
14482 p
= get_tv_string(&argvars
[0]);
14483 #ifdef FEAT_SHORTCUT
14487 v
= mch_resolve_shortcut(p
);
14489 rettv
->vval
.v_string
= v
;
14491 rettv
->vval
.v_string
= vim_strsave(p
);
14494 # ifdef HAVE_READLINK
14496 char_u buf
[MAXPATHL
+ 1];
14499 char_u
*remain
= NULL
;
14501 int is_relative_to_current
= FALSE
;
14502 int has_trailing_pathsep
= FALSE
;
14505 p
= vim_strsave(p
);
14507 if (p
[0] == '.' && (vim_ispathsep(p
[1])
14508 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
14509 is_relative_to_current
= TRUE
;
14512 if (len
> 0 && after_pathsep(p
, p
+ len
))
14513 has_trailing_pathsep
= TRUE
;
14515 q
= getnextcomp(p
);
14518 /* Separate the first path component in "p", and keep the
14519 * remainder (beginning with the path separator). */
14520 remain
= vim_strsave(q
- 1);
14528 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
14537 EMSG(_("E655: Too many symbolic links (cycle?)"));
14538 rettv
->vval
.v_string
= NULL
;
14542 /* Ensure that the result will have a trailing path separator
14543 * if the argument has one. */
14544 if (remain
== NULL
&& has_trailing_pathsep
)
14547 /* Separate the first path component in the link value and
14548 * concatenate the remainders. */
14549 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
14552 if (remain
== NULL
)
14553 remain
= vim_strsave(q
- 1);
14556 cpy
= concat_str(q
- 1, remain
);
14567 if (q
> p
&& *q
== NUL
)
14569 /* Ignore trailing path separator. */
14573 if (q
> p
&& !mch_isFullName(buf
))
14575 /* symlink is relative to directory of argument */
14576 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
14580 STRCPY(gettail(cpy
), buf
);
14588 p
= vim_strsave(buf
);
14592 if (remain
== NULL
)
14595 /* Append the first path component of "remain" to "p". */
14596 q
= getnextcomp(remain
+ 1);
14597 len
= q
- remain
- (*q
!= NUL
);
14598 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
14601 STRNCAT(cpy
, remain
, len
);
14605 /* Shorten "remain". */
14607 STRMOVE(remain
, q
- 1);
14615 /* If the result is a relative path name, make it explicitly relative to
14616 * the current directory if and only if the argument had this form. */
14617 if (!vim_ispathsep(*p
))
14619 if (is_relative_to_current
14623 || vim_ispathsep(p
[1])
14626 || vim_ispathsep(p
[2]))))))
14628 /* Prepend "./". */
14629 cpy
= concat_str((char_u
*)"./", p
);
14636 else if (!is_relative_to_current
)
14638 /* Strip leading "./". */
14640 while (q
[0] == '.' && vim_ispathsep(q
[1]))
14647 /* Ensure that the result will have no trailing path separator
14648 * if the argument had none. But keep "/" or "//". */
14649 if (!has_trailing_pathsep
)
14652 if (after_pathsep(p
, q
))
14653 *gettail_sep(p
) = NUL
;
14656 rettv
->vval
.v_string
= p
;
14659 rettv
->vval
.v_string
= vim_strsave(p
);
14663 simplify_filename(rettv
->vval
.v_string
);
14665 #ifdef HAVE_READLINK
14668 rettv
->v_type
= VAR_STRING
;
14672 * "reverse({list})" function
14675 f_reverse(argvars
, rettv
)
14680 listitem_T
*li
, *ni
;
14682 if (argvars
[0].v_type
!= VAR_LIST
)
14683 EMSG2(_(e_listarg
), "reverse()");
14684 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14685 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
14688 l
->lv_first
= l
->lv_last
= NULL
;
14693 list_append(l
, li
);
14696 rettv
->vval
.v_list
= l
;
14697 rettv
->v_type
= VAR_LIST
;
14699 l
->lv_idx
= l
->lv_len
- l
->lv_idx
- 1;
14703 #define SP_NOMOVE 0x01 /* don't move cursor */
14704 #define SP_REPEAT 0x02 /* repeat to find outer pair */
14705 #define SP_RETCOUNT 0x04 /* return matchcount */
14706 #define SP_SETPCMARK 0x08 /* set previous context mark */
14707 #define SP_START 0x10 /* accept match at start position */
14708 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14709 #define SP_END 0x40 /* leave cursor at end of match */
14711 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
14714 * Get flags for a search function.
14715 * Possibly sets "p_ws".
14716 * Returns BACKWARD, FORWARD or zero (for an error).
14719 get_search_arg(varp
, flagsp
)
14725 char_u nbuf
[NUMBUFLEN
];
14728 if (varp
->v_type
!= VAR_UNKNOWN
)
14730 flags
= get_tv_string_buf_chk(varp
, nbuf
);
14732 return 0; /* type error; errmsg already given */
14733 while (*flags
!= NUL
)
14737 case 'b': dir
= BACKWARD
; break;
14738 case 'w': p_ws
= TRUE
; break;
14739 case 'W': p_ws
= FALSE
; break;
14741 if (flagsp
!= NULL
)
14744 case 'c': mask
= SP_START
; break;
14745 case 'e': mask
= SP_END
; break;
14746 case 'm': mask
= SP_RETCOUNT
; break;
14747 case 'n': mask
= SP_NOMOVE
; break;
14748 case 'p': mask
= SP_SUBPAT
; break;
14749 case 'r': mask
= SP_REPEAT
; break;
14750 case 's': mask
= SP_SETPCMARK
; break;
14754 EMSG2(_(e_invarg2
), flags
);
14769 * Shared by search() and searchpos() functions
14772 search_cmn(argvars
, match_pos
, flagsp
)
14781 int save_p_ws
= p_ws
;
14783 int retval
= 0; /* default: FAIL */
14784 long lnum_stop
= 0;
14786 #ifdef FEAT_RELTIME
14787 long time_limit
= 0;
14789 int options
= SEARCH_KEEP
;
14792 pat
= get_tv_string(&argvars
[0]);
14793 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
14797 if (flags
& SP_START
)
14798 options
|= SEARCH_START
;
14799 if (flags
& SP_END
)
14800 options
|= SEARCH_END
;
14802 /* Optional arguments: line number to stop searching and timeout. */
14803 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14805 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
14808 #ifdef FEAT_RELTIME
14809 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
14811 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
14812 if (time_limit
< 0)
14818 #ifdef FEAT_RELTIME
14819 /* Set the time limit, if there is one. */
14820 profile_setlimit(time_limit
, &tm
);
14824 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
14825 * Check to make sure only those flags are set.
14826 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14827 * flags cannot be set. Check for that condition also.
14829 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
14830 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14832 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
14836 pos
= save_cursor
= curwin
->w_cursor
;
14837 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14838 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
14839 if (subpatnum
!= FAIL
)
14841 if (flags
& SP_SUBPAT
)
14842 retval
= subpatnum
;
14845 if (flags
& SP_SETPCMARK
)
14847 curwin
->w_cursor
= pos
;
14848 if (match_pos
!= NULL
)
14850 /* Store the match cursor position */
14851 match_pos
->lnum
= pos
.lnum
;
14852 match_pos
->col
= pos
.col
+ 1;
14854 /* "/$" will put the cursor after the end of the line, may need to
14855 * correct that here */
14859 /* If 'n' flag is used: restore cursor position. */
14860 if (flags
& SP_NOMOVE
)
14861 curwin
->w_cursor
= save_cursor
;
14863 curwin
->w_set_curswant
= TRUE
;
14872 * "round({float})" function
14875 f_round(argvars
, rettv
)
14881 rettv
->v_type
= VAR_FLOAT
;
14882 if (get_float_arg(argvars
, &f
) == OK
)
14883 /* round() is not in C90, use ceil() or floor() instead. */
14884 rettv
->vval
.v_float
= f
> 0 ? floor(f
+ 0.5) : ceil(f
- 0.5);
14886 rettv
->vval
.v_float
= 0.0;
14891 * "search()" function
14894 f_search(argvars
, rettv
)
14900 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
14904 * "searchdecl()" function
14907 f_searchdecl(argvars
, rettv
)
14916 rettv
->vval
.v_number
= 1; /* default: FAIL */
14918 name
= get_tv_string_chk(&argvars
[0]);
14919 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14921 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
14922 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14923 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
14925 if (!error
&& name
!= NULL
)
14926 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
14927 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
14931 * Used by searchpair() and searchpairpos()
14934 searchpair_cmn(argvars
, match_pos
)
14938 char_u
*spat
, *mpat
, *epat
;
14940 int save_p_ws
= p_ws
;
14943 char_u nbuf1
[NUMBUFLEN
];
14944 char_u nbuf2
[NUMBUFLEN
];
14945 char_u nbuf3
[NUMBUFLEN
];
14946 int retval
= 0; /* default: FAIL */
14947 long lnum_stop
= 0;
14948 long time_limit
= 0;
14950 /* Get the three pattern arguments: start, middle, end. */
14951 spat
= get_tv_string_chk(&argvars
[0]);
14952 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
14953 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
14954 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
14955 goto theend
; /* type error */
14957 /* Handle the optional fourth argument: flags */
14958 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
14962 /* Don't accept SP_END or SP_SUBPAT.
14963 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14965 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
14966 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14968 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
14972 /* Using 'r' implies 'W', otherwise it doesn't work. */
14973 if (flags
& SP_REPEAT
)
14976 /* Optional fifth argument: skip expression */
14977 if (argvars
[3].v_type
== VAR_UNKNOWN
14978 || argvars
[4].v_type
== VAR_UNKNOWN
)
14979 skip
= (char_u
*)"";
14982 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
14983 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
14985 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
14988 #ifdef FEAT_RELTIME
14989 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
14991 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
14992 if (time_limit
< 0)
14999 goto theend
; /* type error */
15001 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
15002 match_pos
, lnum_stop
, time_limit
);
15011 * "searchpair()" function
15014 f_searchpair(argvars
, rettv
)
15018 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
15022 * "searchpairpos()" function
15025 f_searchpairpos(argvars
, rettv
)
15033 if (rettv_list_alloc(rettv
) == FAIL
)
15036 if (searchpair_cmn(argvars
, &match_pos
) > 0)
15038 lnum
= match_pos
.lnum
;
15039 col
= match_pos
.col
;
15042 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15043 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15047 * Search for a start/middle/end thing.
15048 * Used by searchpair(), see its documentation for the details.
15049 * Returns 0 or -1 for no match,
15052 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
15053 lnum_stop
, time_limit
)
15054 char_u
*spat
; /* start pattern */
15055 char_u
*mpat
; /* middle pattern */
15056 char_u
*epat
; /* end pattern */
15057 int dir
; /* BACKWARD or FORWARD */
15058 char_u
*skip
; /* skip expression */
15059 int flags
; /* SP_SETPCMARK and other SP_ values */
15061 linenr_T lnum_stop
; /* stop at this line if not zero */
15062 long time_limit
; /* stop after this many msec */
15065 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
15076 int options
= SEARCH_KEEP
;
15079 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15081 p_cpo
= empty_option
;
15083 #ifdef FEAT_RELTIME
15084 /* Set the time limit, if there is one. */
15085 profile_setlimit(time_limit
, &tm
);
15088 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15089 * start/middle/end (pat3, for the top pair). */
15090 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
15091 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
15092 if (pat2
== NULL
|| pat3
== NULL
)
15094 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
15096 STRCPY(pat3
, pat2
);
15098 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15100 if (flags
& SP_START
)
15101 options
|= SEARCH_START
;
15103 save_cursor
= curwin
->w_cursor
;
15104 pos
= curwin
->w_cursor
;
15105 clearpos(&firstpos
);
15106 clearpos(&foundpos
);
15110 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15111 options
, RE_SEARCH
, lnum_stop
, &tm
);
15112 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
15113 /* didn't find it or found the first match again: FAIL */
15116 if (firstpos
.lnum
== 0)
15118 if (equalpos(pos
, foundpos
))
15120 /* Found the same position again. Can happen with a pattern that
15121 * has "\zs" at the end and searching backwards. Advance one
15122 * character and try again. */
15123 if (dir
== BACKWARD
)
15130 /* clear the start flag to avoid getting stuck here */
15131 options
&= ~SEARCH_START
;
15133 /* If the skip pattern matches, ignore this match. */
15136 save_pos
= curwin
->w_cursor
;
15137 curwin
->w_cursor
= pos
;
15138 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
15139 curwin
->w_cursor
= save_pos
;
15142 /* Evaluating {skip} caused an error, break here. */
15143 curwin
->w_cursor
= save_cursor
;
15151 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
15153 /* Found end when searching backwards or start when searching
15154 * forward: nested pair. */
15156 pat
= pat2
; /* nested, don't search for middle */
15160 /* Found end when searching forward or start when searching
15161 * backward: end of (nested) pair; or found middle in outer pair. */
15163 pat
= pat3
; /* outer level, search for middle */
15168 /* Found the match: return matchcount or line number. */
15169 if (flags
& SP_RETCOUNT
)
15173 if (flags
& SP_SETPCMARK
)
15175 curwin
->w_cursor
= pos
;
15176 if (!(flags
& SP_REPEAT
))
15178 nest
= 1; /* search for next unmatched */
15182 if (match_pos
!= NULL
)
15184 /* Store the match cursor position */
15185 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
15186 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
15189 /* If 'n' flag is used or search failed: restore cursor position. */
15190 if ((flags
& SP_NOMOVE
) || retval
== 0)
15191 curwin
->w_cursor
= save_cursor
;
15196 if (p_cpo
== empty_option
)
15199 /* Darn, evaluating the {skip} expression changed the value. */
15200 free_string_option(save_cpo
);
15206 * "searchpos()" function
15209 f_searchpos(argvars
, rettv
)
15219 if (rettv_list_alloc(rettv
) == FAIL
)
15222 n
= search_cmn(argvars
, &match_pos
, &flags
);
15225 lnum
= match_pos
.lnum
;
15226 col
= match_pos
.col
;
15229 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15230 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15231 if (flags
& SP_SUBPAT
)
15232 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
15238 f_server2client(argvars
, rettv
)
15242 #ifdef FEAT_CLIENTSERVER
15243 char_u buf
[NUMBUFLEN
];
15244 char_u
*server
= get_tv_string_chk(&argvars
[0]);
15245 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
15247 rettv
->vval
.v_number
= -1;
15248 if (server
== NULL
|| reply
== NULL
)
15250 if (check_restricted() || check_secure())
15253 if (check_connection() == FAIL
)
15257 if (serverSendReply(server
, reply
) < 0)
15259 EMSG(_("E258: Unable to send to client"));
15262 rettv
->vval
.v_number
= 0;
15264 rettv
->vval
.v_number
= -1;
15270 f_serverlist(argvars
, rettv
)
15276 #ifdef FEAT_CLIENTSERVER
15277 # if defined(WIN32) || defined(MAC_CLIENTSERVER)
15278 r
= serverGetVimNames();
15279 # elif defined(FEAT_X11)
15281 if (X_DISPLAY
!= NULL
)
15282 r
= serverGetVimNames(X_DISPLAY
);
15285 rettv
->v_type
= VAR_STRING
;
15286 rettv
->vval
.v_string
= r
;
15290 * "setbufvar()" function
15294 f_setbufvar(argvars
, rettv
)
15300 char_u
*varname
, *bufvarname
;
15302 char_u nbuf
[NUMBUFLEN
];
15304 if (check_restricted() || check_secure())
15306 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
15307 varname
= get_tv_string_chk(&argvars
[1]);
15308 buf
= get_buf_tv(&argvars
[0]);
15309 varp
= &argvars
[2];
15311 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15313 /* set curbuf to be our buf, temporarily */
15314 aucmd_prepbuf(&aco
, buf
);
15316 if (*varname
== '&')
15323 numval
= get_tv_number_chk(varp
, &error
);
15324 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15325 if (!error
&& strval
!= NULL
)
15326 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15330 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15331 if (bufvarname
!= NULL
)
15333 STRCPY(bufvarname
, "b:");
15334 STRCPY(bufvarname
+ 2, varname
);
15335 set_var(bufvarname
, varp
, TRUE
);
15336 vim_free(bufvarname
);
15340 /* reset notion of buffer */
15341 aucmd_restbuf(&aco
);
15346 * "setcmdpos()" function
15349 f_setcmdpos(argvars
, rettv
)
15353 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
15356 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
15360 * "setline()" function
15363 f_setline(argvars
, rettv
)
15368 char_u
*line
= NULL
;
15370 listitem_T
*li
= NULL
;
15372 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
15374 lnum
= get_tv_lnum(&argvars
[0]);
15375 if (argvars
[1].v_type
== VAR_LIST
)
15377 l
= argvars
[1].vval
.v_list
;
15381 line
= get_tv_string_chk(&argvars
[1]);
15383 /* default result is zero == OK */
15388 /* list argument, get next string */
15391 line
= get_tv_string_chk(&li
->li_tv
);
15395 rettv
->vval
.v_number
= 1; /* FAIL */
15396 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
15398 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
15400 /* existing line, replace it */
15401 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
15403 changed_bytes(lnum
, 0);
15404 if (lnum
== curwin
->w_cursor
.lnum
)
15405 check_cursor_col();
15406 rettv
->vval
.v_number
= 0; /* OK */
15409 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
15411 /* lnum is one past the last line, append the line */
15413 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
15414 rettv
->vval
.v_number
= 0; /* OK */
15417 if (l
== NULL
) /* only one string argument */
15423 appended_lines_mark(lcount
, added
);
15426 static void set_qf_ll_list
__ARGS((win_T
*wp
, typval_T
*list_arg
, typval_T
*action_arg
, typval_T
*rettv
));
15429 * Used by "setqflist()" and "setloclist()" functions
15433 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
15435 typval_T
*list_arg
;
15436 typval_T
*action_arg
;
15439 #ifdef FEAT_QUICKFIX
15444 rettv
->vval
.v_number
= -1;
15446 #ifdef FEAT_QUICKFIX
15447 if (list_arg
->v_type
!= VAR_LIST
)
15448 EMSG(_(e_listreq
));
15451 list_T
*l
= list_arg
->vval
.v_list
;
15453 if (action_arg
->v_type
== VAR_STRING
)
15455 act
= get_tv_string_chk(action_arg
);
15457 return; /* type error; errmsg already given */
15458 if (*act
== 'a' || *act
== 'r')
15462 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
15463 rettv
->vval
.v_number
= 0;
15469 * "setloclist()" function
15473 f_setloclist(argvars
, rettv
)
15479 rettv
->vval
.v_number
= -1;
15481 win
= find_win_by_nr(&argvars
[0], NULL
);
15483 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
15487 * "setmatches()" function
15490 f_setmatches(argvars
, rettv
)
15494 #ifdef FEAT_SEARCH_EXTRA
15499 rettv
->vval
.v_number
= -1;
15500 if (argvars
[0].v_type
!= VAR_LIST
)
15502 EMSG(_(e_listreq
));
15505 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
15508 /* To some extent make sure that we are dealing with a list from
15509 * "getmatches()". */
15513 if (li
->li_tv
.v_type
!= VAR_DICT
15514 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
15519 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
15520 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
15521 && dict_find(d
, (char_u
*)"priority", -1) != NULL
15522 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
15530 clear_matches(curwin
);
15534 d
= li
->li_tv
.vval
.v_dict
;
15535 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
15536 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
15537 (int)get_dict_number(d
, (char_u
*)"priority"),
15538 (int)get_dict_number(d
, (char_u
*)"id"));
15541 rettv
->vval
.v_number
= 0;
15547 * "setpos()" function
15551 f_setpos(argvars
, rettv
)
15559 rettv
->vval
.v_number
= -1;
15560 name
= get_tv_string_chk(argvars
);
15563 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
15566 if (name
[0] == '.' && name
[1] == NUL
)
15569 if (fnum
== curbuf
->b_fnum
)
15571 curwin
->w_cursor
= pos
;
15573 rettv
->vval
.v_number
= 0;
15578 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
15581 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
15582 rettv
->vval
.v_number
= 0;
15591 * "setqflist()" function
15595 f_setqflist(argvars
, rettv
)
15599 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
15603 * "setreg()" function
15606 f_setreg(argvars
, rettv
)
15611 char_u
*strregname
;
15622 strregname
= get_tv_string_chk(argvars
);
15623 rettv
->vval
.v_number
= 1; /* FAIL is default */
15625 if (strregname
== NULL
)
15626 return; /* type error; errmsg already given */
15627 regname
= *strregname
;
15628 if (regname
== 0 || regname
== '@')
15630 else if (regname
== '=')
15633 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15635 stropt
= get_tv_string_chk(&argvars
[2]);
15636 if (stropt
== NULL
)
15637 return; /* type error */
15638 for (; *stropt
!= NUL
; ++stropt
)
15641 case 'a': case 'A': /* append */
15644 case 'v': case 'c': /* character-wise selection */
15647 case 'V': case 'l': /* line-wise selection */
15651 case 'b': case Ctrl_V
: /* block-wise selection */
15652 yank_type
= MBLOCK
;
15653 if (VIM_ISDIGIT(stropt
[1]))
15656 block_len
= getdigits(&stropt
) - 1;
15664 strval
= get_tv_string_chk(&argvars
[1]);
15665 if (strval
!= NULL
)
15666 write_reg_contents_ex(regname
, strval
, -1,
15667 append
, yank_type
, block_len
);
15668 rettv
->vval
.v_number
= 0;
15672 * "settabwinvar()" function
15675 f_settabwinvar(argvars
, rettv
)
15679 setwinvar(argvars
, rettv
, 1);
15683 * "setwinvar()" function
15686 f_setwinvar(argvars
, rettv
)
15690 setwinvar(argvars
, rettv
, 0);
15694 * "setwinvar()" and "settabwinvar()" functions
15698 setwinvar(argvars
, rettv
, off
)
15704 #ifdef FEAT_WINDOWS
15705 win_T
*save_curwin
;
15706 tabpage_T
*save_curtab
;
15708 char_u
*varname
, *winvarname
;
15710 char_u nbuf
[NUMBUFLEN
];
15713 if (check_restricted() || check_secure())
15716 #ifdef FEAT_WINDOWS
15718 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
15722 win
= find_win_by_nr(&argvars
[off
], tp
);
15723 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
15724 varp
= &argvars
[off
+ 2];
15726 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15728 #ifdef FEAT_WINDOWS
15729 /* set curwin to be our win, temporarily */
15730 save_curwin
= curwin
;
15731 save_curtab
= curtab
;
15732 goto_tabpage_tp(tp
);
15733 if (!win_valid(win
))
15736 curbuf
= curwin
->w_buffer
;
15739 if (*varname
== '&')
15746 numval
= get_tv_number_chk(varp
, &error
);
15747 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15748 if (!error
&& strval
!= NULL
)
15749 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15753 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15754 if (winvarname
!= NULL
)
15756 STRCPY(winvarname
, "w:");
15757 STRCPY(winvarname
+ 2, varname
);
15758 set_var(winvarname
, varp
, TRUE
);
15759 vim_free(winvarname
);
15763 #ifdef FEAT_WINDOWS
15764 /* Restore current tabpage and window, if still valid (autocomands can
15765 * make them invalid). */
15766 if (valid_tabpage(save_curtab
))
15767 goto_tabpage_tp(save_curtab
);
15768 if (win_valid(save_curwin
))
15770 curwin
= save_curwin
;
15771 curbuf
= curwin
->w_buffer
;
15778 * "shellescape({string})" function
15781 f_shellescape(argvars
, rettv
)
15785 rettv
->vval
.v_string
= vim_strsave_shellescape(
15786 get_tv_string(&argvars
[0]), non_zero_arg(&argvars
[1]));
15787 rettv
->v_type
= VAR_STRING
;
15791 * "simplify()" function
15794 f_simplify(argvars
, rettv
)
15800 p
= get_tv_string(&argvars
[0]);
15801 rettv
->vval
.v_string
= vim_strsave(p
);
15802 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
15803 rettv
->v_type
= VAR_STRING
;
15811 f_sin(argvars
, rettv
)
15817 rettv
->v_type
= VAR_FLOAT
;
15818 if (get_float_arg(argvars
, &f
) == OK
)
15819 rettv
->vval
.v_float
= sin(f
);
15821 rettv
->vval
.v_float
= 0.0;
15826 #ifdef __BORLANDC__
15829 item_compare
__ARGS((const void *s1
, const void *s2
));
15831 #ifdef __BORLANDC__
15834 item_compare2
__ARGS((const void *s1
, const void *s2
));
15836 static int item_compare_ic
;
15837 static char_u
*item_compare_func
;
15838 static int item_compare_func_err
;
15839 #define ITEM_COMPARE_FAIL 999
15842 * Compare functions for f_sort() below.
15845 #ifdef __BORLANDC__
15848 item_compare(s1
, s2
)
15853 char_u
*tofree1
, *tofree2
;
15855 char_u numbuf1
[NUMBUFLEN
];
15856 char_u numbuf2
[NUMBUFLEN
];
15858 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
15859 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
15864 if (item_compare_ic
)
15865 res
= STRICMP(p1
, p2
);
15867 res
= STRCMP(p1
, p2
);
15874 #ifdef __BORLANDC__
15877 item_compare2(s1
, s2
)
15886 /* shortcut after failure in previous call; compare all items equal */
15887 if (item_compare_func_err
)
15890 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15891 * in the copy without changing the original list items. */
15892 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
15893 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
15895 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
15896 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
15897 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
15898 clear_tv(&argv
[0]);
15899 clear_tv(&argv
[1]);
15902 res
= ITEM_COMPARE_FAIL
;
15904 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
15905 if (item_compare_func_err
)
15906 res
= ITEM_COMPARE_FAIL
; /* return value has wrong type */
15912 * "sort({list})" function
15915 f_sort(argvars
, rettv
)
15925 if (argvars
[0].v_type
!= VAR_LIST
)
15926 EMSG2(_(e_listarg
), "sort()");
15929 l
= argvars
[0].vval
.v_list
;
15930 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
15932 rettv
->vval
.v_list
= l
;
15933 rettv
->v_type
= VAR_LIST
;
15938 return; /* short list sorts pretty quickly */
15940 item_compare_ic
= FALSE
;
15941 item_compare_func
= NULL
;
15942 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15944 if (argvars
[1].v_type
== VAR_FUNC
)
15945 item_compare_func
= argvars
[1].vval
.v_string
;
15950 i
= get_tv_number_chk(&argvars
[1], &error
);
15952 return; /* type error; errmsg already given */
15954 item_compare_ic
= TRUE
;
15956 item_compare_func
= get_tv_string(&argvars
[1]);
15960 /* Make an array with each entry pointing to an item in the List. */
15961 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
15965 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
15968 item_compare_func_err
= FALSE
;
15969 /* test the compare function */
15970 if (item_compare_func
!= NULL
15971 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
15972 == ITEM_COMPARE_FAIL
)
15973 EMSG(_("E702: Sort compare function failed"));
15976 /* Sort the array with item pointers. */
15977 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
15978 item_compare_func
== NULL
? item_compare
: item_compare2
);
15980 if (!item_compare_func_err
)
15982 /* Clear the List and append the items in the sorted order. */
15983 l
->lv_first
= l
->lv_last
= l
->lv_idx_item
= NULL
;
15985 for (i
= 0; i
< len
; ++i
)
15986 list_append(l
, ptrs
[i
]);
15995 * "soundfold({word})" function
15998 f_soundfold(argvars
, rettv
)
16004 rettv
->v_type
= VAR_STRING
;
16005 s
= get_tv_string(&argvars
[0]);
16007 rettv
->vval
.v_string
= eval_soundfold(s
);
16009 rettv
->vval
.v_string
= vim_strsave(s
);
16014 * "spellbadword()" function
16018 f_spellbadword(argvars
, rettv
)
16022 char_u
*word
= (char_u
*)"";
16023 hlf_T attr
= HLF_COUNT
;
16026 if (rettv_list_alloc(rettv
) == FAIL
)
16030 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16032 /* Find the start and length of the badly spelled word. */
16033 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
16035 word
= ml_get_cursor();
16037 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16039 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16044 /* Check the argument for spelling. */
16045 while (*str
!= NUL
)
16047 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
16048 if (attr
!= HLF_COUNT
)
16059 list_append_string(rettv
->vval
.v_list
, word
, len
);
16060 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
16061 attr
== HLF_SPB
? "bad" :
16062 attr
== HLF_SPR
? "rare" :
16063 attr
== HLF_SPL
? "local" :
16064 attr
== HLF_SPC
? "caps" :
16069 * "spellsuggest()" function
16073 f_spellsuggest(argvars
, rettv
)
16079 int typeerr
= FALSE
;
16084 int need_capital
= FALSE
;
16087 if (rettv_list_alloc(rettv
) == FAIL
)
16091 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16093 str
= get_tv_string(&argvars
[0]);
16094 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16096 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
16099 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16101 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
16109 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
16111 for (i
= 0; i
< ga
.ga_len
; ++i
)
16113 str
= ((char_u
**)ga
.ga_data
)[i
];
16115 li
= listitem_alloc();
16120 li
->li_tv
.v_type
= VAR_STRING
;
16121 li
->li_tv
.v_lock
= 0;
16122 li
->li_tv
.vval
.v_string
= str
;
16123 list_append(rettv
->vval
.v_list
, li
);
16132 f_split(argvars
, rettv
)
16138 char_u
*pat
= NULL
;
16139 regmatch_T regmatch
;
16140 char_u patbuf
[NUMBUFLEN
];
16144 int keepempty
= FALSE
;
16145 int typeerr
= FALSE
;
16147 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16149 p_cpo
= (char_u
*)"";
16151 str
= get_tv_string(&argvars
[0]);
16152 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16154 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16157 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16158 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
16160 if (pat
== NULL
|| *pat
== NUL
)
16161 pat
= (char_u
*)"[\\x01- ]\\+";
16163 if (rettv_list_alloc(rettv
) == FAIL
)
16168 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
16169 if (regmatch
.regprog
!= NULL
)
16171 regmatch
.rm_ic
= FALSE
;
16172 while (*str
!= NUL
|| keepempty
)
16175 match
= FALSE
; /* empty item at the end */
16177 match
= vim_regexec_nl(®match
, str
, col
);
16179 end
= regmatch
.startp
[0];
16181 end
= str
+ STRLEN(str
);
16182 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
16183 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
16185 if (list_append_string(rettv
->vval
.v_list
, str
,
16186 (int)(end
- str
)) == FAIL
)
16191 /* Advance to just after the match. */
16192 if (regmatch
.endp
[0] > str
)
16196 /* Don't get stuck at the same match. */
16198 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
16203 str
= regmatch
.endp
[0];
16206 vim_free(regmatch
.regprog
);
16214 * "sqrt()" function
16217 f_sqrt(argvars
, rettv
)
16223 rettv
->v_type
= VAR_FLOAT
;
16224 if (get_float_arg(argvars
, &f
) == OK
)
16225 rettv
->vval
.v_float
= sqrt(f
);
16227 rettv
->vval
.v_float
= 0.0;
16231 * "str2float()" function
16234 f_str2float(argvars
, rettv
)
16238 char_u
*p
= skipwhite(get_tv_string(&argvars
[0]));
16241 p
= skipwhite(p
+ 1);
16242 (void)string2float(p
, &rettv
->vval
.v_float
);
16243 rettv
->v_type
= VAR_FLOAT
;
16248 * "str2nr()" function
16251 f_str2nr(argvars
, rettv
)
16259 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16261 base
= get_tv_number(&argvars
[1]);
16262 if (base
!= 8 && base
!= 10 && base
!= 16)
16269 p
= skipwhite(get_tv_string(&argvars
[0]));
16271 p
= skipwhite(p
+ 1);
16272 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
16273 rettv
->vval
.v_number
= n
;
16276 #ifdef HAVE_STRFTIME
16278 * "strftime({format}[, {time}])" function
16281 f_strftime(argvars
, rettv
)
16285 char_u result_buf
[256];
16286 struct tm
*curtime
;
16290 rettv
->v_type
= VAR_STRING
;
16292 p
= get_tv_string(&argvars
[0]);
16293 if (argvars
[1].v_type
== VAR_UNKNOWN
)
16294 seconds
= time(NULL
);
16296 seconds
= (time_t)get_tv_number(&argvars
[1]);
16297 curtime
= localtime(&seconds
);
16298 /* MSVC returns NULL for an invalid value of seconds. */
16299 if (curtime
== NULL
)
16300 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
16307 conv
.vc_type
= CONV_NONE
;
16308 enc
= enc_locale();
16309 convert_setup(&conv
, p_enc
, enc
);
16310 if (conv
.vc_type
!= CONV_NONE
)
16311 p
= string_convert(&conv
, p
, NULL
);
16314 (void)strftime((char *)result_buf
, sizeof(result_buf
),
16315 (char *)p
, curtime
);
16317 result_buf
[0] = NUL
;
16320 if (conv
.vc_type
!= CONV_NONE
)
16322 convert_setup(&conv
, enc
, p_enc
);
16323 if (conv
.vc_type
!= CONV_NONE
)
16324 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
16327 rettv
->vval
.v_string
= vim_strsave(result_buf
);
16330 /* Release conversion descriptors */
16331 convert_setup(&conv
, NULL
, NULL
);
16339 * "stridx()" function
16342 f_stridx(argvars
, rettv
)
16346 char_u buf
[NUMBUFLEN
];
16349 char_u
*save_haystack
;
16353 needle
= get_tv_string_chk(&argvars
[1]);
16354 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16355 rettv
->vval
.v_number
= -1;
16356 if (needle
== NULL
|| haystack
== NULL
)
16357 return; /* type error; errmsg already given */
16359 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16363 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
16364 if (error
|| start_idx
>= (int)STRLEN(haystack
))
16366 if (start_idx
>= 0)
16367 haystack
+= start_idx
;
16370 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
16372 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
16376 * "string()" function
16379 f_string(argvars
, rettv
)
16384 char_u numbuf
[NUMBUFLEN
];
16386 rettv
->v_type
= VAR_STRING
;
16387 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
16388 /* Make a copy if we have a value but it's not in allocated memory. */
16389 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
16390 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
16394 * "strlen()" function
16397 f_strlen(argvars
, rettv
)
16401 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
16402 get_tv_string(&argvars
[0])));
16406 * "strpart()" function
16409 f_strpart(argvars
, rettv
)
16419 p
= get_tv_string(&argvars
[0]);
16420 slen
= (int)STRLEN(p
);
16422 n
= get_tv_number_chk(&argvars
[1], &error
);
16425 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16426 len
= get_tv_number(&argvars
[2]);
16428 len
= slen
- n
; /* default len: all bytes that are available. */
16431 * Only return the overlap between the specified part and the actual
16443 else if (n
+ len
> slen
)
16446 rettv
->v_type
= VAR_STRING
;
16447 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
16451 * "strridx()" function
16454 f_strridx(argvars
, rettv
)
16458 char_u buf
[NUMBUFLEN
];
16462 char_u
*lastmatch
= NULL
;
16463 int haystack_len
, end_idx
;
16465 needle
= get_tv_string_chk(&argvars
[1]);
16466 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16468 rettv
->vval
.v_number
= -1;
16469 if (needle
== NULL
|| haystack
== NULL
)
16470 return; /* type error; errmsg already given */
16472 haystack_len
= (int)STRLEN(haystack
);
16473 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16475 /* Third argument: upper limit for index */
16476 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
16478 return; /* can never find a match */
16481 end_idx
= haystack_len
;
16483 if (*needle
== NUL
)
16485 /* Empty string matches past the end. */
16486 lastmatch
= haystack
+ end_idx
;
16490 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
16492 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
16493 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
16499 if (lastmatch
== NULL
)
16500 rettv
->vval
.v_number
= -1;
16502 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
16506 * "strtrans()" function
16509 f_strtrans(argvars
, rettv
)
16513 rettv
->v_type
= VAR_STRING
;
16514 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
16518 * "submatch()" function
16521 f_submatch(argvars
, rettv
)
16525 rettv
->v_type
= VAR_STRING
;
16526 rettv
->vval
.v_string
=
16527 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
16531 * "substitute()" function
16534 f_substitute(argvars
, rettv
)
16538 char_u patbuf
[NUMBUFLEN
];
16539 char_u subbuf
[NUMBUFLEN
];
16540 char_u flagsbuf
[NUMBUFLEN
];
16542 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16543 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16544 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
16545 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
16547 rettv
->v_type
= VAR_STRING
;
16548 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
16549 rettv
->vval
.v_string
= NULL
;
16551 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
16555 * "synID(lnum, col, trans)" function
16559 f_synID(argvars
, rettv
)
16568 int transerr
= FALSE
;
16570 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16571 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16572 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
16574 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16575 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
16576 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
16579 rettv
->vval
.v_number
= id
;
16583 * "synIDattr(id, what [, mode])" function
16587 f_synIDattr(argvars
, rettv
)
16596 char_u modebuf
[NUMBUFLEN
];
16599 id
= get_tv_number(&argvars
[0]);
16600 what
= get_tv_string(&argvars
[1]);
16601 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16603 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
16604 modec
= TOLOWER_ASC(mode
[0]);
16605 if (modec
!= 't' && modec
!= 'c'
16610 modec
= 0; /* replace invalid with current */
16626 switch (TOLOWER_ASC(what
[0]))
16629 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
16630 p
= highlight_color(id
, what
, modec
);
16632 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
16635 case 'f': /* fg[#] */
16636 p
= highlight_color(id
, what
, modec
);
16640 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
16641 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16643 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
16646 case 'n': /* name */
16647 p
= get_highlight_name(NULL
, id
- 1);
16650 case 'r': /* reverse */
16651 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16655 if (TOLOWER_ASC(what
[1]) == 'p') /* sp[#] */
16656 p
= highlight_color(id
, what
, modec
);
16657 else /* standout */
16658 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
16662 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
16664 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
16667 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
16672 p
= vim_strsave(p
);
16674 rettv
->v_type
= VAR_STRING
;
16675 rettv
->vval
.v_string
= p
;
16679 * "synIDtrans(id)" function
16683 f_synIDtrans(argvars
, rettv
)
16690 id
= get_tv_number(&argvars
[0]);
16693 id
= syn_get_final_id(id
);
16698 rettv
->vval
.v_number
= id
;
16702 * "synstack(lnum, col)" function
16706 f_synstack(argvars
, rettv
)
16717 rettv
->v_type
= VAR_LIST
;
16718 rettv
->vval
.v_list
= NULL
;
16721 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16722 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16724 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16725 && col
>= 0 && (col
== 0 || col
< (long)STRLEN(ml_get(lnum
)))
16726 && rettv_list_alloc(rettv
) != FAIL
)
16728 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
16731 id
= syn_get_stack_item(i
);
16734 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
16742 * "system()" function
16745 f_system(argvars
, rettv
)
16749 char_u
*res
= NULL
;
16751 char_u
*infile
= NULL
;
16752 char_u buf
[NUMBUFLEN
];
16756 if (check_restricted() || check_secure())
16759 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16762 * Write the string to a temp file, to be used for input of the shell
16765 if ((infile
= vim_tempname('i')) == NULL
)
16771 fd
= mch_fopen((char *)infile
, WRITEBIN
);
16774 EMSG2(_(e_notopen
), infile
);
16777 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
16781 goto done
; /* type error; errmsg already given */
16783 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
16785 if (fclose(fd
) != 0)
16789 EMSG(_("E677: Error writing temp file"));
16794 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
16795 SHELL_SILENT
| SHELL_COOKED
);
16798 /* translate <CR> into <NL> */
16803 for (s
= res
; *s
; ++s
)
16811 /* translate <CR><NL> into <NL> */
16817 for (s
= res
; *s
; ++s
)
16819 if (s
[0] == CAR
&& s
[1] == NL
)
16829 if (infile
!= NULL
)
16831 mch_remove(infile
);
16834 rettv
->v_type
= VAR_STRING
;
16835 rettv
->vval
.v_string
= res
;
16839 * "tabpagebuflist()" function
16843 f_tabpagebuflist(argvars
, rettv
)
16847 #ifdef FEAT_WINDOWS
16851 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16855 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16857 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16859 if (wp
!= NULL
&& rettv_list_alloc(rettv
) != FAIL
)
16861 for (; wp
!= NULL
; wp
= wp
->w_next
)
16862 if (list_append_number(rettv
->vval
.v_list
,
16863 wp
->w_buffer
->b_fnum
) == FAIL
)
16871 * "tabpagenr()" function
16875 f_tabpagenr(argvars
, rettv
)
16880 #ifdef FEAT_WINDOWS
16883 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
16885 arg
= get_tv_string_chk(&argvars
[0]);
16889 if (STRCMP(arg
, "$") == 0)
16890 nr
= tabpage_index(NULL
) - 1;
16892 EMSG2(_(e_invexpr2
), arg
);
16896 nr
= tabpage_index(curtab
);
16898 rettv
->vval
.v_number
= nr
;
16902 #ifdef FEAT_WINDOWS
16903 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
16906 * Common code for tabpagewinnr() and winnr().
16909 get_winnr(tp
, argvar
)
16918 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
16919 if (argvar
->v_type
!= VAR_UNKNOWN
)
16921 arg
= get_tv_string_chk(argvar
);
16923 nr
= 0; /* type error; errmsg already given */
16924 else if (STRCMP(arg
, "$") == 0)
16925 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
16926 else if (STRCMP(arg
, "#") == 0)
16928 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
16934 EMSG2(_(e_invexpr2
), arg
);
16940 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16941 wp
!= twin
; wp
= wp
->w_next
)
16945 /* didn't find it in this tabpage */
16956 * "tabpagewinnr()" function
16960 f_tabpagewinnr(argvars
, rettv
)
16965 #ifdef FEAT_WINDOWS
16968 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16972 nr
= get_winnr(tp
, &argvars
[1]);
16974 rettv
->vval
.v_number
= nr
;
16979 * "tagfiles()" function
16983 f_tagfiles(argvars
, rettv
)
16987 char_u fname
[MAXPATHL
+ 1];
16991 if (rettv_list_alloc(rettv
) == FAIL
)
16994 for (first
= TRUE
; ; first
= FALSE
)
16995 if (get_tagfname(&tn
, first
, fname
) == FAIL
16996 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
17002 * "taglist()" function
17005 f_taglist(argvars
, rettv
)
17009 char_u
*tag_pattern
;
17011 tag_pattern
= get_tv_string(&argvars
[0]);
17013 rettv
->vval
.v_number
= FALSE
;
17014 if (*tag_pattern
== NUL
)
17017 if (rettv_list_alloc(rettv
) == OK
)
17018 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
17022 * "tempname()" function
17026 f_tempname(argvars
, rettv
)
17030 static int x
= 'A';
17032 rettv
->v_type
= VAR_STRING
;
17033 rettv
->vval
.v_string
= vim_tempname(x
);
17035 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17036 * names. Skip 'I' and 'O', they are used for shell redirection. */
17054 } while (x
== 'I' || x
== 'O');
17058 * "test(list)" function: Just checking the walls...
17062 f_test(argvars
, rettv
)
17066 /* Used for unit testing. Change the code below to your liking. */
17070 char_u
*bad
, *good
;
17072 if (argvars
[0].v_type
!= VAR_LIST
)
17074 l
= argvars
[0].vval
.v_list
;
17080 bad
= get_tv_string(&li
->li_tv
);
17084 good
= get_tv_string(&li
->li_tv
);
17085 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
17090 * "tolower(string)" function
17093 f_tolower(argvars
, rettv
)
17099 p
= vim_strsave(get_tv_string(&argvars
[0]));
17100 rettv
->v_type
= VAR_STRING
;
17101 rettv
->vval
.v_string
= p
;
17113 c
= utf_ptr2char(p
);
17114 lc
= utf_tolower(c
);
17115 l
= utf_ptr2len(p
);
17116 /* TODO: reallocate string when byte count changes. */
17117 if (utf_char2len(lc
) == l
)
17118 utf_char2bytes(lc
, p
);
17121 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
17122 p
+= l
; /* skip multi-byte character */
17126 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
17133 * "toupper(string)" function
17136 f_toupper(argvars
, rettv
)
17140 rettv
->v_type
= VAR_STRING
;
17141 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
17145 * "tr(string, fromstr, tostr)" function
17148 f_tr(argvars
, rettv
)
17165 char_u buf
[NUMBUFLEN
];
17166 char_u buf2
[NUMBUFLEN
];
17169 instr
= get_tv_string(&argvars
[0]);
17170 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
17171 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
17173 /* Default return value: empty string. */
17174 rettv
->v_type
= VAR_STRING
;
17175 rettv
->vval
.v_string
= NULL
;
17176 if (fromstr
== NULL
|| tostr
== NULL
)
17177 return; /* type error; errmsg already given */
17178 ga_init2(&ga
, (int)sizeof(char), 80);
17183 /* not multi-byte: fromstr and tostr must be the same length */
17184 if (STRLEN(fromstr
) != STRLEN(tostr
))
17189 EMSG2(_(e_invarg2
), fromstr
);
17194 /* fromstr and tostr have to contain the same number of chars */
17195 while (*instr
!= NUL
)
17200 inlen
= (*mb_ptr2len
)(instr
);
17204 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
17206 fromlen
= (*mb_ptr2len
)(p
);
17207 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
17209 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17211 tolen
= (*mb_ptr2len
)(p
);
17219 if (*p
== NUL
) /* tostr is shorter than fromstr */
17226 if (first
&& cpstr
== instr
)
17228 /* Check that fromstr and tostr have the same number of
17229 * (multi-byte) characters. Done only once when a character
17230 * of instr doesn't appear in fromstr. */
17232 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17234 tolen
= (*mb_ptr2len
)(p
);
17241 ga_grow(&ga
, cplen
);
17242 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
17243 ga
.ga_len
+= cplen
;
17250 /* When not using multi-byte chars we can do it faster. */
17251 p
= vim_strchr(fromstr
, *instr
);
17253 ga_append(&ga
, tostr
[p
- fromstr
]);
17255 ga_append(&ga
, *instr
);
17260 /* add a terminating NUL */
17262 ga_append(&ga
, NUL
);
17264 rettv
->vval
.v_string
= ga
.ga_data
;
17269 * "trunc({float})" function
17272 f_trunc(argvars
, rettv
)
17278 rettv
->v_type
= VAR_FLOAT
;
17279 if (get_float_arg(argvars
, &f
) == OK
)
17280 /* trunc() is not in C90, use floor() or ceil() instead. */
17281 rettv
->vval
.v_float
= f
> 0 ? floor(f
) : ceil(f
);
17283 rettv
->vval
.v_float
= 0.0;
17288 * "type(expr)" function
17291 f_type(argvars
, rettv
)
17297 switch (argvars
[0].v_type
)
17299 case VAR_NUMBER
: n
= 0; break;
17300 case VAR_STRING
: n
= 1; break;
17301 case VAR_FUNC
: n
= 2; break;
17302 case VAR_LIST
: n
= 3; break;
17303 case VAR_DICT
: n
= 4; break;
17305 case VAR_FLOAT
: n
= 5; break;
17307 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
17309 rettv
->vval
.v_number
= n
;
17313 * "values(dict)" function
17316 f_values(argvars
, rettv
)
17320 dict_list(argvars
, rettv
, 1);
17324 * "virtcol(string)" function
17327 f_virtcol(argvars
, rettv
)
17333 int fnum
= curbuf
->b_fnum
;
17335 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
17336 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
17337 && fnum
== curbuf
->b_fnum
)
17339 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
17343 rettv
->vval
.v_number
= vcol
;
17347 * "visualmode()" function
17351 f_visualmode(argvars
, rettv
)
17358 rettv
->v_type
= VAR_STRING
;
17359 str
[0] = curbuf
->b_visual_mode_eval
;
17361 rettv
->vval
.v_string
= vim_strsave(str
);
17363 /* A non-zero number or non-empty string argument: reset mode. */
17364 if (non_zero_arg(&argvars
[0]))
17365 curbuf
->b_visual_mode_eval
= NUL
;
17370 * "winbufnr(nr)" function
17373 f_winbufnr(argvars
, rettv
)
17379 wp
= find_win_by_nr(&argvars
[0], NULL
);
17381 rettv
->vval
.v_number
= -1;
17383 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
17387 * "wincol()" function
17391 f_wincol(argvars
, rettv
)
17396 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
17400 * "winheight(nr)" function
17403 f_winheight(argvars
, rettv
)
17409 wp
= find_win_by_nr(&argvars
[0], NULL
);
17411 rettv
->vval
.v_number
= -1;
17413 rettv
->vval
.v_number
= wp
->w_height
;
17417 * "winline()" function
17421 f_winline(argvars
, rettv
)
17426 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
17430 * "winnr()" function
17434 f_winnr(argvars
, rettv
)
17440 #ifdef FEAT_WINDOWS
17441 nr
= get_winnr(curtab
, &argvars
[0]);
17443 rettv
->vval
.v_number
= nr
;
17447 * "winrestcmd()" function
17451 f_winrestcmd(argvars
, rettv
)
17455 #ifdef FEAT_WINDOWS
17461 ga_init2(&ga
, (int)sizeof(char), 70);
17462 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
17464 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
17465 ga_concat(&ga
, buf
);
17466 # ifdef FEAT_VERTSPLIT
17467 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
17468 ga_concat(&ga
, buf
);
17472 ga_append(&ga
, NUL
);
17474 rettv
->vval
.v_string
= ga
.ga_data
;
17476 rettv
->vval
.v_string
= NULL
;
17478 rettv
->v_type
= VAR_STRING
;
17482 * "winrestview()" function
17486 f_winrestview(argvars
, rettv
)
17492 if (argvars
[0].v_type
!= VAR_DICT
17493 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
17497 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
17498 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
17499 #ifdef FEAT_VIRTUALEDIT
17500 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
17502 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
17503 curwin
->w_set_curswant
= FALSE
;
17505 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
17507 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
17509 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
17510 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
17513 changed_cline_bef_curs();
17514 invalidate_botline();
17515 redraw_later(VALID
);
17517 if (curwin
->w_topline
== 0)
17518 curwin
->w_topline
= 1;
17519 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
17520 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
17522 check_topfill(curwin
, TRUE
);
17528 * "winsaveview()" function
17532 f_winsaveview(argvars
, rettv
)
17538 dict
= dict_alloc();
17541 rettv
->v_type
= VAR_DICT
;
17542 rettv
->vval
.v_dict
= dict
;
17543 ++dict
->dv_refcount
;
17545 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
17546 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
17547 #ifdef FEAT_VIRTUALEDIT
17548 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
17551 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
17553 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
17555 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
17557 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
17558 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
17562 * "winwidth(nr)" function
17565 f_winwidth(argvars
, rettv
)
17571 wp
= find_win_by_nr(&argvars
[0], NULL
);
17573 rettv
->vval
.v_number
= -1;
17575 #ifdef FEAT_VERTSPLIT
17576 rettv
->vval
.v_number
= wp
->w_width
;
17578 rettv
->vval
.v_number
= Columns
;
17583 * "writefile()" function
17586 f_writefile(argvars
, rettv
)
17590 int binary
= FALSE
;
17598 if (check_restricted() || check_secure())
17601 if (argvars
[0].v_type
!= VAR_LIST
)
17603 EMSG2(_(e_listarg
), "writefile()");
17606 if (argvars
[0].vval
.v_list
== NULL
)
17609 if (argvars
[2].v_type
!= VAR_UNKNOWN
17610 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
17613 /* Always open the file in binary mode, library functions have a mind of
17614 * their own about CR-LF conversion. */
17615 fname
= get_tv_string(&argvars
[1]);
17616 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
17618 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
17623 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
17626 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
17638 if (!binary
|| li
->li_next
!= NULL
)
17639 if (putc('\n', fd
) == EOF
)
17653 rettv
->vval
.v_number
= ret
;
17657 * Translate a String variable into a position.
17658 * Returns NULL when there is an error.
17661 var2fpos(varp
, dollar_lnum
, fnum
)
17663 int dollar_lnum
; /* TRUE when $ is last line */
17664 int *fnum
; /* set to fnum for '0, 'A, etc. */
17670 /* Argument can be [lnum, col, coladd]. */
17671 if (varp
->v_type
== VAR_LIST
)
17678 l
= varp
->vval
.v_list
;
17682 /* Get the line number */
17683 pos
.lnum
= list_find_nr(l
, 0L, &error
);
17684 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
17685 return NULL
; /* invalid line number */
17687 /* Get the column number */
17688 pos
.col
= list_find_nr(l
, 1L, &error
);
17691 len
= (long)STRLEN(ml_get(pos
.lnum
));
17693 /* We accept "$" for the column number: last column. */
17694 li
= list_find(l
, 1L);
17695 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
17696 && li
->li_tv
.vval
.v_string
!= NULL
17697 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
17700 /* Accept a position up to the NUL after the line. */
17701 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
17702 return NULL
; /* invalid column number */
17705 #ifdef FEAT_VIRTUALEDIT
17706 /* Get the virtual offset. Defaults to zero. */
17707 pos
.coladd
= list_find_nr(l
, 2L, &error
);
17715 name
= get_tv_string_chk(varp
);
17718 if (name
[0] == '.') /* cursor */
17719 return &curwin
->w_cursor
;
17721 if (name
[0] == 'v' && name
[1] == NUL
) /* Visual start */
17725 return &curwin
->w_cursor
;
17728 if (name
[0] == '\'') /* mark */
17730 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
17731 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
17736 #ifdef FEAT_VIRTUALEDIT
17740 if (name
[0] == 'w' && dollar_lnum
)
17743 if (name
[1] == '0') /* "w0": first visible line */
17746 pos
.lnum
= curwin
->w_topline
;
17749 else if (name
[1] == '$') /* "w$": last visible line */
17751 validate_botline();
17752 pos
.lnum
= curwin
->w_botline
- 1;
17756 else if (name
[0] == '$') /* last column or line */
17760 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
17765 pos
.lnum
= curwin
->w_cursor
.lnum
;
17766 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
17774 * Convert list in "arg" into a position and optional file number.
17775 * When "fnump" is NULL there is no file number, only 3 items.
17776 * Note that the column is passed on as-is, the caller may want to decrement
17777 * it to use 1 for the first column.
17778 * Return FAIL when conversion is not possible, doesn't check the position for
17782 list2fpos(arg
, posp
, fnump
)
17787 list_T
*l
= arg
->vval
.v_list
;
17791 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17792 * when "fnump" isn't NULL and "coladd" is optional. */
17793 if (arg
->v_type
!= VAR_LIST
17795 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
17796 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
17801 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
17805 n
= curbuf
->b_fnum
; /* current buffer */
17809 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
17814 n
= list_find_nr(l
, i
++, NULL
); /* col */
17819 #ifdef FEAT_VIRTUALEDIT
17820 n
= list_find_nr(l
, i
, NULL
);
17831 * Get the length of an environment variable name.
17832 * Advance "arg" to the first character after the name.
17833 * Return 0 for error.
17842 for (p
= *arg
; vim_isIDc(*p
); ++p
)
17844 if (p
== *arg
) /* no name found */
17847 len
= (int)(p
- *arg
);
17853 * Get the length of the name of a function or internal variable.
17854 * "arg" is advanced to the first non-white character after the name.
17855 * Return 0 if something is wrong.
17864 /* Find the end of the name. */
17865 for (p
= *arg
; eval_isnamec(*p
); ++p
)
17867 if (p
== *arg
) /* no name found */
17870 len
= (int)(p
- *arg
);
17871 *arg
= skipwhite(p
);
17877 * Get the length of the name of a variable or function.
17878 * Only the name is recognized, does not handle ".key" or "[idx]".
17879 * "arg" is advanced to the first non-white character after the name.
17880 * Return -1 if curly braces expansion failed.
17881 * Return 0 if something else is wrong.
17882 * If the name contains 'magic' {}'s, expand them and return the
17883 * expanded name in an allocated string via 'alias' - caller must free.
17886 get_name_len(arg
, alias
, evaluate
, verbose
)
17894 char_u
*expr_start
;
17897 *alias
= NULL
; /* default to no alias */
17899 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
17900 && (*arg
)[2] == (int)KE_SNR
)
17902 /* hard coded <SNR>, already translated */
17904 return get_id_len(arg
) + 3;
17906 len
= eval_fname_script(*arg
);
17909 /* literal "<SID>", "s:" or "<SNR>" */
17914 * Find the end of the name; check for {} construction.
17916 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
17917 len
> 0 ? 0 : FNE_CHECK_START
);
17918 if (expr_start
!= NULL
)
17920 char_u
*temp_string
;
17924 len
+= (int)(p
- *arg
);
17925 *arg
= skipwhite(p
);
17930 * Include any <SID> etc in the expanded string:
17931 * Thus the -len here.
17933 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
17934 if (temp_string
== NULL
)
17936 *alias
= temp_string
;
17937 *arg
= skipwhite(p
);
17938 return (int)STRLEN(temp_string
);
17941 len
+= get_id_len(arg
);
17942 if (len
== 0 && verbose
)
17943 EMSG2(_(e_invexpr2
), *arg
);
17949 * Find the end of a variable or function name, taking care of magic braces.
17950 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17951 * start and end of the first magic braces item.
17952 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
17953 * Return a pointer to just after the name. Equal to "arg" if there is no
17957 find_name_end(arg
, expr_start
, expr_end
, flags
)
17959 char_u
**expr_start
;
17967 if (expr_start
!= NULL
)
17969 *expr_start
= NULL
;
17973 /* Quick check for valid starting character. */
17974 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
17977 for (p
= arg
; *p
!= NUL
17978 && (eval_isnamec(*p
)
17980 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
17982 || br_nest
!= 0); mb_ptr_adv(p
))
17986 /* skip over 'string' to avoid counting [ and ] inside it. */
17987 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
17992 else if (*p
== '"')
17994 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17995 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
17996 if (*p
== '\\' && p
[1] != NUL
)
18006 else if (*p
== ']')
18015 if (expr_start
!= NULL
&& *expr_start
== NULL
)
18018 else if (*p
== '}')
18021 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
18031 * Expands out the 'magic' {}'s in a variable/function name.
18032 * Note that this can call itself recursively, to deal with
18033 * constructs like foo{bar}{baz}{bam}
18034 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18040 * Returns a new allocated string, which the caller must free.
18041 * Returns NULL for failure.
18044 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
18046 char_u
*expr_start
;
18051 char_u
*retval
= NULL
;
18052 char_u
*temp_result
;
18053 char_u
*nextcmd
= NULL
;
18055 if (expr_end
== NULL
|| in_end
== NULL
)
18062 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
18063 if (temp_result
!= NULL
&& nextcmd
== NULL
)
18065 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
18066 + (in_end
- expr_end
) + 1));
18067 if (retval
!= NULL
)
18069 STRCPY(retval
, in_start
);
18070 STRCAT(retval
, temp_result
);
18071 STRCAT(retval
, expr_end
+ 1);
18074 vim_free(temp_result
);
18076 *in_end
= c1
; /* put char back for error messages */
18080 if (retval
!= NULL
)
18082 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
18083 if (expr_start
!= NULL
)
18085 /* Further expansion! */
18086 temp_result
= make_expanded_name(retval
, expr_start
,
18087 expr_end
, temp_result
);
18089 retval
= temp_result
;
18097 * Return TRUE if character "c" can be used in a variable or function name.
18098 * Does not include '{' or '}' for magic braces.
18104 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
18108 * Return TRUE if character "c" can be used as the first character in a
18109 * variable or function name (excluding '{' and '}').
18115 return (ASCII_ISALPHA(c
) || c
== '_');
18119 * Set number v: variable to "val".
18122 set_vim_var_nr(idx
, val
)
18126 vimvars
[idx
].vv_nr
= val
;
18130 * Get number v: variable value.
18133 get_vim_var_nr(idx
)
18136 return vimvars
[idx
].vv_nr
;
18140 * Get string v: variable value. Uses a static buffer, can only be used once.
18143 get_vim_var_str(idx
)
18146 return get_tv_string(&vimvars
[idx
].vv_tv
);
18150 * Get List v: variable value. Caller must take care of reference count when
18154 get_vim_var_list(idx
)
18157 return vimvars
[idx
].vv_list
;
18161 * Set v:count to "count" and v:count1 to "count1".
18162 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
18165 set_vcount(count
, count1
, set_prevcount
)
18171 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
18172 vimvars
[VV_COUNT
].vv_nr
= count
;
18173 vimvars
[VV_COUNT1
].vv_nr
= count1
;
18177 * Set string v: variable to a copy of "val".
18180 set_vim_var_string(idx
, val
, len
)
18183 int len
; /* length of "val" to use or -1 (whole string) */
18185 /* Need to do this (at least) once, since we can't initialize a union.
18186 * Will always be invoked when "v:progname" is set. */
18187 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
18189 vim_free(vimvars
[idx
].vv_str
);
18191 vimvars
[idx
].vv_str
= NULL
;
18192 else if (len
== -1)
18193 vimvars
[idx
].vv_str
= vim_strsave(val
);
18195 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
18199 * Set List v: variable to "val".
18202 set_vim_var_list(idx
, val
)
18206 list_unref(vimvars
[idx
].vv_list
);
18207 vimvars
[idx
].vv_list
= val
;
18209 ++val
->lv_refcount
;
18213 * Set v:register if needed.
18221 if (c
== 0 || c
== ' ')
18225 /* Avoid free/alloc when the value is already right. */
18226 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
18227 set_vim_var_string(VV_REG
, ®name
, 1);
18231 * Get or set v:exception. If "oldval" == NULL, return the current value.
18232 * Otherwise, restore the value to "oldval" and return NULL.
18233 * Must always be called in pairs to save and restore v:exception! Does not
18234 * take care of memory allocations.
18237 v_exception(oldval
)
18240 if (oldval
== NULL
)
18241 return vimvars
[VV_EXCEPTION
].vv_str
;
18243 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
18248 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18249 * Otherwise, restore the value to "oldval" and return NULL.
18250 * Must always be called in pairs to save and restore v:throwpoint! Does not
18251 * take care of memory allocations.
18254 v_throwpoint(oldval
)
18257 if (oldval
== NULL
)
18258 return vimvars
[VV_THROWPOINT
].vv_str
;
18260 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
18264 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18267 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18268 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18269 * Must always be called in pairs!
18272 set_cmdarg(eap
, oldarg
)
18280 oldval
= vimvars
[VV_CMDARG
].vv_str
;
18284 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
18288 if (eap
->force_bin
== FORCE_BIN
)
18290 else if (eap
->force_bin
== FORCE_NOBIN
)
18295 if (eap
->read_edit
)
18298 if (eap
->force_ff
!= 0)
18299 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
18301 if (eap
->force_enc
!= 0)
18302 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
18303 if (eap
->bad_char
!= 0)
18304 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
18307 newval
= alloc(len
+ 1);
18308 if (newval
== NULL
)
18311 if (eap
->force_bin
== FORCE_BIN
)
18312 sprintf((char *)newval
, " ++bin");
18313 else if (eap
->force_bin
== FORCE_NOBIN
)
18314 sprintf((char *)newval
, " ++nobin");
18318 if (eap
->read_edit
)
18319 STRCAT(newval
, " ++edit");
18321 if (eap
->force_ff
!= 0)
18322 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
18323 eap
->cmd
+ eap
->force_ff
);
18325 if (eap
->force_enc
!= 0)
18326 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
18327 eap
->cmd
+ eap
->force_enc
);
18328 if (eap
->bad_char
!= 0)
18329 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
18330 eap
->cmd
+ eap
->bad_char
);
18332 vimvars
[VV_CMDARG
].vv_str
= newval
;
18338 * Get the value of internal variable "name".
18339 * Return OK or FAIL.
18342 get_var_tv(name
, len
, rettv
, verbose
)
18344 int len
; /* length of "name" */
18345 typval_T
*rettv
; /* NULL when only checking existence */
18346 int verbose
; /* may give error message */
18349 typval_T
*tv
= NULL
;
18354 /* truncate the name, so that we can use strcmp() */
18359 * Check for "b:changedtick".
18361 if (STRCMP(name
, "b:changedtick") == 0)
18363 atv
.v_type
= VAR_NUMBER
;
18364 atv
.vval
.v_number
= curbuf
->b_changedtick
;
18369 * Check for user-defined variables.
18373 v
= find_var(name
, NULL
);
18380 if (rettv
!= NULL
&& verbose
)
18381 EMSG2(_(e_undefvar
), name
);
18384 else if (rettv
!= NULL
)
18385 copy_tv(tv
, rettv
);
18393 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18394 * Also handle function call with Funcref variable: func(expr)
18395 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18398 handle_subscript(arg
, rettv
, evaluate
, verbose
)
18401 int evaluate
; /* do more than finding the end */
18402 int verbose
; /* give error messages */
18405 dict_T
*selfdict
= NULL
;
18412 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
18413 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
18414 && !vim_iswhite(*(*arg
- 1)))
18418 /* need to copy the funcref so that we can clear rettv */
18420 rettv
->v_type
= VAR_UNKNOWN
;
18422 /* Invoke the function. Recursive! */
18423 s
= functv
.vval
.v_string
;
18424 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
18425 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
18426 &len
, evaluate
, selfdict
);
18428 /* Clear the funcref afterwards, so that deleting it while
18429 * evaluating the arguments is possible (see test55). */
18432 /* Stop the expression evaluation when immediately aborting on
18433 * error, or when an interrupt occurred or an exception was thrown
18434 * but not caught. */
18441 dict_unref(selfdict
);
18444 else /* **arg == '[' || **arg == '.' */
18446 dict_unref(selfdict
);
18447 if (rettv
->v_type
== VAR_DICT
)
18449 selfdict
= rettv
->vval
.v_dict
;
18450 if (selfdict
!= NULL
)
18451 ++selfdict
->dv_refcount
;
18455 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
18462 dict_unref(selfdict
);
18467 * Allocate memory for a variable type-value, and make it empty (0 or NULL
18473 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
18477 * Allocate memory for a variable type-value, and assign a string to it.
18478 * The string "s" must have been allocated, it is consumed.
18479 * Return NULL for out of memory, the variable otherwise.
18487 rettv
= alloc_tv();
18490 rettv
->v_type
= VAR_STRING
;
18491 rettv
->vval
.v_string
= s
;
18499 * Free the memory for a variable type-value.
18507 switch (varp
->v_type
)
18510 func_unref(varp
->vval
.v_string
);
18513 vim_free(varp
->vval
.v_string
);
18516 list_unref(varp
->vval
.v_list
);
18519 dict_unref(varp
->vval
.v_dict
);
18528 EMSG2(_(e_intern2
), "free_tv()");
18536 * Free the memory for a variable value and set the value to NULL or 0.
18544 switch (varp
->v_type
)
18547 func_unref(varp
->vval
.v_string
);
18550 vim_free(varp
->vval
.v_string
);
18551 varp
->vval
.v_string
= NULL
;
18554 list_unref(varp
->vval
.v_list
);
18555 varp
->vval
.v_list
= NULL
;
18558 dict_unref(varp
->vval
.v_dict
);
18559 varp
->vval
.v_dict
= NULL
;
18562 varp
->vval
.v_number
= 0;
18566 varp
->vval
.v_float
= 0.0;
18572 EMSG2(_(e_intern2
), "clear_tv()");
18579 * Set the value of a variable to NULL without freeing items.
18586 vim_memset(varp
, 0, sizeof(typval_T
));
18590 * Get the number value of a variable.
18591 * If it is a String variable, uses vim_str2nr().
18592 * For incompatible types, return 0.
18593 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18594 * caller of incompatible types: it sets *denote to TRUE if "denote"
18595 * is not NULL or returns -1 otherwise.
18598 get_tv_number(varp
)
18603 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
18607 get_tv_number_chk(varp
, denote
)
18613 switch (varp
->v_type
)
18616 return (long)(varp
->vval
.v_number
);
18619 EMSG(_("E805: Using a Float as a Number"));
18623 EMSG(_("E703: Using a Funcref as a Number"));
18626 if (varp
->vval
.v_string
!= NULL
)
18627 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
18628 TRUE
, TRUE
, &n
, NULL
);
18631 EMSG(_("E745: Using a List as a Number"));
18634 EMSG(_("E728: Using a Dictionary as a Number"));
18637 EMSG2(_(e_intern2
), "get_tv_number()");
18640 if (denote
== NULL
) /* useful for values that must be unsigned */
18648 * Get the lnum from the first argument.
18649 * Also accepts ".", "$", etc., but that only works for the current buffer.
18650 * Returns -1 on error.
18653 get_tv_lnum(argvars
)
18659 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
18660 if (lnum
== 0) /* no valid number, try using line() */
18662 rettv
.v_type
= VAR_NUMBER
;
18663 f_line(argvars
, &rettv
);
18664 lnum
= rettv
.vval
.v_number
;
18671 * Get the lnum from the first argument.
18672 * Also accepts "$", then "buf" is used.
18673 * Returns 0 on error.
18676 get_tv_lnum_buf(argvars
, buf
)
18680 if (argvars
[0].v_type
== VAR_STRING
18681 && argvars
[0].vval
.v_string
!= NULL
18682 && argvars
[0].vval
.v_string
[0] == '$'
18684 return buf
->b_ml
.ml_line_count
;
18685 return get_tv_number_chk(&argvars
[0], NULL
);
18689 * Get the string value of a variable.
18690 * If it is a Number variable, the number is converted into a string.
18691 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18692 * get_tv_string_buf() uses a given buffer.
18693 * If the String variable has never been set, return an empty string.
18694 * Never returns NULL;
18695 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18699 get_tv_string(varp
)
18702 static char_u mybuf
[NUMBUFLEN
];
18704 return get_tv_string_buf(varp
, mybuf
);
18708 get_tv_string_buf(varp
, buf
)
18712 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
18714 return res
!= NULL
? res
: (char_u
*)"";
18718 get_tv_string_chk(varp
)
18721 static char_u mybuf
[NUMBUFLEN
];
18723 return get_tv_string_buf_chk(varp
, mybuf
);
18727 get_tv_string_buf_chk(varp
, buf
)
18731 switch (varp
->v_type
)
18734 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
18737 EMSG(_("E729: using Funcref as a String"));
18740 EMSG(_("E730: using List as a String"));
18743 EMSG(_("E731: using Dictionary as a String"));
18747 EMSG(_("E806: using Float as a String"));
18751 if (varp
->vval
.v_string
!= NULL
)
18752 return varp
->vval
.v_string
;
18753 return (char_u
*)"";
18755 EMSG2(_(e_intern2
), "get_tv_string_buf()");
18762 * Find variable "name" in the list of variables.
18763 * Return a pointer to it if found, NULL if not found.
18764 * Careful: "a:0" variables don't have a name.
18765 * When "htp" is not NULL we are writing to the variable, set "htp" to the
18768 static dictitem_T
*
18769 find_var(name
, htp
)
18776 ht
= find_var_ht(name
, &varname
);
18781 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
18785 * Find variable "varname" in hashtab "ht".
18786 * Returns NULL if not found.
18788 static dictitem_T
*
18789 find_var_in_ht(ht
, varname
, writing
)
18796 if (*varname
== NUL
)
18798 /* Must be something like "s:", otherwise "ht" would be NULL. */
18799 switch (varname
[-2])
18801 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
18802 case 'g': return &globvars_var
;
18803 case 'v': return &vimvars_var
;
18804 case 'b': return &curbuf
->b_bufvar
;
18805 case 'w': return &curwin
->w_winvar
;
18806 #ifdef FEAT_WINDOWS
18807 case 't': return &curtab
->tp_winvar
;
18809 case 'l': return current_funccal
== NULL
18810 ? NULL
: ¤t_funccal
->l_vars_var
;
18811 case 'a': return current_funccal
== NULL
18812 ? NULL
: ¤t_funccal
->l_avars_var
;
18817 hi
= hash_find(ht
, varname
);
18818 if (HASHITEM_EMPTY(hi
))
18820 /* For global variables we may try auto-loading the script. If it
18821 * worked find the variable again. Don't auto-load a script if it was
18822 * loaded already, otherwise it would be loaded every time when
18823 * checking if a function name is a Funcref variable. */
18824 if (ht
== &globvarht
&& !writing
18825 && script_autoload(varname
, FALSE
) && !aborting())
18826 hi
= hash_find(ht
, varname
);
18827 if (HASHITEM_EMPTY(hi
))
18834 * Find the hashtab used for a variable name.
18835 * Set "varname" to the start of name without ':'.
18838 find_var_ht(name
, varname
)
18844 if (name
[1] != ':')
18846 /* The name must not start with a colon or #. */
18847 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
18851 /* "version" is "v:version" in all scopes */
18852 hi
= hash_find(&compat_hashtab
, name
);
18853 if (!HASHITEM_EMPTY(hi
))
18854 return &compat_hashtab
;
18856 if (current_funccal
== NULL
)
18857 return &globvarht
; /* global variable */
18858 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
18860 *varname
= name
+ 2;
18861 if (*name
== 'g') /* global variable */
18863 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18865 if (vim_strchr(name
+ 2, ':') != NULL
18866 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
18868 if (*name
== 'b') /* buffer variable */
18869 return &curbuf
->b_vars
.dv_hashtab
;
18870 if (*name
== 'w') /* window variable */
18871 return &curwin
->w_vars
.dv_hashtab
;
18872 #ifdef FEAT_WINDOWS
18873 if (*name
== 't') /* tab page variable */
18874 return &curtab
->tp_vars
.dv_hashtab
;
18876 if (*name
== 'v') /* v: variable */
18878 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
18879 return ¤t_funccal
->l_avars
.dv_hashtab
;
18880 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
18881 return ¤t_funccal
->l_vars
.dv_hashtab
;
18882 if (*name
== 's' /* script variable */
18883 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
18884 return &SCRIPT_VARS(current_SID
);
18889 * Get the string value of a (global/local) variable.
18890 * Returns NULL when it doesn't exist.
18893 get_var_value(name
)
18898 v
= find_var(name
, NULL
);
18901 return get_tv_string(&v
->di_tv
);
18905 * Allocate a new hashtab for a sourced script. It will be used while
18906 * sourcing this script and when executing functions defined in the script.
18909 new_script_vars(id
)
18916 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
18918 /* Re-allocating ga_data means that an ht_array pointing to
18919 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
18920 * at its init value. Also reset "v_dict", it's always the same. */
18921 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
18923 ht
= &SCRIPT_VARS(i
);
18924 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
18925 ht
->ht_array
= ht
->ht_smallarray
;
18926 sv
= &SCRIPT_SV(i
);
18927 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
18930 while (ga_scripts
.ga_len
< id
)
18932 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
18933 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
18934 ++ga_scripts
.ga_len
;
18940 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18944 init_var_dict(dict
, dict_var
)
18946 dictitem_T
*dict_var
;
18948 hash_init(&dict
->dv_hashtab
);
18949 dict
->dv_refcount
= DO_NOT_FREE_CNT
;
18950 dict_var
->di_tv
.vval
.v_dict
= dict
;
18951 dict_var
->di_tv
.v_type
= VAR_DICT
;
18952 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
18953 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
18954 dict_var
->di_key
[0] = NUL
;
18958 * Clean up a list of internal variables.
18959 * Frees all allocated variables and the value they contain.
18960 * Clears hashtab "ht", does not free it.
18966 vars_clear_ext(ht
, TRUE
);
18970 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18973 vars_clear_ext(ht
, free_val
)
18982 todo
= (int)ht
->ht_used
;
18983 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
18985 if (!HASHITEM_EMPTY(hi
))
18989 /* Free the variable. Don't remove it from the hashtab,
18990 * ht_array might change then. hash_clear() takes care of it
18994 clear_tv(&v
->di_tv
);
18995 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
19004 * Delete a variable from hashtab "ht" at item "hi".
19005 * Clear the variable value and free the dictitem.
19012 dictitem_T
*di
= HI2DI(hi
);
19014 hash_remove(ht
, hi
);
19015 clear_tv(&di
->di_tv
);
19020 * List the value of one internal variable.
19023 list_one_var(v
, prefix
, first
)
19030 char_u numbuf
[NUMBUFLEN
];
19032 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
19033 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
19034 s
== NULL
? (char_u
*)"" : s
, first
);
19039 list_one_var_a(prefix
, name
, type
, string
, first
)
19044 int *first
; /* when TRUE clear rest of screen and set to FALSE */
19046 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19049 if (name
!= NULL
) /* "a:" vars don't have a name stored */
19053 if (type
== VAR_NUMBER
)
19055 else if (type
== VAR_FUNC
)
19057 else if (type
== VAR_LIST
)
19060 if (*string
== '[')
19063 else if (type
== VAR_DICT
)
19066 if (*string
== '{')
19072 msg_outtrans(string
);
19074 if (type
== VAR_FUNC
)
19075 msg_puts((char_u
*)"()");
19084 * Set variable "name" to value in "tv".
19085 * If the variable already exists, the value is updated.
19086 * Otherwise the variable is created.
19089 set_var(name
, tv
, copy
)
19092 int copy
; /* make copy of value in "tv" */
19099 if (tv
->v_type
== VAR_FUNC
)
19101 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
19102 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
19103 ? name
[2] : name
[0]))
19105 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
19108 if (function_exists(name
))
19110 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19116 ht
= find_var_ht(name
, &varname
);
19117 if (ht
== NULL
|| *varname
== NUL
)
19119 EMSG2(_(e_illvar
), name
);
19123 v
= find_var_in_ht(ht
, varname
, TRUE
);
19126 /* existing variable, need to clear the value */
19127 if (var_check_ro(v
->di_flags
, name
)
19128 || tv_check_lock(v
->di_tv
.v_lock
, name
))
19130 if (v
->di_tv
.v_type
!= tv
->v_type
19131 && !((v
->di_tv
.v_type
== VAR_STRING
19132 || v
->di_tv
.v_type
== VAR_NUMBER
)
19133 && (tv
->v_type
== VAR_STRING
19134 || tv
->v_type
== VAR_NUMBER
))
19136 && !((v
->di_tv
.v_type
== VAR_NUMBER
19137 || v
->di_tv
.v_type
== VAR_FLOAT
)
19138 && (tv
->v_type
== VAR_NUMBER
19139 || tv
->v_type
== VAR_FLOAT
))
19143 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
19148 * Handle setting internal v: variables separately: we don't change
19151 if (ht
== &vimvarht
)
19153 if (v
->di_tv
.v_type
== VAR_STRING
)
19155 vim_free(v
->di_tv
.vval
.v_string
);
19156 if (copy
|| tv
->v_type
!= VAR_STRING
)
19157 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
19160 /* Take over the string to avoid an extra alloc/free. */
19161 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
19162 tv
->vval
.v_string
= NULL
;
19165 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
19166 EMSG2(_(e_intern2
), "set_var()");
19169 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
19170 if (STRCMP(varname
, "searchforward") == 0)
19171 set_search_direction(v
->di_tv
.vval
.v_number
? '/' : '?');
19176 clear_tv(&v
->di_tv
);
19178 else /* add a new variable */
19180 /* Can't add "v:" variable. */
19181 if (ht
== &vimvarht
)
19183 EMSG2(_(e_illvar
), name
);
19187 /* Make sure the variable name is valid. */
19188 for (p
= varname
; *p
!= NUL
; ++p
)
19189 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
19190 && *p
!= AUTOLOAD_CHAR
)
19192 EMSG2(_(e_illvar
), varname
);
19196 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19197 + STRLEN(varname
)));
19200 STRCPY(v
->di_key
, varname
);
19201 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
19209 if (copy
|| tv
->v_type
== VAR_NUMBER
|| tv
->v_type
== VAR_FLOAT
)
19210 copy_tv(tv
, &v
->di_tv
);
19214 v
->di_tv
.v_lock
= 0;
19220 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
19221 * Also give an error message.
19224 var_check_ro(flags
, name
)
19228 if (flags
& DI_FLAGS_RO
)
19230 EMSG2(_(e_readonlyvar
), name
);
19233 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
19235 EMSG2(_(e_readonlysbx
), name
);
19242 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19243 * Also give an error message.
19246 var_check_fixed(flags
, name
)
19250 if (flags
& DI_FLAGS_FIX
)
19252 EMSG2(_("E795: Cannot delete variable %s"), name
);
19259 * Return TRUE if typeval "tv" is set to be locked (immutable).
19260 * Also give an error message, using "name".
19263 tv_check_lock(lock
, name
)
19267 if (lock
& VAR_LOCKED
)
19269 EMSG2(_("E741: Value is locked: %s"),
19270 name
== NULL
? (char_u
*)_("Unknown") : name
);
19273 if (lock
& VAR_FIXED
)
19275 EMSG2(_("E742: Cannot change value of %s"),
19276 name
== NULL
? (char_u
*)_("Unknown") : name
);
19283 * Copy the values from typval_T "from" to typval_T "to".
19284 * When needed allocates string or increases reference count.
19285 * Does not make a copy of a list or dict but copies the reference!
19286 * It is OK for "from" and "to" to point to the same item. This is used to
19287 * make a copy later.
19294 to
->v_type
= from
->v_type
;
19296 switch (from
->v_type
)
19299 to
->vval
.v_number
= from
->vval
.v_number
;
19303 to
->vval
.v_float
= from
->vval
.v_float
;
19308 if (from
->vval
.v_string
== NULL
)
19309 to
->vval
.v_string
= NULL
;
19312 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
19313 if (from
->v_type
== VAR_FUNC
)
19314 func_ref(to
->vval
.v_string
);
19318 if (from
->vval
.v_list
== NULL
)
19319 to
->vval
.v_list
= NULL
;
19322 to
->vval
.v_list
= from
->vval
.v_list
;
19323 ++to
->vval
.v_list
->lv_refcount
;
19327 if (from
->vval
.v_dict
== NULL
)
19328 to
->vval
.v_dict
= NULL
;
19331 to
->vval
.v_dict
= from
->vval
.v_dict
;
19332 ++to
->vval
.v_dict
->dv_refcount
;
19336 EMSG2(_(e_intern2
), "copy_tv()");
19342 * Make a copy of an item.
19343 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
19344 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19345 * reference to an already copied list/dict can be used.
19346 * Returns FAIL or OK.
19349 item_copy(from
, to
, deep
, copyID
)
19355 static int recurse
= 0;
19358 if (recurse
>= DICT_MAXNEST
)
19360 EMSG(_("E698: variable nested too deep for making a copy"));
19365 switch (from
->v_type
)
19376 to
->v_type
= VAR_LIST
;
19378 if (from
->vval
.v_list
== NULL
)
19379 to
->vval
.v_list
= NULL
;
19380 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
19382 /* use the copy made earlier */
19383 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
19384 ++to
->vval
.v_list
->lv_refcount
;
19387 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
19388 if (to
->vval
.v_list
== NULL
)
19392 to
->v_type
= VAR_DICT
;
19394 if (from
->vval
.v_dict
== NULL
)
19395 to
->vval
.v_dict
= NULL
;
19396 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
19398 /* use the copy made earlier */
19399 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
19400 ++to
->vval
.v_dict
->dv_refcount
;
19403 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
19404 if (to
->vval
.v_dict
== NULL
)
19408 EMSG2(_(e_intern2
), "item_copy()");
19416 * ":echo expr1 ..." print each argument separated with a space, add a
19417 * newline at the end.
19418 * ":echon expr1 ..." print each argument plain.
19424 char_u
*arg
= eap
->arg
;
19428 int needclr
= TRUE
;
19429 int atstart
= TRUE
;
19430 char_u numbuf
[NUMBUFLEN
];
19434 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
19436 /* If eval1() causes an error message the text from the command may
19437 * still need to be cleared. E.g., "echo 22,44". */
19438 need_clr_eos
= needclr
;
19441 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19444 * Report the invalid expression unless the expression evaluation
19445 * has been cancelled due to an aborting error, an interrupt, or an
19449 EMSG2(_(e_invexpr2
), p
);
19450 need_clr_eos
= FALSE
;
19453 need_clr_eos
= FALSE
;
19460 /* Call msg_start() after eval1(), evaluating the expression
19461 * may cause a message to appear. */
19462 if (eap
->cmdidx
== CMD_echo
)
19465 else if (eap
->cmdidx
== CMD_echo
)
19466 msg_puts_attr((char_u
*)" ", echo_attr
);
19467 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
19469 for ( ; *p
!= NUL
&& !got_int
; ++p
)
19471 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
19473 if (*p
!= TAB
&& needclr
)
19475 /* remove any text still there from the command */
19479 msg_putchar_attr(*p
, echo_attr
);
19486 int i
= (*mb_ptr2len
)(p
);
19488 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
19493 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
19499 arg
= skipwhite(arg
);
19501 eap
->nextcmd
= check_nextcmd(arg
);
19507 /* remove text that may still be there from the command */
19510 if (eap
->cmdidx
== CMD_echo
)
19516 * ":echohl {name}".
19524 id
= syn_name2id(eap
->arg
);
19528 echo_attr
= syn_id2attr(id
);
19532 * ":execute expr1 ..." execute the result of an expression.
19533 * ":echomsg expr1 ..." Print a message
19534 * ":echoerr expr1 ..." Print an error
19535 * Each gets spaces around each argument and a newline at the end for
19542 char_u
*arg
= eap
->arg
;
19550 ga_init2(&ga
, 1, 80);
19554 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
19557 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19560 * Report the invalid expression unless the expression evaluation
19561 * has been cancelled due to an aborting error, an interrupt, or an
19565 EMSG2(_(e_invexpr2
), p
);
19572 p
= get_tv_string(&rettv
);
19573 len
= (int)STRLEN(p
);
19574 if (ga_grow(&ga
, len
+ 2) == FAIL
)
19581 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
19582 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
19587 arg
= skipwhite(arg
);
19590 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
19592 if (eap
->cmdidx
== CMD_echomsg
)
19594 MSG_ATTR(ga
.ga_data
, echo_attr
);
19597 else if (eap
->cmdidx
== CMD_echoerr
)
19599 /* We don't want to abort following commands, restore did_emsg. */
19600 save_did_emsg
= did_emsg
;
19601 EMSG((char_u
*)ga
.ga_data
);
19603 did_emsg
= save_did_emsg
;
19605 else if (eap
->cmdidx
== CMD_execute
)
19606 do_cmdline((char_u
*)ga
.ga_data
,
19607 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
19615 eap
->nextcmd
= check_nextcmd(arg
);
19619 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19620 * "arg" points to the "&" or '+' when called, to "option" when returning.
19621 * Returns NULL when no option name found. Otherwise pointer to the char
19622 * after the option name.
19625 find_option_end(arg
, opt_flags
)
19632 if (*p
== 'g' && p
[1] == ':')
19634 *opt_flags
= OPT_GLOBAL
;
19637 else if (*p
== 'l' && p
[1] == ':')
19639 *opt_flags
= OPT_LOCAL
;
19645 if (!ASCII_ISALPHA(*p
))
19649 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
19650 p
+= 4; /* termcap option */
19652 while (ASCII_ISALPHA(*p
))
19667 int saved_did_emsg
;
19668 char_u
*name
= NULL
;
19671 char_u
*line_arg
= NULL
;
19674 int varargs
= FALSE
;
19675 int mustend
= FALSE
;
19680 char_u
*skip_until
= NULL
;
19683 static int func_nr
= 0; /* number for nameless function */
19688 int sourcing_lnum_off
;
19691 * ":function" without argument: list functions.
19693 if (ends_excmd(*eap
->arg
))
19697 todo
= (int)func_hashtab
.ht_used
;
19698 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19700 if (!HASHITEM_EMPTY(hi
))
19704 if (!isdigit(*fp
->uf_name
))
19705 list_func_head(fp
, FALSE
);
19709 eap
->nextcmd
= check_nextcmd(eap
->arg
);
19714 * ":function /pat": list functions matching pattern.
19716 if (*eap
->arg
== '/')
19718 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
19721 regmatch_T regmatch
;
19725 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
19727 if (regmatch
.regprog
!= NULL
)
19729 regmatch
.rm_ic
= p_ic
;
19731 todo
= (int)func_hashtab
.ht_used
;
19732 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19734 if (!HASHITEM_EMPTY(hi
))
19738 if (!isdigit(*fp
->uf_name
)
19739 && vim_regexec(®match
, fp
->uf_name
, 0))
19740 list_func_head(fp
, FALSE
);
19743 vim_free(regmatch
.regprog
);
19748 eap
->nextcmd
= check_nextcmd(p
);
19753 * Get the function name. There are these situations:
19754 * func normal function name
19755 * "name" == func, "fudi.fd_dict" == NULL
19756 * dict.func new dictionary entry
19757 * "name" == NULL, "fudi.fd_dict" set,
19758 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19759 * dict.func existing dict entry with a Funcref
19760 * "name" == func, "fudi.fd_dict" set,
19761 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19762 * dict.func existing dict entry that's not a Funcref
19763 * "name" == NULL, "fudi.fd_dict" set,
19764 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19767 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19768 paren
= (vim_strchr(p
, '(') != NULL
);
19769 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
19772 * Return on an invalid expression in braces, unless the expression
19773 * evaluation has been cancelled due to an aborting error, an
19774 * interrupt, or an exception.
19778 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
19779 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
19780 vim_free(fudi
.fd_newkey
);
19787 /* An error in a function call during evaluation of an expression in magic
19788 * braces should not cause the function not to be defined. */
19789 saved_did_emsg
= did_emsg
;
19793 * ":function func" with only function name: list function.
19797 if (!ends_excmd(*skipwhite(p
)))
19799 EMSG(_(e_trailing
));
19802 eap
->nextcmd
= check_nextcmd(p
);
19803 if (eap
->nextcmd
!= NULL
)
19805 if (!eap
->skip
&& !got_int
)
19807 fp
= find_func(name
);
19810 list_func_head(fp
, TRUE
);
19811 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
19813 if (FUNCLINE(fp
, j
) == NULL
)
19816 msg_outnum((long)(j
+ 1));
19821 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
19822 out_flush(); /* show a line at a time */
19828 msg_puts((char_u
*)" endfunction");
19832 emsg_funcname(N_("E123: Undefined function: %s"), name
);
19838 * ":function name(arg1, arg2)" Define function.
19845 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
19848 /* attempt to continue by skipping some text */
19849 if (vim_strchr(p
, '(') != NULL
)
19850 p
= vim_strchr(p
, '(');
19852 p
= skipwhite(p
+ 1);
19854 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
19855 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
19859 /* Check the name of the function. Unless it's a dictionary function
19860 * (that we are overwriting). */
19864 arg
= fudi
.fd_newkey
;
19865 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
19866 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
19868 if (*arg
== K_SPECIAL
)
19872 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
19873 : eval_isnamec(arg
[j
])))
19876 emsg_funcname((char *)e_invarg2
, arg
);
19881 * Isolate the arguments: "arg1, arg2, ...)"
19885 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
19894 while (ASCII_ISALNUM(*p
) || *p
== '_')
19896 if (arg
== p
|| isdigit(*arg
)
19897 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
19898 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
19901 EMSG2(_("E125: Illegal argument: %s"), arg
);
19904 if (ga_grow(&newargs
, 1) == FAIL
)
19908 arg
= vim_strsave(arg
);
19911 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
19920 if (mustend
&& *p
!= ')')
19923 EMSG2(_(e_invarg2
), eap
->arg
);
19927 ++p
; /* skip the ')' */
19929 /* find extra arguments "range", "dict" and "abort" */
19933 if (STRNCMP(p
, "range", 5) == 0)
19938 else if (STRNCMP(p
, "dict", 4) == 0)
19943 else if (STRNCMP(p
, "abort", 5) == 0)
19952 /* When there is a line break use what follows for the function body.
19953 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19956 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
19957 EMSG(_(e_trailing
));
19960 * Read the body of the function, until ":endfunction" is found.
19964 /* Check if the function already exists, don't let the user type the
19965 * whole function before telling him it doesn't work! For a script we
19966 * need to skip the body to be able to find what follows. */
19967 if (!eap
->skip
&& !eap
->forceit
)
19969 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
19970 EMSG(_(e_funcdict
));
19971 else if (name
!= NULL
&& find_func(name
) != NULL
)
19972 emsg_funcname(e_funcexts
, name
);
19975 if (!eap
->skip
&& did_emsg
)
19978 msg_putchar('\n'); /* don't overwrite the function name */
19979 cmdline_row
= msg_row
;
19987 need_wait_return
= FALSE
;
19988 sourcing_lnum_off
= sourcing_lnum
;
19990 if (line_arg
!= NULL
)
19992 /* Use eap->arg, split up in parts by line breaks. */
19993 theline
= line_arg
;
19994 p
= vim_strchr(theline
, '\n');
19996 line_arg
+= STRLEN(line_arg
);
20003 else if (eap
->getline
== NULL
)
20004 theline
= getcmdline(':', 0L, indent
);
20006 theline
= eap
->getline(':', eap
->cookie
, indent
);
20008 lines_left
= Rows
- 1;
20009 if (theline
== NULL
)
20011 EMSG(_("E126: Missing :endfunction"));
20015 /* Detect line continuation: sourcing_lnum increased more than one. */
20016 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
20017 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
20019 sourcing_lnum_off
= 0;
20021 if (skip_until
!= NULL
)
20023 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20024 * don't check for ":endfunc". */
20025 if (STRCMP(theline
, skip_until
) == 0)
20027 vim_free(skip_until
);
20033 /* skip ':' and blanks*/
20034 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
20037 /* Check for "endfunction". */
20038 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
20040 if (line_arg
== NULL
)
20045 /* Increase indent inside "if", "while", "for" and "try", decrease
20047 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
20049 else if (STRNCMP(p
, "if", 2) == 0
20050 || STRNCMP(p
, "wh", 2) == 0
20051 || STRNCMP(p
, "for", 3) == 0
20052 || STRNCMP(p
, "try", 3) == 0)
20055 /* Check for defining a function inside this function. */
20056 if (checkforcmd(&p
, "function", 2))
20059 p
= skipwhite(p
+ 1);
20060 p
+= eval_fname_script(p
);
20061 if (ASCII_ISALPHA(*p
))
20063 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
20064 if (*skipwhite(p
) == '(')
20072 /* Check for ":append" or ":insert". */
20073 p
= skip_range(p
, NULL
);
20074 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
20076 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
20077 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
20078 skip_until
= vim_strsave((char_u
*)".");
20080 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20081 arg
= skipwhite(skiptowhite(p
));
20082 if (arg
[0] == '<' && arg
[1] =='<'
20083 && ((p
[0] == 'p' && p
[1] == 'y'
20084 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
20085 || (p
[0] == 'p' && p
[1] == 'e'
20086 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
20087 || (p
[0] == 't' && p
[1] == 'c'
20088 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
20089 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
20090 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
20091 || (p
[0] == 'm' && p
[1] == 'z'
20092 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
20095 /* ":python <<" continues until a dot, like ":append" */
20096 p
= skipwhite(arg
+ 2);
20098 skip_until
= vim_strsave((char_u
*)".");
20100 skip_until
= vim_strsave(p
);
20104 /* Add the line to the function. */
20105 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
20107 if (line_arg
== NULL
)
20112 /* Copy the line to newly allocated memory. get_one_sourceline()
20113 * allocates 250 bytes per line, this saves 80% on average. The cost
20114 * is an extra alloc/free. */
20115 p
= vim_strsave(theline
);
20118 if (line_arg
== NULL
)
20123 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
20125 /* Add NULL lines for continuation lines, so that the line count is
20126 * equal to the index in the growarray. */
20127 while (sourcing_lnum_off
-- > 0)
20128 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
20130 /* Check for end of eap->arg. */
20131 if (line_arg
!= NULL
&& *line_arg
== NUL
)
20135 /* Don't define the function when skipping commands or when an error was
20137 if (eap
->skip
|| did_emsg
)
20141 * If there are no errors, add the function
20143 if (fudi
.fd_dict
== NULL
)
20145 v
= find_var(name
, &ht
);
20146 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
20148 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
20153 fp
= find_func(name
);
20158 emsg_funcname(e_funcexts
, name
);
20161 if (fp
->uf_calls
> 0)
20163 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
20167 /* redefine existing function */
20168 ga_clear_strings(&(fp
->uf_args
));
20169 ga_clear_strings(&(fp
->uf_lines
));
20179 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
20181 EMSG(_(e_funcdict
));
20184 if (fudi
.fd_di
== NULL
)
20186 /* Can't add a function to a locked dictionary */
20187 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
20190 /* Can't change an existing function if it is locked */
20191 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
20194 /* Give the function a sequential number. Can only be used with a
20197 sprintf(numbuf
, "%d", ++func_nr
);
20198 name
= vim_strsave((char_u
*)numbuf
);
20205 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
20208 char_u
*scriptname
;
20210 /* Check that the autoload name matches the script name. */
20212 if (sourcing_name
!= NULL
)
20214 scriptname
= autoload_name(name
);
20215 if (scriptname
!= NULL
)
20217 p
= vim_strchr(scriptname
, '/');
20218 plen
= (int)STRLEN(p
);
20219 slen
= (int)STRLEN(sourcing_name
);
20220 if (slen
> plen
&& fnamecmp(p
,
20221 sourcing_name
+ slen
- plen
) == 0)
20223 vim_free(scriptname
);
20228 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
20233 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
20237 if (fudi
.fd_dict
!= NULL
)
20239 if (fudi
.fd_di
== NULL
)
20241 /* add new dict entry */
20242 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
20243 if (fudi
.fd_di
== NULL
)
20248 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
20250 vim_free(fudi
.fd_di
);
20256 /* overwrite existing dict entry */
20257 clear_tv(&fudi
.fd_di
->di_tv
);
20258 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
20259 fudi
.fd_di
->di_tv
.v_lock
= 0;
20260 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
20261 fp
->uf_refcount
= 1;
20263 /* behave like "dict" was used */
20267 /* insert the new function in the function list */
20268 STRCPY(fp
->uf_name
, name
);
20269 hash_add(&func_hashtab
, UF2HIKEY(fp
));
20271 fp
->uf_args
= newargs
;
20272 fp
->uf_lines
= newlines
;
20273 #ifdef FEAT_PROFILE
20274 fp
->uf_tml_count
= NULL
;
20275 fp
->uf_tml_total
= NULL
;
20276 fp
->uf_tml_self
= NULL
;
20277 fp
->uf_profiling
= FALSE
;
20278 if (prof_def_func())
20279 func_do_profile(fp
);
20281 fp
->uf_varargs
= varargs
;
20282 fp
->uf_flags
= flags
;
20284 fp
->uf_script_ID
= current_SID
;
20288 ga_clear_strings(&newargs
);
20289 ga_clear_strings(&newlines
);
20291 vim_free(skip_until
);
20292 vim_free(fudi
.fd_newkey
);
20294 did_emsg
|= saved_did_emsg
;
20298 * Get a function name, translating "<SID>" and "<SNR>".
20299 * Also handles a Funcref in a List or Dictionary.
20300 * Returns the function name in allocated memory, or NULL for failure.
20302 * TFN_INT: internal function name OK
20303 * TFN_QUIET: be quiet
20304 * Advances "pp" to just after the function name (if no error).
20307 trans_function_name(pp
, skip
, flags
, fdp
)
20309 int skip
; /* only find the end, don't evaluate */
20311 funcdict_T
*fdp
; /* return: info about dictionary used */
20313 char_u
*name
= NULL
;
20317 char_u sid_buf
[20];
20322 vim_memset(fdp
, 0, sizeof(funcdict_T
));
20325 /* Check for hard coded <SNR>: already translated function ID (from a user
20327 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
20328 && (*pp
)[2] == (int)KE_SNR
)
20331 len
= get_id_len(pp
) + 3;
20332 return vim_strnsave(start
, len
);
20335 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20336 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
20337 lead
= eval_fname_script(start
);
20341 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
20342 lead
> 2 ? 0 : FNE_CHECK_START
);
20346 EMSG(_("E129: Function name required"));
20349 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
20352 * Report an invalid expression in braces, unless the expression
20353 * evaluation has been cancelled due to an aborting error, an
20354 * interrupt, or an exception.
20359 EMSG2(_(e_invarg2
), start
);
20362 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
20366 if (lv
.ll_tv
!= NULL
)
20370 fdp
->fd_dict
= lv
.ll_dict
;
20371 fdp
->fd_newkey
= lv
.ll_newkey
;
20372 lv
.ll_newkey
= NULL
;
20373 fdp
->fd_di
= lv
.ll_di
;
20375 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
20377 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
20382 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
20383 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
20384 EMSG(_(e_funcref
));
20392 if (lv
.ll_name
== NULL
)
20394 /* Error found, but continue after the function name. */
20399 /* Check if the name is a Funcref. If so, use the value. */
20400 if (lv
.ll_exp_name
!= NULL
)
20402 len
= (int)STRLEN(lv
.ll_exp_name
);
20403 name
= deref_func_name(lv
.ll_exp_name
, &len
);
20404 if (name
== lv
.ll_exp_name
)
20409 len
= (int)(end
- *pp
);
20410 name
= deref_func_name(*pp
, &len
);
20416 name
= vim_strsave(name
);
20421 if (lv
.ll_exp_name
!= NULL
)
20423 len
= (int)STRLEN(lv
.ll_exp_name
);
20424 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
20425 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
20427 /* When there was "s:" already or the name expanded to get a
20428 * leading "s:" then remove it. */
20436 if (lead
== 2) /* skip over "s:" */
20438 len
= (int)(end
- lv
.ll_name
);
20442 * Copy the function name to allocated memory.
20443 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20444 * Accept <SNR>123_name() outside a script.
20447 lead
= 0; /* do nothing */
20451 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
20452 || eval_fname_sid(*pp
))
20454 /* It's "s:" or "<SID>" */
20455 if (current_SID
<= 0)
20457 EMSG(_(e_usingsid
));
20460 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
20461 lead
+= (int)STRLEN(sid_buf
);
20464 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
20466 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
20469 name
= alloc((unsigned)(len
+ lead
+ 1));
20474 name
[0] = K_SPECIAL
;
20475 name
[1] = KS_EXTRA
;
20476 name
[2] = (int)KE_SNR
;
20477 if (lead
> 3) /* If it's "<SID>" */
20478 STRCPY(name
+ 3, sid_buf
);
20480 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
20481 name
[len
+ lead
] = NUL
;
20491 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20492 * Return 2 if "p" starts with "s:".
20493 * Return 0 otherwise.
20496 eval_fname_script(p
)
20499 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
20500 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
20502 if (p
[0] == 's' && p
[1] == ':')
20508 * Return TRUE if "p" starts with "<SID>" or "s:".
20509 * Only works if eval_fname_script() returned non-zero for "p"!
20515 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
20519 * List the head of the function: "name(arg1, arg2)".
20522 list_func_head(fp
, indent
)
20531 MSG_PUTS("function ");
20532 if (fp
->uf_name
[0] == K_SPECIAL
)
20534 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
20535 msg_puts(fp
->uf_name
+ 3);
20538 msg_puts(fp
->uf_name
);
20540 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
20544 msg_puts(FUNCARG(fp
, j
));
20546 if (fp
->uf_varargs
)
20555 last_set_msg(fp
->uf_script_ID
);
20559 * Find a function by name, return pointer to it in ufuncs.
20560 * Return NULL for unknown function.
20568 hi
= hash_find(&func_hashtab
, name
);
20569 if (!HASHITEM_EMPTY(hi
))
20574 #if defined(EXITFREE) || defined(PROTO)
20576 free_all_functions()
20580 /* Need to start all over every time, because func_free() may change the
20582 while (func_hashtab
.ht_used
> 0)
20583 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
20584 if (!HASHITEM_EMPTY(hi
))
20586 func_free(HI2UF(hi
));
20593 * Return TRUE if a function "name" exists.
20596 function_exists(name
)
20603 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
20604 nm
= skipwhite(nm
);
20606 /* Only accept "funcname", "funcname ", "funcname (..." and
20607 * "funcname(...", not "funcname!...". */
20608 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
20610 if (builtin_function(p
))
20611 n
= (find_internal_func(p
) >= 0);
20613 n
= (find_func(p
) != NULL
);
20620 * Return TRUE if "name" looks like a builtin function name: starts with a
20621 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
20624 builtin_function(name
)
20627 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
20628 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
20631 #if defined(FEAT_PROFILE) || defined(PROTO)
20633 * Start profiling function "fp".
20636 func_do_profile(fp
)
20639 fp
->uf_tm_count
= 0;
20640 profile_zero(&fp
->uf_tm_self
);
20641 profile_zero(&fp
->uf_tm_total
);
20642 if (fp
->uf_tml_count
== NULL
)
20643 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
20644 (sizeof(int) * fp
->uf_lines
.ga_len
));
20645 if (fp
->uf_tml_total
== NULL
)
20646 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
20647 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20648 if (fp
->uf_tml_self
== NULL
)
20649 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
20650 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20651 fp
->uf_tml_idx
= -1;
20652 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
20653 || fp
->uf_tml_self
== NULL
)
20654 return; /* out of memory */
20656 fp
->uf_profiling
= TRUE
;
20660 * Dump the profiling results for all functions in file "fd".
20663 func_dump_profile(fd
)
20673 todo
= (int)func_hashtab
.ht_used
;
20675 return; /* nothing to dump */
20677 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
20679 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
20681 if (!HASHITEM_EMPTY(hi
))
20685 if (fp
->uf_profiling
)
20687 if (sorttab
!= NULL
)
20688 sorttab
[st_len
++] = fp
;
20690 if (fp
->uf_name
[0] == K_SPECIAL
)
20691 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
20693 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
20694 if (fp
->uf_tm_count
== 1)
20695 fprintf(fd
, "Called 1 time\n");
20697 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
20698 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
20699 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
20701 fprintf(fd
, "count total (s) self (s)\n");
20703 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
20705 if (FUNCLINE(fp
, i
) == NULL
)
20707 prof_func_line(fd
, fp
->uf_tml_count
[i
],
20708 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
20709 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
20716 if (sorttab
!= NULL
&& st_len
> 0)
20718 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20720 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
20721 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20723 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
20730 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
20735 int prefer_self
; /* when equal print only self time */
20740 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
20741 fprintf(fd
, "count total (s) self (s) function\n");
20742 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
20745 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
20747 if (fp
->uf_name
[0] == K_SPECIAL
)
20748 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
20750 fprintf(fd
, " %s()\n", fp
->uf_name
);
20756 * Print the count and times for one function or function line.
20759 prof_func_line(fd
, count
, total
, self
, prefer_self
)
20764 int prefer_self
; /* when equal print only self time */
20768 fprintf(fd
, "%5d ", count
);
20769 if (prefer_self
&& profile_equal(total
, self
))
20772 fprintf(fd
, "%s ", profile_msg(total
));
20773 if (!prefer_self
&& profile_equal(total
, self
))
20776 fprintf(fd
, "%s ", profile_msg(self
));
20783 * Compare function for total time sorting.
20786 #ifdef __BORLANDC__
20789 prof_total_cmp(s1
, s2
)
20795 p1
= *(ufunc_T
**)s1
;
20796 p2
= *(ufunc_T
**)s2
;
20797 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
20801 * Compare function for self time sorting.
20804 #ifdef __BORLANDC__
20807 prof_self_cmp(s1
, s2
)
20813 p1
= *(ufunc_T
**)s1
;
20814 p2
= *(ufunc_T
**)s2
;
20815 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
20821 * If "name" has a package name try autoloading the script for it.
20822 * Return TRUE if a package was loaded.
20825 script_autoload(name
, reload
)
20827 int reload
; /* load script again when already loaded */
20830 char_u
*scriptname
, *tofree
;
20834 /* If there is no '#' after name[0] there is no package name. */
20835 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
20836 if (p
== NULL
|| p
== name
)
20839 tofree
= scriptname
= autoload_name(name
);
20841 /* Find the name in the list of previously loaded package names. Skip
20842 * "autoload/", it's always the same. */
20843 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
20844 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
20846 if (!reload
&& i
< ga_loaded
.ga_len
)
20847 ret
= FALSE
; /* was loaded already */
20850 /* Remember the name if it wasn't loaded already. */
20851 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
20853 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
20857 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
20858 if (source_runtime(scriptname
, FALSE
) == OK
)
20867 * Return the autoload script name for a function or variable name.
20868 * Returns NULL when out of memory.
20871 autoload_name(name
)
20875 char_u
*scriptname
;
20877 /* Get the script file name: replace '#' with '/', append ".vim". */
20878 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
20879 if (scriptname
== NULL
)
20881 STRCPY(scriptname
, "autoload/");
20882 STRCAT(scriptname
, name
);
20883 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
20884 STRCAT(scriptname
, ".vim");
20885 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
20890 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20893 * Function given to ExpandGeneric() to obtain the list of user defined
20897 get_user_func_name(xp
, idx
)
20901 static long_u done
;
20902 static hashitem_T
*hi
;
20908 hi
= func_hashtab
.ht_array
;
20910 if (done
< func_hashtab
.ht_used
)
20914 while (HASHITEM_EMPTY(hi
))
20918 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
20919 return fp
->uf_name
; /* prevents overflow */
20921 cat_func_name(IObuff
, fp
);
20922 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
20924 STRCAT(IObuff
, "(");
20925 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
20926 STRCAT(IObuff
, ")");
20933 #endif /* FEAT_CMDL_COMPL */
20936 * Copy the function name of "fp" to buffer "buf".
20937 * "buf" must be able to hold the function name plus three bytes.
20938 * Takes care of script-local function names.
20941 cat_func_name(buf
, fp
)
20945 if (fp
->uf_name
[0] == K_SPECIAL
)
20947 STRCPY(buf
, "<SNR>");
20948 STRCAT(buf
, fp
->uf_name
+ 3);
20951 STRCPY(buf
, fp
->uf_name
);
20955 * ":delfunction {name}"
20958 ex_delfunction(eap
)
20961 ufunc_T
*fp
= NULL
;
20967 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
20968 vim_free(fudi
.fd_newkey
);
20971 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
20972 EMSG(_(e_funcref
));
20975 if (!ends_excmd(*skipwhite(p
)))
20978 EMSG(_(e_trailing
));
20981 eap
->nextcmd
= check_nextcmd(p
);
20982 if (eap
->nextcmd
!= NULL
)
20986 fp
= find_func(name
);
20993 EMSG2(_(e_nofunc
), eap
->arg
);
20996 if (fp
->uf_calls
> 0)
20998 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
21002 if (fudi
.fd_dict
!= NULL
)
21004 /* Delete the dict item that refers to the function, it will
21005 * invoke func_unref() and possibly delete the function. */
21006 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
21014 * Free a function and remove it from the list of functions.
21022 /* clear this function */
21023 ga_clear_strings(&(fp
->uf_args
));
21024 ga_clear_strings(&(fp
->uf_lines
));
21025 #ifdef FEAT_PROFILE
21026 vim_free(fp
->uf_tml_count
);
21027 vim_free(fp
->uf_tml_total
);
21028 vim_free(fp
->uf_tml_self
);
21031 /* remove the function from the function hashtable */
21032 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
21033 if (HASHITEM_EMPTY(hi
))
21034 EMSG2(_(e_intern2
), "func_free()");
21036 hash_remove(&func_hashtab
, hi
);
21042 * Unreference a Function: decrement the reference count and free it when it
21043 * becomes zero. Only for numbered functions.
21051 if (name
!= NULL
&& isdigit(*name
))
21053 fp
= find_func(name
);
21055 EMSG2(_(e_intern2
), "func_unref()");
21056 else if (--fp
->uf_refcount
<= 0)
21058 /* Only delete it when it's not being used. Otherwise it's done
21059 * when "uf_calls" becomes zero. */
21060 if (fp
->uf_calls
== 0)
21067 * Count a reference to a Function.
21075 if (name
!= NULL
&& isdigit(*name
))
21077 fp
= find_func(name
);
21079 EMSG2(_(e_intern2
), "func_ref()");
21086 * Call a user function.
21089 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
21090 ufunc_T
*fp
; /* pointer to function */
21091 int argcount
; /* nr of args */
21092 typval_T
*argvars
; /* arguments */
21093 typval_T
*rettv
; /* return value */
21094 linenr_T firstline
; /* first line of range */
21095 linenr_T lastline
; /* last line of range */
21096 dict_T
*selfdict
; /* Dictionary for "self" */
21098 char_u
*save_sourcing_name
;
21099 linenr_T save_sourcing_lnum
;
21100 scid_T save_current_SID
;
21103 static int depth
= 0;
21105 int fixvar_idx
= 0; /* index in fixvar[] */
21108 char_u numbuf
[NUMBUFLEN
];
21110 #ifdef FEAT_PROFILE
21111 proftime_T wait_start
;
21112 proftime_T call_start
;
21115 /* If depth of calling is getting too high, don't execute the function */
21116 if (depth
>= p_mfd
)
21118 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
21119 rettv
->v_type
= VAR_NUMBER
;
21120 rettv
->vval
.v_number
= -1;
21125 line_breakcheck(); /* check for CTRL-C hit */
21127 fc
= (funccall_T
*)alloc(sizeof(funccall_T
));
21128 fc
->caller
= current_funccal
;
21129 current_funccal
= fc
;
21132 rettv
->vval
.v_number
= 0;
21134 fc
->returned
= FALSE
;
21135 fc
->level
= ex_nesting_level
;
21136 /* Check if this function has a breakpoint. */
21137 fc
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
21138 fc
->dbg_tick
= debug_tick
;
21141 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
21142 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21143 * each argument variable and saves a lot of time.
21146 * Init l: variables.
21148 init_var_dict(&fc
->l_vars
, &fc
->l_vars_var
);
21149 if (selfdict
!= NULL
)
21151 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21152 * some compiler that checks the destination size. */
21153 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21155 STRCPY(name
, "self");
21156 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
21157 hash_add(&fc
->l_vars
.dv_hashtab
, DI2HIKEY(v
));
21158 v
->di_tv
.v_type
= VAR_DICT
;
21159 v
->di_tv
.v_lock
= 0;
21160 v
->di_tv
.vval
.v_dict
= selfdict
;
21161 ++selfdict
->dv_refcount
;
21165 * Init a: variables.
21166 * Set a:0 to "argcount".
21167 * Set a:000 to a list with room for the "..." arguments.
21169 init_var_dict(&fc
->l_avars
, &fc
->l_avars_var
);
21170 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "0",
21171 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
21172 /* Use "name" to avoid a warning from some compiler that checks the
21173 * destination size. */
21174 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21176 STRCPY(name
, "000");
21177 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21178 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21179 v
->di_tv
.v_type
= VAR_LIST
;
21180 v
->di_tv
.v_lock
= VAR_FIXED
;
21181 v
->di_tv
.vval
.v_list
= &fc
->l_varlist
;
21182 vim_memset(&fc
->l_varlist
, 0, sizeof(list_T
));
21183 fc
->l_varlist
.lv_refcount
= DO_NOT_FREE_CNT
;
21184 fc
->l_varlist
.lv_lock
= VAR_FIXED
;
21187 * Set a:firstline to "firstline" and a:lastline to "lastline".
21188 * Set a:name to named arguments.
21189 * Set a:N to the "..." arguments.
21191 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "firstline",
21192 (varnumber_T
)firstline
);
21193 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "lastline",
21194 (varnumber_T
)lastline
);
21195 for (i
= 0; i
< argcount
; ++i
)
21197 ai
= i
- fp
->uf_args
.ga_len
;
21199 /* named argument a:name */
21200 name
= FUNCARG(fp
, i
);
21203 /* "..." argument a:1, a:2, etc. */
21204 sprintf((char *)numbuf
, "%d", ai
+ 1);
21207 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
21209 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21210 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21214 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
21218 v
->di_flags
= DI_FLAGS_RO
;
21220 STRCPY(v
->di_key
, name
);
21221 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21223 /* Note: the values are copied directly to avoid alloc/free.
21224 * "argvars" must have VAR_FIXED for v_lock. */
21225 v
->di_tv
= argvars
[i
];
21226 v
->di_tv
.v_lock
= VAR_FIXED
;
21228 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
21230 list_append(&fc
->l_varlist
, &fc
->l_listitems
[ai
]);
21231 fc
->l_listitems
[ai
].li_tv
= argvars
[i
];
21232 fc
->l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
21236 /* Don't redraw while executing the function. */
21237 ++RedrawingDisabled
;
21238 save_sourcing_name
= sourcing_name
;
21239 save_sourcing_lnum
= sourcing_lnum
;
21241 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
21242 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
21243 if (sourcing_name
!= NULL
)
21245 if (save_sourcing_name
!= NULL
21246 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
21247 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
21249 STRCPY(sourcing_name
, "function ");
21250 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
21252 if (p_verbose
>= 12)
21255 verbose_enter_scroll();
21257 smsg((char_u
*)_("calling %s"), sourcing_name
);
21258 if (p_verbose
>= 14)
21260 char_u buf
[MSG_BUF_LEN
];
21261 char_u numbuf2
[NUMBUFLEN
];
21265 msg_puts((char_u
*)"(");
21266 for (i
= 0; i
< argcount
; ++i
)
21269 msg_puts((char_u
*)", ");
21270 if (argvars
[i
].v_type
== VAR_NUMBER
)
21271 msg_outnum((long)argvars
[i
].vval
.v_number
);
21274 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
21277 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21283 msg_puts((char_u
*)")");
21285 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21287 verbose_leave_scroll();
21291 #ifdef FEAT_PROFILE
21292 if (do_profiling
== PROF_YES
)
21294 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
21295 func_do_profile(fp
);
21296 if (fp
->uf_profiling
21297 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
))
21300 profile_start(&call_start
);
21301 profile_zero(&fp
->uf_tm_children
);
21303 script_prof_save(&wait_start
);
21307 save_current_SID
= current_SID
;
21308 current_SID
= fp
->uf_script_ID
;
21309 save_did_emsg
= did_emsg
;
21312 /* call do_cmdline() to execute the lines */
21313 do_cmdline(NULL
, get_func_line
, (void *)fc
,
21314 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
21316 --RedrawingDisabled
;
21318 /* when the function was aborted because of an error, return -1 */
21319 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
21322 rettv
->v_type
= VAR_NUMBER
;
21323 rettv
->vval
.v_number
= -1;
21326 #ifdef FEAT_PROFILE
21327 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
21328 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)))
21330 profile_end(&call_start
);
21331 profile_sub_wait(&wait_start
, &call_start
);
21332 profile_add(&fp
->uf_tm_total
, &call_start
);
21333 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
21334 if (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)
21336 profile_add(&fc
->caller
->func
->uf_tm_children
, &call_start
);
21337 profile_add(&fc
->caller
->func
->uf_tml_children
, &call_start
);
21342 /* when being verbose, mention the return value */
21343 if (p_verbose
>= 12)
21346 verbose_enter_scroll();
21349 smsg((char_u
*)_("%s aborted"), sourcing_name
);
21350 else if (fc
->rettv
->v_type
== VAR_NUMBER
)
21351 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
21352 (long)fc
->rettv
->vval
.v_number
);
21355 char_u buf
[MSG_BUF_LEN
];
21356 char_u numbuf2
[NUMBUFLEN
];
21360 /* The value may be very long. Skip the middle part, so that we
21361 * have some idea how it starts and ends. smsg() would always
21362 * truncate it at the end. */
21363 s
= tv2string(fc
->rettv
, &tofree
, numbuf2
, 0);
21366 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21367 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
21371 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21373 verbose_leave_scroll();
21377 vim_free(sourcing_name
);
21378 sourcing_name
= save_sourcing_name
;
21379 sourcing_lnum
= save_sourcing_lnum
;
21380 current_SID
= save_current_SID
;
21381 #ifdef FEAT_PROFILE
21382 if (do_profiling
== PROF_YES
)
21383 script_prof_restore(&wait_start
);
21386 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
21389 verbose_enter_scroll();
21391 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
21392 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21394 verbose_leave_scroll();
21398 did_emsg
|= save_did_emsg
;
21399 current_funccal
= fc
->caller
;
21402 /* if the a:000 list and the a: dict are not referenced we can free the
21403 * funccall_T and what's in it. */
21404 if (fc
->l_varlist
.lv_refcount
== DO_NOT_FREE_CNT
21405 && fc
->l_vars
.dv_refcount
== DO_NOT_FREE_CNT
21406 && fc
->l_avars
.dv_refcount
== DO_NOT_FREE_CNT
)
21408 free_funccal(fc
, FALSE
);
21416 /* "fc" is still in use. This can happen when returning "a:000" or
21417 * assigning "l:" to a global variable.
21418 * Link "fc" in the list for garbage collection later. */
21419 fc
->caller
= previous_funccal
;
21420 previous_funccal
= fc
;
21422 /* Make a copy of the a: variables, since we didn't do that above. */
21423 todo
= (int)fc
->l_avars
.dv_hashtab
.ht_used
;
21424 for (hi
= fc
->l_avars
.dv_hashtab
.ht_array
; todo
> 0; ++hi
)
21426 if (!HASHITEM_EMPTY(hi
))
21430 copy_tv(&v
->di_tv
, &v
->di_tv
);
21434 /* Make a copy of the a:000 items, since we didn't do that above. */
21435 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21436 copy_tv(&li
->li_tv
, &li
->li_tv
);
21441 * Return TRUE if items in "fc" do not have "copyID". That means they are not
21442 * referenced from anywhere.
21445 can_free_funccal(fc
, copyID
)
21449 return (fc
->l_varlist
.lv_copyID
!= copyID
21450 && fc
->l_vars
.dv_copyID
!= copyID
21451 && fc
->l_avars
.dv_copyID
!= copyID
);
21455 * Free "fc" and what it contains.
21458 free_funccal(fc
, free_val
)
21460 int free_val
; /* a: vars were allocated */
21464 /* The a: variables typevals may not have been allocated, only free the
21465 * allocated variables. */
21466 vars_clear_ext(&fc
->l_avars
.dv_hashtab
, free_val
);
21468 /* free all l: variables */
21469 vars_clear(&fc
->l_vars
.dv_hashtab
);
21471 /* Free the a:000 variables if they were allocated. */
21473 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21474 clear_tv(&li
->li_tv
);
21480 * Add a number variable "name" to dict "dp" with value "nr".
21483 add_nr_var(dp
, v
, name
, nr
)
21489 STRCPY(v
->di_key
, name
);
21490 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21491 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
21492 v
->di_tv
.v_type
= VAR_NUMBER
;
21493 v
->di_tv
.v_lock
= VAR_FIXED
;
21494 v
->di_tv
.vval
.v_number
= nr
;
21504 char_u
*arg
= eap
->arg
;
21506 int returning
= FALSE
;
21508 if (current_funccal
== NULL
)
21510 EMSG(_("E133: :return not inside a function"));
21517 eap
->nextcmd
= NULL
;
21518 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
21519 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
21522 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
21526 /* It's safer to return also on error. */
21527 else if (!eap
->skip
)
21530 * Return unless the expression evaluation has been cancelled due to an
21531 * aborting error, an interrupt, or an exception.
21534 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
21537 /* When skipping or the return gets pending, advance to the next command
21538 * in this line (!returning). Otherwise, ignore the rest of the line.
21539 * Following lines will be ignored by get_func_line(). */
21541 eap
->nextcmd
= NULL
;
21542 else if (eap
->nextcmd
== NULL
) /* no argument */
21543 eap
->nextcmd
= check_nextcmd(arg
);
21550 * Return from a function. Possibly makes the return pending. Also called
21551 * for a pending return at the ":endtry" or after returning from an extra
21552 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
21553 * when called due to a ":return" command. "rettv" may point to a typval_T
21554 * with the return rettv. Returns TRUE when the return can be carried out,
21555 * FALSE when the return gets pending.
21558 do_return(eap
, reanimate
, is_cmd
, rettv
)
21565 struct condstack
*cstack
= eap
->cstack
;
21568 /* Undo the return. */
21569 current_funccal
->returned
= FALSE
;
21572 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21573 * not in its finally clause (which then is to be executed next) is found.
21574 * In this case, make the ":return" pending for execution at the ":endtry".
21575 * Otherwise, return normally.
21577 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
21580 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
21582 if (!is_cmd
&& !reanimate
)
21583 /* A pending return again gets pending. "rettv" points to an
21584 * allocated variable with the rettv of the original ":return"'s
21585 * argument if present or is NULL else. */
21586 cstack
->cs_rettv
[idx
] = rettv
;
21589 /* When undoing a return in order to make it pending, get the stored
21592 rettv
= current_funccal
->rettv
;
21596 /* Store the value of the pending return. */
21597 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
21598 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
21600 EMSG(_(e_outofmem
));
21603 cstack
->cs_rettv
[idx
] = NULL
;
21607 /* The pending return value could be overwritten by a ":return"
21608 * without argument in a finally clause; reset the default
21610 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
21611 current_funccal
->rettv
->vval
.v_number
= 0;
21614 report_make_pending(CSTP_RETURN
, rettv
);
21618 current_funccal
->returned
= TRUE
;
21620 /* If the return is carried out now, store the return value. For
21621 * a return immediately after reanimation, the value is already
21623 if (!reanimate
&& rettv
!= NULL
)
21625 clear_tv(current_funccal
->rettv
);
21626 *current_funccal
->rettv
= *(typval_T
*)rettv
;
21636 * Free the variable with a pending return value.
21639 discard_pending_return(rettv
)
21642 free_tv((typval_T
*)rettv
);
21646 * Generate a return command for producing the value of "rettv". The result
21647 * is an allocated string. Used by report_pending() for verbose messages.
21650 get_return_cmd(rettv
)
21654 char_u
*tofree
= NULL
;
21655 char_u numbuf
[NUMBUFLEN
];
21658 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
21662 STRCPY(IObuff
, ":return ");
21663 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
21664 if (STRLEN(s
) + 8 >= IOSIZE
)
21665 STRCPY(IObuff
+ IOSIZE
- 4, "...");
21667 return vim_strsave(IObuff
);
21671 * Get next function line.
21672 * Called by do_cmdline() to get the next line.
21673 * Returns allocated string, or NULL for end of function.
21677 get_func_line(c
, cookie
, indent
)
21678 int c
; /* not used */
21680 int indent
; /* not used */
21682 funccall_T
*fcp
= (funccall_T
*)cookie
;
21683 ufunc_T
*fp
= fcp
->func
;
21685 garray_T
*gap
; /* growarray with function lines */
21687 /* If breakpoints have been added/deleted need to check for it. */
21688 if (fcp
->dbg_tick
!= debug_tick
)
21690 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21692 fcp
->dbg_tick
= debug_tick
;
21694 #ifdef FEAT_PROFILE
21695 if (do_profiling
== PROF_YES
)
21696 func_line_end(cookie
);
21699 gap
= &fp
->uf_lines
;
21700 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21705 /* Skip NULL lines (continuation lines). */
21706 while (fcp
->linenr
< gap
->ga_len
21707 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
21709 if (fcp
->linenr
>= gap
->ga_len
)
21713 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
21714 sourcing_lnum
= fcp
->linenr
;
21715 #ifdef FEAT_PROFILE
21716 if (do_profiling
== PROF_YES
)
21717 func_line_start(cookie
);
21722 /* Did we encounter a breakpoint? */
21723 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
21725 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
21726 /* Find next breakpoint. */
21727 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21729 fcp
->dbg_tick
= debug_tick
;
21735 #if defined(FEAT_PROFILE) || defined(PROTO)
21737 * Called when starting to read a function line.
21738 * "sourcing_lnum" must be correct!
21739 * When skipping lines it may not actually be executed, but we won't find out
21740 * until later and we need to store the time now.
21743 func_line_start(cookie
)
21746 funccall_T
*fcp
= (funccall_T
*)cookie
;
21747 ufunc_T
*fp
= fcp
->func
;
21749 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
21750 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
21752 fp
->uf_tml_idx
= sourcing_lnum
- 1;
21753 /* Skip continuation lines. */
21754 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
21756 fp
->uf_tml_execed
= FALSE
;
21757 profile_start(&fp
->uf_tml_start
);
21758 profile_zero(&fp
->uf_tml_children
);
21759 profile_get_wait(&fp
->uf_tml_wait
);
21764 * Called when actually executing a function line.
21767 func_line_exec(cookie
)
21770 funccall_T
*fcp
= (funccall_T
*)cookie
;
21771 ufunc_T
*fp
= fcp
->func
;
21773 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21774 fp
->uf_tml_execed
= TRUE
;
21778 * Called when done with a function line.
21781 func_line_end(cookie
)
21784 funccall_T
*fcp
= (funccall_T
*)cookie
;
21785 ufunc_T
*fp
= fcp
->func
;
21787 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21789 if (fp
->uf_tml_execed
)
21791 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
21792 profile_end(&fp
->uf_tml_start
);
21793 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
21794 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
21795 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
21796 &fp
->uf_tml_children
);
21798 fp
->uf_tml_idx
= -1;
21804 * Return TRUE if the currently active function should be ended, because a
21805 * return was encountered or an error occurred. Used inside a ":while".
21808 func_has_ended(cookie
)
21811 funccall_T
*fcp
= (funccall_T
*)cookie
;
21813 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21814 * an error inside a try conditional. */
21815 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21820 * return TRUE if cookie indicates a function which "abort"s on errors.
21823 func_has_abort(cookie
)
21826 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
21829 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21832 VAR_FLAVOUR_DEFAULT
, /* doesn't start with uppercase */
21833 VAR_FLAVOUR_SESSION
, /* starts with uppercase, some lower */
21834 VAR_FLAVOUR_VIMINFO
/* all uppercase */
21837 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
21839 static var_flavour_T
21840 var_flavour(varname
)
21843 char_u
*p
= varname
;
21845 if (ASCII_ISUPPER(*p
))
21848 if (ASCII_ISLOWER(*p
))
21849 return VAR_FLAVOUR_SESSION
;
21850 return VAR_FLAVOUR_VIMINFO
;
21853 return VAR_FLAVOUR_DEFAULT
;
21857 #if defined(FEAT_VIMINFO) || defined(PROTO)
21859 * Restore global vars that start with a capital from the viminfo file
21862 read_viminfo_varlist(virp
, writing
)
21867 int type
= VAR_NUMBER
;
21870 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
21872 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
21875 *tab
++ = '\0'; /* isolate the variable name */
21876 if (*tab
== 'S') /* string var */
21879 else if (*tab
== 'F')
21883 tab
= vim_strchr(tab
, '\t');
21887 if (type
== VAR_STRING
)
21888 tv
.vval
.v_string
= viminfo_readstring(virp
,
21889 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
21891 else if (type
== VAR_FLOAT
)
21892 (void)string2float(tab
+ 1, &tv
.vval
.v_float
);
21895 tv
.vval
.v_number
= atol((char *)tab
+ 1);
21896 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
21897 if (type
== VAR_STRING
)
21898 vim_free(tv
.vval
.v_string
);
21903 return viminfo_readline(virp
);
21907 * Write global vars that start with a capital to the viminfo file
21910 write_viminfo_varlist(fp
)
21914 dictitem_T
*this_var
;
21919 char_u numbuf
[NUMBUFLEN
];
21921 if (find_viminfo_parameter('!') == NULL
)
21924 fprintf(fp
, _("\n# global variables:\n"));
21926 todo
= (int)globvarht
.ht_used
;
21927 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21929 if (!HASHITEM_EMPTY(hi
))
21932 this_var
= HI2DI(hi
);
21933 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
21935 switch (this_var
->di_tv
.v_type
)
21937 case VAR_STRING
: s
= "STR"; break;
21938 case VAR_NUMBER
: s
= "NUM"; break;
21940 case VAR_FLOAT
: s
= "FLO"; break;
21944 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
21945 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
21947 viminfo_writestring(fp
, p
);
21955 #if defined(FEAT_SESSION) || defined(PROTO)
21957 store_session_globals(fd
)
21961 dictitem_T
*this_var
;
21965 todo
= (int)globvarht
.ht_used
;
21966 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21968 if (!HASHITEM_EMPTY(hi
))
21971 this_var
= HI2DI(hi
);
21972 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
21973 || this_var
->di_tv
.v_type
== VAR_STRING
)
21974 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
21976 /* Escape special characters with a backslash. Turn a LF and
21977 * CR into \n and \r. */
21978 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
21979 (char_u
*)"\\\"\n\r");
21980 if (p
== NULL
) /* out of memory */
21982 for (t
= p
; *t
!= NUL
; ++t
)
21985 else if (*t
== '\r')
21987 if ((fprintf(fd
, "let %s = %c%s%c",
21989 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
21992 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
21994 || put_eol(fd
) == FAIL
)
22002 else if (this_var
->di_tv
.v_type
== VAR_FLOAT
22003 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22005 float_T f
= this_var
->di_tv
.vval
.v_float
;
22013 if ((fprintf(fd
, "let %s = %c&%f",
22014 this_var
->di_key
, sign
, f
) < 0)
22015 || put_eol(fd
) == FAIL
)
22026 * Display script name where an item was last set.
22027 * Should only be invoked when 'verbose' is non-zero.
22030 last_set_msg(scriptID
)
22037 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
22041 MSG_PUTS(_("\n\tLast set from "));
22050 * List v:oldfiles in a nice way.
22057 list_T
*l
= vimvars
[VV_OLDFILES
].vv_list
;
22062 msg((char_u
*)_("No old files"));
22067 for (li
= l
->lv_first
; li
!= NULL
&& !got_int
; li
= li
->li_next
)
22069 msg_outnum((long)++nr
);
22071 msg_outtrans(get_tv_string(&li
->li_tv
));
22073 out_flush(); /* output one line at a time */
22076 /* Assume "got_int" was set to truncate the listing. */
22079 #ifdef FEAT_BROWSE_CMD
22083 nr
= prompt_for_number(FALSE
);
22087 char_u
*p
= list_find_str(get_vim_var_list(VV_OLDFILES
),
22092 p
= expand_env_save(p
);
22094 eap
->cmdidx
= CMD_edit
;
22095 cmdmod
.browse
= FALSE
;
22096 do_exedit(eap
, NULL
);
22105 #endif /* FEAT_EVAL */
22108 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
22112 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22114 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22115 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
22116 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22119 * Get the short path (8.3) for the filename in "fnamep".
22120 * Only works for a valid file name.
22121 * When the path gets longer "fnamep" is changed and the allocated buffer
22122 * is put in "bufp".
22123 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22124 * Returns OK on success, FAIL on failure.
22127 get_short_pathname(fnamep
, bufp
, fnamelen
)
22136 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
22139 /* If that doesn't work (not enough space), then save the string
22140 * and try again with a new buffer big enough. */
22141 newbuf
= vim_strnsave(*fnamep
, l
);
22142 if (newbuf
== NULL
)
22146 *fnamep
= *bufp
= newbuf
;
22148 /* Really should always succeed, as the buffer is big enough. */
22149 l
= GetShortPathName(*fnamep
, *fnamep
, l
+1);
22157 * Get the short path (8.3) for the filename in "fname". The converted
22158 * path is returned in "bufp".
22160 * Some of the directories specified in "fname" may not exist. This function
22161 * will shorten the existing directories at the beginning of the path and then
22162 * append the remaining non-existing path.
22164 * fname - Pointer to the filename to shorten. On return, contains the
22165 * pointer to the shortened pathname
22166 * bufp - Pointer to an allocated buffer for the filename.
22167 * fnamelen - Length of the filename pointed to by fname
22169 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
22172 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
22177 char_u
*short_fname
, *save_fname
, *pbuf_unused
;
22178 char_u
*endp
, *save_endp
;
22181 int new_len
, sfx_len
;
22185 old_len
= *fnamelen
;
22186 save_fname
= vim_strnsave(*fname
, old_len
);
22187 pbuf_unused
= NULL
;
22188 short_fname
= NULL
;
22190 endp
= save_fname
+ old_len
- 1; /* Find the end of the copy */
22194 * Try shortening the supplied path till it succeeds by removing one
22195 * directory at a time from the tail of the path.
22200 /* go back one path-separator */
22201 while (endp
> save_fname
&& !after_pathsep(save_fname
, endp
+ 1))
22203 if (endp
<= save_fname
)
22204 break; /* processed the complete path */
22207 * Replace the path separator with a NUL and try to shorten the
22212 short_fname
= save_fname
;
22213 len
= (int)STRLEN(short_fname
) + 1;
22214 if (get_short_pathname(&short_fname
, &pbuf_unused
, &len
) == FAIL
)
22219 *endp
= ch
; /* preserve the string */
22222 break; /* successfully shortened the path */
22224 /* failed to shorten the path. Skip the path separator */
22231 * Succeeded in shortening the path. Now concatenate the shortened
22232 * path with the remaining path at the tail.
22235 /* Compute the length of the new path. */
22236 sfx_len
= (int)(save_endp
- endp
) + 1;
22237 new_len
= len
+ sfx_len
;
22239 *fnamelen
= new_len
;
22241 if (new_len
> old_len
)
22243 /* There is not enough space in the currently allocated string,
22244 * copy it to a buffer big enough. */
22245 *fname
= *bufp
= vim_strnsave(short_fname
, new_len
);
22246 if (*fname
== NULL
)
22254 /* Transfer short_fname to the main buffer (it's big enough),
22255 * unless get_short_pathname() did its work in-place. */
22256 *fname
= *bufp
= save_fname
;
22257 if (short_fname
!= save_fname
)
22258 vim_strncpy(save_fname
, short_fname
, len
);
22262 /* concat the not-shortened part of the path */
22263 vim_strncpy(*fname
+ len
, endp
, sfx_len
);
22264 (*fname
)[new_len
] = NUL
;
22268 vim_free(pbuf_unused
);
22269 vim_free(save_fname
);
22275 * Get a pathname for a partial path.
22276 * Returns OK for success, FAIL for failure.
22279 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
22284 int sepcount
, len
, tflen
;
22286 char_u
*pbuf
, *tfname
;
22289 /* Count up the path separators from the RHS.. so we know which part
22290 * of the path to return. */
22292 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
22293 if (vim_ispathsep(*p
))
22296 /* Need full path first (use expand_env() to remove a "~/") */
22297 hasTilde
= (**fnamep
== '~');
22299 pbuf
= tfname
= expand_env_save(*fnamep
);
22301 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
22303 len
= tflen
= (int)STRLEN(tfname
);
22305 if (get_short_pathname(&tfname
, &pbuf
, &len
) == FAIL
)
22310 /* Don't have a valid filename, so shorten the rest of the
22311 * path if we can. This CAN give us invalid 8.3 filenames, but
22312 * there's not a lot of point in guessing what it might be.
22315 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == FAIL
)
22319 /* Count the paths backward to find the beginning of the desired string. */
22320 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
22324 p
-= mb_head_off(tfname
, p
);
22326 if (vim_ispathsep(*p
))
22328 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
22345 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22347 *fnamelen
= (int)STRLEN(p
);
22353 #endif /* WIN3264 */
22356 * Adjust a filename, according to a string of modifiers.
22357 * *fnamep must be NUL terminated when called. When returning, the length is
22358 * determined by *fnamelen.
22359 * Returns VALID_ flags or -1 for failure.
22360 * When there is an error, *fnamep is set to NULL.
22363 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
22364 char_u
*src
; /* string with modifiers */
22365 int *usedlen
; /* characters after src that are used */
22366 char_u
**fnamep
; /* file name so far */
22367 char_u
**bufp
; /* buffer for allocated file name or NULL */
22368 int *fnamelen
; /* length of fnamep */
22372 char_u
*s
, *p
, *pbuf
;
22373 char_u dirname
[MAXPATHL
];
22375 int has_fullname
= 0;
22377 int has_shortname
= 0;
22381 /* ":p" - full path/file_name */
22382 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
22386 valid
|= VALID_PATH
;
22389 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22390 if ((*fnamep
)[0] == '~'
22391 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22392 && ((*fnamep
)[1] == '/'
22393 # ifdef BACKSLASH_IN_FILENAME
22394 || (*fnamep
)[1] == '\\'
22396 || (*fnamep
)[1] == NUL
)
22401 *fnamep
= expand_env_save(*fnamep
);
22402 vim_free(*bufp
); /* free any allocated file name */
22404 if (*fnamep
== NULL
)
22408 /* When "/." or "/.." is used: force expansion to get rid of it. */
22409 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
22411 if (vim_ispathsep(*p
)
22414 || vim_ispathsep(p
[2])
22416 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
22420 /* FullName_save() is slow, don't use it when not needed. */
22421 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
22423 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
22424 vim_free(*bufp
); /* free any allocated file name */
22426 if (*fnamep
== NULL
)
22430 /* Append a path separator to a directory. */
22431 if (mch_isdir(*fnamep
))
22433 /* Make room for one or two extra characters. */
22434 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
22435 vim_free(*bufp
); /* free any allocated file name */
22437 if (*fnamep
== NULL
)
22439 add_pathsep(*fnamep
);
22443 /* ":." - path relative to the current directory */
22444 /* ":~" - path relative to the home directory */
22445 /* ":8" - shortname path - postponed till after */
22446 while (src
[*usedlen
] == ':'
22447 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
22453 has_shortname
= 1; /* Postpone this. */
22458 /* Need full path first (use expand_env() to remove a "~/") */
22461 if (c
== '.' && **fnamep
== '~')
22462 p
= pbuf
= expand_env_save(*fnamep
);
22464 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
22475 mch_dirname(dirname
, MAXPATHL
);
22476 s
= shorten_fname(p
, dirname
);
22482 vim_free(*bufp
); /* free any allocated file name */
22490 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
22491 /* Only replace it when it starts with '~' */
22492 if (*dirname
== '~')
22494 s
= vim_strsave(dirname
);
22507 tail
= gettail(*fnamep
);
22508 *fnamelen
= (int)STRLEN(*fnamep
);
22510 /* ":h" - head, remove "/file_name", can be repeated */
22511 /* Don't remove the first "/" or "c:\" */
22512 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
22514 valid
|= VALID_HEAD
;
22516 s
= get_past_head(*fnamep
);
22517 while (tail
> s
&& after_pathsep(s
, tail
))
22518 mb_ptr_back(*fnamep
, tail
);
22519 *fnamelen
= (int)(tail
- *fnamep
);
22522 *fnamelen
+= 1; /* the path separator is part of the path */
22524 if (*fnamelen
== 0)
22526 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22527 p
= vim_strsave((char_u
*)".");
22531 *bufp
= *fnamep
= tail
= p
;
22536 while (tail
> s
&& !after_pathsep(s
, tail
))
22537 mb_ptr_back(*fnamep
, tail
);
22541 /* ":8" - shortname */
22542 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
22551 /* Check shortname after we have done 'heads' and before we do 'tails'
22556 /* Copy the string if it is shortened by :h */
22557 if (*fnamelen
< (int)STRLEN(*fnamep
))
22559 p
= vim_strnsave(*fnamep
, *fnamelen
);
22563 *bufp
= *fnamep
= p
;
22566 /* Split into two implementations - makes it easier. First is where
22567 * there isn't a full name already, second is where there is.
22569 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
22571 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == FAIL
)
22578 /* Simple case, already have the full-name
22579 * Nearly always shorter, so try first time. */
22581 if (get_short_pathname(fnamep
, bufp
, &l
) == FAIL
)
22586 /* Couldn't find the filename.. search the paths.
22589 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == FAIL
)
22595 #endif /* WIN3264 */
22597 /* ":t" - tail, just the basename */
22598 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
22601 *fnamelen
-= (int)(tail
- *fnamep
);
22605 /* ":e" - extension, can be repeated */
22606 /* ":r" - root, without extension, can be repeated */
22607 while (src
[*usedlen
] == ':'
22608 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
22610 /* find a '.' in the tail:
22611 * - for second :e: before the current fname
22612 * - otherwise: The last '.'
22614 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
22617 s
= *fnamep
+ *fnamelen
- 1;
22618 for ( ; s
> tail
; --s
)
22621 if (src
[*usedlen
+ 1] == 'e') /* :e */
22625 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
22628 /* cut version from the extension */
22629 s
= *fnamep
+ *fnamelen
- 1;
22630 for ( ; s
> *fnamep
; --s
)
22634 *fnamelen
= s
- *fnamep
;
22637 else if (*fnamep
<= tail
)
22642 if (s
> tail
) /* remove one extension */
22643 *fnamelen
= (int)(s
- *fnamep
);
22648 /* ":s?pat?foo?" - substitute */
22649 /* ":gs?pat?foo?" - global substitute */
22650 if (src
[*usedlen
] == ':'
22651 && (src
[*usedlen
+ 1] == 's'
22652 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
22661 flags
= (char_u
*)"";
22662 s
= src
+ *usedlen
+ 2;
22663 if (src
[*usedlen
+ 1] == 'g')
22665 flags
= (char_u
*)"g";
22672 /* find end of pattern */
22673 p
= vim_strchr(s
, sep
);
22676 pat
= vim_strnsave(s
, (int)(p
- s
));
22680 /* find end of substitution */
22681 p
= vim_strchr(s
, sep
);
22684 sub
= vim_strnsave(s
, (int)(p
- s
));
22685 str
= vim_strnsave(*fnamep
, *fnamelen
);
22686 if (sub
!= NULL
&& str
!= NULL
)
22688 *usedlen
= (int)(p
+ 1 - src
);
22689 s
= do_string_sub(str
, pat
, sub
, flags
);
22693 *fnamelen
= (int)STRLEN(s
);
22705 /* after using ":s", repeat all the modifiers */
22715 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22716 * "flags" can be "g" to do a global substitute.
22717 * Returns an allocated string, NULL for error.
22720 do_string_sub(str
, pat
, sub
, flags
)
22727 regmatch_T regmatch
;
22735 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22737 p_cpo
= empty_option
;
22739 ga_init2(&ga
, 1, 200);
22741 do_all
= (flags
[0] == 'g');
22743 regmatch
.rm_ic
= p_ic
;
22744 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
22745 if (regmatch
.regprog
!= NULL
)
22748 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
22751 * Get some space for a temporary buffer to do the substitution
22752 * into. It will contain:
22753 * - The text up to where the match is.
22754 * - The substituted text.
22755 * - The text after the match.
22757 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
22758 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
22759 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
22765 /* copy the text up to where the match is */
22766 i
= (int)(regmatch
.startp
[0] - tail
);
22767 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
22768 /* add the substituted text */
22769 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
22770 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
22771 ga
.ga_len
+= i
+ sublen
- 1;
22772 /* avoid getting stuck on a match with an empty string */
22773 if (tail
== regmatch
.endp
[0])
22777 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
22782 tail
= regmatch
.endp
[0];
22790 if (ga
.ga_data
!= NULL
)
22791 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
22793 vim_free(regmatch
.regprog
);
22796 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
22798 if (p_cpo
== empty_option
)
22801 /* Darn, evaluating {sub} expression changed the value. */
22802 free_string_option(save_cpo
);
22807 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */