1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * eval.c: Expression evaluation.
13 #if defined(MSDOS) || defined(MSWIN)
14 # include "vimio.h" /* for mch_open(), must be before vim.h */
19 #if defined(FEAT_EVAL) || defined(PROTO)
22 # include <time.h> /* for strftime() */
26 # include <time.h> /* for time_t */
29 #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
33 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
36 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
38 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 static dictitem_T dumdi
;
43 #define DI2HIKEY(di) ((di)->di_key)
44 #define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
45 #define HI2DI(hi) HIKEY2DI((hi)->hi_key)
48 * Structure returned by get_lval() and used by set_var_lval().
50 * "name" points to the variable name.
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
71 * "tv" points to the Dictionary typval_T
72 * "newkey" is the key for the new item.
76 char_u
*ll_name
; /* start of variable name (can be NULL) */
77 char_u
*ll_exp_name
; /* NULL or expanded name in allocated memory. */
78 typval_T
*ll_tv
; /* Typeval of item being used. If "newkey"
79 isn't NULL it's the Dict to which to add
81 listitem_T
*ll_li
; /* The list item or NULL. */
82 list_T
*ll_list
; /* The list or NULL. */
83 int ll_range
; /* TRUE when a [i:j] range was used */
84 long ll_n1
; /* First index for list */
85 long ll_n2
; /* Second index for list range */
86 int ll_empty2
; /* Second index is empty: [i:] */
87 dict_T
*ll_dict
; /* The Dictionary or NULL */
88 dictitem_T
*ll_di
; /* The dictitem or NULL */
89 char_u
*ll_newkey
; /* New key for Dict in alloc. mem or NULL. */
93 static char *e_letunexp
= N_("E18: Unexpected characters in :let");
94 static char *e_listidx
= N_("E684: list index out of range: %ld");
95 static char *e_undefvar
= N_("E121: Undefined variable: %s");
96 static char *e_missbrac
= N_("E111: Missing ']'");
97 static char *e_listarg
= N_("E686: Argument of %s must be a List");
98 static char *e_listdictarg
= N_("E712: Argument of %s must be a List or Dictionary");
99 static char *e_emptykey
= N_("E713: Cannot use empty key for Dictionary");
100 static char *e_listreq
= N_("E714: List required");
101 static char *e_dictreq
= N_("E715: Dictionary required");
102 static char *e_toomanyarg
= N_("E118: Too many arguments for function: %s");
103 static char *e_dictkey
= N_("E716: Key not present in Dictionary: %s");
104 static char *e_funcexts
= N_("E122: Function %s already exists, add ! to replace it");
105 static char *e_funcdict
= N_("E717: Dictionary entry already exists");
106 static char *e_funcref
= N_("E718: Funcref required");
107 static char *e_dictrange
= N_("E719: Cannot use [:] with a Dictionary");
108 static char *e_letwrong
= N_("E734: Wrong variable type for %s=");
109 static char *e_nofunc
= N_("E130: Unknown function: %s");
110 static char *e_illvar
= N_("E461: Illegal variable name: %s");
113 * All user-defined global variables are stored in dictionary "globvardict".
114 * "globvars_var" is the variable that is used for "g:".
116 static dict_T globvardict
;
117 static dictitem_T globvars_var
;
118 #define globvarht globvardict.dv_hashtab
121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
124 static hashtab_T compat_hashtab
;
127 * When recursively copying lists and dicts we need to remember which ones we
128 * have done to avoid endless recursiveness. This unique ID is used for that.
130 static int current_copyID
= 0;
133 * Array to hold the hashtab with variables local to each sourced script.
134 * Each item holds a variable (nameless) that points to the dict_T.
142 static garray_T ga_scripts
= {0, 0, sizeof(scriptvar_T
), 4, NULL
};
143 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
144 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
146 static int echo_attr
= 0; /* attributes used for ":echo" */
148 /* Values for trans_function_name() argument: */
149 #define TFN_INT 1 /* internal function name OK */
150 #define TFN_QUIET 2 /* no error messages */
153 * Structure to hold info for a user function.
155 typedef struct ufunc ufunc_T
;
159 int uf_varargs
; /* variable nr of arguments */
161 int uf_calls
; /* nr of active calls */
162 garray_T uf_args
; /* arguments */
163 garray_T uf_lines
; /* function lines */
165 int uf_profiling
; /* TRUE when func is being profiled */
166 /* profiling the function as a whole */
167 int uf_tm_count
; /* nr of calls */
168 proftime_T uf_tm_total
; /* time spent in function + children */
169 proftime_T uf_tm_self
; /* time spent in function itself */
170 proftime_T uf_tm_children
; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count
; /* nr of times line was executed */
173 proftime_T
*uf_tml_total
; /* time spent in a line + children */
174 proftime_T
*uf_tml_self
; /* time spent in a line itself */
175 proftime_T uf_tml_start
; /* start time for current line */
176 proftime_T uf_tml_children
; /* time spent in children for this line */
177 proftime_T uf_tml_wait
; /* start wait time for current line */
178 int uf_tml_idx
; /* index of line being timed; -1 if none */
179 int uf_tml_execed
; /* line being timed was executed */
181 scid_T uf_script_ID
; /* ID of script where function was defined,
182 used for s: variables */
183 int uf_refcount
; /* for numbered function: reference count */
184 char_u uf_name
[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
190 #define FC_ABORT 1 /* abort function on error */
191 #define FC_RANGE 2 /* function accepts range */
192 #define FC_DICT 4 /* Dict function, uses "self" */
195 * All user-defined functions are found in this hashtable.
197 static hashtab_T func_hashtab
;
199 /* The names of packages that once were loaded are remembered. */
200 static garray_T ga_loaded
= {0, 0, sizeof(char_u
*), 4, NULL
};
202 /* list heads for garbage collection */
203 static dict_T
*first_dict
= NULL
; /* list of all dicts */
204 static list_T
*first_list
= NULL
; /* list of all lists */
206 /* From user function to hashitem and back. */
207 static ufunc_T dumuf
;
208 #define UF2HIKEY(fp) ((fp)->uf_name)
209 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
210 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
212 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
213 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
215 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
216 #define VAR_SHORT_LEN 20 /* short variable name length */
217 #define FIXVAR_CNT 12 /* number of fixed variables */
219 /* structure to hold info for a function that is currently being executed. */
220 typedef struct funccall_S funccall_T
;
224 ufunc_T
*func
; /* function being called */
225 int linenr
; /* next line to be executed */
226 int returned
; /* ":return" used */
227 struct /* fixed variables for arguments */
229 dictitem_T var
; /* variable (without room for name) */
230 char_u room
[VAR_SHORT_LEN
]; /* room for the name */
231 } fixvar
[FIXVAR_CNT
];
232 dict_T l_vars
; /* l: local function variables */
233 dictitem_T l_vars_var
; /* variable for l: scope */
234 dict_T l_avars
; /* a: argument variables */
235 dictitem_T l_avars_var
; /* variable for a: scope */
236 list_T l_varlist
; /* list for a:000 */
237 listitem_T l_listitems
[MAX_FUNC_ARGS
]; /* listitems for a:000 */
238 typval_T
*rettv
; /* return value */
239 linenr_T breakpoint
; /* next line with breakpoint or zero */
240 int dbg_tick
; /* debug_tick when breakpoint was set */
241 int level
; /* top nesting level of executed function */
243 proftime_T prof_child
; /* time spent in a child */
245 funccall_T
*caller
; /* calling function or NULL */
249 * Info used by a ":for" loop.
253 int fi_semicolon
; /* TRUE if ending in '; var]' */
254 int fi_varcount
; /* nr of variables in the list */
255 listwatch_T fi_lw
; /* keep an eye on the item used. */
256 list_T
*fi_list
; /* list being used */
260 * Struct used by trans_function_name()
264 dict_T
*fd_dict
; /* Dictionary used */
265 char_u
*fd_newkey
; /* new key in "dict" in allocated memory */
266 dictitem_T
*fd_di
; /* Dictionary item used */
271 * Array to hold the value of v: variables.
272 * The value is in a dictitem, so that it can also be used in the v: scope.
273 * The reason to use this table anyway is for very quick access to the
274 * variables with the VV_ defines.
278 /* values for vv_flags: */
279 #define VV_COMPAT 1 /* compatible, also used without "v:" */
280 #define VV_RO 2 /* read-only */
281 #define VV_RO_SBX 4 /* read-only in the sandbox */
283 #define VV_NAME(s, t) s, {{t}}, {0}
287 char *vv_name
; /* name of variable, without v: */
288 dictitem_T vv_di
; /* value and name for key */
289 char vv_filler
[16]; /* space for LONGEST name below!!! */
290 char vv_flags
; /* VV_COMPAT, VV_RO, VV_RO_SBX */
294 * The order here must match the VV_ defines in vim.h!
295 * Initializing a union does not work, leave tv.vval empty to get zero's.
297 {VV_NAME("count", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
298 {VV_NAME("count1", VAR_NUMBER
), VV_RO
},
299 {VV_NAME("prevcount", VAR_NUMBER
), VV_RO
},
300 {VV_NAME("errmsg", VAR_STRING
), VV_COMPAT
},
301 {VV_NAME("warningmsg", VAR_STRING
), 0},
302 {VV_NAME("statusmsg", VAR_STRING
), 0},
303 {VV_NAME("shell_error", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
304 {VV_NAME("this_session", VAR_STRING
), VV_COMPAT
},
305 {VV_NAME("version", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
306 {VV_NAME("lnum", VAR_NUMBER
), VV_RO_SBX
},
307 {VV_NAME("termresponse", VAR_STRING
), VV_RO
},
308 {VV_NAME("fname", VAR_STRING
), VV_RO
},
309 {VV_NAME("lang", VAR_STRING
), VV_RO
},
310 {VV_NAME("lc_time", VAR_STRING
), VV_RO
},
311 {VV_NAME("ctype", VAR_STRING
), VV_RO
},
312 {VV_NAME("charconvert_from", VAR_STRING
), VV_RO
},
313 {VV_NAME("charconvert_to", VAR_STRING
), VV_RO
},
314 {VV_NAME("fname_in", VAR_STRING
), VV_RO
},
315 {VV_NAME("fname_out", VAR_STRING
), VV_RO
},
316 {VV_NAME("fname_new", VAR_STRING
), VV_RO
},
317 {VV_NAME("fname_diff", VAR_STRING
), VV_RO
},
318 {VV_NAME("cmdarg", VAR_STRING
), VV_RO
},
319 {VV_NAME("foldstart", VAR_NUMBER
), VV_RO_SBX
},
320 {VV_NAME("foldend", VAR_NUMBER
), VV_RO_SBX
},
321 {VV_NAME("folddashes", VAR_STRING
), VV_RO_SBX
},
322 {VV_NAME("foldlevel", VAR_NUMBER
), VV_RO_SBX
},
323 {VV_NAME("progname", VAR_STRING
), VV_RO
},
324 {VV_NAME("servername", VAR_STRING
), VV_RO
},
325 {VV_NAME("dying", VAR_NUMBER
), VV_RO
},
326 {VV_NAME("exception", VAR_STRING
), VV_RO
},
327 {VV_NAME("throwpoint", VAR_STRING
), VV_RO
},
328 {VV_NAME("register", VAR_STRING
), VV_RO
},
329 {VV_NAME("cmdbang", VAR_NUMBER
), VV_RO
},
330 {VV_NAME("insertmode", VAR_STRING
), VV_RO
},
331 {VV_NAME("val", VAR_UNKNOWN
), VV_RO
},
332 {VV_NAME("key", VAR_UNKNOWN
), VV_RO
},
333 {VV_NAME("profiling", VAR_NUMBER
), VV_RO
},
334 {VV_NAME("fcs_reason", VAR_STRING
), VV_RO
},
335 {VV_NAME("fcs_choice", VAR_STRING
), 0},
336 {VV_NAME("beval_bufnr", VAR_NUMBER
), VV_RO
},
337 {VV_NAME("beval_winnr", VAR_NUMBER
), VV_RO
},
338 {VV_NAME("beval_lnum", VAR_NUMBER
), VV_RO
},
339 {VV_NAME("beval_col", VAR_NUMBER
), VV_RO
},
340 {VV_NAME("beval_text", VAR_STRING
), VV_RO
},
341 {VV_NAME("scrollstart", VAR_STRING
), 0},
342 {VV_NAME("swapname", VAR_STRING
), VV_RO
},
343 {VV_NAME("swapchoice", VAR_STRING
), 0},
344 {VV_NAME("swapcommand", VAR_STRING
), VV_RO
},
345 {VV_NAME("char", VAR_STRING
), VV_RO
},
346 {VV_NAME("mouse_win", VAR_NUMBER
), 0},
347 {VV_NAME("mouse_lnum", VAR_NUMBER
), 0},
348 {VV_NAME("mouse_col", VAR_NUMBER
), 0},
349 {VV_NAME("operator", VAR_STRING
), VV_RO
},
350 {VV_NAME("searchforward", VAR_NUMBER
), 0},
354 #define vv_type vv_di.di_tv.v_type
355 #define vv_nr vv_di.di_tv.vval.v_number
356 #define vv_float vv_di.di_tv.vval.v_float
357 #define vv_str vv_di.di_tv.vval.v_string
358 #define vv_tv vv_di.di_tv
361 * The v: variables are stored in dictionary "vimvardict".
362 * "vimvars_var" is the variable that is used for the "l:" scope.
364 static dict_T vimvardict
;
365 static dictitem_T vimvars_var
;
366 #define vimvarht vimvardict.dv_hashtab
368 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
369 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
370 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
371 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
373 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
374 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
375 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
376 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
, int *first
));
377 static void list_glob_vars
__ARGS((int *first
));
378 static void list_buf_vars
__ARGS((int *first
));
379 static void list_win_vars
__ARGS((int *first
));
381 static void list_tab_vars
__ARGS((int *first
));
383 static void list_vim_vars
__ARGS((int *first
));
384 static void list_script_vars
__ARGS((int *first
));
385 static void list_func_vars
__ARGS((int *first
));
386 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
, int *first
));
387 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
388 static int check_changedtick
__ARGS((char_u
*arg
));
389 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
390 static void clear_lval
__ARGS((lval_T
*lp
));
391 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
392 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
393 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
394 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
395 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
396 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
397 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
398 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
399 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
400 static int tv_islocked
__ARGS((typval_T
*tv
));
402 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
403 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
404 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
405 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
406 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
407 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
408 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
409 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
411 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
412 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
413 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
414 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
415 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
416 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
417 static listitem_T
*listitem_alloc
__ARGS((void));
418 static void listitem_free
__ARGS((listitem_T
*item
));
419 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
420 static long list_len
__ARGS((list_T
*l
));
421 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
422 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
423 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
424 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
425 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
426 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
427 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
428 static int list_append_tv
__ARGS((list_T
*l
, typval_T
*tv
));
429 static int list_append_string
__ARGS((list_T
*l
, char_u
*str
, int len
));
430 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
431 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
432 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
433 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
434 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
435 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
436 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
437 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
438 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
439 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
440 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
441 static void dict_unref
__ARGS((dict_T
*d
));
442 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
443 static dictitem_T
*dictitem_alloc
__ARGS((char_u
*key
));
444 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
445 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
446 static void dictitem_free
__ARGS((dictitem_T
*item
));
447 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
448 static int dict_add
__ARGS((dict_T
*d
, dictitem_T
*item
));
449 static long dict_len
__ARGS((dict_T
*d
));
450 static dictitem_T
*dict_find
__ARGS((dict_T
*d
, char_u
*key
, int len
));
451 static char_u
*dict2string
__ARGS((typval_T
*tv
, int copyID
));
452 static int get_dict_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
453 static char_u
*echo_string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
454 static char_u
*tv2string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
455 static char_u
*string_quote
__ARGS((char_u
*str
, int function
));
457 static int string2float
__ARGS((char_u
*text
, float_T
*value
));
459 static int get_env_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
460 static int find_internal_func
__ARGS((char_u
*name
));
461 static char_u
*deref_func_name
__ARGS((char_u
*name
, int *lenp
));
462 static int get_func_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, char_u
**arg
, linenr_T firstline
, linenr_T lastline
, int *doesrange
, int evaluate
, dict_T
*selfdict
));
463 static int call_func
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int argcount
, typval_T
*argvars
, linenr_T firstline
, linenr_T lastline
, int *doesrange
, int evaluate
, dict_T
*selfdict
));
464 static void emsg_funcname
__ARGS((char *ermsg
, char_u
*name
));
467 static void f_abs
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
469 static void f_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
470 static void f_append
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
471 static void f_argc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
472 static void f_argidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
473 static void f_argv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
475 static void f_atan
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
477 static void f_browse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
478 static void f_browsedir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
479 static void f_bufexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
480 static void f_buflisted
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
481 static void f_bufloaded
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
482 static void f_bufname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
483 static void f_bufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
484 static void f_bufwinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
485 static void f_byte2line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
486 static void f_byteidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
487 static void f_call
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
489 static void f_ceil
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
491 static void f_changenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
492 static void f_char2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
493 static void f_cindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
494 static void f_clearmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
495 static void f_col
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
496 #if defined(FEAT_INS_EXPAND)
497 static void f_complete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
498 static void f_complete_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
499 static void f_complete_check
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
501 static void f_confirm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
502 static void f_copy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
504 static void f_cos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
506 static void f_count
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
507 static void f_cscope_connection
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
508 static void f_cursor
__ARGS((typval_T
*argsvars
, typval_T
*rettv
));
509 static void f_deepcopy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
510 static void f_delete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
511 static void f_did_filetype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
512 static void f_diff_filler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
513 static void f_diff_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
514 static void f_empty
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
515 static void f_escape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
516 static void f_eval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
517 static void f_eventhandler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
518 static void f_executable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
519 static void f_exists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
520 static void f_expand
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
521 static void f_extend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
522 static void f_feedkeys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
523 static void f_filereadable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
524 static void f_filewritable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
525 static void f_filter
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
526 static void f_finddir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
527 static void f_findfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
529 static void f_float2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
530 static void f_floor
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
532 static void f_fnameescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
533 static void f_fnamemodify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
534 static void f_foldclosed
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
535 static void f_foldclosedend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
536 static void f_foldlevel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
537 static void f_foldtext
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
538 static void f_foldtextresult
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
539 static void f_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
540 static void f_function
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
541 static void f_garbagecollect
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
542 static void f_get
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
543 static void f_getbufline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
544 static void f_getbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
545 static void f_getchar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
546 static void f_getcharmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
547 static void f_getcmdline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
548 static void f_getcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
549 static void f_getcmdtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
550 static void f_getcwd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
551 static void f_getfontname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
552 static void f_getfperm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
553 static void f_getfsize
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
554 static void f_getftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
555 static void f_getftype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
556 static void f_getline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
557 static void f_getmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
558 static void f_getpid
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
559 static void f_getpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
560 static void f_getqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
561 static void f_getreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
562 static void f_getregtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
563 static void f_gettabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
564 static void f_getwinposx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
565 static void f_getwinposy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
566 static void f_getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
567 static void f_glob
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
568 static void f_globpath
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
569 static void f_has
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
570 static void f_has_key
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
571 static void f_haslocaldir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
572 static void f_hasmapto
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
573 static void f_histadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
574 static void f_histdel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
575 static void f_histget
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
576 static void f_histnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
577 static void f_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
578 static void f_hlexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
579 static void f_hostname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
580 static void f_iconv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
581 static void f_indent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
582 static void f_index
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
583 static void f_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
584 static void f_inputdialog
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
585 static void f_inputlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
586 static void f_inputrestore
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
587 static void f_inputsave
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
588 static void f_inputsecret
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
589 static void f_insert
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
590 static void f_isdirectory
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
591 static void f_islocked
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
592 static void f_items
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
593 static void f_join
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
594 static void f_keys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
595 static void f_last_buffer_nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
596 static void f_len
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
597 static void f_libcall
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
598 static void f_libcallnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
599 static void f_line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
600 static void f_line2byte
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
601 static void f_lispindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
602 static void f_localtime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
604 static void f_log10
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
606 static void f_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
607 static void f_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
608 static void f_mapcheck
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
609 static void f_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
610 static void f_matchadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
611 static void f_matcharg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
612 static void f_matchdelete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
613 static void f_matchend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
614 static void f_matchlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
615 static void f_matchstr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
616 static void f_max
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
617 static void f_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
619 static void f_mkdir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
621 static void f_mode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
622 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
623 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
624 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
626 static void f_pow
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
628 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
629 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
630 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
631 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
632 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
633 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
634 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
635 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
636 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
637 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
638 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
639 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
640 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
641 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
642 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
643 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
644 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_round
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
648 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
650 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
652 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
654 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
656 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_setmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
665 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
668 static void f_sin
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
670 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
672 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
673 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
674 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
676 static void f_sqrt
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
677 static void f_str2float
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
679 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
681 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
683 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
684 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
685 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
686 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
687 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
688 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
689 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
690 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
691 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
692 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
693 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
694 static void f_synstack
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
695 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
696 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
697 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
698 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
699 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
700 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
701 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
702 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
703 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
704 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
705 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
707 static void f_trunc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
709 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
710 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
711 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
712 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
713 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
714 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
715 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
716 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
717 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
718 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
719 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
720 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
721 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
722 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
724 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
725 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int dollar_lnum
, int *fnum
));
726 static int get_env_len
__ARGS((char_u
**arg
));
727 static int get_id_len
__ARGS((char_u
**arg
));
728 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
729 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
730 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
731 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
733 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
734 static int eval_isnamec
__ARGS((int c
));
735 static int eval_isnamec1
__ARGS((int c
));
736 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
737 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
738 static typval_T
*alloc_tv
__ARGS((void));
739 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
740 static void init_tv
__ARGS((typval_T
*varp
));
741 static long get_tv_number
__ARGS((typval_T
*varp
));
742 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
743 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
744 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
745 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
746 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
747 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
748 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
749 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
750 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
751 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
752 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
, int *first
));
753 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
, int *first
));
754 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
755 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
756 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
757 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
758 static void copy_tv
__ARGS((typval_T
*from
, typval_T
*to
));
759 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
760 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
761 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
762 static int eval_fname_script
__ARGS((char_u
*p
));
763 static int eval_fname_sid
__ARGS((char_u
*p
));
764 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
765 static ufunc_T
*find_func
__ARGS((char_u
*name
));
766 static int function_exists
__ARGS((char_u
*name
));
767 static int builtin_function
__ARGS((char_u
*name
));
769 static void func_do_profile
__ARGS((ufunc_T
*fp
));
770 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
771 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
776 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
781 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
783 static int script_autoload
__ARGS((char_u
*name
, int reload
));
784 static char_u
*autoload_name
__ARGS((char_u
*name
));
785 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
786 static void func_free
__ARGS((ufunc_T
*fp
));
787 static void func_unref
__ARGS((char_u
*name
));
788 static void func_ref
__ARGS((char_u
*name
));
789 static void call_user_func
__ARGS((ufunc_T
*fp
, int argcount
, typval_T
*argvars
, typval_T
*rettv
, linenr_T firstline
, linenr_T lastline
, dict_T
*selfdict
));
790 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
791 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
792 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
793 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
794 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
795 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
797 /* Character used as separated in autoload function/variable names. */
798 #define AUTOLOAD_CHAR '#'
801 * Initialize the global and v: variables.
809 init_var_dict(&globvardict
, &globvars_var
);
810 init_var_dict(&vimvardict
, &vimvars_var
);
811 hash_init(&compat_hashtab
);
812 hash_init(&func_hashtab
);
814 for (i
= 0; i
< VV_LEN
; ++i
)
817 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
818 if (p
->vv_flags
& VV_RO
)
819 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
820 else if (p
->vv_flags
& VV_RO_SBX
)
821 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
823 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
825 /* add to v: scope dict, unless the value is not always available */
826 if (p
->vv_type
!= VAR_UNKNOWN
)
827 hash_add(&vimvarht
, p
->vv_di
.di_key
);
828 if (p
->vv_flags
& VV_COMPAT
)
829 /* add to compat scope dict */
830 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
832 set_vim_var_nr(VV_SEARCHFORWARD
, 1L);
835 #if defined(EXITFREE) || defined(PROTO)
842 for (i
= 0; i
< VV_LEN
; ++i
)
845 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
847 vim_free(p
->vv_di
.di_tv
.vval
.v_string
);
848 p
->vv_di
.di_tv
.vval
.v_string
= NULL
;
851 hash_clear(&vimvarht
);
852 hash_clear(&compat_hashtab
);
854 /* script-local variables */
855 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
856 vars_clear(&SCRIPT_VARS(i
));
857 ga_clear(&ga_scripts
);
860 /* global variables */
861 vars_clear(&globvarht
);
863 /* autoloaded script names */
864 ga_clear_strings(&ga_loaded
);
866 /* unreferenced lists and dicts */
867 (void)garbage_collect();
870 free_all_functions();
871 hash_clear(&func_hashtab
);
876 * Return the name of the executed function.
882 return ((funccall_T
*)cookie
)->func
->uf_name
;
886 * Return the address holding the next breakpoint line for a funccall cookie.
889 func_breakpoint(cookie
)
892 return &((funccall_T
*)cookie
)->breakpoint
;
896 * Return the address holding the debug tick for a funccall cookie.
899 func_dbg_tick(cookie
)
902 return &((funccall_T
*)cookie
)->dbg_tick
;
906 * Return the nesting level for a funccall cookie.
912 return ((funccall_T
*)cookie
)->level
;
915 /* pointer to funccal for currently active function */
916 funccall_T
*current_funccal
= NULL
;
919 * Return TRUE when a function was ended by a ":return" command.
922 current_func_returned()
924 return current_funccal
->returned
;
929 * Set an internal variable to a string value. Creates the variable if it does
933 set_internal_string_var(name
, value
)
940 val
= vim_strsave(value
);
943 tvp
= alloc_string_tv(val
);
946 set_var(name
, tvp
, FALSE
);
952 static lval_T
*redir_lval
= NULL
;
953 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
954 static char_u
*redir_endp
= NULL
;
955 static char_u
*redir_varname
= NULL
;
958 * Start recording command output to a variable
959 * Returns OK if successfully completed the setup. FAIL otherwise.
962 var_redir_start(name
, append
)
964 int append
; /* append to an existing variable */
970 /* Make sure a valid variable name is specified */
971 if (!eval_isnamec1(*name
))
977 redir_varname
= vim_strsave(name
);
978 if (redir_varname
== NULL
)
981 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
982 if (redir_lval
== NULL
)
988 /* The output is stored in growarray "redir_ga" until redirection ends. */
989 ga_init2(&redir_ga
, (int)sizeof(char), 500);
991 /* Parse the variable name (can be a dict or list entry). */
992 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
994 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
996 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
997 /* Trailing characters are present after the variable name */
1005 /* check if we can write to the variable: set it to or append an empty
1007 save_emsg
= did_emsg
;
1009 tv
.v_type
= VAR_STRING
;
1010 tv
.vval
.v_string
= (char_u
*)"";
1012 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
1014 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
1016 did_emsg
|= save_emsg
;
1022 if (redir_lval
->ll_newkey
!= NULL
)
1024 /* Dictionary item was created, don't do it again. */
1025 vim_free(redir_lval
->ll_newkey
);
1026 redir_lval
->ll_newkey
= NULL
;
1033 * Append "value[value_len]" to the variable set by var_redir_start().
1034 * The actual appending is postponed until redirection ends, because the value
1035 * appended may in fact be the string we write to, changing it may cause freed
1036 * memory to be used:
1042 var_redir_str(value
, value_len
)
1048 if (redir_lval
== NULL
)
1051 if (value_len
== -1)
1052 len
= (int)STRLEN(value
); /* Append the entire string */
1054 len
= value_len
; /* Append only "value_len" characters */
1056 if (ga_grow(&redir_ga
, len
) == OK
)
1058 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1059 redir_ga
.ga_len
+= len
;
1066 * Stop redirecting command output to a variable.
1073 if (redir_lval
!= NULL
)
1075 /* Append the trailing NUL. */
1076 ga_append(&redir_ga
, NUL
);
1078 /* Assign the text to the variable. */
1079 tv
.v_type
= VAR_STRING
;
1080 tv
.vval
.v_string
= redir_ga
.ga_data
;
1081 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1082 vim_free(tv
.vval
.v_string
);
1084 clear_lval(redir_lval
);
1085 vim_free(redir_lval
);
1088 vim_free(redir_varname
);
1089 redir_varname
= NULL
;
1092 # if defined(FEAT_MBYTE) || defined(PROTO)
1094 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1102 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1103 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1104 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1105 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1106 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1108 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1109 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1110 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1111 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1119 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1121 eval_printexpr(fname
, args
)
1127 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1128 set_vim_var_string(VV_CMDARG
, args
, -1);
1129 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1131 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1132 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1143 # if defined(FEAT_DIFF) || defined(PROTO)
1145 eval_diff(origfile
, newfile
, outfile
)
1152 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1153 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1154 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1155 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1156 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1157 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1158 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1162 eval_patch(origfile
, difffile
, outfile
)
1169 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1170 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1171 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1172 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1173 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1174 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1175 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1180 * Top level evaluation function, returning a boolean.
1181 * Sets "error" to TRUE if there was an error.
1182 * Return TRUE or FALSE.
1185 eval_to_bool(arg
, error
, nextcmd
, skip
)
1189 int skip
; /* only parse, don't execute */
1196 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1203 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1214 * Top level evaluation function, returning a string. If "skip" is TRUE,
1215 * only parsing to "nextcmd" is done, without reporting errors. Return
1216 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1219 eval_to_string_skip(arg
, nextcmd
, skip
)
1222 int skip
; /* only parse, don't execute */
1229 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1233 retval
= vim_strsave(get_tv_string(&tv
));
1243 * Skip over an expression at "*pp".
1244 * Return FAIL for an error, OK otherwise.
1252 *pp
= skipwhite(*pp
);
1253 return eval1(pp
, &rettv
, FALSE
);
1257 * Top level evaluation function, returning a string.
1258 * Return pointer to allocated memory, or NULL for failure.
1261 eval_to_string(arg
, nextcmd
, dolist
)
1264 int dolist
; /* turn List into sequence of lines */
1270 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1274 if (dolist
&& tv
.v_type
== VAR_LIST
)
1276 ga_init2(&ga
, (int)sizeof(char), 80);
1277 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1278 ga_append(&ga
, NUL
);
1279 retval
= (char_u
*)ga
.ga_data
;
1282 retval
= vim_strsave(get_tv_string(&tv
));
1290 * Call eval_to_string() without using current local variables and using
1291 * textlock. When "use_sandbox" is TRUE use the sandbox.
1294 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1300 void *save_funccalp
;
1302 save_funccalp
= save_funccal();
1306 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1310 restore_funccal(save_funccalp
);
1315 * Top level evaluation function, returning a number.
1316 * Evaluates "expr" silently.
1317 * Returns -1 for an error.
1320 eval_to_number(expr
)
1325 char_u
*p
= skipwhite(expr
);
1329 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1333 retval
= get_tv_number_chk(&rettv
, NULL
);
1342 * Prepare v: variable "idx" to be used.
1343 * Save the current typeval in "save_tv".
1344 * When not used yet add the variable to the v: hashtable.
1347 prepare_vimvar(idx
, save_tv
)
1351 *save_tv
= vimvars
[idx
].vv_tv
;
1352 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1353 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1357 * Restore v: variable "idx" to typeval "save_tv".
1358 * When no longer defined, remove the variable from the v: hashtable.
1361 restore_vimvar(idx
, save_tv
)
1367 vimvars
[idx
].vv_tv
= *save_tv
;
1368 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1370 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1371 if (HASHITEM_EMPTY(hi
))
1372 EMSG2(_(e_intern2
), "restore_vimvar()");
1374 hash_remove(&vimvarht
, hi
);
1378 #if defined(FEAT_SPELL) || defined(PROTO)
1380 * Evaluate an expression to a list with suggestions.
1381 * For the "expr:" part of 'spellsuggest'.
1384 eval_spell_expr(badword
, expr
)
1390 list_T
*list
= NULL
;
1391 char_u
*p
= skipwhite(expr
);
1393 /* Set "v:val" to the bad word. */
1394 prepare_vimvar(VV_VAL
, &save_val
);
1395 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1396 vimvars
[VV_VAL
].vv_str
= badword
;
1400 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1402 if (rettv
.v_type
!= VAR_LIST
)
1405 list
= rettv
.vval
.v_list
;
1410 restore_vimvar(VV_VAL
, &save_val
);
1416 * "list" is supposed to contain two items: a word and a number. Return the
1417 * word in "pp" and the number as the return value.
1418 * Return -1 if anything isn't right.
1419 * Used to get the good word and score from the eval_spell_expr() result.
1422 get_spellword(list
, pp
)
1428 li
= list
->lv_first
;
1431 *pp
= get_tv_string(&li
->li_tv
);
1436 return get_tv_number(&li
->li_tv
);
1441 * Top level evaluation function.
1442 * Returns an allocated typval_T with the result.
1443 * Returns NULL when there is an error.
1446 eval_expr(arg
, nextcmd
)
1452 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1453 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1463 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1464 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1466 * Call some vimL function and return the result in "*rettv".
1467 * Uses argv[argc] for the function arguments. Only Number and String
1468 * arguments are currently supported.
1469 * Returns OK or FAIL.
1472 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1476 int safe
; /* use the sandbox */
1484 void *save_funccalp
= NULL
;
1487 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1488 if (argvars
== NULL
)
1491 for (i
= 0; i
< argc
; i
++)
1493 /* Pass a NULL or empty argument as an empty string */
1494 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1496 argvars
[i
].v_type
= VAR_STRING
;
1497 argvars
[i
].vval
.v_string
= (char_u
*)"";
1501 /* Recognize a number argument, the others must be strings. */
1502 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1503 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1505 argvars
[i
].v_type
= VAR_NUMBER
;
1506 argvars
[i
].vval
.v_number
= n
;
1510 argvars
[i
].v_type
= VAR_STRING
;
1511 argvars
[i
].vval
.v_string
= argv
[i
];
1517 save_funccalp
= save_funccal();
1521 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1522 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1523 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1524 &doesrange
, TRUE
, NULL
);
1528 restore_funccal(save_funccalp
);
1538 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1540 * Call vimL function "func" and return the result as a string.
1541 * Returns NULL when calling the function fails.
1542 * Uses argv[argc] for the function arguments.
1545 call_func_retstr(func
, argc
, argv
, safe
)
1549 int safe
; /* use the sandbox */
1554 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1557 retval
= vim_strsave(get_tv_string(&rettv
));
1563 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1565 * Call vimL function "func" and return the result as a number.
1566 * Returns -1 when calling the function fails.
1567 * Uses argv[argc] for the function arguments.
1570 call_func_retnr(func
, argc
, argv
, safe
)
1574 int safe
; /* use the sandbox */
1579 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1582 retval
= get_tv_number_chk(&rettv
, NULL
);
1589 * Call vimL function "func" and return the result as a list
1590 * Uses argv[argc] for the function arguments.
1593 call_func_retlist(func
, argc
, argv
, safe
)
1597 int safe
; /* use the sandbox */
1601 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1604 if (rettv
.v_type
!= VAR_LIST
)
1610 return rettv
.vval
.v_list
;
1616 * Save the current function call pointer, and set it to NULL.
1617 * Used when executing autocommands and for ":source".
1622 funccall_T
*fc
= current_funccal
;
1624 current_funccal
= NULL
;
1629 restore_funccal(vfc
)
1632 funccall_T
*fc
= (funccall_T
*)vfc
;
1634 current_funccal
= fc
;
1637 #if defined(FEAT_PROFILE) || defined(PROTO)
1639 * Prepare profiling for entering a child or something else that is not
1640 * counted for the script/function itself.
1641 * Should always be called in pair with prof_child_exit().
1644 prof_child_enter(tm
)
1645 proftime_T
*tm
; /* place to store waittime */
1647 funccall_T
*fc
= current_funccal
;
1649 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1650 profile_start(&fc
->prof_child
);
1651 script_prof_save(tm
);
1655 * Take care of time spent in a child.
1656 * Should always be called after prof_child_enter().
1660 proftime_T
*tm
; /* where waittime was stored */
1662 funccall_T
*fc
= current_funccal
;
1664 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1666 profile_end(&fc
->prof_child
);
1667 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1668 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1669 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1671 script_prof_restore(tm
);
1678 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1679 * it in "*cp". Doesn't give error messages.
1682 eval_foldexpr(arg
, cp
)
1689 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1697 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1701 /* If the result is a number, just return the number. */
1702 if (tv
.v_type
== VAR_NUMBER
)
1703 retval
= tv
.vval
.v_number
;
1704 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1708 /* If the result is a string, check if there is a non-digit before
1710 s
= tv
.vval
.v_string
;
1711 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1713 retval
= atol((char *)s
);
1727 * ":let" list all variable values
1728 * ":let var1 var2" list variable values
1729 * ":let var = expr" assignment command.
1730 * ":let var += expr" assignment command.
1731 * ":let var -= expr" assignment command.
1732 * ":let var .= expr" assignment command.
1733 * ":let [var1, var2] = expr" unpack list.
1739 char_u
*arg
= eap
->arg
;
1740 char_u
*expr
= NULL
;
1749 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1752 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1754 expr
= vim_strchr(argend
, '=');
1758 * ":let" without "=": list variables
1762 else if (!ends_excmd(*arg
))
1763 /* ":let var1 var2" */
1764 arg
= list_arg_vars(eap
, arg
, &first
);
1765 else if (!eap
->skip
)
1768 list_glob_vars(&first
);
1769 list_buf_vars(&first
);
1770 list_win_vars(&first
);
1772 list_tab_vars(&first
);
1774 list_script_vars(&first
);
1775 list_func_vars(&first
);
1776 list_vim_vars(&first
);
1778 eap
->nextcmd
= check_nextcmd(arg
);
1786 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1787 op
[0] = expr
[-1]; /* +=, -= or .= */
1789 expr
= skipwhite(expr
+ 1);
1793 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1802 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1810 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1811 * Handles both "var" with any type and "[var, var; var]" with a list type.
1812 * When "nextchars" is not NULL it points to a string with characters that
1813 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1815 * Returns OK or FAIL;
1818 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1821 int copy
; /* copy values from "tv", don't move */
1822 int semicolon
; /* from skip_var_list() */
1823 int var_count
; /* from skip_var_list() */
1826 char_u
*arg
= arg_start
;
1835 * ":let var = expr" or ":for var in list"
1837 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1843 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1845 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1852 if (semicolon
== 0 && var_count
< i
)
1854 EMSG(_("E687: Less targets than List items"));
1857 if (var_count
- semicolon
> i
)
1859 EMSG(_("E688: More targets than List items"));
1866 arg
= skipwhite(arg
+ 1);
1867 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1868 item
= item
->li_next
;
1872 arg
= skipwhite(arg
);
1875 /* Put the rest of the list (may be empty) in the var after ';'.
1876 * Create a new list for this. */
1880 while (item
!= NULL
)
1882 list_append_tv(l
, &item
->li_tv
);
1883 item
= item
->li_next
;
1886 ltv
.v_type
= VAR_LIST
;
1888 ltv
.vval
.v_list
= l
;
1891 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1892 (char_u
*)"]", nextchars
);
1898 else if (*arg
!= ',' && *arg
!= ']')
1900 EMSG2(_(e_intern2
), "ex_let_vars()");
1909 * Skip over assignable variable "var" or list of variables "[var, var]".
1910 * Used for ":let varvar = expr" and ":for varvar in expr".
1911 * For "[var, var]" increment "*var_count" for each variable.
1912 * for "[var, var; var]" set "semicolon".
1913 * Return NULL for an error.
1916 skip_var_list(arg
, var_count
, semicolon
)
1925 /* "[var, var]": find the matching ']'. */
1929 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1930 s
= skip_var_one(p
);
1933 EMSG2(_(e_invarg2
), p
);
1943 if (*semicolon
== 1)
1945 EMSG(_("Double ; in list of variables"));
1952 EMSG2(_(e_invarg2
), p
);
1959 return skip_var_one(arg
);
1963 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1970 if (*arg
== '@' && arg
[1] != NUL
)
1972 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
1973 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
1977 * List variables for hashtab "ht" with prefix "prefix".
1978 * If "empty" is TRUE also list NULL strings as empty strings.
1981 list_hashtable_vars(ht
, prefix
, empty
, first
)
1991 todo
= (int)ht
->ht_used
;
1992 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
1994 if (!HASHITEM_EMPTY(hi
))
1998 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
1999 || di
->di_tv
.vval
.v_string
!= NULL
)
2000 list_one_var(di
, prefix
, first
);
2006 * List global variables.
2009 list_glob_vars(first
)
2012 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
2016 * List buffer variables.
2019 list_buf_vars(first
)
2022 char_u numbuf
[NUMBUFLEN
];
2024 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
2027 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
2028 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
2033 * List window variables.
2036 list_win_vars(first
)
2039 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
2040 (char_u
*)"w:", TRUE
, first
);
2045 * List tab page variables.
2048 list_tab_vars(first
)
2051 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2052 (char_u
*)"t:", TRUE
, first
);
2057 * List Vim variables.
2060 list_vim_vars(first
)
2063 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2067 * List script-local variables, if there is a script.
2070 list_script_vars(first
)
2073 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2074 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2075 (char_u
*)"s:", FALSE
, first
);
2079 * List function variables, if there is a function.
2082 list_func_vars(first
)
2085 if (current_funccal
!= NULL
)
2086 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2087 (char_u
*)"l:", FALSE
, first
);
2091 * List variables in "arg".
2094 list_arg_vars(eap
, arg
, first
)
2107 while (!ends_excmd(*arg
) && !got_int
)
2109 if (error
|| eap
->skip
)
2111 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2112 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2115 EMSG(_(e_trailing
));
2121 /* get_name_len() takes care of expanding curly braces */
2122 name_start
= name
= arg
;
2123 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2126 /* This is mainly to keep test 49 working: when expanding
2127 * curly braces fails overrule the exception error message. */
2128 if (len
< 0 && !aborting())
2131 EMSG2(_(e_invarg2
), arg
);
2140 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2144 /* handle d.key, l[idx], f(expr) */
2146 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2150 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2154 case 'g': list_glob_vars(first
); break;
2155 case 'b': list_buf_vars(first
); break;
2156 case 'w': list_win_vars(first
); break;
2158 case 't': list_tab_vars(first
); break;
2160 case 'v': list_vim_vars(first
); break;
2161 case 's': list_script_vars(first
); break;
2162 case 'l': list_func_vars(first
); break;
2164 EMSG2(_("E738: Can't list variables for %s"), name
);
2169 char_u numbuf
[NUMBUFLEN
];
2174 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2177 list_one_var_a((char_u
*)"",
2178 arg
== arg_subsc
? name
: name_start
,
2180 s
== NULL
? (char_u
*)"" : s
,
2193 arg
= skipwhite(arg
);
2200 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2201 * Returns a pointer to the char just after the var name.
2202 * Returns NULL if there is an error.
2205 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2206 char_u
*arg
; /* points to variable name */
2207 typval_T
*tv
; /* value to assign to variable */
2208 int copy
; /* copy value from "tv" */
2209 char_u
*endchars
; /* valid chars after variable name or NULL */
2210 char_u
*op
; /* "+", "-", "." or NULL*/
2215 char_u
*arg_end
= NULL
;
2218 char_u
*tofree
= NULL
;
2221 * ":let $VAR = expr": Set environment variable.
2225 /* Find the end of the name. */
2228 len
= get_env_len(&arg
);
2230 EMSG2(_(e_invarg2
), name
- 1);
2233 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2234 EMSG2(_(e_letwrong
), op
);
2235 else if (endchars
!= NULL
2236 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2237 EMSG(_(e_letunexp
));
2242 p
= get_tv_string_chk(tv
);
2243 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2245 int mustfree
= FALSE
;
2246 char_u
*s
= vim_getenv(name
, &mustfree
);
2250 p
= tofree
= concat_str(s
, p
);
2257 vim_setenv(name
, p
);
2258 if (STRICMP(name
, "HOME") == 0)
2260 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2262 else if (didset_vimruntime
2263 && STRICMP(name
, "VIMRUNTIME") == 0)
2264 didset_vimruntime
= FALSE
;
2274 * ":let &option = expr": Set option value.
2275 * ":let &l:option = expr": Set local option value.
2276 * ":let &g:option = expr": Set global option value.
2278 else if (*arg
== '&')
2280 /* Find the end of the name. */
2281 p
= find_option_end(&arg
, &opt_flags
);
2282 if (p
== NULL
|| (endchars
!= NULL
2283 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2284 EMSG(_(e_letunexp
));
2290 char_u
*stringval
= NULL
;
2296 n
= get_tv_number(tv
);
2297 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2298 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2300 opt_type
= get_option_value(arg
, &numval
,
2301 &stringval
, opt_flags
);
2302 if ((opt_type
== 1 && *op
== '.')
2303 || (opt_type
== 0 && *op
!= '.'))
2304 EMSG2(_(e_letwrong
), op
);
2307 if (opt_type
== 1) /* number */
2314 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2316 s
= concat_str(stringval
, s
);
2317 vim_free(stringval
);
2324 set_option_value(arg
, n
, s
, opt_flags
);
2328 vim_free(stringval
);
2333 * ":let @r = expr": Set register contents.
2335 else if (*arg
== '@')
2338 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2339 EMSG2(_(e_letwrong
), op
);
2340 else if (endchars
!= NULL
2341 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2342 EMSG(_(e_letunexp
));
2345 char_u
*ptofree
= NULL
;
2348 p
= get_tv_string_chk(tv
);
2349 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2351 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2354 p
= ptofree
= concat_str(s
, p
);
2360 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2368 * ":let var = expr": Set internal variable.
2369 * ":let {expr} = expr": Idem, name made with curly braces
2371 else if (eval_isnamec1(*arg
) || *arg
== '{')
2375 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2376 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2378 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2379 EMSG(_(e_letunexp
));
2382 set_var_lval(&lv
, p
, tv
, copy
, op
);
2390 EMSG2(_(e_invarg2
), arg
);
2396 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2399 check_changedtick(arg
)
2402 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2404 EMSG2(_(e_readonlyvar
), arg
);
2411 * Get an lval: variable, Dict item or List item that can be assigned a value
2412 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2413 * "name.key", "name.key[expr]" etc.
2414 * Indexing only works if "name" is an existing List or Dictionary.
2415 * "name" points to the start of the name.
2416 * If "rettv" is not NULL it points to the value to be assigned.
2417 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2418 * wrong; must end in space or cmd separator.
2420 * Returns a pointer to just after the name, including indexes.
2421 * When an evaluation error occurs "lp->ll_name" is NULL;
2422 * Returns NULL for a parsing error. Still need to free items in "lp"!
2425 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2431 int quiet
; /* don't give error messages */
2432 int fne_flags
; /* flags for find_name_end() */
2435 char_u
*expr_start
, *expr_end
;
2446 /* Clear everything in "lp". */
2447 vim_memset(lp
, 0, sizeof(lval_T
));
2451 /* When skipping just find the end of the name. */
2453 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2456 /* Find the end of the name. */
2457 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2458 if (expr_start
!= NULL
)
2460 /* Don't expand the name when we already know there is an error. */
2461 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2462 && *p
!= '[' && *p
!= '.')
2464 EMSG(_(e_trailing
));
2468 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2469 if (lp
->ll_exp_name
== NULL
)
2471 /* Report an invalid expression in braces, unless the
2472 * expression evaluation has been cancelled due to an
2473 * aborting error, an interrupt, or an exception. */
2474 if (!aborting() && !quiet
)
2477 EMSG2(_(e_invarg2
), name
);
2481 lp
->ll_name
= lp
->ll_exp_name
;
2486 /* Without [idx] or .key we are done. */
2487 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2492 v
= find_var(lp
->ll_name
, &ht
);
2493 if (v
== NULL
&& !quiet
)
2494 EMSG2(_(e_undefvar
), lp
->ll_name
);
2500 * Loop until no more [idx] or .key is following.
2502 lp
->ll_tv
= &v
->di_tv
;
2503 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2505 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2506 && !(lp
->ll_tv
->v_type
== VAR_DICT
2507 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2510 EMSG(_("E689: Can only index a List or Dictionary"));
2516 EMSG(_("E708: [:] must come last"));
2524 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2529 EMSG(_(e_emptykey
));
2536 /* Get the index [expr] or the first index [expr: ]. */
2537 p
= skipwhite(p
+ 1);
2543 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2545 if (get_tv_string_chk(&var1
) == NULL
)
2547 /* not a number or string */
2553 /* Optionally get the second index [ :expr]. */
2556 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2559 EMSG(_(e_dictrange
));
2564 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2565 || rettv
->vval
.v_list
== NULL
))
2568 EMSG(_("E709: [:] requires a List value"));
2573 p
= skipwhite(p
+ 1);
2575 lp
->ll_empty2
= TRUE
;
2578 lp
->ll_empty2
= FALSE
;
2579 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2585 if (get_tv_string_chk(&var2
) == NULL
)
2587 /* not a number or string */
2594 lp
->ll_range
= TRUE
;
2597 lp
->ll_range
= FALSE
;
2602 EMSG(_(e_missbrac
));
2605 if (lp
->ll_range
&& !lp
->ll_empty2
)
2610 /* Skip to past ']'. */
2614 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2618 /* "[key]": get key from "var1" */
2619 key
= get_tv_string(&var1
); /* is number or string */
2623 EMSG(_(e_emptykey
));
2629 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2630 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2631 if (lp
->ll_di
== NULL
)
2633 /* Key does not exist in dict: may need to add it. */
2634 if (*p
== '[' || *p
== '.' || unlet
)
2637 EMSG2(_(e_dictkey
), key
);
2643 lp
->ll_newkey
= vim_strsave(key
);
2645 lp
->ll_newkey
= vim_strnsave(key
, len
);
2648 if (lp
->ll_newkey
== NULL
)
2654 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2659 * Get the number and item for the only or first index of the List.
2665 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2669 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2670 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2671 if (lp
->ll_li
== NULL
)
2676 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2679 if (lp
->ll_li
== NULL
)
2681 if (lp
->ll_range
&& !lp
->ll_empty2
)
2687 * May need to find the item or absolute index for the second
2689 * When no index given: "lp->ll_empty2" is TRUE.
2690 * Otherwise "lp->ll_n2" is set to the second index.
2692 if (lp
->ll_range
&& !lp
->ll_empty2
)
2694 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2698 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2701 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2704 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2706 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2707 if (lp
->ll_n2
< lp
->ll_n1
)
2711 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2719 * Clear lval "lp" that was filled by get_lval().
2725 vim_free(lp
->ll_exp_name
);
2726 vim_free(lp
->ll_newkey
);
2730 * Set a variable that was parsed by get_lval() to "rettv".
2731 * "endp" points to just after the parsed name.
2732 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2735 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2746 if (lp
->ll_tv
== NULL
)
2748 if (!check_changedtick(lp
->ll_name
))
2752 if (op
!= NULL
&& *op
!= '=')
2756 /* handle +=, -= and .= */
2757 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2760 if (tv_op(&tv
, rettv
, op
) == OK
)
2761 set_var(lp
->ll_name
, &tv
, FALSE
);
2766 set_var(lp
->ll_name
, rettv
, copy
);
2770 else if (tv_check_lock(lp
->ll_newkey
== NULL
2772 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2774 else if (lp
->ll_range
)
2777 * Assign the List values to the list items.
2779 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2781 if (op
!= NULL
&& *op
!= '=')
2782 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2785 clear_tv(&lp
->ll_li
->li_tv
);
2786 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2789 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2791 if (lp
->ll_li
->li_next
== NULL
)
2793 /* Need to add an empty item. */
2794 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2800 lp
->ll_li
= lp
->ll_li
->li_next
;
2804 EMSG(_("E710: List value has more items than target"));
2805 else if (lp
->ll_empty2
2806 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2807 : lp
->ll_n1
!= lp
->ll_n2
)
2808 EMSG(_("E711: List value has not enough items"));
2813 * Assign to a List or Dictionary item.
2815 if (lp
->ll_newkey
!= NULL
)
2817 if (op
!= NULL
&& *op
!= '=')
2819 EMSG2(_(e_letwrong
), op
);
2823 /* Need to add an item to the Dictionary. */
2824 di
= dictitem_alloc(lp
->ll_newkey
);
2827 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2832 lp
->ll_tv
= &di
->di_tv
;
2834 else if (op
!= NULL
&& *op
!= '=')
2836 tv_op(lp
->ll_tv
, rettv
, op
);
2840 clear_tv(lp
->ll_tv
);
2843 * Assign the value to the variable or list item.
2846 copy_tv(rettv
, lp
->ll_tv
);
2849 *lp
->ll_tv
= *rettv
;
2850 lp
->ll_tv
->v_lock
= 0;
2857 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2858 * Returns OK or FAIL.
2867 char_u numbuf
[NUMBUFLEN
];
2870 /* Can't do anything with a Funcref or a Dict on the right. */
2871 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2873 switch (tv1
->v_type
)
2880 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2883 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2884 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2889 if (tv2
->v_type
== VAR_LIST
)
2891 if (*op
== '+' || *op
== '-')
2893 /* nr += nr or nr -= nr*/
2894 n
= get_tv_number(tv1
);
2896 if (tv2
->v_type
== VAR_FLOAT
)
2901 f
+= tv2
->vval
.v_float
;
2903 f
-= tv2
->vval
.v_float
;
2905 tv1
->v_type
= VAR_FLOAT
;
2906 tv1
->vval
.v_float
= f
;
2912 n
+= get_tv_number(tv2
);
2914 n
-= get_tv_number(tv2
);
2916 tv1
->v_type
= VAR_NUMBER
;
2917 tv1
->vval
.v_number
= n
;
2922 if (tv2
->v_type
== VAR_FLOAT
)
2926 s
= get_tv_string(tv1
);
2927 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2929 tv1
->v_type
= VAR_STRING
;
2930 tv1
->vval
.v_string
= s
;
2939 if (*op
== '.' || (tv2
->v_type
!= VAR_FLOAT
2940 && tv2
->v_type
!= VAR_NUMBER
2941 && tv2
->v_type
!= VAR_STRING
))
2943 if (tv2
->v_type
== VAR_FLOAT
)
2944 f
= tv2
->vval
.v_float
;
2946 f
= get_tv_number(tv2
);
2948 tv1
->vval
.v_float
+= f
;
2950 tv1
->vval
.v_float
-= f
;
2957 EMSG2(_(e_letwrong
), op
);
2962 * Add a watcher to a list.
2965 list_add_watch(l
, lw
)
2969 lw
->lw_next
= l
->lv_watch
;
2974 * Remove a watcher from a list.
2975 * No warning when it isn't found...
2978 list_rem_watch(l
, lwrem
)
2982 listwatch_T
*lw
, **lwp
;
2985 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2997 * Just before removing an item from a list: advance watchers to the next
3001 list_fix_watch(l
, item
)
3007 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3008 if (lw
->lw_item
== item
)
3009 lw
->lw_item
= item
->li_next
;
3013 * Evaluate the expression used in a ":for var in expr" command.
3014 * "arg" points to "var".
3015 * Set "*errp" to TRUE for an error, FALSE otherwise;
3016 * Return a pointer that holds the info. Null when there is an error.
3019 eval_for_line(arg
, errp
, nextcmdp
, skip
)
3030 *errp
= TRUE
; /* default: there is an error */
3032 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
3036 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
3040 expr
= skipwhite(expr
);
3041 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
3043 EMSG(_("E690: Missing \"in\" after :for"));
3049 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
3055 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
3062 /* No need to increment the refcount, it's already set for the
3063 * list being used in "tv". */
3065 list_add_watch(l
, &fi
->fi_lw
);
3066 fi
->fi_lw
.lw_item
= l
->lv_first
;
3077 * Use the first item in a ":for" list. Advance to the next.
3078 * Assign the values to the variable (list). "arg" points to the first one.
3079 * Return TRUE when a valid item was found, FALSE when at end of list or
3083 next_for_item(fi_void
, arg
)
3087 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3091 item
= fi
->fi_lw
.lw_item
;
3096 fi
->fi_lw
.lw_item
= item
->li_next
;
3097 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3098 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3104 * Free the structure used to store info used by ":for".
3107 free_for_info(fi_void
)
3110 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3112 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3114 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3115 list_unref(fi
->fi_list
);
3120 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3123 set_context_for_expression(xp
, arg
, cmdidx
)
3132 if (cmdidx
== CMD_let
)
3134 xp
->xp_context
= EXPAND_USER_VARS
;
3135 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3137 /* ":let var1 var2 ...": find last space. */
3138 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3141 mb_ptr_back(arg
, p
);
3142 if (vim_iswhite(*p
))
3149 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3150 : EXPAND_EXPRESSION
;
3151 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3152 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3154 c
= *xp
->xp_pattern
;
3157 c
= xp
->xp_pattern
[1];
3161 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3162 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3166 xp
->xp_context
= EXPAND_SETTINGS
;
3167 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3168 xp
->xp_pattern
+= 2;
3174 /* environment variable */
3175 xp
->xp_context
= EXPAND_ENV_VARS
;
3180 xp
->xp_context
= EXPAND_EXPRESSION
;
3183 && xp
->xp_context
== EXPAND_FUNCTIONS
3184 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3186 /* Function name can start with "<SNR>" */
3189 else if (cmdidx
!= CMD_let
|| got_eq
)
3191 if (c
== '"') /* string */
3193 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3194 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3196 xp
->xp_context
= EXPAND_NOTHING
;
3198 else if (c
== '\'') /* literal string */
3200 /* Trick: '' is like stopping and starting a literal string. */
3201 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3203 xp
->xp_context
= EXPAND_NOTHING
;
3207 if (xp
->xp_pattern
[1] == '|')
3210 xp
->xp_context
= EXPAND_EXPRESSION
;
3213 xp
->xp_context
= EXPAND_COMMANDS
;
3216 xp
->xp_context
= EXPAND_EXPRESSION
;
3219 /* Doesn't look like something valid, expand as an expression
3221 xp
->xp_context
= EXPAND_EXPRESSION
;
3222 arg
= xp
->xp_pattern
;
3224 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3227 xp
->xp_pattern
= arg
;
3230 #endif /* FEAT_CMDL_COMPL */
3233 * ":1,25call func(arg1, arg2)" function call.
3239 char_u
*arg
= eap
->arg
;
3250 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3251 if (fudi
.fd_newkey
!= NULL
)
3253 /* Still need to give an error message for missing key. */
3254 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3255 vim_free(fudi
.fd_newkey
);
3260 /* Increase refcount on dictionary, it could get deleted when evaluating
3262 if (fudi
.fd_dict
!= NULL
)
3263 ++fudi
.fd_dict
->dv_refcount
;
3265 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3266 len
= (int)STRLEN(tofree
);
3267 name
= deref_func_name(tofree
, &len
);
3269 /* Skip white space to allow ":call func ()". Not good, but required for
3270 * backward compatibility. */
3271 startarg
= skipwhite(arg
);
3272 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3274 if (*startarg
!= '(')
3276 EMSG2(_("E107: Missing braces: %s"), eap
->arg
);
3281 * When skipping, evaluate the function once, to find the end of the
3283 * When the function takes a range, this is discovered after the first
3284 * call, and the loop is broken.
3289 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3293 for ( ; lnum
<= eap
->line2
; ++lnum
)
3295 if (!eap
->skip
&& eap
->addr_count
> 0)
3297 curwin
->w_cursor
.lnum
= lnum
;
3298 curwin
->w_cursor
.col
= 0;
3301 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3302 eap
->line1
, eap
->line2
, &doesrange
,
3303 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3309 /* Handle a function returning a Funcref, Dictionary or List. */
3310 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3317 if (doesrange
|| eap
->skip
)
3320 /* Stop when immediately aborting on error, or when an interrupt
3321 * occurred or an exception was thrown but not caught.
3322 * get_func_tv() returned OK, so that the check for trailing
3323 * characters below is executed. */
3332 /* Check for trailing illegal characters and a following command. */
3333 if (!ends_excmd(*arg
))
3336 EMSG(_(e_trailing
));
3339 eap
->nextcmd
= check_nextcmd(arg
);
3343 dict_unref(fudi
.fd_dict
);
3348 * ":unlet[!] var1 ... " command.
3354 ex_unletlock(eap
, eap
->arg
, 0);
3358 * ":lockvar" and ":unlockvar" commands
3364 char_u
*arg
= eap
->arg
;
3369 else if (vim_isdigit(*arg
))
3371 deep
= getdigits(&arg
);
3372 arg
= skipwhite(arg
);
3375 ex_unletlock(eap
, arg
, deep
);
3379 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3382 ex_unletlock(eap
, argstart
, deep
)
3387 char_u
*arg
= argstart
;
3394 /* Parse the name and find the end. */
3395 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3397 if (lv
.ll_name
== NULL
)
3398 error
= TRUE
; /* error but continue parsing */
3399 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3400 && !ends_excmd(*name_end
)))
3402 if (name_end
!= NULL
)
3405 EMSG(_(e_trailing
));
3407 if (!(eap
->skip
|| error
))
3412 if (!error
&& !eap
->skip
)
3414 if (eap
->cmdidx
== CMD_unlet
)
3416 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3421 if (do_lock_var(&lv
, name_end
, deep
,
3422 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3430 arg
= skipwhite(name_end
);
3431 } while (!ends_excmd(*arg
));
3433 eap
->nextcmd
= check_nextcmd(arg
);
3437 do_unlet_var(lp
, name_end
, forceit
)
3445 if (lp
->ll_tv
== NULL
)
3450 /* Normal name or expanded name. */
3451 if (check_changedtick(lp
->ll_name
))
3453 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3457 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3459 else if (lp
->ll_range
)
3463 /* Delete a range of List items. */
3464 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3466 li
= lp
->ll_li
->li_next
;
3467 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3474 if (lp
->ll_list
!= NULL
)
3475 /* unlet a List item. */
3476 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3478 /* unlet a Dictionary item. */
3479 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3486 * "unlet" a variable. Return OK if it existed, FAIL if not.
3487 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3490 do_unlet(name
, forceit
)
3499 ht
= find_var_ht(name
, &varname
);
3500 if (ht
!= NULL
&& *varname
!= NUL
)
3502 hi
= hash_find(ht
, varname
);
3503 if (!HASHITEM_EMPTY(hi
))
3506 if (var_check_fixed(di
->di_flags
, name
)
3507 || var_check_ro(di
->di_flags
, name
))
3515 EMSG2(_("E108: No such variable: \"%s\""), name
);
3520 * Lock or unlock variable indicated by "lp".
3521 * "deep" is the levels to go (-1 for unlimited);
3522 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3525 do_lock_var(lp
, name_end
, deep
, lock
)
3535 if (deep
== 0) /* nothing to do */
3538 if (lp
->ll_tv
== NULL
)
3543 /* Normal name or expanded name. */
3544 if (check_changedtick(lp
->ll_name
))
3548 di
= find_var(lp
->ll_name
, NULL
);
3554 di
->di_flags
|= DI_FLAGS_LOCK
;
3556 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3557 item_lock(&di
->di_tv
, deep
, lock
);
3562 else if (lp
->ll_range
)
3564 listitem_T
*li
= lp
->ll_li
;
3566 /* (un)lock a range of List items. */
3567 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3569 item_lock(&li
->li_tv
, deep
, lock
);
3574 else if (lp
->ll_list
!= NULL
)
3575 /* (un)lock a List item. */
3576 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3578 /* un(lock) a Dictionary item. */
3579 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3585 * Lock or unlock an item. "deep" is nr of levels to go.
3588 item_lock(tv
, deep
, lock
)
3593 static int recurse
= 0;
3600 if (recurse
>= DICT_MAXNEST
)
3602 EMSG(_("E743: variable nested too deep for (un)lock"));
3609 /* lock/unlock the item itself */
3611 tv
->v_lock
|= VAR_LOCKED
;
3613 tv
->v_lock
&= ~VAR_LOCKED
;
3618 if ((l
= tv
->vval
.v_list
) != NULL
)
3621 l
->lv_lock
|= VAR_LOCKED
;
3623 l
->lv_lock
&= ~VAR_LOCKED
;
3624 if (deep
< 0 || deep
> 1)
3625 /* recursive: lock/unlock the items the List contains */
3626 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3627 item_lock(&li
->li_tv
, deep
- 1, lock
);
3631 if ((d
= tv
->vval
.v_dict
) != NULL
)
3634 d
->dv_lock
|= VAR_LOCKED
;
3636 d
->dv_lock
&= ~VAR_LOCKED
;
3637 if (deep
< 0 || deep
> 1)
3639 /* recursive: lock/unlock the items the List contains */
3640 todo
= (int)d
->dv_hashtab
.ht_used
;
3641 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3643 if (!HASHITEM_EMPTY(hi
))
3646 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3656 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3657 * it refers to a List or Dictionary that is locked.
3663 return (tv
->v_lock
& VAR_LOCKED
)
3664 || (tv
->v_type
== VAR_LIST
3665 && tv
->vval
.v_list
!= NULL
3666 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3667 || (tv
->v_type
== VAR_DICT
3668 && tv
->vval
.v_dict
!= NULL
3669 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3672 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3674 * Delete all "menutrans_" variables.
3677 del_menutrans_vars()
3682 hash_lock(&globvarht
);
3683 todo
= (int)globvarht
.ht_used
;
3684 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3686 if (!HASHITEM_EMPTY(hi
))
3689 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3690 delete_var(&globvarht
, hi
);
3693 hash_unlock(&globvarht
);
3697 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3700 * Local string buffer for the next two functions to store a variable name
3701 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3702 * get_user_var_name().
3705 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3707 static char_u
*varnamebuf
= NULL
;
3708 static int varnamebuflen
= 0;
3711 * Function to concatenate a prefix and a variable name.
3714 cat_prefix_varname(prefix
, name
)
3720 len
= (int)STRLEN(name
) + 3;
3721 if (len
> varnamebuflen
)
3723 vim_free(varnamebuf
);
3724 len
+= 10; /* some additional space */
3725 varnamebuf
= alloc(len
);
3726 if (varnamebuf
== NULL
)
3731 varnamebuflen
= len
;
3733 *varnamebuf
= prefix
;
3734 varnamebuf
[1] = ':';
3735 STRCPY(varnamebuf
+ 2, name
);
3740 * Function given to ExpandGeneric() to obtain the list of user defined
3741 * (global/buffer/window/built-in) variable names.
3745 get_user_var_name(xp
, idx
)
3749 static long_u gdone
;
3750 static long_u bdone
;
3751 static long_u wdone
;
3753 static long_u tdone
;
3756 static hashitem_T
*hi
;
3761 gdone
= bdone
= wdone
= vidx
= 0;
3767 /* Global variables */
3768 if (gdone
< globvarht
.ht_used
)
3771 hi
= globvarht
.ht_array
;
3774 while (HASHITEM_EMPTY(hi
))
3776 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3777 return cat_prefix_varname('g', hi
->hi_key
);
3782 ht
= &curbuf
->b_vars
.dv_hashtab
;
3783 if (bdone
< ht
->ht_used
)
3789 while (HASHITEM_EMPTY(hi
))
3791 return cat_prefix_varname('b', hi
->hi_key
);
3793 if (bdone
== ht
->ht_used
)
3796 return (char_u
*)"b:changedtick";
3800 ht
= &curwin
->w_vars
.dv_hashtab
;
3801 if (wdone
< ht
->ht_used
)
3807 while (HASHITEM_EMPTY(hi
))
3809 return cat_prefix_varname('w', hi
->hi_key
);
3814 ht
= &curtab
->tp_vars
.dv_hashtab
;
3815 if (tdone
< ht
->ht_used
)
3821 while (HASHITEM_EMPTY(hi
))
3823 return cat_prefix_varname('t', hi
->hi_key
);
3829 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3831 vim_free(varnamebuf
);
3837 #endif /* FEAT_CMDL_COMPL */
3840 * types for expressions.
3845 , TYPE_EQUAL
/* == */
3846 , TYPE_NEQUAL
/* != */
3847 , TYPE_GREATER
/* > */
3848 , TYPE_GEQUAL
/* >= */
3849 , TYPE_SMALLER
/* < */
3850 , TYPE_SEQUAL
/* <= */
3851 , TYPE_MATCH
/* =~ */
3852 , TYPE_NOMATCH
/* !~ */
3856 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3857 * executed. The function may return OK, but the rettv will be of type
3858 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3862 * Handle zero level expression.
3863 * This calls eval1() and handles error message and nextcmd.
3864 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3865 * Note: "rettv.v_lock" is not set.
3866 * Return OK or FAIL.
3869 eval0(arg
, rettv
, nextcmd
, evaluate
)
3879 ret
= eval1(&p
, rettv
, evaluate
);
3880 if (ret
== FAIL
|| !ends_excmd(*p
))
3885 * Report the invalid expression unless the expression evaluation has
3886 * been cancelled due to an aborting error, an interrupt, or an
3890 EMSG2(_(e_invexpr2
), arg
);
3893 if (nextcmd
!= NULL
)
3894 *nextcmd
= check_nextcmd(p
);
3900 * Handle top level expression:
3901 * expr1 ? expr0 : expr0
3903 * "arg" must point to the first non-white of the expression.
3904 * "arg" is advanced to the next non-white after the recognized expression.
3906 * Note: "rettv.v_lock" is not set.
3908 * Return OK or FAIL.
3911 eval1(arg
, rettv
, evaluate
)
3920 * Get the first variable.
3922 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3925 if ((*arg
)[0] == '?')
3932 if (get_tv_number_chk(rettv
, &error
) != 0)
3940 * Get the second variable.
3942 *arg
= skipwhite(*arg
+ 1);
3943 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3947 * Check for the ":".
3949 if ((*arg
)[0] != ':')
3951 EMSG(_("E109: Missing ':' after '?'"));
3952 if (evaluate
&& result
)
3958 * Get the third variable.
3960 *arg
= skipwhite(*arg
+ 1);
3961 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
3963 if (evaluate
&& result
)
3967 if (evaluate
&& !result
)
3975 * Handle first level expression:
3976 * expr2 || expr2 || expr2 logical OR
3978 * "arg" must point to the first non-white of the expression.
3979 * "arg" is advanced to the next non-white after the recognized expression.
3981 * Return OK or FAIL.
3984 eval2(arg
, rettv
, evaluate
)
3995 * Get the first variable.
3997 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
4001 * Repeat until there is no following "||".
4005 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
4007 if (evaluate
&& first
)
4009 if (get_tv_number_chk(rettv
, &error
) != 0)
4018 * Get the second variable.
4020 *arg
= skipwhite(*arg
+ 2);
4021 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
4025 * Compute the result.
4027 if (evaluate
&& !result
)
4029 if (get_tv_number_chk(&var2
, &error
) != 0)
4037 rettv
->v_type
= VAR_NUMBER
;
4038 rettv
->vval
.v_number
= result
;
4046 * Handle second level expression:
4047 * expr3 && expr3 && expr3 logical AND
4049 * "arg" must point to the first non-white of the expression.
4050 * "arg" is advanced to the next non-white after the recognized expression.
4052 * Return OK or FAIL.
4055 eval3(arg
, rettv
, evaluate
)
4066 * Get the first variable.
4068 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
4072 * Repeat until there is no following "&&".
4076 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
4078 if (evaluate
&& first
)
4080 if (get_tv_number_chk(rettv
, &error
) == 0)
4089 * Get the second variable.
4091 *arg
= skipwhite(*arg
+ 2);
4092 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4096 * Compute the result.
4098 if (evaluate
&& result
)
4100 if (get_tv_number_chk(&var2
, &error
) == 0)
4108 rettv
->v_type
= VAR_NUMBER
;
4109 rettv
->vval
.v_number
= result
;
4117 * Handle third level expression:
4129 * "arg" must point to the first non-white of the expression.
4130 * "arg" is advanced to the next non-white after the recognized expression.
4132 * Return OK or FAIL.
4135 eval4(arg
, rettv
, evaluate
)
4143 exptype_T type
= TYPE_UNKNOWN
;
4144 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4148 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4149 regmatch_T regmatch
;
4154 * Get the first variable.
4156 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4162 case '=': if (p
[1] == '=')
4164 else if (p
[1] == '~')
4167 case '!': if (p
[1] == '=')
4169 else if (p
[1] == '~')
4170 type
= TYPE_NOMATCH
;
4172 case '>': if (p
[1] != '=')
4174 type
= TYPE_GREATER
;
4180 case '<': if (p
[1] != '=')
4182 type
= TYPE_SMALLER
;
4188 case 'i': if (p
[1] == 's')
4190 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4192 if (!vim_isIDc(p
[len
]))
4194 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4202 * If there is a comparative operator, use it.
4204 if (type
!= TYPE_UNKNOWN
)
4206 /* extra question mark appended: ignore case */
4212 /* extra '#' appended: match case */
4213 else if (p
[len
] == '#')
4218 /* nothing appended: use 'ignorecase' */
4223 * Get the second variable.
4225 *arg
= skipwhite(p
+ len
);
4226 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4234 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4236 /* For "is" a different type always means FALSE, for "notis"
4238 n1
= (type
== TYPE_NEQUAL
);
4240 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4244 n1
= (rettv
->v_type
== var2
.v_type
4245 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4246 if (type
== TYPE_NEQUAL
)
4249 else if (rettv
->v_type
!= var2
.v_type
4250 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4252 if (rettv
->v_type
!= var2
.v_type
)
4253 EMSG(_("E691: Can only compare List with List"));
4255 EMSG(_("E692: Invalid operation for Lists"));
4262 /* Compare two Lists for being equal or unequal. */
4263 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4264 if (type
== TYPE_NEQUAL
)
4269 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4273 n1
= (rettv
->v_type
== var2
.v_type
4274 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4275 if (type
== TYPE_NEQUAL
)
4278 else if (rettv
->v_type
!= var2
.v_type
4279 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4281 if (rettv
->v_type
!= var2
.v_type
)
4282 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4284 EMSG(_("E736: Invalid operation for Dictionary"));
4291 /* Compare two Dictionaries for being equal or unequal. */
4292 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4293 if (type
== TYPE_NEQUAL
)
4298 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4300 if (rettv
->v_type
!= var2
.v_type
4301 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4303 if (rettv
->v_type
!= var2
.v_type
)
4304 EMSG(_("E693: Can only compare Funcref with Funcref"));
4306 EMSG(_("E694: Invalid operation for Funcrefs"));
4313 /* Compare two Funcrefs for being equal or unequal. */
4314 if (rettv
->vval
.v_string
== NULL
4315 || var2
.vval
.v_string
== NULL
)
4318 n1
= STRCMP(rettv
->vval
.v_string
,
4319 var2
.vval
.v_string
) == 0;
4320 if (type
== TYPE_NEQUAL
)
4327 * If one of the two variables is a float, compare as a float.
4328 * When using "=~" or "!~", always compare as string.
4330 else if ((rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4331 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4335 if (rettv
->v_type
== VAR_FLOAT
)
4336 f1
= rettv
->vval
.v_float
;
4338 f1
= get_tv_number(rettv
);
4339 if (var2
.v_type
== VAR_FLOAT
)
4340 f2
= var2
.vval
.v_float
;
4342 f2
= get_tv_number(&var2
);
4346 case TYPE_EQUAL
: n1
= (f1
== f2
); break;
4347 case TYPE_NEQUAL
: n1
= (f1
!= f2
); break;
4348 case TYPE_GREATER
: n1
= (f1
> f2
); break;
4349 case TYPE_GEQUAL
: n1
= (f1
>= f2
); break;
4350 case TYPE_SMALLER
: n1
= (f1
< f2
); break;
4351 case TYPE_SEQUAL
: n1
= (f1
<= f2
); break;
4354 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4360 * If one of the two variables is a number, compare as a number.
4361 * When using "=~" or "!~", always compare as string.
4363 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4364 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4366 n1
= get_tv_number(rettv
);
4367 n2
= get_tv_number(&var2
);
4370 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4371 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4372 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4373 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4374 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4375 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4378 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4383 s1
= get_tv_string_buf(rettv
, buf1
);
4384 s2
= get_tv_string_buf(&var2
, buf2
);
4385 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4386 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4392 case TYPE_EQUAL
: n1
= (i
== 0); break;
4393 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4394 case TYPE_GREATER
: n1
= (i
> 0); break;
4395 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4396 case TYPE_SMALLER
: n1
= (i
< 0); break;
4397 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4401 /* avoid 'l' flag in 'cpoptions' */
4403 p_cpo
= (char_u
*)"";
4404 regmatch
.regprog
= vim_regcomp(s2
,
4405 RE_MAGIC
+ RE_STRING
);
4406 regmatch
.rm_ic
= ic
;
4407 if (regmatch
.regprog
!= NULL
)
4409 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4410 vim_free(regmatch
.regprog
);
4411 if (type
== TYPE_NOMATCH
)
4417 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4422 rettv
->v_type
= VAR_NUMBER
;
4423 rettv
->vval
.v_number
= n1
;
4431 * Handle fourth level expression:
4433 * - number subtraction
4434 * . string concatenation
4436 * "arg" must point to the first non-white of the expression.
4437 * "arg" is advanced to the next non-white after the recognized expression.
4439 * Return OK or FAIL.
4442 eval5(arg
, rettv
, evaluate
)
4452 float_T f1
= 0, f2
= 0;
4455 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4459 * Get the first variable.
4461 if (eval6(arg
, rettv
, evaluate
, FALSE
) == FAIL
)
4465 * Repeat computing, until no '+', '-' or '.' is following.
4470 if (op
!= '+' && op
!= '-' && op
!= '.')
4473 if ((op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4475 && (op
== '.' || rettv
->v_type
!= VAR_FLOAT
)
4479 /* For "list + ...", an illegal use of the first operand as
4480 * a number cannot be determined before evaluating the 2nd
4481 * operand: if this is also a list, all is ok.
4482 * For "something . ...", "something - ..." or "non-list + ...",
4483 * we know that the first operand needs to be a string or number
4484 * without evaluating the 2nd operand. So check before to avoid
4485 * side effects after an error. */
4486 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4494 * Get the second variable.
4496 *arg
= skipwhite(*arg
+ 1);
4497 if (eval6(arg
, &var2
, evaluate
, op
== '.') == FAIL
)
4506 * Compute the result.
4510 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4511 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4512 if (s2
== NULL
) /* type error ? */
4518 p
= concat_str(s1
, s2
);
4520 rettv
->v_type
= VAR_STRING
;
4521 rettv
->vval
.v_string
= p
;
4523 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4524 && var2
.v_type
== VAR_LIST
)
4526 /* concatenate Lists */
4527 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4542 if (rettv
->v_type
== VAR_FLOAT
)
4544 f1
= rettv
->vval
.v_float
;
4550 n1
= get_tv_number_chk(rettv
, &error
);
4553 /* This can only happen for "list + non-list". For
4554 * "non-list + ..." or "something - ...", we returned
4555 * before evaluating the 2nd operand. */
4560 if (var2
.v_type
== VAR_FLOAT
)
4565 if (var2
.v_type
== VAR_FLOAT
)
4567 f2
= var2
.vval
.v_float
;
4573 n2
= get_tv_number_chk(&var2
, &error
);
4581 if (rettv
->v_type
== VAR_FLOAT
)
4588 /* If there is a float on either side the result is a float. */
4589 if (rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4595 rettv
->v_type
= VAR_FLOAT
;
4596 rettv
->vval
.v_float
= f1
;
4605 rettv
->v_type
= VAR_NUMBER
;
4606 rettv
->vval
.v_number
= n1
;
4616 * Handle fifth level expression:
4617 * * number multiplication
4621 * "arg" must point to the first non-white of the expression.
4622 * "arg" is advanced to the next non-white after the recognized expression.
4624 * Return OK or FAIL.
4627 eval6(arg
, rettv
, evaluate
, want_string
)
4631 int want_string
; /* after "." operator */
4637 int use_float
= FALSE
;
4643 * Get the first variable.
4645 if (eval7(arg
, rettv
, evaluate
, want_string
) == FAIL
)
4649 * Repeat computing, until no '*', '/' or '%' is following.
4654 if (op
!= '*' && op
!= '/' && op
!= '%')
4660 if (rettv
->v_type
== VAR_FLOAT
)
4662 f1
= rettv
->vval
.v_float
;
4668 n1
= get_tv_number_chk(rettv
, &error
);
4677 * Get the second variable.
4679 *arg
= skipwhite(*arg
+ 1);
4680 if (eval7(arg
, &var2
, evaluate
, FALSE
) == FAIL
)
4686 if (var2
.v_type
== VAR_FLOAT
)
4693 f2
= var2
.vval
.v_float
;
4699 n2
= get_tv_number_chk(&var2
, &error
);
4710 * Compute the result.
4711 * When either side is a float the result is a float.
4720 /* We rely on the floating point library to handle divide
4721 * by zero to result in "inf" and not a crash. */
4726 EMSG(_("E804: Cannot use % with float"));
4729 rettv
->v_type
= VAR_FLOAT
;
4730 rettv
->vval
.v_float
= f1
;
4739 if (n2
== 0) /* give an error message? */
4742 n1
= -0x7fffffffL
- 1L; /* similar to NaN */
4753 if (n2
== 0) /* give an error message? */
4758 rettv
->v_type
= VAR_NUMBER
;
4759 rettv
->vval
.v_number
= n1
;
4768 * Handle sixth level expression:
4769 * number number constant
4770 * "string" string constant
4771 * 'string' literal string constant
4772 * &option-name option value
4773 * @r register contents
4774 * identifier variable value
4775 * function() function call
4776 * $VAR environment variable
4777 * (expression) nested expression
4779 * {key: val, key: val} Dictionary
4782 * ! in front logical NOT
4783 * - in front unary minus
4784 * + in front unary plus (ignored)
4785 * trailing [] subscript in String or List
4786 * trailing .name entry in Dictionary
4788 * "arg" must point to the first non-white of the expression.
4789 * "arg" is advanced to the next non-white after the recognized expression.
4791 * Return OK or FAIL.
4794 eval7(arg
, rettv
, evaluate
, want_string
)
4798 int want_string
; /* after "." operator */
4803 char_u
*start_leader
, *end_leader
;
4808 * Initialise variable so that clear_tv() can't mistake this for a
4809 * string and free a string that isn't there.
4811 rettv
->v_type
= VAR_UNKNOWN
;
4814 * Skip '!' and '-' characters. They are handled later.
4816 start_leader
= *arg
;
4817 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4818 *arg
= skipwhite(*arg
+ 1);
4838 char_u
*p
= skipdigits(*arg
+ 1);
4839 int get_float
= FALSE
;
4841 /* We accept a float when the format matches
4842 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4843 * strict to avoid backwards compatibility problems.
4844 * Don't look for a float after the "." operator, so that
4845 * ":let vers = 1.2.3" doesn't fail. */
4846 if (!want_string
&& p
[0] == '.' && vim_isdigit(p
[1]))
4849 p
= skipdigits(p
+ 2);
4850 if (*p
== 'e' || *p
== 'E')
4853 if (*p
== '-' || *p
== '+')
4855 if (!vim_isdigit(*p
))
4858 p
= skipdigits(p
+ 1);
4860 if (ASCII_ISALPHA(*p
) || *p
== '.')
4867 *arg
+= string2float(*arg
, &f
);
4870 rettv
->v_type
= VAR_FLOAT
;
4871 rettv
->vval
.v_float
= f
;
4877 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4881 rettv
->v_type
= VAR_NUMBER
;
4882 rettv
->vval
.v_number
= n
;
4889 * String constant: "string".
4891 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4895 * Literal string constant: 'str''ing'.
4897 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4901 * List: [expr, expr]
4903 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4907 * Dictionary: {key: val, key: val}
4909 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4913 * Option value: &name
4915 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4919 * Environment variable: $VAR.
4921 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4925 * Register contents: @r.
4930 rettv
->v_type
= VAR_STRING
;
4931 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4938 * nested expression: (expression).
4940 case '(': *arg
= skipwhite(*arg
+ 1);
4941 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4946 EMSG(_("E110: Missing ')'"));
4952 default: ret
= NOTDONE
;
4959 * Must be a variable or function name.
4960 * Can also be a curly-braces kind of name: {expr}.
4963 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
4971 if (**arg
== '(') /* recursive! */
4973 /* If "s" is the name of a variable of type VAR_FUNC
4974 * use its contents. */
4975 s
= deref_func_name(s
, &len
);
4977 /* Invoke the function. */
4978 ret
= get_func_tv(s
, len
, rettv
, arg
,
4979 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
4980 &len
, evaluate
, NULL
);
4981 /* Stop the expression evaluation when immediately
4982 * aborting on error, or when an interrupt occurred or
4983 * an exception was thrown but not caught. */
4992 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
5001 *arg
= skipwhite(*arg
);
5003 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5006 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
5009 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5011 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
5018 if (rettv
->v_type
== VAR_FLOAT
)
5019 f
= rettv
->vval
.v_float
;
5022 val
= get_tv_number_chk(rettv
, &error
);
5030 while (end_leader
> start_leader
)
5033 if (*end_leader
== '!')
5036 if (rettv
->v_type
== VAR_FLOAT
)
5042 else if (*end_leader
== '-')
5045 if (rettv
->v_type
== VAR_FLOAT
)
5053 if (rettv
->v_type
== VAR_FLOAT
)
5056 rettv
->vval
.v_float
= f
;
5062 rettv
->v_type
= VAR_NUMBER
;
5063 rettv
->vval
.v_number
= val
;
5072 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5073 * "*arg" points to the '[' or '.'.
5074 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5077 eval_index(arg
, rettv
, evaluate
, verbose
)
5081 int verbose
; /* give error messages */
5083 int empty1
= FALSE
, empty2
= FALSE
;
5084 typval_T var1
, var2
;
5091 if (rettv
->v_type
== VAR_FUNC
5093 || rettv
->v_type
== VAR_FLOAT
5098 EMSG(_("E695: Cannot index a Funcref"));
5108 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
5112 *arg
= skipwhite(key
+ len
);
5119 * Get the (first) variable from inside the [].
5121 *arg
= skipwhite(*arg
+ 1);
5124 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
5126 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
5128 /* not a number or string */
5134 * Get the second variable from inside the [:].
5139 *arg
= skipwhite(*arg
+ 1);
5142 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
5148 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
5150 /* not a number or string */
5158 /* Check for the ']'. */
5162 EMSG(_(e_missbrac
));
5168 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
5174 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
5176 n1
= get_tv_number(&var1
);
5185 n2
= get_tv_number(&var2
);
5190 switch (rettv
->v_type
)
5194 s
= get_tv_string(rettv
);
5195 len
= (long)STRLEN(s
);
5198 /* The resulting variable is a substring. If the indexes
5199 * are out of range the result is empty. */
5210 if (n1
>= len
|| n2
< 0 || n1
> n2
)
5213 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
5217 /* The resulting variable is a string of a single
5218 * character. If the index is too big or negative the
5219 * result is empty. */
5220 if (n1
>= len
|| n1
< 0)
5223 s
= vim_strnsave(s
+ n1
, 1);
5226 rettv
->v_type
= VAR_STRING
;
5227 rettv
->vval
.v_string
= s
;
5231 len
= list_len(rettv
->vval
.v_list
);
5234 if (!empty1
&& (n1
< 0 || n1
>= len
))
5236 /* For a range we allow invalid values and return an empty
5237 * list. A list index out of range is an error. */
5241 EMSGN(_(e_listidx
), n1
);
5255 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
5260 for (item
= list_find(rettv
->vval
.v_list
, n1
);
5263 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
5268 item
= item
->li_next
;
5271 rettv
->v_type
= VAR_LIST
;
5272 rettv
->vval
.v_list
= l
;
5277 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
5287 EMSG(_(e_dictrange
));
5297 key
= get_tv_string(&var1
);
5301 EMSG(_(e_emptykey
));
5307 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
5309 if (item
== NULL
&& verbose
)
5310 EMSG2(_(e_dictkey
), key
);
5316 copy_tv(&item
->di_tv
, &var1
);
5328 * Get an option value.
5329 * "arg" points to the '&' or '+' before the option name.
5330 * "arg" is advanced to character after the option name.
5331 * Return OK or FAIL.
5334 get_option_tv(arg
, rettv
, evaluate
)
5336 typval_T
*rettv
; /* when NULL, only check if option exists */
5344 int working
= (**arg
== '+'); /* has("+option") */
5349 * Isolate the option name and find its value.
5351 option_end
= find_option_end(arg
, &opt_flags
);
5352 if (option_end
== NULL
)
5355 EMSG2(_("E112: Option name missing: %s"), *arg
);
5367 opt_type
= get_option_value(*arg
, &numval
,
5368 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5370 if (opt_type
== -3) /* invalid name */
5373 EMSG2(_("E113: Unknown option: %s"), *arg
);
5376 else if (rettv
!= NULL
)
5378 if (opt_type
== -2) /* hidden string option */
5380 rettv
->v_type
= VAR_STRING
;
5381 rettv
->vval
.v_string
= NULL
;
5383 else if (opt_type
== -1) /* hidden number option */
5385 rettv
->v_type
= VAR_NUMBER
;
5386 rettv
->vval
.v_number
= 0;
5388 else if (opt_type
== 1) /* number option */
5390 rettv
->v_type
= VAR_NUMBER
;
5391 rettv
->vval
.v_number
= numval
;
5393 else /* string option */
5395 rettv
->v_type
= VAR_STRING
;
5396 rettv
->vval
.v_string
= stringval
;
5399 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5402 *option_end
= c
; /* put back for error messages */
5409 * Allocate a variable for a string constant.
5410 * Return OK or FAIL.
5413 get_string_tv(arg
, rettv
, evaluate
)
5423 * Find the end of the string, skipping backslashed characters.
5425 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5427 if (*p
== '\\' && p
[1] != NUL
)
5430 /* A "\<x>" form occupies at least 4 characters, and produces up
5431 * to 6 characters: reserve space for 2 extra */
5439 EMSG2(_("E114: Missing quote: %s"), *arg
);
5443 /* If only parsing, set *arg and return here */
5451 * Copy the string into allocated memory, handling backslashed
5454 name
= alloc((unsigned)(p
- *arg
+ extra
));
5457 rettv
->v_type
= VAR_STRING
;
5458 rettv
->vval
.v_string
= name
;
5460 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5466 case 'b': *name
++ = BS
; ++p
; break;
5467 case 'e': *name
++ = ESC
; ++p
; break;
5468 case 'f': *name
++ = FF
; ++p
; break;
5469 case 'n': *name
++ = NL
; ++p
; break;
5470 case 'r': *name
++ = CAR
; ++p
; break;
5471 case 't': *name
++ = TAB
; ++p
; break;
5473 case 'X': /* hex: "\x1", "\x12" */
5475 case 'u': /* Unicode: "\u0023" */
5477 if (vim_isxdigit(p
[1]))
5480 int c
= toupper(*p
);
5487 while (--n
>= 0 && vim_isxdigit(p
[1]))
5490 nr
= (nr
<< 4) + hex2nr(*p
);
5494 /* For "\u" store the number according to
5497 name
+= (*mb_char2bytes
)(nr
, name
);
5504 /* octal: "\1", "\12", "\123" */
5512 case '7': *name
= *p
++ - '0';
5513 if (*p
>= '0' && *p
<= '7')
5515 *name
= (*name
<< 3) + *p
++ - '0';
5516 if (*p
>= '0' && *p
<= '7')
5517 *name
= (*name
<< 3) + *p
++ - '0';
5522 /* Special key, e.g.: "\<C-W>" */
5523 case '<': extra
= trans_special(&p
, name
, TRUE
);
5531 default: MB_COPY_CHAR(p
, name
);
5536 MB_COPY_CHAR(p
, name
);
5546 * Allocate a variable for a 'str''ing' constant.
5547 * Return OK or FAIL.
5550 get_lit_string_tv(arg
, rettv
, evaluate
)
5560 * Find the end of the string, skipping ''.
5562 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5575 EMSG2(_("E115: Missing quote: %s"), *arg
);
5579 /* If only parsing return after setting "*arg" */
5587 * Copy the string into allocated memory, handling '' to ' reduction.
5589 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5592 rettv
->v_type
= VAR_STRING
;
5593 rettv
->vval
.v_string
= str
;
5595 for (p
= *arg
+ 1; *p
!= NUL
; )
5603 MB_COPY_CHAR(p
, str
);
5612 * Allocate a variable for a List and fill it from "*arg".
5613 * Return OK or FAIL.
5616 get_list_tv(arg
, rettv
, evaluate
)
5632 *arg
= skipwhite(*arg
+ 1);
5633 while (**arg
!= ']' && **arg
!= NUL
)
5635 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5639 item
= listitem_alloc();
5643 item
->li_tv
.v_lock
= 0;
5644 list_append(l
, item
);
5654 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5657 *arg
= skipwhite(*arg
+ 1);
5662 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5669 *arg
= skipwhite(*arg
+ 1);
5672 rettv
->v_type
= VAR_LIST
;
5673 rettv
->vval
.v_list
= l
;
5681 * Allocate an empty header for a list.
5682 * Caller should take care of the reference count.
5689 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5692 /* Prepend the list to the list of lists for garbage collection. */
5693 if (first_list
!= NULL
)
5694 first_list
->lv_used_prev
= l
;
5695 l
->lv_used_prev
= NULL
;
5696 l
->lv_used_next
= first_list
;
5703 * Allocate an empty list for a return value.
5704 * Returns OK or FAIL.
5707 rettv_list_alloc(rettv
)
5710 list_T
*l
= list_alloc();
5715 rettv
->vval
.v_list
= l
;
5716 rettv
->v_type
= VAR_LIST
;
5722 * Unreference a list: decrement the reference count and free it when it
5729 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5734 * Free a list, including all items it points to.
5735 * Ignores the reference count.
5738 list_free(l
, recurse
)
5740 int recurse
; /* Free Lists and Dictionaries recursively. */
5744 /* Remove the list from the list of lists for garbage collection. */
5745 if (l
->lv_used_prev
== NULL
)
5746 first_list
= l
->lv_used_next
;
5748 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5749 if (l
->lv_used_next
!= NULL
)
5750 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5752 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5754 /* Remove the item before deleting it. */
5755 l
->lv_first
= item
->li_next
;
5756 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5757 && item
->li_tv
.v_type
!= VAR_DICT
))
5758 clear_tv(&item
->li_tv
);
5765 * Allocate a list item.
5770 return (listitem_T
*)alloc(sizeof(listitem_T
));
5774 * Free a list item. Also clears the value. Does not notify watchers.
5780 clear_tv(&item
->li_tv
);
5785 * Remove a list item from a List and free it. Also clears the value.
5788 listitem_remove(l
, item
)
5792 list_remove(l
, item
, item
);
5793 listitem_free(item
);
5797 * Get the number of items in a list.
5809 * Return TRUE when two lists have exactly the same values.
5812 list_equal(l1
, l2
, ic
)
5815 int ic
; /* ignore case for strings */
5817 listitem_T
*item1
, *item2
;
5821 if (list_len(l1
) != list_len(l2
))
5824 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5825 item1
!= NULL
&& item2
!= NULL
;
5826 item1
= item1
->li_next
, item2
= item2
->li_next
)
5827 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5829 return item1
== NULL
&& item2
== NULL
;
5832 #if defined(FEAT_PYTHON) || defined(PROTO) || defined(FEAT_GUI_MACVIM)
5834 * Return the dictitem that an entry in a hashtable points to.
5845 * Return TRUE when two dictionaries have exactly the same key/values.
5848 dict_equal(d1
, d2
, ic
)
5851 int ic
; /* ignore case for strings */
5859 if (dict_len(d1
) != dict_len(d2
))
5862 todo
= (int)d1
->dv_hashtab
.ht_used
;
5863 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5865 if (!HASHITEM_EMPTY(hi
))
5867 item2
= dict_find(d2
, hi
->hi_key
, -1);
5870 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5879 * Return TRUE if "tv1" and "tv2" have the same value.
5880 * Compares the items just like "==" would compare them, but strings and
5881 * numbers are different. Floats and numbers are also different.
5884 tv_equal(tv1
, tv2
, ic
)
5887 int ic
; /* ignore case */
5889 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5891 static int recursive
= 0; /* cach recursive loops */
5894 if (tv1
->v_type
!= tv2
->v_type
)
5896 /* Catch lists and dicts that have an endless loop by limiting
5897 * recursiveness to 1000. We guess they are equal then. */
5898 if (recursive
>= 1000)
5901 switch (tv1
->v_type
)
5905 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5911 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5916 return (tv1
->vval
.v_string
!= NULL
5917 && tv2
->vval
.v_string
!= NULL
5918 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5921 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5925 return tv1
->vval
.v_float
== tv2
->vval
.v_float
;
5929 s1
= get_tv_string_buf(tv1
, buf1
);
5930 s2
= get_tv_string_buf(tv2
, buf2
);
5931 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5934 EMSG2(_(e_intern2
), "tv_equal()");
5939 * Locate item with index "n" in list "l" and return it.
5940 * A negative index is counted from the end; -1 is the last item.
5941 * Returns NULL when "n" is out of range.
5954 /* Negative index is relative to the end. */
5958 /* Check for index out of range. */
5959 if (n
< 0 || n
>= l
->lv_len
)
5962 /* When there is a cached index may start search from there. */
5963 if (l
->lv_idx_item
!= NULL
)
5965 if (n
< l
->lv_idx
/ 2)
5967 /* closest to the start of the list */
5971 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
5973 /* closest to the end of the list */
5975 idx
= l
->lv_len
- 1;
5979 /* closest to the cached index */
5980 item
= l
->lv_idx_item
;
5986 if (n
< l
->lv_len
/ 2)
5988 /* closest to the start of the list */
5994 /* closest to the end of the list */
5996 idx
= l
->lv_len
- 1;
6002 /* search forward */
6003 item
= item
->li_next
;
6008 /* search backward */
6009 item
= item
->li_prev
;
6013 /* cache the used index */
6015 l
->lv_idx_item
= item
;
6021 * Get list item "l[idx]" as a number.
6024 list_find_nr(l
, idx
, errorp
)
6027 int *errorp
; /* set to TRUE when something wrong */
6031 li
= list_find(l
, idx
);
6038 return get_tv_number_chk(&li
->li_tv
, errorp
);
6042 * Locate "item" list "l" and return its index.
6043 * Returns -1 when "item" is not in the list.
6046 list_idx_of_item(l
, item
)
6056 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
6064 * Append item "item" to the end of list "l".
6067 list_append(l
, item
)
6071 if (l
->lv_last
== NULL
)
6076 item
->li_prev
= NULL
;
6080 l
->lv_last
->li_next
= item
;
6081 item
->li_prev
= l
->lv_last
;
6085 item
->li_next
= NULL
;
6089 * Append typval_T "tv" to the end of list "l".
6090 * Return FAIL when out of memory.
6093 list_append_tv(l
, tv
)
6097 listitem_T
*li
= listitem_alloc();
6101 copy_tv(tv
, &li
->li_tv
);
6107 * Add a dictionary to a list. Used by getqflist().
6108 * Return FAIL when out of memory.
6111 list_append_dict(list
, dict
)
6115 listitem_T
*li
= listitem_alloc();
6119 li
->li_tv
.v_type
= VAR_DICT
;
6120 li
->li_tv
.v_lock
= 0;
6121 li
->li_tv
.vval
.v_dict
= dict
;
6122 list_append(list
, li
);
6123 ++dict
->dv_refcount
;
6128 * Make a copy of "str" and append it as an item to list "l".
6129 * When "len" >= 0 use "str[len]".
6130 * Returns FAIL when out of memory.
6133 list_append_string(l
, str
, len
)
6138 listitem_T
*li
= listitem_alloc();
6143 li
->li_tv
.v_type
= VAR_STRING
;
6144 li
->li_tv
.v_lock
= 0;
6146 li
->li_tv
.vval
.v_string
= NULL
;
6147 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
6148 : vim_strsave(str
))) == NULL
)
6154 * Append "n" to list "l".
6155 * Returns FAIL when out of memory.
6158 list_append_number(l
, n
)
6164 li
= listitem_alloc();
6167 li
->li_tv
.v_type
= VAR_NUMBER
;
6168 li
->li_tv
.v_lock
= 0;
6169 li
->li_tv
.vval
.v_number
= n
;
6175 * Insert typval_T "tv" in list "l" before "item".
6176 * If "item" is NULL append at the end.
6177 * Return FAIL when out of memory.
6180 list_insert_tv(l
, tv
, item
)
6185 listitem_T
*ni
= listitem_alloc();
6189 copy_tv(tv
, &ni
->li_tv
);
6191 /* Append new item at end of list. */
6195 /* Insert new item before existing item. */
6196 ni
->li_prev
= item
->li_prev
;
6198 if (item
->li_prev
== NULL
)
6205 item
->li_prev
->li_next
= ni
;
6206 l
->lv_idx_item
= NULL
;
6215 * Extend "l1" with "l2".
6216 * If "bef" is NULL append at the end, otherwise insert before this item.
6217 * Returns FAIL when out of memory.
6220 list_extend(l1
, l2
, bef
)
6227 for (item
= l2
->lv_first
; item
!= NULL
; item
= item
->li_next
)
6228 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
6234 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6235 * Return FAIL when out of memory.
6238 list_concat(l1
, l2
, tv
)
6245 /* make a copy of the first list. */
6246 l
= list_copy(l1
, FALSE
, 0);
6249 tv
->v_type
= VAR_LIST
;
6250 tv
->vval
.v_list
= l
;
6252 /* append all items from the second list */
6253 return list_extend(l
, l2
, NULL
);
6257 * Make a copy of list "orig". Shallow if "deep" is FALSE.
6258 * The refcount of the new list is set to 1.
6259 * See item_copy() for "copyID".
6260 * Returns NULL when out of memory.
6263 list_copy(orig
, deep
, copyID
)
6275 copy
= list_alloc();
6280 /* Do this before adding the items, because one of the items may
6281 * refer back to this list. */
6282 orig
->lv_copyID
= copyID
;
6283 orig
->lv_copylist
= copy
;
6285 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
6286 item
= item
->li_next
)
6288 ni
= listitem_alloc();
6293 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
6300 copy_tv(&item
->li_tv
, &ni
->li_tv
);
6301 list_append(copy
, ni
);
6303 ++copy
->lv_refcount
;
6315 * Remove items "item" to "item2" from list "l".
6316 * Does not free the listitem or the value!
6319 list_remove(l
, item
, item2
)
6326 /* notify watchers */
6327 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6330 list_fix_watch(l
, ip
);
6335 if (item2
->li_next
== NULL
)
6336 l
->lv_last
= item
->li_prev
;
6338 item2
->li_next
->li_prev
= item
->li_prev
;
6339 if (item
->li_prev
== NULL
)
6340 l
->lv_first
= item2
->li_next
;
6342 item
->li_prev
->li_next
= item2
->li_next
;
6343 l
->lv_idx_item
= NULL
;
6347 * Return an allocated string with the string representation of a list.
6351 list2string(tv
, copyID
)
6357 if (tv
->vval
.v_list
== NULL
)
6359 ga_init2(&ga
, (int)sizeof(char), 80);
6360 ga_append(&ga
, '[');
6361 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6363 vim_free(ga
.ga_data
);
6366 ga_append(&ga
, ']');
6367 ga_append(&ga
, NUL
);
6368 return (char_u
*)ga
.ga_data
;
6372 * Join list "l" into a string in "*gap", using separator "sep".
6373 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6374 * Return FAIL or OK.
6377 list_join(gap
, l
, sep
, echo
, copyID
)
6386 char_u numbuf
[NUMBUFLEN
];
6390 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6395 ga_concat(gap
, sep
);
6398 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6400 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6411 * Garbage collection for lists and dictionaries.
6413 * We use reference counts to be able to free most items right away when they
6414 * are no longer used. But for composite items it's possible that it becomes
6415 * unused while the reference count is > 0: When there is a recursive
6416 * reference. Example:
6417 * :let l = [1, 2, 3]
6421 * Since this is quite unusual we handle this with garbage collection: every
6422 * once in a while find out which lists and dicts are not referenced from any
6425 * Here is a good reference text about garbage collection (refers to Python
6426 * but it applies to all reference-counting mechanisms):
6427 * http://python.ca/nas/python/gc/
6431 * Do garbage collection for lists and dicts.
6432 * Return TRUE if some memory was freed.
6439 int copyID
= ++current_copyID
;
6444 int did_free
= FALSE
;
6449 /* Only do this once. */
6450 want_garbage_collect
= FALSE
;
6451 may_garbage_collect
= FALSE
;
6452 garbage_collect_at_exit
= FALSE
;
6455 * 1. Go through all accessible variables and mark all lists and dicts
6458 /* script-local variables */
6459 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6460 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6462 /* buffer-local variables */
6463 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6464 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6466 /* window-local variables */
6467 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6468 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6471 /* tabpage-local variables */
6472 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6473 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6476 /* global variables */
6477 set_ref_in_ht(&globvarht
, copyID
);
6479 /* function-local variables */
6480 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6482 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6483 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6487 * 2. Go through the list of dicts and free items without the copyID.
6489 for (dd
= first_dict
; dd
!= NULL
; )
6490 if (dd
->dv_copyID
!= copyID
)
6492 /* Free the Dictionary and ordinary items it contains, but don't
6493 * recurse into Lists and Dictionaries, they will be in the list
6494 * of dicts or list of lists. */
6495 dict_free(dd
, FALSE
);
6498 /* restart, next dict may also have been freed */
6502 dd
= dd
->dv_used_next
;
6505 * 3. Go through the list of lists and free items without the copyID.
6506 * But don't free a list that has a watcher (used in a for loop), these
6507 * are not referenced anywhere.
6509 for (ll
= first_list
; ll
!= NULL
; )
6510 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6512 /* Free the List and ordinary items it contains, but don't recurse
6513 * into Lists and Dictionaries, they will be in the list of dicts
6514 * or list of lists. */
6515 list_free(ll
, FALSE
);
6518 /* restart, next list may also have been freed */
6522 ll
= ll
->lv_used_next
;
6528 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6531 set_ref_in_ht(ht
, copyID
)
6538 todo
= (int)ht
->ht_used
;
6539 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6540 if (!HASHITEM_EMPTY(hi
))
6543 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6548 * Mark all lists and dicts referenced through list "l" with "copyID".
6551 set_ref_in_list(l
, copyID
)
6557 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6558 set_ref_in_item(&li
->li_tv
, copyID
);
6562 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6565 set_ref_in_item(tv
, copyID
)
6575 dd
= tv
->vval
.v_dict
;
6576 if (dd
->dv_copyID
!= copyID
)
6578 /* Didn't see this dict yet. */
6579 dd
->dv_copyID
= copyID
;
6580 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6585 ll
= tv
->vval
.v_list
;
6586 if (ll
->lv_copyID
!= copyID
)
6588 /* Didn't see this list yet. */
6589 ll
->lv_copyID
= copyID
;
6590 set_ref_in_list(ll
, copyID
);
6598 * Allocate an empty header for a dictionary.
6605 d
= (dict_T
*)alloc(sizeof(dict_T
));
6608 /* Add the list to the list of dicts for garbage collection. */
6609 if (first_dict
!= NULL
)
6610 first_dict
->dv_used_prev
= d
;
6611 d
->dv_used_next
= first_dict
;
6612 d
->dv_used_prev
= NULL
;
6615 hash_init(&d
->dv_hashtab
);
6624 * Unreference a Dictionary: decrement the reference count and free it when it
6631 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6636 * Free a Dictionary, including all items it contains.
6637 * Ignores the reference count.
6640 dict_free(d
, recurse
)
6642 int recurse
; /* Free Lists and Dictionaries recursively. */
6648 /* Remove the dict from the list of dicts for garbage collection. */
6649 if (d
->dv_used_prev
== NULL
)
6650 first_dict
= d
->dv_used_next
;
6652 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6653 if (d
->dv_used_next
!= NULL
)
6654 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6656 /* Lock the hashtab, we don't want it to resize while freeing items. */
6657 hash_lock(&d
->dv_hashtab
);
6658 todo
= (int)d
->dv_hashtab
.ht_used
;
6659 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6661 if (!HASHITEM_EMPTY(hi
))
6663 /* Remove the item before deleting it, just in case there is
6664 * something recursive causing trouble. */
6666 hash_remove(&d
->dv_hashtab
, hi
);
6667 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6668 && di
->di_tv
.v_type
!= VAR_DICT
))
6669 clear_tv(&di
->di_tv
);
6674 hash_clear(&d
->dv_hashtab
);
6679 * Allocate a Dictionary item.
6680 * The "key" is copied to the new item.
6681 * Note that the value of the item "di_tv" still needs to be initialized!
6682 * Returns NULL when out of memory.
6690 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6693 STRCPY(di
->di_key
, key
);
6700 * Make a copy of a Dictionary item.
6708 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6709 + STRLEN(org
->di_key
)));
6712 STRCPY(di
->di_key
, org
->di_key
);
6714 copy_tv(&org
->di_tv
, &di
->di_tv
);
6720 * Remove item "item" from Dictionary "dict" and free it.
6723 dictitem_remove(dict
, item
)
6729 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6730 if (HASHITEM_EMPTY(hi
))
6731 EMSG2(_(e_intern2
), "dictitem_remove()");
6733 hash_remove(&dict
->dv_hashtab
, hi
);
6734 dictitem_free(item
);
6738 * Free a dict item. Also clears the value.
6744 clear_tv(&item
->di_tv
);
6749 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6750 * The refcount of the new dict is set to 1.
6751 * See item_copy() for "copyID".
6752 * Returns NULL when out of memory.
6755 dict_copy(orig
, deep
, copyID
)
6768 copy
= dict_alloc();
6773 orig
->dv_copyID
= copyID
;
6774 orig
->dv_copydict
= copy
;
6776 todo
= (int)orig
->dv_hashtab
.ht_used
;
6777 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6779 if (!HASHITEM_EMPTY(hi
))
6783 di
= dictitem_alloc(hi
->hi_key
);
6788 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6796 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6797 if (dict_add(copy
, di
) == FAIL
)
6805 ++copy
->dv_refcount
;
6817 * Add item "item" to Dictionary "d".
6818 * Returns FAIL when out of memory and when key already existed.
6825 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6829 * Add a number or string entry to dictionary "d".
6830 * When "str" is NULL use number "nr", otherwise use "str".
6831 * Returns FAIL when out of memory and when key already exists.
6834 dict_add_nr_str(d
, key
, nr
, str
)
6842 item
= dictitem_alloc((char_u
*)key
);
6845 item
->di_tv
.v_lock
= 0;
6848 item
->di_tv
.v_type
= VAR_NUMBER
;
6849 item
->di_tv
.vval
.v_number
= nr
;
6853 item
->di_tv
.v_type
= VAR_STRING
;
6854 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6856 if (dict_add(d
, item
) == FAIL
)
6858 dictitem_free(item
);
6865 * Get the number of items in a Dictionary.
6873 return (long)d
->dv_hashtab
.ht_used
;
6877 * Find item "key[len]" in Dictionary "d".
6878 * If "len" is negative use strlen(key).
6879 * Returns NULL when not found.
6882 dict_find(d
, key
, len
)
6888 char_u buf
[AKEYLEN
];
6890 char_u
*tofree
= NULL
;
6895 else if (len
>= AKEYLEN
)
6897 tofree
= akey
= vim_strnsave(key
, len
);
6903 /* Avoid a malloc/free by using buf[]. */
6904 vim_strncpy(buf
, key
, len
);
6908 hi
= hash_find(&d
->dv_hashtab
, akey
);
6910 if (HASHITEM_EMPTY(hi
))
6916 * Get a string item from a dictionary.
6917 * When "save" is TRUE allocate memory for it.
6918 * Returns NULL if the entry doesn't exist or out of memory.
6921 get_dict_string(d
, key
, save
)
6929 di
= dict_find(d
, key
, -1);
6932 s
= get_tv_string(&di
->di_tv
);
6933 if (save
&& s
!= NULL
)
6939 * Get a number item from a dictionary.
6940 * Returns 0 if the entry doesn't exist or out of memory.
6943 get_dict_number(d
, key
)
6949 di
= dict_find(d
, key
, -1);
6952 return get_tv_number(&di
->di_tv
);
6956 * Return an allocated string with the string representation of a Dictionary.
6960 dict2string(tv
, copyID
)
6967 char_u numbuf
[NUMBUFLEN
];
6973 if ((d
= tv
->vval
.v_dict
) == NULL
)
6975 ga_init2(&ga
, (int)sizeof(char), 80);
6976 ga_append(&ga
, '{');
6978 todo
= (int)d
->dv_hashtab
.ht_used
;
6979 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6981 if (!HASHITEM_EMPTY(hi
))
6988 ga_concat(&ga
, (char_u
*)", ");
6990 tofree
= string_quote(hi
->hi_key
, FALSE
);
6993 ga_concat(&ga
, tofree
);
6996 ga_concat(&ga
, (char_u
*)": ");
6997 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
7007 vim_free(ga
.ga_data
);
7011 ga_append(&ga
, '}');
7012 ga_append(&ga
, NUL
);
7013 return (char_u
*)ga
.ga_data
;
7017 * Allocate a variable for a Dictionary and fill it from "*arg".
7018 * Return OK or FAIL. Returns NOTDONE for {expr}.
7021 get_dict_tv(arg
, rettv
, evaluate
)
7031 char_u
*start
= skipwhite(*arg
+ 1);
7032 char_u buf
[NUMBUFLEN
];
7035 * First check if it's not a curly-braces thing: {expr}.
7036 * Must do this without evaluating, otherwise a function may be called
7037 * twice. Unfortunately this means we need to call eval1() twice for the
7039 * But {} is an empty Dictionary.
7043 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
7055 tvkey
.v_type
= VAR_UNKNOWN
;
7056 tv
.v_type
= VAR_UNKNOWN
;
7058 *arg
= skipwhite(*arg
+ 1);
7059 while (**arg
!= '}' && **arg
!= NUL
)
7061 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
7065 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
7071 key
= get_tv_string_buf_chk(&tvkey
, buf
);
7072 if (key
== NULL
|| *key
== NUL
)
7074 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7076 EMSG(_(e_emptykey
));
7082 *arg
= skipwhite(*arg
+ 1);
7083 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
7091 item
= dict_find(d
, key
, -1);
7094 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
7099 item
= dictitem_alloc(key
);
7104 item
->di_tv
.v_lock
= 0;
7105 if (dict_add(d
, item
) == FAIL
)
7106 dictitem_free(item
);
7114 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
7117 *arg
= skipwhite(*arg
+ 1);
7122 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
7129 *arg
= skipwhite(*arg
+ 1);
7132 rettv
->v_type
= VAR_DICT
;
7133 rettv
->vval
.v_dict
= d
;
7141 * Return a string with the string representation of a variable.
7142 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7143 * "numbuf" is used for a number.
7144 * Does not put quotes around strings, as ":echo" displays values.
7145 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7149 echo_string(tv
, tofree
, numbuf
, copyID
)
7155 static int recurse
= 0;
7158 if (recurse
>= DICT_MAXNEST
)
7160 EMSG(_("E724: variable nested too deep for displaying"));
7170 r
= tv
->vval
.v_string
;
7174 if (tv
->vval
.v_list
== NULL
)
7179 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
7182 r
= (char_u
*)"[...]";
7186 tv
->vval
.v_list
->lv_copyID
= copyID
;
7187 *tofree
= list2string(tv
, copyID
);
7193 if (tv
->vval
.v_dict
== NULL
)
7198 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
7201 r
= (char_u
*)"{...}";
7205 tv
->vval
.v_dict
->dv_copyID
= copyID
;
7206 *tofree
= dict2string(tv
, copyID
);
7214 r
= get_tv_string_buf(tv
, numbuf
);
7220 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
->vval
.v_float
);
7226 EMSG2(_(e_intern2
), "echo_string()");
7235 * Return a string with the string representation of a variable.
7236 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7237 * "numbuf" is used for a number.
7238 * Puts quotes around strings, so that they can be parsed back by eval().
7242 tv2string(tv
, tofree
, numbuf
, copyID
)
7251 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
7254 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
7259 vim_snprintf((char *)numbuf
, NUMBUFLEN
- 1, "%g", tv
->vval
.v_float
);
7267 EMSG2(_(e_intern2
), "tv2string()");
7269 return echo_string(tv
, tofree
, numbuf
, copyID
);
7273 * Return string "str" in ' quotes, doubling ' characters.
7274 * If "str" is NULL an empty string is assumed.
7275 * If "function" is TRUE make it function('string').
7278 string_quote(str
, function
)
7285 len
= (function
? 13 : 3);
7288 len
+= (unsigned)STRLEN(str
);
7289 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
7298 STRCPY(r
, "function('");
7304 for (p
= str
; *p
!= NUL
; )
7320 * Convert the string "text" to a floating point number.
7321 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7322 * this always uses a decimal point.
7323 * Returns the length of the text that was consumed.
7326 string2float(text
, value
)
7328 float_T
*value
; /* result stored here */
7330 char *s
= (char *)text
;
7335 return (int)((char_u
*)s
- text
);
7340 * Get the value of an environment variable.
7341 * "arg" is pointing to the '$'. It is advanced to after the name.
7342 * If the environment variable was not set, silently assume it is empty.
7346 get_env_tv(arg
, rettv
, evaluate
)
7351 char_u
*string
= NULL
;
7355 int mustfree
= FALSE
;
7359 len
= get_env_len(arg
);
7366 /* first try vim_getenv(), fast for normal environment vars */
7367 string
= vim_getenv(name
, &mustfree
);
7368 if (string
!= NULL
&& *string
!= NUL
)
7371 string
= vim_strsave(string
);
7378 /* next try expanding things like $VIM and ${HOME} */
7379 string
= expand_env_save(name
- 1);
7380 if (string
!= NULL
&& *string
== '$')
7388 rettv
->v_type
= VAR_STRING
;
7389 rettv
->vval
.v_string
= string
;
7396 * Array with names and number of arguments of all internal functions
7397 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7401 char *f_name
; /* function name */
7402 char f_min_argc
; /* minimal number of arguments */
7403 char f_max_argc
; /* maximal number of arguments */
7404 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7405 /* implementation of function */
7409 {"abs", 1, 1, f_abs
},
7411 {"add", 2, 2, f_add
},
7412 {"append", 2, 2, f_append
},
7413 {"argc", 0, 0, f_argc
},
7414 {"argidx", 0, 0, f_argidx
},
7415 {"argv", 0, 1, f_argv
},
7417 {"atan", 1, 1, f_atan
},
7419 {"browse", 4, 4, f_browse
},
7420 {"browsedir", 2, 2, f_browsedir
},
7421 {"bufexists", 1, 1, f_bufexists
},
7422 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7423 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7424 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7425 {"buflisted", 1, 1, f_buflisted
},
7426 {"bufloaded", 1, 1, f_bufloaded
},
7427 {"bufname", 1, 1, f_bufname
},
7428 {"bufnr", 1, 2, f_bufnr
},
7429 {"bufwinnr", 1, 1, f_bufwinnr
},
7430 {"byte2line", 1, 1, f_byte2line
},
7431 {"byteidx", 2, 2, f_byteidx
},
7432 {"call", 2, 3, f_call
},
7434 {"ceil", 1, 1, f_ceil
},
7436 {"changenr", 0, 0, f_changenr
},
7437 {"char2nr", 1, 1, f_char2nr
},
7438 {"cindent", 1, 1, f_cindent
},
7439 {"clearmatches", 0, 0, f_clearmatches
},
7440 {"col", 1, 1, f_col
},
7441 #if defined(FEAT_INS_EXPAND)
7442 {"complete", 2, 2, f_complete
},
7443 {"complete_add", 1, 1, f_complete_add
},
7444 {"complete_check", 0, 0, f_complete_check
},
7446 {"confirm", 1, 4, f_confirm
},
7447 {"copy", 1, 1, f_copy
},
7449 {"cos", 1, 1, f_cos
},
7451 {"count", 2, 4, f_count
},
7452 {"cscope_connection",0,3, f_cscope_connection
},
7453 {"cursor", 1, 3, f_cursor
},
7454 {"deepcopy", 1, 2, f_deepcopy
},
7455 {"delete", 1, 1, f_delete
},
7456 {"did_filetype", 0, 0, f_did_filetype
},
7457 {"diff_filler", 1, 1, f_diff_filler
},
7458 {"diff_hlID", 2, 2, f_diff_hlID
},
7459 {"empty", 1, 1, f_empty
},
7460 {"escape", 2, 2, f_escape
},
7461 {"eval", 1, 1, f_eval
},
7462 {"eventhandler", 0, 0, f_eventhandler
},
7463 {"executable", 1, 1, f_executable
},
7464 {"exists", 1, 1, f_exists
},
7465 {"expand", 1, 2, f_expand
},
7466 {"extend", 2, 3, f_extend
},
7467 {"feedkeys", 1, 2, f_feedkeys
},
7468 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7469 {"filereadable", 1, 1, f_filereadable
},
7470 {"filewritable", 1, 1, f_filewritable
},
7471 {"filter", 2, 2, f_filter
},
7472 {"finddir", 1, 3, f_finddir
},
7473 {"findfile", 1, 3, f_findfile
},
7475 {"float2nr", 1, 1, f_float2nr
},
7476 {"floor", 1, 1, f_floor
},
7478 {"fnameescape", 1, 1, f_fnameescape
},
7479 {"fnamemodify", 2, 2, f_fnamemodify
},
7480 {"foldclosed", 1, 1, f_foldclosed
},
7481 {"foldclosedend", 1, 1, f_foldclosedend
},
7482 {"foldlevel", 1, 1, f_foldlevel
},
7483 {"foldtext", 0, 0, f_foldtext
},
7484 {"foldtextresult", 1, 1, f_foldtextresult
},
7485 {"foreground", 0, 0, f_foreground
},
7486 {"function", 1, 1, f_function
},
7487 {"garbagecollect", 0, 1, f_garbagecollect
},
7488 {"get", 2, 3, f_get
},
7489 {"getbufline", 2, 3, f_getbufline
},
7490 {"getbufvar", 2, 2, f_getbufvar
},
7491 {"getchar", 0, 1, f_getchar
},
7492 {"getcharmod", 0, 0, f_getcharmod
},
7493 {"getcmdline", 0, 0, f_getcmdline
},
7494 {"getcmdpos", 0, 0, f_getcmdpos
},
7495 {"getcmdtype", 0, 0, f_getcmdtype
},
7496 {"getcwd", 0, 0, f_getcwd
},
7497 {"getfontname", 0, 1, f_getfontname
},
7498 {"getfperm", 1, 1, f_getfperm
},
7499 {"getfsize", 1, 1, f_getfsize
},
7500 {"getftime", 1, 1, f_getftime
},
7501 {"getftype", 1, 1, f_getftype
},
7502 {"getline", 1, 2, f_getline
},
7503 {"getloclist", 1, 1, f_getqflist
},
7504 {"getmatches", 0, 0, f_getmatches
},
7505 {"getpid", 0, 0, f_getpid
},
7506 {"getpos", 1, 1, f_getpos
},
7507 {"getqflist", 0, 0, f_getqflist
},
7508 {"getreg", 0, 2, f_getreg
},
7509 {"getregtype", 0, 1, f_getregtype
},
7510 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7511 {"getwinposx", 0, 0, f_getwinposx
},
7512 {"getwinposy", 0, 0, f_getwinposy
},
7513 {"getwinvar", 2, 2, f_getwinvar
},
7514 {"glob", 1, 1, f_glob
},
7515 {"globpath", 2, 2, f_globpath
},
7516 {"has", 1, 1, f_has
},
7517 {"has_key", 2, 2, f_has_key
},
7518 {"haslocaldir", 0, 0, f_haslocaldir
},
7519 {"hasmapto", 1, 3, f_hasmapto
},
7520 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7521 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7522 {"histadd", 2, 2, f_histadd
},
7523 {"histdel", 1, 2, f_histdel
},
7524 {"histget", 1, 2, f_histget
},
7525 {"histnr", 1, 1, f_histnr
},
7526 {"hlID", 1, 1, f_hlID
},
7527 {"hlexists", 1, 1, f_hlexists
},
7528 {"hostname", 0, 0, f_hostname
},
7529 {"iconv", 3, 3, f_iconv
},
7530 {"indent", 1, 1, f_indent
},
7531 {"index", 2, 4, f_index
},
7532 {"input", 1, 3, f_input
},
7533 {"inputdialog", 1, 3, f_inputdialog
},
7534 {"inputlist", 1, 1, f_inputlist
},
7535 {"inputrestore", 0, 0, f_inputrestore
},
7536 {"inputsave", 0, 0, f_inputsave
},
7537 {"inputsecret", 1, 2, f_inputsecret
},
7538 {"insert", 2, 3, f_insert
},
7539 {"isdirectory", 1, 1, f_isdirectory
},
7540 {"islocked", 1, 1, f_islocked
},
7541 {"items", 1, 1, f_items
},
7542 {"join", 1, 2, f_join
},
7543 {"keys", 1, 1, f_keys
},
7544 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7545 {"len", 1, 1, f_len
},
7546 {"libcall", 3, 3, f_libcall
},
7547 {"libcallnr", 3, 3, f_libcallnr
},
7548 {"line", 1, 1, f_line
},
7549 {"line2byte", 1, 1, f_line2byte
},
7550 {"lispindent", 1, 1, f_lispindent
},
7551 {"localtime", 0, 0, f_localtime
},
7553 {"log10", 1, 1, f_log10
},
7555 {"map", 2, 2, f_map
},
7556 {"maparg", 1, 3, f_maparg
},
7557 {"mapcheck", 1, 3, f_mapcheck
},
7558 {"match", 2, 4, f_match
},
7559 {"matchadd", 2, 4, f_matchadd
},
7560 {"matcharg", 1, 1, f_matcharg
},
7561 {"matchdelete", 1, 1, f_matchdelete
},
7562 {"matchend", 2, 4, f_matchend
},
7563 {"matchlist", 2, 4, f_matchlist
},
7564 {"matchstr", 2, 4, f_matchstr
},
7565 {"max", 1, 1, f_max
},
7566 {"min", 1, 1, f_min
},
7568 {"mkdir", 1, 3, f_mkdir
},
7570 {"mode", 0, 1, f_mode
},
7571 {"nextnonblank", 1, 1, f_nextnonblank
},
7572 {"nr2char", 1, 1, f_nr2char
},
7573 {"pathshorten", 1, 1, f_pathshorten
},
7575 {"pow", 2, 2, f_pow
},
7577 {"prevnonblank", 1, 1, f_prevnonblank
},
7578 {"printf", 2, 19, f_printf
},
7579 {"pumvisible", 0, 0, f_pumvisible
},
7580 {"range", 1, 3, f_range
},
7581 {"readfile", 1, 3, f_readfile
},
7582 {"reltime", 0, 2, f_reltime
},
7583 {"reltimestr", 1, 1, f_reltimestr
},
7584 {"remote_expr", 2, 3, f_remote_expr
},
7585 {"remote_foreground", 1, 1, f_remote_foreground
},
7586 {"remote_peek", 1, 2, f_remote_peek
},
7587 {"remote_read", 1, 1, f_remote_read
},
7588 {"remote_send", 2, 3, f_remote_send
},
7589 {"remove", 2, 3, f_remove
},
7590 {"rename", 2, 2, f_rename
},
7591 {"repeat", 2, 2, f_repeat
},
7592 {"resolve", 1, 1, f_resolve
},
7593 {"reverse", 1, 1, f_reverse
},
7595 {"round", 1, 1, f_round
},
7597 {"search", 1, 4, f_search
},
7598 {"searchdecl", 1, 3, f_searchdecl
},
7599 {"searchpair", 3, 7, f_searchpair
},
7600 {"searchpairpos", 3, 7, f_searchpairpos
},
7601 {"searchpos", 1, 4, f_searchpos
},
7602 {"server2client", 2, 2, f_server2client
},
7603 {"serverlist", 0, 0, f_serverlist
},
7604 {"setbufvar", 3, 3, f_setbufvar
},
7605 {"setcmdpos", 1, 1, f_setcmdpos
},
7606 {"setline", 2, 2, f_setline
},
7607 {"setloclist", 2, 3, f_setloclist
},
7608 {"setmatches", 1, 1, f_setmatches
},
7609 {"setpos", 2, 2, f_setpos
},
7610 {"setqflist", 1, 2, f_setqflist
},
7611 {"setreg", 2, 3, f_setreg
},
7612 {"settabwinvar", 4, 4, f_settabwinvar
},
7613 {"setwinvar", 3, 3, f_setwinvar
},
7614 {"shellescape", 1, 1, f_shellescape
},
7615 {"simplify", 1, 1, f_simplify
},
7617 {"sin", 1, 1, f_sin
},
7619 {"sort", 1, 2, f_sort
},
7620 {"soundfold", 1, 1, f_soundfold
},
7621 {"spellbadword", 0, 1, f_spellbadword
},
7622 {"spellsuggest", 1, 3, f_spellsuggest
},
7623 {"split", 1, 3, f_split
},
7625 {"sqrt", 1, 1, f_sqrt
},
7626 {"str2float", 1, 1, f_str2float
},
7628 {"str2nr", 1, 2, f_str2nr
},
7629 #ifdef HAVE_STRFTIME
7630 {"strftime", 1, 2, f_strftime
},
7632 {"stridx", 2, 3, f_stridx
},
7633 {"string", 1, 1, f_string
},
7634 {"strlen", 1, 1, f_strlen
},
7635 {"strpart", 2, 3, f_strpart
},
7636 {"strridx", 2, 3, f_strridx
},
7637 {"strtrans", 1, 1, f_strtrans
},
7638 {"submatch", 1, 1, f_submatch
},
7639 {"substitute", 4, 4, f_substitute
},
7640 {"synID", 3, 3, f_synID
},
7641 {"synIDattr", 2, 3, f_synIDattr
},
7642 {"synIDtrans", 1, 1, f_synIDtrans
},
7643 {"synstack", 2, 2, f_synstack
},
7644 {"system", 1, 2, f_system
},
7645 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7646 {"tabpagenr", 0, 1, f_tabpagenr
},
7647 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7648 {"tagfiles", 0, 0, f_tagfiles
},
7649 {"taglist", 1, 1, f_taglist
},
7650 {"tempname", 0, 0, f_tempname
},
7651 {"test", 1, 1, f_test
},
7652 {"tolower", 1, 1, f_tolower
},
7653 {"toupper", 1, 1, f_toupper
},
7656 {"trunc", 1, 1, f_trunc
},
7658 {"type", 1, 1, f_type
},
7659 {"values", 1, 1, f_values
},
7660 {"virtcol", 1, 1, f_virtcol
},
7661 {"visualmode", 0, 1, f_visualmode
},
7662 {"winbufnr", 1, 1, f_winbufnr
},
7663 {"wincol", 0, 0, f_wincol
},
7664 {"winheight", 1, 1, f_winheight
},
7665 {"winline", 0, 0, f_winline
},
7666 {"winnr", 0, 1, f_winnr
},
7667 {"winrestcmd", 0, 0, f_winrestcmd
},
7668 {"winrestview", 1, 1, f_winrestview
},
7669 {"winsaveview", 0, 0, f_winsaveview
},
7670 {"winwidth", 1, 1, f_winwidth
},
7671 {"writefile", 2, 3, f_writefile
},
7674 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7677 * Function given to ExpandGeneric() to obtain the list of internal
7678 * or user defined function names.
7681 get_function_name(xp
, idx
)
7685 static int intidx
= -1;
7692 name
= get_user_func_name(xp
, idx
);
7696 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7698 STRCPY(IObuff
, functions
[intidx
].f_name
);
7699 STRCAT(IObuff
, "(");
7700 if (functions
[intidx
].f_max_argc
== 0)
7701 STRCAT(IObuff
, ")");
7709 * Function given to ExpandGeneric() to obtain the list of internal or
7710 * user defined variable or function names.
7714 get_expr_name(xp
, idx
)
7718 static int intidx
= -1;
7725 name
= get_function_name(xp
, idx
);
7729 return get_user_var_name(xp
, ++intidx
);
7732 #endif /* FEAT_CMDL_COMPL */
7735 * Find internal function in table above.
7736 * Return index, or -1 if not found
7739 find_internal_func(name
)
7740 char_u
*name
; /* name of the function */
7743 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7748 * Find the function name in the table. Binary search.
7750 while (first
<= last
)
7752 x
= first
+ ((unsigned)(last
- first
) >> 1);
7753 cmp
= STRCMP(name
, functions
[x
].f_name
);
7765 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7766 * name it contains, otherwise return "name".
7769 deref_func_name(name
, lenp
)
7778 v
= find_var(name
, NULL
);
7780 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7782 if (v
->di_tv
.vval
.v_string
== NULL
)
7785 return (char_u
*)""; /* just in case */
7787 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7788 return v
->di_tv
.vval
.v_string
;
7795 * Allocate a variable for the result of a function.
7796 * Return OK or FAIL.
7799 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7801 char_u
*name
; /* name of the function */
7802 int len
; /* length of "name" */
7804 char_u
**arg
; /* argument, pointing to the '(' */
7805 linenr_T firstline
; /* first line of range */
7806 linenr_T lastline
; /* last line of range */
7807 int *doesrange
; /* return: function handled range */
7809 dict_T
*selfdict
; /* Dictionary for "self" */
7813 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7814 int argcount
= 0; /* number of arguments found */
7817 * Get the arguments.
7820 while (argcount
< MAX_FUNC_ARGS
)
7822 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7823 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7825 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7840 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7841 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7842 else if (!aborting())
7844 if (argcount
== MAX_FUNC_ARGS
)
7845 emsg_funcname("E740: Too many arguments for function %s", name
);
7847 emsg_funcname("E116: Invalid arguments for function %s", name
);
7850 while (--argcount
>= 0)
7851 clear_tv(&argvars
[argcount
]);
7853 *arg
= skipwhite(argp
);
7859 * Call a function with its resolved parameters
7860 * Return OK when the function can't be called, FAIL otherwise.
7861 * Also returns OK when an error was encountered while executing the function.
7864 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7865 doesrange
, evaluate
, selfdict
)
7866 char_u
*name
; /* name of the function */
7867 int len
; /* length of "name" */
7868 typval_T
*rettv
; /* return value goes here */
7869 int argcount
; /* number of "argvars" */
7870 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7871 PLUS ONE elements! */
7872 linenr_T firstline
; /* first line of range */
7873 linenr_T lastline
; /* last line of range */
7874 int *doesrange
; /* return: function handled range */
7876 dict_T
*selfdict
; /* Dictionary for "self" */
7879 #define ERROR_UNKNOWN 0
7880 #define ERROR_TOOMANY 1
7881 #define ERROR_TOOFEW 2
7882 #define ERROR_SCRIPT 3
7883 #define ERROR_DICT 4
7884 #define ERROR_NONE 5
7885 #define ERROR_OTHER 6
7886 int error
= ERROR_NONE
;
7891 #define FLEN_FIXED 40
7892 char_u fname_buf
[FLEN_FIXED
+ 1];
7896 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7897 * Change <SNR>123_name() to K_SNR 123_name().
7898 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7902 llen
= eval_fname_script(name
);
7905 fname_buf
[0] = K_SPECIAL
;
7906 fname_buf
[1] = KS_EXTRA
;
7907 fname_buf
[2] = (int)KE_SNR
;
7909 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
7911 if (current_SID
<= 0)
7912 error
= ERROR_SCRIPT
;
7915 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
7916 i
= (int)STRLEN(fname_buf
);
7919 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
7921 STRCPY(fname_buf
+ i
, name
+ llen
);
7926 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
7928 error
= ERROR_OTHER
;
7931 mch_memmove(fname
, fname_buf
, (size_t)i
);
7932 STRCPY(fname
+ i
, name
+ llen
);
7942 /* execute the function if no errors detected and executing */
7943 if (evaluate
&& error
== ERROR_NONE
)
7945 rettv
->v_type
= VAR_NUMBER
; /* default is number rettv */
7946 error
= ERROR_UNKNOWN
;
7948 if (!builtin_function(fname
))
7951 * User defined function.
7953 fp
= find_func(fname
);
7956 /* Trigger FuncUndefined event, may load the function. */
7958 && apply_autocmds(EVENT_FUNCUNDEFINED
,
7959 fname
, fname
, TRUE
, NULL
)
7962 /* executed an autocommand, search for the function again */
7963 fp
= find_func(fname
);
7966 /* Try loading a package. */
7967 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
7969 /* loaded a package, search for the function again */
7970 fp
= find_func(fname
);
7975 if (fp
->uf_flags
& FC_RANGE
)
7977 if (argcount
< fp
->uf_args
.ga_len
)
7978 error
= ERROR_TOOFEW
;
7979 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
7980 error
= ERROR_TOOMANY
;
7981 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
7986 * Call the user function.
7987 * Save and restore search patterns, script variables and
7990 save_search_patterns();
7993 call_user_func(fp
, argcount
, argvars
, rettv
,
7994 firstline
, lastline
,
7995 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
7996 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
7997 && fp
->uf_refcount
<= 0)
7998 /* Function was unreferenced while being used, free it
8002 restore_search_patterns();
8010 * Find the function name in the table, call its implementation.
8012 i
= find_internal_func(fname
);
8015 if (argcount
< functions
[i
].f_min_argc
)
8016 error
= ERROR_TOOFEW
;
8017 else if (argcount
> functions
[i
].f_max_argc
)
8018 error
= ERROR_TOOMANY
;
8021 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
8022 functions
[i
].f_func(argvars
, rettv
);
8028 * The function call (or "FuncUndefined" autocommand sequence) might
8029 * have been aborted by an error, an interrupt, or an explicitly thrown
8030 * exception that has not been caught so far. This situation can be
8031 * tested for by calling aborting(). For an error in an internal
8032 * function or for the "E132" error in call_user_func(), however, the
8033 * throw point at which the "force_abort" flag (temporarily reset by
8034 * emsg()) is normally updated has not been reached yet. We need to
8035 * update that flag first to make aborting() reliable.
8037 update_force_abort();
8039 if (error
== ERROR_NONE
)
8043 * Report an error unless the argument evaluation or function call has been
8044 * cancelled due to an aborting error, an interrupt, or an exception.
8051 emsg_funcname(N_("E117: Unknown function: %s"), name
);
8054 emsg_funcname(e_toomanyarg
, name
);
8057 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
8061 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
8065 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
8072 if (fname
!= name
&& fname
!= fname_buf
)
8079 * Give an error message with a function name. Handle <SNR> things.
8082 emsg_funcname(ermsg
, name
)
8088 if (*name
== K_SPECIAL
)
8089 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
8097 /*********************************************
8098 * Implementation of the built-in functions
8103 * "abs(expr)" function
8106 f_abs(argvars
, rettv
)
8110 if (argvars
[0].v_type
== VAR_FLOAT
)
8112 rettv
->v_type
= VAR_FLOAT
;
8113 rettv
->vval
.v_float
= fabs(argvars
[0].vval
.v_float
);
8120 n
= get_tv_number_chk(&argvars
[0], &error
);
8122 rettv
->vval
.v_number
= -1;
8124 rettv
->vval
.v_number
= n
;
8126 rettv
->vval
.v_number
= -n
;
8132 * "add(list, item)" function
8135 f_add(argvars
, rettv
)
8141 rettv
->vval
.v_number
= 1; /* Default: Failed */
8142 if (argvars
[0].v_type
== VAR_LIST
)
8144 if ((l
= argvars
[0].vval
.v_list
) != NULL
8145 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
8146 && list_append_tv(l
, &argvars
[1]) == OK
)
8147 copy_tv(&argvars
[0], rettv
);
8154 * "append(lnum, string/list)" function
8157 f_append(argvars
, rettv
)
8164 listitem_T
*li
= NULL
;
8168 lnum
= get_tv_lnum(argvars
);
8170 && lnum
<= curbuf
->b_ml
.ml_line_count
8171 && u_save(lnum
, lnum
+ 1) == OK
)
8173 if (argvars
[1].v_type
== VAR_LIST
)
8175 l
= argvars
[1].vval
.v_list
;
8180 rettv
->vval
.v_number
= 0; /* Default: Success */
8184 tv
= &argvars
[1]; /* append a string */
8185 else if (li
== NULL
)
8186 break; /* end of list */
8188 tv
= &li
->li_tv
; /* append item from list */
8189 line
= get_tv_string_chk(tv
);
8190 if (line
== NULL
) /* type error */
8192 rettv
->vval
.v_number
= 1; /* Failed */
8195 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
8202 appended_lines_mark(lnum
, added
);
8203 if (curwin
->w_cursor
.lnum
> lnum
)
8204 curwin
->w_cursor
.lnum
+= added
;
8207 rettv
->vval
.v_number
= 1; /* Failed */
8215 f_argc(argvars
, rettv
)
8219 rettv
->vval
.v_number
= ARGCOUNT
;
8223 * "argidx()" function
8227 f_argidx(argvars
, rettv
)
8231 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
8235 * "argv(nr)" function
8238 f_argv(argvars
, rettv
)
8244 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
8246 idx
= get_tv_number_chk(&argvars
[0], NULL
);
8247 if (idx
>= 0 && idx
< ARGCOUNT
)
8248 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
8250 rettv
->vval
.v_string
= NULL
;
8251 rettv
->v_type
= VAR_STRING
;
8253 else if (rettv_list_alloc(rettv
) == OK
)
8254 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
8255 list_append_string(rettv
->vval
.v_list
,
8256 alist_name(&ARGLIST
[idx
]), -1);
8260 static int get_float_arg
__ARGS((typval_T
*argvars
, float_T
*f
));
8263 * Get the float value of "argvars[0]" into "f".
8264 * Returns FAIL when the argument is not a Number or Float.
8267 get_float_arg(argvars
, f
)
8271 if (argvars
[0].v_type
== VAR_FLOAT
)
8273 *f
= argvars
[0].vval
.v_float
;
8276 if (argvars
[0].v_type
== VAR_NUMBER
)
8278 *f
= (float_T
)argvars
[0].vval
.v_number
;
8281 EMSG(_("E808: Number or Float required"));
8289 f_atan(argvars
, rettv
)
8295 rettv
->v_type
= VAR_FLOAT
;
8296 if (get_float_arg(argvars
, &f
) == OK
)
8297 rettv
->vval
.v_float
= atan(f
);
8299 rettv
->vval
.v_float
= 0.0;
8304 * "browse(save, title, initdir, default)" function
8308 f_browse(argvars
, rettv
)
8317 char_u buf
[NUMBUFLEN
];
8318 char_u buf2
[NUMBUFLEN
];
8321 save
= get_tv_number_chk(&argvars
[0], &error
);
8322 title
= get_tv_string_chk(&argvars
[1]);
8323 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
8324 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8326 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
8327 rettv
->vval
.v_string
= NULL
;
8329 rettv
->vval
.v_string
=
8330 do_browse(save
? BROWSE_SAVE
: 0,
8331 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
8333 rettv
->vval
.v_string
= NULL
;
8335 rettv
->v_type
= VAR_STRING
;
8339 * "browsedir(title, initdir)" function
8343 f_browsedir(argvars
, rettv
)
8350 char_u buf
[NUMBUFLEN
];
8352 title
= get_tv_string_chk(&argvars
[0]);
8353 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
8355 if (title
== NULL
|| initdir
== NULL
)
8356 rettv
->vval
.v_string
= NULL
;
8358 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
8359 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
8361 rettv
->vval
.v_string
= NULL
;
8363 rettv
->v_type
= VAR_STRING
;
8366 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
8369 * Find a buffer by number or exact name.
8377 if (avar
->v_type
== VAR_NUMBER
)
8378 buf
= buflist_findnr((int)avar
->vval
.v_number
);
8379 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
8381 buf
= buflist_findname_exp(avar
->vval
.v_string
);
8384 /* No full path name match, try a match with a URL or a "nofile"
8385 * buffer, these don't use the full path. */
8386 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
8387 if (buf
->b_fname
!= NULL
8388 && (path_with_url(buf
->b_fname
)
8389 #ifdef FEAT_QUICKFIX
8393 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
8401 * "bufexists(expr)" function
8404 f_bufexists(argvars
, rettv
)
8408 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
8412 * "buflisted(expr)" function
8415 f_buflisted(argvars
, rettv
)
8421 buf
= find_buffer(&argvars
[0]);
8422 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
8426 * "bufloaded(expr)" function
8429 f_bufloaded(argvars
, rettv
)
8435 buf
= find_buffer(&argvars
[0]);
8436 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
8439 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
8442 * Get buffer by number or pattern.
8448 char_u
*name
= tv
->vval
.v_string
;
8453 if (tv
->v_type
== VAR_NUMBER
)
8454 return buflist_findnr((int)tv
->vval
.v_number
);
8455 if (tv
->v_type
!= VAR_STRING
)
8457 if (name
== NULL
|| *name
== NUL
)
8459 if (name
[0] == '$' && name
[1] == NUL
)
8462 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8463 save_magic
= p_magic
;
8466 p_cpo
= (char_u
*)"";
8468 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8471 p_magic
= save_magic
;
8474 /* If not found, try expanding the name, like done for bufexists(). */
8476 buf
= find_buffer(tv
);
8482 * "bufname(expr)" function
8485 f_bufname(argvars
, rettv
)
8491 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8493 buf
= get_buf_tv(&argvars
[0]);
8494 rettv
->v_type
= VAR_STRING
;
8495 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8496 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8498 rettv
->vval
.v_string
= NULL
;
8503 * "bufnr(expr)" function
8506 f_bufnr(argvars
, rettv
)
8514 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8516 buf
= get_buf_tv(&argvars
[0]);
8519 /* If the buffer isn't found and the second argument is not zero create a
8522 && argvars
[1].v_type
!= VAR_UNKNOWN
8523 && get_tv_number_chk(&argvars
[1], &error
) != 0
8525 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8527 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8530 rettv
->vval
.v_number
= buf
->b_fnum
;
8532 rettv
->vval
.v_number
= -1;
8536 * "bufwinnr(nr)" function
8539 f_bufwinnr(argvars
, rettv
)
8549 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8551 buf
= get_buf_tv(&argvars
[0]);
8553 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8556 if (wp
->w_buffer
== buf
)
8559 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8561 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8567 * "byte2line(byte)" function
8571 f_byte2line(argvars
, rettv
)
8575 #ifndef FEAT_BYTEOFF
8576 rettv
->vval
.v_number
= -1;
8580 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8582 rettv
->vval
.v_number
= -1;
8584 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8585 (linenr_T
)0, &boff
);
8590 * "byteidx()" function
8594 f_byteidx(argvars
, rettv
)
8604 str
= get_tv_string_chk(&argvars
[0]);
8605 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8606 rettv
->vval
.v_number
= -1;
8607 if (str
== NULL
|| idx
< 0)
8612 for ( ; idx
> 0; idx
--)
8614 if (*t
== NUL
) /* EOL reached */
8616 t
+= (*mb_ptr2len
)(t
);
8618 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8620 if ((size_t)idx
<= STRLEN(str
))
8621 rettv
->vval
.v_number
= idx
;
8626 * "call(func, arglist)" function
8629 f_call(argvars
, rettv
)
8634 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8638 dict_T
*selfdict
= NULL
;
8640 rettv
->vval
.v_number
= 0;
8641 if (argvars
[1].v_type
!= VAR_LIST
)
8646 if (argvars
[1].vval
.v_list
== NULL
)
8649 if (argvars
[0].v_type
== VAR_FUNC
)
8650 func
= argvars
[0].vval
.v_string
;
8652 func
= get_tv_string(&argvars
[0]);
8654 return; /* type error or empty name */
8656 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8658 if (argvars
[2].v_type
!= VAR_DICT
)
8663 selfdict
= argvars
[2].vval
.v_dict
;
8666 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8667 item
= item
->li_next
)
8669 if (argc
== MAX_FUNC_ARGS
)
8671 EMSG(_("E699: Too many arguments"));
8674 /* Make a copy of each argument. This is needed to be able to set
8675 * v_lock to VAR_FIXED in the copy without changing the original list.
8677 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8681 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8682 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8683 &dummy
, TRUE
, selfdict
);
8685 /* Free the arguments. */
8687 clear_tv(&argv
[--argc
]);
8692 * "ceil({float})" function
8695 f_ceil(argvars
, rettv
)
8701 rettv
->v_type
= VAR_FLOAT
;
8702 if (get_float_arg(argvars
, &f
) == OK
)
8703 rettv
->vval
.v_float
= ceil(f
);
8705 rettv
->vval
.v_float
= 0.0;
8710 * "changenr()" function
8714 f_changenr(argvars
, rettv
)
8718 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8722 * "char2nr(string)" function
8725 f_char2nr(argvars
, rettv
)
8731 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8734 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8738 * "cindent(lnum)" function
8741 f_cindent(argvars
, rettv
)
8749 pos
= curwin
->w_cursor
;
8750 lnum
= get_tv_lnum(argvars
);
8751 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8753 curwin
->w_cursor
.lnum
= lnum
;
8754 rettv
->vval
.v_number
= get_c_indent();
8755 curwin
->w_cursor
= pos
;
8759 rettv
->vval
.v_number
= -1;
8763 * "clearmatches()" function
8767 f_clearmatches(argvars
, rettv
)
8771 #ifdef FEAT_SEARCH_EXTRA
8772 clear_matches(curwin
);
8777 * "col(string)" function
8780 f_col(argvars
, rettv
)
8786 int fnum
= curbuf
->b_fnum
;
8788 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8789 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8791 if (fp
->col
== MAXCOL
)
8793 /* '> can be MAXCOL, get the length of the line then */
8794 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8795 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8802 #ifdef FEAT_VIRTUALEDIT
8803 /* col(".") when the cursor is on the NUL at the end of the line
8804 * because of "coladd" can be seen as an extra column. */
8805 if (virtual_active() && fp
== &curwin
->w_cursor
)
8807 char_u
*p
= ml_get_cursor();
8809 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8810 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8815 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8818 if (*p
!= NUL
&& p
[1] == NUL
)
8826 rettv
->vval
.v_number
= col
;
8829 #if defined(FEAT_INS_EXPAND)
8831 * "complete()" function
8835 f_complete(argvars
, rettv
)
8841 if ((State
& INSERT
) == 0)
8843 EMSG(_("E785: complete() can only be used in Insert mode"));
8847 /* Check for undo allowed here, because if something was already inserted
8848 * the line was already saved for undo and this check isn't done. */
8849 if (!undo_allowed())
8852 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8858 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
8862 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
8866 * "complete_add()" function
8870 f_complete_add(argvars
, rettv
)
8874 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
8878 * "complete_check()" function
8882 f_complete_check(argvars
, rettv
)
8886 int saved
= RedrawingDisabled
;
8888 RedrawingDisabled
= 0;
8889 ins_compl_check_keys(0);
8890 rettv
->vval
.v_number
= compl_interrupted
;
8891 RedrawingDisabled
= saved
;
8896 * "confirm(message, buttons[, default [, type]])" function
8900 f_confirm(argvars
, rettv
)
8904 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8906 char_u
*buttons
= NULL
;
8907 char_u buf
[NUMBUFLEN
];
8908 char_u buf2
[NUMBUFLEN
];
8910 int type
= VIM_GENERIC
;
8914 message
= get_tv_string_chk(&argvars
[0]);
8915 if (message
== NULL
)
8917 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8919 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
8920 if (buttons
== NULL
)
8922 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8924 def
= get_tv_number_chk(&argvars
[2], &error
);
8925 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8927 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8928 if (typestr
== NULL
)
8932 switch (TOUPPER_ASC(*typestr
))
8934 case 'E': type
= VIM_ERROR
; break;
8935 case 'Q': type
= VIM_QUESTION
; break;
8936 case 'I': type
= VIM_INFO
; break;
8937 case 'W': type
= VIM_WARNING
; break;
8938 case 'G': type
= VIM_GENERIC
; break;
8945 if (buttons
== NULL
|| *buttons
== NUL
)
8946 buttons
= (char_u
*)_("&Ok");
8949 rettv
->vval
.v_number
= 0;
8951 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
8954 rettv
->vval
.v_number
= 0;
8962 f_copy(argvars
, rettv
)
8966 item_copy(&argvars
[0], rettv
, FALSE
, 0);
8974 f_cos(argvars
, rettv
)
8980 rettv
->v_type
= VAR_FLOAT
;
8981 if (get_float_arg(argvars
, &f
) == OK
)
8982 rettv
->vval
.v_float
= cos(f
);
8984 rettv
->vval
.v_float
= 0.0;
8989 * "count()" function
8992 f_count(argvars
, rettv
)
8999 if (argvars
[0].v_type
== VAR_LIST
)
9005 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9008 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9012 ic
= get_tv_number_chk(&argvars
[2], &error
);
9013 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9015 idx
= get_tv_number_chk(&argvars
[3], &error
);
9018 li
= list_find(l
, idx
);
9020 EMSGN(_(e_listidx
), idx
);
9027 for ( ; li
!= NULL
; li
= li
->li_next
)
9028 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
9032 else if (argvars
[0].v_type
== VAR_DICT
)
9038 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9042 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9044 ic
= get_tv_number_chk(&argvars
[2], &error
);
9045 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9049 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
9050 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
9052 if (!HASHITEM_EMPTY(hi
))
9055 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
9062 EMSG2(_(e_listdictarg
), "count()");
9063 rettv
->vval
.v_number
= n
;
9067 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9069 * Checks the existence of a cscope connection.
9073 f_cscope_connection(argvars
, rettv
)
9079 char_u
*dbpath
= NULL
;
9080 char_u
*prepend
= NULL
;
9081 char_u buf
[NUMBUFLEN
];
9083 if (argvars
[0].v_type
!= VAR_UNKNOWN
9084 && argvars
[1].v_type
!= VAR_UNKNOWN
)
9086 num
= (int)get_tv_number(&argvars
[0]);
9087 dbpath
= get_tv_string(&argvars
[1]);
9088 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9089 prepend
= get_tv_string_buf(&argvars
[2], buf
);
9092 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
9094 rettv
->vval
.v_number
= 0;
9099 * "cursor(lnum, col)" function
9101 * Moves the cursor to the specified line and column
9105 f_cursor(argvars
, rettv
)
9110 #ifdef FEAT_VIRTUALEDIT
9114 if (argvars
[1].v_type
== VAR_UNKNOWN
)
9118 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
9122 #ifdef FEAT_VIRTUALEDIT
9123 coladd
= pos
.coladd
;
9128 line
= get_tv_lnum(argvars
);
9129 col
= get_tv_number_chk(&argvars
[1], NULL
);
9130 #ifdef FEAT_VIRTUALEDIT
9131 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9132 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
9135 if (line
< 0 || col
< 0
9136 #ifdef FEAT_VIRTUALEDIT
9140 return; /* type error; errmsg already given */
9142 curwin
->w_cursor
.lnum
= line
;
9144 curwin
->w_cursor
.col
= col
- 1;
9145 #ifdef FEAT_VIRTUALEDIT
9146 curwin
->w_cursor
.coladd
= coladd
;
9149 /* Make sure the cursor is in a valid position. */
9152 /* Correct cursor for multi-byte character. */
9157 curwin
->w_set_curswant
= TRUE
;
9161 * "deepcopy()" function
9164 f_deepcopy(argvars
, rettv
)
9170 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9171 noref
= get_tv_number_chk(&argvars
[1], NULL
);
9172 if (noref
< 0 || noref
> 1)
9175 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
9179 * "delete()" function
9182 f_delete(argvars
, rettv
)
9186 if (check_restricted() || check_secure())
9187 rettv
->vval
.v_number
= -1;
9189 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
9193 * "did_filetype()" function
9197 f_did_filetype(argvars
, rettv
)
9202 rettv
->vval
.v_number
= did_filetype
;
9204 rettv
->vval
.v_number
= 0;
9209 * "diff_filler()" function
9213 f_diff_filler(argvars
, rettv
)
9218 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
9223 * "diff_hlID()" function
9227 f_diff_hlID(argvars
, rettv
)
9232 linenr_T lnum
= get_tv_lnum(argvars
);
9233 static linenr_T prev_lnum
= 0;
9234 static int changedtick
= 0;
9235 static int fnum
= 0;
9236 static int change_start
= 0;
9237 static int change_end
= 0;
9238 static hlf_T hlID
= (hlf_T
)0;
9242 if (lnum
< 0) /* ignore type error in {lnum} arg */
9244 if (lnum
!= prev_lnum
9245 || changedtick
!= curbuf
->b_changedtick
9246 || fnum
!= curbuf
->b_fnum
)
9248 /* New line, buffer, change: need to get the values. */
9249 filler_lines
= diff_check(curwin
, lnum
);
9250 if (filler_lines
< 0)
9252 if (filler_lines
== -1)
9254 change_start
= MAXCOL
;
9256 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
9257 hlID
= HLF_ADD
; /* added line */
9259 hlID
= HLF_CHD
; /* changed line */
9262 hlID
= HLF_ADD
; /* added line */
9267 changedtick
= curbuf
->b_changedtick
;
9268 fnum
= curbuf
->b_fnum
;
9271 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
9273 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
9274 if (col
>= change_start
&& col
<= change_end
)
9275 hlID
= HLF_TXD
; /* changed text */
9277 hlID
= HLF_CHD
; /* changed line */
9279 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
9284 * "empty({expr})" function
9287 f_empty(argvars
, rettv
)
9293 switch (argvars
[0].v_type
)
9297 n
= argvars
[0].vval
.v_string
== NULL
9298 || *argvars
[0].vval
.v_string
== NUL
;
9301 n
= argvars
[0].vval
.v_number
== 0;
9305 n
= argvars
[0].vval
.v_float
== 0.0;
9309 n
= argvars
[0].vval
.v_list
== NULL
9310 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
9313 n
= argvars
[0].vval
.v_dict
== NULL
9314 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
9317 EMSG2(_(e_intern2
), "f_empty()");
9321 rettv
->vval
.v_number
= n
;
9325 * "escape({string}, {chars})" function
9328 f_escape(argvars
, rettv
)
9332 char_u buf
[NUMBUFLEN
];
9334 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
9335 get_tv_string_buf(&argvars
[1], buf
));
9336 rettv
->v_type
= VAR_STRING
;
9344 f_eval(argvars
, rettv
)
9350 s
= get_tv_string_chk(&argvars
[0]);
9354 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
9356 rettv
->v_type
= VAR_NUMBER
;
9357 rettv
->vval
.v_number
= 0;
9360 EMSG(_(e_trailing
));
9364 * "eventhandler()" function
9368 f_eventhandler(argvars
, rettv
)
9372 rettv
->vval
.v_number
= vgetc_busy
;
9376 * "executable()" function
9379 f_executable(argvars
, rettv
)
9383 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
9387 * "exists()" function
9390 f_exists(argvars
, rettv
)
9399 p
= get_tv_string(&argvars
[0]);
9400 if (*p
== '$') /* environment variable */
9402 /* first try "normal" environment variables (fast) */
9403 if (mch_getenv(p
+ 1) != NULL
)
9407 /* try expanding things like $VIM and ${HOME} */
9408 p
= expand_env_save(p
);
9409 if (p
!= NULL
&& *p
!= '$')
9414 else if (*p
== '&' || *p
== '+') /* option */
9416 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
9417 if (*skipwhite(p
) != NUL
)
9418 n
= FALSE
; /* trailing garbage */
9420 else if (*p
== '*') /* internal or user defined function */
9422 n
= function_exists(p
+ 1);
9426 n
= cmd_exists(p
+ 1);
9432 n
= autocmd_supported(p
+ 2);
9434 n
= au_exists(p
+ 1);
9437 else /* internal variable */
9442 /* get_name_len() takes care of expanding curly braces */
9444 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
9449 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
9452 /* handle d.key, l[idx], f(expr) */
9453 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
9464 rettv
->vval
.v_number
= n
;
9468 * "expand()" function
9471 f_expand(argvars
, rettv
)
9478 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
9482 rettv
->v_type
= VAR_STRING
;
9483 s
= get_tv_string(&argvars
[0]);
9484 if (*s
== '%' || *s
== '#' || *s
== '<')
9487 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
9492 /* When the optional second argument is non-zero, don't remove matches
9493 * for 'suffixes' and 'wildignore' */
9494 if (argvars
[1].v_type
!= VAR_UNKNOWN
9495 && get_tv_number_chk(&argvars
[1], &error
))
9496 flags
|= WILD_KEEP_ALL
;
9500 xpc
.xp_context
= EXPAND_FILES
;
9501 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
9504 rettv
->vval
.v_string
= NULL
;
9509 * "extend(list, list [, idx])" function
9510 * "extend(dict, dict [, action])" function
9513 f_extend(argvars
, rettv
)
9517 rettv
->vval
.v_number
= 0;
9518 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9525 l1
= argvars
[0].vval
.v_list
;
9526 l2
= argvars
[1].vval
.v_list
;
9527 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9530 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9532 before
= get_tv_number_chk(&argvars
[2], &error
);
9534 return; /* type error; errmsg already given */
9536 if (before
== l1
->lv_len
)
9540 item
= list_find(l1
, before
);
9543 EMSGN(_(e_listidx
), before
);
9550 list_extend(l1
, l2
, item
);
9552 copy_tv(&argvars
[0], rettv
);
9555 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9564 d1
= argvars
[0].vval
.v_dict
;
9565 d2
= argvars
[1].vval
.v_dict
;
9566 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9569 /* Check the third argument. */
9570 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9572 static char *(av
[]) = {"keep", "force", "error"};
9574 action
= get_tv_string_chk(&argvars
[2]);
9576 return; /* type error; errmsg already given */
9577 for (i
= 0; i
< 3; ++i
)
9578 if (STRCMP(action
, av
[i
]) == 0)
9582 EMSG2(_(e_invarg2
), action
);
9587 action
= (char_u
*)"force";
9589 /* Go over all entries in the second dict and add them to the
9591 todo
= (int)d2
->dv_hashtab
.ht_used
;
9592 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9594 if (!HASHITEM_EMPTY(hi2
))
9597 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9600 di1
= dictitem_copy(HI2DI(hi2
));
9601 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9604 else if (*action
== 'e')
9606 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9609 else if (*action
== 'f')
9611 clear_tv(&di1
->di_tv
);
9612 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9617 copy_tv(&argvars
[0], rettv
);
9621 EMSG2(_(e_listdictarg
), "extend()");
9625 * "feedkeys()" function
9629 f_feedkeys(argvars
, rettv
)
9634 char_u
*keys
, *flags
;
9635 char_u nbuf
[NUMBUFLEN
];
9639 /* This is not allowed in the sandbox. If the commands would still be
9640 * executed in the sandbox it would be OK, but it probably happens later,
9641 * when "sandbox" is no longer set. */
9645 rettv
->vval
.v_number
= 0;
9646 keys
= get_tv_string(&argvars
[0]);
9649 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9651 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9652 for ( ; *flags
!= NUL
; ++flags
)
9656 case 'n': remap
= FALSE
; break;
9657 case 'm': remap
= TRUE
; break;
9658 case 't': typed
= TRUE
; break;
9663 /* Need to escape K_SPECIAL and CSI before putting the string in the
9664 * typeahead buffer. */
9665 keys_esc
= vim_strsave_escape_csi(keys
);
9666 if (keys_esc
!= NULL
)
9668 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9669 typebuf
.tb_len
, !typed
, FALSE
);
9672 typebuf_was_filled
= TRUE
;
9678 * "filereadable()" function
9681 f_filereadable(argvars
, rettv
)
9689 p
= get_tv_string(&argvars
[0]);
9690 if (*p
&& !mch_isdir(p
) && (fd
= mch_fopen((char *)p
, "r")) != NULL
)
9698 rettv
->vval
.v_number
= n
;
9702 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9703 * rights to write into.
9706 f_filewritable(argvars
, rettv
)
9710 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9713 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int find_what
));
9716 findfilendir(argvars
, rettv
, find_what
)
9721 #ifdef FEAT_SEARCHPATH
9723 char_u
*fresult
= NULL
;
9724 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9726 char_u pathbuf
[NUMBUFLEN
];
9732 rettv
->vval
.v_string
= NULL
;
9733 rettv
->v_type
= VAR_STRING
;
9735 #ifdef FEAT_SEARCHPATH
9736 fname
= get_tv_string(&argvars
[0]);
9738 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9740 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9748 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9749 count
= get_tv_number_chk(&argvars
[2], &error
);
9753 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9756 if (*fname
!= NUL
&& !error
)
9760 if (rettv
->v_type
== VAR_STRING
)
9762 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9763 first
? (int)STRLEN(fname
) : 0,
9767 find_what
== FINDFILE_DIR
9768 ? (char_u
*)"" : curbuf
->b_p_sua
);
9771 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9772 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9774 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9777 if (rettv
->v_type
== VAR_STRING
)
9778 rettv
->vval
.v_string
= fresult
;
9782 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9783 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9786 * Implementation of map() and filter().
9789 filter_map(argvars
, rettv
, map
)
9794 char_u buf
[NUMBUFLEN
];
9796 listitem_T
*li
, *nli
;
9806 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9809 rettv
->vval
.v_number
= 0;
9810 if (argvars
[0].v_type
== VAR_LIST
)
9812 if ((l
= argvars
[0].vval
.v_list
) == NULL
9813 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9816 else if (argvars
[0].v_type
== VAR_DICT
)
9818 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9819 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9824 EMSG2(_(e_listdictarg
), ermsg
);
9828 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9829 /* On type errors, the preceding call has already displayed an error
9830 * message. Avoid a misleading error message for an empty string that
9831 * was not passed as argument. */
9834 prepare_vimvar(VV_VAL
, &save_val
);
9835 expr
= skipwhite(expr
);
9837 /* We reset "did_emsg" to be able to detect whether an error
9838 * occurred during evaluation of the expression. */
9839 save_did_emsg
= did_emsg
;
9842 if (argvars
[0].v_type
== VAR_DICT
)
9844 prepare_vimvar(VV_KEY
, &save_key
);
9845 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9847 ht
= &d
->dv_hashtab
;
9849 todo
= (int)ht
->ht_used
;
9850 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9852 if (!HASHITEM_EMPTY(hi
))
9856 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9858 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9859 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9863 dictitem_remove(d
, di
);
9864 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9869 restore_vimvar(VV_KEY
, &save_key
);
9873 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
9875 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
9878 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
9882 listitem_remove(l
, li
);
9886 restore_vimvar(VV_VAL
, &save_val
);
9888 did_emsg
|= save_did_emsg
;
9891 copy_tv(&argvars
[0], rettv
);
9895 filter_map_one(tv
, expr
, map
, remp
)
9905 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
9907 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
9909 if (*s
!= NUL
) /* check for trailing chars after expr */
9911 EMSG2(_(e_invexpr2
), s
);
9916 /* map(): replace the list item value */
9925 /* filter(): when expr is zero remove the item */
9926 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
9928 /* On type error, nothing has been removed; return FAIL to stop the
9929 * loop. The error message was given by get_tv_number_chk(). */
9935 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
9940 * "filter()" function
9943 f_filter(argvars
, rettv
)
9947 filter_map(argvars
, rettv
, FALSE
);
9951 * "finddir({fname}[, {path}[, {count}]])" function
9954 f_finddir(argvars
, rettv
)
9958 findfilendir(argvars
, rettv
, FINDFILE_DIR
);
9962 * "findfile({fname}[, {path}[, {count}]])" function
9965 f_findfile(argvars
, rettv
)
9969 findfilendir(argvars
, rettv
, FINDFILE_FILE
);
9974 * "float2nr({float})" function
9977 f_float2nr(argvars
, rettv
)
9983 if (get_float_arg(argvars
, &f
) == OK
)
9985 if (f
< -0x7fffffff)
9986 rettv
->vval
.v_number
= -0x7fffffff;
9987 else if (f
> 0x7fffffff)
9988 rettv
->vval
.v_number
= 0x7fffffff;
9990 rettv
->vval
.v_number
= (varnumber_T
)f
;
9993 rettv
->vval
.v_number
= 0;
9997 * "floor({float})" function
10000 f_floor(argvars
, rettv
)
10006 rettv
->v_type
= VAR_FLOAT
;
10007 if (get_float_arg(argvars
, &f
) == OK
)
10008 rettv
->vval
.v_float
= floor(f
);
10010 rettv
->vval
.v_float
= 0.0;
10015 * "fnameescape({string})" function
10018 f_fnameescape(argvars
, rettv
)
10022 rettv
->vval
.v_string
= vim_strsave_fnameescape(
10023 get_tv_string(&argvars
[0]), FALSE
);
10024 rettv
->v_type
= VAR_STRING
;
10028 * "fnamemodify({fname}, {mods})" function
10031 f_fnamemodify(argvars
, rettv
)
10039 char_u
*fbuf
= NULL
;
10040 char_u buf
[NUMBUFLEN
];
10042 fname
= get_tv_string_chk(&argvars
[0]);
10043 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
10044 if (fname
== NULL
|| mods
== NULL
)
10048 len
= (int)STRLEN(fname
);
10049 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
10052 rettv
->v_type
= VAR_STRING
;
10054 rettv
->vval
.v_string
= NULL
;
10056 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
10060 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
10063 * "foldclosed()" function
10066 foldclosed_both(argvars
, rettv
, end
)
10071 #ifdef FEAT_FOLDING
10073 linenr_T first
, last
;
10075 lnum
= get_tv_lnum(argvars
);
10076 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10078 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
10081 rettv
->vval
.v_number
= (varnumber_T
)last
;
10083 rettv
->vval
.v_number
= (varnumber_T
)first
;
10088 rettv
->vval
.v_number
= -1;
10092 * "foldclosed()" function
10095 f_foldclosed(argvars
, rettv
)
10099 foldclosed_both(argvars
, rettv
, FALSE
);
10103 * "foldclosedend()" function
10106 f_foldclosedend(argvars
, rettv
)
10110 foldclosed_both(argvars
, rettv
, TRUE
);
10114 * "foldlevel()" function
10117 f_foldlevel(argvars
, rettv
)
10121 #ifdef FEAT_FOLDING
10124 lnum
= get_tv_lnum(argvars
);
10125 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10126 rettv
->vval
.v_number
= foldLevel(lnum
);
10129 rettv
->vval
.v_number
= 0;
10133 * "foldtext()" function
10137 f_foldtext(argvars
, rettv
)
10141 #ifdef FEAT_FOLDING
10149 rettv
->v_type
= VAR_STRING
;
10150 rettv
->vval
.v_string
= NULL
;
10151 #ifdef FEAT_FOLDING
10152 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
10153 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10154 <= curbuf
->b_ml
.ml_line_count
10155 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
10157 /* Find first non-empty line in the fold. */
10158 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
10159 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10161 if (!linewhite(lnum
))
10166 /* Find interesting text in this line. */
10167 s
= skipwhite(ml_get(lnum
));
10168 /* skip C comment-start */
10169 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
10171 s
= skipwhite(s
+ 2);
10172 if (*skipwhite(s
) == NUL
10173 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10175 s
= skipwhite(ml_get(lnum
+ 1));
10177 s
= skipwhite(s
+ 1);
10180 txt
= _("+-%s%3ld lines: ");
10181 r
= alloc((unsigned)(STRLEN(txt
)
10182 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
10183 + 20 /* for %3ld */
10184 + STRLEN(s
))); /* concatenated */
10187 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
10188 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10189 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
10190 len
= (int)STRLEN(r
);
10192 /* remove 'foldmarker' and 'commentstring' */
10193 foldtext_cleanup(r
+ len
);
10194 rettv
->vval
.v_string
= r
;
10201 * "foldtextresult(lnum)" function
10205 f_foldtextresult(argvars
, rettv
)
10209 #ifdef FEAT_FOLDING
10213 foldinfo_T foldinfo
;
10217 rettv
->v_type
= VAR_STRING
;
10218 rettv
->vval
.v_string
= NULL
;
10219 #ifdef FEAT_FOLDING
10220 lnum
= get_tv_lnum(argvars
);
10221 /* treat illegal types and illegal string values for {lnum} the same */
10224 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
10225 if (fold_count
> 0)
10227 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
10230 text
= vim_strsave(text
);
10231 rettv
->vval
.v_string
= text
;
10237 * "foreground()" function
10241 f_foreground(argvars
, rettv
)
10245 rettv
->vval
.v_number
= 0;
10248 gui_mch_set_foreground();
10251 win32_set_foreground();
10257 * "function()" function
10261 f_function(argvars
, rettv
)
10267 rettv
->vval
.v_number
= 0;
10268 s
= get_tv_string(&argvars
[0]);
10269 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
10270 EMSG2(_(e_invarg2
), s
);
10271 else if (!function_exists(s
))
10272 EMSG2(_("E700: Unknown function: %s"), s
);
10275 rettv
->vval
.v_string
= vim_strsave(s
);
10276 rettv
->v_type
= VAR_FUNC
;
10281 * "garbagecollect()" function
10285 f_garbagecollect(argvars
, rettv
)
10289 /* This is postponed until we are back at the toplevel, because we may be
10290 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10291 want_garbage_collect
= TRUE
;
10293 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
10294 garbage_collect_at_exit
= TRUE
;
10301 f_get(argvars
, rettv
)
10309 typval_T
*tv
= NULL
;
10311 if (argvars
[0].v_type
== VAR_LIST
)
10313 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
10317 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
10318 if (!error
&& li
!= NULL
)
10322 else if (argvars
[0].v_type
== VAR_DICT
)
10324 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
10326 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
10332 EMSG2(_(e_listdictarg
), "get()");
10336 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10337 rettv
->vval
.v_number
= 0;
10339 copy_tv(&argvars
[2], rettv
);
10342 copy_tv(tv
, rettv
);
10345 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
10348 * Get line or list of lines from buffer "buf" into "rettv".
10349 * Return a range (from start to end) of lines in rettv from the specified
10351 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
10354 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
10365 if (rettv_list_alloc(rettv
) == FAIL
)
10369 rettv
->vval
.v_number
= 0;
10371 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
10376 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
10377 p
= ml_get_buf(buf
, start
, FALSE
);
10381 rettv
->v_type
= VAR_STRING
;
10382 rettv
->vval
.v_string
= vim_strsave(p
);
10391 if (end
> buf
->b_ml
.ml_line_count
)
10392 end
= buf
->b_ml
.ml_line_count
;
10393 while (start
<= end
)
10394 if (list_append_string(rettv
->vval
.v_list
,
10395 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
10401 * "getbufline()" function
10404 f_getbufline(argvars
, rettv
)
10412 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10414 buf
= get_buf_tv(&argvars
[0]);
10417 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
10418 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10421 end
= get_tv_lnum_buf(&argvars
[2], buf
);
10423 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
10427 * "getbufvar()" function
10430 f_getbufvar(argvars
, rettv
)
10435 buf_T
*save_curbuf
;
10439 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10440 varname
= get_tv_string_chk(&argvars
[1]);
10442 buf
= get_buf_tv(&argvars
[0]);
10444 rettv
->v_type
= VAR_STRING
;
10445 rettv
->vval
.v_string
= NULL
;
10447 if (buf
!= NULL
&& varname
!= NULL
)
10449 /* set curbuf to be our buf, temporarily */
10450 save_curbuf
= curbuf
;
10453 if (*varname
== '&') /* buffer-local-option */
10454 get_option_tv(&varname
, rettv
, TRUE
);
10457 if (*varname
== NUL
)
10458 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10459 * scope prefix before the NUL byte is required by
10460 * find_var_in_ht(). */
10461 varname
= (char_u
*)"b:" + 2;
10462 /* look up the variable */
10463 v
= find_var_in_ht(&curbuf
->b_vars
.dv_hashtab
, varname
, FALSE
);
10465 copy_tv(&v
->di_tv
, rettv
);
10468 /* restore previous notion of curbuf */
10469 curbuf
= save_curbuf
;
10476 * "getchar()" function
10479 f_getchar(argvars
, rettv
)
10486 /* Position the cursor. Needed after a message that ends in a space. */
10487 windgoto(msg_row
, msg_col
);
10493 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10494 /* getchar(): blocking wait. */
10496 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
10497 /* getchar(1): only check if char avail */
10499 else if (error
|| vpeekc() == NUL
)
10500 /* illegal argument or getchar(0) and no char avail: return zero */
10503 /* getchar(0) and char avail: return char */
10512 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
10513 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
10514 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
10516 rettv
->vval
.v_number
= n
;
10517 if (IS_SPECIAL(n
) || mod_mask
!= 0)
10519 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10522 /* Turn a special key into three bytes, plus modifier. */
10525 temp
[i
++] = K_SPECIAL
;
10526 temp
[i
++] = KS_MODIFIER
;
10527 temp
[i
++] = mod_mask
;
10531 temp
[i
++] = K_SPECIAL
;
10532 temp
[i
++] = K_SECOND(n
);
10533 temp
[i
++] = K_THIRD(n
);
10536 else if (has_mbyte
)
10537 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
10542 rettv
->v_type
= VAR_STRING
;
10543 rettv
->vval
.v_string
= vim_strsave(temp
);
10546 if (n
== K_LEFTMOUSE
10547 || n
== K_LEFTMOUSE_NM
10549 || n
== K_LEFTRELEASE
10550 || n
== K_LEFTRELEASE_NM
10551 || n
== K_MIDDLEMOUSE
10552 || n
== K_MIDDLEDRAG
10553 || n
== K_MIDDLERELEASE
10554 || n
== K_RIGHTMOUSE
10555 || n
== K_RIGHTDRAG
10556 || n
== K_RIGHTRELEASE
10559 || n
== K_X1RELEASE
10562 || n
== K_X2RELEASE
10563 || n
== K_MOUSEDOWN
10566 int row
= mouse_row
;
10567 int col
= mouse_col
;
10570 # ifdef FEAT_WINDOWS
10575 if (row
>= 0 && col
>= 0)
10577 /* Find the window at the mouse coordinates and compute the
10578 * text position. */
10579 win
= mouse_find_win(&row
, &col
);
10580 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10581 # ifdef FEAT_WINDOWS
10582 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10585 vimvars
[VV_MOUSE_WIN
].vv_nr
= n
;
10586 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10587 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10595 * "getcharmod()" function
10599 f_getcharmod(argvars
, rettv
)
10603 rettv
->vval
.v_number
= mod_mask
;
10607 * "getcmdline()" function
10611 f_getcmdline(argvars
, rettv
)
10615 rettv
->v_type
= VAR_STRING
;
10616 rettv
->vval
.v_string
= get_cmdline_str();
10620 * "getcmdpos()" function
10624 f_getcmdpos(argvars
, rettv
)
10628 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10632 * "getcmdtype()" function
10636 f_getcmdtype(argvars
, rettv
)
10640 rettv
->v_type
= VAR_STRING
;
10641 rettv
->vval
.v_string
= alloc(2);
10642 if (rettv
->vval
.v_string
!= NULL
)
10644 rettv
->vval
.v_string
[0] = get_cmdline_type();
10645 rettv
->vval
.v_string
[1] = NUL
;
10650 * "getcwd()" function
10654 f_getcwd(argvars
, rettv
)
10658 char_u cwd
[MAXPATHL
];
10660 rettv
->v_type
= VAR_STRING
;
10661 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10662 rettv
->vval
.v_string
= NULL
;
10665 rettv
->vval
.v_string
= vim_strsave(cwd
);
10666 #ifdef BACKSLASH_IN_FILENAME
10667 if (rettv
->vval
.v_string
!= NULL
)
10668 slash_adjust(rettv
->vval
.v_string
);
10674 * "getfontname()" function
10678 f_getfontname(argvars
, rettv
)
10682 rettv
->v_type
= VAR_STRING
;
10683 rettv
->vval
.v_string
= NULL
;
10688 char_u
*name
= NULL
;
10690 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10692 /* Get the "Normal" font. Either the name saved by
10693 * hl_set_font_name() or from the font ID. */
10694 font
= gui
.norm_font
;
10695 name
= hl_get_font_name();
10699 name
= get_tv_string(&argvars
[0]);
10700 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10702 font
= gui_mch_get_font(name
, FALSE
);
10703 if (font
== NOFONT
)
10704 return; /* Invalid font name, return empty string. */
10706 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10707 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10708 gui_mch_free_font(font
);
10714 * "getfperm({fname})" function
10717 f_getfperm(argvars
, rettv
)
10723 char_u
*perm
= NULL
;
10724 char_u flags
[] = "rwx";
10727 fname
= get_tv_string(&argvars
[0]);
10729 rettv
->v_type
= VAR_STRING
;
10730 if (mch_stat((char *)fname
, &st
) >= 0)
10732 perm
= vim_strsave((char_u
*)"---------");
10735 for (i
= 0; i
< 9; i
++)
10737 if (st
.st_mode
& (1 << (8 - i
)))
10738 perm
[i
] = flags
[i
% 3];
10742 rettv
->vval
.v_string
= perm
;
10746 * "getfsize({fname})" function
10749 f_getfsize(argvars
, rettv
)
10756 fname
= get_tv_string(&argvars
[0]);
10758 rettv
->v_type
= VAR_NUMBER
;
10760 if (mch_stat((char *)fname
, &st
) >= 0)
10762 if (mch_isdir(fname
))
10763 rettv
->vval
.v_number
= 0;
10766 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10768 /* non-perfect check for overflow */
10769 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
10770 rettv
->vval
.v_number
= -2;
10774 rettv
->vval
.v_number
= -1;
10778 * "getftime({fname})" function
10781 f_getftime(argvars
, rettv
)
10788 fname
= get_tv_string(&argvars
[0]);
10790 if (mch_stat((char *)fname
, &st
) >= 0)
10791 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10793 rettv
->vval
.v_number
= -1;
10797 * "getftype({fname})" function
10800 f_getftype(argvars
, rettv
)
10806 char_u
*type
= NULL
;
10809 fname
= get_tv_string(&argvars
[0]);
10811 rettv
->v_type
= VAR_STRING
;
10812 if (mch_lstat((char *)fname
, &st
) >= 0)
10815 if (S_ISREG(st
.st_mode
))
10817 else if (S_ISDIR(st
.st_mode
))
10820 else if (S_ISLNK(st
.st_mode
))
10824 else if (S_ISBLK(st
.st_mode
))
10828 else if (S_ISCHR(st
.st_mode
))
10832 else if (S_ISFIFO(st
.st_mode
))
10836 else if (S_ISSOCK(st
.st_mode
))
10843 switch (st
.st_mode
& S_IFMT
)
10845 case S_IFREG
: t
= "file"; break;
10846 case S_IFDIR
: t
= "dir"; break;
10848 case S_IFLNK
: t
= "link"; break;
10851 case S_IFBLK
: t
= "bdev"; break;
10854 case S_IFCHR
: t
= "cdev"; break;
10857 case S_IFIFO
: t
= "fifo"; break;
10860 case S_IFSOCK
: t
= "socket"; break;
10862 default: t
= "other";
10865 if (mch_isdir(fname
))
10871 type
= vim_strsave((char_u
*)t
);
10873 rettv
->vval
.v_string
= type
;
10877 * "getline(lnum, [end])" function
10880 f_getline(argvars
, rettv
)
10888 lnum
= get_tv_lnum(argvars
);
10889 if (argvars
[1].v_type
== VAR_UNKNOWN
)
10896 end
= get_tv_lnum(&argvars
[1]);
10900 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
10904 * "getmatches()" function
10908 f_getmatches(argvars
, rettv
)
10912 #ifdef FEAT_SEARCH_EXTRA
10914 matchitem_T
*cur
= curwin
->w_match_head
;
10916 rettv
->vval
.v_number
= 0;
10918 if (rettv_list_alloc(rettv
) == OK
)
10920 while (cur
!= NULL
)
10922 dict
= dict_alloc();
10925 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
10926 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
10927 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
10928 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
10929 list_append_dict(rettv
->vval
.v_list
, dict
);
10937 * "getpid()" function
10941 f_getpid(argvars
, rettv
)
10945 rettv
->vval
.v_number
= mch_get_pid();
10949 * "getpos(string)" function
10952 f_getpos(argvars
, rettv
)
10960 if (rettv_list_alloc(rettv
) == OK
)
10962 l
= rettv
->vval
.v_list
;
10963 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
10965 list_append_number(l
, (varnumber_T
)fnum
);
10967 list_append_number(l
, (varnumber_T
)0);
10968 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
10970 list_append_number(l
, (fp
!= NULL
)
10971 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
10973 list_append_number(l
,
10974 #ifdef FEAT_VIRTUALEDIT
10975 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
10980 rettv
->vval
.v_number
= FALSE
;
10984 * "getqflist()" and "getloclist()" functions
10988 f_getqflist(argvars
, rettv
)
10992 #ifdef FEAT_QUICKFIX
10996 rettv
->vval
.v_number
= 0;
10997 #ifdef FEAT_QUICKFIX
10998 if (rettv_list_alloc(rettv
) == OK
)
11001 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
11003 wp
= find_win_by_nr(&argvars
[0], NULL
);
11008 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
11014 * "getreg()" function
11017 f_getreg(argvars
, rettv
)
11021 char_u
*strregname
;
11026 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11028 strregname
= get_tv_string_chk(&argvars
[0]);
11029 error
= strregname
== NULL
;
11030 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11031 arg2
= get_tv_number_chk(&argvars
[1], &error
);
11034 strregname
= vimvars
[VV_REG
].vv_str
;
11035 regname
= (strregname
== NULL
? '"' : *strregname
);
11039 rettv
->v_type
= VAR_STRING
;
11040 rettv
->vval
.v_string
= error
? NULL
:
11041 get_reg_contents(regname
, TRUE
, arg2
);
11045 * "getregtype()" function
11048 f_getregtype(argvars
, rettv
)
11052 char_u
*strregname
;
11054 char_u buf
[NUMBUFLEN
+ 2];
11057 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11059 strregname
= get_tv_string_chk(&argvars
[0]);
11060 if (strregname
== NULL
) /* type error; errmsg already given */
11062 rettv
->v_type
= VAR_STRING
;
11063 rettv
->vval
.v_string
= NULL
;
11068 /* Default to v:register */
11069 strregname
= vimvars
[VV_REG
].vv_str
;
11071 regname
= (strregname
== NULL
? '"' : *strregname
);
11077 switch (get_reg_type(regname
, ®len
))
11079 case MLINE
: buf
[0] = 'V'; break;
11080 case MCHAR
: buf
[0] = 'v'; break;
11084 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
11088 rettv
->v_type
= VAR_STRING
;
11089 rettv
->vval
.v_string
= vim_strsave(buf
);
11093 * "gettabwinvar()" function
11096 f_gettabwinvar(argvars
, rettv
)
11100 getwinvar(argvars
, rettv
, 1);
11104 * "getwinposx()" function
11108 f_getwinposx(argvars
, rettv
)
11112 rettv
->vval
.v_number
= -1;
11118 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11119 rettv
->vval
.v_number
= x
;
11125 * "getwinposy()" function
11129 f_getwinposy(argvars
, rettv
)
11133 rettv
->vval
.v_number
= -1;
11139 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11140 rettv
->vval
.v_number
= y
;
11146 * Find window specified by "vp" in tabpage "tp".
11149 find_win_by_nr(vp
, tp
)
11151 tabpage_T
*tp
; /* NULL for current tab page */
11153 #ifdef FEAT_WINDOWS
11158 nr
= get_tv_number_chk(vp
, NULL
);
11160 #ifdef FEAT_WINDOWS
11166 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
11167 wp
!= NULL
; wp
= wp
->w_next
)
11172 if (nr
== 0 || nr
== 1)
11179 * "getwinvar()" function
11182 f_getwinvar(argvars
, rettv
)
11186 getwinvar(argvars
, rettv
, 0);
11190 * getwinvar() and gettabwinvar()
11193 getwinvar(argvars
, rettv
, off
)
11196 int off
; /* 1 for gettabwinvar() */
11198 win_T
*win
, *oldcurwin
;
11203 #ifdef FEAT_WINDOWS
11205 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
11209 win
= find_win_by_nr(&argvars
[off
], tp
);
11210 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
11213 rettv
->v_type
= VAR_STRING
;
11214 rettv
->vval
.v_string
= NULL
;
11216 if (win
!= NULL
&& varname
!= NULL
)
11218 /* Set curwin to be our win, temporarily. Also set curbuf, so
11219 * that we can get buffer-local options. */
11220 oldcurwin
= curwin
;
11222 curbuf
= win
->w_buffer
;
11224 if (*varname
== '&') /* window-local-option */
11225 get_option_tv(&varname
, rettv
, 1);
11228 if (*varname
== NUL
)
11229 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11230 * scope prefix before the NUL byte is required by
11231 * find_var_in_ht(). */
11232 varname
= (char_u
*)"w:" + 2;
11233 /* look up the variable */
11234 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
11236 copy_tv(&v
->di_tv
, rettv
);
11239 /* restore previous notion of curwin */
11240 curwin
= oldcurwin
;
11241 curbuf
= curwin
->w_buffer
;
11248 * "glob()" function
11251 f_glob(argvars
, rettv
)
11258 xpc
.xp_context
= EXPAND_FILES
;
11259 rettv
->v_type
= VAR_STRING
;
11260 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
11261 NULL
, WILD_USE_NL
|WILD_SILENT
, WILD_ALL
);
11265 * "globpath()" function
11268 f_globpath(argvars
, rettv
)
11272 char_u buf1
[NUMBUFLEN
];
11273 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
11275 rettv
->v_type
= VAR_STRING
;
11277 rettv
->vval
.v_string
= NULL
;
11279 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
);
11286 f_has(argvars
, rettv
)
11293 static char *(has_list
[]) =
11314 #if defined(MACOS_X_UNIX)
11338 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11347 #ifndef CASE_INSENSITIVE_FILENAME
11353 #ifdef FEAT_AUTOCMD
11358 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11359 "balloon_multiline",
11362 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11364 # ifdef ALL_BUILTIN_TCAPS
11365 "all_builtin_terms",
11368 #ifdef FEAT_BYTEOFF
11371 #ifdef FEAT_CINDENT
11374 #ifdef FEAT_CLIENTSERVER
11377 #ifdef FEAT_CLIPBOARD
11380 #ifdef FEAT_CMDL_COMPL
11383 #ifdef FEAT_CMDHIST
11386 #ifdef FEAT_COMMENTS
11395 #ifdef CURSOR_SHAPE
11401 #ifdef FEAT_CON_DIALOG
11404 #ifdef FEAT_GUI_DIALOG
11410 #ifdef FEAT_DIGRAPHS
11416 #ifdef FEAT_EMACS_TAGS
11419 "eval", /* always present, of course! */
11420 #ifdef FEAT_EX_EXTRA
11423 #ifdef FEAT_SEARCH_EXTRA
11429 #ifdef FEAT_SEARCHPATH
11432 #if defined(UNIX) && !defined(USE_SYSTEM)
11435 #ifdef FEAT_FIND_ID
11441 #ifdef FEAT_FOLDING
11447 #if !defined(USE_SYSTEM) && defined(UNIX)
11450 #ifdef FEAT_FULLSCREEN
11453 #ifdef FEAT_GETTEXT
11459 #ifdef FEAT_GUI_ATHENA
11460 # ifdef FEAT_GUI_NEXTAW
11466 #ifdef FEAT_GUI_GTK
11472 #ifdef FEAT_GUI_GNOME
11475 #ifdef FEAT_GUI_MAC
11478 #ifdef FEAT_GUI_MACVIM
11481 #ifdef FEAT_GUI_MOTIF
11484 #ifdef FEAT_GUI_PHOTON
11487 #ifdef FEAT_GUI_W16
11490 #ifdef FEAT_GUI_W32
11493 #ifdef FEAT_HANGULIN
11496 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11499 #ifdef FEAT_INS_EXPAND
11502 #ifdef FEAT_JUMPLIST
11508 #ifdef FEAT_LANGMAP
11511 #ifdef FEAT_LIBCALL
11514 #ifdef FEAT_LINEBREAK
11520 #ifdef FEAT_LISTCMDS
11523 #ifdef FEAT_LOCALMAP
11529 #ifdef FEAT_SESSION
11532 #ifdef FEAT_MODIFY_FNAME
11538 #ifdef FEAT_MOUSESHAPE
11541 #if defined(UNIX) || defined(VMS)
11542 # ifdef FEAT_MOUSE_DEC
11545 # ifdef FEAT_MOUSE_GPM
11548 # ifdef FEAT_MOUSE_JSB
11551 # ifdef FEAT_MOUSE_NET
11554 # ifdef FEAT_MOUSE_PTERM
11557 # ifdef FEAT_SYSMOUSE
11560 # ifdef FEAT_MOUSE_XTERM
11567 #ifdef FEAT_MBYTE_IME
11570 #ifdef FEAT_MULTI_LANG
11573 #ifdef FEAT_MZSCHEME
11574 #ifndef DYNAMIC_MZSCHEME
11581 #ifdef FEAT_OSFILETYPE
11584 #ifdef FEAT_PATH_EXTRA
11588 #ifndef DYNAMIC_PERL
11593 #ifndef DYNAMIC_PYTHON
11597 #ifdef FEAT_POSTSCRIPT
11600 #ifdef FEAT_PRINTER
11603 #ifdef FEAT_PROFILE
11606 #ifdef FEAT_RELTIME
11609 #ifdef FEAT_QUICKFIX
11612 #ifdef FEAT_RIGHTLEFT
11615 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11618 #ifdef FEAT_SCROLLBIND
11621 #ifdef FEAT_CMDL_INFO
11628 #ifdef FEAT_SMARTINDENT
11634 #ifdef FEAT_STL_OPT
11637 #ifdef FEAT_SUN_WORKSHOP
11640 #ifdef FEAT_NETBEANS_INTG
11643 #ifdef FEAT_ODB_EDITOR
11652 #if defined(USE_SYSTEM) || !defined(UNIX)
11655 #ifdef FEAT_TAG_BINS
11658 #ifdef FEAT_TAG_OLDSTATIC
11661 #ifdef FEAT_TAG_ANYWHITE
11665 # ifndef DYNAMIC_TCL
11672 #ifdef FEAT_TERMRESPONSE
11675 #ifdef FEAT_TEXTOBJ
11678 #ifdef HAVE_TGETENT
11684 #ifdef FEAT_TOOLBAR
11687 #ifdef FEAT_TRANSPARENCY
11690 #ifdef FEAT_USR_CMDS
11691 "user-commands", /* was accidentally included in 5.4 */
11694 #ifdef FEAT_VIMINFO
11697 #ifdef FEAT_VERTSPLIT
11700 #ifdef FEAT_VIRTUALEDIT
11706 #ifdef FEAT_VISUALEXTRA
11709 #ifdef FEAT_VREPLACE
11712 #ifdef FEAT_WILDIGN
11715 #ifdef FEAT_WILDMENU
11718 #ifdef FEAT_WINDOWS
11724 #ifdef FEAT_WRITEBACKUP
11730 #ifdef FEAT_XFONTSET
11736 #ifdef USE_XSMP_INTERACT
11739 #ifdef FEAT_XCLIPBOARD
11742 #ifdef FEAT_XTERM_SAVE
11745 #if defined(UNIX) && defined(FEAT_X11)
11751 name
= get_tv_string(&argvars
[0]);
11752 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11753 if (STRICMP(name
, has_list
[i
]) == 0)
11761 if (STRNICMP(name
, "patch", 5) == 0)
11762 n
= has_patch(atoi((char *)name
+ 5));
11763 else if (STRICMP(name
, "vim_starting") == 0)
11764 n
= (starting
!= 0);
11765 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11766 else if (STRICMP(name
, "balloon_multiline") == 0)
11767 n
= multiline_balloon_available();
11770 else if (STRICMP(name
, "tcl") == 0)
11771 n
= tcl_enabled(FALSE
);
11773 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11774 else if (STRICMP(name
, "iconv") == 0)
11775 n
= iconv_enabled(FALSE
);
11777 #ifdef DYNAMIC_MZSCHEME
11778 else if (STRICMP(name
, "mzscheme") == 0)
11779 n
= mzscheme_enabled(FALSE
);
11781 #ifdef DYNAMIC_RUBY
11782 else if (STRICMP(name
, "ruby") == 0)
11783 n
= ruby_enabled(FALSE
);
11785 #ifdef DYNAMIC_PYTHON
11786 else if (STRICMP(name
, "python") == 0)
11787 n
= python_enabled(FALSE
);
11789 #ifdef DYNAMIC_PERL
11790 else if (STRICMP(name
, "perl") == 0)
11791 n
= perl_enabled(FALSE
);
11794 else if (STRICMP(name
, "gui_running") == 0)
11795 n
= (gui
.in_use
|| gui
.starting
);
11796 # ifdef FEAT_GUI_W32
11797 else if (STRICMP(name
, "gui_win32s") == 0)
11798 n
= gui_is_win32s();
11800 # ifdef FEAT_BROWSE
11801 else if (STRICMP(name
, "browse") == 0)
11802 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11806 else if (STRICMP(name
, "syntax_items") == 0)
11807 n
= syntax_present(curbuf
);
11809 #if defined(WIN3264)
11810 else if (STRICMP(name
, "win95") == 0)
11811 n
= mch_windows95();
11813 #ifdef FEAT_NETBEANS_INTG
11814 else if (STRICMP(name
, "netbeans_enabled") == 0)
11819 rettv
->vval
.v_number
= n
;
11823 * "has_key()" function
11826 f_has_key(argvars
, rettv
)
11830 rettv
->vval
.v_number
= 0;
11831 if (argvars
[0].v_type
!= VAR_DICT
)
11833 EMSG(_(e_dictreq
));
11836 if (argvars
[0].vval
.v_dict
== NULL
)
11839 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11840 get_tv_string(&argvars
[1]), -1) != NULL
;
11844 * "haslocaldir()" function
11848 f_haslocaldir(argvars
, rettv
)
11852 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11856 * "hasmapto()" function
11859 f_hasmapto(argvars
, rettv
)
11865 char_u buf
[NUMBUFLEN
];
11868 name
= get_tv_string(&argvars
[0]);
11869 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11870 mode
= (char_u
*)"nvo";
11873 mode
= get_tv_string_buf(&argvars
[1], buf
);
11874 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11875 abbr
= get_tv_number(&argvars
[2]);
11878 if (map_to_exists(name
, mode
, abbr
))
11879 rettv
->vval
.v_number
= TRUE
;
11881 rettv
->vval
.v_number
= FALSE
;
11885 * "histadd()" function
11889 f_histadd(argvars
, rettv
)
11893 #ifdef FEAT_CMDHIST
11896 char_u buf
[NUMBUFLEN
];
11899 rettv
->vval
.v_number
= FALSE
;
11900 if (check_restricted() || check_secure())
11902 #ifdef FEAT_CMDHIST
11903 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11904 histype
= str
!= NULL
? get_histtype(str
) : -1;
11907 str
= get_tv_string_buf(&argvars
[1], buf
);
11910 add_to_history(histype
, str
, FALSE
, NUL
);
11911 rettv
->vval
.v_number
= TRUE
;
11919 * "histdel()" function
11923 f_histdel(argvars
, rettv
)
11927 #ifdef FEAT_CMDHIST
11929 char_u buf
[NUMBUFLEN
];
11932 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11935 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
11936 /* only one argument: clear entire history */
11937 n
= clr_history(get_histtype(str
));
11938 else if (argvars
[1].v_type
== VAR_NUMBER
)
11939 /* index given: remove that entry */
11940 n
= del_history_idx(get_histtype(str
),
11941 (int)get_tv_number(&argvars
[1]));
11943 /* string given: remove all matching entries */
11944 n
= del_history_entry(get_histtype(str
),
11945 get_tv_string_buf(&argvars
[1], buf
));
11946 rettv
->vval
.v_number
= n
;
11948 rettv
->vval
.v_number
= 0;
11953 * "histget()" function
11957 f_histget(argvars
, rettv
)
11961 #ifdef FEAT_CMDHIST
11966 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11968 rettv
->vval
.v_string
= NULL
;
11971 type
= get_histtype(str
);
11972 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11973 idx
= get_history_idx(type
);
11975 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
11976 /* -1 on type error */
11977 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
11980 rettv
->vval
.v_string
= NULL
;
11982 rettv
->v_type
= VAR_STRING
;
11986 * "histnr()" function
11990 f_histnr(argvars
, rettv
)
11996 #ifdef FEAT_CMDHIST
11997 char_u
*history
= get_tv_string_chk(&argvars
[0]);
11999 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
12000 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
12001 i
= get_history_idx(i
);
12005 rettv
->vval
.v_number
= i
;
12009 * "highlightID(name)" function
12012 f_hlID(argvars
, rettv
)
12016 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
12020 * "highlight_exists()" function
12023 f_hlexists(argvars
, rettv
)
12027 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
12031 * "hostname()" function
12035 f_hostname(argvars
, rettv
)
12039 char_u hostname
[256];
12041 mch_get_host_name(hostname
, 256);
12042 rettv
->v_type
= VAR_STRING
;
12043 rettv
->vval
.v_string
= vim_strsave(hostname
);
12051 f_iconv(argvars
, rettv
)
12056 char_u buf1
[NUMBUFLEN
];
12057 char_u buf2
[NUMBUFLEN
];
12058 char_u
*from
, *to
, *str
;
12062 rettv
->v_type
= VAR_STRING
;
12063 rettv
->vval
.v_string
= NULL
;
12066 str
= get_tv_string(&argvars
[0]);
12067 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
12068 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
12069 vimconv
.vc_type
= CONV_NONE
;
12070 convert_setup(&vimconv
, from
, to
);
12072 /* If the encodings are equal, no conversion needed. */
12073 if (vimconv
.vc_type
== CONV_NONE
)
12074 rettv
->vval
.v_string
= vim_strsave(str
);
12076 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
12078 convert_setup(&vimconv
, NULL
, NULL
);
12085 * "indent()" function
12088 f_indent(argvars
, rettv
)
12094 lnum
= get_tv_lnum(argvars
);
12095 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12096 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
12098 rettv
->vval
.v_number
= -1;
12102 * "index()" function
12105 f_index(argvars
, rettv
)
12114 rettv
->vval
.v_number
= -1;
12115 if (argvars
[0].v_type
!= VAR_LIST
)
12117 EMSG(_(e_listreq
));
12120 l
= argvars
[0].vval
.v_list
;
12123 item
= l
->lv_first
;
12124 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12128 /* Start at specified item. Use the cached index that list_find()
12129 * sets, so that a negative number also works. */
12130 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
12132 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12133 ic
= get_tv_number_chk(&argvars
[3], &error
);
12138 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
12139 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
12141 rettv
->vval
.v_number
= idx
;
12147 static int inputsecret_flag
= 0;
12149 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
12152 * This function is used by f_input() and f_inputdialog() functions. The third
12153 * argument to f_input() specifies the type of completion to use at the
12154 * prompt. The third argument to f_inputdialog() specifies the value to return
12155 * when the user cancels the prompt.
12158 get_user_input(argvars
, rettv
, inputdialog
)
12163 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
12166 char_u buf
[NUMBUFLEN
];
12167 int cmd_silent_save
= cmd_silent
;
12168 char_u
*defstr
= (char_u
*)"";
12169 int xp_type
= EXPAND_NOTHING
;
12170 char_u
*xp_arg
= NULL
;
12172 rettv
->v_type
= VAR_STRING
;
12173 rettv
->vval
.v_string
= NULL
;
12175 #ifdef NO_CONSOLE_INPUT
12176 /* While starting up, there is no place to enter text. */
12177 if (no_console_input())
12181 cmd_silent
= FALSE
; /* Want to see the prompt. */
12182 if (prompt
!= NULL
)
12184 /* Only the part of the message after the last NL is considered as
12185 * prompt for the command line */
12186 p
= vim_strrchr(prompt
, '\n');
12196 msg_puts_attr(prompt
, echo_attr
);
12197 msg_didout
= FALSE
;
12201 cmdline_row
= msg_row
;
12203 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12205 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
12206 if (defstr
!= NULL
)
12207 stuffReadbuffSpec(defstr
);
12209 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
12215 rettv
->vval
.v_string
= NULL
;
12217 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
12218 if (xp_name
== NULL
)
12221 xp_namelen
= (int)STRLEN(xp_name
);
12223 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
12229 if (defstr
!= NULL
)
12230 rettv
->vval
.v_string
=
12231 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
12236 /* since the user typed this, no need to wait for return */
12237 need_wait_return
= FALSE
;
12238 msg_didout
= FALSE
;
12240 cmd_silent
= cmd_silent_save
;
12244 * "input()" function
12245 * Also handles inputsecret() when inputsecret is set.
12248 f_input(argvars
, rettv
)
12252 get_user_input(argvars
, rettv
, FALSE
);
12256 * "inputdialog()" function
12259 f_inputdialog(argvars
, rettv
)
12263 #if defined(FEAT_GUI_TEXTDIALOG)
12264 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12265 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
12268 char_u buf
[NUMBUFLEN
];
12269 char_u
*defstr
= (char_u
*)"";
12271 message
= get_tv_string_chk(&argvars
[0]);
12272 if (argvars
[1].v_type
!= VAR_UNKNOWN
12273 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
12274 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
12277 if (message
!= NULL
&& defstr
!= NULL
12278 && do_dialog(VIM_QUESTION
, NULL
, message
,
12279 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
12280 rettv
->vval
.v_string
= vim_strsave(IObuff
);
12283 if (message
!= NULL
&& defstr
!= NULL
12284 && argvars
[1].v_type
!= VAR_UNKNOWN
12285 && argvars
[2].v_type
!= VAR_UNKNOWN
)
12286 rettv
->vval
.v_string
= vim_strsave(
12287 get_tv_string_buf(&argvars
[2], buf
));
12289 rettv
->vval
.v_string
= NULL
;
12291 rettv
->v_type
= VAR_STRING
;
12295 get_user_input(argvars
, rettv
, TRUE
);
12299 * "inputlist()" function
12302 f_inputlist(argvars
, rettv
)
12310 rettv
->vval
.v_number
= 0;
12311 #ifdef NO_CONSOLE_INPUT
12312 /* While starting up, there is no place to enter text. */
12313 if (no_console_input())
12316 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
12318 EMSG2(_(e_listarg
), "inputlist()");
12323 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
12324 lines_left
= Rows
; /* avoid more prompt */
12328 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
12330 msg_puts(get_tv_string(&li
->li_tv
));
12334 /* Ask for choice. */
12335 selected
= prompt_for_number(&mouse_used
);
12337 selected
-= lines_left
;
12339 rettv
->vval
.v_number
= selected
;
12343 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
12346 * "inputrestore()" function
12350 f_inputrestore(argvars
, rettv
)
12354 if (ga_userinput
.ga_len
> 0)
12356 --ga_userinput
.ga_len
;
12357 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12358 + ga_userinput
.ga_len
);
12359 rettv
->vval
.v_number
= 0; /* OK */
12361 else if (p_verbose
> 1)
12363 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
12364 rettv
->vval
.v_number
= 1; /* Failed */
12369 * "inputsave()" function
12373 f_inputsave(argvars
, rettv
)
12377 /* Add an entry to the stack of typeahead storage. */
12378 if (ga_grow(&ga_userinput
, 1) == OK
)
12380 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12381 + ga_userinput
.ga_len
);
12382 ++ga_userinput
.ga_len
;
12383 rettv
->vval
.v_number
= 0; /* OK */
12386 rettv
->vval
.v_number
= 1; /* Failed */
12390 * "inputsecret()" function
12393 f_inputsecret(argvars
, rettv
)
12398 ++inputsecret_flag
;
12399 f_input(argvars
, rettv
);
12401 --inputsecret_flag
;
12405 * "insert()" function
12408 f_insert(argvars
, rettv
)
12417 rettv
->vval
.v_number
= 0;
12418 if (argvars
[0].v_type
!= VAR_LIST
)
12419 EMSG2(_(e_listarg
), "insert()");
12420 else if ((l
= argvars
[0].vval
.v_list
) != NULL
12421 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
12423 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12424 before
= get_tv_number_chk(&argvars
[2], &error
);
12426 return; /* type error; errmsg already given */
12428 if (before
== l
->lv_len
)
12432 item
= list_find(l
, before
);
12435 EMSGN(_(e_listidx
), before
);
12441 list_insert_tv(l
, &argvars
[1], item
);
12442 copy_tv(&argvars
[0], rettv
);
12448 * "isdirectory()" function
12451 f_isdirectory(argvars
, rettv
)
12455 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
12459 * "islocked()" function
12462 f_islocked(argvars
, rettv
)
12470 rettv
->vval
.v_number
= -1;
12471 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
12473 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
12476 EMSG(_(e_trailing
));
12479 if (lv
.ll_tv
== NULL
)
12481 if (check_changedtick(lv
.ll_name
))
12482 rettv
->vval
.v_number
= 1; /* always locked */
12485 di
= find_var(lv
.ll_name
, NULL
);
12488 /* Consider a variable locked when:
12489 * 1. the variable itself is locked
12490 * 2. the value of the variable is locked.
12491 * 3. the List or Dict value is locked.
12493 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
12494 || tv_islocked(&di
->di_tv
));
12498 else if (lv
.ll_range
)
12499 EMSG(_("E786: Range not allowed"));
12500 else if (lv
.ll_newkey
!= NULL
)
12501 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
12502 else if (lv
.ll_list
!= NULL
)
12504 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
12506 /* Dictionary item. */
12507 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
12514 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
12517 * Turn a dict into a list:
12518 * "what" == 0: list of keys
12519 * "what" == 1: list of values
12520 * "what" == 2: list of items
12523 dict_list(argvars
, rettv
, what
)
12536 rettv
->vval
.v_number
= 0;
12537 if (argvars
[0].v_type
!= VAR_DICT
)
12539 EMSG(_(e_dictreq
));
12542 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
12545 if (rettv_list_alloc(rettv
) == FAIL
)
12548 todo
= (int)d
->dv_hashtab
.ht_used
;
12549 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12551 if (!HASHITEM_EMPTY(hi
))
12556 li
= listitem_alloc();
12559 list_append(rettv
->vval
.v_list
, li
);
12564 li
->li_tv
.v_type
= VAR_STRING
;
12565 li
->li_tv
.v_lock
= 0;
12566 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12568 else if (what
== 1)
12571 copy_tv(&di
->di_tv
, &li
->li_tv
);
12577 li
->li_tv
.v_type
= VAR_LIST
;
12578 li
->li_tv
.v_lock
= 0;
12579 li
->li_tv
.vval
.v_list
= l2
;
12584 li2
= listitem_alloc();
12587 list_append(l2
, li2
);
12588 li2
->li_tv
.v_type
= VAR_STRING
;
12589 li2
->li_tv
.v_lock
= 0;
12590 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12592 li2
= listitem_alloc();
12595 list_append(l2
, li2
);
12596 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12603 * "items(dict)" function
12606 f_items(argvars
, rettv
)
12610 dict_list(argvars
, rettv
, 2);
12614 * "join()" function
12617 f_join(argvars
, rettv
)
12624 rettv
->vval
.v_number
= 0;
12625 if (argvars
[0].v_type
!= VAR_LIST
)
12627 EMSG(_(e_listreq
));
12630 if (argvars
[0].vval
.v_list
== NULL
)
12632 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12633 sep
= (char_u
*)" ";
12635 sep
= get_tv_string_chk(&argvars
[1]);
12637 rettv
->v_type
= VAR_STRING
;
12641 ga_init2(&ga
, (int)sizeof(char), 80);
12642 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12643 ga_append(&ga
, NUL
);
12644 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12647 rettv
->vval
.v_string
= NULL
;
12651 * "keys()" function
12654 f_keys(argvars
, rettv
)
12658 dict_list(argvars
, rettv
, 0);
12662 * "last_buffer_nr()" function.
12666 f_last_buffer_nr(argvars
, rettv
)
12673 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12674 if (n
< buf
->b_fnum
)
12677 rettv
->vval
.v_number
= n
;
12684 f_len(argvars
, rettv
)
12688 switch (argvars
[0].v_type
)
12692 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12693 get_tv_string(&argvars
[0]));
12696 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12699 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12702 EMSG(_("E701: Invalid type for len()"));
12707 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12710 libcall_common(argvars
, rettv
, type
)
12715 #ifdef FEAT_LIBCALL
12717 char_u
**string_result
;
12721 rettv
->v_type
= type
;
12722 if (type
== VAR_NUMBER
)
12723 rettv
->vval
.v_number
= 0;
12725 rettv
->vval
.v_string
= NULL
;
12727 if (check_restricted() || check_secure())
12730 #ifdef FEAT_LIBCALL
12731 /* The first two args must be strings, otherwise its meaningless */
12732 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12735 if (argvars
[2].v_type
== VAR_STRING
)
12736 string_in
= argvars
[2].vval
.v_string
;
12737 if (type
== VAR_NUMBER
)
12738 string_result
= NULL
;
12740 string_result
= &rettv
->vval
.v_string
;
12741 if (mch_libcall(argvars
[0].vval
.v_string
,
12742 argvars
[1].vval
.v_string
,
12744 argvars
[2].vval
.v_number
,
12747 && type
== VAR_NUMBER
)
12748 rettv
->vval
.v_number
= nr_result
;
12754 * "libcall()" function
12757 f_libcall(argvars
, rettv
)
12761 libcall_common(argvars
, rettv
, VAR_STRING
);
12765 * "libcallnr()" function
12768 f_libcallnr(argvars
, rettv
)
12772 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12776 * "line(string)" function
12779 f_line(argvars
, rettv
)
12787 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12790 rettv
->vval
.v_number
= lnum
;
12794 * "line2byte(lnum)" function
12798 f_line2byte(argvars
, rettv
)
12802 #ifndef FEAT_BYTEOFF
12803 rettv
->vval
.v_number
= -1;
12807 lnum
= get_tv_lnum(argvars
);
12808 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12809 rettv
->vval
.v_number
= -1;
12811 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12812 if (rettv
->vval
.v_number
>= 0)
12813 ++rettv
->vval
.v_number
;
12818 * "lispindent(lnum)" function
12821 f_lispindent(argvars
, rettv
)
12829 pos
= curwin
->w_cursor
;
12830 lnum
= get_tv_lnum(argvars
);
12831 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12833 curwin
->w_cursor
.lnum
= lnum
;
12834 rettv
->vval
.v_number
= get_lisp_indent();
12835 curwin
->w_cursor
= pos
;
12839 rettv
->vval
.v_number
= -1;
12843 * "localtime()" function
12847 f_localtime(argvars
, rettv
)
12851 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12854 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12857 get_maparg(argvars
, rettv
, exact
)
12864 char_u buf
[NUMBUFLEN
];
12865 char_u
*keys_buf
= NULL
;
12871 /* return empty string for failure */
12872 rettv
->v_type
= VAR_STRING
;
12873 rettv
->vval
.v_string
= NULL
;
12875 keys
= get_tv_string(&argvars
[0]);
12879 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12881 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12882 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12883 abbr
= get_tv_number(&argvars
[2]);
12886 which
= (char_u
*)"";
12890 mode
= get_map_mode(&which
, 0);
12892 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12893 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
12894 vim_free(keys_buf
);
12898 ga
.ga_itemsize
= 1;
12899 ga
.ga_growsize
= 40;
12901 while (*rhs
!= NUL
)
12902 ga_concat(&ga
, str2special(&rhs
, FALSE
));
12904 ga_append(&ga
, NUL
);
12905 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12911 * "log10()" function
12914 f_log10(argvars
, rettv
)
12920 rettv
->v_type
= VAR_FLOAT
;
12921 if (get_float_arg(argvars
, &f
) == OK
)
12922 rettv
->vval
.v_float
= log10(f
);
12924 rettv
->vval
.v_float
= 0.0;
12932 f_map(argvars
, rettv
)
12936 filter_map(argvars
, rettv
, TRUE
);
12940 * "maparg()" function
12943 f_maparg(argvars
, rettv
)
12947 get_maparg(argvars
, rettv
, TRUE
);
12951 * "mapcheck()" function
12954 f_mapcheck(argvars
, rettv
)
12958 get_maparg(argvars
, rettv
, FALSE
);
12961 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
12964 find_some_match(argvars
, rettv
, type
)
12969 char_u
*str
= NULL
;
12970 char_u
*expr
= NULL
;
12972 regmatch_T regmatch
;
12973 char_u patbuf
[NUMBUFLEN
];
12974 char_u strbuf
[NUMBUFLEN
];
12978 colnr_T startcol
= 0;
12981 listitem_T
*li
= NULL
;
12983 char_u
*tofree
= NULL
;
12985 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12987 p_cpo
= (char_u
*)"";
12989 rettv
->vval
.v_number
= -1;
12992 /* return empty list when there are no matches */
12993 if (rettv_list_alloc(rettv
) == FAIL
)
12996 else if (type
== 2)
12998 rettv
->v_type
= VAR_STRING
;
12999 rettv
->vval
.v_string
= NULL
;
13002 if (argvars
[0].v_type
== VAR_LIST
)
13004 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
13009 expr
= str
= get_tv_string(&argvars
[0]);
13011 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
13015 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13019 start
= get_tv_number_chk(&argvars
[2], &error
);
13024 li
= list_find(l
, start
);
13027 idx
= l
->lv_idx
; /* use the cached index */
13033 if (start
> (long)STRLEN(str
))
13035 /* When "count" argument is there ignore matches before "start",
13036 * otherwise skip part of the string. Differs when pattern is "^"
13038 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13044 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13045 nth
= get_tv_number_chk(&argvars
[3], &error
);
13050 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
13051 if (regmatch
.regprog
!= NULL
)
13053 regmatch
.rm_ic
= p_ic
;
13065 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
13070 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
13072 if (match
&& --nth
<= 0)
13074 if (l
== NULL
&& !match
)
13077 /* Advance to just after the match. */
13086 startcol
= (colnr_T
)(regmatch
.startp
[0]
13087 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
13089 startcol
= regmatch
.startp
[0] + 1 - str
;
13100 /* return list with matched string and submatches */
13101 for (i
= 0; i
< NSUBEXP
; ++i
)
13103 if (regmatch
.endp
[i
] == NULL
)
13105 if (list_append_string(rettv
->vval
.v_list
,
13106 (char_u
*)"", 0) == FAIL
)
13109 else if (list_append_string(rettv
->vval
.v_list
,
13110 regmatch
.startp
[i
],
13111 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
13116 else if (type
== 2)
13118 /* return matched string */
13120 copy_tv(&li
->li_tv
, rettv
);
13122 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
13123 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
13125 else if (l
!= NULL
)
13126 rettv
->vval
.v_number
= idx
;
13130 rettv
->vval
.v_number
=
13131 (varnumber_T
)(regmatch
.startp
[0] - str
);
13133 rettv
->vval
.v_number
=
13134 (varnumber_T
)(regmatch
.endp
[0] - str
);
13135 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
13138 vim_free(regmatch
.regprog
);
13147 * "match()" function
13150 f_match(argvars
, rettv
)
13154 find_some_match(argvars
, rettv
, 1);
13158 * "matchadd()" function
13161 f_matchadd(argvars
, rettv
)
13165 #ifdef FEAT_SEARCH_EXTRA
13166 char_u buf
[NUMBUFLEN
];
13167 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
13168 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
13169 int prio
= 10; /* default priority */
13173 rettv
->vval
.v_number
= -1;
13175 if (grp
== NULL
|| pat
== NULL
)
13177 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13179 prio
= get_tv_number_chk(&argvars
[2], &error
);
13180 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13181 id
= get_tv_number_chk(&argvars
[3], &error
);
13185 if (id
>= 1 && id
<= 3)
13187 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
13191 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
13196 * "matcharg()" function
13199 f_matcharg(argvars
, rettv
)
13203 if (rettv_list_alloc(rettv
) == OK
)
13205 #ifdef FEAT_SEARCH_EXTRA
13206 int id
= get_tv_number(&argvars
[0]);
13209 if (id
>= 1 && id
<= 3)
13211 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
13213 list_append_string(rettv
->vval
.v_list
,
13214 syn_id2name(m
->hlg_id
), -1);
13215 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
13219 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13220 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13228 * "matchdelete()" function
13231 f_matchdelete(argvars
, rettv
)
13235 #ifdef FEAT_SEARCH_EXTRA
13236 rettv
->vval
.v_number
= match_delete(curwin
,
13237 (int)get_tv_number(&argvars
[0]), TRUE
);
13242 * "matchend()" function
13245 f_matchend(argvars
, rettv
)
13249 find_some_match(argvars
, rettv
, 0);
13253 * "matchlist()" function
13256 f_matchlist(argvars
, rettv
)
13260 find_some_match(argvars
, rettv
, 3);
13264 * "matchstr()" function
13267 f_matchstr(argvars
, rettv
)
13271 find_some_match(argvars
, rettv
, 2);
13274 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
13277 max_min(argvars
, rettv
, domax
)
13286 if (argvars
[0].v_type
== VAR_LIST
)
13291 l
= argvars
[0].vval
.v_list
;
13297 n
= get_tv_number_chk(&li
->li_tv
, &error
);
13303 i
= get_tv_number_chk(&li
->li_tv
, &error
);
13304 if (domax
? i
> n
: i
< n
)
13310 else if (argvars
[0].v_type
== VAR_DICT
)
13317 d
= argvars
[0].vval
.v_dict
;
13320 todo
= (int)d
->dv_hashtab
.ht_used
;
13321 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
13323 if (!HASHITEM_EMPTY(hi
))
13326 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
13332 else if (domax
? i
> n
: i
< n
)
13339 EMSG(_(e_listdictarg
));
13340 rettv
->vval
.v_number
= error
? 0 : n
;
13347 f_max(argvars
, rettv
)
13351 max_min(argvars
, rettv
, TRUE
);
13358 f_min(argvars
, rettv
)
13362 max_min(argvars
, rettv
, FALSE
);
13365 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
13368 * Create the directory in which "dir" is located, and higher levels when
13372 mkdir_recurse(dir
, prot
)
13380 /* Get end of directory name in "dir".
13381 * We're done when it's "/" or "c:/". */
13382 p
= gettail_sep(dir
);
13383 if (p
<= get_past_head(dir
))
13386 /* If the directory exists we're done. Otherwise: create it.*/
13387 updir
= vim_strnsave(dir
, (int)(p
- dir
));
13390 if (mch_isdir(updir
))
13392 else if (mkdir_recurse(updir
, prot
) == OK
)
13393 r
= vim_mkdir_emsg(updir
, prot
);
13400 * "mkdir()" function
13403 f_mkdir(argvars
, rettv
)
13408 char_u buf
[NUMBUFLEN
];
13411 rettv
->vval
.v_number
= FAIL
;
13412 if (check_restricted() || check_secure())
13415 dir
= get_tv_string_buf(&argvars
[0], buf
);
13416 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13418 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13419 prot
= get_tv_number_chk(&argvars
[2], NULL
);
13420 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
13421 mkdir_recurse(dir
, prot
);
13423 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
13428 * "mode()" function
13432 f_mode(argvars
, rettv
)
13445 buf
[0] = VIsual_mode
+ 's' - 'v';
13447 buf
[0] = VIsual_mode
;
13451 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
13452 || State
== CONFIRM
)
13455 if (State
== ASKMORE
)
13457 else if (State
== CONFIRM
)
13460 else if (State
== EXTERNCMD
)
13462 else if (State
& INSERT
)
13464 #ifdef FEAT_VREPLACE
13465 if (State
& VREPLACE_FLAG
)
13472 if (State
& REPLACE_FLAG
)
13477 else if (State
& CMDLINE
)
13483 else if (exmode_active
)
13495 /* A zero number or empty string argument: return only major mode. */
13496 if (!(argvars
[0].v_type
== VAR_NUMBER
&& argvars
[0].vval
.v_number
!= 0)
13497 && !(argvars
[0].v_type
== VAR_STRING
13498 && *get_tv_string(&argvars
[0]) != NUL
))
13501 rettv
->vval
.v_string
= vim_strsave(buf
);
13502 rettv
->v_type
= VAR_STRING
;
13506 * "nextnonblank()" function
13509 f_nextnonblank(argvars
, rettv
)
13515 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
13517 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
13522 if (*skipwhite(ml_get(lnum
)) != NUL
)
13525 rettv
->vval
.v_number
= lnum
;
13529 * "nr2char()" function
13532 f_nr2char(argvars
, rettv
)
13536 char_u buf
[NUMBUFLEN
];
13540 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
13544 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
13547 rettv
->v_type
= VAR_STRING
;
13548 rettv
->vval
.v_string
= vim_strsave(buf
);
13552 * "pathshorten()" function
13555 f_pathshorten(argvars
, rettv
)
13561 rettv
->v_type
= VAR_STRING
;
13562 p
= get_tv_string_chk(&argvars
[0]);
13564 rettv
->vval
.v_string
= NULL
;
13567 p
= vim_strsave(p
);
13568 rettv
->vval
.v_string
= p
;
13579 f_pow(argvars
, rettv
)
13585 rettv
->v_type
= VAR_FLOAT
;
13586 if (get_float_arg(argvars
, &fx
) == OK
13587 && get_float_arg(&argvars
[1], &fy
) == OK
)
13588 rettv
->vval
.v_float
= pow(fx
, fy
);
13590 rettv
->vval
.v_float
= 0.0;
13595 * "prevnonblank()" function
13598 f_prevnonblank(argvars
, rettv
)
13604 lnum
= get_tv_lnum(argvars
);
13605 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
13608 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
13610 rettv
->vval
.v_number
= lnum
;
13613 #ifdef HAVE_STDARG_H
13614 /* This dummy va_list is here because:
13615 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13616 * - locally in the function results in a "used before set" warning
13617 * - using va_start() to initialize it gives "function with fixed args" error */
13622 * "printf()" function
13625 f_printf(argvars
, rettv
)
13629 rettv
->v_type
= VAR_STRING
;
13630 rettv
->vval
.v_string
= NULL
;
13631 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
13633 char_u buf
[NUMBUFLEN
];
13636 int saved_did_emsg
= did_emsg
;
13639 /* Get the required length, allocate the buffer and do it for real. */
13641 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
13642 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
13645 s
= alloc(len
+ 1);
13648 rettv
->vval
.v_string
= s
;
13649 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
13652 did_emsg
|= saved_did_emsg
;
13658 * "pumvisible()" function
13662 f_pumvisible(argvars
, rettv
)
13666 rettv
->vval
.v_number
= 0;
13667 #ifdef FEAT_INS_EXPAND
13669 rettv
->vval
.v_number
= 1;
13674 * "range()" function
13677 f_range(argvars
, rettv
)
13687 start
= get_tv_number_chk(&argvars
[0], &error
);
13688 if (argvars
[1].v_type
== VAR_UNKNOWN
)
13695 end
= get_tv_number_chk(&argvars
[1], &error
);
13696 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13697 stride
= get_tv_number_chk(&argvars
[2], &error
);
13700 rettv
->vval
.v_number
= 0;
13702 return; /* type error; errmsg already given */
13704 EMSG(_("E726: Stride is zero"));
13705 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
13706 EMSG(_("E727: Start past end"));
13709 if (rettv_list_alloc(rettv
) == OK
)
13710 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
13711 if (list_append_number(rettv
->vval
.v_list
,
13712 (varnumber_T
)i
) == FAIL
)
13718 * "readfile()" function
13721 f_readfile(argvars
, rettv
)
13725 int binary
= FALSE
;
13729 #define FREAD_SIZE 200 /* optimized for text lines */
13730 char_u buf
[FREAD_SIZE
];
13731 int readlen
; /* size of last fread() */
13732 int buflen
; /* nr of valid chars in buf[] */
13733 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
13734 int tolist
; /* first byte in buf[] still to be put in list */
13735 int chop
; /* how many CR to chop off */
13736 char_u
*prev
= NULL
; /* previously read bytes, if any */
13737 int prevlen
= 0; /* length of "prev" if not NULL */
13740 long maxline
= MAXLNUM
;
13743 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13745 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
13747 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13748 maxline
= get_tv_number(&argvars
[2]);
13751 if (rettv_list_alloc(rettv
) == FAIL
)
13754 /* Always open the file in binary mode, library functions have a mind of
13755 * their own about CR-LF conversion. */
13756 fname
= get_tv_string(&argvars
[0]);
13757 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
13759 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
13764 while (cnt
< maxline
|| maxline
< 0)
13766 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
13767 buflen
= filtd
+ readlen
;
13769 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
13771 if (buf
[filtd
] == '\n' || readlen
<= 0)
13773 /* Only when in binary mode add an empty list item when the
13774 * last line ends in a '\n'. */
13775 if (!binary
&& readlen
== 0 && filtd
== 0)
13778 /* Found end-of-line or end-of-file: add a text line to the
13782 while (filtd
- chop
- 1 >= tolist
13783 && buf
[filtd
- chop
- 1] == '\r')
13785 len
= filtd
- tolist
- chop
;
13787 s
= vim_strnsave(buf
+ tolist
, len
);
13790 s
= alloc((unsigned)(prevlen
+ len
+ 1));
13793 mch_memmove(s
, prev
, prevlen
);
13796 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
13797 s
[prevlen
+ len
] = NUL
;
13800 tolist
= filtd
+ 1;
13802 li
= listitem_alloc();
13808 li
->li_tv
.v_type
= VAR_STRING
;
13809 li
->li_tv
.v_lock
= 0;
13810 li
->li_tv
.vval
.v_string
= s
;
13811 list_append(rettv
->vval
.v_list
, li
);
13813 if (++cnt
>= maxline
&& maxline
>= 0)
13818 else if (buf
[filtd
] == NUL
)
13826 /* "buf" is full, need to move text to an allocated buffer */
13829 prev
= vim_strnsave(buf
, buflen
);
13834 s
= alloc((unsigned)(prevlen
+ buflen
));
13837 mch_memmove(s
, prev
, prevlen
);
13838 mch_memmove(s
+ prevlen
, buf
, buflen
);
13848 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13854 * For a negative line count use only the lines at the end of the file,
13858 while (cnt
> -maxline
)
13860 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
13868 #if defined(FEAT_RELTIME)
13869 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
13872 * Convert a List to proftime_T.
13873 * Return FAIL when there is something wrong.
13876 list2proftime(arg
, tm
)
13883 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
13884 || arg
->vval
.v_list
->lv_len
!= 2)
13886 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
13887 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
13895 return error
? FAIL
: OK
;
13897 #endif /* FEAT_RELTIME */
13900 * "reltime()" function
13903 f_reltime(argvars
, rettv
)
13907 #ifdef FEAT_RELTIME
13911 if (argvars
[0].v_type
== VAR_UNKNOWN
)
13913 /* No arguments: get current time. */
13914 profile_start(&res
);
13916 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
13918 if (list2proftime(&argvars
[0], &res
) == FAIL
)
13924 /* Two arguments: compute the difference. */
13925 if (list2proftime(&argvars
[0], &start
) == FAIL
13926 || list2proftime(&argvars
[1], &res
) == FAIL
)
13928 profile_sub(&res
, &start
);
13931 if (rettv_list_alloc(rettv
) == OK
)
13942 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
13943 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
13949 * "reltimestr()" function
13952 f_reltimestr(argvars
, rettv
)
13956 #ifdef FEAT_RELTIME
13960 rettv
->v_type
= VAR_STRING
;
13961 rettv
->vval
.v_string
= NULL
;
13962 #ifdef FEAT_RELTIME
13963 if (list2proftime(&argvars
[0], &tm
) == OK
)
13964 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
13968 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13969 static void make_connection
__ARGS((void));
13970 static int check_connection
__ARGS((void));
13975 if (X_DISPLAY
== NULL
13981 x_force_connect
= TRUE
;
13983 x_force_connect
= FALSE
;
13991 if (X_DISPLAY
== NULL
)
13993 EMSG(_("E240: No connection to Vim server"));
14000 #ifdef FEAT_CLIENTSERVER
14001 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
14004 remote_common(argvars
, rettv
, expr
)
14009 char_u
*server_name
;
14012 char_u buf
[NUMBUFLEN
];
14015 # elif defined(FEAT_X11)
14017 # elif defined(MAC_CLIENTSERVER)
14018 int w
; // This is the port number ('w' is a bit confusing)
14021 if (check_restricted() || check_secure())
14025 if (check_connection() == FAIL
)
14029 server_name
= get_tv_string_chk(&argvars
[0]);
14030 if (server_name
== NULL
)
14031 return; /* type error; errmsg already given */
14032 keys
= get_tv_string_buf(&argvars
[1], buf
);
14034 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14035 # elif defined(FEAT_X11)
14036 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
14038 # elif defined(MAC_CLIENTSERVER)
14039 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14043 EMSG(r
); /* sending worked but evaluation failed */
14045 EMSG2(_("E241: Unable to send to %s"), server_name
);
14049 rettv
->vval
.v_string
= r
;
14051 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14057 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
14058 v
.di_tv
.v_type
= VAR_STRING
;
14059 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
14060 idvar
= get_tv_string_chk(&argvars
[2]);
14062 set_var(idvar
, &v
.di_tv
, FALSE
);
14063 vim_free(v
.di_tv
.vval
.v_string
);
14069 * "remote_expr()" function
14073 f_remote_expr(argvars
, rettv
)
14077 rettv
->v_type
= VAR_STRING
;
14078 rettv
->vval
.v_string
= NULL
;
14079 #ifdef FEAT_CLIENTSERVER
14080 remote_common(argvars
, rettv
, TRUE
);
14085 * "remote_foreground()" function
14089 f_remote_foreground(argvars
, rettv
)
14093 rettv
->vval
.v_number
= 0;
14094 #ifdef FEAT_CLIENTSERVER
14096 /* On Win32 it's done in this application. */
14098 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
14100 if (server_name
!= NULL
)
14101 serverForeground(server_name
);
14103 # elif defined(FEAT_X11) || defined(MAC_CLIENTSERVER)
14104 /* Send a foreground() expression to the server. */
14105 argvars
[1].v_type
= VAR_STRING
;
14106 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
14107 argvars
[2].v_type
= VAR_UNKNOWN
;
14108 remote_common(argvars
, rettv
, TRUE
);
14109 vim_free(argvars
[1].vval
.v_string
);
14116 f_remote_peek(argvars
, rettv
)
14120 #ifdef FEAT_CLIENTSERVER
14128 if (check_restricted() || check_secure())
14130 rettv
->vval
.v_number
= -1;
14133 serverid
= get_tv_string_chk(&argvars
[0]);
14134 if (serverid
== NULL
)
14136 rettv
->vval
.v_number
= -1;
14137 return; /* type error; errmsg already given */
14140 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14142 rettv
->vval
.v_number
= -1;
14145 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
14146 rettv
->vval
.v_number
= (s
!= NULL
);
14148 # elif defined(FEAT_X11)
14149 rettv
->vval
.v_number
= 0;
14150 if (check_connection() == FAIL
)
14153 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
14154 serverStrToWin(serverid
), &s
);
14155 # elif defined(MAC_CLIENTSERVER)
14156 rettv
->vval
.v_number
= serverPeekReply(serverStrToPort(serverid
), &s
);
14159 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
14163 v
.di_tv
.v_type
= VAR_STRING
;
14164 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
14165 retvar
= get_tv_string_chk(&argvars
[1]);
14166 if (retvar
!= NULL
)
14167 set_var(retvar
, &v
.di_tv
, FALSE
);
14168 vim_free(v
.di_tv
.vval
.v_string
);
14171 rettv
->vval
.v_number
= -1;
14177 f_remote_read(argvars
, rettv
)
14183 #ifdef FEAT_CLIENTSERVER
14184 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
14186 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
14189 /* The server's HWND is encoded in the 'id' parameter */
14192 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14194 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
14196 # elif defined(FEAT_X11)
14197 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
14198 serverStrToWin(serverid
), &r
, FALSE
) < 0)
14199 # elif defined(MAC_CLIENTSERVER)
14200 if (serverReadReply(serverStrToPort(serverid
), &r
) < 0)
14202 EMSG(_("E277: Unable to read a server reply"));
14205 rettv
->v_type
= VAR_STRING
;
14206 rettv
->vval
.v_string
= r
;
14210 * "remote_send()" function
14214 f_remote_send(argvars
, rettv
)
14218 rettv
->v_type
= VAR_STRING
;
14219 rettv
->vval
.v_string
= NULL
;
14220 #ifdef FEAT_CLIENTSERVER
14221 remote_common(argvars
, rettv
, FALSE
);
14226 * "remove()" function
14229 f_remove(argvars
, rettv
)
14234 listitem_T
*item
, *item2
;
14242 rettv
->vval
.v_number
= 0;
14243 if (argvars
[0].v_type
== VAR_DICT
)
14245 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14246 EMSG2(_(e_toomanyarg
), "remove()");
14247 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
14248 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
14250 key
= get_tv_string_chk(&argvars
[1]);
14253 di
= dict_find(d
, key
, -1);
14255 EMSG2(_(e_dictkey
), key
);
14258 *rettv
= di
->di_tv
;
14259 init_tv(&di
->di_tv
);
14260 dictitem_remove(d
, di
);
14265 else if (argvars
[0].v_type
!= VAR_LIST
)
14266 EMSG2(_(e_listdictarg
), "remove()");
14267 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14268 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
14272 idx
= get_tv_number_chk(&argvars
[1], &error
);
14274 ; /* type error: do nothing, errmsg already given */
14275 else if ((item
= list_find(l
, idx
)) == NULL
)
14276 EMSGN(_(e_listidx
), idx
);
14279 if (argvars
[2].v_type
== VAR_UNKNOWN
)
14281 /* Remove one item, return its value. */
14282 list_remove(l
, item
, item
);
14283 *rettv
= item
->li_tv
;
14288 /* Remove range of items, return list with values. */
14289 end
= get_tv_number_chk(&argvars
[2], &error
);
14291 ; /* type error: do nothing */
14292 else if ((item2
= list_find(l
, end
)) == NULL
)
14293 EMSGN(_(e_listidx
), end
);
14298 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
14304 if (li
== NULL
) /* didn't find "item2" after "item" */
14305 EMSG(_(e_invrange
));
14308 list_remove(l
, item
, item2
);
14309 if (rettv_list_alloc(rettv
) == OK
)
14311 l
= rettv
->vval
.v_list
;
14312 l
->lv_first
= item
;
14313 l
->lv_last
= item2
;
14314 item
->li_prev
= NULL
;
14315 item2
->li_next
= NULL
;
14326 * "rename({from}, {to})" function
14329 f_rename(argvars
, rettv
)
14333 char_u buf
[NUMBUFLEN
];
14335 if (check_restricted() || check_secure())
14336 rettv
->vval
.v_number
= -1;
14338 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
14339 get_tv_string_buf(&argvars
[1], buf
));
14343 * "repeat()" function
14347 f_repeat(argvars
, rettv
)
14358 n
= get_tv_number(&argvars
[1]);
14359 if (argvars
[0].v_type
== VAR_LIST
)
14361 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
14363 if (list_extend(rettv
->vval
.v_list
,
14364 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
14369 p
= get_tv_string(&argvars
[0]);
14370 rettv
->v_type
= VAR_STRING
;
14371 rettv
->vval
.v_string
= NULL
;
14373 slen
= (int)STRLEN(p
);
14378 r
= alloc(len
+ 1);
14381 for (i
= 0; i
< n
; i
++)
14382 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
14386 rettv
->vval
.v_string
= r
;
14391 * "resolve()" function
14394 f_resolve(argvars
, rettv
)
14400 p
= get_tv_string(&argvars
[0]);
14401 #ifdef FEAT_SHORTCUT
14405 v
= mch_resolve_shortcut(p
);
14407 rettv
->vval
.v_string
= v
;
14409 rettv
->vval
.v_string
= vim_strsave(p
);
14412 # ifdef HAVE_READLINK
14414 char_u buf
[MAXPATHL
+ 1];
14417 char_u
*remain
= NULL
;
14419 int is_relative_to_current
= FALSE
;
14420 int has_trailing_pathsep
= FALSE
;
14423 p
= vim_strsave(p
);
14425 if (p
[0] == '.' && (vim_ispathsep(p
[1])
14426 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
14427 is_relative_to_current
= TRUE
;
14430 if (len
> 0 && after_pathsep(p
, p
+ len
))
14431 has_trailing_pathsep
= TRUE
;
14433 q
= getnextcomp(p
);
14436 /* Separate the first path component in "p", and keep the
14437 * remainder (beginning with the path separator). */
14438 remain
= vim_strsave(q
- 1);
14446 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
14455 EMSG(_("E655: Too many symbolic links (cycle?)"));
14456 rettv
->vval
.v_string
= NULL
;
14460 /* Ensure that the result will have a trailing path separator
14461 * if the argument has one. */
14462 if (remain
== NULL
&& has_trailing_pathsep
)
14465 /* Separate the first path component in the link value and
14466 * concatenate the remainders. */
14467 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
14470 if (remain
== NULL
)
14471 remain
= vim_strsave(q
- 1);
14474 cpy
= concat_str(q
- 1, remain
);
14485 if (q
> p
&& *q
== NUL
)
14487 /* Ignore trailing path separator. */
14491 if (q
> p
&& !mch_isFullName(buf
))
14493 /* symlink is relative to directory of argument */
14494 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
14498 STRCPY(gettail(cpy
), buf
);
14506 p
= vim_strsave(buf
);
14510 if (remain
== NULL
)
14513 /* Append the first path component of "remain" to "p". */
14514 q
= getnextcomp(remain
+ 1);
14515 len
= q
- remain
- (*q
!= NUL
);
14516 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
14519 STRNCAT(cpy
, remain
, len
);
14523 /* Shorten "remain". */
14525 STRMOVE(remain
, q
- 1);
14533 /* If the result is a relative path name, make it explicitly relative to
14534 * the current directory if and only if the argument had this form. */
14535 if (!vim_ispathsep(*p
))
14537 if (is_relative_to_current
14541 || vim_ispathsep(p
[1])
14544 || vim_ispathsep(p
[2]))))))
14546 /* Prepend "./". */
14547 cpy
= concat_str((char_u
*)"./", p
);
14554 else if (!is_relative_to_current
)
14556 /* Strip leading "./". */
14558 while (q
[0] == '.' && vim_ispathsep(q
[1]))
14565 /* Ensure that the result will have no trailing path separator
14566 * if the argument had none. But keep "/" or "//". */
14567 if (!has_trailing_pathsep
)
14570 if (after_pathsep(p
, q
))
14571 *gettail_sep(p
) = NUL
;
14574 rettv
->vval
.v_string
= p
;
14577 rettv
->vval
.v_string
= vim_strsave(p
);
14581 simplify_filename(rettv
->vval
.v_string
);
14583 #ifdef HAVE_READLINK
14586 rettv
->v_type
= VAR_STRING
;
14590 * "reverse({list})" function
14593 f_reverse(argvars
, rettv
)
14598 listitem_T
*li
, *ni
;
14600 rettv
->vval
.v_number
= 0;
14601 if (argvars
[0].v_type
!= VAR_LIST
)
14602 EMSG2(_(e_listarg
), "reverse()");
14603 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14604 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
14607 l
->lv_first
= l
->lv_last
= NULL
;
14612 list_append(l
, li
);
14615 rettv
->vval
.v_list
= l
;
14616 rettv
->v_type
= VAR_LIST
;
14618 l
->lv_idx
= l
->lv_len
- l
->lv_idx
- 1;
14622 #define SP_NOMOVE 0x01 /* don't move cursor */
14623 #define SP_REPEAT 0x02 /* repeat to find outer pair */
14624 #define SP_RETCOUNT 0x04 /* return matchcount */
14625 #define SP_SETPCMARK 0x08 /* set previous context mark */
14626 #define SP_START 0x10 /* accept match at start position */
14627 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14628 #define SP_END 0x40 /* leave cursor at end of match */
14630 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
14633 * Get flags for a search function.
14634 * Possibly sets "p_ws".
14635 * Returns BACKWARD, FORWARD or zero (for an error).
14638 get_search_arg(varp
, flagsp
)
14644 char_u nbuf
[NUMBUFLEN
];
14647 if (varp
->v_type
!= VAR_UNKNOWN
)
14649 flags
= get_tv_string_buf_chk(varp
, nbuf
);
14651 return 0; /* type error; errmsg already given */
14652 while (*flags
!= NUL
)
14656 case 'b': dir
= BACKWARD
; break;
14657 case 'w': p_ws
= TRUE
; break;
14658 case 'W': p_ws
= FALSE
; break;
14660 if (flagsp
!= NULL
)
14663 case 'c': mask
= SP_START
; break;
14664 case 'e': mask
= SP_END
; break;
14665 case 'm': mask
= SP_RETCOUNT
; break;
14666 case 'n': mask
= SP_NOMOVE
; break;
14667 case 'p': mask
= SP_SUBPAT
; break;
14668 case 'r': mask
= SP_REPEAT
; break;
14669 case 's': mask
= SP_SETPCMARK
; break;
14673 EMSG2(_(e_invarg2
), flags
);
14688 * Shared by search() and searchpos() functions
14691 search_cmn(argvars
, match_pos
, flagsp
)
14700 int save_p_ws
= p_ws
;
14702 int retval
= 0; /* default: FAIL */
14703 long lnum_stop
= 0;
14705 #ifdef FEAT_RELTIME
14706 long time_limit
= 0;
14708 int options
= SEARCH_KEEP
;
14711 pat
= get_tv_string(&argvars
[0]);
14712 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
14716 if (flags
& SP_START
)
14717 options
|= SEARCH_START
;
14718 if (flags
& SP_END
)
14719 options
|= SEARCH_END
;
14721 /* Optional arguments: line number to stop searching and timeout. */
14722 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14724 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
14727 #ifdef FEAT_RELTIME
14728 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
14730 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
14731 if (time_limit
< 0)
14737 #ifdef FEAT_RELTIME
14738 /* Set the time limit, if there is one. */
14739 profile_setlimit(time_limit
, &tm
);
14743 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
14744 * Check to make sure only those flags are set.
14745 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14746 * flags cannot be set. Check for that condition also.
14748 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
14749 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14751 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
14755 pos
= save_cursor
= curwin
->w_cursor
;
14756 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14757 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
14758 if (subpatnum
!= FAIL
)
14760 if (flags
& SP_SUBPAT
)
14761 retval
= subpatnum
;
14764 if (flags
& SP_SETPCMARK
)
14766 curwin
->w_cursor
= pos
;
14767 if (match_pos
!= NULL
)
14769 /* Store the match cursor position */
14770 match_pos
->lnum
= pos
.lnum
;
14771 match_pos
->col
= pos
.col
+ 1;
14773 /* "/$" will put the cursor after the end of the line, may need to
14774 * correct that here */
14778 /* If 'n' flag is used: restore cursor position. */
14779 if (flags
& SP_NOMOVE
)
14780 curwin
->w_cursor
= save_cursor
;
14782 curwin
->w_set_curswant
= TRUE
;
14791 * "round({float})" function
14794 f_round(argvars
, rettv
)
14800 rettv
->v_type
= VAR_FLOAT
;
14801 if (get_float_arg(argvars
, &f
) == OK
)
14802 /* round() is not in C90, use ceil() or floor() instead. */
14803 rettv
->vval
.v_float
= f
> 0 ? floor(f
+ 0.5) : ceil(f
- 0.5);
14805 rettv
->vval
.v_float
= 0.0;
14810 * "search()" function
14813 f_search(argvars
, rettv
)
14819 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
14823 * "searchdecl()" function
14826 f_searchdecl(argvars
, rettv
)
14835 rettv
->vval
.v_number
= 1; /* default: FAIL */
14837 name
= get_tv_string_chk(&argvars
[0]);
14838 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14840 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
14841 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14842 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
14844 if (!error
&& name
!= NULL
)
14845 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
14846 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
14850 * Used by searchpair() and searchpairpos()
14853 searchpair_cmn(argvars
, match_pos
)
14857 char_u
*spat
, *mpat
, *epat
;
14859 int save_p_ws
= p_ws
;
14862 char_u nbuf1
[NUMBUFLEN
];
14863 char_u nbuf2
[NUMBUFLEN
];
14864 char_u nbuf3
[NUMBUFLEN
];
14865 int retval
= 0; /* default: FAIL */
14866 long lnum_stop
= 0;
14867 long time_limit
= 0;
14869 /* Get the three pattern arguments: start, middle, end. */
14870 spat
= get_tv_string_chk(&argvars
[0]);
14871 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
14872 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
14873 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
14874 goto theend
; /* type error */
14876 /* Handle the optional fourth argument: flags */
14877 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
14881 /* Don't accept SP_END or SP_SUBPAT.
14882 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14884 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
14885 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14887 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
14891 /* Using 'r' implies 'W', otherwise it doesn't work. */
14892 if (flags
& SP_REPEAT
)
14895 /* Optional fifth argument: skip expression */
14896 if (argvars
[3].v_type
== VAR_UNKNOWN
14897 || argvars
[4].v_type
== VAR_UNKNOWN
)
14898 skip
= (char_u
*)"";
14901 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
14902 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
14904 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
14907 #ifdef FEAT_RELTIME
14908 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
14910 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
14911 if (time_limit
< 0)
14918 goto theend
; /* type error */
14920 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
14921 match_pos
, lnum_stop
, time_limit
);
14930 * "searchpair()" function
14933 f_searchpair(argvars
, rettv
)
14937 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
14941 * "searchpairpos()" function
14944 f_searchpairpos(argvars
, rettv
)
14952 rettv
->vval
.v_number
= 0;
14954 if (rettv_list_alloc(rettv
) == FAIL
)
14957 if (searchpair_cmn(argvars
, &match_pos
) > 0)
14959 lnum
= match_pos
.lnum
;
14960 col
= match_pos
.col
;
14963 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14964 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14968 * Search for a start/middle/end thing.
14969 * Used by searchpair(), see its documentation for the details.
14970 * Returns 0 or -1 for no match,
14973 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
14974 lnum_stop
, time_limit
)
14975 char_u
*spat
; /* start pattern */
14976 char_u
*mpat
; /* middle pattern */
14977 char_u
*epat
; /* end pattern */
14978 int dir
; /* BACKWARD or FORWARD */
14979 char_u
*skip
; /* skip expression */
14980 int flags
; /* SP_SETPCMARK and other SP_ values */
14982 linenr_T lnum_stop
; /* stop at this line if not zero */
14983 long time_limit
; /* stop after this many msec */
14986 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
14997 int options
= SEARCH_KEEP
;
15000 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15002 p_cpo
= (char_u
*)"";
15004 #ifdef FEAT_RELTIME
15005 /* Set the time limit, if there is one. */
15006 profile_setlimit(time_limit
, &tm
);
15009 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15010 * start/middle/end (pat3, for the top pair). */
15011 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
15012 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
15013 if (pat2
== NULL
|| pat3
== NULL
)
15015 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
15017 STRCPY(pat3
, pat2
);
15019 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15021 if (flags
& SP_START
)
15022 options
|= SEARCH_START
;
15024 save_cursor
= curwin
->w_cursor
;
15025 pos
= curwin
->w_cursor
;
15026 clearpos(&firstpos
);
15027 clearpos(&foundpos
);
15031 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15032 options
, RE_SEARCH
, lnum_stop
, &tm
);
15033 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
15034 /* didn't find it or found the first match again: FAIL */
15037 if (firstpos
.lnum
== 0)
15039 if (equalpos(pos
, foundpos
))
15041 /* Found the same position again. Can happen with a pattern that
15042 * has "\zs" at the end and searching backwards. Advance one
15043 * character and try again. */
15044 if (dir
== BACKWARD
)
15051 /* clear the start flag to avoid getting stuck here */
15052 options
&= ~SEARCH_START
;
15054 /* If the skip pattern matches, ignore this match. */
15057 save_pos
= curwin
->w_cursor
;
15058 curwin
->w_cursor
= pos
;
15059 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
15060 curwin
->w_cursor
= save_pos
;
15063 /* Evaluating {skip} caused an error, break here. */
15064 curwin
->w_cursor
= save_cursor
;
15072 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
15074 /* Found end when searching backwards or start when searching
15075 * forward: nested pair. */
15077 pat
= pat2
; /* nested, don't search for middle */
15081 /* Found end when searching forward or start when searching
15082 * backward: end of (nested) pair; or found middle in outer pair. */
15084 pat
= pat3
; /* outer level, search for middle */
15089 /* Found the match: return matchcount or line number. */
15090 if (flags
& SP_RETCOUNT
)
15094 if (flags
& SP_SETPCMARK
)
15096 curwin
->w_cursor
= pos
;
15097 if (!(flags
& SP_REPEAT
))
15099 nest
= 1; /* search for next unmatched */
15103 if (match_pos
!= NULL
)
15105 /* Store the match cursor position */
15106 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
15107 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
15110 /* If 'n' flag is used or search failed: restore cursor position. */
15111 if ((flags
& SP_NOMOVE
) || retval
== 0)
15112 curwin
->w_cursor
= save_cursor
;
15123 * "searchpos()" function
15126 f_searchpos(argvars
, rettv
)
15136 rettv
->vval
.v_number
= 0;
15138 if (rettv_list_alloc(rettv
) == FAIL
)
15141 n
= search_cmn(argvars
, &match_pos
, &flags
);
15144 lnum
= match_pos
.lnum
;
15145 col
= match_pos
.col
;
15148 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15149 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15150 if (flags
& SP_SUBPAT
)
15151 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
15157 f_server2client(argvars
, rettv
)
15161 #ifdef FEAT_CLIENTSERVER
15162 char_u buf
[NUMBUFLEN
];
15163 char_u
*server
= get_tv_string_chk(&argvars
[0]);
15164 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
15166 rettv
->vval
.v_number
= -1;
15167 if (server
== NULL
|| reply
== NULL
)
15169 if (check_restricted() || check_secure())
15172 if (check_connection() == FAIL
)
15176 if (serverSendReply(server
, reply
) < 0)
15178 EMSG(_("E258: Unable to send to client"));
15181 rettv
->vval
.v_number
= 0;
15183 rettv
->vval
.v_number
= -1;
15189 f_serverlist(argvars
, rettv
)
15195 #ifdef FEAT_CLIENTSERVER
15196 # if defined(WIN32) || defined(MAC_CLIENTSERVER)
15197 r
= serverGetVimNames();
15198 # elif defined(FEAT_X11)
15200 if (X_DISPLAY
!= NULL
)
15201 r
= serverGetVimNames(X_DISPLAY
);
15204 rettv
->v_type
= VAR_STRING
;
15205 rettv
->vval
.v_string
= r
;
15209 * "setbufvar()" function
15213 f_setbufvar(argvars
, rettv
)
15219 char_u
*varname
, *bufvarname
;
15221 char_u nbuf
[NUMBUFLEN
];
15223 rettv
->vval
.v_number
= 0;
15225 if (check_restricted() || check_secure())
15227 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
15228 varname
= get_tv_string_chk(&argvars
[1]);
15229 buf
= get_buf_tv(&argvars
[0]);
15230 varp
= &argvars
[2];
15232 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15234 /* set curbuf to be our buf, temporarily */
15235 aucmd_prepbuf(&aco
, buf
);
15237 if (*varname
== '&')
15244 numval
= get_tv_number_chk(varp
, &error
);
15245 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15246 if (!error
&& strval
!= NULL
)
15247 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15251 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15252 if (bufvarname
!= NULL
)
15254 STRCPY(bufvarname
, "b:");
15255 STRCPY(bufvarname
+ 2, varname
);
15256 set_var(bufvarname
, varp
, TRUE
);
15257 vim_free(bufvarname
);
15261 /* reset notion of buffer */
15262 aucmd_restbuf(&aco
);
15267 * "setcmdpos()" function
15270 f_setcmdpos(argvars
, rettv
)
15274 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
15277 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
15281 * "setline()" function
15284 f_setline(argvars
, rettv
)
15289 char_u
*line
= NULL
;
15291 listitem_T
*li
= NULL
;
15293 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
15295 lnum
= get_tv_lnum(&argvars
[0]);
15296 if (argvars
[1].v_type
== VAR_LIST
)
15298 l
= argvars
[1].vval
.v_list
;
15302 line
= get_tv_string_chk(&argvars
[1]);
15304 rettv
->vval
.v_number
= 0; /* OK */
15309 /* list argument, get next string */
15312 line
= get_tv_string_chk(&li
->li_tv
);
15316 rettv
->vval
.v_number
= 1; /* FAIL */
15317 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
15319 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
15321 /* existing line, replace it */
15322 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
15324 changed_bytes(lnum
, 0);
15325 if (lnum
== curwin
->w_cursor
.lnum
)
15326 check_cursor_col();
15327 rettv
->vval
.v_number
= 0; /* OK */
15330 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
15332 /* lnum is one past the last line, append the line */
15334 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
15335 rettv
->vval
.v_number
= 0; /* OK */
15338 if (l
== NULL
) /* only one string argument */
15344 appended_lines_mark(lcount
, added
);
15347 static void set_qf_ll_list
__ARGS((win_T
*wp
, typval_T
*list_arg
, typval_T
*action_arg
, typval_T
*rettv
));
15350 * Used by "setqflist()" and "setloclist()" functions
15354 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
15356 typval_T
*list_arg
;
15357 typval_T
*action_arg
;
15360 #ifdef FEAT_QUICKFIX
15365 rettv
->vval
.v_number
= -1;
15367 #ifdef FEAT_QUICKFIX
15368 if (list_arg
->v_type
!= VAR_LIST
)
15369 EMSG(_(e_listreq
));
15372 list_T
*l
= list_arg
->vval
.v_list
;
15374 if (action_arg
->v_type
== VAR_STRING
)
15376 act
= get_tv_string_chk(action_arg
);
15378 return; /* type error; errmsg already given */
15379 if (*act
== 'a' || *act
== 'r')
15383 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
15384 rettv
->vval
.v_number
= 0;
15390 * "setloclist()" function
15394 f_setloclist(argvars
, rettv
)
15400 rettv
->vval
.v_number
= -1;
15402 win
= find_win_by_nr(&argvars
[0], NULL
);
15404 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
15408 * "setmatches()" function
15411 f_setmatches(argvars
, rettv
)
15415 #ifdef FEAT_SEARCH_EXTRA
15420 rettv
->vval
.v_number
= -1;
15421 if (argvars
[0].v_type
!= VAR_LIST
)
15423 EMSG(_(e_listreq
));
15426 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
15429 /* To some extent make sure that we are dealing with a list from
15430 * "getmatches()". */
15434 if (li
->li_tv
.v_type
!= VAR_DICT
15435 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
15440 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
15441 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
15442 && dict_find(d
, (char_u
*)"priority", -1) != NULL
15443 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
15451 clear_matches(curwin
);
15455 d
= li
->li_tv
.vval
.v_dict
;
15456 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
15457 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
15458 (int)get_dict_number(d
, (char_u
*)"priority"),
15459 (int)get_dict_number(d
, (char_u
*)"id"));
15462 rettv
->vval
.v_number
= 0;
15468 * "setpos()" function
15472 f_setpos(argvars
, rettv
)
15480 rettv
->vval
.v_number
= -1;
15481 name
= get_tv_string_chk(argvars
);
15484 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
15487 if (name
[0] == '.' && name
[1] == NUL
)
15490 if (fnum
== curbuf
->b_fnum
)
15492 curwin
->w_cursor
= pos
;
15494 rettv
->vval
.v_number
= 0;
15499 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
15502 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
15503 rettv
->vval
.v_number
= 0;
15512 * "setqflist()" function
15516 f_setqflist(argvars
, rettv
)
15520 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
15524 * "setreg()" function
15527 f_setreg(argvars
, rettv
)
15532 char_u
*strregname
;
15543 strregname
= get_tv_string_chk(argvars
);
15544 rettv
->vval
.v_number
= 1; /* FAIL is default */
15546 if (strregname
== NULL
)
15547 return; /* type error; errmsg already given */
15548 regname
= *strregname
;
15549 if (regname
== 0 || regname
== '@')
15551 else if (regname
== '=')
15554 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15556 stropt
= get_tv_string_chk(&argvars
[2]);
15557 if (stropt
== NULL
)
15558 return; /* type error */
15559 for (; *stropt
!= NUL
; ++stropt
)
15562 case 'a': case 'A': /* append */
15565 case 'v': case 'c': /* character-wise selection */
15568 case 'V': case 'l': /* line-wise selection */
15572 case 'b': case Ctrl_V
: /* block-wise selection */
15573 yank_type
= MBLOCK
;
15574 if (VIM_ISDIGIT(stropt
[1]))
15577 block_len
= getdigits(&stropt
) - 1;
15585 strval
= get_tv_string_chk(&argvars
[1]);
15586 if (strval
!= NULL
)
15587 write_reg_contents_ex(regname
, strval
, -1,
15588 append
, yank_type
, block_len
);
15589 rettv
->vval
.v_number
= 0;
15593 * "settabwinvar()" function
15596 f_settabwinvar(argvars
, rettv
)
15600 setwinvar(argvars
, rettv
, 1);
15604 * "setwinvar()" function
15607 f_setwinvar(argvars
, rettv
)
15611 setwinvar(argvars
, rettv
, 0);
15615 * "setwinvar()" and "settabwinvar()" functions
15618 setwinvar(argvars
, rettv
, off
)
15624 #ifdef FEAT_WINDOWS
15625 win_T
*save_curwin
;
15626 tabpage_T
*save_curtab
;
15628 char_u
*varname
, *winvarname
;
15630 char_u nbuf
[NUMBUFLEN
];
15633 rettv
->vval
.v_number
= 0;
15635 if (check_restricted() || check_secure())
15638 #ifdef FEAT_WINDOWS
15640 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
15644 win
= find_win_by_nr(&argvars
[off
], tp
);
15645 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
15646 varp
= &argvars
[off
+ 2];
15648 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15650 #ifdef FEAT_WINDOWS
15651 /* set curwin to be our win, temporarily */
15652 save_curwin
= curwin
;
15653 save_curtab
= curtab
;
15654 goto_tabpage_tp(tp
);
15655 if (!win_valid(win
))
15658 curbuf
= curwin
->w_buffer
;
15661 if (*varname
== '&')
15668 numval
= get_tv_number_chk(varp
, &error
);
15669 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15670 if (!error
&& strval
!= NULL
)
15671 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15675 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15676 if (winvarname
!= NULL
)
15678 STRCPY(winvarname
, "w:");
15679 STRCPY(winvarname
+ 2, varname
);
15680 set_var(winvarname
, varp
, TRUE
);
15681 vim_free(winvarname
);
15685 #ifdef FEAT_WINDOWS
15686 /* Restore current tabpage and window, if still valid (autocomands can
15687 * make them invalid). */
15688 if (valid_tabpage(save_curtab
))
15689 goto_tabpage_tp(save_curtab
);
15690 if (win_valid(save_curwin
))
15692 curwin
= save_curwin
;
15693 curbuf
= curwin
->w_buffer
;
15700 * "shellescape({string})" function
15703 f_shellescape(argvars
, rettv
)
15707 rettv
->vval
.v_string
= vim_strsave_shellescape(get_tv_string(&argvars
[0]));
15708 rettv
->v_type
= VAR_STRING
;
15712 * "simplify()" function
15715 f_simplify(argvars
, rettv
)
15721 p
= get_tv_string(&argvars
[0]);
15722 rettv
->vval
.v_string
= vim_strsave(p
);
15723 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
15724 rettv
->v_type
= VAR_STRING
;
15732 f_sin(argvars
, rettv
)
15738 rettv
->v_type
= VAR_FLOAT
;
15739 if (get_float_arg(argvars
, &f
) == OK
)
15740 rettv
->vval
.v_float
= sin(f
);
15742 rettv
->vval
.v_float
= 0.0;
15747 #ifdef __BORLANDC__
15750 item_compare
__ARGS((const void *s1
, const void *s2
));
15752 #ifdef __BORLANDC__
15755 item_compare2
__ARGS((const void *s1
, const void *s2
));
15757 static int item_compare_ic
;
15758 static char_u
*item_compare_func
;
15759 static int item_compare_func_err
;
15760 #define ITEM_COMPARE_FAIL 999
15763 * Compare functions for f_sort() below.
15766 #ifdef __BORLANDC__
15769 item_compare(s1
, s2
)
15774 char_u
*tofree1
, *tofree2
;
15776 char_u numbuf1
[NUMBUFLEN
];
15777 char_u numbuf2
[NUMBUFLEN
];
15779 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
15780 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
15785 if (item_compare_ic
)
15786 res
= STRICMP(p1
, p2
);
15788 res
= STRCMP(p1
, p2
);
15795 #ifdef __BORLANDC__
15798 item_compare2(s1
, s2
)
15807 /* shortcut after failure in previous call; compare all items equal */
15808 if (item_compare_func_err
)
15811 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15812 * in the copy without changing the original list items. */
15813 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
15814 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
15816 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
15817 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
15818 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
15819 clear_tv(&argv
[0]);
15820 clear_tv(&argv
[1]);
15823 res
= ITEM_COMPARE_FAIL
;
15825 /* return value has wrong type */
15826 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
15827 if (item_compare_func_err
)
15828 res
= ITEM_COMPARE_FAIL
;
15834 * "sort({list})" function
15837 f_sort(argvars
, rettv
)
15847 rettv
->vval
.v_number
= 0;
15848 if (argvars
[0].v_type
!= VAR_LIST
)
15849 EMSG2(_(e_listarg
), "sort()");
15852 l
= argvars
[0].vval
.v_list
;
15853 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
15855 rettv
->vval
.v_list
= l
;
15856 rettv
->v_type
= VAR_LIST
;
15861 return; /* short list sorts pretty quickly */
15863 item_compare_ic
= FALSE
;
15864 item_compare_func
= NULL
;
15865 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15867 if (argvars
[1].v_type
== VAR_FUNC
)
15868 item_compare_func
= argvars
[1].vval
.v_string
;
15873 i
= get_tv_number_chk(&argvars
[1], &error
);
15875 return; /* type error; errmsg already given */
15877 item_compare_ic
= TRUE
;
15879 item_compare_func
= get_tv_string(&argvars
[1]);
15883 /* Make an array with each entry pointing to an item in the List. */
15884 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
15888 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
15891 item_compare_func_err
= FALSE
;
15892 /* test the compare function */
15893 if (item_compare_func
!= NULL
15894 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
15895 == ITEM_COMPARE_FAIL
)
15896 EMSG(_("E702: Sort compare function failed"));
15899 /* Sort the array with item pointers. */
15900 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
15901 item_compare_func
== NULL
? item_compare
: item_compare2
);
15903 if (!item_compare_func_err
)
15905 /* Clear the List and append the items in the sorted order. */
15906 l
->lv_first
= l
->lv_last
= l
->lv_idx_item
= NULL
;
15908 for (i
= 0; i
< len
; ++i
)
15909 list_append(l
, ptrs
[i
]);
15918 * "soundfold({word})" function
15921 f_soundfold(argvars
, rettv
)
15927 rettv
->v_type
= VAR_STRING
;
15928 s
= get_tv_string(&argvars
[0]);
15930 rettv
->vval
.v_string
= eval_soundfold(s
);
15932 rettv
->vval
.v_string
= vim_strsave(s
);
15937 * "spellbadword()" function
15941 f_spellbadword(argvars
, rettv
)
15945 char_u
*word
= (char_u
*)"";
15946 hlf_T attr
= HLF_COUNT
;
15949 if (rettv_list_alloc(rettv
) == FAIL
)
15953 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15955 /* Find the start and length of the badly spelled word. */
15956 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
15958 word
= ml_get_cursor();
15960 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15962 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15967 /* Check the argument for spelling. */
15968 while (*str
!= NUL
)
15970 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
15971 if (attr
!= HLF_COUNT
)
15982 list_append_string(rettv
->vval
.v_list
, word
, len
);
15983 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
15984 attr
== HLF_SPB
? "bad" :
15985 attr
== HLF_SPR
? "rare" :
15986 attr
== HLF_SPL
? "local" :
15987 attr
== HLF_SPC
? "caps" :
15992 * "spellsuggest()" function
15996 f_spellsuggest(argvars
, rettv
)
16002 int typeerr
= FALSE
;
16007 int need_capital
= FALSE
;
16010 if (rettv_list_alloc(rettv
) == FAIL
)
16014 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16016 str
= get_tv_string(&argvars
[0]);
16017 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16019 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
16022 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16024 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
16032 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
16034 for (i
= 0; i
< ga
.ga_len
; ++i
)
16036 str
= ((char_u
**)ga
.ga_data
)[i
];
16038 li
= listitem_alloc();
16043 li
->li_tv
.v_type
= VAR_STRING
;
16044 li
->li_tv
.v_lock
= 0;
16045 li
->li_tv
.vval
.v_string
= str
;
16046 list_append(rettv
->vval
.v_list
, li
);
16055 f_split(argvars
, rettv
)
16061 char_u
*pat
= NULL
;
16062 regmatch_T regmatch
;
16063 char_u patbuf
[NUMBUFLEN
];
16067 int keepempty
= FALSE
;
16068 int typeerr
= FALSE
;
16070 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16072 p_cpo
= (char_u
*)"";
16074 str
= get_tv_string(&argvars
[0]);
16075 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16077 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16080 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16081 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
16083 if (pat
== NULL
|| *pat
== NUL
)
16084 pat
= (char_u
*)"[\\x01- ]\\+";
16086 if (rettv_list_alloc(rettv
) == FAIL
)
16091 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
16092 if (regmatch
.regprog
!= NULL
)
16094 regmatch
.rm_ic
= FALSE
;
16095 while (*str
!= NUL
|| keepempty
)
16098 match
= FALSE
; /* empty item at the end */
16100 match
= vim_regexec_nl(®match
, str
, col
);
16102 end
= regmatch
.startp
[0];
16104 end
= str
+ STRLEN(str
);
16105 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
16106 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
16108 if (list_append_string(rettv
->vval
.v_list
, str
,
16109 (int)(end
- str
)) == FAIL
)
16114 /* Advance to just after the match. */
16115 if (regmatch
.endp
[0] > str
)
16119 /* Don't get stuck at the same match. */
16121 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
16126 str
= regmatch
.endp
[0];
16129 vim_free(regmatch
.regprog
);
16137 * "sqrt()" function
16140 f_sqrt(argvars
, rettv
)
16146 rettv
->v_type
= VAR_FLOAT
;
16147 if (get_float_arg(argvars
, &f
) == OK
)
16148 rettv
->vval
.v_float
= sqrt(f
);
16150 rettv
->vval
.v_float
= 0.0;
16154 * "str2float()" function
16157 f_str2float(argvars
, rettv
)
16161 char_u
*p
= skipwhite(get_tv_string(&argvars
[0]));
16164 p
= skipwhite(p
+ 1);
16165 (void)string2float(p
, &rettv
->vval
.v_float
);
16166 rettv
->v_type
= VAR_FLOAT
;
16171 * "str2nr()" function
16174 f_str2nr(argvars
, rettv
)
16182 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16184 base
= get_tv_number(&argvars
[1]);
16185 if (base
!= 8 && base
!= 10 && base
!= 16)
16192 p
= skipwhite(get_tv_string(&argvars
[0]));
16194 p
= skipwhite(p
+ 1);
16195 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
16196 rettv
->vval
.v_number
= n
;
16199 #ifdef HAVE_STRFTIME
16201 * "strftime({format}[, {time}])" function
16204 f_strftime(argvars
, rettv
)
16208 char_u result_buf
[256];
16209 struct tm
*curtime
;
16213 rettv
->v_type
= VAR_STRING
;
16215 p
= get_tv_string(&argvars
[0]);
16216 if (argvars
[1].v_type
== VAR_UNKNOWN
)
16217 seconds
= time(NULL
);
16219 seconds
= (time_t)get_tv_number(&argvars
[1]);
16220 curtime
= localtime(&seconds
);
16221 /* MSVC returns NULL for an invalid value of seconds. */
16222 if (curtime
== NULL
)
16223 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
16230 conv
.vc_type
= CONV_NONE
;
16231 enc
= enc_locale();
16232 convert_setup(&conv
, p_enc
, enc
);
16233 if (conv
.vc_type
!= CONV_NONE
)
16234 p
= string_convert(&conv
, p
, NULL
);
16237 (void)strftime((char *)result_buf
, sizeof(result_buf
),
16238 (char *)p
, curtime
);
16240 result_buf
[0] = NUL
;
16243 if (conv
.vc_type
!= CONV_NONE
)
16245 convert_setup(&conv
, enc
, p_enc
);
16246 if (conv
.vc_type
!= CONV_NONE
)
16247 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
16250 rettv
->vval
.v_string
= vim_strsave(result_buf
);
16253 /* Release conversion descriptors */
16254 convert_setup(&conv
, NULL
, NULL
);
16262 * "stridx()" function
16265 f_stridx(argvars
, rettv
)
16269 char_u buf
[NUMBUFLEN
];
16272 char_u
*save_haystack
;
16276 needle
= get_tv_string_chk(&argvars
[1]);
16277 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16278 rettv
->vval
.v_number
= -1;
16279 if (needle
== NULL
|| haystack
== NULL
)
16280 return; /* type error; errmsg already given */
16282 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16286 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
16287 if (error
|| start_idx
>= (int)STRLEN(haystack
))
16289 if (start_idx
>= 0)
16290 haystack
+= start_idx
;
16293 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
16295 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
16299 * "string()" function
16302 f_string(argvars
, rettv
)
16307 char_u numbuf
[NUMBUFLEN
];
16309 rettv
->v_type
= VAR_STRING
;
16310 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
16311 /* Make a copy if we have a value but it's not in allocated memory. */
16312 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
16313 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
16317 * "strlen()" function
16320 f_strlen(argvars
, rettv
)
16324 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
16325 get_tv_string(&argvars
[0])));
16329 * "strpart()" function
16332 f_strpart(argvars
, rettv
)
16342 p
= get_tv_string(&argvars
[0]);
16343 slen
= (int)STRLEN(p
);
16345 n
= get_tv_number_chk(&argvars
[1], &error
);
16348 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16349 len
= get_tv_number(&argvars
[2]);
16351 len
= slen
- n
; /* default len: all bytes that are available. */
16354 * Only return the overlap between the specified part and the actual
16366 else if (n
+ len
> slen
)
16369 rettv
->v_type
= VAR_STRING
;
16370 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
16374 * "strridx()" function
16377 f_strridx(argvars
, rettv
)
16381 char_u buf
[NUMBUFLEN
];
16385 char_u
*lastmatch
= NULL
;
16386 int haystack_len
, end_idx
;
16388 needle
= get_tv_string_chk(&argvars
[1]);
16389 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16391 rettv
->vval
.v_number
= -1;
16392 if (needle
== NULL
|| haystack
== NULL
)
16393 return; /* type error; errmsg already given */
16395 haystack_len
= (int)STRLEN(haystack
);
16396 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16398 /* Third argument: upper limit for index */
16399 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
16401 return; /* can never find a match */
16404 end_idx
= haystack_len
;
16406 if (*needle
== NUL
)
16408 /* Empty string matches past the end. */
16409 lastmatch
= haystack
+ end_idx
;
16413 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
16415 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
16416 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
16422 if (lastmatch
== NULL
)
16423 rettv
->vval
.v_number
= -1;
16425 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
16429 * "strtrans()" function
16432 f_strtrans(argvars
, rettv
)
16436 rettv
->v_type
= VAR_STRING
;
16437 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
16441 * "submatch()" function
16444 f_submatch(argvars
, rettv
)
16448 rettv
->v_type
= VAR_STRING
;
16449 rettv
->vval
.v_string
=
16450 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
16454 * "substitute()" function
16457 f_substitute(argvars
, rettv
)
16461 char_u patbuf
[NUMBUFLEN
];
16462 char_u subbuf
[NUMBUFLEN
];
16463 char_u flagsbuf
[NUMBUFLEN
];
16465 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16466 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16467 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
16468 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
16470 rettv
->v_type
= VAR_STRING
;
16471 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
16472 rettv
->vval
.v_string
= NULL
;
16474 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
16478 * "synID(lnum, col, trans)" function
16482 f_synID(argvars
, rettv
)
16491 int transerr
= FALSE
;
16493 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16494 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16495 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
16497 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16498 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
16499 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
16502 rettv
->vval
.v_number
= id
;
16506 * "synIDattr(id, what [, mode])" function
16510 f_synIDattr(argvars
, rettv
)
16519 char_u modebuf
[NUMBUFLEN
];
16522 id
= get_tv_number(&argvars
[0]);
16523 what
= get_tv_string(&argvars
[1]);
16524 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16526 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
16527 modec
= TOLOWER_ASC(mode
[0]);
16528 if (modec
!= 't' && modec
!= 'c'
16533 modec
= 0; /* replace invalid with current */
16549 switch (TOLOWER_ASC(what
[0]))
16552 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
16553 p
= highlight_color(id
, what
, modec
);
16555 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
16558 case 'f': /* fg[#] */
16559 p
= highlight_color(id
, what
, modec
);
16563 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
16564 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16566 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
16569 case 'n': /* name */
16570 p
= get_highlight_name(NULL
, id
- 1);
16573 case 'r': /* reverse */
16574 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16577 case 's': /* standout */
16578 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
16582 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
16584 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
16587 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
16592 p
= vim_strsave(p
);
16594 rettv
->v_type
= VAR_STRING
;
16595 rettv
->vval
.v_string
= p
;
16599 * "synIDtrans(id)" function
16603 f_synIDtrans(argvars
, rettv
)
16610 id
= get_tv_number(&argvars
[0]);
16613 id
= syn_get_final_id(id
);
16618 rettv
->vval
.v_number
= id
;
16622 * "synstack(lnum, col)" function
16626 f_synstack(argvars
, rettv
)
16637 rettv
->v_type
= VAR_LIST
;
16638 rettv
->vval
.v_list
= NULL
;
16641 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16642 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16644 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16645 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
))
16646 && rettv_list_alloc(rettv
) != FAIL
)
16648 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
16651 id
= syn_get_stack_item(i
);
16654 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
16662 * "system()" function
16665 f_system(argvars
, rettv
)
16669 char_u
*res
= NULL
;
16671 char_u
*infile
= NULL
;
16672 char_u buf
[NUMBUFLEN
];
16676 if (check_restricted() || check_secure())
16679 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16682 * Write the string to a temp file, to be used for input of the shell
16685 if ((infile
= vim_tempname('i')) == NULL
)
16691 fd
= mch_fopen((char *)infile
, WRITEBIN
);
16694 EMSG2(_(e_notopen
), infile
);
16697 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
16701 goto done
; /* type error; errmsg already given */
16703 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
16705 if (fclose(fd
) != 0)
16709 EMSG(_("E677: Error writing temp file"));
16714 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
16715 SHELL_SILENT
| SHELL_COOKED
);
16718 /* translate <CR> into <NL> */
16723 for (s
= res
; *s
; ++s
)
16731 /* translate <CR><NL> into <NL> */
16737 for (s
= res
; *s
; ++s
)
16739 if (s
[0] == CAR
&& s
[1] == NL
)
16749 if (infile
!= NULL
)
16751 mch_remove(infile
);
16754 rettv
->v_type
= VAR_STRING
;
16755 rettv
->vval
.v_string
= res
;
16759 * "tabpagebuflist()" function
16763 f_tabpagebuflist(argvars
, rettv
)
16767 #ifndef FEAT_WINDOWS
16768 rettv
->vval
.v_number
= 0;
16773 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16777 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16779 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16782 rettv
->vval
.v_number
= 0;
16785 if (rettv_list_alloc(rettv
) == FAIL
)
16786 rettv
->vval
.v_number
= 0;
16789 for (; wp
!= NULL
; wp
= wp
->w_next
)
16790 if (list_append_number(rettv
->vval
.v_list
,
16791 wp
->w_buffer
->b_fnum
) == FAIL
)
16800 * "tabpagenr()" function
16804 f_tabpagenr(argvars
, rettv
)
16809 #ifdef FEAT_WINDOWS
16812 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
16814 arg
= get_tv_string_chk(&argvars
[0]);
16818 if (STRCMP(arg
, "$") == 0)
16819 nr
= tabpage_index(NULL
) - 1;
16821 EMSG2(_(e_invexpr2
), arg
);
16825 nr
= tabpage_index(curtab
);
16827 rettv
->vval
.v_number
= nr
;
16831 #ifdef FEAT_WINDOWS
16832 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
16835 * Common code for tabpagewinnr() and winnr().
16838 get_winnr(tp
, argvar
)
16847 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
16848 if (argvar
->v_type
!= VAR_UNKNOWN
)
16850 arg
= get_tv_string_chk(argvar
);
16852 nr
= 0; /* type error; errmsg already given */
16853 else if (STRCMP(arg
, "$") == 0)
16854 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
16855 else if (STRCMP(arg
, "#") == 0)
16857 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
16863 EMSG2(_(e_invexpr2
), arg
);
16869 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16870 wp
!= twin
; wp
= wp
->w_next
)
16874 /* didn't find it in this tabpage */
16885 * "tabpagewinnr()" function
16889 f_tabpagewinnr(argvars
, rettv
)
16894 #ifdef FEAT_WINDOWS
16897 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16901 nr
= get_winnr(tp
, &argvars
[1]);
16903 rettv
->vval
.v_number
= nr
;
16908 * "tagfiles()" function
16912 f_tagfiles(argvars
, rettv
)
16916 char_u fname
[MAXPATHL
+ 1];
16920 if (rettv_list_alloc(rettv
) == FAIL
)
16922 rettv
->vval
.v_number
= 0;
16926 for (first
= TRUE
; ; first
= FALSE
)
16927 if (get_tagfname(&tn
, first
, fname
) == FAIL
16928 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
16934 * "taglist()" function
16937 f_taglist(argvars
, rettv
)
16941 char_u
*tag_pattern
;
16943 tag_pattern
= get_tv_string(&argvars
[0]);
16945 rettv
->vval
.v_number
= FALSE
;
16946 if (*tag_pattern
== NUL
)
16949 if (rettv_list_alloc(rettv
) == OK
)
16950 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
16954 * "tempname()" function
16958 f_tempname(argvars
, rettv
)
16962 static int x
= 'A';
16964 rettv
->v_type
= VAR_STRING
;
16965 rettv
->vval
.v_string
= vim_tempname(x
);
16967 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16968 * names. Skip 'I' and 'O', they are used for shell redirection. */
16986 } while (x
== 'I' || x
== 'O');
16990 * "test(list)" function: Just checking the walls...
16994 f_test(argvars
, rettv
)
16998 /* Used for unit testing. Change the code below to your liking. */
17002 char_u
*bad
, *good
;
17004 if (argvars
[0].v_type
!= VAR_LIST
)
17006 l
= argvars
[0].vval
.v_list
;
17012 bad
= get_tv_string(&li
->li_tv
);
17016 good
= get_tv_string(&li
->li_tv
);
17017 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
17022 * "tolower(string)" function
17025 f_tolower(argvars
, rettv
)
17031 p
= vim_strsave(get_tv_string(&argvars
[0]));
17032 rettv
->v_type
= VAR_STRING
;
17033 rettv
->vval
.v_string
= p
;
17045 c
= utf_ptr2char(p
);
17046 lc
= utf_tolower(c
);
17047 l
= utf_ptr2len(p
);
17048 /* TODO: reallocate string when byte count changes. */
17049 if (utf_char2len(lc
) == l
)
17050 utf_char2bytes(lc
, p
);
17053 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
17054 p
+= l
; /* skip multi-byte character */
17058 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
17065 * "toupper(string)" function
17068 f_toupper(argvars
, rettv
)
17072 rettv
->v_type
= VAR_STRING
;
17073 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
17077 * "tr(string, fromstr, tostr)" function
17080 f_tr(argvars
, rettv
)
17097 char_u buf
[NUMBUFLEN
];
17098 char_u buf2
[NUMBUFLEN
];
17101 instr
= get_tv_string(&argvars
[0]);
17102 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
17103 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
17105 /* Default return value: empty string. */
17106 rettv
->v_type
= VAR_STRING
;
17107 rettv
->vval
.v_string
= NULL
;
17108 if (fromstr
== NULL
|| tostr
== NULL
)
17109 return; /* type error; errmsg already given */
17110 ga_init2(&ga
, (int)sizeof(char), 80);
17115 /* not multi-byte: fromstr and tostr must be the same length */
17116 if (STRLEN(fromstr
) != STRLEN(tostr
))
17121 EMSG2(_(e_invarg2
), fromstr
);
17126 /* fromstr and tostr have to contain the same number of chars */
17127 while (*instr
!= NUL
)
17132 inlen
= (*mb_ptr2len
)(instr
);
17136 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
17138 fromlen
= (*mb_ptr2len
)(p
);
17139 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
17141 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17143 tolen
= (*mb_ptr2len
)(p
);
17151 if (*p
== NUL
) /* tostr is shorter than fromstr */
17158 if (first
&& cpstr
== instr
)
17160 /* Check that fromstr and tostr have the same number of
17161 * (multi-byte) characters. Done only once when a character
17162 * of instr doesn't appear in fromstr. */
17164 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17166 tolen
= (*mb_ptr2len
)(p
);
17173 ga_grow(&ga
, cplen
);
17174 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
17175 ga
.ga_len
+= cplen
;
17182 /* When not using multi-byte chars we can do it faster. */
17183 p
= vim_strchr(fromstr
, *instr
);
17185 ga_append(&ga
, tostr
[p
- fromstr
]);
17187 ga_append(&ga
, *instr
);
17192 /* add a terminating NUL */
17194 ga_append(&ga
, NUL
);
17196 rettv
->vval
.v_string
= ga
.ga_data
;
17201 * "trunc({float})" function
17204 f_trunc(argvars
, rettv
)
17210 rettv
->v_type
= VAR_FLOAT
;
17211 if (get_float_arg(argvars
, &f
) == OK
)
17212 /* trunc() is not in C90, use floor() or ceil() instead. */
17213 rettv
->vval
.v_float
= f
> 0 ? floor(f
) : ceil(f
);
17215 rettv
->vval
.v_float
= 0.0;
17220 * "type(expr)" function
17223 f_type(argvars
, rettv
)
17229 switch (argvars
[0].v_type
)
17231 case VAR_NUMBER
: n
= 0; break;
17232 case VAR_STRING
: n
= 1; break;
17233 case VAR_FUNC
: n
= 2; break;
17234 case VAR_LIST
: n
= 3; break;
17235 case VAR_DICT
: n
= 4; break;
17237 case VAR_FLOAT
: n
= 5; break;
17239 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
17241 rettv
->vval
.v_number
= n
;
17245 * "values(dict)" function
17248 f_values(argvars
, rettv
)
17252 dict_list(argvars
, rettv
, 1);
17256 * "virtcol(string)" function
17259 f_virtcol(argvars
, rettv
)
17265 int fnum
= curbuf
->b_fnum
;
17267 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
17268 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
17269 && fnum
== curbuf
->b_fnum
)
17271 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
17275 rettv
->vval
.v_number
= vcol
;
17279 * "visualmode()" function
17283 f_visualmode(argvars
, rettv
)
17290 rettv
->v_type
= VAR_STRING
;
17291 str
[0] = curbuf
->b_visual_mode_eval
;
17293 rettv
->vval
.v_string
= vim_strsave(str
);
17295 /* A non-zero number or non-empty string argument: reset mode. */
17296 if ((argvars
[0].v_type
== VAR_NUMBER
&& argvars
[0].vval
.v_number
!= 0)
17297 || (argvars
[0].v_type
== VAR_STRING
17298 && *get_tv_string(&argvars
[0]) != NUL
))
17299 curbuf
->b_visual_mode_eval
= NUL
;
17301 rettv
->vval
.v_number
= 0; /* return anything, it won't work anyway */
17306 * "winbufnr(nr)" function
17309 f_winbufnr(argvars
, rettv
)
17315 wp
= find_win_by_nr(&argvars
[0], NULL
);
17317 rettv
->vval
.v_number
= -1;
17319 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
17323 * "wincol()" function
17327 f_wincol(argvars
, rettv
)
17332 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
17336 * "winheight(nr)" function
17339 f_winheight(argvars
, rettv
)
17345 wp
= find_win_by_nr(&argvars
[0], NULL
);
17347 rettv
->vval
.v_number
= -1;
17349 rettv
->vval
.v_number
= wp
->w_height
;
17353 * "winline()" function
17357 f_winline(argvars
, rettv
)
17362 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
17366 * "winnr()" function
17370 f_winnr(argvars
, rettv
)
17376 #ifdef FEAT_WINDOWS
17377 nr
= get_winnr(curtab
, &argvars
[0]);
17379 rettv
->vval
.v_number
= nr
;
17383 * "winrestcmd()" function
17387 f_winrestcmd(argvars
, rettv
)
17391 #ifdef FEAT_WINDOWS
17397 ga_init2(&ga
, (int)sizeof(char), 70);
17398 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
17400 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
17401 ga_concat(&ga
, buf
);
17402 # ifdef FEAT_VERTSPLIT
17403 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
17404 ga_concat(&ga
, buf
);
17408 ga_append(&ga
, NUL
);
17410 rettv
->vval
.v_string
= ga
.ga_data
;
17412 rettv
->vval
.v_string
= NULL
;
17414 rettv
->v_type
= VAR_STRING
;
17418 * "winrestview()" function
17422 f_winrestview(argvars
, rettv
)
17428 if (argvars
[0].v_type
!= VAR_DICT
17429 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
17433 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
17434 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
17435 #ifdef FEAT_VIRTUALEDIT
17436 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
17438 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
17439 curwin
->w_set_curswant
= FALSE
;
17441 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
17443 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
17445 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
17446 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
17449 changed_cline_bef_curs();
17450 invalidate_botline();
17451 redraw_later(VALID
);
17453 if (curwin
->w_topline
== 0)
17454 curwin
->w_topline
= 1;
17455 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
17456 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
17458 check_topfill(curwin
, TRUE
);
17464 * "winsaveview()" function
17468 f_winsaveview(argvars
, rettv
)
17474 dict
= dict_alloc();
17477 rettv
->v_type
= VAR_DICT
;
17478 rettv
->vval
.v_dict
= dict
;
17479 ++dict
->dv_refcount
;
17481 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
17482 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
17483 #ifdef FEAT_VIRTUALEDIT
17484 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
17487 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
17489 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
17491 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
17493 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
17494 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
17498 * "winwidth(nr)" function
17501 f_winwidth(argvars
, rettv
)
17507 wp
= find_win_by_nr(&argvars
[0], NULL
);
17509 rettv
->vval
.v_number
= -1;
17511 #ifdef FEAT_VERTSPLIT
17512 rettv
->vval
.v_number
= wp
->w_width
;
17514 rettv
->vval
.v_number
= Columns
;
17519 * "writefile()" function
17522 f_writefile(argvars
, rettv
)
17526 int binary
= FALSE
;
17534 if (check_restricted() || check_secure())
17537 if (argvars
[0].v_type
!= VAR_LIST
)
17539 EMSG2(_(e_listarg
), "writefile()");
17542 if (argvars
[0].vval
.v_list
== NULL
)
17545 if (argvars
[2].v_type
!= VAR_UNKNOWN
17546 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
17549 /* Always open the file in binary mode, library functions have a mind of
17550 * their own about CR-LF conversion. */
17551 fname
= get_tv_string(&argvars
[1]);
17552 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
17554 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
17559 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
17562 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
17574 if (!binary
|| li
->li_next
!= NULL
)
17575 if (putc('\n', fd
) == EOF
)
17589 rettv
->vval
.v_number
= ret
;
17593 * Translate a String variable into a position.
17594 * Returns NULL when there is an error.
17597 var2fpos(varp
, dollar_lnum
, fnum
)
17599 int dollar_lnum
; /* TRUE when $ is last line */
17600 int *fnum
; /* set to fnum for '0, 'A, etc. */
17606 /* Argument can be [lnum, col, coladd]. */
17607 if (varp
->v_type
== VAR_LIST
)
17614 l
= varp
->vval
.v_list
;
17618 /* Get the line number */
17619 pos
.lnum
= list_find_nr(l
, 0L, &error
);
17620 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
17621 return NULL
; /* invalid line number */
17623 /* Get the column number */
17624 pos
.col
= list_find_nr(l
, 1L, &error
);
17627 len
= (long)STRLEN(ml_get(pos
.lnum
));
17629 /* We accept "$" for the column number: last column. */
17630 li
= list_find(l
, 1L);
17631 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
17632 && li
->li_tv
.vval
.v_string
!= NULL
17633 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
17636 /* Accept a position up to the NUL after the line. */
17637 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
17638 return NULL
; /* invalid column number */
17641 #ifdef FEAT_VIRTUALEDIT
17642 /* Get the virtual offset. Defaults to zero. */
17643 pos
.coladd
= list_find_nr(l
, 2L, &error
);
17651 name
= get_tv_string_chk(varp
);
17654 if (name
[0] == '.') /* cursor */
17655 return &curwin
->w_cursor
;
17657 if (name
[0] == 'v' && name
[1] == NUL
) /* Visual start */
17661 return &curwin
->w_cursor
;
17664 if (name
[0] == '\'') /* mark */
17666 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
17667 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
17672 #ifdef FEAT_VIRTUALEDIT
17676 if (name
[0] == 'w' && dollar_lnum
)
17679 if (name
[1] == '0') /* "w0": first visible line */
17682 pos
.lnum
= curwin
->w_topline
;
17685 else if (name
[1] == '$') /* "w$": last visible line */
17687 validate_botline();
17688 pos
.lnum
= curwin
->w_botline
- 1;
17692 else if (name
[0] == '$') /* last column or line */
17696 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
17701 pos
.lnum
= curwin
->w_cursor
.lnum
;
17702 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
17710 * Convert list in "arg" into a position and optional file number.
17711 * When "fnump" is NULL there is no file number, only 3 items.
17712 * Note that the column is passed on as-is, the caller may want to decrement
17713 * it to use 1 for the first column.
17714 * Return FAIL when conversion is not possible, doesn't check the position for
17718 list2fpos(arg
, posp
, fnump
)
17723 list_T
*l
= arg
->vval
.v_list
;
17727 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17728 * when "fnump" isn't NULL and "coladd" is optional. */
17729 if (arg
->v_type
!= VAR_LIST
17731 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
17732 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
17737 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
17741 n
= curbuf
->b_fnum
; /* current buffer */
17745 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
17750 n
= list_find_nr(l
, i
++, NULL
); /* col */
17755 #ifdef FEAT_VIRTUALEDIT
17756 n
= list_find_nr(l
, i
, NULL
);
17767 * Get the length of an environment variable name.
17768 * Advance "arg" to the first character after the name.
17769 * Return 0 for error.
17778 for (p
= *arg
; vim_isIDc(*p
); ++p
)
17780 if (p
== *arg
) /* no name found */
17783 len
= (int)(p
- *arg
);
17789 * Get the length of the name of a function or internal variable.
17790 * "arg" is advanced to the first non-white character after the name.
17791 * Return 0 if something is wrong.
17800 /* Find the end of the name. */
17801 for (p
= *arg
; eval_isnamec(*p
); ++p
)
17803 if (p
== *arg
) /* no name found */
17806 len
= (int)(p
- *arg
);
17807 *arg
= skipwhite(p
);
17813 * Get the length of the name of a variable or function.
17814 * Only the name is recognized, does not handle ".key" or "[idx]".
17815 * "arg" is advanced to the first non-white character after the name.
17816 * Return -1 if curly braces expansion failed.
17817 * Return 0 if something else is wrong.
17818 * If the name contains 'magic' {}'s, expand them and return the
17819 * expanded name in an allocated string via 'alias' - caller must free.
17822 get_name_len(arg
, alias
, evaluate
, verbose
)
17830 char_u
*expr_start
;
17833 *alias
= NULL
; /* default to no alias */
17835 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
17836 && (*arg
)[2] == (int)KE_SNR
)
17838 /* hard coded <SNR>, already translated */
17840 return get_id_len(arg
) + 3;
17842 len
= eval_fname_script(*arg
);
17845 /* literal "<SID>", "s:" or "<SNR>" */
17850 * Find the end of the name; check for {} construction.
17852 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
17853 len
> 0 ? 0 : FNE_CHECK_START
);
17854 if (expr_start
!= NULL
)
17856 char_u
*temp_string
;
17860 len
+= (int)(p
- *arg
);
17861 *arg
= skipwhite(p
);
17866 * Include any <SID> etc in the expanded string:
17867 * Thus the -len here.
17869 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
17870 if (temp_string
== NULL
)
17872 *alias
= temp_string
;
17873 *arg
= skipwhite(p
);
17874 return (int)STRLEN(temp_string
);
17877 len
+= get_id_len(arg
);
17878 if (len
== 0 && verbose
)
17879 EMSG2(_(e_invexpr2
), *arg
);
17885 * Find the end of a variable or function name, taking care of magic braces.
17886 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17887 * start and end of the first magic braces item.
17888 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
17889 * Return a pointer to just after the name. Equal to "arg" if there is no
17893 find_name_end(arg
, expr_start
, expr_end
, flags
)
17895 char_u
**expr_start
;
17903 if (expr_start
!= NULL
)
17905 *expr_start
= NULL
;
17909 /* Quick check for valid starting character. */
17910 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
17913 for (p
= arg
; *p
!= NUL
17914 && (eval_isnamec(*p
)
17916 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
17918 || br_nest
!= 0); mb_ptr_adv(p
))
17922 /* skip over 'string' to avoid counting [ and ] inside it. */
17923 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
17928 else if (*p
== '"')
17930 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17931 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
17932 if (*p
== '\\' && p
[1] != NUL
)
17942 else if (*p
== ']')
17951 if (expr_start
!= NULL
&& *expr_start
== NULL
)
17954 else if (*p
== '}')
17957 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
17967 * Expands out the 'magic' {}'s in a variable/function name.
17968 * Note that this can call itself recursively, to deal with
17969 * constructs like foo{bar}{baz}{bam}
17970 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17976 * Returns a new allocated string, which the caller must free.
17977 * Returns NULL for failure.
17980 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
17982 char_u
*expr_start
;
17987 char_u
*retval
= NULL
;
17988 char_u
*temp_result
;
17989 char_u
*nextcmd
= NULL
;
17991 if (expr_end
== NULL
|| in_end
== NULL
)
17998 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
17999 if (temp_result
!= NULL
&& nextcmd
== NULL
)
18001 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
18002 + (in_end
- expr_end
) + 1));
18003 if (retval
!= NULL
)
18005 STRCPY(retval
, in_start
);
18006 STRCAT(retval
, temp_result
);
18007 STRCAT(retval
, expr_end
+ 1);
18010 vim_free(temp_result
);
18012 *in_end
= c1
; /* put char back for error messages */
18016 if (retval
!= NULL
)
18018 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
18019 if (expr_start
!= NULL
)
18021 /* Further expansion! */
18022 temp_result
= make_expanded_name(retval
, expr_start
,
18023 expr_end
, temp_result
);
18025 retval
= temp_result
;
18033 * Return TRUE if character "c" can be used in a variable or function name.
18034 * Does not include '{' or '}' for magic braces.
18040 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
18044 * Return TRUE if character "c" can be used as the first character in a
18045 * variable or function name (excluding '{' and '}').
18051 return (ASCII_ISALPHA(c
) || c
== '_');
18055 * Set number v: variable to "val".
18058 set_vim_var_nr(idx
, val
)
18062 vimvars
[idx
].vv_nr
= val
;
18066 * Get number v: variable value.
18069 get_vim_var_nr(idx
)
18072 return vimvars
[idx
].vv_nr
;
18075 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18077 * Get string v: variable value. Uses a static buffer, can only be used once.
18080 get_vim_var_str(idx
)
18083 return get_tv_string(&vimvars
[idx
].vv_tv
);
18088 * Set v:count, v:count1 and v:prevcount.
18091 set_vcount(count
, count1
)
18095 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
18096 vimvars
[VV_COUNT
].vv_nr
= count
;
18097 vimvars
[VV_COUNT1
].vv_nr
= count1
;
18101 * Set string v: variable to a copy of "val".
18104 set_vim_var_string(idx
, val
, len
)
18107 int len
; /* length of "val" to use or -1 (whole string) */
18109 /* Need to do this (at least) once, since we can't initialize a union.
18110 * Will always be invoked when "v:progname" is set. */
18111 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
18113 vim_free(vimvars
[idx
].vv_str
);
18115 vimvars
[idx
].vv_str
= NULL
;
18116 else if (len
== -1)
18117 vimvars
[idx
].vv_str
= vim_strsave(val
);
18119 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
18123 * Set v:register if needed.
18131 if (c
== 0 || c
== ' ')
18135 /* Avoid free/alloc when the value is already right. */
18136 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
18137 set_vim_var_string(VV_REG
, ®name
, 1);
18141 * Get or set v:exception. If "oldval" == NULL, return the current value.
18142 * Otherwise, restore the value to "oldval" and return NULL.
18143 * Must always be called in pairs to save and restore v:exception! Does not
18144 * take care of memory allocations.
18147 v_exception(oldval
)
18150 if (oldval
== NULL
)
18151 return vimvars
[VV_EXCEPTION
].vv_str
;
18153 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
18158 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18159 * Otherwise, restore the value to "oldval" and return NULL.
18160 * Must always be called in pairs to save and restore v:throwpoint! Does not
18161 * take care of memory allocations.
18164 v_throwpoint(oldval
)
18167 if (oldval
== NULL
)
18168 return vimvars
[VV_THROWPOINT
].vv_str
;
18170 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
18174 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18177 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18178 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18179 * Must always be called in pairs!
18182 set_cmdarg(eap
, oldarg
)
18190 oldval
= vimvars
[VV_CMDARG
].vv_str
;
18194 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
18198 if (eap
->force_bin
== FORCE_BIN
)
18200 else if (eap
->force_bin
== FORCE_NOBIN
)
18205 if (eap
->read_edit
)
18208 if (eap
->force_ff
!= 0)
18209 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
18211 if (eap
->force_enc
!= 0)
18212 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
18213 if (eap
->bad_char
!= 0)
18214 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
18217 newval
= alloc(len
+ 1);
18218 if (newval
== NULL
)
18221 if (eap
->force_bin
== FORCE_BIN
)
18222 sprintf((char *)newval
, " ++bin");
18223 else if (eap
->force_bin
== FORCE_NOBIN
)
18224 sprintf((char *)newval
, " ++nobin");
18228 if (eap
->read_edit
)
18229 STRCAT(newval
, " ++edit");
18231 if (eap
->force_ff
!= 0)
18232 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
18233 eap
->cmd
+ eap
->force_ff
);
18235 if (eap
->force_enc
!= 0)
18236 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
18237 eap
->cmd
+ eap
->force_enc
);
18238 if (eap
->bad_char
!= 0)
18239 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
18240 eap
->cmd
+ eap
->bad_char
);
18242 vimvars
[VV_CMDARG
].vv_str
= newval
;
18248 * Get the value of internal variable "name".
18249 * Return OK or FAIL.
18252 get_var_tv(name
, len
, rettv
, verbose
)
18254 int len
; /* length of "name" */
18255 typval_T
*rettv
; /* NULL when only checking existence */
18256 int verbose
; /* may give error message */
18259 typval_T
*tv
= NULL
;
18264 /* truncate the name, so that we can use strcmp() */
18269 * Check for "b:changedtick".
18271 if (STRCMP(name
, "b:changedtick") == 0)
18273 atv
.v_type
= VAR_NUMBER
;
18274 atv
.vval
.v_number
= curbuf
->b_changedtick
;
18279 * Check for user-defined variables.
18283 v
= find_var(name
, NULL
);
18290 if (rettv
!= NULL
&& verbose
)
18291 EMSG2(_(e_undefvar
), name
);
18294 else if (rettv
!= NULL
)
18295 copy_tv(tv
, rettv
);
18303 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18304 * Also handle function call with Funcref variable: func(expr)
18305 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18308 handle_subscript(arg
, rettv
, evaluate
, verbose
)
18311 int evaluate
; /* do more than finding the end */
18312 int verbose
; /* give error messages */
18315 dict_T
*selfdict
= NULL
;
18322 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
18323 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
18324 && !vim_iswhite(*(*arg
- 1)))
18328 /* need to copy the funcref so that we can clear rettv */
18330 rettv
->v_type
= VAR_UNKNOWN
;
18332 /* Invoke the function. Recursive! */
18333 s
= functv
.vval
.v_string
;
18334 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
18335 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
18336 &len
, evaluate
, selfdict
);
18338 /* Clear the funcref afterwards, so that deleting it while
18339 * evaluating the arguments is possible (see test55). */
18342 /* Stop the expression evaluation when immediately aborting on
18343 * error, or when an interrupt occurred or an exception was thrown
18344 * but not caught. */
18351 dict_unref(selfdict
);
18354 else /* **arg == '[' || **arg == '.' */
18356 dict_unref(selfdict
);
18357 if (rettv
->v_type
== VAR_DICT
)
18359 selfdict
= rettv
->vval
.v_dict
;
18360 if (selfdict
!= NULL
)
18361 ++selfdict
->dv_refcount
;
18365 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
18372 dict_unref(selfdict
);
18377 * Allocate memory for a variable type-value, and make it empty (0 or NULL
18383 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
18387 * Allocate memory for a variable type-value, and assign a string to it.
18388 * The string "s" must have been allocated, it is consumed.
18389 * Return NULL for out of memory, the variable otherwise.
18397 rettv
= alloc_tv();
18400 rettv
->v_type
= VAR_STRING
;
18401 rettv
->vval
.v_string
= s
;
18409 * Free the memory for a variable type-value.
18417 switch (varp
->v_type
)
18420 func_unref(varp
->vval
.v_string
);
18423 vim_free(varp
->vval
.v_string
);
18426 list_unref(varp
->vval
.v_list
);
18429 dict_unref(varp
->vval
.v_dict
);
18438 EMSG2(_(e_intern2
), "free_tv()");
18446 * Free the memory for a variable value and set the value to NULL or 0.
18454 switch (varp
->v_type
)
18457 func_unref(varp
->vval
.v_string
);
18460 vim_free(varp
->vval
.v_string
);
18461 varp
->vval
.v_string
= NULL
;
18464 list_unref(varp
->vval
.v_list
);
18465 varp
->vval
.v_list
= NULL
;
18468 dict_unref(varp
->vval
.v_dict
);
18469 varp
->vval
.v_dict
= NULL
;
18472 varp
->vval
.v_number
= 0;
18476 varp
->vval
.v_float
= 0.0;
18482 EMSG2(_(e_intern2
), "clear_tv()");
18489 * Set the value of a variable to NULL without freeing items.
18496 vim_memset(varp
, 0, sizeof(typval_T
));
18500 * Get the number value of a variable.
18501 * If it is a String variable, uses vim_str2nr().
18502 * For incompatible types, return 0.
18503 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18504 * caller of incompatible types: it sets *denote to TRUE if "denote"
18505 * is not NULL or returns -1 otherwise.
18508 get_tv_number(varp
)
18513 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
18517 get_tv_number_chk(varp
, denote
)
18523 switch (varp
->v_type
)
18526 return (long)(varp
->vval
.v_number
);
18529 EMSG(_("E805: Using a Float as a Number"));
18533 EMSG(_("E703: Using a Funcref as a Number"));
18536 if (varp
->vval
.v_string
!= NULL
)
18537 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
18538 TRUE
, TRUE
, &n
, NULL
);
18541 EMSG(_("E745: Using a List as a Number"));
18544 EMSG(_("E728: Using a Dictionary as a Number"));
18547 EMSG2(_(e_intern2
), "get_tv_number()");
18550 if (denote
== NULL
) /* useful for values that must be unsigned */
18558 * Get the lnum from the first argument.
18559 * Also accepts ".", "$", etc., but that only works for the current buffer.
18560 * Returns -1 on error.
18563 get_tv_lnum(argvars
)
18569 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
18570 if (lnum
== 0) /* no valid number, try using line() */
18572 rettv
.v_type
= VAR_NUMBER
;
18573 f_line(argvars
, &rettv
);
18574 lnum
= rettv
.vval
.v_number
;
18581 * Get the lnum from the first argument.
18582 * Also accepts "$", then "buf" is used.
18583 * Returns 0 on error.
18586 get_tv_lnum_buf(argvars
, buf
)
18590 if (argvars
[0].v_type
== VAR_STRING
18591 && argvars
[0].vval
.v_string
!= NULL
18592 && argvars
[0].vval
.v_string
[0] == '$'
18594 return buf
->b_ml
.ml_line_count
;
18595 return get_tv_number_chk(&argvars
[0], NULL
);
18599 * Get the string value of a variable.
18600 * If it is a Number variable, the number is converted into a string.
18601 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18602 * get_tv_string_buf() uses a given buffer.
18603 * If the String variable has never been set, return an empty string.
18604 * Never returns NULL;
18605 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18609 get_tv_string(varp
)
18612 static char_u mybuf
[NUMBUFLEN
];
18614 return get_tv_string_buf(varp
, mybuf
);
18618 get_tv_string_buf(varp
, buf
)
18622 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
18624 return res
!= NULL
? res
: (char_u
*)"";
18628 get_tv_string_chk(varp
)
18631 static char_u mybuf
[NUMBUFLEN
];
18633 return get_tv_string_buf_chk(varp
, mybuf
);
18637 get_tv_string_buf_chk(varp
, buf
)
18641 switch (varp
->v_type
)
18644 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
18647 EMSG(_("E729: using Funcref as a String"));
18650 EMSG(_("E730: using List as a String"));
18653 EMSG(_("E731: using Dictionary as a String"));
18657 EMSG(_("E806: using Float as a String"));
18661 if (varp
->vval
.v_string
!= NULL
)
18662 return varp
->vval
.v_string
;
18663 return (char_u
*)"";
18665 EMSG2(_(e_intern2
), "get_tv_string_buf()");
18672 * Find variable "name" in the list of variables.
18673 * Return a pointer to it if found, NULL if not found.
18674 * Careful: "a:0" variables don't have a name.
18675 * When "htp" is not NULL we are writing to the variable, set "htp" to the
18678 static dictitem_T
*
18679 find_var(name
, htp
)
18686 ht
= find_var_ht(name
, &varname
);
18691 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
18695 * Find variable "varname" in hashtab "ht".
18696 * Returns NULL if not found.
18698 static dictitem_T
*
18699 find_var_in_ht(ht
, varname
, writing
)
18706 if (*varname
== NUL
)
18708 /* Must be something like "s:", otherwise "ht" would be NULL. */
18709 switch (varname
[-2])
18711 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
18712 case 'g': return &globvars_var
;
18713 case 'v': return &vimvars_var
;
18714 case 'b': return &curbuf
->b_bufvar
;
18715 case 'w': return &curwin
->w_winvar
;
18716 #ifdef FEAT_WINDOWS
18717 case 't': return &curtab
->tp_winvar
;
18719 case 'l': return current_funccal
== NULL
18720 ? NULL
: ¤t_funccal
->l_vars_var
;
18721 case 'a': return current_funccal
== NULL
18722 ? NULL
: ¤t_funccal
->l_avars_var
;
18727 hi
= hash_find(ht
, varname
);
18728 if (HASHITEM_EMPTY(hi
))
18730 /* For global variables we may try auto-loading the script. If it
18731 * worked find the variable again. Don't auto-load a script if it was
18732 * loaded already, otherwise it would be loaded every time when
18733 * checking if a function name is a Funcref variable. */
18734 if (ht
== &globvarht
&& !writing
18735 && script_autoload(varname
, FALSE
) && !aborting())
18736 hi
= hash_find(ht
, varname
);
18737 if (HASHITEM_EMPTY(hi
))
18744 * Find the hashtab used for a variable name.
18745 * Set "varname" to the start of name without ':'.
18748 find_var_ht(name
, varname
)
18754 if (name
[1] != ':')
18756 /* The name must not start with a colon or #. */
18757 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
18761 /* "version" is "v:version" in all scopes */
18762 hi
= hash_find(&compat_hashtab
, name
);
18763 if (!HASHITEM_EMPTY(hi
))
18764 return &compat_hashtab
;
18766 if (current_funccal
== NULL
)
18767 return &globvarht
; /* global variable */
18768 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
18770 *varname
= name
+ 2;
18771 if (*name
== 'g') /* global variable */
18773 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18775 if (vim_strchr(name
+ 2, ':') != NULL
18776 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
18778 if (*name
== 'b') /* buffer variable */
18779 return &curbuf
->b_vars
.dv_hashtab
;
18780 if (*name
== 'w') /* window variable */
18781 return &curwin
->w_vars
.dv_hashtab
;
18782 #ifdef FEAT_WINDOWS
18783 if (*name
== 't') /* tab page variable */
18784 return &curtab
->tp_vars
.dv_hashtab
;
18786 if (*name
== 'v') /* v: variable */
18788 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
18789 return ¤t_funccal
->l_avars
.dv_hashtab
;
18790 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
18791 return ¤t_funccal
->l_vars
.dv_hashtab
;
18792 if (*name
== 's' /* script variable */
18793 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
18794 return &SCRIPT_VARS(current_SID
);
18799 * Get the string value of a (global/local) variable.
18800 * Returns NULL when it doesn't exist.
18803 get_var_value(name
)
18808 v
= find_var(name
, NULL
);
18811 return get_tv_string(&v
->di_tv
);
18815 * Allocate a new hashtab for a sourced script. It will be used while
18816 * sourcing this script and when executing functions defined in the script.
18819 new_script_vars(id
)
18826 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
18828 /* Re-allocating ga_data means that an ht_array pointing to
18829 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
18830 * at its init value. Also reset "v_dict", it's always the same. */
18831 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
18833 ht
= &SCRIPT_VARS(i
);
18834 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
18835 ht
->ht_array
= ht
->ht_smallarray
;
18836 sv
= &SCRIPT_SV(i
);
18837 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
18840 while (ga_scripts
.ga_len
< id
)
18842 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
18843 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
18844 ++ga_scripts
.ga_len
;
18850 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18854 init_var_dict(dict
, dict_var
)
18856 dictitem_T
*dict_var
;
18858 hash_init(&dict
->dv_hashtab
);
18859 dict
->dv_refcount
= 99999;
18860 dict_var
->di_tv
.vval
.v_dict
= dict
;
18861 dict_var
->di_tv
.v_type
= VAR_DICT
;
18862 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
18863 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
18864 dict_var
->di_key
[0] = NUL
;
18868 * Clean up a list of internal variables.
18869 * Frees all allocated variables and the value they contain.
18870 * Clears hashtab "ht", does not free it.
18876 vars_clear_ext(ht
, TRUE
);
18880 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18883 vars_clear_ext(ht
, free_val
)
18892 todo
= (int)ht
->ht_used
;
18893 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
18895 if (!HASHITEM_EMPTY(hi
))
18899 /* Free the variable. Don't remove it from the hashtab,
18900 * ht_array might change then. hash_clear() takes care of it
18904 clear_tv(&v
->di_tv
);
18905 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
18914 * Delete a variable from hashtab "ht" at item "hi".
18915 * Clear the variable value and free the dictitem.
18922 dictitem_T
*di
= HI2DI(hi
);
18924 hash_remove(ht
, hi
);
18925 clear_tv(&di
->di_tv
);
18930 * List the value of one internal variable.
18933 list_one_var(v
, prefix
, first
)
18940 char_u numbuf
[NUMBUFLEN
];
18942 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
18943 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
18944 s
== NULL
? (char_u
*)"" : s
, first
);
18949 list_one_var_a(prefix
, name
, type
, string
, first
)
18954 int *first
; /* when TRUE clear rest of screen and set to FALSE */
18956 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18959 if (name
!= NULL
) /* "a:" vars don't have a name stored */
18963 if (type
== VAR_NUMBER
)
18965 else if (type
== VAR_FUNC
)
18967 else if (type
== VAR_LIST
)
18970 if (*string
== '[')
18973 else if (type
== VAR_DICT
)
18976 if (*string
== '{')
18982 msg_outtrans(string
);
18984 if (type
== VAR_FUNC
)
18985 msg_puts((char_u
*)"()");
18994 * Set variable "name" to value in "tv".
18995 * If the variable already exists, the value is updated.
18996 * Otherwise the variable is created.
18999 set_var(name
, tv
, copy
)
19002 int copy
; /* make copy of value in "tv" */
19009 if (tv
->v_type
== VAR_FUNC
)
19011 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
19012 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
19013 ? name
[2] : name
[0]))
19015 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
19018 if (function_exists(name
))
19020 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19026 ht
= find_var_ht(name
, &varname
);
19027 if (ht
== NULL
|| *varname
== NUL
)
19029 EMSG2(_(e_illvar
), name
);
19033 v
= find_var_in_ht(ht
, varname
, TRUE
);
19036 /* existing variable, need to clear the value */
19037 if (var_check_ro(v
->di_flags
, name
)
19038 || tv_check_lock(v
->di_tv
.v_lock
, name
))
19040 if (v
->di_tv
.v_type
!= tv
->v_type
19041 && !((v
->di_tv
.v_type
== VAR_STRING
19042 || v
->di_tv
.v_type
== VAR_NUMBER
)
19043 && (tv
->v_type
== VAR_STRING
19044 || tv
->v_type
== VAR_NUMBER
))
19046 && !((v
->di_tv
.v_type
== VAR_NUMBER
19047 || v
->di_tv
.v_type
== VAR_FLOAT
)
19048 && (tv
->v_type
== VAR_NUMBER
19049 || tv
->v_type
== VAR_FLOAT
))
19053 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
19058 * Handle setting internal v: variables separately: we don't change
19061 if (ht
== &vimvarht
)
19063 if (v
->di_tv
.v_type
== VAR_STRING
)
19065 vim_free(v
->di_tv
.vval
.v_string
);
19066 if (copy
|| tv
->v_type
!= VAR_STRING
)
19067 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
19070 /* Take over the string to avoid an extra alloc/free. */
19071 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
19072 tv
->vval
.v_string
= NULL
;
19075 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
19076 EMSG2(_(e_intern2
), "set_var()");
19079 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
19080 if (STRCMP(varname
, "searchforward") == 0)
19081 set_search_direction(v
->di_tv
.vval
.v_number
? '/' : '?');
19086 clear_tv(&v
->di_tv
);
19088 else /* add a new variable */
19090 /* Can't add "v:" variable. */
19091 if (ht
== &vimvarht
)
19093 EMSG2(_(e_illvar
), name
);
19097 /* Make sure the variable name is valid. */
19098 for (p
= varname
; *p
!= NUL
; ++p
)
19099 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
19100 && *p
!= AUTOLOAD_CHAR
)
19102 EMSG2(_(e_illvar
), varname
);
19106 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19107 + STRLEN(varname
)));
19110 STRCPY(v
->di_key
, varname
);
19111 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
19119 if (copy
|| tv
->v_type
== VAR_NUMBER
|| tv
->v_type
== VAR_FLOAT
)
19120 copy_tv(tv
, &v
->di_tv
);
19124 v
->di_tv
.v_lock
= 0;
19130 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
19131 * Also give an error message.
19134 var_check_ro(flags
, name
)
19138 if (flags
& DI_FLAGS_RO
)
19140 EMSG2(_(e_readonlyvar
), name
);
19143 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
19145 EMSG2(_(e_readonlysbx
), name
);
19152 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19153 * Also give an error message.
19156 var_check_fixed(flags
, name
)
19160 if (flags
& DI_FLAGS_FIX
)
19162 EMSG2(_("E795: Cannot delete variable %s"), name
);
19169 * Return TRUE if typeval "tv" is set to be locked (immutable).
19170 * Also give an error message, using "name".
19173 tv_check_lock(lock
, name
)
19177 if (lock
& VAR_LOCKED
)
19179 EMSG2(_("E741: Value is locked: %s"),
19180 name
== NULL
? (char_u
*)_("Unknown") : name
);
19183 if (lock
& VAR_FIXED
)
19185 EMSG2(_("E742: Cannot change value of %s"),
19186 name
== NULL
? (char_u
*)_("Unknown") : name
);
19193 * Copy the values from typval_T "from" to typval_T "to".
19194 * When needed allocates string or increases reference count.
19195 * Does not make a copy of a list or dict but copies the reference!
19202 to
->v_type
= from
->v_type
;
19204 switch (from
->v_type
)
19207 to
->vval
.v_number
= from
->vval
.v_number
;
19211 to
->vval
.v_float
= from
->vval
.v_float
;
19216 if (from
->vval
.v_string
== NULL
)
19217 to
->vval
.v_string
= NULL
;
19220 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
19221 if (from
->v_type
== VAR_FUNC
)
19222 func_ref(to
->vval
.v_string
);
19226 if (from
->vval
.v_list
== NULL
)
19227 to
->vval
.v_list
= NULL
;
19230 to
->vval
.v_list
= from
->vval
.v_list
;
19231 ++to
->vval
.v_list
->lv_refcount
;
19235 if (from
->vval
.v_dict
== NULL
)
19236 to
->vval
.v_dict
= NULL
;
19239 to
->vval
.v_dict
= from
->vval
.v_dict
;
19240 ++to
->vval
.v_dict
->dv_refcount
;
19244 EMSG2(_(e_intern2
), "copy_tv()");
19250 * Make a copy of an item.
19251 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
19252 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19253 * reference to an already copied list/dict can be used.
19254 * Returns FAIL or OK.
19257 item_copy(from
, to
, deep
, copyID
)
19263 static int recurse
= 0;
19266 if (recurse
>= DICT_MAXNEST
)
19268 EMSG(_("E698: variable nested too deep for making a copy"));
19273 switch (from
->v_type
)
19284 to
->v_type
= VAR_LIST
;
19286 if (from
->vval
.v_list
== NULL
)
19287 to
->vval
.v_list
= NULL
;
19288 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
19290 /* use the copy made earlier */
19291 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
19292 ++to
->vval
.v_list
->lv_refcount
;
19295 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
19296 if (to
->vval
.v_list
== NULL
)
19300 to
->v_type
= VAR_DICT
;
19302 if (from
->vval
.v_dict
== NULL
)
19303 to
->vval
.v_dict
= NULL
;
19304 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
19306 /* use the copy made earlier */
19307 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
19308 ++to
->vval
.v_dict
->dv_refcount
;
19311 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
19312 if (to
->vval
.v_dict
== NULL
)
19316 EMSG2(_(e_intern2
), "item_copy()");
19324 * ":echo expr1 ..." print each argument separated with a space, add a
19325 * newline at the end.
19326 * ":echon expr1 ..." print each argument plain.
19332 char_u
*arg
= eap
->arg
;
19336 int needclr
= TRUE
;
19337 int atstart
= TRUE
;
19338 char_u numbuf
[NUMBUFLEN
];
19342 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
19344 /* If eval1() causes an error message the text from the command may
19345 * still need to be cleared. E.g., "echo 22,44". */
19346 need_clr_eos
= needclr
;
19349 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19352 * Report the invalid expression unless the expression evaluation
19353 * has been cancelled due to an aborting error, an interrupt, or an
19357 EMSG2(_(e_invexpr2
), p
);
19358 need_clr_eos
= FALSE
;
19361 need_clr_eos
= FALSE
;
19368 /* Call msg_start() after eval1(), evaluating the expression
19369 * may cause a message to appear. */
19370 if (eap
->cmdidx
== CMD_echo
)
19373 else if (eap
->cmdidx
== CMD_echo
)
19374 msg_puts_attr((char_u
*)" ", echo_attr
);
19375 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
19377 for ( ; *p
!= NUL
&& !got_int
; ++p
)
19379 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
19381 if (*p
!= TAB
&& needclr
)
19383 /* remove any text still there from the command */
19387 msg_putchar_attr(*p
, echo_attr
);
19394 int i
= (*mb_ptr2len
)(p
);
19396 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
19401 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
19407 arg
= skipwhite(arg
);
19409 eap
->nextcmd
= check_nextcmd(arg
);
19415 /* remove text that may still be there from the command */
19418 if (eap
->cmdidx
== CMD_echo
)
19424 * ":echohl {name}".
19432 id
= syn_name2id(eap
->arg
);
19436 echo_attr
= syn_id2attr(id
);
19440 * ":execute expr1 ..." execute the result of an expression.
19441 * ":echomsg expr1 ..." Print a message
19442 * ":echoerr expr1 ..." Print an error
19443 * Each gets spaces around each argument and a newline at the end for
19450 char_u
*arg
= eap
->arg
;
19458 ga_init2(&ga
, 1, 80);
19462 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
19465 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19468 * Report the invalid expression unless the expression evaluation
19469 * has been cancelled due to an aborting error, an interrupt, or an
19473 EMSG2(_(e_invexpr2
), p
);
19480 p
= get_tv_string(&rettv
);
19481 len
= (int)STRLEN(p
);
19482 if (ga_grow(&ga
, len
+ 2) == FAIL
)
19489 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
19490 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
19495 arg
= skipwhite(arg
);
19498 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
19500 if (eap
->cmdidx
== CMD_echomsg
)
19502 MSG_ATTR(ga
.ga_data
, echo_attr
);
19505 else if (eap
->cmdidx
== CMD_echoerr
)
19507 /* We don't want to abort following commands, restore did_emsg. */
19508 save_did_emsg
= did_emsg
;
19509 EMSG((char_u
*)ga
.ga_data
);
19511 did_emsg
= save_did_emsg
;
19513 else if (eap
->cmdidx
== CMD_execute
)
19514 do_cmdline((char_u
*)ga
.ga_data
,
19515 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
19523 eap
->nextcmd
= check_nextcmd(arg
);
19527 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19528 * "arg" points to the "&" or '+' when called, to "option" when returning.
19529 * Returns NULL when no option name found. Otherwise pointer to the char
19530 * after the option name.
19533 find_option_end(arg
, opt_flags
)
19540 if (*p
== 'g' && p
[1] == ':')
19542 *opt_flags
= OPT_GLOBAL
;
19545 else if (*p
== 'l' && p
[1] == ':')
19547 *opt_flags
= OPT_LOCAL
;
19553 if (!ASCII_ISALPHA(*p
))
19557 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
19558 p
+= 4; /* termcap option */
19560 while (ASCII_ISALPHA(*p
))
19575 int saved_did_emsg
;
19576 char_u
*name
= NULL
;
19579 char_u
*line_arg
= NULL
;
19582 int varargs
= FALSE
;
19583 int mustend
= FALSE
;
19588 char_u
*skip_until
= NULL
;
19591 static int func_nr
= 0; /* number for nameless function */
19596 int sourcing_lnum_off
;
19599 * ":function" without argument: list functions.
19601 if (ends_excmd(*eap
->arg
))
19605 todo
= (int)func_hashtab
.ht_used
;
19606 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19608 if (!HASHITEM_EMPTY(hi
))
19612 if (!isdigit(*fp
->uf_name
))
19613 list_func_head(fp
, FALSE
);
19617 eap
->nextcmd
= check_nextcmd(eap
->arg
);
19622 * ":function /pat": list functions matching pattern.
19624 if (*eap
->arg
== '/')
19626 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
19629 regmatch_T regmatch
;
19633 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
19635 if (regmatch
.regprog
!= NULL
)
19637 regmatch
.rm_ic
= p_ic
;
19639 todo
= (int)func_hashtab
.ht_used
;
19640 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19642 if (!HASHITEM_EMPTY(hi
))
19646 if (!isdigit(*fp
->uf_name
)
19647 && vim_regexec(®match
, fp
->uf_name
, 0))
19648 list_func_head(fp
, FALSE
);
19655 eap
->nextcmd
= check_nextcmd(p
);
19660 * Get the function name. There are these situations:
19661 * func normal function name
19662 * "name" == func, "fudi.fd_dict" == NULL
19663 * dict.func new dictionary entry
19664 * "name" == NULL, "fudi.fd_dict" set,
19665 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19666 * dict.func existing dict entry with a Funcref
19667 * "name" == func, "fudi.fd_dict" set,
19668 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19669 * dict.func existing dict entry that's not a Funcref
19670 * "name" == NULL, "fudi.fd_dict" set,
19671 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19674 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19675 paren
= (vim_strchr(p
, '(') != NULL
);
19676 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
19679 * Return on an invalid expression in braces, unless the expression
19680 * evaluation has been cancelled due to an aborting error, an
19681 * interrupt, or an exception.
19685 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
19686 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
19687 vim_free(fudi
.fd_newkey
);
19694 /* An error in a function call during evaluation of an expression in magic
19695 * braces should not cause the function not to be defined. */
19696 saved_did_emsg
= did_emsg
;
19700 * ":function func" with only function name: list function.
19704 if (!ends_excmd(*skipwhite(p
)))
19706 EMSG(_(e_trailing
));
19709 eap
->nextcmd
= check_nextcmd(p
);
19710 if (eap
->nextcmd
!= NULL
)
19712 if (!eap
->skip
&& !got_int
)
19714 fp
= find_func(name
);
19717 list_func_head(fp
, TRUE
);
19718 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
19720 if (FUNCLINE(fp
, j
) == NULL
)
19723 msg_outnum((long)(j
+ 1));
19728 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
19729 out_flush(); /* show a line at a time */
19735 msg_puts((char_u
*)" endfunction");
19739 emsg_funcname("E123: Undefined function: %s", name
);
19745 * ":function name(arg1, arg2)" Define function.
19752 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
19755 /* attempt to continue by skipping some text */
19756 if (vim_strchr(p
, '(') != NULL
)
19757 p
= vim_strchr(p
, '(');
19759 p
= skipwhite(p
+ 1);
19761 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
19762 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
19766 /* Check the name of the function. Unless it's a dictionary function
19767 * (that we are overwriting). */
19771 arg
= fudi
.fd_newkey
;
19772 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
19773 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
19775 if (*arg
== K_SPECIAL
)
19779 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
19780 : eval_isnamec(arg
[j
])))
19783 emsg_funcname(_(e_invarg2
), arg
);
19788 * Isolate the arguments: "arg1, arg2, ...)"
19792 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
19801 while (ASCII_ISALNUM(*p
) || *p
== '_')
19803 if (arg
== p
|| isdigit(*arg
)
19804 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
19805 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
19808 EMSG2(_("E125: Illegal argument: %s"), arg
);
19811 if (ga_grow(&newargs
, 1) == FAIL
)
19815 arg
= vim_strsave(arg
);
19818 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
19827 if (mustend
&& *p
!= ')')
19830 EMSG2(_(e_invarg2
), eap
->arg
);
19834 ++p
; /* skip the ')' */
19836 /* find extra arguments "range", "dict" and "abort" */
19840 if (STRNCMP(p
, "range", 5) == 0)
19845 else if (STRNCMP(p
, "dict", 4) == 0)
19850 else if (STRNCMP(p
, "abort", 5) == 0)
19859 /* When there is a line break use what follows for the function body.
19860 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19863 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
19864 EMSG(_(e_trailing
));
19867 * Read the body of the function, until ":endfunction" is found.
19871 /* Check if the function already exists, don't let the user type the
19872 * whole function before telling him it doesn't work! For a script we
19873 * need to skip the body to be able to find what follows. */
19874 if (!eap
->skip
&& !eap
->forceit
)
19876 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
19877 EMSG(_(e_funcdict
));
19878 else if (name
!= NULL
&& find_func(name
) != NULL
)
19879 emsg_funcname(e_funcexts
, name
);
19882 if (!eap
->skip
&& did_emsg
)
19885 msg_putchar('\n'); /* don't overwrite the function name */
19886 cmdline_row
= msg_row
;
19894 need_wait_return
= FALSE
;
19895 sourcing_lnum_off
= sourcing_lnum
;
19897 if (line_arg
!= NULL
)
19899 /* Use eap->arg, split up in parts by line breaks. */
19900 theline
= line_arg
;
19901 p
= vim_strchr(theline
, '\n');
19903 line_arg
+= STRLEN(line_arg
);
19910 else if (eap
->getline
== NULL
)
19911 theline
= getcmdline(':', 0L, indent
);
19913 theline
= eap
->getline(':', eap
->cookie
, indent
);
19915 lines_left
= Rows
- 1;
19916 if (theline
== NULL
)
19918 EMSG(_("E126: Missing :endfunction"));
19922 /* Detect line continuation: sourcing_lnum increased more than one. */
19923 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
19924 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
19926 sourcing_lnum_off
= 0;
19928 if (skip_until
!= NULL
)
19930 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19931 * don't check for ":endfunc". */
19932 if (STRCMP(theline
, skip_until
) == 0)
19934 vim_free(skip_until
);
19940 /* skip ':' and blanks*/
19941 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
19944 /* Check for "endfunction". */
19945 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
19947 if (line_arg
== NULL
)
19952 /* Increase indent inside "if", "while", "for" and "try", decrease
19954 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
19956 else if (STRNCMP(p
, "if", 2) == 0
19957 || STRNCMP(p
, "wh", 2) == 0
19958 || STRNCMP(p
, "for", 3) == 0
19959 || STRNCMP(p
, "try", 3) == 0)
19962 /* Check for defining a function inside this function. */
19963 if (checkforcmd(&p
, "function", 2))
19966 p
= skipwhite(p
+ 1);
19967 p
+= eval_fname_script(p
);
19968 if (ASCII_ISALPHA(*p
))
19970 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
19971 if (*skipwhite(p
) == '(')
19979 /* Check for ":append" or ":insert". */
19980 p
= skip_range(p
, NULL
);
19981 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
19983 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
19984 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
19985 skip_until
= vim_strsave((char_u
*)".");
19987 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19988 arg
= skipwhite(skiptowhite(p
));
19989 if (arg
[0] == '<' && arg
[1] =='<'
19990 && ((p
[0] == 'p' && p
[1] == 'y'
19991 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
19992 || (p
[0] == 'p' && p
[1] == 'e'
19993 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
19994 || (p
[0] == 't' && p
[1] == 'c'
19995 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
19996 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
19997 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
19998 || (p
[0] == 'm' && p
[1] == 'z'
19999 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
20002 /* ":python <<" continues until a dot, like ":append" */
20003 p
= skipwhite(arg
+ 2);
20005 skip_until
= vim_strsave((char_u
*)".");
20007 skip_until
= vim_strsave(p
);
20011 /* Add the line to the function. */
20012 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
20014 if (line_arg
== NULL
)
20019 /* Copy the line to newly allocated memory. get_one_sourceline()
20020 * allocates 250 bytes per line, this saves 80% on average. The cost
20021 * is an extra alloc/free. */
20022 p
= vim_strsave(theline
);
20025 if (line_arg
== NULL
)
20030 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
20032 /* Add NULL lines for continuation lines, so that the line count is
20033 * equal to the index in the growarray. */
20034 while (sourcing_lnum_off
-- > 0)
20035 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
20037 /* Check for end of eap->arg. */
20038 if (line_arg
!= NULL
&& *line_arg
== NUL
)
20042 /* Don't define the function when skipping commands or when an error was
20044 if (eap
->skip
|| did_emsg
)
20048 * If there are no errors, add the function
20050 if (fudi
.fd_dict
== NULL
)
20052 v
= find_var(name
, &ht
);
20053 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
20055 emsg_funcname("E707: Function name conflicts with variable: %s",
20060 fp
= find_func(name
);
20065 emsg_funcname(e_funcexts
, name
);
20068 if (fp
->uf_calls
> 0)
20070 emsg_funcname("E127: Cannot redefine function %s: It is in use",
20074 /* redefine existing function */
20075 ga_clear_strings(&(fp
->uf_args
));
20076 ga_clear_strings(&(fp
->uf_lines
));
20086 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
20088 EMSG(_(e_funcdict
));
20091 if (fudi
.fd_di
== NULL
)
20093 /* Can't add a function to a locked dictionary */
20094 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
20097 /* Can't change an existing function if it is locked */
20098 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
20101 /* Give the function a sequential number. Can only be used with a
20104 sprintf(numbuf
, "%d", ++func_nr
);
20105 name
= vim_strsave((char_u
*)numbuf
);
20112 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
20115 char_u
*scriptname
;
20117 /* Check that the autoload name matches the script name. */
20119 if (sourcing_name
!= NULL
)
20121 scriptname
= autoload_name(name
);
20122 if (scriptname
!= NULL
)
20124 p
= vim_strchr(scriptname
, '/');
20125 plen
= (int)STRLEN(p
);
20126 slen
= (int)STRLEN(sourcing_name
);
20127 if (slen
> plen
&& fnamecmp(p
,
20128 sourcing_name
+ slen
- plen
) == 0)
20130 vim_free(scriptname
);
20135 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
20140 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
20144 if (fudi
.fd_dict
!= NULL
)
20146 if (fudi
.fd_di
== NULL
)
20148 /* add new dict entry */
20149 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
20150 if (fudi
.fd_di
== NULL
)
20155 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
20157 vim_free(fudi
.fd_di
);
20163 /* overwrite existing dict entry */
20164 clear_tv(&fudi
.fd_di
->di_tv
);
20165 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
20166 fudi
.fd_di
->di_tv
.v_lock
= 0;
20167 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
20168 fp
->uf_refcount
= 1;
20170 /* behave like "dict" was used */
20174 /* insert the new function in the function list */
20175 STRCPY(fp
->uf_name
, name
);
20176 hash_add(&func_hashtab
, UF2HIKEY(fp
));
20178 fp
->uf_args
= newargs
;
20179 fp
->uf_lines
= newlines
;
20180 #ifdef FEAT_PROFILE
20181 fp
->uf_tml_count
= NULL
;
20182 fp
->uf_tml_total
= NULL
;
20183 fp
->uf_tml_self
= NULL
;
20184 fp
->uf_profiling
= FALSE
;
20185 if (prof_def_func())
20186 func_do_profile(fp
);
20188 fp
->uf_varargs
= varargs
;
20189 fp
->uf_flags
= flags
;
20191 fp
->uf_script_ID
= current_SID
;
20195 ga_clear_strings(&newargs
);
20196 ga_clear_strings(&newlines
);
20198 vim_free(skip_until
);
20199 vim_free(fudi
.fd_newkey
);
20201 did_emsg
|= saved_did_emsg
;
20205 * Get a function name, translating "<SID>" and "<SNR>".
20206 * Also handles a Funcref in a List or Dictionary.
20207 * Returns the function name in allocated memory, or NULL for failure.
20209 * TFN_INT: internal function name OK
20210 * TFN_QUIET: be quiet
20211 * Advances "pp" to just after the function name (if no error).
20214 trans_function_name(pp
, skip
, flags
, fdp
)
20216 int skip
; /* only find the end, don't evaluate */
20218 funcdict_T
*fdp
; /* return: info about dictionary used */
20220 char_u
*name
= NULL
;
20224 char_u sid_buf
[20];
20229 vim_memset(fdp
, 0, sizeof(funcdict_T
));
20232 /* Check for hard coded <SNR>: already translated function ID (from a user
20234 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
20235 && (*pp
)[2] == (int)KE_SNR
)
20238 len
= get_id_len(pp
) + 3;
20239 return vim_strnsave(start
, len
);
20242 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20243 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
20244 lead
= eval_fname_script(start
);
20248 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
20249 lead
> 2 ? 0 : FNE_CHECK_START
);
20253 EMSG(_("E129: Function name required"));
20256 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
20259 * Report an invalid expression in braces, unless the expression
20260 * evaluation has been cancelled due to an aborting error, an
20261 * interrupt, or an exception.
20266 EMSG2(_(e_invarg2
), start
);
20269 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
20273 if (lv
.ll_tv
!= NULL
)
20277 fdp
->fd_dict
= lv
.ll_dict
;
20278 fdp
->fd_newkey
= lv
.ll_newkey
;
20279 lv
.ll_newkey
= NULL
;
20280 fdp
->fd_di
= lv
.ll_di
;
20282 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
20284 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
20289 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
20290 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
20291 EMSG(_(e_funcref
));
20299 if (lv
.ll_name
== NULL
)
20301 /* Error found, but continue after the function name. */
20306 /* Check if the name is a Funcref. If so, use the value. */
20307 if (lv
.ll_exp_name
!= NULL
)
20309 len
= (int)STRLEN(lv
.ll_exp_name
);
20310 name
= deref_func_name(lv
.ll_exp_name
, &len
);
20311 if (name
== lv
.ll_exp_name
)
20316 len
= (int)(end
- *pp
);
20317 name
= deref_func_name(*pp
, &len
);
20323 name
= vim_strsave(name
);
20328 if (lv
.ll_exp_name
!= NULL
)
20330 len
= (int)STRLEN(lv
.ll_exp_name
);
20331 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
20332 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
20334 /* When there was "s:" already or the name expanded to get a
20335 * leading "s:" then remove it. */
20343 if (lead
== 2) /* skip over "s:" */
20345 len
= (int)(end
- lv
.ll_name
);
20349 * Copy the function name to allocated memory.
20350 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20351 * Accept <SNR>123_name() outside a script.
20354 lead
= 0; /* do nothing */
20358 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
20359 || eval_fname_sid(*pp
))
20361 /* It's "s:" or "<SID>" */
20362 if (current_SID
<= 0)
20364 EMSG(_(e_usingsid
));
20367 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
20368 lead
+= (int)STRLEN(sid_buf
);
20371 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
20373 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
20376 name
= alloc((unsigned)(len
+ lead
+ 1));
20381 name
[0] = K_SPECIAL
;
20382 name
[1] = KS_EXTRA
;
20383 name
[2] = (int)KE_SNR
;
20384 if (lead
> 3) /* If it's "<SID>" */
20385 STRCPY(name
+ 3, sid_buf
);
20387 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
20388 name
[len
+ lead
] = NUL
;
20398 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20399 * Return 2 if "p" starts with "s:".
20400 * Return 0 otherwise.
20403 eval_fname_script(p
)
20406 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
20407 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
20409 if (p
[0] == 's' && p
[1] == ':')
20415 * Return TRUE if "p" starts with "<SID>" or "s:".
20416 * Only works if eval_fname_script() returned non-zero for "p"!
20422 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
20426 * List the head of the function: "name(arg1, arg2)".
20429 list_func_head(fp
, indent
)
20438 MSG_PUTS("function ");
20439 if (fp
->uf_name
[0] == K_SPECIAL
)
20441 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
20442 msg_puts(fp
->uf_name
+ 3);
20445 msg_puts(fp
->uf_name
);
20447 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
20451 msg_puts(FUNCARG(fp
, j
));
20453 if (fp
->uf_varargs
)
20462 last_set_msg(fp
->uf_script_ID
);
20466 * Find a function by name, return pointer to it in ufuncs.
20467 * Return NULL for unknown function.
20475 hi
= hash_find(&func_hashtab
, name
);
20476 if (!HASHITEM_EMPTY(hi
))
20481 #if defined(EXITFREE) || defined(PROTO)
20483 free_all_functions()
20487 /* Need to start all over every time, because func_free() may change the
20489 while (func_hashtab
.ht_used
> 0)
20490 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
20491 if (!HASHITEM_EMPTY(hi
))
20493 func_free(HI2UF(hi
));
20500 * Return TRUE if a function "name" exists.
20503 function_exists(name
)
20510 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
20511 nm
= skipwhite(nm
);
20513 /* Only accept "funcname", "funcname ", "funcname (..." and
20514 * "funcname(...", not "funcname!...". */
20515 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
20517 if (builtin_function(p
))
20518 n
= (find_internal_func(p
) >= 0);
20520 n
= (find_func(p
) != NULL
);
20527 * Return TRUE if "name" looks like a builtin function name: starts with a
20528 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
20531 builtin_function(name
)
20534 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
20535 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
20538 #if defined(FEAT_PROFILE) || defined(PROTO)
20540 * Start profiling function "fp".
20543 func_do_profile(fp
)
20546 fp
->uf_tm_count
= 0;
20547 profile_zero(&fp
->uf_tm_self
);
20548 profile_zero(&fp
->uf_tm_total
);
20549 if (fp
->uf_tml_count
== NULL
)
20550 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
20551 (sizeof(int) * fp
->uf_lines
.ga_len
));
20552 if (fp
->uf_tml_total
== NULL
)
20553 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
20554 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20555 if (fp
->uf_tml_self
== NULL
)
20556 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
20557 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20558 fp
->uf_tml_idx
= -1;
20559 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
20560 || fp
->uf_tml_self
== NULL
)
20561 return; /* out of memory */
20563 fp
->uf_profiling
= TRUE
;
20567 * Dump the profiling results for all functions in file "fd".
20570 func_dump_profile(fd
)
20580 todo
= (int)func_hashtab
.ht_used
;
20581 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
20583 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
20585 if (!HASHITEM_EMPTY(hi
))
20589 if (fp
->uf_profiling
)
20591 if (sorttab
!= NULL
)
20592 sorttab
[st_len
++] = fp
;
20594 if (fp
->uf_name
[0] == K_SPECIAL
)
20595 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
20597 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
20598 if (fp
->uf_tm_count
== 1)
20599 fprintf(fd
, "Called 1 time\n");
20601 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
20602 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
20603 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
20605 fprintf(fd
, "count total (s) self (s)\n");
20607 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
20609 if (FUNCLINE(fp
, i
) == NULL
)
20611 prof_func_line(fd
, fp
->uf_tml_count
[i
],
20612 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
20613 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
20620 if (sorttab
!= NULL
&& st_len
> 0)
20622 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20624 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
20625 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20627 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
20632 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
20637 int prefer_self
; /* when equal print only self time */
20642 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
20643 fprintf(fd
, "count total (s) self (s) function\n");
20644 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
20647 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
20649 if (fp
->uf_name
[0] == K_SPECIAL
)
20650 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
20652 fprintf(fd
, " %s()\n", fp
->uf_name
);
20658 * Print the count and times for one function or function line.
20661 prof_func_line(fd
, count
, total
, self
, prefer_self
)
20666 int prefer_self
; /* when equal print only self time */
20670 fprintf(fd
, "%5d ", count
);
20671 if (prefer_self
&& profile_equal(total
, self
))
20674 fprintf(fd
, "%s ", profile_msg(total
));
20675 if (!prefer_self
&& profile_equal(total
, self
))
20678 fprintf(fd
, "%s ", profile_msg(self
));
20685 * Compare function for total time sorting.
20688 #ifdef __BORLANDC__
20691 prof_total_cmp(s1
, s2
)
20697 p1
= *(ufunc_T
**)s1
;
20698 p2
= *(ufunc_T
**)s2
;
20699 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
20703 * Compare function for self time sorting.
20706 #ifdef __BORLANDC__
20709 prof_self_cmp(s1
, s2
)
20715 p1
= *(ufunc_T
**)s1
;
20716 p2
= *(ufunc_T
**)s2
;
20717 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
20723 * If "name" has a package name try autoloading the script for it.
20724 * Return TRUE if a package was loaded.
20727 script_autoload(name
, reload
)
20729 int reload
; /* load script again when already loaded */
20732 char_u
*scriptname
, *tofree
;
20736 /* If there is no '#' after name[0] there is no package name. */
20737 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
20738 if (p
== NULL
|| p
== name
)
20741 tofree
= scriptname
= autoload_name(name
);
20743 /* Find the name in the list of previously loaded package names. Skip
20744 * "autoload/", it's always the same. */
20745 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
20746 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
20748 if (!reload
&& i
< ga_loaded
.ga_len
)
20749 ret
= FALSE
; /* was loaded already */
20752 /* Remember the name if it wasn't loaded already. */
20753 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
20755 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
20759 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
20760 if (source_runtime(scriptname
, FALSE
) == OK
)
20769 * Return the autoload script name for a function or variable name.
20770 * Returns NULL when out of memory.
20773 autoload_name(name
)
20777 char_u
*scriptname
;
20779 /* Get the script file name: replace '#' with '/', append ".vim". */
20780 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
20781 if (scriptname
== NULL
)
20783 STRCPY(scriptname
, "autoload/");
20784 STRCAT(scriptname
, name
);
20785 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
20786 STRCAT(scriptname
, ".vim");
20787 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
20792 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20795 * Function given to ExpandGeneric() to obtain the list of user defined
20799 get_user_func_name(xp
, idx
)
20803 static long_u done
;
20804 static hashitem_T
*hi
;
20810 hi
= func_hashtab
.ht_array
;
20812 if (done
< func_hashtab
.ht_used
)
20816 while (HASHITEM_EMPTY(hi
))
20820 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
20821 return fp
->uf_name
; /* prevents overflow */
20823 cat_func_name(IObuff
, fp
);
20824 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
20826 STRCAT(IObuff
, "(");
20827 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
20828 STRCAT(IObuff
, ")");
20835 #endif /* FEAT_CMDL_COMPL */
20838 * Copy the function name of "fp" to buffer "buf".
20839 * "buf" must be able to hold the function name plus three bytes.
20840 * Takes care of script-local function names.
20843 cat_func_name(buf
, fp
)
20847 if (fp
->uf_name
[0] == K_SPECIAL
)
20849 STRCPY(buf
, "<SNR>");
20850 STRCAT(buf
, fp
->uf_name
+ 3);
20853 STRCPY(buf
, fp
->uf_name
);
20857 * ":delfunction {name}"
20860 ex_delfunction(eap
)
20863 ufunc_T
*fp
= NULL
;
20869 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
20870 vim_free(fudi
.fd_newkey
);
20873 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
20874 EMSG(_(e_funcref
));
20877 if (!ends_excmd(*skipwhite(p
)))
20880 EMSG(_(e_trailing
));
20883 eap
->nextcmd
= check_nextcmd(p
);
20884 if (eap
->nextcmd
!= NULL
)
20888 fp
= find_func(name
);
20895 EMSG2(_(e_nofunc
), eap
->arg
);
20898 if (fp
->uf_calls
> 0)
20900 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
20904 if (fudi
.fd_dict
!= NULL
)
20906 /* Delete the dict item that refers to the function, it will
20907 * invoke func_unref() and possibly delete the function. */
20908 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
20916 * Free a function and remove it from the list of functions.
20924 /* clear this function */
20925 ga_clear_strings(&(fp
->uf_args
));
20926 ga_clear_strings(&(fp
->uf_lines
));
20927 #ifdef FEAT_PROFILE
20928 vim_free(fp
->uf_tml_count
);
20929 vim_free(fp
->uf_tml_total
);
20930 vim_free(fp
->uf_tml_self
);
20933 /* remove the function from the function hashtable */
20934 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
20935 if (HASHITEM_EMPTY(hi
))
20936 EMSG2(_(e_intern2
), "func_free()");
20938 hash_remove(&func_hashtab
, hi
);
20944 * Unreference a Function: decrement the reference count and free it when it
20945 * becomes zero. Only for numbered functions.
20953 if (name
!= NULL
&& isdigit(*name
))
20955 fp
= find_func(name
);
20957 EMSG2(_(e_intern2
), "func_unref()");
20958 else if (--fp
->uf_refcount
<= 0)
20960 /* Only delete it when it's not being used. Otherwise it's done
20961 * when "uf_calls" becomes zero. */
20962 if (fp
->uf_calls
== 0)
20969 * Count a reference to a Function.
20977 if (name
!= NULL
&& isdigit(*name
))
20979 fp
= find_func(name
);
20981 EMSG2(_(e_intern2
), "func_ref()");
20988 * Call a user function.
20991 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
20992 ufunc_T
*fp
; /* pointer to function */
20993 int argcount
; /* nr of args */
20994 typval_T
*argvars
; /* arguments */
20995 typval_T
*rettv
; /* return value */
20996 linenr_T firstline
; /* first line of range */
20997 linenr_T lastline
; /* last line of range */
20998 dict_T
*selfdict
; /* Dictionary for "self" */
21000 char_u
*save_sourcing_name
;
21001 linenr_T save_sourcing_lnum
;
21002 scid_T save_current_SID
;
21005 static int depth
= 0;
21007 int fixvar_idx
= 0; /* index in fixvar[] */
21010 char_u numbuf
[NUMBUFLEN
];
21012 #ifdef FEAT_PROFILE
21013 proftime_T wait_start
;
21014 proftime_T call_start
;
21017 /* If depth of calling is getting too high, don't execute the function */
21018 if (depth
>= p_mfd
)
21020 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
21021 rettv
->v_type
= VAR_NUMBER
;
21022 rettv
->vval
.v_number
= -1;
21027 line_breakcheck(); /* check for CTRL-C hit */
21029 fc
.caller
= current_funccal
;
21030 current_funccal
= &fc
;
21033 rettv
->vval
.v_number
= 0;
21035 fc
.returned
= FALSE
;
21036 fc
.level
= ex_nesting_level
;
21037 /* Check if this function has a breakpoint. */
21038 fc
.breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
21039 fc
.dbg_tick
= debug_tick
;
21042 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21043 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21044 * each argument variable and saves a lot of time.
21047 * Init l: variables.
21049 init_var_dict(&fc
.l_vars
, &fc
.l_vars_var
);
21050 if (selfdict
!= NULL
)
21052 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21053 * some compiler that checks the destination size. */
21054 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21056 STRCPY(name
, "self");
21057 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
21058 hash_add(&fc
.l_vars
.dv_hashtab
, DI2HIKEY(v
));
21059 v
->di_tv
.v_type
= VAR_DICT
;
21060 v
->di_tv
.v_lock
= 0;
21061 v
->di_tv
.vval
.v_dict
= selfdict
;
21062 ++selfdict
->dv_refcount
;
21066 * Init a: variables.
21067 * Set a:0 to "argcount".
21068 * Set a:000 to a list with room for the "..." arguments.
21070 init_var_dict(&fc
.l_avars
, &fc
.l_avars_var
);
21071 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "0",
21072 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
21073 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21074 STRCPY(v
->di_key
, "000");
21075 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21076 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
21077 v
->di_tv
.v_type
= VAR_LIST
;
21078 v
->di_tv
.v_lock
= VAR_FIXED
;
21079 v
->di_tv
.vval
.v_list
= &fc
.l_varlist
;
21080 vim_memset(&fc
.l_varlist
, 0, sizeof(list_T
));
21081 fc
.l_varlist
.lv_refcount
= 99999;
21082 fc
.l_varlist
.lv_lock
= VAR_FIXED
;
21085 * Set a:firstline to "firstline" and a:lastline to "lastline".
21086 * Set a:name to named arguments.
21087 * Set a:N to the "..." arguments.
21089 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "firstline",
21090 (varnumber_T
)firstline
);
21091 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "lastline",
21092 (varnumber_T
)lastline
);
21093 for (i
= 0; i
< argcount
; ++i
)
21095 ai
= i
- fp
->uf_args
.ga_len
;
21097 /* named argument a:name */
21098 name
= FUNCARG(fp
, i
);
21101 /* "..." argument a:1, a:2, etc. */
21102 sprintf((char *)numbuf
, "%d", ai
+ 1);
21105 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
21107 v
= &fc
.fixvar
[fixvar_idx
++].var
;
21108 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21112 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
21116 v
->di_flags
= DI_FLAGS_RO
;
21118 STRCPY(v
->di_key
, name
);
21119 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
21121 /* Note: the values are copied directly to avoid alloc/free.
21122 * "argvars" must have VAR_FIXED for v_lock. */
21123 v
->di_tv
= argvars
[i
];
21124 v
->di_tv
.v_lock
= VAR_FIXED
;
21126 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
21128 list_append(&fc
.l_varlist
, &fc
.l_listitems
[ai
]);
21129 fc
.l_listitems
[ai
].li_tv
= argvars
[i
];
21130 fc
.l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
21134 /* Don't redraw while executing the function. */
21135 ++RedrawingDisabled
;
21136 save_sourcing_name
= sourcing_name
;
21137 save_sourcing_lnum
= sourcing_lnum
;
21139 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
21140 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
21141 if (sourcing_name
!= NULL
)
21143 if (save_sourcing_name
!= NULL
21144 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
21145 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
21147 STRCPY(sourcing_name
, "function ");
21148 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
21150 if (p_verbose
>= 12)
21153 verbose_enter_scroll();
21155 smsg((char_u
*)_("calling %s"), sourcing_name
);
21156 if (p_verbose
>= 14)
21158 char_u buf
[MSG_BUF_LEN
];
21159 char_u numbuf2
[NUMBUFLEN
];
21163 msg_puts((char_u
*)"(");
21164 for (i
= 0; i
< argcount
; ++i
)
21167 msg_puts((char_u
*)", ");
21168 if (argvars
[i
].v_type
== VAR_NUMBER
)
21169 msg_outnum((long)argvars
[i
].vval
.v_number
);
21172 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
21175 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21181 msg_puts((char_u
*)")");
21183 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21185 verbose_leave_scroll();
21189 #ifdef FEAT_PROFILE
21190 if (do_profiling
== PROF_YES
)
21192 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
21193 func_do_profile(fp
);
21194 if (fp
->uf_profiling
21195 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
))
21198 profile_start(&call_start
);
21199 profile_zero(&fp
->uf_tm_children
);
21201 script_prof_save(&wait_start
);
21205 save_current_SID
= current_SID
;
21206 current_SID
= fp
->uf_script_ID
;
21207 save_did_emsg
= did_emsg
;
21210 /* call do_cmdline() to execute the lines */
21211 do_cmdline(NULL
, get_func_line
, (void *)&fc
,
21212 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
21214 --RedrawingDisabled
;
21216 /* when the function was aborted because of an error, return -1 */
21217 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
21220 rettv
->v_type
= VAR_NUMBER
;
21221 rettv
->vval
.v_number
= -1;
21224 #ifdef FEAT_PROFILE
21225 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
21226 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)))
21228 profile_end(&call_start
);
21229 profile_sub_wait(&wait_start
, &call_start
);
21230 profile_add(&fp
->uf_tm_total
, &call_start
);
21231 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
21232 if (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)
21234 profile_add(&fc
.caller
->func
->uf_tm_children
, &call_start
);
21235 profile_add(&fc
.caller
->func
->uf_tml_children
, &call_start
);
21240 /* when being verbose, mention the return value */
21241 if (p_verbose
>= 12)
21244 verbose_enter_scroll();
21247 smsg((char_u
*)_("%s aborted"), sourcing_name
);
21248 else if (fc
.rettv
->v_type
== VAR_NUMBER
)
21249 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
21250 (long)fc
.rettv
->vval
.v_number
);
21253 char_u buf
[MSG_BUF_LEN
];
21254 char_u numbuf2
[NUMBUFLEN
];
21258 /* The value may be very long. Skip the middle part, so that we
21259 * have some idea how it starts and ends. smsg() would always
21260 * truncate it at the end. */
21261 s
= tv2string(fc
.rettv
, &tofree
, numbuf2
, 0);
21264 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21265 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
21269 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21271 verbose_leave_scroll();
21275 vim_free(sourcing_name
);
21276 sourcing_name
= save_sourcing_name
;
21277 sourcing_lnum
= save_sourcing_lnum
;
21278 current_SID
= save_current_SID
;
21279 #ifdef FEAT_PROFILE
21280 if (do_profiling
== PROF_YES
)
21281 script_prof_restore(&wait_start
);
21284 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
21287 verbose_enter_scroll();
21289 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
21290 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21292 verbose_leave_scroll();
21296 did_emsg
|= save_did_emsg
;
21297 current_funccal
= fc
.caller
;
21299 /* The a: variables typevals were not allocated, only free the allocated
21301 vars_clear_ext(&fc
.l_avars
.dv_hashtab
, FALSE
);
21303 vars_clear(&fc
.l_vars
.dv_hashtab
); /* free all l: variables */
21308 * Add a number variable "name" to dict "dp" with value "nr".
21311 add_nr_var(dp
, v
, name
, nr
)
21317 STRCPY(v
->di_key
, name
);
21318 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21319 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
21320 v
->di_tv
.v_type
= VAR_NUMBER
;
21321 v
->di_tv
.v_lock
= VAR_FIXED
;
21322 v
->di_tv
.vval
.v_number
= nr
;
21332 char_u
*arg
= eap
->arg
;
21334 int returning
= FALSE
;
21336 if (current_funccal
== NULL
)
21338 EMSG(_("E133: :return not inside a function"));
21345 eap
->nextcmd
= NULL
;
21346 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
21347 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
21350 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
21354 /* It's safer to return also on error. */
21355 else if (!eap
->skip
)
21358 * Return unless the expression evaluation has been cancelled due to an
21359 * aborting error, an interrupt, or an exception.
21362 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
21365 /* When skipping or the return gets pending, advance to the next command
21366 * in this line (!returning). Otherwise, ignore the rest of the line.
21367 * Following lines will be ignored by get_func_line(). */
21369 eap
->nextcmd
= NULL
;
21370 else if (eap
->nextcmd
== NULL
) /* no argument */
21371 eap
->nextcmd
= check_nextcmd(arg
);
21378 * Return from a function. Possibly makes the return pending. Also called
21379 * for a pending return at the ":endtry" or after returning from an extra
21380 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
21381 * when called due to a ":return" command. "rettv" may point to a typval_T
21382 * with the return rettv. Returns TRUE when the return can be carried out,
21383 * FALSE when the return gets pending.
21386 do_return(eap
, reanimate
, is_cmd
, rettv
)
21393 struct condstack
*cstack
= eap
->cstack
;
21396 /* Undo the return. */
21397 current_funccal
->returned
= FALSE
;
21400 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21401 * not in its finally clause (which then is to be executed next) is found.
21402 * In this case, make the ":return" pending for execution at the ":endtry".
21403 * Otherwise, return normally.
21405 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
21408 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
21410 if (!is_cmd
&& !reanimate
)
21411 /* A pending return again gets pending. "rettv" points to an
21412 * allocated variable with the rettv of the original ":return"'s
21413 * argument if present or is NULL else. */
21414 cstack
->cs_rettv
[idx
] = rettv
;
21417 /* When undoing a return in order to make it pending, get the stored
21420 rettv
= current_funccal
->rettv
;
21424 /* Store the value of the pending return. */
21425 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
21426 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
21428 EMSG(_(e_outofmem
));
21431 cstack
->cs_rettv
[idx
] = NULL
;
21435 /* The pending return value could be overwritten by a ":return"
21436 * without argument in a finally clause; reset the default
21438 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
21439 current_funccal
->rettv
->vval
.v_number
= 0;
21442 report_make_pending(CSTP_RETURN
, rettv
);
21446 current_funccal
->returned
= TRUE
;
21448 /* If the return is carried out now, store the return value. For
21449 * a return immediately after reanimation, the value is already
21451 if (!reanimate
&& rettv
!= NULL
)
21453 clear_tv(current_funccal
->rettv
);
21454 *current_funccal
->rettv
= *(typval_T
*)rettv
;
21464 * Free the variable with a pending return value.
21467 discard_pending_return(rettv
)
21470 free_tv((typval_T
*)rettv
);
21474 * Generate a return command for producing the value of "rettv". The result
21475 * is an allocated string. Used by report_pending() for verbose messages.
21478 get_return_cmd(rettv
)
21482 char_u
*tofree
= NULL
;
21483 char_u numbuf
[NUMBUFLEN
];
21486 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
21490 STRCPY(IObuff
, ":return ");
21491 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
21492 if (STRLEN(s
) + 8 >= IOSIZE
)
21493 STRCPY(IObuff
+ IOSIZE
- 4, "...");
21495 return vim_strsave(IObuff
);
21499 * Get next function line.
21500 * Called by do_cmdline() to get the next line.
21501 * Returns allocated string, or NULL for end of function.
21505 get_func_line(c
, cookie
, indent
)
21506 int c
; /* not used */
21508 int indent
; /* not used */
21510 funccall_T
*fcp
= (funccall_T
*)cookie
;
21511 ufunc_T
*fp
= fcp
->func
;
21513 garray_T
*gap
; /* growarray with function lines */
21515 /* If breakpoints have been added/deleted need to check for it. */
21516 if (fcp
->dbg_tick
!= debug_tick
)
21518 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21520 fcp
->dbg_tick
= debug_tick
;
21522 #ifdef FEAT_PROFILE
21523 if (do_profiling
== PROF_YES
)
21524 func_line_end(cookie
);
21527 gap
= &fp
->uf_lines
;
21528 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21533 /* Skip NULL lines (continuation lines). */
21534 while (fcp
->linenr
< gap
->ga_len
21535 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
21537 if (fcp
->linenr
>= gap
->ga_len
)
21541 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
21542 sourcing_lnum
= fcp
->linenr
;
21543 #ifdef FEAT_PROFILE
21544 if (do_profiling
== PROF_YES
)
21545 func_line_start(cookie
);
21550 /* Did we encounter a breakpoint? */
21551 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
21553 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
21554 /* Find next breakpoint. */
21555 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21557 fcp
->dbg_tick
= debug_tick
;
21563 #if defined(FEAT_PROFILE) || defined(PROTO)
21565 * Called when starting to read a function line.
21566 * "sourcing_lnum" must be correct!
21567 * When skipping lines it may not actually be executed, but we won't find out
21568 * until later and we need to store the time now.
21571 func_line_start(cookie
)
21574 funccall_T
*fcp
= (funccall_T
*)cookie
;
21575 ufunc_T
*fp
= fcp
->func
;
21577 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
21578 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
21580 fp
->uf_tml_idx
= sourcing_lnum
- 1;
21581 /* Skip continuation lines. */
21582 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
21584 fp
->uf_tml_execed
= FALSE
;
21585 profile_start(&fp
->uf_tml_start
);
21586 profile_zero(&fp
->uf_tml_children
);
21587 profile_get_wait(&fp
->uf_tml_wait
);
21592 * Called when actually executing a function line.
21595 func_line_exec(cookie
)
21598 funccall_T
*fcp
= (funccall_T
*)cookie
;
21599 ufunc_T
*fp
= fcp
->func
;
21601 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21602 fp
->uf_tml_execed
= TRUE
;
21606 * Called when done with a function line.
21609 func_line_end(cookie
)
21612 funccall_T
*fcp
= (funccall_T
*)cookie
;
21613 ufunc_T
*fp
= fcp
->func
;
21615 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21617 if (fp
->uf_tml_execed
)
21619 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
21620 profile_end(&fp
->uf_tml_start
);
21621 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
21622 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
21623 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
21624 &fp
->uf_tml_children
);
21626 fp
->uf_tml_idx
= -1;
21632 * Return TRUE if the currently active function should be ended, because a
21633 * return was encountered or an error occurred. Used inside a ":while".
21636 func_has_ended(cookie
)
21639 funccall_T
*fcp
= (funccall_T
*)cookie
;
21641 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21642 * an error inside a try conditional. */
21643 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21648 * return TRUE if cookie indicates a function which "abort"s on errors.
21651 func_has_abort(cookie
)
21654 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
21657 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21660 VAR_FLAVOUR_DEFAULT
, /* doesn't start with uppercase */
21661 VAR_FLAVOUR_SESSION
, /* starts with uppercase, some lower */
21662 VAR_FLAVOUR_VIMINFO
/* all uppercase */
21665 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
21667 static var_flavour_T
21668 var_flavour(varname
)
21671 char_u
*p
= varname
;
21673 if (ASCII_ISUPPER(*p
))
21676 if (ASCII_ISLOWER(*p
))
21677 return VAR_FLAVOUR_SESSION
;
21678 return VAR_FLAVOUR_VIMINFO
;
21681 return VAR_FLAVOUR_DEFAULT
;
21685 #if defined(FEAT_VIMINFO) || defined(PROTO)
21687 * Restore global vars that start with a capital from the viminfo file
21690 read_viminfo_varlist(virp
, writing
)
21695 int type
= VAR_NUMBER
;
21698 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
21700 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
21703 *tab
++ = '\0'; /* isolate the variable name */
21704 if (*tab
== 'S') /* string var */
21707 else if (*tab
== 'F')
21711 tab
= vim_strchr(tab
, '\t');
21715 if (type
== VAR_STRING
)
21716 tv
.vval
.v_string
= viminfo_readstring(virp
,
21717 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
21719 else if (type
== VAR_FLOAT
)
21720 (void)string2float(tab
+ 1, &tv
.vval
.v_float
);
21723 tv
.vval
.v_number
= atol((char *)tab
+ 1);
21724 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
21725 if (type
== VAR_STRING
)
21726 vim_free(tv
.vval
.v_string
);
21731 return viminfo_readline(virp
);
21735 * Write global vars that start with a capital to the viminfo file
21738 write_viminfo_varlist(fp
)
21742 dictitem_T
*this_var
;
21747 char_u numbuf
[NUMBUFLEN
];
21749 if (find_viminfo_parameter('!') == NULL
)
21752 fprintf(fp
, _("\n# global variables:\n"));
21754 todo
= (int)globvarht
.ht_used
;
21755 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21757 if (!HASHITEM_EMPTY(hi
))
21760 this_var
= HI2DI(hi
);
21761 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
21763 switch (this_var
->di_tv
.v_type
)
21765 case VAR_STRING
: s
= "STR"; break;
21766 case VAR_NUMBER
: s
= "NUM"; break;
21768 case VAR_FLOAT
: s
= "FLO"; break;
21772 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
21773 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
21775 viminfo_writestring(fp
, p
);
21783 #if defined(FEAT_SESSION) || defined(PROTO)
21785 store_session_globals(fd
)
21789 dictitem_T
*this_var
;
21793 todo
= (int)globvarht
.ht_used
;
21794 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21796 if (!HASHITEM_EMPTY(hi
))
21799 this_var
= HI2DI(hi
);
21800 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
21801 || this_var
->di_tv
.v_type
== VAR_STRING
)
21802 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
21804 /* Escape special characters with a backslash. Turn a LF and
21805 * CR into \n and \r. */
21806 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
21807 (char_u
*)"\\\"\n\r");
21808 if (p
== NULL
) /* out of memory */
21810 for (t
= p
; *t
!= NUL
; ++t
)
21813 else if (*t
== '\r')
21815 if ((fprintf(fd
, "let %s = %c%s%c",
21817 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
21820 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
21822 || put_eol(fd
) == FAIL
)
21830 else if (this_var
->di_tv
.v_type
== VAR_FLOAT
21831 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
21833 float_T f
= this_var
->di_tv
.vval
.v_float
;
21841 if ((fprintf(fd
, "let %s = %c&%f",
21842 this_var
->di_key
, sign
, f
) < 0)
21843 || put_eol(fd
) == FAIL
)
21854 * Display script name where an item was last set.
21855 * Should only be invoked when 'verbose' is non-zero.
21858 last_set_msg(scriptID
)
21865 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
21869 MSG_PUTS(_("\n\tLast set from "));
21877 #endif /* FEAT_EVAL */
21880 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21884 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21886 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
21887 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
21888 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
21891 * Get the short path (8.3) for the filename in "fnamep".
21892 * Only works for a valid file name.
21893 * When the path gets longer "fnamep" is changed and the allocated buffer
21894 * is put in "bufp".
21895 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21896 * Returns OK on success, FAIL on failure.
21899 get_short_pathname(fnamep
, bufp
, fnamelen
)
21908 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
21911 /* If that doesn't work (not enough space), then save the string
21912 * and try again with a new buffer big enough. */
21913 newbuf
= vim_strnsave(*fnamep
, l
);
21914 if (newbuf
== NULL
)
21918 *fnamep
= *bufp
= newbuf
;
21920 /* Really should always succeed, as the buffer is big enough. */
21921 l
= GetShortPathName(*fnamep
, *fnamep
, l
+1);
21929 * Get the short path (8.3) for the filename in "fname". The converted
21930 * path is returned in "bufp".
21932 * Some of the directories specified in "fname" may not exist. This function
21933 * will shorten the existing directories at the beginning of the path and then
21934 * append the remaining non-existing path.
21936 * fname - Pointer to the filename to shorten. On return, contains the
21937 * pointer to the shortened pathname
21938 * bufp - Pointer to an allocated buffer for the filename.
21939 * fnamelen - Length of the filename pointed to by fname
21941 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
21944 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
21949 char_u
*short_fname
, *save_fname
, *pbuf_unused
;
21950 char_u
*endp
, *save_endp
;
21953 int new_len
, sfx_len
;
21957 old_len
= *fnamelen
;
21958 save_fname
= vim_strnsave(*fname
, old_len
);
21959 pbuf_unused
= NULL
;
21960 short_fname
= NULL
;
21962 endp
= save_fname
+ old_len
- 1; /* Find the end of the copy */
21966 * Try shortening the supplied path till it succeeds by removing one
21967 * directory at a time from the tail of the path.
21972 /* go back one path-separator */
21973 while (endp
> save_fname
&& !after_pathsep(save_fname
, endp
+ 1))
21975 if (endp
<= save_fname
)
21976 break; /* processed the complete path */
21979 * Replace the path separator with a NUL and try to shorten the
21984 short_fname
= save_fname
;
21985 len
= STRLEN(short_fname
) + 1;
21986 if (get_short_pathname(&short_fname
, &pbuf_unused
, &len
) == FAIL
)
21991 *endp
= ch
; /* preserve the string */
21994 break; /* successfully shortened the path */
21996 /* failed to shorten the path. Skip the path separator */
22003 * Succeeded in shortening the path. Now concatenate the shortened
22004 * path with the remaining path at the tail.
22007 /* Compute the length of the new path. */
22008 sfx_len
= (int)(save_endp
- endp
) + 1;
22009 new_len
= len
+ sfx_len
;
22011 *fnamelen
= new_len
;
22013 if (new_len
> old_len
)
22015 /* There is not enough space in the currently allocated string,
22016 * copy it to a buffer big enough. */
22017 *fname
= *bufp
= vim_strnsave(short_fname
, new_len
);
22018 if (*fname
== NULL
)
22026 /* Transfer short_fname to the main buffer (it's big enough),
22027 * unless get_short_pathname() did its work in-place. */
22028 *fname
= *bufp
= save_fname
;
22029 if (short_fname
!= save_fname
)
22030 vim_strncpy(save_fname
, short_fname
, len
);
22034 /* concat the not-shortened part of the path */
22035 vim_strncpy(*fname
+ len
, endp
, sfx_len
);
22036 (*fname
)[new_len
] = NUL
;
22040 vim_free(pbuf_unused
);
22041 vim_free(save_fname
);
22047 * Get a pathname for a partial path.
22048 * Returns OK for success, FAIL for failure.
22051 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
22056 int sepcount
, len
, tflen
;
22058 char_u
*pbuf
, *tfname
;
22061 /* Count up the path separators from the RHS.. so we know which part
22062 * of the path to return. */
22064 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
22065 if (vim_ispathsep(*p
))
22068 /* Need full path first (use expand_env() to remove a "~/") */
22069 hasTilde
= (**fnamep
== '~');
22071 pbuf
= tfname
= expand_env_save(*fnamep
);
22073 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
22075 len
= tflen
= (int)STRLEN(tfname
);
22077 if (get_short_pathname(&tfname
, &pbuf
, &len
) == FAIL
)
22082 /* Don't have a valid filename, so shorten the rest of the
22083 * path if we can. This CAN give us invalid 8.3 filenames, but
22084 * there's not a lot of point in guessing what it might be.
22087 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == FAIL
)
22091 /* Count the paths backward to find the beginning of the desired string. */
22092 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
22096 p
-= mb_head_off(tfname
, p
);
22098 if (vim_ispathsep(*p
))
22100 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
22117 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22119 *fnamelen
= (int)STRLEN(p
);
22125 #endif /* WIN3264 */
22128 * Adjust a filename, according to a string of modifiers.
22129 * *fnamep must be NUL terminated when called. When returning, the length is
22130 * determined by *fnamelen.
22131 * Returns VALID_ flags or -1 for failure.
22132 * When there is an error, *fnamep is set to NULL.
22135 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
22136 char_u
*src
; /* string with modifiers */
22137 int *usedlen
; /* characters after src that are used */
22138 char_u
**fnamep
; /* file name so far */
22139 char_u
**bufp
; /* buffer for allocated file name or NULL */
22140 int *fnamelen
; /* length of fnamep */
22144 char_u
*s
, *p
, *pbuf
;
22145 char_u dirname
[MAXPATHL
];
22147 int has_fullname
= 0;
22149 int has_shortname
= 0;
22153 /* ":p" - full path/file_name */
22154 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
22158 valid
|= VALID_PATH
;
22161 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22162 if ((*fnamep
)[0] == '~'
22163 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22164 && ((*fnamep
)[1] == '/'
22165 # ifdef BACKSLASH_IN_FILENAME
22166 || (*fnamep
)[1] == '\\'
22168 || (*fnamep
)[1] == NUL
)
22173 *fnamep
= expand_env_save(*fnamep
);
22174 vim_free(*bufp
); /* free any allocated file name */
22176 if (*fnamep
== NULL
)
22180 /* When "/." or "/.." is used: force expansion to get rid of it. */
22181 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
22183 if (vim_ispathsep(*p
)
22186 || vim_ispathsep(p
[2])
22188 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
22192 /* FullName_save() is slow, don't use it when not needed. */
22193 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
22195 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
22196 vim_free(*bufp
); /* free any allocated file name */
22198 if (*fnamep
== NULL
)
22202 /* Append a path separator to a directory. */
22203 if (mch_isdir(*fnamep
))
22205 /* Make room for one or two extra characters. */
22206 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
22207 vim_free(*bufp
); /* free any allocated file name */
22209 if (*fnamep
== NULL
)
22211 add_pathsep(*fnamep
);
22215 /* ":." - path relative to the current directory */
22216 /* ":~" - path relative to the home directory */
22217 /* ":8" - shortname path - postponed till after */
22218 while (src
[*usedlen
] == ':'
22219 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
22225 has_shortname
= 1; /* Postpone this. */
22230 /* Need full path first (use expand_env() to remove a "~/") */
22233 if (c
== '.' && **fnamep
== '~')
22234 p
= pbuf
= expand_env_save(*fnamep
);
22236 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
22247 mch_dirname(dirname
, MAXPATHL
);
22248 s
= shorten_fname(p
, dirname
);
22254 vim_free(*bufp
); /* free any allocated file name */
22262 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
22263 /* Only replace it when it starts with '~' */
22264 if (*dirname
== '~')
22266 s
= vim_strsave(dirname
);
22279 tail
= gettail(*fnamep
);
22280 *fnamelen
= (int)STRLEN(*fnamep
);
22282 /* ":h" - head, remove "/file_name", can be repeated */
22283 /* Don't remove the first "/" or "c:\" */
22284 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
22286 valid
|= VALID_HEAD
;
22288 s
= get_past_head(*fnamep
);
22289 while (tail
> s
&& after_pathsep(s
, tail
))
22290 mb_ptr_back(*fnamep
, tail
);
22291 *fnamelen
= (int)(tail
- *fnamep
);
22294 *fnamelen
+= 1; /* the path separator is part of the path */
22296 if (*fnamelen
== 0)
22298 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22299 p
= vim_strsave((char_u
*)".");
22303 *bufp
= *fnamep
= tail
= p
;
22308 while (tail
> s
&& !after_pathsep(s
, tail
))
22309 mb_ptr_back(*fnamep
, tail
);
22313 /* ":8" - shortname */
22314 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
22323 /* Check shortname after we have done 'heads' and before we do 'tails'
22328 /* Copy the string if it is shortened by :h */
22329 if (*fnamelen
< (int)STRLEN(*fnamep
))
22331 p
= vim_strnsave(*fnamep
, *fnamelen
);
22335 *bufp
= *fnamep
= p
;
22338 /* Split into two implementations - makes it easier. First is where
22339 * there isn't a full name already, second is where there is.
22341 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
22343 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == FAIL
)
22350 /* Simple case, already have the full-name
22351 * Nearly always shorter, so try first time. */
22353 if (get_short_pathname(fnamep
, bufp
, &l
) == FAIL
)
22358 /* Couldn't find the filename.. search the paths.
22361 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == FAIL
)
22367 #endif /* WIN3264 */
22369 /* ":t" - tail, just the basename */
22370 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
22373 *fnamelen
-= (int)(tail
- *fnamep
);
22377 /* ":e" - extension, can be repeated */
22378 /* ":r" - root, without extension, can be repeated */
22379 while (src
[*usedlen
] == ':'
22380 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
22382 /* find a '.' in the tail:
22383 * - for second :e: before the current fname
22384 * - otherwise: The last '.'
22386 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
22389 s
= *fnamep
+ *fnamelen
- 1;
22390 for ( ; s
> tail
; --s
)
22393 if (src
[*usedlen
+ 1] == 'e') /* :e */
22397 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
22400 /* cut version from the extension */
22401 s
= *fnamep
+ *fnamelen
- 1;
22402 for ( ; s
> *fnamep
; --s
)
22406 *fnamelen
= s
- *fnamep
;
22409 else if (*fnamep
<= tail
)
22414 if (s
> tail
) /* remove one extension */
22415 *fnamelen
= (int)(s
- *fnamep
);
22420 /* ":s?pat?foo?" - substitute */
22421 /* ":gs?pat?foo?" - global substitute */
22422 if (src
[*usedlen
] == ':'
22423 && (src
[*usedlen
+ 1] == 's'
22424 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
22433 flags
= (char_u
*)"";
22434 s
= src
+ *usedlen
+ 2;
22435 if (src
[*usedlen
+ 1] == 'g')
22437 flags
= (char_u
*)"g";
22444 /* find end of pattern */
22445 p
= vim_strchr(s
, sep
);
22448 pat
= vim_strnsave(s
, (int)(p
- s
));
22452 /* find end of substitution */
22453 p
= vim_strchr(s
, sep
);
22456 sub
= vim_strnsave(s
, (int)(p
- s
));
22457 str
= vim_strnsave(*fnamep
, *fnamelen
);
22458 if (sub
!= NULL
&& str
!= NULL
)
22460 *usedlen
= (int)(p
+ 1 - src
);
22461 s
= do_string_sub(str
, pat
, sub
, flags
);
22465 *fnamelen
= (int)STRLEN(s
);
22477 /* after using ":s", repeat all the modifiers */
22487 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22488 * "flags" can be "g" to do a global substitute.
22489 * Returns an allocated string, NULL for error.
22492 do_string_sub(str
, pat
, sub
, flags
)
22499 regmatch_T regmatch
;
22507 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22509 p_cpo
= (char_u
*)"";
22511 ga_init2(&ga
, 1, 200);
22513 do_all
= (flags
[0] == 'g');
22515 regmatch
.rm_ic
= p_ic
;
22516 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
22517 if (regmatch
.regprog
!= NULL
)
22520 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
22523 * Get some space for a temporary buffer to do the substitution
22524 * into. It will contain:
22525 * - The text up to where the match is.
22526 * - The substituted text.
22527 * - The text after the match.
22529 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
22530 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
22531 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
22537 /* copy the text up to where the match is */
22538 i
= (int)(regmatch
.startp
[0] - tail
);
22539 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
22540 /* add the substituted text */
22541 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
22542 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
22543 ga
.ga_len
+= i
+ sublen
- 1;
22544 /* avoid getting stuck on a match with an empty string */
22545 if (tail
== regmatch
.endp
[0])
22549 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
22554 tail
= regmatch
.endp
[0];
22562 if (ga
.ga_data
!= NULL
)
22563 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
22565 vim_free(regmatch
.regprog
);
22568 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
22575 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */