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 */
20 # include <time.h> /* for strftime() */
24 # include <time.h> /* for time_t */
31 #if defined(FEAT_EVAL) || defined(PROTO)
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");
112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
115 static dict_T globvardict
;
116 static dictitem_T globvars_var
;
117 #define globvarht globvardict.dv_hashtab
120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
123 static hashtab_T compat_hashtab
;
126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
129 static int current_copyID
= 0;
132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
141 static garray_T ga_scripts
= {0, 0, sizeof(scriptvar_T
), 4, NULL
};
142 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
145 static int echo_attr
= 0; /* attributes used for ":echo" */
147 /* Values for trans_function_name() argument: */
148 #define TFN_INT 1 /* internal function name OK */
149 #define TFN_QUIET 2 /* no error messages */
152 * Structure to hold info for a user function.
154 typedef struct ufunc ufunc_T
;
158 int uf_varargs
; /* variable nr of arguments */
160 int uf_calls
; /* nr of active calls */
161 garray_T uf_args
; /* arguments */
162 garray_T uf_lines
; /* function lines */
164 int uf_profiling
; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count
; /* nr of calls */
167 proftime_T uf_tm_total
; /* time spend in function + children */
168 proftime_T uf_tm_self
; /* time spend in function itself */
169 proftime_T uf_tm_children
; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count
; /* nr of times line was executed */
172 proftime_T
*uf_tml_total
; /* time spend in a line + children */
173 proftime_T
*uf_tml_self
; /* time spend in a line itself */
174 proftime_T uf_tml_start
; /* start time for current line */
175 proftime_T uf_tml_children
; /* time spent in children for this line */
176 proftime_T uf_tml_wait
; /* start wait time for current line */
177 int uf_tml_idx
; /* index of line being timed; -1 if none */
178 int uf_tml_execed
; /* line being timed was executed */
180 scid_T uf_script_ID
; /* ID of script where function was defined,
181 used for s: variables */
182 int uf_refcount
; /* for numbered function: reference count */
183 char_u uf_name
[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
189 #define FC_ABORT 1 /* abort function on error */
190 #define FC_RANGE 2 /* function accepts range */
191 #define FC_DICT 4 /* Dict function, uses "self" */
194 * All user-defined functions are found in this hashtable.
196 static hashtab_T func_hashtab
;
198 /* The names of packages that once were loaded are remembered. */
199 static garray_T ga_loaded
= {0, 0, sizeof(char_u
*), 4, NULL
};
201 /* list heads for garbage collection */
202 static dict_T
*first_dict
= NULL
; /* list of all dicts */
203 static list_T
*first_list
= NULL
; /* list of all lists */
205 /* From user function to hashitem and back. */
206 static ufunc_T dumuf
;
207 #define UF2HIKEY(fp) ((fp)->uf_name)
208 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
211 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
214 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215 #define VAR_SHORT_LEN 20 /* short variable name length */
216 #define FIXVAR_CNT 12 /* number of fixed variables */
218 /* structure to hold info for a function that is currently being executed. */
219 typedef struct funccall_S funccall_T
;
223 ufunc_T
*func
; /* function being called */
224 int linenr
; /* next line to be executed */
225 int returned
; /* ":return" used */
226 struct /* fixed variables for arguments */
228 dictitem_T var
; /* variable (without room for name) */
229 char_u room
[VAR_SHORT_LEN
]; /* room for the name */
230 } fixvar
[FIXVAR_CNT
];
231 dict_T l_vars
; /* l: local function variables */
232 dictitem_T l_vars_var
; /* variable for l: scope */
233 dict_T l_avars
; /* a: argument variables */
234 dictitem_T l_avars_var
; /* variable for a: scope */
235 list_T l_varlist
; /* list for a:000 */
236 listitem_T l_listitems
[MAX_FUNC_ARGS
]; /* listitems for a:000 */
237 typval_T
*rettv
; /* return value */
238 linenr_T breakpoint
; /* next line with breakpoint or zero */
239 int dbg_tick
; /* debug_tick when breakpoint was set */
240 int level
; /* top nesting level of executed function */
242 proftime_T prof_child
; /* time spent in a child */
244 funccall_T
*caller
; /* calling function or NULL */
248 * Info used by a ":for" loop.
252 int fi_semicolon
; /* TRUE if ending in '; var]' */
253 int fi_varcount
; /* nr of variables in the list */
254 listwatch_T fi_lw
; /* keep an eye on the item used. */
255 list_T
*fi_list
; /* list being used */
259 * Struct used by trans_function_name()
263 dict_T
*fd_dict
; /* Dictionary used */
264 char_u
*fd_newkey
; /* new key in "dict" in allocated memory */
265 dictitem_T
*fd_di
; /* Dictionary item used */
270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
277 /* values for vv_flags: */
278 #define VV_COMPAT 1 /* compatible, also used without "v:" */
279 #define VV_RO 2 /* read-only */
280 #define VV_RO_SBX 4 /* read-only in the sandbox */
282 #define VV_NAME(s, t) s, {{t}}, {0}
286 char *vv_name
; /* name of variable, without v: */
287 dictitem_T vv_di
; /* value and name for key */
288 char vv_filler
[16]; /* space for LONGEST name below!!! */
289 char vv_flags
; /* VV_COMPAT, VV_RO, VV_RO_SBX */
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
296 {VV_NAME("count", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
297 {VV_NAME("count1", VAR_NUMBER
), VV_RO
},
298 {VV_NAME("prevcount", VAR_NUMBER
), VV_RO
},
299 {VV_NAME("errmsg", VAR_STRING
), VV_COMPAT
},
300 {VV_NAME("warningmsg", VAR_STRING
), 0},
301 {VV_NAME("statusmsg", VAR_STRING
), 0},
302 {VV_NAME("shell_error", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
303 {VV_NAME("this_session", VAR_STRING
), VV_COMPAT
},
304 {VV_NAME("version", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
305 {VV_NAME("lnum", VAR_NUMBER
), VV_RO_SBX
},
306 {VV_NAME("termresponse", VAR_STRING
), VV_RO
},
307 {VV_NAME("fname", VAR_STRING
), VV_RO
},
308 {VV_NAME("lang", VAR_STRING
), VV_RO
},
309 {VV_NAME("lc_time", VAR_STRING
), VV_RO
},
310 {VV_NAME("ctype", VAR_STRING
), VV_RO
},
311 {VV_NAME("charconvert_from", VAR_STRING
), VV_RO
},
312 {VV_NAME("charconvert_to", VAR_STRING
), VV_RO
},
313 {VV_NAME("fname_in", VAR_STRING
), VV_RO
},
314 {VV_NAME("fname_out", VAR_STRING
), VV_RO
},
315 {VV_NAME("fname_new", VAR_STRING
), VV_RO
},
316 {VV_NAME("fname_diff", VAR_STRING
), VV_RO
},
317 {VV_NAME("cmdarg", VAR_STRING
), VV_RO
},
318 {VV_NAME("foldstart", VAR_NUMBER
), VV_RO_SBX
},
319 {VV_NAME("foldend", VAR_NUMBER
), VV_RO_SBX
},
320 {VV_NAME("folddashes", VAR_STRING
), VV_RO_SBX
},
321 {VV_NAME("foldlevel", VAR_NUMBER
), VV_RO_SBX
},
322 {VV_NAME("progname", VAR_STRING
), VV_RO
},
323 {VV_NAME("servername", VAR_STRING
), VV_RO
},
324 {VV_NAME("dying", VAR_NUMBER
), VV_RO
},
325 {VV_NAME("exception", VAR_STRING
), VV_RO
},
326 {VV_NAME("throwpoint", VAR_STRING
), VV_RO
},
327 {VV_NAME("register", VAR_STRING
), VV_RO
},
328 {VV_NAME("cmdbang", VAR_NUMBER
), VV_RO
},
329 {VV_NAME("insertmode", VAR_STRING
), VV_RO
},
330 {VV_NAME("val", VAR_UNKNOWN
), VV_RO
},
331 {VV_NAME("key", VAR_UNKNOWN
), VV_RO
},
332 {VV_NAME("profiling", VAR_NUMBER
), VV_RO
},
333 {VV_NAME("fcs_reason", VAR_STRING
), VV_RO
},
334 {VV_NAME("fcs_choice", VAR_STRING
), 0},
335 {VV_NAME("beval_bufnr", VAR_NUMBER
), VV_RO
},
336 {VV_NAME("beval_winnr", VAR_NUMBER
), VV_RO
},
337 {VV_NAME("beval_lnum", VAR_NUMBER
), VV_RO
},
338 {VV_NAME("beval_col", VAR_NUMBER
), VV_RO
},
339 {VV_NAME("beval_text", VAR_STRING
), VV_RO
},
340 {VV_NAME("scrollstart", VAR_STRING
), 0},
341 {VV_NAME("swapname", VAR_STRING
), VV_RO
},
342 {VV_NAME("swapchoice", VAR_STRING
), 0},
343 {VV_NAME("swapcommand", VAR_STRING
), VV_RO
},
344 {VV_NAME("char", VAR_STRING
), VV_RO
},
345 {VV_NAME("mouse_win", VAR_NUMBER
), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER
), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER
), 0},
351 #define vv_type vv_di.di_tv.v_type
352 #define vv_nr vv_di.di_tv.vval.v_number
353 #define vv_str vv_di.di_tv.vval.v_string
354 #define vv_tv vv_di.di_tv
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
360 static dict_T vimvardict
;
361 static dictitem_T vimvars_var
;
362 #define vimvarht vimvardict.dv_hashtab
364 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
365 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
366 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
369 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
370 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
371 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
372 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
));
373 static void list_glob_vars
__ARGS((void));
374 static void list_buf_vars
__ARGS((void));
375 static void list_win_vars
__ARGS((void));
377 static void list_tab_vars
__ARGS((void));
379 static void list_vim_vars
__ARGS((void));
380 static void list_script_vars
__ARGS((void));
381 static void list_func_vars
__ARGS((void));
382 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
));
383 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
384 static int check_changedtick
__ARGS((char_u
*arg
));
385 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
386 static void clear_lval
__ARGS((lval_T
*lp
));
387 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
388 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
389 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
390 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
391 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
392 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
393 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
394 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
395 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
396 static int tv_islocked
__ARGS((typval_T
*tv
));
398 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
399 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
400 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
401 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
402 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
403 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
404 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
405 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
407 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
408 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
409 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
410 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
411 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
412 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
413 static listitem_T
*listitem_alloc
__ARGS((void));
414 static void listitem_free
__ARGS((listitem_T
*item
));
415 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
416 static long list_len
__ARGS((list_T
*l
));
417 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
418 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
419 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
420 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
421 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
422 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
423 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
424 static int list_append_tv
__ARGS((list_T
*l
, typval_T
*tv
));
425 static int list_append_string
__ARGS((list_T
*l
, char_u
*str
, int len
));
426 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
427 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
428 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
429 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
430 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
431 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
432 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
433 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
434 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
435 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
436 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
437 static void dict_unref
__ARGS((dict_T
*d
));
438 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
439 static dictitem_T
*dictitem_alloc
__ARGS((char_u
*key
));
440 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
441 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
442 static void dictitem_free
__ARGS((dictitem_T
*item
));
443 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
444 static int dict_add
__ARGS((dict_T
*d
, dictitem_T
*item
));
445 static long dict_len
__ARGS((dict_T
*d
));
446 static dictitem_T
*dict_find
__ARGS((dict_T
*d
, char_u
*key
, int len
));
447 static char_u
*dict2string
__ARGS((typval_T
*tv
, int copyID
));
448 static int get_dict_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
449 static char_u
*echo_string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
450 static char_u
*tv2string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
451 static char_u
*string_quote
__ARGS((char_u
*str
, int function
));
452 static int get_env_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
453 static int find_internal_func
__ARGS((char_u
*name
));
454 static char_u
*deref_func_name
__ARGS((char_u
*name
, int *lenp
));
455 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
));
456 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
));
457 static void emsg_funcname
__ARGS((char *ermsg
, char_u
*name
));
459 static void f_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
460 static void f_append
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
461 static void f_argc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
462 static void f_argidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
463 static void f_argv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
464 static void f_browse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
465 static void f_browsedir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
466 static void f_bufexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
467 static void f_buflisted
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
468 static void f_bufloaded
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
469 static void f_bufname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
470 static void f_bufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
471 static void f_bufwinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
472 static void f_byte2line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
473 static void f_byteidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
474 static void f_call
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
475 static void f_changenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
476 static void f_char2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
477 static void f_cindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
478 static void f_col
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
479 #if defined(FEAT_INS_EXPAND)
480 static void f_complete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
481 static void f_complete_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
482 static void f_complete_check
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
484 static void f_confirm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
485 static void f_copy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
486 static void f_count
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
487 static void f_cscope_connection
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
488 static void f_cursor
__ARGS((typval_T
*argsvars
, typval_T
*rettv
));
489 static void f_deepcopy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
490 static void f_delete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
491 static void f_did_filetype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
492 static void f_diff_filler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
493 static void f_diff_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
494 static void f_empty
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
495 static void f_escape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
496 static void f_eval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
497 static void f_eventhandler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
498 static void f_executable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
499 static void f_exists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
500 static void f_expand
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
501 static void f_extend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
502 static void f_feedkeys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
503 static void f_filereadable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
504 static void f_filewritable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
505 static void f_filter
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
506 static void f_finddir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
507 static void f_findfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
508 static void f_fnamemodify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
509 static void f_foldclosed
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
510 static void f_foldclosedend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
511 static void f_foldlevel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
512 static void f_foldtext
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
513 static void f_foldtextresult
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
514 static void f_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
515 static void f_function
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
516 static void f_garbagecollect
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
517 static void f_get
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
518 static void f_getbufline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
519 static void f_getbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
520 static void f_getchar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
521 static void f_getcharmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
522 static void f_getcmdline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
523 static void f_getcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
524 static void f_getcmdtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
525 static void f_getcwd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
526 static void f_getfontname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
527 static void f_getfperm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
528 static void f_getfsize
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
529 static void f_getftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
530 static void f_getftype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
531 static void f_getline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
532 static void f_getpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
533 static void f_getqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
534 static void f_getreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
535 static void f_getregtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
536 static void f_gettabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
537 static void f_getwinposx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
538 static void f_getwinposy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
539 static void f_getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
540 static void f_glob
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
541 static void f_globpath
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
542 static void f_has
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
543 static void f_has_key
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
544 static void f_haslocaldir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
545 static void f_hasmapto
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
546 static void f_histadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
547 static void f_histdel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
548 static void f_histget
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
549 static void f_histnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
550 static void f_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
551 static void f_hlexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
552 static void f_hostname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
553 static void f_iconv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
554 static void f_indent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
555 static void f_index
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
556 static void f_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
557 static void f_inputdialog
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
558 static void f_inputlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
559 static void f_inputrestore
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
560 static void f_inputsave
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
561 static void f_inputsecret
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
562 static void f_insert
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
563 static void f_isdirectory
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
564 static void f_islocked
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
565 static void f_items
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
566 static void f_join
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
567 static void f_keys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
568 static void f_last_buffer_nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
569 static void f_len
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
570 static void f_libcall
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
571 static void f_libcallnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
572 static void f_line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
573 static void f_line2byte
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
574 static void f_lispindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
575 static void f_localtime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
576 static void f_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
577 static void f_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
578 static void f_mapcheck
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
579 static void f_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
580 static void f_matcharg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
581 static void f_matchend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
582 static void f_matchlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
583 static void f_matchstr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
584 static void f_max
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
585 static void f_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
587 static void f_mkdir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
589 static void f_mode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
590 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
591 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
592 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
593 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
594 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
595 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
596 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
597 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
598 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
599 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
600 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
601 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
602 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
603 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
604 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
605 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
606 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
607 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
608 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
609 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
610 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
611 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
612 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
613 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
614 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
615 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
616 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
617 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
618 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
619 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
620 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
621 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
622 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
623 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
624 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
625 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
626 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
627 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
628 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
629 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
630 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
631 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
632 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
633 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
635 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
637 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
638 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
639 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
640 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
641 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
642 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
643 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
644 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
645 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
647 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
648 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
650 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
652 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
654 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
656 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
665 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
667 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
668 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
669 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
670 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
672 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
674 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
675 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int lnum
, int *fnum
));
676 static int get_env_len
__ARGS((char_u
**arg
));
677 static int get_id_len
__ARGS((char_u
**arg
));
678 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
679 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
680 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
681 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
683 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
684 static int eval_isnamec
__ARGS((int c
));
685 static int eval_isnamec1
__ARGS((int c
));
686 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
687 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
688 static typval_T
*alloc_tv
__ARGS((void));
689 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
690 static void init_tv
__ARGS((typval_T
*varp
));
691 static long get_tv_number
__ARGS((typval_T
*varp
));
692 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
693 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
694 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
695 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
696 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
697 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
698 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
699 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
700 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
701 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
702 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
));
703 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
));
704 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
705 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
706 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
707 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
708 static void copy_tv
__ARGS((typval_T
*from
, typval_T
*to
));
709 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
710 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
711 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
712 static int eval_fname_script
__ARGS((char_u
*p
));
713 static int eval_fname_sid
__ARGS((char_u
*p
));
714 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
715 static ufunc_T
*find_func
__ARGS((char_u
*name
));
716 static int function_exists
__ARGS((char_u
*name
));
717 static int builtin_function
__ARGS((char_u
*name
));
719 static void func_do_profile
__ARGS((ufunc_T
*fp
));
720 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
721 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
726 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
731 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
733 static int script_autoload
__ARGS((char_u
*name
, int reload
));
734 static char_u
*autoload_name
__ARGS((char_u
*name
));
735 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
736 static void func_free
__ARGS((ufunc_T
*fp
));
737 static void func_unref
__ARGS((char_u
*name
));
738 static void func_ref
__ARGS((char_u
*name
));
739 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
));
740 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
741 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
742 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
743 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
744 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
745 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
747 /* Character used as separated in autoload function/variable names. */
748 #define AUTOLOAD_CHAR '#'
751 * Initialize the global and v: variables.
759 init_var_dict(&globvardict
, &globvars_var
);
760 init_var_dict(&vimvardict
, &vimvars_var
);
761 hash_init(&compat_hashtab
);
762 hash_init(&func_hashtab
);
764 for (i
= 0; i
< VV_LEN
; ++i
)
767 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
768 if (p
->vv_flags
& VV_RO
)
769 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
770 else if (p
->vv_flags
& VV_RO_SBX
)
771 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
773 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
775 /* add to v: scope dict, unless the value is not always available */
776 if (p
->vv_type
!= VAR_UNKNOWN
)
777 hash_add(&vimvarht
, p
->vv_di
.di_key
);
778 if (p
->vv_flags
& VV_COMPAT
)
779 /* add to compat scope dict */
780 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
784 #if defined(EXITFREE) || defined(PROTO)
791 for (i
= 0; i
< VV_LEN
; ++i
)
794 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
796 vim_free(p
->vv_di
.di_tv
.vval
.v_string
);
797 p
->vv_di
.di_tv
.vval
.v_string
= NULL
;
800 hash_clear(&vimvarht
);
801 hash_clear(&compat_hashtab
);
803 /* script-local variables */
804 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
805 vars_clear(&SCRIPT_VARS(i
));
806 ga_clear(&ga_scripts
);
809 /* global variables */
810 vars_clear(&globvarht
);
813 free_all_functions();
814 hash_clear(&func_hashtab
);
816 /* autoloaded script names */
817 ga_clear_strings(&ga_loaded
);
819 /* unreferenced lists and dicts */
820 (void)garbage_collect();
825 * Return the name of the executed function.
831 return ((funccall_T
*)cookie
)->func
->uf_name
;
835 * Return the address holding the next breakpoint line for a funccall cookie.
838 func_breakpoint(cookie
)
841 return &((funccall_T
*)cookie
)->breakpoint
;
845 * Return the address holding the debug tick for a funccall cookie.
848 func_dbg_tick(cookie
)
851 return &((funccall_T
*)cookie
)->dbg_tick
;
855 * Return the nesting level for a funccall cookie.
861 return ((funccall_T
*)cookie
)->level
;
864 /* pointer to funccal for currently active function */
865 funccall_T
*current_funccal
= NULL
;
868 * Return TRUE when a function was ended by a ":return" command.
871 current_func_returned()
873 return current_funccal
->returned
;
878 * Set an internal variable to a string value. Creates the variable if it does
882 set_internal_string_var(name
, value
)
889 val
= vim_strsave(value
);
892 tvp
= alloc_string_tv(val
);
895 set_var(name
, tvp
, FALSE
);
901 static lval_T
*redir_lval
= NULL
;
902 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
903 static char_u
*redir_endp
= NULL
;
904 static char_u
*redir_varname
= NULL
;
907 * Start recording command output to a variable
908 * Returns OK if successfully completed the setup. FAIL otherwise.
911 var_redir_start(name
, append
)
913 int append
; /* append to an existing variable */
919 /* Make sure a valid variable name is specified */
920 if (!eval_isnamec1(*name
))
926 redir_varname
= vim_strsave(name
);
927 if (redir_varname
== NULL
)
930 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
931 if (redir_lval
== NULL
)
937 /* The output is stored in growarray "redir_ga" until redirection ends. */
938 ga_init2(&redir_ga
, (int)sizeof(char), 500);
940 /* Parse the variable name (can be a dict or list entry). */
941 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
943 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
945 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
946 /* Trailing characters are present after the variable name */
954 /* check if we can write to the variable: set it to or append an empty
956 save_emsg
= did_emsg
;
958 tv
.v_type
= VAR_STRING
;
959 tv
.vval
.v_string
= (char_u
*)"";
961 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
963 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
965 did_emsg
|= save_emsg
;
971 if (redir_lval
->ll_newkey
!= NULL
)
973 /* Dictionary item was created, don't do it again. */
974 vim_free(redir_lval
->ll_newkey
);
975 redir_lval
->ll_newkey
= NULL
;
982 * Append "value[value_len]" to the variable set by var_redir_start().
983 * The actual appending is postponed until redirection ends, because the value
984 * appended may in fact be the string we write to, changing it may cause freed
991 var_redir_str(value
, value_len
)
997 if (redir_lval
== NULL
)
1000 if (value_len
== -1)
1001 len
= STRLEN(value
); /* Append the entire string */
1003 len
= value_len
; /* Append only "value_len" characters */
1005 if (ga_grow(&redir_ga
, (int)len
) == OK
)
1007 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1008 redir_ga
.ga_len
+= (int)len
;
1015 * Stop redirecting command output to a variable.
1022 if (redir_lval
!= NULL
)
1024 /* Append the trailing NUL. */
1025 ga_append(&redir_ga
, NUL
);
1027 /* Assign the text to the variable. */
1028 tv
.v_type
= VAR_STRING
;
1029 tv
.vval
.v_string
= redir_ga
.ga_data
;
1030 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1031 vim_free(tv
.vval
.v_string
);
1033 clear_lval(redir_lval
);
1034 vim_free(redir_lval
);
1037 vim_free(redir_varname
);
1038 redir_varname
= NULL
;
1041 # if defined(FEAT_MBYTE) || defined(PROTO)
1043 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1051 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1052 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1053 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1054 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1055 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1057 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1058 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1059 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1060 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1068 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1070 eval_printexpr(fname
, args
)
1076 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1077 set_vim_var_string(VV_CMDARG
, args
, -1);
1078 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1080 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1081 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1092 # if defined(FEAT_DIFF) || defined(PROTO)
1094 eval_diff(origfile
, newfile
, outfile
)
1101 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1102 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1103 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1104 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1105 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1106 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1107 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1111 eval_patch(origfile
, difffile
, outfile
)
1118 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1119 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1120 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1121 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1122 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1123 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1124 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1129 * Top level evaluation function, returning a boolean.
1130 * Sets "error" to TRUE if there was an error.
1131 * Return TRUE or FALSE.
1134 eval_to_bool(arg
, error
, nextcmd
, skip
)
1138 int skip
; /* only parse, don't execute */
1145 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1152 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1163 * Top level evaluation function, returning a string. If "skip" is TRUE,
1164 * only parsing to "nextcmd" is done, without reporting errors. Return
1165 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1168 eval_to_string_skip(arg
, nextcmd
, skip
)
1171 int skip
; /* only parse, don't execute */
1178 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1182 retval
= vim_strsave(get_tv_string(&tv
));
1192 * Skip over an expression at "*pp".
1193 * Return FAIL for an error, OK otherwise.
1201 *pp
= skipwhite(*pp
);
1202 return eval1(pp
, &rettv
, FALSE
);
1206 * Top level evaluation function, returning a string.
1207 * Return pointer to allocated memory, or NULL for failure.
1210 eval_to_string(arg
, nextcmd
, dolist
)
1213 int dolist
; /* turn List into sequence of lines */
1219 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1223 if (dolist
&& tv
.v_type
== VAR_LIST
)
1225 ga_init2(&ga
, (int)sizeof(char), 80);
1226 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1227 ga_append(&ga
, NUL
);
1228 retval
= (char_u
*)ga
.ga_data
;
1231 retval
= vim_strsave(get_tv_string(&tv
));
1239 * Call eval_to_string() without using current local variables and using
1240 * textlock. When "use_sandbox" is TRUE use the sandbox.
1243 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1249 void *save_funccalp
;
1251 save_funccalp
= save_funccal();
1255 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1259 restore_funccal(save_funccalp
);
1264 * Top level evaluation function, returning a number.
1265 * Evaluates "expr" silently.
1266 * Returns -1 for an error.
1269 eval_to_number(expr
)
1274 char_u
*p
= skipwhite(expr
);
1278 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1282 retval
= get_tv_number_chk(&rettv
, NULL
);
1291 * Prepare v: variable "idx" to be used.
1292 * Save the current typeval in "save_tv".
1293 * When not used yet add the variable to the v: hashtable.
1296 prepare_vimvar(idx
, save_tv
)
1300 *save_tv
= vimvars
[idx
].vv_tv
;
1301 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1302 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1306 * Restore v: variable "idx" to typeval "save_tv".
1307 * When no longer defined, remove the variable from the v: hashtable.
1310 restore_vimvar(idx
, save_tv
)
1316 clear_tv(&vimvars
[idx
].vv_tv
);
1317 vimvars
[idx
].vv_tv
= *save_tv
;
1318 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1320 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1321 if (HASHITEM_EMPTY(hi
))
1322 EMSG2(_(e_intern2
), "restore_vimvar()");
1324 hash_remove(&vimvarht
, hi
);
1328 #if defined(FEAT_SPELL) || defined(PROTO)
1330 * Evaluate an expression to a list with suggestions.
1331 * For the "expr:" part of 'spellsuggest'.
1334 eval_spell_expr(badword
, expr
)
1340 list_T
*list
= NULL
;
1341 char_u
*p
= skipwhite(expr
);
1343 /* Set "v:val" to the bad word. */
1344 prepare_vimvar(VV_VAL
, &save_val
);
1345 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1346 vimvars
[VV_VAL
].vv_str
= badword
;
1350 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1352 if (rettv
.v_type
!= VAR_LIST
)
1355 list
= rettv
.vval
.v_list
;
1360 vimvars
[VV_VAL
].vv_str
= NULL
;
1361 restore_vimvar(VV_VAL
, &save_val
);
1367 * "list" is supposed to contain two items: a word and a number. Return the
1368 * word in "pp" and the number as the return value.
1369 * Return -1 if anything isn't right.
1370 * Used to get the good word and score from the eval_spell_expr() result.
1373 get_spellword(list
, pp
)
1379 li
= list
->lv_first
;
1382 *pp
= get_tv_string(&li
->li_tv
);
1387 return get_tv_number(&li
->li_tv
);
1392 * Top level evaluation function.
1393 * Returns an allocated typval_T with the result.
1394 * Returns NULL when there is an error.
1397 eval_expr(arg
, nextcmd
)
1403 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1404 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1414 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1416 * Call some vimL function and return the result in "*rettv".
1417 * Uses argv[argc] for the function arguments.
1418 * Returns OK or FAIL.
1421 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1425 int safe
; /* use the sandbox */
1433 void *save_funccalp
= NULL
;
1436 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1437 if (argvars
== NULL
)
1440 for (i
= 0; i
< argc
; i
++)
1442 /* Pass a NULL or empty argument as an empty string */
1443 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1445 argvars
[i
].v_type
= VAR_STRING
;
1446 argvars
[i
].vval
.v_string
= (char_u
*)"";
1450 /* Recognize a number argument, the others must be strings. */
1451 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1452 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1454 argvars
[i
].v_type
= VAR_NUMBER
;
1455 argvars
[i
].vval
.v_number
= n
;
1459 argvars
[i
].v_type
= VAR_STRING
;
1460 argvars
[i
].vval
.v_string
= argv
[i
];
1466 save_funccalp
= save_funccal();
1470 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1471 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1472 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1473 &doesrange
, TRUE
, NULL
);
1477 restore_funccal(save_funccalp
);
1488 * Call vimL function "func" and return the result as a string.
1489 * Returns NULL when calling the function fails.
1490 * Uses argv[argc] for the function arguments.
1493 call_func_retstr(func
, argc
, argv
, safe
)
1497 int safe
; /* use the sandbox */
1502 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1505 retval
= vim_strsave(get_tv_string(&rettv
));
1510 #if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1512 * Call vimL function "func" and return the result as a number.
1513 * Returns -1 when calling the function fails.
1514 * Uses argv[argc] for the function arguments.
1517 call_func_retnr(func
, argc
, argv
, safe
)
1521 int safe
; /* use the sandbox */
1526 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1529 retval
= get_tv_number_chk(&rettv
, NULL
);
1536 * Call vimL function "func" and return the result as a list
1537 * Uses argv[argc] for the function arguments.
1540 call_func_retlist(func
, argc
, argv
, safe
)
1544 int safe
; /* use the sandbox */
1548 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1551 if (rettv
.v_type
!= VAR_LIST
)
1557 return rettv
.vval
.v_list
;
1563 * Save the current function call pointer, and set it to NULL.
1564 * Used when executing autocommands and for ":source".
1569 funccall_T
*fc
= current_funccal
;
1571 current_funccal
= NULL
;
1576 restore_funccal(vfc
)
1579 funccall_T
*fc
= (funccall_T
*)vfc
;
1581 current_funccal
= fc
;
1584 #if defined(FEAT_PROFILE) || defined(PROTO)
1586 * Prepare profiling for entering a child or something else that is not
1587 * counted for the script/function itself.
1588 * Should always be called in pair with prof_child_exit().
1591 prof_child_enter(tm
)
1592 proftime_T
*tm
; /* place to store waittime */
1594 funccall_T
*fc
= current_funccal
;
1596 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1597 profile_start(&fc
->prof_child
);
1598 script_prof_save(tm
);
1602 * Take care of time spent in a child.
1603 * Should always be called after prof_child_enter().
1607 proftime_T
*tm
; /* where waittime was stored */
1609 funccall_T
*fc
= current_funccal
;
1611 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1613 profile_end(&fc
->prof_child
);
1614 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1615 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1616 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1618 script_prof_restore(tm
);
1625 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1626 * it in "*cp". Doesn't give error messages.
1629 eval_foldexpr(arg
, cp
)
1636 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1644 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1648 /* If the result is a number, just return the number. */
1649 if (tv
.v_type
== VAR_NUMBER
)
1650 retval
= tv
.vval
.v_number
;
1651 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1655 /* If the result is a string, check if there is a non-digit before
1657 s
= tv
.vval
.v_string
;
1658 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1660 retval
= atol((char *)s
);
1674 * ":let" list all variable values
1675 * ":let var1 var2" list variable values
1676 * ":let var = expr" assignment command.
1677 * ":let var += expr" assignment command.
1678 * ":let var -= expr" assignment command.
1679 * ":let var .= expr" assignment command.
1680 * ":let [var1, var2] = expr" unpack list.
1686 char_u
*arg
= eap
->arg
;
1687 char_u
*expr
= NULL
;
1695 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1698 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1700 expr
= vim_strchr(argend
, '=');
1704 * ":let" without "=": list variables
1708 else if (!ends_excmd(*arg
))
1709 /* ":let var1 var2" */
1710 arg
= list_arg_vars(eap
, arg
);
1711 else if (!eap
->skip
)
1724 eap
->nextcmd
= check_nextcmd(arg
);
1732 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1733 op
[0] = expr
[-1]; /* +=, -= or .= */
1735 expr
= skipwhite(expr
+ 1);
1739 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1748 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1756 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1757 * Handles both "var" with any type and "[var, var; var]" with a list type.
1758 * When "nextchars" is not NULL it points to a string with characters that
1759 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1761 * Returns OK or FAIL;
1764 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1767 int copy
; /* copy values from "tv", don't move */
1768 int semicolon
; /* from skip_var_list() */
1769 int var_count
; /* from skip_var_list() */
1772 char_u
*arg
= arg_start
;
1781 * ":let var = expr" or ":for var in list"
1783 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1789 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1791 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1798 if (semicolon
== 0 && var_count
< i
)
1800 EMSG(_("E687: Less targets than List items"));
1803 if (var_count
- semicolon
> i
)
1805 EMSG(_("E688: More targets than List items"));
1812 arg
= skipwhite(arg
+ 1);
1813 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1814 item
= item
->li_next
;
1818 arg
= skipwhite(arg
);
1821 /* Put the rest of the list (may be empty) in the var after ';'.
1822 * Create a new list for this. */
1826 while (item
!= NULL
)
1828 list_append_tv(l
, &item
->li_tv
);
1829 item
= item
->li_next
;
1832 ltv
.v_type
= VAR_LIST
;
1834 ltv
.vval
.v_list
= l
;
1837 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1838 (char_u
*)"]", nextchars
);
1844 else if (*arg
!= ',' && *arg
!= ']')
1846 EMSG2(_(e_intern2
), "ex_let_vars()");
1855 * Skip over assignable variable "var" or list of variables "[var, var]".
1856 * Used for ":let varvar = expr" and ":for varvar in expr".
1857 * For "[var, var]" increment "*var_count" for each variable.
1858 * for "[var, var; var]" set "semicolon".
1859 * Return NULL for an error.
1862 skip_var_list(arg
, var_count
, semicolon
)
1871 /* "[var, var]": find the matching ']'. */
1875 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1876 s
= skip_var_one(p
);
1879 EMSG2(_(e_invarg2
), p
);
1889 if (*semicolon
== 1)
1891 EMSG(_("Double ; in list of variables"));
1898 EMSG2(_(e_invarg2
), p
);
1905 return skip_var_one(arg
);
1909 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1916 if (*arg
== '@' && arg
[1] != NUL
)
1918 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
1919 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
1923 * List variables for hashtab "ht" with prefix "prefix".
1924 * If "empty" is TRUE also list NULL strings as empty strings.
1927 list_hashtable_vars(ht
, prefix
, empty
)
1936 todo
= (int)ht
->ht_used
;
1937 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
1939 if (!HASHITEM_EMPTY(hi
))
1943 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
1944 || di
->di_tv
.vval
.v_string
!= NULL
)
1945 list_one_var(di
, prefix
);
1951 * List global variables.
1956 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
);
1960 * List buffer variables.
1965 char_u numbuf
[NUMBUFLEN
];
1967 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:", TRUE
);
1969 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
1970 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
, numbuf
);
1974 * List window variables.
1979 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
, (char_u
*)"w:", TRUE
);
1984 * List tab page variables.
1989 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
, (char_u
*)"t:", TRUE
);
1994 * List Vim variables.
1999 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
);
2003 * List script-local variables, if there is a script.
2008 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2009 list_hashtable_vars(&SCRIPT_VARS(current_SID
), (char_u
*)"s:", FALSE
);
2013 * List function variables, if there is a function.
2018 if (current_funccal
!= NULL
)
2019 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2020 (char_u
*)"l:", FALSE
);
2024 * List variables in "arg".
2027 list_arg_vars(eap
, arg
)
2039 while (!ends_excmd(*arg
) && !got_int
)
2041 if (error
|| eap
->skip
)
2043 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2044 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2047 EMSG(_(e_trailing
));
2053 /* get_name_len() takes care of expanding curly braces */
2054 name_start
= name
= arg
;
2055 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2058 /* This is mainly to keep test 49 working: when expanding
2059 * curly braces fails overrule the exception error message. */
2060 if (len
< 0 && !aborting())
2063 EMSG2(_(e_invarg2
), arg
);
2072 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2076 /* handle d.key, l[idx], f(expr) */
2078 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2082 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2086 case 'g': list_glob_vars(); break;
2087 case 'b': list_buf_vars(); break;
2088 case 'w': list_win_vars(); break;
2090 case 't': list_tab_vars(); break;
2092 case 'v': list_vim_vars(); break;
2093 case 's': list_script_vars(); break;
2094 case 'l': list_func_vars(); break;
2096 EMSG2(_("E738: Can't list variables for %s"), name
);
2101 char_u numbuf
[NUMBUFLEN
];
2106 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2109 list_one_var_a((char_u
*)"",
2110 arg
== arg_subsc
? name
: name_start
,
2111 tv
.v_type
, s
== NULL
? (char_u
*)"" : s
);
2123 arg
= skipwhite(arg
);
2130 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2131 * Returns a pointer to the char just after the var name.
2132 * Returns NULL if there is an error.
2135 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2136 char_u
*arg
; /* points to variable name */
2137 typval_T
*tv
; /* value to assign to variable */
2138 int copy
; /* copy value from "tv" */
2139 char_u
*endchars
; /* valid chars after variable name or NULL */
2140 char_u
*op
; /* "+", "-", "." or NULL*/
2145 char_u
*arg_end
= NULL
;
2148 char_u
*tofree
= NULL
;
2151 * ":let $VAR = expr": Set environment variable.
2155 /* Find the end of the name. */
2158 len
= get_env_len(&arg
);
2160 EMSG2(_(e_invarg2
), name
- 1);
2163 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2164 EMSG2(_(e_letwrong
), op
);
2165 else if (endchars
!= NULL
2166 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2167 EMSG(_(e_letunexp
));
2172 p
= get_tv_string_chk(tv
);
2173 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2175 int mustfree
= FALSE
;
2176 char_u
*s
= vim_getenv(name
, &mustfree
);
2180 p
= tofree
= concat_str(s
, p
);
2187 vim_setenv(name
, p
);
2188 if (STRICMP(name
, "HOME") == 0)
2190 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2192 else if (didset_vimruntime
2193 && STRICMP(name
, "VIMRUNTIME") == 0)
2194 didset_vimruntime
= FALSE
;
2204 * ":let &option = expr": Set option value.
2205 * ":let &l:option = expr": Set local option value.
2206 * ":let &g:option = expr": Set global option value.
2208 else if (*arg
== '&')
2210 /* Find the end of the name. */
2211 p
= find_option_end(&arg
, &opt_flags
);
2212 if (p
== NULL
|| (endchars
!= NULL
2213 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2214 EMSG(_(e_letunexp
));
2220 char_u
*stringval
= NULL
;
2226 n
= get_tv_number(tv
);
2227 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2228 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2230 opt_type
= get_option_value(arg
, &numval
,
2231 &stringval
, opt_flags
);
2232 if ((opt_type
== 1 && *op
== '.')
2233 || (opt_type
== 0 && *op
!= '.'))
2234 EMSG2(_(e_letwrong
), op
);
2237 if (opt_type
== 1) /* number */
2244 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2246 s
= concat_str(stringval
, s
);
2247 vim_free(stringval
);
2254 set_option_value(arg
, n
, s
, opt_flags
);
2258 vim_free(stringval
);
2263 * ":let @r = expr": Set register contents.
2265 else if (*arg
== '@')
2268 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2269 EMSG2(_(e_letwrong
), op
);
2270 else if (endchars
!= NULL
2271 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2272 EMSG(_(e_letunexp
));
2275 char_u
*ptofree
= NULL
;
2278 p
= get_tv_string_chk(tv
);
2279 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2281 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2284 p
= ptofree
= concat_str(s
, p
);
2290 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2298 * ":let var = expr": Set internal variable.
2299 * ":let {expr} = expr": Idem, name made with curly braces
2301 else if (eval_isnamec1(*arg
) || *arg
== '{')
2305 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2306 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2308 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2309 EMSG(_(e_letunexp
));
2312 set_var_lval(&lv
, p
, tv
, copy
, op
);
2320 EMSG2(_(e_invarg2
), arg
);
2326 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2329 check_changedtick(arg
)
2332 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2334 EMSG2(_(e_readonlyvar
), arg
);
2341 * Get an lval: variable, Dict item or List item that can be assigned a value
2342 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2343 * "name.key", "name.key[expr]" etc.
2344 * Indexing only works if "name" is an existing List or Dictionary.
2345 * "name" points to the start of the name.
2346 * If "rettv" is not NULL it points to the value to be assigned.
2347 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2348 * wrong; must end in space or cmd separator.
2350 * Returns a pointer to just after the name, including indexes.
2351 * When an evaluation error occurs "lp->ll_name" is NULL;
2352 * Returns NULL for a parsing error. Still need to free items in "lp"!
2355 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2361 int quiet
; /* don't give error messages */
2362 int fne_flags
; /* flags for find_name_end() */
2365 char_u
*expr_start
, *expr_end
;
2376 /* Clear everything in "lp". */
2377 vim_memset(lp
, 0, sizeof(lval_T
));
2381 /* When skipping just find the end of the name. */
2383 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2386 /* Find the end of the name. */
2387 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2388 if (expr_start
!= NULL
)
2390 /* Don't expand the name when we already know there is an error. */
2391 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2392 && *p
!= '[' && *p
!= '.')
2394 EMSG(_(e_trailing
));
2398 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2399 if (lp
->ll_exp_name
== NULL
)
2401 /* Report an invalid expression in braces, unless the
2402 * expression evaluation has been cancelled due to an
2403 * aborting error, an interrupt, or an exception. */
2404 if (!aborting() && !quiet
)
2407 EMSG2(_(e_invarg2
), name
);
2411 lp
->ll_name
= lp
->ll_exp_name
;
2416 /* Without [idx] or .key we are done. */
2417 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2422 v
= find_var(lp
->ll_name
, &ht
);
2423 if (v
== NULL
&& !quiet
)
2424 EMSG2(_(e_undefvar
), lp
->ll_name
);
2430 * Loop until no more [idx] or .key is following.
2432 lp
->ll_tv
= &v
->di_tv
;
2433 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2435 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2436 && !(lp
->ll_tv
->v_type
== VAR_DICT
2437 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2440 EMSG(_("E689: Can only index a List or Dictionary"));
2446 EMSG(_("E708: [:] must come last"));
2454 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2459 EMSG(_(e_emptykey
));
2466 /* Get the index [expr] or the first index [expr: ]. */
2467 p
= skipwhite(p
+ 1);
2473 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2475 if (get_tv_string_chk(&var1
) == NULL
)
2477 /* not a number or string */
2483 /* Optionally get the second index [ :expr]. */
2486 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2489 EMSG(_(e_dictrange
));
2494 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2495 || rettv
->vval
.v_list
== NULL
))
2498 EMSG(_("E709: [:] requires a List value"));
2503 p
= skipwhite(p
+ 1);
2505 lp
->ll_empty2
= TRUE
;
2508 lp
->ll_empty2
= FALSE
;
2509 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2515 if (get_tv_string_chk(&var2
) == NULL
)
2517 /* not a number or string */
2524 lp
->ll_range
= TRUE
;
2527 lp
->ll_range
= FALSE
;
2532 EMSG(_(e_missbrac
));
2535 if (lp
->ll_range
&& !lp
->ll_empty2
)
2540 /* Skip to past ']'. */
2544 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2548 /* "[key]": get key from "var1" */
2549 key
= get_tv_string(&var1
); /* is number or string */
2553 EMSG(_(e_emptykey
));
2559 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2560 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2561 if (lp
->ll_di
== NULL
)
2563 /* Key does not exist in dict: may need to add it. */
2564 if (*p
== '[' || *p
== '.' || unlet
)
2567 EMSG2(_(e_dictkey
), key
);
2573 lp
->ll_newkey
= vim_strsave(key
);
2575 lp
->ll_newkey
= vim_strnsave(key
, len
);
2578 if (lp
->ll_newkey
== NULL
)
2584 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2589 * Get the number and item for the only or first index of the List.
2595 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2599 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2600 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2601 if (lp
->ll_li
== NULL
)
2606 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2609 if (lp
->ll_li
== NULL
)
2611 if (lp
->ll_range
&& !lp
->ll_empty2
)
2617 * May need to find the item or absolute index for the second
2619 * When no index given: "lp->ll_empty2" is TRUE.
2620 * Otherwise "lp->ll_n2" is set to the second index.
2622 if (lp
->ll_range
&& !lp
->ll_empty2
)
2624 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2628 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2631 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2634 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2636 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2637 if (lp
->ll_n2
< lp
->ll_n1
)
2641 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2649 * Clear lval "lp" that was filled by get_lval().
2655 vim_free(lp
->ll_exp_name
);
2656 vim_free(lp
->ll_newkey
);
2660 * Set a variable that was parsed by get_lval() to "rettv".
2661 * "endp" points to just after the parsed name.
2662 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2665 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2676 if (lp
->ll_tv
== NULL
)
2678 if (!check_changedtick(lp
->ll_name
))
2682 if (op
!= NULL
&& *op
!= '=')
2686 /* handle +=, -= and .= */
2687 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2690 if (tv_op(&tv
, rettv
, op
) == OK
)
2691 set_var(lp
->ll_name
, &tv
, FALSE
);
2696 set_var(lp
->ll_name
, rettv
, copy
);
2700 else if (tv_check_lock(lp
->ll_newkey
== NULL
2702 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2704 else if (lp
->ll_range
)
2707 * Assign the List values to the list items.
2709 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2711 if (op
!= NULL
&& *op
!= '=')
2712 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2715 clear_tv(&lp
->ll_li
->li_tv
);
2716 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2719 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2721 if (lp
->ll_li
->li_next
== NULL
)
2723 /* Need to add an empty item. */
2724 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2730 lp
->ll_li
= lp
->ll_li
->li_next
;
2734 EMSG(_("E710: List value has more items than target"));
2735 else if (lp
->ll_empty2
2736 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2737 : lp
->ll_n1
!= lp
->ll_n2
)
2738 EMSG(_("E711: List value has not enough items"));
2743 * Assign to a List or Dictionary item.
2745 if (lp
->ll_newkey
!= NULL
)
2747 if (op
!= NULL
&& *op
!= '=')
2749 EMSG2(_(e_letwrong
), op
);
2753 /* Need to add an item to the Dictionary. */
2754 di
= dictitem_alloc(lp
->ll_newkey
);
2757 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2762 lp
->ll_tv
= &di
->di_tv
;
2764 else if (op
!= NULL
&& *op
!= '=')
2766 tv_op(lp
->ll_tv
, rettv
, op
);
2770 clear_tv(lp
->ll_tv
);
2773 * Assign the value to the variable or list item.
2776 copy_tv(rettv
, lp
->ll_tv
);
2779 *lp
->ll_tv
= *rettv
;
2780 lp
->ll_tv
->v_lock
= 0;
2787 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2788 * Returns OK or FAIL.
2797 char_u numbuf
[NUMBUFLEN
];
2800 /* Can't do anything with a Funcref or a Dict on the right. */
2801 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2803 switch (tv1
->v_type
)
2810 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2813 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2814 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2819 if (tv2
->v_type
== VAR_LIST
)
2821 if (*op
== '+' || *op
== '-')
2823 /* nr += nr or nr -= nr*/
2824 n
= get_tv_number(tv1
);
2826 n
+= get_tv_number(tv2
);
2828 n
-= get_tv_number(tv2
);
2830 tv1
->v_type
= VAR_NUMBER
;
2831 tv1
->vval
.v_number
= n
;
2836 s
= get_tv_string(tv1
);
2837 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2839 tv1
->v_type
= VAR_STRING
;
2840 tv1
->vval
.v_string
= s
;
2846 EMSG2(_(e_letwrong
), op
);
2851 * Add a watcher to a list.
2854 list_add_watch(l
, lw
)
2858 lw
->lw_next
= l
->lv_watch
;
2863 * Remove a watcher from a list.
2864 * No warning when it isn't found...
2867 list_rem_watch(l
, lwrem
)
2871 listwatch_T
*lw
, **lwp
;
2874 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2886 * Just before removing an item from a list: advance watchers to the next
2890 list_fix_watch(l
, item
)
2896 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2897 if (lw
->lw_item
== item
)
2898 lw
->lw_item
= item
->li_next
;
2902 * Evaluate the expression used in a ":for var in expr" command.
2903 * "arg" points to "var".
2904 * Set "*errp" to TRUE for an error, FALSE otherwise;
2905 * Return a pointer that holds the info. Null when there is an error.
2908 eval_for_line(arg
, errp
, nextcmdp
, skip
)
2919 *errp
= TRUE
; /* default: there is an error */
2921 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
2925 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
2929 expr
= skipwhite(expr
);
2930 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
2932 EMSG(_("E690: Missing \"in\" after :for"));
2938 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
2944 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
2951 /* No need to increment the refcount, it's already set for the
2952 * list being used in "tv". */
2954 list_add_watch(l
, &fi
->fi_lw
);
2955 fi
->fi_lw
.lw_item
= l
->lv_first
;
2966 * Use the first item in a ":for" list. Advance to the next.
2967 * Assign the values to the variable (list). "arg" points to the first one.
2968 * Return TRUE when a valid item was found, FALSE when at end of list or
2972 next_for_item(fi_void
, arg
)
2976 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
2980 item
= fi
->fi_lw
.lw_item
;
2985 fi
->fi_lw
.lw_item
= item
->li_next
;
2986 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
2987 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
2993 * Free the structure used to store info used by ":for".
2996 free_for_info(fi_void
)
2999 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3001 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3003 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3004 list_unref(fi
->fi_list
);
3009 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3012 set_context_for_expression(xp
, arg
, cmdidx
)
3021 if (cmdidx
== CMD_let
)
3023 xp
->xp_context
= EXPAND_USER_VARS
;
3024 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3026 /* ":let var1 var2 ...": find last space. */
3027 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3030 mb_ptr_back(arg
, p
);
3031 if (vim_iswhite(*p
))
3038 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3039 : EXPAND_EXPRESSION
;
3040 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3041 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3043 c
= *xp
->xp_pattern
;
3046 c
= xp
->xp_pattern
[1];
3050 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3051 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3055 xp
->xp_context
= EXPAND_SETTINGS
;
3056 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3057 xp
->xp_pattern
+= 2;
3063 /* environment variable */
3064 xp
->xp_context
= EXPAND_ENV_VARS
;
3069 xp
->xp_context
= EXPAND_EXPRESSION
;
3072 && xp
->xp_context
== EXPAND_FUNCTIONS
3073 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3075 /* Function name can start with "<SNR>" */
3078 else if (cmdidx
!= CMD_let
|| got_eq
)
3080 if (c
== '"') /* string */
3082 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3083 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3085 xp
->xp_context
= EXPAND_NOTHING
;
3087 else if (c
== '\'') /* literal string */
3089 /* Trick: '' is like stopping and starting a literal string. */
3090 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3092 xp
->xp_context
= EXPAND_NOTHING
;
3096 if (xp
->xp_pattern
[1] == '|')
3099 xp
->xp_context
= EXPAND_EXPRESSION
;
3102 xp
->xp_context
= EXPAND_COMMANDS
;
3105 xp
->xp_context
= EXPAND_EXPRESSION
;
3108 /* Doesn't look like something valid, expand as an expression
3110 xp
->xp_context
= EXPAND_EXPRESSION
;
3111 arg
= xp
->xp_pattern
;
3113 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3116 xp
->xp_pattern
= arg
;
3119 #endif /* FEAT_CMDL_COMPL */
3122 * ":1,25call func(arg1, arg2)" function call.
3128 char_u
*arg
= eap
->arg
;
3139 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3140 if (fudi
.fd_newkey
!= NULL
)
3142 /* Still need to give an error message for missing key. */
3143 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3144 vim_free(fudi
.fd_newkey
);
3149 /* Increase refcount on dictionary, it could get deleted when evaluating
3151 if (fudi
.fd_dict
!= NULL
)
3152 ++fudi
.fd_dict
->dv_refcount
;
3154 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3155 len
= (int)STRLEN(tofree
);
3156 name
= deref_func_name(tofree
, &len
);
3158 /* Skip white space to allow ":call func ()". Not good, but required for
3159 * backward compatibility. */
3160 startarg
= skipwhite(arg
);
3161 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3163 if (*startarg
!= '(')
3165 EMSG2(_("E107: Missing braces: %s"), eap
->arg
);
3170 * When skipping, evaluate the function once, to find the end of the
3172 * When the function takes a range, this is discovered after the first
3173 * call, and the loop is broken.
3178 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3182 for ( ; lnum
<= eap
->line2
; ++lnum
)
3184 if (!eap
->skip
&& eap
->addr_count
> 0)
3186 curwin
->w_cursor
.lnum
= lnum
;
3187 curwin
->w_cursor
.col
= 0;
3190 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3191 eap
->line1
, eap
->line2
, &doesrange
,
3192 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3198 /* Handle a function returning a Funcref, Dictionary or List. */
3199 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3206 if (doesrange
|| eap
->skip
)
3209 /* Stop when immediately aborting on error, or when an interrupt
3210 * occurred or an exception was thrown but not caught.
3211 * get_func_tv() returned OK, so that the check for trailing
3212 * characters below is executed. */
3221 /* Check for trailing illegal characters and a following command. */
3222 if (!ends_excmd(*arg
))
3225 EMSG(_(e_trailing
));
3228 eap
->nextcmd
= check_nextcmd(arg
);
3232 dict_unref(fudi
.fd_dict
);
3237 * ":unlet[!] var1 ... " command.
3243 ex_unletlock(eap
, eap
->arg
, 0);
3247 * ":lockvar" and ":unlockvar" commands
3253 char_u
*arg
= eap
->arg
;
3258 else if (vim_isdigit(*arg
))
3260 deep
= getdigits(&arg
);
3261 arg
= skipwhite(arg
);
3264 ex_unletlock(eap
, arg
, deep
);
3268 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3271 ex_unletlock(eap
, argstart
, deep
)
3276 char_u
*arg
= argstart
;
3283 /* Parse the name and find the end. */
3284 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3286 if (lv
.ll_name
== NULL
)
3287 error
= TRUE
; /* error but continue parsing */
3288 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3289 && !ends_excmd(*name_end
)))
3291 if (name_end
!= NULL
)
3294 EMSG(_(e_trailing
));
3296 if (!(eap
->skip
|| error
))
3301 if (!error
&& !eap
->skip
)
3303 if (eap
->cmdidx
== CMD_unlet
)
3305 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3310 if (do_lock_var(&lv
, name_end
, deep
,
3311 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3319 arg
= skipwhite(name_end
);
3320 } while (!ends_excmd(*arg
));
3322 eap
->nextcmd
= check_nextcmd(arg
);
3326 do_unlet_var(lp
, name_end
, forceit
)
3334 if (lp
->ll_tv
== NULL
)
3339 /* Normal name or expanded name. */
3340 if (check_changedtick(lp
->ll_name
))
3342 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3346 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3348 else if (lp
->ll_range
)
3352 /* Delete a range of List items. */
3353 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3355 li
= lp
->ll_li
->li_next
;
3356 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3363 if (lp
->ll_list
!= NULL
)
3364 /* unlet a List item. */
3365 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3367 /* unlet a Dictionary item. */
3368 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3375 * "unlet" a variable. Return OK if it existed, FAIL if not.
3376 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3379 do_unlet(name
, forceit
)
3387 ht
= find_var_ht(name
, &varname
);
3388 if (ht
!= NULL
&& *varname
!= NUL
)
3390 hi
= hash_find(ht
, varname
);
3391 if (!HASHITEM_EMPTY(hi
))
3393 if (var_check_fixed(HI2DI(hi
)->di_flags
, name
))
3395 if (var_check_ro(HI2DI(hi
)->di_flags
, name
))
3403 EMSG2(_("E108: No such variable: \"%s\""), name
);
3408 * Lock or unlock variable indicated by "lp".
3409 * "deep" is the levels to go (-1 for unlimited);
3410 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3413 do_lock_var(lp
, name_end
, deep
, lock
)
3423 if (deep
== 0) /* nothing to do */
3426 if (lp
->ll_tv
== NULL
)
3431 /* Normal name or expanded name. */
3432 if (check_changedtick(lp
->ll_name
))
3436 di
= find_var(lp
->ll_name
, NULL
);
3442 di
->di_flags
|= DI_FLAGS_LOCK
;
3444 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3445 item_lock(&di
->di_tv
, deep
, lock
);
3450 else if (lp
->ll_range
)
3452 listitem_T
*li
= lp
->ll_li
;
3454 /* (un)lock a range of List items. */
3455 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3457 item_lock(&li
->li_tv
, deep
, lock
);
3462 else if (lp
->ll_list
!= NULL
)
3463 /* (un)lock a List item. */
3464 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3466 /* un(lock) a Dictionary item. */
3467 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3473 * Lock or unlock an item. "deep" is nr of levels to go.
3476 item_lock(tv
, deep
, lock
)
3481 static int recurse
= 0;
3488 if (recurse
>= DICT_MAXNEST
)
3490 EMSG(_("E743: variable nested too deep for (un)lock"));
3497 /* lock/unlock the item itself */
3499 tv
->v_lock
|= VAR_LOCKED
;
3501 tv
->v_lock
&= ~VAR_LOCKED
;
3506 if ((l
= tv
->vval
.v_list
) != NULL
)
3509 l
->lv_lock
|= VAR_LOCKED
;
3511 l
->lv_lock
&= ~VAR_LOCKED
;
3512 if (deep
< 0 || deep
> 1)
3513 /* recursive: lock/unlock the items the List contains */
3514 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3515 item_lock(&li
->li_tv
, deep
- 1, lock
);
3519 if ((d
= tv
->vval
.v_dict
) != NULL
)
3522 d
->dv_lock
|= VAR_LOCKED
;
3524 d
->dv_lock
&= ~VAR_LOCKED
;
3525 if (deep
< 0 || deep
> 1)
3527 /* recursive: lock/unlock the items the List contains */
3528 todo
= (int)d
->dv_hashtab
.ht_used
;
3529 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3531 if (!HASHITEM_EMPTY(hi
))
3534 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3544 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3545 * it refers to a List or Dictionary that is locked.
3551 return (tv
->v_lock
& VAR_LOCKED
)
3552 || (tv
->v_type
== VAR_LIST
3553 && tv
->vval
.v_list
!= NULL
3554 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3555 || (tv
->v_type
== VAR_DICT
3556 && tv
->vval
.v_dict
!= NULL
3557 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3560 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3562 * Delete all "menutrans_" variables.
3565 del_menutrans_vars()
3570 hash_lock(&globvarht
);
3571 todo
= (int)globvarht
.ht_used
;
3572 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3574 if (!HASHITEM_EMPTY(hi
))
3577 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3578 delete_var(&globvarht
, hi
);
3581 hash_unlock(&globvarht
);
3585 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3588 * Local string buffer for the next two functions to store a variable name
3589 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3590 * get_user_var_name().
3593 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3595 static char_u
*varnamebuf
= NULL
;
3596 static int varnamebuflen
= 0;
3599 * Function to concatenate a prefix and a variable name.
3602 cat_prefix_varname(prefix
, name
)
3608 len
= (int)STRLEN(name
) + 3;
3609 if (len
> varnamebuflen
)
3611 vim_free(varnamebuf
);
3612 len
+= 10; /* some additional space */
3613 varnamebuf
= alloc(len
);
3614 if (varnamebuf
== NULL
)
3619 varnamebuflen
= len
;
3621 *varnamebuf
= prefix
;
3622 varnamebuf
[1] = ':';
3623 STRCPY(varnamebuf
+ 2, name
);
3628 * Function given to ExpandGeneric() to obtain the list of user defined
3629 * (global/buffer/window/built-in) variable names.
3633 get_user_var_name(xp
, idx
)
3637 static long_u gdone
;
3638 static long_u bdone
;
3639 static long_u wdone
;
3641 static long_u tdone
;
3644 static hashitem_T
*hi
;
3649 gdone
= bdone
= wdone
= vidx
= 0;
3655 /* Global variables */
3656 if (gdone
< globvarht
.ht_used
)
3659 hi
= globvarht
.ht_array
;
3662 while (HASHITEM_EMPTY(hi
))
3664 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3665 return cat_prefix_varname('g', hi
->hi_key
);
3670 ht
= &curbuf
->b_vars
.dv_hashtab
;
3671 if (bdone
< ht
->ht_used
)
3677 while (HASHITEM_EMPTY(hi
))
3679 return cat_prefix_varname('b', hi
->hi_key
);
3681 if (bdone
== ht
->ht_used
)
3684 return (char_u
*)"b:changedtick";
3688 ht
= &curwin
->w_vars
.dv_hashtab
;
3689 if (wdone
< ht
->ht_used
)
3695 while (HASHITEM_EMPTY(hi
))
3697 return cat_prefix_varname('w', hi
->hi_key
);
3702 ht
= &curtab
->tp_vars
.dv_hashtab
;
3703 if (tdone
< ht
->ht_used
)
3709 while (HASHITEM_EMPTY(hi
))
3711 return cat_prefix_varname('t', hi
->hi_key
);
3717 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3719 vim_free(varnamebuf
);
3725 #endif /* FEAT_CMDL_COMPL */
3728 * types for expressions.
3733 , TYPE_EQUAL
/* == */
3734 , TYPE_NEQUAL
/* != */
3735 , TYPE_GREATER
/* > */
3736 , TYPE_GEQUAL
/* >= */
3737 , TYPE_SMALLER
/* < */
3738 , TYPE_SEQUAL
/* <= */
3739 , TYPE_MATCH
/* =~ */
3740 , TYPE_NOMATCH
/* !~ */
3744 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3745 * executed. The function may return OK, but the rettv will be of type
3746 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3750 * Handle zero level expression.
3751 * This calls eval1() and handles error message and nextcmd.
3752 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3753 * Note: "rettv.v_lock" is not set.
3754 * Return OK or FAIL.
3757 eval0(arg
, rettv
, nextcmd
, evaluate
)
3767 ret
= eval1(&p
, rettv
, evaluate
);
3768 if (ret
== FAIL
|| !ends_excmd(*p
))
3773 * Report the invalid expression unless the expression evaluation has
3774 * been cancelled due to an aborting error, an interrupt, or an
3778 EMSG2(_(e_invexpr2
), arg
);
3781 if (nextcmd
!= NULL
)
3782 *nextcmd
= check_nextcmd(p
);
3788 * Handle top level expression:
3789 * expr1 ? expr0 : expr0
3791 * "arg" must point to the first non-white of the expression.
3792 * "arg" is advanced to the next non-white after the recognized expression.
3794 * Note: "rettv.v_lock" is not set.
3796 * Return OK or FAIL.
3799 eval1(arg
, rettv
, evaluate
)
3808 * Get the first variable.
3810 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3813 if ((*arg
)[0] == '?')
3820 if (get_tv_number_chk(rettv
, &error
) != 0)
3828 * Get the second variable.
3830 *arg
= skipwhite(*arg
+ 1);
3831 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3835 * Check for the ":".
3837 if ((*arg
)[0] != ':')
3839 EMSG(_("E109: Missing ':' after '?'"));
3840 if (evaluate
&& result
)
3846 * Get the third variable.
3848 *arg
= skipwhite(*arg
+ 1);
3849 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
3851 if (evaluate
&& result
)
3855 if (evaluate
&& !result
)
3863 * Handle first level expression:
3864 * expr2 || expr2 || expr2 logical OR
3866 * "arg" must point to the first non-white of the expression.
3867 * "arg" is advanced to the next non-white after the recognized expression.
3869 * Return OK or FAIL.
3872 eval2(arg
, rettv
, evaluate
)
3883 * Get the first variable.
3885 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
3889 * Repeat until there is no following "||".
3893 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
3895 if (evaluate
&& first
)
3897 if (get_tv_number_chk(rettv
, &error
) != 0)
3906 * Get the second variable.
3908 *arg
= skipwhite(*arg
+ 2);
3909 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
3913 * Compute the result.
3915 if (evaluate
&& !result
)
3917 if (get_tv_number_chk(&var2
, &error
) != 0)
3925 rettv
->v_type
= VAR_NUMBER
;
3926 rettv
->vval
.v_number
= result
;
3934 * Handle second level expression:
3935 * expr3 && expr3 && expr3 logical AND
3937 * "arg" must point to the first non-white of the expression.
3938 * "arg" is advanced to the next non-white after the recognized expression.
3940 * Return OK or FAIL.
3943 eval3(arg
, rettv
, evaluate
)
3954 * Get the first variable.
3956 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
3960 * Repeat until there is no following "&&".
3964 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
3966 if (evaluate
&& first
)
3968 if (get_tv_number_chk(rettv
, &error
) == 0)
3977 * Get the second variable.
3979 *arg
= skipwhite(*arg
+ 2);
3980 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
3984 * Compute the result.
3986 if (evaluate
&& result
)
3988 if (get_tv_number_chk(&var2
, &error
) == 0)
3996 rettv
->v_type
= VAR_NUMBER
;
3997 rettv
->vval
.v_number
= result
;
4005 * Handle third level expression:
4017 * "arg" must point to the first non-white of the expression.
4018 * "arg" is advanced to the next non-white after the recognized expression.
4020 * Return OK or FAIL.
4023 eval4(arg
, rettv
, evaluate
)
4031 exptype_T type
= TYPE_UNKNOWN
;
4032 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4036 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4037 regmatch_T regmatch
;
4042 * Get the first variable.
4044 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4050 case '=': if (p
[1] == '=')
4052 else if (p
[1] == '~')
4055 case '!': if (p
[1] == '=')
4057 else if (p
[1] == '~')
4058 type
= TYPE_NOMATCH
;
4060 case '>': if (p
[1] != '=')
4062 type
= TYPE_GREATER
;
4068 case '<': if (p
[1] != '=')
4070 type
= TYPE_SMALLER
;
4076 case 'i': if (p
[1] == 's')
4078 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4080 if (!vim_isIDc(p
[len
]))
4082 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4090 * If there is a comparitive operator, use it.
4092 if (type
!= TYPE_UNKNOWN
)
4094 /* extra question mark appended: ignore case */
4100 /* extra '#' appended: match case */
4101 else if (p
[len
] == '#')
4106 /* nothing appened: use 'ignorecase' */
4111 * Get the second variable.
4113 *arg
= skipwhite(p
+ len
);
4114 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4122 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4124 /* For "is" a different type always means FALSE, for "notis"
4126 n1
= (type
== TYPE_NEQUAL
);
4128 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4132 n1
= (rettv
->v_type
== var2
.v_type
4133 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4134 if (type
== TYPE_NEQUAL
)
4137 else if (rettv
->v_type
!= var2
.v_type
4138 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4140 if (rettv
->v_type
!= var2
.v_type
)
4141 EMSG(_("E691: Can only compare List with List"));
4143 EMSG(_("E692: Invalid operation for Lists"));
4150 /* Compare two Lists for being equal or unequal. */
4151 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4152 if (type
== TYPE_NEQUAL
)
4157 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4161 n1
= (rettv
->v_type
== var2
.v_type
4162 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4163 if (type
== TYPE_NEQUAL
)
4166 else if (rettv
->v_type
!= var2
.v_type
4167 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4169 if (rettv
->v_type
!= var2
.v_type
)
4170 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4172 EMSG(_("E736: Invalid operation for Dictionary"));
4179 /* Compare two Dictionaries for being equal or unequal. */
4180 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4181 if (type
== TYPE_NEQUAL
)
4186 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4188 if (rettv
->v_type
!= var2
.v_type
4189 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4191 if (rettv
->v_type
!= var2
.v_type
)
4192 EMSG(_("E693: Can only compare Funcref with Funcref"));
4194 EMSG(_("E694: Invalid operation for Funcrefs"));
4201 /* Compare two Funcrefs for being equal or unequal. */
4202 if (rettv
->vval
.v_string
== NULL
4203 || var2
.vval
.v_string
== NULL
)
4206 n1
= STRCMP(rettv
->vval
.v_string
,
4207 var2
.vval
.v_string
) == 0;
4208 if (type
== TYPE_NEQUAL
)
4214 * If one of the two variables is a number, compare as a number.
4215 * When using "=~" or "!~", always compare as string.
4217 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4218 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4220 n1
= get_tv_number(rettv
);
4221 n2
= get_tv_number(&var2
);
4224 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4225 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4226 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4227 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4228 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4229 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4232 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4237 s1
= get_tv_string_buf(rettv
, buf1
);
4238 s2
= get_tv_string_buf(&var2
, buf2
);
4239 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4240 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4246 case TYPE_EQUAL
: n1
= (i
== 0); break;
4247 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4248 case TYPE_GREATER
: n1
= (i
> 0); break;
4249 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4250 case TYPE_SMALLER
: n1
= (i
< 0); break;
4251 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4255 /* avoid 'l' flag in 'cpoptions' */
4257 p_cpo
= (char_u
*)"";
4258 regmatch
.regprog
= vim_regcomp(s2
,
4259 RE_MAGIC
+ RE_STRING
);
4260 regmatch
.rm_ic
= ic
;
4261 if (regmatch
.regprog
!= NULL
)
4263 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4264 vim_free(regmatch
.regprog
);
4265 if (type
== TYPE_NOMATCH
)
4271 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4276 rettv
->v_type
= VAR_NUMBER
;
4277 rettv
->vval
.v_number
= n1
;
4285 * Handle fourth level expression:
4287 * - number subtraction
4288 * . string concatenation
4290 * "arg" must point to the first non-white of the expression.
4291 * "arg" is advanced to the next non-white after the recognized expression.
4293 * Return OK or FAIL.
4296 eval5(arg
, rettv
, evaluate
)
4306 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4310 * Get the first variable.
4312 if (eval6(arg
, rettv
, evaluate
) == FAIL
)
4316 * Repeat computing, until no '+', '-' or '.' is following.
4321 if (op
!= '+' && op
!= '-' && op
!= '.')
4324 if (op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4326 /* For "list + ...", an illegal use of the first operand as
4327 * a number cannot be determined before evaluating the 2nd
4328 * operand: if this is also a list, all is ok.
4329 * For "something . ...", "something - ..." or "non-list + ...",
4330 * we know that the first operand needs to be a string or number
4331 * without evaluating the 2nd operand. So check before to avoid
4332 * side effects after an error. */
4333 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4341 * Get the second variable.
4343 *arg
= skipwhite(*arg
+ 1);
4344 if (eval6(arg
, &var2
, evaluate
) == FAIL
)
4353 * Compute the result.
4357 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4358 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4359 if (s2
== NULL
) /* type error ? */
4365 p
= concat_str(s1
, s2
);
4367 rettv
->v_type
= VAR_STRING
;
4368 rettv
->vval
.v_string
= p
;
4370 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4371 && var2
.v_type
== VAR_LIST
)
4373 /* concatenate Lists */
4374 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4388 n1
= get_tv_number_chk(rettv
, &error
);
4391 /* This can only happen for "list + non-list".
4392 * For "non-list + ..." or "something - ...", we returned
4393 * before evaluating the 2nd operand. */
4397 n2
= get_tv_number_chk(&var2
, &error
);
4409 rettv
->v_type
= VAR_NUMBER
;
4410 rettv
->vval
.v_number
= n1
;
4419 * Handle fifth level expression:
4420 * * number multiplication
4424 * "arg" must point to the first non-white of the expression.
4425 * "arg" is advanced to the next non-white after the recognized expression.
4427 * Return OK or FAIL.
4430 eval6(arg
, rettv
, evaluate
)
4441 * Get the first variable.
4443 if (eval7(arg
, rettv
, evaluate
) == FAIL
)
4447 * Repeat computing, until no '*', '/' or '%' is following.
4452 if (op
!= '*' && op
!= '/' && op
!= '%')
4457 n1
= get_tv_number_chk(rettv
, &error
);
4466 * Get the second variable.
4468 *arg
= skipwhite(*arg
+ 1);
4469 if (eval7(arg
, &var2
, evaluate
) == FAIL
)
4474 n2
= get_tv_number_chk(&var2
, &error
);
4480 * Compute the result.
4486 if (n2
== 0) /* give an error message? */
4493 if (n2
== 0) /* give an error message? */
4498 rettv
->v_type
= VAR_NUMBER
;
4499 rettv
->vval
.v_number
= n1
;
4507 * Handle sixth level expression:
4508 * number number constant
4509 * "string" string contstant
4510 * 'string' literal string contstant
4511 * &option-name option value
4512 * @r register contents
4513 * identifier variable value
4514 * function() function call
4515 * $VAR environment variable
4516 * (expression) nested expression
4518 * {key: val, key: val} Dictionary
4521 * ! in front logical NOT
4522 * - in front unary minus
4523 * + in front unary plus (ignored)
4524 * trailing [] subscript in String or List
4525 * trailing .name entry in Dictionary
4527 * "arg" must point to the first non-white of the expression.
4528 * "arg" is advanced to the next non-white after the recognized expression.
4530 * Return OK or FAIL.
4533 eval7(arg
, rettv
, evaluate
)
4542 char_u
*start_leader
, *end_leader
;
4547 * Initialise variable so that clear_tv() can't mistake this for a
4548 * string and free a string that isn't there.
4550 rettv
->v_type
= VAR_UNKNOWN
;
4553 * Skip '!' and '-' characters. They are handled later.
4555 start_leader
= *arg
;
4556 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4557 *arg
= skipwhite(*arg
+ 1);
4575 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4579 rettv
->v_type
= VAR_NUMBER
;
4580 rettv
->vval
.v_number
= n
;
4585 * String constant: "string".
4587 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4591 * Literal string constant: 'str''ing'.
4593 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4597 * List: [expr, expr]
4599 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4603 * Dictionary: {key: val, key: val}
4605 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4609 * Option value: &name
4611 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4615 * Environment variable: $VAR.
4617 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4621 * Register contents: @r.
4626 rettv
->v_type
= VAR_STRING
;
4627 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4634 * nested expression: (expression).
4636 case '(': *arg
= skipwhite(*arg
+ 1);
4637 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4642 EMSG(_("E110: Missing ')'"));
4648 default: ret
= NOTDONE
;
4655 * Must be a variable or function name.
4656 * Can also be a curly-braces kind of name: {expr}.
4659 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
4667 if (**arg
== '(') /* recursive! */
4669 /* If "s" is the name of a variable of type VAR_FUNC
4670 * use its contents. */
4671 s
= deref_func_name(s
, &len
);
4673 /* Invoke the function. */
4674 ret
= get_func_tv(s
, len
, rettv
, arg
,
4675 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
4676 &len
, evaluate
, NULL
);
4677 /* Stop the expression evaluation when immediately
4678 * aborting on error, or when an interrupt occurred or
4679 * an exception was thrown but not caught. */
4688 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
4697 *arg
= skipwhite(*arg
);
4699 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4702 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
4705 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4707 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
4711 val
= get_tv_number_chk(rettv
, &error
);
4719 while (end_leader
> start_leader
)
4722 if (*end_leader
== '!')
4724 else if (*end_leader
== '-')
4728 rettv
->v_type
= VAR_NUMBER
;
4729 rettv
->vval
.v_number
= val
;
4737 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4738 * "*arg" points to the '[' or '.'.
4739 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4742 eval_index(arg
, rettv
, evaluate
, verbose
)
4746 int verbose
; /* give error messages */
4748 int empty1
= FALSE
, empty2
= FALSE
;
4749 typval_T var1
, var2
;
4756 if (rettv
->v_type
== VAR_FUNC
)
4759 EMSG(_("E695: Cannot index a Funcref"));
4769 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
4773 *arg
= skipwhite(key
+ len
);
4780 * Get the (first) variable from inside the [].
4782 *arg
= skipwhite(*arg
+ 1);
4785 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
4787 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
4789 /* not a number or string */
4795 * Get the second variable from inside the [:].
4800 *arg
= skipwhite(*arg
+ 1);
4803 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
4809 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
4811 /* not a number or string */
4819 /* Check for the ']'. */
4823 EMSG(_(e_missbrac
));
4829 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
4835 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
4837 n1
= get_tv_number(&var1
);
4846 n2
= get_tv_number(&var2
);
4851 switch (rettv
->v_type
)
4855 s
= get_tv_string(rettv
);
4856 len
= (long)STRLEN(s
);
4859 /* The resulting variable is a substring. If the indexes
4860 * are out of range the result is empty. */
4871 if (n1
>= len
|| n2
< 0 || n1
> n2
)
4874 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
4878 /* The resulting variable is a string of a single
4879 * character. If the index is too big or negative the
4880 * result is empty. */
4881 if (n1
>= len
|| n1
< 0)
4884 s
= vim_strnsave(s
+ n1
, 1);
4887 rettv
->v_type
= VAR_STRING
;
4888 rettv
->vval
.v_string
= s
;
4892 len
= list_len(rettv
->vval
.v_list
);
4895 if (!empty1
&& (n1
< 0 || n1
>= len
))
4897 /* For a range we allow invalid values and return an empty
4898 * list. A list index out of range is an error. */
4902 EMSGN(_(e_listidx
), n1
);
4916 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
4921 for (item
= list_find(rettv
->vval
.v_list
, n1
);
4924 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
4929 item
= item
->li_next
;
4932 rettv
->v_type
= VAR_LIST
;
4933 rettv
->vval
.v_list
= l
;
4938 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
4948 EMSG(_(e_dictrange
));
4958 key
= get_tv_string(&var1
);
4962 EMSG(_(e_emptykey
));
4968 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
4970 if (item
== NULL
&& verbose
)
4971 EMSG2(_(e_dictkey
), key
);
4977 copy_tv(&item
->di_tv
, &var1
);
4989 * Get an option value.
4990 * "arg" points to the '&' or '+' before the option name.
4991 * "arg" is advanced to character after the option name.
4992 * Return OK or FAIL.
4995 get_option_tv(arg
, rettv
, evaluate
)
4997 typval_T
*rettv
; /* when NULL, only check if option exists */
5005 int working
= (**arg
== '+'); /* has("+option") */
5010 * Isolate the option name and find its value.
5012 option_end
= find_option_end(arg
, &opt_flags
);
5013 if (option_end
== NULL
)
5016 EMSG2(_("E112: Option name missing: %s"), *arg
);
5028 opt_type
= get_option_value(*arg
, &numval
,
5029 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5031 if (opt_type
== -3) /* invalid name */
5034 EMSG2(_("E113: Unknown option: %s"), *arg
);
5037 else if (rettv
!= NULL
)
5039 if (opt_type
== -2) /* hidden string option */
5041 rettv
->v_type
= VAR_STRING
;
5042 rettv
->vval
.v_string
= NULL
;
5044 else if (opt_type
== -1) /* hidden number option */
5046 rettv
->v_type
= VAR_NUMBER
;
5047 rettv
->vval
.v_number
= 0;
5049 else if (opt_type
== 1) /* number option */
5051 rettv
->v_type
= VAR_NUMBER
;
5052 rettv
->vval
.v_number
= numval
;
5054 else /* string option */
5056 rettv
->v_type
= VAR_STRING
;
5057 rettv
->vval
.v_string
= stringval
;
5060 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5063 *option_end
= c
; /* put back for error messages */
5070 * Allocate a variable for a string constant.
5071 * Return OK or FAIL.
5074 get_string_tv(arg
, rettv
, evaluate
)
5084 * Find the end of the string, skipping backslashed characters.
5086 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5088 if (*p
== '\\' && p
[1] != NUL
)
5091 /* A "\<x>" form occupies at least 4 characters, and produces up
5092 * to 6 characters: reserve space for 2 extra */
5100 EMSG2(_("E114: Missing quote: %s"), *arg
);
5104 /* If only parsing, set *arg and return here */
5112 * Copy the string into allocated memory, handling backslashed
5115 name
= alloc((unsigned)(p
- *arg
+ extra
));
5118 rettv
->v_type
= VAR_STRING
;
5119 rettv
->vval
.v_string
= name
;
5121 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5127 case 'b': *name
++ = BS
; ++p
; break;
5128 case 'e': *name
++ = ESC
; ++p
; break;
5129 case 'f': *name
++ = FF
; ++p
; break;
5130 case 'n': *name
++ = NL
; ++p
; break;
5131 case 'r': *name
++ = CAR
; ++p
; break;
5132 case 't': *name
++ = TAB
; ++p
; break;
5134 case 'X': /* hex: "\x1", "\x12" */
5136 case 'u': /* Unicode: "\u0023" */
5138 if (vim_isxdigit(p
[1]))
5141 int c
= toupper(*p
);
5148 while (--n
>= 0 && vim_isxdigit(p
[1]))
5151 nr
= (nr
<< 4) + hex2nr(*p
);
5155 /* For "\u" store the number according to
5158 name
+= (*mb_char2bytes
)(nr
, name
);
5165 /* octal: "\1", "\12", "\123" */
5173 case '7': *name
= *p
++ - '0';
5174 if (*p
>= '0' && *p
<= '7')
5176 *name
= (*name
<< 3) + *p
++ - '0';
5177 if (*p
>= '0' && *p
<= '7')
5178 *name
= (*name
<< 3) + *p
++ - '0';
5183 /* Special key, e.g.: "\<C-W>" */
5184 case '<': extra
= trans_special(&p
, name
, TRUE
);
5192 default: MB_COPY_CHAR(p
, name
);
5197 MB_COPY_CHAR(p
, name
);
5207 * Allocate a variable for a 'str''ing' constant.
5208 * Return OK or FAIL.
5211 get_lit_string_tv(arg
, rettv
, evaluate
)
5221 * Find the end of the string, skipping ''.
5223 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5236 EMSG2(_("E115: Missing quote: %s"), *arg
);
5240 /* If only parsing return after setting "*arg" */
5248 * Copy the string into allocated memory, handling '' to ' reduction.
5250 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5253 rettv
->v_type
= VAR_STRING
;
5254 rettv
->vval
.v_string
= str
;
5256 for (p
= *arg
+ 1; *p
!= NUL
; )
5264 MB_COPY_CHAR(p
, str
);
5273 * Allocate a variable for a List and fill it from "*arg".
5274 * Return OK or FAIL.
5277 get_list_tv(arg
, rettv
, evaluate
)
5293 *arg
= skipwhite(*arg
+ 1);
5294 while (**arg
!= ']' && **arg
!= NUL
)
5296 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5300 item
= listitem_alloc();
5304 item
->li_tv
.v_lock
= 0;
5305 list_append(l
, item
);
5315 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5318 *arg
= skipwhite(*arg
+ 1);
5323 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5330 *arg
= skipwhite(*arg
+ 1);
5333 rettv
->v_type
= VAR_LIST
;
5334 rettv
->vval
.v_list
= l
;
5342 * Allocate an empty header for a list.
5343 * Caller should take care of the reference count.
5350 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5353 /* Prepend the list to the list of lists for garbage collection. */
5354 if (first_list
!= NULL
)
5355 first_list
->lv_used_prev
= l
;
5356 l
->lv_used_prev
= NULL
;
5357 l
->lv_used_next
= first_list
;
5364 * Allocate an empty list for a return value.
5365 * Returns OK or FAIL.
5368 rettv_list_alloc(rettv
)
5371 list_T
*l
= list_alloc();
5376 rettv
->vval
.v_list
= l
;
5377 rettv
->v_type
= VAR_LIST
;
5383 * Unreference a list: decrement the reference count and free it when it
5390 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5395 * Free a list, including all items it points to.
5396 * Ignores the reference count.
5399 list_free(l
, recurse
)
5401 int recurse
; /* Free Lists and Dictionaries recursively. */
5405 /* Remove the list from the list of lists for garbage collection. */
5406 if (l
->lv_used_prev
== NULL
)
5407 first_list
= l
->lv_used_next
;
5409 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5410 if (l
->lv_used_next
!= NULL
)
5411 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5413 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5415 /* Remove the item before deleting it. */
5416 l
->lv_first
= item
->li_next
;
5417 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5418 && item
->li_tv
.v_type
!= VAR_DICT
))
5419 clear_tv(&item
->li_tv
);
5426 * Allocate a list item.
5431 return (listitem_T
*)alloc(sizeof(listitem_T
));
5435 * Free a list item. Also clears the value. Does not notify watchers.
5441 clear_tv(&item
->li_tv
);
5446 * Remove a list item from a List and free it. Also clears the value.
5449 listitem_remove(l
, item
)
5453 list_remove(l
, item
, item
);
5454 listitem_free(item
);
5458 * Get the number of items in a list.
5470 * Return TRUE when two lists have exactly the same values.
5473 list_equal(l1
, l2
, ic
)
5476 int ic
; /* ignore case for strings */
5478 listitem_T
*item1
, *item2
;
5482 if (list_len(l1
) != list_len(l2
))
5485 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5486 item1
!= NULL
&& item2
!= NULL
;
5487 item1
= item1
->li_next
, item2
= item2
->li_next
)
5488 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5490 return item1
== NULL
&& item2
== NULL
;
5493 #if defined(FEAT_PYTHON) || defined(PROTO)
5495 * Return the dictitem that an entry in a hashtable points to.
5506 * Return TRUE when two dictionaries have exactly the same key/values.
5509 dict_equal(d1
, d2
, ic
)
5512 int ic
; /* ignore case for strings */
5520 if (dict_len(d1
) != dict_len(d2
))
5523 todo
= (int)d1
->dv_hashtab
.ht_used
;
5524 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5526 if (!HASHITEM_EMPTY(hi
))
5528 item2
= dict_find(d2
, hi
->hi_key
, -1);
5531 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5540 * Return TRUE if "tv1" and "tv2" have the same value.
5541 * Compares the items just like "==" would compare them, but strings and
5542 * numbers are different.
5545 tv_equal(tv1
, tv2
, ic
)
5548 int ic
; /* ignore case */
5550 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5552 static int recursive
= 0; /* cach recursive loops */
5555 if (tv1
->v_type
!= tv2
->v_type
)
5557 /* Catch lists and dicts that have an endless loop by limiting
5558 * recursiveness to 1000. We guess they are equal then. */
5559 if (recursive
>= 1000)
5562 switch (tv1
->v_type
)
5566 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5572 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5577 return (tv1
->vval
.v_string
!= NULL
5578 && tv2
->vval
.v_string
!= NULL
5579 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5582 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5585 s1
= get_tv_string_buf(tv1
, buf1
);
5586 s2
= get_tv_string_buf(tv2
, buf2
);
5587 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5590 EMSG2(_(e_intern2
), "tv_equal()");
5595 * Locate item with index "n" in list "l" and return it.
5596 * A negative index is counted from the end; -1 is the last item.
5597 * Returns NULL when "n" is out of range.
5610 /* Negative index is relative to the end. */
5614 /* Check for index out of range. */
5615 if (n
< 0 || n
>= l
->lv_len
)
5618 /* When there is a cached index may start search from there. */
5619 if (l
->lv_idx_item
!= NULL
)
5621 if (n
< l
->lv_idx
/ 2)
5623 /* closest to the start of the list */
5627 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
5629 /* closest to the end of the list */
5631 idx
= l
->lv_len
- 1;
5635 /* closest to the cached index */
5636 item
= l
->lv_idx_item
;
5642 if (n
< l
->lv_len
/ 2)
5644 /* closest to the start of the list */
5650 /* closest to the end of the list */
5652 idx
= l
->lv_len
- 1;
5658 /* search forward */
5659 item
= item
->li_next
;
5664 /* search backward */
5665 item
= item
->li_prev
;
5669 /* cache the used index */
5671 l
->lv_idx_item
= item
;
5677 * Get list item "l[idx]" as a number.
5680 list_find_nr(l
, idx
, errorp
)
5683 int *errorp
; /* set to TRUE when something wrong */
5687 li
= list_find(l
, idx
);
5694 return get_tv_number_chk(&li
->li_tv
, errorp
);
5698 * Locate "item" list "l" and return its index.
5699 * Returns -1 when "item" is not in the list.
5702 list_idx_of_item(l
, item
)
5712 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
5720 * Append item "item" to the end of list "l".
5723 list_append(l
, item
)
5727 if (l
->lv_last
== NULL
)
5732 item
->li_prev
= NULL
;
5736 l
->lv_last
->li_next
= item
;
5737 item
->li_prev
= l
->lv_last
;
5741 item
->li_next
= NULL
;
5745 * Append typval_T "tv" to the end of list "l".
5746 * Return FAIL when out of memory.
5749 list_append_tv(l
, tv
)
5753 listitem_T
*li
= listitem_alloc();
5757 copy_tv(tv
, &li
->li_tv
);
5763 * Add a dictionary to a list. Used by getqflist().
5764 * Return FAIL when out of memory.
5767 list_append_dict(list
, dict
)
5771 listitem_T
*li
= listitem_alloc();
5775 li
->li_tv
.v_type
= VAR_DICT
;
5776 li
->li_tv
.v_lock
= 0;
5777 li
->li_tv
.vval
.v_dict
= dict
;
5778 list_append(list
, li
);
5779 ++dict
->dv_refcount
;
5784 * Make a copy of "str" and append it as an item to list "l".
5785 * When "len" >= 0 use "str[len]".
5786 * Returns FAIL when out of memory.
5789 list_append_string(l
, str
, len
)
5794 listitem_T
*li
= listitem_alloc();
5799 li
->li_tv
.v_type
= VAR_STRING
;
5800 li
->li_tv
.v_lock
= 0;
5802 li
->li_tv
.vval
.v_string
= NULL
;
5803 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
5804 : vim_strsave(str
))) == NULL
)
5810 * Append "n" to list "l".
5811 * Returns FAIL when out of memory.
5814 list_append_number(l
, n
)
5820 li
= listitem_alloc();
5823 li
->li_tv
.v_type
= VAR_NUMBER
;
5824 li
->li_tv
.v_lock
= 0;
5825 li
->li_tv
.vval
.v_number
= n
;
5831 * Insert typval_T "tv" in list "l" before "item".
5832 * If "item" is NULL append at the end.
5833 * Return FAIL when out of memory.
5836 list_insert_tv(l
, tv
, item
)
5841 listitem_T
*ni
= listitem_alloc();
5845 copy_tv(tv
, &ni
->li_tv
);
5847 /* Append new item at end of list. */
5851 /* Insert new item before existing item. */
5852 ni
->li_prev
= item
->li_prev
;
5854 if (item
->li_prev
== NULL
)
5861 item
->li_prev
->li_next
= ni
;
5862 l
->lv_idx_item
= NULL
;
5871 * Extend "l1" with "l2".
5872 * If "bef" is NULL append at the end, otherwise insert before this item.
5873 * Returns FAIL when out of memory.
5876 list_extend(l1
, l2
, bef
)
5883 for (item
= l2
->lv_first
; item
!= NULL
; item
= item
->li_next
)
5884 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
5890 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5891 * Return FAIL when out of memory.
5894 list_concat(l1
, l2
, tv
)
5901 /* make a copy of the first list. */
5902 l
= list_copy(l1
, FALSE
, 0);
5905 tv
->v_type
= VAR_LIST
;
5906 tv
->vval
.v_list
= l
;
5908 /* append all items from the second list */
5909 return list_extend(l
, l2
, NULL
);
5913 * Make a copy of list "orig". Shallow if "deep" is FALSE.
5914 * The refcount of the new list is set to 1.
5915 * See item_copy() for "copyID".
5916 * Returns NULL when out of memory.
5919 list_copy(orig
, deep
, copyID
)
5931 copy
= list_alloc();
5936 /* Do this before adding the items, because one of the items may
5937 * refer back to this list. */
5938 orig
->lv_copyID
= copyID
;
5939 orig
->lv_copylist
= copy
;
5941 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
5942 item
= item
->li_next
)
5944 ni
= listitem_alloc();
5949 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
5956 copy_tv(&item
->li_tv
, &ni
->li_tv
);
5957 list_append(copy
, ni
);
5959 ++copy
->lv_refcount
;
5971 * Remove items "item" to "item2" from list "l".
5972 * Does not free the listitem or the value!
5975 list_remove(l
, item
, item2
)
5982 /* notify watchers */
5983 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
5986 list_fix_watch(l
, ip
);
5991 if (item2
->li_next
== NULL
)
5992 l
->lv_last
= item
->li_prev
;
5994 item2
->li_next
->li_prev
= item
->li_prev
;
5995 if (item
->li_prev
== NULL
)
5996 l
->lv_first
= item2
->li_next
;
5998 item
->li_prev
->li_next
= item2
->li_next
;
5999 l
->lv_idx_item
= NULL
;
6003 * Return an allocated string with the string representation of a list.
6007 list2string(tv
, copyID
)
6013 if (tv
->vval
.v_list
== NULL
)
6015 ga_init2(&ga
, (int)sizeof(char), 80);
6016 ga_append(&ga
, '[');
6017 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6019 vim_free(ga
.ga_data
);
6022 ga_append(&ga
, ']');
6023 ga_append(&ga
, NUL
);
6024 return (char_u
*)ga
.ga_data
;
6028 * Join list "l" into a string in "*gap", using separator "sep".
6029 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6030 * Return FAIL or OK.
6033 list_join(gap
, l
, sep
, echo
, copyID
)
6042 char_u numbuf
[NUMBUFLEN
];
6046 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6051 ga_concat(gap
, sep
);
6054 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6056 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6067 * Garbage collection for lists and dictionaries.
6069 * We use reference counts to be able to free most items right away when they
6070 * are no longer used. But for composite items it's possible that it becomes
6071 * unused while the reference count is > 0: When there is a recursive
6072 * reference. Example:
6073 * :let l = [1, 2, 3]
6077 * Since this is quite unusual we handle this with garbage collection: every
6078 * once in a while find out which lists and dicts are not referenced from any
6081 * Here is a good reference text about garbage collection (refers to Python
6082 * but it applies to all reference-counting mechanisms):
6083 * http://python.ca/nas/python/gc/
6087 * Do garbage collection for lists and dicts.
6088 * Return TRUE if some memory was freed.
6095 int copyID
= ++current_copyID
;
6100 int did_free
= FALSE
;
6105 /* Only do this once. */
6106 want_garbage_collect
= FALSE
;
6107 may_garbage_collect
= FALSE
;
6110 * 1. Go through all accessible variables and mark all lists and dicts
6113 /* script-local variables */
6114 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6115 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6117 /* buffer-local variables */
6118 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6119 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6121 /* window-local variables */
6122 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6123 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6126 /* tabpage-local variables */
6127 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6128 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6131 /* global variables */
6132 set_ref_in_ht(&globvarht
, copyID
);
6134 /* function-local variables */
6135 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6137 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6138 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6142 * 2. Go through the list of dicts and free items without the copyID.
6144 for (dd
= first_dict
; dd
!= NULL
; )
6145 if (dd
->dv_copyID
!= copyID
)
6147 /* Free the Dictionary and ordinary items it contains, but don't
6148 * recurse into Lists and Dictionaries, they will be in the list
6149 * of dicts or list of lists. */
6150 dict_free(dd
, FALSE
);
6153 /* restart, next dict may also have been freed */
6157 dd
= dd
->dv_used_next
;
6160 * 3. Go through the list of lists and free items without the copyID.
6161 * But don't free a list that has a watcher (used in a for loop), these
6162 * are not referenced anywhere.
6164 for (ll
= first_list
; ll
!= NULL
; )
6165 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6167 /* Free the List and ordinary items it contains, but don't recurse
6168 * into Lists and Dictionaries, they will be in the list of dicts
6169 * or list of lists. */
6170 list_free(ll
, FALSE
);
6173 /* restart, next list may also have been freed */
6177 ll
= ll
->lv_used_next
;
6183 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6186 set_ref_in_ht(ht
, copyID
)
6193 todo
= (int)ht
->ht_used
;
6194 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6195 if (!HASHITEM_EMPTY(hi
))
6198 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6203 * Mark all lists and dicts referenced through list "l" with "copyID".
6206 set_ref_in_list(l
, copyID
)
6212 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6213 set_ref_in_item(&li
->li_tv
, copyID
);
6217 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6220 set_ref_in_item(tv
, copyID
)
6230 dd
= tv
->vval
.v_dict
;
6231 if (dd
->dv_copyID
!= copyID
)
6233 /* Didn't see this dict yet. */
6234 dd
->dv_copyID
= copyID
;
6235 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6240 ll
= tv
->vval
.v_list
;
6241 if (ll
->lv_copyID
!= copyID
)
6243 /* Didn't see this list yet. */
6244 ll
->lv_copyID
= copyID
;
6245 set_ref_in_list(ll
, copyID
);
6253 * Allocate an empty header for a dictionary.
6260 d
= (dict_T
*)alloc(sizeof(dict_T
));
6263 /* Add the list to the list of dicts for garbage collection. */
6264 if (first_dict
!= NULL
)
6265 first_dict
->dv_used_prev
= d
;
6266 d
->dv_used_next
= first_dict
;
6267 d
->dv_used_prev
= NULL
;
6270 hash_init(&d
->dv_hashtab
);
6279 * Unreference a Dictionary: decrement the reference count and free it when it
6286 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6291 * Free a Dictionary, including all items it contains.
6292 * Ignores the reference count.
6295 dict_free(d
, recurse
)
6297 int recurse
; /* Free Lists and Dictionaries recursively. */
6303 /* Remove the dict from the list of dicts for garbage collection. */
6304 if (d
->dv_used_prev
== NULL
)
6305 first_dict
= d
->dv_used_next
;
6307 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6308 if (d
->dv_used_next
!= NULL
)
6309 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6311 /* Lock the hashtab, we don't want it to resize while freeing items. */
6312 hash_lock(&d
->dv_hashtab
);
6313 todo
= (int)d
->dv_hashtab
.ht_used
;
6314 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6316 if (!HASHITEM_EMPTY(hi
))
6318 /* Remove the item before deleting it, just in case there is
6319 * something recursive causing trouble. */
6321 hash_remove(&d
->dv_hashtab
, hi
);
6322 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6323 && di
->di_tv
.v_type
!= VAR_DICT
))
6324 clear_tv(&di
->di_tv
);
6329 hash_clear(&d
->dv_hashtab
);
6334 * Allocate a Dictionary item.
6335 * The "key" is copied to the new item.
6336 * Note that the value of the item "di_tv" still needs to be initialized!
6337 * Returns NULL when out of memory.
6345 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6348 STRCPY(di
->di_key
, key
);
6355 * Make a copy of a Dictionary item.
6363 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6364 + STRLEN(org
->di_key
)));
6367 STRCPY(di
->di_key
, org
->di_key
);
6369 copy_tv(&org
->di_tv
, &di
->di_tv
);
6375 * Remove item "item" from Dictionary "dict" and free it.
6378 dictitem_remove(dict
, item
)
6384 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6385 if (HASHITEM_EMPTY(hi
))
6386 EMSG2(_(e_intern2
), "dictitem_remove()");
6388 hash_remove(&dict
->dv_hashtab
, hi
);
6389 dictitem_free(item
);
6393 * Free a dict item. Also clears the value.
6399 clear_tv(&item
->di_tv
);
6404 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6405 * The refcount of the new dict is set to 1.
6406 * See item_copy() for "copyID".
6407 * Returns NULL when out of memory.
6410 dict_copy(orig
, deep
, copyID
)
6423 copy
= dict_alloc();
6428 orig
->dv_copyID
= copyID
;
6429 orig
->dv_copydict
= copy
;
6431 todo
= (int)orig
->dv_hashtab
.ht_used
;
6432 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6434 if (!HASHITEM_EMPTY(hi
))
6438 di
= dictitem_alloc(hi
->hi_key
);
6443 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6451 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6452 if (dict_add(copy
, di
) == FAIL
)
6460 ++copy
->dv_refcount
;
6472 * Add item "item" to Dictionary "d".
6473 * Returns FAIL when out of memory and when key already existed.
6480 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6484 * Add a number or string entry to dictionary "d".
6485 * When "str" is NULL use number "nr", otherwise use "str".
6486 * Returns FAIL when out of memory and when key already exists.
6489 dict_add_nr_str(d
, key
, nr
, str
)
6497 item
= dictitem_alloc((char_u
*)key
);
6500 item
->di_tv
.v_lock
= 0;
6503 item
->di_tv
.v_type
= VAR_NUMBER
;
6504 item
->di_tv
.vval
.v_number
= nr
;
6508 item
->di_tv
.v_type
= VAR_STRING
;
6509 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6511 if (dict_add(d
, item
) == FAIL
)
6513 dictitem_free(item
);
6520 * Get the number of items in a Dictionary.
6528 return (long)d
->dv_hashtab
.ht_used
;
6532 * Find item "key[len]" in Dictionary "d".
6533 * If "len" is negative use strlen(key).
6534 * Returns NULL when not found.
6537 dict_find(d
, key
, len
)
6543 char_u buf
[AKEYLEN
];
6545 char_u
*tofree
= NULL
;
6550 else if (len
>= AKEYLEN
)
6552 tofree
= akey
= vim_strnsave(key
, len
);
6558 /* Avoid a malloc/free by using buf[]. */
6559 vim_strncpy(buf
, key
, len
);
6563 hi
= hash_find(&d
->dv_hashtab
, akey
);
6565 if (HASHITEM_EMPTY(hi
))
6571 * Get a string item from a dictionary.
6572 * When "save" is TRUE allocate memory for it.
6573 * Returns NULL if the entry doesn't exist or out of memory.
6576 get_dict_string(d
, key
, save
)
6584 di
= dict_find(d
, key
, -1);
6587 s
= get_tv_string(&di
->di_tv
);
6588 if (save
&& s
!= NULL
)
6594 * Get a number item from a dictionary.
6595 * Returns 0 if the entry doesn't exist or out of memory.
6598 get_dict_number(d
, key
)
6604 di
= dict_find(d
, key
, -1);
6607 return get_tv_number(&di
->di_tv
);
6611 * Return an allocated string with the string representation of a Dictionary.
6615 dict2string(tv
, copyID
)
6622 char_u numbuf
[NUMBUFLEN
];
6628 if ((d
= tv
->vval
.v_dict
) == NULL
)
6630 ga_init2(&ga
, (int)sizeof(char), 80);
6631 ga_append(&ga
, '{');
6633 todo
= (int)d
->dv_hashtab
.ht_used
;
6634 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6636 if (!HASHITEM_EMPTY(hi
))
6643 ga_concat(&ga
, (char_u
*)", ");
6645 tofree
= string_quote(hi
->hi_key
, FALSE
);
6648 ga_concat(&ga
, tofree
);
6651 ga_concat(&ga
, (char_u
*)": ");
6652 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
6662 vim_free(ga
.ga_data
);
6666 ga_append(&ga
, '}');
6667 ga_append(&ga
, NUL
);
6668 return (char_u
*)ga
.ga_data
;
6672 * Allocate a variable for a Dictionary and fill it from "*arg".
6673 * Return OK or FAIL. Returns NOTDONE for {expr}.
6676 get_dict_tv(arg
, rettv
, evaluate
)
6686 char_u
*start
= skipwhite(*arg
+ 1);
6687 char_u buf
[NUMBUFLEN
];
6690 * First check if it's not a curly-braces thing: {expr}.
6691 * Must do this without evaluating, otherwise a function may be called
6692 * twice. Unfortunately this means we need to call eval1() twice for the
6694 * But {} is an empty Dictionary.
6698 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
6710 tvkey
.v_type
= VAR_UNKNOWN
;
6711 tv
.v_type
= VAR_UNKNOWN
;
6713 *arg
= skipwhite(*arg
+ 1);
6714 while (**arg
!= '}' && **arg
!= NUL
)
6716 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
6720 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
6724 key
= get_tv_string_buf_chk(&tvkey
, buf
);
6725 if (key
== NULL
|| *key
== NUL
)
6727 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6729 EMSG(_(e_emptykey
));
6734 *arg
= skipwhite(*arg
+ 1);
6735 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
6742 item
= dict_find(d
, key
, -1);
6745 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
6750 item
= dictitem_alloc(key
);
6755 item
->di_tv
.v_lock
= 0;
6756 if (dict_add(d
, item
) == FAIL
)
6757 dictitem_free(item
);
6765 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
6768 *arg
= skipwhite(*arg
+ 1);
6773 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
6780 *arg
= skipwhite(*arg
+ 1);
6783 rettv
->v_type
= VAR_DICT
;
6784 rettv
->vval
.v_dict
= d
;
6792 * Return a string with the string representation of a variable.
6793 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6794 * "numbuf" is used for a number.
6795 * Does not put quotes around strings, as ":echo" displays values.
6796 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6800 echo_string(tv
, tofree
, numbuf
, copyID
)
6806 static int recurse
= 0;
6809 if (recurse
>= DICT_MAXNEST
)
6811 EMSG(_("E724: variable nested too deep for displaying"));
6821 r
= tv
->vval
.v_string
;
6825 if (tv
->vval
.v_list
== NULL
)
6830 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
6833 r
= (char_u
*)"[...]";
6837 tv
->vval
.v_list
->lv_copyID
= copyID
;
6838 *tofree
= list2string(tv
, copyID
);
6844 if (tv
->vval
.v_dict
== NULL
)
6849 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
6852 r
= (char_u
*)"{...}";
6856 tv
->vval
.v_dict
->dv_copyID
= copyID
;
6857 *tofree
= dict2string(tv
, copyID
);
6865 r
= get_tv_string_buf(tv
, numbuf
);
6869 EMSG2(_(e_intern2
), "echo_string()");
6878 * Return a string with the string representation of a variable.
6879 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6880 * "numbuf" is used for a number.
6881 * Puts quotes around strings, so that they can be parsed back by eval().
6885 tv2string(tv
, tofree
, numbuf
, copyID
)
6894 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
6897 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
6904 EMSG2(_(e_intern2
), "tv2string()");
6906 return echo_string(tv
, tofree
, numbuf
, copyID
);
6910 * Return string "str" in ' quotes, doubling ' characters.
6911 * If "str" is NULL an empty string is assumed.
6912 * If "function" is TRUE make it function('string').
6915 string_quote(str
, function
)
6922 len
= (function
? 13 : 3);
6925 len
+= (unsigned)STRLEN(str
);
6926 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
6935 STRCPY(r
, "function('");
6941 for (p
= str
; *p
!= NUL
; )
6956 * Get the value of an environment variable.
6957 * "arg" is pointing to the '$'. It is advanced to after the name.
6958 * If the environment variable was not set, silently assume it is empty.
6962 get_env_tv(arg
, rettv
, evaluate
)
6967 char_u
*string
= NULL
;
6971 int mustfree
= FALSE
;
6975 len
= get_env_len(arg
);
6982 /* first try vim_getenv(), fast for normal environment vars */
6983 string
= vim_getenv(name
, &mustfree
);
6984 if (string
!= NULL
&& *string
!= NUL
)
6987 string
= vim_strsave(string
);
6994 /* next try expanding things like $VIM and ${HOME} */
6995 string
= expand_env_save(name
- 1);
6996 if (string
!= NULL
&& *string
== '$')
7004 rettv
->v_type
= VAR_STRING
;
7005 rettv
->vval
.v_string
= string
;
7012 * Array with names and number of arguments of all internal functions
7013 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7017 char *f_name
; /* function name */
7018 char f_min_argc
; /* minimal number of arguments */
7019 char f_max_argc
; /* maximal number of arguments */
7020 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7021 /* implemenation of function */
7024 {"add", 2, 2, f_add
},
7025 {"append", 2, 2, f_append
},
7026 {"argc", 0, 0, f_argc
},
7027 {"argidx", 0, 0, f_argidx
},
7028 {"argv", 0, 1, f_argv
},
7029 {"browse", 4, 4, f_browse
},
7030 {"browsedir", 2, 2, f_browsedir
},
7031 {"bufexists", 1, 1, f_bufexists
},
7032 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7033 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7034 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7035 {"buflisted", 1, 1, f_buflisted
},
7036 {"bufloaded", 1, 1, f_bufloaded
},
7037 {"bufname", 1, 1, f_bufname
},
7038 {"bufnr", 1, 2, f_bufnr
},
7039 {"bufwinnr", 1, 1, f_bufwinnr
},
7040 {"byte2line", 1, 1, f_byte2line
},
7041 {"byteidx", 2, 2, f_byteidx
},
7042 {"call", 2, 3, f_call
},
7043 {"changenr", 0, 0, f_changenr
},
7044 {"char2nr", 1, 1, f_char2nr
},
7045 {"cindent", 1, 1, f_cindent
},
7046 {"col", 1, 1, f_col
},
7047 #if defined(FEAT_INS_EXPAND)
7048 {"complete", 2, 2, f_complete
},
7049 {"complete_add", 1, 1, f_complete_add
},
7050 {"complete_check", 0, 0, f_complete_check
},
7052 {"confirm", 1, 4, f_confirm
},
7053 {"copy", 1, 1, f_copy
},
7054 {"count", 2, 4, f_count
},
7055 {"cscope_connection",0,3, f_cscope_connection
},
7056 {"cursor", 1, 3, f_cursor
},
7057 {"deepcopy", 1, 2, f_deepcopy
},
7058 {"delete", 1, 1, f_delete
},
7059 {"did_filetype", 0, 0, f_did_filetype
},
7060 {"diff_filler", 1, 1, f_diff_filler
},
7061 {"diff_hlID", 2, 2, f_diff_hlID
},
7062 {"empty", 1, 1, f_empty
},
7063 {"escape", 2, 2, f_escape
},
7064 {"eval", 1, 1, f_eval
},
7065 {"eventhandler", 0, 0, f_eventhandler
},
7066 {"executable", 1, 1, f_executable
},
7067 {"exists", 1, 1, f_exists
},
7068 {"expand", 1, 2, f_expand
},
7069 {"extend", 2, 3, f_extend
},
7070 {"feedkeys", 1, 2, f_feedkeys
},
7071 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7072 {"filereadable", 1, 1, f_filereadable
},
7073 {"filewritable", 1, 1, f_filewritable
},
7074 {"filter", 2, 2, f_filter
},
7075 {"finddir", 1, 3, f_finddir
},
7076 {"findfile", 1, 3, f_findfile
},
7077 {"fnamemodify", 2, 2, f_fnamemodify
},
7078 {"foldclosed", 1, 1, f_foldclosed
},
7079 {"foldclosedend", 1, 1, f_foldclosedend
},
7080 {"foldlevel", 1, 1, f_foldlevel
},
7081 {"foldtext", 0, 0, f_foldtext
},
7082 {"foldtextresult", 1, 1, f_foldtextresult
},
7083 {"foreground", 0, 0, f_foreground
},
7084 {"function", 1, 1, f_function
},
7085 {"garbagecollect", 0, 0, f_garbagecollect
},
7086 {"get", 2, 3, f_get
},
7087 {"getbufline", 2, 3, f_getbufline
},
7088 {"getbufvar", 2, 2, f_getbufvar
},
7089 {"getchar", 0, 1, f_getchar
},
7090 {"getcharmod", 0, 0, f_getcharmod
},
7091 {"getcmdline", 0, 0, f_getcmdline
},
7092 {"getcmdpos", 0, 0, f_getcmdpos
},
7093 {"getcmdtype", 0, 0, f_getcmdtype
},
7094 {"getcwd", 0, 0, f_getcwd
},
7095 {"getfontname", 0, 1, f_getfontname
},
7096 {"getfperm", 1, 1, f_getfperm
},
7097 {"getfsize", 1, 1, f_getfsize
},
7098 {"getftime", 1, 1, f_getftime
},
7099 {"getftype", 1, 1, f_getftype
},
7100 {"getline", 1, 2, f_getline
},
7101 {"getloclist", 1, 1, f_getqflist
},
7102 {"getpos", 1, 1, f_getpos
},
7103 {"getqflist", 0, 0, f_getqflist
},
7104 {"getreg", 0, 2, f_getreg
},
7105 {"getregtype", 0, 1, f_getregtype
},
7106 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7107 {"getwinposx", 0, 0, f_getwinposx
},
7108 {"getwinposy", 0, 0, f_getwinposy
},
7109 {"getwinvar", 2, 2, f_getwinvar
},
7110 {"glob", 1, 1, f_glob
},
7111 {"globpath", 2, 2, f_globpath
},
7112 {"has", 1, 1, f_has
},
7113 {"has_key", 2, 2, f_has_key
},
7114 {"haslocaldir", 0, 0, f_haslocaldir
},
7115 {"hasmapto", 1, 3, f_hasmapto
},
7116 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7117 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7118 {"histadd", 2, 2, f_histadd
},
7119 {"histdel", 1, 2, f_histdel
},
7120 {"histget", 1, 2, f_histget
},
7121 {"histnr", 1, 1, f_histnr
},
7122 {"hlID", 1, 1, f_hlID
},
7123 {"hlexists", 1, 1, f_hlexists
},
7124 {"hostname", 0, 0, f_hostname
},
7125 {"iconv", 3, 3, f_iconv
},
7126 {"indent", 1, 1, f_indent
},
7127 {"index", 2, 4, f_index
},
7128 {"input", 1, 3, f_input
},
7129 {"inputdialog", 1, 3, f_inputdialog
},
7130 {"inputlist", 1, 1, f_inputlist
},
7131 {"inputrestore", 0, 0, f_inputrestore
},
7132 {"inputsave", 0, 0, f_inputsave
},
7133 {"inputsecret", 1, 2, f_inputsecret
},
7134 {"insert", 2, 3, f_insert
},
7135 {"isdirectory", 1, 1, f_isdirectory
},
7136 {"islocked", 1, 1, f_islocked
},
7137 {"items", 1, 1, f_items
},
7138 {"join", 1, 2, f_join
},
7139 {"keys", 1, 1, f_keys
},
7140 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7141 {"len", 1, 1, f_len
},
7142 {"libcall", 3, 3, f_libcall
},
7143 {"libcallnr", 3, 3, f_libcallnr
},
7144 {"line", 1, 1, f_line
},
7145 {"line2byte", 1, 1, f_line2byte
},
7146 {"lispindent", 1, 1, f_lispindent
},
7147 {"localtime", 0, 0, f_localtime
},
7148 {"map", 2, 2, f_map
},
7149 {"maparg", 1, 3, f_maparg
},
7150 {"mapcheck", 1, 3, f_mapcheck
},
7151 {"match", 2, 4, f_match
},
7152 {"matcharg", 1, 1, f_matcharg
},
7153 {"matchend", 2, 4, f_matchend
},
7154 {"matchlist", 2, 4, f_matchlist
},
7155 {"matchstr", 2, 4, f_matchstr
},
7156 {"max", 1, 1, f_max
},
7157 {"min", 1, 1, f_min
},
7159 {"mkdir", 1, 3, f_mkdir
},
7161 {"mode", 0, 0, f_mode
},
7162 {"nextnonblank", 1, 1, f_nextnonblank
},
7163 {"nr2char", 1, 1, f_nr2char
},
7164 {"pathshorten", 1, 1, f_pathshorten
},
7165 {"prevnonblank", 1, 1, f_prevnonblank
},
7166 {"printf", 2, 19, f_printf
},
7167 {"pumvisible", 0, 0, f_pumvisible
},
7168 {"range", 1, 3, f_range
},
7169 {"readfile", 1, 3, f_readfile
},
7170 {"reltime", 0, 2, f_reltime
},
7171 {"reltimestr", 1, 1, f_reltimestr
},
7172 {"remote_expr", 2, 3, f_remote_expr
},
7173 {"remote_foreground", 1, 1, f_remote_foreground
},
7174 {"remote_peek", 1, 2, f_remote_peek
},
7175 {"remote_read", 1, 1, f_remote_read
},
7176 {"remote_send", 2, 3, f_remote_send
},
7177 {"remove", 2, 3, f_remove
},
7178 {"rename", 2, 2, f_rename
},
7179 {"repeat", 2, 2, f_repeat
},
7180 {"resolve", 1, 1, f_resolve
},
7181 {"reverse", 1, 1, f_reverse
},
7182 {"search", 1, 3, f_search
},
7183 {"searchdecl", 1, 3, f_searchdecl
},
7184 {"searchpair", 3, 6, f_searchpair
},
7185 {"searchpairpos", 3, 6, f_searchpairpos
},
7186 {"searchpos", 1, 3, f_searchpos
},
7187 {"server2client", 2, 2, f_server2client
},
7188 {"serverlist", 0, 0, f_serverlist
},
7189 {"setbufvar", 3, 3, f_setbufvar
},
7190 {"setcmdpos", 1, 1, f_setcmdpos
},
7191 {"setline", 2, 2, f_setline
},
7192 {"setloclist", 2, 3, f_setloclist
},
7193 {"setpos", 2, 2, f_setpos
},
7194 {"setqflist", 1, 2, f_setqflist
},
7195 {"setreg", 2, 3, f_setreg
},
7196 {"settabwinvar", 4, 4, f_settabwinvar
},
7197 {"setwinvar", 3, 3, f_setwinvar
},
7198 {"shellescape", 1, 1, f_shellescape
},
7199 {"simplify", 1, 1, f_simplify
},
7200 {"sort", 1, 2, f_sort
},
7201 {"soundfold", 1, 1, f_soundfold
},
7202 {"spellbadword", 0, 1, f_spellbadword
},
7203 {"spellsuggest", 1, 3, f_spellsuggest
},
7204 {"split", 1, 3, f_split
},
7205 {"str2nr", 1, 2, f_str2nr
},
7206 #ifdef HAVE_STRFTIME
7207 {"strftime", 1, 2, f_strftime
},
7209 {"stridx", 2, 3, f_stridx
},
7210 {"string", 1, 1, f_string
},
7211 {"strlen", 1, 1, f_strlen
},
7212 {"strpart", 2, 3, f_strpart
},
7213 {"strridx", 2, 3, f_strridx
},
7214 {"strtrans", 1, 1, f_strtrans
},
7215 {"submatch", 1, 1, f_submatch
},
7216 {"substitute", 4, 4, f_substitute
},
7217 {"synID", 3, 3, f_synID
},
7218 {"synIDattr", 2, 3, f_synIDattr
},
7219 {"synIDtrans", 1, 1, f_synIDtrans
},
7220 {"system", 1, 2, f_system
},
7221 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7222 {"tabpagenr", 0, 1, f_tabpagenr
},
7223 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7224 {"tagfiles", 0, 0, f_tagfiles
},
7225 {"taglist", 1, 1, f_taglist
},
7226 {"tempname", 0, 0, f_tempname
},
7227 {"test", 1, 1, f_test
},
7228 {"tolower", 1, 1, f_tolower
},
7229 {"toupper", 1, 1, f_toupper
},
7231 {"type", 1, 1, f_type
},
7232 {"values", 1, 1, f_values
},
7233 {"virtcol", 1, 1, f_virtcol
},
7234 {"visualmode", 0, 1, f_visualmode
},
7235 {"winbufnr", 1, 1, f_winbufnr
},
7236 {"wincol", 0, 0, f_wincol
},
7237 {"winheight", 1, 1, f_winheight
},
7238 {"winline", 0, 0, f_winline
},
7239 {"winnr", 0, 1, f_winnr
},
7240 {"winrestcmd", 0, 0, f_winrestcmd
},
7241 {"winrestview", 1, 1, f_winrestview
},
7242 {"winsaveview", 0, 0, f_winsaveview
},
7243 {"winwidth", 1, 1, f_winwidth
},
7244 {"writefile", 2, 3, f_writefile
},
7247 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7250 * Function given to ExpandGeneric() to obtain the list of internal
7251 * or user defined function names.
7254 get_function_name(xp
, idx
)
7258 static int intidx
= -1;
7265 name
= get_user_func_name(xp
, idx
);
7269 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7271 STRCPY(IObuff
, functions
[intidx
].f_name
);
7272 STRCAT(IObuff
, "(");
7273 if (functions
[intidx
].f_max_argc
== 0)
7274 STRCAT(IObuff
, ")");
7282 * Function given to ExpandGeneric() to obtain the list of internal or
7283 * user defined variable or function names.
7287 get_expr_name(xp
, idx
)
7291 static int intidx
= -1;
7298 name
= get_function_name(xp
, idx
);
7302 return get_user_var_name(xp
, ++intidx
);
7305 #endif /* FEAT_CMDL_COMPL */
7308 * Find internal function in table above.
7309 * Return index, or -1 if not found
7312 find_internal_func(name
)
7313 char_u
*name
; /* name of the function */
7316 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7321 * Find the function name in the table. Binary search.
7323 while (first
<= last
)
7325 x
= first
+ ((unsigned)(last
- first
) >> 1);
7326 cmp
= STRCMP(name
, functions
[x
].f_name
);
7338 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7339 * name it contains, otherwise return "name".
7342 deref_func_name(name
, lenp
)
7351 v
= find_var(name
, NULL
);
7353 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7355 if (v
->di_tv
.vval
.v_string
== NULL
)
7358 return (char_u
*)""; /* just in case */
7360 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7361 return v
->di_tv
.vval
.v_string
;
7368 * Allocate a variable for the result of a function.
7369 * Return OK or FAIL.
7372 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7374 char_u
*name
; /* name of the function */
7375 int len
; /* length of "name" */
7377 char_u
**arg
; /* argument, pointing to the '(' */
7378 linenr_T firstline
; /* first line of range */
7379 linenr_T lastline
; /* last line of range */
7380 int *doesrange
; /* return: function handled range */
7382 dict_T
*selfdict
; /* Dictionary for "self" */
7386 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7387 int argcount
= 0; /* number of arguments found */
7390 * Get the arguments.
7393 while (argcount
< MAX_FUNC_ARGS
)
7395 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7396 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7398 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7413 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7414 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7415 else if (!aborting())
7417 if (argcount
== MAX_FUNC_ARGS
)
7418 emsg_funcname("E740: Too many arguments for function %s", name
);
7420 emsg_funcname("E116: Invalid arguments for function %s", name
);
7423 while (--argcount
>= 0)
7424 clear_tv(&argvars
[argcount
]);
7426 *arg
= skipwhite(argp
);
7432 * Call a function with its resolved parameters
7433 * Return OK when the function can't be called, FAIL otherwise.
7434 * Also returns OK when an error was encountered while executing the function.
7437 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7438 doesrange
, evaluate
, selfdict
)
7439 char_u
*name
; /* name of the function */
7440 int len
; /* length of "name" */
7441 typval_T
*rettv
; /* return value goes here */
7442 int argcount
; /* number of "argvars" */
7443 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7444 PLUS ONE elements! */
7445 linenr_T firstline
; /* first line of range */
7446 linenr_T lastline
; /* last line of range */
7447 int *doesrange
; /* return: function handled range */
7449 dict_T
*selfdict
; /* Dictionary for "self" */
7452 #define ERROR_UNKNOWN 0
7453 #define ERROR_TOOMANY 1
7454 #define ERROR_TOOFEW 2
7455 #define ERROR_SCRIPT 3
7456 #define ERROR_DICT 4
7457 #define ERROR_NONE 5
7458 #define ERROR_OTHER 6
7459 int error
= ERROR_NONE
;
7464 #define FLEN_FIXED 40
7465 char_u fname_buf
[FLEN_FIXED
+ 1];
7469 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7470 * Change <SNR>123_name() to K_SNR 123_name().
7471 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7475 llen
= eval_fname_script(name
);
7478 fname_buf
[0] = K_SPECIAL
;
7479 fname_buf
[1] = KS_EXTRA
;
7480 fname_buf
[2] = (int)KE_SNR
;
7482 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
7484 if (current_SID
<= 0)
7485 error
= ERROR_SCRIPT
;
7488 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
7489 i
= (int)STRLEN(fname_buf
);
7492 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
7494 STRCPY(fname_buf
+ i
, name
+ llen
);
7499 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
7501 error
= ERROR_OTHER
;
7504 mch_memmove(fname
, fname_buf
, (size_t)i
);
7505 STRCPY(fname
+ i
, name
+ llen
);
7515 /* execute the function if no errors detected and executing */
7516 if (evaluate
&& error
== ERROR_NONE
)
7518 rettv
->v_type
= VAR_NUMBER
; /* default is number rettv */
7519 error
= ERROR_UNKNOWN
;
7521 if (!builtin_function(fname
))
7524 * User defined function.
7526 fp
= find_func(fname
);
7529 /* Trigger FuncUndefined event, may load the function. */
7531 && apply_autocmds(EVENT_FUNCUNDEFINED
,
7532 fname
, fname
, TRUE
, NULL
)
7535 /* executed an autocommand, search for the function again */
7536 fp
= find_func(fname
);
7539 /* Try loading a package. */
7540 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
7542 /* loaded a package, search for the function again */
7543 fp
= find_func(fname
);
7548 if (fp
->uf_flags
& FC_RANGE
)
7550 if (argcount
< fp
->uf_args
.ga_len
)
7551 error
= ERROR_TOOFEW
;
7552 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
7553 error
= ERROR_TOOMANY
;
7554 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
7559 * Call the user function.
7560 * Save and restore search patterns, script variables and
7563 save_search_patterns();
7566 call_user_func(fp
, argcount
, argvars
, rettv
,
7567 firstline
, lastline
,
7568 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
7569 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
7570 && fp
->uf_refcount
<= 0)
7571 /* Function was unreferenced while being used, free it
7575 restore_search_patterns();
7583 * Find the function name in the table, call its implementation.
7585 i
= find_internal_func(fname
);
7588 if (argcount
< functions
[i
].f_min_argc
)
7589 error
= ERROR_TOOFEW
;
7590 else if (argcount
> functions
[i
].f_max_argc
)
7591 error
= ERROR_TOOMANY
;
7594 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
7595 functions
[i
].f_func(argvars
, rettv
);
7601 * The function call (or "FuncUndefined" autocommand sequence) might
7602 * have been aborted by an error, an interrupt, or an explicitly thrown
7603 * exception that has not been caught so far. This situation can be
7604 * tested for by calling aborting(). For an error in an internal
7605 * function or for the "E132" error in call_user_func(), however, the
7606 * throw point at which the "force_abort" flag (temporarily reset by
7607 * emsg()) is normally updated has not been reached yet. We need to
7608 * update that flag first to make aborting() reliable.
7610 update_force_abort();
7612 if (error
== ERROR_NONE
)
7616 * Report an error unless the argument evaluation or function call has been
7617 * cancelled due to an aborting error, an interrupt, or an exception.
7624 emsg_funcname(N_("E117: Unknown function: %s"), name
);
7627 emsg_funcname(e_toomanyarg
, name
);
7630 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
7634 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
7638 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
7645 if (fname
!= name
&& fname
!= fname_buf
)
7652 * Give an error message with a function name. Handle <SNR> things.
7655 emsg_funcname(ermsg
, name
)
7661 if (*name
== K_SPECIAL
)
7662 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
7670 /*********************************************
7671 * Implementation of the built-in functions
7675 * "add(list, item)" function
7678 f_add(argvars
, rettv
)
7684 rettv
->vval
.v_number
= 1; /* Default: Failed */
7685 if (argvars
[0].v_type
== VAR_LIST
)
7687 if ((l
= argvars
[0].vval
.v_list
) != NULL
7688 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
7689 && list_append_tv(l
, &argvars
[1]) == OK
)
7690 copy_tv(&argvars
[0], rettv
);
7697 * "append(lnum, string/list)" function
7700 f_append(argvars
, rettv
)
7707 listitem_T
*li
= NULL
;
7711 lnum
= get_tv_lnum(argvars
);
7713 && lnum
<= curbuf
->b_ml
.ml_line_count
7714 && u_save(lnum
, lnum
+ 1) == OK
)
7716 if (argvars
[1].v_type
== VAR_LIST
)
7718 l
= argvars
[1].vval
.v_list
;
7723 rettv
->vval
.v_number
= 0; /* Default: Success */
7727 tv
= &argvars
[1]; /* append a string */
7728 else if (li
== NULL
)
7729 break; /* end of list */
7731 tv
= &li
->li_tv
; /* append item from list */
7732 line
= get_tv_string_chk(tv
);
7733 if (line
== NULL
) /* type error */
7735 rettv
->vval
.v_number
= 1; /* Failed */
7738 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
7745 appended_lines_mark(lnum
, added
);
7746 if (curwin
->w_cursor
.lnum
> lnum
)
7747 curwin
->w_cursor
.lnum
+= added
;
7750 rettv
->vval
.v_number
= 1; /* Failed */
7758 f_argc(argvars
, rettv
)
7762 rettv
->vval
.v_number
= ARGCOUNT
;
7766 * "argidx()" function
7770 f_argidx(argvars
, rettv
)
7774 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
7778 * "argv(nr)" function
7781 f_argv(argvars
, rettv
)
7787 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
7789 idx
= get_tv_number_chk(&argvars
[0], NULL
);
7790 if (idx
>= 0 && idx
< ARGCOUNT
)
7791 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
7793 rettv
->vval
.v_string
= NULL
;
7794 rettv
->v_type
= VAR_STRING
;
7796 else if (rettv_list_alloc(rettv
) == OK
)
7797 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
7798 list_append_string(rettv
->vval
.v_list
,
7799 alist_name(&ARGLIST
[idx
]), -1);
7803 * "browse(save, title, initdir, default)" function
7807 f_browse(argvars
, rettv
)
7816 char_u buf
[NUMBUFLEN
];
7817 char_u buf2
[NUMBUFLEN
];
7820 save
= get_tv_number_chk(&argvars
[0], &error
);
7821 title
= get_tv_string_chk(&argvars
[1]);
7822 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
7823 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
7825 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
7826 rettv
->vval
.v_string
= NULL
;
7828 rettv
->vval
.v_string
=
7829 do_browse(save
? BROWSE_SAVE
: 0,
7830 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
7832 rettv
->vval
.v_string
= NULL
;
7834 rettv
->v_type
= VAR_STRING
;
7838 * "browsedir(title, initdir)" function
7842 f_browsedir(argvars
, rettv
)
7849 char_u buf
[NUMBUFLEN
];
7851 title
= get_tv_string_chk(&argvars
[0]);
7852 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
7854 if (title
== NULL
|| initdir
== NULL
)
7855 rettv
->vval
.v_string
= NULL
;
7857 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
7858 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
7860 rettv
->vval
.v_string
= NULL
;
7862 rettv
->v_type
= VAR_STRING
;
7865 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
7868 * Find a buffer by number or exact name.
7876 if (avar
->v_type
== VAR_NUMBER
)
7877 buf
= buflist_findnr((int)avar
->vval
.v_number
);
7878 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
7880 buf
= buflist_findname_exp(avar
->vval
.v_string
);
7883 /* No full path name match, try a match with a URL or a "nofile"
7884 * buffer, these don't use the full path. */
7885 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
7886 if (buf
->b_fname
!= NULL
7887 && (path_with_url(buf
->b_fname
)
7888 #ifdef FEAT_QUICKFIX
7892 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
7900 * "bufexists(expr)" function
7903 f_bufexists(argvars
, rettv
)
7907 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
7911 * "buflisted(expr)" function
7914 f_buflisted(argvars
, rettv
)
7920 buf
= find_buffer(&argvars
[0]);
7921 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
7925 * "bufloaded(expr)" function
7928 f_bufloaded(argvars
, rettv
)
7934 buf
= find_buffer(&argvars
[0]);
7935 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
7938 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
7941 * Get buffer by number or pattern.
7947 char_u
*name
= tv
->vval
.v_string
;
7952 if (tv
->v_type
== VAR_NUMBER
)
7953 return buflist_findnr((int)tv
->vval
.v_number
);
7954 if (tv
->v_type
!= VAR_STRING
)
7956 if (name
== NULL
|| *name
== NUL
)
7958 if (name
[0] == '$' && name
[1] == NUL
)
7961 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7962 save_magic
= p_magic
;
7965 p_cpo
= (char_u
*)"";
7967 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
7970 p_magic
= save_magic
;
7973 /* If not found, try expanding the name, like done for bufexists(). */
7975 buf
= find_buffer(tv
);
7981 * "bufname(expr)" function
7984 f_bufname(argvars
, rettv
)
7990 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
7992 buf
= get_buf_tv(&argvars
[0]);
7993 rettv
->v_type
= VAR_STRING
;
7994 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
7995 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
7997 rettv
->vval
.v_string
= NULL
;
8002 * "bufnr(expr)" function
8005 f_bufnr(argvars
, rettv
)
8013 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8015 buf
= get_buf_tv(&argvars
[0]);
8018 /* If the buffer isn't found and the second argument is not zero create a
8021 && argvars
[1].v_type
!= VAR_UNKNOWN
8022 && get_tv_number_chk(&argvars
[1], &error
) != 0
8024 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8026 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8029 rettv
->vval
.v_number
= buf
->b_fnum
;
8031 rettv
->vval
.v_number
= -1;
8035 * "bufwinnr(nr)" function
8038 f_bufwinnr(argvars
, rettv
)
8048 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8050 buf
= get_buf_tv(&argvars
[0]);
8052 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8055 if (wp
->w_buffer
== buf
)
8058 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8060 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8066 * "byte2line(byte)" function
8070 f_byte2line(argvars
, rettv
)
8074 #ifndef FEAT_BYTEOFF
8075 rettv
->vval
.v_number
= -1;
8079 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8081 rettv
->vval
.v_number
= -1;
8083 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8084 (linenr_T
)0, &boff
);
8089 * "byteidx()" function
8093 f_byteidx(argvars
, rettv
)
8103 str
= get_tv_string_chk(&argvars
[0]);
8104 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8105 rettv
->vval
.v_number
= -1;
8106 if (str
== NULL
|| idx
< 0)
8111 for ( ; idx
> 0; idx
--)
8113 if (*t
== NUL
) /* EOL reached */
8115 t
+= (*mb_ptr2len
)(t
);
8117 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8119 if (idx
<= STRLEN(str
))
8120 rettv
->vval
.v_number
= idx
;
8125 * "call(func, arglist)" function
8128 f_call(argvars
, rettv
)
8133 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8137 dict_T
*selfdict
= NULL
;
8139 rettv
->vval
.v_number
= 0;
8140 if (argvars
[1].v_type
!= VAR_LIST
)
8145 if (argvars
[1].vval
.v_list
== NULL
)
8148 if (argvars
[0].v_type
== VAR_FUNC
)
8149 func
= argvars
[0].vval
.v_string
;
8151 func
= get_tv_string(&argvars
[0]);
8153 return; /* type error or empty name */
8155 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8157 if (argvars
[2].v_type
!= VAR_DICT
)
8162 selfdict
= argvars
[2].vval
.v_dict
;
8165 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8166 item
= item
->li_next
)
8168 if (argc
== MAX_FUNC_ARGS
)
8170 EMSG(_("E699: Too many arguments"));
8173 /* Make a copy of each argument. This is needed to be able to set
8174 * v_lock to VAR_FIXED in the copy without changing the original list.
8176 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8180 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8181 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8182 &dummy
, TRUE
, selfdict
);
8184 /* Free the arguments. */
8186 clear_tv(&argv
[--argc
]);
8190 * "changenr()" function
8194 f_changenr(argvars
, rettv
)
8198 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8202 * "char2nr(string)" function
8205 f_char2nr(argvars
, rettv
)
8211 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8214 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8218 * "cindent(lnum)" function
8221 f_cindent(argvars
, rettv
)
8229 pos
= curwin
->w_cursor
;
8230 lnum
= get_tv_lnum(argvars
);
8231 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8233 curwin
->w_cursor
.lnum
= lnum
;
8234 rettv
->vval
.v_number
= get_c_indent();
8235 curwin
->w_cursor
= pos
;
8239 rettv
->vval
.v_number
= -1;
8243 * "col(string)" function
8246 f_col(argvars
, rettv
)
8252 int fnum
= curbuf
->b_fnum
;
8254 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8255 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8257 if (fp
->col
== MAXCOL
)
8259 /* '> can be MAXCOL, get the length of the line then */
8260 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8261 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8268 #ifdef FEAT_VIRTUALEDIT
8269 /* col(".") when the cursor is on the NUL at the end of the line
8270 * because of "coladd" can be seen as an extra column. */
8271 if (virtual_active() && fp
== &curwin
->w_cursor
)
8273 char_u
*p
= ml_get_cursor();
8275 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8276 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8281 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8284 if (*p
!= NUL
&& p
[1] == NUL
)
8292 rettv
->vval
.v_number
= col
;
8295 #if defined(FEAT_INS_EXPAND)
8297 * "complete()" function
8301 f_complete(argvars
, rettv
)
8307 if ((State
& INSERT
) == 0)
8309 EMSG(_("E785: complete() can only be used in Insert mode"));
8313 /* Check for undo allowed here, because if something was already inserted
8314 * the line was already saved for undo and this check isn't done. */
8315 if (!undo_allowed())
8318 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8324 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
8328 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
8332 * "complete_add()" function
8336 f_complete_add(argvars
, rettv
)
8340 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
8344 * "complete_check()" function
8348 f_complete_check(argvars
, rettv
)
8352 int saved
= RedrawingDisabled
;
8354 RedrawingDisabled
= 0;
8355 ins_compl_check_keys(0);
8356 rettv
->vval
.v_number
= compl_interrupted
;
8357 RedrawingDisabled
= saved
;
8362 * "confirm(message, buttons[, default [, type]])" function
8366 f_confirm(argvars
, rettv
)
8370 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8372 char_u
*buttons
= NULL
;
8373 char_u buf
[NUMBUFLEN
];
8374 char_u buf2
[NUMBUFLEN
];
8376 int type
= VIM_GENERIC
;
8380 message
= get_tv_string_chk(&argvars
[0]);
8381 if (message
== NULL
)
8383 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8385 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
8386 if (buttons
== NULL
)
8388 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8390 def
= get_tv_number_chk(&argvars
[2], &error
);
8391 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8393 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8394 if (typestr
== NULL
)
8398 switch (TOUPPER_ASC(*typestr
))
8400 case 'E': type
= VIM_ERROR
; break;
8401 case 'Q': type
= VIM_QUESTION
; break;
8402 case 'I': type
= VIM_INFO
; break;
8403 case 'W': type
= VIM_WARNING
; break;
8404 case 'G': type
= VIM_GENERIC
; break;
8411 if (buttons
== NULL
|| *buttons
== NUL
)
8412 buttons
= (char_u
*)_("&Ok");
8415 rettv
->vval
.v_number
= 0;
8417 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
8420 rettv
->vval
.v_number
= 0;
8428 f_copy(argvars
, rettv
)
8432 item_copy(&argvars
[0], rettv
, FALSE
, 0);
8436 * "count()" function
8439 f_count(argvars
, rettv
)
8446 if (argvars
[0].v_type
== VAR_LIST
)
8452 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
8455 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8459 ic
= get_tv_number_chk(&argvars
[2], &error
);
8460 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8462 idx
= get_tv_number_chk(&argvars
[3], &error
);
8465 li
= list_find(l
, idx
);
8467 EMSGN(_(e_listidx
), idx
);
8474 for ( ; li
!= NULL
; li
= li
->li_next
)
8475 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
8479 else if (argvars
[0].v_type
== VAR_DICT
)
8485 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
8489 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8491 ic
= get_tv_number_chk(&argvars
[2], &error
);
8492 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8496 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
8497 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
8499 if (!HASHITEM_EMPTY(hi
))
8502 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
8509 EMSG2(_(e_listdictarg
), "count()");
8510 rettv
->vval
.v_number
= n
;
8514 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8516 * Checks the existence of a cscope connection.
8520 f_cscope_connection(argvars
, rettv
)
8526 char_u
*dbpath
= NULL
;
8527 char_u
*prepend
= NULL
;
8528 char_u buf
[NUMBUFLEN
];
8530 if (argvars
[0].v_type
!= VAR_UNKNOWN
8531 && argvars
[1].v_type
!= VAR_UNKNOWN
)
8533 num
= (int)get_tv_number(&argvars
[0]);
8534 dbpath
= get_tv_string(&argvars
[1]);
8535 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8536 prepend
= get_tv_string_buf(&argvars
[2], buf
);
8539 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
8541 rettv
->vval
.v_number
= 0;
8546 * "cursor(lnum, col)" function
8548 * Moves the cursor to the specified line and column
8552 f_cursor(argvars
, rettv
)
8557 #ifdef FEAT_VIRTUALEDIT
8561 if (argvars
[1].v_type
== VAR_UNKNOWN
)
8565 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
8569 #ifdef FEAT_VIRTUALEDIT
8570 coladd
= pos
.coladd
;
8575 line
= get_tv_lnum(argvars
);
8576 col
= get_tv_number_chk(&argvars
[1], NULL
);
8577 #ifdef FEAT_VIRTUALEDIT
8578 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8579 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
8582 if (line
< 0 || col
< 0
8583 #ifdef FEAT_VIRTUALEDIT
8587 return; /* type error; errmsg already given */
8589 curwin
->w_cursor
.lnum
= line
;
8591 curwin
->w_cursor
.col
= col
- 1;
8592 #ifdef FEAT_VIRTUALEDIT
8593 curwin
->w_cursor
.coladd
= coladd
;
8596 /* Make sure the cursor is in a valid position. */
8599 /* Correct cursor for multi-byte character. */
8604 curwin
->w_set_curswant
= TRUE
;
8608 * "deepcopy()" function
8611 f_deepcopy(argvars
, rettv
)
8617 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8618 noref
= get_tv_number_chk(&argvars
[1], NULL
);
8619 if (noref
< 0 || noref
> 1)
8622 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
8626 * "delete()" function
8629 f_delete(argvars
, rettv
)
8633 if (check_restricted() || check_secure())
8634 rettv
->vval
.v_number
= -1;
8636 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
8640 * "did_filetype()" function
8644 f_did_filetype(argvars
, rettv
)
8649 rettv
->vval
.v_number
= did_filetype
;
8651 rettv
->vval
.v_number
= 0;
8656 * "diff_filler()" function
8660 f_diff_filler(argvars
, rettv
)
8665 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
8670 * "diff_hlID()" function
8674 f_diff_hlID(argvars
, rettv
)
8679 linenr_T lnum
= get_tv_lnum(argvars
);
8680 static linenr_T prev_lnum
= 0;
8681 static int changedtick
= 0;
8682 static int fnum
= 0;
8683 static int change_start
= 0;
8684 static int change_end
= 0;
8685 static hlf_T hlID
= 0;
8689 if (lnum
< 0) /* ignore type error in {lnum} arg */
8691 if (lnum
!= prev_lnum
8692 || changedtick
!= curbuf
->b_changedtick
8693 || fnum
!= curbuf
->b_fnum
)
8695 /* New line, buffer, change: need to get the values. */
8696 filler_lines
= diff_check(curwin
, lnum
);
8697 if (filler_lines
< 0)
8699 if (filler_lines
== -1)
8701 change_start
= MAXCOL
;
8703 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
8704 hlID
= HLF_ADD
; /* added line */
8706 hlID
= HLF_CHD
; /* changed line */
8709 hlID
= HLF_ADD
; /* added line */
8714 changedtick
= curbuf
->b_changedtick
;
8715 fnum
= curbuf
->b_fnum
;
8718 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
8720 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
8721 if (col
>= change_start
&& col
<= change_end
)
8722 hlID
= HLF_TXD
; /* changed text */
8724 hlID
= HLF_CHD
; /* changed line */
8726 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
8731 * "empty({expr})" function
8734 f_empty(argvars
, rettv
)
8740 switch (argvars
[0].v_type
)
8744 n
= argvars
[0].vval
.v_string
== NULL
8745 || *argvars
[0].vval
.v_string
== NUL
;
8748 n
= argvars
[0].vval
.v_number
== 0;
8751 n
= argvars
[0].vval
.v_list
== NULL
8752 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
8755 n
= argvars
[0].vval
.v_dict
== NULL
8756 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
8759 EMSG2(_(e_intern2
), "f_empty()");
8763 rettv
->vval
.v_number
= n
;
8767 * "escape({string}, {chars})" function
8770 f_escape(argvars
, rettv
)
8774 char_u buf
[NUMBUFLEN
];
8776 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
8777 get_tv_string_buf(&argvars
[1], buf
));
8778 rettv
->v_type
= VAR_STRING
;
8786 f_eval(argvars
, rettv
)
8792 s
= get_tv_string_chk(&argvars
[0]);
8796 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
8798 rettv
->v_type
= VAR_NUMBER
;
8799 rettv
->vval
.v_number
= 0;
8802 EMSG(_(e_trailing
));
8806 * "eventhandler()" function
8810 f_eventhandler(argvars
, rettv
)
8814 rettv
->vval
.v_number
= vgetc_busy
;
8818 * "executable()" function
8821 f_executable(argvars
, rettv
)
8825 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
8829 * "exists()" function
8832 f_exists(argvars
, rettv
)
8841 p
= get_tv_string(&argvars
[0]);
8842 if (*p
== '$') /* environment variable */
8844 /* first try "normal" environment variables (fast) */
8845 if (mch_getenv(p
+ 1) != NULL
)
8849 /* try expanding things like $VIM and ${HOME} */
8850 p
= expand_env_save(p
);
8851 if (p
!= NULL
&& *p
!= '$')
8856 else if (*p
== '&' || *p
== '+') /* option */
8858 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
8859 if (*skipwhite(p
) != NUL
)
8860 n
= FALSE
; /* trailing garbage */
8862 else if (*p
== '*') /* internal or user defined function */
8864 n
= function_exists(p
+ 1);
8868 n
= cmd_exists(p
+ 1);
8874 n
= autocmd_supported(p
+ 2);
8876 n
= au_exists(p
+ 1);
8879 else /* internal variable */
8884 /* get_name_len() takes care of expanding curly braces */
8886 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
8891 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
8894 /* handle d.key, l[idx], f(expr) */
8895 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
8906 rettv
->vval
.v_number
= n
;
8910 * "expand()" function
8913 f_expand(argvars
, rettv
)
8920 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
8924 rettv
->v_type
= VAR_STRING
;
8925 s
= get_tv_string(&argvars
[0]);
8926 if (*s
== '%' || *s
== '#' || *s
== '<')
8929 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
8934 /* When the optional second argument is non-zero, don't remove matches
8935 * for 'suffixes' and 'wildignore' */
8936 if (argvars
[1].v_type
!= VAR_UNKNOWN
8937 && get_tv_number_chk(&argvars
[1], &error
))
8938 flags
|= WILD_KEEP_ALL
;
8942 xpc
.xp_context
= EXPAND_FILES
;
8943 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
8946 rettv
->vval
.v_string
= NULL
;
8951 * "extend(list, list [, idx])" function
8952 * "extend(dict, dict [, action])" function
8955 f_extend(argvars
, rettv
)
8959 rettv
->vval
.v_number
= 0;
8960 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
8967 l1
= argvars
[0].vval
.v_list
;
8968 l2
= argvars
[1].vval
.v_list
;
8969 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
8972 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8974 before
= get_tv_number_chk(&argvars
[2], &error
);
8976 return; /* type error; errmsg already given */
8978 if (before
== l1
->lv_len
)
8982 item
= list_find(l1
, before
);
8985 EMSGN(_(e_listidx
), before
);
8992 list_extend(l1
, l2
, item
);
8994 copy_tv(&argvars
[0], rettv
);
8997 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9006 d1
= argvars
[0].vval
.v_dict
;
9007 d2
= argvars
[1].vval
.v_dict
;
9008 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9011 /* Check the third argument. */
9012 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9014 static char *(av
[]) = {"keep", "force", "error"};
9016 action
= get_tv_string_chk(&argvars
[2]);
9018 return; /* type error; errmsg already given */
9019 for (i
= 0; i
< 3; ++i
)
9020 if (STRCMP(action
, av
[i
]) == 0)
9024 EMSG2(_(e_invarg2
), action
);
9029 action
= (char_u
*)"force";
9031 /* Go over all entries in the second dict and add them to the
9033 todo
= (int)d2
->dv_hashtab
.ht_used
;
9034 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9036 if (!HASHITEM_EMPTY(hi2
))
9039 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9042 di1
= dictitem_copy(HI2DI(hi2
));
9043 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9046 else if (*action
== 'e')
9048 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9051 else if (*action
== 'f')
9053 clear_tv(&di1
->di_tv
);
9054 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9059 copy_tv(&argvars
[0], rettv
);
9063 EMSG2(_(e_listdictarg
), "extend()");
9067 * "feedkeys()" function
9071 f_feedkeys(argvars
, rettv
)
9076 char_u
*keys
, *flags
;
9077 char_u nbuf
[NUMBUFLEN
];
9081 /* This is not allowed in the sandbox. If the commands would still be
9082 * executed in the sandbox it would be OK, but it probably happens later,
9083 * when "sandbox" is no longer set. */
9087 rettv
->vval
.v_number
= 0;
9088 keys
= get_tv_string(&argvars
[0]);
9091 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9093 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9094 for ( ; *flags
!= NUL
; ++flags
)
9098 case 'n': remap
= FALSE
; break;
9099 case 'm': remap
= TRUE
; break;
9100 case 't': typed
= TRUE
; break;
9105 /* Need to escape K_SPECIAL and CSI before putting the string in the
9106 * typeahead buffer. */
9107 keys_esc
= vim_strsave_escape_csi(keys
);
9108 if (keys_esc
!= NULL
)
9110 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9111 typebuf
.tb_len
, !typed
, FALSE
);
9114 typebuf_was_filled
= TRUE
;
9120 * "filereadable()" function
9123 f_filereadable(argvars
, rettv
)
9131 p
= get_tv_string(&argvars
[0]);
9132 if (*p
&& !mch_isdir(p
) && (fd
= mch_fopen((char *)p
, "r")) != NULL
)
9140 rettv
->vval
.v_number
= n
;
9144 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9145 * rights to write into.
9148 f_filewritable(argvars
, rettv
)
9152 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9155 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int dir
));
9158 findfilendir(argvars
, rettv
, dir
)
9163 #ifdef FEAT_SEARCHPATH
9165 char_u
*fresult
= NULL
;
9166 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9168 char_u pathbuf
[NUMBUFLEN
];
9174 rettv
->vval
.v_string
= NULL
;
9175 rettv
->v_type
= VAR_STRING
;
9177 #ifdef FEAT_SEARCHPATH
9178 fname
= get_tv_string(&argvars
[0]);
9180 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9182 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9190 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9191 count
= get_tv_number_chk(&argvars
[2], &error
);
9195 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9198 if (*fname
!= NUL
&& !error
)
9202 if (rettv
->v_type
== VAR_STRING
)
9204 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9205 first
? (int)STRLEN(fname
) : 0,
9206 0, first
, path
, dir
, curbuf
->b_ffname
,
9207 dir
? (char_u
*)"" : curbuf
->b_p_sua
);
9210 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9211 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9213 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9216 if (rettv
->v_type
== VAR_STRING
)
9217 rettv
->vval
.v_string
= fresult
;
9221 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9222 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9225 * Implementation of map() and filter().
9228 filter_map(argvars
, rettv
, map
)
9233 char_u buf
[NUMBUFLEN
];
9235 listitem_T
*li
, *nli
;
9245 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9248 rettv
->vval
.v_number
= 0;
9249 if (argvars
[0].v_type
== VAR_LIST
)
9251 if ((l
= argvars
[0].vval
.v_list
) == NULL
9252 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9255 else if (argvars
[0].v_type
== VAR_DICT
)
9257 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9258 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9263 EMSG2(_(e_listdictarg
), ermsg
);
9267 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9268 /* On type errors, the preceding call has already displayed an error
9269 * message. Avoid a misleading error message for an empty string that
9270 * was not passed as argument. */
9273 prepare_vimvar(VV_VAL
, &save_val
);
9274 expr
= skipwhite(expr
);
9276 /* We reset "did_emsg" to be able to detect whether an error
9277 * occurred during evaluation of the expression. */
9278 save_did_emsg
= did_emsg
;
9281 if (argvars
[0].v_type
== VAR_DICT
)
9283 prepare_vimvar(VV_KEY
, &save_key
);
9284 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9286 ht
= &d
->dv_hashtab
;
9288 todo
= (int)ht
->ht_used
;
9289 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9291 if (!HASHITEM_EMPTY(hi
))
9295 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9297 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9298 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9302 dictitem_remove(d
, di
);
9303 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9308 restore_vimvar(VV_KEY
, &save_key
);
9312 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
9314 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
9317 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
9321 listitem_remove(l
, li
);
9325 restore_vimvar(VV_VAL
, &save_val
);
9327 did_emsg
|= save_did_emsg
;
9330 copy_tv(&argvars
[0], rettv
);
9334 filter_map_one(tv
, expr
, map
, remp
)
9343 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
9345 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
9347 if (*s
!= NUL
) /* check for trailing chars after expr */
9349 EMSG2(_(e_invexpr2
), s
);
9354 /* map(): replace the list item value */
9363 /* filter(): when expr is zero remove the item */
9364 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
9366 /* On type error, nothing has been removed; return FAIL to stop the
9367 * loop. The error message was given by get_tv_number_chk(). */
9371 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
9376 * "filter()" function
9379 f_filter(argvars
, rettv
)
9383 filter_map(argvars
, rettv
, FALSE
);
9387 * "finddir({fname}[, {path}[, {count}]])" function
9390 f_finddir(argvars
, rettv
)
9394 findfilendir(argvars
, rettv
, TRUE
);
9398 * "findfile({fname}[, {path}[, {count}]])" function
9401 f_findfile(argvars
, rettv
)
9405 findfilendir(argvars
, rettv
, FALSE
);
9409 * "fnamemodify({fname}, {mods})" function
9412 f_fnamemodify(argvars
, rettv
)
9420 char_u
*fbuf
= NULL
;
9421 char_u buf
[NUMBUFLEN
];
9423 fname
= get_tv_string_chk(&argvars
[0]);
9424 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
9425 if (fname
== NULL
|| mods
== NULL
)
9429 len
= (int)STRLEN(fname
);
9430 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
9433 rettv
->v_type
= VAR_STRING
;
9435 rettv
->vval
.v_string
= NULL
;
9437 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
9441 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
9444 * "foldclosed()" function
9447 foldclosed_both(argvars
, rettv
, end
)
9454 linenr_T first
, last
;
9456 lnum
= get_tv_lnum(argvars
);
9457 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9459 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
9462 rettv
->vval
.v_number
= (varnumber_T
)last
;
9464 rettv
->vval
.v_number
= (varnumber_T
)first
;
9469 rettv
->vval
.v_number
= -1;
9473 * "foldclosed()" function
9476 f_foldclosed(argvars
, rettv
)
9480 foldclosed_both(argvars
, rettv
, FALSE
);
9484 * "foldclosedend()" function
9487 f_foldclosedend(argvars
, rettv
)
9491 foldclosed_both(argvars
, rettv
, TRUE
);
9495 * "foldlevel()" function
9498 f_foldlevel(argvars
, rettv
)
9505 lnum
= get_tv_lnum(argvars
);
9506 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9507 rettv
->vval
.v_number
= foldLevel(lnum
);
9510 rettv
->vval
.v_number
= 0;
9514 * "foldtext()" function
9518 f_foldtext(argvars
, rettv
)
9530 rettv
->v_type
= VAR_STRING
;
9531 rettv
->vval
.v_string
= NULL
;
9533 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
9534 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
9535 <= curbuf
->b_ml
.ml_line_count
9536 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
9538 /* Find first non-empty line in the fold. */
9539 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
9540 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
9542 if (!linewhite(lnum
))
9547 /* Find interesting text in this line. */
9548 s
= skipwhite(ml_get(lnum
));
9549 /* skip C comment-start */
9550 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
9552 s
= skipwhite(s
+ 2);
9553 if (*skipwhite(s
) == NUL
9554 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
9556 s
= skipwhite(ml_get(lnum
+ 1));
9558 s
= skipwhite(s
+ 1);
9561 txt
= _("+-%s%3ld lines: ");
9562 r
= alloc((unsigned)(STRLEN(txt
)
9563 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
9565 + STRLEN(s
))); /* concatenated */
9568 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
9569 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
9570 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
9571 len
= (int)STRLEN(r
);
9573 /* remove 'foldmarker' and 'commentstring' */
9574 foldtext_cleanup(r
+ len
);
9575 rettv
->vval
.v_string
= r
;
9582 * "foldtextresult(lnum)" function
9586 f_foldtextresult(argvars
, rettv
)
9594 foldinfo_T foldinfo
;
9598 rettv
->v_type
= VAR_STRING
;
9599 rettv
->vval
.v_string
= NULL
;
9601 lnum
= get_tv_lnum(argvars
);
9602 /* treat illegal types and illegal string values for {lnum} the same */
9605 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
9608 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
9611 text
= vim_strsave(text
);
9612 rettv
->vval
.v_string
= text
;
9618 * "foreground()" function
9622 f_foreground(argvars
, rettv
)
9626 rettv
->vval
.v_number
= 0;
9629 gui_mch_set_foreground();
9632 win32_set_foreground();
9638 * "function()" function
9642 f_function(argvars
, rettv
)
9648 rettv
->vval
.v_number
= 0;
9649 s
= get_tv_string(&argvars
[0]);
9650 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
9651 EMSG2(_(e_invarg2
), s
);
9652 else if (!function_exists(s
))
9653 EMSG2(_("E700: Unknown function: %s"), s
);
9656 rettv
->vval
.v_string
= vim_strsave(s
);
9657 rettv
->v_type
= VAR_FUNC
;
9662 * "garbagecollect()" function
9666 f_garbagecollect(argvars
, rettv
)
9670 /* This is postponed until we are back at the toplevel, because we may be
9671 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9672 want_garbage_collect
= TRUE
;
9679 f_get(argvars
, rettv
)
9687 typval_T
*tv
= NULL
;
9689 if (argvars
[0].v_type
== VAR_LIST
)
9691 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9695 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
9696 if (!error
&& li
!= NULL
)
9700 else if (argvars
[0].v_type
== VAR_DICT
)
9702 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9704 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
9710 EMSG2(_(e_listdictarg
), "get()");
9714 if (argvars
[2].v_type
== VAR_UNKNOWN
)
9715 rettv
->vval
.v_number
= 0;
9717 copy_tv(&argvars
[2], rettv
);
9723 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
9726 * Get line or list of lines from buffer "buf" into "rettv".
9727 * Return a range (from start to end) of lines in rettv from the specified
9729 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
9732 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
9743 if (rettv_list_alloc(rettv
) == FAIL
)
9747 rettv
->vval
.v_number
= 0;
9749 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
9754 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
9755 p
= ml_get_buf(buf
, start
, FALSE
);
9759 rettv
->v_type
= VAR_STRING
;
9760 rettv
->vval
.v_string
= vim_strsave(p
);
9769 if (end
> buf
->b_ml
.ml_line_count
)
9770 end
= buf
->b_ml
.ml_line_count
;
9771 while (start
<= end
)
9772 if (list_append_string(rettv
->vval
.v_list
,
9773 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
9779 * "getbufline()" function
9782 f_getbufline(argvars
, rettv
)
9790 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
9792 buf
= get_buf_tv(&argvars
[0]);
9795 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
9796 if (argvars
[2].v_type
== VAR_UNKNOWN
)
9799 end
= get_tv_lnum_buf(&argvars
[2], buf
);
9801 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
9805 * "getbufvar()" function
9808 f_getbufvar(argvars
, rettv
)
9817 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
9818 varname
= get_tv_string_chk(&argvars
[1]);
9820 buf
= get_buf_tv(&argvars
[0]);
9822 rettv
->v_type
= VAR_STRING
;
9823 rettv
->vval
.v_string
= NULL
;
9825 if (buf
!= NULL
&& varname
!= NULL
)
9827 if (*varname
== '&') /* buffer-local-option */
9829 /* set curbuf to be our buf, temporarily */
9830 save_curbuf
= curbuf
;
9833 get_option_tv(&varname
, rettv
, TRUE
);
9835 /* restore previous notion of curbuf */
9836 curbuf
= save_curbuf
;
9840 if (*varname
== NUL
)
9841 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9842 * scope prefix before the NUL byte is required by
9843 * find_var_in_ht(). */
9844 varname
= (char_u
*)"b:" + 2;
9845 /* look up the variable */
9846 v
= find_var_in_ht(&buf
->b_vars
.dv_hashtab
, varname
, FALSE
);
9848 copy_tv(&v
->di_tv
, rettv
);
9856 * "getchar()" function
9859 f_getchar(argvars
, rettv
)
9866 /* Position the cursor. Needed after a message that ends in a space. */
9867 windgoto(msg_row
, msg_col
);
9871 if (argvars
[0].v_type
== VAR_UNKNOWN
)
9872 /* getchar(): blocking wait. */
9874 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
9875 /* getchar(1): only check if char avail */
9877 else if (error
|| vpeekc() == NUL
)
9878 /* illegal argument or getchar(0) and no char avail: return zero */
9881 /* getchar(0) and char avail: return char */
9886 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
9887 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
9888 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
9890 rettv
->vval
.v_number
= n
;
9891 if (IS_SPECIAL(n
) || mod_mask
!= 0)
9893 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9896 /* Turn a special key into three bytes, plus modifier. */
9899 temp
[i
++] = K_SPECIAL
;
9900 temp
[i
++] = KS_MODIFIER
;
9901 temp
[i
++] = mod_mask
;
9905 temp
[i
++] = K_SPECIAL
;
9906 temp
[i
++] = K_SECOND(n
);
9907 temp
[i
++] = K_THIRD(n
);
9911 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
9916 rettv
->v_type
= VAR_STRING
;
9917 rettv
->vval
.v_string
= vim_strsave(temp
);
9920 if (n
== K_LEFTMOUSE
9921 || n
== K_LEFTMOUSE_NM
9923 || n
== K_LEFTRELEASE
9924 || n
== K_LEFTRELEASE_NM
9925 || n
== K_MIDDLEMOUSE
9926 || n
== K_MIDDLEDRAG
9927 || n
== K_MIDDLERELEASE
9928 || n
== K_RIGHTMOUSE
9930 || n
== K_RIGHTRELEASE
9940 int row
= mouse_row
;
9941 int col
= mouse_col
;
9944 # ifdef FEAT_WINDOWS
9949 if (row
>= 0 && col
>= 0)
9951 /* Find the window at the mouse coordinates and compute the
9953 win
= mouse_find_win(&row
, &col
);
9954 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
9955 # ifdef FEAT_WINDOWS
9956 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
9959 vimvars
[VV_MOUSE_WIN
].vv_nr
= n
;
9960 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
9961 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
9969 * "getcharmod()" function
9973 f_getcharmod(argvars
, rettv
)
9977 rettv
->vval
.v_number
= mod_mask
;
9981 * "getcmdline()" function
9985 f_getcmdline(argvars
, rettv
)
9989 rettv
->v_type
= VAR_STRING
;
9990 rettv
->vval
.v_string
= get_cmdline_str();
9994 * "getcmdpos()" function
9998 f_getcmdpos(argvars
, rettv
)
10002 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10006 * "getcmdtype()" function
10010 f_getcmdtype(argvars
, rettv
)
10014 rettv
->v_type
= VAR_STRING
;
10015 rettv
->vval
.v_string
= alloc(2);
10016 if (rettv
->vval
.v_string
!= NULL
)
10018 rettv
->vval
.v_string
[0] = get_cmdline_type();
10019 rettv
->vval
.v_string
[1] = NUL
;
10024 * "getcwd()" function
10028 f_getcwd(argvars
, rettv
)
10032 char_u cwd
[MAXPATHL
];
10034 rettv
->v_type
= VAR_STRING
;
10035 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10036 rettv
->vval
.v_string
= NULL
;
10039 rettv
->vval
.v_string
= vim_strsave(cwd
);
10040 #ifdef BACKSLASH_IN_FILENAME
10041 if (rettv
->vval
.v_string
!= NULL
)
10042 slash_adjust(rettv
->vval
.v_string
);
10048 * "getfontname()" function
10052 f_getfontname(argvars
, rettv
)
10056 rettv
->v_type
= VAR_STRING
;
10057 rettv
->vval
.v_string
= NULL
;
10062 char_u
*name
= NULL
;
10064 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10066 /* Get the "Normal" font. Either the name saved by
10067 * hl_set_font_name() or from the font ID. */
10068 font
= gui
.norm_font
;
10069 name
= hl_get_font_name();
10073 name
= get_tv_string(&argvars
[0]);
10074 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10076 font
= gui_mch_get_font(name
, FALSE
);
10077 if (font
== NOFONT
)
10078 return; /* Invalid font name, return empty string. */
10080 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10081 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10082 gui_mch_free_font(font
);
10088 * "getfperm({fname})" function
10091 f_getfperm(argvars
, rettv
)
10097 char_u
*perm
= NULL
;
10098 char_u flags
[] = "rwx";
10101 fname
= get_tv_string(&argvars
[0]);
10103 rettv
->v_type
= VAR_STRING
;
10104 if (mch_stat((char *)fname
, &st
) >= 0)
10106 perm
= vim_strsave((char_u
*)"---------");
10109 for (i
= 0; i
< 9; i
++)
10111 if (st
.st_mode
& (1 << (8 - i
)))
10112 perm
[i
] = flags
[i
% 3];
10116 rettv
->vval
.v_string
= perm
;
10120 * "getfsize({fname})" function
10123 f_getfsize(argvars
, rettv
)
10130 fname
= get_tv_string(&argvars
[0]);
10132 rettv
->v_type
= VAR_NUMBER
;
10134 if (mch_stat((char *)fname
, &st
) >= 0)
10136 if (mch_isdir(fname
))
10137 rettv
->vval
.v_number
= 0;
10139 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10142 rettv
->vval
.v_number
= -1;
10146 * "getftime({fname})" function
10149 f_getftime(argvars
, rettv
)
10156 fname
= get_tv_string(&argvars
[0]);
10158 if (mch_stat((char *)fname
, &st
) >= 0)
10159 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10161 rettv
->vval
.v_number
= -1;
10165 * "getftype({fname})" function
10168 f_getftype(argvars
, rettv
)
10174 char_u
*type
= NULL
;
10177 fname
= get_tv_string(&argvars
[0]);
10179 rettv
->v_type
= VAR_STRING
;
10180 if (mch_lstat((char *)fname
, &st
) >= 0)
10183 if (S_ISREG(st
.st_mode
))
10185 else if (S_ISDIR(st
.st_mode
))
10188 else if (S_ISLNK(st
.st_mode
))
10192 else if (S_ISBLK(st
.st_mode
))
10196 else if (S_ISCHR(st
.st_mode
))
10200 else if (S_ISFIFO(st
.st_mode
))
10204 else if (S_ISSOCK(st
.st_mode
))
10211 switch (st
.st_mode
& S_IFMT
)
10213 case S_IFREG
: t
= "file"; break;
10214 case S_IFDIR
: t
= "dir"; break;
10216 case S_IFLNK
: t
= "link"; break;
10219 case S_IFBLK
: t
= "bdev"; break;
10222 case S_IFCHR
: t
= "cdev"; break;
10225 case S_IFIFO
: t
= "fifo"; break;
10228 case S_IFSOCK
: t
= "socket"; break;
10230 default: t
= "other";
10233 if (mch_isdir(fname
))
10239 type
= vim_strsave((char_u
*)t
);
10241 rettv
->vval
.v_string
= type
;
10245 * "getline(lnum, [end])" function
10248 f_getline(argvars
, rettv
)
10256 lnum
= get_tv_lnum(argvars
);
10257 if (argvars
[1].v_type
== VAR_UNKNOWN
)
10264 end
= get_tv_lnum(&argvars
[1]);
10268 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
10272 * "getpos(string)" function
10275 f_getpos(argvars
, rettv
)
10283 if (rettv_list_alloc(rettv
) == OK
)
10285 l
= rettv
->vval
.v_list
;
10286 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
10288 list_append_number(l
, (varnumber_T
)fnum
);
10290 list_append_number(l
, (varnumber_T
)0);
10291 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
10293 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->col
+ 1
10295 list_append_number(l
,
10296 #ifdef FEAT_VIRTUALEDIT
10297 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
10302 rettv
->vval
.v_number
= FALSE
;
10306 * "getqflist()" and "getloclist()" functions
10310 f_getqflist(argvars
, rettv
)
10314 #ifdef FEAT_QUICKFIX
10318 rettv
->vval
.v_number
= 0;
10319 #ifdef FEAT_QUICKFIX
10320 if (rettv_list_alloc(rettv
) == OK
)
10323 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
10325 wp
= find_win_by_nr(&argvars
[0], NULL
);
10330 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
10336 * "getreg()" function
10339 f_getreg(argvars
, rettv
)
10343 char_u
*strregname
;
10348 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10350 strregname
= get_tv_string_chk(&argvars
[0]);
10351 error
= strregname
== NULL
;
10352 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
10353 arg2
= get_tv_number_chk(&argvars
[1], &error
);
10356 strregname
= vimvars
[VV_REG
].vv_str
;
10357 regname
= (strregname
== NULL
? '"' : *strregname
);
10361 rettv
->v_type
= VAR_STRING
;
10362 rettv
->vval
.v_string
= error
? NULL
:
10363 get_reg_contents(regname
, TRUE
, arg2
);
10367 * "getregtype()" function
10370 f_getregtype(argvars
, rettv
)
10374 char_u
*strregname
;
10376 char_u buf
[NUMBUFLEN
+ 2];
10379 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10381 strregname
= get_tv_string_chk(&argvars
[0]);
10382 if (strregname
== NULL
) /* type error; errmsg already given */
10384 rettv
->v_type
= VAR_STRING
;
10385 rettv
->vval
.v_string
= NULL
;
10390 /* Default to v:register */
10391 strregname
= vimvars
[VV_REG
].vv_str
;
10393 regname
= (strregname
== NULL
? '"' : *strregname
);
10399 switch (get_reg_type(regname
, ®len
))
10401 case MLINE
: buf
[0] = 'V'; break;
10402 case MCHAR
: buf
[0] = 'v'; break;
10406 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
10410 rettv
->v_type
= VAR_STRING
;
10411 rettv
->vval
.v_string
= vim_strsave(buf
);
10415 * "gettabwinvar()" function
10418 f_gettabwinvar(argvars
, rettv
)
10422 getwinvar(argvars
, rettv
, 1);
10426 * "getwinposx()" function
10430 f_getwinposx(argvars
, rettv
)
10434 rettv
->vval
.v_number
= -1;
10440 if (gui_mch_get_winpos(&x
, &y
) == OK
)
10441 rettv
->vval
.v_number
= x
;
10447 * "getwinposy()" function
10451 f_getwinposy(argvars
, rettv
)
10455 rettv
->vval
.v_number
= -1;
10461 if (gui_mch_get_winpos(&x
, &y
) == OK
)
10462 rettv
->vval
.v_number
= y
;
10468 * Find window specifed by "vp" in tabpage "tp".
10471 find_win_by_nr(vp
, tp
)
10473 tabpage_T
*tp
; /* NULL for current tab page */
10475 #ifdef FEAT_WINDOWS
10480 nr
= get_tv_number_chk(vp
, NULL
);
10482 #ifdef FEAT_WINDOWS
10488 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
10489 wp
!= NULL
; wp
= wp
->w_next
)
10494 if (nr
== 0 || nr
== 1)
10501 * "getwinvar()" function
10504 f_getwinvar(argvars
, rettv
)
10508 getwinvar(argvars
, rettv
, 0);
10512 * getwinvar() and gettabwinvar()
10515 getwinvar(argvars
, rettv
, off
)
10518 int off
; /* 1 for gettabwinvar() */
10520 win_T
*win
, *oldcurwin
;
10525 #ifdef FEAT_WINDOWS
10527 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
10531 win
= find_win_by_nr(&argvars
[off
], tp
);
10532 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
10535 rettv
->v_type
= VAR_STRING
;
10536 rettv
->vval
.v_string
= NULL
;
10538 if (win
!= NULL
&& varname
!= NULL
)
10540 /* Set curwin to be our win, temporarily. Also set curbuf, so
10541 * that we can get buffer-local options. */
10542 oldcurwin
= curwin
;
10544 curbuf
= win
->w_buffer
;
10546 if (*varname
== '&') /* window-local-option */
10547 get_option_tv(&varname
, rettv
, 1);
10550 if (*varname
== NUL
)
10551 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10552 * scope prefix before the NUL byte is required by
10553 * find_var_in_ht(). */
10554 varname
= (char_u
*)"w:" + 2;
10555 /* look up the variable */
10556 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
10558 copy_tv(&v
->di_tv
, rettv
);
10561 /* restore previous notion of curwin */
10562 curwin
= oldcurwin
;
10563 curbuf
= curwin
->w_buffer
;
10570 * "glob()" function
10573 f_glob(argvars
, rettv
)
10580 xpc
.xp_context
= EXPAND_FILES
;
10581 rettv
->v_type
= VAR_STRING
;
10582 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
10583 NULL
, WILD_USE_NL
|WILD_SILENT
, WILD_ALL
);
10587 * "globpath()" function
10590 f_globpath(argvars
, rettv
)
10594 char_u buf1
[NUMBUFLEN
];
10595 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
10597 rettv
->v_type
= VAR_STRING
;
10599 rettv
->vval
.v_string
= NULL
;
10601 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
);
10608 f_has(argvars
, rettv
)
10615 static char *(has_list
[]) =
10636 #if defined(MACOS_X_UNIX)
10660 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10669 #ifndef CASE_INSENSITIVE_FILENAME
10675 #ifdef FEAT_AUTOCMD
10680 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10681 "balloon_multiline",
10684 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10686 # ifdef ALL_BUILTIN_TCAPS
10687 "all_builtin_terms",
10690 #ifdef FEAT_BYTEOFF
10693 #ifdef FEAT_CINDENT
10696 #ifdef FEAT_CLIENTSERVER
10699 #ifdef FEAT_CLIPBOARD
10702 #ifdef FEAT_CMDL_COMPL
10705 #ifdef FEAT_CMDHIST
10708 #ifdef FEAT_COMMENTS
10717 #ifdef CURSOR_SHAPE
10723 #ifdef FEAT_CON_DIALOG
10726 #ifdef FEAT_GUI_DIALOG
10732 #ifdef FEAT_DIGRAPHS
10738 #ifdef FEAT_EMACS_TAGS
10741 "eval", /* always present, of course! */
10742 #ifdef FEAT_EX_EXTRA
10745 #ifdef FEAT_SEARCH_EXTRA
10751 #ifdef FEAT_SEARCHPATH
10754 #if defined(UNIX) && !defined(USE_SYSTEM)
10757 #ifdef FEAT_FIND_ID
10760 #ifdef FEAT_FOLDING
10766 #if !defined(USE_SYSTEM) && defined(UNIX)
10769 #ifdef FEAT_GETTEXT
10775 #ifdef FEAT_GUI_ATHENA
10776 # ifdef FEAT_GUI_NEXTAW
10782 #ifdef FEAT_GUI_GTK
10788 #ifdef FEAT_GUI_MAC
10791 #ifdef FEAT_GUI_MOTIF
10794 #ifdef FEAT_GUI_PHOTON
10797 #ifdef FEAT_GUI_W16
10800 #ifdef FEAT_GUI_W32
10803 #ifdef FEAT_HANGULIN
10806 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10809 #ifdef FEAT_INS_EXPAND
10812 #ifdef FEAT_JUMPLIST
10818 #ifdef FEAT_LANGMAP
10821 #ifdef FEAT_LIBCALL
10824 #ifdef FEAT_LINEBREAK
10830 #ifdef FEAT_LISTCMDS
10833 #ifdef FEAT_LOCALMAP
10839 #ifdef FEAT_SESSION
10842 #ifdef FEAT_MODIFY_FNAME
10848 #ifdef FEAT_MOUSESHAPE
10851 #if defined(UNIX) || defined(VMS)
10852 # ifdef FEAT_MOUSE_DEC
10855 # ifdef FEAT_MOUSE_GPM
10858 # ifdef FEAT_MOUSE_JSB
10861 # ifdef FEAT_MOUSE_NET
10864 # ifdef FEAT_MOUSE_PTERM
10867 # ifdef FEAT_MOUSE_XTERM
10874 #ifdef FEAT_MBYTE_IME
10877 #ifdef FEAT_MULTI_LANG
10880 #ifdef FEAT_MZSCHEME
10881 #ifndef DYNAMIC_MZSCHEME
10888 #ifdef FEAT_OSFILETYPE
10891 #ifdef FEAT_PATH_EXTRA
10895 #ifndef DYNAMIC_PERL
10900 #ifndef DYNAMIC_PYTHON
10904 #ifdef FEAT_POSTSCRIPT
10907 #ifdef FEAT_PRINTER
10910 #ifdef FEAT_PROFILE
10913 #ifdef FEAT_RELTIME
10916 #ifdef FEAT_QUICKFIX
10919 #ifdef FEAT_RIGHTLEFT
10922 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10925 #ifdef FEAT_SCROLLBIND
10928 #ifdef FEAT_CMDL_INFO
10935 #ifdef FEAT_SMARTINDENT
10941 #ifdef FEAT_STL_OPT
10944 #ifdef FEAT_SUN_WORKSHOP
10947 #ifdef FEAT_NETBEANS_INTG
10956 #if defined(USE_SYSTEM) || !defined(UNIX)
10959 #ifdef FEAT_TAG_BINS
10962 #ifdef FEAT_TAG_OLDSTATIC
10965 #ifdef FEAT_TAG_ANYWHITE
10969 # ifndef DYNAMIC_TCL
10976 #ifdef FEAT_TERMRESPONSE
10979 #ifdef FEAT_TEXTOBJ
10982 #ifdef HAVE_TGETENT
10988 #ifdef FEAT_TOOLBAR
10991 #ifdef FEAT_USR_CMDS
10992 "user-commands", /* was accidentally included in 5.4 */
10995 #ifdef FEAT_VIMINFO
10998 #ifdef FEAT_VERTSPLIT
11001 #ifdef FEAT_VIRTUALEDIT
11007 #ifdef FEAT_VISUALEXTRA
11010 #ifdef FEAT_VREPLACE
11013 #ifdef FEAT_WILDIGN
11016 #ifdef FEAT_WILDMENU
11019 #ifdef FEAT_WINDOWS
11025 #ifdef FEAT_WRITEBACKUP
11031 #ifdef FEAT_XFONTSET
11037 #ifdef USE_XSMP_INTERACT
11040 #ifdef FEAT_XCLIPBOARD
11043 #ifdef FEAT_XTERM_SAVE
11046 #if defined(UNIX) && defined(FEAT_X11)
11052 name
= get_tv_string(&argvars
[0]);
11053 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11054 if (STRICMP(name
, has_list
[i
]) == 0)
11062 if (STRNICMP(name
, "patch", 5) == 0)
11063 n
= has_patch(atoi((char *)name
+ 5));
11064 else if (STRICMP(name
, "vim_starting") == 0)
11065 n
= (starting
!= 0);
11066 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11067 else if (STRICMP(name
, "balloon_multiline") == 0)
11068 n
= multiline_balloon_available();
11071 else if (STRICMP(name
, "tcl") == 0)
11072 n
= tcl_enabled(FALSE
);
11074 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11075 else if (STRICMP(name
, "iconv") == 0)
11076 n
= iconv_enabled(FALSE
);
11078 #ifdef DYNAMIC_MZSCHEME
11079 else if (STRICMP(name
, "mzscheme") == 0)
11080 n
= mzscheme_enabled(FALSE
);
11082 #ifdef DYNAMIC_RUBY
11083 else if (STRICMP(name
, "ruby") == 0)
11084 n
= ruby_enabled(FALSE
);
11086 #ifdef DYNAMIC_PYTHON
11087 else if (STRICMP(name
, "python") == 0)
11088 n
= python_enabled(FALSE
);
11090 #ifdef DYNAMIC_PERL
11091 else if (STRICMP(name
, "perl") == 0)
11092 n
= perl_enabled(FALSE
);
11095 else if (STRICMP(name
, "gui_running") == 0)
11096 n
= (gui
.in_use
|| gui
.starting
);
11097 # ifdef FEAT_GUI_W32
11098 else if (STRICMP(name
, "gui_win32s") == 0)
11099 n
= gui_is_win32s();
11101 # ifdef FEAT_BROWSE
11102 else if (STRICMP(name
, "browse") == 0)
11103 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11107 else if (STRICMP(name
, "syntax_items") == 0)
11108 n
= syntax_present(curbuf
);
11110 #if defined(WIN3264)
11111 else if (STRICMP(name
, "win95") == 0)
11112 n
= mch_windows95();
11114 #ifdef FEAT_NETBEANS_INTG
11115 else if (STRICMP(name
, "netbeans_enabled") == 0)
11120 rettv
->vval
.v_number
= n
;
11124 * "has_key()" function
11127 f_has_key(argvars
, rettv
)
11131 rettv
->vval
.v_number
= 0;
11132 if (argvars
[0].v_type
!= VAR_DICT
)
11134 EMSG(_(e_dictreq
));
11137 if (argvars
[0].vval
.v_dict
== NULL
)
11140 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11141 get_tv_string(&argvars
[1]), -1) != NULL
;
11145 * "haslocaldir()" function
11149 f_haslocaldir(argvars
, rettv
)
11153 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11157 * "hasmapto()" function
11160 f_hasmapto(argvars
, rettv
)
11166 char_u buf
[NUMBUFLEN
];
11169 name
= get_tv_string(&argvars
[0]);
11170 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11171 mode
= (char_u
*)"nvo";
11174 mode
= get_tv_string_buf(&argvars
[1], buf
);
11175 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11176 abbr
= get_tv_number(&argvars
[2]);
11179 if (map_to_exists(name
, mode
, abbr
))
11180 rettv
->vval
.v_number
= TRUE
;
11182 rettv
->vval
.v_number
= FALSE
;
11186 * "histadd()" function
11190 f_histadd(argvars
, rettv
)
11194 #ifdef FEAT_CMDHIST
11197 char_u buf
[NUMBUFLEN
];
11200 rettv
->vval
.v_number
= FALSE
;
11201 if (check_restricted() || check_secure())
11203 #ifdef FEAT_CMDHIST
11204 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11205 histype
= str
!= NULL
? get_histtype(str
) : -1;
11208 str
= get_tv_string_buf(&argvars
[1], buf
);
11211 add_to_history(histype
, str
, FALSE
, NUL
);
11212 rettv
->vval
.v_number
= TRUE
;
11220 * "histdel()" function
11224 f_histdel(argvars
, rettv
)
11228 #ifdef FEAT_CMDHIST
11230 char_u buf
[NUMBUFLEN
];
11233 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11236 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
11237 /* only one argument: clear entire history */
11238 n
= clr_history(get_histtype(str
));
11239 else if (argvars
[1].v_type
== VAR_NUMBER
)
11240 /* index given: remove that entry */
11241 n
= del_history_idx(get_histtype(str
),
11242 (int)get_tv_number(&argvars
[1]));
11244 /* string given: remove all matching entries */
11245 n
= del_history_entry(get_histtype(str
),
11246 get_tv_string_buf(&argvars
[1], buf
));
11247 rettv
->vval
.v_number
= n
;
11249 rettv
->vval
.v_number
= 0;
11254 * "histget()" function
11258 f_histget(argvars
, rettv
)
11262 #ifdef FEAT_CMDHIST
11267 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11269 rettv
->vval
.v_string
= NULL
;
11272 type
= get_histtype(str
);
11273 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11274 idx
= get_history_idx(type
);
11276 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
11277 /* -1 on type error */
11278 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
11281 rettv
->vval
.v_string
= NULL
;
11283 rettv
->v_type
= VAR_STRING
;
11287 * "histnr()" function
11291 f_histnr(argvars
, rettv
)
11297 #ifdef FEAT_CMDHIST
11298 char_u
*history
= get_tv_string_chk(&argvars
[0]);
11300 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
11301 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
11302 i
= get_history_idx(i
);
11306 rettv
->vval
.v_number
= i
;
11310 * "highlightID(name)" function
11313 f_hlID(argvars
, rettv
)
11317 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
11321 * "highlight_exists()" function
11324 f_hlexists(argvars
, rettv
)
11328 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
11332 * "hostname()" function
11336 f_hostname(argvars
, rettv
)
11340 char_u hostname
[256];
11342 mch_get_host_name(hostname
, 256);
11343 rettv
->v_type
= VAR_STRING
;
11344 rettv
->vval
.v_string
= vim_strsave(hostname
);
11352 f_iconv(argvars
, rettv
)
11357 char_u buf1
[NUMBUFLEN
];
11358 char_u buf2
[NUMBUFLEN
];
11359 char_u
*from
, *to
, *str
;
11363 rettv
->v_type
= VAR_STRING
;
11364 rettv
->vval
.v_string
= NULL
;
11367 str
= get_tv_string(&argvars
[0]);
11368 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
11369 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
11370 vimconv
.vc_type
= CONV_NONE
;
11371 convert_setup(&vimconv
, from
, to
);
11373 /* If the encodings are equal, no conversion needed. */
11374 if (vimconv
.vc_type
== CONV_NONE
)
11375 rettv
->vval
.v_string
= vim_strsave(str
);
11377 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
11379 convert_setup(&vimconv
, NULL
, NULL
);
11386 * "indent()" function
11389 f_indent(argvars
, rettv
)
11395 lnum
= get_tv_lnum(argvars
);
11396 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
11397 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
11399 rettv
->vval
.v_number
= -1;
11403 * "index()" function
11406 f_index(argvars
, rettv
)
11415 rettv
->vval
.v_number
= -1;
11416 if (argvars
[0].v_type
!= VAR_LIST
)
11418 EMSG(_(e_listreq
));
11421 l
= argvars
[0].vval
.v_list
;
11424 item
= l
->lv_first
;
11425 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11429 /* Start at specified item. Use the cached index that list_find()
11430 * sets, so that a negative number also works. */
11431 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
11433 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
11434 ic
= get_tv_number_chk(&argvars
[3], &error
);
11439 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
11440 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
11442 rettv
->vval
.v_number
= idx
;
11448 static int inputsecret_flag
= 0;
11450 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
11453 * This function is used by f_input() and f_inputdialog() functions. The third
11454 * argument to f_input() specifies the type of completion to use at the
11455 * prompt. The third argument to f_inputdialog() specifies the value to return
11456 * when the user cancels the prompt.
11459 get_user_input(argvars
, rettv
, inputdialog
)
11464 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
11467 char_u buf
[NUMBUFLEN
];
11468 int cmd_silent_save
= cmd_silent
;
11469 char_u
*defstr
= (char_u
*)"";
11470 int xp_type
= EXPAND_NOTHING
;
11471 char_u
*xp_arg
= NULL
;
11473 rettv
->v_type
= VAR_STRING
;
11475 #ifdef NO_CONSOLE_INPUT
11476 /* While starting up, there is no place to enter text. */
11477 if (no_console_input())
11479 rettv
->vval
.v_string
= NULL
;
11484 cmd_silent
= FALSE
; /* Want to see the prompt. */
11485 if (prompt
!= NULL
)
11487 /* Only the part of the message after the last NL is considered as
11488 * prompt for the command line */
11489 p
= vim_strrchr(prompt
, '\n');
11499 msg_puts_attr(prompt
, echo_attr
);
11500 msg_didout
= FALSE
;
11504 cmdline_row
= msg_row
;
11506 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11508 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
11509 if (defstr
!= NULL
)
11510 stuffReadbuffSpec(defstr
);
11512 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
11518 rettv
->vval
.v_string
= NULL
;
11520 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
11521 if (xp_name
== NULL
)
11524 xp_namelen
= (int)STRLEN(xp_name
);
11526 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
11532 if (defstr
!= NULL
)
11533 rettv
->vval
.v_string
=
11534 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
11539 /* since the user typed this, no need to wait for return */
11540 need_wait_return
= FALSE
;
11541 msg_didout
= FALSE
;
11543 cmd_silent
= cmd_silent_save
;
11547 * "input()" function
11548 * Also handles inputsecret() when inputsecret is set.
11551 f_input(argvars
, rettv
)
11555 get_user_input(argvars
, rettv
, FALSE
);
11559 * "inputdialog()" function
11562 f_inputdialog(argvars
, rettv
)
11566 #if defined(FEAT_GUI_TEXTDIALOG)
11567 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11568 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
11571 char_u buf
[NUMBUFLEN
];
11572 char_u
*defstr
= (char_u
*)"";
11574 message
= get_tv_string_chk(&argvars
[0]);
11575 if (argvars
[1].v_type
!= VAR_UNKNOWN
11576 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
11577 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
11580 if (message
!= NULL
&& defstr
!= NULL
11581 && do_dialog(VIM_QUESTION
, NULL
, message
,
11582 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
11583 rettv
->vval
.v_string
= vim_strsave(IObuff
);
11586 if (message
!= NULL
&& defstr
!= NULL
11587 && argvars
[1].v_type
!= VAR_UNKNOWN
11588 && argvars
[2].v_type
!= VAR_UNKNOWN
)
11589 rettv
->vval
.v_string
= vim_strsave(
11590 get_tv_string_buf(&argvars
[2], buf
));
11592 rettv
->vval
.v_string
= NULL
;
11594 rettv
->v_type
= VAR_STRING
;
11598 get_user_input(argvars
, rettv
, TRUE
);
11602 * "inputlist()" function
11605 f_inputlist(argvars
, rettv
)
11613 rettv
->vval
.v_number
= 0;
11614 #ifdef NO_CONSOLE_INPUT
11615 /* While starting up, there is no place to enter text. */
11616 if (no_console_input())
11619 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
11621 EMSG2(_(e_listarg
), "inputlist()");
11626 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
11627 lines_left
= Rows
; /* avoid more prompt */
11631 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
11633 msg_puts(get_tv_string(&li
->li_tv
));
11637 /* Ask for choice. */
11638 selected
= prompt_for_number(&mouse_used
);
11640 selected
-= lines_left
;
11642 rettv
->vval
.v_number
= selected
;
11646 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
11649 * "inputrestore()" function
11653 f_inputrestore(argvars
, rettv
)
11657 if (ga_userinput
.ga_len
> 0)
11659 --ga_userinput
.ga_len
;
11660 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
11661 + ga_userinput
.ga_len
);
11662 rettv
->vval
.v_number
= 0; /* OK */
11664 else if (p_verbose
> 1)
11666 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
11667 rettv
->vval
.v_number
= 1; /* Failed */
11672 * "inputsave()" function
11676 f_inputsave(argvars
, rettv
)
11680 /* Add an entry to the stack of typehead storage. */
11681 if (ga_grow(&ga_userinput
, 1) == OK
)
11683 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
11684 + ga_userinput
.ga_len
);
11685 ++ga_userinput
.ga_len
;
11686 rettv
->vval
.v_number
= 0; /* OK */
11689 rettv
->vval
.v_number
= 1; /* Failed */
11693 * "inputsecret()" function
11696 f_inputsecret(argvars
, rettv
)
11701 ++inputsecret_flag
;
11702 f_input(argvars
, rettv
);
11704 --inputsecret_flag
;
11708 * "insert()" function
11711 f_insert(argvars
, rettv
)
11720 rettv
->vval
.v_number
= 0;
11721 if (argvars
[0].v_type
!= VAR_LIST
)
11722 EMSG2(_(e_listarg
), "insert()");
11723 else if ((l
= argvars
[0].vval
.v_list
) != NULL
11724 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
11726 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11727 before
= get_tv_number_chk(&argvars
[2], &error
);
11729 return; /* type error; errmsg already given */
11731 if (before
== l
->lv_len
)
11735 item
= list_find(l
, before
);
11738 EMSGN(_(e_listidx
), before
);
11744 list_insert_tv(l
, &argvars
[1], item
);
11745 copy_tv(&argvars
[0], rettv
);
11751 * "isdirectory()" function
11754 f_isdirectory(argvars
, rettv
)
11758 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
11762 * "islocked()" function
11765 f_islocked(argvars
, rettv
)
11773 rettv
->vval
.v_number
= -1;
11774 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
11776 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
11779 EMSG(_(e_trailing
));
11782 if (lv
.ll_tv
== NULL
)
11784 if (check_changedtick(lv
.ll_name
))
11785 rettv
->vval
.v_number
= 1; /* always locked */
11788 di
= find_var(lv
.ll_name
, NULL
);
11791 /* Consider a variable locked when:
11792 * 1. the variable itself is locked
11793 * 2. the value of the variable is locked.
11794 * 3. the List or Dict value is locked.
11796 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
11797 || tv_islocked(&di
->di_tv
));
11801 else if (lv
.ll_range
)
11802 EMSG(_("E786: Range not allowed"));
11803 else if (lv
.ll_newkey
!= NULL
)
11804 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
11805 else if (lv
.ll_list
!= NULL
)
11807 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
11809 /* Dictionary item. */
11810 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
11817 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
11820 * Turn a dict into a list:
11821 * "what" == 0: list of keys
11822 * "what" == 1: list of values
11823 * "what" == 2: list of items
11826 dict_list(argvars
, rettv
, what
)
11839 rettv
->vval
.v_number
= 0;
11840 if (argvars
[0].v_type
!= VAR_DICT
)
11842 EMSG(_(e_dictreq
));
11845 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
11848 if (rettv_list_alloc(rettv
) == FAIL
)
11851 todo
= (int)d
->dv_hashtab
.ht_used
;
11852 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
11854 if (!HASHITEM_EMPTY(hi
))
11859 li
= listitem_alloc();
11862 list_append(rettv
->vval
.v_list
, li
);
11867 li
->li_tv
.v_type
= VAR_STRING
;
11868 li
->li_tv
.v_lock
= 0;
11869 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
11871 else if (what
== 1)
11874 copy_tv(&di
->di_tv
, &li
->li_tv
);
11880 li
->li_tv
.v_type
= VAR_LIST
;
11881 li
->li_tv
.v_lock
= 0;
11882 li
->li_tv
.vval
.v_list
= l2
;
11887 li2
= listitem_alloc();
11890 list_append(l2
, li2
);
11891 li2
->li_tv
.v_type
= VAR_STRING
;
11892 li2
->li_tv
.v_lock
= 0;
11893 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
11895 li2
= listitem_alloc();
11898 list_append(l2
, li2
);
11899 copy_tv(&di
->di_tv
, &li2
->li_tv
);
11906 * "items(dict)" function
11909 f_items(argvars
, rettv
)
11913 dict_list(argvars
, rettv
, 2);
11917 * "join()" function
11920 f_join(argvars
, rettv
)
11927 rettv
->vval
.v_number
= 0;
11928 if (argvars
[0].v_type
!= VAR_LIST
)
11930 EMSG(_(e_listreq
));
11933 if (argvars
[0].vval
.v_list
== NULL
)
11935 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11936 sep
= (char_u
*)" ";
11938 sep
= get_tv_string_chk(&argvars
[1]);
11940 rettv
->v_type
= VAR_STRING
;
11944 ga_init2(&ga
, (int)sizeof(char), 80);
11945 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
11946 ga_append(&ga
, NUL
);
11947 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
11950 rettv
->vval
.v_string
= NULL
;
11954 * "keys()" function
11957 f_keys(argvars
, rettv
)
11961 dict_list(argvars
, rettv
, 0);
11965 * "last_buffer_nr()" function.
11969 f_last_buffer_nr(argvars
, rettv
)
11976 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
11977 if (n
< buf
->b_fnum
)
11980 rettv
->vval
.v_number
= n
;
11987 f_len(argvars
, rettv
)
11991 switch (argvars
[0].v_type
)
11995 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
11996 get_tv_string(&argvars
[0]));
11999 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12002 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12005 EMSG(_("E701: Invalid type for len()"));
12010 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12013 libcall_common(argvars
, rettv
, type
)
12018 #ifdef FEAT_LIBCALL
12020 char_u
**string_result
;
12024 rettv
->v_type
= type
;
12025 if (type
== VAR_NUMBER
)
12026 rettv
->vval
.v_number
= 0;
12028 rettv
->vval
.v_string
= NULL
;
12030 if (check_restricted() || check_secure())
12033 #ifdef FEAT_LIBCALL
12034 /* The first two args must be strings, otherwise its meaningless */
12035 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12038 if (argvars
[2].v_type
== VAR_STRING
)
12039 string_in
= argvars
[2].vval
.v_string
;
12040 if (type
== VAR_NUMBER
)
12041 string_result
= NULL
;
12043 string_result
= &rettv
->vval
.v_string
;
12044 if (mch_libcall(argvars
[0].vval
.v_string
,
12045 argvars
[1].vval
.v_string
,
12047 argvars
[2].vval
.v_number
,
12050 && type
== VAR_NUMBER
)
12051 rettv
->vval
.v_number
= nr_result
;
12057 * "libcall()" function
12060 f_libcall(argvars
, rettv
)
12064 libcall_common(argvars
, rettv
, VAR_STRING
);
12068 * "libcallnr()" function
12071 f_libcallnr(argvars
, rettv
)
12075 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12079 * "line(string)" function
12082 f_line(argvars
, rettv
)
12090 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12093 rettv
->vval
.v_number
= lnum
;
12097 * "line2byte(lnum)" function
12101 f_line2byte(argvars
, rettv
)
12105 #ifndef FEAT_BYTEOFF
12106 rettv
->vval
.v_number
= -1;
12110 lnum
= get_tv_lnum(argvars
);
12111 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12112 rettv
->vval
.v_number
= -1;
12114 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12115 if (rettv
->vval
.v_number
>= 0)
12116 ++rettv
->vval
.v_number
;
12121 * "lispindent(lnum)" function
12124 f_lispindent(argvars
, rettv
)
12132 pos
= curwin
->w_cursor
;
12133 lnum
= get_tv_lnum(argvars
);
12134 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12136 curwin
->w_cursor
.lnum
= lnum
;
12137 rettv
->vval
.v_number
= get_lisp_indent();
12138 curwin
->w_cursor
= pos
;
12142 rettv
->vval
.v_number
= -1;
12146 * "localtime()" function
12150 f_localtime(argvars
, rettv
)
12154 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12157 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12160 get_maparg(argvars
, rettv
, exact
)
12167 char_u buf
[NUMBUFLEN
];
12168 char_u
*keys_buf
= NULL
;
12174 /* return empty string for failure */
12175 rettv
->v_type
= VAR_STRING
;
12176 rettv
->vval
.v_string
= NULL
;
12178 keys
= get_tv_string(&argvars
[0]);
12182 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12184 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12185 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12186 abbr
= get_tv_number(&argvars
[2]);
12189 which
= (char_u
*)"";
12193 mode
= get_map_mode(&which
, 0);
12195 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12196 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
12197 vim_free(keys_buf
);
12201 ga
.ga_itemsize
= 1;
12202 ga
.ga_growsize
= 40;
12204 while (*rhs
!= NUL
)
12205 ga_concat(&ga
, str2special(&rhs
, FALSE
));
12207 ga_append(&ga
, NUL
);
12208 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12216 f_map(argvars
, rettv
)
12220 filter_map(argvars
, rettv
, TRUE
);
12224 * "maparg()" function
12227 f_maparg(argvars
, rettv
)
12231 get_maparg(argvars
, rettv
, TRUE
);
12235 * "mapcheck()" function
12238 f_mapcheck(argvars
, rettv
)
12242 get_maparg(argvars
, rettv
, FALSE
);
12245 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
12248 find_some_match(argvars
, rettv
, type
)
12253 char_u
*str
= NULL
;
12254 char_u
*expr
= NULL
;
12256 regmatch_T regmatch
;
12257 char_u patbuf
[NUMBUFLEN
];
12258 char_u strbuf
[NUMBUFLEN
];
12262 colnr_T startcol
= 0;
12265 listitem_T
*li
= NULL
;
12267 char_u
*tofree
= NULL
;
12269 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12271 p_cpo
= (char_u
*)"";
12273 rettv
->vval
.v_number
= -1;
12276 /* return empty list when there are no matches */
12277 if (rettv_list_alloc(rettv
) == FAIL
)
12280 else if (type
== 2)
12282 rettv
->v_type
= VAR_STRING
;
12283 rettv
->vval
.v_string
= NULL
;
12286 if (argvars
[0].v_type
== VAR_LIST
)
12288 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
12293 expr
= str
= get_tv_string(&argvars
[0]);
12295 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
12299 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12303 start
= get_tv_number_chk(&argvars
[2], &error
);
12308 li
= list_find(l
, start
);
12311 idx
= l
->lv_idx
; /* use the cached index */
12317 if (start
> (long)STRLEN(str
))
12319 /* When "count" argument is there ignore matches before "start",
12320 * otherwise skip part of the string. Differs when pattern is "^"
12322 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12328 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12329 nth
= get_tv_number_chk(&argvars
[3], &error
);
12334 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
12335 if (regmatch
.regprog
!= NULL
)
12337 regmatch
.rm_ic
= p_ic
;
12349 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
12354 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
12356 if (match
&& --nth
<= 0)
12358 if (l
== NULL
&& !match
)
12361 /* Advance to just after the match. */
12370 startcol
= (colnr_T
)(regmatch
.startp
[0]
12371 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
12373 startcol
= regmatch
.startp
[0] + 1 - str
;
12384 /* return list with matched string and submatches */
12385 for (i
= 0; i
< NSUBEXP
; ++i
)
12387 if (regmatch
.endp
[i
] == NULL
)
12389 if (list_append_string(rettv
->vval
.v_list
,
12390 (char_u
*)"", 0) == FAIL
)
12393 else if (list_append_string(rettv
->vval
.v_list
,
12394 regmatch
.startp
[i
],
12395 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
12400 else if (type
== 2)
12402 /* return matched string */
12404 copy_tv(&li
->li_tv
, rettv
);
12406 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
12407 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
12409 else if (l
!= NULL
)
12410 rettv
->vval
.v_number
= idx
;
12414 rettv
->vval
.v_number
=
12415 (varnumber_T
)(regmatch
.startp
[0] - str
);
12417 rettv
->vval
.v_number
=
12418 (varnumber_T
)(regmatch
.endp
[0] - str
);
12419 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
12422 vim_free(regmatch
.regprog
);
12431 * "match()" function
12434 f_match(argvars
, rettv
)
12438 find_some_match(argvars
, rettv
, 1);
12442 * "matcharg()" function
12445 f_matcharg(argvars
, rettv
)
12449 if (rettv_list_alloc(rettv
) == OK
)
12451 #ifdef FEAT_SEARCH_EXTRA
12452 int mi
= get_tv_number(&argvars
[0]);
12454 if (mi
>= 1 && mi
<= 3)
12456 list_append_string(rettv
->vval
.v_list
,
12457 syn_id2name(curwin
->w_match_id
[mi
- 1]), -1);
12458 list_append_string(rettv
->vval
.v_list
,
12459 curwin
->w_match_pat
[mi
- 1], -1);
12466 * "matchend()" function
12469 f_matchend(argvars
, rettv
)
12473 find_some_match(argvars
, rettv
, 0);
12477 * "matchlist()" function
12480 f_matchlist(argvars
, rettv
)
12484 find_some_match(argvars
, rettv
, 3);
12488 * "matchstr()" function
12491 f_matchstr(argvars
, rettv
)
12495 find_some_match(argvars
, rettv
, 2);
12498 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
12501 max_min(argvars
, rettv
, domax
)
12510 if (argvars
[0].v_type
== VAR_LIST
)
12515 l
= argvars
[0].vval
.v_list
;
12521 n
= get_tv_number_chk(&li
->li_tv
, &error
);
12527 i
= get_tv_number_chk(&li
->li_tv
, &error
);
12528 if (domax
? i
> n
: i
< n
)
12534 else if (argvars
[0].v_type
== VAR_DICT
)
12541 d
= argvars
[0].vval
.v_dict
;
12544 todo
= (int)d
->dv_hashtab
.ht_used
;
12545 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12547 if (!HASHITEM_EMPTY(hi
))
12550 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
12556 else if (domax
? i
> n
: i
< n
)
12563 EMSG(_(e_listdictarg
));
12564 rettv
->vval
.v_number
= error
? 0 : n
;
12571 f_max(argvars
, rettv
)
12575 max_min(argvars
, rettv
, TRUE
);
12582 f_min(argvars
, rettv
)
12586 max_min(argvars
, rettv
, FALSE
);
12589 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
12592 * Create the directory in which "dir" is located, and higher levels when
12596 mkdir_recurse(dir
, prot
)
12604 /* Get end of directory name in "dir".
12605 * We're done when it's "/" or "c:/". */
12606 p
= gettail_sep(dir
);
12607 if (p
<= get_past_head(dir
))
12610 /* If the directory exists we're done. Otherwise: create it.*/
12611 updir
= vim_strnsave(dir
, (int)(p
- dir
));
12614 if (mch_isdir(updir
))
12616 else if (mkdir_recurse(updir
, prot
) == OK
)
12617 r
= vim_mkdir_emsg(updir
, prot
);
12624 * "mkdir()" function
12627 f_mkdir(argvars
, rettv
)
12632 char_u buf
[NUMBUFLEN
];
12635 rettv
->vval
.v_number
= FAIL
;
12636 if (check_restricted() || check_secure())
12639 dir
= get_tv_string_buf(&argvars
[0], buf
);
12640 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12642 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12643 prot
= get_tv_number_chk(&argvars
[2], NULL
);
12644 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
12645 mkdir_recurse(dir
, prot
);
12647 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
12652 * "mode()" function
12656 f_mode(argvars
, rettv
)
12666 buf
[0] = VIsual_mode
+ 's' - 'v';
12668 buf
[0] = VIsual_mode
;
12672 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
)
12674 else if (State
& INSERT
)
12676 if (State
& REPLACE_FLAG
)
12681 else if (State
& CMDLINE
)
12687 rettv
->vval
.v_string
= vim_strsave(buf
);
12688 rettv
->v_type
= VAR_STRING
;
12692 * "nextnonblank()" function
12695 f_nextnonblank(argvars
, rettv
)
12701 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
12703 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
12708 if (*skipwhite(ml_get(lnum
)) != NUL
)
12711 rettv
->vval
.v_number
= lnum
;
12715 * "nr2char()" function
12718 f_nr2char(argvars
, rettv
)
12722 char_u buf
[NUMBUFLEN
];
12726 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
12730 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
12733 rettv
->v_type
= VAR_STRING
;
12734 rettv
->vval
.v_string
= vim_strsave(buf
);
12738 * "pathshorten()" function
12741 f_pathshorten(argvars
, rettv
)
12747 rettv
->v_type
= VAR_STRING
;
12748 p
= get_tv_string_chk(&argvars
[0]);
12750 rettv
->vval
.v_string
= NULL
;
12753 p
= vim_strsave(p
);
12754 rettv
->vval
.v_string
= p
;
12761 * "prevnonblank()" function
12764 f_prevnonblank(argvars
, rettv
)
12770 lnum
= get_tv_lnum(argvars
);
12771 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
12774 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
12776 rettv
->vval
.v_number
= lnum
;
12779 #ifdef HAVE_STDARG_H
12780 /* This dummy va_list is here because:
12781 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12782 * - locally in the function results in a "used before set" warning
12783 * - using va_start() to initialize it gives "function with fixed args" error */
12788 * "printf()" function
12791 f_printf(argvars
, rettv
)
12795 rettv
->v_type
= VAR_STRING
;
12796 rettv
->vval
.v_string
= NULL
;
12797 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
12799 char_u buf
[NUMBUFLEN
];
12802 int saved_did_emsg
= did_emsg
;
12805 /* Get the required length, allocate the buffer and do it for real. */
12807 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
12808 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
12811 s
= alloc(len
+ 1);
12814 rettv
->vval
.v_string
= s
;
12815 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
12818 did_emsg
|= saved_did_emsg
;
12824 * "pumvisible()" function
12828 f_pumvisible(argvars
, rettv
)
12832 rettv
->vval
.v_number
= 0;
12833 #ifdef FEAT_INS_EXPAND
12835 rettv
->vval
.v_number
= 1;
12840 * "range()" function
12843 f_range(argvars
, rettv
)
12853 start
= get_tv_number_chk(&argvars
[0], &error
);
12854 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12861 end
= get_tv_number_chk(&argvars
[1], &error
);
12862 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12863 stride
= get_tv_number_chk(&argvars
[2], &error
);
12866 rettv
->vval
.v_number
= 0;
12868 return; /* type error; errmsg already given */
12870 EMSG(_("E726: Stride is zero"));
12871 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
12872 EMSG(_("E727: Start past end"));
12875 if (rettv_list_alloc(rettv
) == OK
)
12876 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
12877 if (list_append_number(rettv
->vval
.v_list
,
12878 (varnumber_T
)i
) == FAIL
)
12884 * "readfile()" function
12887 f_readfile(argvars
, rettv
)
12891 int binary
= FALSE
;
12895 #define FREAD_SIZE 200 /* optimized for text lines */
12896 char_u buf
[FREAD_SIZE
];
12897 int readlen
; /* size of last fread() */
12898 int buflen
; /* nr of valid chars in buf[] */
12899 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
12900 int tolist
; /* first byte in buf[] still to be put in list */
12901 int chop
; /* how many CR to chop off */
12902 char_u
*prev
= NULL
; /* previously read bytes, if any */
12903 int prevlen
= 0; /* length of "prev" if not NULL */
12906 long maxline
= MAXLNUM
;
12909 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12911 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
12913 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12914 maxline
= get_tv_number(&argvars
[2]);
12917 if (rettv_list_alloc(rettv
) == FAIL
)
12920 /* Always open the file in binary mode, library functions have a mind of
12921 * their own about CR-LF conversion. */
12922 fname
= get_tv_string(&argvars
[0]);
12923 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
12925 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
12930 while (cnt
< maxline
|| maxline
< 0)
12932 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
12933 buflen
= filtd
+ readlen
;
12935 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
12937 if (buf
[filtd
] == '\n' || readlen
<= 0)
12939 /* Only when in binary mode add an empty list item when the
12940 * last line ends in a '\n'. */
12941 if (!binary
&& readlen
== 0 && filtd
== 0)
12944 /* Found end-of-line or end-of-file: add a text line to the
12948 while (filtd
- chop
- 1 >= tolist
12949 && buf
[filtd
- chop
- 1] == '\r')
12951 len
= filtd
- tolist
- chop
;
12953 s
= vim_strnsave(buf
+ tolist
, len
);
12956 s
= alloc((unsigned)(prevlen
+ len
+ 1));
12959 mch_memmove(s
, prev
, prevlen
);
12962 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
12963 s
[prevlen
+ len
] = NUL
;
12966 tolist
= filtd
+ 1;
12968 li
= listitem_alloc();
12974 li
->li_tv
.v_type
= VAR_STRING
;
12975 li
->li_tv
.v_lock
= 0;
12976 li
->li_tv
.vval
.v_string
= s
;
12977 list_append(rettv
->vval
.v_list
, li
);
12979 if (++cnt
>= maxline
&& maxline
>= 0)
12984 else if (buf
[filtd
] == NUL
)
12992 /* "buf" is full, need to move text to an allocated buffer */
12995 prev
= vim_strnsave(buf
, buflen
);
13000 s
= alloc((unsigned)(prevlen
+ buflen
));
13003 mch_memmove(s
, prev
, prevlen
);
13004 mch_memmove(s
+ prevlen
, buf
, buflen
);
13014 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13020 * For a negative line count use only the lines at the end of the file,
13024 while (cnt
> -maxline
)
13026 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
13034 #if defined(FEAT_RELTIME)
13035 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
13038 * Convert a List to proftime_T.
13039 * Return FAIL when there is something wrong.
13042 list2proftime(arg
, tm
)
13049 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
13050 || arg
->vval
.v_list
->lv_len
!= 2)
13052 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
13053 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
13061 return error
? FAIL
: OK
;
13063 #endif /* FEAT_RELTIME */
13066 * "reltime()" function
13069 f_reltime(argvars
, rettv
)
13073 #ifdef FEAT_RELTIME
13077 if (argvars
[0].v_type
== VAR_UNKNOWN
)
13079 /* No arguments: get current time. */
13080 profile_start(&res
);
13082 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
13084 if (list2proftime(&argvars
[0], &res
) == FAIL
)
13090 /* Two arguments: compute the difference. */
13091 if (list2proftime(&argvars
[0], &start
) == FAIL
13092 || list2proftime(&argvars
[1], &res
) == FAIL
)
13094 profile_sub(&res
, &start
);
13097 if (rettv_list_alloc(rettv
) == OK
)
13108 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
13109 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
13115 * "reltimestr()" function
13118 f_reltimestr(argvars
, rettv
)
13122 #ifdef FEAT_RELTIME
13126 rettv
->v_type
= VAR_STRING
;
13127 rettv
->vval
.v_string
= NULL
;
13128 #ifdef FEAT_RELTIME
13129 if (list2proftime(&argvars
[0], &tm
) == OK
)
13130 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
13134 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13135 static void make_connection
__ARGS((void));
13136 static int check_connection
__ARGS((void));
13141 if (X_DISPLAY
== NULL
13147 x_force_connect
= TRUE
;
13149 x_force_connect
= FALSE
;
13157 if (X_DISPLAY
== NULL
)
13159 EMSG(_("E240: No connection to Vim server"));
13166 #ifdef FEAT_CLIENTSERVER
13167 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
13170 remote_common(argvars
, rettv
, expr
)
13175 char_u
*server_name
;
13178 char_u buf
[NUMBUFLEN
];
13185 if (check_restricted() || check_secure())
13189 if (check_connection() == FAIL
)
13193 server_name
= get_tv_string_chk(&argvars
[0]);
13194 if (server_name
== NULL
)
13195 return; /* type error; errmsg already given */
13196 keys
= get_tv_string_buf(&argvars
[1], buf
);
13198 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
13200 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
13205 EMSG(r
); /* sending worked but evaluation failed */
13207 EMSG2(_("E241: Unable to send to %s"), server_name
);
13211 rettv
->vval
.v_string
= r
;
13213 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13219 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
13220 v
.di_tv
.v_type
= VAR_STRING
;
13221 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
13222 idvar
= get_tv_string_chk(&argvars
[2]);
13224 set_var(idvar
, &v
.di_tv
, FALSE
);
13225 vim_free(v
.di_tv
.vval
.v_string
);
13231 * "remote_expr()" function
13235 f_remote_expr(argvars
, rettv
)
13239 rettv
->v_type
= VAR_STRING
;
13240 rettv
->vval
.v_string
= NULL
;
13241 #ifdef FEAT_CLIENTSERVER
13242 remote_common(argvars
, rettv
, TRUE
);
13247 * "remote_foreground()" function
13251 f_remote_foreground(argvars
, rettv
)
13255 rettv
->vval
.v_number
= 0;
13256 #ifdef FEAT_CLIENTSERVER
13258 /* On Win32 it's done in this application. */
13260 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
13262 if (server_name
!= NULL
)
13263 serverForeground(server_name
);
13266 /* Send a foreground() expression to the server. */
13267 argvars
[1].v_type
= VAR_STRING
;
13268 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
13269 argvars
[2].v_type
= VAR_UNKNOWN
;
13270 remote_common(argvars
, rettv
, TRUE
);
13271 vim_free(argvars
[1].vval
.v_string
);
13278 f_remote_peek(argvars
, rettv
)
13282 #ifdef FEAT_CLIENTSERVER
13290 if (check_restricted() || check_secure())
13292 rettv
->vval
.v_number
= -1;
13295 serverid
= get_tv_string_chk(&argvars
[0]);
13296 if (serverid
== NULL
)
13298 rettv
->vval
.v_number
= -1;
13299 return; /* type error; errmsg already given */
13302 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
13304 rettv
->vval
.v_number
= -1;
13307 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
13308 rettv
->vval
.v_number
= (s
!= NULL
);
13311 rettv
->vval
.v_number
= 0;
13312 if (check_connection() == FAIL
)
13315 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
13316 serverStrToWin(serverid
), &s
);
13319 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
13323 v
.di_tv
.v_type
= VAR_STRING
;
13324 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
13325 retvar
= get_tv_string_chk(&argvars
[1]);
13326 if (retvar
!= NULL
)
13327 set_var(retvar
, &v
.di_tv
, FALSE
);
13328 vim_free(v
.di_tv
.vval
.v_string
);
13331 rettv
->vval
.v_number
= -1;
13337 f_remote_read(argvars
, rettv
)
13343 #ifdef FEAT_CLIENTSERVER
13344 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
13346 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
13349 /* The server's HWND is encoded in the 'id' parameter */
13352 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
13354 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
13357 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
13358 serverStrToWin(serverid
), &r
, FALSE
) < 0)
13360 EMSG(_("E277: Unable to read a server reply"));
13363 rettv
->v_type
= VAR_STRING
;
13364 rettv
->vval
.v_string
= r
;
13368 * "remote_send()" function
13372 f_remote_send(argvars
, rettv
)
13376 rettv
->v_type
= VAR_STRING
;
13377 rettv
->vval
.v_string
= NULL
;
13378 #ifdef FEAT_CLIENTSERVER
13379 remote_common(argvars
, rettv
, FALSE
);
13384 * "remove()" function
13387 f_remove(argvars
, rettv
)
13392 listitem_T
*item
, *item2
;
13400 rettv
->vval
.v_number
= 0;
13401 if (argvars
[0].v_type
== VAR_DICT
)
13403 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13404 EMSG2(_(e_toomanyarg
), "remove()");
13405 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
13406 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
13408 key
= get_tv_string_chk(&argvars
[1]);
13411 di
= dict_find(d
, key
, -1);
13413 EMSG2(_(e_dictkey
), key
);
13416 *rettv
= di
->di_tv
;
13417 init_tv(&di
->di_tv
);
13418 dictitem_remove(d
, di
);
13423 else if (argvars
[0].v_type
!= VAR_LIST
)
13424 EMSG2(_(e_listdictarg
), "remove()");
13425 else if ((l
= argvars
[0].vval
.v_list
) != NULL
13426 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
13430 idx
= get_tv_number_chk(&argvars
[1], &error
);
13432 ; /* type error: do nothing, errmsg already given */
13433 else if ((item
= list_find(l
, idx
)) == NULL
)
13434 EMSGN(_(e_listidx
), idx
);
13437 if (argvars
[2].v_type
== VAR_UNKNOWN
)
13439 /* Remove one item, return its value. */
13440 list_remove(l
, item
, item
);
13441 *rettv
= item
->li_tv
;
13446 /* Remove range of items, return list with values. */
13447 end
= get_tv_number_chk(&argvars
[2], &error
);
13449 ; /* type error: do nothing */
13450 else if ((item2
= list_find(l
, end
)) == NULL
)
13451 EMSGN(_(e_listidx
), end
);
13456 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
13462 if (li
== NULL
) /* didn't find "item2" after "item" */
13463 EMSG(_(e_invrange
));
13466 list_remove(l
, item
, item2
);
13467 if (rettv_list_alloc(rettv
) == OK
)
13469 l
= rettv
->vval
.v_list
;
13470 l
->lv_first
= item
;
13471 l
->lv_last
= item2
;
13472 item
->li_prev
= NULL
;
13473 item2
->li_next
= NULL
;
13484 * "rename({from}, {to})" function
13487 f_rename(argvars
, rettv
)
13491 char_u buf
[NUMBUFLEN
];
13493 if (check_restricted() || check_secure())
13494 rettv
->vval
.v_number
= -1;
13496 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
13497 get_tv_string_buf(&argvars
[1], buf
));
13501 * "repeat()" function
13505 f_repeat(argvars
, rettv
)
13516 n
= get_tv_number(&argvars
[1]);
13517 if (argvars
[0].v_type
== VAR_LIST
)
13519 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
13521 if (list_extend(rettv
->vval
.v_list
,
13522 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
13527 p
= get_tv_string(&argvars
[0]);
13528 rettv
->v_type
= VAR_STRING
;
13529 rettv
->vval
.v_string
= NULL
;
13531 slen
= (int)STRLEN(p
);
13536 r
= alloc(len
+ 1);
13539 for (i
= 0; i
< n
; i
++)
13540 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
13544 rettv
->vval
.v_string
= r
;
13549 * "resolve()" function
13552 f_resolve(argvars
, rettv
)
13558 p
= get_tv_string(&argvars
[0]);
13559 #ifdef FEAT_SHORTCUT
13563 v
= mch_resolve_shortcut(p
);
13565 rettv
->vval
.v_string
= v
;
13567 rettv
->vval
.v_string
= vim_strsave(p
);
13570 # ifdef HAVE_READLINK
13572 char_u buf
[MAXPATHL
+ 1];
13575 char_u
*remain
= NULL
;
13577 int is_relative_to_current
= FALSE
;
13578 int has_trailing_pathsep
= FALSE
;
13581 p
= vim_strsave(p
);
13583 if (p
[0] == '.' && (vim_ispathsep(p
[1])
13584 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
13585 is_relative_to_current
= TRUE
;
13588 if (len
> 0 && after_pathsep(p
, p
+ len
))
13589 has_trailing_pathsep
= TRUE
;
13591 q
= getnextcomp(p
);
13594 /* Separate the first path component in "p", and keep the
13595 * remainder (beginning with the path separator). */
13596 remain
= vim_strsave(q
- 1);
13604 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
13613 EMSG(_("E655: Too many symbolic links (cycle?)"));
13614 rettv
->vval
.v_string
= NULL
;
13618 /* Ensure that the result will have a trailing path separator
13619 * if the argument has one. */
13620 if (remain
== NULL
&& has_trailing_pathsep
)
13623 /* Separate the first path component in the link value and
13624 * concatenate the remainders. */
13625 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
13628 if (remain
== NULL
)
13629 remain
= vim_strsave(q
- 1);
13632 cpy
= concat_str(q
- 1, remain
);
13643 if (q
> p
&& *q
== NUL
)
13645 /* Ignore trailing path separator. */
13649 if (q
> p
&& !mch_isFullName(buf
))
13651 /* symlink is relative to directory of argument */
13652 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
13656 STRCPY(gettail(cpy
), buf
);
13664 p
= vim_strsave(buf
);
13668 if (remain
== NULL
)
13671 /* Append the first path component of "remain" to "p". */
13672 q
= getnextcomp(remain
+ 1);
13673 len
= q
- remain
- (*q
!= NUL
);
13674 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
13677 STRNCAT(cpy
, remain
, len
);
13681 /* Shorten "remain". */
13683 STRCPY(remain
, q
- 1);
13691 /* If the result is a relative path name, make it explicitly relative to
13692 * the current directory if and only if the argument had this form. */
13693 if (!vim_ispathsep(*p
))
13695 if (is_relative_to_current
13699 || vim_ispathsep(p
[1])
13702 || vim_ispathsep(p
[2]))))))
13704 /* Prepend "./". */
13705 cpy
= concat_str((char_u
*)"./", p
);
13712 else if (!is_relative_to_current
)
13714 /* Strip leading "./". */
13716 while (q
[0] == '.' && vim_ispathsep(q
[1]))
13719 mch_memmove(p
, p
+ 2, STRLEN(p
+ 2) + (size_t)1);
13723 /* Ensure that the result will have no trailing path separator
13724 * if the argument had none. But keep "/" or "//". */
13725 if (!has_trailing_pathsep
)
13728 if (after_pathsep(p
, q
))
13729 *gettail_sep(p
) = NUL
;
13732 rettv
->vval
.v_string
= p
;
13735 rettv
->vval
.v_string
= vim_strsave(p
);
13739 simplify_filename(rettv
->vval
.v_string
);
13741 #ifdef HAVE_READLINK
13744 rettv
->v_type
= VAR_STRING
;
13748 * "reverse({list})" function
13751 f_reverse(argvars
, rettv
)
13756 listitem_T
*li
, *ni
;
13758 rettv
->vval
.v_number
= 0;
13759 if (argvars
[0].v_type
!= VAR_LIST
)
13760 EMSG2(_(e_listarg
), "reverse()");
13761 else if ((l
= argvars
[0].vval
.v_list
) != NULL
13762 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
13765 l
->lv_first
= l
->lv_last
= NULL
;
13770 list_append(l
, li
);
13773 rettv
->vval
.v_list
= l
;
13774 rettv
->v_type
= VAR_LIST
;
13779 #define SP_NOMOVE 0x01 /* don't move cursor */
13780 #define SP_REPEAT 0x02 /* repeat to find outer pair */
13781 #define SP_RETCOUNT 0x04 /* return matchcount */
13782 #define SP_SETPCMARK 0x08 /* set previous context mark */
13783 #define SP_START 0x10 /* accept match at start position */
13784 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13785 #define SP_END 0x40 /* leave cursor at end of match */
13787 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
13790 * Get flags for a search function.
13791 * Possibly sets "p_ws".
13792 * Returns BACKWARD, FORWARD or zero (for an error).
13795 get_search_arg(varp
, flagsp
)
13801 char_u nbuf
[NUMBUFLEN
];
13804 if (varp
->v_type
!= VAR_UNKNOWN
)
13806 flags
= get_tv_string_buf_chk(varp
, nbuf
);
13808 return 0; /* type error; errmsg already given */
13809 while (*flags
!= NUL
)
13813 case 'b': dir
= BACKWARD
; break;
13814 case 'w': p_ws
= TRUE
; break;
13815 case 'W': p_ws
= FALSE
; break;
13817 if (flagsp
!= NULL
)
13820 case 'c': mask
= SP_START
; break;
13821 case 'e': mask
= SP_END
; break;
13822 case 'm': mask
= SP_RETCOUNT
; break;
13823 case 'n': mask
= SP_NOMOVE
; break;
13824 case 'p': mask
= SP_SUBPAT
; break;
13825 case 'r': mask
= SP_REPEAT
; break;
13826 case 's': mask
= SP_SETPCMARK
; break;
13830 EMSG2(_(e_invarg2
), flags
);
13845 * Shared by search() and searchpos() functions
13848 search_cmn(argvars
, match_pos
, flagsp
)
13857 int save_p_ws
= p_ws
;
13859 int retval
= 0; /* default: FAIL */
13860 long lnum_stop
= 0;
13861 int options
= SEARCH_KEEP
;
13864 pat
= get_tv_string(&argvars
[0]);
13865 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
13869 if (flags
& SP_START
)
13870 options
|= SEARCH_START
;
13871 if (flags
& SP_END
)
13872 options
|= SEARCH_END
;
13874 /* Optional extra argument: line number to stop searching. */
13875 if (argvars
[1].v_type
!= VAR_UNKNOWN
13876 && argvars
[2].v_type
!= VAR_UNKNOWN
)
13878 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
13884 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
13885 * Check to make sure only those flags are set.
13886 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13887 * flags cannot be set. Check for that condition also.
13889 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
13890 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
13892 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
13896 pos
= save_cursor
= curwin
->w_cursor
;
13897 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
13898 options
, RE_SEARCH
, (linenr_T
)lnum_stop
);
13899 if (subpatnum
!= FAIL
)
13901 if (flags
& SP_SUBPAT
)
13902 retval
= subpatnum
;
13905 if (flags
& SP_SETPCMARK
)
13907 curwin
->w_cursor
= pos
;
13908 if (match_pos
!= NULL
)
13910 /* Store the match cursor position */
13911 match_pos
->lnum
= pos
.lnum
;
13912 match_pos
->col
= pos
.col
+ 1;
13914 /* "/$" will put the cursor after the end of the line, may need to
13915 * correct that here */
13919 /* If 'n' flag is used: restore cursor position. */
13920 if (flags
& SP_NOMOVE
)
13921 curwin
->w_cursor
= save_cursor
;
13929 * "search()" function
13932 f_search(argvars
, rettv
)
13938 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
13942 * "searchdecl()" function
13945 f_searchdecl(argvars
, rettv
)
13954 rettv
->vval
.v_number
= 1; /* default: FAIL */
13956 name
= get_tv_string_chk(&argvars
[0]);
13957 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13959 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
13960 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
13961 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
13963 if (!error
&& name
!= NULL
)
13964 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
13965 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
13969 * Used by searchpair() and searchpairpos()
13972 searchpair_cmn(argvars
, match_pos
)
13976 char_u
*spat
, *mpat
, *epat
;
13978 int save_p_ws
= p_ws
;
13981 char_u nbuf1
[NUMBUFLEN
];
13982 char_u nbuf2
[NUMBUFLEN
];
13983 char_u nbuf3
[NUMBUFLEN
];
13984 int retval
= 0; /* default: FAIL */
13985 long lnum_stop
= 0;
13987 /* Get the three pattern arguments: start, middle, end. */
13988 spat
= get_tv_string_chk(&argvars
[0]);
13989 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
13990 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
13991 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
13992 goto theend
; /* type error */
13994 /* Handle the optional fourth argument: flags */
13995 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
13999 /* Don't accept SP_END or SP_SUBPAT.
14000 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14002 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
14003 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14005 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
14009 /* Optional fifth argument: skip expression */
14010 if (argvars
[3].v_type
== VAR_UNKNOWN
14011 || argvars
[4].v_type
== VAR_UNKNOWN
)
14012 skip
= (char_u
*)"";
14015 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
14016 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
14018 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
14024 goto theend
; /* type error */
14026 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
14027 match_pos
, lnum_stop
);
14036 * "searchpair()" function
14039 f_searchpair(argvars
, rettv
)
14043 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
14047 * "searchpairpos()" function
14050 f_searchpairpos(argvars
, rettv
)
14058 rettv
->vval
.v_number
= 0;
14060 if (rettv_list_alloc(rettv
) == FAIL
)
14063 if (searchpair_cmn(argvars
, &match_pos
) > 0)
14065 lnum
= match_pos
.lnum
;
14066 col
= match_pos
.col
;
14069 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14070 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14074 * Search for a start/middle/end thing.
14075 * Used by searchpair(), see its documentation for the details.
14076 * Returns 0 or -1 for no match,
14079 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
, lnum_stop
)
14080 char_u
*spat
; /* start pattern */
14081 char_u
*mpat
; /* middle pattern */
14082 char_u
*epat
; /* end pattern */
14083 int dir
; /* BACKWARD or FORWARD */
14084 char_u
*skip
; /* skip expression */
14085 int flags
; /* SP_SETPCMARK and other SP_ values */
14087 linenr_T lnum_stop
; /* stop at this line if not zero */
14090 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
14101 int options
= SEARCH_KEEP
;
14103 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14105 p_cpo
= (char_u
*)"";
14107 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14108 * start/middle/end (pat3, for the top pair). */
14109 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
14110 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
14111 if (pat2
== NULL
|| pat3
== NULL
)
14113 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
14115 STRCPY(pat3
, pat2
);
14117 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14119 if (flags
& SP_START
)
14120 options
|= SEARCH_START
;
14122 save_cursor
= curwin
->w_cursor
;
14123 pos
= curwin
->w_cursor
;
14124 clearpos(&firstpos
);
14125 clearpos(&foundpos
);
14129 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14130 options
, RE_SEARCH
, lnum_stop
);
14131 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
14132 /* didn't find it or found the first match again: FAIL */
14135 if (firstpos
.lnum
== 0)
14137 if (equalpos(pos
, foundpos
))
14139 /* Found the same position again. Can happen with a pattern that
14140 * has "\zs" at the end and searching backwards. Advance one
14141 * character and try again. */
14142 if (dir
== BACKWARD
)
14149 /* If the skip pattern matches, ignore this match. */
14152 save_pos
= curwin
->w_cursor
;
14153 curwin
->w_cursor
= pos
;
14154 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
14155 curwin
->w_cursor
= save_pos
;
14158 /* Evaluating {skip} caused an error, break here. */
14159 curwin
->w_cursor
= save_cursor
;
14167 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
14169 /* Found end when searching backwards or start when searching
14170 * forward: nested pair. */
14172 pat
= pat2
; /* nested, don't search for middle */
14176 /* Found end when searching forward or start when searching
14177 * backward: end of (nested) pair; or found middle in outer pair. */
14179 pat
= pat3
; /* outer level, search for middle */
14184 /* Found the match: return matchcount or line number. */
14185 if (flags
& SP_RETCOUNT
)
14189 if (flags
& SP_SETPCMARK
)
14191 curwin
->w_cursor
= pos
;
14192 if (!(flags
& SP_REPEAT
))
14194 nest
= 1; /* search for next unmatched */
14198 if (match_pos
!= NULL
)
14200 /* Store the match cursor position */
14201 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
14202 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
14205 /* If 'n' flag is used or search failed: restore cursor position. */
14206 if ((flags
& SP_NOMOVE
) || retval
== 0)
14207 curwin
->w_cursor
= save_cursor
;
14218 * "searchpos()" function
14221 f_searchpos(argvars
, rettv
)
14231 rettv
->vval
.v_number
= 0;
14233 if (rettv_list_alloc(rettv
) == FAIL
)
14236 n
= search_cmn(argvars
, &match_pos
, &flags
);
14239 lnum
= match_pos
.lnum
;
14240 col
= match_pos
.col
;
14243 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14244 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14245 if (flags
& SP_SUBPAT
)
14246 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
14252 f_server2client(argvars
, rettv
)
14256 #ifdef FEAT_CLIENTSERVER
14257 char_u buf
[NUMBUFLEN
];
14258 char_u
*server
= get_tv_string_chk(&argvars
[0]);
14259 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
14261 rettv
->vval
.v_number
= -1;
14262 if (server
== NULL
|| reply
== NULL
)
14264 if (check_restricted() || check_secure())
14267 if (check_connection() == FAIL
)
14271 if (serverSendReply(server
, reply
) < 0)
14273 EMSG(_("E258: Unable to send to client"));
14276 rettv
->vval
.v_number
= 0;
14278 rettv
->vval
.v_number
= -1;
14284 f_serverlist(argvars
, rettv
)
14290 #ifdef FEAT_CLIENTSERVER
14292 r
= serverGetVimNames();
14295 if (X_DISPLAY
!= NULL
)
14296 r
= serverGetVimNames(X_DISPLAY
);
14299 rettv
->v_type
= VAR_STRING
;
14300 rettv
->vval
.v_string
= r
;
14304 * "setbufvar()" function
14308 f_setbufvar(argvars
, rettv
)
14314 char_u
*varname
, *bufvarname
;
14316 char_u nbuf
[NUMBUFLEN
];
14318 rettv
->vval
.v_number
= 0;
14320 if (check_restricted() || check_secure())
14322 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
14323 varname
= get_tv_string_chk(&argvars
[1]);
14324 buf
= get_buf_tv(&argvars
[0]);
14325 varp
= &argvars
[2];
14327 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
14329 /* set curbuf to be our buf, temporarily */
14330 aucmd_prepbuf(&aco
, buf
);
14332 if (*varname
== '&')
14339 numval
= get_tv_number_chk(varp
, &error
);
14340 strval
= get_tv_string_buf_chk(varp
, nbuf
);
14341 if (!error
&& strval
!= NULL
)
14342 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
14346 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
14347 if (bufvarname
!= NULL
)
14349 STRCPY(bufvarname
, "b:");
14350 STRCPY(bufvarname
+ 2, varname
);
14351 set_var(bufvarname
, varp
, TRUE
);
14352 vim_free(bufvarname
);
14356 /* reset notion of buffer */
14357 aucmd_restbuf(&aco
);
14362 * "setcmdpos()" function
14365 f_setcmdpos(argvars
, rettv
)
14369 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
14372 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
14376 * "setline()" function
14379 f_setline(argvars
, rettv
)
14384 char_u
*line
= NULL
;
14386 listitem_T
*li
= NULL
;
14388 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
14390 lnum
= get_tv_lnum(&argvars
[0]);
14391 if (argvars
[1].v_type
== VAR_LIST
)
14393 l
= argvars
[1].vval
.v_list
;
14397 line
= get_tv_string_chk(&argvars
[1]);
14399 rettv
->vval
.v_number
= 0; /* OK */
14404 /* list argument, get next string */
14407 line
= get_tv_string_chk(&li
->li_tv
);
14411 rettv
->vval
.v_number
= 1; /* FAIL */
14412 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
14414 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
14416 /* existing line, replace it */
14417 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
14419 changed_bytes(lnum
, 0);
14420 if (lnum
== curwin
->w_cursor
.lnum
)
14421 check_cursor_col();
14422 rettv
->vval
.v_number
= 0; /* OK */
14425 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
14427 /* lnum is one past the last line, append the line */
14429 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
14430 rettv
->vval
.v_number
= 0; /* OK */
14433 if (l
== NULL
) /* only one string argument */
14439 appended_lines_mark(lcount
, added
);
14443 * Used by "setqflist()" and "setloclist()" functions
14447 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
14449 typval_T
*list_arg
;
14450 typval_T
*action_arg
;
14453 #ifdef FEAT_QUICKFIX
14458 rettv
->vval
.v_number
= -1;
14460 #ifdef FEAT_QUICKFIX
14461 if (list_arg
->v_type
!= VAR_LIST
)
14462 EMSG(_(e_listreq
));
14465 list_T
*l
= list_arg
->vval
.v_list
;
14467 if (action_arg
->v_type
== VAR_STRING
)
14469 act
= get_tv_string_chk(action_arg
);
14471 return; /* type error; errmsg already given */
14472 if (*act
== 'a' || *act
== 'r')
14476 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
14477 rettv
->vval
.v_number
= 0;
14483 * "setloclist()" function
14487 f_setloclist(argvars
, rettv
)
14493 rettv
->vval
.v_number
= -1;
14495 win
= find_win_by_nr(&argvars
[0], NULL
);
14497 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
14501 * "setpos()" function
14505 f_setpos(argvars
, rettv
)
14513 name
= get_tv_string_chk(argvars
);
14516 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
14519 if (name
[0] == '.') /* cursor */
14521 if (fnum
== curbuf
->b_fnum
)
14523 curwin
->w_cursor
= pos
;
14529 else if (name
[0] == '\'') /* mark */
14530 (void)setmark_pos(name
[1], &pos
, fnum
);
14538 * "setqflist()" function
14542 f_setqflist(argvars
, rettv
)
14546 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
14550 * "setreg()" function
14553 f_setreg(argvars
, rettv
)
14558 char_u
*strregname
;
14569 strregname
= get_tv_string_chk(argvars
);
14570 rettv
->vval
.v_number
= 1; /* FAIL is default */
14572 if (strregname
== NULL
)
14573 return; /* type error; errmsg already given */
14574 regname
= *strregname
;
14575 if (regname
== 0 || regname
== '@')
14577 else if (regname
== '=')
14580 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14582 stropt
= get_tv_string_chk(&argvars
[2]);
14583 if (stropt
== NULL
)
14584 return; /* type error */
14585 for (; *stropt
!= NUL
; ++stropt
)
14588 case 'a': case 'A': /* append */
14591 case 'v': case 'c': /* character-wise selection */
14594 case 'V': case 'l': /* line-wise selection */
14598 case 'b': case Ctrl_V
: /* block-wise selection */
14599 yank_type
= MBLOCK
;
14600 if (VIM_ISDIGIT(stropt
[1]))
14603 block_len
= getdigits(&stropt
) - 1;
14611 strval
= get_tv_string_chk(&argvars
[1]);
14612 if (strval
!= NULL
)
14613 write_reg_contents_ex(regname
, strval
, -1,
14614 append
, yank_type
, block_len
);
14615 rettv
->vval
.v_number
= 0;
14619 * "settabwinvar()" function
14622 f_settabwinvar(argvars
, rettv
)
14626 setwinvar(argvars
, rettv
, 1);
14630 * "setwinvar()" function
14633 f_setwinvar(argvars
, rettv
)
14637 setwinvar(argvars
, rettv
, 0);
14641 * "setwinvar()" and "settabwinvar()" functions
14644 setwinvar(argvars
, rettv
, off
)
14650 #ifdef FEAT_WINDOWS
14651 win_T
*save_curwin
;
14652 tabpage_T
*save_curtab
;
14654 char_u
*varname
, *winvarname
;
14656 char_u nbuf
[NUMBUFLEN
];
14659 rettv
->vval
.v_number
= 0;
14661 if (check_restricted() || check_secure())
14664 #ifdef FEAT_WINDOWS
14666 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
14670 win
= find_win_by_nr(&argvars
[off
], tp
);
14671 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
14672 varp
= &argvars
[off
+ 2];
14674 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
14676 #ifdef FEAT_WINDOWS
14677 /* set curwin to be our win, temporarily */
14678 save_curwin
= curwin
;
14679 save_curtab
= curtab
;
14680 goto_tabpage_tp(tp
);
14681 if (!win_valid(win
))
14684 curbuf
= curwin
->w_buffer
;
14687 if (*varname
== '&')
14694 numval
= get_tv_number_chk(varp
, &error
);
14695 strval
= get_tv_string_buf_chk(varp
, nbuf
);
14696 if (!error
&& strval
!= NULL
)
14697 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
14701 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
14702 if (winvarname
!= NULL
)
14704 STRCPY(winvarname
, "w:");
14705 STRCPY(winvarname
+ 2, varname
);
14706 set_var(winvarname
, varp
, TRUE
);
14707 vim_free(winvarname
);
14711 #ifdef FEAT_WINDOWS
14712 /* Restore current tabpage and window, if still valid (autocomands can
14713 * make them invalid). */
14714 if (valid_tabpage(save_curtab
))
14715 goto_tabpage_tp(save_curtab
);
14716 if (win_valid(save_curwin
))
14718 curwin
= save_curwin
;
14719 curbuf
= curwin
->w_buffer
;
14726 * "shellescape({string})" function
14729 f_shellescape(argvars
, rettv
)
14733 rettv
->vval
.v_string
= vim_strsave_shellescape(get_tv_string(&argvars
[0]));
14734 rettv
->v_type
= VAR_STRING
;
14738 * "simplify()" function
14741 f_simplify(argvars
, rettv
)
14747 p
= get_tv_string(&argvars
[0]);
14748 rettv
->vval
.v_string
= vim_strsave(p
);
14749 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
14750 rettv
->v_type
= VAR_STRING
;
14754 #ifdef __BORLANDC__
14757 item_compare
__ARGS((const void *s1
, const void *s2
));
14759 #ifdef __BORLANDC__
14762 item_compare2
__ARGS((const void *s1
, const void *s2
));
14764 static int item_compare_ic
;
14765 static char_u
*item_compare_func
;
14766 static int item_compare_func_err
;
14767 #define ITEM_COMPARE_FAIL 999
14770 * Compare functions for f_sort() below.
14773 #ifdef __BORLANDC__
14776 item_compare(s1
, s2
)
14781 char_u
*tofree1
, *tofree2
;
14783 char_u numbuf1
[NUMBUFLEN
];
14784 char_u numbuf2
[NUMBUFLEN
];
14786 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
14787 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
14788 if (item_compare_ic
)
14789 res
= STRICMP(p1
, p2
);
14791 res
= STRCMP(p1
, p2
);
14798 #ifdef __BORLANDC__
14801 item_compare2(s1
, s2
)
14810 /* shortcut after failure in previous call; compare all items equal */
14811 if (item_compare_func_err
)
14814 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14815 * in the copy without changing the original list items. */
14816 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
14817 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
14819 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
14820 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
14821 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
14822 clear_tv(&argv
[0]);
14823 clear_tv(&argv
[1]);
14826 res
= ITEM_COMPARE_FAIL
;
14828 /* return value has wrong type */
14829 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
14830 if (item_compare_func_err
)
14831 res
= ITEM_COMPARE_FAIL
;
14837 * "sort({list})" function
14840 f_sort(argvars
, rettv
)
14850 rettv
->vval
.v_number
= 0;
14851 if (argvars
[0].v_type
!= VAR_LIST
)
14852 EMSG2(_(e_listarg
), "sort()");
14855 l
= argvars
[0].vval
.v_list
;
14856 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
14858 rettv
->vval
.v_list
= l
;
14859 rettv
->v_type
= VAR_LIST
;
14864 return; /* short list sorts pretty quickly */
14866 item_compare_ic
= FALSE
;
14867 item_compare_func
= NULL
;
14868 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14870 if (argvars
[1].v_type
== VAR_FUNC
)
14871 item_compare_func
= argvars
[1].vval
.v_string
;
14876 i
= get_tv_number_chk(&argvars
[1], &error
);
14878 return; /* type error; errmsg already given */
14880 item_compare_ic
= TRUE
;
14882 item_compare_func
= get_tv_string(&argvars
[1]);
14886 /* Make an array with each entry pointing to an item in the List. */
14887 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
14891 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
14894 item_compare_func_err
= FALSE
;
14895 /* test the compare function */
14896 if (item_compare_func
!= NULL
14897 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
14898 == ITEM_COMPARE_FAIL
)
14899 EMSG(_("E702: Sort compare function failed"));
14902 /* Sort the array with item pointers. */
14903 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
14904 item_compare_func
== NULL
? item_compare
: item_compare2
);
14906 if (!item_compare_func_err
)
14908 /* Clear the List and append the items in the sorted order. */
14909 l
->lv_first
= l
->lv_last
= NULL
;
14911 for (i
= 0; i
< len
; ++i
)
14912 list_append(l
, ptrs
[i
]);
14921 * "soundfold({word})" function
14924 f_soundfold(argvars
, rettv
)
14930 rettv
->v_type
= VAR_STRING
;
14931 s
= get_tv_string(&argvars
[0]);
14933 rettv
->vval
.v_string
= eval_soundfold(s
);
14935 rettv
->vval
.v_string
= vim_strsave(s
);
14940 * "spellbadword()" function
14944 f_spellbadword(argvars
, rettv
)
14948 char_u
*word
= (char_u
*)"";
14949 hlf_T attr
= HLF_COUNT
;
14952 if (rettv_list_alloc(rettv
) == FAIL
)
14956 if (argvars
[0].v_type
== VAR_UNKNOWN
)
14958 /* Find the start and length of the badly spelled word. */
14959 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
14961 word
= ml_get_cursor();
14963 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
14965 char_u
*str
= get_tv_string_chk(&argvars
[0]);
14970 /* Check the argument for spelling. */
14971 while (*str
!= NUL
)
14973 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
14974 if (attr
!= HLF_COUNT
)
14985 list_append_string(rettv
->vval
.v_list
, word
, len
);
14986 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
14987 attr
== HLF_SPB
? "bad" :
14988 attr
== HLF_SPR
? "rare" :
14989 attr
== HLF_SPL
? "local" :
14990 attr
== HLF_SPC
? "caps" :
14995 * "spellsuggest()" function
14999 f_spellsuggest(argvars
, rettv
)
15005 int typeerr
= FALSE
;
15010 int need_capital
= FALSE
;
15013 if (rettv_list_alloc(rettv
) == FAIL
)
15017 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15019 str
= get_tv_string(&argvars
[0]);
15020 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15022 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
15025 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15027 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
15035 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
15037 for (i
= 0; i
< ga
.ga_len
; ++i
)
15039 str
= ((char_u
**)ga
.ga_data
)[i
];
15041 li
= listitem_alloc();
15046 li
->li_tv
.v_type
= VAR_STRING
;
15047 li
->li_tv
.v_lock
= 0;
15048 li
->li_tv
.vval
.v_string
= str
;
15049 list_append(rettv
->vval
.v_list
, li
);
15058 f_split(argvars
, rettv
)
15064 char_u
*pat
= NULL
;
15065 regmatch_T regmatch
;
15066 char_u patbuf
[NUMBUFLEN
];
15070 int keepempty
= FALSE
;
15071 int typeerr
= FALSE
;
15073 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15075 p_cpo
= (char_u
*)"";
15077 str
= get_tv_string(&argvars
[0]);
15078 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15080 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
15083 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15084 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
15086 if (pat
== NULL
|| *pat
== NUL
)
15087 pat
= (char_u
*)"[\\x01- ]\\+";
15089 if (rettv_list_alloc(rettv
) == FAIL
)
15094 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
15095 if (regmatch
.regprog
!= NULL
)
15097 regmatch
.rm_ic
= FALSE
;
15098 while (*str
!= NUL
|| keepempty
)
15101 match
= FALSE
; /* empty item at the end */
15103 match
= vim_regexec_nl(®match
, str
, col
);
15105 end
= regmatch
.startp
[0];
15107 end
= str
+ STRLEN(str
);
15108 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
15109 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
15111 if (list_append_string(rettv
->vval
.v_list
, str
,
15112 (int)(end
- str
)) == FAIL
)
15117 /* Advance to just after the match. */
15118 if (regmatch
.endp
[0] > str
)
15122 /* Don't get stuck at the same match. */
15124 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
15129 str
= regmatch
.endp
[0];
15132 vim_free(regmatch
.regprog
);
15139 * "str2nr()" function
15142 f_str2nr(argvars
, rettv
)
15150 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15152 base
= get_tv_number(&argvars
[1]);
15153 if (base
!= 8 && base
!= 10 && base
!= 16)
15160 p
= skipwhite(get_tv_string(&argvars
[0]));
15161 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
15162 rettv
->vval
.v_number
= n
;
15165 #ifdef HAVE_STRFTIME
15167 * "strftime({format}[, {time}])" function
15170 f_strftime(argvars
, rettv
)
15174 char_u result_buf
[256];
15175 struct tm
*curtime
;
15179 rettv
->v_type
= VAR_STRING
;
15181 p
= get_tv_string(&argvars
[0]);
15182 if (argvars
[1].v_type
== VAR_UNKNOWN
)
15183 seconds
= time(NULL
);
15185 seconds
= (time_t)get_tv_number(&argvars
[1]);
15186 curtime
= localtime(&seconds
);
15187 /* MSVC returns NULL for an invalid value of seconds. */
15188 if (curtime
== NULL
)
15189 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
15196 conv
.vc_type
= CONV_NONE
;
15197 enc
= enc_locale();
15198 convert_setup(&conv
, p_enc
, enc
);
15199 if (conv
.vc_type
!= CONV_NONE
)
15200 p
= string_convert(&conv
, p
, NULL
);
15203 (void)strftime((char *)result_buf
, sizeof(result_buf
),
15204 (char *)p
, curtime
);
15206 result_buf
[0] = NUL
;
15209 if (conv
.vc_type
!= CONV_NONE
)
15211 convert_setup(&conv
, enc
, p_enc
);
15212 if (conv
.vc_type
!= CONV_NONE
)
15213 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
15216 rettv
->vval
.v_string
= vim_strsave(result_buf
);
15219 /* Release conversion descriptors */
15220 convert_setup(&conv
, NULL
, NULL
);
15228 * "stridx()" function
15231 f_stridx(argvars
, rettv
)
15235 char_u buf
[NUMBUFLEN
];
15238 char_u
*save_haystack
;
15242 needle
= get_tv_string_chk(&argvars
[1]);
15243 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
15244 rettv
->vval
.v_number
= -1;
15245 if (needle
== NULL
|| haystack
== NULL
)
15246 return; /* type error; errmsg already given */
15248 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15252 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
15253 if (error
|| start_idx
>= (int)STRLEN(haystack
))
15255 if (start_idx
>= 0)
15256 haystack
+= start_idx
;
15259 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
15261 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
15265 * "string()" function
15268 f_string(argvars
, rettv
)
15273 char_u numbuf
[NUMBUFLEN
];
15275 rettv
->v_type
= VAR_STRING
;
15276 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
15277 if (tofree
== NULL
)
15278 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
15282 * "strlen()" function
15285 f_strlen(argvars
, rettv
)
15289 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
15290 get_tv_string(&argvars
[0])));
15294 * "strpart()" function
15297 f_strpart(argvars
, rettv
)
15307 p
= get_tv_string(&argvars
[0]);
15308 slen
= (int)STRLEN(p
);
15310 n
= get_tv_number_chk(&argvars
[1], &error
);
15313 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15314 len
= get_tv_number(&argvars
[2]);
15316 len
= slen
- n
; /* default len: all bytes that are available. */
15319 * Only return the overlap between the specified part and the actual
15331 else if (n
+ len
> slen
)
15334 rettv
->v_type
= VAR_STRING
;
15335 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
15339 * "strridx()" function
15342 f_strridx(argvars
, rettv
)
15346 char_u buf
[NUMBUFLEN
];
15350 char_u
*lastmatch
= NULL
;
15351 int haystack_len
, end_idx
;
15353 needle
= get_tv_string_chk(&argvars
[1]);
15354 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
15356 rettv
->vval
.v_number
= -1;
15357 if (needle
== NULL
|| haystack
== NULL
)
15358 return; /* type error; errmsg already given */
15360 haystack_len
= (int)STRLEN(haystack
);
15361 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15363 /* Third argument: upper limit for index */
15364 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
15366 return; /* can never find a match */
15369 end_idx
= haystack_len
;
15371 if (*needle
== NUL
)
15373 /* Empty string matches past the end. */
15374 lastmatch
= haystack
+ end_idx
;
15378 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
15380 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
15381 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
15387 if (lastmatch
== NULL
)
15388 rettv
->vval
.v_number
= -1;
15390 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
15394 * "strtrans()" function
15397 f_strtrans(argvars
, rettv
)
15401 rettv
->v_type
= VAR_STRING
;
15402 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
15406 * "submatch()" function
15409 f_submatch(argvars
, rettv
)
15413 rettv
->v_type
= VAR_STRING
;
15414 rettv
->vval
.v_string
=
15415 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
15419 * "substitute()" function
15422 f_substitute(argvars
, rettv
)
15426 char_u patbuf
[NUMBUFLEN
];
15427 char_u subbuf
[NUMBUFLEN
];
15428 char_u flagsbuf
[NUMBUFLEN
];
15430 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15431 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
15432 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
15433 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
15435 rettv
->v_type
= VAR_STRING
;
15436 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
15437 rettv
->vval
.v_string
= NULL
;
15439 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
15443 * "synID(lnum, col, trans)" function
15447 f_synID(argvars
, rettv
)
15456 int transerr
= FALSE
;
15458 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
15459 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
15460 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
15462 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
15463 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
15464 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
);
15467 rettv
->vval
.v_number
= id
;
15471 * "synIDattr(id, what [, mode])" function
15475 f_synIDattr(argvars
, rettv
)
15484 char_u modebuf
[NUMBUFLEN
];
15487 id
= get_tv_number(&argvars
[0]);
15488 what
= get_tv_string(&argvars
[1]);
15489 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15491 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
15492 modec
= TOLOWER_ASC(mode
[0]);
15493 if (modec
!= 't' && modec
!= 'c'
15498 modec
= 0; /* replace invalid with current */
15514 switch (TOLOWER_ASC(what
[0]))
15517 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
15518 p
= highlight_color(id
, what
, modec
);
15520 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
15523 case 'f': /* fg[#] */
15524 p
= highlight_color(id
, what
, modec
);
15528 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
15529 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
15531 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
15534 case 'n': /* name */
15535 p
= get_highlight_name(NULL
, id
- 1);
15538 case 'r': /* reverse */
15539 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
15542 case 's': /* standout */
15543 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
15547 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
15549 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
15552 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
15557 p
= vim_strsave(p
);
15559 rettv
->v_type
= VAR_STRING
;
15560 rettv
->vval
.v_string
= p
;
15564 * "synIDtrans(id)" function
15568 f_synIDtrans(argvars
, rettv
)
15575 id
= get_tv_number(&argvars
[0]);
15578 id
= syn_get_final_id(id
);
15583 rettv
->vval
.v_number
= id
;
15587 * "system()" function
15590 f_system(argvars
, rettv
)
15594 char_u
*res
= NULL
;
15596 char_u
*infile
= NULL
;
15597 char_u buf
[NUMBUFLEN
];
15601 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15604 * Write the string to a temp file, to be used for input of the shell
15607 if ((infile
= vim_tempname('i')) == NULL
)
15613 fd
= mch_fopen((char *)infile
, WRITEBIN
);
15616 EMSG2(_(e_notopen
), infile
);
15619 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
15623 goto done
; /* type error; errmsg already given */
15625 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
15627 if (fclose(fd
) != 0)
15631 EMSG(_("E677: Error writing temp file"));
15636 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
15637 SHELL_SILENT
| SHELL_COOKED
);
15640 /* translate <CR> into <NL> */
15645 for (s
= res
; *s
; ++s
)
15653 /* translate <CR><NL> into <NL> */
15659 for (s
= res
; *s
; ++s
)
15661 if (s
[0] == CAR
&& s
[1] == NL
)
15671 if (infile
!= NULL
)
15673 mch_remove(infile
);
15676 rettv
->v_type
= VAR_STRING
;
15677 rettv
->vval
.v_string
= res
;
15681 * "tabpagebuflist()" function
15685 f_tabpagebuflist(argvars
, rettv
)
15689 #ifndef FEAT_WINDOWS
15690 rettv
->vval
.v_number
= 0;
15695 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15699 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
15701 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
15704 rettv
->vval
.v_number
= 0;
15707 if (rettv_list_alloc(rettv
) == FAIL
)
15708 rettv
->vval
.v_number
= 0;
15711 for (; wp
!= NULL
; wp
= wp
->w_next
)
15712 if (list_append_number(rettv
->vval
.v_list
,
15713 wp
->w_buffer
->b_fnum
) == FAIL
)
15722 * "tabpagenr()" function
15726 f_tabpagenr(argvars
, rettv
)
15731 #ifdef FEAT_WINDOWS
15734 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
15736 arg
= get_tv_string_chk(&argvars
[0]);
15740 if (STRCMP(arg
, "$") == 0)
15741 nr
= tabpage_index(NULL
) - 1;
15743 EMSG2(_(e_invexpr2
), arg
);
15747 nr
= tabpage_index(curtab
);
15749 rettv
->vval
.v_number
= nr
;
15753 #ifdef FEAT_WINDOWS
15754 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
15757 * Common code for tabpagewinnr() and winnr().
15760 get_winnr(tp
, argvar
)
15769 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
15770 if (argvar
->v_type
!= VAR_UNKNOWN
)
15772 arg
= get_tv_string_chk(argvar
);
15774 nr
= 0; /* type error; errmsg already given */
15775 else if (STRCMP(arg
, "$") == 0)
15776 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
15777 else if (STRCMP(arg
, "#") == 0)
15779 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
15785 EMSG2(_(e_invexpr2
), arg
);
15791 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
15792 wp
!= twin
; wp
= wp
->w_next
)
15796 /* didn't find it in this tabpage */
15807 * "tabpagewinnr()" function
15811 f_tabpagewinnr(argvars
, rettv
)
15816 #ifdef FEAT_WINDOWS
15819 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
15823 nr
= get_winnr(tp
, &argvars
[1]);
15825 rettv
->vval
.v_number
= nr
;
15830 * "tagfiles()" function
15834 f_tagfiles(argvars
, rettv
)
15838 char_u fname
[MAXPATHL
+ 1];
15842 if (rettv_list_alloc(rettv
) == FAIL
)
15844 rettv
->vval
.v_number
= 0;
15848 for (first
= TRUE
; ; first
= FALSE
)
15849 if (get_tagfname(&tn
, first
, fname
) == FAIL
15850 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
15856 * "taglist()" function
15859 f_taglist(argvars
, rettv
)
15863 char_u
*tag_pattern
;
15865 tag_pattern
= get_tv_string(&argvars
[0]);
15867 rettv
->vval
.v_number
= FALSE
;
15868 if (*tag_pattern
== NUL
)
15871 if (rettv_list_alloc(rettv
) == OK
)
15872 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
15876 * "tempname()" function
15880 f_tempname(argvars
, rettv
)
15884 static int x
= 'A';
15886 rettv
->v_type
= VAR_STRING
;
15887 rettv
->vval
.v_string
= vim_tempname(x
);
15889 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15890 * names. Skip 'I' and 'O', they are used for shell redirection. */
15908 } while (x
== 'I' || x
== 'O');
15912 * "test(list)" function: Just checking the walls...
15916 f_test(argvars
, rettv
)
15920 /* Used for unit testing. Change the code below to your liking. */
15924 char_u
*bad
, *good
;
15926 if (argvars
[0].v_type
!= VAR_LIST
)
15928 l
= argvars
[0].vval
.v_list
;
15934 bad
= get_tv_string(&li
->li_tv
);
15938 good
= get_tv_string(&li
->li_tv
);
15939 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
15944 * "tolower(string)" function
15947 f_tolower(argvars
, rettv
)
15953 p
= vim_strsave(get_tv_string(&argvars
[0]));
15954 rettv
->v_type
= VAR_STRING
;
15955 rettv
->vval
.v_string
= p
;
15967 c
= utf_ptr2char(p
);
15968 lc
= utf_tolower(c
);
15969 l
= utf_ptr2len(p
);
15970 /* TODO: reallocate string when byte count changes. */
15971 if (utf_char2len(lc
) == l
)
15972 utf_char2bytes(lc
, p
);
15975 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
15976 p
+= l
; /* skip multi-byte character */
15980 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
15987 * "toupper(string)" function
15990 f_toupper(argvars
, rettv
)
15994 rettv
->v_type
= VAR_STRING
;
15995 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
15999 * "tr(string, fromstr, tostr)" function
16002 f_tr(argvars
, rettv
)
16019 char_u buf
[NUMBUFLEN
];
16020 char_u buf2
[NUMBUFLEN
];
16023 instr
= get_tv_string(&argvars
[0]);
16024 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
16025 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
16027 /* Default return value: empty string. */
16028 rettv
->v_type
= VAR_STRING
;
16029 rettv
->vval
.v_string
= NULL
;
16030 if (fromstr
== NULL
|| tostr
== NULL
)
16031 return; /* type error; errmsg already given */
16032 ga_init2(&ga
, (int)sizeof(char), 80);
16037 /* not multi-byte: fromstr and tostr must be the same length */
16038 if (STRLEN(fromstr
) != STRLEN(tostr
))
16043 EMSG2(_(e_invarg2
), fromstr
);
16048 /* fromstr and tostr have to contain the same number of chars */
16049 while (*instr
!= NUL
)
16054 inlen
= (*mb_ptr2len
)(instr
);
16058 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
16060 fromlen
= (*mb_ptr2len
)(p
);
16061 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
16063 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
16065 tolen
= (*mb_ptr2len
)(p
);
16073 if (*p
== NUL
) /* tostr is shorter than fromstr */
16080 if (first
&& cpstr
== instr
)
16082 /* Check that fromstr and tostr have the same number of
16083 * (multi-byte) characters. Done only once when a character
16084 * of instr doesn't appear in fromstr. */
16086 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
16088 tolen
= (*mb_ptr2len
)(p
);
16095 ga_grow(&ga
, cplen
);
16096 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
16097 ga
.ga_len
+= cplen
;
16104 /* When not using multi-byte chars we can do it faster. */
16105 p
= vim_strchr(fromstr
, *instr
);
16107 ga_append(&ga
, tostr
[p
- fromstr
]);
16109 ga_append(&ga
, *instr
);
16114 /* add a terminating NUL */
16116 ga_append(&ga
, NUL
);
16118 rettv
->vval
.v_string
= ga
.ga_data
;
16122 * "type(expr)" function
16125 f_type(argvars
, rettv
)
16131 switch (argvars
[0].v_type
)
16133 case VAR_NUMBER
: n
= 0; break;
16134 case VAR_STRING
: n
= 1; break;
16135 case VAR_FUNC
: n
= 2; break;
16136 case VAR_LIST
: n
= 3; break;
16137 case VAR_DICT
: n
= 4; break;
16138 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
16140 rettv
->vval
.v_number
= n
;
16144 * "values(dict)" function
16147 f_values(argvars
, rettv
)
16151 dict_list(argvars
, rettv
, 1);
16155 * "virtcol(string)" function
16158 f_virtcol(argvars
, rettv
)
16164 int fnum
= curbuf
->b_fnum
;
16166 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
16167 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
16168 && fnum
== curbuf
->b_fnum
)
16170 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
16174 rettv
->vval
.v_number
= vcol
;
16178 * "visualmode()" function
16182 f_visualmode(argvars
, rettv
)
16189 rettv
->v_type
= VAR_STRING
;
16190 str
[0] = curbuf
->b_visual_mode_eval
;
16192 rettv
->vval
.v_string
= vim_strsave(str
);
16194 /* A non-zero number or non-empty string argument: reset mode. */
16195 if ((argvars
[0].v_type
== VAR_NUMBER
16196 && argvars
[0].vval
.v_number
!= 0)
16197 || (argvars
[0].v_type
== VAR_STRING
16198 && *get_tv_string(&argvars
[0]) != NUL
))
16199 curbuf
->b_visual_mode_eval
= NUL
;
16201 rettv
->vval
.v_number
= 0; /* return anything, it won't work anyway */
16206 * "winbufnr(nr)" function
16209 f_winbufnr(argvars
, rettv
)
16215 wp
= find_win_by_nr(&argvars
[0], NULL
);
16217 rettv
->vval
.v_number
= -1;
16219 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
16223 * "wincol()" function
16227 f_wincol(argvars
, rettv
)
16232 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
16236 * "winheight(nr)" function
16239 f_winheight(argvars
, rettv
)
16245 wp
= find_win_by_nr(&argvars
[0], NULL
);
16247 rettv
->vval
.v_number
= -1;
16249 rettv
->vval
.v_number
= wp
->w_height
;
16253 * "winline()" function
16257 f_winline(argvars
, rettv
)
16262 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
16266 * "winnr()" function
16270 f_winnr(argvars
, rettv
)
16276 #ifdef FEAT_WINDOWS
16277 nr
= get_winnr(curtab
, &argvars
[0]);
16279 rettv
->vval
.v_number
= nr
;
16283 * "winrestcmd()" function
16287 f_winrestcmd(argvars
, rettv
)
16291 #ifdef FEAT_WINDOWS
16297 ga_init2(&ga
, (int)sizeof(char), 70);
16298 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
16300 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
16301 ga_concat(&ga
, buf
);
16302 # ifdef FEAT_VERTSPLIT
16303 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
16304 ga_concat(&ga
, buf
);
16308 ga_append(&ga
, NUL
);
16310 rettv
->vval
.v_string
= ga
.ga_data
;
16312 rettv
->vval
.v_string
= NULL
;
16314 rettv
->v_type
= VAR_STRING
;
16318 * "winrestview()" function
16322 f_winrestview(argvars
, rettv
)
16328 if (argvars
[0].v_type
!= VAR_DICT
16329 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
16333 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
16334 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
16335 #ifdef FEAT_VIRTUALEDIT
16336 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
16338 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
16339 curwin
->w_set_curswant
= FALSE
;
16341 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
16343 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
16345 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
16346 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
16349 changed_cline_bef_curs();
16350 invalidate_botline();
16351 redraw_later(VALID
);
16353 if (curwin
->w_topline
== 0)
16354 curwin
->w_topline
= 1;
16355 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
16356 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
16358 check_topfill(curwin
, TRUE
);
16364 * "winsaveview()" function
16368 f_winsaveview(argvars
, rettv
)
16374 dict
= dict_alloc();
16377 rettv
->v_type
= VAR_DICT
;
16378 rettv
->vval
.v_dict
= dict
;
16379 ++dict
->dv_refcount
;
16381 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
16382 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
16383 #ifdef FEAT_VIRTUALEDIT
16384 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
16387 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
16389 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
16391 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
16393 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
16394 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
16398 * "winwidth(nr)" function
16401 f_winwidth(argvars
, rettv
)
16407 wp
= find_win_by_nr(&argvars
[0], NULL
);
16409 rettv
->vval
.v_number
= -1;
16411 #ifdef FEAT_VERTSPLIT
16412 rettv
->vval
.v_number
= wp
->w_width
;
16414 rettv
->vval
.v_number
= Columns
;
16419 * "writefile()" function
16422 f_writefile(argvars
, rettv
)
16426 int binary
= FALSE
;
16434 if (argvars
[0].v_type
!= VAR_LIST
)
16436 EMSG2(_(e_listarg
), "writefile()");
16439 if (argvars
[0].vval
.v_list
== NULL
)
16442 if (argvars
[2].v_type
!= VAR_UNKNOWN
16443 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
16446 /* Always open the file in binary mode, library functions have a mind of
16447 * their own about CR-LF conversion. */
16448 fname
= get_tv_string(&argvars
[1]);
16449 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
16451 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
16456 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
16459 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
16471 if (!binary
|| li
->li_next
!= NULL
)
16472 if (putc('\n', fd
) == EOF
)
16486 rettv
->vval
.v_number
= ret
;
16490 * Translate a String variable into a position.
16491 * Returns NULL when there is an error.
16494 var2fpos(varp
, lnum
, fnum
)
16496 int lnum
; /* TRUE when $ is last line */
16497 int *fnum
; /* set to fnum for '0, 'A, etc. */
16503 /* Argument can be [lnum, col, coladd]. */
16504 if (varp
->v_type
== VAR_LIST
)
16510 l
= varp
->vval
.v_list
;
16514 /* Get the line number */
16515 pos
.lnum
= list_find_nr(l
, 0L, &error
);
16516 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
16517 return NULL
; /* invalid line number */
16519 /* Get the column number */
16520 pos
.col
= list_find_nr(l
, 1L, &error
);
16523 len
= (long)STRLEN(ml_get(pos
.lnum
));
16524 /* Accept a position up to the NUL after the line. */
16525 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
16526 return NULL
; /* invalid column number */
16529 #ifdef FEAT_VIRTUALEDIT
16530 /* Get the virtual offset. Defaults to zero. */
16531 pos
.coladd
= list_find_nr(l
, 2L, &error
);
16539 name
= get_tv_string_chk(varp
);
16542 if (name
[0] == '.') /* cursor */
16543 return &curwin
->w_cursor
;
16544 if (name
[0] == '\'') /* mark */
16546 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
16547 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
16552 #ifdef FEAT_VIRTUALEDIT
16556 if (name
[0] == 'w' && lnum
)
16559 if (name
[1] == '0') /* "w0": first visible line */
16562 pos
.lnum
= curwin
->w_topline
;
16565 else if (name
[1] == '$') /* "w$": last visible line */
16567 validate_botline();
16568 pos
.lnum
= curwin
->w_botline
- 1;
16572 else if (name
[0] == '$') /* last column or line */
16576 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
16581 pos
.lnum
= curwin
->w_cursor
.lnum
;
16582 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
16590 * Convert list in "arg" into a position and optional file number.
16591 * When "fnump" is NULL there is no file number, only 3 items.
16592 * Note that the column is passed on as-is, the caller may want to decrement
16593 * it to use 1 for the first column.
16594 * Return FAIL when conversion is not possible, doesn't check the position for
16598 list2fpos(arg
, posp
, fnump
)
16603 list_T
*l
= arg
->vval
.v_list
;
16607 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16608 * when "fnump" isn't NULL and "coladd" is optional. */
16609 if (arg
->v_type
!= VAR_LIST
16611 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
16612 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
16617 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
16621 n
= curbuf
->b_fnum
; /* current buffer */
16625 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
16630 n
= list_find_nr(l
, i
++, NULL
); /* col */
16635 #ifdef FEAT_VIRTUALEDIT
16636 n
= list_find_nr(l
, i
, NULL
);
16647 * Get the length of an environment variable name.
16648 * Advance "arg" to the first character after the name.
16649 * Return 0 for error.
16658 for (p
= *arg
; vim_isIDc(*p
); ++p
)
16660 if (p
== *arg
) /* no name found */
16663 len
= (int)(p
- *arg
);
16669 * Get the length of the name of a function or internal variable.
16670 * "arg" is advanced to the first non-white character after the name.
16671 * Return 0 if something is wrong.
16680 /* Find the end of the name. */
16681 for (p
= *arg
; eval_isnamec(*p
); ++p
)
16683 if (p
== *arg
) /* no name found */
16686 len
= (int)(p
- *arg
);
16687 *arg
= skipwhite(p
);
16693 * Get the length of the name of a variable or function.
16694 * Only the name is recognized, does not handle ".key" or "[idx]".
16695 * "arg" is advanced to the first non-white character after the name.
16696 * Return -1 if curly braces expansion failed.
16697 * Return 0 if something else is wrong.
16698 * If the name contains 'magic' {}'s, expand them and return the
16699 * expanded name in an allocated string via 'alias' - caller must free.
16702 get_name_len(arg
, alias
, evaluate
, verbose
)
16710 char_u
*expr_start
;
16713 *alias
= NULL
; /* default to no alias */
16715 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
16716 && (*arg
)[2] == (int)KE_SNR
)
16718 /* hard coded <SNR>, already translated */
16720 return get_id_len(arg
) + 3;
16722 len
= eval_fname_script(*arg
);
16725 /* literal "<SID>", "s:" or "<SNR>" */
16730 * Find the end of the name; check for {} construction.
16732 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
16733 len
> 0 ? 0 : FNE_CHECK_START
);
16734 if (expr_start
!= NULL
)
16736 char_u
*temp_string
;
16740 len
+= (int)(p
- *arg
);
16741 *arg
= skipwhite(p
);
16746 * Include any <SID> etc in the expanded string:
16747 * Thus the -len here.
16749 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
16750 if (temp_string
== NULL
)
16752 *alias
= temp_string
;
16753 *arg
= skipwhite(p
);
16754 return (int)STRLEN(temp_string
);
16757 len
+= get_id_len(arg
);
16758 if (len
== 0 && verbose
)
16759 EMSG2(_(e_invexpr2
), *arg
);
16765 * Find the end of a variable or function name, taking care of magic braces.
16766 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16767 * start and end of the first magic braces item.
16768 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
16769 * Return a pointer to just after the name. Equal to "arg" if there is no
16773 find_name_end(arg
, expr_start
, expr_end
, flags
)
16775 char_u
**expr_start
;
16783 if (expr_start
!= NULL
)
16785 *expr_start
= NULL
;
16789 /* Quick check for valid starting character. */
16790 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
16793 for (p
= arg
; *p
!= NUL
16794 && (eval_isnamec(*p
)
16796 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
16798 || br_nest
!= 0); mb_ptr_adv(p
))
16802 /* skip over 'string' to avoid counting [ and ] inside it. */
16803 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
16808 else if (*p
== '"')
16810 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16811 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
16812 if (*p
== '\\' && p
[1] != NUL
)
16822 else if (*p
== ']')
16831 if (expr_start
!= NULL
&& *expr_start
== NULL
)
16834 else if (*p
== '}')
16837 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
16847 * Expands out the 'magic' {}'s in a variable/function name.
16848 * Note that this can call itself recursively, to deal with
16849 * constructs like foo{bar}{baz}{bam}
16850 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16856 * Returns a new allocated string, which the caller must free.
16857 * Returns NULL for failure.
16860 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
16862 char_u
*expr_start
;
16867 char_u
*retval
= NULL
;
16868 char_u
*temp_result
;
16869 char_u
*nextcmd
= NULL
;
16871 if (expr_end
== NULL
|| in_end
== NULL
)
16878 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
16879 if (temp_result
!= NULL
&& nextcmd
== NULL
)
16881 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
16882 + (in_end
- expr_end
) + 1));
16883 if (retval
!= NULL
)
16885 STRCPY(retval
, in_start
);
16886 STRCAT(retval
, temp_result
);
16887 STRCAT(retval
, expr_end
+ 1);
16890 vim_free(temp_result
);
16892 *in_end
= c1
; /* put char back for error messages */
16896 if (retval
!= NULL
)
16898 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
16899 if (expr_start
!= NULL
)
16901 /* Further expansion! */
16902 temp_result
= make_expanded_name(retval
, expr_start
,
16903 expr_end
, temp_result
);
16905 retval
= temp_result
;
16913 * Return TRUE if character "c" can be used in a variable or function name.
16914 * Does not include '{' or '}' for magic braces.
16920 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
16924 * Return TRUE if character "c" can be used as the first character in a
16925 * variable or function name (excluding '{' and '}').
16931 return (ASCII_ISALPHA(c
) || c
== '_');
16935 * Set number v: variable to "val".
16938 set_vim_var_nr(idx
, val
)
16942 vimvars
[idx
].vv_nr
= val
;
16946 * Get number v: variable value.
16949 get_vim_var_nr(idx
)
16952 return vimvars
[idx
].vv_nr
;
16955 #if defined(FEAT_AUTOCMD) || defined(PROTO)
16957 * Get string v: variable value. Uses a static buffer, can only be used once.
16960 get_vim_var_str(idx
)
16963 return get_tv_string(&vimvars
[idx
].vv_tv
);
16968 * Set v:count, v:count1 and v:prevcount.
16971 set_vcount(count
, count1
)
16975 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
16976 vimvars
[VV_COUNT
].vv_nr
= count
;
16977 vimvars
[VV_COUNT1
].vv_nr
= count1
;
16981 * Set string v: variable to a copy of "val".
16984 set_vim_var_string(idx
, val
, len
)
16987 int len
; /* length of "val" to use or -1 (whole string) */
16989 /* Need to do this (at least) once, since we can't initialize a union.
16990 * Will always be invoked when "v:progname" is set. */
16991 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
16993 vim_free(vimvars
[idx
].vv_str
);
16995 vimvars
[idx
].vv_str
= NULL
;
16996 else if (len
== -1)
16997 vimvars
[idx
].vv_str
= vim_strsave(val
);
16999 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
17003 * Set v:register if needed.
17011 if (c
== 0 || c
== ' ')
17015 /* Avoid free/alloc when the value is already right. */
17016 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
17017 set_vim_var_string(VV_REG
, ®name
, 1);
17021 * Get or set v:exception. If "oldval" == NULL, return the current value.
17022 * Otherwise, restore the value to "oldval" and return NULL.
17023 * Must always be called in pairs to save and restore v:exception! Does not
17024 * take care of memory allocations.
17027 v_exception(oldval
)
17030 if (oldval
== NULL
)
17031 return vimvars
[VV_EXCEPTION
].vv_str
;
17033 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
17038 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17039 * Otherwise, restore the value to "oldval" and return NULL.
17040 * Must always be called in pairs to save and restore v:throwpoint! Does not
17041 * take care of memory allocations.
17044 v_throwpoint(oldval
)
17047 if (oldval
== NULL
)
17048 return vimvars
[VV_THROWPOINT
].vv_str
;
17050 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
17054 #if defined(FEAT_AUTOCMD) || defined(PROTO)
17057 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17058 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17059 * Must always be called in pairs!
17062 set_cmdarg(eap
, oldarg
)
17070 oldval
= vimvars
[VV_CMDARG
].vv_str
;
17074 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
17078 if (eap
->force_bin
== FORCE_BIN
)
17080 else if (eap
->force_bin
== FORCE_NOBIN
)
17085 if (eap
->read_edit
)
17088 if (eap
->force_ff
!= 0)
17089 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
17091 if (eap
->force_enc
!= 0)
17092 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
17093 if (eap
->bad_char
!= 0)
17094 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
17097 newval
= alloc(len
+ 1);
17098 if (newval
== NULL
)
17101 if (eap
->force_bin
== FORCE_BIN
)
17102 sprintf((char *)newval
, " ++bin");
17103 else if (eap
->force_bin
== FORCE_NOBIN
)
17104 sprintf((char *)newval
, " ++nobin");
17108 if (eap
->read_edit
)
17109 STRCAT(newval
, " ++edit");
17111 if (eap
->force_ff
!= 0)
17112 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
17113 eap
->cmd
+ eap
->force_ff
);
17115 if (eap
->force_enc
!= 0)
17116 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
17117 eap
->cmd
+ eap
->force_enc
);
17118 if (eap
->bad_char
!= 0)
17119 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
17120 eap
->cmd
+ eap
->bad_char
);
17122 vimvars
[VV_CMDARG
].vv_str
= newval
;
17128 * Get the value of internal variable "name".
17129 * Return OK or FAIL.
17132 get_var_tv(name
, len
, rettv
, verbose
)
17134 int len
; /* length of "name" */
17135 typval_T
*rettv
; /* NULL when only checking existence */
17136 int verbose
; /* may give error message */
17139 typval_T
*tv
= NULL
;
17144 /* truncate the name, so that we can use strcmp() */
17149 * Check for "b:changedtick".
17151 if (STRCMP(name
, "b:changedtick") == 0)
17153 atv
.v_type
= VAR_NUMBER
;
17154 atv
.vval
.v_number
= curbuf
->b_changedtick
;
17159 * Check for user-defined variables.
17163 v
= find_var(name
, NULL
);
17170 if (rettv
!= NULL
&& verbose
)
17171 EMSG2(_(e_undefvar
), name
);
17174 else if (rettv
!= NULL
)
17175 copy_tv(tv
, rettv
);
17183 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17184 * Also handle function call with Funcref variable: func(expr)
17185 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17188 handle_subscript(arg
, rettv
, evaluate
, verbose
)
17191 int evaluate
; /* do more than finding the end */
17192 int verbose
; /* give error messages */
17195 dict_T
*selfdict
= NULL
;
17202 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
17203 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
17204 && !vim_iswhite(*(*arg
- 1)))
17208 /* need to copy the funcref so that we can clear rettv */
17210 rettv
->v_type
= VAR_UNKNOWN
;
17212 /* Invoke the function. Recursive! */
17213 s
= functv
.vval
.v_string
;
17214 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
17215 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
17216 &len
, evaluate
, selfdict
);
17218 /* Clear the funcref afterwards, so that deleting it while
17219 * evaluating the arguments is possible (see test55). */
17222 /* Stop the expression evaluation when immediately aborting on
17223 * error, or when an interrupt occurred or an exception was thrown
17224 * but not caught. */
17231 dict_unref(selfdict
);
17234 else /* **arg == '[' || **arg == '.' */
17236 dict_unref(selfdict
);
17237 if (rettv
->v_type
== VAR_DICT
)
17239 selfdict
= rettv
->vval
.v_dict
;
17240 if (selfdict
!= NULL
)
17241 ++selfdict
->dv_refcount
;
17245 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
17252 dict_unref(selfdict
);
17257 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17263 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
17267 * Allocate memory for a variable type-value, and assign a string to it.
17268 * The string "s" must have been allocated, it is consumed.
17269 * Return NULL for out of memory, the variable otherwise.
17277 rettv
= alloc_tv();
17280 rettv
->v_type
= VAR_STRING
;
17281 rettv
->vval
.v_string
= s
;
17289 * Free the memory for a variable type-value.
17297 switch (varp
->v_type
)
17300 func_unref(varp
->vval
.v_string
);
17303 vim_free(varp
->vval
.v_string
);
17306 list_unref(varp
->vval
.v_list
);
17309 dict_unref(varp
->vval
.v_dict
);
17315 EMSG2(_(e_intern2
), "free_tv()");
17323 * Free the memory for a variable value and set the value to NULL or 0.
17331 switch (varp
->v_type
)
17334 func_unref(varp
->vval
.v_string
);
17337 vim_free(varp
->vval
.v_string
);
17338 varp
->vval
.v_string
= NULL
;
17341 list_unref(varp
->vval
.v_list
);
17342 varp
->vval
.v_list
= NULL
;
17345 dict_unref(varp
->vval
.v_dict
);
17346 varp
->vval
.v_dict
= NULL
;
17349 varp
->vval
.v_number
= 0;
17354 EMSG2(_(e_intern2
), "clear_tv()");
17361 * Set the value of a variable to NULL without freeing items.
17368 vim_memset(varp
, 0, sizeof(typval_T
));
17372 * Get the number value of a variable.
17373 * If it is a String variable, uses vim_str2nr().
17374 * For incompatible types, return 0.
17375 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17376 * caller of incompatible types: it sets *denote to TRUE if "denote"
17377 * is not NULL or returns -1 otherwise.
17380 get_tv_number(varp
)
17385 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
17389 get_tv_number_chk(varp
, denote
)
17395 switch (varp
->v_type
)
17398 return (long)(varp
->vval
.v_number
);
17400 EMSG(_("E703: Using a Funcref as a number"));
17403 if (varp
->vval
.v_string
!= NULL
)
17404 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
17405 TRUE
, TRUE
, &n
, NULL
);
17408 EMSG(_("E745: Using a List as a number"));
17411 EMSG(_("E728: Using a Dictionary as a number"));
17414 EMSG2(_(e_intern2
), "get_tv_number()");
17417 if (denote
== NULL
) /* useful for values that must be unsigned */
17425 * Get the lnum from the first argument.
17426 * Also accepts ".", "$", etc., but that only works for the current buffer.
17427 * Returns -1 on error.
17430 get_tv_lnum(argvars
)
17436 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
17437 if (lnum
== 0) /* no valid number, try using line() */
17439 rettv
.v_type
= VAR_NUMBER
;
17440 f_line(argvars
, &rettv
);
17441 lnum
= rettv
.vval
.v_number
;
17448 * Get the lnum from the first argument.
17449 * Also accepts "$", then "buf" is used.
17450 * Returns 0 on error.
17453 get_tv_lnum_buf(argvars
, buf
)
17457 if (argvars
[0].v_type
== VAR_STRING
17458 && argvars
[0].vval
.v_string
!= NULL
17459 && argvars
[0].vval
.v_string
[0] == '$'
17461 return buf
->b_ml
.ml_line_count
;
17462 return get_tv_number_chk(&argvars
[0], NULL
);
17466 * Get the string value of a variable.
17467 * If it is a Number variable, the number is converted into a string.
17468 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17469 * get_tv_string_buf() uses a given buffer.
17470 * If the String variable has never been set, return an empty string.
17471 * Never returns NULL;
17472 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17476 get_tv_string(varp
)
17479 static char_u mybuf
[NUMBUFLEN
];
17481 return get_tv_string_buf(varp
, mybuf
);
17485 get_tv_string_buf(varp
, buf
)
17489 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
17491 return res
!= NULL
? res
: (char_u
*)"";
17495 get_tv_string_chk(varp
)
17498 static char_u mybuf
[NUMBUFLEN
];
17500 return get_tv_string_buf_chk(varp
, mybuf
);
17504 get_tv_string_buf_chk(varp
, buf
)
17508 switch (varp
->v_type
)
17511 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
17514 EMSG(_("E729: using Funcref as a String"));
17517 EMSG(_("E730: using List as a String"));
17520 EMSG(_("E731: using Dictionary as a String"));
17523 if (varp
->vval
.v_string
!= NULL
)
17524 return varp
->vval
.v_string
;
17525 return (char_u
*)"";
17527 EMSG2(_(e_intern2
), "get_tv_string_buf()");
17534 * Find variable "name" in the list of variables.
17535 * Return a pointer to it if found, NULL if not found.
17536 * Careful: "a:0" variables don't have a name.
17537 * When "htp" is not NULL we are writing to the variable, set "htp" to the
17540 static dictitem_T
*
17541 find_var(name
, htp
)
17548 ht
= find_var_ht(name
, &varname
);
17553 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
17557 * Find variable "varname" in hashtab "ht".
17558 * Returns NULL if not found.
17560 static dictitem_T
*
17561 find_var_in_ht(ht
, varname
, writing
)
17568 if (*varname
== NUL
)
17570 /* Must be something like "s:", otherwise "ht" would be NULL. */
17571 switch (varname
[-2])
17573 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
17574 case 'g': return &globvars_var
;
17575 case 'v': return &vimvars_var
;
17576 case 'b': return &curbuf
->b_bufvar
;
17577 case 'w': return &curwin
->w_winvar
;
17578 #ifdef FEAT_WINDOWS
17579 case 't': return &curtab
->tp_winvar
;
17581 case 'l': return current_funccal
== NULL
17582 ? NULL
: ¤t_funccal
->l_vars_var
;
17583 case 'a': return current_funccal
== NULL
17584 ? NULL
: ¤t_funccal
->l_avars_var
;
17589 hi
= hash_find(ht
, varname
);
17590 if (HASHITEM_EMPTY(hi
))
17592 /* For global variables we may try auto-loading the script. If it
17593 * worked find the variable again. Don't auto-load a script if it was
17594 * loaded already, otherwise it would be loaded every time when
17595 * checking if a function name is a Funcref variable. */
17596 if (ht
== &globvarht
&& !writing
17597 && script_autoload(varname
, FALSE
) && !aborting())
17598 hi
= hash_find(ht
, varname
);
17599 if (HASHITEM_EMPTY(hi
))
17606 * Find the hashtab used for a variable name.
17607 * Set "varname" to the start of name without ':'.
17610 find_var_ht(name
, varname
)
17616 if (name
[1] != ':')
17618 /* The name must not start with a colon or #. */
17619 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
17623 /* "version" is "v:version" in all scopes */
17624 hi
= hash_find(&compat_hashtab
, name
);
17625 if (!HASHITEM_EMPTY(hi
))
17626 return &compat_hashtab
;
17628 if (current_funccal
== NULL
)
17629 return &globvarht
; /* global variable */
17630 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
17632 *varname
= name
+ 2;
17633 if (*name
== 'g') /* global variable */
17635 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17637 if (vim_strchr(name
+ 2, ':') != NULL
17638 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
17640 if (*name
== 'b') /* buffer variable */
17641 return &curbuf
->b_vars
.dv_hashtab
;
17642 if (*name
== 'w') /* window variable */
17643 return &curwin
->w_vars
.dv_hashtab
;
17644 #ifdef FEAT_WINDOWS
17645 if (*name
== 't') /* tab page variable */
17646 return &curtab
->tp_vars
.dv_hashtab
;
17648 if (*name
== 'v') /* v: variable */
17650 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
17651 return ¤t_funccal
->l_avars
.dv_hashtab
;
17652 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
17653 return ¤t_funccal
->l_vars
.dv_hashtab
;
17654 if (*name
== 's' /* script variable */
17655 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
17656 return &SCRIPT_VARS(current_SID
);
17661 * Get the string value of a (global/local) variable.
17662 * Returns NULL when it doesn't exist.
17665 get_var_value(name
)
17670 v
= find_var(name
, NULL
);
17673 return get_tv_string(&v
->di_tv
);
17677 * Allocate a new hashtab for a sourced script. It will be used while
17678 * sourcing this script and when executing functions defined in the script.
17681 new_script_vars(id
)
17688 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
17690 /* Re-allocating ga_data means that an ht_array pointing to
17691 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
17692 * at its init value. Also reset "v_dict", it's always the same. */
17693 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
17695 ht
= &SCRIPT_VARS(i
);
17696 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
17697 ht
->ht_array
= ht
->ht_smallarray
;
17698 sv
= &SCRIPT_SV(i
);
17699 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
17702 while (ga_scripts
.ga_len
< id
)
17704 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
17705 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
17706 ++ga_scripts
.ga_len
;
17712 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17716 init_var_dict(dict
, dict_var
)
17718 dictitem_T
*dict_var
;
17720 hash_init(&dict
->dv_hashtab
);
17721 dict
->dv_refcount
= 99999;
17722 dict_var
->di_tv
.vval
.v_dict
= dict
;
17723 dict_var
->di_tv
.v_type
= VAR_DICT
;
17724 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
17725 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
17726 dict_var
->di_key
[0] = NUL
;
17730 * Clean up a list of internal variables.
17731 * Frees all allocated variables and the value they contain.
17732 * Clears hashtab "ht", does not free it.
17738 vars_clear_ext(ht
, TRUE
);
17742 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17745 vars_clear_ext(ht
, free_val
)
17754 todo
= (int)ht
->ht_used
;
17755 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
17757 if (!HASHITEM_EMPTY(hi
))
17761 /* Free the variable. Don't remove it from the hashtab,
17762 * ht_array might change then. hash_clear() takes care of it
17766 clear_tv(&v
->di_tv
);
17767 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
17776 * Delete a variable from hashtab "ht" at item "hi".
17777 * Clear the variable value and free the dictitem.
17784 dictitem_T
*di
= HI2DI(hi
);
17786 hash_remove(ht
, hi
);
17787 clear_tv(&di
->di_tv
);
17792 * List the value of one internal variable.
17795 list_one_var(v
, prefix
)
17801 char_u numbuf
[NUMBUFLEN
];
17803 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
17804 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
17805 s
== NULL
? (char_u
*)"" : s
);
17810 list_one_var_a(prefix
, name
, type
, string
)
17816 msg_attr(prefix
, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17817 if (name
!= NULL
) /* "a:" vars don't have a name stored */
17821 if (type
== VAR_NUMBER
)
17823 else if (type
== VAR_FUNC
)
17825 else if (type
== VAR_LIST
)
17828 if (*string
== '[')
17831 else if (type
== VAR_DICT
)
17834 if (*string
== '{')
17840 msg_outtrans(string
);
17842 if (type
== VAR_FUNC
)
17843 msg_puts((char_u
*)"()");
17847 * Set variable "name" to value in "tv".
17848 * If the variable already exists, the value is updated.
17849 * Otherwise the variable is created.
17852 set_var(name
, tv
, copy
)
17855 int copy
; /* make copy of value in "tv" */
17862 if (tv
->v_type
== VAR_FUNC
)
17864 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
17865 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
17866 ? name
[2] : name
[0]))
17868 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
17871 if (function_exists(name
))
17873 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
17879 ht
= find_var_ht(name
, &varname
);
17880 if (ht
== NULL
|| *varname
== NUL
)
17882 EMSG2(_(e_illvar
), name
);
17886 v
= find_var_in_ht(ht
, varname
, TRUE
);
17889 /* existing variable, need to clear the value */
17890 if (var_check_ro(v
->di_flags
, name
)
17891 || tv_check_lock(v
->di_tv
.v_lock
, name
))
17893 if (v
->di_tv
.v_type
!= tv
->v_type
17894 && !((v
->di_tv
.v_type
== VAR_STRING
17895 || v
->di_tv
.v_type
== VAR_NUMBER
)
17896 && (tv
->v_type
== VAR_STRING
17897 || tv
->v_type
== VAR_NUMBER
)))
17899 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
17904 * Handle setting internal v: variables separately: we don't change
17907 if (ht
== &vimvarht
)
17909 if (v
->di_tv
.v_type
== VAR_STRING
)
17911 vim_free(v
->di_tv
.vval
.v_string
);
17912 if (copy
|| tv
->v_type
!= VAR_STRING
)
17913 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
17916 /* Take over the string to avoid an extra alloc/free. */
17917 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
17918 tv
->vval
.v_string
= NULL
;
17921 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
17922 EMSG2(_(e_intern2
), "set_var()");
17924 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
17928 clear_tv(&v
->di_tv
);
17930 else /* add a new variable */
17932 /* Can't add "v:" variable. */
17933 if (ht
== &vimvarht
)
17935 EMSG2(_(e_illvar
), name
);
17939 /* Make sure the variable name is valid. */
17940 for (p
= varname
; *p
!= NUL
; ++p
)
17941 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
17942 && *p
!= AUTOLOAD_CHAR
)
17944 EMSG2(_(e_illvar
), varname
);
17948 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
17949 + STRLEN(varname
)));
17952 STRCPY(v
->di_key
, varname
);
17953 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
17961 if (copy
|| tv
->v_type
== VAR_NUMBER
)
17962 copy_tv(tv
, &v
->di_tv
);
17966 v
->di_tv
.v_lock
= 0;
17972 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
17973 * Also give an error message.
17976 var_check_ro(flags
, name
)
17980 if (flags
& DI_FLAGS_RO
)
17982 EMSG2(_(e_readonlyvar
), name
);
17985 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
17987 EMSG2(_(e_readonlysbx
), name
);
17994 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17995 * Also give an error message.
17998 var_check_fixed(flags
, name
)
18002 if (flags
& DI_FLAGS_FIX
)
18004 EMSG2(_("E795: Cannot delete variable %s"), name
);
18011 * Return TRUE if typeval "tv" is set to be locked (immutable).
18012 * Also give an error message, using "name".
18015 tv_check_lock(lock
, name
)
18019 if (lock
& VAR_LOCKED
)
18021 EMSG2(_("E741: Value is locked: %s"),
18022 name
== NULL
? (char_u
*)_("Unknown") : name
);
18025 if (lock
& VAR_FIXED
)
18027 EMSG2(_("E742: Cannot change value of %s"),
18028 name
== NULL
? (char_u
*)_("Unknown") : name
);
18035 * Copy the values from typval_T "from" to typval_T "to".
18036 * When needed allocates string or increases reference count.
18037 * Does not make a copy of a list or dict but copies the reference!
18044 to
->v_type
= from
->v_type
;
18046 switch (from
->v_type
)
18049 to
->vval
.v_number
= from
->vval
.v_number
;
18053 if (from
->vval
.v_string
== NULL
)
18054 to
->vval
.v_string
= NULL
;
18057 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
18058 if (from
->v_type
== VAR_FUNC
)
18059 func_ref(to
->vval
.v_string
);
18063 if (from
->vval
.v_list
== NULL
)
18064 to
->vval
.v_list
= NULL
;
18067 to
->vval
.v_list
= from
->vval
.v_list
;
18068 ++to
->vval
.v_list
->lv_refcount
;
18072 if (from
->vval
.v_dict
== NULL
)
18073 to
->vval
.v_dict
= NULL
;
18076 to
->vval
.v_dict
= from
->vval
.v_dict
;
18077 ++to
->vval
.v_dict
->dv_refcount
;
18081 EMSG2(_(e_intern2
), "copy_tv()");
18087 * Make a copy of an item.
18088 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
18089 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18090 * reference to an already copied list/dict can be used.
18091 * Returns FAIL or OK.
18094 item_copy(from
, to
, deep
, copyID
)
18100 static int recurse
= 0;
18103 if (recurse
>= DICT_MAXNEST
)
18105 EMSG(_("E698: variable nested too deep for making a copy"));
18110 switch (from
->v_type
)
18118 to
->v_type
= VAR_LIST
;
18120 if (from
->vval
.v_list
== NULL
)
18121 to
->vval
.v_list
= NULL
;
18122 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
18124 /* use the copy made earlier */
18125 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
18126 ++to
->vval
.v_list
->lv_refcount
;
18129 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
18130 if (to
->vval
.v_list
== NULL
)
18134 to
->v_type
= VAR_DICT
;
18136 if (from
->vval
.v_dict
== NULL
)
18137 to
->vval
.v_dict
= NULL
;
18138 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
18140 /* use the copy made earlier */
18141 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
18142 ++to
->vval
.v_dict
->dv_refcount
;
18145 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
18146 if (to
->vval
.v_dict
== NULL
)
18150 EMSG2(_(e_intern2
), "item_copy()");
18158 * ":echo expr1 ..." print each argument separated with a space, add a
18159 * newline at the end.
18160 * ":echon expr1 ..." print each argument plain.
18166 char_u
*arg
= eap
->arg
;
18170 int needclr
= TRUE
;
18171 int atstart
= TRUE
;
18172 char_u numbuf
[NUMBUFLEN
];
18176 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
18179 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
18182 * Report the invalid expression unless the expression evaluation
18183 * has been cancelled due to an aborting error, an interrupt, or an
18187 EMSG2(_(e_invexpr2
), p
);
18195 /* Call msg_start() after eval1(), evaluating the expression
18196 * may cause a message to appear. */
18197 if (eap
->cmdidx
== CMD_echo
)
18200 else if (eap
->cmdidx
== CMD_echo
)
18201 msg_puts_attr((char_u
*)" ", echo_attr
);
18202 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
18204 for ( ; *p
!= NUL
&& !got_int
; ++p
)
18206 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
18208 if (*p
!= TAB
&& needclr
)
18210 /* remove any text still there from the command */
18214 msg_putchar_attr(*p
, echo_attr
);
18221 int i
= (*mb_ptr2len
)(p
);
18223 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
18228 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
18234 arg
= skipwhite(arg
);
18236 eap
->nextcmd
= check_nextcmd(arg
);
18242 /* remove text that may still be there from the command */
18245 if (eap
->cmdidx
== CMD_echo
)
18251 * ":echohl {name}".
18259 id
= syn_name2id(eap
->arg
);
18263 echo_attr
= syn_id2attr(id
);
18267 * ":execute expr1 ..." execute the result of an expression.
18268 * ":echomsg expr1 ..." Print a message
18269 * ":echoerr expr1 ..." Print an error
18270 * Each gets spaces around each argument and a newline at the end for
18277 char_u
*arg
= eap
->arg
;
18285 ga_init2(&ga
, 1, 80);
18289 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
18292 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
18295 * Report the invalid expression unless the expression evaluation
18296 * has been cancelled due to an aborting error, an interrupt, or an
18300 EMSG2(_(e_invexpr2
), p
);
18307 p
= get_tv_string(&rettv
);
18308 len
= (int)STRLEN(p
);
18309 if (ga_grow(&ga
, len
+ 2) == FAIL
)
18316 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
18317 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
18322 arg
= skipwhite(arg
);
18325 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
18327 if (eap
->cmdidx
== CMD_echomsg
)
18329 MSG_ATTR(ga
.ga_data
, echo_attr
);
18332 else if (eap
->cmdidx
== CMD_echoerr
)
18334 /* We don't want to abort following commands, restore did_emsg. */
18335 save_did_emsg
= did_emsg
;
18336 EMSG((char_u
*)ga
.ga_data
);
18338 did_emsg
= save_did_emsg
;
18340 else if (eap
->cmdidx
== CMD_execute
)
18341 do_cmdline((char_u
*)ga
.ga_data
,
18342 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
18350 eap
->nextcmd
= check_nextcmd(arg
);
18354 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18355 * "arg" points to the "&" or '+' when called, to "option" when returning.
18356 * Returns NULL when no option name found. Otherwise pointer to the char
18357 * after the option name.
18360 find_option_end(arg
, opt_flags
)
18367 if (*p
== 'g' && p
[1] == ':')
18369 *opt_flags
= OPT_GLOBAL
;
18372 else if (*p
== 'l' && p
[1] == ':')
18374 *opt_flags
= OPT_LOCAL
;
18380 if (!ASCII_ISALPHA(*p
))
18384 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
18385 p
+= 4; /* termcap option */
18387 while (ASCII_ISALPHA(*p
))
18402 int saved_did_emsg
;
18403 char_u
*name
= NULL
;
18406 char_u
*line_arg
= NULL
;
18409 int varargs
= FALSE
;
18410 int mustend
= FALSE
;
18415 char_u
*skip_until
= NULL
;
18418 static int func_nr
= 0; /* number for nameless function */
18423 int sourcing_lnum_off
;
18426 * ":function" without argument: list functions.
18428 if (ends_excmd(*eap
->arg
))
18432 todo
= (int)func_hashtab
.ht_used
;
18433 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
18435 if (!HASHITEM_EMPTY(hi
))
18439 if (!isdigit(*fp
->uf_name
))
18440 list_func_head(fp
, FALSE
);
18444 eap
->nextcmd
= check_nextcmd(eap
->arg
);
18449 * ":function /pat": list functions matching pattern.
18451 if (*eap
->arg
== '/')
18453 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
18456 regmatch_T regmatch
;
18460 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
18462 if (regmatch
.regprog
!= NULL
)
18464 regmatch
.rm_ic
= p_ic
;
18466 todo
= (int)func_hashtab
.ht_used
;
18467 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
18469 if (!HASHITEM_EMPTY(hi
))
18473 if (!isdigit(*fp
->uf_name
)
18474 && vim_regexec(®match
, fp
->uf_name
, 0))
18475 list_func_head(fp
, FALSE
);
18482 eap
->nextcmd
= check_nextcmd(p
);
18487 * Get the function name. There are these situations:
18488 * func normal function name
18489 * "name" == func, "fudi.fd_dict" == NULL
18490 * dict.func new dictionary entry
18491 * "name" == NULL, "fudi.fd_dict" set,
18492 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18493 * dict.func existing dict entry with a Funcref
18494 * "name" == func, "fudi.fd_dict" set,
18495 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18496 * dict.func existing dict entry that's not a Funcref
18497 * "name" == NULL, "fudi.fd_dict" set,
18498 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18501 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
18502 paren
= (vim_strchr(p
, '(') != NULL
);
18503 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
18506 * Return on an invalid expression in braces, unless the expression
18507 * evaluation has been cancelled due to an aborting error, an
18508 * interrupt, or an exception.
18512 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
18513 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
18514 vim_free(fudi
.fd_newkey
);
18521 /* An error in a function call during evaluation of an expression in magic
18522 * braces should not cause the function not to be defined. */
18523 saved_did_emsg
= did_emsg
;
18527 * ":function func" with only function name: list function.
18531 if (!ends_excmd(*skipwhite(p
)))
18533 EMSG(_(e_trailing
));
18536 eap
->nextcmd
= check_nextcmd(p
);
18537 if (eap
->nextcmd
!= NULL
)
18539 if (!eap
->skip
&& !got_int
)
18541 fp
= find_func(name
);
18544 list_func_head(fp
, TRUE
);
18545 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
18547 if (FUNCLINE(fp
, j
) == NULL
)
18550 msg_outnum((long)(j
+ 1));
18555 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
18556 out_flush(); /* show a line at a time */
18562 msg_puts((char_u
*)" endfunction");
18566 emsg_funcname("E123: Undefined function: %s", name
);
18572 * ":function name(arg1, arg2)" Define function.
18579 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
18582 /* attempt to continue by skipping some text */
18583 if (vim_strchr(p
, '(') != NULL
)
18584 p
= vim_strchr(p
, '(');
18586 p
= skipwhite(p
+ 1);
18588 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
18589 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
18593 /* Check the name of the function. Unless it's a dictionary function
18594 * (that we are overwriting). */
18598 arg
= fudi
.fd_newkey
;
18599 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
18600 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
18602 if (*arg
== K_SPECIAL
)
18606 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
18607 : eval_isnamec(arg
[j
])))
18610 emsg_funcname(_(e_invarg2
), arg
);
18615 * Isolate the arguments: "arg1, arg2, ...)"
18619 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
18628 while (ASCII_ISALNUM(*p
) || *p
== '_')
18630 if (arg
== p
|| isdigit(*arg
)
18631 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
18632 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
18635 EMSG2(_("E125: Illegal argument: %s"), arg
);
18638 if (ga_grow(&newargs
, 1) == FAIL
)
18642 arg
= vim_strsave(arg
);
18645 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
18654 if (mustend
&& *p
!= ')')
18657 EMSG2(_(e_invarg2
), eap
->arg
);
18661 ++p
; /* skip the ')' */
18663 /* find extra arguments "range", "dict" and "abort" */
18667 if (STRNCMP(p
, "range", 5) == 0)
18672 else if (STRNCMP(p
, "dict", 4) == 0)
18677 else if (STRNCMP(p
, "abort", 5) == 0)
18686 /* When there is a line break use what follows for the function body.
18687 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18690 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
18691 EMSG(_(e_trailing
));
18694 * Read the body of the function, until ":endfunction" is found.
18698 /* Check if the function already exists, don't let the user type the
18699 * whole function before telling him it doesn't work! For a script we
18700 * need to skip the body to be able to find what follows. */
18701 if (!eap
->skip
&& !eap
->forceit
)
18703 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
18704 EMSG(_(e_funcdict
));
18705 else if (name
!= NULL
&& find_func(name
) != NULL
)
18706 emsg_funcname(e_funcexts
, name
);
18709 if (!eap
->skip
&& did_emsg
)
18712 msg_putchar('\n'); /* don't overwrite the function name */
18713 cmdline_row
= msg_row
;
18721 need_wait_return
= FALSE
;
18722 sourcing_lnum_off
= sourcing_lnum
;
18724 if (line_arg
!= NULL
)
18726 /* Use eap->arg, split up in parts by line breaks. */
18727 theline
= line_arg
;
18728 p
= vim_strchr(theline
, '\n');
18730 line_arg
+= STRLEN(line_arg
);
18737 else if (eap
->getline
== NULL
)
18738 theline
= getcmdline(':', 0L, indent
);
18740 theline
= eap
->getline(':', eap
->cookie
, indent
);
18742 lines_left
= Rows
- 1;
18743 if (theline
== NULL
)
18745 EMSG(_("E126: Missing :endfunction"));
18749 /* Detect line continuation: sourcing_lnum increased more than one. */
18750 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
18751 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
18753 sourcing_lnum_off
= 0;
18755 if (skip_until
!= NULL
)
18757 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18758 * don't check for ":endfunc". */
18759 if (STRCMP(theline
, skip_until
) == 0)
18761 vim_free(skip_until
);
18767 /* skip ':' and blanks*/
18768 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
18771 /* Check for "endfunction". */
18772 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
18774 if (line_arg
== NULL
)
18779 /* Increase indent inside "if", "while", "for" and "try", decrease
18781 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
18783 else if (STRNCMP(p
, "if", 2) == 0
18784 || STRNCMP(p
, "wh", 2) == 0
18785 || STRNCMP(p
, "for", 3) == 0
18786 || STRNCMP(p
, "try", 3) == 0)
18789 /* Check for defining a function inside this function. */
18790 if (checkforcmd(&p
, "function", 2))
18793 p
= skipwhite(p
+ 1);
18794 p
+= eval_fname_script(p
);
18795 if (ASCII_ISALPHA(*p
))
18797 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
18798 if (*skipwhite(p
) == '(')
18806 /* Check for ":append" or ":insert". */
18807 p
= skip_range(p
, NULL
);
18808 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
18810 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
18811 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
18812 skip_until
= vim_strsave((char_u
*)".");
18814 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18815 arg
= skipwhite(skiptowhite(p
));
18816 if (arg
[0] == '<' && arg
[1] =='<'
18817 && ((p
[0] == 'p' && p
[1] == 'y'
18818 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
18819 || (p
[0] == 'p' && p
[1] == 'e'
18820 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
18821 || (p
[0] == 't' && p
[1] == 'c'
18822 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
18823 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
18824 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
18825 || (p
[0] == 'm' && p
[1] == 'z'
18826 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
18829 /* ":python <<" continues until a dot, like ":append" */
18830 p
= skipwhite(arg
+ 2);
18832 skip_until
= vim_strsave((char_u
*)".");
18834 skip_until
= vim_strsave(p
);
18838 /* Add the line to the function. */
18839 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
18841 if (line_arg
== NULL
)
18846 /* Copy the line to newly allocated memory. get_one_sourceline()
18847 * allocates 250 bytes per line, this saves 80% on average. The cost
18848 * is an extra alloc/free. */
18849 p
= vim_strsave(theline
);
18852 if (line_arg
== NULL
)
18857 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
18859 /* Add NULL lines for continuation lines, so that the line count is
18860 * equal to the index in the growarray. */
18861 while (sourcing_lnum_off
-- > 0)
18862 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
18864 /* Check for end of eap->arg. */
18865 if (line_arg
!= NULL
&& *line_arg
== NUL
)
18869 /* Don't define the function when skipping commands or when an error was
18871 if (eap
->skip
|| did_emsg
)
18875 * If there are no errors, add the function
18877 if (fudi
.fd_dict
== NULL
)
18879 v
= find_var(name
, &ht
);
18880 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
18882 emsg_funcname("E707: Function name conflicts with variable: %s",
18887 fp
= find_func(name
);
18892 emsg_funcname(e_funcexts
, name
);
18895 if (fp
->uf_calls
> 0)
18897 emsg_funcname("E127: Cannot redefine function %s: It is in use",
18901 /* redefine existing function */
18902 ga_clear_strings(&(fp
->uf_args
));
18903 ga_clear_strings(&(fp
->uf_lines
));
18913 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
18915 EMSG(_(e_funcdict
));
18918 if (fudi
.fd_di
== NULL
)
18920 /* Can't add a function to a locked dictionary */
18921 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
18924 /* Can't change an existing function if it is locked */
18925 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
18928 /* Give the function a sequential number. Can only be used with a
18931 sprintf(numbuf
, "%d", ++func_nr
);
18932 name
= vim_strsave((char_u
*)numbuf
);
18939 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
18942 char_u
*scriptname
;
18944 /* Check that the autoload name matches the script name. */
18946 if (sourcing_name
!= NULL
)
18948 scriptname
= autoload_name(name
);
18949 if (scriptname
!= NULL
)
18951 p
= vim_strchr(scriptname
, '/');
18952 plen
= (int)STRLEN(p
);
18953 slen
= (int)STRLEN(sourcing_name
);
18954 if (slen
> plen
&& fnamecmp(p
,
18955 sourcing_name
+ slen
- plen
) == 0)
18957 vim_free(scriptname
);
18962 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
18967 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
18971 if (fudi
.fd_dict
!= NULL
)
18973 if (fudi
.fd_di
== NULL
)
18975 /* add new dict entry */
18976 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
18977 if (fudi
.fd_di
== NULL
)
18982 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
18984 vim_free(fudi
.fd_di
);
18990 /* overwrite existing dict entry */
18991 clear_tv(&fudi
.fd_di
->di_tv
);
18992 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
18993 fudi
.fd_di
->di_tv
.v_lock
= 0;
18994 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
18995 fp
->uf_refcount
= 1;
18997 /* behave like "dict" was used */
19001 /* insert the new function in the function list */
19002 STRCPY(fp
->uf_name
, name
);
19003 hash_add(&func_hashtab
, UF2HIKEY(fp
));
19005 fp
->uf_args
= newargs
;
19006 fp
->uf_lines
= newlines
;
19007 #ifdef FEAT_PROFILE
19008 fp
->uf_tml_count
= NULL
;
19009 fp
->uf_tml_total
= NULL
;
19010 fp
->uf_tml_self
= NULL
;
19011 fp
->uf_profiling
= FALSE
;
19012 if (prof_def_func())
19013 func_do_profile(fp
);
19015 fp
->uf_varargs
= varargs
;
19016 fp
->uf_flags
= flags
;
19018 fp
->uf_script_ID
= current_SID
;
19022 ga_clear_strings(&newargs
);
19023 ga_clear_strings(&newlines
);
19025 vim_free(skip_until
);
19026 vim_free(fudi
.fd_newkey
);
19028 did_emsg
|= saved_did_emsg
;
19032 * Get a function name, translating "<SID>" and "<SNR>".
19033 * Also handles a Funcref in a List or Dictionary.
19034 * Returns the function name in allocated memory, or NULL for failure.
19036 * TFN_INT: internal function name OK
19037 * TFN_QUIET: be quiet
19038 * Advances "pp" to just after the function name (if no error).
19041 trans_function_name(pp
, skip
, flags
, fdp
)
19043 int skip
; /* only find the end, don't evaluate */
19045 funcdict_T
*fdp
; /* return: info about dictionary used */
19047 char_u
*name
= NULL
;
19051 char_u sid_buf
[20];
19056 vim_memset(fdp
, 0, sizeof(funcdict_T
));
19059 /* Check for hard coded <SNR>: already translated function ID (from a user
19061 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
19062 && (*pp
)[2] == (int)KE_SNR
)
19065 len
= get_id_len(pp
) + 3;
19066 return vim_strnsave(start
, len
);
19069 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19070 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
19071 lead
= eval_fname_script(start
);
19075 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
19076 lead
> 2 ? 0 : FNE_CHECK_START
);
19080 EMSG(_("E129: Function name required"));
19083 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
19086 * Report an invalid expression in braces, unless the expression
19087 * evaluation has been cancelled due to an aborting error, an
19088 * interrupt, or an exception.
19093 EMSG2(_(e_invarg2
), start
);
19096 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
19100 if (lv
.ll_tv
!= NULL
)
19104 fdp
->fd_dict
= lv
.ll_dict
;
19105 fdp
->fd_newkey
= lv
.ll_newkey
;
19106 lv
.ll_newkey
= NULL
;
19107 fdp
->fd_di
= lv
.ll_di
;
19109 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
19111 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
19116 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
19117 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
19118 EMSG(_(e_funcref
));
19126 if (lv
.ll_name
== NULL
)
19128 /* Error found, but continue after the function name. */
19133 if (lv
.ll_exp_name
!= NULL
)
19135 len
= (int)STRLEN(lv
.ll_exp_name
);
19136 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
19137 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
19139 /* When there was "s:" already or the name expanded to get a
19140 * leading "s:" then remove it. */
19148 if (lead
== 2) /* skip over "s:" */
19150 len
= (int)(end
- lv
.ll_name
);
19154 * Copy the function name to allocated memory.
19155 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19156 * Accept <SNR>123_name() outside a script.
19159 lead
= 0; /* do nothing */
19163 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
19164 || eval_fname_sid(*pp
))
19166 /* It's "s:" or "<SID>" */
19167 if (current_SID
<= 0)
19169 EMSG(_(e_usingsid
));
19172 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
19173 lead
+= (int)STRLEN(sid_buf
);
19176 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
19178 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
19181 name
= alloc((unsigned)(len
+ lead
+ 1));
19186 name
[0] = K_SPECIAL
;
19187 name
[1] = KS_EXTRA
;
19188 name
[2] = (int)KE_SNR
;
19189 if (lead
> 3) /* If it's "<SID>" */
19190 STRCPY(name
+ 3, sid_buf
);
19192 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
19193 name
[len
+ lead
] = NUL
;
19203 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19204 * Return 2 if "p" starts with "s:".
19205 * Return 0 otherwise.
19208 eval_fname_script(p
)
19211 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
19212 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
19214 if (p
[0] == 's' && p
[1] == ':')
19220 * Return TRUE if "p" starts with "<SID>" or "s:".
19221 * Only works if eval_fname_script() returned non-zero for "p"!
19227 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
19231 * List the head of the function: "name(arg1, arg2)".
19234 list_func_head(fp
, indent
)
19243 MSG_PUTS("function ");
19244 if (fp
->uf_name
[0] == K_SPECIAL
)
19246 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
19247 msg_puts(fp
->uf_name
+ 3);
19250 msg_puts(fp
->uf_name
);
19252 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
19256 msg_puts(FUNCARG(fp
, j
));
19258 if (fp
->uf_varargs
)
19267 last_set_msg(fp
->uf_script_ID
);
19271 * Find a function by name, return pointer to it in ufuncs.
19272 * Return NULL for unknown function.
19280 hi
= hash_find(&func_hashtab
, name
);
19281 if (!HASHITEM_EMPTY(hi
))
19286 #if defined(EXITFREE) || defined(PROTO)
19288 free_all_functions()
19292 /* Need to start all over every time, because func_free() may change the
19294 while (func_hashtab
.ht_used
> 0)
19295 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
19296 if (!HASHITEM_EMPTY(hi
))
19298 func_free(HI2UF(hi
));
19305 * Return TRUE if a function "name" exists.
19308 function_exists(name
)
19315 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
19316 nm
= skipwhite(nm
);
19318 /* Only accept "funcname", "funcname ", "funcname (..." and
19319 * "funcname(...", not "funcname!...". */
19320 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
19322 if (builtin_function(p
))
19323 n
= (find_internal_func(p
) >= 0);
19325 n
= (find_func(p
) != NULL
);
19332 * Return TRUE if "name" looks like a builtin function name: starts with a
19333 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
19336 builtin_function(name
)
19339 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
19340 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
19343 #if defined(FEAT_PROFILE) || defined(PROTO)
19345 * Start profiling function "fp".
19348 func_do_profile(fp
)
19351 fp
->uf_tm_count
= 0;
19352 profile_zero(&fp
->uf_tm_self
);
19353 profile_zero(&fp
->uf_tm_total
);
19354 if (fp
->uf_tml_count
== NULL
)
19355 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
19356 (sizeof(int) * fp
->uf_lines
.ga_len
));
19357 if (fp
->uf_tml_total
== NULL
)
19358 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
19359 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
19360 if (fp
->uf_tml_self
== NULL
)
19361 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
19362 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
19363 fp
->uf_tml_idx
= -1;
19364 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
19365 || fp
->uf_tml_self
== NULL
)
19366 return; /* out of memory */
19368 fp
->uf_profiling
= TRUE
;
19372 * Dump the profiling results for all functions in file "fd".
19375 func_dump_profile(fd
)
19385 todo
= (int)func_hashtab
.ht_used
;
19386 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
19388 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
19390 if (!HASHITEM_EMPTY(hi
))
19394 if (fp
->uf_profiling
)
19396 if (sorttab
!= NULL
)
19397 sorttab
[st_len
++] = fp
;
19399 if (fp
->uf_name
[0] == K_SPECIAL
)
19400 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
19402 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
19403 if (fp
->uf_tm_count
== 1)
19404 fprintf(fd
, "Called 1 time\n");
19406 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
19407 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
19408 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
19410 fprintf(fd
, "count total (s) self (s)\n");
19412 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
19414 if (FUNCLINE(fp
, i
) == NULL
)
19416 prof_func_line(fd
, fp
->uf_tml_count
[i
],
19417 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
19418 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
19425 if (sorttab
!= NULL
&& st_len
> 0)
19427 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
19429 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
19430 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
19432 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
19437 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
19442 int prefer_self
; /* when equal print only self time */
19447 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
19448 fprintf(fd
, "count total (s) self (s) function\n");
19449 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
19452 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
19454 if (fp
->uf_name
[0] == K_SPECIAL
)
19455 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
19457 fprintf(fd
, " %s()\n", fp
->uf_name
);
19463 * Print the count and times for one function or function line.
19466 prof_func_line(fd
, count
, total
, self
, prefer_self
)
19471 int prefer_self
; /* when equal print only self time */
19475 fprintf(fd
, "%5d ", count
);
19476 if (prefer_self
&& profile_equal(total
, self
))
19479 fprintf(fd
, "%s ", profile_msg(total
));
19480 if (!prefer_self
&& profile_equal(total
, self
))
19483 fprintf(fd
, "%s ", profile_msg(self
));
19490 * Compare function for total time sorting.
19493 #ifdef __BORLANDC__
19496 prof_total_cmp(s1
, s2
)
19502 p1
= *(ufunc_T
**)s1
;
19503 p2
= *(ufunc_T
**)s2
;
19504 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
19508 * Compare function for self time sorting.
19511 #ifdef __BORLANDC__
19514 prof_self_cmp(s1
, s2
)
19520 p1
= *(ufunc_T
**)s1
;
19521 p2
= *(ufunc_T
**)s2
;
19522 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
19528 * If "name" has a package name try autoloading the script for it.
19529 * Return TRUE if a package was loaded.
19532 script_autoload(name
, reload
)
19534 int reload
; /* load script again when already loaded */
19537 char_u
*scriptname
, *tofree
;
19541 /* If there is no '#' after name[0] there is no package name. */
19542 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
19543 if (p
== NULL
|| p
== name
)
19546 tofree
= scriptname
= autoload_name(name
);
19548 /* Find the name in the list of previously loaded package names. Skip
19549 * "autoload/", it's always the same. */
19550 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
19551 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
19553 if (!reload
&& i
< ga_loaded
.ga_len
)
19554 ret
= FALSE
; /* was loaded already */
19557 /* Remember the name if it wasn't loaded already. */
19558 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
19560 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
19564 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
19565 if (source_runtime(scriptname
, FALSE
) == OK
)
19574 * Return the autoload script name for a function or variable name.
19575 * Returns NULL when out of memory.
19578 autoload_name(name
)
19582 char_u
*scriptname
;
19584 /* Get the script file name: replace '#' with '/', append ".vim". */
19585 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
19586 if (scriptname
== NULL
)
19588 STRCPY(scriptname
, "autoload/");
19589 STRCAT(scriptname
, name
);
19590 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
19591 STRCAT(scriptname
, ".vim");
19592 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
19597 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19600 * Function given to ExpandGeneric() to obtain the list of user defined
19604 get_user_func_name(xp
, idx
)
19608 static long_u done
;
19609 static hashitem_T
*hi
;
19615 hi
= func_hashtab
.ht_array
;
19617 if (done
< func_hashtab
.ht_used
)
19621 while (HASHITEM_EMPTY(hi
))
19625 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
19626 return fp
->uf_name
; /* prevents overflow */
19628 cat_func_name(IObuff
, fp
);
19629 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
19631 STRCAT(IObuff
, "(");
19632 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
19633 STRCAT(IObuff
, ")");
19640 #endif /* FEAT_CMDL_COMPL */
19643 * Copy the function name of "fp" to buffer "buf".
19644 * "buf" must be able to hold the function name plus three bytes.
19645 * Takes care of script-local function names.
19648 cat_func_name(buf
, fp
)
19652 if (fp
->uf_name
[0] == K_SPECIAL
)
19654 STRCPY(buf
, "<SNR>");
19655 STRCAT(buf
, fp
->uf_name
+ 3);
19658 STRCPY(buf
, fp
->uf_name
);
19662 * ":delfunction {name}"
19665 ex_delfunction(eap
)
19668 ufunc_T
*fp
= NULL
;
19674 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19675 vim_free(fudi
.fd_newkey
);
19678 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
19679 EMSG(_(e_funcref
));
19682 if (!ends_excmd(*skipwhite(p
)))
19685 EMSG(_(e_trailing
));
19688 eap
->nextcmd
= check_nextcmd(p
);
19689 if (eap
->nextcmd
!= NULL
)
19693 fp
= find_func(name
);
19700 EMSG2(_(e_nofunc
), eap
->arg
);
19703 if (fp
->uf_calls
> 0)
19705 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
19709 if (fudi
.fd_dict
!= NULL
)
19711 /* Delete the dict item that refers to the function, it will
19712 * invoke func_unref() and possibly delete the function. */
19713 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
19721 * Free a function and remove it from the list of functions.
19729 /* clear this function */
19730 ga_clear_strings(&(fp
->uf_args
));
19731 ga_clear_strings(&(fp
->uf_lines
));
19732 #ifdef FEAT_PROFILE
19733 vim_free(fp
->uf_tml_count
);
19734 vim_free(fp
->uf_tml_total
);
19735 vim_free(fp
->uf_tml_self
);
19738 /* remove the function from the function hashtable */
19739 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
19740 if (HASHITEM_EMPTY(hi
))
19741 EMSG2(_(e_intern2
), "func_free()");
19743 hash_remove(&func_hashtab
, hi
);
19749 * Unreference a Function: decrement the reference count and free it when it
19750 * becomes zero. Only for numbered functions.
19758 if (name
!= NULL
&& isdigit(*name
))
19760 fp
= find_func(name
);
19762 EMSG2(_(e_intern2
), "func_unref()");
19763 else if (--fp
->uf_refcount
<= 0)
19765 /* Only delete it when it's not being used. Otherwise it's done
19766 * when "uf_calls" becomes zero. */
19767 if (fp
->uf_calls
== 0)
19774 * Count a reference to a Function.
19782 if (name
!= NULL
&& isdigit(*name
))
19784 fp
= find_func(name
);
19786 EMSG2(_(e_intern2
), "func_ref()");
19793 * Call a user function.
19796 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
19797 ufunc_T
*fp
; /* pointer to function */
19798 int argcount
; /* nr of args */
19799 typval_T
*argvars
; /* arguments */
19800 typval_T
*rettv
; /* return value */
19801 linenr_T firstline
; /* first line of range */
19802 linenr_T lastline
; /* last line of range */
19803 dict_T
*selfdict
; /* Dictionary for "self" */
19805 char_u
*save_sourcing_name
;
19806 linenr_T save_sourcing_lnum
;
19807 scid_T save_current_SID
;
19810 static int depth
= 0;
19812 int fixvar_idx
= 0; /* index in fixvar[] */
19815 char_u numbuf
[NUMBUFLEN
];
19817 #ifdef FEAT_PROFILE
19818 proftime_T wait_start
;
19819 proftime_T call_start
;
19822 /* If depth of calling is getting too high, don't execute the function */
19823 if (depth
>= p_mfd
)
19825 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
19826 rettv
->v_type
= VAR_NUMBER
;
19827 rettv
->vval
.v_number
= -1;
19832 line_breakcheck(); /* check for CTRL-C hit */
19834 fc
.caller
= current_funccal
;
19835 current_funccal
= &fc
;
19838 rettv
->vval
.v_number
= 0;
19840 fc
.returned
= FALSE
;
19841 fc
.level
= ex_nesting_level
;
19842 /* Check if this function has a breakpoint. */
19843 fc
.breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
19844 fc
.dbg_tick
= debug_tick
;
19847 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19848 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19849 * each argument variable and saves a lot of time.
19852 * Init l: variables.
19854 init_var_dict(&fc
.l_vars
, &fc
.l_vars_var
);
19855 if (selfdict
!= NULL
)
19857 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19858 * some compiler that checks the destination size. */
19859 v
= &fc
.fixvar
[fixvar_idx
++].var
;
19861 STRCPY(name
, "self");
19862 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
19863 hash_add(&fc
.l_vars
.dv_hashtab
, DI2HIKEY(v
));
19864 v
->di_tv
.v_type
= VAR_DICT
;
19865 v
->di_tv
.v_lock
= 0;
19866 v
->di_tv
.vval
.v_dict
= selfdict
;
19867 ++selfdict
->dv_refcount
;
19871 * Init a: variables.
19872 * Set a:0 to "argcount".
19873 * Set a:000 to a list with room for the "..." arguments.
19875 init_var_dict(&fc
.l_avars
, &fc
.l_avars_var
);
19876 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "0",
19877 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
19878 v
= &fc
.fixvar
[fixvar_idx
++].var
;
19879 STRCPY(v
->di_key
, "000");
19880 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
19881 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
19882 v
->di_tv
.v_type
= VAR_LIST
;
19883 v
->di_tv
.v_lock
= VAR_FIXED
;
19884 v
->di_tv
.vval
.v_list
= &fc
.l_varlist
;
19885 vim_memset(&fc
.l_varlist
, 0, sizeof(list_T
));
19886 fc
.l_varlist
.lv_refcount
= 99999;
19887 fc
.l_varlist
.lv_lock
= VAR_FIXED
;
19890 * Set a:firstline to "firstline" and a:lastline to "lastline".
19891 * Set a:name to named arguments.
19892 * Set a:N to the "..." arguments.
19894 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "firstline",
19895 (varnumber_T
)firstline
);
19896 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "lastline",
19897 (varnumber_T
)lastline
);
19898 for (i
= 0; i
< argcount
; ++i
)
19900 ai
= i
- fp
->uf_args
.ga_len
;
19902 /* named argument a:name */
19903 name
= FUNCARG(fp
, i
);
19906 /* "..." argument a:1, a:2, etc. */
19907 sprintf((char *)numbuf
, "%d", ai
+ 1);
19910 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
19912 v
= &fc
.fixvar
[fixvar_idx
++].var
;
19913 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
19917 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19921 v
->di_flags
= DI_FLAGS_RO
;
19923 STRCPY(v
->di_key
, name
);
19924 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
19926 /* Note: the values are copied directly to avoid alloc/free.
19927 * "argvars" must have VAR_FIXED for v_lock. */
19928 v
->di_tv
= argvars
[i
];
19929 v
->di_tv
.v_lock
= VAR_FIXED
;
19931 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
19933 list_append(&fc
.l_varlist
, &fc
.l_listitems
[ai
]);
19934 fc
.l_listitems
[ai
].li_tv
= argvars
[i
];
19935 fc
.l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
19939 /* Don't redraw while executing the function. */
19940 ++RedrawingDisabled
;
19941 save_sourcing_name
= sourcing_name
;
19942 save_sourcing_lnum
= sourcing_lnum
;
19944 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
19945 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
19946 if (sourcing_name
!= NULL
)
19948 if (save_sourcing_name
!= NULL
19949 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
19950 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
19952 STRCPY(sourcing_name
, "function ");
19953 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
19955 if (p_verbose
>= 12)
19958 verbose_enter_scroll();
19960 smsg((char_u
*)_("calling %s"), sourcing_name
);
19961 if (p_verbose
>= 14)
19963 char_u buf
[MSG_BUF_LEN
];
19964 char_u numbuf2
[NUMBUFLEN
];
19967 msg_puts((char_u
*)"(");
19968 for (i
= 0; i
< argcount
; ++i
)
19971 msg_puts((char_u
*)", ");
19972 if (argvars
[i
].v_type
== VAR_NUMBER
)
19973 msg_outnum((long)argvars
[i
].vval
.v_number
);
19976 trunc_string(tv2string(&argvars
[i
], &tofree
,
19977 numbuf2
, 0), buf
, MSG_BUF_CLEN
);
19982 msg_puts((char_u
*)")");
19984 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
19986 verbose_leave_scroll();
19990 #ifdef FEAT_PROFILE
19991 if (do_profiling
== PROF_YES
)
19993 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
19994 func_do_profile(fp
);
19995 if (fp
->uf_profiling
19996 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
))
19999 profile_start(&call_start
);
20000 profile_zero(&fp
->uf_tm_children
);
20002 script_prof_save(&wait_start
);
20006 save_current_SID
= current_SID
;
20007 current_SID
= fp
->uf_script_ID
;
20008 save_did_emsg
= did_emsg
;
20011 /* call do_cmdline() to execute the lines */
20012 do_cmdline(NULL
, get_func_line
, (void *)&fc
,
20013 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
20015 --RedrawingDisabled
;
20017 /* when the function was aborted because of an error, return -1 */
20018 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
20021 rettv
->v_type
= VAR_NUMBER
;
20022 rettv
->vval
.v_number
= -1;
20025 #ifdef FEAT_PROFILE
20026 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
20027 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)))
20029 profile_end(&call_start
);
20030 profile_sub_wait(&wait_start
, &call_start
);
20031 profile_add(&fp
->uf_tm_total
, &call_start
);
20032 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
20033 if (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)
20035 profile_add(&fc
.caller
->func
->uf_tm_children
, &call_start
);
20036 profile_add(&fc
.caller
->func
->uf_tml_children
, &call_start
);
20041 /* when being verbose, mention the return value */
20042 if (p_verbose
>= 12)
20045 verbose_enter_scroll();
20048 smsg((char_u
*)_("%s aborted"), sourcing_name
);
20049 else if (fc
.rettv
->v_type
== VAR_NUMBER
)
20050 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
20051 (long)fc
.rettv
->vval
.v_number
);
20054 char_u buf
[MSG_BUF_LEN
];
20055 char_u numbuf2
[NUMBUFLEN
];
20058 /* The value may be very long. Skip the middle part, so that we
20059 * have some idea how it starts and ends. smsg() would always
20060 * truncate it at the end. */
20061 trunc_string(tv2string(fc
.rettv
, &tofree
, numbuf2
, 0),
20062 buf
, MSG_BUF_CLEN
);
20063 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
20066 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20068 verbose_leave_scroll();
20072 vim_free(sourcing_name
);
20073 sourcing_name
= save_sourcing_name
;
20074 sourcing_lnum
= save_sourcing_lnum
;
20075 current_SID
= save_current_SID
;
20076 #ifdef FEAT_PROFILE
20077 if (do_profiling
== PROF_YES
)
20078 script_prof_restore(&wait_start
);
20081 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
20084 verbose_enter_scroll();
20086 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
20087 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20089 verbose_leave_scroll();
20093 did_emsg
|= save_did_emsg
;
20094 current_funccal
= fc
.caller
;
20096 /* The a: variables typevals were not alloced, only free the allocated
20098 vars_clear_ext(&fc
.l_avars
.dv_hashtab
, FALSE
);
20100 vars_clear(&fc
.l_vars
.dv_hashtab
); /* free all l: variables */
20105 * Add a number variable "name" to dict "dp" with value "nr".
20108 add_nr_var(dp
, v
, name
, nr
)
20114 STRCPY(v
->di_key
, name
);
20115 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20116 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
20117 v
->di_tv
.v_type
= VAR_NUMBER
;
20118 v
->di_tv
.v_lock
= VAR_FIXED
;
20119 v
->di_tv
.vval
.v_number
= nr
;
20129 char_u
*arg
= eap
->arg
;
20131 int returning
= FALSE
;
20133 if (current_funccal
== NULL
)
20135 EMSG(_("E133: :return not inside a function"));
20142 eap
->nextcmd
= NULL
;
20143 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
20144 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
20147 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
20151 /* It's safer to return also on error. */
20152 else if (!eap
->skip
)
20155 * Return unless the expression evaluation has been cancelled due to an
20156 * aborting error, an interrupt, or an exception.
20159 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
20162 /* When skipping or the return gets pending, advance to the next command
20163 * in this line (!returning). Otherwise, ignore the rest of the line.
20164 * Following lines will be ignored by get_func_line(). */
20166 eap
->nextcmd
= NULL
;
20167 else if (eap
->nextcmd
== NULL
) /* no argument */
20168 eap
->nextcmd
= check_nextcmd(arg
);
20175 * Return from a function. Possibly makes the return pending. Also called
20176 * for a pending return at the ":endtry" or after returning from an extra
20177 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
20178 * when called due to a ":return" command. "rettv" may point to a typval_T
20179 * with the return rettv. Returns TRUE when the return can be carried out,
20180 * FALSE when the return gets pending.
20183 do_return(eap
, reanimate
, is_cmd
, rettv
)
20190 struct condstack
*cstack
= eap
->cstack
;
20193 /* Undo the return. */
20194 current_funccal
->returned
= FALSE
;
20197 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20198 * not in its finally clause (which then is to be executed next) is found.
20199 * In this case, make the ":return" pending for execution at the ":endtry".
20200 * Otherwise, return normally.
20202 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
20205 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
20207 if (!is_cmd
&& !reanimate
)
20208 /* A pending return again gets pending. "rettv" points to an
20209 * allocated variable with the rettv of the original ":return"'s
20210 * argument if present or is NULL else. */
20211 cstack
->cs_rettv
[idx
] = rettv
;
20214 /* When undoing a return in order to make it pending, get the stored
20217 rettv
= current_funccal
->rettv
;
20221 /* Store the value of the pending return. */
20222 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
20223 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
20225 EMSG(_(e_outofmem
));
20228 cstack
->cs_rettv
[idx
] = NULL
;
20232 /* The pending return value could be overwritten by a ":return"
20233 * without argument in a finally clause; reset the default
20235 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
20236 current_funccal
->rettv
->vval
.v_number
= 0;
20239 report_make_pending(CSTP_RETURN
, rettv
);
20243 current_funccal
->returned
= TRUE
;
20245 /* If the return is carried out now, store the return value. For
20246 * a return immediately after reanimation, the value is already
20248 if (!reanimate
&& rettv
!= NULL
)
20250 clear_tv(current_funccal
->rettv
);
20251 *current_funccal
->rettv
= *(typval_T
*)rettv
;
20261 * Free the variable with a pending return value.
20264 discard_pending_return(rettv
)
20267 free_tv((typval_T
*)rettv
);
20271 * Generate a return command for producing the value of "rettv". The result
20272 * is an allocated string. Used by report_pending() for verbose messages.
20275 get_return_cmd(rettv
)
20279 char_u
*tofree
= NULL
;
20280 char_u numbuf
[NUMBUFLEN
];
20283 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
20287 STRCPY(IObuff
, ":return ");
20288 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
20289 if (STRLEN(s
) + 8 >= IOSIZE
)
20290 STRCPY(IObuff
+ IOSIZE
- 4, "...");
20292 return vim_strsave(IObuff
);
20296 * Get next function line.
20297 * Called by do_cmdline() to get the next line.
20298 * Returns allocated string, or NULL for end of function.
20302 get_func_line(c
, cookie
, indent
)
20303 int c
; /* not used */
20305 int indent
; /* not used */
20307 funccall_T
*fcp
= (funccall_T
*)cookie
;
20308 ufunc_T
*fp
= fcp
->func
;
20310 garray_T
*gap
; /* growarray with function lines */
20312 /* If breakpoints have been added/deleted need to check for it. */
20313 if (fcp
->dbg_tick
!= debug_tick
)
20315 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
20317 fcp
->dbg_tick
= debug_tick
;
20319 #ifdef FEAT_PROFILE
20320 if (do_profiling
== PROF_YES
)
20321 func_line_end(cookie
);
20324 gap
= &fp
->uf_lines
;
20325 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
20330 /* Skip NULL lines (continuation lines). */
20331 while (fcp
->linenr
< gap
->ga_len
20332 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
20334 if (fcp
->linenr
>= gap
->ga_len
)
20338 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
20339 sourcing_lnum
= fcp
->linenr
;
20340 #ifdef FEAT_PROFILE
20341 if (do_profiling
== PROF_YES
)
20342 func_line_start(cookie
);
20347 /* Did we encounter a breakpoint? */
20348 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
20350 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
20351 /* Find next breakpoint. */
20352 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
20354 fcp
->dbg_tick
= debug_tick
;
20360 #if defined(FEAT_PROFILE) || defined(PROTO)
20362 * Called when starting to read a function line.
20363 * "sourcing_lnum" must be correct!
20364 * When skipping lines it may not actually be executed, but we won't find out
20365 * until later and we need to store the time now.
20368 func_line_start(cookie
)
20371 funccall_T
*fcp
= (funccall_T
*)cookie
;
20372 ufunc_T
*fp
= fcp
->func
;
20374 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
20375 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
20377 fp
->uf_tml_idx
= sourcing_lnum
- 1;
20378 /* Skip continuation lines. */
20379 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
20381 fp
->uf_tml_execed
= FALSE
;
20382 profile_start(&fp
->uf_tml_start
);
20383 profile_zero(&fp
->uf_tml_children
);
20384 profile_get_wait(&fp
->uf_tml_wait
);
20389 * Called when actually executing a function line.
20392 func_line_exec(cookie
)
20395 funccall_T
*fcp
= (funccall_T
*)cookie
;
20396 ufunc_T
*fp
= fcp
->func
;
20398 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
20399 fp
->uf_tml_execed
= TRUE
;
20403 * Called when done with a function line.
20406 func_line_end(cookie
)
20409 funccall_T
*fcp
= (funccall_T
*)cookie
;
20410 ufunc_T
*fp
= fcp
->func
;
20412 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
20414 if (fp
->uf_tml_execed
)
20416 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
20417 profile_end(&fp
->uf_tml_start
);
20418 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
20419 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
20420 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
20421 &fp
->uf_tml_children
);
20423 fp
->uf_tml_idx
= -1;
20429 * Return TRUE if the currently active function should be ended, because a
20430 * return was encountered or an error occured. Used inside a ":while".
20433 func_has_ended(cookie
)
20436 funccall_T
*fcp
= (funccall_T
*)cookie
;
20438 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20439 * an error inside a try conditional. */
20440 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
20445 * return TRUE if cookie indicates a function which "abort"s on errors.
20448 func_has_abort(cookie
)
20451 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
20454 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20457 VAR_FLAVOUR_DEFAULT
,
20458 VAR_FLAVOUR_SESSION
,
20459 VAR_FLAVOUR_VIMINFO
20462 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
20464 static var_flavour_T
20465 var_flavour(varname
)
20468 char_u
*p
= varname
;
20470 if (ASCII_ISUPPER(*p
))
20473 if (ASCII_ISLOWER(*p
))
20474 return VAR_FLAVOUR_SESSION
;
20475 return VAR_FLAVOUR_VIMINFO
;
20478 return VAR_FLAVOUR_DEFAULT
;
20482 #if defined(FEAT_VIMINFO) || defined(PROTO)
20484 * Restore global vars that start with a capital from the viminfo file
20487 read_viminfo_varlist(virp
, writing
)
20492 int is_string
= FALSE
;
20495 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
20497 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
20500 *tab
++ = '\0'; /* isolate the variable name */
20501 if (*tab
== 'S') /* string var */
20504 tab
= vim_strchr(tab
, '\t');
20509 tv
.v_type
= VAR_STRING
;
20510 tv
.vval
.v_string
= viminfo_readstring(virp
,
20511 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
20515 tv
.v_type
= VAR_NUMBER
;
20516 tv
.vval
.v_number
= atol((char *)tab
+ 1);
20518 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
20520 vim_free(tv
.vval
.v_string
);
20525 return viminfo_readline(virp
);
20529 * Write global vars that start with a capital to the viminfo file
20532 write_viminfo_varlist(fp
)
20536 dictitem_T
*this_var
;
20541 char_u numbuf
[NUMBUFLEN
];
20543 if (find_viminfo_parameter('!') == NULL
)
20546 fprintf(fp
, _("\n# global variables:\n"));
20548 todo
= (int)globvarht
.ht_used
;
20549 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
20551 if (!HASHITEM_EMPTY(hi
))
20554 this_var
= HI2DI(hi
);
20555 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
20557 switch (this_var
->di_tv
.v_type
)
20559 case VAR_STRING
: s
= "STR"; break;
20560 case VAR_NUMBER
: s
= "NUM"; break;
20563 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
20564 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
20566 viminfo_writestring(fp
, p
);
20574 #if defined(FEAT_SESSION) || defined(PROTO)
20576 store_session_globals(fd
)
20580 dictitem_T
*this_var
;
20584 todo
= (int)globvarht
.ht_used
;
20585 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
20587 if (!HASHITEM_EMPTY(hi
))
20590 this_var
= HI2DI(hi
);
20591 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
20592 || this_var
->di_tv
.v_type
== VAR_STRING
)
20593 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
20595 /* Escape special characters with a backslash. Turn a LF and
20596 * CR into \n and \r. */
20597 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
20598 (char_u
*)"\\\"\n\r");
20599 if (p
== NULL
) /* out of memory */
20601 for (t
= p
; *t
!= NUL
; ++t
)
20604 else if (*t
== '\r')
20606 if ((fprintf(fd
, "let %s = %c%s%c",
20608 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
20611 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
20613 || put_eol(fd
) == FAIL
)
20627 * Display script name where an item was last set.
20628 * Should only be invoked when 'verbose' is non-zero.
20631 last_set_msg(scriptID
)
20638 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
20642 MSG_PUTS(_("\n\tLast set from "));
20650 #endif /* FEAT_EVAL */
20652 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20657 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20659 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
20660 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
20661 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
20664 * Get the short pathname of a file.
20665 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20668 get_short_pathname(fnamep
, bufp
, fnamelen
)
20678 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
20681 /* If that doesn't work (not enough space), then save the string
20682 * and try again with a new buffer big enough
20684 newbuf
= vim_strnsave(*fnamep
, l
);
20685 if (newbuf
== NULL
)
20689 *fnamep
= *bufp
= newbuf
;
20691 l
= GetShortPathName(*fnamep
,*fnamep
,l
+1);
20693 /* Really should always succeed, as the buffer is big enough */
20701 * Create a short path name. Returns the length of the buffer it needs.
20702 * Doesn't copy over the end of the buffer passed in.
20705 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
20710 char_u
*s
, *p
, *pbuf2
, *pbuf3
;
20712 int len
, len2
, plen
, slen
;
20716 pbuf2
= vim_strnsave(*fname
, len2
);
20719 s
= pbuf2
+ len2
- 1; /* Find the end */
20723 if (after_pathsep(pbuf2
, s
+ 1))
20732 /* Go back one path-seperator */
20733 while (s
> pbuf2
&& !after_pathsep(pbuf2
, s
+ 1))
20742 /* Remeber the character that is about to be blatted */
20744 *s
= 0; /* get_short_pathname requires a null-terminated string */
20746 /* Try it in situ */
20748 if (!get_short_pathname(&p
, &pbuf3
, &plen
))
20753 *s
= ch
; /* Preserve the string */
20754 } while (plen
== 0);
20758 /* Remeber the length of the new string. */
20759 *fnamelen
= len
= plen
+ slen
;
20763 /* If there's not enough space in the currently allocated string,
20764 * then copy it to a buffer big enough.
20766 *fname
= *bufp
= vim_strnsave(p
, len
);
20767 if (*fname
== NULL
)
20772 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20773 *fname
= *bufp
= pbuf2
;
20775 strncpy(*fname
, p
, plen
);
20778 /* Concat the next bit */
20779 strncpy(*fname
+ plen
, s
, slen
);
20780 (*fname
)[len
] = '\0';
20788 * Get a pathname for a partial path.
20791 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
20796 int sepcount
, len
, tflen
;
20798 char_u
*pbuf
, *tfname
;
20801 /* Count up the path seperators from the RHS.. so we know which part
20802 * of the path to return.
20805 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
20806 if (vim_ispathsep(*p
))
20809 /* Need full path first (use expand_env() to remove a "~/") */
20810 hasTilde
= (**fnamep
== '~');
20812 pbuf
= tfname
= expand_env_save(*fnamep
);
20814 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
20816 len
= tflen
= (int)STRLEN(tfname
);
20818 if (!get_short_pathname(&tfname
, &pbuf
, &len
))
20823 /* Don't have a valid filename, so shorten the rest of the
20824 * path if we can. This CAN give us invalid 8.3 filenames, but
20825 * there's not a lot of point in guessing what it might be.
20828 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == -1)
20832 /* Count the paths backward to find the beginning of the desired string. */
20833 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
20837 p
-= mb_head_off(tfname
, p
);
20839 if (vim_ispathsep(*p
))
20841 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
20858 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20860 *fnamelen
= (int)STRLEN(p
);
20866 #endif /* WIN3264 */
20869 * Adjust a filename, according to a string of modifiers.
20870 * *fnamep must be NUL terminated when called. When returning, the length is
20871 * determined by *fnamelen.
20872 * Returns valid flags.
20873 * When there is an error, *fnamep is set to NULL.
20876 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
20877 char_u
*src
; /* string with modifiers */
20878 int *usedlen
; /* characters after src that are used */
20879 char_u
**fnamep
; /* file name so far */
20880 char_u
**bufp
; /* buffer for allocated file name or NULL */
20881 int *fnamelen
; /* length of fnamep */
20885 char_u
*s
, *p
, *pbuf
;
20886 char_u dirname
[MAXPATHL
];
20888 int has_fullname
= 0;
20890 int has_shortname
= 0;
20894 /* ":p" - full path/file_name */
20895 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
20899 valid
|= VALID_PATH
;
20902 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20903 if ((*fnamep
)[0] == '~'
20904 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20905 && ((*fnamep
)[1] == '/'
20906 # ifdef BACKSLASH_IN_FILENAME
20907 || (*fnamep
)[1] == '\\'
20909 || (*fnamep
)[1] == NUL
)
20914 *fnamep
= expand_env_save(*fnamep
);
20915 vim_free(*bufp
); /* free any allocated file name */
20917 if (*fnamep
== NULL
)
20921 /* When "/." or "/.." is used: force expansion to get rid of it. */
20922 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
20924 if (vim_ispathsep(*p
)
20927 || vim_ispathsep(p
[2])
20929 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
20933 /* FullName_save() is slow, don't use it when not needed. */
20934 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
20936 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
20937 vim_free(*bufp
); /* free any allocated file name */
20939 if (*fnamep
== NULL
)
20943 /* Append a path separator to a directory. */
20944 if (mch_isdir(*fnamep
))
20946 /* Make room for one or two extra characters. */
20947 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
20948 vim_free(*bufp
); /* free any allocated file name */
20950 if (*fnamep
== NULL
)
20952 add_pathsep(*fnamep
);
20956 /* ":." - path relative to the current directory */
20957 /* ":~" - path relative to the home directory */
20958 /* ":8" - shortname path - postponed till after */
20959 while (src
[*usedlen
] == ':'
20960 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
20966 has_shortname
= 1; /* Postpone this. */
20971 /* Need full path first (use expand_env() to remove a "~/") */
20974 if (c
== '.' && **fnamep
== '~')
20975 p
= pbuf
= expand_env_save(*fnamep
);
20977 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
20988 mch_dirname(dirname
, MAXPATHL
);
20989 s
= shorten_fname(p
, dirname
);
20995 vim_free(*bufp
); /* free any allocated file name */
21003 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
21004 /* Only replace it when it starts with '~' */
21005 if (*dirname
== '~')
21007 s
= vim_strsave(dirname
);
21020 tail
= gettail(*fnamep
);
21021 *fnamelen
= (int)STRLEN(*fnamep
);
21023 /* ":h" - head, remove "/file_name", can be repeated */
21024 /* Don't remove the first "/" or "c:\" */
21025 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
21027 valid
|= VALID_HEAD
;
21029 s
= get_past_head(*fnamep
);
21030 while (tail
> s
&& after_pathsep(s
, tail
))
21032 *fnamelen
= (int)(tail
- *fnamep
);
21035 *fnamelen
+= 1; /* the path separator is part of the path */
21037 while (tail
> s
&& !after_pathsep(s
, tail
))
21038 mb_ptr_back(*fnamep
, tail
);
21041 /* ":8" - shortname */
21042 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
21051 /* Check shortname after we have done 'heads' and before we do 'tails'
21056 /* Copy the string if it is shortened by :h */
21057 if (*fnamelen
< (int)STRLEN(*fnamep
))
21059 p
= vim_strnsave(*fnamep
, *fnamelen
);
21063 *bufp
= *fnamep
= p
;
21066 /* Split into two implementations - makes it easier. First is where
21067 * there isn't a full name already, second is where there is.
21069 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
21071 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == -1)
21078 /* Simple case, already have the full-name
21079 * Nearly always shorter, so try first time. */
21081 if (!get_short_pathname(fnamep
, bufp
, &l
))
21086 /* Couldn't find the filename.. search the paths.
21089 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == -1)
21095 #endif /* WIN3264 */
21097 /* ":t" - tail, just the basename */
21098 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
21101 *fnamelen
-= (int)(tail
- *fnamep
);
21105 /* ":e" - extension, can be repeated */
21106 /* ":r" - root, without extension, can be repeated */
21107 while (src
[*usedlen
] == ':'
21108 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
21110 /* find a '.' in the tail:
21111 * - for second :e: before the current fname
21112 * - otherwise: The last '.'
21114 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
21117 s
= *fnamep
+ *fnamelen
- 1;
21118 for ( ; s
> tail
; --s
)
21121 if (src
[*usedlen
+ 1] == 'e') /* :e */
21125 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
21128 /* cut version from the extension */
21129 s
= *fnamep
+ *fnamelen
- 1;
21130 for ( ; s
> *fnamep
; --s
)
21134 *fnamelen
= s
- *fnamep
;
21137 else if (*fnamep
<= tail
)
21142 if (s
> tail
) /* remove one extension */
21143 *fnamelen
= (int)(s
- *fnamep
);
21148 /* ":s?pat?foo?" - substitute */
21149 /* ":gs?pat?foo?" - global substitute */
21150 if (src
[*usedlen
] == ':'
21151 && (src
[*usedlen
+ 1] == 's'
21152 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
21161 flags
= (char_u
*)"";
21162 s
= src
+ *usedlen
+ 2;
21163 if (src
[*usedlen
+ 1] == 'g')
21165 flags
= (char_u
*)"g";
21172 /* find end of pattern */
21173 p
= vim_strchr(s
, sep
);
21176 pat
= vim_strnsave(s
, (int)(p
- s
));
21180 /* find end of substitution */
21181 p
= vim_strchr(s
, sep
);
21184 sub
= vim_strnsave(s
, (int)(p
- s
));
21185 str
= vim_strnsave(*fnamep
, *fnamelen
);
21186 if (sub
!= NULL
&& str
!= NULL
)
21188 *usedlen
= (int)(p
+ 1 - src
);
21189 s
= do_string_sub(str
, pat
, sub
, flags
);
21193 *fnamelen
= (int)STRLEN(s
);
21205 /* after using ":s", repeat all the modifiers */
21215 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21216 * "flags" can be "g" to do a global substitute.
21217 * Returns an allocated string, NULL for error.
21220 do_string_sub(str
, pat
, sub
, flags
)
21227 regmatch_T regmatch
;
21235 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21237 p_cpo
= (char_u
*)"";
21239 ga_init2(&ga
, 1, 200);
21241 do_all
= (flags
[0] == 'g');
21243 regmatch
.rm_ic
= p_ic
;
21244 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
21245 if (regmatch
.regprog
!= NULL
)
21248 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
21251 * Get some space for a temporary buffer to do the substitution
21252 * into. It will contain:
21253 * - The text up to where the match is.
21254 * - The substituted text.
21255 * - The text after the match.
21257 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
21258 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
21259 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
21265 /* copy the text up to where the match is */
21266 i
= (int)(regmatch
.startp
[0] - tail
);
21267 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
21268 /* add the substituted text */
21269 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
21270 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
21271 ga
.ga_len
+= i
+ sublen
- 1;
21272 /* avoid getting stuck on a match with an empty string */
21273 if (tail
== regmatch
.endp
[0])
21277 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
21282 tail
= regmatch
.endp
[0];
21290 if (ga
.ga_data
!= NULL
)
21291 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
21293 vim_free(regmatch
.regprog
);
21296 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
21303 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */