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(MSWIN)
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 */
36 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
38 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 static dictitem_T dumdi
;
43 #define DI2HIKEY(di) ((di)->di_key)
44 #define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
45 #define HI2DI(hi) HIKEY2DI((hi)->hi_key)
48 * Structure returned by get_lval() and used by set_var_lval().
50 * "name" points to the variable name.
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
71 * "tv" points to the Dictionary typval_T
72 * "newkey" is the key for the new item.
76 char_u
*ll_name
; /* start of variable name (can be NULL) */
77 char_u
*ll_exp_name
; /* NULL or expanded name in allocated memory. */
78 typval_T
*ll_tv
; /* Typeval of item being used. If "newkey"
79 isn't NULL it's the Dict to which to add
81 listitem_T
*ll_li
; /* The list item or NULL. */
82 list_T
*ll_list
; /* The list or NULL. */
83 int ll_range
; /* TRUE when a [i:j] range was used */
84 long ll_n1
; /* First index for list */
85 long ll_n2
; /* Second index for list range */
86 int ll_empty2
; /* Second index is empty: [i:] */
87 dict_T
*ll_dict
; /* The Dictionary or NULL */
88 dictitem_T
*ll_di
; /* The dictitem or NULL */
89 char_u
*ll_newkey
; /* New key for Dict in alloc. mem or NULL. */
93 static char *e_letunexp
= N_("E18: Unexpected characters in :let");
94 static char *e_listidx
= N_("E684: list index out of range: %ld");
95 static char *e_undefvar
= N_("E121: Undefined variable: %s");
96 static char *e_missbrac
= N_("E111: Missing ']'");
97 static char *e_listarg
= N_("E686: Argument of %s must be a List");
98 static char *e_listdictarg
= N_("E712: Argument of %s must be a List or Dictionary");
99 static char *e_emptykey
= N_("E713: Cannot use empty key for Dictionary");
100 static char *e_listreq
= N_("E714: List required");
101 static char *e_dictreq
= N_("E715: Dictionary required");
102 static char *e_toomanyarg
= N_("E118: Too many arguments for function: %s");
103 static char *e_dictkey
= N_("E716: Key not present in Dictionary: %s");
104 static char *e_funcexts
= N_("E122: Function %s already exists, add ! to replace it");
105 static char *e_funcdict
= N_("E717: Dictionary entry already exists");
106 static char *e_funcref
= N_("E718: Funcref required");
107 static char *e_dictrange
= N_("E719: Cannot use [:] with a Dictionary");
108 static char *e_letwrong
= N_("E734: Wrong variable type for %s=");
109 static char *e_nofunc
= N_("E130: Unknown function: %s");
110 static char *e_illvar
= N_("E461: Illegal variable name: %s");
113 * All user-defined global variables are stored in dictionary "globvardict".
114 * "globvars_var" is the variable that is used for "g:".
116 static dict_T globvardict
;
117 static dictitem_T globvars_var
;
118 #define globvarht globvardict.dv_hashtab
121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
124 static hashtab_T compat_hashtab
;
127 * When recursively copying lists and dicts we need to remember which ones we
128 * have done to avoid endless recursiveness. This unique ID is used for that.
130 static int current_copyID
= 0;
133 * Array to hold the hashtab with variables local to each sourced script.
134 * Each item holds a variable (nameless) that points to the dict_T.
142 static garray_T ga_scripts
= {0, 0, sizeof(scriptvar_T
), 4, NULL
};
143 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
144 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
146 static int echo_attr
= 0; /* attributes used for ":echo" */
148 /* Values for trans_function_name() argument: */
149 #define TFN_INT 1 /* internal function name OK */
150 #define TFN_QUIET 2 /* no error messages */
153 * Structure to hold info for a user function.
155 typedef struct ufunc ufunc_T
;
159 int uf_varargs
; /* variable nr of arguments */
161 int uf_calls
; /* nr of active calls */
162 garray_T uf_args
; /* arguments */
163 garray_T uf_lines
; /* function lines */
165 int uf_profiling
; /* TRUE when func is being profiled */
166 /* profiling the function as a whole */
167 int uf_tm_count
; /* nr of calls */
168 proftime_T uf_tm_total
; /* time spent in function + children */
169 proftime_T uf_tm_self
; /* time spent in function itself */
170 proftime_T uf_tm_children
; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count
; /* nr of times line was executed */
173 proftime_T
*uf_tml_total
; /* time spent in a line + children */
174 proftime_T
*uf_tml_self
; /* time spent in a line itself */
175 proftime_T uf_tml_start
; /* start time for current line */
176 proftime_T uf_tml_children
; /* time spent in children for this line */
177 proftime_T uf_tml_wait
; /* start wait time for current line */
178 int uf_tml_idx
; /* index of line being timed; -1 if none */
179 int uf_tml_execed
; /* line being timed was executed */
181 scid_T uf_script_ID
; /* ID of script where function was defined,
182 used for s: variables */
183 int uf_refcount
; /* for numbered function: reference count */
184 char_u uf_name
[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
190 #define FC_ABORT 1 /* abort function on error */
191 #define FC_RANGE 2 /* function accepts range */
192 #define FC_DICT 4 /* Dict function, uses "self" */
195 * All user-defined functions are found in this hashtable.
197 static hashtab_T func_hashtab
;
199 /* The names of packages that once were loaded are remembered. */
200 static garray_T ga_loaded
= {0, 0, sizeof(char_u
*), 4, NULL
};
202 /* list heads for garbage collection */
203 static dict_T
*first_dict
= NULL
; /* list of all dicts */
204 static list_T
*first_list
= NULL
; /* list of all lists */
206 /* From user function to hashitem and back. */
207 static ufunc_T dumuf
;
208 #define UF2HIKEY(fp) ((fp)->uf_name)
209 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
210 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
212 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
213 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
215 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
216 #define VAR_SHORT_LEN 20 /* short variable name length */
217 #define FIXVAR_CNT 12 /* number of fixed variables */
219 /* structure to hold info for a function that is currently being executed. */
220 typedef struct funccall_S funccall_T
;
224 ufunc_T
*func
; /* function being called */
225 int linenr
; /* next line to be executed */
226 int returned
; /* ":return" used */
227 struct /* fixed variables for arguments */
229 dictitem_T var
; /* variable (without room for name) */
230 char_u room
[VAR_SHORT_LEN
]; /* room for the name */
231 } fixvar
[FIXVAR_CNT
];
232 dict_T l_vars
; /* l: local function variables */
233 dictitem_T l_vars_var
; /* variable for l: scope */
234 dict_T l_avars
; /* a: argument variables */
235 dictitem_T l_avars_var
; /* variable for a: scope */
236 list_T l_varlist
; /* list for a:000 */
237 listitem_T l_listitems
[MAX_FUNC_ARGS
]; /* listitems for a:000 */
238 typval_T
*rettv
; /* return value */
239 linenr_T breakpoint
; /* next line with breakpoint or zero */
240 int dbg_tick
; /* debug_tick when breakpoint was set */
241 int level
; /* top nesting level of executed function */
243 proftime_T prof_child
; /* time spent in a child */
245 funccall_T
*caller
; /* calling function or NULL */
249 * Info used by a ":for" loop.
253 int fi_semicolon
; /* TRUE if ending in '; var]' */
254 int fi_varcount
; /* nr of variables in the list */
255 listwatch_T fi_lw
; /* keep an eye on the item used. */
256 list_T
*fi_list
; /* list being used */
260 * Struct used by trans_function_name()
264 dict_T
*fd_dict
; /* Dictionary used */
265 char_u
*fd_newkey
; /* new key in "dict" in allocated memory */
266 dictitem_T
*fd_di
; /* Dictionary item used */
271 * Array to hold the value of v: variables.
272 * The value is in a dictitem, so that it can also be used in the v: scope.
273 * The reason to use this table anyway is for very quick access to the
274 * variables with the VV_ defines.
278 /* values for vv_flags: */
279 #define VV_COMPAT 1 /* compatible, also used without "v:" */
280 #define VV_RO 2 /* read-only */
281 #define VV_RO_SBX 4 /* read-only in the sandbox */
283 #define VV_NAME(s, t) s, {{t}}, {0}
287 char *vv_name
; /* name of variable, without v: */
288 dictitem_T vv_di
; /* value and name for key */
289 char vv_filler
[16]; /* space for LONGEST name below!!! */
290 char vv_flags
; /* VV_COMPAT, VV_RO, VV_RO_SBX */
294 * The order here must match the VV_ defines in vim.h!
295 * Initializing a union does not work, leave tv.vval empty to get zero's.
297 {VV_NAME("count", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
298 {VV_NAME("count1", VAR_NUMBER
), VV_RO
},
299 {VV_NAME("prevcount", VAR_NUMBER
), VV_RO
},
300 {VV_NAME("errmsg", VAR_STRING
), VV_COMPAT
},
301 {VV_NAME("warningmsg", VAR_STRING
), 0},
302 {VV_NAME("statusmsg", VAR_STRING
), 0},
303 {VV_NAME("shell_error", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
304 {VV_NAME("this_session", VAR_STRING
), VV_COMPAT
},
305 {VV_NAME("version", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
306 {VV_NAME("lnum", VAR_NUMBER
), VV_RO_SBX
},
307 {VV_NAME("termresponse", VAR_STRING
), VV_RO
},
308 {VV_NAME("fname", VAR_STRING
), VV_RO
},
309 {VV_NAME("lang", VAR_STRING
), VV_RO
},
310 {VV_NAME("lc_time", VAR_STRING
), VV_RO
},
311 {VV_NAME("ctype", VAR_STRING
), VV_RO
},
312 {VV_NAME("charconvert_from", VAR_STRING
), VV_RO
},
313 {VV_NAME("charconvert_to", VAR_STRING
), VV_RO
},
314 {VV_NAME("fname_in", VAR_STRING
), VV_RO
},
315 {VV_NAME("fname_out", VAR_STRING
), VV_RO
},
316 {VV_NAME("fname_new", VAR_STRING
), VV_RO
},
317 {VV_NAME("fname_diff", VAR_STRING
), VV_RO
},
318 {VV_NAME("cmdarg", VAR_STRING
), VV_RO
},
319 {VV_NAME("foldstart", VAR_NUMBER
), VV_RO_SBX
},
320 {VV_NAME("foldend", VAR_NUMBER
), VV_RO_SBX
},
321 {VV_NAME("folddashes", VAR_STRING
), VV_RO_SBX
},
322 {VV_NAME("foldlevel", VAR_NUMBER
), VV_RO_SBX
},
323 {VV_NAME("progname", VAR_STRING
), VV_RO
},
324 {VV_NAME("servername", VAR_STRING
), VV_RO
},
325 {VV_NAME("dying", VAR_NUMBER
), VV_RO
},
326 {VV_NAME("exception", VAR_STRING
), VV_RO
},
327 {VV_NAME("throwpoint", VAR_STRING
), VV_RO
},
328 {VV_NAME("register", VAR_STRING
), VV_RO
},
329 {VV_NAME("cmdbang", VAR_NUMBER
), VV_RO
},
330 {VV_NAME("insertmode", VAR_STRING
), VV_RO
},
331 {VV_NAME("val", VAR_UNKNOWN
), VV_RO
},
332 {VV_NAME("key", VAR_UNKNOWN
), VV_RO
},
333 {VV_NAME("profiling", VAR_NUMBER
), VV_RO
},
334 {VV_NAME("fcs_reason", VAR_STRING
), VV_RO
},
335 {VV_NAME("fcs_choice", VAR_STRING
), 0},
336 {VV_NAME("beval_bufnr", VAR_NUMBER
), VV_RO
},
337 {VV_NAME("beval_winnr", VAR_NUMBER
), VV_RO
},
338 {VV_NAME("beval_lnum", VAR_NUMBER
), VV_RO
},
339 {VV_NAME("beval_col", VAR_NUMBER
), VV_RO
},
340 {VV_NAME("beval_text", VAR_STRING
), VV_RO
},
341 {VV_NAME("scrollstart", VAR_STRING
), 0},
342 {VV_NAME("swapname", VAR_STRING
), VV_RO
},
343 {VV_NAME("swapchoice", VAR_STRING
), 0},
344 {VV_NAME("swapcommand", VAR_STRING
), VV_RO
},
345 {VV_NAME("char", VAR_STRING
), VV_RO
},
346 {VV_NAME("mouse_win", VAR_NUMBER
), 0},
347 {VV_NAME("mouse_lnum", VAR_NUMBER
), 0},
348 {VV_NAME("mouse_col", VAR_NUMBER
), 0},
349 {VV_NAME("operator", VAR_STRING
), VV_RO
},
350 {VV_NAME("searchforward", VAR_NUMBER
), 0},
354 #define vv_type vv_di.di_tv.v_type
355 #define vv_nr vv_di.di_tv.vval.v_number
356 #define vv_float vv_di.di_tv.vval.v_float
357 #define vv_str vv_di.di_tv.vval.v_string
358 #define vv_tv vv_di.di_tv
361 * The v: variables are stored in dictionary "vimvardict".
362 * "vimvars_var" is the variable that is used for the "l:" scope.
364 static dict_T vimvardict
;
365 static dictitem_T vimvars_var
;
366 #define vimvarht vimvardict.dv_hashtab
368 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
369 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
370 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
371 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
373 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
374 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
375 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
376 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
, int *first
));
377 static void list_glob_vars
__ARGS((int *first
));
378 static void list_buf_vars
__ARGS((int *first
));
379 static void list_win_vars
__ARGS((int *first
));
381 static void list_tab_vars
__ARGS((int *first
));
383 static void list_vim_vars
__ARGS((int *first
));
384 static void list_script_vars
__ARGS((int *first
));
385 static void list_func_vars
__ARGS((int *first
));
386 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
, int *first
));
387 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
388 static int check_changedtick
__ARGS((char_u
*arg
));
389 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
390 static void clear_lval
__ARGS((lval_T
*lp
));
391 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
392 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
393 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
394 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
395 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
396 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
397 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
398 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
399 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
400 static int tv_islocked
__ARGS((typval_T
*tv
));
402 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
403 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
404 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
405 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
406 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
407 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
408 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
409 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
411 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
412 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
413 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
414 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
415 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
416 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
417 static listitem_T
*listitem_alloc
__ARGS((void));
418 static void listitem_free
__ARGS((listitem_T
*item
));
419 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
420 static long list_len
__ARGS((list_T
*l
));
421 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
422 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
423 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
424 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
425 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
426 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
427 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
428 static int list_append_tv
__ARGS((list_T
*l
, typval_T
*tv
));
429 static int list_append_string
__ARGS((list_T
*l
, char_u
*str
, int len
));
430 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
431 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
432 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
433 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
434 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
435 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
436 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
437 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
438 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
439 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
440 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
441 static void dict_unref
__ARGS((dict_T
*d
));
442 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
443 static dictitem_T
*dictitem_alloc
__ARGS((char_u
*key
));
444 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
445 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
446 static void dictitem_free
__ARGS((dictitem_T
*item
));
447 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
448 static int dict_add
__ARGS((dict_T
*d
, dictitem_T
*item
));
449 static long dict_len
__ARGS((dict_T
*d
));
450 static dictitem_T
*dict_find
__ARGS((dict_T
*d
, char_u
*key
, int len
));
451 static char_u
*dict2string
__ARGS((typval_T
*tv
, int copyID
));
452 static int get_dict_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
453 static char_u
*echo_string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
454 static char_u
*tv2string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
455 static char_u
*string_quote
__ARGS((char_u
*str
, int function
));
457 static int string2float
__ARGS((char_u
*text
, float_T
*value
));
459 static int get_env_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
460 static int find_internal_func
__ARGS((char_u
*name
));
461 static char_u
*deref_func_name
__ARGS((char_u
*name
, int *lenp
));
462 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
));
463 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
));
464 static void emsg_funcname
__ARGS((char *ermsg
, char_u
*name
));
467 static void f_abs
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
469 static void f_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
470 static void f_append
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
471 static void f_argc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
472 static void f_argidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
473 static void f_argv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
475 static void f_atan
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
477 static void f_browse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
478 static void f_browsedir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
479 static void f_bufexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
480 static void f_buflisted
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
481 static void f_bufloaded
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
482 static void f_bufname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
483 static void f_bufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
484 static void f_bufwinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
485 static void f_byte2line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
486 static void f_byteidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
487 static void f_call
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
489 static void f_ceil
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
491 static void f_changenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
492 static void f_char2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
493 static void f_cindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
494 static void f_clearmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
495 static void f_col
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
496 #if defined(FEAT_INS_EXPAND)
497 static void f_complete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
498 static void f_complete_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
499 static void f_complete_check
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
501 static void f_confirm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
502 static void f_copy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
504 static void f_cos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
506 static void f_count
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
507 static void f_cscope_connection
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
508 static void f_cursor
__ARGS((typval_T
*argsvars
, typval_T
*rettv
));
509 static void f_deepcopy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
510 static void f_delete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
511 static void f_did_filetype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
512 static void f_diff_filler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
513 static void f_diff_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
514 static void f_empty
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
515 static void f_escape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
516 static void f_eval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
517 static void f_eventhandler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
518 static void f_executable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
519 static void f_exists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
520 static void f_expand
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
521 static void f_extend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
522 static void f_feedkeys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
523 static void f_filereadable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
524 static void f_filewritable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
525 static void f_filter
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
526 static void f_finddir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
527 static void f_findfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
529 static void f_float2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
530 static void f_floor
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
532 static void f_fnameescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
533 static void f_fnamemodify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
534 static void f_foldclosed
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
535 static void f_foldclosedend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
536 static void f_foldlevel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
537 static void f_foldtext
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
538 static void f_foldtextresult
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
539 static void f_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
540 static void f_function
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
541 static void f_garbagecollect
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
542 static void f_get
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
543 static void f_getbufline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
544 static void f_getbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
545 static void f_getchar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
546 static void f_getcharmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
547 static void f_getcmdline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
548 static void f_getcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
549 static void f_getcmdtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
550 static void f_getcwd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
551 static void f_getfontname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
552 static void f_getfperm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
553 static void f_getfsize
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
554 static void f_getftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
555 static void f_getftype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
556 static void f_getline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
557 static void f_getmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
558 static void f_getpid
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
559 static void f_getpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
560 static void f_getqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
561 static void f_getreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
562 static void f_getregtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
563 static void f_gettabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
564 static void f_getwinposx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
565 static void f_getwinposy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
566 static void f_getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
567 static void f_glob
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
568 static void f_globpath
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
569 static void f_has
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
570 static void f_has_key
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
571 static void f_haslocaldir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
572 static void f_hasmapto
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
573 static void f_histadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
574 static void f_histdel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
575 static void f_histget
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
576 static void f_histnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
577 static void f_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
578 static void f_hlexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
579 static void f_hostname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
580 static void f_iconv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
581 static void f_indent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
582 static void f_index
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
583 static void f_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
584 static void f_inputdialog
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
585 static void f_inputlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
586 static void f_inputrestore
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
587 static void f_inputsave
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
588 static void f_inputsecret
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
589 static void f_insert
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
590 static void f_isdirectory
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
591 static void f_islocked
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
592 static void f_items
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
593 static void f_join
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
594 static void f_keys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
595 static void f_last_buffer_nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
596 static void f_len
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
597 static void f_libcall
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
598 static void f_libcallnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
599 static void f_line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
600 static void f_line2byte
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
601 static void f_lispindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
602 static void f_localtime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
604 static void f_log10
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
606 static void f_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
607 static void f_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
608 static void f_mapcheck
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
609 static void f_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
610 static void f_matchadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
611 static void f_matcharg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
612 static void f_matchdelete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
613 static void f_matchend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
614 static void f_matchlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
615 static void f_matchstr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
616 static void f_max
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
617 static void f_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
619 static void f_mkdir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
621 static void f_mode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
622 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
623 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
624 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
626 static void f_pow
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
628 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
629 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
630 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
631 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
632 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
633 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
634 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
635 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
636 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
637 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
638 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
639 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
640 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
641 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
642 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
643 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
644 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_round
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
648 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
650 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
652 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
654 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
656 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_setmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
665 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
668 static void f_sin
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
670 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
672 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
673 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
674 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
676 static void f_sqrt
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
677 static void f_str2float
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
679 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
681 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
683 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
684 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
685 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
686 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
687 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
688 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
689 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
690 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
691 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
692 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
693 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
694 static void f_synstack
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
695 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
696 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
697 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
698 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
699 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
700 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
701 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
702 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
703 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
704 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
705 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
707 static void f_trunc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
709 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
710 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
711 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
712 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
713 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
714 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
715 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
716 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
717 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
718 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
719 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
720 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
721 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
722 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
724 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
725 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int dollar_lnum
, int *fnum
));
726 static int get_env_len
__ARGS((char_u
**arg
));
727 static int get_id_len
__ARGS((char_u
**arg
));
728 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
729 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
730 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
731 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
733 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
734 static int eval_isnamec
__ARGS((int c
));
735 static int eval_isnamec1
__ARGS((int c
));
736 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
737 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
738 static typval_T
*alloc_tv
__ARGS((void));
739 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
740 static void init_tv
__ARGS((typval_T
*varp
));
741 static long get_tv_number
__ARGS((typval_T
*varp
));
742 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
743 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
744 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
745 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
746 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
747 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
748 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
749 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
750 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
751 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
752 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
, int *first
));
753 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
, int *first
));
754 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
755 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
756 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
757 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
758 static void copy_tv
__ARGS((typval_T
*from
, typval_T
*to
));
759 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
760 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
761 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
762 static int eval_fname_script
__ARGS((char_u
*p
));
763 static int eval_fname_sid
__ARGS((char_u
*p
));
764 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
765 static ufunc_T
*find_func
__ARGS((char_u
*name
));
766 static int function_exists
__ARGS((char_u
*name
));
767 static int builtin_function
__ARGS((char_u
*name
));
769 static void func_do_profile
__ARGS((ufunc_T
*fp
));
770 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
771 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
776 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
781 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
783 static int script_autoload
__ARGS((char_u
*name
, int reload
));
784 static char_u
*autoload_name
__ARGS((char_u
*name
));
785 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
786 static void func_free
__ARGS((ufunc_T
*fp
));
787 static void func_unref
__ARGS((char_u
*name
));
788 static void func_ref
__ARGS((char_u
*name
));
789 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
));
790 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
791 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
792 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
793 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
794 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
795 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
797 /* Character used as separated in autoload function/variable names. */
798 #define AUTOLOAD_CHAR '#'
801 * Initialize the global and v: variables.
809 init_var_dict(&globvardict
, &globvars_var
);
810 init_var_dict(&vimvardict
, &vimvars_var
);
811 hash_init(&compat_hashtab
);
812 hash_init(&func_hashtab
);
814 for (i
= 0; i
< VV_LEN
; ++i
)
817 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
818 if (p
->vv_flags
& VV_RO
)
819 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
820 else if (p
->vv_flags
& VV_RO_SBX
)
821 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
823 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
825 /* add to v: scope dict, unless the value is not always available */
826 if (p
->vv_type
!= VAR_UNKNOWN
)
827 hash_add(&vimvarht
, p
->vv_di
.di_key
);
828 if (p
->vv_flags
& VV_COMPAT
)
829 /* add to compat scope dict */
830 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
832 set_vim_var_nr(VV_SEARCHFORWARD
, 1L);
835 #if defined(EXITFREE) || defined(PROTO)
842 for (i
= 0; i
< VV_LEN
; ++i
)
845 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
847 vim_free(p
->vv_di
.di_tv
.vval
.v_string
);
848 p
->vv_di
.di_tv
.vval
.v_string
= NULL
;
851 hash_clear(&vimvarht
);
852 hash_clear(&compat_hashtab
);
854 /* script-local variables */
855 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
856 vars_clear(&SCRIPT_VARS(i
));
857 ga_clear(&ga_scripts
);
860 /* global variables */
861 vars_clear(&globvarht
);
863 /* autoloaded script names */
864 ga_clear_strings(&ga_loaded
);
866 /* unreferenced lists and dicts */
867 (void)garbage_collect();
870 free_all_functions();
871 hash_clear(&func_hashtab
);
876 * Return the name of the executed function.
882 return ((funccall_T
*)cookie
)->func
->uf_name
;
886 * Return the address holding the next breakpoint line for a funccall cookie.
889 func_breakpoint(cookie
)
892 return &((funccall_T
*)cookie
)->breakpoint
;
896 * Return the address holding the debug tick for a funccall cookie.
899 func_dbg_tick(cookie
)
902 return &((funccall_T
*)cookie
)->dbg_tick
;
906 * Return the nesting level for a funccall cookie.
912 return ((funccall_T
*)cookie
)->level
;
915 /* pointer to funccal for currently active function */
916 funccall_T
*current_funccal
= NULL
;
919 * Return TRUE when a function was ended by a ":return" command.
922 current_func_returned()
924 return current_funccal
->returned
;
929 * Set an internal variable to a string value. Creates the variable if it does
933 set_internal_string_var(name
, value
)
940 val
= vim_strsave(value
);
943 tvp
= alloc_string_tv(val
);
946 set_var(name
, tvp
, FALSE
);
952 static lval_T
*redir_lval
= NULL
;
953 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
954 static char_u
*redir_endp
= NULL
;
955 static char_u
*redir_varname
= NULL
;
958 * Start recording command output to a variable
959 * Returns OK if successfully completed the setup. FAIL otherwise.
962 var_redir_start(name
, append
)
964 int append
; /* append to an existing variable */
970 /* Make sure a valid variable name is specified */
971 if (!eval_isnamec1(*name
))
977 redir_varname
= vim_strsave(name
);
978 if (redir_varname
== NULL
)
981 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
982 if (redir_lval
== NULL
)
988 /* The output is stored in growarray "redir_ga" until redirection ends. */
989 ga_init2(&redir_ga
, (int)sizeof(char), 500);
991 /* Parse the variable name (can be a dict or list entry). */
992 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
994 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
996 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
997 /* Trailing characters are present after the variable name */
1005 /* check if we can write to the variable: set it to or append an empty
1007 save_emsg
= did_emsg
;
1009 tv
.v_type
= VAR_STRING
;
1010 tv
.vval
.v_string
= (char_u
*)"";
1012 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
1014 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
1016 did_emsg
|= save_emsg
;
1022 if (redir_lval
->ll_newkey
!= NULL
)
1024 /* Dictionary item was created, don't do it again. */
1025 vim_free(redir_lval
->ll_newkey
);
1026 redir_lval
->ll_newkey
= NULL
;
1033 * Append "value[value_len]" to the variable set by var_redir_start().
1034 * The actual appending is postponed until redirection ends, because the value
1035 * appended may in fact be the string we write to, changing it may cause freed
1036 * memory to be used:
1042 var_redir_str(value
, value_len
)
1048 if (redir_lval
== NULL
)
1051 if (value_len
== -1)
1052 len
= (int)STRLEN(value
); /* Append the entire string */
1054 len
= value_len
; /* Append only "value_len" characters */
1056 if (ga_grow(&redir_ga
, len
) == OK
)
1058 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1059 redir_ga
.ga_len
+= len
;
1066 * Stop redirecting command output to a variable.
1073 if (redir_lval
!= NULL
)
1075 /* Append the trailing NUL. */
1076 ga_append(&redir_ga
, NUL
);
1078 /* Assign the text to the variable. */
1079 tv
.v_type
= VAR_STRING
;
1080 tv
.vval
.v_string
= redir_ga
.ga_data
;
1081 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1082 vim_free(tv
.vval
.v_string
);
1084 clear_lval(redir_lval
);
1085 vim_free(redir_lval
);
1088 vim_free(redir_varname
);
1089 redir_varname
= NULL
;
1092 # if defined(FEAT_MBYTE) || defined(PROTO)
1094 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1102 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1103 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1104 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1105 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1106 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1108 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1109 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1110 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1111 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1119 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1121 eval_printexpr(fname
, args
)
1127 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1128 set_vim_var_string(VV_CMDARG
, args
, -1);
1129 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1131 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1132 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1143 # if defined(FEAT_DIFF) || defined(PROTO)
1145 eval_diff(origfile
, newfile
, outfile
)
1152 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1153 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1154 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1155 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1156 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1157 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1158 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1162 eval_patch(origfile
, difffile
, outfile
)
1169 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1170 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1171 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1172 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1173 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1174 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1175 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1180 * Top level evaluation function, returning a boolean.
1181 * Sets "error" to TRUE if there was an error.
1182 * Return TRUE or FALSE.
1185 eval_to_bool(arg
, error
, nextcmd
, skip
)
1189 int skip
; /* only parse, don't execute */
1196 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1203 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1214 * Top level evaluation function, returning a string. If "skip" is TRUE,
1215 * only parsing to "nextcmd" is done, without reporting errors. Return
1216 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1219 eval_to_string_skip(arg
, nextcmd
, skip
)
1222 int skip
; /* only parse, don't execute */
1229 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1233 retval
= vim_strsave(get_tv_string(&tv
));
1243 * Skip over an expression at "*pp".
1244 * Return FAIL for an error, OK otherwise.
1252 *pp
= skipwhite(*pp
);
1253 return eval1(pp
, &rettv
, FALSE
);
1257 * Top level evaluation function, returning a string.
1258 * Return pointer to allocated memory, or NULL for failure.
1261 eval_to_string(arg
, nextcmd
, dolist
)
1264 int dolist
; /* turn List into sequence of lines */
1270 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1274 if (dolist
&& tv
.v_type
== VAR_LIST
)
1276 ga_init2(&ga
, (int)sizeof(char), 80);
1277 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1278 ga_append(&ga
, NUL
);
1279 retval
= (char_u
*)ga
.ga_data
;
1282 retval
= vim_strsave(get_tv_string(&tv
));
1290 * Call eval_to_string() without using current local variables and using
1291 * textlock. When "use_sandbox" is TRUE use the sandbox.
1294 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1300 void *save_funccalp
;
1302 save_funccalp
= save_funccal();
1306 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1310 restore_funccal(save_funccalp
);
1315 * Top level evaluation function, returning a number.
1316 * Evaluates "expr" silently.
1317 * Returns -1 for an error.
1320 eval_to_number(expr
)
1325 char_u
*p
= skipwhite(expr
);
1329 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1333 retval
= get_tv_number_chk(&rettv
, NULL
);
1342 * Prepare v: variable "idx" to be used.
1343 * Save the current typeval in "save_tv".
1344 * When not used yet add the variable to the v: hashtable.
1347 prepare_vimvar(idx
, save_tv
)
1351 *save_tv
= vimvars
[idx
].vv_tv
;
1352 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1353 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1357 * Restore v: variable "idx" to typeval "save_tv".
1358 * When no longer defined, remove the variable from the v: hashtable.
1361 restore_vimvar(idx
, save_tv
)
1367 vimvars
[idx
].vv_tv
= *save_tv
;
1368 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1370 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1371 if (HASHITEM_EMPTY(hi
))
1372 EMSG2(_(e_intern2
), "restore_vimvar()");
1374 hash_remove(&vimvarht
, hi
);
1378 #if defined(FEAT_SPELL) || defined(PROTO)
1380 * Evaluate an expression to a list with suggestions.
1381 * For the "expr:" part of 'spellsuggest'.
1384 eval_spell_expr(badword
, expr
)
1390 list_T
*list
= NULL
;
1391 char_u
*p
= skipwhite(expr
);
1393 /* Set "v:val" to the bad word. */
1394 prepare_vimvar(VV_VAL
, &save_val
);
1395 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1396 vimvars
[VV_VAL
].vv_str
= badword
;
1400 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1402 if (rettv
.v_type
!= VAR_LIST
)
1405 list
= rettv
.vval
.v_list
;
1410 restore_vimvar(VV_VAL
, &save_val
);
1416 * "list" is supposed to contain two items: a word and a number. Return the
1417 * word in "pp" and the number as the return value.
1418 * Return -1 if anything isn't right.
1419 * Used to get the good word and score from the eval_spell_expr() result.
1422 get_spellword(list
, pp
)
1428 li
= list
->lv_first
;
1431 *pp
= get_tv_string(&li
->li_tv
);
1436 return get_tv_number(&li
->li_tv
);
1441 * Top level evaluation function.
1442 * Returns an allocated typval_T with the result.
1443 * Returns NULL when there is an error.
1446 eval_expr(arg
, nextcmd
)
1452 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1453 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1463 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1464 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1466 * Call some vimL function and return the result in "*rettv".
1467 * Uses argv[argc] for the function arguments. Only Number and String
1468 * arguments are currently supported.
1469 * Returns OK or FAIL.
1472 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1476 int safe
; /* use the sandbox */
1484 void *save_funccalp
= NULL
;
1487 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1488 if (argvars
== NULL
)
1491 for (i
= 0; i
< argc
; i
++)
1493 /* Pass a NULL or empty argument as an empty string */
1494 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1496 argvars
[i
].v_type
= VAR_STRING
;
1497 argvars
[i
].vval
.v_string
= (char_u
*)"";
1501 /* Recognize a number argument, the others must be strings. */
1502 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1503 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1505 argvars
[i
].v_type
= VAR_NUMBER
;
1506 argvars
[i
].vval
.v_number
= n
;
1510 argvars
[i
].v_type
= VAR_STRING
;
1511 argvars
[i
].vval
.v_string
= argv
[i
];
1517 save_funccalp
= save_funccal();
1521 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1522 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1523 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1524 &doesrange
, TRUE
, NULL
);
1528 restore_funccal(save_funccalp
);
1538 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1540 * Call vimL function "func" and return the result as a string.
1541 * Returns NULL when calling the function fails.
1542 * Uses argv[argc] for the function arguments.
1545 call_func_retstr(func
, argc
, argv
, safe
)
1549 int safe
; /* use the sandbox */
1554 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1557 retval
= vim_strsave(get_tv_string(&rettv
));
1563 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1565 * Call vimL function "func" and return the result as a number.
1566 * Returns -1 when calling the function fails.
1567 * Uses argv[argc] for the function arguments.
1570 call_func_retnr(func
, argc
, argv
, safe
)
1574 int safe
; /* use the sandbox */
1579 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1582 retval
= get_tv_number_chk(&rettv
, NULL
);
1589 * Call vimL function "func" and return the result as a list
1590 * Uses argv[argc] for the function arguments.
1593 call_func_retlist(func
, argc
, argv
, safe
)
1597 int safe
; /* use the sandbox */
1601 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1604 if (rettv
.v_type
!= VAR_LIST
)
1610 return rettv
.vval
.v_list
;
1616 * Save the current function call pointer, and set it to NULL.
1617 * Used when executing autocommands and for ":source".
1622 funccall_T
*fc
= current_funccal
;
1624 current_funccal
= NULL
;
1629 restore_funccal(vfc
)
1632 funccall_T
*fc
= (funccall_T
*)vfc
;
1634 current_funccal
= fc
;
1637 #if defined(FEAT_PROFILE) || defined(PROTO)
1639 * Prepare profiling for entering a child or something else that is not
1640 * counted for the script/function itself.
1641 * Should always be called in pair with prof_child_exit().
1644 prof_child_enter(tm
)
1645 proftime_T
*tm
; /* place to store waittime */
1647 funccall_T
*fc
= current_funccal
;
1649 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1650 profile_start(&fc
->prof_child
);
1651 script_prof_save(tm
);
1655 * Take care of time spent in a child.
1656 * Should always be called after prof_child_enter().
1660 proftime_T
*tm
; /* where waittime was stored */
1662 funccall_T
*fc
= current_funccal
;
1664 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1666 profile_end(&fc
->prof_child
);
1667 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1668 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1669 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1671 script_prof_restore(tm
);
1678 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1679 * it in "*cp". Doesn't give error messages.
1682 eval_foldexpr(arg
, cp
)
1689 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1697 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1701 /* If the result is a number, just return the number. */
1702 if (tv
.v_type
== VAR_NUMBER
)
1703 retval
= tv
.vval
.v_number
;
1704 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1708 /* If the result is a string, check if there is a non-digit before
1710 s
= tv
.vval
.v_string
;
1711 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1713 retval
= atol((char *)s
);
1727 * ":let" list all variable values
1728 * ":let var1 var2" list variable values
1729 * ":let var = expr" assignment command.
1730 * ":let var += expr" assignment command.
1731 * ":let var -= expr" assignment command.
1732 * ":let var .= expr" assignment command.
1733 * ":let [var1, var2] = expr" unpack list.
1739 char_u
*arg
= eap
->arg
;
1740 char_u
*expr
= NULL
;
1749 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1752 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1754 expr
= vim_strchr(argend
, '=');
1758 * ":let" without "=": list variables
1762 else if (!ends_excmd(*arg
))
1763 /* ":let var1 var2" */
1764 arg
= list_arg_vars(eap
, arg
, &first
);
1765 else if (!eap
->skip
)
1768 list_glob_vars(&first
);
1769 list_buf_vars(&first
);
1770 list_win_vars(&first
);
1772 list_tab_vars(&first
);
1774 list_script_vars(&first
);
1775 list_func_vars(&first
);
1776 list_vim_vars(&first
);
1778 eap
->nextcmd
= check_nextcmd(arg
);
1786 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1787 op
[0] = expr
[-1]; /* +=, -= or .= */
1789 expr
= skipwhite(expr
+ 1);
1793 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1802 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1810 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1811 * Handles both "var" with any type and "[var, var; var]" with a list type.
1812 * When "nextchars" is not NULL it points to a string with characters that
1813 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1815 * Returns OK or FAIL;
1818 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1821 int copy
; /* copy values from "tv", don't move */
1822 int semicolon
; /* from skip_var_list() */
1823 int var_count
; /* from skip_var_list() */
1826 char_u
*arg
= arg_start
;
1835 * ":let var = expr" or ":for var in list"
1837 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1843 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1845 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1852 if (semicolon
== 0 && var_count
< i
)
1854 EMSG(_("E687: Less targets than List items"));
1857 if (var_count
- semicolon
> i
)
1859 EMSG(_("E688: More targets than List items"));
1866 arg
= skipwhite(arg
+ 1);
1867 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1868 item
= item
->li_next
;
1872 arg
= skipwhite(arg
);
1875 /* Put the rest of the list (may be empty) in the var after ';'.
1876 * Create a new list for this. */
1880 while (item
!= NULL
)
1882 list_append_tv(l
, &item
->li_tv
);
1883 item
= item
->li_next
;
1886 ltv
.v_type
= VAR_LIST
;
1888 ltv
.vval
.v_list
= l
;
1891 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1892 (char_u
*)"]", nextchars
);
1898 else if (*arg
!= ',' && *arg
!= ']')
1900 EMSG2(_(e_intern2
), "ex_let_vars()");
1909 * Skip over assignable variable "var" or list of variables "[var, var]".
1910 * Used for ":let varvar = expr" and ":for varvar in expr".
1911 * For "[var, var]" increment "*var_count" for each variable.
1912 * for "[var, var; var]" set "semicolon".
1913 * Return NULL for an error.
1916 skip_var_list(arg
, var_count
, semicolon
)
1925 /* "[var, var]": find the matching ']'. */
1929 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1930 s
= skip_var_one(p
);
1933 EMSG2(_(e_invarg2
), p
);
1943 if (*semicolon
== 1)
1945 EMSG(_("Double ; in list of variables"));
1952 EMSG2(_(e_invarg2
), p
);
1959 return skip_var_one(arg
);
1963 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1970 if (*arg
== '@' && arg
[1] != NUL
)
1972 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
1973 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
1977 * List variables for hashtab "ht" with prefix "prefix".
1978 * If "empty" is TRUE also list NULL strings as empty strings.
1981 list_hashtable_vars(ht
, prefix
, empty
, first
)
1991 todo
= (int)ht
->ht_used
;
1992 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
1994 if (!HASHITEM_EMPTY(hi
))
1998 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
1999 || di
->di_tv
.vval
.v_string
!= NULL
)
2000 list_one_var(di
, prefix
, first
);
2006 * List global variables.
2009 list_glob_vars(first
)
2012 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
2016 * List buffer variables.
2019 list_buf_vars(first
)
2022 char_u numbuf
[NUMBUFLEN
];
2024 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
2027 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
2028 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
2033 * List window variables.
2036 list_win_vars(first
)
2039 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
2040 (char_u
*)"w:", TRUE
, first
);
2045 * List tab page variables.
2048 list_tab_vars(first
)
2051 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2052 (char_u
*)"t:", TRUE
, first
);
2057 * List Vim variables.
2060 list_vim_vars(first
)
2063 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2067 * List script-local variables, if there is a script.
2070 list_script_vars(first
)
2073 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2074 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2075 (char_u
*)"s:", FALSE
, first
);
2079 * List function variables, if there is a function.
2082 list_func_vars(first
)
2085 if (current_funccal
!= NULL
)
2086 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2087 (char_u
*)"l:", FALSE
, first
);
2091 * List variables in "arg".
2094 list_arg_vars(eap
, arg
, first
)
2107 while (!ends_excmd(*arg
) && !got_int
)
2109 if (error
|| eap
->skip
)
2111 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2112 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2115 EMSG(_(e_trailing
));
2121 /* get_name_len() takes care of expanding curly braces */
2122 name_start
= name
= arg
;
2123 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2126 /* This is mainly to keep test 49 working: when expanding
2127 * curly braces fails overrule the exception error message. */
2128 if (len
< 0 && !aborting())
2131 EMSG2(_(e_invarg2
), arg
);
2140 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2144 /* handle d.key, l[idx], f(expr) */
2146 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2150 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2154 case 'g': list_glob_vars(first
); break;
2155 case 'b': list_buf_vars(first
); break;
2156 case 'w': list_win_vars(first
); break;
2158 case 't': list_tab_vars(first
); break;
2160 case 'v': list_vim_vars(first
); break;
2161 case 's': list_script_vars(first
); break;
2162 case 'l': list_func_vars(first
); break;
2164 EMSG2(_("E738: Can't list variables for %s"), name
);
2169 char_u numbuf
[NUMBUFLEN
];
2174 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2177 list_one_var_a((char_u
*)"",
2178 arg
== arg_subsc
? name
: name_start
,
2180 s
== NULL
? (char_u
*)"" : s
,
2193 arg
= skipwhite(arg
);
2200 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2201 * Returns a pointer to the char just after the var name.
2202 * Returns NULL if there is an error.
2205 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2206 char_u
*arg
; /* points to variable name */
2207 typval_T
*tv
; /* value to assign to variable */
2208 int copy
; /* copy value from "tv" */
2209 char_u
*endchars
; /* valid chars after variable name or NULL */
2210 char_u
*op
; /* "+", "-", "." or NULL*/
2215 char_u
*arg_end
= NULL
;
2218 char_u
*tofree
= NULL
;
2221 * ":let $VAR = expr": Set environment variable.
2225 /* Find the end of the name. */
2228 len
= get_env_len(&arg
);
2230 EMSG2(_(e_invarg2
), name
- 1);
2233 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2234 EMSG2(_(e_letwrong
), op
);
2235 else if (endchars
!= NULL
2236 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2237 EMSG(_(e_letunexp
));
2242 p
= get_tv_string_chk(tv
);
2243 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2245 int mustfree
= FALSE
;
2246 char_u
*s
= vim_getenv(name
, &mustfree
);
2250 p
= tofree
= concat_str(s
, p
);
2257 vim_setenv(name
, p
);
2258 if (STRICMP(name
, "HOME") == 0)
2260 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2262 else if (didset_vimruntime
2263 && STRICMP(name
, "VIMRUNTIME") == 0)
2264 didset_vimruntime
= FALSE
;
2274 * ":let &option = expr": Set option value.
2275 * ":let &l:option = expr": Set local option value.
2276 * ":let &g:option = expr": Set global option value.
2278 else if (*arg
== '&')
2280 /* Find the end of the name. */
2281 p
= find_option_end(&arg
, &opt_flags
);
2282 if (p
== NULL
|| (endchars
!= NULL
2283 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2284 EMSG(_(e_letunexp
));
2290 char_u
*stringval
= NULL
;
2296 n
= get_tv_number(tv
);
2297 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2298 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2300 opt_type
= get_option_value(arg
, &numval
,
2301 &stringval
, opt_flags
);
2302 if ((opt_type
== 1 && *op
== '.')
2303 || (opt_type
== 0 && *op
!= '.'))
2304 EMSG2(_(e_letwrong
), op
);
2307 if (opt_type
== 1) /* number */
2314 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2316 s
= concat_str(stringval
, s
);
2317 vim_free(stringval
);
2324 set_option_value(arg
, n
, s
, opt_flags
);
2328 vim_free(stringval
);
2333 * ":let @r = expr": Set register contents.
2335 else if (*arg
== '@')
2338 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2339 EMSG2(_(e_letwrong
), op
);
2340 else if (endchars
!= NULL
2341 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2342 EMSG(_(e_letunexp
));
2345 char_u
*ptofree
= NULL
;
2348 p
= get_tv_string_chk(tv
);
2349 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2351 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2354 p
= ptofree
= concat_str(s
, p
);
2360 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2368 * ":let var = expr": Set internal variable.
2369 * ":let {expr} = expr": Idem, name made with curly braces
2371 else if (eval_isnamec1(*arg
) || *arg
== '{')
2375 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2376 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2378 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2379 EMSG(_(e_letunexp
));
2382 set_var_lval(&lv
, p
, tv
, copy
, op
);
2390 EMSG2(_(e_invarg2
), arg
);
2396 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2399 check_changedtick(arg
)
2402 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2404 EMSG2(_(e_readonlyvar
), arg
);
2411 * Get an lval: variable, Dict item or List item that can be assigned a value
2412 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2413 * "name.key", "name.key[expr]" etc.
2414 * Indexing only works if "name" is an existing List or Dictionary.
2415 * "name" points to the start of the name.
2416 * If "rettv" is not NULL it points to the value to be assigned.
2417 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2418 * wrong; must end in space or cmd separator.
2420 * Returns a pointer to just after the name, including indexes.
2421 * When an evaluation error occurs "lp->ll_name" is NULL;
2422 * Returns NULL for a parsing error. Still need to free items in "lp"!
2425 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2431 int quiet
; /* don't give error messages */
2432 int fne_flags
; /* flags for find_name_end() */
2435 char_u
*expr_start
, *expr_end
;
2446 /* Clear everything in "lp". */
2447 vim_memset(lp
, 0, sizeof(lval_T
));
2451 /* When skipping just find the end of the name. */
2453 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2456 /* Find the end of the name. */
2457 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2458 if (expr_start
!= NULL
)
2460 /* Don't expand the name when we already know there is an error. */
2461 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2462 && *p
!= '[' && *p
!= '.')
2464 EMSG(_(e_trailing
));
2468 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2469 if (lp
->ll_exp_name
== NULL
)
2471 /* Report an invalid expression in braces, unless the
2472 * expression evaluation has been cancelled due to an
2473 * aborting error, an interrupt, or an exception. */
2474 if (!aborting() && !quiet
)
2477 EMSG2(_(e_invarg2
), name
);
2481 lp
->ll_name
= lp
->ll_exp_name
;
2486 /* Without [idx] or .key we are done. */
2487 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2492 v
= find_var(lp
->ll_name
, &ht
);
2493 if (v
== NULL
&& !quiet
)
2494 EMSG2(_(e_undefvar
), lp
->ll_name
);
2500 * Loop until no more [idx] or .key is following.
2502 lp
->ll_tv
= &v
->di_tv
;
2503 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2505 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2506 && !(lp
->ll_tv
->v_type
== VAR_DICT
2507 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2510 EMSG(_("E689: Can only index a List or Dictionary"));
2516 EMSG(_("E708: [:] must come last"));
2524 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2529 EMSG(_(e_emptykey
));
2536 /* Get the index [expr] or the first index [expr: ]. */
2537 p
= skipwhite(p
+ 1);
2543 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2545 if (get_tv_string_chk(&var1
) == NULL
)
2547 /* not a number or string */
2553 /* Optionally get the second index [ :expr]. */
2556 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2559 EMSG(_(e_dictrange
));
2564 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2565 || rettv
->vval
.v_list
== NULL
))
2568 EMSG(_("E709: [:] requires a List value"));
2573 p
= skipwhite(p
+ 1);
2575 lp
->ll_empty2
= TRUE
;
2578 lp
->ll_empty2
= FALSE
;
2579 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2585 if (get_tv_string_chk(&var2
) == NULL
)
2587 /* not a number or string */
2594 lp
->ll_range
= TRUE
;
2597 lp
->ll_range
= FALSE
;
2602 EMSG(_(e_missbrac
));
2605 if (lp
->ll_range
&& !lp
->ll_empty2
)
2610 /* Skip to past ']'. */
2614 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2618 /* "[key]": get key from "var1" */
2619 key
= get_tv_string(&var1
); /* is number or string */
2623 EMSG(_(e_emptykey
));
2629 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2630 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2631 if (lp
->ll_di
== NULL
)
2633 /* Key does not exist in dict: may need to add it. */
2634 if (*p
== '[' || *p
== '.' || unlet
)
2637 EMSG2(_(e_dictkey
), key
);
2643 lp
->ll_newkey
= vim_strsave(key
);
2645 lp
->ll_newkey
= vim_strnsave(key
, len
);
2648 if (lp
->ll_newkey
== NULL
)
2654 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2659 * Get the number and item for the only or first index of the List.
2665 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2669 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2670 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2671 if (lp
->ll_li
== NULL
)
2676 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2679 if (lp
->ll_li
== NULL
)
2681 if (lp
->ll_range
&& !lp
->ll_empty2
)
2687 * May need to find the item or absolute index for the second
2689 * When no index given: "lp->ll_empty2" is TRUE.
2690 * Otherwise "lp->ll_n2" is set to the second index.
2692 if (lp
->ll_range
&& !lp
->ll_empty2
)
2694 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2698 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2701 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2704 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2706 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2707 if (lp
->ll_n2
< lp
->ll_n1
)
2711 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2719 * Clear lval "lp" that was filled by get_lval().
2725 vim_free(lp
->ll_exp_name
);
2726 vim_free(lp
->ll_newkey
);
2730 * Set a variable that was parsed by get_lval() to "rettv".
2731 * "endp" points to just after the parsed name.
2732 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2735 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2746 if (lp
->ll_tv
== NULL
)
2748 if (!check_changedtick(lp
->ll_name
))
2752 if (op
!= NULL
&& *op
!= '=')
2756 /* handle +=, -= and .= */
2757 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2760 if (tv_op(&tv
, rettv
, op
) == OK
)
2761 set_var(lp
->ll_name
, &tv
, FALSE
);
2766 set_var(lp
->ll_name
, rettv
, copy
);
2770 else if (tv_check_lock(lp
->ll_newkey
== NULL
2772 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2774 else if (lp
->ll_range
)
2777 * Assign the List values to the list items.
2779 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2781 if (op
!= NULL
&& *op
!= '=')
2782 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2785 clear_tv(&lp
->ll_li
->li_tv
);
2786 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2789 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2791 if (lp
->ll_li
->li_next
== NULL
)
2793 /* Need to add an empty item. */
2794 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2800 lp
->ll_li
= lp
->ll_li
->li_next
;
2804 EMSG(_("E710: List value has more items than target"));
2805 else if (lp
->ll_empty2
2806 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2807 : lp
->ll_n1
!= lp
->ll_n2
)
2808 EMSG(_("E711: List value has not enough items"));
2813 * Assign to a List or Dictionary item.
2815 if (lp
->ll_newkey
!= NULL
)
2817 if (op
!= NULL
&& *op
!= '=')
2819 EMSG2(_(e_letwrong
), op
);
2823 /* Need to add an item to the Dictionary. */
2824 di
= dictitem_alloc(lp
->ll_newkey
);
2827 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2832 lp
->ll_tv
= &di
->di_tv
;
2834 else if (op
!= NULL
&& *op
!= '=')
2836 tv_op(lp
->ll_tv
, rettv
, op
);
2840 clear_tv(lp
->ll_tv
);
2843 * Assign the value to the variable or list item.
2846 copy_tv(rettv
, lp
->ll_tv
);
2849 *lp
->ll_tv
= *rettv
;
2850 lp
->ll_tv
->v_lock
= 0;
2857 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2858 * Returns OK or FAIL.
2867 char_u numbuf
[NUMBUFLEN
];
2870 /* Can't do anything with a Funcref or a Dict on the right. */
2871 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2873 switch (tv1
->v_type
)
2880 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2883 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2884 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2889 if (tv2
->v_type
== VAR_LIST
)
2891 if (*op
== '+' || *op
== '-')
2893 /* nr += nr or nr -= nr*/
2894 n
= get_tv_number(tv1
);
2896 if (tv2
->v_type
== VAR_FLOAT
)
2901 f
+= tv2
->vval
.v_float
;
2903 f
-= tv2
->vval
.v_float
;
2905 tv1
->v_type
= VAR_FLOAT
;
2906 tv1
->vval
.v_float
= f
;
2912 n
+= get_tv_number(tv2
);
2914 n
-= get_tv_number(tv2
);
2916 tv1
->v_type
= VAR_NUMBER
;
2917 tv1
->vval
.v_number
= n
;
2922 if (tv2
->v_type
== VAR_FLOAT
)
2926 s
= get_tv_string(tv1
);
2927 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2929 tv1
->v_type
= VAR_STRING
;
2930 tv1
->vval
.v_string
= s
;
2939 if (*op
== '.' || (tv2
->v_type
!= VAR_FLOAT
2940 && tv2
->v_type
!= VAR_NUMBER
2941 && tv2
->v_type
!= VAR_STRING
))
2943 if (tv2
->v_type
== VAR_FLOAT
)
2944 f
= tv2
->vval
.v_float
;
2946 f
= get_tv_number(tv2
);
2948 tv1
->vval
.v_float
+= f
;
2950 tv1
->vval
.v_float
-= f
;
2957 EMSG2(_(e_letwrong
), op
);
2962 * Add a watcher to a list.
2965 list_add_watch(l
, lw
)
2969 lw
->lw_next
= l
->lv_watch
;
2974 * Remove a watcher from a list.
2975 * No warning when it isn't found...
2978 list_rem_watch(l
, lwrem
)
2982 listwatch_T
*lw
, **lwp
;
2985 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2997 * Just before removing an item from a list: advance watchers to the next
3001 list_fix_watch(l
, item
)
3007 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3008 if (lw
->lw_item
== item
)
3009 lw
->lw_item
= item
->li_next
;
3013 * Evaluate the expression used in a ":for var in expr" command.
3014 * "arg" points to "var".
3015 * Set "*errp" to TRUE for an error, FALSE otherwise;
3016 * Return a pointer that holds the info. Null when there is an error.
3019 eval_for_line(arg
, errp
, nextcmdp
, skip
)
3030 *errp
= TRUE
; /* default: there is an error */
3032 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
3036 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
3040 expr
= skipwhite(expr
);
3041 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
3043 EMSG(_("E690: Missing \"in\" after :for"));
3049 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
3055 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
3062 /* No need to increment the refcount, it's already set for the
3063 * list being used in "tv". */
3065 list_add_watch(l
, &fi
->fi_lw
);
3066 fi
->fi_lw
.lw_item
= l
->lv_first
;
3077 * Use the first item in a ":for" list. Advance to the next.
3078 * Assign the values to the variable (list). "arg" points to the first one.
3079 * Return TRUE when a valid item was found, FALSE when at end of list or
3083 next_for_item(fi_void
, arg
)
3087 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3091 item
= fi
->fi_lw
.lw_item
;
3096 fi
->fi_lw
.lw_item
= item
->li_next
;
3097 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3098 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3104 * Free the structure used to store info used by ":for".
3107 free_for_info(fi_void
)
3110 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3112 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3114 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3115 list_unref(fi
->fi_list
);
3120 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3123 set_context_for_expression(xp
, arg
, cmdidx
)
3132 if (cmdidx
== CMD_let
)
3134 xp
->xp_context
= EXPAND_USER_VARS
;
3135 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3137 /* ":let var1 var2 ...": find last space. */
3138 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3141 mb_ptr_back(arg
, p
);
3142 if (vim_iswhite(*p
))
3149 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3150 : EXPAND_EXPRESSION
;
3151 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3152 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3154 c
= *xp
->xp_pattern
;
3157 c
= xp
->xp_pattern
[1];
3161 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3162 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3166 xp
->xp_context
= EXPAND_SETTINGS
;
3167 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3168 xp
->xp_pattern
+= 2;
3174 /* environment variable */
3175 xp
->xp_context
= EXPAND_ENV_VARS
;
3180 xp
->xp_context
= EXPAND_EXPRESSION
;
3183 && xp
->xp_context
== EXPAND_FUNCTIONS
3184 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3186 /* Function name can start with "<SNR>" */
3189 else if (cmdidx
!= CMD_let
|| got_eq
)
3191 if (c
== '"') /* string */
3193 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3194 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3196 xp
->xp_context
= EXPAND_NOTHING
;
3198 else if (c
== '\'') /* literal string */
3200 /* Trick: '' is like stopping and starting a literal string. */
3201 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3203 xp
->xp_context
= EXPAND_NOTHING
;
3207 if (xp
->xp_pattern
[1] == '|')
3210 xp
->xp_context
= EXPAND_EXPRESSION
;
3213 xp
->xp_context
= EXPAND_COMMANDS
;
3216 xp
->xp_context
= EXPAND_EXPRESSION
;
3219 /* Doesn't look like something valid, expand as an expression
3221 xp
->xp_context
= EXPAND_EXPRESSION
;
3222 arg
= xp
->xp_pattern
;
3224 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3227 xp
->xp_pattern
= arg
;
3230 #endif /* FEAT_CMDL_COMPL */
3233 * ":1,25call func(arg1, arg2)" function call.
3239 char_u
*arg
= eap
->arg
;
3250 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3251 if (fudi
.fd_newkey
!= NULL
)
3253 /* Still need to give an error message for missing key. */
3254 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3255 vim_free(fudi
.fd_newkey
);
3260 /* Increase refcount on dictionary, it could get deleted when evaluating
3262 if (fudi
.fd_dict
!= NULL
)
3263 ++fudi
.fd_dict
->dv_refcount
;
3265 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3266 len
= (int)STRLEN(tofree
);
3267 name
= deref_func_name(tofree
, &len
);
3269 /* Skip white space to allow ":call func ()". Not good, but required for
3270 * backward compatibility. */
3271 startarg
= skipwhite(arg
);
3272 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3274 if (*startarg
!= '(')
3276 EMSG2(_("E107: Missing braces: %s"), eap
->arg
);
3281 * When skipping, evaluate the function once, to find the end of the
3283 * When the function takes a range, this is discovered after the first
3284 * call, and the loop is broken.
3289 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3293 for ( ; lnum
<= eap
->line2
; ++lnum
)
3295 if (!eap
->skip
&& eap
->addr_count
> 0)
3297 curwin
->w_cursor
.lnum
= lnum
;
3298 curwin
->w_cursor
.col
= 0;
3301 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3302 eap
->line1
, eap
->line2
, &doesrange
,
3303 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3309 /* Handle a function returning a Funcref, Dictionary or List. */
3310 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3317 if (doesrange
|| eap
->skip
)
3320 /* Stop when immediately aborting on error, or when an interrupt
3321 * occurred or an exception was thrown but not caught.
3322 * get_func_tv() returned OK, so that the check for trailing
3323 * characters below is executed. */
3332 /* Check for trailing illegal characters and a following command. */
3333 if (!ends_excmd(*arg
))
3336 EMSG(_(e_trailing
));
3339 eap
->nextcmd
= check_nextcmd(arg
);
3343 dict_unref(fudi
.fd_dict
);
3348 * ":unlet[!] var1 ... " command.
3354 ex_unletlock(eap
, eap
->arg
, 0);
3358 * ":lockvar" and ":unlockvar" commands
3364 char_u
*arg
= eap
->arg
;
3369 else if (vim_isdigit(*arg
))
3371 deep
= getdigits(&arg
);
3372 arg
= skipwhite(arg
);
3375 ex_unletlock(eap
, arg
, deep
);
3379 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3382 ex_unletlock(eap
, argstart
, deep
)
3387 char_u
*arg
= argstart
;
3394 /* Parse the name and find the end. */
3395 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3397 if (lv
.ll_name
== NULL
)
3398 error
= TRUE
; /* error but continue parsing */
3399 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3400 && !ends_excmd(*name_end
)))
3402 if (name_end
!= NULL
)
3405 EMSG(_(e_trailing
));
3407 if (!(eap
->skip
|| error
))
3412 if (!error
&& !eap
->skip
)
3414 if (eap
->cmdidx
== CMD_unlet
)
3416 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3421 if (do_lock_var(&lv
, name_end
, deep
,
3422 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3430 arg
= skipwhite(name_end
);
3431 } while (!ends_excmd(*arg
));
3433 eap
->nextcmd
= check_nextcmd(arg
);
3437 do_unlet_var(lp
, name_end
, forceit
)
3445 if (lp
->ll_tv
== NULL
)
3450 /* Normal name or expanded name. */
3451 if (check_changedtick(lp
->ll_name
))
3453 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3457 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3459 else if (lp
->ll_range
)
3463 /* Delete a range of List items. */
3464 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3466 li
= lp
->ll_li
->li_next
;
3467 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3474 if (lp
->ll_list
!= NULL
)
3475 /* unlet a List item. */
3476 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3478 /* unlet a Dictionary item. */
3479 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3486 * "unlet" a variable. Return OK if it existed, FAIL if not.
3487 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3490 do_unlet(name
, forceit
)
3499 ht
= find_var_ht(name
, &varname
);
3500 if (ht
!= NULL
&& *varname
!= NUL
)
3502 hi
= hash_find(ht
, varname
);
3503 if (!HASHITEM_EMPTY(hi
))
3506 if (var_check_fixed(di
->di_flags
, name
)
3507 || var_check_ro(di
->di_flags
, name
))
3515 EMSG2(_("E108: No such variable: \"%s\""), name
);
3520 * Lock or unlock variable indicated by "lp".
3521 * "deep" is the levels to go (-1 for unlimited);
3522 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3525 do_lock_var(lp
, name_end
, deep
, lock
)
3535 if (deep
== 0) /* nothing to do */
3538 if (lp
->ll_tv
== NULL
)
3543 /* Normal name or expanded name. */
3544 if (check_changedtick(lp
->ll_name
))
3548 di
= find_var(lp
->ll_name
, NULL
);
3554 di
->di_flags
|= DI_FLAGS_LOCK
;
3556 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3557 item_lock(&di
->di_tv
, deep
, lock
);
3562 else if (lp
->ll_range
)
3564 listitem_T
*li
= lp
->ll_li
;
3566 /* (un)lock a range of List items. */
3567 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3569 item_lock(&li
->li_tv
, deep
, lock
);
3574 else if (lp
->ll_list
!= NULL
)
3575 /* (un)lock a List item. */
3576 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3578 /* un(lock) a Dictionary item. */
3579 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3585 * Lock or unlock an item. "deep" is nr of levels to go.
3588 item_lock(tv
, deep
, lock
)
3593 static int recurse
= 0;
3600 if (recurse
>= DICT_MAXNEST
)
3602 EMSG(_("E743: variable nested too deep for (un)lock"));
3609 /* lock/unlock the item itself */
3611 tv
->v_lock
|= VAR_LOCKED
;
3613 tv
->v_lock
&= ~VAR_LOCKED
;
3618 if ((l
= tv
->vval
.v_list
) != NULL
)
3621 l
->lv_lock
|= VAR_LOCKED
;
3623 l
->lv_lock
&= ~VAR_LOCKED
;
3624 if (deep
< 0 || deep
> 1)
3625 /* recursive: lock/unlock the items the List contains */
3626 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3627 item_lock(&li
->li_tv
, deep
- 1, lock
);
3631 if ((d
= tv
->vval
.v_dict
) != NULL
)
3634 d
->dv_lock
|= VAR_LOCKED
;
3636 d
->dv_lock
&= ~VAR_LOCKED
;
3637 if (deep
< 0 || deep
> 1)
3639 /* recursive: lock/unlock the items the List contains */
3640 todo
= (int)d
->dv_hashtab
.ht_used
;
3641 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3643 if (!HASHITEM_EMPTY(hi
))
3646 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3656 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3657 * it refers to a List or Dictionary that is locked.
3663 return (tv
->v_lock
& VAR_LOCKED
)
3664 || (tv
->v_type
== VAR_LIST
3665 && tv
->vval
.v_list
!= NULL
3666 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3667 || (tv
->v_type
== VAR_DICT
3668 && tv
->vval
.v_dict
!= NULL
3669 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3672 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3674 * Delete all "menutrans_" variables.
3677 del_menutrans_vars()
3682 hash_lock(&globvarht
);
3683 todo
= (int)globvarht
.ht_used
;
3684 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3686 if (!HASHITEM_EMPTY(hi
))
3689 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3690 delete_var(&globvarht
, hi
);
3693 hash_unlock(&globvarht
);
3697 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3700 * Local string buffer for the next two functions to store a variable name
3701 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3702 * get_user_var_name().
3705 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3707 static char_u
*varnamebuf
= NULL
;
3708 static int varnamebuflen
= 0;
3711 * Function to concatenate a prefix and a variable name.
3714 cat_prefix_varname(prefix
, name
)
3720 len
= (int)STRLEN(name
) + 3;
3721 if (len
> varnamebuflen
)
3723 vim_free(varnamebuf
);
3724 len
+= 10; /* some additional space */
3725 varnamebuf
= alloc(len
);
3726 if (varnamebuf
== NULL
)
3731 varnamebuflen
= len
;
3733 *varnamebuf
= prefix
;
3734 varnamebuf
[1] = ':';
3735 STRCPY(varnamebuf
+ 2, name
);
3740 * Function given to ExpandGeneric() to obtain the list of user defined
3741 * (global/buffer/window/built-in) variable names.
3745 get_user_var_name(xp
, idx
)
3749 static long_u gdone
;
3750 static long_u bdone
;
3751 static long_u wdone
;
3753 static long_u tdone
;
3756 static hashitem_T
*hi
;
3761 gdone
= bdone
= wdone
= vidx
= 0;
3767 /* Global variables */
3768 if (gdone
< globvarht
.ht_used
)
3771 hi
= globvarht
.ht_array
;
3774 while (HASHITEM_EMPTY(hi
))
3776 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3777 return cat_prefix_varname('g', hi
->hi_key
);
3782 ht
= &curbuf
->b_vars
.dv_hashtab
;
3783 if (bdone
< ht
->ht_used
)
3789 while (HASHITEM_EMPTY(hi
))
3791 return cat_prefix_varname('b', hi
->hi_key
);
3793 if (bdone
== ht
->ht_used
)
3796 return (char_u
*)"b:changedtick";
3800 ht
= &curwin
->w_vars
.dv_hashtab
;
3801 if (wdone
< ht
->ht_used
)
3807 while (HASHITEM_EMPTY(hi
))
3809 return cat_prefix_varname('w', hi
->hi_key
);
3814 ht
= &curtab
->tp_vars
.dv_hashtab
;
3815 if (tdone
< ht
->ht_used
)
3821 while (HASHITEM_EMPTY(hi
))
3823 return cat_prefix_varname('t', hi
->hi_key
);
3829 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3831 vim_free(varnamebuf
);
3837 #endif /* FEAT_CMDL_COMPL */
3840 * types for expressions.
3845 , TYPE_EQUAL
/* == */
3846 , TYPE_NEQUAL
/* != */
3847 , TYPE_GREATER
/* > */
3848 , TYPE_GEQUAL
/* >= */
3849 , TYPE_SMALLER
/* < */
3850 , TYPE_SEQUAL
/* <= */
3851 , TYPE_MATCH
/* =~ */
3852 , TYPE_NOMATCH
/* !~ */
3856 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3857 * executed. The function may return OK, but the rettv will be of type
3858 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3862 * Handle zero level expression.
3863 * This calls eval1() and handles error message and nextcmd.
3864 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3865 * Note: "rettv.v_lock" is not set.
3866 * Return OK or FAIL.
3869 eval0(arg
, rettv
, nextcmd
, evaluate
)
3879 ret
= eval1(&p
, rettv
, evaluate
);
3880 if (ret
== FAIL
|| !ends_excmd(*p
))
3885 * Report the invalid expression unless the expression evaluation has
3886 * been cancelled due to an aborting error, an interrupt, or an
3890 EMSG2(_(e_invexpr2
), arg
);
3893 if (nextcmd
!= NULL
)
3894 *nextcmd
= check_nextcmd(p
);
3900 * Handle top level expression:
3901 * expr1 ? expr0 : expr0
3903 * "arg" must point to the first non-white of the expression.
3904 * "arg" is advanced to the next non-white after the recognized expression.
3906 * Note: "rettv.v_lock" is not set.
3908 * Return OK or FAIL.
3911 eval1(arg
, rettv
, evaluate
)
3920 * Get the first variable.
3922 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3925 if ((*arg
)[0] == '?')
3932 if (get_tv_number_chk(rettv
, &error
) != 0)
3940 * Get the second variable.
3942 *arg
= skipwhite(*arg
+ 1);
3943 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3947 * Check for the ":".
3949 if ((*arg
)[0] != ':')
3951 EMSG(_("E109: Missing ':' after '?'"));
3952 if (evaluate
&& result
)
3958 * Get the third variable.
3960 *arg
= skipwhite(*arg
+ 1);
3961 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
3963 if (evaluate
&& result
)
3967 if (evaluate
&& !result
)
3975 * Handle first level expression:
3976 * expr2 || expr2 || expr2 logical OR
3978 * "arg" must point to the first non-white of the expression.
3979 * "arg" is advanced to the next non-white after the recognized expression.
3981 * Return OK or FAIL.
3984 eval2(arg
, rettv
, evaluate
)
3995 * Get the first variable.
3997 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
4001 * Repeat until there is no following "||".
4005 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
4007 if (evaluate
&& first
)
4009 if (get_tv_number_chk(rettv
, &error
) != 0)
4018 * Get the second variable.
4020 *arg
= skipwhite(*arg
+ 2);
4021 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
4025 * Compute the result.
4027 if (evaluate
&& !result
)
4029 if (get_tv_number_chk(&var2
, &error
) != 0)
4037 rettv
->v_type
= VAR_NUMBER
;
4038 rettv
->vval
.v_number
= result
;
4046 * Handle second level expression:
4047 * expr3 && expr3 && expr3 logical AND
4049 * "arg" must point to the first non-white of the expression.
4050 * "arg" is advanced to the next non-white after the recognized expression.
4052 * Return OK or FAIL.
4055 eval3(arg
, rettv
, evaluate
)
4066 * Get the first variable.
4068 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
4072 * Repeat until there is no following "&&".
4076 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
4078 if (evaluate
&& first
)
4080 if (get_tv_number_chk(rettv
, &error
) == 0)
4089 * Get the second variable.
4091 *arg
= skipwhite(*arg
+ 2);
4092 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4096 * Compute the result.
4098 if (evaluate
&& result
)
4100 if (get_tv_number_chk(&var2
, &error
) == 0)
4108 rettv
->v_type
= VAR_NUMBER
;
4109 rettv
->vval
.v_number
= result
;
4117 * Handle third level expression:
4129 * "arg" must point to the first non-white of the expression.
4130 * "arg" is advanced to the next non-white after the recognized expression.
4132 * Return OK or FAIL.
4135 eval4(arg
, rettv
, evaluate
)
4143 exptype_T type
= TYPE_UNKNOWN
;
4144 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4148 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4149 regmatch_T regmatch
;
4154 * Get the first variable.
4156 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4162 case '=': if (p
[1] == '=')
4164 else if (p
[1] == '~')
4167 case '!': if (p
[1] == '=')
4169 else if (p
[1] == '~')
4170 type
= TYPE_NOMATCH
;
4172 case '>': if (p
[1] != '=')
4174 type
= TYPE_GREATER
;
4180 case '<': if (p
[1] != '=')
4182 type
= TYPE_SMALLER
;
4188 case 'i': if (p
[1] == 's')
4190 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4192 if (!vim_isIDc(p
[len
]))
4194 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4202 * If there is a comparative operator, use it.
4204 if (type
!= TYPE_UNKNOWN
)
4206 /* extra question mark appended: ignore case */
4212 /* extra '#' appended: match case */
4213 else if (p
[len
] == '#')
4218 /* nothing appended: use 'ignorecase' */
4223 * Get the second variable.
4225 *arg
= skipwhite(p
+ len
);
4226 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4234 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4236 /* For "is" a different type always means FALSE, for "notis"
4238 n1
= (type
== TYPE_NEQUAL
);
4240 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4244 n1
= (rettv
->v_type
== var2
.v_type
4245 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4246 if (type
== TYPE_NEQUAL
)
4249 else if (rettv
->v_type
!= var2
.v_type
4250 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4252 if (rettv
->v_type
!= var2
.v_type
)
4253 EMSG(_("E691: Can only compare List with List"));
4255 EMSG(_("E692: Invalid operation for Lists"));
4262 /* Compare two Lists for being equal or unequal. */
4263 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4264 if (type
== TYPE_NEQUAL
)
4269 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4273 n1
= (rettv
->v_type
== var2
.v_type
4274 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4275 if (type
== TYPE_NEQUAL
)
4278 else if (rettv
->v_type
!= var2
.v_type
4279 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4281 if (rettv
->v_type
!= var2
.v_type
)
4282 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4284 EMSG(_("E736: Invalid operation for Dictionary"));
4291 /* Compare two Dictionaries for being equal or unequal. */
4292 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4293 if (type
== TYPE_NEQUAL
)
4298 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4300 if (rettv
->v_type
!= var2
.v_type
4301 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4303 if (rettv
->v_type
!= var2
.v_type
)
4304 EMSG(_("E693: Can only compare Funcref with Funcref"));
4306 EMSG(_("E694: Invalid operation for Funcrefs"));
4313 /* Compare two Funcrefs for being equal or unequal. */
4314 if (rettv
->vval
.v_string
== NULL
4315 || var2
.vval
.v_string
== NULL
)
4318 n1
= STRCMP(rettv
->vval
.v_string
,
4319 var2
.vval
.v_string
) == 0;
4320 if (type
== TYPE_NEQUAL
)
4327 * If one of the two variables is a float, compare as a float.
4328 * When using "=~" or "!~", always compare as string.
4330 else if ((rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4331 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4335 if (rettv
->v_type
== VAR_FLOAT
)
4336 f1
= rettv
->vval
.v_float
;
4338 f1
= get_tv_number(rettv
);
4339 if (var2
.v_type
== VAR_FLOAT
)
4340 f2
= var2
.vval
.v_float
;
4342 f2
= get_tv_number(&var2
);
4346 case TYPE_EQUAL
: n1
= (f1
== f2
); break;
4347 case TYPE_NEQUAL
: n1
= (f1
!= f2
); break;
4348 case TYPE_GREATER
: n1
= (f1
> f2
); break;
4349 case TYPE_GEQUAL
: n1
= (f1
>= f2
); break;
4350 case TYPE_SMALLER
: n1
= (f1
< f2
); break;
4351 case TYPE_SEQUAL
: n1
= (f1
<= f2
); break;
4354 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4360 * If one of the two variables is a number, compare as a number.
4361 * When using "=~" or "!~", always compare as string.
4363 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4364 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4366 n1
= get_tv_number(rettv
);
4367 n2
= get_tv_number(&var2
);
4370 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4371 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4372 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4373 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4374 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4375 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4378 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4383 s1
= get_tv_string_buf(rettv
, buf1
);
4384 s2
= get_tv_string_buf(&var2
, buf2
);
4385 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4386 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4392 case TYPE_EQUAL
: n1
= (i
== 0); break;
4393 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4394 case TYPE_GREATER
: n1
= (i
> 0); break;
4395 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4396 case TYPE_SMALLER
: n1
= (i
< 0); break;
4397 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4401 /* avoid 'l' flag in 'cpoptions' */
4403 p_cpo
= (char_u
*)"";
4404 regmatch
.regprog
= vim_regcomp(s2
,
4405 RE_MAGIC
+ RE_STRING
);
4406 regmatch
.rm_ic
= ic
;
4407 if (regmatch
.regprog
!= NULL
)
4409 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4410 vim_free(regmatch
.regprog
);
4411 if (type
== TYPE_NOMATCH
)
4417 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4422 rettv
->v_type
= VAR_NUMBER
;
4423 rettv
->vval
.v_number
= n1
;
4431 * Handle fourth level expression:
4433 * - number subtraction
4434 * . string concatenation
4436 * "arg" must point to the first non-white of the expression.
4437 * "arg" is advanced to the next non-white after the recognized expression.
4439 * Return OK or FAIL.
4442 eval5(arg
, rettv
, evaluate
)
4452 float_T f1
= 0, f2
= 0;
4455 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4459 * Get the first variable.
4461 if (eval6(arg
, rettv
, evaluate
, FALSE
) == FAIL
)
4465 * Repeat computing, until no '+', '-' or '.' is following.
4470 if (op
!= '+' && op
!= '-' && op
!= '.')
4473 if ((op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4475 && (op
== '.' || rettv
->v_type
!= VAR_FLOAT
)
4479 /* For "list + ...", an illegal use of the first operand as
4480 * a number cannot be determined before evaluating the 2nd
4481 * operand: if this is also a list, all is ok.
4482 * For "something . ...", "something - ..." or "non-list + ...",
4483 * we know that the first operand needs to be a string or number
4484 * without evaluating the 2nd operand. So check before to avoid
4485 * side effects after an error. */
4486 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4494 * Get the second variable.
4496 *arg
= skipwhite(*arg
+ 1);
4497 if (eval6(arg
, &var2
, evaluate
, op
== '.') == FAIL
)
4506 * Compute the result.
4510 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4511 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4512 if (s2
== NULL
) /* type error ? */
4518 p
= concat_str(s1
, s2
);
4520 rettv
->v_type
= VAR_STRING
;
4521 rettv
->vval
.v_string
= p
;
4523 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4524 && var2
.v_type
== VAR_LIST
)
4526 /* concatenate Lists */
4527 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4542 if (rettv
->v_type
== VAR_FLOAT
)
4544 f1
= rettv
->vval
.v_float
;
4550 n1
= get_tv_number_chk(rettv
, &error
);
4553 /* This can only happen for "list + non-list". For
4554 * "non-list + ..." or "something - ...", we returned
4555 * before evaluating the 2nd operand. */
4560 if (var2
.v_type
== VAR_FLOAT
)
4565 if (var2
.v_type
== VAR_FLOAT
)
4567 f2
= var2
.vval
.v_float
;
4573 n2
= get_tv_number_chk(&var2
, &error
);
4581 if (rettv
->v_type
== VAR_FLOAT
)
4588 /* If there is a float on either side the result is a float. */
4589 if (rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4595 rettv
->v_type
= VAR_FLOAT
;
4596 rettv
->vval
.v_float
= f1
;
4605 rettv
->v_type
= VAR_NUMBER
;
4606 rettv
->vval
.v_number
= n1
;
4616 * Handle fifth level expression:
4617 * * number multiplication
4621 * "arg" must point to the first non-white of the expression.
4622 * "arg" is advanced to the next non-white after the recognized expression.
4624 * Return OK or FAIL.
4627 eval6(arg
, rettv
, evaluate
, want_string
)
4631 int want_string
; /* after "." operator */
4637 int use_float
= FALSE
;
4643 * Get the first variable.
4645 if (eval7(arg
, rettv
, evaluate
, want_string
) == FAIL
)
4649 * Repeat computing, until no '*', '/' or '%' is following.
4654 if (op
!= '*' && op
!= '/' && op
!= '%')
4660 if (rettv
->v_type
== VAR_FLOAT
)
4662 f1
= rettv
->vval
.v_float
;
4668 n1
= get_tv_number_chk(rettv
, &error
);
4677 * Get the second variable.
4679 *arg
= skipwhite(*arg
+ 1);
4680 if (eval7(arg
, &var2
, evaluate
, FALSE
) == FAIL
)
4686 if (var2
.v_type
== VAR_FLOAT
)
4693 f2
= var2
.vval
.v_float
;
4699 n2
= get_tv_number_chk(&var2
, &error
);
4710 * Compute the result.
4711 * When either side is a float the result is a float.
4720 /* We rely on the floating point library to handle divide
4721 * by zero to result in "inf" and not a crash. */
4726 EMSG(_("E804: Cannot use % with float"));
4729 rettv
->v_type
= VAR_FLOAT
;
4730 rettv
->vval
.v_float
= f1
;
4739 if (n2
== 0) /* give an error message? */
4742 n1
= -0x7fffffffL
- 1L; /* similar to NaN */
4753 if (n2
== 0) /* give an error message? */
4758 rettv
->v_type
= VAR_NUMBER
;
4759 rettv
->vval
.v_number
= n1
;
4768 * Handle sixth level expression:
4769 * number number constant
4770 * "string" string constant
4771 * 'string' literal string constant
4772 * &option-name option value
4773 * @r register contents
4774 * identifier variable value
4775 * function() function call
4776 * $VAR environment variable
4777 * (expression) nested expression
4779 * {key: val, key: val} Dictionary
4782 * ! in front logical NOT
4783 * - in front unary minus
4784 * + in front unary plus (ignored)
4785 * trailing [] subscript in String or List
4786 * trailing .name entry in Dictionary
4788 * "arg" must point to the first non-white of the expression.
4789 * "arg" is advanced to the next non-white after the recognized expression.
4791 * Return OK or FAIL.
4794 eval7(arg
, rettv
, evaluate
, want_string
)
4798 int want_string
; /* after "." operator */
4803 char_u
*start_leader
, *end_leader
;
4808 * Initialise variable so that clear_tv() can't mistake this for a
4809 * string and free a string that isn't there.
4811 rettv
->v_type
= VAR_UNKNOWN
;
4814 * Skip '!' and '-' characters. They are handled later.
4816 start_leader
= *arg
;
4817 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4818 *arg
= skipwhite(*arg
+ 1);
4838 char_u
*p
= skipdigits(*arg
+ 1);
4839 int get_float
= FALSE
;
4841 /* We accept a float when the format matches
4842 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4843 * strict to avoid backwards compatibility problems.
4844 * Don't look for a float after the "." operator, so that
4845 * ":let vers = 1.2.3" doesn't fail. */
4846 if (!want_string
&& p
[0] == '.' && vim_isdigit(p
[1]))
4849 p
= skipdigits(p
+ 2);
4850 if (*p
== 'e' || *p
== 'E')
4853 if (*p
== '-' || *p
== '+')
4855 if (!vim_isdigit(*p
))
4858 p
= skipdigits(p
+ 1);
4860 if (ASCII_ISALPHA(*p
) || *p
== '.')
4867 *arg
+= string2float(*arg
, &f
);
4870 rettv
->v_type
= VAR_FLOAT
;
4871 rettv
->vval
.v_float
= f
;
4877 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4881 rettv
->v_type
= VAR_NUMBER
;
4882 rettv
->vval
.v_number
= n
;
4889 * String constant: "string".
4891 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4895 * Literal string constant: 'str''ing'.
4897 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4901 * List: [expr, expr]
4903 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4907 * Dictionary: {key: val, key: val}
4909 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4913 * Option value: &name
4915 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4919 * Environment variable: $VAR.
4921 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4925 * Register contents: @r.
4930 rettv
->v_type
= VAR_STRING
;
4931 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4938 * nested expression: (expression).
4940 case '(': *arg
= skipwhite(*arg
+ 1);
4941 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4946 EMSG(_("E110: Missing ')'"));
4952 default: ret
= NOTDONE
;
4959 * Must be a variable or function name.
4960 * Can also be a curly-braces kind of name: {expr}.
4963 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
4971 if (**arg
== '(') /* recursive! */
4973 /* If "s" is the name of a variable of type VAR_FUNC
4974 * use its contents. */
4975 s
= deref_func_name(s
, &len
);
4977 /* Invoke the function. */
4978 ret
= get_func_tv(s
, len
, rettv
, arg
,
4979 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
4980 &len
, evaluate
, NULL
);
4981 /* Stop the expression evaluation when immediately
4982 * aborting on error, or when an interrupt occurred or
4983 * an exception was thrown but not caught. */
4992 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
5001 *arg
= skipwhite(*arg
);
5003 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5006 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
5009 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5011 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
5018 if (rettv
->v_type
== VAR_FLOAT
)
5019 f
= rettv
->vval
.v_float
;
5022 val
= get_tv_number_chk(rettv
, &error
);
5030 while (end_leader
> start_leader
)
5033 if (*end_leader
== '!')
5036 if (rettv
->v_type
== VAR_FLOAT
)
5042 else if (*end_leader
== '-')
5045 if (rettv
->v_type
== VAR_FLOAT
)
5053 if (rettv
->v_type
== VAR_FLOAT
)
5056 rettv
->vval
.v_float
= f
;
5062 rettv
->v_type
= VAR_NUMBER
;
5063 rettv
->vval
.v_number
= val
;
5072 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5073 * "*arg" points to the '[' or '.'.
5074 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5077 eval_index(arg
, rettv
, evaluate
, verbose
)
5081 int verbose
; /* give error messages */
5083 int empty1
= FALSE
, empty2
= FALSE
;
5084 typval_T var1
, var2
;
5091 if (rettv
->v_type
== VAR_FUNC
5093 || rettv
->v_type
== VAR_FLOAT
5098 EMSG(_("E695: Cannot index a Funcref"));
5108 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
5112 *arg
= skipwhite(key
+ len
);
5119 * Get the (first) variable from inside the [].
5121 *arg
= skipwhite(*arg
+ 1);
5124 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
5126 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
5128 /* not a number or string */
5134 * Get the second variable from inside the [:].
5139 *arg
= skipwhite(*arg
+ 1);
5142 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
5148 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
5150 /* not a number or string */
5158 /* Check for the ']'. */
5162 EMSG(_(e_missbrac
));
5168 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
5174 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
5176 n1
= get_tv_number(&var1
);
5185 n2
= get_tv_number(&var2
);
5190 switch (rettv
->v_type
)
5194 s
= get_tv_string(rettv
);
5195 len
= (long)STRLEN(s
);
5198 /* The resulting variable is a substring. If the indexes
5199 * are out of range the result is empty. */
5210 if (n1
>= len
|| n2
< 0 || n1
> n2
)
5213 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
5217 /* The resulting variable is a string of a single
5218 * character. If the index is too big or negative the
5219 * result is empty. */
5220 if (n1
>= len
|| n1
< 0)
5223 s
= vim_strnsave(s
+ n1
, 1);
5226 rettv
->v_type
= VAR_STRING
;
5227 rettv
->vval
.v_string
= s
;
5231 len
= list_len(rettv
->vval
.v_list
);
5234 if (!empty1
&& (n1
< 0 || n1
>= len
))
5236 /* For a range we allow invalid values and return an empty
5237 * list. A list index out of range is an error. */
5241 EMSGN(_(e_listidx
), n1
);
5255 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
5260 for (item
= list_find(rettv
->vval
.v_list
, n1
);
5263 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
5268 item
= item
->li_next
;
5271 rettv
->v_type
= VAR_LIST
;
5272 rettv
->vval
.v_list
= l
;
5277 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
5287 EMSG(_(e_dictrange
));
5297 key
= get_tv_string(&var1
);
5301 EMSG(_(e_emptykey
));
5307 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
5309 if (item
== NULL
&& verbose
)
5310 EMSG2(_(e_dictkey
), key
);
5316 copy_tv(&item
->di_tv
, &var1
);
5328 * Get an option value.
5329 * "arg" points to the '&' or '+' before the option name.
5330 * "arg" is advanced to character after the option name.
5331 * Return OK or FAIL.
5334 get_option_tv(arg
, rettv
, evaluate
)
5336 typval_T
*rettv
; /* when NULL, only check if option exists */
5344 int working
= (**arg
== '+'); /* has("+option") */
5349 * Isolate the option name and find its value.
5351 option_end
= find_option_end(arg
, &opt_flags
);
5352 if (option_end
== NULL
)
5355 EMSG2(_("E112: Option name missing: %s"), *arg
);
5367 opt_type
= get_option_value(*arg
, &numval
,
5368 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5370 if (opt_type
== -3) /* invalid name */
5373 EMSG2(_("E113: Unknown option: %s"), *arg
);
5376 else if (rettv
!= NULL
)
5378 if (opt_type
== -2) /* hidden string option */
5380 rettv
->v_type
= VAR_STRING
;
5381 rettv
->vval
.v_string
= NULL
;
5383 else if (opt_type
== -1) /* hidden number option */
5385 rettv
->v_type
= VAR_NUMBER
;
5386 rettv
->vval
.v_number
= 0;
5388 else if (opt_type
== 1) /* number option */
5390 rettv
->v_type
= VAR_NUMBER
;
5391 rettv
->vval
.v_number
= numval
;
5393 else /* string option */
5395 rettv
->v_type
= VAR_STRING
;
5396 rettv
->vval
.v_string
= stringval
;
5399 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5402 *option_end
= c
; /* put back for error messages */
5409 * Allocate a variable for a string constant.
5410 * Return OK or FAIL.
5413 get_string_tv(arg
, rettv
, evaluate
)
5423 * Find the end of the string, skipping backslashed characters.
5425 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5427 if (*p
== '\\' && p
[1] != NUL
)
5430 /* A "\<x>" form occupies at least 4 characters, and produces up
5431 * to 6 characters: reserve space for 2 extra */
5439 EMSG2(_("E114: Missing quote: %s"), *arg
);
5443 /* If only parsing, set *arg and return here */
5451 * Copy the string into allocated memory, handling backslashed
5454 name
= alloc((unsigned)(p
- *arg
+ extra
));
5457 rettv
->v_type
= VAR_STRING
;
5458 rettv
->vval
.v_string
= name
;
5460 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5466 case 'b': *name
++ = BS
; ++p
; break;
5467 case 'e': *name
++ = ESC
; ++p
; break;
5468 case 'f': *name
++ = FF
; ++p
; break;
5469 case 'n': *name
++ = NL
; ++p
; break;
5470 case 'r': *name
++ = CAR
; ++p
; break;
5471 case 't': *name
++ = TAB
; ++p
; break;
5473 case 'X': /* hex: "\x1", "\x12" */
5475 case 'u': /* Unicode: "\u0023" */
5477 if (vim_isxdigit(p
[1]))
5480 int c
= toupper(*p
);
5487 while (--n
>= 0 && vim_isxdigit(p
[1]))
5490 nr
= (nr
<< 4) + hex2nr(*p
);
5494 /* For "\u" store the number according to
5497 name
+= (*mb_char2bytes
)(nr
, name
);
5504 /* octal: "\1", "\12", "\123" */
5512 case '7': *name
= *p
++ - '0';
5513 if (*p
>= '0' && *p
<= '7')
5515 *name
= (*name
<< 3) + *p
++ - '0';
5516 if (*p
>= '0' && *p
<= '7')
5517 *name
= (*name
<< 3) + *p
++ - '0';
5522 /* Special key, e.g.: "\<C-W>" */
5523 case '<': extra
= trans_special(&p
, name
, TRUE
);
5531 default: MB_COPY_CHAR(p
, name
);
5536 MB_COPY_CHAR(p
, name
);
5546 * Allocate a variable for a 'str''ing' constant.
5547 * Return OK or FAIL.
5550 get_lit_string_tv(arg
, rettv
, evaluate
)
5560 * Find the end of the string, skipping ''.
5562 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5575 EMSG2(_("E115: Missing quote: %s"), *arg
);
5579 /* If only parsing return after setting "*arg" */
5587 * Copy the string into allocated memory, handling '' to ' reduction.
5589 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5592 rettv
->v_type
= VAR_STRING
;
5593 rettv
->vval
.v_string
= str
;
5595 for (p
= *arg
+ 1; *p
!= NUL
; )
5603 MB_COPY_CHAR(p
, str
);
5612 * Allocate a variable for a List and fill it from "*arg".
5613 * Return OK or FAIL.
5616 get_list_tv(arg
, rettv
, evaluate
)
5632 *arg
= skipwhite(*arg
+ 1);
5633 while (**arg
!= ']' && **arg
!= NUL
)
5635 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5639 item
= listitem_alloc();
5643 item
->li_tv
.v_lock
= 0;
5644 list_append(l
, item
);
5654 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5657 *arg
= skipwhite(*arg
+ 1);
5662 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5669 *arg
= skipwhite(*arg
+ 1);
5672 rettv
->v_type
= VAR_LIST
;
5673 rettv
->vval
.v_list
= l
;
5681 * Allocate an empty header for a list.
5682 * Caller should take care of the reference count.
5689 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5692 /* Prepend the list to the list of lists for garbage collection. */
5693 if (first_list
!= NULL
)
5694 first_list
->lv_used_prev
= l
;
5695 l
->lv_used_prev
= NULL
;
5696 l
->lv_used_next
= first_list
;
5703 * Allocate an empty list for a return value.
5704 * Returns OK or FAIL.
5707 rettv_list_alloc(rettv
)
5710 list_T
*l
= list_alloc();
5715 rettv
->vval
.v_list
= l
;
5716 rettv
->v_type
= VAR_LIST
;
5722 * Unreference a list: decrement the reference count and free it when it
5729 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5734 * Free a list, including all items it points to.
5735 * Ignores the reference count.
5738 list_free(l
, recurse
)
5740 int recurse
; /* Free Lists and Dictionaries recursively. */
5744 /* Remove the list from the list of lists for garbage collection. */
5745 if (l
->lv_used_prev
== NULL
)
5746 first_list
= l
->lv_used_next
;
5748 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5749 if (l
->lv_used_next
!= NULL
)
5750 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5752 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5754 /* Remove the item before deleting it. */
5755 l
->lv_first
= item
->li_next
;
5756 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5757 && item
->li_tv
.v_type
!= VAR_DICT
))
5758 clear_tv(&item
->li_tv
);
5765 * Allocate a list item.
5770 return (listitem_T
*)alloc(sizeof(listitem_T
));
5774 * Free a list item. Also clears the value. Does not notify watchers.
5780 clear_tv(&item
->li_tv
);
5785 * Remove a list item from a List and free it. Also clears the value.
5788 listitem_remove(l
, item
)
5792 list_remove(l
, item
, item
);
5793 listitem_free(item
);
5797 * Get the number of items in a list.
5809 * Return TRUE when two lists have exactly the same values.
5812 list_equal(l1
, l2
, ic
)
5815 int ic
; /* ignore case for strings */
5817 listitem_T
*item1
, *item2
;
5821 if (list_len(l1
) != list_len(l2
))
5824 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5825 item1
!= NULL
&& item2
!= NULL
;
5826 item1
= item1
->li_next
, item2
= item2
->li_next
)
5827 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5829 return item1
== NULL
&& item2
== NULL
;
5832 #if defined(FEAT_PYTHON) || defined(PROTO)
5834 * Return the dictitem that an entry in a hashtable points to.
5845 * Return TRUE when two dictionaries have exactly the same key/values.
5848 dict_equal(d1
, d2
, ic
)
5851 int ic
; /* ignore case for strings */
5859 if (dict_len(d1
) != dict_len(d2
))
5862 todo
= (int)d1
->dv_hashtab
.ht_used
;
5863 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5865 if (!HASHITEM_EMPTY(hi
))
5867 item2
= dict_find(d2
, hi
->hi_key
, -1);
5870 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5879 * Return TRUE if "tv1" and "tv2" have the same value.
5880 * Compares the items just like "==" would compare them, but strings and
5881 * numbers are different. Floats and numbers are also different.
5884 tv_equal(tv1
, tv2
, ic
)
5887 int ic
; /* ignore case */
5889 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5891 static int recursive
= 0; /* cach recursive loops */
5894 if (tv1
->v_type
!= tv2
->v_type
)
5896 /* Catch lists and dicts that have an endless loop by limiting
5897 * recursiveness to 1000. We guess they are equal then. */
5898 if (recursive
>= 1000)
5901 switch (tv1
->v_type
)
5905 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5911 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5916 return (tv1
->vval
.v_string
!= NULL
5917 && tv2
->vval
.v_string
!= NULL
5918 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5921 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5925 return tv1
->vval
.v_float
== tv2
->vval
.v_float
;
5929 s1
= get_tv_string_buf(tv1
, buf1
);
5930 s2
= get_tv_string_buf(tv2
, buf2
);
5931 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5934 EMSG2(_(e_intern2
), "tv_equal()");
5939 * Locate item with index "n" in list "l" and return it.
5940 * A negative index is counted from the end; -1 is the last item.
5941 * Returns NULL when "n" is out of range.
5954 /* Negative index is relative to the end. */
5958 /* Check for index out of range. */
5959 if (n
< 0 || n
>= l
->lv_len
)
5962 /* When there is a cached index may start search from there. */
5963 if (l
->lv_idx_item
!= NULL
)
5965 if (n
< l
->lv_idx
/ 2)
5967 /* closest to the start of the list */
5971 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
5973 /* closest to the end of the list */
5975 idx
= l
->lv_len
- 1;
5979 /* closest to the cached index */
5980 item
= l
->lv_idx_item
;
5986 if (n
< l
->lv_len
/ 2)
5988 /* closest to the start of the list */
5994 /* closest to the end of the list */
5996 idx
= l
->lv_len
- 1;
6002 /* search forward */
6003 item
= item
->li_next
;
6008 /* search backward */
6009 item
= item
->li_prev
;
6013 /* cache the used index */
6015 l
->lv_idx_item
= item
;
6021 * Get list item "l[idx]" as a number.
6024 list_find_nr(l
, idx
, errorp
)
6027 int *errorp
; /* set to TRUE when something wrong */
6031 li
= list_find(l
, idx
);
6038 return get_tv_number_chk(&li
->li_tv
, errorp
);
6042 * Locate "item" list "l" and return its index.
6043 * Returns -1 when "item" is not in the list.
6046 list_idx_of_item(l
, item
)
6056 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
6064 * Append item "item" to the end of list "l".
6067 list_append(l
, item
)
6071 if (l
->lv_last
== NULL
)
6076 item
->li_prev
= NULL
;
6080 l
->lv_last
->li_next
= item
;
6081 item
->li_prev
= l
->lv_last
;
6085 item
->li_next
= NULL
;
6089 * Append typval_T "tv" to the end of list "l".
6090 * Return FAIL when out of memory.
6093 list_append_tv(l
, tv
)
6097 listitem_T
*li
= listitem_alloc();
6101 copy_tv(tv
, &li
->li_tv
);
6107 * Add a dictionary to a list. Used by getqflist().
6108 * Return FAIL when out of memory.
6111 list_append_dict(list
, dict
)
6115 listitem_T
*li
= listitem_alloc();
6119 li
->li_tv
.v_type
= VAR_DICT
;
6120 li
->li_tv
.v_lock
= 0;
6121 li
->li_tv
.vval
.v_dict
= dict
;
6122 list_append(list
, li
);
6123 ++dict
->dv_refcount
;
6128 * Make a copy of "str" and append it as an item to list "l".
6129 * When "len" >= 0 use "str[len]".
6130 * Returns FAIL when out of memory.
6133 list_append_string(l
, str
, len
)
6138 listitem_T
*li
= listitem_alloc();
6143 li
->li_tv
.v_type
= VAR_STRING
;
6144 li
->li_tv
.v_lock
= 0;
6146 li
->li_tv
.vval
.v_string
= NULL
;
6147 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
6148 : vim_strsave(str
))) == NULL
)
6154 * Append "n" to list "l".
6155 * Returns FAIL when out of memory.
6158 list_append_number(l
, n
)
6164 li
= listitem_alloc();
6167 li
->li_tv
.v_type
= VAR_NUMBER
;
6168 li
->li_tv
.v_lock
= 0;
6169 li
->li_tv
.vval
.v_number
= n
;
6175 * Insert typval_T "tv" in list "l" before "item".
6176 * If "item" is NULL append at the end.
6177 * Return FAIL when out of memory.
6180 list_insert_tv(l
, tv
, item
)
6185 listitem_T
*ni
= listitem_alloc();
6189 copy_tv(tv
, &ni
->li_tv
);
6191 /* Append new item at end of list. */
6195 /* Insert new item before existing item. */
6196 ni
->li_prev
= item
->li_prev
;
6198 if (item
->li_prev
== NULL
)
6205 item
->li_prev
->li_next
= ni
;
6206 l
->lv_idx_item
= NULL
;
6215 * Extend "l1" with "l2".
6216 * If "bef" is NULL append at the end, otherwise insert before this item.
6217 * Returns FAIL when out of memory.
6220 list_extend(l1
, l2
, bef
)
6227 for (item
= l2
->lv_first
; item
!= NULL
; item
= item
->li_next
)
6228 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
6234 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6235 * Return FAIL when out of memory.
6238 list_concat(l1
, l2
, tv
)
6245 /* make a copy of the first list. */
6246 l
= list_copy(l1
, FALSE
, 0);
6249 tv
->v_type
= VAR_LIST
;
6250 tv
->vval
.v_list
= l
;
6252 /* append all items from the second list */
6253 return list_extend(l
, l2
, NULL
);
6257 * Make a copy of list "orig". Shallow if "deep" is FALSE.
6258 * The refcount of the new list is set to 1.
6259 * See item_copy() for "copyID".
6260 * Returns NULL when out of memory.
6263 list_copy(orig
, deep
, copyID
)
6275 copy
= list_alloc();
6280 /* Do this before adding the items, because one of the items may
6281 * refer back to this list. */
6282 orig
->lv_copyID
= copyID
;
6283 orig
->lv_copylist
= copy
;
6285 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
6286 item
= item
->li_next
)
6288 ni
= listitem_alloc();
6293 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
6300 copy_tv(&item
->li_tv
, &ni
->li_tv
);
6301 list_append(copy
, ni
);
6303 ++copy
->lv_refcount
;
6315 * Remove items "item" to "item2" from list "l".
6316 * Does not free the listitem or the value!
6319 list_remove(l
, item
, item2
)
6326 /* notify watchers */
6327 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6330 list_fix_watch(l
, ip
);
6335 if (item2
->li_next
== NULL
)
6336 l
->lv_last
= item
->li_prev
;
6338 item2
->li_next
->li_prev
= item
->li_prev
;
6339 if (item
->li_prev
== NULL
)
6340 l
->lv_first
= item2
->li_next
;
6342 item
->li_prev
->li_next
= item2
->li_next
;
6343 l
->lv_idx_item
= NULL
;
6347 * Return an allocated string with the string representation of a list.
6351 list2string(tv
, copyID
)
6357 if (tv
->vval
.v_list
== NULL
)
6359 ga_init2(&ga
, (int)sizeof(char), 80);
6360 ga_append(&ga
, '[');
6361 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6363 vim_free(ga
.ga_data
);
6366 ga_append(&ga
, ']');
6367 ga_append(&ga
, NUL
);
6368 return (char_u
*)ga
.ga_data
;
6372 * Join list "l" into a string in "*gap", using separator "sep".
6373 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6374 * Return FAIL or OK.
6377 list_join(gap
, l
, sep
, echo
, copyID
)
6386 char_u numbuf
[NUMBUFLEN
];
6390 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6395 ga_concat(gap
, sep
);
6398 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6400 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6411 * Garbage collection for lists and dictionaries.
6413 * We use reference counts to be able to free most items right away when they
6414 * are no longer used. But for composite items it's possible that it becomes
6415 * unused while the reference count is > 0: When there is a recursive
6416 * reference. Example:
6417 * :let l = [1, 2, 3]
6421 * Since this is quite unusual we handle this with garbage collection: every
6422 * once in a while find out which lists and dicts are not referenced from any
6425 * Here is a good reference text about garbage collection (refers to Python
6426 * but it applies to all reference-counting mechanisms):
6427 * http://python.ca/nas/python/gc/
6431 * Do garbage collection for lists and dicts.
6432 * Return TRUE if some memory was freed.
6439 int copyID
= ++current_copyID
;
6444 int did_free
= FALSE
;
6449 /* Only do this once. */
6450 want_garbage_collect
= FALSE
;
6451 may_garbage_collect
= FALSE
;
6452 garbage_collect_at_exit
= FALSE
;
6455 * 1. Go through all accessible variables and mark all lists and dicts
6458 /* script-local variables */
6459 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6460 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6462 /* buffer-local variables */
6463 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6464 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6466 /* window-local variables */
6467 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6468 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6471 /* tabpage-local variables */
6472 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6473 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6476 /* global variables */
6477 set_ref_in_ht(&globvarht
, copyID
);
6479 /* function-local variables */
6480 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6482 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6483 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6487 * 2. Go through the list of dicts and free items without the copyID.
6489 for (dd
= first_dict
; dd
!= NULL
; )
6490 if (dd
->dv_copyID
!= copyID
)
6492 /* Free the Dictionary and ordinary items it contains, but don't
6493 * recurse into Lists and Dictionaries, they will be in the list
6494 * of dicts or list of lists. */
6495 dict_free(dd
, FALSE
);
6498 /* restart, next dict may also have been freed */
6502 dd
= dd
->dv_used_next
;
6505 * 3. Go through the list of lists and free items without the copyID.
6506 * But don't free a list that has a watcher (used in a for loop), these
6507 * are not referenced anywhere.
6509 for (ll
= first_list
; ll
!= NULL
; )
6510 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6512 /* Free the List and ordinary items it contains, but don't recurse
6513 * into Lists and Dictionaries, they will be in the list of dicts
6514 * or list of lists. */
6515 list_free(ll
, FALSE
);
6518 /* restart, next list may also have been freed */
6522 ll
= ll
->lv_used_next
;
6528 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6531 set_ref_in_ht(ht
, copyID
)
6538 todo
= (int)ht
->ht_used
;
6539 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6540 if (!HASHITEM_EMPTY(hi
))
6543 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6548 * Mark all lists and dicts referenced through list "l" with "copyID".
6551 set_ref_in_list(l
, copyID
)
6557 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6558 set_ref_in_item(&li
->li_tv
, copyID
);
6562 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6565 set_ref_in_item(tv
, copyID
)
6575 dd
= tv
->vval
.v_dict
;
6576 if (dd
->dv_copyID
!= copyID
)
6578 /* Didn't see this dict yet. */
6579 dd
->dv_copyID
= copyID
;
6580 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6585 ll
= tv
->vval
.v_list
;
6586 if (ll
->lv_copyID
!= copyID
)
6588 /* Didn't see this list yet. */
6589 ll
->lv_copyID
= copyID
;
6590 set_ref_in_list(ll
, copyID
);
6598 * Allocate an empty header for a dictionary.
6605 d
= (dict_T
*)alloc(sizeof(dict_T
));
6608 /* Add the list to the list of dicts for garbage collection. */
6609 if (first_dict
!= NULL
)
6610 first_dict
->dv_used_prev
= d
;
6611 d
->dv_used_next
= first_dict
;
6612 d
->dv_used_prev
= NULL
;
6615 hash_init(&d
->dv_hashtab
);
6624 * Unreference a Dictionary: decrement the reference count and free it when it
6631 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6636 * Free a Dictionary, including all items it contains.
6637 * Ignores the reference count.
6640 dict_free(d
, recurse
)
6642 int recurse
; /* Free Lists and Dictionaries recursively. */
6648 /* Remove the dict from the list of dicts for garbage collection. */
6649 if (d
->dv_used_prev
== NULL
)
6650 first_dict
= d
->dv_used_next
;
6652 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6653 if (d
->dv_used_next
!= NULL
)
6654 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6656 /* Lock the hashtab, we don't want it to resize while freeing items. */
6657 hash_lock(&d
->dv_hashtab
);
6658 todo
= (int)d
->dv_hashtab
.ht_used
;
6659 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6661 if (!HASHITEM_EMPTY(hi
))
6663 /* Remove the item before deleting it, just in case there is
6664 * something recursive causing trouble. */
6666 hash_remove(&d
->dv_hashtab
, hi
);
6667 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6668 && di
->di_tv
.v_type
!= VAR_DICT
))
6669 clear_tv(&di
->di_tv
);
6674 hash_clear(&d
->dv_hashtab
);
6679 * Allocate a Dictionary item.
6680 * The "key" is copied to the new item.
6681 * Note that the value of the item "di_tv" still needs to be initialized!
6682 * Returns NULL when out of memory.
6690 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6693 STRCPY(di
->di_key
, key
);
6700 * Make a copy of a Dictionary item.
6708 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6709 + STRLEN(org
->di_key
)));
6712 STRCPY(di
->di_key
, org
->di_key
);
6714 copy_tv(&org
->di_tv
, &di
->di_tv
);
6720 * Remove item "item" from Dictionary "dict" and free it.
6723 dictitem_remove(dict
, item
)
6729 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6730 if (HASHITEM_EMPTY(hi
))
6731 EMSG2(_(e_intern2
), "dictitem_remove()");
6733 hash_remove(&dict
->dv_hashtab
, hi
);
6734 dictitem_free(item
);
6738 * Free a dict item. Also clears the value.
6744 clear_tv(&item
->di_tv
);
6749 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6750 * The refcount of the new dict is set to 1.
6751 * See item_copy() for "copyID".
6752 * Returns NULL when out of memory.
6755 dict_copy(orig
, deep
, copyID
)
6768 copy
= dict_alloc();
6773 orig
->dv_copyID
= copyID
;
6774 orig
->dv_copydict
= copy
;
6776 todo
= (int)orig
->dv_hashtab
.ht_used
;
6777 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6779 if (!HASHITEM_EMPTY(hi
))
6783 di
= dictitem_alloc(hi
->hi_key
);
6788 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6796 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6797 if (dict_add(copy
, di
) == FAIL
)
6805 ++copy
->dv_refcount
;
6817 * Add item "item" to Dictionary "d".
6818 * Returns FAIL when out of memory and when key already existed.
6825 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6829 * Add a number or string entry to dictionary "d".
6830 * When "str" is NULL use number "nr", otherwise use "str".
6831 * Returns FAIL when out of memory and when key already exists.
6834 dict_add_nr_str(d
, key
, nr
, str
)
6842 item
= dictitem_alloc((char_u
*)key
);
6845 item
->di_tv
.v_lock
= 0;
6848 item
->di_tv
.v_type
= VAR_NUMBER
;
6849 item
->di_tv
.vval
.v_number
= nr
;
6853 item
->di_tv
.v_type
= VAR_STRING
;
6854 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6856 if (dict_add(d
, item
) == FAIL
)
6858 dictitem_free(item
);
6865 * Get the number of items in a Dictionary.
6873 return (long)d
->dv_hashtab
.ht_used
;
6877 * Find item "key[len]" in Dictionary "d".
6878 * If "len" is negative use strlen(key).
6879 * Returns NULL when not found.
6882 dict_find(d
, key
, len
)
6888 char_u buf
[AKEYLEN
];
6890 char_u
*tofree
= NULL
;
6895 else if (len
>= AKEYLEN
)
6897 tofree
= akey
= vim_strnsave(key
, len
);
6903 /* Avoid a malloc/free by using buf[]. */
6904 vim_strncpy(buf
, key
, len
);
6908 hi
= hash_find(&d
->dv_hashtab
, akey
);
6910 if (HASHITEM_EMPTY(hi
))
6916 * Get a string item from a dictionary.
6917 * When "save" is TRUE allocate memory for it.
6918 * Returns NULL if the entry doesn't exist or out of memory.
6921 get_dict_string(d
, key
, save
)
6929 di
= dict_find(d
, key
, -1);
6932 s
= get_tv_string(&di
->di_tv
);
6933 if (save
&& s
!= NULL
)
6939 * Get a number item from a dictionary.
6940 * Returns 0 if the entry doesn't exist or out of memory.
6943 get_dict_number(d
, key
)
6949 di
= dict_find(d
, key
, -1);
6952 return get_tv_number(&di
->di_tv
);
6956 * Return an allocated string with the string representation of a Dictionary.
6960 dict2string(tv
, copyID
)
6967 char_u numbuf
[NUMBUFLEN
];
6973 if ((d
= tv
->vval
.v_dict
) == NULL
)
6975 ga_init2(&ga
, (int)sizeof(char), 80);
6976 ga_append(&ga
, '{');
6978 todo
= (int)d
->dv_hashtab
.ht_used
;
6979 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6981 if (!HASHITEM_EMPTY(hi
))
6988 ga_concat(&ga
, (char_u
*)", ");
6990 tofree
= string_quote(hi
->hi_key
, FALSE
);
6993 ga_concat(&ga
, tofree
);
6996 ga_concat(&ga
, (char_u
*)": ");
6997 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
7007 vim_free(ga
.ga_data
);
7011 ga_append(&ga
, '}');
7012 ga_append(&ga
, NUL
);
7013 return (char_u
*)ga
.ga_data
;
7017 * Allocate a variable for a Dictionary and fill it from "*arg".
7018 * Return OK or FAIL. Returns NOTDONE for {expr}.
7021 get_dict_tv(arg
, rettv
, evaluate
)
7031 char_u
*start
= skipwhite(*arg
+ 1);
7032 char_u buf
[NUMBUFLEN
];
7035 * First check if it's not a curly-braces thing: {expr}.
7036 * Must do this without evaluating, otherwise a function may be called
7037 * twice. Unfortunately this means we need to call eval1() twice for the
7039 * But {} is an empty Dictionary.
7043 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
7055 tvkey
.v_type
= VAR_UNKNOWN
;
7056 tv
.v_type
= VAR_UNKNOWN
;
7058 *arg
= skipwhite(*arg
+ 1);
7059 while (**arg
!= '}' && **arg
!= NUL
)
7061 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
7065 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
7071 key
= get_tv_string_buf_chk(&tvkey
, buf
);
7072 if (key
== NULL
|| *key
== NUL
)
7074 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7076 EMSG(_(e_emptykey
));
7082 *arg
= skipwhite(*arg
+ 1);
7083 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
7091 item
= dict_find(d
, key
, -1);
7094 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
7099 item
= dictitem_alloc(key
);
7104 item
->di_tv
.v_lock
= 0;
7105 if (dict_add(d
, item
) == FAIL
)
7106 dictitem_free(item
);
7114 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
7117 *arg
= skipwhite(*arg
+ 1);
7122 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
7129 *arg
= skipwhite(*arg
+ 1);
7132 rettv
->v_type
= VAR_DICT
;
7133 rettv
->vval
.v_dict
= d
;
7141 * Return a string with the string representation of a variable.
7142 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7143 * "numbuf" is used for a number.
7144 * Does not put quotes around strings, as ":echo" displays values.
7145 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7149 echo_string(tv
, tofree
, numbuf
, copyID
)
7155 static int recurse
= 0;
7158 if (recurse
>= DICT_MAXNEST
)
7160 EMSG(_("E724: variable nested too deep for displaying"));
7170 r
= tv
->vval
.v_string
;
7174 if (tv
->vval
.v_list
== NULL
)
7179 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
7182 r
= (char_u
*)"[...]";
7186 tv
->vval
.v_list
->lv_copyID
= copyID
;
7187 *tofree
= list2string(tv
, copyID
);
7193 if (tv
->vval
.v_dict
== NULL
)
7198 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
7201 r
= (char_u
*)"{...}";
7205 tv
->vval
.v_dict
->dv_copyID
= copyID
;
7206 *tofree
= dict2string(tv
, copyID
);
7214 r
= get_tv_string_buf(tv
, numbuf
);
7220 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
->vval
.v_float
);
7226 EMSG2(_(e_intern2
), "echo_string()");
7235 * Return a string with the string representation of a variable.
7236 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7237 * "numbuf" is used for a number.
7238 * Puts quotes around strings, so that they can be parsed back by eval().
7242 tv2string(tv
, tofree
, numbuf
, copyID
)
7251 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
7254 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
7259 vim_snprintf((char *)numbuf
, NUMBUFLEN
- 1, "%g", tv
->vval
.v_float
);
7267 EMSG2(_(e_intern2
), "tv2string()");
7269 return echo_string(tv
, tofree
, numbuf
, copyID
);
7273 * Return string "str" in ' quotes, doubling ' characters.
7274 * If "str" is NULL an empty string is assumed.
7275 * If "function" is TRUE make it function('string').
7278 string_quote(str
, function
)
7285 len
= (function
? 13 : 3);
7288 len
+= (unsigned)STRLEN(str
);
7289 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
7298 STRCPY(r
, "function('");
7304 for (p
= str
; *p
!= NUL
; )
7320 * Convert the string "text" to a floating point number.
7321 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7322 * this always uses a decimal point.
7323 * Returns the length of the text that was consumed.
7326 string2float(text
, value
)
7328 float_T
*value
; /* result stored here */
7330 char *s
= (char *)text
;
7335 return (int)((char_u
*)s
- text
);
7340 * Get the value of an environment variable.
7341 * "arg" is pointing to the '$'. It is advanced to after the name.
7342 * If the environment variable was not set, silently assume it is empty.
7346 get_env_tv(arg
, rettv
, evaluate
)
7351 char_u
*string
= NULL
;
7355 int mustfree
= FALSE
;
7359 len
= get_env_len(arg
);
7366 /* first try vim_getenv(), fast for normal environment vars */
7367 string
= vim_getenv(name
, &mustfree
);
7368 if (string
!= NULL
&& *string
!= NUL
)
7371 string
= vim_strsave(string
);
7378 /* next try expanding things like $VIM and ${HOME} */
7379 string
= expand_env_save(name
- 1);
7380 if (string
!= NULL
&& *string
== '$')
7388 rettv
->v_type
= VAR_STRING
;
7389 rettv
->vval
.v_string
= string
;
7396 * Array with names and number of arguments of all internal functions
7397 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7401 char *f_name
; /* function name */
7402 char f_min_argc
; /* minimal number of arguments */
7403 char f_max_argc
; /* maximal number of arguments */
7404 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7405 /* implementation of function */
7409 {"abs", 1, 1, f_abs
},
7411 {"add", 2, 2, f_add
},
7412 {"append", 2, 2, f_append
},
7413 {"argc", 0, 0, f_argc
},
7414 {"argidx", 0, 0, f_argidx
},
7415 {"argv", 0, 1, f_argv
},
7417 {"atan", 1, 1, f_atan
},
7419 {"browse", 4, 4, f_browse
},
7420 {"browsedir", 2, 2, f_browsedir
},
7421 {"bufexists", 1, 1, f_bufexists
},
7422 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7423 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7424 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7425 {"buflisted", 1, 1, f_buflisted
},
7426 {"bufloaded", 1, 1, f_bufloaded
},
7427 {"bufname", 1, 1, f_bufname
},
7428 {"bufnr", 1, 2, f_bufnr
},
7429 {"bufwinnr", 1, 1, f_bufwinnr
},
7430 {"byte2line", 1, 1, f_byte2line
},
7431 {"byteidx", 2, 2, f_byteidx
},
7432 {"call", 2, 3, f_call
},
7434 {"ceil", 1, 1, f_ceil
},
7436 {"changenr", 0, 0, f_changenr
},
7437 {"char2nr", 1, 1, f_char2nr
},
7438 {"cindent", 1, 1, f_cindent
},
7439 {"clearmatches", 0, 0, f_clearmatches
},
7440 {"col", 1, 1, f_col
},
7441 #if defined(FEAT_INS_EXPAND)
7442 {"complete", 2, 2, f_complete
},
7443 {"complete_add", 1, 1, f_complete_add
},
7444 {"complete_check", 0, 0, f_complete_check
},
7446 {"confirm", 1, 4, f_confirm
},
7447 {"copy", 1, 1, f_copy
},
7449 {"cos", 1, 1, f_cos
},
7451 {"count", 2, 4, f_count
},
7452 {"cscope_connection",0,3, f_cscope_connection
},
7453 {"cursor", 1, 3, f_cursor
},
7454 {"deepcopy", 1, 2, f_deepcopy
},
7455 {"delete", 1, 1, f_delete
},
7456 {"did_filetype", 0, 0, f_did_filetype
},
7457 {"diff_filler", 1, 1, f_diff_filler
},
7458 {"diff_hlID", 2, 2, f_diff_hlID
},
7459 {"empty", 1, 1, f_empty
},
7460 {"escape", 2, 2, f_escape
},
7461 {"eval", 1, 1, f_eval
},
7462 {"eventhandler", 0, 0, f_eventhandler
},
7463 {"executable", 1, 1, f_executable
},
7464 {"exists", 1, 1, f_exists
},
7465 {"expand", 1, 2, f_expand
},
7466 {"extend", 2, 3, f_extend
},
7467 {"feedkeys", 1, 2, f_feedkeys
},
7468 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7469 {"filereadable", 1, 1, f_filereadable
},
7470 {"filewritable", 1, 1, f_filewritable
},
7471 {"filter", 2, 2, f_filter
},
7472 {"finddir", 1, 3, f_finddir
},
7473 {"findfile", 1, 3, f_findfile
},
7475 {"float2nr", 1, 1, f_float2nr
},
7476 {"floor", 1, 1, f_floor
},
7478 {"fnameescape", 1, 1, f_fnameescape
},
7479 {"fnamemodify", 2, 2, f_fnamemodify
},
7480 {"foldclosed", 1, 1, f_foldclosed
},
7481 {"foldclosedend", 1, 1, f_foldclosedend
},
7482 {"foldlevel", 1, 1, f_foldlevel
},
7483 {"foldtext", 0, 0, f_foldtext
},
7484 {"foldtextresult", 1, 1, f_foldtextresult
},
7485 {"foreground", 0, 0, f_foreground
},
7486 {"function", 1, 1, f_function
},
7487 {"garbagecollect", 0, 1, f_garbagecollect
},
7488 {"get", 2, 3, f_get
},
7489 {"getbufline", 2, 3, f_getbufline
},
7490 {"getbufvar", 2, 2, f_getbufvar
},
7491 {"getchar", 0, 1, f_getchar
},
7492 {"getcharmod", 0, 0, f_getcharmod
},
7493 {"getcmdline", 0, 0, f_getcmdline
},
7494 {"getcmdpos", 0, 0, f_getcmdpos
},
7495 {"getcmdtype", 0, 0, f_getcmdtype
},
7496 {"getcwd", 0, 0, f_getcwd
},
7497 {"getfontname", 0, 1, f_getfontname
},
7498 {"getfperm", 1, 1, f_getfperm
},
7499 {"getfsize", 1, 1, f_getfsize
},
7500 {"getftime", 1, 1, f_getftime
},
7501 {"getftype", 1, 1, f_getftype
},
7502 {"getline", 1, 2, f_getline
},
7503 {"getloclist", 1, 1, f_getqflist
},
7504 {"getmatches", 0, 0, f_getmatches
},
7505 {"getpid", 0, 0, f_getpid
},
7506 {"getpos", 1, 1, f_getpos
},
7507 {"getqflist", 0, 0, f_getqflist
},
7508 {"getreg", 0, 2, f_getreg
},
7509 {"getregtype", 0, 1, f_getregtype
},
7510 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7511 {"getwinposx", 0, 0, f_getwinposx
},
7512 {"getwinposy", 0, 0, f_getwinposy
},
7513 {"getwinvar", 2, 2, f_getwinvar
},
7514 {"glob", 1, 1, f_glob
},
7515 {"globpath", 2, 2, f_globpath
},
7516 {"has", 1, 1, f_has
},
7517 {"has_key", 2, 2, f_has_key
},
7518 {"haslocaldir", 0, 0, f_haslocaldir
},
7519 {"hasmapto", 1, 3, f_hasmapto
},
7520 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7521 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7522 {"histadd", 2, 2, f_histadd
},
7523 {"histdel", 1, 2, f_histdel
},
7524 {"histget", 1, 2, f_histget
},
7525 {"histnr", 1, 1, f_histnr
},
7526 {"hlID", 1, 1, f_hlID
},
7527 {"hlexists", 1, 1, f_hlexists
},
7528 {"hostname", 0, 0, f_hostname
},
7529 {"iconv", 3, 3, f_iconv
},
7530 {"indent", 1, 1, f_indent
},
7531 {"index", 2, 4, f_index
},
7532 {"input", 1, 3, f_input
},
7533 {"inputdialog", 1, 3, f_inputdialog
},
7534 {"inputlist", 1, 1, f_inputlist
},
7535 {"inputrestore", 0, 0, f_inputrestore
},
7536 {"inputsave", 0, 0, f_inputsave
},
7537 {"inputsecret", 1, 2, f_inputsecret
},
7538 {"insert", 2, 3, f_insert
},
7539 {"isdirectory", 1, 1, f_isdirectory
},
7540 {"islocked", 1, 1, f_islocked
},
7541 {"items", 1, 1, f_items
},
7542 {"join", 1, 2, f_join
},
7543 {"keys", 1, 1, f_keys
},
7544 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7545 {"len", 1, 1, f_len
},
7546 {"libcall", 3, 3, f_libcall
},
7547 {"libcallnr", 3, 3, f_libcallnr
},
7548 {"line", 1, 1, f_line
},
7549 {"line2byte", 1, 1, f_line2byte
},
7550 {"lispindent", 1, 1, f_lispindent
},
7551 {"localtime", 0, 0, f_localtime
},
7553 {"log10", 1, 1, f_log10
},
7555 {"map", 2, 2, f_map
},
7556 {"maparg", 1, 3, f_maparg
},
7557 {"mapcheck", 1, 3, f_mapcheck
},
7558 {"match", 2, 4, f_match
},
7559 {"matchadd", 2, 4, f_matchadd
},
7560 {"matcharg", 1, 1, f_matcharg
},
7561 {"matchdelete", 1, 1, f_matchdelete
},
7562 {"matchend", 2, 4, f_matchend
},
7563 {"matchlist", 2, 4, f_matchlist
},
7564 {"matchstr", 2, 4, f_matchstr
},
7565 {"max", 1, 1, f_max
},
7566 {"min", 1, 1, f_min
},
7568 {"mkdir", 1, 3, f_mkdir
},
7570 {"mode", 0, 1, f_mode
},
7571 {"nextnonblank", 1, 1, f_nextnonblank
},
7572 {"nr2char", 1, 1, f_nr2char
},
7573 {"pathshorten", 1, 1, f_pathshorten
},
7575 {"pow", 2, 2, f_pow
},
7577 {"prevnonblank", 1, 1, f_prevnonblank
},
7578 {"printf", 2, 19, f_printf
},
7579 {"pumvisible", 0, 0, f_pumvisible
},
7580 {"range", 1, 3, f_range
},
7581 {"readfile", 1, 3, f_readfile
},
7582 {"reltime", 0, 2, f_reltime
},
7583 {"reltimestr", 1, 1, f_reltimestr
},
7584 {"remote_expr", 2, 3, f_remote_expr
},
7585 {"remote_foreground", 1, 1, f_remote_foreground
},
7586 {"remote_peek", 1, 2, f_remote_peek
},
7587 {"remote_read", 1, 1, f_remote_read
},
7588 {"remote_send", 2, 3, f_remote_send
},
7589 {"remove", 2, 3, f_remove
},
7590 {"rename", 2, 2, f_rename
},
7591 {"repeat", 2, 2, f_repeat
},
7592 {"resolve", 1, 1, f_resolve
},
7593 {"reverse", 1, 1, f_reverse
},
7595 {"round", 1, 1, f_round
},
7597 {"search", 1, 4, f_search
},
7598 {"searchdecl", 1, 3, f_searchdecl
},
7599 {"searchpair", 3, 7, f_searchpair
},
7600 {"searchpairpos", 3, 7, f_searchpairpos
},
7601 {"searchpos", 1, 4, f_searchpos
},
7602 {"server2client", 2, 2, f_server2client
},
7603 {"serverlist", 0, 0, f_serverlist
},
7604 {"setbufvar", 3, 3, f_setbufvar
},
7605 {"setcmdpos", 1, 1, f_setcmdpos
},
7606 {"setline", 2, 2, f_setline
},
7607 {"setloclist", 2, 3, f_setloclist
},
7608 {"setmatches", 1, 1, f_setmatches
},
7609 {"setpos", 2, 2, f_setpos
},
7610 {"setqflist", 1, 2, f_setqflist
},
7611 {"setreg", 2, 3, f_setreg
},
7612 {"settabwinvar", 4, 4, f_settabwinvar
},
7613 {"setwinvar", 3, 3, f_setwinvar
},
7614 {"shellescape", 1, 1, f_shellescape
},
7615 {"simplify", 1, 1, f_simplify
},
7617 {"sin", 1, 1, f_sin
},
7619 {"sort", 1, 2, f_sort
},
7620 {"soundfold", 1, 1, f_soundfold
},
7621 {"spellbadword", 0, 1, f_spellbadword
},
7622 {"spellsuggest", 1, 3, f_spellsuggest
},
7623 {"split", 1, 3, f_split
},
7625 {"sqrt", 1, 1, f_sqrt
},
7626 {"str2float", 1, 1, f_str2float
},
7628 {"str2nr", 1, 2, f_str2nr
},
7629 #ifdef HAVE_STRFTIME
7630 {"strftime", 1, 2, f_strftime
},
7632 {"stridx", 2, 3, f_stridx
},
7633 {"string", 1, 1, f_string
},
7634 {"strlen", 1, 1, f_strlen
},
7635 {"strpart", 2, 3, f_strpart
},
7636 {"strridx", 2, 3, f_strridx
},
7637 {"strtrans", 1, 1, f_strtrans
},
7638 {"submatch", 1, 1, f_submatch
},
7639 {"substitute", 4, 4, f_substitute
},
7640 {"synID", 3, 3, f_synID
},
7641 {"synIDattr", 2, 3, f_synIDattr
},
7642 {"synIDtrans", 1, 1, f_synIDtrans
},
7643 {"synstack", 2, 2, f_synstack
},
7644 {"system", 1, 2, f_system
},
7645 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7646 {"tabpagenr", 0, 1, f_tabpagenr
},
7647 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7648 {"tagfiles", 0, 0, f_tagfiles
},
7649 {"taglist", 1, 1, f_taglist
},
7650 {"tempname", 0, 0, f_tempname
},
7651 {"test", 1, 1, f_test
},
7652 {"tolower", 1, 1, f_tolower
},
7653 {"toupper", 1, 1, f_toupper
},
7656 {"trunc", 1, 1, f_trunc
},
7658 {"type", 1, 1, f_type
},
7659 {"values", 1, 1, f_values
},
7660 {"virtcol", 1, 1, f_virtcol
},
7661 {"visualmode", 0, 1, f_visualmode
},
7662 {"winbufnr", 1, 1, f_winbufnr
},
7663 {"wincol", 0, 0, f_wincol
},
7664 {"winheight", 1, 1, f_winheight
},
7665 {"winline", 0, 0, f_winline
},
7666 {"winnr", 0, 1, f_winnr
},
7667 {"winrestcmd", 0, 0, f_winrestcmd
},
7668 {"winrestview", 1, 1, f_winrestview
},
7669 {"winsaveview", 0, 0, f_winsaveview
},
7670 {"winwidth", 1, 1, f_winwidth
},
7671 {"writefile", 2, 3, f_writefile
},
7674 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7677 * Function given to ExpandGeneric() to obtain the list of internal
7678 * or user defined function names.
7681 get_function_name(xp
, idx
)
7685 static int intidx
= -1;
7692 name
= get_user_func_name(xp
, idx
);
7696 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7698 STRCPY(IObuff
, functions
[intidx
].f_name
);
7699 STRCAT(IObuff
, "(");
7700 if (functions
[intidx
].f_max_argc
== 0)
7701 STRCAT(IObuff
, ")");
7709 * Function given to ExpandGeneric() to obtain the list of internal or
7710 * user defined variable or function names.
7714 get_expr_name(xp
, idx
)
7718 static int intidx
= -1;
7725 name
= get_function_name(xp
, idx
);
7729 return get_user_var_name(xp
, ++intidx
);
7732 #endif /* FEAT_CMDL_COMPL */
7735 * Find internal function in table above.
7736 * Return index, or -1 if not found
7739 find_internal_func(name
)
7740 char_u
*name
; /* name of the function */
7743 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7748 * Find the function name in the table. Binary search.
7750 while (first
<= last
)
7752 x
= first
+ ((unsigned)(last
- first
) >> 1);
7753 cmp
= STRCMP(name
, functions
[x
].f_name
);
7765 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7766 * name it contains, otherwise return "name".
7769 deref_func_name(name
, lenp
)
7778 v
= find_var(name
, NULL
);
7780 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7782 if (v
->di_tv
.vval
.v_string
== NULL
)
7785 return (char_u
*)""; /* just in case */
7787 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7788 return v
->di_tv
.vval
.v_string
;
7795 * Allocate a variable for the result of a function.
7796 * Return OK or FAIL.
7799 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7801 char_u
*name
; /* name of the function */
7802 int len
; /* length of "name" */
7804 char_u
**arg
; /* argument, pointing to the '(' */
7805 linenr_T firstline
; /* first line of range */
7806 linenr_T lastline
; /* last line of range */
7807 int *doesrange
; /* return: function handled range */
7809 dict_T
*selfdict
; /* Dictionary for "self" */
7813 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7814 int argcount
= 0; /* number of arguments found */
7817 * Get the arguments.
7820 while (argcount
< MAX_FUNC_ARGS
)
7822 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7823 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7825 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7840 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7841 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7842 else if (!aborting())
7844 if (argcount
== MAX_FUNC_ARGS
)
7845 emsg_funcname("E740: Too many arguments for function %s", name
);
7847 emsg_funcname("E116: Invalid arguments for function %s", name
);
7850 while (--argcount
>= 0)
7851 clear_tv(&argvars
[argcount
]);
7853 *arg
= skipwhite(argp
);
7859 * Call a function with its resolved parameters
7860 * Return OK when the function can't be called, FAIL otherwise.
7861 * Also returns OK when an error was encountered while executing the function.
7864 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7865 doesrange
, evaluate
, selfdict
)
7866 char_u
*name
; /* name of the function */
7867 int len
; /* length of "name" */
7868 typval_T
*rettv
; /* return value goes here */
7869 int argcount
; /* number of "argvars" */
7870 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7871 PLUS ONE elements! */
7872 linenr_T firstline
; /* first line of range */
7873 linenr_T lastline
; /* last line of range */
7874 int *doesrange
; /* return: function handled range */
7876 dict_T
*selfdict
; /* Dictionary for "self" */
7879 #define ERROR_UNKNOWN 0
7880 #define ERROR_TOOMANY 1
7881 #define ERROR_TOOFEW 2
7882 #define ERROR_SCRIPT 3
7883 #define ERROR_DICT 4
7884 #define ERROR_NONE 5
7885 #define ERROR_OTHER 6
7886 int error
= ERROR_NONE
;
7891 #define FLEN_FIXED 40
7892 char_u fname_buf
[FLEN_FIXED
+ 1];
7896 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7897 * Change <SNR>123_name() to K_SNR 123_name().
7898 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7902 llen
= eval_fname_script(name
);
7905 fname_buf
[0] = K_SPECIAL
;
7906 fname_buf
[1] = KS_EXTRA
;
7907 fname_buf
[2] = (int)KE_SNR
;
7909 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
7911 if (current_SID
<= 0)
7912 error
= ERROR_SCRIPT
;
7915 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
7916 i
= (int)STRLEN(fname_buf
);
7919 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
7921 STRCPY(fname_buf
+ i
, name
+ llen
);
7926 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
7928 error
= ERROR_OTHER
;
7931 mch_memmove(fname
, fname_buf
, (size_t)i
);
7932 STRCPY(fname
+ i
, name
+ llen
);
7942 /* execute the function if no errors detected and executing */
7943 if (evaluate
&& error
== ERROR_NONE
)
7945 rettv
->v_type
= VAR_NUMBER
; /* default is number rettv */
7946 error
= ERROR_UNKNOWN
;
7948 if (!builtin_function(fname
))
7951 * User defined function.
7953 fp
= find_func(fname
);
7956 /* Trigger FuncUndefined event, may load the function. */
7958 && apply_autocmds(EVENT_FUNCUNDEFINED
,
7959 fname
, fname
, TRUE
, NULL
)
7962 /* executed an autocommand, search for the function again */
7963 fp
= find_func(fname
);
7966 /* Try loading a package. */
7967 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
7969 /* loaded a package, search for the function again */
7970 fp
= find_func(fname
);
7975 if (fp
->uf_flags
& FC_RANGE
)
7977 if (argcount
< fp
->uf_args
.ga_len
)
7978 error
= ERROR_TOOFEW
;
7979 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
7980 error
= ERROR_TOOMANY
;
7981 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
7986 * Call the user function.
7987 * Save and restore search patterns, script variables and
7990 save_search_patterns();
7993 call_user_func(fp
, argcount
, argvars
, rettv
,
7994 firstline
, lastline
,
7995 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
7996 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
7997 && fp
->uf_refcount
<= 0)
7998 /* Function was unreferenced while being used, free it
8002 restore_search_patterns();
8010 * Find the function name in the table, call its implementation.
8012 i
= find_internal_func(fname
);
8015 if (argcount
< functions
[i
].f_min_argc
)
8016 error
= ERROR_TOOFEW
;
8017 else if (argcount
> functions
[i
].f_max_argc
)
8018 error
= ERROR_TOOMANY
;
8021 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
8022 functions
[i
].f_func(argvars
, rettv
);
8028 * The function call (or "FuncUndefined" autocommand sequence) might
8029 * have been aborted by an error, an interrupt, or an explicitly thrown
8030 * exception that has not been caught so far. This situation can be
8031 * tested for by calling aborting(). For an error in an internal
8032 * function or for the "E132" error in call_user_func(), however, the
8033 * throw point at which the "force_abort" flag (temporarily reset by
8034 * emsg()) is normally updated has not been reached yet. We need to
8035 * update that flag first to make aborting() reliable.
8037 update_force_abort();
8039 if (error
== ERROR_NONE
)
8043 * Report an error unless the argument evaluation or function call has been
8044 * cancelled due to an aborting error, an interrupt, or an exception.
8051 emsg_funcname(N_("E117: Unknown function: %s"), name
);
8054 emsg_funcname(e_toomanyarg
, name
);
8057 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
8061 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
8065 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
8072 if (fname
!= name
&& fname
!= fname_buf
)
8079 * Give an error message with a function name. Handle <SNR> things.
8082 emsg_funcname(ermsg
, name
)
8088 if (*name
== K_SPECIAL
)
8089 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
8097 /*********************************************
8098 * Implementation of the built-in functions
8103 * "abs(expr)" function
8106 f_abs(argvars
, rettv
)
8110 if (argvars
[0].v_type
== VAR_FLOAT
)
8112 rettv
->v_type
= VAR_FLOAT
;
8113 rettv
->vval
.v_float
= fabs(argvars
[0].vval
.v_float
);
8120 n
= get_tv_number_chk(&argvars
[0], &error
);
8122 rettv
->vval
.v_number
= -1;
8124 rettv
->vval
.v_number
= n
;
8126 rettv
->vval
.v_number
= -n
;
8132 * "add(list, item)" function
8135 f_add(argvars
, rettv
)
8141 rettv
->vval
.v_number
= 1; /* Default: Failed */
8142 if (argvars
[0].v_type
== VAR_LIST
)
8144 if ((l
= argvars
[0].vval
.v_list
) != NULL
8145 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
8146 && list_append_tv(l
, &argvars
[1]) == OK
)
8147 copy_tv(&argvars
[0], rettv
);
8154 * "append(lnum, string/list)" function
8157 f_append(argvars
, rettv
)
8164 listitem_T
*li
= NULL
;
8168 lnum
= get_tv_lnum(argvars
);
8170 && lnum
<= curbuf
->b_ml
.ml_line_count
8171 && u_save(lnum
, lnum
+ 1) == OK
)
8173 if (argvars
[1].v_type
== VAR_LIST
)
8175 l
= argvars
[1].vval
.v_list
;
8180 rettv
->vval
.v_number
= 0; /* Default: Success */
8184 tv
= &argvars
[1]; /* append a string */
8185 else if (li
== NULL
)
8186 break; /* end of list */
8188 tv
= &li
->li_tv
; /* append item from list */
8189 line
= get_tv_string_chk(tv
);
8190 if (line
== NULL
) /* type error */
8192 rettv
->vval
.v_number
= 1; /* Failed */
8195 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
8202 appended_lines_mark(lnum
, added
);
8203 if (curwin
->w_cursor
.lnum
> lnum
)
8204 curwin
->w_cursor
.lnum
+= added
;
8207 rettv
->vval
.v_number
= 1; /* Failed */
8215 f_argc(argvars
, rettv
)
8219 rettv
->vval
.v_number
= ARGCOUNT
;
8223 * "argidx()" function
8227 f_argidx(argvars
, rettv
)
8231 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
8235 * "argv(nr)" function
8238 f_argv(argvars
, rettv
)
8244 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
8246 idx
= get_tv_number_chk(&argvars
[0], NULL
);
8247 if (idx
>= 0 && idx
< ARGCOUNT
)
8248 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
8250 rettv
->vval
.v_string
= NULL
;
8251 rettv
->v_type
= VAR_STRING
;
8253 else if (rettv_list_alloc(rettv
) == OK
)
8254 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
8255 list_append_string(rettv
->vval
.v_list
,
8256 alist_name(&ARGLIST
[idx
]), -1);
8260 static int get_float_arg
__ARGS((typval_T
*argvars
, float_T
*f
));
8263 * Get the float value of "argvars[0]" into "f".
8264 * Returns FAIL when the argument is not a Number or Float.
8267 get_float_arg(argvars
, f
)
8271 if (argvars
[0].v_type
== VAR_FLOAT
)
8273 *f
= argvars
[0].vval
.v_float
;
8276 if (argvars
[0].v_type
== VAR_NUMBER
)
8278 *f
= (float_T
)argvars
[0].vval
.v_number
;
8281 EMSG(_("E808: Number or Float required"));
8289 f_atan(argvars
, rettv
)
8295 rettv
->v_type
= VAR_FLOAT
;
8296 if (get_float_arg(argvars
, &f
) == OK
)
8297 rettv
->vval
.v_float
= atan(f
);
8299 rettv
->vval
.v_float
= 0.0;
8304 * "browse(save, title, initdir, default)" function
8308 f_browse(argvars
, rettv
)
8317 char_u buf
[NUMBUFLEN
];
8318 char_u buf2
[NUMBUFLEN
];
8321 save
= get_tv_number_chk(&argvars
[0], &error
);
8322 title
= get_tv_string_chk(&argvars
[1]);
8323 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
8324 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8326 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
8327 rettv
->vval
.v_string
= NULL
;
8329 rettv
->vval
.v_string
=
8330 do_browse(save
? BROWSE_SAVE
: 0,
8331 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
8333 rettv
->vval
.v_string
= NULL
;
8335 rettv
->v_type
= VAR_STRING
;
8339 * "browsedir(title, initdir)" function
8343 f_browsedir(argvars
, rettv
)
8350 char_u buf
[NUMBUFLEN
];
8352 title
= get_tv_string_chk(&argvars
[0]);
8353 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
8355 if (title
== NULL
|| initdir
== NULL
)
8356 rettv
->vval
.v_string
= NULL
;
8358 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
8359 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
8361 rettv
->vval
.v_string
= NULL
;
8363 rettv
->v_type
= VAR_STRING
;
8366 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
8369 * Find a buffer by number or exact name.
8377 if (avar
->v_type
== VAR_NUMBER
)
8378 buf
= buflist_findnr((int)avar
->vval
.v_number
);
8379 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
8381 buf
= buflist_findname_exp(avar
->vval
.v_string
);
8384 /* No full path name match, try a match with a URL or a "nofile"
8385 * buffer, these don't use the full path. */
8386 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
8387 if (buf
->b_fname
!= NULL
8388 && (path_with_url(buf
->b_fname
)
8389 #ifdef FEAT_QUICKFIX
8393 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
8401 * "bufexists(expr)" function
8404 f_bufexists(argvars
, rettv
)
8408 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
8412 * "buflisted(expr)" function
8415 f_buflisted(argvars
, rettv
)
8421 buf
= find_buffer(&argvars
[0]);
8422 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
8426 * "bufloaded(expr)" function
8429 f_bufloaded(argvars
, rettv
)
8435 buf
= find_buffer(&argvars
[0]);
8436 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
8439 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
8442 * Get buffer by number or pattern.
8448 char_u
*name
= tv
->vval
.v_string
;
8453 if (tv
->v_type
== VAR_NUMBER
)
8454 return buflist_findnr((int)tv
->vval
.v_number
);
8455 if (tv
->v_type
!= VAR_STRING
)
8457 if (name
== NULL
|| *name
== NUL
)
8459 if (name
[0] == '$' && name
[1] == NUL
)
8462 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8463 save_magic
= p_magic
;
8466 p_cpo
= (char_u
*)"";
8468 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8471 p_magic
= save_magic
;
8474 /* If not found, try expanding the name, like done for bufexists(). */
8476 buf
= find_buffer(tv
);
8482 * "bufname(expr)" function
8485 f_bufname(argvars
, rettv
)
8491 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8493 buf
= get_buf_tv(&argvars
[0]);
8494 rettv
->v_type
= VAR_STRING
;
8495 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8496 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8498 rettv
->vval
.v_string
= NULL
;
8503 * "bufnr(expr)" function
8506 f_bufnr(argvars
, rettv
)
8514 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8516 buf
= get_buf_tv(&argvars
[0]);
8519 /* If the buffer isn't found and the second argument is not zero create a
8522 && argvars
[1].v_type
!= VAR_UNKNOWN
8523 && get_tv_number_chk(&argvars
[1], &error
) != 0
8525 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8527 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8530 rettv
->vval
.v_number
= buf
->b_fnum
;
8532 rettv
->vval
.v_number
= -1;
8536 * "bufwinnr(nr)" function
8539 f_bufwinnr(argvars
, rettv
)
8549 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8551 buf
= get_buf_tv(&argvars
[0]);
8553 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8556 if (wp
->w_buffer
== buf
)
8559 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8561 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8567 * "byte2line(byte)" function
8571 f_byte2line(argvars
, rettv
)
8575 #ifndef FEAT_BYTEOFF
8576 rettv
->vval
.v_number
= -1;
8580 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8582 rettv
->vval
.v_number
= -1;
8584 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8585 (linenr_T
)0, &boff
);
8590 * "byteidx()" function
8594 f_byteidx(argvars
, rettv
)
8604 str
= get_tv_string_chk(&argvars
[0]);
8605 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8606 rettv
->vval
.v_number
= -1;
8607 if (str
== NULL
|| idx
< 0)
8612 for ( ; idx
> 0; idx
--)
8614 if (*t
== NUL
) /* EOL reached */
8616 t
+= (*mb_ptr2len
)(t
);
8618 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8620 if ((size_t)idx
<= STRLEN(str
))
8621 rettv
->vval
.v_number
= idx
;
8626 * "call(func, arglist)" function
8629 f_call(argvars
, rettv
)
8634 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8638 dict_T
*selfdict
= NULL
;
8640 rettv
->vval
.v_number
= 0;
8641 if (argvars
[1].v_type
!= VAR_LIST
)
8646 if (argvars
[1].vval
.v_list
== NULL
)
8649 if (argvars
[0].v_type
== VAR_FUNC
)
8650 func
= argvars
[0].vval
.v_string
;
8652 func
= get_tv_string(&argvars
[0]);
8654 return; /* type error or empty name */
8656 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8658 if (argvars
[2].v_type
!= VAR_DICT
)
8663 selfdict
= argvars
[2].vval
.v_dict
;
8666 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8667 item
= item
->li_next
)
8669 if (argc
== MAX_FUNC_ARGS
)
8671 EMSG(_("E699: Too many arguments"));
8674 /* Make a copy of each argument. This is needed to be able to set
8675 * v_lock to VAR_FIXED in the copy without changing the original list.
8677 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8681 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8682 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8683 &dummy
, TRUE
, selfdict
);
8685 /* Free the arguments. */
8687 clear_tv(&argv
[--argc
]);
8692 * "ceil({float})" function
8695 f_ceil(argvars
, rettv
)
8701 rettv
->v_type
= VAR_FLOAT
;
8702 if (get_float_arg(argvars
, &f
) == OK
)
8703 rettv
->vval
.v_float
= ceil(f
);
8705 rettv
->vval
.v_float
= 0.0;
8710 * "changenr()" function
8714 f_changenr(argvars
, rettv
)
8718 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8722 * "char2nr(string)" function
8725 f_char2nr(argvars
, rettv
)
8731 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8734 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8738 * "cindent(lnum)" function
8741 f_cindent(argvars
, rettv
)
8749 pos
= curwin
->w_cursor
;
8750 lnum
= get_tv_lnum(argvars
);
8751 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8753 curwin
->w_cursor
.lnum
= lnum
;
8754 rettv
->vval
.v_number
= get_c_indent();
8755 curwin
->w_cursor
= pos
;
8759 rettv
->vval
.v_number
= -1;
8763 * "clearmatches()" function
8767 f_clearmatches(argvars
, rettv
)
8771 #ifdef FEAT_SEARCH_EXTRA
8772 clear_matches(curwin
);
8777 * "col(string)" function
8780 f_col(argvars
, rettv
)
8786 int fnum
= curbuf
->b_fnum
;
8788 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8789 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8791 if (fp
->col
== MAXCOL
)
8793 /* '> can be MAXCOL, get the length of the line then */
8794 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8795 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8802 #ifdef FEAT_VIRTUALEDIT
8803 /* col(".") when the cursor is on the NUL at the end of the line
8804 * because of "coladd" can be seen as an extra column. */
8805 if (virtual_active() && fp
== &curwin
->w_cursor
)
8807 char_u
*p
= ml_get_cursor();
8809 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8810 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8815 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8818 if (*p
!= NUL
&& p
[1] == NUL
)
8826 rettv
->vval
.v_number
= col
;
8829 #if defined(FEAT_INS_EXPAND)
8831 * "complete()" function
8835 f_complete(argvars
, rettv
)
8841 if ((State
& INSERT
) == 0)
8843 EMSG(_("E785: complete() can only be used in Insert mode"));
8847 /* Check for undo allowed here, because if something was already inserted
8848 * the line was already saved for undo and this check isn't done. */
8849 if (!undo_allowed())
8852 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8858 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
8862 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
8866 * "complete_add()" function
8870 f_complete_add(argvars
, rettv
)
8874 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
8878 * "complete_check()" function
8882 f_complete_check(argvars
, rettv
)
8886 int saved
= RedrawingDisabled
;
8888 RedrawingDisabled
= 0;
8889 ins_compl_check_keys(0);
8890 rettv
->vval
.v_number
= compl_interrupted
;
8891 RedrawingDisabled
= saved
;
8896 * "confirm(message, buttons[, default [, type]])" function
8900 f_confirm(argvars
, rettv
)
8904 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8906 char_u
*buttons
= NULL
;
8907 char_u buf
[NUMBUFLEN
];
8908 char_u buf2
[NUMBUFLEN
];
8910 int type
= VIM_GENERIC
;
8914 message
= get_tv_string_chk(&argvars
[0]);
8915 if (message
== NULL
)
8917 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8919 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
8920 if (buttons
== NULL
)
8922 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8924 def
= get_tv_number_chk(&argvars
[2], &error
);
8925 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8927 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8928 if (typestr
== NULL
)
8932 switch (TOUPPER_ASC(*typestr
))
8934 case 'E': type
= VIM_ERROR
; break;
8935 case 'Q': type
= VIM_QUESTION
; break;
8936 case 'I': type
= VIM_INFO
; break;
8937 case 'W': type
= VIM_WARNING
; break;
8938 case 'G': type
= VIM_GENERIC
; break;
8945 if (buttons
== NULL
|| *buttons
== NUL
)
8946 buttons
= (char_u
*)_("&Ok");
8949 rettv
->vval
.v_number
= 0;
8951 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
8954 rettv
->vval
.v_number
= 0;
8962 f_copy(argvars
, rettv
)
8966 item_copy(&argvars
[0], rettv
, FALSE
, 0);
8974 f_cos(argvars
, rettv
)
8980 rettv
->v_type
= VAR_FLOAT
;
8981 if (get_float_arg(argvars
, &f
) == OK
)
8982 rettv
->vval
.v_float
= cos(f
);
8984 rettv
->vval
.v_float
= 0.0;
8989 * "count()" function
8992 f_count(argvars
, rettv
)
8999 if (argvars
[0].v_type
== VAR_LIST
)
9005 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9008 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9012 ic
= get_tv_number_chk(&argvars
[2], &error
);
9013 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9015 idx
= get_tv_number_chk(&argvars
[3], &error
);
9018 li
= list_find(l
, idx
);
9020 EMSGN(_(e_listidx
), idx
);
9027 for ( ; li
!= NULL
; li
= li
->li_next
)
9028 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
9032 else if (argvars
[0].v_type
== VAR_DICT
)
9038 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9042 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9044 ic
= get_tv_number_chk(&argvars
[2], &error
);
9045 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9049 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
9050 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
9052 if (!HASHITEM_EMPTY(hi
))
9055 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
9062 EMSG2(_(e_listdictarg
), "count()");
9063 rettv
->vval
.v_number
= n
;
9067 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9069 * Checks the existence of a cscope connection.
9073 f_cscope_connection(argvars
, rettv
)
9079 char_u
*dbpath
= NULL
;
9080 char_u
*prepend
= NULL
;
9081 char_u buf
[NUMBUFLEN
];
9083 if (argvars
[0].v_type
!= VAR_UNKNOWN
9084 && argvars
[1].v_type
!= VAR_UNKNOWN
)
9086 num
= (int)get_tv_number(&argvars
[0]);
9087 dbpath
= get_tv_string(&argvars
[1]);
9088 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9089 prepend
= get_tv_string_buf(&argvars
[2], buf
);
9092 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
9094 rettv
->vval
.v_number
= 0;
9099 * "cursor(lnum, col)" function
9101 * Moves the cursor to the specified line and column
9105 f_cursor(argvars
, rettv
)
9110 #ifdef FEAT_VIRTUALEDIT
9114 if (argvars
[1].v_type
== VAR_UNKNOWN
)
9118 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
9122 #ifdef FEAT_VIRTUALEDIT
9123 coladd
= pos
.coladd
;
9128 line
= get_tv_lnum(argvars
);
9129 col
= get_tv_number_chk(&argvars
[1], NULL
);
9130 #ifdef FEAT_VIRTUALEDIT
9131 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9132 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
9135 if (line
< 0 || col
< 0
9136 #ifdef FEAT_VIRTUALEDIT
9140 return; /* type error; errmsg already given */
9142 curwin
->w_cursor
.lnum
= line
;
9144 curwin
->w_cursor
.col
= col
- 1;
9145 #ifdef FEAT_VIRTUALEDIT
9146 curwin
->w_cursor
.coladd
= coladd
;
9149 /* Make sure the cursor is in a valid position. */
9152 /* Correct cursor for multi-byte character. */
9157 curwin
->w_set_curswant
= TRUE
;
9161 * "deepcopy()" function
9164 f_deepcopy(argvars
, rettv
)
9170 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9171 noref
= get_tv_number_chk(&argvars
[1], NULL
);
9172 if (noref
< 0 || noref
> 1)
9175 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
9179 * "delete()" function
9182 f_delete(argvars
, rettv
)
9186 if (check_restricted() || check_secure())
9187 rettv
->vval
.v_number
= -1;
9189 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
9193 * "did_filetype()" function
9197 f_did_filetype(argvars
, rettv
)
9202 rettv
->vval
.v_number
= did_filetype
;
9204 rettv
->vval
.v_number
= 0;
9209 * "diff_filler()" function
9213 f_diff_filler(argvars
, rettv
)
9218 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
9223 * "diff_hlID()" function
9227 f_diff_hlID(argvars
, rettv
)
9232 linenr_T lnum
= get_tv_lnum(argvars
);
9233 static linenr_T prev_lnum
= 0;
9234 static int changedtick
= 0;
9235 static int fnum
= 0;
9236 static int change_start
= 0;
9237 static int change_end
= 0;
9238 static hlf_T hlID
= (hlf_T
)0;
9242 if (lnum
< 0) /* ignore type error in {lnum} arg */
9244 if (lnum
!= prev_lnum
9245 || changedtick
!= curbuf
->b_changedtick
9246 || fnum
!= curbuf
->b_fnum
)
9248 /* New line, buffer, change: need to get the values. */
9249 filler_lines
= diff_check(curwin
, lnum
);
9250 if (filler_lines
< 0)
9252 if (filler_lines
== -1)
9254 change_start
= MAXCOL
;
9256 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
9257 hlID
= HLF_ADD
; /* added line */
9259 hlID
= HLF_CHD
; /* changed line */
9262 hlID
= HLF_ADD
; /* added line */
9267 changedtick
= curbuf
->b_changedtick
;
9268 fnum
= curbuf
->b_fnum
;
9271 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
9273 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
9274 if (col
>= change_start
&& col
<= change_end
)
9275 hlID
= HLF_TXD
; /* changed text */
9277 hlID
= HLF_CHD
; /* changed line */
9279 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
9284 * "empty({expr})" function
9287 f_empty(argvars
, rettv
)
9293 switch (argvars
[0].v_type
)
9297 n
= argvars
[0].vval
.v_string
== NULL
9298 || *argvars
[0].vval
.v_string
== NUL
;
9301 n
= argvars
[0].vval
.v_number
== 0;
9305 n
= argvars
[0].vval
.v_float
== 0.0;
9309 n
= argvars
[0].vval
.v_list
== NULL
9310 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
9313 n
= argvars
[0].vval
.v_dict
== NULL
9314 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
9317 EMSG2(_(e_intern2
), "f_empty()");
9321 rettv
->vval
.v_number
= n
;
9325 * "escape({string}, {chars})" function
9328 f_escape(argvars
, rettv
)
9332 char_u buf
[NUMBUFLEN
];
9334 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
9335 get_tv_string_buf(&argvars
[1], buf
));
9336 rettv
->v_type
= VAR_STRING
;
9344 f_eval(argvars
, rettv
)
9350 s
= get_tv_string_chk(&argvars
[0]);
9354 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
9356 rettv
->v_type
= VAR_NUMBER
;
9357 rettv
->vval
.v_number
= 0;
9360 EMSG(_(e_trailing
));
9364 * "eventhandler()" function
9368 f_eventhandler(argvars
, rettv
)
9372 rettv
->vval
.v_number
= vgetc_busy
;
9376 * "executable()" function
9379 f_executable(argvars
, rettv
)
9383 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
9387 * "exists()" function
9390 f_exists(argvars
, rettv
)
9399 p
= get_tv_string(&argvars
[0]);
9400 if (*p
== '$') /* environment variable */
9402 /* first try "normal" environment variables (fast) */
9403 if (mch_getenv(p
+ 1) != NULL
)
9407 /* try expanding things like $VIM and ${HOME} */
9408 p
= expand_env_save(p
);
9409 if (p
!= NULL
&& *p
!= '$')
9414 else if (*p
== '&' || *p
== '+') /* option */
9416 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
9417 if (*skipwhite(p
) != NUL
)
9418 n
= FALSE
; /* trailing garbage */
9420 else if (*p
== '*') /* internal or user defined function */
9422 n
= function_exists(p
+ 1);
9426 n
= cmd_exists(p
+ 1);
9432 n
= autocmd_supported(p
+ 2);
9434 n
= au_exists(p
+ 1);
9437 else /* internal variable */
9442 /* get_name_len() takes care of expanding curly braces */
9444 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
9449 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
9452 /* handle d.key, l[idx], f(expr) */
9453 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
9464 rettv
->vval
.v_number
= n
;
9468 * "expand()" function
9471 f_expand(argvars
, rettv
)
9478 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
9482 rettv
->v_type
= VAR_STRING
;
9483 s
= get_tv_string(&argvars
[0]);
9484 if (*s
== '%' || *s
== '#' || *s
== '<')
9487 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
9492 /* When the optional second argument is non-zero, don't remove matches
9493 * for 'suffixes' and 'wildignore' */
9494 if (argvars
[1].v_type
!= VAR_UNKNOWN
9495 && get_tv_number_chk(&argvars
[1], &error
))
9496 flags
|= WILD_KEEP_ALL
;
9500 xpc
.xp_context
= EXPAND_FILES
;
9501 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
9504 rettv
->vval
.v_string
= NULL
;
9509 * "extend(list, list [, idx])" function
9510 * "extend(dict, dict [, action])" function
9513 f_extend(argvars
, rettv
)
9517 rettv
->vval
.v_number
= 0;
9518 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9525 l1
= argvars
[0].vval
.v_list
;
9526 l2
= argvars
[1].vval
.v_list
;
9527 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9530 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9532 before
= get_tv_number_chk(&argvars
[2], &error
);
9534 return; /* type error; errmsg already given */
9536 if (before
== l1
->lv_len
)
9540 item
= list_find(l1
, before
);
9543 EMSGN(_(e_listidx
), before
);
9550 list_extend(l1
, l2
, item
);
9552 copy_tv(&argvars
[0], rettv
);
9555 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9564 d1
= argvars
[0].vval
.v_dict
;
9565 d2
= argvars
[1].vval
.v_dict
;
9566 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9569 /* Check the third argument. */
9570 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9572 static char *(av
[]) = {"keep", "force", "error"};
9574 action
= get_tv_string_chk(&argvars
[2]);
9576 return; /* type error; errmsg already given */
9577 for (i
= 0; i
< 3; ++i
)
9578 if (STRCMP(action
, av
[i
]) == 0)
9582 EMSG2(_(e_invarg2
), action
);
9587 action
= (char_u
*)"force";
9589 /* Go over all entries in the second dict and add them to the
9591 todo
= (int)d2
->dv_hashtab
.ht_used
;
9592 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9594 if (!HASHITEM_EMPTY(hi2
))
9597 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9600 di1
= dictitem_copy(HI2DI(hi2
));
9601 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9604 else if (*action
== 'e')
9606 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9609 else if (*action
== 'f')
9611 clear_tv(&di1
->di_tv
);
9612 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9617 copy_tv(&argvars
[0], rettv
);
9621 EMSG2(_(e_listdictarg
), "extend()");
9625 * "feedkeys()" function
9629 f_feedkeys(argvars
, rettv
)
9634 char_u
*keys
, *flags
;
9635 char_u nbuf
[NUMBUFLEN
];
9639 /* This is not allowed in the sandbox. If the commands would still be
9640 * executed in the sandbox it would be OK, but it probably happens later,
9641 * when "sandbox" is no longer set. */
9645 rettv
->vval
.v_number
= 0;
9646 keys
= get_tv_string(&argvars
[0]);
9649 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9651 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9652 for ( ; *flags
!= NUL
; ++flags
)
9656 case 'n': remap
= FALSE
; break;
9657 case 'm': remap
= TRUE
; break;
9658 case 't': typed
= TRUE
; break;
9663 /* Need to escape K_SPECIAL and CSI before putting the string in the
9664 * typeahead buffer. */
9665 keys_esc
= vim_strsave_escape_csi(keys
);
9666 if (keys_esc
!= NULL
)
9668 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9669 typebuf
.tb_len
, !typed
, FALSE
);
9672 typebuf_was_filled
= TRUE
;
9678 * "filereadable()" function
9681 f_filereadable(argvars
, rettv
)
9689 p
= get_tv_string(&argvars
[0]);
9690 if (*p
&& !mch_isdir(p
) && (fd
= mch_fopen((char *)p
, "r")) != NULL
)
9698 rettv
->vval
.v_number
= n
;
9702 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9703 * rights to write into.
9706 f_filewritable(argvars
, rettv
)
9710 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9713 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int find_what
));
9716 findfilendir(argvars
, rettv
, find_what
)
9721 #ifdef FEAT_SEARCHPATH
9723 char_u
*fresult
= NULL
;
9724 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9726 char_u pathbuf
[NUMBUFLEN
];
9732 rettv
->vval
.v_string
= NULL
;
9733 rettv
->v_type
= VAR_STRING
;
9735 #ifdef FEAT_SEARCHPATH
9736 fname
= get_tv_string(&argvars
[0]);
9738 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9740 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9748 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9749 count
= get_tv_number_chk(&argvars
[2], &error
);
9753 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9756 if (*fname
!= NUL
&& !error
)
9760 if (rettv
->v_type
== VAR_STRING
)
9762 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9763 first
? (int)STRLEN(fname
) : 0,
9767 find_what
== FINDFILE_DIR
9768 ? (char_u
*)"" : curbuf
->b_p_sua
);
9771 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9772 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9774 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9777 if (rettv
->v_type
== VAR_STRING
)
9778 rettv
->vval
.v_string
= fresult
;
9782 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9783 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9786 * Implementation of map() and filter().
9789 filter_map(argvars
, rettv
, map
)
9794 char_u buf
[NUMBUFLEN
];
9796 listitem_T
*li
, *nli
;
9806 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9809 rettv
->vval
.v_number
= 0;
9810 if (argvars
[0].v_type
== VAR_LIST
)
9812 if ((l
= argvars
[0].vval
.v_list
) == NULL
9813 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9816 else if (argvars
[0].v_type
== VAR_DICT
)
9818 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9819 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9824 EMSG2(_(e_listdictarg
), ermsg
);
9828 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9829 /* On type errors, the preceding call has already displayed an error
9830 * message. Avoid a misleading error message for an empty string that
9831 * was not passed as argument. */
9834 prepare_vimvar(VV_VAL
, &save_val
);
9835 expr
= skipwhite(expr
);
9837 /* We reset "did_emsg" to be able to detect whether an error
9838 * occurred during evaluation of the expression. */
9839 save_did_emsg
= did_emsg
;
9842 if (argvars
[0].v_type
== VAR_DICT
)
9844 prepare_vimvar(VV_KEY
, &save_key
);
9845 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9847 ht
= &d
->dv_hashtab
;
9849 todo
= (int)ht
->ht_used
;
9850 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9852 if (!HASHITEM_EMPTY(hi
))
9856 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9858 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9859 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9863 dictitem_remove(d
, di
);
9864 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9869 restore_vimvar(VV_KEY
, &save_key
);
9873 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
9875 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
9878 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
9882 listitem_remove(l
, li
);
9886 restore_vimvar(VV_VAL
, &save_val
);
9888 did_emsg
|= save_did_emsg
;
9891 copy_tv(&argvars
[0], rettv
);
9895 filter_map_one(tv
, expr
, map
, remp
)
9905 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
9907 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
9909 if (*s
!= NUL
) /* check for trailing chars after expr */
9911 EMSG2(_(e_invexpr2
), s
);
9916 /* map(): replace the list item value */
9925 /* filter(): when expr is zero remove the item */
9926 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
9928 /* On type error, nothing has been removed; return FAIL to stop the
9929 * loop. The error message was given by get_tv_number_chk(). */
9935 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
9940 * "filter()" function
9943 f_filter(argvars
, rettv
)
9947 filter_map(argvars
, rettv
, FALSE
);
9951 * "finddir({fname}[, {path}[, {count}]])" function
9954 f_finddir(argvars
, rettv
)
9958 findfilendir(argvars
, rettv
, FINDFILE_DIR
);
9962 * "findfile({fname}[, {path}[, {count}]])" function
9965 f_findfile(argvars
, rettv
)
9969 findfilendir(argvars
, rettv
, FINDFILE_FILE
);
9974 * "float2nr({float})" function
9977 f_float2nr(argvars
, rettv
)
9983 if (get_float_arg(argvars
, &f
) == OK
)
9985 if (f
< -0x7fffffff)
9986 rettv
->vval
.v_number
= -0x7fffffff;
9987 else if (f
> 0x7fffffff)
9988 rettv
->vval
.v_number
= 0x7fffffff;
9990 rettv
->vval
.v_number
= (varnumber_T
)f
;
9993 rettv
->vval
.v_number
= 0;
9997 * "floor({float})" function
10000 f_floor(argvars
, rettv
)
10006 rettv
->v_type
= VAR_FLOAT
;
10007 if (get_float_arg(argvars
, &f
) == OK
)
10008 rettv
->vval
.v_float
= floor(f
);
10010 rettv
->vval
.v_float
= 0.0;
10015 * "fnameescape({string})" function
10018 f_fnameescape(argvars
, rettv
)
10022 rettv
->vval
.v_string
= vim_strsave_fnameescape(
10023 get_tv_string(&argvars
[0]), FALSE
);
10024 rettv
->v_type
= VAR_STRING
;
10028 * "fnamemodify({fname}, {mods})" function
10031 f_fnamemodify(argvars
, rettv
)
10039 char_u
*fbuf
= NULL
;
10040 char_u buf
[NUMBUFLEN
];
10042 fname
= get_tv_string_chk(&argvars
[0]);
10043 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
10044 if (fname
== NULL
|| mods
== NULL
)
10048 len
= (int)STRLEN(fname
);
10049 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
10052 rettv
->v_type
= VAR_STRING
;
10054 rettv
->vval
.v_string
= NULL
;
10056 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
10060 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
10063 * "foldclosed()" function
10066 foldclosed_both(argvars
, rettv
, end
)
10071 #ifdef FEAT_FOLDING
10073 linenr_T first
, last
;
10075 lnum
= get_tv_lnum(argvars
);
10076 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10078 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
10081 rettv
->vval
.v_number
= (varnumber_T
)last
;
10083 rettv
->vval
.v_number
= (varnumber_T
)first
;
10088 rettv
->vval
.v_number
= -1;
10092 * "foldclosed()" function
10095 f_foldclosed(argvars
, rettv
)
10099 foldclosed_both(argvars
, rettv
, FALSE
);
10103 * "foldclosedend()" function
10106 f_foldclosedend(argvars
, rettv
)
10110 foldclosed_both(argvars
, rettv
, TRUE
);
10114 * "foldlevel()" function
10117 f_foldlevel(argvars
, rettv
)
10121 #ifdef FEAT_FOLDING
10124 lnum
= get_tv_lnum(argvars
);
10125 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10126 rettv
->vval
.v_number
= foldLevel(lnum
);
10129 rettv
->vval
.v_number
= 0;
10133 * "foldtext()" function
10137 f_foldtext(argvars
, rettv
)
10141 #ifdef FEAT_FOLDING
10149 rettv
->v_type
= VAR_STRING
;
10150 rettv
->vval
.v_string
= NULL
;
10151 #ifdef FEAT_FOLDING
10152 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
10153 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10154 <= curbuf
->b_ml
.ml_line_count
10155 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
10157 /* Find first non-empty line in the fold. */
10158 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
10159 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10161 if (!linewhite(lnum
))
10166 /* Find interesting text in this line. */
10167 s
= skipwhite(ml_get(lnum
));
10168 /* skip C comment-start */
10169 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
10171 s
= skipwhite(s
+ 2);
10172 if (*skipwhite(s
) == NUL
10173 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10175 s
= skipwhite(ml_get(lnum
+ 1));
10177 s
= skipwhite(s
+ 1);
10180 txt
= _("+-%s%3ld lines: ");
10181 r
= alloc((unsigned)(STRLEN(txt
)
10182 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
10183 + 20 /* for %3ld */
10184 + STRLEN(s
))); /* concatenated */
10187 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
10188 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10189 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
10190 len
= (int)STRLEN(r
);
10192 /* remove 'foldmarker' and 'commentstring' */
10193 foldtext_cleanup(r
+ len
);
10194 rettv
->vval
.v_string
= r
;
10201 * "foldtextresult(lnum)" function
10205 f_foldtextresult(argvars
, rettv
)
10209 #ifdef FEAT_FOLDING
10213 foldinfo_T foldinfo
;
10217 rettv
->v_type
= VAR_STRING
;
10218 rettv
->vval
.v_string
= NULL
;
10219 #ifdef FEAT_FOLDING
10220 lnum
= get_tv_lnum(argvars
);
10221 /* treat illegal types and illegal string values for {lnum} the same */
10224 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
10225 if (fold_count
> 0)
10227 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
10230 text
= vim_strsave(text
);
10231 rettv
->vval
.v_string
= text
;
10237 * "foreground()" function
10241 f_foreground(argvars
, rettv
)
10245 rettv
->vval
.v_number
= 0;
10248 gui_mch_set_foreground();
10251 win32_set_foreground();
10257 * "function()" function
10261 f_function(argvars
, rettv
)
10267 rettv
->vval
.v_number
= 0;
10268 s
= get_tv_string(&argvars
[0]);
10269 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
10270 EMSG2(_(e_invarg2
), s
);
10271 else if (!function_exists(s
))
10272 EMSG2(_("E700: Unknown function: %s"), s
);
10275 rettv
->vval
.v_string
= vim_strsave(s
);
10276 rettv
->v_type
= VAR_FUNC
;
10281 * "garbagecollect()" function
10285 f_garbagecollect(argvars
, rettv
)
10289 /* This is postponed until we are back at the toplevel, because we may be
10290 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10291 want_garbage_collect
= TRUE
;
10293 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
10294 garbage_collect_at_exit
= TRUE
;
10301 f_get(argvars
, rettv
)
10309 typval_T
*tv
= NULL
;
10311 if (argvars
[0].v_type
== VAR_LIST
)
10313 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
10317 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
10318 if (!error
&& li
!= NULL
)
10322 else if (argvars
[0].v_type
== VAR_DICT
)
10324 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
10326 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
10332 EMSG2(_(e_listdictarg
), "get()");
10336 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10337 rettv
->vval
.v_number
= 0;
10339 copy_tv(&argvars
[2], rettv
);
10342 copy_tv(tv
, rettv
);
10345 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
10348 * Get line or list of lines from buffer "buf" into "rettv".
10349 * Return a range (from start to end) of lines in rettv from the specified
10351 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
10354 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
10365 if (rettv_list_alloc(rettv
) == FAIL
)
10369 rettv
->vval
.v_number
= 0;
10371 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
10376 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
10377 p
= ml_get_buf(buf
, start
, FALSE
);
10381 rettv
->v_type
= VAR_STRING
;
10382 rettv
->vval
.v_string
= vim_strsave(p
);
10391 if (end
> buf
->b_ml
.ml_line_count
)
10392 end
= buf
->b_ml
.ml_line_count
;
10393 while (start
<= end
)
10394 if (list_append_string(rettv
->vval
.v_list
,
10395 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
10401 * "getbufline()" function
10404 f_getbufline(argvars
, rettv
)
10412 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10414 buf
= get_buf_tv(&argvars
[0]);
10417 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
10418 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10421 end
= get_tv_lnum_buf(&argvars
[2], buf
);
10423 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
10427 * "getbufvar()" function
10430 f_getbufvar(argvars
, rettv
)
10435 buf_T
*save_curbuf
;
10439 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10440 varname
= get_tv_string_chk(&argvars
[1]);
10442 buf
= get_buf_tv(&argvars
[0]);
10444 rettv
->v_type
= VAR_STRING
;
10445 rettv
->vval
.v_string
= NULL
;
10447 if (buf
!= NULL
&& varname
!= NULL
)
10449 /* set curbuf to be our buf, temporarily */
10450 save_curbuf
= curbuf
;
10453 if (*varname
== '&') /* buffer-local-option */
10454 get_option_tv(&varname
, rettv
, TRUE
);
10457 if (*varname
== NUL
)
10458 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10459 * scope prefix before the NUL byte is required by
10460 * find_var_in_ht(). */
10461 varname
= (char_u
*)"b:" + 2;
10462 /* look up the variable */
10463 v
= find_var_in_ht(&curbuf
->b_vars
.dv_hashtab
, varname
, FALSE
);
10465 copy_tv(&v
->di_tv
, rettv
);
10468 /* restore previous notion of curbuf */
10469 curbuf
= save_curbuf
;
10476 * "getchar()" function
10479 f_getchar(argvars
, rettv
)
10486 /* Position the cursor. Needed after a message that ends in a space. */
10487 windgoto(msg_row
, msg_col
);
10493 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10494 /* getchar(): blocking wait. */
10496 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
10497 /* getchar(1): only check if char avail */
10499 else if (error
|| vpeekc() == NUL
)
10500 /* illegal argument or getchar(0) and no char avail: return zero */
10503 /* getchar(0) and char avail: return char */
10512 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
10513 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
10514 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
10516 rettv
->vval
.v_number
= n
;
10517 if (IS_SPECIAL(n
) || mod_mask
!= 0)
10519 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10522 /* Turn a special key into three bytes, plus modifier. */
10525 temp
[i
++] = K_SPECIAL
;
10526 temp
[i
++] = KS_MODIFIER
;
10527 temp
[i
++] = mod_mask
;
10531 temp
[i
++] = K_SPECIAL
;
10532 temp
[i
++] = K_SECOND(n
);
10533 temp
[i
++] = K_THIRD(n
);
10536 else if (has_mbyte
)
10537 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
10542 rettv
->v_type
= VAR_STRING
;
10543 rettv
->vval
.v_string
= vim_strsave(temp
);
10546 if (n
== K_LEFTMOUSE
10547 || n
== K_LEFTMOUSE_NM
10549 || n
== K_LEFTRELEASE
10550 || n
== K_LEFTRELEASE_NM
10551 || n
== K_MIDDLEMOUSE
10552 || n
== K_MIDDLEDRAG
10553 || n
== K_MIDDLERELEASE
10554 || n
== K_RIGHTMOUSE
10555 || n
== K_RIGHTDRAG
10556 || n
== K_RIGHTRELEASE
10559 || n
== K_X1RELEASE
10562 || n
== K_X2RELEASE
10563 || n
== K_MOUSEDOWN
10566 int row
= mouse_row
;
10567 int col
= mouse_col
;
10570 # ifdef FEAT_WINDOWS
10575 if (row
>= 0 && col
>= 0)
10577 /* Find the window at the mouse coordinates and compute the
10578 * text position. */
10579 win
= mouse_find_win(&row
, &col
);
10580 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10581 # ifdef FEAT_WINDOWS
10582 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10585 vimvars
[VV_MOUSE_WIN
].vv_nr
= n
;
10586 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10587 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10595 * "getcharmod()" function
10599 f_getcharmod(argvars
, rettv
)
10603 rettv
->vval
.v_number
= mod_mask
;
10607 * "getcmdline()" function
10611 f_getcmdline(argvars
, rettv
)
10615 rettv
->v_type
= VAR_STRING
;
10616 rettv
->vval
.v_string
= get_cmdline_str();
10620 * "getcmdpos()" function
10624 f_getcmdpos(argvars
, rettv
)
10628 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10632 * "getcmdtype()" function
10636 f_getcmdtype(argvars
, rettv
)
10640 rettv
->v_type
= VAR_STRING
;
10641 rettv
->vval
.v_string
= alloc(2);
10642 if (rettv
->vval
.v_string
!= NULL
)
10644 rettv
->vval
.v_string
[0] = get_cmdline_type();
10645 rettv
->vval
.v_string
[1] = NUL
;
10650 * "getcwd()" function
10654 f_getcwd(argvars
, rettv
)
10658 char_u cwd
[MAXPATHL
];
10660 rettv
->v_type
= VAR_STRING
;
10661 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10662 rettv
->vval
.v_string
= NULL
;
10665 rettv
->vval
.v_string
= vim_strsave(cwd
);
10666 #ifdef BACKSLASH_IN_FILENAME
10667 if (rettv
->vval
.v_string
!= NULL
)
10668 slash_adjust(rettv
->vval
.v_string
);
10674 * "getfontname()" function
10678 f_getfontname(argvars
, rettv
)
10682 rettv
->v_type
= VAR_STRING
;
10683 rettv
->vval
.v_string
= NULL
;
10688 char_u
*name
= NULL
;
10690 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10692 /* Get the "Normal" font. Either the name saved by
10693 * hl_set_font_name() or from the font ID. */
10694 font
= gui
.norm_font
;
10695 name
= hl_get_font_name();
10699 name
= get_tv_string(&argvars
[0]);
10700 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10702 font
= gui_mch_get_font(name
, FALSE
);
10703 if (font
== NOFONT
)
10704 return; /* Invalid font name, return empty string. */
10706 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10707 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10708 gui_mch_free_font(font
);
10714 * "getfperm({fname})" function
10717 f_getfperm(argvars
, rettv
)
10723 char_u
*perm
= NULL
;
10724 char_u flags
[] = "rwx";
10727 fname
= get_tv_string(&argvars
[0]);
10729 rettv
->v_type
= VAR_STRING
;
10730 if (mch_stat((char *)fname
, &st
) >= 0)
10732 perm
= vim_strsave((char_u
*)"---------");
10735 for (i
= 0; i
< 9; i
++)
10737 if (st
.st_mode
& (1 << (8 - i
)))
10738 perm
[i
] = flags
[i
% 3];
10742 rettv
->vval
.v_string
= perm
;
10746 * "getfsize({fname})" function
10749 f_getfsize(argvars
, rettv
)
10756 fname
= get_tv_string(&argvars
[0]);
10758 rettv
->v_type
= VAR_NUMBER
;
10760 if (mch_stat((char *)fname
, &st
) >= 0)
10762 if (mch_isdir(fname
))
10763 rettv
->vval
.v_number
= 0;
10766 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10768 /* non-perfect check for overflow */
10769 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
10770 rettv
->vval
.v_number
= -2;
10774 rettv
->vval
.v_number
= -1;
10778 * "getftime({fname})" function
10781 f_getftime(argvars
, rettv
)
10788 fname
= get_tv_string(&argvars
[0]);
10790 if (mch_stat((char *)fname
, &st
) >= 0)
10791 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10793 rettv
->vval
.v_number
= -1;
10797 * "getftype({fname})" function
10800 f_getftype(argvars
, rettv
)
10806 char_u
*type
= NULL
;
10809 fname
= get_tv_string(&argvars
[0]);
10811 rettv
->v_type
= VAR_STRING
;
10812 if (mch_lstat((char *)fname
, &st
) >= 0)
10815 if (S_ISREG(st
.st_mode
))
10817 else if (S_ISDIR(st
.st_mode
))
10820 else if (S_ISLNK(st
.st_mode
))
10824 else if (S_ISBLK(st
.st_mode
))
10828 else if (S_ISCHR(st
.st_mode
))
10832 else if (S_ISFIFO(st
.st_mode
))
10836 else if (S_ISSOCK(st
.st_mode
))
10843 switch (st
.st_mode
& S_IFMT
)
10845 case S_IFREG
: t
= "file"; break;
10846 case S_IFDIR
: t
= "dir"; break;
10848 case S_IFLNK
: t
= "link"; break;
10851 case S_IFBLK
: t
= "bdev"; break;
10854 case S_IFCHR
: t
= "cdev"; break;
10857 case S_IFIFO
: t
= "fifo"; break;
10860 case S_IFSOCK
: t
= "socket"; break;
10862 default: t
= "other";
10865 if (mch_isdir(fname
))
10871 type
= vim_strsave((char_u
*)t
);
10873 rettv
->vval
.v_string
= type
;
10877 * "getline(lnum, [end])" function
10880 f_getline(argvars
, rettv
)
10888 lnum
= get_tv_lnum(argvars
);
10889 if (argvars
[1].v_type
== VAR_UNKNOWN
)
10896 end
= get_tv_lnum(&argvars
[1]);
10900 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
10904 * "getmatches()" function
10908 f_getmatches(argvars
, rettv
)
10912 #ifdef FEAT_SEARCH_EXTRA
10914 matchitem_T
*cur
= curwin
->w_match_head
;
10916 rettv
->vval
.v_number
= 0;
10918 if (rettv_list_alloc(rettv
) == OK
)
10920 while (cur
!= NULL
)
10922 dict
= dict_alloc();
10925 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
10926 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
10927 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
10928 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
10929 list_append_dict(rettv
->vval
.v_list
, dict
);
10937 * "getpid()" function
10941 f_getpid(argvars
, rettv
)
10945 rettv
->vval
.v_number
= mch_get_pid();
10949 * "getpos(string)" function
10952 f_getpos(argvars
, rettv
)
10960 if (rettv_list_alloc(rettv
) == OK
)
10962 l
= rettv
->vval
.v_list
;
10963 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
10965 list_append_number(l
, (varnumber_T
)fnum
);
10967 list_append_number(l
, (varnumber_T
)0);
10968 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
10970 list_append_number(l
, (fp
!= NULL
)
10971 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
10973 list_append_number(l
,
10974 #ifdef FEAT_VIRTUALEDIT
10975 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
10980 rettv
->vval
.v_number
= FALSE
;
10984 * "getqflist()" and "getloclist()" functions
10988 f_getqflist(argvars
, rettv
)
10992 #ifdef FEAT_QUICKFIX
10996 rettv
->vval
.v_number
= 0;
10997 #ifdef FEAT_QUICKFIX
10998 if (rettv_list_alloc(rettv
) == OK
)
11001 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
11003 wp
= find_win_by_nr(&argvars
[0], NULL
);
11008 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
11014 * "getreg()" function
11017 f_getreg(argvars
, rettv
)
11021 char_u
*strregname
;
11026 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11028 strregname
= get_tv_string_chk(&argvars
[0]);
11029 error
= strregname
== NULL
;
11030 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11031 arg2
= get_tv_number_chk(&argvars
[1], &error
);
11034 strregname
= vimvars
[VV_REG
].vv_str
;
11035 regname
= (strregname
== NULL
? '"' : *strregname
);
11039 rettv
->v_type
= VAR_STRING
;
11040 rettv
->vval
.v_string
= error
? NULL
:
11041 get_reg_contents(regname
, TRUE
, arg2
);
11045 * "getregtype()" function
11048 f_getregtype(argvars
, rettv
)
11052 char_u
*strregname
;
11054 char_u buf
[NUMBUFLEN
+ 2];
11057 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11059 strregname
= get_tv_string_chk(&argvars
[0]);
11060 if (strregname
== NULL
) /* type error; errmsg already given */
11062 rettv
->v_type
= VAR_STRING
;
11063 rettv
->vval
.v_string
= NULL
;
11068 /* Default to v:register */
11069 strregname
= vimvars
[VV_REG
].vv_str
;
11071 regname
= (strregname
== NULL
? '"' : *strregname
);
11077 switch (get_reg_type(regname
, ®len
))
11079 case MLINE
: buf
[0] = 'V'; break;
11080 case MCHAR
: buf
[0] = 'v'; break;
11084 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
11088 rettv
->v_type
= VAR_STRING
;
11089 rettv
->vval
.v_string
= vim_strsave(buf
);
11093 * "gettabwinvar()" function
11096 f_gettabwinvar(argvars
, rettv
)
11100 getwinvar(argvars
, rettv
, 1);
11104 * "getwinposx()" function
11108 f_getwinposx(argvars
, rettv
)
11112 rettv
->vval
.v_number
= -1;
11118 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11119 rettv
->vval
.v_number
= x
;
11125 * "getwinposy()" function
11129 f_getwinposy(argvars
, rettv
)
11133 rettv
->vval
.v_number
= -1;
11139 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11140 rettv
->vval
.v_number
= y
;
11146 * Find window specified by "vp" in tabpage "tp".
11149 find_win_by_nr(vp
, tp
)
11151 tabpage_T
*tp
; /* NULL for current tab page */
11153 #ifdef FEAT_WINDOWS
11158 nr
= get_tv_number_chk(vp
, NULL
);
11160 #ifdef FEAT_WINDOWS
11166 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
11167 wp
!= NULL
; wp
= wp
->w_next
)
11172 if (nr
== 0 || nr
== 1)
11179 * "getwinvar()" function
11182 f_getwinvar(argvars
, rettv
)
11186 getwinvar(argvars
, rettv
, 0);
11190 * getwinvar() and gettabwinvar()
11193 getwinvar(argvars
, rettv
, off
)
11196 int off
; /* 1 for gettabwinvar() */
11198 win_T
*win
, *oldcurwin
;
11203 #ifdef FEAT_WINDOWS
11205 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
11209 win
= find_win_by_nr(&argvars
[off
], tp
);
11210 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
11213 rettv
->v_type
= VAR_STRING
;
11214 rettv
->vval
.v_string
= NULL
;
11216 if (win
!= NULL
&& varname
!= NULL
)
11218 /* Set curwin to be our win, temporarily. Also set curbuf, so
11219 * that we can get buffer-local options. */
11220 oldcurwin
= curwin
;
11222 curbuf
= win
->w_buffer
;
11224 if (*varname
== '&') /* window-local-option */
11225 get_option_tv(&varname
, rettv
, 1);
11228 if (*varname
== NUL
)
11229 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11230 * scope prefix before the NUL byte is required by
11231 * find_var_in_ht(). */
11232 varname
= (char_u
*)"w:" + 2;
11233 /* look up the variable */
11234 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
11236 copy_tv(&v
->di_tv
, rettv
);
11239 /* restore previous notion of curwin */
11240 curwin
= oldcurwin
;
11241 curbuf
= curwin
->w_buffer
;
11248 * "glob()" function
11251 f_glob(argvars
, rettv
)
11258 xpc
.xp_context
= EXPAND_FILES
;
11259 rettv
->v_type
= VAR_STRING
;
11260 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
11261 NULL
, WILD_USE_NL
|WILD_SILENT
, WILD_ALL
);
11265 * "globpath()" function
11268 f_globpath(argvars
, rettv
)
11272 char_u buf1
[NUMBUFLEN
];
11273 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
11275 rettv
->v_type
= VAR_STRING
;
11277 rettv
->vval
.v_string
= NULL
;
11279 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
);
11286 f_has(argvars
, rettv
)
11293 static char *(has_list
[]) =
11314 #if defined(MACOS_X_UNIX)
11338 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11347 #ifndef CASE_INSENSITIVE_FILENAME
11353 #ifdef FEAT_AUTOCMD
11358 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11359 "balloon_multiline",
11362 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11364 # ifdef ALL_BUILTIN_TCAPS
11365 "all_builtin_terms",
11368 #ifdef FEAT_BYTEOFF
11371 #ifdef FEAT_CINDENT
11374 #ifdef FEAT_CLIENTSERVER
11377 #ifdef FEAT_CLIPBOARD
11380 #ifdef FEAT_CMDL_COMPL
11383 #ifdef FEAT_CMDHIST
11386 #ifdef FEAT_COMMENTS
11395 #ifdef CURSOR_SHAPE
11401 #ifdef FEAT_CON_DIALOG
11404 #ifdef FEAT_GUI_DIALOG
11410 #ifdef FEAT_DIGRAPHS
11416 #ifdef FEAT_EMACS_TAGS
11419 "eval", /* always present, of course! */
11420 #ifdef FEAT_EX_EXTRA
11423 #ifdef FEAT_SEARCH_EXTRA
11429 #ifdef FEAT_SEARCHPATH
11432 #if defined(UNIX) && !defined(USE_SYSTEM)
11435 #ifdef FEAT_FIND_ID
11441 #ifdef FEAT_FOLDING
11447 #if !defined(USE_SYSTEM) && defined(UNIX)
11450 #ifdef FEAT_GETTEXT
11456 #ifdef FEAT_GUI_ATHENA
11457 # ifdef FEAT_GUI_NEXTAW
11463 #ifdef FEAT_GUI_GTK
11469 #ifdef FEAT_GUI_GNOME
11472 #ifdef FEAT_GUI_MAC
11475 #ifdef FEAT_GUI_MOTIF
11478 #ifdef FEAT_GUI_PHOTON
11481 #ifdef FEAT_GUI_W16
11484 #ifdef FEAT_GUI_W32
11487 #ifdef FEAT_HANGULIN
11490 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11493 #ifdef FEAT_INS_EXPAND
11496 #ifdef FEAT_JUMPLIST
11502 #ifdef FEAT_LANGMAP
11505 #ifdef FEAT_LIBCALL
11508 #ifdef FEAT_LINEBREAK
11514 #ifdef FEAT_LISTCMDS
11517 #ifdef FEAT_LOCALMAP
11523 #ifdef FEAT_SESSION
11526 #ifdef FEAT_MODIFY_FNAME
11532 #ifdef FEAT_MOUSESHAPE
11535 #if defined(UNIX) || defined(VMS)
11536 # ifdef FEAT_MOUSE_DEC
11539 # ifdef FEAT_MOUSE_GPM
11542 # ifdef FEAT_MOUSE_JSB
11545 # ifdef FEAT_MOUSE_NET
11548 # ifdef FEAT_MOUSE_PTERM
11551 # ifdef FEAT_SYSMOUSE
11554 # ifdef FEAT_MOUSE_XTERM
11561 #ifdef FEAT_MBYTE_IME
11564 #ifdef FEAT_MULTI_LANG
11567 #ifdef FEAT_MZSCHEME
11568 #ifndef DYNAMIC_MZSCHEME
11575 #ifdef FEAT_OSFILETYPE
11578 #ifdef FEAT_PATH_EXTRA
11582 #ifndef DYNAMIC_PERL
11587 #ifndef DYNAMIC_PYTHON
11591 #ifdef FEAT_POSTSCRIPT
11594 #ifdef FEAT_PRINTER
11597 #ifdef FEAT_PROFILE
11600 #ifdef FEAT_RELTIME
11603 #ifdef FEAT_QUICKFIX
11606 #ifdef FEAT_RIGHTLEFT
11609 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11612 #ifdef FEAT_SCROLLBIND
11615 #ifdef FEAT_CMDL_INFO
11622 #ifdef FEAT_SMARTINDENT
11628 #ifdef FEAT_STL_OPT
11631 #ifdef FEAT_SUN_WORKSHOP
11634 #ifdef FEAT_NETBEANS_INTG
11643 #if defined(USE_SYSTEM) || !defined(UNIX)
11646 #ifdef FEAT_TAG_BINS
11649 #ifdef FEAT_TAG_OLDSTATIC
11652 #ifdef FEAT_TAG_ANYWHITE
11656 # ifndef DYNAMIC_TCL
11663 #ifdef FEAT_TERMRESPONSE
11666 #ifdef FEAT_TEXTOBJ
11669 #ifdef HAVE_TGETENT
11675 #ifdef FEAT_TOOLBAR
11678 #ifdef FEAT_USR_CMDS
11679 "user-commands", /* was accidentally included in 5.4 */
11682 #ifdef FEAT_VIMINFO
11685 #ifdef FEAT_VERTSPLIT
11688 #ifdef FEAT_VIRTUALEDIT
11694 #ifdef FEAT_VISUALEXTRA
11697 #ifdef FEAT_VREPLACE
11700 #ifdef FEAT_WILDIGN
11703 #ifdef FEAT_WILDMENU
11706 #ifdef FEAT_WINDOWS
11712 #ifdef FEAT_WRITEBACKUP
11718 #ifdef FEAT_XFONTSET
11724 #ifdef USE_XSMP_INTERACT
11727 #ifdef FEAT_XCLIPBOARD
11730 #ifdef FEAT_XTERM_SAVE
11733 #if defined(UNIX) && defined(FEAT_X11)
11739 name
= get_tv_string(&argvars
[0]);
11740 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11741 if (STRICMP(name
, has_list
[i
]) == 0)
11749 if (STRNICMP(name
, "patch", 5) == 0)
11750 n
= has_patch(atoi((char *)name
+ 5));
11751 else if (STRICMP(name
, "vim_starting") == 0)
11752 n
= (starting
!= 0);
11753 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11754 else if (STRICMP(name
, "balloon_multiline") == 0)
11755 n
= multiline_balloon_available();
11758 else if (STRICMP(name
, "tcl") == 0)
11759 n
= tcl_enabled(FALSE
);
11761 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11762 else if (STRICMP(name
, "iconv") == 0)
11763 n
= iconv_enabled(FALSE
);
11765 #ifdef DYNAMIC_MZSCHEME
11766 else if (STRICMP(name
, "mzscheme") == 0)
11767 n
= mzscheme_enabled(FALSE
);
11769 #ifdef DYNAMIC_RUBY
11770 else if (STRICMP(name
, "ruby") == 0)
11771 n
= ruby_enabled(FALSE
);
11773 #ifdef DYNAMIC_PYTHON
11774 else if (STRICMP(name
, "python") == 0)
11775 n
= python_enabled(FALSE
);
11777 #ifdef DYNAMIC_PERL
11778 else if (STRICMP(name
, "perl") == 0)
11779 n
= perl_enabled(FALSE
);
11782 else if (STRICMP(name
, "gui_running") == 0)
11783 n
= (gui
.in_use
|| gui
.starting
);
11784 # ifdef FEAT_GUI_W32
11785 else if (STRICMP(name
, "gui_win32s") == 0)
11786 n
= gui_is_win32s();
11788 # ifdef FEAT_BROWSE
11789 else if (STRICMP(name
, "browse") == 0)
11790 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11794 else if (STRICMP(name
, "syntax_items") == 0)
11795 n
= syntax_present(curbuf
);
11797 #if defined(WIN3264)
11798 else if (STRICMP(name
, "win95") == 0)
11799 n
= mch_windows95();
11801 #ifdef FEAT_NETBEANS_INTG
11802 else if (STRICMP(name
, "netbeans_enabled") == 0)
11807 rettv
->vval
.v_number
= n
;
11811 * "has_key()" function
11814 f_has_key(argvars
, rettv
)
11818 rettv
->vval
.v_number
= 0;
11819 if (argvars
[0].v_type
!= VAR_DICT
)
11821 EMSG(_(e_dictreq
));
11824 if (argvars
[0].vval
.v_dict
== NULL
)
11827 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11828 get_tv_string(&argvars
[1]), -1) != NULL
;
11832 * "haslocaldir()" function
11836 f_haslocaldir(argvars
, rettv
)
11840 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11844 * "hasmapto()" function
11847 f_hasmapto(argvars
, rettv
)
11853 char_u buf
[NUMBUFLEN
];
11856 name
= get_tv_string(&argvars
[0]);
11857 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11858 mode
= (char_u
*)"nvo";
11861 mode
= get_tv_string_buf(&argvars
[1], buf
);
11862 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11863 abbr
= get_tv_number(&argvars
[2]);
11866 if (map_to_exists(name
, mode
, abbr
))
11867 rettv
->vval
.v_number
= TRUE
;
11869 rettv
->vval
.v_number
= FALSE
;
11873 * "histadd()" function
11877 f_histadd(argvars
, rettv
)
11881 #ifdef FEAT_CMDHIST
11884 char_u buf
[NUMBUFLEN
];
11887 rettv
->vval
.v_number
= FALSE
;
11888 if (check_restricted() || check_secure())
11890 #ifdef FEAT_CMDHIST
11891 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11892 histype
= str
!= NULL
? get_histtype(str
) : -1;
11895 str
= get_tv_string_buf(&argvars
[1], buf
);
11898 add_to_history(histype
, str
, FALSE
, NUL
);
11899 rettv
->vval
.v_number
= TRUE
;
11907 * "histdel()" function
11911 f_histdel(argvars
, rettv
)
11915 #ifdef FEAT_CMDHIST
11917 char_u buf
[NUMBUFLEN
];
11920 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11923 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
11924 /* only one argument: clear entire history */
11925 n
= clr_history(get_histtype(str
));
11926 else if (argvars
[1].v_type
== VAR_NUMBER
)
11927 /* index given: remove that entry */
11928 n
= del_history_idx(get_histtype(str
),
11929 (int)get_tv_number(&argvars
[1]));
11931 /* string given: remove all matching entries */
11932 n
= del_history_entry(get_histtype(str
),
11933 get_tv_string_buf(&argvars
[1], buf
));
11934 rettv
->vval
.v_number
= n
;
11936 rettv
->vval
.v_number
= 0;
11941 * "histget()" function
11945 f_histget(argvars
, rettv
)
11949 #ifdef FEAT_CMDHIST
11954 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11956 rettv
->vval
.v_string
= NULL
;
11959 type
= get_histtype(str
);
11960 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11961 idx
= get_history_idx(type
);
11963 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
11964 /* -1 on type error */
11965 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
11968 rettv
->vval
.v_string
= NULL
;
11970 rettv
->v_type
= VAR_STRING
;
11974 * "histnr()" function
11978 f_histnr(argvars
, rettv
)
11984 #ifdef FEAT_CMDHIST
11985 char_u
*history
= get_tv_string_chk(&argvars
[0]);
11987 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
11988 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
11989 i
= get_history_idx(i
);
11993 rettv
->vval
.v_number
= i
;
11997 * "highlightID(name)" function
12000 f_hlID(argvars
, rettv
)
12004 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
12008 * "highlight_exists()" function
12011 f_hlexists(argvars
, rettv
)
12015 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
12019 * "hostname()" function
12023 f_hostname(argvars
, rettv
)
12027 char_u hostname
[256];
12029 mch_get_host_name(hostname
, 256);
12030 rettv
->v_type
= VAR_STRING
;
12031 rettv
->vval
.v_string
= vim_strsave(hostname
);
12039 f_iconv(argvars
, rettv
)
12044 char_u buf1
[NUMBUFLEN
];
12045 char_u buf2
[NUMBUFLEN
];
12046 char_u
*from
, *to
, *str
;
12050 rettv
->v_type
= VAR_STRING
;
12051 rettv
->vval
.v_string
= NULL
;
12054 str
= get_tv_string(&argvars
[0]);
12055 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
12056 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
12057 vimconv
.vc_type
= CONV_NONE
;
12058 convert_setup(&vimconv
, from
, to
);
12060 /* If the encodings are equal, no conversion needed. */
12061 if (vimconv
.vc_type
== CONV_NONE
)
12062 rettv
->vval
.v_string
= vim_strsave(str
);
12064 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
12066 convert_setup(&vimconv
, NULL
, NULL
);
12073 * "indent()" function
12076 f_indent(argvars
, rettv
)
12082 lnum
= get_tv_lnum(argvars
);
12083 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12084 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
12086 rettv
->vval
.v_number
= -1;
12090 * "index()" function
12093 f_index(argvars
, rettv
)
12102 rettv
->vval
.v_number
= -1;
12103 if (argvars
[0].v_type
!= VAR_LIST
)
12105 EMSG(_(e_listreq
));
12108 l
= argvars
[0].vval
.v_list
;
12111 item
= l
->lv_first
;
12112 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12116 /* Start at specified item. Use the cached index that list_find()
12117 * sets, so that a negative number also works. */
12118 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
12120 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12121 ic
= get_tv_number_chk(&argvars
[3], &error
);
12126 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
12127 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
12129 rettv
->vval
.v_number
= idx
;
12135 static int inputsecret_flag
= 0;
12137 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
12140 * This function is used by f_input() and f_inputdialog() functions. The third
12141 * argument to f_input() specifies the type of completion to use at the
12142 * prompt. The third argument to f_inputdialog() specifies the value to return
12143 * when the user cancels the prompt.
12146 get_user_input(argvars
, rettv
, inputdialog
)
12151 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
12154 char_u buf
[NUMBUFLEN
];
12155 int cmd_silent_save
= cmd_silent
;
12156 char_u
*defstr
= (char_u
*)"";
12157 int xp_type
= EXPAND_NOTHING
;
12158 char_u
*xp_arg
= NULL
;
12160 rettv
->v_type
= VAR_STRING
;
12161 rettv
->vval
.v_string
= NULL
;
12163 #ifdef NO_CONSOLE_INPUT
12164 /* While starting up, there is no place to enter text. */
12165 if (no_console_input())
12169 cmd_silent
= FALSE
; /* Want to see the prompt. */
12170 if (prompt
!= NULL
)
12172 /* Only the part of the message after the last NL is considered as
12173 * prompt for the command line */
12174 p
= vim_strrchr(prompt
, '\n');
12184 msg_puts_attr(prompt
, echo_attr
);
12185 msg_didout
= FALSE
;
12189 cmdline_row
= msg_row
;
12191 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12193 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
12194 if (defstr
!= NULL
)
12195 stuffReadbuffSpec(defstr
);
12197 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
12203 rettv
->vval
.v_string
= NULL
;
12205 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
12206 if (xp_name
== NULL
)
12209 xp_namelen
= (int)STRLEN(xp_name
);
12211 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
12217 if (defstr
!= NULL
)
12218 rettv
->vval
.v_string
=
12219 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
12224 /* since the user typed this, no need to wait for return */
12225 need_wait_return
= FALSE
;
12226 msg_didout
= FALSE
;
12228 cmd_silent
= cmd_silent_save
;
12232 * "input()" function
12233 * Also handles inputsecret() when inputsecret is set.
12236 f_input(argvars
, rettv
)
12240 get_user_input(argvars
, rettv
, FALSE
);
12244 * "inputdialog()" function
12247 f_inputdialog(argvars
, rettv
)
12251 #if defined(FEAT_GUI_TEXTDIALOG)
12252 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12253 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
12256 char_u buf
[NUMBUFLEN
];
12257 char_u
*defstr
= (char_u
*)"";
12259 message
= get_tv_string_chk(&argvars
[0]);
12260 if (argvars
[1].v_type
!= VAR_UNKNOWN
12261 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
12262 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
12265 if (message
!= NULL
&& defstr
!= NULL
12266 && do_dialog(VIM_QUESTION
, NULL
, message
,
12267 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
12268 rettv
->vval
.v_string
= vim_strsave(IObuff
);
12271 if (message
!= NULL
&& defstr
!= NULL
12272 && argvars
[1].v_type
!= VAR_UNKNOWN
12273 && argvars
[2].v_type
!= VAR_UNKNOWN
)
12274 rettv
->vval
.v_string
= vim_strsave(
12275 get_tv_string_buf(&argvars
[2], buf
));
12277 rettv
->vval
.v_string
= NULL
;
12279 rettv
->v_type
= VAR_STRING
;
12283 get_user_input(argvars
, rettv
, TRUE
);
12287 * "inputlist()" function
12290 f_inputlist(argvars
, rettv
)
12298 rettv
->vval
.v_number
= 0;
12299 #ifdef NO_CONSOLE_INPUT
12300 /* While starting up, there is no place to enter text. */
12301 if (no_console_input())
12304 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
12306 EMSG2(_(e_listarg
), "inputlist()");
12311 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
12312 lines_left
= Rows
; /* avoid more prompt */
12316 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
12318 msg_puts(get_tv_string(&li
->li_tv
));
12322 /* Ask for choice. */
12323 selected
= prompt_for_number(&mouse_used
);
12325 selected
-= lines_left
;
12327 rettv
->vval
.v_number
= selected
;
12331 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
12334 * "inputrestore()" function
12338 f_inputrestore(argvars
, rettv
)
12342 if (ga_userinput
.ga_len
> 0)
12344 --ga_userinput
.ga_len
;
12345 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12346 + ga_userinput
.ga_len
);
12347 rettv
->vval
.v_number
= 0; /* OK */
12349 else if (p_verbose
> 1)
12351 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
12352 rettv
->vval
.v_number
= 1; /* Failed */
12357 * "inputsave()" function
12361 f_inputsave(argvars
, rettv
)
12365 /* Add an entry to the stack of typeahead storage. */
12366 if (ga_grow(&ga_userinput
, 1) == OK
)
12368 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12369 + ga_userinput
.ga_len
);
12370 ++ga_userinput
.ga_len
;
12371 rettv
->vval
.v_number
= 0; /* OK */
12374 rettv
->vval
.v_number
= 1; /* Failed */
12378 * "inputsecret()" function
12381 f_inputsecret(argvars
, rettv
)
12386 ++inputsecret_flag
;
12387 f_input(argvars
, rettv
);
12389 --inputsecret_flag
;
12393 * "insert()" function
12396 f_insert(argvars
, rettv
)
12405 rettv
->vval
.v_number
= 0;
12406 if (argvars
[0].v_type
!= VAR_LIST
)
12407 EMSG2(_(e_listarg
), "insert()");
12408 else if ((l
= argvars
[0].vval
.v_list
) != NULL
12409 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
12411 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12412 before
= get_tv_number_chk(&argvars
[2], &error
);
12414 return; /* type error; errmsg already given */
12416 if (before
== l
->lv_len
)
12420 item
= list_find(l
, before
);
12423 EMSGN(_(e_listidx
), before
);
12429 list_insert_tv(l
, &argvars
[1], item
);
12430 copy_tv(&argvars
[0], rettv
);
12436 * "isdirectory()" function
12439 f_isdirectory(argvars
, rettv
)
12443 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
12447 * "islocked()" function
12450 f_islocked(argvars
, rettv
)
12458 rettv
->vval
.v_number
= -1;
12459 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
12461 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
12464 EMSG(_(e_trailing
));
12467 if (lv
.ll_tv
== NULL
)
12469 if (check_changedtick(lv
.ll_name
))
12470 rettv
->vval
.v_number
= 1; /* always locked */
12473 di
= find_var(lv
.ll_name
, NULL
);
12476 /* Consider a variable locked when:
12477 * 1. the variable itself is locked
12478 * 2. the value of the variable is locked.
12479 * 3. the List or Dict value is locked.
12481 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
12482 || tv_islocked(&di
->di_tv
));
12486 else if (lv
.ll_range
)
12487 EMSG(_("E786: Range not allowed"));
12488 else if (lv
.ll_newkey
!= NULL
)
12489 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
12490 else if (lv
.ll_list
!= NULL
)
12492 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
12494 /* Dictionary item. */
12495 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
12502 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
12505 * Turn a dict into a list:
12506 * "what" == 0: list of keys
12507 * "what" == 1: list of values
12508 * "what" == 2: list of items
12511 dict_list(argvars
, rettv
, what
)
12524 rettv
->vval
.v_number
= 0;
12525 if (argvars
[0].v_type
!= VAR_DICT
)
12527 EMSG(_(e_dictreq
));
12530 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
12533 if (rettv_list_alloc(rettv
) == FAIL
)
12536 todo
= (int)d
->dv_hashtab
.ht_used
;
12537 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12539 if (!HASHITEM_EMPTY(hi
))
12544 li
= listitem_alloc();
12547 list_append(rettv
->vval
.v_list
, li
);
12552 li
->li_tv
.v_type
= VAR_STRING
;
12553 li
->li_tv
.v_lock
= 0;
12554 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12556 else if (what
== 1)
12559 copy_tv(&di
->di_tv
, &li
->li_tv
);
12565 li
->li_tv
.v_type
= VAR_LIST
;
12566 li
->li_tv
.v_lock
= 0;
12567 li
->li_tv
.vval
.v_list
= l2
;
12572 li2
= listitem_alloc();
12575 list_append(l2
, li2
);
12576 li2
->li_tv
.v_type
= VAR_STRING
;
12577 li2
->li_tv
.v_lock
= 0;
12578 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12580 li2
= listitem_alloc();
12583 list_append(l2
, li2
);
12584 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12591 * "items(dict)" function
12594 f_items(argvars
, rettv
)
12598 dict_list(argvars
, rettv
, 2);
12602 * "join()" function
12605 f_join(argvars
, rettv
)
12612 rettv
->vval
.v_number
= 0;
12613 if (argvars
[0].v_type
!= VAR_LIST
)
12615 EMSG(_(e_listreq
));
12618 if (argvars
[0].vval
.v_list
== NULL
)
12620 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12621 sep
= (char_u
*)" ";
12623 sep
= get_tv_string_chk(&argvars
[1]);
12625 rettv
->v_type
= VAR_STRING
;
12629 ga_init2(&ga
, (int)sizeof(char), 80);
12630 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12631 ga_append(&ga
, NUL
);
12632 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12635 rettv
->vval
.v_string
= NULL
;
12639 * "keys()" function
12642 f_keys(argvars
, rettv
)
12646 dict_list(argvars
, rettv
, 0);
12650 * "last_buffer_nr()" function.
12654 f_last_buffer_nr(argvars
, rettv
)
12661 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12662 if (n
< buf
->b_fnum
)
12665 rettv
->vval
.v_number
= n
;
12672 f_len(argvars
, rettv
)
12676 switch (argvars
[0].v_type
)
12680 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12681 get_tv_string(&argvars
[0]));
12684 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12687 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12690 EMSG(_("E701: Invalid type for len()"));
12695 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12698 libcall_common(argvars
, rettv
, type
)
12703 #ifdef FEAT_LIBCALL
12705 char_u
**string_result
;
12709 rettv
->v_type
= type
;
12710 if (type
== VAR_NUMBER
)
12711 rettv
->vval
.v_number
= 0;
12713 rettv
->vval
.v_string
= NULL
;
12715 if (check_restricted() || check_secure())
12718 #ifdef FEAT_LIBCALL
12719 /* The first two args must be strings, otherwise its meaningless */
12720 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12723 if (argvars
[2].v_type
== VAR_STRING
)
12724 string_in
= argvars
[2].vval
.v_string
;
12725 if (type
== VAR_NUMBER
)
12726 string_result
= NULL
;
12728 string_result
= &rettv
->vval
.v_string
;
12729 if (mch_libcall(argvars
[0].vval
.v_string
,
12730 argvars
[1].vval
.v_string
,
12732 argvars
[2].vval
.v_number
,
12735 && type
== VAR_NUMBER
)
12736 rettv
->vval
.v_number
= nr_result
;
12742 * "libcall()" function
12745 f_libcall(argvars
, rettv
)
12749 libcall_common(argvars
, rettv
, VAR_STRING
);
12753 * "libcallnr()" function
12756 f_libcallnr(argvars
, rettv
)
12760 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12764 * "line(string)" function
12767 f_line(argvars
, rettv
)
12775 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12778 rettv
->vval
.v_number
= lnum
;
12782 * "line2byte(lnum)" function
12786 f_line2byte(argvars
, rettv
)
12790 #ifndef FEAT_BYTEOFF
12791 rettv
->vval
.v_number
= -1;
12795 lnum
= get_tv_lnum(argvars
);
12796 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12797 rettv
->vval
.v_number
= -1;
12799 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12800 if (rettv
->vval
.v_number
>= 0)
12801 ++rettv
->vval
.v_number
;
12806 * "lispindent(lnum)" function
12809 f_lispindent(argvars
, rettv
)
12817 pos
= curwin
->w_cursor
;
12818 lnum
= get_tv_lnum(argvars
);
12819 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12821 curwin
->w_cursor
.lnum
= lnum
;
12822 rettv
->vval
.v_number
= get_lisp_indent();
12823 curwin
->w_cursor
= pos
;
12827 rettv
->vval
.v_number
= -1;
12831 * "localtime()" function
12835 f_localtime(argvars
, rettv
)
12839 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12842 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12845 get_maparg(argvars
, rettv
, exact
)
12852 char_u buf
[NUMBUFLEN
];
12853 char_u
*keys_buf
= NULL
;
12859 /* return empty string for failure */
12860 rettv
->v_type
= VAR_STRING
;
12861 rettv
->vval
.v_string
= NULL
;
12863 keys
= get_tv_string(&argvars
[0]);
12867 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12869 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12870 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12871 abbr
= get_tv_number(&argvars
[2]);
12874 which
= (char_u
*)"";
12878 mode
= get_map_mode(&which
, 0);
12880 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12881 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
12882 vim_free(keys_buf
);
12886 ga
.ga_itemsize
= 1;
12887 ga
.ga_growsize
= 40;
12889 while (*rhs
!= NUL
)
12890 ga_concat(&ga
, str2special(&rhs
, FALSE
));
12892 ga_append(&ga
, NUL
);
12893 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12899 * "log10()" function
12902 f_log10(argvars
, rettv
)
12908 rettv
->v_type
= VAR_FLOAT
;
12909 if (get_float_arg(argvars
, &f
) == OK
)
12910 rettv
->vval
.v_float
= log10(f
);
12912 rettv
->vval
.v_float
= 0.0;
12920 f_map(argvars
, rettv
)
12924 filter_map(argvars
, rettv
, TRUE
);
12928 * "maparg()" function
12931 f_maparg(argvars
, rettv
)
12935 get_maparg(argvars
, rettv
, TRUE
);
12939 * "mapcheck()" function
12942 f_mapcheck(argvars
, rettv
)
12946 get_maparg(argvars
, rettv
, FALSE
);
12949 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
12952 find_some_match(argvars
, rettv
, type
)
12957 char_u
*str
= NULL
;
12958 char_u
*expr
= NULL
;
12960 regmatch_T regmatch
;
12961 char_u patbuf
[NUMBUFLEN
];
12962 char_u strbuf
[NUMBUFLEN
];
12966 colnr_T startcol
= 0;
12969 listitem_T
*li
= NULL
;
12971 char_u
*tofree
= NULL
;
12973 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12975 p_cpo
= (char_u
*)"";
12977 rettv
->vval
.v_number
= -1;
12980 /* return empty list when there are no matches */
12981 if (rettv_list_alloc(rettv
) == FAIL
)
12984 else if (type
== 2)
12986 rettv
->v_type
= VAR_STRING
;
12987 rettv
->vval
.v_string
= NULL
;
12990 if (argvars
[0].v_type
== VAR_LIST
)
12992 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
12997 expr
= str
= get_tv_string(&argvars
[0]);
12999 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
13003 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13007 start
= get_tv_number_chk(&argvars
[2], &error
);
13012 li
= list_find(l
, start
);
13015 idx
= l
->lv_idx
; /* use the cached index */
13021 if (start
> (long)STRLEN(str
))
13023 /* When "count" argument is there ignore matches before "start",
13024 * otherwise skip part of the string. Differs when pattern is "^"
13026 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13032 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13033 nth
= get_tv_number_chk(&argvars
[3], &error
);
13038 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
13039 if (regmatch
.regprog
!= NULL
)
13041 regmatch
.rm_ic
= p_ic
;
13053 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
13058 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
13060 if (match
&& --nth
<= 0)
13062 if (l
== NULL
&& !match
)
13065 /* Advance to just after the match. */
13074 startcol
= (colnr_T
)(regmatch
.startp
[0]
13075 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
13077 startcol
= regmatch
.startp
[0] + 1 - str
;
13088 /* return list with matched string and submatches */
13089 for (i
= 0; i
< NSUBEXP
; ++i
)
13091 if (regmatch
.endp
[i
] == NULL
)
13093 if (list_append_string(rettv
->vval
.v_list
,
13094 (char_u
*)"", 0) == FAIL
)
13097 else if (list_append_string(rettv
->vval
.v_list
,
13098 regmatch
.startp
[i
],
13099 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
13104 else if (type
== 2)
13106 /* return matched string */
13108 copy_tv(&li
->li_tv
, rettv
);
13110 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
13111 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
13113 else if (l
!= NULL
)
13114 rettv
->vval
.v_number
= idx
;
13118 rettv
->vval
.v_number
=
13119 (varnumber_T
)(regmatch
.startp
[0] - str
);
13121 rettv
->vval
.v_number
=
13122 (varnumber_T
)(regmatch
.endp
[0] - str
);
13123 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
13126 vim_free(regmatch
.regprog
);
13135 * "match()" function
13138 f_match(argvars
, rettv
)
13142 find_some_match(argvars
, rettv
, 1);
13146 * "matchadd()" function
13149 f_matchadd(argvars
, rettv
)
13153 #ifdef FEAT_SEARCH_EXTRA
13154 char_u buf
[NUMBUFLEN
];
13155 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
13156 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
13157 int prio
= 10; /* default priority */
13161 rettv
->vval
.v_number
= -1;
13163 if (grp
== NULL
|| pat
== NULL
)
13165 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13167 prio
= get_tv_number_chk(&argvars
[2], &error
);
13168 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13169 id
= get_tv_number_chk(&argvars
[3], &error
);
13173 if (id
>= 1 && id
<= 3)
13175 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
13179 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
13184 * "matcharg()" function
13187 f_matcharg(argvars
, rettv
)
13191 if (rettv_list_alloc(rettv
) == OK
)
13193 #ifdef FEAT_SEARCH_EXTRA
13194 int id
= get_tv_number(&argvars
[0]);
13197 if (id
>= 1 && id
<= 3)
13199 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
13201 list_append_string(rettv
->vval
.v_list
,
13202 syn_id2name(m
->hlg_id
), -1);
13203 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
13207 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13208 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13216 * "matchdelete()" function
13219 f_matchdelete(argvars
, rettv
)
13223 #ifdef FEAT_SEARCH_EXTRA
13224 rettv
->vval
.v_number
= match_delete(curwin
,
13225 (int)get_tv_number(&argvars
[0]), TRUE
);
13230 * "matchend()" function
13233 f_matchend(argvars
, rettv
)
13237 find_some_match(argvars
, rettv
, 0);
13241 * "matchlist()" function
13244 f_matchlist(argvars
, rettv
)
13248 find_some_match(argvars
, rettv
, 3);
13252 * "matchstr()" function
13255 f_matchstr(argvars
, rettv
)
13259 find_some_match(argvars
, rettv
, 2);
13262 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
13265 max_min(argvars
, rettv
, domax
)
13274 if (argvars
[0].v_type
== VAR_LIST
)
13279 l
= argvars
[0].vval
.v_list
;
13285 n
= get_tv_number_chk(&li
->li_tv
, &error
);
13291 i
= get_tv_number_chk(&li
->li_tv
, &error
);
13292 if (domax
? i
> n
: i
< n
)
13298 else if (argvars
[0].v_type
== VAR_DICT
)
13305 d
= argvars
[0].vval
.v_dict
;
13308 todo
= (int)d
->dv_hashtab
.ht_used
;
13309 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
13311 if (!HASHITEM_EMPTY(hi
))
13314 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
13320 else if (domax
? i
> n
: i
< n
)
13327 EMSG(_(e_listdictarg
));
13328 rettv
->vval
.v_number
= error
? 0 : n
;
13335 f_max(argvars
, rettv
)
13339 max_min(argvars
, rettv
, TRUE
);
13346 f_min(argvars
, rettv
)
13350 max_min(argvars
, rettv
, FALSE
);
13353 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
13356 * Create the directory in which "dir" is located, and higher levels when
13360 mkdir_recurse(dir
, prot
)
13368 /* Get end of directory name in "dir".
13369 * We're done when it's "/" or "c:/". */
13370 p
= gettail_sep(dir
);
13371 if (p
<= get_past_head(dir
))
13374 /* If the directory exists we're done. Otherwise: create it.*/
13375 updir
= vim_strnsave(dir
, (int)(p
- dir
));
13378 if (mch_isdir(updir
))
13380 else if (mkdir_recurse(updir
, prot
) == OK
)
13381 r
= vim_mkdir_emsg(updir
, prot
);
13388 * "mkdir()" function
13391 f_mkdir(argvars
, rettv
)
13396 char_u buf
[NUMBUFLEN
];
13399 rettv
->vval
.v_number
= FAIL
;
13400 if (check_restricted() || check_secure())
13403 dir
= get_tv_string_buf(&argvars
[0], buf
);
13404 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13406 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13407 prot
= get_tv_number_chk(&argvars
[2], NULL
);
13408 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
13409 mkdir_recurse(dir
, prot
);
13411 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
13416 * "mode()" function
13420 f_mode(argvars
, rettv
)
13433 buf
[0] = VIsual_mode
+ 's' - 'v';
13435 buf
[0] = VIsual_mode
;
13439 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
13440 || State
== CONFIRM
)
13443 if (State
== ASKMORE
)
13445 else if (State
== CONFIRM
)
13448 else if (State
== EXTERNCMD
)
13450 else if (State
& INSERT
)
13452 #ifdef FEAT_VREPLACE
13453 if (State
& VREPLACE_FLAG
)
13460 if (State
& REPLACE_FLAG
)
13465 else if (State
& CMDLINE
)
13471 else if (exmode_active
)
13483 /* A zero number or empty string argument: return only major mode. */
13484 if (!(argvars
[0].v_type
== VAR_NUMBER
&& argvars
[0].vval
.v_number
!= 0)
13485 && !(argvars
[0].v_type
== VAR_STRING
13486 && *get_tv_string(&argvars
[0]) != NUL
))
13489 rettv
->vval
.v_string
= vim_strsave(buf
);
13490 rettv
->v_type
= VAR_STRING
;
13494 * "nextnonblank()" function
13497 f_nextnonblank(argvars
, rettv
)
13503 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
13505 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
13510 if (*skipwhite(ml_get(lnum
)) != NUL
)
13513 rettv
->vval
.v_number
= lnum
;
13517 * "nr2char()" function
13520 f_nr2char(argvars
, rettv
)
13524 char_u buf
[NUMBUFLEN
];
13528 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
13532 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
13535 rettv
->v_type
= VAR_STRING
;
13536 rettv
->vval
.v_string
= vim_strsave(buf
);
13540 * "pathshorten()" function
13543 f_pathshorten(argvars
, rettv
)
13549 rettv
->v_type
= VAR_STRING
;
13550 p
= get_tv_string_chk(&argvars
[0]);
13552 rettv
->vval
.v_string
= NULL
;
13555 p
= vim_strsave(p
);
13556 rettv
->vval
.v_string
= p
;
13567 f_pow(argvars
, rettv
)
13573 rettv
->v_type
= VAR_FLOAT
;
13574 if (get_float_arg(argvars
, &fx
) == OK
13575 && get_float_arg(&argvars
[1], &fy
) == OK
)
13576 rettv
->vval
.v_float
= pow(fx
, fy
);
13578 rettv
->vval
.v_float
= 0.0;
13583 * "prevnonblank()" function
13586 f_prevnonblank(argvars
, rettv
)
13592 lnum
= get_tv_lnum(argvars
);
13593 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
13596 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
13598 rettv
->vval
.v_number
= lnum
;
13601 #ifdef HAVE_STDARG_H
13602 /* This dummy va_list is here because:
13603 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13604 * - locally in the function results in a "used before set" warning
13605 * - using va_start() to initialize it gives "function with fixed args" error */
13610 * "printf()" function
13613 f_printf(argvars
, rettv
)
13617 rettv
->v_type
= VAR_STRING
;
13618 rettv
->vval
.v_string
= NULL
;
13619 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
13621 char_u buf
[NUMBUFLEN
];
13624 int saved_did_emsg
= did_emsg
;
13627 /* Get the required length, allocate the buffer and do it for real. */
13629 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
13630 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
13633 s
= alloc(len
+ 1);
13636 rettv
->vval
.v_string
= s
;
13637 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
13640 did_emsg
|= saved_did_emsg
;
13646 * "pumvisible()" function
13650 f_pumvisible(argvars
, rettv
)
13654 rettv
->vval
.v_number
= 0;
13655 #ifdef FEAT_INS_EXPAND
13657 rettv
->vval
.v_number
= 1;
13662 * "range()" function
13665 f_range(argvars
, rettv
)
13675 start
= get_tv_number_chk(&argvars
[0], &error
);
13676 if (argvars
[1].v_type
== VAR_UNKNOWN
)
13683 end
= get_tv_number_chk(&argvars
[1], &error
);
13684 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13685 stride
= get_tv_number_chk(&argvars
[2], &error
);
13688 rettv
->vval
.v_number
= 0;
13690 return; /* type error; errmsg already given */
13692 EMSG(_("E726: Stride is zero"));
13693 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
13694 EMSG(_("E727: Start past end"));
13697 if (rettv_list_alloc(rettv
) == OK
)
13698 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
13699 if (list_append_number(rettv
->vval
.v_list
,
13700 (varnumber_T
)i
) == FAIL
)
13706 * "readfile()" function
13709 f_readfile(argvars
, rettv
)
13713 int binary
= FALSE
;
13717 #define FREAD_SIZE 200 /* optimized for text lines */
13718 char_u buf
[FREAD_SIZE
];
13719 int readlen
; /* size of last fread() */
13720 int buflen
; /* nr of valid chars in buf[] */
13721 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
13722 int tolist
; /* first byte in buf[] still to be put in list */
13723 int chop
; /* how many CR to chop off */
13724 char_u
*prev
= NULL
; /* previously read bytes, if any */
13725 int prevlen
= 0; /* length of "prev" if not NULL */
13728 long maxline
= MAXLNUM
;
13731 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13733 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
13735 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13736 maxline
= get_tv_number(&argvars
[2]);
13739 if (rettv_list_alloc(rettv
) == FAIL
)
13742 /* Always open the file in binary mode, library functions have a mind of
13743 * their own about CR-LF conversion. */
13744 fname
= get_tv_string(&argvars
[0]);
13745 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
13747 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
13752 while (cnt
< maxline
|| maxline
< 0)
13754 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
13755 buflen
= filtd
+ readlen
;
13757 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
13759 if (buf
[filtd
] == '\n' || readlen
<= 0)
13761 /* Only when in binary mode add an empty list item when the
13762 * last line ends in a '\n'. */
13763 if (!binary
&& readlen
== 0 && filtd
== 0)
13766 /* Found end-of-line or end-of-file: add a text line to the
13770 while (filtd
- chop
- 1 >= tolist
13771 && buf
[filtd
- chop
- 1] == '\r')
13773 len
= filtd
- tolist
- chop
;
13775 s
= vim_strnsave(buf
+ tolist
, len
);
13778 s
= alloc((unsigned)(prevlen
+ len
+ 1));
13781 mch_memmove(s
, prev
, prevlen
);
13784 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
13785 s
[prevlen
+ len
] = NUL
;
13788 tolist
= filtd
+ 1;
13790 li
= listitem_alloc();
13796 li
->li_tv
.v_type
= VAR_STRING
;
13797 li
->li_tv
.v_lock
= 0;
13798 li
->li_tv
.vval
.v_string
= s
;
13799 list_append(rettv
->vval
.v_list
, li
);
13801 if (++cnt
>= maxline
&& maxline
>= 0)
13806 else if (buf
[filtd
] == NUL
)
13814 /* "buf" is full, need to move text to an allocated buffer */
13817 prev
= vim_strnsave(buf
, buflen
);
13822 s
= alloc((unsigned)(prevlen
+ buflen
));
13825 mch_memmove(s
, prev
, prevlen
);
13826 mch_memmove(s
+ prevlen
, buf
, buflen
);
13836 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13842 * For a negative line count use only the lines at the end of the file,
13846 while (cnt
> -maxline
)
13848 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
13856 #if defined(FEAT_RELTIME)
13857 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
13860 * Convert a List to proftime_T.
13861 * Return FAIL when there is something wrong.
13864 list2proftime(arg
, tm
)
13871 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
13872 || arg
->vval
.v_list
->lv_len
!= 2)
13874 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
13875 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
13883 return error
? FAIL
: OK
;
13885 #endif /* FEAT_RELTIME */
13888 * "reltime()" function
13891 f_reltime(argvars
, rettv
)
13895 #ifdef FEAT_RELTIME
13899 if (argvars
[0].v_type
== VAR_UNKNOWN
)
13901 /* No arguments: get current time. */
13902 profile_start(&res
);
13904 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
13906 if (list2proftime(&argvars
[0], &res
) == FAIL
)
13912 /* Two arguments: compute the difference. */
13913 if (list2proftime(&argvars
[0], &start
) == FAIL
13914 || list2proftime(&argvars
[1], &res
) == FAIL
)
13916 profile_sub(&res
, &start
);
13919 if (rettv_list_alloc(rettv
) == OK
)
13930 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
13931 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
13937 * "reltimestr()" function
13940 f_reltimestr(argvars
, rettv
)
13944 #ifdef FEAT_RELTIME
13948 rettv
->v_type
= VAR_STRING
;
13949 rettv
->vval
.v_string
= NULL
;
13950 #ifdef FEAT_RELTIME
13951 if (list2proftime(&argvars
[0], &tm
) == OK
)
13952 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
13956 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13957 static void make_connection
__ARGS((void));
13958 static int check_connection
__ARGS((void));
13963 if (X_DISPLAY
== NULL
13969 x_force_connect
= TRUE
;
13971 x_force_connect
= FALSE
;
13979 if (X_DISPLAY
== NULL
)
13981 EMSG(_("E240: No connection to Vim server"));
13988 #ifdef FEAT_CLIENTSERVER
13989 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
13992 remote_common(argvars
, rettv
, expr
)
13997 char_u
*server_name
;
14000 char_u buf
[NUMBUFLEN
];
14007 if (check_restricted() || check_secure())
14011 if (check_connection() == FAIL
)
14015 server_name
= get_tv_string_chk(&argvars
[0]);
14016 if (server_name
== NULL
)
14017 return; /* type error; errmsg already given */
14018 keys
= get_tv_string_buf(&argvars
[1], buf
);
14020 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14022 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
14027 EMSG(r
); /* sending worked but evaluation failed */
14029 EMSG2(_("E241: Unable to send to %s"), server_name
);
14033 rettv
->vval
.v_string
= r
;
14035 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14041 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
14042 v
.di_tv
.v_type
= VAR_STRING
;
14043 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
14044 idvar
= get_tv_string_chk(&argvars
[2]);
14046 set_var(idvar
, &v
.di_tv
, FALSE
);
14047 vim_free(v
.di_tv
.vval
.v_string
);
14053 * "remote_expr()" function
14057 f_remote_expr(argvars
, rettv
)
14061 rettv
->v_type
= VAR_STRING
;
14062 rettv
->vval
.v_string
= NULL
;
14063 #ifdef FEAT_CLIENTSERVER
14064 remote_common(argvars
, rettv
, TRUE
);
14069 * "remote_foreground()" function
14073 f_remote_foreground(argvars
, rettv
)
14077 rettv
->vval
.v_number
= 0;
14078 #ifdef FEAT_CLIENTSERVER
14080 /* On Win32 it's done in this application. */
14082 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
14084 if (server_name
!= NULL
)
14085 serverForeground(server_name
);
14088 /* Send a foreground() expression to the server. */
14089 argvars
[1].v_type
= VAR_STRING
;
14090 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
14091 argvars
[2].v_type
= VAR_UNKNOWN
;
14092 remote_common(argvars
, rettv
, TRUE
);
14093 vim_free(argvars
[1].vval
.v_string
);
14100 f_remote_peek(argvars
, rettv
)
14104 #ifdef FEAT_CLIENTSERVER
14112 if (check_restricted() || check_secure())
14114 rettv
->vval
.v_number
= -1;
14117 serverid
= get_tv_string_chk(&argvars
[0]);
14118 if (serverid
== NULL
)
14120 rettv
->vval
.v_number
= -1;
14121 return; /* type error; errmsg already given */
14124 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14126 rettv
->vval
.v_number
= -1;
14129 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
14130 rettv
->vval
.v_number
= (s
!= NULL
);
14133 rettv
->vval
.v_number
= 0;
14134 if (check_connection() == FAIL
)
14137 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
14138 serverStrToWin(serverid
), &s
);
14141 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
14145 v
.di_tv
.v_type
= VAR_STRING
;
14146 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
14147 retvar
= get_tv_string_chk(&argvars
[1]);
14148 if (retvar
!= NULL
)
14149 set_var(retvar
, &v
.di_tv
, FALSE
);
14150 vim_free(v
.di_tv
.vval
.v_string
);
14153 rettv
->vval
.v_number
= -1;
14159 f_remote_read(argvars
, rettv
)
14165 #ifdef FEAT_CLIENTSERVER
14166 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
14168 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
14171 /* The server's HWND is encoded in the 'id' parameter */
14174 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14176 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
14179 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
14180 serverStrToWin(serverid
), &r
, FALSE
) < 0)
14182 EMSG(_("E277: Unable to read a server reply"));
14185 rettv
->v_type
= VAR_STRING
;
14186 rettv
->vval
.v_string
= r
;
14190 * "remote_send()" function
14194 f_remote_send(argvars
, rettv
)
14198 rettv
->v_type
= VAR_STRING
;
14199 rettv
->vval
.v_string
= NULL
;
14200 #ifdef FEAT_CLIENTSERVER
14201 remote_common(argvars
, rettv
, FALSE
);
14206 * "remove()" function
14209 f_remove(argvars
, rettv
)
14214 listitem_T
*item
, *item2
;
14222 rettv
->vval
.v_number
= 0;
14223 if (argvars
[0].v_type
== VAR_DICT
)
14225 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14226 EMSG2(_(e_toomanyarg
), "remove()");
14227 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
14228 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
14230 key
= get_tv_string_chk(&argvars
[1]);
14233 di
= dict_find(d
, key
, -1);
14235 EMSG2(_(e_dictkey
), key
);
14238 *rettv
= di
->di_tv
;
14239 init_tv(&di
->di_tv
);
14240 dictitem_remove(d
, di
);
14245 else if (argvars
[0].v_type
!= VAR_LIST
)
14246 EMSG2(_(e_listdictarg
), "remove()");
14247 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14248 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
14252 idx
= get_tv_number_chk(&argvars
[1], &error
);
14254 ; /* type error: do nothing, errmsg already given */
14255 else if ((item
= list_find(l
, idx
)) == NULL
)
14256 EMSGN(_(e_listidx
), idx
);
14259 if (argvars
[2].v_type
== VAR_UNKNOWN
)
14261 /* Remove one item, return its value. */
14262 list_remove(l
, item
, item
);
14263 *rettv
= item
->li_tv
;
14268 /* Remove range of items, return list with values. */
14269 end
= get_tv_number_chk(&argvars
[2], &error
);
14271 ; /* type error: do nothing */
14272 else if ((item2
= list_find(l
, end
)) == NULL
)
14273 EMSGN(_(e_listidx
), end
);
14278 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
14284 if (li
== NULL
) /* didn't find "item2" after "item" */
14285 EMSG(_(e_invrange
));
14288 list_remove(l
, item
, item2
);
14289 if (rettv_list_alloc(rettv
) == OK
)
14291 l
= rettv
->vval
.v_list
;
14292 l
->lv_first
= item
;
14293 l
->lv_last
= item2
;
14294 item
->li_prev
= NULL
;
14295 item2
->li_next
= NULL
;
14306 * "rename({from}, {to})" function
14309 f_rename(argvars
, rettv
)
14313 char_u buf
[NUMBUFLEN
];
14315 if (check_restricted() || check_secure())
14316 rettv
->vval
.v_number
= -1;
14318 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
14319 get_tv_string_buf(&argvars
[1], buf
));
14323 * "repeat()" function
14327 f_repeat(argvars
, rettv
)
14338 n
= get_tv_number(&argvars
[1]);
14339 if (argvars
[0].v_type
== VAR_LIST
)
14341 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
14343 if (list_extend(rettv
->vval
.v_list
,
14344 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
14349 p
= get_tv_string(&argvars
[0]);
14350 rettv
->v_type
= VAR_STRING
;
14351 rettv
->vval
.v_string
= NULL
;
14353 slen
= (int)STRLEN(p
);
14358 r
= alloc(len
+ 1);
14361 for (i
= 0; i
< n
; i
++)
14362 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
14366 rettv
->vval
.v_string
= r
;
14371 * "resolve()" function
14374 f_resolve(argvars
, rettv
)
14380 p
= get_tv_string(&argvars
[0]);
14381 #ifdef FEAT_SHORTCUT
14385 v
= mch_resolve_shortcut(p
);
14387 rettv
->vval
.v_string
= v
;
14389 rettv
->vval
.v_string
= vim_strsave(p
);
14392 # ifdef HAVE_READLINK
14394 char_u buf
[MAXPATHL
+ 1];
14397 char_u
*remain
= NULL
;
14399 int is_relative_to_current
= FALSE
;
14400 int has_trailing_pathsep
= FALSE
;
14403 p
= vim_strsave(p
);
14405 if (p
[0] == '.' && (vim_ispathsep(p
[1])
14406 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
14407 is_relative_to_current
= TRUE
;
14410 if (len
> 0 && after_pathsep(p
, p
+ len
))
14411 has_trailing_pathsep
= TRUE
;
14413 q
= getnextcomp(p
);
14416 /* Separate the first path component in "p", and keep the
14417 * remainder (beginning with the path separator). */
14418 remain
= vim_strsave(q
- 1);
14426 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
14435 EMSG(_("E655: Too many symbolic links (cycle?)"));
14436 rettv
->vval
.v_string
= NULL
;
14440 /* Ensure that the result will have a trailing path separator
14441 * if the argument has one. */
14442 if (remain
== NULL
&& has_trailing_pathsep
)
14445 /* Separate the first path component in the link value and
14446 * concatenate the remainders. */
14447 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
14450 if (remain
== NULL
)
14451 remain
= vim_strsave(q
- 1);
14454 cpy
= concat_str(q
- 1, remain
);
14465 if (q
> p
&& *q
== NUL
)
14467 /* Ignore trailing path separator. */
14471 if (q
> p
&& !mch_isFullName(buf
))
14473 /* symlink is relative to directory of argument */
14474 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
14478 STRCPY(gettail(cpy
), buf
);
14486 p
= vim_strsave(buf
);
14490 if (remain
== NULL
)
14493 /* Append the first path component of "remain" to "p". */
14494 q
= getnextcomp(remain
+ 1);
14495 len
= q
- remain
- (*q
!= NUL
);
14496 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
14499 STRNCAT(cpy
, remain
, len
);
14503 /* Shorten "remain". */
14505 STRMOVE(remain
, q
- 1);
14513 /* If the result is a relative path name, make it explicitly relative to
14514 * the current directory if and only if the argument had this form. */
14515 if (!vim_ispathsep(*p
))
14517 if (is_relative_to_current
14521 || vim_ispathsep(p
[1])
14524 || vim_ispathsep(p
[2]))))))
14526 /* Prepend "./". */
14527 cpy
= concat_str((char_u
*)"./", p
);
14534 else if (!is_relative_to_current
)
14536 /* Strip leading "./". */
14538 while (q
[0] == '.' && vim_ispathsep(q
[1]))
14545 /* Ensure that the result will have no trailing path separator
14546 * if the argument had none. But keep "/" or "//". */
14547 if (!has_trailing_pathsep
)
14550 if (after_pathsep(p
, q
))
14551 *gettail_sep(p
) = NUL
;
14554 rettv
->vval
.v_string
= p
;
14557 rettv
->vval
.v_string
= vim_strsave(p
);
14561 simplify_filename(rettv
->vval
.v_string
);
14563 #ifdef HAVE_READLINK
14566 rettv
->v_type
= VAR_STRING
;
14570 * "reverse({list})" function
14573 f_reverse(argvars
, rettv
)
14578 listitem_T
*li
, *ni
;
14580 rettv
->vval
.v_number
= 0;
14581 if (argvars
[0].v_type
!= VAR_LIST
)
14582 EMSG2(_(e_listarg
), "reverse()");
14583 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14584 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
14587 l
->lv_first
= l
->lv_last
= NULL
;
14592 list_append(l
, li
);
14595 rettv
->vval
.v_list
= l
;
14596 rettv
->v_type
= VAR_LIST
;
14598 l
->lv_idx
= l
->lv_len
- l
->lv_idx
- 1;
14602 #define SP_NOMOVE 0x01 /* don't move cursor */
14603 #define SP_REPEAT 0x02 /* repeat to find outer pair */
14604 #define SP_RETCOUNT 0x04 /* return matchcount */
14605 #define SP_SETPCMARK 0x08 /* set previous context mark */
14606 #define SP_START 0x10 /* accept match at start position */
14607 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14608 #define SP_END 0x40 /* leave cursor at end of match */
14610 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
14613 * Get flags for a search function.
14614 * Possibly sets "p_ws".
14615 * Returns BACKWARD, FORWARD or zero (for an error).
14618 get_search_arg(varp
, flagsp
)
14624 char_u nbuf
[NUMBUFLEN
];
14627 if (varp
->v_type
!= VAR_UNKNOWN
)
14629 flags
= get_tv_string_buf_chk(varp
, nbuf
);
14631 return 0; /* type error; errmsg already given */
14632 while (*flags
!= NUL
)
14636 case 'b': dir
= BACKWARD
; break;
14637 case 'w': p_ws
= TRUE
; break;
14638 case 'W': p_ws
= FALSE
; break;
14640 if (flagsp
!= NULL
)
14643 case 'c': mask
= SP_START
; break;
14644 case 'e': mask
= SP_END
; break;
14645 case 'm': mask
= SP_RETCOUNT
; break;
14646 case 'n': mask
= SP_NOMOVE
; break;
14647 case 'p': mask
= SP_SUBPAT
; break;
14648 case 'r': mask
= SP_REPEAT
; break;
14649 case 's': mask
= SP_SETPCMARK
; break;
14653 EMSG2(_(e_invarg2
), flags
);
14668 * Shared by search() and searchpos() functions
14671 search_cmn(argvars
, match_pos
, flagsp
)
14680 int save_p_ws
= p_ws
;
14682 int retval
= 0; /* default: FAIL */
14683 long lnum_stop
= 0;
14685 #ifdef FEAT_RELTIME
14686 long time_limit
= 0;
14688 int options
= SEARCH_KEEP
;
14691 pat
= get_tv_string(&argvars
[0]);
14692 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
14696 if (flags
& SP_START
)
14697 options
|= SEARCH_START
;
14698 if (flags
& SP_END
)
14699 options
|= SEARCH_END
;
14701 /* Optional arguments: line number to stop searching and timeout. */
14702 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14704 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
14707 #ifdef FEAT_RELTIME
14708 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
14710 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
14711 if (time_limit
< 0)
14717 #ifdef FEAT_RELTIME
14718 /* Set the time limit, if there is one. */
14719 profile_setlimit(time_limit
, &tm
);
14723 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
14724 * Check to make sure only those flags are set.
14725 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14726 * flags cannot be set. Check for that condition also.
14728 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
14729 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14731 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
14735 pos
= save_cursor
= curwin
->w_cursor
;
14736 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14737 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
14738 if (subpatnum
!= FAIL
)
14740 if (flags
& SP_SUBPAT
)
14741 retval
= subpatnum
;
14744 if (flags
& SP_SETPCMARK
)
14746 curwin
->w_cursor
= pos
;
14747 if (match_pos
!= NULL
)
14749 /* Store the match cursor position */
14750 match_pos
->lnum
= pos
.lnum
;
14751 match_pos
->col
= pos
.col
+ 1;
14753 /* "/$" will put the cursor after the end of the line, may need to
14754 * correct that here */
14758 /* If 'n' flag is used: restore cursor position. */
14759 if (flags
& SP_NOMOVE
)
14760 curwin
->w_cursor
= save_cursor
;
14762 curwin
->w_set_curswant
= TRUE
;
14771 * "round({float})" function
14774 f_round(argvars
, rettv
)
14780 rettv
->v_type
= VAR_FLOAT
;
14781 if (get_float_arg(argvars
, &f
) == OK
)
14782 /* round() is not in C90, use ceil() or floor() instead. */
14783 rettv
->vval
.v_float
= f
> 0 ? floor(f
+ 0.5) : ceil(f
- 0.5);
14785 rettv
->vval
.v_float
= 0.0;
14790 * "search()" function
14793 f_search(argvars
, rettv
)
14799 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
14803 * "searchdecl()" function
14806 f_searchdecl(argvars
, rettv
)
14815 rettv
->vval
.v_number
= 1; /* default: FAIL */
14817 name
= get_tv_string_chk(&argvars
[0]);
14818 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14820 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
14821 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14822 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
14824 if (!error
&& name
!= NULL
)
14825 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
14826 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
14830 * Used by searchpair() and searchpairpos()
14833 searchpair_cmn(argvars
, match_pos
)
14837 char_u
*spat
, *mpat
, *epat
;
14839 int save_p_ws
= p_ws
;
14842 char_u nbuf1
[NUMBUFLEN
];
14843 char_u nbuf2
[NUMBUFLEN
];
14844 char_u nbuf3
[NUMBUFLEN
];
14845 int retval
= 0; /* default: FAIL */
14846 long lnum_stop
= 0;
14847 long time_limit
= 0;
14849 /* Get the three pattern arguments: start, middle, end. */
14850 spat
= get_tv_string_chk(&argvars
[0]);
14851 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
14852 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
14853 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
14854 goto theend
; /* type error */
14856 /* Handle the optional fourth argument: flags */
14857 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
14861 /* Don't accept SP_END or SP_SUBPAT.
14862 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14864 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
14865 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14867 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
14871 /* Using 'r' implies 'W', otherwise it doesn't work. */
14872 if (flags
& SP_REPEAT
)
14875 /* Optional fifth argument: skip expression */
14876 if (argvars
[3].v_type
== VAR_UNKNOWN
14877 || argvars
[4].v_type
== VAR_UNKNOWN
)
14878 skip
= (char_u
*)"";
14881 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
14882 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
14884 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
14887 #ifdef FEAT_RELTIME
14888 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
14890 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
14891 if (time_limit
< 0)
14898 goto theend
; /* type error */
14900 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
14901 match_pos
, lnum_stop
, time_limit
);
14910 * "searchpair()" function
14913 f_searchpair(argvars
, rettv
)
14917 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
14921 * "searchpairpos()" function
14924 f_searchpairpos(argvars
, rettv
)
14932 rettv
->vval
.v_number
= 0;
14934 if (rettv_list_alloc(rettv
) == FAIL
)
14937 if (searchpair_cmn(argvars
, &match_pos
) > 0)
14939 lnum
= match_pos
.lnum
;
14940 col
= match_pos
.col
;
14943 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14944 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14948 * Search for a start/middle/end thing.
14949 * Used by searchpair(), see its documentation for the details.
14950 * Returns 0 or -1 for no match,
14953 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
14954 lnum_stop
, time_limit
)
14955 char_u
*spat
; /* start pattern */
14956 char_u
*mpat
; /* middle pattern */
14957 char_u
*epat
; /* end pattern */
14958 int dir
; /* BACKWARD or FORWARD */
14959 char_u
*skip
; /* skip expression */
14960 int flags
; /* SP_SETPCMARK and other SP_ values */
14962 linenr_T lnum_stop
; /* stop at this line if not zero */
14963 long time_limit
; /* stop after this many msec */
14966 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
14977 int options
= SEARCH_KEEP
;
14980 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14982 p_cpo
= (char_u
*)"";
14984 #ifdef FEAT_RELTIME
14985 /* Set the time limit, if there is one. */
14986 profile_setlimit(time_limit
, &tm
);
14989 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14990 * start/middle/end (pat3, for the top pair). */
14991 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
14992 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
14993 if (pat2
== NULL
|| pat3
== NULL
)
14995 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
14997 STRCPY(pat3
, pat2
);
14999 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15001 if (flags
& SP_START
)
15002 options
|= SEARCH_START
;
15004 save_cursor
= curwin
->w_cursor
;
15005 pos
= curwin
->w_cursor
;
15006 clearpos(&firstpos
);
15007 clearpos(&foundpos
);
15011 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15012 options
, RE_SEARCH
, lnum_stop
, &tm
);
15013 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
15014 /* didn't find it or found the first match again: FAIL */
15017 if (firstpos
.lnum
== 0)
15019 if (equalpos(pos
, foundpos
))
15021 /* Found the same position again. Can happen with a pattern that
15022 * has "\zs" at the end and searching backwards. Advance one
15023 * character and try again. */
15024 if (dir
== BACKWARD
)
15031 /* clear the start flag to avoid getting stuck here */
15032 options
&= ~SEARCH_START
;
15034 /* If the skip pattern matches, ignore this match. */
15037 save_pos
= curwin
->w_cursor
;
15038 curwin
->w_cursor
= pos
;
15039 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
15040 curwin
->w_cursor
= save_pos
;
15043 /* Evaluating {skip} caused an error, break here. */
15044 curwin
->w_cursor
= save_cursor
;
15052 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
15054 /* Found end when searching backwards or start when searching
15055 * forward: nested pair. */
15057 pat
= pat2
; /* nested, don't search for middle */
15061 /* Found end when searching forward or start when searching
15062 * backward: end of (nested) pair; or found middle in outer pair. */
15064 pat
= pat3
; /* outer level, search for middle */
15069 /* Found the match: return matchcount or line number. */
15070 if (flags
& SP_RETCOUNT
)
15074 if (flags
& SP_SETPCMARK
)
15076 curwin
->w_cursor
= pos
;
15077 if (!(flags
& SP_REPEAT
))
15079 nest
= 1; /* search for next unmatched */
15083 if (match_pos
!= NULL
)
15085 /* Store the match cursor position */
15086 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
15087 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
15090 /* If 'n' flag is used or search failed: restore cursor position. */
15091 if ((flags
& SP_NOMOVE
) || retval
== 0)
15092 curwin
->w_cursor
= save_cursor
;
15103 * "searchpos()" function
15106 f_searchpos(argvars
, rettv
)
15116 rettv
->vval
.v_number
= 0;
15118 if (rettv_list_alloc(rettv
) == FAIL
)
15121 n
= search_cmn(argvars
, &match_pos
, &flags
);
15124 lnum
= match_pos
.lnum
;
15125 col
= match_pos
.col
;
15128 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15129 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15130 if (flags
& SP_SUBPAT
)
15131 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
15137 f_server2client(argvars
, rettv
)
15141 #ifdef FEAT_CLIENTSERVER
15142 char_u buf
[NUMBUFLEN
];
15143 char_u
*server
= get_tv_string_chk(&argvars
[0]);
15144 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
15146 rettv
->vval
.v_number
= -1;
15147 if (server
== NULL
|| reply
== NULL
)
15149 if (check_restricted() || check_secure())
15152 if (check_connection() == FAIL
)
15156 if (serverSendReply(server
, reply
) < 0)
15158 EMSG(_("E258: Unable to send to client"));
15161 rettv
->vval
.v_number
= 0;
15163 rettv
->vval
.v_number
= -1;
15169 f_serverlist(argvars
, rettv
)
15175 #ifdef FEAT_CLIENTSERVER
15177 r
= serverGetVimNames();
15180 if (X_DISPLAY
!= NULL
)
15181 r
= serverGetVimNames(X_DISPLAY
);
15184 rettv
->v_type
= VAR_STRING
;
15185 rettv
->vval
.v_string
= r
;
15189 * "setbufvar()" function
15193 f_setbufvar(argvars
, rettv
)
15199 char_u
*varname
, *bufvarname
;
15201 char_u nbuf
[NUMBUFLEN
];
15203 rettv
->vval
.v_number
= 0;
15205 if (check_restricted() || check_secure())
15207 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
15208 varname
= get_tv_string_chk(&argvars
[1]);
15209 buf
= get_buf_tv(&argvars
[0]);
15210 varp
= &argvars
[2];
15212 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15214 /* set curbuf to be our buf, temporarily */
15215 aucmd_prepbuf(&aco
, buf
);
15217 if (*varname
== '&')
15224 numval
= get_tv_number_chk(varp
, &error
);
15225 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15226 if (!error
&& strval
!= NULL
)
15227 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15231 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15232 if (bufvarname
!= NULL
)
15234 STRCPY(bufvarname
, "b:");
15235 STRCPY(bufvarname
+ 2, varname
);
15236 set_var(bufvarname
, varp
, TRUE
);
15237 vim_free(bufvarname
);
15241 /* reset notion of buffer */
15242 aucmd_restbuf(&aco
);
15247 * "setcmdpos()" function
15250 f_setcmdpos(argvars
, rettv
)
15254 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
15257 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
15261 * "setline()" function
15264 f_setline(argvars
, rettv
)
15269 char_u
*line
= NULL
;
15271 listitem_T
*li
= NULL
;
15273 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
15275 lnum
= get_tv_lnum(&argvars
[0]);
15276 if (argvars
[1].v_type
== VAR_LIST
)
15278 l
= argvars
[1].vval
.v_list
;
15282 line
= get_tv_string_chk(&argvars
[1]);
15284 rettv
->vval
.v_number
= 0; /* OK */
15289 /* list argument, get next string */
15292 line
= get_tv_string_chk(&li
->li_tv
);
15296 rettv
->vval
.v_number
= 1; /* FAIL */
15297 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
15299 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
15301 /* existing line, replace it */
15302 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
15304 changed_bytes(lnum
, 0);
15305 if (lnum
== curwin
->w_cursor
.lnum
)
15306 check_cursor_col();
15307 rettv
->vval
.v_number
= 0; /* OK */
15310 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
15312 /* lnum is one past the last line, append the line */
15314 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
15315 rettv
->vval
.v_number
= 0; /* OK */
15318 if (l
== NULL
) /* only one string argument */
15324 appended_lines_mark(lcount
, added
);
15327 static void set_qf_ll_list
__ARGS((win_T
*wp
, typval_T
*list_arg
, typval_T
*action_arg
, typval_T
*rettv
));
15330 * Used by "setqflist()" and "setloclist()" functions
15334 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
15336 typval_T
*list_arg
;
15337 typval_T
*action_arg
;
15340 #ifdef FEAT_QUICKFIX
15345 rettv
->vval
.v_number
= -1;
15347 #ifdef FEAT_QUICKFIX
15348 if (list_arg
->v_type
!= VAR_LIST
)
15349 EMSG(_(e_listreq
));
15352 list_T
*l
= list_arg
->vval
.v_list
;
15354 if (action_arg
->v_type
== VAR_STRING
)
15356 act
= get_tv_string_chk(action_arg
);
15358 return; /* type error; errmsg already given */
15359 if (*act
== 'a' || *act
== 'r')
15363 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
15364 rettv
->vval
.v_number
= 0;
15370 * "setloclist()" function
15374 f_setloclist(argvars
, rettv
)
15380 rettv
->vval
.v_number
= -1;
15382 win
= find_win_by_nr(&argvars
[0], NULL
);
15384 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
15388 * "setmatches()" function
15391 f_setmatches(argvars
, rettv
)
15395 #ifdef FEAT_SEARCH_EXTRA
15400 rettv
->vval
.v_number
= -1;
15401 if (argvars
[0].v_type
!= VAR_LIST
)
15403 EMSG(_(e_listreq
));
15406 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
15409 /* To some extent make sure that we are dealing with a list from
15410 * "getmatches()". */
15414 if (li
->li_tv
.v_type
!= VAR_DICT
15415 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
15420 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
15421 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
15422 && dict_find(d
, (char_u
*)"priority", -1) != NULL
15423 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
15431 clear_matches(curwin
);
15435 d
= li
->li_tv
.vval
.v_dict
;
15436 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
15437 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
15438 (int)get_dict_number(d
, (char_u
*)"priority"),
15439 (int)get_dict_number(d
, (char_u
*)"id"));
15442 rettv
->vval
.v_number
= 0;
15448 * "setpos()" function
15452 f_setpos(argvars
, rettv
)
15460 rettv
->vval
.v_number
= -1;
15461 name
= get_tv_string_chk(argvars
);
15464 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
15467 if (name
[0] == '.' && name
[1] == NUL
)
15470 if (fnum
== curbuf
->b_fnum
)
15472 curwin
->w_cursor
= pos
;
15474 rettv
->vval
.v_number
= 0;
15479 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
15482 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
15483 rettv
->vval
.v_number
= 0;
15492 * "setqflist()" function
15496 f_setqflist(argvars
, rettv
)
15500 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
15504 * "setreg()" function
15507 f_setreg(argvars
, rettv
)
15512 char_u
*strregname
;
15523 strregname
= get_tv_string_chk(argvars
);
15524 rettv
->vval
.v_number
= 1; /* FAIL is default */
15526 if (strregname
== NULL
)
15527 return; /* type error; errmsg already given */
15528 regname
= *strregname
;
15529 if (regname
== 0 || regname
== '@')
15531 else if (regname
== '=')
15534 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15536 stropt
= get_tv_string_chk(&argvars
[2]);
15537 if (stropt
== NULL
)
15538 return; /* type error */
15539 for (; *stropt
!= NUL
; ++stropt
)
15542 case 'a': case 'A': /* append */
15545 case 'v': case 'c': /* character-wise selection */
15548 case 'V': case 'l': /* line-wise selection */
15552 case 'b': case Ctrl_V
: /* block-wise selection */
15553 yank_type
= MBLOCK
;
15554 if (VIM_ISDIGIT(stropt
[1]))
15557 block_len
= getdigits(&stropt
) - 1;
15565 strval
= get_tv_string_chk(&argvars
[1]);
15566 if (strval
!= NULL
)
15567 write_reg_contents_ex(regname
, strval
, -1,
15568 append
, yank_type
, block_len
);
15569 rettv
->vval
.v_number
= 0;
15573 * "settabwinvar()" function
15576 f_settabwinvar(argvars
, rettv
)
15580 setwinvar(argvars
, rettv
, 1);
15584 * "setwinvar()" function
15587 f_setwinvar(argvars
, rettv
)
15591 setwinvar(argvars
, rettv
, 0);
15595 * "setwinvar()" and "settabwinvar()" functions
15598 setwinvar(argvars
, rettv
, off
)
15604 #ifdef FEAT_WINDOWS
15605 win_T
*save_curwin
;
15606 tabpage_T
*save_curtab
;
15608 char_u
*varname
, *winvarname
;
15610 char_u nbuf
[NUMBUFLEN
];
15613 rettv
->vval
.v_number
= 0;
15615 if (check_restricted() || check_secure())
15618 #ifdef FEAT_WINDOWS
15620 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
15624 win
= find_win_by_nr(&argvars
[off
], tp
);
15625 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
15626 varp
= &argvars
[off
+ 2];
15628 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15630 #ifdef FEAT_WINDOWS
15631 /* set curwin to be our win, temporarily */
15632 save_curwin
= curwin
;
15633 save_curtab
= curtab
;
15634 goto_tabpage_tp(tp
);
15635 if (!win_valid(win
))
15638 curbuf
= curwin
->w_buffer
;
15641 if (*varname
== '&')
15648 numval
= get_tv_number_chk(varp
, &error
);
15649 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15650 if (!error
&& strval
!= NULL
)
15651 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15655 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15656 if (winvarname
!= NULL
)
15658 STRCPY(winvarname
, "w:");
15659 STRCPY(winvarname
+ 2, varname
);
15660 set_var(winvarname
, varp
, TRUE
);
15661 vim_free(winvarname
);
15665 #ifdef FEAT_WINDOWS
15666 /* Restore current tabpage and window, if still valid (autocomands can
15667 * make them invalid). */
15668 if (valid_tabpage(save_curtab
))
15669 goto_tabpage_tp(save_curtab
);
15670 if (win_valid(save_curwin
))
15672 curwin
= save_curwin
;
15673 curbuf
= curwin
->w_buffer
;
15680 * "shellescape({string})" function
15683 f_shellescape(argvars
, rettv
)
15687 rettv
->vval
.v_string
= vim_strsave_shellescape(get_tv_string(&argvars
[0]));
15688 rettv
->v_type
= VAR_STRING
;
15692 * "simplify()" function
15695 f_simplify(argvars
, rettv
)
15701 p
= get_tv_string(&argvars
[0]);
15702 rettv
->vval
.v_string
= vim_strsave(p
);
15703 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
15704 rettv
->v_type
= VAR_STRING
;
15712 f_sin(argvars
, rettv
)
15718 rettv
->v_type
= VAR_FLOAT
;
15719 if (get_float_arg(argvars
, &f
) == OK
)
15720 rettv
->vval
.v_float
= sin(f
);
15722 rettv
->vval
.v_float
= 0.0;
15727 #ifdef __BORLANDC__
15730 item_compare
__ARGS((const void *s1
, const void *s2
));
15732 #ifdef __BORLANDC__
15735 item_compare2
__ARGS((const void *s1
, const void *s2
));
15737 static int item_compare_ic
;
15738 static char_u
*item_compare_func
;
15739 static int item_compare_func_err
;
15740 #define ITEM_COMPARE_FAIL 999
15743 * Compare functions for f_sort() below.
15746 #ifdef __BORLANDC__
15749 item_compare(s1
, s2
)
15754 char_u
*tofree1
, *tofree2
;
15756 char_u numbuf1
[NUMBUFLEN
];
15757 char_u numbuf2
[NUMBUFLEN
];
15759 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
15760 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
15765 if (item_compare_ic
)
15766 res
= STRICMP(p1
, p2
);
15768 res
= STRCMP(p1
, p2
);
15775 #ifdef __BORLANDC__
15778 item_compare2(s1
, s2
)
15787 /* shortcut after failure in previous call; compare all items equal */
15788 if (item_compare_func_err
)
15791 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15792 * in the copy without changing the original list items. */
15793 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
15794 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
15796 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
15797 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
15798 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
15799 clear_tv(&argv
[0]);
15800 clear_tv(&argv
[1]);
15803 res
= ITEM_COMPARE_FAIL
;
15805 /* return value has wrong type */
15806 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
15807 if (item_compare_func_err
)
15808 res
= ITEM_COMPARE_FAIL
;
15814 * "sort({list})" function
15817 f_sort(argvars
, rettv
)
15827 rettv
->vval
.v_number
= 0;
15828 if (argvars
[0].v_type
!= VAR_LIST
)
15829 EMSG2(_(e_listarg
), "sort()");
15832 l
= argvars
[0].vval
.v_list
;
15833 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
15835 rettv
->vval
.v_list
= l
;
15836 rettv
->v_type
= VAR_LIST
;
15841 return; /* short list sorts pretty quickly */
15843 item_compare_ic
= FALSE
;
15844 item_compare_func
= NULL
;
15845 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15847 if (argvars
[1].v_type
== VAR_FUNC
)
15848 item_compare_func
= argvars
[1].vval
.v_string
;
15853 i
= get_tv_number_chk(&argvars
[1], &error
);
15855 return; /* type error; errmsg already given */
15857 item_compare_ic
= TRUE
;
15859 item_compare_func
= get_tv_string(&argvars
[1]);
15863 /* Make an array with each entry pointing to an item in the List. */
15864 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
15868 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
15871 item_compare_func_err
= FALSE
;
15872 /* test the compare function */
15873 if (item_compare_func
!= NULL
15874 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
15875 == ITEM_COMPARE_FAIL
)
15876 EMSG(_("E702: Sort compare function failed"));
15879 /* Sort the array with item pointers. */
15880 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
15881 item_compare_func
== NULL
? item_compare
: item_compare2
);
15883 if (!item_compare_func_err
)
15885 /* Clear the List and append the items in the sorted order. */
15886 l
->lv_first
= l
->lv_last
= l
->lv_idx_item
= NULL
;
15888 for (i
= 0; i
< len
; ++i
)
15889 list_append(l
, ptrs
[i
]);
15898 * "soundfold({word})" function
15901 f_soundfold(argvars
, rettv
)
15907 rettv
->v_type
= VAR_STRING
;
15908 s
= get_tv_string(&argvars
[0]);
15910 rettv
->vval
.v_string
= eval_soundfold(s
);
15912 rettv
->vval
.v_string
= vim_strsave(s
);
15917 * "spellbadword()" function
15921 f_spellbadword(argvars
, rettv
)
15925 char_u
*word
= (char_u
*)"";
15926 hlf_T attr
= HLF_COUNT
;
15929 if (rettv_list_alloc(rettv
) == FAIL
)
15933 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15935 /* Find the start and length of the badly spelled word. */
15936 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
15938 word
= ml_get_cursor();
15940 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15942 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15947 /* Check the argument for spelling. */
15948 while (*str
!= NUL
)
15950 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
15951 if (attr
!= HLF_COUNT
)
15962 list_append_string(rettv
->vval
.v_list
, word
, len
);
15963 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
15964 attr
== HLF_SPB
? "bad" :
15965 attr
== HLF_SPR
? "rare" :
15966 attr
== HLF_SPL
? "local" :
15967 attr
== HLF_SPC
? "caps" :
15972 * "spellsuggest()" function
15976 f_spellsuggest(argvars
, rettv
)
15982 int typeerr
= FALSE
;
15987 int need_capital
= FALSE
;
15990 if (rettv_list_alloc(rettv
) == FAIL
)
15994 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15996 str
= get_tv_string(&argvars
[0]);
15997 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15999 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
16002 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16004 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
16012 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
16014 for (i
= 0; i
< ga
.ga_len
; ++i
)
16016 str
= ((char_u
**)ga
.ga_data
)[i
];
16018 li
= listitem_alloc();
16023 li
->li_tv
.v_type
= VAR_STRING
;
16024 li
->li_tv
.v_lock
= 0;
16025 li
->li_tv
.vval
.v_string
= str
;
16026 list_append(rettv
->vval
.v_list
, li
);
16035 f_split(argvars
, rettv
)
16041 char_u
*pat
= NULL
;
16042 regmatch_T regmatch
;
16043 char_u patbuf
[NUMBUFLEN
];
16047 int keepempty
= FALSE
;
16048 int typeerr
= FALSE
;
16050 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16052 p_cpo
= (char_u
*)"";
16054 str
= get_tv_string(&argvars
[0]);
16055 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16057 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16060 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16061 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
16063 if (pat
== NULL
|| *pat
== NUL
)
16064 pat
= (char_u
*)"[\\x01- ]\\+";
16066 if (rettv_list_alloc(rettv
) == FAIL
)
16071 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
16072 if (regmatch
.regprog
!= NULL
)
16074 regmatch
.rm_ic
= FALSE
;
16075 while (*str
!= NUL
|| keepempty
)
16078 match
= FALSE
; /* empty item at the end */
16080 match
= vim_regexec_nl(®match
, str
, col
);
16082 end
= regmatch
.startp
[0];
16084 end
= str
+ STRLEN(str
);
16085 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
16086 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
16088 if (list_append_string(rettv
->vval
.v_list
, str
,
16089 (int)(end
- str
)) == FAIL
)
16094 /* Advance to just after the match. */
16095 if (regmatch
.endp
[0] > str
)
16099 /* Don't get stuck at the same match. */
16101 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
16106 str
= regmatch
.endp
[0];
16109 vim_free(regmatch
.regprog
);
16117 * "sqrt()" function
16120 f_sqrt(argvars
, rettv
)
16126 rettv
->v_type
= VAR_FLOAT
;
16127 if (get_float_arg(argvars
, &f
) == OK
)
16128 rettv
->vval
.v_float
= sqrt(f
);
16130 rettv
->vval
.v_float
= 0.0;
16134 * "str2float()" function
16137 f_str2float(argvars
, rettv
)
16141 char_u
*p
= skipwhite(get_tv_string(&argvars
[0]));
16144 p
= skipwhite(p
+ 1);
16145 (void)string2float(p
, &rettv
->vval
.v_float
);
16146 rettv
->v_type
= VAR_FLOAT
;
16151 * "str2nr()" function
16154 f_str2nr(argvars
, rettv
)
16162 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16164 base
= get_tv_number(&argvars
[1]);
16165 if (base
!= 8 && base
!= 10 && base
!= 16)
16172 p
= skipwhite(get_tv_string(&argvars
[0]));
16174 p
= skipwhite(p
+ 1);
16175 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
16176 rettv
->vval
.v_number
= n
;
16179 #ifdef HAVE_STRFTIME
16181 * "strftime({format}[, {time}])" function
16184 f_strftime(argvars
, rettv
)
16188 char_u result_buf
[256];
16189 struct tm
*curtime
;
16193 rettv
->v_type
= VAR_STRING
;
16195 p
= get_tv_string(&argvars
[0]);
16196 if (argvars
[1].v_type
== VAR_UNKNOWN
)
16197 seconds
= time(NULL
);
16199 seconds
= (time_t)get_tv_number(&argvars
[1]);
16200 curtime
= localtime(&seconds
);
16201 /* MSVC returns NULL for an invalid value of seconds. */
16202 if (curtime
== NULL
)
16203 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
16210 conv
.vc_type
= CONV_NONE
;
16211 enc
= enc_locale();
16212 convert_setup(&conv
, p_enc
, enc
);
16213 if (conv
.vc_type
!= CONV_NONE
)
16214 p
= string_convert(&conv
, p
, NULL
);
16217 (void)strftime((char *)result_buf
, sizeof(result_buf
),
16218 (char *)p
, curtime
);
16220 result_buf
[0] = NUL
;
16223 if (conv
.vc_type
!= CONV_NONE
)
16225 convert_setup(&conv
, enc
, p_enc
);
16226 if (conv
.vc_type
!= CONV_NONE
)
16227 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
16230 rettv
->vval
.v_string
= vim_strsave(result_buf
);
16233 /* Release conversion descriptors */
16234 convert_setup(&conv
, NULL
, NULL
);
16242 * "stridx()" function
16245 f_stridx(argvars
, rettv
)
16249 char_u buf
[NUMBUFLEN
];
16252 char_u
*save_haystack
;
16256 needle
= get_tv_string_chk(&argvars
[1]);
16257 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16258 rettv
->vval
.v_number
= -1;
16259 if (needle
== NULL
|| haystack
== NULL
)
16260 return; /* type error; errmsg already given */
16262 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16266 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
16267 if (error
|| start_idx
>= (int)STRLEN(haystack
))
16269 if (start_idx
>= 0)
16270 haystack
+= start_idx
;
16273 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
16275 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
16279 * "string()" function
16282 f_string(argvars
, rettv
)
16287 char_u numbuf
[NUMBUFLEN
];
16289 rettv
->v_type
= VAR_STRING
;
16290 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
16291 /* Make a copy if we have a value but it's not in allocated memory. */
16292 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
16293 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
16297 * "strlen()" function
16300 f_strlen(argvars
, rettv
)
16304 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
16305 get_tv_string(&argvars
[0])));
16309 * "strpart()" function
16312 f_strpart(argvars
, rettv
)
16322 p
= get_tv_string(&argvars
[0]);
16323 slen
= (int)STRLEN(p
);
16325 n
= get_tv_number_chk(&argvars
[1], &error
);
16328 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16329 len
= get_tv_number(&argvars
[2]);
16331 len
= slen
- n
; /* default len: all bytes that are available. */
16334 * Only return the overlap between the specified part and the actual
16346 else if (n
+ len
> slen
)
16349 rettv
->v_type
= VAR_STRING
;
16350 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
16354 * "strridx()" function
16357 f_strridx(argvars
, rettv
)
16361 char_u buf
[NUMBUFLEN
];
16365 char_u
*lastmatch
= NULL
;
16366 int haystack_len
, end_idx
;
16368 needle
= get_tv_string_chk(&argvars
[1]);
16369 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16371 rettv
->vval
.v_number
= -1;
16372 if (needle
== NULL
|| haystack
== NULL
)
16373 return; /* type error; errmsg already given */
16375 haystack_len
= (int)STRLEN(haystack
);
16376 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16378 /* Third argument: upper limit for index */
16379 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
16381 return; /* can never find a match */
16384 end_idx
= haystack_len
;
16386 if (*needle
== NUL
)
16388 /* Empty string matches past the end. */
16389 lastmatch
= haystack
+ end_idx
;
16393 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
16395 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
16396 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
16402 if (lastmatch
== NULL
)
16403 rettv
->vval
.v_number
= -1;
16405 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
16409 * "strtrans()" function
16412 f_strtrans(argvars
, rettv
)
16416 rettv
->v_type
= VAR_STRING
;
16417 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
16421 * "submatch()" function
16424 f_submatch(argvars
, rettv
)
16428 rettv
->v_type
= VAR_STRING
;
16429 rettv
->vval
.v_string
=
16430 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
16434 * "substitute()" function
16437 f_substitute(argvars
, rettv
)
16441 char_u patbuf
[NUMBUFLEN
];
16442 char_u subbuf
[NUMBUFLEN
];
16443 char_u flagsbuf
[NUMBUFLEN
];
16445 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16446 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16447 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
16448 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
16450 rettv
->v_type
= VAR_STRING
;
16451 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
16452 rettv
->vval
.v_string
= NULL
;
16454 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
16458 * "synID(lnum, col, trans)" function
16462 f_synID(argvars
, rettv
)
16471 int transerr
= FALSE
;
16473 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16474 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16475 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
16477 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16478 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
16479 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
16482 rettv
->vval
.v_number
= id
;
16486 * "synIDattr(id, what [, mode])" function
16490 f_synIDattr(argvars
, rettv
)
16499 char_u modebuf
[NUMBUFLEN
];
16502 id
= get_tv_number(&argvars
[0]);
16503 what
= get_tv_string(&argvars
[1]);
16504 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16506 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
16507 modec
= TOLOWER_ASC(mode
[0]);
16508 if (modec
!= 't' && modec
!= 'c'
16513 modec
= 0; /* replace invalid with current */
16529 switch (TOLOWER_ASC(what
[0]))
16532 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
16533 p
= highlight_color(id
, what
, modec
);
16535 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
16538 case 'f': /* fg[#] */
16539 p
= highlight_color(id
, what
, modec
);
16543 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
16544 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16546 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
16549 case 'n': /* name */
16550 p
= get_highlight_name(NULL
, id
- 1);
16553 case 'r': /* reverse */
16554 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16557 case 's': /* standout */
16558 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
16562 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
16564 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
16567 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
16572 p
= vim_strsave(p
);
16574 rettv
->v_type
= VAR_STRING
;
16575 rettv
->vval
.v_string
= p
;
16579 * "synIDtrans(id)" function
16583 f_synIDtrans(argvars
, rettv
)
16590 id
= get_tv_number(&argvars
[0]);
16593 id
= syn_get_final_id(id
);
16598 rettv
->vval
.v_number
= id
;
16602 * "synstack(lnum, col)" function
16606 f_synstack(argvars
, rettv
)
16617 rettv
->v_type
= VAR_LIST
;
16618 rettv
->vval
.v_list
= NULL
;
16621 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16622 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16624 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16625 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
))
16626 && rettv_list_alloc(rettv
) != FAIL
)
16628 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
16631 id
= syn_get_stack_item(i
);
16634 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
16642 * "system()" function
16645 f_system(argvars
, rettv
)
16649 char_u
*res
= NULL
;
16651 char_u
*infile
= NULL
;
16652 char_u buf
[NUMBUFLEN
];
16656 if (check_restricted() || check_secure())
16659 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16662 * Write the string to a temp file, to be used for input of the shell
16665 if ((infile
= vim_tempname('i')) == NULL
)
16671 fd
= mch_fopen((char *)infile
, WRITEBIN
);
16674 EMSG2(_(e_notopen
), infile
);
16677 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
16681 goto done
; /* type error; errmsg already given */
16683 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
16685 if (fclose(fd
) != 0)
16689 EMSG(_("E677: Error writing temp file"));
16694 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
16695 SHELL_SILENT
| SHELL_COOKED
);
16698 /* translate <CR> into <NL> */
16703 for (s
= res
; *s
; ++s
)
16711 /* translate <CR><NL> into <NL> */
16717 for (s
= res
; *s
; ++s
)
16719 if (s
[0] == CAR
&& s
[1] == NL
)
16729 if (infile
!= NULL
)
16731 mch_remove(infile
);
16734 rettv
->v_type
= VAR_STRING
;
16735 rettv
->vval
.v_string
= res
;
16739 * "tabpagebuflist()" function
16743 f_tabpagebuflist(argvars
, rettv
)
16747 #ifndef FEAT_WINDOWS
16748 rettv
->vval
.v_number
= 0;
16753 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16757 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16759 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16762 rettv
->vval
.v_number
= 0;
16765 if (rettv_list_alloc(rettv
) == FAIL
)
16766 rettv
->vval
.v_number
= 0;
16769 for (; wp
!= NULL
; wp
= wp
->w_next
)
16770 if (list_append_number(rettv
->vval
.v_list
,
16771 wp
->w_buffer
->b_fnum
) == FAIL
)
16780 * "tabpagenr()" function
16784 f_tabpagenr(argvars
, rettv
)
16789 #ifdef FEAT_WINDOWS
16792 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
16794 arg
= get_tv_string_chk(&argvars
[0]);
16798 if (STRCMP(arg
, "$") == 0)
16799 nr
= tabpage_index(NULL
) - 1;
16801 EMSG2(_(e_invexpr2
), arg
);
16805 nr
= tabpage_index(curtab
);
16807 rettv
->vval
.v_number
= nr
;
16811 #ifdef FEAT_WINDOWS
16812 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
16815 * Common code for tabpagewinnr() and winnr().
16818 get_winnr(tp
, argvar
)
16827 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
16828 if (argvar
->v_type
!= VAR_UNKNOWN
)
16830 arg
= get_tv_string_chk(argvar
);
16832 nr
= 0; /* type error; errmsg already given */
16833 else if (STRCMP(arg
, "$") == 0)
16834 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
16835 else if (STRCMP(arg
, "#") == 0)
16837 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
16843 EMSG2(_(e_invexpr2
), arg
);
16849 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16850 wp
!= twin
; wp
= wp
->w_next
)
16854 /* didn't find it in this tabpage */
16865 * "tabpagewinnr()" function
16869 f_tabpagewinnr(argvars
, rettv
)
16874 #ifdef FEAT_WINDOWS
16877 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16881 nr
= get_winnr(tp
, &argvars
[1]);
16883 rettv
->vval
.v_number
= nr
;
16888 * "tagfiles()" function
16892 f_tagfiles(argvars
, rettv
)
16896 char_u fname
[MAXPATHL
+ 1];
16900 if (rettv_list_alloc(rettv
) == FAIL
)
16902 rettv
->vval
.v_number
= 0;
16906 for (first
= TRUE
; ; first
= FALSE
)
16907 if (get_tagfname(&tn
, first
, fname
) == FAIL
16908 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
16914 * "taglist()" function
16917 f_taglist(argvars
, rettv
)
16921 char_u
*tag_pattern
;
16923 tag_pattern
= get_tv_string(&argvars
[0]);
16925 rettv
->vval
.v_number
= FALSE
;
16926 if (*tag_pattern
== NUL
)
16929 if (rettv_list_alloc(rettv
) == OK
)
16930 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
16934 * "tempname()" function
16938 f_tempname(argvars
, rettv
)
16942 static int x
= 'A';
16944 rettv
->v_type
= VAR_STRING
;
16945 rettv
->vval
.v_string
= vim_tempname(x
);
16947 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16948 * names. Skip 'I' and 'O', they are used for shell redirection. */
16966 } while (x
== 'I' || x
== 'O');
16970 * "test(list)" function: Just checking the walls...
16974 f_test(argvars
, rettv
)
16978 /* Used for unit testing. Change the code below to your liking. */
16982 char_u
*bad
, *good
;
16984 if (argvars
[0].v_type
!= VAR_LIST
)
16986 l
= argvars
[0].vval
.v_list
;
16992 bad
= get_tv_string(&li
->li_tv
);
16996 good
= get_tv_string(&li
->li_tv
);
16997 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
17002 * "tolower(string)" function
17005 f_tolower(argvars
, rettv
)
17011 p
= vim_strsave(get_tv_string(&argvars
[0]));
17012 rettv
->v_type
= VAR_STRING
;
17013 rettv
->vval
.v_string
= p
;
17025 c
= utf_ptr2char(p
);
17026 lc
= utf_tolower(c
);
17027 l
= utf_ptr2len(p
);
17028 /* TODO: reallocate string when byte count changes. */
17029 if (utf_char2len(lc
) == l
)
17030 utf_char2bytes(lc
, p
);
17033 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
17034 p
+= l
; /* skip multi-byte character */
17038 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
17045 * "toupper(string)" function
17048 f_toupper(argvars
, rettv
)
17052 rettv
->v_type
= VAR_STRING
;
17053 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
17057 * "tr(string, fromstr, tostr)" function
17060 f_tr(argvars
, rettv
)
17077 char_u buf
[NUMBUFLEN
];
17078 char_u buf2
[NUMBUFLEN
];
17081 instr
= get_tv_string(&argvars
[0]);
17082 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
17083 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
17085 /* Default return value: empty string. */
17086 rettv
->v_type
= VAR_STRING
;
17087 rettv
->vval
.v_string
= NULL
;
17088 if (fromstr
== NULL
|| tostr
== NULL
)
17089 return; /* type error; errmsg already given */
17090 ga_init2(&ga
, (int)sizeof(char), 80);
17095 /* not multi-byte: fromstr and tostr must be the same length */
17096 if (STRLEN(fromstr
) != STRLEN(tostr
))
17101 EMSG2(_(e_invarg2
), fromstr
);
17106 /* fromstr and tostr have to contain the same number of chars */
17107 while (*instr
!= NUL
)
17112 inlen
= (*mb_ptr2len
)(instr
);
17116 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
17118 fromlen
= (*mb_ptr2len
)(p
);
17119 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
17121 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17123 tolen
= (*mb_ptr2len
)(p
);
17131 if (*p
== NUL
) /* tostr is shorter than fromstr */
17138 if (first
&& cpstr
== instr
)
17140 /* Check that fromstr and tostr have the same number of
17141 * (multi-byte) characters. Done only once when a character
17142 * of instr doesn't appear in fromstr. */
17144 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17146 tolen
= (*mb_ptr2len
)(p
);
17153 ga_grow(&ga
, cplen
);
17154 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
17155 ga
.ga_len
+= cplen
;
17162 /* When not using multi-byte chars we can do it faster. */
17163 p
= vim_strchr(fromstr
, *instr
);
17165 ga_append(&ga
, tostr
[p
- fromstr
]);
17167 ga_append(&ga
, *instr
);
17172 /* add a terminating NUL */
17174 ga_append(&ga
, NUL
);
17176 rettv
->vval
.v_string
= ga
.ga_data
;
17181 * "trunc({float})" function
17184 f_trunc(argvars
, rettv
)
17190 rettv
->v_type
= VAR_FLOAT
;
17191 if (get_float_arg(argvars
, &f
) == OK
)
17192 /* trunc() is not in C90, use floor() or ceil() instead. */
17193 rettv
->vval
.v_float
= f
> 0 ? floor(f
) : ceil(f
);
17195 rettv
->vval
.v_float
= 0.0;
17200 * "type(expr)" function
17203 f_type(argvars
, rettv
)
17209 switch (argvars
[0].v_type
)
17211 case VAR_NUMBER
: n
= 0; break;
17212 case VAR_STRING
: n
= 1; break;
17213 case VAR_FUNC
: n
= 2; break;
17214 case VAR_LIST
: n
= 3; break;
17215 case VAR_DICT
: n
= 4; break;
17217 case VAR_FLOAT
: n
= 5; break;
17219 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
17221 rettv
->vval
.v_number
= n
;
17225 * "values(dict)" function
17228 f_values(argvars
, rettv
)
17232 dict_list(argvars
, rettv
, 1);
17236 * "virtcol(string)" function
17239 f_virtcol(argvars
, rettv
)
17245 int fnum
= curbuf
->b_fnum
;
17247 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
17248 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
17249 && fnum
== curbuf
->b_fnum
)
17251 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
17255 rettv
->vval
.v_number
= vcol
;
17259 * "visualmode()" function
17263 f_visualmode(argvars
, rettv
)
17270 rettv
->v_type
= VAR_STRING
;
17271 str
[0] = curbuf
->b_visual_mode_eval
;
17273 rettv
->vval
.v_string
= vim_strsave(str
);
17275 /* A non-zero number or non-empty string argument: reset mode. */
17276 if ((argvars
[0].v_type
== VAR_NUMBER
&& argvars
[0].vval
.v_number
!= 0)
17277 || (argvars
[0].v_type
== VAR_STRING
17278 && *get_tv_string(&argvars
[0]) != NUL
))
17279 curbuf
->b_visual_mode_eval
= NUL
;
17281 rettv
->vval
.v_number
= 0; /* return anything, it won't work anyway */
17286 * "winbufnr(nr)" function
17289 f_winbufnr(argvars
, rettv
)
17295 wp
= find_win_by_nr(&argvars
[0], NULL
);
17297 rettv
->vval
.v_number
= -1;
17299 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
17303 * "wincol()" function
17307 f_wincol(argvars
, rettv
)
17312 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
17316 * "winheight(nr)" function
17319 f_winheight(argvars
, rettv
)
17325 wp
= find_win_by_nr(&argvars
[0], NULL
);
17327 rettv
->vval
.v_number
= -1;
17329 rettv
->vval
.v_number
= wp
->w_height
;
17333 * "winline()" function
17337 f_winline(argvars
, rettv
)
17342 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
17346 * "winnr()" function
17350 f_winnr(argvars
, rettv
)
17356 #ifdef FEAT_WINDOWS
17357 nr
= get_winnr(curtab
, &argvars
[0]);
17359 rettv
->vval
.v_number
= nr
;
17363 * "winrestcmd()" function
17367 f_winrestcmd(argvars
, rettv
)
17371 #ifdef FEAT_WINDOWS
17377 ga_init2(&ga
, (int)sizeof(char), 70);
17378 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
17380 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
17381 ga_concat(&ga
, buf
);
17382 # ifdef FEAT_VERTSPLIT
17383 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
17384 ga_concat(&ga
, buf
);
17388 ga_append(&ga
, NUL
);
17390 rettv
->vval
.v_string
= ga
.ga_data
;
17392 rettv
->vval
.v_string
= NULL
;
17394 rettv
->v_type
= VAR_STRING
;
17398 * "winrestview()" function
17402 f_winrestview(argvars
, rettv
)
17408 if (argvars
[0].v_type
!= VAR_DICT
17409 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
17413 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
17414 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
17415 #ifdef FEAT_VIRTUALEDIT
17416 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
17418 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
17419 curwin
->w_set_curswant
= FALSE
;
17421 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
17423 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
17425 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
17426 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
17429 changed_cline_bef_curs();
17430 invalidate_botline();
17431 redraw_later(VALID
);
17433 if (curwin
->w_topline
== 0)
17434 curwin
->w_topline
= 1;
17435 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
17436 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
17438 check_topfill(curwin
, TRUE
);
17444 * "winsaveview()" function
17448 f_winsaveview(argvars
, rettv
)
17454 dict
= dict_alloc();
17457 rettv
->v_type
= VAR_DICT
;
17458 rettv
->vval
.v_dict
= dict
;
17459 ++dict
->dv_refcount
;
17461 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
17462 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
17463 #ifdef FEAT_VIRTUALEDIT
17464 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
17467 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
17469 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
17471 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
17473 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
17474 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
17478 * "winwidth(nr)" function
17481 f_winwidth(argvars
, rettv
)
17487 wp
= find_win_by_nr(&argvars
[0], NULL
);
17489 rettv
->vval
.v_number
= -1;
17491 #ifdef FEAT_VERTSPLIT
17492 rettv
->vval
.v_number
= wp
->w_width
;
17494 rettv
->vval
.v_number
= Columns
;
17499 * "writefile()" function
17502 f_writefile(argvars
, rettv
)
17506 int binary
= FALSE
;
17514 if (check_restricted() || check_secure())
17517 if (argvars
[0].v_type
!= VAR_LIST
)
17519 EMSG2(_(e_listarg
), "writefile()");
17522 if (argvars
[0].vval
.v_list
== NULL
)
17525 if (argvars
[2].v_type
!= VAR_UNKNOWN
17526 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
17529 /* Always open the file in binary mode, library functions have a mind of
17530 * their own about CR-LF conversion. */
17531 fname
= get_tv_string(&argvars
[1]);
17532 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
17534 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
17539 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
17542 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
17554 if (!binary
|| li
->li_next
!= NULL
)
17555 if (putc('\n', fd
) == EOF
)
17569 rettv
->vval
.v_number
= ret
;
17573 * Translate a String variable into a position.
17574 * Returns NULL when there is an error.
17577 var2fpos(varp
, dollar_lnum
, fnum
)
17579 int dollar_lnum
; /* TRUE when $ is last line */
17580 int *fnum
; /* set to fnum for '0, 'A, etc. */
17586 /* Argument can be [lnum, col, coladd]. */
17587 if (varp
->v_type
== VAR_LIST
)
17594 l
= varp
->vval
.v_list
;
17598 /* Get the line number */
17599 pos
.lnum
= list_find_nr(l
, 0L, &error
);
17600 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
17601 return NULL
; /* invalid line number */
17603 /* Get the column number */
17604 pos
.col
= list_find_nr(l
, 1L, &error
);
17607 len
= (long)STRLEN(ml_get(pos
.lnum
));
17609 /* We accept "$" for the column number: last column. */
17610 li
= list_find(l
, 1L);
17611 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
17612 && li
->li_tv
.vval
.v_string
!= NULL
17613 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
17616 /* Accept a position up to the NUL after the line. */
17617 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
17618 return NULL
; /* invalid column number */
17621 #ifdef FEAT_VIRTUALEDIT
17622 /* Get the virtual offset. Defaults to zero. */
17623 pos
.coladd
= list_find_nr(l
, 2L, &error
);
17631 name
= get_tv_string_chk(varp
);
17634 if (name
[0] == '.') /* cursor */
17635 return &curwin
->w_cursor
;
17637 if (name
[0] == 'v' && name
[1] == NUL
) /* Visual start */
17641 return &curwin
->w_cursor
;
17644 if (name
[0] == '\'') /* mark */
17646 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
17647 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
17652 #ifdef FEAT_VIRTUALEDIT
17656 if (name
[0] == 'w' && dollar_lnum
)
17659 if (name
[1] == '0') /* "w0": first visible line */
17662 pos
.lnum
= curwin
->w_topline
;
17665 else if (name
[1] == '$') /* "w$": last visible line */
17667 validate_botline();
17668 pos
.lnum
= curwin
->w_botline
- 1;
17672 else if (name
[0] == '$') /* last column or line */
17676 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
17681 pos
.lnum
= curwin
->w_cursor
.lnum
;
17682 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
17690 * Convert list in "arg" into a position and optional file number.
17691 * When "fnump" is NULL there is no file number, only 3 items.
17692 * Note that the column is passed on as-is, the caller may want to decrement
17693 * it to use 1 for the first column.
17694 * Return FAIL when conversion is not possible, doesn't check the position for
17698 list2fpos(arg
, posp
, fnump
)
17703 list_T
*l
= arg
->vval
.v_list
;
17707 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17708 * when "fnump" isn't NULL and "coladd" is optional. */
17709 if (arg
->v_type
!= VAR_LIST
17711 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
17712 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
17717 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
17721 n
= curbuf
->b_fnum
; /* current buffer */
17725 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
17730 n
= list_find_nr(l
, i
++, NULL
); /* col */
17735 #ifdef FEAT_VIRTUALEDIT
17736 n
= list_find_nr(l
, i
, NULL
);
17747 * Get the length of an environment variable name.
17748 * Advance "arg" to the first character after the name.
17749 * Return 0 for error.
17758 for (p
= *arg
; vim_isIDc(*p
); ++p
)
17760 if (p
== *arg
) /* no name found */
17763 len
= (int)(p
- *arg
);
17769 * Get the length of the name of a function or internal variable.
17770 * "arg" is advanced to the first non-white character after the name.
17771 * Return 0 if something is wrong.
17780 /* Find the end of the name. */
17781 for (p
= *arg
; eval_isnamec(*p
); ++p
)
17783 if (p
== *arg
) /* no name found */
17786 len
= (int)(p
- *arg
);
17787 *arg
= skipwhite(p
);
17793 * Get the length of the name of a variable or function.
17794 * Only the name is recognized, does not handle ".key" or "[idx]".
17795 * "arg" is advanced to the first non-white character after the name.
17796 * Return -1 if curly braces expansion failed.
17797 * Return 0 if something else is wrong.
17798 * If the name contains 'magic' {}'s, expand them and return the
17799 * expanded name in an allocated string via 'alias' - caller must free.
17802 get_name_len(arg
, alias
, evaluate
, verbose
)
17810 char_u
*expr_start
;
17813 *alias
= NULL
; /* default to no alias */
17815 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
17816 && (*arg
)[2] == (int)KE_SNR
)
17818 /* hard coded <SNR>, already translated */
17820 return get_id_len(arg
) + 3;
17822 len
= eval_fname_script(*arg
);
17825 /* literal "<SID>", "s:" or "<SNR>" */
17830 * Find the end of the name; check for {} construction.
17832 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
17833 len
> 0 ? 0 : FNE_CHECK_START
);
17834 if (expr_start
!= NULL
)
17836 char_u
*temp_string
;
17840 len
+= (int)(p
- *arg
);
17841 *arg
= skipwhite(p
);
17846 * Include any <SID> etc in the expanded string:
17847 * Thus the -len here.
17849 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
17850 if (temp_string
== NULL
)
17852 *alias
= temp_string
;
17853 *arg
= skipwhite(p
);
17854 return (int)STRLEN(temp_string
);
17857 len
+= get_id_len(arg
);
17858 if (len
== 0 && verbose
)
17859 EMSG2(_(e_invexpr2
), *arg
);
17865 * Find the end of a variable or function name, taking care of magic braces.
17866 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17867 * start and end of the first magic braces item.
17868 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
17869 * Return a pointer to just after the name. Equal to "arg" if there is no
17873 find_name_end(arg
, expr_start
, expr_end
, flags
)
17875 char_u
**expr_start
;
17883 if (expr_start
!= NULL
)
17885 *expr_start
= NULL
;
17889 /* Quick check for valid starting character. */
17890 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
17893 for (p
= arg
; *p
!= NUL
17894 && (eval_isnamec(*p
)
17896 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
17898 || br_nest
!= 0); mb_ptr_adv(p
))
17902 /* skip over 'string' to avoid counting [ and ] inside it. */
17903 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
17908 else if (*p
== '"')
17910 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17911 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
17912 if (*p
== '\\' && p
[1] != NUL
)
17922 else if (*p
== ']')
17931 if (expr_start
!= NULL
&& *expr_start
== NULL
)
17934 else if (*p
== '}')
17937 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
17947 * Expands out the 'magic' {}'s in a variable/function name.
17948 * Note that this can call itself recursively, to deal with
17949 * constructs like foo{bar}{baz}{bam}
17950 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17956 * Returns a new allocated string, which the caller must free.
17957 * Returns NULL for failure.
17960 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
17962 char_u
*expr_start
;
17967 char_u
*retval
= NULL
;
17968 char_u
*temp_result
;
17969 char_u
*nextcmd
= NULL
;
17971 if (expr_end
== NULL
|| in_end
== NULL
)
17978 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
17979 if (temp_result
!= NULL
&& nextcmd
== NULL
)
17981 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
17982 + (in_end
- expr_end
) + 1));
17983 if (retval
!= NULL
)
17985 STRCPY(retval
, in_start
);
17986 STRCAT(retval
, temp_result
);
17987 STRCAT(retval
, expr_end
+ 1);
17990 vim_free(temp_result
);
17992 *in_end
= c1
; /* put char back for error messages */
17996 if (retval
!= NULL
)
17998 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
17999 if (expr_start
!= NULL
)
18001 /* Further expansion! */
18002 temp_result
= make_expanded_name(retval
, expr_start
,
18003 expr_end
, temp_result
);
18005 retval
= temp_result
;
18013 * Return TRUE if character "c" can be used in a variable or function name.
18014 * Does not include '{' or '}' for magic braces.
18020 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
18024 * Return TRUE if character "c" can be used as the first character in a
18025 * variable or function name (excluding '{' and '}').
18031 return (ASCII_ISALPHA(c
) || c
== '_');
18035 * Set number v: variable to "val".
18038 set_vim_var_nr(idx
, val
)
18042 vimvars
[idx
].vv_nr
= val
;
18046 * Get number v: variable value.
18049 get_vim_var_nr(idx
)
18052 return vimvars
[idx
].vv_nr
;
18055 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18057 * Get string v: variable value. Uses a static buffer, can only be used once.
18060 get_vim_var_str(idx
)
18063 return get_tv_string(&vimvars
[idx
].vv_tv
);
18068 * Set v:count, v:count1 and v:prevcount.
18071 set_vcount(count
, count1
)
18075 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
18076 vimvars
[VV_COUNT
].vv_nr
= count
;
18077 vimvars
[VV_COUNT1
].vv_nr
= count1
;
18081 * Set string v: variable to a copy of "val".
18084 set_vim_var_string(idx
, val
, len
)
18087 int len
; /* length of "val" to use or -1 (whole string) */
18089 /* Need to do this (at least) once, since we can't initialize a union.
18090 * Will always be invoked when "v:progname" is set. */
18091 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
18093 vim_free(vimvars
[idx
].vv_str
);
18095 vimvars
[idx
].vv_str
= NULL
;
18096 else if (len
== -1)
18097 vimvars
[idx
].vv_str
= vim_strsave(val
);
18099 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
18103 * Set v:register if needed.
18111 if (c
== 0 || c
== ' ')
18115 /* Avoid free/alloc when the value is already right. */
18116 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
18117 set_vim_var_string(VV_REG
, ®name
, 1);
18121 * Get or set v:exception. If "oldval" == NULL, return the current value.
18122 * Otherwise, restore the value to "oldval" and return NULL.
18123 * Must always be called in pairs to save and restore v:exception! Does not
18124 * take care of memory allocations.
18127 v_exception(oldval
)
18130 if (oldval
== NULL
)
18131 return vimvars
[VV_EXCEPTION
].vv_str
;
18133 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
18138 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18139 * Otherwise, restore the value to "oldval" and return NULL.
18140 * Must always be called in pairs to save and restore v:throwpoint! Does not
18141 * take care of memory allocations.
18144 v_throwpoint(oldval
)
18147 if (oldval
== NULL
)
18148 return vimvars
[VV_THROWPOINT
].vv_str
;
18150 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
18154 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18157 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18158 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18159 * Must always be called in pairs!
18162 set_cmdarg(eap
, oldarg
)
18170 oldval
= vimvars
[VV_CMDARG
].vv_str
;
18174 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
18178 if (eap
->force_bin
== FORCE_BIN
)
18180 else if (eap
->force_bin
== FORCE_NOBIN
)
18185 if (eap
->read_edit
)
18188 if (eap
->force_ff
!= 0)
18189 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
18191 if (eap
->force_enc
!= 0)
18192 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
18193 if (eap
->bad_char
!= 0)
18194 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
18197 newval
= alloc(len
+ 1);
18198 if (newval
== NULL
)
18201 if (eap
->force_bin
== FORCE_BIN
)
18202 sprintf((char *)newval
, " ++bin");
18203 else if (eap
->force_bin
== FORCE_NOBIN
)
18204 sprintf((char *)newval
, " ++nobin");
18208 if (eap
->read_edit
)
18209 STRCAT(newval
, " ++edit");
18211 if (eap
->force_ff
!= 0)
18212 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
18213 eap
->cmd
+ eap
->force_ff
);
18215 if (eap
->force_enc
!= 0)
18216 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
18217 eap
->cmd
+ eap
->force_enc
);
18218 if (eap
->bad_char
!= 0)
18219 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
18220 eap
->cmd
+ eap
->bad_char
);
18222 vimvars
[VV_CMDARG
].vv_str
= newval
;
18228 * Get the value of internal variable "name".
18229 * Return OK or FAIL.
18232 get_var_tv(name
, len
, rettv
, verbose
)
18234 int len
; /* length of "name" */
18235 typval_T
*rettv
; /* NULL when only checking existence */
18236 int verbose
; /* may give error message */
18239 typval_T
*tv
= NULL
;
18244 /* truncate the name, so that we can use strcmp() */
18249 * Check for "b:changedtick".
18251 if (STRCMP(name
, "b:changedtick") == 0)
18253 atv
.v_type
= VAR_NUMBER
;
18254 atv
.vval
.v_number
= curbuf
->b_changedtick
;
18259 * Check for user-defined variables.
18263 v
= find_var(name
, NULL
);
18270 if (rettv
!= NULL
&& verbose
)
18271 EMSG2(_(e_undefvar
), name
);
18274 else if (rettv
!= NULL
)
18275 copy_tv(tv
, rettv
);
18283 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18284 * Also handle function call with Funcref variable: func(expr)
18285 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18288 handle_subscript(arg
, rettv
, evaluate
, verbose
)
18291 int evaluate
; /* do more than finding the end */
18292 int verbose
; /* give error messages */
18295 dict_T
*selfdict
= NULL
;
18302 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
18303 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
18304 && !vim_iswhite(*(*arg
- 1)))
18308 /* need to copy the funcref so that we can clear rettv */
18310 rettv
->v_type
= VAR_UNKNOWN
;
18312 /* Invoke the function. Recursive! */
18313 s
= functv
.vval
.v_string
;
18314 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
18315 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
18316 &len
, evaluate
, selfdict
);
18318 /* Clear the funcref afterwards, so that deleting it while
18319 * evaluating the arguments is possible (see test55). */
18322 /* Stop the expression evaluation when immediately aborting on
18323 * error, or when an interrupt occurred or an exception was thrown
18324 * but not caught. */
18331 dict_unref(selfdict
);
18334 else /* **arg == '[' || **arg == '.' */
18336 dict_unref(selfdict
);
18337 if (rettv
->v_type
== VAR_DICT
)
18339 selfdict
= rettv
->vval
.v_dict
;
18340 if (selfdict
!= NULL
)
18341 ++selfdict
->dv_refcount
;
18345 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
18352 dict_unref(selfdict
);
18357 * Allocate memory for a variable type-value, and make it empty (0 or NULL
18363 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
18367 * Allocate memory for a variable type-value, and assign a string to it.
18368 * The string "s" must have been allocated, it is consumed.
18369 * Return NULL for out of memory, the variable otherwise.
18377 rettv
= alloc_tv();
18380 rettv
->v_type
= VAR_STRING
;
18381 rettv
->vval
.v_string
= s
;
18389 * Free the memory for a variable type-value.
18397 switch (varp
->v_type
)
18400 func_unref(varp
->vval
.v_string
);
18403 vim_free(varp
->vval
.v_string
);
18406 list_unref(varp
->vval
.v_list
);
18409 dict_unref(varp
->vval
.v_dict
);
18418 EMSG2(_(e_intern2
), "free_tv()");
18426 * Free the memory for a variable value and set the value to NULL or 0.
18434 switch (varp
->v_type
)
18437 func_unref(varp
->vval
.v_string
);
18440 vim_free(varp
->vval
.v_string
);
18441 varp
->vval
.v_string
= NULL
;
18444 list_unref(varp
->vval
.v_list
);
18445 varp
->vval
.v_list
= NULL
;
18448 dict_unref(varp
->vval
.v_dict
);
18449 varp
->vval
.v_dict
= NULL
;
18452 varp
->vval
.v_number
= 0;
18456 varp
->vval
.v_float
= 0.0;
18462 EMSG2(_(e_intern2
), "clear_tv()");
18469 * Set the value of a variable to NULL without freeing items.
18476 vim_memset(varp
, 0, sizeof(typval_T
));
18480 * Get the number value of a variable.
18481 * If it is a String variable, uses vim_str2nr().
18482 * For incompatible types, return 0.
18483 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18484 * caller of incompatible types: it sets *denote to TRUE if "denote"
18485 * is not NULL or returns -1 otherwise.
18488 get_tv_number(varp
)
18493 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
18497 get_tv_number_chk(varp
, denote
)
18503 switch (varp
->v_type
)
18506 return (long)(varp
->vval
.v_number
);
18509 EMSG(_("E805: Using a Float as a Number"));
18513 EMSG(_("E703: Using a Funcref as a Number"));
18516 if (varp
->vval
.v_string
!= NULL
)
18517 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
18518 TRUE
, TRUE
, &n
, NULL
);
18521 EMSG(_("E745: Using a List as a Number"));
18524 EMSG(_("E728: Using a Dictionary as a Number"));
18527 EMSG2(_(e_intern2
), "get_tv_number()");
18530 if (denote
== NULL
) /* useful for values that must be unsigned */
18538 * Get the lnum from the first argument.
18539 * Also accepts ".", "$", etc., but that only works for the current buffer.
18540 * Returns -1 on error.
18543 get_tv_lnum(argvars
)
18549 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
18550 if (lnum
== 0) /* no valid number, try using line() */
18552 rettv
.v_type
= VAR_NUMBER
;
18553 f_line(argvars
, &rettv
);
18554 lnum
= rettv
.vval
.v_number
;
18561 * Get the lnum from the first argument.
18562 * Also accepts "$", then "buf" is used.
18563 * Returns 0 on error.
18566 get_tv_lnum_buf(argvars
, buf
)
18570 if (argvars
[0].v_type
== VAR_STRING
18571 && argvars
[0].vval
.v_string
!= NULL
18572 && argvars
[0].vval
.v_string
[0] == '$'
18574 return buf
->b_ml
.ml_line_count
;
18575 return get_tv_number_chk(&argvars
[0], NULL
);
18579 * Get the string value of a variable.
18580 * If it is a Number variable, the number is converted into a string.
18581 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18582 * get_tv_string_buf() uses a given buffer.
18583 * If the String variable has never been set, return an empty string.
18584 * Never returns NULL;
18585 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18589 get_tv_string(varp
)
18592 static char_u mybuf
[NUMBUFLEN
];
18594 return get_tv_string_buf(varp
, mybuf
);
18598 get_tv_string_buf(varp
, buf
)
18602 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
18604 return res
!= NULL
? res
: (char_u
*)"";
18608 get_tv_string_chk(varp
)
18611 static char_u mybuf
[NUMBUFLEN
];
18613 return get_tv_string_buf_chk(varp
, mybuf
);
18617 get_tv_string_buf_chk(varp
, buf
)
18621 switch (varp
->v_type
)
18624 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
18627 EMSG(_("E729: using Funcref as a String"));
18630 EMSG(_("E730: using List as a String"));
18633 EMSG(_("E731: using Dictionary as a String"));
18637 EMSG(_("E806: using Float as a String"));
18641 if (varp
->vval
.v_string
!= NULL
)
18642 return varp
->vval
.v_string
;
18643 return (char_u
*)"";
18645 EMSG2(_(e_intern2
), "get_tv_string_buf()");
18652 * Find variable "name" in the list of variables.
18653 * Return a pointer to it if found, NULL if not found.
18654 * Careful: "a:0" variables don't have a name.
18655 * When "htp" is not NULL we are writing to the variable, set "htp" to the
18658 static dictitem_T
*
18659 find_var(name
, htp
)
18666 ht
= find_var_ht(name
, &varname
);
18671 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
18675 * Find variable "varname" in hashtab "ht".
18676 * Returns NULL if not found.
18678 static dictitem_T
*
18679 find_var_in_ht(ht
, varname
, writing
)
18686 if (*varname
== NUL
)
18688 /* Must be something like "s:", otherwise "ht" would be NULL. */
18689 switch (varname
[-2])
18691 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
18692 case 'g': return &globvars_var
;
18693 case 'v': return &vimvars_var
;
18694 case 'b': return &curbuf
->b_bufvar
;
18695 case 'w': return &curwin
->w_winvar
;
18696 #ifdef FEAT_WINDOWS
18697 case 't': return &curtab
->tp_winvar
;
18699 case 'l': return current_funccal
== NULL
18700 ? NULL
: ¤t_funccal
->l_vars_var
;
18701 case 'a': return current_funccal
== NULL
18702 ? NULL
: ¤t_funccal
->l_avars_var
;
18707 hi
= hash_find(ht
, varname
);
18708 if (HASHITEM_EMPTY(hi
))
18710 /* For global variables we may try auto-loading the script. If it
18711 * worked find the variable again. Don't auto-load a script if it was
18712 * loaded already, otherwise it would be loaded every time when
18713 * checking if a function name is a Funcref variable. */
18714 if (ht
== &globvarht
&& !writing
18715 && script_autoload(varname
, FALSE
) && !aborting())
18716 hi
= hash_find(ht
, varname
);
18717 if (HASHITEM_EMPTY(hi
))
18724 * Find the hashtab used for a variable name.
18725 * Set "varname" to the start of name without ':'.
18728 find_var_ht(name
, varname
)
18734 if (name
[1] != ':')
18736 /* The name must not start with a colon or #. */
18737 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
18741 /* "version" is "v:version" in all scopes */
18742 hi
= hash_find(&compat_hashtab
, name
);
18743 if (!HASHITEM_EMPTY(hi
))
18744 return &compat_hashtab
;
18746 if (current_funccal
== NULL
)
18747 return &globvarht
; /* global variable */
18748 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
18750 *varname
= name
+ 2;
18751 if (*name
== 'g') /* global variable */
18753 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18755 if (vim_strchr(name
+ 2, ':') != NULL
18756 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
18758 if (*name
== 'b') /* buffer variable */
18759 return &curbuf
->b_vars
.dv_hashtab
;
18760 if (*name
== 'w') /* window variable */
18761 return &curwin
->w_vars
.dv_hashtab
;
18762 #ifdef FEAT_WINDOWS
18763 if (*name
== 't') /* tab page variable */
18764 return &curtab
->tp_vars
.dv_hashtab
;
18766 if (*name
== 'v') /* v: variable */
18768 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
18769 return ¤t_funccal
->l_avars
.dv_hashtab
;
18770 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
18771 return ¤t_funccal
->l_vars
.dv_hashtab
;
18772 if (*name
== 's' /* script variable */
18773 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
18774 return &SCRIPT_VARS(current_SID
);
18779 * Get the string value of a (global/local) variable.
18780 * Returns NULL when it doesn't exist.
18783 get_var_value(name
)
18788 v
= find_var(name
, NULL
);
18791 return get_tv_string(&v
->di_tv
);
18795 * Allocate a new hashtab for a sourced script. It will be used while
18796 * sourcing this script and when executing functions defined in the script.
18799 new_script_vars(id
)
18806 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
18808 /* Re-allocating ga_data means that an ht_array pointing to
18809 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
18810 * at its init value. Also reset "v_dict", it's always the same. */
18811 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
18813 ht
= &SCRIPT_VARS(i
);
18814 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
18815 ht
->ht_array
= ht
->ht_smallarray
;
18816 sv
= &SCRIPT_SV(i
);
18817 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
18820 while (ga_scripts
.ga_len
< id
)
18822 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
18823 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
18824 ++ga_scripts
.ga_len
;
18830 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18834 init_var_dict(dict
, dict_var
)
18836 dictitem_T
*dict_var
;
18838 hash_init(&dict
->dv_hashtab
);
18839 dict
->dv_refcount
= 99999;
18840 dict_var
->di_tv
.vval
.v_dict
= dict
;
18841 dict_var
->di_tv
.v_type
= VAR_DICT
;
18842 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
18843 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
18844 dict_var
->di_key
[0] = NUL
;
18848 * Clean up a list of internal variables.
18849 * Frees all allocated variables and the value they contain.
18850 * Clears hashtab "ht", does not free it.
18856 vars_clear_ext(ht
, TRUE
);
18860 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18863 vars_clear_ext(ht
, free_val
)
18872 todo
= (int)ht
->ht_used
;
18873 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
18875 if (!HASHITEM_EMPTY(hi
))
18879 /* Free the variable. Don't remove it from the hashtab,
18880 * ht_array might change then. hash_clear() takes care of it
18884 clear_tv(&v
->di_tv
);
18885 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
18894 * Delete a variable from hashtab "ht" at item "hi".
18895 * Clear the variable value and free the dictitem.
18902 dictitem_T
*di
= HI2DI(hi
);
18904 hash_remove(ht
, hi
);
18905 clear_tv(&di
->di_tv
);
18910 * List the value of one internal variable.
18913 list_one_var(v
, prefix
, first
)
18920 char_u numbuf
[NUMBUFLEN
];
18922 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
18923 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
18924 s
== NULL
? (char_u
*)"" : s
, first
);
18929 list_one_var_a(prefix
, name
, type
, string
, first
)
18934 int *first
; /* when TRUE clear rest of screen and set to FALSE */
18936 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18939 if (name
!= NULL
) /* "a:" vars don't have a name stored */
18943 if (type
== VAR_NUMBER
)
18945 else if (type
== VAR_FUNC
)
18947 else if (type
== VAR_LIST
)
18950 if (*string
== '[')
18953 else if (type
== VAR_DICT
)
18956 if (*string
== '{')
18962 msg_outtrans(string
);
18964 if (type
== VAR_FUNC
)
18965 msg_puts((char_u
*)"()");
18974 * Set variable "name" to value in "tv".
18975 * If the variable already exists, the value is updated.
18976 * Otherwise the variable is created.
18979 set_var(name
, tv
, copy
)
18982 int copy
; /* make copy of value in "tv" */
18989 if (tv
->v_type
== VAR_FUNC
)
18991 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
18992 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
18993 ? name
[2] : name
[0]))
18995 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
18998 if (function_exists(name
))
19000 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19006 ht
= find_var_ht(name
, &varname
);
19007 if (ht
== NULL
|| *varname
== NUL
)
19009 EMSG2(_(e_illvar
), name
);
19013 v
= find_var_in_ht(ht
, varname
, TRUE
);
19016 /* existing variable, need to clear the value */
19017 if (var_check_ro(v
->di_flags
, name
)
19018 || tv_check_lock(v
->di_tv
.v_lock
, name
))
19020 if (v
->di_tv
.v_type
!= tv
->v_type
19021 && !((v
->di_tv
.v_type
== VAR_STRING
19022 || v
->di_tv
.v_type
== VAR_NUMBER
)
19023 && (tv
->v_type
== VAR_STRING
19024 || tv
->v_type
== VAR_NUMBER
))
19026 && !((v
->di_tv
.v_type
== VAR_NUMBER
19027 || v
->di_tv
.v_type
== VAR_FLOAT
)
19028 && (tv
->v_type
== VAR_NUMBER
19029 || tv
->v_type
== VAR_FLOAT
))
19033 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
19038 * Handle setting internal v: variables separately: we don't change
19041 if (ht
== &vimvarht
)
19043 if (v
->di_tv
.v_type
== VAR_STRING
)
19045 vim_free(v
->di_tv
.vval
.v_string
);
19046 if (copy
|| tv
->v_type
!= VAR_STRING
)
19047 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
19050 /* Take over the string to avoid an extra alloc/free. */
19051 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
19052 tv
->vval
.v_string
= NULL
;
19055 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
19056 EMSG2(_(e_intern2
), "set_var()");
19059 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
19060 if (STRCMP(varname
, "searchforward") == 0)
19061 set_search_direction(v
->di_tv
.vval
.v_number
? '/' : '?');
19066 clear_tv(&v
->di_tv
);
19068 else /* add a new variable */
19070 /* Can't add "v:" variable. */
19071 if (ht
== &vimvarht
)
19073 EMSG2(_(e_illvar
), name
);
19077 /* Make sure the variable name is valid. */
19078 for (p
= varname
; *p
!= NUL
; ++p
)
19079 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
19080 && *p
!= AUTOLOAD_CHAR
)
19082 EMSG2(_(e_illvar
), varname
);
19086 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19087 + STRLEN(varname
)));
19090 STRCPY(v
->di_key
, varname
);
19091 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
19099 if (copy
|| tv
->v_type
== VAR_NUMBER
|| tv
->v_type
== VAR_FLOAT
)
19100 copy_tv(tv
, &v
->di_tv
);
19104 v
->di_tv
.v_lock
= 0;
19110 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
19111 * Also give an error message.
19114 var_check_ro(flags
, name
)
19118 if (flags
& DI_FLAGS_RO
)
19120 EMSG2(_(e_readonlyvar
), name
);
19123 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
19125 EMSG2(_(e_readonlysbx
), name
);
19132 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19133 * Also give an error message.
19136 var_check_fixed(flags
, name
)
19140 if (flags
& DI_FLAGS_FIX
)
19142 EMSG2(_("E795: Cannot delete variable %s"), name
);
19149 * Return TRUE if typeval "tv" is set to be locked (immutable).
19150 * Also give an error message, using "name".
19153 tv_check_lock(lock
, name
)
19157 if (lock
& VAR_LOCKED
)
19159 EMSG2(_("E741: Value is locked: %s"),
19160 name
== NULL
? (char_u
*)_("Unknown") : name
);
19163 if (lock
& VAR_FIXED
)
19165 EMSG2(_("E742: Cannot change value of %s"),
19166 name
== NULL
? (char_u
*)_("Unknown") : name
);
19173 * Copy the values from typval_T "from" to typval_T "to".
19174 * When needed allocates string or increases reference count.
19175 * Does not make a copy of a list or dict but copies the reference!
19182 to
->v_type
= from
->v_type
;
19184 switch (from
->v_type
)
19187 to
->vval
.v_number
= from
->vval
.v_number
;
19191 to
->vval
.v_float
= from
->vval
.v_float
;
19196 if (from
->vval
.v_string
== NULL
)
19197 to
->vval
.v_string
= NULL
;
19200 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
19201 if (from
->v_type
== VAR_FUNC
)
19202 func_ref(to
->vval
.v_string
);
19206 if (from
->vval
.v_list
== NULL
)
19207 to
->vval
.v_list
= NULL
;
19210 to
->vval
.v_list
= from
->vval
.v_list
;
19211 ++to
->vval
.v_list
->lv_refcount
;
19215 if (from
->vval
.v_dict
== NULL
)
19216 to
->vval
.v_dict
= NULL
;
19219 to
->vval
.v_dict
= from
->vval
.v_dict
;
19220 ++to
->vval
.v_dict
->dv_refcount
;
19224 EMSG2(_(e_intern2
), "copy_tv()");
19230 * Make a copy of an item.
19231 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
19232 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19233 * reference to an already copied list/dict can be used.
19234 * Returns FAIL or OK.
19237 item_copy(from
, to
, deep
, copyID
)
19243 static int recurse
= 0;
19246 if (recurse
>= DICT_MAXNEST
)
19248 EMSG(_("E698: variable nested too deep for making a copy"));
19253 switch (from
->v_type
)
19264 to
->v_type
= VAR_LIST
;
19266 if (from
->vval
.v_list
== NULL
)
19267 to
->vval
.v_list
= NULL
;
19268 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
19270 /* use the copy made earlier */
19271 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
19272 ++to
->vval
.v_list
->lv_refcount
;
19275 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
19276 if (to
->vval
.v_list
== NULL
)
19280 to
->v_type
= VAR_DICT
;
19282 if (from
->vval
.v_dict
== NULL
)
19283 to
->vval
.v_dict
= NULL
;
19284 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
19286 /* use the copy made earlier */
19287 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
19288 ++to
->vval
.v_dict
->dv_refcount
;
19291 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
19292 if (to
->vval
.v_dict
== NULL
)
19296 EMSG2(_(e_intern2
), "item_copy()");
19304 * ":echo expr1 ..." print each argument separated with a space, add a
19305 * newline at the end.
19306 * ":echon expr1 ..." print each argument plain.
19312 char_u
*arg
= eap
->arg
;
19316 int needclr
= TRUE
;
19317 int atstart
= TRUE
;
19318 char_u numbuf
[NUMBUFLEN
];
19322 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
19324 /* If eval1() causes an error message the text from the command may
19325 * still need to be cleared. E.g., "echo 22,44". */
19326 need_clr_eos
= needclr
;
19329 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19332 * Report the invalid expression unless the expression evaluation
19333 * has been cancelled due to an aborting error, an interrupt, or an
19337 EMSG2(_(e_invexpr2
), p
);
19338 need_clr_eos
= FALSE
;
19341 need_clr_eos
= FALSE
;
19348 /* Call msg_start() after eval1(), evaluating the expression
19349 * may cause a message to appear. */
19350 if (eap
->cmdidx
== CMD_echo
)
19353 else if (eap
->cmdidx
== CMD_echo
)
19354 msg_puts_attr((char_u
*)" ", echo_attr
);
19355 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
19357 for ( ; *p
!= NUL
&& !got_int
; ++p
)
19359 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
19361 if (*p
!= TAB
&& needclr
)
19363 /* remove any text still there from the command */
19367 msg_putchar_attr(*p
, echo_attr
);
19374 int i
= (*mb_ptr2len
)(p
);
19376 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
19381 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
19387 arg
= skipwhite(arg
);
19389 eap
->nextcmd
= check_nextcmd(arg
);
19395 /* remove text that may still be there from the command */
19398 if (eap
->cmdidx
== CMD_echo
)
19404 * ":echohl {name}".
19412 id
= syn_name2id(eap
->arg
);
19416 echo_attr
= syn_id2attr(id
);
19420 * ":execute expr1 ..." execute the result of an expression.
19421 * ":echomsg expr1 ..." Print a message
19422 * ":echoerr expr1 ..." Print an error
19423 * Each gets spaces around each argument and a newline at the end for
19430 char_u
*arg
= eap
->arg
;
19438 ga_init2(&ga
, 1, 80);
19442 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
19445 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19448 * Report the invalid expression unless the expression evaluation
19449 * has been cancelled due to an aborting error, an interrupt, or an
19453 EMSG2(_(e_invexpr2
), p
);
19460 p
= get_tv_string(&rettv
);
19461 len
= (int)STRLEN(p
);
19462 if (ga_grow(&ga
, len
+ 2) == FAIL
)
19469 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
19470 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
19475 arg
= skipwhite(arg
);
19478 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
19480 if (eap
->cmdidx
== CMD_echomsg
)
19482 MSG_ATTR(ga
.ga_data
, echo_attr
);
19485 else if (eap
->cmdidx
== CMD_echoerr
)
19487 /* We don't want to abort following commands, restore did_emsg. */
19488 save_did_emsg
= did_emsg
;
19489 EMSG((char_u
*)ga
.ga_data
);
19491 did_emsg
= save_did_emsg
;
19493 else if (eap
->cmdidx
== CMD_execute
)
19494 do_cmdline((char_u
*)ga
.ga_data
,
19495 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
19503 eap
->nextcmd
= check_nextcmd(arg
);
19507 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19508 * "arg" points to the "&" or '+' when called, to "option" when returning.
19509 * Returns NULL when no option name found. Otherwise pointer to the char
19510 * after the option name.
19513 find_option_end(arg
, opt_flags
)
19520 if (*p
== 'g' && p
[1] == ':')
19522 *opt_flags
= OPT_GLOBAL
;
19525 else if (*p
== 'l' && p
[1] == ':')
19527 *opt_flags
= OPT_LOCAL
;
19533 if (!ASCII_ISALPHA(*p
))
19537 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
19538 p
+= 4; /* termcap option */
19540 while (ASCII_ISALPHA(*p
))
19555 int saved_did_emsg
;
19556 char_u
*name
= NULL
;
19559 char_u
*line_arg
= NULL
;
19562 int varargs
= FALSE
;
19563 int mustend
= FALSE
;
19568 char_u
*skip_until
= NULL
;
19571 static int func_nr
= 0; /* number for nameless function */
19576 int sourcing_lnum_off
;
19579 * ":function" without argument: list functions.
19581 if (ends_excmd(*eap
->arg
))
19585 todo
= (int)func_hashtab
.ht_used
;
19586 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19588 if (!HASHITEM_EMPTY(hi
))
19592 if (!isdigit(*fp
->uf_name
))
19593 list_func_head(fp
, FALSE
);
19597 eap
->nextcmd
= check_nextcmd(eap
->arg
);
19602 * ":function /pat": list functions matching pattern.
19604 if (*eap
->arg
== '/')
19606 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
19609 regmatch_T regmatch
;
19613 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
19615 if (regmatch
.regprog
!= NULL
)
19617 regmatch
.rm_ic
= p_ic
;
19619 todo
= (int)func_hashtab
.ht_used
;
19620 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19622 if (!HASHITEM_EMPTY(hi
))
19626 if (!isdigit(*fp
->uf_name
)
19627 && vim_regexec(®match
, fp
->uf_name
, 0))
19628 list_func_head(fp
, FALSE
);
19635 eap
->nextcmd
= check_nextcmd(p
);
19640 * Get the function name. There are these situations:
19641 * func normal function name
19642 * "name" == func, "fudi.fd_dict" == NULL
19643 * dict.func new dictionary entry
19644 * "name" == NULL, "fudi.fd_dict" set,
19645 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19646 * dict.func existing dict entry with a Funcref
19647 * "name" == func, "fudi.fd_dict" set,
19648 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19649 * dict.func existing dict entry that's not a Funcref
19650 * "name" == NULL, "fudi.fd_dict" set,
19651 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19654 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19655 paren
= (vim_strchr(p
, '(') != NULL
);
19656 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
19659 * Return on an invalid expression in braces, unless the expression
19660 * evaluation has been cancelled due to an aborting error, an
19661 * interrupt, or an exception.
19665 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
19666 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
19667 vim_free(fudi
.fd_newkey
);
19674 /* An error in a function call during evaluation of an expression in magic
19675 * braces should not cause the function not to be defined. */
19676 saved_did_emsg
= did_emsg
;
19680 * ":function func" with only function name: list function.
19684 if (!ends_excmd(*skipwhite(p
)))
19686 EMSG(_(e_trailing
));
19689 eap
->nextcmd
= check_nextcmd(p
);
19690 if (eap
->nextcmd
!= NULL
)
19692 if (!eap
->skip
&& !got_int
)
19694 fp
= find_func(name
);
19697 list_func_head(fp
, TRUE
);
19698 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
19700 if (FUNCLINE(fp
, j
) == NULL
)
19703 msg_outnum((long)(j
+ 1));
19708 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
19709 out_flush(); /* show a line at a time */
19715 msg_puts((char_u
*)" endfunction");
19719 emsg_funcname("E123: Undefined function: %s", name
);
19725 * ":function name(arg1, arg2)" Define function.
19732 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
19735 /* attempt to continue by skipping some text */
19736 if (vim_strchr(p
, '(') != NULL
)
19737 p
= vim_strchr(p
, '(');
19739 p
= skipwhite(p
+ 1);
19741 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
19742 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
19746 /* Check the name of the function. Unless it's a dictionary function
19747 * (that we are overwriting). */
19751 arg
= fudi
.fd_newkey
;
19752 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
19753 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
19755 if (*arg
== K_SPECIAL
)
19759 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
19760 : eval_isnamec(arg
[j
])))
19763 emsg_funcname(_(e_invarg2
), arg
);
19768 * Isolate the arguments: "arg1, arg2, ...)"
19772 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
19781 while (ASCII_ISALNUM(*p
) || *p
== '_')
19783 if (arg
== p
|| isdigit(*arg
)
19784 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
19785 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
19788 EMSG2(_("E125: Illegal argument: %s"), arg
);
19791 if (ga_grow(&newargs
, 1) == FAIL
)
19795 arg
= vim_strsave(arg
);
19798 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
19807 if (mustend
&& *p
!= ')')
19810 EMSG2(_(e_invarg2
), eap
->arg
);
19814 ++p
; /* skip the ')' */
19816 /* find extra arguments "range", "dict" and "abort" */
19820 if (STRNCMP(p
, "range", 5) == 0)
19825 else if (STRNCMP(p
, "dict", 4) == 0)
19830 else if (STRNCMP(p
, "abort", 5) == 0)
19839 /* When there is a line break use what follows for the function body.
19840 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19843 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
19844 EMSG(_(e_trailing
));
19847 * Read the body of the function, until ":endfunction" is found.
19851 /* Check if the function already exists, don't let the user type the
19852 * whole function before telling him it doesn't work! For a script we
19853 * need to skip the body to be able to find what follows. */
19854 if (!eap
->skip
&& !eap
->forceit
)
19856 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
19857 EMSG(_(e_funcdict
));
19858 else if (name
!= NULL
&& find_func(name
) != NULL
)
19859 emsg_funcname(e_funcexts
, name
);
19862 if (!eap
->skip
&& did_emsg
)
19865 msg_putchar('\n'); /* don't overwrite the function name */
19866 cmdline_row
= msg_row
;
19874 need_wait_return
= FALSE
;
19875 sourcing_lnum_off
= sourcing_lnum
;
19877 if (line_arg
!= NULL
)
19879 /* Use eap->arg, split up in parts by line breaks. */
19880 theline
= line_arg
;
19881 p
= vim_strchr(theline
, '\n');
19883 line_arg
+= STRLEN(line_arg
);
19890 else if (eap
->getline
== NULL
)
19891 theline
= getcmdline(':', 0L, indent
);
19893 theline
= eap
->getline(':', eap
->cookie
, indent
);
19895 lines_left
= Rows
- 1;
19896 if (theline
== NULL
)
19898 EMSG(_("E126: Missing :endfunction"));
19902 /* Detect line continuation: sourcing_lnum increased more than one. */
19903 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
19904 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
19906 sourcing_lnum_off
= 0;
19908 if (skip_until
!= NULL
)
19910 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19911 * don't check for ":endfunc". */
19912 if (STRCMP(theline
, skip_until
) == 0)
19914 vim_free(skip_until
);
19920 /* skip ':' and blanks*/
19921 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
19924 /* Check for "endfunction". */
19925 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
19927 if (line_arg
== NULL
)
19932 /* Increase indent inside "if", "while", "for" and "try", decrease
19934 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
19936 else if (STRNCMP(p
, "if", 2) == 0
19937 || STRNCMP(p
, "wh", 2) == 0
19938 || STRNCMP(p
, "for", 3) == 0
19939 || STRNCMP(p
, "try", 3) == 0)
19942 /* Check for defining a function inside this function. */
19943 if (checkforcmd(&p
, "function", 2))
19946 p
= skipwhite(p
+ 1);
19947 p
+= eval_fname_script(p
);
19948 if (ASCII_ISALPHA(*p
))
19950 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
19951 if (*skipwhite(p
) == '(')
19959 /* Check for ":append" or ":insert". */
19960 p
= skip_range(p
, NULL
);
19961 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
19963 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
19964 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
19965 skip_until
= vim_strsave((char_u
*)".");
19967 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19968 arg
= skipwhite(skiptowhite(p
));
19969 if (arg
[0] == '<' && arg
[1] =='<'
19970 && ((p
[0] == 'p' && p
[1] == 'y'
19971 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
19972 || (p
[0] == 'p' && p
[1] == 'e'
19973 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
19974 || (p
[0] == 't' && p
[1] == 'c'
19975 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
19976 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
19977 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
19978 || (p
[0] == 'm' && p
[1] == 'z'
19979 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
19982 /* ":python <<" continues until a dot, like ":append" */
19983 p
= skipwhite(arg
+ 2);
19985 skip_until
= vim_strsave((char_u
*)".");
19987 skip_until
= vim_strsave(p
);
19991 /* Add the line to the function. */
19992 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
19994 if (line_arg
== NULL
)
19999 /* Copy the line to newly allocated memory. get_one_sourceline()
20000 * allocates 250 bytes per line, this saves 80% on average. The cost
20001 * is an extra alloc/free. */
20002 p
= vim_strsave(theline
);
20005 if (line_arg
== NULL
)
20010 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
20012 /* Add NULL lines for continuation lines, so that the line count is
20013 * equal to the index in the growarray. */
20014 while (sourcing_lnum_off
-- > 0)
20015 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
20017 /* Check for end of eap->arg. */
20018 if (line_arg
!= NULL
&& *line_arg
== NUL
)
20022 /* Don't define the function when skipping commands or when an error was
20024 if (eap
->skip
|| did_emsg
)
20028 * If there are no errors, add the function
20030 if (fudi
.fd_dict
== NULL
)
20032 v
= find_var(name
, &ht
);
20033 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
20035 emsg_funcname("E707: Function name conflicts with variable: %s",
20040 fp
= find_func(name
);
20045 emsg_funcname(e_funcexts
, name
);
20048 if (fp
->uf_calls
> 0)
20050 emsg_funcname("E127: Cannot redefine function %s: It is in use",
20054 /* redefine existing function */
20055 ga_clear_strings(&(fp
->uf_args
));
20056 ga_clear_strings(&(fp
->uf_lines
));
20066 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
20068 EMSG(_(e_funcdict
));
20071 if (fudi
.fd_di
== NULL
)
20073 /* Can't add a function to a locked dictionary */
20074 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
20077 /* Can't change an existing function if it is locked */
20078 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
20081 /* Give the function a sequential number. Can only be used with a
20084 sprintf(numbuf
, "%d", ++func_nr
);
20085 name
= vim_strsave((char_u
*)numbuf
);
20092 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
20095 char_u
*scriptname
;
20097 /* Check that the autoload name matches the script name. */
20099 if (sourcing_name
!= NULL
)
20101 scriptname
= autoload_name(name
);
20102 if (scriptname
!= NULL
)
20104 p
= vim_strchr(scriptname
, '/');
20105 plen
= (int)STRLEN(p
);
20106 slen
= (int)STRLEN(sourcing_name
);
20107 if (slen
> plen
&& fnamecmp(p
,
20108 sourcing_name
+ slen
- plen
) == 0)
20110 vim_free(scriptname
);
20115 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
20120 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
20124 if (fudi
.fd_dict
!= NULL
)
20126 if (fudi
.fd_di
== NULL
)
20128 /* add new dict entry */
20129 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
20130 if (fudi
.fd_di
== NULL
)
20135 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
20137 vim_free(fudi
.fd_di
);
20143 /* overwrite existing dict entry */
20144 clear_tv(&fudi
.fd_di
->di_tv
);
20145 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
20146 fudi
.fd_di
->di_tv
.v_lock
= 0;
20147 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
20148 fp
->uf_refcount
= 1;
20150 /* behave like "dict" was used */
20154 /* insert the new function in the function list */
20155 STRCPY(fp
->uf_name
, name
);
20156 hash_add(&func_hashtab
, UF2HIKEY(fp
));
20158 fp
->uf_args
= newargs
;
20159 fp
->uf_lines
= newlines
;
20160 #ifdef FEAT_PROFILE
20161 fp
->uf_tml_count
= NULL
;
20162 fp
->uf_tml_total
= NULL
;
20163 fp
->uf_tml_self
= NULL
;
20164 fp
->uf_profiling
= FALSE
;
20165 if (prof_def_func())
20166 func_do_profile(fp
);
20168 fp
->uf_varargs
= varargs
;
20169 fp
->uf_flags
= flags
;
20171 fp
->uf_script_ID
= current_SID
;
20175 ga_clear_strings(&newargs
);
20176 ga_clear_strings(&newlines
);
20178 vim_free(skip_until
);
20179 vim_free(fudi
.fd_newkey
);
20181 did_emsg
|= saved_did_emsg
;
20185 * Get a function name, translating "<SID>" and "<SNR>".
20186 * Also handles a Funcref in a List or Dictionary.
20187 * Returns the function name in allocated memory, or NULL for failure.
20189 * TFN_INT: internal function name OK
20190 * TFN_QUIET: be quiet
20191 * Advances "pp" to just after the function name (if no error).
20194 trans_function_name(pp
, skip
, flags
, fdp
)
20196 int skip
; /* only find the end, don't evaluate */
20198 funcdict_T
*fdp
; /* return: info about dictionary used */
20200 char_u
*name
= NULL
;
20204 char_u sid_buf
[20];
20209 vim_memset(fdp
, 0, sizeof(funcdict_T
));
20212 /* Check for hard coded <SNR>: already translated function ID (from a user
20214 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
20215 && (*pp
)[2] == (int)KE_SNR
)
20218 len
= get_id_len(pp
) + 3;
20219 return vim_strnsave(start
, len
);
20222 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20223 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
20224 lead
= eval_fname_script(start
);
20228 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
20229 lead
> 2 ? 0 : FNE_CHECK_START
);
20233 EMSG(_("E129: Function name required"));
20236 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
20239 * Report an invalid expression in braces, unless the expression
20240 * evaluation has been cancelled due to an aborting error, an
20241 * interrupt, or an exception.
20246 EMSG2(_(e_invarg2
), start
);
20249 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
20253 if (lv
.ll_tv
!= NULL
)
20257 fdp
->fd_dict
= lv
.ll_dict
;
20258 fdp
->fd_newkey
= lv
.ll_newkey
;
20259 lv
.ll_newkey
= NULL
;
20260 fdp
->fd_di
= lv
.ll_di
;
20262 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
20264 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
20269 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
20270 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
20271 EMSG(_(e_funcref
));
20279 if (lv
.ll_name
== NULL
)
20281 /* Error found, but continue after the function name. */
20286 /* Check if the name is a Funcref. If so, use the value. */
20287 if (lv
.ll_exp_name
!= NULL
)
20289 len
= (int)STRLEN(lv
.ll_exp_name
);
20290 name
= deref_func_name(lv
.ll_exp_name
, &len
);
20291 if (name
== lv
.ll_exp_name
)
20296 len
= (int)(end
- *pp
);
20297 name
= deref_func_name(*pp
, &len
);
20303 name
= vim_strsave(name
);
20308 if (lv
.ll_exp_name
!= NULL
)
20310 len
= (int)STRLEN(lv
.ll_exp_name
);
20311 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
20312 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
20314 /* When there was "s:" already or the name expanded to get a
20315 * leading "s:" then remove it. */
20323 if (lead
== 2) /* skip over "s:" */
20325 len
= (int)(end
- lv
.ll_name
);
20329 * Copy the function name to allocated memory.
20330 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20331 * Accept <SNR>123_name() outside a script.
20334 lead
= 0; /* do nothing */
20338 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
20339 || eval_fname_sid(*pp
))
20341 /* It's "s:" or "<SID>" */
20342 if (current_SID
<= 0)
20344 EMSG(_(e_usingsid
));
20347 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
20348 lead
+= (int)STRLEN(sid_buf
);
20351 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
20353 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
20356 name
= alloc((unsigned)(len
+ lead
+ 1));
20361 name
[0] = K_SPECIAL
;
20362 name
[1] = KS_EXTRA
;
20363 name
[2] = (int)KE_SNR
;
20364 if (lead
> 3) /* If it's "<SID>" */
20365 STRCPY(name
+ 3, sid_buf
);
20367 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
20368 name
[len
+ lead
] = NUL
;
20378 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20379 * Return 2 if "p" starts with "s:".
20380 * Return 0 otherwise.
20383 eval_fname_script(p
)
20386 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
20387 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
20389 if (p
[0] == 's' && p
[1] == ':')
20395 * Return TRUE if "p" starts with "<SID>" or "s:".
20396 * Only works if eval_fname_script() returned non-zero for "p"!
20402 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
20406 * List the head of the function: "name(arg1, arg2)".
20409 list_func_head(fp
, indent
)
20418 MSG_PUTS("function ");
20419 if (fp
->uf_name
[0] == K_SPECIAL
)
20421 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
20422 msg_puts(fp
->uf_name
+ 3);
20425 msg_puts(fp
->uf_name
);
20427 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
20431 msg_puts(FUNCARG(fp
, j
));
20433 if (fp
->uf_varargs
)
20442 last_set_msg(fp
->uf_script_ID
);
20446 * Find a function by name, return pointer to it in ufuncs.
20447 * Return NULL for unknown function.
20455 hi
= hash_find(&func_hashtab
, name
);
20456 if (!HASHITEM_EMPTY(hi
))
20461 #if defined(EXITFREE) || defined(PROTO)
20463 free_all_functions()
20467 /* Need to start all over every time, because func_free() may change the
20469 while (func_hashtab
.ht_used
> 0)
20470 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
20471 if (!HASHITEM_EMPTY(hi
))
20473 func_free(HI2UF(hi
));
20480 * Return TRUE if a function "name" exists.
20483 function_exists(name
)
20490 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
20491 nm
= skipwhite(nm
);
20493 /* Only accept "funcname", "funcname ", "funcname (..." and
20494 * "funcname(...", not "funcname!...". */
20495 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
20497 if (builtin_function(p
))
20498 n
= (find_internal_func(p
) >= 0);
20500 n
= (find_func(p
) != NULL
);
20507 * Return TRUE if "name" looks like a builtin function name: starts with a
20508 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
20511 builtin_function(name
)
20514 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
20515 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
20518 #if defined(FEAT_PROFILE) || defined(PROTO)
20520 * Start profiling function "fp".
20523 func_do_profile(fp
)
20526 fp
->uf_tm_count
= 0;
20527 profile_zero(&fp
->uf_tm_self
);
20528 profile_zero(&fp
->uf_tm_total
);
20529 if (fp
->uf_tml_count
== NULL
)
20530 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
20531 (sizeof(int) * fp
->uf_lines
.ga_len
));
20532 if (fp
->uf_tml_total
== NULL
)
20533 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
20534 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20535 if (fp
->uf_tml_self
== NULL
)
20536 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
20537 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20538 fp
->uf_tml_idx
= -1;
20539 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
20540 || fp
->uf_tml_self
== NULL
)
20541 return; /* out of memory */
20543 fp
->uf_profiling
= TRUE
;
20547 * Dump the profiling results for all functions in file "fd".
20550 func_dump_profile(fd
)
20560 todo
= (int)func_hashtab
.ht_used
;
20561 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
20563 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
20565 if (!HASHITEM_EMPTY(hi
))
20569 if (fp
->uf_profiling
)
20571 if (sorttab
!= NULL
)
20572 sorttab
[st_len
++] = fp
;
20574 if (fp
->uf_name
[0] == K_SPECIAL
)
20575 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
20577 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
20578 if (fp
->uf_tm_count
== 1)
20579 fprintf(fd
, "Called 1 time\n");
20581 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
20582 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
20583 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
20585 fprintf(fd
, "count total (s) self (s)\n");
20587 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
20589 if (FUNCLINE(fp
, i
) == NULL
)
20591 prof_func_line(fd
, fp
->uf_tml_count
[i
],
20592 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
20593 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
20600 if (sorttab
!= NULL
&& st_len
> 0)
20602 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20604 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
20605 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20607 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
20612 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
20617 int prefer_self
; /* when equal print only self time */
20622 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
20623 fprintf(fd
, "count total (s) self (s) function\n");
20624 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
20627 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
20629 if (fp
->uf_name
[0] == K_SPECIAL
)
20630 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
20632 fprintf(fd
, " %s()\n", fp
->uf_name
);
20638 * Print the count and times for one function or function line.
20641 prof_func_line(fd
, count
, total
, self
, prefer_self
)
20646 int prefer_self
; /* when equal print only self time */
20650 fprintf(fd
, "%5d ", count
);
20651 if (prefer_self
&& profile_equal(total
, self
))
20654 fprintf(fd
, "%s ", profile_msg(total
));
20655 if (!prefer_self
&& profile_equal(total
, self
))
20658 fprintf(fd
, "%s ", profile_msg(self
));
20665 * Compare function for total time sorting.
20668 #ifdef __BORLANDC__
20671 prof_total_cmp(s1
, s2
)
20677 p1
= *(ufunc_T
**)s1
;
20678 p2
= *(ufunc_T
**)s2
;
20679 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
20683 * Compare function for self time sorting.
20686 #ifdef __BORLANDC__
20689 prof_self_cmp(s1
, s2
)
20695 p1
= *(ufunc_T
**)s1
;
20696 p2
= *(ufunc_T
**)s2
;
20697 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
20703 * If "name" has a package name try autoloading the script for it.
20704 * Return TRUE if a package was loaded.
20707 script_autoload(name
, reload
)
20709 int reload
; /* load script again when already loaded */
20712 char_u
*scriptname
, *tofree
;
20716 /* If there is no '#' after name[0] there is no package name. */
20717 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
20718 if (p
== NULL
|| p
== name
)
20721 tofree
= scriptname
= autoload_name(name
);
20723 /* Find the name in the list of previously loaded package names. Skip
20724 * "autoload/", it's always the same. */
20725 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
20726 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
20728 if (!reload
&& i
< ga_loaded
.ga_len
)
20729 ret
= FALSE
; /* was loaded already */
20732 /* Remember the name if it wasn't loaded already. */
20733 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
20735 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
20739 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
20740 if (source_runtime(scriptname
, FALSE
) == OK
)
20749 * Return the autoload script name for a function or variable name.
20750 * Returns NULL when out of memory.
20753 autoload_name(name
)
20757 char_u
*scriptname
;
20759 /* Get the script file name: replace '#' with '/', append ".vim". */
20760 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
20761 if (scriptname
== NULL
)
20763 STRCPY(scriptname
, "autoload/");
20764 STRCAT(scriptname
, name
);
20765 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
20766 STRCAT(scriptname
, ".vim");
20767 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
20772 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20775 * Function given to ExpandGeneric() to obtain the list of user defined
20779 get_user_func_name(xp
, idx
)
20783 static long_u done
;
20784 static hashitem_T
*hi
;
20790 hi
= func_hashtab
.ht_array
;
20792 if (done
< func_hashtab
.ht_used
)
20796 while (HASHITEM_EMPTY(hi
))
20800 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
20801 return fp
->uf_name
; /* prevents overflow */
20803 cat_func_name(IObuff
, fp
);
20804 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
20806 STRCAT(IObuff
, "(");
20807 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
20808 STRCAT(IObuff
, ")");
20815 #endif /* FEAT_CMDL_COMPL */
20818 * Copy the function name of "fp" to buffer "buf".
20819 * "buf" must be able to hold the function name plus three bytes.
20820 * Takes care of script-local function names.
20823 cat_func_name(buf
, fp
)
20827 if (fp
->uf_name
[0] == K_SPECIAL
)
20829 STRCPY(buf
, "<SNR>");
20830 STRCAT(buf
, fp
->uf_name
+ 3);
20833 STRCPY(buf
, fp
->uf_name
);
20837 * ":delfunction {name}"
20840 ex_delfunction(eap
)
20843 ufunc_T
*fp
= NULL
;
20849 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
20850 vim_free(fudi
.fd_newkey
);
20853 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
20854 EMSG(_(e_funcref
));
20857 if (!ends_excmd(*skipwhite(p
)))
20860 EMSG(_(e_trailing
));
20863 eap
->nextcmd
= check_nextcmd(p
);
20864 if (eap
->nextcmd
!= NULL
)
20868 fp
= find_func(name
);
20875 EMSG2(_(e_nofunc
), eap
->arg
);
20878 if (fp
->uf_calls
> 0)
20880 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
20884 if (fudi
.fd_dict
!= NULL
)
20886 /* Delete the dict item that refers to the function, it will
20887 * invoke func_unref() and possibly delete the function. */
20888 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
20896 * Free a function and remove it from the list of functions.
20904 /* clear this function */
20905 ga_clear_strings(&(fp
->uf_args
));
20906 ga_clear_strings(&(fp
->uf_lines
));
20907 #ifdef FEAT_PROFILE
20908 vim_free(fp
->uf_tml_count
);
20909 vim_free(fp
->uf_tml_total
);
20910 vim_free(fp
->uf_tml_self
);
20913 /* remove the function from the function hashtable */
20914 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
20915 if (HASHITEM_EMPTY(hi
))
20916 EMSG2(_(e_intern2
), "func_free()");
20918 hash_remove(&func_hashtab
, hi
);
20924 * Unreference a Function: decrement the reference count and free it when it
20925 * becomes zero. Only for numbered functions.
20933 if (name
!= NULL
&& isdigit(*name
))
20935 fp
= find_func(name
);
20937 EMSG2(_(e_intern2
), "func_unref()");
20938 else if (--fp
->uf_refcount
<= 0)
20940 /* Only delete it when it's not being used. Otherwise it's done
20941 * when "uf_calls" becomes zero. */
20942 if (fp
->uf_calls
== 0)
20949 * Count a reference to a Function.
20957 if (name
!= NULL
&& isdigit(*name
))
20959 fp
= find_func(name
);
20961 EMSG2(_(e_intern2
), "func_ref()");
20968 * Call a user function.
20971 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
20972 ufunc_T
*fp
; /* pointer to function */
20973 int argcount
; /* nr of args */
20974 typval_T
*argvars
; /* arguments */
20975 typval_T
*rettv
; /* return value */
20976 linenr_T firstline
; /* first line of range */
20977 linenr_T lastline
; /* last line of range */
20978 dict_T
*selfdict
; /* Dictionary for "self" */
20980 char_u
*save_sourcing_name
;
20981 linenr_T save_sourcing_lnum
;
20982 scid_T save_current_SID
;
20985 static int depth
= 0;
20987 int fixvar_idx
= 0; /* index in fixvar[] */
20990 char_u numbuf
[NUMBUFLEN
];
20992 #ifdef FEAT_PROFILE
20993 proftime_T wait_start
;
20994 proftime_T call_start
;
20997 /* If depth of calling is getting too high, don't execute the function */
20998 if (depth
>= p_mfd
)
21000 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
21001 rettv
->v_type
= VAR_NUMBER
;
21002 rettv
->vval
.v_number
= -1;
21007 line_breakcheck(); /* check for CTRL-C hit */
21009 fc
.caller
= current_funccal
;
21010 current_funccal
= &fc
;
21013 rettv
->vval
.v_number
= 0;
21015 fc
.returned
= FALSE
;
21016 fc
.level
= ex_nesting_level
;
21017 /* Check if this function has a breakpoint. */
21018 fc
.breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
21019 fc
.dbg_tick
= debug_tick
;
21022 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21023 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21024 * each argument variable and saves a lot of time.
21027 * Init l: variables.
21029 init_var_dict(&fc
.l_vars
, &fc
.l_vars_var
);
21030 if (selfdict
!= NULL
)
21032 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21033 * some compiler that checks the destination size. */
21034 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21036 STRCPY(name
, "self");
21037 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
21038 hash_add(&fc
.l_vars
.dv_hashtab
, DI2HIKEY(v
));
21039 v
->di_tv
.v_type
= VAR_DICT
;
21040 v
->di_tv
.v_lock
= 0;
21041 v
->di_tv
.vval
.v_dict
= selfdict
;
21042 ++selfdict
->dv_refcount
;
21046 * Init a: variables.
21047 * Set a:0 to "argcount".
21048 * Set a:000 to a list with room for the "..." arguments.
21050 init_var_dict(&fc
.l_avars
, &fc
.l_avars_var
);
21051 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "0",
21052 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
21053 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21054 STRCPY(v
->di_key
, "000");
21055 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21056 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
21057 v
->di_tv
.v_type
= VAR_LIST
;
21058 v
->di_tv
.v_lock
= VAR_FIXED
;
21059 v
->di_tv
.vval
.v_list
= &fc
.l_varlist
;
21060 vim_memset(&fc
.l_varlist
, 0, sizeof(list_T
));
21061 fc
.l_varlist
.lv_refcount
= 99999;
21062 fc
.l_varlist
.lv_lock
= VAR_FIXED
;
21065 * Set a:firstline to "firstline" and a:lastline to "lastline".
21066 * Set a:name to named arguments.
21067 * Set a:N to the "..." arguments.
21069 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "firstline",
21070 (varnumber_T
)firstline
);
21071 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "lastline",
21072 (varnumber_T
)lastline
);
21073 for (i
= 0; i
< argcount
; ++i
)
21075 ai
= i
- fp
->uf_args
.ga_len
;
21077 /* named argument a:name */
21078 name
= FUNCARG(fp
, i
);
21081 /* "..." argument a:1, a:2, etc. */
21082 sprintf((char *)numbuf
, "%d", ai
+ 1);
21085 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
21087 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21088 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21092 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
21096 v
->di_flags
= DI_FLAGS_RO
;
21098 STRCPY(v
->di_key
, name
);
21099 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
21101 /* Note: the values are copied directly to avoid alloc/free.
21102 * "argvars" must have VAR_FIXED for v_lock. */
21103 v
->di_tv
= argvars
[i
];
21104 v
->di_tv
.v_lock
= VAR_FIXED
;
21106 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
21108 list_append(&fc
.l_varlist
, &fc
.l_listitems
[ai
]);
21109 fc
.l_listitems
[ai
].li_tv
= argvars
[i
];
21110 fc
.l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
21114 /* Don't redraw while executing the function. */
21115 ++RedrawingDisabled
;
21116 save_sourcing_name
= sourcing_name
;
21117 save_sourcing_lnum
= sourcing_lnum
;
21119 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
21120 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
21121 if (sourcing_name
!= NULL
)
21123 if (save_sourcing_name
!= NULL
21124 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
21125 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
21127 STRCPY(sourcing_name
, "function ");
21128 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
21130 if (p_verbose
>= 12)
21133 verbose_enter_scroll();
21135 smsg((char_u
*)_("calling %s"), sourcing_name
);
21136 if (p_verbose
>= 14)
21138 char_u buf
[MSG_BUF_LEN
];
21139 char_u numbuf2
[NUMBUFLEN
];
21143 msg_puts((char_u
*)"(");
21144 for (i
= 0; i
< argcount
; ++i
)
21147 msg_puts((char_u
*)", ");
21148 if (argvars
[i
].v_type
== VAR_NUMBER
)
21149 msg_outnum((long)argvars
[i
].vval
.v_number
);
21152 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
21155 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21161 msg_puts((char_u
*)")");
21163 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21165 verbose_leave_scroll();
21169 #ifdef FEAT_PROFILE
21170 if (do_profiling
== PROF_YES
)
21172 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
21173 func_do_profile(fp
);
21174 if (fp
->uf_profiling
21175 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
))
21178 profile_start(&call_start
);
21179 profile_zero(&fp
->uf_tm_children
);
21181 script_prof_save(&wait_start
);
21185 save_current_SID
= current_SID
;
21186 current_SID
= fp
->uf_script_ID
;
21187 save_did_emsg
= did_emsg
;
21190 /* call do_cmdline() to execute the lines */
21191 do_cmdline(NULL
, get_func_line
, (void *)&fc
,
21192 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
21194 --RedrawingDisabled
;
21196 /* when the function was aborted because of an error, return -1 */
21197 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
21200 rettv
->v_type
= VAR_NUMBER
;
21201 rettv
->vval
.v_number
= -1;
21204 #ifdef FEAT_PROFILE
21205 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
21206 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)))
21208 profile_end(&call_start
);
21209 profile_sub_wait(&wait_start
, &call_start
);
21210 profile_add(&fp
->uf_tm_total
, &call_start
);
21211 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
21212 if (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)
21214 profile_add(&fc
.caller
->func
->uf_tm_children
, &call_start
);
21215 profile_add(&fc
.caller
->func
->uf_tml_children
, &call_start
);
21220 /* when being verbose, mention the return value */
21221 if (p_verbose
>= 12)
21224 verbose_enter_scroll();
21227 smsg((char_u
*)_("%s aborted"), sourcing_name
);
21228 else if (fc
.rettv
->v_type
== VAR_NUMBER
)
21229 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
21230 (long)fc
.rettv
->vval
.v_number
);
21233 char_u buf
[MSG_BUF_LEN
];
21234 char_u numbuf2
[NUMBUFLEN
];
21238 /* The value may be very long. Skip the middle part, so that we
21239 * have some idea how it starts and ends. smsg() would always
21240 * truncate it at the end. */
21241 s
= tv2string(fc
.rettv
, &tofree
, numbuf2
, 0);
21244 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21245 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
21249 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21251 verbose_leave_scroll();
21255 vim_free(sourcing_name
);
21256 sourcing_name
= save_sourcing_name
;
21257 sourcing_lnum
= save_sourcing_lnum
;
21258 current_SID
= save_current_SID
;
21259 #ifdef FEAT_PROFILE
21260 if (do_profiling
== PROF_YES
)
21261 script_prof_restore(&wait_start
);
21264 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
21267 verbose_enter_scroll();
21269 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
21270 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21272 verbose_leave_scroll();
21276 did_emsg
|= save_did_emsg
;
21277 current_funccal
= fc
.caller
;
21279 /* The a: variables typevals were not allocated, only free the allocated
21281 vars_clear_ext(&fc
.l_avars
.dv_hashtab
, FALSE
);
21283 vars_clear(&fc
.l_vars
.dv_hashtab
); /* free all l: variables */
21288 * Add a number variable "name" to dict "dp" with value "nr".
21291 add_nr_var(dp
, v
, name
, nr
)
21297 STRCPY(v
->di_key
, name
);
21298 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21299 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
21300 v
->di_tv
.v_type
= VAR_NUMBER
;
21301 v
->di_tv
.v_lock
= VAR_FIXED
;
21302 v
->di_tv
.vval
.v_number
= nr
;
21312 char_u
*arg
= eap
->arg
;
21314 int returning
= FALSE
;
21316 if (current_funccal
== NULL
)
21318 EMSG(_("E133: :return not inside a function"));
21325 eap
->nextcmd
= NULL
;
21326 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
21327 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
21330 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
21334 /* It's safer to return also on error. */
21335 else if (!eap
->skip
)
21338 * Return unless the expression evaluation has been cancelled due to an
21339 * aborting error, an interrupt, or an exception.
21342 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
21345 /* When skipping or the return gets pending, advance to the next command
21346 * in this line (!returning). Otherwise, ignore the rest of the line.
21347 * Following lines will be ignored by get_func_line(). */
21349 eap
->nextcmd
= NULL
;
21350 else if (eap
->nextcmd
== NULL
) /* no argument */
21351 eap
->nextcmd
= check_nextcmd(arg
);
21358 * Return from a function. Possibly makes the return pending. Also called
21359 * for a pending return at the ":endtry" or after returning from an extra
21360 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
21361 * when called due to a ":return" command. "rettv" may point to a typval_T
21362 * with the return rettv. Returns TRUE when the return can be carried out,
21363 * FALSE when the return gets pending.
21366 do_return(eap
, reanimate
, is_cmd
, rettv
)
21373 struct condstack
*cstack
= eap
->cstack
;
21376 /* Undo the return. */
21377 current_funccal
->returned
= FALSE
;
21380 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21381 * not in its finally clause (which then is to be executed next) is found.
21382 * In this case, make the ":return" pending for execution at the ":endtry".
21383 * Otherwise, return normally.
21385 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
21388 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
21390 if (!is_cmd
&& !reanimate
)
21391 /* A pending return again gets pending. "rettv" points to an
21392 * allocated variable with the rettv of the original ":return"'s
21393 * argument if present or is NULL else. */
21394 cstack
->cs_rettv
[idx
] = rettv
;
21397 /* When undoing a return in order to make it pending, get the stored
21400 rettv
= current_funccal
->rettv
;
21404 /* Store the value of the pending return. */
21405 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
21406 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
21408 EMSG(_(e_outofmem
));
21411 cstack
->cs_rettv
[idx
] = NULL
;
21415 /* The pending return value could be overwritten by a ":return"
21416 * without argument in a finally clause; reset the default
21418 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
21419 current_funccal
->rettv
->vval
.v_number
= 0;
21422 report_make_pending(CSTP_RETURN
, rettv
);
21426 current_funccal
->returned
= TRUE
;
21428 /* If the return is carried out now, store the return value. For
21429 * a return immediately after reanimation, the value is already
21431 if (!reanimate
&& rettv
!= NULL
)
21433 clear_tv(current_funccal
->rettv
);
21434 *current_funccal
->rettv
= *(typval_T
*)rettv
;
21444 * Free the variable with a pending return value.
21447 discard_pending_return(rettv
)
21450 free_tv((typval_T
*)rettv
);
21454 * Generate a return command for producing the value of "rettv". The result
21455 * is an allocated string. Used by report_pending() for verbose messages.
21458 get_return_cmd(rettv
)
21462 char_u
*tofree
= NULL
;
21463 char_u numbuf
[NUMBUFLEN
];
21466 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
21470 STRCPY(IObuff
, ":return ");
21471 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
21472 if (STRLEN(s
) + 8 >= IOSIZE
)
21473 STRCPY(IObuff
+ IOSIZE
- 4, "...");
21475 return vim_strsave(IObuff
);
21479 * Get next function line.
21480 * Called by do_cmdline() to get the next line.
21481 * Returns allocated string, or NULL for end of function.
21485 get_func_line(c
, cookie
, indent
)
21486 int c
; /* not used */
21488 int indent
; /* not used */
21490 funccall_T
*fcp
= (funccall_T
*)cookie
;
21491 ufunc_T
*fp
= fcp
->func
;
21493 garray_T
*gap
; /* growarray with function lines */
21495 /* If breakpoints have been added/deleted need to check for it. */
21496 if (fcp
->dbg_tick
!= debug_tick
)
21498 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21500 fcp
->dbg_tick
= debug_tick
;
21502 #ifdef FEAT_PROFILE
21503 if (do_profiling
== PROF_YES
)
21504 func_line_end(cookie
);
21507 gap
= &fp
->uf_lines
;
21508 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21513 /* Skip NULL lines (continuation lines). */
21514 while (fcp
->linenr
< gap
->ga_len
21515 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
21517 if (fcp
->linenr
>= gap
->ga_len
)
21521 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
21522 sourcing_lnum
= fcp
->linenr
;
21523 #ifdef FEAT_PROFILE
21524 if (do_profiling
== PROF_YES
)
21525 func_line_start(cookie
);
21530 /* Did we encounter a breakpoint? */
21531 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
21533 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
21534 /* Find next breakpoint. */
21535 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21537 fcp
->dbg_tick
= debug_tick
;
21543 #if defined(FEAT_PROFILE) || defined(PROTO)
21545 * Called when starting to read a function line.
21546 * "sourcing_lnum" must be correct!
21547 * When skipping lines it may not actually be executed, but we won't find out
21548 * until later and we need to store the time now.
21551 func_line_start(cookie
)
21554 funccall_T
*fcp
= (funccall_T
*)cookie
;
21555 ufunc_T
*fp
= fcp
->func
;
21557 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
21558 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
21560 fp
->uf_tml_idx
= sourcing_lnum
- 1;
21561 /* Skip continuation lines. */
21562 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
21564 fp
->uf_tml_execed
= FALSE
;
21565 profile_start(&fp
->uf_tml_start
);
21566 profile_zero(&fp
->uf_tml_children
);
21567 profile_get_wait(&fp
->uf_tml_wait
);
21572 * Called when actually executing a function line.
21575 func_line_exec(cookie
)
21578 funccall_T
*fcp
= (funccall_T
*)cookie
;
21579 ufunc_T
*fp
= fcp
->func
;
21581 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21582 fp
->uf_tml_execed
= TRUE
;
21586 * Called when done with a function line.
21589 func_line_end(cookie
)
21592 funccall_T
*fcp
= (funccall_T
*)cookie
;
21593 ufunc_T
*fp
= fcp
->func
;
21595 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21597 if (fp
->uf_tml_execed
)
21599 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
21600 profile_end(&fp
->uf_tml_start
);
21601 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
21602 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
21603 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
21604 &fp
->uf_tml_children
);
21606 fp
->uf_tml_idx
= -1;
21612 * Return TRUE if the currently active function should be ended, because a
21613 * return was encountered or an error occurred. Used inside a ":while".
21616 func_has_ended(cookie
)
21619 funccall_T
*fcp
= (funccall_T
*)cookie
;
21621 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21622 * an error inside a try conditional. */
21623 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21628 * return TRUE if cookie indicates a function which "abort"s on errors.
21631 func_has_abort(cookie
)
21634 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
21637 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21640 VAR_FLAVOUR_DEFAULT
, /* doesn't start with uppercase */
21641 VAR_FLAVOUR_SESSION
, /* starts with uppercase, some lower */
21642 VAR_FLAVOUR_VIMINFO
/* all uppercase */
21645 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
21647 static var_flavour_T
21648 var_flavour(varname
)
21651 char_u
*p
= varname
;
21653 if (ASCII_ISUPPER(*p
))
21656 if (ASCII_ISLOWER(*p
))
21657 return VAR_FLAVOUR_SESSION
;
21658 return VAR_FLAVOUR_VIMINFO
;
21661 return VAR_FLAVOUR_DEFAULT
;
21665 #if defined(FEAT_VIMINFO) || defined(PROTO)
21667 * Restore global vars that start with a capital from the viminfo file
21670 read_viminfo_varlist(virp
, writing
)
21675 int type
= VAR_NUMBER
;
21678 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
21680 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
21683 *tab
++ = '\0'; /* isolate the variable name */
21684 if (*tab
== 'S') /* string var */
21687 else if (*tab
== 'F')
21691 tab
= vim_strchr(tab
, '\t');
21695 if (type
== VAR_STRING
)
21696 tv
.vval
.v_string
= viminfo_readstring(virp
,
21697 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
21699 else if (type
== VAR_FLOAT
)
21700 (void)string2float(tab
+ 1, &tv
.vval
.v_float
);
21703 tv
.vval
.v_number
= atol((char *)tab
+ 1);
21704 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
21705 if (type
== VAR_STRING
)
21706 vim_free(tv
.vval
.v_string
);
21711 return viminfo_readline(virp
);
21715 * Write global vars that start with a capital to the viminfo file
21718 write_viminfo_varlist(fp
)
21722 dictitem_T
*this_var
;
21727 char_u numbuf
[NUMBUFLEN
];
21729 if (find_viminfo_parameter('!') == NULL
)
21732 fprintf(fp
, _("\n# global variables:\n"));
21734 todo
= (int)globvarht
.ht_used
;
21735 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21737 if (!HASHITEM_EMPTY(hi
))
21740 this_var
= HI2DI(hi
);
21741 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
21743 switch (this_var
->di_tv
.v_type
)
21745 case VAR_STRING
: s
= "STR"; break;
21746 case VAR_NUMBER
: s
= "NUM"; break;
21748 case VAR_FLOAT
: s
= "FLO"; break;
21752 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
21753 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
21755 viminfo_writestring(fp
, p
);
21763 #if defined(FEAT_SESSION) || defined(PROTO)
21765 store_session_globals(fd
)
21769 dictitem_T
*this_var
;
21773 todo
= (int)globvarht
.ht_used
;
21774 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21776 if (!HASHITEM_EMPTY(hi
))
21779 this_var
= HI2DI(hi
);
21780 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
21781 || this_var
->di_tv
.v_type
== VAR_STRING
)
21782 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
21784 /* Escape special characters with a backslash. Turn a LF and
21785 * CR into \n and \r. */
21786 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
21787 (char_u
*)"\\\"\n\r");
21788 if (p
== NULL
) /* out of memory */
21790 for (t
= p
; *t
!= NUL
; ++t
)
21793 else if (*t
== '\r')
21795 if ((fprintf(fd
, "let %s = %c%s%c",
21797 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
21800 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
21802 || put_eol(fd
) == FAIL
)
21810 else if (this_var
->di_tv
.v_type
== VAR_FLOAT
21811 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
21813 float_T f
= this_var
->di_tv
.vval
.v_float
;
21821 if ((fprintf(fd
, "let %s = %c&%f",
21822 this_var
->di_key
, sign
, f
) < 0)
21823 || put_eol(fd
) == FAIL
)
21834 * Display script name where an item was last set.
21835 * Should only be invoked when 'verbose' is non-zero.
21838 last_set_msg(scriptID
)
21845 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
21849 MSG_PUTS(_("\n\tLast set from "));
21857 #endif /* FEAT_EVAL */
21860 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21864 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21866 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
21867 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
21868 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
21871 * Get the short path (8.3) for the filename in "fnamep".
21872 * Only works for a valid file name.
21873 * When the path gets longer "fnamep" is changed and the allocated buffer
21874 * is put in "bufp".
21875 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21876 * Returns OK on success, FAIL on failure.
21879 get_short_pathname(fnamep
, bufp
, fnamelen
)
21888 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
21891 /* If that doesn't work (not enough space), then save the string
21892 * and try again with a new buffer big enough. */
21893 newbuf
= vim_strnsave(*fnamep
, l
);
21894 if (newbuf
== NULL
)
21898 *fnamep
= *bufp
= newbuf
;
21900 /* Really should always succeed, as the buffer is big enough. */
21901 l
= GetShortPathName(*fnamep
, *fnamep
, l
+1);
21909 * Get the short path (8.3) for the filename in "fname". The converted
21910 * path is returned in "bufp".
21912 * Some of the directories specified in "fname" may not exist. This function
21913 * will shorten the existing directories at the beginning of the path and then
21914 * append the remaining non-existing path.
21916 * fname - Pointer to the filename to shorten. On return, contains the
21917 * pointer to the shortened pathname
21918 * bufp - Pointer to an allocated buffer for the filename.
21919 * fnamelen - Length of the filename pointed to by fname
21921 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
21924 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
21929 char_u
*short_fname
, *save_fname
, *pbuf_unused
;
21930 char_u
*endp
, *save_endp
;
21933 int new_len
, sfx_len
;
21937 old_len
= *fnamelen
;
21938 save_fname
= vim_strnsave(*fname
, old_len
);
21939 pbuf_unused
= NULL
;
21940 short_fname
= NULL
;
21942 endp
= save_fname
+ old_len
- 1; /* Find the end of the copy */
21946 * Try shortening the supplied path till it succeeds by removing one
21947 * directory at a time from the tail of the path.
21952 /* go back one path-separator */
21953 while (endp
> save_fname
&& !after_pathsep(save_fname
, endp
+ 1))
21955 if (endp
<= save_fname
)
21956 break; /* processed the complete path */
21959 * Replace the path separator with a NUL and try to shorten the
21964 short_fname
= save_fname
;
21965 len
= STRLEN(short_fname
) + 1;
21966 if (get_short_pathname(&short_fname
, &pbuf_unused
, &len
) == FAIL
)
21971 *endp
= ch
; /* preserve the string */
21974 break; /* successfully shortened the path */
21976 /* failed to shorten the path. Skip the path separator */
21983 * Succeeded in shortening the path. Now concatenate the shortened
21984 * path with the remaining path at the tail.
21987 /* Compute the length of the new path. */
21988 sfx_len
= (int)(save_endp
- endp
) + 1;
21989 new_len
= len
+ sfx_len
;
21991 *fnamelen
= new_len
;
21993 if (new_len
> old_len
)
21995 /* There is not enough space in the currently allocated string,
21996 * copy it to a buffer big enough. */
21997 *fname
= *bufp
= vim_strnsave(short_fname
, new_len
);
21998 if (*fname
== NULL
)
22006 /* Transfer short_fname to the main buffer (it's big enough),
22007 * unless get_short_pathname() did its work in-place. */
22008 *fname
= *bufp
= save_fname
;
22009 if (short_fname
!= save_fname
)
22010 vim_strncpy(save_fname
, short_fname
, len
);
22014 /* concat the not-shortened part of the path */
22015 vim_strncpy(*fname
+ len
, endp
, sfx_len
);
22016 (*fname
)[new_len
] = NUL
;
22020 vim_free(pbuf_unused
);
22021 vim_free(save_fname
);
22027 * Get a pathname for a partial path.
22028 * Returns OK for success, FAIL for failure.
22031 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
22036 int sepcount
, len
, tflen
;
22038 char_u
*pbuf
, *tfname
;
22041 /* Count up the path separators from the RHS.. so we know which part
22042 * of the path to return. */
22044 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
22045 if (vim_ispathsep(*p
))
22048 /* Need full path first (use expand_env() to remove a "~/") */
22049 hasTilde
= (**fnamep
== '~');
22051 pbuf
= tfname
= expand_env_save(*fnamep
);
22053 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
22055 len
= tflen
= (int)STRLEN(tfname
);
22057 if (get_short_pathname(&tfname
, &pbuf
, &len
) == FAIL
)
22062 /* Don't have a valid filename, so shorten the rest of the
22063 * path if we can. This CAN give us invalid 8.3 filenames, but
22064 * there's not a lot of point in guessing what it might be.
22067 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == FAIL
)
22071 /* Count the paths backward to find the beginning of the desired string. */
22072 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
22076 p
-= mb_head_off(tfname
, p
);
22078 if (vim_ispathsep(*p
))
22080 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
22097 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22099 *fnamelen
= (int)STRLEN(p
);
22105 #endif /* WIN3264 */
22108 * Adjust a filename, according to a string of modifiers.
22109 * *fnamep must be NUL terminated when called. When returning, the length is
22110 * determined by *fnamelen.
22111 * Returns VALID_ flags or -1 for failure.
22112 * When there is an error, *fnamep is set to NULL.
22115 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
22116 char_u
*src
; /* string with modifiers */
22117 int *usedlen
; /* characters after src that are used */
22118 char_u
**fnamep
; /* file name so far */
22119 char_u
**bufp
; /* buffer for allocated file name or NULL */
22120 int *fnamelen
; /* length of fnamep */
22124 char_u
*s
, *p
, *pbuf
;
22125 char_u dirname
[MAXPATHL
];
22127 int has_fullname
= 0;
22129 int has_shortname
= 0;
22133 /* ":p" - full path/file_name */
22134 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
22138 valid
|= VALID_PATH
;
22141 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22142 if ((*fnamep
)[0] == '~'
22143 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22144 && ((*fnamep
)[1] == '/'
22145 # ifdef BACKSLASH_IN_FILENAME
22146 || (*fnamep
)[1] == '\\'
22148 || (*fnamep
)[1] == NUL
)
22153 *fnamep
= expand_env_save(*fnamep
);
22154 vim_free(*bufp
); /* free any allocated file name */
22156 if (*fnamep
== NULL
)
22160 /* When "/." or "/.." is used: force expansion to get rid of it. */
22161 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
22163 if (vim_ispathsep(*p
)
22166 || vim_ispathsep(p
[2])
22168 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
22172 /* FullName_save() is slow, don't use it when not needed. */
22173 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
22175 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
22176 vim_free(*bufp
); /* free any allocated file name */
22178 if (*fnamep
== NULL
)
22182 /* Append a path separator to a directory. */
22183 if (mch_isdir(*fnamep
))
22185 /* Make room for one or two extra characters. */
22186 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
22187 vim_free(*bufp
); /* free any allocated file name */
22189 if (*fnamep
== NULL
)
22191 add_pathsep(*fnamep
);
22195 /* ":." - path relative to the current directory */
22196 /* ":~" - path relative to the home directory */
22197 /* ":8" - shortname path - postponed till after */
22198 while (src
[*usedlen
] == ':'
22199 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
22205 has_shortname
= 1; /* Postpone this. */
22210 /* Need full path first (use expand_env() to remove a "~/") */
22213 if (c
== '.' && **fnamep
== '~')
22214 p
= pbuf
= expand_env_save(*fnamep
);
22216 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
22227 mch_dirname(dirname
, MAXPATHL
);
22228 s
= shorten_fname(p
, dirname
);
22234 vim_free(*bufp
); /* free any allocated file name */
22242 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
22243 /* Only replace it when it starts with '~' */
22244 if (*dirname
== '~')
22246 s
= vim_strsave(dirname
);
22259 tail
= gettail(*fnamep
);
22260 *fnamelen
= (int)STRLEN(*fnamep
);
22262 /* ":h" - head, remove "/file_name", can be repeated */
22263 /* Don't remove the first "/" or "c:\" */
22264 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
22266 valid
|= VALID_HEAD
;
22268 s
= get_past_head(*fnamep
);
22269 while (tail
> s
&& after_pathsep(s
, tail
))
22270 mb_ptr_back(*fnamep
, tail
);
22271 *fnamelen
= (int)(tail
- *fnamep
);
22274 *fnamelen
+= 1; /* the path separator is part of the path */
22276 if (*fnamelen
== 0)
22278 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22279 p
= vim_strsave((char_u
*)".");
22283 *bufp
= *fnamep
= tail
= p
;
22288 while (tail
> s
&& !after_pathsep(s
, tail
))
22289 mb_ptr_back(*fnamep
, tail
);
22293 /* ":8" - shortname */
22294 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
22303 /* Check shortname after we have done 'heads' and before we do 'tails'
22308 /* Copy the string if it is shortened by :h */
22309 if (*fnamelen
< (int)STRLEN(*fnamep
))
22311 p
= vim_strnsave(*fnamep
, *fnamelen
);
22315 *bufp
= *fnamep
= p
;
22318 /* Split into two implementations - makes it easier. First is where
22319 * there isn't a full name already, second is where there is.
22321 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
22323 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == FAIL
)
22330 /* Simple case, already have the full-name
22331 * Nearly always shorter, so try first time. */
22333 if (get_short_pathname(fnamep
, bufp
, &l
) == FAIL
)
22338 /* Couldn't find the filename.. search the paths.
22341 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == FAIL
)
22347 #endif /* WIN3264 */
22349 /* ":t" - tail, just the basename */
22350 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
22353 *fnamelen
-= (int)(tail
- *fnamep
);
22357 /* ":e" - extension, can be repeated */
22358 /* ":r" - root, without extension, can be repeated */
22359 while (src
[*usedlen
] == ':'
22360 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
22362 /* find a '.' in the tail:
22363 * - for second :e: before the current fname
22364 * - otherwise: The last '.'
22366 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
22369 s
= *fnamep
+ *fnamelen
- 1;
22370 for ( ; s
> tail
; --s
)
22373 if (src
[*usedlen
+ 1] == 'e') /* :e */
22377 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
22380 /* cut version from the extension */
22381 s
= *fnamep
+ *fnamelen
- 1;
22382 for ( ; s
> *fnamep
; --s
)
22386 *fnamelen
= s
- *fnamep
;
22389 else if (*fnamep
<= tail
)
22394 if (s
> tail
) /* remove one extension */
22395 *fnamelen
= (int)(s
- *fnamep
);
22400 /* ":s?pat?foo?" - substitute */
22401 /* ":gs?pat?foo?" - global substitute */
22402 if (src
[*usedlen
] == ':'
22403 && (src
[*usedlen
+ 1] == 's'
22404 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
22413 flags
= (char_u
*)"";
22414 s
= src
+ *usedlen
+ 2;
22415 if (src
[*usedlen
+ 1] == 'g')
22417 flags
= (char_u
*)"g";
22424 /* find end of pattern */
22425 p
= vim_strchr(s
, sep
);
22428 pat
= vim_strnsave(s
, (int)(p
- s
));
22432 /* find end of substitution */
22433 p
= vim_strchr(s
, sep
);
22436 sub
= vim_strnsave(s
, (int)(p
- s
));
22437 str
= vim_strnsave(*fnamep
, *fnamelen
);
22438 if (sub
!= NULL
&& str
!= NULL
)
22440 *usedlen
= (int)(p
+ 1 - src
);
22441 s
= do_string_sub(str
, pat
, sub
, flags
);
22445 *fnamelen
= (int)STRLEN(s
);
22457 /* after using ":s", repeat all the modifiers */
22467 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22468 * "flags" can be "g" to do a global substitute.
22469 * Returns an allocated string, NULL for error.
22472 do_string_sub(str
, pat
, sub
, flags
)
22479 regmatch_T regmatch
;
22487 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22489 p_cpo
= (char_u
*)"";
22491 ga_init2(&ga
, 1, 200);
22493 do_all
= (flags
[0] == 'g');
22495 regmatch
.rm_ic
= p_ic
;
22496 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
22497 if (regmatch
.regprog
!= NULL
)
22500 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
22503 * Get some space for a temporary buffer to do the substitution
22504 * into. It will contain:
22505 * - The text up to where the match is.
22506 * - The substituted text.
22507 * - The text after the match.
22509 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
22510 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
22511 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
22517 /* copy the text up to where the match is */
22518 i
= (int)(regmatch
.startp
[0] - tail
);
22519 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
22520 /* add the substituted text */
22521 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
22522 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
22523 ga
.ga_len
+= i
+ sublen
- 1;
22524 /* avoid getting stuck on a match with an empty string */
22525 if (tail
== regmatch
.endp
[0])
22529 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
22534 tail
= regmatch
.endp
[0];
22542 if (ga
.ga_data
!= NULL
)
22543 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
22545 vim_free(regmatch
.regprog
);
22548 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
22555 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */