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
));
409 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
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
) == 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
) == 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
)
4636 int use_float
= FALSE
;
4642 * Get the first variable.
4644 if (eval7(arg
, rettv
, evaluate
) == FAIL
)
4648 * Repeat computing, until no '*', '/' or '%' is following.
4653 if (op
!= '*' && op
!= '/' && op
!= '%')
4659 if (rettv
->v_type
== VAR_FLOAT
)
4661 f1
= rettv
->vval
.v_float
;
4667 n1
= get_tv_number_chk(rettv
, &error
);
4676 * Get the second variable.
4678 *arg
= skipwhite(*arg
+ 1);
4679 if (eval7(arg
, &var2
, evaluate
) == FAIL
)
4685 if (var2
.v_type
== VAR_FLOAT
)
4692 f2
= var2
.vval
.v_float
;
4698 n2
= get_tv_number_chk(&var2
, &error
);
4709 * Compute the result.
4710 * When either side is a float the result is a float.
4719 /* We rely on the floating point library to handle divide
4720 * by zero to result in "inf" and not a crash. */
4725 EMSG(_("E804: Cannot use % with float"));
4728 rettv
->v_type
= VAR_FLOAT
;
4729 rettv
->vval
.v_float
= f1
;
4738 if (n2
== 0) /* give an error message? */
4741 n1
= -0x7fffffffL
- 1L; /* similar to NaN */
4752 if (n2
== 0) /* give an error message? */
4757 rettv
->v_type
= VAR_NUMBER
;
4758 rettv
->vval
.v_number
= n1
;
4767 * Handle sixth level expression:
4768 * number number constant
4769 * "string" string constant
4770 * 'string' literal string constant
4771 * &option-name option value
4772 * @r register contents
4773 * identifier variable value
4774 * function() function call
4775 * $VAR environment variable
4776 * (expression) nested expression
4778 * {key: val, key: val} Dictionary
4781 * ! in front logical NOT
4782 * - in front unary minus
4783 * + in front unary plus (ignored)
4784 * trailing [] subscript in String or List
4785 * trailing .name entry in Dictionary
4787 * "arg" must point to the first non-white of the expression.
4788 * "arg" is advanced to the next non-white after the recognized expression.
4790 * Return OK or FAIL.
4793 eval7(arg
, rettv
, evaluate
)
4801 char_u
*start_leader
, *end_leader
;
4806 * Initialise variable so that clear_tv() can't mistake this for a
4807 * string and free a string that isn't there.
4809 rettv
->v_type
= VAR_UNKNOWN
;
4812 * Skip '!' and '-' characters. They are handled later.
4814 start_leader
= *arg
;
4815 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4816 *arg
= skipwhite(*arg
+ 1);
4836 char_u
*p
= skipdigits(*arg
+ 1);
4837 int get_float
= FALSE
;
4839 /* We accept a float when the format matches
4840 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4841 * strict to avoid backwards compatibility problems. */
4842 if (p
[0] == '.' && vim_isdigit(p
[1]))
4845 p
= skipdigits(p
+ 2);
4846 if (*p
== 'e' || *p
== 'E')
4849 if (*p
== '-' || *p
== '+')
4851 if (!vim_isdigit(*p
))
4854 p
= skipdigits(p
+ 1);
4856 if (ASCII_ISALPHA(*p
) || *p
== '.')
4863 *arg
+= string2float(*arg
, &f
);
4866 rettv
->v_type
= VAR_FLOAT
;
4867 rettv
->vval
.v_float
= f
;
4873 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4877 rettv
->v_type
= VAR_NUMBER
;
4878 rettv
->vval
.v_number
= n
;
4885 * String constant: "string".
4887 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4891 * Literal string constant: 'str''ing'.
4893 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4897 * List: [expr, expr]
4899 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4903 * Dictionary: {key: val, key: val}
4905 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4909 * Option value: &name
4911 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4915 * Environment variable: $VAR.
4917 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4921 * Register contents: @r.
4926 rettv
->v_type
= VAR_STRING
;
4927 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4934 * nested expression: (expression).
4936 case '(': *arg
= skipwhite(*arg
+ 1);
4937 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4942 EMSG(_("E110: Missing ')'"));
4948 default: ret
= NOTDONE
;
4955 * Must be a variable or function name.
4956 * Can also be a curly-braces kind of name: {expr}.
4959 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
4967 if (**arg
== '(') /* recursive! */
4969 /* If "s" is the name of a variable of type VAR_FUNC
4970 * use its contents. */
4971 s
= deref_func_name(s
, &len
);
4973 /* Invoke the function. */
4974 ret
= get_func_tv(s
, len
, rettv
, arg
,
4975 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
4976 &len
, evaluate
, NULL
);
4977 /* Stop the expression evaluation when immediately
4978 * aborting on error, or when an interrupt occurred or
4979 * an exception was thrown but not caught. */
4988 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
4997 *arg
= skipwhite(*arg
);
4999 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5002 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
5005 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5007 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
5014 if (rettv
->v_type
== VAR_FLOAT
)
5015 f
= rettv
->vval
.v_float
;
5018 val
= get_tv_number_chk(rettv
, &error
);
5026 while (end_leader
> start_leader
)
5029 if (*end_leader
== '!')
5032 if (rettv
->v_type
== VAR_FLOAT
)
5038 else if (*end_leader
== '-')
5041 if (rettv
->v_type
== VAR_FLOAT
)
5049 if (rettv
->v_type
== VAR_FLOAT
)
5052 rettv
->vval
.v_float
= f
;
5058 rettv
->v_type
= VAR_NUMBER
;
5059 rettv
->vval
.v_number
= val
;
5068 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5069 * "*arg" points to the '[' or '.'.
5070 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5073 eval_index(arg
, rettv
, evaluate
, verbose
)
5077 int verbose
; /* give error messages */
5079 int empty1
= FALSE
, empty2
= FALSE
;
5080 typval_T var1
, var2
;
5087 if (rettv
->v_type
== VAR_FUNC
5089 || rettv
->v_type
== VAR_FLOAT
5094 EMSG(_("E695: Cannot index a Funcref"));
5104 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
5108 *arg
= skipwhite(key
+ len
);
5115 * Get the (first) variable from inside the [].
5117 *arg
= skipwhite(*arg
+ 1);
5120 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
5122 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
5124 /* not a number or string */
5130 * Get the second variable from inside the [:].
5135 *arg
= skipwhite(*arg
+ 1);
5138 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
5144 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
5146 /* not a number or string */
5154 /* Check for the ']'. */
5158 EMSG(_(e_missbrac
));
5164 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
5170 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
5172 n1
= get_tv_number(&var1
);
5181 n2
= get_tv_number(&var2
);
5186 switch (rettv
->v_type
)
5190 s
= get_tv_string(rettv
);
5191 len
= (long)STRLEN(s
);
5194 /* The resulting variable is a substring. If the indexes
5195 * are out of range the result is empty. */
5206 if (n1
>= len
|| n2
< 0 || n1
> n2
)
5209 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
5213 /* The resulting variable is a string of a single
5214 * character. If the index is too big or negative the
5215 * result is empty. */
5216 if (n1
>= len
|| n1
< 0)
5219 s
= vim_strnsave(s
+ n1
, 1);
5222 rettv
->v_type
= VAR_STRING
;
5223 rettv
->vval
.v_string
= s
;
5227 len
= list_len(rettv
->vval
.v_list
);
5230 if (!empty1
&& (n1
< 0 || n1
>= len
))
5232 /* For a range we allow invalid values and return an empty
5233 * list. A list index out of range is an error. */
5237 EMSGN(_(e_listidx
), n1
);
5251 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
5256 for (item
= list_find(rettv
->vval
.v_list
, n1
);
5259 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
5264 item
= item
->li_next
;
5267 rettv
->v_type
= VAR_LIST
;
5268 rettv
->vval
.v_list
= l
;
5273 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
5283 EMSG(_(e_dictrange
));
5293 key
= get_tv_string(&var1
);
5297 EMSG(_(e_emptykey
));
5303 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
5305 if (item
== NULL
&& verbose
)
5306 EMSG2(_(e_dictkey
), key
);
5312 copy_tv(&item
->di_tv
, &var1
);
5324 * Get an option value.
5325 * "arg" points to the '&' or '+' before the option name.
5326 * "arg" is advanced to character after the option name.
5327 * Return OK or FAIL.
5330 get_option_tv(arg
, rettv
, evaluate
)
5332 typval_T
*rettv
; /* when NULL, only check if option exists */
5340 int working
= (**arg
== '+'); /* has("+option") */
5345 * Isolate the option name and find its value.
5347 option_end
= find_option_end(arg
, &opt_flags
);
5348 if (option_end
== NULL
)
5351 EMSG2(_("E112: Option name missing: %s"), *arg
);
5363 opt_type
= get_option_value(*arg
, &numval
,
5364 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5366 if (opt_type
== -3) /* invalid name */
5369 EMSG2(_("E113: Unknown option: %s"), *arg
);
5372 else if (rettv
!= NULL
)
5374 if (opt_type
== -2) /* hidden string option */
5376 rettv
->v_type
= VAR_STRING
;
5377 rettv
->vval
.v_string
= NULL
;
5379 else if (opt_type
== -1) /* hidden number option */
5381 rettv
->v_type
= VAR_NUMBER
;
5382 rettv
->vval
.v_number
= 0;
5384 else if (opt_type
== 1) /* number option */
5386 rettv
->v_type
= VAR_NUMBER
;
5387 rettv
->vval
.v_number
= numval
;
5389 else /* string option */
5391 rettv
->v_type
= VAR_STRING
;
5392 rettv
->vval
.v_string
= stringval
;
5395 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5398 *option_end
= c
; /* put back for error messages */
5405 * Allocate a variable for a string constant.
5406 * Return OK or FAIL.
5409 get_string_tv(arg
, rettv
, evaluate
)
5419 * Find the end of the string, skipping backslashed characters.
5421 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5423 if (*p
== '\\' && p
[1] != NUL
)
5426 /* A "\<x>" form occupies at least 4 characters, and produces up
5427 * to 6 characters: reserve space for 2 extra */
5435 EMSG2(_("E114: Missing quote: %s"), *arg
);
5439 /* If only parsing, set *arg and return here */
5447 * Copy the string into allocated memory, handling backslashed
5450 name
= alloc((unsigned)(p
- *arg
+ extra
));
5453 rettv
->v_type
= VAR_STRING
;
5454 rettv
->vval
.v_string
= name
;
5456 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5462 case 'b': *name
++ = BS
; ++p
; break;
5463 case 'e': *name
++ = ESC
; ++p
; break;
5464 case 'f': *name
++ = FF
; ++p
; break;
5465 case 'n': *name
++ = NL
; ++p
; break;
5466 case 'r': *name
++ = CAR
; ++p
; break;
5467 case 't': *name
++ = TAB
; ++p
; break;
5469 case 'X': /* hex: "\x1", "\x12" */
5471 case 'u': /* Unicode: "\u0023" */
5473 if (vim_isxdigit(p
[1]))
5476 int c
= toupper(*p
);
5483 while (--n
>= 0 && vim_isxdigit(p
[1]))
5486 nr
= (nr
<< 4) + hex2nr(*p
);
5490 /* For "\u" store the number according to
5493 name
+= (*mb_char2bytes
)(nr
, name
);
5500 /* octal: "\1", "\12", "\123" */
5508 case '7': *name
= *p
++ - '0';
5509 if (*p
>= '0' && *p
<= '7')
5511 *name
= (*name
<< 3) + *p
++ - '0';
5512 if (*p
>= '0' && *p
<= '7')
5513 *name
= (*name
<< 3) + *p
++ - '0';
5518 /* Special key, e.g.: "\<C-W>" */
5519 case '<': extra
= trans_special(&p
, name
, TRUE
);
5527 default: MB_COPY_CHAR(p
, name
);
5532 MB_COPY_CHAR(p
, name
);
5542 * Allocate a variable for a 'str''ing' constant.
5543 * Return OK or FAIL.
5546 get_lit_string_tv(arg
, rettv
, evaluate
)
5556 * Find the end of the string, skipping ''.
5558 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5571 EMSG2(_("E115: Missing quote: %s"), *arg
);
5575 /* If only parsing return after setting "*arg" */
5583 * Copy the string into allocated memory, handling '' to ' reduction.
5585 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5588 rettv
->v_type
= VAR_STRING
;
5589 rettv
->vval
.v_string
= str
;
5591 for (p
= *arg
+ 1; *p
!= NUL
; )
5599 MB_COPY_CHAR(p
, str
);
5608 * Allocate a variable for a List and fill it from "*arg".
5609 * Return OK or FAIL.
5612 get_list_tv(arg
, rettv
, evaluate
)
5628 *arg
= skipwhite(*arg
+ 1);
5629 while (**arg
!= ']' && **arg
!= NUL
)
5631 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5635 item
= listitem_alloc();
5639 item
->li_tv
.v_lock
= 0;
5640 list_append(l
, item
);
5650 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5653 *arg
= skipwhite(*arg
+ 1);
5658 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5665 *arg
= skipwhite(*arg
+ 1);
5668 rettv
->v_type
= VAR_LIST
;
5669 rettv
->vval
.v_list
= l
;
5677 * Allocate an empty header for a list.
5678 * Caller should take care of the reference count.
5685 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5688 /* Prepend the list to the list of lists for garbage collection. */
5689 if (first_list
!= NULL
)
5690 first_list
->lv_used_prev
= l
;
5691 l
->lv_used_prev
= NULL
;
5692 l
->lv_used_next
= first_list
;
5699 * Allocate an empty list for a return value.
5700 * Returns OK or FAIL.
5703 rettv_list_alloc(rettv
)
5706 list_T
*l
= list_alloc();
5711 rettv
->vval
.v_list
= l
;
5712 rettv
->v_type
= VAR_LIST
;
5718 * Unreference a list: decrement the reference count and free it when it
5725 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5730 * Free a list, including all items it points to.
5731 * Ignores the reference count.
5734 list_free(l
, recurse
)
5736 int recurse
; /* Free Lists and Dictionaries recursively. */
5740 /* Remove the list from the list of lists for garbage collection. */
5741 if (l
->lv_used_prev
== NULL
)
5742 first_list
= l
->lv_used_next
;
5744 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5745 if (l
->lv_used_next
!= NULL
)
5746 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5748 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5750 /* Remove the item before deleting it. */
5751 l
->lv_first
= item
->li_next
;
5752 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5753 && item
->li_tv
.v_type
!= VAR_DICT
))
5754 clear_tv(&item
->li_tv
);
5761 * Allocate a list item.
5766 return (listitem_T
*)alloc(sizeof(listitem_T
));
5770 * Free a list item. Also clears the value. Does not notify watchers.
5776 clear_tv(&item
->li_tv
);
5781 * Remove a list item from a List and free it. Also clears the value.
5784 listitem_remove(l
, item
)
5788 list_remove(l
, item
, item
);
5789 listitem_free(item
);
5793 * Get the number of items in a list.
5805 * Return TRUE when two lists have exactly the same values.
5808 list_equal(l1
, l2
, ic
)
5811 int ic
; /* ignore case for strings */
5813 listitem_T
*item1
, *item2
;
5817 if (list_len(l1
) != list_len(l2
))
5820 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5821 item1
!= NULL
&& item2
!= NULL
;
5822 item1
= item1
->li_next
, item2
= item2
->li_next
)
5823 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5825 return item1
== NULL
&& item2
== NULL
;
5828 #if defined(FEAT_PYTHON) || defined(PROTO)
5830 * Return the dictitem that an entry in a hashtable points to.
5841 * Return TRUE when two dictionaries have exactly the same key/values.
5844 dict_equal(d1
, d2
, ic
)
5847 int ic
; /* ignore case for strings */
5855 if (dict_len(d1
) != dict_len(d2
))
5858 todo
= (int)d1
->dv_hashtab
.ht_used
;
5859 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5861 if (!HASHITEM_EMPTY(hi
))
5863 item2
= dict_find(d2
, hi
->hi_key
, -1);
5866 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5875 * Return TRUE if "tv1" and "tv2" have the same value.
5876 * Compares the items just like "==" would compare them, but strings and
5877 * numbers are different. Floats and numbers are also different.
5880 tv_equal(tv1
, tv2
, ic
)
5883 int ic
; /* ignore case */
5885 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5887 static int recursive
= 0; /* cach recursive loops */
5890 if (tv1
->v_type
!= tv2
->v_type
)
5892 /* Catch lists and dicts that have an endless loop by limiting
5893 * recursiveness to 1000. We guess they are equal then. */
5894 if (recursive
>= 1000)
5897 switch (tv1
->v_type
)
5901 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5907 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5912 return (tv1
->vval
.v_string
!= NULL
5913 && tv2
->vval
.v_string
!= NULL
5914 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5917 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5921 return tv1
->vval
.v_float
== tv2
->vval
.v_float
;
5925 s1
= get_tv_string_buf(tv1
, buf1
);
5926 s2
= get_tv_string_buf(tv2
, buf2
);
5927 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5930 EMSG2(_(e_intern2
), "tv_equal()");
5935 * Locate item with index "n" in list "l" and return it.
5936 * A negative index is counted from the end; -1 is the last item.
5937 * Returns NULL when "n" is out of range.
5950 /* Negative index is relative to the end. */
5954 /* Check for index out of range. */
5955 if (n
< 0 || n
>= l
->lv_len
)
5958 /* When there is a cached index may start search from there. */
5959 if (l
->lv_idx_item
!= NULL
)
5961 if (n
< l
->lv_idx
/ 2)
5963 /* closest to the start of the list */
5967 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
5969 /* closest to the end of the list */
5971 idx
= l
->lv_len
- 1;
5975 /* closest to the cached index */
5976 item
= l
->lv_idx_item
;
5982 if (n
< l
->lv_len
/ 2)
5984 /* closest to the start of the list */
5990 /* closest to the end of the list */
5992 idx
= l
->lv_len
- 1;
5998 /* search forward */
5999 item
= item
->li_next
;
6004 /* search backward */
6005 item
= item
->li_prev
;
6009 /* cache the used index */
6011 l
->lv_idx_item
= item
;
6017 * Get list item "l[idx]" as a number.
6020 list_find_nr(l
, idx
, errorp
)
6023 int *errorp
; /* set to TRUE when something wrong */
6027 li
= list_find(l
, idx
);
6034 return get_tv_number_chk(&li
->li_tv
, errorp
);
6038 * Locate "item" list "l" and return its index.
6039 * Returns -1 when "item" is not in the list.
6042 list_idx_of_item(l
, item
)
6052 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
6060 * Append item "item" to the end of list "l".
6063 list_append(l
, item
)
6067 if (l
->lv_last
== NULL
)
6072 item
->li_prev
= NULL
;
6076 l
->lv_last
->li_next
= item
;
6077 item
->li_prev
= l
->lv_last
;
6081 item
->li_next
= NULL
;
6085 * Append typval_T "tv" to the end of list "l".
6086 * Return FAIL when out of memory.
6089 list_append_tv(l
, tv
)
6093 listitem_T
*li
= listitem_alloc();
6097 copy_tv(tv
, &li
->li_tv
);
6103 * Add a dictionary to a list. Used by getqflist().
6104 * Return FAIL when out of memory.
6107 list_append_dict(list
, dict
)
6111 listitem_T
*li
= listitem_alloc();
6115 li
->li_tv
.v_type
= VAR_DICT
;
6116 li
->li_tv
.v_lock
= 0;
6117 li
->li_tv
.vval
.v_dict
= dict
;
6118 list_append(list
, li
);
6119 ++dict
->dv_refcount
;
6124 * Make a copy of "str" and append it as an item to list "l".
6125 * When "len" >= 0 use "str[len]".
6126 * Returns FAIL when out of memory.
6129 list_append_string(l
, str
, len
)
6134 listitem_T
*li
= listitem_alloc();
6139 li
->li_tv
.v_type
= VAR_STRING
;
6140 li
->li_tv
.v_lock
= 0;
6142 li
->li_tv
.vval
.v_string
= NULL
;
6143 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
6144 : vim_strsave(str
))) == NULL
)
6150 * Append "n" to list "l".
6151 * Returns FAIL when out of memory.
6154 list_append_number(l
, n
)
6160 li
= listitem_alloc();
6163 li
->li_tv
.v_type
= VAR_NUMBER
;
6164 li
->li_tv
.v_lock
= 0;
6165 li
->li_tv
.vval
.v_number
= n
;
6171 * Insert typval_T "tv" in list "l" before "item".
6172 * If "item" is NULL append at the end.
6173 * Return FAIL when out of memory.
6176 list_insert_tv(l
, tv
, item
)
6181 listitem_T
*ni
= listitem_alloc();
6185 copy_tv(tv
, &ni
->li_tv
);
6187 /* Append new item at end of list. */
6191 /* Insert new item before existing item. */
6192 ni
->li_prev
= item
->li_prev
;
6194 if (item
->li_prev
== NULL
)
6201 item
->li_prev
->li_next
= ni
;
6202 l
->lv_idx_item
= NULL
;
6211 * Extend "l1" with "l2".
6212 * If "bef" is NULL append at the end, otherwise insert before this item.
6213 * Returns FAIL when out of memory.
6216 list_extend(l1
, l2
, bef
)
6223 for (item
= l2
->lv_first
; item
!= NULL
; item
= item
->li_next
)
6224 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
6230 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6231 * Return FAIL when out of memory.
6234 list_concat(l1
, l2
, tv
)
6241 /* make a copy of the first list. */
6242 l
= list_copy(l1
, FALSE
, 0);
6245 tv
->v_type
= VAR_LIST
;
6246 tv
->vval
.v_list
= l
;
6248 /* append all items from the second list */
6249 return list_extend(l
, l2
, NULL
);
6253 * Make a copy of list "orig". Shallow if "deep" is FALSE.
6254 * The refcount of the new list is set to 1.
6255 * See item_copy() for "copyID".
6256 * Returns NULL when out of memory.
6259 list_copy(orig
, deep
, copyID
)
6271 copy
= list_alloc();
6276 /* Do this before adding the items, because one of the items may
6277 * refer back to this list. */
6278 orig
->lv_copyID
= copyID
;
6279 orig
->lv_copylist
= copy
;
6281 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
6282 item
= item
->li_next
)
6284 ni
= listitem_alloc();
6289 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
6296 copy_tv(&item
->li_tv
, &ni
->li_tv
);
6297 list_append(copy
, ni
);
6299 ++copy
->lv_refcount
;
6311 * Remove items "item" to "item2" from list "l".
6312 * Does not free the listitem or the value!
6315 list_remove(l
, item
, item2
)
6322 /* notify watchers */
6323 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6326 list_fix_watch(l
, ip
);
6331 if (item2
->li_next
== NULL
)
6332 l
->lv_last
= item
->li_prev
;
6334 item2
->li_next
->li_prev
= item
->li_prev
;
6335 if (item
->li_prev
== NULL
)
6336 l
->lv_first
= item2
->li_next
;
6338 item
->li_prev
->li_next
= item2
->li_next
;
6339 l
->lv_idx_item
= NULL
;
6343 * Return an allocated string with the string representation of a list.
6347 list2string(tv
, copyID
)
6353 if (tv
->vval
.v_list
== NULL
)
6355 ga_init2(&ga
, (int)sizeof(char), 80);
6356 ga_append(&ga
, '[');
6357 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6359 vim_free(ga
.ga_data
);
6362 ga_append(&ga
, ']');
6363 ga_append(&ga
, NUL
);
6364 return (char_u
*)ga
.ga_data
;
6368 * Join list "l" into a string in "*gap", using separator "sep".
6369 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6370 * Return FAIL or OK.
6373 list_join(gap
, l
, sep
, echo
, copyID
)
6382 char_u numbuf
[NUMBUFLEN
];
6386 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6391 ga_concat(gap
, sep
);
6394 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6396 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6407 * Garbage collection for lists and dictionaries.
6409 * We use reference counts to be able to free most items right away when they
6410 * are no longer used. But for composite items it's possible that it becomes
6411 * unused while the reference count is > 0: When there is a recursive
6412 * reference. Example:
6413 * :let l = [1, 2, 3]
6417 * Since this is quite unusual we handle this with garbage collection: every
6418 * once in a while find out which lists and dicts are not referenced from any
6421 * Here is a good reference text about garbage collection (refers to Python
6422 * but it applies to all reference-counting mechanisms):
6423 * http://python.ca/nas/python/gc/
6427 * Do garbage collection for lists and dicts.
6428 * Return TRUE if some memory was freed.
6435 int copyID
= ++current_copyID
;
6440 int did_free
= FALSE
;
6445 /* Only do this once. */
6446 want_garbage_collect
= FALSE
;
6447 may_garbage_collect
= FALSE
;
6448 garbage_collect_at_exit
= FALSE
;
6451 * 1. Go through all accessible variables and mark all lists and dicts
6454 /* script-local variables */
6455 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6456 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6458 /* buffer-local variables */
6459 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6460 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6462 /* window-local variables */
6463 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6464 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6467 /* tabpage-local variables */
6468 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6469 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6472 /* global variables */
6473 set_ref_in_ht(&globvarht
, copyID
);
6475 /* function-local variables */
6476 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6478 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6479 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6483 * 2. Go through the list of dicts and free items without the copyID.
6485 for (dd
= first_dict
; dd
!= NULL
; )
6486 if (dd
->dv_copyID
!= copyID
)
6488 /* Free the Dictionary and ordinary items it contains, but don't
6489 * recurse into Lists and Dictionaries, they will be in the list
6490 * of dicts or list of lists. */
6491 dict_free(dd
, FALSE
);
6494 /* restart, next dict may also have been freed */
6498 dd
= dd
->dv_used_next
;
6501 * 3. Go through the list of lists and free items without the copyID.
6502 * But don't free a list that has a watcher (used in a for loop), these
6503 * are not referenced anywhere.
6505 for (ll
= first_list
; ll
!= NULL
; )
6506 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6508 /* Free the List and ordinary items it contains, but don't recurse
6509 * into Lists and Dictionaries, they will be in the list of dicts
6510 * or list of lists. */
6511 list_free(ll
, FALSE
);
6514 /* restart, next list may also have been freed */
6518 ll
= ll
->lv_used_next
;
6524 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6527 set_ref_in_ht(ht
, copyID
)
6534 todo
= (int)ht
->ht_used
;
6535 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6536 if (!HASHITEM_EMPTY(hi
))
6539 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6544 * Mark all lists and dicts referenced through list "l" with "copyID".
6547 set_ref_in_list(l
, copyID
)
6553 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6554 set_ref_in_item(&li
->li_tv
, copyID
);
6558 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6561 set_ref_in_item(tv
, copyID
)
6571 dd
= tv
->vval
.v_dict
;
6572 if (dd
->dv_copyID
!= copyID
)
6574 /* Didn't see this dict yet. */
6575 dd
->dv_copyID
= copyID
;
6576 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6581 ll
= tv
->vval
.v_list
;
6582 if (ll
->lv_copyID
!= copyID
)
6584 /* Didn't see this list yet. */
6585 ll
->lv_copyID
= copyID
;
6586 set_ref_in_list(ll
, copyID
);
6594 * Allocate an empty header for a dictionary.
6601 d
= (dict_T
*)alloc(sizeof(dict_T
));
6604 /* Add the list to the list of dicts for garbage collection. */
6605 if (first_dict
!= NULL
)
6606 first_dict
->dv_used_prev
= d
;
6607 d
->dv_used_next
= first_dict
;
6608 d
->dv_used_prev
= NULL
;
6611 hash_init(&d
->dv_hashtab
);
6620 * Unreference a Dictionary: decrement the reference count and free it when it
6627 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6632 * Free a Dictionary, including all items it contains.
6633 * Ignores the reference count.
6636 dict_free(d
, recurse
)
6638 int recurse
; /* Free Lists and Dictionaries recursively. */
6644 /* Remove the dict from the list of dicts for garbage collection. */
6645 if (d
->dv_used_prev
== NULL
)
6646 first_dict
= d
->dv_used_next
;
6648 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6649 if (d
->dv_used_next
!= NULL
)
6650 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6652 /* Lock the hashtab, we don't want it to resize while freeing items. */
6653 hash_lock(&d
->dv_hashtab
);
6654 todo
= (int)d
->dv_hashtab
.ht_used
;
6655 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6657 if (!HASHITEM_EMPTY(hi
))
6659 /* Remove the item before deleting it, just in case there is
6660 * something recursive causing trouble. */
6662 hash_remove(&d
->dv_hashtab
, hi
);
6663 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6664 && di
->di_tv
.v_type
!= VAR_DICT
))
6665 clear_tv(&di
->di_tv
);
6670 hash_clear(&d
->dv_hashtab
);
6675 * Allocate a Dictionary item.
6676 * The "key" is copied to the new item.
6677 * Note that the value of the item "di_tv" still needs to be initialized!
6678 * Returns NULL when out of memory.
6686 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6689 STRCPY(di
->di_key
, key
);
6696 * Make a copy of a Dictionary item.
6704 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6705 + STRLEN(org
->di_key
)));
6708 STRCPY(di
->di_key
, org
->di_key
);
6710 copy_tv(&org
->di_tv
, &di
->di_tv
);
6716 * Remove item "item" from Dictionary "dict" and free it.
6719 dictitem_remove(dict
, item
)
6725 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6726 if (HASHITEM_EMPTY(hi
))
6727 EMSG2(_(e_intern2
), "dictitem_remove()");
6729 hash_remove(&dict
->dv_hashtab
, hi
);
6730 dictitem_free(item
);
6734 * Free a dict item. Also clears the value.
6740 clear_tv(&item
->di_tv
);
6745 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6746 * The refcount of the new dict is set to 1.
6747 * See item_copy() for "copyID".
6748 * Returns NULL when out of memory.
6751 dict_copy(orig
, deep
, copyID
)
6764 copy
= dict_alloc();
6769 orig
->dv_copyID
= copyID
;
6770 orig
->dv_copydict
= copy
;
6772 todo
= (int)orig
->dv_hashtab
.ht_used
;
6773 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6775 if (!HASHITEM_EMPTY(hi
))
6779 di
= dictitem_alloc(hi
->hi_key
);
6784 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6792 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6793 if (dict_add(copy
, di
) == FAIL
)
6801 ++copy
->dv_refcount
;
6813 * Add item "item" to Dictionary "d".
6814 * Returns FAIL when out of memory and when key already existed.
6821 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6825 * Add a number or string entry to dictionary "d".
6826 * When "str" is NULL use number "nr", otherwise use "str".
6827 * Returns FAIL when out of memory and when key already exists.
6830 dict_add_nr_str(d
, key
, nr
, str
)
6838 item
= dictitem_alloc((char_u
*)key
);
6841 item
->di_tv
.v_lock
= 0;
6844 item
->di_tv
.v_type
= VAR_NUMBER
;
6845 item
->di_tv
.vval
.v_number
= nr
;
6849 item
->di_tv
.v_type
= VAR_STRING
;
6850 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6852 if (dict_add(d
, item
) == FAIL
)
6854 dictitem_free(item
);
6861 * Get the number of items in a Dictionary.
6869 return (long)d
->dv_hashtab
.ht_used
;
6873 * Find item "key[len]" in Dictionary "d".
6874 * If "len" is negative use strlen(key).
6875 * Returns NULL when not found.
6878 dict_find(d
, key
, len
)
6884 char_u buf
[AKEYLEN
];
6886 char_u
*tofree
= NULL
;
6891 else if (len
>= AKEYLEN
)
6893 tofree
= akey
= vim_strnsave(key
, len
);
6899 /* Avoid a malloc/free by using buf[]. */
6900 vim_strncpy(buf
, key
, len
);
6904 hi
= hash_find(&d
->dv_hashtab
, akey
);
6906 if (HASHITEM_EMPTY(hi
))
6912 * Get a string item from a dictionary.
6913 * When "save" is TRUE allocate memory for it.
6914 * Returns NULL if the entry doesn't exist or out of memory.
6917 get_dict_string(d
, key
, save
)
6925 di
= dict_find(d
, key
, -1);
6928 s
= get_tv_string(&di
->di_tv
);
6929 if (save
&& s
!= NULL
)
6935 * Get a number item from a dictionary.
6936 * Returns 0 if the entry doesn't exist or out of memory.
6939 get_dict_number(d
, key
)
6945 di
= dict_find(d
, key
, -1);
6948 return get_tv_number(&di
->di_tv
);
6952 * Return an allocated string with the string representation of a Dictionary.
6956 dict2string(tv
, copyID
)
6963 char_u numbuf
[NUMBUFLEN
];
6969 if ((d
= tv
->vval
.v_dict
) == NULL
)
6971 ga_init2(&ga
, (int)sizeof(char), 80);
6972 ga_append(&ga
, '{');
6974 todo
= (int)d
->dv_hashtab
.ht_used
;
6975 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6977 if (!HASHITEM_EMPTY(hi
))
6984 ga_concat(&ga
, (char_u
*)", ");
6986 tofree
= string_quote(hi
->hi_key
, FALSE
);
6989 ga_concat(&ga
, tofree
);
6992 ga_concat(&ga
, (char_u
*)": ");
6993 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
7003 vim_free(ga
.ga_data
);
7007 ga_append(&ga
, '}');
7008 ga_append(&ga
, NUL
);
7009 return (char_u
*)ga
.ga_data
;
7013 * Allocate a variable for a Dictionary and fill it from "*arg".
7014 * Return OK or FAIL. Returns NOTDONE for {expr}.
7017 get_dict_tv(arg
, rettv
, evaluate
)
7027 char_u
*start
= skipwhite(*arg
+ 1);
7028 char_u buf
[NUMBUFLEN
];
7031 * First check if it's not a curly-braces thing: {expr}.
7032 * Must do this without evaluating, otherwise a function may be called
7033 * twice. Unfortunately this means we need to call eval1() twice for the
7035 * But {} is an empty Dictionary.
7039 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
7051 tvkey
.v_type
= VAR_UNKNOWN
;
7052 tv
.v_type
= VAR_UNKNOWN
;
7054 *arg
= skipwhite(*arg
+ 1);
7055 while (**arg
!= '}' && **arg
!= NUL
)
7057 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
7061 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
7067 key
= get_tv_string_buf_chk(&tvkey
, buf
);
7068 if (key
== NULL
|| *key
== NUL
)
7070 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7072 EMSG(_(e_emptykey
));
7078 *arg
= skipwhite(*arg
+ 1);
7079 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
7087 item
= dict_find(d
, key
, -1);
7090 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
7095 item
= dictitem_alloc(key
);
7100 item
->di_tv
.v_lock
= 0;
7101 if (dict_add(d
, item
) == FAIL
)
7102 dictitem_free(item
);
7110 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
7113 *arg
= skipwhite(*arg
+ 1);
7118 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
7125 *arg
= skipwhite(*arg
+ 1);
7128 rettv
->v_type
= VAR_DICT
;
7129 rettv
->vval
.v_dict
= d
;
7137 * Return a string with the string representation of a variable.
7138 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7139 * "numbuf" is used for a number.
7140 * Does not put quotes around strings, as ":echo" displays values.
7141 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7145 echo_string(tv
, tofree
, numbuf
, copyID
)
7151 static int recurse
= 0;
7154 if (recurse
>= DICT_MAXNEST
)
7156 EMSG(_("E724: variable nested too deep for displaying"));
7166 r
= tv
->vval
.v_string
;
7170 if (tv
->vval
.v_list
== NULL
)
7175 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
7178 r
= (char_u
*)"[...]";
7182 tv
->vval
.v_list
->lv_copyID
= copyID
;
7183 *tofree
= list2string(tv
, copyID
);
7189 if (tv
->vval
.v_dict
== NULL
)
7194 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
7197 r
= (char_u
*)"{...}";
7201 tv
->vval
.v_dict
->dv_copyID
= copyID
;
7202 *tofree
= dict2string(tv
, copyID
);
7210 r
= get_tv_string_buf(tv
, numbuf
);
7216 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
->vval
.v_float
);
7222 EMSG2(_(e_intern2
), "echo_string()");
7231 * Return a string with the string representation of a variable.
7232 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7233 * "numbuf" is used for a number.
7234 * Puts quotes around strings, so that they can be parsed back by eval().
7238 tv2string(tv
, tofree
, numbuf
, copyID
)
7247 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
7250 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
7255 vim_snprintf((char *)numbuf
, NUMBUFLEN
- 1, "%g", tv
->vval
.v_float
);
7263 EMSG2(_(e_intern2
), "tv2string()");
7265 return echo_string(tv
, tofree
, numbuf
, copyID
);
7269 * Return string "str" in ' quotes, doubling ' characters.
7270 * If "str" is NULL an empty string is assumed.
7271 * If "function" is TRUE make it function('string').
7274 string_quote(str
, function
)
7281 len
= (function
? 13 : 3);
7284 len
+= (unsigned)STRLEN(str
);
7285 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
7294 STRCPY(r
, "function('");
7300 for (p
= str
; *p
!= NUL
; )
7316 * Convert the string "text" to a floating point number.
7317 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7318 * this always uses a decimal point.
7319 * Returns the length of the text that was consumed.
7322 string2float(text
, value
)
7324 float_T
*value
; /* result stored here */
7326 char *s
= (char *)text
;
7331 return (int)((char_u
*)s
- text
);
7336 * Get the value of an environment variable.
7337 * "arg" is pointing to the '$'. It is advanced to after the name.
7338 * If the environment variable was not set, silently assume it is empty.
7342 get_env_tv(arg
, rettv
, evaluate
)
7347 char_u
*string
= NULL
;
7351 int mustfree
= FALSE
;
7355 len
= get_env_len(arg
);
7362 /* first try vim_getenv(), fast for normal environment vars */
7363 string
= vim_getenv(name
, &mustfree
);
7364 if (string
!= NULL
&& *string
!= NUL
)
7367 string
= vim_strsave(string
);
7374 /* next try expanding things like $VIM and ${HOME} */
7375 string
= expand_env_save(name
- 1);
7376 if (string
!= NULL
&& *string
== '$')
7384 rettv
->v_type
= VAR_STRING
;
7385 rettv
->vval
.v_string
= string
;
7392 * Array with names and number of arguments of all internal functions
7393 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7397 char *f_name
; /* function name */
7398 char f_min_argc
; /* minimal number of arguments */
7399 char f_max_argc
; /* maximal number of arguments */
7400 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7401 /* implementation of function */
7405 {"abs", 1, 1, f_abs
},
7407 {"add", 2, 2, f_add
},
7408 {"append", 2, 2, f_append
},
7409 {"argc", 0, 0, f_argc
},
7410 {"argidx", 0, 0, f_argidx
},
7411 {"argv", 0, 1, f_argv
},
7413 {"atan", 1, 1, f_atan
},
7415 {"browse", 4, 4, f_browse
},
7416 {"browsedir", 2, 2, f_browsedir
},
7417 {"bufexists", 1, 1, f_bufexists
},
7418 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7419 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7420 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7421 {"buflisted", 1, 1, f_buflisted
},
7422 {"bufloaded", 1, 1, f_bufloaded
},
7423 {"bufname", 1, 1, f_bufname
},
7424 {"bufnr", 1, 2, f_bufnr
},
7425 {"bufwinnr", 1, 1, f_bufwinnr
},
7426 {"byte2line", 1, 1, f_byte2line
},
7427 {"byteidx", 2, 2, f_byteidx
},
7428 {"call", 2, 3, f_call
},
7430 {"ceil", 1, 1, f_ceil
},
7432 {"changenr", 0, 0, f_changenr
},
7433 {"char2nr", 1, 1, f_char2nr
},
7434 {"cindent", 1, 1, f_cindent
},
7435 {"clearmatches", 0, 0, f_clearmatches
},
7436 {"col", 1, 1, f_col
},
7437 #if defined(FEAT_INS_EXPAND)
7438 {"complete", 2, 2, f_complete
},
7439 {"complete_add", 1, 1, f_complete_add
},
7440 {"complete_check", 0, 0, f_complete_check
},
7442 {"confirm", 1, 4, f_confirm
},
7443 {"copy", 1, 1, f_copy
},
7445 {"cos", 1, 1, f_cos
},
7447 {"count", 2, 4, f_count
},
7448 {"cscope_connection",0,3, f_cscope_connection
},
7449 {"cursor", 1, 3, f_cursor
},
7450 {"deepcopy", 1, 2, f_deepcopy
},
7451 {"delete", 1, 1, f_delete
},
7452 {"did_filetype", 0, 0, f_did_filetype
},
7453 {"diff_filler", 1, 1, f_diff_filler
},
7454 {"diff_hlID", 2, 2, f_diff_hlID
},
7455 {"empty", 1, 1, f_empty
},
7456 {"escape", 2, 2, f_escape
},
7457 {"eval", 1, 1, f_eval
},
7458 {"eventhandler", 0, 0, f_eventhandler
},
7459 {"executable", 1, 1, f_executable
},
7460 {"exists", 1, 1, f_exists
},
7461 {"expand", 1, 2, f_expand
},
7462 {"extend", 2, 3, f_extend
},
7463 {"feedkeys", 1, 2, f_feedkeys
},
7464 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7465 {"filereadable", 1, 1, f_filereadable
},
7466 {"filewritable", 1, 1, f_filewritable
},
7467 {"filter", 2, 2, f_filter
},
7468 {"finddir", 1, 3, f_finddir
},
7469 {"findfile", 1, 3, f_findfile
},
7471 {"float2nr", 1, 1, f_float2nr
},
7472 {"floor", 1, 1, f_floor
},
7474 {"fnameescape", 1, 1, f_fnameescape
},
7475 {"fnamemodify", 2, 2, f_fnamemodify
},
7476 {"foldclosed", 1, 1, f_foldclosed
},
7477 {"foldclosedend", 1, 1, f_foldclosedend
},
7478 {"foldlevel", 1, 1, f_foldlevel
},
7479 {"foldtext", 0, 0, f_foldtext
},
7480 {"foldtextresult", 1, 1, f_foldtextresult
},
7481 {"foreground", 0, 0, f_foreground
},
7482 {"function", 1, 1, f_function
},
7483 {"garbagecollect", 0, 1, f_garbagecollect
},
7484 {"get", 2, 3, f_get
},
7485 {"getbufline", 2, 3, f_getbufline
},
7486 {"getbufvar", 2, 2, f_getbufvar
},
7487 {"getchar", 0, 1, f_getchar
},
7488 {"getcharmod", 0, 0, f_getcharmod
},
7489 {"getcmdline", 0, 0, f_getcmdline
},
7490 {"getcmdpos", 0, 0, f_getcmdpos
},
7491 {"getcmdtype", 0, 0, f_getcmdtype
},
7492 {"getcwd", 0, 0, f_getcwd
},
7493 {"getfontname", 0, 1, f_getfontname
},
7494 {"getfperm", 1, 1, f_getfperm
},
7495 {"getfsize", 1, 1, f_getfsize
},
7496 {"getftime", 1, 1, f_getftime
},
7497 {"getftype", 1, 1, f_getftype
},
7498 {"getline", 1, 2, f_getline
},
7499 {"getloclist", 1, 1, f_getqflist
},
7500 {"getmatches", 0, 0, f_getmatches
},
7501 {"getpid", 0, 0, f_getpid
},
7502 {"getpos", 1, 1, f_getpos
},
7503 {"getqflist", 0, 0, f_getqflist
},
7504 {"getreg", 0, 2, f_getreg
},
7505 {"getregtype", 0, 1, f_getregtype
},
7506 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7507 {"getwinposx", 0, 0, f_getwinposx
},
7508 {"getwinposy", 0, 0, f_getwinposy
},
7509 {"getwinvar", 2, 2, f_getwinvar
},
7510 {"glob", 1, 1, f_glob
},
7511 {"globpath", 2, 2, f_globpath
},
7512 {"has", 1, 1, f_has
},
7513 {"has_key", 2, 2, f_has_key
},
7514 {"haslocaldir", 0, 0, f_haslocaldir
},
7515 {"hasmapto", 1, 3, f_hasmapto
},
7516 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7517 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7518 {"histadd", 2, 2, f_histadd
},
7519 {"histdel", 1, 2, f_histdel
},
7520 {"histget", 1, 2, f_histget
},
7521 {"histnr", 1, 1, f_histnr
},
7522 {"hlID", 1, 1, f_hlID
},
7523 {"hlexists", 1, 1, f_hlexists
},
7524 {"hostname", 0, 0, f_hostname
},
7525 {"iconv", 3, 3, f_iconv
},
7526 {"indent", 1, 1, f_indent
},
7527 {"index", 2, 4, f_index
},
7528 {"input", 1, 3, f_input
},
7529 {"inputdialog", 1, 3, f_inputdialog
},
7530 {"inputlist", 1, 1, f_inputlist
},
7531 {"inputrestore", 0, 0, f_inputrestore
},
7532 {"inputsave", 0, 0, f_inputsave
},
7533 {"inputsecret", 1, 2, f_inputsecret
},
7534 {"insert", 2, 3, f_insert
},
7535 {"isdirectory", 1, 1, f_isdirectory
},
7536 {"islocked", 1, 1, f_islocked
},
7537 {"items", 1, 1, f_items
},
7538 {"join", 1, 2, f_join
},
7539 {"keys", 1, 1, f_keys
},
7540 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7541 {"len", 1, 1, f_len
},
7542 {"libcall", 3, 3, f_libcall
},
7543 {"libcallnr", 3, 3, f_libcallnr
},
7544 {"line", 1, 1, f_line
},
7545 {"line2byte", 1, 1, f_line2byte
},
7546 {"lispindent", 1, 1, f_lispindent
},
7547 {"localtime", 0, 0, f_localtime
},
7549 {"log10", 1, 1, f_log10
},
7551 {"map", 2, 2, f_map
},
7552 {"maparg", 1, 3, f_maparg
},
7553 {"mapcheck", 1, 3, f_mapcheck
},
7554 {"match", 2, 4, f_match
},
7555 {"matchadd", 2, 4, f_matchadd
},
7556 {"matcharg", 1, 1, f_matcharg
},
7557 {"matchdelete", 1, 1, f_matchdelete
},
7558 {"matchend", 2, 4, f_matchend
},
7559 {"matchlist", 2, 4, f_matchlist
},
7560 {"matchstr", 2, 4, f_matchstr
},
7561 {"max", 1, 1, f_max
},
7562 {"min", 1, 1, f_min
},
7564 {"mkdir", 1, 3, f_mkdir
},
7566 {"mode", 0, 1, f_mode
},
7567 {"nextnonblank", 1, 1, f_nextnonblank
},
7568 {"nr2char", 1, 1, f_nr2char
},
7569 {"pathshorten", 1, 1, f_pathshorten
},
7571 {"pow", 2, 2, f_pow
},
7573 {"prevnonblank", 1, 1, f_prevnonblank
},
7574 {"printf", 2, 19, f_printf
},
7575 {"pumvisible", 0, 0, f_pumvisible
},
7576 {"range", 1, 3, f_range
},
7577 {"readfile", 1, 3, f_readfile
},
7578 {"reltime", 0, 2, f_reltime
},
7579 {"reltimestr", 1, 1, f_reltimestr
},
7580 {"remote_expr", 2, 3, f_remote_expr
},
7581 {"remote_foreground", 1, 1, f_remote_foreground
},
7582 {"remote_peek", 1, 2, f_remote_peek
},
7583 {"remote_read", 1, 1, f_remote_read
},
7584 {"remote_send", 2, 3, f_remote_send
},
7585 {"remove", 2, 3, f_remove
},
7586 {"rename", 2, 2, f_rename
},
7587 {"repeat", 2, 2, f_repeat
},
7588 {"resolve", 1, 1, f_resolve
},
7589 {"reverse", 1, 1, f_reverse
},
7591 {"round", 1, 1, f_round
},
7593 {"search", 1, 4, f_search
},
7594 {"searchdecl", 1, 3, f_searchdecl
},
7595 {"searchpair", 3, 7, f_searchpair
},
7596 {"searchpairpos", 3, 7, f_searchpairpos
},
7597 {"searchpos", 1, 4, f_searchpos
},
7598 {"server2client", 2, 2, f_server2client
},
7599 {"serverlist", 0, 0, f_serverlist
},
7600 {"setbufvar", 3, 3, f_setbufvar
},
7601 {"setcmdpos", 1, 1, f_setcmdpos
},
7602 {"setline", 2, 2, f_setline
},
7603 {"setloclist", 2, 3, f_setloclist
},
7604 {"setmatches", 1, 1, f_setmatches
},
7605 {"setpos", 2, 2, f_setpos
},
7606 {"setqflist", 1, 2, f_setqflist
},
7607 {"setreg", 2, 3, f_setreg
},
7608 {"settabwinvar", 4, 4, f_settabwinvar
},
7609 {"setwinvar", 3, 3, f_setwinvar
},
7610 {"shellescape", 1, 1, f_shellescape
},
7611 {"simplify", 1, 1, f_simplify
},
7613 {"sin", 1, 1, f_sin
},
7615 {"sort", 1, 2, f_sort
},
7616 {"soundfold", 1, 1, f_soundfold
},
7617 {"spellbadword", 0, 1, f_spellbadword
},
7618 {"spellsuggest", 1, 3, f_spellsuggest
},
7619 {"split", 1, 3, f_split
},
7621 {"sqrt", 1, 1, f_sqrt
},
7622 {"str2float", 1, 1, f_str2float
},
7624 {"str2nr", 1, 2, f_str2nr
},
7625 #ifdef HAVE_STRFTIME
7626 {"strftime", 1, 2, f_strftime
},
7628 {"stridx", 2, 3, f_stridx
},
7629 {"string", 1, 1, f_string
},
7630 {"strlen", 1, 1, f_strlen
},
7631 {"strpart", 2, 3, f_strpart
},
7632 {"strridx", 2, 3, f_strridx
},
7633 {"strtrans", 1, 1, f_strtrans
},
7634 {"submatch", 1, 1, f_submatch
},
7635 {"substitute", 4, 4, f_substitute
},
7636 {"synID", 3, 3, f_synID
},
7637 {"synIDattr", 2, 3, f_synIDattr
},
7638 {"synIDtrans", 1, 1, f_synIDtrans
},
7639 {"synstack", 2, 2, f_synstack
},
7640 {"system", 1, 2, f_system
},
7641 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7642 {"tabpagenr", 0, 1, f_tabpagenr
},
7643 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7644 {"tagfiles", 0, 0, f_tagfiles
},
7645 {"taglist", 1, 1, f_taglist
},
7646 {"tempname", 0, 0, f_tempname
},
7647 {"test", 1, 1, f_test
},
7648 {"tolower", 1, 1, f_tolower
},
7649 {"toupper", 1, 1, f_toupper
},
7652 {"trunc", 1, 1, f_trunc
},
7654 {"type", 1, 1, f_type
},
7655 {"values", 1, 1, f_values
},
7656 {"virtcol", 1, 1, f_virtcol
},
7657 {"visualmode", 0, 1, f_visualmode
},
7658 {"winbufnr", 1, 1, f_winbufnr
},
7659 {"wincol", 0, 0, f_wincol
},
7660 {"winheight", 1, 1, f_winheight
},
7661 {"winline", 0, 0, f_winline
},
7662 {"winnr", 0, 1, f_winnr
},
7663 {"winrestcmd", 0, 0, f_winrestcmd
},
7664 {"winrestview", 1, 1, f_winrestview
},
7665 {"winsaveview", 0, 0, f_winsaveview
},
7666 {"winwidth", 1, 1, f_winwidth
},
7667 {"writefile", 2, 3, f_writefile
},
7670 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7673 * Function given to ExpandGeneric() to obtain the list of internal
7674 * or user defined function names.
7677 get_function_name(xp
, idx
)
7681 static int intidx
= -1;
7688 name
= get_user_func_name(xp
, idx
);
7692 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7694 STRCPY(IObuff
, functions
[intidx
].f_name
);
7695 STRCAT(IObuff
, "(");
7696 if (functions
[intidx
].f_max_argc
== 0)
7697 STRCAT(IObuff
, ")");
7705 * Function given to ExpandGeneric() to obtain the list of internal or
7706 * user defined variable or function names.
7710 get_expr_name(xp
, idx
)
7714 static int intidx
= -1;
7721 name
= get_function_name(xp
, idx
);
7725 return get_user_var_name(xp
, ++intidx
);
7728 #endif /* FEAT_CMDL_COMPL */
7731 * Find internal function in table above.
7732 * Return index, or -1 if not found
7735 find_internal_func(name
)
7736 char_u
*name
; /* name of the function */
7739 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7744 * Find the function name in the table. Binary search.
7746 while (first
<= last
)
7748 x
= first
+ ((unsigned)(last
- first
) >> 1);
7749 cmp
= STRCMP(name
, functions
[x
].f_name
);
7761 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7762 * name it contains, otherwise return "name".
7765 deref_func_name(name
, lenp
)
7774 v
= find_var(name
, NULL
);
7776 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7778 if (v
->di_tv
.vval
.v_string
== NULL
)
7781 return (char_u
*)""; /* just in case */
7783 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7784 return v
->di_tv
.vval
.v_string
;
7791 * Allocate a variable for the result of a function.
7792 * Return OK or FAIL.
7795 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7797 char_u
*name
; /* name of the function */
7798 int len
; /* length of "name" */
7800 char_u
**arg
; /* argument, pointing to the '(' */
7801 linenr_T firstline
; /* first line of range */
7802 linenr_T lastline
; /* last line of range */
7803 int *doesrange
; /* return: function handled range */
7805 dict_T
*selfdict
; /* Dictionary for "self" */
7809 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7810 int argcount
= 0; /* number of arguments found */
7813 * Get the arguments.
7816 while (argcount
< MAX_FUNC_ARGS
)
7818 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7819 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7821 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7836 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7837 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7838 else if (!aborting())
7840 if (argcount
== MAX_FUNC_ARGS
)
7841 emsg_funcname("E740: Too many arguments for function %s", name
);
7843 emsg_funcname("E116: Invalid arguments for function %s", name
);
7846 while (--argcount
>= 0)
7847 clear_tv(&argvars
[argcount
]);
7849 *arg
= skipwhite(argp
);
7855 * Call a function with its resolved parameters
7856 * Return OK when the function can't be called, FAIL otherwise.
7857 * Also returns OK when an error was encountered while executing the function.
7860 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7861 doesrange
, evaluate
, selfdict
)
7862 char_u
*name
; /* name of the function */
7863 int len
; /* length of "name" */
7864 typval_T
*rettv
; /* return value goes here */
7865 int argcount
; /* number of "argvars" */
7866 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7867 PLUS ONE elements! */
7868 linenr_T firstline
; /* first line of range */
7869 linenr_T lastline
; /* last line of range */
7870 int *doesrange
; /* return: function handled range */
7872 dict_T
*selfdict
; /* Dictionary for "self" */
7875 #define ERROR_UNKNOWN 0
7876 #define ERROR_TOOMANY 1
7877 #define ERROR_TOOFEW 2
7878 #define ERROR_SCRIPT 3
7879 #define ERROR_DICT 4
7880 #define ERROR_NONE 5
7881 #define ERROR_OTHER 6
7882 int error
= ERROR_NONE
;
7887 #define FLEN_FIXED 40
7888 char_u fname_buf
[FLEN_FIXED
+ 1];
7892 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7893 * Change <SNR>123_name() to K_SNR 123_name().
7894 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7898 llen
= eval_fname_script(name
);
7901 fname_buf
[0] = K_SPECIAL
;
7902 fname_buf
[1] = KS_EXTRA
;
7903 fname_buf
[2] = (int)KE_SNR
;
7905 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
7907 if (current_SID
<= 0)
7908 error
= ERROR_SCRIPT
;
7911 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
7912 i
= (int)STRLEN(fname_buf
);
7915 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
7917 STRCPY(fname_buf
+ i
, name
+ llen
);
7922 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
7924 error
= ERROR_OTHER
;
7927 mch_memmove(fname
, fname_buf
, (size_t)i
);
7928 STRCPY(fname
+ i
, name
+ llen
);
7938 /* execute the function if no errors detected and executing */
7939 if (evaluate
&& error
== ERROR_NONE
)
7941 rettv
->v_type
= VAR_NUMBER
; /* default is number rettv */
7942 error
= ERROR_UNKNOWN
;
7944 if (!builtin_function(fname
))
7947 * User defined function.
7949 fp
= find_func(fname
);
7952 /* Trigger FuncUndefined event, may load the function. */
7954 && apply_autocmds(EVENT_FUNCUNDEFINED
,
7955 fname
, fname
, TRUE
, NULL
)
7958 /* executed an autocommand, search for the function again */
7959 fp
= find_func(fname
);
7962 /* Try loading a package. */
7963 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
7965 /* loaded a package, search for the function again */
7966 fp
= find_func(fname
);
7971 if (fp
->uf_flags
& FC_RANGE
)
7973 if (argcount
< fp
->uf_args
.ga_len
)
7974 error
= ERROR_TOOFEW
;
7975 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
7976 error
= ERROR_TOOMANY
;
7977 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
7982 * Call the user function.
7983 * Save and restore search patterns, script variables and
7986 save_search_patterns();
7989 call_user_func(fp
, argcount
, argvars
, rettv
,
7990 firstline
, lastline
,
7991 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
7992 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
7993 && fp
->uf_refcount
<= 0)
7994 /* Function was unreferenced while being used, free it
7998 restore_search_patterns();
8006 * Find the function name in the table, call its implementation.
8008 i
= find_internal_func(fname
);
8011 if (argcount
< functions
[i
].f_min_argc
)
8012 error
= ERROR_TOOFEW
;
8013 else if (argcount
> functions
[i
].f_max_argc
)
8014 error
= ERROR_TOOMANY
;
8017 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
8018 functions
[i
].f_func(argvars
, rettv
);
8024 * The function call (or "FuncUndefined" autocommand sequence) might
8025 * have been aborted by an error, an interrupt, or an explicitly thrown
8026 * exception that has not been caught so far. This situation can be
8027 * tested for by calling aborting(). For an error in an internal
8028 * function or for the "E132" error in call_user_func(), however, the
8029 * throw point at which the "force_abort" flag (temporarily reset by
8030 * emsg()) is normally updated has not been reached yet. We need to
8031 * update that flag first to make aborting() reliable.
8033 update_force_abort();
8035 if (error
== ERROR_NONE
)
8039 * Report an error unless the argument evaluation or function call has been
8040 * cancelled due to an aborting error, an interrupt, or an exception.
8047 emsg_funcname(N_("E117: Unknown function: %s"), name
);
8050 emsg_funcname(e_toomanyarg
, name
);
8053 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
8057 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
8061 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
8068 if (fname
!= name
&& fname
!= fname_buf
)
8075 * Give an error message with a function name. Handle <SNR> things.
8078 emsg_funcname(ermsg
, name
)
8084 if (*name
== K_SPECIAL
)
8085 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
8093 /*********************************************
8094 * Implementation of the built-in functions
8099 * "abs(expr)" function
8102 f_abs(argvars
, rettv
)
8106 if (argvars
[0].v_type
== VAR_FLOAT
)
8108 rettv
->v_type
= VAR_FLOAT
;
8109 rettv
->vval
.v_float
= fabs(argvars
[0].vval
.v_float
);
8116 n
= get_tv_number_chk(&argvars
[0], &error
);
8118 rettv
->vval
.v_number
= -1;
8120 rettv
->vval
.v_number
= n
;
8122 rettv
->vval
.v_number
= -n
;
8128 * "add(list, item)" function
8131 f_add(argvars
, rettv
)
8137 rettv
->vval
.v_number
= 1; /* Default: Failed */
8138 if (argvars
[0].v_type
== VAR_LIST
)
8140 if ((l
= argvars
[0].vval
.v_list
) != NULL
8141 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
8142 && list_append_tv(l
, &argvars
[1]) == OK
)
8143 copy_tv(&argvars
[0], rettv
);
8150 * "append(lnum, string/list)" function
8153 f_append(argvars
, rettv
)
8160 listitem_T
*li
= NULL
;
8164 lnum
= get_tv_lnum(argvars
);
8166 && lnum
<= curbuf
->b_ml
.ml_line_count
8167 && u_save(lnum
, lnum
+ 1) == OK
)
8169 if (argvars
[1].v_type
== VAR_LIST
)
8171 l
= argvars
[1].vval
.v_list
;
8176 rettv
->vval
.v_number
= 0; /* Default: Success */
8180 tv
= &argvars
[1]; /* append a string */
8181 else if (li
== NULL
)
8182 break; /* end of list */
8184 tv
= &li
->li_tv
; /* append item from list */
8185 line
= get_tv_string_chk(tv
);
8186 if (line
== NULL
) /* type error */
8188 rettv
->vval
.v_number
= 1; /* Failed */
8191 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
8198 appended_lines_mark(lnum
, added
);
8199 if (curwin
->w_cursor
.lnum
> lnum
)
8200 curwin
->w_cursor
.lnum
+= added
;
8203 rettv
->vval
.v_number
= 1; /* Failed */
8211 f_argc(argvars
, rettv
)
8215 rettv
->vval
.v_number
= ARGCOUNT
;
8219 * "argidx()" function
8223 f_argidx(argvars
, rettv
)
8227 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
8231 * "argv(nr)" function
8234 f_argv(argvars
, rettv
)
8240 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
8242 idx
= get_tv_number_chk(&argvars
[0], NULL
);
8243 if (idx
>= 0 && idx
< ARGCOUNT
)
8244 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
8246 rettv
->vval
.v_string
= NULL
;
8247 rettv
->v_type
= VAR_STRING
;
8249 else if (rettv_list_alloc(rettv
) == OK
)
8250 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
8251 list_append_string(rettv
->vval
.v_list
,
8252 alist_name(&ARGLIST
[idx
]), -1);
8256 static int get_float_arg
__ARGS((typval_T
*argvars
, float_T
*f
));
8259 * Get the float value of "argvars[0]" into "f".
8260 * Returns FAIL when the argument is not a Number or Float.
8263 get_float_arg(argvars
, f
)
8267 if (argvars
[0].v_type
== VAR_FLOAT
)
8269 *f
= argvars
[0].vval
.v_float
;
8272 if (argvars
[0].v_type
== VAR_NUMBER
)
8274 *f
= (float_T
)argvars
[0].vval
.v_number
;
8277 EMSG(_("E808: Number or Float required"));
8285 f_atan(argvars
, rettv
)
8291 rettv
->v_type
= VAR_FLOAT
;
8292 if (get_float_arg(argvars
, &f
) == OK
)
8293 rettv
->vval
.v_float
= atan(f
);
8295 rettv
->vval
.v_float
= 0.0;
8300 * "browse(save, title, initdir, default)" function
8304 f_browse(argvars
, rettv
)
8313 char_u buf
[NUMBUFLEN
];
8314 char_u buf2
[NUMBUFLEN
];
8317 save
= get_tv_number_chk(&argvars
[0], &error
);
8318 title
= get_tv_string_chk(&argvars
[1]);
8319 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
8320 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8322 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
8323 rettv
->vval
.v_string
= NULL
;
8325 rettv
->vval
.v_string
=
8326 do_browse(save
? BROWSE_SAVE
: 0,
8327 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
8329 rettv
->vval
.v_string
= NULL
;
8331 rettv
->v_type
= VAR_STRING
;
8335 * "browsedir(title, initdir)" function
8339 f_browsedir(argvars
, rettv
)
8346 char_u buf
[NUMBUFLEN
];
8348 title
= get_tv_string_chk(&argvars
[0]);
8349 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
8351 if (title
== NULL
|| initdir
== NULL
)
8352 rettv
->vval
.v_string
= NULL
;
8354 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
8355 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
8357 rettv
->vval
.v_string
= NULL
;
8359 rettv
->v_type
= VAR_STRING
;
8362 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
8365 * Find a buffer by number or exact name.
8373 if (avar
->v_type
== VAR_NUMBER
)
8374 buf
= buflist_findnr((int)avar
->vval
.v_number
);
8375 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
8377 buf
= buflist_findname_exp(avar
->vval
.v_string
);
8380 /* No full path name match, try a match with a URL or a "nofile"
8381 * buffer, these don't use the full path. */
8382 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
8383 if (buf
->b_fname
!= NULL
8384 && (path_with_url(buf
->b_fname
)
8385 #ifdef FEAT_QUICKFIX
8389 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
8397 * "bufexists(expr)" function
8400 f_bufexists(argvars
, rettv
)
8404 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
8408 * "buflisted(expr)" function
8411 f_buflisted(argvars
, rettv
)
8417 buf
= find_buffer(&argvars
[0]);
8418 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
8422 * "bufloaded(expr)" function
8425 f_bufloaded(argvars
, rettv
)
8431 buf
= find_buffer(&argvars
[0]);
8432 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
8435 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
8438 * Get buffer by number or pattern.
8444 char_u
*name
= tv
->vval
.v_string
;
8449 if (tv
->v_type
== VAR_NUMBER
)
8450 return buflist_findnr((int)tv
->vval
.v_number
);
8451 if (tv
->v_type
!= VAR_STRING
)
8453 if (name
== NULL
|| *name
== NUL
)
8455 if (name
[0] == '$' && name
[1] == NUL
)
8458 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8459 save_magic
= p_magic
;
8462 p_cpo
= (char_u
*)"";
8464 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8467 p_magic
= save_magic
;
8470 /* If not found, try expanding the name, like done for bufexists(). */
8472 buf
= find_buffer(tv
);
8478 * "bufname(expr)" function
8481 f_bufname(argvars
, rettv
)
8487 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8489 buf
= get_buf_tv(&argvars
[0]);
8490 rettv
->v_type
= VAR_STRING
;
8491 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8492 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8494 rettv
->vval
.v_string
= NULL
;
8499 * "bufnr(expr)" function
8502 f_bufnr(argvars
, rettv
)
8510 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8512 buf
= get_buf_tv(&argvars
[0]);
8515 /* If the buffer isn't found and the second argument is not zero create a
8518 && argvars
[1].v_type
!= VAR_UNKNOWN
8519 && get_tv_number_chk(&argvars
[1], &error
) != 0
8521 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8523 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8526 rettv
->vval
.v_number
= buf
->b_fnum
;
8528 rettv
->vval
.v_number
= -1;
8532 * "bufwinnr(nr)" function
8535 f_bufwinnr(argvars
, rettv
)
8545 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8547 buf
= get_buf_tv(&argvars
[0]);
8549 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8552 if (wp
->w_buffer
== buf
)
8555 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8557 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8563 * "byte2line(byte)" function
8567 f_byte2line(argvars
, rettv
)
8571 #ifndef FEAT_BYTEOFF
8572 rettv
->vval
.v_number
= -1;
8576 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8578 rettv
->vval
.v_number
= -1;
8580 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8581 (linenr_T
)0, &boff
);
8586 * "byteidx()" function
8590 f_byteidx(argvars
, rettv
)
8600 str
= get_tv_string_chk(&argvars
[0]);
8601 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8602 rettv
->vval
.v_number
= -1;
8603 if (str
== NULL
|| idx
< 0)
8608 for ( ; idx
> 0; idx
--)
8610 if (*t
== NUL
) /* EOL reached */
8612 t
+= (*mb_ptr2len
)(t
);
8614 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8616 if ((size_t)idx
<= STRLEN(str
))
8617 rettv
->vval
.v_number
= idx
;
8622 * "call(func, arglist)" function
8625 f_call(argvars
, rettv
)
8630 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8634 dict_T
*selfdict
= NULL
;
8636 rettv
->vval
.v_number
= 0;
8637 if (argvars
[1].v_type
!= VAR_LIST
)
8642 if (argvars
[1].vval
.v_list
== NULL
)
8645 if (argvars
[0].v_type
== VAR_FUNC
)
8646 func
= argvars
[0].vval
.v_string
;
8648 func
= get_tv_string(&argvars
[0]);
8650 return; /* type error or empty name */
8652 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8654 if (argvars
[2].v_type
!= VAR_DICT
)
8659 selfdict
= argvars
[2].vval
.v_dict
;
8662 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8663 item
= item
->li_next
)
8665 if (argc
== MAX_FUNC_ARGS
)
8667 EMSG(_("E699: Too many arguments"));
8670 /* Make a copy of each argument. This is needed to be able to set
8671 * v_lock to VAR_FIXED in the copy without changing the original list.
8673 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8677 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8678 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8679 &dummy
, TRUE
, selfdict
);
8681 /* Free the arguments. */
8683 clear_tv(&argv
[--argc
]);
8688 * "ceil({float})" function
8691 f_ceil(argvars
, rettv
)
8697 rettv
->v_type
= VAR_FLOAT
;
8698 if (get_float_arg(argvars
, &f
) == OK
)
8699 rettv
->vval
.v_float
= ceil(f
);
8701 rettv
->vval
.v_float
= 0.0;
8706 * "changenr()" function
8710 f_changenr(argvars
, rettv
)
8714 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8718 * "char2nr(string)" function
8721 f_char2nr(argvars
, rettv
)
8727 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8730 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8734 * "cindent(lnum)" function
8737 f_cindent(argvars
, rettv
)
8745 pos
= curwin
->w_cursor
;
8746 lnum
= get_tv_lnum(argvars
);
8747 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8749 curwin
->w_cursor
.lnum
= lnum
;
8750 rettv
->vval
.v_number
= get_c_indent();
8751 curwin
->w_cursor
= pos
;
8755 rettv
->vval
.v_number
= -1;
8759 * "clearmatches()" function
8763 f_clearmatches(argvars
, rettv
)
8767 #ifdef FEAT_SEARCH_EXTRA
8768 clear_matches(curwin
);
8773 * "col(string)" function
8776 f_col(argvars
, rettv
)
8782 int fnum
= curbuf
->b_fnum
;
8784 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8785 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8787 if (fp
->col
== MAXCOL
)
8789 /* '> can be MAXCOL, get the length of the line then */
8790 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8791 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8798 #ifdef FEAT_VIRTUALEDIT
8799 /* col(".") when the cursor is on the NUL at the end of the line
8800 * because of "coladd" can be seen as an extra column. */
8801 if (virtual_active() && fp
== &curwin
->w_cursor
)
8803 char_u
*p
= ml_get_cursor();
8805 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8806 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8811 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8814 if (*p
!= NUL
&& p
[1] == NUL
)
8822 rettv
->vval
.v_number
= col
;
8825 #if defined(FEAT_INS_EXPAND)
8827 * "complete()" function
8831 f_complete(argvars
, rettv
)
8837 if ((State
& INSERT
) == 0)
8839 EMSG(_("E785: complete() can only be used in Insert mode"));
8843 /* Check for undo allowed here, because if something was already inserted
8844 * the line was already saved for undo and this check isn't done. */
8845 if (!undo_allowed())
8848 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8854 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
8858 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
8862 * "complete_add()" function
8866 f_complete_add(argvars
, rettv
)
8870 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
8874 * "complete_check()" function
8878 f_complete_check(argvars
, rettv
)
8882 int saved
= RedrawingDisabled
;
8884 RedrawingDisabled
= 0;
8885 ins_compl_check_keys(0);
8886 rettv
->vval
.v_number
= compl_interrupted
;
8887 RedrawingDisabled
= saved
;
8892 * "confirm(message, buttons[, default [, type]])" function
8896 f_confirm(argvars
, rettv
)
8900 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8902 char_u
*buttons
= NULL
;
8903 char_u buf
[NUMBUFLEN
];
8904 char_u buf2
[NUMBUFLEN
];
8906 int type
= VIM_GENERIC
;
8910 message
= get_tv_string_chk(&argvars
[0]);
8911 if (message
== NULL
)
8913 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8915 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
8916 if (buttons
== NULL
)
8918 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8920 def
= get_tv_number_chk(&argvars
[2], &error
);
8921 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8923 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8924 if (typestr
== NULL
)
8928 switch (TOUPPER_ASC(*typestr
))
8930 case 'E': type
= VIM_ERROR
; break;
8931 case 'Q': type
= VIM_QUESTION
; break;
8932 case 'I': type
= VIM_INFO
; break;
8933 case 'W': type
= VIM_WARNING
; break;
8934 case 'G': type
= VIM_GENERIC
; break;
8941 if (buttons
== NULL
|| *buttons
== NUL
)
8942 buttons
= (char_u
*)_("&Ok");
8945 rettv
->vval
.v_number
= 0;
8947 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
8950 rettv
->vval
.v_number
= 0;
8958 f_copy(argvars
, rettv
)
8962 item_copy(&argvars
[0], rettv
, FALSE
, 0);
8970 f_cos(argvars
, rettv
)
8976 rettv
->v_type
= VAR_FLOAT
;
8977 if (get_float_arg(argvars
, &f
) == OK
)
8978 rettv
->vval
.v_float
= cos(f
);
8980 rettv
->vval
.v_float
= 0.0;
8985 * "count()" function
8988 f_count(argvars
, rettv
)
8995 if (argvars
[0].v_type
== VAR_LIST
)
9001 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9004 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9008 ic
= get_tv_number_chk(&argvars
[2], &error
);
9009 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9011 idx
= get_tv_number_chk(&argvars
[3], &error
);
9014 li
= list_find(l
, idx
);
9016 EMSGN(_(e_listidx
), idx
);
9023 for ( ; li
!= NULL
; li
= li
->li_next
)
9024 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
9028 else if (argvars
[0].v_type
== VAR_DICT
)
9034 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9038 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9040 ic
= get_tv_number_chk(&argvars
[2], &error
);
9041 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9045 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
9046 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
9048 if (!HASHITEM_EMPTY(hi
))
9051 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
9058 EMSG2(_(e_listdictarg
), "count()");
9059 rettv
->vval
.v_number
= n
;
9063 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9065 * Checks the existence of a cscope connection.
9069 f_cscope_connection(argvars
, rettv
)
9075 char_u
*dbpath
= NULL
;
9076 char_u
*prepend
= NULL
;
9077 char_u buf
[NUMBUFLEN
];
9079 if (argvars
[0].v_type
!= VAR_UNKNOWN
9080 && argvars
[1].v_type
!= VAR_UNKNOWN
)
9082 num
= (int)get_tv_number(&argvars
[0]);
9083 dbpath
= get_tv_string(&argvars
[1]);
9084 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9085 prepend
= get_tv_string_buf(&argvars
[2], buf
);
9088 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
9090 rettv
->vval
.v_number
= 0;
9095 * "cursor(lnum, col)" function
9097 * Moves the cursor to the specified line and column
9101 f_cursor(argvars
, rettv
)
9106 #ifdef FEAT_VIRTUALEDIT
9110 if (argvars
[1].v_type
== VAR_UNKNOWN
)
9114 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
9118 #ifdef FEAT_VIRTUALEDIT
9119 coladd
= pos
.coladd
;
9124 line
= get_tv_lnum(argvars
);
9125 col
= get_tv_number_chk(&argvars
[1], NULL
);
9126 #ifdef FEAT_VIRTUALEDIT
9127 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9128 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
9131 if (line
< 0 || col
< 0
9132 #ifdef FEAT_VIRTUALEDIT
9136 return; /* type error; errmsg already given */
9138 curwin
->w_cursor
.lnum
= line
;
9140 curwin
->w_cursor
.col
= col
- 1;
9141 #ifdef FEAT_VIRTUALEDIT
9142 curwin
->w_cursor
.coladd
= coladd
;
9145 /* Make sure the cursor is in a valid position. */
9148 /* Correct cursor for multi-byte character. */
9153 curwin
->w_set_curswant
= TRUE
;
9157 * "deepcopy()" function
9160 f_deepcopy(argvars
, rettv
)
9166 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9167 noref
= get_tv_number_chk(&argvars
[1], NULL
);
9168 if (noref
< 0 || noref
> 1)
9171 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
9175 * "delete()" function
9178 f_delete(argvars
, rettv
)
9182 if (check_restricted() || check_secure())
9183 rettv
->vval
.v_number
= -1;
9185 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
9189 * "did_filetype()" function
9193 f_did_filetype(argvars
, rettv
)
9198 rettv
->vval
.v_number
= did_filetype
;
9200 rettv
->vval
.v_number
= 0;
9205 * "diff_filler()" function
9209 f_diff_filler(argvars
, rettv
)
9214 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
9219 * "diff_hlID()" function
9223 f_diff_hlID(argvars
, rettv
)
9228 linenr_T lnum
= get_tv_lnum(argvars
);
9229 static linenr_T prev_lnum
= 0;
9230 static int changedtick
= 0;
9231 static int fnum
= 0;
9232 static int change_start
= 0;
9233 static int change_end
= 0;
9234 static hlf_T hlID
= (hlf_T
)0;
9238 if (lnum
< 0) /* ignore type error in {lnum} arg */
9240 if (lnum
!= prev_lnum
9241 || changedtick
!= curbuf
->b_changedtick
9242 || fnum
!= curbuf
->b_fnum
)
9244 /* New line, buffer, change: need to get the values. */
9245 filler_lines
= diff_check(curwin
, lnum
);
9246 if (filler_lines
< 0)
9248 if (filler_lines
== -1)
9250 change_start
= MAXCOL
;
9252 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
9253 hlID
= HLF_ADD
; /* added line */
9255 hlID
= HLF_CHD
; /* changed line */
9258 hlID
= HLF_ADD
; /* added line */
9263 changedtick
= curbuf
->b_changedtick
;
9264 fnum
= curbuf
->b_fnum
;
9267 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
9269 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
9270 if (col
>= change_start
&& col
<= change_end
)
9271 hlID
= HLF_TXD
; /* changed text */
9273 hlID
= HLF_CHD
; /* changed line */
9275 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
9280 * "empty({expr})" function
9283 f_empty(argvars
, rettv
)
9289 switch (argvars
[0].v_type
)
9293 n
= argvars
[0].vval
.v_string
== NULL
9294 || *argvars
[0].vval
.v_string
== NUL
;
9297 n
= argvars
[0].vval
.v_number
== 0;
9301 n
= argvars
[0].vval
.v_float
== 0.0;
9305 n
= argvars
[0].vval
.v_list
== NULL
9306 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
9309 n
= argvars
[0].vval
.v_dict
== NULL
9310 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
9313 EMSG2(_(e_intern2
), "f_empty()");
9317 rettv
->vval
.v_number
= n
;
9321 * "escape({string}, {chars})" function
9324 f_escape(argvars
, rettv
)
9328 char_u buf
[NUMBUFLEN
];
9330 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
9331 get_tv_string_buf(&argvars
[1], buf
));
9332 rettv
->v_type
= VAR_STRING
;
9340 f_eval(argvars
, rettv
)
9346 s
= get_tv_string_chk(&argvars
[0]);
9350 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
9352 rettv
->v_type
= VAR_NUMBER
;
9353 rettv
->vval
.v_number
= 0;
9356 EMSG(_(e_trailing
));
9360 * "eventhandler()" function
9364 f_eventhandler(argvars
, rettv
)
9368 rettv
->vval
.v_number
= vgetc_busy
;
9372 * "executable()" function
9375 f_executable(argvars
, rettv
)
9379 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
9383 * "exists()" function
9386 f_exists(argvars
, rettv
)
9395 p
= get_tv_string(&argvars
[0]);
9396 if (*p
== '$') /* environment variable */
9398 /* first try "normal" environment variables (fast) */
9399 if (mch_getenv(p
+ 1) != NULL
)
9403 /* try expanding things like $VIM and ${HOME} */
9404 p
= expand_env_save(p
);
9405 if (p
!= NULL
&& *p
!= '$')
9410 else if (*p
== '&' || *p
== '+') /* option */
9412 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
9413 if (*skipwhite(p
) != NUL
)
9414 n
= FALSE
; /* trailing garbage */
9416 else if (*p
== '*') /* internal or user defined function */
9418 n
= function_exists(p
+ 1);
9422 n
= cmd_exists(p
+ 1);
9428 n
= autocmd_supported(p
+ 2);
9430 n
= au_exists(p
+ 1);
9433 else /* internal variable */
9438 /* get_name_len() takes care of expanding curly braces */
9440 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
9445 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
9448 /* handle d.key, l[idx], f(expr) */
9449 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
9460 rettv
->vval
.v_number
= n
;
9464 * "expand()" function
9467 f_expand(argvars
, rettv
)
9474 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
9478 rettv
->v_type
= VAR_STRING
;
9479 s
= get_tv_string(&argvars
[0]);
9480 if (*s
== '%' || *s
== '#' || *s
== '<')
9483 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
9488 /* When the optional second argument is non-zero, don't remove matches
9489 * for 'suffixes' and 'wildignore' */
9490 if (argvars
[1].v_type
!= VAR_UNKNOWN
9491 && get_tv_number_chk(&argvars
[1], &error
))
9492 flags
|= WILD_KEEP_ALL
;
9496 xpc
.xp_context
= EXPAND_FILES
;
9497 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
9500 rettv
->vval
.v_string
= NULL
;
9505 * "extend(list, list [, idx])" function
9506 * "extend(dict, dict [, action])" function
9509 f_extend(argvars
, rettv
)
9513 rettv
->vval
.v_number
= 0;
9514 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9521 l1
= argvars
[0].vval
.v_list
;
9522 l2
= argvars
[1].vval
.v_list
;
9523 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9526 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9528 before
= get_tv_number_chk(&argvars
[2], &error
);
9530 return; /* type error; errmsg already given */
9532 if (before
== l1
->lv_len
)
9536 item
= list_find(l1
, before
);
9539 EMSGN(_(e_listidx
), before
);
9546 list_extend(l1
, l2
, item
);
9548 copy_tv(&argvars
[0], rettv
);
9551 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9560 d1
= argvars
[0].vval
.v_dict
;
9561 d2
= argvars
[1].vval
.v_dict
;
9562 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9565 /* Check the third argument. */
9566 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9568 static char *(av
[]) = {"keep", "force", "error"};
9570 action
= get_tv_string_chk(&argvars
[2]);
9572 return; /* type error; errmsg already given */
9573 for (i
= 0; i
< 3; ++i
)
9574 if (STRCMP(action
, av
[i
]) == 0)
9578 EMSG2(_(e_invarg2
), action
);
9583 action
= (char_u
*)"force";
9585 /* Go over all entries in the second dict and add them to the
9587 todo
= (int)d2
->dv_hashtab
.ht_used
;
9588 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9590 if (!HASHITEM_EMPTY(hi2
))
9593 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9596 di1
= dictitem_copy(HI2DI(hi2
));
9597 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9600 else if (*action
== 'e')
9602 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9605 else if (*action
== 'f')
9607 clear_tv(&di1
->di_tv
);
9608 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9613 copy_tv(&argvars
[0], rettv
);
9617 EMSG2(_(e_listdictarg
), "extend()");
9621 * "feedkeys()" function
9625 f_feedkeys(argvars
, rettv
)
9630 char_u
*keys
, *flags
;
9631 char_u nbuf
[NUMBUFLEN
];
9635 /* This is not allowed in the sandbox. If the commands would still be
9636 * executed in the sandbox it would be OK, but it probably happens later,
9637 * when "sandbox" is no longer set. */
9641 rettv
->vval
.v_number
= 0;
9642 keys
= get_tv_string(&argvars
[0]);
9645 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9647 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9648 for ( ; *flags
!= NUL
; ++flags
)
9652 case 'n': remap
= FALSE
; break;
9653 case 'm': remap
= TRUE
; break;
9654 case 't': typed
= TRUE
; break;
9659 /* Need to escape K_SPECIAL and CSI before putting the string in the
9660 * typeahead buffer. */
9661 keys_esc
= vim_strsave_escape_csi(keys
);
9662 if (keys_esc
!= NULL
)
9664 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9665 typebuf
.tb_len
, !typed
, FALSE
);
9668 typebuf_was_filled
= TRUE
;
9674 * "filereadable()" function
9677 f_filereadable(argvars
, rettv
)
9685 p
= get_tv_string(&argvars
[0]);
9686 if (*p
&& !mch_isdir(p
) && (fd
= mch_fopen((char *)p
, "r")) != NULL
)
9694 rettv
->vval
.v_number
= n
;
9698 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9699 * rights to write into.
9702 f_filewritable(argvars
, rettv
)
9706 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9709 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int find_what
));
9712 findfilendir(argvars
, rettv
, find_what
)
9717 #ifdef FEAT_SEARCHPATH
9719 char_u
*fresult
= NULL
;
9720 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9722 char_u pathbuf
[NUMBUFLEN
];
9728 rettv
->vval
.v_string
= NULL
;
9729 rettv
->v_type
= VAR_STRING
;
9731 #ifdef FEAT_SEARCHPATH
9732 fname
= get_tv_string(&argvars
[0]);
9734 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9736 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9744 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9745 count
= get_tv_number_chk(&argvars
[2], &error
);
9749 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9752 if (*fname
!= NUL
&& !error
)
9756 if (rettv
->v_type
== VAR_STRING
)
9758 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9759 first
? (int)STRLEN(fname
) : 0,
9763 find_what
== FINDFILE_DIR
9764 ? (char_u
*)"" : curbuf
->b_p_sua
);
9767 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9768 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9770 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9773 if (rettv
->v_type
== VAR_STRING
)
9774 rettv
->vval
.v_string
= fresult
;
9778 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9779 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9782 * Implementation of map() and filter().
9785 filter_map(argvars
, rettv
, map
)
9790 char_u buf
[NUMBUFLEN
];
9792 listitem_T
*li
, *nli
;
9802 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9805 rettv
->vval
.v_number
= 0;
9806 if (argvars
[0].v_type
== VAR_LIST
)
9808 if ((l
= argvars
[0].vval
.v_list
) == NULL
9809 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9812 else if (argvars
[0].v_type
== VAR_DICT
)
9814 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9815 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9820 EMSG2(_(e_listdictarg
), ermsg
);
9824 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9825 /* On type errors, the preceding call has already displayed an error
9826 * message. Avoid a misleading error message for an empty string that
9827 * was not passed as argument. */
9830 prepare_vimvar(VV_VAL
, &save_val
);
9831 expr
= skipwhite(expr
);
9833 /* We reset "did_emsg" to be able to detect whether an error
9834 * occurred during evaluation of the expression. */
9835 save_did_emsg
= did_emsg
;
9838 if (argvars
[0].v_type
== VAR_DICT
)
9840 prepare_vimvar(VV_KEY
, &save_key
);
9841 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9843 ht
= &d
->dv_hashtab
;
9845 todo
= (int)ht
->ht_used
;
9846 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9848 if (!HASHITEM_EMPTY(hi
))
9852 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9854 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9855 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9859 dictitem_remove(d
, di
);
9860 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9865 restore_vimvar(VV_KEY
, &save_key
);
9869 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
9871 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
9874 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
9878 listitem_remove(l
, li
);
9882 restore_vimvar(VV_VAL
, &save_val
);
9884 did_emsg
|= save_did_emsg
;
9887 copy_tv(&argvars
[0], rettv
);
9891 filter_map_one(tv
, expr
, map
, remp
)
9901 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
9903 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
9905 if (*s
!= NUL
) /* check for trailing chars after expr */
9907 EMSG2(_(e_invexpr2
), s
);
9912 /* map(): replace the list item value */
9921 /* filter(): when expr is zero remove the item */
9922 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
9924 /* On type error, nothing has been removed; return FAIL to stop the
9925 * loop. The error message was given by get_tv_number_chk(). */
9931 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
9936 * "filter()" function
9939 f_filter(argvars
, rettv
)
9943 filter_map(argvars
, rettv
, FALSE
);
9947 * "finddir({fname}[, {path}[, {count}]])" function
9950 f_finddir(argvars
, rettv
)
9954 findfilendir(argvars
, rettv
, FINDFILE_DIR
);
9958 * "findfile({fname}[, {path}[, {count}]])" function
9961 f_findfile(argvars
, rettv
)
9965 findfilendir(argvars
, rettv
, FINDFILE_FILE
);
9970 * "float2nr({float})" function
9973 f_float2nr(argvars
, rettv
)
9979 if (get_float_arg(argvars
, &f
) == OK
)
9981 if (f
< -0x7fffffff)
9982 rettv
->vval
.v_number
= -0x7fffffff;
9983 else if (f
> 0x7fffffff)
9984 rettv
->vval
.v_number
= 0x7fffffff;
9986 rettv
->vval
.v_number
= (varnumber_T
)f
;
9989 rettv
->vval
.v_number
= 0;
9993 * "floor({float})" function
9996 f_floor(argvars
, rettv
)
10002 rettv
->v_type
= VAR_FLOAT
;
10003 if (get_float_arg(argvars
, &f
) == OK
)
10004 rettv
->vval
.v_float
= floor(f
);
10006 rettv
->vval
.v_float
= 0.0;
10011 * "fnameescape({string})" function
10014 f_fnameescape(argvars
, rettv
)
10018 rettv
->vval
.v_string
= vim_strsave_fnameescape(
10019 get_tv_string(&argvars
[0]), FALSE
);
10020 rettv
->v_type
= VAR_STRING
;
10024 * "fnamemodify({fname}, {mods})" function
10027 f_fnamemodify(argvars
, rettv
)
10035 char_u
*fbuf
= NULL
;
10036 char_u buf
[NUMBUFLEN
];
10038 fname
= get_tv_string_chk(&argvars
[0]);
10039 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
10040 if (fname
== NULL
|| mods
== NULL
)
10044 len
= (int)STRLEN(fname
);
10045 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
10048 rettv
->v_type
= VAR_STRING
;
10050 rettv
->vval
.v_string
= NULL
;
10052 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
10056 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
10059 * "foldclosed()" function
10062 foldclosed_both(argvars
, rettv
, end
)
10067 #ifdef FEAT_FOLDING
10069 linenr_T first
, last
;
10071 lnum
= get_tv_lnum(argvars
);
10072 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10074 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
10077 rettv
->vval
.v_number
= (varnumber_T
)last
;
10079 rettv
->vval
.v_number
= (varnumber_T
)first
;
10084 rettv
->vval
.v_number
= -1;
10088 * "foldclosed()" function
10091 f_foldclosed(argvars
, rettv
)
10095 foldclosed_both(argvars
, rettv
, FALSE
);
10099 * "foldclosedend()" function
10102 f_foldclosedend(argvars
, rettv
)
10106 foldclosed_both(argvars
, rettv
, TRUE
);
10110 * "foldlevel()" function
10113 f_foldlevel(argvars
, rettv
)
10117 #ifdef FEAT_FOLDING
10120 lnum
= get_tv_lnum(argvars
);
10121 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10122 rettv
->vval
.v_number
= foldLevel(lnum
);
10125 rettv
->vval
.v_number
= 0;
10129 * "foldtext()" function
10133 f_foldtext(argvars
, rettv
)
10137 #ifdef FEAT_FOLDING
10145 rettv
->v_type
= VAR_STRING
;
10146 rettv
->vval
.v_string
= NULL
;
10147 #ifdef FEAT_FOLDING
10148 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
10149 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10150 <= curbuf
->b_ml
.ml_line_count
10151 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
10153 /* Find first non-empty line in the fold. */
10154 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
10155 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10157 if (!linewhite(lnum
))
10162 /* Find interesting text in this line. */
10163 s
= skipwhite(ml_get(lnum
));
10164 /* skip C comment-start */
10165 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
10167 s
= skipwhite(s
+ 2);
10168 if (*skipwhite(s
) == NUL
10169 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10171 s
= skipwhite(ml_get(lnum
+ 1));
10173 s
= skipwhite(s
+ 1);
10176 txt
= _("+-%s%3ld lines: ");
10177 r
= alloc((unsigned)(STRLEN(txt
)
10178 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
10179 + 20 /* for %3ld */
10180 + STRLEN(s
))); /* concatenated */
10183 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
10184 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10185 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
10186 len
= (int)STRLEN(r
);
10188 /* remove 'foldmarker' and 'commentstring' */
10189 foldtext_cleanup(r
+ len
);
10190 rettv
->vval
.v_string
= r
;
10197 * "foldtextresult(lnum)" function
10201 f_foldtextresult(argvars
, rettv
)
10205 #ifdef FEAT_FOLDING
10209 foldinfo_T foldinfo
;
10213 rettv
->v_type
= VAR_STRING
;
10214 rettv
->vval
.v_string
= NULL
;
10215 #ifdef FEAT_FOLDING
10216 lnum
= get_tv_lnum(argvars
);
10217 /* treat illegal types and illegal string values for {lnum} the same */
10220 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
10221 if (fold_count
> 0)
10223 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
10226 text
= vim_strsave(text
);
10227 rettv
->vval
.v_string
= text
;
10233 * "foreground()" function
10237 f_foreground(argvars
, rettv
)
10241 rettv
->vval
.v_number
= 0;
10244 gui_mch_set_foreground();
10247 win32_set_foreground();
10253 * "function()" function
10257 f_function(argvars
, rettv
)
10263 rettv
->vval
.v_number
= 0;
10264 s
= get_tv_string(&argvars
[0]);
10265 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
10266 EMSG2(_(e_invarg2
), s
);
10267 else if (!function_exists(s
))
10268 EMSG2(_("E700: Unknown function: %s"), s
);
10271 rettv
->vval
.v_string
= vim_strsave(s
);
10272 rettv
->v_type
= VAR_FUNC
;
10277 * "garbagecollect()" function
10281 f_garbagecollect(argvars
, rettv
)
10285 /* This is postponed until we are back at the toplevel, because we may be
10286 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10287 want_garbage_collect
= TRUE
;
10289 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
10290 garbage_collect_at_exit
= TRUE
;
10297 f_get(argvars
, rettv
)
10305 typval_T
*tv
= NULL
;
10307 if (argvars
[0].v_type
== VAR_LIST
)
10309 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
10313 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
10314 if (!error
&& li
!= NULL
)
10318 else if (argvars
[0].v_type
== VAR_DICT
)
10320 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
10322 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
10328 EMSG2(_(e_listdictarg
), "get()");
10332 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10333 rettv
->vval
.v_number
= 0;
10335 copy_tv(&argvars
[2], rettv
);
10338 copy_tv(tv
, rettv
);
10341 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
10344 * Get line or list of lines from buffer "buf" into "rettv".
10345 * Return a range (from start to end) of lines in rettv from the specified
10347 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
10350 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
10361 if (rettv_list_alloc(rettv
) == FAIL
)
10365 rettv
->vval
.v_number
= 0;
10367 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
10372 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
10373 p
= ml_get_buf(buf
, start
, FALSE
);
10377 rettv
->v_type
= VAR_STRING
;
10378 rettv
->vval
.v_string
= vim_strsave(p
);
10387 if (end
> buf
->b_ml
.ml_line_count
)
10388 end
= buf
->b_ml
.ml_line_count
;
10389 while (start
<= end
)
10390 if (list_append_string(rettv
->vval
.v_list
,
10391 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
10397 * "getbufline()" function
10400 f_getbufline(argvars
, rettv
)
10408 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10410 buf
= get_buf_tv(&argvars
[0]);
10413 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
10414 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10417 end
= get_tv_lnum_buf(&argvars
[2], buf
);
10419 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
10423 * "getbufvar()" function
10426 f_getbufvar(argvars
, rettv
)
10431 buf_T
*save_curbuf
;
10435 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10436 varname
= get_tv_string_chk(&argvars
[1]);
10438 buf
= get_buf_tv(&argvars
[0]);
10440 rettv
->v_type
= VAR_STRING
;
10441 rettv
->vval
.v_string
= NULL
;
10443 if (buf
!= NULL
&& varname
!= NULL
)
10445 /* set curbuf to be our buf, temporarily */
10446 save_curbuf
= curbuf
;
10449 if (*varname
== '&') /* buffer-local-option */
10450 get_option_tv(&varname
, rettv
, TRUE
);
10453 if (*varname
== NUL
)
10454 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10455 * scope prefix before the NUL byte is required by
10456 * find_var_in_ht(). */
10457 varname
= (char_u
*)"b:" + 2;
10458 /* look up the variable */
10459 v
= find_var_in_ht(&curbuf
->b_vars
.dv_hashtab
, varname
, FALSE
);
10461 copy_tv(&v
->di_tv
, rettv
);
10464 /* restore previous notion of curbuf */
10465 curbuf
= save_curbuf
;
10472 * "getchar()" function
10475 f_getchar(argvars
, rettv
)
10482 /* Position the cursor. Needed after a message that ends in a space. */
10483 windgoto(msg_row
, msg_col
);
10489 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10490 /* getchar(): blocking wait. */
10492 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
10493 /* getchar(1): only check if char avail */
10495 else if (error
|| vpeekc() == NUL
)
10496 /* illegal argument or getchar(0) and no char avail: return zero */
10499 /* getchar(0) and char avail: return char */
10508 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
10509 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
10510 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
10512 rettv
->vval
.v_number
= n
;
10513 if (IS_SPECIAL(n
) || mod_mask
!= 0)
10515 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10518 /* Turn a special key into three bytes, plus modifier. */
10521 temp
[i
++] = K_SPECIAL
;
10522 temp
[i
++] = KS_MODIFIER
;
10523 temp
[i
++] = mod_mask
;
10527 temp
[i
++] = K_SPECIAL
;
10528 temp
[i
++] = K_SECOND(n
);
10529 temp
[i
++] = K_THIRD(n
);
10532 else if (has_mbyte
)
10533 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
10538 rettv
->v_type
= VAR_STRING
;
10539 rettv
->vval
.v_string
= vim_strsave(temp
);
10542 if (n
== K_LEFTMOUSE
10543 || n
== K_LEFTMOUSE_NM
10545 || n
== K_LEFTRELEASE
10546 || n
== K_LEFTRELEASE_NM
10547 || n
== K_MIDDLEMOUSE
10548 || n
== K_MIDDLEDRAG
10549 || n
== K_MIDDLERELEASE
10550 || n
== K_RIGHTMOUSE
10551 || n
== K_RIGHTDRAG
10552 || n
== K_RIGHTRELEASE
10555 || n
== K_X1RELEASE
10558 || n
== K_X2RELEASE
10559 || n
== K_MOUSEDOWN
10562 int row
= mouse_row
;
10563 int col
= mouse_col
;
10566 # ifdef FEAT_WINDOWS
10571 if (row
>= 0 && col
>= 0)
10573 /* Find the window at the mouse coordinates and compute the
10574 * text position. */
10575 win
= mouse_find_win(&row
, &col
);
10576 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10577 # ifdef FEAT_WINDOWS
10578 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10581 vimvars
[VV_MOUSE_WIN
].vv_nr
= n
;
10582 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10583 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10591 * "getcharmod()" function
10595 f_getcharmod(argvars
, rettv
)
10599 rettv
->vval
.v_number
= mod_mask
;
10603 * "getcmdline()" function
10607 f_getcmdline(argvars
, rettv
)
10611 rettv
->v_type
= VAR_STRING
;
10612 rettv
->vval
.v_string
= get_cmdline_str();
10616 * "getcmdpos()" function
10620 f_getcmdpos(argvars
, rettv
)
10624 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10628 * "getcmdtype()" function
10632 f_getcmdtype(argvars
, rettv
)
10636 rettv
->v_type
= VAR_STRING
;
10637 rettv
->vval
.v_string
= alloc(2);
10638 if (rettv
->vval
.v_string
!= NULL
)
10640 rettv
->vval
.v_string
[0] = get_cmdline_type();
10641 rettv
->vval
.v_string
[1] = NUL
;
10646 * "getcwd()" function
10650 f_getcwd(argvars
, rettv
)
10654 char_u cwd
[MAXPATHL
];
10656 rettv
->v_type
= VAR_STRING
;
10657 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10658 rettv
->vval
.v_string
= NULL
;
10661 rettv
->vval
.v_string
= vim_strsave(cwd
);
10662 #ifdef BACKSLASH_IN_FILENAME
10663 if (rettv
->vval
.v_string
!= NULL
)
10664 slash_adjust(rettv
->vval
.v_string
);
10670 * "getfontname()" function
10674 f_getfontname(argvars
, rettv
)
10678 rettv
->v_type
= VAR_STRING
;
10679 rettv
->vval
.v_string
= NULL
;
10684 char_u
*name
= NULL
;
10686 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10688 /* Get the "Normal" font. Either the name saved by
10689 * hl_set_font_name() or from the font ID. */
10690 font
= gui
.norm_font
;
10691 name
= hl_get_font_name();
10695 name
= get_tv_string(&argvars
[0]);
10696 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10698 font
= gui_mch_get_font(name
, FALSE
);
10699 if (font
== NOFONT
)
10700 return; /* Invalid font name, return empty string. */
10702 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10703 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10704 gui_mch_free_font(font
);
10710 * "getfperm({fname})" function
10713 f_getfperm(argvars
, rettv
)
10719 char_u
*perm
= NULL
;
10720 char_u flags
[] = "rwx";
10723 fname
= get_tv_string(&argvars
[0]);
10725 rettv
->v_type
= VAR_STRING
;
10726 if (mch_stat((char *)fname
, &st
) >= 0)
10728 perm
= vim_strsave((char_u
*)"---------");
10731 for (i
= 0; i
< 9; i
++)
10733 if (st
.st_mode
& (1 << (8 - i
)))
10734 perm
[i
] = flags
[i
% 3];
10738 rettv
->vval
.v_string
= perm
;
10742 * "getfsize({fname})" function
10745 f_getfsize(argvars
, rettv
)
10752 fname
= get_tv_string(&argvars
[0]);
10754 rettv
->v_type
= VAR_NUMBER
;
10756 if (mch_stat((char *)fname
, &st
) >= 0)
10758 if (mch_isdir(fname
))
10759 rettv
->vval
.v_number
= 0;
10762 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10764 /* non-perfect check for overflow */
10765 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
10766 rettv
->vval
.v_number
= -2;
10770 rettv
->vval
.v_number
= -1;
10774 * "getftime({fname})" function
10777 f_getftime(argvars
, rettv
)
10784 fname
= get_tv_string(&argvars
[0]);
10786 if (mch_stat((char *)fname
, &st
) >= 0)
10787 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10789 rettv
->vval
.v_number
= -1;
10793 * "getftype({fname})" function
10796 f_getftype(argvars
, rettv
)
10802 char_u
*type
= NULL
;
10805 fname
= get_tv_string(&argvars
[0]);
10807 rettv
->v_type
= VAR_STRING
;
10808 if (mch_lstat((char *)fname
, &st
) >= 0)
10811 if (S_ISREG(st
.st_mode
))
10813 else if (S_ISDIR(st
.st_mode
))
10816 else if (S_ISLNK(st
.st_mode
))
10820 else if (S_ISBLK(st
.st_mode
))
10824 else if (S_ISCHR(st
.st_mode
))
10828 else if (S_ISFIFO(st
.st_mode
))
10832 else if (S_ISSOCK(st
.st_mode
))
10839 switch (st
.st_mode
& S_IFMT
)
10841 case S_IFREG
: t
= "file"; break;
10842 case S_IFDIR
: t
= "dir"; break;
10844 case S_IFLNK
: t
= "link"; break;
10847 case S_IFBLK
: t
= "bdev"; break;
10850 case S_IFCHR
: t
= "cdev"; break;
10853 case S_IFIFO
: t
= "fifo"; break;
10856 case S_IFSOCK
: t
= "socket"; break;
10858 default: t
= "other";
10861 if (mch_isdir(fname
))
10867 type
= vim_strsave((char_u
*)t
);
10869 rettv
->vval
.v_string
= type
;
10873 * "getline(lnum, [end])" function
10876 f_getline(argvars
, rettv
)
10884 lnum
= get_tv_lnum(argvars
);
10885 if (argvars
[1].v_type
== VAR_UNKNOWN
)
10892 end
= get_tv_lnum(&argvars
[1]);
10896 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
10900 * "getmatches()" function
10904 f_getmatches(argvars
, rettv
)
10908 #ifdef FEAT_SEARCH_EXTRA
10910 matchitem_T
*cur
= curwin
->w_match_head
;
10912 rettv
->vval
.v_number
= 0;
10914 if (rettv_list_alloc(rettv
) == OK
)
10916 while (cur
!= NULL
)
10918 dict
= dict_alloc();
10921 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
10922 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
10923 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
10924 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
10925 list_append_dict(rettv
->vval
.v_list
, dict
);
10933 * "getpid()" function
10937 f_getpid(argvars
, rettv
)
10941 rettv
->vval
.v_number
= mch_get_pid();
10945 * "getpos(string)" function
10948 f_getpos(argvars
, rettv
)
10956 if (rettv_list_alloc(rettv
) == OK
)
10958 l
= rettv
->vval
.v_list
;
10959 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
10961 list_append_number(l
, (varnumber_T
)fnum
);
10963 list_append_number(l
, (varnumber_T
)0);
10964 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
10966 list_append_number(l
, (fp
!= NULL
)
10967 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
10969 list_append_number(l
,
10970 #ifdef FEAT_VIRTUALEDIT
10971 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
10976 rettv
->vval
.v_number
= FALSE
;
10980 * "getqflist()" and "getloclist()" functions
10984 f_getqflist(argvars
, rettv
)
10988 #ifdef FEAT_QUICKFIX
10992 rettv
->vval
.v_number
= 0;
10993 #ifdef FEAT_QUICKFIX
10994 if (rettv_list_alloc(rettv
) == OK
)
10997 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
10999 wp
= find_win_by_nr(&argvars
[0], NULL
);
11004 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
11010 * "getreg()" function
11013 f_getreg(argvars
, rettv
)
11017 char_u
*strregname
;
11022 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11024 strregname
= get_tv_string_chk(&argvars
[0]);
11025 error
= strregname
== NULL
;
11026 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11027 arg2
= get_tv_number_chk(&argvars
[1], &error
);
11030 strregname
= vimvars
[VV_REG
].vv_str
;
11031 regname
= (strregname
== NULL
? '"' : *strregname
);
11035 rettv
->v_type
= VAR_STRING
;
11036 rettv
->vval
.v_string
= error
? NULL
:
11037 get_reg_contents(regname
, TRUE
, arg2
);
11041 * "getregtype()" function
11044 f_getregtype(argvars
, rettv
)
11048 char_u
*strregname
;
11050 char_u buf
[NUMBUFLEN
+ 2];
11053 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11055 strregname
= get_tv_string_chk(&argvars
[0]);
11056 if (strregname
== NULL
) /* type error; errmsg already given */
11058 rettv
->v_type
= VAR_STRING
;
11059 rettv
->vval
.v_string
= NULL
;
11064 /* Default to v:register */
11065 strregname
= vimvars
[VV_REG
].vv_str
;
11067 regname
= (strregname
== NULL
? '"' : *strregname
);
11073 switch (get_reg_type(regname
, ®len
))
11075 case MLINE
: buf
[0] = 'V'; break;
11076 case MCHAR
: buf
[0] = 'v'; break;
11080 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
11084 rettv
->v_type
= VAR_STRING
;
11085 rettv
->vval
.v_string
= vim_strsave(buf
);
11089 * "gettabwinvar()" function
11092 f_gettabwinvar(argvars
, rettv
)
11096 getwinvar(argvars
, rettv
, 1);
11100 * "getwinposx()" function
11104 f_getwinposx(argvars
, rettv
)
11108 rettv
->vval
.v_number
= -1;
11114 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11115 rettv
->vval
.v_number
= x
;
11121 * "getwinposy()" function
11125 f_getwinposy(argvars
, rettv
)
11129 rettv
->vval
.v_number
= -1;
11135 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11136 rettv
->vval
.v_number
= y
;
11142 * Find window specified by "vp" in tabpage "tp".
11145 find_win_by_nr(vp
, tp
)
11147 tabpage_T
*tp
; /* NULL for current tab page */
11149 #ifdef FEAT_WINDOWS
11154 nr
= get_tv_number_chk(vp
, NULL
);
11156 #ifdef FEAT_WINDOWS
11162 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
11163 wp
!= NULL
; wp
= wp
->w_next
)
11168 if (nr
== 0 || nr
== 1)
11175 * "getwinvar()" function
11178 f_getwinvar(argvars
, rettv
)
11182 getwinvar(argvars
, rettv
, 0);
11186 * getwinvar() and gettabwinvar()
11189 getwinvar(argvars
, rettv
, off
)
11192 int off
; /* 1 for gettabwinvar() */
11194 win_T
*win
, *oldcurwin
;
11199 #ifdef FEAT_WINDOWS
11201 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
11205 win
= find_win_by_nr(&argvars
[off
], tp
);
11206 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
11209 rettv
->v_type
= VAR_STRING
;
11210 rettv
->vval
.v_string
= NULL
;
11212 if (win
!= NULL
&& varname
!= NULL
)
11214 /* Set curwin to be our win, temporarily. Also set curbuf, so
11215 * that we can get buffer-local options. */
11216 oldcurwin
= curwin
;
11218 curbuf
= win
->w_buffer
;
11220 if (*varname
== '&') /* window-local-option */
11221 get_option_tv(&varname
, rettv
, 1);
11224 if (*varname
== NUL
)
11225 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11226 * scope prefix before the NUL byte is required by
11227 * find_var_in_ht(). */
11228 varname
= (char_u
*)"w:" + 2;
11229 /* look up the variable */
11230 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
11232 copy_tv(&v
->di_tv
, rettv
);
11235 /* restore previous notion of curwin */
11236 curwin
= oldcurwin
;
11237 curbuf
= curwin
->w_buffer
;
11244 * "glob()" function
11247 f_glob(argvars
, rettv
)
11254 xpc
.xp_context
= EXPAND_FILES
;
11255 rettv
->v_type
= VAR_STRING
;
11256 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
11257 NULL
, WILD_USE_NL
|WILD_SILENT
, WILD_ALL
);
11261 * "globpath()" function
11264 f_globpath(argvars
, rettv
)
11268 char_u buf1
[NUMBUFLEN
];
11269 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
11271 rettv
->v_type
= VAR_STRING
;
11273 rettv
->vval
.v_string
= NULL
;
11275 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
);
11282 f_has(argvars
, rettv
)
11289 static char *(has_list
[]) =
11310 #if defined(MACOS_X_UNIX)
11334 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11343 #ifndef CASE_INSENSITIVE_FILENAME
11349 #ifdef FEAT_AUTOCMD
11354 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11355 "balloon_multiline",
11358 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11360 # ifdef ALL_BUILTIN_TCAPS
11361 "all_builtin_terms",
11364 #ifdef FEAT_BYTEOFF
11367 #ifdef FEAT_CINDENT
11370 #ifdef FEAT_CLIENTSERVER
11373 #ifdef FEAT_CLIPBOARD
11376 #ifdef FEAT_CMDL_COMPL
11379 #ifdef FEAT_CMDHIST
11382 #ifdef FEAT_COMMENTS
11391 #ifdef CURSOR_SHAPE
11397 #ifdef FEAT_CON_DIALOG
11400 #ifdef FEAT_GUI_DIALOG
11406 #ifdef FEAT_DIGRAPHS
11412 #ifdef FEAT_EMACS_TAGS
11415 "eval", /* always present, of course! */
11416 #ifdef FEAT_EX_EXTRA
11419 #ifdef FEAT_SEARCH_EXTRA
11425 #ifdef FEAT_SEARCHPATH
11428 #if defined(UNIX) && !defined(USE_SYSTEM)
11431 #ifdef FEAT_FIND_ID
11437 #ifdef FEAT_FOLDING
11443 #if !defined(USE_SYSTEM) && defined(UNIX)
11446 #ifdef FEAT_GETTEXT
11452 #ifdef FEAT_GUI_ATHENA
11453 # ifdef FEAT_GUI_NEXTAW
11459 #ifdef FEAT_GUI_GTK
11465 #ifdef FEAT_GUI_GNOME
11468 #ifdef FEAT_GUI_MAC
11471 #ifdef FEAT_GUI_MOTIF
11474 #ifdef FEAT_GUI_PHOTON
11477 #ifdef FEAT_GUI_W16
11480 #ifdef FEAT_GUI_W32
11483 #ifdef FEAT_HANGULIN
11486 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11489 #ifdef FEAT_INS_EXPAND
11492 #ifdef FEAT_JUMPLIST
11498 #ifdef FEAT_LANGMAP
11501 #ifdef FEAT_LIBCALL
11504 #ifdef FEAT_LINEBREAK
11510 #ifdef FEAT_LISTCMDS
11513 #ifdef FEAT_LOCALMAP
11519 #ifdef FEAT_SESSION
11522 #ifdef FEAT_MODIFY_FNAME
11528 #ifdef FEAT_MOUSESHAPE
11531 #if defined(UNIX) || defined(VMS)
11532 # ifdef FEAT_MOUSE_DEC
11535 # ifdef FEAT_MOUSE_GPM
11538 # ifdef FEAT_MOUSE_JSB
11541 # ifdef FEAT_MOUSE_NET
11544 # ifdef FEAT_MOUSE_PTERM
11547 # ifdef FEAT_SYSMOUSE
11550 # ifdef FEAT_MOUSE_XTERM
11557 #ifdef FEAT_MBYTE_IME
11560 #ifdef FEAT_MULTI_LANG
11563 #ifdef FEAT_MZSCHEME
11564 #ifndef DYNAMIC_MZSCHEME
11571 #ifdef FEAT_OSFILETYPE
11574 #ifdef FEAT_PATH_EXTRA
11578 #ifndef DYNAMIC_PERL
11583 #ifndef DYNAMIC_PYTHON
11587 #ifdef FEAT_POSTSCRIPT
11590 #ifdef FEAT_PRINTER
11593 #ifdef FEAT_PROFILE
11596 #ifdef FEAT_RELTIME
11599 #ifdef FEAT_QUICKFIX
11602 #ifdef FEAT_RIGHTLEFT
11605 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11608 #ifdef FEAT_SCROLLBIND
11611 #ifdef FEAT_CMDL_INFO
11618 #ifdef FEAT_SMARTINDENT
11624 #ifdef FEAT_STL_OPT
11627 #ifdef FEAT_SUN_WORKSHOP
11630 #ifdef FEAT_NETBEANS_INTG
11639 #if defined(USE_SYSTEM) || !defined(UNIX)
11642 #ifdef FEAT_TAG_BINS
11645 #ifdef FEAT_TAG_OLDSTATIC
11648 #ifdef FEAT_TAG_ANYWHITE
11652 # ifndef DYNAMIC_TCL
11659 #ifdef FEAT_TERMRESPONSE
11662 #ifdef FEAT_TEXTOBJ
11665 #ifdef HAVE_TGETENT
11671 #ifdef FEAT_TOOLBAR
11674 #ifdef FEAT_USR_CMDS
11675 "user-commands", /* was accidentally included in 5.4 */
11678 #ifdef FEAT_VIMINFO
11681 #ifdef FEAT_VERTSPLIT
11684 #ifdef FEAT_VIRTUALEDIT
11690 #ifdef FEAT_VISUALEXTRA
11693 #ifdef FEAT_VREPLACE
11696 #ifdef FEAT_WILDIGN
11699 #ifdef FEAT_WILDMENU
11702 #ifdef FEAT_WINDOWS
11708 #ifdef FEAT_WRITEBACKUP
11714 #ifdef FEAT_XFONTSET
11720 #ifdef USE_XSMP_INTERACT
11723 #ifdef FEAT_XCLIPBOARD
11726 #ifdef FEAT_XTERM_SAVE
11729 #if defined(UNIX) && defined(FEAT_X11)
11735 name
= get_tv_string(&argvars
[0]);
11736 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11737 if (STRICMP(name
, has_list
[i
]) == 0)
11745 if (STRNICMP(name
, "patch", 5) == 0)
11746 n
= has_patch(atoi((char *)name
+ 5));
11747 else if (STRICMP(name
, "vim_starting") == 0)
11748 n
= (starting
!= 0);
11749 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11750 else if (STRICMP(name
, "balloon_multiline") == 0)
11751 n
= multiline_balloon_available();
11754 else if (STRICMP(name
, "tcl") == 0)
11755 n
= tcl_enabled(FALSE
);
11757 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11758 else if (STRICMP(name
, "iconv") == 0)
11759 n
= iconv_enabled(FALSE
);
11761 #ifdef DYNAMIC_MZSCHEME
11762 else if (STRICMP(name
, "mzscheme") == 0)
11763 n
= mzscheme_enabled(FALSE
);
11765 #ifdef DYNAMIC_RUBY
11766 else if (STRICMP(name
, "ruby") == 0)
11767 n
= ruby_enabled(FALSE
);
11769 #ifdef DYNAMIC_PYTHON
11770 else if (STRICMP(name
, "python") == 0)
11771 n
= python_enabled(FALSE
);
11773 #ifdef DYNAMIC_PERL
11774 else if (STRICMP(name
, "perl") == 0)
11775 n
= perl_enabled(FALSE
);
11778 else if (STRICMP(name
, "gui_running") == 0)
11779 n
= (gui
.in_use
|| gui
.starting
);
11780 # ifdef FEAT_GUI_W32
11781 else if (STRICMP(name
, "gui_win32s") == 0)
11782 n
= gui_is_win32s();
11784 # ifdef FEAT_BROWSE
11785 else if (STRICMP(name
, "browse") == 0)
11786 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11790 else if (STRICMP(name
, "syntax_items") == 0)
11791 n
= syntax_present(curbuf
);
11793 #if defined(WIN3264)
11794 else if (STRICMP(name
, "win95") == 0)
11795 n
= mch_windows95();
11797 #ifdef FEAT_NETBEANS_INTG
11798 else if (STRICMP(name
, "netbeans_enabled") == 0)
11803 rettv
->vval
.v_number
= n
;
11807 * "has_key()" function
11810 f_has_key(argvars
, rettv
)
11814 rettv
->vval
.v_number
= 0;
11815 if (argvars
[0].v_type
!= VAR_DICT
)
11817 EMSG(_(e_dictreq
));
11820 if (argvars
[0].vval
.v_dict
== NULL
)
11823 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11824 get_tv_string(&argvars
[1]), -1) != NULL
;
11828 * "haslocaldir()" function
11832 f_haslocaldir(argvars
, rettv
)
11836 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11840 * "hasmapto()" function
11843 f_hasmapto(argvars
, rettv
)
11849 char_u buf
[NUMBUFLEN
];
11852 name
= get_tv_string(&argvars
[0]);
11853 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11854 mode
= (char_u
*)"nvo";
11857 mode
= get_tv_string_buf(&argvars
[1], buf
);
11858 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11859 abbr
= get_tv_number(&argvars
[2]);
11862 if (map_to_exists(name
, mode
, abbr
))
11863 rettv
->vval
.v_number
= TRUE
;
11865 rettv
->vval
.v_number
= FALSE
;
11869 * "histadd()" function
11873 f_histadd(argvars
, rettv
)
11877 #ifdef FEAT_CMDHIST
11880 char_u buf
[NUMBUFLEN
];
11883 rettv
->vval
.v_number
= FALSE
;
11884 if (check_restricted() || check_secure())
11886 #ifdef FEAT_CMDHIST
11887 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11888 histype
= str
!= NULL
? get_histtype(str
) : -1;
11891 str
= get_tv_string_buf(&argvars
[1], buf
);
11894 add_to_history(histype
, str
, FALSE
, NUL
);
11895 rettv
->vval
.v_number
= TRUE
;
11903 * "histdel()" function
11907 f_histdel(argvars
, rettv
)
11911 #ifdef FEAT_CMDHIST
11913 char_u buf
[NUMBUFLEN
];
11916 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11919 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
11920 /* only one argument: clear entire history */
11921 n
= clr_history(get_histtype(str
));
11922 else if (argvars
[1].v_type
== VAR_NUMBER
)
11923 /* index given: remove that entry */
11924 n
= del_history_idx(get_histtype(str
),
11925 (int)get_tv_number(&argvars
[1]));
11927 /* string given: remove all matching entries */
11928 n
= del_history_entry(get_histtype(str
),
11929 get_tv_string_buf(&argvars
[1], buf
));
11930 rettv
->vval
.v_number
= n
;
11932 rettv
->vval
.v_number
= 0;
11937 * "histget()" function
11941 f_histget(argvars
, rettv
)
11945 #ifdef FEAT_CMDHIST
11950 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11952 rettv
->vval
.v_string
= NULL
;
11955 type
= get_histtype(str
);
11956 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11957 idx
= get_history_idx(type
);
11959 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
11960 /* -1 on type error */
11961 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
11964 rettv
->vval
.v_string
= NULL
;
11966 rettv
->v_type
= VAR_STRING
;
11970 * "histnr()" function
11974 f_histnr(argvars
, rettv
)
11980 #ifdef FEAT_CMDHIST
11981 char_u
*history
= get_tv_string_chk(&argvars
[0]);
11983 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
11984 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
11985 i
= get_history_idx(i
);
11989 rettv
->vval
.v_number
= i
;
11993 * "highlightID(name)" function
11996 f_hlID(argvars
, rettv
)
12000 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
12004 * "highlight_exists()" function
12007 f_hlexists(argvars
, rettv
)
12011 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
12015 * "hostname()" function
12019 f_hostname(argvars
, rettv
)
12023 char_u hostname
[256];
12025 mch_get_host_name(hostname
, 256);
12026 rettv
->v_type
= VAR_STRING
;
12027 rettv
->vval
.v_string
= vim_strsave(hostname
);
12035 f_iconv(argvars
, rettv
)
12040 char_u buf1
[NUMBUFLEN
];
12041 char_u buf2
[NUMBUFLEN
];
12042 char_u
*from
, *to
, *str
;
12046 rettv
->v_type
= VAR_STRING
;
12047 rettv
->vval
.v_string
= NULL
;
12050 str
= get_tv_string(&argvars
[0]);
12051 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
12052 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
12053 vimconv
.vc_type
= CONV_NONE
;
12054 convert_setup(&vimconv
, from
, to
);
12056 /* If the encodings are equal, no conversion needed. */
12057 if (vimconv
.vc_type
== CONV_NONE
)
12058 rettv
->vval
.v_string
= vim_strsave(str
);
12060 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
12062 convert_setup(&vimconv
, NULL
, NULL
);
12069 * "indent()" function
12072 f_indent(argvars
, rettv
)
12078 lnum
= get_tv_lnum(argvars
);
12079 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12080 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
12082 rettv
->vval
.v_number
= -1;
12086 * "index()" function
12089 f_index(argvars
, rettv
)
12098 rettv
->vval
.v_number
= -1;
12099 if (argvars
[0].v_type
!= VAR_LIST
)
12101 EMSG(_(e_listreq
));
12104 l
= argvars
[0].vval
.v_list
;
12107 item
= l
->lv_first
;
12108 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12112 /* Start at specified item. Use the cached index that list_find()
12113 * sets, so that a negative number also works. */
12114 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
12116 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12117 ic
= get_tv_number_chk(&argvars
[3], &error
);
12122 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
12123 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
12125 rettv
->vval
.v_number
= idx
;
12131 static int inputsecret_flag
= 0;
12133 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
12136 * This function is used by f_input() and f_inputdialog() functions. The third
12137 * argument to f_input() specifies the type of completion to use at the
12138 * prompt. The third argument to f_inputdialog() specifies the value to return
12139 * when the user cancels the prompt.
12142 get_user_input(argvars
, rettv
, inputdialog
)
12147 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
12150 char_u buf
[NUMBUFLEN
];
12151 int cmd_silent_save
= cmd_silent
;
12152 char_u
*defstr
= (char_u
*)"";
12153 int xp_type
= EXPAND_NOTHING
;
12154 char_u
*xp_arg
= NULL
;
12156 rettv
->v_type
= VAR_STRING
;
12157 rettv
->vval
.v_string
= NULL
;
12159 #ifdef NO_CONSOLE_INPUT
12160 /* While starting up, there is no place to enter text. */
12161 if (no_console_input())
12165 cmd_silent
= FALSE
; /* Want to see the prompt. */
12166 if (prompt
!= NULL
)
12168 /* Only the part of the message after the last NL is considered as
12169 * prompt for the command line */
12170 p
= vim_strrchr(prompt
, '\n');
12180 msg_puts_attr(prompt
, echo_attr
);
12181 msg_didout
= FALSE
;
12185 cmdline_row
= msg_row
;
12187 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12189 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
12190 if (defstr
!= NULL
)
12191 stuffReadbuffSpec(defstr
);
12193 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
12199 rettv
->vval
.v_string
= NULL
;
12201 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
12202 if (xp_name
== NULL
)
12205 xp_namelen
= (int)STRLEN(xp_name
);
12207 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
12213 if (defstr
!= NULL
)
12214 rettv
->vval
.v_string
=
12215 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
12220 /* since the user typed this, no need to wait for return */
12221 need_wait_return
= FALSE
;
12222 msg_didout
= FALSE
;
12224 cmd_silent
= cmd_silent_save
;
12228 * "input()" function
12229 * Also handles inputsecret() when inputsecret is set.
12232 f_input(argvars
, rettv
)
12236 get_user_input(argvars
, rettv
, FALSE
);
12240 * "inputdialog()" function
12243 f_inputdialog(argvars
, rettv
)
12247 #if defined(FEAT_GUI_TEXTDIALOG)
12248 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12249 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
12252 char_u buf
[NUMBUFLEN
];
12253 char_u
*defstr
= (char_u
*)"";
12255 message
= get_tv_string_chk(&argvars
[0]);
12256 if (argvars
[1].v_type
!= VAR_UNKNOWN
12257 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
12258 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
12261 if (message
!= NULL
&& defstr
!= NULL
12262 && do_dialog(VIM_QUESTION
, NULL
, message
,
12263 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
12264 rettv
->vval
.v_string
= vim_strsave(IObuff
);
12267 if (message
!= NULL
&& defstr
!= NULL
12268 && argvars
[1].v_type
!= VAR_UNKNOWN
12269 && argvars
[2].v_type
!= VAR_UNKNOWN
)
12270 rettv
->vval
.v_string
= vim_strsave(
12271 get_tv_string_buf(&argvars
[2], buf
));
12273 rettv
->vval
.v_string
= NULL
;
12275 rettv
->v_type
= VAR_STRING
;
12279 get_user_input(argvars
, rettv
, TRUE
);
12283 * "inputlist()" function
12286 f_inputlist(argvars
, rettv
)
12294 rettv
->vval
.v_number
= 0;
12295 #ifdef NO_CONSOLE_INPUT
12296 /* While starting up, there is no place to enter text. */
12297 if (no_console_input())
12300 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
12302 EMSG2(_(e_listarg
), "inputlist()");
12307 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
12308 lines_left
= Rows
; /* avoid more prompt */
12312 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
12314 msg_puts(get_tv_string(&li
->li_tv
));
12318 /* Ask for choice. */
12319 selected
= prompt_for_number(&mouse_used
);
12321 selected
-= lines_left
;
12323 rettv
->vval
.v_number
= selected
;
12327 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
12330 * "inputrestore()" function
12334 f_inputrestore(argvars
, rettv
)
12338 if (ga_userinput
.ga_len
> 0)
12340 --ga_userinput
.ga_len
;
12341 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12342 + ga_userinput
.ga_len
);
12343 rettv
->vval
.v_number
= 0; /* OK */
12345 else if (p_verbose
> 1)
12347 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
12348 rettv
->vval
.v_number
= 1; /* Failed */
12353 * "inputsave()" function
12357 f_inputsave(argvars
, rettv
)
12361 /* Add an entry to the stack of typeahead storage. */
12362 if (ga_grow(&ga_userinput
, 1) == OK
)
12364 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12365 + ga_userinput
.ga_len
);
12366 ++ga_userinput
.ga_len
;
12367 rettv
->vval
.v_number
= 0; /* OK */
12370 rettv
->vval
.v_number
= 1; /* Failed */
12374 * "inputsecret()" function
12377 f_inputsecret(argvars
, rettv
)
12382 ++inputsecret_flag
;
12383 f_input(argvars
, rettv
);
12385 --inputsecret_flag
;
12389 * "insert()" function
12392 f_insert(argvars
, rettv
)
12401 rettv
->vval
.v_number
= 0;
12402 if (argvars
[0].v_type
!= VAR_LIST
)
12403 EMSG2(_(e_listarg
), "insert()");
12404 else if ((l
= argvars
[0].vval
.v_list
) != NULL
12405 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
12407 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12408 before
= get_tv_number_chk(&argvars
[2], &error
);
12410 return; /* type error; errmsg already given */
12412 if (before
== l
->lv_len
)
12416 item
= list_find(l
, before
);
12419 EMSGN(_(e_listidx
), before
);
12425 list_insert_tv(l
, &argvars
[1], item
);
12426 copy_tv(&argvars
[0], rettv
);
12432 * "isdirectory()" function
12435 f_isdirectory(argvars
, rettv
)
12439 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
12443 * "islocked()" function
12446 f_islocked(argvars
, rettv
)
12454 rettv
->vval
.v_number
= -1;
12455 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
12457 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
12460 EMSG(_(e_trailing
));
12463 if (lv
.ll_tv
== NULL
)
12465 if (check_changedtick(lv
.ll_name
))
12466 rettv
->vval
.v_number
= 1; /* always locked */
12469 di
= find_var(lv
.ll_name
, NULL
);
12472 /* Consider a variable locked when:
12473 * 1. the variable itself is locked
12474 * 2. the value of the variable is locked.
12475 * 3. the List or Dict value is locked.
12477 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
12478 || tv_islocked(&di
->di_tv
));
12482 else if (lv
.ll_range
)
12483 EMSG(_("E786: Range not allowed"));
12484 else if (lv
.ll_newkey
!= NULL
)
12485 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
12486 else if (lv
.ll_list
!= NULL
)
12488 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
12490 /* Dictionary item. */
12491 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
12498 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
12501 * Turn a dict into a list:
12502 * "what" == 0: list of keys
12503 * "what" == 1: list of values
12504 * "what" == 2: list of items
12507 dict_list(argvars
, rettv
, what
)
12520 rettv
->vval
.v_number
= 0;
12521 if (argvars
[0].v_type
!= VAR_DICT
)
12523 EMSG(_(e_dictreq
));
12526 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
12529 if (rettv_list_alloc(rettv
) == FAIL
)
12532 todo
= (int)d
->dv_hashtab
.ht_used
;
12533 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12535 if (!HASHITEM_EMPTY(hi
))
12540 li
= listitem_alloc();
12543 list_append(rettv
->vval
.v_list
, li
);
12548 li
->li_tv
.v_type
= VAR_STRING
;
12549 li
->li_tv
.v_lock
= 0;
12550 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12552 else if (what
== 1)
12555 copy_tv(&di
->di_tv
, &li
->li_tv
);
12561 li
->li_tv
.v_type
= VAR_LIST
;
12562 li
->li_tv
.v_lock
= 0;
12563 li
->li_tv
.vval
.v_list
= l2
;
12568 li2
= listitem_alloc();
12571 list_append(l2
, li2
);
12572 li2
->li_tv
.v_type
= VAR_STRING
;
12573 li2
->li_tv
.v_lock
= 0;
12574 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12576 li2
= listitem_alloc();
12579 list_append(l2
, li2
);
12580 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12587 * "items(dict)" function
12590 f_items(argvars
, rettv
)
12594 dict_list(argvars
, rettv
, 2);
12598 * "join()" function
12601 f_join(argvars
, rettv
)
12608 rettv
->vval
.v_number
= 0;
12609 if (argvars
[0].v_type
!= VAR_LIST
)
12611 EMSG(_(e_listreq
));
12614 if (argvars
[0].vval
.v_list
== NULL
)
12616 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12617 sep
= (char_u
*)" ";
12619 sep
= get_tv_string_chk(&argvars
[1]);
12621 rettv
->v_type
= VAR_STRING
;
12625 ga_init2(&ga
, (int)sizeof(char), 80);
12626 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12627 ga_append(&ga
, NUL
);
12628 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12631 rettv
->vval
.v_string
= NULL
;
12635 * "keys()" function
12638 f_keys(argvars
, rettv
)
12642 dict_list(argvars
, rettv
, 0);
12646 * "last_buffer_nr()" function.
12650 f_last_buffer_nr(argvars
, rettv
)
12657 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12658 if (n
< buf
->b_fnum
)
12661 rettv
->vval
.v_number
= n
;
12668 f_len(argvars
, rettv
)
12672 switch (argvars
[0].v_type
)
12676 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12677 get_tv_string(&argvars
[0]));
12680 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12683 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12686 EMSG(_("E701: Invalid type for len()"));
12691 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12694 libcall_common(argvars
, rettv
, type
)
12699 #ifdef FEAT_LIBCALL
12701 char_u
**string_result
;
12705 rettv
->v_type
= type
;
12706 if (type
== VAR_NUMBER
)
12707 rettv
->vval
.v_number
= 0;
12709 rettv
->vval
.v_string
= NULL
;
12711 if (check_restricted() || check_secure())
12714 #ifdef FEAT_LIBCALL
12715 /* The first two args must be strings, otherwise its meaningless */
12716 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12719 if (argvars
[2].v_type
== VAR_STRING
)
12720 string_in
= argvars
[2].vval
.v_string
;
12721 if (type
== VAR_NUMBER
)
12722 string_result
= NULL
;
12724 string_result
= &rettv
->vval
.v_string
;
12725 if (mch_libcall(argvars
[0].vval
.v_string
,
12726 argvars
[1].vval
.v_string
,
12728 argvars
[2].vval
.v_number
,
12731 && type
== VAR_NUMBER
)
12732 rettv
->vval
.v_number
= nr_result
;
12738 * "libcall()" function
12741 f_libcall(argvars
, rettv
)
12745 libcall_common(argvars
, rettv
, VAR_STRING
);
12749 * "libcallnr()" function
12752 f_libcallnr(argvars
, rettv
)
12756 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12760 * "line(string)" function
12763 f_line(argvars
, rettv
)
12771 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12774 rettv
->vval
.v_number
= lnum
;
12778 * "line2byte(lnum)" function
12782 f_line2byte(argvars
, rettv
)
12786 #ifndef FEAT_BYTEOFF
12787 rettv
->vval
.v_number
= -1;
12791 lnum
= get_tv_lnum(argvars
);
12792 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12793 rettv
->vval
.v_number
= -1;
12795 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12796 if (rettv
->vval
.v_number
>= 0)
12797 ++rettv
->vval
.v_number
;
12802 * "lispindent(lnum)" function
12805 f_lispindent(argvars
, rettv
)
12813 pos
= curwin
->w_cursor
;
12814 lnum
= get_tv_lnum(argvars
);
12815 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12817 curwin
->w_cursor
.lnum
= lnum
;
12818 rettv
->vval
.v_number
= get_lisp_indent();
12819 curwin
->w_cursor
= pos
;
12823 rettv
->vval
.v_number
= -1;
12827 * "localtime()" function
12831 f_localtime(argvars
, rettv
)
12835 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12838 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12841 get_maparg(argvars
, rettv
, exact
)
12848 char_u buf
[NUMBUFLEN
];
12849 char_u
*keys_buf
= NULL
;
12855 /* return empty string for failure */
12856 rettv
->v_type
= VAR_STRING
;
12857 rettv
->vval
.v_string
= NULL
;
12859 keys
= get_tv_string(&argvars
[0]);
12863 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12865 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12866 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12867 abbr
= get_tv_number(&argvars
[2]);
12870 which
= (char_u
*)"";
12874 mode
= get_map_mode(&which
, 0);
12876 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12877 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
12878 vim_free(keys_buf
);
12882 ga
.ga_itemsize
= 1;
12883 ga
.ga_growsize
= 40;
12885 while (*rhs
!= NUL
)
12886 ga_concat(&ga
, str2special(&rhs
, FALSE
));
12888 ga_append(&ga
, NUL
);
12889 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12895 * "log10()" function
12898 f_log10(argvars
, rettv
)
12904 rettv
->v_type
= VAR_FLOAT
;
12905 if (get_float_arg(argvars
, &f
) == OK
)
12906 rettv
->vval
.v_float
= log10(f
);
12908 rettv
->vval
.v_float
= 0.0;
12916 f_map(argvars
, rettv
)
12920 filter_map(argvars
, rettv
, TRUE
);
12924 * "maparg()" function
12927 f_maparg(argvars
, rettv
)
12931 get_maparg(argvars
, rettv
, TRUE
);
12935 * "mapcheck()" function
12938 f_mapcheck(argvars
, rettv
)
12942 get_maparg(argvars
, rettv
, FALSE
);
12945 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
12948 find_some_match(argvars
, rettv
, type
)
12953 char_u
*str
= NULL
;
12954 char_u
*expr
= NULL
;
12956 regmatch_T regmatch
;
12957 char_u patbuf
[NUMBUFLEN
];
12958 char_u strbuf
[NUMBUFLEN
];
12962 colnr_T startcol
= 0;
12965 listitem_T
*li
= NULL
;
12967 char_u
*tofree
= NULL
;
12969 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12971 p_cpo
= (char_u
*)"";
12973 rettv
->vval
.v_number
= -1;
12976 /* return empty list when there are no matches */
12977 if (rettv_list_alloc(rettv
) == FAIL
)
12980 else if (type
== 2)
12982 rettv
->v_type
= VAR_STRING
;
12983 rettv
->vval
.v_string
= NULL
;
12986 if (argvars
[0].v_type
== VAR_LIST
)
12988 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
12993 expr
= str
= get_tv_string(&argvars
[0]);
12995 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
12999 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13003 start
= get_tv_number_chk(&argvars
[2], &error
);
13008 li
= list_find(l
, start
);
13011 idx
= l
->lv_idx
; /* use the cached index */
13017 if (start
> (long)STRLEN(str
))
13019 /* When "count" argument is there ignore matches before "start",
13020 * otherwise skip part of the string. Differs when pattern is "^"
13022 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13028 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13029 nth
= get_tv_number_chk(&argvars
[3], &error
);
13034 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
13035 if (regmatch
.regprog
!= NULL
)
13037 regmatch
.rm_ic
= p_ic
;
13049 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
13054 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
13056 if (match
&& --nth
<= 0)
13058 if (l
== NULL
&& !match
)
13061 /* Advance to just after the match. */
13070 startcol
= (colnr_T
)(regmatch
.startp
[0]
13071 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
13073 startcol
= regmatch
.startp
[0] + 1 - str
;
13084 /* return list with matched string and submatches */
13085 for (i
= 0; i
< NSUBEXP
; ++i
)
13087 if (regmatch
.endp
[i
] == NULL
)
13089 if (list_append_string(rettv
->vval
.v_list
,
13090 (char_u
*)"", 0) == FAIL
)
13093 else if (list_append_string(rettv
->vval
.v_list
,
13094 regmatch
.startp
[i
],
13095 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
13100 else if (type
== 2)
13102 /* return matched string */
13104 copy_tv(&li
->li_tv
, rettv
);
13106 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
13107 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
13109 else if (l
!= NULL
)
13110 rettv
->vval
.v_number
= idx
;
13114 rettv
->vval
.v_number
=
13115 (varnumber_T
)(regmatch
.startp
[0] - str
);
13117 rettv
->vval
.v_number
=
13118 (varnumber_T
)(regmatch
.endp
[0] - str
);
13119 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
13122 vim_free(regmatch
.regprog
);
13131 * "match()" function
13134 f_match(argvars
, rettv
)
13138 find_some_match(argvars
, rettv
, 1);
13142 * "matchadd()" function
13145 f_matchadd(argvars
, rettv
)
13149 #ifdef FEAT_SEARCH_EXTRA
13150 char_u buf
[NUMBUFLEN
];
13151 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
13152 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
13153 int prio
= 10; /* default priority */
13157 rettv
->vval
.v_number
= -1;
13159 if (grp
== NULL
|| pat
== NULL
)
13161 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13163 prio
= get_tv_number_chk(&argvars
[2], &error
);
13164 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13165 id
= get_tv_number_chk(&argvars
[3], &error
);
13169 if (id
>= 1 && id
<= 3)
13171 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
13175 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
13180 * "matcharg()" function
13183 f_matcharg(argvars
, rettv
)
13187 if (rettv_list_alloc(rettv
) == OK
)
13189 #ifdef FEAT_SEARCH_EXTRA
13190 int id
= get_tv_number(&argvars
[0]);
13193 if (id
>= 1 && id
<= 3)
13195 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
13197 list_append_string(rettv
->vval
.v_list
,
13198 syn_id2name(m
->hlg_id
), -1);
13199 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
13203 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13204 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13212 * "matchdelete()" function
13215 f_matchdelete(argvars
, rettv
)
13219 #ifdef FEAT_SEARCH_EXTRA
13220 rettv
->vval
.v_number
= match_delete(curwin
,
13221 (int)get_tv_number(&argvars
[0]), TRUE
);
13226 * "matchend()" function
13229 f_matchend(argvars
, rettv
)
13233 find_some_match(argvars
, rettv
, 0);
13237 * "matchlist()" function
13240 f_matchlist(argvars
, rettv
)
13244 find_some_match(argvars
, rettv
, 3);
13248 * "matchstr()" function
13251 f_matchstr(argvars
, rettv
)
13255 find_some_match(argvars
, rettv
, 2);
13258 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
13261 max_min(argvars
, rettv
, domax
)
13270 if (argvars
[0].v_type
== VAR_LIST
)
13275 l
= argvars
[0].vval
.v_list
;
13281 n
= get_tv_number_chk(&li
->li_tv
, &error
);
13287 i
= get_tv_number_chk(&li
->li_tv
, &error
);
13288 if (domax
? i
> n
: i
< n
)
13294 else if (argvars
[0].v_type
== VAR_DICT
)
13301 d
= argvars
[0].vval
.v_dict
;
13304 todo
= (int)d
->dv_hashtab
.ht_used
;
13305 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
13307 if (!HASHITEM_EMPTY(hi
))
13310 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
13316 else if (domax
? i
> n
: i
< n
)
13323 EMSG(_(e_listdictarg
));
13324 rettv
->vval
.v_number
= error
? 0 : n
;
13331 f_max(argvars
, rettv
)
13335 max_min(argvars
, rettv
, TRUE
);
13342 f_min(argvars
, rettv
)
13346 max_min(argvars
, rettv
, FALSE
);
13349 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
13352 * Create the directory in which "dir" is located, and higher levels when
13356 mkdir_recurse(dir
, prot
)
13364 /* Get end of directory name in "dir".
13365 * We're done when it's "/" or "c:/". */
13366 p
= gettail_sep(dir
);
13367 if (p
<= get_past_head(dir
))
13370 /* If the directory exists we're done. Otherwise: create it.*/
13371 updir
= vim_strnsave(dir
, (int)(p
- dir
));
13374 if (mch_isdir(updir
))
13376 else if (mkdir_recurse(updir
, prot
) == OK
)
13377 r
= vim_mkdir_emsg(updir
, prot
);
13384 * "mkdir()" function
13387 f_mkdir(argvars
, rettv
)
13392 char_u buf
[NUMBUFLEN
];
13395 rettv
->vval
.v_number
= FAIL
;
13396 if (check_restricted() || check_secure())
13399 dir
= get_tv_string_buf(&argvars
[0], buf
);
13400 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13402 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13403 prot
= get_tv_number_chk(&argvars
[2], NULL
);
13404 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
13405 mkdir_recurse(dir
, prot
);
13407 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
13412 * "mode()" function
13416 f_mode(argvars
, rettv
)
13429 buf
[0] = VIsual_mode
+ 's' - 'v';
13431 buf
[0] = VIsual_mode
;
13435 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
13436 || State
== CONFIRM
)
13439 if (State
== ASKMORE
)
13441 else if (State
== CONFIRM
)
13444 else if (State
== EXTERNCMD
)
13446 else if (State
& INSERT
)
13448 #ifdef FEAT_VREPLACE
13449 if (State
& VREPLACE_FLAG
)
13456 if (State
& REPLACE_FLAG
)
13461 else if (State
& CMDLINE
)
13467 else if (exmode_active
)
13479 /* A zero number or empty string argument: return only major mode. */
13480 if (!(argvars
[0].v_type
== VAR_NUMBER
&& argvars
[0].vval
.v_number
!= 0)
13481 && !(argvars
[0].v_type
== VAR_STRING
13482 && *get_tv_string(&argvars
[0]) != NUL
))
13485 rettv
->vval
.v_string
= vim_strsave(buf
);
13486 rettv
->v_type
= VAR_STRING
;
13490 * "nextnonblank()" function
13493 f_nextnonblank(argvars
, rettv
)
13499 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
13501 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
13506 if (*skipwhite(ml_get(lnum
)) != NUL
)
13509 rettv
->vval
.v_number
= lnum
;
13513 * "nr2char()" function
13516 f_nr2char(argvars
, rettv
)
13520 char_u buf
[NUMBUFLEN
];
13524 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
13528 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
13531 rettv
->v_type
= VAR_STRING
;
13532 rettv
->vval
.v_string
= vim_strsave(buf
);
13536 * "pathshorten()" function
13539 f_pathshorten(argvars
, rettv
)
13545 rettv
->v_type
= VAR_STRING
;
13546 p
= get_tv_string_chk(&argvars
[0]);
13548 rettv
->vval
.v_string
= NULL
;
13551 p
= vim_strsave(p
);
13552 rettv
->vval
.v_string
= p
;
13563 f_pow(argvars
, rettv
)
13569 rettv
->v_type
= VAR_FLOAT
;
13570 if (get_float_arg(argvars
, &fx
) == OK
13571 && get_float_arg(&argvars
[1], &fy
) == OK
)
13572 rettv
->vval
.v_float
= pow(fx
, fy
);
13574 rettv
->vval
.v_float
= 0.0;
13579 * "prevnonblank()" function
13582 f_prevnonblank(argvars
, rettv
)
13588 lnum
= get_tv_lnum(argvars
);
13589 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
13592 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
13594 rettv
->vval
.v_number
= lnum
;
13597 #ifdef HAVE_STDARG_H
13598 /* This dummy va_list is here because:
13599 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13600 * - locally in the function results in a "used before set" warning
13601 * - using va_start() to initialize it gives "function with fixed args" error */
13606 * "printf()" function
13609 f_printf(argvars
, rettv
)
13613 rettv
->v_type
= VAR_STRING
;
13614 rettv
->vval
.v_string
= NULL
;
13615 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
13617 char_u buf
[NUMBUFLEN
];
13620 int saved_did_emsg
= did_emsg
;
13623 /* Get the required length, allocate the buffer and do it for real. */
13625 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
13626 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
13629 s
= alloc(len
+ 1);
13632 rettv
->vval
.v_string
= s
;
13633 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
13636 did_emsg
|= saved_did_emsg
;
13642 * "pumvisible()" function
13646 f_pumvisible(argvars
, rettv
)
13650 rettv
->vval
.v_number
= 0;
13651 #ifdef FEAT_INS_EXPAND
13653 rettv
->vval
.v_number
= 1;
13658 * "range()" function
13661 f_range(argvars
, rettv
)
13671 start
= get_tv_number_chk(&argvars
[0], &error
);
13672 if (argvars
[1].v_type
== VAR_UNKNOWN
)
13679 end
= get_tv_number_chk(&argvars
[1], &error
);
13680 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13681 stride
= get_tv_number_chk(&argvars
[2], &error
);
13684 rettv
->vval
.v_number
= 0;
13686 return; /* type error; errmsg already given */
13688 EMSG(_("E726: Stride is zero"));
13689 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
13690 EMSG(_("E727: Start past end"));
13693 if (rettv_list_alloc(rettv
) == OK
)
13694 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
13695 if (list_append_number(rettv
->vval
.v_list
,
13696 (varnumber_T
)i
) == FAIL
)
13702 * "readfile()" function
13705 f_readfile(argvars
, rettv
)
13709 int binary
= FALSE
;
13713 #define FREAD_SIZE 200 /* optimized for text lines */
13714 char_u buf
[FREAD_SIZE
];
13715 int readlen
; /* size of last fread() */
13716 int buflen
; /* nr of valid chars in buf[] */
13717 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
13718 int tolist
; /* first byte in buf[] still to be put in list */
13719 int chop
; /* how many CR to chop off */
13720 char_u
*prev
= NULL
; /* previously read bytes, if any */
13721 int prevlen
= 0; /* length of "prev" if not NULL */
13724 long maxline
= MAXLNUM
;
13727 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13729 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
13731 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13732 maxline
= get_tv_number(&argvars
[2]);
13735 if (rettv_list_alloc(rettv
) == FAIL
)
13738 /* Always open the file in binary mode, library functions have a mind of
13739 * their own about CR-LF conversion. */
13740 fname
= get_tv_string(&argvars
[0]);
13741 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
13743 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
13748 while (cnt
< maxline
|| maxline
< 0)
13750 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
13751 buflen
= filtd
+ readlen
;
13753 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
13755 if (buf
[filtd
] == '\n' || readlen
<= 0)
13757 /* Only when in binary mode add an empty list item when the
13758 * last line ends in a '\n'. */
13759 if (!binary
&& readlen
== 0 && filtd
== 0)
13762 /* Found end-of-line or end-of-file: add a text line to the
13766 while (filtd
- chop
- 1 >= tolist
13767 && buf
[filtd
- chop
- 1] == '\r')
13769 len
= filtd
- tolist
- chop
;
13771 s
= vim_strnsave(buf
+ tolist
, len
);
13774 s
= alloc((unsigned)(prevlen
+ len
+ 1));
13777 mch_memmove(s
, prev
, prevlen
);
13780 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
13781 s
[prevlen
+ len
] = NUL
;
13784 tolist
= filtd
+ 1;
13786 li
= listitem_alloc();
13792 li
->li_tv
.v_type
= VAR_STRING
;
13793 li
->li_tv
.v_lock
= 0;
13794 li
->li_tv
.vval
.v_string
= s
;
13795 list_append(rettv
->vval
.v_list
, li
);
13797 if (++cnt
>= maxline
&& maxline
>= 0)
13802 else if (buf
[filtd
] == NUL
)
13810 /* "buf" is full, need to move text to an allocated buffer */
13813 prev
= vim_strnsave(buf
, buflen
);
13818 s
= alloc((unsigned)(prevlen
+ buflen
));
13821 mch_memmove(s
, prev
, prevlen
);
13822 mch_memmove(s
+ prevlen
, buf
, buflen
);
13832 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13838 * For a negative line count use only the lines at the end of the file,
13842 while (cnt
> -maxline
)
13844 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
13852 #if defined(FEAT_RELTIME)
13853 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
13856 * Convert a List to proftime_T.
13857 * Return FAIL when there is something wrong.
13860 list2proftime(arg
, tm
)
13867 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
13868 || arg
->vval
.v_list
->lv_len
!= 2)
13870 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
13871 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
13879 return error
? FAIL
: OK
;
13881 #endif /* FEAT_RELTIME */
13884 * "reltime()" function
13887 f_reltime(argvars
, rettv
)
13891 #ifdef FEAT_RELTIME
13895 if (argvars
[0].v_type
== VAR_UNKNOWN
)
13897 /* No arguments: get current time. */
13898 profile_start(&res
);
13900 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
13902 if (list2proftime(&argvars
[0], &res
) == FAIL
)
13908 /* Two arguments: compute the difference. */
13909 if (list2proftime(&argvars
[0], &start
) == FAIL
13910 || list2proftime(&argvars
[1], &res
) == FAIL
)
13912 profile_sub(&res
, &start
);
13915 if (rettv_list_alloc(rettv
) == OK
)
13926 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
13927 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
13933 * "reltimestr()" function
13936 f_reltimestr(argvars
, rettv
)
13940 #ifdef FEAT_RELTIME
13944 rettv
->v_type
= VAR_STRING
;
13945 rettv
->vval
.v_string
= NULL
;
13946 #ifdef FEAT_RELTIME
13947 if (list2proftime(&argvars
[0], &tm
) == OK
)
13948 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
13952 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13953 static void make_connection
__ARGS((void));
13954 static int check_connection
__ARGS((void));
13959 if (X_DISPLAY
== NULL
13965 x_force_connect
= TRUE
;
13967 x_force_connect
= FALSE
;
13975 if (X_DISPLAY
== NULL
)
13977 EMSG(_("E240: No connection to Vim server"));
13984 #ifdef FEAT_CLIENTSERVER
13985 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
13988 remote_common(argvars
, rettv
, expr
)
13993 char_u
*server_name
;
13996 char_u buf
[NUMBUFLEN
];
14003 if (check_restricted() || check_secure())
14007 if (check_connection() == FAIL
)
14011 server_name
= get_tv_string_chk(&argvars
[0]);
14012 if (server_name
== NULL
)
14013 return; /* type error; errmsg already given */
14014 keys
= get_tv_string_buf(&argvars
[1], buf
);
14016 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14018 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
14023 EMSG(r
); /* sending worked but evaluation failed */
14025 EMSG2(_("E241: Unable to send to %s"), server_name
);
14029 rettv
->vval
.v_string
= r
;
14031 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14037 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
14038 v
.di_tv
.v_type
= VAR_STRING
;
14039 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
14040 idvar
= get_tv_string_chk(&argvars
[2]);
14042 set_var(idvar
, &v
.di_tv
, FALSE
);
14043 vim_free(v
.di_tv
.vval
.v_string
);
14049 * "remote_expr()" function
14053 f_remote_expr(argvars
, rettv
)
14057 rettv
->v_type
= VAR_STRING
;
14058 rettv
->vval
.v_string
= NULL
;
14059 #ifdef FEAT_CLIENTSERVER
14060 remote_common(argvars
, rettv
, TRUE
);
14065 * "remote_foreground()" function
14069 f_remote_foreground(argvars
, rettv
)
14073 rettv
->vval
.v_number
= 0;
14074 #ifdef FEAT_CLIENTSERVER
14076 /* On Win32 it's done in this application. */
14078 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
14080 if (server_name
!= NULL
)
14081 serverForeground(server_name
);
14084 /* Send a foreground() expression to the server. */
14085 argvars
[1].v_type
= VAR_STRING
;
14086 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
14087 argvars
[2].v_type
= VAR_UNKNOWN
;
14088 remote_common(argvars
, rettv
, TRUE
);
14089 vim_free(argvars
[1].vval
.v_string
);
14096 f_remote_peek(argvars
, rettv
)
14100 #ifdef FEAT_CLIENTSERVER
14108 if (check_restricted() || check_secure())
14110 rettv
->vval
.v_number
= -1;
14113 serverid
= get_tv_string_chk(&argvars
[0]);
14114 if (serverid
== NULL
)
14116 rettv
->vval
.v_number
= -1;
14117 return; /* type error; errmsg already given */
14120 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14122 rettv
->vval
.v_number
= -1;
14125 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
14126 rettv
->vval
.v_number
= (s
!= NULL
);
14129 rettv
->vval
.v_number
= 0;
14130 if (check_connection() == FAIL
)
14133 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
14134 serverStrToWin(serverid
), &s
);
14137 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
14141 v
.di_tv
.v_type
= VAR_STRING
;
14142 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
14143 retvar
= get_tv_string_chk(&argvars
[1]);
14144 if (retvar
!= NULL
)
14145 set_var(retvar
, &v
.di_tv
, FALSE
);
14146 vim_free(v
.di_tv
.vval
.v_string
);
14149 rettv
->vval
.v_number
= -1;
14155 f_remote_read(argvars
, rettv
)
14161 #ifdef FEAT_CLIENTSERVER
14162 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
14164 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
14167 /* The server's HWND is encoded in the 'id' parameter */
14170 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14172 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
14175 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
14176 serverStrToWin(serverid
), &r
, FALSE
) < 0)
14178 EMSG(_("E277: Unable to read a server reply"));
14181 rettv
->v_type
= VAR_STRING
;
14182 rettv
->vval
.v_string
= r
;
14186 * "remote_send()" function
14190 f_remote_send(argvars
, rettv
)
14194 rettv
->v_type
= VAR_STRING
;
14195 rettv
->vval
.v_string
= NULL
;
14196 #ifdef FEAT_CLIENTSERVER
14197 remote_common(argvars
, rettv
, FALSE
);
14202 * "remove()" function
14205 f_remove(argvars
, rettv
)
14210 listitem_T
*item
, *item2
;
14218 rettv
->vval
.v_number
= 0;
14219 if (argvars
[0].v_type
== VAR_DICT
)
14221 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14222 EMSG2(_(e_toomanyarg
), "remove()");
14223 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
14224 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
14226 key
= get_tv_string_chk(&argvars
[1]);
14229 di
= dict_find(d
, key
, -1);
14231 EMSG2(_(e_dictkey
), key
);
14234 *rettv
= di
->di_tv
;
14235 init_tv(&di
->di_tv
);
14236 dictitem_remove(d
, di
);
14241 else if (argvars
[0].v_type
!= VAR_LIST
)
14242 EMSG2(_(e_listdictarg
), "remove()");
14243 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14244 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
14248 idx
= get_tv_number_chk(&argvars
[1], &error
);
14250 ; /* type error: do nothing, errmsg already given */
14251 else if ((item
= list_find(l
, idx
)) == NULL
)
14252 EMSGN(_(e_listidx
), idx
);
14255 if (argvars
[2].v_type
== VAR_UNKNOWN
)
14257 /* Remove one item, return its value. */
14258 list_remove(l
, item
, item
);
14259 *rettv
= item
->li_tv
;
14264 /* Remove range of items, return list with values. */
14265 end
= get_tv_number_chk(&argvars
[2], &error
);
14267 ; /* type error: do nothing */
14268 else if ((item2
= list_find(l
, end
)) == NULL
)
14269 EMSGN(_(e_listidx
), end
);
14274 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
14280 if (li
== NULL
) /* didn't find "item2" after "item" */
14281 EMSG(_(e_invrange
));
14284 list_remove(l
, item
, item2
);
14285 if (rettv_list_alloc(rettv
) == OK
)
14287 l
= rettv
->vval
.v_list
;
14288 l
->lv_first
= item
;
14289 l
->lv_last
= item2
;
14290 item
->li_prev
= NULL
;
14291 item2
->li_next
= NULL
;
14302 * "rename({from}, {to})" function
14305 f_rename(argvars
, rettv
)
14309 char_u buf
[NUMBUFLEN
];
14311 if (check_restricted() || check_secure())
14312 rettv
->vval
.v_number
= -1;
14314 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
14315 get_tv_string_buf(&argvars
[1], buf
));
14319 * "repeat()" function
14323 f_repeat(argvars
, rettv
)
14334 n
= get_tv_number(&argvars
[1]);
14335 if (argvars
[0].v_type
== VAR_LIST
)
14337 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
14339 if (list_extend(rettv
->vval
.v_list
,
14340 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
14345 p
= get_tv_string(&argvars
[0]);
14346 rettv
->v_type
= VAR_STRING
;
14347 rettv
->vval
.v_string
= NULL
;
14349 slen
= (int)STRLEN(p
);
14354 r
= alloc(len
+ 1);
14357 for (i
= 0; i
< n
; i
++)
14358 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
14362 rettv
->vval
.v_string
= r
;
14367 * "resolve()" function
14370 f_resolve(argvars
, rettv
)
14376 p
= get_tv_string(&argvars
[0]);
14377 #ifdef FEAT_SHORTCUT
14381 v
= mch_resolve_shortcut(p
);
14383 rettv
->vval
.v_string
= v
;
14385 rettv
->vval
.v_string
= vim_strsave(p
);
14388 # ifdef HAVE_READLINK
14390 char_u buf
[MAXPATHL
+ 1];
14393 char_u
*remain
= NULL
;
14395 int is_relative_to_current
= FALSE
;
14396 int has_trailing_pathsep
= FALSE
;
14399 p
= vim_strsave(p
);
14401 if (p
[0] == '.' && (vim_ispathsep(p
[1])
14402 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
14403 is_relative_to_current
= TRUE
;
14406 if (len
> 0 && after_pathsep(p
, p
+ len
))
14407 has_trailing_pathsep
= TRUE
;
14409 q
= getnextcomp(p
);
14412 /* Separate the first path component in "p", and keep the
14413 * remainder (beginning with the path separator). */
14414 remain
= vim_strsave(q
- 1);
14422 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
14431 EMSG(_("E655: Too many symbolic links (cycle?)"));
14432 rettv
->vval
.v_string
= NULL
;
14436 /* Ensure that the result will have a trailing path separator
14437 * if the argument has one. */
14438 if (remain
== NULL
&& has_trailing_pathsep
)
14441 /* Separate the first path component in the link value and
14442 * concatenate the remainders. */
14443 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
14446 if (remain
== NULL
)
14447 remain
= vim_strsave(q
- 1);
14450 cpy
= concat_str(q
- 1, remain
);
14461 if (q
> p
&& *q
== NUL
)
14463 /* Ignore trailing path separator. */
14467 if (q
> p
&& !mch_isFullName(buf
))
14469 /* symlink is relative to directory of argument */
14470 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
14474 STRCPY(gettail(cpy
), buf
);
14482 p
= vim_strsave(buf
);
14486 if (remain
== NULL
)
14489 /* Append the first path component of "remain" to "p". */
14490 q
= getnextcomp(remain
+ 1);
14491 len
= q
- remain
- (*q
!= NUL
);
14492 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
14495 STRNCAT(cpy
, remain
, len
);
14499 /* Shorten "remain". */
14501 STRMOVE(remain
, q
- 1);
14509 /* If the result is a relative path name, make it explicitly relative to
14510 * the current directory if and only if the argument had this form. */
14511 if (!vim_ispathsep(*p
))
14513 if (is_relative_to_current
14517 || vim_ispathsep(p
[1])
14520 || vim_ispathsep(p
[2]))))))
14522 /* Prepend "./". */
14523 cpy
= concat_str((char_u
*)"./", p
);
14530 else if (!is_relative_to_current
)
14532 /* Strip leading "./". */
14534 while (q
[0] == '.' && vim_ispathsep(q
[1]))
14541 /* Ensure that the result will have no trailing path separator
14542 * if the argument had none. But keep "/" or "//". */
14543 if (!has_trailing_pathsep
)
14546 if (after_pathsep(p
, q
))
14547 *gettail_sep(p
) = NUL
;
14550 rettv
->vval
.v_string
= p
;
14553 rettv
->vval
.v_string
= vim_strsave(p
);
14557 simplify_filename(rettv
->vval
.v_string
);
14559 #ifdef HAVE_READLINK
14562 rettv
->v_type
= VAR_STRING
;
14566 * "reverse({list})" function
14569 f_reverse(argvars
, rettv
)
14574 listitem_T
*li
, *ni
;
14576 rettv
->vval
.v_number
= 0;
14577 if (argvars
[0].v_type
!= VAR_LIST
)
14578 EMSG2(_(e_listarg
), "reverse()");
14579 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14580 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
14583 l
->lv_first
= l
->lv_last
= NULL
;
14588 list_append(l
, li
);
14591 rettv
->vval
.v_list
= l
;
14592 rettv
->v_type
= VAR_LIST
;
14594 l
->lv_idx
= l
->lv_len
- l
->lv_idx
- 1;
14598 #define SP_NOMOVE 0x01 /* don't move cursor */
14599 #define SP_REPEAT 0x02 /* repeat to find outer pair */
14600 #define SP_RETCOUNT 0x04 /* return matchcount */
14601 #define SP_SETPCMARK 0x08 /* set previous context mark */
14602 #define SP_START 0x10 /* accept match at start position */
14603 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14604 #define SP_END 0x40 /* leave cursor at end of match */
14606 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
14609 * Get flags for a search function.
14610 * Possibly sets "p_ws".
14611 * Returns BACKWARD, FORWARD or zero (for an error).
14614 get_search_arg(varp
, flagsp
)
14620 char_u nbuf
[NUMBUFLEN
];
14623 if (varp
->v_type
!= VAR_UNKNOWN
)
14625 flags
= get_tv_string_buf_chk(varp
, nbuf
);
14627 return 0; /* type error; errmsg already given */
14628 while (*flags
!= NUL
)
14632 case 'b': dir
= BACKWARD
; break;
14633 case 'w': p_ws
= TRUE
; break;
14634 case 'W': p_ws
= FALSE
; break;
14636 if (flagsp
!= NULL
)
14639 case 'c': mask
= SP_START
; break;
14640 case 'e': mask
= SP_END
; break;
14641 case 'm': mask
= SP_RETCOUNT
; break;
14642 case 'n': mask
= SP_NOMOVE
; break;
14643 case 'p': mask
= SP_SUBPAT
; break;
14644 case 'r': mask
= SP_REPEAT
; break;
14645 case 's': mask
= SP_SETPCMARK
; break;
14649 EMSG2(_(e_invarg2
), flags
);
14664 * Shared by search() and searchpos() functions
14667 search_cmn(argvars
, match_pos
, flagsp
)
14676 int save_p_ws
= p_ws
;
14678 int retval
= 0; /* default: FAIL */
14679 long lnum_stop
= 0;
14681 #ifdef FEAT_RELTIME
14682 long time_limit
= 0;
14684 int options
= SEARCH_KEEP
;
14687 pat
= get_tv_string(&argvars
[0]);
14688 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
14692 if (flags
& SP_START
)
14693 options
|= SEARCH_START
;
14694 if (flags
& SP_END
)
14695 options
|= SEARCH_END
;
14697 /* Optional arguments: line number to stop searching and timeout. */
14698 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14700 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
14703 #ifdef FEAT_RELTIME
14704 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
14706 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
14707 if (time_limit
< 0)
14713 #ifdef FEAT_RELTIME
14714 /* Set the time limit, if there is one. */
14715 profile_setlimit(time_limit
, &tm
);
14719 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
14720 * Check to make sure only those flags are set.
14721 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14722 * flags cannot be set. Check for that condition also.
14724 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
14725 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14727 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
14731 pos
= save_cursor
= curwin
->w_cursor
;
14732 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14733 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
14734 if (subpatnum
!= FAIL
)
14736 if (flags
& SP_SUBPAT
)
14737 retval
= subpatnum
;
14740 if (flags
& SP_SETPCMARK
)
14742 curwin
->w_cursor
= pos
;
14743 if (match_pos
!= NULL
)
14745 /* Store the match cursor position */
14746 match_pos
->lnum
= pos
.lnum
;
14747 match_pos
->col
= pos
.col
+ 1;
14749 /* "/$" will put the cursor after the end of the line, may need to
14750 * correct that here */
14754 /* If 'n' flag is used: restore cursor position. */
14755 if (flags
& SP_NOMOVE
)
14756 curwin
->w_cursor
= save_cursor
;
14758 curwin
->w_set_curswant
= TRUE
;
14767 * "round({float})" function
14770 f_round(argvars
, rettv
)
14776 rettv
->v_type
= VAR_FLOAT
;
14777 if (get_float_arg(argvars
, &f
) == OK
)
14778 /* round() is not in C90, use ceil() or floor() instead. */
14779 rettv
->vval
.v_float
= f
> 0 ? floor(f
+ 0.5) : ceil(f
- 0.5);
14781 rettv
->vval
.v_float
= 0.0;
14786 * "search()" function
14789 f_search(argvars
, rettv
)
14795 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
14799 * "searchdecl()" function
14802 f_searchdecl(argvars
, rettv
)
14811 rettv
->vval
.v_number
= 1; /* default: FAIL */
14813 name
= get_tv_string_chk(&argvars
[0]);
14814 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14816 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
14817 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14818 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
14820 if (!error
&& name
!= NULL
)
14821 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
14822 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
14826 * Used by searchpair() and searchpairpos()
14829 searchpair_cmn(argvars
, match_pos
)
14833 char_u
*spat
, *mpat
, *epat
;
14835 int save_p_ws
= p_ws
;
14838 char_u nbuf1
[NUMBUFLEN
];
14839 char_u nbuf2
[NUMBUFLEN
];
14840 char_u nbuf3
[NUMBUFLEN
];
14841 int retval
= 0; /* default: FAIL */
14842 long lnum_stop
= 0;
14843 long time_limit
= 0;
14845 /* Get the three pattern arguments: start, middle, end. */
14846 spat
= get_tv_string_chk(&argvars
[0]);
14847 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
14848 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
14849 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
14850 goto theend
; /* type error */
14852 /* Handle the optional fourth argument: flags */
14853 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
14857 /* Don't accept SP_END or SP_SUBPAT.
14858 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14860 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
14861 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14863 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
14867 /* Using 'r' implies 'W', otherwise it doesn't work. */
14868 if (flags
& SP_REPEAT
)
14871 /* Optional fifth argument: skip expression */
14872 if (argvars
[3].v_type
== VAR_UNKNOWN
14873 || argvars
[4].v_type
== VAR_UNKNOWN
)
14874 skip
= (char_u
*)"";
14877 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
14878 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
14880 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
14883 #ifdef FEAT_RELTIME
14884 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
14886 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
14887 if (time_limit
< 0)
14894 goto theend
; /* type error */
14896 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
14897 match_pos
, lnum_stop
, time_limit
);
14906 * "searchpair()" function
14909 f_searchpair(argvars
, rettv
)
14913 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
14917 * "searchpairpos()" function
14920 f_searchpairpos(argvars
, rettv
)
14928 rettv
->vval
.v_number
= 0;
14930 if (rettv_list_alloc(rettv
) == FAIL
)
14933 if (searchpair_cmn(argvars
, &match_pos
) > 0)
14935 lnum
= match_pos
.lnum
;
14936 col
= match_pos
.col
;
14939 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14940 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14944 * Search for a start/middle/end thing.
14945 * Used by searchpair(), see its documentation for the details.
14946 * Returns 0 or -1 for no match,
14949 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
14950 lnum_stop
, time_limit
)
14951 char_u
*spat
; /* start pattern */
14952 char_u
*mpat
; /* middle pattern */
14953 char_u
*epat
; /* end pattern */
14954 int dir
; /* BACKWARD or FORWARD */
14955 char_u
*skip
; /* skip expression */
14956 int flags
; /* SP_SETPCMARK and other SP_ values */
14958 linenr_T lnum_stop
; /* stop at this line if not zero */
14959 long time_limit
; /* stop after this many msec */
14962 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
14973 int options
= SEARCH_KEEP
;
14976 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14978 p_cpo
= (char_u
*)"";
14980 #ifdef FEAT_RELTIME
14981 /* Set the time limit, if there is one. */
14982 profile_setlimit(time_limit
, &tm
);
14985 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14986 * start/middle/end (pat3, for the top pair). */
14987 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
14988 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
14989 if (pat2
== NULL
|| pat3
== NULL
)
14991 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
14993 STRCPY(pat3
, pat2
);
14995 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14997 if (flags
& SP_START
)
14998 options
|= SEARCH_START
;
15000 save_cursor
= curwin
->w_cursor
;
15001 pos
= curwin
->w_cursor
;
15002 clearpos(&firstpos
);
15003 clearpos(&foundpos
);
15007 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15008 options
, RE_SEARCH
, lnum_stop
, &tm
);
15009 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
15010 /* didn't find it or found the first match again: FAIL */
15013 if (firstpos
.lnum
== 0)
15015 if (equalpos(pos
, foundpos
))
15017 /* Found the same position again. Can happen with a pattern that
15018 * has "\zs" at the end and searching backwards. Advance one
15019 * character and try again. */
15020 if (dir
== BACKWARD
)
15027 /* clear the start flag to avoid getting stuck here */
15028 options
&= ~SEARCH_START
;
15030 /* If the skip pattern matches, ignore this match. */
15033 save_pos
= curwin
->w_cursor
;
15034 curwin
->w_cursor
= pos
;
15035 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
15036 curwin
->w_cursor
= save_pos
;
15039 /* Evaluating {skip} caused an error, break here. */
15040 curwin
->w_cursor
= save_cursor
;
15048 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
15050 /* Found end when searching backwards or start when searching
15051 * forward: nested pair. */
15053 pat
= pat2
; /* nested, don't search for middle */
15057 /* Found end when searching forward or start when searching
15058 * backward: end of (nested) pair; or found middle in outer pair. */
15060 pat
= pat3
; /* outer level, search for middle */
15065 /* Found the match: return matchcount or line number. */
15066 if (flags
& SP_RETCOUNT
)
15070 if (flags
& SP_SETPCMARK
)
15072 curwin
->w_cursor
= pos
;
15073 if (!(flags
& SP_REPEAT
))
15075 nest
= 1; /* search for next unmatched */
15079 if (match_pos
!= NULL
)
15081 /* Store the match cursor position */
15082 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
15083 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
15086 /* If 'n' flag is used or search failed: restore cursor position. */
15087 if ((flags
& SP_NOMOVE
) || retval
== 0)
15088 curwin
->w_cursor
= save_cursor
;
15099 * "searchpos()" function
15102 f_searchpos(argvars
, rettv
)
15112 rettv
->vval
.v_number
= 0;
15114 if (rettv_list_alloc(rettv
) == FAIL
)
15117 n
= search_cmn(argvars
, &match_pos
, &flags
);
15120 lnum
= match_pos
.lnum
;
15121 col
= match_pos
.col
;
15124 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15125 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15126 if (flags
& SP_SUBPAT
)
15127 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
15133 f_server2client(argvars
, rettv
)
15137 #ifdef FEAT_CLIENTSERVER
15138 char_u buf
[NUMBUFLEN
];
15139 char_u
*server
= get_tv_string_chk(&argvars
[0]);
15140 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
15142 rettv
->vval
.v_number
= -1;
15143 if (server
== NULL
|| reply
== NULL
)
15145 if (check_restricted() || check_secure())
15148 if (check_connection() == FAIL
)
15152 if (serverSendReply(server
, reply
) < 0)
15154 EMSG(_("E258: Unable to send to client"));
15157 rettv
->vval
.v_number
= 0;
15159 rettv
->vval
.v_number
= -1;
15165 f_serverlist(argvars
, rettv
)
15171 #ifdef FEAT_CLIENTSERVER
15173 r
= serverGetVimNames();
15176 if (X_DISPLAY
!= NULL
)
15177 r
= serverGetVimNames(X_DISPLAY
);
15180 rettv
->v_type
= VAR_STRING
;
15181 rettv
->vval
.v_string
= r
;
15185 * "setbufvar()" function
15189 f_setbufvar(argvars
, rettv
)
15195 char_u
*varname
, *bufvarname
;
15197 char_u nbuf
[NUMBUFLEN
];
15199 rettv
->vval
.v_number
= 0;
15201 if (check_restricted() || check_secure())
15203 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
15204 varname
= get_tv_string_chk(&argvars
[1]);
15205 buf
= get_buf_tv(&argvars
[0]);
15206 varp
= &argvars
[2];
15208 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15210 /* set curbuf to be our buf, temporarily */
15211 aucmd_prepbuf(&aco
, buf
);
15213 if (*varname
== '&')
15220 numval
= get_tv_number_chk(varp
, &error
);
15221 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15222 if (!error
&& strval
!= NULL
)
15223 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15227 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15228 if (bufvarname
!= NULL
)
15230 STRCPY(bufvarname
, "b:");
15231 STRCPY(bufvarname
+ 2, varname
);
15232 set_var(bufvarname
, varp
, TRUE
);
15233 vim_free(bufvarname
);
15237 /* reset notion of buffer */
15238 aucmd_restbuf(&aco
);
15243 * "setcmdpos()" function
15246 f_setcmdpos(argvars
, rettv
)
15250 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
15253 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
15257 * "setline()" function
15260 f_setline(argvars
, rettv
)
15265 char_u
*line
= NULL
;
15267 listitem_T
*li
= NULL
;
15269 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
15271 lnum
= get_tv_lnum(&argvars
[0]);
15272 if (argvars
[1].v_type
== VAR_LIST
)
15274 l
= argvars
[1].vval
.v_list
;
15278 line
= get_tv_string_chk(&argvars
[1]);
15280 rettv
->vval
.v_number
= 0; /* OK */
15285 /* list argument, get next string */
15288 line
= get_tv_string_chk(&li
->li_tv
);
15292 rettv
->vval
.v_number
= 1; /* FAIL */
15293 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
15295 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
15297 /* existing line, replace it */
15298 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
15300 changed_bytes(lnum
, 0);
15301 if (lnum
== curwin
->w_cursor
.lnum
)
15302 check_cursor_col();
15303 rettv
->vval
.v_number
= 0; /* OK */
15306 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
15308 /* lnum is one past the last line, append the line */
15310 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
15311 rettv
->vval
.v_number
= 0; /* OK */
15314 if (l
== NULL
) /* only one string argument */
15320 appended_lines_mark(lcount
, added
);
15323 static void set_qf_ll_list
__ARGS((win_T
*wp
, typval_T
*list_arg
, typval_T
*action_arg
, typval_T
*rettv
));
15326 * Used by "setqflist()" and "setloclist()" functions
15330 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
15332 typval_T
*list_arg
;
15333 typval_T
*action_arg
;
15336 #ifdef FEAT_QUICKFIX
15341 rettv
->vval
.v_number
= -1;
15343 #ifdef FEAT_QUICKFIX
15344 if (list_arg
->v_type
!= VAR_LIST
)
15345 EMSG(_(e_listreq
));
15348 list_T
*l
= list_arg
->vval
.v_list
;
15350 if (action_arg
->v_type
== VAR_STRING
)
15352 act
= get_tv_string_chk(action_arg
);
15354 return; /* type error; errmsg already given */
15355 if (*act
== 'a' || *act
== 'r')
15359 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
15360 rettv
->vval
.v_number
= 0;
15366 * "setloclist()" function
15370 f_setloclist(argvars
, rettv
)
15376 rettv
->vval
.v_number
= -1;
15378 win
= find_win_by_nr(&argvars
[0], NULL
);
15380 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
15384 * "setmatches()" function
15387 f_setmatches(argvars
, rettv
)
15391 #ifdef FEAT_SEARCH_EXTRA
15396 rettv
->vval
.v_number
= -1;
15397 if (argvars
[0].v_type
!= VAR_LIST
)
15399 EMSG(_(e_listreq
));
15402 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
15405 /* To some extent make sure that we are dealing with a list from
15406 * "getmatches()". */
15410 if (li
->li_tv
.v_type
!= VAR_DICT
15411 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
15416 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
15417 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
15418 && dict_find(d
, (char_u
*)"priority", -1) != NULL
15419 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
15427 clear_matches(curwin
);
15431 d
= li
->li_tv
.vval
.v_dict
;
15432 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
15433 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
15434 (int)get_dict_number(d
, (char_u
*)"priority"),
15435 (int)get_dict_number(d
, (char_u
*)"id"));
15438 rettv
->vval
.v_number
= 0;
15444 * "setpos()" function
15448 f_setpos(argvars
, rettv
)
15456 rettv
->vval
.v_number
= -1;
15457 name
= get_tv_string_chk(argvars
);
15460 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
15463 if (name
[0] == '.' && name
[1] == NUL
)
15466 if (fnum
== curbuf
->b_fnum
)
15468 curwin
->w_cursor
= pos
;
15470 rettv
->vval
.v_number
= 0;
15475 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
15478 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
15479 rettv
->vval
.v_number
= 0;
15488 * "setqflist()" function
15492 f_setqflist(argvars
, rettv
)
15496 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
15500 * "setreg()" function
15503 f_setreg(argvars
, rettv
)
15508 char_u
*strregname
;
15519 strregname
= get_tv_string_chk(argvars
);
15520 rettv
->vval
.v_number
= 1; /* FAIL is default */
15522 if (strregname
== NULL
)
15523 return; /* type error; errmsg already given */
15524 regname
= *strregname
;
15525 if (regname
== 0 || regname
== '@')
15527 else if (regname
== '=')
15530 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15532 stropt
= get_tv_string_chk(&argvars
[2]);
15533 if (stropt
== NULL
)
15534 return; /* type error */
15535 for (; *stropt
!= NUL
; ++stropt
)
15538 case 'a': case 'A': /* append */
15541 case 'v': case 'c': /* character-wise selection */
15544 case 'V': case 'l': /* line-wise selection */
15548 case 'b': case Ctrl_V
: /* block-wise selection */
15549 yank_type
= MBLOCK
;
15550 if (VIM_ISDIGIT(stropt
[1]))
15553 block_len
= getdigits(&stropt
) - 1;
15561 strval
= get_tv_string_chk(&argvars
[1]);
15562 if (strval
!= NULL
)
15563 write_reg_contents_ex(regname
, strval
, -1,
15564 append
, yank_type
, block_len
);
15565 rettv
->vval
.v_number
= 0;
15569 * "settabwinvar()" function
15572 f_settabwinvar(argvars
, rettv
)
15576 setwinvar(argvars
, rettv
, 1);
15580 * "setwinvar()" function
15583 f_setwinvar(argvars
, rettv
)
15587 setwinvar(argvars
, rettv
, 0);
15591 * "setwinvar()" and "settabwinvar()" functions
15594 setwinvar(argvars
, rettv
, off
)
15600 #ifdef FEAT_WINDOWS
15601 win_T
*save_curwin
;
15602 tabpage_T
*save_curtab
;
15604 char_u
*varname
, *winvarname
;
15606 char_u nbuf
[NUMBUFLEN
];
15609 rettv
->vval
.v_number
= 0;
15611 if (check_restricted() || check_secure())
15614 #ifdef FEAT_WINDOWS
15616 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
15620 win
= find_win_by_nr(&argvars
[off
], tp
);
15621 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
15622 varp
= &argvars
[off
+ 2];
15624 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15626 #ifdef FEAT_WINDOWS
15627 /* set curwin to be our win, temporarily */
15628 save_curwin
= curwin
;
15629 save_curtab
= curtab
;
15630 goto_tabpage_tp(tp
);
15631 if (!win_valid(win
))
15634 curbuf
= curwin
->w_buffer
;
15637 if (*varname
== '&')
15644 numval
= get_tv_number_chk(varp
, &error
);
15645 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15646 if (!error
&& strval
!= NULL
)
15647 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15651 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15652 if (winvarname
!= NULL
)
15654 STRCPY(winvarname
, "w:");
15655 STRCPY(winvarname
+ 2, varname
);
15656 set_var(winvarname
, varp
, TRUE
);
15657 vim_free(winvarname
);
15661 #ifdef FEAT_WINDOWS
15662 /* Restore current tabpage and window, if still valid (autocomands can
15663 * make them invalid). */
15664 if (valid_tabpage(save_curtab
))
15665 goto_tabpage_tp(save_curtab
);
15666 if (win_valid(save_curwin
))
15668 curwin
= save_curwin
;
15669 curbuf
= curwin
->w_buffer
;
15676 * "shellescape({string})" function
15679 f_shellescape(argvars
, rettv
)
15683 rettv
->vval
.v_string
= vim_strsave_shellescape(get_tv_string(&argvars
[0]));
15684 rettv
->v_type
= VAR_STRING
;
15688 * "simplify()" function
15691 f_simplify(argvars
, rettv
)
15697 p
= get_tv_string(&argvars
[0]);
15698 rettv
->vval
.v_string
= vim_strsave(p
);
15699 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
15700 rettv
->v_type
= VAR_STRING
;
15708 f_sin(argvars
, rettv
)
15714 rettv
->v_type
= VAR_FLOAT
;
15715 if (get_float_arg(argvars
, &f
) == OK
)
15716 rettv
->vval
.v_float
= sin(f
);
15718 rettv
->vval
.v_float
= 0.0;
15723 #ifdef __BORLANDC__
15726 item_compare
__ARGS((const void *s1
, const void *s2
));
15728 #ifdef __BORLANDC__
15731 item_compare2
__ARGS((const void *s1
, const void *s2
));
15733 static int item_compare_ic
;
15734 static char_u
*item_compare_func
;
15735 static int item_compare_func_err
;
15736 #define ITEM_COMPARE_FAIL 999
15739 * Compare functions for f_sort() below.
15742 #ifdef __BORLANDC__
15745 item_compare(s1
, s2
)
15750 char_u
*tofree1
, *tofree2
;
15752 char_u numbuf1
[NUMBUFLEN
];
15753 char_u numbuf2
[NUMBUFLEN
];
15755 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
15756 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
15761 if (item_compare_ic
)
15762 res
= STRICMP(p1
, p2
);
15764 res
= STRCMP(p1
, p2
);
15771 #ifdef __BORLANDC__
15774 item_compare2(s1
, s2
)
15783 /* shortcut after failure in previous call; compare all items equal */
15784 if (item_compare_func_err
)
15787 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15788 * in the copy without changing the original list items. */
15789 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
15790 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
15792 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
15793 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
15794 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
15795 clear_tv(&argv
[0]);
15796 clear_tv(&argv
[1]);
15799 res
= ITEM_COMPARE_FAIL
;
15801 /* return value has wrong type */
15802 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
15803 if (item_compare_func_err
)
15804 res
= ITEM_COMPARE_FAIL
;
15810 * "sort({list})" function
15813 f_sort(argvars
, rettv
)
15823 rettv
->vval
.v_number
= 0;
15824 if (argvars
[0].v_type
!= VAR_LIST
)
15825 EMSG2(_(e_listarg
), "sort()");
15828 l
= argvars
[0].vval
.v_list
;
15829 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
15831 rettv
->vval
.v_list
= l
;
15832 rettv
->v_type
= VAR_LIST
;
15837 return; /* short list sorts pretty quickly */
15839 item_compare_ic
= FALSE
;
15840 item_compare_func
= NULL
;
15841 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15843 if (argvars
[1].v_type
== VAR_FUNC
)
15844 item_compare_func
= argvars
[1].vval
.v_string
;
15849 i
= get_tv_number_chk(&argvars
[1], &error
);
15851 return; /* type error; errmsg already given */
15853 item_compare_ic
= TRUE
;
15855 item_compare_func
= get_tv_string(&argvars
[1]);
15859 /* Make an array with each entry pointing to an item in the List. */
15860 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
15864 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
15867 item_compare_func_err
= FALSE
;
15868 /* test the compare function */
15869 if (item_compare_func
!= NULL
15870 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
15871 == ITEM_COMPARE_FAIL
)
15872 EMSG(_("E702: Sort compare function failed"));
15875 /* Sort the array with item pointers. */
15876 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
15877 item_compare_func
== NULL
? item_compare
: item_compare2
);
15879 if (!item_compare_func_err
)
15881 /* Clear the List and append the items in the sorted order. */
15882 l
->lv_first
= l
->lv_last
= l
->lv_idx_item
= NULL
;
15884 for (i
= 0; i
< len
; ++i
)
15885 list_append(l
, ptrs
[i
]);
15894 * "soundfold({word})" function
15897 f_soundfold(argvars
, rettv
)
15903 rettv
->v_type
= VAR_STRING
;
15904 s
= get_tv_string(&argvars
[0]);
15906 rettv
->vval
.v_string
= eval_soundfold(s
);
15908 rettv
->vval
.v_string
= vim_strsave(s
);
15913 * "spellbadword()" function
15917 f_spellbadword(argvars
, rettv
)
15921 char_u
*word
= (char_u
*)"";
15922 hlf_T attr
= HLF_COUNT
;
15925 if (rettv_list_alloc(rettv
) == FAIL
)
15929 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15931 /* Find the start and length of the badly spelled word. */
15932 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
15934 word
= ml_get_cursor();
15936 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15938 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15943 /* Check the argument for spelling. */
15944 while (*str
!= NUL
)
15946 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
15947 if (attr
!= HLF_COUNT
)
15958 list_append_string(rettv
->vval
.v_list
, word
, len
);
15959 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
15960 attr
== HLF_SPB
? "bad" :
15961 attr
== HLF_SPR
? "rare" :
15962 attr
== HLF_SPL
? "local" :
15963 attr
== HLF_SPC
? "caps" :
15968 * "spellsuggest()" function
15972 f_spellsuggest(argvars
, rettv
)
15978 int typeerr
= FALSE
;
15983 int need_capital
= FALSE
;
15986 if (rettv_list_alloc(rettv
) == FAIL
)
15990 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15992 str
= get_tv_string(&argvars
[0]);
15993 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15995 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
15998 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16000 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
16008 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
16010 for (i
= 0; i
< ga
.ga_len
; ++i
)
16012 str
= ((char_u
**)ga
.ga_data
)[i
];
16014 li
= listitem_alloc();
16019 li
->li_tv
.v_type
= VAR_STRING
;
16020 li
->li_tv
.v_lock
= 0;
16021 li
->li_tv
.vval
.v_string
= str
;
16022 list_append(rettv
->vval
.v_list
, li
);
16031 f_split(argvars
, rettv
)
16037 char_u
*pat
= NULL
;
16038 regmatch_T regmatch
;
16039 char_u patbuf
[NUMBUFLEN
];
16043 int keepempty
= FALSE
;
16044 int typeerr
= FALSE
;
16046 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16048 p_cpo
= (char_u
*)"";
16050 str
= get_tv_string(&argvars
[0]);
16051 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16053 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16056 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16057 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
16059 if (pat
== NULL
|| *pat
== NUL
)
16060 pat
= (char_u
*)"[\\x01- ]\\+";
16062 if (rettv_list_alloc(rettv
) == FAIL
)
16067 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
16068 if (regmatch
.regprog
!= NULL
)
16070 regmatch
.rm_ic
= FALSE
;
16071 while (*str
!= NUL
|| keepempty
)
16074 match
= FALSE
; /* empty item at the end */
16076 match
= vim_regexec_nl(®match
, str
, col
);
16078 end
= regmatch
.startp
[0];
16080 end
= str
+ STRLEN(str
);
16081 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
16082 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
16084 if (list_append_string(rettv
->vval
.v_list
, str
,
16085 (int)(end
- str
)) == FAIL
)
16090 /* Advance to just after the match. */
16091 if (regmatch
.endp
[0] > str
)
16095 /* Don't get stuck at the same match. */
16097 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
16102 str
= regmatch
.endp
[0];
16105 vim_free(regmatch
.regprog
);
16113 * "sqrt()" function
16116 f_sqrt(argvars
, rettv
)
16122 rettv
->v_type
= VAR_FLOAT
;
16123 if (get_float_arg(argvars
, &f
) == OK
)
16124 rettv
->vval
.v_float
= sqrt(f
);
16126 rettv
->vval
.v_float
= 0.0;
16130 * "str2float()" function
16133 f_str2float(argvars
, rettv
)
16137 char_u
*p
= skipwhite(get_tv_string(&argvars
[0]));
16140 p
= skipwhite(p
+ 1);
16141 (void)string2float(p
, &rettv
->vval
.v_float
);
16142 rettv
->v_type
= VAR_FLOAT
;
16147 * "str2nr()" function
16150 f_str2nr(argvars
, rettv
)
16158 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16160 base
= get_tv_number(&argvars
[1]);
16161 if (base
!= 8 && base
!= 10 && base
!= 16)
16168 p
= skipwhite(get_tv_string(&argvars
[0]));
16170 p
= skipwhite(p
+ 1);
16171 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
16172 rettv
->vval
.v_number
= n
;
16175 #ifdef HAVE_STRFTIME
16177 * "strftime({format}[, {time}])" function
16180 f_strftime(argvars
, rettv
)
16184 char_u result_buf
[256];
16185 struct tm
*curtime
;
16189 rettv
->v_type
= VAR_STRING
;
16191 p
= get_tv_string(&argvars
[0]);
16192 if (argvars
[1].v_type
== VAR_UNKNOWN
)
16193 seconds
= time(NULL
);
16195 seconds
= (time_t)get_tv_number(&argvars
[1]);
16196 curtime
= localtime(&seconds
);
16197 /* MSVC returns NULL for an invalid value of seconds. */
16198 if (curtime
== NULL
)
16199 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
16206 conv
.vc_type
= CONV_NONE
;
16207 enc
= enc_locale();
16208 convert_setup(&conv
, p_enc
, enc
);
16209 if (conv
.vc_type
!= CONV_NONE
)
16210 p
= string_convert(&conv
, p
, NULL
);
16213 (void)strftime((char *)result_buf
, sizeof(result_buf
),
16214 (char *)p
, curtime
);
16216 result_buf
[0] = NUL
;
16219 if (conv
.vc_type
!= CONV_NONE
)
16221 convert_setup(&conv
, enc
, p_enc
);
16222 if (conv
.vc_type
!= CONV_NONE
)
16223 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
16226 rettv
->vval
.v_string
= vim_strsave(result_buf
);
16229 /* Release conversion descriptors */
16230 convert_setup(&conv
, NULL
, NULL
);
16238 * "stridx()" function
16241 f_stridx(argvars
, rettv
)
16245 char_u buf
[NUMBUFLEN
];
16248 char_u
*save_haystack
;
16252 needle
= get_tv_string_chk(&argvars
[1]);
16253 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16254 rettv
->vval
.v_number
= -1;
16255 if (needle
== NULL
|| haystack
== NULL
)
16256 return; /* type error; errmsg already given */
16258 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16262 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
16263 if (error
|| start_idx
>= (int)STRLEN(haystack
))
16265 if (start_idx
>= 0)
16266 haystack
+= start_idx
;
16269 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
16271 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
16275 * "string()" function
16278 f_string(argvars
, rettv
)
16283 char_u numbuf
[NUMBUFLEN
];
16285 rettv
->v_type
= VAR_STRING
;
16286 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
16287 /* Make a copy if we have a value but it's not in allocated memory. */
16288 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
16289 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
16293 * "strlen()" function
16296 f_strlen(argvars
, rettv
)
16300 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
16301 get_tv_string(&argvars
[0])));
16305 * "strpart()" function
16308 f_strpart(argvars
, rettv
)
16318 p
= get_tv_string(&argvars
[0]);
16319 slen
= (int)STRLEN(p
);
16321 n
= get_tv_number_chk(&argvars
[1], &error
);
16324 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16325 len
= get_tv_number(&argvars
[2]);
16327 len
= slen
- n
; /* default len: all bytes that are available. */
16330 * Only return the overlap between the specified part and the actual
16342 else if (n
+ len
> slen
)
16345 rettv
->v_type
= VAR_STRING
;
16346 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
16350 * "strridx()" function
16353 f_strridx(argvars
, rettv
)
16357 char_u buf
[NUMBUFLEN
];
16361 char_u
*lastmatch
= NULL
;
16362 int haystack_len
, end_idx
;
16364 needle
= get_tv_string_chk(&argvars
[1]);
16365 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16367 rettv
->vval
.v_number
= -1;
16368 if (needle
== NULL
|| haystack
== NULL
)
16369 return; /* type error; errmsg already given */
16371 haystack_len
= (int)STRLEN(haystack
);
16372 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16374 /* Third argument: upper limit for index */
16375 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
16377 return; /* can never find a match */
16380 end_idx
= haystack_len
;
16382 if (*needle
== NUL
)
16384 /* Empty string matches past the end. */
16385 lastmatch
= haystack
+ end_idx
;
16389 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
16391 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
16392 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
16398 if (lastmatch
== NULL
)
16399 rettv
->vval
.v_number
= -1;
16401 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
16405 * "strtrans()" function
16408 f_strtrans(argvars
, rettv
)
16412 rettv
->v_type
= VAR_STRING
;
16413 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
16417 * "submatch()" function
16420 f_submatch(argvars
, rettv
)
16424 rettv
->v_type
= VAR_STRING
;
16425 rettv
->vval
.v_string
=
16426 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
16430 * "substitute()" function
16433 f_substitute(argvars
, rettv
)
16437 char_u patbuf
[NUMBUFLEN
];
16438 char_u subbuf
[NUMBUFLEN
];
16439 char_u flagsbuf
[NUMBUFLEN
];
16441 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16442 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16443 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
16444 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
16446 rettv
->v_type
= VAR_STRING
;
16447 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
16448 rettv
->vval
.v_string
= NULL
;
16450 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
16454 * "synID(lnum, col, trans)" function
16458 f_synID(argvars
, rettv
)
16467 int transerr
= FALSE
;
16469 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16470 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16471 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
16473 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16474 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
16475 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
16478 rettv
->vval
.v_number
= id
;
16482 * "synIDattr(id, what [, mode])" function
16486 f_synIDattr(argvars
, rettv
)
16495 char_u modebuf
[NUMBUFLEN
];
16498 id
= get_tv_number(&argvars
[0]);
16499 what
= get_tv_string(&argvars
[1]);
16500 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16502 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
16503 modec
= TOLOWER_ASC(mode
[0]);
16504 if (modec
!= 't' && modec
!= 'c'
16509 modec
= 0; /* replace invalid with current */
16525 switch (TOLOWER_ASC(what
[0]))
16528 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
16529 p
= highlight_color(id
, what
, modec
);
16531 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
16534 case 'f': /* fg[#] */
16535 p
= highlight_color(id
, what
, modec
);
16539 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
16540 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16542 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
16545 case 'n': /* name */
16546 p
= get_highlight_name(NULL
, id
- 1);
16549 case 'r': /* reverse */
16550 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16553 case 's': /* standout */
16554 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
16558 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
16560 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
16563 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
16568 p
= vim_strsave(p
);
16570 rettv
->v_type
= VAR_STRING
;
16571 rettv
->vval
.v_string
= p
;
16575 * "synIDtrans(id)" function
16579 f_synIDtrans(argvars
, rettv
)
16586 id
= get_tv_number(&argvars
[0]);
16589 id
= syn_get_final_id(id
);
16594 rettv
->vval
.v_number
= id
;
16598 * "synstack(lnum, col)" function
16602 f_synstack(argvars
, rettv
)
16613 rettv
->v_type
= VAR_LIST
;
16614 rettv
->vval
.v_list
= NULL
;
16617 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16618 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16620 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16621 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
))
16622 && rettv_list_alloc(rettv
) != FAIL
)
16624 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
16627 id
= syn_get_stack_item(i
);
16630 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
16638 * "system()" function
16641 f_system(argvars
, rettv
)
16645 char_u
*res
= NULL
;
16647 char_u
*infile
= NULL
;
16648 char_u buf
[NUMBUFLEN
];
16652 if (check_restricted() || check_secure())
16655 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16658 * Write the string to a temp file, to be used for input of the shell
16661 if ((infile
= vim_tempname('i')) == NULL
)
16667 fd
= mch_fopen((char *)infile
, WRITEBIN
);
16670 EMSG2(_(e_notopen
), infile
);
16673 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
16677 goto done
; /* type error; errmsg already given */
16679 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
16681 if (fclose(fd
) != 0)
16685 EMSG(_("E677: Error writing temp file"));
16690 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
16691 SHELL_SILENT
| SHELL_COOKED
);
16694 /* translate <CR> into <NL> */
16699 for (s
= res
; *s
; ++s
)
16707 /* translate <CR><NL> into <NL> */
16713 for (s
= res
; *s
; ++s
)
16715 if (s
[0] == CAR
&& s
[1] == NL
)
16725 if (infile
!= NULL
)
16727 mch_remove(infile
);
16730 rettv
->v_type
= VAR_STRING
;
16731 rettv
->vval
.v_string
= res
;
16735 * "tabpagebuflist()" function
16739 f_tabpagebuflist(argvars
, rettv
)
16743 #ifndef FEAT_WINDOWS
16744 rettv
->vval
.v_number
= 0;
16749 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16753 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16755 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16758 rettv
->vval
.v_number
= 0;
16761 if (rettv_list_alloc(rettv
) == FAIL
)
16762 rettv
->vval
.v_number
= 0;
16765 for (; wp
!= NULL
; wp
= wp
->w_next
)
16766 if (list_append_number(rettv
->vval
.v_list
,
16767 wp
->w_buffer
->b_fnum
) == FAIL
)
16776 * "tabpagenr()" function
16780 f_tabpagenr(argvars
, rettv
)
16785 #ifdef FEAT_WINDOWS
16788 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
16790 arg
= get_tv_string_chk(&argvars
[0]);
16794 if (STRCMP(arg
, "$") == 0)
16795 nr
= tabpage_index(NULL
) - 1;
16797 EMSG2(_(e_invexpr2
), arg
);
16801 nr
= tabpage_index(curtab
);
16803 rettv
->vval
.v_number
= nr
;
16807 #ifdef FEAT_WINDOWS
16808 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
16811 * Common code for tabpagewinnr() and winnr().
16814 get_winnr(tp
, argvar
)
16823 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
16824 if (argvar
->v_type
!= VAR_UNKNOWN
)
16826 arg
= get_tv_string_chk(argvar
);
16828 nr
= 0; /* type error; errmsg already given */
16829 else if (STRCMP(arg
, "$") == 0)
16830 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
16831 else if (STRCMP(arg
, "#") == 0)
16833 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
16839 EMSG2(_(e_invexpr2
), arg
);
16845 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16846 wp
!= twin
; wp
= wp
->w_next
)
16850 /* didn't find it in this tabpage */
16861 * "tabpagewinnr()" function
16865 f_tabpagewinnr(argvars
, rettv
)
16870 #ifdef FEAT_WINDOWS
16873 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16877 nr
= get_winnr(tp
, &argvars
[1]);
16879 rettv
->vval
.v_number
= nr
;
16884 * "tagfiles()" function
16888 f_tagfiles(argvars
, rettv
)
16892 char_u fname
[MAXPATHL
+ 1];
16896 if (rettv_list_alloc(rettv
) == FAIL
)
16898 rettv
->vval
.v_number
= 0;
16902 for (first
= TRUE
; ; first
= FALSE
)
16903 if (get_tagfname(&tn
, first
, fname
) == FAIL
16904 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
16910 * "taglist()" function
16913 f_taglist(argvars
, rettv
)
16917 char_u
*tag_pattern
;
16919 tag_pattern
= get_tv_string(&argvars
[0]);
16921 rettv
->vval
.v_number
= FALSE
;
16922 if (*tag_pattern
== NUL
)
16925 if (rettv_list_alloc(rettv
) == OK
)
16926 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
16930 * "tempname()" function
16934 f_tempname(argvars
, rettv
)
16938 static int x
= 'A';
16940 rettv
->v_type
= VAR_STRING
;
16941 rettv
->vval
.v_string
= vim_tempname(x
);
16943 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16944 * names. Skip 'I' and 'O', they are used for shell redirection. */
16962 } while (x
== 'I' || x
== 'O');
16966 * "test(list)" function: Just checking the walls...
16970 f_test(argvars
, rettv
)
16974 /* Used for unit testing. Change the code below to your liking. */
16978 char_u
*bad
, *good
;
16980 if (argvars
[0].v_type
!= VAR_LIST
)
16982 l
= argvars
[0].vval
.v_list
;
16988 bad
= get_tv_string(&li
->li_tv
);
16992 good
= get_tv_string(&li
->li_tv
);
16993 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
16998 * "tolower(string)" function
17001 f_tolower(argvars
, rettv
)
17007 p
= vim_strsave(get_tv_string(&argvars
[0]));
17008 rettv
->v_type
= VAR_STRING
;
17009 rettv
->vval
.v_string
= p
;
17021 c
= utf_ptr2char(p
);
17022 lc
= utf_tolower(c
);
17023 l
= utf_ptr2len(p
);
17024 /* TODO: reallocate string when byte count changes. */
17025 if (utf_char2len(lc
) == l
)
17026 utf_char2bytes(lc
, p
);
17029 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
17030 p
+= l
; /* skip multi-byte character */
17034 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
17041 * "toupper(string)" function
17044 f_toupper(argvars
, rettv
)
17048 rettv
->v_type
= VAR_STRING
;
17049 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
17053 * "tr(string, fromstr, tostr)" function
17056 f_tr(argvars
, rettv
)
17073 char_u buf
[NUMBUFLEN
];
17074 char_u buf2
[NUMBUFLEN
];
17077 instr
= get_tv_string(&argvars
[0]);
17078 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
17079 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
17081 /* Default return value: empty string. */
17082 rettv
->v_type
= VAR_STRING
;
17083 rettv
->vval
.v_string
= NULL
;
17084 if (fromstr
== NULL
|| tostr
== NULL
)
17085 return; /* type error; errmsg already given */
17086 ga_init2(&ga
, (int)sizeof(char), 80);
17091 /* not multi-byte: fromstr and tostr must be the same length */
17092 if (STRLEN(fromstr
) != STRLEN(tostr
))
17097 EMSG2(_(e_invarg2
), fromstr
);
17102 /* fromstr and tostr have to contain the same number of chars */
17103 while (*instr
!= NUL
)
17108 inlen
= (*mb_ptr2len
)(instr
);
17112 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
17114 fromlen
= (*mb_ptr2len
)(p
);
17115 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
17117 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17119 tolen
= (*mb_ptr2len
)(p
);
17127 if (*p
== NUL
) /* tostr is shorter than fromstr */
17134 if (first
&& cpstr
== instr
)
17136 /* Check that fromstr and tostr have the same number of
17137 * (multi-byte) characters. Done only once when a character
17138 * of instr doesn't appear in fromstr. */
17140 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17142 tolen
= (*mb_ptr2len
)(p
);
17149 ga_grow(&ga
, cplen
);
17150 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
17151 ga
.ga_len
+= cplen
;
17158 /* When not using multi-byte chars we can do it faster. */
17159 p
= vim_strchr(fromstr
, *instr
);
17161 ga_append(&ga
, tostr
[p
- fromstr
]);
17163 ga_append(&ga
, *instr
);
17168 /* add a terminating NUL */
17170 ga_append(&ga
, NUL
);
17172 rettv
->vval
.v_string
= ga
.ga_data
;
17177 * "trunc({float})" function
17180 f_trunc(argvars
, rettv
)
17186 rettv
->v_type
= VAR_FLOAT
;
17187 if (get_float_arg(argvars
, &f
) == OK
)
17188 /* trunc() is not in C90, use floor() or ceil() instead. */
17189 rettv
->vval
.v_float
= f
> 0 ? floor(f
) : ceil(f
);
17191 rettv
->vval
.v_float
= 0.0;
17196 * "type(expr)" function
17199 f_type(argvars
, rettv
)
17205 switch (argvars
[0].v_type
)
17207 case VAR_NUMBER
: n
= 0; break;
17208 case VAR_STRING
: n
= 1; break;
17209 case VAR_FUNC
: n
= 2; break;
17210 case VAR_LIST
: n
= 3; break;
17211 case VAR_DICT
: n
= 4; break;
17213 case VAR_FLOAT
: n
= 5; break;
17215 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
17217 rettv
->vval
.v_number
= n
;
17221 * "values(dict)" function
17224 f_values(argvars
, rettv
)
17228 dict_list(argvars
, rettv
, 1);
17232 * "virtcol(string)" function
17235 f_virtcol(argvars
, rettv
)
17241 int fnum
= curbuf
->b_fnum
;
17243 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
17244 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
17245 && fnum
== curbuf
->b_fnum
)
17247 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
17251 rettv
->vval
.v_number
= vcol
;
17255 * "visualmode()" function
17259 f_visualmode(argvars
, rettv
)
17266 rettv
->v_type
= VAR_STRING
;
17267 str
[0] = curbuf
->b_visual_mode_eval
;
17269 rettv
->vval
.v_string
= vim_strsave(str
);
17271 /* A non-zero number or non-empty string argument: reset mode. */
17272 if ((argvars
[0].v_type
== VAR_NUMBER
&& argvars
[0].vval
.v_number
!= 0)
17273 || (argvars
[0].v_type
== VAR_STRING
17274 && *get_tv_string(&argvars
[0]) != NUL
))
17275 curbuf
->b_visual_mode_eval
= NUL
;
17277 rettv
->vval
.v_number
= 0; /* return anything, it won't work anyway */
17282 * "winbufnr(nr)" function
17285 f_winbufnr(argvars
, rettv
)
17291 wp
= find_win_by_nr(&argvars
[0], NULL
);
17293 rettv
->vval
.v_number
= -1;
17295 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
17299 * "wincol()" function
17303 f_wincol(argvars
, rettv
)
17308 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
17312 * "winheight(nr)" function
17315 f_winheight(argvars
, rettv
)
17321 wp
= find_win_by_nr(&argvars
[0], NULL
);
17323 rettv
->vval
.v_number
= -1;
17325 rettv
->vval
.v_number
= wp
->w_height
;
17329 * "winline()" function
17333 f_winline(argvars
, rettv
)
17338 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
17342 * "winnr()" function
17346 f_winnr(argvars
, rettv
)
17352 #ifdef FEAT_WINDOWS
17353 nr
= get_winnr(curtab
, &argvars
[0]);
17355 rettv
->vval
.v_number
= nr
;
17359 * "winrestcmd()" function
17363 f_winrestcmd(argvars
, rettv
)
17367 #ifdef FEAT_WINDOWS
17373 ga_init2(&ga
, (int)sizeof(char), 70);
17374 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
17376 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
17377 ga_concat(&ga
, buf
);
17378 # ifdef FEAT_VERTSPLIT
17379 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
17380 ga_concat(&ga
, buf
);
17384 ga_append(&ga
, NUL
);
17386 rettv
->vval
.v_string
= ga
.ga_data
;
17388 rettv
->vval
.v_string
= NULL
;
17390 rettv
->v_type
= VAR_STRING
;
17394 * "winrestview()" function
17398 f_winrestview(argvars
, rettv
)
17404 if (argvars
[0].v_type
!= VAR_DICT
17405 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
17409 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
17410 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
17411 #ifdef FEAT_VIRTUALEDIT
17412 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
17414 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
17415 curwin
->w_set_curswant
= FALSE
;
17417 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
17419 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
17421 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
17422 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
17425 changed_cline_bef_curs();
17426 invalidate_botline();
17427 redraw_later(VALID
);
17429 if (curwin
->w_topline
== 0)
17430 curwin
->w_topline
= 1;
17431 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
17432 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
17434 check_topfill(curwin
, TRUE
);
17440 * "winsaveview()" function
17444 f_winsaveview(argvars
, rettv
)
17450 dict
= dict_alloc();
17453 rettv
->v_type
= VAR_DICT
;
17454 rettv
->vval
.v_dict
= dict
;
17455 ++dict
->dv_refcount
;
17457 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
17458 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
17459 #ifdef FEAT_VIRTUALEDIT
17460 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
17463 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
17465 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
17467 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
17469 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
17470 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
17474 * "winwidth(nr)" function
17477 f_winwidth(argvars
, rettv
)
17483 wp
= find_win_by_nr(&argvars
[0], NULL
);
17485 rettv
->vval
.v_number
= -1;
17487 #ifdef FEAT_VERTSPLIT
17488 rettv
->vval
.v_number
= wp
->w_width
;
17490 rettv
->vval
.v_number
= Columns
;
17495 * "writefile()" function
17498 f_writefile(argvars
, rettv
)
17502 int binary
= FALSE
;
17510 if (check_restricted() || check_secure())
17513 if (argvars
[0].v_type
!= VAR_LIST
)
17515 EMSG2(_(e_listarg
), "writefile()");
17518 if (argvars
[0].vval
.v_list
== NULL
)
17521 if (argvars
[2].v_type
!= VAR_UNKNOWN
17522 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
17525 /* Always open the file in binary mode, library functions have a mind of
17526 * their own about CR-LF conversion. */
17527 fname
= get_tv_string(&argvars
[1]);
17528 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
17530 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
17535 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
17538 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
17550 if (!binary
|| li
->li_next
!= NULL
)
17551 if (putc('\n', fd
) == EOF
)
17565 rettv
->vval
.v_number
= ret
;
17569 * Translate a String variable into a position.
17570 * Returns NULL when there is an error.
17573 var2fpos(varp
, dollar_lnum
, fnum
)
17575 int dollar_lnum
; /* TRUE when $ is last line */
17576 int *fnum
; /* set to fnum for '0, 'A, etc. */
17582 /* Argument can be [lnum, col, coladd]. */
17583 if (varp
->v_type
== VAR_LIST
)
17590 l
= varp
->vval
.v_list
;
17594 /* Get the line number */
17595 pos
.lnum
= list_find_nr(l
, 0L, &error
);
17596 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
17597 return NULL
; /* invalid line number */
17599 /* Get the column number */
17600 pos
.col
= list_find_nr(l
, 1L, &error
);
17603 len
= (long)STRLEN(ml_get(pos
.lnum
));
17605 /* We accept "$" for the column number: last column. */
17606 li
= list_find(l
, 1L);
17607 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
17608 && li
->li_tv
.vval
.v_string
!= NULL
17609 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
17612 /* Accept a position up to the NUL after the line. */
17613 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
17614 return NULL
; /* invalid column number */
17617 #ifdef FEAT_VIRTUALEDIT
17618 /* Get the virtual offset. Defaults to zero. */
17619 pos
.coladd
= list_find_nr(l
, 2L, &error
);
17627 name
= get_tv_string_chk(varp
);
17630 if (name
[0] == '.') /* cursor */
17631 return &curwin
->w_cursor
;
17633 if (name
[0] == 'v' && name
[1] == NUL
) /* Visual start */
17637 return &curwin
->w_cursor
;
17640 if (name
[0] == '\'') /* mark */
17642 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
17643 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
17648 #ifdef FEAT_VIRTUALEDIT
17652 if (name
[0] == 'w' && dollar_lnum
)
17655 if (name
[1] == '0') /* "w0": first visible line */
17658 pos
.lnum
= curwin
->w_topline
;
17661 else if (name
[1] == '$') /* "w$": last visible line */
17663 validate_botline();
17664 pos
.lnum
= curwin
->w_botline
- 1;
17668 else if (name
[0] == '$') /* last column or line */
17672 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
17677 pos
.lnum
= curwin
->w_cursor
.lnum
;
17678 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
17686 * Convert list in "arg" into a position and optional file number.
17687 * When "fnump" is NULL there is no file number, only 3 items.
17688 * Note that the column is passed on as-is, the caller may want to decrement
17689 * it to use 1 for the first column.
17690 * Return FAIL when conversion is not possible, doesn't check the position for
17694 list2fpos(arg
, posp
, fnump
)
17699 list_T
*l
= arg
->vval
.v_list
;
17703 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17704 * when "fnump" isn't NULL and "coladd" is optional. */
17705 if (arg
->v_type
!= VAR_LIST
17707 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
17708 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
17713 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
17717 n
= curbuf
->b_fnum
; /* current buffer */
17721 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
17726 n
= list_find_nr(l
, i
++, NULL
); /* col */
17731 #ifdef FEAT_VIRTUALEDIT
17732 n
= list_find_nr(l
, i
, NULL
);
17743 * Get the length of an environment variable name.
17744 * Advance "arg" to the first character after the name.
17745 * Return 0 for error.
17754 for (p
= *arg
; vim_isIDc(*p
); ++p
)
17756 if (p
== *arg
) /* no name found */
17759 len
= (int)(p
- *arg
);
17765 * Get the length of the name of a function or internal variable.
17766 * "arg" is advanced to the first non-white character after the name.
17767 * Return 0 if something is wrong.
17776 /* Find the end of the name. */
17777 for (p
= *arg
; eval_isnamec(*p
); ++p
)
17779 if (p
== *arg
) /* no name found */
17782 len
= (int)(p
- *arg
);
17783 *arg
= skipwhite(p
);
17789 * Get the length of the name of a variable or function.
17790 * Only the name is recognized, does not handle ".key" or "[idx]".
17791 * "arg" is advanced to the first non-white character after the name.
17792 * Return -1 if curly braces expansion failed.
17793 * Return 0 if something else is wrong.
17794 * If the name contains 'magic' {}'s, expand them and return the
17795 * expanded name in an allocated string via 'alias' - caller must free.
17798 get_name_len(arg
, alias
, evaluate
, verbose
)
17806 char_u
*expr_start
;
17809 *alias
= NULL
; /* default to no alias */
17811 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
17812 && (*arg
)[2] == (int)KE_SNR
)
17814 /* hard coded <SNR>, already translated */
17816 return get_id_len(arg
) + 3;
17818 len
= eval_fname_script(*arg
);
17821 /* literal "<SID>", "s:" or "<SNR>" */
17826 * Find the end of the name; check for {} construction.
17828 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
17829 len
> 0 ? 0 : FNE_CHECK_START
);
17830 if (expr_start
!= NULL
)
17832 char_u
*temp_string
;
17836 len
+= (int)(p
- *arg
);
17837 *arg
= skipwhite(p
);
17842 * Include any <SID> etc in the expanded string:
17843 * Thus the -len here.
17845 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
17846 if (temp_string
== NULL
)
17848 *alias
= temp_string
;
17849 *arg
= skipwhite(p
);
17850 return (int)STRLEN(temp_string
);
17853 len
+= get_id_len(arg
);
17854 if (len
== 0 && verbose
)
17855 EMSG2(_(e_invexpr2
), *arg
);
17861 * Find the end of a variable or function name, taking care of magic braces.
17862 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17863 * start and end of the first magic braces item.
17864 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
17865 * Return a pointer to just after the name. Equal to "arg" if there is no
17869 find_name_end(arg
, expr_start
, expr_end
, flags
)
17871 char_u
**expr_start
;
17879 if (expr_start
!= NULL
)
17881 *expr_start
= NULL
;
17885 /* Quick check for valid starting character. */
17886 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
17889 for (p
= arg
; *p
!= NUL
17890 && (eval_isnamec(*p
)
17892 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
17894 || br_nest
!= 0); mb_ptr_adv(p
))
17898 /* skip over 'string' to avoid counting [ and ] inside it. */
17899 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
17904 else if (*p
== '"')
17906 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17907 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
17908 if (*p
== '\\' && p
[1] != NUL
)
17918 else if (*p
== ']')
17927 if (expr_start
!= NULL
&& *expr_start
== NULL
)
17930 else if (*p
== '}')
17933 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
17943 * Expands out the 'magic' {}'s in a variable/function name.
17944 * Note that this can call itself recursively, to deal with
17945 * constructs like foo{bar}{baz}{bam}
17946 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17952 * Returns a new allocated string, which the caller must free.
17953 * Returns NULL for failure.
17956 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
17958 char_u
*expr_start
;
17963 char_u
*retval
= NULL
;
17964 char_u
*temp_result
;
17965 char_u
*nextcmd
= NULL
;
17967 if (expr_end
== NULL
|| in_end
== NULL
)
17974 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
17975 if (temp_result
!= NULL
&& nextcmd
== NULL
)
17977 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
17978 + (in_end
- expr_end
) + 1));
17979 if (retval
!= NULL
)
17981 STRCPY(retval
, in_start
);
17982 STRCAT(retval
, temp_result
);
17983 STRCAT(retval
, expr_end
+ 1);
17986 vim_free(temp_result
);
17988 *in_end
= c1
; /* put char back for error messages */
17992 if (retval
!= NULL
)
17994 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
17995 if (expr_start
!= NULL
)
17997 /* Further expansion! */
17998 temp_result
= make_expanded_name(retval
, expr_start
,
17999 expr_end
, temp_result
);
18001 retval
= temp_result
;
18009 * Return TRUE if character "c" can be used in a variable or function name.
18010 * Does not include '{' or '}' for magic braces.
18016 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
18020 * Return TRUE if character "c" can be used as the first character in a
18021 * variable or function name (excluding '{' and '}').
18027 return (ASCII_ISALPHA(c
) || c
== '_');
18031 * Set number v: variable to "val".
18034 set_vim_var_nr(idx
, val
)
18038 vimvars
[idx
].vv_nr
= val
;
18042 * Get number v: variable value.
18045 get_vim_var_nr(idx
)
18048 return vimvars
[idx
].vv_nr
;
18051 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18053 * Get string v: variable value. Uses a static buffer, can only be used once.
18056 get_vim_var_str(idx
)
18059 return get_tv_string(&vimvars
[idx
].vv_tv
);
18064 * Set v:count, v:count1 and v:prevcount.
18067 set_vcount(count
, count1
)
18071 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
18072 vimvars
[VV_COUNT
].vv_nr
= count
;
18073 vimvars
[VV_COUNT1
].vv_nr
= count1
;
18077 * Set string v: variable to a copy of "val".
18080 set_vim_var_string(idx
, val
, len
)
18083 int len
; /* length of "val" to use or -1 (whole string) */
18085 /* Need to do this (at least) once, since we can't initialize a union.
18086 * Will always be invoked when "v:progname" is set. */
18087 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
18089 vim_free(vimvars
[idx
].vv_str
);
18091 vimvars
[idx
].vv_str
= NULL
;
18092 else if (len
== -1)
18093 vimvars
[idx
].vv_str
= vim_strsave(val
);
18095 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
18099 * Set v:register if needed.
18107 if (c
== 0 || c
== ' ')
18111 /* Avoid free/alloc when the value is already right. */
18112 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
18113 set_vim_var_string(VV_REG
, ®name
, 1);
18117 * Get or set v:exception. If "oldval" == NULL, return the current value.
18118 * Otherwise, restore the value to "oldval" and return NULL.
18119 * Must always be called in pairs to save and restore v:exception! Does not
18120 * take care of memory allocations.
18123 v_exception(oldval
)
18126 if (oldval
== NULL
)
18127 return vimvars
[VV_EXCEPTION
].vv_str
;
18129 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
18134 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18135 * Otherwise, restore the value to "oldval" and return NULL.
18136 * Must always be called in pairs to save and restore v:throwpoint! Does not
18137 * take care of memory allocations.
18140 v_throwpoint(oldval
)
18143 if (oldval
== NULL
)
18144 return vimvars
[VV_THROWPOINT
].vv_str
;
18146 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
18150 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18153 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18154 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18155 * Must always be called in pairs!
18158 set_cmdarg(eap
, oldarg
)
18166 oldval
= vimvars
[VV_CMDARG
].vv_str
;
18170 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
18174 if (eap
->force_bin
== FORCE_BIN
)
18176 else if (eap
->force_bin
== FORCE_NOBIN
)
18181 if (eap
->read_edit
)
18184 if (eap
->force_ff
!= 0)
18185 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
18187 if (eap
->force_enc
!= 0)
18188 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
18189 if (eap
->bad_char
!= 0)
18190 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
18193 newval
= alloc(len
+ 1);
18194 if (newval
== NULL
)
18197 if (eap
->force_bin
== FORCE_BIN
)
18198 sprintf((char *)newval
, " ++bin");
18199 else if (eap
->force_bin
== FORCE_NOBIN
)
18200 sprintf((char *)newval
, " ++nobin");
18204 if (eap
->read_edit
)
18205 STRCAT(newval
, " ++edit");
18207 if (eap
->force_ff
!= 0)
18208 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
18209 eap
->cmd
+ eap
->force_ff
);
18211 if (eap
->force_enc
!= 0)
18212 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
18213 eap
->cmd
+ eap
->force_enc
);
18214 if (eap
->bad_char
!= 0)
18215 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
18216 eap
->cmd
+ eap
->bad_char
);
18218 vimvars
[VV_CMDARG
].vv_str
= newval
;
18224 * Get the value of internal variable "name".
18225 * Return OK or FAIL.
18228 get_var_tv(name
, len
, rettv
, verbose
)
18230 int len
; /* length of "name" */
18231 typval_T
*rettv
; /* NULL when only checking existence */
18232 int verbose
; /* may give error message */
18235 typval_T
*tv
= NULL
;
18240 /* truncate the name, so that we can use strcmp() */
18245 * Check for "b:changedtick".
18247 if (STRCMP(name
, "b:changedtick") == 0)
18249 atv
.v_type
= VAR_NUMBER
;
18250 atv
.vval
.v_number
= curbuf
->b_changedtick
;
18255 * Check for user-defined variables.
18259 v
= find_var(name
, NULL
);
18266 if (rettv
!= NULL
&& verbose
)
18267 EMSG2(_(e_undefvar
), name
);
18270 else if (rettv
!= NULL
)
18271 copy_tv(tv
, rettv
);
18279 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18280 * Also handle function call with Funcref variable: func(expr)
18281 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18284 handle_subscript(arg
, rettv
, evaluate
, verbose
)
18287 int evaluate
; /* do more than finding the end */
18288 int verbose
; /* give error messages */
18291 dict_T
*selfdict
= NULL
;
18298 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
18299 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
18300 && !vim_iswhite(*(*arg
- 1)))
18304 /* need to copy the funcref so that we can clear rettv */
18306 rettv
->v_type
= VAR_UNKNOWN
;
18308 /* Invoke the function. Recursive! */
18309 s
= functv
.vval
.v_string
;
18310 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
18311 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
18312 &len
, evaluate
, selfdict
);
18314 /* Clear the funcref afterwards, so that deleting it while
18315 * evaluating the arguments is possible (see test55). */
18318 /* Stop the expression evaluation when immediately aborting on
18319 * error, or when an interrupt occurred or an exception was thrown
18320 * but not caught. */
18327 dict_unref(selfdict
);
18330 else /* **arg == '[' || **arg == '.' */
18332 dict_unref(selfdict
);
18333 if (rettv
->v_type
== VAR_DICT
)
18335 selfdict
= rettv
->vval
.v_dict
;
18336 if (selfdict
!= NULL
)
18337 ++selfdict
->dv_refcount
;
18341 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
18348 dict_unref(selfdict
);
18353 * Allocate memory for a variable type-value, and make it empty (0 or NULL
18359 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
18363 * Allocate memory for a variable type-value, and assign a string to it.
18364 * The string "s" must have been allocated, it is consumed.
18365 * Return NULL for out of memory, the variable otherwise.
18373 rettv
= alloc_tv();
18376 rettv
->v_type
= VAR_STRING
;
18377 rettv
->vval
.v_string
= s
;
18385 * Free the memory for a variable type-value.
18393 switch (varp
->v_type
)
18396 func_unref(varp
->vval
.v_string
);
18399 vim_free(varp
->vval
.v_string
);
18402 list_unref(varp
->vval
.v_list
);
18405 dict_unref(varp
->vval
.v_dict
);
18414 EMSG2(_(e_intern2
), "free_tv()");
18422 * Free the memory for a variable value and set the value to NULL or 0.
18430 switch (varp
->v_type
)
18433 func_unref(varp
->vval
.v_string
);
18436 vim_free(varp
->vval
.v_string
);
18437 varp
->vval
.v_string
= NULL
;
18440 list_unref(varp
->vval
.v_list
);
18441 varp
->vval
.v_list
= NULL
;
18444 dict_unref(varp
->vval
.v_dict
);
18445 varp
->vval
.v_dict
= NULL
;
18448 varp
->vval
.v_number
= 0;
18452 varp
->vval
.v_float
= 0.0;
18458 EMSG2(_(e_intern2
), "clear_tv()");
18465 * Set the value of a variable to NULL without freeing items.
18472 vim_memset(varp
, 0, sizeof(typval_T
));
18476 * Get the number value of a variable.
18477 * If it is a String variable, uses vim_str2nr().
18478 * For incompatible types, return 0.
18479 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18480 * caller of incompatible types: it sets *denote to TRUE if "denote"
18481 * is not NULL or returns -1 otherwise.
18484 get_tv_number(varp
)
18489 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
18493 get_tv_number_chk(varp
, denote
)
18499 switch (varp
->v_type
)
18502 return (long)(varp
->vval
.v_number
);
18505 EMSG(_("E805: Using a Float as a Number"));
18509 EMSG(_("E703: Using a Funcref as a Number"));
18512 if (varp
->vval
.v_string
!= NULL
)
18513 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
18514 TRUE
, TRUE
, &n
, NULL
);
18517 EMSG(_("E745: Using a List as a Number"));
18520 EMSG(_("E728: Using a Dictionary as a Number"));
18523 EMSG2(_(e_intern2
), "get_tv_number()");
18526 if (denote
== NULL
) /* useful for values that must be unsigned */
18534 * Get the lnum from the first argument.
18535 * Also accepts ".", "$", etc., but that only works for the current buffer.
18536 * Returns -1 on error.
18539 get_tv_lnum(argvars
)
18545 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
18546 if (lnum
== 0) /* no valid number, try using line() */
18548 rettv
.v_type
= VAR_NUMBER
;
18549 f_line(argvars
, &rettv
);
18550 lnum
= rettv
.vval
.v_number
;
18557 * Get the lnum from the first argument.
18558 * Also accepts "$", then "buf" is used.
18559 * Returns 0 on error.
18562 get_tv_lnum_buf(argvars
, buf
)
18566 if (argvars
[0].v_type
== VAR_STRING
18567 && argvars
[0].vval
.v_string
!= NULL
18568 && argvars
[0].vval
.v_string
[0] == '$'
18570 return buf
->b_ml
.ml_line_count
;
18571 return get_tv_number_chk(&argvars
[0], NULL
);
18575 * Get the string value of a variable.
18576 * If it is a Number variable, the number is converted into a string.
18577 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18578 * get_tv_string_buf() uses a given buffer.
18579 * If the String variable has never been set, return an empty string.
18580 * Never returns NULL;
18581 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18585 get_tv_string(varp
)
18588 static char_u mybuf
[NUMBUFLEN
];
18590 return get_tv_string_buf(varp
, mybuf
);
18594 get_tv_string_buf(varp
, buf
)
18598 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
18600 return res
!= NULL
? res
: (char_u
*)"";
18604 get_tv_string_chk(varp
)
18607 static char_u mybuf
[NUMBUFLEN
];
18609 return get_tv_string_buf_chk(varp
, mybuf
);
18613 get_tv_string_buf_chk(varp
, buf
)
18617 switch (varp
->v_type
)
18620 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
18623 EMSG(_("E729: using Funcref as a String"));
18626 EMSG(_("E730: using List as a String"));
18629 EMSG(_("E731: using Dictionary as a String"));
18633 EMSG(_("E806: using Float as a String"));
18637 if (varp
->vval
.v_string
!= NULL
)
18638 return varp
->vval
.v_string
;
18639 return (char_u
*)"";
18641 EMSG2(_(e_intern2
), "get_tv_string_buf()");
18648 * Find variable "name" in the list of variables.
18649 * Return a pointer to it if found, NULL if not found.
18650 * Careful: "a:0" variables don't have a name.
18651 * When "htp" is not NULL we are writing to the variable, set "htp" to the
18654 static dictitem_T
*
18655 find_var(name
, htp
)
18662 ht
= find_var_ht(name
, &varname
);
18667 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
18671 * Find variable "varname" in hashtab "ht".
18672 * Returns NULL if not found.
18674 static dictitem_T
*
18675 find_var_in_ht(ht
, varname
, writing
)
18682 if (*varname
== NUL
)
18684 /* Must be something like "s:", otherwise "ht" would be NULL. */
18685 switch (varname
[-2])
18687 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
18688 case 'g': return &globvars_var
;
18689 case 'v': return &vimvars_var
;
18690 case 'b': return &curbuf
->b_bufvar
;
18691 case 'w': return &curwin
->w_winvar
;
18692 #ifdef FEAT_WINDOWS
18693 case 't': return &curtab
->tp_winvar
;
18695 case 'l': return current_funccal
== NULL
18696 ? NULL
: ¤t_funccal
->l_vars_var
;
18697 case 'a': return current_funccal
== NULL
18698 ? NULL
: ¤t_funccal
->l_avars_var
;
18703 hi
= hash_find(ht
, varname
);
18704 if (HASHITEM_EMPTY(hi
))
18706 /* For global variables we may try auto-loading the script. If it
18707 * worked find the variable again. Don't auto-load a script if it was
18708 * loaded already, otherwise it would be loaded every time when
18709 * checking if a function name is a Funcref variable. */
18710 if (ht
== &globvarht
&& !writing
18711 && script_autoload(varname
, FALSE
) && !aborting())
18712 hi
= hash_find(ht
, varname
);
18713 if (HASHITEM_EMPTY(hi
))
18720 * Find the hashtab used for a variable name.
18721 * Set "varname" to the start of name without ':'.
18724 find_var_ht(name
, varname
)
18730 if (name
[1] != ':')
18732 /* The name must not start with a colon or #. */
18733 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
18737 /* "version" is "v:version" in all scopes */
18738 hi
= hash_find(&compat_hashtab
, name
);
18739 if (!HASHITEM_EMPTY(hi
))
18740 return &compat_hashtab
;
18742 if (current_funccal
== NULL
)
18743 return &globvarht
; /* global variable */
18744 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
18746 *varname
= name
+ 2;
18747 if (*name
== 'g') /* global variable */
18749 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18751 if (vim_strchr(name
+ 2, ':') != NULL
18752 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
18754 if (*name
== 'b') /* buffer variable */
18755 return &curbuf
->b_vars
.dv_hashtab
;
18756 if (*name
== 'w') /* window variable */
18757 return &curwin
->w_vars
.dv_hashtab
;
18758 #ifdef FEAT_WINDOWS
18759 if (*name
== 't') /* tab page variable */
18760 return &curtab
->tp_vars
.dv_hashtab
;
18762 if (*name
== 'v') /* v: variable */
18764 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
18765 return ¤t_funccal
->l_avars
.dv_hashtab
;
18766 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
18767 return ¤t_funccal
->l_vars
.dv_hashtab
;
18768 if (*name
== 's' /* script variable */
18769 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
18770 return &SCRIPT_VARS(current_SID
);
18775 * Get the string value of a (global/local) variable.
18776 * Returns NULL when it doesn't exist.
18779 get_var_value(name
)
18784 v
= find_var(name
, NULL
);
18787 return get_tv_string(&v
->di_tv
);
18791 * Allocate a new hashtab for a sourced script. It will be used while
18792 * sourcing this script and when executing functions defined in the script.
18795 new_script_vars(id
)
18802 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
18804 /* Re-allocating ga_data means that an ht_array pointing to
18805 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
18806 * at its init value. Also reset "v_dict", it's always the same. */
18807 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
18809 ht
= &SCRIPT_VARS(i
);
18810 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
18811 ht
->ht_array
= ht
->ht_smallarray
;
18812 sv
= &SCRIPT_SV(i
);
18813 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
18816 while (ga_scripts
.ga_len
< id
)
18818 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
18819 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
18820 ++ga_scripts
.ga_len
;
18826 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18830 init_var_dict(dict
, dict_var
)
18832 dictitem_T
*dict_var
;
18834 hash_init(&dict
->dv_hashtab
);
18835 dict
->dv_refcount
= 99999;
18836 dict_var
->di_tv
.vval
.v_dict
= dict
;
18837 dict_var
->di_tv
.v_type
= VAR_DICT
;
18838 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
18839 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
18840 dict_var
->di_key
[0] = NUL
;
18844 * Clean up a list of internal variables.
18845 * Frees all allocated variables and the value they contain.
18846 * Clears hashtab "ht", does not free it.
18852 vars_clear_ext(ht
, TRUE
);
18856 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18859 vars_clear_ext(ht
, free_val
)
18868 todo
= (int)ht
->ht_used
;
18869 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
18871 if (!HASHITEM_EMPTY(hi
))
18875 /* Free the variable. Don't remove it from the hashtab,
18876 * ht_array might change then. hash_clear() takes care of it
18880 clear_tv(&v
->di_tv
);
18881 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
18890 * Delete a variable from hashtab "ht" at item "hi".
18891 * Clear the variable value and free the dictitem.
18898 dictitem_T
*di
= HI2DI(hi
);
18900 hash_remove(ht
, hi
);
18901 clear_tv(&di
->di_tv
);
18906 * List the value of one internal variable.
18909 list_one_var(v
, prefix
, first
)
18916 char_u numbuf
[NUMBUFLEN
];
18918 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
18919 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
18920 s
== NULL
? (char_u
*)"" : s
, first
);
18925 list_one_var_a(prefix
, name
, type
, string
, first
)
18930 int *first
; /* when TRUE clear rest of screen and set to FALSE */
18932 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18935 if (name
!= NULL
) /* "a:" vars don't have a name stored */
18939 if (type
== VAR_NUMBER
)
18941 else if (type
== VAR_FUNC
)
18943 else if (type
== VAR_LIST
)
18946 if (*string
== '[')
18949 else if (type
== VAR_DICT
)
18952 if (*string
== '{')
18958 msg_outtrans(string
);
18960 if (type
== VAR_FUNC
)
18961 msg_puts((char_u
*)"()");
18970 * Set variable "name" to value in "tv".
18971 * If the variable already exists, the value is updated.
18972 * Otherwise the variable is created.
18975 set_var(name
, tv
, copy
)
18978 int copy
; /* make copy of value in "tv" */
18985 if (tv
->v_type
== VAR_FUNC
)
18987 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
18988 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
18989 ? name
[2] : name
[0]))
18991 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
18994 if (function_exists(name
))
18996 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19002 ht
= find_var_ht(name
, &varname
);
19003 if (ht
== NULL
|| *varname
== NUL
)
19005 EMSG2(_(e_illvar
), name
);
19009 v
= find_var_in_ht(ht
, varname
, TRUE
);
19012 /* existing variable, need to clear the value */
19013 if (var_check_ro(v
->di_flags
, name
)
19014 || tv_check_lock(v
->di_tv
.v_lock
, name
))
19016 if (v
->di_tv
.v_type
!= tv
->v_type
19017 && !((v
->di_tv
.v_type
== VAR_STRING
19018 || v
->di_tv
.v_type
== VAR_NUMBER
)
19019 && (tv
->v_type
== VAR_STRING
19020 || tv
->v_type
== VAR_NUMBER
))
19022 && !((v
->di_tv
.v_type
== VAR_NUMBER
19023 || v
->di_tv
.v_type
== VAR_FLOAT
)
19024 && (tv
->v_type
== VAR_NUMBER
19025 || tv
->v_type
== VAR_FLOAT
))
19029 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
19034 * Handle setting internal v: variables separately: we don't change
19037 if (ht
== &vimvarht
)
19039 if (v
->di_tv
.v_type
== VAR_STRING
)
19041 vim_free(v
->di_tv
.vval
.v_string
);
19042 if (copy
|| tv
->v_type
!= VAR_STRING
)
19043 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
19046 /* Take over the string to avoid an extra alloc/free. */
19047 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
19048 tv
->vval
.v_string
= NULL
;
19051 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
19052 EMSG2(_(e_intern2
), "set_var()");
19055 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
19056 if (STRCMP(varname
, "searchforward") == 0)
19057 set_search_direction(v
->di_tv
.vval
.v_number
? '/' : '?');
19062 clear_tv(&v
->di_tv
);
19064 else /* add a new variable */
19066 /* Can't add "v:" variable. */
19067 if (ht
== &vimvarht
)
19069 EMSG2(_(e_illvar
), name
);
19073 /* Make sure the variable name is valid. */
19074 for (p
= varname
; *p
!= NUL
; ++p
)
19075 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
19076 && *p
!= AUTOLOAD_CHAR
)
19078 EMSG2(_(e_illvar
), varname
);
19082 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19083 + STRLEN(varname
)));
19086 STRCPY(v
->di_key
, varname
);
19087 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
19095 if (copy
|| tv
->v_type
== VAR_NUMBER
|| tv
->v_type
== VAR_FLOAT
)
19096 copy_tv(tv
, &v
->di_tv
);
19100 v
->di_tv
.v_lock
= 0;
19106 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
19107 * Also give an error message.
19110 var_check_ro(flags
, name
)
19114 if (flags
& DI_FLAGS_RO
)
19116 EMSG2(_(e_readonlyvar
), name
);
19119 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
19121 EMSG2(_(e_readonlysbx
), name
);
19128 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19129 * Also give an error message.
19132 var_check_fixed(flags
, name
)
19136 if (flags
& DI_FLAGS_FIX
)
19138 EMSG2(_("E795: Cannot delete variable %s"), name
);
19145 * Return TRUE if typeval "tv" is set to be locked (immutable).
19146 * Also give an error message, using "name".
19149 tv_check_lock(lock
, name
)
19153 if (lock
& VAR_LOCKED
)
19155 EMSG2(_("E741: Value is locked: %s"),
19156 name
== NULL
? (char_u
*)_("Unknown") : name
);
19159 if (lock
& VAR_FIXED
)
19161 EMSG2(_("E742: Cannot change value of %s"),
19162 name
== NULL
? (char_u
*)_("Unknown") : name
);
19169 * Copy the values from typval_T "from" to typval_T "to".
19170 * When needed allocates string or increases reference count.
19171 * Does not make a copy of a list or dict but copies the reference!
19178 to
->v_type
= from
->v_type
;
19180 switch (from
->v_type
)
19183 to
->vval
.v_number
= from
->vval
.v_number
;
19187 to
->vval
.v_float
= from
->vval
.v_float
;
19192 if (from
->vval
.v_string
== NULL
)
19193 to
->vval
.v_string
= NULL
;
19196 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
19197 if (from
->v_type
== VAR_FUNC
)
19198 func_ref(to
->vval
.v_string
);
19202 if (from
->vval
.v_list
== NULL
)
19203 to
->vval
.v_list
= NULL
;
19206 to
->vval
.v_list
= from
->vval
.v_list
;
19207 ++to
->vval
.v_list
->lv_refcount
;
19211 if (from
->vval
.v_dict
== NULL
)
19212 to
->vval
.v_dict
= NULL
;
19215 to
->vval
.v_dict
= from
->vval
.v_dict
;
19216 ++to
->vval
.v_dict
->dv_refcount
;
19220 EMSG2(_(e_intern2
), "copy_tv()");
19226 * Make a copy of an item.
19227 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
19228 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19229 * reference to an already copied list/dict can be used.
19230 * Returns FAIL or OK.
19233 item_copy(from
, to
, deep
, copyID
)
19239 static int recurse
= 0;
19242 if (recurse
>= DICT_MAXNEST
)
19244 EMSG(_("E698: variable nested too deep for making a copy"));
19249 switch (from
->v_type
)
19260 to
->v_type
= VAR_LIST
;
19262 if (from
->vval
.v_list
== NULL
)
19263 to
->vval
.v_list
= NULL
;
19264 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
19266 /* use the copy made earlier */
19267 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
19268 ++to
->vval
.v_list
->lv_refcount
;
19271 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
19272 if (to
->vval
.v_list
== NULL
)
19276 to
->v_type
= VAR_DICT
;
19278 if (from
->vval
.v_dict
== NULL
)
19279 to
->vval
.v_dict
= NULL
;
19280 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
19282 /* use the copy made earlier */
19283 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
19284 ++to
->vval
.v_dict
->dv_refcount
;
19287 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
19288 if (to
->vval
.v_dict
== NULL
)
19292 EMSG2(_(e_intern2
), "item_copy()");
19300 * ":echo expr1 ..." print each argument separated with a space, add a
19301 * newline at the end.
19302 * ":echon expr1 ..." print each argument plain.
19308 char_u
*arg
= eap
->arg
;
19312 int needclr
= TRUE
;
19313 int atstart
= TRUE
;
19314 char_u numbuf
[NUMBUFLEN
];
19318 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
19320 /* If eval1() causes an error message the text from the command may
19321 * still need to be cleared. E.g., "echo 22,44". */
19322 need_clr_eos
= needclr
;
19325 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19328 * Report the invalid expression unless the expression evaluation
19329 * has been cancelled due to an aborting error, an interrupt, or an
19333 EMSG2(_(e_invexpr2
), p
);
19334 need_clr_eos
= FALSE
;
19337 need_clr_eos
= FALSE
;
19344 /* Call msg_start() after eval1(), evaluating the expression
19345 * may cause a message to appear. */
19346 if (eap
->cmdidx
== CMD_echo
)
19349 else if (eap
->cmdidx
== CMD_echo
)
19350 msg_puts_attr((char_u
*)" ", echo_attr
);
19351 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
19353 for ( ; *p
!= NUL
&& !got_int
; ++p
)
19355 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
19357 if (*p
!= TAB
&& needclr
)
19359 /* remove any text still there from the command */
19363 msg_putchar_attr(*p
, echo_attr
);
19370 int i
= (*mb_ptr2len
)(p
);
19372 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
19377 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
19383 arg
= skipwhite(arg
);
19385 eap
->nextcmd
= check_nextcmd(arg
);
19391 /* remove text that may still be there from the command */
19394 if (eap
->cmdidx
== CMD_echo
)
19400 * ":echohl {name}".
19408 id
= syn_name2id(eap
->arg
);
19412 echo_attr
= syn_id2attr(id
);
19416 * ":execute expr1 ..." execute the result of an expression.
19417 * ":echomsg expr1 ..." Print a message
19418 * ":echoerr expr1 ..." Print an error
19419 * Each gets spaces around each argument and a newline at the end for
19426 char_u
*arg
= eap
->arg
;
19434 ga_init2(&ga
, 1, 80);
19438 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
19441 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19444 * Report the invalid expression unless the expression evaluation
19445 * has been cancelled due to an aborting error, an interrupt, or an
19449 EMSG2(_(e_invexpr2
), p
);
19456 p
= get_tv_string(&rettv
);
19457 len
= (int)STRLEN(p
);
19458 if (ga_grow(&ga
, len
+ 2) == FAIL
)
19465 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
19466 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
19471 arg
= skipwhite(arg
);
19474 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
19476 if (eap
->cmdidx
== CMD_echomsg
)
19478 MSG_ATTR(ga
.ga_data
, echo_attr
);
19481 else if (eap
->cmdidx
== CMD_echoerr
)
19483 /* We don't want to abort following commands, restore did_emsg. */
19484 save_did_emsg
= did_emsg
;
19485 EMSG((char_u
*)ga
.ga_data
);
19487 did_emsg
= save_did_emsg
;
19489 else if (eap
->cmdidx
== CMD_execute
)
19490 do_cmdline((char_u
*)ga
.ga_data
,
19491 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
19499 eap
->nextcmd
= check_nextcmd(arg
);
19503 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19504 * "arg" points to the "&" or '+' when called, to "option" when returning.
19505 * Returns NULL when no option name found. Otherwise pointer to the char
19506 * after the option name.
19509 find_option_end(arg
, opt_flags
)
19516 if (*p
== 'g' && p
[1] == ':')
19518 *opt_flags
= OPT_GLOBAL
;
19521 else if (*p
== 'l' && p
[1] == ':')
19523 *opt_flags
= OPT_LOCAL
;
19529 if (!ASCII_ISALPHA(*p
))
19533 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
19534 p
+= 4; /* termcap option */
19536 while (ASCII_ISALPHA(*p
))
19551 int saved_did_emsg
;
19552 char_u
*name
= NULL
;
19555 char_u
*line_arg
= NULL
;
19558 int varargs
= FALSE
;
19559 int mustend
= FALSE
;
19564 char_u
*skip_until
= NULL
;
19567 static int func_nr
= 0; /* number for nameless function */
19572 int sourcing_lnum_off
;
19575 * ":function" without argument: list functions.
19577 if (ends_excmd(*eap
->arg
))
19581 todo
= (int)func_hashtab
.ht_used
;
19582 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19584 if (!HASHITEM_EMPTY(hi
))
19588 if (!isdigit(*fp
->uf_name
))
19589 list_func_head(fp
, FALSE
);
19593 eap
->nextcmd
= check_nextcmd(eap
->arg
);
19598 * ":function /pat": list functions matching pattern.
19600 if (*eap
->arg
== '/')
19602 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
19605 regmatch_T regmatch
;
19609 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
19611 if (regmatch
.regprog
!= NULL
)
19613 regmatch
.rm_ic
= p_ic
;
19615 todo
= (int)func_hashtab
.ht_used
;
19616 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19618 if (!HASHITEM_EMPTY(hi
))
19622 if (!isdigit(*fp
->uf_name
)
19623 && vim_regexec(®match
, fp
->uf_name
, 0))
19624 list_func_head(fp
, FALSE
);
19631 eap
->nextcmd
= check_nextcmd(p
);
19636 * Get the function name. There are these situations:
19637 * func normal function name
19638 * "name" == func, "fudi.fd_dict" == NULL
19639 * dict.func new dictionary entry
19640 * "name" == NULL, "fudi.fd_dict" set,
19641 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19642 * dict.func existing dict entry with a Funcref
19643 * "name" == func, "fudi.fd_dict" set,
19644 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19645 * dict.func existing dict entry that's not a Funcref
19646 * "name" == NULL, "fudi.fd_dict" set,
19647 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19650 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19651 paren
= (vim_strchr(p
, '(') != NULL
);
19652 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
19655 * Return on an invalid expression in braces, unless the expression
19656 * evaluation has been cancelled due to an aborting error, an
19657 * interrupt, or an exception.
19661 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
19662 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
19663 vim_free(fudi
.fd_newkey
);
19670 /* An error in a function call during evaluation of an expression in magic
19671 * braces should not cause the function not to be defined. */
19672 saved_did_emsg
= did_emsg
;
19676 * ":function func" with only function name: list function.
19680 if (!ends_excmd(*skipwhite(p
)))
19682 EMSG(_(e_trailing
));
19685 eap
->nextcmd
= check_nextcmd(p
);
19686 if (eap
->nextcmd
!= NULL
)
19688 if (!eap
->skip
&& !got_int
)
19690 fp
= find_func(name
);
19693 list_func_head(fp
, TRUE
);
19694 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
19696 if (FUNCLINE(fp
, j
) == NULL
)
19699 msg_outnum((long)(j
+ 1));
19704 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
19705 out_flush(); /* show a line at a time */
19711 msg_puts((char_u
*)" endfunction");
19715 emsg_funcname("E123: Undefined function: %s", name
);
19721 * ":function name(arg1, arg2)" Define function.
19728 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
19731 /* attempt to continue by skipping some text */
19732 if (vim_strchr(p
, '(') != NULL
)
19733 p
= vim_strchr(p
, '(');
19735 p
= skipwhite(p
+ 1);
19737 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
19738 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
19742 /* Check the name of the function. Unless it's a dictionary function
19743 * (that we are overwriting). */
19747 arg
= fudi
.fd_newkey
;
19748 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
19749 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
19751 if (*arg
== K_SPECIAL
)
19755 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
19756 : eval_isnamec(arg
[j
])))
19759 emsg_funcname(_(e_invarg2
), arg
);
19764 * Isolate the arguments: "arg1, arg2, ...)"
19768 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
19777 while (ASCII_ISALNUM(*p
) || *p
== '_')
19779 if (arg
== p
|| isdigit(*arg
)
19780 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
19781 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
19784 EMSG2(_("E125: Illegal argument: %s"), arg
);
19787 if (ga_grow(&newargs
, 1) == FAIL
)
19791 arg
= vim_strsave(arg
);
19794 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
19803 if (mustend
&& *p
!= ')')
19806 EMSG2(_(e_invarg2
), eap
->arg
);
19810 ++p
; /* skip the ')' */
19812 /* find extra arguments "range", "dict" and "abort" */
19816 if (STRNCMP(p
, "range", 5) == 0)
19821 else if (STRNCMP(p
, "dict", 4) == 0)
19826 else if (STRNCMP(p
, "abort", 5) == 0)
19835 /* When there is a line break use what follows for the function body.
19836 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19839 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
19840 EMSG(_(e_trailing
));
19843 * Read the body of the function, until ":endfunction" is found.
19847 /* Check if the function already exists, don't let the user type the
19848 * whole function before telling him it doesn't work! For a script we
19849 * need to skip the body to be able to find what follows. */
19850 if (!eap
->skip
&& !eap
->forceit
)
19852 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
19853 EMSG(_(e_funcdict
));
19854 else if (name
!= NULL
&& find_func(name
) != NULL
)
19855 emsg_funcname(e_funcexts
, name
);
19858 if (!eap
->skip
&& did_emsg
)
19861 msg_putchar('\n'); /* don't overwrite the function name */
19862 cmdline_row
= msg_row
;
19870 need_wait_return
= FALSE
;
19871 sourcing_lnum_off
= sourcing_lnum
;
19873 if (line_arg
!= NULL
)
19875 /* Use eap->arg, split up in parts by line breaks. */
19876 theline
= line_arg
;
19877 p
= vim_strchr(theline
, '\n');
19879 line_arg
+= STRLEN(line_arg
);
19886 else if (eap
->getline
== NULL
)
19887 theline
= getcmdline(':', 0L, indent
);
19889 theline
= eap
->getline(':', eap
->cookie
, indent
);
19891 lines_left
= Rows
- 1;
19892 if (theline
== NULL
)
19894 EMSG(_("E126: Missing :endfunction"));
19898 /* Detect line continuation: sourcing_lnum increased more than one. */
19899 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
19900 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
19902 sourcing_lnum_off
= 0;
19904 if (skip_until
!= NULL
)
19906 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19907 * don't check for ":endfunc". */
19908 if (STRCMP(theline
, skip_until
) == 0)
19910 vim_free(skip_until
);
19916 /* skip ':' and blanks*/
19917 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
19920 /* Check for "endfunction". */
19921 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
19923 if (line_arg
== NULL
)
19928 /* Increase indent inside "if", "while", "for" and "try", decrease
19930 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
19932 else if (STRNCMP(p
, "if", 2) == 0
19933 || STRNCMP(p
, "wh", 2) == 0
19934 || STRNCMP(p
, "for", 3) == 0
19935 || STRNCMP(p
, "try", 3) == 0)
19938 /* Check for defining a function inside this function. */
19939 if (checkforcmd(&p
, "function", 2))
19942 p
= skipwhite(p
+ 1);
19943 p
+= eval_fname_script(p
);
19944 if (ASCII_ISALPHA(*p
))
19946 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
19947 if (*skipwhite(p
) == '(')
19955 /* Check for ":append" or ":insert". */
19956 p
= skip_range(p
, NULL
);
19957 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
19959 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
19960 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
19961 skip_until
= vim_strsave((char_u
*)".");
19963 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19964 arg
= skipwhite(skiptowhite(p
));
19965 if (arg
[0] == '<' && arg
[1] =='<'
19966 && ((p
[0] == 'p' && p
[1] == 'y'
19967 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
19968 || (p
[0] == 'p' && p
[1] == 'e'
19969 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
19970 || (p
[0] == 't' && p
[1] == 'c'
19971 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
19972 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
19973 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
19974 || (p
[0] == 'm' && p
[1] == 'z'
19975 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
19978 /* ":python <<" continues until a dot, like ":append" */
19979 p
= skipwhite(arg
+ 2);
19981 skip_until
= vim_strsave((char_u
*)".");
19983 skip_until
= vim_strsave(p
);
19987 /* Add the line to the function. */
19988 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
19990 if (line_arg
== NULL
)
19995 /* Copy the line to newly allocated memory. get_one_sourceline()
19996 * allocates 250 bytes per line, this saves 80% on average. The cost
19997 * is an extra alloc/free. */
19998 p
= vim_strsave(theline
);
20001 if (line_arg
== NULL
)
20006 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
20008 /* Add NULL lines for continuation lines, so that the line count is
20009 * equal to the index in the growarray. */
20010 while (sourcing_lnum_off
-- > 0)
20011 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
20013 /* Check for end of eap->arg. */
20014 if (line_arg
!= NULL
&& *line_arg
== NUL
)
20018 /* Don't define the function when skipping commands or when an error was
20020 if (eap
->skip
|| did_emsg
)
20024 * If there are no errors, add the function
20026 if (fudi
.fd_dict
== NULL
)
20028 v
= find_var(name
, &ht
);
20029 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
20031 emsg_funcname("E707: Function name conflicts with variable: %s",
20036 fp
= find_func(name
);
20041 emsg_funcname(e_funcexts
, name
);
20044 if (fp
->uf_calls
> 0)
20046 emsg_funcname("E127: Cannot redefine function %s: It is in use",
20050 /* redefine existing function */
20051 ga_clear_strings(&(fp
->uf_args
));
20052 ga_clear_strings(&(fp
->uf_lines
));
20062 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
20064 EMSG(_(e_funcdict
));
20067 if (fudi
.fd_di
== NULL
)
20069 /* Can't add a function to a locked dictionary */
20070 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
20073 /* Can't change an existing function if it is locked */
20074 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
20077 /* Give the function a sequential number. Can only be used with a
20080 sprintf(numbuf
, "%d", ++func_nr
);
20081 name
= vim_strsave((char_u
*)numbuf
);
20088 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
20091 char_u
*scriptname
;
20093 /* Check that the autoload name matches the script name. */
20095 if (sourcing_name
!= NULL
)
20097 scriptname
= autoload_name(name
);
20098 if (scriptname
!= NULL
)
20100 p
= vim_strchr(scriptname
, '/');
20101 plen
= (int)STRLEN(p
);
20102 slen
= (int)STRLEN(sourcing_name
);
20103 if (slen
> plen
&& fnamecmp(p
,
20104 sourcing_name
+ slen
- plen
) == 0)
20106 vim_free(scriptname
);
20111 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
20116 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
20120 if (fudi
.fd_dict
!= NULL
)
20122 if (fudi
.fd_di
== NULL
)
20124 /* add new dict entry */
20125 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
20126 if (fudi
.fd_di
== NULL
)
20131 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
20133 vim_free(fudi
.fd_di
);
20139 /* overwrite existing dict entry */
20140 clear_tv(&fudi
.fd_di
->di_tv
);
20141 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
20142 fudi
.fd_di
->di_tv
.v_lock
= 0;
20143 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
20144 fp
->uf_refcount
= 1;
20146 /* behave like "dict" was used */
20150 /* insert the new function in the function list */
20151 STRCPY(fp
->uf_name
, name
);
20152 hash_add(&func_hashtab
, UF2HIKEY(fp
));
20154 fp
->uf_args
= newargs
;
20155 fp
->uf_lines
= newlines
;
20156 #ifdef FEAT_PROFILE
20157 fp
->uf_tml_count
= NULL
;
20158 fp
->uf_tml_total
= NULL
;
20159 fp
->uf_tml_self
= NULL
;
20160 fp
->uf_profiling
= FALSE
;
20161 if (prof_def_func())
20162 func_do_profile(fp
);
20164 fp
->uf_varargs
= varargs
;
20165 fp
->uf_flags
= flags
;
20167 fp
->uf_script_ID
= current_SID
;
20171 ga_clear_strings(&newargs
);
20172 ga_clear_strings(&newlines
);
20174 vim_free(skip_until
);
20175 vim_free(fudi
.fd_newkey
);
20177 did_emsg
|= saved_did_emsg
;
20181 * Get a function name, translating "<SID>" and "<SNR>".
20182 * Also handles a Funcref in a List or Dictionary.
20183 * Returns the function name in allocated memory, or NULL for failure.
20185 * TFN_INT: internal function name OK
20186 * TFN_QUIET: be quiet
20187 * Advances "pp" to just after the function name (if no error).
20190 trans_function_name(pp
, skip
, flags
, fdp
)
20192 int skip
; /* only find the end, don't evaluate */
20194 funcdict_T
*fdp
; /* return: info about dictionary used */
20196 char_u
*name
= NULL
;
20200 char_u sid_buf
[20];
20205 vim_memset(fdp
, 0, sizeof(funcdict_T
));
20208 /* Check for hard coded <SNR>: already translated function ID (from a user
20210 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
20211 && (*pp
)[2] == (int)KE_SNR
)
20214 len
= get_id_len(pp
) + 3;
20215 return vim_strnsave(start
, len
);
20218 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20219 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
20220 lead
= eval_fname_script(start
);
20224 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
20225 lead
> 2 ? 0 : FNE_CHECK_START
);
20229 EMSG(_("E129: Function name required"));
20232 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
20235 * Report an invalid expression in braces, unless the expression
20236 * evaluation has been cancelled due to an aborting error, an
20237 * interrupt, or an exception.
20242 EMSG2(_(e_invarg2
), start
);
20245 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
20249 if (lv
.ll_tv
!= NULL
)
20253 fdp
->fd_dict
= lv
.ll_dict
;
20254 fdp
->fd_newkey
= lv
.ll_newkey
;
20255 lv
.ll_newkey
= NULL
;
20256 fdp
->fd_di
= lv
.ll_di
;
20258 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
20260 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
20265 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
20266 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
20267 EMSG(_(e_funcref
));
20275 if (lv
.ll_name
== NULL
)
20277 /* Error found, but continue after the function name. */
20282 /* Check if the name is a Funcref. If so, use the value. */
20283 if (lv
.ll_exp_name
!= NULL
)
20285 len
= (int)STRLEN(lv
.ll_exp_name
);
20286 name
= deref_func_name(lv
.ll_exp_name
, &len
);
20287 if (name
== lv
.ll_exp_name
)
20292 len
= (int)(end
- *pp
);
20293 name
= deref_func_name(*pp
, &len
);
20299 name
= vim_strsave(name
);
20304 if (lv
.ll_exp_name
!= NULL
)
20306 len
= (int)STRLEN(lv
.ll_exp_name
);
20307 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
20308 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
20310 /* When there was "s:" already or the name expanded to get a
20311 * leading "s:" then remove it. */
20319 if (lead
== 2) /* skip over "s:" */
20321 len
= (int)(end
- lv
.ll_name
);
20325 * Copy the function name to allocated memory.
20326 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20327 * Accept <SNR>123_name() outside a script.
20330 lead
= 0; /* do nothing */
20334 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
20335 || eval_fname_sid(*pp
))
20337 /* It's "s:" or "<SID>" */
20338 if (current_SID
<= 0)
20340 EMSG(_(e_usingsid
));
20343 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
20344 lead
+= (int)STRLEN(sid_buf
);
20347 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
20349 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
20352 name
= alloc((unsigned)(len
+ lead
+ 1));
20357 name
[0] = K_SPECIAL
;
20358 name
[1] = KS_EXTRA
;
20359 name
[2] = (int)KE_SNR
;
20360 if (lead
> 3) /* If it's "<SID>" */
20361 STRCPY(name
+ 3, sid_buf
);
20363 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
20364 name
[len
+ lead
] = NUL
;
20374 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20375 * Return 2 if "p" starts with "s:".
20376 * Return 0 otherwise.
20379 eval_fname_script(p
)
20382 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
20383 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
20385 if (p
[0] == 's' && p
[1] == ':')
20391 * Return TRUE if "p" starts with "<SID>" or "s:".
20392 * Only works if eval_fname_script() returned non-zero for "p"!
20398 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
20402 * List the head of the function: "name(arg1, arg2)".
20405 list_func_head(fp
, indent
)
20414 MSG_PUTS("function ");
20415 if (fp
->uf_name
[0] == K_SPECIAL
)
20417 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
20418 msg_puts(fp
->uf_name
+ 3);
20421 msg_puts(fp
->uf_name
);
20423 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
20427 msg_puts(FUNCARG(fp
, j
));
20429 if (fp
->uf_varargs
)
20438 last_set_msg(fp
->uf_script_ID
);
20442 * Find a function by name, return pointer to it in ufuncs.
20443 * Return NULL for unknown function.
20451 hi
= hash_find(&func_hashtab
, name
);
20452 if (!HASHITEM_EMPTY(hi
))
20457 #if defined(EXITFREE) || defined(PROTO)
20459 free_all_functions()
20463 /* Need to start all over every time, because func_free() may change the
20465 while (func_hashtab
.ht_used
> 0)
20466 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
20467 if (!HASHITEM_EMPTY(hi
))
20469 func_free(HI2UF(hi
));
20476 * Return TRUE if a function "name" exists.
20479 function_exists(name
)
20486 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
20487 nm
= skipwhite(nm
);
20489 /* Only accept "funcname", "funcname ", "funcname (..." and
20490 * "funcname(...", not "funcname!...". */
20491 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
20493 if (builtin_function(p
))
20494 n
= (find_internal_func(p
) >= 0);
20496 n
= (find_func(p
) != NULL
);
20503 * Return TRUE if "name" looks like a builtin function name: starts with a
20504 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
20507 builtin_function(name
)
20510 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
20511 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
20514 #if defined(FEAT_PROFILE) || defined(PROTO)
20516 * Start profiling function "fp".
20519 func_do_profile(fp
)
20522 fp
->uf_tm_count
= 0;
20523 profile_zero(&fp
->uf_tm_self
);
20524 profile_zero(&fp
->uf_tm_total
);
20525 if (fp
->uf_tml_count
== NULL
)
20526 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
20527 (sizeof(int) * fp
->uf_lines
.ga_len
));
20528 if (fp
->uf_tml_total
== NULL
)
20529 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
20530 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20531 if (fp
->uf_tml_self
== NULL
)
20532 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
20533 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20534 fp
->uf_tml_idx
= -1;
20535 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
20536 || fp
->uf_tml_self
== NULL
)
20537 return; /* out of memory */
20539 fp
->uf_profiling
= TRUE
;
20543 * Dump the profiling results for all functions in file "fd".
20546 func_dump_profile(fd
)
20556 todo
= (int)func_hashtab
.ht_used
;
20557 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
20559 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
20561 if (!HASHITEM_EMPTY(hi
))
20565 if (fp
->uf_profiling
)
20567 if (sorttab
!= NULL
)
20568 sorttab
[st_len
++] = fp
;
20570 if (fp
->uf_name
[0] == K_SPECIAL
)
20571 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
20573 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
20574 if (fp
->uf_tm_count
== 1)
20575 fprintf(fd
, "Called 1 time\n");
20577 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
20578 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
20579 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
20581 fprintf(fd
, "count total (s) self (s)\n");
20583 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
20585 if (FUNCLINE(fp
, i
) == NULL
)
20587 prof_func_line(fd
, fp
->uf_tml_count
[i
],
20588 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
20589 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
20596 if (sorttab
!= NULL
&& st_len
> 0)
20598 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20600 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
20601 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20603 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
20608 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
20613 int prefer_self
; /* when equal print only self time */
20618 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
20619 fprintf(fd
, "count total (s) self (s) function\n");
20620 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
20623 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
20625 if (fp
->uf_name
[0] == K_SPECIAL
)
20626 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
20628 fprintf(fd
, " %s()\n", fp
->uf_name
);
20634 * Print the count and times for one function or function line.
20637 prof_func_line(fd
, count
, total
, self
, prefer_self
)
20642 int prefer_self
; /* when equal print only self time */
20646 fprintf(fd
, "%5d ", count
);
20647 if (prefer_self
&& profile_equal(total
, self
))
20650 fprintf(fd
, "%s ", profile_msg(total
));
20651 if (!prefer_self
&& profile_equal(total
, self
))
20654 fprintf(fd
, "%s ", profile_msg(self
));
20661 * Compare function for total time sorting.
20664 #ifdef __BORLANDC__
20667 prof_total_cmp(s1
, s2
)
20673 p1
= *(ufunc_T
**)s1
;
20674 p2
= *(ufunc_T
**)s2
;
20675 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
20679 * Compare function for self time sorting.
20682 #ifdef __BORLANDC__
20685 prof_self_cmp(s1
, s2
)
20691 p1
= *(ufunc_T
**)s1
;
20692 p2
= *(ufunc_T
**)s2
;
20693 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
20699 * If "name" has a package name try autoloading the script for it.
20700 * Return TRUE if a package was loaded.
20703 script_autoload(name
, reload
)
20705 int reload
; /* load script again when already loaded */
20708 char_u
*scriptname
, *tofree
;
20712 /* If there is no '#' after name[0] there is no package name. */
20713 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
20714 if (p
== NULL
|| p
== name
)
20717 tofree
= scriptname
= autoload_name(name
);
20719 /* Find the name in the list of previously loaded package names. Skip
20720 * "autoload/", it's always the same. */
20721 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
20722 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
20724 if (!reload
&& i
< ga_loaded
.ga_len
)
20725 ret
= FALSE
; /* was loaded already */
20728 /* Remember the name if it wasn't loaded already. */
20729 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
20731 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
20735 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
20736 if (source_runtime(scriptname
, FALSE
) == OK
)
20745 * Return the autoload script name for a function or variable name.
20746 * Returns NULL when out of memory.
20749 autoload_name(name
)
20753 char_u
*scriptname
;
20755 /* Get the script file name: replace '#' with '/', append ".vim". */
20756 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
20757 if (scriptname
== NULL
)
20759 STRCPY(scriptname
, "autoload/");
20760 STRCAT(scriptname
, name
);
20761 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
20762 STRCAT(scriptname
, ".vim");
20763 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
20768 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20771 * Function given to ExpandGeneric() to obtain the list of user defined
20775 get_user_func_name(xp
, idx
)
20779 static long_u done
;
20780 static hashitem_T
*hi
;
20786 hi
= func_hashtab
.ht_array
;
20788 if (done
< func_hashtab
.ht_used
)
20792 while (HASHITEM_EMPTY(hi
))
20796 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
20797 return fp
->uf_name
; /* prevents overflow */
20799 cat_func_name(IObuff
, fp
);
20800 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
20802 STRCAT(IObuff
, "(");
20803 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
20804 STRCAT(IObuff
, ")");
20811 #endif /* FEAT_CMDL_COMPL */
20814 * Copy the function name of "fp" to buffer "buf".
20815 * "buf" must be able to hold the function name plus three bytes.
20816 * Takes care of script-local function names.
20819 cat_func_name(buf
, fp
)
20823 if (fp
->uf_name
[0] == K_SPECIAL
)
20825 STRCPY(buf
, "<SNR>");
20826 STRCAT(buf
, fp
->uf_name
+ 3);
20829 STRCPY(buf
, fp
->uf_name
);
20833 * ":delfunction {name}"
20836 ex_delfunction(eap
)
20839 ufunc_T
*fp
= NULL
;
20845 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
20846 vim_free(fudi
.fd_newkey
);
20849 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
20850 EMSG(_(e_funcref
));
20853 if (!ends_excmd(*skipwhite(p
)))
20856 EMSG(_(e_trailing
));
20859 eap
->nextcmd
= check_nextcmd(p
);
20860 if (eap
->nextcmd
!= NULL
)
20864 fp
= find_func(name
);
20871 EMSG2(_(e_nofunc
), eap
->arg
);
20874 if (fp
->uf_calls
> 0)
20876 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
20880 if (fudi
.fd_dict
!= NULL
)
20882 /* Delete the dict item that refers to the function, it will
20883 * invoke func_unref() and possibly delete the function. */
20884 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
20892 * Free a function and remove it from the list of functions.
20900 /* clear this function */
20901 ga_clear_strings(&(fp
->uf_args
));
20902 ga_clear_strings(&(fp
->uf_lines
));
20903 #ifdef FEAT_PROFILE
20904 vim_free(fp
->uf_tml_count
);
20905 vim_free(fp
->uf_tml_total
);
20906 vim_free(fp
->uf_tml_self
);
20909 /* remove the function from the function hashtable */
20910 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
20911 if (HASHITEM_EMPTY(hi
))
20912 EMSG2(_(e_intern2
), "func_free()");
20914 hash_remove(&func_hashtab
, hi
);
20920 * Unreference a Function: decrement the reference count and free it when it
20921 * becomes zero. Only for numbered functions.
20929 if (name
!= NULL
&& isdigit(*name
))
20931 fp
= find_func(name
);
20933 EMSG2(_(e_intern2
), "func_unref()");
20934 else if (--fp
->uf_refcount
<= 0)
20936 /* Only delete it when it's not being used. Otherwise it's done
20937 * when "uf_calls" becomes zero. */
20938 if (fp
->uf_calls
== 0)
20945 * Count a reference to a Function.
20953 if (name
!= NULL
&& isdigit(*name
))
20955 fp
= find_func(name
);
20957 EMSG2(_(e_intern2
), "func_ref()");
20964 * Call a user function.
20967 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
20968 ufunc_T
*fp
; /* pointer to function */
20969 int argcount
; /* nr of args */
20970 typval_T
*argvars
; /* arguments */
20971 typval_T
*rettv
; /* return value */
20972 linenr_T firstline
; /* first line of range */
20973 linenr_T lastline
; /* last line of range */
20974 dict_T
*selfdict
; /* Dictionary for "self" */
20976 char_u
*save_sourcing_name
;
20977 linenr_T save_sourcing_lnum
;
20978 scid_T save_current_SID
;
20981 static int depth
= 0;
20983 int fixvar_idx
= 0; /* index in fixvar[] */
20986 char_u numbuf
[NUMBUFLEN
];
20988 #ifdef FEAT_PROFILE
20989 proftime_T wait_start
;
20990 proftime_T call_start
;
20993 /* If depth of calling is getting too high, don't execute the function */
20994 if (depth
>= p_mfd
)
20996 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
20997 rettv
->v_type
= VAR_NUMBER
;
20998 rettv
->vval
.v_number
= -1;
21003 line_breakcheck(); /* check for CTRL-C hit */
21005 fc
.caller
= current_funccal
;
21006 current_funccal
= &fc
;
21009 rettv
->vval
.v_number
= 0;
21011 fc
.returned
= FALSE
;
21012 fc
.level
= ex_nesting_level
;
21013 /* Check if this function has a breakpoint. */
21014 fc
.breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
21015 fc
.dbg_tick
= debug_tick
;
21018 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21019 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21020 * each argument variable and saves a lot of time.
21023 * Init l: variables.
21025 init_var_dict(&fc
.l_vars
, &fc
.l_vars_var
);
21026 if (selfdict
!= NULL
)
21028 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21029 * some compiler that checks the destination size. */
21030 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21032 STRCPY(name
, "self");
21033 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
21034 hash_add(&fc
.l_vars
.dv_hashtab
, DI2HIKEY(v
));
21035 v
->di_tv
.v_type
= VAR_DICT
;
21036 v
->di_tv
.v_lock
= 0;
21037 v
->di_tv
.vval
.v_dict
= selfdict
;
21038 ++selfdict
->dv_refcount
;
21042 * Init a: variables.
21043 * Set a:0 to "argcount".
21044 * Set a:000 to a list with room for the "..." arguments.
21046 init_var_dict(&fc
.l_avars
, &fc
.l_avars_var
);
21047 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "0",
21048 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
21049 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21050 STRCPY(v
->di_key
, "000");
21051 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21052 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
21053 v
->di_tv
.v_type
= VAR_LIST
;
21054 v
->di_tv
.v_lock
= VAR_FIXED
;
21055 v
->di_tv
.vval
.v_list
= &fc
.l_varlist
;
21056 vim_memset(&fc
.l_varlist
, 0, sizeof(list_T
));
21057 fc
.l_varlist
.lv_refcount
= 99999;
21058 fc
.l_varlist
.lv_lock
= VAR_FIXED
;
21061 * Set a:firstline to "firstline" and a:lastline to "lastline".
21062 * Set a:name to named arguments.
21063 * Set a:N to the "..." arguments.
21065 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "firstline",
21066 (varnumber_T
)firstline
);
21067 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "lastline",
21068 (varnumber_T
)lastline
);
21069 for (i
= 0; i
< argcount
; ++i
)
21071 ai
= i
- fp
->uf_args
.ga_len
;
21073 /* named argument a:name */
21074 name
= FUNCARG(fp
, i
);
21077 /* "..." argument a:1, a:2, etc. */
21078 sprintf((char *)numbuf
, "%d", ai
+ 1);
21081 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
21083 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21084 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21088 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
21092 v
->di_flags
= DI_FLAGS_RO
;
21094 STRCPY(v
->di_key
, name
);
21095 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
21097 /* Note: the values are copied directly to avoid alloc/free.
21098 * "argvars" must have VAR_FIXED for v_lock. */
21099 v
->di_tv
= argvars
[i
];
21100 v
->di_tv
.v_lock
= VAR_FIXED
;
21102 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
21104 list_append(&fc
.l_varlist
, &fc
.l_listitems
[ai
]);
21105 fc
.l_listitems
[ai
].li_tv
= argvars
[i
];
21106 fc
.l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
21110 /* Don't redraw while executing the function. */
21111 ++RedrawingDisabled
;
21112 save_sourcing_name
= sourcing_name
;
21113 save_sourcing_lnum
= sourcing_lnum
;
21115 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
21116 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
21117 if (sourcing_name
!= NULL
)
21119 if (save_sourcing_name
!= NULL
21120 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
21121 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
21123 STRCPY(sourcing_name
, "function ");
21124 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
21126 if (p_verbose
>= 12)
21129 verbose_enter_scroll();
21131 smsg((char_u
*)_("calling %s"), sourcing_name
);
21132 if (p_verbose
>= 14)
21134 char_u buf
[MSG_BUF_LEN
];
21135 char_u numbuf2
[NUMBUFLEN
];
21139 msg_puts((char_u
*)"(");
21140 for (i
= 0; i
< argcount
; ++i
)
21143 msg_puts((char_u
*)", ");
21144 if (argvars
[i
].v_type
== VAR_NUMBER
)
21145 msg_outnum((long)argvars
[i
].vval
.v_number
);
21148 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
21151 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21157 msg_puts((char_u
*)")");
21159 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21161 verbose_leave_scroll();
21165 #ifdef FEAT_PROFILE
21166 if (do_profiling
== PROF_YES
)
21168 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
21169 func_do_profile(fp
);
21170 if (fp
->uf_profiling
21171 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
))
21174 profile_start(&call_start
);
21175 profile_zero(&fp
->uf_tm_children
);
21177 script_prof_save(&wait_start
);
21181 save_current_SID
= current_SID
;
21182 current_SID
= fp
->uf_script_ID
;
21183 save_did_emsg
= did_emsg
;
21186 /* call do_cmdline() to execute the lines */
21187 do_cmdline(NULL
, get_func_line
, (void *)&fc
,
21188 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
21190 --RedrawingDisabled
;
21192 /* when the function was aborted because of an error, return -1 */
21193 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
21196 rettv
->v_type
= VAR_NUMBER
;
21197 rettv
->vval
.v_number
= -1;
21200 #ifdef FEAT_PROFILE
21201 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
21202 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)))
21204 profile_end(&call_start
);
21205 profile_sub_wait(&wait_start
, &call_start
);
21206 profile_add(&fp
->uf_tm_total
, &call_start
);
21207 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
21208 if (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)
21210 profile_add(&fc
.caller
->func
->uf_tm_children
, &call_start
);
21211 profile_add(&fc
.caller
->func
->uf_tml_children
, &call_start
);
21216 /* when being verbose, mention the return value */
21217 if (p_verbose
>= 12)
21220 verbose_enter_scroll();
21223 smsg((char_u
*)_("%s aborted"), sourcing_name
);
21224 else if (fc
.rettv
->v_type
== VAR_NUMBER
)
21225 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
21226 (long)fc
.rettv
->vval
.v_number
);
21229 char_u buf
[MSG_BUF_LEN
];
21230 char_u numbuf2
[NUMBUFLEN
];
21234 /* The value may be very long. Skip the middle part, so that we
21235 * have some idea how it starts and ends. smsg() would always
21236 * truncate it at the end. */
21237 s
= tv2string(fc
.rettv
, &tofree
, numbuf2
, 0);
21240 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21241 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
21245 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21247 verbose_leave_scroll();
21251 vim_free(sourcing_name
);
21252 sourcing_name
= save_sourcing_name
;
21253 sourcing_lnum
= save_sourcing_lnum
;
21254 current_SID
= save_current_SID
;
21255 #ifdef FEAT_PROFILE
21256 if (do_profiling
== PROF_YES
)
21257 script_prof_restore(&wait_start
);
21260 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
21263 verbose_enter_scroll();
21265 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
21266 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21268 verbose_leave_scroll();
21272 did_emsg
|= save_did_emsg
;
21273 current_funccal
= fc
.caller
;
21275 /* The a: variables typevals were not allocated, only free the allocated
21277 vars_clear_ext(&fc
.l_avars
.dv_hashtab
, FALSE
);
21279 vars_clear(&fc
.l_vars
.dv_hashtab
); /* free all l: variables */
21284 * Add a number variable "name" to dict "dp" with value "nr".
21287 add_nr_var(dp
, v
, name
, nr
)
21293 STRCPY(v
->di_key
, name
);
21294 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21295 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
21296 v
->di_tv
.v_type
= VAR_NUMBER
;
21297 v
->di_tv
.v_lock
= VAR_FIXED
;
21298 v
->di_tv
.vval
.v_number
= nr
;
21308 char_u
*arg
= eap
->arg
;
21310 int returning
= FALSE
;
21312 if (current_funccal
== NULL
)
21314 EMSG(_("E133: :return not inside a function"));
21321 eap
->nextcmd
= NULL
;
21322 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
21323 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
21326 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
21330 /* It's safer to return also on error. */
21331 else if (!eap
->skip
)
21334 * Return unless the expression evaluation has been cancelled due to an
21335 * aborting error, an interrupt, or an exception.
21338 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
21341 /* When skipping or the return gets pending, advance to the next command
21342 * in this line (!returning). Otherwise, ignore the rest of the line.
21343 * Following lines will be ignored by get_func_line(). */
21345 eap
->nextcmd
= NULL
;
21346 else if (eap
->nextcmd
== NULL
) /* no argument */
21347 eap
->nextcmd
= check_nextcmd(arg
);
21354 * Return from a function. Possibly makes the return pending. Also called
21355 * for a pending return at the ":endtry" or after returning from an extra
21356 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
21357 * when called due to a ":return" command. "rettv" may point to a typval_T
21358 * with the return rettv. Returns TRUE when the return can be carried out,
21359 * FALSE when the return gets pending.
21362 do_return(eap
, reanimate
, is_cmd
, rettv
)
21369 struct condstack
*cstack
= eap
->cstack
;
21372 /* Undo the return. */
21373 current_funccal
->returned
= FALSE
;
21376 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21377 * not in its finally clause (which then is to be executed next) is found.
21378 * In this case, make the ":return" pending for execution at the ":endtry".
21379 * Otherwise, return normally.
21381 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
21384 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
21386 if (!is_cmd
&& !reanimate
)
21387 /* A pending return again gets pending. "rettv" points to an
21388 * allocated variable with the rettv of the original ":return"'s
21389 * argument if present or is NULL else. */
21390 cstack
->cs_rettv
[idx
] = rettv
;
21393 /* When undoing a return in order to make it pending, get the stored
21396 rettv
= current_funccal
->rettv
;
21400 /* Store the value of the pending return. */
21401 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
21402 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
21404 EMSG(_(e_outofmem
));
21407 cstack
->cs_rettv
[idx
] = NULL
;
21411 /* The pending return value could be overwritten by a ":return"
21412 * without argument in a finally clause; reset the default
21414 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
21415 current_funccal
->rettv
->vval
.v_number
= 0;
21418 report_make_pending(CSTP_RETURN
, rettv
);
21422 current_funccal
->returned
= TRUE
;
21424 /* If the return is carried out now, store the return value. For
21425 * a return immediately after reanimation, the value is already
21427 if (!reanimate
&& rettv
!= NULL
)
21429 clear_tv(current_funccal
->rettv
);
21430 *current_funccal
->rettv
= *(typval_T
*)rettv
;
21440 * Free the variable with a pending return value.
21443 discard_pending_return(rettv
)
21446 free_tv((typval_T
*)rettv
);
21450 * Generate a return command for producing the value of "rettv". The result
21451 * is an allocated string. Used by report_pending() for verbose messages.
21454 get_return_cmd(rettv
)
21458 char_u
*tofree
= NULL
;
21459 char_u numbuf
[NUMBUFLEN
];
21462 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
21466 STRCPY(IObuff
, ":return ");
21467 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
21468 if (STRLEN(s
) + 8 >= IOSIZE
)
21469 STRCPY(IObuff
+ IOSIZE
- 4, "...");
21471 return vim_strsave(IObuff
);
21475 * Get next function line.
21476 * Called by do_cmdline() to get the next line.
21477 * Returns allocated string, or NULL for end of function.
21481 get_func_line(c
, cookie
, indent
)
21482 int c
; /* not used */
21484 int indent
; /* not used */
21486 funccall_T
*fcp
= (funccall_T
*)cookie
;
21487 ufunc_T
*fp
= fcp
->func
;
21489 garray_T
*gap
; /* growarray with function lines */
21491 /* If breakpoints have been added/deleted need to check for it. */
21492 if (fcp
->dbg_tick
!= debug_tick
)
21494 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21496 fcp
->dbg_tick
= debug_tick
;
21498 #ifdef FEAT_PROFILE
21499 if (do_profiling
== PROF_YES
)
21500 func_line_end(cookie
);
21503 gap
= &fp
->uf_lines
;
21504 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21509 /* Skip NULL lines (continuation lines). */
21510 while (fcp
->linenr
< gap
->ga_len
21511 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
21513 if (fcp
->linenr
>= gap
->ga_len
)
21517 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
21518 sourcing_lnum
= fcp
->linenr
;
21519 #ifdef FEAT_PROFILE
21520 if (do_profiling
== PROF_YES
)
21521 func_line_start(cookie
);
21526 /* Did we encounter a breakpoint? */
21527 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
21529 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
21530 /* Find next breakpoint. */
21531 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21533 fcp
->dbg_tick
= debug_tick
;
21539 #if defined(FEAT_PROFILE) || defined(PROTO)
21541 * Called when starting to read a function line.
21542 * "sourcing_lnum" must be correct!
21543 * When skipping lines it may not actually be executed, but we won't find out
21544 * until later and we need to store the time now.
21547 func_line_start(cookie
)
21550 funccall_T
*fcp
= (funccall_T
*)cookie
;
21551 ufunc_T
*fp
= fcp
->func
;
21553 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
21554 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
21556 fp
->uf_tml_idx
= sourcing_lnum
- 1;
21557 /* Skip continuation lines. */
21558 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
21560 fp
->uf_tml_execed
= FALSE
;
21561 profile_start(&fp
->uf_tml_start
);
21562 profile_zero(&fp
->uf_tml_children
);
21563 profile_get_wait(&fp
->uf_tml_wait
);
21568 * Called when actually executing a function line.
21571 func_line_exec(cookie
)
21574 funccall_T
*fcp
= (funccall_T
*)cookie
;
21575 ufunc_T
*fp
= fcp
->func
;
21577 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21578 fp
->uf_tml_execed
= TRUE
;
21582 * Called when done with a function line.
21585 func_line_end(cookie
)
21588 funccall_T
*fcp
= (funccall_T
*)cookie
;
21589 ufunc_T
*fp
= fcp
->func
;
21591 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21593 if (fp
->uf_tml_execed
)
21595 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
21596 profile_end(&fp
->uf_tml_start
);
21597 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
21598 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
21599 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
21600 &fp
->uf_tml_children
);
21602 fp
->uf_tml_idx
= -1;
21608 * Return TRUE if the currently active function should be ended, because a
21609 * return was encountered or an error occurred. Used inside a ":while".
21612 func_has_ended(cookie
)
21615 funccall_T
*fcp
= (funccall_T
*)cookie
;
21617 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21618 * an error inside a try conditional. */
21619 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21624 * return TRUE if cookie indicates a function which "abort"s on errors.
21627 func_has_abort(cookie
)
21630 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
21633 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21636 VAR_FLAVOUR_DEFAULT
, /* doesn't start with uppercase */
21637 VAR_FLAVOUR_SESSION
, /* starts with uppercase, some lower */
21638 VAR_FLAVOUR_VIMINFO
/* all uppercase */
21641 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
21643 static var_flavour_T
21644 var_flavour(varname
)
21647 char_u
*p
= varname
;
21649 if (ASCII_ISUPPER(*p
))
21652 if (ASCII_ISLOWER(*p
))
21653 return VAR_FLAVOUR_SESSION
;
21654 return VAR_FLAVOUR_VIMINFO
;
21657 return VAR_FLAVOUR_DEFAULT
;
21661 #if defined(FEAT_VIMINFO) || defined(PROTO)
21663 * Restore global vars that start with a capital from the viminfo file
21666 read_viminfo_varlist(virp
, writing
)
21671 int type
= VAR_NUMBER
;
21674 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
21676 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
21679 *tab
++ = '\0'; /* isolate the variable name */
21680 if (*tab
== 'S') /* string var */
21683 else if (*tab
== 'F')
21687 tab
= vim_strchr(tab
, '\t');
21691 if (type
== VAR_STRING
)
21692 tv
.vval
.v_string
= viminfo_readstring(virp
,
21693 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
21695 else if (type
== VAR_FLOAT
)
21696 (void)string2float(tab
+ 1, &tv
.vval
.v_float
);
21699 tv
.vval
.v_number
= atol((char *)tab
+ 1);
21700 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
21701 if (type
== VAR_STRING
)
21702 vim_free(tv
.vval
.v_string
);
21707 return viminfo_readline(virp
);
21711 * Write global vars that start with a capital to the viminfo file
21714 write_viminfo_varlist(fp
)
21718 dictitem_T
*this_var
;
21723 char_u numbuf
[NUMBUFLEN
];
21725 if (find_viminfo_parameter('!') == NULL
)
21728 fprintf(fp
, _("\n# global variables:\n"));
21730 todo
= (int)globvarht
.ht_used
;
21731 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21733 if (!HASHITEM_EMPTY(hi
))
21736 this_var
= HI2DI(hi
);
21737 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
21739 switch (this_var
->di_tv
.v_type
)
21741 case VAR_STRING
: s
= "STR"; break;
21742 case VAR_NUMBER
: s
= "NUM"; break;
21744 case VAR_FLOAT
: s
= "FLO"; break;
21748 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
21749 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
21751 viminfo_writestring(fp
, p
);
21759 #if defined(FEAT_SESSION) || defined(PROTO)
21761 store_session_globals(fd
)
21765 dictitem_T
*this_var
;
21769 todo
= (int)globvarht
.ht_used
;
21770 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21772 if (!HASHITEM_EMPTY(hi
))
21775 this_var
= HI2DI(hi
);
21776 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
21777 || this_var
->di_tv
.v_type
== VAR_STRING
)
21778 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
21780 /* Escape special characters with a backslash. Turn a LF and
21781 * CR into \n and \r. */
21782 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
21783 (char_u
*)"\\\"\n\r");
21784 if (p
== NULL
) /* out of memory */
21786 for (t
= p
; *t
!= NUL
; ++t
)
21789 else if (*t
== '\r')
21791 if ((fprintf(fd
, "let %s = %c%s%c",
21793 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
21796 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
21798 || put_eol(fd
) == FAIL
)
21806 else if (this_var
->di_tv
.v_type
== VAR_FLOAT
21807 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
21809 float_T f
= this_var
->di_tv
.vval
.v_float
;
21817 if ((fprintf(fd
, "let %s = %c&%f",
21818 this_var
->di_key
, sign
, f
) < 0)
21819 || put_eol(fd
) == FAIL
)
21830 * Display script name where an item was last set.
21831 * Should only be invoked when 'verbose' is non-zero.
21834 last_set_msg(scriptID
)
21841 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
21845 MSG_PUTS(_("\n\tLast set from "));
21853 #endif /* FEAT_EVAL */
21856 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21860 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21862 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
21863 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
21864 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
21867 * Get the short path (8.3) for the filename in "fnamep".
21868 * Only works for a valid file name.
21869 * When the path gets longer "fnamep" is changed and the allocated buffer
21870 * is put in "bufp".
21871 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21872 * Returns OK on success, FAIL on failure.
21875 get_short_pathname(fnamep
, bufp
, fnamelen
)
21884 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
21887 /* If that doesn't work (not enough space), then save the string
21888 * and try again with a new buffer big enough. */
21889 newbuf
= vim_strnsave(*fnamep
, l
);
21890 if (newbuf
== NULL
)
21894 *fnamep
= *bufp
= newbuf
;
21896 /* Really should always succeed, as the buffer is big enough. */
21897 l
= GetShortPathName(*fnamep
, *fnamep
, l
+1);
21905 * Get the short path (8.3) for the filename in "fname". The converted
21906 * path is returned in "bufp".
21908 * Some of the directories specified in "fname" may not exist. This function
21909 * will shorten the existing directories at the beginning of the path and then
21910 * append the remaining non-existing path.
21912 * fname - Pointer to the filename to shorten. On return, contains the
21913 * pointer to the shortened pathname
21914 * bufp - Pointer to an allocated buffer for the filename.
21915 * fnamelen - Length of the filename pointed to by fname
21917 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
21920 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
21925 char_u
*short_fname
, *save_fname
, *pbuf_unused
;
21926 char_u
*endp
, *save_endp
;
21929 int new_len
, sfx_len
;
21933 old_len
= *fnamelen
;
21934 save_fname
= vim_strnsave(*fname
, old_len
);
21935 pbuf_unused
= NULL
;
21936 short_fname
= NULL
;
21938 endp
= save_fname
+ old_len
- 1; /* Find the end of the copy */
21942 * Try shortening the supplied path till it succeeds by removing one
21943 * directory at a time from the tail of the path.
21948 /* go back one path-separator */
21949 while (endp
> save_fname
&& !after_pathsep(save_fname
, endp
+ 1))
21951 if (endp
<= save_fname
)
21952 break; /* processed the complete path */
21955 * Replace the path separator with a NUL and try to shorten the
21960 short_fname
= save_fname
;
21961 len
= STRLEN(short_fname
) + 1;
21962 if (get_short_pathname(&short_fname
, &pbuf_unused
, &len
) == FAIL
)
21967 *endp
= ch
; /* preserve the string */
21970 break; /* successfully shortened the path */
21972 /* failed to shorten the path. Skip the path separator */
21979 * Succeeded in shortening the path. Now concatenate the shortened
21980 * path with the remaining path at the tail.
21983 /* Compute the length of the new path. */
21984 sfx_len
= (int)(save_endp
- endp
) + 1;
21985 new_len
= len
+ sfx_len
;
21987 *fnamelen
= new_len
;
21989 if (new_len
> old_len
)
21991 /* There is not enough space in the currently allocated string,
21992 * copy it to a buffer big enough. */
21993 *fname
= *bufp
= vim_strnsave(short_fname
, new_len
);
21994 if (*fname
== NULL
)
22002 /* Transfer short_fname to the main buffer (it's big enough),
22003 * unless get_short_pathname() did its work in-place. */
22004 *fname
= *bufp
= save_fname
;
22005 if (short_fname
!= save_fname
)
22006 vim_strncpy(save_fname
, short_fname
, len
);
22010 /* concat the not-shortened part of the path */
22011 vim_strncpy(*fname
+ len
, endp
, sfx_len
);
22012 (*fname
)[new_len
] = NUL
;
22016 vim_free(pbuf_unused
);
22017 vim_free(save_fname
);
22023 * Get a pathname for a partial path.
22024 * Returns OK for success, FAIL for failure.
22027 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
22032 int sepcount
, len
, tflen
;
22034 char_u
*pbuf
, *tfname
;
22037 /* Count up the path separators from the RHS.. so we know which part
22038 * of the path to return. */
22040 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
22041 if (vim_ispathsep(*p
))
22044 /* Need full path first (use expand_env() to remove a "~/") */
22045 hasTilde
= (**fnamep
== '~');
22047 pbuf
= tfname
= expand_env_save(*fnamep
);
22049 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
22051 len
= tflen
= (int)STRLEN(tfname
);
22053 if (get_short_pathname(&tfname
, &pbuf
, &len
) == FAIL
)
22058 /* Don't have a valid filename, so shorten the rest of the
22059 * path if we can. This CAN give us invalid 8.3 filenames, but
22060 * there's not a lot of point in guessing what it might be.
22063 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == FAIL
)
22067 /* Count the paths backward to find the beginning of the desired string. */
22068 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
22072 p
-= mb_head_off(tfname
, p
);
22074 if (vim_ispathsep(*p
))
22076 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
22093 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22095 *fnamelen
= (int)STRLEN(p
);
22101 #endif /* WIN3264 */
22104 * Adjust a filename, according to a string of modifiers.
22105 * *fnamep must be NUL terminated when called. When returning, the length is
22106 * determined by *fnamelen.
22107 * Returns VALID_ flags or -1 for failure.
22108 * When there is an error, *fnamep is set to NULL.
22111 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
22112 char_u
*src
; /* string with modifiers */
22113 int *usedlen
; /* characters after src that are used */
22114 char_u
**fnamep
; /* file name so far */
22115 char_u
**bufp
; /* buffer for allocated file name or NULL */
22116 int *fnamelen
; /* length of fnamep */
22120 char_u
*s
, *p
, *pbuf
;
22121 char_u dirname
[MAXPATHL
];
22123 int has_fullname
= 0;
22125 int has_shortname
= 0;
22129 /* ":p" - full path/file_name */
22130 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
22134 valid
|= VALID_PATH
;
22137 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22138 if ((*fnamep
)[0] == '~'
22139 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22140 && ((*fnamep
)[1] == '/'
22141 # ifdef BACKSLASH_IN_FILENAME
22142 || (*fnamep
)[1] == '\\'
22144 || (*fnamep
)[1] == NUL
)
22149 *fnamep
= expand_env_save(*fnamep
);
22150 vim_free(*bufp
); /* free any allocated file name */
22152 if (*fnamep
== NULL
)
22156 /* When "/." or "/.." is used: force expansion to get rid of it. */
22157 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
22159 if (vim_ispathsep(*p
)
22162 || vim_ispathsep(p
[2])
22164 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
22168 /* FullName_save() is slow, don't use it when not needed. */
22169 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
22171 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
22172 vim_free(*bufp
); /* free any allocated file name */
22174 if (*fnamep
== NULL
)
22178 /* Append a path separator to a directory. */
22179 if (mch_isdir(*fnamep
))
22181 /* Make room for one or two extra characters. */
22182 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
22183 vim_free(*bufp
); /* free any allocated file name */
22185 if (*fnamep
== NULL
)
22187 add_pathsep(*fnamep
);
22191 /* ":." - path relative to the current directory */
22192 /* ":~" - path relative to the home directory */
22193 /* ":8" - shortname path - postponed till after */
22194 while (src
[*usedlen
] == ':'
22195 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
22201 has_shortname
= 1; /* Postpone this. */
22206 /* Need full path first (use expand_env() to remove a "~/") */
22209 if (c
== '.' && **fnamep
== '~')
22210 p
= pbuf
= expand_env_save(*fnamep
);
22212 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
22223 mch_dirname(dirname
, MAXPATHL
);
22224 s
= shorten_fname(p
, dirname
);
22230 vim_free(*bufp
); /* free any allocated file name */
22238 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
22239 /* Only replace it when it starts with '~' */
22240 if (*dirname
== '~')
22242 s
= vim_strsave(dirname
);
22255 tail
= gettail(*fnamep
);
22256 *fnamelen
= (int)STRLEN(*fnamep
);
22258 /* ":h" - head, remove "/file_name", can be repeated */
22259 /* Don't remove the first "/" or "c:\" */
22260 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
22262 valid
|= VALID_HEAD
;
22264 s
= get_past_head(*fnamep
);
22265 while (tail
> s
&& after_pathsep(s
, tail
))
22266 mb_ptr_back(*fnamep
, tail
);
22267 *fnamelen
= (int)(tail
- *fnamep
);
22270 *fnamelen
+= 1; /* the path separator is part of the path */
22272 if (*fnamelen
== 0)
22274 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22275 p
= vim_strsave((char_u
*)".");
22279 *bufp
= *fnamep
= tail
= p
;
22284 while (tail
> s
&& !after_pathsep(s
, tail
))
22285 mb_ptr_back(*fnamep
, tail
);
22289 /* ":8" - shortname */
22290 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
22299 /* Check shortname after we have done 'heads' and before we do 'tails'
22304 /* Copy the string if it is shortened by :h */
22305 if (*fnamelen
< (int)STRLEN(*fnamep
))
22307 p
= vim_strnsave(*fnamep
, *fnamelen
);
22311 *bufp
= *fnamep
= p
;
22314 /* Split into two implementations - makes it easier. First is where
22315 * there isn't a full name already, second is where there is.
22317 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
22319 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == FAIL
)
22326 /* Simple case, already have the full-name
22327 * Nearly always shorter, so try first time. */
22329 if (get_short_pathname(fnamep
, bufp
, &l
) == FAIL
)
22334 /* Couldn't find the filename.. search the paths.
22337 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == FAIL
)
22343 #endif /* WIN3264 */
22345 /* ":t" - tail, just the basename */
22346 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
22349 *fnamelen
-= (int)(tail
- *fnamep
);
22353 /* ":e" - extension, can be repeated */
22354 /* ":r" - root, without extension, can be repeated */
22355 while (src
[*usedlen
] == ':'
22356 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
22358 /* find a '.' in the tail:
22359 * - for second :e: before the current fname
22360 * - otherwise: The last '.'
22362 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
22365 s
= *fnamep
+ *fnamelen
- 1;
22366 for ( ; s
> tail
; --s
)
22369 if (src
[*usedlen
+ 1] == 'e') /* :e */
22373 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
22376 /* cut version from the extension */
22377 s
= *fnamep
+ *fnamelen
- 1;
22378 for ( ; s
> *fnamep
; --s
)
22382 *fnamelen
= s
- *fnamep
;
22385 else if (*fnamep
<= tail
)
22390 if (s
> tail
) /* remove one extension */
22391 *fnamelen
= (int)(s
- *fnamep
);
22396 /* ":s?pat?foo?" - substitute */
22397 /* ":gs?pat?foo?" - global substitute */
22398 if (src
[*usedlen
] == ':'
22399 && (src
[*usedlen
+ 1] == 's'
22400 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
22409 flags
= (char_u
*)"";
22410 s
= src
+ *usedlen
+ 2;
22411 if (src
[*usedlen
+ 1] == 'g')
22413 flags
= (char_u
*)"g";
22420 /* find end of pattern */
22421 p
= vim_strchr(s
, sep
);
22424 pat
= vim_strnsave(s
, (int)(p
- s
));
22428 /* find end of substitution */
22429 p
= vim_strchr(s
, sep
);
22432 sub
= vim_strnsave(s
, (int)(p
- s
));
22433 str
= vim_strnsave(*fnamep
, *fnamelen
);
22434 if (sub
!= NULL
&& str
!= NULL
)
22436 *usedlen
= (int)(p
+ 1 - src
);
22437 s
= do_string_sub(str
, pat
, sub
, flags
);
22441 *fnamelen
= (int)STRLEN(s
);
22453 /* after using ":s", repeat all the modifiers */
22463 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22464 * "flags" can be "g" to do a global substitute.
22465 * Returns an allocated string, NULL for error.
22468 do_string_sub(str
, pat
, sub
, flags
)
22475 regmatch_T regmatch
;
22483 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22485 p_cpo
= (char_u
*)"";
22487 ga_init2(&ga
, 1, 200);
22489 do_all
= (flags
[0] == 'g');
22491 regmatch
.rm_ic
= p_ic
;
22492 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
22493 if (regmatch
.regprog
!= NULL
)
22496 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
22499 * Get some space for a temporary buffer to do the substitution
22500 * into. It will contain:
22501 * - The text up to where the match is.
22502 * - The substituted text.
22503 * - The text after the match.
22505 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
22506 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
22507 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
22513 /* copy the text up to where the match is */
22514 i
= (int)(regmatch
.startp
[0] - tail
);
22515 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
22516 /* add the substituted text */
22517 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
22518 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
22519 ga
.ga_len
+= i
+ sublen
- 1;
22520 /* avoid getting stuck on a match with an empty string */
22521 if (tail
== regmatch
.endp
[0])
22525 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
22530 tail
= regmatch
.endp
[0];
22538 if (ga
.ga_data
!= NULL
)
22539 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
22541 vim_free(regmatch
.regprog
);
22544 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
22551 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */